]> rtime.felk.cvut.cz Git - frescor/forb.git/blob - forb-idl/forb-idl-c-stubs.c
Added serialization and deserialization to common.
[frescor/forb.git] / forb-idl / forb-idl-c-stubs.c
1 #include "config.h"
2
3 #include "forb-idl-c-backend.h"
4
5 #include <string.h>
6
7 static void
8 cs_output_stub (IDL_tree     tree,
9                 OIDL_C_Info *ci,
10                 int         *idx)
11 {
12         FILE     *of = ci->fh;
13         IDL_tree  sub;
14         char     *iface_id;
15         char     *opname;
16         char     *opname_plain;
17         gboolean  has_retval, has_args;
18
19         g_return_if_fail (idx != NULL);
20
21         iface_id = IDL_ns_ident_to_qstring (
22                         IDL_IDENT_TO_NS (IDL_INTERFACE (
23                                 IDL_get_parent_node (tree, IDLN_INTERFACE, NULL)
24                                         ).ident), "_", 0);
25         opname_plain = IDL_IDENT(IDL_OP_DCL (tree).ident).str;
26         opname = IDL_ns_ident_to_qstring (IDL_IDENT_TO_NS (IDL_OP_DCL (tree).ident), "_", 0);
27
28         has_retval = IDL_OP_DCL (tree).op_type_spec != NULL;
29         has_args   = IDL_OP_DCL (tree).parameter_dcls != NULL;
30
31         forb_cbe_op_write_proto (of, tree, "", FALSE);
32
33         fprintf (of, "\n{\n");
34
35         if (has_retval) {
36                 fprintf (of, "  ");
37                 forb_cbe_write_param_typespec (of, tree);
38                 fprintf (of, " " FORB_RETVAL_VAR_NAME ";\n");
39         }
40
41         fprintf(of, "  if (_obj->type != %s_type) {\n"
42                     "    ev->major = FORB_EX_BAD_OPERATION;\n", iface_id);
43         if (has_retval) {
44                 fprintf(of, "    return "FORB_RETVAL_VAR_NAME";\n");
45         }
46         fprintf(of, "  }\n"
47                     "  if (_obj->methods) {\n"
48                     "    /* in-proc object */\n"
49                     "    ");
50         if (has_retval) {
51                 fprintf(of, FORB_RETVAL_VAR_NAME " = ");
52         }
53         fprintf(of, "forb_%s_methods(obj)->%s(_obj, ",
54                 iface_id, opname_plain);
55         for (sub = IDL_OP_DCL (tree).parameter_dcls; sub; sub = IDL_LIST (sub).next) {
56                 IDL_tree parm = IDL_LIST (sub).data;
57                 fprintf (of, "%s, ", IDL_IDENT (IDL_PARAM_DCL (parm).simple_declarator).str);
58         }
59         fprintf(of, "ev);\n");
60         
61         fprintf(of, "  } else {\n");
62         fprintf(of, "    /* remote object */\n"
63                     "    CORBA_boolean r;\n"
64                     "    CDR_Codec codec;\n"
65                     "    forb_request_handle req;\n"
66                     "    CDR_codec_init_static(&codec);\n"
67                     "    ex_on_fail(CDR_buffer_init(&codec, 256), FORB_EX_NO_MEMORY);\n");
68 /*      fprintf(of, "    forb_prepare_request(&codec, _obj, FORB_METHOD_INDEX(%s));\n", */
69 /*              opname); */
70         for (sub = IDL_OP_DCL (tree).parameter_dcls; sub; sub = IDL_LIST (sub).next) {
71                 IDL_tree        parm = IDL_LIST (sub).data;
72                 IDL_ParamRole   role;
73                 role = oidl_attr_to_paramrole(IDL_PARAM_DCL(parm).attr);
74                 if (role == DATA_IN || role == DATA_INOUT) {
75                   char *name = IDL_IDENT(IDL_PARAM_DCL(parm).simple_declarator).str;
76                   fprintf(of, "    ex_on_fail(");
77                   forb_cbe_write_typespec(of, IDL_PARAM_DCL(parm).param_type_spec);
78                   fprintf(of, "_serialize(&codec, %s), FORB_EX_IMP_LIMIT);\n", name);
79                 }
80         }
81         fprintf(of, "    forb_send_request(_obj, FORB_METHOD_INDEX(%s), &codec, &req, ev);\n",
82                 opname);
83         fprintf(of, "    if (forb_exception_occured(ev)) goto exception;\n");
84         fprintf(of, "    forb_wait_for_reply(&req, ev);\n");
85         fprintf(of, "    if (forb_exception_occured(ev)) goto exception;\n");
86         if (has_retval) {
87           fprintf(of, "    ");
88           forb_cbe_write_typespec(of, IDL_OP_DCL(tree).op_type_spec);
89           fprintf(of, "_deserialize(&codec, &"FORB_RETVAL_VAR_NAME");\n");
90         }
91         for (sub = IDL_OP_DCL (tree).parameter_dcls; sub; sub = IDL_LIST (sub).next) {
92                 IDL_tree        parm = IDL_LIST (sub).data;
93                 IDL_ParamRole   role;
94                 role = oidl_attr_to_paramrole(IDL_PARAM_DCL(parm).attr);
95                 if (role == DATA_OUT || role == DATA_INOUT) {
96                   char *name = IDL_IDENT(IDL_PARAM_DCL(parm).simple_declarator).str;
97                   fprintf(of, "    ");
98                   forb_cbe_write_typespec(of, IDL_PARAM_DCL(parm).param_type_spec);
99                   fprintf(of, "_deserialize(&codec, %s);\n", name);
100                 }
101         }
102         fprintf(of, "exception:\n"
103                     "    CDR_codec_release_buffer(&codec);\n");
104         fprintf(of, "  }\n");
105         if (has_retval) {
106                 fprintf(of, "  return " FORB_RETVAL_VAR_NAME ";\n");
107         }
108
109         fprintf (of, "}\n\n");
110
111         g_free (iface_id);
112         g_free (opname);
113
114         (*idx)++;
115 }
116
117 static void
118 cs_output_stubs (IDL_tree     tree,
119                  OIDL_C_Info *ci,
120                  int         *idx)
121 {
122         if (!tree)
123                 return;
124
125         switch (IDL_NODE_TYPE (tree)) {
126         case IDLN_MODULE:
127                 cs_output_stubs (IDL_MODULE (tree).definition_list, ci, idx);
128                 break;
129         case IDLN_LIST: {
130                 IDL_tree sub;
131
132                 for (sub = tree; sub; sub = IDL_LIST (sub).next)
133                         cs_output_stubs (IDL_LIST (sub).data, ci, idx);
134                 break;
135                 }
136         case IDLN_ATTR_DCL: {
137 /*              IDL_tree node; */
138       
139 /*              for (node = IDL_ATTR_DCL (tree).simple_declarations; node; node = IDL_LIST (node).next) { */
140 /*                      OIDL_Attr_Info *ai; */
141
142 /*                      ai = IDL_LIST (node).data->data; */
143         
144 /*                      cs_output_stubs (ai->op1, ci, idx); */
145
146 /*                      if (ai->op2) */
147 /*                              cs_output_stubs (ai->op2, ci, idx); */
148 /*              } */
149                 break;
150                 }
151         case IDLN_INTERFACE: {
152                 int real_idx = 0;
153
154                 cs_output_stubs (IDL_INTERFACE (tree).body, ci, &real_idx);
155                 break;
156                 }
157         case IDLN_OP_DCL:
158                 cs_output_stub (tree, ci, idx);
159                 break;
160         default:
161                 break;
162         }
163 }
164
165 void
166 forb_idl_output_c_stubs (IDL_tree       tree,
167                           OIDL_Run_Info *rinfo,
168                           OIDL_C_Info   *ci)
169 {
170         fprintf (ci->fh, OIDL_C_WARNING);
171 /*      fprintf (ci->fh, "#include <string.h>\n"); */
172 /*      fprintf (ci->fh, "#define FORB2_STUBS_API\n"); */
173         fprintf (ci->fh, "#include \"%s.h\"\n\n", ci->base_name);
174
175         fprintf (ci->fh, "#define ex_on_fail(command, ex) if (!(command)) do { ev->major = (ex); goto exception; } while(0)\n\n");
176
177         cs_output_stubs (tree, ci, NULL);
178 }