Skip to content

Commit e9610ba

Browse files
Gnimucvgvassilev
authored andcommitted
Add libclang-style C API
- Add `CXScope` and `CXQualType` as a bridge between libclang types and CppInterOp's types - Add C API for interpreter and scope manipulations
1 parent 05d65c8 commit e9610ba

File tree

8 files changed

+1047
-1
lines changed

8 files changed

+1047
-1
lines changed

include/clang-c/CXCppInterOp.h

Lines changed: 366 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,366 @@
1+
// NOLINTBEGIN()
2+
#ifndef LLVM_CLANG_C_CXCPPINTEROP_H
3+
#define LLVM_CLANG_C_CXCPPINTEROP_H
4+
5+
#include "clang-c/CXErrorCode.h"
6+
#include "clang-c/CXString.h"
7+
#include "clang-c/ExternC.h"
8+
#include "clang-c/Index.h"
9+
#include "clang-c/Platform.h"
10+
11+
#include <stdbool.h>
12+
#include <stddef.h>
13+
#include <stdint.h>
14+
15+
LLVM_CLANG_C_EXTERN_C_BEGIN
16+
17+
/**
18+
* \defgroup CPPINTEROP_INTERPRETER_MANIP Interpreter manipulations
19+
*
20+
* @{
21+
*/
22+
23+
/**
24+
* An opaque pointer representing an interpreter context.
25+
*/
26+
typedef struct CXInterpreterImpl* CXInterpreter;
27+
28+
/**
29+
* Create a Clang interpreter instance from the given arguments.
30+
*
31+
* \param argv The arguments that would be passed to the interpreter.
32+
*
33+
* \param argc The number of arguments in \c argv.
34+
*
35+
* \returns a \c CXInterpreter.
36+
*/
37+
CXInterpreter clang_createInterpreter(const char* const* argv, int argc);
38+
39+
typedef void* TInterp_t;
40+
41+
/**
42+
* Bridge between C API and C++ API.
43+
*
44+
* \returns a \c CXInterpreter.
45+
*/
46+
CXInterpreter clang_createInterpreterFromRawPtr(TInterp_t I);
47+
48+
/**
49+
* Returns a pointer to the underlying interpreter.
50+
*/
51+
void* clang_Interpreter_getClangInterpreter(CXInterpreter I);
52+
53+
/**
54+
* Returns a \c TInterp_t and takes the ownership.
55+
*/
56+
TInterp_t clang_Interpreter_takeInterpreterAsPtr(CXInterpreter I);
57+
58+
/**
59+
* Undo N previous incremental inputs.
60+
*/
61+
enum CXErrorCode clang_Interpreter_undo(CXInterpreter I, unsigned int N);
62+
63+
/**
64+
* Dispose of the given interpreter context.
65+
*/
66+
void clang_Interpreter_dispose(CXInterpreter I);
67+
68+
/**
69+
* Describes the return result of the different routines that do the incremental
70+
* compilation.
71+
*/
72+
typedef enum {
73+
/**
74+
* The compilation was successful.
75+
*/
76+
CXInterpreter_Success = 0,
77+
/**
78+
* The compilation failed.
79+
*/
80+
CXInterpreter_Failure = 1,
81+
/**
82+
* More more input is expected.
83+
*/
84+
CXInterpreter_MoreInputExpected = 2,
85+
} CXInterpreter_CompilationResult;
86+
87+
/**
88+
* Add a search path to the interpreter.
89+
*
90+
* \param I The interpreter.
91+
*
92+
* \param dir The directory to add.
93+
*
94+
* \param isUser Whether the directory is a user directory.
95+
*
96+
* \param prepend Whether to prepend the directory to the search path.
97+
*/
98+
void clang_Interpreter_addSearchPath(CXInterpreter I, const char* dir,
99+
bool isUser, bool prepend);
100+
101+
/**
102+
* Add an include path.
103+
*
104+
* \param I The interpreter.
105+
*
106+
* \param dir The directory to add.
107+
*/
108+
void clang_Interpreter_addIncludePath(CXInterpreter I, const char* dir);
109+
110+
/**
111+
* Declares a code snippet in \c code and does not execute it.
112+
*
113+
* \param I The interpreter.
114+
*
115+
* \param code The code snippet to declare.
116+
*
117+
* \param silent Whether to suppress the diagnostics or not
118+
*
119+
* \returns a \c CXErrorCode.
120+
*/
121+
enum CXErrorCode clang_Interpreter_declare(CXInterpreter I, const char* code,
122+
bool silent);
123+
124+
/**
125+
* Declares and executes a code snippet in \c code.
126+
*
127+
* \param I The interpreter.
128+
*
129+
* \param code The code snippet to execute.
130+
*
131+
* \returns a \c CXErrorCode.
132+
*/
133+
enum CXErrorCode clang_Interpreter_process(CXInterpreter I, const char* code);
134+
135+
/**
136+
* An opaque pointer representing a lightweight struct that is used for carrying
137+
* execution results.
138+
*/
139+
typedef void* CXValue;
140+
141+
/**
142+
* Create a CXValue.
143+
*
144+
* \returns a \c CXValue.
145+
*/
146+
CXValue clang_createValue(void);
147+
148+
/**
149+
* Dispose of the given CXValue.
150+
*
151+
* \param V The CXValue to dispose.
152+
*/
153+
void clang_Value_dispose(CXValue V);
154+
155+
/**
156+
* Declares, executes and stores the execution result to \c V.
157+
*
158+
* \param[in] I The interpreter.
159+
*
160+
* \param[in] code The code snippet to evaluate.
161+
*
162+
* \param[out] V The value to store the execution result.
163+
*
164+
* \returns a \c CXErrorCode.
165+
*/
166+
enum CXErrorCode clang_Interpreter_evaluate(CXInterpreter I, const char* code,
167+
CXValue V);
168+
169+
/**
170+
* Looks up the library if access is enabled.
171+
*
172+
* \param I The interpreter.
173+
*
174+
* \param lib_name The name of the library to lookup.
175+
*
176+
* \returns the path to the library.
177+
*/
178+
CXString clang_Interpreter_lookupLibrary(CXInterpreter I, const char* lib_name);
179+
180+
/**
181+
* Finds \c lib_stem considering the list of search paths and loads it by
182+
* calling dlopen.
183+
*
184+
* \param I The interpreter.
185+
*
186+
* \param lib_stem The stem of the library to load.
187+
*
188+
* \param lookup Whether to lookup the library or not.
189+
*
190+
* \returns a \c CXInterpreter_CompilationResult.
191+
*/
192+
CXInterpreter_CompilationResult
193+
clang_Interpreter_loadLibrary(CXInterpreter I, const char* lib_stem,
194+
bool lookup);
195+
196+
/**
197+
* Finds \c lib_stem considering the list of search paths and unloads it by
198+
* calling dlclose.
199+
*
200+
* \param I The interpreter.
201+
*
202+
* \param lib_stem The stem of the library to unload.
203+
*/
204+
void clang_Interpreter_unloadLibrary(CXInterpreter I, const char* lib_stem);
205+
206+
/**
207+
* @}
208+
*/
209+
210+
/**
211+
* \defgroup CPPINTEROP_SCOPE_MANIP Scope manipulations
212+
*
213+
* @{
214+
*/
215+
216+
/**
217+
* A fake CXCursor for working with the interpreter.
218+
* It has the same structure as CXCursor, but unlike CXCursor, it stores a
219+
* handle to the interpreter in the third slot of the data field.
220+
* This pave the way for upstreaming features to the LLVM project.
221+
*/
222+
typedef struct {
223+
enum CXCursorKind kind;
224+
int xdata;
225+
const void* data[3];
226+
} CXScope;
227+
228+
// for debugging purposes
229+
void clang_scope_dump(CXScope S);
230+
231+
/**
232+
* Checks if a class has a default constructor.
233+
*/
234+
bool clang_hasDefaultConstructor(CXScope S);
235+
236+
/**
237+
* Returns the default constructor of a class, if any.
238+
*/
239+
CXScope clang_getDefaultConstructor(CXScope S);
240+
241+
/**
242+
* Returns the class destructor, if any.
243+
*/
244+
CXScope clang_getDestructor(CXScope S);
245+
246+
/**
247+
* Returns a stringified version of a given function signature in the form:
248+
* void N::f(int i, double d, long l = 0, char ch = 'a').
249+
*/
250+
CXString clang_getFunctionSignature(CXScope func);
251+
252+
/**
253+
* Checks if a function is a templated function.
254+
*/
255+
bool clang_isTemplatedFunction(CXScope func);
256+
257+
/**
258+
* This function performs a lookup to check if there is a templated function of
259+
* that type. \c parent is mandatory, the global scope should be used as the
260+
* default value.
261+
*/
262+
bool clang_existsFunctionTemplate(const char* name, CXScope parent);
263+
264+
typedef struct {
265+
void* Type;
266+
const char* IntegralValue;
267+
} CXTemplateArgInfo;
268+
269+
/**
270+
* Builds a template instantiation for a given templated declaration.
271+
* Offers a single interface for instantiation of class, function and variable
272+
* templates.
273+
*
274+
* \param[in] tmpl The uninstantiated template class/function.
275+
*
276+
* \param[in] template_args The pointer to vector of template arguments stored
277+
* in the \c TemplateArgInfo struct
278+
*
279+
* \param[in] template_args_size The size of the vector of template arguments
280+
* passed as \c template_args
281+
*
282+
* \returns a \c CXScope representing the instantiated templated
283+
* class/function/variable.
284+
*/
285+
CXScope clang_instantiateTemplate(CXScope tmpl,
286+
CXTemplateArgInfo* template_args,
287+
size_t template_args_size);
288+
289+
/**
290+
* A fake CXType for working with the interpreter.
291+
* It has the same structure as CXType, but unlike CXType, it stores a
292+
* handle to the interpreter in the second slot of the data field.
293+
*/
294+
typedef struct {
295+
enum CXTypeKind kind;
296+
void* data[2];
297+
} CXQualType;
298+
299+
/**
300+
* Gets the string of the type that is passed as a parameter.
301+
*/
302+
CXString clang_getTypeAsString(CXQualType type);
303+
304+
/**
305+
* Returns the complex of the provided type.
306+
*/
307+
CXQualType clang_getComplexType(CXQualType eltype);
308+
309+
/**
310+
* An opaque pointer representing the object of a given type (\c CXScope).
311+
*/
312+
typedef void* CXObject;
313+
314+
/**
315+
* Allocates memory for the given type.
316+
*/
317+
CXObject clang_allocate(unsigned int n);
318+
319+
/**
320+
* Deallocates memory for a given class.
321+
*/
322+
void clang_deallocate(CXObject address);
323+
324+
/**
325+
* Creates an object of class \c scope and calls its default constructor. If \c
326+
* arena is set it uses placement new.
327+
*/
328+
CXObject clang_construct(CXScope scope, void* arena);
329+
330+
/**
331+
* Creates a trampoline function and makes a call to a generic function or
332+
* method.
333+
*
334+
* \param func The function or method to call.
335+
*
336+
* \param result The location where the return result will be placed.
337+
*
338+
* \param args The arguments to pass to the invocation.
339+
*
340+
* \param n The number of arguments.
341+
*
342+
* \param self The 'this pointer' of the object.
343+
*/
344+
void clang_invoke(CXScope func, void* result, void** args, size_t n,
345+
void* self);
346+
347+
/**
348+
* Calls the destructor of object of type \c type. When withFree is true it
349+
* calls operator delete/free.
350+
*
351+
* \param This The object to destruct.
352+
*
353+
* \param type The type of the object.
354+
*
355+
* \param withFree Whether to call operator delete/free or not.
356+
*/
357+
void clang_destruct(CXObject This, CXScope S, bool withFree);
358+
359+
/**
360+
* @}
361+
*/
362+
363+
LLVM_CLANG_C_EXTERN_C_END
364+
365+
#endif // LLVM_CLANG_C_CXCPPINTEROP_H
366+
// NOLINTEND()

lib/Interpreter/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ endif(LLVM_LINK_LLVM_DYLIB)
9797
add_llvm_library(clangCppInterOp
9898
DISABLE_LLVM_LINK_LLVM_DYLIB
9999
CppInterOp.cpp
100+
CXCppInterOp.cpp
100101
${DLM}
101102
LINK_LIBS
102103
${link_libs}

0 commit comments

Comments
 (0)