OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 library backend_ast_emitter; | 5 library backend_ast_emitter; |
6 | 6 |
7 import '../tree_ir/tree_ir_nodes.dart' as tree; | 7 import '../tree_ir/tree_ir_nodes.dart' as tree; |
8 import 'backend_ast_nodes.dart'; | 8 import 'backend_ast_nodes.dart'; |
9 import '../constants/expressions.dart'; | 9 import '../constants/expressions.dart'; |
10 import '../constants/values.dart'; | 10 import '../constants/values.dart'; |
(...skipping 1096 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1107 ..dartType = type; | 1107 ..dartType = type; |
1108 } else { | 1108 } else { |
1109 throw "Unsupported type annotation: $type"; | 1109 throw "Unsupported type annotation: $type"; |
1110 } | 1110 } |
1111 } | 1111 } |
1112 | 1112 |
1113 } | 1113 } |
1114 | 1114 |
1115 | 1115 |
1116 class ConstantEmitter | 1116 class ConstantEmitter |
1117 extends ConstantExpressionVisitor<BuilderContext<Statement>, Expression> { | 1117 extends ConstantExpressionVisitor<Expression, BuilderContext<Statement>> { |
1118 const ConstantEmitter(); | 1118 const ConstantEmitter(); |
1119 | 1119 |
1120 /// Creates the [Expression] for the constant [exp]. | 1120 /// Creates the [Expression] for the constant [exp]. |
1121 static Expression createExpression(ConstantExpression exp, | 1121 static Expression createExpression(ConstantExpression exp, |
1122 BuilderContext<Statement> context) { | 1122 BuilderContext<Statement> context) { |
1123 return const ConstantEmitter().visit(exp, context); | 1123 return const ConstantEmitter().visit(exp, context); |
1124 } | 1124 } |
1125 | 1125 |
1126 Expression handlePrimitiveConstant(PrimitiveConstantValue value) { | 1126 Expression handlePrimitiveConstant(PrimitiveConstantValue value) { |
1127 // Num constants may be negative, while literals must be non-negative: | 1127 // Num constants may be negative, while literals must be non-negative: |
(...skipping 12 matching lines...) Expand all Loading... |
1140 return new Literal(value); | 1140 return new Literal(value); |
1141 } | 1141 } |
1142 | 1142 |
1143 List<Expression> visitExpressions(List<ConstantExpression> expressions, | 1143 List<Expression> visitExpressions(List<ConstantExpression> expressions, |
1144 BuilderContext<Statement> context) { | 1144 BuilderContext<Statement> context) { |
1145 return expressions.map((expression) => visit(expression, context)) | 1145 return expressions.map((expression) => visit(expression, context)) |
1146 .toList(growable: false); | 1146 .toList(growable: false); |
1147 } | 1147 } |
1148 | 1148 |
1149 @override | 1149 @override |
1150 Expression visitPrimitive(PrimitiveConstantExpression exp, | 1150 Expression visitBool(BoolConstantExpression exp, |
1151 BuilderContext<Statement> context) { | 1151 BuilderContext<Statement> context) { |
1152 return handlePrimitiveConstant(exp.value); | 1152 return handlePrimitiveConstant(exp.value); |
1153 } | 1153 } |
1154 | 1154 |
| 1155 @override |
| 1156 Expression visitInt(IntConstantExpression exp, |
| 1157 BuilderContext<Statement> context) { |
| 1158 return handlePrimitiveConstant(exp.value); |
| 1159 } |
| 1160 |
| 1161 @override |
| 1162 Expression visitDouble(DoubleConstantExpression exp, |
| 1163 BuilderContext<Statement> context) { |
| 1164 return handlePrimitiveConstant(exp.value); |
| 1165 } |
| 1166 |
| 1167 @override |
| 1168 Expression visitString(StringConstantExpression exp, |
| 1169 BuilderContext<Statement> context) { |
| 1170 return handlePrimitiveConstant(exp.value); |
| 1171 } |
| 1172 |
| 1173 @override |
| 1174 Expression visitNull(NullConstantExpression exp, |
| 1175 BuilderContext<Statement> context) { |
| 1176 return handlePrimitiveConstant(exp.value); |
| 1177 } |
| 1178 |
1155 /// Given a negative num constant, returns the corresponding positive | 1179 /// Given a negative num constant, returns the corresponding positive |
1156 /// literal wrapped by a unary minus operator. | 1180 /// literal wrapped by a unary minus operator. |
1157 Expression negatedLiteral(NumConstantValue constant) { | 1181 Expression negatedLiteral(NumConstantValue constant) { |
1158 assert(constant.primitiveValue.isNegative); | 1182 assert(constant.primitiveValue.isNegative); |
1159 NumConstantValue positiveConstant; | 1183 NumConstantValue positiveConstant; |
1160 if (constant.isInt) { | 1184 if (constant.isInt) { |
1161 positiveConstant = new IntConstantValue(-constant.primitiveValue); | 1185 positiveConstant = new IntConstantValue(-constant.primitiveValue); |
1162 } else if (constant.isDouble) { | 1186 } else if (constant.isDouble) { |
1163 positiveConstant = new DoubleConstantValue(-constant.primitiveValue); | 1187 positiveConstant = new DoubleConstantValue(-constant.primitiveValue); |
1164 } else { | 1188 } else { |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1209 constructorName: name, | 1233 constructorName: name, |
1210 isConst: true) | 1234 isConst: true) |
1211 ..constructor = constructor | 1235 ..constructor = constructor |
1212 ..dartType = exp.type; | 1236 ..dartType = exp.type; |
1213 } | 1237 } |
1214 | 1238 |
1215 @override | 1239 @override |
1216 Expression visitConcatenate(ConcatenateConstantExpression exp, | 1240 Expression visitConcatenate(ConcatenateConstantExpression exp, |
1217 BuilderContext<Statement> context) { | 1241 BuilderContext<Statement> context) { |
1218 | 1242 |
1219 return new StringConcat(visitExpressions(exp.arguments, context)); | 1243 return new StringConcat(visitExpressions(exp.expressions, context)); |
1220 } | 1244 } |
1221 | 1245 |
1222 @override | 1246 @override |
1223 Expression visitSymbol(SymbolConstantExpression exp, | 1247 Expression visitSymbol(SymbolConstantExpression exp, |
1224 BuilderContext<Statement> context) { | 1248 BuilderContext<Statement> context) { |
1225 return new LiteralSymbol(exp.name); | 1249 return new LiteralSymbol(exp.name); |
1226 } | 1250 } |
1227 | 1251 |
1228 @override | 1252 @override |
1229 Expression visitType(TypeConstantExpression exp, | 1253 Expression visitType(TypeConstantExpression exp, |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1272 } else { | 1296 } else { |
1273 return exp.falseExp.accept(this); | 1297 return exp.falseExp.accept(this); |
1274 } | 1298 } |
1275 } | 1299 } |
1276 | 1300 |
1277 @override | 1301 @override |
1278 Expression visitUnary(UnaryConstantExpression exp, | 1302 Expression visitUnary(UnaryConstantExpression exp, |
1279 BuilderContext<Statement> context) { | 1303 BuilderContext<Statement> context) { |
1280 return handlePrimitiveConstant(exp.value); | 1304 return handlePrimitiveConstant(exp.value); |
1281 } | 1305 } |
| 1306 |
| 1307 @override |
| 1308 Expression visitNamed(NamedArgumentReference exp, |
| 1309 BuilderContext<Statement> context) { |
| 1310 throw new UnsupportedError("ConstantEmitter.visitNamed"); |
| 1311 } |
| 1312 |
| 1313 @override |
| 1314 Expression visitPositional(PositionalArgumentReference exp, |
| 1315 BuilderContext<Statement> context) { |
| 1316 throw new UnsupportedError("ConstantEmitter.visitPositional"); |
| 1317 } |
| 1318 |
| 1319 @override |
| 1320 Expression visitBoolFromEnvironment( |
| 1321 BoolFromEnvironmentConstantExpression exp, |
| 1322 BuilderContext<Statement> context) { |
| 1323 return handlePrimitiveConstant(exp.value); |
| 1324 } |
| 1325 |
| 1326 @override |
| 1327 Expression visitIntFromEnvironment( |
| 1328 IntFromEnvironmentConstantExpression exp, |
| 1329 BuilderContext<Statement> context) { |
| 1330 return handlePrimitiveConstant(exp.value); |
| 1331 } |
| 1332 |
| 1333 @override |
| 1334 Expression visitStringFromEnvironment( |
| 1335 StringFromEnvironmentConstantExpression exp, |
| 1336 BuilderContext<Statement> context) { |
| 1337 return handlePrimitiveConstant(exp.value); |
| 1338 } |
1282 } | 1339 } |
1283 | 1340 |
1284 /// Moves function parameters into a separate variable if one of its uses is | 1341 /// Moves function parameters into a separate variable if one of its uses is |
1285 /// shadowed by an inner function parameter. | 1342 /// shadowed by an inner function parameter. |
1286 /// This artifact is necessary because function parameters cannot be renamed. | 1343 /// This artifact is necessary because function parameters cannot be renamed. |
1287 class UnshadowParameters extends tree.RecursiveVisitor { | 1344 class UnshadowParameters extends tree.RecursiveVisitor { |
1288 | 1345 |
1289 /// Maps parameter names to their bindings. | 1346 /// Maps parameter names to their bindings. |
1290 Map<String, tree.Variable> environment = <String, tree.Variable>{}; | 1347 Map<String, tree.Variable> environment = <String, tree.Variable>{}; |
1291 | 1348 |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1359 : super(name, ElementKind.VARIABLE, enclosingElement, variables, null); | 1416 : super(name, ElementKind.VARIABLE, enclosingElement, variables, null); |
1360 | 1417 |
1361 ExecutableElement get executableContext => enclosingElement; | 1418 ExecutableElement get executableContext => enclosingElement; |
1362 | 1419 |
1363 ExecutableElement get memberContext => executableContext.memberContext; | 1420 ExecutableElement get memberContext => executableContext.memberContext; |
1364 | 1421 |
1365 bool get isLocal => true; | 1422 bool get isLocal => true; |
1366 | 1423 |
1367 LibraryElement get implementationLibrary => enclosingElement.library; | 1424 LibraryElement get implementationLibrary => enclosingElement.library; |
1368 } | 1425 } |
OLD | NEW |