]> rtime.felk.cvut.cz Git - frescor/forb.git/blob - forb-idl/forb-idl-c-stubs.c
Start of code generation for FORB
[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                     "    if (_obj->methods) {\n"
43                     "      /* in-proc object */\n"
44                     "      ", iface_id);
45         if (has_retval) {
46                 fprintf(of, FORB_RETVAL_VAR_NAME " = ");
47         }
48         fprintf(of, "forb_%s_methods(obj)->%s(_obj, ",
49                 iface_id, opname_plain);
50         for (sub = IDL_OP_DCL (tree).parameter_dcls; sub; sub = IDL_LIST (sub).next) {
51                 IDL_tree parm = IDL_LIST (sub).data;
52                 fprintf (of, "%s, ", IDL_IDENT (IDL_PARAM_DCL (parm).simple_declarator).str);
53         }
54         fprintf(of, "ev);\n");
55         
56         fprintf(of, "    } else {\n");
57         fprintf(of, "      /* remote object */\n"
58                     "      CDR_Codec codec;\n"
59                     "      CDR_codec_init_static(&codec);\n");
60         fprintf(of, "      forb_prepare_request(&codec, _obj, FORB_METHOD_INDEX(%s));\n",
61                 opname);
62         for (sub = IDL_OP_DCL (tree).parameter_dcls; sub; sub = IDL_LIST (sub).next) {
63                 IDL_tree        parm = IDL_LIST (sub).data;
64                 IDL_ParamRole   role;
65                 role = oidl_attr_to_paramrole(IDL_PARAM_DCL(parm).attr);
66                 if (role == DATA_IN || role == DATA_INOUT) {
67                   char *name = IDL_IDENT(IDL_PARAM_DCL(parm).simple_declarator).str;
68                   fprintf(of, "      ");
69                   forb_cbe_write_typespec(of, IDL_PARAM_DCL(parm).param_type_spec);
70                   fprintf(of, "_serialize(&codec, %s);\n", name);
71                 }
72         }
73         fprintf(of, "      /* TODO: Send request */\n");
74         if (has_retval) {
75           fprintf(of, "      ");
76           forb_cbe_write_typespec(of, IDL_OP_DCL(tree).op_type_spec);
77           fprintf(of, "_deserialize(&codec, &"FORB_RETVAL_VAR_NAME");\n");
78         }
79         for (sub = IDL_OP_DCL (tree).parameter_dcls; sub; sub = IDL_LIST (sub).next) {
80                 IDL_tree        parm = IDL_LIST (sub).data;
81                 IDL_ParamRole   role;
82                 role = oidl_attr_to_paramrole(IDL_PARAM_DCL(parm).attr);
83                 if (role == DATA_OUT || role == DATA_INOUT) {
84                   char *name = IDL_IDENT(IDL_PARAM_DCL(parm).simple_declarator).str;
85                   fprintf(of, "      ");
86                   forb_cbe_write_typespec(of, IDL_PARAM_DCL(parm).param_type_spec);
87                   fprintf(of, "_deserialize(&codec, %s);\n", name);
88                 }
89         }
90         fprintf(of, "    }\n");
91         fprintf(of, "  } else {\n");
92         fprintf(of, "    ev->major = FORB_EX_BAD_OPERATION;\n");
93         fprintf(of, "  }\n");
94         if (has_retval) {
95                 fprintf(of, "  return " FORB_RETVAL_VAR_NAME ";\n");
96         }
97
98         fprintf (of, "}\n\n");
99
100         g_free (iface_id);
101         g_free (opname);
102
103         (*idx)++;
104 }
105
106 static void
107 cs_output_stubs (IDL_tree     tree,
108                  OIDL_C_Info *ci,
109                  int         *idx)
110 {
111         if (!tree)
112                 return;
113
114         switch (IDL_NODE_TYPE (tree)) {
115         case IDLN_MODULE:
116                 cs_output_stubs (IDL_MODULE (tree).definition_list, ci, idx);
117                 break;
118         case IDLN_LIST: {
119                 IDL_tree sub;
120
121                 for (sub = tree; sub; sub = IDL_LIST (sub).next)
122                         cs_output_stubs (IDL_LIST (sub).data, ci, idx);
123                 break;
124                 }
125         case IDLN_ATTR_DCL: {
126 /*              IDL_tree node; */
127       
128 /*              for (node = IDL_ATTR_DCL (tree).simple_declarations; node; node = IDL_LIST (node).next) { */
129 /*                      OIDL_Attr_Info *ai; */
130
131 /*                      ai = IDL_LIST (node).data->data; */
132         
133 /*                      cs_output_stubs (ai->op1, ci, idx); */
134
135 /*                      if (ai->op2) */
136 /*                              cs_output_stubs (ai->op2, ci, idx); */
137 /*              } */
138                 break;
139                 }
140         case IDLN_INTERFACE: {
141                 int real_idx = 0;
142
143                 cs_output_stubs (IDL_INTERFACE (tree).body, ci, &real_idx);
144                 break;
145                 }
146         case IDLN_OP_DCL:
147                 cs_output_stub (tree, ci, idx);
148                 break;
149         default:
150                 break;
151         }
152 }
153
154 void
155 forb_idl_output_c_stubs (IDL_tree       tree,
156                           OIDL_Run_Info *rinfo,
157                           OIDL_C_Info   *ci)
158 {
159         fprintf (ci->fh, OIDL_C_WARNING);
160 /*      fprintf (ci->fh, "#include <string.h>\n"); */
161 /*      fprintf (ci->fh, "#define FORB2_STUBS_API\n"); */
162         fprintf (ci->fh, "#include \"%s.h\"\n\n", ci->base_name);
163
164         cs_output_stubs (tree, ci, NULL);
165 }