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