Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
jcp19 committed Jan 20, 2025
1 parent 22c5f86 commit 5ce6644
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 25 deletions.
6 changes: 2 additions & 4 deletions src/main/scala/viper/gobra/ast/frontend/Ast.scala
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ case class PPackage(

case class PProgram(
packageClause: PPackageClause,
// TODO: doc
staticInvs: Vector[PPkgInvariant],
pkgInvariants: Vector[PPkgInvariant],
imports: Vector[PImport],
friends: Vector[PFriendPkgDecl],
declarations: Vector[PMember]
Expand All @@ -63,8 +62,7 @@ case class PProgram(

case class PPreamble(
packageClause: PPackageClause,
// TODO: doc
staticInvs: Vector[PPkgInvariant],
pkgInvariants: Vector[PPkgInvariant],
imports: Vector[PImport],
friends: Vector[PFriendPkgDecl],
positions: PositionManager,
Expand Down
12 changes: 6 additions & 6 deletions src/main/scala/viper/gobra/ast/frontend/PrettyPrinter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,25 @@ class DefaultPrettyPrinter extends PrettyPrinter with kiama.output.PrettyPrinter
// program

def showProgram(p: PProgram): Doc = p match {
case PProgram(packageClause, /*progPosts,*/ staticInvs, imports, friends, declarations) =>
showPreamble(packageClause, /*progPosts,*/ staticInvs, imports, friends) <>
case PProgram(packageClause, pkgInvs, imports, friends, declarations) =>
showPreamble(packageClause, pkgInvs, imports, friends) <>
ssep(declarations map showMember, line <> line) <> line
}

// preamble

def showPreamble(p: PPreamble): Doc = p match {
case PPreamble(packageClause, staticInvs, imports, friends, _) =>
showPreamble(packageClause, staticInvs, imports, friends)
case PPreamble(packageClause, pkgInvs, imports, friends, _) =>
showPreamble(packageClause, pkgInvs, imports, friends)
}

private def showPreamble(
packageClause: PPackageClause,
staticInvs: Vector[PPkgInvariant],
pkgInvs: Vector[PPkgInvariant],
imports: Vector[PImport],
friends: Vector[PFriendPkgDecl]
): Doc =
vcat(staticInvs.map(showPkgInvariant)) <>
vcat(pkgInvs.map(showPkgInvariant)) <>
showPackageClause(packageClause) <> line <> line <>
ssep(friends map showFriend, line) <> line <>
ssep(imports map showImport, line) <> line
Expand Down
8 changes: 4 additions & 4 deletions src/main/scala/viper/gobra/frontend/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ object Desugar extends LazyLogging {
val pkg = i.getTypeInfo.tree.originalRoot
importsCollector.registerCurrentPackage(pkg)
val nonDupInvs = pkg.programs
.flatMap(_.staticInvs)
.flatMap(_.pkgInvariants)
.filter(!_.duplicable)
.map(_.inv)
.map(mainDesugarer.specificationD(mainDesugarer.FunctionContext.empty(), i.getTypeInfo)(_))
Expand Down Expand Up @@ -3476,7 +3476,7 @@ object Desugar extends LazyLogging {

// Check that all duplicable static invariants are actually duplicable.
mainPkg.programs
.flatMap(_.staticInvs)
.flatMap(_.pkgInvariants)
.filter(_.duplicable)
.map(_.inv)
.map(genCheckAssertionIsDup)
Expand Down Expand Up @@ -3572,7 +3572,7 @@ object Desugar extends LazyLogging {

// collect package invariants
val partitionFunc = (inv: PPkgInvariant) => if (inv.duplicable) Left(inv.inv) else Right(inv.inv)
val (dups, nonDups) = pkg.programs.flatMap(_.staticInvs).partitionMap(partitionFunc)
val (dups, nonDups) = pkg.programs.flatMap(_.pkgInvariants).partitionMap(partitionFunc)
val goA = specificationD(FunctionContext.empty(), info)(_)
val dDups = dups.map(goA)
val dNonDups = nonDups.map(goA)
Expand Down Expand Up @@ -3678,7 +3678,7 @@ object Desugar extends LazyLogging {
val progPres: Vector[in.Assertion] = p.imports.flatMap(_.importPres).map(specificationD(FunctionContext.empty(), info)(_))
// val progPosts: Vector[in.Assertion] = p.initPosts.map(specificationD(FunctionContext.empty(), info)(_))
val progPosts: Vector[in.Assertion] = Vector.empty // p.initPosts.map(specificationD(FunctionContext.empty(), info)(_))
val pkgInvariants: Vector[in.Assertion] = p.staticInvs.map{i => specificationD(FunctionContext.empty(), info)(i.inv)}
val pkgInvariants: Vector[in.Assertion] = p.pkgInvariants.map{ i => specificationD(FunctionContext.empty(), info)(i.inv)}
val resourcesForFriends: Vector[in.Assertion] = p.friends.map{i => specificationD(FunctionContext.empty(), info)(i.assertion)}
val pkgInvariantsImportedPackages: Vector[in.Assertion] =
initSpecs match {
Expand Down
10 changes: 5 additions & 5 deletions src/main/scala/viper/gobra/frontend/ParseTreeTranslator.scala
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ class ParseTreeTranslator(pom: PositionManager, source: Source, specOnly : Boole
val spec = if (ctx.specification() != null)
visitSpecification(ctx.specification())
else
PFunctionSpec(Vector.empty,Vector.empty,Vector.empty, Vector.empty, Vector.empty, mayBeUsedInInit = false).at(ctx) // TODO: drop
PFunctionSpec(Vector.empty,Vector.empty,Vector.empty, Vector.empty, Vector.empty).at(ctx)
// The name of each explicitly specified method must be unique and not blank.
val id = idnDef.get(ctx.IDENTIFIER())
val args = visitNode[Vector[Vector[PParameter]]](ctx.parameters())
Expand Down Expand Up @@ -2265,15 +2265,15 @@ class ParseTreeTranslator(pom: PositionManager, source: Source, specOnly : Boole
if (initPosts.nonEmpty) {
violation(s"initEnsures clauses are no longer supported after Gobra PR #797.")
}
val staticInvs: Vector[PPkgInvariant] = visitListNode[PPkgInvariant](ctx.pkgInvariant())
val pkgInvs: Vector[PPkgInvariant] = visitListNode[PPkgInvariant](ctx.pkgInvariant())
val importDecls = ctx.importDecl().asScala.toVector.flatMap(visitImportDecl)
val friendPkgs: Vector[PFriendPkgDecl] = visitListNode[PFriendPkgDecl](ctx.friendPkgDecl())

// Don't parse functions/methods if the identifier is blank
val members = visitListNode[PMember](ctx.specMember())
val ghostMembers = ctx.ghostMember().asScala.flatMap(visitNode[Vector[PGhostMember]])
val decls = ctx.declaration().asScala.toVector.flatMap(visitDeclaration(_).asInstanceOf[Vector[PDeclaration]])
PProgram(packageClause, /* initPosts, */ staticInvs, importDecls, friendPkgs, members ++ decls ++ ghostMembers).at(ctx)
PProgram(packageClause, pkgInvs, importDecls, friendPkgs, members ++ decls ++ ghostMembers).at(ctx)
}

override def visitPreamble(ctx: GobraParser.PreambleContext): PPreamble = {
Expand All @@ -2282,10 +2282,10 @@ class ParseTreeTranslator(pom: PositionManager, source: Source, specOnly : Boole
if (initPosts.nonEmpty) {
violation(s"initEnsures clauses are no longer supported after Gobra PR #797.")
}
val staticInvs: Vector[PPkgInvariant] = visitListNode[PPkgInvariant](ctx.pkgInvariant())
val pkgInvs: Vector[PPkgInvariant] = visitListNode[PPkgInvariant](ctx.pkgInvariant())
val importDecls = ctx.importDecl().asScala.toVector.flatMap(visitImportDecl)
val friendPkgs: Vector[PFriendPkgDecl] = visitListNode[PFriendPkgDecl](ctx.friendPkgDecl())
PPreamble(packageClause, staticInvs, importDecls, friendPkgs, pom).at(ctx)
PPreamble(packageClause, pkgInvs, importDecls, friendPkgs, pom).at(ctx)
}

/**
Expand Down
6 changes: 3 additions & 3 deletions src/main/scala/viper/gobra/frontend/Parser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ object Parser extends LazyLogging {
// note that the resolveImports strategy could be embedded in e.g. a logfail strategy to report a
// failed strategy application
val updatedImports = rewrite(topdown(attempt(resolveImports)))(prog.imports)
PProgram(prog.packageClause, prog.staticInvs, updatedImports, prog.friends, prog.declarations).at(prog)
PProgram(prog.packageClause, prog.pkgInvariants, updatedImports, prog.friends, prog.declarations).at(prog)
})
// create a new package node with the updated programs
val updatedPkg = PPackage(pkg.packageClause, updatedProgs, pkg.positions, pkg.info).at(pkg)
Expand Down Expand Up @@ -493,7 +493,7 @@ object Parser extends LazyLogging {
case n@PTupleTerminationMeasure(_, cond) => PWildcardMeasure(cond).at(n)
case t => t
}
PFunctionSpec(spec.pres, spec.preserves, spec.posts, replacedMeasures, spec.backendAnnotations, spec.isPure, spec.isTrusted, mayBeUsedInInit = spec.mayBeUsedInInit) // TODO: drop
PFunctionSpec(spec.pres, spec.preserves, spec.posts, replacedMeasures, spec.backendAnnotations, spec.isPure, spec.isTrusted, mayBeUsedInInit = spec.mayBeUsedInInit)
}

val replaceTerminationMeasuresForFunctionsAndMethods: Strategy =
Expand All @@ -509,7 +509,7 @@ object Parser extends LazyLogging {
// apply the replaceTerminationMeasuresForFunctionsAndMethods to declarations until the strategy has succeeded
// (i.e. has reached PMember nodes) and stop then
val updatedDecls = rewrite(alltd(replaceTerminationMeasuresForFunctionsAndMethods))(prog.declarations)
PProgram(prog.packageClause, prog.staticInvs, prog.imports, prog.friends, updatedDecls).at(prog)
PProgram(prog.packageClause, prog.pkgInvariants, prog.imports, prog.friends, updatedDecls).at(prog)
})
// create a new package node with the updated programs
Right(PPackage(pkg.packageClause, updatedProgs, pkg.positions, pkg.info).at(pkg))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,16 @@ trait GhostMemberTyping extends BaseTyping { this: TypeInfoImpl =>
needsMeasureError
}

private def pureFunctionsDoNotNeedMayInitMsg = "Pure functions and methods cannot open package invariants," +
"and thus, they must not be annotated with 'mayInit'."

private[typing] def wellDefIfPureMethod(member: PMethodDecl): Messages = {
if (member.spec.isPure) {
isSingleResultArg(member) ++
isSinglePureReturnExpr(member) ++
isPurePostcondition(member.spec) ++
nonVariadicArguments(member.args) ++
error(member, "Pure functions cannot be annotated with 'mayInit'.", member.spec.mayBeUsedInInit)
error(member, pureFunctionsDoNotNeedMayInitMsg, member.spec.mayBeUsedInInit)
} else noMessages
}

Expand All @@ -66,7 +69,7 @@ trait GhostMemberTyping extends BaseTyping { this: TypeInfoImpl =>
isSinglePureReturnExpr(member) ++
isPurePostcondition(member.spec) ++
nonVariadicArguments(member.args) ++
error(member, "Pure functions do not need to be annotated with 'mayInit'.", member.spec.mayBeUsedInInit)
error(member, pureFunctionsDoNotNeedMayInitMsg, member.spec.mayBeUsedInInit)
} else noMessages
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ trait GhostStmtTyping extends BaseTyping { this: TypeInfoImpl =>
case PFold(acc) => wellDefFoldable(acc)
case PUnfold(acc) => wellDefFoldable(acc)
case POpenDupPkgInv() =>
// TODO: do the same for opening non-dup invs
tryEnclosingFunctionOrMethod(stmt) match {
case Some(m) =>
val occursInInitMember = m match {
Expand Down

0 comments on commit 5ce6644

Please sign in to comment.