Index: sdk/lib/_internal/compiler/implementation/mirror_renamer/renamer.dart |
diff --git a/sdk/lib/_internal/compiler/implementation/mirror_renamer/renamer.dart b/sdk/lib/_internal/compiler/implementation/mirror_renamer/renamer.dart |
index 003dda44768370e4daaa8994d6ab2ca587f32386..e6b3021e879a1ade673ce13e985cd445791b728f 100644 |
--- a/sdk/lib/_internal/compiler/implementation/mirror_renamer/renamer.dart |
+++ b/sdk/lib/_internal/compiler/implementation/mirror_renamer/renamer.dart |
@@ -5,28 +5,85 @@ |
part of mirror_renamer; |
class MirrorRenamer { |
- static const String MIRROR_HELPER_CLASS = 'MirrorHelper'; |
- static const String MIRROR_HELPER_GET_NAME_FUNCTION = 'getName'; |
- static const String MIRROR_HELPER_LIBRARY_NAME = 'mirror_helper.dart'; |
- static const String MIRROR_HELPER_LIBRARY_PREFIX = 'm'; |
- static const String MIRROR_HELPER_CLASS_FULLY_QUALIFIED_NAME = |
- '$MIRROR_HELPER_LIBRARY_PREFIX.$MIRROR_HELPER_CLASS'; |
- |
- static void handleStaticSend(Map<Node, String> renames, Element element, |
- Send node, Compiler compiler) { |
+ static const String MIRROR_HELPER_GET_NAME_FUNCTION = 'helperGetName'; |
+ static const String MIRROR_HELPER_LIBRARY_NAME = '_mirror_helper'; |
+ static const String MIRROR_HELPER_SYMBOLS_MAP_NAME = '_SYMBOLS'; |
+ |
+ 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.
|
+ List<Node> mirrorSystemGetNameNodes = <Node>[]; |
Johnni Winther
2013/08/23 09:25:19
Document this.
zarah
2013/08/23 16:16:37
Done.
|
+ /** |
+ * Initialized when the placeholderCollector collects the FunctionElement |
+ * backend.mirrorHelperGetNameFunction which represents the helperGetName |
+ * function in _mirror_helper. |
+ */ |
+ FunctionExpression mirrorHelperGetNameFunctionNode; |
+ Compiler compiler; |
+ DartBackend backend; |
+ |
+ MirrorRenamer(this.compiler, this.backend); |
+ |
+ void registerStaticSend(Element element, Send node) { |
if (element == compiler.mirrorSystemGetNameFunction) { |
- renames[node.selector] = MIRROR_HELPER_GET_NAME_FUNCTION; |
- renames[node.receiver] = MIRROR_HELPER_CLASS_FULLY_QUALIFIED_NAME; |
- } |
+ mirrorSystemGetNameNodes.add(node); |
+ } |
} |
- static void addMirrorHelperImport(Map<LibraryElement, String> imports) { |
- Uri mirrorHelperUri = new Uri(path: MIRROR_HELPER_LIBRARY_NAME); |
- // TODO(zarah): Remove this hack! LibraryElementX should not be created |
- // outside the library loader. When actual mirror helper library |
- // is created, change to load that. |
- LibraryElement mirrorHelperLib = new LibraryElementX( |
- new Script(mirrorHelperUri, null)); |
- imports.putIfAbsent(mirrorHelperLib, () => MIRROR_HELPER_LIBRARY_PREFIX); |
+ void registerHelperFunction(Element element, Node node) { |
+ if (element == backend.mirrorHelperGetNameFunction) { |
+ mirrorHelperGetNameFunctionNode = node; |
+ } |
+ } |
+ |
+ /** |
+ * Adds a toplevel node to the output containing a map from the mangled |
+ * to the unmangled names and replaces calls to MirrorSystem.getName() |
+ * with calls to the corresponding wrapper from _mirror_helper which has |
+ * been added during resolution. |
+ */ |
+ 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.
|
+ symbols = new Map<String, SourceString>(); |
+ String renameMapName; |
+ FunctionElement getNameElement; |
+ |
+ Node parse(String text) { |
+ Token tokens = compiler.scanner.tokenize(text); |
+ return compiler.parser.parseCompilationUnit(tokens); |
+ } |
+ |
+ // Add toplevel map containing all renames. |
+ for (Node node in renames.keys) { |
+ Identifier identifier = node.asIdentifier(); |
+ if (identifier != null) { |
+ if (identifier.source == |
+ 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
|
+ renameMapName = renames[node]; |
+ } else { |
+ symbols.putIfAbsent(renames[node], () => identifier.source); |
+ } |
+ } |
+ } |
+ |
+ StringBuffer sb = new StringBuffer('const $renameMapName = ' |
+ 'const<String, SourceString>{'); |
+ bool first = true; |
+ for (String mangledName in symbols.keys) { |
+ if (!first) { |
+ sb.write(','); |
+ } else { |
+ first = false; |
+ } |
+ sb.write("'$mangledName' : '${symbols[mangledName]}'"); |
+ } |
+ sb.write('};'); |
+ topLevelNodes.add(parse(sb.toString())); |
+ |
+ // Replace calls to Mirrorsystem.getName with calls to helper function. |
+ getNameElement = backend.mirrorHelperLibrary.find(const |
+ SourceString(MIRROR_HELPER_GET_NAME_FUNCTION)); |
+ Node getNameNode = getNameElement.parseNode(compiler); |
+ mirrorSystemGetNameNodes.forEach((node) { |
+ renames[node.selector] = renames[mirrorHelperGetNameFunctionNode.name]; |
+ renames[node.receiver] = ''; |
+ }); |
} |
-} |
+} |