]> git.buserror.net Git - polintos/scott/priv.git/blob - idlcomp/languages/c++/main.cc
7c6ebe8243e27fe00ab3d002b1f7348502402f35
[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(NameSpace *ns, nsdecl_callback cb)
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         if (cb)
418                 cb(this, 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         upscope();
432         
433         file << indent << "}\n";
434
435         if (indent.indent_level == 1)
436                 ns_out(ns);
437 }
438
439 void CPPFile::output_aliases_and_types(NameSpace *ns)
440 {
441         for (NameSpace::const_iterator i = ns->begin(); i != ns->end(); ++i) {
442                 Symbol *sym = (*i).second;
443                 if (dynamic_cast<Alias *>(sym) || dynamic_cast<Type *>(sym))
444                         output_pass(sym, trav_full);
445         }
446 }               
447
448 void CPPFile::output_vstruct_ns(CPPFile *cpp, NameSpace *sym)
449 {
450         Struct *str = dynamic_cast<Struct *>(sym);
451         assert(str);
452         assert(str->is_virtual());
453         
454         cpp->output_guid(str->def.guid);
455 }
456
457 void CPPFile::output_vstruct_info(Struct *sym)
458 {
459         all_ns_in(sym, true, "VINFO_");
460
461         file << indent << "static const __attribute__((unused)) "
462                           "unsigned long *const _guids[] = {\n";
463         
464         stack<StructRef> supers;
465         sym->chainlen = 0;
466                         
467         for (Struct *i = sym; i; i = i->get_super()) {
468                 sym->chainlen++;
469                 supers.push(i);
470         }
471         
472         for (int i = 0; i < sym->chainlen; i++) {
473                 Struct *super = supers.top();
474                 supers.pop();
475
476                 file << indent << '\t';
477                 cpp_output_name(file, super);
478                 file << "_ns::_guid.l,\n";
479         }
480
481         file << indent << "};\n\n"
482              << indent << "static const __attribute__((unused)) "
483                           "::System::RunTime::VStructInfo _info = {\n"
484              << indent << "\t_guids, " << sym->chainlen << '\n'
485              << indent << "};\n";
486
487         
488         all_ns_out(sym, true);
489 }
490
491 void CPPFile::output_vstruct_main(Struct *sym)
492 {
493         assert(sym->is_virtual());
494         Struct *super = sym->get_super();
495
496         const char *name = sym->name->c_str();
497
498         if (!super) {
499                 assert(sym == System_VStruct);
500                 file << indent << "const ::System::RunTime::VStructInfo "
501                                   "*const _infoptr;\n\n";
502         }
503         
504         file << indent << name
505              << "(const ::System::RunTime::VStructInfo *realinfo = &"
506              << name << "_ns::_info) :\n"
507              << indent;
508
509         if (!super)
510                 file << "_infoptr";
511         else
512                 cpp_output_name(file, super);
513         
514         file << "(realinfo)\n"
515              << indent << "{\n"
516              << indent << "}\n";
517
518         if (super)
519                 file << '\n'
520                      << indent << "static " << name << " *downcast(::System::VStruct *base)\n"
521                      << indent << "{\n"
522                      << indent << "\tif (!base)\n"
523                      << indent << "\t\treturn NULL;\n\n"
524                      << indent << "\tconst ::System::RunTime::VStructInfo *info = base->_infoptr;\n\n"
525                      << indent << "\tif (info->chainlen < " << sym->chainlen << ")\n"
526                      << indent << "\t\treturn NULL;\n\n"
527                      << indent << "\tif (::System::RunTime::guids_equal(info->guids["
528                                << sym->chainlen - 1 << "], " << name << "_ns::_guid.l))\n"
529                      << indent << "\t\treturn static_cast<" << name << " *>(base);\n\n"
530                      << indent << "\treturn NULL;\n"
531                      << indent << "}\n";
532
533         do_extra_newline = true;
534 }
535
536 // Output an init method that initializes all the elements in the
537 // struct; this is useful for throwing exceptions.  Due to the
538 // annoying, apparently unwaiveable-by-the-struct requirement that any
539 // struct with a ctor be initialized using the ctor (and thus at
540 // runtime, not statically), this can't be an actual ctor in
541 // non-virtual structs.
542 //
543 // In virtual structs, there's already a ctor, so it wouldn't
544 // matter.  It'd generally be best to be consistent and use the
545 // init method for both, but the main intended use for this is
546 // throwing exceptions (which are virtual), and it'd be
547 // unfortunate to cripple 99% of the uses with unnecessary
548 // syntactic cruft.  The init method will remain available
549 // for vstructs so that things won't break if a non-vstruct
550 // gets made virtual (or if a user really wants to be consistent
551 // between both types in their own code).
552
553 void CPPFile::output_struct_ctor(Struct *sym, bool extra_vstruct)
554 {
555         const char *method_name = extra_vstruct ? sym->name->c_str() : " _init";
556
557         file << '\n' << indent << **sym->name;
558         indent.align_spaces = sym->name->length() + 1;
559
560         if (!extra_vstruct) {
561                 file << " &_init";
562                 indent.align_spaces += 7;
563         }
564
565         file << '(';
566         output_struct_ctor_rec1(sym, 0);
567         
568         if (extra_vstruct) {
569                 Struct *super = sym->get_super();
570
571                 file << ",\n" << indent
572                      << "const ::System::RunTime::VStructInfo *_realinfo = &"
573                      << **sym->name << "_ns::_info) :\n";
574
575                 indent.align_spaces = 0;
576                 file << indent;
577
578                 if (!super)
579                         file << "_infoptr";
580                 else
581                         cpp_output_name(file, super);
582
583                 file << "(_realinfo";
584         }
585         
586         indent.align_spaces = 0;
587         file << ")\n" << indent << "{\n";
588         
589         indent.indent_level++;
590
591         output_struct_ctor_rec2(sym, 0);
592         
593         if (!extra_vstruct) 
594                 file << indent << "return *this;\n";
595         
596         indent.indent_level--;
597         file << indent << "}\n";
598 }
599
600 int CPPFile::output_struct_ctor_rec1(Struct *sym, int num)
601 {
602         if (sym->get_super())
603                 num = output_struct_ctor_rec1(sym->get_super(), num);
604         
605         for (Struct::entries_iterator i = sym->entries_begin();
606              i != sym->entries_end(); ++i)
607         {
608                 if (num++)
609                         file << ",\n" << indent;
610                 
611                 output_datum(sym, *i, -1);
612                 file << "_arg" << num;
613         }
614         
615         return num;
616 }
617
618 int CPPFile::output_struct_ctor_rec2(Struct *sym, int num)
619 {
620         if (sym->get_super())
621                 num = output_struct_ctor_rec2(sym->get_super(), num);
622         
623         for (Struct::entries_iterator i = sym->entries_begin();
624              i != sym->entries_end(); ++i)
625         {
626                 Datum *d = *i;
627                 file << indent << **d->name << " = _arg" << ++num << ";\n";
628         }
629         
630         return num;
631 }
632
633 void CPPFile::output(Struct *sym, int pass, void *arg2)
634 {
635         switch (pass) {
636                 case trav_nsdecl:
637                         if (sym->is_virtual()) {
638                                 output_nsdecl(sym, output_vstruct_ns);
639                         } else {
640                                 output_nsdecl(sym);
641                         }
642
643                         break;
644                 
645                 case trav_forward: {
646                         bool nested = indent.indent_level != 0;
647
648                         if (!nested)
649                                 ns_in(sym, "FWD_");
650                         else
651                                 extra_newline();
652
653                         file << indent << "struct " << **sym->name << ";\n";
654                         
655                         if (!nested)
656                                 ns_out(sym);
657                                 
658                         break;
659                 }
660                 
661                 case trav_full: {
662                         output_pass(sym, trav_nsdecl);
663                         Struct *super = sym->get_super();
664
665                         if (super)
666                                 output_pass(super, trav_full);
667                         
668                         if (sym->is_virtual())
669                                 output_vstruct_info(sym);
670
671                         declare_dependencies(sym);
672                         ns_in(sym);
673
674                         file << indent << "struct " << get_definition_name(sym);
675
676                         if (super) {
677                                 const String *supername = super->get_fq_name("_ns")
678                                                                ->flatten("::");
679                                 file << " :\n" << indent << "public ::" << *supername;
680                         }
681         
682                         file << "\n" << indent << "{\n";
683                         downscope();
684                         
685                         if (sym->is_virtual())
686                                 output_vstruct_main(sym);
687
688                         int offset = 0;
689                         for (Struct::entries_iterator i = sym->entries_begin();
690                              i != sym->entries_end(); ++i)
691                         {
692                                 extra_newline();
693                                 Datum *d = *i;
694                                 file << indent;
695                                 offset = output_datum(sym, d, offset);
696                                 file << **d->name << ";\n";
697                         }
698                         
699                         bool empty_struct = true;
700                         for (Struct *s = sym; s; s = s->get_super()) {
701                                 if (s->entries_begin() != s->entries_end()) {
702                                         empty_struct = false;
703                                         break;
704                                 } 
705                         }
706                         
707                         if (!empty_struct) {
708                                 output_struct_ctor(sym, false);
709                                 
710                                 if (sym->is_virtual())
711                                         output_struct_ctor(sym, true);
712                         }
713
714                         upscope();
715                         file << indent << "};\n";
716         
717                         ns_out(sym);
718                         
719                         output_aliases_and_types(sym);
720                         
721                         for (NameSpace::const_iterator i = sym->begin(); i != sym->end(); ++i) {
722                                 Symbol *sym2 = (*i).second;
723                                 output_pass(sym2, trav_full);
724                         }
725
726                         break;
727                 }
728                 
729                 default:
730                         BUG();
731         }
732 }
733
734 void CPPFile::output_iface_ns(CPPFile *file, NameSpace *sym)
735 {
736         Interface *i = dynamic_cast<Interface *>(sym);
737         assert(i);
738
739         file->output_guid(i->def.guid);
740         file->output_ifaceinfo(i);
741 }
742
743 void CPPFile::output(Interface *sym, int pass, void *arg2)
744 {
745         switch (pass) {
746                 case trav_nsdecl:
747                         output_nsdecl(sym, output_iface_ns);
748                         break;
749                 
750                 case trav_forward: {
751                         bool nested = indent.indent_level != 0;
752
753                         if (!nested)
754                                 ns_in(sym, "FWD_");
755                         else
756                                 extra_newline();
757
758                         file << indent << "struct " << **sym->name << ";\n"
759                              << indent << "struct _i_" << **sym->name << ";\n";
760
761                         if (!nested)
762                                 ns_out(sym);
763
764                         break;
765                 }
766                 
767                 case trav_obj_stub: {
768                         output_pass(sym, trav_forward);
769
770                         for (Interface::supers_iterator i = sym->supers_begin();
771                              i != sym->supers_end(); ++i)
772                         {
773                                 Interface *super = *i;
774                                 output_pass(super, trav_obj_stub);
775                         }
776
777                         declare_dependencies(sym);
778                         ns_in(sym, "STUB_");
779                         
780                         file << indent << "struct "
781                              << get_definition_name(sym) << " {\n";
782
783                         downscope();
784
785                         output_wrapper(sym);
786
787                         upscope();
788                         file << indent << "};\n";
789
790                         ns_out(sym);
791                         break;
792                 }
793                 
794                 case trav_obj_def: {
795                         output_pass(sym, trav_obj_stub);
796
797                         for (Interface::supers_iterator i = sym->supers_begin();
798                              i != sym->supers_end(); ++i)
799                         {
800                                 Interface *super = *i;
801                                 output_pass(super, trav_full);
802                         }
803                         
804                         declare_dependencies(sym);
805                         ns_in(sym, "OBJDEF_");
806
807                         file << indent << "struct ";
808                         file << get_definition_name(sym, "_i_") << " {\n";
809
810                         downscope();
811
812                         output_internal(sym);
813
814                         upscope();
815                         file << indent << "};\n\n";
816
817                         output_casts(sym);
818
819                         ns_out(sym);
820                         break;
821                         
822                 case trav_full:
823                         output_pass(sym, trav_obj_def);
824
825                         declare_dependencies(sym, true);
826                         ns_in(sym);
827                         
828                         output_method_defs(sym);
829                         
830                         ns_out(sym);
831                         output_aliases_and_types(sym);
832
833                         for (NameSpace::const_iterator i = sym->begin(); i != sym->end(); ++i) {
834                                 Symbol *sym2 = (*i).second;
835                                 output_pass(sym2, trav_full);
836                         }
837         
838                         break;
839                 
840                 default:
841                         BUG();
842                 }
843         }
844 }
845
846 void CPPFile::output_bf_elem(Datum *d, int pos, int bits,
847                              string &prefix)
848 {
849         Type *t = d->type;
850         BitField *bf = dynamic_cast<BitField *>(t);
851         Enum *en = dynamic_cast<Enum *>(t);
852         
853         string fieldname;
854
855         if (!d->name->compare(0, 4, "get_") ||
856             !d->name->compare(0, 4, "set_"))
857                 fieldname = '_';
858         
859         fieldname.append(**d->name);
860         
861         if (bf) {
862                 string newprefix(prefix);
863                 newprefix.append(**d->name);
864                 newprefix.append("_IDLNS_");
865                 output_bf(bf, d->def.icon, bits, newprefix);
866                 
867                 // FIXME: getters and setters
868         } else if (en || !t) {
869                 file << indent << "uint" << bits << "_t "
870                      << prefix << fieldname << ':' << d->def.icon << ";\n";
871         } else {
872                 // This is checked here rather than in input.cc, because
873                 // I'm lazy.
874                 
875                 fprintf(stderr, "idlc: Bad input: \"%s\" cannot be the type of \"%s\"\n",
876                         t->get_fq_name()->flatten()->c_str(),
877                         d->get_fq_name()->flatten()->c_str());
878         
879                 throw UserError();
880         }
881 }
882
883 void CPPFile::output_bf(BitField *sym, int bits, int typebits,
884                         string &prefix)
885 {
886         int size = 0;
887         
888         assert(bits == sym->def.bits || bits == typebits);
889         
890         for (BitField::entries_iterator i = sym->entries_begin();
891              i != sym->entries_end(); ++i)
892                 size += (*i)->def.icon;
893         
894         if (size > sym->def.bits) {
895                 // FIXME: This isn't detected in the front end,
896                 // but even once it is, this should stay as a
897                 // replacement for input.cc checking.
898                 
899                 fprintf(stderr, "idlc: \"%s\" is too small (%d bits) for its "
900                                 "contents (%d bits)\n",
901                         sym->get_fq_name()->flatten()->c_str(),
902                         sym->def.bits, size);
903         
904                 throw UserError();
905         }
906         
907         if (target->bitfield_big_endian) {
908                 if (size != bits) {
909                         // The prefix is put at the end, so that we can avoid
910                         // consecutive underscores or an underscore followed
911                         // by a capital, both of which are reserved in C++.
912                 
913                         file << indent << "uint" << bits << "_t _pad_" << prefix
914                              << ':' << bits - size << ";\n";
915                 }
916
917                 int pos = sym->def.bits;
918
919                 for (BitField::entries_reverse_iterator i = sym->entries_rbegin();
920                      i != sym->entries_rend(); ++i)
921                 {
922                         Datum *d = *i;
923                         pos -= d->def.icon;
924                         output_bf_elem(d, pos, typebits, prefix);
925                 }
926         } else {
927                 int pos = 0;
928         
929                 for (BitField::entries_iterator i = sym->entries_begin();
930                      i != sym->entries_end(); ++i)
931                 {
932                         Datum *d = *i;
933                         output_bf_elem(d, pos, typebits, prefix);
934                         pos += d->def.icon;
935                 }
936         }
937 }
938
939 void CPPFile::output(BitField *sym, int pass, void *arg2)
940 {
941         switch (pass) {
942                 case trav_nsdecl:
943                         output_nsdecl(sym);
944                         break;
945
946                 case trav_forward:
947                         extra_newline();
948                         file << indent << "union " << **sym->name << ";\n";
949                         break;
950                 
951                 case trav_full: {
952                         int bits = round_up_bits(sym->def.bits);
953                         ns_in(sym);
954
955                         file << indent << "union ";
956                         file << get_definition_name(sym) << " {\n";
957                         downscope();
958                         
959                         file << indent << "struct {\n";
960
961                         downscope();
962                         string nullprefix;
963                         output_bf(sym, bits, bits, nullprefix);
964                         upscope();
965                         
966                         file << indent << "};\n\n"
967                              << indent << "uint" << bits << "_t _raw;\n\n"
968                              << indent << **sym->name << "()\n"
969                              << indent << "{\n"
970                              << indent << "\t_raw = 0;\n"
971                              << indent << "}\n\n"
972                              << indent << **sym->name << "(uint" << bits << "_t _init)\n"
973                              << indent << "{\n"
974                              << indent << "\t_raw = _init;\n"
975                              << indent << "}\n\n"
976                              << indent << "operator uint" << bits << "_t()\n"
977                              << indent << "{\n"
978                              << indent << "\treturn _raw;\n"
979                              << indent << "}\n";
980                         
981                         upscope();
982                         file << indent << "};\n";
983
984                         ns_out(sym);
985                         output_aliases_and_types(sym);
986                         break;
987                 }
988
989                 default:
990                         BUG();
991         }
992 }
993
994
995 void CPPFile::output(Enum *sym, int pass, void *arg2)
996 {
997         switch (pass) {
998                 case trav_nsdecl:
999                         /* no-op */
1000                         break;
1001
1002                 case trav_forward: {
1003                         bool do_ns_out = false;
1004                         
1005                         if (indent.indent_level == 0) {
1006                                 ns_in(sym);
1007                                 do_ns_out = true;
1008                         } else {
1009                                 extra_newline();
1010                         }
1011                         
1012                         file << indent << "struct " << **sym->name << " {\n";
1013                         downscope();
1014                         
1015                         for (Enum::entries_iterator i = sym->entries_begin();
1016                              i != sym->entries_end(); ++i)
1017                         {
1018                                 Datum *d = *i;
1019                                 
1020                                 file << indent << "static const uint" 
1021                                      << round_up_bits(sym->def.bits) << "_t "
1022                                      << **d->name << " = " << d->def.ucon << ";\n";
1023                         }
1024                         
1025                         file << '\n' << indent << "unsigned int _val;\n\n";
1026
1027                         file << indent << **sym->name << "()\n"
1028                              << indent << "{\n"
1029                              << indent << "\t_val = 0;\n"
1030                              << indent << "}\n\n";
1031
1032                         file << indent << **sym->name << "(unsigned int val)\n"
1033                              << indent << "{\n"
1034                              << indent << "\t_val = val;\n"
1035                              << indent << "}\n\n";
1036
1037                         file << indent << "operator unsigned int()\n"
1038                              << indent << "{\n"
1039                              << indent << "\treturn _val;\n"
1040                              << indent << "}\n\n";
1041                         
1042                         upscope();
1043                         file << indent << "};\n";
1044
1045                         if (do_ns_out)
1046                                 ns_out(sym);
1047
1048                         break;
1049                 }
1050                 
1051                 case trav_full:
1052                         // Nothing particular to do here, other than to make sure
1053                         // that trav_forward has happened (which will always need to
1054                         // be done here if it's not a nested type).
1055                         
1056                         output_pass(sym, trav_forward);
1057                         break;
1058
1059                 default:
1060                         BUG();
1061         }
1062 }
1063
1064 void CPPFile::output(BasicType *sym, int pass, void *arg2)
1065 {
1066         switch (pass) {
1067                 case trav_nsdecl:
1068                         /* no-op */
1069                         break;
1070
1071                 case trav_forward: {
1072                         bool do_ns_out = false;
1073                         
1074                         if (indent.indent_level == 0) {
1075                                 ns_in(sym);
1076                                 do_ns_out = true;
1077                         } else {
1078                                 extra_newline();
1079                         }
1080                         
1081                         file << indent << "typedef ";
1082                         assert(!is_array(sym->def));
1083                         cpp_output_type(file, sym->def, false);
1084                         file << **sym->name << ";\n";
1085
1086                         if (do_ns_out)
1087                                 ns_out(sym);
1088
1089                         break;
1090                 }
1091                 
1092                 case trav_full:
1093                         output_pass(sym, trav_forward);
1094                         break;
1095
1096                 default:
1097                         BUG();
1098         }
1099 }
1100
1101 void CPPFile::output(Alias *sym, int pass, void *arg2)
1102 {
1103         switch (pass) {
1104                 case trav_nsdecl:
1105                         /* no-op */
1106                         break;
1107
1108                 case trav_forward: {
1109                         bool do_ns_out = false;
1110                         
1111                         if (indent.indent_level == 0) {
1112                                 all_ns_in(sym);
1113                                 do_ns_out = true;
1114                         } else {
1115                                 extra_newline();
1116                         }
1117                         
1118                         const String *type = sym->get_concrete_sym()->get_fq_name("_ns")
1119                                                 ->flatten("::");
1120
1121                         file << indent << "typedef " << *type << " " 
1122                              << **sym->name << ";\n";
1123
1124                         if (do_ns_out)
1125                                 all_ns_out(sym);
1126
1127                         break;
1128                 }
1129                 
1130                 case trav_full:
1131                         output_pass(sym, trav_forward);
1132                         break;
1133
1134                 default:
1135                         BUG();
1136         }
1137 }
1138
1139 void CPPFile::output(TypeDef *sym, int pass, void *arg2)
1140 {
1141         switch (pass) {
1142                 case trav_nsdecl:
1143                 case trav_forward:
1144                         /* no-op */
1145                         break;
1146
1147                 case trav_full: {
1148                         output_pass(sym->get_concrete_sym(), trav_forward);
1149                         
1150                         bool do_ns_out = false;
1151                         
1152                         if (indent.indent_level == 0) {
1153                                 all_ns_in(sym);
1154                                 do_ns_out = true;
1155                         } else {
1156                                 extra_newline();
1157                         }
1158                         
1159                         const String *type = sym->get_concrete_sym()->get_fq_name("_ns")
1160                                                 ->flatten("::");
1161
1162                         file << indent << "typedef " << *type << " " 
1163                              << **sym->name << ";\n";
1164
1165                         if (do_ns_out)
1166                                 all_ns_out(sym);
1167
1168                         break;
1169                 }
1170                 
1171                 default:
1172                         BUG();
1173         }
1174 }
1175
1176 void CPPFile::output(Datum *sym, int pass, void *arg2)
1177 {
1178         assert(sym->def.flags.field.Const);
1179         
1180         switch (pass) {
1181                 case trav_nsdecl:
1182                 case trav_forward:
1183                         /* no-op */
1184                         break;
1185
1186                 case trav_full: {
1187                         if (sym->type)
1188                                 declare_type_dependency(sym->type, false);
1189
1190                         bool do_ns_out = false;
1191                         
1192                         if (indent.indent_level == 0) {
1193                                 all_ns_in(sym);
1194                                 do_ns_out = true;
1195                         } else {
1196                                 extra_newline();
1197                         }
1198                         
1199                         file << indent << "static const ";
1200                         
1201                         assert(!is_array(sym->def.basictype));
1202                         
1203                         if (sym->type)
1204                                 cpp_output_type(file, sym->type, false, false);
1205                         else
1206                                 cpp_output_type(file, sym->def.basictype, false);
1207
1208                         file << **sym->name << " = ";
1209                         
1210                         CompiledBasicType *def;
1211                         if (sym->type) {
1212                                 Symbol *real_type = sym->type->get_concrete_sym();
1213                                 BasicType *bt = dynamic_cast<BasicType *>(real_type);
1214                                 assert(bt);
1215                                 
1216                                 def = &bt->def;
1217                         } else {
1218                                 def = &sym->def.basictype;
1219                         }
1220                         
1221                         if (def->flags.field.Float) {
1222                                 file << sym->def.fcon;
1223                         } else if (def->flags.field.Bool) {
1224                                 if (sym->def.ucon == 0)
1225                                         file << "false";
1226                                 else
1227                                         file << "true";
1228                         } else {
1229                                 if (def->flags.field.Unsigned) {
1230                                         file << "0x" << std::hex << sym->def.ucon << std::dec << 'U';
1231                                 } else {
1232                                         file << sym->def.icon;
1233                                 }
1234
1235                                 file << "LL";
1236                         }
1237                         
1238                         file << ";\n";
1239
1240                         if (do_ns_out)
1241                                 all_ns_out(sym);
1242
1243                         break;
1244                 }
1245
1246                 default:
1247                         BUG();
1248         }
1249 }
1250
1251 void CPPBinding::output_root(UserNameSpace *ns, const char *dir)
1252 {
1253         delete new CPPFile(ns, dir);
1254 }