PIPS
genC.h
Go to the documentation of this file.
1 /*
2 
3  $Id: genC.h 1383 2018-10-22 08:31:50Z coelho $
4 
5  Copyright 1989-2016 MINES ParisTech
6 
7  This file is part of NewGen.
8 
9  NewGen is free software: you can redistribute it and/or modify it under the
10  terms of the GNU General Public License as published by the Free Software
11  Foundation, either version 3 of the License, or any later version.
12 
13  NewGen is distributed in the hope that it will be useful, but WITHOUT ANY
14  WARRANTY; without even the implied warranty of MERCHANTABILITY or
15  FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
16  License for more details.
17 
18  You should have received a copy of the GNU General Public License along with
19  NewGen. If not, see <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #ifndef GENC_INCLUDED
24 #define GENC_INCLUDED
25 #define NEWGEN
26 
27 /*
28  * This is the include file to be used for the generation of C code.
29  */
30 /* #include <sys/stdtypes.h> */
31 #include <stdio.h>
32 #include <stdint.h>
33 #include <strings.h>
34 #include <stdarg.h>
35 
36 #include "newgen_assert.h"
37 
38 #include "newgen_types.h"
39 #include "newgen_set.h"
40 
41 /* The size of the management information inside each Newgen object
42  * (in gen_chunks)
43  */
44 
45 #define GEN_HEADER (1)
46 #define GEN_HEADER_SIZE (sizeof(gen_chunk)*GEN_HEADER)
47 
48 /* A gen_chunk is used to store every object. It has to be able to store,
49  * at least, a (CHUNK *) and every inlinable value. To use a union is a
50  * trick to enable the assignment opereator and the ability of passing and
51  * returning them as values (for function): this requires a sufficiently
52  * clever compiler ! * Note that the field name of inlinable types have
53  * to begin with the same letter as the type itself (this can be fixed if
54  * necessary but why bother). This characteristic is used by the Newgen
55  * code generator.
56  */
57 
58 typedef union gen_chunk {
60  bool b;
61  char c;
63  float f;
64  string s;
65  void * e; /**< For externals (foreign objects) */
66  struct cons * l; /**< A pointer to a list element */
67  set t;
69  union gen_chunk * p;
71 
72 typedef void *(gen_extract_func_t)(const gen_chunk);
73 
74 #define gen_chunk_undefined ((gen_chunk *)(-16))
75 #define gen_chunk_undefined_p(c) ((c)==gen_chunk_undefined)
76 
77 /* obsolete
78  */
79 #define chunk_undefined gen_chunk_undefined
80 #define chunk_undefined_p(c) gen_chunk_undefined_p(c)
81 
82 #define UNIT(x) "You don't want to take the value of a unit type, do you?"
83 #define BOOL(x) ((x).b)
84 #define CHAR(x) ((x).c)
85 #define INT(x) ((x).i)
86 #define FLOAT(x) ((x).f)
87 #define STRING(x) ((x).s)
88 #define CONSP(x) ((x).l)
89 #define SETP(x) ((x).t)
90 #define CHUNK(x) ((x).p)
91 #define HASH(x) ((x).h)
92 #define CHUNKP(x) ((x).p)
93 #define LIST(x) ((x).l)
94 #define SET(x) ((x).t)
95 #define VOID_STAR(x) ((x).e)
96 
97 /* for the MAP macro to handle simple types correctly. FC.
98  */
99 #define UNIT_TYPE "You don't want a unit type, do you?"
100 #define BOOL_TYPE bool
101 #define CHAR_TYPE char
102 #define INT_TYPE int
103 #define FLOAT_TYPE float
104 #define STRING_TYPE string
105 #define VOID_STAR_TYPE void *
106 
107 #define bool_TYPE bool
108 #define char_TYPE char
109 #define int_TYPE int
110 #define float_TYPE float
111 #define string_TYPE string
112 
113 #define BOOL_CAST(x) BOOL(x)
114 #define CHAR_CAST(x) CHAR(x)
115 #define INT_CAST(x) INT(x)
116 #define FLOAT_CAST(x) FLOAT(x)
117 #define STRING_CAST(x) STRING(x)
118 #define VOID_STAR_CAST(x) VOID_STAR(x)
119 
120 #define bool_CAST(x) BOOL(x)
121 #define char_CAST(x) CHAR(x)
122 #define int_CAST(x) INT(x)
123 #define float_CAST(x) FLOAT(x)
124 #define string_CAST(x) STRING(x)
125 
126 #define CONSP_TYPE list
127 #define LIST_TYPE list
128 #define SETP_TYPE set
129 #define SET_TYPE set
130 #define CHUNK_TYPE gen_chunkp
131 #define CHUNKP_TYPE gen_chunkp
132 #define HASH_TYPE hash_table
133 
134 #define consp_TYPE list
135 #define list_TYPE list
136 #define setp_TYPE set
137 #define set_TYPE set
138 #define chunk_TYPE gen_chunkp
139 #define chunkp_TYPE gen_chunkp
140 #define hash_TYPE hash_table
141 
142 #define CONSP_CAST(x) LIST(x)
143 #define LIST_CAST(x) LIST(x)
144 #define SETP_CAST(x) SET(x)
145 #define SET_CAST(x) SET(x)
146 #define CHUNK_CAST(x) CHUNK(x)
147 #define CHUNKP_CAST(x) CHUNKP(x)
148 #define HASH_CAST(x) HASH_TABLE(x)
149 
150 #define consp_CAST(x) LIST(x)
151 #define list_CAST(x) LIST(x)
152 #define setp_CAST(x) SET(x)
153 #define set_CAST(x) SET(x)
154 #define chunk_CAST(x) CHUNK(x)
155 #define chunkp_CAST(x) CHUNKP(x)
156 #define hash_CAST(x) HASH_TABLE(x)
157 
158 /* some other macros need the domain number to keep track of the type.
159  * they are provided here for the internal types.
160  */
162  unit_domain = 0, /**< Should start at 1 to be able to iterate on them with
163  gen_recurse() functions */
171 };
172 
173 /* utils for typed cons */
174 #define BOOL_NEWGEN_DOMAIN (bool_domain)
175 #define CHAR_NEWGEN_DOMAIN (char_domain)
176 #define INT_NEWGEN_DOMAIN (int_domain)
177 #define FLOAT_NEWGEN_DOMAIN (float_domain)
178 #define STRING_NEWGEN_DOMAIN (string_domain)
179 
180 #define bool_NEWGEN_DOMAIN (bool_domain)
181 #define char_NEWGEN_DOMAIN (char_domain)
182 #define int_NEWGEN_DOMAIN (int_domain)
183 #define float_NEWGEN_DOMAIN (float_domain)
184 #define string_NEWGEN_DOMAIN (string_domain)
185 
186 #define LIST_NEWGEN_DOMAIN (-1) /* means unknown type... */
187 
188 #include "newgen_list.h"
189 #include "newgen_stack.h"
190 #include "newgen_string_buffer.h"
191 #include "newgen_auto_string.h"
192 
193 /* Function interface for user applications. */
194 
195 extern int gen_debug ;
196 
197 #define GEN_DBG_TRAV_LEAF 1
198 #define GEN_DBG_TRAV_SIMPLE 2
199 #define GEN_DBG_TRAV_OBJECT 4
200 #define GEN_DBG_CHECK 8
201 #define GEN_DBG_RECURSE 16
202 
203 #define GEN_DBG_TRAV \
204  (GEN_DBG_TRAV_LEAF|GEN_DBG_TRAV_SIMPLE|GEN_DBG_TRAV_OBJECT)
205 
206 extern void gen_free GEN_PROTO(( gen_chunk * )) ;
207 extern int gen_free_tabulated GEN_PROTO(( int )) ;
208 extern void gen_write GEN_PROTO(( FILE *, gen_chunk * )) ;
209 extern int gen_write_tabulated GEN_PROTO(( FILE *, int )) ;
210 extern void gen_read_spec GEN_PROTO((char *, ...)) ;
211 extern gen_chunk *gen_read GEN_PROTO(( FILE * )) ;
212 extern int gen_read_tabulated GEN_PROTO(( FILE *, int )) ;
213 extern int gen_read_and_check_tabulated GEN_PROTO(( FILE *, int )) ;
214 extern gen_chunk *gen_make_array GEN_PROTO(( int )) ;
215 extern gen_chunk *gen_alloc GEN_PROTO((int, int, int, ...)) ;
216 extern char * alloc GEN_PROTO((int));
217 
218 /* exported type translation functions. */
222 extern void gen_type_translation_default GEN_PROTO((void));
223 extern void gen_type_translation_read GEN_PROTO((string));
224 extern void gen_type_translation_write GEN_PROTO((string));
225 
226 extern void gen_init_external GEN_PROTO((int,
227  void*(*)(FILE*, int(*)(void)),
228  void (*)(FILE*, void*),
229  void (*)(void*),
230  void* (*)(void*),
231  int (*)(void*))) ;
232 extern gen_chunk *gen_check GEN_PROTO(( gen_chunk *, int )) ;
234 extern int gen_type GEN_PROTO((gen_chunk *)) ;
235 extern string gen_domain_name GEN_PROTO((int)) ;
239 extern int gen_consistent_p GEN_PROTO(( gen_chunk * )) ;
240 extern int gen_tabulated_consistent_p GEN_PROTO((int));
242 extern int gen_defined_p GEN_PROTO((gen_chunk *));
243 
244 /* recursion and utilities
245  */
246 extern bool gen_true GEN_PROTO((gen_chunk *)) ;
247 extern bool gen_true2 GEN_PROTO((gen_chunk *, void *)) ;
248 extern bool gen_false GEN_PROTO((gen_chunk *)) ;
249 extern bool gen_false2 GEN_PROTO((gen_chunk *, void *)) ;
250 extern void gen_null GEN_PROTO((void *)) ;
251 extern void gen_null2 GEN_PROTO((void *, void *)) ;
252 extern void * gen_NULL GEN_PROTO((void *)) ;
253 extern void * gen_NULL2 GEN_PROTO((void *, void *)) ;
254 extern void gen_core GEN_PROTO((void *)) ;
255 extern void * gen_core_NULL GEN_PROTO((void *)) ;
256 
257 extern void gen_recurse_stop GEN_PROTO((void *));
258 extern void gen_multi_recurse GEN_PROTO((void *, ...));
259 extern void gen_context_multi_recurse GEN_PROTO((void *, void *,...));
260 extern void gen_full_recurse GEN_PROTO((void *, void *, ...));
261 extern void gen_recurse(void * start, int domain_number,
262  bool (*flt)(void *),
263  void (*rwt)(void *));
264 extern void gen_context_recurse(void * start, void * context, int domain_number,
265  bool (*flt)(void *, void * context),
266  void (*rwt)(void *, void * context));
267 
270 extern gen_chunk * gen_get_recurse_ancestor(const void *);
271 extern gen_chunk * gen_get_ancestor(int, const void *);
272 extern gen_chunk * gen_get_current_object(void);
273 extern gen_chunk * gen_get_current_ancestor(int);
274 
275 // compatibility with previous version:
276 #define gen_get_ancestor_type(i,o) gen_get_ancestor(i,o)
277 
278 // temporary fix on removed functions with empty macros
279 #define gen_start_recurse_ancestor_tracking() /* NOPE */
280 #define gen_stop_recurse_ancestor_tracking() /* NOPE */
281 
282 // fix gcc warning concerning gen_recurse and gen_context_recurse
283 #define gen_recurse(start,domain_number,flt,rwt)\
284  gen_recurse((start),(domain_number),((bool (*)(void*))(flt)),((void (*)(void*))(rwt)))
285 #define gen_context_recurse(start,ctxt,domain_number,flt,rwt)\
286  gen_context_recurse((start),(ctxt),(domain_number),((bool (*)(void*,void*))(flt)),((void (*)(void*,void*))(rwt)))
287 
288 /* Since C is not-orthogonal (chunk1 == chunk2 is prohibited),
289  * this one is needed.
290  */
291 
292 #ifndef MEMORY_INCLUDED
293 #include <memory.h>
294 #define MEMORY_INCLUDED
295 #endif
296 
297 #define gen_equal(lhs,rhs) (memcmp((lhs),(rhs))==0)
298 
299 /* GEN_CHECK can be used to test run-time coherence of Newgen values.
300  */
301 #ifdef GEN_CHECK
302 #undef GEN_CHECK
303 #define GEN_CHECK(e,t) (gen_check((e),(t)),e)
304 #define GEN_CHECK_ALLOC 1
305 #else
306 #define GEN_CHECK(e,t) (e)
307 #define GEN_CHECK_ALLOC 0
308 #endif
309 
310 /* this macro does about the same as gen_check, but inlined and safer.
311  * the item *must* be some newgen allocated structure.
312  */
313 #define NEWGEN_CHECK_TYPE(dom, item) \
314  { \
315  _int __type = dom, __itype; \
316  void * __item = (void *) item; \
317  message_assert("valid required domaine number", \
318  __type>0 && __type<MAX_DOMAIN); \
319  if (Domains[__type].domain && \
320  Domains[__type].domain->co.type==CONSTRUCTED_DT) { \
321  message_assert("some item", __item!=NULL); \
322  message_assert("item is defined", __item!=gen_chunk_undefined); \
323  __itype = ((gen_chunk*) __item)->i; \
324  if (__itype!=__type) { \
325  message_assert("valid item domain number", \
326  __itype>0 && __itype<MAX_DOMAIN); \
327  fprintf(stderr, "type error: expecting %s, got %s\n", \
328  Domains[__type].name, Domains[__itype].name); \
329  message_assert("check type", __itype==__type); \
330  } \
331  } \
332  /* else should I say something? Hmmm... */ \
333  }
334 
335 /* for debug */
336 extern int current_shared_obj_table_size(void);
337 
338 #include "newgen_map.h"
339 #include "newgen_array.h"
340 
341 #include "newgen_generic_mapping.h"
342 #include "newgen_generic_stack.h"
343 #include "newgen_generic_function.h"
344 
345 #endif
char * alloc(int size)
ALLOC is an "iron-clad" version of malloc(3).
Definition: build.c:501
int current_shared_obj_table_size(void)
for debug
Definition: genClib.c:654
void gen_free GEN_PROTO((gen_chunk *))
recursion and utilities
int gen_debug
Function interface for user applications.
Definition: genClib.c:69
#define gen_context_recurse(start, ctxt, domain_number, flt, rwt)
Definition: genC.h:285
union gen_chunk gen_chunk
A gen_chunk is used to store every object.
internal_type
some other macros need the domain number to keep track of the type.
Definition: genC.h:161
@ _int_domain
Definition: genC.h:168
@ int_domain
Definition: genC.h:166
@ unit_domain
Should start at 1 to be able to iterate on them with gen_recurse() functions.
Definition: genC.h:162
@ float_domain
Definition: genC.h:169
@ bool_domain
Definition: genC.h:164
@ intptr_t_domain
Definition: genC.h:167
@ char_domain
Definition: genC.h:165
@ string_domain
Definition: genC.h:170
void *() gen_extract_func_t(const gen_chunk)
Definition: genC.h:72
#define gen_recurse(start, domain_number, flt, rwt)
Definition: genC.h:283
union gen_chunk * gen_chunkp
void gen_free(gen_chunk *obj)
version without shared_pointers.
Definition: genClib.c:992
gen_chunk * gen_alloc(int size, int gen_check_p, int dom,...)
allocates something in newgen.
Definition: genClib.c:298
int gen_write_tabulated(FILE *fd, int domain)
GEN_WRITE_TABULATED writes the tabulated object TABLE on FD.
Definition: genClib.c:1866
string gen_domain_name(int t)
GEN_DOMAIN_NAME returns the domain name, and may be used for debug purposes.
Definition: genClib.c:97
int gen_type_translation_old_to_actual(int n)
forwards conversion
Definition: genClib.c:2147
void gen_type_translation_reset(void)
Definition: genClib.c:2171
int gen_read_tabulated(FILE *file, int create_p)
GEN_READ_TABULATED reads FILE to update the Gen_tabulated_ table.
Definition: genClib.c:2334
void gen_type_translation_write(string filename)
Definition: genClib.c:2195
int gen_consistent_p(gen_chunk *obj)
GEN_CONSISTENT_P dynamically checks the type correctness of OBJ.
Definition: genClib.c:2398
bool gen_sharing_p(gen_chunk *obj1, gen_chunk *obj2)
Definition: genClib.c:2518
int gen_defined_p(gen_chunk *obj)
Definition: genClib.c:2438
gen_chunk * gen_copy_tree_with_sharing(gen_chunk *obj)
for re-entry only in gen_copy_tree...
Definition: genClib.c:1442
gen_chunk * gen_check(gen_chunk *obj, int t)
GEN_CHECK checks that the gen_chunk received OBJ is of the appropriate TYPE.
Definition: genClib.c:2356
int gen_free_tabulated(int domain)
free tabulated elements of this domain.
Definition: genClib.c:1461
gen_chunk * gen_make_array(num)
GEN_MAKE_ARRAY allocates an initialized array of NUM gen_chunks.
Definition: genClib.c:2307
void gen_init_external(int which, void *(*read)(FILE *, int(*)(void)), void(*write)(FILE *, void *), void(*free)(void *), void *(*copy)(void *), int(*allocated_memory)(void *))
GEN_INIT_EXTERNAL defines entry points for free, read and write functions of external types.
Definition: genClib.c:2276
int gen_type(gen_chunk *obj)
GEN_TYPE returns the domain number for the object in argument.
Definition: genClib.c:82
gen_chunk * gen_read(FILE *file)
GEN_READ reads any object from the FILE stream.
Definition: genClib.c:2323
int gen_type_translation_actual_to_old(int n)
backwards conversion
Definition: genClib.c:2160
void gen_read_spec(char *spec,...)
Definition: genClib.c:2218
gen_chunk * gen_copy_tree(gen_chunk *obj)
Definition: genClib.c:1429
int gen_allocated_memory(gen_chunk *obj)
re-entry is automatic for this function.
Definition: genClib.c:2690
int gen_tabulated_consistent_p(int domain)
Definition: genClib.c:2420
void gen_type_translation_default(void)
Definition: genClib.c:2180
void gen_write(FILE *fd, gen_chunk *obj)
GEN_WRITE writes the OBJect on the stream FD.
Definition: genClib.c:1745
void gen_type_translation_read(string filename)
set current type translation table according to file
Definition: genClib.c:2189
static char start[1024]
The name of the variable from which to start counting domain numbers.
Definition: genLisp.c:55
void gen_recurse_stop(void *obj)
Tells the recursion not to go in this object.
Definition: genClib.c:3251
void gen_full_recurse(void *o, void *context,...)
Full multi-recursion with context function visitor.
Definition: genClib.c:3402
void gen_multi_recurse(void *o,...)
Multi recursion visitor function.
Definition: genClib.c:3428
void gen_context_multi_recurse(void *o, void *context,...)
Multi-recursion with context function visitor.
Definition: genClib.c:3373
gen_chunk * gen_get_recurse_previous_visited_object(void)
Get the previously visited object.
Definition: genClib.c:3513
gen_chunk * gen_get_current_ancestor(int)
Return current object of that type...
Definition: genClib.c:3587
gen_chunk * gen_get_current_object(void)
return the current visited object, or NULL if not in a recursion.
Definition: genClib.c:3580
gen_chunk * gen_get_ancestor(int, const void *)
return the first ancestor object found of the given type.
Definition: genClib.c:3560
gen_chunk * gen_get_recurse_ancestor(const void *)
Get the first ancestor object encountered during the recursion for the given object.
Definition: genClib.c:3546
gen_chunk * gen_get_recurse_current_ancestor(void)
Get the ancestor of the current object.
Definition: genClib.c:3526
void * gen_NULL(__attribute__((unused)) void *unused)
idem with a void* return
Definition: genClib.c:2764
void gen_null2(__attribute__((unused)) void *u1, __attribute__((unused)) void *u2)
idem with 2 args, to please overpeaky compiler checks
Definition: genClib.c:2758
void * gen_core_NULL(__attribute__((unused)) void *p)
Definition: genClib.c:2827
bool gen_false(__attribute__((unused)) gen_chunk *unused)
Return false and ignore the argument.
Definition: genClib.c:2796
void gen_core(__attribute__((unused)) void *p)
Abort when called.
Definition: genClib.c:2822
void * gen_NULL2(__attribute__((unused)) void *u1, __attribute__((unused)) void *u2)
idem with 2 args, to please overpeaky compiler checks
Definition: genClib.c:2770
bool gen_true2(__attribute__((unused)) gen_chunk *u1, __attribute__((unused)) void *u2)
Definition: genClib.c:2785
void gen_null(__attribute__((unused)) void *unused)
Ignore the argument.
Definition: genClib.c:2752
bool gen_false2(__attribute__((unused)) gen_chunk *u1, __attribute__((unused)) void *u2)
Definition: genClib.c:2801
bool gen_true(__attribute__((unused)) gen_chunk *unused)
Return true and ignore the argument.
Definition: genClib.c:2780
intptr_t _int
_INT
Definition: newgen_types.h:53
int unit
UNIT.
Definition: newgen_types.h:97
static void rwt(call c)
Definition: run-time.c:332
FI: I do not understand why the type is duplicated at the set level.
Definition: set.c:59
The structure used to build lists in NewGen.
Definition: newgen_list.h:41
Definition: delay.c:253
void gen_clear_tabulated_element(gen_chunk *obj)
GEN_CLEAR_TABULATED_ELEMENT only clears the entry for object OBJ in the gen_tabulated_ and gen_tabula...
Definition: tabulated.c:251
int gen_read_and_check_tabulated(FILE *file, int create_p)
Definition: tabulated.c:239
A gen_chunk is used to store every object.
Definition: genC.h:58
void * e
For externals (foreign objects)
Definition: genC.h:65
hash_table h
Definition: genC.h:68
float f
Definition: genC.h:63
unit u
Definition: genC.h:59
_int i
Definition: genC.h:62
char c
Definition: genC.h:61
struct cons * l
A pointer to a list element.
Definition: genC.h:66
set t
Definition: genC.h:67
bool b
Definition: genC.h:60
union gen_chunk * p
Definition: genC.h:69
string s
Definition: genC.h:64