| OLD | NEW |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 /// This file declares a "shadow hierarchy" of concrete classes which extend | 5 /// This file declares a "shadow hierarchy" of concrete classes which extend |
| 6 /// the kernel class hierarchy using mixins from `builder/shadow_ast.dart`. | 6 /// the kernel class hierarchy, adding methods and fields needed by the |
| 7 /// BodyBuilder. |
| 7 /// | 8 /// |
| 8 /// Instances of these classes may be created using the factory methods in | 9 /// Instances of these classes may be created using the factory methods in |
| 9 /// `ast_factory.dart`. | 10 /// `ast_factory.dart`. |
| 10 /// | 11 /// |
| 11 /// Note that these classes represent the Dart language prior to desugaring. | 12 /// Note that these classes represent the Dart language prior to desugaring. |
| 12 /// When a single Dart construct desugars to a tree containing multiple kernel | 13 /// When a single Dart construct desugars to a tree containing multiple kernel |
| 13 /// AST nodes, the shadow class extends the kernel object at the top of the | 14 /// AST nodes, the shadow class extends the kernel object at the top of the |
| 14 /// desugared tree. | 15 /// desugared tree. |
| 15 /// | 16 /// |
| 16 /// This means that in some cases multiple shadow classes may extend the same | 17 /// This means that in some cases multiple shadow classes may extend the same |
| 17 /// kernel class, because multiple constructs in Dart may desugar to a tree | 18 /// kernel class, because multiple constructs in Dart may desugar to a tree |
| 18 /// with the same kind of root node. | 19 /// with the same kind of root node. |
| 19 import 'package:kernel/ast.dart'; | 20 import 'package:kernel/ast.dart'; |
| 20 | 21 |
| 21 import '../builder/shadow_ast.dart'; | |
| 22 | |
| 23 /// Concrete shadow object representing a statement block in kernel form. | 22 /// Concrete shadow object representing a statement block in kernel form. |
| 24 class KernelBlock extends Block with ShadowBlock implements KernelStatement { | 23 class KernelBlock extends Block implements KernelStatement { |
| 25 KernelBlock(List<KernelStatement> statements) : super(statements); | 24 KernelBlock(List<KernelStatement> statements) : super(statements); |
| 26 | |
| 27 @override | |
| 28 List<KernelStatement> get shadowStatements => statements; | |
| 29 } | 25 } |
| 30 | 26 |
| 31 /// Common base class for shadow objects representing expressions in kernel | 27 /// Common base class for shadow objects representing expressions in kernel |
| 32 /// form. | 28 /// form. |
| 33 abstract class KernelExpression implements Expression, ShadowExpression {} | 29 abstract class KernelExpression implements Expression {} |
| 34 | 30 |
| 35 /// Concrete shadow object representing a function expression in kernel form. | 31 /// Concrete shadow object representing a function expression in kernel form. |
| 36 class KernelFunctionExpression extends FunctionExpression | 32 class KernelFunctionExpression extends FunctionExpression |
| 37 with ShadowFunctionExpression | |
| 38 implements KernelExpression { | 33 implements KernelExpression { |
| 39 KernelFunctionExpression(FunctionNode function) : super(function); | 34 KernelFunctionExpression(FunctionNode function) : super(function); |
| 40 | |
| 41 @override | |
| 42 KernelStatement get shadowBody => function.body; | |
| 43 | |
| 44 @override | |
| 45 DartType get shadowFunctionType { | |
| 46 return function.functionType; | |
| 47 } | |
| 48 | |
| 49 @override | |
| 50 bool get shadowIsAsync { | |
| 51 // TODO(paulberry): is there a helper function in kernel that does this? | |
| 52 var asyncMarker = function.asyncMarker; | |
| 53 return asyncMarker == AsyncMarker.Async || | |
| 54 asyncMarker == AsyncMarker.AsyncStar; | |
| 55 } | |
| 56 | |
| 57 @override | |
| 58 bool get shadowIsExpressionFunction => function.body is ReturnStatement; | |
| 59 | |
| 60 @override | |
| 61 bool get shadowIsGenerator { | |
| 62 // TODO(paulberry): is there a helper function in kernel that does this? | |
| 63 var asyncMarker = function.asyncMarker; | |
| 64 return asyncMarker == AsyncMarker.SyncStar || | |
| 65 asyncMarker == AsyncMarker.AsyncStar; | |
| 66 } | |
| 67 | |
| 68 @override | |
| 69 set shadowReturnType(DartType type) { | |
| 70 function.returnType = type; | |
| 71 } | |
| 72 } | 35 } |
| 73 | 36 |
| 74 /// Concrete shadow object representing an integer literal in kernel form. | 37 /// Concrete shadow object representing an integer literal in kernel form. |
| 75 class KernelIntLiteral extends IntLiteral | 38 class KernelIntLiteral extends IntLiteral implements KernelExpression { |
| 76 with ShadowIntLiteral | |
| 77 implements KernelExpression { | |
| 78 KernelIntLiteral(int value) : super(value); | 39 KernelIntLiteral(int value) : super(value); |
| 79 } | 40 } |
| 80 | 41 |
| 81 /// Concrete shadow object representing a list literal in kernel form. | 42 /// Concrete shadow object representing a list literal in kernel form. |
| 82 class KernelListLiteral extends _KernelListLiteral | 43 class KernelListLiteral extends ListLiteral implements KernelExpression { |
| 83 with ShadowListLiteral | |
| 84 implements KernelExpression { | |
| 85 /// TODO(paulberry): see if we can eliminate the need for this by allowing | |
| 86 /// `null` to be stored in [ListLiteral] prior to type inference. | |
| 87 DartType _declaredTypeArgument; | |
| 88 | |
| 89 KernelListLiteral(List<KernelExpression> expressions, | 44 KernelListLiteral(List<KernelExpression> expressions, |
| 90 {DartType typeArgument, bool isConst: false}) | 45 {DartType typeArgument, bool isConst: false}) |
| 91 : _declaredTypeArgument = typeArgument, | 46 : super(expressions, typeArgument: typeArgument, isConst: isConst); |
| 92 super(expressions, typeArgument ?? const DynamicType(), isConst); | |
| 93 | |
| 94 @override | |
| 95 Iterable<KernelExpression> get shadowExpressions { | |
| 96 List<KernelExpression> shadowExpressions = expressions; | |
| 97 return shadowExpressions; | |
| 98 } | |
| 99 | |
| 100 @override | |
| 101 DartType get shadowTypeArgument => _declaredTypeArgument; | |
| 102 | |
| 103 @override | |
| 104 set shadowTypeArgument(DartType type) { | |
| 105 typeArgument = type; | |
| 106 } | |
| 107 } | 47 } |
| 108 | 48 |
| 109 /// Concrete shadow object representing a null literal in kernel form. | 49 /// Concrete shadow object representing a null literal in kernel form. |
| 110 class KernelNullLiteral extends NullLiteral | 50 class KernelNullLiteral extends NullLiteral implements KernelExpression {} |
| 111 with ShadowNullLiteral | |
| 112 implements KernelExpression {} | |
| 113 | 51 |
| 114 /// Concrete shadow object representing a return statement in kernel form. | 52 /// Concrete shadow object representing a return statement in kernel form. |
| 115 class KernelReturnStatement extends _KernelReturnStatement | 53 class KernelReturnStatement extends ReturnStatement implements KernelStatement { |
| 116 with ShadowReturnStatement | |
| 117 implements KernelStatement { | |
| 118 KernelReturnStatement([KernelExpression expression]) : super(expression); | 54 KernelReturnStatement([KernelExpression expression]) : super(expression); |
| 119 | |
| 120 @override | |
| 121 KernelExpression get shadowExpression => expression; | |
| 122 } | 55 } |
| 123 | 56 |
| 124 /// Common base class for shadow objects representing statements in kernel | 57 /// Common base class for shadow objects representing statements in kernel |
| 125 /// form. | 58 /// form. |
| 126 abstract class KernelStatement extends Statement implements ShadowStatement {} | 59 abstract class KernelStatement extends Statement {} |
| 127 | 60 |
| 128 /// Concrete shadow object representing a variable declaration in kernel form. | 61 /// Concrete shadow object representing a variable declaration in kernel form. |
| 129 class KernelVariableDeclaration extends _KernelVariableDeclaration | 62 class KernelVariableDeclaration extends VariableDeclaration |
| 130 with ShadowVariableDeclaration | |
| 131 implements KernelStatement { | 63 implements KernelStatement { |
| 132 /// TODO(paulberry): see if we can eliminate the need for this by allowing | |
| 133 /// `null` to be stored in [VariableDeclaration] prior to type | |
| 134 /// inference. Alternative: create a subclass of DynamicType which represents | |
| 135 /// implicit dynamic ("MissingType" or "ImplicitDynamicType" perhaps). | |
| 136 DartType _declaredType; | |
| 137 | |
| 138 KernelVariableDeclaration(String name, | 64 KernelVariableDeclaration(String name, |
| 139 {KernelExpression initializer, | 65 {KernelExpression initializer, |
| 140 DartType type, | 66 DartType type, |
| 141 bool isFinal: false, | 67 bool isFinal: false, |
| 142 bool isConst: false}) | 68 bool isConst: false}) |
| 143 : _declaredType = type, | |
| 144 super(name, initializer, type ?? const DynamicType(), isFinal, isConst); | |
| 145 | |
| 146 @override | |
| 147 KernelExpression get shadowInitializer => initializer; | |
| 148 | |
| 149 @override | |
| 150 DartType get shadowType => _declaredType; | |
| 151 | |
| 152 @override | |
| 153 set shadowType(DartType type) { | |
| 154 this.type = type; | |
| 155 } | |
| 156 | |
| 157 @override | |
| 158 set type(DartType type) { | |
| 159 super.type = _declaredType = type; | |
| 160 } | |
| 161 } | |
| 162 | |
| 163 /// Concrete shadow object representing a read from a variable in kernel form. | |
| 164 class KernelVariableGet extends _KernelVariableGet | |
| 165 with ShadowVariableGet | |
| 166 implements KernelExpression { | |
| 167 KernelVariableGet(VariableDeclaration variable, [DartType promotedType]) | |
| 168 : super(variable, promotedType); | |
| 169 | |
| 170 @override | |
| 171 KernelVariableDeclaration get shadowDeclaration => variable; | |
| 172 } | |
| 173 | |
| 174 /// Adaptor class allowing [ListLiteral] to be extended with a mixin. | |
| 175 /// | |
| 176 /// TODO(paulberry): see if we can eliminate the need for this class by adding | |
| 177 /// a named constructor to [ListLiteral] in which all arguments are | |
| 178 /// required. | |
| 179 class _KernelListLiteral extends ListLiteral { | |
| 180 _KernelListLiteral( | |
| 181 List<Expression> expressions, DartType typeArgument, bool isConst) | |
| 182 : super(expressions, typeArgument: typeArgument, isConst: isConst); | |
| 183 } | |
| 184 | |
| 185 /// Adaptor class allowing [ReturnStatement] to be extended with a mixin. | |
| 186 /// | |
| 187 /// TODO(paulberry): see if we can eliminate the need for this class by adding | |
| 188 /// a named constructor to [ReturnStatement] in which all arguments are | |
| 189 /// required. | |
| 190 class _KernelReturnStatement extends ReturnStatement { | |
| 191 _KernelReturnStatement(KernelExpression expression) : super(expression); | |
| 192 } | |
| 193 | |
| 194 /// Adaptor class allowing [VariableDeclaration] to be extended with a | |
| 195 /// mixin. | |
| 196 /// | |
| 197 /// TODO(paulberry): see if we can eliminate the need for this class by adding | |
| 198 /// a named constructor to [VariableDeclaration] in which all arguments | |
| 199 /// are required. | |
| 200 class _KernelVariableDeclaration extends VariableDeclaration { | |
| 201 _KernelVariableDeclaration(String name, Expression initializer, DartType type, | |
| 202 bool isFinal, bool isConst) | |
| 203 : super(name, | 69 : super(name, |
| 204 initializer: initializer, | 70 initializer: initializer, |
| 205 type: type, | 71 type: type, |
| 206 isFinal: isFinal, | 72 isFinal: isFinal, |
| 207 isConst: isConst); | 73 isConst: isConst); |
| 208 } | 74 } |
| 209 | 75 |
| 210 /// Adaptor class allowing [VariableGet] to be extended with a mixin. | 76 /// Concrete shadow object representing a read from a variable in kernel form. |
| 211 /// | 77 class KernelVariableGet extends VariableGet implements KernelExpression { |
| 212 /// TODO(paulberry): see if we can eliminate the need for this class by adding | 78 KernelVariableGet(VariableDeclaration variable, [DartType promotedType]) |
| 213 /// a named constructor to [VariableGet] in which all arguments are | |
| 214 /// required. | |
| 215 class _KernelVariableGet extends VariableGet { | |
| 216 _KernelVariableGet(VariableDeclaration variable, DartType promotedType) | |
| 217 : super(variable, promotedType); | 79 : super(variable, promotedType); |
| 218 } | 80 } |
| OLD | NEW |