/interpreter/tags/at2dist220411/src/edu/vub/at/objects/natives/NATTypeTag.java

http://ambienttalk.googlecode.com/ · Java · 264 lines · 147 code · 34 blank · 83 comment · 17 complexity · 9487b1fd68145bc018e0058e94ca5757 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. */
  28. package edu.vub.at.objects.natives;
  29. import java.lang.reflect.Field;
  30. import java.util.HashMap;
  31. import java.util.Set;
  32. import java.util.Vector;
  33. import edu.vub.at.actors.natives.DiscoveryManager;
  34. import edu.vub.at.eval.Evaluator;
  35. import edu.vub.at.exceptions.InterpreterException;
  36. import edu.vub.at.objects.ATBoolean;
  37. import edu.vub.at.objects.ATMethod;
  38. import edu.vub.at.objects.ATObject;
  39. import edu.vub.at.objects.ATTable;
  40. import edu.vub.at.objects.ATTypeTag;
  41. import edu.vub.at.objects.coercion.NativeTypeTags;
  42. import edu.vub.at.objects.grammar.ATSymbol;
  43. import edu.vub.at.objects.mirrors.NATMirrorRoot;
  44. import edu.vub.at.objects.mirrors.NativeClosure;
  45. import edu.vub.at.objects.natives.grammar.AGSymbol;
  46. import edu.vub.util.TempFieldGenerator;
  47. /**
  48. * The native implementation of AmbientTalk type tag objects.
  49. *
  50. * In principle, care should be taken that all objects implementing the
  51. * type tag interface are isolates, because type tags are usually attributed
  52. * to messages which are isolates themselves.
  53. *
  54. * @author tvcutsem
  55. */
  56. public class NATTypeTag extends NATByCopy implements ATTypeTag {
  57. private final ATSymbol typeName_;
  58. private final ATTable parentTypes_;
  59. public static ATTypeTag[] toTypeTagArray(ATTable types) throws InterpreterException {
  60. if (types == NATTable.EMPTY) {
  61. return NATObject._NO_TYPETAGS_;
  62. }
  63. ATObject[] unwrapped = types.asNativeTable().elements_;
  64. ATTypeTag[] unwrappedTypes = new ATTypeTag[unwrapped.length];
  65. for (int i = 0; i < unwrappedTypes.length; i++) {
  66. unwrappedTypes[i] = unwrapped[i].asTypeTag();
  67. }
  68. return unwrappedTypes;
  69. }
  70. public static NATTypeTag atValue(String typeName) {
  71. return atValue(AGSymbol.jAlloc(typeName));
  72. }
  73. public static NATTypeTag atValue(ATSymbol typeName) {
  74. return new NATTypeTag(typeName,
  75. NATTable.atValue(new ATObject[] { OBJRootType._INSTANCE_ }));
  76. }
  77. public static NATTypeTag atValue(String typeName, NATTypeTag singleParent) {
  78. return new NATTypeTag(AGSymbol.jAlloc(typeName),
  79. NATTable.atValue(new ATObject[] { singleParent }));
  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. * The constructor is declared protected such that it cannot be used externally,
  96. * but can be used by the OBJRootType class to create a type with an empty
  97. * parent table, which is normally not allowed. Hence, by construction the only
  98. * type with an empty parent table is the root type.
  99. */
  100. protected NATTypeTag(ATSymbol typeName, ATTable parentTypes) {
  101. typeName_ = typeName;
  102. parentTypes_ = parentTypes;
  103. }
  104. public ATSymbol base_typeName() throws InterpreterException {
  105. return typeName_;
  106. }
  107. public ATTable base_superTypes() throws InterpreterException {
  108. return parentTypes_;
  109. }
  110. /**
  111. * Native implementation of:
  112. *
  113. * def isSubtypeOf(supertype) {
  114. * (supertype.name() == name).or:
  115. * { (supertypes.find: { |type|
  116. * type.isSubtypeOf(supertype) }) != nil }
  117. * };
  118. */
  119. public ATBoolean base_isSubtypeOf(final ATTypeTag supertype) throws InterpreterException {
  120. if (supertype.base_typeName().equals(typeName_)) {
  121. return NATBoolean._TRUE_;
  122. } else {
  123. ATObject found = parentTypes_.base_find_(new NativeClosure(this) {
  124. public ATObject base_apply(ATTable args) throws InterpreterException {
  125. ATTypeTag type = get(args, 1).asTypeTag();
  126. return type.base_isSubtypeOf(supertype);
  127. }
  128. });
  129. return NATBoolean.atValue(found != Evaluator.getNil());
  130. }
  131. }
  132. /**
  133. * By default, annotateMessage is the identity function, it does not add any new metadata
  134. * to the message.
  135. */
  136. public ATObject base_annotateMessage(ATObject originalMessage) throws InterpreterException {
  137. return originalMessage;
  138. }
  139. /**
  140. * By default, annotateMethod is the identity function, it does not add any new metadata
  141. * to the method.
  142. */
  143. public ATMethod base_annotateMethod(ATMethod originalMethod) throws InterpreterException {
  144. return originalMethod;
  145. }
  146. /**
  147. * Identity of types is based on their name
  148. */
  149. public ATBoolean base__opeql__opeql_(ATObject comparand) throws InterpreterException {
  150. if (comparand.isTypeTag()) {
  151. return NATBoolean.atValue(comparand.asTypeTag().base_typeName().equals(typeName_));
  152. } else {
  153. return NATBoolean._FALSE_;
  154. }
  155. }
  156. public boolean isTypeTag() { return true; }
  157. public ATTypeTag asTypeTag() { return this; }
  158. public NATText meta_print() throws InterpreterException {
  159. return NATText.atValue("<type tag:"+typeName_+">");
  160. }
  161. public NATText impl_asCode(TempFieldGenerator objectMap) throws InterpreterException {
  162. if(objectMap.containsType(this)) {
  163. return objectMap.getTypeName(this);
  164. }
  165. String typeName = typeName_.toString();
  166. final Set<NATTypeTag> natives = NativeTypeTags.getNativeTypeTags();
  167. ATObject[] pTypes = parentTypes_.base_filter_(new NativeClosure(this) {
  168. public ATObject base_apply(ATTable args) throws InterpreterException {
  169. NATTypeTag tt = (NATTypeTag) args.base_at(NATNumber.ONE);
  170. // filter out the root type and types native to the interpreter
  171. return NATBoolean.atValue(
  172. !(args.base_at(NATNumber.ONE).equals(NATTypeTag.OBJRootType._INSTANCE_) &&
  173. !(natives.contains(args.base_at(NATNumber.ONE))))
  174. );
  175. }
  176. }).asNativeTable().elements_;
  177. NATText name;
  178. if (pTypes.length == 0) {
  179. name = objectMap.putType(
  180. this,
  181. NATText.atValue(typeName),
  182. NATText.atValue("deftype " + typeName));
  183. } else {
  184. StringBuffer out = new StringBuffer("deftype " + typeName + " <: ");
  185. for (int i = 0 ; i < pTypes.length ; i++) {
  186. NATText parentName = pTypes[i].impl_asCode(objectMap);
  187. out.append(parentName.javaValue);
  188. if (i < pTypes.length - 1) {
  189. out.append(", ");
  190. }
  191. }
  192. name = objectMap.putType(
  193. this,
  194. NATText.atValue(typeName),
  195. NATText.atValue(out.toString()));
  196. }
  197. return name;
  198. }
  199. /**
  200. * Types are singletons
  201. */
  202. public ATObject meta_clone() throws InterpreterException {
  203. return this;
  204. }
  205. public ATTable meta_typeTags() throws InterpreterException {
  206. return NATTable.of(NativeTypeTags._TYPETAG_, NativeTypeTags._ISOLATE_);
  207. }
  208. /** required as type tags are stored in a hashset in the {@link DiscoveryManager} */
  209. public int hashCode() { return typeName_.hashCode(); }
  210. /**
  211. * The root type of the type hierarchy: every type eventually
  212. * has this type as its parent.
  213. */
  214. public static class OBJRootType extends NATTypeTag implements ATTypeTag {
  215. private final static AGSymbol _ROOT_NAME_ = AGSymbol.jAlloc("Type");
  216. public static final OBJRootType _INSTANCE_ = new OBJRootType();
  217. /**
  218. * The root type is named `Type and has no parent types
  219. */
  220. private OBJRootType() {
  221. super(_ROOT_NAME_, NATTable.EMPTY);
  222. }
  223. /**
  224. * The root type is only a subtype of the root type itself
  225. */
  226. public ATBoolean base_isSubtypeOf(ATTypeTag supertype) throws InterpreterException {
  227. return NATBoolean.atValue(supertype.base_typeName().equals(_ROOT_NAME_));
  228. }
  229. }
  230. }