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

Unified Diff: frog/leg/resolver.dart

Issue 8974014: Resolve initializers in constructors. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Add tests. Created 9 years 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
diff --git a/frog/leg/resolver.dart b/frog/leg/resolver.dart
index 6a37626531eabdbbdd8a13c25f8fa34068d4fbb9..c094cb1b5daa9d58ca6427986286c770c26b5d61 100644
--- a/frog/leg/resolver.dart
+++ b/frog/leg/resolver.dart
@@ -20,13 +20,20 @@ class ResolverTask extends CompilerTask {
TreeElements resolve(FunctionElement element) {
return measure(() {
FunctionExpression tree = element.parseNode(compiler, compiler);
- if (tree.initializers !== null) {
- compiler.cancel('initializers are not implemented',
- node: tree.initializers);
- }
ResolverVisitor visitor = new SignatureResolverVisitor(compiler, element);
visitor.visit(tree);
+ if (tree.initializers != null) {
+ visitor = new FullResolverVisitor.from(visitor);
+ for (Link<Node> link = tree.initializers.nodes;
+ !link.isEmpty();
+ link = link.tail) {
+ SendSet init = link.head;
floitsch 2011/12/19 17:25:29 How can you be sure that this is a SendSet? What a
karlklose 2011/12/21 10:19:44 Done, added a test.
+ Node value = init.arguments.head;
+ visitor.visitIn(value, new StaticScope(visitor.context));
+ }
+ }
+
visitor = new FullResolverVisitor.from(visitor);
visitor.visit(tree.body);
@@ -143,7 +150,6 @@ class SignatureResolverVisitor extends ResolverVisitor {
visitFunctionExpression(FunctionExpression node) {
useElement(node, element);
context = new MethodScope(context, element);
-
if (element.parameters == null) {
ParametersVisitor visitor = new ParametersVisitor(this);
visitor.visit(node.parameters);
@@ -157,6 +163,37 @@ class SignatureResolverVisitor extends ResolverVisitor {
}
}
+ if (node.initializers !== null) {
ngeoffray 2011/12/20 15:09:51 As discussed, I don't think this is the right plac
karlklose 2011/12/21 10:19:44 Done.
+ Set<SourceString> initializedNames = new Set<SourceString>();
+ for (Link<Node> link = node.initializers.nodes;
+ !link.isEmpty();
+ link = link.tail) {
+ SendSet init = link.head.asSendSet();
+ if (init == null
floitsch 2011/12/19 17:25:29 super call is missing. Add TODO?
ahe 2011/12/19 18:07:26 In addition, there might be a this(...) send.
karlklose 2011/12/21 10:19:44 Done, added a todo for super(...) and this(...).
+ || init.assignmentOperator.token.stringValue != '=') {
+ compiler.cancel('internal error: invalid initializer',
+ node: node.initializers);
ahe 2011/12/19 18:07:26 "node.initializers" should be "link.head".
karlklose 2011/12/21 10:19:44 Done.
+ } else {
+ if (init.receiver != null &&
+ (init.receiver.asIdentifier() == null
+ || init.receiver.asIdentifier().source.stringValue != 'this')) {
+ error(init, MessageKind.INVALID_RECEIVER_IN_INITIALIZER);
+ }
+ SourceString name = init.selector.asIdentifier().source;
ahe 2011/12/19 18:07:26 The above code looks a bit brittle and seems to re
karlklose 2011/12/21 10:19:44 Done.
+ ClassElement classElement = element.enclosingElement;
+ Element target = classElement.lookupLocalElement(name);
+ if (target == null) {
+ error(init, MessageKind.CANNOT_RESOLVE, [name]);
+ }
+ if (initializedNames.contains(name)) {
+ warning(init, MessageKind.DUPLICATION_INITIALIZATION, [name]);
+ }
+ initializedNames.add(name);
+ useElement(init.selector, target);
floitsch 2011/12/20 14:18:27 This should be useElement(init, target);
karlklose 2011/12/21 10:19:44 Done.
+ }
+ }
+ }
+
return element;
}
}
@@ -555,7 +592,7 @@ class Scope {
Scope(this.parent, this.element);
abstract Element add(Element element);
- abstract Element lookup(Element element);
+ abstract Element lookup(SourceString name);
ngeoffray 2011/12/20 15:09:51 Thanks!
}
class MethodScope extends Scope {
@@ -600,6 +637,17 @@ class ClassScope extends Scope {
}
}
+class StaticScope extends Scope {
+ StaticScope(Scope parent) : super(parent, null);
+
+ Element lookup(SourceString name) {
ahe 2011/12/19 18:07:26 This concerns me as it seems to create an extra na
ngeoffray 2011/12/20 15:09:51 Two suggestions to address this problem. 1) Create
+ Element result = parent.lookup(name);
+ return (result != null && !result.isInstanceMember()) ? result : null;
+ }
+
+ Element add(Element element) => parent.add(element);
ngeoffray 2011/12/20 15:09:51 Do we expect to add to this scope?
+}
+
// TODO(ngeoffray): this top scope should have libraryElement as
// element.
class TopScope extends Scope {
« no previous file with comments | « frog/leg/elements/elements.dart ('k') | frog/leg/warnings.dart » ('j') | frog/leg/warnings.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698