Skip to content
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

Support outlines in closures #633

Merged
merged 4 commits into from
Mar 7, 2023
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
30 changes: 28 additions & 2 deletions src/main/scala/viper/gobra/frontend/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1808,7 +1808,7 @@ object Desugar {
} yield in.Seqn(Vector(dPre, exprAss, clauseBody))(src)

case n: POutline =>
val name = s"${rootName(n, info)}$$${nm.relativeId(n, info)}"
val name = s"${rootName(n, info)}$$${nm.relativeIdEnclosingFuncOrMethodDecl(n, info)}"
val pres = (n.spec.pres ++ n.spec.preserves) map preconditionD(ctx, info)
val posts = (n.spec.preserves ++ n.spec.posts) map postconditionD(ctx, info)
val terminationMeasures = sequence(n.spec.terminationMeasures map terminationMeasureD(ctx, info)).res
Expand Down Expand Up @@ -3808,6 +3808,13 @@ object Desugar {
case decl: PMethodSig => idName(decl.id, context)
case decl: PMPredicateSig => idName(decl.id, context)
case decl: PDomainFunction => idName(decl.id, context)
case decl: PClosureDecl =>
// closure declarations do not have an associated name and may be arbitrarily nested.
// to simplify, we just return the enclosing function or method's name
info.enclosingFunctionOrMethod(decl) match {
case Some(d) => idName(d.id, context)
case None => violation(s"Could not find a function or method declaration enclosing the closure declaration.")
}
case _ => ??? // axiom and method-implementation-proof
}
}
Expand Down Expand Up @@ -4852,7 +4859,7 @@ object Desugar {
* The id is of the form 'L$<a>$<b>' where a is the difference of the lines
* of the node with the code root and b is the difference of the columns
* of the node with the code root.
* If a differce is negative, the '-' character is replaced with '_'.
* If the difference is negative, the '-' character is replaced with '_'.
*
* @param node the node we are interested in
* @param info type info to get position information
Expand All @@ -4865,6 +4872,25 @@ object Desugar {
("L$" + (lpos.line - rpos.line) + "$" + (lpos.column - rpos.column)).replace("-", "_")
}

/**
* Returns an id for a node with respect to its enclosing function or method declaration.
* The id is of the form 'L$<a>$<b>' where a is the difference of the lines
* of the node with the enclosing declaration and b is the difference of the columns
* of the node with the enclosing declaration.
* If the difference is negative, the '-' character is replaced with '_'.
*
* @param node the node we are interested in
* @param info type info to get position information
* @return string
*/
def relativeIdEnclosingFuncOrMethodDecl(node: PNode, info: TypeInfo) : String = {
val pom = info.getTypeInfo.tree.originalRoot.positions
val lpos = pom.positions.getStart(node).get
val enclosingMember = info.enclosingFunctionOrMethod(node).get
val rpos = pom.positions.getStart(enclosingMember).get
("L$" + (lpos.line - rpos.line) + "$" + (lpos.column - rpos.column)).replace("-", "_")
}

/** returns the relativeId with the CONTINUE_LABEL_SUFFIX appended */
def continueLabel(loop: PGeneralForStmt, info: TypeInfo) : String = relativeId(loop, info) + CONTINUE_LABEL_SUFFIX

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,16 @@ func test6() {

//:: ExpectedOutput(assert_error:assertion_error)
assert x == 11
}
}

func test7() {
f :=
ensures acc(y) && *y >= 5
func fspec() (y *int) {
ensures acc(&x) && x >= 5
outline (
x@ := 10
)
return &x
}
}