This repository has been archived on 2024-03-07. You can view files and clone it, but cannot push or open issues/pull-requests.
wongola/lib/SymTable.cpp

91 lines
2.1 KiB
C++
Raw Normal View History

#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();
}
}