1 /* -*- Mode: C; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // -------------------------------------------------------------------------*/
11 * Handling of classes.
16 #ifndef SQUIRRELJME_CLASSY_H
17 #define SQUIRRELJME_CLASSY_H
19 #include "sjme/nvm/nvm.h"
20 #include "sjme/nvm/stringPool.h"
21 #include "sjme/seekable.h"
22 #include "sjme/list.h"
23 #include "sjme/nvm/descriptor.h"
24 #include "sjme/stream.h"
28 #ifndef SJME_CXX_IS_EXTERNED
29 #define SJME_CXX_IS_EXTERNED
30 #define SJME_CXX_SQUIRRELJME_CLASSY_H
32 #endif /* #ifdef SJME_CXX_IS_EXTERNED */
33 #endif /* #ifdef __cplusplus */
35 /*--------------------------------------------------------------------------*/
38 * The version of the class.
42 typedef enum sjme_class_version
44 /** CLDC 1.1 (JSR 30). */
45 SJME_CLASS_CLDC_1_0
= 2949123,
47 /** CLDC 1.1 (JSR 139). */
48 SJME_CLASS_CLDC_1_1
= 3080192,
51 SJME_CLASS_CLDC_1_8
= 3342336,
55 * Core class information structure.
59 typedef struct sjme_class_infoCore sjme_class_infoCore
;
62 * Opaque class information structure.
66 typedef struct sjme_class_infoCore
* sjme_class_info
;
69 * Opaque constant pool information.
73 typedef struct sjme_class_poolInfoCore sjme_class_poolInfoCore
;
76 * Opaque constant pool information.
80 typedef sjme_class_poolInfoCore
* sjme_class_poolInfo
;
83 * Core method information structure.
87 typedef struct sjme_class_methodInfoCore sjme_class_methodInfoCore
;
90 * Opaque method information structure.
94 typedef struct sjme_class_methodInfoCore
* sjme_class_methodInfo
;
101 SJME_LIST_DECLARE(sjme_class_methodInfo
, 0);
103 /** The basic type of @c sjme_class_methodInfo . */
104 #define SJME_TYPEOF_BASIC_sjme_class_methodInfo \
105 SJME_BASIC_TYPE_ID_OBJECT
108 * Core field information structure.
112 typedef struct sjme_class_fieldInfoCore sjme_class_fieldInfoCore
;
115 * Opaque field information structure.
119 typedef struct sjme_class_fieldInfoCore
* sjme_class_fieldInfo
;
126 SJME_LIST_DECLARE(sjme_class_fieldInfo
, 0);
128 /** The basic type of @c sjme_class_fieldInfo . */
129 #define SJME_TYPEOF_BASIC_sjme_class_fieldInfo \
130 SJME_BASIC_TYPE_ID_OBJECT
133 * Exception handling information.
137 typedef struct sjme_class_exceptionHandler
139 /** The range of the exception where it applies. */
142 /** The handler PC address. */
145 /** The type that this catches. */
146 sjme_desc_binaryName handles
;
147 } sjme_class_exceptionHandler
;
149 /** A list of exceptions. */
150 SJME_LIST_DECLARE(sjme_class_exceptionHandler
, 0);
152 /** The basic type of @c sjme_class_exceptionHandler . */
153 #define SJME_TYPEOF_BASIC_sjme_class_exceptionHandler \
154 SJME_BASIC_TYPE_ID_OBJECT
157 * Method code information structure.
161 typedef struct sjme_class_codeInfoCore sjme_class_codeInfoCore
;
164 * Opaque method code structure.
168 typedef struct sjme_class_codeInfoCore
* sjme_class_codeInfo
;
175 typedef struct sjme_class_accessFlags
177 /** Is this public? */
178 sjme_jboolean
public : 1;
180 /** Is this private? */
181 sjme_jboolean
private : 1;
183 /** Is this protected? */
184 sjme_jboolean
protected : 1;
185 } sjme_class_accessFlags
;
192 typedef struct sjme_class_classFlags
195 sjme_class_accessFlags access
;
197 /** Is the class final? */
198 sjme_jboolean final
: 1;
200 /** Is the class super? */
201 sjme_jboolean super
: 1;
203 /** Is the class an interface? */
204 sjme_jboolean interface
: 1;
206 /** Is the class abstract? */
207 sjme_jboolean abstract
: 1;
209 /** Is the class synthetic? */
210 sjme_jboolean synthetic
: 1;
212 /** Is the class an annotation? */
213 sjme_jboolean annotation
: 1;
215 /** Is the class an enum? */
216 sjme_jboolean enumeration
: 1;
217 } sjme_class_classFlags
;
220 * Common member flags.
224 typedef struct sjme_class_memberFlags
227 sjme_class_accessFlags access
;
229 /** Static member? */
230 sjme_jboolean isStatic
: 1;
233 sjme_jboolean final
: 1;
235 /** Synthetic member? */
236 sjme_jboolean synthetic
: 1;
237 } sjme_class_memberFlags
;
244 typedef struct sjme_class_fieldFlags
246 /** Common member flags. */
247 sjme_class_memberFlags member
;
249 /** Is this volatile? */
250 sjme_jboolean isVolatile
: 1;
252 /** Is this transient? */
253 sjme_jboolean transient
: 1;
255 /** Is this an enumeration. */
256 sjme_jboolean enumeration
: 1;
257 } sjme_class_fieldFlags
;
264 typedef struct sjme_class_methodFlags
266 /** Common member flags. */
267 sjme_class_memberFlags member
;
269 /** Synchronized, monitor entry/exit on call? */
270 sjme_jboolean synchronized
: 1;
272 /** Bridge method, generated by the compiler? */
273 sjme_jboolean bridge
: 1;
275 /** Variadic arguments? */
276 sjme_jboolean varargs
: 1;
278 /** Is this a native method? */
279 sjme_jboolean native
: 1;
282 sjme_jboolean abstract
: 1;
284 /** Strict floating point? */
285 sjme_jboolean strictfp
: 1;
286 } sjme_class_methodFlags
;
289 * The type that a constant pool entry may be.
293 typedef enum sjme_class_poolType
296 SJME_CLASS_POOL_TYPE_NULL
= 0,
299 SJME_CLASS_POOL_TYPE_UTF
= 1,
302 SJME_CLASS_POOL_TYPE_UNUSED_2
= 2,
304 /** Integer constant. */
305 SJME_CLASS_POOL_TYPE_INTEGER
= 3,
307 /** Float constant. */
308 SJME_CLASS_POOL_TYPE_FLOAT
= 4,
310 /** Long constant. */
311 SJME_CLASS_POOL_TYPE_LONG
= 5,
313 /** Double constant. */
314 SJME_CLASS_POOL_TYPE_DOUBLE
= 6,
316 /** Class constant. */
317 SJME_CLASS_POOL_TYPE_CLASS
= 7,
319 /** String constant. */
320 SJME_CLASS_POOL_TYPE_STRING
= 8,
322 /** Field reference. */
323 SJME_CLASS_POOL_TYPE_FIELD
= 9,
325 /** Method reference. */
326 SJME_CLASS_POOL_TYPE_METHOD
= 10,
328 /** Interface method reference. */
329 SJME_CLASS_POOL_TYPE_INTERFACE_METHOD
= 11,
331 /** Name and type. */
332 SJME_CLASS_POOL_TYPE_NAME_AND_TYPE
= 12,
335 SJME_CLASS_POOL_TYPE_UNUSED_13
= 13,
338 SJME_CLASS_POOL_TYPE_UNUSED_14
= 14,
340 /** Method handle. */
341 SJME_CLASS_POOL_TYPE_METHOD_HANDLE
= 15,
344 SJME_CLASS_POOL_TYPE_METHOD_TYPE
= 16,
347 SJME_CLASS_POOL_TYPE_UNUSED_17
= 17,
349 /** Invoke dynamic. */
350 SJME_CLASS_POOL_TYPE_INVOKE_DYNAMIC
= 18,
352 /** The number of pool types. */
353 SJME_NUM_CLASS_POOL_TYPE
354 } sjme_class_poolType
;
357 * A @c SJME_CLASS_POOL_TYPE_CLASS which represents a class or interface.
361 typedef struct sjme_class_poolEntryClass
363 /** The type of entry that this is. */
364 sjme_class_poolType type
;
366 /** The descriptor this represents. */
367 sjme_stringPool_string descriptor
;
368 } sjme_class_poolEntryClass
;
371 * A @c SJME_CLASS_POOL_TYPE_DOUBLE which represents a double constant.
375 typedef struct sjme_class_poolEntryDouble
377 /** The type of entry that this is. */
378 sjme_class_poolType type
;
380 /** The constant value. */
382 } sjme_class_poolEntryDouble
;
385 * A @c SJME_CLASS_POOL_TYPE_NAME_AND_TYPE which represents a name and type
386 * of a member without the class.
390 typedef struct sjme_class_poolEntryNameAndType sjme_class_poolEntryNameAndType
;
393 * Either @c SJME_CLASS_POOL_TYPE_FIELD , @c SJME_CLASS_POOL_TYPE_METHOD ,
394 * or @c SJME_CLASS_POOL_TYPE_INTERFACE_METHOD which represents a reference
399 typedef struct sjme_class_poolEntryMember
401 /** The type of entry that this is. */
402 sjme_class_poolType type
;
404 /** The class this refers to. */
405 sjme_stringPool_string inClass
;
407 /** The name and type used. */
408 const sjme_class_poolEntryNameAndType
* nameAndType
;
409 } sjme_class_poolEntryMember
;
412 * A @c SJME_CLASS_POOL_TYPE_FLOAT which represents a float constant.
416 typedef struct sjme_class_poolEntryFloat
418 /** The type of entry that this is. */
419 sjme_class_poolType type
;
421 /** The constant value. */
423 } sjme_class_poolEntryFloat
;
426 * A @c SJME_CLASS_POOL_TYPE_INTEGER which represents an integer constant.
430 typedef struct sjme_class_poolEntryInteger
432 /** The type of entry that this is. */
433 sjme_class_poolType type
;
435 /** The constant value. */
437 } sjme_class_poolEntryInteger
;
440 * A @c SJME_CLASS_POOL_TYPE_LONG which represents a long constant.
444 typedef struct sjme_class_poolEntryLong
446 /** The type of entry that this is. */
447 sjme_class_poolType type
;
449 /** The constant value. */
451 } sjme_class_poolEntryLong
;
453 struct sjme_class_poolEntryNameAndType
455 /** The type of entry that this is. */
456 sjme_class_poolType type
;
459 sjme_stringPool_string name
;
462 sjme_stringPool_string descriptor
;
466 * A @c SJME_CLASS_POOL_TYPE_UTF which is a modified UTF-8 entry.
470 typedef struct sjme_class_poolEntryUtf
472 /** The type of entry that this is. */
473 sjme_class_poolType type
;
475 /** The hash code for the entry. */
478 /** The UTF data for this entry. */
479 sjme_stringPool_string utf
;
480 } sjme_class_poolEntryUtf
;
483 * Represents a single constant pool entry.
487 typedef union sjme_class_poolEntry
489 /** The type of entry that this is. */
490 sjme_class_poolType type
;
493 sjme_class_poolEntryClass classRef
;
496 sjme_class_poolEntryDouble constDouble
;
499 sjme_class_poolEntryFloat constFloat
;
502 sjme_class_poolEntryInteger constInteger
;
505 sjme_class_poolEntryLong constLong
;
507 /** A class member. */
508 sjme_class_poolEntryMember member
;
510 /** Name and type. */
511 sjme_class_poolEntryNameAndType nameAndType
;
513 /** UTF pool entry. */
514 sjme_class_poolEntryUtf utf
;
515 } sjme_class_poolEntry
;
517 /** A list of constant pool entries. */
518 SJME_LIST_DECLARE(sjme_class_poolEntry
, 0);
520 struct sjme_class_poolInfoCore
522 /** The common NanoCoat base. */
523 sjme_nvm_commonBase common
;
525 /** Constant pool entries. */
526 sjme_list_sjme_class_poolEntry
* pool
;
529 struct sjme_class_infoCore
531 /** The common NanoCoat base. */
532 sjme_nvm_commonBase common
;
534 /** The constant pool of this class. */
535 sjme_class_poolInfo pool
;
537 /** The name of this class. */
538 sjme_stringPool_string name
;
540 /** The superclass of this class. */
541 sjme_stringPool_string superName
;
543 /** The interfaces this class implements. */
544 const sjme_list_sjme_stringPool_string
* interfaceNames
;
546 /** Fields within the method. */
547 const sjme_list_sjme_class_fieldInfo
* fields
;
549 /** Methods within the class. */
550 const sjme_list_sjme_class_methodInfo
* methods
;
554 * Represents a field's constant value.
558 typedef struct sjme_class_fieldConstVal
560 /** The type of value. */
561 sjme_javaTypeId type
;
563 /** The value of the field. */
566 /** The Java value. */
569 /** Constant string. */
570 sjme_stringPool_string string
;
572 } sjme_class_fieldConstVal
;
574 struct sjme_class_fieldInfoCore
576 /** The class which contains this field. */
577 sjme_class_info inClass
;
580 sjme_class_fieldFlags flags
;
582 /** The constant value, if any. */
583 sjme_class_fieldConstVal constVal
;
586 struct sjme_class_methodInfoCore
588 /** The class which contains this method. */
589 sjme_class_info inClass
;
592 sjme_class_methodFlags flags
;
594 /** The method code, if it is not native. */
595 sjme_class_codeInfo code
;
598 struct sjme_class_codeInfoCore
600 /** The method which contains this code. */
601 sjme_class_methodInfo inMethod
;
603 /** Maximum number of locals. */
606 /** Maximum number of stack entries. */
609 /** Exception table. */
610 const sjme_list_sjme_class_exceptionHandler
* exceptions
;
612 /** Method byte code. */
613 sjme_list_sjme_jubyte code
;
617 * Stack map table representation.
621 typedef struct sjme_class_stackMap
625 } sjme_class_stackMap
;
628 * Parses a single class and loads its class information.
630 * @param inPool The pool to allocate within.
631 * @param inStream The stream to parse from when reading the class.
632 * @param inStringPool The pool for strings existing in memory already.
633 * @param outClass The resultant class information
634 * @return Any resultant error code, if any.
637 sjme_errorCode
sjme_class_parse(
638 sjme_attrInNotNull sjme_alloc_pool
* inPool
,
639 sjme_attrInNotNull sjme_stream_input inStream
,
640 sjme_attrInNotNull sjme_stringPool inStringPool
,
641 sjme_attrOutNotNull sjme_class_info
* outClass
);
643 sjme_errorCode
sjme_class_parseAttributeCode(
644 sjme_attrInNotNull sjme_stream_input inStream
,
645 sjme_attrInNotNull sjme_class_methodInfo inMethod
,
646 sjme_attrOutNotNull sjme_class_codeInfo
* outCode
);
648 sjme_errorCode
sjme_class_parseAttributeConstVal(
649 sjme_attrInNotNull sjme_stream_input inStream
,
650 sjme_attrInNotNull sjme_class_fieldInfo inField
,
651 sjme_attrOutNotNull sjme_class_fieldConstVal
* outConstVal
);
653 sjme_errorCode
sjme_class_parseAttributeStackMapOld(
654 sjme_attrInNotNull sjme_stream_input inStream
,
655 sjme_attrOutNotNull sjme_class_codeInfo inCode
,
656 sjme_attrOutNotNull sjme_class_stackMap
* outStackMap
);
658 sjme_errorCode
sjme_class_parseAttributeStackMapNew(
659 sjme_attrInNotNull sjme_stream_input inStream
,
660 sjme_attrOutNotNull sjme_class_codeInfo inCode
,
661 sjme_attrOutNotNull sjme_class_stackMap
* outStackMap
);
664 * Parses the constant pool of an input class.
666 * @param inPool The input pool.
667 * @param inStream The stream to read from.
668 * @param inStringPool The string pool for reused strings.
669 * @param outPool The resultant read constant pool.
670 * @return Any resultant error, if any.
673 sjme_errorCode
sjme_class_parseConstantPool(
674 sjme_attrInNotNull sjme_alloc_pool
* inPool
,
675 sjme_attrInNotNull sjme_stream_input inStream
,
676 sjme_attrInNotNull sjme_stringPool inStringPool
,
677 sjme_attrOutNotNull sjme_class_poolInfo
* outPool
);
679 sjme_errorCode
sjme_class_parseField(
680 sjme_attrInNotNull sjme_stream_input inStream
,
681 sjme_attrOutNotNull sjme_class_fieldInfo
* outField
);
683 sjme_errorCode
sjme_class_parseFields(
684 sjme_attrInNotNull sjme_stream_input inStream
,
685 sjme_attrInNotNull sjme_class_info inClass
,
686 sjme_attrOutNotNull sjme_list_sjme_class_fieldInfo
* outFields
);
688 sjme_errorCode
sjme_class_parseMethod(
689 sjme_attrInNotNull sjme_stream_input inStream
,
690 sjme_attrInOutNotNull sjme_class_methodInfo
* outMethod
);
692 sjme_errorCode
sjme_class_parseMethods(
693 sjme_attrInNotNull sjme_stream_input inStream
,
694 sjme_attrInNotNull sjme_class_info inClass
,
695 sjme_attrOutNotNull sjme_list_sjme_class_methodInfo
* outMethods
);
697 /*--------------------------------------------------------------------------*/
701 #ifdef SJME_CXX_SQUIRRELJME_CLASSY_H
703 #undef SJME_CXX_SQUIRRELJME_CLASSY_H
704 #undef SJME_CXX_IS_EXTERNED
705 #endif /* #ifdef SJME_CXX_SQUIRRELJME_CLASSY_H */
706 #endif /* #ifdef __cplusplus */
708 #endif /* SQUIRRELJME_CLASSY_H */