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

Unified Diff: frog/leg/resolver.dart

Issue 8769012: Start resolving classes and default constructors. (Closed) Base URL: http://dart.googlecode.com/svn/branches/bleeding_edge/dart/
Patch Set: '' Created 9 years, 1 month 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
Index: frog/leg/resolver.dart
===================================================================
--- frog/leg/resolver.dart (revision 1986)
+++ frog/leg/resolver.dart (working copy)
@@ -16,9 +16,9 @@
String get name() => 'Resolver';
- TreeElements resolve(FunctionExpression tree) {
+ TreeElements resolve(FunctionExpression tree, FunctionElement element) {
return measure(() {
- ResolverVisitor visitor = new SignatureResolverVisitor(compiler);
+ ResolverVisitor visitor = new SignatureResolverVisitor(compiler, element);
visitor.visit(tree);
visitor = new FullResolverVisitor.from(visitor);
@@ -32,28 +32,17 @@
});
}
- // Used for testing.
- TreeElements resolveStatement(Node node) {
- ResolverVisitor visitor = new FullResolverVisitor(compiler);
- visitor.visit(node);
-
- // Resolve the type annotations encountered in the code.
- while (!toResolve.isEmpty()) {
- toResolve.removeFirst().resolve(compiler);
- }
- return visitor.mapping;
- }
-
- void resolveType(ClassNode tree) {
+ void resolveType(ClassNode tree, Element element) {
measure(() {
ClassResolverVisitor visitor = new ClassResolverVisitor(compiler);
visitor.visit(tree);
});
}
- void resolveSignature(FunctionExpression node) {
+ void resolveSignature(FunctionExpression node, FunctionElement element) {
measure(() {
- SignatureResolverVisitor visitor = new SignatureResolverVisitor(compiler);
+ SignatureResolverVisitor visitor =
+ new SignatureResolverVisitor(compiler, element);
visitor.visitFunctionExpression(node);
});
}
@@ -64,10 +53,12 @@
final TreeElements mapping;
Scope context;
- ResolverVisitor(Compiler compiler)
+ ResolverVisitor(Compiler compiler, Element element)
: this.compiler = compiler,
karlklose 2011/12/05 14:54:55 Extra space in the following two lines.
- mapping = new TreeElements(),
- context = new Scope(new TopScope(compiler.universe));
+ this.mapping = new TreeElements(),
+ this.context = element.isClassMember()
+ ? new ClassScope(element.enclosingElement, compiler.universe)
+ : new TopScope(compiler.universe);
ResolverVisitor.from(ResolverVisitor other)
: compiler = other.compiler,
@@ -136,34 +127,36 @@
}
class SignatureResolverVisitor extends ResolverVisitor {
+ FunctionElement element;
- SignatureResolverVisitor(Compiler compiler) : super(compiler);
+ SignatureResolverVisitor(Compiler compiler, FunctionElement element)
+ : super(compiler, element), this.element = element;
visitFunctionExpression(FunctionExpression node) {
- FunctionElement enclosingElement = context.lookup(node.name.dynamic.source);
- useElement(node, enclosingElement);
- context = new Scope.enclosing(context, enclosingElement);
+ useElement(node, element);
+ context = new Scope.enclosing(context, element);
- if (enclosingElement.parameters == null) {
+ if (element.parameters == null) {
ParametersVisitor visitor = new ParametersVisitor(this);
visitor.visit(node.parameters);
- enclosingElement.parameters = visitor.elements.toLink();
+ element.parameters = visitor.elements.toLink();
} else {
Link<Node> parameterNodes = node.parameters.nodes;
- for (Link<Element> link = enclosingElement.parameters;
+ for (Link<Element> link = element.parameters;
!link.isEmpty() && !parameterNodes.isEmpty();
link = link.tail, parameterNodes = parameterNodes.tail) {
defineElement(parameterNodes.head.definitions.nodes.head, link.head);
}
}
- return enclosingElement;
+ return element;
}
}
class FullResolverVisitor extends ResolverVisitor {
- FullResolverVisitor(Compiler compiler) : super(compiler);
+ FullResolverVisitor(Compiler compiler, Element element)
+ : super(compiler, element);
FullResolverVisitor.from(ResolverVisitor other) : super.from(other);
Element visitClassNode(ClassNode node) {
@@ -200,8 +193,8 @@
visitFunctionExpression(FunctionExpression node) {
visit(node.returnType);
- FunctionElement enclosingElement =
- new FunctionElement.node(node, context.enclosingElement);
+ FunctionElement enclosingElement = new FunctionElement.node(
+ node, ElementKind.FUNCTION, context.enclosingElement);
defineElement(node, enclosingElement);
context = new Scope.enclosing(context, enclosingElement);
@@ -381,7 +374,26 @@
}
visitNewExpression(NewExpression node) {
- cancel(node, "Unimplemented");
+ visit(node.send.argumentsNode);
+
+ ClassElement cls = visit(node.send.selector);
+ SourceString name = const SourceString("");
+ Element constructor = null;
+ if (cls !== null) {
+ Element constructor = cls.lookupLocalElement(name);
+ if (name.stringValue === ''
+ && constructor === null
+ && node.send.argumentsNode.isEmpty()
+ && cls.canHaveDefaultConstructor()) {
+ constructor = new SynthesizedConstructorElement(cls);
+ cls.addConstructor(constructor);
+ }
+ if (constructor === null) {
+ error(node, MessageKind.CANNOT_FIND_CONSTRUCTOR, [node]);
+ }
+ }
+
+ return useElement(node, constructor);
}
}
@@ -513,6 +525,25 @@
}
}
+class ClassScope extends Scope {
+ ClassScope(ClassElement element, Universe universe)
+ : super.enclosing(new TopScope(universe), element);
+
+ Element lookup(SourceString name) {
+ ClassElement cls = enclosingElement;
ahe 2011/12/02 11:27:41 The word "enclosingElement" seems wrong in this co
ngeoffray 2011/12/02 14:44:54 Changed to 'element'.
+ Element element = cls.lookupLocalElement(name);
+ if (element != null) return element;
+ element = parent.lookup(name);
+ if (element != null) return element;
+ // TODO(ngeoffray): Lookup in the super class.
+ return null;
+ }
+
+ Element add(Element element) {
+ throw "Cannot add an element in a class scope";
+ }
+}
+
// TODO(ngeoffray): this top scope should have libraryElement as
// enclosingElement.
class TopScope extends Scope {

Powered by Google App Engine
This is Rietveld 408576698