|
| 1 | +# Marshaling LLVMBool |
| 2 | + |
| 3 | +LLVMBool is a typdef in the LLVM-C API that is both simple and problematic. In it's |
| 4 | +simplest sense an LLVMBool is a representation of a bi-modal value. However, the |
| 5 | +problematic part is that the semantics for the value are different depending on any |
| 6 | +given API. That is, in some cases LLVMBool != 0 is a failure case, and others it is |
| 7 | +a success! The confusion stems from LLVMBool serving a dual role: |
| 8 | + 1) A real boolean true/false |
| 9 | + 2) A status code where 0 == success and non-zero indicates an error |
| 10 | + |
| 11 | +This duality is confusing and can lead to subtle errors in usage of APIs if translated |
| 12 | +directly into language projections. This makes hands-off automatic generation of P/Invoke |
| 13 | +calls to LLVM either imposible or error prone. Thus, Llvm.NET uses manually updated P/Invoke |
| 14 | +calls that were initially auto generated to get things started but not maintined via any |
| 15 | +generation tools. In the case of LLVMBool Llvm.NET uses distinct types for the different |
| 16 | +semantics and declares the interop signatures with the form appropriate to the function |
| 17 | +being called. The two types are LLVMStatus and standard `System.Boolean` or `bool` in C# |
| 18 | + |
| 19 | +## LLVMStatus |
| 20 | +This is a status value where 0 == Success and non-zero is a failure or false status. |
| 21 | +LLVMStatus is used whenever the 0 == success semantics apply to the API. For example: |
| 22 | + |
| 23 | +```C# |
| 24 | +[DllImport( LibraryPath, EntryPoint = "LLVMWriteBitcodeToFD", CallingConvention = CallingConvention.Cdecl )] |
| 25 | +internal static extern LLVMStatus LLVMWriteBitcodeToFD( LLVMModuleRef @M, int @FD, int @ShouldClose, int @Unbuffered ); |
| 26 | +``` |
| 27 | + |
| 28 | +## LLVMBool |
| 29 | +This is the traditioanl boolean value where 0==false and non-zero is true and uses the |
| 30 | +standard boolean marshaling support for System.Boolean |
| 31 | +```C# |
| 32 | +[DllImport( LibraryPath, EntryPoint = "LLVMTypeIsSized", CallingConvention = CallingConvention.Cdecl )] |
| 33 | +[return: MarshalAs( UnmanagedType.Bool )] |
| 34 | +internal static extern bool LLVMTypeIsSized( LLVMTypeRef @Ty ); |
| 35 | +``` |
| 36 | + |
0 commit comments