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.transformation.binding; |
| 6 |
| 7 import 'package:kernel/ast.dart'; |
| 8 |
| 9 class RuntimeLibrary { |
| 10 final Library typesLibrary; |
| 11 final Library declarationsLibrary; |
| 12 final Library interceptorsLibrary; |
| 13 |
| 14 final Constructor dynamicTypeConstructor; |
| 15 final Constructor interfaceTypeConstructor; |
| 16 final Constructor declarationClassConstructor; |
| 17 final Constructor functionTypeConstructor; |
| 18 final Constructor voidTypeConstructor; |
| 19 |
| 20 // The class used to mark instances that implement `$type`. |
| 21 final Class markerClass; |
| 22 final Class declarationClass; |
| 23 final Class reifiedTypeClass; |
| 24 |
| 25 DartType get typeType => reifiedTypeClass.rawType; |
| 26 |
| 27 final Name variablesFieldName = new Name("variables"); |
| 28 |
| 29 /// The name of the field to create for the type information. This name is |
| 30 /// extracted from the class `HasRuntimeTypeGetter`. |
| 31 final Name runtimeTypeName; |
| 32 |
| 33 final Procedure asInstanceOfFunction; |
| 34 final Procedure isSubtypeOfFunction; |
| 35 final Procedure typeArgumentsFunction; |
| 36 final Procedure allocateDeclarationsFunction; |
| 37 final Procedure initFunction; |
| 38 final Procedure interceptorFunction; |
| 39 final Procedure reifyFunction; |
| 40 final Procedure attachTypeFunction; |
| 41 |
| 42 factory RuntimeLibrary(Library typesLibrary, Library declarationsLibrary, |
| 43 Library interceptorsLibrary) { |
| 44 Class dynamicTypeClass; |
| 45 Class interfaceTypeClass; |
| 46 Class declarationClass; |
| 47 Class functionTypeClass; |
| 48 Class voidTypeClass; |
| 49 Class markerClass; |
| 50 Class reifiedTypeClass; |
| 51 |
| 52 Procedure allocateDeclarationsFunction; |
| 53 Procedure initFunction; |
| 54 Procedure interceptorFunction; |
| 55 Procedure reifyFunction; |
| 56 Procedure attachTypeFunction; |
| 57 Procedure asInstanceOfFunction; |
| 58 Procedure isSubtypeOfFunction; |
| 59 Procedure typeArgumentsFunction; |
| 60 |
| 61 for (Procedure p in interceptorsLibrary.procedures) { |
| 62 if (p.name.name == "type") { |
| 63 interceptorFunction = p; |
| 64 } else if (p.name.name == "reify") { |
| 65 reifyFunction = p; |
| 66 } else if (p.name.name == "attachType") { |
| 67 attachTypeFunction = p; |
| 68 } |
| 69 } |
| 70 for (Class c in interceptorsLibrary.classes) { |
| 71 if (c.name == "HasRuntimeTypeGetter") { |
| 72 markerClass = c; |
| 73 } |
| 74 } |
| 75 for (Class c in typesLibrary.classes) { |
| 76 if (c.name == 'Dynamic') { |
| 77 dynamicTypeClass = c; |
| 78 } else if (c.name == 'Interface') { |
| 79 interfaceTypeClass = c; |
| 80 } else if (c.name == 'FunctionType') { |
| 81 functionTypeClass = c; |
| 82 } else if (c.name == 'Void') { |
| 83 voidTypeClass = c; |
| 84 } else if (c.name == 'ReifiedType') { |
| 85 reifiedTypeClass = c; |
| 86 } |
| 87 } |
| 88 for (Procedure p in typesLibrary.procedures) { |
| 89 if (p.name.name == "asInstanceOf") { |
| 90 asInstanceOfFunction = p; |
| 91 } else if (p.name.name == "isSubtypeOf") { |
| 92 isSubtypeOfFunction = p; |
| 93 } else if (p.name.name == "getTypeArguments") { |
| 94 typeArgumentsFunction = p; |
| 95 } |
| 96 } |
| 97 for (Procedure p in declarationsLibrary.procedures) { |
| 98 if (p.name.name == "allocateDeclarations") { |
| 99 allocateDeclarationsFunction = p; |
| 100 } else if (p.name.name == "init") { |
| 101 initFunction = p; |
| 102 } |
| 103 } |
| 104 for (Class c in declarationsLibrary.classes) { |
| 105 if (c.name == 'Class') { |
| 106 declarationClass = c; |
| 107 } |
| 108 } |
| 109 |
| 110 assert(dynamicTypeClass != null); |
| 111 assert(declarationClass != null); |
| 112 assert(interfaceTypeClass != null); |
| 113 assert(functionTypeClass != null); |
| 114 assert(voidTypeClass != null); |
| 115 assert(markerClass != null); |
| 116 assert(declarationClass != null); |
| 117 assert(reifiedTypeClass != null); |
| 118 assert(allocateDeclarationsFunction != null); |
| 119 assert(initFunction != null); |
| 120 assert(interceptorFunction != null); |
| 121 assert(reifyFunction != null); |
| 122 assert(attachTypeFunction != null); |
| 123 assert(asInstanceOfFunction != null); |
| 124 assert(isSubtypeOfFunction != null); |
| 125 assert(typeArgumentsFunction != null); |
| 126 |
| 127 return new RuntimeLibrary._( |
| 128 markerClass.procedures.single.name, |
| 129 typesLibrary, |
| 130 declarationsLibrary, |
| 131 interceptorsLibrary, |
| 132 markerClass, |
| 133 declarationClass, |
| 134 reifiedTypeClass, |
| 135 dynamicTypeClass.constructors.single, |
| 136 interfaceTypeClass.constructors.single, |
| 137 declarationClass.constructors.single, |
| 138 functionTypeClass.constructors.single, |
| 139 voidTypeClass.constructors.single, |
| 140 allocateDeclarationsFunction, |
| 141 initFunction, |
| 142 interceptorFunction, |
| 143 reifyFunction, |
| 144 attachTypeFunction, |
| 145 asInstanceOfFunction, |
| 146 isSubtypeOfFunction, |
| 147 typeArgumentsFunction); |
| 148 } |
| 149 |
| 150 RuntimeLibrary._( |
| 151 this.runtimeTypeName, |
| 152 this.typesLibrary, |
| 153 this.declarationsLibrary, |
| 154 this.interceptorsLibrary, |
| 155 this.markerClass, |
| 156 this.declarationClass, |
| 157 this.reifiedTypeClass, |
| 158 this.dynamicTypeConstructor, |
| 159 this.interfaceTypeConstructor, |
| 160 this.declarationClassConstructor, |
| 161 this.functionTypeConstructor, |
| 162 this.voidTypeConstructor, |
| 163 this.allocateDeclarationsFunction, |
| 164 this.initFunction, |
| 165 this.interceptorFunction, |
| 166 this.reifyFunction, |
| 167 this.attachTypeFunction, |
| 168 this.asInstanceOfFunction, |
| 169 this.isSubtypeOfFunction, |
| 170 this.typeArgumentsFunction); |
| 171 |
| 172 /// Returns `true` if [node] is defined in the runtime library. |
| 173 bool contains(TreeNode node) { |
| 174 while (node is! Library) { |
| 175 node = node.parent; |
| 176 if (node == null) return false; |
| 177 } |
| 178 return node == typesLibrary || node == declarationsLibrary; |
| 179 } |
| 180 } |
OLD | NEW |