| OLD | NEW |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, 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 import 'package:compiler/src/elements/elements.dart'; | 5 import 'package:compiler/src/elements/elements.dart'; |
| 6 import 'package:compiler/src/resolution/access_semantics.dart'; | 6 import 'package:compiler/src/resolution/access_semantics.dart'; |
| 7 import 'package:compiler/src/resolution/send_structure.dart'; | 7 import 'package:compiler/src/resolution/send_structure.dart'; |
| 8 import 'package:compiler/src/resolution/tree_elements.dart'; | 8 import 'package:compiler/src/resolution/tree_elements.dart'; |
| 9 import 'package:compiler/src/tree/nodes.dart' as ast; | 9 import 'package:compiler/src/tree/nodes.dart' as ast; |
| 10 import 'package:kernel/ast.dart' as ir; |
| 10 | 11 |
| 11 enum IdKind { element, node } | 12 enum IdKind { element, node } |
| 12 | 13 |
| 13 /// Id for a code point or element with type inference information. | 14 /// Id for a code point or element with type inference information. |
| 14 abstract class Id { | 15 abstract class Id { |
| 15 IdKind get kind; | 16 IdKind get kind; |
| 16 } | 17 } |
| 17 | 18 |
| 18 /// Id for an element with type inference information. | 19 /// Id for an element with type inference information. |
| 19 // TODO(johnniwinther): Support local variables, functions and parameters. | 20 // TODO(johnniwinther): Support local variables, functions and parameters. |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 switch (sendStructure.kind) { | 95 switch (sendStructure.kind) { |
| 95 case SendStructureKind.GET: | 96 case SendStructureKind.GET: |
| 96 case SendStructureKind.INVOKE: | 97 case SendStructureKind.INVOKE: |
| 97 case SendStructureKind.INCOMPATIBLE_INVOKE: | 98 case SendStructureKind.INCOMPATIBLE_INVOKE: |
| 98 return computeAccessId(node, sendStructure.semantics); | 99 return computeAccessId(node, sendStructure.semantics); |
| 99 default: | 100 default: |
| 100 return new NodeId(node.getBeginToken().charOffset); | 101 return new NodeId(node.getBeginToken().charOffset); |
| 101 } | 102 } |
| 102 } | 103 } |
| 103 } | 104 } |
| 105 |
| 106 /// Visitor that finds the AST node or element corresponding to an [Id]. |
| 107 class AstIdFinder extends ast.Visitor with AstEnumeratorMixin { |
| 108 Id soughtId; |
| 109 var /*AstElement|ast.Node*/ found; |
| 110 final TreeElements elements; |
| 111 |
| 112 AstIdFinder(this.elements); |
| 113 |
| 114 /// Visits the subtree of [root] returns the [ast.Node] or [AstElement] |
| 115 /// corresponding to [id]. |
| 116 /*AstElement|ast.Node*/ find(ast.Node root, Id id) { |
| 117 soughtId = id; |
| 118 root.accept(this); |
| 119 var result = found; |
| 120 found = null; |
| 121 return result; |
| 122 } |
| 123 |
| 124 visit(ast.Node node) { |
| 125 if (found == null) { |
| 126 node?.accept(this); |
| 127 } |
| 128 } |
| 129 |
| 130 visitNode(ast.Node node) { |
| 131 if (found == null) { |
| 132 node.visitChildren(this); |
| 133 } |
| 134 } |
| 135 |
| 136 visitSend(ast.Send node) { |
| 137 if (found == null) { |
| 138 visitNode(node); |
| 139 Id id = computeNodeId(node); |
| 140 if (id == soughtId) { |
| 141 found = node; |
| 142 } |
| 143 } |
| 144 } |
| 145 |
| 146 visitVariableDefinitions(ast.VariableDefinitions node) { |
| 147 if (found == null) { |
| 148 for (ast.Node child in node.definitions) { |
| 149 AstElement element = elements[child]; |
| 150 if (element != null) { |
| 151 Id id = computeElementId(element); |
| 152 if (id == soughtId) { |
| 153 found = element; |
| 154 return; |
| 155 } |
| 156 } |
| 157 } |
| 158 visitNode(node); |
| 159 } |
| 160 } |
| 161 |
| 162 visitFunctionExpression(ast.FunctionExpression node) { |
| 163 if (found == null) { |
| 164 AstElement element = elements.getFunctionDefinition(node); |
| 165 if (element != null) { |
| 166 Id id = computeElementId(element); |
| 167 if (id == soughtId) { |
| 168 found = element; |
| 169 return; |
| 170 } |
| 171 } |
| 172 visitNode(node); |
| 173 } |
| 174 } |
| 175 } |
| 176 |
| 177 abstract class IrEnumeratorMixin { |
| 178 Id computeElementId(ir.Member node) { |
| 179 String className; |
| 180 if (node.enclosingClass != null) { |
| 181 className = node.enclosingClass.name; |
| 182 } |
| 183 String memberName = node.name.name; |
| 184 if (node is ir.Procedure && node.kind == ir.ProcedureKind.Setter) { |
| 185 memberName += '='; |
| 186 } |
| 187 return new ElementId.internal(memberName, className); |
| 188 } |
| 189 |
| 190 Id computeNodeId(ir.Node node) { |
| 191 if (node is ir.MethodInvocation) { |
| 192 assert(node.fileOffset != ir.TreeNode.noOffset); |
| 193 return new NodeId(node.fileOffset); |
| 194 } else if (node is ir.PropertyGet) { |
| 195 assert(node.fileOffset != ir.TreeNode.noOffset); |
| 196 return new NodeId(node.fileOffset); |
| 197 } |
| 198 return null; |
| 199 } |
| 200 } |
| 201 |
| 202 /// Visitor that finds the IR node corresponding to an [Id]. |
| 203 class IrIdFinder extends ir.Visitor with IrEnumeratorMixin { |
| 204 Id soughtId; |
| 205 ir.Node found; |
| 206 |
| 207 /// Visits the subtree of [root] returns the [ir.Node] corresponding to [id]. |
| 208 ir.Node find(ir.Node root, Id id) { |
| 209 soughtId = id; |
| 210 root.accept(this); |
| 211 var result = found; |
| 212 found = null; |
| 213 return result; |
| 214 } |
| 215 |
| 216 defaultNode(ir.Node node) { |
| 217 if (found == null) { |
| 218 Id id = computeNodeId(node); |
| 219 if (id == soughtId) { |
| 220 found = node; |
| 221 return; |
| 222 } |
| 223 node.visitChildren(this); |
| 224 } |
| 225 } |
| 226 |
| 227 defaultMember(ir.Member node) { |
| 228 if (found == null) { |
| 229 Id id = computeElementId(node); |
| 230 if (id == soughtId) { |
| 231 found = node; |
| 232 return; |
| 233 } |
| 234 defaultNode(node); |
| 235 } |
| 236 } |
| 237 } |
| OLD | NEW |