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 }
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_scopet::lookup
id_sett lookup(const irep_idt &base_name_to_lookup, lookup_kindt kind)
Definition: cpp_scope.h:32
cpp_scopet::add_using_scope
void add_using_scope(cpp_scopet &other)
Definition: cpp_scope.h:109
cpp_scopet::QUALIFIED
@ QUALIFIED
Definition: cpp_scope.h:30
cpp_typecheck_resolvet
Definition: cpp_typecheck_resolve.h:23
messaget::eom
static eomt eom
Definition: message.h:297
messaget::error
mstreamt & error() const
Definition: message.h:399
cpp_usingt
Definition: cpp_using.h:17
cpp_idt::id_classt::TEMPLATE_PARAMETER
@ TEMPLATE_PARAMETER
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_usingt::get_namespace
bool get_namespace() const
Definition: cpp_using.h:34
source_location.h
cpp_typecheck.h
cpp_namet::is_qualified
bool is_qualified() const
Definition: cpp_name.h:109
cpp_scopet::RECURSIVE
@ RECURSIVE
Definition: cpp_scope.h:30
cpp_template_args_non_tct
Definition: cpp_template_args.h:44
cpp_namet::source_location
const source_locationt & source_location() const
Definition: cpp_name.h:73
cpp_save_scopet::restore
void restore()
Definition: cpp_scopes.h:141
cpp_typecheck_resolvet::resolve_scope
cpp_scopet & resolve_scope(const cpp_namet &cpp_name, irep_idt &base_name, cpp_template_args_non_tct &template_args)
Definition: cpp_typecheck_resolve.cpp:856
cpp_idt::id_classt::NAMESPACE
@ NAMESPACE
cpp_usingt::name
cpp_namet & name()
Definition: cpp_using.h:24
cpp_scopet::insert
cpp_idt & insert(const irep_idt &_base_name)
Definition: cpp_scope.h:52