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

Unified Diff: lib/compiler/implementation/resolver.dart

Issue 10915083: Change assert implementation to not depend on a top-level function called 'assert'. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Moved _assert to js_helper. Created 8 years, 3 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/compiler/implementation/lib/mock.dart ('k') | lib/compiler/implementation/scanner/keyword.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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";
« no previous file with comments | « lib/compiler/implementation/lib/mock.dart ('k') | lib/compiler/implementation/scanner/keyword.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698