Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(266)

Side by Side Diff: lib/compiler/implementation/dart_backend/renamer.dart

Issue 10795064: dart2dart: Introduce Renamer that is used by renaming unparser (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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 */
8 class ConflictingRenamer implements Renamer {
9 final Compiler compiler;
10 final Map<Element, String> renamed;
11 final Set<String> usedTopLevelIdentifiers;
12 TreeElements contextElements;
13 Element context;
14
15 Map<Element, TreeElements> get resolvedElements() =>
16 compiler.enqueuer.resolution.resolvedElements;
17
18 ConflictingRenamer(this.compiler) :
19 renamed = new Map<Element, String>(),
20 usedTopLevelIdentifiers = new Set<String>();
21
22 void setContext(Element element) {
23 this.context = element;
24 contextElements = resolvedElements[element];
25 }
26
27 /**
28 * Renames method name. Returns [null] if no rename is needed.
29 */
30 String renameSendMethod(Send send) {
31 // Rename only if this Send is a function call.
32 if (contextElements[send] !== null && contextElements[send].isFunction()) {
33 return renameElement(contextElements[send]);
34 } else {
35 return null;
36 }
37 }
38
39 /**
40 * Renames type name for given type annotation.
41 * Should not touch type arguments.
42 */
43 String renameTypeName(TypeAnnotation typeAnnotation) {
44 if (contextElements === null
Anton Muhin 2012/07/23 17:01:27 please, comment what goes on here.
Roman 2012/07/23 17:38:33 Done.
45 || contextElements.getType(typeAnnotation) === null) {
46 // We have no info about this type from resolver.
47 // TODO(smok): Maybe resolver should have this information, fix if so.
48 if (context.isField()
49 && context.variables.computeType(compiler) !== null) {
50 // A field.
51 return renameType(context.variables.type);
52 } else {
53 return typeAnnotation.typeName.unparse();
54 }
55 }
56
57 // TODO(smok): Check if resolver can help us identifying factory
58 // constructors.
59 Type type = contextElements.getType(typeAnnotation);
60 if (typeAnnotation.typeName is Send
61 && typeAnnotation.typeName.receiver.source.slowToString()
62 == type.name.slowToString()) {
63 // Got factory invocation. Need to rename first part.
64 return "${renameType(type)}."
65 "${typeAnnotation.typeName.selector.source.slowToString()}";
66 }
67 return renameType(type);
68 }
69
70 String renameType(Type type) => renameElement(type.element);
71
72 /**
73 * Renames identifier. Returns [null] if no rename is needed.
74 */
75 String renameIdentifier(Identifier node) {
76 if (context.isGenerativeConstructor()) {
77 // This is either a factory constructor or simple one.
78 // TODO(smok): Check if resolver can help us identifying factory
79 // constructors.
80 var enclosingClass = context.getEnclosingClass();
81 if (node.token.slowToString() == context.name.slowToString()
82 || enclosingClass.name.slowToString() == node.token.slowToString()) {
83 return renameElement(enclosingClass);
84 }
85 }
86 if (context.isFunction() && context.cachedNode.name == node) {
87 return renameElement(context);
88 }
89 return null;
90 }
91
92 String renameElement(Element element) {
93 String originalName = element.name.slowToString();
94 if (element == null || element.getLibrary() == compiler.coreLibrary
95 || !element.isTopLevel()) {
96 return originalName;
97 }
98 if (renamed[element] !== null) {
99 return renamed[element];
100 }
101
102 // Not renamed and top element.
103 // TODO(smok): Make sure that the new name does not conflict with existing
104 // local identifiers.
105 String name = originalName;
106 while (usedTopLevelIdentifiers.contains(name)) {
107 name = "_$name";
108 }
109 usedTopLevelIdentifiers.add(name);
110 renamed[element] = name;
111 return name;
112 }
113 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698