Also make ReParentCircuit work on ReferenceTargets

This commit is contained in:
John Wright
2021-07-12 21:46:46 -07:00
parent 479e63c1ce
commit 53a2d698e5

View File

@@ -35,18 +35,26 @@ class ReParentCircuit extends Transform with DependencyAPIMigration {
}
val newAnnotations = newTopName.map({ topName =>
// Update InstanceTargets
def updateInstanceTarget(t: InstanceTarget): Option[InstanceTarget] = {
// Update InstanceTargets and ReferenceTargets
// Yes, these are identical functions, but the copy methods force separate implementations
def updateInstance(t: InstanceTarget): Option[InstanceTarget] = {
val idx = t.path.lastIndexWhere(_._2.value == topName)
if (idx == -1) Some(t.copy(circuit=topName)) else Some(t.copy(circuit=topName, module=topName, path=t.path.drop(idx+1)))
}
def updateReference(t: ReferenceTarget): Option[ReferenceTarget] = {
val idx = t.path.lastIndexWhere(_._2.value == topName)
if (idx == -1) Some(t.copy(circuit=topName)) else Some(t.copy(circuit=topName, module=topName, path=t.path.drop(idx+1)))
}
AnnotationSeq(state.annotations.toSeq.map({
case x: SingleTargetAnnotation[InstanceTarget] if x.target.isInstanceOf[InstanceTarget] =>
updateInstanceTarget(x.target).map(y => x.duplicate(y))
updateInstance(x.target).map(y => x.duplicate(y))
case x: SingleTargetAnnotation[ReferenceTarget] if x.target.isInstanceOf[ReferenceTarget] =>
updateReference(x.target).map(y => x.duplicate(y))
case x: MultiTargetAnnotation =>
val newTargets: Seq[Seq[Option[Target]]] = x.targets.map(_.map({
case y: InstanceTarget => updateInstanceTarget(y)
case y: InstanceTarget => updateInstance(y)
case y: ReferenceTarget => updateReference(y)
case y => Some(y)
}))
if (newTargets.flatten.forall(_.isDefined)) Some(x.duplicate(newTargets.map(_.map(_.get)))) else None