@@ -42,12 +42,13 @@ using cir::MissingFeatures;
42
42
// CIR Custom Parser/Printer Signatures
43
43
// ===----------------------------------------------------------------------===//
44
44
45
- static mlir::ParseResult
46
- parseFuncTypeArgs (mlir::AsmParser &p, llvm::SmallVector<mlir::Type> ¶ms,
47
- bool &isVarArg);
48
- static void printFuncTypeArgs (mlir::AsmPrinter &p,
49
- mlir::ArrayRef<mlir::Type> params, bool isVarArg);
45
+ static mlir::ParseResult parseFuncType (mlir::AsmParser &p,
46
+ mlir::Type &optionalReturnTypes,
47
+ llvm::SmallVector<mlir::Type> ¶ms,
48
+ bool &isVarArg);
50
49
50
+ static void printFuncType (mlir::AsmPrinter &p, mlir::Type optionalReturnTypes,
51
+ mlir::ArrayRef<mlir::Type> params, bool isVarArg);
51
52
static mlir::ParseResult parsePointerAddrSpace (mlir::AsmParser &p,
52
53
mlir::Attribute &addrSpaceAttr);
53
54
static void printPointerAddrSpace (mlir::AsmPrinter &p,
@@ -914,9 +915,38 @@ FuncType FuncType::clone(TypeRange inputs, TypeRange results) const {
914
915
return get (llvm::to_vector (inputs), results[0 ], isVarArg ());
915
916
}
916
917
917
- mlir::ParseResult parseFuncTypeArgs (mlir::AsmParser &p,
918
- llvm::SmallVector<mlir::Type> ¶ms,
919
- bool &isVarArg) {
918
+ // A special parser is needed for function returning void to handle the missing
919
+ // type.
920
+ static mlir::ParseResult parseFuncTypeReturn (mlir::AsmParser &p,
921
+ mlir::Type &optionalReturnType) {
922
+ if (succeeded (p.parseOptionalLParen ())) {
923
+ // If we have already a '(', the function has no return type
924
+ optionalReturnType = {};
925
+ return mlir::success ();
926
+ }
927
+ mlir::Type type;
928
+ if (p.parseType (type))
929
+ return mlir::failure ();
930
+ if (isa<cir::VoidType>(type))
931
+ // An explicit !cir.void means also no return type.
932
+ optionalReturnType = {};
933
+ else
934
+ // Otherwise use the actual type.
935
+ optionalReturnType = type;
936
+ return p.parseLParen ();
937
+ }
938
+
939
+ // A special pretty-printer for function returning or not a result.
940
+ static void printFuncTypeReturn (mlir::AsmPrinter &p,
941
+ mlir::Type optionalReturnType) {
942
+ if (optionalReturnType)
943
+ p << optionalReturnType << ' ' ;
944
+ p << ' (' ;
945
+ }
946
+
947
+ static mlir::ParseResult
948
+ parseFuncTypeArgs (mlir::AsmParser &p, llvm::SmallVector<mlir::Type> ¶ms,
949
+ bool &isVarArg) {
920
950
isVarArg = false ;
921
951
// `(` `)`
922
952
if (succeeded (p.parseOptionalRParen ()))
@@ -946,8 +976,9 @@ mlir::ParseResult parseFuncTypeArgs(mlir::AsmParser &p,
946
976
return p.parseRParen ();
947
977
}
948
978
949
- void printFuncTypeArgs (mlir::AsmPrinter &p, mlir::ArrayRef<mlir::Type> params,
950
- bool isVarArg) {
979
+ static void printFuncTypeArgs (mlir::AsmPrinter &p,
980
+ mlir::ArrayRef<mlir::Type> params,
981
+ bool isVarArg) {
951
982
llvm::interleaveComma (params, p,
952
983
[&p](mlir::Type type) { p.printType (type); });
953
984
if (isVarArg) {
@@ -958,6 +989,23 @@ void printFuncTypeArgs(mlir::AsmPrinter &p, mlir::ArrayRef<mlir::Type> params,
958
989
p << ' )' ;
959
990
}
960
991
992
+ // Use a custom parser to handle the optional return and argument types without
993
+ // an optional anchor.
994
+ static mlir::ParseResult parseFuncType (mlir::AsmParser &p,
995
+ mlir::Type &optionalReturnTypes,
996
+ llvm::SmallVector<mlir::Type> ¶ms,
997
+ bool &isVarArg) {
998
+ if (failed (parseFuncTypeReturn (p, optionalReturnTypes)))
999
+ return failure ();
1000
+ return parseFuncTypeArgs (p, params, isVarArg);
1001
+ }
1002
+
1003
+ static void printFuncType (mlir::AsmPrinter &p, mlir::Type optionalReturnTypes,
1004
+ mlir::ArrayRef<mlir::Type> params, bool isVarArg) {
1005
+ printFuncTypeReturn (p, optionalReturnTypes);
1006
+ printFuncTypeArgs (p, params, isVarArg);
1007
+ }
1008
+
961
1009
// Return the actual return type or an explicit !cir.void if the function does
962
1010
// not return anything
963
1011
mlir::Type FuncType::getReturnType () const {
0 commit comments