Spicy
ternary.h
1 // Copyright (c) 2020-now by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 
5 #include <memory>
6 #include <utility>
7 
8 #include <hilti/ast/expression.h>
9 #include <hilti/ast/type.h>
10 
11 namespace hilti::expression {
12 
14 class Ternary : public Expression {
15 public:
16  auto condition() const { return child<Expression>(0); }
17  auto true_() const { return child<Expression>(1); }
18  auto false_() const { return child<Expression>(2); }
19 
20  QualifiedType* type() const final {
21  // TODO(robin): Currently we enforce both having the same type; we
22  // might need to coerce to target type though.
23  return true_()->type();
24  }
25 
26  void setTrue(ASTContext* ctx, Expression* e) { setChild(ctx, 1, e); }
27  void setFalse(ASTContext* ctx, Expression* e) { setChild(ctx, 2, e); }
28 
29  static auto create(ASTContext* ctx, Expression* cond, Expression* true_, Expression* false_, Meta meta = {}) {
30  return ctx->make<Ternary>(ctx, {cond, true_, false_}, std::move(meta));
31  }
32 
33 protected:
34  Ternary(ASTContext* ctx, Nodes children, Meta meta)
35  : Expression(ctx, NodeTags, std::move(children), std::move(meta)) {}
36 
37  HILTI_NODE_1(expression::Ternary, Expression, final);
38 };
39 
40 } // namespace hilti::expression
Definition: ast-context.h:121
Definition: expression.h:15
void setChild(ASTContext *ctx, size_t idx, Node *n)
Definition: node.h:602
const auto & children() const
Definition: node.h:364
const auto & meta() const
Definition: node.h:306
Definition: type.h:362
Definition: ternary.h:14
QualifiedType * type() const final
Definition: ternary.h:20