summaryrefslogtreecommitdiff
path: root/src/go/types/typeparam.go
diff options
context:
space:
mode:
authorRob Findley <rfindley@google.com>2021-07-16 10:59:53 -0400
committerRobert Findley <rfindley@google.com>2021-07-16 23:35:10 +0000
commit521828091c73e2af67bc2210b7c94cc54076f17b (patch)
tree2c52539b076bf2e0c27061b7e2748ffd17e98b6c /src/go/types/typeparam.go
parent624d152db711cff77466b2049ae29377a110396a (diff)
downloadgo-git-521828091c73e2af67bc2210b7c94cc54076f17b.tar.gz
[dev.typeparams] go/types: move (remaining) type decls into their own files (cleanup)
This is a port of CL 332093 to go/types. A missing comment is added to named.go, and some TODOs were added to converge on the TypeParam API. Change-Id: I781a1d0d3fc6c11bb323123e954c106094d998ef Reviewed-on: https://go-review.googlesource.com/c/go/+/335040 Trust: Robert Findley <rfindley@google.com> Run-TryBot: Robert Findley <rfindley@google.com> TryBot-Result: Go Bot <gobot@golang.org> Reviewed-by: Robert Griesemer <gri@golang.org>
Diffstat (limited to 'src/go/types/typeparam.go')
-rw-r--r--src/go/types/typeparam.go72
1 files changed, 72 insertions, 0 deletions
diff --git a/src/go/types/typeparam.go b/src/go/types/typeparam.go
new file mode 100644
index 0000000000..e134508855
--- /dev/null
+++ b/src/go/types/typeparam.go
@@ -0,0 +1,72 @@
+// Copyright 2011 The Go Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style
+// license that can be found in the LICENSE file.
+
+package types
+
+import (
+ "go/token"
+ "sync/atomic"
+)
+
+// Note: This is a uint32 rather than a uint64 because the
+// respective 64 bit atomic instructions are not available
+// on all platforms.
+var lastID uint32
+
+// nextID returns a value increasing monotonically by 1 with
+// each call, starting with 1. It may be called concurrently.
+func nextID() uint64 { return uint64(atomic.AddUint32(&lastID, 1)) }
+
+// A TypeParam represents a type parameter type.
+type TypeParam struct {
+ check *Checker // for lazy type bound completion
+ id uint64 // unique id, for debugging only
+ obj *TypeName // corresponding type name
+ index int // type parameter index in source order, starting at 0
+ bound Type // *Named or *Interface; underlying type is always *Interface
+}
+
+// NewTypeParam returns a new TypeParam.
+func NewTypeParam(obj *TypeName, index int, bound Type) *TypeParam {
+ return (*Checker)(nil).newTypeParam(obj, index, bound)
+}
+
+// TODO(rfindley): this is factored slightly differently in types2.
+func (check *Checker) newTypeParam(obj *TypeName, index int, bound Type) *TypeParam {
+ assert(bound != nil)
+
+ // Always increment lastID, even if it is not used.
+ id := nextID()
+ if check != nil {
+ check.nextID++
+ id = check.nextID
+ }
+
+ typ := &TypeParam{check: check, id: id, obj: obj, index: index, bound: bound}
+ if obj.typ == nil {
+ obj.typ = typ
+ }
+ return typ
+}
+
+// TODO(rfindley): types2 to has Index and SetID. Should we add them here?
+
+func (t *TypeParam) Bound() *Interface {
+ // we may not have an interface (error reported elsewhere)
+ iface, _ := under(t.bound).(*Interface)
+ if iface == nil {
+ return &emptyInterface
+ }
+ // use the type bound position if we have one
+ pos := token.NoPos
+ if n, _ := t.bound.(*Named); n != nil {
+ pos = n.obj.pos
+ }
+ // TODO(rFindley) switch this to an unexported method on Checker.
+ computeTypeSet(t.check, pos, iface)
+ return iface
+}
+
+func (t *TypeParam) Underlying() Type { return t }
+func (t *TypeParam) String() string { return TypeString(t, nil) }