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 }
The type of an expression.
Definition: type.h:22
irep_idt name
The unique identifier.
Definition: symbol.h:43
const std::string & id2string(const irep_idt &d)
Definition: irep.h:44
bool is_not_nil() const
Definition: irep.h:173
irep_idt mode
Language mode.
Definition: symbol.h:52
exprt value
Initial value of symbol.
Definition: symbol.h:37
cpp_scopet & new_namespace(const irep_idt &new_scope_name)
Definition: cpp_scopes.h:50
irep_idt module
Name of module the symbol belongs to.
Definition: symbol.h:46
Symbol table entry.This is a symbol in the symbol table, stored in an object of type symbol_tablet...
Definition: symbol.h:30
static mstreamt & eom(mstreamt &m)
Definition: message.h:272
void convert(cpp_linkage_spect &)
symbol_tablet & symbol_table
std::string prefix
Definition: cpp_id.h:80
source_locationt source_location
Definition: message.h:214
mstreamt & error() const
Definition: message.h:302
C++ Language Type Checking.
dstringt has one field, an unsigned integer no which is an index into a static table of strings...
Definition: dstring.h:33
const symbolst & symbols
cpp_scopet & set_scope(const irep_idt &identifier)
Definition: cpp_scopes.h:88
typet type
Type of symbol.
Definition: symbol.h:34
source_locationt location
Source code location of definition of symbol.
Definition: symbol.h:40
const irep_idt & get_namespace() const
irep_idt base_name
Base (non-scoped) name.
Definition: symbol.h:49
cpp_scopet & current_scope()
Definition: cpp_scopes.h:33
const source_locationt & source_location() const
Definition: expr.h:125
const irep_idt module
void make_nil()
Definition: irep.h:315
const itemst & items() const
cpp_scopet & resolve_namespace(const cpp_namet &cpp_name)
void add_using_scope(cpp_scopet &other)
Definition: cpp_scope.h:110
virtual std::pair< symbolt &, bool > insert(symbolt symbol) override
Author: Diffblue Ltd.
cpp_scopest cpp_scopes