| 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 11 matching lines...) Expand all Loading... |
| 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); |
| 48 bool isMember() => | 49 bool isMember() => |
| 49 enclosingElement !== null && enclosingElement.kind == ElementKind.CLASS; | 50 enclosingElement !== null && enclosingElement.kind == ElementKind.CLASS; |
| 50 bool isInstanceMember() => false; | 51 bool isInstanceMember() => false; |
| 52 bool isGenerativeConstructor() => kind == ElementKind.GENERATIVE_CONSTRUCTOR; |
| 51 | 53 |
| 52 const Element(this.name, this.kind, this.enclosingElement); | 54 const Element(this.name, this.kind, this.enclosingElement); |
| 53 | 55 |
| 54 // TODO(kasperl): This is a very bad hash code for the element and | 56 // TODO(kasperl): This is a very bad hash code for the element and |
| 55 // there's no reason why two elements with the same name should have | 57 // there's no reason why two elements with the same name should have |
| 56 // the same hash code. Replace this with a simple id in the element? | 58 // the same hash code. Replace this with a simple id in the element? |
| 57 int hashCode() => name.hashCode(); | 59 int hashCode() => name.hashCode(); |
| 58 | 60 |
| 59 toString() => '$kind($name)'; | 61 toString() => '$kind($name)'; |
| 60 } | 62 } |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 166 : super(name, kind, enclosing); | 168 : super(name, kind, enclosing); |
| 167 FunctionElement.node(FunctionExpression node, | 169 FunctionElement.node(FunctionExpression node, |
| 168 ElementKind kind, | 170 ElementKind kind, |
| 169 Modifiers this.modifiers, | 171 Modifiers this.modifiers, |
| 170 Element enclosing) | 172 Element enclosing) |
| 171 : super(node.name.asIdentifier().source, kind, enclosing), | 173 : super(node.name.asIdentifier().source, kind, enclosing), |
| 172 this.node = node; | 174 this.node = node; |
| 173 | 175 |
| 174 bool isInstanceMember() { | 176 bool isInstanceMember() { |
| 175 return isMember() | 177 return isMember() |
| 176 && kind != ElementKind.CONSTRUCTOR | 178 && kind != ElementKind.GENERATIVE_CONSTRUCTOR |
| 179 && !modifiers.isFactory() |
| 177 && !modifiers.isStatic(); | 180 && !modifiers.isStatic(); |
| 178 } | 181 } |
| 179 | 182 |
| 180 FunctionType computeType(Compiler compiler, types) { | 183 FunctionType computeType(Compiler compiler, types) { |
| 181 if (type != null) return type; | 184 if (type != null) return type; |
| 182 if (parameters == null) compiler.resolveSignature(this); | 185 if (parameters == null) compiler.resolveSignature(this); |
| 183 FunctionExpression node = | 186 FunctionExpression node = |
| 184 compiler.parser.measure(() => parseNode(compiler, compiler)); | 187 compiler.parser.measure(() => parseNode(compiler, compiler)); |
| 185 Type returnType = getType(node.returnType, compiler, types); | 188 Type returnType = getType(node.returnType, compiler, types); |
| 186 if (returnType === null) compiler.cancel('unknown type ${node.returnType}'); | 189 if (returnType === null) returnType = types.dynamicType; |
| 187 | 190 |
| 188 LinkBuilder<Type> parameterTypes = new LinkBuilder<Type>(); | 191 LinkBuilder<Type> parameterTypes = new LinkBuilder<Type>(); |
| 189 for (Link<Element> link = parameters; !link.isEmpty(); link = link.tail) { | 192 for (Link<Element> link = parameters; !link.isEmpty(); link = link.tail) { |
| 190 parameterTypes.addLast(link.head.computeType(compiler, types)); | 193 parameterTypes.addLast(link.head.computeType(compiler, types)); |
| 191 } | 194 } |
| 192 type = new FunctionType(returnType, parameterTypes.toLink(), this); | 195 type = new FunctionType(returnType, parameterTypes.toLink(), this); |
| 193 return type; | 196 return type; |
| 194 } | 197 } |
| 195 | 198 |
| 196 Node parseNode(Canceler canceler, Logger logger) => node; | 199 Node parseNode(Canceler canceler, Logger logger) => node; |
| 197 } | 200 } |
| 198 | 201 |
| 199 class ConstructorBodyElement extends FunctionElement { | 202 class ConstructorBodyElement extends FunctionElement { |
| 200 FunctionElement constructor; | 203 FunctionElement constructor; |
| 201 | 204 |
| 202 ConstructorBodyElement(FunctionElement constructor) | 205 ConstructorBodyElement(FunctionElement constructor) |
| 203 : this.constructor = constructor, | 206 : this.constructor = constructor, |
| 204 super(constructor.name, | 207 super(constructor.name, |
| 205 ElementKind.CONSTRUCTOR_BODY, | 208 ElementKind.GENERATIVE_CONSTRUCTOR_BODY, |
| 206 null, | 209 null, |
| 207 constructor.enclosingElement) { | 210 constructor.enclosingElement) { |
| 208 assert(constructor.node !== null); | 211 assert(constructor.node !== null); |
| 209 this.parameters = constructor.parameters; | 212 this.parameters = constructor.parameters; |
| 210 this.node = constructor.node; | 213 this.node = constructor.node; |
| 211 this.type = constructor.type; | 214 this.type = constructor.type; |
| 212 } | 215 } |
| 213 | 216 |
| 214 bool isInstanceMember() => true; | 217 bool isInstanceMember() => true; |
| 215 | 218 |
| 216 FunctionType computeType(Compiler compiler, types) { unreachable(); } | 219 FunctionType computeType(Compiler compiler, types) { unreachable(); } |
| 217 Node parseNode(Canceler canceler, Logger logger) { unreachable(); } | 220 Node parseNode(Canceler canceler, Logger logger) { unreachable(); } |
| 218 } | 221 } |
| 219 | 222 |
| 220 class SynthesizedConstructorElement extends FunctionElement { | 223 class SynthesizedConstructorElement extends FunctionElement { |
| 221 SynthesizedConstructorElement(Element enclosing) | 224 SynthesizedConstructorElement(Element enclosing) |
| 222 : super(enclosing.name, ElementKind.CONSTRUCTOR, null, enclosing) { | 225 : super(enclosing.name, ElementKind.GENERATIVE_CONSTRUCTOR, |
| 226 null, enclosing) { |
| 223 parameters = const EmptyLink<Element>(); | 227 parameters = const EmptyLink<Element>(); |
| 224 } | 228 } |
| 225 | 229 |
| 226 FunctionType computeType(Compiler compiler, types) { | 230 FunctionType computeType(Compiler compiler, types) { |
| 227 if (type != null) return type; | 231 if (type != null) return type; |
| 228 type = new FunctionType(types.voidType, const EmptyLink<Type>(), this); | 232 type = new FunctionType(types.voidType, const EmptyLink<Type>(), this); |
| 229 return type; | 233 return type; |
| 230 } | 234 } |
| 231 | 235 |
| 232 Node parseNode(Canceler canceler, Logger logger) { | 236 Node parseNode(Canceler canceler, Logger logger) { |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 280 link = link.tail) { | 284 link = link.tail) { |
| 281 Element element = link.head; | 285 Element element = link.head; |
| 282 if (matches(element)) return element; | 286 if (matches(element)) return element; |
| 283 } | 287 } |
| 284 return null; | 288 return null; |
| 285 } | 289 } |
| 286 | 290 |
| 287 Element lookupLocalMember(SourceString name) { | 291 Element lookupLocalMember(SourceString name) { |
| 288 bool matches(Element element) { | 292 bool matches(Element element) { |
| 289 return element.name == name | 293 return element.name == name |
| 290 && element.kind != ElementKind.CONSTRUCTOR; | 294 && element.kind != ElementKind.GENERATIVE_CONSTRUCTOR; |
| 291 } | 295 } |
| 292 return lookupLocalElement(name, matches); | 296 return lookupLocalElement(name, matches); |
| 293 } | 297 } |
| 294 | 298 |
| 295 Element lookupConstructor(SourceString name) { | 299 Element lookupConstructor(SourceString name) { |
| 296 bool matches(Element element) { | 300 bool matches(FunctionElement element) { |
| 297 return element.name == name | 301 return element.name == name |
| 298 && element.kind == ElementKind.CONSTRUCTOR; | 302 && (element.kind == ElementKind.GENERATIVE_CONSTRUCTOR |
| 303 || element.modifiers.isFactory()); |
| 299 } | 304 } |
| 300 return lookupLocalElement(name, matches); | 305 return lookupLocalElement(name, matches); |
| 301 } | 306 } |
| 302 | 307 |
| 303 // TODO(ngeoffray): Implement these. | 308 // TODO(ngeoffray): Implement these. |
| 304 bool canHaveDefaultConstructor() => true; | 309 bool canHaveDefaultConstructor() => true; |
| 305 | 310 |
| 306 SynthesizedConstructorElement getSynthesizedConstructor() { | 311 SynthesizedConstructorElement getSynthesizedConstructor() { |
| 307 if (synthesizedConstructor === null && canHaveDefaultConstructor()) { | 312 if (synthesizedConstructor === null && canHaveDefaultConstructor()) { |
| 308 synthesizedConstructor = new SynthesizedConstructorElement(this); | 313 synthesizedConstructor = new SynthesizedConstructorElement(this); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 335 && !element.isInstanceMember() | 340 && !element.isInstanceMember() |
| 336 && (element.kind === ElementKind.FIELD); | 341 && (element.kind === ElementKind.FIELD); |
| 337 } | 342 } |
| 338 | 343 |
| 339 static bool isInstanceMethod(Element element) { | 344 static bool isInstanceMethod(Element element) { |
| 340 return (element != null) | 345 return (element != null) |
| 341 && element.isInstanceMember() | 346 && element.isInstanceMember() |
| 342 && (element.kind === ElementKind.FUNCTION); | 347 && (element.kind === ElementKind.FUNCTION); |
| 343 } | 348 } |
| 344 } | 349 } |
| OLD | NEW |