Joos1W Compiler Framework
All Classes Functions Typedefs Pages
ScopeID.cc
1 #include <sstream>
2 
3 #include "ast/AST.h"
4 
5 namespace ast {
6 
7 std::string ScopeID::toString() const {
8  std::ostringstream oss;
9  print(oss);
10  return oss.str();
11 }
12 
13 std::ostream& ScopeID::print(std::ostream& os) const {
14  if(parent_) {
15  parent_->print(os);
16  os << ".";
17  }
18  os << pos_;
19  return os;
20 }
21 
22 void ScopeID::dump() const { print(std::cerr) << std::endl; }
23 
24 bool ScopeID::canView(ScopeID const* other) const {
25  assert(other != nullptr && "Can't view the null scope");
26  // If under same scope, we can view other iff we are later position
27  if(this->parent_ == other->parent_) {
28  return this->pos_ >= other->pos_;
29  }
30  // If under different scope, check if other is visible from parent
31  if(this->parent_) {
32  return this->parent_->canView(other);
33  }
34  // If we're the topmost scope, then other is a child we cannot see.
35  return false;
36 }
37 
38 } // namespace ast