1 #ifndef __TAN_SRC_AST_EXPR_H__
2 #define __TAN_SRC_AST_EXPR_H__
17 virtual bool is_comptime_known() {
return false; }
18 virtual bool is_lvalue() {
return _is_lvalue; }
19 virtual void set_lvalue(
bool is_lvalue) { _is_lvalue = is_lvalue; }
20 [[nodiscard]] vector<ASTBase *>
get_children()
const override;
22 bool is_expr()
const override {
return true; }
23 bool is_stmt()
const override {
return false; }
26 bool _is_lvalue =
false;
41 vector<Literal *> elements = {});
45 bool is_comptime_known()
override {
return true; }
54 [[nodiscard]]
bool get_value()
const;
67 [[nodiscard]] uint64_t get_value()
const {
return _value; }
68 [[nodiscard]]
bool is_unsigned()
const {
return _is_unsigned; }
72 bool _is_unsigned =
false;
81 [[nodiscard]]
double get_value()
const;
82 void set_value(
double value);
95 [[nodiscard]] str get_value()
const;
96 void set_value(str val) { _value = std::move(val); }
109 void set_value(uint8_t val);
110 [[nodiscard]] uint8_t get_value()
const;
124 void set_elements(
const vector<Literal *> &elements);
125 [[nodiscard]] vector<Literal *> get_elements()
const;
128 vector<Literal *> _elements{};
145 [[nodiscard]]
Decl *get_referred()
const;
147 [[nodiscard]]
Type *get_type()
const override;
148 void set_type(
Type *)
override;
151 Decl *_referred =
nullptr;
154 enum class IdentifierType {
167 [[nodiscard]] IdentifierType get_id_type()
const;
168 void set_var_ref(
VarRef *var_ref);
169 void set_type_ref(
Type *type_ref);
170 [[nodiscard]]
VarRef *get_var_ref()
const;
171 bool is_lvalue()
override;
172 void set_lvalue(
bool is_lvalue)
override;
175 IdentifierType _id_type = IdentifierType::INVALID;
176 VarRef *_var_ref =
nullptr;
180 enum class BinaryOpKind {
211 void set_lhs(
Expr *lhs);
212 void set_rhs(
Expr *rhs);
213 [[nodiscard]]
Expr *get_lhs()
const;
214 [[nodiscard]]
Expr *get_rhs()
const;
215 [[nodiscard]] BinaryOpKind get_op()
const;
216 [[nodiscard]] vector<ASTBase *>
get_children()
const override;
220 Expr *_lhs =
nullptr;
221 Expr *_rhs =
nullptr;
230 bool is_lvalue()
override;
231 void set_lvalue(
bool is_lvalue)
override;
235 MemberAccessInvalid = 0,
237 MemberAccessMemberVariable,
238 MemberAccessMemberFunction,
239 } _access_type = MemberAccessInvalid;
240 int _access_idx = -1;
244 enum class UnaryOpKind {
265 [[nodiscard]] UnaryOpKind get_op()
const;
266 [[nodiscard]]
Expr *get_rhs()
const;
267 void set_rhs(
Expr *rhs);
269 [[nodiscard]] vector<ASTBase *>
get_children()
const override;
272 UnaryOpKind _op = UnaryOpKind::INVALID;
273 Expr *_rhs =
nullptr;
287 enum BinaryOrUnaryKind {
296 [[nodiscard]]
Expr *get_expr_ptr()
const;
298 [[nodiscard]]
Type *get_type()
const override;
299 void set_type(
Type *type)
override;
301 [[nodiscard]] vector<ASTBase *>
get_children()
const override;
303 bool is_lvalue()
override;
304 void set_lvalue(
bool is_lvalue)
override;
307 BinaryOrUnaryKind _kind = UNKNOWN;
321 void set_sub(
Expr *sub);
322 [[nodiscard]]
Expr *get_sub()
const;
323 [[nodiscard]] vector<ASTBase *>
get_children()
const override;
325 bool is_lvalue()
override;
326 void set_lvalue(
bool is_lvalue)
override;
329 Expr *_sub =
nullptr;
338 [[nodiscard]]
size_t get_n_args()
const;
339 [[nodiscard]]
Expr *get_arg(
size_t i)
const;
341 [[nodiscard]] vector<ASTBase *>
get_children()
const override;
344 vector<Expr *> _args{};
355 [[nodiscard]]
ASTBase *get_lhs()
const;
358 void set_rhs(
Expr *rhs);
360 [[nodiscard]] vector<ASTBase *>
get_children()
const override;
374 void set_lhs(
Expr *lhs);
376 [[nodiscard]] vector<ASTBase *>
get_children()
const override;
378 bool is_lvalue()
override;
379 void set_lvalue(
bool is_lvalue)
override;
380 bool is_comptime_known()
override;
383 Expr *_lhs =
nullptr;
All named AST nodes should inherit this class.
Expr * _rhs
lhs can be decl or expr (identifier)
vector< ASTBase * > get_children() const override
Get a ordered list of child nodes.
BinaryOperator(BinaryOpKind op, TokenizedSourceFile *src)
vector< ASTBase * > get_children() const override
Get a ordered list of child nodes.
static umap< BinaryOpKind, int > BOPPrecedence
binary operator precedence
ASTBase * get() const override
Get the "actual" this. Used for implementing proxy classes.
vector< ASTBase * > get_children() const override
Get a ordered list of child nodes.
vector< ASTBase * > get_children() const override
Get a ordered list of child nodes.
vector< ASTBase * > get_children() const override
Get a ordered list of child nodes.
vector< ASTBase * > get_children() const override
Get a ordered list of child nodes.
static FunctionCall * Create(TokenizedSourceFile *src)
Literal(ASTNodeType type, TokenizedSourceFile *src, int bp)
static MemberAccess * Create(TokenizedSourceFile *src)
static Parenthesis * Create(TokenizedSourceFile *src)
vector< ASTBase * > get_children() const override
Get a ordered list of child nodes.
Different from SourceFile, TokenizedSourceFile manages the tokenized text of a source file.
Type is immutable once created. The exception is StructType. Its information is updated in multiple s...
All typed AST nodes should inherit this class.
static umap< UnaryOpKind, int > UOPPrecedence
binary operator precedence
vector< ASTBase * > get_children() const override
Get a ordered list of child nodes.
static VarRef * Create(TokenizedSourceFile *src, const str &name, Decl *referred)