Index: lib/compiler/implementation/resolver.dart |
diff --git a/lib/compiler/implementation/resolver.dart b/lib/compiler/implementation/resolver.dart |
index 4b6a8f49fdbe5ed61d7e91227689569694408830..4484391c619b89d2d7b9702203b72dff9eddf389 100644 |
--- a/lib/compiler/implementation/resolver.dart |
+++ b/lib/compiler/implementation/resolver.dart |
@@ -954,6 +954,7 @@ class ResolverVisitor extends CommonResolverVisitor<Element> { |
bool inInstanceContext; |
Scope scope; |
ClassElement currentClass; |
+ ExpressionStatement currentExpressionStatement; |
bool typeRequired = false; |
StatementScope statementScope; |
int allowedCategory = ElementCategory.VARIABLE | ElementCategory.FUNCTION; |
@@ -1138,7 +1139,10 @@ class ResolverVisitor extends CommonResolverVisitor<Element> { |
visitEmptyStatement(EmptyStatement node) { } |
visitExpressionStatement(ExpressionStatement node) { |
+ ExpressionStatement oldExpressionStatement = currentExpressionStatement; |
+ currentExpressionStatement = node; |
visit(node.expression); |
+ currentExpressionStatement = oldExpressionStatement; |
} |
visitFor(For node) { |
@@ -1330,7 +1334,35 @@ class ResolverVisitor extends CommonResolverVisitor<Element> { |
} |
} |
+ /** |
+ * Checks if a [Send] represents an assertionStatement. |
+ */ |
+ bool isAssert(Send send) { |
+ if (currentExpressionStatement === null || |
+ currentExpressionStatement.expression !== send) return false; |
+ if (send.receiver !== null) return false; |
+ if (!send.isCall) return false; |
+ Identifier selector = send.selector.asIdentifier(); |
+ if (selector === null) return false; |
+ if (selector.source.stringValue !== "assert") return false; |
ahe
2012/09/05 13:12:24
I would think this should be the first thing to ch
|
+ if (send.argumentCount() != 1) return false; |
+ return true; |
+ } |
+ |
visitSend(Send node) { |
+ if (isAssert(node)) { |
ahe
2012/09/05 13:12:24
I still think you're fighting the system. Let's ch
|
+ Element scopedAssert = scope.lexicalLookup(const SourceString("assert")); |
+ if (scopedAssert == null) { |
+ // Compile as a call to a top-level function. |
+ Element target = compiler.assertMethod; |
+ Selector selector = resolveSelector(node); |
+ resolveArguments(node.argumentsNode); |
+ useElement(node, target); |
+ registerSend(selector, target); |
+ return; |
+ } |
+ } |
+ |
Element target = resolveSend(node); |
if (!Element.isInvalid(target) |
&& target.kind == ElementKind.ABSTRACT_FIELD) { |
@@ -1379,16 +1411,6 @@ class ResolverVisitor extends CommonResolverVisitor<Element> { |
world.registerDynamicInvocation(call.name, call); |
} |
- // TODO(ngeoffray): We should do the check in |
- // visitExpressionStatement instead. |
- if (target === compiler.assertMethod && !node.isCall) { |
- // We can only use assert by calling it. |
- if (!inInstanceContext) { |
- error(node, MessageKind.MISSING_ARGUMENTS_TO_ASSERT, [node]); |
- } |
- target = null; |
- } |
- |
// TODO(ngeoffray): Warn if target is null and the send is |
// unqualified. |
useElement(node, target); |
@@ -2557,7 +2579,20 @@ class Scope { |
Scope(this.parent, this.element); |
abstract Element add(Element element); |
- abstract Element lookup(SourceString name); |
+ |
+ Element lookup(SourceString name) { |
+ Element result = localLookup(name); |
+ if (result != null) return result; |
+ return parent.lookup(name); |
+ } |
+ |
+ Element lexicalLookup(SourceString name) { |
+ Element result = localLookup(name); |
+ if (result != null) return result; |
+ return parent.lexicalLookup(name); |
+ } |
+ |
+ abstract Element localLookup(SourceString name); |
} |
/** |
@@ -2586,7 +2621,7 @@ class TypeDeclarationScope extends Scope { |
return null; |
} |
- Element lookup(SourceString name) { |
+ Element localLookup(SourceString name) { |
Link<DartType> typeVariableLink = element.typeVariables; |
while (!typeVariableLink.isEmpty()) { |
TypeVariableType typeVariable = typeVariableLink.head; |
@@ -2595,8 +2630,7 @@ class TypeDeclarationScope extends Scope { |
} |
typeVariableLink = typeVariableLink.tail; |
} |
- |
- return parent.lookup(name); |
+ return null; |
} |
String toString() => |
@@ -2612,11 +2646,7 @@ class MethodScope extends Scope { |
assert(parent !== null); |
} |
- Element lookup(SourceString name) { |
- Element found = elements[name]; |
- if (found !== null) return found; |
- return parent.lookup(name); |
- } |
+ Element localLookup(SourceString name) => elements[name]; |
Element add(Element newElement) { |
if (elements.containsKey(newElement.name)) { |
@@ -2644,12 +2674,19 @@ class ClassScope extends TypeDeclarationScope { |
ClassScope(Scope parentScope, ClassElement element) |
: super(parentScope, element); |
- Element lookup(SourceString name) { |
+ Element localLookup(SourceString name) { |
ClassElement cls = element; |
Element result = cls.lookupLocalMember(name); |
if (result !== null) return result; |
- result = super.lookup(name); |
- if (result != null) return result; |
+ return super.localLookup(name); |
+ } |
+ |
+ Element lookup(SourceString name) { |
+ Element result = localLookup(name); |
+ if (result !== null) return result; |
+ result = parent.lexicalLookup(name); |
+ if (result !== null) return result; |
+ ClassElement cls = element; |
return cls.lookupSuperMember(name); |
} |
@@ -2664,9 +2701,10 @@ class TopScope extends Scope { |
LibraryElement get library => element; |
TopScope(LibraryElement library) : super(null, library); |
- Element lookup(SourceString name) { |
- return library.find(name); |
- } |
+ |
+ Element localLookup(SourceString name) => library.find(name); |
+ Element lookup(SourceString name) => localLookup(name); |
+ Element lexicalLookup(SourceString name) => localLookup(name); |
Element add(Element newElement) { |
throw "Cannot add an element in the top scope"; |