]> rtime.felk.cvut.cz Git - frescor/forb.git/blob - forb-idl/forb-idl-c-skels.c
Not finished attempt to support sequences
[frescor/forb.git] / forb-idl / forb-idl-c-skels.c
1 #include "config.h"
2
3 #include "forb-idl-c-backend.h"
4
5 #include <string.h>
6
7 typedef struct {
8         OIDL_C_Info *ci;
9         GSList      *oplist;
10         int          curlevel;
11 } CBESkelInterfaceTraverseInfo;
12
13 typedef struct {
14         char     *iface_id;
15         char     *opname;
16         IDL_tree  op;
17         int       idx;
18 } CBESkelOpInfo;
19
20 static void cbe_skel_do_interface(IDL_tree tree, OIDL_Run_Info *rinfo, OIDL_C_Info *ci);
21
22 static void
23 ck_output_poastuff(IDL_tree tree, OIDL_Run_Info *rinfo, OIDL_C_Info *ci)
24 {
25   if( !tree || (tree->declspec & IDLF_DECLSPEC_PIDL)!=0 ) 
26     return;
27
28   switch(IDL_NODE_TYPE(tree)) {
29   case IDLN_MODULE:
30     ck_output_poastuff(IDL_MODULE(tree).definition_list, rinfo, ci);
31     break;
32   case IDLN_LIST:
33     {
34       IDL_tree sub;
35       for(sub = tree; sub; sub = IDL_LIST(sub).next) {
36         ck_output_poastuff(IDL_LIST(sub).data, rinfo, ci);
37       }
38     }
39     break;
40   case IDLN_INTERFACE:
41     cbe_skel_do_interface(tree, rinfo, ci);
42     break;
43   default:
44     break;
45   }
46 }
47
48 /* static gint */
49 /* cbe_skel_compare_op_dcls(CBESkelOpInfo *op1, CBESkelOpInfo *op2) */
50 /* { */
51 /*   return strcmp(op1->opname, op2->opname); */
52 /* } */
53
54 /* static void */
55 /* cbe_skel_free_op_info(CBESkelOpInfo *op) */
56 /* { */
57 /*   g_free(op->opname); */
58 /*   g_free(op->iface_id); */
59 /*   g_free(op); */
60 /* } */
61
62 /* static void */
63 /* cbe_skel_interface_add_relayer (IDL_tree intf, CBESkelInterfaceTraverseInfo *iti) */
64 /* { */
65 /*   CBESkelOpInfo *newopi; */
66 /*   IDL_tree curitem, curdcl, curattr, curattrdcl; */
67 /*   char *iface_id; */
68 /*   int   idx = 0; */
69
70 /*   iface_id = */
71 /*     IDL_ns_ident_to_qstring(IDL_IDENT_TO_NS(IDL_INTERFACE(intf).ident), */
72 /*                          "_", 0); */
73
74 /*   for(curitem = IDL_INTERFACE(intf).body; curitem; */
75 /*       curitem = IDL_LIST(curitem).next) { */
76 /*     curdcl = IDL_LIST(curitem).data; */
77
78 /*     switch(IDL_NODE_TYPE(curdcl)) { */
79 /*     case IDLN_OP_DCL: */
80 /*       newopi = g_new0(CBESkelOpInfo, 1); */
81 /*       newopi->iface_id = g_strdup(iface_id); */
82 /*       newopi->opname = g_strdup(IDL_IDENT(IDL_OP_DCL(curdcl).ident).str); */
83 /*       newopi->idx = idx++; */
84 /*       iti->oplist = g_slist_insert_sorted(iti->oplist, newopi, */
85 /*                                        (GCompareFunc)cbe_skel_compare_op_dcls); */
86 /*       break; */
87 /*     case IDLN_ATTR_DCL: */
88 /*       for(curattr = IDL_ATTR_DCL(curdcl).simple_declarations; */
89 /*        curattr; curattr = IDL_LIST(curattr).next) { */
90 /*      curattrdcl = IDL_LIST(curattr).data; */
91
92 /*      newopi = g_new0(CBESkelOpInfo, 1); */
93 /*      newopi->iface_id = g_strdup(iface_id); */
94 /*      newopi->opname = g_strdup_printf("_get_%s", IDL_IDENT(curattrdcl).str); */
95 /*      newopi->idx = idx++; */
96 /*      iti->oplist = g_slist_insert_sorted(iti->oplist, newopi, */
97 /*                                          (GCompareFunc)cbe_skel_compare_op_dcls); */
98 /*      if(!IDL_ATTR_DCL(curdcl).f_readonly) { */
99 /*        newopi = g_new0(CBESkelOpInfo, 1); */
100 /*        newopi->iface_id = g_strdup(iface_id); */
101 /*        newopi->opname = g_strdup_printf("_set_%s", IDL_IDENT(curattrdcl).str); */
102 /*        newopi->idx = idx++; */
103 /*        iti->oplist = g_slist_insert_sorted(iti->oplist, newopi, */
104 /*                                            (GCompareFunc)cbe_skel_compare_op_dcls); */
105 /*      } */
106 /*       } */
107 /*       break; */
108 /*     default: */
109 /*       break; */
110 /*     } */
111 /*   } */
112
113 /*   g_free(iface_id); */
114 /* } */
115
116 /* static void */
117 /* cbe_skel_interface_print_relayers(const CBESkelInterfaceTraverseInfo *iti) */
118 /* { */
119 /*   CBESkelInterfaceTraverseInfo subiti = *iti; */
120 /*   GSList *curnode; */
121 /*   CBESkelOpInfo *opi; */
122 /*   char curchar; */
123
124 /*   curnode = iti->oplist; */
125 /*   subiti.curlevel = iti->curlevel+1; */
126 /*   fprintf(iti->ci->fh, "switch(opname[%d]) {\n", iti->curlevel); */
127 /*   while(curnode) { */
128 /*     opi = (CBESkelOpInfo *)curnode->data; */
129 /*     if(iti->curlevel > strlen(opi->opname)) { */
130 /*       curnode = g_slist_next(curnode); */
131 /*       continue; */
132 /*     } */
133 /*     curchar = opi->opname[iti->curlevel]; */
134 /*     if(curchar) */
135 /*       fprintf(iti->ci->fh, "case '%c':\n", curchar); */
136 /*     else */
137 /*       fprintf(iti->ci->fh, "case '\\0':\n"); */
138 /*     subiti.oplist = NULL; */
139 /*     while(curnode && ((CBESkelOpInfo *)curnode->data)->opname[iti->curlevel] */
140 /*        == curchar) { */
141 /*       subiti.oplist = g_slist_append(subiti.oplist, curnode->data); */
142 /*       curnode = g_slist_next(curnode); */
143 /*     } */
144
145 /*     if(g_slist_length(subiti.oplist) > 1) { */
146 /*       if(curchar) */
147 /*      cbe_skel_interface_print_relayers(&subiti); */
148 /*       else */
149 /*      g_error("two ops with same name!!!!"); */
150 /*     } else { */
151 /*       if(strlen(opi->opname + iti->curlevel)) */
152 /*      fprintf(iti->ci->fh, "if(strcmp((opname + %d), \"%s\")) break;\n", */
153 /*              iti->curlevel + 1, opi->opname + iti->curlevel+1); */
154 /*       fprintf(iti->ci->fh, "*impl = (gpointer)servant->vepv->%s_epv->%s;\n", */
155 /*            opi->iface_id, opi->opname); */
156 /*       fprintf(iti->ci->fh, "{Forb_IInterface *volatile _t_=&%s__iinterface;*m_data = (gpointer)&_t_->methods._buffer [%d];}\n", */
157 /*            opi->iface_id, opi->idx); */
158 /*       fprintf(iti->ci->fh, "return (ForbSmallSkeleton)_FORB_skel_small_%s_%s;\n", */
159 /*            opi->iface_id, opi->opname); */
160 /*     } */
161 /*     fprintf(iti->ci->fh, "break;\n"); */
162 /*     g_slist_free(subiti.oplist); */
163 /*   } */
164 /*   fprintf(iti->ci->fh, "default: break; \n}\n"); */
165 /* } */
166
167 /* static void */
168 /* cbe_skel_interface_print_relayer(IDL_tree tree, OIDL_Run_Info *rinfo, OIDL_C_Info *ci) */
169 /* { */
170 /*   char *id; */
171 /*   CBESkelInterfaceTraverseInfo iti; */
172
173 /*   id = IDL_ns_ident_to_qstring(IDL_IDENT_TO_NS(IDL_INTERFACE(tree).ident), "_", 0); */
174 /*   fprintf(ci->fh, "static ForbSmallSkeleton get_skel_small_%s(POA_%s *servant,\nconst char *opname," */
175 /*        "gpointer *m_data, gpointer *impl)\n{\n", id, id); */
176
177 /*   iti.ci = ci; */
178 /*   iti.oplist = NULL; */
179 /*   iti.curlevel = 0; */
180
181 /*   IDL_tree_traverse_parents(tree, */
182 /*                          (GFunc)cbe_skel_interface_add_relayer, &iti); */
183
184 /*   cbe_skel_interface_print_relayers(&iti); */
185
186 /*   g_slist_foreach(iti.oplist, (GFunc)cbe_skel_free_op_info, NULL); */
187 /*   g_slist_free(iti.oplist); */
188
189 /*   fprintf(ci->fh, "return NULL;\n"); */
190 /*   fprintf(ci->fh, "}\n\n"); */
191
192 /*   g_free(id); */
193 /* } */
194
195 typedef struct {
196         OIDL_C_Info *ci;
197         const char  *for_id;
198 } CBESkelVEPVMapTraverseInfo;
199
200 /* static void */
201 /* cbe_skel_interface_print_vepvmap_offsets (IDL_tree node, CBESkelVEPVMapTraverseInfo *ti) */
202 /* { */
203 /*      char *id; */
204 /*      id = IDL_ns_ident_to_qstring (IDL_IDENT_TO_NS (IDL_INTERFACE (node).ident), "_", 0); */
205 /*      fprintf (ti->ci->fh, "(CORBA_unsigned_long) %s__classid,\n", id); */
206 /*      fprintf (ti->ci->fh, "FORB_VEPV_OFFSET (POA_%s__vepv, %s_epv),\n", ti->for_id, id); */
207 /*      g_free (id); */
208 /* } */
209
210 static void ck_output_op_skel(IDL_tree op, char *intf_id, OIDL_Run_Info *rinfo, OIDL_C_Info *ci)
211 {
212         char *id, *id_plain;
213         IDL_tree sub;
214         gboolean  has_retval, has_args;
215
216         has_retval = IDL_OP_DCL (op).op_type_spec != NULL;
217         has_args   = IDL_OP_DCL (op).parameter_dcls != NULL;
218         
219         id = IDL_ns_ident_to_qstring(IDL_IDENT_TO_NS(IDL_OP_DCL(op).ident), "_", 0);
220         id_plain = IDL_IDENT(IDL_OP_DCL(op).ident).str;
221         fprintf(ci->fh, "static void\n"
222                 "forb_skel_%s(CDR_Codec *cin, CDR_Codec *cout, %s _obj, struct forb_env *env)\n"
223                 "{\n",
224                 id, intf_id);
225 /*      fprintf(ci->fh, "  if (_obj->interface != %s_interface) {\n", intf_id); */
226 /*      fprintf(ci->fh, "    env->major = FORB_EX_BAD_OPERATION;\n" */
227 /*                      "    return;\n" */
228 /*                      "  }\n"); */
229         fprintf(ci->fh, "  if (_%s_impl(_obj) && _%s_impl(_obj)->%s) {\n", intf_id, intf_id, id_plain);
230         for (sub = IDL_OP_DCL (op).parameter_dcls; sub; sub = IDL_LIST (sub).next) {
231                 IDL_tree        parm = IDL_LIST (sub).data;
232                 char *name = IDL_IDENT(IDL_PARAM_DCL(parm).simple_declarator).str;
233                 fprintf(ci->fh, "    ");
234                 forb_cbe_write_typespec(ci->fh, IDL_PARAM_DCL(parm).param_type_spec);
235                 fprintf(ci->fh, " %s;\n", name);
236         }
237         if (has_retval) {
238                 fprintf (ci->fh, "    ");
239                 forb_cbe_write_param_typespec (ci->fh, op);
240                 fprintf (ci->fh, " _forb_ret;\n");
241         }
242         for (sub = IDL_OP_DCL (op).parameter_dcls; sub; sub = IDL_LIST (sub).next) {
243                 IDL_tree        parm = IDL_LIST (sub).data;
244                 IDL_ParamRole   role;
245                 role = oidl_attr_to_paramrole(IDL_PARAM_DCL(parm).attr);
246                 if (role == DATA_IN || role == DATA_INOUT) {
247                         char *name = IDL_IDENT(IDL_PARAM_DCL(parm).simple_declarator).str;
248                         fprintf(ci->fh, /* "  r = " */ "    ");
249                         forb_cbe_write_typespec(ci->fh, IDL_PARAM_DCL(parm).param_type_spec);
250                         fprintf(ci->fh, "_deserialize(cin, &%s);\n", name);
251 /*                      fprintf(ci->fh, "    if (!r) { ev->major = FORB_EX_IMP_LIMIT; goto exception; }\n"); */
252                 }
253         }
254         fprintf(ci->fh, "    ");
255         if (has_retval)
256                 fprintf(ci->fh, "_forb_ret = ");
257         fprintf(ci->fh, "_%s_impl(_obj)->%s(_obj, ",
258                 intf_id, id_plain);
259         for (sub = IDL_OP_DCL (op).parameter_dcls; sub; sub = IDL_LIST (sub).next) {
260                 IDL_tree        parm = IDL_LIST (sub).data;
261                 IDL_ParamRole   role;
262                 role = oidl_attr_to_paramrole(IDL_PARAM_DCL(parm).attr);
263                 char *name = IDL_IDENT(IDL_PARAM_DCL(parm).simple_declarator).str;
264                 /* FIXME: This pointer types also depends on type -
265                  * use something like in
266                  * forb_cbe_write_param_typespec_str() */
267                 if (role == DATA_OUT || role == DATA_INOUT)
268                         fprintf(ci->fh, "&");
269                 fprintf(ci->fh, "%s, ", name);
270         }
271         fprintf(ci->fh, "env);\n");
272         fprintf(ci->fh,
273                 "    if (forb_exception_occured(env)) goto exception;\n");
274         if (has_retval) {
275           fprintf(ci->fh, "    ");
276           forb_cbe_write_typespec(ci->fh, IDL_OP_DCL(op).op_type_spec);
277           fprintf(ci->fh, "_serialize(cout, &_forb_ret);\n");
278         }
279         for (sub = IDL_OP_DCL (op).parameter_dcls; sub; sub = IDL_LIST (sub).next) {
280                 IDL_tree        parm = IDL_LIST (sub).data;
281                 IDL_ParamRole   role;
282                 role = oidl_attr_to_paramrole(IDL_PARAM_DCL(parm).attr);
283                 if (role == DATA_OUT || role == DATA_INOUT) {
284                   char *name = IDL_IDENT(IDL_PARAM_DCL(parm).simple_declarator).str;
285                   fprintf(ci->fh, "    ");
286                   forb_cbe_write_typespec(ci->fh, IDL_PARAM_DCL(parm).param_type_spec);
287                   fprintf(ci->fh, "_serialize(cout, &%s);\n", name);
288                 }
289         }
290         fprintf(ci->fh, "exception:;\n");
291         fprintf(ci->fh, "  } else {\n");
292         fprintf(ci->fh, "    env->major = FORB_EX_NO_IMPLEMENT;\n");
293         fprintf(ci->fh, "  }\n");
294         fprintf(ci->fh, "}\n\n");
295         g_free(id);
296 }
297
298 static void
299 cbe_skel_do_interface(IDL_tree tree, OIDL_Run_Info *rinfo, OIDL_C_Info *ci)
300 {
301   char *id;
302   IDL_tree pnt;
303   IDL_tree sub;
304   //int i;
305   //CBESkelVEPVMapTraverseInfo ti;
306   int num_methods = 0;
307
308   /* PIDL methods dont have normal skel functions. */
309   for ( pnt=tree; pnt; pnt=IDL_NODE_UP(pnt) ) {
310       if ( pnt->declspec & IDLF_DECLSPEC_PIDL )
311         return;
312   }
313
314   id = IDL_ns_ident_to_qstring(IDL_IDENT_TO_NS(IDL_INTERFACE(tree).ident), "_", 0);
315
316   fprintf(ci->fh, "#define _%s_impl(obj) ((struct forb_%s_implementation*)(obj)->implementation)\n\n", id, id);
317   fprintf(ci->fh, "static const struct forb_interface %s_interface;\n\n", id);
318   
319   for(sub = IDL_INTERFACE(tree).body; sub; sub = IDL_LIST(sub).next) {
320     IDL_tree cur = IDL_LIST(sub).data;
321     
322     switch(IDL_NODE_TYPE(cur)) {
323     case IDLN_OP_DCL:
324       ck_output_op_skel(cur, id, rinfo, ci);
325       num_methods++;
326       break;
327     default:
328       break;
329     }
330   }
331
332   fprintf(ci->fh, "static const forb_skel_func %s_skeletons[%d] = {\n",
333           id, num_methods);
334   for(sub = IDL_INTERFACE(tree).body; sub; sub = IDL_LIST(sub).next) {
335     IDL_tree cur = IDL_LIST(sub).data;
336     
337     switch(IDL_NODE_TYPE(cur)) {
338     case IDLN_OP_DCL: {
339       char *op_id;
340       op_id = IDL_ns_ident_to_qstring(IDL_IDENT_TO_NS(IDL_OP_DCL(cur).ident), "_", 0);
341       fprintf(ci->fh, "  forb_skel_%s,\n", op_id);
342       g_free(op_id);
343       break;
344     }
345     default:
346       break;
347     }
348   }
349   fprintf(ci->fh, "};\n\n");
350
351   fprintf(ci->fh, "static const struct forb_interface %s_interface = {\n", id);
352   fprintf(ci->fh, "  .name = \"%s\",\n", id);
353   fprintf(ci->fh, "  .num_methods = %d,\n", num_methods);
354   fprintf(ci->fh, "  .skeletons = %s_skeletons,\n", id);
355   fprintf(ci->fh, "  .type_hash = 0 /* not implemented */\n");
356   fprintf(ci->fh, "};\n\n");
357
358   fprintf(ci->fh, "void forb_register_%s_interface(void)\n"
359                   "{\n"
360                   "  forb_register_interface(&%s_interface);\n"
361                   "}\n\n", id, id);
362
363   fprintf(ci->fh, "%s forb_%s_new(void *instance_data, "
364                   "const struct forb_%s_implementation *impl)\n"
365                   "{\n"
366                   "  %s obj = forb_malloc(sizeof(*obj));\n"
367                   "  if (obj) {\n"
368                   "    obj->instance_data = instance_data;\n"
369                   "    obj->implementation = impl;\n"
370                   "    obj->interface = &%s_interface;\n"
371                   "  }\n"
372                   "  return obj;\n"
373           "}\n\n", id, id, id, id, id);
374   fprintf(ci->fh, "\n");
375   
376
377 /*   cbe_skel_interface_print_relayer(tree, rinfo, ci); */
378
379 /*   fprintf(ci->fh, */
380 /*        "void POA_%s__init(PortableServer_Servant servant,\nCORBA_Environment *env)\n", */
381 /*        id); */
382 /*   fprintf(ci->fh, "{\n"); */
383 /*   fprintf(ci->fh,"  static PortableServer_ClassInfo class_info = {"); */
384
385 /*   fprintf(ci->fh, "NULL, (Forb_small_impl_finder)&get_skel_small_%s, ", id); */
386
387 /*   fprintf(ci->fh,"\"%s\", &%s__classid, NULL, &%s__iinterface};\n", */
388 /*        IDL_IDENT(IDL_INTERFACE(tree).ident).repo_id, id, id); */
389
390 /*   fprintf(ci->fh,"  PortableServer_ServantBase__init (" */
391 /*               "       ((PortableServer_ServantBase *)servant), env);\n"); */
392
393 /*   for(curitem = IDL_INTERFACE(tree).inheritance_spec; curitem; */
394 /*       curitem = IDL_LIST(curitem).next) { */
395 /*     id2 = IDL_ns_ident_to_qstring(IDL_IDENT_TO_NS(IDL_LIST(curitem).data), */
396 /*                                "_", 0); */
397 /*     fprintf(ci->fh, "  POA_%s__init(servant, env);\n", id2); */
398 /*     g_free(id2); */
399 /*   } */
400
401   /* registering after other __inits() makes the classids increment nicely. */
402 /*   fprintf (ci->fh, "   Forb_skel_class_register (&class_info,\n"); */
403 /*   fprintf (ci->fh, "   (PortableServer_ServantBase *)servant, POA_%s__fini,\n", id); */
404 /*   fprintf (ci->fh, "   FORB_VEPV_OFFSET (POA_%s__vepv, %s_epv),\n", id, id); */
405 /*   ti.for_id = id; ti.ci = ci; */
406 /*   IDL_tree_traverse_parents_full (tree, (GFunc) cbe_skel_interface_print_vepvmap_offsets, &ti, FALSE); */
407 /*   fprintf (ci->fh, "   (CORBA_unsigned_long) 0);"); */
408
409 /*   fprintf(ci->fh, "}\n\n"); */
410
411 /*   fprintf(ci->fh, */
412 /*        "void POA_%s__fini(PortableServer_Servant servant,\nCORBA_Environment *env)\n", */
413 /*        id); */
414 /*   fprintf(ci->fh, "{\n"); */
415 /*   if(IDL_INTERFACE(tree).inheritance_spec) */
416 /*     { */
417 /*       for(i = IDL_list_length(IDL_INTERFACE(tree).inheritance_spec) - 1; */
418 /*        i >= 0; i--) { */
419 /*      curitem = IDL_list_nth(IDL_INTERFACE(tree).inheritance_spec, i); */
420 /*      id2 = IDL_ns_ident_to_qstring(IDL_IDENT_TO_NS(IDL_LIST(curitem).data), */
421 /*                                    "_", 0); */
422 /*      /\* XXX fixme - this is going to call ServantBase__fini multiple times *\/ */
423 /*      fprintf(ci->fh, "  POA_%s__fini(servant, env);\n", */
424 /*              id2); */
425 /*      g_free(id2); */
426 /*       } */
427 /*     } */
428 /*   fprintf(ci->fh, "  PortableServer_ServantBase__fini(servant, env);\n"); */
429 /*   fprintf(ci->fh, "}\n\n"); */
430
431   g_free(id);
432 }
433
434 void
435 forb_idl_output_c_skeletons (IDL_tree       tree,
436                               OIDL_Run_Info *rinfo,
437                               OIDL_C_Info   *ci)
438 {
439         fprintf (ci->fh, OIDL_C_WARNING);
440 /*      fprintf (ci->fh, "#include <string.h>\n"); */
441 /*      fprintf (ci->fh, "#define FORB2_STUBS_API\n"); */
442         fprintf (ci->fh, "#include \"%s.h\"\n\n", ci->base_name);
443
444         ck_output_poastuff (tree, rinfo, ci);
445 }