Chromium Code Reviews| Index: compiler/java/com/google/dart/compiler/resolver/Elements.java |
| diff --git a/compiler/java/com/google/dart/compiler/resolver/Elements.java b/compiler/java/com/google/dart/compiler/resolver/Elements.java |
| index ea345029b2f3abf274287740c21a3646031c9675..5a432f9e82d528341914a7a803788629bed8c863 100644 |
| --- a/compiler/java/com/google/dart/compiler/resolver/Elements.java |
| +++ b/compiler/java/com/google/dart/compiler/resolver/Elements.java |
| @@ -12,10 +12,7 @@ import com.google.dart.compiler.LibrarySource; |
| import com.google.dart.compiler.Source; |
| import com.google.dart.compiler.ast.DartClass; |
| import com.google.dart.compiler.ast.DartClassMember; |
| -import com.google.dart.compiler.ast.DartDeclaration; |
| -import com.google.dart.compiler.ast.DartExpression; |
| import com.google.dart.compiler.ast.DartField; |
| -import com.google.dart.compiler.ast.DartFunction; |
| import com.google.dart.compiler.ast.DartFunctionExpression; |
| import com.google.dart.compiler.ast.DartFunctionTypeAlias; |
| import com.google.dart.compiler.ast.DartIdentifier; |
| @@ -24,12 +21,9 @@ import com.google.dart.compiler.ast.DartMethodDefinition; |
| import com.google.dart.compiler.ast.DartNativeBlock; |
| import com.google.dart.compiler.ast.DartNode; |
| import com.google.dart.compiler.ast.DartParameter; |
| -import com.google.dart.compiler.ast.DartParameterizedTypeNode; |
| -import com.google.dart.compiler.ast.DartPropertyAccess; |
| import com.google.dart.compiler.ast.DartSuperExpression; |
| import com.google.dart.compiler.ast.DartTypeNode; |
| import com.google.dart.compiler.ast.DartTypeParameter; |
| -import com.google.dart.compiler.ast.DartUnit; |
| import com.google.dart.compiler.ast.DartVariable; |
| import com.google.dart.compiler.ast.LibraryUnit; |
| import com.google.dart.compiler.ast.Modifiers; |
| @@ -87,26 +81,36 @@ public class Elements { |
| return new MethodElementImplementation(node, name, Modifiers.NONE); |
| } |
| - public static TypeVariableElement typeVariableElement(DartNode node, String name, Element owner) { |
| - return new TypeVariableElementImplementation(node, name, owner); |
| + public static TypeVariableElement typeVariableElement(String name, Type bound) { |
| + return new TypeVariableElementImplementation(name, bound); |
| } |
| - public static VariableElement variableElement(DartVariable node, String name, |
| - Modifiers modifiers) { |
| - return new VariableElementImplementation(node, name, ElementKind.VARIABLE, modifiers, false, |
| - null); |
| + public static VariableElement variableElement(Element owner, |
| + DartVariable node, |
| + String name, |
| + Modifiers modifiers) { |
| + return new VariableElementImplementation(owner, |
| + node, |
| + node.getName().getSourceInfo(), |
| + name, |
| + ElementKind.VARIABLE, |
| + modifiers, |
| + false, |
| + null); |
| } |
| - public static VariableElement parameterElement(DartParameter node, String name, |
| - Modifiers modifiers) { |
| - return new VariableElementImplementation(node, name, ElementKind.PARAMETER, modifiers, |
| - node.getModifiers().isNamed(), |
| - node.getDefaultExpr()); |
| - } |
| - |
| - public static VariableElement makeVariable(String name) { |
| - return new VariableElementImplementation(null, name, |
| - ElementKind.VARIABLE, Modifiers.NONE, false, null); |
| + public static VariableElement parameterElement(Element owner, |
| + DartParameter node, |
| + String name, |
| + Modifiers modifiers) { |
| + return new VariableElementImplementation(owner, |
| + node, |
| + node.getName().getSourceInfo(), |
| + name, |
| + ElementKind.PARAMETER, |
| + modifiers, |
| + node.getModifiers().isNamed(), |
| + node.getDefaultExpr()); |
| } |
| public static SuperElement superElement(DartSuperExpression node, ClassElement cls) { |
| @@ -213,11 +217,6 @@ static FieldElementImplementation fieldFromNode(DartField node, |
| return ((ClassElementImplementation) cls).lookupLocalField(name); |
| } |
| - static ConstructorElement constructorNamed(String name, ClassElement declaringClass, |
| - ClassElement constructorType) { |
| - return ConstructorElementImplementation.named(name, declaringClass, constructorType); |
| - } |
| - |
| public static FunctionAliasElement functionTypeAliasFromNode(DartFunctionTypeAlias node, |
| LibraryElement library) { |
| return FunctionAliasElementImplementation.fromNode(node, library); |
| @@ -228,33 +227,9 @@ static FieldElementImplementation fieldFromNode(DartField node, |
| * parameter in {@link DartMethodDefinition}. |
| */ |
| public static boolean isConstructorParameter(Element element) { |
| - if (element instanceof VariableElement) { |
| - DartNode parentNode = element.getNode().getParent(); |
| - if (parentNode instanceof DartFunction |
| - && parentNode.getParent() instanceof DartMethodDefinition) { |
| - DartMethodDefinition parentMethod = (DartMethodDefinition) parentNode.getParent(); |
| - if (parentMethod.getElement().isConstructor()) { |
| - return true; |
| - } |
| - } |
| - } |
| - return false; |
| - } |
| - |
| - /** |
| - * @return <code>true</code> if given {@link Element} represents {@link VariableElement} for |
| - * parameter in identically named setter {@link DartMethodDefinition}. |
| - */ |
| - public static boolean isParameterOfSameNameSetter(Element element) { |
| - if (element instanceof VariableElement) { |
| - DartNode parentNode = element.getNode().getParent(); |
| - if (parentNode instanceof DartFunction |
| - && parentNode.getParent() instanceof DartMethodDefinition) { |
| - DartMethodDefinition parentMethod = (DartMethodDefinition) parentNode.getParent(); |
| - if (parentMethod.getElement().getName().equals(element.getName())) { |
| - return true; |
| - } |
| - } |
| + Element parent = element.getEnclosingElement(); |
| + if (parent instanceof MethodElement) { |
| + return ((MethodElement) parent).isConstructor(); |
| } |
| return false; |
| } |
| @@ -265,13 +240,11 @@ static FieldElementImplementation fieldFromNode(DartField node, |
| * as body. |
| */ |
| public static boolean isParameterOfMethodWithoutBody(Element element) { |
| - if (element instanceof VariableElementImplementation) { |
| - DartNode parentNode = element.getNode().getParent(); |
| - if (parentNode instanceof DartFunction) { |
| - DartFunction parentFunction = (DartFunction) parentNode; |
| - if (parentFunction.getBody() == null || parentFunction.getBody() instanceof DartNativeBlock) { |
| - return true; |
| - } |
| + if (element instanceof VariableElement) { |
| + Element parent = element.getEnclosingElement(); |
| + if (parent instanceof MethodElement) { |
| + MethodElement parentMethod = (MethodElement) parent; |
| + return !parentMethod.hasBody(); |
| } |
| } |
| return false; |
| @@ -338,26 +311,25 @@ static FieldElementImplementation fieldFromNode(DartField node, |
| * {@link DartClassMember} or part of top level declaration. |
| */ |
| public static boolean isStaticContext(Element element) { |
| - DartNode node = element.getNode(); |
| - while (node != null) { |
| - // Found DartUnit, so top level element was given. |
| - if (node instanceof DartUnit) { |
| - return true; |
| - } |
| - // Found DartClass, so not top level element, can not be static. |
| - if (node instanceof DartClass) { |
| - break; |
| + while (element != null) { |
| + if (element instanceof MethodElement) { |
| + MethodElement methodElement = (MethodElement) element; |
| + if (methodElement.isStatic()) { |
| + return true; |
| + } |
| } |
| - // May be static method or field. |
| - if (node instanceof DartClassMember) { |
| - if (((DartClassMember<?>) node).getModifiers().isStatic()) { |
| + if (element instanceof FieldElement) { |
| + FieldElement fieldElement = (FieldElement) element; |
| + if (fieldElement.isStatic()) { |
| return true; |
| } |
| } |
| - // Go to parent. |
| - node = node.getParent(); |
| + if (element instanceof ClassElement) { |
| + return false; |
| + } |
| + element = element.getEnclosingElement(); |
| } |
| - return false; |
| + return true; |
| } |
| public static boolean isNonFactoryConstructor(Element method) { |
| @@ -417,25 +389,11 @@ static FieldElementImplementation fieldFromNode(DartField node, |
| * corresponds the given {@link MethodElement}. |
| */ |
| public static String getRawMethodName(MethodElement methodElement) { |
| - DartMethodDefinition method = (DartMethodDefinition) methodElement.getNode(); |
| - // Synthetic method (implicit default constructor). |
| - if (method == null) { |
| - return methodElement.getEnclosingElement().getName(); |
| - } |
| - // Real method. |
| - DartExpression nameExpression = method.getName(); |
| - return getRawName(nameExpression); |
| - } |
| - |
| - private static String getRawName(DartNode name) { |
| - if (name instanceof DartIdentifier) { |
| - return ((DartIdentifier) name).getName(); |
| - } else if (name instanceof DartParameterizedTypeNode) { |
| - return getRawName(((DartParameterizedTypeNode) name).getExpression()); |
| - } else { |
| - DartPropertyAccess propertyAccess = (DartPropertyAccess) name; |
| - return getRawName(propertyAccess.getQualifier()) + "." + getRawName(propertyAccess.getName()); |
| + if (methodElement instanceof ConstructorElement) { |
| + ConstructorElement constructorElement = (ConstructorElement) methodElement; |
| + return constructorElement.getRawName(); |
| } |
| + return methodElement.getName(); |
| } |
| /** |
| @@ -480,22 +438,13 @@ static FieldElementImplementation fieldFromNode(DartField node, |
| } |
| /** |
| - * @return the {@link DartNode} which is name of underlying {@link Element}, or just its |
| - * {@link DartNode} if name can not be found. |
| + * @return the {@link SourceInfo} of the name name of underlying {@link Element}, or |
| + * {@link SourceInfo} of {@link Element} itself. |
| */ |
| - @SuppressWarnings("unchecked") |
| - public static DartNode getNameNode(Element element) { |
| - DartNode node = element.getNode(); |
| - if (node instanceof DartDeclaration) { |
| - node = ((DartDeclaration<DartExpression>) node).getName(); |
| - } |
| - if (node instanceof DartFunctionExpression) { |
| - node = ((DartFunctionExpression) node).getName(); |
| - } |
| - return node; |
| + public static SourceInfo getNameLocation(Element element) { |
|
Brian Wilkerson
2012/03/12 19:09:26
In a subsequent CL this method should go away.
scheglov
2012/03/13 00:42:25
Done.
|
| + return element.getNameLocation(); |
| } |
| - |
| /** |
| * @return the {@link String} which contains user-readable description of "target" {@link Element} |
| * location relative to "source". |
| @@ -504,16 +453,15 @@ static FieldElementImplementation fieldFromNode(DartField node, |
| // Prepare "target" SourceInfo. |
| SourceInfo targetInfo; |
| { |
| - DartNode targetNode = getNameNode(target); |
| - if (targetNode == null) { |
| + targetInfo = getNameLocation(target); |
| + if (targetInfo == null) { |
| return "unknown"; |
| } |
| - targetInfo = targetNode.getSourceInfo(); |
| } |
| // Prepare path to the target unit from source unit. |
| String targetPath; |
| { |
| - SourceInfo sourceInfo = source.getNode().getSourceInfo(); |
| + SourceInfo sourceInfo = source.getSourceInfo(); |
| targetPath = getRelativeSourcePath(sourceInfo, targetInfo); |
| } |
| // Prepare (may be empty) target class name. |
| @@ -560,15 +508,11 @@ static FieldElementImplementation fieldFromNode(DartField node, |
| * may be <code>null</code> if top level element. |
| */ |
| public static ClassElement getEnclosingClassElement(Element element) { |
| - DartNode node = element.getNode(); |
| - if (node != null) { |
| - node = node.getParent(); |
| - while (node != null) { |
| - if (node instanceof DartClass) { |
| - return ((DartClass) node).getElement(); |
| - } |
| - node = node.getParent(); |
| + while (element != null) { |
| + if (element instanceof ClassElement) { |
| + return (ClassElement) element; |
| } |
| + element = element.getEnclosingElement(); |
| } |
| return null; |
| } |
| @@ -606,15 +550,16 @@ static FieldElementImplementation fieldFromNode(DartField node, |
| * constructor. |
| */ |
| public static boolean isSyntheticConstructor(ConstructorElement element) { |
| - return element != null && element.getNode() == null; |
| + return element != null && element.isSynthetic(); |
| } |
| /** |
| * @return <code>true</code> if the given {@link ConstructorElement} is a default constructor. |
| */ |
| public static boolean isDefaultConstructor(ConstructorElement element) { |
| - return element != null && element.getParameters().isEmpty() |
| - && Elements.getRawMethodName(element).equals(element.getEnclosingElement().getName()); |
| + return element != null |
| + && element.getParameters().isEmpty() |
| + && getRawMethodName(element).equals(element.getEnclosingElement().getName()); |
| } |
| /** |