Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 209 FunctionExpression cachedNode; | 209 FunctionExpression cachedNode; |
| 210 Type type; | 210 Type type; |
| 211 final Modifiers modifiers; | 211 final Modifiers modifiers; |
| 212 | 212 |
| 213 FunctionElement(SourceString name, | 213 FunctionElement(SourceString name, |
| 214 ElementKind kind, | 214 ElementKind kind, |
| 215 Modifiers this.modifiers, | 215 Modifiers this.modifiers, |
| 216 Element enclosing, | 216 Element enclosing, |
| 217 [Node node]) | 217 [Node node]) |
| 218 : super(name, kind, enclosing), cachedNode = node; | 218 : super(name, kind, enclosing), cachedNode = node; |
| 219 FunctionElement.node(FunctionExpression node, | 219 FunctionElement.node(SourceString name, |
| 220 FunctionExpression node, | |
| 220 ElementKind kind, | 221 ElementKind kind, |
| 221 Modifiers this.modifiers, | 222 Modifiers this.modifiers, |
| 222 Element enclosing) | 223 Element enclosing) |
| 223 : super(node.name.asIdentifier().source, kind, enclosing), | 224 : super(name, kind, enclosing), |
| 224 this.cachedNode = node; | 225 this.cachedNode = node; |
| 225 | 226 |
| 226 bool isInstanceMember() { | 227 bool isInstanceMember() { |
| 227 return isMember() | 228 return isMember() |
| 228 && kind != ElementKind.GENERATIVE_CONSTRUCTOR | 229 && kind != ElementKind.GENERATIVE_CONSTRUCTOR |
| 229 && !modifiers.isFactory() | 230 && !modifiers.isFactory() |
| 230 && !modifiers.isStatic(); | 231 && !modifiers.isStatic(); |
| 231 } | 232 } |
| 232 | 233 |
| 233 FunctionType computeType(Compiler compiler) { | 234 FunctionType computeType(Compiler compiler) { |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 296 new Block(new NodeList.empty()), | 297 new Block(new NodeList.empty()), |
| 297 null, null, null); | 298 null, null, null); |
| 298 return cachedNode; | 299 return cachedNode; |
| 299 } | 300 } |
| 300 } | 301 } |
| 301 | 302 |
| 302 class ClassElement extends Element { | 303 class ClassElement extends Element { |
| 303 Type type; | 304 Type type; |
| 304 Type supertype; | 305 Type supertype; |
| 305 Link<Element> members = const EmptyLink<Element>(); | 306 Link<Element> members = const EmptyLink<Element>(); |
| 307 Map<SourceString, Element> localMembers; | |
| 308 Map<SourceString, Element> constructors; | |
| 306 Link<Type> interfaces = const EmptyLink<Type>(); | 309 Link<Type> interfaces = const EmptyLink<Type>(); |
| 307 bool isResolved = false; | 310 bool isResolved = false; |
| 308 // backendMembers are members that have been added by the backend to simplify | 311 // backendMembers are members that have been added by the backend to simplify |
| 309 // compilation. They don't have any user-side counter-part. | 312 // compilation. They don't have any user-side counter-part. |
| 310 Link<Element> backendMembers = const EmptyLink<Element>(); | 313 Link<Element> backendMembers = const EmptyLink<Element>(); |
| 311 SynthesizedConstructorElement synthesizedConstructor; | 314 SynthesizedConstructorElement synthesizedConstructor; |
| 312 | 315 |
| 313 ClassElement(SourceString name, CompilationUnitElement enclosing) | 316 ClassElement(SourceString name, CompilationUnitElement enclosing) |
| 314 : super(name, ElementKind.CLASS, enclosing); | 317 : localMembers = new Map<SourceString, Element>(), |
| 318 constructors = new Map<SourceString, Element>(), | |
| 319 super(name, ElementKind.CLASS, enclosing); | |
| 315 | 320 |
| 316 void addMember(Element element) { | 321 void addMember(Element element) { |
| 317 members = members.prepend(element); | 322 members = members.prepend(element); |
| 323 if (element.kind == ElementKind.GENERATIVE_CONSTRUCTOR || | |
| 324 element.modifiers.isFactory()) { | |
| 325 constructors[element.name] = element; | |
| 326 } else { | |
| 327 localMembers[element.name] = element; | |
| 328 } | |
| 318 } | 329 } |
| 319 | 330 |
| 320 Type computeType(compiler) { | 331 Type computeType(compiler) { |
| 321 if (type === null) { | 332 if (type === null) { |
| 322 type = new SimpleType(name, this); | 333 type = new SimpleType(name, this); |
| 323 } | 334 } |
| 324 return type; | 335 return type; |
| 325 } | 336 } |
| 326 | 337 |
| 327 ClassElement resolve(Compiler compiler) { | 338 ClassElement resolve(Compiler compiler) { |
| 328 if (!isResolved) { | 339 if (!isResolved) { |
| 329 compiler.resolveType(this); | 340 compiler.resolveType(this); |
| 330 isResolved = true; | 341 isResolved = true; |
| 331 } | 342 } |
| 332 return this; | 343 return this; |
| 333 } | 344 } |
| 334 | 345 |
| 335 Element lookupLocalElement(SourceString name, bool matches(Element)) { | 346 Element lookupLocalMember(SourceString name) { |
| 336 // TODO(karlklose): replace with more efficient solution. | 347 return localMembers[name]; |
| 337 for (Link<Element> link = members; | |
| 338 link !== null && !link.isEmpty(); | |
| 339 link = link.tail) { | |
| 340 Element element = link.head; | |
| 341 if (matches(element)) return element; | |
| 342 } | |
| 343 return null; | |
| 344 } | 348 } |
| 345 | 349 |
| 346 Element lookupLocalMember(SourceString name) { | 350 Element lookupConstructor(SourceString name, [Element noMatch(Element)]) { |
| 347 bool matches(Element element) { | 351 Element result = constructors[name]; |
| 348 return element.name == name | 352 if (result === null && noMatch !== null) { |
| 349 && element.kind != ElementKind.GENERATIVE_CONSTRUCTOR; | 353 result = noMatch(lookupLocalMember(name)); |
| 350 } | 354 } |
| 351 return lookupLocalElement(name, matches); | 355 return result; |
| 352 } | |
| 353 | |
| 354 Element lookupConstructor(SourceString name) { | |
| 355 bool matches(Element element) { | |
| 356 return element.name == name | |
| 357 && (element.kind == ElementKind.GENERATIVE_CONSTRUCTOR | |
| 358 || element.modifiers.isFactory()); | |
| 359 } | |
| 360 return lookupLocalElement(name, matches); | |
| 361 } | 356 } |
| 362 | 357 |
| 363 // TODO(ngeoffray): Implement these. | 358 // TODO(ngeoffray): Implement these. |
|
ngeoffray
2012/01/19 08:56:12
Remove the TODO?
karlklose
2012/01/19 13:51:24
Done.
| |
| 364 bool canHaveDefaultConstructor() => true; | 359 bool canHaveDefaultConstructor() => constructors.length == 0; |
| 365 | 360 |
| 366 SynthesizedConstructorElement getSynthesizedConstructor() { | 361 SynthesizedConstructorElement getSynthesizedConstructor() { |
| 367 if (synthesizedConstructor === null && canHaveDefaultConstructor()) { | 362 if (synthesizedConstructor === null && canHaveDefaultConstructor()) { |
| 368 synthesizedConstructor = new SynthesizedConstructorElement(this); | 363 synthesizedConstructor = new SynthesizedConstructorElement(this); |
| 369 } | 364 } |
| 370 return synthesizedConstructor; | 365 return synthesizedConstructor; |
| 371 } | 366 } |
| 372 | 367 |
| 373 /** | 368 /** |
| 374 * Returns the super class, if any. | 369 * Returns the super class, if any. |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 404 if (send.isPropertyAccess) return false; | 399 if (send.isPropertyAccess) return false; |
| 405 if (send.receiver !== null) return false; | 400 if (send.receiver !== null) return false; |
| 406 Element element = elements[send]; | 401 Element element = elements[send]; |
| 407 // (o)() or foo()(). | 402 // (o)() or foo()(). |
| 408 if (element === null && send.selector.asIdentifier() === null) return true; | 403 if (element === null && send.selector.asIdentifier() === null) return true; |
| 409 if (element === null) return false; | 404 if (element === null) return false; |
| 410 // foo() with foo a local or a parameter. | 405 // foo() with foo a local or a parameter. |
| 411 return element.isVariable() || element.isParameter(); | 406 return element.isVariable() || element.isParameter(); |
| 412 } | 407 } |
| 413 } | 408 } |
| OLD | NEW |