Debian Codegraph @

v2/parser/parse.go

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /*
2 Copyright 2015 The Kubernetes Authors.
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15 */
16
17 package parser
18
19 import (
20 "errors"
21 "fmt"
22 "go/ast"
23 "go/constant"
24 "go/token"
25 gotypes "go/types"
26 "path/filepath"
27 "sort"
28 "strings"
29 "time"
30
31 "golang.org/x/tools/go/packages"
32
33 "k8s.io/gengo/v2/types"
34 "k8s.io/klog/v2"
35 )
36
37 // Parser lets you add all the go files in all the packages that you care
38 // about, then constructs the type source data.
39 type Parser struct {
40 // Map of package paths to definitions. These keys should be canonical
41 // Go import paths (example.com/foo/bar) and not local paths (./foo/bar).
42 goPkgs map[string]*packages.Package
43
44 // Keep track of which packages were directly requested (as opposed to
45 // those which are transitively loaded).
46 userRequested map[string]bool
47
48 // Keep track of which packages have already been scanned for types.
49 fullyProcessed map[string]bool
50
51 // Build tags to set when loading packages.
52 buildTags []string
53
54 // Tracks accumulated parsed files, so we can do position lookups later.
55 fset *token.FileSet
56
57 // All comments from everywhere in every parsed file. This map is keyed by
58 // the file-line on which the comment block ends, which makes it easy to
59 // look up comments which immediately precede a given obect (e.g. a type or
60 // function definition), which is what we almost always want. We need this
61 // because Go's own ast package does a very poor job of handling comments.
62 endLineToCommentGroup map[fileLine]*ast.CommentGroup
63 }
64
65 // key type for finding comments.
66 type fileLine struct {
67 file string
68 line int
69 }
70
71 // New constructs a new Parser.
72 func New() *Parser {
73 return NewWithOptions(Options{})
74 }
75
76 func NewWithOptions(opts Options) *Parser {
77 return &Parser{
78 goPkgs: map[string]*packages.Package{},
79 userRequested: map[string]bool{},
80 fullyProcessed: map[string]bool{},
81 fset: token.NewFileSet(),
82 endLineToCommentGroup: map[fileLine]*ast.CommentGroup{},
83 buildTags: opts.BuildTags,
84 }
85 }
86
87 // Options holds optional settings for the Parser.
88 type Options struct {
89 // BuildTags is a list of optional tags to be specified when loading
90 // packages.
91 BuildTags []string
92 }
93
94 // FindPackages expands the provided patterns into a list of Go import-paths,
95 // much like `go list -find`.
96 func (p *Parser) FindPackages(patterns ...string) ([]string, error) {
97 return p.findPackages(nil, patterns...)
98 }
99
100 // baseCfg is an optional (may be nil) config which might be injected by tests.
101 func (p *Parser) findPackages(baseCfg *packages.Config, patterns ...string) ([]string, error) {
102 toFind := make([]string, 0, len(patterns))
103 results := make([]string, 0, len(patterns))
104 for _, pat := range patterns {
105 if pkg := p.goPkgs[pat]; pkg != nil {
106 results = append(results, pkg.PkgPath)
107 } else {
108 toFind = append(toFind, pat)
109 }
110 }
111 if len(toFind) == 0 {
112 return results, nil
113 }
114
115 cfg := packages.Config{
116 Mode: packages.NeedName | packages.NeedFiles,
117 BuildFlags: []string{"-tags", strings.Join(p.buildTags, ",")},
118 Tests: false,
119 }
120 if baseCfg != nil {
121 // This is to support tests, e.g. to inject a fake GOPATH or CWD.
122 cfg.Dir = baseCfg.Dir
123 cfg.Env = baseCfg.Env
124 }
125
126 pkgs, err := packages.Load(&cfg, toFind...)
127 if err != nil {
128 return nil, fmt.Errorf("error loading packages: %w", err)
129 }
130 var allErrs []error
131 for _, pkg := range pkgs {
132 results = append(results, pkg.PkgPath)
133
134 // pkg.Errors is not a slice of `error`, but concrete types. We have
135 // to iteratively convert each one into `error`.
136 var errs []error
137 for _, e := range pkg.Errors {
138 errs = append(errs, e)
139 }
140 if len(errs) > 0 {
141 allErrs = append(allErrs, fmt.Errorf("error(s) in %q:\n%w", pkg.PkgPath, errors.Join(errs...)))
142 }
143 }
144 if len(allErrs) != 0 {
145 return nil, errors.Join(allErrs...)
146 }
147 return results, nil
148 }
149
150 // LoadPackages loads and parses the specified Go packages. Specifically
151 // named packages (without a trailing "/...") which do not exist or have no Go
152 // files are an error.
153 func (p *Parser) LoadPackages(patterns ...string) error {
154 _, err := p.loadPackages(patterns...)
155 return err
156 }
157
158 // LoadPackagesWithConfigForTesting loads and parses the specified Go packages with the
159 // specified packages.Config as a starting point. This is for testing, and
160 // only the .Dir and .Env fields of the Config will be considered.
161 func (p *Parser) LoadPackagesWithConfigForTesting(cfg *packages.Config, patterns ...string) error {
162 _, err := p.loadPackagesWithConfig(cfg, patterns...)
163 return err
164 }
165
166 // LoadPackagesTo loads and parses the specified Go packages, and inserts them
167 // into the specified Universe. It returns the packages which match the
168 // patterns, but loads all packages and their imports, recursively, into the
169 // universe. See NewUniverse for more.
170 func (p *Parser) LoadPackagesTo(u *types.Universe, patterns ...string) ([]*types.Package, error) {
171 // Load Packages.
172 pkgs, err := p.loadPackages(patterns...)
173 if err != nil {
174 return nil, err
175 }
176
177 // Load types in all packages (it will internally filter).
178 if err := p.addPkgsToUniverse(pkgs, u); err != nil {
179 return nil, err
180 }
181
182 // Return the results as gengo types.Packages.
183 ret := make([]*types.Package, 0, len(pkgs))
184 for _, pkg := range pkgs {
185 ret = append(ret, u.Package(pkg.PkgPath))
186 }
187
188 return ret, nil
189 }
190
191 func (p *Parser) loadPackages(patterns ...string) ([]*packages.Package, error) {
192 return p.loadPackagesWithConfig(nil, patterns...)
193 }
194
195 // baseCfg is an optional (may be nil) config which might be injected by tests.
196 func (p *Parser) loadPackagesWithConfig(baseCfg *packages.Config, patterns ...string) ([]*packages.Package, error) {
197 klog.V(5).Infof("loadPackages %q", patterns)
198
199 // Loading packages is slow - only do ones we know we have not already done
200 // (e.g. if a tool calls LoadPackages itself).
201 existingPkgs, netNewPkgs, err := p.alreadyLoaded(baseCfg, patterns...)
202 if err != nil {
203 return nil, err
204 }
205 if vlog := klog.V(5); vlog.Enabled() {
206 if len(existingPkgs) > 0 {
207 keys := make([]string, 0, len(existingPkgs))
208 for _, p := range existingPkgs {
209 keys = append(keys, p.PkgPath)
210 }
211 vlog.Infof(" already have: %q", keys)
212 }
213 if len(netNewPkgs) > 0 {
214 vlog.Infof(" to be loaded: %q", netNewPkgs)
215 }
216 }
217
218 // If these were not user-requested before, they are now.
219 for _, pkg := range existingPkgs {
220 if !p.userRequested[pkg.PkgPath] {
221 p.userRequested[pkg.PkgPath] = true
222 }
223 }
224 for _, pkg := range netNewPkgs {
225 if !p.userRequested[pkg] {
226 p.userRequested[pkg] = true
227 }
228 }
229
230 if len(netNewPkgs) == 0 {
231 return existingPkgs, nil
232 }
233
234 cfg := packages.Config{
235 Mode: packages.NeedName |
236 packages.NeedFiles | packages.NeedImports | packages.NeedDeps |
237 packages.NeedModule | packages.NeedTypes | packages.NeedSyntax,
238 BuildFlags: []string{"-tags", strings.Join(p.buildTags, ",")},
239 Fset: p.fset,
240 Tests: false,
241 }
242 if baseCfg != nil {
243 // This is to support tests, e.g. to inject a fake GOPATH or CWD.
244 cfg.Dir = baseCfg.Dir
245 cfg.Env = baseCfg.Env
246 }
247
248 tBefore := time.Now()
249 pkgs, err := packages.Load(&cfg, netNewPkgs...)
250 if err != nil {
251 return nil, fmt.Errorf("error loading packages: %w", err)
252 }
253 klog.V(5).Infof(" loaded %d pkg(s) in %v", len(pkgs), time.Since(tBefore))
254
255 // Handle any errors.
256 collectErrors := func(pkg *packages.Package) error {
257 var errs []error
258 for _, e := range pkg.Errors {
259 if e.Kind == packages.ListError || e.Kind == packages.ParseError {
260 errs = append(errs, e)
261 }
262 }
263 if len(errs) > 0 {
264 return fmt.Errorf("error(s) in %q:\n%w", pkg.PkgPath, errors.Join(errs...))
265 }
266 return nil
267 }
268 if err := forEachPackageRecursive(pkgs, collectErrors); err != nil {
269 return nil, err
270 }
271
272 // Finish integrating packages into our state.
273 absorbPkg := func(pkg *packages.Package) error {
274 p.goPkgs[pkg.PkgPath] = pkg
275
276 for _, f := range pkg.Syntax {
277 for _, c := range f.Comments {
278 // We need to do this on _every_ pkg, not just user-requested
279 // ones, because some generators look at tags in other
280 // packages.
281 //
282 // TODO: It would be nice if we only did this on user-requested
283 // packages. The problem is that we don't always know which
284 // other packages will need this information, and even when we
285 // do we may have already loaded the package (as a transitive
286 // dep) and might have stored pointers into it. Doing a
287 // thorough "reload" without invalidating all those pointers is
288 // a problem for another day.
289 position := p.fset.Position(c.End()) // Fset is synchronized
290 p.endLineToCommentGroup[fileLine{position.Filename, position.Line}] = c
291 }
292 }
293
294 return nil
295 }
296 if err := forEachPackageRecursive(pkgs, absorbPkg); err != nil {
297 return nil, err
298 }
299
300 return append(existingPkgs, pkgs...), nil
301 }
302
303 // alreadyLoaded figures out which of the specified patterns have already been loaded
304 // and which have not, and returns those respectively.
305 // baseCfg is an optional (may be nil) config which might be injected by tests.
306 func (p *Parser) alreadyLoaded(baseCfg *packages.Config, patterns ...string) ([]*packages.Package, []string, error) {
307 existingPkgs := make([]*packages.Package, 0, len(patterns))
308 netNewPkgs := make([]string, 0, len(patterns))
309
310 // Expand and canonicalize the requested patterns. This should be fast.
311 if pkgPaths, err := p.findPackages(baseCfg, patterns...); err != nil {
312 return nil, nil, err
313 } else {
314 for _, pkgPath := range pkgPaths {
315 if pkg := p.goPkgs[pkgPath]; pkg != nil {
316 existingPkgs = append(existingPkgs, pkg)
317 } else {
318 netNewPkgs = append(netNewPkgs, pkgPath)
319 }
320 }
321 }
322 return existingPkgs, netNewPkgs, nil
323 }
324
325 // forEachPackageRecursive will run the provided function on all of the specified
326 // packages, and on their imports recursively. Errors are accumulated and
327 // returned as via errors.Join.
328 func forEachPackageRecursive(pkgs []*packages.Package, fn func(pkg *packages.Package) error) error {
329 seen := map[string]bool{} // PkgPaths we have already visited
330 var errs []error
331 for _, pkg := range pkgs {
332 errs = append(errs, recursePackage(pkg, fn, seen)...)
333 }
334 if len(errs) > 0 {
335 return errors.Join(errs...)
336 }
337 return nil
338 }
339
340 func recursePackage(pkg *packages.Package, fn func(pkg *packages.Package) error, seen map[string]bool) []error {
341 if seen[pkg.PkgPath] {
342 return nil
343 }
344 var errs []error
345 seen[pkg.PkgPath] = true
346 if err := fn(pkg); err != nil {
347 errs = append(errs, err)
348 }
349 for _, imp := range pkg.Imports {
350 errs = append(errs, recursePackage(imp, fn, seen)...)
351 }
352 return errs
353 }
354
355 // UserRequestedPackages fetches a list of the user-imported packages.
356 func (p *Parser) UserRequestedPackages() []string {
357 // Iterate packages in a predictable order.
358 pkgPaths := make([]string, 0, len(p.userRequested))
359 for k := range p.userRequested {
360 pkgPaths = append(pkgPaths, string(k))
361 }
362 sort.Strings(pkgPaths)
363 return pkgPaths
364 }
365
366 // NewUniverse finalizes the loaded packages, searches through them for types
367 // and produces a new Universe. The returned Universe has one types.Package
368 // entry for each Go package that has been loaded, including all of their
369 // dependencies, recursively. It also has one entry, whose key is "", which
370 // represents "builtin" types.
371 func (p *Parser) NewUniverse() (types.Universe, error) {
372 u := types.Universe{}
373
374 pkgs := []*packages.Package{}
375 for _, path := range p.UserRequestedPackages() {
376 pkgs = append(pkgs, p.goPkgs[path])
377 }
378 if err := p.addPkgsToUniverse(pkgs, &u); err != nil {
379 return nil, err
380 }
381
382 return u, nil
383 }
384
385 // addCommentsToType takes any accumulated comment lines prior to obj and
386 // attaches them to the type t.
387 func (p *Parser) addCommentsToType(obj gotypes.Object, t *types.Type) {
388 t.CommentLines = p.docComment(obj.Pos())
389 t.SecondClosestCommentLines = p.priorDetachedComment(obj.Pos())
390 }
391
392 // packageDir tries to figure out the directory of the specified package.
393 func packageDir(pkg *packages.Package) (string, error) {
394 // Sometimes Module is present but has no Dir, e.g. when it is vendored.
395 if pkg.Module != nil && pkg.Module.Dir != "" {
396 // NOTE: this will not work if tests are loaded, because Go mutates the
397 // Package.PkgPath.
398 subdir := strings.TrimPrefix(pkg.PkgPath, pkg.Module.Path)
399 return filepath.Join(pkg.Module.Dir, subdir), nil
400 }
401 if len(pkg.GoFiles) > 0 {
402 return filepath.Dir(pkg.GoFiles[0]), nil
403 }
404 if len(pkg.IgnoredFiles) > 0 {
405 return filepath.Dir(pkg.IgnoredFiles[0]), nil
406 }
407 return "", fmt.Errorf("can't find package dir for %q - no module info and no Go files", pkg.PkgPath)
408 }
409
410 // addPkgsToUniverse adds the packages, and all of their deps, recursively, to
411 // the universe and (if needed) searches through them for types.
412 func (p *Parser) addPkgsToUniverse(pkgs []*packages.Package, u *types.Universe) error {
413 addOne := func(pkg *packages.Package) error {
414 if err := p.addPkgToUniverse(pkg, u); err != nil {
415 return err
416 }
417 return nil
418 }
419 if err := forEachPackageRecursive(pkgs, addOne); err != nil {
420 return err
421 }
422 return nil
423 }
424
425 // addPkgToUniverse adds one package to the universe and (if needed) searches
426 // through it for types.
427 func (p *Parser) addPkgToUniverse(pkg *packages.Package, u *types.Universe) error {
428 pkgPath := pkg.PkgPath
429 if p.fullyProcessed[pkgPath] {
430 return nil
431 }
432
433 // This will get-or-create the Package.
434 gengoPkg := u.Package(pkgPath)
435
436 if gengoPkg.Dir == "" {
437 // We're keeping this package, though we might not fully process it.
438 if vlog := klog.V(5); vlog.Enabled() {
439 why := "user-requested"
440 if !p.userRequested[pkgPath] {
441 why = "dependency"
442 }
443 vlog.Infof("addPkgToUniverse %q (%s)", pkgPath, why)
444 }
445
446 absPath := ""
447 if dir, err := packageDir(pkg); err != nil {
448 return err
449 } else {
450 absPath = dir
451 }
452
453 gengoPkg.Path = pkg.PkgPath
454 gengoPkg.Dir = absPath
455 }
456
457 // If the package was not user-requested, we can stop here.
458 if !p.userRequested[pkgPath] {
459 return nil
460 }
461
462 // Mark it as done, so we don't ever re-process it.
463 p.fullyProcessed[pkgPath] = true
464 gengoPkg.Name = pkg.Name
465
466 // For historical reasons we treat files named "doc.go" specially.
467 // TODO: It would be nice to not do this and instead treat package
468 // doc-comments as the "global" config place. This would require changing
469 // most generators and input files.
470 for _, f := range pkg.Syntax {
471 // This gets the filename for the ast.File. Iterating pkg.GoFiles is
472 // documented as unreliable.
473 pos := p.fset.Position(f.FileStart)
474 if filepath.Base(pos.Filename) == "doc.go" {
475 gengoPkg.Comments = []string{}
476 for i := range f.Comments {
477 gengoPkg.Comments = append(gengoPkg.Comments, splitLines(f.Comments[i].Text())...)
478 }
479 if f.Doc != nil {
480 gengoPkg.DocComments = splitLines(f.Doc.Text())
481 }
482 }
483 }
484
485 // Walk all the types, recursively and save them for later access.
486 s := pkg.Types.Scope()
487 for _, n := range s.Names() {
488 switch obj := s.Lookup(n).(type) {
489 case *gotypes.TypeName:
490 t := p.walkType(*u, nil, obj.Type())
491 p.addCommentsToType(obj, t)
492 case *gotypes.Func:
493 // We only care about functions, not concrete/abstract methods.
494 if obj.Type() != nil && obj.Type().(*gotypes.Signature).Recv() == nil {
495 t := p.addFunction(*u, nil, obj)
496 p.addCommentsToType(obj, t)
497 }
498 case *gotypes.Var:
499 if !obj.IsField() {
500 t := p.addVariable(*u, nil, obj)
501 p.addCommentsToType(obj, t)
502 }
503 case *gotypes.Const:
504 t := p.addConstant(*u, nil, obj)
505 p.addCommentsToType(obj, t)
506 default:
507 klog.Infof("addPkgToUniverse %q: unhandled object of type %T: %v", pkgPath, obj, obj)
508 }
509 }
510
511 // Add all of this package's imports.
512 importedPkgs := []string{}
513 for _, imp := range pkg.Imports {
514 if err := p.addPkgToUniverse(imp, u); err != nil {
515 return err
516 }
517 importedPkgs = append(importedPkgs, imp.PkgPath)
518 }
519 sort.Strings(importedPkgs)
520 u.AddImports(pkg.PkgPath, importedPkgs...)
521
522 return nil
523 }
524
525 // If the specified position has a "doc comment", return that.
526 func (p *Parser) docComment(pos token.Pos) []string {
527 // An object's doc comment always ends on the line before the object's own
528 // declaration.
529 c1 := p.priorCommentLines(pos, 1)
530 return splitLines(c1.Text()) // safe even if c1 is nil
531 }
532
533 // If there is a detached (not immediately before a declaration) comment,
534 // return that.
535 func (p *Parser) priorDetachedComment(pos token.Pos) []string {
536 // An object's doc comment always ends on the line before the object's own
537 // declaration.
538 c1 := p.priorCommentLines(pos, 1)
539
540 // Using a literal "2" here is brittle in theory (it means literally 2
541 // lines), but in practice Go code is gofmt'ed (which elides repeated blank
542 // lines), so it works.
543 var c2 *ast.CommentGroup
544 if c1 == nil {
545 c2 = p.priorCommentLines(pos, 2)
546 } else {
547 c2 = p.priorCommentLines(c1.List[0].Slash, 2)
548 }
549 return splitLines(c2.Text()) // safe even if c1 is nil
550 }
551
552 // If there's a comment block which ends nlines before pos, return it.
553 func (p *Parser) priorCommentLines(pos token.Pos, lines int) *ast.CommentGroup {
554 position := p.fset.Position(pos)
555 key := fileLine{position.Filename, position.Line - lines}
556 return p.endLineToCommentGroup[key]
557 }
558
559 func splitLines(str string) []string {
560 return strings.Split(strings.TrimRight(str, "\n"), "\n")
561 }
562
563 func goFuncNameToName(in string) types.Name {
564 name := strings.TrimPrefix(in, "func ")
565 nameParts := strings.Split(name, "(")
566 return goNameToName(nameParts[0])
567 }
568
569 func goVarNameToName(in string) types.Name {
570 nameParts := strings.Split(in, " ")
571 // nameParts[0] is "var".
572 // nameParts[2:] is the type of the variable, we ignore it for now.
573 return goNameToName(nameParts[1])
574 }
575
576 // goNameToName converts a go name string to a gengo types.Name.
577 // It operates solely on the string on a best effort basis. The name may be updated
578 // in walkType for generics.
579 func goNameToName(in string) types.Name {
580 // Detect anonymous type names. (These may have '.' characters because
581 // embedded types may have packages, so we detect them specially.)
582 if strings.HasPrefix(in, "struct{") ||
583 strings.HasPrefix(in, "<-chan") ||
584 strings.HasPrefix(in, "chan<-") ||
585 strings.HasPrefix(in, "chan ") ||
586 strings.HasPrefix(in, "func(") ||
587 strings.HasPrefix(in, "func (") ||
588 strings.HasPrefix(in, "*") ||
589 strings.HasPrefix(in, "map[") ||
590 strings.HasPrefix(in, "[") {
591 return types.Name{Name: in}
592 }
593
594 // There may be '.' characters within a generic. Temporarily remove
595 // the generic.
596 genericIndex := strings.IndexRune(in, '[')
597 if genericIndex == -1 {
598 genericIndex = len(in)
599 }
600
601 // Otherwise, if there are '.' characters present, the name has a
602 // package path in front.
603 nameParts := strings.Split(in[:genericIndex], ".")
604 name := types.Name{Name: in}
605 if n := len(nameParts); n >= 2 {
606 // The final "." is the name of the type--previous ones must
607 // have been in the package path.
608 name.Package, name.Name = strings.Join(nameParts[:n-1], "."), nameParts[n-1]
609 // Add back the generic component now that the package and type name have been separated.
610 if genericIndex != len(in) {
611 name.Name = name.Name + in[genericIndex:]
612 }
613 }
614 return name
615 }
616
617 func (p *Parser) convertSignature(u types.Universe, t *gotypes.Signature) *types.Signature {
618 signature := &types.Signature{}
619 for i := 0; i < t.Params().Len(); i++ {
620 signature.Parameters = append(signature.Parameters, &types.ParamResult{
621 Name: t.Params().At(i).Name(),
622 Type: p.walkType(u, nil, t.Params().At(i).Type()),
623 })
624 }
625 for i := 0; i < t.Results().Len(); i++ {
626 signature.Results = append(signature.Results, &types.ParamResult{
627 Name: t.Results().At(i).Name(),
628 Type: p.walkType(u, nil, t.Results().At(i).Type()),
629 })
630 }
631 if r := t.Recv(); r != nil {
632 signature.Receiver = p.walkType(u, nil, r.Type())
633 }
634 signature.Variadic = t.Variadic()
635 return signature
636 }
637
638 // walkType adds the type, and any necessary child types.
639 func (p *Parser) walkType(u types.Universe, useName *types.Name, in gotypes.Type) *types.Type {
640 // Most of the cases are underlying types of the named type.
641 name := goNameToName(in.String())
642 if useName != nil {
643 name = *useName
644 }
645
646 // Handle alias types conditionally on go1.22+.
647 // Inline this once the minimum supported version is go1.22
648 if out := p.walkAliasType(u, in); out != nil {
649 return out
650 }
651
652 switch t := in.(type) {
653 case *gotypes.Struct:
654 out := u.Type(name)
655 out.GoType = in
656 if out.Kind != types.Unknown {
657 return out
658 }
659 out.Kind = types.Struct
660 for i := 0; i < t.NumFields(); i++ {
661 f := t.Field(i)
662 m := types.Member{
663 Name: f.Name(),
664 Embedded: f.Anonymous(),
665 Tags: t.Tag(i),
666 Type: p.walkType(u, nil, f.Type()),
667 CommentLines: p.docComment(f.Pos()),
668 }
669 out.Members = append(out.Members, m)
670 }
671 return out
672 case *gotypes.Map:
673 out := u.Type(name)
674 out.GoType = in
675 if out.Kind != types.Unknown {
676 return out
677 }
678 out.Kind = types.Map
679 out.Elem = p.walkType(u, nil, t.Elem())
680 out.Key = p.walkType(u, nil, t.Key())
681 return out
682 case *gotypes.Pointer:
683 out := u.Type(name)
684 out.GoType = in
685 if out.Kind != types.Unknown {
686 return out
687 }
688 out.Kind = types.Pointer
689 out.Elem = p.walkType(u, nil, t.Elem())
690 return out
691 case *gotypes.Slice:
692 out := u.Type(name)
693 out.GoType = in
694 if out.Kind != types.Unknown {
695 return out
696 }
697 out.Kind = types.Slice
698 out.Elem = p.walkType(u, nil, t.Elem())
699 return out
700 case *gotypes.Array:
701 out := u.Type(name)
702 out.GoType = in
703 if out.Kind != types.Unknown {
704 return out
705 }
706 out.Kind = types.Array
707 out.Elem = p.walkType(u, nil, t.Elem())
708 out.Len = in.(*gotypes.Array).Len()
709 return out
710 case *gotypes.Chan:
711 out := u.Type(name)
712 out.GoType = in
713 if out.Kind != types.Unknown {
714 return out
715 }
716 out.Kind = types.Chan
717 out.Elem = p.walkType(u, nil, t.Elem())
718 // TODO: need to store direction, otherwise raw type name
719 // cannot be properly written.
720 return out
721 case *gotypes.Basic:
722 out := u.Type(types.Name{
723 Package: "", // This is a magic package name in the Universe.
724 Name: t.Name(),
725 })
726 out.GoType = in
727 if out.Kind != types.Unknown {
728 return out
729 }
730 out.Kind = types.Unsupported
731 return out
732 case *gotypes.Signature:
733 out := u.Type(name)
734 out.GoType = in
735 if out.Kind != types.Unknown {
736 return out
737 }
738 out.Kind = types.Func
739 out.Signature = p.convertSignature(u, t)
740 return out
741 case *gotypes.Interface:
742 out := u.Type(name)
743 out.GoType = in
744 if out.Kind != types.Unknown {
745 return out
746 }
747 out.Kind = types.Interface
748 t.Complete()
749 for i := 0; i < t.NumMethods(); i++ {
750 if out.Methods == nil {
751 out.Methods = map[string]*types.Type{}
752 }
753 method := t.Method(i)
754 name := goNameToName(method.String())
755 mt := p.walkType(u, &name, method.Type())
756 mt.CommentLines = p.docComment(method.Pos())
757 out.Methods[method.Name()] = mt
758 }
759 return out
760 case *gotypes.Named:
761 var out *types.Type
762 switch t.Underlying().(type) {
763 case *gotypes.Named, *gotypes.Basic, *gotypes.Map, *gotypes.Slice:
764 name := goNameToName(t.String())
765 out = u.Type(name)
766 out.GoType = in
767 if out.Kind != types.Unknown {
768 return out
769 }
770 out.Kind = types.Alias
771 out.Underlying = p.walkType(u, nil, t.Underlying())
772 case *gotypes.Struct, *gotypes.Interface:
773 name := goNameToName(t.String())
774 tpMap := map[string]*types.Type{}
775 if t.TypeParams().Len() != 0 {
776 // Remove generics, then readd them without the encoded
777 // type, e.g. Foo[T any] => Foo[T]
778 var tpNames []string
779 for i := 0; i < t.TypeParams().Len(); i++ {
780 tp := t.TypeParams().At(i)
781 tpName := tp.Obj().Name()
782 tpNames = append(tpNames, tpName)
783 tpMap[tpName] = p.walkType(u, nil, tp.Constraint())
784 }
785 name.Name = fmt.Sprintf("%s[%s]", strings.SplitN(name.Name, "[", 2)[0], strings.Join(tpNames, ","))
786 }
787
788 if out := u.Type(name); out.Kind != types.Unknown {
789 out.GoType = in
790 return out // short circuit if we've already made this.
791 }
792 out = p.walkType(u, &name, t.Underlying())
793 out.TypeParams = tpMap
794 default:
795 // gotypes package makes everything "named" with an
796 // underlying anonymous type--we remove that annoying
797 // "feature" for users. This flattens those types
798 // together.
799 name := goNameToName(t.String())
800 if out := u.Type(name); out.Kind != types.Unknown {
801 return out // short circuit if we've already made this.
802 }
803 out = p.walkType(u, &name, t.Underlying())
804 }
805 // If the underlying type didn't already add methods, add them.
806 // (Interface types will have already added methods.)
807 if len(out.Methods) == 0 {
808 for i := 0; i < t.NumMethods(); i++ {
809 if out.Methods == nil {
810 out.Methods = map[string]*types.Type{}
811 }
812 method := t.Method(i)
813 name := goNameToName(method.String())
814 mt := p.walkType(u, &name, method.Type())
815 mt.CommentLines = p.docComment(method.Pos())
816 out.Methods[method.Name()] = mt
817 }
818 }
819 return out
820 case *gotypes.TypeParam:
821 // DO NOT retrieve the type from the universe. The default type-param name is only the
822 // generic variable name. Ideally, it would be namespaced by package and struct but it is
823 // not. Thus, if we try to use the universe, we would start polluting it.
824 // e.g. if Foo[T] and Bar[T] exists, we'd mistakenly use the same type T for both.
825 return &types.Type{
826 Name: name,
827 Kind: types.TypeParam,
828 }
829 default:
830 out := u.Type(name)
831 out.GoType = in
832 if out.Kind != types.Unknown {
833 return out
834 }
835 out.Kind = types.Unsupported
836 klog.Warningf("Making unsupported type entry %q for: %#v\n", out, t)
837 return out
838 }
839 }
840
841 func (p *Parser) addFunction(u types.Universe, useName *types.Name, in *gotypes.Func) *types.Type {
842 name := goFuncNameToName(in.String())
843 if useName != nil {
844 name = *useName
845 }
846 out := u.Function(name)
847 out.Kind = types.DeclarationOf
848 out.Underlying = p.walkType(u, nil, in.Type())
849 return out
850 }
851
852 func (p *Parser) addVariable(u types.Universe, useName *types.Name, in *gotypes.Var) *types.Type {
853 name := goVarNameToName(in.String())
854 if useName != nil {
855 name = *useName
856 }
857 out := u.Variable(name)
858 out.Kind = types.DeclarationOf
859 out.Underlying = p.walkType(u, nil, in.Type())
860 return out
861 }
862
863 func (p *Parser) addConstant(u types.Universe, useName *types.Name, in *gotypes.Const) *types.Type {
864 name := goVarNameToName(in.String())
865 if useName != nil {
866 name = *useName
867 }
868 out := u.Constant(name)
869 out.Kind = types.DeclarationOf
870 out.Underlying = p.walkType(u, nil, in.Type())
871
872 var constval string
873
874 // For strings, we use `StringVal()` to get the un-truncated,
875 // un-quoted string. For other values, `.String()` is preferable to
876 // get something relatively human readable (especially since for
877 // floating point types, `ExactString()` will generate numeric
878 // expressions using `big.(*Float).Text()`.
879 switch in.Val().Kind() {
880 case constant.String:
881 constval = constant.StringVal(in.Val())
882 default:
883 constval = in.Val().String()
884 }
885
886 out.ConstValue = &constval
887 return out
888 }