| OLD | NEW |
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 ddc.src.codegen.dart_codegen; | 5 library ddc.src.codegen.dart_codegen; |
| 6 | 6 |
| 7 import 'dart:io' show File; | 7 import 'dart:io' show File; |
| 8 | 8 |
| 9 import 'package:analyzer/analyzer.dart' as analyzer; | 9 import 'package:analyzer/analyzer.dart' as analyzer; |
| 10 import 'package:analyzer/src/generated/ast.dart'; | 10 import 'package:analyzer/src/generated/ast.dart'; |
| 11 import 'package:analyzer/src/generated/element.dart'; | 11 import 'package:analyzer/src/generated/element.dart'; |
| 12 import 'package:analyzer/src/generated/java_core.dart' as java_core; | 12 import 'package:analyzer/src/generated/java_core.dart' as java_core; |
| 13 import 'package:analyzer/src/generated/scanner.dart' show Token; | 13 import 'package:analyzer/src/generated/scanner.dart' show Token; |
| 14 import 'package:dart_style/dart_style.dart'; | 14 import 'package:dart_style/dart_style.dart'; |
| 15 import 'package:logging/logging.dart' as logger; | 15 import 'package:logging/logging.dart' as logger; |
| 16 import 'package:path/path.dart' as path; | 16 import 'package:path/path.dart' as path; |
| 17 | 17 |
| 18 import 'package:dev_compiler/src/info.dart'; | 18 import 'package:dev_compiler/src/info.dart'; |
| 19 import 'package:dev_compiler/src/checker/rules.dart'; | 19 import 'package:dev_compiler/src/checker/rules.dart'; |
| 20 import 'package:dev_compiler/src/options.dart'; |
| 20 import 'package:dev_compiler/src/report.dart'; | 21 import 'package:dev_compiler/src/report.dart'; |
| 21 import 'package:dev_compiler/src/utils.dart' as utils; | 22 import 'package:dev_compiler/src/utils.dart' as utils; |
| 22 import 'ast_builder.dart'; | 23 import 'ast_builder.dart'; |
| 23 import 'code_generator.dart' as codegenerator; | 24 import 'code_generator.dart' as codegenerator; |
| 24 import 'reify_coercions.dart' as reifier; | 25 import 'reify_coercions.dart' as reifier; |
| 25 | 26 |
| 26 final _log = new logger.Logger('ddc.dartgenerator'); | 27 final _log = new logger.Logger('ddc.dartgenerator'); |
| 27 | 28 |
| 28 class DdcRuntime { | 29 class DdcRuntime { |
| 29 Identifier _ddcRuntimeId = AstBuilder.identifierFromString("DDC\$RT"); | 30 Identifier _ddcRuntimeId = AstBuilder.identifierFromString("DDC\$RT"); |
| (...skipping 27 matching lines...) Expand all Loading... |
| 57 | 58 |
| 58 Expression runtimeOperation(RuntimeOperation oper) { | 59 Expression runtimeOperation(RuntimeOperation oper) { |
| 59 var id = runtimeId(oper); | 60 var id = runtimeId(oper); |
| 60 var args = oper.arguments; | 61 var args = oper.arguments; |
| 61 return AstBuilder.application(id, args); | 62 return AstBuilder.application(id, args); |
| 62 } | 63 } |
| 63 } | 64 } |
| 64 | 65 |
| 65 // TODO(leafp) This is kind of a hack, but it works for now. | 66 // TODO(leafp) This is kind of a hack, but it works for now. |
| 66 class FileWriter extends java_core.PrintStringWriter { | 67 class FileWriter extends java_core.PrintStringWriter { |
| 67 bool _format; | 68 final CompilerOptions options; |
| 68 String _path; | 69 String _path; |
| 69 FileWriter(this._format, this._path); | 70 FileWriter(this.options, this._path); |
| 70 int indent = 0; | 71 int indent = 0; |
| 72 int withinInterpolationExpression = 0; |
| 73 bool insideForLoop = false; |
| 71 | 74 |
| 72 void print(x) { | 75 void print(x) { |
| 73 if (_format) { | 76 if (!options.cheapTestFormat) { |
| 74 super.print(x); | 77 super.print(x); |
| 75 return; | 78 return; |
| 76 } | 79 } |
| 77 | 80 |
| 78 if (x == '{') { | 81 switch (x) { |
| 79 indent++; | 82 case '{': |
| 80 x = '{\n${" " * indent}'; | 83 indent++; |
| 81 } else if (x == ';') { | 84 x = '{\n${" " * indent}'; |
| 82 x = ';\n${" " * indent}'; | 85 break; |
| 83 } else if (x == '}') { | 86 case ';': |
| 84 indent--; | 87 if (!insideForLoop) { |
| 85 x = '}\n${" " * indent}'; | 88 x = ';\n${" " * indent}'; |
| 89 } |
| 90 break; |
| 91 case 'for (': |
| 92 insideForLoop = true; |
| 93 break; |
| 94 case ') ': |
| 95 insideForLoop = false; |
| 96 break; |
| 97 case r'${': |
| 98 withinInterpolationExpression++; |
| 99 break; |
| 100 case '}': |
| 101 if (withinInterpolationExpression > 0) { |
| 102 withinInterpolationExpression--; |
| 103 } else { |
| 104 indent--; |
| 105 x = '}\n${" " * indent}'; |
| 106 } |
| 107 break; |
| 86 } | 108 } |
| 87 super.print(x); | 109 super.print(x); |
| 88 } | 110 } |
| 89 | 111 |
| 90 void finalize() { | 112 void finalize() { |
| 91 String s = toString(); | 113 String s = toString(); |
| 92 if (_format) { | 114 if (options.formatOutput && !options.cheapTestFormat) { |
| 93 DartFormatter d = new DartFormatter(); | 115 DartFormatter d = new DartFormatter(); |
| 94 try { | 116 try { |
| 95 _log.fine("Formatting file $_path "); | 117 _log.fine("Formatting file $_path "); |
| 96 s = d.format(s, uri: _path); | 118 s = d.format(s, uri: _path); |
| 97 } catch (e) { | 119 } catch (e) { |
| 98 _log.severe("Failed to format $_path: " + e.toString()); | 120 _log.severe("Failed to format $_path: " + e.toString()); |
| 99 } | 121 } |
| 100 } | 122 } |
| 101 _log.fine("Writing file $_path"); | 123 _log.fine("Writing file $_path"); |
| 102 new File(_path).writeAsStringSync(s); | 124 new File(_path).writeAsStringSync(s); |
| (...skipping 280 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 383 output(id.name); | 405 output(id.name); |
| 384 return null; | 406 return null; |
| 385 } | 407 } |
| 386 | 408 |
| 387 void generate() { | 409 void generate() { |
| 388 visitCompilationUnit(unit); | 410 visitCompilationUnit(unit); |
| 389 } | 411 } |
| 390 } | 412 } |
| 391 | 413 |
| 392 class DartGenerator extends codegenerator.CodeGenerator { | 414 class DartGenerator extends codegenerator.CodeGenerator { |
| 393 bool _format; | 415 final CompilerOptions options; |
| 394 reifier.VariableManager _vm; | 416 reifier.VariableManager _vm; |
| 395 Set<LibraryElement> _extraImports; | 417 Set<LibraryElement> _extraImports; |
| 396 TypeRules _rules; | 418 TypeRules _rules; |
| 397 | 419 |
| 398 DartGenerator(String outDir, Uri root, TypeRules rules, this._format) | 420 DartGenerator(String outDir, Uri root, TypeRules rules, this.options) |
| 399 : _rules = rules, | 421 : _rules = rules, |
| 400 super(outDir, root, rules); | 422 super(outDir, root, rules); |
| 401 | 423 |
| 402 void generateUnit(CompilationUnit unit, LibraryInfo info, String libraryDir) { | 424 void generateUnit(CompilationUnit unit, LibraryInfo info, String libraryDir) { |
| 403 var uri = unit.element.source.uri; | 425 var uri = unit.element.source.uri; |
| 404 _log.fine("Generating unit " + uri.toString()); | 426 _log.fine("Generating unit " + uri.toString()); |
| 405 FileWriter out = new FileWriter( | 427 FileWriter out = new FileWriter( |
| 406 _format, path.join(libraryDir, '${uri.pathSegments.last}')); | 428 options, path.join(libraryDir, '${uri.pathSegments.last}')); |
| 407 var tm = new reifier.TypeManager(_vm); | 429 var tm = new reifier.TypeManager(_vm); |
| 408 var r = new reifier.UnitCoercionReifier(tm, _vm, _rules); | 430 var r = new reifier.UnitCoercionReifier(tm, _vm, _rules); |
| 409 r.reify(unit); | 431 r.reify(unit); |
| 410 var ids = new Set<Identifier>.from(tm.addedTypes.map((tn) => tn.name)); | 432 var ids = new Set<Identifier>.from(tm.addedTypes.map((tn) => tn.name)); |
| 411 var unitGen = new UnitGenerator(unit, out, outDir, _extraImports, ids); | 433 var unitGen = new UnitGenerator(unit, out, outDir, _extraImports, ids); |
| 412 unitGen.generate(); | 434 unitGen.generate(); |
| 413 out.finalize(); | 435 out.finalize(); |
| 414 } | 436 } |
| 415 | 437 |
| 416 void _generateLibrary(Iterable<CompilationUnit> units, LibraryInfo info, | 438 void _generateLibrary(Iterable<CompilationUnit> units, LibraryInfo info, |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 451 void output(String s) => _out.print(s); | 473 void output(String s) => _out.print(s); |
| 452 void outputln(String s) => _out.println(s); | 474 void outputln(String s) => _out.println(s); |
| 453 | 475 |
| 454 void generate() { | 476 void generate() { |
| 455 unit.visitChildren(this); | 477 unit.visitChildren(this); |
| 456 } | 478 } |
| 457 } | 479 } |
| 458 | 480 |
| 459 // This class emits the code unchanged, for comparison purposes. | 481 // This class emits the code unchanged, for comparison purposes. |
| 460 class EmptyDartGenerator extends codegenerator.CodeGenerator { | 482 class EmptyDartGenerator extends codegenerator.CodeGenerator { |
| 461 bool _format; | 483 final CompilerOptions options; |
| 462 | 484 |
| 463 EmptyDartGenerator(String outDir, Uri root, TypeRules rules, this._format) | 485 EmptyDartGenerator(String outDir, Uri root, TypeRules rules, this.options) |
| 464 : super(outDir, root, rules); | 486 : super(outDir, root, rules); |
| 465 | 487 |
| 466 void generateUnit(CompilationUnit unit, LibraryInfo info, String libraryDir) { | 488 void generateUnit(CompilationUnit unit, LibraryInfo info, String libraryDir) { |
| 467 var uri = unit.element.source.uri; | 489 var uri = unit.element.source.uri; |
| 468 _log.fine("Emitting original unit " + uri.toString()); | 490 _log.fine("Emitting original unit " + uri.toString()); |
| 469 FileWriter out = new FileWriter( | 491 FileWriter out = new FileWriter( |
| 470 _format, path.join(libraryDir, '${uri.pathSegments.last}')); | 492 options, path.join(libraryDir, '${uri.pathSegments.last}')); |
| 471 var unitGen = new EmptyUnitGenerator(unit, out); | 493 var unitGen = new EmptyUnitGenerator(unit, out); |
| 472 unitGen.generate(); | 494 unitGen.generate(); |
| 473 out.finalize(); | 495 out.finalize(); |
| 474 } | 496 } |
| 475 } | 497 } |
| OLD | NEW |