aboutsummaryrefslogtreecommitdiffstats
path: root/binapigen/types.go
blob: 0dbbeb13923663a76ef2d5d8fc823dcdbf24a9a6 (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
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
//  Copyright (c) 2020 Cisco and/or its affiliates.
//
//  Licensed under the Apache License, Version 2.0 (the "License");
//  you may not use this file except in compliance with the License.
//  You may obtain a copy of the License at:
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
//  Unless required by applicable law or agreed to in writing, software
//  distributed under the License is distributed on an "AS IS" BASIS,
//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//  See the License for the specific language governing permissions and
//  limitations under the License.

package binapigen

import (
	"strings"

	"github.com/sirupsen/logrus"
)

// define api
const (
	defineApiPrefix = "vl_api_"
	defineApiSuffix = "_t"
)

// BaseType represents base types in VPP binary API.
type BaseType int

const (
	U8 BaseType = iota + 1
	I8
	U16
	I16
	U32
	I32
	U64
	I64
	F64
	BOOL
	STRING
)

var (
	BaseTypes = map[BaseType]string{
		U8:     "u8",
		I8:     "i8",
		U16:    "u16",
		I16:    "i16",
		U32:    "u32",
		I32:    "i32",
		U64:    "u64",
		I64:    "i64",
		F64:    "f64",
		BOOL:   "bool",
		STRING: "string",
	}
	BaseTypeNames = map[string]BaseType{
		"u8":     U8,
		"i8":     I8,
		"u16":    U16,
		"i16":    I16,
		"u32":    U32,
		"i32":    I32,
		"u64":    U64,
		"i64":    I64,
		"f64":    F64,
		"bool":   BOOL,
		"string": STRING,
	}
)

var BaseTypeSizes = map[BaseType]int{
	U8:     1,
	I8:     1,
	U16:    2,
	I16:    2,
	U32:    4,
	I32:    4,
	U64:    8,
	I64:    8,
	F64:    8,
	BOOL:   1,
	STRING: 1,
}

type Kind int

const (
	_ = iota
	Uint8Kind
	Int8Kind
	Uint16Kind
	Int16Kind
	Uint32Kind
	Int32Kind
	Uint64Kind
	Int64Kind
	Float64Kind
	BoolKind
	StringKind
	EnumKind
	AliasKind
	StructKind
	UnionKind
	MessageKind
)

// toApiType returns name that is used as type reference in VPP binary API
func toApiType(name string) string {
	return defineApiPrefix + name + defineApiSuffix
}

func fromApiType(typ string) string {
	name := typ
	name = strings.TrimPrefix(name, defineApiPrefix)
	name = strings.TrimSuffix(name, defineApiSuffix)
	return name
}

func getSizeOfType(module *File, typ *Struct) (size int) {
	for _, field := range typ.Fields {
		enum := getEnumByRef(module, field.Type)
		if enum != nil {
			size += getSizeOfBinapiTypeLength(enum.Type, field.Length)
			continue
		}
		size += getSizeOfBinapiTypeLength(field.Type, field.Length)
	}
	return size
}

func getEnumByRef(file *File, ref string) *Enum {
	for _, typ := range file.Enums {
		if ref == toApiType(typ.Name) {
			return typ
		}
	}
	return nil
}

func getTypeByRef(file *File, ref string) *Struct {
	for _, typ := range file.Structs {
		if ref == toApiType(typ.Name) {
			return typ
		}
	}
	return nil
}

func getAliasByRef(file *File, ref string) *Alias {
	for _, alias := range file.Aliases {
		if ref == toApiType(alias.Name) {
			return alias
		}
	}
	return nil
}

func getUnionByRef(file *File, ref string) *Union {
	for _, union := range file.Unions {
		if ref == toApiType(union.Name) {
			return union
		}
	}
	return nil
}

func getBinapiTypeSize(binapiType string) (size int) {
	typName := BaseTypeNames[binapiType]
	return BaseTypeSizes[typName]
}

// binapiTypes is a set of types used VPP binary API for translation to Go types
var binapiTypes = map[string]string{
	"u8":  "uint8",
	"i8":  "int8",
	"u16": "uint16",
	"i16": "int16",
	"u32": "uint32",
	"i32": "int32",
	"u64": "uint64",
	"i64": "int64",
	"f64": "float64",
}
var BaseTypesGo = map[BaseType]string{
	U8:     "uint8",
	I8:     "int8",
	U16:    "uint16",
	I16:    "int16",
	U32:    "uint32",
	I32:    "int32",
	U64:    "uint64",
	I64:    "int64",
	F64:    "float64",
	BOOL:   "bool",
	STRING: "string",
}

func getActualType(file *File, typ string) (actual string) {
	for _, enum := range file.Enums {
		if enum.GoName == typ {
			return enum.Type
		}
	}
	for _, alias := range file.Aliases {
		if alias.GoName == typ {
			return alias.Type
		}
	}
	return typ
}

// convertToGoType translates the VPP binary API type into Go type
func convertToGoType(file *File, binapiType string) (typ string) {
	if t, ok := binapiTypes[binapiType]; ok {
		// basic types
		typ = t
	} else if r, ok := file.refmap[binapiType]; ok {
		// specific types (enums/types/unions)
		typ = camelCaseName(r)
	} else {
		switch binapiType {
		case "bool", "string":
			typ = binapiType
		default:
			// fallback type
			logrus.Warnf("found unknown VPP binary API type %q, using byte", binapiType)
			typ = "byte"
		}
	}
	return typ
}

func getSizeOfBinapiTypeLength(typ string, length int) (size int) {
	if n := getBinapiTypeSize(typ); n > 0 {
		if length > 0 {
			return n * length
		} else {
			return n
		}
	}

	return
}

func getUnionSize(file *File, union *Union) (maxSize int) {
	for _, field := range union.Fields {
		typ := getTypeByRef(file, field.Type)
		if typ != nil {
			if size := getSizeOfType(file, typ); size > maxSize {
				maxSize = size
			}
			continue
		}
		alias := getAliasByRef(file, field.Type)
		if alias != nil {
			if size := getSizeOfBinapiTypeLength(alias.Type, alias.Length); size > maxSize {
				maxSize = size
			}
			continue
		} else {
			logf("no type or alias found for union %s field type %q", union.Name, field.Type)
			continue
		}
	}
	logf("getUnionSize: %s %+v max=%v", union.Name, union.Fields, maxSize)
	return
}