Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(820)

Side by Side Diff: pkg/dev_compiler/lib/src/compiler/compiler.dart

Issue 2423313002: Emulate compiling a source file in the context of an existing library. Add --debugger-compile flag … (Closed)
Patch Set: Code review comments. Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2016, 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 import 'dart:collection' show HashSet, Queue; 5 import 'dart:collection' show HashSet, Queue;
6 import 'dart:convert' show BASE64, JSON, UTF8; 6 import 'dart:convert' show BASE64, JSON, UTF8;
7 import 'dart:io' show File; 7 import 'dart:io' show File;
8 import 'package:analyzer/dart/element/element.dart' show LibraryElement; 8 import 'package:analyzer/dart/element/element.dart' show LibraryElement;
9 import 'package:analyzer/analyzer.dart' 9 import 'package:analyzer/analyzer.dart'
10 show AnalysisError, CompilationUnit, ErrorSeverity; 10 show AnalysisError, CompilationUnit, ErrorSeverity;
11 import 'package:analyzer/file_system/file_system.dart' show ResourceProvider; 11 import 'package:analyzer/file_system/file_system.dart' show ResourceProvider;
12 import 'package:analyzer/src/generated/engine.dart' 12 import 'package:analyzer/src/generated/engine.dart'
13 show AnalysisContext, AnalysisEngine; 13 show AnalysisContext, AnalysisEngine;
14 import 'package:analyzer/src/generated/source.dart' show DartUriResolver; 14 import 'package:analyzer/src/generated/source.dart' show DartUriResolver;
15 import 'package:analyzer/src/generated/source_io.dart' 15 import 'package:analyzer/src/generated/source_io.dart'
16 show Source, SourceKind, UriResolver; 16 show Source, SourceKind, UriResolver;
17 import 'package:analyzer/src/summary/package_bundle_reader.dart' 17 import 'package:analyzer/src/summary/package_bundle_reader.dart'
18 show InSummarySource, InputPackagesResultProvider, SummaryDataStore; 18 show InSummarySource, InputPackagesResultProvider, SummaryDataStore;
19 import 'package:analyzer/src/error/codes.dart' show StaticTypeWarningCode;
19 import 'package:args/args.dart' show ArgParser, ArgResults; 20 import 'package:args/args.dart' show ArgParser, ArgResults;
20 import 'package:args/src/usage_exception.dart' show UsageException; 21 import 'package:args/src/usage_exception.dart' show UsageException;
21 import 'package:func/func.dart' show Func1; 22 import 'package:func/func.dart' show Func1;
22 import 'package:path/path.dart' as path; 23 import 'package:path/path.dart' as path;
23 import 'package:source_maps/source_maps.dart'; 24 import 'package:source_maps/source_maps.dart';
24 25
25 import '../analyzer/context.dart' 26 import '../analyzer/context.dart'
26 show 27 show
27 AnalyzerOptions, 28 AnalyzerOptions,
28 createAnalysisContext, 29 createAnalysisContext,
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
90 91
91 var context = createAnalysisContext(); 92 var context = createAnalysisContext();
92 context.sourceFactory = srcFactory; 93 context.sourceFactory = srcFactory;
93 context.typeProvider = sdkResolver.dartSdk.context.typeProvider; 94 context.typeProvider = sdkResolver.dartSdk.context.typeProvider;
94 context.resultProvider = 95 context.resultProvider =
95 new InputPackagesResultProvider(context, summaryData); 96 new InputPackagesResultProvider(context, summaryData);
96 97
97 return new ModuleCompiler.withContext(context, summaryData); 98 return new ModuleCompiler.withContext(context, summaryData);
98 } 99 }
99 100
101 bool _isFatalError(AnalysisError e, CompilerOptions options) {
102 if (errorSeverity(context, e) != ErrorSeverity.ERROR) return false;
103
104 // These errors are not fatal in the REPL compile mode as we
105 // allow access to private members across library boundaries
106 // and those accesses will show up as undefined members unless
107 // additional analyzer changes are made to support them.
108 // TODO(jacobr): consider checking that the identifier name
109 // referenced by the error is private.
110 return !options.replCompile ||
111 (e.errorCode != StaticTypeWarningCode.UNDEFINED_GETTER &&
112 e.errorCode != StaticTypeWarningCode.UNDEFINED_SETTER &&
113 e.errorCode != StaticTypeWarningCode.UNDEFINED_METHOD);
114 }
115
100 /// Compiles a single Dart build unit into a JavaScript module. 116 /// Compiles a single Dart build unit into a JavaScript module.
101 /// 117 ///
102 /// *Warning* - this may require resolving the entire world. 118 /// *Warning* - this may require resolving the entire world.
103 /// If that is not desired, the analysis context must be pre-configured using 119 /// If that is not desired, the analysis context must be pre-configured using
104 /// summaries before calling this method. 120 /// summaries before calling this method.
105 JSModuleFile compile(BuildUnit unit, CompilerOptions options) { 121 JSModuleFile compile(BuildUnit unit, CompilerOptions options) {
106 var trees = <CompilationUnit>[]; 122 var trees = <CompilationUnit>[];
107 var errors = <AnalysisError>[]; 123 var errors = <AnalysisError>[];
108 124
109 var librariesToCompile = new Queue<LibraryElement>(); 125 var librariesToCompile = new Queue<LibraryElement>();
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 trees.add(tree); 168 trees.add(tree);
153 errors.addAll(context.computeErrors(library.source)); 169 errors.addAll(context.computeErrors(library.source));
154 170
155 for (var part in library.parts) { 171 for (var part in library.parts) {
156 trees.add(context.resolveCompilationUnit(part.source, library)); 172 trees.add(context.resolveCompilationUnit(part.source, library));
157 errors.addAll(context.computeErrors(part.source)); 173 errors.addAll(context.computeErrors(part.source));
158 } 174 }
159 } 175 }
160 176
161 sortErrors(context, errors); 177 sortErrors(context, errors);
178
162 var messages = <String>[]; 179 var messages = <String>[];
163 for (var e in errors) { 180 for (var e in errors) {
164 var m = formatError(context, e); 181 var m = formatError(context, e);
165 if (m != null) messages.add(m); 182 if (m != null) messages.add(m);
166 } 183 }
167 184
168 if (!options.unsafeForceCompile && 185 if (!options.unsafeForceCompile &&
169 errors.any((e) => errorSeverity(context, e) == ErrorSeverity.ERROR)) { 186 errors.any((e) => _isFatalError(e, options))) {
170 return new JSModuleFile.invalid(unit.name, messages, options); 187 return new JSModuleFile.invalid(unit.name, messages, options);
171 } 188 }
172
173 var codeGenerator = 189 var codeGenerator =
174 new CodeGenerator(context, summaryData, options, _extensionTypes); 190 new CodeGenerator(context, summaryData, options, _extensionTypes);
175 return codeGenerator.compile(unit, trees, messages); 191 return codeGenerator.compile(unit, trees, messages);
176 } 192 }
177 } 193 }
178 194
179 class CompilerOptions { 195 class CompilerOptions {
180 /// Whether to emit the source mapping file. 196 /// Whether to emit the source mapping file.
181 /// 197 ///
182 /// This supports debugging the original source code instead of the generated 198 /// This supports debugging the original source code instead of the generated
(...skipping 14 matching lines...) Expand all
197 213
198 /// The file extension for summaries. 214 /// The file extension for summaries.
199 final String summaryExtension; 215 final String summaryExtension;
200 216
201 /// Whether to preserve metdata only accessible via mirrors 217 /// Whether to preserve metdata only accessible via mirrors
202 final bool emitMetadata; 218 final bool emitMetadata;
203 219
204 /// Whether to force compilation of code with static errors. 220 /// Whether to force compilation of code with static errors.
205 final bool unsafeForceCompile; 221 final bool unsafeForceCompile;
206 222
223 /// Whether to compile code in a more permissive REPL mode allowing access
224 /// to private members across library boundaries.
225 final bool replCompile;
226
207 /// Whether to emit Closure Compiler-friendly code. 227 /// Whether to emit Closure Compiler-friendly code.
208 final bool closure; 228 final bool closure;
209 229
210 /// Hoist the types at instance creation sites 230 /// Hoist the types at instance creation sites
211 final bool hoistInstanceCreation; 231 final bool hoistInstanceCreation;
212 232
213 /// Hoist types from class signatures 233 /// Hoist types from class signatures
214 final bool hoistSignatureTypes; 234 final bool hoistSignatureTypes;
215 235
216 /// Name types in type tests 236 /// Name types in type tests
(...skipping 24 matching lines...) Expand all
241 /// source maps. 261 /// source maps.
242 final Map<String, String> bazelMapping; 262 final Map<String, String> bazelMapping;
243 263
244 const CompilerOptions( 264 const CompilerOptions(
245 {this.sourceMap: true, 265 {this.sourceMap: true,
246 this.sourceMapComment: true, 266 this.sourceMapComment: true,
247 this.inlineSourceMap: false, 267 this.inlineSourceMap: false,
248 this.summarizeApi: true, 268 this.summarizeApi: true,
249 this.summaryExtension: 'sum', 269 this.summaryExtension: 'sum',
250 this.unsafeForceCompile: false, 270 this.unsafeForceCompile: false,
271 this.replCompile: false,
251 this.emitMetadata: false, 272 this.emitMetadata: false,
252 this.closure: false, 273 this.closure: false,
253 this.destructureNamedParams: false, 274 this.destructureNamedParams: false,
254 this.hoistInstanceCreation: true, 275 this.hoistInstanceCreation: true,
255 this.hoistSignatureTypes: false, 276 this.hoistSignatureTypes: false,
256 this.nameTypeTests: true, 277 this.nameTypeTests: true,
257 this.hoistTypeTests: true, 278 this.hoistTypeTests: true,
258 this.useAngular2Whitelist: false, 279 this.useAngular2Whitelist: false,
259 this.bazelMapping: const {}}); 280 this.bazelMapping: const {}});
260 281
261 CompilerOptions.fromArguments(ArgResults args) 282 CompilerOptions.fromArguments(ArgResults args)
262 : sourceMap = args['source-map'], 283 : sourceMap = args['source-map'],
263 sourceMapComment = args['source-map-comment'], 284 sourceMapComment = args['source-map-comment'],
264 inlineSourceMap = args['inline-source-map'], 285 inlineSourceMap = args['inline-source-map'],
265 summarizeApi = args['summarize'], 286 summarizeApi = args['summarize'],
266 summaryExtension = args['summary-extension'], 287 summaryExtension = args['summary-extension'],
267 unsafeForceCompile = args['unsafe-force-compile'], 288 unsafeForceCompile = args['unsafe-force-compile'],
289 replCompile = args['repl-compile'],
268 emitMetadata = args['emit-metadata'], 290 emitMetadata = args['emit-metadata'],
269 closure = args['closure-experimental'], 291 closure = args['closure-experimental'],
270 destructureNamedParams = args['destructure-named-params'], 292 destructureNamedParams = args['destructure-named-params'],
271 hoistInstanceCreation = args['hoist-instance-creation'], 293 hoistInstanceCreation = args['hoist-instance-creation'],
272 hoistSignatureTypes = args['hoist-signature-types'], 294 hoistSignatureTypes = args['hoist-signature-types'],
273 nameTypeTests = args['name-type-tests'], 295 nameTypeTests = args['name-type-tests'],
274 hoistTypeTests = args['hoist-type-tests'], 296 hoistTypeTests = args['hoist-type-tests'],
275 useAngular2Whitelist = args['unsafe-angular2-whitelist'], 297 useAngular2Whitelist = args['unsafe-angular2-whitelist'],
276 bazelMapping = _parseBazelMappings(args['bazel-mapping']); 298 bazelMapping = _parseBazelMappings(args['bazel-mapping']);
277 299
(...skipping 18 matching lines...) Expand all
296 ..addFlag('closure-experimental', 318 ..addFlag('closure-experimental',
297 help: 'emit Closure Compiler-friendly code (experimental)', 319 help: 'emit Closure Compiler-friendly code (experimental)',
298 defaultsTo: false) 320 defaultsTo: false)
299 ..addFlag('destructure-named-params', 321 ..addFlag('destructure-named-params',
300 help: 'Destructure named parameters', defaultsTo: false, hide: true) 322 help: 'Destructure named parameters', defaultsTo: false, hide: true)
301 ..addFlag('unsafe-force-compile', 323 ..addFlag('unsafe-force-compile',
302 help: 'Compile code even if it has errors. ಠ_ಠ\n' 324 help: 'Compile code even if it has errors. ಠ_ಠ\n'
303 'This has undefined behavior!', 325 'This has undefined behavior!',
304 defaultsTo: false, 326 defaultsTo: false,
305 hide: true) 327 hide: true)
328 ..addFlag('repl-compile',
329 help: 'Compile code more permissively when in REPL mode allowing '
330 'access to private members across library boundaries.',
331 defaultsTo: false,
332 hide: true)
306 ..addFlag('hoist-instance-creation', 333 ..addFlag('hoist-instance-creation',
307 help: 'Hoist the class type from generic instance creations', 334 help: 'Hoist the class type from generic instance creations',
308 defaultsTo: true, 335 defaultsTo: true,
309 hide: true) 336 hide: true)
310 ..addFlag('hoist-signature-types', 337 ..addFlag('hoist-signature-types',
311 help: 'Hoist types from class signatures', 338 help: 'Hoist types from class signatures',
312 defaultsTo: false, 339 defaultsTo: false,
313 hide: true) 340 hide: true)
314 ..addFlag('name-type-tests', 341 ..addFlag('name-type-tests',
315 help: 'Name types used in type tests', defaultsTo: true, hide: true) 342 help: 'Name types used in type tests', defaultsTo: true, hide: true)
(...skipping 189 matching lines...) Expand 10 before | Expand all | Expand 10 after
505 // Fall back to a relative path. 532 // Fall back to a relative path.
506 return path.toUri(path.relative(path.fromUri(uri), from: dir)).toString(); 533 return path.toUri(path.relative(path.fromUri(uri), from: dir)).toString();
507 } 534 }
508 535
509 for (int i = 0; i < list.length; i++) { 536 for (int i = 0; i < list.length; i++) {
510 list[i] = transformUri(list[i]); 537 list[i] = transformUri(list[i]);
511 } 538 }
512 map['file'] = transformUri(map['file']); 539 map['file'] = transformUri(map['file']);
513 return map; 540 return map;
514 } 541 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698