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 package com.google.dart.compiler.resolver; | 5 package com.google.dart.compiler.resolver; |
| 6 | 6 |
| 7 import com.google.dart.compiler.ErrorCode; | 7 import com.google.dart.compiler.ErrorCode; |
| 8 import com.google.dart.compiler.ast.DartCatchBlock; | 8 import com.google.dart.compiler.ast.DartCatchBlock; |
| 9 import com.google.dart.compiler.ast.DartFunction; | 9 import com.google.dart.compiler.ast.DartFunction; |
| 10 import com.google.dart.compiler.ast.DartFunctionTypeAlias; | |
| 10 import com.google.dart.compiler.ast.DartNode; | 11 import com.google.dart.compiler.ast.DartNode; |
| 11 import com.google.dart.compiler.ast.DartNodeTraverser; | 12 import com.google.dart.compiler.ast.DartNodeTraverser; |
| 12 import com.google.dart.compiler.ast.DartParameter; | 13 import com.google.dart.compiler.ast.DartParameter; |
| 13 import com.google.dart.compiler.ast.DartTypeNode; | 14 import com.google.dart.compiler.ast.DartTypeNode; |
| 14 import com.google.dart.compiler.ast.DartTypeParameter; | 15 import com.google.dart.compiler.ast.DartTypeParameter; |
| 15 import com.google.dart.compiler.type.DynamicType; | 16 import com.google.dart.compiler.type.DynamicType; |
| 16 import com.google.dart.compiler.type.FunctionType; | 17 import com.google.dart.compiler.type.FunctionType; |
| 17 import com.google.dart.compiler.type.Type; | 18 import com.google.dart.compiler.type.Type; |
| 18 import com.google.dart.compiler.type.TypeVariable; | 19 import com.google.dart.compiler.type.TypeVariable; |
| 19 import com.google.dart.compiler.type.Types; | 20 import com.google.dart.compiler.type.Types; |
| 20 | 21 |
| 21 import java.util.ArrayList; | 22 import java.util.ArrayList; |
| 22 import java.util.List; | 23 import java.util.List; |
| 23 | 24 |
| 24 /** | 25 /** |
| 25 * Shared visitor between Resolver and MemberBuilder. | 26 * Shared visitor between Resolver and MemberBuilder. |
| 26 */ | 27 */ |
| 27 abstract class ResolveVisitor extends DartNodeTraverser<Element> { | 28 abstract class ResolveVisitor extends DartNodeTraverser<Element> { |
| 28 private final CoreTypeProvider typeProvider; | 29 private final CoreTypeProvider typeProvider; |
| 29 | 30 |
| 30 ResolveVisitor(CoreTypeProvider typeProvider) { | 31 ResolveVisitor(CoreTypeProvider typeProvider) { |
| 31 this.typeProvider = typeProvider; | 32 this.typeProvider = typeProvider; |
| 32 } | 33 } |
| 33 | 34 |
| 34 abstract ResolutionContext getContext(); | 35 abstract ResolutionContext getContext(); |
| 35 | 36 |
| 36 final MethodElement resolveFunction(DartFunction node, MethodElement element, | 37 final MethodElement resolveFunction(DartFunction node, MethodElement element, |
| 37 List<TypeVariable> typeVariables) { | 38 List<TypeVariable> typeVariables) { |
| 38 if (typeVariables != null) { | 39 bindTypeVariables(typeVariables); |
| 39 for (TypeVariable typeParameter : typeVariables) { | |
| 40 TypeVariableElement variable = (TypeVariableElement) typeParameter.getEl ement(); | |
| 41 getContext().getScope().declareElement(variable.getName(), variable); | |
| 42 DartTypeParameter typeParameterNode = (DartTypeParameter) variable.getNo de(); | |
| 43 DartTypeNode boundNode = typeParameterNode.getBound(); | |
| 44 Type bound; | |
| 45 if (boundNode != null) { | |
| 46 bound = getContext().resolveType(boundNode, true, ResolverErrorCode.NO _SUCH_TYPE); | |
| 47 boundNode.setType(bound); | |
| 48 } else { | |
| 49 bound = typeProvider.getObjectType(); | |
| 50 } | |
| 51 variable.setBound(bound); | |
| 52 } | |
| 53 } | |
| 54 for (DartParameter parameter : node.getParams()) { | 40 for (DartParameter parameter : node.getParams()) { |
| 55 Elements.addParameter(element, (VariableElement) parameter.accept(this)); | 41 Elements.addParameter(element, (VariableElement) parameter.accept(this)); |
| 56 } | 42 } |
| 57 Type returnType = | 43 Type returnType = |
| 58 resolveType( | 44 resolveType( |
| 59 node.getReturnTypeNode(), | 45 node.getReturnTypeNode(), |
| 60 element.getModifiers().isStatic(), | 46 element.getModifiers().isStatic(), |
| 61 TypeErrorCode.NO_SUCH_TYPE); | 47 TypeErrorCode.NO_SUCH_TYPE); |
| 62 ClassElement functionElement = typeProvider.getFunctionType().getElement(); | 48 ClassElement functionElement = typeProvider.getFunctionType().getElement(); |
| 63 FunctionType type = Types.makeFunctionType(getContext(), functionElement, | 49 FunctionType type = Types.makeFunctionType(getContext(), functionElement, |
| 64 element.getParameters(), returnTy pe, | 50 element.getParameters(), returnTy pe, |
| 65 typeVariables); | 51 typeVariables); |
| 66 Elements.setType(element, type); | 52 Elements.setType(element, type); |
| 67 return element; | 53 return element; |
| 68 } | 54 } |
| 69 | 55 |
| 56 private void bindReturnGenerics(DartTypeNode node) { | |
| 57 for (DartTypeNode typeNode : node.getTypeArguments()) { | |
| 58 if (!(typeNode.getType().getElement() instanceof TypeVariableElement)) { | |
|
zundel
2011/11/29 21:53:46
better to use ElementKind.of()
codefu
2011/11/29 22:42:13
Done.
| |
| 59 bindReturnGenerics(typeNode); | |
| 60 continue; | |
| 61 } | |
| 62 bindTypeVariable((TypeVariableElement) typeNode.getType().getElement()); | |
| 63 } | |
| 64 } | |
| 65 | |
| 66 final FunctionAliasElement resolveFunctionAlias(DartFunctionTypeAlias node) { | |
| 67 // The purpose of this to find generic types and make sure they are bound to object. | |
| 68 DartTypeNode returnNode = node.getReturnTypeNode(); | |
| 69 if (returnNode != null) { | |
| 70 bindReturnGenerics(returnNode); | |
| 71 } | |
| 72 FunctionAliasElement funcAlias = node.getSymbol(); | |
| 73 for (Type type : funcAlias.getTypeParameters()) { | |
| 74 TypeVariableElement typeVar = (TypeVariableElement) type.getElement(); | |
| 75 getContext().getScope().declareElement(typeVar.getName(), typeVar); | |
| 76 } | |
| 77 for (DartTypeParameter param : node.getTypeParameters()) { | |
| 78 param.accept(this); | |
| 79 } | |
| 80 return null; | |
| 81 } | |
| 82 | |
| 83 void bindTypeVariable(TypeVariableElement variable) { | |
| 84 DartTypeParameter typeParameterNode = (DartTypeParameter) variable.getNode() ; | |
| 85 DartTypeNode boundNode = typeParameterNode.getBound(); | |
| 86 Type bound; | |
| 87 if (boundNode != null) { | |
| 88 bound = getContext().resolveType(boundNode, true, ResolverErrorCode.NO_SUC H_TYPE); | |
| 89 boundNode.setType(bound); | |
| 90 } else { | |
| 91 bound = typeProvider.getObjectType(); | |
| 92 } | |
| 93 variable.setBound(bound); | |
| 94 } | |
| 95 | |
| 96 void bindTypeVariables(List<TypeVariable> typeVariables) { | |
| 97 if (typeVariables == null) { | |
| 98 return; | |
| 99 } | |
| 100 for (TypeVariable typeParameter : typeVariables) { | |
| 101 TypeVariableElement variable = (TypeVariableElement) typeParameter.getElem ent(); | |
| 102 bindTypeVariable(variable); | |
| 103 getContext().getScope().declareElement(variable.getName(), variable); | |
| 104 } | |
| 105 } | |
| 106 | |
| 70 abstract boolean isStaticContext(); | 107 abstract boolean isStaticContext(); |
| 71 | 108 |
| 72 @Override | 109 @Override |
| 73 public Element visitParameter(DartParameter node) { | 110 public Element visitParameter(DartParameter node) { |
| 74 ErrorCode typeErrorCode = | 111 ErrorCode typeErrorCode = |
| 75 node.getParent() instanceof DartCatchBlock | 112 node.getParent() instanceof DartCatchBlock |
| 76 ? ResolverErrorCode.NO_SUCH_TYPE | 113 ? ResolverErrorCode.NO_SUCH_TYPE |
| 77 : TypeErrorCode.NO_SUCH_TYPE; | 114 : TypeErrorCode.NO_SUCH_TYPE; |
| 78 Type type = resolveType(node.getTypeNode(), isStaticContext(), typeErrorCode ); | 115 Type type = resolveType(node.getTypeNode(), isStaticContext(), typeErrorCode ); |
| 79 VariableElement element = Elements.parameterElement(node, node.getParameterN ame(), | 116 VariableElement element = Elements.parameterElement(node, node.getParameterN ame(), |
| 80 node.getModifiers()); | 117 node.getModifiers()); |
| 81 List<DartParameter> functionParameters = node.getFunctionParameters(); | 118 List<DartParameter> functionParameters = node.getFunctionParameters(); |
| 82 if (functionParameters != null) { | 119 if (functionParameters != null) { |
| 83 List<VariableElement> parameterElements = | 120 List<VariableElement> parameterElements = |
| 84 new ArrayList<VariableElement>(functionParameters.size()); | 121 new ArrayList<VariableElement>(functionParameters.size()); |
| 85 for (DartParameter parameter: functionParameters) { | 122 for (DartParameter parameter: functionParameters) { |
| 86 parameterElements.add((VariableElement) parameter.accept(this)); | 123 parameterElements.add((VariableElement) parameter.accept(this)); |
| 87 } | 124 } |
| 88 ClassElement functionElement = typeProvider.getFunctionType().getElement() ; | 125 ClassElement functionElement = typeProvider.getFunctionType().getElement() ; |
| 89 type = Types.makeFunctionType(getContext(), functionElement, parameterElem ents, type, null); | 126 type = Types.makeFunctionType(getContext(), functionElement, parameterElem ents, type, null); |
| 127 DartTypeNode typeNode = node.getTypeNode(); | |
| 128 if (typeNode != null) { | |
| 129 typeNode.setType(type); | |
| 130 } | |
| 90 } | 131 } |
| 91 Elements.setType(element, type); | 132 Elements.setType(element, type); |
| 92 return recordElement(node, element); | 133 return recordElement(node, element); |
| 93 } | 134 } |
| 94 | 135 |
| 95 final Type resolveType(DartTypeNode node, boolean isStatic, ErrorCode errorCod e) { | 136 final Type resolveType(DartTypeNode node, boolean isStatic, ErrorCode errorCod e) { |
| 96 if (node == null) { | 137 if (node == null) { |
| 97 return getTypeProvider().getDynamicType(); | 138 return getTypeProvider().getDynamicType(); |
| 98 } | 139 } |
| 99 assert node.getType() == null || node.getType() instanceof DynamicType; | 140 assert node.getType() == null || node.getType() instanceof DynamicType; |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 113 return null; | 154 return null; |
| 114 } | 155 } |
| 115 node.setSymbol(element); | 156 node.setSymbol(element); |
| 116 return element; | 157 return element; |
| 117 } | 158 } |
| 118 | 159 |
| 119 CoreTypeProvider getTypeProvider() { | 160 CoreTypeProvider getTypeProvider() { |
| 120 return typeProvider; | 161 return typeProvider; |
| 121 } | 162 } |
| 122 } | 163 } |
| OLD | NEW |