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