bool type_eq(const typet &type1, const typet &type2, const namespacet &ns) { if(type1 == type2) return true; if(type1.id() == "symbol") { const symbolt &symbol = ns.lookup(type1); if(!symbol.is_type) throw "symbol " + id2string(symbol.name) + " is not a type"; return type_eq(symbol.type, type2, ns); } if(type2.id() == "symbol") { const symbolt &symbol = ns.lookup(type2); if(!symbol.is_type) throw "symbol " + id2string(symbol.name) + " is not a type"; return type_eq(type1, symbol.type, ns); } return false; }
bool string_abstractiont::build_wrap(const exprt &object, exprt &dest, bool write) { // debugging if(build(object, dest, write)) return true; // extra consistency check // use // #define build_wrap(a,b,c) build(a,b,c) // to avoid it const typet &a_t=build_abstraction_type(object.type()); /*assert(type_eq(dest.type(), a_t, ns) || (dest.type().id()==ID_array && a_t.id()==ID_pointer && type_eq(dest.type().subtype(), a_t.subtype(), ns))); */ if(!type_eq(dest.type(), a_t, ns) && !(dest.type().id()==ID_array && a_t.id()==ID_pointer && type_eq(dest.type().subtype(), a_t.subtype(), ns))) { std::string msg="warning: inconsistent abstract type for "+object.pretty(); warning(msg); return true; } return false; }
void remove_function_pointerst::fix_return_type( code_function_callt &function_call, goto_programt &dest) { // are we returning anything at all? if(function_call.lhs().is_nil()) return; const code_typet &code_type= to_code_type(ns.follow(function_call.function().type())); // type already ok? if(type_eq( function_call.lhs().type(), code_type.return_type(), ns)) return; symbolt &tmp_symbol=new_tmp_symbol(); tmp_symbol.type=code_type.return_type(); tmp_symbol.location=function_call.location(); symbol_exprt tmp_symbol_expr; tmp_symbol_expr.type()=tmp_symbol.type; tmp_symbol_expr.set_identifier(tmp_symbol.name); exprt old_lhs=function_call.lhs(); function_call.lhs()=tmp_symbol_expr; goto_programt::targett t_assign=dest.add_instruction(); t_assign->make_assignment(); t_assign->code=code_assignt( old_lhs, typecast_exprt(tmp_symbol_expr, old_lhs.type())); }
bool remove_function_pointerst::arg_is_type_compatible( const typet &call_type, const typet &function_type) { if(type_eq(call_type, function_type, ns)) return true; // any integer-vs-enum-vs-pointer is ok if(call_type.id()==ID_signedbv || call_type.id()==ID_unsigned || call_type.id()==ID_bool || call_type.id()==ID_pointer || call_type.id()==ID_c_enum || call_type.id()==ID_c_enum_tag) { if(function_type.id()==ID_signedbv || function_type.id()==ID_unsigned || function_type.id()==ID_bool || function_type.id()==ID_pointer || function_type.id()==ID_c_enum || function_type.id()==ID_c_enum_tag) return true; return false; } // structs/unions need to match, // which could be made more generous return false; }
bool instanceof(const symbol_tablet &st, const typet &lhs, const typet &rhs) { const namespacet ns(st); const typet &resolved_lhs=ns.follow(lhs); const typet &resolved_rhs=ns.follow(rhs); if (ID_class != resolved_lhs.id() || ID_class != resolved_rhs.id()) return type_eq(resolved_lhs, resolved_rhs, ns); return instanceof(resolved_lhs, resolved_rhs, ns); }
void cegis_assign(const symbol_tablet &st, goto_programt::instructiont &instr, const exprt &lhs, const exprt &rhs, const source_locationt &loc) { instr.type=goto_program_instruction_typet::ASSIGN; instr.source_location=loc; const namespacet ns(st); const typet &type=lhs.type(); if (type_eq(type, rhs.type(), ns)) instr.code=code_assignt(lhs, rhs); else instr.code=code_assignt(lhs, typecast_exprt(rhs, type)); }
void cpp_typecheckt::find_constructor( const typet &start_dest_type, exprt &constructor_expr) { constructor_expr.make_nil(); source_locationt source_location=start_dest_type.source_location(); typet dest_type(start_dest_type); follow_symbol(dest_type); if(dest_type.id()!=ID_struct) return; const struct_typet::componentst &components= to_struct_type(dest_type).components(); for(struct_typet::componentst::const_iterator it=components.begin(); it!=components.end(); it++) { const struct_typet::componentt &component=*it; const typet &type=component.type(); if(type.find(ID_return_type).id()==ID_constructor) { const irept::subt ¶meters= type.find(ID_parameters).get_sub(); namespacet ns(symbol_table); if(parameters.size()==1) { const exprt ¶meter=(exprt &)parameters.front(); const typet &arg_type=parameter.type(); if(arg_type.id()==ID_pointer && type_eq(arg_type.subtype(), dest_type, ns)) { // found! const irep_idt &identifier= component.get(ID_name); if(identifier=="") throw "constructor without identifier"; constructor_expr=exprt(ID_symbol, type); constructor_expr.set(ID_identifier, identifier); constructor_expr.add_source_location()=source_location; return; } } } } }
bool remove_function_pointerst::is_type_compatible( bool return_value_used, const code_typet &call_type, const code_typet &function_type) { // we are willing to ignore anything that's returned // if we call with 'void' if(!return_value_used) { } else if(type_eq(call_type.return_type(), empty_typet(), ns)) { // ok } else { if(!arg_is_type_compatible(call_type.return_type(), function_type.return_type())) return false; } // let's look at the parameters const code_typet::parameterst &call_parameters=call_type.parameters(); const code_typet::parameterst &function_parameters=function_type.parameters(); if(function_type.has_ellipsis() && function_parameters.empty()) { // always ok } else if(call_type.has_ellipsis() && call_parameters.empty()) { // always ok } else { // we are quite strict here, could be much more generous if(call_parameters.size()!=function_parameters.size()) return false; for(unsigned i=0; i<call_parameters.size(); i++) if(!arg_is_type_compatible(call_parameters[i].type(), function_parameters[i].type())) return false; } return true; }
void remove_function_pointerst::fix_argument_types( code_function_callt &function_call) { const code_typet &code_type= to_code_type(ns.follow(function_call.function().type())); const code_typet::parameterst &function_parameters= code_type.parameters(); code_function_callt::argumentst &call_arguments= function_call.arguments(); for(unsigned i=0; i<function_parameters.size(); i++) { if(i<call_arguments.size()) { if(!type_eq(call_arguments[i].type(), function_parameters[i].type(), ns)) { call_arguments[i].make_typecast(function_parameters[i].type()); } } } }
static struct expr_val cgasm_handle_ptr_sub(struct cgasm_context *ctx, struct expr_val lhs, struct expr_val rhs) { lhs = cgasm_handle_deref_flag(ctx, lhs); rhs = cgasm_handle_deref_flag(ctx, rhs); if (lhs.ctype->tag == T_PTR && is_integer_type(rhs.ctype)) { struct type *oldtype = lhs.ctype; struct type *subtype = lhs.ctype->subtype; if (subtype->tag != T_VOID && type_get_size(subtype) != 1) { panic("non unit ptr"); } lhs.ctype = get_int_type(); struct expr_val res = cgasm_handle_binary_op(ctx, TOK_SUB, lhs, rhs); res = cgasm_handle_deref_flag(ctx, res); res.ctype = oldtype; return res; } if (lhs.ctype->tag == T_PTR && rhs.ctype->tag == T_PTR) { if (!type_eq(lhs.ctype->subtype, rhs.ctype->subtype)) { panic("incompatible pointer types"); } struct type *elem_type = lhs.ctype->subtype; lhs.ctype = get_int_type(); rhs.ctype = get_int_type(); struct expr_val res = cgasm_handle_binary_op(ctx, TOK_SUB, lhs, rhs); int elem_size = type_get_size(elem_type); if (elem_type->tag != T_VOID && elem_size != 1) { res = cgasm_handle_binary_op(ctx, TOK_DIV, res, int_const_expr_val(elem_size)); } return res; } panic("invalid ptr subtraction"); }
END_TEST START_TEST(test_lib_save) { { tree_t ent = tree_new(T_ENTITY); tree_set_ident(ent, ident_new("name")); tree_add_attr_str(ent, ident_new("attr"), ident_new("test string")); type_t e = type_new(T_ENUM); type_set_ident(e, ident_new("myenum")); tree_t a = tree_new(T_ENUM_LIT); tree_set_ident(a, ident_new("a")); tree_set_type(a, e); tree_set_pos(a, 55); type_enum_add_literal(e, a); tree_t b = tree_new(T_ENUM_LIT); tree_set_ident(b, ident_new("b")); tree_set_type(b, e); type_enum_add_literal(e, b); tree_t p1 = tree_new(T_PORT_DECL); tree_set_ident(p1, ident_new("foo")); tree_set_subkind(p1, PORT_OUT); tree_set_type(p1, type_universal_int()); tree_add_port(ent, p1); tree_t p2 = tree_new(T_PORT_DECL); tree_set_ident(p2, ident_new("bar")); tree_set_subkind(p2, PORT_IN); tree_set_type(p2, e); tree_add_port(ent, p2); tree_t ar = tree_new(T_ARCH); tree_set_ident(ar, ident_new("arch")); tree_set_ident2(ar, ident_new("foo")); tree_t pr = tree_new(T_PROCESS); tree_set_ident(pr, ident_new("proc")); tree_add_stmt(ar, pr); tree_t v1 = tree_new(T_VAR_DECL); tree_set_ident(v1, ident_new("v1")); tree_set_type(v1, e); tree_t r = tree_new(T_REF); tree_set_ident(r, ident_new("v1")); tree_set_ref(r, v1); tree_t s = tree_new(T_VAR_ASSIGN); tree_set_ident(s, ident_new("var_assign")); tree_set_target(s, r); tree_set_value(s, r); tree_add_stmt(pr, s); tree_t c = tree_new(T_LITERAL); tree_set_subkind(c, L_INT); tree_set_ival(c, 53); tree_t s2 = tree_new(T_VAR_ASSIGN); tree_set_ident(s2, ident_new("var_assign")); tree_set_target(s2, r); tree_set_value(s2, c); tree_add_stmt(pr, s2); tree_t s3 = tree_new(T_VAR_ASSIGN); tree_set_ident(s3, ident_new("var_assign")); tree_set_target(s3, r); tree_set_value(s3, str_to_agg("foobar", NULL)); tree_add_stmt(pr, s3); tree_t s4 = tree_new(T_ASSERT); tree_set_ident(s4, ident_new("assert")); tree_set_value(s4, c); tree_set_severity(s4, c); tree_set_message(s4, str_to_agg("message", NULL)); tree_add_stmt(pr, s4); lib_put(work, ar); lib_put(work, ent); } tree_gc(); lib_save(work); lib_free(work); lib_add_search_path("/tmp"); work = lib_find(ident_new("test_lib"), false); fail_if(work == NULL); { tree_t ent = lib_get(work, ident_new("name")); fail_if(ent == NULL); fail_unless(tree_kind(ent) == T_ENTITY); fail_unless(tree_ident(ent) == ident_new("name")); fail_unless(tree_ports(ent) == 2); ident_t attr = tree_attr_str(ent, ident_new("attr")); fail_if(attr == NULL); fail_unless(icmp(attr, "test string")); tree_t p1 = tree_port(ent, 0); fail_unless(tree_kind(p1) == T_PORT_DECL); fail_unless(tree_subkind(p1) == PORT_OUT); fail_unless(type_kind(tree_type(p1)) == T_INTEGER); tree_t p2 = tree_port(ent, 1); fail_unless(tree_kind(p2) == T_PORT_DECL); fail_unless(tree_subkind(p2) == PORT_IN); type_t e = tree_type(p2); fail_unless(type_kind(e) == T_ENUM); fail_unless(type_enum_literals(e) == 2); tree_t a = type_enum_literal(e, 0); fail_unless(tree_kind(a) == T_ENUM_LIT); fail_unless(tree_ident(a) == ident_new("a")); fail_unless(tree_type(a) == e); fail_unless(tree_pos(a) == 55); tree_t b = type_enum_literal(e, 1); fail_unless(tree_kind(b) == T_ENUM_LIT); fail_unless(tree_ident(b) == ident_new("b")); fail_unless(tree_type(b) == e); tree_t ar = lib_get(work, ident_new("arch")); fail_if(ar == NULL); fail_unless(tree_ident(ar) == ident_new("arch")); fail_unless(tree_ident2(ar) == ident_new("foo")); tree_t pr = tree_stmt(ar, 0); fail_unless(tree_kind(pr) == T_PROCESS); fail_unless(tree_ident(pr) == ident_new("proc")); tree_t s = tree_stmt(pr, 0); fail_unless(tree_kind(s) == T_VAR_ASSIGN); tree_t r = tree_target(s); fail_unless(tree_kind(r) == T_REF); fail_unless(tree_value(s) == r); tree_t s2 = tree_stmt(pr, 1); fail_unless(tree_kind(s2) == T_VAR_ASSIGN); fail_unless(tree_target(s2) == r); tree_t s3 = tree_stmt(pr, 2); fail_unless(tree_kind(s3) == T_VAR_ASSIGN); fail_unless(tree_target(s3) == r); fail_unless(tree_kind(tree_value(s3)) == T_AGGREGATE); tree_t s4 = tree_stmt(pr, 3); fail_unless(tree_kind(s4) == T_ASSERT); fail_unless(tree_ident(s4) == ident_new("assert")); tree_t c = tree_value(s2); fail_unless(tree_kind(c) == T_LITERAL); fail_unless(tree_subkind(c) == L_INT); fail_unless(tree_ival(c) == 53); // Type declaration and reference written to different units // so two copies of the type declaration will be read back // hence can't check for pointer equality here fail_unless(type_eq(tree_type(tree_ref(r)), e)); } }
void remove_virtual_functionst::remove_virtual_function( goto_programt &goto_program, goto_programt::targett target) { const code_function_callt &code= to_code_function_call(target->code); const auto &vcall_source_loc=target->source_location; const exprt &function=code.function(); assert(function.id()==ID_virtual_function); assert(!code.arguments().empty()); functionst functions; get_functions(function, functions); if(functions.empty()) { target->make_skip(); return; // give up } // only one option? if(functions.size()==1) { assert(target->is_function_call()); if(functions.begin()->symbol_expr==symbol_exprt()) target->make_skip(); else to_code_function_call(target->code).function()= functions.begin()->symbol_expr; return; } // the final target is a skip goto_programt final_skip; goto_programt::targett t_final=final_skip.add_instruction(); t_final->source_location=vcall_source_loc; t_final->make_skip(); // build the calls and gotos goto_programt new_code_calls; goto_programt new_code_gotos; exprt this_expr=code.arguments()[0]; // If necessary, cast to the last candidate function to // get the object's clsid. By the structure of get_functions, // this is the parent of all other classes under consideration. const auto &base_classid=functions.back().class_id; const auto &base_function_symbol=functions.back().symbol_expr; symbol_typet suggested_type(base_classid); exprt c_id2=get_class_identifier_field(this_expr, suggested_type, ns); std::map<irep_idt, goto_programt::targett> calls; // Note backwards iteration, to get the least-derived candidate first. for(auto it=functions.crbegin(), itend=functions.crend(); it!=itend; ++it) { const auto &fun=*it; auto insertit=calls.insert( {fun.symbol_expr.get_identifier(), goto_programt::targett()}); // Only create one call sequence per possible target: if(insertit.second) { goto_programt::targett t1=new_code_calls.add_instruction(); t1->source_location=vcall_source_loc; if(!fun.symbol_expr.get_identifier().empty()) { // call function t1->make_function_call(code); auto &newcall=to_code_function_call(t1->code); newcall.function()=fun.symbol_expr; pointer_typet need_type(symbol_typet(fun.symbol_expr.get(ID_C_class))); if(!type_eq(newcall.arguments()[0].type(), need_type, ns)) newcall.arguments()[0].make_typecast(need_type); } else { // No definition for this type; shouldn't be possible... t1->make_assertion(false_exprt()); } insertit.first->second=t1; // goto final goto_programt::targett t3=new_code_calls.add_instruction(); t3->source_location=vcall_source_loc; t3->make_goto(t_final, true_exprt()); } // If this calls the base function we just fall through. // Otherwise branch to the right call: if(fun.symbol_expr!=base_function_symbol) { exprt c_id1=constant_exprt(fun.class_id, string_typet()); goto_programt::targett t4=new_code_gotos.add_instruction(); t4->source_location=vcall_source_loc; t4->make_goto(insertit.first->second, equal_exprt(c_id1, c_id2)); } } goto_programt new_code; // patch them all together new_code.destructive_append(new_code_gotos); new_code.destructive_append(new_code_calls); new_code.destructive_append(final_skip); // set locations Forall_goto_program_instructions(it, new_code) { const irep_idt property_class=it->source_location.get_property_class(); const irep_idt comment=it->source_location.get_comment(); it->source_location=target->source_location; it->function=target->function; if(!property_class.empty()) it->source_location.set_property_class(property_class); if(!comment.empty()) it->source_location.set_comment(comment); } goto_programt::targett next_target=target; next_target++; goto_program.destructive_insert(next_target, new_code); // finally, kill original invocation target->make_skip(); }
bool string_abstractiont::is_ptr_string_struct(const typet &type) const { return type.id()==ID_pointer && type_eq(type.subtype(), string_struct, ns); }
bool type_eq(type_t a, type_t b) { assert(a != NULL); assert(b != NULL); type_kind_t kind_a = type_kind(a); type_kind_t kind_b = type_kind(b); if ((kind_a == T_UNRESOLVED) || (kind_b == T_UNRESOLVED)) return false; if (a == b) return true; // Subtypes are convertible to the base type while ((kind_a = type_kind(a)) == T_SUBTYPE) a = type_base(a); while ((kind_b = type_kind(b)) == T_SUBTYPE) b = type_base(b); const bool compare_c_u_arrays = (kind_a == T_CARRAY && kind_b == T_UARRAY) || (kind_a == T_UARRAY && kind_b == T_CARRAY); if ((kind_a != kind_b) && !compare_c_u_arrays) return false; // Universal integer type is equal to any other integer type type_t universal_int = type_universal_int(); ident_t uint_i = type_ident(universal_int); if (kind_a == T_INTEGER && (type_ident(a) == uint_i || type_ident(b) == uint_i)) return true; // Universal real type is equal to any other real type type_t universal_real = type_universal_real(); ident_t ureal_i = type_ident(universal_real); if (kind_a == T_REAL && (type_ident(a) == ureal_i || type_ident(b) == ureal_i)) return true; // XXX: this is not quite right as structurally equivalent types // may be declared in different scopes with the same name but // shouldn't compare equal if (type_ident(a) != type_ident(b)) return false; // Access types are equal if the pointed to type is the same if (kind_a == T_ACCESS) return type_eq(type_access(a), type_access(b)); if (compare_c_u_arrays) return type_eq(type_elem(a), type_elem(b)); const imask_t has = has_map[a->kind]; if ((has & I_DIMS) && (type_dims(a) != type_dims(b))) return false; if (type_kind(a) == T_FUNC) { if (!type_eq(type_result(a), type_result(b))) return false; } if (has & I_PARAMS) { if (type_params(a) != type_params(b)) return false; const int nparams = type_params(a); for (int i = 0; i < nparams; i++) { if (!type_eq(type_param(a, i), type_param(b, i))) return false; } } return true; }