string_constantt::string_constantt(const irep_idt &_value): exprt(ID_string_constant) { set_value(_value); type()=array_typet(); type().subtype()=char_type(); }
array_exprt string_constantt::to_array_expr() const { const std::string &str=get_string(ID_value); unsigned string_size=str.size()+1; // we add the zero const typet &char_type=type().subtype(); bool char_is_unsigned=char_type.id()==ID_unsignedbv; exprt size=from_integer(string_size, index_type()); array_exprt dest; dest.type()=array_typet(); dest.type().subtype()=char_type; dest.type().set(ID_size, size); dest.operands().resize(string_size); exprt::operandst::iterator it=dest.operands().begin(); for(unsigned i=0; i<string_size; i++, it++) { // Are we at the end? Do implicit zero. int ch=i==string_size-1?0:str[i]; if(char_is_unsigned) ch=(unsigned char)ch; exprt &op=*it; op=from_integer(ch, char_type); if(ch>=32 && ch<=126) { std::string ch_str="'"; if(ch=='\'' || ch=='\\') ch_str+='\\'; ch_str+=(char)ch; ch_str+="'"; op.set(ID_C_cformat, ch_str); } } return dest; }
array_exprt string_constantt::to_array_expr() const { const std::string &str=get_string(ID_value); unsigned string_size=str.size()+1; // zero const typet &char_type=type().subtype(); bool char_is_unsigned=char_type.id()==ID_unsignedbv; exprt size=from_integer(string_size, index_type()); array_exprt dest; dest.type()=array_typet(); dest.type().subtype()=char_type; dest.type().set(ID_size, size); dest.operands().resize(string_size); exprt::operandst::iterator it=dest.operands().begin(); for(unsigned i=0; i<string_size; i++, it++) { int ch=i==string_size-1?0:str[i]; if(char_is_unsigned) ch=(unsigned char)ch; exprt &op=*it; op=from_integer(ch, char_type); if(ch>=32 && ch<=126) { char ch_str[2]; ch_str[0]=ch; ch_str[1]=0; op.set(ID_C_cformat, "'"+std::string(ch_str)+"'"); } } return dest; }
array_typet jsa_query_type(const exprt &size) { return array_typet(jsa_query_instruction_type(), size); }
array_typet jsa_invariant_type(const exprt & size) { return array_typet(jsa_invariant_instruction_type(), size); }
array_typet jsa_predicate_type(const exprt &size) { return array_typet(jsa_predicate_instruction_type(), size); }
void c_typecheck_baset::add_argc_argv(const symbolt &main_symbol) { const irept &arguments= main_symbol.type.find(ID_arguments); if(arguments.get_sub().size()==0) return; if(arguments.get_sub().size()!=2 && arguments.get_sub().size()!=3) { err_location(main_symbol.location); throw "main expected to have no or two or three arguments"; } symbolt *argc_new_symbol; const exprt &op0=static_cast<const exprt &>(arguments.get_sub()[0]); const exprt &op1=static_cast<const exprt &>(arguments.get_sub()[1]); { symbolt argc_symbol; argc_symbol.base_name="argc"; argc_symbol.name="c::argc'"; argc_symbol.type=op0.type(); argc_symbol.is_static_lifetime=true; argc_symbol.is_lvalue=true; if(argc_symbol.type.id()!=ID_signedbv && argc_symbol.type.id()!=ID_unsignedbv) { err_location(main_symbol.location); str << "argc argument expected to be integer type, but got `" << to_string(argc_symbol.type) << "'"; throw 0; } move_symbol(argc_symbol, argc_new_symbol); } { if(op1.type().id()!=ID_pointer || op1.type().subtype().id()!=ID_pointer) { err_location(main_symbol.location); str << "argv argument expected to be pointer-to-pointer type, " "but got `" << to_string(op1.type()) << "'"; throw 0; } // we make the type of this thing an array of pointers typet argv_type=array_typet(); argv_type.subtype()=op1.type().subtype(); // need to add one to the size -- the array is terminated // with NULL exprt one_expr=from_integer(1, argc_new_symbol->type); exprt size_expr(ID_plus, argc_new_symbol->type); size_expr.copy_to_operands(symbol_expr(*argc_new_symbol), one_expr); argv_type.add(ID_size).swap(size_expr); symbolt argv_symbol; argv_symbol.base_name="argv'"; argv_symbol.name="c::argv'"; argv_symbol.type=argv_type; argv_symbol.is_static_lifetime=true; argv_symbol.is_lvalue=true; symbolt *argv_new_symbol; move_symbol(argv_symbol, argv_new_symbol); } if(arguments.get_sub().size()==3) { symbolt envp_symbol; envp_symbol.base_name="envp'"; envp_symbol.name="c::envp'"; envp_symbol.type=(static_cast<const exprt&>(arguments.get_sub()[2])).type(); envp_symbol.is_static_lifetime=true; symbolt envp_size_symbol, *envp_new_size_symbol; envp_size_symbol.base_name="envp_size"; envp_size_symbol.name="c::envp_size'"; envp_size_symbol.type=op0.type(); // same type as argc! envp_size_symbol.is_static_lifetime=true; move_symbol(envp_size_symbol, envp_new_size_symbol); if(envp_symbol.type.id()!=ID_pointer) { err_location(main_symbol.location); str << "envp argument expected to be pointer type, but got `" << to_string(envp_symbol.type) << "'"; throw 0; } exprt size_expr = symbol_expr(*envp_new_size_symbol); envp_symbol.type.id(ID_array); envp_symbol.type.add(ID_size).swap(size_expr); symbolt *envp_new_symbol; move_symbol(envp_symbol, envp_new_symbol); } }
typet get_type(const format_tokent &token) { switch(token.type) { case format_tokent::INT: switch(token.length_modifier) { case format_tokent::LEN_h: if(token.representation==format_tokent::SIGNED_DEC) return signed_char_type(); else return unsigned_char_type(); case format_tokent::LEN_hh: if(token.representation==format_tokent::SIGNED_DEC) return signed_short_int_type(); else return unsigned_short_int_type(); case format_tokent::LEN_l: if(token.representation==format_tokent::SIGNED_DEC) return signed_long_int_type(); else return unsigned_long_int_type(); case format_tokent::LEN_ll: if(token.representation==format_tokent::SIGNED_DEC) return signed_long_long_int_type(); else return unsigned_long_long_int_type(); default: if(token.representation==format_tokent::SIGNED_DEC) return signed_int_type(); else return unsigned_int_type(); } case format_tokent::FLOAT: switch(token.length_modifier) { case format_tokent::LEN_l: return double_type(); case format_tokent::LEN_L: return long_double_type(); default: return float_type(); } case format_tokent::CHAR: switch(token.length_modifier) { case format_tokent::LEN_l: return wchar_t_type(); default: return char_type(); } case format_tokent::POINTER: return pointer_type(void_type()); case format_tokent::STRING: switch(token.length_modifier) { case format_tokent::LEN_l: return array_typet(wchar_t_type(), nil_exprt()); default: return array_typet(char_type(), nil_exprt()); } default: return nil_typet(); } }