Skip to content

Commit 5dd4bf2

Browse files
committed
misc. updates
1 parent 2a3c103 commit 5dd4bf2

File tree

4 files changed

+65
-61
lines changed

4 files changed

+65
-61
lines changed

include/clang-c/CXCppInterOp.h

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -358,21 +358,19 @@ bool clang_qualtype_isTypeDerivedFrom(CXQualType derived, CXQualType base);
358358
* Describes the kind of entity that a scope refers to.
359359
*/
360360
enum CXScopeKind {
361-
CXScope_Unexposed = 0,
362-
CXScope_Invalid = 1,
363-
/** The global scope. */
364-
CXScope_Global = 2,
365-
/** Namespaces. */
366-
CXScope_Namespace = 3,
367-
/** Function, methods, constructor etc. */
368-
CXScope_Function = 4,
369-
/** Variables. */
370-
CXScope_Variable = 5,
371-
/** Enum Constants. */
372-
CXScope_EnumConstant = 6,
373-
/** Fields. */
374-
CXScope_Field = 7,
375-
// reserved for future use
361+
CXScope_Unexposed = 1, // FIXME: merge with CXCursorKind?
362+
CXScope_Typedef = 20,
363+
CXScope_Namespace = 22,
364+
CXScope_ClassTemplate = 31,
365+
CXScope_TypeAlias = 36,
366+
CXScope_Invalid = 70,
367+
CXScope_TranslationUnit = 350,
368+
369+
CXScope_Record,
370+
CXScope_Function,
371+
CXScope_Variable,
372+
CXScope_EnumConstant,
373+
CXScope_Field,
376374
};
377375

378376
/**
@@ -835,8 +833,6 @@ CXObject clang_construct(CXScope scope, void* arena);
835833
* Calls the destructor of object of type \c type. When withFree is true it
836834
* calls operator delete/free.
837835
*
838-
* \param I The interpreter.
839-
*
840836
* \param This The object to destruct.
841837
*
842838
* \param type The type of the object.

lib/Interpreter/CXCppInterOp.cpp

Lines changed: 42 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,11 @@ TInterp_t clang_interpreter_takeInterpreterAsPtr(CXInterpreter I) {
7171
}
7272

7373
enum CXErrorCode clang_interpreter_Undo(CXInterpreter I, unsigned int N) {
74+
#ifdef USE_CLING
75+
return CXError_Failure;
76+
#else
7477
return getInterpreter(I)->Undo(N) ? CXError_Failure : CXError_Success;
78+
#endif // USE_CLING
7579
}
7680

7781
void clang_interpreter_dispose(CXInterpreter I) {
@@ -286,19 +290,6 @@ CXQualType clang_qualtype_getCanonicalType(CXQualType type) {
286290
return makeCXQualType(getMeta(type), QT.getCanonicalType());
287291
}
288292

289-
namespace Cpp {
290-
clang::QualType GetType(const std::string& name, clang::Sema& sema);
291-
} // namespace Cpp
292-
293-
CXQualType clang_qualtype_getType(CXInterpreter I, const char* name) {
294-
auto& S = getInterpreter(I)->getSema();
295-
const clang::QualType QT = Cpp::GetType(std::string(name), S);
296-
if (QT.isNull())
297-
return makeCXQualType(I, QT, CXType_Invalid);
298-
299-
return makeCXQualType(I, QT);
300-
}
301-
302293
CXQualType clang_qualtype_getComplexType(CXQualType eltype) {
303294
const auto& C = getInterpreter(eltype)->getSema().getASTContext();
304295
return makeCXQualType(getMeta(eltype), C.getComplexType(getType(eltype)));
@@ -353,6 +344,28 @@ static inline compat::Interpreter* getInterpreter(const CXScope& S) {
353344

354345
void clang_scope_dump(CXScope S) { getDecl(S)->dump(); }
355346

347+
namespace Cpp {
348+
clang::QualType GetBuiltinType(const std::string& name, clang::Sema& sema);
349+
} // namespace Cpp
350+
351+
CXQualType clang_qualtype_getType(CXInterpreter I, const char* name) {
352+
auto& S = getInterpreter(I)->getSema();
353+
const clang::QualType builtin = Cpp::GetBuiltinType(std::string(name), S);
354+
if (!builtin.isNull())
355+
return makeCXQualType(I, builtin);
356+
357+
auto scope = clang_scope_getNamed(name, clang_scope_getGlobalScope(I));
358+
if (isNull(scope))
359+
return makeCXQualType(I, clang::QualType(), CXType_Invalid);
360+
361+
if (auto* TD = llvm::dyn_cast_or_null<clang::TypeDecl>(getDecl(scope))) {
362+
auto QT = clang::QualType(TD->getTypeForDecl(), 0);
363+
return makeCXQualType(I, QT);
364+
}
365+
366+
return makeCXQualType(I, clang::QualType(), CXType_Invalid);
367+
}
368+
356369
CXQualType clang_scope_getTypeFromScope(CXScope S) {
357370
if (isNull(S))
358371
return makeCXQualType(getMeta(S), clang::QualType(), CXType_Invalid);
@@ -605,7 +618,7 @@ CXScopeSet* clang_scope_getUsingNamespaces(CXScope S) {
605618
CXScope clang_scope_getGlobalScope(CXInterpreter I) {
606619
const auto& C = getInterpreter(I)->getSema().getASTContext();
607620
auto* DC = C.getTranslationUnitDecl();
608-
return makeCXScope(I, DC, CXScope_Global);
621+
return makeCXScope(I, DC, CXScope_TranslationUnit);
609622
}
610623

611624
CXScope clang_scope_getUnderlyingScope(CXScope S) {
@@ -627,11 +640,21 @@ CXScope clang_scope_getScope(const char* name, CXScope parent) {
627640
return S;
628641

629642
auto* ND = llvm::dyn_cast<clang::NamedDecl>(getDecl(S));
630-
if (llvm::isa<clang::NamespaceDecl>(ND) || llvm::isa<clang::RecordDecl>(ND) ||
631-
llvm::isa<clang::ClassTemplateDecl>(ND) ||
632-
llvm::isa<clang::TypedefNameDecl>(ND)) {
633-
return makeCXScope(getMeta(S), ND->getCanonicalDecl());
634-
}
643+
if (llvm::isa<clang::NamespaceDecl>(ND))
644+
return makeCXScope(getMeta(S), ND->getCanonicalDecl(), CXScope_Namespace);
645+
646+
if (llvm::isa<clang::TypedefDecl>(ND))
647+
return makeCXScope(getMeta(S), ND->getCanonicalDecl(), CXScope_Typedef);
648+
649+
if (llvm::isa<clang::TypeAliasDecl>(ND))
650+
return makeCXScope(getMeta(S), ND->getCanonicalDecl(), CXScope_TypeAlias);
651+
652+
if (llvm::isa<clang::RecordDecl>(ND))
653+
return makeCXScope(getMeta(S), ND->getCanonicalDecl(), CXScope_Record);
654+
655+
if (llvm::isa<clang::ClassTemplateDecl>(ND))
656+
return makeCXScope(getMeta(S), ND->getCanonicalDecl(),
657+
CXScope_ClassTemplate);
635658

636659
return S;
637660
}

lib/Interpreter/CppInterOp.cpp

Lines changed: 9 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -571,27 +571,21 @@ namespace Cpp {
571571
return GetScope(name.substr(start, end), curr_scope);
572572
}
573573

574-
Decl* GetNamedImpl(Sema* sema, const std::string& name,
575-
Decl* parent /*= nullptr*/) {
574+
TCppScope_t GetNamed(const std::string& name,
575+
TCppScope_t parent /*= nullptr*/) {
576576
clang::DeclContext *Within = 0;
577577
if (parent) {
578-
Decl* D = GetUnderlyingScope(parent);
578+
auto* D = (clang::Decl*)parent;
579+
D = GetUnderlyingScope(D);
579580
Within = llvm::dyn_cast<clang::DeclContext>(D);
580581
}
581582

582-
auto* ND = Cpp_utils::Lookup::Named(sema, name, Within);
583+
auto* ND = Cpp_utils::Lookup::Named(&getSema(), name, Within);
583584
if (ND && ND != (clang::NamedDecl*) -1) {
584-
return ND->getCanonicalDecl();
585+
return (TCppScope_t)(ND->getCanonicalDecl());
585586
}
586587

587-
return nullptr;
588-
}
589-
590-
TCppScope_t GetNamed(const std::string& name,
591-
TCppScope_t parent /*= nullptr*/) {
592-
auto& sema = getSema();
593-
auto* D = static_cast<Decl*>(parent);
594-
return GetNamedImpl(&sema, name, D);
588+
return 0;
595589
}
596590

597591
TCppScope_t GetParentScope(TCppScope_t scope)
@@ -1457,17 +1451,8 @@ namespace Cpp {
14571451
}
14581452
}
14591453

1460-
QualType GetType(const std::string& name, Sema& sema) {
1461-
QualType builtin = findBuiltinType(name, sema.getASTContext());
1462-
if (!builtin.isNull())
1463-
return builtin;
1464-
1465-
auto* D = (Decl*)GetNamedImpl(&sema, name, /* Within= */ 0);
1466-
if (auto* TD = llvm::dyn_cast_or_null<TypeDecl>(D)) {
1467-
return QualType(TD->getTypeForDecl(), 0);
1468-
}
1469-
1470-
return QualType();
1454+
QualType GetBuiltinType(const std::string& name, Sema& sema) {
1455+
return findBuiltinType(name, sema.getASTContext());
14711456
}
14721457

14731458
TCppType_t GetType(const std::string &name) {

unittests/CppInterOp/ScopeReflectionTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ TEST(ScopeReflectionTest, GetNamed) {
783783
auto NS_N1 = clang_scope_getNamed("N1", GS);
784784
auto NS_N2 = clang_scope_getNamed("N2", NS_N1);
785785
auto CL_C = clang_scope_getNamed("C", NS_N2);
786-
auto EN_E = clang_scope_getScope("E", CL_C);
786+
auto EN_E = clang_scope_getNamed("E", CL_C);
787787

788788
EXPECT_EQ(C_API_SHIM(NS_N1), "N1");
789789
EXPECT_EQ(C_API_SHIM(NS_N2), "N1::N2");

0 commit comments

Comments
 (0)