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; }
99 std::optional<cxx::Expression> _init = {},
Linkage _linkage = {})
100 :
id(std::move(_id)),
101 type(std::move(_type)),
102 args(std::move(_args)),
103 init(std::move(_init)),
104 linkage(std::move(_linkage)) {}
108 std::vector<cxx::Expression> args;
109 std::optional<cxx::Expression> init;
114 bool isInternal()
const {
return util::startsWith(
id.local(),
"__"); }
116 std::string str()
const;
117 operator std::string()
const {
return str(); }
124 std::vector<cxx::Expression> args;
125 std::optional<cxx::Expression> init;
128 bool operator==(
const Global& other)
const {
129 return id == other.id && type == other.type && init == other.init && linkage == other.linkage;
132 std::string str()
const;
133 operator std::string()
const {
return str(); }
140 std::optional<cxx::Expression> init;
142 bool forward_decl =
false;
144 bool operator<(
const Constant& s)
const {
return id < s.id; }
145 bool operator==(
const Constant& other)
const {
146 return id == other.id && type == other.type && init == other.init && linkage == other.linkage;
150 extern void to_json(nlohmann::json& j,
const Constant& c);
151 extern void from_json(
const nlohmann::json& j,
Constant& c);
157 std::string inline_code;
158 bool forward_decl =
false;
159 bool forward_decl_prio =
false;
160 bool no_using =
false;
162 Type(
cxx::ID _id = {},
cxx::Type _type = {}, std::string _inline_code = {},
bool _forward_decl =
false,
163 bool _forward_decl_prio =
false,
bool _no_using =
false)
164 :
id(std::move(_id)),
165 type(std::move(_type)),
166 inline_code(std::move(_inline_code)),
167 forward_decl(_forward_decl),
168 forward_decl_prio(_forward_decl_prio),
169 no_using(_no_using) {}
171 bool operator==(
const Type& other)
const {
172 return id == other.id && type == other.type && inline_code == other.inline_code &&
173 forward_decl == other.forward_decl && forward_decl_prio == other.forward_decl_prio &&
174 no_using == other.no_using;
178 extern void to_json(nlohmann::json& j,
const Type& t);
179 extern void from_json(
const nlohmann::json& j,
Type& t);
185 std::optional<cxx::Expression> default_;
187 operator std::string()
const {
return id ? util::fmt(
"%s %s", type,
id) : std::string(type); }
189 bool operator==(
const Argument& other)
const {
return type == other.type &&
id == other.id; }
192 extern void to_json(nlohmann::json& j,
const Argument& a);
193 extern void from_json(
const nlohmann::json& j,
Argument& a);
201 Block(std::vector<std::string> stmts);
203 void addStatement(std::string stmt);
204 void addStatementAtFront(std::string stmt);
205 void addBlock(
Block child);
206 void addComment(
const std::string& stmt,
bool sep_before =
true,
bool sep_after =
false);
215 void addElse(
Block true_);
221 void addLambda(
const std::string& name,
const std::string& signature,
Block body);
222 void addSwitch(
const Expression& cond,
const std::vector<std::pair<Expression, Block>>& cases_,
223 std::optional<Block> default_ = {});
224 void appendFromBlock(
Block b);
225 void addTry(
Block body, std::vector<std::pair<declaration::Argument, Block>> catches);
227 bool ensureBracesForBlock()
const {
return _ensure_braces_for_block; }
228 void setEnsureBracesforBlock() { _ensure_braces_for_block =
true; }
230 size_t size(
bool ignore_comments =
false)
const;
234 operator bool()
const {
return ! (_stmts.empty() && _tmps.empty()); }
236 friend ::hilti::detail::cxx::Formatter& operator<<(
Formatter& f,
const Block& x);
238 bool operator==(
const Block& other)
const {
return _stmts == other._stmts; }
241 using Flags =
unsigned int;
242 std::vector<std::tuple<std::string, Block, Flags>> _stmts;
243 std::vector<std::string> _tmps;
244 bool _ensure_braces_for_block =
false;
247 namespace declaration {
253 std::vector<Argument> args;
257 std::optional<Block> inline_body;
259 std::string prototype(
bool qualify)
const;
260 std::string parameters()
const;
262 bool operator==(
const Function& other)
const {
263 return result == other.result &&
id == other.id && args == other.args && linkage == other.linkage &&
264 attribute == other.attribute && inline_body == other.inline_body;
268 extern void to_json(nlohmann::json& j,
const Function& f);
269 extern void from_json(
const nlohmann::json& j,
Function& f);
277 bool default_ =
false;
279 bool operator==(
const Function& other)
const {
return declaration == other.declaration && body == other.body; }
285 using Member = std::variant<declaration::Local, declaration::Function>;
287 inline bool operator<(
const Member& m1,
const Member& m2) {
288 auto id = [](
auto m) {
289 if (
auto x = std::get_if<declaration::Local>(&m) )
291 if (
auto x = std::get_if<declaration::Function>(&m) )
294 throw std::bad_variant_access();
297 return id(m1) < id(m2);
304 std::vector<declaration::Argument> args;
305 std::vector<struct_::Member> members;
307 std::optional<cxx::Type>
self;
308 bool add_ctors =
false;
309 std::string str()
const;
310 std::string inlineCode()
const;
312 operator std::string()
const {
return str(); }
313 operator cxx::Type()
const {
return str(); }
317 using Member = struct_::Member;
322 std::vector<union_::Member> members;
324 std::string str()
const;
325 operator std::string()
const {
return str(); }
326 operator cxx::Type()
const {
return str(); }
330 using Label = std::pair<cxx::ID, int>;
335 std::vector<enum_::Label> labels;
337 std::string str()
const;
338 operator std::string()
const {
return str(); }
339 operator cxx::Type()
const {
return str(); }
344 inline std::ostream& operator<<(std::ostream& o,
const ID& i) {
return o << std::string(i); }
345 inline std::ostream& operator<<(std::ostream& o,
const Linkage& l) {
return o << std::string(l); }
346 inline std::ostream& operator<<(std::ostream& o,
const Type& t) {
return o << std::string(t); }
347 inline std::ostream& operator<<(std::ostream& o,
const Attribute& a) {
return o << std::string(a); }
348 inline std::ostream& operator<<(std::ostream& o,
const declaration::Argument& t) {
return o << std::string(t); }
349 inline std::ostream& operator<<(std::ostream& o,
const Expression& e) {
return o << std::string(e); }
Definition: elements.h:250
Definition: elements.h:198
Definition: elements.h:28
Definition: elements.h:93
Definition: elements.h:121
Definition: elements.h:87
Definition: elements.h:334
Definition: elements.h:182
Definition: elements.h:18
Definition: elements.h:303
Definition: elements.h:137
Definition: elements.h:66
Definition: elements.h:274
Definition: elements.h:154
static ID fromNormalized(std::string id)
Definition: elements.h:78
Definition: elements.h:321