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