cprover
Loading...
Searching...
No Matches
cpp_typecheck_namespace.cpp
Go to the documentation of this file.
1/*******************************************************************\
2
3Module: C++ Language Type Checking
4
5Author: Daniel Kroening, kroening@cs.cmu.edu
6
7\*******************************************************************/
8
11
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=
35 cpp_scopes.current_scope().prefix+id2string(final_name);
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());
85 cpp_scopes.current_scope().add_using_scope(s);
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 itemst & items() const
const irep_idt & get_namespace() const
cpp_scopet & resolve_namespace(const cpp_namet &cpp_name)
void convert(cpp_linkage_spect &)
cpp_scopest cpp_scopes
friend class cpp_typecheck_resolvet
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
Symbol table entry.
Definition symbol.h:28
irep_idt base_name
Base (non-scoped) name.
Definition symbol.h:46
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.
dstringt irep_idt