OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| 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. |
| 4 |
| 5 library kernel.transformations.reify.runtime.declarations; |
| 6 |
| 7 import 'types.dart' show FunctionType, Interface, ReifiedType, TypeVariable; |
| 8 |
| 9 typedef String Id2String(Object id); |
| 10 |
| 11 /// Represents a class. |
| 12 /// |
| 13 /// There's one instance of this class for each class in the program. |
| 14 /// |
| 15 /// Note: not all classes can be represented as a compile-time constants. For |
| 16 /// example, most core classes, such as, `String`, `int`, and `double` |
| 17 /// implement `Comparable` in a way that cannot be expressed as a constant. |
| 18 class Class { |
| 19 final id; |
| 20 |
| 21 Interface supertype; |
| 22 |
| 23 FunctionType callableType; |
| 24 |
| 25 final List<TypeVariable> variables; |
| 26 |
| 27 List<Interface> interfaces; |
| 28 |
| 29 static Id2String debugId2String; |
| 30 |
| 31 Class(this.id, this.supertype, |
| 32 {this.callableType, |
| 33 this.variables: const <TypeVariable>[], |
| 34 this.interfaces /* set in init */}); |
| 35 |
| 36 Interface get thisType { |
| 37 return new Interface(this, variables); |
| 38 } |
| 39 |
| 40 String get name => debugId2String == null ? "$id" : debugId2String(id); |
| 41 |
| 42 String toString() { |
| 43 StringBuffer sb = new StringBuffer(); |
| 44 sb.write("class $name"); |
| 45 if (variables.isNotEmpty) { |
| 46 sb.write("<"); |
| 47 bool first = true; |
| 48 for (TypeVariable tv in variables) { |
| 49 if (!first) { |
| 50 sb.write(", "); |
| 51 } |
| 52 sb.write(tv); |
| 53 sb.write(" extends "); |
| 54 sb.write(tv.bound); |
| 55 } |
| 56 sb.write(">"); |
| 57 } |
| 58 if (supertype != null) { |
| 59 sb.write(" extends $supertype"); |
| 60 } |
| 61 if (interfaces.isNotEmpty) { |
| 62 sb.write(" implements "); |
| 63 sb.writeAll(interfaces, ", "); |
| 64 } |
| 65 return "$sb"; |
| 66 } |
| 67 } |
| 68 |
| 69 /// Allocates a (non-growable) list of [amount] class declarations with ids `0` |
| 70 /// to `amount - 1`. This function is called from the generated code that |
| 71 /// initializes the type information. |
| 72 List<Class> allocateDeclarations(List<String> names, List<int> typeParameters) { |
| 73 List<TypeVariable> allocateVariables(int amount) { |
| 74 if (amount == 0) return const <TypeVariable>[]; |
| 75 return new List<TypeVariable>.generate( |
| 76 amount, (int i) => new TypeVariable(i), |
| 77 growable: false); |
| 78 } |
| 79 |
| 80 return new List<Class>.generate( |
| 81 names.length, |
| 82 (int i) => new Class(names[i], null, |
| 83 variables: allocateVariables(typeParameters[i])), |
| 84 growable: false); |
| 85 } |
| 86 |
| 87 /// Initializes the supertype and interfaces of `classes[index]`. |
| 88 /// This function is called from generated code. |
| 89 void init(List<Class> classes, int index, ReifiedType supertype, |
| 90 [List<Interface> interfaces = const <Interface>[], |
| 91 FunctionType callableType]) { |
| 92 Class declaration = classes[index]; |
| 93 assert(supertype == null); |
| 94 declaration.supertype = supertype; |
| 95 assert(interfaces == null); |
| 96 declaration.interfaces = interfaces; |
| 97 declaration.callableType = callableType; |
| 98 } |
OLD | NEW |