tan  0.0.1
context.cpp
1 #include "ast/context.h"
2 #include "ast/decl.h"
3 
4 using namespace tanlang;
5 
6 ASTBase *Context::owner() const { return _owner; }
7 
8 Context::Context(ASTBase *owner) : _owner(owner) {}
9 
10 void Context::set_decl(const str &name, Decl *decl) {
11  TAN_ASSERT(!name.empty());
12  _type_decls[name] = decl;
13 }
14 
15 Decl *Context::get_decl(const str &name) const {
16  TAN_ASSERT(name != "");
17  auto q = _type_decls.find(name);
18  if (q != _type_decls.end()) {
19  return q->second;
20  }
21  return nullptr;
22 }
23 
24 vector<Decl *> Context::get_decls() const {
25  vector<Decl *> ret(_type_decls.size(), nullptr);
26  size_t i = 0;
27  for (const auto &p : _type_decls)
28  ret[i++] = p.second;
29  return ret;
30 }
31 
32 FunctionDecl *Context::get_func_decl(const str &name) const {
33  TAN_ASSERT(name != "");
34  auto q = _func_decls.find(name);
35  if (q != _func_decls.end()) {
36  return q->second;
37  }
38  return nullptr;
39 }
40 
42  str name = func->get_name();
43  TAN_ASSERT(!name.empty());
44  _func_decls[name] = func;
45 }
46 
47 vector<FunctionDecl *> Context::get_func_decls() const {
48  vector<FunctionDecl *> ret(_func_decls.size(), nullptr);
49  size_t i = 0;
50  for (const auto &p : _func_decls)
51  ret[i++] = p.second;
52  return ret;
53 }
vector< Decl * > get_decls() const
Get all type declarations in the context.
Definition: context.cpp:24
Decl * get_decl(const str &name) const
Search for a declaration by name.
Definition: context.cpp:15
void set_function_decl(FunctionDecl *func)
Register a function declaration.
Definition: context.cpp:41
vector< FunctionDecl * > get_func_decls() const
Get all functions registered in the context.
Definition: context.cpp:47
FunctionDecl * get_func_decl(const str &name) const
Search for a function declaration by name.
Definition: context.cpp:32