2020// CIR Custom Parser/Printer Signatures
2121// ===----------------------------------------------------------------------===//
2222
23- static mlir::ParseResult parseFuncType (mlir::AsmParser &p,
24- mlir::Type &optionalReturnTypes ,
25- llvm::SmallVector<mlir::Type> ¶ms,
26- bool &isVarArg);
27- static void printFuncType ( mlir::AsmPrinter &p, mlir::Type optionalReturnTypes ,
28- mlir::ArrayRef<mlir::Type> params, bool isVarArg);
23+ static mlir::ParseResult
24+ parseFuncTypeParams (mlir::AsmParser &p, llvm::SmallVector< mlir::Type> ¶ms ,
25+ bool &isVarArg);
26+ static void printFuncTypeParams (mlir::AsmPrinter &p,
27+ mlir::ArrayRef< mlir::Type> params ,
28+ bool isVarArg);
2929
3030// ===----------------------------------------------------------------------===//
3131// Get autogenerated stuff
@@ -283,54 +283,31 @@ FuncType FuncType::clone(TypeRange inputs, TypeRange results) const {
283283 return get (llvm::to_vector (inputs), results[0 ], isVarArg ());
284284}
285285
286- // A special parser is needed for function returning void to handle the missing
287- // type.
288- static mlir::ParseResult parseFuncTypeReturn (mlir::AsmParser &p,
289- mlir::Type &optionalReturnType) {
290- if (succeeded (p.parseOptionalArrow ())) {
291- // `->` found. It must be followed by the return type.
292- return p.parseType (optionalReturnType);
293- }
294- // Function has `void` return in C++, no return in MLIR.
295- optionalReturnType = {};
296- return success ();
297- }
298-
299- // A special pretty-printer for function returning or not a result.
300- static void printFuncTypeReturn (mlir::AsmPrinter &p,
301- mlir::Type optionalReturnType) {
302- if (optionalReturnType)
303- p << " -> " << optionalReturnType;
304- }
305-
286+ // Custom parser that parses function parameters of form `(<type>*, ...)`.
306287static mlir::ParseResult
307- parseFuncTypeArgs (mlir::AsmParser &p, llvm::SmallVector<mlir::Type> ¶ms,
308- bool &isVarArg) {
288+ parseFuncTypeParams (mlir::AsmParser &p, llvm::SmallVector<mlir::Type> ¶ms,
289+ bool &isVarArg) {
309290 isVarArg = false ;
310- if (failed (p.parseLParen ()))
311- return failure ();
312- if (succeeded (p.parseOptionalRParen ())) {
313- // `()` empty argument list
314- return mlir::success ();
315- }
316- do {
317- if (succeeded (p.parseOptionalEllipsis ())) {
318- // `...`, which must be the last thing in the list.
319- isVarArg = true ;
320- break ;
321- } else {
322- mlir::Type argType;
323- if (failed (p.parseType (argType)))
324- return failure ();
325- params.push_back (argType);
326- }
327- } while (succeeded (p.parseOptionalComma ()));
328- return p.parseRParen ();
329- }
330-
331- static void printFuncTypeArgs (mlir::AsmPrinter &p,
332- mlir::ArrayRef<mlir::Type> params,
333- bool isVarArg) {
291+ return p.parseCommaSeparatedList (
292+ AsmParser::Delimiter::Paren, [&]() -> mlir::ParseResult {
293+ if (isVarArg)
294+ return p.emitError (p.getCurrentLocation (),
295+ " variadic `...` must be the last parameter" );
296+ if (succeeded (p.parseOptionalEllipsis ())) {
297+ isVarArg = true ;
298+ return success ();
299+ }
300+ mlir::Type type;
301+ if (failed (p.parseType (type)))
302+ return failure ();
303+ params.push_back (type);
304+ return success ();
305+ });
306+ }
307+
308+ static void printFuncTypeParams (mlir::AsmPrinter &p,
309+ mlir::ArrayRef<mlir::Type> params,
310+ bool isVarArg) {
334311 p << ' (' ;
335312 llvm::interleaveComma (params, p,
336313 [&p](mlir::Type type) { p.printType (type); });
@@ -342,23 +319,6 @@ static void printFuncTypeArgs(mlir::AsmPrinter &p,
342319 p << ' )' ;
343320}
344321
345- // Use a custom parser to handle the optional return and argument types without
346- // an optional anchor.
347- static mlir::ParseResult parseFuncType (mlir::AsmParser &p,
348- mlir::Type &optionalReturnType,
349- llvm::SmallVector<mlir::Type> ¶ms,
350- bool &isVarArg) {
351- if (failed (parseFuncTypeArgs (p, params, isVarArg)))
352- return failure ();
353- return parseFuncTypeReturn (p, optionalReturnType);
354- }
355-
356- static void printFuncType (mlir::AsmPrinter &p, mlir::Type optionalReturnType,
357- mlir::ArrayRef<mlir::Type> params, bool isVarArg) {
358- printFuncTypeArgs (p, params, isVarArg);
359- printFuncTypeReturn (p, optionalReturnType);
360- }
361-
362322// / Get the C-style return type of the function, which is !cir.void if the
363323// / function returns nothing and the actual return type otherwise.
364324mlir::Type FuncType::getReturnType () const {
0 commit comments