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

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

Issue 10870010: Evaluate compile-time constants of metadata. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge
Patch Set: Rebased and use const instead of final 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 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
146 FunctionExpression node) { 146 FunctionExpression node) {
147 if (constructor.defaultImplementation !== constructor) return; 147 if (constructor.defaultImplementation !== constructor) return;
148 ClassElement intrface = constructor.getEnclosingClass(); 148 ClassElement intrface = constructor.getEnclosingClass();
149 if (!intrface.isInterface()) return; 149 if (!intrface.isInterface()) return;
150 Type defaultType = intrface.defaultClass; 150 Type defaultType = intrface.defaultClass;
151 if (defaultType === null) { 151 if (defaultType === null) {
152 error(node, MessageKind.NO_DEFAULT_CLASS, [intrface.name]); 152 error(node, MessageKind.NO_DEFAULT_CLASS, [intrface.name]);
153 } 153 }
154 ClassElement defaultClass = defaultType.element; 154 ClassElement defaultClass = defaultType.element;
155 defaultClass.ensureResolved(compiler); 155 defaultClass.ensureResolved(compiler);
156 assert(defaultClass.resolutionState == ClassElement.STATE_DONE); 156 assert(defaultClass.resolutionState == STATE_DONE);
157 assert(defaultClass.supertypeLoadState == ClassElement.STATE_DONE); 157 assert(defaultClass.supertypeLoadState == STATE_DONE);
158 if (defaultClass.isInterface()) { 158 if (defaultClass.isInterface()) {
159 error(node, MessageKind.CANNOT_INSTANTIATE_INTERFACE, 159 error(node, MessageKind.CANNOT_INSTANTIATE_INTERFACE,
160 [defaultClass.name]); 160 [defaultClass.name]);
161 } 161 }
162 // We have now established the following: 162 // We have now established the following:
163 // [intrface] is an interface, let's say "MyInterface". 163 // [intrface] is an interface, let's say "MyInterface".
164 // [defaultClass] is a class, let's say "MyClass". 164 // [defaultClass] is a class, let's say "MyClass".
165 165
166 // If the default class implements the interface then we must use the 166 // If the default class implements the interface then we must use the
167 // default class' name. Otherwise we look for a factory with the name 167 // default class' name. Otherwise we look for a factory with the name
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 } 213 }
214 214
215 /** 215 /**
216 * Load and resolve the supertypes of [cls]. 216 * Load and resolve the supertypes of [cls].
217 * 217 *
218 * Warning: do not call this method directly. It should only be 218 * Warning: do not call this method directly. It should only be
219 * called by [resolveClass] and [ClassSupertypeResolver]. 219 * called by [resolveClass] and [ClassSupertypeResolver].
220 */ 220 */
221 void loadSupertypes(ClassElement cls, Node from) { 221 void loadSupertypes(ClassElement cls, Node from) {
222 compiler.withCurrentElement(cls, () => measure(() { 222 compiler.withCurrentElement(cls, () => measure(() {
223 if (cls.supertypeLoadState == ClassElement.STATE_DONE) return; 223 if (cls.supertypeLoadState == STATE_DONE) return;
224 if (cls.supertypeLoadState == ClassElement.STATE_STARTED) { 224 if (cls.supertypeLoadState == STATE_STARTED) {
225 compiler.reportMessage( 225 compiler.reportMessage(
226 compiler.spanFromNode(from), 226 compiler.spanFromNode(from),
227 MessageKind.CYCLIC_CLASS_HIERARCHY.error([cls.name]), 227 MessageKind.CYCLIC_CLASS_HIERARCHY.error([cls.name]),
228 api.Diagnostic.ERROR); 228 api.Diagnostic.ERROR);
229 cls.supertypeLoadState = ClassElement.STATE_DONE; 229 cls.supertypeLoadState = STATE_DONE;
230 cls.allSupertypes = const EmptyLink<Type>().prepend( 230 cls.allSupertypes = const EmptyLink<Type>().prepend(
231 compiler.objectClass.computeType(compiler)); 231 compiler.objectClass.computeType(compiler));
232 // TODO(ahe): We should also set cls.supertype here to avoid 232 // TODO(ahe): We should also set cls.supertype here to avoid
233 // creating a malformed class hierarchy. 233 // creating a malformed class hierarchy.
234 return; 234 return;
235 } 235 }
236 cls.supertypeLoadState = ClassElement.STATE_STARTED; 236 cls.supertypeLoadState = STATE_STARTED;
237 compiler.withCurrentElement(cls, () { 237 compiler.withCurrentElement(cls, () {
238 // TODO(ahe): Cache the node in cls. 238 // TODO(ahe): Cache the node in cls.
239 cls.parseNode(compiler).accept(new ClassSupertypeResolver(compiler, 239 cls.parseNode(compiler).accept(new ClassSupertypeResolver(compiler,
240 cls)); 240 cls));
241 if (cls.supertypeLoadState != ClassElement.STATE_DONE) { 241 if (cls.supertypeLoadState != STATE_DONE) {
242 cls.supertypeLoadState = ClassElement.STATE_DONE; 242 cls.supertypeLoadState = STATE_DONE;
243 } 243 }
244 }); 244 });
245 })); 245 }));
246 } 246 }
247 247
248 /** 248 /**
249 * Resolve the class [element]. 249 * Resolve the class [element].
250 * 250 *
251 * Before calling this method, [element] was constructed by the 251 * Before calling this method, [element] was constructed by the
252 * scanner and most fields are null or empty. This method fills in 252 * scanner and most fields are null or empty. This method fills in
253 * these fields and also ensure that the supertypes of [element] are 253 * these fields and also ensure that the supertypes of [element] are
254 * resolved. 254 * resolved.
255 * 255 *
256 * Warning: Do not call this method directly. Instead use 256 * Warning: Do not call this method directly. Instead use
257 * [:element.ensureResolved(compiler):]. 257 * [:element.ensureResolved(compiler):].
258 */ 258 */
259 void resolveClass(ClassElement element) { 259 void resolveClass(ClassElement element) {
260 assert(element.resolutionState == ClassElement.STATE_NOT_STARTED);
261 element.resolutionState = ClassElement.STATE_STARTED;
262 compiler.withCurrentElement(element, () => measure(() { 260 compiler.withCurrentElement(element, () => measure(() {
261 assert(element.resolutionState == STATE_NOT_STARTED);
262 element.resolutionState = STATE_STARTED;
263 ClassNode tree = element.parseNode(compiler); 263 ClassNode tree = element.parseNode(compiler);
264 loadSupertypes(element, tree); 264 loadSupertypes(element, tree);
265 265
266 ClassResolverVisitor visitor = 266 ClassResolverVisitor visitor =
267 new ClassResolverVisitor(compiler, element); 267 new ClassResolverVisitor(compiler, element);
268 visitor.visit(tree); 268 visitor.visit(tree);
269 element.resolutionState = ClassElement.STATE_DONE; 269 element.resolutionState = STATE_DONE;
270 })); 270 }));
271 } 271 }
272 272
273 void checkMembers(ClassElement cls) { 273 void checkMembers(ClassElement cls) {
274 if (cls === compiler.objectClass) return; 274 if (cls === compiler.objectClass) return;
275 cls.forEachMember((holder, member) { 275 cls.forEachMember((holder, member) {
276 checkAbstractField(member); 276 checkAbstractField(member);
277 checkValidOverride(member, cls.lookupSuperMember(member.name)); 277 checkValidOverride(member, cls.lookupSuperMember(member.name));
278 }); 278 });
279 } 279 }
(...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after
408 !link.isEmpty(); 408 !link.isEmpty();
409 link = link.tail) { 409 link = link.tail) {
410 parameterTypes.addLast(link.head.computeType(compiler)); 410 parameterTypes.addLast(link.head.computeType(compiler));
411 // TODO(karlklose): optional parameters. 411 // TODO(karlklose): optional parameters.
412 } 412 }
413 return new FunctionType(signature.returnType, 413 return new FunctionType(signature.returnType,
414 parameterTypes.toLink(), 414 parameterTypes.toLink(),
415 element); 415 element);
416 } 416 }
417 417
418 void resolveMetadataAnnotation(MetadataAnnotation annotation) {
419 compiler.withCurrentElement(annotation.annotatedElement, () => measure(() {
420 assert(annotation.resolutionState == STATE_NOT_STARTED);
421 annotation.resolutionState = STATE_STARTED;
422
423 Node node = annotation.parseNode(compiler);
424 ResolverVisitor visitor =
425 new ResolverVisitor(compiler, annotation.annotatedElement);
426 node.accept(visitor);
427 annotation.value = compiler.constantHandler.compileNodeWithDefinitions(
428 node, visitor.mapping);
429
430 annotation.resolutionState = STATE_DONE;
431 }));
432 }
433
418 error(Node node, MessageKind kind, [arguments = const []]) { 434 error(Node node, MessageKind kind, [arguments = const []]) {
419 ResolutionError message = new ResolutionError(kind, arguments); 435 ResolutionError message = new ResolutionError(kind, arguments);
420 compiler.reportError(node, message); 436 compiler.reportError(node, message);
421 } 437 }
422 } 438 }
423 439
424 class InitializerResolver { 440 class InitializerResolver {
425 final ResolverVisitor visitor; 441 final ResolverVisitor visitor;
426 final Map<SourceString, Node> initialized; 442 final Map<SourceString, Node> initialized;
427 Link<Node> initializers; 443 Link<Node> initializers;
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after
498 return result; 514 return result;
499 } 515 }
500 516
501 void resolveImplicitSuperConstructorSend(FunctionElement constructor, 517 void resolveImplicitSuperConstructorSend(FunctionElement constructor,
502 FunctionExpression functionNode) { 518 FunctionExpression functionNode) {
503 // If the class has a super resolve the implicit super call. 519 // If the class has a super resolve the implicit super call.
504 ClassElement classElement = constructor.getEnclosingClass(); 520 ClassElement classElement = constructor.getEnclosingClass();
505 ClassElement superClass = classElement.superclass; 521 ClassElement superClass = classElement.superclass;
506 if (classElement != visitor.compiler.objectClass) { 522 if (classElement != visitor.compiler.objectClass) {
507 assert(superClass !== null); 523 assert(superClass !== null);
508 assert(superClass.resolutionState == ClassElement.STATE_DONE); 524 assert(superClass.resolutionState == STATE_DONE);
509 SourceString name = const SourceString(''); 525 SourceString name = const SourceString('');
510 Selector call = new Selector.call(name, classElement.getLibrary(), 0); 526 Selector call = new Selector.call(name, classElement.getLibrary(), 0);
511 var element = resolveSuperOrThis(constructor, true, true, 527 var element = resolveSuperOrThis(constructor, true, true,
512 name, call, functionNode); 528 name, call, functionNode);
513 visitor.world.registerStaticUse(element); 529 visitor.world.registerStaticUse(element);
514 } 530 }
515 } 531 }
516 532
517 Element resolveSuperOrThis(FunctionElement constructor, 533 Element resolveSuperOrThis(FunctionElement constructor,
518 bool isSuperCall, 534 bool isSuperCall,
(...skipping 1350 matching lines...) Expand 10 before | Expand all | Expand 10 after
1869 * types. 1885 * types.
1870 */ 1886 */
1871 class ClassResolverVisitor extends TypeDefinitionVisitor { 1887 class ClassResolverVisitor extends TypeDefinitionVisitor {
1872 ClassElement get element => super.element; 1888 ClassElement get element => super.element;
1873 1889
1874 ClassResolverVisitor(Compiler compiler, ClassElement classElement) 1890 ClassResolverVisitor(Compiler compiler, ClassElement classElement)
1875 : super(compiler, classElement); 1891 : super(compiler, classElement);
1876 1892
1877 Type visitClassNode(ClassNode node) { 1893 Type visitClassNode(ClassNode node) {
1878 compiler.ensure(element !== null); 1894 compiler.ensure(element !== null);
1879 compiler.ensure(element.resolutionState == ClassElement.STATE_STARTED); 1895 compiler.ensure(element.resolutionState == STATE_STARTED);
1880 1896
1881 InterfaceType type = element.computeType(compiler); 1897 InterfaceType type = element.computeType(compiler);
1882 scope = new TypeDeclarationScope(scope, element); 1898 scope = new TypeDeclarationScope(scope, element);
1883 // TODO(ahe): It is not safe to call resolveTypeVariableBounds yet. 1899 // TODO(ahe): It is not safe to call resolveTypeVariableBounds yet.
1884 // As a side-effect, this may get us back here trying to 1900 // As a side-effect, this may get us back here trying to
1885 // resolve this class again. 1901 // resolve this class again.
1886 resolveTypeVariableBounds(node.typeParameters); 1902 resolveTypeVariableBounds(node.typeParameters);
1887 1903
1888 // Find super type. 1904 // Find super type.
1889 Type supertype = visit(node.superclass); 1905 Type supertype = visit(node.superclass);
(...skipping 652 matching lines...) Expand 10 before | Expand all | Expand 10 after
2542 TopScope(LibraryElement library) : super(null, library); 2558 TopScope(LibraryElement library) : super(null, library);
2543 Element lookup(SourceString name) { 2559 Element lookup(SourceString name) {
2544 return library.find(name); 2560 return library.find(name);
2545 } 2561 }
2546 2562
2547 Element add(Element newElement) { 2563 Element add(Element newElement) {
2548 throw "Cannot add an element in the top scope"; 2564 throw "Cannot add an element in the top scope";
2549 } 2565 }
2550 String toString() => '$element'; 2566 String toString() => '$element';
2551 } 2567 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698