bf9130f5ad5b38cd9c7e8c6c07a8fe690028cd39
[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 7797 2007-04-23 20:12:39Z 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 #include "vm/types.h"
43
44 #include "toolbox/list.h"
45
46 #include "vm/global.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.  If the
81    size is big enough, is checked during runtime in vm_create. */
82
83 typedef struct {
84         java_objectheader header;
85         ptrint            padding[4];
86 } dummy_java_lang_Class;
87
88 struct classinfo {                /* class structure                          */
89         dummy_java_lang_Class object;
90
91         s4          flags;            /* ACC flags                                */
92         utf        *name;             /* class name                               */
93
94         s4          cpcount;          /* number of entries in constant pool       */
95         u1         *cptags;           /* constant pool tags                       */
96         voidptr    *cpinfos;          /* pointer to constant pool info structures */
97
98         s4          classrefcount;    /* number of symbolic class references      */
99         constant_classref *classrefs; /* table of symbolic class references       */
100         extra_classref *extclassrefs; /* additional classrefs                     */
101         s4          parseddescsize;   /* size of the parsed descriptors block     */
102         u1         *parseddescs;      /* parsed descriptors                       */
103
104         classref_or_classinfo super;  /* super class                              */
105         classinfo  *sub;              /* sub class pointer                        */
106         classinfo  *nextsub;          /* pointer to next class in sub class list  */
107
108         s4          interfacescount;  /* number of interfaces                     */
109         classref_or_classinfo *interfaces; /* superinterfaces                     */
110
111         s4          fieldscount;      /* number of fields                         */
112         fieldinfo  *fields;           /* field table                              */
113
114         s4          methodscount;     /* number of methods                        */
115         methodinfo *methods;          /* method table                             */
116
117         listnode_t  listnode;         /* linkage                                  */
118
119         s4          state;            /* current class state                      */
120         s4          index;            /* hierarchy depth (classes) or index       */
121                                       /* (interfaces)                             */
122         s4          instancesize;     /* size of an instance of this class        */
123
124         vftbl_t    *vftbl;            /* pointer to virtual function table        */
125
126         methodinfo *finalizer;        /* finalizer method                         */
127
128         u2          innerclasscount;  /* number of inner classes                  */
129         innerclassinfo *innerclass;
130
131 #if defined(ENABLE_JAVASE)
132         classref_or_classinfo  enclosingclass;  /* enclosing class                */
133         constant_nameandtype  *enclosingmethod; /* enclosing method               */
134 #endif
135
136         utf        *packagename;      /* full name of the package                 */
137         utf        *sourcefile;       /* SourceFile attribute                     */
138 #if defined(ENABLE_JAVASE)
139         utf        *signature;        /* Signature attribute                      */
140         s4            runtimevisibleannotationscount;
141         annotation_t *runtimevisibleannotations;
142 #endif
143         classloader *classloader;     /* NULL for bootstrap classloader           */
144 };
145
146
147 /* innerclassinfo *************************************************************/
148
149 struct innerclassinfo {
150         classref_or_classinfo inner_class; /* inner class pointer                 */
151         classref_or_classinfo outer_class; /* outer class pointer                 */
152         utf                  *name;        /* innerclass name                     */
153         s4                    flags;       /* ACC flags                           */
154 };
155
156
157 /* extra_classref **************************************************************
158
159    for classrefs not occurring within descriptors
160
161 *******************************************************************************/
162
163 struct extra_classref {
164         extra_classref    *next;
165         constant_classref  classref;
166 };
167
168
169 /* castinfo *******************************************************************/
170
171 struct castinfo {
172         s4 super_baseval;
173         s4 super_diffval;
174         s4 sub_baseval;
175 };
176
177
178 /* global variables ***********************************************************/
179
180 extern list_t unlinkedclasses; /* this is only used for eager class loading   */
181
182
183 /* frequently used classes ****************************************************/
184
185 /* important system classes */
186
187 extern classinfo *class_java_lang_Object;
188 extern classinfo *class_java_lang_Class;
189 extern classinfo *class_java_lang_ClassLoader;
190 extern classinfo *class_java_lang_Cloneable;
191 extern classinfo *class_java_lang_SecurityManager;
192 extern classinfo *class_java_lang_String;
193 extern classinfo *class_java_lang_System;
194 extern classinfo *class_java_lang_Thread;
195 extern classinfo *class_java_lang_ThreadGroup;
196 extern classinfo *class_java_lang_VMSystem;
197 extern classinfo *class_java_lang_VMThread;
198 extern classinfo *class_java_io_Serializable;
199
200
201 /* system exception classes required in cacao */
202
203 extern classinfo *class_java_lang_Throwable;
204 extern classinfo *class_java_lang_Error;
205 extern classinfo *class_java_lang_LinkageError;
206 extern classinfo *class_java_lang_NoClassDefFoundError;
207 extern classinfo *class_java_lang_OutOfMemoryError;
208 extern classinfo *class_java_lang_VirtualMachineError;
209
210 #if defined(WITH_CLASSPATH_GNU)
211 extern classinfo *class_java_lang_VMThrowable;
212 #endif
213
214 extern classinfo *class_java_lang_Exception;
215 extern classinfo *class_java_lang_ClassCastException;
216 extern classinfo *class_java_lang_ClassNotFoundException;
217
218 #if defined(ENABLE_JAVASE)
219 extern classinfo *class_java_lang_Void;
220 #endif
221
222 extern classinfo *class_java_lang_Boolean;
223 extern classinfo *class_java_lang_Byte;
224 extern classinfo *class_java_lang_Character;
225 extern classinfo *class_java_lang_Short;
226 extern classinfo *class_java_lang_Integer;
227 extern classinfo *class_java_lang_Long;
228 extern classinfo *class_java_lang_Float;
229 extern classinfo *class_java_lang_Double;
230
231
232 /* some runtime exception */
233
234 extern classinfo *class_java_lang_NullPointerException;
235
236
237 /* some classes which may be used more often */
238
239 #if defined(ENABLE_JAVASE)
240 extern classinfo *class_java_lang_StackTraceElement;
241 extern classinfo *class_java_lang_reflect_Constructor;
242 extern classinfo *class_java_lang_reflect_Field;
243 extern classinfo *class_java_lang_reflect_Method;
244 extern classinfo *class_java_security_PrivilegedAction;
245 extern classinfo *class_java_util_Vector;
246
247 extern classinfo *arrayclass_java_lang_Object;
248 #endif
249
250
251 /* pseudo classes for the type checker ****************************************/
252
253 /*
254  * pseudo_class_Arraystub
255  *     (extends Object implements Cloneable, java.io.Serializable)
256  *
257  *     If two arrays of incompatible component types are merged,
258  *     the resulting reference has no accessible components.
259  *     The result does, however, implement the interfaces Cloneable
260  *     and java.io.Serializable. This pseudo class is used internally
261  *     to represent such results. (They are *not* considered arrays!)
262  *
263  * pseudo_class_Null
264  *
265  *     This pseudo class is used internally to represent the
266  *     null type.
267  *
268  * pseudo_class_New
269  *
270  *     This pseudo class is used internally to represent the
271  *     the 'uninitialized object' type.
272  */
273
274 extern classinfo *pseudo_class_Arraystub;
275 extern classinfo *pseudo_class_Null;
276 extern classinfo *pseudo_class_New;
277
278
279 /* function prototypes ********************************************************/
280
281 /* create a new classinfo struct */
282 classinfo *class_create_classinfo(utf *u);
283
284 /* postset's the header.vftbl */
285 void class_postset_header_vftbl(void);
286
287 /* set the package name after the name has been set */
288 void class_set_packagename(classinfo *c);
289
290 bool class_load_attributes(classbuffer *cb);
291
292 /* retrieve constantpool element */
293 voidptr class_getconstant(classinfo *class, u4 pos, u4 ctype);
294 voidptr innerclass_getconstant(classinfo *c, u4 pos, u4 ctype);
295
296 /* frees all resources used by the class */
297 void class_free(classinfo *);
298
299 /* return an array class with the given component class */
300 classinfo *class_array_of(classinfo *component,bool link);
301
302 /* return an array class with the given dimension and element class */
303 classinfo *class_multiarray_of(s4 dim, classinfo *element,bool link);
304
305 /* return a classref for the given class name */
306 /* (does a linear search!)                    */
307 constant_classref *class_lookup_classref(classinfo *cls,utf *name);
308
309 /* return a classref for the given class name */
310 /* (does a linear search!)                    */
311 constant_classref *class_get_classref(classinfo *cls,utf *name);
312
313 /* return a classref to the class itself */
314 /* (does a linear search!)                    */
315 constant_classref *class_get_self_classref(classinfo *cls);
316
317 /* return a classref for an array with the given dimension of with the */
318 /* given component type */
319 constant_classref *class_get_classref_multiarray_of(s4 dim,constant_classref *ref);
320
321 /* return a classref for the component type of the given array type */
322 constant_classref *class_get_classref_component_of(constant_classref *ref);
323
324 /* get a class' field by name and descriptor */
325 fieldinfo *class_findfield(classinfo *c, utf *name, utf *desc);
326
327 /* search 'classinfo'-structure for a field with the specified name */
328 fieldinfo *class_findfield_by_name(classinfo *c, utf *name);
329 s4 class_findfield_index_by_name(classinfo *c, utf *name);
330
331 /* search class for a field */
332 fieldinfo *class_resolvefield(classinfo *c, utf *name, utf *desc, classinfo *referer, bool throwexception);
333
334 /* search for a method with a specified name and descriptor */
335 methodinfo *class_findmethod(classinfo *c, utf *name, utf *desc);
336 methodinfo *class_resolvemethod(classinfo *c, utf *name, utf *dest);
337 methodinfo *class_resolveclassmethod(classinfo *c, utf *name, utf *dest, classinfo *referer, bool throwexception);
338 methodinfo *class_resolveinterfacemethod(classinfo *c, utf *name, utf *dest, classinfo *referer, bool throwexception);
339
340 bool class_issubclass(classinfo *sub, classinfo *super);
341 bool class_isanysubclass(classinfo *sub, classinfo *super);
342
343 /* some debugging functions */
344
345 #if !defined(NDEBUG)
346 void class_printflags(classinfo *c);
347 void class_print(classinfo *c);
348 void class_println(classinfo *c);
349 void class_classref_print(constant_classref *cr);
350 void class_classref_println(constant_classref *cr);
351 void class_classref_or_classinfo_print(classref_or_classinfo c);
352 void class_classref_or_classinfo_println(classref_or_classinfo c);
353 #endif
354
355 /* debug purposes */
356 void class_showmethods(classinfo *c);
357 void class_showconstantpool(classinfo *c);
358
359 #endif /* _CLASS_H */
360
361
362 /*
363  * These are local overrides for various environment variables in Emacs.
364  * Please do not remove this and leave it at the end of the file, where
365  * Emacs will automagically detect them.
366  * ---------------------------------------------------------------------
367  * Local variables:
368  * mode: c
369  * indent-tabs-mode: t
370  * c-basic-offset: 4
371  * tab-width: 4
372  * End:
373  */