-
Notifications
You must be signed in to change notification settings - Fork 28
/
tmpl_struct.go
175 lines (159 loc) · 5.46 KB
/
tmpl_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
// Copyright 2016 The Vanadium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Copyright 2015 The Vanadium Authors. All rights reserved.
// Use of self source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package swift
import (
"bytes"
"log"
"fmt"
"v.io/v23/vdl"
"v.io/x/ref/lib/vdl/compile"
)
const structTmpl = `{{ .Doc }}///
/// Auto-generated from {{.VdlTypeName}}
{{ .AccessModifier }} class {{.Name}} : VdlStruct, CustomDebugStringConvertible {{ if .IsHashable }}, Hashable {{ end }}{
/// Vdl type for {@link {{.Name}}}.
{{ .AccessModifier }} static let vdlTypeName = "{{.VdlTypeName}}"
{{/* Field declarations */}}
{{ range $index, $field := .Fields }}
/// "{{$field.VdlName}}" index {{$index}}
var {{$field.Name}}: {{$field.Type}}
{{ end }}
{{ .AccessModifier }} init({{ range $index, $field := .Fields }}{{ if gt $index 0 }},{{ end }}
{{$field.Name}}: {{$field.Type}}{{end}}) {
{{range $field := .Fields}}self.{{$field.Name}} = {{$field.Name}}
{{end}}{{/* range $field */}}
}
{{ if .IsHashable }}
{{ .AccessModifier }} var hashValue: Int {
var result = 1
let prime = 31
{{ range $field := .Fields }}result = prime * result + Int({{$field.HashcodeComputation}})
{{ end }}
return result
}
{{ end }}
{{ .AccessModifier }} var description: String {
return "{{ .VdlTypeName }}"
}
{{ .AccessModifier }} var debugDescription: String {
var result = "[{{ .VdlTypeName }} "
{{ range $index, $field := .Fields }}
{{ if gt $index 0 }}result += ", "{{ end }}
{{ if .IsAny }}
result += "{{$field.Name}}:" + (({{$field.Name}} as? CustomDebugStringConvertible)?.debugDescription ?? "\({{$field.Name}})")
{{ else }}
result += "{{$field.Name}}:" + {{$field.Name}}.debugDescription
{{ end }}{{/* IsAny */}}
{{ end }}{{/* range over fields */}}
return result + "]"
}
}
{{ if .IsHashable }}
{{ .AccessModifier }} func ==(lhs: {{.Name}}, rhs: {{.Name}}) -> Bool {
{{ range $field := .Fields }}
{{ if $field.IsOptional }}
switch (lhs.{{$field.Name}}, rhs.{{$field.Name}}) {
case (nil, nil): break
case (nil, _): return false
case (_, nil): return false
case let (lfield?, rfield?):
if lfield != rfield {
return false
}
default: break
}
{{ else }}
if lhs.{{$field.Name}} != rhs.{{$field.Name}} {
return false
}
{{ end }} {{/* optional comparison */}}
{{ end }} {{/* range over fields */}}
return true
}{{ end }}`
type structDefinitionField struct {
AccessModifier string
Class string
Doc string
HashcodeComputation string
IsAny bool
IsOptional bool
Name string
Type string
VdlName string
}
func swiftFieldArgStr(tdef *compile.TypeDef, ctx *swiftContext) string {
var buf bytes.Buffer
for i := 0; i < tdef.Type.NumField(); i++ {
if i > 0 {
buf.WriteString(", ")
}
fld := tdef.Type.Field(i)
buf.WriteString(ctx.swiftType(fld.Type))
buf.WriteString(" ")
buf.WriteString(swiftVariableName(tdef, fld))
}
return buf.String()
}
// genSwiftStructFile generates the Swift class file for the provided user-defined type.
func genSwiftStructTdef(tdef *compile.TypeDef, ctx *swiftContext) string {
isHashable := isTypeHashable(tdef.Type, make(map[*vdl.Type]bool))
fields := make([]structDefinitionField, tdef.Type.NumField())
for i := 0; i < tdef.Type.NumField(); i++ {
fld := tdef.Type.Field(i)
hashComp := ""
if isHashable {
var err error
hashComp, err = ctx.swiftHashCode("self."+swiftVariableName(tdef, fld), fld.Type)
if err != nil {
panic(fmt.Sprintf("Unable to compute hashcode: %v", err))
}
}
fields[i] = structDefinitionField{
AccessModifier: swiftAccessModifierForName(fld.Name),
Class: ctx.swiftType(fld.Type),
Doc: swiftDoc(tdef.FieldDoc[i], tdef.FieldDocSuffix[i]),
HashcodeComputation: hashComp,
IsAny: fld.Type.Kind() == vdl.Any,
IsOptional: fld.Type.Kind() == vdl.Optional,
Name: swiftVariableName(tdef, fld),
Type: ctx.swiftType(fld.Type),
VdlName: fld.Name,
}
}
// Determine if we can create a hashValue out of this. A hashValue implies
// equality and access to primitives in a useful way. With Any types we cannot
// do this. One plausible hack is to use "\(x)", but if for some reason the
// underlying type doesn't convey enough information in its forced string
// conversion then we're not properly comparing.
data := struct {
AccessModifier string
Doc string
Fields []structDefinitionField
FieldsAsArgs string
IsHashable bool
Name string
Source string
VdlTypeName string
VdlTypeString string
}{
AccessModifier: swiftAccessModifier(tdef),
Doc: swiftDoc(tdef.Doc, tdef.DocSuffix),
Fields: fields,
FieldsAsArgs: swiftFieldArgStr(tdef, ctx),
IsHashable: isHashable,
Name: ctx.swiftTypeName(tdef),
Source: tdef.File.BaseName,
VdlTypeName: tdef.Type.Name(),
VdlTypeString: tdef.Type.String(),
}
var buf bytes.Buffer
err := parseTmpl("swift struct", structTmpl).Execute(&buf, data)
if err != nil {
log.Fatalf("vdl: couldn't execute struct template: %v", err)
}
return formatSwiftCode(buf.String())
}