Skip to content

Commit

Permalink
Replace AddAssignedVariables with AddAssignedIdentifiers (#945)
Browse files Browse the repository at this point in the history
Replace AddAssignedVariables with AddAssignedIdentifiers, because the
latter can already be computed before resolution, which is useful for
Dafny.
  • Loading branch information
keyboardDrummer authored Sep 11, 2024
1 parent 16c53d9 commit 50ef2fb
Show file tree
Hide file tree
Showing 16 changed files with 586 additions and 573 deletions.
3 changes: 1 addition & 2 deletions Source/Concurrency/CivlTypeChecker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,8 +229,7 @@ private void CheckAsyncToSyncSafety(ActionDecl actionDecl, HashSet<string> refin
Error(callCmd, "unable to eliminate pending async since a global is modified subsequently");
}
}
var assignedVariables = new List<Variable>();
cmd.AddAssignedVariables(assignedVariables);
var assignedVariables = cmd.GetAssignedVariables().ToList();
if (assignedVariables.OfType<GlobalVariable>().Any())
{
modifiedGlobals = true;
Expand Down
8 changes: 4 additions & 4 deletions Source/Core/AST/Absy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2042,6 +2042,7 @@ public DatatypeConstructor(Function func)

public class Function : DeclWithFormals
{
public bool AlwaysRevealed;
public string Comment;

public Expr Body; // Only set if the function is declared with {:inline}
Expand Down Expand Up @@ -2121,7 +2122,7 @@ public override void Emit(TokenTextWriter stream, int level)
stream.WriteLine(this, level, "// " + Comment);
}

stream.Write(this, level, "function ");
stream.Write(this, level, (AlwaysRevealed ? "revealed " : "") + "function ");
EmitAttributes(stream);
if (Body != null && !QKeyValue.FindBoolAttribute(Attributes, "inline"))
{
Expand Down Expand Up @@ -3823,10 +3824,9 @@ public RE()
{
}

public override void AddAssignedVariables(List<Variable> vars)
public override void AddAssignedIdentifiers(List<IdentifierExpr> vars)
{
//Contract.Requires(vars != null);
throw new NotImplementedException();
throw new NotSupportedException();
}
}

Expand Down
73 changes: 34 additions & 39 deletions Source/Core/AST/AbsyCmd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1108,7 +1108,6 @@ void ObjectInvariant()
Contract.Invariant(cce.NonNullElements(Invariants));
}


public WhileCmd(IToken tok, [Captured] Expr guard, List<PredicateCmd> invariants, List<CallCmd> yields, StmtList body)
: base(tok)
{
Expand Down Expand Up @@ -1205,10 +1204,8 @@ public override void Emit(TokenTextWriter stream, int level)
throw new NotImplementedException();
}

public override void AddAssignedVariables(List<Variable> vars)
{
Contract.Requires(vars != null);
throw new NotImplementedException();
public override void AddAssignedIdentifiers(List<IdentifierExpr> vars) {
throw new NotSupportedException();
}
}

Expand Down Expand Up @@ -1352,14 +1349,22 @@ public Cmd(IToken /*!*/ tok)
}

public abstract void Emit(TokenTextWriter /*!*/ stream, int level);
public abstract void AddAssignedVariables(List<Variable> /*!*/ vars);

/// <summary>
/// Should only be called after resolution
/// </summary>
public IEnumerable<Variable> GetAssignedVariables() {
List<IdentifierExpr> ids = new();
AddAssignedIdentifiers(ids);
return ids.Select(id => id.Decl);
}

public abstract void AddAssignedIdentifiers(List<IdentifierExpr> /*!*/ vars);

public void CheckAssignments(TypecheckingContext tc)
{
Contract.Requires(tc != null);
List<Variable> /*!*/
vars = new List<Variable>();
this.AddAssignedVariables(vars);
var /*!*/ vars = GetAssignedVariables().ToList();
foreach (Variable /*!*/ v in vars)
{
Contract.Assert(v != null);
Expand Down Expand Up @@ -1527,9 +1532,8 @@ public override void Emit(TokenTextWriter stream, int level)
public override void Resolve(ResolutionContext rc)
{
}

public override void AddAssignedVariables(List<Variable> vars)
{

public override void AddAssignedIdentifiers(List<IdentifierExpr> vars) {
}

public override void Typecheck(TypecheckingContext tc)
Expand Down Expand Up @@ -1777,12 +1781,10 @@ public override void Typecheck(TypecheckingContext tc)
}
}

public override void AddAssignedVariables(List<Variable> vars)
{
public override void AddAssignedIdentifiers(List<IdentifierExpr> vars) {
foreach (AssignLhs /*!*/ l in Lhss)
{
Contract.Assert(l != null);
vars.Add(l.DeepAssignedVariable);
vars.Add(l.DeepAssignedIdentifier);
}
}

Expand Down Expand Up @@ -2307,9 +2309,8 @@ public Expr Rhs

public IEnumerable<IdentifierExpr> UnpackedLhs => lhs.Args.Cast<IdentifierExpr>();

public override void AddAssignedVariables(List<Variable> vars)
{
lhs.Args.Cast<IdentifierExpr>().ForEach(arg => vars.Add(arg.Decl));
public override void AddAssignedIdentifiers(List<IdentifierExpr> vars) {
lhs.Args.Cast<IdentifierExpr>().ForEach(vars.Add);
}

public override void Emit(TokenTextWriter stream, int level)
Expand Down Expand Up @@ -2423,21 +2424,19 @@ public override void Resolve(ResolutionContext rc)
rc.PopVarContext();
}

public override void AddAssignedVariables(List<Variable> vars)
{
//Contract.Requires(vars != null);
List<Variable> vs = new List<Variable>();
foreach (Cmd cmd in this.Cmds)
public override void AddAssignedIdentifiers(List<IdentifierExpr> vars) {
var vs = new List<IdentifierExpr>();
foreach (Cmd cmd in Cmds)
{
Contract.Assert(cmd != null);
cmd.AddAssignedVariables(vs);
cmd.AddAssignedIdentifiers(vs);
}

var localsSet = new HashSet<Variable>(this.Locals);
foreach (Variable v in vs)
foreach (var v in vs)
{
Contract.Assert(v != null);
if (!localsSet.Contains(v))
Debug.Assert(v.Decl != null);
if (!localsSet.Contains(v.Decl))
{
vars.Add(v);
}
Expand Down Expand Up @@ -2773,11 +2772,10 @@ public override void Typecheck(TypecheckingContext tc)
}
}

public override void AddAssignedVariables(List<Variable> vars)
{
public override void AddAssignedIdentifiers(List<IdentifierExpr> vars) {
foreach (CallCmd callCmd in CallCmds)
{
callCmd.AddAssignedVariables(vars);
callCmd.AddAssignedIdentifiers(vars);
}
}

Expand Down Expand Up @@ -2860,6 +2858,7 @@ void ObjectInvariant()
Contract.Invariant(Expr != null);
}


public PredicateCmd(IToken /*!*/ tok, Expr /*!*/ expr)
: base(tok)
{
Expand Down Expand Up @@ -2900,8 +2899,7 @@ public override void Resolve(ResolutionContext rc)
}
}

public override void AddAssignedVariables(List<Variable> vars)
{
public override void AddAssignedIdentifiers(List<IdentifierExpr> vars) {
}
}

Expand Down Expand Up @@ -3335,8 +3333,7 @@ public override Absy StdDispatch(StandardVisitor visitor)

public class HavocCmd : Cmd
{
private List<IdentifierExpr> /*!*/
_vars;
private List<IdentifierExpr> /*!*/ _vars;

public List<IdentifierExpr> /*!*/ Vars
{
Expand Down Expand Up @@ -3384,13 +3381,11 @@ public override void Resolve(ResolutionContext rc)
}
}

public override void AddAssignedVariables(List<Variable> vars)
{
//Contract.Requires(vars != null);
public override void AddAssignedIdentifiers(List<IdentifierExpr> vars) {
foreach (IdentifierExpr /*!*/ e in this.Vars)
{
Contract.Assert(e != null);
vars.Add(e.Decl);
vars.Add(e);
}
}

Expand Down
10 changes: 5 additions & 5 deletions Source/Core/AST/CallCmd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -540,8 +540,8 @@ private void TypecheckCallCmdInActionDecl(TypecheckingContext tc)
}
}

public override void AddAssignedVariables(List<Variable> vars)
{
public override void AddAssignedIdentifiers(List<IdentifierExpr> vars) {
if (this.IsAsync)
{
return;
Expand All @@ -551,20 +551,20 @@ public override void AddAssignedVariables(List<Variable> vars)
{
if (e != null)
{
vars.Add(e.Decl);
vars.Add(e);
}
}

Contract.Assume(this.Proc != null);
foreach (IdentifierExpr /*!*/ e in this.Proc.Modifies)
{
Contract.Assert(e != null);
vars.Add(e.Decl);
vars.Add(e);
}

if (AssignedAssumptionVariable != null)
{
vars.Add(AssignedAssumptionVariable);
vars.Add(new IdentifierExpr(tok, AssignedAssumptionVariable));
}
}

Expand Down
2 changes: 1 addition & 1 deletion Source/Core/AST/ChangeScope.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public override void Emit(TokenTextWriter stream, int level) {
stream.WriteLine(";");
}

public override void AddAssignedVariables(List<Variable> vars) {
public override void AddAssignedIdentifiers(List<IdentifierExpr> vars) {
}

public override Absy StdDispatch(StandardVisitor visitor) {
Expand Down
3 changes: 1 addition & 2 deletions Source/Core/AST/HideRevealCmd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ public override void Emit(TokenTextWriter stream, int level)
stream.WriteLine(";");
}

public override void AddAssignedVariables(List<Variable> vars)
{
public override void AddAssignedIdentifiers(List<IdentifierExpr> vars) {
}

public override Absy StdDispatch(StandardVisitor visitor)
Expand Down
4 changes: 4 additions & 0 deletions Source/Core/BoogiePL.atg
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,10 @@ Function<.out List<Declaration>/*!*/ ds.>
Expr definition = null;
Expr/*!*/ tmp;
Axiom ax;
bool revealed = false;
.)
[ "revealed" (. revealed = true; .)
]
"function" { Attribute<ref kv> } Ident<out z>
[ TypeParams<out typeParamTok, out typeParams> ]
"("
Expand All @@ -483,6 +486,7 @@ Function<.out List<Declaration>/*!*/ ds.>
}
Function/*!*/ func = new Function(z, z.val, typeParams, arguments,
new Formal(retTyd.tok, retTyd, false, argKv), null, kv);
func.AlwaysRevealed = revealed;
foreach(var axiom in axioms) {
ds.Add(axiom);
func.OtherDefinitionAxioms.Add(axiom);
Expand Down
Loading

0 comments on commit 50ef2fb

Please sign in to comment.