Skip to content

Resolve static attributes of records #322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 5 additions & 3 deletions include/clang/Interpreter/CppInterOp.h
Original file line number Diff line number Diff line change
Expand Up @@ -424,11 +424,13 @@ namespace Cpp {
/// Checks if the provided parameter is a 'Virtual' method.
CPPINTEROP_API bool IsVirtualMethod(TCppFunction_t method);

/// Gets all the Fields/Data Members of a Class. For now, it
/// only gets non-static data members but in a future update,
/// it may support getting static data members as well.
/// Gets all the Fields/Data Members of a Class
CPPINTEROP_API std::vector<TCppScope_t> GetDatamembers(TCppScope_t scope);

/// Gets all the Static Fields/Data Members of a Class
CPPINTEROP_API std::vector<TCppScope_t>
GetStaticDatamembers(TCppScope_t scope);

/// This is a Lookup function to be used specifically for data members.
CPPINTEROP_API TCppScope_t LookupDatamember(const std::string& name,
TCppScope_t parent);
Expand Down
9 changes: 6 additions & 3 deletions lib/Interpreter/CppInterOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1155,15 +1155,18 @@ namespace Cpp {
stack_begin.back()++;
}

// static field member
GetClassDecls<VarDecl>(scope, datamembers);

return datamembers;
}

return {};
}

std::vector<TCppScope_t> GetStaticDatamembers(TCppScope_t scope) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should pass the std::vector as an out parameter.

std::vector<TCppScope_t> datamembers;
GetClassDecls<VarDecl>(scope, datamembers);
return datamembers;
}

TCppScope_t LookupDatamember(const std::string& name, TCppScope_t parent) {
clang::DeclContext *Within = 0;
if (parent) {
Expand Down
15 changes: 10 additions & 5 deletions unittests/CppInterOp/VariableReflectionTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,17 +31,22 @@ TEST(VariableReflectionTest, GetDatamembers) {
GetAllTopLevelDecls(code, Decls);
auto datamembers = Cpp::GetDatamembers(Decls[0]);
auto datamembers1 = Cpp::GetDatamembers(Decls[1]);
auto static_datamembers = Cpp::GetStaticDatamembers(Decls[0]);
auto static_datamembers1 = Cpp::GetStaticDatamembers(Decls[1]);

// non static field first
EXPECT_EQ(Cpp::GetQualifiedName(datamembers[0]), "C::a");
EXPECT_EQ(Cpp::GetQualifiedName(datamembers[1]), "C::c");
EXPECT_EQ(Cpp::GetQualifiedName(datamembers[2]), "C::e");
// static fields
EXPECT_EQ(Cpp::GetQualifiedName(datamembers[3]), "C::b");
EXPECT_EQ(Cpp::GetQualifiedName(datamembers[4]), "C::d");
EXPECT_EQ(Cpp::GetQualifiedName(datamembers[5]), "C::f");
EXPECT_EQ(datamembers.size(), 6);
EXPECT_EQ(datamembers.size(), 3);
EXPECT_EQ(datamembers1.size(), 0);

// static fields
EXPECT_EQ(Cpp::GetQualifiedName(static_datamembers[0]), "C::b");
EXPECT_EQ(Cpp::GetQualifiedName(static_datamembers[1]), "C::d");
EXPECT_EQ(Cpp::GetQualifiedName(static_datamembers[2]), "C::f");
EXPECT_EQ(static_datamembers.size(), 3);
EXPECT_EQ(static_datamembers1.size(), 0);
}

#define CODE \
Expand Down
Loading