Debian Codegraph @

gopls/internal/protocol/generate/typenames.go

raw symbols SCIP
1 // Copyright 2022 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package main
6
7 import (
8 "fmt"
9 "log"
10 "strings"
11 )
12
13 var typeNames = make(map[*Type]string)
14 var genTypes []*newType
15
16 func findTypeNames(model *Model) {
17 for _, s := range model.Structures {
18 for _, e := range s.Extends {
19 nameType(e, nil) // all references
20 }
21 for _, m := range s.Mixins {
22 nameType(m, nil) // all references
23 }
24 for _, p := range s.Properties {
25 nameType(p.Type, []string{s.Name, p.Name})
26 }
27 }
28 for _, t := range model.Enumerations {
29 nameType(t.Type, []string{t.Name})
30 }
31 for _, t := range model.TypeAliases {
32 nameType(t.Type, []string{t.Name})
33 }
34 for _, r := range model.Requests {
35 nameType(r.Params, []string{"Param", r.Method})
36 nameType(r.Result, []string{"Result", r.Method})
37 nameType(r.RegistrationOptions, []string{"RegOpt", r.Method})
38 }
39 for _, n := range model.Notifications {
40 nameType(n.Params, []string{"Param", n.Method})
41 nameType(n.RegistrationOptions, []string{"RegOpt", n.Method})
42 }
43 }
44
45 // nameType populates typeNames[t] with the computed name of the type.
46 // path is the list of enclosing constructs in the JSON model.
47 func nameType(t *Type, path []string) string {
48 if t == nil || typeNames[t] != "" {
49 return ""
50 }
51 switch t.Kind {
52 case "base":
53 typeNames[t] = t.Name
54 return t.Name
55 case "reference":
56 typeNames[t] = t.Name
57 return t.Name
58 case "array":
59 nm := "[]" + nameType(t.Element, append(path, "Elem"))
60 typeNames[t] = nm
61 return nm
62 case "map":
63 key := nameType(t.Key, nil) // never a generated type
64 value := nameType(t.Value.(*Type), append(path, "Value"))
65 nm := "map[" + key + "]" + value
66 typeNames[t] = nm
67 return nm
68 // generated types
69 case "and":
70 nm := nameFromPath("And", path)
71 typeNames[t] = nm
72 for _, it := range t.Items {
73 nameType(it, append(path, "Item"))
74 }
75 genTypes = append(genTypes, &newType{
76 name: nm,
77 typ: t,
78 kind: "and",
79 items: t.Items,
80 line: t.Line,
81 })
82 return nm
83 case "literal":
84 nm := nameFromPath("Lit", path)
85 typeNames[t] = nm
86 for _, p := range t.Value.(ParseLiteral).Properties {
87 nameType(p.Type, append(path, p.Name))
88 }
89 genTypes = append(genTypes, &newType{
90 name: nm,
91 typ: t,
92 kind: "literal",
93 properties: t.Value.(ParseLiteral).Properties,
94 line: t.Line,
95 })
96 return nm
97 case "tuple":
98 nm := nameFromPath("Tuple", path)
99 typeNames[t] = nm
100 for _, it := range t.Items {
101 nameType(it, append(path, "Item"))
102 }
103 genTypes = append(genTypes, &newType{
104 name: nm,
105 typ: t,
106 kind: "tuple",
107 items: t.Items,
108 line: t.Line,
109 })
110 return nm
111 case "or":
112 nm := nameFromPath("Or", path)
113 typeNames[t] = nm
114 for i, it := range t.Items {
115 // these names depend on the ordering within the "or" type
116 nameType(it, append(path, fmt.Sprintf("Item%d", i)))
117 }
118 // this code handles an "or" of stringLiterals (_InitializeParams.trace)
119 names := make(map[string]int)
120 var msg strings.Builder
121 for _, it := range t.Items {
122 if line, ok := names[typeNames[it]]; ok {
123 // duplicate component names are bad
124 fmt.Fprintf(&msg, "lines %d %d dup, %s for %s\n", line, it.Line, typeNames[it], nm)
125 }
126 names[typeNames[it]] = t.Line
127 }
128 // this code handles an "or" of stringLiterals (_InitializeParams.trace)
129 if len(names) == 1 {
130 var solekey string
131 for k := range names {
132 solekey = k // the sole name
133 }
134 if solekey == "string" { // _InitializeParams.trace
135 typeNames[t] = "string"
136 return "string"
137 }
138 // otherwise unexpected
139 log.Printf("unexpected: single-case 'or' type has non-string key %s: %s", nm, solekey)
140 log.Fatal(msg.String())
141 } else if len(names) == 2 {
142 // if one of the names is null, just use the other, rather than generating an "or".
143 // This removes about 40 types from the generated code. An entry in goplsStar
144 // could be added to handle the null case, if necessary.
145 newNm := ""
146 sawNull := false
147 for k := range names {
148 if k == "null" {
149 sawNull = true
150 } else {
151 newNm = k
152 }
153 }
154 if sawNull {
155 typeNames[t] = newNm
156 return newNm
157 }
158 }
159 genTypes = append(genTypes, &newType{
160 name: nm,
161 typ: t,
162 kind: "or",
163 items: t.Items,
164 line: t.Line,
165 })
166 return nm
167 case "stringLiteral": // a single type, like 'kind' or 'rename'
168 typeNames[t] = "string"
169 return "string"
170 default:
171 log.Fatalf("nameType: %T unexpected, line:%d path:%v", t, t.Line, path)
172 panic("unreachable in nameType")
173 }
174 }
175
176 func nameFromPath(prefix string, path []string) string {
177 nm := prefix + "_" + strings.Join(path, "_")
178 // methods have slashes
179 nm = strings.ReplaceAll(nm, "/", "_")
180 return nm
181 }