Generator improvements and cleanup
[govpp.git] / cmd / binapi-generator / types.go
1 // Copyright (c) 2019 Cisco and/or its affiliates.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at:
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package main
16
17 import (
18         "fmt"
19         "strconv"
20         "strings"
21 )
22
23 // toApiType returns name that is used as type reference in VPP binary API
24 func toApiType(name string) string {
25         return fmt.Sprintf("vl_api_%s_t", name)
26 }
27
28 // binapiTypes is a set of types used VPP binary API for translation to Go types
29 var binapiTypes = map[string]string{
30         "u8":  "uint8",
31         "i8":  "int8",
32         "u16": "uint16",
33         "i16": "int16",
34         "u32": "uint32",
35         "i32": "int32",
36         "u64": "uint64",
37         "i64": "int64",
38         "f64": "float64",
39 }
40
41 func getBinapiTypeSize(binapiType string) int {
42         if _, ok := binapiTypes[binapiType]; ok {
43                 b, err := strconv.Atoi(strings.TrimLeft(binapiType, "uif"))
44                 if err == nil {
45                         return b / 8
46                 }
47         }
48         return -1
49 }
50
51 // convertToGoType translates the VPP binary API type into Go type
52 func convertToGoType(ctx *context, binapiType string) (typ string) {
53         if t, ok := binapiTypes[binapiType]; ok {
54                 // basic types
55                 typ = t
56         } else if r, ok := ctx.packageData.RefMap[binapiType]; ok {
57                 // specific types (enums/types/unions)
58                 typ = camelCaseName(r)
59         } else {
60                 switch binapiType {
61                 case "bool", "string":
62                         typ = binapiType
63                 default:
64                         // fallback type
65                         log.Warnf("found unknown VPP binary API type %q, using byte", binapiType)
66                         typ = "byte"
67                 }
68         }
69         return typ
70 }
71
72 func getSizeOfType(typ *Type) (size int) {
73         for _, field := range typ.Fields {
74                 size += getSizeOfBinapiTypeLength(field.Type, field.Length)
75         }
76         return size
77 }
78
79 func getSizeOfBinapiTypeLength(typ string, length int) (size int) {
80         if n := getBinapiTypeSize(typ); n > 0 {
81                 if length > 0 {
82                         return n * length
83                 } else {
84                         return n
85                 }
86         }
87         return
88 }
89
90 func getTypeByRef(ctx *context, ref string) *Type {
91         for _, typ := range ctx.packageData.Types {
92                 if ref == toApiType(typ.Name) {
93                         return &typ
94                 }
95         }
96         return nil
97 }
98
99 func getAliasByRef(ctx *context, ref string) *Alias {
100         for _, alias := range ctx.packageData.Aliases {
101                 if ref == toApiType(alias.Name) {
102                         return &alias
103                 }
104         }
105         return nil
106 }
107
108 func getUnionSize(ctx *context, union *Union) (maxSize int) {
109         for _, field := range union.Fields {
110                 typ := getTypeByRef(ctx, field.Type)
111                 if typ != nil {
112                         if size := getSizeOfType(typ); size > maxSize {
113                                 maxSize = size
114                         }
115                         continue
116                 }
117                 alias := getAliasByRef(ctx, field.Type)
118                 if alias != nil {
119                         if size := getSizeOfBinapiTypeLength(alias.Type, alias.Length); size > maxSize {
120                                 maxSize = size
121                         }
122                         continue
123                 } else {
124                         logf("no type or alias found for union %s field type %q", union.Name, field.Type)
125                         continue
126                 }
127         }
128         return
129 }