Line data Source code
1 : /* src/vm/class.hpp - class related functions header
2 :
3 : Copyright (C) 1996-2013
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_ 1
28 :
29 : #include <stddef.h> // for NULL
30 : #include <stdint.h> // for intptr_t, uint8_t
31 : #include <sys/types.h> // for int32_t
32 : #include "config.h" // for ENABLE_JAVASE, etc
33 : #include "vm/global.hpp" // for java_handle_t, etc
34 : #include "vm/linker.hpp" // for link_class
35 : #include "vm/loader.hpp" // for classloader_t
36 : #include "vm/references.hpp" // for classref_or_classinfo, etc
37 : #include "vm/types.hpp" // for s4, u4, u1, u2
38 : #include "vm/utf8.hpp" // for Utf8String
39 : #include "vm/vftbl.hpp" // for vftbl_t
40 :
41 : namespace cacao {
42 : struct ClassBuffer;
43 : class OStream;
44 : }
45 :
46 : struct classinfo;
47 : struct extra_classref;
48 : struct fieldinfo;
49 : struct innerclassinfo;
50 : struct methodinfo;
51 :
52 : /* class state defines ********************************************************/
53 :
54 : enum ClassState {
55 : CLASS_LOADING = 0x0001,
56 : CLASS_LOADED = 0x0002,
57 : CLASS_LINKING = 0x0004,
58 : CLASS_LINKED = 0x0008,
59 : CLASS_INITIALIZING = 0x0010,
60 : CLASS_INITIALIZED = 0x0020,
61 : CLASS_ERROR = 0x0040
62 : };
63 :
64 :
65 : /* classinfo ******************************************************************/
66 :
67 : /* We define this dummy structure of java_lang_Class so we can
68 : bootstrap cacaoh without needing a java_lang_Class.h file. Whether
69 : the size of the dummy structure is big enough is checked during
70 : runtime in vm_create. */
71 :
72 : typedef struct {
73 : java_object_t header;
74 : #if defined(WITH_JAVA_RUNTIME_LIBRARY_GNU_CLASSPATH)
75 : intptr_t padding[4];
76 : #elif defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
77 : intptr_t padding[19];
78 : #elif defined(WITH_JAVA_RUNTIME_LIBRARY_CLDC1_1)
79 : intptr_t padding[3];
80 : #else
81 : # error unknown classpath configuration
82 : #endif
83 : } dummy_java_lang_Class;
84 :
85 : struct threadobject;
86 :
87 : struct classinfo { /* class structure */
88 : dummy_java_lang_Class object;
89 :
90 : s4 flags; /* ACC flags */
91 : Utf8String name; /* class name */
92 : cacao::ClassFileVersion version;
93 :
94 : s4 cpcount; /* number of entries in constant pool */
95 : u1 *cptags; /* constant pool tags */
96 : void* *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 :
102 : classinfo *super; /* super class */
103 : classinfo *sub; /* sub class pointer */
104 : classinfo *nextsub; /* pointer to next class in sub class list */
105 :
106 : int32_t interfacescount; /* number of interfaces */
107 : classinfo **interfaces; /* super interfaces */
108 :
109 : int32_t fieldscount; /* number of fields */
110 : fieldinfo *fields; /* field table */
111 :
112 : int32_t methodscount; /* number of methods */
113 : methodinfo *methods; /* method table */
114 :
115 : s4 state; /* current class state */
116 : s4 index; /* hierarchy depth (classes) or index */
117 : /* (interfaces) */
118 : s4 instancesize; /* size of an instance of this class */
119 :
120 : threadobject *initializing_thread;
121 : vftbl_t *vftbl; /* pointer to virtual function table */
122 :
123 : methodinfo *finalizer; /* finalizer method */
124 :
125 : u2 innerclasscount; /* number of inner classes */
126 : innerclassinfo *innerclass;
127 :
128 : classref_or_classinfo declaringclass;
129 : classref_or_classinfo enclosingclass; /* enclosing class */
130 : constant_nameandtype *enclosingmethod; /* enclosing method */
131 :
132 : Utf8String packagename; /* full name of the package */
133 : Utf8String sourcefile; /* SourceFile attribute */
134 : #if defined(ENABLE_JAVASE)
135 : Utf8String signature; /* Signature attribute */
136 : #if defined(ENABLE_ANNOTATIONS)
137 : /* All the annotation attributes are NULL (and not a zero length array) */
138 : /* if there is nothing. */
139 : java_object_t *annotations; /* annotations of this class */
140 :
141 : java_object_t *method_annotations; /* array of annotations of the methods */
142 : java_object_t *method_parameterannotations; /* array of parameter */
143 : /* annotations of the methods */
144 : java_object_t *method_annotationdefaults; /* array of annotation default */
145 : /* values of the methods */
146 :
147 : java_object_t *field_annotations; /* array of annotations of the fields */
148 :
149 : #endif
150 : #endif
151 : classloader_t *classloader; /* NULL for bootstrap classloader */
152 :
153 : #if defined(ENABLE_JAVASE)
154 : # if defined(WITH_JAVA_RUNTIME_LIBRARY_OPENJDK)
155 : java_object_t *protectiondomain;
156 : java_objectarray_t *signers;
157 : # endif
158 : #endif
159 : };
160 :
161 :
162 : /* innerclassinfo *************************************************************/
163 :
164 : struct innerclassinfo {
165 : classref_or_classinfo inner_class; /* inner class pointer */
166 : classref_or_classinfo outer_class; /* outer class pointer */
167 : Utf8String name; /* innerclass name */
168 : s4 flags; /* ACC flags */
169 : };
170 :
171 :
172 : /* extra_classref **************************************************************
173 :
174 : for classrefs not occurring within descriptors
175 :
176 : *******************************************************************************/
177 :
178 : struct extra_classref {
179 4244 : extra_classref(extra_classref *next, classinfo *referer, Utf8String name)
180 4244 : : next(next), classref(referer, name) {}
181 :
182 : extra_classref *next;
183 : constant_classref classref;
184 : };
185 :
186 : /* function prototypes ********************************************************/
187 :
188 : classinfo *class_create_classinfo(Utf8String u);
189 : void class_postset_header_vftbl(void);
190 : classinfo *class_define(Utf8String name, classloader_t *cl, int32_t length, uint8_t *data, java_handle_t *pd);
191 : void class_set_packagename(classinfo *c);
192 :
193 : bool class_load_attributes(cacao::ClassBuffer& cb);
194 :
195 : bool class_initializing_thread_is_self(classinfo *c);
196 :
197 : /* retrieve constantpool element */
198 : void* class_getconstant(classinfo *c, u4 pos, ConstantPoolTag ctype);
199 : void* innerclass_getconstant(classinfo *c, u4 pos, ConstantPoolTag ctype);
200 :
201 : /* frees all resources used by the class */
202 : void class_free(classinfo *);
203 :
204 : /* return an array class with the given component class */
205 : classinfo *class_array_of(classinfo *component,bool link);
206 :
207 : /* return an array class with the given dimension and element class */
208 : classinfo *class_multiarray_of(s4 dim, classinfo *element,bool link);
209 :
210 : /* return a classref for the given class name */
211 : /* (does a linear search!) */
212 : constant_classref *class_lookup_classref(classinfo *cls,Utf8String name);
213 :
214 : /* return a classref for the given class name */
215 : /* (does a linear search!) */
216 : constant_classref *class_get_classref(classinfo *cls,Utf8String name);
217 :
218 : /* return a classref to the class itself */
219 : /* (does a linear search!) */
220 : constant_classref *class_get_self_classref(classinfo *cls);
221 :
222 : /* return a classref for an array with the given dimension of with the */
223 : /* given component type */
224 : constant_classref *class_get_classref_multiarray_of(s4 dim,constant_classref *ref);
225 :
226 : /* return a classref for the component type of the given array type */
227 : constant_classref *class_get_classref_component_of(constant_classref *ref);
228 :
229 : /* get a class' field by name and descriptor */
230 : fieldinfo *class_findfield(classinfo *c, Utf8String name, Utf8String desc);
231 :
232 : /* search 'classinfo'-structure for a field with the specified name */
233 : fieldinfo *class_findfield_by_name(classinfo *c, Utf8String name);
234 :
235 : /* search class for a field */
236 : fieldinfo *class_resolvefield(classinfo *c, Utf8String name, Utf8String desc, classinfo *referer);
237 :
238 : /* search for a method with a specified name and descriptor */
239 : methodinfo *class_findmethod(classinfo *c, Utf8String name, Utf8String desc);
240 : methodinfo *class_resolvemethod(classinfo *c, Utf8String name, Utf8String dest);
241 : methodinfo *class_resolveclassmethod(classinfo *c, Utf8String name, Utf8String dest, classinfo *referer, bool throwexception);
242 : methodinfo *class_resolveinterfacemethod(classinfo *c, Utf8String name, Utf8String dest, classinfo *referer, bool throwexception);
243 :
244 : bool class_issubclass(classinfo *sub, classinfo *super);
245 : bool class_isanysubclass(classinfo *sub, classinfo *super);
246 : bool class_is_arraycompatible(struct arraydescriptor *desc, struct arraydescriptor *target);
247 : bool class_is_assignable_from(classinfo *to, classinfo *from);
248 : bool class_is_instance(classinfo *c, java_handle_t *h);
249 :
250 : static inline classloader_t *class_get_classloader(classinfo *c);
251 : static inline classinfo *class_get_superclass(classinfo *c);
252 : classinfo *class_get_componenttype(classinfo *c);
253 : java_handle_objectarray_t *class_get_declaredclasses(classinfo *c, bool publicOnly);
254 : java_handle_objectarray_t *class_get_declaredconstructors(classinfo *c, bool publicOnly);
255 : java_handle_objectarray_t *class_get_declaredfields(classinfo *c, bool publicOnly);
256 : java_handle_objectarray_t *class_get_declaredmethods(classinfo *c, bool publicOnly);
257 : classinfo *class_get_declaringclass(classinfo *c);
258 : classinfo *class_get_enclosingclass(classinfo *c);
259 : java_handle_t* class_get_enclosingconstructor(classinfo *c);
260 : methodinfo* class_get_enclosingmethod_raw(classinfo *c);
261 : java_handle_t* class_get_enclosingmethod(classinfo *c);
262 : java_handle_objectarray_t *class_get_interfaces(classinfo *c);
263 : java_handle_bytearray_t *class_get_annotations(classinfo *c);
264 : int32_t class_get_modifiers(classinfo *c, bool ignoreInnerClassesAttrib);
265 : java_handle_t *class_get_name(classinfo *c);
266 :
267 : #if defined(ENABLE_JAVASE)
268 : Utf8String class_get_signature(classinfo *c);
269 : #endif
270 :
271 : /* some debugging functions */
272 :
273 : #if !defined(NDEBUG)
274 : void class_printflags(classinfo *c);
275 : void class_print(classinfo *c);
276 : void class_println(classinfo *c);
277 : void class_classref_print(constant_classref *cr);
278 : void class_classref_println(constant_classref *cr);
279 : void class_classref_or_classinfo_print(classref_or_classinfo c);
280 : void class_classref_or_classinfo_println(classref_or_classinfo c);
281 : #endif
282 :
283 : namespace cacao {
284 :
285 : /* OStream overloads */
286 : OStream& operator<<(OStream& os, const classinfo *c);
287 :
288 : }
289 :
290 : /* debug purposes */
291 : void class_showmethods(classinfo *c);
292 : void class_showconstantpool(classinfo *c);
293 :
294 : /* inline functions ***********************************************************/
295 :
296 : /**
297 : * Returns the classname of the class, where slashes ('/') are
298 : * replaced by dots ('.').
299 : *
300 : * @param c class to get name of
301 : * @return classname
302 : */
303 : java_handle_t* class_get_classname(classinfo* c);
304 :
305 : /* class_is_primitive **********************************************************
306 :
307 : Checks if the given class is a primitive class.
308 :
309 : *******************************************************************************/
310 :
311 525770 : static inline bool class_is_primitive(classinfo *c)
312 : {
313 525770 : if (c->flags & ACC_CLASS_PRIMITIVE)
314 166 : return true;
315 :
316 525604 : return false;
317 : }
318 :
319 :
320 : /* class_is_anonymousclass *****************************************************
321 :
322 : Checks if the given class is an anonymous class.
323 :
324 : *******************************************************************************/
325 :
326 53 : static inline bool class_is_anonymousclass(classinfo *c)
327 : {
328 53 : if (c->flags & ACC_CLASS_ANONYMOUS)
329 30 : return true;
330 :
331 23 : return false;
332 : }
333 :
334 :
335 : /* class_is_array **************************************************************
336 :
337 : Checks if the given class is an array class.
338 :
339 : *******************************************************************************/
340 :
341 836791 : static inline bool class_is_array(classinfo *c)
342 : {
343 836791 : if (!(c->state & CLASS_LINKED))
344 9 : if (!link_class(c))
345 0 : return false;
346 :
347 836791 : return (c->vftbl->arraydesc != NULL);
348 : }
349 :
350 :
351 : /* class_is_interface **********************************************************
352 :
353 : Checks if the given class is an interface.
354 :
355 : *******************************************************************************/
356 :
357 29 : static inline bool class_is_interface(classinfo *c)
358 : {
359 29 : if (c->flags & ACC_INTERFACE)
360 22 : return true;
361 :
362 7 : return false;
363 : }
364 :
365 :
366 : /* class_is_localclass *********************************************************
367 :
368 : Checks if the given class is a local class.
369 :
370 : *******************************************************************************/
371 :
372 26 : static inline bool class_is_localclass(classinfo *c)
373 : {
374 26 : if ((c->enclosingmethod != NULL) && !class_is_anonymousclass(c))
375 8 : return true;
376 :
377 18 : return false;
378 : }
379 :
380 :
381 : /* class_is_memberclass ********************************************************
382 :
383 : Checks if the given class is a member class.
384 :
385 : *******************************************************************************/
386 :
387 15 : static inline bool class_is_memberclass(classinfo *c)
388 : {
389 15 : if (c->flags & ACC_CLASS_MEMBER)
390 4 : return true;
391 :
392 11 : return false;
393 : }
394 :
395 :
396 : /* class_get_classloader *******************************************************
397 :
398 : Return the classloader of the given class.
399 :
400 : *******************************************************************************/
401 :
402 3556 : static inline classloader_t *class_get_classloader(classinfo *c)
403 : {
404 : classloader_t *cl;
405 :
406 3556 : cl = c->classloader;
407 :
408 : /* The classloader may be NULL. */
409 :
410 3556 : return cl;
411 : }
412 :
413 :
414 : /* class_get_superclass ********************************************************
415 :
416 : Return the super class of the given class.
417 :
418 : *******************************************************************************/
419 :
420 929 : static inline classinfo *class_get_superclass(classinfo *c)
421 : {
422 : /* For interfaces we return NULL. */
423 :
424 929 : if (c->flags & ACC_INTERFACE)
425 105 : return NULL;
426 :
427 : /* For java/lang/Object, primitive-type and Void classes c->super
428 : is NULL and we return NULL. */
429 :
430 824 : return c->super;
431 : }
432 :
433 157124 : static inline bool class_is_or_almost_initialized(classinfo *c) {
434 : return ((c)->state & CLASS_INITIALIZED)
435 157124 : || ((c)->state & CLASS_INITIALIZING && class_initializing_thread_is_self((c)));
436 : }
437 :
438 : #endif // CLASS_HPP_
439 :
440 :
441 : /*
442 : * These are local overrides for various environment variables in Emacs.
443 : * Please do not remove this and leave it at the end of the file, where
444 : * Emacs will automagically detect them.
445 : * ---------------------------------------------------------------------
446 : * Local variables:
447 : * mode: c++
448 : * indent-tabs-mode: t
449 : * c-basic-offset: 4
450 : * tab-width: 4
451 : * End:
452 : * vim:noexpandtab:sw=4:ts=4:
453 : */
|