| 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/expressions.dart'; |
| 9 import '../core_types.dart'; | 10 import '../core_types.dart'; |
| 10 import '../elements/elements.dart'; | 11 import '../elements/elements.dart'; |
| 11 import '../elements/entities.dart'; | 12 import '../elements/entities.dart'; |
| 12 import '../elements/types.dart'; | 13 import '../elements/types.dart'; |
| 13 import '../js_backend/backend_helpers.dart'; | 14 import '../js_backend/backend_helpers.dart'; |
| 14 import '../native/native.dart' as native; | 15 import '../native/native.dart' as native; |
| 15 import '../universe/call_structure.dart'; | 16 import '../universe/call_structure.dart'; |
| 16 import '../universe/selector.dart'; | 17 import '../universe/selector.dart'; |
| 18 import 'kernel_debug.dart'; |
| 17 | 19 |
| 18 /// Interface that translates between Kernel IR nodes and entities. | 20 /// Interface that translates between Kernel IR nodes and entities. |
| 19 abstract class KernelElementAdapter { | 21 abstract class KernelElementAdapter { |
| 20 /// Access to the commonly used elements and types. | 22 /// Access to the commonly used elements and types. |
| 21 CommonElements get commonElements; | 23 CommonElements get commonElements; |
| 22 | 24 |
| 23 /// Returns the [DartType] corresponding to [type]. | 25 /// Returns the [DartType] corresponding to [type]. |
| 24 DartType getDartType(ir.DartType type); | 26 DartType getDartType(ir.DartType type); |
| 25 | 27 |
| 26 /// Returns the list of [DartType]s corresponding to [types]. | 28 /// Returns the list of [DartType]s corresponding to [types]. |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 irName.name, irName.isPrivate ? getLibrary(irName.library) : null); | 177 irName.name, irName.isPrivate ? getLibrary(irName.library) : null); |
| 176 return new Selector.getter(name); | 178 return new Selector.getter(name); |
| 177 } | 179 } |
| 178 | 180 |
| 179 Selector getSetterSelector(ir.Name irName) { | 181 Selector getSetterSelector(ir.Name irName) { |
| 180 Name name = new Name( | 182 Name name = new Name( |
| 181 irName.name, irName.isPrivate ? getLibrary(irName.library) : null); | 183 irName.name, irName.isPrivate ? getLibrary(irName.library) : null); |
| 182 return new Selector.setter(name); | 184 return new Selector.setter(name); |
| 183 } | 185 } |
| 184 | 186 |
| 187 /// Converts [annotations] into a list of [ConstantExpression]s. |
| 188 List<ConstantExpression> getMetadata(List<ir.Expression> annotations) { |
| 189 List<ConstantExpression> metadata = <ConstantExpression>[]; |
| 190 annotations.forEach((ir.Expression node) { |
| 191 ConstantExpression constant = node.accept(new Constantifier(this)); |
| 192 if (constant == null) { |
| 193 throw new UnsupportedError( |
| 194 'No constant for ${DebugPrinter.prettyPrint(node)}'); |
| 195 } |
| 196 metadata.add(constant); |
| 197 }); |
| 198 return metadata; |
| 199 } |
| 200 |
| 185 /// Returns `true` is [node] has a `@Native(...)` annotation. | 201 /// Returns `true` is [node] has a `@Native(...)` annotation. |
| 186 // TODO(johnniwinther): Cache this for later use. | 202 // TODO(johnniwinther): Cache this for later use. |
| 187 bool isNativeClass(ir.Class node) { | 203 bool isNativeClass(ir.Class node) { |
| 188 for (ir.Expression annotation in node.annotations) { | 204 for (ir.Expression annotation in node.annotations) { |
| 189 if (annotation is ir.ConstructorInvocation) { | 205 if (annotation is ir.ConstructorInvocation) { |
| 190 FunctionEntity target = getConstructor(annotation.target); | 206 FunctionEntity target = getConstructor(annotation.target); |
| 191 if (target.enclosingClass == commonElements.nativeAnnotationClass) { | 207 if (target.enclosingClass == commonElements.nativeAnnotationClass) { |
| 192 return true; | 208 return true; |
| 193 } | 209 } |
| 194 } | 210 } |
| (...skipping 180 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 375 String visitStringConcatenation(ir.StringConcatenation node) { | 391 String visitStringConcatenation(ir.StringConcatenation node) { |
| 376 StringBuffer sb = new StringBuffer(); | 392 StringBuffer sb = new StringBuffer(); |
| 377 for (ir.Expression expression in node.expressions) { | 393 for (ir.Expression expression in node.expressions) { |
| 378 String value = expression.accept(this); | 394 String value = expression.accept(this); |
| 379 if (value == null) return null; | 395 if (value == null) return null; |
| 380 sb.write(value); | 396 sb.write(value); |
| 381 } | 397 } |
| 382 return sb.toString(); | 398 return sb.toString(); |
| 383 } | 399 } |
| 384 } | 400 } |
| 401 |
| 402 /// Visitor that converts a kernel constant expression into a |
| 403 /// [ConstantExpression]. |
| 404 class Constantifier extends ir.ExpressionVisitor<ConstantExpression> { |
| 405 final KernelElementAdapter elementAdapter; |
| 406 |
| 407 Constantifier(this.elementAdapter); |
| 408 |
| 409 @override |
| 410 ConstantExpression visitConstructorInvocation(ir.ConstructorInvocation node) { |
| 411 List<ConstantExpression> arguments = <ConstantExpression>[]; |
| 412 List<String> argumentNames = <String>[]; |
| 413 for (ir.Expression argument in node.arguments.positional) { |
| 414 ConstantExpression constant = argument.accept(this); |
| 415 if (constant == null) return null; |
| 416 arguments.add(constant); |
| 417 } |
| 418 for (ir.NamedExpression argument in node.arguments.named) { |
| 419 argumentNames.add(argument.name); |
| 420 ConstantExpression constant = argument.value.accept(this); |
| 421 if (constant == null) return null; |
| 422 arguments.add(constant); |
| 423 } |
| 424 return new ConstructedConstantExpression( |
| 425 elementAdapter.createInterfaceType( |
| 426 node.target.enclosingClass, node.arguments.types), |
| 427 elementAdapter.getConstructor(node.target), |
| 428 new CallStructure( |
| 429 node.arguments.positional.length + argumentNames.length, |
| 430 argumentNames), |
| 431 arguments); |
| 432 } |
| 433 |
| 434 @override |
| 435 ConstantExpression visitStaticGet(ir.StaticGet node) { |
| 436 return new FieldConstantExpression(elementAdapter.getField(node.target)); |
| 437 } |
| 438 |
| 439 @override |
| 440 ConstantExpression visitStringLiteral(ir.StringLiteral node) { |
| 441 return new StringConstantExpression(node.value); |
| 442 } |
| 443 } |
| OLD | NEW |