Modify dev_compiler to use analyzer's new AstFactory class.

The old mechanism for creating analyzer ASTs (factory constructors on
the interface classes) will be going away in analyzer 0.30.

R=jmesserly@google.com

Review URL: https://codereview.chromium.org/2545843002 .
This commit is contained in:
Paul Berry 2016-12-01 10:37:22 -08:00
parent 2368a59d60
commit 2d289c2df6
3 changed files with 58 additions and 51 deletions

View file

@ -3,6 +3,7 @@
// BSD-style license that can be found in the LICENSE file. // BSD-style license that can be found in the LICENSE file.
import 'package:analyzer/dart/ast/ast.dart'; import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/standard_ast_factory.dart';
import 'package:analyzer/dart/ast/token.dart'; import 'package:analyzer/dart/ast/token.dart';
import 'package:analyzer/src/dart/ast/token.dart'; import 'package:analyzer/src/dart/ast/token.dart';
import 'package:analyzer/src/generated/utilities_dart.dart'; import 'package:analyzer/src/generated/utilities_dart.dart';
@ -102,13 +103,13 @@ class AstBuilder {
static PropertyAccess propertyAccess( static PropertyAccess propertyAccess(
Expression target, SimpleIdentifier name) { Expression target, SimpleIdentifier name) {
var p = new Token(TokenType.PERIOD, 0); var p = new Token(TokenType.PERIOD, 0);
return new PropertyAccess(target, p, name); return astFactory.propertyAccess(target, p, name);
} }
static MethodInvocation methodInvoke(Expression target, SimpleIdentifier name, static MethodInvocation methodInvoke(Expression target, SimpleIdentifier name,
TypeArgumentList typeArguments, NodeList<Expression> args) { TypeArgumentList typeArguments, NodeList<Expression> args) {
var p = new Token(TokenType.PERIOD, 0); var p = new Token(TokenType.PERIOD, 0);
return new MethodInvocation( return astFactory.methodInvocation(
target, p, name, typeArguments, argumentList(args)); target, p, name, typeArguments, argumentList(args));
} }
@ -359,8 +360,8 @@ class AstBuilder {
[Expression init]) { [Expression init]) {
var eqToken = init != null ? new Token(TokenType.EQ, 0) : null; var eqToken = init != null ? new Token(TokenType.EQ, 0) : null;
var varToken = new KeywordToken(Keyword.VAR, 0); var varToken = new KeywordToken(Keyword.VAR, 0);
return new VariableDeclarationList(null, null, varToken, null, return astFactory.variableDeclarationList(null, null, varToken, null,
[new VariableDeclaration(name, eqToken, init)]); [astFactory.variableDeclaration(name, eqToken, init)]);
} }
static VariableDeclarationStatement variableStatement(SimpleIdentifier name, static VariableDeclarationStatement variableStatement(SimpleIdentifier name,
@ -372,7 +373,7 @@ class AstBuilder {
static InstanceCreationExpression instanceCreation( static InstanceCreationExpression instanceCreation(
ConstructorName ctor, List<Expression> args) { ConstructorName ctor, List<Expression> args) {
var newToken = new KeywordToken(Keyword.NEW, 0); var newToken = new KeywordToken(Keyword.NEW, 0);
return new InstanceCreationExpression( return astFactory.instanceCreationExpression(
newToken, ctor, RawAstBuilder.argumentList(args)); newToken, ctor, RawAstBuilder.argumentList(args));
} }
} }
@ -383,112 +384,113 @@ class RawAstBuilder {
static ConstructorName constructorName(TypeName type, static ConstructorName constructorName(TypeName type,
[SimpleIdentifier name]) { [SimpleIdentifier name]) {
Token period = name != null ? new Token(TokenType.PERIOD, 0) : null; Token period = name != null ? new Token(TokenType.PERIOD, 0) : null;
return new ConstructorName(type, period, name); return astFactory.constructorName(type, period, name);
} }
static SimpleIdentifier identifierFromString(String name) { static SimpleIdentifier identifierFromString(String name) {
StringToken token = new SyntheticStringToken(TokenType.IDENTIFIER, name, 0); StringToken token = new SyntheticStringToken(TokenType.IDENTIFIER, name, 0);
return new SimpleIdentifier(token); return astFactory.simpleIdentifier(token);
} }
static PrefixedIdentifier prefixedIdentifier( static PrefixedIdentifier prefixedIdentifier(
SimpleIdentifier pre, SimpleIdentifier id) { SimpleIdentifier pre, SimpleIdentifier id) {
Token period = new Token(TokenType.PERIOD, 0); Token period = new Token(TokenType.PERIOD, 0);
return new PrefixedIdentifier(pre, period, id); return astFactory.prefixedIdentifier(pre, period, id);
} }
static TypeParameter typeParameter(SimpleIdentifier name, static TypeParameter typeParameter(SimpleIdentifier name,
[TypeName bound = null]) { [TypeName bound = null]) {
Token keyword = Token keyword =
(bound == null) ? null : new KeywordToken(Keyword.EXTENDS, 0); (bound == null) ? null : new KeywordToken(Keyword.EXTENDS, 0);
return new TypeParameter(null, null, name, keyword, bound); return astFactory.typeParameter(null, null, name, keyword, bound);
} }
static TypeParameterList typeParameterList(List<TypeParameter> params) { static TypeParameterList typeParameterList(List<TypeParameter> params) {
Token lb = new Token(TokenType.LT, 0); Token lb = new Token(TokenType.LT, 0);
Token rb = new Token(TokenType.GT, 0); Token rb = new Token(TokenType.GT, 0);
return new TypeParameterList(lb, params, rb); return astFactory.typeParameterList(lb, params, rb);
} }
static TypeArgumentList typeArgumentList(List<TypeName> args) { static TypeArgumentList typeArgumentList(List<TypeName> args) {
Token lb = new Token(TokenType.LT, 0); Token lb = new Token(TokenType.LT, 0);
Token rb = new Token(TokenType.GT, 0); Token rb = new Token(TokenType.GT, 0);
return new TypeArgumentList(lb, args, rb); return astFactory.typeArgumentList(lb, args, rb);
} }
static ArgumentList argumentList(List<Expression> args) { static ArgumentList argumentList(List<Expression> args) {
Token lp = new BeginToken(TokenType.OPEN_PAREN, 0); Token lp = new BeginToken(TokenType.OPEN_PAREN, 0);
Token rp = new Token(TokenType.CLOSE_PAREN, 0); Token rp = new Token(TokenType.CLOSE_PAREN, 0);
return new ArgumentList(lp, args, rp); return astFactory.argumentList(lp, args, rp);
} }
static TypeName typeName(Identifier id, TypeArgumentList l) { static TypeName typeName(Identifier id, TypeArgumentList l) {
return new TypeName(id, l); return astFactory.typeName(id, l);
} }
static FunctionTypeAlias functionTypeAlias(TypeName ret, static FunctionTypeAlias functionTypeAlias(TypeName ret,
SimpleIdentifier name, TypeParameterList tps, FormalParameterList fps) { SimpleIdentifier name, TypeParameterList tps, FormalParameterList fps) {
Token semi = new Token(TokenType.SEMICOLON, 0); Token semi = new Token(TokenType.SEMICOLON, 0);
Token td = new KeywordToken(Keyword.TYPEDEF, 0); Token td = new KeywordToken(Keyword.TYPEDEF, 0);
return new FunctionTypeAlias(null, null, td, ret, name, tps, fps, semi); return astFactory.functionTypeAlias(
null, null, td, ret, name, tps, fps, semi);
} }
static BooleanLiteral booleanLiteral(bool b) { static BooleanLiteral booleanLiteral(bool b) {
var k = new KeywordToken(b ? Keyword.TRUE : Keyword.FALSE, 0); var k = new KeywordToken(b ? Keyword.TRUE : Keyword.FALSE, 0);
return new BooleanLiteral(k, b); return astFactory.booleanLiteral(k, b);
} }
static NullLiteral nullLiteral() { static NullLiteral nullLiteral() {
var n = new KeywordToken(Keyword.NULL, 0); var n = new KeywordToken(Keyword.NULL, 0);
return new NullLiteral(n); return astFactory.nullLiteral(n);
} }
static IntegerLiteral integerLiteral(int i) { static IntegerLiteral integerLiteral(int i) {
StringToken token = new StringToken(TokenType.INT, '$i', 0); StringToken token = new StringToken(TokenType.INT, '$i', 0);
return new IntegerLiteral(token, i); return astFactory.integerLiteral(token, i);
} }
static SimpleStringLiteral simpleStringLiteral(String s) { static SimpleStringLiteral simpleStringLiteral(String s) {
StringToken token = new StringToken(TokenType.STRING, "\"" + s + "\"", 0); StringToken token = new StringToken(TokenType.STRING, "\"" + s + "\"", 0);
return new SimpleStringLiteral(token, s); return astFactory.simpleStringLiteral(token, s);
} }
static SimpleStringLiteral tripleQuotedStringLiteral(String s) { static SimpleStringLiteral tripleQuotedStringLiteral(String s) {
StringToken token = new StringToken(TokenType.STRING, '"""' + s + '"""', 0); StringToken token = new StringToken(TokenType.STRING, '"""' + s + '"""', 0);
return new SimpleStringLiteral(token, s); return astFactory.simpleStringLiteral(token, s);
} }
static AsExpression asExpression(Expression exp, TypeName type) { static AsExpression asExpression(Expression exp, TypeName type) {
Token token = new KeywordToken(Keyword.AS, 0); Token token = new KeywordToken(Keyword.AS, 0);
return new AsExpression(exp, token, type); return astFactory.asExpression(exp, token, type);
} }
static IsExpression isExpression(Expression exp, TypeName type) { static IsExpression isExpression(Expression exp, TypeName type) {
Token token = new KeywordToken(Keyword.IS, 0); Token token = new KeywordToken(Keyword.IS, 0);
return new IsExpression(exp, token, null, type); return astFactory.isExpression(exp, token, null, type);
} }
static ParenthesizedExpression parenthesizedExpression(Expression exp) { static ParenthesizedExpression parenthesizedExpression(Expression exp) {
Token lp = new BeginToken(TokenType.OPEN_PAREN, exp.offset); Token lp = new BeginToken(TokenType.OPEN_PAREN, exp.offset);
Token rp = new Token(TokenType.CLOSE_PAREN, exp.end); Token rp = new Token(TokenType.CLOSE_PAREN, exp.end);
return new ParenthesizedExpression(lp, exp, rp); return astFactory.parenthesizedExpression(lp, exp, rp);
} }
static BinaryExpression binaryExpression( static BinaryExpression binaryExpression(
Expression l, Token op, Expression r) { Expression l, Token op, Expression r) {
return new BinaryExpression(l, op, r); return astFactory.binaryExpression(l, op, r);
} }
static ConditionalExpression conditionalExpression( static ConditionalExpression conditionalExpression(
Expression cond, Expression tExp, Expression fExp) { Expression cond, Expression tExp, Expression fExp) {
var q = new Token(TokenType.QUESTION, 0); var q = new Token(TokenType.QUESTION, 0);
var c = new Token(TokenType.COLON, 0); var c = new Token(TokenType.COLON, 0);
return new ConditionalExpression(cond, q, tExp, c, fExp); return astFactory.conditionalExpression(cond, q, tExp, c, fExp);
} }
static Expression functionExpressionInvocation( static Expression functionExpressionInvocation(
Expression function, ArgumentList es) { Expression function, ArgumentList es) {
return new FunctionExpressionInvocation(function, null, es); return astFactory.functionExpressionInvocation(function, null, es);
} }
static FormalParameterList formalParameterList(List<FormalParameter> params) { static FormalParameterList formalParameterList(List<FormalParameter> params) {
@ -507,67 +509,67 @@ class RawAstBuilder {
ld = new BeginToken(TokenType.OPEN_CURLY_BRACKET, 0); ld = new BeginToken(TokenType.OPEN_CURLY_BRACKET, 0);
rd = new Token(TokenType.CLOSE_CURLY_BRACKET, 0); rd = new Token(TokenType.CLOSE_CURLY_BRACKET, 0);
} }
return new FormalParameterList(lp, params, ld, rd, rp); return astFactory.formalParameterList(lp, params, ld, rd, rp);
} }
static Block block(List<Statement> statements) { static Block block(List<Statement> statements) {
Token ld = new BeginToken(TokenType.OPEN_CURLY_BRACKET, 0); Token ld = new BeginToken(TokenType.OPEN_CURLY_BRACKET, 0);
Token rd = new Token(TokenType.CLOSE_CURLY_BRACKET, 0); Token rd = new Token(TokenType.CLOSE_CURLY_BRACKET, 0);
return new Block(ld, statements, rd); return astFactory.block(ld, statements, rd);
} }
static BlockFunctionBody blockFunctionBody(Block b) { static BlockFunctionBody blockFunctionBody(Block b) {
return new BlockFunctionBody(null, null, b); return astFactory.blockFunctionBody(null, null, b);
} }
static ExpressionFunctionBody expressionFunctionBody(Expression body, static ExpressionFunctionBody expressionFunctionBody(Expression body,
[bool decl = false]) { [bool decl = false]) {
Token semi = (decl) ? new Token(TokenType.SEMICOLON, 0) : null; Token semi = (decl) ? new Token(TokenType.SEMICOLON, 0) : null;
return new ExpressionFunctionBody(null, null, body, semi); return astFactory.expressionFunctionBody(null, null, body, semi);
} }
static ExpressionStatement expressionStatement(Expression expression) { static ExpressionStatement expressionStatement(Expression expression) {
Token semi = new Token(TokenType.SEMICOLON, 0); Token semi = new Token(TokenType.SEMICOLON, 0);
return new ExpressionStatement(expression, semi); return astFactory.expressionStatement(expression, semi);
} }
static FunctionDeclaration functionDeclaration( static FunctionDeclaration functionDeclaration(
TypeName rt, SimpleIdentifier f, FunctionExpression fexp) { TypeName rt, SimpleIdentifier f, FunctionExpression fexp) {
return new FunctionDeclaration(null, null, null, rt, null, f, fexp); return astFactory.functionDeclaration(null, null, null, rt, null, f, fexp);
} }
static MethodDeclaration methodDeclaration(TypeName rt, SimpleIdentifier m, static MethodDeclaration methodDeclaration(TypeName rt, SimpleIdentifier m,
FormalParameterList fl, FunctionBody body, FormalParameterList fl, FunctionBody body,
{bool isStatic: false}) { {bool isStatic: false}) {
Token st = isStatic ? new KeywordToken(Keyword.STATIC, 0) : null; Token st = isStatic ? new KeywordToken(Keyword.STATIC, 0) : null;
return new MethodDeclaration( return astFactory.methodDeclaration(
null, null, null, st, rt, null, null, m, null, fl, body); null, null, null, st, rt, null, null, m, null, fl, body);
} }
static FunctionExpression functionExpression( static FunctionExpression functionExpression(
FormalParameterList fl, FunctionBody body) { FormalParameterList fl, FunctionBody body) {
return new FunctionExpression(null, fl, body); return astFactory.functionExpression(null, fl, body);
} }
static FunctionDeclarationStatement functionDeclarationStatement( static FunctionDeclarationStatement functionDeclarationStatement(
FunctionDeclaration fd) { FunctionDeclaration fd) {
return new FunctionDeclarationStatement(fd); return astFactory.functionDeclarationStatement(fd);
} }
static Statement returnExpression([Expression e]) { static Statement returnExpression([Expression e]) {
Token ret = new KeywordToken(Keyword.RETURN, 0); Token ret = new KeywordToken(Keyword.RETURN, 0);
Token semi = new Token(TokenType.SEMICOLON, 0); Token semi = new Token(TokenType.SEMICOLON, 0);
return new ReturnStatement(ret, e, semi); return astFactory.returnStatement(ret, e, semi);
} }
static SimpleFormalParameter simpleFormalParameter( static SimpleFormalParameter simpleFormalParameter(
SimpleIdentifier v, TypeName t) { SimpleIdentifier v, TypeName t) {
return new SimpleFormalParameter(null, <Annotation>[], null, t, v); return astFactory.simpleFormalParameter(null, <Annotation>[], null, t, v);
} }
static FunctionTypedFormalParameter functionTypedFormalParameter( static FunctionTypedFormalParameter functionTypedFormalParameter(
TypeName ret, SimpleIdentifier v, FormalParameterList ps) { TypeName ret, SimpleIdentifier v, FormalParameterList ps) {
return new FunctionTypedFormalParameter( return astFactory.functionTypedFormalParameter(
null, <Annotation>[], ret, v, null, ps); null, <Annotation>[], ret, v, null, ps);
} }
@ -576,11 +578,13 @@ class RawAstBuilder {
} }
static FormalParameter optionalFormalParameter(NormalFormalParameter fp) { static FormalParameter optionalFormalParameter(NormalFormalParameter fp) {
return new DefaultFormalParameter(fp, ParameterKind.POSITIONAL, null, null); return astFactory.defaultFormalParameter(
fp, ParameterKind.POSITIONAL, null, null);
} }
static FormalParameter namedFormalParameter(NormalFormalParameter fp) { static FormalParameter namedFormalParameter(NormalFormalParameter fp) {
return new DefaultFormalParameter(fp, ParameterKind.NAMED, null, null); return astFactory.defaultFormalParameter(
fp, ParameterKind.NAMED, null, null);
} }
static NamedExpression namedParameter(SimpleIdentifier s, Expression e) { static NamedExpression namedParameter(SimpleIdentifier s, Expression e) {
@ -588,13 +592,13 @@ class RawAstBuilder {
} }
static NamedExpression namedExpression(SimpleIdentifier s, Expression e) { static NamedExpression namedExpression(SimpleIdentifier s, Expression e) {
Label l = new Label(s, new Token(TokenType.COLON, 0)); Label l = astFactory.label(s, new Token(TokenType.COLON, 0));
return new NamedExpression(l, e); return astFactory.namedExpression(l, e);
} }
static VariableDeclarationStatement variableDeclarationStatement( static VariableDeclarationStatement variableDeclarationStatement(
VariableDeclarationList varDecl) { VariableDeclarationList varDecl) {
var semi = new Token(TokenType.SEMICOLON, 0); var semi = new Token(TokenType.SEMICOLON, 0);
return new VariableDeclarationStatement(varDecl, semi); return astFactory.variableDeclarationStatement(varDecl, semi);
} }
} }

View file

@ -8,6 +8,7 @@ import 'dart:math' show min, max;
import 'package:analyzer/analyzer.dart' hide ConstantEvaluator; import 'package:analyzer/analyzer.dart' hide ConstantEvaluator;
import 'package:analyzer/dart/ast/ast.dart'; 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/token.dart' show Token, TokenType;
import 'package:analyzer/dart/element/element.dart'; import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/type.dart'; import 'package:analyzer/dart/element/type.dart';
@ -4453,8 +4454,8 @@ class CodeGenerator extends GeneralizingAstVisitor
// LocalVariableElementImpl, so we could repurpose to mean "temp". // LocalVariableElementImpl, so we could repurpose to mean "temp".
// * add a new property to LocalVariableElementImpl. // * add a new property to LocalVariableElementImpl.
// * create a new subtype of LocalVariableElementImpl to mark a temp. // * create a new subtype of LocalVariableElementImpl to mark a temp.
var id = var id = astFactory
new SimpleIdentifier(new StringToken(TokenType.IDENTIFIER, name, -1)); .simpleIdentifier(new StringToken(TokenType.IDENTIFIER, name, -1));
variable ??= new JS.TemporaryId(name); variable ??= new JS.TemporaryId(name);
@ -4509,14 +4510,14 @@ class CodeGenerator extends GeneralizingAstVisitor
Expression result; Expression result;
if (expr is IndexExpression) { if (expr is IndexExpression) {
IndexExpression index = expr; IndexExpression index = expr;
result = new IndexExpression.forTarget( result = astFactory.indexExpressionForTarget(
_bindValue(scope, 'o', index.target, context: context), _bindValue(scope, 'o', index.target, context: context),
index.leftBracket, index.leftBracket,
_bindValue(scope, 'i', index.index, context: context), _bindValue(scope, 'i', index.index, context: context),
index.rightBracket); index.rightBracket);
} else if (expr is PropertyAccess) { } else if (expr is PropertyAccess) {
PropertyAccess prop = expr; PropertyAccess prop = expr;
result = new PropertyAccess( result = astFactory.propertyAccess(
_bindValue(scope, 'o', _getTarget(prop), context: context), _bindValue(scope, 'o', _getTarget(prop), context: context),
prop.operator, prop.operator,
prop.propertyName); prop.propertyName);
@ -4525,7 +4526,7 @@ class CodeGenerator extends GeneralizingAstVisitor
if (isLibraryPrefix(ident.prefix)) { if (isLibraryPrefix(ident.prefix)) {
return expr; return expr;
} }
result = new PrefixedIdentifier( result = astFactory.prefixedIdentifier(
_bindValue(scope, 'o', ident.prefix, context: context) _bindValue(scope, 'o', ident.prefix, context: context)
as SimpleIdentifier, as SimpleIdentifier,
ident.period, ident.period,

View file

@ -4,6 +4,7 @@
import 'package:analyzer/analyzer.dart' as analyzer; import 'package:analyzer/analyzer.dart' as analyzer;
import 'package:analyzer/dart/ast/ast.dart'; import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/standard_ast_factory.dart';
import 'package:analyzer/dart/element/type.dart' show DartType; import 'package:analyzer/dart/element/type.dart' show DartType;
import 'package:analyzer/src/dart/ast/ast.dart' show FunctionBodyImpl; import 'package:analyzer/src/dart/ast/ast.dart' show FunctionBodyImpl;
import 'package:analyzer/src/dart/ast/utilities.dart' show NodeReplacer; import 'package:analyzer/src/dart/ast/utilities.dart' show NodeReplacer;
@ -40,7 +41,8 @@ class CoercionReifier extends analyzer.GeneralizingAstVisitor<Object> {
static Expression castExpression(Expression e, DartType toType) { static Expression castExpression(Expression e, DartType toType) {
// We use an empty name in the AST, because the JS code generator only cares // We use an empty name in the AST, because the JS code generator only cares
// about the target type. It does not look at the AST name. // about the target type. It does not look at the AST name.
var typeName = new TypeName(AstBuilder.identifierFromString(''), null); var typeName =
astFactory.typeName(AstBuilder.identifierFromString(''), null);
typeName.type = toType; typeName.type = toType;
var cast = AstBuilder.asExpression(e, typeName); var cast = AstBuilder.asExpression(e, typeName);
cast.staticType = toType; cast.staticType = toType;