]> rtime.felk.cvut.cz Git - frescor/forb.git/blob - forb-idl/forb-idl-c-stubs.c
Added check to skeletons for correct interface in in-proc invocation
[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 (_obj->implementation) {\n"
47                     "    /* in-proc object */\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, "((struct %s_implementation*)(_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                     "    CDR_Codec codec;\n"
68                     "    forb_request_handle_t req;\n"
69                     "    CDR_codec_init_static(&codec);\n"
70                     "    ex_on_fail(CDR_buffer_init(&codec, 256), FORB_EX_NO_MEMORY);\n");
71 /*      fprintf(of, "    forb_prepare_request(&codec, _obj, FORB_METHOD_INDEX(%s));\n", */
72 /*              opname); */
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                   fprintf(of, "    ex_on_fail(");
80                   forb_cbe_write_typespec(of, IDL_PARAM_DCL(parm).param_type_spec);
81                   fprintf(of, "_serialize(&codec, %s%s), FORB_EX_IMP_LIMIT);\n",
82                           role == DATA_IN ? "&":"",
83                           name);
84                 }
85         }
86         fprintf(of, "    forb_send_request(_obj, FORB_METHOD_INDEX(%s), &codec, &req, ev);\n",
87                 opname);
88         fprintf(of, "    if (forb_exception_occured(ev)) goto exception;\n");
89         fprintf(of, "    forb_wait_for_reply(&req, ev);\n");
90         fprintf(of, "    if (forb_exception_occured(ev)) goto exception;\n");
91         if (has_retval) {
92           fprintf(of, "    ");
93           forb_cbe_write_typespec(of, IDL_OP_DCL(tree).op_type_spec);
94           fprintf(of, "_deserialize(&codec, &"FORB_RETVAL_VAR_NAME");\n");
95         }
96         for (sub = IDL_OP_DCL (tree).parameter_dcls; sub; sub = IDL_LIST (sub).next) {
97                 IDL_tree        parm = IDL_LIST (sub).data;
98                 IDL_ParamRole   role;
99                 role = oidl_attr_to_paramrole(IDL_PARAM_DCL(parm).attr);
100                 if (role == DATA_OUT || role == DATA_INOUT) {
101                   char *name = IDL_IDENT(IDL_PARAM_DCL(parm).simple_declarator).str;
102                   fprintf(of, "    ");
103                   forb_cbe_write_typespec(of, IDL_PARAM_DCL(parm).param_type_spec);
104                   fprintf(of, "_deserialize(&codec, %s);\n", name);
105                 }
106         }
107         fprintf(of, "exception:\n"
108                     "    CDR_codec_release_buffer(&codec);\n");
109         fprintf(of, "  }\n");
110         if (has_retval) {
111                 fprintf(of, "  return " FORB_RETVAL_VAR_NAME ";\n");
112         }
113
114         fprintf (of, "}\n\n");
115
116         g_free (iface_id);
117         g_free (opname);
118
119         (*idx)++;
120 }
121
122 static void
123 cs_output_stubs (IDL_tree     tree,
124                  OIDL_C_Info *ci,
125                  int         *idx)
126 {
127         if (!tree)
128                 return;
129
130         switch (IDL_NODE_TYPE (tree)) {
131         case IDLN_MODULE:
132                 cs_output_stubs (IDL_MODULE (tree).definition_list, ci, idx);
133                 break;
134         case IDLN_LIST: {
135                 IDL_tree sub;
136
137                 for (sub = tree; sub; sub = IDL_LIST (sub).next)
138                         cs_output_stubs (IDL_LIST (sub).data, ci, idx);
139                 break;
140                 }
141         case IDLN_ATTR_DCL: {
142 /*              IDL_tree node; */
143       
144 /*              for (node = IDL_ATTR_DCL (tree).simple_declarations; node; node = IDL_LIST (node).next) { */
145 /*                      OIDL_Attr_Info *ai; */
146
147 /*                      ai = IDL_LIST (node).data->data; */
148         
149 /*                      cs_output_stubs (ai->op1, ci, idx); */
150
151 /*                      if (ai->op2) */
152 /*                              cs_output_stubs (ai->op2, ci, idx); */
153 /*              } */
154                 break;
155                 }
156         case IDLN_INTERFACE: {
157                 int real_idx = 0;
158
159                 cs_output_stubs (IDL_INTERFACE (tree).body, ci, &real_idx);
160                 break;
161                 }
162         case IDLN_OP_DCL:
163                 cs_output_stub (tree, ci, idx);
164                 break;
165         default:
166                 break;
167         }
168 }
169
170 void
171 forb_idl_output_c_stubs (IDL_tree       tree,
172                           OIDL_Run_Info *rinfo,
173                           OIDL_C_Info   *ci)
174 {
175         fprintf (ci->fh, OIDL_C_WARNING);
176         fprintf (ci->fh, "#include <string.h>\n");
177 /*      fprintf (ci->fh, "#define FORB2_STUBS_API\n"); */
178         fprintf (ci->fh, "#include \"%s.h\"\n\n", ci->base_name);
179
180         fprintf (ci->fh, "#define ex_on_fail(command, ex) if (!(command)) do { ev->major = (ex); goto exception; } while(0)\n\n");
181
182         cs_output_stubs (tree, ci, NULL);
183 }