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

Side by Side Diff: frog/leg/elements/elements.dart

Issue 9027002: Support factory methods. (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 static final ElementKind GENERATIVE_CONSTRUCTOR =
33 const ElementKind('generative_constructor');
33 static final ElementKind FIELD = const ElementKind('field'); 34 static final ElementKind FIELD = const ElementKind('field');
34 static final ElementKind VARIABLE_LIST = const ElementKind('variable_list'); 35 static final ElementKind VARIABLE_LIST = const ElementKind('variable_list');
35 static final ElementKind FIELD_LIST = const ElementKind('field_list'); 36 static final ElementKind FIELD_LIST = const ElementKind('field_list');
36 static final ElementKind CONSTRUCTOR_BODY = 37 static final ElementKind GENERATIVE_CONSTRUCTOR_BODY =
37 const ElementKind('constructor_body'); 38 const ElementKind('generative_constructor_body');
38 39
39 toString() => id; 40 toString() => id;
40 } 41 }
41 42
42 class Element implements Hashable { 43 class Element implements Hashable {
43 final SourceString name; 44 final SourceString name;
44 final ElementKind kind; 45 final ElementKind kind;
45 final Element enclosingElement; 46 final Element enclosingElement;
46 abstract Node parseNode(Canceler canceler, Logger logger); 47 abstract Node parseNode(Canceler canceler, Logger logger);
47 abstract Type computeType(Compiler compiler, Types types); 48 abstract Type computeType(Compiler compiler, Types types);
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 : super(name, kind, enclosing); 162 : super(name, kind, enclosing);
162 FunctionElement.node(FunctionExpression node, 163 FunctionElement.node(FunctionExpression node,
163 ElementKind kind, 164 ElementKind kind,
164 Modifiers this.modifiers, 165 Modifiers this.modifiers,
165 Element enclosing) 166 Element enclosing)
166 : super(node.name.asIdentifier().source, kind, enclosing), 167 : super(node.name.asIdentifier().source, kind, enclosing),
167 this.node = node; 168 this.node = node;
168 169
169 bool isInstanceMember() { 170 bool isInstanceMember() {
170 return isMember() 171 return isMember()
171 && kind != ElementKind.CONSTRUCTOR 172 && kind != ElementKind.GENERATIVE_CONSTRUCTOR
173 && !modifiers.isFactory()
172 && !modifiers.isStatic(); 174 && !modifiers.isStatic();
173 } 175 }
174 176
175 FunctionType computeType(Compiler compiler, types) { 177 FunctionType computeType(Compiler compiler, types) {
176 if (type != null) return type; 178 if (type != null) return type;
177 if (parameters == null) compiler.resolveSignature(this); 179 if (parameters == null) compiler.resolveSignature(this);
178 FunctionExpression node = 180 FunctionExpression node =
179 compiler.parser.measure(() => parseNode(compiler, compiler)); 181 compiler.parser.measure(() => parseNode(compiler, compiler));
180 Type returnType = getType(node.returnType, compiler, types); 182 Type returnType = getType(node.returnType, compiler, types);
181 if (returnType === null) compiler.cancel('unknown type ${node.returnType}'); 183 if (returnType === null) returnType = types.dynamicType;
182 184
183 LinkBuilder<Type> parameterTypes = new LinkBuilder<Type>(); 185 LinkBuilder<Type> parameterTypes = new LinkBuilder<Type>();
184 for (Link<Element> link = parameters; !link.isEmpty(); link = link.tail) { 186 for (Link<Element> link = parameters; !link.isEmpty(); link = link.tail) {
185 parameterTypes.addLast(link.head.computeType(compiler, types)); 187 parameterTypes.addLast(link.head.computeType(compiler, types));
186 } 188 }
187 type = new FunctionType(returnType, parameterTypes.toLink(), this); 189 type = new FunctionType(returnType, parameterTypes.toLink(), this);
188 return type; 190 return type;
189 } 191 }
190 192
191 Node parseNode(Canceler canceler, Logger logger) => node; 193 Node parseNode(Canceler canceler, Logger logger) => node;
192 } 194 }
193 195
194 class ConstructorBodyElement extends FunctionElement { 196 class ConstructorBodyElement extends FunctionElement {
195 FunctionElement constructor; 197 FunctionElement constructor;
196 198
197 ConstructorBodyElement(FunctionElement constructor) 199 ConstructorBodyElement(FunctionElement constructor)
198 : this.constructor = constructor, 200 : this.constructor = constructor,
199 super(constructor.name, 201 super(constructor.name,
200 ElementKind.CONSTRUCTOR_BODY, 202 ElementKind.GENERATIVE_CONSTRUCTOR_BODY,
201 null, 203 null,
202 constructor.enclosingElement) { 204 constructor.enclosingElement) {
203 assert(constructor.node !== null); 205 assert(constructor.node !== null);
204 this.parameters = constructor.parameters; 206 this.parameters = constructor.parameters;
205 this.node = constructor.node; 207 this.node = constructor.node;
206 this.type = constructor.type; 208 this.type = constructor.type;
207 } 209 }
208 210
209 bool isInstanceMember() => true; 211 bool isInstanceMember() => true;
210 212
211 FunctionType computeType(Compiler compiler, types) { unreachable(); } 213 FunctionType computeType(Compiler compiler, types) { unreachable(); }
212 Node parseNode(Canceler canceler, Logger logger) { unreachable(); } 214 Node parseNode(Canceler canceler, Logger logger) { unreachable(); }
213 } 215 }
214 216
215 class SynthesizedConstructorElement extends FunctionElement { 217 class SynthesizedConstructorElement extends FunctionElement {
216 SynthesizedConstructorElement(Element enclosing) 218 SynthesizedConstructorElement(Element enclosing)
217 : super(enclosing.name, ElementKind.CONSTRUCTOR, null, enclosing) { 219 : super(enclosing.name, ElementKind.GENERATIVE_CONSTRUCTOR,
220 null, enclosing) {
218 parameters = const EmptyLink<Element>(); 221 parameters = const EmptyLink<Element>();
219 } 222 }
220 223
221 FunctionType computeType(Compiler compiler, types) { 224 FunctionType computeType(Compiler compiler, types) {
222 if (type != null) return type; 225 if (type != null) return type;
223 type = new FunctionType(types.voidType, const EmptyLink<Type>(), this); 226 type = new FunctionType(types.voidType, const EmptyLink<Type>(), this);
224 return type; 227 return type;
225 } 228 }
226 229
227 Node parseNode(Canceler canceler, Logger logger) { 230 Node parseNode(Canceler canceler, Logger logger) {
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
275 link = link.tail) { 278 link = link.tail) {
276 Element element = link.head; 279 Element element = link.head;
277 if (matches(element)) return element; 280 if (matches(element)) return element;
278 } 281 }
279 return null; 282 return null;
280 } 283 }
281 284
282 Element lookupLocalMember(SourceString name) { 285 Element lookupLocalMember(SourceString name) {
283 bool matches(Element element) { 286 bool matches(Element element) {
284 return element.name == name 287 return element.name == name
285 && element.kind != ElementKind.CONSTRUCTOR; 288 && element.kind != ElementKind.GENERATIVE_CONSTRUCTOR;
286 } 289 }
287 return lookupLocalElement(name, matches); 290 return lookupLocalElement(name, matches);
288 } 291 }
289 292
290 Element lookupConstructor(SourceString name) { 293 Element lookupConstructor(SourceString name) {
291 bool matches(Element element) { 294 bool matches(Element element) {
292 return element.name == name 295 return element.name == name
293 && element.kind == ElementKind.CONSTRUCTOR; 296 && (element.kind == ElementKind.GENERATIVE_CONSTRUCTOR
297 || element.modifiers.isFactory());
294 } 298 }
295 return lookupLocalElement(name, matches); 299 return lookupLocalElement(name, matches);
296 } 300 }
297 301
298 // TODO(ngeoffray): Implement these. 302 // TODO(ngeoffray): Implement these.
299 bool canHaveDefaultConstructor() => true; 303 bool canHaveDefaultConstructor() => true;
300 304
301 SynthesizedConstructorElement getSynthesizedConstructor() { 305 SynthesizedConstructorElement getSynthesizedConstructor() {
302 if (synthesizedConstructor === null && canHaveDefaultConstructor()) { 306 if (synthesizedConstructor === null && canHaveDefaultConstructor()) {
303 synthesizedConstructor = new SynthesizedConstructorElement(this); 307 synthesizedConstructor = new SynthesizedConstructorElement(this);
(...skipping 26 matching lines...) Expand all
330 && !element.isInstanceMember() 334 && !element.isInstanceMember()
331 && (element.kind === ElementKind.FIELD); 335 && (element.kind === ElementKind.FIELD);
332 } 336 }
333 337
334 static bool isInstanceMethod(Element element) { 338 static bool isInstanceMethod(Element element) {
335 return (element != null) 339 return (element != null)
336 && element.isInstanceMember() 340 && element.isInstanceMember()
337 && (element.kind === ElementKind.FUNCTION); 341 && (element.kind === ElementKind.FUNCTION);
338 } 342 }
339 } 343 }
OLDNEW
« no previous file with comments | « frog/leg/compiler.dart ('k') | frog/leg/emitter.dart » ('j') | frog/leg/lib/core.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698