Merged revisions 8321-8342 via svnmerge from
[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    $Id: class.h 8343 2007-08-17 21:39:32Z michi $
26
27 */
28
29
30 #ifndef _CLASS_H
31 #define _CLASS_H
32
33 /* forward typedefs ***********************************************************/
34
35 typedef struct classinfo      classinfo; 
36 typedef struct innerclassinfo innerclassinfo;
37 typedef struct extra_classref extra_classref;
38 typedef struct castinfo       castinfo;
39
40
41 #include "config.h"
42
43 #include <stdint.h>
44
45 #include "vm/types.h"
46
47 #include "toolbox/list.h"
48
49 #include "vm/global.h"
50
51 #if defined(ENABLE_JAVASE)
52 # include "vmcore/annotation.h"
53 #endif
54
55 #include "vmcore/field.h"
56 #include "vmcore/linker.h"
57 #include "vmcore/loader.h"
58 #include "vmcore/method.h"
59 #include "vmcore/references.h"
60 #include "vmcore/utf8.h"
61
62
63 /* class state defines ********************************************************/
64
65 #define CLASS_LOADING         0x0001
66 #define CLASS_LOADED          0x0002
67 #define CLASS_LINKING         0x0004
68 #define CLASS_LINKED          0x0008
69 #define CLASS_INITIALIZING    0x0010
70 #define CLASS_INITIALIZED     0x0020
71 #define CLASS_ERROR           0x0040
72
73
74 /* some macros ****************************************************************/
75
76 #define CLASS_IS_OR_ALMOST_INITIALIZED(c) \
77     (((c)->state & CLASS_INITIALIZING) || ((c)->state & CLASS_INITIALIZED))
78
79
80 /* classinfo ******************************************************************/
81
82 /* We define this dummy structure of java_lang_Class so we can
83    bootstrap cacaoh without needing a java_lang_Class.h file.  Whether
84    the size of the dummy structure is big enough is checked during
85    runtime in vm_create. */
86
87 typedef struct {
88         java_object_t      header;
89 #if defined(WITH_CLASSPATH_GNU)
90         intptr_t           padding[4];
91 #elif defined(WITH_CLASSPATH_SUN)
92         intptr_t           padding[19];
93 #elif defined(WITH_CLASSPATH_CLDC1_1)
94         intptr_t           padding[3];
95 #else
96 # error unknown classpath configuration
97 #endif
98 } dummy_java_lang_Class;
99
100 struct classinfo {                /* class structure                          */
101         dummy_java_lang_Class object;
102
103         s4          flags;            /* ACC flags                                */
104         utf        *name;             /* class name                               */
105
106         s4          cpcount;          /* number of entries in constant pool       */
107         u1         *cptags;           /* constant pool tags                       */
108         voidptr    *cpinfos;          /* pointer to constant pool info structures */
109
110         s4          classrefcount;    /* number of symbolic class references      */
111         constant_classref *classrefs; /* table of symbolic class references       */
112         extra_classref *extclassrefs; /* additional classrefs                     */
113         s4          parseddescsize;   /* size of the parsed descriptors block     */
114         u1         *parseddescs;      /* parsed descriptors                       */
115
116         classref_or_classinfo super;  /* super class                              */
117         classinfo  *sub;              /* sub class pointer                        */
118         classinfo  *nextsub;          /* pointer to next class in sub class list  */
119
120         s4          interfacescount;  /* number of interfaces                     */
121         classref_or_classinfo *interfaces; /* superinterfaces                     */
122
123         s4          fieldscount;      /* number of fields                         */
124         fieldinfo  *fields;           /* field table                              */
125
126         s4          methodscount;     /* number of methods                        */
127         methodinfo *methods;          /* method table                             */
128
129         s4          state;            /* current class state                      */
130         s4          index;            /* hierarchy depth (classes) or index       */
131                                       /* (interfaces)                             */
132         s4          instancesize;     /* size of an instance of this class        */
133
134         vftbl_t    *vftbl;            /* pointer to virtual function table        */
135
136         methodinfo *finalizer;        /* finalizer method                         */
137
138         u2          innerclasscount;  /* number of inner classes                  */
139         innerclassinfo *innerclass;
140
141         classref_or_classinfo  declaringclass;
142
143 #if defined(ENABLE_JAVASE)
144         classref_or_classinfo  enclosingclass;  /* enclosing class                */
145         constant_nameandtype  *enclosingmethod; /* enclosing method               */
146 #endif
147
148         utf        *packagename;      /* full name of the package                 */
149         utf        *sourcefile;       /* SourceFile attribute                     */
150 #if defined(ENABLE_JAVASE)
151         utf        *signature;        /* Signature attribute                      */
152 #if defined(ENABLE_ANNOTATIONS)
153         annotation_bytearray_t  *annotations;
154         
155         annotation_bytearrays_t *method_annotations;
156         annotation_bytearrays_t *method_parameterannotations;
157         annotation_bytearrays_t *method_annotationdefaults;
158
159         annotation_bytearrays_t *field_annotations;
160 #endif
161         classloader *classloader;     /* NULL for bootstrap classloader           */
162 #endif
163 };
164
165
166 /* innerclassinfo *************************************************************/
167
168 struct innerclassinfo {
169         classref_or_classinfo inner_class; /* inner class pointer                 */
170         classref_or_classinfo outer_class; /* outer class pointer                 */
171         utf                  *name;        /* innerclass name                     */
172         s4                    flags;       /* ACC flags                           */
173 };
174
175
176 /* extra_classref **************************************************************
177
178    for classrefs not occurring within descriptors
179
180 *******************************************************************************/
181
182 struct extra_classref {
183         extra_classref    *next;
184         constant_classref  classref;
185 };
186
187
188 /* castinfo *******************************************************************/
189
190 struct castinfo {
191         s4 super_baseval;
192         s4 super_diffval;
193         s4 sub_baseval;
194 };
195
196
197 /* global variables ***********************************************************/
198
199 /* frequently used classes ****************************************************/
200
201 /* important system classes */
202
203 extern classinfo *class_java_lang_Object;
204 extern classinfo *class_java_lang_Class;
205 extern classinfo *class_java_lang_ClassLoader;
206 extern classinfo *class_java_lang_Cloneable;
207 extern classinfo *class_java_lang_SecurityManager;
208 extern classinfo *class_java_lang_String;
209 extern classinfo *class_java_lang_System;
210 extern classinfo *class_java_lang_Thread;
211 extern classinfo *class_java_lang_ThreadGroup;
212 extern classinfo *class_java_lang_VMSystem;
213 extern classinfo *class_java_lang_VMThread;
214 extern classinfo *class_java_io_Serializable;
215
216 #if defined(WITH_CLASSPATH_SUN)
217 extern classinfo *class_sun_reflect_MagicAccessorImpl;
218 #endif
219
220 /* system exception classes required in cacao */
221
222 extern classinfo *class_java_lang_Throwable;
223 extern classinfo *class_java_lang_Error;
224 extern classinfo *class_java_lang_LinkageError;
225 extern classinfo *class_java_lang_NoClassDefFoundError;
226 extern classinfo *class_java_lang_OutOfMemoryError;
227 extern classinfo *class_java_lang_VirtualMachineError;
228
229 #if defined(WITH_CLASSPATH_GNU)
230 extern classinfo *class_java_lang_VMThrowable;
231 #endif
232
233 extern classinfo *class_java_lang_Exception;
234 extern classinfo *class_java_lang_ClassCastException;
235 extern classinfo *class_java_lang_ClassNotFoundException;
236
237 #if defined(ENABLE_JAVASE)
238 extern classinfo *class_java_lang_Void;
239
240 #if defined(ENABLE_ANNOTATIONS)
241 extern classinfo *class_sun_reflect_ConstantPool;
242 #if defined(WITH_CLASSPATH_GNU)
243 extern classinfo *class_sun_reflect_annotation_AnnotationParser;
244 #endif
245 #endif
246 #endif
247
248 extern classinfo *class_java_lang_Boolean;
249 extern classinfo *class_java_lang_Byte;
250 extern classinfo *class_java_lang_Character;
251 extern classinfo *class_java_lang_Short;
252 extern classinfo *class_java_lang_Integer;
253 extern classinfo *class_java_lang_Long;
254 extern classinfo *class_java_lang_Float;
255 extern classinfo *class_java_lang_Double;
256
257
258 /* some runtime exception */
259
260 extern classinfo *class_java_lang_NullPointerException;
261
262
263 /* some classes which may be used more often */
264
265 #if defined(ENABLE_JAVASE)
266 extern classinfo *class_java_lang_StackTraceElement;
267 extern classinfo *class_java_lang_reflect_Constructor;
268 extern classinfo *class_java_lang_reflect_Field;
269 extern classinfo *class_java_lang_reflect_Method;
270 extern classinfo *class_java_security_PrivilegedAction;
271 extern classinfo *class_java_util_Vector;
272
273 extern classinfo *arrayclass_java_lang_Object;
274 #endif
275
276
277 /* pseudo classes for the type checker ****************************************/
278
279 /*
280  * pseudo_class_Arraystub
281  *     (extends Object implements Cloneable, java.io.Serializable)
282  *
283  *     If two arrays of incompatible component types are merged,
284  *     the resulting reference has no accessible components.
285  *     The result does, however, implement the interfaces Cloneable
286  *     and java.io.Serializable. This pseudo class is used internally
287  *     to represent such results. (They are *not* considered arrays!)
288  *
289  * pseudo_class_Null
290  *
291  *     This pseudo class is used internally to represent the
292  *     null type.
293  *
294  * pseudo_class_New
295  *
296  *     This pseudo class is used internally to represent the
297  *     the 'uninitialized object' type.
298  */
299
300 extern classinfo *pseudo_class_Arraystub;
301 extern classinfo *pseudo_class_Null;
302 extern classinfo *pseudo_class_New;
303
304
305 /* function prototypes ********************************************************/
306
307 classinfo *class_create_classinfo(utf *u);
308 void       class_postset_header_vftbl(void);
309 classinfo *class_define(utf *name, classloader *cl, int32_t length, const uint8_t *data);
310 void       class_set_packagename(classinfo *c);
311
312 bool       class_load_attributes(classbuffer *cb);
313
314 /* retrieve constantpool element */
315 voidptr class_getconstant(classinfo *class, u4 pos, u4 ctype);
316 voidptr innerclass_getconstant(classinfo *c, u4 pos, u4 ctype);
317
318 /* frees all resources used by the class */
319 void class_free(classinfo *);
320
321 /* return an array class with the given component class */
322 classinfo *class_array_of(classinfo *component,bool link);
323
324 /* return an array class with the given dimension and element class */
325 classinfo *class_multiarray_of(s4 dim, classinfo *element,bool link);
326
327 /* return a classref for the given class name */
328 /* (does a linear search!)                    */
329 constant_classref *class_lookup_classref(classinfo *cls,utf *name);
330
331 /* return a classref for the given class name */
332 /* (does a linear search!)                    */
333 constant_classref *class_get_classref(classinfo *cls,utf *name);
334
335 /* return a classref to the class itself */
336 /* (does a linear search!)                    */
337 constant_classref *class_get_self_classref(classinfo *cls);
338
339 /* return a classref for an array with the given dimension of with the */
340 /* given component type */
341 constant_classref *class_get_classref_multiarray_of(s4 dim,constant_classref *ref);
342
343 /* return a classref for the component type of the given array type */
344 constant_classref *class_get_classref_component_of(constant_classref *ref);
345
346 /* get a class' field by name and descriptor */
347 fieldinfo *class_findfield(classinfo *c, utf *name, utf *desc);
348
349 /* search 'classinfo'-structure for a field with the specified name */
350 fieldinfo *class_findfield_by_name(classinfo *c, utf *name);
351 s4 class_findfield_index_by_name(classinfo *c, utf *name);
352
353 /* search class for a field */
354 fieldinfo *class_resolvefield(classinfo *c, utf *name, utf *desc, classinfo *referer, bool throwexception);
355
356 /* search for a method with a specified name and descriptor */
357 methodinfo *class_findmethod(classinfo *c, utf *name, utf *desc);
358 methodinfo *class_resolvemethod(classinfo *c, utf *name, utf *dest);
359 methodinfo *class_resolveclassmethod(classinfo *c, utf *name, utf *dest, classinfo *referer, bool throwexception);
360 methodinfo *class_resolveinterfacemethod(classinfo *c, utf *name, utf *dest, classinfo *referer, bool throwexception);
361
362 bool class_issubclass(classinfo *sub, classinfo *super);
363 bool class_isanysubclass(classinfo *sub, classinfo *super);
364
365 bool                       class_is_primitive(classinfo *c);
366 bool                       class_is_anonymousclass(classinfo *c);
367 bool                       class_is_array(classinfo *c);
368 bool                       class_is_interface(classinfo *c);
369 bool                       class_is_localclass(classinfo *c);
370 bool                       class_is_memberclass(classinfo *c);
371
372 classinfo                 *class_get_superclass(classinfo *c);
373 classinfo                 *class_get_componenttype(classinfo *c);
374 java_handle_objectarray_t *class_get_declaredclasses(classinfo *c, bool publicOnly);
375 classinfo                 *class_get_declaringclass(classinfo *c);
376 classinfo                 *class_get_enclosingclass(classinfo *c);
377 java_handle_objectarray_t *class_get_interfaces(classinfo *c);
378
379 #if defined(ENABLE_JAVASE)
380 utf                       *class_get_signature(classinfo *c);
381 #endif
382
383 /* some debugging functions */
384
385 #if !defined(NDEBUG)
386 void class_printflags(classinfo *c);
387 void class_print(classinfo *c);
388 void class_println(classinfo *c);
389 void class_classref_print(constant_classref *cr);
390 void class_classref_println(constant_classref *cr);
391 void class_classref_or_classinfo_print(classref_or_classinfo c);
392 void class_classref_or_classinfo_println(classref_or_classinfo c);
393 #endif
394
395 /* debug purposes */
396 void class_showmethods(classinfo *c);
397 void class_showconstantpool(classinfo *c);
398
399 #endif /* _CLASS_H */
400
401
402 /*
403  * These are local overrides for various environment variables in Emacs.
404  * Please do not remove this and leave it at the end of the file, where
405  * Emacs will automagically detect them.
406  * ---------------------------------------------------------------------
407  * Local variables:
408  * mode: c
409  * indent-tabs-mode: t
410  * c-basic-offset: 4
411  * tab-width: 4
412  * End:
413  */