fixes to strings in generic usuage
[official-gcc.git] / gcc / python / py-types.c
blob40226fc5cc4d2241b2112a34d1250967d1b4753a
1 /* This file is part of GCC.
3 GCC is free software; you can redistribute it and/or modify it under
4 the terms of the GNU General Public License as published by the Free
5 Software Foundation; either version 3, or (at your option) any later
6 version.
8 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
9 WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
11 for more details.
13 You should have received a copy of the GNU General Public License
14 along with GCC; see the file COPYING3. If not see
15 <http://www.gnu.org/licenses/>. */
17 #include "config.h"
18 #include "system.h"
19 #include "ansidecl.h"
20 #include "coretypes.h"
21 #include "opts.h"
22 #include "tree.h"
23 #include "gimple.h"
24 #include "toplev.h"
25 #include "debug.h"
26 #include "options.h"
27 #include "flags.h"
28 #include "convert.h"
29 #include "diagnostic-core.h"
30 #include "langhooks.h"
31 #include "langhooks-def.h"
32 #include "target.h"
34 #include <gmp.h>
35 #include <mpfr.h>
37 #include "vec.h"
38 #include "hashtab.h"
40 #include "gpython.h"
41 #include "py-dot-codes.def"
42 #include "py-dot.h"
43 #include "py-vec.h"
44 #include "py-tree.h"
45 #include "py-types.h"
46 #include "py-runtime.h"
48 VEC(tree,gc) * gpy_builtin_types_vec;
50 /* @see coverage.c build_fn_info_type ( unsigned int )
51 for more examples on RECORD_TYPE's
53 Better still @see fortran/trans-types.c - gfc_get_desc_dim_type
55 Or even better @see go/types.cc - 2027
57 typedef gpy_object_state_t * (*__callable)( void );
59 typedef struct gpy_callable_def_t {
60 char * ident; int n_args;
61 bool class; __callable call;
62 } gpy_callable_def_t;
65 /* go/gofrontend/types.cc 4517 && 5066 for making an array type */
69 typedef struct gpy_rr_object_state_t {
70 char * obj_t_ident;
71 signed long ref_count;
72 void * self;
73 struct gpy_typedef_t * definition;
74 } gpy_object_state_t ;
76 typedef struct gpy_object_t {
77 enum GPY_OBJECT_T T;
78 union{
79 gpy_object_state_t * object_state;
80 gpy_literal_t * literal;
81 } o ;
82 } gpy_object_t ;
85 static
86 tree gpy_build_py_object_type (void)
88 tree object_state_struct_Type = make_node (RECORD_TYPE);
90 tree name = get_identifier("obj_t_ident");
91 tree field = build_decl(BUILTINS_LOCATION, FIELD_DECL, name,
92 build_pointer_type(char_type_node));
93 DECL_CONTEXT(field) = object_state_struct_Type;
94 TYPE_FIELDS(object_state_struct_Type) = field;
95 tree last_field = field;
97 name = get_identifier("ref_count");
98 field = build_decl(BUILTINS_LOCATION, FIELD_DECL, name, integer_type_node);
99 DECL_CONTEXT(field) = object_state_struct_Type;
100 DECL_CHAIN(last_field) = field;
101 last_field = field;
103 name = get_identifier("self");
104 field = build_decl(BUILTINS_LOCATION, FIELD_DECL, name,
105 build_pointer_type (void_type_node));
106 DECL_CONTEXT(field) = object_state_struct_Type;
107 DECL_CHAIN(last_field) = field;
108 last_field = field;
110 name = get_identifier("definition");
111 field = build_decl(BUILTINS_LOCATION, FIELD_DECL, name,
112 build_pointer_type (void_type_node));
113 DECL_CONTEXT(field) = object_state_struct_Type;
114 DECL_CHAIN(last_field) = field;
115 last_field = field;
117 layout_type(object_state_struct_Type);
119 // Give the struct a name for better debugging info.
120 name = get_identifier("gpy_object_state_t");
121 tree object_state_type_decl = build_decl(BUILTINS_LOCATION, TYPE_DECL, name,
122 object_state_struct_Type);
123 DECL_ARTIFICIAL(object_state_type_decl) = 1;
124 TYPE_NAME(object_state_struct_Type) = object_state_type_decl;
125 gpy_preserve_from_gc(object_state_type_decl);
126 rest_of_decl_compilation(object_state_type_decl, 1, 0);
128 debug_tree (object_state_type_decl);
129 tree object_state_ptr_type = build_pointer_type (object_state_struct_Type);
130 gpy_preserve_from_gc (object_state_ptr_type);
132 //....................
134 tree union_type__ = make_node (UNION_TYPE);
136 name = get_identifier ("object_state");
137 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, name,
138 object_state_ptr_type);
139 DECL_CONTEXT(field) = union_type__;
140 DECL_CHAIN(last_field) = field;
141 last_field = field;
143 name = get_identifier ("literal");
144 field = build_decl (BUILTINS_LOCATION, FIELD_DECL, name,
145 ptr_type_node);
146 DECL_CONTEXT(field) = union_type__;
147 DECL_CHAIN(last_field) = field;
148 last_field = field;
150 layout_type (union_type__);
152 name = get_identifier("o");
153 tree union_type_decl = build_decl(BUILTINS_LOCATION, TYPE_DECL, name,
154 union_type__);
155 DECL_ARTIFICIAL(union_type_decl) = 1;
156 TYPE_NAME(union_type__) = union_type_decl;
157 gpy_preserve_from_gc(union_type_decl);
158 rest_of_decl_compilation(union_type_decl, 1, 0);
160 //.........................
162 tree gpy_object_struct_Type = make_node (RECORD_TYPE);
164 name = get_identifier("type");
165 field = build_decl(BUILTINS_LOCATION, FIELD_DECL, name, integer_type_node);
166 DECL_CONTEXT(field) = gpy_object_struct_Type;
167 DECL_CHAIN(last_field) = field;
168 last_field = field;
170 name = get_identifier("o");
171 field = build_decl(BUILTINS_LOCATION, FIELD_DECL, name,
172 union_type_decl);
173 DECL_CONTEXT(field) = gpy_object_struct_Type;
174 DECL_CHAIN(last_field) = field;
175 last_field = field;
177 layout_type (object_state_struct_Type);
179 name = get_identifier("gpy_object_t");
180 tree gpy_object_type_decl = build_decl(BUILTINS_LOCATION, TYPE_DECL, name,
181 gpy_object_struct_Type);
182 DECL_ARTIFICIAL(gpy_object_type_decl) = 1;
183 TYPE_NAME(gpy_object_struct_Type) = name;
184 gpy_preserve_from_gc(gpy_object_type_decl);
185 rest_of_decl_compilation(gpy_object_type_decl, 1, 0);
187 return build_pointer_type (gpy_object_struct_Type);
191 typedef gpy_object_t * (*gpy_std_callable)
192 (gpy_object_t **);
194 typedef gpy_callable__t {
195 char * ident;
196 gpy_std_callable call;
197 int n;
198 } gpy_callable_t ;
200 static
201 tree gpy_build_callable_record_type (void)
203 tree callable_struct_Type = make_node (RECORD_TYPE);
205 tree name = get_identifier("ident");
206 tree field = build_decl(BUILTINS_LOCATION, FIELD_DECL, name,
207 build_pointer_type(char_type_node));
208 DECL_CONTEXT(field) = callable_struct_Type;
209 TYPE_FIELDS(callable_struct_Type) = field;
210 tree last_field = field;
212 name = get_identifier("call");
213 field = build_decl(BUILTINS_LOCATION, FIELD_DECL, name, ptr_type_node);
214 DECL_CONTEXT(field) = callable_struct_Type;
215 DECL_CHAIN(last_field) = field;
216 last_field = field;
218 name = get_identifier("n");
219 field = build_decl(BUILTINS_LOCATION, FIELD_DECL, name,
220 integer_type_node);
221 DECL_CONTEXT(field) = callable_struct_Type;
222 DECL_CHAIN(last_field) = field;
223 last_field = field;
225 layout_type(callable_struct_Type);
227 name = get_identifier("gpy_callable_t");
228 tree gpy_callable_type_decl = build_decl(BUILTINS_LOCATION, TYPE_DECL, name,
229 callable_struct_Type);
230 DECL_ARTIFICIAL(gpy_callable_type_decl) = 1;
231 TYPE_NAME(callable_struct_Type) = name;
232 gpy_preserve_from_gc(gpy_callable_type_decl);
233 rest_of_decl_compilation(gpy_callable_type_decl, 1, 0);
235 return callable_struct_Type;
238 static
239 tree gpy_build_callable_record_type_ptr (void)
241 tree callable_struct_Type = make_node (RECORD_TYPE);
243 tree const_char_type = build_qualified_type(unsigned_char_type_node,
244 TYPE_QUAL_CONST);
245 tree ctype = build_pointer_type(const_char_type);
246 gpy_preserve_from_gc(ctype);
248 tree name = get_identifier("ident");
249 tree field = build_decl(BUILTINS_LOCATION, FIELD_DECL, name,
250 ctype);
251 DECL_CONTEXT(field) = callable_struct_Type;
252 TYPE_FIELDS(callable_struct_Type) = field;
253 tree last_field = field;
255 name = get_identifier("call");
256 field = build_decl(BUILTINS_LOCATION, FIELD_DECL, name, ptr_type_node);
257 DECL_CONTEXT(field) = callable_struct_Type;
258 DECL_CHAIN(last_field) = field;
259 last_field = field;
261 name = get_identifier("n");
262 field = build_decl(BUILTINS_LOCATION, FIELD_DECL, name,
263 integer_type_node);
264 DECL_CONTEXT(field) = callable_struct_Type;
265 DECL_CHAIN(last_field) = field;
266 last_field = field;
268 layout_type(callable_struct_Type);
270 name = get_identifier("gpy_callable_t");
271 tree gpy_callable_type_decl = build_decl(BUILTINS_LOCATION, TYPE_DECL, name,
272 callable_struct_Type);
273 DECL_ARTIFICIAL(gpy_callable_type_decl) = 1;
274 TYPE_NAME(callable_struct_Type) = name;
275 gpy_preserve_from_gc(gpy_callable_type_decl);
276 rest_of_decl_compilation(gpy_callable_type_decl, 1, 0);
278 return build_pointer_type (callable_struct_Type);
281 tree gpy_init_callable_record (tree id, int n, tree decl)
283 tree struct_tree = gpy_callable_type;
285 VEC(constructor_elt,gc)* init = VEC_alloc(constructor_elt, gc, 3);
287 constructor_elt* elt = VEC_quick_push(constructor_elt, init, NULL);
288 tree field = TYPE_FIELDS(struct_tree);
289 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "ident") == 0);
290 elt->index = field;
291 if (id == NULL_TREE)
292 elt->value = build_string(strlen("__null"),
293 "__null");
294 else
295 elt->value = build_string(strlen(IDENTIFIER_POINTER(id)),
296 IDENTIFIER_POINTER(id));
298 if (decl != NULL_TREE)
300 elt = VEC_quick_push(constructor_elt, init, NULL);
301 field = DECL_CHAIN(field);
302 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "call") == 0);
303 elt->index = field;
304 elt->value = fold_convert_loc(BUILTINS_LOCATION, TREE_TYPE(field),
305 build_fold_addr_expr (decl));
307 else
309 elt = VEC_quick_push(constructor_elt, init, NULL);
310 field = DECL_CHAIN(field);
311 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "call") == 0);
312 elt->index = field;
313 elt->value = fold_convert_loc(BUILTINS_LOCATION, TREE_TYPE(field),
314 build_int_cst (integer_type_node,0));
317 elt = VEC_quick_push(constructor_elt, init, NULL);
318 field = DECL_CHAIN(field);
319 gcc_assert(strcmp(IDENTIFIER_POINTER(DECL_NAME(field)), "n") == 0);
320 elt->index = field;
321 elt->value = fold_convert_loc(BUILTINS_LOCATION, TREE_TYPE(field),
322 build_int_cst (integer_type_node,n));
324 tree constructor = build_constructor(struct_tree, init);
325 TREE_CONSTANT(constructor) = 1;
327 return constructor;
330 void gpy_initilize_types (void)
332 gpy_builtin_types_vec = VEC_alloc(tree,gc,0);
334 tree const_char_type = build_qualified_type(unsigned_char_type_node,
335 TYPE_QUAL_CONST);
336 tree ctype = build_pointer_type(const_char_type);
337 gpy_preserve_from_gc(ctype);
339 VEC_safe_push (tree,gc,gpy_builtin_types_vec,
340 gpy_build_py_object_type ());
342 VEC_safe_push (tree,gc,gpy_builtin_types_vec,
343 build_pointer_type (gpy_object_type_ptr));
345 VEC_safe_push (tree,gc,gpy_builtin_types_vec,
346 gpy_build_callable_record_type ());
348 VEC_safe_push (tree,gc,gpy_builtin_types_vec,
349 gpy_build_callable_record_type_ptr ());
351 VEC_safe_push (tree,gc,gpy_builtin_types_vec,ctype);