@@ -38,10 +38,11 @@ using ParseCoefficientFn = std::function<OptionalParseResult(MonomialType &)>;
38
38
// / a '+'.
39
39
// /
40
40
template <typename Monomial>
41
- ParseResult
42
- parseMonomial (AsmParser &parser, Monomial &monomial, llvm::StringRef &variable,
43
- bool &isConstantTerm, bool &shouldParseMore,
44
- ParseCoefficientFn<Monomial> parseAndStoreCoefficient) {
41
+ ParseResult parseMonomial (AsmParser &parser, Monomial &monomial,
42
+ llvm::StringRef &variable, bool &isConstantTerm,
43
+ bool &shouldParseMore,
44
+ ParseCoefficientFn<Monomial> parseAndStoreCoefficient,
45
+ bool optional) {
45
46
OptionalParseResult parsedCoeffResult = parseAndStoreCoefficient (monomial);
46
47
47
48
isConstantTerm = false ;
@@ -85,8 +86,9 @@ parseMonomial(AsmParser &parser, Monomial &monomial, llvm::StringRef &variable,
85
86
// If there's a **, then the integer exponent is required.
86
87
APInt parsedExponent (apintBitWidth, 0 );
87
88
if (failed (parser.parseInteger (parsedExponent))) {
88
- parser.emitError (parser.getCurrentLocation (),
89
- " found invalid integer exponent" );
89
+ if (!optional)
90
+ parser.emitError (parser.getCurrentLocation (),
91
+ " found invalid integer exponent" );
90
92
return failure ();
91
93
}
92
94
@@ -101,20 +103,22 @@ parseMonomial(AsmParser &parser, Monomial &monomial, llvm::StringRef &variable,
101
103
return success ();
102
104
}
103
105
104
- template <typename PolynoimalAttrTy, typename Monomial>
106
+ template <typename Monomial>
105
107
LogicalResult
106
108
parsePolynomialAttr (AsmParser &parser, llvm::SmallVector<Monomial> &monomials,
107
109
llvm::StringSet<> &variables,
108
- ParseCoefficientFn<Monomial> parseAndStoreCoefficient) {
110
+ ParseCoefficientFn<Monomial> parseAndStoreCoefficient,
111
+ bool optional) {
109
112
while (true ) {
110
113
Monomial parsedMonomial;
111
114
llvm::StringRef parsedVariableRef;
112
115
bool isConstantTerm;
113
116
bool shouldParseMore;
114
117
if (failed (parseMonomial<Monomial>(
115
118
parser, parsedMonomial, parsedVariableRef, isConstantTerm,
116
- shouldParseMore, parseAndStoreCoefficient))) {
117
- parser.emitError (parser.getCurrentLocation (), " expected a monomial" );
119
+ shouldParseMore, parseAndStoreCoefficient, optional))) {
120
+ if (!optional)
121
+ parser.emitError (parser.getCurrentLocation (), " expected a monomial" );
118
122
return failure ();
119
123
}
120
124
@@ -130,53 +134,67 @@ parsePolynomialAttr(AsmParser &parser, llvm::SmallVector<Monomial> &monomials,
130
134
if (succeeded (parser.parseOptionalGreater ())) {
131
135
break ;
132
136
}
133
- parser.emitError (
134
- parser.getCurrentLocation (),
135
- " expected + and more monomials, or > to end polynomial attribute" );
137
+ if (!optional)
138
+ parser.emitError (
139
+ parser.getCurrentLocation (),
140
+ " expected + and more monomials, or > to end polynomial attribute" );
136
141
return failure ();
137
142
}
138
143
139
144
if (variables.size () > 1 ) {
140
145
std::string vars = llvm::join (variables.keys (), " , " );
141
- parser.emitError (
142
- parser.getCurrentLocation (),
143
- " polynomials must have one indeterminate, but there were multiple: " +
144
- vars);
146
+ if (!optional)
147
+ parser.emitError (
148
+ parser.getCurrentLocation (),
149
+ " polynomials must have one indeterminate, but there were multiple: " +
150
+ vars);
145
151
return failure ();
146
152
}
147
153
148
154
return success ();
149
155
}
150
156
151
157
Attribute IntPolynomialAttr::parse (AsmParser &parser, Type type) {
158
+ return IntPolynomialAttr::parse (parser, type, /* optional=*/ false );
159
+ }
160
+
161
+ Attribute IntPolynomialAttr::parse (AsmParser &parser, Type type,
162
+ bool optional) {
152
163
if (failed (parser.parseLess ()))
153
164
return {};
154
165
155
166
llvm::SmallVector<IntMonomial> monomials;
156
167
llvm::StringSet<> variables;
157
168
158
- if (failed (parsePolynomialAttr<IntPolynomialAttr, IntMonomial>(
169
+ if (failed (parsePolynomialAttr<IntMonomial>(
159
170
parser, monomials, variables,
160
171
[&](IntMonomial &monomial) -> OptionalParseResult {
161
172
APInt parsedCoeff (apintBitWidth, 1 );
162
173
OptionalParseResult result =
163
174
parser.parseOptionalInteger (parsedCoeff);
164
175
monomial.setCoefficient (parsedCoeff);
165
176
return result;
166
- }))) {
177
+ },
178
+ optional))) {
167
179
return {};
168
180
}
169
181
170
182
auto result = IntPolynomial::fromMonomials (monomials);
171
183
if (failed (result)) {
172
- parser.emitError (parser.getCurrentLocation ())
173
- << " parsed polynomial must have unique exponents among monomials" ;
184
+ if (!optional)
185
+ parser.emitError (parser.getCurrentLocation ())
186
+ << " parsed polynomial must have unique exponents among monomials" ;
174
187
return {};
175
188
}
176
189
return IntPolynomialAttr::get (parser.getContext (), result.value ());
177
190
}
178
191
179
192
Attribute FloatPolynomialAttr::parse (AsmParser &parser, Type type) {
193
+ return FloatPolynomialAttr::parse (parser, type, /* optional=*/ false );
194
+ }
195
+
196
+ Attribute FloatPolynomialAttr::parse (AsmParser &parser, Type type,
197
+ bool optional) {
180
198
if (failed (parser.parseLess ()))
181
199
return {};
182
200
@@ -191,8 +209,8 @@ Attribute FloatPolynomialAttr::parse(AsmParser &parser, Type type) {
191
209
return OptionalParseResult (result);
192
210
};
193
211
194
- if (failed (parsePolynomialAttr<FloatPolynomialAttr, FloatMonomial>(
195
- parser, monomials, variables, parseAndStoreCoefficient))) {
212
+ if (failed (parsePolynomialAttr<FloatMonomial>(
213
+ parser, monomials, variables, parseAndStoreCoefficient, optional ))) {
196
214
return {};
197
215
}
198
216
0 commit comments