Skip to content

Commit

Permalink
[v0.0.3] Big refactoring! See message for details.
Browse files Browse the repository at this point in the history
So, it was a long refactoring session, but now we have a few breaking changes:
1. Removed type alias. Now RG3 will try to expand whole type as separated (from original type) with all information.
2. Added 'annotations' method to annotate type (like comments but in place where extra comments not available)
3. Refactored global routing system: now logic divided between CxxRouterVisitor (root visitor), CxxClassTypeVisitor (non-templated classes), CxxTemplateSpecializationVisitor (template specialization) and CxxTypeVisitor (trivial, enum types and aliases)
4. Removed template specialization type kind. Now all templates are part of TK_STRUCT_OR_CLASS. NOTE: In future I'll add flag into type base to check is type templated or not.
5. Fixed sOwnerClassName field in class member methods. Now this field contains pretty name of owner class (with template view when it's templated)
6. Added tests to support unsigned enum
  • Loading branch information
DronCode committed Mar 31, 2024
1 parent 50d3d7b commit d7c80c5
Show file tree
Hide file tree
Showing 32 changed files with 1,247 additions and 1,469 deletions.
2 changes: 2 additions & 0 deletions Cpp/include/RG3/Cpp/CppNamespace.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ namespace rg3::cpp

const std::string& asString() const { return m_sNamespace; }

bool isEmpty() const { return m_vNamespace.empty(); }

private:
void parseNamespace();

Expand Down
6 changes: 6 additions & 0 deletions Cpp/include/RG3/Cpp/Tag.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,5 +95,11 @@ namespace rg3::cpp
[[nodiscard]] Tag getTag(const std::string& tag) const;
[[nodiscard]] const Storage_t& getTags() const;
[[nodiscard]] Storage_t& getTags();

bool isEmpty() const { return m_tags.empty(); }

Tags& operator+=(const Tags& another);
Tags& operator+=(const Tag& another);
friend Tags operator+(const Tags& a, const Tags& b);
};
}
36 changes: 0 additions & 36 deletions Cpp/include/RG3/Cpp/TypeAlias.h

This file was deleted.

7 changes: 7 additions & 0 deletions Cpp/include/RG3/Cpp/TypeBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,13 @@ namespace rg3::cpp
[[nodiscard]] bool operator==(const TypeBase& other) const { return areSame(&other); }
[[nodiscard]] bool operator!=(const TypeBase& other) const { return !areSame(&other); }

void overrideTypeData(const std::string& name, const std::string& prettyName);
void overrideTypeData(const std::string& name, const std::string& prettyName, const CppNamespace& aNamespace);
void overrideTypeData(const std::string& name, const std::string& prettyName, const CppNamespace& aNamespace, const DefinitionLocation& aLocation);
void overrideTypeData(const std::string& name, const std::string& prettyName, const CppNamespace& aNamespace, const DefinitionLocation& aLocation, const Tags& tags);

void addTags(const Tags& vTags);

protected:
virtual bool doAreSame(const TypeBase* pOther) const;

Expand Down
2 changes: 0 additions & 2 deletions Cpp/include/RG3/Cpp/TypeKind.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,5 @@ namespace rg3::cpp
TK_TRIVIAL, ///< See TypeBase
TK_ENUM, ///< See TypeEnum
TK_STRUCT_OR_CLASS, ///< See TypeClass
TK_ALIAS, ///< See TypeAlias
TK_TEMPLATE_SPECIALIZATION
};
}
35 changes: 35 additions & 0 deletions Cpp/source/Tag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,39 @@ namespace rg3::cpp
{
return m_tags;
}
Tags operator+(const Tags& a, const Tags& b)
{
Tags result = a;

for (const auto& [name, tag] : b.getTags())
{
// DronCode: maybe we need to avoid of override keys here?
result.getTags()[name] = tag;
}

return result;
}

Tags& Tags::operator+=(const Tags& another)
{
for (const auto& [name, tag] : another.getTags())
{
if (m_tags.contains(name))
continue;

m_tags[name] = tag;
}

return *this;
}

Tags& Tags::operator+=(const Tag& another)
{
if (!m_tags.contains(another.getName()))
{
m_tags[another.getName()] = another;
}

return *this;
}
}
48 changes: 0 additions & 48 deletions Cpp/source/TypeAlias.cpp

This file was deleted.

35 changes: 35 additions & 0 deletions Cpp/source/TypeBase.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,41 @@ namespace rg3::cpp
return m_tags;
}

void TypeBase::overrideTypeData(const std::string& name, const std::string& prettyName)
{
m_name = name;
m_prettyName = prettyName;
}

void TypeBase::overrideTypeData(const std::string& name, const std::string& prettyName, const rg3::cpp::CppNamespace& aNamespace)
{
m_name = name;
m_prettyName = prettyName;
m_nameSpace = aNamespace;
}

void TypeBase::overrideTypeData(const std::string& name, const std::string& prettyName, const rg3::cpp::CppNamespace& aNamespace, const rg3::cpp::DefinitionLocation& aLocation)
{
m_name = name;
m_prettyName = prettyName;
m_nameSpace = aNamespace;
m_location = aLocation;
}

void TypeBase::overrideTypeData(const std::string& name, const std::string& prettyName, const CppNamespace& aNamespace, const DefinitionLocation& aLocation, const Tags& tags)
{
m_name = name;
m_prettyName = prettyName;
m_nameSpace = aNamespace;
m_location = aLocation;
m_tags = tags;
}

void TypeBase::addTags(const Tags& vTags)
{
m_tags += vTags;
}

bool TypeBase::doAreSame(const TypeBase* pOther) const
{
return
Expand Down
5 changes: 5 additions & 0 deletions LLVM/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ target_include_directories(RG3_LLVM PUBLIC ${CLANG_INCLUDE_DIRS})
target_include_directories(RG3_LLVM PUBLIC ${LLVM_INCLUDE_DIRS})
target_include_directories(RG3_LLVM PRIVATE ${CMAKE_CURRENT_BINARY_DIR})

if (MSVC)
# /bigobj support
target_compile_options(RG3_LLVM PUBLIC /bigobj)
endif()

target_link_libraries(RG3_LLVM PUBLIC
# LLVM
LLVMCore
Expand Down
76 changes: 76 additions & 0 deletions LLVM/include/RG3/LLVM/Annotations.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#pragma once

#include <clang/AST/Decl.h>

#include <RG3/Cpp/Tag.h>
#include <RG3/Cpp/TypeClass.h>

#include <algorithm>
#include <optional>
#include <string>
#include <vector>


namespace rg3::llvm
{
struct PropertyDescription
{
std::string propertyRefName {}; // A reference name to property (original)
std::string propertyAliasName {}; // A new name of property (alias)
};

struct ExtraPropertiesFilter
{
const std::vector<PropertyDescription>& vKnownProperties;

bool operator()(const std::string& sPropertyName) const
{
auto it = std::find_if(vKnownProperties.begin(), vKnownProperties.end(), [&sPropertyName](const PropertyDescription& pd) -> bool {
return pd.propertyRefName == sPropertyName;
});

return it != vKnownProperties.end();
}

/**
* @brief Mutator overflow: this method return true if property acceptable and modified for future use
*/
bool operator()(cpp::ClassProperty& sProperty)
{
auto it = std::find_if(vKnownProperties.begin(), vKnownProperties.end(), [&sProperty](const PropertyDescription& pd) -> bool {
return pd.propertyRefName == sProperty.sName;
});

if (it != vKnownProperties.end())
{
sProperty.sAlias = it->propertyAliasName;
return true;
}

return false;
}
};

struct ExtraFunctionsFilter
{
const std::vector<std::string>& vKnownFunctions;

bool operator()(const std::string& sFuncName) const
{
return std::find(vKnownFunctions.begin(), vKnownFunctions.end(), sFuncName) != vKnownFunctions.end();
}
};

struct Annotations
{
bool bIsRuntime { false };
std::optional<std::string> overrideLocation {};
std::vector<PropertyDescription> knownProperties {};
std::vector<std::string> knownFunctions {};
cpp::Tags additionalTags {};

[[nodiscard]] bool isRuntime() const { return bIsRuntime; }

void collectFromDecl(clang::Decl* pDecl);
};
}
18 changes: 1 addition & 17 deletions LLVM/include/RG3/LLVM/Visitors/CxxClassTypeVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <clang/AST/ASTConsumer.h>

#include <RG3/LLVM/CompilerConfig.h>
#include <RG3/LLVM/Annotations.h>
#include <RG3/Cpp/TypeClass.h>
#include <functional>
#include <vector>
Expand Down Expand Up @@ -34,23 +35,6 @@ namespace rg3::llvm::visitors
// Extra types
std::vector<rg3::cpp::TypeBasePtr> vFoundExtraTypes {};

public: // Types

struct PropertyDescription
{
std::string propertyRefName {}; // A reference name to property (original)
std::string propertyAliasName {}; // A new name of property (alias)
};

private:
void handleTypeAnnotation(clang::CXXRecordDecl* cxxRecordDecl, clang::AnnotateAttr* pAnnotateAttr);
void handleAndMergeTypedefTypeWithInner(const clang::TypedefType* pType, const std::vector<PropertyDescription>& vKnownProperties, const std::vector<std::string>& vKnownFunctions);
void handleCxxDeclAndOverridePropertiesOwner(const clang::CXXRecordDecl* pCxxDecl,
const rg3::cpp::TypeBase* pNewOwnerType,
CxxClassTypeVisitor& sTypeVisitor,
const std::function<bool(const std::string&)>& propFilter,
const std::function<bool(const std::string&)>& funcFilter);

private:
const CompilerConfig& compilerConfig;
};
Expand Down
39 changes: 39 additions & 0 deletions LLVM/include/RG3/LLVM/Visitors/CxxRouterVisitor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once

#include <clang/AST/RecursiveASTVisitor.h>
#include <clang/AST/ASTConsumer.h>

#include <RG3/LLVM/CompilerConfig.h>
#include <RG3/LLVM/Annotations.h>
#include <RG3/Cpp/TypeBase.h>

#include <vector>


namespace rg3::llvm::visitors
{
/**
* @brief This class handle all types and understand which visitor should handle this type.
*
*/
class CxxRouterVisitor : public clang::RecursiveASTVisitor<CxxRouterVisitor>
{
public:
CxxRouterVisitor(std::vector<rg3::cpp::TypeBasePtr>& vFoundTypes, const CompilerConfig& compilerConfig);

public: // visitors
bool VisitCXXRecordDecl(clang::CXXRecordDecl* cxxRecordDecl); // For C++ types (struct, class)
bool VisitEnumDecl(clang::EnumDecl* enumDecl); // For enumerations
bool VisitTypedefNameDecl(clang::TypedefNameDecl* typedefNameDecl); // For aliases (typedef, using)

private:
bool handleAnnotationBasedType(const clang::Type* pType,
const rg3::llvm::Annotations& annotation,
const clang::ASTContext& ctx,
bool bDirectInvoke);

private:
const CompilerConfig& m_compilerConfig;
std::vector<rg3::cpp::TypeBasePtr>& m_vFoundTypes;
};
}
5 changes: 0 additions & 5 deletions LLVM/include/RG3/LLVM/Visitors/CxxTypeVisitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,6 @@ namespace rg3::llvm::visitors

bool VisitCXXRecordDecl(clang::CXXRecordDecl* cxxRecordDecl);

bool VisitTypedefNameDecl(clang::TypedefNameDecl* typedefNameDecl);

private:
bool HandleNamedTypedefDecl(clang::TypedefNameDecl* typedefNameDecl);

private:
template <typename T>
static bool isDeclInsideClassOrStruct(T* pDecl) requires (std::is_base_of_v<clang::Decl, T>)
Expand Down
Loading

0 comments on commit d7c80c5

Please sign in to comment.