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 /// Maps mangled name to original name. |
16 Send node, Compiler compiler) { | 13 Map<String, SourceString> symbols = new Map<String, SourceString>(); |
14 /// Contains all occurrencs of MirrorSystem.getName() calls in the user code. | |
15 List<Node> mirrorSystemGetNameNodes = <Node>[]; | |
16 /** | |
17 * Initialized when the placeholderCollector collects the FunctionElement | |
18 * backend.mirrorHelperGetNameFunction which represents the helperGetName | |
19 * function in _mirror_helper. | |
20 */ | |
21 FunctionExpression mirrorHelperGetNameFunctionNode; | |
22 VariableDefinitions mirrorHelperSymbolsMapNode; | |
23 Compiler compiler; | |
24 DartBackend backend; | |
25 | |
26 MirrorRenamer(this.compiler, this.backend); | |
27 | |
28 void registerStaticSend(Element element, Send node) { | |
17 if (element == compiler.mirrorSystemGetNameFunction) { | 29 if (element == compiler.mirrorSystemGetNameFunction) { |
18 renames[node.selector] = MIRROR_HELPER_GET_NAME_FUNCTION; | 30 mirrorSystemGetNameNodes.add(node); |
19 renames[node.receiver] = MIRROR_HELPER_CLASS_FULLY_QUALIFIED_NAME; | 31 } |
20 } | |
21 } | 32 } |
22 | 33 |
23 static void addMirrorHelperImport(Map<LibraryElement, String> imports) { | 34 void registerHelperElement(Element element, Node node) { |
24 Uri mirrorHelperUri = new Uri(path: MIRROR_HELPER_LIBRARY_NAME); | 35 if (element == backend.mirrorHelperGetNameFunction) { |
25 // TODO(zarah): Remove this hack! LibraryElementX should not be created | 36 mirrorHelperGetNameFunctionNode = node; |
26 // outside the library loader. When actual mirror helper library | 37 } else if (element == backend.mirrorHelperSymbolsMap) { |
27 // is created, change to load that. | 38 mirrorHelperSymbolsMapNode = node; |
28 LibraryElement mirrorHelperLib = new LibraryElementX( | 39 } |
29 new Script(mirrorHelperUri, null)); | |
30 imports.putIfAbsent(mirrorHelperLib, () => MIRROR_HELPER_LIBRARY_PREFIX); | |
31 } | 40 } |
32 } | 41 |
42 /** | |
43 * Adds a toplevel node to the output containing a map from the mangled | |
44 * to the unmangled names and replaces calls to MirrorSystem.getName() | |
45 * with calls to the corresponding wrapper from _mirror_helper which has | |
46 * been added during resolution. [renames] is assumed to map nodes in user | |
47 * code to mangled names appearing in output code, and [topLevelNodes] should | |
48 * contain all the toplevel ast nodes that will be emitted in the output. | |
49 */ | |
50 void addRenames(Map<Node, String> renames, List<Node> topLevelNodes) { | |
51 | |
52 Node parse(String text) { | |
53 Token tokens = compiler.scanner.tokenize(text); | |
54 return compiler.parser.parseCompilationUnit(tokens); | |
55 } | |
56 | |
57 // Add toplevel map containing all renames. | |
58 symbols = new Map<String, SourceString>(); | |
59 for (Node node in renames.keys) { | |
60 Identifier identifier = node.asIdentifier(); | |
61 if (identifier != null) { | |
62 symbols.putIfAbsent(renames[node], () => identifier.source); | |
63 } | |
64 } | |
65 | |
66 Identifier symbolsMapIdentifier = | |
Johnni Winther
2013/08/26 07:38:27
Assert that [symbolsMapIdentifer] is not null.
zarah
2013/08/28 10:19:03
Done.
| |
67 mirrorHelperSymbolsMapNode.definitions.nodes.head.asSend().selector; | |
68 topLevelNodes.remove(mirrorHelperSymbolsMapNode); | |
69 | |
70 StringBuffer sb = new StringBuffer( | |
71 'const ${renames[symbolsMapIdentifier]} = const<String,SourceString>{'); | |
72 bool first = true; | |
73 for (String mangledName in symbols.keys) { | |
74 if (!first) { | |
75 sb.write(','); | |
76 } else { | |
77 first = false; | |
78 } | |
79 sb.write("'$mangledName' : '${symbols[mangledName]}'"); | |
80 } | |
81 sb.write('};'); | |
82 topLevelNodes.add(parse(sb.toString())); | |
83 | |
84 // Replace calls to Mirrorsystem.getName with calls to helper function. | |
85 mirrorSystemGetNameNodes.forEach((node) { | |
86 renames[node.selector] = renames[mirrorHelperGetNameFunctionNode.name]; | |
87 renames[node.receiver] = ''; | |
88 }); | |
89 } | |
90 } | |
OLD | NEW |