...
Source file
src/go/types/alias.go
1
2
3
4
5
6
7 package types
8
9 import "fmt"
10
11
12
13
14
15
16
17 type Alias struct {
18 obj *TypeName
19 fromRHS Type
20 actual Type
21 }
22
23
24
25 func NewAlias(obj *TypeName, rhs Type) *Alias {
26 alias := (*Checker)(nil).newAlias(obj, rhs)
27
28 unalias(alias)
29 return alias
30 }
31
32 func (a *Alias) Obj() *TypeName { return a.obj }
33 func (a *Alias) Underlying() Type { return unalias(a).Underlying() }
34 func (a *Alias) String() string { return TypeString(a, nil) }
35
36
37
38
39
40
41
42 func Unalias(t Type) Type {
43 if a0, _ := t.(*Alias); a0 != nil {
44 return unalias(a0)
45 }
46 return t
47 }
48
49 func unalias(a0 *Alias) Type {
50 if a0.actual != nil {
51 return a0.actual
52 }
53 var t Type
54 for a := a0; a != nil; a, _ = t.(*Alias) {
55 t = a.fromRHS
56 }
57 if t == nil {
58 panic(fmt.Sprintf("non-terminated alias %s", a0.obj.name))
59 }
60 a0.actual = t
61 return t
62 }
63
64
65
66 func asNamed(t Type) *Named {
67 n, _ := Unalias(t).(*Named)
68 return n
69 }
70
71
72
73 func (check *Checker) newAlias(obj *TypeName, rhs Type) *Alias {
74 assert(rhs != nil)
75 a := &Alias{obj, rhs, nil}
76 if obj.typ == nil {
77 obj.typ = a
78 }
79
80
81 if check != nil {
82 check.needsCleanup(a)
83 }
84
85 return a
86 }
87
88 func (a *Alias) cleanup() {
89 Unalias(a)
90 }
91
View as plain text