OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 '../common.dart'; | 5 import '../common.dart'; |
6 import '../common_elements.dart'; | 6 import '../common_elements.dart'; |
7 import '../constants/values.dart'; | 7 import '../constants/values.dart'; |
| 8 import '../elements/elements.dart'; |
| 9 import '../elements/entities.dart'; |
8 import '../elements/resolution_types.dart'; | 10 import '../elements/resolution_types.dart'; |
9 import '../elements/elements.dart'; | |
10 import '../io/code_output.dart'; | 11 import '../io/code_output.dart'; |
11 import '../js/js.dart' as jsAst; | 12 import '../js/js.dart' as jsAst; |
12 import '../js/js.dart' show js; | 13 import '../js/js.dart' show js; |
13 import '../js_emitter/code_emitter_task.dart'; | 14 import '../js_emitter/code_emitter_task.dart'; |
14 import '../options.dart'; | 15 import '../options.dart'; |
| 16 import '../universe/world_builder.dart'; |
15 import 'constant_system_javascript.dart'; | 17 import 'constant_system_javascript.dart'; |
16 import 'js_backend.dart'; | 18 import 'js_backend.dart'; |
17 import 'namer.dart'; | 19 import 'namer.dart'; |
18 import 'runtime_types.dart'; | 20 import 'runtime_types.dart'; |
19 | 21 |
20 typedef jsAst.Expression _ConstantReferenceGenerator(ConstantValue constant); | 22 typedef jsAst.Expression _ConstantReferenceGenerator(ConstantValue constant); |
21 | 23 |
22 typedef jsAst.Expression _ConstantListGenerator(jsAst.Expression array); | 24 typedef jsAst.Expression _ConstantListGenerator(jsAst.Expression array); |
23 | 25 |
24 /** | 26 /** |
25 * Generates the JavaScript expressions for constants. | 27 * Generates the JavaScript expressions for constants. |
26 * | 28 * |
27 * It uses a given [constantReferenceGenerator] to reference nested constants | 29 * It uses a given [constantReferenceGenerator] to reference nested constants |
28 * (if there are some). It is hence up to that function to decide which | 30 * (if there are some). It is hence up to that function to decide which |
29 * constants should be inlined or not. | 31 * constants should be inlined or not. |
30 */ | 32 */ |
31 class ConstantEmitter implements ConstantValueVisitor<jsAst.Expression, Null> { | 33 class ConstantEmitter implements ConstantValueVisitor<jsAst.Expression, Null> { |
32 // Matches blank lines, comment lines and trailing comments that can't be part | 34 // Matches blank lines, comment lines and trailing comments that can't be part |
33 // of a string. | 35 // of a string. |
34 static final RegExp COMMENT_RE = | 36 static final RegExp COMMENT_RE = |
35 new RegExp(r'''^ *(//.*)?\n| *//[^''"\n]*$''', multiLine: true); | 37 new RegExp(r'''^ *(//.*)?\n| *//[^''"\n]*$''', multiLine: true); |
36 | 38 |
37 final CompilerOptions _options; | 39 final CompilerOptions _options; |
38 final CommonElements _commonElements; | 40 final CommonElements _commonElements; |
| 41 final CodegenWorldBuilder _worldBuilder; |
39 final RuntimeTypesNeed _rtiNeed; | 42 final RuntimeTypesNeed _rtiNeed; |
40 final RuntimeTypesEncoder _rtiEncoder; | 43 final RuntimeTypesEncoder _rtiEncoder; |
41 final Namer _namer; | 44 final Namer _namer; |
42 final CodeEmitterTask _task; | 45 final CodeEmitterTask _task; |
43 final _ConstantReferenceGenerator constantReferenceGenerator; | 46 final _ConstantReferenceGenerator constantReferenceGenerator; |
44 final _ConstantListGenerator makeConstantList; | 47 final _ConstantListGenerator makeConstantList; |
45 | 48 |
46 /** | 49 /** |
47 * The given [constantReferenceGenerator] function must, when invoked with a | 50 * The given [constantReferenceGenerator] function must, when invoked with a |
48 * constant, either return a reference or return its literal expression if it | 51 * constant, either return a reference or return its literal expression if it |
49 * can be inlined. | 52 * can be inlined. |
50 */ | 53 */ |
51 ConstantEmitter( | 54 ConstantEmitter( |
52 this._options, | 55 this._options, |
53 this._commonElements, | 56 this._commonElements, |
| 57 this._worldBuilder, |
54 this._rtiNeed, | 58 this._rtiNeed, |
55 this._rtiEncoder, | 59 this._rtiEncoder, |
56 this._namer, | 60 this._namer, |
57 this._task, | 61 this._task, |
58 this.constantReferenceGenerator, | 62 this.constantReferenceGenerator, |
59 this.makeConstantList); | 63 this.makeConstantList); |
60 | 64 |
61 Emitter get _emitter => _task.emitter; | 65 Emitter get _emitter => _task.emitter; |
62 | 66 |
63 /** | 67 /** |
(...skipping 212 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 @override | 280 @override |
277 jsAst.Expression visitType(TypeConstantValue constant, [_]) { | 281 jsAst.Expression visitType(TypeConstantValue constant, [_]) { |
278 ResolutionDartType type = constant.representedType; | 282 ResolutionDartType type = constant.representedType; |
279 jsAst.Name typeName = _namer.runtimeTypeName(type.element); | 283 jsAst.Name typeName = _namer.runtimeTypeName(type.element); |
280 return new jsAst.Call(getHelperProperty(_commonElements.createRuntimeType), | 284 return new jsAst.Call(getHelperProperty(_commonElements.createRuntimeType), |
281 [js.quoteName(typeName)]); | 285 [js.quoteName(typeName)]); |
282 } | 286 } |
283 | 287 |
284 @override | 288 @override |
285 jsAst.Expression visitInterceptor(InterceptorConstantValue constant, [_]) { | 289 jsAst.Expression visitInterceptor(InterceptorConstantValue constant, [_]) { |
286 ClassElement interceptorClass = constant.cls; | 290 ClassEntity interceptorClass = constant.cls; |
287 return _task.interceptorPrototypeAccess(interceptorClass); | 291 return _task.interceptorPrototypeAccess(interceptorClass); |
288 } | 292 } |
289 | 293 |
290 @override | 294 @override |
291 jsAst.Expression visitSynthetic(SyntheticConstantValue constant, [_]) { | 295 jsAst.Expression visitSynthetic(SyntheticConstantValue constant, [_]) { |
292 switch (constant.valueKind) { | 296 switch (constant.valueKind) { |
293 case SyntheticConstantKind.DUMMY_INTERCEPTOR: | 297 case SyntheticConstantKind.DUMMY_INTERCEPTOR: |
294 case SyntheticConstantKind.EMPTY_VALUE: | 298 case SyntheticConstantKind.EMPTY_VALUE: |
295 return new jsAst.LiteralNumber('0'); | 299 return new jsAst.LiteralNumber('0'); |
296 case SyntheticConstantKind.TYPEVARIABLE_REFERENCE: | 300 case SyntheticConstantKind.TYPEVARIABLE_REFERENCE: |
297 case SyntheticConstantKind.NAME: | 301 case SyntheticConstantKind.NAME: |
298 return constant.payload; | 302 return constant.payload; |
299 default: | 303 default: |
300 throw new SpannableAssertionFailure(NO_LOCATION_SPANNABLE, | 304 throw new SpannableAssertionFailure(NO_LOCATION_SPANNABLE, |
301 "Unexpected DummyConstantKind ${constant.kind}"); | 305 "Unexpected DummyConstantKind ${constant.kind}"); |
302 } | 306 } |
303 } | 307 } |
304 | 308 |
305 @override | 309 @override |
306 jsAst.Expression visitConstructed(ConstructedConstantValue constant, [_]) { | 310 jsAst.Expression visitConstructed(ConstructedConstantValue constant, [_]) { |
307 ClassElement element = constant.type.element; | 311 ClassEntity element = constant.type.element; |
308 if (element == _commonElements.jsConstClass) { | 312 if (element == _commonElements.jsConstClass) { |
309 StringConstantValue str = constant.fields.values.single; | 313 StringConstantValue str = constant.fields.values.single; |
310 String value = str.primitiveValue; | 314 String value = str.primitiveValue; |
311 return new jsAst.LiteralExpression(stripComments(value)); | 315 return new jsAst.LiteralExpression(stripComments(value)); |
312 } | 316 } |
313 jsAst.Expression constructor = | 317 jsAst.Expression constructor = |
314 _emitter.constructorAccess(constant.type.element); | 318 _emitter.constructorAccess(constant.type.element); |
315 List<jsAst.Expression> fields = <jsAst.Expression>[]; | 319 List<jsAst.Expression> fields = <jsAst.Expression>[]; |
316 element.forEachInstanceField((_, FieldElement field) { | 320 _worldBuilder.forEachInstanceField(element, (_, FieldEntity field) { |
317 fields.add(constantReferenceGenerator(constant.fields[field])); | 321 fields.add(constantReferenceGenerator(constant.fields[field])); |
318 }, includeSuperAndInjectedMembers: true); | 322 }); |
319 if (_rtiNeed.classNeedsRtiField(constant.type.element)) { | 323 if (_rtiNeed.classNeedsRtiField(constant.type.element)) { |
320 fields.add(_reifiedTypeArguments(constant.type)); | 324 fields.add(_reifiedTypeArguments(constant.type)); |
321 } | 325 } |
322 jsAst.New instantiation = new jsAst.New(constructor, fields); | 326 jsAst.New instantiation = new jsAst.New(constructor, fields); |
323 return instantiation; | 327 return instantiation; |
324 } | 328 } |
325 | 329 |
326 String stripComments(String rawJavaScript) { | 330 String stripComments(String rawJavaScript) { |
327 return rawJavaScript.replaceAll(COMMENT_RE, ''); | 331 return rawJavaScript.replaceAll(COMMENT_RE, ''); |
328 } | 332 } |
(...skipping 24 matching lines...) Expand all Loading... |
353 _rtiEncoder.getTypeRepresentation(_emitter, argument, unexpected)); | 357 _rtiEncoder.getTypeRepresentation(_emitter, argument, unexpected)); |
354 } | 358 } |
355 return new jsAst.ArrayInitializer(arguments); | 359 return new jsAst.ArrayInitializer(arguments); |
356 } | 360 } |
357 | 361 |
358 @override | 362 @override |
359 jsAst.Expression visitDeferred(DeferredConstantValue constant, [_]) { | 363 jsAst.Expression visitDeferred(DeferredConstantValue constant, [_]) { |
360 return constantReferenceGenerator(constant.referenced); | 364 return constantReferenceGenerator(constant.referenced); |
361 } | 365 } |
362 } | 366 } |
OLD | NEW |