9 #include <hilti/ast/declarations/local-variable.h> 10 #include <hilti/ast/expression.h> 11 #include <hilti/ast/expressions/id.h> 12 #include <hilti/ast/expressions/unresolved-operator.h> 13 #include <hilti/ast/statement.h> 22 using Default =
struct {};
36 Case(hilti::Expression expr, Statement body,
Meta m =
Meta())
37 :
NodeBase(nodes(std::move(body), std::move(expr)), std::move(m)), _end_exprs(2) {}
38 Case(std::vector<hilti::Expression> exprs, Statement body,
Meta m =
Meta())
39 :
NodeBase(nodes(std::move(body), std::move(exprs)), std::move(m)), _end_exprs(
childs().size()) {}
41 :
NodeBase(nodes(std::move(body)), std::move(m)), _end_exprs(1) {}
44 auto expressions()
const {
return childs<hilti::Expression>(1, _end_exprs); }
45 auto preprocessedExpressions()
const {
return childs<hilti::Expression>(_end_exprs, -1); }
46 const auto& body()
const {
return child<Statement>(0); }
48 bool isDefault()
const {
return expressions().empty(); }
56 bool operator==(
const Case& other)
const {
return expressions() == other.expressions() && body() == other.body(); }
73 void _addPreprocessedExpression(hilti::Expression e) {
childs().emplace_back(std::move(e)); }
78 inline Node to_node(
Case c) {
return Node(std::move(c)); }
85 Switch(hilti::Expression cond,
const std::vector<switch_::Case>& cases,
Meta m =
Meta())
87 _preprocessCases(
"__x");
90 Switch(
const hilti::Declaration& init, hilti::Expression cond,
const std::vector<switch_::Case>& cases,
92 :
NodeBase(nodes(init, std::move(cond), cases), std::move(m)) {
94 logger().internalError(
"initialization for 'switch' must be a local declaration");
95 _preprocessCases(init.id());
98 auto init()
const {
return childs()[0].tryReferenceAs<hilti::Declaration>(); }
99 const auto& expression()
const {
return childs()[1].as<hilti::Expression>(); }
101 if (
auto i = init() )
104 return expression().type();
108 std::vector<switch_::Case> cases;
109 for (
auto& c : childs<switch_::Case>(2, -1) ) {
110 if ( ! c.isDefault() )
117 auto caseNodes() {
return nodesOfType<switch_::Case>(); }
119 std::optional<switch_::Case> default_()
const {
120 for (
auto& c : childs<switch_::Case>(2, -1) ) {
127 bool operator==(
const Switch& other)
const {
128 return init() == other.init() && expression() == other.expression() && default_() == other.default_() &&
129 cases() == other.cases();
137 for (
const auto& c : case_.expressions() )
139 {expression::UnresolvedID(ID(_id)), c},
147 auto isEqual(
const Statement& other)
const {
return node::isEqual(
this, other); }
153 void _preprocessCases(
const std::string&
id) {
156 for ( uint64_t i = 2; i <
childs().size(); i++ ) {
158 for (
const auto& c : case_.expressions() )
160 {expression::UnresolvedID(ID(id)), c},
static Case setExpressions(Case c, std::vector< hilti::Expression > exprs)
Definition: switch.h:65
Definition: local-variable.h:19
auto isEqual(const Statement &other) const
Definition: switch.h:147
auto & childs() const
Definition: node.h:445
std::vector< T > childs(int begin, int end) const
Definition: node.h:373
Definition: unresolved-operator.h:16
const Node none
Definition: node.cc:12
auto properties() const
Definition: switch.h:54
void addChild(Node n)
Definition: node.h:434
Definition: statement.h:14
std::map< std::string, node::detail::PropertyValue > Properties
Definition: node.h:83
void _addCase(switch_::Case case_)
Definition: switch.h:136
auto & _bodyNode()
Definition: switch.h:51
auto properties() const
Definition: switch.h:150
auto & _lastCaseNode()
Definition: switch.h:133