| 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 | //go:build !go1.18 |
| 6 | // +build !go1.18 |
| 7 | |
| 8 | package typeparams |
| 9 | |
| 10 | import ( |
| 11 | "go/ast" |
| 12 | "go/token" |
| 13 | "go/types" |
| 14 | ) |
| 15 | |
| 16 | func unsupported() { |
| 17 | panic("type parameters are unsupported at this go version") |
| 18 | } |
| 19 | |
| 20 | // IndexListExpr is a placeholder type, as type parameters are not supported at |
| 21 | // this Go version. Its methods panic on use. |
| 22 | type IndexListExpr struct { |
| 23 | ast.Expr |
| 24 | X ast.Expr // expression |
| 25 | Lbrack token.Pos // position of "[" |
| 26 | Indices []ast.Expr // index expressions |
| 27 | Rbrack token.Pos // position of "]" |
| 28 | } |
| 29 | |
| 30 | // ForTypeSpec returns an empty field list, as type parameters on not supported |
| 31 | // at this Go version. |
| 32 | func ForTypeSpec(*ast.TypeSpec) *ast.FieldList { |
| 33 | return nil |
| 34 | } |
| 35 | |
| 36 | // ForFuncType returns an empty field list, as type parameters are not |
| 37 | // supported at this Go version. |
| 38 | func ForFuncType(*ast.FuncType) *ast.FieldList { |
| 39 | return nil |
| 40 | } |
| 41 | |
| 42 | // TypeParam is a placeholder type, as type parameters are not supported at |
| 43 | // this Go version. Its methods panic on use. |
| 44 | type TypeParam struct{ types.Type } |
| 45 | |
| 46 | func (*TypeParam) Index() int { unsupported(); return 0 } |
| 47 | func (*TypeParam) Constraint() types.Type { unsupported(); return nil } |
| 48 | func (*TypeParam) Obj() *types.TypeName { unsupported(); return nil } |
| 49 | |
| 50 | // TypeParamList is a placeholder for an empty type parameter list. |
| 51 | type TypeParamList struct{} |
| 52 | |
| 53 | func (*TypeParamList) Len() int { return 0 } |
| 54 | func (*TypeParamList) At(int) *TypeParam { unsupported(); return nil } |
| 55 | |
| 56 | // TypeList is a placeholder for an empty type list. |
| 57 | type TypeList struct{} |
| 58 | |
| 59 | func (*TypeList) Len() int { return 0 } |
| 60 | func (*TypeList) At(int) types.Type { unsupported(); return nil } |
| 61 | |
| 62 | // NewTypeParam is unsupported at this Go version, and panics. |
| 63 | func NewTypeParam(name *types.TypeName, constraint types.Type) *TypeParam { |
| 64 | unsupported() |
| 65 | return nil |
| 66 | } |
| 67 | |
| 68 | // SetTypeParamConstraint is unsupported at this Go version, and panics. |
| 69 | func SetTypeParamConstraint(tparam *TypeParam, constraint types.Type) { |
| 70 | unsupported() |
| 71 | } |
| 72 | |
| 73 | // NewSignatureType calls types.NewSignature, panicking if recvTypeParams or |
| 74 | // typeParams is non-empty. |
| 75 | func NewSignatureType(recv *types.Var, recvTypeParams, typeParams []*TypeParam, params, results *types.Tuple, variadic bool) *types.Signature { |
| 76 | if len(recvTypeParams) != 0 || len(typeParams) != 0 { |
| 77 | panic("signatures cannot have type parameters at this Go version") |
| 78 | } |
| 79 | return types.NewSignature(recv, params, results, variadic) |
| 80 | } |
| 81 | |
| 82 | // ForSignature returns an empty slice. |
| 83 | func ForSignature(*types.Signature) *TypeParamList { |
| 84 | return nil |
| 85 | } |
| 86 | |
| 87 | // RecvTypeParams returns a nil slice. |
| 88 | func RecvTypeParams(sig *types.Signature) *TypeParamList { |
| 89 | return nil |
| 90 | } |
| 91 | |
| 92 | // IsComparable returns false, as no interfaces are type-restricted at this Go |
| 93 | // version. |
| 94 | func IsComparable(*types.Interface) bool { |
| 95 | return false |
| 96 | } |
| 97 | |
| 98 | // IsMethodSet returns true, as no interfaces are type-restricted at this Go |
| 99 | // version. |
| 100 | func IsMethodSet(*types.Interface) bool { |
| 101 | return true |
| 102 | } |
| 103 | |
| 104 | // IsImplicit returns false, as no interfaces are implicit at this Go version. |
| 105 | func IsImplicit(*types.Interface) bool { |
| 106 | return false |
| 107 | } |
| 108 | |
| 109 | // MarkImplicit does nothing, because this Go version does not have implicit |
| 110 | // interfaces. |
| 111 | func MarkImplicit(*types.Interface) {} |
| 112 | |
| 113 | // ForNamed returns an empty type parameter list, as type parameters are not |
| 114 | // supported at this Go version. |
| 115 | func ForNamed(*types.Named) *TypeParamList { |
| 116 | return nil |
| 117 | } |
| 118 | |
| 119 | // SetForNamed panics if tparams is non-empty. |
| 120 | func SetForNamed(_ *types.Named, tparams []*TypeParam) { |
| 121 | if len(tparams) > 0 { |
| 122 | unsupported() |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | // NamedTypeArgs returns nil. |
| 127 | func NamedTypeArgs(*types.Named) *TypeList { |
| 128 | return nil |
| 129 | } |
| 130 | |
| 131 | // NamedTypeOrigin is the identity method at this Go version. |
| 132 | func NamedTypeOrigin(named *types.Named) types.Type { |
| 133 | return named |
| 134 | } |
| 135 | |
| 136 | // Term holds information about a structural type restriction. |
| 137 | type Term struct { |
| 138 | tilde bool |
| 139 | typ types.Type |
| 140 | } |
| 141 | |
| 142 | func (m *Term) Tilde() bool { return m.tilde } |
| 143 | func (m *Term) Type() types.Type { return m.typ } |
| 144 | func (m *Term) String() string { |
| 145 | pre := "" |
| 146 | if m.tilde { |
| 147 | pre = "~" |
| 148 | } |
| 149 | return pre + m.typ.String() |
| 150 | } |
| 151 | |
| 152 | // NewTerm is unsupported at this Go version, and panics. |
| 153 | func NewTerm(tilde bool, typ types.Type) *Term { |
| 154 | return &Term{tilde, typ} |
| 155 | } |
| 156 | |
| 157 | // Union is a placeholder type, as type parameters are not supported at this Go |
| 158 | // version. Its methods panic on use. |
| 159 | type Union struct{ types.Type } |
| 160 | |
| 161 | func (*Union) Len() int { return 0 } |
| 162 | func (*Union) Term(i int) *Term { unsupported(); return nil } |
| 163 | |
| 164 | // NewUnion is unsupported at this Go version, and panics. |
| 165 | func NewUnion(terms []*Term) *Union { |
| 166 | unsupported() |
| 167 | return nil |
| 168 | } |
| 169 | |
| 170 | // InitInstanceInfo is a noop at this Go version. |
| 171 | func InitInstanceInfo(*types.Info) {} |
| 172 | |
| 173 | // Instance is a placeholder type, as type parameters are not supported at this |
| 174 | // Go version. |
| 175 | type Instance struct { |
| 176 | TypeArgs *TypeList |
| 177 | Type types.Type |
| 178 | } |
| 179 | |
| 180 | // GetInstances returns a nil map, as type parameters are not supported at this |
| 181 | // Go version. |
| 182 | func GetInstances(info *types.Info) map[*ast.Ident]Instance { return nil } |
| 183 | |
| 184 | // Context is a placeholder type, as type parameters are not supported at |
| 185 | // this Go version. |
| 186 | type Context struct{} |
| 187 | |
| 188 | // NewContext returns a placeholder Context instance. |
| 189 | func NewContext() *Context { |
| 190 | return &Context{} |
| 191 | } |
| 192 | |
| 193 | // Instantiate is unsupported on this Go version, and panics. |
| 194 | func Instantiate(ctxt *Context, typ types.Type, targs []types.Type, validate bool) (types.Type, error) { |
| 195 | unsupported() |
| 196 | return nil, nil |
| 197 | } |
| 198 |
Members