Skip to content

rewrite_union no longer needs namespace #2915

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/cpp/cpp_typecheck_compound_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1458,9 +1458,14 @@ void cpp_typecheckt::convert_anon_struct_union_member(
cpp_scopes.current_scope().prefix+
base_name.c_str();

const symbol_typet symbol_type(struct_union_symbol.name);
typet compound_type;

struct_typet::componentt component(identifier, symbol_type);
if(struct_union_symbol.type.id() == ID_union)
compound_type = union_tag_typet(struct_union_symbol.name);
else
compound_type = symbol_typet(struct_union_symbol.name);

struct_typet::componentt component(identifier, compound_type);
component.set_access(access);
component.set_base_name(base_name);
component.set_pretty_name(base_name);
Expand Down
53 changes: 20 additions & 33 deletions src/goto-symex/rewrite_union.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,76 +20,68 @@ Author: Daniel Kroening, [email protected]

#include <util/c_types.h>

static bool have_to_rewrite_union(
const exprt &expr,
const namespacet &ns)
static bool have_to_rewrite_union(const exprt &expr)
{
if(expr.id()==ID_member)
{
const exprt &op=to_member_expr(expr).struct_op();
const typet &op_type=ns.follow(op.type());

if(op_type.id()==ID_union)
if(op.type().id() == ID_union_tag || op.type().id() == ID_union)
return true;
}
else if(expr.id()==ID_union)
return true;

forall_operands(it, expr)
if(have_to_rewrite_union(*it, ns))
if(have_to_rewrite_union(*it))
return true;

return false;
}

// inside an address of (&x), unions can simply
// be type casts and don't have to be re-written!
void rewrite_union_address_of(
exprt &expr,
const namespacet &ns)
void rewrite_union_address_of(exprt &expr)
{
if(!have_to_rewrite_union(expr, ns))
if(!have_to_rewrite_union(expr))
return;

if(expr.id()==ID_index)
{
rewrite_union_address_of(to_index_expr(expr).array(), ns);
rewrite_union(to_index_expr(expr).index(), ns);
rewrite_union_address_of(to_index_expr(expr).array());
rewrite_union(to_index_expr(expr).index());
}
else if(expr.id()==ID_member)
rewrite_union_address_of(to_member_expr(expr).struct_op(), ns);
rewrite_union_address_of(to_member_expr(expr).struct_op());
else if(expr.id()==ID_symbol)
{
// done!
}
else if(expr.id()==ID_dereference)
rewrite_union(to_dereference_expr(expr).pointer(), ns);
rewrite_union(to_dereference_expr(expr).pointer());
}

/// We rewrite u.c for unions u into byte_extract(u, 0), and { .c = v } into
/// byte_update(NIL, 0, v)
void rewrite_union(
exprt &expr,
const namespacet &ns)
void rewrite_union(exprt &expr)
{
if(expr.id()==ID_address_of)
{
rewrite_union_address_of(to_address_of_expr(expr).object(), ns);
rewrite_union_address_of(to_address_of_expr(expr).object());
return;
}

if(!have_to_rewrite_union(expr, ns))
if(!have_to_rewrite_union(expr))
return;

Forall_operands(it, expr)
rewrite_union(*it, ns);
rewrite_union(*it);

if(expr.id()==ID_member)
{
const exprt &op=to_member_expr(expr).struct_op();
const typet &op_type=ns.follow(op.type());

if(op_type.id()==ID_union)
if(op.type().id() == ID_union_tag || op.type().id() == ID_union)
{
exprt offset=from_integer(0, index_type());
byte_extract_exprt tmp(byte_extract_id(), op, offset, expr.type());
Expand All @@ -107,27 +99,22 @@ void rewrite_union(
}
}

void rewrite_union(
goto_functionst::goto_functiont &goto_function,
const namespacet &ns)
void rewrite_union(goto_functionst::goto_functiont &goto_function)
{
Forall_goto_program_instructions(it, goto_function.body)
{
rewrite_union(it->code, ns);
rewrite_union(it->guard, ns);
rewrite_union(it->code);
rewrite_union(it->guard);
}
}

void rewrite_union(
goto_functionst &goto_functions,
const namespacet &ns)
void rewrite_union(goto_functionst &goto_functions)
{
Forall_goto_functions(it, goto_functions)
rewrite_union(it->second, ns);
rewrite_union(it->second);
}

void rewrite_union(goto_modelt &goto_model)
{
namespacet ns(goto_model.symbol_table);
rewrite_union(goto_model.goto_functions, ns);
rewrite_union(goto_model.goto_functions);
}
16 changes: 4 additions & 12 deletions src/goto-symex/rewrite_union.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,9 @@ class namespacet;
class goto_functionst;
class goto_modelt;

void rewrite_union(
exprt &expr,
const namespacet &ns);

void rewrite_union(
goto_functionst::goto_functiont &goto_function,
const namespacet &ns);

void rewrite_union(
goto_functionst &goto_functions,
const namespacet &ns);
void rewrite_union(goto_modelt &goto_model);
void rewrite_union(exprt &);
void rewrite_union(goto_functionst::goto_functiont &);
void rewrite_union(goto_functionst &);
void rewrite_union(goto_modelt &);

#endif // CPROVER_GOTO_SYMEX_REWRITE_UNION_H