| 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 '../constants/values.dart'; |
| 11 import '../common_elements.dart'; | 12 import '../common_elements.dart'; |
| 12 import '../elements/elements.dart'; | 13 import '../elements/elements.dart'; |
| 13 import '../elements/entities.dart'; | 14 import '../elements/entities.dart'; |
| 14 import '../elements/types.dart'; | 15 import '../elements/types.dart'; |
| 15 import '../js_backend/backend.dart' show JavaScriptBackend; | 16 import '../js_backend/backend.dart' show JavaScriptBackend; |
| 16 import '../native/native.dart' as native; | 17 import '../native/native.dart' as native; |
| 17 import '../universe/call_structure.dart'; | 18 import '../universe/call_structure.dart'; |
| 18 import '../universe/selector.dart'; | 19 import '../universe/selector.dart'; |
| 19 import 'kernel_debug.dart'; | 20 import 'kernel_debug.dart'; |
| 20 | 21 |
| (...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 /// [JS_EMBEDDED_GLOBAL] function. | 101 /// [JS_EMBEDDED_GLOBAL] function. |
| 101 native.NativeBehavior getNativeBehaviorForJsEmbeddedGlobalCall( | 102 native.NativeBehavior getNativeBehaviorForJsEmbeddedGlobalCall( |
| 102 ir.StaticInvocation node); | 103 ir.StaticInvocation node); |
| 103 | 104 |
| 104 /// Compute the kind of foreign helper function called by [node], if any. | 105 /// Compute the kind of foreign helper function called by [node], if any. |
| 105 ForeignKind getForeignKind(ir.StaticInvocation node); | 106 ForeignKind getForeignKind(ir.StaticInvocation node); |
| 106 | 107 |
| 107 /// Computes the [InterfaceType] referenced by a call to the | 108 /// Computes the [InterfaceType] referenced by a call to the |
| 108 /// [JS_INTERCEPTOR_CONSTANT] function, if any. | 109 /// [JS_INTERCEPTOR_CONSTANT] function, if any. |
| 109 InterfaceType getInterfaceTypeForJsInterceptorCall(ir.StaticInvocation node); | 110 InterfaceType getInterfaceTypeForJsInterceptorCall(ir.StaticInvocation node); |
| 111 |
| 112 /// Computes the [ConstantValue] for the constant [expression]. |
| 113 ConstantValue getConstantValue(ir.Expression expression); |
| 110 } | 114 } |
| 111 | 115 |
| 112 /// Kinds of foreign functions. | 116 /// Kinds of foreign functions. |
| 113 enum ForeignKind { | 117 enum ForeignKind { |
| 114 JS, | 118 JS, |
| 115 JS_BUILTIN, | 119 JS_BUILTIN, |
| 116 JS_EMBEDDED_GLOBAL, | 120 JS_EMBEDDED_GLOBAL, |
| 117 JS_INTERCEPTOR_CONSTANT, | 121 JS_INTERCEPTOR_CONSTANT, |
| 118 NONE, | 122 NONE, |
| 119 } | 123 } |
| 120 | 124 |
| 121 abstract class KernelElementAdapterMixin implements KernelElementAdapter { | 125 abstract class KernelElementAdapterMixin implements KernelElementAdapter { |
| 122 DiagnosticReporter get reporter; | 126 DiagnosticReporter get reporter; |
| 123 FunctionType getFunctionType(ir.FunctionNode node); | 127 FunctionType getFunctionType(ir.FunctionNode node); |
| 124 native.BehaviorBuilder get nativeBehaviorBuilder; | 128 native.BehaviorBuilder get nativeBehaviorBuilder; |
| 129 ConstantValue computeConstantValue(ConstantExpression constant); |
| 125 | 130 |
| 126 @override | 131 @override |
| 127 Name getName(ir.Name name) { | 132 Name getName(ir.Name name) { |
| 128 return new Name( | 133 return new Name( |
| 129 name.name, name.isPrivate ? getLibrary(name.library) : null); | 134 name.name, name.isPrivate ? getLibrary(name.library) : null); |
| 130 } | 135 } |
| 131 | 136 |
| 132 @override | 137 @override |
| 133 CallStructure getCallStructure(ir.Arguments arguments) { | 138 CallStructure getCallStructure(ir.Arguments arguments) { |
| 134 int argumentCount = arguments.positional.length + arguments.named.length; | 139 int argumentCount = arguments.positional.length + arguments.named.length; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 irName.name, irName.isPrivate ? getLibrary(irName.library) : null); | 189 irName.name, irName.isPrivate ? getLibrary(irName.library) : null); |
| 185 return new Selector.getter(name); | 190 return new Selector.getter(name); |
| 186 } | 191 } |
| 187 | 192 |
| 188 Selector getSetterSelector(ir.Name irName) { | 193 Selector getSetterSelector(ir.Name irName) { |
| 189 Name name = new Name( | 194 Name name = new Name( |
| 190 irName.name, irName.isPrivate ? getLibrary(irName.library) : null); | 195 irName.name, irName.isPrivate ? getLibrary(irName.library) : null); |
| 191 return new Selector.setter(name); | 196 return new Selector.setter(name); |
| 192 } | 197 } |
| 193 | 198 |
| 194 /// Converts [annotations] into a list of [ConstantExpression]s. | 199 ConstantValue getConstantValue(ir.Expression node) { |
| 195 List<ConstantExpression> getMetadata(List<ir.Expression> annotations) { | 200 ConstantExpression constant = new Constantifier(this).visit(node); |
| 196 if (annotations.isEmpty) return const <ConstantExpression>[]; | 201 if (constant == null) { |
| 197 List<ConstantExpression> metadata = <ConstantExpression>[]; | 202 throw new UnsupportedError( |
| 203 'No constant for ${DebugPrinter.prettyPrint(node)}'); |
| 204 } |
| 205 return computeConstantValue(constant); |
| 206 } |
| 207 |
| 208 /// Converts [annotations] into a list of [ConstantValue]s. |
| 209 List<ConstantValue> getMetadata(List<ir.Expression> annotations) { |
| 210 if (annotations.isEmpty) return const <ConstantValue>[]; |
| 211 List<ConstantValue> metadata = <ConstantValue>[]; |
| 198 annotations.forEach((ir.Expression node) { | 212 annotations.forEach((ir.Expression node) { |
| 199 ConstantExpression constant = new Constantifier(this).visit(node); | 213 metadata.add(getConstantValue(node)); |
| 200 if (constant == null) { | |
| 201 throw new UnsupportedError( | |
| 202 'No constant for ${DebugPrinter.prettyPrint(node)}'); | |
| 203 } | |
| 204 metadata.add(constant); | |
| 205 }); | 214 }); |
| 206 return metadata; | 215 return metadata; |
| 207 } | 216 } |
| 208 | 217 |
| 209 /// Returns `true` is [node] has a `@Native(...)` annotation. | 218 /// Returns `true` is [node] has a `@Native(...)` annotation. |
| 210 // TODO(johnniwinther): Cache this for later use. | 219 // TODO(johnniwinther): Cache this for later use. |
| 211 bool isNativeClass(ir.Class node) { | 220 bool isNativeClass(ir.Class node) { |
| 212 for (ir.Expression annotation in node.annotations) { | 221 for (ir.Expression annotation in node.annotations) { |
| 213 if (annotation is ir.ConstructorInvocation) { | 222 if (annotation is ir.ConstructorInvocation) { |
| 214 FunctionEntity target = getConstructor(annotation.target); | 223 FunctionEntity target = getConstructor(annotation.target); |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 401 if (argument is ir.TypeLiteral && argument.type is ir.InterfaceType) { | 410 if (argument is ir.TypeLiteral && argument.type is ir.InterfaceType) { |
| 402 return getInterfaceType(argument.type); | 411 return getInterfaceType(argument.type); |
| 403 } | 412 } |
| 404 return null; | 413 return null; |
| 405 } | 414 } |
| 406 | 415 |
| 407 /// Computes the native behavior for reading the native [field]. | 416 /// Computes the native behavior for reading the native [field]. |
| 408 // TODO(johnniwinther): Cache this for later use. | 417 // TODO(johnniwinther): Cache this for later use. |
| 409 native.NativeBehavior getNativeBehaviorForFieldLoad(ir.Field field) { | 418 native.NativeBehavior getNativeBehaviorForFieldLoad(ir.Field field) { |
| 410 DartType type = getDartType(field.type); | 419 DartType type = getDartType(field.type); |
| 411 List<ConstantExpression> metadata = getMetadata(field.annotations); | 420 List<ConstantValue> metadata = getMetadata(field.annotations); |
| 412 // TODO(johnniwinther): Provide the correct value for [isJsInterop]. | 421 // TODO(johnniwinther): Provide the correct value for [isJsInterop]. |
| 413 return nativeBehaviorBuilder.buildFieldLoadBehavior( | 422 return nativeBehaviorBuilder.buildFieldLoadBehavior( |
| 414 type, metadata, typeLookup(resolveAsRaw: false), | 423 type, metadata, typeLookup(resolveAsRaw: false), |
| 415 isJsInterop: false); | 424 isJsInterop: false); |
| 416 } | 425 } |
| 417 | 426 |
| 418 /// Computes the native behavior for writing to the native [field]. | 427 /// Computes the native behavior for writing to the native [field]. |
| 419 // TODO(johnniwinther): Cache this for later use. | 428 // TODO(johnniwinther): Cache this for later use. |
| 420 native.NativeBehavior getNativeBehaviorForFieldStore(ir.Field field) { | 429 native.NativeBehavior getNativeBehaviorForFieldStore(ir.Field field) { |
| 421 DartType type = getDartType(field.type); | 430 DartType type = getDartType(field.type); |
| 422 return nativeBehaviorBuilder.buildFieldStoreBehavior(type); | 431 return nativeBehaviorBuilder.buildFieldStoreBehavior(type); |
| 423 } | 432 } |
| 424 | 433 |
| 425 /// Computes the native behavior for calling [procedure]. | 434 /// Computes the native behavior for calling [procedure]. |
| 426 // TODO(johnniwinther): Cache this for later use. | 435 // TODO(johnniwinther): Cache this for later use. |
| 427 native.NativeBehavior getNativeBehaviorForMethod(ir.Procedure procedure) { | 436 native.NativeBehavior getNativeBehaviorForMethod(ir.Procedure procedure) { |
| 428 DartType type = getFunctionType(procedure.function); | 437 DartType type = getFunctionType(procedure.function); |
| 429 List<ConstantExpression> metadata = getMetadata(procedure.annotations); | 438 List<ConstantValue> metadata = getMetadata(procedure.annotations); |
| 430 // TODO(johnniwinther): Provide the correct value for [isJsInterop]. | 439 // TODO(johnniwinther): Provide the correct value for [isJsInterop]. |
| 431 return nativeBehaviorBuilder.buildMethodBehavior( | 440 return nativeBehaviorBuilder.buildMethodBehavior( |
| 432 type, metadata, typeLookup(resolveAsRaw: false), | 441 type, metadata, typeLookup(resolveAsRaw: false), |
| 433 isJsInterop: false); | 442 isJsInterop: false); |
| 434 } | 443 } |
| 435 } | 444 } |
| 436 | 445 |
| 437 /// Visitor that converts string literals and concatenations of string literals | 446 /// Visitor that converts string literals and concatenations of string literals |
| 438 /// into the string value. | 447 /// into the string value. |
| 439 class Stringifier extends ir.ExpressionVisitor<String> { | 448 class Stringifier extends ir.ExpressionVisitor<String> { |
| (...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 606 } | 615 } |
| 607 if (isRedirecting) { | 616 if (isRedirecting) { |
| 608 return new RedirectingGenerativeConstantConstructor( | 617 return new RedirectingGenerativeConstantConstructor( |
| 609 defaultValues, superConstructorInvocation); | 618 defaultValues, superConstructorInvocation); |
| 610 } else { | 619 } else { |
| 611 return new GenerativeConstantConstructor( | 620 return new GenerativeConstantConstructor( |
| 612 type, defaultValues, fieldMap, superConstructorInvocation); | 621 type, defaultValues, fieldMap, superConstructorInvocation); |
| 613 } | 622 } |
| 614 } | 623 } |
| 615 } | 624 } |
| OLD | NEW |