This commit is contained in:
tqcq 2024-03-03 00:20:01 +08:00
commit 0e1b34934d
10 changed files with 137 additions and 12 deletions

1
.gitignore vendored
View File

@ -2,3 +2,4 @@ out/
build/
.cache/
compile_commands.json
clangLog.txt

View File

@ -18,7 +18,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")
@ -31,5 +32,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

@ -9,6 +9,7 @@ extern "C" {
}
#endif
#include <string>
#include <vector>
namespace meta {

View File

@ -30,7 +30,7 @@ private:
"-MG",
"-M",
"-ferror-limit=0",
"-o clangLog.txt"}};
"-oclangLog.txt"}};
};
}// namespace meta

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

@ -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