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 12 matching lines...) Expand all Loading... | |
| 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 CONSTRUCTOR = const ElementKind('constructor'); |
| 33 static final ElementKind FIELD = const ElementKind('field'); | |
| 34 static final ElementKind VARIABLE_LIST = const ElementKind('variable_list'); | |
| 35 static final ElementKind FIELD_LIST = const ElementKind('field_list'); | |
| 33 static final ElementKind CONSTRUCTOR_BODY = | 36 static final ElementKind CONSTRUCTOR_BODY = |
| 34 const ElementKind('constructor_body'); | 37 const ElementKind('constructor_body'); |
| 35 | 38 |
| 36 toString() => id; | 39 toString() => id; |
| 37 } | 40 } |
| 38 | 41 |
| 39 class Element implements Hashable { | 42 class Element implements Hashable { |
| 40 final SourceString name; | 43 final SourceString name; |
| 41 final ElementKind kind; | 44 final ElementKind kind; |
| 42 final Element enclosingElement; | 45 final Element enclosingElement; |
| 43 abstract Node parseNode(Canceler canceler, Logger logger); | 46 abstract Node parseNode(Canceler canceler, Logger logger); |
| 44 abstract Type computeType(Compiler compiler, Types types); | 47 abstract Type computeType(Compiler compiler, Types types); |
| 45 bool isClassMember() => | 48 bool isClassMember() => |
| 46 enclosingElement !== null && enclosingElement.kind == ElementKind.CLASS; | 49 enclosingElement !== null && enclosingElement.kind == ElementKind.CLASS; |
| 47 // TODO(ngeoffray): override in function element to check for modifiers. | 50 // TODO(ngeoffray): override in function element to check for modifiers. |
| 48 bool isInstanceMember() => isClassMember(); | 51 bool isInstanceMember() => isClassMember(); |
| 49 | 52 |
| 50 const Element(this.name, this.kind, this.enclosingElement); | 53 const Element(this.name, this.kind, this.enclosingElement); |
| 51 | 54 |
| 52 // TODO(kasperl): This is a very bad hash code for the element and | 55 // TODO(kasperl): This is a very bad hash code for the element and |
| 53 // there's no reason why two elements with the same name should have | 56 // there's no reason why two elements with the same name should have |
| 54 // the same hash code. Replace this with a simple id in the element? | 57 // the same hash code. Replace this with a simple id in the element? |
| 55 int hashCode() => name.hashCode(); | 58 int hashCode() => name.hashCode(); |
| 56 | 59 |
| 57 toString() => '$name'; | 60 toString() => '$name'; |
| 58 } | 61 } |
| 59 | 62 |
| 60 class VariableElement extends Element { | 63 class VariableElement extends Element { |
| 61 final Node node; | 64 final VariableListElement variables; |
| 62 final TypeAnnotation typeAnnotation; | 65 |
| 66 VariableElement(SourceString name, VariableListElement this.variables, | |
| 67 ElementKind kind, [Element enclosing = null]) | |
| 68 : super(name, kind, enclosing); | |
| 69 | |
| 70 Node parseNode(Canceler canceler, Logger logger) { | |
| 71 return variables.parseNode(canceler, logger); | |
| 72 } | |
| 73 | |
| 74 Type computeType(Compiler compiler, types) { | |
| 75 return variables.computeType(compiler, types); | |
| 76 } | |
| 77 | |
| 78 Type get type() => variables.type; | |
| 79 } | |
| 80 | |
| 81 // This element represents a list of variable or field declaration. | |
| 82 // It contains the node, and the type. A [VariableElement] always | |
| 83 // references its [VariableListElement]. It forwards its | |
| 84 // [computeType] and [parseNode] methods to this element. | |
| 85 class VariableListElement extends Element { | |
| 86 VariableDefinitions node; | |
| 63 Type type; | 87 Type type; |
| 64 | 88 |
| 65 VariableElement(Node this.node, TypeAnnotation this.typeAnnotation, | 89 VariableListElement(ElementKind kind, [Element enclosing = null]) |
| 66 ElementKind kind, SourceString name, Element enclosingElement) | 90 : super(null, kind, enclosing); |
| 67 : super(name, kind, enclosingElement); | 91 |
| 92 VariableListElement.node(VariableDefinitions node, | |
| 93 ElementKind kind, | |
| 94 [Element enclosing = null]) | |
| 95 : super(null, kind, enclosing), | |
| 96 this.node = node; | |
| 68 | 97 |
| 69 Node parseNode(Canceler canceler, Logger logger) { | 98 Node parseNode(Canceler canceler, Logger logger) { |
|
karlklose
2011/12/12 17:02:09
Node -> VariableDefinitions.
ngeoffray
2011/12/12 17:18:04
Done.
| |
| 70 return node; | 99 return node; |
| 71 } | 100 } |
| 72 | 101 |
| 73 Type computeType(Compiler compiler, Types types) { | 102 Type computeType(Compiler compiler, types) { |
| 74 if (type !== null) return type; | 103 if (type != null) return type; |
| 75 type = getType(typeAnnotation, compiler, types); | 104 type = getType(parseNode(compiler, compiler).type, compiler, types); |
| 76 return type; | 105 return type; |
| 77 } | 106 } |
| 78 } | 107 } |
| 79 | 108 |
| 80 class ForeignElement extends Element { | 109 class ForeignElement extends Element { |
| 81 ForeignElement(SourceString name) : super(name, ElementKind.FOREIGN, null); | 110 ForeignElement(SourceString name) : super(name, ElementKind.FOREIGN, null); |
| 82 | 111 |
| 83 Type computeType(Compiler compiler, Types types) { | 112 Type computeType(Compiler compiler, types) { |
| 84 return types.dynamicType; | 113 return types.dynamicType; |
| 85 } | 114 } |
| 86 } | 115 } |
| 87 | 116 |
| 88 /** | 117 /** |
| 89 * TODO(ngeoffray): Remove this method in favor of using the universe. | 118 * TODO(ngeoffray): Remove this method in favor of using the universe. |
| 90 * | 119 * |
| 91 * Return the type referred to by the type annotation. This method | 120 * Return the type referred to by the type annotation. This method |
| 92 * accepts annotations with 'typeName == null' to indicate a missing | 121 * accepts annotations with 'typeName == null' to indicate a missing |
| 93 * annotation. | 122 * annotation. |
| (...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 188 | 217 |
| 189 class ClassElement extends Element { | 218 class ClassElement extends Element { |
| 190 Type type; | 219 Type type; |
| 191 Type supertype; | 220 Type supertype; |
| 192 Link<Element> members = const EmptyLink<Element>(); | 221 Link<Element> members = const EmptyLink<Element>(); |
| 193 Link<Type> interfaces = const EmptyLink<Type>(); | 222 Link<Type> interfaces = const EmptyLink<Type>(); |
| 194 bool isResolved = false; | 223 bool isResolved = false; |
| 195 ClassNode node; | 224 ClassNode node; |
| 196 // backendMembers are members that have been added by the backend to simplify | 225 // backendMembers are members that have been added by the backend to simplify |
| 197 // compilation. They don't have any user-side counter-part. | 226 // compilation. They don't have any user-side counter-part. |
| 198 Link<Element> backendMembers = const EmptyLink<Element>(); | 227 Link<Element> backendMembers = const EmptyLink<Element>(); |
| 199 SynthesizedConstructorElement synthesizedConstructor; | 228 SynthesizedConstructorElement synthesizedConstructor; |
| 200 | 229 |
| 201 ClassElement(SourceString name) : super(name, ElementKind.CLASS, null); | 230 ClassElement(SourceString name) : super(name, ElementKind.CLASS, null); |
| 202 | 231 |
| 203 void addMember(Element element) { | 232 void addMember(Element element) { |
| 204 members = members.prepend(element); | 233 members = members.prepend(element); |
| 205 } | 234 } |
| 206 | 235 |
| 207 Type computeType(compiler, types) { | 236 Type computeType(compiler, types) { |
| 208 if (type === null) { | 237 if (type === null) { |
| (...skipping 21 matching lines...) Expand all Loading... | |
| 230 // TODO(ngeoffray): Implement these. | 259 // TODO(ngeoffray): Implement these. |
| 231 bool canHaveDefaultConstructor() => true; | 260 bool canHaveDefaultConstructor() => true; |
| 232 | 261 |
| 233 SynthesizedConstructorElement getSynthesizedConstructor() { | 262 SynthesizedConstructorElement getSynthesizedConstructor() { |
| 234 if (synthesizedConstructor === null && canHaveDefaultConstructor()) { | 263 if (synthesizedConstructor === null && canHaveDefaultConstructor()) { |
| 235 synthesizedConstructor = new SynthesizedConstructorElement(this); | 264 synthesizedConstructor = new SynthesizedConstructorElement(this); |
| 236 } | 265 } |
| 237 return synthesizedConstructor; | 266 return synthesizedConstructor; |
| 238 } | 267 } |
| 239 } | 268 } |
| OLD | NEW |