3 #include "forb-idl-c-backend.h"
8 forb_cbe_get_typecode_name (IDL_tree tree)
11 return g_strdup ("TC_FIXME");
13 return g_strconcat ("TC_", forb_cbe_get_typespec_str (tree), NULL);
17 forb_cbe_type_is_builtin(IDL_tree tree)
20 switch(IDL_NODE_TYPE(tree)) {
30 g_error("Strange type for being a builtin");
34 case IDLN_WIDE_STRING:
41 case IDLN_TYPE_INTEGER:
44 case IDLN_TYPE_WIDE_CHAR:
45 case IDLN_TYPE_STRING:
46 case IDLN_TYPE_WIDE_STRING:
47 case IDLN_TYPE_BOOLEAN:
50 case IDLN_TYPE_OBJECT:
51 case IDLN_TYPE_TYPECODE:
61 case IDLN_TYPE_SEQUENCE:
63 case IDLN_TYPE_STRUCT:
67 case IDLN_FORWARD_DCL:
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.
81 forb_cbe_get_typespec_str(IDL_tree tree)
84 GString *tmpstr = NULL;
87 return g_strdup("void");
90 switch(IDL_NODE_TYPE(tree)) {
92 return forb_cbe_get_typespec_str(IDL_MEMBER(tree).type_spec);
98 switch(IDL_TYPE_FLOAT(tree).f_type) {
99 case IDL_FLOAT_TYPE_FLOAT:
100 retval = "CORBA_float";
102 case IDL_FLOAT_TYPE_DOUBLE:
103 retval = "CORBA_double";
105 case IDL_FLOAT_TYPE_LONGDOUBLE:
106 retval = "CORBA_long_double";
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);
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_");
121 switch(IDL_TYPE_INTEGER(tree).f_type) {
122 case IDL_INTEGER_TYPE_SHORT:
123 g_string_append(tmpstr, "short");
125 case IDL_INTEGER_TYPE_LONGLONG:
126 g_string_append(tmpstr, "long_");
128 case IDL_INTEGER_TYPE_LONG:
129 g_string_append(tmpstr, "long");
133 case IDLN_TYPE_STRING:
134 retval = "CORBA_string"; /* this is non-standard! */
136 case IDLN_TYPE_OCTET:
137 retval = "CORBA_octet";
139 case IDLN_TYPE_WIDE_STRING:
140 retval = "CORBA_wstring"; /* this is non-standard! */
143 retval = "CORBA_char";
145 case IDLN_TYPE_WIDE_CHAR:
146 retval = "CORBA_wchar";
148 case IDLN_TYPE_BOOLEAN:
149 retval = "CORBA_boolean";
151 case IDLN_TYPE_STRUCT:
152 return forb_cbe_get_typespec_str(IDL_TYPE_STRUCT(tree).ident);
153 case IDLN_EXCEPT_DCL:
154 return forb_cbe_get_typespec_str(IDL_EXCEPT_DCL(tree).ident);
155 case IDLN_TYPE_ARRAY:
156 return forb_cbe_get_typespec_str(IDL_TYPE_ARRAY(tree).ident);
157 case IDLN_TYPE_UNION:
158 return forb_cbe_get_typespec_str(IDL_TYPE_UNION(tree).ident);
160 return forb_cbe_get_typespec_str(IDL_TYPE_ENUM(tree).ident);
162 return IDL_ns_ident_to_qstring(IDL_IDENT_TO_NS(tree), "_", 0);
164 return forb_cbe_get_typespec_str(IDL_PARAM_DCL(tree).param_type_spec);
165 case IDLN_TYPE_SEQUENCE:
167 IDL_tree subtype = IDL_TYPE_SEQUENCE(tree).simple_type_spec;
169 ctmp = forb_cbe_get_typespec_str(subtype);
170 /* We should have built-in alias to make this next line not needed */
171 base = forb_cbe_type_is_builtin(subtype)
172 ? ctmp + strlen("CORBA_") : ctmp;
173 retval = g_strdup_printf( "CORBA_sequence_%s", base);
181 case IDLN_FORWARD_DCL:
183 return forb_cbe_get_typespec_str(IDL_INTERFACE(tree).ident);
184 case IDLN_TYPE_OBJECT:
185 retval = "CORBA_Object";
187 case IDLN_TYPE_TYPECODE:
188 retval = "CORBA_TypeCode";
191 g_error("We were asked to get a typename for a %s",
192 IDL_tree_type_names[IDL_NODE_TYPE(tree)]);
197 return g_strdup (retval);
199 return g_string_free (tmpstr, FALSE);
203 forb_cbe_write_typespec(FILE *of, IDL_tree tree)
205 char *name = forb_cbe_get_typespec_str(tree);
211 Parameters (e.g., arguments to methods, and the return value
212 have some complicated rules in the C mapping for how many
213 levels of pointer and the exact type involved. This function
214 generates the externally visible parameter type.
216 Note the hack below because "const CORBA_string" is not
217 promotable to "const CORBA_char*" (the later the standard
218 to which apps are written, while the former is what would
219 be produced without the hack).
222 forb_cbe_write_param_typespec_str(IDL_tree ts, IDL_ParamRole role)
227 GString *str = g_string_sized_new (23);
228 IDL_tree typedef_spec;
231 n = oidl_param_info (ts, role, &isSlice);
232 name = forb_cbe_get_typespec_str (ts);
234 if ( role == DATA_IN ) {
235 /* We want to check if this is a typedef for CORBA_string so we can do special handling
238 typedef_spec = forb_cbe_get_typespec (ts);
239 typedef_name = forb_cbe_get_typespec_str (typedef_spec);
241 g_string_printf (str, "const %s",
242 !strcmp (typedef_name, "CORBA_string") ?
243 "CORBA_char *" : name);
245 g_free (typedef_name);
247 g_string_printf (str, "%s", name);
252 g_string_append (str, "_slice");
254 for (i = 0; i < n; i++)
255 g_string_append_c (str, '*');
257 return g_string_free (str, FALSE);
261 forb_cbe_write_param_typespec_raw (FILE *of, IDL_tree ts, IDL_ParamRole role)
264 str = forb_cbe_write_param_typespec_str (ts, role);
270 forb_cbe_write_param_typespec(FILE *of, IDL_tree tree) {
271 IDL_tree ts = NULL /* Quiet gcc */;
272 IDL_ParamRole role = 0 /* Quiet gcc */;
274 switch ( IDL_NODE_TYPE(tree) ) {
275 case IDLN_OP_DCL: /* means return value of method */
276 ts = IDL_OP_DCL(tree).op_type_spec;
279 case IDLN_PARAM_DCL: /* one of the parameters */
280 ts = IDL_PARAM_DCL(tree).param_type_spec;
281 role = oidl_attr_to_paramrole(IDL_PARAM_DCL(tree).attr);
284 g_assert_not_reached();
286 forb_cbe_write_param_typespec_raw(of, ts, role);
290 forb_cbe_op_write_proto (FILE *of,
292 const char *nom_prefix,
298 g_assert (IDL_NODE_TYPE(op) == IDLN_OP_DCL);
300 forb_cbe_write_param_typespec (of, op);
302 id = IDL_ns_ident_to_qstring (
303 IDL_IDENT_TO_NS (IDL_INTERFACE (
304 IDL_get_parent_node (op, IDLN_INTERFACE, NULL)).ident), "_", 0);
307 fprintf (of, "\t(*%s%s)", nom_prefix ? nom_prefix : "",
308 IDL_IDENT(IDL_OP_DCL(op).ident).str);
310 fprintf (of, " %s%s_%s", nom_prefix ? nom_prefix : "",
311 id, IDL_IDENT (IDL_OP_DCL (op).ident).str);
316 /* fprintf (of, "PortableServer_Servant _servant, "); */
318 fprintf (of, "%s _obj, ", id);
322 for (sub = IDL_OP_DCL (op).parameter_dcls; sub; sub = IDL_LIST (sub).next) {
323 IDL_tree parm = IDL_LIST (sub).data;
325 forb_cbe_write_param_typespec (of, parm);
327 fprintf (of, " %s, ", IDL_IDENT (IDL_PARAM_DCL (parm).simple_declarator).str);
330 if (IDL_OP_DCL (op).context_expr)
331 fprintf (of, "CORBA_Context _ctx, ");
333 fprintf (of, "CORBA_Environment *ev)");
336 /* Writes the value of the constant in 'tree' to file handle 'of' */
338 forb_cbe_get_const(IDL_tree tree)
340 char *opc = NULL, *retval, *ctmp;
341 GString *tmpstr = g_string_new(NULL);
343 switch(IDL_NODE_TYPE(tree)) {
345 g_string_printf(tmpstr, "%s", IDL_BOOLEAN(tree).value?"CORBA_TRUE":"CORBA_FALSE");
348 g_string_printf(tmpstr, "'\\x%X'", *(unsigned char *)IDL_CHAR(tree).value);
351 g_string_printf(tmpstr, "%f", IDL_FLOAT(tree).value);
354 g_string_printf(tmpstr, "%" IDL_LL "d", IDL_INTEGER(tree).value);
357 g_string_printf(tmpstr, "\"%s\"", IDL_STRING(tree).value);
360 g_string_printf(tmpstr, "L'%ls'", IDL_WIDE_CHAR(tree).value);
362 case IDLN_WIDE_STRING:
363 g_string_printf(tmpstr, "L\"%ls\"", IDL_WIDE_STRING(tree).value);
366 g_string_printf(tmpstr, "(");
367 ctmp = forb_cbe_get_const(IDL_BINOP(tree).left);
368 g_string_append(tmpstr, ctmp);
370 switch(IDL_BINOP(tree).op) {
402 g_string_append_printf(tmpstr, " %s ", opc);
403 ctmp = forb_cbe_get_const(IDL_BINOP(tree).right);
404 g_string_append_printf(tmpstr, "%s)", ctmp);
408 switch(IDL_UNARYOP(tree).op) {
409 case IDL_UNARYOP_PLUS: opc = "+"; break;
410 case IDL_UNARYOP_MINUS: opc = "-"; break;
411 case IDL_UNARYOP_COMPLEMENT: opc = "~"; break;
413 ctmp = forb_cbe_get_const(IDL_UNARYOP(tree).operand);
414 g_string_printf(tmpstr, "%s%s", opc, ctmp);
420 id = IDL_ns_ident_to_qstring(IDL_IDENT_TO_NS(tree), "_", 0);
421 g_string_printf(tmpstr, "%s", id);
426 g_error("We were asked to print a constant for %s", IDL_tree_type_names[tree->_type]);
430 retval = tmpstr->str;
432 g_string_free(tmpstr, FALSE);
438 forb_cbe_write_const(FILE *of, IDL_tree tree)
442 ctmp = forb_cbe_get_const(tree);
443 fprintf(of, "%s", ctmp);
447 /* This is the WORST HACK in the WORLD, really truly, but the C preprocessor doesn't allow us to use
448 strings, so we have to work around it by using individual characters. */
450 forb_cbe_id_define_hack(FILE *fh, const char *def_prefix, const char *def_name, const char *def_value)
453 n = strlen(def_value);
454 for(i = 0; i < n; i++)
455 fprintf(fh, "#define %s_%s_%d '%c'\n", def_prefix, def_name, i, def_value[i]);
459 forb_cbe_id_cond_hack(FILE *fh, const char *def_prefix, const char *def_name, const char *def_value)
462 n = strlen(def_value);
468 for(i = 0; i < n; i++)
469 fprintf(fh, "%s (%s_%s_%d == '%c') \\\n", i?"&&":"", def_prefix, def_name, i, def_value[i]);
475 case IDLN_TYPE_FLOAT: \
476 case IDLN_TYPE_ENUM: \
477 case IDLN_TYPE_BOOLEAN: \
478 case IDLN_TYPE_CHAR: \
479 case IDLN_TYPE_WIDE_CHAR: \
482 #define STRING_TYPES \
484 case IDLN_TYPE_WIDE_STRING
486 #define OBJREF_TYPES \
488 case IDLN_INTERFACE: \
489 case IDLN_FORWARD_DCL
492 forb_cbe_flatten_ref (IDL_ParamRole role, IDL_tree typespec)
496 is_fixed = forb_cbe_type_is_fixed_length (typespec);
500 switch (IDL_NODE_TYPE (typespec)) {
504 case IDLN_TYPE_TYPECODE:
506 return "(gpointer)&";
508 case IDLN_TYPE_STRUCT:
509 case IDLN_TYPE_UNION:
511 case IDLN_TYPE_SEQUENCE:
512 case IDLN_TYPE_ARRAY:
516 case IDLN_TYPE_FIXED:
517 g_error ("Hit evil type %d", IDL_NODE_TYPE (typespec));
522 switch (IDL_NODE_TYPE (typespec)) {
526 case IDLN_TYPE_TYPECODE:
527 case IDLN_TYPE_STRUCT:
528 case IDLN_TYPE_UNION:
529 case IDLN_TYPE_ARRAY:
532 case IDLN_TYPE_SEQUENCE:
536 case IDLN_TYPE_FIXED:
537 g_error ("Hit evil type %d", IDL_NODE_TYPE (typespec));
542 switch (IDL_NODE_TYPE (typespec)) {
546 case IDLN_TYPE_TYPECODE:
550 case IDLN_TYPE_STRUCT:
551 case IDLN_TYPE_UNION:
552 case IDLN_TYPE_ARRAY:
555 else /* drop through */
557 case IDLN_TYPE_SEQUENCE:
562 case IDLN_TYPE_FIXED:
563 g_error ("Hit evil type %d", IDL_NODE_TYPE (typespec));
568 g_error ("No data return handler");
576 forb_cbe_flatten_args (IDL_tree tree, FILE *of, const char *name)
581 for (l = IDL_OP_DCL(tree).parameter_dcls; l;
582 l = IDL_LIST(l).next)
585 fprintf (of, "gpointer %s[%d];\n", name, i);
588 for (l = IDL_OP_DCL(tree).parameter_dcls; l;
589 l = IDL_LIST(l).next) {
590 IDL_tree decl = IDL_LIST (l).data;
591 IDL_tree tspec = forb_cbe_get_typespec (decl);
594 switch(IDL_PARAM_DCL(decl).attr) {
595 case IDL_PARAM_IN: r = DATA_IN; break;
596 case IDL_PARAM_INOUT: r = DATA_INOUT; break;
597 case IDL_PARAM_OUT: r = DATA_OUT; break;
599 g_error("Unknown IDL_PARAM type");
602 fprintf (of, "%s[%d] = %s%s;\n",
604 forb_cbe_flatten_ref (r, tspec),
605 IDL_IDENT (IDL_PARAM_DCL (decl).simple_declarator).str);
611 forb_cbe_unflatten_ref (IDL_ParamRole role, IDL_tree typespec)
617 is_fixed = forb_cbe_type_is_fixed_length (typespec);
619 typestr = forb_cbe_write_param_typespec_str (typespec, role);
623 switch (IDL_NODE_TYPE (typespec)) {
627 case IDLN_TYPE_TYPECODE:
629 retval = g_strdup_printf ("*(%s *)", typestr);
633 case IDLN_TYPE_ARRAY:
634 retval = g_strdup_printf ("(%s_slice *)", typestr);
637 case IDLN_TYPE_STRUCT:
638 case IDLN_TYPE_UNION:
640 case IDLN_TYPE_SEQUENCE:
641 retval = g_strdup_printf ("(%s)", typestr);
645 case IDLN_TYPE_FIXED:
646 g_error ("Hit evil type %d", IDL_NODE_TYPE (typespec));
653 switch (IDL_NODE_TYPE (typespec)) {
654 case IDLN_TYPE_ARRAY:
655 retval = g_strdup_printf ("(%s_slice *)", typestr);
661 case IDLN_TYPE_TYPECODE:
662 case IDLN_TYPE_STRUCT:
663 case IDLN_TYPE_UNION:
666 case IDLN_TYPE_SEQUENCE:
667 retval = g_strdup_printf ("(%s)", typestr);
671 case IDLN_TYPE_FIXED:
672 g_error ("Hit evil type %d", IDL_NODE_TYPE (typespec));
679 switch (IDL_NODE_TYPE (typespec)) {
683 case IDLN_TYPE_TYPECODE:
685 retval = g_strdup_printf ("*(%s *)", typestr);
688 case IDLN_TYPE_ARRAY:
690 retval = g_strdup_printf ("*(%s_slice **)", typestr);
695 case IDLN_TYPE_STRUCT:
696 case IDLN_TYPE_UNION:
698 retval = g_strdup_printf ("*(%s *)", typestr);
703 case IDLN_TYPE_SEQUENCE:
705 retval = g_strdup_printf ("(%s)", typestr);
709 case IDLN_TYPE_FIXED:
710 g_error ("Hit evil type %d", IDL_NODE_TYPE (typespec));
718 g_error ("No data return handler");
729 forb_cbe_unflatten_args (IDL_tree tree, FILE *of, const char *name)
734 for (l = IDL_OP_DCL(tree).parameter_dcls; l;
735 l = IDL_LIST(l).next) {
736 IDL_tree decl = IDL_LIST (l).data;
737 IDL_tree tspec = forb_cbe_get_typespec (decl);
741 switch(IDL_PARAM_DCL(decl).attr) {
742 case IDL_PARAM_IN: r = DATA_IN; break;
743 case IDL_PARAM_INOUT: r = DATA_INOUT; break;
744 case IDL_PARAM_OUT: r = DATA_OUT; break;
746 g_error("Unknown IDL_PARAM type");
749 unflatten = forb_cbe_unflatten_ref (r, tspec);
750 fprintf (of, "%s%s[%d], ", unflatten, name, i++);