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

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, 5 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 types, identifiers before printing them out.
7 */
8 interface Renamer {
Anton Muhin 2012/07/23 11:23:10 interfaces are going away, let's use just a single
Roman 2012/07/23 15:34:15 As discussed, created Renamer interface and NoRena
9 void setContext(Element element);
10
11 /**
12 * Renames type name for given type annotation.
13 * Should not touch type arguments.
14 */
15 String renameTypeName(TypeAnnotation type);
16
17 String renameType(Type type);
18
19 /**
20 * Renames method name.
21 */
22 String renameSendMethod(Send send);
23 String renameIdentifier(Identifier node);
24 }
25
26 /**
27 * Renames only top-level elements that would let to ambiguity if not renamed.
28 */
29 class ConflictingRenamer implements Renamer {
Anton Muhin 2012/07/23 11:23:10 Conflicting sounds weird, what do you try to conve
Roman 2012/07/23 15:34:15 That it renames only conflicting elements. I'm hap
30 final Compiler compiler;
31 Map<Element, String> renamed;
Anton Muhin 2012/07/23 11:23:10 final?
Roman 2012/07/23 15:34:15 Done.
32 Set<String> usedTopLevelIdentifiers;
Anton Muhin 2012/07/23 11:23:10 final?
Roman 2012/07/23 15:34:15 Done.
33 Map<Element, TreeElements> get resolvedElements() => compiler.enqueuer.resolut ion.resolvedElements;
Anton Muhin 2012/07/23 11:23:10 nit: please, move getter below the fields proper a
Roman 2012/07/23 15:34:15 Done.
34 TreeElements contextElements;
35 Element context;
36
37 ConflictingRenamer(this.compiler) :
38 renamed = new Map<Element, String>(),
39 usedTopLevelIdentifiers = new Set<String>();
40
41 void setContext(Element element) {
Anton Muhin 2012/07/23 11:23:10 I don't like this setContext thing, cannot we crea
Roman 2012/07/23 15:34:15 Unfortunately not. Renamer should have a state (li
42 this.context = element;
43 contextElements = resolvedElements[element];
44 }
45
46 String renameSendMethod(Send send) {
47 // Rename only if this Send is a function call.
48 if (contextElements[send] !== null && contextElements[send].isFunction()) {
Anton Muhin 2012/07/23 11:23:10 what about top-level variables?
Roman 2012/07/23 15:34:15 I don't quite understand. What about them?
Anton Muhin 2012/07/23 17:01:27 Shouldn't you rename them as well? On 2012/07/23
49 return renameElement(contextElements[send]);
50 } else {
51 return null;
52 }
53 }
54
55 String renameTypeName(TypeAnnotation typeAnnotation) {
56 if (contextElements == null
Anton Muhin 2012/07/23 11:23:10 nit: === null (here and below)
Anton Muhin 2012/07/23 11:23:10 how contextElements can be null?
Roman 2012/07/23 15:34:15 Done.
Roman 2012/07/23 15:34:15 this happens for class member fields.
Anton Muhin 2012/07/23 17:01:27 why? and, please, assert.
Roman 2012/07/23 17:38:32 I'm not sure why. Maybe resolver needs some fixes.
57 || contextElements.getType(typeAnnotation) == null) {
58 // We have no info about this type from resolver.
Anton Muhin 2012/07/23 11:23:10 when does it happen?
Roman 2012/07/23 15:34:15 this happens for class member fields. Maybe it's a
59 if (context.isField() && context.variables.computeType(compiler) !== null) {
60 // A field.
61 return renameType(context.variables.type);
62 } else {
63 return typeAnnotation.typeName.unparse();
64 }
65 }
66
67 Type type = contextElements.getType(typeAnnotation);
68 if (typeAnnotation.typeName is Send
Anton Muhin 2012/07/23 11:23:10 what about generic types?
Roman 2012/07/23 15:34:15 Generics are in type arguments. Here we care only
Anton Muhin 2012/07/23 17:01:27 Sorry, do you process generics correctly or not?
Roman 2012/07/23 17:38:33 Yes.
69 && typeAnnotation.typeName.receiver.source.slowToString() == type.name.s lowToString()) {
Anton Muhin 2012/07/23 11:23:10 what's the case when typeName is Send, but receive
Roman 2012/07/23 15:34:15 For example when we have a method that returns "my
70 // Got factory invocation. Need to rename first part.
71 return "${renameType(type)}.${typeAnnotation.typeName.selector.source.slow ToString()}";
72 }
73 String newName = renameType(type);
Anton Muhin 2012/07/23 11:23:10 nit: no need in newName local.
Roman 2012/07/23 15:34:15 Done.
74 return newName;
75 }
76
77 String renameType(Type type) {
Anton Muhin 2012/07/23 11:23:10 nit: => renameElement(type.element);
Roman 2012/07/23 15:34:15 Done.
78 Element typeElement = type.element;
79 return renameElement(typeElement);
80 }
81
82 String renameIdentifier(Identifier node) {
83 if (context.isGenerativeConstructor()) {
84 // This is either a factory constructor or simple one.
85 var enclosingClass = context.getEnclosingClass();
86 if (node.token.slowToString() == context.name.slowToString()
Anton Muhin 2012/07/23 11:23:10 it looks like you duplicate a work of a resolver:
Roman 2012/07/23 15:34:15 Added TODO as above. I'll try to investigate if re
87 || enclosingClass.name.slowToString() == node.token.slowToString()) {
88 return renameElement(enclosingClass);
89 }
90 }
91 if (context.isFunction() && context.cachedNode.name == node) {
92 return renameElement(context);
93 }
94 return null;
Anton Muhin 2012/07/23 11:23:10 again, what about top-level fields and privates?
Roman 2012/07/23 15:34:15 Again please elaborate :)
Anton Muhin 2012/07/23 17:01:27 :) I think we should rename both top level vars an
95 }
96
97 String renameElement(Element element) {
98 String originalName = element.name.slowToString();
99 if (element == null || element.getLibrary() == compiler.coreLibrary
100 || !element.isTopLevel()) {
101 return originalName;
102 }
103 if (renamed[element] !== null) {
104 return renamed[element];
105 }
106
107 // Not renamed and top element.
108 String name = originalName;
109 while (usedTopLevelIdentifiers.contains(name)) {
110 name = "_$name";
111 }
112 usedTopLevelIdentifiers.add(name);
113 renamed[element] = name;
114 return name;
115 }
116 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698