| 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 dev_compiler.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/options.dart'; |
| 21 import 'package:dev_compiler/src/report.dart'; | 21 import 'package:dev_compiler/src/report.dart'; |
| 22 import 'package:dev_compiler/src/utils.dart' as utils; | 22 import 'package:dev_compiler/src/utils.dart' as utils; |
| 23 import 'ast_builder.dart'; | 23 import 'ast_builder.dart'; |
| 24 import 'code_generator.dart' as codegenerator; | 24 import 'code_generator.dart' as codegenerator; |
| 25 import 'reify_coercions.dart' as reifier; | 25 import 'reify_coercions.dart' as reifier; |
| 26 | 26 |
| 27 final _log = new logger.Logger('ddc.dartgenerator'); | 27 final _log = new logger.Logger('dev_compiler.dart_codegen'); |
| 28 | 28 |
| 29 class DdcRuntime { | 29 class DevCompilerRuntime { |
| 30 Identifier _ddcRuntimeId = AstBuilder.identifierFromString("DDC\$RT"); | 30 Identifier _runtimeId = AstBuilder.identifierFromString("DEVC\$RT"); |
| 31 | 31 |
| 32 Identifier _castId; | 32 Identifier _castId; |
| 33 Identifier _typeToTypeId; | 33 Identifier _typeToTypeId; |
| 34 Identifier _wrapId; | 34 Identifier _wrapId; |
| 35 | 35 |
| 36 DdcRuntime() { | 36 DevCompilerRuntime() { |
| 37 _castId = _prefixId(AstBuilder.identifierFromString("cast")); | 37 _castId = _prefixId(AstBuilder.identifierFromString("cast")); |
| 38 _typeToTypeId = _prefixId(AstBuilder.identifierFromString("type")); | 38 _typeToTypeId = _prefixId(AstBuilder.identifierFromString("type")); |
| 39 _wrapId = _prefixId(AstBuilder.identifierFromString("wrap")); | 39 _wrapId = _prefixId(AstBuilder.identifierFromString("wrap")); |
| 40 } | 40 } |
| 41 | 41 |
| 42 String get importString { | 42 String get importString { |
| 43 var name = _ddcRuntimeId; | 43 var name = _runtimeId; |
| 44 var uri = "package:dev_compiler/runtime/dart_logging_runtime.dart"; | 44 var uri = "package:dev_compiler/runtime/dart_logging_runtime.dart"; |
| 45 return "import '$uri' as $name;"; | 45 return "import '$uri' as $name;"; |
| 46 } | 46 } |
| 47 | 47 |
| 48 Identifier _prefixId(Identifier id) => | 48 Identifier _prefixId(Identifier id) => |
| 49 AstBuilder.prefixedIdentifier(_ddcRuntimeId, id); | 49 AstBuilder.prefixedIdentifier(_runtimeId, id); |
| 50 | 50 |
| 51 Identifier runtimeId(RuntimeOperation oper) { | 51 Identifier runtimeId(RuntimeOperation oper) { |
| 52 if (oper.operation == "cast") return _castId; | 52 if (oper.operation == "cast") return _castId; |
| 53 if (oper.operation == "wrap") return _wrapId; | 53 if (oper.operation == "wrap") return _wrapId; |
| 54 if (oper.operation == "type") return _typeToTypeId; | 54 if (oper.operation == "type") return _typeToTypeId; |
| 55 assert(false); | 55 assert(false); |
| 56 return null; | 56 return null; |
| 57 } | 57 } |
| 58 | 58 |
| 59 Expression runtimeOperation(RuntimeOperation oper) { | 59 Expression runtimeOperation(RuntimeOperation oper) { |
| (...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 269 } | 269 } |
| 270 } | 270 } |
| 271 | 271 |
| 272 // TODO(leafp) Not sure if this is the right way to generate | 272 // TODO(leafp) Not sure if this is the right way to generate |
| 273 // Dart source going forward, but it's a quick way to get started. | 273 // Dart source going forward, but it's a quick way to get started. |
| 274 class UnitGenerator extends UnitGeneratorCommon with ConversionVisitor<Object> { | 274 class UnitGenerator extends UnitGeneratorCommon with ConversionVisitor<Object> { |
| 275 CompilationUnit unit; | 275 CompilationUnit unit; |
| 276 final java_core.PrintWriter _out; | 276 final java_core.PrintWriter _out; |
| 277 final String outDir; | 277 final String outDir; |
| 278 Set<LibraryElement> _extraImports; | 278 Set<LibraryElement> _extraImports; |
| 279 final ddc = new DdcRuntime(); | 279 final _runtime = new DevCompilerRuntime(); |
| 280 LibraryElement _currentLibrary; | 280 LibraryElement _currentLibrary; |
| 281 bool _qualifyNames = true; | 281 bool _qualifyNames = true; |
| 282 Set<Identifier> _newIdentifiers; | 282 Set<Identifier> _newIdentifiers; |
| 283 | 283 |
| 284 UnitGenerator(this.unit, java_core.PrintWriter out, String this.outDir, | 284 UnitGenerator(this.unit, java_core.PrintWriter out, String this.outDir, |
| 285 this._extraImports, this._newIdentifiers) | 285 this._extraImports, this._newIdentifiers) |
| 286 : _out = out, | 286 : _out = out, |
| 287 super(out) { | 287 super(out) { |
| 288 _currentLibrary = unit.element.enclosingElement; | 288 _currentLibrary = unit.element.enclosingElement; |
| 289 final UnitImportResolver r = new UnitImportResolver( | 289 final UnitImportResolver r = new UnitImportResolver( |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 330 | 330 |
| 331 // Rewrite the import directives with additional library imports | 331 // Rewrite the import directives with additional library imports |
| 332 // covering the inferred types added in as part of this pass. | 332 // covering the inferred types added in as part of this pass. |
| 333 void _visitDirectives(String prefix, List<Directive> directives) { | 333 void _visitDirectives(String prefix, List<Directive> directives) { |
| 334 _qualifyNames = false; | 334 _qualifyNames = false; |
| 335 var ds = splitDirectives(directives); | 335 var ds = splitDirectives(directives); |
| 336 visitListWithSeparatorAndPrefix(prefix, ds['library'], " "); | 336 visitListWithSeparatorAndPrefix(prefix, ds['library'], " "); |
| 337 if (ds['library'].length != 0) { | 337 if (ds['library'].length != 0) { |
| 338 assert(ds['partof'].length == 0); | 338 assert(ds['partof'].length == 0); |
| 339 var es = buildExtraImportDirectives(); | 339 var es = buildExtraImportDirectives(); |
| 340 es.add(ddc.importString); | 340 es.add(_runtime.importString); |
| 341 es.forEach(outputln); | 341 es.forEach(outputln); |
| 342 } | 342 } |
| 343 visitListWithSeparatorAndPrefix(prefix, ds['partof'], " "); | 343 visitListWithSeparatorAndPrefix(prefix, ds['partof'], " "); |
| 344 visitListWithSeparatorAndPrefix(prefix, ds['import'], " "); | 344 visitListWithSeparatorAndPrefix(prefix, ds['import'], " "); |
| 345 visitListWithSeparatorAndPrefix(prefix, ds['export'], " "); | 345 visitListWithSeparatorAndPrefix(prefix, ds['export'], " "); |
| 346 visitListWithSeparatorAndPrefix(prefix, ds['part'], " "); | 346 visitListWithSeparatorAndPrefix(prefix, ds['part'], " "); |
| 347 _qualifyNames = true; | 347 _qualifyNames = true; |
| 348 } | 348 } |
| 349 | 349 |
| 350 @override | 350 @override |
| 351 Object visitNode(AstNode node) { | 351 Object visitNode(AstNode node) { |
| 352 if (node != null) { | 352 if (node != null) { |
| 353 node.visitChildren(this); | 353 node.visitChildren(this); |
| 354 } | 354 } |
| 355 return null; | 355 return null; |
| 356 } | 356 } |
| 357 | 357 |
| 358 @override | 358 @override |
| 359 Object visitRuntimeOperation(RuntimeOperation oper) { | 359 Object visitRuntimeOperation(RuntimeOperation oper) { |
| 360 var e = ddc.runtimeOperation(oper); | 360 var e = _runtime.runtimeOperation(oper); |
| 361 e.accept(this); | 361 e.accept(this); |
| 362 return null; | 362 return null; |
| 363 } | 363 } |
| 364 | 364 |
| 365 @override | 365 @override |
| 366 Object visitAsExpression(AsExpression node) { | 366 Object visitAsExpression(AsExpression node) { |
| 367 _log.severe("Unlowered as expression"); | 367 _log.severe("Unlowered as expression"); |
| 368 assert(false); | 368 assert(false); |
| 369 return null; | 369 return null; |
| 370 } | 370 } |
| (...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 488 void generateUnit(CompilationUnit unit, LibraryInfo info, String libraryDir) { | 488 void generateUnit(CompilationUnit unit, LibraryInfo info, String libraryDir) { |
| 489 var uri = unit.element.source.uri; | 489 var uri = unit.element.source.uri; |
| 490 _log.fine("Emitting original unit " + uri.toString()); | 490 _log.fine("Emitting original unit " + uri.toString()); |
| 491 FileWriter out = new FileWriter( | 491 FileWriter out = new FileWriter( |
| 492 options, path.join(libraryDir, '${uri.pathSegments.last}')); | 492 options, path.join(libraryDir, '${uri.pathSegments.last}')); |
| 493 var unitGen = new EmptyUnitGenerator(unit, out); | 493 var unitGen = new EmptyUnitGenerator(unit, out); |
| 494 unitGen.generate(); | 494 unitGen.generate(); |
| 495 out.finalize(); | 495 out.finalize(); |
| 496 } | 496 } |
| 497 } | 497 } |
| OLD | NEW |