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