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

fix #1025 and better inline binding origins #1029

Merged
merged 2 commits into from
May 8, 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
17 changes: 11 additions & 6 deletions src/col/vct/col/print/Namer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ case class Namer[G](syntax: Ctx.Syntax) {
private val stack = ScopedStack[Node[G]]()
private val names = mutable.Map[(scala.Any, String, Int), Declaration[G]]()

def nearest(f: PartialFunction[Node[G], Unit]): Option[Node[G]] =
stack.toSeq.find(n => f.isDefinedAt(n))
def nearest(f: PartialFunction[Node[G], Unit]): Seq[Node[G]] =
stack.toSeq.filter(n => f.isDefinedAt(n))

private def nearestClass = nearest {
case _: Class[G] | _: JavaClass[G] | _: VeyMontSeqProg[G] | _: JavaInterface[G] | _: JavaAnnotationInterface[G] => ()
Expand Down Expand Up @@ -73,14 +73,19 @@ case class Namer[G](syntax: Ctx.Syntax) {
if (index == 0) name
else s"$name$index"

def nameKeyed(key: scala.Any, decl: Declaration[G]): Unit = {
def nameKeyed(keys: Seq[scala.Any], decl: Declaration[G]): Unit = {
if(keys.isEmpty) {
// Refuse to name this declaration, it is unclear how to distinguish them.
return
}

var (baseName, index) = unpackName(decl.o.preferredName)

while(names.contains((key, baseName, index))) {
while(keys.exists(key => names.contains((key, baseName, index)))) {
index += 1
}

names((key, baseName, index)) = decl
names((keys.head, baseName, index)) = decl
}

def name(node: Node[G]): Unit = {
Expand All @@ -89,7 +94,7 @@ case class Namer[G](syntax: Ctx.Syntax) {
node match {
case decl: GlobalDeclaration[G] => nameKeyed(nearest { case _: Program[G] => () }, decl)
case decl: ClassDeclaration[G] => nameKeyed(nearestClass, decl)
case decl: ADTDeclaration[G] => nameKeyed(if(syntax == Ctx.Silver) 3 else nearest { case _: AxiomaticDataType[G] => () }, decl)
case decl: ADTDeclaration[G] => nameKeyed(if(syntax == Ctx.Silver) Seq(3) else nearest { case _: AxiomaticDataType[G] => () }, decl)
case decl: ModelDeclaration[G] => nameKeyed(nearest { case _: Model[G] => () }, decl)
case decl: EnumConstant[G] => nameKeyed(nearest { case _: Enum[G] => () }, decl)
case decl: Variable[G] => nameKeyed(nearestVariableScope, decl)
Expand Down
17 changes: 12 additions & 5 deletions src/rewrite/vct/rewrite/InlineApplicables.scala
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ case object InlineApplicables extends RewriterBuilder {
override def inlineContext: String = s"${definition.inlineContext} [inlined from] ${usages.head.o.inlineContext}"
}

case object InlineLetThisOrigin extends Origin {
override def preferredName: String = "self"
override def context: String = "[At let binding for `this`]"
override def inlineContext: String = "[Let binding for `this`]"
override def shortPosition: String = "generated"
}

case class InlineFoldAssertFailed(fold: Fold[_]) extends Blame[AssertFailed] {
override def blame(error: AssertFailed): Unit =
fold.blame.blame(FoldFailed(error.failure, fold))
Expand Down Expand Up @@ -191,26 +198,26 @@ case class InlineApplicables[Pre <: Generation]() extends Rewriter[Pre] with Laz
lazy val obj = {
val instanceApply = apply.asInstanceOf[InstanceApply[Pre]]
val cls = classOwner(instanceApply.ref.decl)
Replacement(ThisObject[Pre](cls.ref), instanceApply.obj)
Replacement(ThisObject[Pre](cls.ref), instanceApply.obj)(InlineLetThisOrigin)
}

lazy val args =
Replacements(for((arg, v) <- apply.args.zip(apply.ref.decl.args))
yield Replacement(v.get, arg))
yield Replacement(v.get, arg)(v.o))

lazy val givenArgs =
Replacements(for((Ref(v), arg) <- apply.asInstanceOf[InvokingNode[Pre]].givenMap)
yield Replacement(v.get, arg))
yield Replacement(v.get, arg)(v.o))

lazy val outArgs = {
val inv = apply.asInstanceOf[AnyMethodInvocation[Pre]]
Replacements(for((out, v) <- inv.outArgs.zip(inv.ref.decl.outArgs))
yield Replacement(v.get, out))
yield Replacement(v.get, out)(v.o))
}

lazy val yields =
Replacements(for((out, Ref(v)) <- apply.asInstanceOf[InvokingNode[Pre]].yields)
yield Replacement(v.get, out))
yield Replacement(v.get, out)(v.o))

val replacements = apply.ref.decl.args.map(_.get).zip(apply.args).toMap[Expr[Pre], Expr[Pre]]
// TODO: consider type arguments and out-arguments and given and yields (oof)
Expand Down