@@ -6,30 +6,23 @@ import 'dart:collection' show HashMap, HashSet;
6
6
import 'dart:math' show min, max;
7
7
8
8
import 'package:analyzer/analyzer.dart' hide ConstantEvaluator;
9
+ import 'package:analyzer/dart/analysis/declared_variables.dart' ;
9
10
import 'package:analyzer/dart/ast/standard_ast_factory.dart' ;
10
11
import 'package:analyzer/dart/ast/standard_resolution_map.dart' ;
11
12
import 'package:analyzer/dart/ast/token.dart' show Token, TokenType;
12
13
import 'package:analyzer/dart/element/element.dart' ;
13
14
import 'package:analyzer/dart/element/type.dart' ;
14
15
import 'package:analyzer/src/dart/ast/token.dart' show StringToken;
15
16
import 'package:analyzer/src/dart/element/element.dart' ;
17
+ import 'package:analyzer/src/dart/element/handle.dart' ;
16
18
import 'package:analyzer/src/dart/element/type.dart' ;
17
- import 'package:analyzer/src/dart/sdk/sdk.dart' ;
18
19
import 'package:analyzer/src/generated/constant.dart'
19
20
show DartObject, DartObjectImpl;
20
- import 'package:analyzer/src/generated/engine.dart' show AnalysisContext;
21
21
import 'package:analyzer/src/generated/resolver.dart'
22
22
show TypeProvider, NamespaceBuilder;
23
23
import 'package:analyzer/src/generated/type_system.dart'
24
24
show StrongTypeSystemImpl;
25
- import 'package:analyzer/src/summary/idl.dart' show UnlinkedUnit;
26
- import 'package:analyzer/src/summary/link.dart' as summary_link;
27
25
import 'package:analyzer/src/summary/package_bundle_reader.dart' ;
28
- import 'package:analyzer/src/summary/summarize_ast.dart'
29
- show serializeAstUnlinked;
30
- import 'package:analyzer/src/summary/summarize_elements.dart'
31
- show PackageBundleAssembler;
32
- import 'package:analyzer/src/summary/summary_sdk.dart' ;
33
26
import 'package:analyzer/src/task/strong/ast_properties.dart' ;
34
27
import 'package:path/path.dart' as path;
35
28
import 'package:source_span/source_span.dart' show SourceLocation;
@@ -43,6 +36,7 @@ import '../js_ast/js_ast.dart' as JS;
43
36
import '../js_ast/js_ast.dart' show js;
44
37
import '../js_ast/source_map_printer.dart' show NodeEnd, NodeSpan, HoverComment;
45
38
import 'ast_builder.dart' ;
39
+ import 'driver.dart' ;
46
40
import 'element_helpers.dart' ;
47
41
import 'error_helpers.dart' ;
48
42
import 'extension_types.dart' show ExtensionTypeSet;
@@ -73,7 +67,6 @@ import 'type_utilities.dart';
73
67
class CodeGenerator extends Object
74
68
with NullableTypeInference , SharedCompiler <LibraryElement >
75
69
implements AstVisitor <JS .Node > {
76
- final AnalysisContext context;
77
70
final SummaryDataStore summaryData;
78
71
79
72
final CompilerOptions options;
@@ -200,38 +193,45 @@ class CodeGenerator extends Object
200
193
201
194
final _usedCovariantPrivateMembers = HashSet <ExecutableElement >();
202
195
203
- CodeGenerator (AnalysisContext c, this .summaryData, this .options,
204
- this ._extensionTypes, this .errors)
205
- : context = c,
206
- rules = StrongTypeSystemImpl (c.typeProvider),
207
- types = c.typeProvider,
208
- _asyncStreamIterator = getClass (c, 'dart:async' , 'StreamIterator' ).type,
209
- _coreIdentical = _getLibrary (c, 'dart:core' )
196
+ final DeclaredVariables declaredVariables;
197
+
198
+ CodeGenerator (LinkedAnalysisDriver driver, this .types, this .summaryData,
199
+ this .options, this ._extensionTypes, this .errors)
200
+ : rules = StrongTypeSystemImpl (types),
201
+ declaredVariables = driver.declaredVariables,
202
+ _asyncStreamIterator =
203
+ driver.getClass ('dart:async' , 'StreamIterator' ).type,
204
+ _coreIdentical = driver
205
+ .getLibrary ('dart:core' )
210
206
.publicNamespace
211
207
.get ('identical' ) as FunctionElement ,
212
- _jsArray = getClass (c, 'dart:_interceptors' , 'JSArray' ),
213
- interceptorClass = getClass (c, 'dart:_interceptors' , 'Interceptor' ),
214
- coreLibrary = _getLibrary (c, 'dart:core' ),
215
- boolClass = getClass (c, 'dart:core' , 'bool' ),
216
- intClass = getClass (c, 'dart:core' , 'int' ),
217
- doubleClass = getClass (c, 'dart:core' , 'double' ),
218
- numClass = getClass (c, 'dart:core' , 'num' ),
219
- nullClass = getClass (c, 'dart:core' , 'Null' ),
220
- objectClass = getClass (c, 'dart:core' , 'Object' ),
221
- stringClass = getClass (c, 'dart:core' , 'String' ),
222
- functionClass = getClass (c, 'dart:core' , 'Function' ),
223
- privateSymbolClass = getClass (c, 'dart:_js_helper' , 'PrivateSymbol' ),
208
+ _jsArray = driver.getClass ('dart:_interceptors' , 'JSArray' ),
209
+ interceptorClass = driver.getClass ('dart:_interceptors' , 'Interceptor' ),
210
+ coreLibrary = driver.getLibrary ('dart:core' ),
211
+ boolClass = driver.getClass ('dart:core' , 'bool' ),
212
+ intClass = driver.getClass ('dart:core' , 'int' ),
213
+ doubleClass = driver.getClass ('dart:core' , 'double' ),
214
+ numClass = driver.getClass ('dart:core' , 'num' ),
215
+ nullClass = driver.getClass ('dart:core' , 'Null' ),
216
+ objectClass = driver.getClass ('dart:core' , 'Object' ),
217
+ stringClass = driver.getClass ('dart:core' , 'String' ),
218
+ functionClass = driver.getClass ('dart:core' , 'Function' ),
219
+ privateSymbolClass =
220
+ driver.getClass ('dart:_js_helper' , 'PrivateSymbol' ),
224
221
linkedHashMapImplType =
225
- getClass (c, 'dart:_js_helper' , 'LinkedMap' ).type,
222
+ driver. getClass ('dart:_js_helper' , 'LinkedMap' ).type,
226
223
identityHashMapImplType =
227
- getClass (c, 'dart:_js_helper' , 'IdentityMap' ).type,
228
- linkedHashSetImplType = getClass (c, 'dart:collection' , '_HashSet' ).type,
224
+ driver.getClass ('dart:_js_helper' , 'IdentityMap' ).type,
225
+ linkedHashSetImplType =
226
+ driver.getClass ('dart:collection' , '_HashSet' ).type,
229
227
identityHashSetImplType =
230
- getClass (c, 'dart:collection' , '_IdentityHashSet' ).type,
231
- syncIterableType = getClass (c, 'dart:_js_helper' , 'SyncIterable' ).type,
232
- asyncStarImplType = getClass (c, 'dart:async' , '_AsyncStarImpl' ).type,
233
- dartJSLibrary = _getLibrary (c, 'dart:js' ) {
234
- jsTypeRep = JSTypeRep (rules, c);
228
+ driver.getClass ('dart:collection' , '_IdentityHashSet' ).type,
229
+ syncIterableType =
230
+ driver.getClass ('dart:_js_helper' , 'SyncIterable' ).type,
231
+ asyncStarImplType =
232
+ driver.getClass ('dart:async' , '_AsyncStarImpl' ).type,
233
+ dartJSLibrary = driver.getLibrary ('dart:js' ) {
234
+ jsTypeRep = JSTypeRep (rules, driver);
235
235
}
236
236
237
237
LibraryElement get currentLibrary => _currentElement.library;
@@ -248,92 +248,12 @@ class CodeGenerator extends Object
248
248
///
249
249
/// Takes the metadata for the build unit, as well as resolved trees and
250
250
/// errors, and computes the output module code and optionally the source map.
251
- JSModuleFile compile (List <CompilationUnit > compilationUnits) {
251
+ JS . Program compile (List <CompilationUnit > compilationUnits) {
252
252
_libraryRoot = options.libraryRoot;
253
253
if (! _libraryRoot.endsWith (path.separator)) {
254
254
_libraryRoot += path.separator;
255
255
}
256
256
257
- var name = options.moduleName;
258
- invalidModule () =>
259
- JSModuleFile .invalid (name, formatErrors (context, errors), options);
260
-
261
- if (! options.unsafeForceCompile && errors.any (_isFatalError)) {
262
- return invalidModule ();
263
- }
264
-
265
- try {
266
- var module = _emitModule (compilationUnits, name);
267
- if (! options.unsafeForceCompile && errors.any (_isFatalError)) {
268
- return invalidModule ();
269
- }
270
-
271
- var dartApiSummary = _summarizeModule (compilationUnits);
272
- return JSModuleFile (
273
- name, formatErrors (context, errors), options, module, dartApiSummary);
274
- } catch (e) {
275
- if (errors.any (_isFatalError)) {
276
- // Force compilation failed. Suppress the exception and report
277
- // the static errors instead.
278
- assert (options.unsafeForceCompile);
279
- return invalidModule ();
280
- }
281
- rethrow ;
282
- }
283
- }
284
-
285
- bool _isFatalError (AnalysisError e) {
286
- if (errorSeverity (context, e) != ErrorSeverity .ERROR ) return false ;
287
-
288
- // These errors are not fatal in the REPL compile mode as we
289
- // allow access to private members across library boundaries
290
- // and those accesses will show up as undefined members unless
291
- // additional analyzer changes are made to support them.
292
- // TODO(jacobr): consider checking that the identifier name
293
- // referenced by the error is private.
294
- return ! options.replCompile ||
295
- (e.errorCode != StaticTypeWarningCode .UNDEFINED_GETTER &&
296
- e.errorCode != StaticTypeWarningCode .UNDEFINED_SETTER &&
297
- e.errorCode != StaticTypeWarningCode .UNDEFINED_METHOD );
298
- }
299
-
300
- List <int > _summarizeModule (List <CompilationUnit > units) {
301
- if (! options.summarizeApi) return null ;
302
-
303
- if (! units.any ((u) => u.declaredElement.source.isInSystemLibrary)) {
304
- var sdk = context.sourceFactory.dartSdk;
305
- summaryData.addBundle (
306
- null ,
307
- sdk is SummaryBasedDartSdk
308
- ? sdk.bundle
309
- : (sdk as FolderBasedDartSdk ).getSummarySdkBundle ());
310
- }
311
-
312
- var assembler = PackageBundleAssembler ();
313
-
314
- var uriToUnit = Map <String , UnlinkedUnit >.fromIterables (
315
- units.map ((u) => u.declaredElement.source.uri.toString ()),
316
- units.map ((unit) {
317
- var unlinked = serializeAstUnlinked (unit);
318
- assembler.addUnlinkedUnit (unit.declaredElement.source, unlinked);
319
- return unlinked;
320
- }));
321
-
322
- summary_link
323
- .link (
324
- uriToUnit.keys.toSet (),
325
- (uri) => summaryData.linkedMap[uri],
326
- (uri) => summaryData.unlinkedMap[uri] ?? uriToUnit[uri],
327
- context.declaredVariables.get )
328
- .forEach (assembler.addLinkedLibrary);
329
-
330
- var bundle = assembler.assemble ();
331
- // Preserve only API-level information in the summary.
332
- bundle.flushInformative ();
333
- return bundle.toBuffer ();
334
- }
335
-
336
- JS .Program _emitModule (List <CompilationUnit > compilationUnits, String name) {
337
257
if (moduleItems.isNotEmpty) {
338
258
throw StateError ('Can only call emitModule once.' );
339
259
}
@@ -388,7 +308,7 @@ class CodeGenerator extends Object
388
308
389
309
// Collect all class/type Element -> Node mappings
390
310
// in case we need to forward declare any classes.
391
- _declarationNodes = HashMap <TypeDefiningElement , AstNode >. identity ();
311
+ _declarationNodes = HashMap <TypeDefiningElement , AstNode >();
392
312
for (var unit in compilationUnits) {
393
313
for (var declaration in unit.declarations) {
394
314
var element = declaration.declaredElement;
@@ -398,7 +318,7 @@ class CodeGenerator extends Object
398
318
}
399
319
}
400
320
if (compilationUnits.isNotEmpty) {
401
- _constants = ConstFieldVisitor (context ,
321
+ _constants = ConstFieldVisitor (types, declaredVariables ,
402
322
dummySource: resolutionMap
403
323
.elementDeclaredByCompilationUnit (compilationUnits.first)
404
324
.source);
@@ -430,7 +350,7 @@ class CodeGenerator extends Object
430
350
items.add (js.statement ('const # = #;' , [id, value]));
431
351
});
432
352
433
- _emitDebuggerExtensionInfo (name );
353
+ _emitDebuggerExtensionInfo (options.moduleName );
434
354
435
355
// Discharge the type table cache variables and
436
356
// hoisted definitions.
@@ -638,7 +558,7 @@ class CodeGenerator extends Object
638
558
void _declareBeforeUse (TypeDefiningElement e) {
639
559
if (e == null ) return ;
640
560
641
- if (_topLevelClass != null && identical ( _currentElement, _topLevelClass) ) {
561
+ if (_topLevelClass != null && _currentElement == _topLevelClass) {
642
562
// If the item is from our library, try to emit it now.
643
563
_emitTypeDeclaration (e);
644
564
}
@@ -2147,7 +2067,7 @@ class CodeGenerator extends Object
2147
2067
var extMembers = _classProperties.extensionMethods;
2148
2068
var staticMethods = < JS .Property > [];
2149
2069
var instanceMethods = < JS .Property > [];
2150
- var classMethods = classElem.methods.where ((m) => ! m.isAbstract). toList ( );
2070
+ var classMethods = List . of ( classElem.methods.where ((m) => ! m.isAbstract));
2151
2071
for (var m in mockMembers.values) {
2152
2072
if (m is MethodElement ) classMethods.add (m);
2153
2073
}
@@ -2327,8 +2247,11 @@ class CodeGenerator extends Object
2327
2247
if (! element.parameters.any (_isCovariant)) return element.type;
2328
2248
2329
2249
var parameters = element.parameters
2330
- .map ((p) => ParameterElementImpl .synthetic (p.name,
2331
- _isCovariant (p) ? objectClass.type : p.type, p.parameterKind))
2250
+ .map ((p) => ParameterElementImpl .synthetic (
2251
+ p.name,
2252
+ // ignore: deprecated_member_use
2253
+ _isCovariant (p) ? objectClass.type : p.type,
2254
+ p.parameterKind))
2332
2255
.toList ();
2333
2256
2334
2257
var function = FunctionElementImpl ("" , - 1 )
@@ -4983,8 +4906,8 @@ class CodeGenerator extends Object
4983
4906
4984
4907
variable ?? = JS .TemporaryId (name);
4985
4908
4986
- var idElement = TemporaryVariableElement . forNode (id, variable)
4987
- ..enclosingElement = _currentElement;
4909
+ var idElement =
4910
+ TemporaryVariableElement . forNode (id, variable, _currentElement) ;
4988
4911
id.staticElement = idElement;
4989
4912
id.staticType = type;
4990
4913
setIsDynamicInvoke (id, dynamicInvoke ?? type.isDynamic);
@@ -6508,17 +6431,19 @@ JS.LiteralString _propertyName(String name) => js.string(name, "'");
6508
6431
class TemporaryVariableElement extends LocalVariableElementImpl {
6509
6432
final JS .Expression jsVariable;
6510
6433
6511
- TemporaryVariableElement .forNode (Identifier name, this .jsVariable)
6512
- : super .forNode (name);
6434
+ TemporaryVariableElement .forNode (
6435
+ Identifier name, this .jsVariable, Element enclosingElement)
6436
+ : super .forNode (name) {
6437
+ this .enclosingElement = enclosingElement is ElementHandle
6438
+ ? enclosingElement.actualElement
6439
+ : enclosingElement;
6440
+ }
6513
6441
6514
6442
int get hashCode => identityHashCode (this );
6515
6443
6516
6444
bool operator == (Object other) => identical (this , other);
6517
6445
}
6518
6446
6519
- LibraryElement _getLibrary (AnalysisContext c, String uri) =>
6520
- c.computeLibraryElement (c.sourceFactory.forUri (uri));
6521
-
6522
6447
/// Returns `true` if [target] is a prefix for a deferred library and [name]
6523
6448
/// is "loadLibrary".
6524
6449
///
0 commit comments