91 lines
2.1 KiB
C++
91 lines
2.1 KiB
C++
|
#include "SymTable.hpp"
|
||
|
#include "commons.hpp"
|
||
|
|
||
|
namespace wg
|
||
|
{
|
||
|
/*explicit*/ SymTable::SymTable()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
/*virtual*/ SymTable::~SymTable()
|
||
|
{
|
||
|
}
|
||
|
|
||
|
bool SymTable::exists(std::string const& name) const
|
||
|
{
|
||
|
return m_entries.find(name) != std::end(m_entries);
|
||
|
}
|
||
|
|
||
|
void SymTable::declare_prototype(std::string const& name,
|
||
|
llvm::Type* type,
|
||
|
Loc const& loc)
|
||
|
{
|
||
|
if (auto itr=m_entries.find(name);
|
||
|
itr != std::end(m_entries))
|
||
|
{
|
||
|
loc.error<symbol_error>("cannot declare existing symbol '"
|
||
|
+ name
|
||
|
+ "'");
|
||
|
}
|
||
|
|
||
|
SymEntry entry;
|
||
|
entry.name = name;
|
||
|
entry.type = type;
|
||
|
entry.prototype = true;
|
||
|
m_entries[name] = entry;
|
||
|
}
|
||
|
|
||
|
void SymTable::declare(std::string const& name,
|
||
|
llvm::Type* type,
|
||
|
Loc const& loc)
|
||
|
{
|
||
|
if (auto itr=m_entries.find(name);
|
||
|
itr != std::end(m_entries)
|
||
|
&& itr->second.prototype == false)
|
||
|
{
|
||
|
loc.error<symbol_error>("cannot declare existing symbol '"
|
||
|
+ name
|
||
|
+ "'");
|
||
|
}
|
||
|
|
||
|
SymEntry entry;
|
||
|
entry.name = name;
|
||
|
entry.type = type;
|
||
|
entry.prototype = false;
|
||
|
m_entries[name] = entry;
|
||
|
}
|
||
|
|
||
|
void SymTable::set(std::string const& name,
|
||
|
llvm::Type* type,
|
||
|
Loc const& loc)
|
||
|
{
|
||
|
if (auto itr=m_entries.find(name);
|
||
|
itr == std::end(m_entries))
|
||
|
{
|
||
|
loc.error<symbol_error>("cannot set inexisting symbol '"
|
||
|
+ name
|
||
|
+ "'");
|
||
|
}
|
||
|
|
||
|
SymEntry entry;
|
||
|
entry.name = name;
|
||
|
entry.type = type;
|
||
|
m_entries[name] = entry;
|
||
|
}
|
||
|
|
||
|
SymEntry& SymTable::get(std::string const& name, Loc const& loc)
|
||
|
{
|
||
|
if (auto itr=m_entries.find(name);
|
||
|
itr != std::end(m_entries))
|
||
|
{
|
||
|
return itr->second;
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
loc.error<symbol_error>("cannot find symbol '" + name + "'");
|
||
|
}
|
||
|
|
||
|
abort();
|
||
|
}
|
||
|
}
|