4cb801dfd61fea3fb7c8472d54fdd05302eab5b9
[cacao.git] / src / vmcore / class.h
1 /* src/vmcore/class.h - class related functions header
2
3    Copyright (C) 1996-2005, 2006, 2007, 2008
4    CACAOVM - Verein zur Foerderung der freien virtuellen Maschine CACAO
5
6    This file is part of CACAO.
7
8    This program is free software; you can redistribute it and/or
9    modify it under the terms of the GNU General Public License as
10    published by the Free Software Foundation; either version 2, or (at
11    your option) any later version.
12
13    This program is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21    02110-1301, USA.
22
23 */
24
25
26 #ifndef _CLASS_H
27 #define _CLASS_H
28
29 /* forward typedefs ***********************************************************/
30
31 typedef struct classinfo      classinfo; 
32 typedef struct innerclassinfo innerclassinfo;
33 typedef struct extra_classref extra_classref;
34 typedef struct castinfo       castinfo;
35
36
37 #include "config.h"
38
39 #include <stdint.h>
40
41 #include "vm/types.h"
42
43 #include "toolbox/list.h"
44
45 #include "vm/global.h"
46
47 #if defined(ENABLE_JAVASE)
48 # include "vmcore/annotation.h"
49 #endif
50
51 #include "vmcore/field.h"
52 #include "vmcore/linker.h"
53 #include "vmcore/loader.h"
54 #include "vmcore/method.h"
55 #include "vmcore/references.h"
56 #include "vmcore/utf8.h"
57
58
59 /* class state defines ********************************************************/
60
61 #define CLASS_LOADING         0x0001
62 #define CLASS_LOADED          0x0002
63 #define CLASS_LINKING         0x0004
64 #define CLASS_LINKED          0x0008
65 #define CLASS_INITIALIZING    0x0010
66 #define CLASS_INITIALIZED     0x0020
67 #define CLASS_ERROR           0x0040
68
69
70 /* some macros ****************************************************************/
71
72 #define CLASS_IS_OR_ALMOST_INITIALIZED(c) \
73     (((c)->state & CLASS_INITIALIZING) || ((c)->state & CLASS_INITIALIZED))
74
75
76 /* classinfo ******************************************************************/
77
78 /* We define this dummy structure of java_lang_Class so we can
79    bootstrap cacaoh without needing a java_lang_Class.h file.  Whether
80    the size of the dummy structure is big enough is checked during
81    runtime in vm_create. */
82
83 typedef struct {
84         java_object_t      header;
85 #if defined(WITH_CLASSPATH_GNU)
86         intptr_t           padding[4];
87 #elif defined(WITH_CLASSPATH_SUN)
88         intptr_t           padding[19];
89 #elif defined(WITH_CLASSPATH_CLDC1_1)
90         intptr_t           padding[3];
91 #else
92 # error unknown classpath configuration
93 #endif
94 } dummy_java_lang_Class;
95
96 struct classinfo {                /* class structure                          */
97         dummy_java_lang_Class object;
98
99         s4          flags;            /* ACC flags                                */
100         utf        *name;             /* class name                               */
101
102         s4          cpcount;          /* number of entries in constant pool       */
103         u1         *cptags;           /* constant pool tags                       */
104         voidptr    *cpinfos;          /* pointer to constant pool info structures */
105
106         s4          classrefcount;    /* number of symbolic class references      */
107         constant_classref *classrefs; /* table of symbolic class references       */
108         extra_classref *extclassrefs; /* additional classrefs                     */
109         s4          parseddescsize;   /* size of the parsed descriptors block     */
110         u1         *parseddescs;      /* parsed descriptors                       */
111
112         classinfo  *super;            /* super class                              */
113         classinfo  *sub;              /* sub class pointer                        */
114         classinfo  *nextsub;          /* pointer to next class in sub class list  */
115
116         int32_t     interfacescount;  /* number of interfaces                     */
117         classinfo **interfaces;       /* super interfaces                         */
118
119         int32_t     fieldscount;      /* number of fields                         */
120         fieldinfo  *fields;           /* field table                              */
121
122         int32_t     methodscount;     /* number of methods                        */
123         methodinfo *methods;          /* method table                             */
124
125         s4          state;            /* current class state                      */
126         s4          index;            /* hierarchy depth (classes) or index       */
127                                       /* (interfaces)                             */
128         s4          instancesize;     /* size of an instance of this class        */
129
130         vftbl_t    *vftbl;            /* pointer to virtual function table        */
131
132         methodinfo *finalizer;        /* finalizer method                         */
133
134         u2          innerclasscount;  /* number of inner classes                  */
135         innerclassinfo *innerclass;
136
137         classref_or_classinfo  declaringclass;
138         classref_or_classinfo  enclosingclass;  /* enclosing class                */
139         constant_nameandtype  *enclosingmethod; /* enclosing method               */
140
141         utf        *packagename;      /* full name of the package                 */
142         utf        *sourcefile;       /* SourceFile attribute                     */
143 #if defined(ENABLE_JAVASE)
144         utf        *signature;        /* Signature attribute                      */
145 #if defined(ENABLE_ANNOTATIONS)
146         /* All the annotation attributes are NULL (and not a zero length array)   */
147         /* if there is nothing.                                                   */
148         java_object_t *annotations;   /* annotations of this class                */
149         
150         java_object_t *method_annotations; /* array of annotations of the methods */
151         java_object_t *method_parameterannotations; /* array of parameter         */
152                                       /* annotations of the methods               */
153         java_object_t *method_annotationdefaults; /* array of annotation default  */
154                                       /* values of the methods                    */
155
156         java_object_t *field_annotations; /* array of annotations of the fields   */
157
158 #endif
159 #endif
160         classloader *classloader;       /* NULL for bootstrap classloader         */
161
162 #if defined(ENABLE_JAVASE)
163 # if defined(WITH_CLASSPATH_SUN)
164         java_object_t      *protectiondomain;
165         java_objectarray_t *signers;
166 # endif
167 #endif
168 };
169
170
171 /* innerclassinfo *************************************************************/
172
173 struct innerclassinfo {
174         classref_or_classinfo inner_class; /* inner class pointer                 */
175         classref_or_classinfo outer_class; /* outer class pointer                 */
176         utf                  *name;        /* innerclass name                     */
177         s4                    flags;       /* ACC flags                           */
178 };
179
180
181 /* extra_classref **************************************************************
182
183    for classrefs not occurring within descriptors
184
185 *******************************************************************************/
186
187 struct extra_classref {
188         extra_classref    *next;
189         constant_classref  classref;
190 };
191
192
193 /* castinfo *******************************************************************/
194
195 struct castinfo {
196         s4 super_baseval;
197         s4 super_diffval;
198         s4 sub_baseval;
199 };
200
201
202 /* global variables ***********************************************************/
203
204 /* frequently used classes ****************************************************/
205
206 /* Important system classes. */
207
208 extern classinfo *class_java_lang_Object;
209 extern classinfo *class_java_lang_Class;
210 extern classinfo *class_java_lang_ClassLoader;
211 extern classinfo *class_java_lang_Cloneable;
212 extern classinfo *class_java_lang_SecurityManager;
213 extern classinfo *class_java_lang_String;
214 extern classinfo *class_java_lang_System;
215 extern classinfo *class_java_lang_Thread;
216 extern classinfo *class_java_lang_ThreadGroup;
217 extern classinfo *class_java_lang_Throwable;
218 extern classinfo *class_java_io_Serializable;
219
220 /* Important system exceptions. */
221
222 extern classinfo *class_java_lang_Exception;
223 extern classinfo *class_java_lang_ClassNotFoundException;
224 extern classinfo *class_java_lang_RuntimeException;
225
226 #if defined(WITH_CLASSPATH_GNU)
227 extern classinfo *class_java_lang_VMSystem;
228 extern classinfo *class_java_lang_VMThread;
229 extern classinfo *class_java_lang_VMThrowable;
230 #endif
231
232 #if defined(WITH_CLASSPATH_SUN)
233 extern classinfo *class_sun_reflect_MagicAccessorImpl;
234 #endif
235
236 #if defined(ENABLE_JAVASE)
237 extern classinfo *class_java_lang_Void;
238 #endif
239
240 extern classinfo *class_java_lang_Boolean;
241 extern classinfo *class_java_lang_Byte;
242 extern classinfo *class_java_lang_Character;
243 extern classinfo *class_java_lang_Short;
244 extern classinfo *class_java_lang_Integer;
245 extern classinfo *class_java_lang_Long;
246 extern classinfo *class_java_lang_Float;
247 extern classinfo *class_java_lang_Double;
248
249 /* some classes which may be used more often */
250
251 #if defined(ENABLE_JAVASE)
252 extern classinfo *class_java_lang_StackTraceElement;
253 extern classinfo *class_java_lang_reflect_Constructor;
254 extern classinfo *class_java_lang_reflect_Field;
255 extern classinfo *class_java_lang_reflect_Method;
256 extern classinfo *class_java_security_PrivilegedAction;
257 extern classinfo *class_java_util_Vector;
258 extern classinfo *class_java_util_HashMap;
259
260 extern classinfo *arrayclass_java_lang_Object;
261
262 # if defined(ENABLE_ANNOTATIONS)
263 extern classinfo *class_sun_reflect_ConstantPool;
264 #  if defined(WITH_CLASSPATH_GNU)
265 extern classinfo *class_sun_reflect_annotation_AnnotationParser;
266 #  endif
267 # endif
268 #endif
269
270
271 /* pseudo classes for the type checker ****************************************/
272
273 /*
274  * pseudo_class_Arraystub
275  *     (extends Object implements Cloneable, java.io.Serializable)
276  *
277  *     If two arrays of incompatible component types are merged,
278  *     the resulting reference has no accessible components.
279  *     The result does, however, implement the interfaces Cloneable
280  *     and java.io.Serializable. This pseudo class is used internally
281  *     to represent such results. (They are *not* considered arrays!)
282  *
283  * pseudo_class_Null
284  *
285  *     This pseudo class is used internally to represent the
286  *     null type.
287  *
288  * pseudo_class_New
289  *
290  *     This pseudo class is used internally to represent the
291  *     the 'uninitialized object' type.
292  */
293
294 extern classinfo *pseudo_class_Arraystub;
295 extern classinfo *pseudo_class_Null;
296 extern classinfo *pseudo_class_New;
297
298
299 /* inline functions ***********************************************************/
300
301 /* class_is_primitive **********************************************************
302
303    Checks if the given class is a primitive class.
304
305 *******************************************************************************/
306
307 static inline bool class_is_primitive(classinfo *c)
308 {
309         if (c->flags & ACC_CLASS_PRIMITIVE)
310                 return true;
311
312         return false;
313 }
314
315
316 /* class_is_anonymousclass *****************************************************
317
318    Checks if the given class is an anonymous class.
319
320 *******************************************************************************/
321
322 static inline bool class_is_anonymousclass(classinfo *c)
323 {
324         if (c->flags & ACC_CLASS_ANONYMOUS)
325                 return true;
326
327         return false;
328 }
329
330
331 /* class_is_array **************************************************************
332
333    Checks if the given class is an array class.
334
335 *******************************************************************************/
336
337 static inline bool class_is_array(classinfo *c)
338 {
339         if (!(c->state & CLASS_LINKED))
340                 if (!link_class(c))
341                         return false;
342
343         return (c->vftbl->arraydesc != NULL);
344 }
345
346
347 /* class_is_interface **********************************************************
348
349    Checks if the given class is an interface.
350
351 *******************************************************************************/
352
353 static inline bool class_is_interface(classinfo *c)
354 {
355         if (c->flags & ACC_INTERFACE)
356                 return true;
357
358         return false;
359 }
360
361
362 /* class_is_localclass *********************************************************
363
364    Checks if the given class is a local class.
365
366 *******************************************************************************/
367
368 static inline bool class_is_localclass(classinfo *c)
369 {
370         if ((c->enclosingmethod != NULL) && !class_is_anonymousclass(c))
371                 return true;
372
373         return false;
374 }
375
376
377 /* class_is_memberclass ********************************************************
378
379    Checks if the given class is a member class.
380
381 *******************************************************************************/
382
383 static inline bool class_is_memberclass(classinfo *c)
384 {
385         if (c->flags & ACC_CLASS_MEMBER)
386                 return true;
387
388         return false;
389 }
390
391
392 /* class_get_classloader *******************************************************
393
394    Return the classloader of the given class.
395
396 *******************************************************************************/
397
398 static inline classloader *class_get_classloader(classinfo *c)
399 {
400         classloader *cl;
401
402         cl = c->classloader;
403
404         /* The classloader may be NULL. */
405
406         return cl;
407 }
408
409
410 /* class_get_superclass ********************************************************
411
412    Return the super class of the given class.
413
414 *******************************************************************************/
415
416 static inline classinfo *class_get_superclass(classinfo *c)
417 {
418         /* For interfaces we return NULL. */
419
420         if (c->flags & ACC_INTERFACE)
421                 return NULL;
422
423         /* For java/lang/Object, primitive-type and Void classes c->super
424            is NULL and we return NULL. */
425
426         return c->super;
427 }
428
429
430 /* function prototypes ********************************************************/
431
432 classinfo *class_create_classinfo(utf *u);
433 void       class_postset_header_vftbl(void);
434 classinfo *class_define(utf *name, classloader *cl, int32_t length, uint8_t *data, java_handle_t *pd);
435 void       class_set_packagename(classinfo *c);
436
437 bool       class_load_attributes(classbuffer *cb);
438
439 /* retrieve constantpool element */
440 voidptr class_getconstant(classinfo *class, u4 pos, u4 ctype);
441 voidptr innerclass_getconstant(classinfo *c, u4 pos, u4 ctype);
442
443 /* frees all resources used by the class */
444 void class_free(classinfo *);
445
446 /* return an array class with the given component class */
447 classinfo *class_array_of(classinfo *component,bool link);
448
449 /* return an array class with the given dimension and element class */
450 classinfo *class_multiarray_of(s4 dim, classinfo *element,bool link);
451
452 /* return a classref for the given class name */
453 /* (does a linear search!)                    */
454 constant_classref *class_lookup_classref(classinfo *cls,utf *name);
455
456 /* return a classref for the given class name */
457 /* (does a linear search!)                    */
458 constant_classref *class_get_classref(classinfo *cls,utf *name);
459
460 /* return a classref to the class itself */
461 /* (does a linear search!)                    */
462 constant_classref *class_get_self_classref(classinfo *cls);
463
464 /* return a classref for an array with the given dimension of with the */
465 /* given component type */
466 constant_classref *class_get_classref_multiarray_of(s4 dim,constant_classref *ref);
467
468 /* return a classref for the component type of the given array type */
469 constant_classref *class_get_classref_component_of(constant_classref *ref);
470
471 /* get a class' field by name and descriptor */
472 fieldinfo *class_findfield(classinfo *c, utf *name, utf *desc);
473
474 /* search 'classinfo'-structure for a field with the specified name */
475 fieldinfo *class_findfield_by_name(classinfo *c, utf *name);
476 s4 class_findfield_index_by_name(classinfo *c, utf *name);
477
478 /* search class for a field */
479 fieldinfo *class_resolvefield(classinfo *c, utf *name, utf *desc, classinfo *referer, bool throwexception);
480
481 /* search for a method with a specified name and descriptor */
482 methodinfo *class_findmethod(classinfo *c, utf *name, utf *desc);
483 methodinfo *class_resolvemethod(classinfo *c, utf *name, utf *dest);
484 methodinfo *class_resolveclassmethod(classinfo *c, utf *name, utf *dest, classinfo *referer, bool throwexception);
485 methodinfo *class_resolveinterfacemethod(classinfo *c, utf *name, utf *dest, classinfo *referer, bool throwexception);
486
487 bool class_issubclass(classinfo *sub, classinfo *super);
488 bool class_isanysubclass(classinfo *sub, classinfo *super);
489
490 bool                       class_is_primitive(classinfo *c);
491 bool                       class_is_anonymousclass(classinfo *c);
492 bool                       class_is_array(classinfo *c);
493 bool                       class_is_interface(classinfo *c);
494 bool                       class_is_localclass(classinfo *c);
495 bool                       class_is_memberclass(classinfo *c);
496
497 classloader               *class_get_classloader(classinfo *c);
498 classinfo                 *class_get_superclass(classinfo *c);
499 classinfo                 *class_get_componenttype(classinfo *c);
500 java_handle_objectarray_t *class_get_declaredclasses(classinfo *c, bool publicOnly);
501 classinfo                 *class_get_declaringclass(classinfo *c);
502 classinfo                 *class_get_enclosingclass(classinfo *c);
503 methodinfo                *class_get_enclosingmethod(classinfo *c);
504 java_handle_objectarray_t *class_get_interfaces(classinfo *c);
505 java_handle_bytearray_t   *class_get_annotations(classinfo *c);
506 int32_t                    class_get_modifiers(classinfo *c, bool ignoreInnerClassesAttrib);
507
508 #if defined(ENABLE_JAVASE)
509 utf                       *class_get_signature(classinfo *c);
510 #endif
511
512 /* some debugging functions */
513
514 #if !defined(NDEBUG)
515 void class_printflags(classinfo *c);
516 void class_print(classinfo *c);
517 void class_println(classinfo *c);
518 void class_classref_print(constant_classref *cr);
519 void class_classref_println(constant_classref *cr);
520 void class_classref_or_classinfo_print(classref_or_classinfo c);
521 void class_classref_or_classinfo_println(classref_or_classinfo c);
522 #endif
523
524 /* debug purposes */
525 void class_showmethods(classinfo *c);
526 void class_showconstantpool(classinfo *c);
527
528 #endif /* _CLASS_H */
529
530
531 /*
532  * These are local overrides for various environment variables in Emacs.
533  * Please do not remove this and leave it at the end of the file, where
534  * Emacs will automagically detect them.
535  * ---------------------------------------------------------------------
536  * Local variables:
537  * mode: c
538  * indent-tabs-mode: t
539  * c-basic-offset: 4
540  * tab-width: 4
541  * End:
542  */