| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2014, 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 /** | |
| 6 * Code for classifying the semantics of identifiers appearing in a Dart file. | |
| 7 */ | |
| 8 library analyzer2dart.identifierSemantics; | |
| 9 | |
| 10 import 'package:analyzer/analyzer.dart'; | |
| 11 import 'package:analyzer/src/generated/element.dart'; | |
| 12 | |
| 13 // TODO(johnniwinther,paulberry): This should be a constant. | |
| 14 final AccessSemanticsVisitor ACCESS_SEMANTICS_VISITOR = | |
| 15 new AccessSemanticsVisitor(); | |
| 16 | |
| 17 /** | |
| 18 * Enum representing the different kinds of destinations which a property | |
| 19 * access or method or function invocation might refer to. | |
| 20 */ | |
| 21 class AccessKind { | |
| 22 /** | |
| 23 * The destination of the access is an instance method, property, or field | |
| 24 * of a class, and thus must be determined dynamically. | |
| 25 */ | |
| 26 static const AccessKind DYNAMIC = const AccessKind._('DYNAMIC'); | |
| 27 | |
| 28 /** | |
| 29 * The destination of the access is a function that is defined locally within | |
| 30 * an enclosing function or method. | |
| 31 */ | |
| 32 static const AccessKind LOCAL_FUNCTION = const AccessKind._('LOCAL_FUNCTION'); | |
| 33 | |
| 34 /** | |
| 35 * The destination of the access is a variable that is defined locally within | |
| 36 * an enclosing function or method. | |
| 37 */ | |
| 38 static const AccessKind LOCAL_VARIABLE = const AccessKind._('LOCAL_VARIABLE'); | |
| 39 | |
| 40 /** | |
| 41 * The destination of the access is a variable that is defined as a parameter | |
| 42 * to an enclosing function or method. | |
| 43 */ | |
| 44 static const AccessKind PARAMETER = const AccessKind._('PARAMETER'); | |
| 45 | |
| 46 /** | |
| 47 * The destination of the access is a field that is defined statically within | |
| 48 * a class, or a top level variable within a library. | |
| 49 */ | |
| 50 static const AccessKind STATIC_FIELD = const AccessKind._('STATIC_FIELD'); | |
| 51 | |
| 52 /** | |
| 53 * The destination of the access is a method that is defined statically | |
| 54 * within a class, or at top level within a library. | |
| 55 */ | |
| 56 static const AccessKind STATIC_METHOD = const AccessKind._('STATIC_METHOD'); | |
| 57 | |
| 58 /** | |
| 59 * The destination of the access is a property getter/setter that is defined | |
| 60 * statically within a class, or at top level within a library. | |
| 61 */ | |
| 62 static const AccessKind STATIC_PROPERTY = | |
| 63 const AccessKind._('STATIC_PROPERTY'); | |
| 64 | |
| 65 /** | |
| 66 * The destination of the access is a toplevel class, function typedef, mixin | |
| 67 * application, or the built-in type "dynamic". | |
| 68 */ | |
| 69 static const AccessKind TOPLEVEL_TYPE = const AccessKind._('TOPLEVEL_TYPE'); | |
| 70 | |
| 71 /** | |
| 72 * The destination of the access is a type parameter of the enclosing class. | |
| 73 */ | |
| 74 static const AccessKind TYPE_PARAMETER = const AccessKind._('TYPE_PARAMETER'); | |
| 75 | |
| 76 final String name; | |
| 77 | |
| 78 const AccessKind._(this.name); | |
| 79 | |
| 80 String toString() => name; | |
| 81 } | |
| 82 | |
| 83 /** | |
| 84 * Data structure used to classify the semantics of a property access or method | |
| 85 * or function invocation. | |
| 86 */ | |
| 87 // TODO(paulberry,johnniwinther): Support index operations in AccessSemantics. | |
| 88 class AccessSemantics { | |
| 89 /** | |
| 90 * The kind of access. | |
| 91 */ | |
| 92 final AccessKind kind; | |
| 93 | |
| 94 /** | |
| 95 * The identifier being used to access the property, method, or function. | |
| 96 */ | |
| 97 final SimpleIdentifier identifier; | |
| 98 | |
| 99 /** | |
| 100 * The element being accessed, if statically known. This will be null if | |
| 101 * [kind] is DYNAMIC or if the element is undefined (e.g. an attempt to | |
| 102 * access a non-existent static method in a class). | |
| 103 */ | |
| 104 final Element element; | |
| 105 | |
| 106 /** | |
| 107 * The class containing the element being accessed, if this is a static | |
| 108 * reference to an element in a class. This will be null if [kind] is | |
| 109 * DYNAMIC, LOCAL_FUNCTION, LOCAL_VARIABLE, PARAMETER, TOPLEVEL_CLASS, or | |
| 110 * TYPE_PARAMETER, or if the element being accessed is defined at toplevel | |
| 111 * within a library. | |
| 112 * | |
| 113 * Note: it is possible for [classElement] to be non-null and for [element] | |
| 114 * to be null; for example this occurs if the element being accessed is a | |
| 115 * non-existent static method or field inside an existing class. | |
| 116 */ | |
| 117 final ClassElement classElement; | |
| 118 | |
| 119 // TODO(paulberry): would it also be useful to store the libraryElement? | |
| 120 | |
| 121 /** | |
| 122 * When [kind] is DYNAMIC, the expression whose runtime type determines the | |
| 123 * class in which [identifier] should be looked up. Null if the expression | |
| 124 * is implicit "this". | |
| 125 * | |
| 126 * When [kind] is not DYNAMIC, this field is always null. | |
| 127 */ | |
| 128 final Expression target; | |
| 129 | |
| 130 /** | |
| 131 * True if this is an invocation of a method, or a call on a property. | |
| 132 */ | |
| 133 final bool isInvoke; | |
| 134 | |
| 135 AccessSemantics.dynamic(this.identifier, this.target, {this.isInvoke: false}) | |
| 136 : kind = AccessKind.DYNAMIC, | |
| 137 element = null, | |
| 138 classElement = null; | |
| 139 | |
| 140 AccessSemantics.localFunction(this.identifier, this.element, {this.isInvoke: | |
| 141 false}) | |
| 142 : kind = AccessKind.LOCAL_FUNCTION, | |
| 143 classElement = null, | |
| 144 target = null; | |
| 145 | |
| 146 AccessSemantics.localVariable(this.identifier, this.element, {this.isInvoke: | |
| 147 false}) | |
| 148 : kind = AccessKind.LOCAL_VARIABLE, | |
| 149 classElement = null, | |
| 150 target = null; | |
| 151 | |
| 152 AccessSemantics.parameter(this.identifier, this.element, {this.isInvoke: | |
| 153 false}) | |
| 154 : kind = AccessKind.PARAMETER, | |
| 155 classElement = null, | |
| 156 target = null; | |
| 157 | |
| 158 AccessSemantics.staticField(this.identifier, this.element, this.classElement, | |
| 159 {this.isInvoke: false}) | |
| 160 : kind = AccessKind.STATIC_FIELD, | |
| 161 target = null; | |
| 162 | |
| 163 AccessSemantics.staticMethod(this.identifier, this.element, this.classElement, | |
| 164 {this.isInvoke: false}) | |
| 165 : kind = AccessKind.STATIC_METHOD, | |
| 166 target = null; | |
| 167 | |
| 168 AccessSemantics.staticProperty(this.identifier, this.element, | |
| 169 this.classElement, {this.isInvoke: false}) | |
| 170 : kind = AccessKind.STATIC_PROPERTY, | |
| 171 target = null; | |
| 172 | |
| 173 AccessSemantics.toplevelType(this.identifier, this.element, {this.isInvoke: | |
| 174 false}) | |
| 175 : kind = AccessKind.TOPLEVEL_TYPE, | |
| 176 classElement = null, | |
| 177 target = null; | |
| 178 | |
| 179 AccessSemantics.typeParameter(this.identifier, this.element, {this.isInvoke: | |
| 180 false}) | |
| 181 : kind = AccessKind.TYPE_PARAMETER, | |
| 182 classElement = null, | |
| 183 target = null; | |
| 184 | |
| 185 /** | |
| 186 * True if this is a read access to a property, or a method tear-off. Note | |
| 187 * that both [isRead] and [isWrite] will be true in the case of a | |
| 188 * read-modify-write operation (e.g. "+="). | |
| 189 */ | |
| 190 bool get isRead => !isInvoke && identifier.inGetterContext(); | |
| 191 | |
| 192 /** | |
| 193 * True if this is a write access to a property, or an (erroneous) attempt to | |
| 194 * write to a method. Note that both [isRead] and [isWrite] will be true in | |
| 195 * the case of a read-modify-write operation (e.g. "+="). | |
| 196 */ | |
| 197 bool get isWrite => identifier.inSetterContext(); | |
| 198 | |
| 199 String toString() { | |
| 200 StringBuffer sb = new StringBuffer(); | |
| 201 sb.write('AccessSemantics['); | |
| 202 sb.write('kind=$kind,'); | |
| 203 if (isRead && isWrite) { | |
| 204 assert(!isInvoke); | |
| 205 sb.write('read/write,'); | |
| 206 } else if (isRead) { | |
| 207 sb.write('read,'); | |
| 208 } else if (isWrite) { | |
| 209 sb.write('write,'); | |
| 210 } else if (isInvoke) { | |
| 211 sb.write('call,'); | |
| 212 } | |
| 213 if (element != null) { | |
| 214 sb.write('element='); | |
| 215 if (classElement != null) { | |
| 216 sb.write('${classElement.name}.'); | |
| 217 } | |
| 218 sb.write('${element}'); | |
| 219 } else { | |
| 220 if (target == null) { | |
| 221 sb.write('target=this.$identifier'); | |
| 222 } else { | |
| 223 sb.write('target=$target.$identifier'); | |
| 224 } | |
| 225 } | |
| 226 sb.write(']'); | |
| 227 return sb.toString(); | |
| 228 } | |
| 229 } | |
| 230 | |
| 231 // TODO(johnniwinther,paulberry): This should extend a non-recursive visitor. | |
| 232 class AccessSemanticsVisitor extends RecursiveAstVisitor<AccessSemantics> { | |
| 233 /** | |
| 234 * Return the semantics for [node]. | |
| 235 */ | |
| 236 @override | |
| 237 AccessSemantics visitMethodInvocation(MethodInvocation node) { | |
| 238 Expression target = node.realTarget; | |
| 239 Element staticElement = node.methodName.staticElement; | |
| 240 if (target == null) { | |
| 241 if (staticElement is FunctionElement) { | |
| 242 if (staticElement.enclosingElement is CompilationUnitElement) { | |
| 243 return new AccessSemantics.staticMethod( | |
| 244 node.methodName, | |
| 245 staticElement, | |
| 246 null, | |
| 247 isInvoke: true); | |
| 248 } else { | |
| 249 return new AccessSemantics.localFunction( | |
| 250 node.methodName, | |
| 251 staticElement, | |
| 252 isInvoke: true); | |
| 253 } | |
| 254 } else if (staticElement is MethodElement && staticElement.isStatic) { | |
| 255 return new AccessSemantics.staticMethod( | |
| 256 node.methodName, | |
| 257 staticElement, | |
| 258 staticElement.enclosingElement, | |
| 259 isInvoke: true); | |
| 260 } else if (staticElement is PropertyAccessorElement) { | |
| 261 if (staticElement.isSynthetic) { | |
| 262 if (staticElement.enclosingElement is CompilationUnitElement) { | |
| 263 return new AccessSemantics.staticField( | |
| 264 node.methodName, | |
| 265 staticElement.variable, | |
| 266 null, | |
| 267 isInvoke: true); | |
| 268 } else if (staticElement.isStatic) { | |
| 269 return new AccessSemantics.staticField( | |
| 270 node.methodName, | |
| 271 staticElement.variable, | |
| 272 staticElement.enclosingElement, | |
| 273 isInvoke: true); | |
| 274 } | |
| 275 } else { | |
| 276 if (staticElement.enclosingElement is CompilationUnitElement) { | |
| 277 return new AccessSemantics.staticProperty( | |
| 278 node.methodName, | |
| 279 staticElement, | |
| 280 null, | |
| 281 isInvoke: true); | |
| 282 } else if (staticElement.isStatic) { | |
| 283 return new AccessSemantics.staticProperty( | |
| 284 node.methodName, | |
| 285 staticElement, | |
| 286 staticElement.enclosingElement, | |
| 287 isInvoke: true); | |
| 288 } | |
| 289 } | |
| 290 } else if (staticElement is LocalVariableElement) { | |
| 291 return new AccessSemantics.localVariable( | |
| 292 node.methodName, | |
| 293 staticElement, | |
| 294 isInvoke: true); | |
| 295 } else if (staticElement is ParameterElement) { | |
| 296 return new AccessSemantics.parameter( | |
| 297 node.methodName, | |
| 298 staticElement, | |
| 299 isInvoke: true); | |
| 300 } else if (staticElement is TypeParameterElement) { | |
| 301 return new AccessSemantics.typeParameter( | |
| 302 node.methodName, | |
| 303 staticElement, | |
| 304 isInvoke: true); | |
| 305 } else if (staticElement is ClassElement || | |
| 306 staticElement is FunctionTypeAliasElement || | |
| 307 staticElement is DynamicElementImpl) { | |
| 308 return new AccessSemantics.toplevelType( | |
| 309 node.methodName, | |
| 310 staticElement, | |
| 311 isInvoke: true); | |
| 312 } | |
| 313 } else if (target is Identifier) { | |
| 314 Element targetStaticElement = target.staticElement; | |
| 315 if (targetStaticElement is PrefixElement) { | |
| 316 if (staticElement == null) { | |
| 317 return new AccessSemantics.dynamic( | |
| 318 node.methodName, | |
| 319 null, | |
| 320 isInvoke: true); | |
| 321 } else if (staticElement is PropertyAccessorElement) { | |
| 322 if (staticElement.isSynthetic) { | |
| 323 return new AccessSemantics.staticField( | |
| 324 node.methodName, | |
| 325 staticElement.variable, | |
| 326 null, | |
| 327 isInvoke: true); | |
| 328 } else { | |
| 329 return new AccessSemantics.staticProperty( | |
| 330 node.methodName, | |
| 331 staticElement, | |
| 332 null, | |
| 333 isInvoke: true); | |
| 334 } | |
| 335 } else if (staticElement is TypeParameterElement) { | |
| 336 return new AccessSemantics.typeParameter( | |
| 337 node.methodName, | |
| 338 staticElement, | |
| 339 isInvoke: true); | |
| 340 } else if (staticElement is ClassElement || | |
| 341 staticElement is FunctionTypeAliasElement) { | |
| 342 return new AccessSemantics.toplevelType( | |
| 343 node.methodName, | |
| 344 staticElement, | |
| 345 isInvoke: true); | |
| 346 } else { | |
| 347 return new AccessSemantics.staticMethod( | |
| 348 node.methodName, | |
| 349 staticElement, | |
| 350 null, | |
| 351 isInvoke: true); | |
| 352 } | |
| 353 } else if (targetStaticElement is ClassElement) { | |
| 354 if (staticElement is PropertyAccessorElement) { | |
| 355 if (staticElement.isSynthetic) { | |
| 356 return new AccessSemantics.staticField( | |
| 357 node.methodName, | |
| 358 staticElement.variable, | |
| 359 targetStaticElement, | |
| 360 isInvoke: true); | |
| 361 } else { | |
| 362 return new AccessSemantics.staticProperty( | |
| 363 node.methodName, | |
| 364 staticElement, | |
| 365 targetStaticElement, | |
| 366 isInvoke: true); | |
| 367 } | |
| 368 } else { | |
| 369 return new AccessSemantics.staticMethod( | |
| 370 node.methodName, | |
| 371 staticElement, | |
| 372 targetStaticElement, | |
| 373 isInvoke: true); | |
| 374 } | |
| 375 } | |
| 376 } | |
| 377 return new AccessSemantics.dynamic(node.methodName, target, isInvoke: true); | |
| 378 } | |
| 379 | |
| 380 /** | |
| 381 * Return the access semantics for [node]. | |
| 382 */ | |
| 383 @override | |
| 384 AccessSemantics visitPrefixedIdentifier(PrefixedIdentifier node) { | |
| 385 return _classifyPrefixed(node.prefix, node.identifier); | |
| 386 } | |
| 387 | |
| 388 /** | |
| 389 * Return the access semantics for [node]. | |
| 390 */ | |
| 391 @override | |
| 392 AccessSemantics visitPropertyAccess(PropertyAccess node) { | |
| 393 if (node.target is Identifier) { | |
| 394 return _classifyPrefixed(node.target, node.propertyName); | |
| 395 } else { | |
| 396 return new AccessSemantics.dynamic(node.propertyName, node.realTarget); | |
| 397 } | |
| 398 } | |
| 399 | |
| 400 /** | |
| 401 * Return the access semantics for [node]. | |
| 402 * | |
| 403 * Note: if [node] is the right hand side of a [PropertyAccess] or | |
| 404 * [PrefixedIdentifier], or the method name of a [MethodInvocation], the retur
n | |
| 405 * value is null, since the semantics are determined by the parent. In | |
| 406 * practice these cases should never arise because the parent will visit the | |
| 407 * parent node before visiting this one. | |
| 408 */ | |
| 409 @override | |
| 410 AccessSemantics visitSimpleIdentifier(SimpleIdentifier node) { | |
| 411 AstNode parent = node.parent; | |
| 412 if (node.inDeclarationContext()) { | |
| 413 // This identifier is a declaration, not a use. | |
| 414 return null; | |
| 415 } | |
| 416 if (parent is TypeName) { | |
| 417 // TODO(paulberry): do we need to handle this case? | |
| 418 return null; | |
| 419 } | |
| 420 if ((parent is PropertyAccess && parent.propertyName == node) || | |
| 421 (parent is PrefixedIdentifier && parent.identifier == node) || | |
| 422 (parent is MethodInvocation && parent.methodName == node)) { | |
| 423 // The access semantics are determined by the parent. | |
| 424 return null; | |
| 425 } | |
| 426 // TODO(paulberry): handle PrefixElement. | |
| 427 Element staticElement = node.staticElement; | |
| 428 if (staticElement is PropertyAccessorElement) { | |
| 429 if (staticElement.isSynthetic) { | |
| 430 if (staticElement.enclosingElement is CompilationUnitElement) { | |
| 431 return new AccessSemantics.staticField( | |
| 432 node, | |
| 433 staticElement.variable, | |
| 434 null); | |
| 435 } else if (staticElement.isStatic) { | |
| 436 return new AccessSemantics.staticField( | |
| 437 node, | |
| 438 staticElement.variable, | |
| 439 staticElement.enclosingElement); | |
| 440 } | |
| 441 } else { | |
| 442 if (staticElement.enclosingElement is CompilationUnitElement) { | |
| 443 return new AccessSemantics.staticProperty(node, staticElement, null); | |
| 444 } else if (staticElement.isStatic) { | |
| 445 return new AccessSemantics.staticProperty( | |
| 446 node, | |
| 447 staticElement, | |
| 448 staticElement.enclosingElement); | |
| 449 } | |
| 450 } | |
| 451 } else if (staticElement is LocalVariableElement) { | |
| 452 return new AccessSemantics.localVariable(node, staticElement); | |
| 453 } else if (staticElement is ParameterElement) { | |
| 454 return new AccessSemantics.parameter(node, staticElement); | |
| 455 } else if (staticElement is FunctionElement) { | |
| 456 if (staticElement.enclosingElement is CompilationUnitElement) { | |
| 457 return new AccessSemantics.staticMethod(node, staticElement, null); | |
| 458 } else { | |
| 459 return new AccessSemantics.localFunction(node, staticElement); | |
| 460 } | |
| 461 } else if (staticElement is MethodElement && staticElement.isStatic) { | |
| 462 return new AccessSemantics.staticMethod( | |
| 463 node, | |
| 464 staticElement, | |
| 465 staticElement.enclosingElement); | |
| 466 } else if (staticElement is TypeParameterElement) { | |
| 467 return new AccessSemantics.typeParameter(node, staticElement); | |
| 468 } else if (staticElement is ClassElement || | |
| 469 staticElement is FunctionTypeAliasElement || | |
| 470 staticElement is DynamicElementImpl) { | |
| 471 return new AccessSemantics.toplevelType(node, staticElement); | |
| 472 } | |
| 473 return new AccessSemantics.dynamic(node, null); | |
| 474 } | |
| 475 | |
| 476 /** | |
| 477 * Helper function for classifying an expression of type | |
| 478 * Identifier.SimpleIdentifier. | |
| 479 */ | |
| 480 AccessSemantics _classifyPrefixed(Identifier lhs, SimpleIdentifier rhs) { | |
| 481 Element lhsElement = lhs.staticElement; | |
| 482 Element rhsElement = rhs.staticElement; | |
| 483 if (lhsElement is PrefixElement) { | |
| 484 if (rhsElement is PropertyAccessorElement) { | |
| 485 if (rhsElement.isSynthetic) { | |
| 486 return new AccessSemantics.staticField( | |
| 487 rhs, | |
| 488 rhsElement.variable, | |
| 489 null); | |
| 490 } else { | |
| 491 return new AccessSemantics.staticProperty(rhs, rhsElement, null); | |
| 492 } | |
| 493 } else if (rhsElement is FunctionElement) { | |
| 494 return new AccessSemantics.staticMethod(rhs, rhsElement, null); | |
| 495 } else if (rhsElement is ClassElement || | |
| 496 rhsElement is FunctionTypeAliasElement) { | |
| 497 return new AccessSemantics.toplevelType(rhs, rhsElement); | |
| 498 } else { | |
| 499 return new AccessSemantics.dynamic(rhs, null); | |
| 500 } | |
| 501 } else if (lhsElement is ClassElement) { | |
| 502 if (rhsElement is PropertyAccessorElement && rhsElement.isSynthetic) { | |
| 503 return new AccessSemantics.staticField( | |
| 504 rhs, | |
| 505 rhsElement.variable, | |
| 506 lhsElement); | |
| 507 } else if (rhsElement is MethodElement) { | |
| 508 return new AccessSemantics.staticMethod(rhs, rhsElement, lhsElement); | |
| 509 } else { | |
| 510 return new AccessSemantics.staticProperty(rhs, rhsElement, lhsElement); | |
| 511 } | |
| 512 } else { | |
| 513 return new AccessSemantics.dynamic(rhs, lhs); | |
| 514 } | |
| 515 } | |
| 516 } | |
| OLD | NEW |