1 //===- Support.h - C API Helpers Implementation -----------------*- 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 definitions for converting MLIR C++ objects into helper
10 // C structures for the purpose of C API. This file should not be included from
11 // C++ code other than C API implementation nor from C code.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef MLIR_CAPI_SUPPORT_H
16 #define MLIR_CAPI_SUPPORT_H
17 
18 #include "mlir-c/Support.h"
19 #include "mlir/Support/LogicalResult.h"
20 #include "llvm/ADT/StringRef.h"
21 
22 /// Converts a StringRef into its MLIR C API equivalent.
wrap(llvm::StringRef ref)23 inline MlirStringRef wrap(llvm::StringRef ref) {
24   return mlirStringRefCreate(ref.data(), ref.size());
25 }
26 
27 /// Creates a StringRef out of its MLIR C API equivalent.
unwrap(MlirStringRef ref)28 inline llvm::StringRef unwrap(MlirStringRef ref) {
29   return llvm::StringRef(ref.data, ref.length);
30 }
31 
wrap(mlir::LogicalResult res)32 inline MlirLogicalResult wrap(mlir::LogicalResult res) {
33   if (mlir::succeeded(res))
34     return mlirLogicalResultSuccess();
35   return mlirLogicalResultFailure();
36 }
37 
unwrap(MlirLogicalResult res)38 inline mlir::LogicalResult unwrap(MlirLogicalResult res) {
39   return mlir::success(mlirLogicalResultIsSuccess(res));
40 }
41 
42 #endif // MLIR_CAPI_SUPPORT_H
43