* Removed all Id tags.
[cacao.git] / src / vmcore / class.h
1 /* src/vmcore/class.h - class related functions header
2
3    Copyright (C) 1996-2005, 2006, 2007 R. Grafl, A. Krall, C. Kruegel,
4    C. Oates, R. Obermaisser, M. Platter, M. Probst, S. Ring,
5    E. Steiner, C. Thalinger, D. Thuernbeck, P. Tomsich, C. Ullrich,
6    J. Wenninger, Institut f. Computersprachen - TU Wien
7
8    This file is part of CACAO.
9
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.
14
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.
19
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., 51 Franklin Street, Fifth Floor, Boston, MA
23    02110-1301, USA.
24
25 */
26
27
28 #ifndef _CLASS_H
29 #define _CLASS_H
30
31 /* forward typedefs ***********************************************************/
32
33 typedef struct classinfo      classinfo; 
34 typedef struct innerclassinfo innerclassinfo;
35 typedef struct extra_classref extra_classref;
36 typedef struct castinfo       castinfo;
37
38
39 #include "config.h"
40
41 #include <stdint.h>
42
43 #include "vm/types.h"
44
45 #include "toolbox/list.h"
46
47 #include "vm/global.h"
48
49 #if defined(ENABLE_JAVASE)
50 # include "vmcore/annotation.h"
51 #endif
52
53 #include "vmcore/field.h"
54 #include "vmcore/linker.h"
55 #include "vmcore/loader.h"
56 #include "vmcore/method.h"
57 #include "vmcore/references.h"
58 #include "vmcore/utf8.h"
59
60
61 /* class state defines ********************************************************/
62
63 #define CLASS_LOADING         0x0001
64 #define CLASS_LOADED          0x0002
65 #define CLASS_LINKING         0x0004
66 #define CLASS_LINKED          0x0008
67 #define CLASS_INITIALIZING    0x0010
68 #define CLASS_INITIALIZED     0x0020
69 #define CLASS_ERROR           0x0040
70
71
72 /* some macros ****************************************************************/
73
74 #define CLASS_IS_OR_ALMOST_INITIALIZED(c) \
75     (((c)->state & CLASS_INITIALIZING) || ((c)->state & CLASS_INITIALIZED))
76
77
78 /* classinfo ******************************************************************/
79
80 /* We define this dummy structure of java_lang_Class so we can
81    bootstrap cacaoh without needing a java_lang_Class.h file.  Whether
82    the size of the dummy structure is big enough is checked during
83    runtime in vm_create. */
84
85 typedef struct {
86         java_object_t      header;
87 #if defined(WITH_CLASSPATH_GNU)
88         intptr_t           padding[4];
89 #elif defined(WITH_CLASSPATH_SUN)
90         intptr_t           padding[19];
91 #elif defined(WITH_CLASSPATH_CLDC1_1)
92         intptr_t           padding[3];
93 #else
94 # error unknown classpath configuration
95 #endif
96 } dummy_java_lang_Class;
97
98 struct classinfo {                /* class structure                          */
99         dummy_java_lang_Class object;
100
101         s4          flags;            /* ACC flags                                */
102         utf        *name;             /* class name                               */
103
104         s4          cpcount;          /* number of entries in constant pool       */
105         u1         *cptags;           /* constant pool tags                       */
106         voidptr    *cpinfos;          /* pointer to constant pool info structures */
107
108         s4          classrefcount;    /* number of symbolic class references      */
109         constant_classref *classrefs; /* table of symbolic class references       */
110         extra_classref *extclassrefs; /* additional classrefs                     */
111         s4          parseddescsize;   /* size of the parsed descriptors block     */
112         u1         *parseddescs;      /* parsed descriptors                       */
113
114         classref_or_classinfo super;  /* super class                              */
115         classinfo  *sub;              /* sub class pointer                        */
116         classinfo  *nextsub;          /* pointer to next class in sub class list  */
117
118         s4          interfacescount;  /* number of interfaces                     */
119         classref_or_classinfo *interfaces; /* superinterfaces                     */
120
121         s4          fieldscount;      /* number of fields                         */
122         fieldinfo  *fields;           /* field table                              */
123
124         s4          methodscount;     /* number of methods                        */
125         methodinfo *methods;          /* method table                             */
126
127         s4          state;            /* current class state                      */
128         s4          index;            /* hierarchy depth (classes) or index       */
129                                       /* (interfaces)                             */
130         s4          instancesize;     /* size of an instance of this class        */
131
132         vftbl_t    *vftbl;            /* pointer to virtual function table        */
133
134         methodinfo *finalizer;        /* finalizer method                         */
135
136         u2          innerclasscount;  /* number of inner classes                  */
137         innerclassinfo *innerclass;
138
139         classref_or_classinfo  declaringclass;
140         classref_or_classinfo  enclosingclass;  /* enclosing class                */
141         constant_nameandtype  *enclosingmethod; /* enclosing method               */
142
143         utf        *packagename;      /* full name of the package                 */
144         utf        *sourcefile;       /* SourceFile attribute                     */
145 #if defined(ENABLE_JAVASE)
146         utf        *signature;        /* Signature attribute                      */
147 #if defined(ENABLE_ANNOTATIONS)
148         /* all the anntation attributes are NULL (and not a zero length array)
149            if there is nothing */
150         annotation_bytearray_t  *annotations; /* annotations of this class        */
151         
152         annotation_bytearrays_t *method_annotations; /* array of annotations for  */
153                                       /* the methods                              */
154         annotation_bytearrays_t *method_parameterannotations; /* array of         */
155                                       /* parameter annotations for the methods    */
156         annotation_bytearrays_t *method_annotationdefaults; /* array for          */
157                                       /* annotation default values for the        */
158                                       /* methods                                  */
159
160         annotation_bytearrays_t *field_annotations; /* array of annotations for   */
161                                       /* the fields                               */
162 #endif
163 #endif
164         classloader *classloader;       /* NULL for bootstrap classloader         */
165
166 #if defined(ENABLE_JAVASE)
167 # if defined(WITH_CLASSPATH_SUN)
168         java_object_t *protectiondomain;
169 # endif
170 #endif
171 };
172
173
174 /* innerclassinfo *************************************************************/
175
176 struct innerclassinfo {
177         classref_or_classinfo inner_class; /* inner class pointer                 */
178         classref_or_classinfo outer_class; /* outer class pointer                 */
179         utf                  *name;        /* innerclass name                     */
180         s4                    flags;       /* ACC flags                           */
181 };
182
183
184 /* extra_classref **************************************************************
185
186    for classrefs not occurring within descriptors
187
188 *******************************************************************************/
189
190 struct extra_classref {
191         extra_classref    *next;
192         constant_classref  classref;
193 };
194
195
196 /* castinfo *******************************************************************/
197
198 struct castinfo {
199         s4 super_baseval;
200         s4 super_diffval;
201         s4 sub_baseval;
202 };
203
204
205 /* global variables ***********************************************************/
206
207 /* frequently used classes ****************************************************/
208
209 /* important system classes */
210
211 extern classinfo *class_java_lang_Object;
212 extern classinfo *class_java_lang_Class;
213 extern classinfo *class_java_lang_ClassLoader;
214 extern classinfo *class_java_lang_Cloneable;
215 extern classinfo *class_java_lang_SecurityManager;
216 extern classinfo *class_java_lang_String;
217 extern classinfo *class_java_lang_System;
218 extern classinfo *class_java_lang_Thread;
219 extern classinfo *class_java_lang_ThreadGroup;
220 extern classinfo *class_java_lang_VMSystem;
221 extern classinfo *class_java_lang_VMThread;
222 extern classinfo *class_java_io_Serializable;
223
224 #if defined(WITH_CLASSPATH_SUN)
225 extern classinfo *class_sun_reflect_MagicAccessorImpl;
226 #endif
227
228 /* system exception classes required in cacao */
229
230 extern classinfo *class_java_lang_Throwable;
231 extern classinfo *class_java_lang_Error;
232 extern classinfo *class_java_lang_LinkageError;
233 extern classinfo *class_java_lang_NoClassDefFoundError;
234 extern classinfo *class_java_lang_OutOfMemoryError;
235 extern classinfo *class_java_lang_VirtualMachineError;
236
237 #if defined(WITH_CLASSPATH_GNU)
238 extern classinfo *class_java_lang_VMThrowable;
239 #endif
240
241 extern classinfo *class_java_lang_Exception;
242 extern classinfo *class_java_lang_ClassCastException;
243 extern classinfo *class_java_lang_ClassNotFoundException;
244
245 #if defined(ENABLE_JAVASE)
246 extern classinfo *class_java_lang_Void;
247
248 #if defined(ENABLE_ANNOTATIONS)
249 extern classinfo *class_sun_reflect_ConstantPool;
250 #if defined(WITH_CLASSPATH_GNU)
251 extern classinfo *class_sun_reflect_annotation_AnnotationParser;
252 #endif
253 #endif
254 #endif
255
256 extern classinfo *class_java_lang_Boolean;
257 extern classinfo *class_java_lang_Byte;
258 extern classinfo *class_java_lang_Character;
259 extern classinfo *class_java_lang_Short;
260 extern classinfo *class_java_lang_Integer;
261 extern classinfo *class_java_lang_Long;
262 extern classinfo *class_java_lang_Float;
263 extern classinfo *class_java_lang_Double;
264
265
266 /* some runtime exception */
267
268 extern classinfo *class_java_lang_NullPointerException;
269
270
271 /* some classes which may be used more often */
272
273 #if defined(ENABLE_JAVASE)
274 extern classinfo *class_java_lang_StackTraceElement;
275 extern classinfo *class_java_lang_reflect_Constructor;
276 extern classinfo *class_java_lang_reflect_Field;
277 extern classinfo *class_java_lang_reflect_Method;
278 extern classinfo *class_java_security_PrivilegedAction;
279 extern classinfo *class_java_util_Vector;
280
281 extern classinfo *arrayclass_java_lang_Object;
282 #endif
283
284
285 /* pseudo classes for the type checker ****************************************/
286
287 /*
288  * pseudo_class_Arraystub
289  *     (extends Object implements Cloneable, java.io.Serializable)
290  *
291  *     If two arrays of incompatible component types are merged,
292  *     the resulting reference has no accessible components.
293  *     The result does, however, implement the interfaces Cloneable
294  *     and java.io.Serializable. This pseudo class is used internally
295  *     to represent such results. (They are *not* considered arrays!)
296  *
297  * pseudo_class_Null
298  *
299  *     This pseudo class is used internally to represent the
300  *     null type.
301  *
302  * pseudo_class_New
303  *
304  *     This pseudo class is used internally to represent the
305  *     the 'uninitialized object' type.
306  */
307
308 extern classinfo *pseudo_class_Arraystub;
309 extern classinfo *pseudo_class_Null;
310 extern classinfo *pseudo_class_New;
311
312
313 /* function prototypes ********************************************************/
314
315 classinfo *class_create_classinfo(utf *u);
316 void       class_postset_header_vftbl(void);
317 classinfo *class_define(utf *name, classloader *cl, int32_t length, const uint8_t *data, java_handle_t *pd);
318 void       class_set_packagename(classinfo *c);
319
320 bool       class_load_attributes(classbuffer *cb);
321
322 /* retrieve constantpool element */
323 voidptr class_getconstant(classinfo *class, u4 pos, u4 ctype);
324 voidptr innerclass_getconstant(classinfo *c, u4 pos, u4 ctype);
325
326 /* frees all resources used by the class */
327 void class_free(classinfo *);
328
329 /* return an array class with the given component class */
330 classinfo *class_array_of(classinfo *component,bool link);
331
332 /* return an array class with the given dimension and element class */
333 classinfo *class_multiarray_of(s4 dim, classinfo *element,bool link);
334
335 /* return a classref for the given class name */
336 /* (does a linear search!)                    */
337 constant_classref *class_lookup_classref(classinfo *cls,utf *name);
338
339 /* return a classref for the given class name */
340 /* (does a linear search!)                    */
341 constant_classref *class_get_classref(classinfo *cls,utf *name);
342
343 /* return a classref to the class itself */
344 /* (does a linear search!)                    */
345 constant_classref *class_get_self_classref(classinfo *cls);
346
347 /* return a classref for an array with the given dimension of with the */
348 /* given component type */
349 constant_classref *class_get_classref_multiarray_of(s4 dim,constant_classref *ref);
350
351 /* return a classref for the component type of the given array type */
352 constant_classref *class_get_classref_component_of(constant_classref *ref);
353
354 /* get a class' field by name and descriptor */
355 fieldinfo *class_findfield(classinfo *c, utf *name, utf *desc);
356
357 /* search 'classinfo'-structure for a field with the specified name */
358 fieldinfo *class_findfield_by_name(classinfo *c, utf *name);
359 s4 class_findfield_index_by_name(classinfo *c, utf *name);
360
361 /* search class for a field */
362 fieldinfo *class_resolvefield(classinfo *c, utf *name, utf *desc, classinfo *referer, bool throwexception);
363
364 /* search for a method with a specified name and descriptor */
365 methodinfo *class_findmethod(classinfo *c, utf *name, utf *desc);
366 methodinfo *class_resolvemethod(classinfo *c, utf *name, utf *dest);
367 methodinfo *class_resolveclassmethod(classinfo *c, utf *name, utf *dest, classinfo *referer, bool throwexception);
368 methodinfo *class_resolveinterfacemethod(classinfo *c, utf *name, utf *dest, classinfo *referer, bool throwexception);
369
370 bool class_issubclass(classinfo *sub, classinfo *super);
371 bool class_isanysubclass(classinfo *sub, classinfo *super);
372
373 bool                       class_is_primitive(classinfo *c);
374 bool                       class_is_anonymousclass(classinfo *c);
375 bool                       class_is_array(classinfo *c);
376 bool                       class_is_interface(classinfo *c);
377 bool                       class_is_localclass(classinfo *c);
378 bool                       class_is_memberclass(classinfo *c);
379
380 classinfo                 *class_get_superclass(classinfo *c);
381 classinfo                 *class_get_componenttype(classinfo *c);
382 java_handle_objectarray_t *class_get_declaredclasses(classinfo *c, bool publicOnly);
383 classinfo                 *class_get_declaringclass(classinfo *c);
384 classinfo                 *class_get_enclosingclass(classinfo *c);
385 java_handle_objectarray_t *class_get_interfaces(classinfo *c);
386 java_handle_bytearray_t   *class_get_annotations(classinfo *c);
387
388 #if defined(ENABLE_JAVASE)
389 utf                       *class_get_signature(classinfo *c);
390 #endif
391
392 /* some debugging functions */
393
394 #if !defined(NDEBUG)
395 void class_printflags(classinfo *c);
396 void class_print(classinfo *c);
397 void class_println(classinfo *c);
398 void class_classref_print(constant_classref *cr);
399 void class_classref_println(constant_classref *cr);
400 void class_classref_or_classinfo_print(classref_or_classinfo c);
401 void class_classref_or_classinfo_println(classref_or_classinfo c);
402 #endif
403
404 /* debug purposes */
405 void class_showmethods(classinfo *c);
406 void class_showconstantpool(classinfo *c);
407
408 #endif /* _CLASS_H */
409
410
411 /*
412  * These are local overrides for various environment variables in Emacs.
413  * Please do not remove this and leave it at the end of the file, where
414  * Emacs will automagically detect them.
415  * ---------------------------------------------------------------------
416  * Local variables:
417  * mode: c
418  * indent-tabs-mode: t
419  * c-basic-offset: 4
420  * tab-width: 4
421  * End:
422  */