| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 dart2js.ir_builder; | 5 library dart2js.ir_builder; |
| 6 | 6 |
| 7 import '../constants/expressions.dart'; | 7 import '../constants/expressions.dart'; |
| 8 import '../constants/values.dart' show PrimitiveConstant; | 8 import '../constants/values.dart' show PrimitiveConstantValue; |
| 9 import '../dart_backend/dart_backend.dart' show DartBackend; | 9 import '../dart_backend/dart_backend.dart' show DartBackend; |
| 10 import '../dart_types.dart'; | 10 import '../dart_types.dart'; |
| 11 import '../dart2jslib.dart'; | 11 import '../dart2jslib.dart'; |
| 12 import '../elements/elements.dart'; | 12 import '../elements/elements.dart'; |
| 13 import '../source_file.dart'; | 13 import '../source_file.dart'; |
| 14 import '../tree/tree.dart' as ast; | 14 import '../tree/tree.dart' as ast; |
| 15 import '../scanner/scannerlib.dart' show Token, isUserDefinableOperator; | 15 import '../scanner/scannerlib.dart' show Token, isUserDefinableOperator; |
| 16 import '../universe/universe.dart' show SelectorKind; | 16 import '../universe/universe.dart' show SelectorKind; |
| 17 import 'cps_ir_nodes.dart' as ir; | 17 import 'cps_ir_nodes.dart' as ir; |
| 18 | 18 |
| (...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 295 } | 295 } |
| 296 | 296 |
| 297 ir.Primitive continueWithExpression(ir.Expression build(ir.Continuation k)) { | 297 ir.Primitive continueWithExpression(ir.Expression build(ir.Continuation k)) { |
| 298 ir.Parameter v = new ir.Parameter(null); | 298 ir.Parameter v = new ir.Parameter(null); |
| 299 ir.Continuation k = new ir.Continuation([v]); | 299 ir.Continuation k = new ir.Continuation([v]); |
| 300 ir.Expression expression = build(k); | 300 ir.Expression expression = build(k); |
| 301 add(new ir.LetCont(k, expression)); | 301 add(new ir.LetCont(k, expression)); |
| 302 return v; | 302 return v; |
| 303 } | 303 } |
| 304 | 304 |
| 305 ir.Constant makeConst(ConstExp exp) { | 305 ir.Constant makeConst(ConstantExpression exp) { |
| 306 return new ir.Constant(exp); | 306 return new ir.Constant(exp); |
| 307 } | 307 } |
| 308 | 308 |
| 309 ir.Constant makePrimConst(PrimitiveConstant value) { | 309 ir.Constant makePrimConst(PrimitiveConstantValue value) { |
| 310 return makeConst(new PrimitiveConstExp(value)); | 310 return makeConst(new PrimitiveConstantExpression(value)); |
| 311 } | 311 } |
| 312 | 312 |
| 313 // TODO(johnniwinther): Build constants directly through [ConstExp] when these | 313 // TODO(johnniwinther): Build constants directly through [ConstExp] when these |
| 314 // are created from analyzer2dart. | 314 // are created from analyzer2dart. |
| 315 ir.Node buildPrimConst(PrimitiveConstant constant) { | 315 ir.Node buildPrimConst(PrimitiveConstantValue constant) { |
| 316 assert(isOpen); | 316 assert(isOpen); |
| 317 ir.Node prim = makePrimConst(constant); | 317 ir.Node prim = makePrimConst(constant); |
| 318 add(new ir.LetPrim(prim)); | 318 add(new ir.LetPrim(prim)); |
| 319 return prim; | 319 return prim; |
| 320 } | 320 } |
| 321 | 321 |
| 322 /// Create an integer literal. | 322 /// Create an integer literal. |
| 323 ir.Constant buildIntegerLiteral(int value) { | 323 ir.Constant buildIntegerLiteral(int value) { |
| 324 return buildPrimConst(constantSystem.createInt(value)); | 324 return buildPrimConst(constantSystem.createInt(value)); |
| 325 } | 325 } |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 372 _current = null; | 372 _current = null; |
| 373 } | 373 } |
| 374 | 374 |
| 375 /// Create a [ir.FunctionDefinition] for [element] using [_root] as the body. | 375 /// Create a [ir.FunctionDefinition] for [element] using [_root] as the body. |
| 376 /// | 376 /// |
| 377 /// Parameters must be created before the construction of the body using | 377 /// Parameters must be created before the construction of the body using |
| 378 /// [createParameter]. | 378 /// [createParameter]. |
| 379 ir.FunctionDefinition buildFunctionDefinition( | 379 ir.FunctionDefinition buildFunctionDefinition( |
| 380 FunctionElement element, | 380 FunctionElement element, |
| 381 List<ConstDeclaration> constants, | 381 List<ConstDeclaration> constants, |
| 382 List<ConstExp> defaults) { | 382 List<ConstantExpression> defaults) { |
| 383 if (!element.isAbstract) { | 383 if (!element.isAbstract) { |
| 384 ensureReturn(); | 384 ensureReturn(); |
| 385 return new ir.FunctionDefinition( | 385 return new ir.FunctionDefinition( |
| 386 element, returnContinuation, _parameters, _root, constants, defaults); | 386 element, returnContinuation, _parameters, _root, constants, defaults); |
| 387 } else { | 387 } else { |
| 388 assert(invariant(element, _root == null, | 388 assert(invariant(element, _root == null, |
| 389 message: "Non-empty body for abstract method $element: $_root")); | 389 message: "Non-empty body for abstract method $element: $_root")); |
| 390 assert(invariant(element, constants.isEmpty, | 390 assert(invariant(element, constants.isEmpty, |
| 391 message: "Local constants for abstract method $element: $constants")); | 391 message: "Local constants for abstract method $element: $constants")); |
| 392 return new ir.FunctionDefinition.abstract( | 392 return new ir.FunctionDefinition.abstract( |
| (...skipping 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 530 closureLocals.visit(function); | 530 closureLocals.visit(function); |
| 531 | 531 |
| 532 _root = _current = null; | 532 _root = _current = null; |
| 533 | 533 |
| 534 FunctionSignature signature = element.functionSignature; | 534 FunctionSignature signature = element.functionSignature; |
| 535 signature.orderedForEachParameter((ParameterElement parameterElement) { | 535 signature.orderedForEachParameter((ParameterElement parameterElement) { |
| 536 createParameter(parameterElement, | 536 createParameter(parameterElement, |
| 537 isClosureVariable: isClosureVariable(parameterElement)); | 537 isClosureVariable: isClosureVariable(parameterElement)); |
| 538 }); | 538 }); |
| 539 | 539 |
| 540 List<ConstExp> defaults = new List<ConstExp>(); | 540 List<ConstantExpression> defaults = new List<ConstantExpression>(); |
| 541 signature.orderedOptionalParameters.forEach((ParameterElement element) { | 541 signature.orderedOptionalParameters.forEach((ParameterElement element) { |
| 542 defaults.add(getConstantForVariable(element)); | 542 defaults.add(getConstantForVariable(element)); |
| 543 }); | 543 }); |
| 544 | 544 |
| 545 visit(function.body); | 545 visit(function.body); |
| 546 return buildFunctionDefinition(element, localConstants, defaults); | 546 return buildFunctionDefinition(element, localConstants, defaults); |
| 547 } | 547 } |
| 548 | 548 |
| 549 ir.Primitive visit(ast.Node node) => node.accept(this); | 549 ir.Primitive visit(ast.Node node) => node.accept(this); |
| 550 | 550 |
| (...skipping 582 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1133 return null; | 1133 return null; |
| 1134 } | 1134 } |
| 1135 | 1135 |
| 1136 ir.Primitive visitVariableDefinitions(ast.VariableDefinitions node) { | 1136 ir.Primitive visitVariableDefinitions(ast.VariableDefinitions node) { |
| 1137 assert(isOpen); | 1137 assert(isOpen); |
| 1138 if (node.modifiers.isConst) { | 1138 if (node.modifiers.isConst) { |
| 1139 for (ast.SendSet definition in node.definitions.nodes) { | 1139 for (ast.SendSet definition in node.definitions.nodes) { |
| 1140 assert(!definition.arguments.isEmpty); | 1140 assert(!definition.arguments.isEmpty); |
| 1141 assert(definition.arguments.tail.isEmpty); | 1141 assert(definition.arguments.tail.isEmpty); |
| 1142 VariableElement element = elements[definition]; | 1142 VariableElement element = elements[definition]; |
| 1143 ConstExp value = getConstantForVariable(element); | 1143 ConstantExpression value = getConstantForVariable(element); |
| 1144 localConstants.add(new ConstDeclaration(element, value)); | 1144 localConstants.add(new ConstDeclaration(element, value)); |
| 1145 } | 1145 } |
| 1146 } else { | 1146 } else { |
| 1147 for (ast.Node definition in node.definitions.nodes) { | 1147 for (ast.Node definition in node.definitions.nodes) { |
| 1148 Element element = elements[definition]; | 1148 Element element = elements[definition]; |
| 1149 ir.Primitive initialValue; | 1149 ir.Primitive initialValue; |
| 1150 // Definitions are either SendSets if there is an initializer, or | 1150 // Definitions are either SendSets if there is an initializer, or |
| 1151 // Identifiers if there is no initializer. | 1151 // Identifiers if there is no initializer. |
| 1152 if (definition is ast.SendSet) { | 1152 if (definition is ast.SendSet) { |
| 1153 assert(!definition.arguments.isEmpty); | 1153 assert(!definition.arguments.isEmpty); |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1254 ir.Primitive visitLiteralNull(ast.LiteralNull node) { | 1254 ir.Primitive visitLiteralNull(ast.LiteralNull node) { |
| 1255 assert(isOpen); | 1255 assert(isOpen); |
| 1256 return translateConstant(node); | 1256 return translateConstant(node); |
| 1257 } | 1257 } |
| 1258 | 1258 |
| 1259 ir.Primitive visitLiteralString(ast.LiteralString node) { | 1259 ir.Primitive visitLiteralString(ast.LiteralString node) { |
| 1260 assert(isOpen); | 1260 assert(isOpen); |
| 1261 return translateConstant(node); | 1261 return translateConstant(node); |
| 1262 } | 1262 } |
| 1263 | 1263 |
| 1264 ConstExp getConstantForNode(ast.Node node) { | 1264 ConstantExpression getConstantForNode(ast.Node node) { |
| 1265 ConstExp constant = | 1265 ConstantExpression constant = |
| 1266 compiler.backend.constantCompilerTask.compileNode(node, elements); | 1266 compiler.backend.constantCompilerTask.compileNode(node, elements); |
| 1267 assert(invariant(node, constant != null, | 1267 assert(invariant(node, constant != null, |
| 1268 message: 'No constant computed for $node')); | 1268 message: 'No constant computed for $node')); |
| 1269 return constant; | 1269 return constant; |
| 1270 } | 1270 } |
| 1271 | 1271 |
| 1272 ConstExp getConstantForVariable(VariableElement element) { | 1272 ConstantExpression getConstantForVariable(VariableElement element) { |
| 1273 ConstExp constant = | 1273 ConstantExpression constant = |
| 1274 compiler.backend.constants.getConstantForVariable(element); | 1274 compiler.backend.constants.getConstantForVariable(element); |
| 1275 assert(invariant(element, constant != null, | 1275 assert(invariant(element, constant != null, |
| 1276 message: 'No constant computed for $element')); | 1276 message: 'No constant computed for $element')); |
| 1277 return constant; | 1277 return constant; |
| 1278 } | 1278 } |
| 1279 | 1279 |
| 1280 ir.Primitive visitLiteralList(ast.LiteralList node) { | 1280 ir.Primitive visitLiteralList(ast.LiteralList node) { |
| 1281 assert(isOpen); | 1281 assert(isOpen); |
| 1282 if (node.isConst) { | 1282 if (node.isConst) { |
| 1283 return translateConstant(node); | 1283 return translateConstant(node); |
| (...skipping 568 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1852 var it = node.parts.iterator; | 1852 var it = node.parts.iterator; |
| 1853 while (it.moveNext()) { | 1853 while (it.moveNext()) { |
| 1854 ast.StringInterpolationPart part = it.current; | 1854 ast.StringInterpolationPart part = it.current; |
| 1855 arguments.add(visit(part.expression)); | 1855 arguments.add(visit(part.expression)); |
| 1856 arguments.add(visitLiteralString(part.string)); | 1856 arguments.add(visitLiteralString(part.string)); |
| 1857 } | 1857 } |
| 1858 return continueWithExpression( | 1858 return continueWithExpression( |
| 1859 (k) => new ir.ConcatenateStrings(k, arguments)); | 1859 (k) => new ir.ConcatenateStrings(k, arguments)); |
| 1860 } | 1860 } |
| 1861 | 1861 |
| 1862 ir.Primitive translateConstant(ast.Node node, [ConstExp constant]) { | 1862 ir.Primitive translateConstant(ast.Node node, [ConstantExpression constant]) { |
| 1863 assert(isOpen); | 1863 assert(isOpen); |
| 1864 if (constant == null) { | 1864 if (constant == null) { |
| 1865 constant = getConstantForNode(node); | 1865 constant = getConstantForNode(node); |
| 1866 } | 1866 } |
| 1867 ir.Primitive primitive = makeConst(constant); | 1867 ir.Primitive primitive = makeConst(constant); |
| 1868 add(new ir.LetPrim(primitive)); | 1868 add(new ir.LetPrim(primitive)); |
| 1869 return primitive; | 1869 return primitive; |
| 1870 } | 1870 } |
| 1871 | 1871 |
| 1872 ir.FunctionDefinition makeSubFunction(ast.FunctionExpression node) { | 1872 ir.FunctionDefinition makeSubFunction(ast.FunctionExpression node) { |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1953 } | 1953 } |
| 1954 | 1954 |
| 1955 visitFunctionExpression(ast.FunctionExpression node) { | 1955 visitFunctionExpression(ast.FunctionExpression node) { |
| 1956 FunctionElement oldFunction = currentFunction; | 1956 FunctionElement oldFunction = currentFunction; |
| 1957 currentFunction = elements[node]; | 1957 currentFunction = elements[node]; |
| 1958 visit(node.body); | 1958 visit(node.body); |
| 1959 currentFunction = oldFunction; | 1959 currentFunction = oldFunction; |
| 1960 } | 1960 } |
| 1961 | 1961 |
| 1962 } | 1962 } |
| OLD | NEW |