CBMC
anonymous_member.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: ANSI-C Language Type Checking
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
11 
12 #include "anonymous_member.h"
13 
14 #include <util/namespace.h>
15 #include <util/std_expr.h>
16 
18  const exprt &struct_union,
20  const namespacet &ns)
21 {
22  member_exprt result(
23  struct_union, component.get_name(), component.type());
24 
25  if(struct_union.get_bool(ID_C_lvalue))
26  result.set(ID_C_lvalue, true);
27 
28  // todo: should to typedef chains properly
29  const typet &type=
30  ns.follow(struct_union.type());
31 
32  if(
33  type.get_bool(ID_C_constant) || struct_union.type().get_bool(ID_C_constant))
34  {
35  result.type().set(ID_C_constant, true);
36  }
37 
38  return result;
39 }
40 
42  const exprt &struct_union,
43  const irep_idt &component_name,
44  const namespacet &ns)
45 {
46  const struct_union_typet &struct_union_type=
47  to_struct_union_type(ns.follow(struct_union.type()));
48 
49  const struct_union_typet::componentst &components=
50  struct_union_type.components();
51 
52  for(const auto &comp : components)
53  {
54  const typet &type = comp.type();
55 
56  if(comp.get_name()==component_name)
57  {
58  return std::move(make_member_expr(struct_union, comp, ns));
59  }
60  else if(
61  comp.get_anonymous() &&
62  (type.id() == ID_struct_tag || type.id() == ID_union_tag))
63  {
64  const member_exprt tmp = make_member_expr(struct_union, comp, ns);
65  exprt result=get_component_rec(tmp, component_name, ns);
66  if(result.is_not_nil())
67  return result;
68  }
69  }
70 
71  return nil_exprt();
72 }
73 
75  const typet &type,
76  const irep_idt &component_name,
77  const namespacet &ns)
78 {
79  const struct_union_typet &struct_union_type=
80  to_struct_union_type(ns.follow(type));
81 
82  const struct_union_typet::componentst &components=
83  struct_union_type.components();
84 
85  for(const auto &comp : components)
86  {
87  if(comp.get_name()==component_name)
88  {
89  return true;
90  }
91  else if(
92  comp.get_anonymous() &&
93  (comp.type().id() == ID_struct_tag || comp.type().id() == ID_union_tag))
94  {
95  if(has_component_rec(comp.type(), component_name, ns))
96  return true;
97  }
98  }
99 
100  return false;
101 }
struct_union_typet::components
const componentst & components() const
Definition: std_types.h:147
dstringt
dstringt has one field, an unsigned integer no which is an index into a static table of strings.
Definition: dstring.h:36
get_component_rec
exprt get_component_rec(const exprt &struct_union, const irep_idt &component_name, const namespacet &ns)
Definition: anonymous_member.cpp:41
to_struct_union_type
const struct_union_typet & to_struct_union_type(const typet &type)
Cast a typet to a struct_union_typet.
Definition: std_types.h:214
typet
The type of an expression, extends irept.
Definition: type.h:28
struct_union_typet
Base type for structs and unions.
Definition: std_types.h:61
exprt
Base class for all expressions.
Definition: expr.h:55
struct_union_typet::componentst
std::vector< componentt > componentst
Definition: std_types.h:140
component
auto component(T &struct_expr, const irep_idt &name, const namespacet &ns) -> decltype(struct_expr.op0())
Definition: std_expr.cpp:76
namespace.h
namespacet
A namespacet is essentially one or two symbol tables bound together, to allow for symbol lookups in t...
Definition: namespace.h:90
exprt::type
typet & type()
Return the type of the expression.
Definition: expr.h:84
has_component_rec
bool has_component_rec(const typet &type, const irep_idt &component_name, const namespacet &ns)
Definition: anonymous_member.cpp:74
irept::is_not_nil
bool is_not_nil() const
Definition: irep.h:380
irept::set
void set(const irep_idt &name, const irep_idt &value)
Definition: irep.h:420
nil_exprt
The NIL expression.
Definition: std_expr.h:3025
make_member_expr
static member_exprt make_member_expr(const exprt &struct_union, const struct_union_typet::componentt &component, const namespacet &ns)
Definition: anonymous_member.cpp:17
irept::id
const irep_idt & id() const
Definition: irep.h:396
member_exprt
Extract member of struct or union.
Definition: std_expr.h:2793
struct_union_typet::componentt
Definition: std_types.h:68
namespace_baset::follow
const typet & follow(const typet &) const
Resolve type symbol to the type it points to.
Definition: namespace.cpp:49
anonymous_member.h
std_expr.h
irept::get_bool
bool get_bool(const irep_idt &name) const
Definition: irep.cpp:58