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/CAPI/Wrap.h"
20 #include "mlir/Support/LogicalResult.h"
21 #include "mlir/Support/TypeID.h"
22 #include "llvm/ADT/StringRef.h"
23 
24 /// Converts a StringRef into its MLIR C API equivalent.
wrap(llvm::StringRef ref)25 inline MlirStringRef wrap(llvm::StringRef ref) {
26   return mlirStringRefCreate(ref.data(), ref.size());
27 }
28 
29 /// Creates a StringRef out of its MLIR C API equivalent.
unwrap(MlirStringRef ref)30 inline llvm::StringRef unwrap(MlirStringRef ref) {
31   return llvm::StringRef(ref.data, ref.length);
32 }
33 
wrap(mlir::LogicalResult res)34 inline MlirLogicalResult wrap(mlir::LogicalResult res) {
35   if (mlir::succeeded(res))
36     return mlirLogicalResultSuccess();
37   return mlirLogicalResultFailure();
38 }
39 
unwrap(MlirLogicalResult res)40 inline mlir::LogicalResult unwrap(MlirLogicalResult res) {
41   return mlir::success(mlirLogicalResultIsSuccess(res));
42 }
43 
44 DEFINE_C_API_METHODS(MlirTypeID, mlir::TypeID)
45 DEFINE_C_API_PTR_METHODS(MlirTypeIDAllocator, mlir::TypeIDAllocator)
46 
47 #endif // MLIR_CAPI_SUPPORT_H
48