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/ScopeExit.h"
15 #include "llvm/ADT/StringSwitch.h"
16 #include "llvm/ADT/TypeSwitch.h"
20 using namespace circt;
21 using llvm::make_scope_exit;
27 return TypeSwitch<Attribute, LogicalResult>(attr)
28 .Case<DictionaryAttr>([&](
auto attr) {
30 auto guard = make_scope_exit([&] {
json.objectEnd(); });
31 for (
auto subAttr : attr) {
32 json.attributeBegin(subAttr.getName());
33 auto guard = make_scope_exit([&] {
json.attributeEnd(); });
39 .Case<ArrayAttr>([&](
auto attr) {
41 auto guard = make_scope_exit([&] {
json.arrayEnd(); });
42 for (
auto subAttr : attr)
47 .Case<BoolAttr, StringAttr>([&](
auto attr) {
48 json.value(attr.getValue());
51 .Case<IntegerAttr>([&](
auto attr) -> LogicalResult {
54 const auto &apint = attr.getValue();
55 if (!apint.isSignedIntN(64)) {
59 json.value(apint.getSExtValue());
62 .Case<FloatAttr>([&](
auto attr) -> LogicalResult {
63 const auto &apfloat = attr.getValue();
64 json.value(apfloat.convertToDouble());
67 .Default([&](
auto) -> LogicalResult {
76 json::Value &value, json::Path p) {
78 if (
auto a = value.getAsString()) {
82 auto unquotedValue = json::parse(*a);
83 auto err = unquotedValue.takeError();
91 if (!err && !unquotedValue.get().getAsNumber())
94 handleAllErrors(std::move(err), [&](
const json::ParseError &a) {});
99 if (
auto a = value.getAsInteger())
103 if (
auto a = value.getAsNumber())
107 if (
auto a = value.getAsBoolean())
111 if (
auto a = value.getAsNull())
115 if (
auto *a = value.getAsObject()) {
116 NamedAttrList metadata;
124 if (
auto *a = value.getAsArray()) {
125 SmallVector<Attribute> metadata;
126 for (
size_t i = 0, e = (*a).size(); i != e; ++i)
131 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.