| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 part of resolution; | 5 part of resolution; |
| 6 | 6 |
| 7 abstract class TreeElements { | 7 abstract class TreeElements { |
| 8 Element get currentElement; | 8 Element get currentElement; |
| 9 Setlet<Node> get superUses; | 9 Setlet<Node> get superUses; |
| 10 | 10 |
| (...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 final ConstantCompiler constantCompiler; | 252 final ConstantCompiler constantCompiler; |
| 253 | 253 |
| 254 ResolverTask(Compiler compiler, this.constantCompiler) : super(compiler); | 254 ResolverTask(Compiler compiler, this.constantCompiler) : super(compiler); |
| 255 | 255 |
| 256 String get name => 'Resolver'; | 256 String get name => 'Resolver'; |
| 257 | 257 |
| 258 TreeElements resolve(Element element) { | 258 TreeElements resolve(Element element) { |
| 259 return measure(() { | 259 return measure(() { |
| 260 if (Elements.isErroneousElement(element)) return null; | 260 if (Elements.isErroneousElement(element)) return null; |
| 261 | 261 |
| 262 for (MetadataAnnotation metadata in element.metadata) { | 262 processMetadata([result]) { |
| 263 metadata.ensureResolved(compiler); | 263 for (MetadataAnnotation metadata in element.metadata) { |
| 264 metadata.ensureResolved(compiler); |
| 265 } |
| 266 return result; |
| 264 } | 267 } |
| 265 | 268 |
| 266 ElementKind kind = element.kind; | 269 ElementKind kind = element.kind; |
| 267 if (identical(kind, ElementKind.GENERATIVE_CONSTRUCTOR) || | 270 if (identical(kind, ElementKind.GENERATIVE_CONSTRUCTOR) || |
| 268 identical(kind, ElementKind.FUNCTION) || | 271 identical(kind, ElementKind.FUNCTION) || |
| 269 identical(kind, ElementKind.GETTER) || | 272 identical(kind, ElementKind.GETTER) || |
| 270 identical(kind, ElementKind.SETTER)) { | 273 identical(kind, ElementKind.SETTER)) { |
| 271 return resolveMethodElement(element); | 274 return processMetadata(resolveMethodElement(element)); |
| 272 } | 275 } |
| 273 | 276 |
| 274 if (identical(kind, ElementKind.FIELD)) return resolveField(element); | 277 if (identical(kind, ElementKind.FIELD)) { |
| 275 | 278 return processMetadata(resolveField(element)); |
| 279 } |
| 276 if (element.isClass) { | 280 if (element.isClass) { |
| 277 ClassElement cls = element; | 281 ClassElement cls = element; |
| 278 cls.ensureResolved(compiler); | 282 cls.ensureResolved(compiler); |
| 279 return null; | 283 return processMetadata(); |
| 280 } else if (element.isTypedef) { | 284 } else if (element.isTypedef) { |
| 281 TypedefElement typdef = element; | 285 TypedefElement typdef = element; |
| 282 return resolveTypedef(typdef); | 286 return processMetadata(resolveTypedef(typdef)); |
| 283 } | 287 } |
| 284 | 288 |
| 285 compiler.unimplemented(element, "resolve($element)"); | 289 compiler.unimplemented(element, "resolve($element)"); |
| 286 }); | 290 }); |
| 287 } | 291 } |
| 288 | 292 |
| 289 String constructorNameForDiagnostics(String className, | 293 String constructorNameForDiagnostics(String className, |
| 290 String constructorName) { | 294 String constructorName) { |
| 291 String classNameString = className; | 295 String classNameString = className; |
| 292 String constructorNameString = constructorName; | 296 String constructorNameString = constructorName; |
| (...skipping 243 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 536 for (MixinApplicationElement mixinApplication in mixinUses) { | 540 for (MixinApplicationElement mixinApplication in mixinUses) { |
| 537 checkMixinSuperUses(resolutionTree, mixinApplication, mixin); | 541 checkMixinSuperUses(resolutionTree, mixinApplication, mixin); |
| 538 } | 542 } |
| 539 } | 543 } |
| 540 } | 544 } |
| 541 return resolutionTree; | 545 return resolutionTree; |
| 542 }); | 546 }); |
| 543 }); | 547 }); |
| 544 } | 548 } |
| 545 | 549 |
| 550 /// Creates a [ResolverVisitor] for resolving an AST in context of [element]. |
| 551 /// If [useEnclosingScope] is `true` then the initial scope of the visitor |
| 552 /// does not include inner scope of [element]. |
| 553 /// |
| 546 /// This method should only be used by this library (or tests of | 554 /// This method should only be used by this library (or tests of |
| 547 /// this library). | 555 /// this library). |
| 548 ResolverVisitor visitorFor(Element element) { | 556 ResolverVisitor visitorFor(Element element, {bool useEnclosingScope: false}) { |
| 549 return new ResolverVisitor(compiler, element, _ensureTreeElements(element)); | 557 return new ResolverVisitor(compiler, element, |
| 558 _ensureTreeElements(element), useEnclosingScope: useEnclosingScope); |
| 550 } | 559 } |
| 551 | 560 |
| 552 TreeElements resolveField(VariableElementX element) { | 561 TreeElements resolveField(VariableElementX element) { |
| 553 VariableDefinitions tree = element.parseNode(compiler); | 562 VariableDefinitions tree = element.parseNode(compiler); |
| 554 if(element.modifiers.isStatic && element.isTopLevel) { | 563 if(element.modifiers.isStatic && element.isTopLevel) { |
| 555 error(element.modifiers.getStatic(), | 564 error(element.modifiers.getStatic(), |
| 556 MessageKind.TOP_LEVEL_VARIABLE_DECLARED_STATIC); | 565 MessageKind.TOP_LEVEL_VARIABLE_DECLARED_STATIC); |
| 557 } | 566 } |
| 558 ResolverVisitor visitor = visitorFor(element); | 567 ResolverVisitor visitor = visitorFor(element); |
| 559 // TODO(johnniwinther): Share the resolved type between all variables | 568 // TODO(johnniwinther): Share the resolved type between all variables |
| (...skipping 618 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1178 }); | 1187 }); |
| 1179 }); | 1188 }); |
| 1180 } | 1189 } |
| 1181 | 1190 |
| 1182 void resolveMetadataAnnotation(MetadataAnnotationX annotation) { | 1191 void resolveMetadataAnnotation(MetadataAnnotationX annotation) { |
| 1183 compiler.withCurrentElement(annotation.annotatedElement, () => measure(() { | 1192 compiler.withCurrentElement(annotation.annotatedElement, () => measure(() { |
| 1184 assert(annotation.resolutionState == STATE_NOT_STARTED); | 1193 assert(annotation.resolutionState == STATE_NOT_STARTED); |
| 1185 annotation.resolutionState = STATE_STARTED; | 1194 annotation.resolutionState = STATE_STARTED; |
| 1186 | 1195 |
| 1187 Node node = annotation.parseNode(compiler); | 1196 Node node = annotation.parseNode(compiler); |
| 1188 // TODO(johnniwinther): Find the right analyzable element to hold the | |
| 1189 // [TreeElements] for the annotation. | |
| 1190 Element annotatedElement = annotation.annotatedElement; | 1197 Element annotatedElement = annotation.annotatedElement; |
| 1191 Element context = annotatedElement.enclosingElement; | 1198 AnalyzableElement context = annotatedElement.analyzableElement; |
| 1192 if (context == null) { | 1199 assert(invariant(node, context != null, |
| 1193 context = annotatedElement; | 1200 message: "No context found for metadata annotation " |
| 1194 } | 1201 "on $annotatedElement.")); |
| 1195 ResolverVisitor visitor = visitorFor(context); | 1202 ResolverVisitor visitor = visitorFor(context, useEnclosingScope: true); |
| 1196 node.accept(visitor); | 1203 node.accept(visitor); |
| 1197 annotation.value = | 1204 annotation.value = |
| 1198 constantCompiler.compileMetadata(annotation, node, visitor.mapping); | 1205 constantCompiler.compileMetadata(annotation, node, visitor.mapping); |
| 1199 // TODO(johnniwinther): Register the relation between the annotation | 1206 // TODO(johnniwinther): Register the relation between the annotation |
| 1200 // and the annotated element instead. This will allow the backed to | 1207 // and the annotated element instead. This will allow the backend to |
| 1201 // retrieve the backend constant and only registered metadata on the | 1208 // retrieve the backend constant and only register metadata on the |
| 1202 // elements for which it is needed. (Issue 17732). | 1209 // elements for which it is needed. (Issue 17732). |
| 1203 compiler.backend.registerMetadataConstant( | 1210 compiler.backend.registerMetadataConstant( |
| 1204 annotation.value, visitor.mapping); | 1211 annotation.value, visitor.mapping); |
| 1205 annotation.resolutionState = STATE_DONE; | 1212 annotation.resolutionState = STATE_DONE; |
| 1206 })); | 1213 })); |
| 1207 } | 1214 } |
| 1208 | 1215 |
| 1209 error(Spannable node, MessageKind kind, [arguments = const {}]) { | 1216 error(Spannable node, MessageKind kind, [arguments = const {}]) { |
| 1210 // TODO(ahe): Make non-fatal. | 1217 // TODO(ahe): Make non-fatal. |
| 1211 compiler.reportFatalError(node, kind, arguments); | 1218 compiler.reportFatalError(node, kind, arguments); |
| (...skipping 784 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1996 r'>|' | 2003 r'>|' |
| 1997 r'<=|' | 2004 r'<=|' |
| 1998 r'<|' | 2005 r'<|' |
| 1999 r'&|' | 2006 r'&|' |
| 2000 r'\^|' | 2007 r'\^|' |
| 2001 r'\|' | 2008 r'\|' |
| 2002 r')$'); | 2009 r')$'); |
| 2003 | 2010 |
| 2004 ResolverVisitor(Compiler compiler, | 2011 ResolverVisitor(Compiler compiler, |
| 2005 Element element, | 2012 Element element, |
| 2006 TreeElementMapping mapping) | 2013 TreeElementMapping mapping, |
| 2014 {bool useEnclosingScope: false}) |
| 2007 : this.enclosingElement = element, | 2015 : this.enclosingElement = element, |
| 2008 // When the element is a field, we are actually resolving its | 2016 // When the element is a field, we are actually resolving its |
| 2009 // initial value, which should not have access to instance | 2017 // initial value, which should not have access to instance |
| 2010 // fields. | 2018 // fields. |
| 2011 inInstanceContext = (element.isInstanceMember && !element.isField) | 2019 inInstanceContext = (element.isInstanceMember && !element.isField) |
| 2012 || element.isGenerativeConstructor, | 2020 || element.isGenerativeConstructor, |
| 2013 this.currentClass = element.isMember ? element.enclosingClass | 2021 this.currentClass = element.isMember ? element.enclosingClass |
| 2014 : null, | 2022 : null, |
| 2015 this.statementScope = new StatementScope(), | 2023 this.statementScope = new StatementScope(), |
| 2016 scope = element.buildScope(), | 2024 scope = useEnclosingScope |
| 2025 ? Scope.buildEnclosingScope(element) : element.buildScope(), |
| 2017 // The type annotations on a typedef do not imply type checks. | 2026 // The type annotations on a typedef do not imply type checks. |
| 2018 // TODO(karlklose): clean this up (dartbug.com/8870). | 2027 // TODO(karlklose): clean this up (dartbug.com/8870). |
| 2019 inCheckContext = compiler.enableTypeAssertions && | 2028 inCheckContext = compiler.enableTypeAssertions && |
| 2020 !element.isLibrary && | 2029 !element.isLibrary && |
| 2021 !element.isTypedef && | 2030 !element.isTypedef && |
| 2022 !element.enclosingElement.isTypedef, | 2031 !element.enclosingElement.isTypedef, |
| 2023 inCatchBlock = false, | 2032 inCatchBlock = false, |
| 2024 super(compiler, mapping); | 2033 super(compiler, mapping); |
| 2025 | 2034 |
| 2026 ResolutionEnqueuer get world => compiler.enqueuer.resolution; | 2035 ResolutionEnqueuer get world => compiler.enqueuer.resolution; |
| (...skipping 2623 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 4650 expression, expression); | 4659 expression, expression); |
| 4651 } | 4660 } |
| 4652 } | 4661 } |
| 4653 | 4662 |
| 4654 /// Looks up [name] in [scope] and unwraps the result. | 4663 /// Looks up [name] in [scope] and unwraps the result. |
| 4655 Element lookupInScope(Compiler compiler, Node node, | 4664 Element lookupInScope(Compiler compiler, Node node, |
| 4656 Scope scope, String name) { | 4665 Scope scope, String name) { |
| 4657 return Elements.unwrap(scope.lookup(name), compiler, node); | 4666 return Elements.unwrap(scope.lookup(name), compiler, node); |
| 4658 } | 4667 } |
| 4659 | 4668 |
| 4660 TreeElements _ensureTreeElements(AnalyzableElement element) { | 4669 TreeElements _ensureTreeElements(AnalyzableElementX element) { |
| 4661 if (element._treeElements == null) { | 4670 if (element._treeElements == null) { |
| 4662 element._treeElements = new TreeElementMapping(element); | 4671 element._treeElements = new TreeElementMapping(element); |
| 4663 } | 4672 } |
| 4664 return element._treeElements; | 4673 return element._treeElements; |
| 4665 } | 4674 } |
| 4666 | 4675 |
| 4667 abstract class AnalyzableElement implements Element { | 4676 abstract class AnalyzableElementX implements AnalyzableElement { |
| 4668 TreeElements _treeElements; | 4677 TreeElements _treeElements; |
| 4669 | 4678 |
| 4670 bool get hasTreeElements => _treeElements != null; | 4679 bool get hasTreeElements => _treeElements != null; |
| 4671 | 4680 |
| 4672 TreeElements get treeElements { | 4681 TreeElements get treeElements { |
| 4673 assert(invariant(this, _treeElements !=null, | 4682 assert(invariant(this, _treeElements !=null, |
| 4674 message: "TreeElements have not been computed for $this.")); | 4683 message: "TreeElements have not been computed for $this.")); |
| 4675 return _treeElements; | 4684 return _treeElements; |
| 4676 } | 4685 } |
| 4677 } | 4686 } |
| OLD | NEW |