]> git.buserror.net Git - polintos/scott/priv.git/blob - idlcomp/languages/c++/main.cc
idlcomp/c++: Separate output_nsdecl into begin/children/end.
[polintos/scott/priv.git] / idlcomp / languages / c++ / main.cc
1 // idlcomp/languages/c++/main.cc -- C++ IDL binding
2 //
3 // This software is copyright (c) 2006 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 // FIXME: escape C++ reserved words
16
17 #include <cerrno>
18
19 #include <sys/types.h>
20 #include <sys/stat.h>
21
22 #include <targets.h>
23 #include "c++.h"
24
25 CPPBinding cppbinding;
26
27 CPPFile::CPPFile(UserNameSpace *NS, const char *dir) :
28 dirname(dir)
29 {
30         indent.indent_level = 0;
31         indent.align_spaces = 0;
32         do_extra_newline = true;
33         ns = NS;
34
35         dirname.append(1, '/');
36         dirname.append(ns->name);
37         string headername(dirname);
38         headername.append(".h");
39
40         file.open(headername.c_str());
41         if (!file.is_open()) {
42                 fprintf(stderr, "Could not create output file \"%s\": %s.\n",
43                         headername.c_str(), strerror(errno));
44
45                 throw UserError();
46         }
47         
48         if (mkdir(dirname.c_str(), 0777) < 0) {
49                 fprintf(stderr, "Could not create output directory \"%s\": %s.\n",
50                         dir, strerror(errno));
51
52                 throw UserError();
53         }
54         
55         file <<   "// " << *ns->get_fq_name()->flatten(".")
56              << "\n// This is a C++ language binding generated by idlc.\n"
57              <<   "// Do not modify this file directly.\n\n";
58
59         fqname = ns->get_fq_name();
60         String *fqname_flat = ns->get_fq_name()->flatten("_IDLNS_");
61         file <<   "#ifndef IDL_HDR_" << *fqname_flat
62              << "\n#define IDL_HDR_" << *fqname_flat << "\n\n";
63         
64         file << "#include <orb.h>\n";
65
66         first_traversal = ++traversal;
67         assert(first_traversal >= 0);
68         
69         for (NameSpace::const_iterator i = ns->begin(); i != ns->end(); ++i) {
70                 Symbol *sym = (*i).second;
71                 UserNameSpace *uns = dynamic_cast<UserNameSpace *>(sym);
72                 
73                 if (uns) {
74                         file << "#include \"" << **ns->name << '/' << **uns->name << ".h\"\n";
75                         
76                         // Process namespaces first, to minimize duplicate definitions
77                         // if this namespace depends on something declared in a sub
78                         // namespace.
79                         
80                         output(uns);
81                 }
82         }
83
84         for (NameSpace::const_iterator i = ns->begin(); i != ns->end(); ++i) {
85                 Symbol *sym = (*i).second;
86                 UserNameSpace *uns = dynamic_cast<UserNameSpace *>(sym);
87                 
88                 if (!uns)
89                         output_pass(sym, trav_nsdecl);
90         }
91
92         for (NameSpace::const_iterator i = ns->begin(); i != ns->end(); ++i) {
93                 Symbol *sym = (*i).second;
94                 UserNameSpace *uns = dynamic_cast<UserNameSpace *>(sym);
95                 
96                 if (!uns)
97                         output_pass(sym, trav_full);
98         }
99 }
100
101 CPPFile::~CPPFile()
102 {
103         file << "#endif\n";
104         
105         assert(indent.indent_level == 0);
106 }
107
108 void CPPFile::ifndef_in(Symbol *sym, const char *extra)
109 {
110         StrList *fqname = sym->get_fq_name();
111         String *fqname_flat = fqname->flatten("_IDLNS_");
112
113         assert(indent.indent_level == 0);
114
115         file << "\n#ifndef IDL_DUP_" << extra << *fqname_flat
116              << "\n#define IDL_DUP_" << extra << *fqname_flat << "\n";
117 }
118
119 void CPPFile::ifndef_out()
120 {
121         file << "\n#endif\n";
122 }
123
124 void CPPFile::ns_in(Symbol *sym, const char *extra)
125 {
126         // Only output user namespaces here; if we're defining a nested type,
127         // it will have been forward declared already, and thus the
128         // non-user-namespaces can be specified directly in the definition.
129         //
130         // For non-forward-declarables such as metadata, use all_ns_in.
131         
132         StrList *ns_fqname = sym->find_toplevel_type()->get_ns()->get_fq_name();
133
134         ifndef_in(sym, extra);
135
136         for (StrList::const_iterator i = ns_fqname->begin();
137              i != ns_fqname->end(); ++i) {
138                 const String *str = *i;
139                 file << "namespace " << *str << " {\n";
140         }
141         
142         downscope();
143 }
144
145 void CPPFile::ns_out(Symbol *sym)
146 {
147         upscope();
148         assert(indent.indent_level == 0);
149
150         for (Symbol *s = sym->find_toplevel_type()->get_ns(); s != toplevel;
151              s = s->get_ns())
152                 file << "}";
153
154         ifndef_out();
155 }
156
157 void CPPFile::all_ns_in(Symbol *sym, bool include_self, const char *extra)
158 {
159         NameSpace *ns = sym->find_toplevel_type()->get_ns();
160         StrList *ns_fqname = ns->get_fq_name();
161
162         ifndef_in(sym, extra);
163
164         for (StrList::const_iterator i = ns_fqname->begin();
165              i != ns_fqname->end(); ++i) {
166                 const String *str = *i;
167                 file << "namespace " << *str << " {\n";
168         }
169         
170         stack<SymbolRef> typens;
171         
172         if (!include_self)
173                 sym = sym->get_ns();
174         
175         for (Symbol *s = sym; s != ns; s = s->get_ns())
176                 typens.push(s);
177         
178         while (!typens.empty()) {
179                 Symbol *s = typens.top();
180                 typens.pop();
181                 file << "namespace " << **s->name << "_ns {\n";
182         }
183         
184         downscope();
185 }
186
187 void CPPFile::all_ns_out(Symbol *sym, bool include_self)
188 {
189         upscope();
190         assert(indent.indent_level == 0);
191
192         if (!include_self)
193                 sym = sym->get_ns();
194
195         for (Symbol *s = sym; s != toplevel; s = s->get_ns())
196                 file << "}";
197
198         ifndef_out();
199 }
200
201 String &CPPFile::get_definition_name(Symbol *sym, const char *prefix)
202 {
203         NameSpace *ns = sym->get_ns();
204         UserNameSpace *uns = dynamic_cast<UserNameSpace *>(ns);
205         String *str;
206         
207         if (!uns) {
208                 str = &get_definition_name(ns);
209                 str->append("_ns::");
210         } else {
211                 str = new String();
212         }
213         
214         str->append(prefix);
215         str->append(**sym->name);
216         return *str;
217 }
218
219 void cpp_output_name(ostream &file, Symbol *sym, const char *prefix)
220 {
221         StrList *sl = sym->get_fq_name("_ns");
222         sl->pop_back();
223
224         file << "::" << *sl->flatten("::") << "::" << prefix << **sym->name;
225 }
226
227 void CPPFile::output(UserNameSpace *sym, int pass, void *arg2)
228 {
229         assert(indent.indent_level == 0);
230         delete new CPPFile(sym, dirname.c_str());
231 }
232
233 static inline int round_up_bits(int bits)
234 {
235         assert(bits >= 1 && bits <= 64);
236         
237         if (bits > 32)
238                 return 64;
239         if (bits > 16)
240                 return 32;
241         if (bits > 8)
242                 return 16;
243
244         return 8;
245 }
246
247 // FIXME: Inline arrays...
248 void cpp_output_type(ostream &file, Type *t, bool array, bool is_mutable)
249 {
250         if (array) {
251                 file << "::System::RunTime::";
252                 
253                 if (is_mutable)
254                         file << "Mutable";
255                 
256                 file << "Array< ";
257         }
258
259         cpp_output_name(file, t);
260
261         if (array)
262                 file << ">";
263
264         file << ' ';
265 }
266
267 // FIXME: Inline arrays...
268 void cpp_output_type(ostream &file, CompiledBasicType &t, bool is_mutable)
269 {
270         if (is_array(t)) {
271                 file << "::System::RunTime::";
272                 
273                 if (is_mutable)
274                         file << "Mutable";
275                 
276                 file << "Array< ";
277         }
278
279         if (t.flags.field.Bool) {
280                 // Don't rely on C++ to provide any particular representation
281                 // of bool...
282                 file << "uint8_t";
283         } else if (t.flags.field.Float) {
284                 if (t.bits == 32)
285                         file << "float";
286                 else
287                         file << "double";
288         } else {
289                 if (t.flags.field.Unsigned)
290                         file << 'u';
291
292                 file << "int" << round_up_bits(t.bits) << "_t";
293         }
294         
295         if (is_array(t))
296                 file << '>';
297         
298         file << ' ';
299 }
300
301 // FIXME: implement padding
302 int CPPFile::output_datum(Struct *ns, Datum *d, int offset)
303 {
304         if (d->type) {
305                 cpp_output_type(file, d->type, is_array(d->def.basictype),
306                                 !d->def.flags.field.Immutable);
307
308                 Struct *dtype = dynamic_cast<Struct *>(*d->type);
309                 if (dtype && !dtype->is_inline())
310                         file << '*';
311         } else {
312                 cpp_output_type(file, d->def.basictype,
313                                 !d->def.flags.field.Immutable);
314         }
315
316         return offset;
317 }
318
319 void CPPFile::declare_type_dependency(Type *t, bool need_obj_def)
320 {
321         Symbol *toplevel_type = t->find_toplevel_type();
322
323         assert(indent.indent_level == 0);
324         
325         output_pass(toplevel_type, trav_nsdecl);
326         
327         if (t == toplevel_type)
328                 output_pass(t, trav_forward);
329
330         if (dynamic_cast<Interface *>(t))
331                 output_pass(t, need_obj_def ? trav_obj_def : trav_obj_stub);
332         else if (!dynamic_cast<Struct *>(t) || need_obj_def)
333                 output_pass(t, trav_full);
334 }
335
336 void CPPFile::declare_dependencies(Interface *iface, bool need_obj_def)
337 {
338         for (Interface::methods_iterator i = iface->methods_begin();
339              i != iface->methods_end(); ++i)
340         {
341                 Method *m = *i;
342                 for (Method::entries_iterator j = m->entries_begin();
343                      j != m->entries_end(); ++j)
344                 {
345                         Param *p = *j;
346                         if (p->type)
347                                 declare_type_dependency(p->type, need_obj_def);
348                 }
349         }
350
351         for (Interface::supers_iterator i = iface->supers_begin();
352              i != iface->supers_end(); ++i)
353         {
354                 Interface *super = *i;
355                 declare_type_dependency(super);
356         }
357 }
358
359 void CPPFile::declare_dependencies(Struct *str)
360 {
361         for (NameSpace::const_iterator i = str->begin(); i != str->end(); ++i) {
362                 Symbol *sym = (*i).second;
363                 Datum *d = dynamic_cast<Datum *>(sym);
364                 if (d) {
365                         if (d->type)
366                                 declare_type_dependency(d->type, d->is_inline());
367                         
368                         continue;
369                 }
370
371                 Struct *mstr = dynamic_cast<Struct *>(sym);
372                 if (mstr) {
373                         declare_dependencies(mstr);
374                         continue;
375                 }
376                 
377                 Interface *miface = dynamic_cast<Interface *>(sym);
378                 if (miface)
379                         declare_dependencies(miface);
380         }
381 }
382
383 void CPPFile::output_guid(const uint64_t *guid64)
384 {
385         const unsigned char *guid = reinterpret_cast<const unsigned char *>(guid64);
386         char guidhex[7];
387         
388         file << indent << "static const __attribute__((unused))\n"
389              << indent << "::System::RunTime::GUID _guid = {\n"
390              << indent << "\t{ ";
391         
392         for (int i = 0; i < 16; i++) {
393                 if (i == 8)
394                         file << '\n' << indent << "\t  ";
395                 
396                 sprintf(guidhex, "0x%02x, ", *guid++);
397                 file << guidhex;
398         }
399
400         file << "}\n"
401              << indent << "};\n";
402
403         do_extra_newline = true;
404 }
405
406 void CPPFile::output_nsdecl_begin(NameSpace *ns)
407 {
408         // If indent level is not zero, this is a nested struct or interface.
409         if (indent.indent_level == 0)
410                 ns_in(ns, "NS_");
411         else
412                 extra_newline();
413
414         file << indent << "namespace " << **ns->name << "_ns {\n";
415         downscope();
416 }       
417         
418 void CPPFile::output_nsdecl_children(NameSpace *ns)
419 {       
420         for (NameSpace::const_iterator i = ns->begin(); i != ns->end(); ++i) {
421                 Symbol *sym = (*i).second;
422
423                 assert(pass_needed(sym, trav_forward));
424                 assert(pass_needed(sym, trav_nsdecl));
425                 
426                 // If it's a Method or a non-const Datum, this is a no-op.
427                 output_pass(sym, trav_forward);
428                 output_pass(sym, trav_nsdecl);
429         }
430 }
431
432 void CPPFile::output_nsdecl_end(NameSpace *ns)
433 {
434         upscope();
435         
436         file << indent << "}\n";
437
438         if (indent.indent_level == 1)
439                 ns_out(ns);
440 }
441
442 void CPPFile::output_nsdecl(NameSpace *ns)
443 {
444         output_nsdecl_begin(ns);
445         output_nsdecl_children(ns);
446         output_nsdecl_end(ns);
447 }
448
449 void CPPFile::output_aliases_and_types(NameSpace *ns)
450 {
451         for (NameSpace::const_iterator i = ns->begin(); i != ns->end(); ++i) {
452                 Symbol *sym = (*i).second;
453                 if (dynamic_cast<Alias *>(sym) || dynamic_cast<Type *>(sym))
454                         output_pass(sym, trav_full);
455         }
456 }               
457
458 void CPPFile::output_vstruct_info(Struct *sym)
459 {
460         all_ns_in(sym, true, "VINFO_");
461
462         file << indent << "static const __attribute__((unused)) "
463                           "unsigned long *const _guids[] = {\n";
464         
465         stack<StructRef> supers;
466         sym->chainlen = 0;
467                         
468         for (Struct *i = sym; i; i = i->get_super()) {
469                 sym->chainlen++;
470                 supers.push(i);
471         }
472         
473         for (int i = 0; i < sym->chainlen; i++) {
474                 Struct *super = supers.top();
475                 supers.pop();
476
477                 file << indent << '\t';
478                 cpp_output_name(file, super);
479                 file << "_ns::_guid.l,\n";
480         }
481
482         file << indent << "};\n\n"
483              << indent << "static const __attribute__((unused)) "
484                           "::System::RunTime::VStructInfo _info = {\n"
485              << indent << "\t_guids, " << sym->chainlen << ",\n"
486 #if 0
487              << indent << "\t_marshall, _unmarshall,\n"
488 #endif
489              << indent << "};\n";
490
491         
492         all_ns_out(sym, true);
493 }
494
495 void CPPFile::output_vstruct_main(Struct *sym)
496 {
497         assert(sym->is_virtual());
498         Struct *super = sym->get_super();
499
500         const char *name = sym->name->c_str();
501
502         if (!super) {
503                 assert(sym == System_VStruct);
504                 file << indent << "const ::System::RunTime::VStructInfo "
505                                   "*const _infoptr;\n\n";
506         }
507         
508         file << indent << name
509              << "(const ::System::RunTime::VStructInfo *realinfo = &"
510              << name << "_ns::_info) :\n"
511              << indent;
512
513         if (!super)
514                 file << "_infoptr";
515         else
516                 cpp_output_name(file, super);
517         
518         file << "(realinfo)\n"
519              << indent << "{\n"
520              << indent << "}\n";
521
522         if (super)
523                 file << '\n'
524                      << indent << "static " << name << " *downcast(::System::VStruct *base)\n"
525                      << indent << "{\n"
526                      << indent << "\tif (!base)\n"
527                      << indent << "\t\treturn NULL;\n\n"
528                      << indent << "\tconst ::System::RunTime::VStructInfo *info = base->_infoptr;\n\n"
529                      << indent << "\tif (info->chainlen < " << sym->chainlen << ")\n"
530                      << indent << "\t\treturn NULL;\n\n"
531                      << indent << "\tif (::System::RunTime::guids_equal(info->guids["
532                                << sym->chainlen - 1 << "], " << name << "_ns::_guid.l))\n"
533                      << indent << "\t\treturn static_cast<" << name << " *>(base);\n\n"
534                      << indent << "\treturn NULL;\n"
535                      << indent << "}\n";
536
537         do_extra_newline = true;
538 }
539
540 // Output an init method that initializes all the elements in the
541 // struct; this is useful for throwing exceptions.  Due to the
542 // annoying, apparently unwaiveable-by-the-struct requirement that any
543 // struct with a ctor be initialized using the ctor (and thus at
544 // runtime, not statically), this can't be an actual ctor in
545 // non-virtual structs.
546 //
547 // In virtual structs, there's already a ctor, so it wouldn't
548 // matter.  It'd generally be best to be consistent and use the
549 // init method for both, but the main intended use for this is
550 // throwing exceptions (which are virtual), and it'd be
551 // unfortunate to cripple 99% of the uses with unnecessary
552 // syntactic cruft.  The init method will remain available
553 // for vstructs so that things won't break if a non-vstruct
554 // gets made virtual (or if a user really wants to be consistent
555 // between both types in their own code).
556
557 void CPPFile::output_struct_ctor(Struct *sym, bool extra_vstruct)
558 {
559         const char *method_name = extra_vstruct ? sym->name->c_str() : " _init";
560
561         file << '\n' << indent << **sym->name;
562         indent.align_spaces = sym->name->length() + 1;
563
564         if (!extra_vstruct) {
565                 file << " &_init";
566                 indent.align_spaces += 7;
567         }
568
569         file << '(';
570         output_struct_ctor_rec1(sym, 0);
571         
572         if (extra_vstruct) {
573                 Struct *super = sym->get_super();
574
575                 file << ",\n" << indent
576                      << "const ::System::RunTime::VStructInfo *_realinfo = &"
577                      << **sym->name << "_ns::_info) :\n";
578
579                 indent.align_spaces = 0;
580                 file << indent;
581
582                 if (!super)
583                         file << "_infoptr";
584                 else
585                         cpp_output_name(file, super);
586
587                 file << "(_realinfo";
588         }
589         
590         indent.align_spaces = 0;
591         file << ")\n" << indent << "{\n";
592         
593         indent.indent_level++;
594
595         output_struct_ctor_rec2(sym, 0);
596         
597         if (!extra_vstruct) 
598                 file << indent << "return *this;\n";
599         
600         indent.indent_level--;
601         file << indent << "}\n";
602 }
603
604 int CPPFile::output_struct_ctor_rec1(Struct *sym, int num)
605 {
606         if (sym->get_super())
607                 num = output_struct_ctor_rec1(sym->get_super(), num);
608         
609         for (Struct::entries_iterator i = sym->entries_begin();
610              i != sym->entries_end(); ++i)
611         {
612                 if (num++)
613                         file << ",\n" << indent;
614                 
615                 output_datum(sym, *i, -1);
616                 file << "_arg" << num;
617         }
618         
619         return num;
620 }
621
622 int CPPFile::output_struct_ctor_rec2(Struct *sym, int num)
623 {
624         if (sym->get_super())
625                 num = output_struct_ctor_rec2(sym->get_super(), num);
626         
627         for (Struct::entries_iterator i = sym->entries_begin();
628              i != sym->entries_end(); ++i)
629         {
630                 Datum *d = *i;
631                 file << indent << **d->name << " = _arg" << ++num << ";\n";
632         }
633         
634         return num;
635 }
636
637 void CPPFile::output(Struct *sym, int pass, void *arg2)
638 {
639         switch (pass) {
640                 case trav_nsdecl:
641                         if (sym->is_virtual()) {
642                                 output_nsdecl_begin(sym);
643                                 output_nsdecl_children(sym);
644                                 output_guid(sym->def.guid);
645                                 output_nsdecl_end(sym);
646                         } else {
647                                 output_nsdecl(sym);
648                         }
649
650                         break;
651                 
652                 case trav_forward: {
653                         bool nested = indent.indent_level != 0;
654
655                         if (!nested)
656                                 ns_in(sym, "FWD_");
657                         else
658                                 extra_newline();
659
660                         file << indent << "struct " << **sym->name << ";\n";
661                         
662                         if (!nested)
663                                 ns_out(sym);
664                                 
665                         break;
666                 }
667                 
668                 case trav_full: {
669                         output_pass(sym, trav_nsdecl);
670                         Struct *super = sym->get_super();
671
672                         if (super)
673                                 output_pass(super, trav_full);
674                         
675                         if (sym->is_virtual())
676                                 output_vstruct_info(sym);
677
678                         declare_dependencies(sym);
679                         ns_in(sym);
680
681                         file << indent << "struct " << get_definition_name(sym);
682
683                         if (super) {
684                                 const String *supername = super->get_fq_name("_ns")
685                                                                ->flatten("::");
686                                 file << " :\n" << indent << "public ::" << *supername;
687                         }
688         
689                         file << "\n" << indent << "{\n";
690                         downscope();
691                         
692                         if (sym->is_virtual())
693                                 output_vstruct_main(sym);
694
695                         int offset = 0;
696                         for (Struct::entries_iterator i = sym->entries_begin();
697                              i != sym->entries_end(); ++i)
698                         {
699                                 extra_newline();
700                                 Datum *d = *i;
701                                 file << indent;
702                                 offset = output_datum(sym, d, offset);
703                                 file << **d->name << ";\n";
704                         }
705                         
706                         bool empty_struct = true;
707                         for (Struct *s = sym; s; s = s->get_super()) {
708                                 if (s->entries_begin() != s->entries_end()) {
709                                         empty_struct = false;
710                                         break;
711                                 } 
712                         }
713                         
714                         if (!empty_struct) {
715                                 output_struct_ctor(sym, false);
716                                 
717                                 if (sym->is_virtual())
718                                         output_struct_ctor(sym, true);
719                         }
720
721                         upscope();
722                         file << indent << "};\n";
723         
724                         ns_out(sym);
725                         
726                         output_aliases_and_types(sym);
727                         
728                         for (NameSpace::const_iterator i = sym->begin(); i != sym->end(); ++i) {
729                                 Symbol *sym2 = (*i).second;
730                                 output_pass(sym2, trav_full);
731                         }
732
733                         break;
734                 }
735                 
736                 default:
737                         BUG();
738         }
739 }
740
741 void CPPFile::output(Interface *sym, int pass, void *arg2)
742 {
743         switch (pass) {
744                 case trav_nsdecl:
745                         output_nsdecl_begin(sym);
746                         output_nsdecl_children(sym);
747
748                         output_guid(sym->def.guid);
749                         output_ifaceinfo(sym);
750
751                         output_nsdecl_end(sym);
752                         break;
753                 
754                 case trav_forward: {
755                         bool nested = indent.indent_level != 0;
756
757                         if (!nested)
758                                 ns_in(sym, "FWD_");
759                         else
760                                 extra_newline();
761
762                         file << indent << "struct " << **sym->name << ";\n"
763                              << indent << "struct _i_" << **sym->name << ";\n";
764
765                         if (!nested)
766                                 ns_out(sym);
767
768                         break;
769                 }
770                 
771                 case trav_obj_stub: {
772                         output_pass(sym, trav_forward);
773
774                         for (Interface::supers_iterator i = sym->supers_begin();
775                              i != sym->supers_end(); ++i)
776                         {
777                                 Interface *super = *i;
778                                 output_pass(super, trav_obj_stub);
779                         }
780
781                         declare_dependencies(sym);
782                         ns_in(sym, "STUB_");
783                         
784                         file << indent << "struct "
785                              << get_definition_name(sym) << " {\n";
786
787                         downscope();
788
789                         output_wrapper(sym);
790
791                         upscope();
792                         file << indent << "};\n";
793
794                         ns_out(sym);
795                         break;
796                 }
797                 
798                 case trav_obj_def: {
799                         output_pass(sym, trav_obj_stub);
800
801                         for (Interface::supers_iterator i = sym->supers_begin();
802                              i != sym->supers_end(); ++i)
803                         {
804                                 Interface *super = *i;
805                                 output_pass(super, trav_full);
806                         }
807                         
808                         declare_dependencies(sym);
809                         ns_in(sym, "OBJDEF_");
810
811                         file << indent << "struct ";
812                         file << get_definition_name(sym, "_i_") << " {\n";
813
814                         downscope();
815
816                         output_internal(sym);
817
818                         upscope();
819                         file << indent << "};\n\n";
820
821                         output_casts(sym);
822
823                         ns_out(sym);
824                         break;
825                         
826                 case trav_full:
827                         output_pass(sym, trav_obj_def);
828
829                         declare_dependencies(sym, true);
830                         ns_in(sym);
831                         
832                         output_method_defs(sym);
833                         
834                         ns_out(sym);
835                         output_aliases_and_types(sym);
836
837                         for (NameSpace::const_iterator i = sym->begin(); i != sym->end(); ++i) {
838                                 Symbol *sym2 = (*i).second;
839                                 output_pass(sym2, trav_full);
840                         }
841         
842                         break;
843                 
844                 default:
845                         BUG();
846                 }
847         }
848 }
849
850 void CPPFile::output_bf_elem(Datum *d, int pos, int bits,
851                              string &prefix)
852 {
853         Type *t = d->type;
854         BitField *bf = dynamic_cast<BitField *>(t);
855         Enum *en = dynamic_cast<Enum *>(t);
856         
857         string fieldname;
858
859         if (!d->name->compare(0, 4, "get_") ||
860             !d->name->compare(0, 4, "set_"))
861                 fieldname = '_';
862         
863         fieldname.append(**d->name);
864         
865         if (bf) {
866                 string newprefix(prefix);
867                 newprefix.append(**d->name);
868                 newprefix.append("_IDLNS_");
869                 output_bf(bf, d->def.icon, bits, newprefix);
870                 
871                 // FIXME: getters and setters
872         } else if (en || !t) {
873                 file << indent << "uint" << bits << "_t "
874                      << prefix << fieldname << ':' << d->def.icon << ";\n";
875         } else {
876                 // This is checked here rather than in input.cc, because
877                 // I'm lazy.
878                 
879                 fprintf(stderr, "idlc: Bad input: \"%s\" cannot be the type of \"%s\"\n",
880                         t->get_fq_name()->flatten()->c_str(),
881                         d->get_fq_name()->flatten()->c_str());
882         
883                 throw UserError();
884         }
885 }
886
887 void CPPFile::output_bf(BitField *sym, int bits, int typebits,
888                         string &prefix)
889 {
890         int size = 0;
891         
892         assert(bits == sym->def.bits || bits == typebits);
893         
894         for (BitField::entries_iterator i = sym->entries_begin();
895              i != sym->entries_end(); ++i)
896                 size += (*i)->def.icon;
897         
898         if (size > sym->def.bits) {
899                 // FIXME: This isn't detected in the front end,
900                 // but even once it is, this should stay as a
901                 // replacement for input.cc checking.
902                 
903                 fprintf(stderr, "idlc: \"%s\" is too small (%d bits) for its "
904                                 "contents (%d bits)\n",
905                         sym->get_fq_name()->flatten()->c_str(),
906                         sym->def.bits, size);
907         
908                 throw UserError();
909         }
910         
911         if (target->bitfield_big_endian) {
912                 if (size != bits) {
913                         // The prefix is put at the end, so that we can avoid
914                         // consecutive underscores or an underscore followed
915                         // by a capital, both of which are reserved in C++.
916                 
917                         file << indent << "uint" << bits << "_t _pad_" << prefix
918                              << ':' << bits - size << ";\n";
919                 }
920
921                 int pos = sym->def.bits;
922
923                 for (BitField::entries_reverse_iterator i = sym->entries_rbegin();
924                      i != sym->entries_rend(); ++i)
925                 {
926                         Datum *d = *i;
927                         pos -= d->def.icon;
928                         output_bf_elem(d, pos, typebits, prefix);
929                 }
930         } else {
931                 int pos = 0;
932         
933                 for (BitField::entries_iterator i = sym->entries_begin();
934                      i != sym->entries_end(); ++i)
935                 {
936                         Datum *d = *i;
937                         output_bf_elem(d, pos, typebits, prefix);
938                         pos += d->def.icon;
939                 }
940         }
941 }
942
943 void CPPFile::output(BitField *sym, int pass, void *arg2)
944 {
945         switch (pass) {
946                 case trav_nsdecl:
947                         output_nsdecl(sym);
948                         break;
949
950                 case trav_forward:
951                         extra_newline();
952                         file << indent << "union " << **sym->name << ";\n";
953                         break;
954                 
955                 case trav_full: {
956                         int bits = round_up_bits(sym->def.bits);
957                         ns_in(sym);
958
959                         file << indent << "union ";
960                         file << get_definition_name(sym) << " {\n";
961                         downscope();
962                         
963                         file << indent << "struct {\n";
964
965                         downscope();
966                         string nullprefix;
967                         output_bf(sym, bits, bits, nullprefix);
968                         upscope();
969                         
970                         file << indent << "};\n\n"
971                              << indent << "uint" << bits << "_t _raw;\n\n"
972                              << indent << **sym->name << "()\n"
973                              << indent << "{\n"
974                              << indent << "\t_raw = 0;\n"
975                              << indent << "}\n\n"
976                              << indent << **sym->name << "(uint" << bits << "_t _init)\n"
977                              << indent << "{\n"
978                              << indent << "\t_raw = _init;\n"
979                              << indent << "}\n\n"
980                              << indent << "operator uint" << bits << "_t()\n"
981                              << indent << "{\n"
982                              << indent << "\treturn _raw;\n"
983                              << indent << "}\n";
984                         
985                         upscope();
986                         file << indent << "};\n";
987
988                         ns_out(sym);
989                         output_aliases_and_types(sym);
990                         break;
991                 }
992
993                 default:
994                         BUG();
995         }
996 }
997
998
999 void CPPFile::output(Enum *sym, int pass, void *arg2)
1000 {
1001         switch (pass) {
1002                 case trav_nsdecl:
1003                         /* no-op */
1004                         break;
1005
1006                 case trav_forward: {
1007                         bool do_ns_out = false;
1008                         
1009                         if (indent.indent_level == 0) {
1010                                 ns_in(sym);
1011                                 do_ns_out = true;
1012                         } else {
1013                                 extra_newline();
1014                         }
1015                         
1016                         file << indent << "struct " << **sym->name << " {\n";
1017                         downscope();
1018                         
1019                         for (Enum::entries_iterator i = sym->entries_begin();
1020                              i != sym->entries_end(); ++i)
1021                         {
1022                                 Datum *d = *i;
1023                                 
1024                                 file << indent << "static const uint" 
1025                                      << round_up_bits(sym->def.bits) << "_t "
1026                                      << **d->name << " = " << d->def.ucon << ";\n";
1027                         }
1028                         
1029                         file << '\n' << indent << "unsigned int _val;\n\n";
1030
1031                         file << indent << **sym->name << "()\n"
1032                              << indent << "{\n"
1033                              << indent << "\t_val = 0;\n"
1034                              << indent << "}\n\n";
1035
1036                         file << indent << **sym->name << "(unsigned int val)\n"
1037                              << indent << "{\n"
1038                              << indent << "\t_val = val;\n"
1039                              << indent << "}\n\n";
1040
1041                         file << indent << "operator unsigned int()\n"
1042                              << indent << "{\n"
1043                              << indent << "\treturn _val;\n"
1044                              << indent << "}\n\n";
1045                         
1046                         upscope();
1047                         file << indent << "};\n";
1048
1049                         if (do_ns_out)
1050                                 ns_out(sym);
1051
1052                         break;
1053                 }
1054                 
1055                 case trav_full:
1056                         // Nothing particular to do here, other than to make sure
1057                         // that trav_forward has happened (which will always need to
1058                         // be done here if it's not a nested type).
1059                         
1060                         output_pass(sym, trav_forward);
1061                         break;
1062
1063                 default:
1064                         BUG();
1065         }
1066 }
1067
1068 void CPPFile::output(BasicType *sym, int pass, void *arg2)
1069 {
1070         switch (pass) {
1071                 case trav_nsdecl:
1072                         /* no-op */
1073                         break;
1074
1075                 case trav_forward: {
1076                         bool do_ns_out = false;
1077                         
1078                         if (indent.indent_level == 0) {
1079                                 ns_in(sym);
1080                                 do_ns_out = true;
1081                         } else {
1082                                 extra_newline();
1083                         }
1084                         
1085                         file << indent << "typedef ";
1086                         assert(!is_array(sym->def));
1087                         cpp_output_type(file, sym->def, false);
1088                         file << **sym->name << ";\n";
1089
1090                         if (do_ns_out)
1091                                 ns_out(sym);
1092
1093                         break;
1094                 }
1095                 
1096                 case trav_full:
1097                         output_pass(sym, trav_forward);
1098                         break;
1099
1100                 default:
1101                         BUG();
1102         }
1103 }
1104
1105 void CPPFile::output(Alias *sym, int pass, void *arg2)
1106 {
1107         switch (pass) {
1108                 case trav_nsdecl:
1109                         /* no-op */
1110                         break;
1111
1112                 case trav_forward: {
1113                         bool do_ns_out = false;
1114                         
1115                         if (indent.indent_level == 0) {
1116                                 all_ns_in(sym);
1117                                 do_ns_out = true;
1118                         } else {
1119                                 extra_newline();
1120                         }
1121                         
1122                         const String *type = sym->get_concrete_sym()->get_fq_name("_ns")
1123                                                 ->flatten("::");
1124
1125                         file << indent << "typedef " << *type << " " 
1126                              << **sym->name << ";\n";
1127
1128                         if (do_ns_out)
1129                                 all_ns_out(sym);
1130
1131                         break;
1132                 }
1133                 
1134                 case trav_full:
1135                         output_pass(sym, trav_forward);
1136                         break;
1137
1138                 default:
1139                         BUG();
1140         }
1141 }
1142
1143 void CPPFile::output(TypeDef *sym, int pass, void *arg2)
1144 {
1145         switch (pass) {
1146                 case trav_nsdecl:
1147                 case trav_forward:
1148                         /* no-op */
1149                         break;
1150
1151                 case trav_full: {
1152                         output_pass(sym->get_concrete_sym(), trav_forward);
1153                         
1154                         bool do_ns_out = false;
1155                         
1156                         if (indent.indent_level == 0) {
1157                                 all_ns_in(sym);
1158                                 do_ns_out = true;
1159                         } else {
1160                                 extra_newline();
1161                         }
1162                         
1163                         const String *type = sym->get_concrete_sym()->get_fq_name("_ns")
1164                                                 ->flatten("::");
1165
1166                         file << indent << "typedef " << *type << " " 
1167                              << **sym->name << ";\n";
1168
1169                         if (do_ns_out)
1170                                 all_ns_out(sym);
1171
1172                         break;
1173                 }
1174                 
1175                 default:
1176                         BUG();
1177         }
1178 }
1179
1180 void CPPFile::output(Datum *sym, int pass, void *arg2)
1181 {
1182         assert(sym->def.flags.field.Const);
1183         
1184         switch (pass) {
1185                 case trav_nsdecl:
1186                 case trav_forward:
1187                         /* no-op */
1188                         break;
1189
1190                 case trav_full: {
1191                         if (sym->type)
1192                                 declare_type_dependency(sym->type, false);
1193
1194                         bool do_ns_out = false;
1195                         
1196                         if (indent.indent_level == 0) {
1197                                 all_ns_in(sym);
1198                                 do_ns_out = true;
1199                         } else {
1200                                 extra_newline();
1201                         }
1202                         
1203                         file << indent << "static const ";
1204                         
1205                         assert(!is_array(sym->def.basictype));
1206                         
1207                         if (sym->type)
1208                                 cpp_output_type(file, sym->type, false, false);
1209                         else
1210                                 cpp_output_type(file, sym->def.basictype, false);
1211
1212                         file << **sym->name << " = ";
1213                         
1214                         CompiledBasicType *def;
1215                         if (sym->type) {
1216                                 Symbol *real_type = sym->type->get_concrete_sym();
1217                                 BasicType *bt = dynamic_cast<BasicType *>(real_type);
1218                                 assert(bt);
1219                                 
1220                                 def = &bt->def;
1221                         } else {
1222                                 def = &sym->def.basictype;
1223                         }
1224                         
1225                         if (def->flags.field.Float) {
1226                                 file << sym->def.fcon;
1227                         } else if (def->flags.field.Bool) {
1228                                 if (sym->def.ucon == 0)
1229                                         file << "false";
1230                                 else
1231                                         file << "true";
1232                         } else {
1233                                 if (def->flags.field.Unsigned) {
1234                                         file << "0x" << std::hex << sym->def.ucon << std::dec << 'U';
1235                                 } else {
1236                                         file << sym->def.icon;
1237                                 }
1238
1239                                 file << "LL";
1240                         }
1241                         
1242                         file << ";\n";
1243
1244                         if (do_ns_out)
1245                                 all_ns_out(sym);
1246
1247                         break;
1248                 }
1249
1250                 default:
1251                         BUG();
1252         }
1253 }
1254
1255 void CPPBinding::output_root(UserNameSpace *ns, const char *dir)
1256 {
1257         delete new CPPFile(ns, dir);
1258 }