tan  0.0.1
decl.h
1 #ifndef TAN_SRC_AST_AST_ARG_DECL_H
2 #define TAN_SRC_AST_AST_ARG_DECL_H
3 #include "base.h"
4 #include "fwd.h"
5 #include "ast/ast_base.h"
6 #include "ast/ast_named.h"
7 #include "ast/typed.h"
8 #include "ast/expr.h"
9 
10 namespace tanlang {
11 
12 class Decl : public Expr, public ASTNamed {
13 public:
14  bool is_lvalue() override { return true; }
15  void set_lvalue(bool) override { TAN_ASSERT(false); }
16  [[nodiscard]] vector<ASTBase *> get_children() const override;
17  virtual bool is_type_decl() const { return false; }
18 
19  [[nodiscard]] bool is_public() const;
20  void set_public(bool is_public);
21  [[nodiscard]] bool is_external() const;
22  void set_external(bool is_external);
23 
24 protected:
25  Decl(ASTNodeType type, TokenizedSourceFile *src, int bp, bool is_extern, bool is_public);
26 
27 private:
28  bool _is_external = false;
29  bool _is_public = false;
30 };
31 
32 // TODO: external variables
33 class VarDecl : public Decl {
34 protected:
35  explicit VarDecl(TokenizedSourceFile *src);
36 
37 public:
38  static VarDecl *Create(TokenizedSourceFile *src);
39  static VarDecl *Create(TokenizedSourceFile *src, const str &name, Type *ty);
40 };
41 
42 class ArgDecl : public Decl {
43 protected:
44  explicit ArgDecl(TokenizedSourceFile *src);
45 
46 public:
47  static ArgDecl *Create(TokenizedSourceFile *src);
48  static ArgDecl *Create(TokenizedSourceFile *src, const str &name, Type *ty);
49 };
50 
51 class FunctionType;
52 class FunctionDecl : public Decl {
53 protected:
54  explicit FunctionDecl(TokenizedSourceFile *src, bool is_extern, bool is_public);
55 
56 public:
57  static FunctionDecl *Create(TokenizedSourceFile *src, bool is_extern, bool is_public);
58  static FunctionDecl *Create(TokenizedSourceFile *src, const str &name, FunctionType *func_type, bool is_external,
59  bool is_public, Stmt *body = nullptr, bool is_intrinsic = false);
60 
61 public:
62  str terminal_token() const override;
63 
64  void set_body(Stmt *body);
65  [[nodiscard]] Stmt *get_body() const;
66 
67  [[nodiscard]] size_t get_n_args() const;
68  [[nodiscard]] str get_arg_name(size_t i) const;
69  void set_arg_names(const vector<str> &names);
70  [[nodiscard]] const vector<ArgDecl *> &get_arg_decls() const;
71  void set_arg_decls(const vector<ArgDecl *> &arg_decls);
72 
73  bool is_intrinsic() const;
74  void set_is_intrinsic(bool is_intrinsic);
75 
76  [[nodiscard]] vector<ASTBase *> get_children() const override;
77 
78 private:
79  bool _is_intrinsic = false;
80 
81  vector<str> _arg_names{};
82  vector<ArgDecl *> _arg_decls{};
83 
84  Stmt *_body = nullptr;
85 };
86 
87 class TypeDecl : public Decl {
88 public:
89  TypeDecl(ASTNodeType node_type, TokenizedSourceFile *src, bool is_extern, bool is_public);
90  bool is_type_decl() const override { return true; }
91 };
92 
93 class StructDecl : public TypeDecl {
94 protected:
95  explicit StructDecl(TokenizedSourceFile *src, bool is_extern, bool is_public);
96 
97 public:
98  static StructDecl *Create(TokenizedSourceFile *src, bool is_extern, bool is_public);
99 
100 public:
101  const vector<Expr *> &get_member_decls() const;
102  void set_member_decls(const vector<Expr *> &member_decls);
103 
104  Type *get_struct_member_ty(int i) const;
105  vector<Type *> get_member_types() const;
106 
107  int get_struct_member_index(const str &name) const;
108  void set_member_index(const str &name, int idx);
109 
110  Expr *get_member_default_val(int i) const;
111  void set_member_default_val(int i, Expr *val);
112 
113 public:
114  vector<ASTBase *> get_children() const override;
115 
116  str terminal_token() const override { return "}"; }
117 
118 private:
119  vector<Expr *> _member_decls{};
120  umap<int, Expr *> _default_vals{};
121  umap<str, int> _member_indices{};
122 };
123 
124 } // namespace tanlang
125 
126 #endif
All named AST nodes should inherit this class.
Definition: ast_named.h:10
vector< ASTBase * > get_children() const override
Get a ordered list of child nodes.
Definition: decl.cpp:10
vector< ASTBase * > get_children() const override
Get a ordered list of child nodes.
Definition: decl.cpp:81
str terminal_token() const override
Which terminal token is expected immediately after this node.
Definition: decl.cpp:83
vector< ASTBase * > get_children() const override
Get a ordered list of child nodes.
Definition: decl.cpp:129
str terminal_token() const override
Which terminal token is expected immediately after this node.
Definition: decl.h:116
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...
Definition: type.h:22