diff options
| author | Chick Markley | 2016-12-06 15:43:45 -0800 |
|---|---|---|
| committer | Jack Koenig | 2016-12-06 15:43:45 -0800 |
| commit | 65b61b4a614748c699982de5ab8072b21d7f9160 (patch) | |
| tree | b93fa5f8c74c562ee43e626e1296f80f7e7e4d69 /src/main/scala/firrtl/annotations | |
| parent | ef4b9e59be86bd83c6c815441cb9c8621d49c89f (diff) | |
Fixes for Annotation serialized/deserialize (#390)
* Fixes for Annotation serialized/deserialize
Made serializer agree with deserializer on text representation
Re-ordered serializations of Named subclasses to be C or C.m or C.m.c where C=circuit, m=module, c=component
Note: component may contain dots
Added serialize deserialize tests to AnnotationSpec
Did some style cleanup on AnnotationSpec
Added explicit return tupe on SimpleTransformSpec#execute
* Make explicit Util.error
remove commented code
* Make Annotation#serialize a nicer format
fix import there and remove new on case class
* In firrtl Driver.execute use annotations passed in through optionsManager#firrtlOptions if nonEmpty
otherwise read the annotations in from an annotations file
Add new option to override this behavior, --force-append-anno-file will append annotations in file
to any that are passed in
A few other style fixes to Driver: remove new with case classes. don't use match when if(boolean) will do
* Added tests of malformed component and circuit names
Diffstat (limited to 'src/main/scala/firrtl/annotations')
| -rw-r--r-- | src/main/scala/firrtl/annotations/Annotation.scala | 15 | ||||
| -rw-r--r-- | src/main/scala/firrtl/annotations/AnnotationYamlProtocol.scala | 48 | ||||
| -rw-r--r-- | src/main/scala/firrtl/annotations/Named.scala | 4 |
3 files changed, 42 insertions, 25 deletions
diff --git a/src/main/scala/firrtl/annotations/Annotation.scala b/src/main/scala/firrtl/annotations/Annotation.scala index 5881001f..2e361833 100644 --- a/src/main/scala/firrtl/annotations/Annotation.scala +++ b/src/main/scala/firrtl/annotations/Annotation.scala @@ -3,19 +3,26 @@ package firrtl package annotations -import firrtl.ir._ - case class AnnotationException(message: String) extends Exception(message) final case class Annotation(target: Named, transform: Class[_ <: Transform], value: String) { val targetString: String = target.serialize val transformClass: String = transform.getName - def serialize: String = this.toString + + /** + * This serialize is basically a pretty printer, actual serialization is handled by + * AnnotationYamlProtocol + * @return a nicer string than the raw case class default + */ + def serialize: String = { + s"Annotation(${target.serialize},${transform.getCanonicalName},$value)" + } + def update(tos: Seq[Named]): Seq[Annotation] = { check(target, tos, this) propagate(target, tos, duplicate) } def propagate(from: Named, tos: Seq[Named], dup: Named=>Annotation): Seq[Annotation] = tos.map(dup(_)) def check(from: Named, tos: Seq[Named], which: Annotation): Unit = {} - def duplicate(n: Named) = new Annotation(n, transform, value) + def duplicate(n: Named) = Annotation(n, transform, value) } diff --git a/src/main/scala/firrtl/annotations/AnnotationYamlProtocol.scala b/src/main/scala/firrtl/annotations/AnnotationYamlProtocol.scala index 5da00282..9018d494 100644 --- a/src/main/scala/firrtl/annotations/AnnotationYamlProtocol.scala +++ b/src/main/scala/firrtl/annotations/AnnotationYamlProtocol.scala @@ -8,30 +8,40 @@ import net.jcazevedo.moultingyaml._ object AnnotationYamlProtocol extends DefaultYamlProtocol { // bottom depends on top implicit object AnnotationYamlFormat extends YamlFormat[Annotation] { - def write(a: Annotation) = - YamlArray( - YamlString(a.targetString), - YamlString(a.transformClass), - YamlString(a.value)) + def write(a: Annotation) = YamlObject( + YamlString("targetString") -> YamlString(a.targetString), + YamlString("transformClass") -> YamlString(a.transformClass), + YamlString("value") -> YamlString(a.value) + ) - def read(value: YamlValue) = { - value.asYamlObject.getFields( - YamlString("targetString"), - YamlString("transformClass"), - YamlString("value")) match { - case Seq( - YamlString(targetString), - YamlString(transformClass), - YamlString(value)) => - new Annotation(toTarget(targetString), Class.forName(transformClass).asInstanceOf[Class[_ <: Transform]], value) - case _ => deserializationError("Color expected") + def read(yamlValue: YamlValue): Annotation = { + try { + yamlValue.asYamlObject.getFields( + YamlString("targetString"), + YamlString("transformClass"), + YamlString("value")) match { + case Seq(YamlString(targetString), YamlString(transformClass), YamlString(value)) => + Annotation( + toTarget(targetString), Class.forName(transformClass).asInstanceOf[Class[_ <: Transform]], value) + case _ => deserializationError("Annotation expected") + } + } + catch { + case annotationException: AnnotationException => + Utils.error( + s"Error: ${annotationException.getMessage} while parsing annotation from yaml\n${yamlValue.prettyPrint}") + case annotationException: FIRRTLException => + Utils.error( + s"Error: ${annotationException.getMessage} while parsing annotation from yaml\n${yamlValue.prettyPrint}") } } - def toTarget(string: String) = string.split('.').toSeq match { + def toTarget(string: String): Named = string.split("""\.""", -1).toSeq match { case Seq(c) => CircuitName(c) case Seq(c, m) => ModuleName(m, CircuitName(c)) - case Nil => error("BAD") - case s => ComponentName(s.drop(2).mkString("."), ModuleName(s(1), CircuitName(s(0)))) + case Nil => Utils.error("BAD") + case s => + val componentString = s.drop(2).mkString(".") + ComponentName(componentString, ModuleName(s.tail.head, CircuitName(s.head))) } } } diff --git a/src/main/scala/firrtl/annotations/Named.scala b/src/main/scala/firrtl/annotations/Named.scala index e9b89e75..4b39c977 100644 --- a/src/main/scala/firrtl/annotations/Named.scala +++ b/src/main/scala/firrtl/annotations/Named.scala @@ -21,11 +21,11 @@ final case class CircuitName(name: String) extends Named { final case class ModuleName(name: String, circuit: CircuitName) extends Named { if(!validModuleName(name)) throw AnnotationException(s"Illegal module name: $name") - def serialize: String = name + "." + circuit.serialize + def serialize: String = circuit.serialize + "." + name } final case class ComponentName(name: String, module: ModuleName) extends Named { if(!validComponentName(name)) throw AnnotationException(s"Illegal component name: $name") def expr: Expression = toExp(name) - def serialize: String = name + "." + module.serialize + def serialize: String = module.serialize + "." + name } |
