]> rtime.felk.cvut.cz Git - orte.git/blob - orte/idl-compiler/orte-idl-c-utils.c
10b36c93ddb138444a3ac4a62a00b072cf092ec3
[orte.git] / orte / idl-compiler / orte-idl-c-utils.c
1 //#include "config.h"
2
3 #include "orte-idl-c-backend.h"
4
5 #include <string.h>
6
7 char *
8 orte_cbe_get_typecode_name (IDL_tree tree)
9 {
10         if (!tree)
11                 return g_strdup ("TC_FIXME");
12         else
13                 return g_strconcat ("TC_", orte_cbe_get_typespec_str (tree), NULL);
14 }
15
16 gboolean
17 orte_cbe_type_is_builtin(IDL_tree tree)
18
19   return FALSE;
20   switch(IDL_NODE_TYPE(tree)) {
21   case IDLN_LIST:
22   case IDLN_GENTREE:
23   case IDLN_MEMBER:
24   case IDLN_NATIVE:
25   case IDLN_CASE_STMT:
26   case IDLN_MODULE:
27   case IDLN_BINOP:
28   case IDLN_UNARYOP:
29   case IDLN_CODEFRAG:
30     g_error("Strange type for being a builtin");
31     break;
32   case IDLN_INTEGER:
33   case IDLN_STRING:
34   case IDLN_WIDE_STRING:
35   case IDLN_CHAR:
36   case IDLN_WIDE_CHAR:
37   case IDLN_FIXED:
38   case IDLN_FLOAT:
39   case IDLN_BOOLEAN:
40   case IDLN_CONST_DCL:
41   case IDLN_TYPE_INTEGER:
42   case IDLN_TYPE_FLOAT:
43   case IDLN_TYPE_CHAR:
44   case IDLN_TYPE_WIDE_CHAR:
45   case IDLN_TYPE_STRING:
46   case IDLN_TYPE_WIDE_STRING:
47   case IDLN_TYPE_BOOLEAN:
48   case IDLN_TYPE_OCTET:
49   case IDLN_TYPE_ANY:
50   case IDLN_TYPE_OBJECT:
51   case IDLN_TYPE_TYPECODE:
52   case IDLN_TYPE_ENUM:
53     return TRUE;
54     break;
55   case IDLN_TYPE_DCL:
56   case IDLN_EXCEPT_DCL:
57   case IDLN_ATTR_DCL:
58   case IDLN_OP_DCL:
59   case IDLN_PARAM_DCL:
60   case IDLN_TYPE_FIXED:
61   case IDLN_TYPE_SEQUENCE:
62   case IDLN_TYPE_ARRAY:
63   case IDLN_TYPE_STRUCT:
64   case IDLN_TYPE_UNION:
65   case IDLN_IDENT:
66   case IDLN_INTERFACE:
67   case IDLN_FORWARD_DCL:
68   default:
69     return FALSE;
70     break;
71   }
72
73   return FALSE;
74 }
75
76 /**
77     Gets the "type" of {tree} as known in C.
78     The return value was alloc'd via g_malloc, and must be g_free'd.
79 **/
80 char *
81 orte_cbe_get_typespec_str(IDL_tree tree)
82 {
83   char *retval = NULL;
84   GString *tmpstr = NULL;
85
86   if(!tree) {
87     return g_strdup("void");
88   }
89
90   switch(IDL_NODE_TYPE(tree)) {
91   case IDLN_MEMBER:
92     return orte_cbe_get_typespec_str(IDL_MEMBER(tree).type_spec);
93     break;
94   case IDLN_TYPE_ANY:
95     retval = "CORBA_any";
96     break;
97   case IDLN_TYPE_FLOAT:
98     switch(IDL_TYPE_FLOAT(tree).f_type) {
99     case IDL_FLOAT_TYPE_FLOAT:
100       retval = "CORBA_float";
101       break;
102     case IDL_FLOAT_TYPE_DOUBLE:
103       retval = "CORBA_double";
104       break;
105     case IDL_FLOAT_TYPE_LONGDOUBLE:
106       retval = "CORBA_long_double";
107       break;
108     }
109     break;
110   case IDLN_TYPE_FIXED:
111     return g_strdup_printf( "CORBA_fixed_%" IDL_LL "d_%" IDL_LL "d",
112                      IDL_INTEGER(IDL_TYPE_FIXED(tree).positive_int_const).value,
113                      IDL_INTEGER(IDL_TYPE_FIXED(tree).integer_lit).value);
114     break;
115   case IDLN_TYPE_INTEGER:
116     tmpstr = g_string_new(NULL);
117     g_string_append(tmpstr, "CORBA_");
118     if(!IDL_TYPE_INTEGER(tree).f_signed)
119         g_string_append(tmpstr, "unsigned_");
120
121     switch(IDL_TYPE_INTEGER(tree).f_type) {
122     case IDL_INTEGER_TYPE_SHORT:
123         g_string_append(tmpstr, "short");
124         break;
125     case IDL_INTEGER_TYPE_LONGLONG:
126         g_string_append(tmpstr, "long_");
127         /* FALLTHROUGH */
128     case IDL_INTEGER_TYPE_LONG:
129         g_string_append(tmpstr, "long");
130         break;
131     }
132     break;
133   case IDLN_TYPE_STRING:
134     retval = "CORBA_string";    /* this is non-standard! */
135     break;
136   case IDLN_TYPE_OCTET:
137     retval = "CORBA_octet";
138     break;
139   case IDLN_TYPE_WIDE_STRING:
140     retval = "CORBA_wstring";   /* this is non-standard! */
141     break;
142   case IDLN_TYPE_CHAR:
143     retval = "CORBA_char";
144     break;
145   case IDLN_TYPE_WIDE_CHAR:
146     retval = "CORBA_wchar";
147     break;
148   case IDLN_TYPE_BOOLEAN:
149     retval = "CORBA_boolean";
150     break;
151   case IDLN_TYPE_STRUCT:
152     return orte_cbe_get_typespec_str(IDL_TYPE_STRUCT(tree).ident);
153   case IDLN_EXCEPT_DCL:
154     return orte_cbe_get_typespec_str(IDL_EXCEPT_DCL(tree).ident);
155   case IDLN_TYPE_ARRAY:
156     return orte_cbe_get_typespec_str(IDL_TYPE_ARRAY(tree).ident);
157   case IDLN_TYPE_UNION:
158     return orte_cbe_get_typespec_str(IDL_TYPE_UNION(tree).ident);
159   case IDLN_TYPE_ENUM:
160     return orte_cbe_get_typespec_str(IDL_TYPE_ENUM(tree).ident);
161   case IDLN_IDENT:
162     return IDL_ns_ident_to_qstring(IDL_IDENT_TO_NS(tree), "_", 0);
163   case IDLN_PARAM_DCL:
164     return orte_cbe_get_typespec_str(IDL_PARAM_DCL(tree).param_type_spec);
165   case IDLN_TYPE_SEQUENCE:
166     {
167         IDL_tree subtype = IDL_TYPE_SEQUENCE(tree).simple_type_spec;
168         char *ctmp, *base;
169         ctmp = orte_cbe_get_typespec_str(subtype);
170         /* We should have built-in alias to make this next line not needed */
171         base = orte_cbe_type_is_builtin(subtype)
172           ? ctmp + strlen("CORBA_") : ctmp;
173         retval = g_strdup_printf( "CORBA_sequence_%s", base);
174         g_free(ctmp);
175         return retval;
176     }
177     break;
178   case IDLN_NATIVE:
179     retval = "gpointer";
180     break;
181   case IDLN_FORWARD_DCL:
182   case IDLN_INTERFACE:
183     return orte_cbe_get_typespec_str(IDL_INTERFACE(tree).ident);
184   case IDLN_TYPE_OBJECT:
185     retval = "CORBA_Object";
186     break;
187   case IDLN_TYPE_TYPECODE:
188     retval = "CORBA_TypeCode";
189     break;
190   default:
191     g_error("We were asked to get a typename for a %s",
192             IDL_tree_type_names[IDL_NODE_TYPE(tree)]);
193     break;
194   }
195
196   if (retval)
197     return g_strdup (retval);
198   else
199     return g_string_free (tmpstr, FALSE);
200 }
201
202 void
203 orte_cbe_write_typespec(FILE *of, IDL_tree tree)
204 {
205     char *name = orte_cbe_get_typespec_str(tree);
206     fprintf( of, name);
207     g_free(name);
208 }
209
210 /* Writes the value of the constant in 'tree' to file handle 'of' */
211 static char *
212 orte_cbe_get_const(IDL_tree tree)
213 {
214   char *opc = NULL, *retval, *ctmp;
215   GString *tmpstr = g_string_new(NULL);
216
217   switch(IDL_NODE_TYPE(tree)) {
218   case IDLN_BOOLEAN:
219     g_string_printf(tmpstr, "%s", IDL_BOOLEAN(tree).value?"CORBA_TRUE":"CORBA_FALSE");
220     break;
221   case IDLN_CHAR:
222     g_string_printf(tmpstr, "'%s'", IDL_CHAR(tree).value);
223     break;
224   case IDLN_FLOAT:
225     g_string_printf(tmpstr, "%f", IDL_FLOAT(tree).value);
226     break;
227   case IDLN_INTEGER:
228     g_string_printf(tmpstr, "%" IDL_LL "d", IDL_INTEGER(tree).value);
229     break;
230   case IDLN_STRING:
231     g_string_printf(tmpstr, "\"%s\"", IDL_STRING(tree).value);
232     break;
233   case IDLN_WIDE_CHAR:
234     g_string_printf(tmpstr, "L'%ls'", IDL_WIDE_CHAR(tree).value);
235     break;
236   case IDLN_WIDE_STRING:
237     g_string_printf(tmpstr, "L\"%ls\"", IDL_WIDE_STRING(tree).value);
238     break;
239   case IDLN_BINOP:
240     g_string_printf(tmpstr, "(");
241     ctmp = orte_cbe_get_const(IDL_BINOP(tree).left);
242     g_string_append(tmpstr, ctmp);
243     g_free(ctmp);
244     switch(IDL_BINOP(tree).op) {
245     case IDL_BINOP_OR:
246       opc = "|";
247       break;
248     case IDL_BINOP_XOR:
249       opc = "^";
250       break;
251     case IDL_BINOP_AND:
252       opc = "&";
253       break;
254     case IDL_BINOP_SHR:
255       opc = ">>";
256       break;
257     case IDL_BINOP_SHL:
258       opc = "<<";
259       break;
260     case IDL_BINOP_ADD:
261       opc = "+";
262       break;
263     case IDL_BINOP_SUB:
264       opc = "-";
265       break;
266     case IDL_BINOP_MULT:
267       opc = "*";
268       break;
269     case IDL_BINOP_DIV:
270       opc = "/";
271       break;
272     case IDL_BINOP_MOD:
273       opc = "%";
274       break;
275     }
276     g_string_append_printf(tmpstr, " %s ", opc);
277     ctmp = orte_cbe_get_const(IDL_BINOP(tree).right);
278     g_string_append_printf(tmpstr, "%s)", ctmp);
279     g_free(ctmp);
280     break;
281   case IDLN_UNARYOP:
282     switch(IDL_UNARYOP(tree).op) {
283     case IDL_UNARYOP_PLUS: opc = "+"; break;
284     case IDL_UNARYOP_MINUS: opc = "-"; break;
285     case IDL_UNARYOP_COMPLEMENT: opc = "~"; break;
286     }
287     ctmp = orte_cbe_get_const(IDL_UNARYOP(tree).operand);
288     g_string_printf(tmpstr, "%s%s", opc, ctmp);
289     g_free(ctmp);
290     break;
291   case IDLN_IDENT:
292     {
293       char *id;
294       id = IDL_ns_ident_to_qstring(IDL_IDENT_TO_NS(tree), "_", 0);
295       g_string_printf(tmpstr, "%s", id);
296       g_free(id);
297     }
298     break;
299   default:
300     g_error("We were asked to print a constant for %s", IDL_tree_type_names[tree->_type]);
301     break;
302   }
303
304   retval = tmpstr->str;
305
306   g_string_free(tmpstr, FALSE);
307
308   return retval;
309 }
310
311 void
312 orte_cbe_write_const(FILE *of, IDL_tree tree)
313 {
314   char *ctmp;
315
316   ctmp = orte_cbe_get_const(tree);
317   fprintf(of, "%s", ctmp);
318   g_free(ctmp);
319 }
320
321 /* This is the WORST HACK in the WORLD, really truly, but the C preprocessor doesn't allow us to use
322    strings, so we have to work around it by using individual characters. */
323 void
324 orte_cbe_id_define_hack(FILE *fh, const char *def_prefix, const char *def_name, const char *def_value)
325 {
326   int i, n;
327   n = strlen(def_value);
328   for(i = 0; i < n; i++)
329     fprintf(fh, "#define %s_%s_%d '%c'\n", def_prefix, def_name, i, def_value[i]);
330 }