/interpreter/tags/at2dist130208/src/edu/vub/at/objects/natives/NATTypeTag.java
Java | 196 lines | 96 code | 26 blank | 74 comment | 11 complexity | e5e69126b133896f5e9c70e171994128 MD5 | raw file
1/** 2 * AmbientTalk/2 Project 3 * NATTypeTag.java created on 18-feb-2007 at 15:59:20 4 * (c) Programming Technology Lab, 2006 - 2007 5 * Authors: Tom Van Cutsem & Stijn Mostinckx 6 * 7 * Permission is hereby granted, free of charge, to any person 8 * obtaining a copy of this software and associated documentation 9 * files (the "Software"), to deal in the Software without 10 * restriction, including without limitation the rights to use, 11 * copy, modify, merge, publish, distribute, sublicense, and/or 12 * sell copies of the Software, and to permit persons to whom the 13 * Software is furnished to do so, subject to the following 14 * conditions: 15 * 16 * The above copyright notice and this permission notice shall be 17 * included in all copies or substantial portions of the Software. 18 * 19 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 20 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES 21 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 22 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT 23 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 24 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 25 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 26 * OTHER DEALINGS IN THE SOFTWARE. 27 */ 28package edu.vub.at.objects.natives; 29 30import edu.vub.at.actors.natives.DiscoveryManager; 31import edu.vub.at.exceptions.InterpreterException; 32import edu.vub.at.objects.ATBoolean; 33import edu.vub.at.objects.ATObject; 34import edu.vub.at.objects.ATTable; 35import edu.vub.at.objects.ATTypeTag; 36import edu.vub.at.objects.coercion.NativeTypeTags; 37import edu.vub.at.objects.grammar.ATSymbol; 38import edu.vub.at.objects.mirrors.NativeClosure; 39import edu.vub.at.objects.natives.grammar.AGSymbol; 40 41/** 42 * The native implementation of AmbientTalk type tag objects. 43 * 44 * In principle, care should be taken that all objects implementing the 45 * type tag interface are isolates, because type tags are usually attributed 46 * to messages which are isolates themselves. 47 * 48 * @author tvcutsem 49 */ 50public class NATTypeTag extends NATByCopy implements ATTypeTag { 51 52 private final ATSymbol typeName_; 53 private final ATTable parentTypes_; 54 55 public static ATTypeTag[] toTypeTagArray(ATTable types) throws InterpreterException { 56 if (types == NATTable.EMPTY) { 57 return NATObject._NO_TYPETAGS_; 58 } 59 ATObject[] unwrapped = types.asNativeTable().elements_; 60 ATTypeTag[] unwrappedTypes = new ATTypeTag[unwrapped.length]; 61 for (int i = 0; i < unwrappedTypes.length; i++) { 62 unwrappedTypes[i] = unwrapped[i].asTypeTag(); 63 } 64 return unwrappedTypes; 65 } 66 67 public static NATTypeTag atValue(String typeName) { 68 return atValue(AGSymbol.jAlloc(typeName)); 69 } 70 71 public static NATTypeTag atValue(ATSymbol typeName) { 72 return new NATTypeTag(typeName, 73 NATTable.atValue(new ATObject[] { OBJRootType._INSTANCE_ })); 74 } 75 76 public static NATTypeTag atValue(String typeName, NATTypeTag singleParent) { 77 return new NATTypeTag(AGSymbol.jAlloc(typeName), 78 NATTable.atValue(new ATObject[] { singleParent })); 79 } 80 81 /** 82 * Types should not be created directly because it should be verified 83 * that their list of parent types is never empty. Types created 84 * with an empty parent list automatically get assigned the root type 85 * as their single parent. 86 */ 87 public static NATTypeTag atValue(ATSymbol typeName, ATTable parentTypes) { 88 if (parentTypes == NATTable.EMPTY) { 89 return new NATTypeTag(typeName, NATTable.atValue(new ATObject[] { OBJRootType._INSTANCE_ })); 90 } else { 91 return new NATTypeTag(typeName, parentTypes); 92 } 93 } 94 95 /** 96 * The constructor is declared protected such that it cannot be used externally, 97 * but can be used by the OBJRootType class to create a type with an empty 98 * parent table, which is normally not allowed. Hence, by construction the only 99 * type with an empty parent table is the root type. 100 */ 101 protected NATTypeTag(ATSymbol typeName, ATTable parentTypes) { 102 typeName_ = typeName; 103 parentTypes_ = parentTypes; 104 } 105 106 public ATSymbol base_typeName() throws InterpreterException { 107 return typeName_; 108 } 109 110 public ATTable base_superTypes() throws InterpreterException { 111 return parentTypes_; 112 } 113 114 /** 115 * Native implementation of: 116 * 117 * def isSubtypeOf(supertype) { 118 * (supertype.name() == name).or: 119 * { (supertypes.find: { |type| 120 * type.isSubtypeOf(supertype) }) != nil } 121 * }; 122 */ 123 public ATBoolean base_isSubtypeOf(final ATTypeTag supertype) throws InterpreterException { 124 if (supertype.base_typeName().equals(typeName_)) { 125 return NATBoolean._TRUE_; 126 } else { 127 ATObject found = parentTypes_.base_find_(new NativeClosure(this) { 128 public ATObject base_apply(ATTable args) throws InterpreterException { 129 ATTypeTag type = get(args, 1).asTypeTag(); 130 return type.base_isSubtypeOf(supertype); 131 } 132 }); 133 return NATBoolean.atValue(found != OBJNil._INSTANCE_); 134 } 135 } 136 137 /** 138 * Identity of types is based on their name 139 */ 140 public ATBoolean base__opeql__opeql_(ATObject comparand) throws InterpreterException { 141 if (comparand.isTypeTag()) { 142 return comparand.asTypeTag().base_typeName().base__opeql__opeql_(typeName_); 143 } else { 144 return NATBoolean._FALSE_; 145 } 146 } 147 148 public boolean isTypeTag() { return true; } 149 150 public ATTypeTag asTypeTag() { return this; } 151 152 public NATText meta_print() throws InterpreterException { 153 return NATText.atValue("<type tag:"+typeName_+">"); 154 } 155 156 /** 157 * Types are singletons 158 */ 159 public ATObject meta_clone() throws InterpreterException { 160 return this; 161 } 162 163 public ATTable meta_typeTags() throws InterpreterException { 164 return NATTable.of(NativeTypeTags._TYPETAG_, NativeTypeTags._ISOLATE_); 165 } 166 167 /** required as type tags are stored in a hashset in the {@link DiscoveryManager} */ 168 public int hashCode() { return typeName_.hashCode(); } 169 170 /** 171 * The root type of the type hierarchy: every type eventually 172 * has this type as its parent. 173 */ 174 public static class OBJRootType extends NATTypeTag implements ATTypeTag { 175 176 private final static AGSymbol _ROOT_NAME_ = AGSymbol.jAlloc("Type"); 177 178 public static final OBJRootType _INSTANCE_ = new OBJRootType(); 179 180 /** 181 * The root type is named `Type and has no parent types 182 */ 183 private OBJRootType() { 184 super(_ROOT_NAME_, NATTable.EMPTY); 185 } 186 187 /** 188 * The root type is only a subtype of the root type itself 189 */ 190 public ATBoolean base_isSubtypeOf(ATTypeTag supertype) throws InterpreterException { 191 return NATBoolean.atValue(supertype.base_typeName().equals(_ROOT_NAME_)); 192 } 193 194 } 195 196}