Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012, 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 * Renames only top-level elements that would let to ambiguity if not renamed. | |
| 7 * TODO(smok): Make sure that top-level fields and methods correctly renamed. | |
| 8 */ | |
| 9 class ConflictingRenamer extends Renamer { | |
| 10 final Compiler compiler; | |
| 11 final Map<Element, String> renamed; | |
| 12 final Set<String> usedTopLevelIdentifiers; | |
| 13 TreeElements contextElements; | |
| 14 Element context; | |
| 15 | |
| 16 Map<Element, TreeElements> get resolvedElements() => | |
| 17 compiler.enqueuer.resolution.resolvedElements; | |
| 18 | |
| 19 ConflictingRenamer(this.compiler) : | |
| 20 renamed = new Map<Element, String>(), | |
| 21 usedTopLevelIdentifiers = new Set<String>(); | |
| 22 | |
| 23 void setContext(Element element) { | |
| 24 this.context = element; | |
| 25 contextElements = resolvedElements[element]; | |
| 26 } | |
| 27 | |
| 28 /** | |
| 29 * Renames method name. Returns [null] if no rename is needed. | |
|
Anton Muhin
2012/07/23 17:58:54
nit: I don't think you need to duplicate documenta
Anton Muhin
2012/07/23 17:58:54
nit: I don't think you need to duplicate documenta
Roman
2012/07/24 08:22:49
Done.
| |
| 30 */ | |
| 31 String renameSendMethod(Send send) { | |
| 32 // Rename only if this Send is a function call. | |
| 33 if (contextElements[send] !== null && contextElements[send].isFunction()) { | |
| 34 return renameElement(contextElements[send]); | |
| 35 } else { | |
| 36 return null; | |
| 37 } | |
| 38 } | |
| 39 | |
| 40 /** | |
| 41 * Renames type name for given type annotation. | |
| 42 * Should not touch type arguments. | |
| 43 */ | |
| 44 String renameTypeName(TypeAnnotation typeAnnotation) { | |
| 45 if (contextElements === null | |
| 46 || contextElements.getType(typeAnnotation) === null) { | |
| 47 // We have no info about this type from resolver. | |
| 48 // This happens for class member fields. | |
| 49 // TODO(smok): Maybe resolver should have this information, fix if so. | |
| 50 if (context.isField() | |
| 51 && context.variables.computeType(compiler) !== null) { | |
| 52 // A field. | |
| 53 return renameType(context.variables.type); | |
| 54 } else { | |
| 55 return typeAnnotation.typeName.unparse(); | |
| 56 } | |
| 57 } | |
| 58 | |
| 59 // TODO(smok): Check if resolver can help us identifying factory | |
| 60 // constructors. | |
| 61 Type type = contextElements.getType(typeAnnotation); | |
| 62 if (typeAnnotation.typeName is Send | |
| 63 && typeAnnotation.typeName.receiver.source.slowToString() | |
| 64 == type.name.slowToString()) { | |
| 65 // Got factory invocation. Need to rename first part. | |
| 66 return "${renameType(type)}." | |
| 67 "${typeAnnotation.typeName.selector.source.slowToString()}"; | |
| 68 } | |
| 69 return renameType(type); | |
| 70 } | |
| 71 | |
| 72 String renameType(Type type) => renameElement(type.element); | |
| 73 | |
| 74 /** | |
| 75 * Renames identifier. Returns [null] if no rename is needed. | |
| 76 */ | |
| 77 String renameIdentifier(Identifier node) { | |
| 78 if (context.isGenerativeConstructor()) { | |
| 79 // This is either a factory constructor or simple one. | |
| 80 // TODO(smok): Check if resolver can help us identifying factory | |
| 81 // constructors. | |
| 82 var enclosingClass = context.getEnclosingClass(); | |
| 83 if (node.token.slowToString() == context.name.slowToString() | |
| 84 || enclosingClass.name.slowToString() == node.token.slowToString()) { | |
| 85 return renameElement(enclosingClass); | |
| 86 } | |
| 87 } | |
| 88 if (context.isFunction() && context.cachedNode.name == node) { | |
| 89 return renameElement(context); | |
| 90 } | |
| 91 return null; | |
| 92 } | |
| 93 | |
| 94 String renameElement(Element element) { | |
| 95 String originalName = element.name.slowToString(); | |
| 96 if (element == null || element.getLibrary() == compiler.coreLibrary | |
|
Anton Muhin
2012/07/23 17:58:54
nit: === null, but more importantly: you just did
Roman
2012/07/24 08:22:49
Correct, removed null check.
| |
| 97 || !element.isTopLevel()) { | |
| 98 return originalName; | |
| 99 } | |
| 100 if (renamed[element] !== null) { | |
| 101 return renamed[element]; | |
| 102 } | |
| 103 | |
| 104 // Not renamed and top element. | |
| 105 // TODO(smok): Make sure that the new name does not conflict with existing | |
| 106 // local identifiers. | |
| 107 String name = originalName; | |
| 108 while (usedTopLevelIdentifiers.contains(name)) { | |
| 109 name = "_$name"; | |
| 110 } | |
| 111 usedTopLevelIdentifiers.add(name); | |
| 112 renamed[element] = name; | |
| 113 return name; | |
| 114 } | |
| 115 } | |
| OLD | NEW |