Chromium Code Reviews| Index: pkg/front_end/lib/src/fasta/kernel/shadow_ast.dart |
| diff --git a/pkg/front_end/lib/src/fasta/kernel/shadow_ast.dart b/pkg/front_end/lib/src/fasta/kernel/shadow_ast.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e1f5ca79ff908dce9c8f49d37543b15bdcb49962 |
| --- /dev/null |
| +++ b/pkg/front_end/lib/src/fasta/kernel/shadow_ast.dart |
| @@ -0,0 +1,226 @@ |
| +// Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file |
|
ahe
2017/03/24 12:00:41
\o/
|
| +// 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. |
| + |
| +/// This file declares a "shadow hierarchy" of concrete classes which extend |
| +/// the kernel class hierarchy using mixins from `builder/shadow_ast.dart`. |
| +/// |
| +/// Instances of these classes may be created using the factory methods in |
| +/// `ast_factory.dart`. |
| +/// |
| +/// Note that these classes represent the Dart language prior to desugaring. |
| +/// When a single Dart construct desugars to a tree containing multiple kernel |
| +/// AST nodes, the shadow class extends the kernel object at the top of the |
| +/// desugared tree. |
| +/// |
| +/// This means that in some cases multiple shadow classes may extend the same |
| +/// kernel class, because multiple constructs in Dart may desugar to a tree |
| +/// with the same kind of root node. |
| +import 'package:kernel/ast.dart' as kernel; |
| +import 'package:kernel/ast.dart' show DartType; |
| + |
| +import '../builder/shadow_ast.dart' as builder; |
| + |
| +/// Concrete shadow object representing a statement block in kernel form. |
| +class Block extends kernel.Block with builder.Block implements Statement { |
|
ahe
2017/03/24 12:00:41
KernelBlock?
Paul Berry
2017/03/24 14:55:24
Done.
|
| + Block(List<Statement> statements) : super(statements); |
| + |
| + @override |
| + List<Statement> get shadowStatements => statements; |
| +} |
| + |
| +/// Common base class for shadow objects representing expressions in kernel |
| +/// form. |
| +abstract class Expression implements kernel.Expression, builder.Expression {} |
| + |
| +/// Concrete shadow object representing a function expression in kernel form. |
| +class FunctionExpression extends kernel.FunctionExpression |
| + with builder.FunctionExpression |
| + implements Expression { |
| + FunctionExpression(kernel.FunctionNode function) : super(function); |
| + |
| + @override |
| + Statement get shadowBody => function.body; |
| + |
| + @override |
| + bool get shadowIsAsync { |
| + // TODO(paulberry): is there a helper function in kernel that does this? |
| + var asyncMarker = function.asyncMarker; |
| + return asyncMarker == kernel.AsyncMarker.Async || |
| + asyncMarker == kernel.AsyncMarker.AsyncStar; |
| + } |
| + |
| + @override |
| + bool get shadowIsExpressionFunction => |
| + function.body is kernel.ReturnStatement; |
| + |
| + @override |
| + bool get shadowIsGenerator { |
| + // TODO(paulberry): is there a helper function in kernel that does this? |
| + var asyncMarker = function.asyncMarker; |
| + return asyncMarker == kernel.AsyncMarker.SyncStar || |
| + asyncMarker == kernel.AsyncMarker.AsyncStar; |
| + } |
| + |
| + @override |
| + set shadowReturnType(DartType type) { |
| + function.returnType = type; |
| + } |
| + |
| + @override |
| + DartType shadowMakeFunctionType() { |
| + return function.functionType; |
| + } |
| +} |
| + |
| +/// Concrete shadow object representing an integer literal in kernel form. |
| +class IntLiteral extends kernel.IntLiteral |
| + with builder.IntLiteral |
| + implements Expression { |
| + IntLiteral(int value) : super(value); |
| +} |
| + |
| +/// Concrete shadow object representing a list literal in kernel form. |
| +class ListLiteral extends _KernelListLiteral |
| + with builder.ListLiteral |
| + implements Expression { |
| + /// TODO(paulberry): see if we can eliminate the need for this by allowing |
| + /// `null` to be stored in [kernel.ListLiteral] prior to type inference. |
| + DartType _declaredTypeArgument; |
| + |
| + ListLiteral(List<Expression> expressions, |
| + {DartType typeArgument, bool isConst: false}) |
| + : _declaredTypeArgument = typeArgument, |
| + super(expressions, typeArgument ?? const kernel.DynamicType(), isConst); |
| + |
| + @override |
| + Iterable<Expression> get shadowExpressions { |
| + List<Expression> shadowExpressions = expressions; |
| + return shadowExpressions; |
| + } |
| + |
| + @override |
| + kernel.DartType get shadowTypeArgument => _declaredTypeArgument; |
| + |
| + @override |
| + set shadowTypeArgument(kernel.DartType type) { |
| + typeArgument = type; |
| + } |
| +} |
| + |
| +/// Concrete shadow object representing a null literal in kernel form. |
| +class NullLiteral extends kernel.NullLiteral |
| + with builder.NullLiteral |
| + implements Expression {} |
| + |
| +/// Concrete shadow object representing a return statement in kernel form. |
| +class ReturnStatement extends _KernelReturnStatement |
| + with builder.ReturnStatement |
| + implements Statement { |
| + ReturnStatement([Expression expression]) : super(expression); |
| + |
| + @override |
| + Expression get shadowExpression => expression; |
| +} |
| + |
| +/// Common base class for shadow objects representing statements in kernel |
| +/// form. |
| +abstract class Statement extends kernel.Statement implements builder.Statement { |
| +} |
| + |
| +/// Concrete shadow object representing a variable declaration in kernel form. |
| +class VariableDeclaration extends _KernelVariableDeclaration |
| + with builder.VariableDeclaration |
| + implements Statement { |
| + /// TODO(paulberry): see if we can eliminate the need for this by allowing |
| + /// `null` to be stored in [kernel.VariableDeclaration] prior to type inference. |
|
ahe
2017/03/24 12:00:40
Alternative: create a subclass of DynamicType call
ahe
2017/03/24 12:00:41
Long line.
Paul Berry
2017/03/24 14:55:24
Done.
Paul Berry
2017/03/24 14:55:24
Interesting idea. I've noted it in the TODO.
|
| + DartType _declaredType; |
| + |
| + VariableDeclaration(String name, |
| + {Expression initializer, |
| + DartType type, |
| + bool isFinal: false, |
| + bool isConst: false}) |
| + : _declaredType = type, |
| + super(name, initializer, type ?? const kernel.DynamicType(), null, |
| + isFinal, isConst); |
| + |
| + @override |
| + Expression get shadowInitializer => initializer; |
| + |
| + @override |
| + DartType get shadowType => _declaredType; |
| + |
| + @override |
| + set shadowType(kernel.DartType type) { |
| + this.type = type; |
| + } |
| + |
| + @override |
| + set type(kernel.DartType type) { |
| + super.type = _declaredType = type; |
| + } |
| +} |
| + |
| +/// Concrete shadow object representing a read from a variable in kernel form. |
| +class VariableGet extends _KernelVariableGet |
| + with builder.VariableGet |
| + implements Expression { |
| + VariableGet(kernel.VariableDeclaration variable, [DartType promotedType]) |
| + : super(variable, promotedType); |
| + |
| + @override |
| + VariableDeclaration get shadowDeclaration => variable; |
| +} |
| + |
| +/// Adaptor class allowing [kernel.ListLiteral] to be extended with a mixin. |
| +/// |
| +/// TODO(paulberry): see if we can eliminate the need for this class by adding |
| +/// a named constructor to [kernel.ListLiteral] in which all arguments are |
| +/// required. |
| +class _KernelListLiteral extends kernel.ListLiteral { |
| + _KernelListLiteral( |
| + List<kernel.Expression> expressions, DartType typeArgument, bool isConst) |
| + : super(expressions, typeArgument: typeArgument, isConst: isConst); |
| +} |
| + |
| +/// Adaptor class allowing [kernel.ReturnStatement] to be extended with a mixin. |
| +/// |
| +/// TODO(paulberry): see if we can eliminate the need for this class by adding |
| +/// a named constructor to [kernel.ReturnStatement] in which all arguments are |
| +/// required. |
| +class _KernelReturnStatement extends kernel.ReturnStatement { |
| + _KernelReturnStatement(Expression expression) : super(expression); |
| +} |
| + |
| +/// Adaptor class allowing [kernel.VariableDeclaration] to be extended with a |
| +/// mixin. |
| +/// |
| +/// TODO(paulberry): see if we can eliminate the need for this class by adding |
| +/// a named constructor to [kernel.VariableDeclaration] in which all arguments |
| +/// are required. |
| +class _KernelVariableDeclaration extends kernel.VariableDeclaration { |
| + _KernelVariableDeclaration( |
| + String name, |
| + kernel.Expression initializer, |
| + DartType type, |
| + kernel.InferredValue inferredValue, |
| + bool isFinal, |
| + bool isConst) |
| + : super(name, |
| + initializer: initializer, |
| + type: type, |
| + inferredValue: inferredValue, |
| + isFinal: isFinal, |
| + isConst: isConst); |
| +} |
| + |
| +/// Adaptor class allowing [kernel.VariableGet] to be extended with a mixin. |
| +/// |
| +/// TODO(paulberry): see if we can eliminate the need for this class by adding |
| +/// a named constructor to [kernel.VariableGet] in which all arguments are |
| +/// required. |
| +class _KernelVariableGet extends kernel.VariableGet { |
| + _KernelVariableGet(kernel.VariableDeclaration variable, DartType promotedType) |
| + : super(variable, promotedType); |
| +} |