feat update

This commit is contained in:
tqcq 2024-03-02 18:06:59 +08:00
parent 9485047cc2
commit ce0755c247
10 changed files with 155 additions and 29 deletions

View File

@ -17,7 +17,8 @@ add_executable(meta
set(CLANG_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/LLVM/include")
if (CMAKE_SYSTEM_NAME STREQUAL "Linux")
message(FATAL "${CMAKE_SYSTEM_NAME} is not supported")
set(CLANG_LIB_DIR "${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/LLVM/bin/Linux")
set(CLANG_LIB_LIBRARIES "${CLANG_LIB_DIR}/libclang.so.12")
elseif (CMAKE_SYSTEM_NAME STREQUAL "Windows")
message(FATAL "${CMAKE_SYSTEM_NAME} is not supported")
elseif (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
@ -30,5 +31,5 @@ elseif (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
endif()
endif()
target_include_directories(meta PRIVATE ${CLANG_INCLUDE_DIR} src)
target_include_directories(meta PRIVATE ${CLANG_INCLUDE_DIR} ${CMAKE_CURRENT_SOURCE_DIR}/src)
target_link_libraries(meta PRIVATE sled ${CLANG_LIB_LIBRARIES})

11
runtime/reflection.cc Normal file
View File

@ -0,0 +1,11 @@
#include "reflection.h"
namespace meta {
std::atomic<int> next_type_id{0};
void
NoOp(void *)
{}
Object::Object() : id_(GetTypeId<void>()), data_(nullptr), deleter_(NoOp) {}
}// namespace meta

61
runtime/reflection.h Normal file
View File

@ -0,0 +1,61 @@
#pragma once
#ifndef META_RUNTIME_REFLECTION_H
#define META_RUNTIME_REFLECTION_H
#include <atomic>
#include <functional>
#include <sled/any.h>
#include <string>
#include <typeindex>
#include <vector>
namespace meta {
#define __REFLECTION_PARSER_
#if defined(__REFLECTION_PARSER_)
#define CLASS(class_name, ...) \
class __attribute__((annotate(#__VA_ARGS__))) class_name
#define STRUCT(struct_name, ...) \
struct __attribute__((annotate(#__VA_ARGS__))) struct_name
#else
#define META(...)
#define CLASS(class_name, ...) class class_name
#define STRUCT(struct_name, ...) struct struct_name
#endif// __REFLECTION_PARSER_
#define REFLECTION_BODY(class_name)
#define REFLECT_TYPE(class_name)
class TypeInterface {
public:
virtual std::string name() const = 0;
};
class Class;
class Method : public TypeInterface {
public:
virtual std::string name() = 0;
template<typename ReturnT, typename... Args>
ReturnT Invoke(void *instance, Args... args) const
{
std::vector<sled::any> arguments = {args...};
return sled::any_cast<ReturnT>(
std::forward<sled::any>(Invoke(instance, arguments)));
}
sled::any Invoke(void *instance, std::vector<sled::any> &args) const
{
return InvokeImpl(instance, args);
}
protected:
virtual sled::any InvokeImpl(void *instance,
std::vector<sled::any> &args) const = 0;
};
class Field;
}// namespace meta
#endif// META_RUNTIME_REFLECTION_H

View File

@ -11,7 +11,7 @@ public:
using List = std::vector<Cursor>;
using Visitor = CXCursorVisitor;
Cursor(const CXCursor& handle);
Cursor(const CXCursor &handle);
CXCursorKind GetKind(void);
std::string GetSpelling(void) const;
@ -20,13 +20,12 @@ public:
bool IsDefinition(void) const;
CursorType GetType(void) const;
List GetChildren(void) const;
void VisitChildren(Visitor visitor, void* data) const;
void VisitChildren(Visitor visitor, void *data) const;
private:
CXCursor handle_;
};
}// namespace meta
} // namespace meta;
#endif // META_CLANG_CURSOR_H
#endif// META_CLANG_CURSOR_H

View File

@ -9,12 +9,15 @@ extern "C" {
}
#endif
#include <string>
#include <vector>
namespace meta {
class Cursor;
class CursorType {
public:
CursorType(const CXType& handle);
CursorType(const CXType &handle);
std::string GetDisplayName(void) const;
int GetArgumentCount(void) const;
@ -27,8 +30,5 @@ public:
private:
CXType handle_;
};
}
#endif // META_CLANG_CURSOR_TYPE_H
}// namespace meta
#endif// META_CLANG_CURSOR_TYPE_H

View File

@ -0,0 +1,15 @@
#pragma once
#ifndef META_LANGUAGE_TYPES_FIELD_H
#define META_LANGUAGE_TYPES_FIELD_H
#include "type.h"
namespace meta {
class Field : public TypeInfo {
public:
virtual bool IsReadable() const;
virtual bool IsWritable() const;
virtual void *GetValue(void *instance) const;
virtual void SetValue(void *instance, void *value);
};
}// namespace meta
#endif// META_LANGUAGE_TYPES_FIELD_H

14
src/language_types/type.h Normal file
View File

@ -0,0 +1,14 @@
#pragma once
#ifndef META_LANGUAGE_TYPES_TYPE_H
#define META_LANGUAGE_TYPES_TYPE_H
#include <string>
class TypeInfo {
public:
virtual ~TypeInfo() = default;
virtual std::string name() const = 0;
virtual std::string qualified_name() const = 0;
};
#endif// META_LANGUAGE_TYPES_TYPE_H

View File

@ -1,21 +1,25 @@
#include <sled/log/log.h>
#include "clang/cursor.h"
#include <sled/log/log.h>
const char* kTag = "main";
const char *kTag = "main";
int main(int argc, char* argv[]) {
int
main(int argc, char *argv[])
{
LOGI(kTag, "");
CXIndex index = clang_createIndex(0, 0);
CXTranslationUnit unit = clang_parseTranslationUnit(index, "/tmp/main.cc", nullptr, 0, nullptr, 0, CXTranslationUnit_None);
CXIndex index = clang_createIndex(0, 0);
CXTranslationUnit unit = clang_parseTranslationUnit(
index, "/tmp/main.cc", nullptr, 0, nullptr, 0, CXTranslationUnit_None);
CXString file_name = clang_getTranslationUnitSpelling(unit);
LOGI(kTag, "file_name={}", clang_getCString(file_name));
meta::Cursor cursor = clang_getTranslationUnitCursor(unit);
for (auto& child : cursor.GetChildren()) {
for (auto &child : cursor.GetChildren()) {
auto kind = child.GetKind();
if (child.IsDefinition() && (kind == CXCursor_ClassDecl || kind == CXCursor_StructDecl)) {
if (child.IsDefinition()
&& (kind == CXCursor_ClassDecl || kind == CXCursor_StructDecl)) {
LOGV(kTag, "class={}", child.GetDisplayName());
}
}

View File

@ -8,13 +8,16 @@
namespace meta {
class MetaInfo {
public:
MetaInfo(const Cursor& cursor);
std::string GetProperty(const std::string& key) const;
bool GetFlag(const std::string& key) const;
private:
using Property = std::pair<std::string, std::string> ;
std::unordered_map<std::string, Property> properties_;
};
}
MetaInfo(const Cursor &cursor);
std::string GetProperty(const std::string &key) const;
bool GetFlag(const std::string &key) const;
#endif // META_CLANG_META_INFO_H
private:
using Property = std::pair<std::string, std::string>;
std::unordered_map<std::string, Property> properties_;
std::vector<Property> ExtractProperties(const Cursor &cursor) const;
};
}// namespace meta
#endif// META_CLANG_META_INFO_H

18
src/parser/parser.h Normal file
View File

@ -0,0 +1,18 @@
#pragma once
#ifndef META_CLANG_PARSER_PARSER_H
#define META_CLANG_PARSER_PARSER_H
#include <clang-c/Index.h>
#include <string>
namespace meta {
class MetaParser {
public:
MetaParser(const std::string &file_name);
~MetaParser();
private:
CXIndex index_;
CXTranslationUnit translation_unit_;
};
}// namespace meta
#endif// META_CLANG_PARSER_PARSER_H