CIRCT  20.0.0git
FIRAnnotations.cpp
Go to the documentation of this file.
1 //===- FIRAnnotations.cpp - FIRRTL Annotation Utilities -------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // Provide utilities related to dealing with FIRRTL Annotations.
10 //
11 //===----------------------------------------------------------------------===//
12 
14 #include "circt/Support/JSON.h"
15 #include "mlir/IR/BuiltinAttributes.h"
16 #include "mlir/IR/OperationSupport.h"
17 
18 namespace json = llvm::json;
19 
20 /// Deserialize a JSON value into FIRRTL Annotations. Annotations are
21 /// represented as a Target-keyed arrays of attributes. The input JSON value is
22 /// checked, at runtime, to be an array of objects. Returns true if successful,
23 /// false if unsuccessful.
25  json::Value &value, SmallVectorImpl<Attribute> &annotations,
26  json::Path path, MLIRContext *context) {
27 
28  // The JSON value must be an array of objects. Anything else is reported as
29  // invalid.
30  auto *array = value.getAsArray();
31  if (!array) {
32  path.report(
33  "Expected annotations to be an array, but found something else.");
34  return false;
35  }
36 
37  // Build an array of annotations.
38  for (size_t i = 0, e = (*array).size(); i != e; ++i) {
39  auto *object = (*array)[i].getAsObject();
40  auto p = path.index(i);
41  if (!object) {
42  p.report("Expected annotations to be an array of objects, but found an "
43  "array of something else.");
44  return false;
45  }
46 
47  // Build up the Attribute to represent the Annotation
48  NamedAttrList metadata;
49 
50  for (auto field : *object) {
51  if (auto value = convertJSONToAttribute(context, field.second, p)) {
52  metadata.append(field.first, value);
53  continue;
54  }
55  return false;
56  }
57 
58  annotations.push_back(DictionaryAttr::get(context, metadata));
59  }
60 
61  return true;
62 }
Direction get(bool isOutput)
Returns an output direction if isOutput is true, otherwise returns an input direction.
Definition: CalyxOps.cpp:55
bool importAnnotationsFromJSONRaw(llvm::json::Value &value, SmallVectorImpl< Attribute > &annotations, llvm::json::Path path, MLIRContext *context)
Deserialize a JSON value into FIRRTL Annotations.
Attribute convertJSONToAttribute(MLIRContext *context, llvm::json::Value &value, llvm::json::Path p)
Convert arbitrary JSON to an MLIR Attribute.