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 /// Encapsulates how to invoke the analyzer resolver and overrides how it | 5 /// Encapsulates how to invoke the analyzer resolver and overrides how it |
6 /// computes types on expressions to use our restricted set of types. | 6 /// computes types on expressions to use our restricted set of types. |
7 library dev_compiler.src.checker.resolver; | 7 library dev_compiler.src.checker.resolver; |
8 | 8 |
9 import 'package:analyzer/analyzer.dart'; | 9 import 'package:analyzer/analyzer.dart'; |
10 import 'package:analyzer/file_system/file_system.dart'; | |
11 import 'package:analyzer/file_system/memory_file_system.dart'; | |
12 import 'package:analyzer/src/generated/ast.dart'; | 10 import 'package:analyzer/src/generated/ast.dart'; |
13 import 'package:analyzer/src/generated/element.dart'; | 11 import 'package:analyzer/src/generated/element.dart'; |
14 import 'package:analyzer/src/generated/engine.dart'; | |
15 import 'package:analyzer/src/generated/java_io.dart' show JavaFile; | |
16 import 'package:analyzer/src/generated/resolver.dart'; | 12 import 'package:analyzer/src/generated/resolver.dart'; |
17 import 'package:analyzer/src/generated/sdk_io.dart' show DirectoryBasedDartSdk; | |
18 import 'package:analyzer/src/generated/source.dart' show DartUriResolver; | |
19 import 'package:analyzer/src/generated/source.dart' show Source; | 13 import 'package:analyzer/src/generated/source.dart' show Source; |
20 import 'package:analyzer/src/generated/source_io.dart'; | 14 import 'package:analyzer/src/generated/source_io.dart'; |
21 import 'package:analyzer/src/generated/static_type_analyzer.dart'; | 15 import 'package:analyzer/src/generated/static_type_analyzer.dart'; |
22 import 'package:analyzer/src/generated/utilities_collection.dart' | 16 import 'package:analyzer/src/generated/utilities_collection.dart' |
23 show DirectedGraph; | 17 show DirectedGraph; |
24 import 'package:logging/logging.dart' as logger; | 18 import 'package:logging/logging.dart' as logger; |
25 import 'package:path/path.dart' as path; | |
26 | 19 |
27 import 'package:dev_compiler/src/options.dart'; | 20 import 'package:dev_compiler/src/options.dart'; |
28 import 'package:dev_compiler/src/utils.dart'; | 21 import 'package:dev_compiler/src/utils.dart'; |
29 import 'dart_sdk.dart'; | |
30 import 'multi_package_resolver.dart'; | |
31 | 22 |
32 final _log = new logger.Logger('dev_compiler.src.resolver'); | 23 final _log = new logger.Logger('dev_compiler.src.resolver'); |
33 | 24 |
34 String _implicitEntryHtml(String src) => ''' | |
35 <html> | |
36 <body> | |
37 <script type="application/dart" src="$src"></script> | |
38 </body> | |
39 </html> | |
40 '''; | |
41 | |
42 // TODO(jmesserly): this class can be removed, and converted to some top-level | |
43 // methods that create the AnalysisContext. | |
44 /// Encapsulates a resolver from the analyzer package. | |
45 class TypeResolver { | |
46 final InternalAnalysisContext context; | |
47 | |
48 TypeResolver(DartUriResolver sdkResolver, ResolverOptions options, | |
49 {List otherResolvers}) | |
50 : context = _initContext(options) { | |
51 var resolvers = options.useImplicitHtml | |
52 ? [_createImplicitEntryResolver(options), sdkResolver] | |
53 : [sdkResolver]; | |
54 if (otherResolvers == null) { | |
55 resolvers.add(new FileUriResolver()); | |
56 resolvers.add(options.useMultiPackage | |
57 ? new MultiPackageResolver(options.packagePaths) | |
58 : new PackageUriResolver([new JavaFile(options.packageRoot)])); | |
59 } else { | |
60 resolvers.addAll(otherResolvers); | |
61 } | |
62 context.sourceFactory = new SourceFactory(resolvers); | |
63 } | |
64 | |
65 /// Creates a [TypeResolver] that uses a mock 'dart:' library contents. | |
66 TypeResolver.fromMock( | |
67 Map<String, String> mockSources, ResolverOptions options, | |
68 {UriResolver entryResolver, List otherResolvers}) | |
69 : this( | |
70 new MockDartSdk(mockSources, reportMissing: true).resolver, options, | |
71 otherResolvers: otherResolvers); | |
72 | |
73 /// Creates a [TypeResolver] that uses the SDK at the given [sdkPath]. | |
74 TypeResolver.fromDir(String sdkPath, ResolverOptions options, | |
75 {UriResolver entryResolver, List otherResolvers}) | |
76 : this( | |
77 new DartUriResolver(new DirectoryBasedDartSdk(new JavaFile(sdkPath))), | |
78 options, otherResolvers: otherResolvers); | |
79 } | |
80 | |
81 UriResolver _createImplicitEntryResolver(ResolverOptions options) { | |
82 var entry = path.absolute(ResolverOptions.implicitHtmlFile); | |
83 var src = path.absolute(options.entryPointFile); | |
84 var provider = new MemoryResourceProvider(); | |
85 provider.newFile(entry, _implicitEntryHtml(src)); | |
86 return new ResourceUriResolver(provider); | |
87 } | |
88 | |
89 /// Creates an analysis context that contains our restricted typing rules. | |
90 InternalAnalysisContext _initContext(ResolverOptions options) { | |
91 var analysisOptions = new AnalysisOptionsImpl()..cacheSize = 512; | |
92 AnalysisContextImpl res = AnalysisEngine.instance.createAnalysisContext(); | |
93 res.analysisOptions = analysisOptions; | |
94 res.libraryResolverFactory = | |
95 (context) => new LibraryResolverWithInference(context, options); | |
96 return res; | |
97 } | |
98 | |
99 /// A [LibraryResolver] that performs inference on top-levels and fields based | 25 /// A [LibraryResolver] that performs inference on top-levels and fields based |
100 /// on the value of the initializer, and on fields and methods based on | 26 /// on the value of the initializer, and on fields and methods based on |
101 /// overridden members in super classes. | 27 /// overridden members in super classes. |
102 class LibraryResolverWithInference extends LibraryResolver { | 28 class LibraryResolverWithInference extends LibraryResolver { |
103 final ResolverOptions _options; | 29 final ResolverOptions _options; |
104 | 30 |
105 LibraryResolverWithInference(context, this._options) : super(context); | 31 LibraryResolverWithInference(context, this._options) : super(context); |
106 | 32 |
107 @override | 33 @override |
108 void resolveReferencesAndTypes() { | 34 void resolveReferencesAndTypes() { |
(...skipping 643 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
752 } | 678 } |
753 } | 679 } |
754 | 680 |
755 // Review note: no longer need to override visitFunctionExpression, this is | 681 // Review note: no longer need to override visitFunctionExpression, this is |
756 // handled by the analyzer internally. | 682 // handled by the analyzer internally. |
757 // TODO(vsm): in visitbinaryExpression: check computeStaticReturnType result? | 683 // TODO(vsm): in visitbinaryExpression: check computeStaticReturnType result? |
758 // TODO(vsm): in visitFunctionDeclaration: Should we ever use the expression | 684 // TODO(vsm): in visitFunctionDeclaration: Should we ever use the expression |
759 // type in a (...) => expr or just the written type? | 685 // type in a (...) => expr or just the written type? |
760 | 686 |
761 } | 687 } |
OLD | NEW |