]> rtime.felk.cvut.cz Git - orte.git/blob - orte/idl-compiler/orte-idl-c-impls.c
JORTE: fraction computation
[orte.git] / orte / idl-compiler / orte-idl-c-impls.c
1 //#include "config.h"
2 #include "orte-idl-c-backend.h"
3
4 #include <string.h>
5 #include <ctype.h>
6
7 /* ch = C header */
8 static void ci_output_impls (IDL_tree tree,OIDL_Run_Info *rinfo,OIDL_C_Info *ci);
9
10
11 void
12 orte_idl_output_c_impls (IDL_tree tree, OIDL_Run_Info *rinfo, OIDL_C_Info *ci)
13 {
14   fprintf (ci->fh, OIDL_C_WARNING);
15
16   fprintf(ci->fh, "#include \"%s.h\"\n\n",ci->base_name);
17
18   /* Do all the serialization, deserialization etc. */
19   ci_output_impls(tree, rinfo, ci);
20
21 }
22
23 static void
24 ci_output_var(IDL_tree val, IDL_tree name, OIDL_C_Info *ci, int type)
25 {
26   IDL_tree curitem;
27   unsigned dim=0;
28   if (IDL_NODE_TYPE(name) == IDLN_TYPE_ARRAY) {
29     for(curitem = IDL_TYPE_ARRAY(name).size_list, dim=0;
30         curitem;
31         curitem = IDL_LIST(curitem).next, dim++) {
32         /* For each dimension */
33       fprintf(ci->fh, "  unsigned i%d;\n", dim);
34       fprintf(ci->fh, "  for (i%d=0; i%d < %" IDL_LL "d; i%d++) {\n",
35               dim, dim, IDL_INTEGER(IDL_LIST(curitem).data).value,
36               dim);
37       }
38   }
39   
40   fprintf(ci->fh, "  ");
41   orte_cbe_write_typespec(ci->fh, val);
42
43   switch (type) {
44   case 0:
45     fprintf(ci->fh, "_serialize(cdrCodec,&(object->");
46     break;      
47   case 1:
48     fprintf(ci->fh, "_deserialize(cdrCodec,&(object->");
49     break;      
50   default:
51     break;
52   }
53
54   switch(IDL_NODE_TYPE(name)) {
55   case IDLN_IDENT:
56     fprintf(ci->fh, "%s", IDL_IDENT(name).str);
57     break;
58   case IDLN_TYPE_ARRAY:
59     {
60       fprintf(ci->fh, "%s", IDL_IDENT(IDL_TYPE_ARRAY(name).ident).str);
61       for(curitem = IDL_TYPE_ARRAY(name).size_list, dim=0;
62           curitem;
63           curitem = IDL_LIST(curitem).next, dim++) {
64         fprintf(ci->fh, "[i%d]", dim);
65       }
66     }
67     break;
68   default:
69     g_error("Weird varname - %s", IDL_tree_type_names[IDL_NODE_TYPE(name)]);
70     break;
71   }
72
73 //  if (type==1)
74     fprintf(ci->fh, ")");
75
76   fprintf(ci->fh, ");\n");
77   while (dim-- > 0)
78     fprintf(ci->fh, "  }");
79   fprintf(ci->fh, "\n");
80     
81 }
82
83 static void
84 ci_output_impls_struct(IDL_tree tree, OIDL_Run_Info *rinfo, OIDL_C_Info *ci)
85 {
86   char *id;
87   IDL_tree cur,curmem,cur_tspec;
88
89   id = IDL_ns_ident_to_qstring(IDL_IDENT_TO_NS(IDL_TYPE_STRUCT(tree).ident), 
90     "_", 0);
91
92   /* serialize */
93   fprintf(ci->fh, "/****************************************************************/\n");
94   fprintf(ci->fh, "/* struct - %-50s  */\n", id);
95   fprintf(ci->fh, "/****************************************************************/\n\n");
96   
97   fprintf(ci->fh, "void %s_serialize(CDR_Codec *cdrCodec,%s *object) {\n", id, id);
98 //  fprintf(ci->fh, "  %s *object=(%s*)instance;\n\n", id, id);
99
100   for(cur = IDL_TYPE_STRUCT(tree).member_list; cur; cur = IDL_LIST(cur).next) {
101     for(curmem = IDL_MEMBER(IDL_LIST(cur).data).dcls; curmem; curmem = IDL_LIST(curmem).next) {
102       ci_output_var(IDL_MEMBER(IDL_LIST(cur).data).type_spec, IDL_LIST(curmem).data, ci, 0);
103     }
104   }
105   fprintf(ci->fh, "}\n\n");
106
107   /* deserialize */
108   fprintf(ci->fh, "void\n%s_deserialize(CDR_Codec *cdrCodec,%s *object) {\n", id, id);
109 //  fprintf(ci->fh, "  %s *object=(%s*)instance;\n\n", id, id);
110
111   for(cur = IDL_TYPE_STRUCT(tree).member_list; cur; cur = IDL_LIST(cur).next) {
112     for(curmem = IDL_MEMBER(IDL_LIST(cur).data).dcls; curmem; curmem = IDL_LIST(curmem).next) {
113       ci_output_var(IDL_MEMBER(IDL_LIST(cur).data).type_spec, IDL_LIST(curmem).data, ci, 1);
114     }
115   }
116   fprintf(ci->fh, "}\n\n");
117
118   /* get_max_size */
119   fprintf(ci->fh, "int\n%s_get_max_size(ORTEGetMaxSizeParam *gms, int num) {\n", id);
120
121   fprintf(ci->fh, "  int loop_lim=2;\n");
122   fprintf(ci->fh, "  int csize_save;\n");
123
124   fprintf(ci->fh, "  while(num) {\n");
125   fprintf(ci->fh, "    if (!loop_lim--) {\n");
126   fprintf(ci->fh, "      gms->csize+=num*(gms->csize-csize_save);\n");
127   fprintf(ci->fh, "      return gms->csize;\n");
128   fprintf(ci->fh, "    }\n");
129   fprintf(ci->fh, "    num--;\n");
130   fprintf(ci->fh, "    csize_save=gms->csize;\n");
131
132   for(cur = IDL_TYPE_STRUCT(tree).member_list; cur; cur = IDL_LIST(cur).next) {
133     for(curmem = IDL_MEMBER(IDL_LIST(cur).data).dcls; curmem; curmem = IDL_LIST(curmem).next) {
134       IDL_tree name = IDL_LIST(curmem).data;
135        fprintf(ci->fh, "    ");
136        orte_cbe_write_typespec(ci->fh, IDL_MEMBER(IDL_LIST(cur).data).type_spec);
137        fprintf(ci->fh, "_get_max_size(gms, ");
138
139        cur_tspec=IDL_MEMBER(IDL_LIST(cur).data).type_spec;
140        if (IDL_NODE_TYPE (cur_tspec) == IDLN_TYPE_STRING) {
141             if (IDL_TYPE_STRING (cur_tspec).positive_int_const) {
142                 int length = IDL_INTEGER (IDL_TYPE_STRING (cur_tspec).positive_int_const).value;
143                 fprintf(ci->fh, "%d",length);                 
144             }
145        } else if (IDL_NODE_TYPE(name) == IDLN_TYPE_ARRAY) {
146          unsigned dim;
147          IDL_tree curitem;
148          for(curitem = IDL_TYPE_ARRAY(name).size_list, dim=0;
149              curitem;
150              curitem = IDL_LIST(curitem).next, dim++) {
151            if (dim>0) fprintf(ci->fh, "*");
152            fprintf(ci->fh, "%" IDL_LL "d",
153                    IDL_INTEGER(IDL_LIST(curitem).data).value);
154          }
155        } else {
156          fprintf(ci->fh, "1");                 
157        }
158        fprintf(ci->fh, ");\n");
159     }
160   }
161   fprintf(ci->fh, "  }\n");
162   fprintf(ci->fh, "  return gms->csize;\n");
163   fprintf(ci->fh, "}\n\n");
164
165   /* get_max_size */
166   fprintf(ci->fh, "Boolean\n%s_type_register(ORTEDomain *d) {\n", id);
167   fprintf(ci->fh, "  Boolean ret;\n\n");
168   fprintf(ci->fh, "  ret=ORTETypeRegisterAdd(d,\n");
169   fprintf(ci->fh, "                          \"%s\",\n",id);
170   fprintf(ci->fh, "                          (ORTETypeSerialize)%s_serialize,\n",id);
171   fprintf(ci->fh, "                          (ORTETypeDeserialize)%s_deserialize,\n",id);
172   fprintf(ci->fh, "                          %s_get_max_size,\n",id);
173   fprintf(ci->fh, "                          0);\n");
174   fprintf(ci->fh, "  return ret;\n");
175   fprintf(ci->fh, "}\n\n");
176
177   /* Scan for any nested decls */
178 /*  for(cur = IDL_TYPE_STRUCT(tree).member_list; cur; cur = IDL_LIST(cur).next) {
179     IDL_tree ts;
180     ts = IDL_MEMBER(IDL_LIST(cur).data).type_spec;
181     switch (IDL_NODE_TYPE (ts)) {
182         case IDLN_TYPE_STRUCT:
183                 ch_output_decl(ts, rinfo, ci);
184                 break;
185         default:
186                 break;
187         }
188   }
189
190   g_free(id);*/
191 }
192
193
194 static void
195 ci_output_impls (IDL_tree       tree,
196                  OIDL_Run_Info *rinfo,
197                  OIDL_C_Info   *ci)
198 {
199         if (!tree)
200                 return;
201
202         switch (IDL_NODE_TYPE (tree)) {
203         case IDLN_TYPE_STRUCT:
204                 ci_output_impls_struct (tree, rinfo, ci);
205                 break;
206         default:
207                 break;
208         }
209
210         switch (IDL_NODE_TYPE (tree)) {
211         case IDLN_MODULE:
212                 ci_output_impls (IDL_MODULE (tree).definition_list, rinfo, ci);
213                 break;
214         case IDLN_LIST: {
215                 IDL_tree sub;
216
217                 for (sub = tree; sub; sub = IDL_LIST (sub).next) {
218                         ci_output_impls (IDL_LIST (sub).data, rinfo, ci);
219                 }
220                 }
221                 break;
222         case IDLN_INTERFACE:
223                 ci_output_impls (IDL_INTERFACE (tree).body, rinfo, ci);
224                 break;
225         default:
226                 break;
227         }
228 }