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