cprover
ansi_c_declaration.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 "ansi_c_declaration.h"
13 
14 #include <ostream>
15 #include <cassert>
16 
17 #include <util/config.h>
18 #include <util/std_types.h>
19 #include <util/invariant.h>
20 
21 #include "merged_type.h"
22 
24 {
25  typet *p=static_cast<typet *>(&src);
26 
27  // walk down subtype until we hit typedef_type, symbol or "abstract"
28  while(true)
29  {
30  typet &t=*p;
31 
32  if(t.id() == ID_typedef_type || t.id() == ID_symbol)
33  {
34  set_base_name(t.get(ID_C_base_name));
36  t.make_nil();
37  break;
38  }
39  else if(t.id().empty() ||
40  t.is_nil())
41  {
43  }
44  else if(t.id()==ID_abstract)
45  {
46  t.make_nil();
47  break;
48  }
49  else if(t.id()==ID_merged_type)
50  {
51  // we always walk down the _last_ member of a merged type
52  auto &merged_type = to_merged_type(t);
53  p = &merged_type.last_type();
54  }
55  else
56  p=&t.subtype();
57  }
58 
59  type()=static_cast<const typet &>(src);
60  value().make_nil();
61 }
62 
63 void ansi_c_declarationt::output(std::ostream &out) const
64 {
65  out << "Flags:";
66  if(get_is_typedef())
67  out << " is_typedef";
69  out << " is_enum_constant";
70  if(get_is_static())
71  out << " is_static";
72  if(get_is_parameter())
73  out << " is_parameter";
74  if(get_is_global())
75  out << " is_global";
76  if(get_is_register())
77  out << " is_register";
79  out << " is_thread_local";
80  if(get_is_inline())
81  out << " is_inline";
82  if(get_is_extern())
83  out << " is_extern";
85  out << " is_static_assert";
86  out << "\n";
87 
88  out << "Type: " << type().pretty() << "\n";
89 
90  for(const auto &declarator : declarators())
91  out << "Declarator: " << declarator.pretty() << "\n";
92 }
93 
95  const ansi_c_declaratort &declarator) const
96 {
97  typet result=declarator.type();
98  typet *p=&result;
99 
100  // this gets types that are still raw parse trees
101  while(p->is_not_nil())
102  {
103  if(p->id()==ID_frontend_pointer || p->id()==ID_array ||
104  p->id()==ID_vector || p->id()==ID_c_bit_field ||
105  p->id()==ID_block_pointer || p->id()==ID_code)
106  p=&p->subtype();
107  else if(p->id()==ID_merged_type)
108  {
109  // we always go down on the right-most subtype
110  auto &merged_type = to_merged_type(*p);
111  p = &merged_type.last_type();
112  }
113  else
114  UNREACHABLE;
115  }
116 
117  *p=type();
118 
119  // retain typedef for dump-c
120  if(get_is_typedef())
121  result.set(ID_C_typedef, declarator.get_name());
122 
123  return result;
124 }
125 
127  const ansi_c_declaratort &declarator,
128  symbolt &symbol) const
129 {
130  symbol.clear();
131  symbol.value=declarator.value();
132  symbol.type=full_type(declarator);
133  symbol.name=declarator.get_name();
134  symbol.pretty_name=symbol.name;
136  symbol.is_type=get_is_typedef();
138  symbol.is_extern=get_is_extern();
141  symbol.is_weak=get_is_weak();
142 
143  // is it a function?
144 
145  if(symbol.type.id()==ID_code && !symbol.is_type)
146  {
147  symbol.is_static_lifetime=false;
148  symbol.is_thread_local=false;
149 
150  symbol.is_file_local=get_is_static();
151 
152  if(get_is_inline())
153  symbol.type.set(ID_C_inlined, true);
154 
155  if(
159  {
160  // GCC extern inline cleanup, to enable remove_internal_symbols
161  // do its full job
162  // https://gcc.gnu.org/ml/gcc/2006-11/msg00006.html
163  // __attribute__((__gnu_inline__))
164  if(get_is_inline())
165  {
166  if(get_is_static()) // C99 and above
167  symbol.is_extern=false;
168  else if(get_is_extern()) // traditional GCC
169  symbol.is_file_local=true;
170  }
171 
172  // GCC __attribute__((__used__)) - do not treat those as file-local
173  if(get_is_used())
174  symbol.is_file_local = false;
175  }
176  }
177  else // non-function
178  {
179  symbol.is_static_lifetime=
180  !symbol.is_macro &&
181  !symbol.is_type &&
182  (get_is_global() || get_is_static());
183 
184  symbol.is_thread_local=
185  (!symbol.is_static_lifetime && !get_is_extern()) ||
187 
188  symbol.is_file_local=
189  symbol.is_macro ||
190  (!get_is_global() && !get_is_extern()) ||
191  (get_is_global() && get_is_static() && !get_is_used()) ||
192  symbol.is_parameter;
193  }
194 }
bool get_is_static_assert() const
The type of an expression, extends irept.
Definition: type.h:27
irep_idt name
The unique identifier.
Definition: symbol.h:40
bool get_is_static() const
struct configt::ansi_ct ansi_c
bool get_is_register() const
bool is_nil() const
Definition: irep.h:172
bool is_not_nil() const
Definition: irep.h:173
bool is_thread_local
Definition: symbol.h:65
std::string pretty(unsigned indent=0, unsigned max_indent=0) const
Definition: irep.cpp:640
bool get_is_typedef() const
void clear()
Zero initialise a symbol object.
Definition: symbol.h:75
void build(irept &src)
exprt value
Initial value of symbol.
Definition: symbol.h:34
void to_symbol(const ansi_c_declaratort &, symbolt &symbol) const
irep_idt pretty_name
Language-specific display name.
Definition: symbol.h:52
typet & type()
Return the type of the expression.
Definition: expr.h:68
Symbol table entry.
Definition: symbol.h:27
configt config
Definition: config.cpp:24
typet full_type(const ansi_c_declaratort &) const
bool is_static_lifetime
Definition: symbol.h:65
const irep_idt & id() const
Definition: irep.h:259
const declaratorst & declarators() const
irep_idt get_base_name() const
void set_base_name(const irep_idt &base_name)
flavourt mode
Definition: config.h:115
bool is_parameter
Definition: symbol.h:66
const merged_typet & to_merged_type(const typet &type)
conversion to merged_typet
Definition: merged_type.h:29
bool get_is_enum_constant() const
const irep_idt & get(const irep_namet &name) const
Definition: irep.cpp:212
irep_idt get_name() const
Base class for tree-like data structures with sharing.
Definition: irep.h:156
const source_locationt & source_location() const
Definition: type.h:62
void output(std::ostream &) const
bool is_extern
Definition: symbol.h:66
typet type
Type of symbol.
Definition: symbol.h:31
source_locationt location
Source code location of definition of symbol.
Definition: symbol.h:37
Pre-defined types.
bool get_is_parameter() const
irep_idt base_name
Base (non-scoped) name.
Definition: symbol.h:46
const source_locationt & source_location() const
Definition: expr.h:228
#define UNREACHABLE
This should be used to mark dead code.
Definition: invariant.h:478
bool is_file_local
Definition: symbol.h:66
ANSI-CC Language Type Checking.
void make_nil()
Definition: irep.h:315
bool is_weak
Definition: symbol.h:66
source_locationt & add_source_location()
Definition: expr.h:233
bool is_type
Definition: symbol.h:61
const typet & subtype() const
Definition: type.h:38
bool empty() const
Definition: dstring.h:75
bool is_macro
Definition: symbol.h:61
void set(const irep_namet &name, const irep_idt &value)
Definition: irep.h:286
bool get_is_thread_local() const
const ansi_c_declaratort & declarator() const