|
| 1 | +// Copyright (c) 2019, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 | + |
| 5 | +import 'package:analyzer/dart/ast/ast.dart'; |
| 6 | +import 'package:analyzer/dart/ast/visitor.dart'; |
| 7 | +import 'package:front_end/src/testing/id.dart' |
| 8 | + show ActualData, DataRegistry, Id, IdKind, NodeId; |
| 9 | + |
| 10 | +/// Abstract IR visitor for computing data corresponding to a node or element, |
| 11 | +/// and record it with a generic [Id] |
| 12 | +/// TODO(paulberry): if I try to extend GeneralizingAstVisitor<void>, the VM |
| 13 | +/// crashes. |
| 14 | +abstract class AstDataExtractor<T> extends GeneralizingAstVisitor<dynamic> |
| 15 | + with DataRegistry<T> { |
| 16 | + final Uri uri; |
| 17 | + |
| 18 | + @override |
| 19 | + final Map<Id, ActualData<T>> actualMap; |
| 20 | + |
| 21 | + AstDataExtractor(this.uri, this.actualMap); |
| 22 | + |
| 23 | + NodeId computeDefaultNodeId(AstNode node) => |
| 24 | + NodeId(_nodeOffset(node), IdKind.node); |
| 25 | + |
| 26 | + void computeForExpression(Expression node, NodeId id) { |
| 27 | + if (id == null) return; |
| 28 | + T value = computeNodeValue(id, node); |
| 29 | + registerValue(uri, node.offset, id, value, node); |
| 30 | + } |
| 31 | + |
| 32 | + void computeForFunctionBody(FunctionBody node, NodeId id) { |
| 33 | + if (id == null) return; |
| 34 | + T value = computeNodeValue(id, node); |
| 35 | + registerValue(uri, node.offset, id, value, node); |
| 36 | + } |
| 37 | + |
| 38 | + void computeForStatement(Statement node, NodeId id) { |
| 39 | + if (id == null) return; |
| 40 | + T value = computeNodeValue(id, node); |
| 41 | + registerValue(uri, node.offset, id, value, node); |
| 42 | + } |
| 43 | + |
| 44 | + /// Implement this to compute the data corresponding to [node]. |
| 45 | + /// |
| 46 | + /// If `null` is returned, [node] has no associated data. |
| 47 | + T computeNodeValue(Id id, AstNode node); |
| 48 | + |
| 49 | + NodeId createFunctionBodyId(FunctionBody node) => |
| 50 | + NodeId(_nodeOffset(node), IdKind.functionBody); |
| 51 | + |
| 52 | + NodeId createStatementId(Statement node) => |
| 53 | + NodeId(_nodeOffset(node), IdKind.statement); |
| 54 | + |
| 55 | + @override |
| 56 | + void fail(String message) { |
| 57 | + throw _Failure(message); |
| 58 | + } |
| 59 | + |
| 60 | + @override |
| 61 | + void report(Uri uri, int offset, String message) { |
| 62 | + // TODO(paulberry): find a way to print the error more nicely. |
| 63 | + print('$uri:$offset: $message'); |
| 64 | + } |
| 65 | + |
| 66 | + void run(CompilationUnit unit) { |
| 67 | + unit.accept(this); |
| 68 | + } |
| 69 | + |
| 70 | + @override |
| 71 | + visitExpression(Expression node) { |
| 72 | + computeForExpression(node, computeDefaultNodeId(node)); |
| 73 | + super.visitExpression(node); |
| 74 | + } |
| 75 | + |
| 76 | + @override |
| 77 | + visitFunctionBody(FunctionBody node) { |
| 78 | + computeForFunctionBody(node, createFunctionBodyId(node)); |
| 79 | + super.visitFunctionBody(node); |
| 80 | + } |
| 81 | + |
| 82 | + @override |
| 83 | + visitStatement(Statement node) { |
| 84 | + computeForStatement(node, createStatementId(node)); |
| 85 | + super.visitStatement(node); |
| 86 | + } |
| 87 | + |
| 88 | + int _nodeOffset(AstNode node) { |
| 89 | + var offset = node.offset; |
| 90 | + assert(offset != null && offset >= 0, |
| 91 | + "No fileOffset on $node (${node.runtimeType})"); |
| 92 | + return offset; |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +class _Failure implements Exception { |
| 97 | + final message; |
| 98 | + |
| 99 | + _Failure([this.message]); |
| 100 | + |
| 101 | + String toString() { |
| 102 | + if (message == null) return "Exception"; |
| 103 | + return "Exception: $message"; |
| 104 | + } |
| 105 | +} |
0 commit comments