]> rtime.felk.cvut.cz Git - frescor/forb.git/blob - forb-idl/forb-idl-c-stubs.c
Shorten names of implementation structure to impl
[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 (_obj->implementation) {\n"
48                     "    /* in-proc object */\n");
49         fprintf(of, "    if (!_obj->interface ||\n"
50                     "        strncmp(_obj->interface->name, \"%s\", %d) != 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                     "    CDR_Codec codec;\n"
69                     "    forb_request_handle_t req;\n"
70                     "    CDR_codec_init_static(&codec);\n"
71                     "    ex_on_fail(CDR_buffer_init(&codec, 256), FORB_EX_NO_MEMORY);\n");
72 /*      fprintf(of, "    forb_prepare_request(&codec, _obj, FORB_METHOD_INDEX(%s));\n", */
73 /*              opname); */
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                   fprintf(of, "    ex_on_fail(");
81                   forb_cbe_write_typespec(of, IDL_PARAM_DCL(parm).param_type_spec);
82                   fprintf(of, "_serialize(&codec, %s%s), FORB_EX_IMP_LIMIT);\n",
83                           role == DATA_IN ? "&":"",
84                           name);
85                 }
86         }
87         fprintf(of, "    forb_send_request(_obj, FORB_METHOD_INDEX(%s), &codec, &req, ev);\n",
88                 opname);
89         fprintf(of, "    if (forb_exception_occured(ev)) goto exception;\n");
90         fprintf(of, "    forb_wait_for_reply(&req, ev);\n");
91         fprintf(of, "    if (forb_exception_occured(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(&codec, &"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                   fprintf(of, "    ");
104                   forb_cbe_write_typespec(of, IDL_PARAM_DCL(parm).param_type_spec);
105                   fprintf(of, "_deserialize(&codec, %s);\n", name);
106                 }
107         }
108         fprintf(of, "exception:\n"
109                     "    CDR_codec_release_buffer(&codec);\n");
110         fprintf(of, "  }\n");
111         if (has_retval) {
112                 fprintf(of, "  return " FORB_RETVAL_VAR_NAME ";\n");
113         }
114
115         fprintf (of, "}\n\n");
116
117         g_free (iface_id);
118         g_free (opname);
119
120         (*idx)++;
121 }
122
123 static void
124 cs_output_stubs (IDL_tree     tree,
125                  OIDL_C_Info *ci,
126                  int         *idx)
127 {
128         if (!tree)
129                 return;
130
131         switch (IDL_NODE_TYPE (tree)) {
132         case IDLN_MODULE:
133                 cs_output_stubs (IDL_MODULE (tree).definition_list, ci, idx);
134                 break;
135         case IDLN_LIST: {
136                 IDL_tree sub;
137
138                 for (sub = tree; sub; sub = IDL_LIST (sub).next)
139                         cs_output_stubs (IDL_LIST (sub).data, ci, idx);
140                 break;
141                 }
142         case IDLN_ATTR_DCL: {
143 /*              IDL_tree node; */
144       
145 /*              for (node = IDL_ATTR_DCL (tree).simple_declarations; node; node = IDL_LIST (node).next) { */
146 /*                      OIDL_Attr_Info *ai; */
147
148 /*                      ai = IDL_LIST (node).data->data; */
149         
150 /*                      cs_output_stubs (ai->op1, ci, idx); */
151
152 /*                      if (ai->op2) */
153 /*                              cs_output_stubs (ai->op2, ci, idx); */
154 /*              } */
155                 break;
156                 }
157         case IDLN_INTERFACE: {
158                 int real_idx = 0;
159                 char *id;
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                 break;
166                 }
167         case IDLN_OP_DCL:
168                 cs_output_stub (tree, ci, idx);
169                 break;
170         default:
171                 break;
172         }
173 }
174
175 void
176 forb_idl_output_c_stubs (IDL_tree       tree,
177                           OIDL_Run_Info *rinfo,
178                           OIDL_C_Info   *ci)
179 {
180         fprintf (ci->fh, OIDL_C_WARNING);
181         fprintf (ci->fh, "#include <string.h>\n");
182 /*      fprintf (ci->fh, "#define FORB2_STUBS_API\n"); */
183         fprintf (ci->fh, "#include \"%s.h\"\n\n", ci->base_name);
184
185         fprintf (ci->fh, "#define ex_on_fail(command, ex) if (!(command)) do { ev->major = (ex); goto exception; } while(0)\n\n");
186
187         cs_output_stubs (tree, ci, NULL);
188 }