1 // Copyright (c) 2020 Cisco and/or its affiliates.
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:
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
21 "git.fd.io/govpp.git/binapigen/vppapi"
24 func SortFileObjectsByName(file *vppapi.File) {
25 sort.SliceStable(file.Imports, func(i, j int) bool {
26 return file.Imports[i] < file.Imports[j]
28 sort.SliceStable(file.EnumTypes, func(i, j int) bool {
29 return file.EnumTypes[i].Name < file.EnumTypes[j].Name
31 sort.SliceStable(file.EnumflagTypes, func(i, j int) bool {
32 return file.EnumflagTypes[i].Name < file.EnumflagTypes[j].Name
34 sort.Slice(file.AliasTypes, func(i, j int) bool {
35 return file.AliasTypes[i].Name < file.AliasTypes[j].Name
37 sort.SliceStable(file.StructTypes, func(i, j int) bool {
38 return file.StructTypes[i].Name < file.StructTypes[j].Name
40 sort.SliceStable(file.UnionTypes, func(i, j int) bool {
41 return file.UnionTypes[i].Name < file.UnionTypes[j].Name
43 sort.SliceStable(file.Messages, func(i, j int) bool {
44 return file.Messages[i].Name < file.Messages[j].Name
46 if file.Service != nil {
47 sort.Slice(file.Service.RPCs, func(i, j int) bool {
48 return file.Service.RPCs[i].Request < file.Service.RPCs[j].Request
53 func importedFiles(files []*vppapi.File, file *vppapi.File) []*vppapi.File {
54 var list []*vppapi.File
55 byName := func(s string) *vppapi.File {
56 for _, f := range files {
63 imported := map[string]struct{}{}
64 for _, imp := range file.Imports {
65 imp = normalizeImport(imp)
66 impFile := byName(imp)
68 log.Fatalf("file %q not found", imp)
70 for _, nest := range importedFiles(files, impFile) {
71 if _, ok := imported[nest.Name]; !ok {
72 list = append(list, nest)
73 imported[nest.Name] = struct{}{}
76 if _, ok := imported[impFile.Name]; !ok {
77 list = append(list, impFile)
78 imported[impFile.Name] = struct{}{}
84 func SortFilesByImports(apifiles []*vppapi.File) {
85 dependsOn := func(file *vppapi.File, dep string) bool {
86 for _, imp := range importedFiles(apifiles, file) {
93 sort.Slice(apifiles, func(i, j int) bool {
96 if dependsOn(a, b.Name) {
99 if dependsOn(b, a.Name) {
102 return len(b.Imports) > len(a.Imports)
106 func ListImportedTypes(apifiles []*vppapi.File, file *vppapi.File) []string {
107 var importedTypes []string
108 typeFiles := importedFiles(apifiles, file)
109 for _, t := range file.StructTypes {
111 for _, imp := range typeFiles {
112 for _, at := range imp.StructTypes {
113 if at.Name != t.Name {
116 importedTypes = append(importedTypes, t.Name)
125 for _, t := range file.AliasTypes {
127 for _, imp := range typeFiles {
128 for _, at := range imp.AliasTypes {
129 if at.Name != t.Name {
132 importedTypes = append(importedTypes, t.Name)
141 for _, t := range file.EnumTypes {
143 for _, imp := range typeFiles {
144 for _, at := range imp.EnumTypes {
145 if at.Name != t.Name {
148 importedTypes = append(importedTypes, t.Name)
157 for _, t := range file.EnumflagTypes {
159 for _, imp := range typeFiles {
160 for _, at := range imp.EnumflagTypes {
161 if at.Name != t.Name {
164 importedTypes = append(importedTypes, t.Name)
173 for _, t := range file.UnionTypes {
175 for _, imp := range typeFiles {
176 for _, at := range imp.UnionTypes {
177 if at.Name != t.Name {
180 importedTypes = append(importedTypes, t.Name)
192 func RemoveImportedTypes(apifiles []*vppapi.File, apifile *vppapi.File) {
193 importedTypes := ListImportedTypes(apifiles, apifile)
194 isImportedType := func(s string) bool {
195 for _, t := range importedTypes {
202 var enums []vppapi.EnumType
203 for _, enumType := range apifile.EnumTypes {
204 if !isImportedType(enumType.Name) {
205 enums = append(enums, enumType)
208 var enumflags []vppapi.EnumType
209 for _, enumflagType := range apifile.EnumflagTypes {
210 if !isImportedType(enumflagType.Name) {
211 enumflags = append(enumflags, enumflagType)
214 var aliases []vppapi.AliasType
215 for _, aliasType := range apifile.AliasTypes {
216 if !isImportedType(aliasType.Name) {
217 aliases = append(aliases, aliasType)
220 var structs []vppapi.StructType
221 for _, structType := range apifile.StructTypes {
222 if !isImportedType(structType.Name) {
223 structs = append(structs, structType)
226 var unions []vppapi.UnionType
227 for _, unionType := range apifile.UnionTypes {
228 if !isImportedType(unionType.Name) {
229 unions = append(unions, unionType)
232 apifile.EnumTypes = enums
233 apifile.EnumflagTypes = enumflags
234 apifile.AliasTypes = aliases
235 apifile.StructTypes = structs
236 apifile.UnionTypes = unions