1
2
3
4
5
6
7 package types2
8
9 import (
10 "bytes"
11 "cmd/compile/internal/syntax"
12 "fmt"
13 "strconv"
14 "strings"
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 func quote(s string) string {
43 if s == "_" {
44
45 return s
46 }
47 return "`" + s + "'"
48 }
49
50 func sprintf(qf Qualifier, tpSubscripts bool, format string, args ...any) string {
51 for i, arg := range args {
52 switch a := arg.(type) {
53 case nil:
54 arg = "<nil>"
55 case operand:
56 panic("got operand instead of *operand")
57 case *operand:
58 arg = operandString(a, qf)
59 case syntax.Pos:
60 arg = a.String()
61 case syntax.Expr:
62 arg = ExprString(a)
63 case []syntax.Expr:
64 var buf strings.Builder
65 buf.WriteByte('[')
66 for i, x := range a {
67 if i > 0 {
68 buf.WriteString(", ")
69 }
70 buf.WriteString(ExprString(x))
71 }
72 buf.WriteByte(']')
73 arg = buf.String()
74 case Object:
75 arg = ObjectString(a, qf)
76 case Type:
77 var buf bytes.Buffer
78 w := newTypeWriter(&buf, qf)
79 w.tpSubscripts = tpSubscripts
80 w.typ(a)
81 arg = buf.String()
82 case []Type:
83 var buf bytes.Buffer
84 w := newTypeWriter(&buf, qf)
85 w.tpSubscripts = tpSubscripts
86 buf.WriteByte('[')
87 for i, x := range a {
88 if i > 0 {
89 buf.WriteString(", ")
90 }
91 w.typ(x)
92 }
93 buf.WriteByte(']')
94 arg = buf.String()
95 case []*TypeParam:
96 var buf bytes.Buffer
97 w := newTypeWriter(&buf, qf)
98 w.tpSubscripts = tpSubscripts
99 buf.WriteByte('[')
100 for i, x := range a {
101 if i > 0 {
102 buf.WriteString(", ")
103 }
104 w.typ(x)
105 }
106 buf.WriteByte(']')
107 arg = buf.String()
108 }
109 args[i] = arg
110 }
111 return fmt.Sprintf(format, args...)
112 }
113
114
115 func (check *Checker) sprintf(format string, args ...any) string {
116 var qf Qualifier
117 if check != nil {
118 qf = check.qualifier
119 }
120 return sprintf(qf, false, format, args...)
121 }
122
123 func (check *Checker) trace(pos syntax.Pos, format string, args ...any) {
124 fmt.Printf("%s:\t%s%s\n",
125 pos,
126 strings.Repeat(". ", check.indent),
127 sprintf(check.qualifier, true, format, args...),
128 )
129 }
130
131
132 func (check *Checker) dump(format string, args ...any) {
133 fmt.Println(sprintf(check.qualifier, true, format, args...))
134 }
135
136 func (check *Checker) qualifier(pkg *Package) string {
137
138 if pkg != check.pkg {
139 if check.pkgPathMap == nil {
140 check.pkgPathMap = make(map[string]map[string]bool)
141 check.seenPkgMap = make(map[*Package]bool)
142 check.markImports(check.pkg)
143 }
144
145 if len(check.pkgPathMap[pkg.name]) > 1 {
146 return strconv.Quote(pkg.path)
147 }
148 return pkg.name
149 }
150 return ""
151 }
152
153
154
155 func (check *Checker) markImports(pkg *Package) {
156 if check.seenPkgMap[pkg] {
157 return
158 }
159 check.seenPkgMap[pkg] = true
160
161 forName, ok := check.pkgPathMap[pkg.name]
162 if !ok {
163 forName = make(map[string]bool)
164 check.pkgPathMap[pkg.name] = forName
165 }
166 forName[pkg.path] = true
167
168 for _, imp := range pkg.imports {
169 check.markImports(imp)
170 }
171 }
172
173
174 func stripAnnotations(s string) string {
175 var buf strings.Builder
176 for _, r := range s {
177
178 if r < '₀' || '₀'+10 <= r {
179 buf.WriteRune(r)
180 }
181 }
182 if buf.Len() < len(s) {
183 return buf.String()
184 }
185 return s
186 }
187
View as plain text