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