struct marshall: silence unused warning
[polintos/scott/priv.git] / idlcomp / languages / c++ / marshall.cc
1 // C++ Marshalling
2 //
3 // This software is copyright (c) 2008 Scott Wood <scott@buserror.net>.
4 // 
5 // This software is provided 'as-is', without any express or implied warranty.
6 // In no event will the authors or contributors be held liable for any damages
7 // arising from the use of this software.
8 // 
9 // Permission is hereby granted to everyone, free of charge, to use, copy,
10 // modify, prepare derivative works of, publish, distribute, perform,
11 // sublicense, and/or sell copies of the Software, provided that the above
12 // copyright notice and disclaimer of warranty be included in all copies or
13 // substantial portions of this software.
14
15 #include "c++.h"
16
17 void CPPFile::output_ifaceinfo(Interface *iface)
18 {
19         extra_newline();
20
21 #if 0
22         file << indent << "void _invoke(::System::RunTime::Array"
23                        << "< ::System::RunTime::ID> objlist,\n"
24              << indent << "             ::System::RunTime::ParamInfoBlock::Segment *segs,\n"
25              << indent << "             int nsegs);\n\n";
26 #endif
27
28         file << indent << "__attribute__((weak)) __attribute__((unused))\n"
29              << indent << "::System::RunTime::IFaceInfo _info = {\n"
30              << indent << "\t&_guid,\n"
31              << indent << "};\n";
32 }
33
34 void CPPFile::align_type(Symbol *sym)
35 {
36         file << '\n'
37              << indent << "ctx.buf.align_to(__alignof__(";
38         cpp_output_name(file, sym);
39         file << "));\n";
40 }
41
42 void CPPFile::grow_buf()
43 {
44         file << indent << "ptr = ctx.buf.grow_by(len);\n";
45 }
46
47 void CPPFile::marshall_members(Struct *sym)
48 {
49         for (Struct::entries_iterator i = sym->entries_begin();
50              i != sym->entries_end(); ++i)
51         {
52                 Datum *member = *i;
53                 
54                 if (member->type)
55                         member->type->output_lang(this, trav_marshall, member);
56                 else
57                         output_marshall(member->def.basictype, member);
58         }
59 }
60
61 void CPPFile::output_marshall_array(Datum *d)
62 {
63         file << '\n' 
64              << indent << "ctx.buf.align_to(__alignof__(*data->" << **d->name << ".ptr));\n"
65              << indent << "len = data->" << **d->name
66                        << ".count * sizeof(*data->" << **d->name << ".ptr);\n";
67
68         grow_buf();
69
70         file << indent << "memcpy(ptr, data->" << **d->name << ".ptr, len);\n";
71 }
72
73 void CPPFile::output_marshall_method(Struct *sym)
74 {
75         file << indent << "__attribute__((weak)) __attribute__((unused))\n"
76              << indent << "void *_marshall(::System::RunTime::MarshCtx &ctx, ";
77         cpp_output_name(file, sym);
78         file << " *data, ";
79         cpp_output_name(file, sym);
80         file << " **link)\n"
81              << indent << "{\n";
82
83         indent.indent_level++;
84
85         file << indent << "__attribute__((unused)) ";
86         cpp_output_name(file, sym);
87         file << " *sptr;\n"
88              << indent << "void *ptr;\n"
89              << indent << "size_t len;\n"
90              << indent << "__attribute__((unused)) size_t i = 0;\n";
91
92         align_type(sym);
93         file << indent << "*link = (";
94         cpp_output_name(file, sym);
95         file << " *)ctx.buf.count;\n";
96         file << indent << "len = sizeof(*data);\n";
97         grow_buf();
98         
99         file << indent << "memcpy(ptr, data, len);\n"
100              << indent << "sptr = (";
101         cpp_output_name(file, sym);
102         file << " *)ptr;\n";
103
104         marshall_members(sym);
105         indent.indent_level--;
106
107         file << indent << "\treturn 0;\n"
108              << indent << "}\n\n";
109 }
110
111 void CPPFile::output_marshall_inline_method(Struct *sym)
112 {
113         file << indent << "__attribute__((weak)) __attribute__((unused))\n"
114              << indent << "void *_marshall_inline(::System::RunTime::MarshCtx &ctx, ";
115         cpp_output_name(file, sym);
116         file << " *data, ";
117         cpp_output_name(file, sym);
118         file << " *sptr)\n"
119              << indent << "{\n";
120
121         indent.indent_level++;
122
123         file << indent << "__attribute__((unused)) void *ptr;\n"
124              << indent << "__attribute__((unused)) size_t len;\n"
125              << indent << "__attribute__((unused)) size_t i = 0;\n";
126
127         marshall_members(sym);
128         indent.indent_level--;
129
130         file << indent << "\treturn 0;\n"
131              << indent << "}\n";
132 }
133
134 void CPPFile::output_marshall(Struct *sym, Datum *d)
135 {
136         bool array = d->is_array(), inl = d->is_inline();
137         const char *index = "";
138         
139         file << '\n';
140
141         if (array) {
142                 file << indent << "for (size_t i = 0; i < ";
143                 
144                 if (inl)
145                         file << d->array->cons[0].con.ucon;
146                 else
147                         file << "data->" << **d->name << ".count";
148                 
149                 file << "; i++) {\n";
150
151                 indent.indent_level++;
152                 index = "[i]";
153         }
154         
155         if (!inl) {
156                 file << indent << "if (data->" << **d->name << index << ")\n";
157                 indent.indent_level++;
158         }
159         
160         file << indent;
161         cpp_output_name(file, sym);
162         file << "_ns::_marshall";
163         
164         if (inl)
165                 file << "_inline(ctx, &";
166         else
167                 file << "(ctx, ";
168         
169         file << "data->" << **d->name << index
170              << ", &sptr->" << **d->name << index << ");\n";
171
172         if (!inl)
173                 indent.indent_level--;
174
175         if (array) {
176                 indent.indent_level--;
177                 file << indent << "}\n";
178         }
179 }
180
181 void CPPFile::output_marshall(Interface *sym, Datum *d)
182 {
183         printf("marshall iface\n");
184 }
185
186 void CPPFile::output_marshall(Enum *sym, Datum *d)
187 {
188         if (!d->is_array() || d->is_inline())
189                 return;
190
191         output_marshall_array(d);
192 }
193
194 void CPPFile::output_marshall(BitField *sym, Datum *d)
195 {
196         if (!d->is_array() || d->is_inline())
197                 return;
198
199         output_marshall_array(d);
200 }
201
202 void CPPFile::output_marshall(BasicType *sym, Datum *d)
203 {
204         if (!d->is_array() || d->is_inline())
205                 return;
206
207         output_marshall_array(d);
208 }
209
210 void CPPFile::output_marshall(CompiledBasicType &cbt, Datum *d)
211 {
212         if (!d->is_array() || d->is_inline())
213                 return;
214
215         output_marshall_array(d);
216 }
217
218 void CPPFile::output_marshall_pass(Struct *sym, int pass)
219 {
220         switch (pass) {
221                 case trav_nsdecl:
222                         extra_newline();
223                         
224                         file << indent << "void *_marshall(::System::RunTime::MarshCtx &ctx,\n"
225                              << indent << "              ";
226                         cpp_output_name(file, sym);
227                         file << " *data, ";
228                         cpp_output_name(file, sym);
229                         file << " **link);\n";
230
231                         file << indent << "void *_marshall_inline(::System::RunTime::MarshCtx &ctx,\n"
232                              << indent << "              ";
233                         cpp_output_name(file, sym);
234                         file << " *data, ";
235                         cpp_output_name(file, sym);
236                         file << " *sptr);\n";
237
238                         file << indent << "void _unmarshall(::System::RunTime::MarshCtx &ctx,\n"
239                              << indent << "                 ";
240                         cpp_output_name(file, sym);
241                         file << " *data);\n";
242                         break;
243
244                 case trav_full:
245                         output_marshall_method(sym);
246                         output_marshall_inline_method(sym);
247
248 #if 0
249                         file << indent << "__attribute__((weak)) __attribute__((unused))\n"
250                              << indent << "void _unmarshall(::System::RunTime::Array<uint8_t, ::System::RunTime::ORBMM> buf,\n"
251                              << indent << "                 ::System::RunTime::Array< ::System::_i_Object *, ::System::RunTime::ORBMM> objlist,\n"
252                              << indent << "                 ::System::RunTime::ParamInfoBlock::Segment *segs,\n"
253                              << indent << "                 int nsegs)\n"
254                              << indent << "{\n"
255                              << indent << "}\n";
256 #endif
257
258                         break;
259         }
260 }