7 #include "ast/AstNode.h"
9 #include "diagnostics/Location.h"
10 #include "utils/BumpAllocator.h"
11 #include "utils/EnumMacros.h"
12 #include "utils/Generator.h"
26 explicit ExprNode(
SourceRange loc) : next_{
nullptr}, loc_{loc} {}
27 virtual std::ostream& print(std::ostream& os)
const {
return os <<
"ExprNode"; }
28 virtual ~ExprNode() =
default;
32 assert(!locked_ &&
"Attempt to mutate locked node");
35 const ExprNode* next()
const {
return next_; }
36 ExprNode* mut_next()
const {
return next_; }
42 friend class ExprEvaluator;
44 void const_lock()
const { locked_ =
true; }
45 void const_unlock()
const { locked_ =
false; }
54 mutable bool locked_ =
false;
60 class ExprNodeList
final {
62 explicit ExprNodeList(
ExprNode* node) : head_{node}, tail_{node}, size_{1} {
63 node->setNext(
nullptr);
65 ExprNodeList() : head_{
nullptr}, tail_{
nullptr}, size_{0} {}
66 bool isBracketed =
false;
73 if(node ==
nullptr)
return;
74 if(head_ ==
nullptr) {
81 node->setNext(
nullptr);
91 if(other.size_ == 0)
return;
92 if(head_ ==
nullptr) {
96 tail_->setNext(other.head_);
109 concat(std::move(other));
110 other.head_ =
nullptr;
111 other.tail_ =
nullptr;
116 [[nodiscard]]
auto size()
const {
return size_; }
121 for(
auto const* node = head_; i < size_; node = node->next(), i++) {
129 for(
auto node = head_; i < size_; node = node->mut_next(), i++) {
134 ExprNode* mut_head()
const {
return head_; }
135 ExprNode const* tail()
const {
return tail_; }
138 std::ostream& print(std::ostream&)
const;
141 void check_invariants()
const;
151 namespace ast::exprnode {
160 :
ExprNode{loc}, decl_{
nullptr}, type_{type} {}
161 ast::
Decl const* decl()
const {
return decl_; }
162 virtual bool isDeclResolved()
const {
return decl_ !=
nullptr; }
163 bool isTypeResolved()
const {
return type_ !=
nullptr; }
164 void resolveDeclAndType(ast::
Decl const* decl, ast::
Type const* type) {
165 assert(!decl_ &&
"Tried to resolve expression decl twice");
167 assert(!type_ &&
"Tried to resolve expression type twice");
168 assert((!type || type->isResolved()) &&
169 "Tried to resolve expression with unresolved type");
172 void overrideDecl(ast::
Decl const* decl) { decl_ = decl; }
173 ast::
Type const* type()
const {
return type_; }
176 ast::
Type const* set_type(ast::
Type const* type) {
177 assert(!type_ &&
"Tried to set type twice");
183 ast::
Decl const* decl_;
184 ast::
Type const* type_;
189 MemberName(BumpAllocator& alloc, std::string_view name,
SourceRange loc)
191 std::ostream& print(std::ostream& os)
const;
192 std::string_view name()
const {
return name_; }
195 std::pmr::string name_;
200 MethodName(BumpAllocator& alloc, std::string_view name,
SourceRange loc)
202 std::ostream& print(std::ostream& os)
const override;
208 std::ostream& print(std::ostream& os)
const override;
215 assert(unres_type_ &&
"Tried to resolve underlying type twice");
222 set_type(unres_type_);
224 bool isDeclResolved()
const override {
return true; }
225 std::ostream& print(std::ostream& os)
const override;
228 ast::
Type* unres_type_;
233 LiteralNode(BumpAllocator&, parsetree::
Literal const* node,
235 bool isDeclResolved()
const override {
return true; }
236 std::ostream& print(std::ostream& os)
const override;
237 ast::BuiltInType
const* builtinType()
const;
238 uint32_t getAsInt()
const {
return std::get<uint32_t>(value_); }
239 auto const& getAsString()
const {
return std::get<std::pmr::string>(value_); }
242 std::variant<uint32_t, std::pmr::string> value_;
252 :
ExprNode{loc}, num_args_{num_args}, result_type_{
nullptr} {}
255 auto nargs()
const {
return num_args_; }
256 std::ostream& print(std::ostream& os)
const override = 0;
257 ast::
Type const* resolveResultType(ast::
Type const* type) {
258 assert(!result_type_ &&
"Tried to operator-resolve result type twice");
259 assert((!type || type->isResolved()) &&
260 "Tried to resolve operator with unresolved type");
264 ast::
Type const* resultType()
const {
return result_type_; }
268 ast::
Type const* result_type_;
271 class MemberAccess
final :
public ExprOp {
274 std::ostream& print(std::ostream& os)
const override;
277 class MethodInvocation
final :
public ExprOp {
280 std::ostream& print(std::ostream& os)
const override;
283 class ClassInstanceCreation
final :
public ExprOp {
286 std::ostream& print(std::ostream& os)
const override;
289 class ArrayInstanceCreation
final :
public ExprOp {
292 std::ostream& print(std::ostream& os)
const override;
295 class ArrayAccess
final :
public ExprOp {
298 std::ostream& print(std::ostream& os)
const override;
304 std::ostream& print(std::ostream& os)
const override;
308 #define UNARY_OP_TYPE_LIST(F)
317 #undef UNARY_OP_TYPE_LIST
324 std::ostream& print(std::ostream& os)
const override;
325 OpType opType()
const {
return type; }
329 #define BINARY_OP_TYPE_LIST(F)
332 F(GreaterThanOrEqual)
351 #undef BINARY_OP_TYPE_LIST
356 const ast::
VarDecl* varAssigned =
nullptr;
360 std::ostream& print(std::ostream& os)
const override;
361 OpType opType()
const {
return type; }
362 void setVarAssigned(
const ast::
VarDecl* var) {
363 assert(!varAssigned &&
"Tried to set varAssigned twice");
366 const ast::
VarDecl* getVarAssigned()
const {
return varAssigned; }