22// for details. All rights reserved. Use of this source code is governed by a
33// BSD-style license that can be found in the LICENSE file.
44
5- /// The models used to represent Dart code.
6- library dartdoc.element_type;
5+ /// The models used to represent Dart types, all subclasses of [ElementType] .
6+ ///
7+ /// The only entrypoint for constructing these classes is
8+ /// [ElementTypeBuilderImpl.typeFrom] , which delegates instantiation to various
9+ /// factories.
10+ library ;
711
812import 'package:analyzer/dart/element/element.dart' ;
913import 'package:analyzer/dart/element/nullability_suffix.dart' ;
@@ -12,15 +16,16 @@ import 'package:dartdoc/src/model/comment_referable.dart';
1216import 'package:dartdoc/src/model/model.dart' ;
1317import 'package:dartdoc/src/model/model_object_builder.dart' ;
1418import 'package:dartdoc/src/render/element_type_renderer.dart' ;
19+ import 'package:dartdoc/src/runtime_stats.dart' ;
1520import 'package:dartdoc/src/type_utils.dart' ;
1621import 'package:meta/meta.dart' ;
1722
1823mixin ElementTypeBuilderImpl implements ElementTypeBuilder {
1924 PackageGraph get packageGraph;
2025
2126 @override
22- ElementType typeFrom (DartType f , Library library) =>
23- ElementType ._from (f , library, packageGraph);
27+ ElementType typeFrom (DartType type , Library library) =>
28+ ElementType ._from (type , library, packageGraph);
2429}
2530
2631/// Base class representing a type in Dartdoc. It wraps a [DartType] , and
@@ -36,19 +41,20 @@ abstract class ElementType
3641
3742 final String nullabilitySuffix;
3843
39- ElementType (this .type, this .library, this .packageGraph)
44+ ElementType ._ (this .type, this .library, this .packageGraph)
4045 : nullabilitySuffix = type.nullabilitySuffixWithin (library);
4146
4247 factory ElementType ._from (
43- DartType f, Library library, PackageGraph packageGraph) {
44- var fElement = f.documentableElement;
48+ DartType type, Library library, PackageGraph packageGraph) {
49+ runtimeStats.incrementAccumulator ('elementTypeInstantiation' );
50+ var fElement = type.documentableElement;
4551 if (fElement == null ||
4652 fElement.kind == ElementKind .DYNAMIC ||
4753 fElement.kind == ElementKind .NEVER ) {
48- return UndefinedElementType ._from (f , library, packageGraph);
54+ return UndefinedElementType ._from (type , library, packageGraph);
4955 }
5056 var modelElement = packageGraph.modelBuilder.fromElement (fElement);
51- return DefinedElementType ._from (f , modelElement, library, packageGraph);
57+ return DefinedElementType ._from (type , modelElement, library, packageGraph);
5258 }
5359
5460 bool get canHaveParameters => false ;
@@ -74,24 +80,26 @@ abstract class ElementType
7480/// An [ElementType] that isn't pinned to an [Element] (or one that is, but
7581/// whose element is irrelevant).
7682class UndefinedElementType extends ElementType {
77- UndefinedElementType (super .f, super .library, super .packageGraph);
83+ UndefinedElementType ._(super .type, super .library, super .packageGraph)
84+ : super ._();
7885
7986 factory UndefinedElementType ._from (
80- DartType f , Library library, PackageGraph packageGraph) {
87+ DartType type , Library library, PackageGraph packageGraph) {
8188 // [UndefinedElementType]s.
82- if (f.alias? .element != null ) {
83- if (f is FunctionType ) {
84- return AliasedUndefinedFunctionElementType (f, library, packageGraph);
89+ if (type.alias != null ) {
90+ if (type is FunctionType ) {
91+ return AliasedUndefinedFunctionElementType ._(
92+ type, library, packageGraph);
8593 }
86- return AliasedUndefinedElementType (f , library, packageGraph);
94+ return AliasedUndefinedElementType ._(type , library, packageGraph);
8795 }
88- if (f is RecordType ) {
89- return RecordElementType (f , library, packageGraph);
96+ if (type is RecordType ) {
97+ return RecordElementType ._(type , library, packageGraph);
9098 }
91- if (f is FunctionType ) {
92- return FunctionTypeElementType (f , library, packageGraph);
99+ if (type is FunctionType ) {
100+ return FunctionTypeElementType ._(type , library, packageGraph);
93101 }
94- return UndefinedElementType (f , library, packageGraph);
102+ return UndefinedElementType ._(type , library, packageGraph);
95103 }
96104
97105 @override
@@ -141,8 +149,9 @@ class UndefinedElementType extends ElementType {
141149/// A [FunctionType] that does not have an underpinning [Element] .
142150class FunctionTypeElementType extends UndefinedElementType
143151 with Rendered , Callable {
144- FunctionTypeElementType (
145- FunctionType super .f, super .library, super .packageGraph);
152+ FunctionTypeElementType ._(
153+ FunctionType super .type, super .library, super .packageGraph)
154+ : super ._();
146155
147156 List <TypeParameter > get typeFormals => type.typeFormals
148157 .map ((p) => packageGraph.modelBuilder.from (p, library) as TypeParameter )
@@ -158,7 +167,8 @@ class FunctionTypeElementType extends UndefinedElementType
158167
159168/// A [RecordType] which does not have an underpinning Element.
160169class RecordElementType extends UndefinedElementType with Rendered {
161- RecordElementType (RecordType super .f, super .library, super .packageGraph);
170+ RecordElementType ._(RecordType super .type, super .library, super .packageGraph)
171+ : super ._();
162172
163173 @override
164174 String get name => 'Record' ;
@@ -177,25 +187,26 @@ class RecordElementType extends UndefinedElementType with Rendered {
177187
178188class AliasedUndefinedFunctionElementType extends AliasedUndefinedElementType
179189 with Callable {
180- AliasedUndefinedFunctionElementType (
181- super .f, super .library, super .packageGraph);
190+ AliasedUndefinedFunctionElementType ._(
191+ super .type, super .library, super .packageGraph)
192+ : super ._();
182193}
183194
184195class AliasedUndefinedElementType extends UndefinedElementType
185196 with Aliased , Rendered {
186- AliasedUndefinedElementType (super .f, super .library, super .packageGraph) {
187- assert (type.alias? .element != null );
188- assert (type.alias? .typeArguments != null );
189- }
197+ AliasedUndefinedElementType ._(super .type, super .library, super .packageGraph)
198+ : assert (type.alias != null ),
199+ super ._();
190200
191201 @override
192202 ElementTypeRenderer get _renderer =>
193203 packageGraph.rendererFactory.aliasedUndefinedElementTypeRenderer;
194204}
195205
196206class ParameterizedElementType extends DefinedElementType with Rendered {
197- ParameterizedElementType (ParameterizedType super .type, super .library,
198- super .packageGraph, super .element);
207+ ParameterizedElementType ._(ParameterizedType super .type, super .library,
208+ super .packageGraph, super .element)
209+ : super ._();
199210
200211 @override
201212 ParameterizedType get type => super .type as ParameterizedType ;
@@ -229,9 +240,10 @@ mixin Aliased implements ElementType, ModelBuilderInterface {
229240}
230241
231242class AliasedElementType extends ParameterizedElementType with Aliased {
232- AliasedElementType (
243+ AliasedElementType ._ (
233244 super .type, super .library, super .packageGraph, super .element)
234- : assert (type.alias? .element != null );
245+ : assert (type.alias != null ),
246+ super ._();
235247
236248 @override
237249 ParameterizedType get type;
@@ -242,8 +254,9 @@ class AliasedElementType extends ParameterizedElementType with Aliased {
242254}
243255
244256class TypeParameterElementType extends DefinedElementType {
245- TypeParameterElementType (TypeParameterType super .type, super .library,
246- super .packageGraph, super .element);
257+ TypeParameterElementType ._(TypeParameterType super .type, super .library,
258+ super .packageGraph, super .element)
259+ : super ._();
247260
248261 @override
249262 TypeParameterType get type => super .type as TypeParameterType ;
@@ -262,25 +275,26 @@ class TypeParameterElementType extends DefinedElementType {
262275abstract class DefinedElementType extends ElementType {
263276 final ModelElement modelElement;
264277
265- DefinedElementType (
266- super .type, super .library, super .packageGraph, this .modelElement);
278+ DefinedElementType ._(
279+ super .type, super .library, super .packageGraph, this .modelElement)
280+ : super ._();
267281
268- factory DefinedElementType ._from (DartType f , ModelElement modelElement,
282+ factory DefinedElementType ._from (DartType type , ModelElement modelElement,
269283 Library library, PackageGraph packageGraph) {
270284 // `TypeAliasElement.alias.element` has different implications.
271285 // In that case it is an actual type alias of some kind (generic or
272286 // otherwise). Here however `alias.element` signals that this is a type
273287 // referring to an alias.
274- if (f is ! TypeAliasElement && f .alias? .element != null ) {
275- return AliasedElementType (
276- f as ParameterizedType , library, packageGraph, modelElement);
288+ if (type is ! TypeAliasElement && type .alias != null ) {
289+ return AliasedElementType ._ (
290+ type as ParameterizedType , library, packageGraph, modelElement);
277291 }
278- if (f is TypeParameterType ) {
279- return TypeParameterElementType (f, library, packageGraph, modelElement);
292+ if (type is TypeParameterType ) {
293+ return TypeParameterElementType ._(
294+ type, library, packageGraph, modelElement);
280295 }
281- assert (f is ParameterizedType );
282- return ParameterizedElementType (
283- f as ParameterizedType , library, packageGraph, modelElement);
296+ return ParameterizedElementType ._(
297+ type as ParameterizedType , library, packageGraph, modelElement);
284298 }
285299
286300 @override
@@ -392,8 +406,9 @@ mixin Rendered implements ElementType {
392406/// A callable type that may or may not be backed by a declaration using the
393407/// generic function syntax.
394408class CallableElementType extends DefinedElementType with Rendered , Callable {
395- CallableElementType (
396- FunctionType super .t, super .library, super .packageGraph, super .element);
409+ CallableElementType ._(
410+ FunctionType super .t, super .library, super .packageGraph, super .element)
411+ : super ._();
397412
398413 @override
399414 String get name => super .name.isNotEmpty ? super .name : 'Function' ;
0 commit comments