summaryrefslogtreecommitdiff
path: root/source/slang/slang-serialize-misc-type-info.h
blob: 19151478532f58738a8f136692ee59ed48386e6f (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
185
186
187
188
189
190
191
192
193
194
195
// slang-serialize-misc-type-info.h
#ifndef SLANG_SERIALIZE_MISC_TYPE_INFO_H
#define SLANG_SERIALIZE_MISC_TYPE_INFO_H

#include "slang-serialize-type-info.h"

#include "../compiler-core/slang-source-loc.h"
#include "slang-compiler.h"

namespace Slang {

/* Conversion for serialization for some more misc Slang types
*/


// Because is sized, we don't need to convert
template <>
struct SerialTypeInfo<FeedbackType::Kind> : public SerialIdentityTypeInfo<FeedbackType::Kind> {};

// SamplerStateFlavor

template <>
struct SerialTypeInfo<SamplerStateFlavor> : public SerialConvertTypeInfo<SamplerStateFlavor, uint8_t> {};

// TextureFlavor

template <>
struct SerialTypeInfo<TextureFlavor>
{
    typedef TextureFlavor NativeType;
    typedef uint16_t SerialType;
    enum { SerialAlignment = sizeof(SerialType) };

    static void toSerial(SerialWriter* writer, const void* native, void* serial) { SLANG_UNUSED(writer); *(SerialType*)serial = ((const NativeType*)native)->flavor; }
    static void toNative(SerialReader* reader, const void* serial, void* native) { SLANG_UNUSED(reader); ((NativeType*)native)->flavor = *(const SerialType*)serial; }
};

// ImageFormat
template <>
struct SerialTypeInfo<ImageFormat> : public SerialConvertTypeInfo<ImageFormat, uint8_t> {};

// Stage
template <>
struct SerialTypeInfo<Stage> : public SerialConvertTypeInfo<Stage, uint8_t> {};

// TokenType
template <>
struct SerialTypeInfo<TokenType> : public SerialConvertTypeInfo<TokenType, uint8_t> {};

// BaseType
template <>
struct SerialTypeInfo<BaseType> : public SerialConvertTypeInfo<BaseType, uint8_t> {};

// SemanticVersion
template <>
struct SerialTypeInfo<SemanticVersion> : public SerialIdentityTypeInfo<SemanticVersion> {};

// SourceLoc

// Make the type exposed, so we can look for it if we want to remap.
template <>
struct SerialTypeInfo<SourceLoc>
{
    typedef SourceLoc NativeType;
    typedef SerialSourceLoc SerialType;
    enum { SerialAlignment = SLANG_ALIGN_OF(SerialSourceLoc) };

    static void toSerial(SerialWriter* writer, const void* inNative, void* outSerial)
    {
        SerialSourceLocWriter* sourceLocWriter = writer->getExtraObjects().get<SerialSourceLocWriter>();
        *(SerialType*)outSerial = sourceLocWriter ? sourceLocWriter->addSourceLoc(*(const NativeType*)inNative) : SerialType(0);
    }
    static void toNative(SerialReader* reader, const void* inSerial, void* outNative)
    {
        SerialSourceLocReader* sourceLocReader = reader->getExtraObjects().get<SerialSourceLocReader>();
        *(NativeType*)outNative = sourceLocReader ? sourceLocReader->getSourceLoc(*(const SerialType*)inSerial) : NativeType::fromRaw(0);
    }
};

// Token
template <>
struct SerialTypeInfo<Token>
{
    typedef Token NativeType;
    struct SerialType
    {
        SerialTypeInfo<BaseType>::SerialType type;
        SerialTypeInfo<SourceLoc>::SerialType loc;
        SerialIndex name;
    };
    enum { SerialAlignment = SLANG_ALIGN_OF(SerialType) };

    static void toSerial(SerialWriter* writer, const void* native, void* serial)
    {
        auto& src = *(const NativeType*)native;
        auto& dst = *(SerialType*)serial;

        SerialTypeInfo<TokenType>::toSerial(writer, &src.type, &dst.type);
        SerialTypeInfo<SourceLoc>::toSerial(writer, &src.loc, &dst.loc);

        if (src.flags & TokenFlag::Name)
        {
            dst.name = writer->addName(src.getName());
        }
        else
        {
            dst.name = writer->addString(src.getContent());
        }
    }
    static void toNative(SerialReader* reader, const void* serial, void* native)
    {
        auto& src = *(const SerialType*)serial;
        auto& dst = *(NativeType*)native;

        dst.flags = 0;
        dst.charsNameUnion.chars = nullptr;

        SerialTypeInfo<TokenType>::toNative(reader, &src.type, &dst.type);
        SerialTypeInfo<SourceLoc>::toNative(reader, &src.loc, &dst.loc);

        // At the other end all token content will appear as Names.
        if (src.name != SerialIndex(0))
        {
            dst.charsNameUnion.name = reader->getName(src.name);
            dst.flags |= TokenFlag::Name;
        }
    }
};

// NameLoc
template <>
struct SerialTypeInfo<NameLoc>
{
    typedef NameLoc NativeType;
    struct SerialType
    {
        SerialTypeInfo<SourceLoc>::SerialType loc;
        SerialIndex name;
    };
    enum { SerialAlignment = SLANG_ALIGN_OF(SerialType) };

    static void toSerial(SerialWriter* writer, const void* native, void* serial)
    {
        auto& src = *(const NativeType*)native;
        auto& dst = *(SerialType*)serial;

        dst.name = writer->addName(src.name);
        SerialTypeInfo<SourceLoc>::toSerial(writer, &src.loc, &dst.loc);
    }
    static void toNative(SerialReader* reader, const void* serial, void* native)
    {
        auto& src = *(const SerialType*)serial;
        auto& dst = *(NativeType*)native;

        dst.name = reader->getName(src.name);
        SerialTypeInfo<SourceLoc>::toNative(reader, &src.loc, &dst.loc);
    }
};

// DiagnosticInfo
template <>
struct SerialTypeInfo<const DiagnosticInfo*>
{
    typedef const DiagnosticInfo* NativeType;
    typedef SerialIndex SerialType;

    enum { SerialAlignment = SLANG_ALIGN_OF(SerialType) };

    static void toSerial(SerialWriter* writer, const void* native, void* serial)
    {
        auto& src = *(const NativeType*)native;
        auto& dst = *(SerialType*)serial;
        dst = src ? writer->addString(UnownedStringSlice(src->name)) : SerialIndex(0);
    }
    static void toNative(SerialReader* reader, const void* serial, void* native)
    {
        auto& src = *(const SerialType*)serial;
        auto& dst = *(NativeType*)native;

        if (src == SerialIndex(0))
        {
            dst = nullptr;
        }
        else
        {
            dst = findDiagnosticByName(reader->getStringSlice(src));
        }
    }
};



} // namespace Slang

#endif