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