Move typeVariablesCount to TypeDeclarationBuilder

Also move checks and normalization to the wrapped builder.

Change-Id: Ica8da07bf9191b997b8e00ed0af8cc70a93f9216
Reviewed-on: https://dart-review.googlesource.com/59044
Commit-Queue: Peter von der Ahé <ahe@google.com>
Reviewed-by: Dmitry Stefantsov <dmitryas@google.com>
diff --git a/pkg/front_end/lib/src/fasta/builder/builtin_type_builder.dart b/pkg/front_end/lib/src/fasta/builder/builtin_type_builder.dart
index 40e9cdf..b362d75 100644
--- a/pkg/front_end/lib/src/fasta/builder/builtin_type_builder.dart
+++ b/pkg/front_end/lib/src/fasta/builder/builtin_type_builder.dart
@@ -6,7 +6,7 @@
 
 import 'builder.dart' show LibraryBuilder, TypeBuilder, TypeDeclarationBuilder;
 
-class BuiltinTypeBuilder<T extends TypeBuilder, R>
+abstract class BuiltinTypeBuilder<T extends TypeBuilder, R>
     extends TypeDeclarationBuilder<T, R> {
   final R type;
 
diff --git a/pkg/front_end/lib/src/fasta/builder/class_builder.dart b/pkg/front_end/lib/src/fasta/builder/class_builder.dart
index 1fc5bc5..a0d314c 100644
--- a/pkg/front_end/lib/src/fasta/builder/class_builder.dart
+++ b/pkg/front_end/lib/src/fasta/builder/class_builder.dart
@@ -225,7 +225,5 @@
     library.addProblem(message, charOffset, length, fileUri, context: context);
   }
 
-  int get typeVariablesCount;
-
   void prepareTopLevelInference() {}
 }
diff --git a/pkg/front_end/lib/src/fasta/builder/named_type_builder.dart b/pkg/front_end/lib/src/fasta/builder/named_type_builder.dart
index a302a71..f430cfc 100644
--- a/pkg/front_end/lib/src/fasta/builder/named_type_builder.dart
+++ b/pkg/front_end/lib/src/fasta/builder/named_type_builder.dart
@@ -4,7 +4,7 @@
 
 library fasta.named_type_builder;
 
-import '../fasta_codes.dart' show Message;
+import '../fasta_codes.dart' show Message, templateTypeArgumentMismatch;
 
 import 'builder.dart'
     show
@@ -53,6 +53,27 @@
     declaration = buildInvalidType(charOffset, fileUri);
   }
 
+  @override
+  void check(int charOffset, Uri fileUri) {
+    if (arguments != null &&
+        arguments.length != declaration.typeVariablesCount) {
+      declaration = buildInvalidType(
+          charOffset,
+          fileUri,
+          templateTypeArgumentMismatch.withArguments(
+              name, declaration.typeVariablesCount));
+    }
+  }
+
+  @override
+  void normalize(int charOffset, Uri fileUri) {
+    if (arguments != null &&
+        arguments.length != declaration.typeVariablesCount) {
+      // [arguments] will be normalized later if they are null.
+      arguments = null;
+    }
+  }
+
   String get debugName => "NamedTypeBuilder";
 
   StringBuffer printOn(StringBuffer buffer) {
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 273009a..0b2d80a 100644
--- a/pkg/front_end/lib/src/fasta/builder/type_builder.dart
+++ b/pkg/front_end/lib/src/fasta/builder/type_builder.dart
@@ -12,6 +12,12 @@
 
   void resolveIn(Scope scope, int charOffset, Uri fileUri) {}
 
+  /// See `UnresolvedType.checkType`.
+  void check(int charOffset, Uri fileUri) {}
+
+  /// See `UnresolvedType.normalizeType`.
+  void normalize(int charOffset, Uri fileUri) {}
+
   void bind(TypeDeclarationBuilder builder) {}
 
   /// May return null, for example, for mixin applications.
diff --git a/pkg/front_end/lib/src/fasta/builder/type_declaration_builder.dart b/pkg/front_end/lib/src/fasta/builder/type_declaration_builder.dart
index c8ec2d5..a7a9771 100644
--- a/pkg/front_end/lib/src/fasta/builder/type_declaration_builder.dart
+++ b/pkg/front_end/lib/src/fasta/builder/type_declaration_builder.dart
@@ -31,11 +31,13 @@
 
   bool get isMixinApplication => false;
 
+  @override
+  String get fullNameForErrors => name;
+
+  int get typeVariablesCount => 0;
+
   R buildType(LibraryBuilder library, List<T> arguments);
 
   /// [arguments] have already been built.
   R buildTypesWithBuiltArguments(LibraryBuilder library, List<R> arguments);
-
-  @override
-  String get fullNameForErrors => name;
 }
diff --git a/pkg/front_end/lib/src/fasta/builder/unresolved_type.dart b/pkg/front_end/lib/src/fasta/builder/unresolved_type.dart
index bddec58..392e15e 100644
--- a/pkg/front_end/lib/src/fasta/builder/unresolved_type.dart
+++ b/pkg/front_end/lib/src/fasta/builder/unresolved_type.dart
@@ -4,16 +4,7 @@
 
 library fasta.unresolved_type;
 
-import '../fasta_codes.dart' show templateTypeArgumentMismatch;
-
-import 'builder.dart'
-    show
-        ClassBuilder,
-        FunctionTypeAliasBuilder,
-        NamedTypeBuilder,
-        Scope,
-        TypeBuilder,
-        TypeDeclarationBuilder;
+import 'builder.dart' show Scope, TypeBuilder;
 
 /// A wrapper around a type that is yet to be resolved.
 class UnresolvedType<T extends TypeBuilder> {
@@ -26,50 +17,8 @@
   void resolveIn(Scope scope) => builder.resolveIn(scope, charOffset, fileUri);
 
   /// Performs checks on the type after it's resolved.
-  void checkType() {
-    TypeBuilder resolvedType = builder;
-    if (resolvedType is NamedTypeBuilder) {
-      TypeDeclarationBuilder declaration = resolvedType.declaration;
-      if (declaration is ClassBuilder) {
-        if (resolvedType.arguments != null &&
-            resolvedType.arguments.length != declaration.typeVariablesCount) {
-          resolvedType.declaration = resolvedType.buildInvalidType(
-              charOffset,
-              fileUri,
-              templateTypeArgumentMismatch.withArguments(
-                  resolvedType.name, declaration.typeVariablesCount));
-        }
-      } else if (declaration is FunctionTypeAliasBuilder) {
-        if (resolvedType.arguments != null &&
-            resolvedType.arguments.length != declaration.typeVariablesCount) {
-          resolvedType.declaration = resolvedType.buildInvalidType(
-              charOffset,
-              fileUri,
-              templateTypeArgumentMismatch.withArguments(
-                  resolvedType.name, declaration.typeVariablesCount));
-        }
-      }
-    }
-  }
+  void checkType() => builder.check(charOffset, fileUri);
 
   /// Normalizes the type arguments in accordance with Dart 1 semantics.
-  void normalizeType() {
-    TypeBuilder resolvedType = builder;
-    if (resolvedType is NamedTypeBuilder) {
-      TypeDeclarationBuilder declaration = resolvedType.declaration;
-      if (declaration is ClassBuilder) {
-        if (resolvedType.arguments != null &&
-            resolvedType.arguments.length != declaration.typeVariablesCount) {
-          // [resolveType.arguments] will be normalized later if they are null.
-          resolvedType.arguments = null;
-        }
-      } else if (declaration is FunctionTypeAliasBuilder) {
-        if (resolvedType.arguments != null &&
-            resolvedType.arguments.length != declaration.typeVariablesCount) {
-          // [resolveType.arguments] will be normalized later if they are null.
-          resolvedType.arguments = null;
-        }
-      }
-    }
-  }
+  void normalizeType() => builder.normalize(charOffset, fileUri);
 }