1 // idlcomp/languages/c++/main.cc -- C++ IDL binding
3 // This software is copyright (c) 2006 Scott Wood <scott@buserror.net>.
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.
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.
15 // FIXME: escape C++ reserved words
19 #include <sys/types.h>
25 CPPBinding cppbinding;
27 CPPFile::CPPFile(UserNameSpace *NS, const char *dir) :
30 indent.indent_level = 0;
31 indent.align_spaces = 0;
32 do_extra_newline = true;
35 dirname.append(1, '/');
36 dirname.append(ns->name);
37 string headername(dirname);
38 headername.append(".h");
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));
48 if (mkdir(dirname.c_str(), 0777) < 0) {
49 fprintf(stderr, "Could not create output directory \"%s\": %s.\n",
50 dir, strerror(errno));
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";
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";
64 file << "#include <orb.h>\n";
66 first_traversal = ++traversal;
67 assert(first_traversal >= 0);
69 for (NameSpace::const_iterator i = ns->begin(); i != ns->end(); ++i) {
70 Symbol *sym = (*i).second;
71 UserNameSpace *uns = dynamic_cast<UserNameSpace *>(sym);
74 file << "#include \"" << **ns->name << '/' << **uns->name << ".h\"\n";
76 // Process namespaces first, to minimize duplicate definitions
77 // if this namespace depends on something declared in a sub
84 for (NameSpace::const_iterator i = ns->begin(); i != ns->end(); ++i) {
85 Symbol *sym = (*i).second;
86 UserNameSpace *uns = dynamic_cast<UserNameSpace *>(sym);
89 output_pass(sym, trav_nsdecl);
92 for (NameSpace::const_iterator i = ns->begin(); i != ns->end(); ++i) {
93 Symbol *sym = (*i).second;
94 UserNameSpace *uns = dynamic_cast<UserNameSpace *>(sym);
97 output_pass(sym, trav_full);
105 assert(indent.indent_level == 0);
108 void CPPFile::ifndef_in(Symbol *sym, const char *extra)
110 StrList *fqname = sym->get_fq_name();
111 String *fqname_flat = fqname->flatten("_IDLNS_");
113 assert(indent.indent_level == 0);
115 file << "\n#ifndef IDL_DUP_" << extra << *fqname_flat
116 << "\n#define IDL_DUP_" << extra << *fqname_flat << "\n";
119 void CPPFile::ifndef_out()
121 file << "\n#endif\n";
124 void CPPFile::ns_in(Symbol *sym, const char *extra)
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.
130 // For non-forward-declarables such as metadata, use all_ns_in.
132 StrList *ns_fqname = sym->find_toplevel_type()->get_ns()->get_fq_name();
134 ifndef_in(sym, extra);
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";
145 void CPPFile::ns_out(Symbol *sym)
148 assert(indent.indent_level == 0);
150 for (Symbol *s = sym->find_toplevel_type()->get_ns(); s != toplevel;
157 void CPPFile::all_ns_in(Symbol *sym, bool include_self, const char *extra)
159 NameSpace *ns = sym->find_toplevel_type()->get_ns();
160 StrList *ns_fqname = ns->get_fq_name();
162 ifndef_in(sym, extra);
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";
170 stack<SymbolRef> typens;
175 for (Symbol *s = sym; s != ns; s = s->get_ns())
178 while (!typens.empty()) {
179 Symbol *s = typens.top();
181 file << "namespace " << **s->name << "_ns {\n";
187 void CPPFile::all_ns_out(Symbol *sym, bool include_self)
190 assert(indent.indent_level == 0);
195 for (Symbol *s = sym; s != toplevel; s = s->get_ns())
201 String &CPPFile::get_definition_name(Symbol *sym, const char *prefix)
203 NameSpace *ns = sym->get_ns();
204 UserNameSpace *uns = dynamic_cast<UserNameSpace *>(ns);
208 str = &get_definition_name(ns);
209 str->append("_ns::");
215 str->append(**sym->name);
219 void cpp_output_name(ostream &file, Symbol *sym, const char *prefix)
221 StrList *sl = sym->get_fq_name("_ns");
224 file << "::" << *sl->flatten("::") << "::" << prefix << **sym->name;
227 void CPPFile::output(UserNameSpace *sym, int pass, void *arg2)
229 assert(indent.indent_level == 0);
230 delete new CPPFile(sym, dirname.c_str());
233 static inline int round_up_bits(int bits)
235 assert(bits >= 1 && bits <= 64);
247 // FIXME: Inline arrays...
248 void cpp_output_type(ostream &file, Type *t, bool array, bool is_mutable)
251 file << "::System::RunTime::";
259 cpp_output_name(file, t);
267 // FIXME: Inline arrays...
268 void cpp_output_type(ostream &file, CompiledBasicType &t, bool is_mutable)
271 file << "::System::RunTime::";
279 if (t.flags.field.Bool) {
280 // Don't rely on C++ to provide any particular representation
283 } else if (t.flags.field.Float) {
289 if (t.flags.field.Unsigned)
292 file << "int" << round_up_bits(t.bits) << "_t";
301 // FIXME: implement padding
302 int CPPFile::output_datum(Struct *ns, Datum *d, int offset)
305 cpp_output_type(file, d->type, is_array(d->def.basictype),
306 !d->def.flags.field.Immutable);
308 Struct *dtype = dynamic_cast<Struct *>(*d->type);
309 if (dtype && !dtype->is_inline())
312 cpp_output_type(file, d->def.basictype,
313 !d->def.flags.field.Immutable);
319 void CPPFile::declare_type_dependency(Type *t, bool need_obj_def)
321 Symbol *toplevel_type = t->find_toplevel_type();
323 assert(indent.indent_level == 0);
325 output_pass(toplevel_type, trav_nsdecl);
327 if (t == toplevel_type)
328 output_pass(t, trav_forward);
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);
336 void CPPFile::declare_dependencies(Interface *iface, bool need_obj_def)
338 for (Interface::methods_iterator i = iface->methods_begin();
339 i != iface->methods_end(); ++i)
342 for (Method::entries_iterator j = m->entries_begin();
343 j != m->entries_end(); ++j)
347 declare_type_dependency(p->type, need_obj_def);
351 for (Interface::supers_iterator i = iface->supers_begin();
352 i != iface->supers_end(); ++i)
354 Interface *super = *i;
355 declare_type_dependency(super);
359 void CPPFile::declare_dependencies(Struct *str)
361 for (NameSpace::const_iterator i = str->begin(); i != str->end(); ++i) {
362 Symbol *sym = (*i).second;
363 Datum *d = dynamic_cast<Datum *>(sym);
366 declare_type_dependency(d->type, d->is_inline());
371 Struct *mstr = dynamic_cast<Struct *>(sym);
373 declare_dependencies(mstr);
377 Interface *miface = dynamic_cast<Interface *>(sym);
379 declare_dependencies(miface);
383 void CPPFile::output_guid(const uint64_t *guid64)
385 const unsigned char *guid = reinterpret_cast<const unsigned char *>(guid64);
388 file << indent << "static const __attribute__((unused))\n"
389 << indent << "::System::RunTime::GUID _guid = {\n"
392 for (int i = 0; i < 16; i++) {
394 file << '\n' << indent << "\t ";
396 sprintf(guidhex, "0x%02x, ", *guid++);
403 do_extra_newline = true;
406 void CPPFile::output_nsdecl(NameSpace *ns, nsdecl_callback cb)
408 // If indent level is not zero, this is a nested struct or interface.
409 if (indent.indent_level == 0)
414 file << indent << "namespace " << **ns->name << "_ns {\n";
420 for (NameSpace::const_iterator i = ns->begin(); i != ns->end(); ++i) {
421 Symbol *sym = (*i).second;
423 assert(pass_needed(sym, trav_forward));
424 assert(pass_needed(sym, trav_nsdecl));
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);
433 file << indent << "}\n";
435 if (indent.indent_level == 1)
439 void CPPFile::output_aliases_and_types(NameSpace *ns)
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);
448 void CPPFile::output_vstruct_ns(CPPFile *cpp, NameSpace *sym)
450 Struct *str = dynamic_cast<Struct *>(sym);
452 assert(str->is_virtual());
454 cpp->output_guid(str->def.guid);
457 void CPPFile::output_vstruct_info(Struct *sym)
459 all_ns_in(sym, true, "VINFO_");
461 file << indent << "static const __attribute__((unused)) "
462 "unsigned long *const _guids[] = {\n";
464 stack<StructRef> supers;
467 for (Struct *i = sym; i; i = i->get_super()) {
472 for (int i = 0; i < sym->chainlen; i++) {
473 Struct *super = supers.top();
476 file << indent << '\t';
477 cpp_output_name(file, super);
478 file << "_ns::_guid.l,\n";
481 file << indent << "};\n\n"
482 << indent << "static const __attribute__((unused)) "
483 "::System::RunTime::VStructInfo _info = {\n"
484 << indent << "\t_guids, " << sym->chainlen << ",\n"
486 << indent << "\t_marshall, _unmarshall,\n"
491 all_ns_out(sym, true);
494 void CPPFile::output_vstruct_main(Struct *sym)
496 assert(sym->is_virtual());
497 Struct *super = sym->get_super();
499 const char *name = sym->name->c_str();
502 assert(sym == System_VStruct);
503 file << indent << "const ::System::RunTime::VStructInfo "
504 "*const _infoptr;\n\n";
507 file << indent << name
508 << "(const ::System::RunTime::VStructInfo *realinfo = &"
509 << name << "_ns::_info) :\n"
515 cpp_output_name(file, super);
517 file << "(realinfo)\n"
523 << indent << "static " << name << " *downcast(::System::VStruct *base)\n"
525 << indent << "\tif (!base)\n"
526 << indent << "\t\treturn NULL;\n\n"
527 << indent << "\tconst ::System::RunTime::VStructInfo *info = base->_infoptr;\n\n"
528 << indent << "\tif (info->chainlen < " << sym->chainlen << ")\n"
529 << indent << "\t\treturn NULL;\n\n"
530 << indent << "\tif (::System::RunTime::guids_equal(info->guids["
531 << sym->chainlen - 1 << "], " << name << "_ns::_guid.l))\n"
532 << indent << "\t\treturn static_cast<" << name << " *>(base);\n\n"
533 << indent << "\treturn NULL;\n"
536 do_extra_newline = true;
539 // Output an init method that initializes all the elements in the
540 // struct; this is useful for throwing exceptions. Due to the
541 // annoying, apparently unwaiveable-by-the-struct requirement that any
542 // struct with a ctor be initialized using the ctor (and thus at
543 // runtime, not statically), this can't be an actual ctor in
544 // non-virtual structs.
546 // In virtual structs, there's already a ctor, so it wouldn't
547 // matter. It'd generally be best to be consistent and use the
548 // init method for both, but the main intended use for this is
549 // throwing exceptions (which are virtual), and it'd be
550 // unfortunate to cripple 99% of the uses with unnecessary
551 // syntactic cruft. The init method will remain available
552 // for vstructs so that things won't break if a non-vstruct
553 // gets made virtual (or if a user really wants to be consistent
554 // between both types in their own code).
556 void CPPFile::output_struct_ctor(Struct *sym, bool extra_vstruct)
558 const char *method_name = extra_vstruct ? sym->name->c_str() : " _init";
560 file << '\n' << indent << **sym->name;
561 indent.align_spaces = sym->name->length() + 1;
563 if (!extra_vstruct) {
565 indent.align_spaces += 7;
569 output_struct_ctor_rec1(sym, 0);
572 Struct *super = sym->get_super();
574 file << ",\n" << indent
575 << "const ::System::RunTime::VStructInfo *_realinfo = &"
576 << **sym->name << "_ns::_info) :\n";
578 indent.align_spaces = 0;
584 cpp_output_name(file, super);
586 file << "(_realinfo";
589 indent.align_spaces = 0;
590 file << ")\n" << indent << "{\n";
592 indent.indent_level++;
594 output_struct_ctor_rec2(sym, 0);
597 file << indent << "return *this;\n";
599 indent.indent_level--;
600 file << indent << "}\n";
603 int CPPFile::output_struct_ctor_rec1(Struct *sym, int num)
605 if (sym->get_super())
606 num = output_struct_ctor_rec1(sym->get_super(), num);
608 for (Struct::entries_iterator i = sym->entries_begin();
609 i != sym->entries_end(); ++i)
612 file << ",\n" << indent;
614 output_datum(sym, *i, -1);
615 file << "_arg" << num;
621 int CPPFile::output_struct_ctor_rec2(Struct *sym, int num)
623 if (sym->get_super())
624 num = output_struct_ctor_rec2(sym->get_super(), num);
626 for (Struct::entries_iterator i = sym->entries_begin();
627 i != sym->entries_end(); ++i)
630 file << indent << **d->name << " = _arg" << ++num << ";\n";
636 void CPPFile::output(Struct *sym, int pass, void *arg2)
640 if (sym->is_virtual()) {
641 output_nsdecl(sym, output_vstruct_ns);
649 bool nested = indent.indent_level != 0;
656 file << indent << "struct " << **sym->name << ";\n";
665 output_pass(sym, trav_nsdecl);
666 Struct *super = sym->get_super();
669 output_pass(super, trav_full);
671 if (sym->is_virtual())
672 output_vstruct_info(sym);
674 declare_dependencies(sym);
677 file << indent << "struct " << get_definition_name(sym);
680 const String *supername = super->get_fq_name("_ns")
682 file << " :\n" << indent << "public ::" << *supername;
685 file << "\n" << indent << "{\n";
688 if (sym->is_virtual())
689 output_vstruct_main(sym);
692 for (Struct::entries_iterator i = sym->entries_begin();
693 i != sym->entries_end(); ++i)
698 offset = output_datum(sym, d, offset);
699 file << **d->name << ";\n";
702 bool empty_struct = true;
703 for (Struct *s = sym; s; s = s->get_super()) {
704 if (s->entries_begin() != s->entries_end()) {
705 empty_struct = false;
711 output_struct_ctor(sym, false);
713 if (sym->is_virtual())
714 output_struct_ctor(sym, true);
718 file << indent << "};\n";
722 output_aliases_and_types(sym);
724 for (NameSpace::const_iterator i = sym->begin(); i != sym->end(); ++i) {
725 Symbol *sym2 = (*i).second;
726 output_pass(sym2, trav_full);
737 void CPPFile::output_iface_ns(CPPFile *file, NameSpace *sym)
739 Interface *i = dynamic_cast<Interface *>(sym);
742 file->output_guid(i->def.guid);
743 file->output_ifaceinfo(i);
746 void CPPFile::output(Interface *sym, int pass, void *arg2)
750 output_nsdecl(sym, output_iface_ns);
754 bool nested = indent.indent_level != 0;
761 file << indent << "struct " << **sym->name << ";\n"
762 << indent << "struct _i_" << **sym->name << ";\n";
770 case trav_obj_stub: {
771 output_pass(sym, trav_forward);
773 for (Interface::supers_iterator i = sym->supers_begin();
774 i != sym->supers_end(); ++i)
776 Interface *super = *i;
777 output_pass(super, trav_obj_stub);
780 declare_dependencies(sym);
783 file << indent << "struct "
784 << get_definition_name(sym) << " {\n";
791 file << indent << "};\n";
798 output_pass(sym, trav_obj_stub);
800 for (Interface::supers_iterator i = sym->supers_begin();
801 i != sym->supers_end(); ++i)
803 Interface *super = *i;
804 output_pass(super, trav_full);
807 declare_dependencies(sym);
808 ns_in(sym, "OBJDEF_");
810 file << indent << "struct ";
811 file << get_definition_name(sym, "_i_") << " {\n";
815 output_internal(sym);
818 file << indent << "};\n\n";
826 output_pass(sym, trav_obj_def);
828 declare_dependencies(sym, true);
831 output_method_defs(sym);
834 output_aliases_and_types(sym);
836 for (NameSpace::const_iterator i = sym->begin(); i != sym->end(); ++i) {
837 Symbol *sym2 = (*i).second;
838 output_pass(sym2, trav_full);
849 void CPPFile::output_bf_elem(Datum *d, int pos, int bits,
853 BitField *bf = dynamic_cast<BitField *>(t);
854 Enum *en = dynamic_cast<Enum *>(t);
858 if (!d->name->compare(0, 4, "get_") ||
859 !d->name->compare(0, 4, "set_"))
862 fieldname.append(**d->name);
865 string newprefix(prefix);
866 newprefix.append(**d->name);
867 newprefix.append("_IDLNS_");
868 output_bf(bf, d->def.icon, bits, newprefix);
870 // FIXME: getters and setters
871 } else if (en || !t) {
872 file << indent << "uint" << bits << "_t "
873 << prefix << fieldname << ':' << d->def.icon << ";\n";
875 // This is checked here rather than in input.cc, because
878 fprintf(stderr, "idlc: Bad input: \"%s\" cannot be the type of \"%s\"\n",
879 t->get_fq_name()->flatten()->c_str(),
880 d->get_fq_name()->flatten()->c_str());
886 void CPPFile::output_bf(BitField *sym, int bits, int typebits,
891 assert(bits == sym->def.bits || bits == typebits);
893 for (BitField::entries_iterator i = sym->entries_begin();
894 i != sym->entries_end(); ++i)
895 size += (*i)->def.icon;
897 if (size > sym->def.bits) {
898 // FIXME: This isn't detected in the front end,
899 // but even once it is, this should stay as a
900 // replacement for input.cc checking.
902 fprintf(stderr, "idlc: \"%s\" is too small (%d bits) for its "
903 "contents (%d bits)\n",
904 sym->get_fq_name()->flatten()->c_str(),
905 sym->def.bits, size);
910 if (target->bitfield_big_endian) {
912 // The prefix is put at the end, so that we can avoid
913 // consecutive underscores or an underscore followed
914 // by a capital, both of which are reserved in C++.
916 file << indent << "uint" << bits << "_t _pad_" << prefix
917 << ':' << bits - size << ";\n";
920 int pos = sym->def.bits;
922 for (BitField::entries_reverse_iterator i = sym->entries_rbegin();
923 i != sym->entries_rend(); ++i)
927 output_bf_elem(d, pos, typebits, prefix);
932 for (BitField::entries_iterator i = sym->entries_begin();
933 i != sym->entries_end(); ++i)
936 output_bf_elem(d, pos, typebits, prefix);
942 void CPPFile::output(BitField *sym, int pass, void *arg2)
951 file << indent << "union " << **sym->name << ";\n";
955 int bits = round_up_bits(sym->def.bits);
958 file << indent << "union ";
959 file << get_definition_name(sym) << " {\n";
962 file << indent << "struct {\n";
966 output_bf(sym, bits, bits, nullprefix);
969 file << indent << "};\n\n"
970 << indent << "uint" << bits << "_t _raw;\n\n"
971 << indent << **sym->name << "()\n"
973 << indent << "\t_raw = 0;\n"
975 << indent << **sym->name << "(uint" << bits << "_t _init)\n"
977 << indent << "\t_raw = _init;\n"
979 << indent << "operator uint" << bits << "_t()\n"
981 << indent << "\treturn _raw;\n"
985 file << indent << "};\n";
988 output_aliases_and_types(sym);
998 void CPPFile::output(Enum *sym, int pass, void *arg2)
1005 case trav_forward: {
1006 bool do_ns_out = false;
1008 if (indent.indent_level == 0) {
1015 file << indent << "struct " << **sym->name << " {\n";
1018 for (Enum::entries_iterator i = sym->entries_begin();
1019 i != sym->entries_end(); ++i)
1023 file << indent << "static const uint"
1024 << round_up_bits(sym->def.bits) << "_t "
1025 << **d->name << " = " << d->def.ucon << ";\n";
1028 file << '\n' << indent << "unsigned int _val;\n\n";
1030 file << indent << **sym->name << "()\n"
1032 << indent << "\t_val = 0;\n"
1033 << indent << "}\n\n";
1035 file << indent << **sym->name << "(unsigned int val)\n"
1037 << indent << "\t_val = val;\n"
1038 << indent << "}\n\n";
1040 file << indent << "operator unsigned int()\n"
1042 << indent << "\treturn _val;\n"
1043 << indent << "}\n\n";
1046 file << indent << "};\n";
1055 // Nothing particular to do here, other than to make sure
1056 // that trav_forward has happened (which will always need to
1057 // be done here if it's not a nested type).
1059 output_pass(sym, trav_forward);
1067 void CPPFile::output(BasicType *sym, int pass, void *arg2)
1074 case trav_forward: {
1075 bool do_ns_out = false;
1077 if (indent.indent_level == 0) {
1084 file << indent << "typedef ";
1085 assert(!is_array(sym->def));
1086 cpp_output_type(file, sym->def, false);
1087 file << **sym->name << ";\n";
1096 output_pass(sym, trav_forward);
1104 void CPPFile::output(Alias *sym, int pass, void *arg2)
1111 case trav_forward: {
1112 bool do_ns_out = false;
1114 if (indent.indent_level == 0) {
1121 const String *type = sym->get_concrete_sym()->get_fq_name("_ns")
1124 file << indent << "typedef " << *type << " "
1125 << **sym->name << ";\n";
1134 output_pass(sym, trav_forward);
1142 void CPPFile::output(TypeDef *sym, int pass, void *arg2)
1151 output_pass(sym->get_concrete_sym(), trav_forward);
1153 bool do_ns_out = false;
1155 if (indent.indent_level == 0) {
1162 const String *type = sym->get_concrete_sym()->get_fq_name("_ns")
1165 file << indent << "typedef " << *type << " "
1166 << **sym->name << ";\n";
1179 void CPPFile::output(Datum *sym, int pass, void *arg2)
1181 assert(sym->def.flags.field.Const);
1191 declare_type_dependency(sym->type, false);
1193 bool do_ns_out = false;
1195 if (indent.indent_level == 0) {
1202 file << indent << "static const ";
1204 assert(!is_array(sym->def.basictype));
1207 cpp_output_type(file, sym->type, false, false);
1209 cpp_output_type(file, sym->def.basictype, false);
1211 file << **sym->name << " = ";
1213 CompiledBasicType *def;
1215 Symbol *real_type = sym->type->get_concrete_sym();
1216 BasicType *bt = dynamic_cast<BasicType *>(real_type);
1221 def = &sym->def.basictype;
1224 if (def->flags.field.Float) {
1225 file << sym->def.fcon;
1226 } else if (def->flags.field.Bool) {
1227 if (sym->def.ucon == 0)
1232 if (def->flags.field.Unsigned) {
1233 file << "0x" << std::hex << sym->def.ucon << std::dec << 'U';
1235 file << sym->def.icon;
1254 void CPPBinding::output_root(UserNameSpace *ns, const char *dir)
1256 delete new CPPFile(ns, dir);