|
11 | 11 | //===----------------------------------------------------------------------===// |
12 | 12 |
|
13 | 13 | #include "clang/CIR/Dialect/IR/CIRTypes.h" |
| 14 | +#include "clang/CIR/Dialect/IR/CIRAttrs.h" |
14 | 15 | #include "clang/CIR/Dialect/IR/CIRDialect.h" |
15 | 16 |
|
16 | 17 | #include "mlir/IR/Attributes.h" |
| 18 | +#include "mlir/IR/BuiltinAttributes.h" |
17 | 19 | #include "mlir/IR/BuiltinTypes.h" |
18 | 20 | #include "mlir/IR/DialectImplementation.h" |
19 | 21 | #include "mlir/Support/LogicalResult.h" |
|
22 | 24 | #include "llvm/ADT/SmallVector.h" |
23 | 25 | #include "llvm/ADT/TypeSwitch.h" |
24 | 26 | #include "llvm/Support/ErrorHandling.h" |
| 27 | +#include <optional> |
25 | 28 |
|
26 | 29 | //===----------------------------------------------------------------------===// |
27 | 30 | // CIR Custom Parser/Printer Signatures |
@@ -87,72 +90,69 @@ Type BoolType::parse(mlir::AsmParser &parser) { |
87 | 90 |
|
88 | 91 | void BoolType::print(mlir::AsmPrinter &printer) const {} |
89 | 92 |
|
| 93 | +//===----------------------------------------------------------------------===// |
| 94 | +// StructType Definitions |
| 95 | +//===----------------------------------------------------------------------===// |
| 96 | + |
90 | 97 | Type StructType::parse(mlir::AsmParser &parser) { |
| 98 | + llvm::SmallVector<mlir::Type> members; |
| 99 | + mlir::StringAttr id; |
| 100 | + bool body = false; |
| 101 | + bool packed = false; |
| 102 | + mlir::cir::ASTRecordDeclAttr ast = nullptr; |
| 103 | + |
91 | 104 | if (parser.parseLess()) |
92 | | - return Type(); |
93 | | - std::string typeName; |
94 | | - if (parser.parseString(&typeName)) |
95 | | - return Type(); |
| 105 | + return {}; |
96 | 106 |
|
97 | | - llvm::SmallVector<Type> members; |
98 | | - bool parsedBody = false; |
99 | | - |
100 | | - auto parseASTAttribute = [&](Attribute &attr) { |
101 | | - auto optAttr = parser.parseOptionalAttribute(attr); |
102 | | - if (optAttr.has_value()) { |
103 | | - if (failed(*optAttr)) |
104 | | - return false; |
105 | | - if (attr.isa<ASTFunctionDeclAttr>() || attr.isa<ASTRecordDeclAttr>() || |
106 | | - attr.isa<ASTVarDeclAttr>()) |
107 | | - return true; |
108 | | - parser.emitError(parser.getCurrentLocation(), |
109 | | - "Unknown cir.struct attribute"); |
110 | | - return false; |
111 | | - } |
112 | | - return false; |
113 | | - }; |
114 | | - |
115 | | - while (mlir::succeeded(parser.parseOptionalComma())) { |
116 | | - if (mlir::succeeded(parser.parseOptionalKeyword("incomplete"))) |
117 | | - continue; |
118 | | - |
119 | | - parsedBody = true; |
120 | | - Type nextMember; |
121 | | - auto optTy = parser.parseOptionalType(nextMember); |
122 | | - if (optTy.has_value()) { |
123 | | - if (failed(*optTy)) |
124 | | - return Type(); |
125 | | - members.push_back(nextMember); |
126 | | - continue; |
127 | | - } |
| 107 | + if (parser.parseAttribute(id)) |
| 108 | + return {}; |
128 | 109 |
|
129 | | - // Maybe it's an AST attribute: always last member, break. |
130 | | - Attribute astAttr; |
131 | | - if (parseASTAttribute(astAttr)) |
132 | | - break; |
| 110 | + if (parser.parseOptionalKeyword("packed").succeeded()) |
| 111 | + packed = true; |
| 112 | + |
| 113 | + if (parser.parseOptionalKeyword("incomplete").failed()) { |
| 114 | + body = true; |
| 115 | + const auto delim = AsmParser::Delimiter::Braces; |
| 116 | + auto result = parser.parseCommaSeparatedList(delim, [&]() -> ParseResult { |
| 117 | + mlir::Type ty; |
| 118 | + if (parser.parseType(ty)) |
| 119 | + return mlir::failure(); |
| 120 | + members.push_back(ty); |
| 121 | + return mlir::success(); |
| 122 | + }); |
| 123 | + |
| 124 | + if (result.failed()) |
| 125 | + return {}; |
133 | 126 | } |
134 | 127 |
|
| 128 | + parser.parseOptionalAttribute(ast); |
| 129 | + |
135 | 130 | if (parser.parseGreater()) |
136 | | - return Type(); |
137 | | - auto sTy = get(parser.getContext(), members, typeName, parsedBody); |
138 | | - return sTy; |
| 131 | + return {}; |
| 132 | + |
| 133 | + return StructType::get(parser.getContext(), members, id, body, packed, |
| 134 | + std::nullopt); |
139 | 135 | } |
140 | 136 |
|
141 | 137 | void StructType::print(mlir::AsmPrinter &printer) const { |
142 | | - printer << '<' << getTypeName(); |
| 138 | + printer << '<' << getTypeName() << " "; |
| 139 | + |
| 140 | + if (getPacked()) |
| 141 | + printer << "packed "; |
| 142 | + |
143 | 143 | if (!getBody()) { |
144 | | - printer << ", incomplete"; |
| 144 | + printer << "incomplete"; |
145 | 145 | } else { |
146 | | - auto members = getMembers(); |
147 | | - if (!members.empty()) { |
148 | | - printer << ", "; |
149 | | - llvm::interleaveComma(getMembers(), printer); |
150 | | - } |
| 146 | + printer << "{"; |
| 147 | + llvm::interleaveComma(getMembers(), printer); |
| 148 | + printer << "}"; |
151 | 149 | } |
152 | | - if (getAst()) { |
153 | | - printer << ", "; |
154 | | - printer.printAttributeWithoutType(*getAst()); |
| 150 | + |
| 151 | + if (getAst().has_value()) { |
| 152 | + printer << " "; |
| 153 | + printer.printAttribute(getAst().value()); |
155 | 154 | } |
| 155 | + |
156 | 156 | printer << '>'; |
157 | 157 | } |
158 | 158 |
|
|
0 commit comments