1 //===- TestAttributes.cpp - MLIR Test Dialect Attributes --------*- C++ -*-===//
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 attributes defined by the TestDialect for testing various
10 // features of MLIR.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "TestAttributes.h"
15 #include "TestDialect.h"
16 #include "mlir/IR/Builders.h"
17 #include "mlir/IR/DialectImplementation.h"
18 #include "mlir/IR/Types.h"
19 #include "llvm/ADT/Hashing.h"
20 #include "llvm/ADT/SetVector.h"
21 #include "llvm/ADT/TypeSwitch.h"
22 
23 using namespace mlir;
24 using namespace mlir::test;
25 
26 Attribute CompoundAAttr::parse(MLIRContext *context, DialectAsmParser &parser,
27                                Type type) {
28   int widthOfSomething;
29   Type oneType;
30   SmallVector<int, 4> arrayOfInts;
31   if (parser.parseLess() || parser.parseInteger(widthOfSomething) ||
32       parser.parseComma() || parser.parseType(oneType) || parser.parseComma() ||
33       parser.parseLSquare())
34     return Attribute();
35 
36   int intVal;
37   while (!*parser.parseOptionalInteger(intVal)) {
38     arrayOfInts.push_back(intVal);
39     if (parser.parseOptionalComma())
40       break;
41   }
42 
43   if (parser.parseRSquare() || parser.parseGreater())
44     return Attribute();
45   return get(context, widthOfSomething, oneType, arrayOfInts);
46 }
47 
48 void CompoundAAttr::print(DialectAsmPrinter &printer) const {
49   printer << "cmpnd_a<" << getWidthOfSomething() << ", " << getOneType()
50           << ", [";
51   llvm::interleaveComma(getArrayOfInts(), printer);
52   printer << "]>";
53 }
54 
55 //===----------------------------------------------------------------------===//
56 // Tablegen Generated Definitions
57 //===----------------------------------------------------------------------===//
58 
59 #define GET_ATTRDEF_CLASSES
60 #include "TestAttrDefs.cpp.inc"
61 
62 //===----------------------------------------------------------------------===//
63 // TestDialect
64 //===----------------------------------------------------------------------===//
65 
66 Attribute TestDialect::parseAttribute(DialectAsmParser &parser,
67                                       Type type) const {
68   StringRef attrTag;
69   if (failed(parser.parseKeyword(&attrTag)))
70     return Attribute();
71   if (auto attr = generatedAttributeParser(getContext(), parser, attrTag, type))
72     return attr;
73 
74   parser.emitError(parser.getNameLoc(), "unknown test attribute");
75   return Attribute();
76 }
77 
78 void TestDialect::printAttribute(Attribute attr,
79                                  DialectAsmPrinter &printer) const {
80   if (succeeded(generatedAttributePrinter(attr, printer)))
81     return;
82 }
83