Debian Codegraph @

tools/genbase.cpp

raw symbols SCIP
1 #include "genbase.hpp"
2
3 #include <map>
4
5 static std::string
6 qualify(const std::string &ns, const std::string &cpptype, int flags)
7 {
8 if (cpptype.find(':') == cpptype.npos && !cpptype.empty() &&
9 !(flags & TYPE_BASIC))
10 return ns + "::" + cpptype;
11 return cpptype;
12 }
13
14 GeneratorBase::GeneratorBase(GeneratorContext &_ctx, const std::string _ns)
15 : ctx(_ctx), ns(_ns)
16 {}
17
18 std::string
19 GeneratorBase::qualify(const std::string &cpptype, int flags) const
20 {
21 return ::qualify(ns, cpptype, flags);
22 }
23
24 void
25 GeneratorBase::parse_typeinfo(
26 const std::string &girname, TypeInfo &result) const
27 {
28 auto ti = ctx.repo.lookup(girname);
29 if (ti && ti->info)
30 result = *ti->info;
31 }
32
33 GeneratorBase::ArgInfo
34 GeneratorBase::parse_arginfo(const pt::ptree &node, ArgInfo *routput) const
35 {
36 ArgInfo lresult;
37 ArgInfo &result = routput ? *routput : lresult;
38
39 const pt::ptree *pntype{};
40 std::string kind;
41 // try most likely in turn
42 for (auto &&v : {EL_TYPE, EL_ARRAY, EL_VARARGS}) {
43 const auto &ntypeo = node.get_child_optional(v);
44 if (ntypeo.is_initialized()) {
45 kind = v;
46 pntype = &ntypeo.get();
47 }
48 }
49 if (kind.empty() || !pntype)
50 throw skip("type info not found");
51 if (kind == EL_VARARGS)
52 throw skip("varargs not supported", skip::OK);
53 auto &ntype = *pntype;
54 bool discard_element = false;
55 // no name for array
56 if (kind == EL_ARRAY) {
57 result.flags = TYPE_ARRAY;
58 result.length = get_attribute<int>(ntype, AT_LENGTH, -10);
59 result.zeroterminated = get_attribute<int>(ntype, AT_ZERO_TERMINATED, 1);
60 result.fixedsize = get_attribute<int>(ntype, AT_FIXED_SIZE, 0);
61 if (result.length < 0 && !result.zeroterminated && !result.fixedsize)
62 throw skip("inconsistent array info");
63 auto name = get_name(ntype, std::nothrow);
64 // could be GBytes, etc
65 if (name.size()) {
66 // transform to plain argument if not ignored
67 // FIXME maybe custom wrappers might be more convenient ??
68 if (ctx.match_ignore.matches(ns, EL_RECORD, {name}))
69 throw skip(name + " array not supported", skip::OK);
70 // fresh state and discard element info processing
71 result = ArgInfo{};
72 discard_element = true;
73 parse_typeinfo(name, result);
74 }
75 } else {
76 parse_typeinfo(get_name(ntype), result);
77 }
78 // should be present (but not so for signal)
79 // though const info seems to be missing in array case
80 result.ctype = get_attribute(ntype, AT_CTYPE, "");
81
82 // special case where const is preserved for the cpp type
83 if (result.girname == "gpointer" && is_const(result.ctype))
84 parse_typeinfo("gconstpointer", result);
85
86 // handle element type info
87 int i = 0;
88 for (const auto &n : ntype) {
89 if (n.first == "type" && !discard_element) {
90 ArgTypeInfo &ti = i == 0 ? result.first : result.second;
91 auto elname = get_name(n.second);
92 parse_typeinfo(elname, ti);
93 if (!ti.flags)
94 throw skip("container element not supported", skip::OK);
95 // probably not, but give it a shot
96 ti.ctype = get_attribute(n.second, AT_CTYPE, "");
97 // avoid specialized bool vector
98 if (ti.cpptype == "bool")
99 ti.cpptype = "gboolean";
100 // some char arrays (e.g. g_regex* parameters) specify utf8 element type
101 // along with ctype char rather than char*
102 // so discard utf8 in that case and go for a plain array
103 if (kind == EL_ARRAY && elname == "utf8" && ti.ctype == "gchar")
104 parse_typeinfo(ti.ctype, ti);
105 ++i;
106 }
107 }
108
109 // sanity checks
110 if ((result.flags & (TYPE_ARRAY | TYPE_LIST))) {
111 if (i != 1)
112 throw skip("inconsistent list element info");
113 if (result.flags & TYPE_ARRAY) {
114 // NOTE apparently both are possible, see e.g. g_shell_parse_arg*
115 // in that case it is handled zeroterminated
116 // and the size param becomes a regular one
117 if (result.zeroterminated) {
118 result.length = -1;
119 result.fixedsize = 0;
120 }
121 // 1 pointer level more than the basic type
122 result.pdepth = ((result.first.flags & TYPE_CLASS) ? 1 : 0) + 1;
123 }
124 } else if (result.flags & TYPE_MAP) {
125 if (i != 2)
126 throw skip("inconsistent map element info");
127 } else {
128 if (i != 0)
129 throw skip("inconsistent type info");
130 }
131
132 return result;
133 }
134
135 std::string
136 GeneratorBase::make_ctype(
137 const ArgInfo &info, const std::string &direction, bool callerallocates)
138 {
139 std::string result;
140 bool out = !(direction == DIR_IN || direction == DIR_RETURN);
141 if (info.flags & TYPE_ARRAY) {
142 auto ret = info.first.argtype + GI_PTR;
143 if (out && !callerallocates)
144 ret += GI_PTR;
145 result = ret;
146 } else if (info.flags & TYPE_CALLBACK) {
147 result = info.cpptype + "::cfunction_type";
148 } else {
149 if (!out || callerallocates) {
150 result = info.argtype;
151 } else {
152 result = info.argtype + GI_PTR;
153 }
154 }
155 return (is_const(info.ctype) ? std::string("const ") : EMPTY) + result;
156 }
157
158 void
159 GeneratorBase::track_dependency(DepsSet &deps, const ArgInfo &info) const
160 {
161 auto track = [&](const std::string &cpptype, int flags) {
162 // other items (e.g. enums) are included before anyway
163 if (flags & TYPE_CLASS && !(flags & (TYPE_BASIC | TYPE_TYPEDEF))) {
164 // always track scoped
165 assert(is_qualified(cpptype));
166 deps.insert({"", cpptype});
167 if (flags & TYPE_BOXED)
168 deps.insert({"", cpptype + GI_SUFFIX_REF});
169 }
170 };
171 if (!(info.flags & TYPE_CONTAINER)) {
172 track(info.cpptype, info.flags);
173 } else {
174 track(info.first.cpptype, info.first.flags);
175 if (info.flags & TYPE_MAP)
176 track(info.second.cpptype, info.second.flags);
177 }
178 }
179
180 bool
181 GeneratorBase::check_suppression(const std::string &ns, const std::string &kind,
182 const std::string &name) const
183 {
184 // always generate
185 ctx.suppressions.insert(ctx.match_sup.format(ns, kind, name));
186 return ctx.match_sup.matches(ns, kind, {name});
187 }
188
189 std::string
190 GeneratorBase::make_wrap_format(const ArgInfo &info,
191 const std::string &transfer, const std::string &outtype)
192 {
193 std::string fmts;
194 auto format = "{}";
195 if (info.flags & TYPE_CLASS) {
196 fmts = fmt::format(GI_NS_SCOPED + "wrap ({}, {})", format,
197 get_transfer_parameter(transfer));
198 } else if (info.flags & TYPE_ENUM) {
199 fmts = GI_NS_SCOPED + "wrap ({})";
200 } else if (info.flags & (TYPE_LIST | TYPE_ARRAY | TYPE_MAP)) {
201 assert(!outtype.empty());
202 fmts = fmt::format(GI_NS_SCOPED + "wrap_to<{}>({}, {})", outtype, format,
203 get_transfer_parameter(transfer));
204 } else {
205 fmts = format;
206 }
207 return fmts;
208 }
209
210 std::string
211 GeneratorBase::get_transfer_parameter(const std::string &transfer, bool _type)
212 {
213 std::map<std::string, std::string> m{
214 {TRANSFER_NOTHING, "transfer_none"},
215 {TRANSFER_FULL, "transfer_full"},
216 {TRANSFER_CONTAINER, "transfer_container"},
217 };
218 auto it = m.find(transfer);
219 if (it == m.end())
220 throw std::runtime_error("invalid transfer " + transfer);
221 return GI_NS_SCOPED + it->second + (_type ? "_t" : "");
222 }