1 /* typeinfo.h - type system used by the type checker
3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003
4 R. Grafl, A. Krall, C. Kruegel, C. Oates, R. Obermaisser,
5 M. Probst, S. Ring, E. Steiner, C. Thalinger, D. Thuernbeck,
6 P. Tomsich, J. Wenninger
8 This file is part of CACAO.
10 This program is free software; you can redistribute it and/or
11 modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2, or (at
13 your option) any later version.
15 This program is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
25 Contact: cacao@complang.tuwien.ac.at
27 Authors: Edwin Steiner
29 $Id: typeinfo.h 719 2003-12-08 14:26:05Z edwin $
40 /* resolve typedef cycles *****************************************************/
42 typedef struct typeinfo typeinfo;
43 typedef struct typeinfo_mergedlist typeinfo_mergedlist;
45 /* global variables ***********************************************************/
47 /* XXX move this documentation to global.h */
48 /* The following classinfo pointers are used internally by the type system.
49 * Please do not use them directly, use the TYPEINFO_ macros instead.
53 * pseudo_class_Arraystub
54 * (extends Object implements Cloneable, java.io.Serializable)
56 * If two arrays of incompatible component types are merged,
57 * the resulting reference has no accessible components.
58 * The result does, however, implement the interfaces Cloneable
59 * and java.io.Serializable. This pseudo class is used internally
60 * to represent such results. (They are *not* considered arrays!)
64 * This pseudo class is used internally to represent the
68 /* data structures for the type system ****************************************/
70 /* The typeinfo structure stores detailed information on reference types.
71 * (stack elements, variables, etc. with type == TYPE_ADR.)
72 * XXX: exclude ReturnAddresses?
74 * For primitive types either there is no typeinfo allocated or the
75 * typeclass pointer in the typeinfo struct is NULL.
77 * CAUTION: The typeinfo structure should be considered opaque outside of
78 * typeinfo.[ch]. Please use the macros and functions defined here to
79 * access typeinfo structures!
82 /* At all times *exactly one* of the following conditions is true for
83 * a particular typeinfo struct:
85 * A) typeclass == NULL
87 * In this case the other fields of the structure
90 * B) typeclass == pseudo_class_Null
94 * C) typeclass is an array class
98 * D) typeclass == pseudo_class_Arraystub
102 * E) typeclass is an interface
106 * F) typeclass is a (non-pseudo-)class != java.lang.Object
109 * All classinfos in u.merged.list (if any) are
110 * subclasses of typeclass.
112 * G) typeclass is java.lang.Object
115 * In this case u.merged.count and u.merged.list
116 * are valid and may be non-zero.
117 * The classinfos in u.merged.list (if any) can be
118 * classes, interfaces or pseudo classes.
121 /* The following algorithm is used to determine if the type described
122 * by this typeinfo struct supports the interface X:
124 * 1) If typeclass is X or a subinterface of X the answer is "yes".
125 * 2) If typeclass is a (pseudo) class implementing X the answer is "yes".
126 * 3) XXX If typeclass is not an array and u.merged.count>0
127 * and all classes/interfaces in u.merged.list implement X
128 * the answer is "yes".
129 * 4) If none of the above is true the answer is "no".
133 * CAUTION: The typeinfo structure should be considered opaque outside of
134 * typeinfo.[ch]. Please use the macros and functions defined here to
135 * access typeinfo structures!
138 classinfo *typeclass;
139 classinfo *elementclass; /* valid if dimension>0 */
140 typeinfo_mergedlist *merged;
142 u1 elementtype; /* valid if dimension>0 */
145 struct typeinfo_mergedlist {
147 classinfo *list[1]; /* variable length! */
150 /****************************************************************************/
152 /****************************************************************************/
154 /* NOTE: These macros take typeinfo *structs* not pointers as arguments.
155 * You have to dereference any pointers.
158 /* internally used macros ***************************************************/
160 /* internal, don't use this explicitly! */
161 #define TYPEINFO_ALLOCMERGED(mergedlist,count) \
162 do {(mergedlist) = (typeinfo_mergedlist*)dump_alloc( \
163 sizeof(typeinfo_mergedlist) \
164 + ((count)-1)*sizeof(classinfo*));} while(0)
166 /* internal, don't use this explicitly! */
167 #define TYPEINFO_FREEMERGED(mergedlist)
169 /* internal, don't use this explicitly! */
170 #define TYPEINFO_FREEMERGED_IF_ANY(mergedlist)
172 /* macros for type queries **************************************************/
174 #define TYPEINFO_IS_PRIMITIVE(info) \
175 ((info).typeclass == NULL)
177 #define TYPEINFO_IS_REFERENCE(info) \
178 ((info).typeclass != NULL)
180 #define TYPEINFO_IS_NULLTYPE(info) \
181 ((info).typeclass == pseudo_class_Null)
183 #define TYPEINFO_IS_NEWOBJECT(info) \
184 ((info).typeclass == pseudo_class_New)
186 /* only use this if TYPEINFO_IS_NEWOBJECT returned true! */
187 #define TYPEINFO_NEWOBJECT_INSTRUCTION(info) \
188 ((void *)(info).elementclass)
190 /* macros for array type queries ********************************************/
192 #define TYPEINFO_IS_ARRAY(info) \
193 ( TYPEINFO_IS_REFERENCE(info) \
194 && ((info).dimension != 0) )
196 #define TYPEINFO_IS_SIMPLE_ARRAY(info) \
197 ( ((info).dimension == 1) )
199 #define TYPEINFO_IS_ARRAY_ARRAY(info) \
200 ( ((info).dimension >= 2) )
202 #define TYPEINFO_IS_PRIMITIVE_ARRAY(info,arraytype) \
203 ( TYPEINFO_IS_SIMPLE_ARRAY(info) \
204 && ((info).elementtype == (arraytype)) )
206 #define TYPEINFO_IS_OBJECT_ARRAY(info) \
207 ( TYPEINFO_IS_SIMPLE_ARRAY(info) \
208 && ((info).elementclass != NULL) )
210 /* assumes that info describes an array type */
211 #define TYPEINFO_IS_ARRAY_OF_REFS_NOCHECK(info) \
212 ( ((info).elementclass != NULL) \
213 || ((info).dimension >= 2) )
215 #define TYPEINFO_IS_ARRAY_OF_REFS(info) \
216 ( TYPEINFO_IS_ARRAY(info) \
217 && TYPEINFO_IS_ARRAY_OF_REFS_NOCHECK(info) )
219 /* queries allowing the null type ********************************************/
221 #define TYPEINFO_MAYBE_ARRAY(info) \
222 (TYPEINFO_IS_ARRAY(info) || TYPEINFO_IS_NULLTYPE(info))
224 #define TYPEINFO_MAYBE_PRIMITIVE_ARRAY(info,at) \
225 (TYPEINFO_IS_PRIMITIVE_ARRAY(info,at) || TYPEINFO_IS_NULLTYPE(info))
227 #define TYPEINFO_MAYBE_ARRAY_OF_REFS(info) \
228 (TYPEINFO_IS_ARRAY_OF_REFS(info) || TYPEINFO_IS_NULLTYPE(info))
230 /* macros for initializing typeinfo structures ******************************/
232 #define TYPEINFO_INIT_PRIMITIVE(info) \
233 do {(info).typeclass = NULL; \
234 (info).elementclass = NULL; \
235 (info).merged = NULL; \
236 (info).dimension = 0; \
237 (info).elementtype = 0;} while(0)
239 #define TYPEINFO_INIT_NON_ARRAY_CLASSINFO(info,cinfo) \
240 do {(info).typeclass = (cinfo); \
241 (info).elementclass = NULL; \
242 (info).merged = NULL; \
243 (info).dimension = 0; \
244 (info).elementtype = 0;} while(0)
246 #define TYPEINFO_INIT_NULLTYPE(info) \
247 TYPEINFO_INIT_CLASSINFO(info,pseudo_class_Null)
249 #define TYPEINFO_INIT_NEWOBJECT(info,instr) \
250 do {(info).typeclass = pseudo_class_New; \
251 (info).elementclass = (classinfo*) (instr);\
252 (info).merged = NULL; \
253 (info).dimension = 0; \
254 (info).elementtype = 0;} while(0)
256 #define TYPEINFO_INIT_PRIMITIVE_ARRAY(info,arraytype) \
257 TYPEINFO_INIT_CLASSINFO(info,primitivetype_table[arraytype].arrayclass);
259 #define TYPEINFO_INIT_CLASSINFO(info,cls) \
260 do {if (((info).typeclass = (cls))->vftbl->arraydesc) { \
261 if ((cls)->vftbl->arraydesc->elementvftbl) \
262 (info).elementclass = (cls)->vftbl->arraydesc->elementvftbl->class; \
264 (info).elementclass = NULL; \
265 (info).dimension = (cls)->vftbl->arraydesc->dimension; \
266 (info).elementtype = (cls)->vftbl->arraydesc->elementtype; \
269 (info).elementclass = NULL; \
270 (info).dimension = 0; \
271 (info).elementtype = 0; \
273 (info).merged = NULL;} while(0)
275 #define TYPEINFO_INIT_FROM_FIELDINFO(info,fi) \
276 typeinfo_init_from_descriptor(&(info), \
277 (fi)->descriptor->text,utf_end((fi)->descriptor));
279 /* macros for writing types (destination must have been initialized) ********/
280 /* XXX delete them? */
283 #define TYPEINFO_PUT_NULLTYPE(info) \
284 do {(info).typeclass = pseudo_class_Null;} while(0)
286 #define TYPEINFO_PUT_NON_ARRAY_CLASSINFO(info,cinfo) \
287 do {(info).typeclass = (cinfo);} while(0)
289 #define TYPEINFO_PUT_CLASSINFO(info,cls) \
290 do {if (((info).typeclass = (cls))->vftbl->arraydesc) { \
291 if ((cls)->vftbl->arraydesc->elementvftbl) \
292 (info).elementclass = (cls)->vftbl->arraydesc->elementvftbl->class; \
293 (info).dimension = (cls)->vftbl->arraydesc->dimension; \
294 (info).elementtype = (cls)->vftbl->arraydesc->elementtype; \
297 /* srcarray must be an array (not checked) */
298 #define TYPEINFO_PUT_COMPONENT(srcarray,dst) \
299 do {typeinfo_put_component(&(srcarray),&(dst));} while(0)
303 /* macros for copying types (destinition is not checked or freed) ***********/
305 /* TYPEINFO_COPY makes a shallow copy, the merged pointer is simply copied. */
306 #define TYPEINFO_COPY(src,dst) \
307 do {(dst) = (src);} while(0)
309 /* TYPEINFO_CLONE makes a deep copy, the merged list (if any) is duplicated
310 * into a newly allocated array.
312 #define TYPEINFO_CLONE(src,dst) \
314 if ((dst).merged) typeinfo_clone(&(src),&(dst));} while(0)
316 /****************************************************************************/
318 /****************************************************************************/
320 /* inquiry functions (read-only) ********************************************/
322 bool typeinfo_is_array(typeinfo *info);
323 bool typeinfo_is_primitive_array(typeinfo *info,int arraytype);
324 bool typeinfo_is_array_of_refs(typeinfo *info);
326 bool typeinfo_implements_interface(typeinfo *info,classinfo *interf);
327 bool typeinfo_is_assignable(typeinfo *value,typeinfo *dest);
329 /* initialization functions *************************************************/
331 void typeinfo_init_from_descriptor(typeinfo *info,char *utf_ptr,char *end_ptr);
332 void typeinfo_init_component(typeinfo *srcarray,typeinfo *dst);
334 int typeinfo_count_method_args(utf *d,bool twoword); /* this not included */
335 void typeinfo_init_from_method_args(utf *desc,u1 *typebuf,
337 int buflen,bool twoword,
338 int *returntype,typeinfo *returntypeinfo);
340 void typeinfo_clone(typeinfo *src,typeinfo *dest);
342 /* functions for the type system ********************************************/
344 void typeinfo_free(typeinfo *info);
346 bool typeinfo_merge(typeinfo *dest,typeinfo* y);
348 /* debugging helpers ********************************************************/
350 #ifdef TYPEINFO_DEBUG
352 void typeinfo_test();
353 void typeinfo_init_from_fielddescriptor(typeinfo *info,char *desc);
354 void typeinfo_print(FILE *file,typeinfo *info,int indent);
355 void typeinfo_print_short(FILE *file,typeinfo *info);
356 void typeinfo_print_type(FILE *file,int type,typeinfo *info);
358 #endif // TYPEINFO_DEBUG
360 #endif /* _TYPEINFO_H */
364 * These are local overrides for various environment variables in Emacs.
365 * Please do not remove this and leave it at the end of the file, where
366 * Emacs will automagically detect them.
367 * ---------------------------------------------------------------------
370 * indent-tabs-mode: t