15#include "mlir/IR/DialectImplementation.h"
16#include "llvm/ADT/TypeSwitch.h"
20static mlir::ParseResult
22 mlir::FailureOr<llvm::APFloat> &value,
23 cir::CIRFPTypeInterface fpType);
26 mlir::IntegerAttr &value);
30#define GET_ATTRDEF_CLASSES
31#include "clang/CIR/Dialect/IR/CIROpsAttributes.cpp.inc"
40Attribute CIRDialect::parseAttribute(DialectAsmParser &parser,
42 llvm::SMLoc typeLoc = parser.getCurrentLocation();
43 llvm::StringRef mnemonic;
45 OptionalParseResult parseResult =
46 generatedAttributeParser(parser, &mnemonic, type, genAttr);
47 if (parseResult.has_value())
49 parser.emitError(typeLoc,
"unknown attribute in CIR dialect");
53void CIRDialect::printAttribute(Attribute attr, DialectAsmPrinter &os)
const {
54 if (failed(generatedAttributePrinter(attr, os)))
55 llvm_unreachable(
"unexpected CIR type kind");
64static ParseResult
parseConstPtr(AsmParser &parser, mlir::IntegerAttr &value) {
66 if (parser.parseOptionalKeyword(
"null").succeeded()) {
67 value = mlir::IntegerAttr::get(
68 mlir::IntegerType::get(parser.getContext(), 64), 0);
72 return parser.parseAttribute(value);
86Attribute IntAttr::parse(AsmParser &parser,
Type odsType) {
89 if (!mlir::isa<IntType>(odsType))
91 auto type = mlir::cast<IntType>(odsType);
94 if (parser.parseLess())
98 if (
type.isSigned()) {
100 if (parser.parseInteger(value)) {
101 parser.emitError(parser.getCurrentLocation(),
"expected integer value");
103 apValue = mlir::APInt(
type.getWidth(), value,
type.isSigned(),
105 if (apValue.getSExtValue() != value)
106 parser.emitError(parser.getCurrentLocation(),
107 "integer value too large for the given type");
111 if (parser.parseInteger(value)) {
112 parser.emitError(parser.getCurrentLocation(),
"expected integer value");
114 apValue = mlir::APInt(
type.getWidth(), value,
type.isSigned(),
116 if (apValue.getZExtValue() != value)
117 parser.emitError(parser.getCurrentLocation(),
118 "integer value too large for the given type");
123 if (parser.parseGreater())
126 return IntAttr::get(type, apValue);
129void IntAttr::print(AsmPrinter &printer)
const {
130 auto type = mlir::cast<IntType>(getType());
133 printer << getSInt();
135 printer << getUInt();
139LogicalResult IntAttr::verify(function_ref<InFlightDiagnostic()> emitError,
140 Type type, APInt value) {
141 if (!mlir::isa<IntType>(type)) {
142 emitError() <<
"expected 'simple.int' type";
146 auto intType = mlir::cast<IntType>(type);
147 if (value.getBitWidth() != intType.getWidth()) {
148 emitError() <<
"type and value bitwidth mismatch: " << intType.getWidth()
149 <<
" != " << value.getBitWidth();
165 FailureOr<APFloat> &value,
166 CIRFPTypeInterface fpType) {
168 APFloat parsedValue(0.0);
169 if (parser.parseFloat(fpType.getFloatSemantics(), parsedValue))
172 value.emplace(parsedValue);
176FPAttr FPAttr::getZero(
Type type) {
179 mlir::cast<CIRFPTypeInterface>(type).getFloatSemantics()));
182LogicalResult FPAttr::verify(function_ref<InFlightDiagnostic()> emitError,
183 CIRFPTypeInterface fpType, APFloat value) {
184 if (APFloat::SemanticsToEnum(fpType.getFloatSemantics()) !=
185 APFloat::SemanticsToEnum(value.getSemantics())) {
186 emitError() <<
"floating-point semantics mismatch";
197void CIRDialect::registerAttributes() {
199#define GET_ATTRDEF_LIST
200#include "clang/CIR/Dialect/IR/CIROpsAttributes.cpp.inc"
static void printConstPtr(mlir::AsmPrinter &p, mlir::IntegerAttr value)
static mlir::ParseResult parseConstPtr(mlir::AsmParser &parser, mlir::IntegerAttr &value)
static void printFloatLiteral(mlir::AsmPrinter &p, llvm::APFloat value, mlir::Type ty)
static mlir::ParseResult parseFloatLiteral(mlir::AsmParser &parser, mlir::FailureOr< llvm::APFloat > &value, cir::CIRFPTypeInterface fpType)
const internal::VariadicAllOfMatcher< Type > type
Matches Types in the clang AST.