-
Notifications
You must be signed in to change notification settings - Fork 26
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
Bool/Int type coercion #1170
Draft
ArmborstL
wants to merge
7
commits into
dev
Choose a base branch
from
c-int-bool-coercion
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Bool/Int type coercion #1170
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
31db34d
add type coercion between int and bool for C. Also coerce pointers to…
ArmborstL 18ffaaf
int/bool coercion: incorporated PR feedback
ArmborstL 9b74fd7
Merge remote-tracking branch 'origin/dev' into c-int-bool-coercion
ArmborstL 5f59dc3
Merge remote-tracking branch 'origin/dev' into c-int-bool-coercion
ArmborstL 0acf6eb
Merge branch 'c-int-bool-coercion' of https://github.com/utwente-fmt/…
ArmborstL d9402ac
allow explicit cast from int to boolean and vice versa
ArmborstL 0e8d8a5
Try having _Bool be an integer internally. Let's see what the CI says...
ArmborstL File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
|
||
//@ requires i; | ||
void check(int i) { | ||
if(!i) { | ||
//@ assert false; | ||
} | ||
} | ||
|
||
void main() { | ||
check(5==5); | ||
int *p; | ||
if(!p) return; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package vct.col.ast.family.coercion | ||
|
||
import vct.col.ast.{CoerceBoolCInt, TBool} | ||
import vct.col.ast.ops.CoerceBoolCIntOps | ||
|
||
trait CoerceBoolCIntImpl[G] extends CoerceBoolCIntOps[G] { this: CoerceBoolCInt[G] => | ||
override def target: TBool[G] = TBool() | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package vct.col.ast.family.coercion | ||
|
||
import vct.col.ast.{CoerceCIntBool, TBool} | ||
import vct.col.ast.ops.CoerceCIntBoolOps | ||
|
||
trait CoerceCIntBoolImpl[G] extends CoerceCIntBoolOps[G] { this: CoerceCIntBool[G] => | ||
override def target: TBool[G] = TBool() | ||
|
||
} |
10 changes: 10 additions & 0 deletions
10
src/col/vct/col/ast/family/coercion/CoercePointerBoolImpl.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package vct.col.ast.family.coercion | ||
|
||
import vct.col.ast.ops.CoercePointerBoolOps | ||
import vct.col.ast.{CoercePointerBool, TBool} | ||
|
||
trait CoercePointerBoolImpl[G] extends CoercePointerBoolOps[G] { | ||
this: CoercePointerBool[G] => | ||
// override def layout(implicit ctx: Ctx): Doc = ??? | ||
override def target: TBool[G] = TBool() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package vct.col.rewrite | ||
|
||
import vct.col.ast._ | ||
import vct.col.origin.Origin | ||
import vct.col.typerules.CoercingRewriter | ||
|
||
case object CIntBoolCoercion extends RewriterBuilder { | ||
override def key: String = "CIntBool" | ||
override def desc: String = "Introduce an explicit node to convert integers to boolean and vice versa for C." | ||
} | ||
|
||
case class CIntBoolCoercion[Pre <: Generation]() extends CoercingRewriter[Pre] { | ||
override def applyCoercion(e: => Expr[Post], coercion: Coercion[Pre])(implicit o: Origin): Expr[Post] = coercion match { | ||
case CoerceBoolCInt() => | ||
val zero = new CIntegerValue[Post](0) | ||
val one = new CIntegerValue[Post](1) | ||
new Select[Post](e, one, zero) | ||
case CoerceCIntBool() => | ||
val zero = new CIntegerValue[Post](0) | ||
new Neq[Post](e, zero) | ||
case CoercePointerBool() => | ||
val ptr = new Null[Post]() | ||
new Neq[Post](e, ptr) | ||
case other => super.applyCoercion(e, other) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sakehl I'm not sure what isCPromoting does. Should I add one of the coercions as a case there? In principle, I want all coercions to work in C code, so I'm reluctant to add e.g. CoerceCIntBool as
false
here, but it is not injective and thus not promoting...Also, is it correct that some cases, like CoerceJoinUnion, call the regular
isPromoting
instead of the special C versionisCPromoting
? or is that just a copy-paste error?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What I wanted to capture here is that float/double can only be
demoted
to int (and double to float) in:I based this mostly on this reference and here
This is called demotion, and is only valid in those cases. More specifically it is not valid with arithmetic conversions. (so
5 % 4.0
is not a valid statement, since 4.0 cannot be demoted to an int here, and % does not work on floating point numbers).Anyway it seems that boolean is a bit harder. When looking specifically at if-statements and logical and/or statements of C:
https://en.cppreference.com/w/c/language/operator_logical
They work for
an expression of any scalar type
. Which is of course not how we defined this internally in VerCors.I think we can then best encode this to always allow
demoting
pointers and integers to booleans.So in that case, I'd say that we put:
both to true.
(.... what is C a messy language)
This is definitely a copy paste error, thanks for spotting it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, then I leave it as-is and let the default case handle it.
Ok, I'll fix it :)