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 extern std::string normalize_id(std::string
id);
71 explicit ID(const ::hilti::ID&
id) :
Base(std::string(
id)) {}
72 ID& operator=(const ::hilti::ID&
id) {
78 static ID fromNormalized(std::string
id) {
return ID(std::move(
id), Base::AlreadyNormalized()); }
81 extern void to_json(nlohmann::json& j,
const cxx::ID&
id);
82 extern void from_json(
const nlohmann::json& j,
cxx::ID&
id);
84 namespace declaration {
89 bool operator<(
const IncludeFile& o)
const {
return file < o.file; }
96 std::vector<cxx::Expression> args;
97 std::optional<cxx::Expression> init;
104 std::string str()
const;
105 operator std::string()
const {
return str(); }
112 std::vector<cxx::Expression> args;
113 std::optional<cxx::Expression> init;
116 bool operator==(
const Global& other)
const {
117 return id == other.id && type == other.type && init == other.init && linkage == other.linkage;
120 std::string str()
const;
121 operator std::string()
const {
return str(); }
128 std::optional<cxx::Expression> init;
130 bool forward_decl =
false;
132 bool operator<(
const Constant& s)
const {
return id < s.id; }
133 bool operator==(
const Constant& other)
const {
134 return id == other.id && type == other.type && init == other.init && linkage == other.linkage;
138 extern void to_json(nlohmann::json& j,
const Constant& c);
139 extern void from_json(
const nlohmann::json& j,
Constant& c);
145 std::string inline_code =
"";
146 bool forward_decl =
false;
147 bool forward_decl_prio =
false;
148 bool no_using =
false;
150 bool operator==(
const Type& other)
const {
151 return id == other.id && type == other.type && inline_code == other.inline_code &&
152 forward_decl == other.forward_decl && forward_decl_prio == other.forward_decl_prio &&
153 no_using == other.no_using;
157 extern void to_json(nlohmann::json& j,
const Type& t);
158 extern void from_json(
const nlohmann::json& j,
Type& t);
164 std::optional<cxx::Expression> default_;
166 operator std::string()
const {
return id ?
util::fmt(
"%s %s", type,
id) : std::string(type); }
168 bool operator==(
const Argument& other)
const {
return type == other.type &&
id == other.id; }
171 extern void to_json(nlohmann::json& j,
const Argument& a);
172 extern void from_json(
const nlohmann::json& j,
Argument& a);
180 Block(std::vector<std::string> stmts);
182 void addStatement(std::string stmt);
183 void addStatementAtFront(std::string stmt);
184 void addBlock(
Block child);
185 void addComment(
const std::string& stmt,
bool sep_before =
true,
bool sep_after =
false);
194 void addElse(
Block true_);
200 void addLambda(
const std::string& name,
const std::string& signature,
Block body);
201 void addSwitch(
const Expression& cond,
const std::vector<std::pair<Expression, Block>>& cases_,
202 std::optional<Block> default_ = {});
203 void appendFromBlock(
Block b);
204 void addTry(
Block body, std::vector<std::pair<declaration::Argument, Block>> catches);
206 bool ensureBracesForBlock()
const {
return _ensure_braces_for_block; }
207 void setEnsureBracesforBlock() { _ensure_braces_for_block =
true; }
209 size_t size(
bool ignore_comments =
false)
const;
213 operator bool()
const {
return ! (_stmts.empty() && _tmps.empty()); }
215 friend ::hilti::detail::cxx::Formatter& operator<<(
Formatter& f,
const Block& x);
217 bool operator==(
const Block& other)
const {
return _stmts == other._stmts; }
220 using Flags =
unsigned int;
221 std::vector<std::tuple<std::string, Block, Flags>> _stmts;
222 std::vector<std::string> _tmps;
223 bool _ensure_braces_for_block =
false;
226 namespace declaration {
232 std::vector<Argument> args;
236 std::optional<Block> inline_body;
238 std::string prototype(
bool qualify)
const;
239 std::string parameters()
const;
241 bool operator==(
const Function& other)
const {
242 return result == other.result &&
id == other.id && args == other.args && linkage == other.linkage &&
243 attribute == other.attribute && inline_body == other.inline_body;
247 extern void to_json(nlohmann::json& j,
const Function& f);
248 extern void from_json(
const nlohmann::json& j,
Function& f);
256 bool default_ =
false;
258 bool operator==(
const Function& other)
const {
return declaration == other.declaration && body == other.body; }
264 using Member = std::variant<declaration::Local, declaration::Function>;
266 inline bool operator<(
const Member& m1,
const Member& m2) {
267 auto id = [](
auto m) {
268 if (
auto x = std::get_if<declaration::Local>(&m) )
270 if (
auto x = std::get_if<declaration::Function>(&m) )
273 throw std::bad_variant_access();
276 return id(m1) < id(m2);
283 std::vector<declaration::Argument> args;
284 std::vector<struct_::Member> members;
286 std::optional<cxx::Type>
self;
287 bool add_ctors =
false;
288 std::string str()
const;
289 std::string inlineCode()
const;
291 operator std::string()
const {
return str(); }
292 operator cxx::Type()
const {
return str(); }
296 using Member = struct_::Member;
301 std::vector<union_::Member> members;
303 std::string str()
const;
304 operator std::string()
const {
return str(); }
305 operator cxx::Type()
const {
return str(); }
309 using Label = std::pair<cxx::ID, int>;
314 std::vector<enum_::Label> labels;
316 std::string str()
const;
317 operator std::string()
const {
return str(); }
318 operator cxx::Type()
const {
return str(); }
323 inline std::ostream& operator<<(std::ostream& o,
const ID& i) {
return o << std::string(i); }
324 inline std::ostream& operator<<(std::ostream& o,
const Linkage& l) {
return o << std::string(l); }
325 inline std::ostream& operator<<(std::ostream& o,
const Type& t) {
return o << std::string(t); }
326 inline std::ostream& operator<<(std::ostream& o,
const Attribute& a) {
return o << std::string(a); }
327 inline std::ostream& operator<<(std::ostream& o,
const declaration::Argument& t) {
return o << std::string(t); }
328 inline std::ostream& operator<<(std::ostream& o,
const Expression& e) {
return o << std::string(e); }
Definition: elements.h:229
Definition: elements.h:177
Definition: elements.h:28
Definition: elements.h:93
Definition: elements.h:109
Definition: elements.h:87
Definition: elements.h:313
Definition: elements.h:161
Definition: elements.h:18
std::string fmt(const char *fmt, const Args &... args)
Definition: util.h:80
bool startsWith(const std::string &s, const std::string &prefix)
Definition: util.h:285
Definition: elements.h:282
Definition: elements.h:125
Definition: elements.h:66
Definition: elements.h:253
Definition: elements.h:142
static ID fromNormalized(std::string id)
Definition: elements.h:78
Definition: elements.h:300