Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(910)

Unified Diff: pkg/dev_compiler/lib/src/compiler/code_generator.dart

Issue 2551023005: Prepare for decoupling analyzer ASTs from element model. (Closed)
Patch Set: Address review comments Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: pkg/dev_compiler/lib/src/compiler/code_generator.dart
diff --git a/pkg/dev_compiler/lib/src/compiler/code_generator.dart b/pkg/dev_compiler/lib/src/compiler/code_generator.dart
index 3b9e89e35eb526627161c0b266b5cec3f2aa6237..cad4dc224ab8261d43544cdedb2b1ba4828f9e0f 100644
--- a/pkg/dev_compiler/lib/src/compiler/code_generator.dart
+++ b/pkg/dev_compiler/lib/src/compiler/code_generator.dart
@@ -10,6 +10,7 @@ import 'package:analyzer/analyzer.dart' hide ConstantEvaluator;
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/standard_ast_factory.dart';
import 'package:analyzer/dart/ast/token.dart' show Token, TokenType;
+import 'package:analyzer/dart/ast/standard_resolution_map.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/src/dart/ast/token.dart' show StringToken;
@@ -192,7 +193,10 @@ class CodeGenerator extends GeneralizingAstVisitor
List<int> _summarizeModule(List<CompilationUnit> units) {
if (!options.summarizeApi) return null;
- if (!units.any((u) => u.element.librarySource.isInSystemLibrary)) {
+ if (!units.any((u) => resolutionMap
+ .elementForCompilationUnit(u)
+ .librarySource
+ .isInSystemLibrary)) {
var sdk = context.sourceFactory.dartSdk;
summaryData.addBundle(
null,
@@ -234,7 +238,8 @@ class CodeGenerator extends GeneralizingAstVisitor
// Transform the AST to make coercions explicit.
compilationUnits = CoercionReifier.reify(compilationUnits);
- if (compilationUnits.any((u) => _isDartRuntime(u.element.library))) {
+ if (compilationUnits.any((u) =>
+ _isDartRuntime(resolutionMap.elementForCompilationUnit(u).library))) {
// Don't allow these to be renamed when we're building the SDK.
// There is JS code in dart:* that depends on their names.
_runtimeModule = new JS.Identifier('dart');
@@ -249,7 +254,7 @@ class CodeGenerator extends GeneralizingAstVisitor
// Initialize our library variables.
var items = <JS.ModuleItem>[];
for (var unit in compilationUnits) {
- var library = unit.element.library;
+ var library = resolutionMap.elementForCompilationUnit(unit).library;
if (unit.element != library.definingCompilationUnit) continue;
var libraryTemp = _isDartRuntime(library)
@@ -272,15 +277,19 @@ class CodeGenerator extends GeneralizingAstVisitor
var nodes = new HashMap<Element, AstNode>.identity();
var sdkBootstrappingFns = new List<FunctionElement>();
for (var unit in compilationUnits) {
- if (_isDartRuntime(unit.element.library)) {
- sdkBootstrappingFns.addAll(unit.element.functions);
+ if (_isDartRuntime(
+ resolutionMap.elementForCompilationUnit(unit).library)) {
+ sdkBootstrappingFns
+ .addAll(resolutionMap.elementForCompilationUnit(unit).functions);
}
_collectElements(unit, nodes);
}
_loader = new ElementLoader(nodes);
if (compilationUnits.isNotEmpty) {
_constants = new ConstFieldVisitor(context,
- dummySource: compilationUnits.first.element.source);
+ dummySource: resolutionMap
+ .elementForCompilationUnit(compilationUnits.first)
+ .source);
}
// Add implicit dart:core dependency so it is first.
@@ -728,7 +737,7 @@ class CodeGenerator extends GeneralizingAstVisitor
@override
JS.Statement visitClassDeclaration(ClassDeclaration node) {
- var classElem = node.element;
+ var classElem = resolutionMap.elementDeclaredByClassDeclaration(node);
// If this class is annotated with `@JS`, then there is nothing to emit.
if (findAnnotation(classElem, isPublicJSAnnotation) != null) return null;
@@ -1110,14 +1119,16 @@ class CodeGenerator extends GeneralizingAstVisitor
new JS.Identifier(
// TODO(ochafik): use a refactored _emitMemberName instead.
decl.name.name,
- type: emitTypeRef(decl.element.type)),
+ type: emitTypeRef(resolutionMap
+ .elementDeclaredByVariableDeclaration(decl)
+ .type)),
null))
.toList(growable: false));
}
@override
JS.Statement visitEnumDeclaration(EnumDeclaration node) {
- var element = node.element;
+ var element = resolutionMap.elementDeclaredByEnumDeclaration(node);
var type = element.type;
// Generate a class per section 13 of the spec.
@@ -1288,7 +1299,7 @@ class CodeGenerator extends GeneralizingAstVisitor
List<FieldDeclaration> fields,
List<ClassElement> superclasses,
Map<FieldElement, JS.TemporaryId> virtualFields) {
- var element = node.element;
+ var element = resolutionMap.elementDeclaredByClassDeclaration(node);
var type = element.type;
var isObject = type.isObject;
@@ -1315,7 +1326,7 @@ class CodeGenerator extends GeneralizingAstVisitor
jsMethods.add(new JS.Method(
_propertyName('constructor'),
js.call('function(...args) { return this.new.apply(this, args); }')
- as JS.Fun));
+ as JS.Fun));
} else if (ctors.isEmpty) {
jsMethods.add(_emitImplicitConstructor(node, fields, virtualFields));
}
@@ -1768,7 +1779,7 @@ class CodeGenerator extends GeneralizingAstVisitor
var sNames = <JS.Expression>[];
for (MethodDeclaration node in methods) {
var name = node.name.name;
- var element = node.element;
+ var element = resolutionMap.elementDeclaredByMethodDeclaration(node);
// TODO(vsm): Clean up all the nasty duplication.
if (node.isAbstract) {
continue;
@@ -1823,7 +1834,7 @@ class CodeGenerator extends GeneralizingAstVisitor
var tCtors = <JS.Property>[];
for (ConstructorDeclaration node in ctors) {
var memberName = _constructorName(node.element);
- var element = node.element;
+ var element = resolutionMap.elementDeclaredByConstructorDeclaration(node);
var type = _emitAnnotatedFunctionType(element.type, node.metadata,
parameters: node.parameters.parameters,
nameType: options.hoistSignatureTypes,
@@ -1895,7 +1906,9 @@ class CodeGenerator extends GeneralizingAstVisitor
if (_extensionTypes.hasNativeSubtype(classElem.type)) {
var dartxNames = <JS.Expression>[];
for (var m in methods) {
- if (!m.isAbstract && !m.isStatic && m.element.isPublic) {
+ if (!m.isAbstract &&
+ !m.isStatic &&
+ resolutionMap.elementDeclaredByMethodDeclaration(m).isPublic) {
dartxNames.add(_declareMemberName(m.element, useExtension: false));
}
}
@@ -1957,7 +1970,9 @@ class CodeGenerator extends GeneralizingAstVisitor
if (superCall != null) {
body.add(superCall);
}
- var name = _constructorName(node.element.unnamedConstructor);
+ var name = _constructorName(resolutionMap
+ .elementDeclaredByClassDeclaration(node)
+ .unnamedConstructor);
return annotate(
new JS.Method(name, js.call('function() { #; }', [body]) as JS.Fun),
node,
@@ -1973,12 +1988,18 @@ class CodeGenerator extends GeneralizingAstVisitor
if (_externalOrNative(node)) return null;
var name = _constructorName(node.element);
- var returnType = emitTypeRef(node.element.enclosingElement.type);
+ var returnType = emitTypeRef(resolutionMap
+ .elementDeclaredByConstructorDeclaration(node)
+ .enclosingElement
+ .type);
// Wacky factory redirecting constructors: factory Foo.q(x, y) = Bar.baz;
var redirect = node.redirectedConstructor;
if (redirect != null) {
- var newKeyword = redirect.staticElement.isFactory ? '' : 'new';
+ var newKeyword =
+ resolutionMap.staticElementForConstructorReference(redirect).isFactory
+ ? ''
+ : 'new';
// Pass along all arguments verbatim, and let the callee handle them.
// TODO(jmesserly): we'll need something different once we have
// rest/spread support, but this should work for now.
@@ -2087,7 +2108,7 @@ class CodeGenerator extends GeneralizingAstVisitor
@override
JS.Statement visitRedirectingConstructorInvocation(
RedirectingConstructorInvocation node) {
- var ctor = node.staticElement;
+ var ctor = resolutionMap.staticElementForConstructorReference(node);
var cls = ctor.enclosingElement;
// We can't dispatch to the constructor with `this.new` as that might hit a
// derived class constructor with the same name.
@@ -2264,9 +2285,11 @@ class CodeGenerator extends GeneralizingAstVisitor
// TODO(jmesserly): various problems here, see:
// https://github.com/dart-lang/dev_compiler/issues/116
- var paramType = param.element.type;
+ var paramType =
+ resolutionMap.elementDeclaredByFormalParameter(param).type;
if (node is MethodDeclaration &&
- (param.element.isCovariant || _unsoundCovariant(paramType, true)) &&
+ (resolutionMap.elementDeclaredByFormalParameter(param).isCovariant ||
+ _unsoundCovariant(paramType, true)) &&
!_inWhitelistCode(node)) {
var castType = _emitType(paramType,
nameType: options.nameTypeTests || options.hoistTypeTests,
@@ -2411,7 +2434,7 @@ class CodeGenerator extends GeneralizingAstVisitor
fn = _simplifyPassThroughArrowFunCallBody(fn);
}
- var element = node.element;
+ var element = resolutionMap.elementDeclaredByFunctionDeclaration(node);
var nameExpr = _emitTopLevelName(element);
body.add(annotate(js.statement('# = #', [nameExpr, fn]), node, element));
if (!_isDartRuntime(element.library)) {
@@ -2679,7 +2702,9 @@ class CodeGenerator extends GeneralizingAstVisitor
return new JS.Block([
declareFn,
- _emitFunctionTagged(name, func.element.type).toStatement()
+ _emitFunctionTagged(name,
+ resolutionMap.elementDeclaredByFunctionDeclaration(func).type)
+ .toStatement()
]);
}
@@ -2699,7 +2724,7 @@ class CodeGenerator extends GeneralizingAstVisitor
/// going through the qualified library name if necessary, but *not* handling
/// inferred generic function instantiation.
JS.Expression _emitSimpleIdentifier(SimpleIdentifier node) {
- var accessor = node.staticElement;
+ var accessor = resolutionMap.staticElementForIdentifier(node);
if (accessor == null) {
return js.commentExpression(
'Unimplemented unknown name', new JS.Identifier(node.name));
@@ -3230,7 +3255,7 @@ class CodeGenerator extends GeneralizingAstVisitor
return _badAssignment("Unimplemented: unknown name '$node'", node, rhs);
}
- var accessor = node.staticElement;
+ var accessor = resolutionMap.staticElementForIdentifier(node);
if (accessor == null) return unimplemented();
// Get the original declaring element. If we had a property accessor, this
@@ -3392,7 +3417,7 @@ class CodeGenerator extends GeneralizingAstVisitor
return _emitFunctionCall(node);
}
if (node.methodName.name == 'call') {
- var targetType = target.staticType;
+ var targetType = resolutionMap.staticTypeForExpression(target);
if (targetType is FunctionType) {
// Call methods on function types should be handled as regular function
// invocations.
@@ -3866,8 +3891,9 @@ class CodeGenerator extends GeneralizingAstVisitor
return _emitTopLevelField(node);
}
- var name =
- new JS.Identifier(node.name.name, type: emitTypeRef(node.element.type));
+ var name = new JS.Identifier(node.name.name,
+ type: emitTypeRef(
+ resolutionMap.elementDeclaredByVariableDeclaration(node).type));
return new JS.VariableInitialization(name, _visitInitializer(node));
}
@@ -3972,7 +3998,7 @@ class CodeGenerator extends GeneralizingAstVisitor
new JS.Method(
access,
js.call('function() { return #; }', _visitInitializer(node))
- as JS.Fun,
+ as JS.Fun,
isGetter: true),
node,
_findAccessor(element, getter: true)));
@@ -4084,7 +4110,7 @@ class CodeGenerator extends GeneralizingAstVisitor
@override
visitInstanceCreationExpression(InstanceCreationExpression node) {
- var element = node.staticElement;
+ var element = resolutionMap.staticElementForConstructorReference(node);
var constructor = node.constructorName;
var name = constructor.name;
var type = constructor.type.type;
@@ -4532,7 +4558,7 @@ class CodeGenerator extends GeneralizingAstVisitor
}
result = astFactory.prefixedIdentifier(
_bindValue(scope, 'o', ident.prefix, context: context)
- as SimpleIdentifier,
+ as SimpleIdentifier,
ident.period,
ident.identifier);
} else {
@@ -5773,7 +5799,9 @@ bool _isDeferredLoadLibrary(Expression target, SimpleIdentifier name) {
var prefix = targetIdentifier.staticElement as PrefixElement;
// The library the prefix is referring to must come from a deferred import.
- var containingLibrary = (target.root as CompilationUnit).element.library;
+ var containingLibrary = resolutionMap
+ .elementForCompilationUnit(target.root as CompilationUnit)
+ .library;
var imports = containingLibrary.getImportsWithPrefix(prefix);
return imports.length == 1 && imports[0].isDeferred;
}

Powered by Google App Engine
This is Rietveld 408576698