forked from dart-lang/dartdoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathelement_type.dart
450 lines (359 loc) · 14.2 KB
/
element_type.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
// Copyright (c) 2014, 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.
/// The models used to represent Dart code.
library dartdoc.element_type;
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/nullability_suffix.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:dartdoc/src/model/comment_referable.dart';
import 'package:dartdoc/src/model/model.dart';
import 'package:dartdoc/src/model/model_object_builder.dart';
import 'package:dartdoc/src/render/element_type_renderer.dart';
import 'package:dartdoc/src/type_utils.dart';
import 'package:meta/meta.dart';
mixin ElementTypeBuilderImpl implements ElementTypeBuilder {
PackageGraph get packageGraph;
@override
ElementType typeFrom(DartType f, Library library) =>
ElementType._from(f, library, packageGraph);
}
/// Base class representing a type in Dartdoc. It wraps a [DartType], and
/// may link to a [ModelElement].
abstract class ElementType extends Privacy
with CommentReferable, Nameable, ModelBuilder {
final DartType type;
@override
final PackageGraph packageGraph;
@override
final Library library;
final String nullabilitySuffix;
ElementType(this.type, this.library, this.packageGraph)
: nullabilitySuffix = type.nullabilitySuffixWithin(library);
factory ElementType._from(
DartType f, Library library, PackageGraph packageGraph) {
var fElement = DartTypeExtension(f).element;
if (fElement == null ||
fElement.kind == ElementKind.DYNAMIC ||
fElement.kind == ElementKind.NEVER) {
return UndefinedElementType._from(f, library, packageGraph);
}
var modelElement = packageGraph.modelBuilder.fromElement(fElement);
return DefinedElementType._from(f, modelElement, library, packageGraph);
}
/// The element of [type].
TypeDefiningElement? get typeElement => DartTypeExtension(type).element;
bool get canHaveParameters => false;
bool get isTypedef => false;
String get linkedName;
/// Name with generics and nullability indication.
String get nameWithGenerics;
DartType get instantiatedType;
Iterable<ElementType> get typeArguments;
bool isBoundSupertypeTo(ElementType t);
bool isSubtypeOf(ElementType t);
@override
String toString() => '$type';
}
/// An [ElementType] that isn't pinned to an [Element] (or one that is, but
/// whose element is irrelevant).
class UndefinedElementType extends ElementType {
UndefinedElementType(super.f, super.library, super.packageGraph);
factory UndefinedElementType._from(
DartType f, Library library, PackageGraph packageGraph) {
// [UndefinedElementType]s.
if (f.alias?.element != null) {
if (f is FunctionType) {
return AliasedUndefinedFunctionElementType(f, library, packageGraph);
}
return AliasedUndefinedElementType(f, library, packageGraph);
}
if (f is RecordType) {
return RecordElementType(f, library, packageGraph);
}
if (f is FunctionType) {
return FunctionTypeElementType(f, library, packageGraph);
}
return UndefinedElementType(f, library, packageGraph);
}
@override
bool get isPublic => true;
@override
String get name {
if (type is VoidType) return 'void';
if (type.isDynamic) return 'dynamic';
assert(const {'Never'}.contains(typeElement!.name),
'Unrecognized type for UndefinedElementType: ${type.toString()}');
return typeElement!.name!;
}
@override
String get nameWithGenerics => '$name$nullabilitySuffix';
/// Assume that undefined elements don't have useful bounds.
@override
DartType get instantiatedType => type;
@override
bool isBoundSupertypeTo(ElementType t) => false;
@override
bool isSubtypeOf(ElementType t) => type.isBottom && !t.type.isBottom;
@override
String get linkedName => name;
@override
Iterable<ElementType> get typeArguments => const [];
@override
Map<String, CommentReferable> get referenceChildren => const {};
@override
Iterable<CommentReferable> get referenceParents => const [];
@override
Iterable<CommentReferable>? get referenceGrandparentOverrides => null;
}
/// A [FunctionType] that does not have an underpinning [Element].
class FunctionTypeElementType extends UndefinedElementType
with Rendered, Callable {
FunctionTypeElementType(
FunctionType super.f, super.library, super.packageGraph);
List<TypeParameter> get typeFormals => type.typeFormals
.map((p) => packageGraph.modelBuilder.from(p, library) as TypeParameter)
.toList(growable: false);
@override
String get name => 'Function';
@override
ElementTypeRenderer get _renderer =>
packageGraph.rendererFactory.functionTypeElementTypeRenderer;
}
/// A [RecordType] which does not have an underpinning Element.
class RecordElementType extends UndefinedElementType with Rendered {
RecordElementType(RecordType super.f, super.library, super.packageGraph);
@override
String get name => 'Record';
@override
ElementTypeRenderer get _renderer =>
packageGraph.rendererFactory.recordElementTypeRenderer;
List<RecordTypeField> get positionalFields => type.positionalFields;
List<RecordTypeField> get namedFields => type.namedFields;
@override
RecordType get type => super.type as RecordType;
}
class AliasedUndefinedFunctionElementType extends AliasedUndefinedElementType
with Callable {
AliasedUndefinedFunctionElementType(
super.f, super.library, super.packageGraph);
}
class AliasedUndefinedElementType extends UndefinedElementType
with Aliased, Rendered {
AliasedUndefinedElementType(super.f, super.library, super.packageGraph) {
assert(type.alias?.element != null);
assert(type.alias?.typeArguments != null);
}
@override
ElementTypeRenderer get _renderer =>
packageGraph.rendererFactory.aliasedUndefinedElementTypeRenderer;
}
class ParameterizedElementType extends DefinedElementType with Rendered {
ParameterizedElementType(ParameterizedType super.type, super.library,
super.packageGraph, super.element);
@override
ParameterizedType get type => super.type as ParameterizedType;
@override
ElementTypeRenderer<ParameterizedElementType> get _renderer =>
packageGraph.rendererFactory.parameterizedElementTypeRenderer;
@override
late final Iterable<ElementType> typeArguments = type.typeArguments
.map((f) => modelBuilder.typeFrom(f, library))
.toList(growable: false);
}
/// A [ElementType] whose underlying type was referred to by a type alias.
mixin Aliased implements ElementType, ModelBuilderInterface {
late final Element typeAliasElement = type.alias!.element;
@override
String get name => typeAliasElement.name!;
@override
bool get isTypedef => true;
late final ModelElement aliasElement =
modelBuilder.fromElement(typeAliasElement);
late final Iterable<ElementType> aliasArguments = type.alias!.typeArguments
.map((f) => modelBuilder.typeFrom(f, library))
.toList(growable: false);
}
class AliasedElementType extends ParameterizedElementType with Aliased {
AliasedElementType(
super.type, super.library, super.packageGraph, super.element)
: assert(type.alias?.element != null);
@override
ParameterizedType get type;
/// Parameters, if available, for the underlying typedef.
late final List<Parameter> aliasedParameters =
modelElement.isCallable ? modelElement.parameters : [];
@override
ElementTypeRenderer<AliasedElementType> get _renderer =>
packageGraph.rendererFactory.aliasedElementTypeRenderer;
}
class TypeParameterElementType extends DefinedElementType {
TypeParameterElementType(TypeParameterType super.type, super.library,
super.packageGraph, super.element);
@override
TypeParameterType get type => super.type as TypeParameterType;
@override
String get linkedName => '$name$nullabilitySuffix';
@override
String get nameWithGenerics => '$name$nullabilitySuffix';
@override
DartType get _bound => type.bound;
}
/// An [ElementType] associated with an [Element].
abstract class DefinedElementType extends ElementType {
final ModelElement modelElement;
DefinedElementType(
super.type, super.library, super.packageGraph, this.modelElement);
factory DefinedElementType._from(DartType f, ModelElement modelElement,
Library library, PackageGraph packageGraph) {
// `TypeAliasElement.alias.element` has different implications.
// In that case it is an actual type alias of some kind (generic or
// otherwise). Here however `alias.element` signals that this is a type
// referring to an alias.
if (f is! TypeAliasElement && f.alias?.element != null) {
return AliasedElementType(
f as ParameterizedType, library, packageGraph, modelElement);
}
assert(f is ParameterizedType || f is TypeParameterType);
assert(f is! FunctionType,
'detected DefinedElementType for FunctionType: analyzer version too old?');
// TODO(jcollins-g): strip out all the cruft that's accumulated
// here for non-generic type aliases.
var isGenericTypeAlias = f.alias?.element != null && f is! InterfaceType;
if (isGenericTypeAlias) {
assert(false);
return GenericTypeAliasElementType(
f as TypeParameterType, library, packageGraph, modelElement);
}
if (f is TypeParameterType) {
return TypeParameterElementType(f, library, packageGraph, modelElement);
}
assert(f is ParameterizedType);
return ParameterizedElementType(
f as ParameterizedType, library, packageGraph, modelElement);
}
Element get element => modelElement.element;
@override
String get name => typeElement!.name!;
@override
String get fullyQualifiedName => modelElement.fullyQualifiedName;
bool get isParameterType => type is TypeParameterType;
/// Whether the underlying, canonical element is public.
///
/// This avoids discarding the resolved type information as canonicalization
/// would ordinarily do.
@override
bool get isPublic {
var canonicalClass = modelElement.packageGraph
.findCanonicalModelElementFor(modelElement.element) ??
modelElement;
return canonicalClass.isPublic;
}
DartType get _bound => type;
/// This type, instantiated to bounds if it isn't already.
@override
late final DartType instantiatedType = () {
final bound = _bound;
if (bound is InterfaceType &&
!bound.typeArguments.every((t) => t is InterfaceType)) {
return library.typeSystem.instantiateInterfaceToBounds(
element: bound.element, nullabilitySuffix: _bound.nullabilitySuffix);
} else {
return _bound;
}
}();
/// Returns whether the instantiated-to-bounds type of this type is a subtype
/// of [type].
@override
bool isSubtypeOf(ElementType type) =>
library.typeSystem.isSubtypeOf(instantiatedType, type.instantiatedType);
/// Whether at least one supertype (including via mixins and interfaces) is
/// equivalent to or a subtype of `this` when instantiated to bounds.
@override
bool isBoundSupertypeTo(ElementType t) {
var type = t.instantiatedType;
if (type is InterfaceType) {
var superTypes = type.allSupertypes;
for (var superType in superTypes) {
if (library.typeSystem.isSubtypeOf(superType, instantiatedType)) {
return true;
}
}
}
return false;
}
@override
Iterable<ElementType> get typeArguments => [];
@override
Map<String, CommentReferable> get referenceChildren =>
modelElement.referenceChildren;
@override
Iterable<CommentReferable> get referenceParents =>
modelElement.referenceParents;
@override
Iterable<CommentReferable>? get referenceGrandparentOverrides =>
modelElement.referenceGrandparentOverrides;
@internal
@override
CommentReferable get definingCommentReferable =>
modelBuilder.fromElement(element);
}
/// Any callable [ElementType] will mix-in this class, whether anonymous or not,
/// unless it is an alias reference.
mixin Callable on ElementType {
List<Parameter> get parameters => type.parameters
.map((p) => modelBuilder.from(p, library) as Parameter)
.toList(growable: false);
late final ElementType returnType =
modelBuilder.typeFrom(type.returnType, library);
@override
// TODO(jcollins-g): mustachio should not require this
String get linkedName;
@override
FunctionType get type => super.type as FunctionType;
}
/// This [ElementType] uses an [ElementTypeRenderer] to generate some of its
/// parameters.
mixin Rendered implements ElementType {
@override
late final String linkedName = _renderer.renderLinkedName(this);
@override
late final String nameWithGenerics = _renderer.renderNameWithGenerics(this);
ElementTypeRenderer<ElementType> get _renderer;
}
/// A callable type that may or may not be backed by a declaration using the
/// generic function syntax.
class CallableElementType extends DefinedElementType with Rendered, Callable {
CallableElementType(
FunctionType super.t, super.library, super.packageGraph, super.element);
@override
String get name => super.name.isNotEmpty ? super.name : 'Function';
@override
ElementTypeRenderer<CallableElementType> get _renderer =>
packageGraph.rendererFactory.callableElementTypeRenderer;
@override
late final Iterable<ElementType> typeArguments = type.alias?.typeArguments
.map((f) => modelBuilder.typeFrom(f, library))
.toList(growable: false) ??
const [];
}
/// A non-callable type backed by a [GenericTypeAliasElement].
class GenericTypeAliasElementType extends TypeParameterElementType {
GenericTypeAliasElementType(
super.t, super.library, super.packageGraph, super.element);
}
extension on DartType {
/// The dartdoc nullability suffix for this type in [library].
String nullabilitySuffixWithin(Library library) {
if (this is! VoidType && !isBottom) {
/// If a legacy type appears inside the public interface of a Null
/// safety library, we pretend it is nullable for the purpose of
/// documentation (since star-types are not supposed to be public).
if (nullabilitySuffix == NullabilitySuffix.question ||
nullabilitySuffix == NullabilitySuffix.star) {
return '?';
}
}
return '';
}
}