c_src: update
[nit.git] / c_src / nit.common.h
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include "gc_chooser.h"
5 #ifdef ANDROID
6 #include <android/log.h>
7 #define PRINT_ERROR(...) (void)__android_log_print(ANDROID_LOG_WARN, "Nit", __VA_ARGS__)
8 #else
9 #define PRINT_ERROR(...) fprintf(stderr, __VA_ARGS__)
10 #endif
11 typedef void(*nitmethod_t)(void); /* general C type representing a Nit method. */
12 typedef union {
13 void* val;
14 long l;
15 short int s;
16 char c;
17 double d;
18 char* str;
19 void* ptr;
20 } nitattribute_t; /* general C type representing a Nit attribute. */
21 struct class { int box_kind; nitmethod_t vft[]; }; /* general C type representing a Nit class. */
22 struct type { int id; const char *name; int color; short int is_nullable; const struct types *resolution_table; int table_size; int type_table[]; }; /* general C type representing a Nit type. */
23 struct instance { const struct type *type; const struct class *class; nitattribute_t attrs[]; }; /* general C type representing a Nit instance. */
24 struct types { int dummy; const struct type *types[]; }; /* a list types (used for vts, fts and unresolved lists). */
25 typedef struct instance val; /* general C type representing a Nit instance. */
26 struct nitni_instance {
27 struct nitni_instance *next,
28 *prev; /* adjacent global references in global list */
29 int count; /* number of time this global reference has been marked */
30 struct instance *value;
31 };
32
33 /* Native reference to Nit objects */
34 /* This structure is used to represent every Nit type in extern methods and custom C code. */
35 struct nitni_ref {
36 struct nitni_ref *next,
37 *prev; /* adjacent global references in global list */
38 int count; /* number of time this global reference has been marked */
39 };
40
41 /* List of global references from C code to Nit objects */
42 /* Instanciated empty at init of Nit system and filled explicitly by user in C code */
43 struct nitni_global_ref_list_t {
44 struct nitni_ref *head, *tail;
45 };
46 extern struct nitni_global_ref_list_t *nitni_global_ref_list;
47
48 /* Initializer of global reference list */
49 extern void nitni_global_ref_list_init();
50
51 /* Intern function to add a global reference to the list */
52 extern void nitni_global_ref_add( struct nitni_ref *ref );
53
54 /* Intern function to remove a global reference from the list */
55 extern void nitni_global_ref_remove( struct nitni_ref *ref );
56
57 /* Increase count on an existing global reference */
58 extern void nitni_global_ref_incr( struct nitni_ref *ref );
59
60 /* Decrease count on an existing global reference */
61 extern void nitni_global_ref_decr( struct nitni_ref *ref );
62
63 void show_backtrace(int) __attribute__ ((noreturn));
64 #define likely(x) __builtin_expect((x),1)
65 #define unlikely(x) __builtin_expect((x),0)
66 extern int glob_argc;
67 extern char **glob_argv;
68 extern val *glob_sys;
69 struct instance_kernel__Bool {
70 const struct type *type;
71 const struct class *class;
72 short int value;
73 };
74 struct instance_kernel__Float {
75 const struct type *type;
76 const struct class *class;
77 double value;
78 };
79 struct instance_kernel__Int {
80 const struct type *type;
81 const struct class *class;
82 long value;
83 };
84 struct instance_kernel__Char {
85 const struct type *type;
86 const struct class *class;
87 char value;
88 };
89 struct instance_kernel__Pointer {
90 const struct type *type;
91 const struct class *class;
92 void* value;
93 };
94 struct instance_array__NativeArray {
95 const struct type *type;
96 const struct class *class;
97 int length;
98 val* values[0];
99 };