]> git.xonotic.org Git - xonotic/gmqcc.git/blob - astir.h
GMQCC_WARN macro
[xonotic/gmqcc.git] / astir.h
1 /*
2  * Copyright (C) 2012 
3  *     Wolfgang Bumiller
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a copy of
6  * this software and associated documentation files (the "Software"), to deal in
7  * the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9  * of the Software, and to permit persons to whom the Software is furnished to do
10  * so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in all
13  * copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  */
23 #ifndef ASTIR_COMMON_H__
24 #define ASTIR_COMMON_H__
25
26 #define MEM_VECTOR_PROTO(Towner, Tmem, mem)        \
27     bool Towner##_##mem##_add(Towner*, Tmem);      \
28     bool Towner##_##mem##_remove(Towner*, size_t);
29
30 #define MEM_VECTOR_PROTO_ALL(Towner, Tmem, mem)          \
31     MEM_VECTOR_PROTO(Towner, Tmem, mem)          \
32     bool Towner##_##mem##_find(Towner*, Tmem, size_t*); \
33     void Towner##_##mem##_clear(Towner*);
34
35 #define MEM_VECTOR_MAKE(Twhat, name) \
36     Twhat  *name;                    \
37     size_t name##_count;             \
38     size_t name##_alloc
39
40 #define _MEM_VEC_FUN_ADD(Tself, Twhat, mem)                          \
41 bool Tself##_##mem##_add(Tself *self, Twhat f)                       \
42 {                                                                    \
43     Twhat *reall;                                                    \
44     if (self->mem##_count == self->mem##_alloc) {                    \
45         if (!self->mem##_alloc)                                      \
46             self->mem##_alloc = 16;                                  \
47         else                                                         \
48             self->mem##_alloc *= 2;                                  \
49         reall = (Twhat*)mem_a(sizeof(Twhat) * self->mem##_alloc);    \
50         if (!reall) {                                                \
51             MEM_VECTOR_CLEAR(self, mem);                             \
52             return false;                                            \
53         }                                                            \
54         memcpy(reall, self->mem, sizeof(Twhat) * self->mem##_count); \
55         mem_d(self->mem);                                            \
56         self->mem = reall;                                           \
57     }                                                                \
58     self->mem[self->mem##_count++] = f;                              \
59     return true;                                                     \
60 }
61
62 #define _MEM_VEC_FUN_REMOVE(Tself, Twhat, mem)                       \
63 bool Tself##_##mem##_remove(Tself *self, size_t idx)                 \
64 {                                                                    \
65     size_t i;                                                        \
66     Twhat *reall;                                                    \
67     if (idx >= self->mem##_count)                                    \
68         return true; /* huh... */                                    \
69     for (i = idx; i < self->mem##_count-1; ++i)                      \
70         self->mem[i] = self->mem[i+1];                               \
71     self->mem##_count--;                                             \
72     if (self->mem##_count < self->mem##_count/2)                     \
73     {                                                                \
74         self->mem##_alloc /= 2;                                      \
75         reall = (Twhat*)mem_a(sizeof(Twhat) * self->mem##_count);    \
76         if (!reall) {                                                \
77             MEM_VECTOR_CLEAR(self, mem);                             \
78             return false;                                            \
79         }                                                            \
80         memcpy(reall, self->mem, sizeof(Twhat) * self->mem##_count); \
81         mem_d(self->mem);                                            \
82         self->mem = reall;                                           \
83     }                                                                \
84     return true;                                                     \
85 }
86
87 #define _MEM_VEC_FUN_FIND(Tself, Twhat, mem)                    \
88 bool Tself##_##mem##_find(Tself *self, Twhat obj, size_t *idx) \
89 {                                                               \
90     size_t i;                                                   \
91     for (i = 0; i < self->mem##_count; ++i) {                   \
92         if (self->mem[i] == obj) {                              \
93             if (idx)                                            \
94                 *idx = i;                                       \
95             return true;                                        \
96         }                                                       \
97     }                                                           \
98     return false;                                               \
99 }
100
101 #define _MEM_VEC_FUN_CLEAR(Tself, mem)  \
102 void Tself##_##mem##_clear(Tself *self) \
103 {                                       \
104     if (!self->mem)                     \
105         return;                         \
106     free((void*) self->mem);            \
107     self->mem = NULL;                   \
108     self->mem##_count = 0;              \
109     self->mem##_alloc = 0;              \
110 }
111
112 #define MEM_VECTOR_CLEAR(owner, mem) \
113     if ((owner)->mem)                \
114         free((void*)((owner)->mem)); \
115     (owner)->mem = NULL;             \
116     (owner)->mem##_count = 0;        \
117     (owner)->mem##_alloc = 0
118
119 #define MEM_VECTOR_INIT(owner, mem) \
120 {                                   \
121     (owner)->mem = NULL;            \
122     (owner)->mem##_count = 0;       \
123     (owner)->mem##_alloc = 0;       \
124 }
125
126 #define MEM_VEC_FUNCTIONS(Tself, Twhat, mem) \
127 _MEM_VEC_FUN_REMOVE(Tself, Twhat, mem)       \
128 _MEM_VEC_FUN_ADD(Tself, Twhat, mem)
129
130 #define MEM_VEC_FUNCTIONS_ALL(Tself, Twhat, mem) \
131 MEM_VEC_FUNCTIONS(Tself, Twhat, mem)             \
132 _MEM_VEC_FUN_CLEAR(Tself, mem)                   \
133 _MEM_VEC_FUN_FIND(Tself, Twhat, mem)
134
135 enum qc_types {
136     /* Main QC types */
137     qc_void,
138     qc_float,
139     qc_vector,
140     qc_entity,
141     qc_string,
142
143     qc_int,
144
145     /* "virtual" and internal types */
146     qc_pointer,
147     qc_variant, /* eg. OFS_RETURN/PARAM... */
148     qc_function,
149 };
150
151 enum store_types {
152     store_global,
153     store_local,  /* local, assignable for now, should get promoted later */
154     store_value,  /* unassignable */
155 };
156
157 typedef struct {
158     float x, y, z;
159 } vector_t;
160
161 /* A shallow copy of a lex_file to remember where which ast node
162  * came from.
163  */
164 typedef struct lex_ctx
165 {
166     const char *file;
167     size_t     line;
168 } lex_ctx_t;
169
170 #endif