2
2
// for details. All rights reserved. Use of this source code is governed by a
3
3
// BSD-style license that can be found in the LICENSE file.
4
4
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 ;
7
11
8
12
import 'package:analyzer/dart/element/element.dart' ;
9
13
import 'package:analyzer/dart/element/nullability_suffix.dart' ;
@@ -12,15 +16,16 @@ import 'package:dartdoc/src/model/comment_referable.dart';
12
16
import 'package:dartdoc/src/model/model.dart' ;
13
17
import 'package:dartdoc/src/model/model_object_builder.dart' ;
14
18
import 'package:dartdoc/src/render/element_type_renderer.dart' ;
19
+ import 'package:dartdoc/src/runtime_stats.dart' ;
15
20
import 'package:dartdoc/src/type_utils.dart' ;
16
21
import 'package:meta/meta.dart' ;
17
22
18
23
mixin ElementTypeBuilderImpl implements ElementTypeBuilder {
19
24
PackageGraph get packageGraph;
20
25
21
26
@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);
24
29
}
25
30
26
31
/// Base class representing a type in Dartdoc. It wraps a [DartType] , and
@@ -36,19 +41,20 @@ abstract class ElementType
36
41
37
42
final String nullabilitySuffix;
38
43
39
- ElementType (this .type, this .library, this .packageGraph)
44
+ ElementType ._ (this .type, this .library, this .packageGraph)
40
45
: nullabilitySuffix = type.nullabilitySuffixWithin (library);
41
46
42
47
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;
45
51
if (fElement == null ||
46
52
fElement.kind == ElementKind .DYNAMIC ||
47
53
fElement.kind == ElementKind .NEVER ) {
48
- return UndefinedElementType ._from (f , library, packageGraph);
54
+ return UndefinedElementType ._from (type , library, packageGraph);
49
55
}
50
56
var modelElement = packageGraph.modelBuilder.fromElement (fElement);
51
- return DefinedElementType ._from (f , modelElement, library, packageGraph);
57
+ return DefinedElementType ._from (type , modelElement, library, packageGraph);
52
58
}
53
59
54
60
bool get canHaveParameters => false ;
@@ -74,24 +80,26 @@ abstract class ElementType
74
80
/// An [ElementType] that isn't pinned to an [Element] (or one that is, but
75
81
/// whose element is irrelevant).
76
82
class UndefinedElementType extends ElementType {
77
- UndefinedElementType (super .f, super .library, super .packageGraph);
83
+ UndefinedElementType ._(super .type, super .library, super .packageGraph)
84
+ : super ._();
78
85
79
86
factory UndefinedElementType ._from (
80
- DartType f , Library library, PackageGraph packageGraph) {
87
+ DartType type , Library library, PackageGraph packageGraph) {
81
88
// [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);
85
93
}
86
- return AliasedUndefinedElementType (f , library, packageGraph);
94
+ return AliasedUndefinedElementType ._(type , library, packageGraph);
87
95
}
88
- if (f is RecordType ) {
89
- return RecordElementType (f , library, packageGraph);
96
+ if (type is RecordType ) {
97
+ return RecordElementType ._(type , library, packageGraph);
90
98
}
91
- if (f is FunctionType ) {
92
- return FunctionTypeElementType (f , library, packageGraph);
99
+ if (type is FunctionType ) {
100
+ return FunctionTypeElementType ._(type , library, packageGraph);
93
101
}
94
- return UndefinedElementType (f , library, packageGraph);
102
+ return UndefinedElementType ._(type , library, packageGraph);
95
103
}
96
104
97
105
@override
@@ -141,8 +149,9 @@ class UndefinedElementType extends ElementType {
141
149
/// A [FunctionType] that does not have an underpinning [Element] .
142
150
class FunctionTypeElementType extends UndefinedElementType
143
151
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 ._();
146
155
147
156
List <TypeParameter > get typeFormals => type.typeFormals
148
157
.map ((p) => packageGraph.modelBuilder.from (p, library) as TypeParameter )
@@ -158,7 +167,8 @@ class FunctionTypeElementType extends UndefinedElementType
158
167
159
168
/// A [RecordType] which does not have an underpinning Element.
160
169
class 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 ._();
162
172
163
173
@override
164
174
String get name => 'Record' ;
@@ -177,25 +187,26 @@ class RecordElementType extends UndefinedElementType with Rendered {
177
187
178
188
class AliasedUndefinedFunctionElementType extends AliasedUndefinedElementType
179
189
with Callable {
180
- AliasedUndefinedFunctionElementType (
181
- super .f, super .library, super .packageGraph);
190
+ AliasedUndefinedFunctionElementType ._(
191
+ super .type, super .library, super .packageGraph)
192
+ : super ._();
182
193
}
183
194
184
195
class AliasedUndefinedElementType extends UndefinedElementType
185
196
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 ._();
190
200
191
201
@override
192
202
ElementTypeRenderer get _renderer =>
193
203
packageGraph.rendererFactory.aliasedUndefinedElementTypeRenderer;
194
204
}
195
205
196
206
class 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 ._();
199
210
200
211
@override
201
212
ParameterizedType get type => super .type as ParameterizedType ;
@@ -229,9 +240,10 @@ mixin Aliased implements ElementType, ModelBuilderInterface {
229
240
}
230
241
231
242
class AliasedElementType extends ParameterizedElementType with Aliased {
232
- AliasedElementType (
243
+ AliasedElementType ._ (
233
244
super .type, super .library, super .packageGraph, super .element)
234
- : assert (type.alias? .element != null );
245
+ : assert (type.alias != null ),
246
+ super ._();
235
247
236
248
@override
237
249
ParameterizedType get type;
@@ -242,8 +254,9 @@ class AliasedElementType extends ParameterizedElementType with Aliased {
242
254
}
243
255
244
256
class 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 ._();
247
260
248
261
@override
249
262
TypeParameterType get type => super .type as TypeParameterType ;
@@ -262,25 +275,26 @@ class TypeParameterElementType extends DefinedElementType {
262
275
abstract class DefinedElementType extends ElementType {
263
276
final ModelElement modelElement;
264
277
265
- DefinedElementType (
266
- super .type, super .library, super .packageGraph, this .modelElement);
278
+ DefinedElementType ._(
279
+ super .type, super .library, super .packageGraph, this .modelElement)
280
+ : super ._();
267
281
268
- factory DefinedElementType ._from (DartType f , ModelElement modelElement,
282
+ factory DefinedElementType ._from (DartType type , ModelElement modelElement,
269
283
Library library, PackageGraph packageGraph) {
270
284
// `TypeAliasElement.alias.element` has different implications.
271
285
// In that case it is an actual type alias of some kind (generic or
272
286
// otherwise). Here however `alias.element` signals that this is a type
273
287
// 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);
277
291
}
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);
280
295
}
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);
284
298
}
285
299
286
300
@override
@@ -392,8 +406,9 @@ mixin Rendered implements ElementType {
392
406
/// A callable type that may or may not be backed by a declaration using the
393
407
/// generic function syntax.
394
408
class 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 ._();
397
412
398
413
@override
399
414
String get name => super .name.isNotEmpty ? super .name : 'Function' ;
0 commit comments