Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(78)

Side by Side Diff: pkg/analyzer/lib/src/summary/link.dart

Issue 1836033002: Fix a corner case of building summaries from ASTs. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | pkg/analyzer/test/src/summary/summary_common.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 /** 5 /**
6 * This library is capable of producing linked summaries from unlinked 6 * This library is capable of producing linked summaries from unlinked
7 * ones (or prelinked ones). It functions by building a miniature 7 * ones (or prelinked ones). It functions by building a miniature
8 * element model to represent the contents of the summaries, and then 8 * element model to represent the contents of the summaries, and then
9 * scanning the element model to gather linked information and adding 9 * scanning the element model to gather linked information and adding
10 * it to the summary data structures. 10 * it to the summary data structures.
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after
181 void link(LinkedUnitBuilder linkedUnit); 181 void link(LinkedUnitBuilder linkedUnit);
182 182
183 @override 183 @override
184 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation); 184 noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
185 } 185 }
186 186
187 /** 187 /**
188 * Element representing a class resynthesized from a summary during 188 * Element representing a class resynthesized from a summary during
189 * linking. 189 * linking.
190 */ 190 */
191 class ClassElementForLink_Class extends ClassElementForLink { 191 class ClassElementForLink_Class extends ClassElementForLink
192 implements TypeParameterContext {
192 /** 193 /**
193 * The unlinked representation of the class in the summary. 194 * The unlinked representation of the class in the summary.
194 */ 195 */
195 final UnlinkedClass _unlinkedClass; 196 final UnlinkedClass _unlinkedClass;
196 197
197 @override 198 @override
198 final CompilationUnitElementForLink enclosingElement; 199 final CompilationUnitElementForLink enclosingElement;
199 200
200 List<ConstructorElementForLink> _constructors; 201 List<ConstructorElementForLink> _constructors;
201 ConstructorElementForLink _unnamedConstructor; 202 ConstructorElementForLink _unnamedConstructor;
202 bool _unnamedConstructorComputed = false; 203 bool _unnamedConstructorComputed = false;
203 List<FieldElementForLink_ClassField> _fields; 204 List<FieldElementForLink_ClassField> _fields;
204 InterfaceTypeForLink _supertype; 205 InterfaceTypeForLink _supertype;
205 InterfaceTypeForLink _type; 206 InterfaceTypeForLink _type;
207 List<TypeParameterTypeForLink> _typeParameterTypes;
206 208
207 ClassElementForLink_Class(this.enclosingElement, this._unlinkedClass); 209 ClassElementForLink_Class(this.enclosingElement, this._unlinkedClass);
208 210
209 @override 211 @override
210 List<ConstructorElementForLink> get constructors { 212 List<ConstructorElementForLink> get constructors {
211 if (_constructors == null) { 213 if (_constructors == null) {
212 _constructors = <ConstructorElementForLink>[]; 214 _constructors = <ConstructorElementForLink>[];
213 for (UnlinkedExecutable unlinkedExecutable 215 for (UnlinkedExecutable unlinkedExecutable
214 in _unlinkedClass.executables) { 216 in _unlinkedClass.executables) {
215 if (unlinkedExecutable.kind == UnlinkedExecutableKind.constructor) { 217 if (unlinkedExecutable.kind == UnlinkedExecutableKind.constructor) {
(...skipping 20 matching lines...) Expand all
236 bool get isObject => _unlinkedClass.hasNoSupertype; 238 bool get isObject => _unlinkedClass.hasNoSupertype;
237 239
238 @override 240 @override
239 String get name => _unlinkedClass.name; 241 String get name => _unlinkedClass.name;
240 242
241 @override 243 @override
242 InterfaceTypeForLink get supertype { 244 InterfaceTypeForLink get supertype {
243 if (isObject) { 245 if (isObject) {
244 return null; 246 return null;
245 } 247 }
246 return _supertype ??= _unlinkedClass.supertype == null 248 return _supertype ??= _computeSupertype();
247 ? enclosingElement.enclosingElement._linker.objectType 249 }
248 : enclosingElement._resolveTypeRef(_unlinkedClass.supertype); 250
251 /**
252 * Get a list of [TypeParameterTypeForLink] objects corresponding to the
253 * class's type parameters.
254 */
255 List<TypeParameterTypeForLink> get typeParameterTypes {
256 if (_typeParameterTypes == null) {
257 _typeParameterTypes = _unlinkedClass.typeParameters
258 .map((UnlinkedTypeParam _) => new TypeParameterTypeForLink())
259 .toList();
260 }
261 return _typeParameterTypes;
249 } 262 }
250 263
251 @override 264 @override
252 ConstructorElementForLink get unnamedConstructor { 265 ConstructorElementForLink get unnamedConstructor {
253 if (!_unnamedConstructorComputed) { 266 if (!_unnamedConstructorComputed) {
254 for (ConstructorElementForLink constructor in constructors) { 267 for (ConstructorElementForLink constructor in constructors) {
255 if (constructor.name.isEmpty) { 268 if (constructor.name.isEmpty) {
256 _unnamedConstructor = constructor; 269 _unnamedConstructor = constructor;
257 break; 270 break;
258 } 271 }
259 } 272 }
260 _unnamedConstructorComputed = true; 273 _unnamedConstructorComputed = true;
261 } 274 }
262 return _unnamedConstructor; 275 return _unnamedConstructor;
263 } 276 }
264 277
265 @override 278 @override
266 DartTypeForLink buildType(DartTypeForLink getTypeArgument(int i), 279 DartTypeForLink buildType(DartTypeForLink getTypeArgument(int i),
267 List<int> implicitFunctionTypeIndices) { 280 List<int> implicitFunctionTypeIndices) {
268 if (_unlinkedClass.typeParameters.length != 0) { 281 if (_unlinkedClass.typeParameters.length != 0) {
269 return new InterfaceTypeForLink(this); 282 return new InterfaceTypeForLink(this);
270 } else { 283 } else {
271 return _type ??= new InterfaceTypeForLink(this); 284 return _type ??= new InterfaceTypeForLink(this);
272 } 285 }
273 } 286 }
274 287
275 @override 288 @override
289 TypeParameterTypeForLink getTypeParameterType(int index) {
290 List<TypeParameterTypeForLink> types = typeParameterTypes;
291 return types[types.length - index];
292 }
293
294 @override
276 void link(LinkedUnitBuilder linkedUnit) { 295 void link(LinkedUnitBuilder linkedUnit) {
277 for (ConstructorElementForLink constructorElement in constructors) { 296 for (ConstructorElementForLink constructorElement in constructors) {
278 constructorElement.link(linkedUnit); 297 constructorElement.link(linkedUnit);
279 } 298 }
280 } 299 }
300
301 InterfaceTypeForLink _computeSupertype() {
302 if (_unlinkedClass.supertype != null) {
303 DartTypeForLink supertype =
304 enclosingElement._resolveTypeRef(_unlinkedClass.supertype, this);
305 if (supertype is InterfaceTypeForLink) {
306 return supertype;
307 }
308 // In the event that the supertype isn't an interface type (which may
309 // happen in the event of erroneous code) just fall through and pretend
310 // the supertype is `Object`.
311 }
312 return enclosingElement.enclosingElement._linker.objectType;
313 }
281 } 314 }
282 315
283 /** 316 /**
284 * Element representing an enum resynthesized from a summary during 317 * Element representing an enum resynthesized from a summary during
285 * linking. 318 * linking.
286 */ 319 */
287 class ClassElementForLink_Enum extends ClassElementForLink { 320 class ClassElementForLink_Enum extends ClassElementForLink {
288 /** 321 /**
289 * The unlinked representation of the enum in the summary. 322 * The unlinked representation of the enum in the summary.
290 */ 323 */
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
459 return _references[index]; 492 return _references[index];
460 } 493 }
461 494
462 /** 495 /**
463 * Resolve an [EntityRef] into a type. If the reference is 496 * Resolve an [EntityRef] into a type. If the reference is
464 * unresolved, return [DynamicTypeImpl.instance]. 497 * unresolved, return [DynamicTypeImpl.instance].
465 * 498 *
466 * TODO(paulberry): or should we have a class representing an 499 * TODO(paulberry): or should we have a class representing an
467 * unresolved type, for consistency with the full element model? 500 * unresolved type, for consistency with the full element model?
468 */ 501 */
469 DartTypeForLink _resolveTypeRef(EntityRef type, {bool defaultVoid: false}) { 502 DartTypeForLink _resolveTypeRef(
503 EntityRef type, TypeParameterContext typeParameterContext,
504 {bool defaultVoid: false}) {
470 if (type == null) { 505 if (type == null) {
471 if (defaultVoid) { 506 if (defaultVoid) {
472 return VoidTypeForLink.instance; 507 return VoidTypeForLink.instance;
473 } else { 508 } else {
474 return DynamicTypeForLink.instance; 509 return DynamicTypeForLink.instance;
475 } 510 }
476 } 511 }
477 if (type.paramReference != 0) { 512 if (type.paramReference != 0) {
478 // TODO(paulberry): implement. 513 return typeParameterContext.getTypeParameterType(type.paramReference);
479 throw new UnimplementedError();
480 } else if (type.syntheticReturnType != null) { 514 } else if (type.syntheticReturnType != null) {
481 // TODO(paulberry): implement. 515 // TODO(paulberry): implement.
482 throw new UnimplementedError(); 516 throw new UnimplementedError();
483 } else { 517 } else {
484 DartTypeForLink getTypeArgument(int i) { 518 DartTypeForLink getTypeArgument(int i) {
485 if (i < type.typeArguments.length) { 519 if (i < type.typeArguments.length) {
486 return _resolveTypeRef(type.typeArguments[i]); 520 return _resolveTypeRef(type.typeArguments[i], typeParameterContext);
487 } else { 521 } else {
488 return DynamicTypeForLink.instance; 522 return DynamicTypeForLink.instance;
489 } 523 }
490 } 524 }
491 ReferenceableElementForLink element = _resolveRef(type.reference); 525 ReferenceableElementForLink element = _resolveRef(type.reference);
492 return element.buildType( 526 return element.buildType(
493 getTypeArgument, type.implicitFunctionTypeIndices); 527 getTypeArgument, type.implicitFunctionTypeIndices);
494 } 528 }
495 } 529 }
496 } 530 }
(...skipping 856 matching lines...) Expand 10 before | Expand all | Expand 10 after
1353 implements TopLevelVariableElement { 1387 implements TopLevelVariableElement {
1354 TopLevelVariableElementForLink(CompilationUnitElement enclosingElement, 1388 TopLevelVariableElementForLink(CompilationUnitElement enclosingElement,
1355 UnlinkedVariable unlinkedVariable) 1389 UnlinkedVariable unlinkedVariable)
1356 : super(unlinkedVariable, enclosingElement); 1390 : super(unlinkedVariable, enclosingElement);
1357 1391
1358 @override 1392 @override
1359 bool get isStatic => true; 1393 bool get isStatic => true;
1360 } 1394 }
1361 1395
1362 /** 1396 /**
1397 * Interface representing elements which can serve as the context within which
1398 * type parameter indices are interpreted.
1399 */
1400 abstract class TypeParameterContext {
1401 /**
1402 * Convert the given [index] into a type parameter type.
1403 */
1404 TypeParameterTypeForLink getTypeParameterType(int index);
1405 }
1406
1407 /**
1408 * Representation of a type based on a type parameter during linking.
1409 *
1410 * TODO(paulberry): add more functionality as needed.
1411 */
1412 class TypeParameterTypeForLink extends DartTypeForLink
1413 implements TypeParameterType {}
1414
1415 /**
1363 * Singleton element used for unresolved references. 1416 * Singleton element used for unresolved references.
1364 */ 1417 */
1365 class UndefinedElementForLink implements ReferenceableElementForLink { 1418 class UndefinedElementForLink implements ReferenceableElementForLink {
1366 static const UndefinedElementForLink instance = 1419 static const UndefinedElementForLink instance =
1367 const UndefinedElementForLink._(); 1420 const UndefinedElementForLink._();
1368 1421
1369 const UndefinedElementForLink._(); 1422 const UndefinedElementForLink._();
1370 1423
1371 @override 1424 @override
1372 ConstructorElementForLink get asConstructor => null; 1425 ConstructorElementForLink get asConstructor => null;
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
1534 1587
1535 /** 1588 /**
1536 * Throw away any information produced by a previous call to [link]. 1589 * Throw away any information produced by a previous call to [link].
1537 */ 1590 */
1538 void unlink() { 1591 void unlink() {
1539 for (LibraryElementInBuildUnit library in _librariesInBuildUnit) { 1592 for (LibraryElementInBuildUnit library in _librariesInBuildUnit) {
1540 library.unlink(); 1593 library.unlink();
1541 } 1594 }
1542 } 1595 }
1543 } 1596 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analyzer/test/src/summary/summary_common.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698