]> git.buserror.net Git - polintos/scott/priv.git/blob - idlcomp/languages/c++/c++.h
9abbebd5cc573da4cf96099dcd0a23dc7ee7c65e
[polintos/scott/priv.git] / idlcomp / languages / c++ / c++.h
1 #ifndef IDLC_CPP_H
2 #define IDLC_CPP_H
3
4 #include <fstream>
5 #include <list>
6
7 #include <lang.h>
8 #include <util.h>
9
10 using std::ofstream;
11 using std::ostream;
12
13 // Output the namespace-qualified symbol name, including
14 // user namespaces.   A prefix for the final component (i.e. the
15 // actual name) can optionally be specified.
16
17 void cpp_output_name(ostream &file, Symbol *sym, const char *prefix = "");
18
19 // Output the C++ type, not including the array, including
20 // a trailing space.
21
22 void cpp_output_type(ostream &file, Type *t, bool array, bool is_mutable);
23 void cpp_output_type(ostream &file, CompiledBasicType &t, bool is_mutable);
24
25 // Output the param's fully-qualified type, a space, and the name
26 // of the param.
27
28 void cpp_output_one_param(ostream &file, Param *p, bool is_server,
29                           bool is_copy = false);
30
31 enum CPPTraversal {
32         trav_full,         // Full, final output
33         trav_obj_def,      // Object struct definition
34         trav_obj_stub,     // Object pointer stub,
35                       // so it can be used in structs.
36         trav_nsdecl,       // _ns declaration
37         trav_forward,      // Forward declaration of
38                       // structs/interfaces in nsdecl
39
40         num_traversals,
41         
42         // Values beyond this point are not part of the
43         // above sequential pass scheme; they are merely
44         // for dynamic dispatch.
45         trav_marshall,
46         trav_unmarshall,
47 };
48
49 // Per-symbol language-private data
50 struct CPPData : public Releasable {
51         // The traversal indices are also passed as "arg1" to determine
52         // which pass to generate.  Higher numbers are performed first.
53         
54         int traversed[num_traversals];
55
56         CPPData() : Releasable(1)
57         {
58                 memset(traversed, 0, sizeof(traversed));
59         }
60 };
61
62 static inline CPPData &cpp_symdata(Symbol *sym)
63 {
64         if (!sym->lang_priv)
65                 sym->lang_priv = new CPPData;
66
67         CPPData *ptr = dynamic_cast<CPPData *>(sym->lang_priv);
68         assert(ptr);
69         return *ptr;
70 }
71         
72 // Each IDL namespace is mapped to a C++ header file,
73 // represented by a CPPFile.
74
75 class CPPFile : public LangCallback {
76         Indent indent;
77         string dirname;
78         ofstream file;
79         UserNameSpace *ns;
80         StrList *fqname;
81
82         //// Interface caller-side members:
83         
84         // Outputs the body of the internal _i_<ifacename> struct,
85         // containing a vtable pointer (and definition).
86
87         void output_internal(Interface *sym);
88         
89         // Outputs the user-visible class wrapper that is used to
90         // allow implicit upcasts.
91         
92         void output_wrapper(Interface *sym);
93
94         // Output implementations of the cast methods prototyped in the wrapper.
95         // The implementation is delayed to avoid circular dependency problems.
96
97         void output_casts(Interface *sym);
98         
99         //// Marshalling methods
100         
101         void output_marshall_pass(Struct *sym, int pass);
102
103         void output_marshall(Struct *sym, Datum *d);
104         void output_marshall(Interface *sym, Datum *d);
105         void output_marshall(Enum *sym, Datum *d);
106         void output_marshall(BitField *sym, Datum *d);
107         void output_marshall(BasicType *sym, Datum *d);
108         void output_marshall(CompiledBasicType &cbt, Datum *d);
109
110         void align_type(Symbol *sym);
111         void grow_buf(Symbol *sym);
112
113         // Output the downcast and implicit upcast methods for
114         // the given interface/superinterface pair.
115
116         static void wrapper_cast_callback(Interface *iface, void *arg);
117         void output_downcast(Interface *iface, Interface *super);
118         void output_upcast(Interface *iface, Interface *super);
119
120         static void wrapper_cast_proto_callback(Interface *iface, void *arg);
121         void output_downcast_proto(Interface *iface, Interface *super);
122         void output_upcast_proto(Interface *iface, Interface *super);
123
124         static void wrapper_method_proto_callback(Interface *iface, void *arg);
125         static void wrapper_method_callback(Interface *iface, void *arg);
126
127         void output_iface_tbl(Interface *iface);
128         static void tbl_callback(Interface *iface, void *arg);
129         void output_iface_tbl_entry(Interface *iface, Interface *super);
130
131         static void output_method_ptrs_callback(Interface *iface, void *arg);
132         void output_method_ptrs(Interface *iface);
133         void output_methods(Interface *iface, Interface *super, bool prototype);
134         void output_method_defs(Interface *iface);
135
136         void output_one_method(Interface *iface, Method *m,
137                                bool prototype, bool retval);
138         void output_one_method_ptr(Method *m, Interface *iface);
139         
140         //// Misc members:
141
142         // Output the static const guid[] value, and the IFaceInfo struct.
143
144         void output_guid(const uint64_t *guid);
145         void output_ifaceinfo(Interface *iface);
146
147         // Output a datum in the given struct, along with any necessary
148         // padding.  Return the offset of the next datum.
149         
150         int output_datum(Struct *ns, Datum *d, int offset);
151
152         void output_vstruct_info(Struct *sym);
153         void output_vstruct_main(Struct *sym);
154         void output_struct_ctor(Struct *sym, bool extra_vstruct);
155         int output_struct_ctor_rec1(Struct *sym, int num);
156         int output_struct_ctor_rec2(Struct *sym, int num);
157
158         void output_bf_elem(Datum *d, int pos, int bits, string &prefix);
159         void output_bf(BitField *bf, int bits, int typebits, string &prefix);
160                 
161         // Call ns_in on the symbol in order to declare the containing
162         // namespace.  Call ns_out when finished.  ns_out also takes care
163         // of inserting the extra newline separating declarations.  The
164         // "extra" parameter allows extra output passes to have unique
165         // #ifndef names, and should have a trailing underscore.
166         
167         void ns_in(Symbol *sym, const char *extra = "DEF_");
168         void ns_out(Symbol *sym);
169
170         // As above, but also outputs non-user namespaces (optionally
171         // including "sym" itself).
172
173         void all_ns_in(Symbol *sym, bool include_self = false,
174                        const char *extra = "DEF_");
175         void all_ns_out(Symbol *sym, bool include_self = false);
176
177         // Output a protective #ifndef/#define block to keep a type
178         // from being declared more than once.  Call ifndef_out()
179         // for the #endif.
180         
181         void ifndef_in(Symbol *sym, const char *extra = "DEF_");
182         void ifndef_out();
183         
184         // Return the namespace-qualified symbol name, excluding
185         // user namespaces.  This is used when defining a name
186         // which (if it is nested) has been previously forward
187         // declared.  A prefix for the final component (i.e. the
188         // actual name) can optionally be specified.
189         
190         String &get_definition_name(Symbol *sym, const char *prefix = "");
191         
192         // Output the _ns declaration of a struct or interface.
193         // If a callback is supplied, it is called inside the
194         // namespace, before any namespace members have been
195         // emitted.
196         
197         void output_nsdecl_begin(NameSpace *ns);
198         void output_nsdecl_children(NameSpace *ns);
199         void output_nsdecl_end(NameSpace *ns);
200         void output_nsdecl(NameSpace *ns);
201
202         void output_aliases_and_types(NameSpace *ns);
203         
204         // When a top-level struct or interface is about to be generated,
205         // it is scanned for other types which need to be generated.  If the
206         // full definition is needed, it goes on the need_to_declare list;
207         // otherwise, it goes on the need_to_forward_declare list.  When
208         // processing the lists, if a node has already been marked as
209         // traversed, it is skipped.
210         
211         // Full declaration is needed for superstructs, superinterfaces,
212         // inline structs (once implemented) used, and typedefs used.
213         // It is also necessary to fully declare any type which contains
214         // a type whose forward declaration is needed, as such types
215         // cannot (as far as I know) be forward-declared.
216         
217         std::list<Symbol *> need_to_declare;
218         
219         // Forward declaration is needed for types used via a pointer only.
220         
221         std::list<Symbol *> need_to_forward_declare;
222         
223         void declare_dependencies(Struct *sym);
224         void declare_dependencies(Interface *iface, bool need_obj_def = false);
225         void declare_type_dependency(Type *t, bool need_obj_def = false);
226
227         // This is the traversal of the first namespace; any symbol
228         // whose last traversal is at least this has been written to
229         // at least one of the included namespaces (and/or the
230         // current namespace).
231
232         int first_traversal;
233
234         bool pass_needed(Symbol *sym, int pass)
235         {
236                 return first_traversal > cpp_symdata(sym).traversed[pass];
237         }
238
239         void output_pass(Symbol *sym, int pass)
240         {
241                 if (pass_needed(sym, pass)) {
242                         cpp_symdata(sym).traversed[pass] = traversal;
243                         sym->output_lang(this, pass);
244                 }
245         }
246
247         bool do_extra_newline;
248
249         void downscope()
250         {
251                 indent.indent_level++;
252                 do_extra_newline = false;
253         }
254         
255         void upscope()
256         {
257                 indent.indent_level--;
258                 do_extra_newline = true;
259         }
260         
261         void extra_newline()
262         {
263                 if (do_extra_newline)
264                         file << "\n";
265                 
266                 do_extra_newline = true;
267         }
268
269 public:
270         CPPFile(UserNameSpace *ns, const char *dir);
271         virtual ~CPPFile();
272
273         void output(UserNameSpace *sym, int pass = trav_full, void *arg2 = NULL);
274         void output(Struct *sym, int pass = trav_full, void *arg2 = NULL);
275         void output(Interface *sym, int pass = trav_full, void *arg2 = NULL);
276         void output(BitField *sym, int pass = trav_full, void *arg2 = NULL);
277         void output(Enum *sym, int pass = trav_full, void *arg2 = NULL);
278         void output(BasicType *sym, int pass = trav_full, void *arg2 = NULL);
279         void output(Alias *sym, int pass = trav_full, void *arg2 = NULL);
280         void output(TypeDef *sym, int pass = trav_full, void *arg2 = NULL);
281         void output(Datum *sym, int pass = trav_full, void *arg2 = NULL);
282
283         void marshall(Struct *sym);
284         void marshall(Interface *sym);
285         void marshall(BitField *sym);
286         void marshall(Enum *sym);
287         void marshall(BasicType *sym);
288
289         void unmarshall(Struct *sym);
290         void unmarshall(Interface *sym);
291         void unmarshall(BitField *sym);
292         void unmarshall(Enum *sym);
293         void unmarshall(BasicType *sym);
294 };
295
296 class CPPBinding : public Language {
297 public:
298         CPPBinding()
299         {
300                 name = "C++";
301                 next = first_lang;
302                 first_lang = this;
303         }
304
305         void output_root(UserNameSpace *ns, const char *dir);
306         void output_server(UserNameSpace *ns, const char *dir);
307 };
308
309 #endif