| 1 | #include "genns.hpp" |
| 2 | #include "fs.hpp" |
| 3 | #include "function.hpp" |
| 4 | #include "genbase.hpp" |
| 5 | |
| 6 | #include <boost/algorithm/string/classification.hpp> |
| 7 | #include <boost/algorithm/string/erase.hpp> |
| 8 | #include <boost/algorithm/string/join.hpp> |
| 9 | #include <boost/algorithm/string/replace.hpp> |
| 10 | #include <boost/algorithm/string/split.hpp> |
| 11 | |
| 12 | #include <boost/property_tree/xml_parser.hpp> |
| 13 | |
| 14 | #include <iostream> |
| 15 | #include <ostream> |
| 16 | #include <sstream> |
| 17 | #include <tuple> |
| 18 | |
| 19 | namespace |
| 20 | { |
| 21 | class File : public std::ostringstream |
| 22 | { |
| 23 | std::string ns; |
| 24 | std::string fname; |
| 25 | bool preamble; |
| 26 | bool guard; |
| 27 | bool nsdir; |
| 28 | NamespaceGuard nsg; |
| 29 | |
| 30 | static std::string root; |
| 31 | static bool changed; |
| 32 | |
| 33 | public: |
| 34 | // ok, single threaded processing |
| 35 | static void set_root(const std::string &dir) { root = dir; } |
| 36 | |
| 37 | static void set_changed(bool c) { changed = c; } |
| 38 | |
| 39 | static std::string prepdirs( |
| 40 | const std::string &_ns, const std::string &_fname, bool nsdir) |
| 41 | { |
| 42 | fs::path p(root); |
| 43 | if (nsdir) |
| 44 | p /= tolower(_ns); |
| 45 | fs::create_directories(p); |
| 46 | p /= tolower(_fname); |
| 47 | return p.string(); |
| 48 | } |
| 49 | |
| 50 | File(const std::string &_ns, const std::string _fname, bool _need_ns = true, |
| 51 | bool _need_guard = true, bool _nsdir = true) |
| 52 | : ns(_ns), fname(_fname), preamble(_need_ns), guard(_need_guard), |
| 53 | nsdir(_nsdir), nsg(*this) |
| 54 | { |
| 55 | write_pre(); |
| 56 | } |
| 57 | |
| 58 | std::string get_rel_path() const |
| 59 | { |
| 60 | return nsdir ? (fs::path(tolower(ns)) / tolower(fname)).string() |
| 61 | : tolower(fname); |
| 62 | } |
| 63 | |
| 64 | private: |
| 65 | void write_pre() |
| 66 | { |
| 67 | *this << "// AUTO-GENERATED\n\n"; |
| 68 | auto definc = fmt::format("_GI_{}_{}_", toupper(ns), toupper(fname)); |
| 69 | for (auto &v : definc) |
| 70 | if (!isalnum(v)) |
| 71 | v = '_'; |
| 72 | if (guard) { |
| 73 | *this << "#ifndef " << definc << std::endl; |
| 74 | *this << "#define " << definc << std::endl; |
| 75 | *this << std::endl; |
| 76 | } |
| 77 | if (preamble) |
| 78 | nsg.push(ns); |
| 79 | } |
| 80 | |
| 81 | void write_post() |
| 82 | { |
| 83 | if (preamble) |
| 84 | nsg.pop(); |
| 85 | if (guard) |
| 86 | *this << "#endif" << std::endl; |
| 87 | finish(); |
| 88 | } |
| 89 | |
| 90 | static std::string read(const std::string &fname) |
| 91 | { |
| 92 | std::ifstream f(fname); |
| 93 | std::ostringstream oss; |
| 94 | oss << f.rdbuf(); |
| 95 | return oss.str(); |
| 96 | } |
| 97 | |
| 98 | void finish() |
| 99 | { |
| 100 | auto fullname = prepdirs(ns, fname, nsdir); |
| 101 | auto content = str(); |
| 102 | |
| 103 | // this is a bit racy if multiple parties try to write |
| 104 | // worst case multiple will/should write the same content |
| 105 | auto write = changed ? read(fullname) != content : true; |
| 106 | if (write) { |
| 107 | std::ofstream output(fullname); |
| 108 | output << content; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | public: |
| 113 | ~File() { write_post(); } |
| 114 | }; |
| 115 | |
| 116 | std::string File::root; |
| 117 | bool File::changed{}; |
| 118 | |
| 119 | class NamespaceGeneratorImpl : private GeneratorBase, public NamespaceGenerator |
| 120 | { |
| 121 | std::string version_; |
| 122 | std::vector<std::string> deps_; |
| 123 | pt::ptree root_; |
| 124 | pt::ptree tree_; |
| 125 | // helper exp |
| 126 | std::regex re_unqualify_; |
| 127 | bool allow_deprecated_{}; |
| 128 | |
| 129 | public: |
| 130 | NamespaceGeneratorImpl(GeneratorContext &ctx, const std::string &filename) |
| 131 | : GeneratorBase(ctx, "") |
| 132 | { |
| 133 | logger(Log::INFO, "reading {}", filename); |
| 134 | pt::read_xml(filename, root_); |
| 135 | // extract dependencies |
| 136 | for (auto &&n : root_.get_child("repository")) { |
| 137 | if (n.first == "include") { |
| 138 | auto &&name = get_name(n.second); |
| 139 | auto &&version = get_attribute(n.second, AT_VERSION); |
| 140 | deps_.emplace_back(name + "-" + version); |
| 141 | logger(Log::INFO, indent + "dependency " + deps_.back()); |
| 142 | } |
| 143 | } |
| 144 | // all else is in a subtree |
| 145 | tree_ = root_.get_child("repository.namespace"); |
| 146 | ns = get_name(tree_); |
| 147 | version_ = get_attribute(tree_, AT_VERSION); |
| 148 | re_unqualify_ = |
| 149 | std::regex(fmt::format("^{}(::|\\.)", ns), std::regex::optimize); |
| 150 | } |
| 151 | |
| 152 | std::string get_ns() const { return ns + "-" + version_; } |
| 153 | |
| 154 | std::vector<std::string> get_dependencies() const { return deps_; } |
| 155 | |
| 156 | private: |
| 157 | // in most cases no problems arise if both M1.SomeName and M2.SomeOtherName |
| 158 | // both map to GSomeName |
| 159 | // but some generated code, like declare_XXX template specialization assumes |
| 160 | // a 1-to-1 mapping, as should be the case in with sane GIR |
| 161 | // however, if the latter are somehow not sane, then duplicate definitions |
| 162 | // might arise (ODR and all that), although it *really is* a GIR bug |
| 163 | // |
| 164 | // so try to mitigate that here |
| 165 | // check that only 1 (qualified) girname/cppname claims/maps to a ctype |
| 166 | void check_odr(const std::string &cpptype, const std::string &ctype) const |
| 167 | { |
| 168 | // verify this ctype has not been seen before |
| 169 | // a particular ctype can only be defined/claimed by one GIR symbol |
| 170 | // (in one module), otherwise lots of things will go wrong |
| 171 | // GIRs also have an "alias" concept, so that should be used instead |
| 172 | auto conflict = ctx.repo.check_odr(cpptype, ctype); |
| 173 | if (!conflict.empty()) { |
| 174 | throw std::runtime_error( |
| 175 | fmt::format("{} maps to {}, already claimed by {};\n" |
| 176 | "Please verify/fix GIRs and/or add ignore as needed.", |
| 177 | cpptype, ctype, conflict)); |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | std::string make_declare(bool decl_ctype, const std::string &cpptype, |
| 182 | const std::string &ctype) const |
| 183 | { |
| 184 | check_odr(cpptype, ctype); |
| 185 | return fmt::format( |
| 186 | "template<> struct declare_{}_of<{}>\n{{ typedef {} type; }}; ", |
| 187 | (decl_ctype ? "ctype" : "cpptype"), (decl_ctype ? cpptype : ctype), |
| 188 | (decl_ctype ? ctype : cpptype)); |
| 189 | } |
| 190 | |
| 191 | void process_element_alias(const pt::ptree &node, std::ostream &out) const |
| 192 | { |
| 193 | std::ostringstream oss; |
| 194 | auto name = get_name(node); |
| 195 | // skip some stuff |
| 196 | if (name.find("autoptr") != name.npos) |
| 197 | throw skip("autoptr", skip::OK); |
| 198 | auto ctype = get_attribute(node, AT_CTYPE); |
| 199 | |
| 200 | std::string deftype; |
| 201 | TypeInfo tinfo; |
| 202 | auto tnode = node.get_child(EL_TYPE); |
| 203 | auto btype = get_name(tnode); |
| 204 | parse_typeinfo(btype, tinfo); |
| 205 | if (tinfo.flags & TYPE_BASIC) { |
| 206 | // if typedef refers to a basic type, let's refer here to (global |
| 207 | // scope) C type (this is mostly the case, e.g. GstClockTime, etc) |
| 208 | deftype = GI_SCOPE + ctype; |
| 209 | } else if (tinfo.flags) { |
| 210 | assert(tinfo.cpptype.size()); |
| 211 | // otherwise we alias to the corresponding wrapped type |
| 212 | // (e.g. GtkAllocation) |
| 213 | deftype = tinfo.cpptype; |
| 214 | } |
| 215 | auto aliasfmt = "typedef {} {};\n"; |
| 216 | if (deftype.size()) |
| 217 | out << fmt::format(aliasfmt, deftype, name); |
| 218 | // also mind ref type |
| 219 | if (tinfo.flags & TYPE_BOXED) { |
| 220 | assert(deftype.size()); |
| 221 | out << fmt::format( |
| 222 | aliasfmt, deftype + GI_SUFFIX_REF, name + GI_SUFFIX_REF); |
| 223 | } |
| 224 | out << std::endl; |
| 225 | } |
| 226 | |
| 227 | void process_element_enum(const pt::ptree::value_type &n, std::ostream &out, |
| 228 | std::ostream *_out_impl) const |
| 229 | { |
| 230 | auto &kind = n.first; |
| 231 | auto &node = n.second; |
| 232 | std::ostringstream oss; |
| 233 | auto name = get_name(node); |
| 234 | auto ctype = get_attribute(node, AT_CTYPE); |
| 235 | |
| 236 | if (_out_impl) { |
| 237 | auto &out_impl = *_out_impl; |
| 238 | // need to generate namespaced function members |
| 239 | std::ostringstream oss_decl; |
| 240 | std::ostringstream oss_impl; |
| 241 | DepsSet dummy; |
| 242 | for (const auto &n : node) { |
| 243 | if (n.first == EL_FUNCTION) { |
| 244 | process_element_function(n, oss_decl, oss_impl, "", "", dummy); |
| 245 | } |
| 246 | } |
| 247 | if (oss_decl.tellp()) { |
| 248 | auto enumns = name + "NS_"; |
| 249 | NamespaceGuard ns_d(out); |
| 250 | ns_d.push(enumns, false); |
| 251 | out << oss_decl.str(); |
| 252 | |
| 253 | NamespaceGuard ns_i(out_impl); |
| 254 | ns_i.push(enumns, false); |
| 255 | out_impl << oss_impl.str(); |
| 256 | } |
| 257 | return; |
| 258 | } |
| 259 | |
| 260 | // enum name might end up matching (uppercase) define |
| 261 | name = unreserve(name); |
| 262 | |
| 263 | // otherwise generate enum |
| 264 | NamespaceGuard nsg(oss); |
| 265 | nsg.push(ns); |
| 266 | // auto-adjust underlying type to avoid warnings; |
| 267 | // outside the range of underlying type ‘int’ |
| 268 | // (no forward declare of enum in C, so type should be complete) |
| 269 | oss << fmt::format( |
| 270 | "enum class {} : std::underlying_type<{}>::type {{\n", name, ctype); |
| 271 | std::set<std::string> names; |
| 272 | for (const auto &n : node) { |
| 273 | if (n.first == EL_MEMBER) { |
| 274 | auto value = get_attribute(n.second, AT_CIDENTIFIER); |
| 275 | auto name = get_name(n.second); |
| 276 | // some enums/flags declare different names with same value |
| 277 | // which seems to confuse gobject-introspection |
| 278 | // (e.g. GstVideoFrameFlags, GstVideoBufferFlags, etc) |
| 279 | // and then they end up with repeated/duplicate name in GIR |
| 280 | // so let's only pick the first one of those |
| 281 | if (!std::get<1>(names.insert(name))) { |
| 282 | logger(Log::WARNING, "{} skipping duplicate {}", ctype, value); |
| 283 | continue; |
| 284 | } |
| 285 | oss << indent |
| 286 | << fmt::format("{} = {},\n", unreserve(toupper(name)), value); |
| 287 | } |
| 288 | } |
| 289 | oss << "};\n\n"; |
| 290 | // add operators in case of flag = bitfield |
| 291 | if (kind == EL_FLAGS) |
| 292 | oss << fmt::format("GI_FLAG_OPERATORS({})\n\n", name); |
| 293 | nsg.pop(); |
| 294 | |
| 295 | // declare ctype info |
| 296 | nsg.push(GI_REPOSITORY_NS); |
| 297 | name = ns + "::" + name; |
| 298 | oss << make_declare(true, name, ctype) << std::endl; |
| 299 | oss << make_declare(false, name, ctype) << std::endl; |
| 300 | oss << std::endl; |
| 301 | |
| 302 | // declare gtype info |
| 303 | auto gtype = get_attribute(node, AT_GLIB_GET_TYPE, ""); |
| 304 | if (gtype.size()) { |
| 305 | oss << fmt::format("template<> struct declare_gtype_of<{}>\n" |
| 306 | "{{ static GType get_type() {{ return {}(); }} }};", |
| 307 | name, gtype) |
| 308 | << std::endl; |
| 309 | oss << std::endl; |
| 310 | } |
| 311 | |
| 312 | // in case of a flag = bitfield, also mark it as such |
| 313 | if (kind == EL_FLAGS) { |
| 314 | oss << fmt::format( |
| 315 | "template<> struct is_bitfield<{}> : public std::true_type\n" |
| 316 | "{{}};", |
| 317 | name) |
| 318 | << std::endl; |
| 319 | oss << std::endl; |
| 320 | } |
| 321 | |
| 322 | oss << std::endl; |
| 323 | nsg.pop(); |
| 324 | out << oss.str() << std::endl; |
| 325 | } |
| 326 | |
| 327 | void process_element_const(const pt::ptree &node, std::ostream &out) const |
| 328 | { |
| 329 | auto name = get_name(node); |
| 330 | std::ostringstream oss; |
| 331 | try { |
| 332 | ArgInfo tinfo = parse_arginfo(node); |
| 333 | auto cpptype = tinfo.cpptype; |
| 334 | |
| 335 | auto value = get_attribute(node, AT_CTYPE); |
| 336 | std::string cast; |
| 337 | std::string namesuffix; |
| 338 | if (tinfo.flags & TYPE_BASIC) { |
| 339 | if (tinfo.flags & TYPE_CLASS) { |
| 340 | // special string case |
| 341 | // need some suffix to make the variable (pointer) const |
| 342 | cpptype = "gchar"; |
| 343 | namesuffix = "[]"; |
| 344 | } else { |
| 345 | // normalize back to C (e.g. char*) |
| 346 | cpptype = tinfo.ctype; |
| 347 | } |
| 348 | } else { |
| 349 | // need to cast to enum type (flags/enum) |
| 350 | if (tinfo.flags & TYPE_ENUM) { |
| 351 | cast = fmt::format("({}) ", cpptype); |
| 352 | } else if (!(tinfo.flags & TYPE_VALUE)) { |
| 353 | throw skip("constant type " + cpptype + " not supported"); |
| 354 | } |
| 355 | // accept alias typedef |
| 356 | } |
| 357 | // avoid clashes with existing defines |
| 358 | name = unreserve(name); |
| 359 | // NOTE what about an inline var in C++17 |
| 360 | // recall that const implies static in C++ |
| 361 | oss << fmt::format("GI_MODULE_INLINE const {} {}{} = {}{};", cpptype, |
| 362 | name, namesuffix, cast, value); |
| 363 | } catch (skip &ex) { |
| 364 | oss << "// SKIP constant " << name << "; " << ex.what(); |
| 365 | } |
| 366 | out << oss.str() << std::endl << std::endl; |
| 367 | } |
| 368 | |
| 369 | void process_element_property(const pt::ptree::value_type &entry, |
| 370 | std::ostream &out, std::ostream &impl, const std::string &klass, |
| 371 | DepsSet &deps) const |
| 372 | { |
| 373 | auto &node = entry.second; |
| 374 | |
| 375 | ArgInfo tinfo; |
| 376 | auto name = get_name(node); |
| 377 | auto read = get_attribute<int>(node, AT_READABLE, 1); |
| 378 | auto write = get_attribute<int>(node, AT_WRITABLE, 1); |
| 379 | tinfo = parse_arginfo(node); |
| 380 | |
| 381 | auto &&cpptype = tinfo.cpptype; |
| 382 | if (!tinfo.flags) |
| 383 | throw skip("unknown type " + cpptype); |
| 384 | // let's not go here for now |
| 385 | if (tinfo.flags & TYPE_CONTAINER) |
| 386 | throw skip("container property not supported", skip::TODO); |
| 387 | |
| 388 | track_dependency(deps, tinfo); |
| 389 | |
| 390 | // directly write all in decl |
| 391 | auto &oss = out; |
| 392 | (void)impl; |
| 393 | |
| 394 | auto decl_name = name; |
| 395 | std::replace(decl_name.begin(), decl_name.end(), '-', '_'); |
| 396 | std::string proptype = "property_proxy"; |
| 397 | if (!read) { |
| 398 | proptype = "property_proxy_write"; |
| 399 | } else if (!write) { |
| 400 | proptype = "property_proxy_read"; |
| 401 | } |
| 402 | proptype = GI_NS_SCOPED + proptype; |
| 403 | |
| 404 | auto qklass = qualify(klass, TYPE_OBJECT); |
| 405 | if (write) |
| 406 | oss << fmt::format("{3}<{0}, {4}> property_{2}()\n" |
| 407 | "{{ return {3}<{0}, {4}> (*this, \"{1}\"); }}", |
| 408 | cpptype, name, decl_name, proptype, qklass) |
| 409 | << std::endl; |
| 410 | if (read) |
| 411 | oss << fmt::format("const {3}<{0}, {4}> property_{2}() const\n" |
| 412 | "{{ return {3}<{0}, {4}> (*this, \"{1}\"); }}", |
| 413 | cpptype, name, decl_name, proptype, qklass) |
| 414 | << std::endl; |
| 415 | oss << std::endl; |
| 416 | } |
| 417 | |
| 418 | void process_element_field(const pt::ptree::value_type &entry, |
| 419 | std::ostream &out, std::ostream &impl, const std::string &klass, |
| 420 | const std::string &klasstype, DepsSet &deps) const |
| 421 | { |
| 422 | auto &node = entry.second; |
| 423 | |
| 424 | auto name = get_name(node); |
| 425 | auto readable = get_attribute<int>(node, AT_READABLE, 1); |
| 426 | auto writable = get_attribute<int>(node, AT_WRITABLE, 1); |
| 427 | auto priv = get_attribute<int>(node, AT_PRIVATE, 0); |
| 428 | |
| 429 | ArgInfo tinfo; |
| 430 | try { |
| 431 | tinfo = parse_arginfo(node); |
| 432 | |
| 433 | // only very plain basic fields |
| 434 | // type must be known |
| 435 | // no private |
| 436 | // no int* cookies or other strange things |
| 437 | if (!tinfo.flags || priv || |
| 438 | get_pointer_depth(tinfo.ctype) != tinfo.pdepth || |
| 439 | is_volatile(tinfo.ctype)) |
| 440 | return; |
| 441 | } catch (...) { |
| 442 | // simply fail silently here and never mind |
| 443 | return; |
| 444 | } |
| 445 | |
| 446 | if (!(tinfo.flags & (TYPE_VALUE | TYPE_CLASS))) |
| 447 | return; |
| 448 | |
| 449 | // unconditionally reserve this to avoid clash with non-field member |
| 450 | ElementFunction func; |
| 451 | func.kind = EL_METHOD; |
| 452 | func.name = unreserve(name, true); |
| 453 | func.c_id = klasstype + "::" + name; |
| 454 | |
| 455 | Parameter instance; |
| 456 | // real one, no base |
| 457 | parse_typeinfo(klasstype, instance.tinfo); |
| 458 | instance.instance = true; |
| 459 | instance.name = "obj"; |
| 460 | instance.direction = DIR_IN; |
| 461 | |
| 462 | // field parameter |
| 463 | Parameter param; |
| 464 | param.tinfo = tinfo; |
| 465 | param.name = "_value"; |
| 466 | param.transfer = TRANSFER_NOTHING; |
| 467 | |
| 468 | // common helper lambda for below |
| 469 | auto make_wrapper = [&](const std::string &funcname, |
| 470 | const std::string funcdef, |
| 471 | const std::vector<Parameter> ¶ms) { |
| 472 | func.functionexp = funcname; |
| 473 | // buffer result |
| 474 | std::ostringstream oss; |
| 475 | auto def = ::process_element_function( |
| 476 | ctx, ns, func, params, out, oss, klass, klasstype, deps); |
| 477 | // now write all if wrapping accessor did not fail |
| 478 | if (def.name.size()) |
| 479 | impl << funcdef << std::endl << oss.str(); |
| 480 | }; |
| 481 | |
| 482 | // a static helper is used in the following, |
| 483 | // rather than a compact local lambda, |
| 484 | // as the latter does not always cast to any function type |
| 485 | // also, internal casts are applied in the helper to handle enum/int |
| 486 | // and pointer type conversions |
| 487 | |
| 488 | // also, the ctype info is derived from the cpptype as with func |
| 489 | // argument due to either |
| 490 | // + ctype missing, e.g. enum, |
| 491 | // + or minor discrepancies, e.g. gpointer vs gconstpointer |
| 492 | |
| 493 | if (readable) { |
| 494 | param.direction = DIR_RETURN; |
| 495 | param.tinfo.ctype = make_ctype(param.tinfo, param.direction, false); |
| 496 | instance.tinfo.ctype = fmt::format("const {}*", instance.tinfo.dtype); |
| 497 | // define helper function |
| 498 | auto funcname = "_field_" + name + "_get"; |
| 499 | auto funcdef = fmt::format( |
| 500 | "GI_MODULE_STATIC_OR_INLINE {} {} ({} {}) {{ return ({}) obj->{}; }}", |
| 501 | param.tinfo.ctype, funcname, instance.tinfo.ctype, instance.name, |
| 502 | param.tinfo.ctype, name); |
| 503 | make_wrapper(funcname, funcdef, {param, instance}); |
| 504 | } |
| 505 | |
| 506 | // not safe to assume any particular ownership of some struct field |
| 507 | // so let's not meddle with it other than the very basic cases |
| 508 | if (writable && (tinfo.flags & TYPE_VALUE)) { |
| 509 | param.direction = DIR_IN; |
| 510 | param.tinfo.ctype = make_ctype(param.tinfo, param.direction, false); |
| 511 | instance.tinfo.ctype = instance.tinfo.dtype + "*"; |
| 512 | // define helper function |
| 513 | auto funcname = "_field_" + name + "_set"; |
| 514 | auto funcdef = |
| 515 | fmt::format("GI_MODULE_STATIC_OR_INLINE void {} ({} {}, {} {}) {{ " |
| 516 | "obj->{} = (decltype(obj->{})) {}; }}", |
| 517 | funcname, instance.tinfo.ctype, instance.name, param.tinfo.ctype, |
| 518 | param.name, name, name, param.name); |
| 519 | // void return |
| 520 | Parameter vparam; |
| 521 | parse_typeinfo(GIR_VOID, vparam.tinfo); |
| 522 | vparam.direction = DIR_RETURN; |
| 523 | make_wrapper(funcname, funcdef, {vparam, param, instance}); |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | FunctionDefinition process_element_function( |
| 528 | const pt::ptree::value_type &entry, std::ostream &out, std::ostream &impl, |
| 529 | const std::string &klass, const std::string &klasstype, DepsSet &deps, |
| 530 | std::ostream *call_args = nullptr) const |
| 531 | { |
| 532 | return ::process_element_function(ctx, ns, entry, out, impl, klass, |
| 533 | klasstype, deps, call_args, allow_deprecated_); |
| 534 | } |
| 535 | |
| 536 | // unqualify (current ns qualifed) type |
| 537 | std::string unqualify(const std::string &name) const |
| 538 | { |
| 539 | return std::regex_replace(name, re_unqualify_, ""); |
| 540 | } |
| 541 | |
| 542 | static std::string get_record_filename(const std::string &rname, bool impl) |
| 543 | { |
| 544 | return tolower(rname) + (impl ? "_impl" : "") + ".hpp"; |
| 545 | } |
| 546 | |
| 547 | static std::string make_include(const std::string &hname, bool local) |
| 548 | { |
| 549 | if (hname.empty()) |
| 550 | return hname; |
| 551 | char open = local ? '"' : '<'; |
| 552 | char close = local ? '"' : '>'; |
| 553 | std::ostringstream oss; |
| 554 | oss << "#include " << open << hname << close; |
| 555 | return oss.str(); |
| 556 | } |
| 557 | |
| 558 | // make include for a qualified dependency |
| 559 | // (not in current namespace) |
| 560 | std::string make_dep_include(const std::string &girname) const |
| 561 | { |
| 562 | auto lname = unqualify(girname); |
| 563 | return lname.size() && !is_qualified(lname) |
| 564 | ? make_include(get_record_filename(lname, false), true) + '\n' |
| 565 | : EMPTY; |
| 566 | } |
| 567 | |
| 568 | std::string make_dep_declare(const DepsSet &deps, bool only_ns = false) const |
| 569 | { |
| 570 | std::ostringstream oss; |
| 571 | NamespaceGuard nsg(oss); |
| 572 | std::string last_ns; |
| 573 | for (const auto &de : deps) { |
| 574 | if (last_ns != de.first) { |
| 575 | oss << std::endl; |
| 576 | nsg.pop(); |
| 577 | last_ns = de.first; |
| 578 | if (!last_ns.empty()) |
| 579 | nsg.push(last_ns, false); |
| 580 | } |
| 581 | if (only_ns && last_ns.empty()) |
| 582 | continue; |
| 583 | const auto &d = de.second; |
| 584 | if (d.size() > 7 && (d[6] == ' ' || d[7] == ' ')) { |
| 585 | // there is a space in there, so it is not just a type name |
| 586 | // but rather struct/class XXX (a CallArgs case) |
| 587 | oss << d << ';' << std::endl; |
| 588 | continue; |
| 589 | } |
| 590 | auto c = unqualify(d); |
| 591 | if (!is_qualified(c)) |
| 592 | oss << "class " << c << ";" << std::endl; |
| 593 | } |
| 594 | // apparently guard destructor runs too late |
| 595 | nsg.pop(); |
| 596 | return oss.str(); |
| 597 | } |
| 598 | |
| 599 | static std::string make_conditional_include( |
| 600 | const std::string &hname, bool local) |
| 601 | { |
| 602 | if (hname.empty()) |
| 603 | return hname; |
| 604 | |
| 605 | auto templ = R"|( |
| 606 | #if defined(__has_include) |
| 607 | #if __has_include({}{}{}) |
| 608 | #include {}{}{} |
| 609 | #endif |
| 610 | #endif |
| 611 | )|"; |
| 612 | char open = local ? '"' : '<'; |
| 613 | char close = local ? '"' : '>'; |
| 614 | return fmt::format(templ, open, hname, close, open, hname, close); |
| 615 | } |
| 616 | |
| 617 | static constexpr const char *const CLASS_PLACEHOLDER{"CLASS_PLACEHOLDER"}; |
| 618 | |
| 619 | // minor convencience helper type |
| 620 | struct TypeClassInfo |
| 621 | { |
| 622 | // (optionally) gir qualified |
| 623 | std::string parentgir; |
| 624 | // info on type-struct |
| 625 | TypeInfo ti; |
| 626 | }; |
| 627 | |
| 628 | TypeClassInfo collect_type_class_info(const std::string &girname) const |
| 629 | { |
| 630 | // class struct is skipped above, |
| 631 | // but we do look for it here to find *Class/*Interface struct |
| 632 | TypeClassInfo result; |
| 633 | auto &repo = ctx.repo; |
| 634 | auto &node = repo.tree(girname).second; |
| 635 | result.parentgir = get_attribute(node, AT_PARENT, ""); |
| 636 | // NOTE parent might be in different ns |
| 637 | if (result.parentgir.size()) |
| 638 | result.parentgir = repo.qualify(result.parentgir, girname); |
| 639 | auto cpptype = get_attribute(node, AT_GLIB_TYPE_STRUCT, ""); |
| 640 | if (cpptype.empty()) |
| 641 | throw skip(girname + " missing type-struct info"); |
| 642 | cpptype = repo.qualify(cpptype, girname); |
| 643 | parse_typeinfo(cpptype, result.ti); |
| 644 | // more useful in this setting |
| 645 | result.ti.cpptype = unqualify(result.ti.cpptype); |
| 646 | if (result.ti.dtype.empty()) |
| 647 | throw skip(girname + " missing C type-struct info"); |
| 648 | return result; |
| 649 | } |
| 650 | |
| 651 | typedef std::vector<std::pair<std::string, FunctionDefinition>> |
| 652 | VirtualMethods; |
| 653 | void process_element_record_class(const pt::ptree::value_type &entry, |
| 654 | const std::vector<TypeInfo> &interfaces, const std::string &decl, |
| 655 | const std::string &impl, const VirtualMethods &methods, |
| 656 | std::ostream &out_decl, std::ostream &out_impl) const |
| 657 | { |
| 658 | auto &node = entry.second; |
| 659 | auto name = get_name(node); |
| 660 | bool interface = (entry.first == EL_INTERFACE); |
| 661 | |
| 662 | // run up parent hierarchy to check if all those can be properly |
| 663 | // generated |
| 664 | auto class_info = collect_type_class_info(name); |
| 665 | TypeClassInfo parent_class_info; |
| 666 | // no parent for interface |
| 667 | if (class_info.parentgir.size()) { |
| 668 | parent_class_info = collect_type_class_info(class_info.parentgir); |
| 669 | auto rec_info = parent_class_info; |
| 670 | while (rec_info.parentgir.size()) |
| 671 | rec_info = collect_type_class_info(rec_info.parentgir); |
| 672 | } |
| 673 | |
| 674 | // collect ok interfaces |
| 675 | std::vector<TypeInfo> itfs_info; |
| 676 | for (auto &&itf : interfaces) { |
| 677 | try { |
| 678 | itfs_info.emplace_back(collect_type_class_info(itf.girname).ti); |
| 679 | // base class needs to be declared |
| 680 | out_decl << make_dep_include(itf.girname); |
| 681 | } catch (...) { |
| 682 | } |
| 683 | } |
| 684 | out_decl << std::endl; |
| 685 | |
| 686 | // put into inner namespace to avoid name clash |
| 687 | // declaration part |
| 688 | NamespaceGuard ns_decl(out_decl); |
| 689 | ns_decl.push(ns); |
| 690 | |
| 691 | ns_decl.push(GI_NS_IMPL); |
| 692 | ns_decl.push(GI_NS_INTERNAL); |
| 693 | |
| 694 | // class definition |
| 695 | // explicitly specify a non-public non-virtual destructor |
| 696 | auto def_templ = R"|( |
| 697 | class {0} |
| 698 | {{ |
| 699 | typedef {0} self; |
| 700 | public: |
| 701 | typedef {1} instance_type; |
| 702 | typedef {2} {3}_type; |
| 703 | |
| 704 | {6} |
| 705 | struct TypeInitData; |
| 706 | |
| 707 | protected: |
| 708 | {5} ~{0}() = default; |
| 709 | static {5} void {3}_init (gpointer {3}_struct, gpointer ); |
| 710 | |
| 711 | {4} |
| 712 | }}; |
| 713 | |
| 714 | )|"; |
| 715 | |
| 716 | std::string kind = interface ? "interface" : "class"; |
| 717 | const auto klassnamedef = class_info.ti.cpptype + "Def"; |
| 718 | // used for interfaces |
| 719 | const std::string suffix_class_impl = "ClassImpl"; |
| 720 | const auto klassname = |
| 721 | class_info.ti.cpptype + (interface ? suffix_class_impl : ""); |
| 722 | |
| 723 | // conflict check and type init lists |
| 724 | std::string conflict_check, type_init, type_init_calc; |
| 725 | conflict_check.reserve(1024); |
| 726 | type_init.reserve(1024); |
| 727 | type_init_calc.reserve(1024); |
| 728 | std::string calc_indent = indent + indent; |
| 729 | for (auto &&method : methods) { |
| 730 | auto &&n = method.first; |
| 731 | conflict_check += |
| 732 | fmt::format("using GI_MEMBER_CHECK_CONFLICT({}) = self;\n", n); |
| 733 | type_init += |
| 734 | fmt::format("{}GI_MEMBER_DEFINE({}, {})\n", indent, klassname, n); |
| 735 | type_init_calc += |
| 736 | fmt::format("{}{}GI_MEMBER_HAS_DEFINITION(SubClass, DefData, {})", |
| 737 | type_init_calc.empty() ? "" : ",\n", calc_indent, n); |
| 738 | } |
| 739 | |
| 740 | // (note that e.g. xlib cases prefix might not help, or ns interference) |
| 741 | // drop inline mark on pure virtual interface functions; |
| 742 | // does not quite make sense and might otherwise lead to compiler |
| 743 | // warnings |
| 744 | static const std::regex re_inline(GI_INLINE + ' ', std::regex::optimize); |
| 745 | out_decl << fmt::format(def_templ, klassnamedef, qualify(name, TYPE_OBJECT), |
| 746 | class_info.ti.dtype, kind, std::regex_replace(decl, re_inline, ""), |
| 747 | GI_INLINE, conflict_check); |
| 748 | if (interface) |
| 749 | out_decl << fmt::format("using {}Impl = detail::InterfaceImpl<{}>;", name, |
| 750 | klassnamedef) |
| 751 | << std::endl; |
| 752 | |
| 753 | // avoid ambiguous unqualified name lookup of friend class below |
| 754 | // (as the scopes of parent classes are involved then as well) |
| 755 | auto class_templ = R"|( |
| 756 | class {0}: public {1} |
| 757 | {{ |
| 758 | friend class internal::{4}; |
| 759 | typedef {0} self; |
| 760 | typedef {1} super; |
| 761 | |
| 762 | protected: |
| 763 | using super::super; |
| 764 | {2} |
| 765 | {3} |
| 766 | }}; |
| 767 | |
| 768 | )|"; |
| 769 | |
| 770 | auto classextra = R"|( |
| 771 | private: |
| 772 | // make local helpers private |
| 773 | using super::get_struct_; |
| 774 | using super::gobj_; |
| 775 | |
| 776 | protected: |
| 777 | // disambiguation helper types |
| 778 | {} |
| 779 | )|"; |
| 780 | |
| 781 | // qualification helper; ensure impl::internal qualified |
| 782 | // (also adds current ns if needed) |
| 783 | auto implqualify = [&](const std::string &cpptype) { |
| 784 | auto result = cpptype; |
| 785 | auto pos = result.find(GI_SCOPE.c_str()); |
| 786 | auto insert = GI_SCOPE + GI_NS_IMPL + GI_SCOPE + GI_NS_INTERNAL; |
| 787 | if (pos != result.npos) { |
| 788 | result.insert(pos, insert); |
| 789 | } else { |
| 790 | insert = ns + insert + GI_SCOPE; |
| 791 | result.insert(0, insert); |
| 792 | } |
| 793 | return result; |
| 794 | }; |
| 795 | |
| 796 | // collect interface types and helper types |
| 797 | std::ostringstream oss_types; |
| 798 | std::vector<std::string> itfs; |
| 799 | for (auto &&itf : itfs_info) { |
| 800 | auto kname = itf.cpptype + suffix_class_impl; |
| 801 | kname = implqualify(kname); |
| 802 | itfs.push_back(kname); |
| 803 | // fall back to ctype as it needs to be fully qualified |
| 804 | static const std::regex re_descope(GI_SCOPE, std::regex::optimize); |
| 805 | auto tprefix = std::regex_replace(itf.dtype, re_descope, ""); |
| 806 | oss_types << fmt::format("typedef {} {}_type;\n", kname, tprefix); |
| 807 | } |
| 808 | auto extra = interface ? EMPTY : fmt::format(classextra, oss_types.str()); |
| 809 | // determine baseclasses |
| 810 | // qualified klassnamedef to avoid ambiguous parent class lookup |
| 811 | // (when used as a typedef within class) |
| 812 | std::string superclass = |
| 813 | interface ? fmt::format("detail::InterfaceClassImpl<{}>", name + "Impl") |
| 814 | : fmt::format("detail::ClassTemplate<{}, {}{}{}>", |
| 815 | implqualify(klassnamedef), |
| 816 | implqualify(parent_class_info.ti.cpptype), |
| 817 | itfs.size() ? ", " : "", |
| 818 | boost::algorithm::join(itfs, ", ")); |
| 819 | static const std::regex re_virtual("virtual ", std::regex::optimize); |
| 820 | static const std::regex re_pure("= 0", std::regex::optimize); |
| 821 | auto idecl = std::regex_replace(decl, re_virtual, ""); |
| 822 | idecl = std::regex_replace(idecl, re_pure, "override"); |
| 823 | // add macro for optional warning suppression |
| 824 | if (!interface) |
| 825 | out_decl << GI_CLASS_IMPL_BEGIN << std::endl << std::endl; |
| 826 | out_decl << fmt::format( |
| 827 | class_templ, klassname, superclass, extra, idecl, klassnamedef); |
| 828 | |
| 829 | // type init |
| 830 | // at this later stage as it contains template definitions |
| 831 | // that refer to the above class |
| 832 | auto type_init_templ = R"|( |
| 833 | struct {0}::TypeInitData |
| 834 | {{ |
| 835 | {1} |
| 836 | template<typename SubClass> |
| 837 | constexpr static TypeInitData factory() |
| 838 | {{ |
| 839 | {3}using DefData = detail::DefinitionData<SubClass, TypeInitData>; |
| 840 | return {{ |
| 841 | {2} |
| 842 | }}; |
| 843 | }} |
| 844 | }}; |
| 845 | )|"; |
| 846 | out_decl << fmt::format(type_init_templ, klassnamedef, type_init, |
| 847 | type_init_calc, methods.empty() ? "// " : ""); |
| 848 | // end internal ns |
| 849 | ns_decl.pop(1); |
| 850 | |
| 851 | if (!interface) |
| 852 | out_decl << GI_CLASS_IMPL_END << std::endl |
| 853 | << std::endl |
| 854 | << fmt::format("using {}Impl = detail::ObjectImpl<{}, {}::{}>;", |
| 855 | name, name, GI_NS_INTERNAL, klassname) |
| 856 | << std::endl |
| 857 | << std::endl; |
| 858 | |
| 859 | // implementation part |
| 860 | NamespaceGuard ns_impl(out_impl); |
| 861 | ns_impl.push(ns); |
| 862 | |
| 863 | ns_impl.push(GI_NS_IMPL); |
| 864 | ns_impl.push(GI_NS_INTERNAL); |
| 865 | |
| 866 | auto &ctype = class_info.ti.dtype; |
| 867 | out_impl << fmt::format( |
| 868 | "void {}::{}_init (gpointer {}_struct, gpointer factory)\n{{\n", |
| 869 | klassnamedef, kind, kind); |
| 870 | out_impl << indent |
| 871 | << fmt::format( |
| 872 | "{} *methods = ({} *) {}_struct;\n", ctype, ctype, kind); |
| 873 | // avoid warning if no methods |
| 874 | out_impl << indent << "(void) methods;" << std::endl << std::endl; |
| 875 | // init data from factory |
| 876 | out_impl |
| 877 | << indent |
| 878 | << "auto init_data = GI_MEMBER_INIT_DATA(TypeInitData, factory);\n"; |
| 879 | out_impl << indent << "(void) init_data;" << std::endl << std::endl; |
| 880 | |
| 881 | for (auto &&method : methods) { |
| 882 | auto &&n = method.first; |
| 883 | auto &&def = method.second; |
| 884 | std::vector<std::string> args, transfers; |
| 885 | for (auto &&arg : def.cpp_decl) |
| 886 | args.push_back(arg.second); |
| 887 | auto cpp_return = |
| 888 | def.cpp_outputs.size() ? def.cpp_outputs[0].type : CPP_VOID; |
| 889 | auto sig = fmt::format( |
| 890 | "{} (*) ({})", cpp_return, boost::algorithm::join(args, ", ")); |
| 891 | std::string transferargs, precheck; |
| 892 | if (def.arg_traits.empty()) { |
| 893 | // only arrange to call the raw methods |
| 894 | // if new-style detection (or explicit specification) is in place |
| 895 | precheck = " && factory"; |
| 896 | transferargs = "std::nullptr_t"; |
| 897 | } else { |
| 898 | transferargs = make_arg_traits(def.arg_traits, def.c_sig); |
| 899 | } |
| 900 | // add a hard cast to deal with const differences (e.g. string vs |
| 901 | // const char*) |
| 902 | out_impl << indent |
| 903 | << fmt::format("if (init_data.{0}{3}) methods->{0} = (decltype " |
| 904 | "(methods->{0})) " |
| 905 | "gi::detail::method_wrapper<self, {1}, " |
| 906 | "{2}>::wrapper<&self::{0}_>;", |
| 907 | n, sig, transferargs, precheck) |
| 908 | << std::endl; |
| 909 | } |
| 910 | out_impl << "}" << std::endl << std::endl; |
| 911 | |
| 912 | auto iimpl = |
| 913 | std::regex_replace(impl, std::regex(CLASS_PLACEHOLDER), klassname); |
| 914 | out_impl << iimpl; |
| 915 | } |
| 916 | |
| 917 | std::vector<TypeInfo> record_collect_interfaces( |
| 918 | const pt::ptree::value_type &entry, const TypeInfo ¤t, |
| 919 | const TypeInfo &parent, DepsSet &deps) const |
| 920 | { |
| 921 | auto &repo = ctx.repo; |
| 922 | auto &node = entry.second; |
| 923 | auto name = get_name(node); |
| 924 | |
| 925 | // listed interfaces also include parent's interfaces |
| 926 | // so we should subtract those for good measure |
| 927 | |
| 928 | auto collect_interfaces = [&](const pt::ptree &node, |
| 929 | const std::string &basename) { |
| 930 | std::set<std::string> result; |
| 931 | auto p = node.equal_range(EL_IMPLEMENTS); |
| 932 | assert(basename.size()); |
| 933 | for (auto &q = p.first; q != p.second; ++q) { |
| 934 | auto n = get_name(q->second, std::nothrow); |
| 935 | if (n.size()) { |
| 936 | result.insert(repo.qualify(n, basename)); |
| 937 | } |
| 938 | } |
| 939 | return result; |
| 940 | }; |
| 941 | |
| 942 | std::vector<std::string> itfs; |
| 943 | // ensure all GIR names fully qualified |
| 944 | auto itfs_local = collect_interfaces(node, current.girname); |
| 945 | if (parent.girname.size()) { |
| 946 | // qualify relative to parent |
| 947 | auto itfs_parent = |
| 948 | collect_interfaces(repo.tree(parent.girname).second, parent.girname); |
| 949 | // only keep those not matching a parent's interface (both fully |
| 950 | // qualified) |
| 951 | for (auto &&n : itfs_local) { |
| 952 | if (itfs_parent.find(n) == itfs_parent.end()) |
| 953 | itfs.push_back(n); |
| 954 | } |
| 955 | } else { |
| 956 | std::copy(itfs_local.begin(), itfs_local.end(), std::back_inserter(itfs)); |
| 957 | } |
| 958 | |
| 959 | std::vector<TypeInfo> interfaces; |
| 960 | for (auto &n : itfs) { |
| 961 | TypeInfo itf; |
| 962 | parse_typeinfo(n, itf); |
| 963 | // should know about this interface by now |
| 964 | // but let's continue anyway |
| 965 | if (!itf.flags) { |
| 966 | logger(Log::WARNING, "{} implements unknown interface {}", name, n); |
| 967 | continue; |
| 968 | } |
| 969 | deps.insert({"", itf.cpptype}); |
| 970 | interfaces.push_back(itf); |
| 971 | } |
| 972 | |
| 973 | return interfaces; |
| 974 | } |
| 975 | |
| 976 | // returns (decl header name, impl header name) |
| 977 | std::tuple<std::string, std::string> process_element_record( |
| 978 | const pt::ptree::value_type &entry, bool onlyverify, |
| 979 | std::ostream *call_args = nullptr) const |
| 980 | { |
| 981 | auto &kind = entry.first; |
| 982 | auto &node = entry.second; |
| 983 | auto name = get_name(node); |
| 984 | |
| 985 | logger(Log::LOG, "checking {} {} {}", kind, name, onlyverify); |
| 986 | |
| 987 | TypeInfo current; |
| 988 | parse_typeinfo(name, current); |
| 989 | // only consider real type, but keep info around |
| 990 | if (!current.flags) |
| 991 | return std::make_tuple("", ""); |
| 992 | auto &ctype = current.dtype; |
| 993 | // could happen for a GType only case (e.g. GstFraction) |
| 994 | if (!ctype.size()) |
| 995 | throw skip("no c:type info"); |
| 996 | bool is_object_base = (current.girname == GIR_GOBJECT); |
| 997 | |
| 998 | // check parent |
| 999 | TypeInfo parent; |
| 1000 | auto &repo = ctx.repo; |
| 1001 | // 1 special case here |
| 1002 | if (kind == EL_OBJECT && !is_object_base) { |
| 1003 | auto parentgir = get_attribute(node, AT_PARENT, ""); |
| 1004 | if (parentgir.empty()) |
| 1005 | throw skip("missing parent class"); |
| 1006 | parse_typeinfo(parentgir, parent); |
| 1007 | if (!parent.flags) |
| 1008 | throw skip("unknown parent " + parentgir); |
| 1009 | // parent might be found, but might have problems of its own |
| 1010 | // so that needs to be checked recursively |
| 1011 | // (at least if within current ns) |
| 1012 | if (onlyverify && !is_qualified(parentgir)) { |
| 1013 | try { |
| 1014 | process_element_record(repo.tree(parentgir), onlyverify); |
| 1015 | } catch (const skip &ex) { |
| 1016 | throw skip(std::string("parent problem; ") + ex.what()); |
| 1017 | } |
| 1018 | } |
| 1019 | } |
| 1020 | |
| 1021 | if (onlyverify || is_object_base) |
| 1022 | return std::make_tuple("", ""); |
| 1023 | |
| 1024 | // no throwing or giving up after this point |
| 1025 | // we have now committed to coming up with a class |
| 1026 | // declaration/definition (even if it is a pretty empty one) |
| 1027 | logger(Log::LOG, "processing {} {}", kind, name); |
| 1028 | |
| 1029 | // generate classes in subnamespace |
| 1030 | const std::string nsbase("base"); |
| 1031 | auto namebase = name + "Base"; |
| 1032 | // avoid qualification later on with standard ns (e.g. Gst) |
| 1033 | auto qnamebase = nsbase + "::" + namebase; |
| 1034 | |
| 1035 | // collect output of members and their dependencies |
| 1036 | std::ostringstream oss_decl, oss_class_decl; |
| 1037 | std::ostringstream oss_impl, oss_class_impl; |
| 1038 | VirtualMethods vmethods; |
| 1039 | DepsSet deps; |
| 1040 | for (const auto &n : node) { |
| 1041 | auto el = n.first; |
| 1042 | int introspectable = get_attribute<int>(n.second, AT_INTROSPECTABLE, 1); |
| 1043 | try { |
| 1044 | // try even if marked introspectable |
| 1045 | // some are useful in non-runtime setting |
| 1046 | if (el == EL_FUNCTION || el == EL_CONSTRUCTOR || el == EL_METHOD || |
| 1047 | el == EL_SIGNAL) { |
| 1048 | process_element_function( |
| 1049 | n, oss_decl, oss_impl, qnamebase, name, deps, call_args); |
| 1050 | } else if (el == EL_VIRTUAL_METHOD && introspectable && |
| 1051 | ctx.options.classimpl) { |
| 1052 | // placeholder replaced suitably later on |
| 1053 | auto def = process_element_function(n, oss_class_decl, oss_class_impl, |
| 1054 | CLASS_PLACEHOLDER, name, deps, call_args); |
| 1055 | if (def.name.size()) |
| 1056 | vmethods.push_back({def.name, def}); |
| 1057 | } else if (el == EL_FIELD && introspectable) { |
| 1058 | process_element_field(n, oss_decl, oss_impl, qnamebase, name, deps); |
| 1059 | } else if (el == EL_PROPERTY && introspectable) { |
| 1060 | process_element_property(n, oss_decl, oss_impl, qnamebase, deps); |
| 1061 | } |
| 1062 | } catch (std::runtime_error &ex) { |
| 1063 | handle_exception(n, ex); |
| 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | // actual output to file |
| 1068 | auto fname_decl = get_record_filename(name, false); |
| 1069 | File out_decl(ns, fname_decl, false); |
| 1070 | auto fname_impl = get_record_filename(name, true); |
| 1071 | File out_impl(ns, fname_impl, false); |
| 1072 | |
| 1073 | // a superclass needs full declaration (not only forward declaration) |
| 1074 | // skip include for GObject, which would lead to object.hpp, |
| 1075 | // which is not generated in GObject namespace, but rather provided by gi |
| 1076 | // so it actually references gi/object.hpp, which is only works if gi/ |
| 1077 | // is part of the include path, which is a bit confusing/messy |
| 1078 | // (as it is/should always be referenced by gi/xyz, e.g. gi/gi.hpp) |
| 1079 | if (parent.girname != GIR_GOBJECT) |
| 1080 | out_decl << make_dep_include(parent.girname); |
| 1081 | out_decl << std::endl; |
| 1082 | |
| 1083 | // implemented interfaces are also dependency |
| 1084 | std::vector<TypeInfo> interfaces = |
| 1085 | record_collect_interfaces(entry, current, parent, deps); |
| 1086 | |
| 1087 | // namespace in decl before forward class decl |
| 1088 | NamespaceGuard ns_decl(out_decl); |
| 1089 | ns_decl.push(ns); |
| 1090 | |
| 1091 | // all declarations are included prior to implementation |
| 1092 | // forward class declarations in declaration |
| 1093 | deps.erase({"", current.cpptype}); |
| 1094 | out_decl << make_dep_declare(deps); |
| 1095 | out_decl << std::endl; |
| 1096 | |
| 1097 | // also forward declare 'oneself' since only base is defined below |
| 1098 | out_decl << "class " << name << ";" << std::endl; |
| 1099 | out_decl << std::endl; |
| 1100 | |
| 1101 | // namespace in impl following includes |
| 1102 | NamespaceGuard ns_impl(out_impl); |
| 1103 | ns_impl.push(ns); |
| 1104 | |
| 1105 | // base class subnamespace |
| 1106 | ns_decl.push(nsbase); |
| 1107 | ns_impl.push(nsbase); |
| 1108 | |
| 1109 | // class definition |
| 1110 | auto obj_templ = R"|( |
| 1111 | #define {3} {4}::{0} |
| 1112 | class {0} : public {2} |
| 1113 | {{ |
| 1114 | typedef {2} super_type; |
| 1115 | public: |
| 1116 | typedef {1} BaseObjectType; |
| 1117 | |
| 1118 | {0} (std::nullptr_t = nullptr) : super_type() {{}} |
| 1119 | |
| 1120 | BaseObjectType *gobj_() {{ return (BaseObjectType*) super_type::gobj_(); }} |
| 1121 | const BaseObjectType *gobj_() const {{ return (const BaseObjectType*) super_type::gobj_(); }} |
| 1122 | BaseObjectType *gobj_copy_() const {{ return (BaseObjectType*) super_type::gobj_copy_(); }} |
| 1123 | |
| 1124 | )|"; |
| 1125 | |
| 1126 | auto boxed_templ = R"|( |
| 1127 | #define {2} {3}::{0} |
| 1128 | class {0} : public {1} |
| 1129 | {{ |
| 1130 | typedef {1} super_type; |
| 1131 | public: |
| 1132 | |
| 1133 | {0} (std::nullptr_t = nullptr) : super_type() {{}} |
| 1134 | |
| 1135 | )|"; |
| 1136 | |
| 1137 | auto gtype = get_attribute(node, AT_GLIB_GET_TYPE, ""); |
| 1138 | auto basedef = toupper(fmt::format("GI_{}_{}_BASE", ns, name)); |
| 1139 | // class definition |
| 1140 | bool is_variant = false; |
| 1141 | if (kind == EL_OBJECT) { |
| 1142 | out_decl << fmt::format( |
| 1143 | obj_templ, namebase, ctype, parent.cpptype, basedef, nsbase); |
| 1144 | } else if (kind == EL_INTERFACE) { |
| 1145 | out_decl << fmt::format( |
| 1146 | obj_templ, namebase, ctype, "gi::InterfaceBase", basedef, nsbase); |
| 1147 | } else { |
| 1148 | auto tmpl = GI_NS_DETAIL_SCOPED + |
| 1149 | (gtype.size() ? "GBoxedWrapperBase" : "CBoxedWrapperBase"); |
| 1150 | auto parent = fmt::format("{}<{}, {}>", tmpl, namebase, ctype); |
| 1151 | // override if special fundamental boxed-like case |
| 1152 | if ((is_variant = (current.girname == GIR_GVARIANT))) { |
| 1153 | parent = "detail::VariantWrapper"; |
| 1154 | gtype.clear(); |
| 1155 | } |
| 1156 | out_decl << fmt::format(boxed_templ, namebase, parent, basedef, nsbase); |
| 1157 | } |
| 1158 | |
| 1159 | if (gtype.size()) { |
| 1160 | out_decl << fmt::format( |
| 1161 | "static GType get_type_ () G_GNUC_CONST {{ return {}(); }} ", gtype); |
| 1162 | out_decl << std::endl << std::endl; |
| 1163 | } |
| 1164 | |
| 1165 | // add some helpers to obtain known implemented interfaces |
| 1166 | for (auto &&_itf : interfaces) { |
| 1167 | // temp extend for wrapping below |
| 1168 | ArgInfo itf; |
| 1169 | (TypeInfo &)itf = _itf; |
| 1170 | auto decl_fmt = "{0} {1}interface_ (gi::interface_tag<{0}>)"; |
| 1171 | auto impl_fmt = decl_fmt; |
| 1172 | auto decl = fmt::format(decl_fmt, itf.cpptype, ""); |
| 1173 | out_decl << GI_INLINE << ' ' << decl << ";" << std::endl << std::endl; |
| 1174 | // wrap a properly casted extra reference (ensure no sink'ing) |
| 1175 | auto impl = fmt::format(impl_fmt, itf.cpptype, (namebase + "::")); |
| 1176 | out_impl << impl << std::endl; |
| 1177 | auto towrap = |
| 1178 | fmt::format("({}::BaseObjectType*) gobj_copy_()", itf.cpptype); |
| 1179 | auto w = |
| 1180 | fmt::format(make_wrap_format(ArgInfo{itf}, TRANSFER_FULL), towrap); |
| 1181 | out_impl << fmt::format("{{ return {}; }}", w) << std::endl << std::endl; |
| 1182 | // conversion operator |
| 1183 | auto op_decl = fmt::format("operator {} ()", itf.cpptype); |
| 1184 | auto op_impl = fmt::format( |
| 1185 | "{}::{}\n{{ return interface_ (gi::interface_tag<{}>()); }}", |
| 1186 | namebase, op_decl, itf.cpptype); |
| 1187 | out_decl << GI_INLINE << ' ' << op_decl << ';' << std::endl << std::endl; |
| 1188 | out_impl << op_impl << std::endl << std::endl; |
| 1189 | } |
| 1190 | |
| 1191 | out_decl << oss_decl.str(); |
| 1192 | out_decl << "}; // class" << std::endl; |
| 1193 | out_decl << std::endl; |
| 1194 | ns_decl.pop(); |
| 1195 | |
| 1196 | out_impl << oss_impl.str(); |
| 1197 | out_impl << std::endl; |
| 1198 | ns_impl.pop(); |
| 1199 | |
| 1200 | // optionally include supplements/overrides for this class |
| 1201 | auto include_extra = [&](File &out, bool impl) { |
| 1202 | for (auto &&suffix : {"_extra_def", "_extra"}) { |
| 1203 | auto header = |
| 1204 | (fs::path(tolower(ns)) / get_record_filename(name + suffix, impl)) |
| 1205 | .string(); |
| 1206 | out << make_conditional_include(header, false) << std::endl; |
| 1207 | } |
| 1208 | }; |
| 1209 | include_extra(out_decl, false); |
| 1210 | include_extra(out_impl, true); |
| 1211 | |
| 1212 | ns_decl.push(GI_REPOSITORY_NS); |
| 1213 | // make fragment to define final class |
| 1214 | { |
| 1215 | NamespaceGuard nst(out_decl); |
| 1216 | nst.push(ns, false); |
| 1217 | auto supertype = basedef; |
| 1218 | auto reftype = name + GI_SUFFIX_REF; |
| 1219 | if (kind != EL_OBJECT && kind != EL_INTERFACE && !is_variant) { |
| 1220 | // boxed base templates define a few members with CppType return type |
| 1221 | // so in the CppTypeBase defined above that is still CppTypeBase |
| 1222 | // again add the template as subclass but now with final CppType |
| 1223 | auto tmpl = GI_NS_DETAIL_SCOPED + |
| 1224 | (gtype.size() ? "GBoxedWrapper" : "CBoxedWrapper"); |
| 1225 | supertype = fmt::format( |
| 1226 | "{}<{}, {}, {}, {}>", tmpl, name, ctype, basedef, reftype); |
| 1227 | // forward declaration of corresponding ref type |
| 1228 | out_decl << fmt::format("class {};", reftype) << std::endl << std::endl; |
| 1229 | } |
| 1230 | auto fmtclass = |
| 1231 | "class {0} : public {1}\n" |
| 1232 | "{{ typedef {1} super_type; using super_type::super_type; }};\n"; |
| 1233 | out_decl << fmt::format(fmtclass, name, supertype) << std::endl; |
| 1234 | |
| 1235 | // in case of boxed, also define ref type |
| 1236 | if (kind != EL_OBJECT && kind != EL_INTERFACE && !is_variant) { |
| 1237 | auto tmpl = GI_NS_DETAIL_SCOPED + |
| 1238 | (gtype.size() ? "GBoxedRefWrapper" : "CBoxedRefWrapper"); |
| 1239 | supertype = fmt::format("{}<{}, {}, {}>", tmpl, name, ctype, basedef); |
| 1240 | out_decl << std::endl |
| 1241 | << fmt::format(fmtclass, reftype, supertype) << std::endl; |
| 1242 | } |
| 1243 | } |
| 1244 | // GInitiallyUnowned is typedef'ed to GObject |
| 1245 | // so we have to avoid duplicate definition |
| 1246 | // FIXME generally no way to check for that using Gir, provide some |
| 1247 | // override here ?? |
| 1248 | if (current.girname != GIR_GINITIALLYUNOWNED) { |
| 1249 | // declare type info |
| 1250 | out_decl << make_declare(false, ns + "::" + name, ctype) << std::endl; |
| 1251 | out_decl << std::endl; |
| 1252 | } |
| 1253 | ns_decl.pop(); |
| 1254 | |
| 1255 | // now process the virtual method class parts |
| 1256 | if (ctx.options.classimpl && (kind == EL_OBJECT || kind == EL_INTERFACE)) { |
| 1257 | try { |
| 1258 | process_element_record_class(entry, interfaces, oss_class_decl.str(), |
| 1259 | oss_class_impl.str(), vmethods, out_decl, out_impl); |
| 1260 | } catch (const skip &ex) { |
| 1261 | logger(Log::WARNING, "skipping class generation for {}; {}", name, |
| 1262 | ex.what()); |
| 1263 | } |
| 1264 | } |
| 1265 | |
| 1266 | return std::make_tuple(fname_decl, fname_impl); |
| 1267 | } |
| 1268 | |
| 1269 | std::string handle_exception( |
| 1270 | const pt::ptree::value_type &n, const std::runtime_error &ex) const |
| 1271 | { |
| 1272 | auto &el = n.first; |
| 1273 | const auto &name = get_name(n.second, std::nothrow); |
| 1274 | auto ex_skip = dynamic_cast<const skip *>(&ex); |
| 1275 | if (!ex_skip || ex_skip->cause == skip::INVALID) { |
| 1276 | auto msg = fmt::format("{} {} {}; {}", |
| 1277 | (ex_skip ? "skipping" : "EXCEPTION processing"), el, name, ex.what()); |
| 1278 | Log level = ex_skip ? Log::WARNING : Log::ERROR; |
| 1279 | if (check_suppression(ns, el, name)) |
| 1280 | level = Log::DEBUG; |
| 1281 | logger(level, msg); |
| 1282 | } else { |
| 1283 | logger(Log::DEBUG, "discarding {} {}; {}", el, name, ex.what()); |
| 1284 | } |
| 1285 | return name; |
| 1286 | } |
| 1287 | |
| 1288 | typedef std::function<void(const pt::ptree::value_type &)> entry_processor; |
| 1289 | void process_entries(const pt::ptree &node, const entry_processor &proc) |
| 1290 | { |
| 1291 | for (auto it = node.begin(); it != node.end(); ++it) { |
| 1292 | auto &&n = *it; |
| 1293 | if (n.first == PT_ATTR) |
| 1294 | continue; |
| 1295 | try { |
| 1296 | proc(n); |
| 1297 | } catch (std::runtime_error &ex) { |
| 1298 | auto name = handle_exception(n, ex); |
| 1299 | // in any case give up on this name |
| 1300 | ctx.repo.discard(name); |
| 1301 | } |
| 1302 | } |
| 1303 | } |
| 1304 | |
| 1305 | bool visit_ok(const pt::ptree::value_type &n) const |
| 1306 | { |
| 1307 | const auto &girname = get_name(n.second, std::nothrow); |
| 1308 | auto ninfo = ctx.repo.lookup(girname); |
| 1309 | if (ninfo && !(ninfo->info && (ninfo->info->flags & TYPE_PREDEFINED))) { |
| 1310 | logger(Log::LOG, "visiting {} {}", n.first, girname); |
| 1311 | return true; |
| 1312 | } else { |
| 1313 | return false; |
| 1314 | } |
| 1315 | } |
| 1316 | |
| 1317 | const char *process_libs() |
| 1318 | { |
| 1319 | // also find needed shared libraries in case of dlopen |
| 1320 | auto sl = get_attribute(tree_, AT_SHARED_LIBRARY, ""); |
| 1321 | std::vector<std::string> shlibs, qshlibs; |
| 1322 | boost::split(shlibs, sl, boost::is_any_of(",")); |
| 1323 | for (auto &l : shlibs) |
| 1324 | if (l.size()) |
| 1325 | qshlibs.push_back(fmt::format("\"{}\"", l)); |
| 1326 | |
| 1327 | auto h_libs = "_libs.hpp"; |
| 1328 | File libs(ns, h_libs); |
| 1329 | |
| 1330 | auto libs_templ = R"|( |
| 1331 | namespace internal {{ |
| 1332 | |
| 1333 | GI_INLINE_DECL std::vector<const char*> _libs() |
| 1334 | {{ return {{{0}}}; }} |
| 1335 | |
| 1336 | }} // internal |
| 1337 | )|"; |
| 1338 | libs << fmt::format(libs_templ, boost::join(qshlibs, ",")); |
| 1339 | |
| 1340 | return h_libs; |
| 1341 | } |
| 1342 | |
| 1343 | public: |
| 1344 | std::string process_tree(const std::vector<std::string> &dep_headers) |
| 1345 | { |
| 1346 | logger(Log::INFO, "processing namespace {} {}", ns, version_); |
| 1347 | // set state for ns processing |
| 1348 | ctx.repo.set_ns(ns); |
| 1349 | File::set_root(ctx.options.rootdir); |
| 1350 | File::set_changed(ctx.options.only_changed); |
| 1351 | |
| 1352 | // check if deprecated should pass for this ns |
| 1353 | allow_deprecated_ = ctx.match_ignore.matches("deprecated", ns, {version_}); |
| 1354 | |
| 1355 | // optionally process libs |
| 1356 | auto h_libs = ctx.options.dl ? process_libs() : ""; |
| 1357 | |
| 1358 | auto h_types = "_types.hpp"; |
| 1359 | File types(ns, h_types); |
| 1360 | // index run |
| 1361 | // collect info by name |
| 1362 | // also gather alias/type info |
| 1363 | entry_processor proc_index = [&](const pt::ptree::value_type &n) { |
| 1364 | const auto &name = get_name(n.second, std::nothrow); |
| 1365 | auto deprecated = get_attribute<int>(n.second, AT_DEPRECATED, 0); |
| 1366 | if (deprecated && !allow_deprecated_) |
| 1367 | return; |
| 1368 | auto &el = n.first; |
| 1369 | // redirect to oblivion |
| 1370 | // empty name might originate from a glib:boxed with glib:name attribute |
| 1371 | // discard those as well, as that is a glib type without C-type |
| 1372 | // (which are not really useful in our situation) |
| 1373 | if (name.empty() || ctx.match_ignore.matches(ns, el, {name})) { |
| 1374 | logger(Log::INFO, "ignoring {} {}", el, name); |
| 1375 | } else { |
| 1376 | ctx.repo.add(name, n); |
| 1377 | if (n.first == EL_ALIAS && visit_ok(n)) { |
| 1378 | process_element_alias(n.second, types); |
| 1379 | } |
| 1380 | } |
| 1381 | }; |
| 1382 | process_entries(tree_, proc_index); |
| 1383 | |
| 1384 | // process basic types and callbacks |
| 1385 | auto h_enums = "_enums.hpp"; |
| 1386 | auto h_flags = "_flags.hpp"; |
| 1387 | auto h_constants = "_constants.hpp"; |
| 1388 | auto h_callbacks = "_callbacks.hpp"; |
| 1389 | auto h_callbacks_impl = "_callbacks_impl.hpp"; |
| 1390 | auto h_functions = "_functions.hpp"; |
| 1391 | auto h_functions_impl = "_functions_impl.hpp"; |
| 1392 | auto h_call_args = "_callargs.hpp"; |
| 1393 | |
| 1394 | File enums(ns, h_enums, false); |
| 1395 | File flags(ns, h_flags, false); |
| 1396 | File constants(ns, h_constants); |
| 1397 | File callbacks(ns, h_callbacks); |
| 1398 | File callbacks_impl(ns, h_callbacks_impl); |
| 1399 | File functions(ns, h_functions); |
| 1400 | File functions_impl(ns, h_functions_impl); |
| 1401 | |
| 1402 | // setup CallArgs handling |
| 1403 | // the definition of a CallArgs requires complete types for fields |
| 1404 | // so it will have to be among the last header file, collected as we go |
| 1405 | // actual file handled below |
| 1406 | std::ostringstream call_args; |
| 1407 | |
| 1408 | entry_processor proc_pass_1 = [&](const pt::ptree::value_type &n) { |
| 1409 | auto &el = n.first; |
| 1410 | if (el == EL_ENUM && visit_ok(n)) { |
| 1411 | process_element_enum(n, enums, nullptr); |
| 1412 | } else if (el == EL_FLAGS && visit_ok(n)) { |
| 1413 | process_element_enum(n, flags, nullptr); |
| 1414 | } else if (el == EL_CONST && visit_ok(n)) { |
| 1415 | process_element_const(n.second, constants); |
| 1416 | } |
| 1417 | }; |
| 1418 | process_entries(tree_, proc_pass_1); |
| 1419 | |
| 1420 | // check class types we can handle and will provide a definition for |
| 1421 | entry_processor proc_pass_class = [&](const pt::ptree::value_type &n) { |
| 1422 | auto &el = n.first; |
| 1423 | if ((el == EL_RECORD || el == EL_OBJECT || el == EL_INTERFACE) && |
| 1424 | visit_ok(n)) { |
| 1425 | process_element_record(n, true, &call_args); |
| 1426 | } |
| 1427 | }; |
| 1428 | process_entries(tree_, proc_pass_class); |
| 1429 | |
| 1430 | // so the known classes will be declared/defined |
| 1431 | // check what callback typedefs that allows for |
| 1432 | // check class types we can handle and will provide a definition for |
| 1433 | DepsSet cb_deps; |
| 1434 | std::ostringstream cb_decl; |
| 1435 | entry_processor proc_pass_callbacks = [&](const pt::ptree::value_type &n) { |
| 1436 | auto &el = n.first; |
| 1437 | if (el == EL_CALLBACK && visit_ok(n)) { |
| 1438 | std::ostringstream null; |
| 1439 | process_element_function(n, cb_decl, callbacks_impl, "", "", cb_deps); |
| 1440 | } |
| 1441 | }; |
| 1442 | process_entries(tree_, proc_pass_callbacks); |
| 1443 | // write callbacks |
| 1444 | // need to declare deps first |
| 1445 | callbacks << make_dep_declare(cb_deps); |
| 1446 | callbacks << std::endl; |
| 1447 | callbacks << cb_decl.str(); |
| 1448 | |
| 1449 | // now we know all supported types and supported callbacks |
| 1450 | // pass over class types again and fill in |
| 1451 | std::set<std::pair<std::string, std::string>> includes; |
| 1452 | DepsSet functions_deps; |
| 1453 | std::ostringstream functions_decl; |
| 1454 | entry_processor proc_pass_2 = [&](const pt::ptree::value_type &n) { |
| 1455 | auto &el = n.first; |
| 1456 | if ((el == EL_RECORD || el == EL_OBJECT || el == EL_INTERFACE) && |
| 1457 | visit_ok(n)) { |
| 1458 | auto &&res = process_element_record(n, false, &call_args); |
| 1459 | includes.insert({std::get<0>(res), std::get<1>(res)}); |
| 1460 | } else if (el == EL_FUNCTION && visit_ok(n)) { |
| 1461 | // all types known now, so include CallArgs directly |
| 1462 | process_element_function(n, functions_decl, functions_impl, "", "", |
| 1463 | functions_deps, &call_args); |
| 1464 | } else if ((el == EL_ENUM || el == EL_FLAGS) && visit_ok(n)) { |
| 1465 | process_element_enum(n, functions, &functions_impl); |
| 1466 | } |
| 1467 | }; |
| 1468 | process_entries(tree_, proc_pass_2); |
| 1469 | |
| 1470 | // write functions |
| 1471 | // need to declare CallArgs deps first |
| 1472 | // only those, as the others are already all declared by this stage |
| 1473 | if (auto fd = make_dep_declare(functions_deps, true); fd.size()) |
| 1474 | functions << fd << std::endl; |
| 1475 | functions << functions_decl.str(); |
| 1476 | |
| 1477 | // generate callargs |
| 1478 | if (call_args.tellp()) { |
| 1479 | File ca(ns, h_call_args); |
| 1480 | NamespaceGuard nsg_decl(ca); |
| 1481 | nsg_decl.push(GI_NS_ARGS, false); |
| 1482 | ca << call_args.str(); |
| 1483 | } else { |
| 1484 | h_call_args = nullptr; |
| 1485 | } |
| 1486 | |
| 1487 | auto add_stub_include = [this](const std::string &suffix) { |
| 1488 | auto fpath = (fs::path(tolower(ns)) / (tolower(ns) + suffix)).string(); |
| 1489 | return make_conditional_include(fpath, false); |
| 1490 | }; |
| 1491 | |
| 1492 | auto add_stub_define = [this](bool impl, bool begin) { |
| 1493 | auto nsu = toupper(ns); |
| 1494 | auto infix = impl ? "IMPL_" : ""; |
| 1495 | auto suffix = begin ? "BEGIN" : "END"; |
| 1496 | auto macro = fmt::format("GI_INCLUDE_{}{}_{}", infix, nsu, suffix); |
| 1497 | // avoid deprecated warning floods |
| 1498 | auto guard = begin ? GI_DISABLE_DEPRECATED_WARN_BEGIN |
| 1499 | : GI_DISABLE_DEPRECATED_WARN_END; |
| 1500 | // also allow for custom/override tweak |
| 1501 | return fmt::format("{}\n\n#ifdef {}\n{}\n#endif", guard, macro, macro); |
| 1502 | }; |
| 1503 | |
| 1504 | auto h_ns = tolower(ns) + ".hpp"; |
| 1505 | auto h_ns_inc = tolower(ns) + "_inc.hpp"; |
| 1506 | auto h_ns_impl = tolower(ns) + "_impl.hpp"; |
| 1507 | |
| 1508 | // generate overall includes |
| 1509 | File nsh_inc(ns, h_ns_inc, false); |
| 1510 | // enable dl load coding |
| 1511 | if (h_libs && *h_libs) { |
| 1512 | nsh_inc << "#ifndef GI_DL" << std::endl; |
| 1513 | nsh_inc << "#define GI_DL 1" << std::endl; |
| 1514 | nsh_inc << "#endif" << std::endl; |
| 1515 | } |
| 1516 | if (ctx.options.expected) { |
| 1517 | nsh_inc << "#ifndef GI_EXPECTED" << std::endl; |
| 1518 | nsh_inc << "#define GI_EXPECTED 1" << std::endl; |
| 1519 | nsh_inc << "#endif" << std::endl; |
| 1520 | } |
| 1521 | if (ctx.options.const_method) { |
| 1522 | nsh_inc << "#ifndef GI_CONST_METHOD" << std::endl; |
| 1523 | nsh_inc << "#define GI_CONST_METHOD 1" << std::endl; |
| 1524 | nsh_inc << "#endif" << std::endl; |
| 1525 | } |
| 1526 | if (ctx.options.classimpl) { |
| 1527 | nsh_inc << "#ifndef GI_CLASS_IMPL" << std::endl; |
| 1528 | nsh_inc << "#define GI_CLASS_IMPL 1" << std::endl; |
| 1529 | nsh_inc << "#endif" << std::endl; |
| 1530 | } |
| 1531 | if (ctx.options.call_args >= 0) { |
| 1532 | nsh_inc << "#ifndef GI_CALL_ARGS" << std::endl; |
| 1533 | nsh_inc << "#define GI_CALL_ARGS " << ctx.options.call_args << std::endl; |
| 1534 | nsh_inc << "#endif" << std::endl; |
| 1535 | } |
| 1536 | if (ctx.options.basic_collection) { |
| 1537 | nsh_inc << "#ifndef GI_BASIC_COLLECTION" << std::endl; |
| 1538 | nsh_inc << "#define GI_BASIC_COLLECTION 1" << std::endl; |
| 1539 | nsh_inc << "#endif" << std::endl; |
| 1540 | } |
| 1541 | nsh_inc << std::endl; |
| 1542 | nsh_inc << make_include("gi/gi_inc.hpp", false) << std::endl; |
| 1543 | nsh_inc << std::endl; |
| 1544 | // preserve some behaviour for standard non-module code |
| 1545 | nsh_inc << "#ifndef GI_MODULE_IN_INTERFACE" << std::endl; |
| 1546 | nsh_inc << make_include("gi/gi.hpp", false) << std::endl; |
| 1547 | nsh_inc << "#endif" << std::endl; |
| 1548 | nsh_inc << std::endl; |
| 1549 | // include gi deps; inc version |
| 1550 | for (auto &&d : dep_headers) { |
| 1551 | auto hd = boost::algorithm::replace_last_copy(d, ".hpp", "_inc.hpp"); |
| 1552 | nsh_inc << make_include(hd, false) << std::endl; |
| 1553 | } |
| 1554 | nsh_inc << std::endl; |
| 1555 | // package includes |
| 1556 | // in some cases, these are totally missing |
| 1557 | // so the headers will have to be supplied by extra header override |
| 1558 | // repo supplied |
| 1559 | nsh_inc << add_stub_include("_setup_pre_def.hpp") << std::endl; |
| 1560 | // user supplied |
| 1561 | nsh_inc << add_stub_include("_setup_pre.hpp") << std::endl; |
| 1562 | // include the above before the includes |
| 1563 | // (so as to allow tweaking some defines in the overrides) |
| 1564 | auto node = root_.get_child(EL_REPOSITORY); |
| 1565 | auto p = node.equal_range(EL_CINCLUDE); |
| 1566 | for (auto &q = p.first; q != p.second; ++q) { |
| 1567 | // in some buggy cases, additional headers may have a full path |
| 1568 | // let's only keep the last part |
| 1569 | auto name = get_name(q->second); |
| 1570 | if (name.size() && name[0] == '/') { |
| 1571 | auto pos = name.rfind('/'); |
| 1572 | pos = (pos != name.npos && pos) ? name.rfind('/', pos - 1) : pos; |
| 1573 | if (pos != name.npos) |
| 1574 | name = name.substr(pos + 1); |
| 1575 | } |
| 1576 | nsh_inc << make_include(name, false) << std::endl; |
| 1577 | } |
| 1578 | // we may also need to tweak or fix things after usual includes |
| 1579 | // repo supplied |
| 1580 | nsh_inc << add_stub_include("_setup_post_def.hpp") << std::endl; |
| 1581 | // user supplied |
| 1582 | nsh_inc << add_stub_include("_setup_post.hpp") << std::endl; |
| 1583 | nsh_inc << std::endl; |
| 1584 | |
| 1585 | File nsh(ns, h_ns, false); |
| 1586 | // include above part |
| 1587 | nsh << make_include(h_ns_inc, true) << std::endl; |
| 1588 | nsh << std::endl; |
| 1589 | // gi deps; full version |
| 1590 | nsh << "#ifndef GI_MODULE_NO_REC_INC" << std::endl; |
| 1591 | for (auto &&d : dep_headers) |
| 1592 | nsh << make_include(d, false) << std::endl; |
| 1593 | nsh << "#endif" << std::endl; |
| 1594 | // allow to tweak things (e.g. template specialization) |
| 1595 | // prior to generated code declaration/definition |
| 1596 | // repo supplied |
| 1597 | nsh << add_stub_include("_extra_pre_def.hpp") << std::endl; |
| 1598 | // user supplied |
| 1599 | nsh << add_stub_include("_extra_pre.hpp") << std::endl; |
| 1600 | nsh << std::endl; |
| 1601 | |
| 1602 | // guard begin |
| 1603 | nsh << add_stub_define(false, true) << std::endl; |
| 1604 | nsh << std::endl; |
| 1605 | |
| 1606 | // various basic declaration parts |
| 1607 | for (auto &h : {h_types, h_enums, h_flags, h_constants, h_callbacks}) |
| 1608 | nsh << make_include(h, true) << std::endl; |
| 1609 | nsh << std::endl; |
| 1610 | |
| 1611 | // declarations |
| 1612 | for (auto &h : includes) |
| 1613 | nsh << make_include(h.first, true) << std::endl; |
| 1614 | nsh << std::endl; |
| 1615 | // global functions when we have seen all else |
| 1616 | nsh << make_include(h_functions, true) << std::endl; |
| 1617 | // all collected CallArgs |
| 1618 | if (h_call_args) |
| 1619 | nsh << make_include(h_call_args, true) << std::endl; |
| 1620 | nsh << std::endl; |
| 1621 | // allow for override/supplements |
| 1622 | // repo supplied |
| 1623 | nsh << add_stub_include("_extra_def.hpp") << std::endl; |
| 1624 | // user supplied |
| 1625 | nsh << add_stub_include("_extra.hpp") << std::endl; |
| 1626 | // guard end |
| 1627 | nsh << add_stub_define(false, false) << std::endl; |
| 1628 | nsh << std::endl; |
| 1629 | // include implementation header in inline case |
| 1630 | nsh << "#if defined(GI_INLINE) || defined(GI_INCLUDE_IMPL)" << std::endl; |
| 1631 | nsh << make_include(h_ns_impl, true) << std::endl; |
| 1632 | nsh << "#endif" << std::endl; |
| 1633 | nsh << std::endl; |
| 1634 | |
| 1635 | File nsh_impl(ns, h_ns_impl, false); |
| 1636 | // include declaration |
| 1637 | nsh_impl << make_include(h_ns, true) << std::endl; |
| 1638 | nsh_impl << std::endl; |
| 1639 | // guard begin |
| 1640 | nsh_impl << add_stub_define(true, true) << std::endl; |
| 1641 | nsh_impl << std::endl; |
| 1642 | // lib helper for symbol load |
| 1643 | nsh_impl << make_include(h_libs, true) << std::endl; |
| 1644 | nsh_impl << std::endl; |
| 1645 | // implementations |
| 1646 | for (auto &h : includes) |
| 1647 | nsh_impl << make_include(h.second, true) << std::endl; |
| 1648 | nsh_impl << std::endl; |
| 1649 | nsh_impl << make_include(h_callbacks_impl, true) << std::endl; |
| 1650 | nsh_impl << make_include(h_functions_impl, true) << std::endl; |
| 1651 | nsh_impl << std::endl; |
| 1652 | // likewise for override/supplement implementation |
| 1653 | // repo supplied |
| 1654 | nsh_impl << add_stub_include("_extra_def_impl.hpp") << std::endl; |
| 1655 | // user supplied |
| 1656 | nsh_impl << add_stub_include("_extra_impl.hpp") << std::endl; |
| 1657 | nsh_impl << std::endl; |
| 1658 | // guard end |
| 1659 | nsh_impl << add_stub_define(false, true) << std::endl; |
| 1660 | nsh_impl << std::endl; |
| 1661 | |
| 1662 | // a convenience cpp for non-inline |
| 1663 | auto cpp_ns = tolower(ns) + ".cpp"; |
| 1664 | File cpp(ns, cpp_ns, false, false); |
| 1665 | cpp << make_include(h_ns_impl, true) << std::endl; |
| 1666 | |
| 1667 | // NOTE it is not possible to provide (core) gi as a module, |
| 1668 | // as it also provides a set of macros, and contains some forward |
| 1669 | // declarations (ParamFlags, SignalFlags) which are only fully defined |
| 1670 | // by generated code |
| 1671 | // however, symbol/definitions are attached to module, so a later one |
| 1672 | // can not define another one forward declared by another module |
| 1673 | // in essence; modules break forward declarations |
| 1674 | // so, 2 variants are created; |
| 1675 | // + a separate one per namespace, but the "lowest" one is gobject, |
| 1676 | // which then also includes glib and gi |
| 1677 | // + a recursive one, a module that combines all dependency ns, |
| 1678 | // so a large object and precompiled result |
| 1679 | auto nsl = tolower(ns); |
| 1680 | std::string glib_ns = "glib"; |
| 1681 | std::string gobject_ns = "gobject"; |
| 1682 | if (nsl != glib_ns) { |
| 1683 | // lowest gobject is actually recursive |
| 1684 | std::string modprefix{"gi.repo."}; |
| 1685 | bool is_gobject = nsl == gobject_ns; |
| 1686 | if (!is_gobject) { |
| 1687 | // module form; separate |
| 1688 | auto cppm_ns = cpp_ns + "m"; |
| 1689 | File cppm(ns, cppm_ns, false, false); |
| 1690 | auto module_templ = R"|( |
| 1691 | module; |
| 1692 | #define GI_INLINE 1 |
| 1693 | #define GI_MODULE_IN_INTERFACE 1 |
| 1694 | {0} |
| 1695 | export module {1}; |
| 1696 | {2} |
| 1697 | GI_MODULE_BEGIN |
| 1698 | #define GI_MODULE_NO_REC_INC 1 |
| 1699 | export {{ |
| 1700 | {3} |
| 1701 | }} |
| 1702 | GI_MODULE_END |
| 1703 | )|"; |
| 1704 | auto modname = modprefix + nsl; |
| 1705 | std::ostringstream moddeps; |
| 1706 | bool got_gobject = false; |
| 1707 | for (auto &&d : dep_headers) { |
| 1708 | std::string path = fs::path(d).filename().string(); |
| 1709 | boost::algorithm::erase_all(path, ".hpp"); |
| 1710 | // sigh, glib/gobject collapsed along with core |
| 1711 | // replace references in non-duplicate way |
| 1712 | if (path == glib_ns) |
| 1713 | path = gobject_ns; |
| 1714 | if (path == gobject_ns) { |
| 1715 | if (got_gobject) { |
| 1716 | continue; |
| 1717 | } else { |
| 1718 | got_gobject = true; |
| 1719 | } |
| 1720 | } |
| 1721 | moddeps << "import " << modprefix << path << ';' << std::endl; |
| 1722 | } |
| 1723 | cppm << fmt::format(module_templ, make_include(h_ns_inc, true), modname, |
| 1724 | moddeps.str(), make_include(h_ns, true)); |
| 1725 | } |
| 1726 | |
| 1727 | { // module form; recursive |
| 1728 | auto cppm_ns = (is_gobject ? cpp_ns : nsl + "_rec.cpp") + "m"; |
| 1729 | File cppm(ns, cppm_ns, false, false); |
| 1730 | auto module_templ = R"|( |
| 1731 | module; |
| 1732 | #define GI_INLINE 1 |
| 1733 | #define GI_MODULE_IN_INTERFACE 1 |
| 1734 | {0} |
| 1735 | export module {1}; |
| 1736 | GI_MODULE_BEGIN |
| 1737 | #include "gi/gi.hpp" |
| 1738 | export {{ |
| 1739 | {2} |
| 1740 | }} |
| 1741 | GI_MODULE_END |
| 1742 | )|"; |
| 1743 | auto modname = modprefix + nsl; |
| 1744 | if (!is_gobject) |
| 1745 | modname += ".rec"; |
| 1746 | cppm << fmt::format(module_templ, make_include(h_ns_inc, true), modname, |
| 1747 | make_include(h_ns, true)); |
| 1748 | } |
| 1749 | } |
| 1750 | |
| 1751 | // optional build tool convenience |
| 1752 | // generate refering files in rootdir |
| 1753 | if (ctx.options.output_top) { |
| 1754 | File top_cpp(ns, cpp_ns, false, false, false); |
| 1755 | top_cpp << make_include(nsh_impl.get_rel_path(), true) << std::endl; |
| 1756 | |
| 1757 | File top_hpp(ns, h_ns, false, false, false); |
| 1758 | top_hpp << make_include(nsh.get_rel_path(), true) << std::endl; |
| 1759 | } |
| 1760 | |
| 1761 | return nsh.get_rel_path(); |
| 1762 | } |
| 1763 | }; |
| 1764 | |
| 1765 | } // namespace |
| 1766 | |
| 1767 | std::shared_ptr<NamespaceGenerator> |
| 1768 | NamespaceGenerator::new_(GeneratorContext &ctx, const std::string &filename) |
| 1769 | { |
| 1770 | return std::make_shared<NamespaceGeneratorImpl>(ctx, filename); |
| 1771 | } |