tan  0.0.1
decl.cpp
1 #include "ast/decl.h"
2 #include "ast/type.h"
3 #include <algorithm>
4 
5 using namespace tanlang;
6 
7 Decl::Decl(ASTNodeType type, TokenizedSourceFile *src, int bp, bool is_extern, bool is_public)
8  : Expr(type, src, bp), _is_external(is_extern), _is_public(is_public) {}
9 
10 vector<ASTBase *> Decl::get_children() const { return {}; }
11 
12 bool Decl::is_public() const { return _is_public; }
13 void Decl::set_public(bool is_public) { _is_public = is_public; }
14 bool Decl::is_external() const { return _is_external; }
15 void Decl::set_external(bool is_external) { _is_external = is_external; }
16 
17 ArgDecl::ArgDecl(TokenizedSourceFile *src) : Decl(ASTNodeType::ARG_DECL, src, 0, false, false) {}
18 
19 ArgDecl *ArgDecl::Create(TokenizedSourceFile *src) { return new ArgDecl(src); }
20 
21 ArgDecl *ArgDecl::Create(TokenizedSourceFile *src, const str &name, Type *ty) {
22  auto ret = new ArgDecl(src);
23  ret->set_name(name);
24  ret->set_type(ty);
25  return ret;
26 }
27 
28 VarDecl::VarDecl(TokenizedSourceFile *src) : Decl(ASTNodeType::VAR_DECL, src, 0, false, false) {}
29 
30 VarDecl *VarDecl::Create(TokenizedSourceFile *src) { return new VarDecl(src); }
31 
32 VarDecl *VarDecl::Create(TokenizedSourceFile *src, const str &name, Type *ty) {
33  auto ret = new VarDecl(src);
34  ret->set_name(name);
35  ret->set_type(ty);
36  return ret;
37 }
38 
39 FunctionDecl::FunctionDecl(TokenizedSourceFile *src, bool is_extern, bool is_public)
40  : Decl(ASTNodeType::FUNC_DECL, src, 0, is_extern, is_public) {}
41 
42 FunctionDecl *FunctionDecl::Create(TokenizedSourceFile *src, bool is_extern, bool is_public) {
43  return new FunctionDecl(src, is_extern, is_public);
44 }
45 
46 FunctionDecl *FunctionDecl::Create(TokenizedSourceFile *src,
47  const str &name,
48  FunctionType *func_type,
49  bool is_external,
50  bool is_public,
51  Stmt *body,
52  bool is_intrinsic) {
53  auto ret = new FunctionDecl(src, is_external, is_public);
54  ret->set_name(name);
55  if (!body) {
56  ret->set_body(body);
57  }
58  ret->set_type(func_type);
59  ret->_is_intrinsic = is_intrinsic;
60  return ret;
61 }
62 
63 str FunctionDecl::get_arg_name(size_t i) const { return _arg_names[i]; }
64 
65 size_t FunctionDecl::get_n_args() const { return _arg_names.size(); }
66 
67 void FunctionDecl::set_body(Stmt *body) { _body = body; }
68 
69 void FunctionDecl::set_arg_names(const vector<str> &names) { _arg_names = names; }
70 
71 Stmt *FunctionDecl::get_body() const { return _body; }
72 
73 bool FunctionDecl::is_intrinsic() const { return _is_intrinsic; }
74 
75 void FunctionDecl::set_is_intrinsic(bool is_intrinsic) { _is_intrinsic = is_intrinsic; }
76 
77 const vector<ArgDecl *> &FunctionDecl::get_arg_decls() const { return _arg_decls; }
78 
79 void FunctionDecl::set_arg_decls(const vector<ArgDecl *> &arg_decls) { _arg_decls = arg_decls; }
80 
81 vector<ASTBase *> FunctionDecl::get_children() const { return {(ASTBase *)_body}; }
82 
84  if (is_external())
85  return ";";
86  return "}";
87 }
88 
89 TypeDecl::TypeDecl(ASTNodeType node_type, TokenizedSourceFile *src, bool is_extern, bool is_public)
90  : Decl(node_type, src, 0, is_extern, is_public) {}
91 
92 StructDecl::StructDecl(TokenizedSourceFile *src, bool is_extern, bool is_public)
93  : TypeDecl(ASTNodeType::STRUCT_DECL, src, is_extern, is_public) {}
94 
95 StructDecl *StructDecl::Create(TokenizedSourceFile *src, bool is_extern, bool is_public) {
96  return new StructDecl(src, is_extern, is_public);
97 }
98 
99 const vector<Expr *> &StructDecl::get_member_decls() const { return _member_decls; }
100 
101 void StructDecl::set_member_decls(const vector<Expr *> &member_decls) { _member_decls = member_decls; }
102 
103 Type *StructDecl::get_struct_member_ty(int i) const {
104  TAN_ASSERT(i >= 0 && i < (int)_member_decls.size());
105  return _member_decls[(size_t)i]->get_type();
106 }
107 
108 vector<Type *> StructDecl::get_member_types() const {
109  auto ret = vector<Type *>(_member_decls.size(), nullptr);
110  for (size_t i = 0; i < _member_decls.size(); ++i) {
111  ret[i] = _member_decls[i]->get_type();
112  }
113  return ret;
114 }
115 
116 int StructDecl::get_struct_member_index(const str &name) const {
117  auto search = _member_indices.find(name);
118  if (search == _member_indices.end()) {
119  return -1;
120  }
121  return search->second;
122 }
123 
124 void StructDecl::set_member_index(const str &name, int idx) {
125  TAN_ASSERT(idx >= 0 && idx < (int)_member_decls.size());
126  _member_indices[name] = idx;
127 }
128 
129 vector<ASTBase *> StructDecl::get_children() const {
130  vector<ASTBase *> ret = {};
131  std::for_each(_member_decls.begin(), _member_decls.end(), [&](Expr *e) { ret.push_back(e); });
132  return ret;
133 }
134 
135 Expr *StructDecl::get_member_default_val(int i) const {
136  auto it = _default_vals.find(i);
137  if (it == _default_vals.end()) {
138  return nullptr;
139  }
140  return it->second;
141 }
142 
143 void StructDecl::set_member_default_val(int i, Expr *val) { _default_vals[i] = val; }
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
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