Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions sources/ClangSharp.Interop/CX_DestructorType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) Microsoft and Contributors. All rights reserved. Licensed under the University of Illinois/NCSA Open Source License. See LICENSE.txt in the project root for license information.
// Ported from https://github.com/microsoft/ClangSharp/blob/main/sources/libClangSharp

namespace ClangSharp.Interop
{
public enum CX_DestructorType
{
Deleting,
Complete,
Base,
Comdat
}
}
4 changes: 4 additions & 0 deletions sources/ClangSharp.Interop/clangsharp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,10 @@ public static partial class clangsharp
[DllImport("libClangSharp", CallingConvention = CallingConvention.Cdecl, EntryPoint = "clangsharp_Cursor_getVBase", ExactSpelling = true)]
public static extern CXCursor Cursor_getVBase(CXCursor C, [NativeTypeName("unsigned int")] uint i);

[DllImport("libClangSharp", CallingConvention = CallingConvention.Cdecl, EntryPoint = "clangsharp_Cursor_getDtorVtblIdx", ExactSpelling = true)]
[return: NativeTypeName("int64_t")]
public static extern long Cursor_getDtorVtblIdx(CXCursor C, CX_DestructorType dtor);

[DllImport("libClangSharp", CallingConvention = CallingConvention.Cdecl, EntryPoint = "clangsharp_Cursor_getVtblIdx", ExactSpelling = true)]
[return: NativeTypeName("int64_t")]
public static extern long Cursor_getVtblIdx(CXCursor C);
Expand Down
52 changes: 41 additions & 11 deletions sources/libClangSharp/ClangSharp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@
#include "CXTranslationUnit.h"
#include "CXType.h"

#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4146 4244 4267 4291 4624 4996)
#endif

#include <clang/Basic/SourceManager.h>

#ifdef _MSC_VER
#pragma warning(pop)
#endif

using namespace clang;
using namespace clang::cxcursor;
Expand Down Expand Up @@ -61,6 +65,25 @@ bool isStmtOrExpr(CXCursorKind kind) {
return clang_isStatement(kind) || clang_isExpression(kind);
}

int64_t getVtblIdx(const GlobalDecl& d)
{
const CXXMethodDecl* CMD = static_cast<const CXXMethodDecl*>(d.getDecl());
if (VTableContextBase::hasVtableSlot(CMD)) {
VTableContextBase* VTC = CMD->getASTContext().getVTableContext();

if (MicrosoftVTableContext* MSVTC = dyn_cast<MicrosoftVTableContext>(VTC)) {
MethodVFTableLocation ML = MSVTC->getMethodVFTableLocation(d);
return ML.Index;
}

if (ItaniumVTableContext* IVTC = dyn_cast<ItaniumVTableContext>(VTC)) {
return IVTC->getMethodVTableIndex(d);
}
}

return -1;
}

CXCursor clangsharp_Cursor_getArgument(CXCursor C, unsigned i) {
if (isDeclOrTU(C.kind)) {
const Decl* D = getCursorDecl(C);
Expand Down Expand Up @@ -4693,23 +4716,30 @@ CXCursor clangsharp_Cursor_getVBase(CXCursor C, unsigned i) {
return clang_getNullCursor();
}

int64_t clangsharp_Cursor_getVtblIdx(CXCursor C) {
int64_t clangsharp_Cursor_getDtorVtblIdx(CXCursor C, CX_DestructorType dtor)
{
if (isDeclOrTU(C.kind)) {
const Decl* D = getCursorDecl(C);

if (const CXXMethodDecl* CMD = dyn_cast<CXXMethodDecl>(D)) {
if (VTableContextBase::hasVtableSlot(CMD)) {
VTableContextBase* VTC = getASTUnit(getCursorTU(C))->getASTContext().getVTableContext();
if (const CXXDestructorDecl* CMD = dyn_cast<CXXDestructorDecl>(D)) {
return getVtblIdx(GlobalDecl(CMD, static_cast<CXXDtorType>(dtor)));
}
}
return -1;
}

if (MicrosoftVTableContext* MSVTC = dyn_cast<MicrosoftVTableContext>(VTC)) {
MethodVFTableLocation ML = MSVTC->getMethodVFTableLocation(CMD);
return ML.Index;
}
int64_t clangsharp_Cursor_getVtblIdx(CXCursor C) {
if (isDeclOrTU(C.kind)) {
const Decl* D = getCursorDecl(C);

if (ItaniumVTableContext* IVTC = dyn_cast<ItaniumVTableContext>(VTC)) {
return IVTC->getMethodVTableIndex(CMD);
}
if (const CXXMethodDecl* CMD = dyn_cast<CXXMethodDecl>(D)) {
int64_t dtorIdx = clangsharp_Cursor_getDtorVtblIdx(C, Deleting); // will test if CMD is a dtor
if (dtorIdx != -1) { // yes, it is a dtor
return dtorIdx;
}

// no, it is a regular method
return getVtblIdx(CMD);
}
}

Expand Down
13 changes: 13 additions & 0 deletions sources/libClangSharp/ClangSharp.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
#ifndef LIBCLANGSHARP_CLANGSHARP_H
#define LIBCLANGSHARP_CLANGSHARP_H

#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4146 4244 4267 4291 4624 4996)
#endif

#include <clang/AST/Decl.h>
#include <clang/AST/DeclCXX.h>
Expand All @@ -19,7 +21,9 @@
#include <clang/Basic/Specifiers.h>
#include <clang-c/Index.h>

#ifdef _MSC_VER
#pragma warning(pop)
#endif

#ifdef __cplusplus
#define EXTERN_C extern "C"
Expand Down Expand Up @@ -202,6 +206,13 @@ enum CX_VariableCaptureKind {
CX_VCK_VLAType = clang::CapturedStmt::VCK_VLAType + 1
};

enum CX_DestructorType {
Deleting = clang::Dtor_Deleting,
Complete = clang::Dtor_Complete,
Base = clang::Dtor_Base,
Comdat = clang::Dtor_Comdat
};

struct CX_TemplateArgument {
CXTemplateArgumentKind kind;
int xdata;
Expand Down Expand Up @@ -722,6 +733,8 @@ CLANGSHARP_LINKAGE CXCursor clangsharp_Cursor_getUsedContext(CXCursor C);

CLANGSHARP_LINKAGE CXCursor clangsharp_Cursor_getVBase(CXCursor C, unsigned i);

CLANGSHARP_LINKAGE int64_t clangsharp_Cursor_getDtorVtblIdx(CXCursor C, CX_DestructorType dtor);

CLANGSHARP_LINKAGE int64_t clangsharp_Cursor_getVtblIdx(CXCursor C);

CLANGSHARP_LINKAGE void clangsharp_TemplateArgument_dispose(CX_TemplateArgument T);
Expand Down