| OLD | NEW |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, 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 'package:kernel/ast.dart' as ir; | 5 import 'package:kernel/ast.dart' as ir; |
| 6 | 6 |
| 7 import '../common.dart'; | 7 import '../common.dart'; |
| 8 import '../common/names.dart'; | 8 import '../common/names.dart'; |
| 9 import '../constants/constructors.dart'; | 9 import '../constants/constructors.dart'; |
| 10 import '../constants/expressions.dart'; | 10 import '../constants/expressions.dart'; |
| 11 import '../core_types.dart'; | 11 import '../core_types.dart'; |
| 12 import '../elements/elements.dart'; | 12 import '../elements/elements.dart'; |
| 13 import '../elements/entities.dart'; | 13 import '../elements/entities.dart'; |
| 14 import '../elements/types.dart'; | 14 import '../elements/types.dart'; |
| 15 import '../js_backend/backend_helpers.dart'; | 15 import '../js_backend/backend_helpers.dart'; |
| 16 import '../native/native.dart' as native; | 16 import '../native/native.dart' as native; |
| 17 import '../universe/call_structure.dart'; | 17 import '../universe/call_structure.dart'; |
| 18 import '../universe/selector.dart'; | 18 import '../universe/selector.dart'; |
| 19 import 'kernel_debug.dart'; | 19 import 'kernel_debug.dart'; |
| 20 | 20 |
| 21 /// Interface that translates between Kernel IR nodes and entities. | 21 /// Interface that translates between Kernel IR nodes and entities. |
| 22 abstract class KernelElementAdapter { | 22 abstract class KernelElementAdapter { |
| 23 /// Access to the commonly used elements and types. | 23 /// Access to the commonly used elements and types. |
| 24 CommonElements get commonElements; | 24 CommonElements get commonElements; |
| 25 | 25 |
| 26 // Access to backend helpers. |
| 27 BackendHelpers get helpers; |
| 28 |
| 26 /// [ElementEnvironment] for library, class and member lookup. | 29 /// [ElementEnvironment] for library, class and member lookup. |
| 27 ElementEnvironment get elementEnvironment; | 30 ElementEnvironment get elementEnvironment; |
| 28 | 31 |
| 29 /// Returns the [DartType] corresponding to [type]. | 32 /// Returns the [DartType] corresponding to [type]. |
| 30 DartType getDartType(ir.DartType type); | 33 DartType getDartType(ir.DartType type); |
| 31 | 34 |
| 32 /// Returns the list of [DartType]s corresponding to [types]. | 35 /// Returns the list of [DartType]s corresponding to [types]. |
| 33 List<DartType> getDartTypes(List<ir.DartType> types); | 36 List<DartType> getDartTypes(List<ir.DartType> types); |
| 34 | 37 |
| 35 /// Returns the [InterfaceType] corresponding to [type]. | 38 /// Returns the [InterfaceType] corresponding to [type]. |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 115 JS_BUILTIN, | 118 JS_BUILTIN, |
| 116 JS_EMBEDDED_GLOBAL, | 119 JS_EMBEDDED_GLOBAL, |
| 117 JS_INTERCEPTOR_CONSTANT, | 120 JS_INTERCEPTOR_CONSTANT, |
| 118 NONE, | 121 NONE, |
| 119 } | 122 } |
| 120 | 123 |
| 121 abstract class KernelElementAdapterMixin implements KernelElementAdapter { | 124 abstract class KernelElementAdapterMixin implements KernelElementAdapter { |
| 122 DiagnosticReporter get reporter; | 125 DiagnosticReporter get reporter; |
| 123 FunctionType getFunctionType(ir.FunctionNode node); | 126 FunctionType getFunctionType(ir.FunctionNode node); |
| 124 native.BehaviorBuilder get nativeBehaviorBuilder; | 127 native.BehaviorBuilder get nativeBehaviorBuilder; |
| 128 BackendHelpers _helpers; |
| 129 |
| 130 @override |
| 131 BackendHelpers get helpers => |
| 132 _helpers ??= new BackendHelpers(elementEnvironment, commonElements); |
| 125 | 133 |
| 126 @override | 134 @override |
| 127 Name getName(ir.Name name) { | 135 Name getName(ir.Name name) { |
| 128 return new Name( | 136 return new Name( |
| 129 name.name, name.isPrivate ? getLibrary(name.library) : null); | 137 name.name, name.isPrivate ? getLibrary(name.library) : null); |
| 130 } | 138 } |
| 131 | 139 |
| 132 @override | 140 @override |
| 133 CallStructure getCallStructure(ir.Arguments arguments) { | 141 CallStructure getCallStructure(ir.Arguments arguments) { |
| 134 int argumentCount = arguments.positional.length + arguments.named.length; | 142 int argumentCount = arguments.positional.length + arguments.named.length; |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 }); | 212 }); |
| 205 return metadata; | 213 return metadata; |
| 206 } | 214 } |
| 207 | 215 |
| 208 /// Returns `true` is [node] has a `@Native(...)` annotation. | 216 /// Returns `true` is [node] has a `@Native(...)` annotation. |
| 209 // TODO(johnniwinther): Cache this for later use. | 217 // TODO(johnniwinther): Cache this for later use. |
| 210 bool isNativeClass(ir.Class node) { | 218 bool isNativeClass(ir.Class node) { |
| 211 for (ir.Expression annotation in node.annotations) { | 219 for (ir.Expression annotation in node.annotations) { |
| 212 if (annotation is ir.ConstructorInvocation) { | 220 if (annotation is ir.ConstructorInvocation) { |
| 213 FunctionEntity target = getConstructor(annotation.target); | 221 FunctionEntity target = getConstructor(annotation.target); |
| 214 if (target.enclosingClass == commonElements.nativeAnnotationClass) { | 222 if (target.enclosingClass == helpers.nativeAnnotationClass) { |
| 215 return true; | 223 return true; |
| 216 } | 224 } |
| 217 } | 225 } |
| 218 } | 226 } |
| 219 return false; | 227 return false; |
| 220 } | 228 } |
| 221 | 229 |
| 222 /// Compute the kind of foreign helper function called by [node], if any. | 230 /// Compute the kind of foreign helper function called by [node], if any. |
| 223 ForeignKind getForeignKind(ir.StaticInvocation node) { | 231 ForeignKind getForeignKind(ir.StaticInvocation node) { |
| 224 if (isForeignLibrary(node.target.enclosingLibrary)) { | 232 if (isForeignLibrary(node.target.enclosingLibrary)) { |
| (...skipping 224 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 449 class Constantifier extends ir.ExpressionVisitor<ConstantExpression> { | 457 class Constantifier extends ir.ExpressionVisitor<ConstantExpression> { |
| 450 final KernelElementAdapter elementAdapter; | 458 final KernelElementAdapter elementAdapter; |
| 451 | 459 |
| 452 Constantifier(this.elementAdapter); | 460 Constantifier(this.elementAdapter); |
| 453 | 461 |
| 454 ConstantExpression defaultExpression(ir.Expression node) { | 462 ConstantExpression defaultExpression(ir.Expression node) { |
| 455 throw new UnimplementedError( | 463 throw new UnimplementedError( |
| 456 'Unimplemented constant expression $node (${node.runtimeType})'); | 464 'Unimplemented constant expression $node (${node.runtimeType})'); |
| 457 } | 465 } |
| 458 | 466 |
| 467 List<ConstantExpression> _computeList(List<ir.Expression> expressions) { |
| 468 List<ConstantExpression> list = <ConstantExpression>[]; |
| 469 for (ir.Expression expression in expressions) { |
| 470 ConstantExpression constant = expression.accept(this); |
| 471 if (constant == null) return null; |
| 472 list.add(constant); |
| 473 } |
| 474 return list; |
| 475 } |
| 476 |
| 459 List<ConstantExpression> _computeArguments(ir.Arguments node) { | 477 List<ConstantExpression> _computeArguments(ir.Arguments node) { |
| 460 List<ConstantExpression> arguments = <ConstantExpression>[]; | 478 List<ConstantExpression> arguments = <ConstantExpression>[]; |
| 461 for (ir.Expression argument in node.positional) { | 479 for (ir.Expression argument in node.positional) { |
| 462 ConstantExpression constant = argument.accept(this); | 480 ConstantExpression constant = argument.accept(this); |
| 463 if (constant == null) return null; | 481 if (constant == null) return null; |
| 464 arguments.add(constant); | 482 arguments.add(constant); |
| 465 } | 483 } |
| 466 for (ir.NamedExpression argument in node.named) { | 484 for (ir.NamedExpression argument in node.named) { |
| 467 ConstantExpression constant = argument.value.accept(this); | 485 ConstantExpression constant = argument.value.accept(this); |
| 468 if (constant == null) return null; | 486 if (constant == null) return null; |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 505 @override | 523 @override |
| 506 ConstantExpression visitStaticGet(ir.StaticGet node) { | 524 ConstantExpression visitStaticGet(ir.StaticGet node) { |
| 507 return new FieldConstantExpression(elementAdapter.getField(node.target)); | 525 return new FieldConstantExpression(elementAdapter.getField(node.target)); |
| 508 } | 526 } |
| 509 | 527 |
| 510 @override | 528 @override |
| 511 ConstantExpression visitStringLiteral(ir.StringLiteral node) { | 529 ConstantExpression visitStringLiteral(ir.StringLiteral node) { |
| 512 return new StringConstantExpression(node.value); | 530 return new StringConstantExpression(node.value); |
| 513 } | 531 } |
| 514 | 532 |
| 533 @override |
| 534 ConstantExpression visitStringConcatenation(ir.StringConcatenation node) { |
| 535 return new ConcatenateConstantExpression(_computeList(node.expressions)); |
| 536 } |
| 537 |
| 515 /// Compute the [ConstantConstructor] corresponding to the const constructor | 538 /// Compute the [ConstantConstructor] corresponding to the const constructor |
| 516 /// [node]. | 539 /// [node]. |
| 517 ConstantConstructor computeConstantConstructor(ir.Constructor node) { | 540 ConstantConstructor computeConstantConstructor(ir.Constructor node) { |
| 518 assert(node.isConst); | 541 assert(node.isConst); |
| 519 ir.Class cls = node.enclosingClass; | 542 ir.Class cls = node.enclosingClass; |
| 520 InterfaceType type = elementAdapter.elementEnvironment | 543 InterfaceType type = elementAdapter.elementEnvironment |
| 521 .getThisType(elementAdapter.getClass(cls)); | 544 .getThisType(elementAdapter.getClass(cls)); |
| 522 | 545 |
| 523 Map<dynamic, ConstantExpression> defaultValues = | 546 Map<dynamic, ConstantExpression> defaultValues = |
| 524 <dynamic, ConstantExpression>{}; | 547 <dynamic, ConstantExpression>{}; |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 569 } | 592 } |
| 570 if (isRedirecting) { | 593 if (isRedirecting) { |
| 571 return new RedirectingGenerativeConstantConstructor( | 594 return new RedirectingGenerativeConstantConstructor( |
| 572 defaultValues, superConstructorInvocation); | 595 defaultValues, superConstructorInvocation); |
| 573 } else { | 596 } else { |
| 574 return new GenerativeConstantConstructor( | 597 return new GenerativeConstantConstructor( |
| 575 type, defaultValues, fieldMap, superConstructorInvocation); | 598 type, defaultValues, fieldMap, superConstructorInvocation); |
| 576 } | 599 } |
| 577 } | 600 } |
| 578 } | 601 } |
| OLD | NEW |