Loading [MathJax]/extensions/tex2jax.js
CIRCT 22.0.0git
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
OMAttributes.cpp
Go to the documentation of this file.
1//===- OMAttributes.cpp - Object Model attribute definitions --------------===//
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// This file contains the Object Model attribute definitions.
10//
11//===----------------------------------------------------------------------===//
12
16#include "mlir/IR/Builders.h"
17#include "mlir/IR/DialectImplementation.h"
18#include "llvm/ADT/TypeSwitch.h"
19
20using namespace mlir;
21using namespace circt::om;
22
23#define GET_ATTRDEF_CLASSES
24#include "circt/Dialect/OM/OMAttributes.cpp.inc"
25
26Type circt::om::ReferenceAttr::getType() {
27 return ReferenceType::get(getContext());
28}
29
30Type circt::om::SymbolRefAttr::getType() {
31 return SymbolRefType::get(getContext());
32}
33
34Type circt::om::ListAttr::getType() {
35 return ListType::get(getContext(), getElementType());
36}
37
38circt::om::SymbolRefAttr circt::om::SymbolRefAttr::get(mlir::Operation *op) {
39 return om::SymbolRefAttr::get(op->getContext(),
40 mlir::FlatSymbolRefAttr::get(op));
41}
42
43circt::om::SymbolRefAttr
44circt::om::SymbolRefAttr::get(mlir::StringAttr symName) {
45 return om::SymbolRefAttr::get(symName.getContext(),
46 mlir::FlatSymbolRefAttr::get(symName));
47}
48
49LogicalResult
50circt::om::ListAttr::verify(function_ref<InFlightDiagnostic()> emitError,
51 mlir::Type elementType, mlir::ArrayAttr elements) {
52 return success(llvm::all_of(elements, [&](mlir::Attribute attr) {
53 auto typedAttr = llvm::dyn_cast<mlir::TypedAttr>(attr);
54 if (!typedAttr) {
55 emitError()
56 << "an element of a list attribute must be a typed attr but got "
57 << attr;
58 return false;
59 }
60 if (typedAttr.getType() != elementType) {
61 emitError() << "an element of a list attribute must have a type "
62 << elementType << " but got " << typedAttr.getType();
63 return false;
64 }
65
66 return true;
67 }));
68}
69
70void PathAttr::print(AsmPrinter &odsPrinter) const {
71 odsPrinter << '[';
72 llvm::interleaveComma(getPath(), odsPrinter, [&](PathElement element) {
73 odsPrinter.printKeywordOrString(element.module);
74 odsPrinter << ':';
75 odsPrinter.printKeywordOrString(element.instance);
76 });
77 odsPrinter << ']';
78}
79
80Attribute PathAttr::parse(AsmParser &odsParser, Type odsType) {
81 auto *context = odsParser.getContext();
82 SmallVector<PathElement> path;
83 if (odsParser.parseCommaSeparatedList(
84 OpAsmParser::Delimiter::Square, [&]() -> ParseResult {
85 std::string module;
86 std::string instance;
87 if (odsParser.parseKeywordOrString(&module) ||
88 odsParser.parseColon() ||
89 odsParser.parseKeywordOrString(&instance))
90 return failure();
91 path.emplace_back(StringAttr::get(context, module),
92 StringAttr::get(context, instance));
93 return success();
94 }))
95 return nullptr;
96 return PathAttr::get(context, path);
97}
98
99LogicalResult PathAttr::verify(function_ref<mlir::InFlightDiagnostic()>,
100 ArrayRef<PathElement> path) {
101 return success();
102}
103
104Type circt::om::IntegerAttr::getType() {
105 return OMIntegerType::get(getContext());
106}
107
108void circt::om::OMDialect::registerAttributes() {
109 addAttributes<
110#define GET_ATTRDEF_LIST
111#include "circt/Dialect/OM/OMAttributes.cpp.inc"
112 >();
113}
MlirType elementType
Definition CHIRRTL.cpp:29
A module name, and the name of an instance inside that module.
mlir::StringAttr mlir::StringAttr instance