5 using namespace tanlang;
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) {}
10 vector<ASTBase *> Decl::get_children()
const {
return {}; }
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; }
40 :
Decl(ASTNodeType::FUNC_DECL, src, 0, is_extern, is_public) {}
53 auto ret =
new FunctionDecl(src, is_external, is_public);
58 ret->set_type(func_type);
59 ret->_is_intrinsic = is_intrinsic;
63 str FunctionDecl::get_arg_name(
size_t i)
const {
return _arg_names[i]; }
65 size_t FunctionDecl::get_n_args()
const {
return _arg_names.size(); }
67 void FunctionDecl::set_body(
Stmt *body) { _body = body; }
69 void FunctionDecl::set_arg_names(
const vector<str> &names) { _arg_names = names; }
71 Stmt *FunctionDecl::get_body()
const {
return _body; }
73 bool FunctionDecl::is_intrinsic()
const {
return _is_intrinsic; }
75 void FunctionDecl::set_is_intrinsic(
bool is_intrinsic) { _is_intrinsic = is_intrinsic; }
77 const vector<ArgDecl *> &FunctionDecl::get_arg_decls()
const {
return _arg_decls; }
79 void FunctionDecl::set_arg_decls(
const vector<ArgDecl *> &arg_decls) { _arg_decls = arg_decls; }
89 TypeDecl::TypeDecl(ASTNodeType node_type,
TokenizedSourceFile *src,
bool is_extern,
bool is_public)
90 :
Decl(node_type, src, 0, is_extern, is_public) {}
93 :
TypeDecl(ASTNodeType::STRUCT_DECL, src, is_extern, is_public) {}
96 return new StructDecl(src, is_extern, is_public);
99 const vector<Expr *> &StructDecl::get_member_decls()
const {
return _member_decls; }
101 void StructDecl::set_member_decls(
const vector<Expr *> &member_decls) { _member_decls = member_decls; }
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();
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();
116 int StructDecl::get_struct_member_index(
const str &name)
const {
117 auto search = _member_indices.find(name);
118 if (search == _member_indices.end()) {
121 return search->second;
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;
130 vector<ASTBase *> ret = {};
131 std::for_each(_member_decls.begin(), _member_decls.end(), [&](
Expr *e) { ret.push_back(e); });
135 Expr *StructDecl::get_member_default_val(
int i)
const {
136 auto it = _default_vals.find(i);
137 if (it == _default_vals.end()) {
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.
str terminal_token() const override
Which terminal token is expected immediately after this node.
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...