Chromium Code Reviews| Index: compiler/java/com/google/dart/compiler/resolver/Resolver.java |
| diff --git a/compiler/java/com/google/dart/compiler/resolver/Resolver.java b/compiler/java/com/google/dart/compiler/resolver/Resolver.java |
| index 7af6cf664b40f2c339ae930a08c7bd6e17b947fd..1082e4846a50859b704c584f132d0884027ad0b0 100644 |
| --- a/compiler/java/com/google/dart/compiler/resolver/Resolver.java |
| +++ b/compiler/java/com/google/dart/compiler/resolver/Resolver.java |
| @@ -59,6 +59,7 @@ import com.google.dart.compiler.ast.DartUnit; |
| import com.google.dart.compiler.ast.DartUnqualifiedInvocation; |
| import com.google.dart.compiler.ast.DartVariable; |
| import com.google.dart.compiler.ast.DartVariableStatement; |
| +import com.google.dart.compiler.ast.DartVisitor; |
| import com.google.dart.compiler.ast.DartWhileStatement; |
| import com.google.dart.compiler.ast.Modifiers; |
| import com.google.dart.compiler.type.FunctionType; |
| @@ -338,20 +339,32 @@ public class Resolver { |
| return member; |
| } |
| - private void resolveConstantExpression(DartExpression defaultExpr) { |
| - resolve(defaultExpr); |
| - checkConstantExpression(defaultExpr); |
| + private void resolveConstantExpression(DartExpression expr) { |
| + resolve(expr); |
| + checkConstantExpression(expr); |
| } |
| - private void checkConstantExpression(DartExpression defaultExpr) { |
| - // See bug 4568007. |
| + private void checkConstantExpression(DartExpression expr) { |
| + if (expr != null) { |
| + DartVisitor v = CompileTimeConstVisitor.create(typeProvider, context); |
| + v.accept(expr); |
| + } |
| } |
| - private void checkConstantLiteral(DartExpression defaultExpr) { |
| - if ((!(defaultExpr instanceof DartLiteral)) |
| - || ((defaultExpr instanceof DartTypedLiteral) |
| - && (!((DartTypedLiteral) defaultExpr).isConst()))) { |
| - resolutionError(defaultExpr, DartCompilerErrorCode.EXPECTED_CONSTANT_LITERAL); |
| + private void checkConstantLiteral(DartExpression expr) { |
| + boolean isConstant = true; |
| + if (expr instanceof DartStringInterpolation) { |
| + isConstant = false; |
| + } else if (expr instanceof DartLiteral) { |
| + if (expr instanceof DartTypedLiteral && !((DartTypedLiteral) expr).isConst()){ |
| + isConstant = false; |
| + } |
| + } else { |
| + isConstant = false; |
| + } |
| + |
| + if (!isConstant) { |
| + resolutionError(expr, DartCompilerErrorCode.EXPECTED_CONSTANT_LITERAL); |
| } |
| } |
| @@ -364,18 +377,23 @@ public class Resolver { |
| boolean isTopLevel = ElementKind.of(currentHolder).equals(ElementKind.LIBRARY); |
| if (expression != null) { |
| + resolve(expression); |
| if (isStatic || isTopLevel) { |
| checkConstantExpression(expression); |
| + // Now, this constant has a type. Save it for future reference |
| + Element element = node.getSymbol(); |
| + if (expression.getType() != null) { |
| + Elements.setType(element, expression.getType()); |
| + } |
| } else { |
| // TODO(5200401): Only allow constant literals for inline field initializers for now. |
| checkConstantLiteral(expression); |
|
ngeoffray
2011/10/14 09:26:56
Could you remove that TODO now? We should be able
zundel
2011/10/14 10:04:21
I wasn't sure that non static variables needed to
ngeoffray
2011/10/14 10:33:42
Right now, non-static variables can only have comp
floitsch
2011/10/14 14:36:58
I agree. This should be a checkConstantExpression.
|
| } |
| - resolve(expression); |
| } else if (isStatic && isFinal) { |
| resolutionError(node, DartCompilerErrorCode.STATIC_FINAL_REQUIRES_VALUE); |
| } |
| - // If field is an acessor, both getter and setter need to be visited (if present). |
| + // If field is an accessor, both getter and setter need to be visited (if present). |
| FieldElement field = node.getSymbol(); |
| if (field.getGetter() != null) { |
| resolve(field.getGetter().getNode()); |
| @@ -854,8 +872,15 @@ public class Resolver { |
| @Override |
| public Element visitNewExpression(DartNewExpression x) { |
| + |
| this.visit(x.getArgs()); |
| + if (x.isConst()) { |
| + for (DartExpression arg : x.getArgs()) { |
| + checkConstantExpression(arg); |
| + } |
| + } |
| + |
| Element element = x.getConstructor().accept(getContext().new Selector() { |
| // Only 'new' expressions can have a type in a property access. |
| @Override public Element visitTypeNode(DartTypeNode type) { |
| @@ -1224,13 +1249,15 @@ public class Resolver { |
| private void checkVariableStatement(DartVariableStatement node, |
| DartVariable variable, |
| boolean isImplicitlyInitialized) { |
| - if (node.getModifiers().isFinal()) { |
| + Modifiers modifiers = node.getModifiers(); |
| + if (modifiers.isFinal()) { |
| if (!isImplicitlyInitialized && (variable.getValue() == null)) { |
| resolutionError(variable.getName(), DartCompilerErrorCode.CONSTANTS_MUST_BE_INITIALIZED); |
| } else if (isImplicitlyInitialized && (variable.getValue() != null)) { |
| resolutionError(variable.getName(), DartCompilerErrorCode.CANNOT_BE_INITIALIZED); |
| - } else { |
| - checkConstantExpression(variable.getValue()); |
| + } else if (modifiers.isConstant() && variable.getValue() != null) { |
| + resolveConstantExpression(variable.getValue()); |
| + node.setType(variable.getValue().getType()); |
| } |
| } |
| } |