Thorin 1.9.0
The Higher ORder INtermediate representation
Loading...
Searching...
No Matches
scopes.cpp
Go to the documentation of this file.
1#include "thorin/fe/scopes.h"
2
3#include "thorin/world.h"
4
5namespace thorin {
6
7void Scopes::pop() {
8 assert(!scopes_.empty());
9 scopes_.pop_back();
10}
11
12const Def* Scopes::query(Dbg dbg) const {
13 if (dbg.sym == '_') return nullptr;
14
15 for (auto& scope : scopes_ | std::ranges::views::reverse)
16 if (auto i = scope.find(dbg.sym); i != scope.end()) return i->second.second;
17
18 return nullptr;
19}
20
21const Def* Scopes::find(Dbg dbg) const {
22 if (dbg.sym == '_') error(dbg.loc, "the symbol '_' is special and never binds to anything");
23 if (auto res = query(dbg)) return res;
24 error(dbg.loc, "'{}' not found", dbg.sym);
25}
26
27void Scopes::bind(Scope* scope, Dbg dbg, const Def* def, bool rebind) {
28 auto [loc, sym] = dbg;
29 if (sym == '_') return; // don't do anything with '_'
30
31 if (rebind) {
32 (*scope)[sym] = std::pair(loc, def);
33 } else if (auto [i, ins] = scope->emplace(sym, std::pair(loc, def)); !ins) {
34 auto prev = i->second.first;
35 error(loc, "redeclaration of '{}'; previous declaration here: {}", sym, prev);
36 }
37}
38
39} // namespace thorin
Base class for all Defs.
Definition def.h:222
const Def * query(Dbg) const
Definition scopes.cpp:12
void pop()
Definition scopes.cpp:7
void bind(Scope *, Dbg, const Def *, bool rebind=false)
Definition scopes.cpp:27
fe::SymMap< std::pair< Loc, const Def * > > Scope
Definition scopes.h:14
const Def * find(Dbg) const
Same as Scopes::query but potentially raises an error.
Definition scopes.cpp:21
Definition cfg.h:11
void error(const Def *def, const char *fmt, Args &&... args)
Definition def.h:622