| 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 dart_tree_printer; | 5 library dart_tree_printer; |
| 6 | 6 |
| 7 import '../constants/values.dart' as values; | 7 import '../constants/values.dart' as values; |
| 8 import '../dart_types.dart' as types; | 8 import '../dart_types.dart' as types; |
| 9 import '../dart2jslib.dart' as dart2js; | 9 import '../dart2jslib.dart' as dart2js; |
| 10 import '../elements/elements.dart' as elements; | 10 import '../elements/elements.dart' as elements; |
| 11 import '../tree/tree.dart' as tree; | 11 import '../tree/tree.dart' as tree; |
| 12 import '../scanner/scannerlib.dart'; | 12 import '../scanner/scannerlib.dart'; |
| 13 import '../util/util.dart'; | 13 import '../util/util.dart'; |
| 14 import 'backend_ast_nodes.dart'; | 14 import 'backend_ast_nodes.dart'; |
| 15 import 'backend_ast_emitter.dart' show createTypeAnnotation; | 15 import 'backend_ast_emitter.dart' show createTypeAnnotation; |
| 16 | 16 |
| 17 /// Translates the backend AST to Dart frontend AST. | 17 /// Translates the backend AST to Dart frontend AST. |
| 18 tree.FunctionExpression emit(dart2js.TreeElementMapping treeElements, | 18 tree.Node emit(dart2js.TreeElementMapping treeElements, |
| 19 Node definition) { | 19 ExecutableDefinition definition) { |
| 20 return new TreePrinter(treeElements).makeExpression(definition); | 20 return new TreePrinter(treeElements).makeDefinition(definition); |
| 21 } | 21 } |
| 22 | 22 |
| 23 /// If true, the unparser will insert a coment in front of every function | 23 /// If true, the unparser will insert a coment in front of every function |
| 24 /// it emits. This helps indicate which functions were translated by the new | 24 /// it emits. This helps indicate which functions were translated by the new |
| 25 /// backend. | 25 /// backend. |
| 26 bool INSERT_NEW_BACKEND_COMMENT = | 26 bool INSERT_NEW_BACKEND_COMMENT = |
| 27 const bool.fromEnvironment('USE_NEW_BACKEND', defaultValue: false); | 27 const bool.fromEnvironment('USE_NEW_BACKEND', defaultValue: false); |
| 28 | 28 |
| 29 /// Converts backend ASTs to frontend ASTs. | 29 /// Converts backend ASTs to frontend ASTs. |
| 30 class TreePrinter { | 30 class TreePrinter { |
| 31 dart2js.TreeElementMapping treeElements; | 31 dart2js.TreeElementMapping treeElements; |
| 32 | 32 |
| 33 TreePrinter([this.treeElements]); | 33 TreePrinter([this.treeElements]); |
| 34 | 34 |
| 35 tree.Node makeDefinition(ExecutableDefinition node) { |
| 36 if (node is FieldDefinition) { |
| 37 tree.Node definition; |
| 38 if (node.initializer == null) { |
| 39 definition = makeIdentifier(node.element.name); |
| 40 } else { |
| 41 definition = new tree.SendSet( |
| 42 null, |
| 43 makeIdentifier(node.element.name), |
| 44 new tree.Operator(assignmentToken("=")), |
| 45 singleton(makeExpression(node.initializer))); |
| 46 } |
| 47 setElement(definition, node.element, node); |
| 48 return new tree.VariableDefinitions( |
| 49 null, // TODO(sigurdm): Type |
| 50 makeVarModifiers(useVar: true, |
| 51 isFinal: node.element.isFinal, |
| 52 isStatic: node.element.isStatic, |
| 53 isConst: node.element.isConst), |
| 54 makeList(null, [definition], close: semicolon)); |
| 55 } else if (node is FunctionExpression) { |
| 56 return makeExpression(node); |
| 57 } else { |
| 58 assert(false); |
| 59 return null; |
| 60 } |
| 61 } |
| 62 |
| 35 void setElement(tree.Node node, elements.Element element, source) { | 63 void setElement(tree.Node node, elements.Element element, source) { |
| 36 if (treeElements != null) { | 64 if (treeElements != null) { |
| 37 if (element == null) { | 65 if (element == null) { |
| 38 throw "Missing element from ${source}"; | 66 throw "Missing element from ${source}"; |
| 39 } | 67 } |
| 40 treeElements[node] = element; | 68 treeElements[node] = element; |
| 41 } | 69 } |
| 42 } | 70 } |
| 43 | 71 |
| 44 void setType(tree.Node node, types.DartType type, source) { | 72 void setType(tree.Node node, types.DartType type, source) { |
| (...skipping 366 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 411 result = new tree.FunctionExpression( | 439 result = new tree.FunctionExpression( |
| 412 functionName(exp), | 440 functionName(exp), |
| 413 parameters, | 441 parameters, |
| 414 body, | 442 body, |
| 415 exp.returnType == null || exp.element.isConstructor | 443 exp.returnType == null || exp.element.isConstructor |
| 416 ? null | 444 ? null |
| 417 : makeType(exp.returnType), | 445 : makeType(exp.returnType), |
| 418 makeFunctionModifiers(exp), | 446 makeFunctionModifiers(exp), |
| 419 null, // initializers | 447 null, // initializers |
| 420 getOrSet, // get/set | 448 getOrSet, // get/set |
| 421 null); // async modifier | 449 null); // async modifier |
| 422 setElement(result, exp.element, exp); | 450 elements.Element element = exp.element; |
| 451 if (element != null) setElement(result, element, exp); |
| 423 } else if (exp is Identifier) { | 452 } else if (exp is Identifier) { |
| 424 precedence = CALLEE; | 453 precedence = CALLEE; |
| 425 result = new tree.Send( | 454 result = new tree.Send( |
| 426 makeStaticReceiver(exp.element), | 455 makeStaticReceiver(exp.element), |
| 427 makeIdentifier(exp.name)); | 456 makeIdentifier(exp.name)); |
| 428 setElement(result, exp.element, exp); | 457 setElement(result, exp.element, exp); |
| 429 } else if (exp is Increment) { | 458 } else if (exp is Increment) { |
| 430 Expression lvalue = exp.expression; | 459 Expression lvalue = exp.expression; |
| 431 tree.Node receiver; | 460 tree.Node receiver; |
| 432 tree.Node selector; | 461 tree.Node selector; |
| (...skipping 522 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 955 } | 984 } |
| 956 if (isConst) { | 985 if (isConst) { |
| 957 nodes.add(makeIdentifier('const')); | 986 nodes.add(makeIdentifier('const')); |
| 958 } | 987 } |
| 959 if (isFinal) { | 988 if (isFinal) { |
| 960 nodes.add(makeIdentifier('final')); | 989 nodes.add(makeIdentifier('final')); |
| 961 } | 990 } |
| 962 if (isVar) { | 991 if (isVar) { |
| 963 nodes.add(makeIdentifier('var')); | 992 nodes.add(makeIdentifier('var')); |
| 964 } | 993 } |
| 965 return new tree.Modifiers(makeList('', nodes)); | 994 return new tree.Modifiers(makeList(' ', nodes)); |
| 966 } | 995 } |
| 967 | 996 |
| 968 tree.Modifiers makeVarModifiers({bool isConst: false, | 997 tree.Modifiers makeVarModifiers({bool isConst: false, |
| 969 bool isFinal: false, | 998 bool isFinal: false, |
| 970 bool useVar: false}) { | 999 bool useVar: false, |
| 971 return makeModifiers(isConst: isConst, | 1000 bool isStatic: false}) { |
| 1001 return makeModifiers(isStatic: isStatic, |
| 1002 isConst: isConst, |
| 972 isFinal: isFinal, | 1003 isFinal: isFinal, |
| 973 isVar: useVar && !(isConst || isFinal)); | 1004 isVar: useVar && !(isConst || isFinal)); |
| 974 } | 1005 } |
| 975 | 1006 |
| 976 tree.Modifiers makeFunctionModifiers(FunctionExpression exp) { | 1007 tree.Modifiers makeFunctionModifiers(FunctionExpression exp) { |
| 977 if (exp.element == null) return makeEmptyModifiers(); | 1008 if (exp.element == null) return makeEmptyModifiers(); |
| 978 return makeModifiers(isExternal: exp.element.isExternal, | 1009 return makeModifiers(isExternal: exp.element.isExternal, |
| 979 isStatic: exp.element.isStatic, | 1010 isStatic: exp.element.isStatic, |
| 980 isFactory: exp.element.isFactoryConstructor); | 1011 isFactory: exp.element.isFactoryConstructor); |
| 981 } | 1012 } |
| (...skipping 231 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1213 printStringChunk(chunk.previous), | 1244 printStringChunk(chunk.previous), |
| 1214 node); | 1245 node); |
| 1215 } else { | 1246 } else { |
| 1216 return node; | 1247 return node; |
| 1217 } | 1248 } |
| 1218 } | 1249 } |
| 1219 return printStringChunk(output.chunk); | 1250 return printStringChunk(output.chunk); |
| 1220 } | 1251 } |
| 1221 | 1252 |
| 1222 } | 1253 } |
| OLD | NEW |