OLD | NEW |
1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, 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 import 'package:analyzer/dart/ast/ast.dart'; | 5 import 'package:analyzer/dart/ast/ast.dart'; |
6 import 'package:analyzer/dart/ast/standard_ast_factory.dart'; | 6 import 'package:analyzer/dart/ast/standard_ast_factory.dart'; |
7 import 'package:analyzer/dart/ast/token.dart'; | 7 import 'package:analyzer/dart/ast/token.dart'; |
8 import 'package:analyzer/dart/element/element.dart'; | 8 import 'package:analyzer/dart/element/element.dart'; |
9 import 'package:analyzer/dart/element/type.dart'; | 9 import 'package:analyzer/dart/element/type.dart'; |
10 import 'package:analyzer/src/dart/element/element.dart'; | 10 import 'package:analyzer/src/dart/element/element.dart'; |
(...skipping 30 matching lines...) Expand all Loading... |
41 * Return the [LibraryElementImpl] for the given [uriStr], or `null` if | 41 * Return the [LibraryElementImpl] for the given [uriStr], or `null` if |
42 * the library is not part of the Kernel libraries bundle. | 42 * the library is not part of the Kernel libraries bundle. |
43 */ | 43 */ |
44 LibraryElementImpl getLibrary(String uriStr) { | 44 LibraryElementImpl getLibrary(String uriStr) { |
45 return _libraryMap.putIfAbsent(uriStr, () { | 45 return _libraryMap.putIfAbsent(uriStr, () { |
46 var kernel = _kernelMap[uriStr]; | 46 var kernel = _kernelMap[uriStr]; |
47 if (kernel == null) return null; | 47 if (kernel == null) return null; |
48 | 48 |
49 var libraryContext = | 49 var libraryContext = |
50 new _KernelLibraryResynthesizerContextImpl(this, kernel); | 50 new _KernelLibraryResynthesizerContextImpl(this, kernel); |
51 Source librarySource = _getSource(uriStr); | 51 LibraryElementImpl libraryElement = libraryContext._buildLibrary(uriStr); |
52 LibraryElementImpl libraryElement = | 52 |
53 new LibraryElementImpl.forKernel(_analysisContext, libraryContext); | 53 // Build the defining unit. |
54 CompilationUnitElementImpl definingUnit = | 54 var definingUnit = libraryContext._buildUnit(null).unit; |
55 libraryElement.definingCompilationUnit; | 55 libraryElement.definingCompilationUnit = definingUnit; |
56 definingUnit.source = librarySource; | 56 |
57 definingUnit.librarySource = librarySource; | 57 // Build units for parts. |
| 58 var parts = new List<CompilationUnitElementImpl>(kernel.parts.length); |
| 59 for (int i = 0; i < kernel.parts.length; i++) { |
| 60 var fileUri = kernel.parts[i].fileUri; |
| 61 var unitContext = libraryContext._buildUnit(fileUri); |
| 62 parts[i] = unitContext.unit; |
| 63 } |
| 64 libraryElement.parts = parts; |
| 65 |
58 return libraryElement; | 66 return libraryElement; |
59 }); | 67 }); |
60 } | 68 } |
61 | 69 |
62 /** | 70 /** |
| 71 * Return the [ElementImpl] that corresponds to the given [name], or `null` |
| 72 * if the corresponding element cannot be found. |
| 73 */ |
| 74 ElementImpl _getElement(kernel.CanonicalName name) { |
| 75 if (name == null) return null; |
| 76 kernel.CanonicalName parentName = name.parent; |
| 77 |
| 78 // If the parent is the root, then this name is a library. |
| 79 if (parentName.isRoot) { |
| 80 return getLibrary(name.name); |
| 81 } |
| 82 |
| 83 // If the name is private, it is prefixed with a library URI. |
| 84 if (name.name.startsWith('_')) { |
| 85 parentName = parentName.parent; |
| 86 } |
| 87 |
| 88 // Skip qualifiers. |
| 89 bool isGetter = false; |
| 90 bool isSetter = false; |
| 91 bool isField = false; |
| 92 bool isConstructor = false; |
| 93 bool isMethod = false; |
| 94 if (parentName.name == '@getters') { |
| 95 isGetter = true; |
| 96 parentName = parentName.parent; |
| 97 } else if (parentName.name == '@setters') { |
| 98 isSetter = true; |
| 99 parentName = parentName.parent; |
| 100 } else if (parentName.name == '@fields') { |
| 101 isField = true; |
| 102 parentName = parentName.parent; |
| 103 } else if (parentName.name == '@constructors') { |
| 104 isConstructor = true; |
| 105 parentName = parentName.parent; |
| 106 } else if (parentName.name == '@methods') { |
| 107 isMethod = true; |
| 108 parentName = parentName.parent; |
| 109 } |
| 110 |
| 111 ElementImpl parentElement = _getElement(parentName); |
| 112 if (parentElement == null) return null; |
| 113 |
| 114 // Search in units of the library. |
| 115 if (parentElement is LibraryElementImpl) { |
| 116 for (CompilationUnitElement unit in parentElement.units) { |
| 117 CompilationUnitElementImpl unitImpl = unit; |
| 118 ElementImpl child = unitImpl.getChild(name.name); |
| 119 if (child != null) { |
| 120 return child; |
| 121 } |
| 122 } |
| 123 return null; |
| 124 } |
| 125 |
| 126 // Search in the class. |
| 127 if (parentElement is AbstractClassElementImpl) { |
| 128 if (isGetter) { |
| 129 return parentElement.getGetter(name.name) as ElementImpl; |
| 130 } else if (isSetter) { |
| 131 return parentElement.getSetter(name.name) as ElementImpl; |
| 132 } else if (isField) { |
| 133 return parentElement.getField(name.name) as ElementImpl; |
| 134 } else if (isConstructor) { |
| 135 if (name.name.isEmpty) { |
| 136 return parentElement.unnamedConstructor as ConstructorElementImpl; |
| 137 } |
| 138 return parentElement.getNamedConstructor(name.name) as ElementImpl; |
| 139 } else if (isMethod) { |
| 140 return parentElement.getMethod(name.name) as ElementImpl; |
| 141 } |
| 142 return null; |
| 143 } |
| 144 |
| 145 throw new UnimplementedError( |
| 146 'Internal error: ${parentElement.runtimeType} unexpected.'); |
| 147 } |
| 148 |
| 149 /** |
63 * Get the [Source] object for the given [uri]. | 150 * Get the [Source] object for the given [uri]. |
64 */ | 151 */ |
65 Source _getSource(String uri) { | 152 Source _getSource(String uri) { |
66 return _sources.putIfAbsent( | 153 return _sources.putIfAbsent( |
67 uri, () => _analysisContext.sourceFactory.forUri(uri)); | 154 uri, () => _analysisContext.sourceFactory.forUri(uri)); |
68 } | 155 } |
69 } | 156 } |
70 | 157 |
71 /** | 158 /** |
72 * Builder of [Expression]s from [kernel.Expression]s. | 159 * Builder of [Expression]s from [kernel.Expression]s. |
73 */ | 160 */ |
74 class _ExprBuilder { | 161 class _ExprBuilder { |
75 final _KernelLibraryResynthesizerContextImpl _context; | 162 final _KernelUnitResynthesizerContextImpl _context; |
76 final ElementImpl _contextElement; | 163 final ElementImpl _contextElement; |
77 | 164 |
78 _ExprBuilder(this._context, this._contextElement); | 165 _ExprBuilder(this._context, this._contextElement); |
79 | 166 |
80 Expression build(kernel.Expression expr) { | 167 Expression build(kernel.Expression expr) { |
81 if (expr is kernel.NullLiteral) { | 168 if (expr is kernel.NullLiteral) { |
82 return AstTestFactory.nullLiteral(); | 169 return AstTestFactory.nullLiteral(); |
83 } | 170 } |
84 if (expr is kernel.BoolLiteral) { | 171 if (expr is kernel.BoolLiteral) { |
85 return AstTestFactory.booleanLiteral(expr.value); | 172 return AstTestFactory.booleanLiteral(expr.value); |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 List<Expression> arguments = _toArguments(expr.arguments); | 276 List<Expression> arguments = _toArguments(expr.arguments); |
190 MethodInvocation invocation = | 277 MethodInvocation invocation = |
191 AstTestFactory.methodInvocation3(null, name, null, arguments); | 278 AstTestFactory.methodInvocation3(null, name, null, arguments); |
192 invocation.methodName.staticElement = _getElement(target.reference); | 279 invocation.methodName.staticElement = _getElement(target.reference); |
193 return invocation; | 280 return invocation; |
194 } | 281 } |
195 | 282 |
196 if (expr is kernel.ConstructorInvocation) { | 283 if (expr is kernel.ConstructorInvocation) { |
197 var element = _getElement(expr.targetReference); | 284 var element = _getElement(expr.targetReference); |
198 | 285 |
199 var kernelType = expr.getStaticType(_context._resynthesizer._types); | 286 var kernelType = |
| 287 expr.getStaticType(_context.libraryContext.resynthesizer._types); |
200 var type = _context.getType(_contextElement, kernelType); | 288 var type = _context.getType(_contextElement, kernelType); |
201 TypeName typeName = _buildType(type); | 289 TypeName typeName = _buildType(type); |
202 | 290 |
203 var constructorName = AstTestFactory.constructorName( | 291 var constructorName = AstTestFactory.constructorName( |
204 typeName, element.name.isNotEmpty ? element.name : null); | 292 typeName, element.name.isNotEmpty ? element.name : null); |
205 constructorName?.name?.staticElement = element; | 293 constructorName?.name?.staticElement = element; |
206 | 294 |
207 var keyword = expr.isConst ? Keyword.CONST : Keyword.NEW; | 295 var keyword = expr.isConst ? Keyword.CONST : Keyword.NEW; |
208 var arguments = _toArguments(expr.arguments); | 296 var arguments = _toArguments(expr.arguments); |
209 return AstTestFactory.instanceCreationExpression( | 297 return AstTestFactory.instanceCreationExpression( |
210 keyword, constructorName, arguments); | 298 keyword, constructorName, arguments); |
211 } | 299 } |
212 | 300 |
213 if (expr is kernel.TypeLiteral) { | 301 if (expr is kernel.TypeLiteral) { |
214 var type = _context.getType(_contextElement, expr.type); | 302 var type = _context.getType(_contextElement, expr.type); |
215 var identifier = AstTestFactory.identifier3(type.element.name); | 303 var identifier = AstTestFactory.identifier3(type.element.name); |
216 identifier.staticElement = type.element; | 304 identifier.staticElement = type.element; |
217 identifier.staticType = _context._resynthesizer.typeType; | 305 identifier.staticType = _context.libraryContext.resynthesizer.typeType; |
218 return identifier; | 306 return identifier; |
219 } | 307 } |
220 | 308 |
221 // TODO(scheglov): complete getExpression | 309 // TODO(scheglov): complete getExpression |
222 throw new UnimplementedError('kernel: (${expr.runtimeType}) $expr'); | 310 throw new UnimplementedError('kernel: (${expr.runtimeType}) $expr'); |
223 } | 311 } |
224 | 312 |
225 ConstructorInitializer buildInitializer(kernel.Initializer k) { | 313 ConstructorInitializer buildInitializer(kernel.Initializer k) { |
226 if (k is kernel.FieldInitializer) { | 314 if (k is kernel.FieldInitializer) { |
227 Expression value = build(k.value); | 315 Expression value = build(k.value); |
(...skipping 95 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
323 } | 411 } |
324 return AstTestFactory.typeArgumentList(types); | 412 return AstTestFactory.typeArgumentList(types); |
325 } | 413 } |
326 | 414 |
327 List<TypeAnnotation> _buildTypeArguments(List<DartType> types) { | 415 List<TypeAnnotation> _buildTypeArguments(List<DartType> types) { |
328 if (types.every((t) => t.isDynamic)) return null; | 416 if (types.every((t) => t.isDynamic)) return null; |
329 return types.map(_buildType).toList(); | 417 return types.map(_buildType).toList(); |
330 } | 418 } |
331 | 419 |
332 ElementImpl _getElement(kernel.Reference reference) { | 420 ElementImpl _getElement(kernel.Reference reference) { |
333 return _context._getElement(reference?.canonicalName); | 421 return _context.libraryContext.resynthesizer |
| 422 ._getElement(reference?.canonicalName); |
334 } | 423 } |
335 | 424 |
336 InterpolationElement _newInterpolationElement(Expression expr) { | 425 InterpolationElement _newInterpolationElement(Expression expr) { |
337 if (expr is SimpleStringLiteral) { | 426 if (expr is SimpleStringLiteral) { |
338 return astFactory.interpolationString(expr.literal, expr.value); | 427 return astFactory.interpolationString(expr.literal, expr.value); |
339 } else { | 428 } else { |
340 return AstTestFactory.interpolationExpression(expr); | 429 return AstTestFactory.interpolationExpression(expr); |
341 } | 430 } |
342 } | 431 } |
343 | 432 |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
383 if (name == 'unary-') return TokenType.MINUS; | 472 if (name == 'unary-') return TokenType.MINUS; |
384 throw new ArgumentError(name); | 473 throw new ArgumentError(name); |
385 } | 474 } |
386 } | 475 } |
387 | 476 |
388 /** | 477 /** |
389 * Implementation of [KernelLibraryResynthesizerContext]. | 478 * Implementation of [KernelLibraryResynthesizerContext]. |
390 */ | 479 */ |
391 class _KernelLibraryResynthesizerContextImpl | 480 class _KernelLibraryResynthesizerContextImpl |
392 implements KernelLibraryResynthesizerContext { | 481 implements KernelLibraryResynthesizerContext { |
393 final KernelResynthesizer _resynthesizer; | 482 final KernelResynthesizer resynthesizer; |
394 | 483 |
395 @override | 484 @override |
396 final kernel.Library library; | 485 final kernel.Library library; |
397 | 486 |
398 _KernelLibraryResynthesizerContextImpl(this._resynthesizer, this.library); | 487 Source librarySource; |
| 488 LibraryElementImpl libraryElement; |
| 489 |
| 490 _KernelLibraryResynthesizerContextImpl(this.resynthesizer, this.library); |
| 491 |
| 492 @override |
| 493 LibraryElementImpl getLibrary(String uriStr) { |
| 494 return resynthesizer.getLibrary(uriStr); |
| 495 } |
| 496 |
| 497 LibraryElementImpl _buildLibrary(String uriStr) { |
| 498 librarySource = resynthesizer._getSource(uriStr); |
| 499 return libraryElement = |
| 500 new LibraryElementImpl.forKernel(resynthesizer._analysisContext, this); |
| 501 } |
| 502 |
| 503 _KernelUnitResynthesizerContextImpl _buildUnit(String fileUri) { |
| 504 var unitContext = new _KernelUnitResynthesizerContextImpl( |
| 505 this, fileUri ?? library.fileUri); |
| 506 var unitElement = new CompilationUnitElementImpl.forKernel( |
| 507 libraryElement, unitContext, '<no name>'); |
| 508 unitContext.unit = unitElement; |
| 509 unitElement.librarySource = librarySource; |
| 510 unitElement.source = |
| 511 fileUri != null ? resynthesizer._getSource(fileUri) : librarySource; |
| 512 unitContext.unit = unitElement; |
| 513 return unitContext; |
| 514 } |
| 515 } |
| 516 |
| 517 /** |
| 518 * Implementation of [KernelUnit]. |
| 519 */ |
| 520 class _KernelUnitImpl implements KernelUnit { |
| 521 final _KernelUnitResynthesizerContextImpl context; |
| 522 |
| 523 List<kernel.Class> _classes; |
| 524 List<kernel.Field> _fields; |
| 525 List<kernel.Procedure> _procedures; |
| 526 List<kernel.Typedef> _typedefs; |
| 527 |
| 528 _KernelUnitImpl(this.context); |
| 529 |
| 530 @override |
| 531 List<kernel.Class> get classes => |
| 532 _classes ??= context.libraryContext.library.classes |
| 533 .where((n) => n.fileUri == context.fileUri) |
| 534 .toList(growable: false); |
| 535 |
| 536 @override |
| 537 List<kernel.Field> get fields => |
| 538 _fields ??= context.libraryContext.library.fields |
| 539 .where((n) => n.fileUri == context.fileUri) |
| 540 .toList(growable: false); |
| 541 |
| 542 @override |
| 543 List<kernel.Procedure> get procedures => |
| 544 _procedures ??= context.libraryContext.library.procedures |
| 545 .where((n) => n.fileUri == context.fileUri) |
| 546 .toList(growable: false); |
| 547 |
| 548 @override |
| 549 List<kernel.Typedef> get typedefs => |
| 550 _typedefs ??= context.libraryContext.library.typedefs |
| 551 .where((n) => n.fileUri == context.fileUri) |
| 552 .toList(growable: false); |
| 553 } |
| 554 |
| 555 /** |
| 556 * Implementation of [KernelUnitResynthesizerContext]. |
| 557 */ |
| 558 class _KernelUnitResynthesizerContextImpl |
| 559 implements KernelUnitResynthesizerContext { |
| 560 final _KernelLibraryResynthesizerContextImpl libraryContext; |
| 561 final String fileUri; |
| 562 |
| 563 CompilationUnitElementImpl unit; |
| 564 |
| 565 _KernelUnitResynthesizerContextImpl(this.libraryContext, this.fileUri); |
| 566 |
| 567 @override |
| 568 KernelUnit get kernelUnit => new _KernelUnitImpl(this); |
399 | 569 |
400 @override | 570 @override |
401 List<ElementAnnotation> buildAnnotations( | 571 List<ElementAnnotation> buildAnnotations( |
402 CompilationUnitElementImpl unit, List<kernel.Expression> expressions) { | 572 List<kernel.Expression> expressions) { |
403 int length = expressions.length; | 573 int length = expressions.length; |
404 if (length != 0) { | 574 if (length != 0) { |
405 var annotations = new List<ElementAnnotation>(length); | 575 var annotations = new List<ElementAnnotation>(length); |
406 for (int i = 0; i < length; i++) { | 576 for (int i = 0; i < length; i++) { |
407 annotations[i] = _buildAnnotation(unit, expressions[i]); | 577 annotations[i] = _buildAnnotation(unit, expressions[i]); |
408 } | 578 } |
409 return annotations; | 579 return annotations; |
410 } else { | 580 } else { |
411 return const <ElementAnnotation>[]; | 581 return const <ElementAnnotation>[]; |
412 } | 582 } |
413 } | 583 } |
414 | 584 |
415 @override | 585 @override |
416 UnitExplicitTopLevelAccessors buildTopLevelAccessors( | 586 UnitExplicitTopLevelAccessors buildTopLevelAccessors() { |
417 CompilationUnitElementImpl unit) { | |
418 var accessorsData = new UnitExplicitTopLevelAccessors(); | 587 var accessorsData = new UnitExplicitTopLevelAccessors(); |
419 var implicitVariables = <String, TopLevelVariableElementImpl>{}; | 588 var implicitVariables = <String, TopLevelVariableElementImpl>{}; |
420 // Build explicit property accessors and implicit fields. | 589 // Build explicit property accessors and implicit fields. |
421 for (var procedure in library.procedures) { | 590 for (var procedure in kernelUnit.procedures) { |
422 bool isGetter = procedure.kind == kernel.ProcedureKind.Getter; | 591 bool isGetter = procedure.kind == kernel.ProcedureKind.Getter; |
423 bool isSetter = procedure.kind == kernel.ProcedureKind.Setter; | 592 bool isSetter = procedure.kind == kernel.ProcedureKind.Setter; |
424 if (isGetter || isSetter) { | 593 if (isGetter || isSetter) { |
425 var accessor = | 594 var accessor = |
426 new PropertyAccessorElementImpl.forKernel(unit, procedure); | 595 new PropertyAccessorElementImpl.forKernel(unit, procedure); |
427 accessorsData.accessors.add(accessor); | 596 accessorsData.accessors.add(accessor); |
428 | 597 |
429 // Create or update the implicit variable. | 598 // Create or update the implicit variable. |
430 String name = accessor.displayName; | 599 String name = accessor.displayName; |
431 TopLevelVariableElementImpl variable = implicitVariables[name]; | 600 TopLevelVariableElementImpl variable = implicitVariables[name]; |
(...skipping 14 matching lines...) Expand all Loading... |
446 } else { | 615 } else { |
447 variable.setter = accessor; | 616 variable.setter = accessor; |
448 } | 617 } |
449 } | 618 } |
450 } | 619 } |
451 accessorsData.implicitVariables.addAll(implicitVariables.values); | 620 accessorsData.implicitVariables.addAll(implicitVariables.values); |
452 return accessorsData; | 621 return accessorsData; |
453 } | 622 } |
454 | 623 |
455 @override | 624 @override |
456 UnitExplicitTopLevelVariables buildTopLevelVariables( | 625 UnitExplicitTopLevelVariables buildTopLevelVariables() { |
457 CompilationUnitElementImpl unit) { | 626 List<kernel.Field> kernelFields = kernelUnit.fields; |
458 int numberOfVariables = library.fields.length; | 627 int numberOfVariables = kernelFields.length; |
459 var variablesData = new UnitExplicitTopLevelVariables(numberOfVariables); | 628 var variablesData = new UnitExplicitTopLevelVariables(numberOfVariables); |
460 for (int i = 0; i < numberOfVariables; i++) { | 629 for (int i = 0; i < numberOfVariables; i++) { |
461 kernel.Field field = library.fields[i]; | 630 kernel.Field field = kernelFields[i]; |
462 | 631 |
463 // Add the explicit variables. | 632 // Add the explicit variables. |
464 TopLevelVariableElementImpl variable; | 633 TopLevelVariableElementImpl variable; |
465 if (field.isConst && field.initializer != null) { | 634 if (field.isConst && field.initializer != null) { |
466 variable = new ConstTopLevelVariableElementImpl.forKernel(unit, field); | 635 variable = new ConstTopLevelVariableElementImpl.forKernel(unit, field); |
467 } else { | 636 } else { |
468 variable = new TopLevelVariableElementImpl.forKernel(unit, field); | 637 variable = new TopLevelVariableElementImpl.forKernel(unit, field); |
469 } | 638 } |
470 variablesData.variables[i] = variable; | 639 variablesData.variables[i] = variable; |
471 | 640 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
518 } | 687 } |
519 | 688 |
520 @override | 689 @override |
521 InterfaceType getInterfaceType( | 690 InterfaceType getInterfaceType( |
522 ElementImpl context, kernel.Supertype kernelType) { | 691 ElementImpl context, kernel.Supertype kernelType) { |
523 return _getInterfaceType( | 692 return _getInterfaceType( |
524 context, kernelType.className.canonicalName, kernelType.typeArguments); | 693 context, kernelType.className.canonicalName, kernelType.typeArguments); |
525 } | 694 } |
526 | 695 |
527 @override | 696 @override |
528 LibraryElement getLibrary(String uriStr) { | |
529 return _resynthesizer.getLibrary(uriStr); | |
530 } | |
531 | |
532 @override | |
533 ConstructorElementImpl getRedirectedConstructor( | 697 ConstructorElementImpl getRedirectedConstructor( |
534 kernel.Constructor kernelConstructor, kernel.Procedure kernelFactory) { | 698 kernel.Constructor kernelConstructor, kernel.Procedure kernelFactory) { |
535 if (kernelConstructor != null) { | 699 if (kernelConstructor != null) { |
536 for (var initializer in kernelConstructor.initializers) { | 700 for (var initializer in kernelConstructor.initializers) { |
537 if (initializer is kernel.RedirectingInitializer) { | 701 if (initializer is kernel.RedirectingInitializer) { |
538 return _getElement(initializer.targetReference.canonicalName) | 702 return libraryContext.resynthesizer |
| 703 ._getElement(initializer.targetReference.canonicalName) |
539 as ConstructorElementImpl; | 704 as ConstructorElementImpl; |
540 } | 705 } |
541 } | 706 } |
542 } | 707 } |
543 if (kernelFactory != null) { | 708 if (kernelFactory != null) { |
544 kernel.Statement body = kernelFactory.function.body; | 709 kernel.Statement body = kernelFactory.function.body; |
545 if (body is RedirectingFactoryBody) { | 710 if (body is RedirectingFactoryBody) { |
546 kernel.Member target = body.target; | 711 kernel.Member target = body.target; |
547 if (target != null) { | 712 if (target != null) { |
548 return _getElement(target.reference.canonicalName) | 713 return libraryContext.resynthesizer |
| 714 ._getElement(target.reference.canonicalName) |
549 as ConstructorElementImpl; | 715 as ConstructorElementImpl; |
550 } | 716 } |
551 } | 717 } |
552 } | 718 } |
553 return null; | 719 return null; |
554 } | 720 } |
555 | 721 |
556 DartType getType(ElementImpl context, kernel.DartType kernelType) { | 722 DartType getType(ElementImpl context, kernel.DartType kernelType) { |
557 if (kernelType is kernel.DynamicType) return DynamicTypeImpl.instance; | 723 if (kernelType is kernel.DynamicType) return DynamicTypeImpl.instance; |
558 if (kernelType is kernel.VoidType) return VoidTypeImpl.instance; | 724 if (kernelType is kernel.VoidType) return VoidTypeImpl.instance; |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
605 elementAnnotation.annotationAst = AstTestFactory.annotation2( | 771 elementAnnotation.annotationAst = AstTestFactory.annotation2( |
606 typeName, constructorName, constExpr.argumentList) | 772 typeName, constructorName, constExpr.argumentList) |
607 ..element = constExpr.staticElement; | 773 ..element = constExpr.staticElement; |
608 } else { | 774 } else { |
609 throw new StateError( | 775 throw new StateError( |
610 'Unexpected annotation type: ${constExpr.runtimeType}'); | 776 'Unexpected annotation type: ${constExpr.runtimeType}'); |
611 } | 777 } |
612 return elementAnnotation; | 778 return elementAnnotation; |
613 } | 779 } |
614 | 780 |
615 /** | |
616 * Return the [ElementImpl] that corresponds to the given [name], or `null` | |
617 * if the corresponding element cannot be found. | |
618 */ | |
619 ElementImpl _getElement(kernel.CanonicalName name) { | |
620 if (name == null) return null; | |
621 kernel.CanonicalName parentName = name.parent; | |
622 | |
623 // If the parent is the root, then this name is a library. | |
624 if (parentName.isRoot) { | |
625 return _resynthesizer.getLibrary(name.name); | |
626 } | |
627 | |
628 // If the name is private, it is prefixed with a library URI. | |
629 if (name.name.startsWith('_')) { | |
630 parentName = parentName.parent; | |
631 } | |
632 | |
633 // Skip qualifiers. | |
634 bool isGetter = false; | |
635 bool isSetter = false; | |
636 bool isField = false; | |
637 bool isConstructor = false; | |
638 bool isMethod = false; | |
639 if (parentName.name == '@getters') { | |
640 isGetter = true; | |
641 parentName = parentName.parent; | |
642 } else if (parentName.name == '@setters') { | |
643 isSetter = true; | |
644 parentName = parentName.parent; | |
645 } else if (parentName.name == '@fields') { | |
646 isField = true; | |
647 parentName = parentName.parent; | |
648 } else if (parentName.name == '@constructors') { | |
649 isConstructor = true; | |
650 parentName = parentName.parent; | |
651 } else if (parentName.name == '@methods') { | |
652 isMethod = true; | |
653 parentName = parentName.parent; | |
654 } | |
655 | |
656 ElementImpl parentElement = _getElement(parentName); | |
657 if (parentElement == null) return null; | |
658 | |
659 // Search in units of the library. | |
660 if (parentElement is LibraryElementImpl) { | |
661 for (CompilationUnitElement unit in parentElement.units) { | |
662 CompilationUnitElementImpl unitImpl = unit; | |
663 ElementImpl child = unitImpl.getChild(name.name); | |
664 if (child != null) { | |
665 return child; | |
666 } | |
667 } | |
668 return null; | |
669 } | |
670 | |
671 // Search in the class. | |
672 if (parentElement is AbstractClassElementImpl) { | |
673 if (isGetter) { | |
674 return parentElement.getGetter(name.name) as ElementImpl; | |
675 } else if (isSetter) { | |
676 return parentElement.getSetter(name.name) as ElementImpl; | |
677 } else if (isField) { | |
678 return parentElement.getField(name.name) as ElementImpl; | |
679 } else if (isConstructor) { | |
680 if (name.name.isEmpty) { | |
681 return parentElement.unnamedConstructor as ConstructorElementImpl; | |
682 } | |
683 return parentElement.getNamedConstructor(name.name) as ElementImpl; | |
684 } else if (isMethod) { | |
685 return parentElement.getMethod(name.name) as ElementImpl; | |
686 } | |
687 return null; | |
688 } | |
689 | |
690 throw new UnimplementedError('Should not be reached.'); | |
691 } | |
692 | |
693 InterfaceType _getInterfaceType(ElementImpl context, | 781 InterfaceType _getInterfaceType(ElementImpl context, |
694 kernel.CanonicalName className, List<kernel.DartType> kernelArguments) { | 782 kernel.CanonicalName className, List<kernel.DartType> kernelArguments) { |
695 var libraryName = className.parent; | 783 var libraryName = className.parent; |
696 var libraryElement = _resynthesizer.getLibrary(libraryName.name); | 784 var libraryElement = libraryContext.getLibrary(libraryName.name); |
697 ClassElement classElement = libraryElement.getType(className.name); | 785 ClassElement classElement = libraryElement.getType(className.name); |
698 classElement ??= libraryElement.getEnum(className.name); | 786 classElement ??= libraryElement.getEnum(className.name); |
699 | 787 |
700 if (kernelArguments.isEmpty) { | 788 if (kernelArguments.isEmpty) { |
701 return classElement.type; | 789 return classElement.type; |
702 } | 790 } |
703 | 791 |
704 return new InterfaceTypeImpl.elementWithNameAndArgs( | 792 return new InterfaceTypeImpl.elementWithNameAndArgs( |
705 classElement, classElement.name, () { | 793 classElement, classElement.name, () { |
706 List<DartType> arguments = kernelArguments | 794 List<DartType> arguments = kernelArguments |
(...skipping 12 matching lines...) Expand all Loading... |
719 for (var typeParameter in ctx.typeParameters) { | 807 for (var typeParameter in ctx.typeParameters) { |
720 if (typeParameter.name == name) { | 808 if (typeParameter.name == name) { |
721 return typeParameter; | 809 return typeParameter; |
722 } | 810 } |
723 } | 811 } |
724 } | 812 } |
725 } | 813 } |
726 throw new StateError('Not found $kernelTypeParameter in $context'); | 814 throw new StateError('Not found $kernelTypeParameter in $context'); |
727 } | 815 } |
728 } | 816 } |
OLD | NEW |