[ozulis] starting to add support for alias and typedef
[ozulis.git] / src / ozulis / ast / scope.cc
blobd079a4374e99736a07a6812a20a6b1c88c25b55f
1 #include <sstream>
2 #include <boost/format.hpp>
4 #include <ozulis/compiler.hh>
5 #include <ozulis/core/assert.hh>
6 #include "scope.hh"
8 template class std::map<std::string, ozulis::ast::SymbolPtr>;
9 template class std::map<std::string, ozulis::ast::TypePtr>;
11 namespace ozulis
13 namespace ast
15 Scope::Scope()
16 : prefix(),
17 parent(0),
18 symbols_(),
19 types_(),
20 nextId_(0)
24 Scope::~Scope()
28 SymbolPtr
29 Scope::findSymbol(const std::string & name) const
31 symbols_t::const_iterator it = symbols_.find(name);
32 if (it == symbols_.end())
33 return parent ? parent->findSymbol(name) : SymbolPtr(0);
34 return it->second;
37 void
38 Scope::addSymbol(SymbolPtr symbol)
40 assert(symbol);
41 assert(!symbol->name.empty());
42 assert(symbol->type);
43 assert(symbol->address);
45 /* Check that the symbol is undefined */
46 if (symbols_.find(symbol->name) != symbols_.end())
47 Compiler::instance().error((boost::format(
48 _("multiple definition of the variable %1%")) %
49 symbol->name).str());
51 symbols_[symbol->name] = symbol;
53 /* make the address */
54 if (symbol->address->nodeType == MemoryAddress::nodeTypeId())
55 reinterpret_cast<MemoryAddress *>(symbol->address.ptr())->address =
56 prefix + symbol->name;
57 if (symbol->address->nodeType == RegisterAddress::nodeTypeId())
58 reinterpret_cast<RegisterAddress *>(symbol->address.ptr())->address =
59 prefix + symbol->name;
62 TypePtr
63 Scope::findType(const std::string & name) const
65 types_t::const_iterator it = types_.find(name);
66 if (it == types_.end())
67 return parent ? parent->findType(name) : TypePtr();
68 return it->second;
71 void
72 Scope::addType(const std::string & name, TypePtr type)
74 assert(type);
75 assert(!name.empty());
77 /* Check that the type is undefined */
78 if (types_.find(name) != types_.end())
79 Compiler::instance().error((boost::format(
80 _("multiple definition of the type %1%")) % name).str());
82 types_[name] = type;
85 std::string
86 Scope::nextId()
88 std::stringstream ss;
89 ss << "$scope$" << nextId_++;
90 return ss.str();
93 std::string
94 Scope::nextStringId()
96 return nextId();