blob: f3ab5fd68a6275a07d992fb5d1f14a4029468932 [file] [log] [blame] [edit]
// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
/// Defines static information collected by the type checker and used later by
/// emitters to generate code.
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/src/dart/ast/ast.dart';
import 'package:analyzer/src/dart/ast/utilities.dart';
import 'package:analyzer/src/generated/parser.dart';
import 'utils.dart' as utils;
import 'package:analyzer/src/task/strong/info.dart';
export 'package:analyzer/src/task/strong/info.dart';
/// Represents a summary of the results collected by running the program
/// checker.
class CheckerResults {
final List<LibraryInfo> libraries;
final bool failure;
CheckerResults(this.libraries, this.failure);
}
/// Computed information about each library.
class LibraryInfo {
/// Canonical name of the library. This is unfortunately not derived from the
/// library directive as it doesn't have any meaningful rules enforced.
/// Instead, this is inferred from the path to the file defining the library.
final String name;
/// Corresponding analyzer element.
final LibraryElement library;
LibraryInfo(library)
: library = library,
name = utils.canonicalLibraryName(library);
}
class LibraryUnit {
final CompilationUnit library;
final List<CompilationUnit> parts;
LibraryUnit(this.library, this.parts);
Iterable<CompilationUnit> get libraryThenParts sync* {
yield library;
yield* parts;
}
Iterable<CompilationUnit> get partsThenLibrary sync* {
yield* parts;
yield library;
}
/// Creates a clone of this library's AST.
LibraryUnit clone() {
return new LibraryUnit(
_cloneUnit(library), parts.map(_cloneUnit).toList(growable: false));
}
static CompilationUnit _cloneUnit(CompilationUnit oldNode) {
var newNode = oldNode.accept(new _AstCloner());
ResolutionCopier.copyResolutionData(oldNode, newNode);
return newNode;
}
}
class _AstCloner extends AstCloner {
void _cloneProperties(AstNode clone, AstNode node) {
if (clone != null) {
CoercionInfo.set(clone, CoercionInfo.get(node));
DynamicInvoke.set(clone, DynamicInvoke.get(node));
}
}
@override
AstNode cloneNode(AstNode node) {
var clone = super.cloneNode(node);
_cloneProperties(clone, node);
return clone;
}
@override
List cloneNodeList(List list) {
var clone = super.cloneNodeList(list);
for (int i = 0, len = list.length; i < len; i++) {
_cloneProperties(clone[i], list[i]);
}
return clone;
}
// TODO(jmesserly): ResolutionCopier is not copying this yet.
@override
BlockFunctionBody visitBlockFunctionBody(BlockFunctionBody node) {
var clone = super.visitBlockFunctionBody(node);
(clone as FunctionBodyImpl).localVariableInfo =
(node as FunctionBodyImpl).localVariableInfo;
return clone;
}
@override
ExpressionFunctionBody visitExpressionFunctionBody(
ExpressionFunctionBody node) {
var clone = super.visitExpressionFunctionBody(node);
(clone as FunctionBodyImpl).localVariableInfo =
(node as FunctionBodyImpl).localVariableInfo;
return clone;
}
}