FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
daScript/include/daScript/ast/ast_serializer.h at master · PushoN/daScript · GitHub
PushoN
/
daScript
Public
forked from
GaijinEntertainment/daScript
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
daScript
/
include
/
daScript
/
ast
/
ast_serializer.h
Copy path
More file actions
More file actions
Latest commit
History
History
History
351 lines (317 loc) · 15.7 KB
Breadcrumbs
daScript
/
include
/
daScript
/
ast
/
ast_serializer.h
Copy path
File metadata and controls
351 lines (317 loc) · 15.7 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
#
pragma
once
#
include
"
daScript/ast/ast_expressions.h
"
#
include
"
daScript/ast/ast_handle.h
"
#
include
"
daScript/ast/ast.h
"
#
include
"
daScript/misc/hash.h
"
#
include
<
optional
>
#
include
<
utility
>
//
enable this one if serialization breaks on tag, to get more info
#
ifndef
DAS_SERIALIZE_DTAG
#
define
DAS_SERIALIZE_DTAG
0
#
endif
namespace
das
{
struct
SerializationStorage
{
vector<
uint8_t
> buffer;
size_t
bufferPos =
0
;
template
<
typename
T>
__forceinline
bool
read
( T & data ) {
if
( bufferPos +
sizeof
(T) < buffer.
size
() ) {
data = *(T*)(buffer.
data
() + bufferPos);
bufferPos +=
sizeof
(T);
return
true
;
}
return
readOverflow
(&data,
sizeof
(T));
}
__forceinline
bool
read
( vec4f & data ) {
if
( bufferPos +
sizeof
(vec4f) < buffer.
size
() ) {
data =
v_ldu
((
const
float
*)(buffer.
data
() + bufferPos));
bufferPos +=
sizeof
(vec4f);
return
true
;
}
return
readOverflow
(&data,
sizeof
(vec4f));
}
__forceinline
bool
read
(
void
* data,
size_t
size ) {
if
( bufferPos + size < buffer.
size
() ) {
memcpy
(data, buffer.
data
() + bufferPos, size);
bufferPos += size;
return
true
;
}
return
readOverflow
(data, size);
}
virtual
bool
readOverflow
(
void
* data,
size_t
size ) = 0;
virtual
void
write
(
const
void
* data,
size_t
size ) = 0;
virtual
size_t
writingSize
()
const
= 0;
virtual
~SerializationStorage
() {}
};
struct
SerializationStorageVector
: SerializationStorage {
virtual
size_t
writingSize
()
const
override
{
return
buffer.
size
();
}
virtual
bool
readOverflow
(
void
* data,
size_t
size )
override
{
if
( bufferPos + size > buffer.
size
() )
return
false
;
memcpy
(data, buffer.
data
() + bufferPos, size);
bufferPos += size;
return
true
;
}
virtual
void
write
(
const
void
* data,
size_t
size )
override
{
auto
at = buffer.
size
();
buffer.
resize
(at + size);
memcpy
(buffer.
data
() + at, data, size);
}
};
//
Composite key for serializer maps. Pairs a raw pointer with a per-
//
AstSerializer epoch so reused addresses don't collide with prior entries.
//
Hoisted to namespace das so daslang_hash can be specialized before the
//
map instantiations inside AstSerializer.
struct
SerializeNodeId
{
void
* ptr =
nullptr
;
size_t
epoch =
0
;
bool
operator
== (
const
SerializeNodeId & o )
const
noexcept
{
return
ptr == o.
ptr
&& epoch == o.
epoch
;
}
bool
operator
!= (
const
SerializeNodeId & o )
const
noexcept
{
return
!(*
this
== o);
}
};
template
<>
struct
daslang_hash
<SerializeNodeId,
void
> {
size_t
operator
() (
const
SerializeNodeId & s )
const
noexcept
{
const
size_t
pmix = (
reinterpret_cast
<
size_t
>(s.
ptr
) >>
4
)
*
size_t
(
0x9E3779B97F4A7C15ull
);
return
pmix ^ (s.
epoch
*
size_t
(
0xBF58476D1CE4E5B9ull
));
}
};
struct
DAS_API
AstSerializer {
~AstSerializer
();
AstSerializer
( SerializationStorage * storage,
bool
isWriting );
AstSerializer
(
const
AstSerializer & from ) =
delete
;
AstSerializer
( AstSerializer && from ) =
default
;
AstSerializer &
operator
= (
const
AstSerializer & from ) =
delete
;
AstSerializer &
operator
= ( AstSerializer && from ) =
default
;
ModuleLibrary * moduleLibrary =
nullptr
;
//
some passes require module group (it's passed from top-level)
ModuleGroup * thisModuleGroup =
nullptr
;
Module * thisModule =
nullptr
;
Module * astModule =
nullptr
;
bool
writing =
false
;
bool
failed =
false
;
size_t
readOffset =
0
;
SerializationStorage * buffer =
nullptr
;
bool
seenNewModule =
false
;
//
expression lookup
das_hash_map<
uint32_t
, Annotation *> rttiHash2Annotation;
//
file info clean up
vector<FileInfo*> deleteUponFinish;
//
these pointers are for builtins (which we don't serialize) and need to be cleaned manually
das_hash_set<FileInfo*> doNotDelete;
//
profile data
uint64_t
totMacroTime =
0
;
//
Per-program epoch for SerializeNodeId. Bumped at the start of each
//
serialize/deserialize call so reused pointer addresses across program
//
boundaries don't collide with prior map entries on this persistent
//
serializer. Must be initialized — uninitialized epoch produces garbage
//
keys and silent map collisions.
size_t
epoch =
0
;
//
pointers
das_hash_map<SerializeNodeId, ExprBlock*> exprBlockMap;
using
DataOffset =
uint64_t
;
das_hash_map<FileInfo*, DataOffset> writingFileInfoMap;
das_hash_map<DataOffset, FileInfo*> readingFileInfoMap;
das_hash_map<SerializeNodeId, FileAccess*> fileAccessMap;
//
smart pointers
das_hash_map<SerializeNodeId, MakeFieldDeclPtr> smartMakeFieldDeclMap;
das_hash_map<SerializeNodeId, EnumerationPtr> smartEnumerationMap;
das_hash_map<SerializeNodeId, StructurePtr> smartStructureMap;
das_hash_map<SerializeNodeId, VariablePtr> smartVariableMap;
das_hash_map<SerializeNodeId, FunctionPtr> smartFunctionMap;
das_hash_map<SerializeNodeId, MakeStructPtr> smartMakeStructMap;
das_hash_map<SerializeNodeId, TypeDeclPtr> smartTypeDeclMap;
//
refs
vector<pair<ExprBlock**,SerializeNodeId>> blockRefs;
vector<pair<Function **,SerializeNodeId>> functionRefs;
vector<pair<Variable **,SerializeNodeId>> variableRefs;
vector<pair<Structure **,SerializeNodeId>> structureRefs;
vector<pair<Enumeration **,SerializeNodeId>> enumerationRefs;
//
fieldRefs tuple contains: fieldptr, module, structname, fieldname
vector<tuple<Structure::FieldDeclarationRef*, Module *, string, string>> fieldRefs;
//
parseModule tuple contains: moduleName, mtime, thisModule, thisModule
vector<tuple<string,
uint64_t
, ProgramPtr, Module*>> parsedModules;
//
tracking for shared modules
das_hash_set<Module *> writingReadyModules;
bool
ignoreEmptyExternal =
false
;
void
tag
(
const
char
* name,
uint32_t
hash );
#
if
DAS_SERIALIZE_DTAG
__forceinline
void
dtag
(
const
char
* name,
uint32_t
hash ) {
tag
(name,hash); }
#
else
__forceinline
void
dtag
(
const
char
*,
uint32_t
) {}
#
endif
template
<
typename
T>
void
read
( T & data ) { buffer->
read
(data); }
void
read
(
void
* data,
size_t
size );
void
write
(
const
void
* data,
size_t
size );
template
<
typename
T>
void
serialize
( T & data ) {
if
( writing ) {
write
(&data,
sizeof
(data));
}
else
{
read
(data);
}
}
void
serialize
(
void
* data,
size_t
size );
void
serializeAdaptiveSize64
(
uint64_t
& size );
void
serializeAdaptiveSize32
(
uint32_t
& size );
void
collectFileInfo
( vector<FileInfoPtr> & orphanedFileInfos );
void
getCompiledModules
( );
void
patch
();
AstSerializer &
operator
<< ( string & str );
AstSerializer &
operator
<< (
const
char
* & value );
AstSerializer &
operator
<< (
bool
& value ) {
serialize
(value);
return
*
this
; }
AstSerializer &
operator
<< ( vec4f & value ) {
serialize
(value);
return
*
this
; }
AstSerializer &
operator
<< (
float
& value ) {
serialize
(value);
return
*
this
; }
AstSerializer &
operator
<< (
void
* & value ) {
serialize
(value);
return
*
this
; }
AstSerializer &
operator
<< (
uint8_t
& value ) {
serialize
(value);
return
*
this
; }
AstSerializer &
operator
<< (
int32_t
& value ) {
serialize
(value);
return
*
this
; }
AstSerializer &
operator
<< (
int64_t
& value ) {
serialize
(value);
return
*
this
; }
AstSerializer &
operator
<< (
uint16_t
& value ) {
serialize
(value);
return
*
this
; }
AstSerializer &
operator
<< (
uint32_t
& value ) {
serialize
(value);
return
*
this
; }
AstSerializer &
operator
<< (
uint64_t
& value ) {
serialize
(value);
return
*
this
; }
AstSerializer &
operator
<< ( pair<
uint32_t
,
uint32_t
> & value ) {
serialize
(value);
return
*
this
; }
AstSerializer &
operator
<< ( pair<
uint64_t
,
uint64_t
> & value ) {
serialize
(value);
return
*
this
; }
AstSerializer &
operator
<< ( pair<string,
bool
> & value ) { *
this
<< value.
first
<< value.
second
;
return
*
this
; }
AstSerializer &
operator
<< ( tuple<Module *, string, string,
bool
, LineInfo> & value );
AstSerializer &
operator
<< ( CodeOfPolicies & ser );
AstSerializer &
operator
<< ( TypeDeclPtr & type );
AstSerializer &
operator
<< ( AnnotationArgument & arg );
AstSerializer &
operator
<< ( AnnotationDeclarationPtr & annotation_decl );
AstSerializer &
operator
<< ( AnnotationPtr & anno );
AstSerializer &
operator
<< ( Structure::FieldDeclaration & field_declaration );
AstSerializer &
operator
<< ( ExpressionPtr & expr );
AstSerializer &
operator
<< ( FunctionPtr & func );
AstSerializer &
operator
<< ( Type & baseType );
AstSerializer &
operator
<< ( LineInfo & at );
AstSerializer &
operator
<< ( Module * &
module
);
AstSerializer &
operator
<< ( FileInfo * & info );
AstSerializer &
operator
<< ( FileInfoPtr & ptr );
AstSerializer &
operator
<< ( FileAccessPtr & ptr );
AstSerializer &
operator
<< ( StructurePtr & struct_ );
AstSerializer &
operator
<< ( EnumerationPtr & enum_type );
AstSerializer &
operator
<< ( Enumeration::EnumEntry & entry );
AstSerializer &
operator
<< ( TypeAnnotationPtr & type_anno );
AstSerializer &
operator
<< ( VariablePtr & var );
AstSerializer &
operator
<< ( Function::AliasInfo & alias_info );
AstSerializer &
operator
<< ( InferHistory & history );
AstSerializer &
operator
<< ( ReaderMacroPtr & ptr );
AstSerializer &
operator
<< ( TypeInfoMacro * & ptr );
AstSerializer &
operator
<< ( ExprBlock * & block );
AstSerializer &
operator
<< ( CallMacro * & ptr );
AstSerializer &
operator
<< ( CaptureEntry & entry );
AstSerializer &
operator
<< ( MakeFieldDeclPtr & ptr );
AstSerializer &
operator
<< ( MakeStructPtr & ptr );
AstSerializer &
operator
<< ( SerializeNodeId & value );
//
Top-level
AstSerializer &
operator
<< ( Module &
module
);
AstSerializer &
serializeModule
( Module &
module
,
bool
already_exists );
uint32_t
getVersion
();
void
serializeProgram
( ProgramPtr program, ModuleGroup & libGroup )
noexcept
;
bool
serializeScript
( ProgramPtr program )
noexcept
;
template
<
uint64_t
n>
AstSerializer&
operator
<< (
int
(&value)[n] ) {
serialize
(value, n *
sizeof
(
int
));
return
*
this
;
}
___noinline
bool
trySerialize
(
const
callable<
void
(AstSerializer&)> &cb )
noexcept
;
template
<
typename
TT
>
AstSerializer &
operator
<< ( vector<
TT
> & value ) {
dtag
(
"
Vector
"
,
hash_tag
(
"
Vector
"
));
if
( writing ) {
uint64_t
size = value.
size
();
serializeAdaptiveSize64
(size);
}
else
{
uint64_t
size =
0
;
serializeAdaptiveSize64
(size);
value.
resize
(size);
}
for
(
TT
& v : value ) {
*
this
<< v;
}
return
*
this
;
}
template
<
typename
K,
typename
V,
typename
H,
typename
E>
void
serialize_hash_map
( das_hash_map<K, V, H, E> & value );
template
<
typename
K,
typename
V,
typename
H,
typename
E>
AstSerializer &
operator
<< ( das_hash_map<K, V, H, E> & value );
template
<
typename
V>
AstSerializer &
operator
<< ( safebox_map<V> & box );
template
<
typename
V,
typename
VT
>
AstSerializer &
operator
<< ( safebox<V,
VT
> & box );
template
<
typename
TT
>
AstSerializer &
serializePointer
(
TT
* & ptr );
bool
isInThisModule
( Function * & ptr );
bool
isInThisModule
( Enumeration * & ptr );
bool
isInThisModule
( Structure * & ptr );
bool
isInThisModule
( Variable * & ptr );
bool
isInThisModule
( TypeInfoMacro * & ptr );
void
writeIdentifications
( Function * & func );
void
writeIdentifications
( Enumeration * & ptr );
void
writeIdentifications
( Structure * & ptr );
void
writeIdentifications
( Variable * & ptr );
void
writeIdentifications
( TypeInfoMacro * & ptr );
void
fillOrPatchLater
( Function * & func, SerializeNodeId id );
void
fillOrPatchLater
( Enumeration * & ptr, SerializeNodeId id );
void
fillOrPatchLater
( Structure * & ptr, SerializeNodeId id );
void
fillOrPatchLater
( Variable * & ptr, SerializeNodeId id );
auto
readModuleAndName
() -> pair<Module *, string>;
auto
readModuleAndNameHash
() -> pair<Module *, uint64_t>;
void
findExternal
( Function * & func );
void
findExternal
( Enumeration * & ptr );
void
findExternal
( Structure * & ptr );
void
findExternal
( Variable * & ptr );
void
findExternal
( TypeInfoMacro * & ptr );
SerializeNodeId
getSerializeId
(
void
*ptr) {
return
{ptr, epoch}; }
template
<
typename
EnumType>
void
serialize_small_enum
( EnumType & baseType ) {
if
( writing ) {
uint8_t
bt = (
uint8_t
) baseType;
*
this
<< bt;
}
else
{
uint8_t
bt =
0
;
*
this
<< bt;
baseType = (EnumType) bt;
}
}
template
<
typename
EnumType>
void
serialize_enum
( EnumType & baseType ) {
if
( writing ) {
uint32_t
bt = (
uint32_t
) baseType;
*
this
<< bt;
}
else
{
uint32_t
bt =
0
;
*
this
<< bt;
baseType = (EnumType) bt;
}
}
};
//
Opaque handle to expose serializer to daslang.
struct
AstSerializerState
{
unique_ptr<SerializationStorageVector> storage;
unique_ptr<AstSerializer> serializer;
};
//
Create a writing serializer.
AstSerializerState *
rtti_create_ast_serializer
();
//
Create a reading serializer from a blob.
AstSerializerState *
rtti_create_ast_deserializer
(
const
TArray<
uint8_t
> & data );
//
Delete a serializer state.
void
rtti_delete_ast_serializer
( AstSerializerState * state );
//
Serialize one program (writing mode). Returns false on failure.
bool
rtti_ast_serializer_serialize_program
(
AstSerializerState * state,
const
smart_ptr<Program> & program );
//
Deserialize one program (reading mode).
void
rtti_ast_serializer_deserialize_program
(
AstSerializerState * state,
const
TBlock<
void
,
bool
,smart_ptr<Program>,
const
string> & block,
Context * context, LineInfoArg * at );
//
Get serialized data from a writing serializer.
void
rtti_ast_serializer_get_data
(
AstSerializerState * state,
const
TBlock<
void
,TTemporary<TArray<
uint8_t
>
const
>> & block,
Context * context, LineInfoArg * at );
}
Back
|
FazBrowse Home
|
New Git URL