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::shared_ptr<hilti::Context> context)
25 : _context(std::move(context)), _our_block(
statement::Block()), _block(*_our_block) {}
32 Expression addTmp(
const std::string& prefix,
const Expression& init);
33 Expression addTmp(
const std::string& prefix,
const Type& t,
const std::vector<Expression>& args = {});
34 Expression addTmp(
const std::string& prefix,
const Type& t,
const Expression& init);
36 void addLocal(
ID id, Type t,
Meta m =
Meta()) {
37 _block.
_add(builder::local(std::move(
id), std::move(t), std::move(m)));
40 void addLocal(
ID id, Expression init,
Meta m =
Meta()) {
41 _block.
_add(builder::local(std::move(
id), std::move(init), std::move(m)));
44 void addLocal(
ID id, Type t, Expression init,
Meta m =
Meta()) {
45 _block.
_add(builder::local(std::move(
id), std::move(t), std::move(init), std::move(m)));
48 void addLocal(
ID id, Type t, std::vector<hilti::Expression> args,
Meta m =
Meta()) {
49 _block.
_add(builder::local(std::move(
id), std::move(t), std::move(args), std::move(m)));
54 void addAssert(Expression cond, std::string msg,
Meta m =
Meta()) {
58 void addAssign(Expression dst, Expression src,
const Meta& m =
Meta()) {
62 void addSumAssign(Expression dst, Expression src,
const Meta& m =
Meta()) {
66 void addAssign(
ID dst, Expression src,
const Meta& m =
Meta()) {
74 void addSumAssign(
ID dst, Expression src,
const Meta& m =
Meta()) {
78 void addCall(
ID id, std::vector<Expression> v,
const Meta& m =
Meta()) {
82 void addMemberCall(Expression
self,
const ID&
id, std::vector<Expression> v,
const Meta& m =
Meta()) {
86 void addComment(std::string comment,
87 hilti::statement::comment::Separator separator = hilti::statement::comment::Separator::Before,
95 void addReturn(Ctor c,
const Meta& m =
Meta()) {
104 void addDebugMsg(
const std::string& stream,
const std::string& fmt, std::vector<Expression> args = {});
105 void addDebugIndent(
const std::string& stream);
106 void addDebugDedent(
const std::string& stream);
108 void addPrint(
const std::vector<Expression>& exprs) { addCall(
"hilti::print", exprs); }
109 void addPrint(
const Expression& expr) { addCall(
"hilti::print", {expr}); }
113 return newBuilder(lastStatement<statement::While>()._bodyNode());
116 auto addWhile(Expression cond,
Meta m =
Meta()) {
118 return newBuilder(lastStatement<statement::While>()._bodyNode());
124 return std::make_pair(newBuilder(lastStatement<statement::While>()._bodyNode()),
125 newBuilder(lastStatement<statement::While>()._elseNode()));
128 auto addWhileElse(Expression cond,
Meta m =
Meta()) {
130 return std::make_pair(newBuilder(lastStatement<statement::While>()._bodyNode()),
131 newBuilder(lastStatement<statement::While>()._elseNode()));
136 return newBuilder(lastStatement<statement::If>()._trueNode());
141 return newBuilder(lastStatement<statement::If>()._trueNode());
144 auto addIf(Expression cond,
Meta m =
Meta()) {
146 return newBuilder(lastStatement<statement::If>()._trueNode());
152 return std::make_pair(newBuilder(lastStatement<statement::If>()._trueNode()),
153 newBuilder(lastStatement<statement::If>()._falseNode()));
158 return std::make_pair(newBuilder(lastStatement<statement::If>()._trueNode()),
159 newBuilder(lastStatement<statement::If>()._falseNode()));
162 auto addIfElse(Expression cond,
Meta m =
Meta()) {
164 return std::make_pair(newBuilder(lastStatement<statement::If>()._trueNode()),
165 newBuilder(lastStatement<statement::If>()._falseNode()));
177 auto addCase(Expression expr,
Meta m =
Meta()) {
return _addCase({std::move(expr)}, std::move(m)); }
179 auto addCase(std::vector<Expression> exprs,
Meta m =
Meta()) {
180 return _addCase(std::move(exprs), std::move(m));
183 auto addDefault(
Meta m =
Meta()) {
return _addCase({}, std::move(m)); }
186 std::shared_ptr<Builder> _addCase(std::vector<Expression> exprs,
Meta m =
Meta()) {
195 auto addSwitch(Expression cond,
Meta m =
Meta()) {
197 return SwitchProxy(
this, lastStatement<statement::Switch>());
201 _block._add(
statement::Switch(init.declaration(), std::move(cond), {}, std::move(m)));
202 return SwitchProxy(
this, lastStatement<statement::Switch>());
233 return std::make_pair(newBuilder(lastStatement<statement::Try>()._bodyNode()),
234 TryProxy(
this, lastStatement<statement::Try>()));
237 bool empty()
const {
return _block.statements().empty() && _tmps.empty(); }
242 Builder(std::shared_ptr<hilti::Context> context, Statement& s)
247 return _block._lastStatementNode().as<T>();
250 std::shared_ptr<Builder> newBuilder(
Node& n) {
251 return std::shared_ptr<Builder>(
new Builder(_context, n.template as<Statement>()));
254 std::shared_ptr<hilti::Context> _context;
255 std::optional<statement::Block> _our_block;
258 std::map<std::string, int> _tmps;
std::string replace(const std::string &s, const std::string &o, const std::string &n)
Definition: util.cc:69
Definition: declaration.h:14
Definition: expression.h:14
Definition: builder.h:205
Definition: builder.h:173
void _add(Statement s)
Definition: block.h:27
auto & _lastStatementNode()
Definition: block.h:30
Definition: parameter.h:45
void _addCase(switch_::Case case_)
Definition: switch.h:136
Definition: continue.h:14
auto & _lastCaseNode()
Definition: switch.h:133