| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 part of dart2js.mirrors; | 5 part of dart2js.mirrors; |
| 6 | 6 |
| 7 //------------------------------------------------------------------------------ | 7 //------------------------------------------------------------------------------ |
| 8 // Member mirrors implementation. | 8 // Member mirrors implementation. |
| 9 //------------------------------------------------------------------------------ | 9 //------------------------------------------------------------------------------ |
| 10 | 10 |
| 11 abstract class Dart2JsMemberMirror extends Dart2JsElementMirror { | 11 abstract class Dart2JsMemberMirror extends Dart2JsElementMirror { |
| 12 | |
| 13 Dart2JsMemberMirror(Dart2JsMirrorSystem system, AstElement element) | 12 Dart2JsMemberMirror(Dart2JsMirrorSystem system, AstElement element) |
| 14 : super(system, element); | 13 : super(system, element); |
| 15 | 14 |
| 16 bool get isStatic => false; | 15 bool get isStatic => false; |
| 17 } | 16 } |
| 18 | 17 |
| 19 | |
| 20 class Dart2JsMethodKind { | 18 class Dart2JsMethodKind { |
| 21 static const Dart2JsMethodKind REGULAR = const Dart2JsMethodKind("regular"); | 19 static const Dart2JsMethodKind REGULAR = const Dart2JsMethodKind("regular"); |
| 22 static const Dart2JsMethodKind GENERATIVE = | 20 static const Dart2JsMethodKind GENERATIVE = |
| 23 const Dart2JsMethodKind("generative"); | 21 const Dart2JsMethodKind("generative"); |
| 24 static const Dart2JsMethodKind REDIRECTING = | 22 static const Dart2JsMethodKind REDIRECTING = |
| 25 const Dart2JsMethodKind("redirecting"); | 23 const Dart2JsMethodKind("redirecting"); |
| 26 static const Dart2JsMethodKind CONST = const Dart2JsMethodKind("const"); | 24 static const Dart2JsMethodKind CONST = const Dart2JsMethodKind("const"); |
| 27 static const Dart2JsMethodKind FACTORY = const Dart2JsMethodKind("factory"); | 25 static const Dart2JsMethodKind FACTORY = const Dart2JsMethodKind("factory"); |
| 28 static const Dart2JsMethodKind GETTER = const Dart2JsMethodKind("getter"); | 26 static const Dart2JsMethodKind GETTER = const Dart2JsMethodKind("getter"); |
| 29 static const Dart2JsMethodKind SETTER = const Dart2JsMethodKind("setter"); | 27 static const Dart2JsMethodKind SETTER = const Dart2JsMethodKind("setter"); |
| 30 static const Dart2JsMethodKind OPERATOR = const Dart2JsMethodKind("operator"); | 28 static const Dart2JsMethodKind OPERATOR = const Dart2JsMethodKind("operator"); |
| 31 | 29 |
| 32 final String text; | 30 final String text; |
| 33 | 31 |
| 34 const Dart2JsMethodKind(this.text); | 32 const Dart2JsMethodKind(this.text); |
| 35 | 33 |
| 36 String toString() => text; | 34 String toString() => text; |
| 37 } | 35 } |
| 38 | 36 |
| 39 class Dart2JsMethodMirror extends Dart2JsMemberMirror | 37 class Dart2JsMethodMirror extends Dart2JsMemberMirror implements MethodMirror { |
| 40 implements MethodMirror { | |
| 41 final Dart2JsDeclarationMirror owner; | 38 final Dart2JsDeclarationMirror owner; |
| 42 final String _simpleNameString; | 39 final String _simpleNameString; |
| 43 final Dart2JsMethodKind _kind; | 40 final Dart2JsMethodKind _kind; |
| 44 | 41 |
| 45 Dart2JsMethodMirror._internal(Dart2JsDeclarationMirror owner, | 42 Dart2JsMethodMirror._internal( |
| 43 Dart2JsDeclarationMirror owner, |
| 46 FunctionElement function, | 44 FunctionElement function, |
| 47 String this._simpleNameString, | 45 String this._simpleNameString, |
| 48 Dart2JsMethodKind this._kind) | 46 Dart2JsMethodKind this._kind) |
| 49 : this.owner = owner, | 47 : this.owner = owner, |
| 50 super(owner.mirrorSystem, function); | 48 super(owner.mirrorSystem, function); |
| 51 | 49 |
| 52 factory Dart2JsMethodMirror(Dart2JsDeclarationMirror owner, | 50 factory Dart2JsMethodMirror( |
| 53 FunctionElement function) { | 51 Dart2JsDeclarationMirror owner, FunctionElement function) { |
| 54 String simpleName = function.name; | 52 String simpleName = function.name; |
| 55 // TODO(ahe): This method should not be calling | 53 // TODO(ahe): This method should not be calling |
| 56 // Elements.operatorNameToIdentifier. | 54 // Elements.operatorNameToIdentifier. |
| 57 Dart2JsMethodKind kind; | 55 Dart2JsMethodKind kind; |
| 58 if (function.kind == ElementKind.GETTER) { | 56 if (function.kind == ElementKind.GETTER) { |
| 59 kind = Dart2JsMethodKind.GETTER; | 57 kind = Dart2JsMethodKind.GETTER; |
| 60 } else if (function.kind == ElementKind.SETTER) { | 58 } else if (function.kind == ElementKind.SETTER) { |
| 61 kind = Dart2JsMethodKind.SETTER; | 59 kind = Dart2JsMethodKind.SETTER; |
| 62 simpleName = '$simpleName='; | 60 simpleName = '$simpleName='; |
| 63 } else if (function.kind == ElementKind.GENERATIVE_CONSTRUCTOR) { | 61 } else if (function.kind == ElementKind.GENERATIVE_CONSTRUCTOR) { |
| 64 // TODO(johnniwinther): Support detection of redirecting constructors. | 62 // TODO(johnniwinther): Support detection of redirecting constructors. |
| 65 if (function.isConst) { | 63 if (function.isConst) { |
| 66 kind = Dart2JsMethodKind.CONST; | 64 kind = Dart2JsMethodKind.CONST; |
| 67 } else { | 65 } else { |
| 68 kind = Dart2JsMethodKind.GENERATIVE; | 66 kind = Dart2JsMethodKind.GENERATIVE; |
| 69 } | 67 } |
| 70 } else if (function.isFactoryConstructor) { | 68 } else if (function.isFactoryConstructor) { |
| 71 // TODO(johnniwinther): Support detection of redirecting constructors. | 69 // TODO(johnniwinther): Support detection of redirecting constructors. |
| 72 kind = Dart2JsMethodKind.FACTORY; | 70 kind = Dart2JsMethodKind.FACTORY; |
| 73 } else if (function.isOperator) { | 71 } else if (function.isOperator) { |
| 74 kind = Dart2JsMethodKind.OPERATOR; | 72 kind = Dart2JsMethodKind.OPERATOR; |
| 75 } else { | 73 } else { |
| 76 kind = Dart2JsMethodKind.REGULAR; | 74 kind = Dart2JsMethodKind.REGULAR; |
| 77 } | 75 } |
| 78 return new Dart2JsMethodMirror._internal(owner, function, | 76 return new Dart2JsMethodMirror._internal(owner, function, simpleName, kind); |
| 79 simpleName, kind); | |
| 80 } | 77 } |
| 81 | 78 |
| 82 FunctionElement get _function => _element; | 79 FunctionElement get _function => _element; |
| 83 | 80 |
| 84 bool get isTopLevel => owner is LibraryMirror; | 81 bool get isTopLevel => owner is LibraryMirror; |
| 85 | 82 |
| 86 // TODO(johnniwinther): This seems stale and broken. | 83 // TODO(johnniwinther): This seems stale and broken. |
| 87 Symbol get constructorName => isConstructor ? simpleName : const Symbol(''); | 84 Symbol get constructorName => isConstructor ? simpleName : const Symbol(''); |
| 88 | 85 |
| 89 bool get isConstructor | 86 bool get isConstructor => |
| 90 => isGenerativeConstructor || isConstConstructor || | 87 isGenerativeConstructor || |
| 91 isFactoryConstructor || isRedirectingConstructor; | 88 isConstConstructor || |
| 89 isFactoryConstructor || |
| 90 isRedirectingConstructor; |
| 92 | 91 |
| 93 bool get isSynthetic => false; | 92 bool get isSynthetic => false; |
| 94 | 93 |
| 95 bool get isStatic => _function.isStatic; | 94 bool get isStatic => _function.isStatic; |
| 96 | 95 |
| 97 List<ParameterMirror> get parameters { | 96 List<ParameterMirror> get parameters { |
| 98 return _parametersFromFunctionSignature(this, | 97 return _parametersFromFunctionSignature(this, _function.functionSignature); |
| 99 _function.functionSignature); | |
| 100 } | 98 } |
| 101 | 99 |
| 102 TypeMirror get returnType => owner._getTypeMirror( | 100 TypeMirror get returnType => |
| 103 _function.functionSignature.type.returnType); | 101 owner._getTypeMirror(_function.functionSignature.type.returnType); |
| 104 | 102 |
| 105 bool get isAbstract => _function.isAbstract; | 103 bool get isAbstract => _function.isAbstract; |
| 106 | 104 |
| 107 bool get isRegularMethod => !(isGetter || isSetter || isConstructor); | 105 bool get isRegularMethod => !(isGetter || isSetter || isConstructor); |
| 108 | 106 |
| 109 bool get isConstConstructor => _kind == Dart2JsMethodKind.CONST; | 107 bool get isConstConstructor => _kind == Dart2JsMethodKind.CONST; |
| 110 | 108 |
| 111 bool get isGenerativeConstructor => _kind == Dart2JsMethodKind.GENERATIVE; | 109 bool get isGenerativeConstructor => _kind == Dart2JsMethodKind.GENERATIVE; |
| 112 | 110 |
| 113 bool get isRedirectingConstructor => _kind == Dart2JsMethodKind.REDIRECTING; | 111 bool get isRedirectingConstructor => _kind == Dart2JsMethodKind.REDIRECTING; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 133 // [MethodMirror]? | 131 // [MethodMirror]? |
| 134 String get source => location.sourceText; | 132 String get source => location.sourceText; |
| 135 | 133 |
| 136 String toString() => 'Mirror on method ${_element.name}'; | 134 String toString() => 'Mirror on method ${_element.name}'; |
| 137 } | 135 } |
| 138 | 136 |
| 139 class Dart2JsFieldMirror extends Dart2JsMemberMirror implements VariableMirror { | 137 class Dart2JsFieldMirror extends Dart2JsMemberMirror implements VariableMirror { |
| 140 final Dart2JsDeclarationMirror owner; | 138 final Dart2JsDeclarationMirror owner; |
| 141 VariableElement _variable; | 139 VariableElement _variable; |
| 142 | 140 |
| 143 Dart2JsFieldMirror(Dart2JsDeclarationMirror owner, | 141 Dart2JsFieldMirror(Dart2JsDeclarationMirror owner, VariableElement variable) |
| 144 VariableElement variable) | |
| 145 : this.owner = owner, | 142 : this.owner = owner, |
| 146 this._variable = variable, | 143 this._variable = variable, |
| 147 super(owner.mirrorSystem, variable); | 144 super(owner.mirrorSystem, variable); |
| 148 | 145 |
| 149 bool get isTopLevel => owner is LibraryMirror; | 146 bool get isTopLevel => owner is LibraryMirror; |
| 150 | 147 |
| 151 bool get isStatic => _variable.isStatic; | 148 bool get isStatic => _variable.isStatic; |
| 152 | 149 |
| 153 bool get isFinal => _variable.isFinal; | 150 bool get isFinal => _variable.isFinal; |
| 154 | 151 |
| 155 bool get isConst => _variable.isConst; | 152 bool get isConst => _variable.isConst; |
| 156 | 153 |
| 157 TypeMirror get type => owner._getTypeMirror(_variable.type); | 154 TypeMirror get type => owner._getTypeMirror(_variable.type); |
| 158 | |
| 159 | |
| 160 } | 155 } |
| 161 | 156 |
| 162 class Dart2JsParameterMirror extends Dart2JsMemberMirror | 157 class Dart2JsParameterMirror extends Dart2JsMemberMirror |
| 163 implements ParameterMirror { | 158 implements ParameterMirror { |
| 164 final Dart2JsDeclarationMirror owner; | 159 final Dart2JsDeclarationMirror owner; |
| 165 final bool isOptional; | 160 final bool isOptional; |
| 166 final bool isNamed; | 161 final bool isNamed; |
| 167 | 162 |
| 168 factory Dart2JsParameterMirror(Dart2JsDeclarationMirror owner, | 163 factory Dart2JsParameterMirror( |
| 169 FormalElement element, | 164 Dart2JsDeclarationMirror owner, FormalElement element, |
| 170 {bool isOptional: false, | 165 {bool isOptional: false, bool isNamed: false}) { |
| 171 bool isNamed: false}) { | |
| 172 if (element is InitializingFormalElement) { | 166 if (element is InitializingFormalElement) { |
| 173 return new Dart2JsFieldParameterMirror( | 167 return new Dart2JsFieldParameterMirror( |
| 174 owner, element, isOptional, isNamed); | 168 owner, element, isOptional, isNamed); |
| 175 } else { | 169 } else { |
| 176 return new Dart2JsParameterMirror._normal( | 170 return new Dart2JsParameterMirror._normal( |
| 177 owner, element, isOptional, isNamed); | 171 owner, element, isOptional, isNamed); |
| 178 } | 172 } |
| 179 } | 173 } |
| 180 | 174 |
| 181 Dart2JsParameterMirror._normal(Dart2JsDeclarationMirror owner, | 175 Dart2JsParameterMirror._normal(Dart2JsDeclarationMirror owner, |
| 182 FormalElement element, | 176 FormalElement element, this.isOptional, this.isNamed) |
| 183 this.isOptional, | 177 : this.owner = owner, |
| 184 this.isNamed) | 178 super(owner.mirrorSystem, element); |
| 185 : this.owner = owner, | |
| 186 super(owner.mirrorSystem, element); | |
| 187 | 179 |
| 188 FormalElement get _element => super._element; | 180 FormalElement get _element => super._element; |
| 189 | 181 |
| 190 TypeMirror get type => owner._getTypeMirror(_element.type); | 182 TypeMirror get type => owner._getTypeMirror(_element.type); |
| 191 | 183 |
| 192 bool get isFinal => false; | 184 bool get isFinal => false; |
| 193 | 185 |
| 194 bool get isConst => false; | 186 bool get isConst => false; |
| 195 | 187 |
| 196 InstanceMirror get defaultValue { | 188 InstanceMirror get defaultValue { |
| 197 if (hasDefaultValue) { | 189 if (hasDefaultValue) { |
| 198 // TODO(johnniwinther): Get the constant from the [TreeElements] | 190 // TODO(johnniwinther): Get the constant from the [TreeElements] |
| 199 // associated with the enclosing method. | 191 // associated with the enclosing method. |
| 200 ParameterElement parameter = _element; | 192 ParameterElement parameter = _element; |
| 201 ConstantExpression constant = mirrorSystem.compiler.constants | 193 ConstantExpression constant = |
| 202 .getConstantForVariable(parameter); | 194 mirrorSystem.compiler.constants.getConstantForVariable(parameter); |
| 203 assert(invariant(parameter, constant != null, | 195 assert(invariant(parameter, constant != null, |
| 204 message: "Missing constant for parameter " | 196 message: "Missing constant for parameter " |
| 205 "$parameter with default value.")); | 197 "$parameter with default value.")); |
| 206 return _convertConstantToInstanceMirror(mirrorSystem, | 198 return _convertConstantToInstanceMirror(mirrorSystem, constant, |
| 207 constant, mirrorSystem.compiler.constants.getConstantValue(constant)); | 199 mirrorSystem.compiler.constants.getConstantValue(constant)); |
| 208 } | 200 } |
| 209 return null; | 201 return null; |
| 210 } | 202 } |
| 211 | 203 |
| 212 bool get hasDefaultValue { | 204 bool get hasDefaultValue { |
| 213 if (_element is ParameterElement) { | 205 if (_element is ParameterElement) { |
| 214 ParameterElement parameter = _element; | 206 ParameterElement parameter = _element; |
| 215 return parameter.initializer != null; | 207 return parameter.initializer != null; |
| 216 } | 208 } |
| 217 return false; | 209 return false; |
| 218 } | 210 } |
| 219 | 211 |
| 220 bool get isInitializingFormal => false; | 212 bool get isInitializingFormal => false; |
| 221 | 213 |
| 222 VariableMirror get initializedField => null; | 214 VariableMirror get initializedField => null; |
| 223 } | 215 } |
| 224 | 216 |
| 225 class Dart2JsFieldParameterMirror extends Dart2JsParameterMirror { | 217 class Dart2JsFieldParameterMirror extends Dart2JsParameterMirror { |
| 226 | |
| 227 Dart2JsFieldParameterMirror(Dart2JsDeclarationMirror method, | 218 Dart2JsFieldParameterMirror(Dart2JsDeclarationMirror method, |
| 228 InitializingFormalElement element, | 219 InitializingFormalElement element, bool isOptional, bool isNamed) |
| 229 bool isOptional, | |
| 230 bool isNamed) | |
| 231 : super._normal(method, element, isOptional, isNamed); | 220 : super._normal(method, element, isOptional, isNamed); |
| 232 | 221 |
| 233 InitializingFormalElement get _fieldParameterElement => _element; | 222 InitializingFormalElement get _fieldParameterElement => _element; |
| 234 | 223 |
| 235 bool get isInitializingFormal => true; | 224 bool get isInitializingFormal => true; |
| 236 | 225 |
| 237 VariableMirror get initializedField => new Dart2JsFieldMirror( | 226 VariableMirror get initializedField => |
| 238 owner.owner, _fieldParameterElement.fieldElement); | 227 new Dart2JsFieldMirror(owner.owner, _fieldParameterElement.fieldElement); |
| 239 } | 228 } |
| OLD | NEW |