Initial base for string pools, which are reference counted; Have closeable variant...
[SquirrelJME.git] / nanocoat / include / sjme / nvm / classy.h
blob3c0eceb2cb9f419f8d930933d209fa1e3eb400cd
1 /* -*- Mode: C; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
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 // -------------------------------------------------------------------------*/
10 /**
11 * Handling of classes.
13 * @since 2024/01/01
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"
26 /* Anti-C++. */
27 #ifdef __cplusplus
28 #ifndef SJME_CXX_IS_EXTERNED
29 #define SJME_CXX_IS_EXTERNED
30 #define SJME_CXX_SQUIRRELJME_CLASSY_H
31 extern "C" {
32 #endif /* #ifdef SJME_CXX_IS_EXTERNED */
33 #endif /* #ifdef __cplusplus */
35 /*--------------------------------------------------------------------------*/
37 /**
38 * The version of the class.
40 * @since 2024/09/13
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,
50 /** CLDC 8. */
51 SJME_CLASS_CLDC_1_8 = 3342336,
52 } sjme_class_version;
54 /**
55 * Core class information structure.
57 * @since 2024/01/01
59 typedef struct sjme_class_infoCore sjme_class_infoCore;
61 /**
62 * Opaque class information structure.
64 * @since 2024/01/01
66 typedef struct sjme_class_infoCore* sjme_class_info;
68 /**
69 * Opaque constant pool information.
71 * @since 2024/09/13
73 typedef struct sjme_class_poolInfoCore sjme_class_poolInfoCore;
75 /**
76 * Opaque constant pool information.
78 * @since 2024/09/13
80 typedef sjme_class_poolInfoCore* sjme_class_poolInfo;
82 /**
83 * Core method information structure.
85 * @since 2024/01/03
87 typedef struct sjme_class_methodInfoCore sjme_class_methodInfoCore;
89 /**
90 * Opaque method information structure.
92 * @since 2024/01/03
94 typedef struct sjme_class_methodInfoCore* sjme_class_methodInfo;
96 /**
97 * Method list.
99 * @since 2024/01/03
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.
110 * @since 2024/01/03
112 typedef struct sjme_class_fieldInfoCore sjme_class_fieldInfoCore;
115 * Opaque field information structure.
117 * @since 2024/01/03
119 typedef struct sjme_class_fieldInfoCore* sjme_class_fieldInfo;
122 * Field list.
124 * @since 2024/01/03
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.
135 * @since 2024/01/03
137 typedef struct sjme_class_exceptionHandler
139 /** The range of the exception where it applies. */
140 sjme_range range;
142 /** The handler PC address. */
143 sjme_jint handlerPc;
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.
159 * @since 2024/01/03
161 typedef struct sjme_class_codeInfoCore sjme_class_codeInfoCore;
164 * Opaque method code structure.
166 * @since 2024/01/03
168 typedef struct sjme_class_codeInfoCore* sjme_class_codeInfo;
171 * Access flags.
173 * @since 2024/01/03
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;
188 * Class flags.
190 * @since 2024/01/03
192 typedef struct sjme_class_classFlags
194 /** Access flags. */
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.
222 * @since 2024/01/03
224 typedef struct sjme_class_memberFlags
226 /** Access flags. */
227 sjme_class_accessFlags access;
229 /** Static member? */
230 sjme_jboolean isStatic : 1;
232 /** Final member? */
233 sjme_jboolean final : 1;
235 /** Synthetic member? */
236 sjme_jboolean synthetic : 1;
237 } sjme_class_memberFlags;
240 * Field flags.
242 * @since 2024/01/03
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;
260 * Method flags.
262 * @since 2024/01/03
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;
281 /** Abstract? */
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.
291 * @since 2024/01/04
293 typedef enum sjme_class_poolType
295 /** Null entry. */
296 SJME_CLASS_POOL_TYPE_NULL = 0,
298 /** Modified UTF. */
299 SJME_CLASS_POOL_TYPE_UTF = 1,
301 /** Unused 2. */
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,
334 /** Unused 13. */
335 SJME_CLASS_POOL_TYPE_UNUSED_13 = 13,
337 /** Unused 14. */
338 SJME_CLASS_POOL_TYPE_UNUSED_14 = 14,
340 /** Method handle. */
341 SJME_CLASS_POOL_TYPE_METHOD_HANDLE = 15,
343 /** Method type. */
344 SJME_CLASS_POOL_TYPE_METHOD_TYPE = 16,
346 /** Unused 17. */
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.
359 * @since 2024/01/04
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.
373 * @since 2024/01/04
375 typedef struct sjme_class_poolEntryDouble
377 /** The type of entry that this is. */
378 sjme_class_poolType type;
380 /** The constant value. */
381 sjme_jdouble 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.
388 * @since 2024/01/04
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
395 * to a class member.
397 * @since 2024/01/04
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.
414 * @since 2024/01/04
416 typedef struct sjme_class_poolEntryFloat
418 /** The type of entry that this is. */
419 sjme_class_poolType type;
421 /** The constant value. */
422 sjme_jfloat value;
423 } sjme_class_poolEntryFloat;
426 * A @c SJME_CLASS_POOL_TYPE_INTEGER which represents an integer constant.
428 * @since 2024/01/04
430 typedef struct sjme_class_poolEntryInteger
432 /** The type of entry that this is. */
433 sjme_class_poolType type;
435 /** The constant value. */
436 sjme_jint value;
437 } sjme_class_poolEntryInteger;
440 * A @c SJME_CLASS_POOL_TYPE_LONG which represents a long constant.
442 * @since 2024/01/04
444 typedef struct sjme_class_poolEntryLong
446 /** The type of entry that this is. */
447 sjme_class_poolType type;
449 /** The constant value. */
450 sjme_jlong value;
451 } sjme_class_poolEntryLong;
453 struct sjme_class_poolEntryNameAndType
455 /** The type of entry that this is. */
456 sjme_class_poolType type;
458 /** The name. */
459 sjme_stringPool_string name;
461 /** The type. */
462 sjme_stringPool_string descriptor;
466 * A @c SJME_CLASS_POOL_TYPE_UTF which is a modified UTF-8 entry.
468 * @since 2024/01/04
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. */
476 sjme_jint hash;
478 /** The UTF data for this entry. */
479 sjme_stringPool_string utf;
480 } sjme_class_poolEntryUtf;
483 * Represents a single constant pool entry.
485 * @since 2024/01/04
487 typedef union sjme_class_poolEntry
489 /** The type of entry that this is. */
490 sjme_class_poolType type;
492 /** Class. */
493 sjme_class_poolEntryClass classRef;
495 /** Double. */
496 sjme_class_poolEntryDouble constDouble;
498 /** Float. */
499 sjme_class_poolEntryFloat constFloat;
501 /** Integer. */
502 sjme_class_poolEntryInteger constInteger;
504 /** Long. */
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.
556 * @since 2024/01/08
558 typedef struct sjme_class_fieldConstVal
560 /** The type of value. */
561 sjme_javaTypeId type;
563 /** The value of the field. */
564 union
566 /** The Java value. */
567 sjme_jvalue java;
569 /** Constant string. */
570 sjme_stringPool_string string;
571 } value;
572 } sjme_class_fieldConstVal;
574 struct sjme_class_fieldInfoCore
576 /** The class which contains this field. */
577 sjme_class_info inClass;
579 /** Field flags. */
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;
591 /** Method flags. */
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. */
604 sjme_jint maxLocals;
606 /** Maximum number of stack entries. */
607 sjme_jint maxStack;
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.
619 * @since 2024/01/09
621 typedef struct sjme_class_stackMap
623 /** Todo. */
624 int todo;
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.
635 * @since 2024/01/03
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.
671 * @since 2024/09/13
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 /*--------------------------------------------------------------------------*/
699 /* Anti-C++. */
700 #ifdef __cplusplus
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 */