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=="")
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
45  << "' previously declared\n"
46  << "location of previous declaration: "
47  << it->second.location << 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
55  << "' previously declared\n"
56  << "location of previous declaration: "
57  << it->second.location << eom;
58  throw 0;
59  }
60 
61  // enter that scope
62  cpp_scopes.set_scope(it->first);
63  }
64  else
65  {
66  symbolt symbol;
67 
68  symbol.name=identifier;
69  symbol.base_name=final_name;
70  symbol.value.make_nil();
71  symbol.location=namespace_spec.source_location();
72  symbol.mode=ID_cpp;
73  symbol.module=module;
74  symbol.type=typet(ID_namespace);
75 
76  if(!symbol_table.insert(std::move(symbol)).second)
77  {
79  error() << "cpp_typecheckt::convert_namespace: symbol_table.move() failed"
80  << eom;
81  throw 0;
82  }
83 
84  cpp_scopes.new_namespace(final_name);
85  }
86 
87  if(namespace_spec.alias().is_not_nil())
88  {
89  cpp_typecheck_resolvet resolver(*this);
90  cpp_scopet &s=resolver.resolve_namespace(namespace_spec.alias());
92  }
93  else
94  {
95  // do the declarations
96  for(cpp_namespace_spect::itemst::iterator
97  it=namespace_spec.items().begin();
98  it!=namespace_spec.items().end();
99  it++)
100  convert(*it);
101  }
102 }
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:35
cpp_scopet
Definition: cpp_scope.h:20
cpp_save_scopet
Definition: cpp_scopes.h:128
cpp_typecheckt::cpp_scopes
cpp_scopest cpp_scopes
Definition: cpp_typecheck.h:109
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:114
irept::make_nil
void make_nil()
Definition: irep.h:315
typet
The type of an expression, extends irept.
Definition: type.h:27
cpp_scopest::new_namespace
cpp_scopet & new_namespace(const irep_idt &new_scope_name)
Definition: cpp_scopes.h:50
symbolt::type
typet type
Type of symbol.
Definition: symbol.h:31
cpp_typecheck_resolvet
Definition: cpp_typecheck_resolve.h:20
symbolt::base_name
irep_idt base_name
Base (non-scoped) name.
Definition: symbol.h:46
messaget::eom
static eomt eom
Definition: message.h:284
cpp_namespace_spect
Definition: cpp_namespace_spec.h:19
irept::is_not_nil
bool is_not_nil() const
Definition: irep.h:173
c_typecheck_baset::module
const irep_idt module
Definition: c_typecheck_base.h:68
symbolt::mode
irep_idt mode
Language mode.
Definition: symbol.h:49
messaget::error
mstreamt & error() const
Definition: message.h:386
id2string
const std::string & id2string(const irep_idt &d)
Definition: irep.h:44
cpp_scopest::current_scope
cpp_scopet & current_scope()
Definition: cpp_scopes.h:33
messaget::mstreamt::source_location
source_locationt source_location
Definition: message.h:236
cpp_typecheck_resolvet::resolve_namespace
cpp_scopet & resolve_namespace(const cpp_namet &cpp_name)
Definition: cpp_typecheck_resolve.cpp:1217
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:67
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
Definition: symbol_table_base.h:27
cpp_idt::prefix
std::string prefix
Definition: cpp_id.h:80
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:228
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:88