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