summaryrefslogtreecommitdiffstats
path: root/source/core/slang-rtti-info.cpp
blob: f53cf742fa947932c31679ec40dc4e0b13045701 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include "slang-rtti-info.h"

#include "../../slang-com-helper.h"

#include <mutex>

namespace Slang {

#define SLANG_RTTI_INFO_INVALID(name) RttiInfo{RttiInfo::Kind::Invalid, 0, 0}
#define SLANG_RTTI_INFO_BASIC(name, type) \
    RttiInfo{RttiInfo::Kind::name, RttiInfo::AlignmentType(SLANG_ALIGN_OF(type)), RttiInfo::SizeType(sizeof(type))}

/* static */const RttiInfo RttiInfo::g_basicTypes[Index(Kind::CountOf)] =
{
    SLANG_RTTI_INFO_INVALID(Invalid),
    SLANG_RTTI_INFO_BASIC(I32, int32_t),
    SLANG_RTTI_INFO_BASIC(U32, uint32_t),
    SLANG_RTTI_INFO_BASIC(I64, int64_t),
    SLANG_RTTI_INFO_BASIC(U64, uint64_t),
    SLANG_RTTI_INFO_BASIC(F32, float),
    SLANG_RTTI_INFO_BASIC(F64, double),
    SLANG_RTTI_INFO_BASIC(Bool, bool),
    SLANG_RTTI_INFO_BASIC(String, String),
    SLANG_RTTI_INFO_BASIC(UnownedStringSlice, UnownedStringSlice),
    SLANG_RTTI_INFO_BASIC(Ptr, void*),
    SLANG_RTTI_INFO_BASIC(RefPtr, RefPtr<StringRepresentation>),
    SLANG_RTTI_INFO_INVALID(FixedArray),
    SLANG_RTTI_INFO_INVALID(Struct),
    SLANG_RTTI_INFO_INVALID(Other),
    SLANG_RTTI_INFO_INVALID(Enum),
    SLANG_RTTI_INFO_INVALID(List),
    SLANG_RTTI_INFO_INVALID(Dictionary),
};

struct RttiInfoManager
{
    void* allocate(size_t size)
    {
        std::lock_guard<std::recursive_mutex> guard(m_mutex);
        return m_arena.allocate(size);
    }

    static RttiInfoManager& getSingleton()
    {
        static RttiInfoManager g_manager;
        return g_manager;
    }

protected:
    RttiInfoManager() :
        m_arena(1024)
    {
    }

    std::recursive_mutex m_mutex;             ///< We need a mutex to guard access to m_arena
    MemoryArena m_arena;
};

/* static */void* RttiInfo::allocate(size_t size)
{
    return RttiInfoManager::getSingleton().allocate(size);
}

/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! StructRttiBuilder !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */

static void _appendFixedArray(const FixedArrayRttiInfo* inFixedArray, StringBuilder& out)
{
    List<const FixedArrayRttiInfo*> fixedArrays;
    fixedArrays.add(inFixedArray);

    const RttiInfo* cur = inFixedArray->m_elementType;
    while (cur->m_kind == RttiInfo::Kind::FixedArray)
    {
        const FixedArrayRttiInfo* curArray = static_cast<const FixedArrayRttiInfo*>(cur);
        fixedArrays.add(curArray);
        cur = curArray->m_elementType;
    }

    // Append the 'target' which is in cur
    RttiInfo::append(cur, out);
    // Now all the fixed array values, in order
    for (auto fixedArray : fixedArrays)
    {
        out << "[" << int32_t(fixedArray->m_elementCount) << "]";
    }
}

/* static */void RttiInfo::append(const RttiInfo* info, StringBuilder& out)
{
    switch (info->m_kind)
    {
        case RttiInfo::Kind::I32:       out << "int32_t"; break;
        case RttiInfo::Kind::U32:       out << "uint32_t"; break;
        case RttiInfo::Kind::I64:       out << "int64_t"; break;
        case RttiInfo::Kind::U64:       out << "uint64_t"; break;
        case RttiInfo::Kind::F32:       out << "float"; break;
        case RttiInfo::Kind::F64:       out << "double"; break;
        case RttiInfo::Kind::Bool:      out << "bool"; break;
        case RttiInfo::Kind::String:    out << "String"; break;
        case RttiInfo::Kind::UnownedStringSlice:    out << "UnownedStringSlice"; break;
        case RttiInfo::Kind::Ptr:
        {
            const PtrRttiInfo* ptrRttiInfo = static_cast<const PtrRttiInfo*>(info);
            append(ptrRttiInfo->m_targetType, out);
            out << "*";
            break;
        }
        case RttiInfo::Kind::RefPtr:
        {
            const RefPtrRttiInfo* ptrRttiInfo = static_cast<const RefPtrRttiInfo*>(info);
            out << "RefPtr<";
            append(ptrRttiInfo->m_targetType, out);
            out << ">";
            break;
        }
        case RttiInfo::Kind::FixedArray:
        {
            const FixedArrayRttiInfo* arrayRttiInfo = static_cast<const FixedArrayRttiInfo*>(info);
            _appendFixedArray(arrayRttiInfo, out);
            break;
        }
        case RttiInfo::Kind::List:
        {
            const ListRttiInfo* listRttiInfo = static_cast<const ListRttiInfo*>(info);
            out << "List<";
            append(listRttiInfo->m_elementType, out);
            out << ">";
            break;
        }
        case RttiInfo::Kind::Dictionary:
        {
            const DictionaryRttiInfo* dictionaryRttiInfo = static_cast<const DictionaryRttiInfo*>(info);

            out << "Dictionary<";
            append(dictionaryRttiInfo->m_keyType, out);
            out << ",";
            append(dictionaryRttiInfo->m_valueType, out);
            out << ">";
            break;
        }
        default:
        {
            if (info->isNamed())
            {
                const NamedRttiInfo* namedRttiInfo = static_cast<const NamedRttiInfo*>(info);
                out << namedRttiInfo->m_name;
                break;
            }

            out << "%Unknown%";
            break;
        }
    }
}

/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! StructRttiBuilder !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */

void StructRttiBuilder::_init(const char* name, const StructRttiInfo* super, const Byte* base)
{
    m_rttiInfo.m_name = name;
    m_rttiInfo.m_super = super;
    m_base = base;

    m_rttiInfo.m_fieldCount = 0;
    m_rttiInfo.m_fields = nullptr;
}

StructRttiInfo StructRttiBuilder::make()
{
    const Index fieldCount = m_fields.getCount();

    if (fieldCount)
    {
        StructRttiInfo::Field* dstFields = (StructRttiInfo::Field*)RttiInfo::allocate(sizeof(StructRttiInfo::Field) * fieldCount);
        ::memcpy(dstFields, m_fields.getBuffer(), sizeof(StructRttiInfo::Field) * fieldCount);

        m_rttiInfo.m_fields = dstFields;
        m_rttiInfo.m_fieldCount = fieldCount;
    }

    return m_rttiInfo;
}

} // namespace Slang