Skip to content

expr2c now distinguishes binary and multi-ary expressions #1726

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 1 commit into from
Jan 20, 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
78 changes: 66 additions & 12 deletions src/ansi-c/expr2c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -813,10 +813,9 @@ std::string expr2ct::convert_trinary(
if(src.operands().size()!=3)
return convert_norep(src, precedence);

const exprt::operandst &operands=src.operands();
const exprt &op0=operands.front();
const exprt &op1=*(++operands.begin());
const exprt &op2=operands.back();
const exprt &op0=src.op0();
const exprt &op1=src.op1();
const exprt &op2=src.op2();

unsigned p0, p1, p2;

Expand Down Expand Up @@ -1028,6 +1027,61 @@ std::string expr2ct::convert_binary(
const std::string &symbol,
unsigned precedence,
bool full_parentheses)
{
if(src.operands().size()!=2)
return convert_norep(src, precedence);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't that now be a PRECONDITON?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kroening Would you mind fixing/commenting on this?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

expr2c has, historically, not enforced any data invariants on expressions; malformed expressions are instead printed as "Lisp" expressions.

Using a PRECONDITION here would be a very substantial change in semantics.


const exprt &op0=src.op0();
const exprt &op1=src.op1();

unsigned p0, p1;

std::string s_op0=convert_with_precedence(op0, p0);
std::string s_op1=convert_with_precedence(op1, p1);

std::string dest;

// In pointer arithmetic, x+(y-z) is unfortunately
// not the same as (x+y)-z, even though + and -
// have the same precedence. We thus add parentheses
// for the case x+(y-z). Similarly, (x*y)/z is not
// the same as x*(y/z), but * and / have the same
// precedence.

bool use_parentheses0=
precedence>p0 ||
(precedence==p0 && full_parentheses) ||
(precedence==p0 && src.id()!=op0.id());

if(use_parentheses0)
dest+='(';
dest+=s_op0;
if(use_parentheses0)
dest+=')';

dest+=' ';
dest+=symbol;
dest+=' ';

bool use_parentheses1=
precedence>p1 ||
(precedence==p1 && full_parentheses) ||
(precedence==p1 && src.id()!=op1.id());

if(use_parentheses1)
dest+='(';
dest+=s_op1;
if(use_parentheses1)
dest+=')';

return dest;
}

std::string expr2ct::convert_multi_ary(
const exprt &src,
const std::string &symbol,
unsigned precedence,
bool full_parentheses)
{
if(src.operands().size()<2)
return convert_norep(src, precedence);
Expand Down Expand Up @@ -3386,7 +3440,7 @@ std::string expr2ct::convert_with_precedence(
precedence=16;

if(src.id()==ID_plus)
return convert_binary(src, "+", precedence=12, false);
return convert_multi_ary(src, "+", precedence=12, false);

else if(src.id()==ID_minus)
return convert_binary(src, "-", precedence=12, true);
Expand Down Expand Up @@ -3690,7 +3744,7 @@ std::string expr2ct::convert_with_precedence(
return convert_unary(src, "~", precedence=15);

else if(src.id()==ID_mult)
return convert_binary(src, "*", precedence=13, false);
return convert_multi_ary(src, "*", precedence=13, false);

else if(src.id()==ID_div)
return convert_binary(src, "/", precedence=13, true);
Expand Down Expand Up @@ -3739,22 +3793,22 @@ std::string expr2ct::convert_with_precedence(
return convert_complex(src, precedence=16);

else if(src.id()==ID_bitand)
return convert_binary(src, "&", precedence=8, false);
return convert_multi_ary(src, "&", precedence=8, false);

else if(src.id()==ID_bitxor)
return convert_binary(src, "^", precedence=7, false);
return convert_multi_ary(src, "^", precedence=7, false);

else if(src.id()==ID_bitor)
return convert_binary(src, "|", precedence=6, false);
return convert_multi_ary(src, "|", precedence=6, false);

else if(src.id()==ID_and)
return convert_binary(src, "&&", precedence=5, false);
return convert_multi_ary(src, "&&", precedence=5, false);

else if(src.id()==ID_or)
return convert_binary(src, "||", precedence=4, false);
return convert_multi_ary(src, "||", precedence=4, false);

else if(src.id()==ID_xor)
return convert_binary(src, "^", precedence=7, false);
return convert_multi_ary(src, "^", precedence=7, false);

else if(src.id()==ID_implies)
return convert_binary(src, "==>", precedence=3, true);
Expand Down
4 changes: 4 additions & 0 deletions src/ansi-c/expr2c_class.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ class expr2ct
const exprt &src, const std::string &symbol,
unsigned precedence, bool full_parentheses);

std::string convert_multi_ary(
const exprt &src, const std::string &symbol,
unsigned precedence, bool full_parentheses);

std::string convert_cond(
const exprt &src, unsigned precedence);

Expand Down