PageRenderTime 22ms CodeModel.GetById 1ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

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