summaryrefslogtreecommitdiffstats
path: root/source/slang/slang-serialize-ast.cpp
blob: a7837edea08f1d6de74664a7d2053a1999bd5f52 (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
196
197
198
199
200
201
202
203
204
205
206
207
208
// slang-serialize-ast.cpp
#include "slang-serialize-ast.h"

#include "slang-ast-dump.h"
#include "slang-ast-support-types.h"
#include "slang-generated-ast-macro.h"
#include "slang-generated-ast.h"
#include "slang-serialize-ast-type-info.h"
#include "slang-serialize-factory.h"

namespace Slang
{

// !!!!!!!!!!!!!!!!!!!!!! Generate fields for a type !!!!!!!!!!!!!!!!!!!!!!!!!!!

static const SerialClass* _addClass(
    SerialClasses* serialClasses,
    ASTNodeType type,
    ASTNodeType super,
    const List<SerialField>& fields)
{
    const SerialClass* superClass =
        serialClasses->getSerialClass(SerialTypeKind::NodeBase, SerialSubType(super));
    return serialClasses->add(
        SerialTypeKind::NodeBase,
        SerialSubType(type),
        fields.getBuffer(),
        fields.getCount(),
        superClass);
}

#define SLANG_AST_ADD_SERIAL_FIELD(FIELD_NAME, TYPE, param) \
    fields.add(SerialField::make(#FIELD_NAME, &obj->FIELD_NAME));

// Note that the obj point is not nullptr, because some compilers notice this is 'indexing from
// null' and warn/error. So we offset from 1.
#define SLANG_AST_ADD_SERIAL_CLASS(NAME, SUPER, ORIGIN, LAST, MARKER, TYPE, param)   \
    {                                                                                \
        NAME* obj = SerialField::getPtr<NAME>();                                     \
        SLANG_UNUSED(obj);                                                           \
        fields.clear();                                                              \
        SLANG_FIELDS_ASTNode_##NAME(SLANG_AST_ADD_SERIAL_FIELD, param)               \
            _addClass(serialClasses, ASTNodeType::NAME, ASTNodeType::SUPER, fields); \
    }

struct ASTFieldAccess
{
    static void calcClasses(SerialClasses* serialClasses)
    {
        // Add NodeBase first, and specially handle so that we add a null super class
        serialClasses->add(
            SerialTypeKind::NodeBase,
            SerialSubType(ASTNodeType::NodeBase),
            nullptr,
            0,
            nullptr);

        // Add the rest in order such that Super class is always added before its children
        List<SerialField> fields;
        SLANG_CHILDREN_ASTNode_NodeBase(SLANG_AST_ADD_SERIAL_CLASS, _)
    }
};

// !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! ASTSerialUtil  !!!!!!!!!!!!!!!!!!!!!!!!!!!!

/* static */ void ASTSerialUtil::addSerialClasses(SerialClasses* serialClasses)
{
    ASTFieldAccess::calcClasses(serialClasses);
}

/* static */ SlangResult ASTSerialUtil::testSerialize(
    NodeBase* node,
    RootNamePool* rootNamePool,
    SharedASTBuilder* sharedASTBuilder,
    SourceManager* sourceManager)
{
    RefPtr<SerialClasses> classes;

    SerialClassesUtil::create(classes);

    List<uint8_t> contents;

    {
        OwnedMemoryStream stream(FileAccess::ReadWrite);

        ModuleDecl* moduleDecl = as<ModuleDecl>(node);
        // Only serialize out things *in* this module
        ModuleSerialFilter filterStorage(moduleDecl);

        SerialFilter* filter = moduleDecl ? &filterStorage : nullptr;

        SerialWriter writer(classes, filter);

        // Lets serialize it all
        writer.addPointer(node);
        // Let's stick it all in a stream
        writer.write(&stream);

        stream.swapContents(contents);

        NamePool namePool;
        namePool.setRootNamePool(rootNamePool);

        ASTBuilder builder(sharedASTBuilder, "Serialize Check");

        SetASTBuilderContextRAII astBuilderRAII(&builder);

        DefaultSerialObjectFactory objectFactory(&builder);

        // We could now check that the loaded data matches

        {
            const List<SerialInfo::Entry*>& writtenEntries = writer.getEntries();
            List<const SerialInfo::Entry*> readEntries;

            SlangResult res = SerialReader::loadEntries(
                contents.getBuffer(),
                contents.getCount(),
                classes,
                readEntries);
            SLANG_UNUSED(res);

            SLANG_ASSERT(writtenEntries.getCount() == readEntries.getCount());

            // They should be identical up to the
            for (Index i = 1; i < readEntries.getCount(); ++i)
            {
                auto writtenEntry = writtenEntries[i];
                auto readEntry = readEntries[i];

                const size_t writtenSize = writtenEntry->calcSize(classes);
                const size_t readSize = readEntry->calcSize(classes);
                SLANG_UNUSED(writtenSize);
                SLANG_UNUSED(readSize);

                SLANG_ASSERT(readSize == writtenSize);
                // Check the payload is the same
                SLANG_ASSERT(memcmp(readEntry, writtenEntry, readSize) == 0);
            }
        }

        SerialReader reader(classes, nullptr);
        {

            SlangResult res = reader.load(contents.getBuffer(), contents.getCount(), &namePool);
            SLANG_UNUSED(res);
        }

        // Lets see what we have
        const ASTDumpUtil::Flags dumpFlags =
            ASTDumpUtil::Flag::HideSourceLoc | ASTDumpUtil::Flag::HideScope;

        String readDump;
        {
            SourceWriter sourceWriter(sourceManager, LineDirectiveMode::None, nullptr);
            ASTDumpUtil::dump(
                reader.getPointer(SerialIndex(1)).dynamicCast<NodeBase>(),
                ASTDumpUtil::Style::Hierachical,
                dumpFlags,
                &sourceWriter);
            readDump = sourceWriter.getContentAndClear();
        }
        String origDump;
        {
            SourceWriter sourceWriter(sourceManager, LineDirectiveMode::None, nullptr);
            ASTDumpUtil::dump(node, ASTDumpUtil::Style::Hierachical, dumpFlags, &sourceWriter);
            origDump = sourceWriter.getContentAndClear();
        }

        // Write out
        File::writeAllText("ast-read.ast-dump", readDump);
        File::writeAllText("ast-orig.ast-dump", origDump);

        if (readDump != origDump)
        {
            return SLANG_FAIL;
        }
    }

    return SLANG_OK;
}

/* static */ List<uint8_t> ASTSerialUtil::serializeAST(ModuleDecl* moduleDecl)
{
    // TODO: we should store `classes` in GlobalSession to avoid recomputing them every time.
    RefPtr<SerialClasses> classes;
    SerialClassesUtil::create(classes);

    List<uint8_t> contents;
    OwnedMemoryStream stream(FileAccess::ReadWrite);

    // Only serialize out things *in* this module
    ModuleSerialFilter filterStorage(moduleDecl);

    SerialFilter* filter = moduleDecl ? &filterStorage : nullptr;

    SerialWriter writer(classes, filter);

    // Lets serialize it all
    writer.addPointer(moduleDecl);
    // Let's stick it all in a stream
    writer.write(&stream);

    stream.swapContents(contents);
    return contents;
}

} // namespace Slang