CBMC
cpp_name.h
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module:
4 
5 Author: Daniel Kroening, kroening@cs.cmu.edu
6 
7 \*******************************************************************/
8 
9 
10 #ifndef CPROVER_CPP_CPP_NAME_H
11 #define CPROVER_CPP_CPP_NAME_H
12 
13 #include <util/expr.h>
14 #include <util/invariant.h>
15 
16 class cpp_namet:public irept
17 {
18 public:
19  // the subs are one of the following:
20  // ID_name (see namet)
21  // ID_operator
22  // ID_template_args
23  // ::
24  // ~
25 
26  class namet:public irept
27  {
28  public:
29  namet():irept(ID_name)
30  {
31  }
32 
33  explicit namet(const irep_idt &base_name):irept(ID_name)
34  {
35  set(ID_identifier, base_name);
36  }
37 
39  const irep_idt &_base_name,
40  const source_locationt &_source_location):irept(ID_name)
41  {
42  set(ID_identifier, _base_name);
43  add_source_location()=_source_location;
44  }
45 
47  {
48  return static_cast<source_locationt &>(add(ID_C_source_location));
49  }
50 
52  {
53  return static_cast<const source_locationt &>(find(ID_C_source_location));
54  }
55  };
56 
57  cpp_namet():irept(ID_cpp_name)
58  {
59  }
60 
61  explicit cpp_namet(const irep_idt &base_name):irept(ID_cpp_name)
62  {
63  get_sub().push_back(namet(base_name));
64  }
65 
67  const irep_idt &_base_name,
68  const source_locationt &_source_location):irept(ID_cpp_name)
69  {
70  get_sub().push_back(namet(_base_name, _source_location));
71  }
72 
74  {
75  if(get_sub().empty())
76  return source_locationt::nil();
77  else
78  return static_cast<const source_locationt &>(
79  get_sub().front().find(ID_C_source_location));
80  }
81 
82  // void convert(std::string &identifier, std::string &base_name) const;
83  irep_idt get_base_name() const;
84 
85  // one of three:
86  // 'identifier'
87  // 'operator X'
88  // '~identifier'
89  bool is_simple_name() const
90  {
91  const subt &sub=get_sub();
92  return (sub.size()==1 && sub.front().id()==ID_name) ||
93  (sub.size()==2 && sub.front().id()==ID_operator) ||
94  (sub.size()==2 && sub[0].id()=="~" && sub[1].id()==ID_name);
95  }
96 
97  bool is_operator() const
98  {
99  if(get_sub().empty())
100  return false;
101  return get_sub().front().id()==ID_operator;
102  }
103 
104  bool is_typename() const
105  {
106  return get_bool(ID_typename);
107  }
108 
109  bool is_qualified() const
110  {
111  for(const auto &irep : get_sub())
112  {
113  if(irep.id() == "::")
114  return true;
115  }
116  return false;
117  }
118 
119  bool is_destructor() const
120  {
121  return get_sub().size()>=1 && get_sub().front().id()=="~";
122  }
123 
124  bool has_template_args() const
125  {
126  for(const auto &irep : get_sub())
127  {
128  if(irep.id() == ID_template_args)
129  return true;
130  }
131 
132  return false;
133  }
134 
135  std::string to_string() const;
136 
137  const exprt &as_expr() const
138  {
139  return static_cast<const exprt &>(static_cast<const irept &>(*this));
140  }
141 
142  const typet &as_type() const
143  {
144  return static_cast<const typet &>(static_cast<const irept &>(*this));
145  }
146 };
147 
148 inline cpp_namet &to_cpp_name(irept &cpp_name)
149 {
150  PRECONDITION(cpp_name.id() == ID_cpp_name);
151  return static_cast<cpp_namet &>(cpp_name);
152 }
153 
154 inline const cpp_namet &to_cpp_name(const irept &cpp_name)
155 {
156  PRECONDITION(cpp_name.id() == ID_cpp_name);
157  return static_cast<const cpp_namet &>(cpp_name);
158 }
159 
160 #endif // CPROVER_CPP_CPP_NAME_H
namet(const irep_idt &base_name)
Definition: cpp_name.h:33
namet(const irep_idt &_base_name, const source_locationt &_source_location)
Definition: cpp_name.h:38
const source_locationt & source_location() const
Definition: cpp_name.h:51
source_locationt & add_source_location()
Definition: cpp_name.h:46
const typet & as_type() const
Definition: cpp_name.h:142
bool is_operator() const
Definition: cpp_name.h:97
cpp_namet()
Definition: cpp_name.h:57
bool is_qualified() const
Definition: cpp_name.h:109
irep_idt get_base_name() const
Definition: cpp_name.cpp:14
const source_locationt & source_location() const
Definition: cpp_name.h:73
bool has_template_args() const
Definition: cpp_name.h:124
bool is_simple_name() const
Definition: cpp_name.h:89
bool is_typename() const
Definition: cpp_name.h:104
std::string to_string() const
Definition: cpp_name.cpp:73
bool is_destructor() const
Definition: cpp_name.h:119
cpp_namet(const irep_idt &base_name)
Definition: cpp_name.h:61
cpp_namet(const irep_idt &_base_name, const source_locationt &_source_location)
Definition: cpp_name.h:66
const exprt & as_expr() const
Definition: cpp_name.h:137
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:38
Base class for all expressions.
Definition: expr.h:56
There are a large number of kinds of tree structured or tree-like data in CPROVER.
Definition: irep.h:364
bool get_bool(const irep_idt &name) const
Definition: irep.cpp:57
subt & get_sub()
Definition: irep.h:448
const irept & find(const irep_idt &name) const
Definition: irep.cpp:93
void set(const irep_idt &name, const irep_idt &value)
Definition: irep.h:412
const irep_idt & id() const
Definition: irep.h:388
irept & add(const irep_idt &name)
Definition: irep.cpp:103
static const source_locationt & nil()
The type of an expression, extends irept.
Definition: type.h:29
cpp_namet & to_cpp_name(irept &cpp_name)
Definition: cpp_name.h:148
#define PRECONDITION(CONDITION)
Definition: invariant.h:463