feat(rust): manage erros coming from compiler

C struct now contains an additonal char* pointer, which can be either
NULL in case there is no error, or a buffer containing the error
message. It's the responsability of destructor function to free that
memory.
This commit is contained in:
youben11
2022-11-28 11:11:26 +01:00
committed by Ayoub Benaissa
parent 15b4aac0a1
commit 16f3b0bbf6
4 changed files with 209 additions and 94 deletions

View File

@@ -12,15 +12,15 @@
extern "C" {
#endif
// TODO: add a char* to the struct that can return an error message in case of
// an error (where ptr would be null). Error messages can be returned using
// llvm::toString(error.takeError()) and allocating a buffer for the message and
// copy it. The buffer can later be freed during struct destruction.
/// Opaque type declarations. Refer to llvm-project/mlir/include/mlir-c/IR.h for
/// more info
/// Opaque type declarations. Inspired from
/// llvm-project/mlir/include/mlir-c/IR.h
///
/// Adds an error pointer to an allocated buffer holding the error message if
/// any.
#define DEFINE_C_API_STRUCT(name, storage) \
struct name { \
storage *ptr; \
char *error; \
}; \
typedef struct name name

View File

@@ -9,33 +9,54 @@
#include "concretelang-c/Support/CompilerEngine.h"
#include "concretelang/Support/CompilerEngine.h"
#include "concretelang/Support/LibrarySupport.h"
#include "mlir/CAPI/Wrap.h"
DEFINE_C_API_PTR_METHODS(CompilerEngine, mlir::concretelang::CompilerEngine)
DEFINE_C_API_PTR_METHODS(CompilationContext,
mlir::concretelang::CompilationContext)
DEFINE_C_API_PTR_METHODS(CompilationResult,
mlir::concretelang::CompilerEngine::CompilationResult)
DEFINE_C_API_PTR_METHODS(Library, mlir::concretelang::CompilerEngine::Library)
DEFINE_C_API_PTR_METHODS(LibraryCompilationResult,
mlir::concretelang::LibraryCompilationResult)
DEFINE_C_API_PTR_METHODS(LibrarySupport, mlir::concretelang::LibrarySupport)
DEFINE_C_API_PTR_METHODS(CompilationOptions,
mlir::concretelang::CompilationOptions)
DEFINE_C_API_PTR_METHODS(OptimizerConfig, mlir::concretelang::optimizer::Config)
DEFINE_C_API_PTR_METHODS(ServerLambda,
mlir::concretelang::serverlib::ServerLambda)
DEFINE_C_API_PTR_METHODS(ClientParameters,
mlir::concretelang::clientlib::ClientParameters)
DEFINE_C_API_PTR_METHODS(KeySet, mlir::concretelang::clientlib::KeySet)
DEFINE_C_API_PTR_METHODS(KeySetCache,
mlir::concretelang::clientlib::KeySetCache)
DEFINE_C_API_PTR_METHODS(EvaluationKeys,
mlir::concretelang::clientlib::EvaluationKeys)
DEFINE_C_API_PTR_METHODS(LambdaArgument, mlir::concretelang::LambdaArgument)
DEFINE_C_API_PTR_METHODS(PublicArguments,
mlir::concretelang::clientlib::PublicArguments)
DEFINE_C_API_PTR_METHODS(PublicResult,
mlir::concretelang::clientlib::PublicResult)
/// Add a mechanism to go from Cpp objects to C-struct, with the ability to
/// represent errors. Also the other way arround.
#define DEFINE_C_API_PTR_METHODS_WITH_ERROR(name, cpptype) \
static inline name wrap(cpptype *cpp) { return name{cpp, (char *)NULL}; } \
static inline name wrap(cpptype *cpp, std::string errorStr) { \
char *error = new char[errorStr.size()]; \
strcpy(error, errorStr.c_str()); \
return name{(cpptype *)NULL, error}; \
} \
static inline cpptype *unwrap(name c) { \
return static_cast<cpptype *>(c.ptr); \
} \
static inline char *getErrorPtr(name c) { return c.error; }
DEFINE_C_API_PTR_METHODS_WITH_ERROR(CompilerEngine,
mlir::concretelang::CompilerEngine)
DEFINE_C_API_PTR_METHODS_WITH_ERROR(CompilationContext,
mlir::concretelang::CompilationContext)
DEFINE_C_API_PTR_METHODS_WITH_ERROR(
CompilationResult, mlir::concretelang::CompilerEngine::CompilationResult)
DEFINE_C_API_PTR_METHODS_WITH_ERROR(Library,
mlir::concretelang::CompilerEngine::Library)
DEFINE_C_API_PTR_METHODS_WITH_ERROR(
LibraryCompilationResult, mlir::concretelang::LibraryCompilationResult)
DEFINE_C_API_PTR_METHODS_WITH_ERROR(LibrarySupport,
mlir::concretelang::LibrarySupport)
DEFINE_C_API_PTR_METHODS_WITH_ERROR(CompilationOptions,
mlir::concretelang::CompilationOptions)
DEFINE_C_API_PTR_METHODS_WITH_ERROR(OptimizerConfig,
mlir::concretelang::optimizer::Config)
DEFINE_C_API_PTR_METHODS_WITH_ERROR(ServerLambda,
mlir::concretelang::serverlib::ServerLambda)
DEFINE_C_API_PTR_METHODS_WITH_ERROR(
ClientParameters, mlir::concretelang::clientlib::ClientParameters)
DEFINE_C_API_PTR_METHODS_WITH_ERROR(KeySet,
mlir::concretelang::clientlib::KeySet)
DEFINE_C_API_PTR_METHODS_WITH_ERROR(KeySetCache,
mlir::concretelang::clientlib::KeySetCache)
DEFINE_C_API_PTR_METHODS_WITH_ERROR(
EvaluationKeys, mlir::concretelang::clientlib::EvaluationKeys)
DEFINE_C_API_PTR_METHODS_WITH_ERROR(LambdaArgument,
mlir::concretelang::LambdaArgument)
DEFINE_C_API_PTR_METHODS_WITH_ERROR(
PublicArguments, mlir::concretelang::clientlib::PublicArguments)
DEFINE_C_API_PTR_METHODS_WITH_ERROR(PublicResult,
mlir::concretelang::clientlib::PublicResult)
#undef DEFINE_C_API_PTR_METHODS_WITH_ERROR
#endif