]> rtime.felk.cvut.cz Git - frescor/frsh-forb.git/blob - src/forb/forb-idl/forb-idl-c-stubs.c
6405e32aa4b544b0bc0606b12c4361ca0c25b1d2
[frescor/frsh-forb.git] / src / forb / 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, "  if (ev) ev->major = FORB_EX_NONE;\n");
47         fprintf(of, "  if (forb_object_is_local(_obj) &&\n"
48                            "forb_get_current_executor() == forb_object_get_executor(_obj)) {\n");
49         fprintf(of, "    if (!_obj->interface ||\n"
50                     "        strncmp(_obj->interface->name, \"%s\", %zd) != 0) {\n"
51                     "      ev->major = FORB_EX_BAD_OPERATION;\n"
52                     "      return %s;\n"
53                     "    }\n", iface_id, strlen(iface_id), has_retval ? FORB_RETVAL_VAR_NAME : "");
54         fprintf(of, "    ");
55         if (has_retval) {
56                 fprintf(of, FORB_RETVAL_VAR_NAME " = ");
57         }
58         fprintf(of, "_%s_impl(_obj)->%s(_obj, ",
59                 iface_id, opname_plain);
60         for (sub = IDL_OP_DCL (tree).parameter_dcls; sub; sub = IDL_LIST (sub).next) {
61                 IDL_tree parm = IDL_LIST (sub).data;
62                 fprintf (of, "%s, ", IDL_IDENT (IDL_PARAM_DCL (parm).simple_declarator).str);
63         }
64         fprintf(of, "ev);\n");
65         
66         fprintf(of, "  } else {\n");
67         fprintf(of, "    /* remote object */\n"
68                     "    forb_request_t req;\n");
69         fprintf(of, "    ex_on_fail(forb_request_init(&req, _obj) == 0, FORB_EX_INTERNAL);\n");
70         fprintf(of, "    forb_iop_prepare_request(&req, \"%s\", FORB_METHOD_INDEX(%s), ev);\n",
71                 iface_id, opname);
72         fprintf(of, "    if (forb_exception_occurred(ev)) goto exception;\n");
73         for (sub = IDL_OP_DCL (tree).parameter_dcls; sub; sub = IDL_LIST (sub).next) {
74                 IDL_tree        parm = IDL_LIST (sub).data;
75                 IDL_ParamRole   role;
76                 role = oidl_attr_to_paramrole(IDL_PARAM_DCL(parm).attr);
77                 if (role == DATA_IN || role == DATA_INOUT) {
78                   char      *name = IDL_IDENT(IDL_PARAM_DCL(parm).simple_declarator).str;
79                   gboolean  isSlice;
80                   IDL_tree  ts = forb_cbe_get_typespec(parm);
81                   int       n = oidl_param_info (ts, role, &isSlice);
82                   fprintf(of, "    ex_on_fail(");
83                   forb_cbe_write_typespec(of, IDL_PARAM_DCL(parm).param_type_spec);
84                   fprintf(of, "_serialize(&req.cdr_request, %s%s), FORB_EX_IMP_LIMIT);\n",
85                           n == 0 ? "&":"", name);
86                 }
87         }
88         fprintf(of, "    forb_request_send(&req, ev);\n");
89         fprintf(of, "    if (forb_exception_occurred(ev)) goto exception;\n");
90         fprintf(of, "    forb_request_wait_for_reply(&req);\n");
91         fprintf(of, "    if (forb_exception_occurred(ev)) goto exception;\n");
92         if (has_retval) {
93           fprintf(of, "    ");
94           forb_cbe_write_typespec(of, IDL_OP_DCL(tree).op_type_spec);
95           fprintf(of, "_deserialize(req.cdr_reply, &"FORB_RETVAL_VAR_NAME");\n");
96         }
97         for (sub = IDL_OP_DCL (tree).parameter_dcls; sub; sub = IDL_LIST (sub).next) {
98                 IDL_tree        parm = IDL_LIST (sub).data;
99                 IDL_ParamRole   role;
100                 role = oidl_attr_to_paramrole(IDL_PARAM_DCL(parm).attr);
101                 if (role == DATA_OUT || role == DATA_INOUT) {
102                   char *name = IDL_IDENT(IDL_PARAM_DCL(parm).simple_declarator).str;
103                   gboolean  isSlice;
104                   IDL_tree  ts = forb_cbe_get_typespec(parm);
105                   int       n = oidl_param_info (ts, role, &isSlice);
106                   if (IDL_NODE_TYPE(forb_cbe_get_typespec(parm)) == IDLN_TYPE_SEQUENCE &&
107                       role == DATA_OUT) { /* Allocate out sequence */
108                           fprintf(ci->fh, "    *%s = forb_malloc(sizeof(**%s));\n", name, name);
109                           fprintf(ci->fh, "    if (!*%s) { ev->major = FORB_EX_NO_MEMORY; goto exception; }\n", name);
110                           fprintf(ci->fh, "    memset(*%s, 0, sizeof(**%s));\n", name, name);
111                           fprintf(ci->fh, "    CORBA_sequence_set_release(*%s, CORBA_TRUE);\n", name);
112                           /* TODO: Free previously allocated parameters on no memory error. */
113                   }
114                   fprintf(of, "    ");
115                   forb_cbe_write_typespec(of, IDL_PARAM_DCL(parm).param_type_spec);
116                   fprintf(ci->fh, "_deserialize(req.cdr_reply, %s%s);\n", n==2?"*":"", name);
117                 }
118         }
119         fprintf(of, "    forb_request_signal_processed(&req);\n"
120                     "exception:\n"
121                     "    forb_request_destroy(&req);\n");
122         fprintf(of, "  }\n");
123         if (has_retval) {
124                 fprintf(of, "  return " FORB_RETVAL_VAR_NAME ";\n");
125         }
126
127         fprintf (of, "}\n\n");
128
129         g_free (iface_id);
130         g_free (opname);
131
132         (*idx)++;
133 }
134
135 static void
136 cs_output_stubs (IDL_tree     tree,
137                  OIDL_C_Info *ci,
138                  int         *idx)
139 {
140         if (!tree)
141                 return;
142
143         switch (IDL_NODE_TYPE (tree)) {
144         case IDLN_MODULE:
145                 cs_output_stubs (IDL_MODULE (tree).definition_list, ci, idx);
146                 break;
147         case IDLN_LIST: {
148                 IDL_tree sub;
149
150                 for (sub = tree; sub; sub = IDL_LIST (sub).next)
151                         cs_output_stubs (IDL_LIST (sub).data, ci, idx);
152                 break;
153                 }
154         case IDLN_ATTR_DCL: {
155 /*              IDL_tree node; */
156       
157 /*              for (node = IDL_ATTR_DCL (tree).simple_declarations; node; node = IDL_LIST (node).next) { */
158 /*                      OIDL_Attr_Info *ai; */
159
160 /*                      ai = IDL_LIST (node).data->data; */
161         
162 /*                      cs_output_stubs (ai->op1, ci, idx); */
163
164 /*                      if (ai->op2) */
165 /*                              cs_output_stubs (ai->op2, ci, idx); */
166 /*              } */
167                 break;
168                 }
169         case IDLN_INTERFACE: {
170                 int real_idx = 0;
171                 char *id;
172                 /* Do not output skeletons for PIDL interfaces */
173                 if ((tree->declspec & IDLF_DECLSPEC_PIDL) == 0) {
174                   id = IDL_ns_ident_to_qstring (IDL_INTERFACE (tree).ident, "_", 0);
175                   fprintf(ci->fh, "#define _%s_impl(obj) ((struct forb_%s_impl*)(obj)->implementation)\n\n",
176                           id, id);
177                   g_free(id);
178                   cs_output_stubs (IDL_INTERFACE (tree).body, ci, &real_idx);
179                 }
180                 break;
181                 }
182         case IDLN_OP_DCL:
183                 cs_output_stub (tree, ci, idx);
184                 break;
185         default:
186                 break;
187         }
188 }
189
190 void
191 forb_idl_output_c_stubs (IDL_tree       tree,
192                           OIDL_Run_Info *rinfo,
193                           OIDL_C_Info   *ci)
194 {
195         fprintf (ci->fh, OIDL_C_WARNING);
196         fprintf (ci->fh, "#include <string.h>\n");
197 /*      fprintf (ci->fh, "#define FORB2_STUBS_API\n"); */
198         fprintf (ci->fh, "#include <forb/forb-internal.h>\n");
199         fprintf (ci->fh, "#include <forb/request.h>\n");
200         fprintf (ci->fh, "#include <forb/iop.h>\n");
201         fprintf (ci->fh, "#include <forb/cdr.h>\n");
202         fprintf (ci->fh, "#include <forb/object.h>\n\n");
203         fprintf (ci->fh, "#include \"%s.h\"\n\n", ci->base_name);
204
205         fprintf (ci->fh, "#define ex_on_fail(command, ex) if (!(command)) do { ev->major = (ex); goto exception; } while(0)\n");
206         fprintf (ci->fh, "#define FORB_REQEST_HDR_SIZE (forb_iop_MESSAGE_HEADER_SIZE + forb_iop_REQUEST_HEADER_SIZE)\n\n");
207
208         cs_output_stubs (tree, ci, NULL);
209 }