| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 413 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 424 return super.getCompilationUnit(); | 424 return super.getCompilationUnit(); |
| 425 } | 425 } |
| 426 | 426 |
| 427 lookupLocalMember(SourceString memberName) => imported[memberName]; | 427 lookupLocalMember(SourceString memberName) => imported[memberName]; |
| 428 | 428 |
| 429 Type computeType(Compiler compiler) => compiler.types.dynamicType; | 429 Type computeType(Compiler compiler) => compiler.types.dynamicType; |
| 430 | 430 |
| 431 Token position() => firstPosition; | 431 Token position() => firstPosition; |
| 432 } | 432 } |
| 433 | 433 |
| 434 class TypedefElement extends Element { | 434 class TypedefElement extends Element implements TypeDeclarationElement { |
| 435 Type cachedType; | 435 Type cachedType; |
| 436 Typedef cachedNode; | 436 Typedef cachedNode; |
| 437 | 437 |
| 438 TypedefElement(SourceString name, Element enclosing) | 438 TypedefElement(SourceString name, Element enclosing) |
| 439 : super(name, ElementKind.TYPEDEF, enclosing); | 439 : super(name, ElementKind.TYPEDEF, enclosing); |
| 440 | 440 |
| 441 Type computeType(Compiler compiler) { | 441 Type computeType(Compiler compiler) { |
| 442 if (cachedType !== null) return cachedType; | 442 if (cachedType !== null) return cachedType; |
| 443 cachedType = compiler.computeFunctionType( | 443 cachedType = compiler.computeFunctionType( |
| 444 this, compiler.resolveTypedef(this)); | 444 this, compiler.resolveTypedef(this)); |
| 445 return cachedType; | 445 return cachedType; |
| 446 } | 446 } |
| 447 |
| 448 Link<Type> get typeVariables() => const EmptyLink<Type>(); |
| 449 |
| 450 Scope buildScope() => |
| 451 new TypeDeclarationScope(enclosingElement.buildScope(), this); |
| 447 } | 452 } |
| 448 | 453 |
| 449 class VariableElement extends Element { | 454 class VariableElement extends Element { |
| 450 final VariableListElement variables; | 455 final VariableListElement variables; |
| 451 Expression cachedNode; // The send or the identifier in the variables list. | 456 Expression cachedNode; // The send or the identifier in the variables list. |
| 452 | 457 |
| 453 Modifiers get modifiers() => variables.modifiers; | 458 Modifiers get modifiers() => variables.modifiers; |
| 454 | 459 |
| 455 VariableElement(SourceString name, | 460 VariableElement(SourceString name, |
| 456 VariableListElement this.variables, | 461 VariableListElement this.variables, |
| (...skipping 362 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 819 class VoidElement extends Element { | 824 class VoidElement extends Element { |
| 820 VoidElement(Element enclosing) | 825 VoidElement(Element enclosing) |
| 821 : super(const SourceString('void'), ElementKind.VOID, enclosing); | 826 : super(const SourceString('void'), ElementKind.VOID, enclosing); |
| 822 Type computeType(compiler) => compiler.types.voidType; | 827 Type computeType(compiler) => compiler.types.voidType; |
| 823 Node parseNode(_) { | 828 Node parseNode(_) { |
| 824 throw 'internal error: parseNode on void'; | 829 throw 'internal error: parseNode on void'; |
| 825 } | 830 } |
| 826 bool impliesType() => true; | 831 bool impliesType() => true; |
| 827 } | 832 } |
| 828 | 833 |
| 829 class ClassElement extends ContainerElement { | 834 /** |
| 835 * [TypeDeclarationElement] defines the common interface for class/interface |
| 836 * declarations and typedefs. |
| 837 */ |
| 838 abstract class TypeDeclarationElement implements Element { |
| 839 // TODO(johnniwinther): This class should eventually be a mixin. |
| 840 |
| 841 /** |
| 842 * The type variables declared on this declaration. The type variables are not |
| 843 * available until the type of the element has been computed through |
| 844 * [computeType]. |
| 845 */ |
| 846 // TODO(johnniwinther): Find a (better) way to decouple [typeVariables] from |
| 847 // [Compiler]. |
| 848 final Link<Type> typeVariables; |
| 849 |
| 850 /** |
| 851 * Creates the type variables, their type and corresponding element, for the |
| 852 * type variables declared in [parameter] on [element]. The bounds of the type |
| 853 * variables are not set until [element] has been resolved. |
| 854 */ |
| 855 static Link<Type> createTypeVariables(TypeDeclarationElement element, |
| 856 NodeList parameters) { |
| 857 if (parameters === null) return const EmptyLink<Type>(); |
| 858 |
| 859 // Create types and elements for type variable. |
| 860 var arguments = new LinkBuilder<Type>(); |
| 861 for (Link<Node> link = parameters.nodes; !link.isEmpty(); link = link.tail)
{ |
| 862 TypeVariable node = link.head; |
| 863 SourceString variableName = node.name.source; |
| 864 TypeVariableElement variableElement = |
| 865 new TypeVariableElement(variableName, element, node); |
| 866 TypeVariableType variableType = new TypeVariableType(variableElement); |
| 867 variableElement.type = variableType; |
| 868 arguments.addLast(variableType); |
| 869 } |
| 870 return arguments.toLink(); |
| 871 } |
| 872 } |
| 873 |
| 874 class ClassElement extends ContainerElement |
| 875 implements TypeDeclarationElement { |
| 830 final int id; | 876 final int id; |
| 831 Type type; | 877 InterfaceType type; |
| 832 Type supertype; | 878 Type supertype; |
| 833 Type defaultClass; | 879 Type defaultClass; |
| 834 Link<Element> members = const EmptyLink<Element>(); | 880 Link<Element> members = const EmptyLink<Element>(); |
| 835 Map<SourceString, Element> localMembers; | 881 Map<SourceString, Element> localMembers; |
| 836 Map<SourceString, Element> constructors; | 882 Map<SourceString, Element> constructors; |
| 837 Link<Type> interfaces = const EmptyLink<Type>(); | 883 Link<Type> interfaces = const EmptyLink<Type>(); |
| 838 LinkedHashMap<SourceString, TypeVariableElement> typeParameters; | |
| 839 bool isResolved = false; | 884 bool isResolved = false; |
| 840 bool isBeingResolved = false; | 885 bool isBeingResolved = false; |
| 841 // backendMembers are members that have been added by the backend to simplify | 886 // backendMembers are members that have been added by the backend to simplify |
| 842 // compilation. They don't have any user-side counter-part. | 887 // compilation. They don't have any user-side counter-part. |
| 843 Link<Element> backendMembers = const EmptyLink<Element>(); | 888 Link<Element> backendMembers = const EmptyLink<Element>(); |
| 844 | 889 |
| 845 Link<Type> allSupertypes; | 890 Link<Type> allSupertypes; |
| 846 ClassElement patch = null; | 891 ClassElement patch = null; |
| 847 | 892 |
| 848 ClassElement(SourceString name, CompilationUnitElement enclosing, this.id) | 893 ClassElement(SourceString name, CompilationUnitElement enclosing, this.id) |
| 849 : localMembers = new Map<SourceString, Element>(), | 894 : localMembers = new Map<SourceString, Element>(), |
| 850 constructors = new Map<SourceString, Element>(), | 895 constructors = new Map<SourceString, Element>(), |
| 851 typeParameters = new LinkedHashMap<SourceString, TypeVariableElement>(), | |
| 852 super(name, ElementKind.CLASS, enclosing); | 896 super(name, ElementKind.CLASS, enclosing); |
| 853 | 897 |
| 854 void addMember(Element element, DiagnosticListener listener) { | 898 void addMember(Element element, DiagnosticListener listener) { |
| 855 members = members.prepend(element); | 899 members = members.prepend(element); |
| 856 if (element.kind == ElementKind.GENERATIVE_CONSTRUCTOR || | 900 if (element.kind == ElementKind.GENERATIVE_CONSTRUCTOR || |
| 857 element.modifiers.isFactory()) { | 901 element.modifiers.isFactory()) { |
| 858 constructors[element.name] = element; | 902 constructors[element.name] = element; |
| 859 } else if (element.kind == ElementKind.GETTER | 903 } else if (element.kind == ElementKind.GETTER |
| 860 || element.kind == ElementKind.SETTER) { | 904 || element.kind == ElementKind.SETTER) { |
| 861 addGetterOrSetter(element, localMembers[element.name], listener); | 905 addGetterOrSetter(element, localMembers[element.name], listener); |
| 862 } else { | 906 } else { |
| 863 localMembers[element.name] = element; | 907 localMembers[element.name] = element; |
| 864 } | 908 } |
| 865 } | 909 } |
| 866 | 910 |
| 867 Type computeType(compiler) { | 911 InterfaceType computeType(compiler) { |
| 868 if (type === null) { | 912 if (type == null) { |
| 869 type = new InterfaceType(this); | 913 ClassNode node = parseNode(compiler); |
| 914 Link<Type> parameters = |
| 915 TypeDeclarationElement.createTypeVariables(this, node.typeParameters); |
| 916 type = new InterfaceType(this, parameters); |
| 870 } | 917 } |
| 871 return type; | 918 return type; |
| 872 } | 919 } |
| 873 | 920 |
| 921 Link<Type> get typeVariables() => type.arguments; |
| 922 |
| 874 ClassElement ensureResolved(Compiler compiler) { | 923 ClassElement ensureResolved(Compiler compiler) { |
| 875 compiler.resolveClass(this); | 924 compiler.resolveClass(this); |
| 876 return this; | 925 return this; |
| 877 } | 926 } |
| 878 | 927 |
| 879 Element lookupTypeParameter(SourceString parameterName) { | |
| 880 Element result = typeParameters[parameterName]; | |
| 881 return result; | |
| 882 } | |
| 883 | |
| 884 Element lookupLocalMember(SourceString memberName) { | 928 Element lookupLocalMember(SourceString memberName) { |
| 885 return localMembers[memberName]; | 929 return localMembers[memberName]; |
| 886 } | 930 } |
| 887 | 931 |
| 888 Element lookupSuperMember(SourceString memberName) { | 932 Element lookupSuperMember(SourceString memberName) { |
| 889 for (ClassElement s = superclass; s != null; s = s.superclass) { | 933 for (ClassElement s = superclass; s != null; s = s.superclass) { |
| 890 Element e = s.lookupLocalMember(memberName); | 934 Element e = s.lookupLocalMember(memberName); |
| 891 if (e === null) continue; | 935 if (e === null) continue; |
| 892 // Private members from a different library are not visible. | 936 // Private members from a different library are not visible. |
| 893 if (memberName.isPrivate() && getLibrary() !== e.getLibrary()) continue; | 937 if (memberName.isPrivate() && getLibrary() !== e.getLibrary()) continue; |
| (...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1190 | 1234 |
| 1191 Node parseNode(DiagnosticListener l) => statement; | 1235 Node parseNode(DiagnosticListener l) => statement; |
| 1192 | 1236 |
| 1193 bool get isSwitch() => statement is SwitchStatement; | 1237 bool get isSwitch() => statement is SwitchStatement; |
| 1194 | 1238 |
| 1195 Token position() => statement.getBeginToken(); | 1239 Token position() => statement.getBeginToken(); |
| 1196 String toString() => statement.toString(); | 1240 String toString() => statement.toString(); |
| 1197 } | 1241 } |
| 1198 | 1242 |
| 1199 class TypeVariableElement extends Element { | 1243 class TypeVariableElement extends Element { |
| 1200 final Node node; | 1244 final Node cachedNode; |
| 1245 TypeVariableType type; |
| 1201 Type bound; | 1246 Type bound; |
| 1202 Type type; | 1247 |
| 1203 TypeVariableElement(name, Element enclosing, this.node, this.type, | 1248 TypeVariableElement(name, Element enclosing, this.cachedNode, |
| 1204 [this.bound]) | 1249 [this.type, this.bound]) |
| 1205 : super(name, ElementKind.TYPE_VARIABLE, enclosing); | 1250 : super(name, ElementKind.TYPE_VARIABLE, enclosing); |
| 1206 Type computeType(compiler) => type; | 1251 |
| 1207 Node parseNode(compiler) => node; | 1252 TypeVariableType computeType(compiler) => type; |
| 1208 toString() => "${enclosingElement.toString()}.${name.slowToString()}"; | 1253 |
| 1254 Node parseNode(compiler) => cachedNode; |
| 1255 |
| 1256 String toString() => "${enclosingElement.toString()}.${name.slowToString()}"; |
| 1209 } | 1257 } |
| OLD | NEW |