source: trunk/ORBit2-2.14.0/src/idl-compiler/orbit-idl-c-utils.c

Last change on this file was 249, checked in by cinc, 19 years ago

Provide parameter info for methods for runtime type information.

File size: 24.9 KB
Line 
1#include "config.h"
2
3#include "orbit-idl-c-backend.h"
4
5#include <string.h>
6
7char *
8orbit_cbe_get_typecode_name (IDL_tree tree)
9{
10 if (!tree)
11 return g_strdup ("TC_FIXME");
12 else
13 return g_strconcat ("TC_", orbit_cbe_get_typespec_str (tree), NULL);
14}
15
16gboolean
17orbit_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**/
80gulong
81orbit_cbe_get_typespec_size(IDL_tree tree)
82{
83 gulong retval=0;
84
85 if(!tree) {
86 return 0;
87 }
88
89 switch(IDL_NODE_TYPE(tree)) {
90#if 0
91 case IDLN_MEMBER:
92 return orbit_cbe_get_typespec_str(IDL_MEMBER(tree).type_spec);
93 break;
94 case IDLN_TYPE_ANY:
95 retval = sizeof(CORBA_any);
96 break;
97#endif
98 case IDLN_TYPE_FLOAT:
99 switch(IDL_TYPE_FLOAT(tree).f_type) {
100 case IDL_FLOAT_TYPE_FLOAT:
101 retval = sizeof(CORBA_float);
102 break;
103 case IDL_FLOAT_TYPE_DOUBLE:
104 retval = sizeof(CORBA_double);
105 break;
106 case IDL_FLOAT_TYPE_LONGDOUBLE:
107 retval = sizeof(CORBA_long_double);
108 break;
109 }
110 break;
111#if 0
112 case IDLN_TYPE_FIXED:
113 return g_strdup_printf( "CORBA_fixed_%" IDL_LL "d_%" IDL_LL "d",
114 IDL_INTEGER(IDL_TYPE_FIXED(tree).positive_int_const).value,
115 IDL_INTEGER(IDL_TYPE_FIXED(tree).integer_lit).value);
116 break;
117#endif
118 case IDLN_TYPE_INTEGER:
119 if(!IDL_TYPE_INTEGER(tree).f_signed)
120 {
121 /* unsigned types */
122 switch(IDL_TYPE_INTEGER(tree).f_type)
123 {
124 case IDL_INTEGER_TYPE_SHORT:
125 retval=sizeof(CORBA_unsigned_short);
126 break;
127 case IDL_INTEGER_TYPE_LONGLONG:
128 retval=sizeof(CORBA_unsigned_long_long);
129 break;
130 case IDL_INTEGER_TYPE_LONG:
131 retval=sizeof(CORBA_unsigned_long);
132 break;
133 }
134 }
135 else{
136 switch(IDL_TYPE_INTEGER(tree).f_type)
137 {
138 /* signed types */
139 case IDL_INTEGER_TYPE_SHORT:
140 retval=sizeof(CORBA_short);
141 break;
142 case IDL_INTEGER_TYPE_LONGLONG:
143 retval=sizeof(CORBA_long_long);
144 break;
145 case IDL_INTEGER_TYPE_LONG:
146 retval=sizeof(CORBA_long);
147 break;
148 }
149 }
150 break;
151 case IDLN_TYPE_OCTET:
152 retval = sizeof(CORBA_octet);
153 break;
154 case IDLN_TYPE_STRING:
155 retval = sizeof(CORBA_string); /* this is non-standard! */
156 break;
157 case IDLN_TYPE_BOOLEAN:
158 retval = sizeof(CORBA_boolean);
159 break;
160 case IDLN_TYPE_CHAR:
161 retval = sizeof(CORBA_char);
162 break;
163 case IDLN_TYPE_OBJECT:
164 retval = sizeof(CORBA_Object);
165 break;
166 case IDLN_NATIVE:
167 retval = sizeof(gpointer);
168 break;
169#if 0
170 case IDLN_TYPE_WIDE_STRING:
171 retval = "CORBA_wstring"; /* this is non-standard! */
172 break;
173 case IDLN_TYPE_WIDE_CHAR:
174 retval = "CORBA_wchar";
175 break;
176 case IDLN_TYPE_STRUCT:
177 return orbit_cbe_get_typespec_str(IDL_TYPE_STRUCT(tree).ident);
178 case IDLN_EXCEPT_DCL:
179 return orbit_cbe_get_typespec_str(IDL_EXCEPT_DCL(tree).ident);
180 case IDLN_TYPE_ARRAY:
181 return orbit_cbe_get_typespec_str(IDL_TYPE_ARRAY(tree).ident);
182 case IDLN_TYPE_UNION:
183 return orbit_cbe_get_typespec_str(IDL_TYPE_UNION(tree).ident);
184 case IDLN_TYPE_ENUM:
185 return orbit_cbe_get_typespec_str(IDL_TYPE_ENUM(tree).ident);
186 case IDLN_IDENT:
187 return IDL_ns_ident_to_qstring(IDL_IDENT_TO_NS(tree), "_", 0);
188 case IDLN_PARAM_DCL:
189 return orbit_cbe_get_typespec_str(IDL_PARAM_DCL(tree).param_type_spec);
190 case IDLN_TYPE_SEQUENCE:
191 {
192 IDL_tree subtype = IDL_TYPE_SEQUENCE(tree).simple_type_spec;
193 char *ctmp, *base;
194 ctmp = orbit_cbe_get_typespec_str(subtype);
195 /* We should have built-in alias to make this next line not needed */
196 base = orbit_cbe_type_is_builtin(subtype)
197 ? ctmp + strlen("CORBA_") : ctmp;
198 retval = g_strdup_printf( "CORBA_sequence_%s", base);
199 g_free(ctmp);
200 return retval;
201 }
202 break;
203 case IDLN_FORWARD_DCL:
204 case IDLN_INTERFACE:
205 return orbit_cbe_get_typespec_str(IDL_INTERFACE(tree).ident);
206 case IDLN_TYPE_TYPECODE:
207 retval = "CORBA_TypeCode";
208 break;
209#endif
210 default:
211 // retval=sizeof(gpointer);
212 // g_error("We were asked to get a typesize for a %s (%s, %s line %d)",
213 // IDL_tree_type_names[IDL_NODE_TYPE(tree)], __FILE__, __FUNCTION__, __LINE__);
214 g_print("We were asked to get a typesize for a %s -> %s (%s line %d)\n",
215 IDL_tree_type_names[IDL_NODE_TYPE(tree)], IDL_ns_ident_to_qstring(IDL_IDENT_TO_NS(tree), "_", 0),
216 __FUNCTION__, __LINE__);
217 g_print("HACK: Returning sizeof(gpointer) now...\n", IDL_ns_ident_to_qstring(IDL_IDENT_TO_NS(tree), "_", 0));
218 retval=sizeof(gpointer);
219 break;
220 }
221
222 return retval;
223}
224
225/**
226 Gets the "type" of {tree} as known in C.
227 The return value was alloc'd via g_malloc, and must be g_free'd.
228**/
229char *
230orbit_cbe_get_typespec_str(IDL_tree tree)
231{
232 char *retval = NULL;
233 GString *tmpstr = NULL;
234
235 if(!tree) {
236 return g_strdup("void");
237 }
238
239 switch(IDL_NODE_TYPE(tree)) {
240 case IDLN_MEMBER:
241 return orbit_cbe_get_typespec_str(IDL_MEMBER(tree).type_spec);
242 break;
243 case IDLN_TYPE_ANY:
244 retval = "CORBA_any";
245 break;
246 case IDLN_TYPE_FLOAT:
247 switch(IDL_TYPE_FLOAT(tree).f_type) {
248 case IDL_FLOAT_TYPE_FLOAT:
249 retval = "CORBA_float";
250 break;
251 case IDL_FLOAT_TYPE_DOUBLE:
252 retval = "CORBA_double";
253 break;
254 case IDL_FLOAT_TYPE_LONGDOUBLE:
255 retval = "CORBA_long_double";
256 break;
257 }
258 break;
259 case IDLN_TYPE_FIXED:
260 return g_strdup_printf( "CORBA_fixed_%" IDL_LL "d_%" IDL_LL "d",
261 IDL_INTEGER(IDL_TYPE_FIXED(tree).positive_int_const).value,
262 IDL_INTEGER(IDL_TYPE_FIXED(tree).integer_lit).value);
263 break;
264 case IDLN_TYPE_INTEGER:
265 tmpstr = g_string_new(NULL);
266 g_string_append(tmpstr, "CORBA_");
267 if(!IDL_TYPE_INTEGER(tree).f_signed)
268 g_string_append(tmpstr, "unsigned_");
269
270 switch(IDL_TYPE_INTEGER(tree).f_type) {
271 case IDL_INTEGER_TYPE_SHORT:
272 g_string_append(tmpstr, "short");
273 break;
274 case IDL_INTEGER_TYPE_LONGLONG:
275 g_string_append(tmpstr, "long_");
276 /* FALLTHROUGH */
277 case IDL_INTEGER_TYPE_LONG:
278 g_string_append(tmpstr, "long");
279 break;
280 }
281 break;
282 case IDLN_TYPE_STRING:
283 retval = "CORBA_string"; /* this is non-standard! */
284 break;
285 case IDLN_TYPE_OCTET:
286 retval = "CORBA_octet";
287 break;
288 case IDLN_TYPE_WIDE_STRING:
289 retval = "CORBA_wstring"; /* this is non-standard! */
290 break;
291 case IDLN_TYPE_CHAR:
292 retval = "CORBA_char";
293 break;
294 case IDLN_TYPE_WIDE_CHAR:
295 retval = "CORBA_wchar";
296 break;
297 case IDLN_TYPE_BOOLEAN:
298 retval = "CORBA_boolean";
299 break;
300 case IDLN_TYPE_STRUCT:
301 return orbit_cbe_get_typespec_str(IDL_TYPE_STRUCT(tree).ident);
302 case IDLN_EXCEPT_DCL:
303 return orbit_cbe_get_typespec_str(IDL_EXCEPT_DCL(tree).ident);
304 case IDLN_TYPE_ARRAY:
305 return orbit_cbe_get_typespec_str(IDL_TYPE_ARRAY(tree).ident);
306 case IDLN_TYPE_UNION:
307 return orbit_cbe_get_typespec_str(IDL_TYPE_UNION(tree).ident);
308 case IDLN_TYPE_ENUM:
309 return orbit_cbe_get_typespec_str(IDL_TYPE_ENUM(tree).ident);
310 case IDLN_IDENT:
311 return IDL_ns_ident_to_qstring(IDL_IDENT_TO_NS(tree), "_", 0);
312 case IDLN_PARAM_DCL:
313 return orbit_cbe_get_typespec_str(IDL_PARAM_DCL(tree).param_type_spec);
314 case IDLN_TYPE_SEQUENCE:
315 {
316 IDL_tree subtype = IDL_TYPE_SEQUENCE(tree).simple_type_spec;
317 char *ctmp, *base;
318 ctmp = orbit_cbe_get_typespec_str(subtype);
319 /* We should have built-in alias to make this next line not needed */
320 base = orbit_cbe_type_is_builtin(subtype)
321 ? ctmp + strlen("CORBA_") : ctmp;
322 retval = g_strdup_printf( "CORBA_sequence_%s", base);
323 g_free(ctmp);
324 return retval;
325 }
326 break;
327 case IDLN_NATIVE:
328 retval = "gpointer";
329 break;
330 case IDLN_FORWARD_DCL:
331 case IDLN_INTERFACE:
332 return orbit_cbe_get_typespec_str(IDL_INTERFACE(tree).ident);
333 case IDLN_TYPE_OBJECT:
334 retval = "CORBA_Object";
335 break;
336 case IDLN_TYPE_TYPECODE:
337 retval = "CORBA_TypeCode";
338 break;
339 default:
340 g_error("We were asked to get a typename for a %s",
341 IDL_tree_type_names[IDL_NODE_TYPE(tree)]);
342 break;
343 }
344
345 if (retval)
346 return g_strdup (retval);
347 else
348 return g_string_free (tmpstr, FALSE);
349}
350
351void
352orbit_cbe_write_typespec(FILE *of, IDL_tree tree)
353{
354 char *name = orbit_cbe_get_typespec_str(tree);
355 fprintf( of, name);
356 g_free(name);
357}
358
359/**
360 Parameters (e.g., arguments to methods, and the return value
361 have some complicated rules in the C mapping for how many
362 levels of pointer and the exact type involved. This function
363 generates the externally visible parameter type.
364
365 Note the hack below because "const CORBA_string" is not
366 promotable to "const CORBA_char*" (the later the standard
367 to which apps are written, while the former is what would
368 be produced without the hack).
369**/
370static char *
371orbit_cbe_write_param_typespec_str(IDL_tree ts, IDL_ParamRole role)
372{
373 int i, n;
374 gboolean isSlice;
375 char *name;
376 GString *str = g_string_sized_new (23);
377 IDL_tree typedef_spec;
378 char *typedef_name;
379
380 n = oidl_param_info (ts, role, &isSlice);
381 name = orbit_cbe_get_typespec_str (ts);
382
383 if ( role == DATA_IN ) {
384 /* We want to check if this is a typedef for CORBA_string so we can do special handling
385 * in that case.
386 */
387 typedef_spec = orbit_cbe_get_typespec (ts);
388 typedef_name = orbit_cbe_get_typespec_str (typedef_spec);
389
390 g_string_printf (str, "const %s",
391 !strcmp (typedef_name, "CORBA_string") ?
392 "CORBA_char *" : name);
393
394 g_free (typedef_name);
395 } else
396 g_string_printf (str, "%s", name);
397
398 g_free (name);
399
400 if ( isSlice )
401 g_string_append (str, "_slice");
402
403 for (i = 0; i < n; i++)
404 g_string_append_c (str, '*');
405
406 return g_string_free (str, FALSE);
407}
408
409/*
410 This is almost the same like orbit_cbe_write_param_typespec_str(). We only omit the
411 'const' qualifier because we only need the parameter type.
412 */
413static char *
414orbit_cbe_voyager_write_param_typespec_str(IDL_tree ts, IDL_ParamRole role)
415{
416 int i, n;
417 gboolean isSlice;
418 char *name;
419 GString *str = g_string_sized_new (23);
420 IDL_tree typedef_spec;
421 char *typedef_name;
422
423 n = oidl_param_info (ts, role, &isSlice);
424 name = orbit_cbe_get_typespec_str (ts);
425
426 if ( role == DATA_IN ) {
427 /* We want to check if this is a typedef for CORBA_string so we can do special handling
428 * in that case.
429 */
430 typedef_spec = orbit_cbe_get_typespec (ts);
431 typedef_name = orbit_cbe_get_typespec_str (typedef_spec);
432
433 g_string_printf (str, "%s",
434 !strcmp (typedef_name, "CORBA_string") ?
435 "CORBA_char *" : name);
436
437 g_free (typedef_name);
438 } else
439 g_string_printf (str, "%s", name);
440
441 g_free (name);
442
443 if ( isSlice )
444 g_string_append (str, "_slice");
445
446 for (i = 0; i < n; i++)
447 g_string_append_c (str, '*');
448
449 return g_string_free (str, FALSE);
450}
451
452/*
453 This helper just writes the typespec of a parameter to a method without any
454 'const' qualifier.
455 */
456void
457orbit_cbe_voyager_write_param_typespec(FILE *of, IDL_tree tree) {
458 IDL_tree ts = NULL /* Quiet gcc */;
459 IDL_ParamRole role = 0 /* Quiet gcc */;
460 char *str;
461
462 switch ( IDL_NODE_TYPE(tree) ) {
463 case IDLN_PARAM_DCL: /* one of the parameters */
464 ts = IDL_PARAM_DCL(tree).param_type_spec;
465 role = oidl_attr_to_paramrole(IDL_PARAM_DCL(tree).attr);
466 break;
467 default:
468 g_assert_not_reached();
469 }
470
471 str = orbit_cbe_voyager_write_param_typespec_str (ts, role);
472 fprintf (of, str);
473 g_free (str);
474}
475
476static void
477orbit_cbe_write_param_typespec_raw (FILE *of, IDL_tree ts, IDL_ParamRole role)
478{
479 char *str;
480 str = orbit_cbe_write_param_typespec_str (ts, role);
481 fprintf (of, str);
482 g_free (str);
483}
484
485void
486orbit_cbe_write_param_typespec(FILE *of, IDL_tree tree) {
487 IDL_tree ts = NULL /* Quiet gcc */;
488 IDL_ParamRole role = 0 /* Quiet gcc */;
489
490 switch ( IDL_NODE_TYPE(tree) ) {
491 case IDLN_OP_DCL: /* means return value of method */
492 ts = IDL_OP_DCL(tree).op_type_spec;
493 role = DATA_RETURN;
494 break;
495 case IDLN_PARAM_DCL: /* one of the parameters */
496 ts = IDL_PARAM_DCL(tree).param_type_spec;
497 role = oidl_attr_to_paramrole(IDL_PARAM_DCL(tree).attr);
498 break;
499 default:
500 g_assert_not_reached();
501 }
502 orbit_cbe_write_param_typespec_raw(of, ts, role);
503}
504
505void
506orbit_cbe_op_write_proto (FILE *of,
507 IDL_tree op,
508 const char *nom_prefix,
509 gboolean for_epv)
510{
511 IDL_tree sub;
512 char *id;
513 char * id2;
514 char * ptr;
515
516 g_assert (IDL_NODE_TYPE(op) == IDLN_OP_DCL);
517
518 orbit_cbe_write_param_typespec (of, op);
519
520 id = IDL_ns_ident_to_qstring (
521 IDL_IDENT_TO_NS (IDL_INTERFACE (
522 IDL_get_parent_node (op, IDLN_INTERFACE, NULL)).ident), "_", 0);
523
524#ifdef USE_LIBIDL_CODE
525 if (for_epv)
526 fprintf (of, " (*%s%s)", nom_prefix ? nom_prefix : "",
527 IDL_IDENT(IDL_OP_DCL(op).ident).str);
528 else
529 fprintf (of, " %s%s_%s", nom_prefix ? nom_prefix : "",
530 id, IDL_IDENT(IDL_OP_DCL(op).ident).str);
531#else
532 id2=g_strdup(IDL_IDENT (IDL_OP_DCL (op).ident).str);
533 if((ptr=strstr(id2, NOM_OVERRIDE_STRING))!=NULL)
534 *ptr='\0';
535 if (for_epv)
536 fprintf (of, " (*%s%s)", nom_prefix ? nom_prefix : "",
537 IDL_IDENT(IDL_OP_DCL(op).ident).str);
538 else
539 fprintf (of, " %s%s_%s", nom_prefix ? nom_prefix : "",
540 id, id2);
541
542 g_free(id2);
543#endif
544
545 fprintf (of, "(");
546
547 if (for_epv)
548 fprintf (of, "PortableServer_Servant _servant, ");
549 else
550#ifdef USE_LIBIDL_CODE
551 fprintf (of, "%s _obj, ", id);
552#else
553 fprintf (of, "%s* nomSelf, ", id);
554#endif
555 g_free (id);
556
557 for (sub = IDL_OP_DCL (op).parameter_dcls; sub; sub = IDL_LIST (sub).next) {
558 IDL_tree parm = IDL_LIST (sub).data;
559
560 orbit_cbe_write_param_typespec (of, parm);
561
562 fprintf (of, " %s, ", IDL_IDENT (IDL_PARAM_DCL (parm).simple_declarator).str);
563 }
564
565 if (IDL_OP_DCL (op).context_expr)
566 fprintf (of, "CORBA_Context _ctx, ");
567
568 fprintf (of, "CORBA_Environment *ev)");
569}
570
571/* Writes the value of the constant in 'tree' to file handle 'of' */
572static char *
573orbit_cbe_get_const(IDL_tree tree)
574{
575 char *opc = NULL, *retval, *ctmp;
576 GString *tmpstr = g_string_new(NULL);
577
578 switch(IDL_NODE_TYPE(tree)) {
579 case IDLN_BOOLEAN:
580 g_string_printf(tmpstr, "%s", IDL_BOOLEAN(tree).value?"CORBA_TRUE":"CORBA_FALSE");
581 break;
582 case IDLN_CHAR:
583 g_string_printf(tmpstr, "'\\x%X'", *(unsigned char *)IDL_CHAR(tree).value);
584 break;
585 case IDLN_FLOAT:
586 g_string_printf(tmpstr, "%f", IDL_FLOAT(tree).value);
587 break;
588 case IDLN_INTEGER:
589 g_string_printf(tmpstr, "%" IDL_LL "d", IDL_INTEGER(tree).value);
590 break;
591 case IDLN_STRING:
592 g_string_printf(tmpstr, "\"%s\"", IDL_STRING(tree).value);
593 break;
594 case IDLN_WIDE_CHAR:
595 g_string_printf(tmpstr, "L'%ls'", IDL_WIDE_CHAR(tree).value);
596 break;
597 case IDLN_WIDE_STRING:
598 g_string_printf(tmpstr, "L\"%ls\"", IDL_WIDE_STRING(tree).value);
599 break;
600 case IDLN_BINOP:
601 g_string_printf(tmpstr, "(");
602 ctmp = orbit_cbe_get_const(IDL_BINOP(tree).left);
603 g_string_append(tmpstr, ctmp);
604 g_free(ctmp);
605 switch(IDL_BINOP(tree).op) {
606 case IDL_BINOP_OR:
607 opc = "|";
608 break;
609 case IDL_BINOP_XOR:
610 opc = "^";
611 break;
612 case IDL_BINOP_AND:
613 opc = "&";
614 break;
615 case IDL_BINOP_SHR:
616 opc = ">>";
617 break;
618 case IDL_BINOP_SHL:
619 opc = "<<";
620 break;
621 case IDL_BINOP_ADD:
622 opc = "+";
623 break;
624 case IDL_BINOP_SUB:
625 opc = "-";
626 break;
627 case IDL_BINOP_MULT:
628 opc = "*";
629 break;
630 case IDL_BINOP_DIV:
631 opc = "/";
632 break;
633 case IDL_BINOP_MOD:
634 opc = "%";
635 break;
636 }
637 g_string_append_printf(tmpstr, " %s ", opc);
638 ctmp = orbit_cbe_get_const(IDL_BINOP(tree).right);
639 g_string_append_printf(tmpstr, "%s)", ctmp);
640 g_free(ctmp);
641 break;
642 case IDLN_UNARYOP:
643 switch(IDL_UNARYOP(tree).op) {
644 case IDL_UNARYOP_PLUS: opc = "+"; break;
645 case IDL_UNARYOP_MINUS: opc = "-"; break;
646 case IDL_UNARYOP_COMPLEMENT: opc = "~"; break;
647 }
648 ctmp = orbit_cbe_get_const(IDL_UNARYOP(tree).operand);
649 g_string_printf(tmpstr, "%s%s", opc, ctmp);
650 g_free(ctmp);
651 break;
652 case IDLN_IDENT:
653 {
654 char *id;
655 id = IDL_ns_ident_to_qstring(IDL_IDENT_TO_NS(tree), "_", 0);
656 g_string_printf(tmpstr, "%s", id);
657 g_free(id);
658 }
659 break;
660 default:
661 g_error("We were asked to print a constant for %s", IDL_tree_type_names[tree->_type]);
662 break;
663 }
664
665 retval = tmpstr->str;
666
667 g_string_free(tmpstr, FALSE);
668
669 return retval;
670}
671
672void
673orbit_cbe_write_const(FILE *of, IDL_tree tree)
674{
675 char *ctmp;
676
677 ctmp = orbit_cbe_get_const(tree);
678 fprintf(of, "%s", ctmp);
679 g_free(ctmp);
680}
681
682/* This is the WORST HACK in the WORLD, really truly, but the C preprocessor doesn't allow us to use
683 strings, so we have to work around it by using individual characters. */
684void
685orbit_cbe_id_define_hack(FILE *fh, const char *def_prefix, const char *def_name, const char *def_value)
686{
687 int i, n;
688 n = strlen(def_value);
689 for(i = 0; i < n; i++)
690 fprintf(fh, "#define %s_%s_%d '%c'\n", def_prefix, def_name, i, def_value[i]);
691}
692
693void
694orbit_cbe_id_cond_hack(FILE *fh, const char *def_prefix, const char *def_name, const char *def_value)
695{
696 int i, n;
697 n = strlen(def_value);
698 if(n <= 0)
699 return;
700
701 fprintf(fh, "(");
702
703 for(i = 0; i < n; i++)
704 fprintf(fh, "%s (%s_%s_%d == '%c') \\\n", i?"&&":"", def_prefix, def_name, i, def_value[i]);
705 fprintf(fh, ")");
706}
707
708#define BASE_TYPES \
709 IDLN_TYPE_INTEGER: \
710 case IDLN_TYPE_FLOAT: \
711 case IDLN_TYPE_ENUM: \
712 case IDLN_TYPE_BOOLEAN: \
713 case IDLN_TYPE_CHAR: \
714 case IDLN_TYPE_WIDE_CHAR: \
715 case IDLN_TYPE_OCTET
716
717#define STRING_TYPES \
718 IDLN_TYPE_STRING: \
719 case IDLN_TYPE_WIDE_STRING
720
721#define OBJREF_TYPES \
722 IDLN_TYPE_OBJECT: \
723 case IDLN_INTERFACE: \
724 case IDLN_FORWARD_DCL
725
726static const char *
727orbit_cbe_flatten_ref (IDL_ParamRole role, IDL_tree typespec)
728{
729 gboolean is_fixed;
730
731 is_fixed = orbit_cbe_type_is_fixed_length (typespec);
732
733 switch (role) {
734 case DATA_IN:
735 switch (IDL_NODE_TYPE (typespec)) {
736 case BASE_TYPES:
737 case STRING_TYPES:
738 case OBJREF_TYPES:
739 case IDLN_TYPE_TYPECODE:
740 case IDLN_NATIVE:
741 return "(gpointer)&";
742
743 case IDLN_TYPE_STRUCT:
744 case IDLN_TYPE_UNION:
745 case IDLN_TYPE_ANY:
746 case IDLN_TYPE_SEQUENCE:
747 case IDLN_TYPE_ARRAY:
748 return "(gpointer)";
749
750 default:
751 case IDLN_TYPE_FIXED:
752 g_error ("Hit evil type %d", IDL_NODE_TYPE (typespec));
753 };
754 return NULL;
755
756 case DATA_INOUT:
757 switch (IDL_NODE_TYPE (typespec)) {
758 case BASE_TYPES:
759 case STRING_TYPES:
760 case OBJREF_TYPES:
761 case IDLN_TYPE_TYPECODE:
762 case IDLN_TYPE_STRUCT:
763 case IDLN_TYPE_UNION:
764 case IDLN_TYPE_ARRAY:
765 case IDLN_NATIVE:
766 case IDLN_TYPE_ANY:
767 case IDLN_TYPE_SEQUENCE:
768 return "";
769
770 default:
771 case IDLN_TYPE_FIXED:
772 g_error ("Hit evil type %d", IDL_NODE_TYPE (typespec));
773 };
774 return NULL;
775
776 case DATA_OUT:
777 switch (IDL_NODE_TYPE (typespec)) {
778 case BASE_TYPES:
779 case STRING_TYPES:
780 case OBJREF_TYPES:
781 case IDLN_TYPE_TYPECODE:
782 case IDLN_NATIVE:
783 return "&";
784
785 case IDLN_TYPE_STRUCT:
786 case IDLN_TYPE_UNION:
787 case IDLN_TYPE_ARRAY:
788 if (is_fixed)
789 return "&";
790 else /* drop through */
791
792 case IDLN_TYPE_SEQUENCE:
793 case IDLN_TYPE_ANY:
794 return "";
795
796 default:
797 case IDLN_TYPE_FIXED:
798 g_error ("Hit evil type %d", IDL_NODE_TYPE (typespec));
799 };
800 return NULL;
801
802 case DATA_RETURN:
803 g_error ("No data return handler");
804 return NULL;
805 }
806
807 return NULL;
808}
809
810void
811orbit_cbe_flatten_args (IDL_tree tree, FILE *of, const char *name)
812{
813 int i = 0;
814 IDL_tree l;
815
816 for (l = IDL_OP_DCL(tree).parameter_dcls; l;
817 l = IDL_LIST(l).next)
818 i++;
819
820 fprintf (of, "gpointer %s[%d];\n", name, i);
821
822 i = 0;
823 for (l = IDL_OP_DCL(tree).parameter_dcls; l;
824 l = IDL_LIST(l).next) {
825 IDL_tree decl = IDL_LIST (l).data;
826 IDL_tree tspec = orbit_cbe_get_typespec (decl);
827 IDL_ParamRole r = 0;
828
829 switch(IDL_PARAM_DCL(decl).attr) {
830 case IDL_PARAM_IN: r = DATA_IN; break;
831 case IDL_PARAM_INOUT: r = DATA_INOUT; break;
832 case IDL_PARAM_OUT: r = DATA_OUT; break;
833 default:
834 g_error("Unknown IDL_PARAM type");
835 }
836
837 fprintf (of, "%s[%d] = %s%s;\n",
838 name, i,
839 orbit_cbe_flatten_ref (r, tspec),
840 IDL_IDENT (IDL_PARAM_DCL (decl).simple_declarator).str);
841 i++;
842 }
843}
844
845static char *
846orbit_cbe_unflatten_ref (IDL_ParamRole role, IDL_tree typespec)
847{
848 gboolean is_fixed;
849 char *typestr;
850 char *retval;
851
852 is_fixed = orbit_cbe_type_is_fixed_length (typespec);
853
854 typestr = orbit_cbe_write_param_typespec_str (typespec, role);
855
856 switch (role) {
857 case DATA_IN:
858 switch (IDL_NODE_TYPE (typespec)) {
859 case BASE_TYPES:
860 case STRING_TYPES:
861 case OBJREF_TYPES:
862 case IDLN_TYPE_TYPECODE:
863 case IDLN_NATIVE:
864 retval = g_strdup_printf ("*(%s *)", typestr);
865 break;
866
867
868 case IDLN_TYPE_ARRAY:
869 retval = g_strdup_printf ("(%s_slice *)", typestr);
870 break;
871
872 case IDLN_TYPE_STRUCT:
873 case IDLN_TYPE_UNION:
874 case IDLN_TYPE_ANY:
875 case IDLN_TYPE_SEQUENCE:
876 retval = g_strdup_printf ("(%s)", typestr);
877 break;
878
879 default:
880 case IDLN_TYPE_FIXED:
881 g_error ("Hit evil type %d", IDL_NODE_TYPE (typespec));
882 retval = NULL;
883 break;
884 };
885 break;
886
887 case DATA_INOUT:
888 switch (IDL_NODE_TYPE (typespec)) {
889 case IDLN_TYPE_ARRAY:
890 retval = g_strdup_printf ("(%s_slice *)", typestr);
891 break;
892
893 case BASE_TYPES:
894 case STRING_TYPES:
895 case OBJREF_TYPES:
896 case IDLN_TYPE_TYPECODE:
897 case IDLN_TYPE_STRUCT:
898 case IDLN_TYPE_UNION:
899 case IDLN_NATIVE:
900 case IDLN_TYPE_ANY:
901 case IDLN_TYPE_SEQUENCE:
902 retval = g_strdup_printf ("(%s)", typestr);
903 break;
904
905 default:
906 case IDLN_TYPE_FIXED:
907 g_error ("Hit evil type %d", IDL_NODE_TYPE (typespec));
908 retval = NULL;
909 break;
910 };
911 break;
912
913 case DATA_OUT:
914 switch (IDL_NODE_TYPE (typespec)) {
915 case BASE_TYPES:
916 case STRING_TYPES:
917 case OBJREF_TYPES:
918 case IDLN_TYPE_TYPECODE:
919 case IDLN_NATIVE:
920 retval = g_strdup_printf ("*(%s *)", typestr);
921 break;
922
923 case IDLN_TYPE_ARRAY:
924 if (is_fixed) {
925 retval = g_strdup_printf ("*(%s_slice **)", typestr);
926 break;
927 }
928 /* drop through */
929
930 case IDLN_TYPE_STRUCT:
931 case IDLN_TYPE_UNION:
932 if (is_fixed) {
933 retval = g_strdup_printf ("*(%s *)", typestr);
934 break;
935 }
936 /* drop through */
937
938 case IDLN_TYPE_SEQUENCE:
939 case IDLN_TYPE_ANY:
940 retval = g_strdup_printf ("(%s)", typestr);
941 break;
942
943 default:
944 case IDLN_TYPE_FIXED:
945 g_error ("Hit evil type %d", IDL_NODE_TYPE (typespec));
946 retval = NULL;
947 break;
948 };
949 break;
950
951 case DATA_RETURN:
952 default:
953 g_error ("No data return handler");
954 retval = NULL;
955 break;
956 }
957
958 g_free (typestr);
959
960 return retval;
961}
962
963void
964orbit_cbe_unflatten_args (IDL_tree tree, FILE *of, const char *name)
965{
966 IDL_tree l;
967 int i = 0;
968
969 for (l = IDL_OP_DCL(tree).parameter_dcls; l;
970 l = IDL_LIST(l).next) {
971 IDL_tree decl = IDL_LIST (l).data;
972 IDL_tree tspec = orbit_cbe_get_typespec (decl);
973 IDL_ParamRole r = 0;
974 char *unflatten;
975
976 switch(IDL_PARAM_DCL(decl).attr) {
977 case IDL_PARAM_IN: r = DATA_IN; break;
978 case IDL_PARAM_INOUT: r = DATA_INOUT; break;
979 case IDL_PARAM_OUT: r = DATA_OUT; break;
980 default:
981 g_error("Unknown IDL_PARAM type");
982 }
983
984 unflatten = orbit_cbe_unflatten_ref (r, tspec);
985 fprintf (of, "%s%s[%d], ", unflatten, name, i++);
986 g_free (unflatten);
987 }
988}
Note: See TracBrowser for help on using the repository browser.