Debian Codegraph @

internal/typeparams/genericfeatures/features.go

raw symbols SCIP
1 // Copyright 2021 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 // The genericfeatures package provides utilities for detecting usage of
6 // generic programming in Go packages.
7 package genericfeaturesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams/genericfeatures`/
8
9 import (
10 "go/astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/"
11 "go/typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/"
12 "stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/"
13
14 "golang.org/x/tools/go/ast/inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/"
15 )
16
17 // Features is a set of flags reporting which features of generic Go code a
18 // package uses, or 0.
19 type Featuresscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams/genericfeatures`/Features# int
20
21 const (
22 // GenericTypeDecls indicates whether the package declares types with type
23 // parameters.
24 GenericTypeDeclsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams/genericfeatures`/GenericTypeDecls. Featuresscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams/genericfeatures`/Features# = 1 << iota
25
26 // GenericFuncDecls indicates whether the package declares functions with
27 // type parameters.
28 GenericFuncDeclsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams/genericfeatures`/GenericFuncDecls.
29
30 // EmbeddedTypeSets indicates whether the package declares interfaces that
31 // contain structural type restrictions, i.e. are not fully described by
32 // their method sets.
33 EmbeddedTypeSetsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams/genericfeatures`/EmbeddedTypeSets.
34
35 // TypeInstantiation indicates whether the package instantiates any generic
36 // types.
37 TypeInstantiationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams/genericfeatures`/TypeInstantiation.
38
39 // FuncInstantiation indicates whether the package instantiates any generic
40 // functions.
41 FuncInstantiationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams/genericfeatures`/FuncInstantiation.
42 )
43
44 func (f Featuresscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams/genericfeatures`/Features#) Stringscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams/genericfeatures`/Features#String().() string {
45 var feats []string
46 if f&GenericTypeDeclsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams/genericfeatures`/GenericTypeDecls. != 0 {
47 feats = append(feats, "typeDecl")
48 }
49 if f&GenericFuncDeclsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams/genericfeatures`/GenericFuncDecls. != 0 {
50 feats = append(feats, "funcDecl")
51 }
52 if f&EmbeddedTypeSetsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams/genericfeatures`/EmbeddedTypeSets. != 0 {
53 feats = append(feats, "typeSet")
54 }
55 if f&TypeInstantiationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams/genericfeatures`/TypeInstantiation. != 0 {
56 feats = append(feats, "typeInstance")
57 }
58 if f&FuncInstantiationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams/genericfeatures`/FuncInstantiation. != 0 {
59 feats = append(feats, "funcInstance")
60 }
61 return "features{" + stringsscip-go gomod github.com/golang/go/src go1.25.0 strings/.Joinscip-go gomod github.com/golang/go/src go1.25.0 strings/Join().(feats, ",") + "}"
62 }
63
64 // ForPackage computes which generic features are used directly by the
65 // package being analyzed.
66 func ForPackagescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams/genericfeatures`/ForPackage().(inspect *inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/.Inspectorscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#, info *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Infoscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#) Featuresscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams/genericfeatures`/Features# {
67 nodeFilter := []astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#{
68 (*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.FuncTypescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#)(nil),
69 (*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.InterfaceTypescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/InterfaceType#)(nil),
70 (*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.ImportSpecscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/ImportSpec#)(nil),
71 (*astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.TypeSpecscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/TypeSpec#)(nil),
72 }
73
74 var direct Featuresscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams/genericfeatures`/Features#
75
76 inspect.Preorderscip-go gomod golang.org/x/tools . `golang.org/x/tools/go/ast/inspector`/Inspector#Preorder().(nodeFilter, func(node astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.Nodescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/Node#) {
77 switch n := node.(type) {
78 case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.FuncTypescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#:
79 if tparams := n.TypeParamsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/FuncType#TypeParams.; tparams != nil {
80 direct |= GenericFuncDeclsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams/genericfeatures`/GenericFuncDecls.
81 }
82 case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.InterfaceTypescip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/InterfaceType#:
83 tv := info.Typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#Types.[n] // may be zero
84 if iface, _ := tv.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/TypeAndValue#Type..(*typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Interfacescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Interface#); iface != nil && !iface.IsMethodSetscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Interface#IsMethodSet().() {
85 direct |= EmbeddedTypeSetsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams/genericfeatures`/EmbeddedTypeSets.
86 }
87 case *astscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/.TypeSpecscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/TypeSpec#:
88 if tparams := n.TypeParamsscip-go gomod github.com/golang/go/src go1.25.0 `go/ast`/TypeSpec#TypeParams.; tparams != nil {
89 direct |= GenericTypeDeclsscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams/genericfeatures`/GenericTypeDecls.
90 }
91 }
92 })
93
94 for _, inst := range info.Instancesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Info#Instances. {
95 switch typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Unaliasscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Unalias().(inst.Typescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Instance#Type.).(type) {
96 case *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Namedscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Named#:
97 direct |= TypeInstantiationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams/genericfeatures`/TypeInstantiation.
98 case *typesscip-go gomod github.com/golang/go/src go1.25.0 `go/types`/.Signaturescip-go gomod github.com/golang/go/src go1.25.0 `go/types`/Signature#:
99 direct |= FuncInstantiationscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/typeparams/genericfeatures`/FuncInstantiation.
100 }
101 }
102 return direct
103 }