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