CBMC
cpp_typecheck_using.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  // there are two forms of using clauses:
19  // a) using namespace SCOPE; ("using directive")
20  // b) using SCOPE::id; ("using declaration")
21 
22  cpp_typecheck_resolvet resolver(*this);
23  cpp_save_scopet save_scope(this->cpp_scopes);
24 
25  irep_idt base_name;
26  cpp_template_args_non_tct template_args;
27  resolver.resolve_scope(cpp_using.name(), base_name, template_args);
28 
29  bool qualified=cpp_using.name().is_qualified();
30 
31  const auto id_set = cpp_scopes.current_scope().lookup(
32  base_name, qualified ? cpp_scopet::QUALIFIED : cpp_scopet::RECURSIVE);
33 
34  bool using_directive=cpp_using.get_namespace();
35 
36  if(id_set.empty())
37  {
38  error().source_location=cpp_using.name().source_location();
39  error() << "using " << (using_directive ? "namespace" : "identifier")
40  << " '" << base_name << "' not found" << eom;
41  throw 0;
42  }
43 
44  // go back to where we used to be
45  save_scope.restore();
46 
47  for(cpp_scopest::id_sett::iterator
48  it=id_set.begin();
49  it!=id_set.end();
50  it++)
51  {
52  if(using_directive)
53  {
54  if((*it)->id_class==cpp_idt::id_classt::NAMESPACE)
56  static_cast<cpp_scopet &>(**it));
57  else
58  {
59  // we should likely complain about this
60  }
61  }
62  else // declaration
63  {
64  // we copy all 'normal' identifiers into the current scope
65  if((*it)->id_class!=cpp_idt::id_classt::TEMPLATE_PARAMETER &&
66  (*it)->id_class!=cpp_idt::id_classt::NAMESPACE)
68  }
69  }
70 }
bool is_qualified() const
Definition: cpp_name.h:109
const source_locationt & source_location() const
Definition: cpp_name.h:73
cpp_scopet & current_scope()
Definition: cpp_scopes.h:32
void add_using_scope(cpp_scopet &other)
Definition: cpp_scope.h:109
id_sett lookup(const irep_idt &base_name_to_lookup, lookup_kindt kind)
Definition: cpp_scope.h:32
cpp_idt & insert(const irep_idt &_base_name)
Definition: cpp_scope.h:52
cpp_scopet & resolve_scope(const cpp_namet &cpp_name, irep_idt &base_name, cpp_template_args_non_tct &template_args)
void convert(cpp_linkage_spect &)
cpp_scopest cpp_scopes
Definition: cpp_typecheck.h:92
cpp_namet & name()
Definition: cpp_using.h:24
bool get_namespace() const
Definition: cpp_using.h:34
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:38
source_locationt source_location
Definition: message.h:239
mstreamt & error() const
Definition: message.h:391
static eomt eom
Definition: message.h:289
C++ Language Type Checking.