File indexing completed on 2025-09-17 08:53:49
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef CPPINTEROP_CPPINTEROP_H
0011 #define CPPINTEROP_CPPINTEROP_H
0012
0013 #include <cassert>
0014 #include <cstdint>
0015 #include <set>
0016 #include <string>
0017 #include <vector>
0018
0019
0020 #if defined _WIN32 || defined __CYGWIN__
0021 #define CPPINTEROP_API __declspec(dllexport)
0022 #else
0023 #ifdef __GNUC__
0024 #define CPPINTEROP_API __attribute__((__visibility__("default")))
0025 #else
0026 #define CPPINTEROP_API
0027 #endif
0028 #endif
0029
0030 namespace Cpp {
0031 using TCppIndex_t = size_t;
0032 using TCppScope_t = void*;
0033 using TCppType_t = void*;
0034 using TCppFunction_t = void*;
0035 using TCppConstFunction_t = const void*;
0036 using TCppFuncAddr_t = void*;
0037 using TInterp_t = void*;
0038 using TCppObject_t = void*;
0039
0040 enum Operator {
0041 OP_None,
0042 OP_New,
0043 OP_Delete,
0044 OP_Array_New,
0045 OP_Array_Delete,
0046 OP_Plus,
0047 OP_Minus,
0048 OP_Star,
0049 OP_Slash,
0050 OP_Percent,
0051 OP_Caret,
0052 OP_Amp,
0053 OP_Pipe,
0054 OP_Tilde,
0055 OP_Exclaim,
0056 OP_Equal,
0057 OP_Less,
0058 OP_Greater,
0059 OP_PlusEqual,
0060 OP_MinusEqual,
0061 OP_StarEqual,
0062 OP_SlashEqual,
0063 OP_PercentEqual,
0064 OP_CaretEqual,
0065 OP_AmpEqual,
0066 OP_PipeEqual,
0067 OP_LessLess,
0068 OP_GreaterGreater,
0069 OP_LessLessEqual,
0070 OP_GreaterGreaterEqual,
0071 OP_EqualEqual,
0072 OP_ExclaimEqual,
0073 OP_LessEqual,
0074 OP_GreaterEqual,
0075 OP_Spaceship,
0076 OP_AmpAmp,
0077 OP_PipePipe,
0078 OP_PlusPlus,
0079 OP_MinusMinus,
0080 OP_Comma,
0081 OP_ArrowStar,
0082 OP_Arrow,
0083 OP_Call,
0084 OP_Subscript,
0085 OP_Conditional,
0086 OP_Coawait,
0087 };
0088
0089 enum OperatorArity { kUnary = 1, kBinary, kBoth };
0090
0091
0092
0093
0094 class JitCall {
0095 public:
0096 friend CPPINTEROP_API JitCall
0097 MakeFunctionCallable(TInterp_t I, TCppConstFunction_t func);
0098 enum Kind : char {
0099 kUnknown = 0,
0100 kGenericCall,
0101 kDestructorCall,
0102 };
0103 struct ArgList {
0104 void** m_Args = nullptr;
0105 size_t m_ArgSize = 0;
0106
0107 ArgList() : m_Args(nullptr), m_ArgSize(0) {}
0108 ArgList(void** Args, size_t ArgSize)
0109 : m_Args(Args), m_ArgSize(ArgSize) {}
0110 };
0111
0112
0113
0114 using GenericCall = void (*)(void*, int, void**, void*);
0115 using DestructorCall = void (*)(void*, unsigned long, int);
0116 private:
0117 union {
0118 GenericCall m_GenericCall;
0119 DestructorCall m_DestructorCall;
0120 };
0121 const Kind m_Kind;
0122 TCppConstFunction_t m_FD;
0123 JitCall() : m_Kind(kUnknown), m_GenericCall(nullptr), m_FD(nullptr) {}
0124 JitCall(Kind K, GenericCall C, TCppConstFunction_t FD)
0125 : m_Kind(K), m_GenericCall(C), m_FD(FD) {}
0126 JitCall(Kind K, DestructorCall C, TCppConstFunction_t Dtor)
0127 : m_Kind(K), m_DestructorCall(C), m_FD(Dtor) {}
0128
0129
0130 CPPINTEROP_API bool AreArgumentsValid(void* result, ArgList args,
0131 void* self) const;
0132
0133
0134
0135 CPPINTEROP_API void ReportInvokeStart(void* result, ArgList args,
0136 void* self) const;
0137 CPPINTEROP_API void ReportInvokeStart(void* object, unsigned long nary,
0138 int withFree) const;
0139 void ReportInvokeEnd() const;
0140 public:
0141 Kind getKind() const { return m_Kind; }
0142 bool isValid() const { return getKind() != kUnknown; }
0143 bool isInvalid() const { return !isValid(); }
0144 explicit operator bool() const { return isValid(); }
0145
0146
0147 void Invoke(ArgList args = {}, void* self = nullptr) const {
0148 Invoke(nullptr, args, self);
0149 }
0150
0151
0152
0153
0154
0155
0156
0157
0158
0159 void Invoke(void* result, ArgList args = {}, void* self = nullptr) const {
0160
0161 if (m_Kind == kDestructorCall && result && !args.m_Args)
0162 return InvokeDestructor(result, 0UL, true);
0163
0164 #ifndef NDEBUG
0165 assert(AreArgumentsValid(result, args, self) && "Invalid args!");
0166 ReportInvokeStart(result, args, self);
0167 #endif
0168 m_GenericCall(self, args.m_ArgSize, args.m_Args, result);
0169 }
0170
0171
0172
0173
0174
0175
0176
0177 void InvokeDestructor(void* object, unsigned long nary = 0,
0178 int withFree = true) const {
0179 assert(m_Kind == kDestructorCall && "Wrong overload!");
0180 #ifndef NDEBUG
0181 ReportInvokeStart(object, nary, withFree);
0182 #endif
0183 m_DestructorCall(object, nary, withFree);
0184 }
0185 };
0186
0187
0188 CPPINTEROP_API std::string GetVersion();
0189
0190
0191 CPPINTEROP_API std::string Demangle(const std::string& mangled_name);
0192
0193
0194
0195
0196
0197
0198 CPPINTEROP_API void EnableDebugOutput(bool value = true);
0199
0200
0201 CPPINTEROP_API bool IsDebugOutputEnabled();
0202
0203
0204
0205
0206
0207 CPPINTEROP_API bool IsAggregate(TCppScope_t scope);
0208
0209
0210 CPPINTEROP_API bool IsNamespace(TCppScope_t scope);
0211
0212
0213 CPPINTEROP_API bool IsClass(TCppScope_t scope);
0214
0215
0216 CPPINTEROP_API bool IsFunction(TCppScope_t scope);
0217
0218
0219 CPPINTEROP_API bool IsFunctionPointerType(TCppType_t type);
0220
0221
0222
0223 CPPINTEROP_API bool IsClassPolymorphic(TCppScope_t klass);
0224
0225
0226
0227
0228 CPPINTEROP_API bool IsComplete(TCppScope_t scope);
0229
0230 CPPINTEROP_API size_t SizeOf(TCppScope_t scope);
0231
0232
0233 CPPINTEROP_API bool IsBuiltin(TCppType_t type);
0234
0235
0236 CPPINTEROP_API bool IsTemplate(TCppScope_t handle);
0237
0238
0239 CPPINTEROP_API bool IsTemplateSpecialization(TCppScope_t handle);
0240
0241
0242 CPPINTEROP_API bool IsTypedefed(TCppScope_t handle);
0243
0244 CPPINTEROP_API bool IsAbstract(TCppType_t klass);
0245
0246
0247 CPPINTEROP_API bool IsEnumScope(TCppScope_t handle);
0248
0249
0250
0251 CPPINTEROP_API bool IsEnumConstant(TCppScope_t handle);
0252
0253
0254 CPPINTEROP_API bool IsEnumType(TCppType_t type);
0255
0256
0257
0258 CPPINTEROP_API void GetEnums(TCppScope_t scope,
0259 std::vector<std::string>& Result);
0260
0261
0262
0263 CPPINTEROP_API bool IsSmartPtrType(TCppType_t type);
0264
0265
0266
0267
0268 CPPINTEROP_API TCppType_t GetIntegerTypeFromEnumScope(TCppScope_t handle);
0269
0270
0271
0272
0273 CPPINTEROP_API TCppType_t GetIntegerTypeFromEnumType(TCppType_t handle);
0274
0275
0276 CPPINTEROP_API std::vector<TCppScope_t> GetEnumConstants(TCppScope_t scope);
0277
0278
0279 CPPINTEROP_API TCppType_t GetEnumConstantType(TCppScope_t scope);
0280
0281
0282
0283 CPPINTEROP_API TCppIndex_t GetEnumConstantValue(TCppScope_t scope);
0284
0285
0286 CPPINTEROP_API size_t GetSizeOfType(TCppType_t type);
0287
0288
0289 CPPINTEROP_API bool IsVariable(TCppScope_t scope);
0290
0291
0292
0293 CPPINTEROP_API std::string GetName(TCppScope_t klass);
0294
0295
0296
0297 CPPINTEROP_API std::string GetCompleteName(TCppScope_t klass);
0298
0299
0300
0301 CPPINTEROP_API std::string GetQualifiedName(TCppScope_t klass);
0302
0303
0304
0305
0306 CPPINTEROP_API std::string GetQualifiedCompleteName(TCppScope_t klass);
0307
0308
0309 CPPINTEROP_API std::vector<TCppScope_t> GetUsingNamespaces(TCppScope_t scope);
0310
0311
0312 CPPINTEROP_API TCppScope_t GetGlobalScope();
0313
0314
0315
0316 CPPINTEROP_API TCppScope_t GetUnderlyingScope(TCppScope_t scope);
0317
0318
0319
0320
0321 CPPINTEROP_API TCppScope_t GetScope(const std::string& name,
0322 TCppScope_t parent = nullptr);
0323
0324
0325
0326
0327 CPPINTEROP_API TCppScope_t GetScopeFromCompleteName(const std::string& name);
0328
0329
0330
0331 CPPINTEROP_API TCppScope_t GetNamed(const std::string& name,
0332 TCppScope_t parent = nullptr);
0333
0334
0335 CPPINTEROP_API TCppScope_t GetParentScope(TCppScope_t scope);
0336
0337
0338 CPPINTEROP_API TCppScope_t GetScopeFromType(TCppType_t type);
0339
0340
0341
0342 CPPINTEROP_API TCppIndex_t GetNumBases(TCppScope_t klass);
0343
0344
0345
0346
0347
0348 CPPINTEROP_API TCppScope_t GetBaseClass(TCppScope_t klass, TCppIndex_t ibase);
0349
0350
0351
0352 CPPINTEROP_API bool IsSubclass(TCppScope_t derived, TCppScope_t base);
0353
0354
0355
0356 CPPINTEROP_API int64_t GetBaseClassOffset(TCppScope_t derived,
0357 TCppScope_t base);
0358
0359
0360
0361
0362
0363
0364 CPPINTEROP_API void GetClassMethods(TCppScope_t klass,
0365 std::vector<TCppFunction_t>& methods);
0366
0367
0368
0369
0370
0371
0372 CPPINTEROP_API void
0373 GetFunctionTemplatedDecls(TCppScope_t klass,
0374 std::vector<TCppFunction_t>& methods);
0375
0376
0377 CPPINTEROP_API bool HasDefaultConstructor(TCppScope_t scope);
0378
0379
0380 CPPINTEROP_API TCppFunction_t GetDefaultConstructor(TCppScope_t scope);
0381
0382
0383 CPPINTEROP_API TCppFunction_t GetDestructor(TCppScope_t scope);
0384
0385
0386
0387 CPPINTEROP_API std::vector<TCppFunction_t>
0388 GetFunctionsUsingName(TCppScope_t scope, const std::string& name);
0389
0390
0391 CPPINTEROP_API TCppType_t GetFunctionReturnType(TCppFunction_t func);
0392
0393
0394 CPPINTEROP_API TCppIndex_t GetFunctionNumArgs(TCppFunction_t func);
0395
0396
0397 CPPINTEROP_API TCppIndex_t GetFunctionRequiredArgs(TCppConstFunction_t func);
0398
0399
0400
0401
0402 CPPINTEROP_API TCppType_t GetFunctionArgType(TCppFunction_t func,
0403 TCppIndex_t iarg);
0404
0405
0406
0407 CPPINTEROP_API std::string GetFunctionSignature(TCppFunction_t func);
0408
0409
0410 CPPINTEROP_API bool IsFunctionDeleted(TCppConstFunction_t function);
0411
0412 CPPINTEROP_API bool IsTemplatedFunction(TCppFunction_t func);
0413
0414
0415
0416 CPPINTEROP_API bool ExistsFunctionTemplate(const std::string& name,
0417 TCppScope_t parent = nullptr);
0418
0419
0420
0421
0422
0423
0424
0425
0426
0427
0428 CPPINTEROP_API void LookupConstructors(const std::string& name,
0429 TCppScope_t parent,
0430 std::vector<TCppFunction_t>& funcs);
0431
0432
0433
0434
0435
0436
0437
0438
0439 CPPINTEROP_API bool
0440 GetClassTemplatedMethods(const std::string& name, TCppScope_t parent,
0441 std::vector<TCppFunction_t>& funcs);
0442
0443
0444 CPPINTEROP_API bool IsMethod(TCppConstFunction_t method);
0445
0446
0447 CPPINTEROP_API bool IsPublicMethod(TCppFunction_t method);
0448
0449
0450 CPPINTEROP_API bool IsProtectedMethod(TCppFunction_t method);
0451
0452
0453 CPPINTEROP_API bool IsPrivateMethod(TCppFunction_t method);
0454
0455
0456 CPPINTEROP_API bool IsConstructor(TCppConstFunction_t method);
0457
0458
0459 CPPINTEROP_API bool IsDestructor(TCppConstFunction_t method);
0460
0461
0462 CPPINTEROP_API bool IsStaticMethod(TCppConstFunction_t method);
0463
0464
0465 CPPINTEROP_API TCppFuncAddr_t GetFunctionAddress(const char* mangled_name);
0466
0467
0468 CPPINTEROP_API TCppFuncAddr_t GetFunctionAddress(TCppFunction_t method);
0469
0470
0471 CPPINTEROP_API bool IsVirtualMethod(TCppFunction_t method);
0472
0473
0474 CPPINTEROP_API void GetDatamembers(TCppScope_t scope,
0475 std::vector<TCppScope_t>& datamembers);
0476
0477
0478
0479
0480 CPPINTEROP_API void
0481 GetStaticDatamembers(TCppScope_t scope,
0482 std::vector<TCppScope_t>& datamembers);
0483
0484
0485
0486
0487
0488 CPPINTEROP_API
0489 void GetEnumConstantDatamembers(TCppScope_t scope,
0490 std::vector<TCppScope_t>& datamembers,
0491 bool include_enum_class = true);
0492
0493
0494 CPPINTEROP_API TCppScope_t LookupDatamember(const std::string& name,
0495 TCppScope_t parent);
0496
0497
0498 CPPINTEROP_API TCppType_t GetVariableType(TCppScope_t var);
0499
0500
0501
0502 CPPINTEROP_API intptr_t GetVariableOffset(TCppScope_t var,
0503 TCppScope_t parent = nullptr);
0504
0505
0506 CPPINTEROP_API bool IsPublicVariable(TCppScope_t var);
0507
0508
0509 CPPINTEROP_API bool IsProtectedVariable(TCppScope_t var);
0510
0511
0512 CPPINTEROP_API bool IsPrivateVariable(TCppScope_t var);
0513
0514
0515 CPPINTEROP_API bool IsStaticVariable(TCppScope_t var);
0516
0517
0518 CPPINTEROP_API bool IsConstVariable(TCppScope_t var);
0519
0520
0521 CPPINTEROP_API bool IsRecordType(TCppType_t type);
0522
0523
0524 CPPINTEROP_API bool IsPODType(TCppType_t type);
0525
0526
0527 CPPINTEROP_API bool IsPointerType(TCppType_t type);
0528
0529
0530 CPPINTEROP_API TCppType_t GetPointeeType(TCppType_t type);
0531
0532
0533 CPPINTEROP_API bool IsReferenceType(TCppType_t type);
0534
0535
0536 CPPINTEROP_API TCppType_t GetNonReferenceType(TCppType_t type);
0537
0538
0539 CPPINTEROP_API TCppType_t GetUnderlyingType(TCppType_t type);
0540
0541
0542 CPPINTEROP_API std::string GetTypeAsString(TCppType_t type);
0543
0544
0545
0546
0547 CPPINTEROP_API TCppType_t GetCanonicalType(TCppType_t type);
0548
0549
0550
0551 CPPINTEROP_API TCppType_t GetType(const std::string& type);
0552
0553
0554 CPPINTEROP_API TCppType_t GetComplexType(TCppType_t element_type);
0555
0556
0557
0558 CPPINTEROP_API TCppType_t GetTypeFromScope(TCppScope_t klass);
0559
0560
0561 CPPINTEROP_API bool IsTypeDerivedFrom(TCppType_t derived, TCppType_t base);
0562
0563
0564
0565 CPPINTEROP_API JitCall MakeFunctionCallable(TCppConstFunction_t func);
0566
0567 CPPINTEROP_API JitCall MakeFunctionCallable(TInterp_t I,
0568 TCppConstFunction_t func);
0569
0570
0571 CPPINTEROP_API bool IsConstMethod(TCppFunction_t method);
0572
0573
0574 CPPINTEROP_API std::string GetFunctionArgDefault(TCppFunction_t func,
0575 TCppIndex_t param_index);
0576
0577
0578 CPPINTEROP_API std::string GetFunctionArgName(TCppFunction_t func,
0579 TCppIndex_t param_index);
0580
0581
0582 CPPINTEROP_API OperatorArity GetOperatorArity(TCppFunction_t op);
0583
0584
0585 CPPINTEROP_API void GetOperator(TCppScope_t scope, Operator op,
0586 std::vector<TCppFunction_t>& operators,
0587 OperatorArity kind = kBoth);
0588
0589
0590
0591
0592
0593
0594 CPPINTEROP_API TInterp_t
0595 CreateInterpreter(const std::vector<const char*>& Args = {},
0596 const std::vector<const char*>& GpuArgs = {});
0597
0598
0599
0600
0601
0602 CPPINTEROP_API TInterp_t GetInterpreter();
0603
0604
0605
0606
0607
0608
0609 CPPINTEROP_API void UseExternalInterpreter(TInterp_t I);
0610
0611
0612 CPPINTEROP_API void AddSearchPath(const char* dir, bool isUser = true,
0613 bool prepend = false);
0614
0615
0616 CPPINTEROP_API const char* GetResourceDir();
0617
0618
0619
0620
0621
0622
0623 CPPINTEROP_API std::string
0624 DetectResourceDir(const char* ClangBinaryName = "clang");
0625
0626
0627
0628
0629
0630
0631 CPPINTEROP_API void
0632 DetectSystemCompilerIncludePaths(std::vector<std::string>& Paths,
0633 const char* CompilerName = "c++");
0634
0635
0636
0637 CPPINTEROP_API void AddIncludePath(const char* dir);
0638
0639
0640
0641
0642 CPPINTEROP_API void GetIncludePaths(std::vector<std::string>& IncludePaths,
0643 bool withSystem = false,
0644 bool withFlags = false);
0645
0646
0647
0648 CPPINTEROP_API int Declare(const char* code, bool silent = false);
0649
0650
0651
0652 CPPINTEROP_API int Process(const char* code);
0653
0654
0655
0656 CPPINTEROP_API intptr_t Evaluate(const char* code, bool* HadError = nullptr);
0657
0658
0659
0660 CPPINTEROP_API std::string LookupLibrary(const char* lib_name);
0661
0662
0663
0664
0665 CPPINTEROP_API bool LoadLibrary(const char* lib_stem, bool lookup = true);
0666
0667
0668
0669
0670 CPPINTEROP_API void UnloadLibrary(const char* lib_stem);
0671
0672
0673
0674
0675 CPPINTEROP_API std::string
0676 SearchLibrariesForSymbol(const char* mangled_name,
0677 bool search_system );
0678
0679
0680
0681
0682
0683
0684
0685
0686
0687 CPPINTEROP_API bool InsertOrReplaceJitSymbol(const char* linker_mangled_name,
0688 uint64_t address);
0689
0690
0691 CPPINTEROP_API std::string ObjToString(const char* type, void* obj);
0692
0693 struct TemplateArgInfo {
0694 TCppType_t m_Type;
0695 const char* m_IntegralValue;
0696 TemplateArgInfo(TCppScope_t type, const char* integral_value = nullptr)
0697 : m_Type(type), m_IntegralValue(integral_value) {}
0698 };
0699
0700
0701
0702
0703
0704
0705
0706
0707
0708
0709
0710 CPPINTEROP_API TCppScope_t
0711 InstantiateTemplate(TCppScope_t tmpl, const TemplateArgInfo* template_args,
0712 size_t template_args_size);
0713
0714
0715
0716
0717
0718 CPPINTEROP_API void
0719 GetClassTemplateInstantiationArgs(TCppScope_t templ_instance,
0720 std::vector<TemplateArgInfo>& args);
0721
0722
0723
0724
0725 CPPINTEROP_API TCppFunction_t
0726 InstantiateTemplateFunctionFromString(const char* function_template);
0727
0728
0729
0730
0731
0732
0733
0734
0735
0736 CPPINTEROP_API TCppFunction_t
0737 BestOverloadFunctionMatch(const std::vector<TCppFunction_t>& candidates,
0738 const std::vector<TemplateArgInfo>& explicit_types,
0739 const std::vector<TemplateArgInfo>& arg_types);
0740
0741 CPPINTEROP_API void GetAllCppNames(TCppScope_t scope,
0742 std::set<std::string>& names);
0743
0744 CPPINTEROP_API void DumpScope(TCppScope_t scope);
0745
0746 namespace DimensionValue {
0747 enum : long int {
0748 UNKNOWN_SIZE = -1,
0749 };
0750 }
0751
0752
0753 CPPINTEROP_API std::vector<long int> GetDimensions(TCppType_t type);
0754
0755
0756 CPPINTEROP_API TCppObject_t Allocate(TCppScope_t scope);
0757
0758
0759 CPPINTEROP_API void Deallocate(TCppScope_t scope, TCppObject_t address);
0760
0761
0762
0763 CPPINTEROP_API TCppObject_t Construct(TCppScope_t scope,
0764 void* arena = nullptr);
0765
0766
0767
0768 CPPINTEROP_API void Destruct(TCppObject_t This, TCppScope_t type,
0769 bool withFree = true);
0770
0771
0772
0773
0774
0775 enum CaptureStreamKind : char {
0776 kStdOut = 1,
0777 kStdErr,
0778
0779
0780 };
0781
0782
0783
0784 CPPINTEROP_API void BeginStdStreamCapture(CaptureStreamKind fd_kind);
0785
0786
0787 CPPINTEROP_API std::string EndStdStreamCapture();
0788
0789
0790
0791
0792
0793
0794
0795
0796
0797 CPPINTEROP_API void CodeComplete(std::vector<std::string>& Results,
0798 const char* code,
0799 unsigned complete_line = 1U,
0800 unsigned complete_column = 1U);
0801
0802
0803
0804
0805 CPPINTEROP_API int Undo(unsigned N = 1);
0806
0807 }
0808
0809 #endif