Skip to content
This repository has been archived by the owner on Jan 2, 2023. It is now read-only.

Commit

Permalink
refactor: make type-specific data (such as StructInfo) part of TypeInfo
Browse files Browse the repository at this point in the history
  • Loading branch information
Wodann committed Oct 29, 2020
1 parent 1b923f8 commit 73b9c98
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 54 deletions.
2 changes: 1 addition & 1 deletion include/mun/reflection.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ namespace reflection {
template <typename T>
std::optional<std::pair<const char*, const char*>> equals_return_type(
const MunTypeInfo& type_info) noexcept {
if (type_info.group == MunTypeGroup::FundamentalTypes) {
if (type_info.data.tag == MunTypeInfoData_Tag::Primitive) {
if (type_info.guid != ReturnTypeReflection<T>::type_guid()) {
return std::make_pair(type_info.name, ReturnTypeReflection<T>::type_name());
}
Expand Down
108 changes: 59 additions & 49 deletions include/mun/runtime_capi.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,27 +31,6 @@ enum MunStructMemoryKind
typedef uint8_t MunStructMemoryKind;
#endif // __cplusplus

/**
* Represents a group of types that illicit the same characteristics.
*/
enum MunTypeGroup
#ifdef __cplusplus
: uint8_t
#endif // __cplusplus
{
/**
* Fundamental types (i.e. `()`, `bool`, `float`, `int`, etc.)
*/
FundamentalTypes = 0,
/**
* Struct types (i.e. record, tuple, or unit structs)
*/
StructTypes = 1,
};
#ifndef __cplusplus
typedef uint8_t MunTypeGroup;
#endif // __cplusplus

typedef uintptr_t MunToken;

/**
Expand All @@ -75,13 +54,70 @@ typedef struct {
uint8_t _0[16];
} MunGuid;

/**
* Represents a struct declaration.
*/
typedef struct {
/**
* Struct fields' names
*/
const char *const *field_names;
/**
* Struct fields' information
*/
const struct MunTypeInfo *const *field_types;
/**
* Struct fields' offsets
*/
const uint16_t *field_offsets;
/**
* Number of fields
*/
uint16_t num_fields;
/**
* Struct memory kind
*/
MunStructMemoryKind memory_kind;
} MunStructInfo;

/**
* Contains data specific to a group of types that illicit the same characteristics.
*/
enum MunTypeInfoData_Tag
#ifdef __cplusplus
: uint8_t
#endif // __cplusplus
{
/**
* Primitive types (i.e. `()`, `bool`, `float`, `int`, etc.)
*/
Primitive,
/**
* Struct types (i.e. record, tuple, or unit structs)
*/
Struct,
};
#ifndef __cplusplus
typedef uint8_t MunTypeInfoData_Tag;
#endif // __cplusplus

typedef struct {
MunTypeInfoData_Tag tag;
MunStructInfo _0;
} MunStruct_Body;

typedef union {
MunTypeInfoData_Tag tag;
MunStruct_Body struct_;
} MunTypeInfoData;

/**
* Represents the type declaration for a value type.
*
* TODO: add support for polymorphism, enumerations, type parameters, generic type definitions, and
* constructed generic types.
*/
typedef struct {
typedef struct MunTypeInfo {
/**
* Type GUID
*/
Expand All @@ -101,7 +137,7 @@ typedef struct {
/**
* Type group
*/
MunTypeGroup group;
MunTypeInfoData data;
} MunTypeInfo;

/**
Expand Down Expand Up @@ -209,32 +245,6 @@ typedef struct {
uint32_t num_functions;
} MunRuntimeOptions;

/**
* Represents a struct declaration.
*/
typedef struct {
/**
* Struct fields' names
*/
const char *const *field_names;
/**
* Struct fields' information
*/
const MunTypeInfo *const *field_types;
/**
* Struct fields' offsets
*/
const uint16_t *field_offsets;
/**
* Number of fields
*/
uint16_t num_fields;
/**
* Struct memory kind
*/
MunStructMemoryKind memory_kind;
} MunStructInfo;

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
Expand Down
6 changes: 3 additions & 3 deletions include/mun/struct_ref.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ inline std::string format_struct_field(std::string_view struct_name,
} // namespace details

inline std::optional<MunStructInfo> type_info_as_struct(const MunTypeInfo& type_info) noexcept {
if (type_info.group != MunTypeGroup::StructTypes) {
if (type_info.data.tag != MunTypeInfoData_Tag::Struct) {
return std::nullopt;
}

return std::make_optional(*reinterpret_cast<const MunStructInfo*>(&type_info + 1));
return std::make_optional(type_info.data.struct_._0);
}

inline size_t type_info_size_in_bytes(const MunTypeInfo& type_info) noexcept {
Expand All @@ -66,7 +66,7 @@ class StructRef {
*/
StructRef(const Runtime& runtime, MunGcPtr raw) noexcept
: m_runtime(&runtime), m_handle(GcRootPtr(runtime, raw)) {
assert(runtime.ptr_type(raw)->group == MunTypeGroup::StructTypes);
assert(runtime.ptr_type(raw)->data.tag == MunTypeInfoData_Tag::Struct);
}

StructRef(const StructRef&) noexcept = default;
Expand Down
2 changes: 1 addition & 1 deletion include/mun/type_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ struct TypeInfo;
name_literal, \
sizeof(ty), \
std::alignment_of<ty>::value, \
MunTypeGroup::FundamentalTypes, \
MunTypeInfoData_Tag::Primitive \
}; \
}

Expand Down

0 comments on commit 73b9c98

Please sign in to comment.