Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 part of mirror_renamer; | 5 part of mirror_renamer; |
| 6 | 6 |
| 7 class MirrorRenamer { | 7 class MirrorRenamer { |
| 8 static const String MIRROR_HELPER_CLASS = 'MirrorHelper'; | 8 static const String MIRROR_HELPER_GET_NAME_FUNCTION = 'helperGetName'; |
| 9 static const String MIRROR_HELPER_GET_NAME_FUNCTION = 'getName'; | 9 static const String MIRROR_HELPER_LIBRARY_NAME = '_mirror_helper'; |
| 10 static const String MIRROR_HELPER_LIBRARY_NAME = 'mirror_helper.dart'; | 10 static const String MIRROR_HELPER_SYMBOLS_MAP_NAME = '_SYMBOLS'; |
| 11 static const String MIRROR_HELPER_LIBRARY_PREFIX = 'm'; | |
| 12 static const String MIRROR_HELPER_CLASS_FULLY_QUALIFIED_NAME = | |
| 13 '$MIRROR_HELPER_LIBRARY_PREFIX.$MIRROR_HELPER_CLASS'; | |
| 14 | 11 |
| 15 static void handleStaticSend(Map<Node, String> renames, Element element, | 12 static Map<String, String> inverseRenames = new Map<String, String>(); |
|
ahe
2013/08/19 15:34:06
Inverse of what?
ahe
2013/08/19 15:34:06
No static state please.
zarah
2013/08/20 14:08:17
Done.
zarah
2013/08/20 14:08:17
Inverse of the renames. Changed the name to symbol
| |
| 16 Send node, Compiler compiler) { | 13 static List<Node> mirrorSystemGetNameNodes = <Node>[]; |
|
ahe
2013/08/19 15:34:06
Document.
zarah
2013/08/20 14:08:17
Done.
| |
| 14 static FunctionExpression mirrorHelperFunctionNode; | |
|
ahe
2013/08/19 15:34:06
Name too general?
zarah
2013/08/20 14:08:17
Done.
| |
| 15 | |
| 16 static void registerStaticSend(Element element, Send node, | |
| 17 Compiler compiler) { | |
| 17 if (element == compiler.mirrorSystemGetNameFunction) { | 18 if (element == compiler.mirrorSystemGetNameFunction) { |
| 18 renames[node.selector] = MIRROR_HELPER_GET_NAME_FUNCTION; | 19 mirrorSystemGetNameNodes.add(node); |
| 19 renames[node.receiver] = MIRROR_HELPER_CLASS_FULLY_QUALIFIED_NAME; | 20 } |
| 20 } | |
| 21 } | 21 } |
| 22 | 22 |
| 23 static void addMirrorHelperImport(Map<LibraryElement, String> imports) { | 23 static void registerHelperFunction(Element element, Node node, |
| 24 Uri mirrorHelperUri = new Uri(path: MIRROR_HELPER_LIBRARY_NAME); | 24 Compiler compiler) { |
| 25 // TODO(zarah): Remove this hack! LibraryElementX should not be created | 25 if (element == compiler.mirrorHelperFunction) { |
| 26 // outside the library loader. When actual mirror helper library | 26 mirrorHelperFunctionNode = node; |
| 27 // is created, change to load that. | 27 } |
| 28 LibraryElement mirrorHelperLib = new LibraryElementX( | |
| 29 new Script(mirrorHelperUri, null)); | |
| 30 imports.putIfAbsent(mirrorHelperLib, () => MIRROR_HELPER_LIBRARY_PREFIX); | |
| 31 } | 28 } |
| 32 } | 29 |
| 30 static void addRenames(Map<Node, String> renames, List<Node> topLevelNodes, | |
|
ahe
2013/08/19 15:34:06
Document.
zarah
2013/08/20 14:08:17
Done.
| |
| 31 Compiler compiler) { | |
| 32 inverseRenames = new Map<String, String>(); | |
| 33 String renameMapName; | |
| 34 FunctionElement getNameElement; | |
| 35 | |
| 36 Node parse(String text) { | |
| 37 Token tokens = compiler.scanner.tokenize(text); | |
| 38 return compiler.parser.parseCompilationUnit(tokens); | |
| 39 } | |
| 40 | |
| 41 //Add toplevel map containing all renames. | |
|
ahe
2013/08/19 15:34:06
Add space after //.
zarah
2013/08/20 14:08:17
Done.
| |
| 42 for (Node node in renames.keys) { | |
| 43 if (node is Identifier) { | |
|
ahe
2013/08/19 15:34:06
Identifier identifier = node.asIdentifier();
if (n
zarah
2013/08/20 14:08:17
Done.
| |
| 44 var stringValue = node.asIdentifier().source.stringValue; | |
| 45 if (stringValue == MIRROR_HELPER_SYMBOLS_MAP_NAME) { | |
|
ahe
2013/08/19 15:34:06
Accessing stringValue in general won't work. See B
zarah
2013/08/20 14:08:17
Done.
| |
| 46 renameMapName = renames[node]; | |
| 47 } else if (stringValue != renames[node]) { | |
|
ahe
2013/08/19 15:34:06
Could you put SourceStrings in the 'renames' map?
zarah
2013/08/20 14:08:17
Not really, since it is used in renamePlaceHolders
| |
| 48 inverseRenames.putIfAbsent(renames[node], () => stringValue); | |
| 49 } | |
| 50 } | |
| 51 } | |
| 52 | |
| 53 StringBuffer sb = new StringBuffer('const Map<String, String> ' | |
| 54 '$renameMapName = const<String, String>{'); | |
| 55 bool first = true; | |
| 56 for (String mangledName in inverseRenames.keys) { | |
| 57 if (!first) { | |
| 58 sb.write(','); | |
| 59 } else { | |
| 60 first = false; | |
| 61 } | |
| 62 sb.write("'$mangledName' : '${inverseRenames[mangledName]}'"); | |
| 63 } | |
| 64 sb.write('};'); | |
| 65 topLevelNodes.add(parse(sb.toString())); | |
| 66 | |
| 67 //Replace calls to Mirrorsystem.getName with calls to helper function. | |
|
ahe
2013/08/19 15:34:06
Add space after //.
zarah
2013/08/20 14:08:17
Done.
| |
| 68 getNameElement = compiler.mirrorHelperLibrary.find(const | |
| 69 SourceString(MIRROR_HELPER_GET_NAME_FUNCTION)); | |
| 70 Node getNameNode = getNameElement.parseNode(compiler); | |
| 71 mirrorSystemGetNameNodes.forEach((node) { | |
| 72 renames[node.selector] = renames[mirrorHelperFunctionNode.name]; | |
| 73 renames[node.receiver] = ''; | |
| 74 }); | |
| 75 } | |
| 76 } | |
| OLD | NEW |