Skip to content

Commit 802a5a6

Browse files
author
Steffen Märcker
committed
[DAG-copy] Implement deep-copying the AST without unfolding the DAG
After certain replacements, e.g., formula substitution, the AST becomes a directed acyclic graph. On copying wewant to preserve this structure. The identification of duplicates is non-local since duplicates may appear everywhere in the AST/DAG. The implementation introduces a new visitor to keep track of duplicate nodes and makes copying a two-step process: 1. Cloning a node and its internal container types via Object::clone 2. Deep-copying AST child nodes using the visitor
1 parent 7be5c42 commit 802a5a6

53 files changed

Lines changed: 742 additions & 509 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

prism/src/parser/ast/ASTElement.java

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,25 @@ public String getEndString()
186186
/**
187187
* Perform a deep copy.
188188
*/
189-
public abstract ASTElement deepCopy();
189+
public ASTElement deepCopy()
190+
{
191+
try {
192+
return new DeepCopy().copy(this);
193+
} catch (PrismLangException e) {
194+
throw new Error(e);
195+
}
196+
}
197+
198+
/**
199+
* Perform a deep copy of all internal ASTElements using a deep copy visitor.
200+
* This method is usually called after {@code clone()} and must return the receiver.
201+
*
202+
* @param copier the copy visitor
203+
* @returns the receiver with deep-copied subcomponents
204+
* @throws PrismLangException
205+
* @see #clone()
206+
*/
207+
public abstract ASTElement deepCopy(DeepCopy copier) throws PrismLangException;
190208

191209
/**
192210
* Perform a shallow copy of the receiver and

prism/src/parser/ast/Command.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -137,19 +137,14 @@ public String toString()
137137
s += "] " + guard + " -> " + updates;
138138
return s;
139139
}
140-
141-
/**
142-
* Perform a deep copy.
143-
*/
144-
public ASTElement deepCopy()
140+
141+
@Override
142+
public Command deepCopy(DeepCopy copier) throws PrismLangException
145143
{
146-
Command ret = new Command();
147-
ret.setSynch(getSynch());
148-
ret.setSynchIndex(getSynchIndex());
149-
ret.setGuard(getGuard().deepCopy());
150-
ret.setUpdates((Updates)getUpdates().deepCopy());
151-
ret.setPosition(this);
152-
return ret;
144+
guard = copier.copy(guard);
145+
updates = copier.copy(updates);
146+
147+
return this;
153148
}
154149

155150
@Override

prism/src/parser/ast/ConstantList.java

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -429,24 +429,17 @@ public String toString()
429429

430430
return s;
431431
}
432-
433-
/**
434-
* Perform a deep copy.
435-
*/
436-
public ASTElement deepCopy()
432+
433+
@Override
434+
public ConstantList deepCopy(DeepCopy copier) throws PrismLangException
437435
{
438-
int i, n;
439-
ConstantList ret = new ConstantList();
440-
n = size();
441-
for (i = 0; i < n; i++) {
442-
Expression constantNew = (getConstant(i) == null) ? null : getConstant(i).deepCopy();
443-
ret.addConstant((ExpressionIdent)getConstantNameIdent(i).deepCopy(), constantNew, getConstantType(i));
444-
}
445-
ret.setPosition(this);
446-
return ret;
436+
copier.copyAll(constants);
437+
copier.copyAll(nameIdents);
438+
439+
return this;
447440
}
448441

449-
@SuppressWarnings("unchecked")
442+
@SuppressWarnings("unchecked")
450443
@Override
451444
public ConstantList clone()
452445
{

prism/src/parser/ast/Declaration.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -138,17 +138,13 @@ public String toString()
138138
return s;
139139
}
140140

141-
/**
142-
* Perform a deep copy.
143-
*/
144141
@Override
145-
public ASTElement deepCopy()
142+
public Declaration deepCopy(DeepCopy copier) throws PrismLangException
146143
{
147-
Declaration ret = new Declaration(getName(), (DeclarationType)getDeclType().deepCopy());
148-
if (getStart() != null)
149-
ret.setStart(getStart().deepCopy());
150-
ret.setPosition(this);
151-
return ret;
144+
declType = copier.copy(declType);
145+
start = copier.copy(start);
146+
147+
return this;
152148
}
153149

154150
@Override

prism/src/parser/ast/DeclarationArray.java

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import parser.type.*;
3131
import parser.visitor.ASTVisitor;
32+
import parser.visitor.DeepCopy;
3233
import prism.PrismLangException;
3334

3435
public class DeclarationArray extends DeclarationType
@@ -121,18 +122,14 @@ public String toString()
121122
return "array [" + low + ".." + high + "] of " + subtype;
122123
}
123124

124-
/**
125-
* Perform a deep copy.
126-
*/
127125
@Override
128-
public ASTElement deepCopy()
126+
public DeclarationArray deepCopy(DeepCopy copier) throws PrismLangException
129127
{
130-
Expression lowCopy = (low == null) ? null : low.deepCopy();
131-
Expression highCopy = (high == null) ? null : high.deepCopy();
132-
DeclarationType subtypeCopy = (DeclarationType) subtype.deepCopy();
133-
DeclarationArray ret = new DeclarationArray(lowCopy, highCopy, subtypeCopy);
134-
ret.setPosition(this);
135-
return ret;
128+
low = copier.copy(low);
129+
high = copier.copy(high);
130+
subtype = copier.copy(subtype);
131+
132+
return this;
136133
}
137134

138135
@Override

prism/src/parser/ast/DeclarationBool.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import parser.type.TypeBool;
3131
import parser.visitor.ASTVisitor;
32+
import parser.visitor.DeepCopy;
3233
import prism.PrismLangException;
3334

3435
public class DeclarationBool extends DeclarationType
@@ -75,14 +76,10 @@ public String toString()
7576
return "bool";
7677
}
7778

78-
/**
79-
* Perform a deep copy.
80-
*/
81-
public ASTElement deepCopy()
79+
@Override
80+
public DeclarationBool deepCopy(DeepCopy copier) throws PrismLangException
8281
{
83-
DeclarationBool ret = new DeclarationBool();
84-
ret.setPosition(this);
85-
return ret;
82+
return this;
8683
}
8784

8885
@Override

prism/src/parser/ast/DeclarationClock.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import parser.type.*;
3030
import parser.visitor.ASTVisitor;
31+
import parser.visitor.DeepCopy;
3132
import prism.PrismLangException;
3233

3334
public class DeclarationClock extends DeclarationType
@@ -78,15 +79,10 @@ public String toString()
7879
return "clock";
7980
}
8081

81-
/**
82-
* Perform a deep copy.
83-
*/
8482
@Override
85-
public ASTElement deepCopy()
83+
public DeclarationClock deepCopy(DeepCopy copier) throws PrismLangException
8684
{
87-
DeclarationClock ret = new DeclarationClock();
88-
ret.setPosition(this);
89-
return ret;
85+
return this;
9086
}
9187

9288
@Override

prism/src/parser/ast/DeclarationInt.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929

3030
import parser.type.*;
3131
import parser.visitor.ASTVisitor;
32+
import parser.visitor.DeepCopy;
3233
import prism.PrismLangException;
3334

3435
public class DeclarationInt extends DeclarationType
@@ -106,17 +107,13 @@ public String toString()
106107
return "[" + low + ".." + high + "]";
107108
}
108109

109-
/**
110-
* Perform a deep copy.
111-
*/
112110
@Override
113-
public ASTElement deepCopy()
111+
public DeclarationInt deepCopy(DeepCopy copier) throws PrismLangException
114112
{
115-
Expression lowCopy = (low == null) ? null : low.deepCopy();
116-
Expression highCopy = (high == null) ? null : high.deepCopy();
117-
DeclarationInt ret = new DeclarationInt(lowCopy, highCopy);
118-
ret.setPosition(this);
119-
return ret;
113+
low = copier.copy(low);
114+
high = copier.copy(high);
115+
116+
return this;
120117
}
121118

122119
@Override

prism/src/parser/ast/DeclarationIntUnbounded.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
import parser.type.*;
3030
import parser.visitor.ASTVisitor;
31+
import parser.visitor.DeepCopy;
3132
import prism.PrismLangException;
3233

3334
public class DeclarationIntUnbounded extends DeclarationType
@@ -78,15 +79,10 @@ public String toString()
7879
return "int";
7980
}
8081

81-
/**
82-
* Perform a deep copy.
83-
*/
8482
@Override
85-
public ASTElement deepCopy()
83+
public DeclarationIntUnbounded deepCopy(DeepCopy copier) throws PrismLangException
8684
{
87-
DeclarationIntUnbounded ret = new DeclarationIntUnbounded();
88-
ret.setPosition(this);
89-
return ret;
85+
return this;
9086
}
9187

9288
@Override

prism/src/parser/ast/Expression.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,11 @@ public String getResultName()
7878

7979
// Override version of deepCopy() from superclass ASTElement (to reduce casting).
8080

81-
/**
82-
* Perform a deep copy.
83-
*/
84-
public abstract Expression deepCopy();
81+
@Override
82+
public Expression deepCopy()
83+
{
84+
return (Expression) super.deepCopy();
85+
}
8586

8687
// Utility methods:
8788

0 commit comments

Comments
 (0)