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 "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 }
cpp_typecheckt::convert
void convert(cpp_linkage_spect &)
Definition: cpp_typecheck_linkage_spec.cpp:14
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:36
cpp_scopet
Definition: cpp_scope.h:20
cpp_save_scopet
Definition: cpp_scopes.h:127
cpp_typecheckt::cpp_scopes
cpp_scopest cpp_scopes
Definition: cpp_typecheck.h:104
cpp_namespace_spect::items
const itemst & items() const
Definition: cpp_namespace_spec.h:29
cpp_scopet::add_using_scope
void add_using_scope(cpp_scopet &other)
Definition: cpp_scope.h:109
irept::make_nil
void make_nil()
Definition: irep.h:454
typet
The type of an expression, extends irept.
Definition: type.h:28
cpp_scopest::new_namespace
cpp_scopet & new_namespace(const irep_idt &new_scope_name)
Definition: cpp_scopes.h:49
symbolt::type
typet type
Type of symbol.
Definition: symbol.h:31
cpp_typecheck_resolvet
Definition: cpp_typecheck_resolve.h:23
symbolt::base_name
irep_idt base_name
Base (non-scoped) name.
Definition: symbol.h:46
messaget::eom
static eomt eom
Definition: message.h:297
cpp_namespace_spect
Definition: cpp_namespace_spec.h:19
irept::is_not_nil
bool is_not_nil() const
Definition: irep.h:380
c_typecheck_baset::module
const irep_idt module
Definition: c_typecheck_base.h:69
symbolt::mode
irep_idt mode
Language mode.
Definition: symbol.h:49
messaget::error
mstreamt & error() const
Definition: message.h:399
id2string
const std::string & id2string(const irep_idt &d)
Definition: irep.h:47
cpp_scopest::current_scope
cpp_scopet & current_scope()
Definition: cpp_scopes.h:32
messaget::mstreamt::source_location
source_locationt source_location
Definition: message.h:247
cpp_typecheck_resolvet::resolve_namespace
cpp_scopet & resolve_namespace(const cpp_namet &cpp_name)
Definition: cpp_typecheck_resolve.cpp:1227
cpp_namespace_spect::get_namespace
const irep_idt & get_namespace() const
Definition: cpp_namespace_spec.h:39
source_location.h
symbol_tablet::insert
virtual std::pair< symbolt &, bool > insert(symbolt symbol) override
Author: Diffblue Ltd.
Definition: symbol_table.cpp:17
c_typecheck_baset::symbol_table
symbol_tablet & symbol_table
Definition: c_typecheck_base.h:68
dstringt::empty
bool empty() const
Definition: dstring.h:88
cpp_typecheck.h
cpp_namespace_spect::alias
cpp_namet & alias()
Definition: cpp_namespace_spec.h:49
symbolt::value
exprt value
Initial value of symbol.
Definition: symbol.h:34
symbolt::location
source_locationt location
Source code location of definition of symbol.
Definition: symbol.h:37
symbolt
Symbol table entry.
Definition: symbol.h:27
symbol_table_baset::symbols
const symbolst & symbols
Read-only field, used to look up symbols given their names.
Definition: symbol_table_base.h:30
cpp_idt::prefix
std::string prefix
Definition: cpp_id.h:79
symbolt::module
irep_idt module
Name of module the symbol belongs to.
Definition: symbol.h:43
exprt::source_location
const source_locationt & source_location() const
Definition: expr.h:211
symbolt::name
irep_idt name
The unique identifier.
Definition: symbol.h:40
cpp_scopest::set_scope
cpp_scopet & set_scope(const irep_idt &identifier)
Definition: cpp_scopes.h:87