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