|
| 1 | +// Copyright (c) 2021, 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/element/element.dart'; |
| 7 | +import 'package:analyzer/dart/element/visitor.dart'; |
| 8 | +import 'package:analyzer/src/dart/ast/ast.dart'; |
| 9 | +import 'package:analyzer/src/dart/element/element.dart'; |
| 10 | + |
| 11 | +/// Elements have references to AST nodes, for example initializers of constant |
| 12 | +/// variables. These nodes are attached to the whole compilation unit, and |
| 13 | +/// the whole token stream for the file. We don't want all this data after |
| 14 | +/// linking. So, we need to detach these nodes. |
| 15 | +void detachElementsFromNodes(LibraryElementImpl element) { |
| 16 | + element.accept(_Visitor()); |
| 17 | +} |
| 18 | + |
| 19 | +class _Visitor extends GeneralizingElementVisitor<void> { |
| 20 | + @override |
| 21 | + void visitClassElement(ClassElement element) { |
| 22 | + if (element is ClassElementImpl) { |
| 23 | + element.mixinInferenceCallback = null; |
| 24 | + } |
| 25 | + super.visitClassElement(element); |
| 26 | + } |
| 27 | + |
| 28 | + @override |
| 29 | + void visitConstructorElement(ConstructorElement element) { |
| 30 | + if (element is ConstructorElementImpl) { |
| 31 | + // Make a copy, so that it is not a NodeList. |
| 32 | + var initializers = element.constantInitializers.toList(); |
| 33 | + initializers.forEach(_detachNode); |
| 34 | + element.constantInitializers = initializers; |
| 35 | + } |
| 36 | + super.visitConstructorElement(element); |
| 37 | + } |
| 38 | + |
| 39 | + @override |
| 40 | + void visitElement(Element element) { |
| 41 | + for (var elementAnnotation in element.metadata) { |
| 42 | + _detachNode((elementAnnotation as ElementAnnotationImpl).annotationAst); |
| 43 | + } |
| 44 | + super.visitElement(element); |
| 45 | + } |
| 46 | + |
| 47 | + @override |
| 48 | + void visitParameterElement(ParameterElement element) { |
| 49 | + _detachConstVariable(element); |
| 50 | + super.visitParameterElement(element); |
| 51 | + } |
| 52 | + |
| 53 | + @override |
| 54 | + void visitPropertyInducingElement(PropertyInducingElement element) { |
| 55 | + if (element is PropertyInducingElementImpl) { |
| 56 | + element.typeInference = null; |
| 57 | + } |
| 58 | + _detachConstVariable(element); |
| 59 | + } |
| 60 | + |
| 61 | + void _detachConstVariable(Element element) { |
| 62 | + if (element is ConstVariableElement) { |
| 63 | + var initializer = element.constantInitializer; |
| 64 | + if (initializer is ExpressionImpl) { |
| 65 | + _detachNode(initializer); |
| 66 | + ConstantContextForExpressionImpl(initializer); |
| 67 | + } |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + void _detachNode(AstNode? node) { |
| 72 | + if (node is AstNodeImpl) { |
| 73 | + node.detachFromParent(); |
| 74 | + // Also detach from the token stream. |
| 75 | + node.beginToken.previous = null; |
| 76 | + node.endToken.next = null; |
| 77 | + } |
| 78 | + } |
| 79 | +} |
0 commit comments