* src/vm/jit/i386/darwin/md-asm.h: Repaired --enable-cycles-stats.
[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         classinfo  *super;            /* super class                              */
115         classinfo  *sub;              /* sub class pointer                        */
116         classinfo  *nextsub;          /* pointer to next class in sub class list  */
117
118         int32_t     interfacescount;  /* number of interfaces                     */
119         classinfo **interfaces;       /* super interfaces                         */
120
121         int32_t     fieldscount;      /* number of fields                         */
122         fieldinfo  *fields;           /* field table                              */
123
124         int32_t     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 annotation attributes are NULL (and not a zero length array)   */
149         /* if there is nothing.                                                   */
150         java_object_t *annotations;   /* annotations of this class                */
151         
152         java_object_t *method_annotations; /* array of annotations of the methods */
153         java_object_t *method_parameterannotations; /* array of parameter         */
154                                       /* annotations of the methods               */
155         java_object_t *method_annotationdefaults; /* array of annotation default  */
156                                       /* values of the methods                    */
157
158         java_object_t *field_annotations; /* array of annotations of the fields   */
159
160 #endif
161 #endif
162         classloader *classloader;       /* NULL for bootstrap classloader         */
163
164 #if defined(ENABLE_JAVASE)
165 # if defined(WITH_CLASSPATH_SUN)
166         java_object_t      *protectiondomain;
167         java_objectarray_t *signers;
168 # endif
169 #endif
170 };
171
172
173 /* innerclassinfo *************************************************************/
174
175 struct innerclassinfo {
176         classref_or_classinfo inner_class; /* inner class pointer                 */
177         classref_or_classinfo outer_class; /* outer class pointer                 */
178         utf                  *name;        /* innerclass name                     */
179         s4                    flags;       /* ACC flags                           */
180 };
181
182
183 /* extra_classref **************************************************************
184
185    for classrefs not occurring within descriptors
186
187 *******************************************************************************/
188
189 struct extra_classref {
190         extra_classref    *next;
191         constant_classref  classref;
192 };
193
194
195 /* castinfo *******************************************************************/
196
197 struct castinfo {
198         s4 super_baseval;
199         s4 super_diffval;
200         s4 sub_baseval;
201 };
202
203
204 /* global variables ***********************************************************/
205
206 /* frequently used classes ****************************************************/
207
208 /* important system classes */
209
210 extern classinfo *class_java_lang_Object;
211 extern classinfo *class_java_lang_Class;
212 extern classinfo *class_java_lang_ClassLoader;
213 extern classinfo *class_java_lang_Cloneable;
214 extern classinfo *class_java_lang_SecurityManager;
215 extern classinfo *class_java_lang_String;
216 extern classinfo *class_java_lang_System;
217 extern classinfo *class_java_lang_Thread;
218 extern classinfo *class_java_lang_ThreadGroup;
219 extern classinfo *class_java_lang_Throwable;
220 extern classinfo *class_java_io_Serializable;
221
222 #if defined(WITH_CLASSPATH_GNU)
223 extern classinfo *class_java_lang_VMSystem;
224 extern classinfo *class_java_lang_VMThread;
225 extern classinfo *class_java_lang_VMThrowable;
226 #endif
227
228 #if defined(WITH_CLASSPATH_SUN)
229 extern classinfo *class_sun_reflect_MagicAccessorImpl;
230 #endif
231
232 #if defined(ENABLE_JAVASE)
233 extern classinfo *class_java_lang_Void;
234 #endif
235
236 extern classinfo *class_java_lang_Boolean;
237 extern classinfo *class_java_lang_Byte;
238 extern classinfo *class_java_lang_Character;
239 extern classinfo *class_java_lang_Short;
240 extern classinfo *class_java_lang_Integer;
241 extern classinfo *class_java_lang_Long;
242 extern classinfo *class_java_lang_Float;
243 extern classinfo *class_java_lang_Double;
244
245 /* some classes which may be used more often */
246
247 #if defined(ENABLE_JAVASE)
248 extern classinfo *class_java_lang_StackTraceElement;
249 extern classinfo *class_java_lang_reflect_Constructor;
250 extern classinfo *class_java_lang_reflect_Field;
251 extern classinfo *class_java_lang_reflect_Method;
252 extern classinfo *class_java_security_PrivilegedAction;
253 extern classinfo *class_java_util_Vector;
254 extern classinfo *class_java_util_HashMap;
255
256 extern classinfo *arrayclass_java_lang_Object;
257
258 # if defined(ENABLE_ANNOTATIONS)
259 extern classinfo *class_sun_reflect_ConstantPool;
260 #  if defined(WITH_CLASSPATH_GNU)
261 extern classinfo *class_sun_reflect_annotation_AnnotationParser;
262 #  endif
263 # endif
264 #endif
265
266
267 /* pseudo classes for the type checker ****************************************/
268
269 /*
270  * pseudo_class_Arraystub
271  *     (extends Object implements Cloneable, java.io.Serializable)
272  *
273  *     If two arrays of incompatible component types are merged,
274  *     the resulting reference has no accessible components.
275  *     The result does, however, implement the interfaces Cloneable
276  *     and java.io.Serializable. This pseudo class is used internally
277  *     to represent such results. (They are *not* considered arrays!)
278  *
279  * pseudo_class_Null
280  *
281  *     This pseudo class is used internally to represent the
282  *     null type.
283  *
284  * pseudo_class_New
285  *
286  *     This pseudo class is used internally to represent the
287  *     the 'uninitialized object' type.
288  */
289
290 extern classinfo *pseudo_class_Arraystub;
291 extern classinfo *pseudo_class_Null;
292 extern classinfo *pseudo_class_New;
293
294
295 /* inline functions ***********************************************************/
296
297 /* class_is_primitive **********************************************************
298
299    Checks if the given class is a primitive class.
300
301 *******************************************************************************/
302
303 static inline bool class_is_primitive(classinfo *c)
304 {
305         if (c->flags & ACC_CLASS_PRIMITIVE)
306                 return true;
307
308         return false;
309 }
310
311
312 /* class_is_anonymousclass *****************************************************
313
314    Checks if the given class is an anonymous class.
315
316 *******************************************************************************/
317
318 static inline bool class_is_anonymousclass(classinfo *c)
319 {
320         if (c->flags & ACC_CLASS_ANONYMOUS)
321                 return true;
322
323         return false;
324 }
325
326
327 /* class_is_array **************************************************************
328
329    Checks if the given class is an array class.
330
331 *******************************************************************************/
332
333 static inline bool class_is_array(classinfo *c)
334 {
335         if (!(c->state & CLASS_LINKED))
336                 if (!link_class(c))
337                         return false;
338
339         return (c->vftbl->arraydesc != NULL);
340 }
341
342
343 /* class_is_interface **********************************************************
344
345    Checks if the given class is an interface.
346
347 *******************************************************************************/
348
349 static inline bool class_is_interface(classinfo *c)
350 {
351         if (c->flags & ACC_INTERFACE)
352                 return true;
353
354         return false;
355 }
356
357
358 /* class_is_localclass *********************************************************
359
360    Checks if the given class is a local class.
361
362 *******************************************************************************/
363
364 static inline bool class_is_localclass(classinfo *c)
365 {
366         if ((c->enclosingmethod != NULL) && !class_is_anonymousclass(c))
367                 return true;
368
369         return false;
370 }
371
372
373 /* class_is_memberclass ********************************************************
374
375    Checks if the given class is a member class.
376
377 *******************************************************************************/
378
379 static inline bool class_is_memberclass(classinfo *c)
380 {
381         if (c->flags & ACC_CLASS_MEMBER)
382                 return true;
383
384         return false;
385 }
386
387
388 /* class_get_classloader *******************************************************
389
390    Return the classloader of the given class.
391
392 *******************************************************************************/
393
394 static inline classloader *class_get_classloader(classinfo *c)
395 {
396         classloader *cl;
397
398         cl = c->classloader;
399
400         /* The classloader may be NULL. */
401
402         return cl;
403 }
404
405
406 /* class_get_superclass ********************************************************
407
408    Return the super class of the given class.
409
410 *******************************************************************************/
411
412 static inline classinfo *class_get_superclass(classinfo *c)
413 {
414         /* For interfaces we return NULL. */
415
416         if (c->flags & ACC_INTERFACE)
417                 return NULL;
418
419         /* For java/lang/Object, primitive-type and Void classes c->super
420            is NULL and we return NULL. */
421
422         return c->super;
423 }
424
425
426 /* function prototypes ********************************************************/
427
428 classinfo *class_create_classinfo(utf *u);
429 void       class_postset_header_vftbl(void);
430 classinfo *class_define(utf *name, classloader *cl, int32_t length, const uint8_t *data, java_handle_t *pd);
431 void       class_set_packagename(classinfo *c);
432
433 bool       class_load_attributes(classbuffer *cb);
434
435 /* retrieve constantpool element */
436 voidptr class_getconstant(classinfo *class, u4 pos, u4 ctype);
437 voidptr innerclass_getconstant(classinfo *c, u4 pos, u4 ctype);
438
439 /* frees all resources used by the class */
440 void class_free(classinfo *);
441
442 /* return an array class with the given component class */
443 classinfo *class_array_of(classinfo *component,bool link);
444
445 /* return an array class with the given dimension and element class */
446 classinfo *class_multiarray_of(s4 dim, classinfo *element,bool link);
447
448 /* return a classref for the given class name */
449 /* (does a linear search!)                    */
450 constant_classref *class_lookup_classref(classinfo *cls,utf *name);
451
452 /* return a classref for the given class name */
453 /* (does a linear search!)                    */
454 constant_classref *class_get_classref(classinfo *cls,utf *name);
455
456 /* return a classref to the class itself */
457 /* (does a linear search!)                    */
458 constant_classref *class_get_self_classref(classinfo *cls);
459
460 /* return a classref for an array with the given dimension of with the */
461 /* given component type */
462 constant_classref *class_get_classref_multiarray_of(s4 dim,constant_classref *ref);
463
464 /* return a classref for the component type of the given array type */
465 constant_classref *class_get_classref_component_of(constant_classref *ref);
466
467 /* get a class' field by name and descriptor */
468 fieldinfo *class_findfield(classinfo *c, utf *name, utf *desc);
469
470 /* search 'classinfo'-structure for a field with the specified name */
471 fieldinfo *class_findfield_by_name(classinfo *c, utf *name);
472 s4 class_findfield_index_by_name(classinfo *c, utf *name);
473
474 /* search class for a field */
475 fieldinfo *class_resolvefield(classinfo *c, utf *name, utf *desc, classinfo *referer, bool throwexception);
476
477 /* search for a method with a specified name and descriptor */
478 methodinfo *class_findmethod(classinfo *c, utf *name, utf *desc);
479 methodinfo *class_resolvemethod(classinfo *c, utf *name, utf *dest);
480 methodinfo *class_resolveclassmethod(classinfo *c, utf *name, utf *dest, classinfo *referer, bool throwexception);
481 methodinfo *class_resolveinterfacemethod(classinfo *c, utf *name, utf *dest, classinfo *referer, bool throwexception);
482
483 bool class_issubclass(classinfo *sub, classinfo *super);
484 bool class_isanysubclass(classinfo *sub, classinfo *super);
485
486 bool                       class_is_primitive(classinfo *c);
487 bool                       class_is_anonymousclass(classinfo *c);
488 bool                       class_is_array(classinfo *c);
489 bool                       class_is_interface(classinfo *c);
490 bool                       class_is_localclass(classinfo *c);
491 bool                       class_is_memberclass(classinfo *c);
492
493 classloader               *class_get_classloader(classinfo *c);
494 classinfo                 *class_get_superclass(classinfo *c);
495 classinfo                 *class_get_componenttype(classinfo *c);
496 java_handle_objectarray_t *class_get_declaredclasses(classinfo *c, bool publicOnly);
497 classinfo                 *class_get_declaringclass(classinfo *c);
498 classinfo                 *class_get_enclosingclass(classinfo *c);
499 java_handle_objectarray_t *class_get_interfaces(classinfo *c);
500 java_handle_bytearray_t   *class_get_annotations(classinfo *c);
501 int32_t                    class_get_modifiers(classinfo *c, bool ignoreInnerClassesAttrib);
502
503 #if defined(ENABLE_JAVASE)
504 utf                       *class_get_signature(classinfo *c);
505 #endif
506
507 /* some debugging functions */
508
509 #if !defined(NDEBUG)
510 void class_printflags(classinfo *c);
511 void class_print(classinfo *c);
512 void class_println(classinfo *c);
513 void class_classref_print(constant_classref *cr);
514 void class_classref_println(constant_classref *cr);
515 void class_classref_or_classinfo_print(classref_or_classinfo c);
516 void class_classref_or_classinfo_println(classref_or_classinfo c);
517 #endif
518
519 /* debug purposes */
520 void class_showmethods(classinfo *c);
521 void class_showconstantpool(classinfo *c);
522
523 #endif /* _CLASS_H */
524
525
526 /*
527  * These are local overrides for various environment variables in Emacs.
528  * Please do not remove this and leave it at the end of the file, where
529  * Emacs will automagically detect them.
530  * ---------------------------------------------------------------------
531  * Local variables:
532  * mode: c
533  * indent-tabs-mode: t
534  * c-basic-offset: 4
535  * tab-width: 4
536  * End:
537  */