10 #include "mlir/IR/BuiltinAttributes.h"
11 #include "mlir/IR/BuiltinTypes.h"
12 #include "mlir/IR/Diagnostics.h"
13 #include "mlir/IR/OperationSupport.h"
14 #include "llvm/ADT/StringSwitch.h"
15 #include "llvm/ADT/TypeSwitch.h"
19 using namespace circt;
25 return TypeSwitch<Attribute, LogicalResult>(attr)
26 .Case<DictionaryAttr>([&](
auto attr) {
28 for (
auto subAttr : attr) {
29 json.attributeBegin(subAttr.getName());
37 .Case<ArrayAttr>([&](
auto attr) {
39 for (
auto subAttr : attr)
45 .Case<BoolAttr, StringAttr>([&](
auto attr) {
46 json.value(attr.getValue());
49 .Case<IntegerAttr>([&](
auto attr) -> LogicalResult {
52 const auto &apint = attr.getValue();
53 if (!apint.isSignedIntN(64))
55 json.value(apint.getSExtValue());
58 .Case<FloatAttr>([&](
auto attr) -> LogicalResult {
59 const auto &apfloat = attr.getValue();
60 json.value(apfloat.convertToDouble());
63 .Default([&](
auto) -> LogicalResult {
return failure(); });
69 json::Value &value, json::Path p) {
71 if (
auto a = value.getAsString()) {
75 auto unquotedValue = json::parse(*a);
76 auto err = unquotedValue.takeError();
84 if (!err && !unquotedValue.get().getAsNumber())
87 handleAllErrors(std::move(err), [&](
const json::ParseError &a) {});
92 if (
auto a = value.getAsInteger())
96 if (
auto a = value.getAsNumber())
100 if (
auto a = value.getAsBoolean())
104 if (
auto a = value.getAsNull())
108 if (
auto *a = value.getAsObject()) {
109 NamedAttrList metadata;
117 if (
auto *a = value.getAsArray()) {
118 SmallVector<Attribute> metadata;
119 for (
size_t i = 0, e = (*a).size(); i != e; ++i)
124 llvm_unreachable(
"Impossible unhandled JSON type");
Direction get(bool isOutput)
Returns an output direction if isOutput is true, otherwise returns an input direction.
The InstanceGraph op interface, see InstanceGraphInterface.td for more details.
Attribute convertJSONToAttribute(MLIRContext *context, llvm::json::Value &value, llvm::json::Path p)
Convert arbitrary JSON to an MLIR Attribute.
LogicalResult convertAttributeToJSON(llvm::json::OStream &json, Attribute attr)
Convert a simple attribute to JSON.