aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/firrtl/annotations/JsonProtocol.scala
blob: fe35c77da076bbb521a6d7d7e935ecb11db30244 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
// SPDX-License-Identifier: Apache-2.0

package firrtl
package annotations

import firrtl.ir._
import firrtl.stage.AllowUnrecognizedAnnotations
import logger.LazyLogging

import scala.util.{Failure, Success, Try}

import org.json4s._
import org.json4s.native.JsonMethods._
import org.json4s.native.Serialization
import org.json4s.native.Serialization.{read, write, writePretty}

import scala.collection.mutable

trait HasSerializationHints {
  // For serialization of complicated constructor arguments, let the annotation
  // writer specify additional type hints for relevant classes that might be
  // contained within
  def typeHints: Seq[Class[_]]
}

/** Wrapper [[Annotation]] for Annotations that cannot be serialized */
case class UnserializeableAnnotation(error: String, content: String) extends NoTargetAnnotation

object JsonProtocol extends LazyLogging {
  private val GetClassPattern = "[^']*'([^']+)'.*".r

  class TransformClassSerializer
      extends CustomSerializer[Class[_ <: Transform]](format =>
        (
          { case JString(s) => Class.forName(s).asInstanceOf[Class[_ <: Transform]] },
          { case x: Class[_] => JString(x.getName) }
        )
      )
  // TODO Reduce boilerplate?
  class NamedSerializer
      extends CustomSerializer[Named](format =>
        (
          { case JString(s) => AnnotationUtils.toNamed(s) },
          { case named: Named => JString(named.serialize) }
        )
      )
  class CircuitNameSerializer
      extends CustomSerializer[CircuitName](format =>
        (
          { case JString(s) => AnnotationUtils.toNamed(s).asInstanceOf[CircuitName] },
          { case named: CircuitName => JString(named.serialize) }
        )
      )
  class ModuleNameSerializer
      extends CustomSerializer[ModuleName](format =>
        (
          { case JString(s) => AnnotationUtils.toNamed(s).asInstanceOf[ModuleName] },
          { case named: ModuleName => JString(named.serialize) }
        )
      )
  class ComponentNameSerializer
      extends CustomSerializer[ComponentName](format =>
        (
          { case JString(s) => AnnotationUtils.toNamed(s).asInstanceOf[ComponentName] },
          { case named: ComponentName => JString(named.serialize) }
        )
      )
  class TransformSerializer
      extends CustomSerializer[Transform](format =>
        (
          {
            case JString(s) =>
              try {
                Class.forName(s).asInstanceOf[Class[_ <: Transform]].newInstance()
              } catch {
                case e: java.lang.InstantiationException =>
                  throw new FirrtlInternalException(
                    "NoSuchMethodException during construction of serialized Transform. Is your Transform an inner class?",
                    e
                  )
                case t: Throwable => throw t
              }
          },
          { case x: Transform => JString(x.getClass.getName) }
        )
      )
  class LoadMemoryFileTypeSerializer
      extends CustomSerializer[MemoryLoadFileType](format =>
        (
          { case JString(s) => MemoryLoadFileType.deserialize(s) },
          { case named: MemoryLoadFileType => JString(named.serialize) }
        )
      )

  class TargetSerializer
      extends CustomSerializer[Target](format =>
        (
          { case JString(s) => Target.deserialize(s) },
          { case named: Target => JString(named.serialize) }
        )
      )
  class GenericTargetSerializer
      extends CustomSerializer[GenericTarget](format =>
        (
          { case JString(s) => Target.deserialize(s).asInstanceOf[GenericTarget] },
          { case named: GenericTarget => JString(named.serialize) }
        )
      )
  class CircuitTargetSerializer
      extends CustomSerializer[CircuitTarget](format =>
        (
          { case JString(s) => Target.deserialize(s).asInstanceOf[CircuitTarget] },
          { case named: CircuitTarget => JString(named.serialize) }
        )
      )
  class ModuleTargetSerializer
      extends CustomSerializer[ModuleTarget](format =>
        (
          { case JString(s) => Target.deserialize(s).asInstanceOf[ModuleTarget] },
          { case named: ModuleTarget => JString(named.serialize) }
        )
      )
  class InstanceTargetSerializer
      extends CustomSerializer[InstanceTarget](format =>
        (
          { case JString(s) => Target.deserialize(s).asInstanceOf[InstanceTarget] },
          { case named: InstanceTarget => JString(named.serialize) }
        )
      )
  class ReferenceTargetSerializer
      extends CustomSerializer[ReferenceTarget](format =>
        (
          { case JString(s) => Target.deserialize(s).asInstanceOf[ReferenceTarget] },
          { case named: ReferenceTarget => JString(named.serialize) }
        )
      )
  class IsModuleSerializer
      extends CustomSerializer[IsModule](format =>
        (
          { case JString(s) => Target.deserialize(s).asInstanceOf[IsModule] },
          { case named: IsModule => JString(named.serialize) }
        )
      )
  class IsMemberSerializer
      extends CustomSerializer[IsMember](format =>
        (
          { case JString(s) => Target.deserialize(s).asInstanceOf[IsMember] },
          { case named: IsMember => JString(named.serialize) }
        )
      )
  class CompleteTargetSerializer
      extends CustomSerializer[CompleteTarget](format =>
        (
          { case JString(s) => Target.deserialize(s).asInstanceOf[CompleteTarget] },
          { case named: CompleteTarget => JString(named.serialize) }
        )
      )
  // FIRRTL Serializers
  class TypeSerializer
      extends CustomSerializer[Type](format =>
        (
          { case JString(s) => Parser.parseType(s) },
          { case tpe: Type => JString(tpe.serialize) }
        )
      )
  class ExpressionSerializer
      extends CustomSerializer[Expression](format =>
        (
          { case JString(s) => Parser.parseExpression(s) },
          { case expr: Expression => JString(expr.serialize) }
        )
      )
  class StatementSerializer
      extends CustomSerializer[Statement](format =>
        (
          { case JString(s) => Parser.parseStatement(s) },
          { case statement: Statement => JString(statement.serialize) }
        )
      )
  class PortSerializer
      extends CustomSerializer[Port](format =>
        (
          { case JString(s) => Parser.parsePort(s) },
          { case port: Port => JString(port.serialize) }
        )
      )
  class DefModuleSerializer
      extends CustomSerializer[DefModule](format =>
        (
          { case JString(s) => Parser.parseDefModule(s) },
          { case mod: DefModule => JString(mod.serialize) }
        )
      )
  class CircuitSerializer
      extends CustomSerializer[Circuit](format =>
        (
          { case JString(s) => Parser.parse(s) },
          { case cir: Circuit => JString(cir.serialize) }
        )
      )
  class InfoSerializer
      extends CustomSerializer[Info](format =>
        (
          { case JString(s) => Parser.parseInfo(s) },
          { case info: Info => JString(info.serialize) }
        )
      )
  class GroundTypeSerializer
      extends CustomSerializer[GroundType](format =>
        (
          { case JString(s) => Parser.parseType(s).asInstanceOf[GroundType] },
          { case tpe: GroundType => JString(tpe.serialize) }
        )
      )

  class UnrecognizedAnnotationSerializer
      extends CustomSerializer[JObject](format =>
        (
          { case JObject(s) => JObject(s) },
          { case UnrecognizedAnnotation(underlying) => underlying }
        )
      )

  /** Construct Json formatter for annotations */
  def jsonFormat(tags: Seq[Class[_]]) = {
    Serialization.formats(FullTypeHints(tags.toList, "class")) +
      new TransformClassSerializer + new NamedSerializer + new CircuitNameSerializer +
      new ModuleNameSerializer + new ComponentNameSerializer + new TargetSerializer +
      new GenericTargetSerializer + new CircuitTargetSerializer + new ModuleTargetSerializer +
      new InstanceTargetSerializer + new ReferenceTargetSerializer + new TransformSerializer +
      new LoadMemoryFileTypeSerializer + new IsModuleSerializer + new IsMemberSerializer +
      new CompleteTargetSerializer + new TypeSerializer + new ExpressionSerializer +
      new StatementSerializer + new PortSerializer + new DefModuleSerializer +
      new CircuitSerializer + new InfoSerializer + new GroundTypeSerializer +
      new UnrecognizedAnnotationSerializer
  }

  /** Serialize annotations to a String for emission */
  def serialize(annos: Seq[Annotation]): String = serializeTry(annos).get

  private def findUnserializeableAnnos(
    annos: Seq[Annotation]
  )(
    implicit formats: Formats
  ): Seq[(Annotation, Throwable)] =
    annos.map(a => a -> Try(write(a))).collect { case (a, Failure(e)) => (a, e) }

  private def getTags(annos: Seq[Annotation]): Seq[Class[_]] =
    annos
      .flatMap({
        case anno: HasSerializationHints => anno.getClass +: anno.typeHints
        case anno => Seq(anno.getClass)
      })
      .distinct

  def serializeTry(annos: Seq[Annotation]): Try[String] = {
    val tags = getTags(annos)

    implicit val formats = jsonFormat(tags)
    Try(writePretty(annos)).recoverWith {
      case e: org.json4s.MappingException =>
        val badAnnos = findUnserializeableAnnos(annos)
        Failure(if (badAnnos.isEmpty) e else UnserializableAnnotationException(badAnnos))
    }
  }

  /** Serialize annotations to JSON while wrapping unserializeable ones with [[UnserializeableAnnotation]]
    *
    * @note this is slower than standard serialization
    */
  def serializeRecover(annos: Seq[Annotation]): String = {
    val tags = classOf[UnserializeableAnnotation] +: getTags(annos)
    implicit val formats = jsonFormat(tags)

    val safeAnnos = annos.map { anno =>
      Try(write(anno)) match {
        case Success(_) => anno
        case Failure(e) => UnserializeableAnnotation(e.getMessage, anno.toString)
      }
    }
    writePretty(safeAnnos)
  }

  /** Deserialize JSON input into a Seq[Annotation]
    *
    * @param in JsonInput, can be file or string
    * @param allowUnrecognizedAnnotations is set to true if command line contains flag to allow this behavior
    * @return
    */
  def deserialize(in: JsonInput, allowUnrecognizedAnnotations: Boolean = false): Seq[Annotation] = {
    deserializeTry(in, allowUnrecognizedAnnotations).get
  }

  def deserializeTry(in: JsonInput, allowUnrecognizedAnnotations: Boolean = false): Try[Seq[Annotation]] = Try {
    val parsed = parse(in)
    val annos = parsed match {
      case JArray(objs) => objs
      case x =>
        throw new InvalidAnnotationJSONException(
          s"Annotations must be serialized as a JArray, got ${x.getClass.getName} instead!"
        )
    }

    /* Tries to extract class name from the mapping exception */
    def getAnnotationNameFromMappingException(mappingException: MappingException): String = {
      mappingException.getMessage match {
        case GetClassPattern(name) => name
        case other                 => other
      }
    }

    // Recursively gather typeHints by pulling the "class" field from JObjects
    // Json4s should emit this as the first field in all serialized classes
    // Setting requireClassField mandates that all JObjects must provide a typeHint,
    // this used on the first invocation to check all annotations do so
    def findTypeHints(classInst: Seq[JValue], requireClassField: Boolean = false): Seq[String] = classInst
      .flatMap({
        case JObject(fields) =>
          val hint = fields.collectFirst { case ("class", JString(name)) => name }
          if (requireClassField && hint.isEmpty)
            throw new InvalidAnnotationJSONException(s"Expected field 'class' not found! $fields")
          hint ++: findTypeHints(fields.map(_._2))
        case JArray(arr) => findTypeHints(arr)
        case _           => Seq()
      })
      .distinct

    // I don't much like this var here, but it has made it much simpler
    // to maintain backward compatibility with the exception test structure
    var classNotFoundBuildingLoaded = false
    val classes = findTypeHints(annos, true)
    val loaded = classes.flatMap { x =>
      (try {
        Some(Class.forName(x))
      } catch {
        case _: java.lang.ClassNotFoundException =>
          classNotFoundBuildingLoaded = true
          None
      }): Option[Class[_]]
    }
    implicit val formats = jsonFormat(loaded)
    try {
      read[List[Annotation]](in)
    } catch {
      case e: org.json4s.MappingException =>
        // If we get here, the build `read` failed to process an annotation
        // So we will map the annos one a time, wrapping the JSON of the unrecognized annotations
        val exceptionList = new mutable.ArrayBuffer[String]()
        val firrtlAnnos = annos.map { jsonAnno =>
          try {
            jsonAnno.extract[Annotation]
          } catch {
            case mappingException: org.json4s.MappingException =>
              exceptionList += getAnnotationNameFromMappingException(mappingException)
              UnrecognizedAnnotation(jsonAnno)
          }
        }

        if (firrtlAnnos.contains(AllowUnrecognizedAnnotations) || allowUnrecognizedAnnotations) {
          firrtlAnnos
        } else {
          logger.error(
            "Annotation parsing found unrecognized annotations\n" +
              "This error can be ignored with an AllowUnrecognizedAnnotationsAnnotation" +
              " or command line flag --allow-unrecognized-annotations\n" +
              exceptionList.mkString("\n")
          )
          if (classNotFoundBuildingLoaded) {
            val distinctProblems = exceptionList.distinct
            val problems = distinctProblems.take(10).mkString(", ")
            val dots = if (distinctProblems.length > 10) {
              ", ..."
            } else {
              ""
            }
            throw UnrecogizedAnnotationsException(s"($problems$dots)")
          } else {
            throw e
          } // throw the mapping exception
        }
    }
  }.recoverWith {
    // Translate some generic errors to specific ones
    case e: java.lang.ClassNotFoundException =>
      Failure(AnnotationClassNotFoundException(e.getMessage))
    // Eat the stack traces of json4s exceptions
    case e @ (_: org.json4s.ParserUtil.ParseException | _: org.json4s.MappingException) =>
      Failure(InvalidAnnotationJSONException(e.getMessage))
  }.recoverWith { // If the input is a file, wrap in InvalidAnnotationFileException
    case e: UnrecogizedAnnotationsException =>
      in match {
        case FileInput(file) =>
          Failure(InvalidAnnotationFileException(file, e))
        case _ =>
          Failure(e)
      }
    case e: FirrtlUserException =>
      in match {
        case FileInput(file) =>
          Failure(InvalidAnnotationFileException(file, e))
        case _ => Failure(e)
      }
  }
}