[cfe] Remove use of TypeUse.functionSignature

 - and remove VariableDeclarationImpl.functionNestingLevel.

Change-Id: Iee7c619794bc34f1009ec17d03ec34680dba1283
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/245363
Commit-Queue: Johnni Winther <johnniwinther@google.com>
Reviewed-by: Chloe Stefantsova <cstefantsova@google.com>
diff --git a/pkg/front_end/lib/src/fasta/builder/formal_parameter_builder.dart b/pkg/front_end/lib/src/fasta/builder/formal_parameter_builder.dart
index 2832049..57e4d89 100644
--- a/pkg/front_end/lib/src/fasta/builder/formal_parameter_builder.dart
+++ b/pkg/front_end/lib/src/fasta/builder/formal_parameter_builder.dart
@@ -142,12 +142,11 @@
   @override
   String get fullNameForErrors => name;
 
-  VariableDeclaration build(
-      SourceLibraryBuilder library, int functionNestingLevel) {
+  VariableDeclaration build(SourceLibraryBuilder library) {
     if (variable == null) {
       DartType? builtType = type?.build(library, TypeUse.parameterType);
       variable = new VariableDeclarationImpl(
-          name == noNameSentinel ? null : name, functionNestingLevel,
+          name == noNameSentinel ? null : name,
           type: builtType,
           isFinal: isFinal,
           isConst: false,
diff --git a/pkg/front_end/lib/src/fasta/builder/type_builder.dart b/pkg/front_end/lib/src/fasta/builder/type_builder.dart
index 0090e6f..4582b5c 100644
--- a/pkg/front_end/lib/src/fasta/builder/type_builder.dart
+++ b/pkg/front_end/lib/src/fasta/builder/type_builder.dart
@@ -131,12 +131,9 @@
   ///
   typedefAlias,
 
-  /// An internally created function type used when build local functions.
-  functionSignature,
-
   /// The this type of an enum.
-  // TODO(johnniwinther): This is doesn't currently have the correct value
-  // and/or well-boundedness checking.
+  // TODO(johnniwinther): This doesn't currently have the correct value and/or
+  //  well-boundedness checking.
   enumSelfType,
 
   /// A type used as a type literal.
diff --git a/pkg/front_end/lib/src/fasta/incremental_compiler.dart b/pkg/front_end/lib/src/fasta/incremental_compiler.dart
index ef54d70..58d6b73 100644
--- a/pkg/front_end/lib/src/fasta/incremental_compiler.dart
+++ b/pkg/front_end/lib/src/fasta/incremental_compiler.dart
@@ -1978,7 +1978,7 @@
           typeParameters: typeDefinitions,
           positionalParameters: definitions.keys
               .map<VariableDeclaration>((name) =>
-                  new VariableDeclarationImpl(name, 0, type: definitions[name])
+                  new VariableDeclarationImpl(name, type: definitions[name])
                     ..fileOffset = cls?.fileOffset ??
                         extension?.fileOffset ??
                         libraryBuilder.library.fileOffset)
diff --git a/pkg/front_end/lib/src/fasta/kernel/body_builder.dart b/pkg/front_end/lib/src/fasta/kernel/body_builder.dart
index 6f25365..61927d1 100644
--- a/pkg/front_end/lib/src/fasta/kernel/body_builder.dart
+++ b/pkg/front_end/lib/src/fasta/kernel/body_builder.dart
@@ -3265,8 +3265,7 @@
     bool isLate = (currentLocalVariableModifiers & lateMask) != 0;
     bool isRequired = (currentLocalVariableModifiers & requiredMask) != 0;
     assert(isConst == (constantContext == ConstantContext.inferred));
-    VariableDeclaration variable = new VariableDeclarationImpl(
-        identifier.name, functionNestingLevel,
+    VariableDeclaration variable = new VariableDeclarationImpl(identifier.name,
         forSyntheticToken: identifier.token.isSynthetic,
         initializer: initializer,
         type: currentLocalVariableType,
@@ -4383,8 +4382,7 @@
           fileUri: uri)
         ..hasDeclaredInitializer = (initializerStart != null);
     }
-    VariableDeclaration variable =
-        parameter.build(libraryBuilder, functionNestingLevel);
+    VariableDeclaration variable = parameter.build(libraryBuilder);
     Expression? initializer = name?.initializer;
     if (initializer != null) {
       if (member is RedirectingFactoryBuilder) {
@@ -4568,11 +4566,10 @@
       int parameterCount = catchParameters!.parameters!.length;
       if (parameterCount > 0) {
         exception = catchParameters.parameters![0];
-        exception.build(libraryBuilder, functionNestingLevel).type =
-            exceptionType;
+        exception.build(libraryBuilder).type = exceptionType;
         if (parameterCount > 1) {
           stackTrace = catchParameters.parameters![1];
-          stackTrace.build(libraryBuilder, functionNestingLevel).type =
+          stackTrace.build(libraryBuilder).type =
               coreTypes.stackTraceRawType(libraryBuilder.nonNullable);
         }
       }
@@ -5803,8 +5800,7 @@
     debugEvent("FunctionName");
     Identifier name = pop() as Identifier;
     Token nameToken = name.token;
-    VariableDeclaration variable = new VariableDeclarationImpl(
-        name.name, functionNestingLevel,
+    VariableDeclaration variable = new VariableDeclarationImpl(name.name,
         forSyntheticToken: nameToken.isSynthetic,
         isFinal: true,
         isLocalFunction: true)
@@ -6171,8 +6167,7 @@
       }
     } else {
       VariableDeclaration variable = elements.syntheticVariableDeclaration =
-          forest.createVariableDeclaration(
-              offsetForToken(forToken), null, functionNestingLevel,
+          forest.createVariableDeclaration(offsetForToken(forToken), null,
               isFinal: true);
       if (lvalue is Generator) {
         /// We are in this case, where `lvalue` isn't a [VariableDeclaration]:
@@ -7644,37 +7639,49 @@
 
   FunctionNode buildFunctionNode(
       SourceLibraryBuilder library,
-      TypeBuilder? returnType,
-      List<TypeVariableBuilder>? typeParameters,
+      TypeBuilder? returnTypeBuilder,
+      List<TypeVariableBuilder>? typeVariableBuilders,
       AsyncMarker asyncModifier,
       Statement body,
       int fileEndOffset) {
-    // TODO(johnniwinther): Avoid creating a FunctionTypeBuilder to create
-    // the function. The function type is not written as a type by the user
-    // and shouldn't be checked as such.
-    FunctionType type = toFunctionType(
-            returnType, const NullabilityBuilder.omitted(), typeParameters)
-        .build(library, TypeUse.functionSignature) as FunctionType;
+    DartType returnType =
+        returnTypeBuilder?.build(library, TypeUse.returnType) ??
+            const DynamicType();
+    int requiredParameterCount = 0;
     List<VariableDeclaration> positionalParameters = <VariableDeclaration>[];
     List<VariableDeclaration> namedParameters = <VariableDeclaration>[];
     if (parameters != null) {
-      for (FormalParameterBuilder parameter in parameters!) {
-        if (parameter.isNamed) {
-          namedParameters.add(parameter.variable!);
-        } else {
-          positionalParameters.add(parameter.variable!);
+      for (FormalParameterBuilder formal in parameters!) {
+        VariableDeclaration parameter = formal.build(
+          library,
+        );
+        if (formal.isPositional) {
+          positionalParameters.add(parameter);
+          if (formal.isRequiredPositional) requiredParameterCount++;
+        } else if (formal.isNamed) {
+          namedParameters.add(parameter);
         }
       }
       namedParameters.sort((VariableDeclaration a, VariableDeclaration b) {
         return a.name!.compareTo(b.name!);
       });
     }
+
+    List<TypeParameter>? typeParameters;
+    if (typeVariableBuilders != null) {
+      typeParameters = <TypeParameter>[];
+      for (TypeVariableBuilder t in typeVariableBuilders) {
+        typeParameters.add(t.parameter);
+        // Build the bound to detect cycles in typedefs.
+        t.bound?.build(library, TypeUse.typeParameterBound);
+      }
+    }
     return new FunctionNode(body,
-        typeParameters: type.typeParameters,
+        typeParameters: typeParameters,
         positionalParameters: positionalParameters,
         namedParameters: namedParameters,
-        requiredParameterCount: type.requiredParameterCount,
-        returnType: type.returnType,
+        requiredParameterCount: requiredParameterCount,
+        returnType: returnType,
         asyncMarker: asyncModifier)
       ..fileOffset = charOffset
       ..fileEndOffset = fileEndOffset;
diff --git a/pkg/front_end/lib/src/fasta/kernel/forest.dart b/pkg/front_end/lib/src/fasta/kernel/forest.dart
index 73decad..8593b65 100644
--- a/pkg/front_end/lib/src/fasta/kernel/forest.dart
+++ b/pkg/front_end/lib/src/fasta/kernel/forest.dart
@@ -646,8 +646,7 @@
 
   /// Creates [VariableDeclaration] for a variable named [name] at the given
   /// [functionNestingLevel].
-  VariableDeclaration createVariableDeclaration(
-      int fileOffset, String? name, int functionNestingLevel,
+  VariableDeclaration createVariableDeclaration(int fileOffset, String? name,
       {Expression? initializer,
       DartType? type,
       bool isFinal: false,
@@ -657,7 +656,7 @@
       bool isLocalFunction: false}) {
     // ignore: unnecessary_null_comparison
     assert(fileOffset != null);
-    return new VariableDeclarationImpl(name, functionNestingLevel,
+    return new VariableDeclarationImpl(name,
         type: type,
         initializer: initializer,
         isFinal: isFinal,
diff --git a/pkg/front_end/lib/src/fasta/kernel/internal_ast.dart b/pkg/front_end/lib/src/fasta/kernel/internal_ast.dart
index 0fd1abd..01e408e 100644
--- a/pkg/front_end/lib/src/fasta/kernel/internal_ast.dart
+++ b/pkg/front_end/lib/src/fasta/kernel/internal_ast.dart
@@ -1589,10 +1589,6 @@
   /// the kernel.
   final bool isImplicitlyTyped;
 
-  // TODO(ahe): Remove this field. We can get rid of it by recording closure
-  // mutation in [BodyBuilder].
-  final int functionNestingLevel;
-
   // TODO(ahe): Remove this field. It's only used locally when compiling a
   // method, and this can thus be tracked in a [Set] (actually, tracking this
   // information in a [List] is probably even faster as the average size will
@@ -1615,7 +1611,7 @@
   /// used.
   bool isStaticLate;
 
-  VariableDeclarationImpl(String? name, this.functionNestingLevel,
+  VariableDeclarationImpl(String? name,
       {this.forSyntheticToken: false,
       bool hasDeclaredInitializer: false,
       Expression? initializer,
@@ -1645,7 +1641,6 @@
 
   VariableDeclarationImpl.forEffect(Expression initializer)
       : forSyntheticToken = false,
-        functionNestingLevel = 0,
         isImplicitlyTyped = false,
         isLocalFunction = false,
         isStaticLate = false,
@@ -1653,7 +1648,6 @@
 
   VariableDeclarationImpl.forValue(Expression initializer)
       : forSyntheticToken = false,
-        functionNestingLevel = 0,
         isImplicitlyTyped = true,
         isLocalFunction = false,
         isStaticLate = false,
diff --git a/pkg/front_end/lib/src/fasta/source/source_function_builder.dart b/pkg/front_end/lib/src/fasta/source/source_function_builder.dart
index e52c631..082584c 100644
--- a/pkg/front_end/lib/src/fasta/source/source_function_builder.dart
+++ b/pkg/front_end/lib/src/fasta/source/source_function_builder.dart
@@ -343,7 +343,7 @@
     }
     if (formals != null) {
       for (FormalParameterBuilder formal in formals!) {
-        VariableDeclaration parameter = formal.build(libraryBuilder, 0);
+        VariableDeclaration parameter = formal.build(libraryBuilder);
         if (needsCheckVisitor != null) {
           if (parameter.type.accept(needsCheckVisitor)) {
             parameter.isCovariantByClass = true;
@@ -378,8 +378,7 @@
       // Replace illegal parameters by single dummy parameter.
       // Do this after building the parameters, since the diet listener
       // assumes that parameters are built, even if illegal in number.
-      VariableDeclaration parameter =
-          new VariableDeclarationImpl("#synthetic", 0);
+      VariableDeclaration parameter = new VariableDeclarationImpl("#synthetic");
       function.positionalParameters.clear();
       function.positionalParameters.add(parameter);
       parameter.parent = function;
diff --git a/pkg/front_end/lib/src/fasta/source/source_library_builder.dart b/pkg/front_end/lib/src/fasta/source/source_library_builder.dart
index 963b623..ffad317 100644
--- a/pkg/front_end/lib/src/fasta/source/source_library_builder.dart
+++ b/pkg/front_end/lib/src/fasta/source/source_library_builder.dart
@@ -4791,7 +4791,6 @@
         case TypeUse.typeParameterDefaultType:
         case TypeUse.defaultTypeAsTypeArgument:
         case TypeUse.deferredTypeError:
-        case TypeUse.functionSignature:
           break;
       }
     }
diff --git a/pkg/front_end/test/text_representation/internal_ast_text_representation_test.dart b/pkg/front_end/test/text_representation/internal_ast_text_representation_test.dart
index b5fffb7..a1d7543 100644
--- a/pkg/front_end/test/text_representation/internal_ast_text_representation_test.dart
+++ b/pkg/front_end/test/text_representation/internal_ast_text_representation_test.dart
@@ -516,8 +516,8 @@
 
 void _testFunctionDeclarationImpl() {
   testStatement(
-      new FunctionDeclarationImpl(new VariableDeclarationImpl('foo', 0),
-          new FunctionNode(new Block([]))),
+      new FunctionDeclarationImpl(
+          new VariableDeclarationImpl('foo'), new FunctionNode(new Block([]))),
       '''
 dynamic foo() {}''');
 }
@@ -591,7 +591,7 @@
 
 void _testNamedFunctionExpressionJudgment() {
   testExpression(
-      new NamedFunctionExpressionJudgment(new VariableDeclarationImpl('foo', 0,
+      new NamedFunctionExpressionJudgment(new VariableDeclarationImpl('foo',
           initializer:
               new FunctionExpression(new FunctionNode(new Block([]))))),
       '''
@@ -687,13 +687,13 @@
 }
 
 void _testVariableDeclarationImpl() {
-  testStatement(new VariableDeclarationImpl('foo', 0), '''
+  testStatement(new VariableDeclarationImpl('foo'), '''
 dynamic foo;''');
   testStatement(
-      new VariableDeclarationImpl('foo', 0, initializer: new IntLiteral(0)), '''
+      new VariableDeclarationImpl('foo', initializer: new IntLiteral(0)), '''
 dynamic foo = 0;''');
   testStatement(
-      new VariableDeclarationImpl('foo', 0,
+      new VariableDeclarationImpl('foo',
           type: const VoidType(),
           initializer: new IntLiteral(0),
           isFinal: true,
@@ -701,20 +701,20 @@
       '''
 required final void foo;''');
   testStatement(
-      new VariableDeclarationImpl('foo', 0,
+      new VariableDeclarationImpl('foo',
           type: const VoidType(), initializer: new IntLiteral(0), isLate: true),
       '''
 late void foo = 0;''');
   testStatement(
-      new VariableDeclarationImpl('foo', 0,
+      new VariableDeclarationImpl('foo',
           type: const VoidType(), initializer: new IntLiteral(0))
-        ..lateGetter = new VariableDeclarationImpl('foo#getter', 0),
+        ..lateGetter = new VariableDeclarationImpl('foo#getter'),
       '''
 late void foo = 0;''');
   testStatement(
-      new VariableDeclarationImpl('foo', 0,
+      new VariableDeclarationImpl('foo',
           type: const VoidType(), initializer: new IntLiteral(0))
-        ..lateGetter = new VariableDeclarationImpl('foo#getter', 0)
+        ..lateGetter = new VariableDeclarationImpl('foo#getter')
         ..lateType = const DynamicType(),
       '''
 late dynamic foo = 0;''');