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") set(CLANG_INCLUDE_DIR "${CMAKE_CURRENT_SOURCE_DIR}/3rdparty/LLVM/include")
if (CMAKE_SYSTEM_NAME STREQUAL "Linux") 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") elseif (CMAKE_SYSTEM_NAME STREQUAL "Windows")
message(FATAL "${CMAKE_SYSTEM_NAME} is not supported") message(FATAL "${CMAKE_SYSTEM_NAME} is not supported")
elseif (CMAKE_SYSTEM_NAME STREQUAL "Darwin") elseif (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
@ -30,5 +31,5 @@ elseif (CMAKE_SYSTEM_NAME STREQUAL "Darwin")
endif() endif()
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}) 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

@ -26,7 +26,6 @@ private:
CXCursor handle_; CXCursor handle_;
}; };
}// namespace meta
} // namespace meta;
#endif// META_CLANG_CURSOR_H #endif// META_CLANG_CURSOR_H

View File

@ -9,9 +9,12 @@ extern "C" {
} }
#endif #endif
#include <string>
#include <vector> #include <vector>
namespace meta { namespace meta {
class Cursor; class Cursor;
class CursorType { class CursorType {
public: public:
CursorType(const CXType &handle); CursorType(const CXType &handle);
@ -27,8 +30,5 @@ public:
private: private:
CXType handle_; CXType handle_;
}; };
} }// namespace meta
#endif// META_CLANG_CURSOR_TYPE_H #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,12 +1,15 @@
#include <sled/log/log.h>
#include "clang/cursor.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, ""); LOGI(kTag, "");
CXIndex index = clang_createIndex(0, 0); CXIndex index = clang_createIndex(0, 0);
CXTranslationUnit unit = clang_parseTranslationUnit(index, "/tmp/main.cc", nullptr, 0, nullptr, 0, CXTranslationUnit_None); CXTranslationUnit unit = clang_parseTranslationUnit(
index, "/tmp/main.cc", nullptr, 0, nullptr, 0, CXTranslationUnit_None);
CXString file_name = clang_getTranslationUnitSpelling(unit); CXString file_name = clang_getTranslationUnitSpelling(unit);
LOGI(kTag, "file_name={}", clang_getCString(file_name)); LOGI(kTag, "file_name={}", clang_getCString(file_name));
@ -15,7 +18,8 @@ int main(int argc, char* argv[]) {
for (auto &child : cursor.GetChildren()) { for (auto &child : cursor.GetChildren()) {
auto kind = child.GetKind(); 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()); LOGV(kTag, "class={}", child.GetDisplayName());
} }
} }

View File

@ -11,10 +11,13 @@ public:
MetaInfo(const Cursor &cursor); MetaInfo(const Cursor &cursor);
std::string GetProperty(const std::string &key) const; std::string GetProperty(const std::string &key) const;
bool GetFlag(const std::string &key) const; bool GetFlag(const std::string &key) const;
private: private:
using Property = std::pair<std::string, std::string>; using Property = std::pair<std::string, std::string>;
std::unordered_map<std::string, Property> properties_; std::unordered_map<std::string, Property> properties_;
std::vector<Property> ExtractProperties(const Cursor &cursor) const;
}; };
} }// namespace meta
#endif// META_CLANG_META_INFO_H #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