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

Side by Side Diff: compiler/java/com/google/dart/compiler/resolver/Resolver.java

Issue 8231031: Check for compile-time constants in DartCompiler (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Incorporated feedback. Created 9 years, 2 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) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, 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 package com.google.dart.compiler.resolver; 5 package com.google.dart.compiler.resolver;
6 6
7 import com.google.common.annotations.VisibleForTesting; 7 import com.google.common.annotations.VisibleForTesting;
8 import com.google.common.collect.Sets; 8 import com.google.common.collect.Sets;
9 import com.google.dart.compiler.DartCompilationPhase; 9 import com.google.dart.compiler.DartCompilationPhase;
10 import com.google.dart.compiler.DartCompilerContext; 10 import com.google.dart.compiler.DartCompilerContext;
(...skipping 16 matching lines...) Expand all
27 import com.google.dart.compiler.ast.DartFunctionExpression; 27 import com.google.dart.compiler.ast.DartFunctionExpression;
28 import com.google.dart.compiler.ast.DartFunctionObjectInvocation; 28 import com.google.dart.compiler.ast.DartFunctionObjectInvocation;
29 import com.google.dart.compiler.ast.DartFunctionTypeAlias; 29 import com.google.dart.compiler.ast.DartFunctionTypeAlias;
30 import com.google.dart.compiler.ast.DartGotoStatement; 30 import com.google.dart.compiler.ast.DartGotoStatement;
31 import com.google.dart.compiler.ast.DartIdentifier; 31 import com.google.dart.compiler.ast.DartIdentifier;
32 import com.google.dart.compiler.ast.DartIfStatement; 32 import com.google.dart.compiler.ast.DartIfStatement;
33 import com.google.dart.compiler.ast.DartInitializer; 33 import com.google.dart.compiler.ast.DartInitializer;
34 import com.google.dart.compiler.ast.DartIntegerLiteral; 34 import com.google.dart.compiler.ast.DartIntegerLiteral;
35 import com.google.dart.compiler.ast.DartInvocation; 35 import com.google.dart.compiler.ast.DartInvocation;
36 import com.google.dart.compiler.ast.DartLabel; 36 import com.google.dart.compiler.ast.DartLabel;
37 import com.google.dart.compiler.ast.DartLiteral;
38 import com.google.dart.compiler.ast.DartMapLiteral; 37 import com.google.dart.compiler.ast.DartMapLiteral;
39 import com.google.dart.compiler.ast.DartMethodDefinition; 38 import com.google.dart.compiler.ast.DartMethodDefinition;
40 import com.google.dart.compiler.ast.DartMethodInvocation; 39 import com.google.dart.compiler.ast.DartMethodInvocation;
41 import com.google.dart.compiler.ast.DartNamedExpression; 40 import com.google.dart.compiler.ast.DartNamedExpression;
42 import com.google.dart.compiler.ast.DartNewExpression; 41 import com.google.dart.compiler.ast.DartNewExpression;
43 import com.google.dart.compiler.ast.DartNode; 42 import com.google.dart.compiler.ast.DartNode;
43 import com.google.dart.compiler.ast.DartNodeTraverser;
44 import com.google.dart.compiler.ast.DartParameter; 44 import com.google.dart.compiler.ast.DartParameter;
45 import com.google.dart.compiler.ast.DartPropertyAccess; 45 import com.google.dart.compiler.ast.DartPropertyAccess;
46 import com.google.dart.compiler.ast.DartRedirectConstructorInvocation; 46 import com.google.dart.compiler.ast.DartRedirectConstructorInvocation;
47 import com.google.dart.compiler.ast.DartStatement; 47 import com.google.dart.compiler.ast.DartStatement;
48 import com.google.dart.compiler.ast.DartStringInterpolation; 48 import com.google.dart.compiler.ast.DartStringInterpolation;
49 import com.google.dart.compiler.ast.DartStringLiteral; 49 import com.google.dart.compiler.ast.DartStringLiteral;
50 import com.google.dart.compiler.ast.DartSuperConstructorInvocation; 50 import com.google.dart.compiler.ast.DartSuperConstructorInvocation;
51 import com.google.dart.compiler.ast.DartSuperExpression; 51 import com.google.dart.compiler.ast.DartSuperExpression;
52 import com.google.dart.compiler.ast.DartSwitchMember; 52 import com.google.dart.compiler.ast.DartSwitchMember;
53 import com.google.dart.compiler.ast.DartSwitchStatement; 53 import com.google.dart.compiler.ast.DartSwitchStatement;
54 import com.google.dart.compiler.ast.DartThisExpression; 54 import com.google.dart.compiler.ast.DartThisExpression;
55 import com.google.dart.compiler.ast.DartTryStatement; 55 import com.google.dart.compiler.ast.DartTryStatement;
56 import com.google.dart.compiler.ast.DartTypeNode; 56 import com.google.dart.compiler.ast.DartTypeNode;
57 import com.google.dart.compiler.ast.DartTypedLiteral;
58 import com.google.dart.compiler.ast.DartUnit; 57 import com.google.dart.compiler.ast.DartUnit;
59 import com.google.dart.compiler.ast.DartUnqualifiedInvocation; 58 import com.google.dart.compiler.ast.DartUnqualifiedInvocation;
60 import com.google.dart.compiler.ast.DartVariable; 59 import com.google.dart.compiler.ast.DartVariable;
61 import com.google.dart.compiler.ast.DartVariableStatement; 60 import com.google.dart.compiler.ast.DartVariableStatement;
62 import com.google.dart.compiler.ast.DartWhileStatement; 61 import com.google.dart.compiler.ast.DartWhileStatement;
63 import com.google.dart.compiler.ast.Modifiers; 62 import com.google.dart.compiler.ast.Modifiers;
64 import com.google.dart.compiler.type.FunctionType; 63 import com.google.dart.compiler.type.FunctionType;
65 import com.google.dart.compiler.type.InterfaceType; 64 import com.google.dart.compiler.type.InterfaceType;
66 import com.google.dart.compiler.type.InterfaceType.Member; 65 import com.google.dart.compiler.type.InterfaceType.Member;
67 import com.google.dart.compiler.type.Type; 66 import com.google.dart.compiler.type.Type;
(...skipping 263 matching lines...) Expand 10 before | Expand all | Expand 10 after
331 330
332 if (Elements.isNonFactoryConstructor(member)) { 331 if (Elements.isNonFactoryConstructor(member)) {
333 resolveInitializers(node); 332 resolveInitializers(node);
334 } 333 }
335 334
336 context = previousContext; 335 context = previousContext;
337 innermostFunction = currentMethod = null; 336 innermostFunction = currentMethod = null;
338 return member; 337 return member;
339 } 338 }
340 339
341 private void resolveConstantExpression(DartExpression defaultExpr) { 340 private void resolveConstantExpression(DartExpression expr) {
342 resolve(defaultExpr); 341 resolve(expr);
343 checkConstantExpression(defaultExpr); 342 checkConstantExpression(expr);
344 } 343 }
345 344
346 private void checkConstantExpression(DartExpression defaultExpr) { 345 private void checkConstantExpression(DartExpression expr) {
347 // See bug 4568007. 346 if (expr != null) {
348 } 347 DartNodeTraverser<Void> v = CompileTimeConstVisitor.create(typeProvider, context);
349 348 expr.accept(v);
350 private void checkConstantLiteral(DartExpression defaultExpr) {
351 if ((!(defaultExpr instanceof DartLiteral))
352 || ((defaultExpr instanceof DartTypedLiteral)
353 && (!((DartTypedLiteral) defaultExpr).isConst()))) {
354 resolutionError(defaultExpr, DartCompilerErrorCode.EXPECTED_CONSTANT_LIT ERAL);
355 } 349 }
356 } 350 }
357 351
358 @Override 352 @Override
359 public Element visitField(DartField node) { 353 public Element visitField(DartField node) {
360 DartExpression expression = node.getValue(); 354 DartExpression expression = node.getValue();
361 Modifiers modifiers = node.getModifiers(); 355 Modifiers modifiers = node.getModifiers();
362 boolean isStatic = modifiers.isStatic(); 356 boolean isStatic = modifiers.isStatic();
363 boolean isFinal = modifiers.isFinal(); 357 boolean isFinal = modifiers.isFinal();
364 boolean isTopLevel = ElementKind.of(currentHolder).equals(ElementKind.LIBR ARY); 358 boolean isTopLevel = ElementKind.of(currentHolder).equals(ElementKind.LIBR ARY);
365 359
360 if (isTopLevel && isFinal) {
361 modifiers.makeStatic();
362 }
363
366 if (expression != null) { 364 if (expression != null) {
367 if (isStatic || isTopLevel) { 365 resolve(expression);
368 checkConstantExpression(expression); 366 checkConstantExpression(expression);
369 } else { 367 // Now, this constant has a type. Save it for future reference
floitsch 2011/10/14 22:46:15 end sentence with dot.
370 // TODO(5200401): Only allow constant literals for inline field initia lizers for now. 368 Element element = node.getSymbol();
371 checkConstantLiteral(expression); 369 if (expression.getType() != null) {
370 Elements.setType(element, expression.getType());
372 } 371 }
373 resolve(expression);
374 } else if (isStatic && isFinal) { 372 } else if (isStatic && isFinal) {
375 resolutionError(node, DartCompilerErrorCode.STATIC_FINAL_REQUIRES_VALUE) ; 373 resolutionError(node, DartCompilerErrorCode.STATIC_FINAL_REQUIRES_VALUE) ;
376 } 374 }
377 375
378 // If field is an acessor, both getter and setter need to be visited (if p resent). 376 // If field is an accessor, both getter and setter need to be visited (if present).
379 FieldElement field = node.getSymbol(); 377 FieldElement field = node.getSymbol();
380 if (field.getGetter() != null) { 378 if (field.getGetter() != null) {
381 resolve(field.getGetter().getNode()); 379 resolve(field.getGetter().getNode());
382 } 380 }
383 if (field.getSetter() != null) { 381 if (field.getSetter() != null) {
384 resolve(field.getSetter().getNode()); 382 resolve(field.getSetter().getNode());
385 } 383 }
386 return null; 384 return null;
387 } 385 }
388 386
(...skipping 458 matching lines...) Expand 10 before | Expand all | Expand 10 after
847 845
848 @Override 846 @Override
849 public Element visitFunctionObjectInvocation(DartFunctionObjectInvocation x) { 847 public Element visitFunctionObjectInvocation(DartFunctionObjectInvocation x) {
850 x.getTarget().accept(this); 848 x.getTarget().accept(this);
851 visit(x.getArgs()); 849 visit(x.getArgs());
852 return null; 850 return null;
853 } 851 }
854 852
855 @Override 853 @Override
856 public Element visitNewExpression(DartNewExpression x) { 854 public Element visitNewExpression(DartNewExpression x) {
855
857 this.visit(x.getArgs()); 856 this.visit(x.getArgs());
858 857
858 if (x.isConst()) {
859 for (DartExpression arg : x.getArgs()) {
860 checkConstantExpression(arg);
861 }
862 }
863
859 Element element = x.getConstructor().accept(getContext().new Selector() { 864 Element element = x.getConstructor().accept(getContext().new Selector() {
860 // Only 'new' expressions can have a type in a property access. 865 // Only 'new' expressions can have a type in a property access.
861 @Override public Element visitTypeNode(DartTypeNode type) { 866 @Override public Element visitTypeNode(DartTypeNode type) {
862 return recordType(type, resolveType(type, inStaticContext(currentMetho d))); 867 return recordType(type, resolveType(type, inStaticContext(currentMetho d)));
863 } 868 }
864 869
865 @Override public Element visitPropertyAccess(DartPropertyAccess node) { 870 @Override public Element visitPropertyAccess(DartPropertyAccess node) {
866 Element element = node.getQualifier().accept(this); 871 Element element = node.getQualifier().accept(this);
867 if (ElementKind.of(element).equals(ElementKind.CLASS)) { 872 if (ElementKind.of(element).equals(ElementKind.CLASS)) {
868 return Elements.lookupConstructor(((ClassElement) element), node.get PropertyName()); 873 return Elements.lookupConstructor(((ClassElement) element), node.get PropertyName());
(...skipping 348 matching lines...) Expand 10 before | Expand all | Expand 10 after
1217 && ElementKind.of(target).equals(ElementKind.METHOD)) { 1222 && ElementKind.of(target).equals(ElementKind.METHOD)) {
1218 if (!target.getModifiers().isStatic() && !Elements.isTopLevel(target)) { 1223 if (!target.getModifiers().isStatic() && !Elements.isTopLevel(target)) {
1219 resolutionError(node, DartCompilerErrorCode.INSTANCE_METHOD_FROM_STATI C); 1224 resolutionError(node, DartCompilerErrorCode.INSTANCE_METHOD_FROM_STATI C);
1220 } 1225 }
1221 } 1226 }
1222 } 1227 }
1223 1228
1224 private void checkVariableStatement(DartVariableStatement node, 1229 private void checkVariableStatement(DartVariableStatement node,
1225 DartVariable variable, 1230 DartVariable variable,
1226 boolean isImplicitlyInitialized) { 1231 boolean isImplicitlyInitialized) {
1227 if (node.getModifiers().isFinal()) { 1232 Modifiers modifiers = node.getModifiers();
1233 if (modifiers.isFinal()) {
1228 if (!isImplicitlyInitialized && (variable.getValue() == null)) { 1234 if (!isImplicitlyInitialized && (variable.getValue() == null)) {
1229 resolutionError(variable.getName(), DartCompilerErrorCode.CONSTANTS_MU ST_BE_INITIALIZED); 1235 resolutionError(variable.getName(), DartCompilerErrorCode.CONSTANTS_MU ST_BE_INITIALIZED);
1230 } else if (isImplicitlyInitialized && (variable.getValue() != null)) { 1236 } else if (isImplicitlyInitialized && (variable.getValue() != null)) {
1231 resolutionError(variable.getName(), DartCompilerErrorCode.CANNOT_BE_IN ITIALIZED); 1237 resolutionError(variable.getName(), DartCompilerErrorCode.CANNOT_BE_IN ITIALIZED);
1232 } else { 1238 } else if (modifiers.isStatic() && modifiers.isFinal() && variable.getVa lue() != null) {
1233 checkConstantExpression(variable.getValue()); 1239 resolveConstantExpression(variable.getValue());
1240 node.setType(variable.getValue().getType());
1234 } 1241 }
1235 } 1242 }
1236 } 1243 }
1237 1244
1238 private void checkParameterInitializer(DartMethodDefinition method, DartPara meter parameter) { 1245 private void checkParameterInitializer(DartMethodDefinition method, DartPara meter parameter) {
1239 if (Elements.isNonFactoryConstructor(method.getSymbol())) { 1246 if (Elements.isNonFactoryConstructor(method.getSymbol())) {
1240 if (method.getModifiers().isRedirectedConstructor()) { 1247 if (method.getModifiers().isRedirectedConstructor()) {
1241 resolutionError(parameter.getName(), 1248 resolutionError(parameter.getName(),
1242 DartCompilerErrorCode.PARAMETER_INIT_WITH_REDIR_CONSTRUCTOR); 1249 DartCompilerErrorCode.PARAMETER_INIT_WITH_REDIR_CONSTRUCTOR);
1243 } 1250 }
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
1344 ClassElement nextClass = (ClassElement) nextConstructorElement.getEnclos ingElement(); 1351 ClassElement nextClass = (ClassElement) nextConstructorElement.getEnclos ingElement();
1345 ClassElement currentClass = (ClassElement) constructor.getEnclosingEleme nt(); 1352 ClassElement currentClass = (ClassElement) constructor.getEnclosingEleme nt();
1346 if (nextClass.getName().equals(currentClass.getName())) { 1353 if (nextClass.getName().equals(currentClass.getName())) {
1347 return nextConstructorElement; 1354 return nextConstructorElement;
1348 } 1355 }
1349 } 1356 }
1350 } 1357 }
1351 return null; 1358 return null;
1352 } 1359 }
1353 } 1360 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698