Remove DeclaredSimpleIdentifier and AstFactoryImpl.

Change-Id: Ifaf4d2c134a4be075afe75c3b8c841644b14da3d
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/308500
Reviewed-by: Samuel Rawlins <srawlins@google.com>
Commit-Queue: Konstantin Shcheglov <scheglov@google.com>
diff --git a/pkg/analysis_server/lib/src/services/correction/dart/remove_leading_underscore.dart b/pkg/analysis_server/lib/src/services/correction/dart/remove_leading_underscore.dart
index 67b2fb8..1af1643 100644
--- a/pkg/analysis_server/lib/src/services/correction/dart/remove_leading_underscore.dart
+++ b/pkg/analysis_server/lib/src/services/correction/dart/remove_leading_underscore.dart
@@ -30,10 +30,7 @@
     final node = this.node;
     final Token? nameToken;
     final Element? element;
-    if (node is DeclaredSimpleIdentifier) {
-      nameToken = node.token;
-      element = node.staticElement;
-    } else if (node is SimpleIdentifier) {
+    if (node is SimpleIdentifier) {
       nameToken = node.token;
       element = node.staticElement;
     } else if (node is FormalParameter) {
diff --git a/pkg/analyzer/lib/src/dart/ast/ast.dart b/pkg/analyzer/lib/src/dart/ast/ast.dart
index faefa3e..cc3ff84 100644
--- a/pkg/analyzer/lib/src/dart/ast/ast.dart
+++ b/pkg/analyzer/lib/src/dart/ast/ast.dart
@@ -5332,22 +5332,6 @@
   }
 }
 
-/// A simple identifier that declares a name.
-// TODO(rnystrom): Consider making this distinct from [SimpleIdentifier] and
-// get rid of all of the:
-//
-//     if (node.inDeclarationContext()) { ... }
-//
-// code and instead visit this separately. A declaration is semantically pretty
-// different from a use, so using the same node type doesn't seem to buy us
-// much.
-final class DeclaredSimpleIdentifier extends SimpleIdentifierImpl {
-  DeclaredSimpleIdentifier(super.token);
-
-  @override
-  bool inDeclarationContext() => true;
-}
-
 /// A variable pattern that declares a variable.
 ///
 ///    variablePattern ::=
@@ -16894,7 +16878,17 @@
   E? accept<E>(AstVisitor<E> visitor) => visitor.visitSimpleIdentifier(this);
 
   @override
-  bool inDeclarationContext() => false;
+  bool inDeclarationContext() {
+    final parent = this.parent;
+    switch (parent) {
+      case ImportDirective():
+        return parent.prefix == this;
+      case Label():
+        final parent2 = parent.parent;
+        return parent2 is Statement || parent2 is SwitchMember;
+    }
+    return false;
+  }
 
   @override
   bool inGetterContext() {
diff --git a/pkg/analyzer/lib/src/dart/ast/ast_factory.dart b/pkg/analyzer/lib/src/dart/ast/ast_factory.dart
deleted file mode 100644
index 59f3390..0000000
--- a/pkg/analyzer/lib/src/dart/ast/ast_factory.dart
+++ /dev/null
@@ -1,19 +0,0 @@
-// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
-// for details. All rights reserved. Use of this source code is governed by a
-// BSD-style license that can be found in the LICENSE file.
-
-import 'package:_fe_analyzer_shared/src/scanner/token.dart';
-import 'package:analyzer/src/dart/ast/ast.dart';
-
-/// The instance of [AstFactoryImpl].
-final AstFactoryImpl astFactory = AstFactoryImpl();
-
-class AstFactoryImpl {
-  SimpleIdentifierImpl simpleIdentifier(Token token,
-      {bool isDeclaration = false}) {
-    if (isDeclaration) {
-      return DeclaredSimpleIdentifier(token);
-    }
-    return SimpleIdentifierImpl(token);
-  }
-}
diff --git a/pkg/analyzer/lib/src/dart/constant/evaluation.dart b/pkg/analyzer/lib/src/dart/constant/evaluation.dart
index bdd6cf5..185fc9d 100644
--- a/pkg/analyzer/lib/src/dart/constant/evaluation.dart
+++ b/pkg/analyzer/lib/src/dart/constant/evaluation.dart
@@ -15,7 +15,6 @@
 import 'package:analyzer/error/error.dart';
 import 'package:analyzer/error/listener.dart';
 import 'package:analyzer/src/dart/ast/ast.dart';
-import 'package:analyzer/src/dart/ast/ast_factory.dart';
 import 'package:analyzer/src/dart/ast/extensions.dart';
 import 'package:analyzer/src/dart/ast/token.dart';
 import 'package:analyzer/src/dart/constant/from_environment_evaluator.dart';
@@ -2459,7 +2458,7 @@
     var positionalIndex = 0;
     for (var parameter in _constructor.parameters) {
       if (parameter is SuperFormalParameterElement) {
-        var value = astFactory.simpleIdentifier(
+        var value = SimpleIdentifierImpl(
           StringToken(TokenType.STRING, parameter.name, -1),
         )
           ..staticElement = parameter
@@ -2470,7 +2469,7 @@
           superArguments.add(
             NamedExpressionImpl(
               name: LabelImpl(
-                label: astFactory.simpleIdentifier(
+                label: SimpleIdentifierImpl(
                   StringToken(TokenType.STRING, parameter.name, -1),
                 )..staticElement = parameter,
                 colon: StringToken(TokenType.COLON, ':', -1),
diff --git a/pkg/analyzer/lib/src/dart/element/element.dart b/pkg/analyzer/lib/src/dart/element/element.dart
index 15f82cc..c65375c 100644
--- a/pkg/analyzer/lib/src/dart/element/element.dart
+++ b/pkg/analyzer/lib/src/dart/element/element.dart
@@ -21,7 +21,6 @@
 import 'package:analyzer/src/dart/analysis/experiments.dart';
 import 'package:analyzer/src/dart/analysis/session.dart';
 import 'package:analyzer/src/dart/ast/ast.dart';
-import 'package:analyzer/src/dart/ast/ast_factory.dart';
 import 'package:analyzer/src/dart/ast/token.dart';
 import 'package:analyzer/src/dart/constant/compute.dart';
 import 'package:analyzer/src/dart/constant/evaluation.dart';
@@ -970,7 +969,7 @@
               substitution.substituteType(superParameter.type);
           implicitParameters.add(implicitParameter);
           argumentsForSuperInvocation.add(
-            astFactory.simpleIdentifier(
+            SimpleIdentifierImpl(
               StringToken(TokenType.STRING, implicitParameter.name, -1),
             )
               ..staticElement = implicitParameter
@@ -990,7 +989,7 @@
           superKeyword: Tokens.super_(),
           period: isNamed ? Tokens.period() : null,
           constructorName: isNamed
-              ? (astFactory.simpleIdentifier(
+              ? (SimpleIdentifierImpl(
                   StringToken(TokenType.STRING, superclassConstructor.name, -1),
                 )..staticElement = superclassConstructor)
               : null,
diff --git a/pkg/analyzer/lib/src/dart/micro/utils.dart b/pkg/analyzer/lib/src/dart/micro/utils.dart
index 9b54045..7e67313 100644
--- a/pkg/analyzer/lib/src/dart/micro/utils.dart
+++ b/pkg/analyzer/lib/src/dart/micro/utils.dart
@@ -15,9 +15,6 @@
   if (node == null) {
     return null;
   }
-  if (node is DeclaredSimpleIdentifier) {
-    node = node.parent;
-  }
   if (node is SimpleIdentifier && node.parent is LibraryIdentifier) {
     node = node.parent;
   }
diff --git a/pkg/analyzer/lib/src/fasta/ast_builder.dart b/pkg/analyzer/lib/src/fasta/ast_builder.dart
index 2a5daa7..f701da5 100644
--- a/pkg/analyzer/lib/src/fasta/ast_builder.dart
+++ b/pkg/analyzer/lib/src/fasta/ast_builder.dart
@@ -60,7 +60,6 @@
 import 'package:analyzer/source/line_info.dart';
 import 'package:analyzer/src/dart/analysis/experiments.dart';
 import 'package:analyzer/src/dart/ast/ast.dart';
-import 'package:analyzer/src/dart/ast/ast_factory.dart';
 import 'package:analyzer/src/dart/ast/extensions.dart';
 import 'package:analyzer/src/dart/error/syntactic_errors.dart';
 import 'package:analyzer/src/fasta/error_converter.dart';
@@ -71,8 +70,6 @@
 
 /// A parser listener that builds the analyzer's AST structure.
 class AstBuilder extends StackListener {
-  final AstFactoryImpl ast = astFactory;
-
   final FastaErrorReporter errorReporter;
   final Uri fileUri;
   ScriptTagImpl? scriptTag;
@@ -332,16 +329,11 @@
     var metadata = pop() as List<AnnotationImpl>?;
     var comment = _findComment(metadata, extensionKeyword);
 
-    SimpleIdentifierImpl? name;
-    if (nameToken != null) {
-      name = ast.simpleIdentifier(nameToken, isDeclaration: true);
-    }
-
     _classLikeBuilder = _ExtensionDeclarationBuilder(
       comment: comment,
       metadata: metadata,
       extensionKeyword: extensionKeyword,
-      name: name?.token,
+      name: nameToken,
       typeParameters: typeParameters,
       leftBracket: Tokens.openCurlyBracket(),
       rightBracket: Tokens.closeCurlyBracket(),
@@ -662,7 +654,9 @@
         // Recovery:
         // Parser has reported invalid assignment.
         var superExpression = left as SuperExpressionImpl;
-        fieldName = ast.simpleIdentifier(superExpression.superKeyword);
+        fieldName = SimpleIdentifierImpl(
+          superExpression.superKeyword,
+        );
       }
       return ConstructorFieldInitializerImpl(
         thisKeyword: thisKeyword,
@@ -815,8 +809,7 @@
       // upon the type of expression. e.g. "x.this" -> templateThisAsIdentifier
       handleRecoverableError(
           templateExpectedIdentifier.withArguments(token), token, token);
-      SimpleIdentifierImpl identifier =
-          ast.simpleIdentifier(token, isDeclaration: false);
+      SimpleIdentifierImpl identifier = SimpleIdentifierImpl(token);
       push(
         PropertyAccessImpl(
           target: receiver,
@@ -916,7 +909,7 @@
         }
         push(
           FunctionExpressionInvocationImpl(
-            function: ast.simpleIdentifier(assertKeyword),
+            function: SimpleIdentifierImpl(assertKeyword),
             typeArguments: null,
             argumentList: ArgumentListImpl(
               leftParenthesis: leftParenthesis,
@@ -1219,7 +1212,7 @@
       externalKeyword: modifiers?.externalKeyword,
       constKeyword: modifiers?.finalConstOrVarKeyword,
       factoryKeyword: null,
-      returnType: ast.simpleIdentifier(prefixOrName.token),
+      returnType: SimpleIdentifierImpl(prefixOrName.token),
       period: period,
       name: nameOrNull?.token,
       parameters: parameters,
@@ -1290,15 +1283,14 @@
     // the actual constructor name.
     SimpleIdentifierImpl returnType;
     Token? period;
-    SimpleIdentifierImpl? name;
+    Token? nameToken;
     IdentifierImpl typeName = constructorName;
     if (typeName is SimpleIdentifierImpl) {
       returnType = typeName;
     } else if (typeName is PrefixedIdentifierImpl) {
       returnType = typeName.prefix;
       period = typeName.period;
-      name =
-          ast.simpleIdentifier(typeName.identifier.token, isDeclaration: true);
+      nameToken = typeName.identifier.token;
     } else {
       throw UnimplementedError();
     }
@@ -1310,9 +1302,9 @@
         externalKeyword: modifiers?.externalKeyword,
         constKeyword: modifiers?.finalConstOrVarKeyword,
         factoryKeyword: factoryKeyword,
-        returnType: ast.simpleIdentifier(returnType.token),
+        returnType: SimpleIdentifierImpl(returnType.token),
         period: period,
-        name: name?.token,
+        name: nameToken,
         parameters: parameters,
         separator: separator,
         initializers: null,
@@ -3915,12 +3907,12 @@
     Token? secondPeriod,
     Token thirdToken,
   ) {
-    var identifier = ast.simpleIdentifier(thirdToken);
+    var identifier = SimpleIdentifierImpl(thirdToken);
     if (firstToken != null) {
       var target = PrefixedIdentifierImpl(
-        prefix: ast.simpleIdentifier(firstToken),
+        prefix: SimpleIdentifierImpl(firstToken),
         period: firstPeriod!,
-        identifier: ast.simpleIdentifier(secondToken!),
+        identifier: SimpleIdentifierImpl(secondToken!),
       );
       var expression = PropertyAccessImpl(
         target: target,
@@ -3935,7 +3927,7 @@
       );
     } else if (secondToken != null) {
       var expression = PrefixedIdentifierImpl(
-        prefix: ast.simpleIdentifier(secondToken),
+        prefix: SimpleIdentifierImpl(secondToken),
         period: secondPeriod!,
         identifier: identifier,
       );
@@ -4319,7 +4311,7 @@
         if (!leftParenthesis.next!.isIdentifier) {
           parser.rewriter.insertSyntheticIdentifier(leftParenthesis);
         }
-        variableOrDeclaration = ast.simpleIdentifier(leftParenthesis.next!);
+        variableOrDeclaration = SimpleIdentifierImpl(leftParenthesis.next!);
       }
       forLoopParts = ForEachPartsWithIdentifierImpl(
         identifier: variableOrDeclaration,
@@ -4407,8 +4399,7 @@
       return;
     }
 
-    final identifier =
-        ast.simpleIdentifier(token, isDeclaration: context.inDeclaration);
+    final identifier = SimpleIdentifierImpl(token);
     if (context.inLibraryOrPartOfDeclaration) {
       if (!context.isContinuation) {
         push([identifier]);
@@ -4567,8 +4558,12 @@
     assert(optional('operator', operatorKeyword));
     debugEvent("InvalidOperatorName");
 
-    push(_OperatorName(
-        operatorKeyword, ast.simpleIdentifier(token, isDeclaration: true)));
+    push(
+      _OperatorName(
+        operatorKeyword,
+        SimpleIdentifierImpl(token),
+      ),
+    );
   }
 
   @override
@@ -4984,7 +4979,9 @@
     debugEvent("NoTypeNameInConstructorReference");
     final builder = _classLikeBuilder as _EnumDeclarationBuilder;
 
-    push(ast.simpleIdentifier(builder.name));
+    push(
+      SimpleIdentifierImpl(builder.name),
+    );
   }
 
   @override
@@ -5077,8 +5074,12 @@
     assert(token.type.isUserDefinableOperator);
     debugEvent("OperatorName");
 
-    push(_OperatorName(
-        operatorKeyword, ast.simpleIdentifier(token, isDeclaration: true)));
+    push(
+      _OperatorName(
+        operatorKeyword,
+        SimpleIdentifierImpl(token),
+      ),
+    );
   }
 
   @override
@@ -5800,7 +5801,7 @@
   }
 
   SimpleIdentifierImpl _tmpSimpleIdentifier() {
-    return ast.simpleIdentifier(
+    return SimpleIdentifierImpl(
       StringToken(TokenType.STRING, '__tmp', -1),
     );
   }
diff --git a/pkg/analyzer/lib/src/summary2/ast_binary_reader.dart b/pkg/analyzer/lib/src/summary2/ast_binary_reader.dart
index 6d26bfb..79d3d6f 100644
--- a/pkg/analyzer/lib/src/summary2/ast_binary_reader.dart
+++ b/pkg/analyzer/lib/src/summary2/ast_binary_reader.dart
@@ -7,7 +7,6 @@
 import 'package:analyzer/dart/element/element.dart';
 import 'package:analyzer/dart/element/type.dart';
 import 'package:analyzer/src/dart/ast/ast.dart';
-import 'package:analyzer/src/dart/ast/ast_factory.dart';
 import 'package:analyzer/src/dart/ast/token.dart';
 import 'package:analyzer/src/dart/element/element.dart';
 import 'package:analyzer/src/dart/element/type.dart';
@@ -889,7 +888,7 @@
   NamedExpression _readNamedExpression() {
     var name = _readStringReference();
     var nameNode = LabelImpl(
-      label: astFactory.simpleIdentifier(
+      label: SimpleIdentifierImpl(
         StringToken(TokenType.STRING, name, -1),
       ),
       colon: Tokens.colon(),
@@ -1186,7 +1185,7 @@
 
   SimpleIdentifier _readSimpleIdentifier() {
     var name = _readStringReference();
-    var node = astFactory.simpleIdentifier(
+    var node = SimpleIdentifierImpl(
       StringToken(TokenType.STRING, name, -1),
     );
     node.staticElement = _reader.readElement();
diff --git a/pkg/analyzer/lib/src/summary2/element_builder.dart b/pkg/analyzer/lib/src/summary2/element_builder.dart
index 97ac1f5..6606f706 100644
--- a/pkg/analyzer/lib/src/summary2/element_builder.dart
+++ b/pkg/analyzer/lib/src/summary2/element_builder.dart
@@ -6,7 +6,6 @@
 import 'package:analyzer/dart/ast/visitor.dart';
 import 'package:analyzer/dart/element/element.dart';
 import 'package:analyzer/src/dart/ast/ast.dart';
-import 'package:analyzer/src/dart/ast/ast_factory.dart';
 import 'package:analyzer/src/dart/ast/invokes_super_self.dart';
 import 'package:analyzer/src/dart/ast/token.dart';
 import 'package:analyzer/src/dart/element/element.dart';
@@ -263,7 +262,7 @@
           ),
           period: constructorName != null ? Tokens.period() : null,
           name: constructorName != null
-              ? astFactory.simpleIdentifier(
+              ? SimpleIdentifierImpl(
                   StringToken(TokenType.STRING, constructorName, -1),
                 )
               : null,
@@ -296,7 +295,7 @@
       field.constantInitializer = initializer;
       holder.addNonSyntheticField(field);
       valuesElements.add(
-        astFactory.simpleIdentifier(
+        SimpleIdentifierImpl(
           StringToken(TokenType.STRING, name, -1),
         ),
       );
diff --git a/pkg/analyzer/lib/src/summary2/not_serializable_nodes.dart b/pkg/analyzer/lib/src/summary2/not_serializable_nodes.dart
index 7af770e..0a9b7ea 100644
--- a/pkg/analyzer/lib/src/summary2/not_serializable_nodes.dart
+++ b/pkg/analyzer/lib/src/summary2/not_serializable_nodes.dart
@@ -5,7 +5,6 @@
 import 'package:analyzer/dart/ast/token.dart';
 import 'package:analyzer/dart/ast/visitor.dart';
 import 'package:analyzer/src/dart/ast/ast.dart';
-import 'package:analyzer/src/dart/ast/ast_factory.dart';
 import 'package:analyzer/src/dart/ast/token.dart';
 
 const _notSerializableName = '_notSerializableExpression';
@@ -22,7 +21,7 @@
   if (visitor.result) {
     return node;
   }
-  return astFactory.simpleIdentifier(
+  return SimpleIdentifierImpl(
     StringToken(TokenType.STRING, _notSerializableName, -1),
   );
 }
diff --git a/pkg/analyzer/test/generated/resolver_test_case.dart b/pkg/analyzer/test/generated/resolver_test_case.dart
index 8ed3d77..41cbc51 100644
--- a/pkg/analyzer/test/generated/resolver_test_case.dart
+++ b/pkg/analyzer/test/generated/resolver_test_case.dart
@@ -216,10 +216,6 @@
       return;
     }
 
-    if (node is DeclaredSimpleIdentifier) {
-      return;
-    }
-
     var staticType = node.staticType;
     if (staticType is DynamicType && node.staticElement == null) {
       return;