cprover
c_typecheck_argc_argv.cpp
Go to the documentation of this file.
1 /*******************************************************************\
2 
3 Module: ANSI-C Conversion / Type Checking
4 
5 Author: Daniel Kroening, kroening@kroening.com
6 
7 \*******************************************************************/
8 
11 
12 #include "c_typecheck_base.h"
13 
14 #include <util/arith_tools.h>
15 
16 void c_typecheck_baset::add_argc_argv(const symbolt &main_symbol)
17 {
18  const code_typet::parameterst &parameters=
19  to_code_type(main_symbol.type).parameters();
20 
21  if(parameters.empty())
22  return;
23 
24  if(parameters.size()!=2 &&
25  parameters.size()!=3)
26  {
27  error().source_location=main_symbol.location;
28  error() << "main expected to have no or two or three parameters" << eom;
29  throw 0;
30  }
31 
32  symbolt *argc_new_symbol;
33 
34  const exprt &op0=static_cast<const exprt &>(parameters[0]);
35  const exprt &op1=static_cast<const exprt &>(parameters[1]);
36 
37  {
38  symbolt argc_symbol;
39 
40  argc_symbol.base_name="argc";
41  argc_symbol.name="argc'";
42  argc_symbol.type=op0.type();
43  argc_symbol.is_static_lifetime=true;
44  argc_symbol.is_lvalue=true;
45 
46  if(argc_symbol.type.id()!=ID_signedbv &&
47  argc_symbol.type.id()!=ID_unsignedbv)
48  {
49  error().source_location=main_symbol.location;
50  error() << "argc argument expected to be integer type, but got `"
51  << to_string(argc_symbol.type) << "'" << eom;
52  throw 0;
53  }
54 
55  move_symbol(argc_symbol, argc_new_symbol);
56  }
57 
58  {
59  if(op1.type().id()!=ID_pointer ||
60  op1.type().subtype().id()!=ID_pointer)
61  {
62  error().source_location=main_symbol.location;
63  error() << "argv argument expected to be pointer-to-pointer type, "
64  "but got `"
65  << to_string(op1.type()) << '\'' << eom;
66  throw 0;
67  }
68 
69  // we make the type of this thing an array of pointers
70  typet argv_type=array_typet();
71  argv_type.subtype()=op1.type().subtype();
72 
73  // need to add one to the size -- the array is terminated
74  // with NULL
75  exprt one_expr=from_integer(1, argc_new_symbol->type);
76 
77  exprt size_expr(ID_plus, argc_new_symbol->type);
78  size_expr.copy_to_operands(argc_new_symbol->symbol_expr(), one_expr);
79  argv_type.add(ID_size).swap(size_expr);
80 
81  symbolt argv_symbol;
82 
83  argv_symbol.base_name="argv'";
84  argv_symbol.name="argv'";
85  argv_symbol.type=argv_type;
86  argv_symbol.is_static_lifetime=true;
87  argv_symbol.is_lvalue=true;
88 
89  symbolt *argv_new_symbol;
90  move_symbol(argv_symbol, argv_new_symbol);
91  }
92 
93  if(parameters.size()==3)
94  {
95  symbolt envp_symbol;
96  envp_symbol.base_name="envp'";
97  envp_symbol.name="envp'";
98  envp_symbol.type=(static_cast<const exprt&>(parameters[2])).type();
99  envp_symbol.is_static_lifetime=true;
100 
101  symbolt envp_size_symbol, *envp_new_size_symbol;
102  envp_size_symbol.base_name="envp_size";
103  envp_size_symbol.name="envp_size'";
104  envp_size_symbol.type=op0.type(); // same type as argc!
105  envp_size_symbol.is_static_lifetime=true;
106  move_symbol(envp_size_symbol, envp_new_size_symbol);
107 
108  if(envp_symbol.type.id()!=ID_pointer)
109  {
110  error().source_location=main_symbol.location;
111  error() << "envp argument expected to be pointer type, but got `"
112  << to_string(envp_symbol.type) << '\'' << eom;
113  throw 0;
114  }
115 
116  exprt size_expr = envp_new_size_symbol->symbol_expr();
117 
118  envp_symbol.type.id(ID_array);
119  envp_symbol.type.add(ID_size).swap(size_expr);
120 
121  symbolt *envp_new_symbol;
122  move_symbol(envp_symbol, envp_new_symbol);
123  }
124 }
The type of an expression.
Definition: type.h:20
irep_idt name
The unique identifier.
Definition: symbol.h:46
void move_symbol(symbolt &symbol, symbolt *&new_symbol)
void copy_to_operands(const exprt &expr)
Definition: expr.cpp:61
std::vector< parametert > parameterst
Definition: std_types.h:829
typet & type()
Definition: expr.h:60
Symbol table entry.This is a symbol in the symbol table, stored in an object of type symbol_tablet...
Definition: symbol.h:33
static mstreamt & eom(mstreamt &m)
Definition: message.h:193
virtual std::string to_string(const exprt &expr)
bool is_static_lifetime
Definition: symbol.h:70
const irep_idt & id() const
Definition: irep.h:189
class symbol_exprt symbol_expr() const
produces a symbol_exprt for a symbol
Definition: symbol.cpp:191
ANSI-C Language Type Checking.
source_locationt source_location
Definition: message.h:175
typet type
Type of symbol.
Definition: symbol.h:37
source_locationt location
Source code location of definition of symbol.
Definition: symbol.h:43
Base class for all expressions.
Definition: expr.h:46
void add_argc_argv(const symbolt &main_symbol)
const parameterst & parameters() const
Definition: std_types.h:841
irep_idt base_name
Base (non-scoped) name.
Definition: symbol.h:52
irept & add(const irep_namet &name)
Definition: irep.cpp:306
const code_typet & to_code_type(const typet &type)
Cast a generic typet to a code_typet.
Definition: std_types.h:884
void swap(irept &irep)
Definition: irep.h:231
mstreamt & error()
Definition: message.h:223
arrays with given size
Definition: std_types.h:901
const typet & subtype() const
Definition: type.h:31
constant_exprt from_integer(const mp_integer &int_value, const typet &type)
bool is_lvalue
Definition: symbol.h:71