cprover
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/std_types.h>
15 #include <util/std_expr.h>
16 
18  const exprt &struct_union,
19  const struct_union_typet::componentt &component,
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(result.get_bool(ID_C_constant) ||
33  type.get_bool(ID_C_constant) ||
34  struct_union.type().get_bool(ID_C_constant))
35  result.set(ID_C_constant, true);
36 
37  return result;
38 }
39 
41  const exprt &struct_union,
42  const irep_idt &component_name,
43  const namespacet &ns)
44 {
45  const struct_union_typet &struct_union_type=
46  to_struct_union_type(ns.follow(struct_union.type()));
47 
48  const struct_union_typet::componentst &components=
49  struct_union_type.components();
50 
51  for(const auto &comp : components)
52  {
53  const typet &type=ns.follow(comp.type());
54 
55  if(comp.get_name()==component_name)
56  {
57  return make_member_expr(struct_union, comp, ns);
58  }
59  else if(comp.get_anonymous() &&
60  (type.id()==ID_struct || type.id()==ID_union))
61  {
62  exprt tmp=make_member_expr(struct_union, comp, ns);
63  exprt result=get_component_rec(tmp, component_name, ns);
64  if(result.is_not_nil())
65  return result;
66  }
67  }
68 
69  return nil_exprt();
70 }
71 
73  const typet &type,
74  const irep_idt &component_name,
75  const namespacet &ns)
76 {
77  const struct_union_typet &struct_union_type=
78  to_struct_union_type(ns.follow(type));
79 
80  const struct_union_typet::componentst &components=
81  struct_union_type.components();
82 
83  for(const auto &comp : components)
84  {
85  if(comp.get_name()==component_name)
86  {
87  return true;
88  }
89  else if(comp.get_anonymous())
90  {
91  if(has_component_rec(comp.type(), component_name, ns))
92  return true;
93  }
94  }
95 
96  return false;
97 }
const irep_idt & get_name() const
Definition: std_types.h:182
The type of an expression.
Definition: type.h:22
bool is_not_nil() const
Definition: irep.h:103
static exprt make_member_expr(const exprt &struct_union, const struct_union_typet::componentt &component, const namespacet &ns)
std::vector< componentt > componentst
Definition: std_types.h:243
const componentst & components() const
Definition: std_types.h:245
bool has_component_rec(const typet &type, const irep_idt &component_name, const namespacet &ns)
typet & type()
Definition: expr.h:56
bool get_bool(const irep_namet &name) const
Definition: irep.cpp:240
exprt get_component_rec(const exprt &struct_union, const irep_idt &component_name, const namespacet &ns)
Extract member of struct or union.
Definition: std_expr.h:3871
const irep_idt & id() const
Definition: irep.h:189
The NIL expression.
Definition: std_expr.h:4510
API to expression classes.
TO_BE_DOCUMENTED.
Definition: namespace.h:74
const typet & follow(const typet &) const
Definition: namespace.cpp:55
API to type classes.
Base type of C structs and unions, and C++ classes.
Definition: std_types.h:162
Base class for all expressions.
Definition: expr.h:42
const struct_union_typet & to_struct_union_type(const typet &type)
Cast a generic typet to a struct_union_typet.
Definition: std_types.h:280
void set(const irep_namet &name, const irep_idt &value)
Definition: irep.h:214
C Language Type Checking.