cprover
cpp_scopes.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: C++ Language Type Checking
4 
5 Author: Daniel Kroening, kroening@cs.cmu.edu
6 
7 \*******************************************************************/
8 
11 
12 #include "cpp_scopes.h"
13 
14 #include <ostream>
15 
17 {
18  unsigned prefix=++current_scope().compound_counter;
19  return new_scope(std::to_string(prefix), cpp_idt::id_classt::BLOCK_SCOPE);
20 }
21 
23  const symbolt &symbol,
24  cpp_scopet &scope,
25  bool is_friend)
26 {
27  assert(!symbol.name.empty());
28  assert(!symbol.base_name.empty());
29 
30  // functions are also scopes
31  if(symbol.type.id()==ID_code)
32  {
33  cpp_scopest::id_mapt::iterator id_it = id_map.find(symbol.name);
34  if(id_it == id_map.end())
35  {
36  irep_idt block_base_name(std::string("$block:")+symbol.base_name.c_str());
37  cpp_idt &id = scope.insert(block_base_name);
39  id.identifier=symbol.name;
40  id.is_scope=true;
41  id.prefix = id2string(scope.prefix) + id2string(symbol.base_name) + "::";
42  id_map[symbol.name]=&id;
43  }
44  }
45 
46  // should go away, and be replaced by the 'tag only declaration' rule
47  if(is_friend)
48  {
49  cpp_save_scopet saved_scope(*this);
50  go_to(scope);
52 
53  cpp_idt &id=current_scope().insert(symbol.base_name);
54  id.identifier=symbol.name;
55  id.id_class = cpp_idt::id_classt::SYMBOL;
56  if(id_map.find(symbol.name)==id_map.end())
57  id_map[symbol.name]=&id;
58  return id;
59  }
60  else
61  {
62  cpp_idt &id=scope.insert(symbol.base_name);
63  id.identifier=symbol.name;
64  id.id_class = cpp_idt::id_classt::SYMBOL;
65  if(id_map.find(symbol.name)==id_map.end())
66  id_map[symbol.name]=&id;
67  return id;
68  }
69 }
70 
72 void cpp_scopest::print_current(std::ostream &out) const
73 {
74  const cpp_scopet *scope=current_scope_ptr;
75 
76  do
77  {
78  scope->print_fields(out);
79  out << "\n";
80  scope=&scope->get_parent();
81  }
82  while(!scope->is_root_scope());
83 }
irep_idt name
The unique identifier.
Definition: symbol.h:46
C++ Language Type Checking.
const std::string & id2string(const irep_idt &d)
Definition: irep.h:44
void print_fields(std::ostream &out, unsigned indent=0) const
Definition: cpp_id.cpp:46
void go_to(cpp_idt &id)
Definition: cpp_scopes.h:104
cpp_scopet & get_parent() const
Definition: cpp_scope.h:89
cpp_idt & put_into_scope(const symbolt &symbol, cpp_scopet &scope, bool is_friend=false)
Definition: cpp_scopes.cpp:22
Symbol table entry.This is a symbol in the symbol table, stored in an object of type symbol_tablet...
Definition: symbol.h:33
std::string prefix
Definition: cpp_id.h:80
const irep_idt & id() const
Definition: irep.h:189
irep_idt identifier
Definition: cpp_id.h:73
id_classt id_class
Definition: cpp_id.h:51
void go_to_global_scope()
Definition: cpp_scopes.h:111
void print_current(std::ostream &out) const
Definition: cpp_scopes.cpp:72
unsigned compound_counter
Definition: cpp_id.h:81
id_mapt id_map
Definition: cpp_scopes.h:69
typet type
Type of symbol.
Definition: symbol.h:37
irep_idt base_name
Base (non-scoped) name.
Definition: symbol.h:52
cpp_scopet & current_scope()
Definition: cpp_scopes.h:33
cpp_scopet * current_scope_ptr
Definition: cpp_scopes.h:71
cpp_scopet & new_block_scope()
Definition: cpp_scopes.cpp:16
const char * c_str() const
Definition: dstring.h:72
Definition: cpp_id.h:28
cpp_scopet & new_scope(const irep_idt &new_scope_name, cpp_idt::id_classt id_class)
Definition: cpp_scopes.h:38
bool is_root_scope() const
Definition: cpp_scope.h:73
cpp_idt & insert(const irep_idt &_base_name)
Definition: cpp_scope.h:48
bool empty() const
Definition: dstring.h:61