CBMC
cpp_typecheck_namespace.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 <util/source_location.h>
13 #include <util/symbol_table_base.h>
14 
15 #include "cpp_typecheck.h"
16 
18 {
19  // save the scope
20  cpp_save_scopet saved_scope(cpp_scopes);
21 
22  const irep_idt &name=namespace_spec.get_namespace();
23 
24  if(name.empty())
25  {
26  // "unique namespace"
27  error().source_location=namespace_spec.source_location();
28  error() << "unique namespace not supported yet" << eom;
29  throw 0;
30  }
31 
32  irep_idt final_name(name);
33 
34  std::string identifier=
36 
37  symbol_table_baset::symbolst::const_iterator it =
38  symbol_table.symbols.find(identifier);
39 
40  if(it!=symbol_table.symbols.end())
41  {
42  if(namespace_spec.alias().is_not_nil())
43  {
44  error().source_location=namespace_spec.source_location();
45  error() << "namespace alias '" << final_name << "' previously declared\n"
46  << "location of previous declaration: " << it->second.location
47  << eom;
48  throw 0;
49  }
50 
51  if(it->second.type.id()!=ID_namespace)
52  {
53  error().source_location=namespace_spec.source_location();
54  error() << "namespace '" << final_name << "' previously declared\n"
55  << "location of previous declaration: " << it->second.location
56  << eom;
57  throw 0;
58  }
59 
60  // enter that scope
61  cpp_scopes.set_scope(it->first);
62  }
63  else
64  {
65  symbolt symbol{identifier, typet(ID_namespace), ID_cpp};
66  symbol.base_name=final_name;
67  symbol.location=namespace_spec.source_location();
68  symbol.module=module;
69 
70  if(!symbol_table.insert(std::move(symbol)).second)
71  {
72  error().source_location=symbol.location;
73  error() << "cpp_typecheckt::convert_namespace: symbol_table.move() failed"
74  << eom;
75  throw 0;
76  }
77 
78  cpp_scopes.new_namespace(final_name);
79  }
80 
81  if(namespace_spec.alias().is_not_nil())
82  {
83  cpp_typecheck_resolvet resolver(*this);
84  cpp_scopet &s=resolver.resolve_namespace(namespace_spec.alias());
86  }
87  else
88  {
89  // do the declarations
90  for(auto &item : namespace_spec.items())
91  convert(item);
92  }
93 }
symbol_table_baset & symbol_table
const irep_idt module
std::string prefix
Definition: cpp_id.h:79
const itemst & items() const
const irep_idt & get_namespace() const
cpp_scopet & new_namespace(const irep_idt &new_scope_name)
Definition: cpp_scopes.h:49
cpp_scopet & current_scope()
Definition: cpp_scopes.h:32
cpp_scopet & set_scope(const irep_idt &identifier)
Definition: cpp_scopes.h:87
void add_using_scope(cpp_scopet &other)
Definition: cpp_scope.h:109
cpp_scopet & resolve_namespace(const cpp_namet &cpp_name)
void convert(cpp_linkage_spect &)
cpp_scopest cpp_scopes
Definition: cpp_typecheck.h:92
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:38
bool empty() const
Definition: dstring.h:89
const source_locationt & source_location() const
Definition: expr.h:231
bool is_not_nil() const
Definition: irep.h:372
source_locationt source_location
Definition: message.h:239
mstreamt & error() const
Definition: message.h:391
static eomt eom
Definition: message.h:289
const symbolst & symbols
Read-only field, used to look up symbols given their names.
virtual std::pair< symbolt &, bool > insert(symbolt symbol)=0
Move or copy a new symbol to the symbol table.
Symbol table entry.
Definition: symbol.h:28
The type of an expression, extends irept.
Definition: type.h:29
C++ Language Type Checking.
const std::string & id2string(const irep_idt &d)
Definition: irep.h:44
Author: Diffblue Ltd.