CppSharp Developers Manual
- Introduction ===============
Check out the Getting started guide for build instructions.
- Architecture ===============
The driver is responsible for setting up the needed context for the rest of the tool and for implementing the main logic for parsing the user-provided headers, processing the declarations and then calling the language-specific generator to generate the bindings.
Since writing bindings by hand is tedious and error-prone, an automated approach was preferred. The open-source Clang parser is used for the task, providing an AST (Abstract Syntax Tree) of the code, ready to be consumed by the generator.
This is done in Parser.cpp, we walk the AST provided by Clang and mirror it in a .NET-friendly way. Most of this code is pretty straightforward if you are familiar with how Clang represents C++ code in AST nodes.
Recommended Clang documentation: http://clang.llvm.org/docs/InternalsManual.html
After parsing is done, some language-specific binding code needs to be generated.
Different target languages provide different features, so each generator needs to process the declarations in certain ways to provide a good mapping to the target language.
Additionally some of it can be provided directly in the native source code, by annotating the declarations with custom attributes.
This implements the C++ implementation-specific behaviours that allow the target language to communicate with the native code. It will usually use an existing FFI that provides support for interacting with C code.
It needs to know about the object layout, virtual tables, RTTI and exception low level details, so it can interoperate with the C++ code.
- ABI Internals ===============
Each ABI specifies the internal implementation-specific details of how C++ code works at the machine level, involving things like:
- Class Layout
- Symbol Naming
- Virtual tables
- Exceptions
- RTTI (Run-time Type Information)
There are two major C++ ABIs currently in use:
- Microsoft (VC++ / Clang)
- Itanium (GCC / Clang)
Each implementation differs in a lot of low level details, so we have to implement specific code for each one.
The target runtime needs to support calling native methods and this is usually implemented with an FFI (foreign function interface) in the target language VM virtual machine). In .NET this is done via the P/Invoke system.
- Similar Tools =================
-
Sharppy - .NET bindings generator for unmanaged C++ https://code.google.com/p/sharppy/
-
XInterop http://xinterop.com/
-
SWIG http://www.swig.org/