| 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_GET_NAME_FUNCTION = 'helperGetName'; | 8 static const String MIRROR_HELPER_GET_NAME_FUNCTION = 'helperGetName'; |
| 9 static const String MIRROR_HELPER_LIBRARY_NAME = '_mirror_helper'; | 9 static const String MIRROR_HELPER_LIBRARY_NAME = '_mirror_helper'; |
| 10 static const String MIRROR_HELPER_SYMBOLS_MAP_NAME = '_SYMBOLS'; | 10 static const String MIRROR_HELPER_SYMBOLS_MAP_NAME = '_SYMBOLS'; |
| (...skipping 29 matching lines...) Expand all Loading... |
| 40 } | 40 } |
| 41 | 41 |
| 42 /** | 42 /** |
| 43 * Adds a toplevel node to the output containing a map from the mangled | 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() | 44 * to the unmangled names and replaces calls to MirrorSystem.getName() |
| 45 * with calls to the corresponding wrapper from _mirror_helper which has | 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 | 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 | 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. | 48 * contain all the toplevel ast nodes that will be emitted in the output. |
| 49 */ | 49 */ |
| 50 void addRenames(Map<Node, String> renames, List<Node> topLevelNodes) { | 50 void addRenames(Map<Node, String> renames, List<Node> topLevelNodes, |
| 51 PlaceholderCollector placeholderCollector) { |
| 51 | 52 |
| 52 Node parse(String text) { | 53 Node parse(String text) { |
| 53 Token tokens = compiler.scanner.tokenize(text); | 54 Token tokens = compiler.scanner.tokenize(text); |
| 54 return compiler.parser.parseCompilationUnit(tokens); | 55 return compiler.parser.parseCompilationUnit(tokens); |
| 55 } | 56 } |
| 56 | 57 |
| 57 // Add toplevel map containing all renames. | 58 // Add toplevel map containing all renames of members. |
| 58 symbols = new Map<String, SourceString>(); | 59 symbols = new Map<String, SourceString>(); |
| 59 for (Node node in renames.keys) { | 60 for (Set<Identifier> s in placeholderCollector.memberPlaceholders.values) { |
| 60 Identifier identifier = node.asIdentifier(); | 61 // All members in a set have the same name so we only need to look at one. |
| 61 if (identifier != null) { | 62 Identifier sampleNode = s.first; |
| 62 symbols.putIfAbsent(renames[node], () => identifier.source); | 63 symbols.putIfAbsent(renames[sampleNode], () => sampleNode.source); |
| 63 } | |
| 64 } | 64 } |
| 65 | 65 |
| 66 Identifier symbolsMapIdentifier = | 66 Identifier symbolsMapIdentifier = |
| 67 mirrorHelperSymbolsMapNode.definitions.nodes.head.asSend().selector; | 67 mirrorHelperSymbolsMapNode.definitions.nodes.head.asSend().selector; |
| 68 assert(symbolsMapIdentifier != null); | 68 assert(symbolsMapIdentifier != null); |
| 69 topLevelNodes.remove(mirrorHelperSymbolsMapNode); | 69 topLevelNodes.remove(mirrorHelperSymbolsMapNode); |
| 70 | 70 |
| 71 StringBuffer sb = new StringBuffer( | 71 StringBuffer sb = new StringBuffer( |
| 72 'const ${renames[symbolsMapIdentifier]} = const<String,SourceString>{'); | 72 'const ${renames[symbolsMapIdentifier]} = const<String,SourceString>{'); |
| 73 bool first = true; | 73 bool first = true; |
| 74 for (String mangledName in symbols.keys) { | 74 for (String mangledName in symbols.keys) { |
| 75 if (!first) { | 75 if (!first) { |
| 76 sb.write(','); | 76 sb.write(','); |
| 77 } else { | 77 } else { |
| 78 first = false; | 78 first = false; |
| 79 } | 79 } |
| 80 sb.write("'$mangledName' : '${symbols[mangledName]}'"); | 80 sb.write("'$mangledName' : '${symbols[mangledName]}'"); |
| 81 } | 81 } |
| 82 sb.write('};'); | 82 sb.write('};'); |
| 83 topLevelNodes.add(parse(sb.toString())); | 83 topLevelNodes.add(parse(sb.toString())); |
| 84 | 84 |
| 85 // Replace calls to Mirrorsystem.getName with calls to helper function. | 85 // Replace calls to Mirrorsystem.getName with calls to helper function. |
| 86 mirrorSystemGetNameNodes.forEach((node) { | 86 mirrorSystemGetNameNodes.forEach((node) { |
| 87 renames[node.selector] = renames[mirrorHelperGetNameFunctionNode.name]; | 87 renames[node.selector] = renames[mirrorHelperGetNameFunctionNode.name]; |
| 88 renames[node.receiver] = ''; | 88 renames[node.receiver] = ''; |
| 89 }); | 89 }); |
| 90 } | 90 } |
| 91 } | 91 } |
| OLD | NEW |