constant_exprt from_rational(const rationalt &a) { std::string d=integer2string(a.get_numerator()); if(a.get_denominator()!=1) d+="/"+integer2string(a.get_denominator()); constant_exprt result; result.type()=rational_typet(); result.set_value(d); return result; }
void c_typecheck_baset::typecheck_symbol_type(typet &type) { const irep_idt &identifier= to_symbol_type(type).get_identifier(); symbol_tablet::symbolst::const_iterator s_it= symbol_table.symbols.find(identifier); if(s_it==symbol_table.symbols.end()) { error().source_location=type.source_location(); error() << "type symbol `" << identifier << "' not found" << eom; throw 0; } const symbolt &symbol=s_it->second; if(!symbol.is_type) { error().source_location=type.source_location(); error() << "expected type symbol" << eom; throw 0; } if(symbol.is_macro) { // overwrite, but preserve (add) any qualifiers and other flags c_qualifierst c_qualifiers(type); bool is_packed=type.get_bool(ID_C_packed); irept alignment=type.find(ID_C_alignment); c_qualifiers+=c_qualifierst(symbol.type); type=symbol.type; c_qualifiers.write(type); if(is_packed) type.set(ID_C_packed, true); if(alignment.is_not_nil()) type.set(ID_C_alignment, alignment); } // CPROVER extensions if(symbol.base_name=="__CPROVER_rational") { type=rational_typet(); } else if(symbol.base_name=="__CPROVER_integer") { type=integer_typet(); } }
void c_typecastt::implicit_typecast_arithmetic( exprt &expr, c_typet c_type) { typet new_type; const typet &expr_type=ns.follow(expr.type()); switch(c_type) { case PTR: if(expr_type.id()==ID_array) { new_type.id(ID_pointer); new_type.subtype()=expr_type.subtype(); break; } return; case BOOL: assert(false); // should always be promoted to int case CHAR: assert(false); // should always be promoted to int case UCHAR: assert(false); // should always be promoted to int case SHORT: assert(false); // should always be promoted to int case USHORT: assert(false); // should always be promoted to int case INT: new_type=signed_int_type(); break; case UINT: new_type=unsigned_int_type(); break; case LONG: new_type=signed_long_int_type(); break; case ULONG: new_type=unsigned_long_int_type(); break; case LONGLONG: new_type=signed_long_long_int_type(); break; case ULONGLONG: new_type=unsigned_long_long_int_type(); break; case SINGLE: new_type=float_type(); break; case DOUBLE: new_type=double_type(); break; case LONGDOUBLE: new_type=long_double_type(); break; case FLOAT128: new_type=ieee_float_spect::quadruple_precision().to_type(); break; case RATIONAL: new_type=rational_typet(); break; case REAL: new_type=real_typet(); break; case INTEGER: new_type=integer_typet(); break; case COMPLEX: return; // do nothing default: return; } if(new_type!=expr_type) do_typecast(expr, new_type); }
bool smt1_dect::string_to_expr_z3( const typet &type, const std::string &value, exprt &e) const { if(value.substr(0,2)=="bv") { std::string v=value.substr(2, value.find('[')-2); size_t p = value.find('[')+1; std::string w=value.substr(p, value.find(']')-p); std::string binary=integer2binary(string2integer(v,10), string2integer(w,10).to_ulong()); if(type.id()==ID_struct) { e=binary2struct(to_struct_type(type), binary); } else if(type.id()==ID_union) { e=binary2union(to_union_type(type), binary); } else { constant_exprt c(type); c.set_value(binary); e=c; } return true; } else if(value.substr(0,6)=="(const") // const arrays { std::string av = value.substr(7, value.length()-8); exprt ae; if(!string_to_expr_z3(type.subtype(), av, ae)) return false; array_of_exprt ao; ao.type() = typet("array"); ao.type().subtype()=ae.type(); ao.what() = ae; e = ao; return true; } else if(value.substr(0,6)=="(store") { size_t p1=value.rfind(' ')+1; size_t p2=value.rfind(' ', p1-2)+1; assert(p1!=std::string::npos && p2!=std::string::npos); std::string elem = value.substr(p1, value.size()-p1-1); std::string inx = value.substr(p2, p1-p2-1); std::string array = value.substr(7, p2-8); exprt old; if(!string_to_expr_z3(type, array, old)) return false; exprt where; if(!string_to_expr_z3(array_index_type(), inx, where)) return false; exprt new_val; if(!string_to_expr_z3(type.subtype(), elem, new_val)) return false; e = with_exprt(old, where, new_val); return true; } else if(value=="false") { e = false_exprt(); return true; } else if(value=="true") { e = true_exprt(); return true; } else if(value.substr(0,8)=="array_of") { // We assume that array_of has only concrete arguments... irep_idt id(value); array_of_mapt::const_iterator fit=array_of_map.begin(); while(fit!=array_of_map.end() && fit->second!=id) fit++; if(fit==array_of_map.end()) return false; e = fit->first; return true; } else if(type.id()==ID_rational) { constant_exprt result; result.type()=rational_typet(); if(value.substr(0,4)=="val!") result.set_value(value.substr(4)); else result.set_value(value); e = result; return true; } return false; }