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

Side by Side Diff: lib/compiler/implementation/resolver.dart

Issue 10834243: Reduce usage of .enclosingElement to get enclosing class. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 4 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 | Annotate | Revision Log
OLDNEW
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 interface TreeElements { 5 interface TreeElements {
6 Element operator[](Node node); 6 Element operator[](Node node);
7 Selector getSelector(Send send); 7 Selector getSelector(Send send);
8 Type getType(TypeAnnotation annotation); 8 Type getType(TypeAnnotation annotation);
9 } 9 }
10 10
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
79 } 79 }
80 80
81 FunctionElement resolveConstructorRedirection(FunctionElement constructor) { 81 FunctionElement resolveConstructorRedirection(FunctionElement constructor) {
82 FunctionExpression node = constructor.parseNode(compiler); 82 FunctionExpression node = constructor.parseNode(compiler);
83 // A synthetic constructor does not have a node. 83 // A synthetic constructor does not have a node.
84 if (node === null) return null; 84 if (node === null) return null;
85 if (node.initializers === null) return null; 85 if (node.initializers === null) return null;
86 Link<Node> initializers = node.initializers.nodes; 86 Link<Node> initializers = node.initializers.nodes;
87 if (!initializers.isEmpty() && 87 if (!initializers.isEmpty() &&
88 Initializers.isConstructorRedirect(initializers.head)) { 88 Initializers.isConstructorRedirect(initializers.head)) {
89 final ClassElement classElement = constructor.enclosingElement; 89 final ClassElement classElement = constructor.getEnclosingClass();
90 final SourceString constructorName = 90 final SourceString constructorName =
91 getConstructorName(initializers.head); 91 getConstructorName(initializers.head);
92 final SourceString className = classElement.name; 92 final SourceString className = classElement.name;
93 return classElement.lookupConstructor(className, constructorName); 93 return classElement.lookupConstructor(className, constructorName);
94 } 94 }
95 return null; 95 return null;
96 } 96 }
97 97
98 void resolveRedirectingConstructor(InitializerResolver resolver, 98 void resolveRedirectingConstructor(InitializerResolver resolver,
99 Node node, 99 Node node,
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 }); 153 });
154 } 154 }
155 155
156 void visitBody(ResolverVisitor visitor, Statement body) { 156 void visitBody(ResolverVisitor visitor, Statement body) {
157 visitor.visit(body); 157 visitor.visit(body);
158 } 158 }
159 159
160 void resolveConstructorImplementation(FunctionElement constructor, 160 void resolveConstructorImplementation(FunctionElement constructor,
161 FunctionExpression node) { 161 FunctionExpression node) {
162 if (constructor.defaultImplementation !== constructor) return; 162 if (constructor.defaultImplementation !== constructor) return;
163 ClassElement intrface = constructor.enclosingElement; 163 ClassElement intrface = constructor.getEnclosingClass();
164 if (!intrface.isInterface()) return; 164 if (!intrface.isInterface()) return;
165 Type defaultType = intrface.defaultClass; 165 Type defaultType = intrface.defaultClass;
166 if (defaultType === null) { 166 if (defaultType === null) {
167 error(node, MessageKind.NO_DEFAULT_CLASS, [intrface.name]); 167 error(node, MessageKind.NO_DEFAULT_CLASS, [intrface.name]);
168 } 168 }
169 ClassElement defaultClass = defaultType.element; 169 ClassElement defaultClass = defaultType.element;
170 defaultClass.ensureResolved(compiler); 170 defaultClass.ensureResolved(compiler);
171 if (defaultClass.isInterface()) { 171 if (defaultClass.isInterface()) {
172 error(node, MessageKind.CANNOT_INSTANTIATE_INTERFACE, 172 error(node, MessageKind.CANNOT_INSTANTIATE_INTERFACE,
173 [defaultClass.name]); 173 [defaultClass.name]);
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after
397 return node.receiver.asIdentifier().isThis(); 397 return node.receiver.asIdentifier().isThis();
398 } 398 }
399 399
400 void resolveFieldInitializer(FunctionElement constructor, SendSet init) { 400 void resolveFieldInitializer(FunctionElement constructor, SendSet init) {
401 // init is of the form [this.]field = value. 401 // init is of the form [this.]field = value.
402 final Node selector = init.selector; 402 final Node selector = init.selector;
403 final SourceString name = selector.asIdentifier().source; 403 final SourceString name = selector.asIdentifier().source;
404 // Lookup target field. 404 // Lookup target field.
405 Element target; 405 Element target;
406 if (isFieldInitializer(init)) { 406 if (isFieldInitializer(init)) {
407 final ClassElement classElement = constructor.enclosingElement; 407 final ClassElement classElement = constructor.getEnclosingClass();
408 target = classElement.lookupLocalMember(name); 408 target = classElement.lookupLocalMember(name);
409 if (target === null) { 409 if (target === null) {
410 error(selector, MessageKind.CANNOT_RESOLVE, [name]); 410 error(selector, MessageKind.CANNOT_RESOLVE, [name]);
411 } else if (target.kind != ElementKind.FIELD) { 411 } else if (target.kind != ElementKind.FIELD) {
412 error(selector, MessageKind.NOT_A_FIELD, [name]); 412 error(selector, MessageKind.NOT_A_FIELD, [name]);
413 } else if (!target.isInstanceMember()) { 413 } else if (!target.isInstanceMember()) {
414 error(selector, MessageKind.INIT_STATIC_FIELD, [name]); 414 error(selector, MessageKind.INIT_STATIC_FIELD, [name]);
415 } 415 }
416 } else { 416 } else {
417 error(init, MessageKind.INVALID_RECEIVER_IN_INITIALIZER); 417 error(init, MessageKind.INVALID_RECEIVER_IN_INITIALIZER);
(...skipping 21 matching lines...) Expand all
439 SourceString constructorName = resolver.getConstructorName(call); 439 SourceString constructorName = resolver.getConstructorName(call);
440 Element result = resolveSuperOrThis( 440 Element result = resolveSuperOrThis(
441 constructor, isSuperCall, false, constructorName, selector, call); 441 constructor, isSuperCall, false, constructorName, selector, call);
442 visitor.useElement(call, result); 442 visitor.useElement(call, result);
443 return result; 443 return result;
444 } 444 }
445 445
446 void resolveImplicitSuperConstructorSend(FunctionElement constructor, 446 void resolveImplicitSuperConstructorSend(FunctionElement constructor,
447 FunctionExpression functionNode) { 447 FunctionExpression functionNode) {
448 // If the class has a super resolve the implicit super call. 448 // If the class has a super resolve the implicit super call.
449 ClassElement classElement = constructor.enclosingElement; 449 ClassElement classElement = constructor.getEnclosingClass();
450 ClassElement superClass = classElement.superclass; 450 ClassElement superClass = classElement.superclass;
451 if (classElement != visitor.compiler.objectClass) { 451 if (classElement != visitor.compiler.objectClass) {
452 assert(superClass !== null); 452 assert(superClass !== null);
453 assert(superClass.isResolved); 453 assert(superClass.isResolved);
454 var element = resolveSuperOrThis(constructor, true, true, 454 var element = resolveSuperOrThis(constructor, true, true,
455 const SourceString(''), 455 const SourceString(''),
456 Selector.INVOCATION_0, functionNode); 456 Selector.INVOCATION_0, functionNode);
457 visitor.world.registerStaticUse(element); 457 visitor.world.registerStaticUse(element);
458 } 458 }
459 } 459 }
460 460
461 Element resolveSuperOrThis(FunctionElement constructor, 461 Element resolveSuperOrThis(FunctionElement constructor,
462 bool isSuperCall, 462 bool isSuperCall,
463 bool isImplicitSuperCall, 463 bool isImplicitSuperCall,
464 SourceString constructorName, 464 SourceString constructorName,
465 Selector selector, 465 Selector selector,
466 Node diagnosticNode) { 466 Node diagnosticNode) {
467 ClassElement lookupTarget = constructor.enclosingElement; 467 ClassElement lookupTarget = constructor.getEnclosingClass();
468 bool validTarget = true; 468 bool validTarget = true;
469 FunctionElement result; 469 FunctionElement result;
470 if (isSuperCall) { 470 if (isSuperCall) {
471 // Calculate correct lookup target and constructor name. 471 // Calculate correct lookup target and constructor name.
472 if (lookupTarget === visitor.compiler.objectClass) { 472 if (lookupTarget === visitor.compiler.objectClass) {
473 error(diagnosticNode, MessageKind.SUPER_INITIALIZER_IN_OBJECT); 473 error(diagnosticNode, MessageKind.SUPER_INITIALIZER_IN_OBJECT);
474 } else { 474 } else {
475 lookupTarget = lookupTarget.supertype.element; 475 lookupTarget = lookupTarget.supertype.element;
476 } 476 }
477 } 477 }
(...skipping 352 matching lines...) Expand 10 before | Expand all | Expand 10 after
830 ClassElement currentClass; 830 ClassElement currentClass;
831 bool typeRequired = false; 831 bool typeRequired = false;
832 StatementScope statementScope; 832 StatementScope statementScope;
833 int allowedCategory = ElementCategory.VARIABLE | ElementCategory.FUNCTION; 833 int allowedCategory = ElementCategory.VARIABLE | ElementCategory.FUNCTION;
834 834
835 ResolverVisitor(Compiler compiler, Element element) 835 ResolverVisitor(Compiler compiler, Element element)
836 : this.mapping = new TreeElementMapping(), 836 : this.mapping = new TreeElementMapping(),
837 this.enclosingElement = element, 837 this.enclosingElement = element,
838 inInstanceContext = element.isInstanceMember() 838 inInstanceContext = element.isInstanceMember()
839 || element.isGenerativeConstructor(), 839 || element.isGenerativeConstructor(),
840 this.currentClass = element.isMember() ? element.enclosingElement : null, 840 this.currentClass = element.isMember() ?
841 element.getEnclosingClass() :
842 null,
841 this.statementScope = new StatementScope(), 843 this.statementScope = new StatementScope(),
842 typeResolver = new TypeResolver(compiler), 844 typeResolver = new TypeResolver(compiler),
843 scope = element.buildEnclosingScope(), 845 scope = element.buildEnclosingScope(),
844 super(compiler) { 846 super(compiler) {
845 } 847 }
846 848
847 Enqueuer get world() => compiler.enqueuer.resolution; 849 Enqueuer get world() => compiler.enqueuer.resolution;
848 850
849 Element lookup(Node node, SourceString name) { 851 Element lookup(Node node, SourceString name) {
850 Element result = scope.lookup(name); 852 Element result = scope.lookup(name);
(...skipping 502 matching lines...) Expand 10 before | Expand all | Expand 10 after
1353 // TODO(ngeoffray): resolution error with wrong number of 1355 // TODO(ngeoffray): resolution error with wrong number of
1354 // parameters. We cannot do this rigth now because of the 1356 // parameters. We cannot do this rigth now because of the
1355 // List constructor. 1357 // List constructor.
1356 } 1358 }
1357 useElement(node.send, constructor); 1359 useElement(node.send, constructor);
1358 compiler.withCurrentElement(constructor, () { 1360 compiler.withCurrentElement(constructor, () {
1359 FunctionExpression tree = constructor.parseNode(compiler); 1361 FunctionExpression tree = constructor.parseNode(compiler);
1360 compiler.resolver.resolveConstructorImplementation(constructor, tree); 1362 compiler.resolver.resolveConstructorImplementation(constructor, tree);
1361 }); 1363 });
1362 world.registerStaticUse(constructor.defaultImplementation); 1364 world.registerStaticUse(constructor.defaultImplementation);
1363 ClassElement cls = constructor.defaultImplementation.enclosingElement; 1365 ClassElement cls = constructor.defaultImplementation.getEnclosingClass();
1364 world.registerInstantiatedClass(cls); 1366 world.registerInstantiatedClass(cls);
1365 cls.forEachInstanceField( 1367 cls.forEachInstanceField(
1366 includeBackendMembers: false, 1368 includeBackendMembers: false,
1367 includeSuperMembers: true, 1369 includeSuperMembers: true,
1368 f: (ClassElement enclosingClass, Element member) { 1370 f: (ClassElement enclosingClass, Element member) {
1369 world.addToWorkList(member); 1371 world.addToWorkList(member);
1370 }); 1372 });
1371 return null; 1373 return null;
1372 } 1374 }
1373 1375
(...skipping 283 matching lines...) Expand 10 before | Expand all | Expand 10 after
1657 } 1659 }
1658 } 1660 }
1659 1661
1660 class TypeDefinitionVisitor extends CommonResolverVisitor<Type> { 1662 class TypeDefinitionVisitor extends CommonResolverVisitor<Type> {
1661 Scope scope; 1663 Scope scope;
1662 TypeDeclarationElement element; 1664 TypeDeclarationElement element;
1663 TypeResolver typeResolver; 1665 TypeResolver typeResolver;
1664 1666
1665 TypeDefinitionVisitor(Compiler compiler, TypeDeclarationElement element) 1667 TypeDefinitionVisitor(Compiler compiler, TypeDeclarationElement element)
1666 : this.element = element, 1668 : this.element = element,
1667 scope = element.enclosingElement.buildScope(), 1669 scope = element.buildEnclosingScope(),
1668 typeResolver = new TypeResolver(compiler), 1670 typeResolver = new TypeResolver(compiler),
1669 super(compiler); 1671 super(compiler);
1670 1672
1671 void resolveTypeVariableBounds(NodeList node) { 1673 void resolveTypeVariableBounds(NodeList node) {
1672 if (node === null) return; 1674 if (node === null) return;
1673 1675
1674 var nameSet = new Set<SourceString>(); 1676 var nameSet = new Set<SourceString>();
1675 // Resolve the bounds of type variables. 1677 // Resolve the bounds of type variables.
1676 Link<Type> typeLink = element.typeVariables; 1678 Link<Type> typeLink = element.typeVariables;
1677 Link<Node> nodeLink = node.nodes; 1679 Link<Node> nodeLink = node.nodes;
(...skipping 417 matching lines...) Expand 10 before | Expand all | Expand 10 after
2095 2097
2096 // TODO(ahe): This is temporary. 2098 // TODO(ahe): This is temporary.
2097 void resolveExpression(Node node) { 2099 void resolveExpression(Node node) {
2098 if (node == null) return; 2100 if (node == null) return;
2099 node.accept(new ResolverVisitor(compiler, enclosingElement)); 2101 node.accept(new ResolverVisitor(compiler, enclosingElement));
2100 } 2102 }
2101 2103
2102 // TODO(ahe): This is temporary. 2104 // TODO(ahe): This is temporary.
2103 ClassElement get currentClass() { 2105 ClassElement get currentClass() {
2104 return enclosingElement.isMember() 2106 return enclosingElement.isMember()
2105 ? enclosingElement.enclosingElement : null; 2107 ? enclosingElement.getEnclosingClass() : null;
2106 } 2108 }
2107 } 2109 }
2108 2110
2109 class ConstructorResolver extends CommonResolverVisitor<Element> { 2111 class ConstructorResolver extends CommonResolverVisitor<Element> {
2110 final ResolverVisitor resolver; 2112 final ResolverVisitor resolver;
2111 ConstructorResolver(Compiler compiler, this.resolver) : super(compiler); 2113 ConstructorResolver(Compiler compiler, this.resolver) : super(compiler);
2112 2114
2113 visitNode(Node node) { 2115 visitNode(Node node) {
2114 throw 'not supported'; 2116 throw 'not supported';
2115 } 2117 }
(...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after
2301 TopScope(LibraryElement library) : super(null, library); 2303 TopScope(LibraryElement library) : super(null, library);
2302 Element lookup(SourceString name) { 2304 Element lookup(SourceString name) {
2303 return library.find(name); 2305 return library.find(name);
2304 } 2306 }
2305 2307
2306 Element add(Element newElement) { 2308 Element add(Element newElement) {
2307 throw "Cannot add an element in the top scope"; 2309 throw "Cannot add an element in the top scope";
2308 } 2310 }
2309 String toString() => '$element'; 2311 String toString() => '$element';
2310 } 2312 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698