| 1 | #include "repository.hpp" |
| 2 | |
| 3 | #include "genutils.hpp" |
| 4 | |
| 5 | #include <regex> |
| 6 | #include <set> |
| 7 | #include <unordered_map> |
| 8 | |
| 9 | static std::set<std::string> basic_types{"gchar", "guchar", "gshort", "gushort", |
| 10 | "gint", "guint", "glong", "gulong", "gssize", "gsize", "gintptr", |
| 11 | "guintptr", "gpointer", "gconstpointer", "gboolean", "gint8", "gint16", |
| 12 | "guint8", "guint16", "gint32", "guint32", "gint64", "guint64", "gfloat", |
| 13 | "gdouble", "GType", "utf8", "filename", "gi::cstring", "gunichar", |
| 14 | "dev_t", "gid_t", "pid_t", "socklen_t", "uid_t"}; |
| 15 | |
| 16 | class RepositoryPriv : public Repository |
| 17 | { |
| 18 | public: |
| 19 | // holds info collected from GIRs indexed by entry's (qualified) name |
| 20 | // (which should be unique within a namespace) |
| 21 | std::unordered_map<key_type, mapped_type> index; |
| 22 | // active ns |
| 23 | std::string ns; |
| 24 | // ODR check |
| 25 | mutable std::unordered_map<std::string, std::string> type_index; |
| 26 | // substitutes for missing c:type |
| 27 | subst_c_types c_types; |
| 28 | |
| 29 | RepositoryPriv(subst_c_types m) : c_types(std::move(m)) |
| 30 | { |
| 31 | // make basic types known |
| 32 | for (auto &&girname : basic_types) { |
| 33 | auto cpptype = girname; |
| 34 | auto ctype = girname; |
| 35 | int flags = 0; |
| 36 | |
| 37 | flags |= TYPE_BASIC; |
| 38 | if (girname == "utf8" || girname == "filename" || |
| 39 | girname == "gi::cstring") { |
| 40 | flags |= TYPE_CLASS; |
| 41 | ctype = "char*"; |
| 42 | cpptype = "gi::cstring"; |
| 43 | } else { |
| 44 | flags |= TYPE_VALUE; |
| 45 | if (girname == "gboolean") |
| 46 | cpptype = "bool"; |
| 47 | } |
| 48 | auto argtype = |
| 49 | (girname.find("pointer") != girname.npos) ? "void*" : ctype; |
| 50 | index.emplace( |
| 51 | girname, mapped_type{nullptr, std::make_unique<TypeInfo>(girname, |
| 52 | cpptype, ctype, argtype, flags)}); |
| 53 | } |
| 54 | // void case |
| 55 | index.emplace(GIR_VOID, |
| 56 | mapped_type{nullptr, std::make_unique<TypeInfo>(GIR_VOID, CPP_VOID, |
| 57 | EMPTY, CPP_VOID, TYPE_BASIC)}); |
| 58 | |
| 59 | // other special and pre-defined cases |
| 60 | std::vector<std::tuple<std::string, std::string, std::string, int>> pre{ |
| 61 | {std::make_tuple("GLib.List", "GList", "GList", TYPE_LIST)}, |
| 62 | {std::make_tuple("GLib.SList", "GSList", "GSList", TYPE_LIST)}, |
| 63 | {std::make_tuple( |
| 64 | "GLib.HashTable", "GHashTable", "GHashTable", TYPE_MAP)}, |
| 65 | {std::make_tuple("GObject.Value", "GObject::Value", "GValue", |
| 66 | TYPE_CLASS | TYPE_BOXED)}, |
| 67 | {std::make_tuple( |
| 68 | "GLib.Error", "GLib::Error", "GError", TYPE_CLASS | TYPE_BOXED)}, |
| 69 | // avoid namespace mishap |
| 70 | {std::make_tuple("GObject.Object", "GObject::Object", "GObject", |
| 71 | TYPE_CLASS | TYPE_OBJECT)}, |
| 72 | // pretend is like object |
| 73 | {std::make_tuple("GObject.ParamSpec", "GObject::ParamSpec", |
| 74 | "GParamSpec", TYPE_CLASS)}}; |
| 75 | for (auto &&e : pre) { |
| 76 | // qualify argtype to avoid name conflicts |
| 77 | auto ti = std::make_unique<TypeInfo>(std::get<0>(e), std::get<1>(e), |
| 78 | std::get<2>(e), GI_SCOPE + std::get<2>(e) + "*", |
| 79 | std::get<3>(e) | TYPE_PREDEFINED); |
| 80 | auto girname = ti->girname; |
| 81 | index.emplace(girname, mapped_type{nullptr, std::move(ti)}); |
| 82 | } |
| 83 | auto ti = |
| 84 | std::make_unique<TypeInfo>("GLib.DestroyNotify", "GLib::DestroyNotify", |
| 85 | GDESTROYNOTIFY, GDESTROYNOTIFY, TYPE_CALLBACK | TYPE_PREDEFINED); |
| 86 | auto girname = ti->girname; |
| 87 | index.emplace(girname, mapped_type{nullptr, std::move(ti)}); |
| 88 | } |
| 89 | }; |
| 90 | |
| 91 | // C++ on the outside, C trick on the inside |
| 92 | static const RepositoryPriv & |
| 93 | get_self(const Repository *t) |
| 94 | { |
| 95 | return *static_cast<const RepositoryPriv *>(t); |
| 96 | } |
| 97 | static RepositoryPriv & |
| 98 | get_self(Repository *t) |
| 99 | { |
| 100 | return *static_cast<RepositoryPriv *>(t); |
| 101 | } |
| 102 | |
| 103 | // set namespace used for unqualified girname |
| 104 | void |
| 105 | Repository::set_ns(const std::string _ns) |
| 106 | { |
| 107 | auto &self = get_self(this); |
| 108 | self.ns = _ns; |
| 109 | } |
| 110 | |
| 111 | // qualify girname, optionally wrt relative base |
| 112 | std::string |
| 113 | Repository::qualify(const std::string &girname, const std::string &base) const |
| 114 | { |
| 115 | auto &&self = get_self(this); |
| 116 | if (girname.find('.') == girname.npos) { |
| 117 | auto bns = self.ns; |
| 118 | if (base.size()) { |
| 119 | auto pos = base.find('.'); |
| 120 | if (pos == base.npos) |
| 121 | return girname; |
| 122 | bns = base.substr(0, pos); |
| 123 | } |
| 124 | return bns + '.' + girname; |
| 125 | } |
| 126 | return girname; |
| 127 | } |
| 128 | |
| 129 | void |
| 130 | Repository::add(const key_type &girname, const mapped_type::tree_type &n) |
| 131 | { |
| 132 | auto &&self = get_self(this); |
| 133 | auto qualified = qualify(girname); |
| 134 | auto it = self.index.find(qualified); |
| 135 | bool registered = false; |
| 136 | |
| 137 | if (girname.empty()) { |
| 138 | // should not make it here |
| 139 | assert(false); |
| 140 | } else if (it != self.index.end()) { |
| 141 | auto &e = it->second; |
| 142 | // merge in tree data for predefined |
| 143 | if (e.info && (e.info->flags & TYPE_PREDEFINED)) { |
| 144 | e.tree = std::make_unique<mapped_type::tree_type>(n); |
| 145 | } else { |
| 146 | throw std::runtime_error( |
| 147 | fmt::format("duplicate name {} [{}]", girname, qualified)); |
| 148 | } |
| 149 | } else { |
| 150 | // examine node/type |
| 151 | // NOTE not every entry/node represents a type (and therefore has != |
| 152 | // flags) consider e.g. a constant or function |
| 153 | int flags = 0; |
| 154 | auto &el = n.first; |
| 155 | auto &node = n.second; |
| 156 | auto ctype = get_attribute(node, AT_CTYPE, ""); |
| 157 | |
| 158 | // base identification |
| 159 | if (el == EL_RECORD) { |
| 160 | flags |= TYPE_CLASS | TYPE_BOXED; |
| 161 | } else if (el == EL_OBJECT || el == EL_INTERFACE) { |
| 162 | flags |= TYPE_CLASS | TYPE_OBJECT; |
| 163 | } else if (el == EL_ENUM || el == EL_FLAGS) { |
| 164 | flags |= TYPE_ENUM | TYPE_VALUE; |
| 165 | } else if (el == EL_ALIAS) { |
| 166 | // adopt flags of typedef'ed one |
| 167 | // need not be a primitive one (e.g. GtkAllocation = GdkRectangle) |
| 168 | auto btype = node.get(EL_TYPE + '.' + PT_ATTR + '.' + AT_NAME, ""); |
| 169 | auto ti = lookup(btype); |
| 170 | flags = ti && ti->info ? ti->info->flags : 0; |
| 171 | // also consider as sort-of predefined (at least if we know about |
| 172 | // it) |
| 173 | if (flags) |
| 174 | flags |= TYPE_TYPEDEF; |
| 175 | } else if (el == EL_CALLBACK) { |
| 176 | flags |= TYPE_CALLBACK; |
| 177 | } |
| 178 | if (flags) { |
| 179 | // check if registered |
| 180 | auto gettype = get_attribute(n.second, AT_GLIB_GET_TYPE, ""); |
| 181 | if (gettype.size()) |
| 182 | registered = true; |
| 183 | } |
| 184 | |
| 185 | bool keep_node = false; |
| 186 | bool keep_info = flags != 0; |
| 187 | if (flags & TYPE_CLASS) { |
| 188 | keep_node = true; |
| 189 | // additional checks |
| 190 | auto class_struct = get_attribute(node, AT_GLIB_IS_TYPE_STRUCT_FOR, ""); |
| 191 | if (class_struct.size()) { |
| 192 | // is not a valid type, but keep some info around for later |
| 193 | // lookup |
| 194 | flags = 0; |
| 195 | keep_info = true; |
| 196 | } else if (flags & TYPE_BOXED) { |
| 197 | keep_node = false; |
| 198 | } |
| 199 | // e.g. GParamSpec (several), variant |
| 200 | auto fundamental = get_attribute<int>(node, AT_GLIB_FUNDAMENTAL, 0); |
| 201 | // GVariant is marked this way |
| 202 | auto gtype = get_attribute(node, AT_GLIB_GET_TYPE, ""); |
| 203 | if (fundamental || gtype == "intern") { |
| 204 | flags = -1; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | { |
| 209 | // filter some more |
| 210 | auto disguised = get_attribute<int>(node, AT_DISGUISED, 0); |
| 211 | // also never mind if it is designed opaque/disguised |
| 212 | // lots of private stuff, but also possibly type structs |
| 213 | // (some at least, in case of opaque struct) |
| 214 | if (disguised) |
| 215 | flags = -1; |
| 216 | // cairo is notable foreign example |
| 217 | // structs are typically opaque, with custom _create, _copy, |
| 218 | // _destroy |
| 219 | // TODO for now filter all, perhaps not so later |
| 220 | // (and then only filter by ignore and allow custom declaration of |
| 221 | // above functions in the boxed system) |
| 222 | auto foreign = get_attribute<int>(node, AT_FOREIGN, 0); |
| 223 | if (foreign && !registered) |
| 224 | flags = -1; |
| 225 | } |
| 226 | |
| 227 | // special fundamental case |
| 228 | // partly prepared, mostly generated |
| 229 | if (qualified == GIR_GVARIANT) { |
| 230 | keep_node = false; |
| 231 | keep_info = true; |
| 232 | flags = TYPE_CLASS; |
| 233 | } |
| 234 | |
| 235 | // always check |
| 236 | auto movedto = get_attribute(node, AT_MOVED_TO, ""); |
| 237 | if (!movedto.empty()) |
| 238 | flags = -1; |
| 239 | |
| 240 | if (flags == -1) { |
| 241 | logger(Log::DEBUG, "ignoring GIR {} {}", el, qualified); |
| 242 | } else { |
| 243 | logger(Log::LOG, "registering GIR {} {} {}", el, qualified, flags); |
| 244 | |
| 245 | // convert GIR qualification to namespace qualification |
| 246 | static const std::regex re_qualify("\\.", std::regex::optimize); |
| 247 | // enum cpptype is unreserve'd in type definition |
| 248 | auto cpptype = std::regex_replace( |
| 249 | (flags & TYPE_ENUM) ? qualify(unreserve(girname)) : qualified, |
| 250 | re_qualify, "::"); |
| 251 | assert(!(flags & TYPE_CLASS) || is_qualified(cpptype)); |
| 252 | |
| 253 | // in rare cases c:type is missing for a record/class |
| 254 | // (e.g. GtkSnapshot = alias of Gdk type) |
| 255 | // use an override substitute type |
| 256 | if ((flags & TYPE_CLASS) && ctype.empty()) { |
| 257 | auto sit = self.c_types.find(qualified); |
| 258 | if (sit != self.c_types.end()) { |
| 259 | ctype = sit->second; |
| 260 | logger(Log::INFO, "{} using substitute c:type {}", qualified, ctype); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | // always top-level qualify ctype to avoid ns ambiguity |
| 265 | if (ctype.size()) |
| 266 | ctype = GI_SCOPE + ctype; |
| 267 | auto argtype = ctype; |
| 268 | if ((flags & TYPE_CLASS) && argtype.size()) |
| 269 | argtype += GI_PTR; |
| 270 | |
| 271 | std::unique_ptr<mapped_type::tree_type> xmlinfo; |
| 272 | // node only needed for class type |
| 273 | // (only a small part of node is needed later on, |
| 274 | // but let's simply keep all of it) |
| 275 | if (keep_node) |
| 276 | xmlinfo = std::make_unique<mapped_type::tree_type>(n); |
| 277 | |
| 278 | mapped_type entry = { |
| 279 | std::move(xmlinfo), keep_info ? std::make_unique<TypeInfo>(qualified, |
| 280 | cpptype, ctype, argtype, flags) |
| 281 | : nullptr}; |
| 282 | it = std::get<0>( |
| 283 | self.index.emplace(std::move(qualified), std::move(entry))); |
| 284 | } |
| 285 | } |
| 286 | } |
| 287 | |
| 288 | void |
| 289 | Repository::discard(const key_type &girname) |
| 290 | { |
| 291 | auto &&self = get_self(this); |
| 292 | auto qualified = qualify(girname); |
| 293 | logger(Log::LOG, "discarding girname {}", qualified); |
| 294 | if (!self.index.erase(qualified)) |
| 295 | logger(Log::WARNING, "discarded unknown girname {}", qualified); |
| 296 | } |
| 297 | |
| 298 | const Repository::mapped_type::tree_type & |
| 299 | Repository::tree(const std::string &girname) const |
| 300 | { |
| 301 | auto &&self = get_self(this); |
| 302 | auto &index = self.index; |
| 303 | // only used by class types |
| 304 | auto it = index.find(qualify(girname)); |
| 305 | if (it == index.end() || !it->second.tree) |
| 306 | throw std::runtime_error("no node info for " + girname); |
| 307 | return *it->second.tree; |
| 308 | } |
| 309 | |
| 310 | const Repository::mapped_type * |
| 311 | Repository::lookup(const std::string &girname) const |
| 312 | { |
| 313 | auto &&self = get_self(this); |
| 314 | auto &index = self.index; |
| 315 | // also consider non-qualified for basic types |
| 316 | auto it = index.find(girname); |
| 317 | if (it == index.end()) |
| 318 | it = index.find(qualify(girname)); |
| 319 | if (it != index.end()) |
| 320 | return &it->second; |
| 321 | return nullptr; |
| 322 | } |
| 323 | |
| 324 | std::string |
| 325 | Repository::check_odr(const std::string &cpptype, const std::string &ctype) |
| 326 | { |
| 327 | if (!ctype.empty()) { |
| 328 | auto &&self = get_self(this); |
| 329 | auto ret = self.type_index.insert({ctype, cpptype}); |
| 330 | if (!ret.second && ret.first->second != cpptype) { |
| 331 | return ret.first->second; |
| 332 | } |
| 333 | } |
| 334 | return {}; |
| 335 | } |
| 336 | |
| 337 | std::shared_ptr<Repository> |
| 338 | Repository::new_(subst_c_types m) |
| 339 | { |
| 340 | return std::make_shared<RepositoryPriv>(std::move(m)); |
| 341 | } |