* merged default branch into jitcache-arm-x86 branch
[cacao.git] / src / vm / class.hpp
1 /* src/vm/class.hpp - 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_HPP
27 #define _CLASS_HPP
28
29 /* forward typedefs ***********************************************************/
30
31 typedef struct classinfo      classinfo; 
32 typedef struct innerclassinfo innerclassinfo;
33 typedef struct extra_classref extra_classref;
34
35
36 #include "config.h"
37
38 #include <stdint.h>
39
40 #include "vm/types.h"
41
42 #if defined(ENABLE_JAVASE)
43 # include "vm/annotation.h"
44 #endif
45
46 #include "vm/field.hpp"
47 #include "vm/global.h"
48 #include "vm/linker.h"
49 #include "vm/loader.hpp"
50 #include "vm/method.h"
51 #include "vm/references.h"
52 #include "vm/string.hpp"
53 #include "vm/utf8.h"
54
55 /* class state defines ********************************************************/
56
57 #define CLASS_LOADING         0x0001
58 #define CLASS_LOADED          0x0002
59 #define CLASS_LINKING         0x0004
60 #define CLASS_LINKED          0x0008
61 #define CLASS_INITIALIZING    0x0010
62 #define CLASS_INITIALIZED     0x0020
63 #define CLASS_ERROR           0x0040
64
65
66 /* some macros ****************************************************************/
67
68 #define CLASS_IS_OR_ALMOST_INITIALIZED(c) \
69     (((c)->state & CLASS_INITIALIZING) || ((c)->state & CLASS_INITIALIZED))
70
71
72 /* classinfo ******************************************************************/
73
74 /* We define this dummy structure of java_lang_Class so we can
75    bootstrap cacaoh without needing a java_lang_Class.h file.  Whether
76    the size of the dummy structure is big enough is checked during
77    runtime in vm_create. */
78
79 typedef struct {
80         java_object_t      header;
81 #if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
82         intptr_t           padding[4];
83 #elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
84         intptr_t           padding[19];
85 #elif defined(WITH_JAVA_RUNTIME_LIBRARY_CLDC1_1)
86         intptr_t           padding[3];
87 #else
88 # error unknown classpath configuration
89 #endif
90 } dummy_java_lang_Class;
91
92 struct classinfo {                /* class structure                          */
93         dummy_java_lang_Class object;
94
95         s4          flags;            /* ACC flags                                */
96         utf        *name;             /* class name                               */
97
98         s4          cpcount;          /* number of entries in constant pool       */
99         u1         *cptags;           /* constant pool tags                       */
100         void*      *cpinfos;          /* pointer to constant pool info structures */
101
102         s4          classrefcount;    /* number of symbolic class references      */
103         constant_classref *classrefs; /* table of symbolic class references       */
104         extra_classref *extclassrefs; /* additional classrefs                     */
105         s4          parseddescsize;   /* size of the parsed descriptors block     */
106         u1         *parseddescs;      /* parsed descriptors                       */
107
108         classinfo  *super;            /* super class                              */
109         classinfo  *sub;              /* sub class pointer                        */
110         classinfo  *nextsub;          /* pointer to next class in sub class list  */
111
112         int32_t     interfacescount;  /* number of interfaces                     */
113         classinfo **interfaces;       /* super interfaces                         */
114
115         int32_t     fieldscount;      /* number of fields                         */
116         fieldinfo  *fields;           /* field table                              */
117
118         int32_t     methodscount;     /* number of methods                        */
119         methodinfo *methods;          /* method table                             */
120
121         s4          state;            /* current class state                      */
122         s4          index;            /* hierarchy depth (classes) or index       */
123                                       /* (interfaces)                             */
124         s4          instancesize;     /* size of an instance of this class        */
125
126         vftbl_t    *vftbl;            /* pointer to virtual function table        */
127
128         methodinfo *finalizer;        /* finalizer method                         */
129
130         u2          innerclasscount;  /* number of inner classes                  */
131         innerclassinfo *innerclass;
132
133         classref_or_classinfo  declaringclass;
134         classref_or_classinfo  enclosingclass;  /* enclosing class                */
135         constant_nameandtype  *enclosingmethod; /* enclosing method               */
136
137         utf        *packagename;      /* full name of the package                 */
138         utf        *sourcefile;       /* SourceFile attribute                     */
139 #if defined(ENABLE_JAVASE)
140         utf        *signature;        /* Signature attribute                      */
141 #if defined(ENABLE_ANNOTATIONS)
142         /* All the annotation attributes are NULL (and not a zero length array)   */
143         /* if there is nothing.                                                   */
144         java_object_t *annotations;   /* annotations of this class                */
145         
146         java_object_t *method_annotations; /* array of annotations of the methods */
147         java_object_t *method_parameterannotations; /* array of parameter         */
148                                       /* annotations of the methods               */
149         java_object_t *method_annotationdefaults; /* array of annotation default  */
150                                       /* values of the methods                    */
151
152         java_object_t *field_annotations; /* array of annotations of the fields   */
153
154 #endif
155 #endif
156         classloader_t *classloader;       /* NULL for bootstrap classloader         */
157
158 #if defined(ENABLE_JAVASE)
159 # if defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
160         java_object_t      *protectiondomain;
161         java_objectarray_t *signers;
162 # endif
163 #endif
164 #if defined(ENABLE_JITCACHE)
165         int         cache_file_fd;
166 #endif
167 };
168
169
170 /* innerclassinfo *************************************************************/
171
172 struct innerclassinfo {
173         classref_or_classinfo inner_class; /* inner class pointer                 */
174         classref_or_classinfo outer_class; /* outer class pointer                 */
175         utf                  *name;        /* innerclass name                     */
176         s4                    flags;       /* ACC flags                           */
177 };
178
179
180 /* extra_classref **************************************************************
181
182    for classrefs not occurring within descriptors
183
184 *******************************************************************************/
185
186 struct extra_classref {
187         extra_classref    *next;
188         constant_classref  classref;
189 };
190
191
192 /* inline functions ***********************************************************/
193
194 #ifdef __cplusplus
195 extern "C" {
196 #endif
197
198 /**
199  * Returns the classname of the class, where slashes ('/') are
200  * replaced by dots ('.').
201  *
202  * @param c class to get name of
203  * @return classname
204  */
205 inline static java_handle_t* class_get_classname(classinfo* c)
206 {
207         java_handle_t *s;
208
209         /* Create a java string. */
210
211         s = javastring_new_slash_to_dot(c->name);
212
213         return s;
214 }
215
216
217 /* class_is_primitive **********************************************************
218
219    Checks if the given class is a primitive class.
220
221 *******************************************************************************/
222
223 static inline bool class_is_primitive(classinfo *c)
224 {
225         if (c->flags & ACC_CLASS_PRIMITIVE)
226                 return true;
227
228         return false;
229 }
230
231
232 /* class_is_anonymousclass *****************************************************
233
234    Checks if the given class is an anonymous class.
235
236 *******************************************************************************/
237
238 static inline bool class_is_anonymousclass(classinfo *c)
239 {
240         if (c->flags & ACC_CLASS_ANONYMOUS)
241                 return true;
242
243         return false;
244 }
245
246
247 /* class_is_array **************************************************************
248
249    Checks if the given class is an array class.
250
251 *******************************************************************************/
252
253 static inline bool class_is_array(classinfo *c)
254 {
255         if (!(c->state & CLASS_LINKED))
256                 if (!link_class(c))
257                         return false;
258
259         return (c->vftbl->arraydesc != NULL);
260 }
261
262
263 /* class_is_interface **********************************************************
264
265    Checks if the given class is an interface.
266
267 *******************************************************************************/
268
269 static inline bool class_is_interface(classinfo *c)
270 {
271         if (c->flags & ACC_INTERFACE)
272                 return true;
273
274         return false;
275 }
276
277
278 /* class_is_localclass *********************************************************
279
280    Checks if the given class is a local class.
281
282 *******************************************************************************/
283
284 static inline bool class_is_localclass(classinfo *c)
285 {
286         if ((c->enclosingmethod != NULL) && !class_is_anonymousclass(c))
287                 return true;
288
289         return false;
290 }
291
292
293 /* class_is_memberclass ********************************************************
294
295    Checks if the given class is a member class.
296
297 *******************************************************************************/
298
299 static inline bool class_is_memberclass(classinfo *c)
300 {
301         if (c->flags & ACC_CLASS_MEMBER)
302                 return true;
303
304         return false;
305 }
306
307
308 /* class_get_classloader *******************************************************
309
310    Return the classloader of the given class.
311
312 *******************************************************************************/
313
314 static inline classloader_t *class_get_classloader(classinfo *c)
315 {
316         classloader_t *cl;
317
318         cl = c->classloader;
319
320         /* The classloader may be NULL. */
321
322         return cl;
323 }
324
325
326 /* class_get_superclass ********************************************************
327
328    Return the super class of the given class.
329
330 *******************************************************************************/
331
332 static inline classinfo *class_get_superclass(classinfo *c)
333 {
334         /* For interfaces we return NULL. */
335
336         if (c->flags & ACC_INTERFACE)
337                 return NULL;
338
339         /* For java/lang/Object, primitive-type and Void classes c->super
340            is NULL and we return NULL. */
341
342         return c->super;
343 }
344
345
346 /* function prototypes ********************************************************/
347
348 classinfo *class_create_classinfo(utf *u);
349 void       class_postset_header_vftbl(void);
350 classinfo *class_define(utf *name, classloader_t *cl, int32_t length, uint8_t *data, java_handle_t *pd);
351 void       class_set_packagename(classinfo *c);
352
353 bool       class_load_attributes(classbuffer *cb);
354
355 /* retrieve constantpool element */
356 void* class_getconstant(classinfo *c, u4 pos, u4 ctype);
357 void* innerclass_getconstant(classinfo *c, u4 pos, u4 ctype);
358
359 /* frees all resources used by the class */
360 void class_free(classinfo *);
361
362 /* return an array class with the given component class */
363 classinfo *class_array_of(classinfo *component,bool link);
364
365 /* return an array class with the given dimension and element class */
366 classinfo *class_multiarray_of(s4 dim, classinfo *element,bool link);
367
368 /* return a classref for the given class name */
369 /* (does a linear search!)                    */
370 constant_classref *class_lookup_classref(classinfo *cls,utf *name);
371
372 /* return a classref for the given class name */
373 /* (does a linear search!)                    */
374 constant_classref *class_get_classref(classinfo *cls,utf *name);
375
376 /* return a classref to the class itself */
377 /* (does a linear search!)                    */
378 constant_classref *class_get_self_classref(classinfo *cls);
379
380 /* return a classref for an array with the given dimension of with the */
381 /* given component type */
382 constant_classref *class_get_classref_multiarray_of(s4 dim,constant_classref *ref);
383
384 /* return a classref for the component type of the given array type */
385 constant_classref *class_get_classref_component_of(constant_classref *ref);
386
387 /* get a class' field by name and descriptor */
388 fieldinfo *class_findfield(classinfo *c, utf *name, utf *desc);
389
390 /* search 'classinfo'-structure for a field with the specified name */
391 fieldinfo *class_findfield_by_name(classinfo *c, utf *name);
392
393 /* search class for a field */
394 fieldinfo *class_resolvefield(classinfo *c, utf *name, utf *desc, classinfo *referer);
395
396 /* search for a method with a specified name and descriptor */
397 methodinfo *class_findmethod(classinfo *c, utf *name, utf *desc);
398 methodinfo *class_resolvemethod(classinfo *c, utf *name, utf *dest);
399 methodinfo *class_resolveclassmethod(classinfo *c, utf *name, utf *dest, classinfo *referer, bool throwexception);
400 methodinfo *class_resolveinterfacemethod(classinfo *c, utf *name, utf *dest, classinfo *referer, bool throwexception);
401
402 bool                       class_issubclass(classinfo *sub, classinfo *super);
403 bool                       class_isanysubclass(classinfo *sub, classinfo *super);
404 bool                       class_is_assignable_from(classinfo *to, classinfo *from);
405 bool                       class_is_instance(classinfo *c, java_handle_t *h);
406
407 classloader_t             *class_get_classloader(classinfo *c);
408 classinfo                 *class_get_superclass(classinfo *c);
409 classinfo                 *class_get_componenttype(classinfo *c);
410 java_handle_objectarray_t *class_get_declaredclasses(classinfo *c, bool publicOnly);
411 java_handle_objectarray_t *class_get_declaredconstructors(classinfo *c, bool publicOnly);
412 java_handle_objectarray_t *class_get_declaredfields(classinfo *c, bool publicOnly);
413 java_handle_objectarray_t *class_get_declaredmethods(classinfo *c, bool publicOnly);
414 classinfo                 *class_get_declaringclass(classinfo *c);
415 classinfo                 *class_get_enclosingclass(classinfo *c);
416 java_handle_t*             class_get_enclosingconstructor(classinfo *c);
417 methodinfo*                class_get_enclosingmethod_raw(classinfo *c);
418 java_handle_t*             class_get_enclosingmethod(classinfo *c);
419 java_handle_objectarray_t *class_get_interfaces(classinfo *c);
420 java_handle_bytearray_t   *class_get_annotations(classinfo *c);
421 int32_t                    class_get_modifiers(classinfo *c, bool ignoreInnerClassesAttrib);
422 java_handle_t             *class_get_name(classinfo *c);
423
424 #if defined(ENABLE_JAVASE)
425 utf                       *class_get_signature(classinfo *c);
426 #endif
427
428 /* some debugging functions */
429
430 #if !defined(NDEBUG)
431 void class_printflags(classinfo *c);
432 void class_print(classinfo *c);
433 void class_println(classinfo *c);
434 void class_classref_print(constant_classref *cr);
435 void class_classref_println(constant_classref *cr);
436 void class_classref_or_classinfo_print(classref_or_classinfo c);
437 void class_classref_or_classinfo_println(classref_or_classinfo c);
438 #endif
439
440 /* debug purposes */
441 void class_showmethods(classinfo *c);
442 void class_showconstantpool(classinfo *c);
443
444 #ifdef __cplusplus
445 }
446 #endif
447
448 #endif /* _CLASS_HPP */
449
450
451 /*
452  * These are local overrides for various environment variables in Emacs.
453  * Please do not remove this and leave it at the end of the file, where
454  * Emacs will automagically detect them.
455  * ---------------------------------------------------------------------
456  * Local variables:
457  * mode: c
458  * indent-tabs-mode: t
459  * c-basic-offset: 4
460  * tab-width: 4
461  * End:
462  */