11 #include <hilti/ast/builder/declaration.h> 12 #include <hilti/ast/builder/expression.h> 13 #include <hilti/ast/ctor.h> 14 #include <hilti/ast/statements/all.h> 24 Builder(std::weak_ptr<hilti::Context> context)
25 : _context(std::move(context)), _our_block(
statement::Block()), _block(*_our_block) {}
32 auto context()
const {
return _context.lock(); }
34 Expression addTmp(
const std::string& prefix,
const Expression& init);
35 Expression addTmp(
const std::string& prefix,
const Type& t,
const std::vector<Expression>& args = {});
36 Expression addTmp(
const std::string& prefix,
const Type& t,
const Expression& init);
39 _block.
_add(builder::local(std::move(
id), std::move(t), std::move(m)));
42 void addLocal(
ID id, Expression init,
Meta m =
Meta()) {
43 _block.
_add(builder::local(std::move(
id), std::move(init), std::move(m)));
47 _block.
_add(builder::local(std::move(
id), std::move(t), std::move(init), std::move(m)));
50 void addLocal(
ID id,
Type t, std::vector<hilti::Expression> args,
Meta m =
Meta()) {
51 _block.
_add(builder::local(std::move(
id), std::move(t), std::move(args), std::move(m)));
56 void addAssert(Expression cond, std::string msg,
Meta m =
Meta()) {
60 void addAssign(Expression dst, Expression src,
const Meta& m =
Meta()) {
64 void addSumAssign(Expression dst, Expression src,
const Meta& m =
Meta()) {
68 void addAssign(
ID dst, Expression src,
const Meta& m =
Meta()) {
76 void addSumAssign(
ID dst, Expression src,
const Meta& m =
Meta()) {
80 void addCall(
ID id,
const std::vector<Expression>& v,
const Meta& m =
Meta()) {
84 void addMemberCall(Expression
self,
const ID&
id,
const std::vector<Expression>& v,
const Meta& m =
Meta()) {
88 void addComment(std::string comment,
89 hilti::statement::comment::Separator separator = hilti::statement::comment::Separator::Before,
91 comment = util::replace(comment,
"\n",
"");
97 void addReturn(Ctor c,
const Meta& m =
Meta()) {
106 void addDebugMsg(
const std::string& stream,
const std::string& fmt, std::vector<Expression> args = {});
107 void addDebugIndent(
const std::string& stream);
108 void addDebugDedent(
const std::string& stream);
110 void addPrint(
const std::vector<Expression>& exprs) { addCall(
"hilti::print", exprs); }
111 void addPrint(
const Expression& expr) { addCall(
"hilti::print", {expr}); }
115 return newBuilder(lastStatement<statement::While>()._bodyNode());
118 auto addWhile(Expression cond,
Meta m =
Meta()) {
120 return newBuilder(lastStatement<statement::While>()._bodyNode());
126 return std::make_pair(newBuilder(lastStatement<statement::While>()._bodyNode()),
127 newBuilder(lastStatement<statement::While>()._elseNode()));
130 auto addWhileElse(Expression cond,
Meta m =
Meta()) {
132 return std::make_pair(newBuilder(lastStatement<statement::While>()._bodyNode()),
133 newBuilder(lastStatement<statement::While>()._elseNode()));
138 return newBuilder(lastStatement<statement::If>()._trueNode());
143 return newBuilder(lastStatement<statement::If>()._trueNode());
146 auto addIf(Expression cond,
Meta m =
Meta()) {
148 return newBuilder(lastStatement<statement::If>()._trueNode());
154 return std::make_pair(newBuilder(lastStatement<statement::If>()._trueNode()),
155 newBuilder(lastStatement<statement::If>()._falseNode()));
160 return std::make_pair(newBuilder(lastStatement<statement::If>()._trueNode()),
161 newBuilder(lastStatement<statement::If>()._falseNode()));
164 auto addIfElse(Expression cond,
Meta m =
Meta()) {
166 return std::make_pair(newBuilder(lastStatement<statement::If>()._trueNode()),
167 newBuilder(lastStatement<statement::If>()._falseNode()));
179 auto addCase(Expression expr,
Meta m =
Meta()) {
return _addCase({std::move(expr)}, std::move(m)); }
181 auto addCase(std::vector<Expression> exprs,
Meta m =
Meta()) {
182 return _addCase(std::move(exprs), std::move(m));
185 auto addDefault(
Meta m =
Meta()) {
return _addCase({}, std::move(m)); }
188 std::shared_ptr<Builder> _addCase(std::vector<Expression> exprs,
Meta m =
Meta()) {
197 auto addSwitch(Expression cond,
const Meta& m =
Meta()) {
199 return SwitchProxy(
this, lastStatement<statement::Switch>());
204 return SwitchProxy(
this, lastStatement<statement::Switch>());
207 void setLocation(
const Location& l);
237 return std::make_pair(newBuilder(lastStatement<statement::Try>()._bodyNode()),
238 TryProxy(
this, lastStatement<statement::Try>()));
241 bool empty()
const {
return _block.statements().empty() && _tmps.empty(); }
246 Builder(std::weak_ptr<hilti::Context> context, Statement& s)
251 return _block._lastStatementNode().as<T>();
254 std::shared_ptr<Builder> newBuilder(
Node& n) {
255 return std::shared_ptr<Builder>(
new Builder(_context, n.template as<Statement>()));
258 std::weak_ptr<hilti::Context> _context;
259 std::optional<statement::Block> _our_block;
262 std::map<std::string, int> _tmps;
Definition: declaration.h:13
Definition: expression.h:13
Definition: builder.h:209
Definition: builder.h:175
void _add(Statement s)
Definition: block.h:26
auto & _lastStatementNode()
Definition: block.h:29
Definition: parameter.h:46
Definition: location.h:18
void _addCase(switch_::Case case_)
Definition: switch.h:123
Definition: continue.h:13
auto & _lastCaseNode()
Definition: switch.h:120