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

Side by Side Diff: frog/leg/elements/elements.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 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 #library('elements'); 5 #library('elements');
6 6
7 #import('../tree/tree.dart'); 7 #import('../tree/tree.dart');
8 #import('../scanner/scannerlib.dart'); 8 #import('../scanner/scannerlib.dart');
9 #import('../leg.dart'); // TODO(karlklose): we only need type. 9 #import('../leg.dart'); // TODO(karlklose): we only need type.
10 #import('../util/util.dart'); 10 #import('../util/util.dart');
(...skipping 11 matching lines...) Expand all
22 class ElementKind { 22 class ElementKind {
23 final String id; 23 final String id;
24 24
25 const ElementKind(String this.id); 25 const ElementKind(String this.id);
26 26
27 static final ElementKind VARIABLE = const ElementKind('variable'); 27 static final ElementKind VARIABLE = const ElementKind('variable');
28 static final ElementKind PARAMETER = const ElementKind('parameter'); 28 static final ElementKind PARAMETER = const ElementKind('parameter');
29 static final ElementKind FUNCTION = const ElementKind('function'); 29 static final ElementKind FUNCTION = const ElementKind('function');
30 static final ElementKind CLASS = const ElementKind('class'); 30 static final ElementKind CLASS = const ElementKind('class');
31 static final ElementKind FOREIGN = const ElementKind('foreign'); 31 static final ElementKind FOREIGN = const ElementKind('foreign');
32 static final ElementKind CONSTRUCTOR = const ElementKind('constructor');
32 33
33 toString() => id; 34 toString() => id;
34 } 35 }
35 36
36 class Element implements Hashable { 37 class Element implements Hashable {
37 final SourceString name; 38 final SourceString name;
38 final ElementKind kind; 39 final ElementKind kind;
39 final Element enclosingElement; 40 final Element enclosingElement;
40 abstract Node parseNode(Canceler canceler, Logger logger); 41 abstract Node parseNode(Canceler canceler, Logger logger);
41 abstract Type computeType(Compiler compiler, Types types); 42 abstract Type computeType(Compiler compiler, Types types);
43 bool isClassMember() =>
44 enclosingElement !== null && enclosingElement.kind == ElementKind.CLASS;
42 45
43 const Element(this.name, this.kind, this.enclosingElement); 46 const Element(this.name, this.kind, this.enclosingElement);
44 47
45 // TODO(kasperl): This is a very bad hash code for the element and 48 // TODO(kasperl): This is a very bad hash code for the element and
46 // there's no reason why two elements with the same name should have 49 // there's no reason why two elements with the same name should have
47 // the same hash code. Replace this with a simple id in the element? 50 // the same hash code. Replace this with a simple id in the element?
48 int hashCode() => name.hashCode(); 51 int hashCode() => name.hashCode();
49 } 52 }
50 53
51 class VariableElement extends Element { 54 class VariableElement extends Element {
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 return types.dynamicType; 91 return types.dynamicType;
89 } 92 }
90 return types.lookup(annotation.typeName.source); 93 return types.lookup(annotation.typeName.source);
91 } 94 }
92 95
93 class FunctionElement extends Element { 96 class FunctionElement extends Element {
94 Link<Element> parameters; 97 Link<Element> parameters;
95 FunctionExpression node; 98 FunctionExpression node;
96 Type type; 99 Type type;
97 100
98 // TODO(ngeoffray): set the enclosingElement. 101 FunctionElement(SourceString name,
99 FunctionElement(SourceString name) : super(name, ElementKind.FUNCTION, null); 102 ElementKind kind,
100 FunctionElement.node(FunctionExpression node, Element enclosing) 103 Element enclosing)
101 : super(node.name.dynamic.source, ElementKind.FUNCTION, enclosing), 104 : super(name, kind, enclosing);
105 FunctionElement.node(FunctionExpression node,
106 ElementKind kind,
107 Element enclosing)
108 : super(node.name.asIdentifier().source, kind, enclosing),
102 this.node = node; 109 this.node = node;
103 110
104 FunctionType computeType(Compiler compiler, types) { 111 FunctionType computeType(Compiler compiler, types) {
105 if (type != null) return type; 112 if (type != null) return type;
106 if (parameters == null) compiler.resolveSignature(this); 113 if (parameters == null) compiler.resolveSignature(this);
107 114
108 FunctionExpression node = parseNode(compiler, compiler); 115 FunctionExpression node = parseNode(compiler, compiler);
109 Type returnType = getType(node.returnType, types); 116 Type returnType = getType(node.returnType, types);
110 if (returnType === null) compiler.cancel('unknown type ${node.returnType}'); 117 if (returnType === null) compiler.cancel('unknown type ${node.returnType}');
111 118
112 LinkBuilder<Type> parameterTypes = new LinkBuilder<Type>(); 119 LinkBuilder<Type> parameterTypes = new LinkBuilder<Type>();
113 for (Link<Element> link = parameters; !link.isEmpty(); link = link.tail) { 120 for (Link<Element> link = parameters; !link.isEmpty(); link = link.tail) {
114 parameterTypes.addLast(link.head.computeType(compiler, types)); 121 parameterTypes.addLast(link.head.computeType(compiler, types));
115 } 122 }
116 type = new FunctionType(returnType, parameterTypes.toLink()); 123 type = new FunctionType(returnType, parameterTypes.toLink());
117 return type; 124 return type;
118 } 125 }
119 126
120 Node parseNode(Canceler canceler, Logger logger) => node; 127 Node parseNode(Canceler canceler, Logger logger) => node;
121 } 128 }
122 129
130 class SynthesizedConstructorElement extends FunctionElement {
131 SynthesizedConstructorElement(Element enclosing)
132 : super(const SourceString(''), ElementKind.CONSTRUCTOR, enclosing) {
133 parameters = const EmptyLink<Element>();
134 }
135
136 FunctionType computeType(Compiler compiler, types) {
137 if (type != null) return type;
138 type = new FunctionType(types.voidType, const EmptyLink<Type>());
139 return type;
140 }
141
142 Node parseNode(Canceler canceler, Logger logger) {
143 if (node != null) return node;
144 node = new FunctionExpression(
145 new Identifier(new StringToken(null, '', null)),
ahe 2011/12/02 11:27:41 Please add a constructor like: new Identifier.syn
ngeoffray 2011/12/02 14:44:54 Done.
146 new NodeList.empty(),
147 new Block(new NodeList.empty()));
148 return node;
149 }
150 }
151
123 class ClassElement extends Element { 152 class ClassElement extends Element {
124 Type type; 153 Type type;
125 Type supertype; 154 Type supertype;
126 Link<Type> interfaces = const EmptyLink<Type>(); 155 Link<Type> interfaces = const EmptyLink<Type>();
127 bool isResolved = false; 156 bool isResolved = false;
128 157
129 ClassElement(SourceString name) : super(name, ElementKind.CLASS, null); 158 ClassElement(SourceString name) : super(name, ElementKind.CLASS, null);
130 159
131 Type computeType(compiler, types) { 160 Type computeType(compiler, types) {
132 if (type === null) { 161 if (type === null) {
133 type = new SimpleType(name, this); 162 type = new SimpleType(name, this);
134 } 163 }
135 return type; 164 return type;
136 } 165 }
137 166
138 void resolve(Compiler compiler) { 167 void resolve(Compiler compiler) {
139 if (isResolved) return; 168 if (isResolved) return;
140 compiler.resolveType(this); 169 compiler.resolveType(this);
141 isResolved = true; 170 isResolved = true;
142 } 171 }
172
173 // TODO(ngeoffray): Implement these.
174 Element lookupLocalElement(SourceString name) => null;
175 bool canHaveDefaultConstructor() => true;
176 void addConstructor(Element element) {}
143 } 177 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698