-
Notifications
You must be signed in to change notification settings - Fork 2
/
type_rw_struct.go
447 lines (403 loc) · 14.7 KB
/
type_rw_struct.go
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
// Copyright 2022 V Kontakte LLC
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package tlcodegen
import (
"fmt"
"log"
"sort"
)
type TypeRWStruct struct {
wr *TypeRWWrapper
Fields []Field
ResultType *TypeRWWrapper
ResultNatArgs []ActualNatArg
ResultHalfResolved HalfResolvedArgument
fieldsDec Deconflicter // TODO - add all generated methods here
fieldsDecCPP Deconflicter // TODO - add all generated methods here
setNames []string // method names should be the same for bytes and normal versions, so we remember them here
clearNames []string
isSetNames []string
}
func (trw *TypeRWStruct) isTypeDef() bool {
return len(trw.Fields) == 1 && trw.Fields[0].originalName == "" && trw.Fields[0].fieldMask == nil && !trw.Fields[0].recursive
}
func (trw *TypeRWStruct) isUnwrapType() bool {
if !trw.isTypeDef() || trw.wr.preventUnwrap {
return false
}
// Motivation - we want default wrappers for primitive types, vector and tuple to generate primitive language types
primitive, isPrimitive := trw.Fields[0].t.trw.(*TypeRWPrimitive)
if isPrimitive && primitive.tlType == trw.wr.tlName.String() {
return true
}
brackets, isBuiltinBrackets := trw.Fields[0].t.trw.(*TypeRWBrackets)
if isBuiltinBrackets && (brackets.dictLike || trw.wr.tlName.String() == "vector" || trw.wr.tlName.String() == "tuple") {
return true
}
// in combined TL Dictionary is defined via Vector.
// dictionaryField {t:Type} key:string value:t = DictionaryField t;
// dictionary#1f4c618f {t:Type} %(Vector %(DictionaryField t)) = Dictionary t;
// TODO - change combined.tl to use # [] after we fully control generation of C++ & (k)PHP and remove code below
str, isStruct := trw.Fields[0].t.trw.(*TypeRWStruct)
if isStruct && str.wr.tlName.String() == "vector" {
// repeat check above 1 level deeper
brackets, isBuiltinBrackets = str.Fields[0].t.trw.(*TypeRWBrackets)
if isBuiltinBrackets && brackets.dictLike {
return true
}
}
return false
}
func (trw *TypeRWStruct) typeString2(bytesVersion bool, directImports *DirectImports, ins *InternalNamespace, isLocal bool, skipAlias bool) string {
if !skipAlias && trw.isUnwrapType() {
return trw.Fields[0].t.TypeString2(bytesVersion, directImports, ins, isLocal, skipAlias)
}
if isLocal {
return addBytes(trw.wr.goLocalName, bytesVersion)
}
return trw.wr.ins.Prefix(directImports, ins) + addBytes(trw.wr.goGlobalName, bytesVersion)
}
func (trw *TypeRWStruct) markHasBytesVersion(visitedNodes map[*TypeRWWrapper]bool) bool {
result := false
for _, f := range trw.Fields {
result = result || f.t.MarkHasBytesVersion(visitedNodes)
}
if trw.ResultType != nil {
result = result || trw.ResultType.MarkHasBytesVersion(visitedNodes)
}
return result
}
func (trw *TypeRWStruct) markWriteHasError(visitedNodes map[*TypeRWWrapper]bool) bool {
result := false
for _, f := range trw.Fields {
result = result || f.t.MarkWriteHasError(visitedNodes)
}
if trw.ResultType != nil {
result = result || trw.ResultType.MarkWriteHasError(visitedNodes)
}
return result
}
func (trw *TypeRWStruct) fillRecursiveUnwrap(visitedNodes map[*TypeRWWrapper]bool) {
if !trw.isTypeDef() {
return
}
trw.Fields[0].t.FillRecursiveUnwrap(visitedNodes)
}
func (trw *TypeRWStruct) markWantsBytesVersion(visitedNodes map[*TypeRWWrapper]bool) {
for _, f := range trw.Fields {
f.t.MarkWantsBytesVersion(visitedNodes)
}
if trw.ResultType != nil {
trw.ResultType.MarkWantsBytesVersion(visitedNodes)
}
}
func (trw *TypeRWStruct) BeforeCodeGenerationStep1() {
for i, f := range trw.Fields {
visitedNodes := map[*TypeRWWrapper]bool{}
f.t.trw.fillRecursiveChildren(visitedNodes)
trw.Fields[i].recursive = visitedNodes[trw.wr]
}
trw.setNames = make([]string, len(trw.Fields))
trw.clearNames = make([]string, len(trw.Fields))
trw.isSetNames = make([]string, len(trw.Fields))
}
func (trw *TypeRWStruct) GetAllLocallyAffectedByTrueTypeFieldMasks() []Field {
nats := make([]Field, 0)
containingNats := make(map[int]bool)
for _, field := range trw.Fields {
if field.IsAffectingLocalFieldMasks() && field.t.IsTrueType() {
index := field.fieldMask.FieldIndex
if _, contains := containingNats[index]; !contains {
nats = append(nats, trw.Fields[index])
containingNats[index] = true
}
}
}
return nats
}
func (trw *TypeRWStruct) GetAllLocallyAffectedFieldMasks() []Field {
nats := make([]Field, 0)
containingNats := make(map[int]bool)
for _, field := range trw.Fields {
if field.IsAffectingLocalFieldMasks() {
index := field.fieldMask.FieldIndex
if _, contains := containingNats[index]; !contains {
nats = append(nats, trw.Fields[index])
containingNats[index] = true
}
}
}
return nats
}
type FieldNatProperties = int
const (
FieldIsNotNat FieldNatProperties = 0
FieldIsNat FieldNatProperties = 1
FieldUsedAsFieldMask FieldNatProperties = 2
FieldUsedAsSize FieldNatProperties = 4
)
func (trw *TypeRWStruct) GetFieldNatProperties(fieldId int) (FieldNatProperties, []uint32) {
if fieldId < 0 || len(trw.Fields) <= fieldId {
return FieldIsNotNat, nil
}
targetField := trw.Fields[fieldId]
pr, isPr := targetField.t.trw.(*TypeRWPrimitive)
if !isPr || pr.tlType != "#" {
return FieldIsNotNat, nil
}
result := FieldIsNat
affectedIndexes := make(map[uint32]bool)
natParamUsageMap := make(map[VisitedTypeNatParam]VisitResult)
for i, f := range trw.Fields {
if i == fieldId {
continue
}
if f.fieldMask != nil &&
f.fieldMask.isField &&
f.fieldMask.FieldIndex == fieldId {
affectedIndexes[f.BitNumber] = true
result |= FieldUsedAsFieldMask
}
natIndexes := make([]int, 0)
for j, natArg := range f.natArgs {
if natArg.isField && natArg.FieldIndex == fieldId {
natIndexes = append(natIndexes, j)
}
}
for _, j := range natIndexes {
visit(f.t, j, &natParamUsageMap, &affectedIndexes, &result)
}
}
indexes := make([]uint32, 0)
for i := range affectedIndexes {
indexes = append(indexes, i)
}
// not necessary
sort.Slice(indexes, func(i, j int) bool {
return indexes[i] < indexes[j]
})
return result, indexes
}
type VisitedTypeNatParam struct {
Type_ string
NatIndex int
}
type VisitResult = int
const (
VisitSuccess VisitResult = iota
VisitFail
VisitInProgress
)
func visit(
t *TypeRWWrapper,
natIndex int,
visitResults *map[VisitedTypeNatParam]VisitResult,
affectedIndexes *map[uint32]bool,
natProps *FieldNatProperties,
) VisitResult {
natParamName := t.NatParams[natIndex]
typeName := t.goGlobalName
key := VisitedTypeNatParam{typeName, natIndex}
visitResult, isVisited := (*visitResults)[key]
if isVisited {
return visitResult
}
(*visitResults)[key] = VisitInProgress
switch i := t.trw.(type) {
case *TypeRWStruct:
{
for _, f := range i.Fields {
if f.fieldMask != nil &&
!f.fieldMask.isField &&
!f.fieldMask.isArith &&
natParamName == f.fieldMask.name {
*natProps |= FieldUsedAsFieldMask
(*affectedIndexes)[f.BitNumber] = true
(*visitResults)[key] = VisitSuccess
}
natIndexes := make([]int, 0)
for i, natParam := range f.t.NatParams {
if natParam == natParamName {
natIndexes = append(natIndexes, i)
}
}
for _, index := range natIndexes {
res := visit(f.t, index, visitResults, affectedIndexes, natProps)
if res == VisitSuccess {
(*visitResults)[key] = VisitSuccess
}
}
}
}
case *TypeRWUnion:
{
for _, f := range i.Fields {
res := visit(f.t, natIndex, visitResults, affectedIndexes, natProps)
if res == VisitSuccess {
(*visitResults)[key] = VisitSuccess
}
}
}
case *TypeRWMaybe:
{
res := visit(i.element.t, natIndex, visitResults, affectedIndexes, natProps)
if res == VisitSuccess {
(*visitResults)[key] = VisitSuccess
}
}
case *TypeRWBrackets:
{
*natProps |= FieldUsedAsSize
elementType := i.element.t
natIndexes := make([]int, 0)
for i, natParam := range elementType.NatParams {
if natParam == natParamName {
natIndexes = append(natIndexes, i)
}
}
for _, index := range natIndexes {
res := visit(elementType, index, visitResults, affectedIndexes, natProps)
if res == VisitSuccess {
(*visitResults)[key] = VisitSuccess
}
}
}
}
if (*visitResults)[key] == VisitInProgress {
(*visitResults)[key] = VisitFail
}
return (*visitResults)[key]
}
// AllAffectedFieldMasks f must be from trw.Fields
func (trw *TypeRWStruct) AllAffectedFieldMasks(f Field) (nats []Field, bits []uint32) {
curField := f
for curField.IsAffectingLocalFieldMasks() {
ancestor := trw.Fields[curField.fieldMask.FieldIndex]
nats = append(nats, ancestor)
bits = append(bits, curField.BitNumber)
curField = ancestor
}
return
}
func (trw *TypeRWStruct) BeforeCodeGenerationStep2() {
//if trw.wr.gen.options.Language == "cpp" { // TODO - temporary solution to benchmark combined tl
// var nf []Field
// for _, f := range trw.Fields {
// if !f.recursive {
// nf = append(nf, f)
// // panic("recursive field in union " + trw.wr.tlName.String())
//}
//}
//trw.Fields = nf
//}
}
func (trw *TypeRWStruct) fillRecursiveChildren(visitedNodes map[*TypeRWWrapper]bool) {
for _, f := range trw.Fields {
if !f.recursive {
f.t.FillRecursiveChildren(visitedNodes)
}
}
}
func (trw *TypeRWStruct) IsDictKeySafe() (isSafe bool, isString bool) {
if trw.isTypeDef() {
return trw.Fields[0].t.trw.IsDictKeySafe()
}
return false, false
}
func (trw *TypeRWStruct) CanBeBareBoxed() (canBare bool, canBoxed bool) {
return true, true
}
// same code as in func (w *TypeRWWrapper) transformNatArgsToChild
func (trw *TypeRWStruct) replaceUnwrapArgs(natArgs []string) []string {
// Caller called outer.Read( , nat_x, nat_y)
// outer has func Read( ,nat_inner_x uint32, nat_inner_y uint32) {
// which calls for example inner.Read( , nat_inner_y, nat_inner_y)
// in other words, outer passes some parameters to inner in some order, with potential repeats.
// When unwrapping, we do the job of golang compiler, replacing references to outer nat parameters,
// so that at the calling site outer.Read( , nat_x, nat_y) is replaced to
// inner.Read( , nat_y, nat_y)
var result []string
outer:
for _, arg := range trw.Fields[0].natArgs {
if arg.isArith || arg.isField {
panic("cannot replace to child arith or field nat param")
}
for i, p := range trw.wr.NatParams {
if p == arg.name {
result = append(result, natArgs[i])
continue outer
}
}
log.Panicf("internal compiler error, nat parameter %s not found for unwrap type of goName %s", arg.name, trw.wr.goGlobalName)
}
return result
}
func (trw *TypeRWStruct) typeResettingCode(bytesVersion bool, directImports *DirectImports, ins *InternalNamespace, val string, ref bool) string {
if trw.isUnwrapType() {
return trw.Fields[0].t.TypeResettingCode(bytesVersion, directImports, ins, val, ref)
}
return fmt.Sprintf("%s.Reset()", val)
}
func (trw *TypeRWStruct) typeRandomCode(bytesVersion bool, directImports *DirectImports, ins *InternalNamespace, val string, natArgs []string, ref bool) string {
if trw.isUnwrapType() {
return trw.Fields[0].t.TypeRandomCode(bytesVersion, directImports, ins, val, trw.replaceUnwrapArgs(natArgs), ref)
}
return fmt.Sprintf("%s.FillRandom(rg %s)", val, joinWithCommas(natArgs))
}
func (trw *TypeRWStruct) typeWritingCode(bytesVersion bool, directImports *DirectImports, ins *InternalNamespace, val string, bare bool, natArgs []string, ref bool, last bool, needError bool) string {
if trw.isUnwrapType() {
prefix := ""
if !bare {
prefix = fmt.Sprintf("w = basictl.NatWrite(w, 0x%x)\n", trw.wr.tlTag)
}
return prefix + trw.Fields[0].t.TypeWritingCode(bytesVersion, directImports, ins, val, trw.Fields[0].Bare(), trw.replaceUnwrapArgs(natArgs), ref, last, needError)
// was
// goName := addBytes(trw.goGlobalName, bytesVersion)
// return wrapLastW(last, fmt.Sprintf("(*%s)(%s).Write%s(w%s)", trw.wr.ins.Prefix(ins)+goName, addAmpersand(ref, val), addBare(bare), joinWithCommas(natArgs)))
}
return wrapLastW(last, fmt.Sprintf("%s.Write%s(w %s)", val, addBare(bare), joinWithCommas(natArgs)), needError)
}
func (trw *TypeRWStruct) typeReadingCode(bytesVersion bool, directImports *DirectImports, ins *InternalNamespace, val string, bare bool, natArgs []string, ref bool, last bool) string {
if trw.isUnwrapType() {
prefix := ""
if !bare {
prefix = fmt.Sprintf("if w, err = basictl.NatReadExactTag(w, 0x%x); err != nil {\nreturn w, err\n}\n", trw.wr.tlTag)
}
return prefix + trw.Fields[0].t.TypeReadingCode(bytesVersion, directImports, ins, val, trw.Fields[0].Bare(), trw.replaceUnwrapArgs(natArgs), ref, last)
// was
// goName := addBytes(trw.goGlobalName, bytesVersion)
// return wrapLastW(last, fmt.Sprintf("(*%s)(%s).Read%s(w%s)", trw.wr.ins.Prefix(ins)+goName, addAmpersand(ref, val), addBare(bare), joinWithCommas(natArgs)))
}
return wrapLastW(last, fmt.Sprintf("%s.Read%s(w %s)", val, addBare(bare), joinWithCommas(natArgs)), true)
}
func (trw *TypeRWStruct) typeJSONEmptyCondition(bytesVersion bool, val string, ref bool) string {
if trw.isTypeDef() {
return trw.Fields[0].t.TypeJSONEmptyCondition(bytesVersion, val, ref)
}
return ""
}
func (trw *TypeRWStruct) typeJSONWritingCode(bytesVersion bool, directImports *DirectImports, ins *InternalNamespace, val string, natArgs []string, ref bool, needError bool) string {
if trw.isUnwrapType() {
return trw.Fields[0].t.TypeJSONWritingCode(bytesVersion, directImports, ins, val, trw.replaceUnwrapArgs(natArgs), ref, needError)
}
if needError {
return fmt.Sprintf("if w, err = %s.WriteJSONOpt(newTypeNames, short, w %s); err != nil { return w, err }", val, joinWithCommas(natArgs))
} else {
return fmt.Sprintf("w = %s.WriteJSONOpt(newTypeNames, short, w %s)", val, joinWithCommas(natArgs))
}
}
func (trw *TypeRWStruct) typeJSONReadingCode(bytesVersion bool, directImports *DirectImports, ins *InternalNamespace, jvalue string, val string, natArgs []string, ref bool) string {
if trw.isUnwrapType() {
return trw.Fields[0].t.TypeJSONReadingCode(bytesVersion, directImports, ins, jvalue, val, trw.replaceUnwrapArgs(natArgs), ref)
}
return fmt.Sprintf("if err := %s.ReadJSONLegacy(legacyTypeNames, %s %s); err != nil { return err }", val, jvalue, joinWithCommas(natArgs))
}
func (trw *TypeRWStruct) typeJSON2ReadingCode(bytesVersion bool, directImports *DirectImports, ins *InternalNamespace, jvalue string, val string, natArgs []string, ref bool) string {
if trw.isUnwrapType() {
return trw.Fields[0].t.TypeJSON2ReadingCode(bytesVersion, directImports, ins, jvalue, val, trw.replaceUnwrapArgs(natArgs), ref)
}
return fmt.Sprintf("if err := %s.ReadJSON(legacyTypeNames, %s %s); err != nil { return err }", val, jvalue, joinWithCommas(natArgs))
}