12 #include <hilti/rt/json-fwd.h> 14 #include <hilti/ast/id.h> 15 #include <hilti/base/id-base.h> 16 #include <hilti/base/util.h> 23 enum class Type { Expression, Type, Linkage, Attribute };
27 template<element::Type T>
31 Element(std::string s) : _s(std::move(s)) {}
32 Element(
const char* s) : _s(s) {}
34 Element& operator=(
const std::string& s) {
38 Element& operator=(std::string&& s) {
42 Element& operator=(
const char* s) {
47 bool isMultiLine()
const {
return _s.find(
'\n') != std::string::npos; }
49 operator std::string()
const {
return _s; }
50 explicit operator bool()
const {
return ! _s.empty(); }
51 bool operator<(
const Element& s)
const {
return _s < s._s; }
52 bool operator==(
const Element& s)
const {
return _s == s._s; }
63 enum class Side { LHS, RHS };
72 Expression(std::string s, Side side = Side::RHS) : _s(std::move(s)), _side(side) {}
73 Expression(
const char* s, Side side = Side::RHS) : _s(s), _side(side) {}
75 bool isLhs()
const {
return _side == Side::LHS; }
77 operator std::string()
const {
return _s; }
78 explicit operator bool()
const {
return ! _s.empty(); }
79 bool operator<(
const Expression& s)
const {
return _s < s._s; }
80 bool operator==(
const Expression& s)
const {
return _s == s._s; }
81 bool operator!=(
const Expression& s)
const {
return _s != s._s; }
85 Side _side = Side::LHS;
88 extern std::string normalize_id(std::string
id);
96 explicit ID(const ::hilti::ID&
id) :
Base(std::string(
id)) {}
97 ID& operator=(const ::hilti::ID&
id) {
106 extern void to_json(nlohmann::json& j,
const cxx::ID&
id);
107 extern void from_json(
const nlohmann::json& j,
cxx::ID&
id);
109 namespace declaration {
114 bool operator<(
const IncludeFile& o)
const {
return file < o.file; }
124 std::optional<cxx::Expression> _init = {},
Linkage _linkage = {})
125 :
id(std::move(_id)),
126 type(std::move(_type)),
127 args(std::move(_args)),
128 init(std::move(_init)),
129 linkage(std::move(_linkage)) {}
133 std::vector<cxx::Expression> args;
134 std::optional<cxx::Expression> init;
139 bool isInternal()
const {
return util::startsWith(
id.local(),
"__"); }
141 std::string str()
const;
142 operator std::string()
const {
return str(); }
149 std::vector<cxx::Expression> args;
150 std::optional<cxx::Expression> init;
153 bool operator==(
const Global& other)
const {
154 return id == other.id && type == other.type && init == other.init && linkage == other.linkage;
157 std::string str()
const;
158 operator std::string()
const {
return str(); }
165 std::optional<cxx::Expression> init;
167 bool forward_decl =
false;
169 bool operator<(
const Constant& s)
const {
return id < s.id; }
170 bool operator==(
const Constant& other)
const {
171 return id == other.id && type == other.type && init == other.init && linkage == other.linkage;
175 extern void to_json(nlohmann::json& j,
const Constant& c);
176 extern void from_json(
const nlohmann::json& j,
Constant& c);
182 std::string inline_code;
183 bool forward_decl =
false;
184 bool forward_decl_prio =
false;
185 bool no_using =
false;
187 Type(
cxx::ID _id = {},
cxx::Type _type = {}, std::string _inline_code = {},
bool _forward_decl =
false,
188 bool _forward_decl_prio =
false,
bool _no_using =
false)
189 :
id(std::move(_id)),
190 type(std::move(_type)),
191 inline_code(std::move(_inline_code)),
192 forward_decl(_forward_decl),
193 forward_decl_prio(_forward_decl_prio),
194 no_using(_no_using) {}
196 bool operator==(
const Type& other)
const {
197 return id == other.id && type == other.type && inline_code == other.inline_code &&
198 forward_decl == other.forward_decl && forward_decl_prio == other.forward_decl_prio &&
199 no_using == other.no_using;
203 extern void to_json(nlohmann::json& j,
const Type& t);
204 extern void from_json(
const nlohmann::json& j,
Type& t);
210 std::optional<cxx::Expression> default_;
212 operator std::string()
const {
return id ? util::fmt(
"%s %s", type,
id) : std::string(type); }
214 bool operator==(
const Argument& other)
const {
return type == other.type &&
id == other.id; }
217 extern void to_json(nlohmann::json& j,
const Argument& a);
218 extern void from_json(
const nlohmann::json& j,
Argument& a);
226 Block(std::vector<std::string> stmts);
228 void addStatement(std::string stmt);
229 void addStatementAtFront(std::string stmt);
230 void addBlock(
Block child);
231 void addComment(
const std::string& stmt,
bool sep_before =
true,
bool sep_after =
false);
240 void addElse(
Block true_);
246 void addLambda(
const std::string& name,
const std::string& signature,
Block body);
247 void addSwitch(
const Expression& cond,
const std::vector<std::pair<Expression, Block>>& cases_,
248 std::optional<Block> default_ = {});
249 void appendFromBlock(
Block b);
250 void addTry(
Block body, std::vector<std::pair<declaration::Argument, Block>> catches);
252 bool ensureBracesForBlock()
const {
return _ensure_braces_for_block; }
253 void setEnsureBracesforBlock() { _ensure_braces_for_block =
true; }
255 size_t size(
bool ignore_comments =
false)
const;
259 operator bool()
const {
return ! (_stmts.empty() && _tmps.empty()); }
261 friend ::hilti::detail::cxx::Formatter& operator<<(
Formatter& f,
const Block& x);
263 bool operator==(
const Block& other)
const {
return _stmts == other._stmts; }
266 using Flags =
unsigned int;
267 std::vector<std::tuple<std::string, Block, Flags>> _stmts;
268 std::vector<std::string> _tmps;
269 bool _ensure_braces_for_block =
false;
272 namespace declaration {
278 std::vector<Argument> args;
282 std::optional<Block> inline_body;
284 std::string prototype(
bool qualify)
const;
285 std::string parameters()
const;
287 bool operator==(
const Function& other)
const {
288 return result == other.result &&
id == other.id && args == other.args && linkage == other.linkage &&
289 attribute == other.attribute && inline_body == other.inline_body;
293 extern void to_json(nlohmann::json& j,
const Function& f);
294 extern void from_json(
const nlohmann::json& j,
Function& f);
302 bool default_ =
false;
304 bool operator==(
const Function& other)
const {
return declaration == other.declaration && body == other.body; }
310 using Member = std::variant<declaration::Local, declaration::Function>;
312 inline bool operator<(
const Member& m1,
const Member& m2) {
313 auto id = [](
auto m) {
314 if (
auto x = std::get_if<declaration::Local>(&m) )
316 if (
auto x = std::get_if<declaration::Function>(&m) )
319 throw std::bad_variant_access();
322 return id(m1) < id(m2);
329 std::vector<declaration::Argument> args;
330 std::vector<struct_::Member> members;
332 std::optional<cxx::Type>
self;
333 bool add_ctors =
false;
334 std::string str()
const;
335 std::string inlineCode()
const;
337 operator std::string()
const {
return str(); }
338 operator cxx::Type()
const {
return str(); }
342 using Member = struct_::Member;
347 std::vector<union_::Member> members;
349 std::string str()
const;
350 operator std::string()
const {
return str(); }
351 operator cxx::Type()
const {
return str(); }
355 using Label = std::pair<cxx::ID, int>;
360 std::vector<enum_::Label> labels;
362 std::string str()
const;
363 operator std::string()
const {
return str(); }
364 operator cxx::Type()
const {
return str(); }
369 inline std::ostream& operator<<(std::ostream& o,
const ID& i) {
return o << std::string(i); }
370 inline std::ostream& operator<<(std::ostream& o,
const Linkage& l) {
return o << std::string(l); }
371 inline std::ostream& operator<<(std::ostream& o,
const Type& t) {
return o << std::string(t); }
372 inline std::ostream& operator<<(std::ostream& o,
const Attribute& a) {
return o << std::string(a); }
373 inline std::ostream& operator<<(std::ostream& o,
const declaration::Argument& t) {
return o << std::string(t); }
374 inline std::ostream& operator<<(std::ostream& o,
const Expression& e) {
return o << std::string(e); }
Definition: elements.h:275
Definition: elements.h:223
Definition: elements.h:28
Definition: elements.h:118
Definition: elements.h:146
Definition: elements.h:112
Definition: elements.h:359
Definition: elements.h:207
Definition: elements.h:18
Definition: elements.h:328
Definition: elements.h:162
Definition: elements.h:69
Definition: elements.h:91
Definition: elements.h:299
Definition: elements.h:179
static ID fromNormalized(std::string id)
Definition: elements.h:103
Definition: elements.h:346