LEFT | RIGHT |
1 // Copyright 2011 The Go Authors. All rights reserved. | 1 // Copyright 2011 The Go Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style | 2 // Use of this source code is governed by a BSD-style |
3 // license that can be found in the LICENSE file. | 3 // license that can be found in the LICENSE file. |
4 | 4 |
5 // PACKAGE UNDER CONSTRUCTION. | 5 // PACKAGE UNDER CONSTRUCTION. ANY AND ALL PARTS MAY CHANGE. |
6 // The types package declares the types used to represent Go types. | 6 // The types package declares the types used to represent Go types. |
7 // | 7 // |
8 package types | 8 package types |
9 | 9 |
10 import "go/ast" | 10 import "go/ast" |
11 | 11 |
12 | 12 |
13 // All types implement the Type interface. | 13 // All types implement the Type interface. |
14 type Type interface { | 14 type Type interface { |
15 isType() | 15 isType() |
16 } | 16 } |
17 | 17 |
18 | 18 |
19 // All concrete types embed ImplementsType which | 19 // All concrete types embed ImplementsType which |
20 // ensures that all types implement the Type interface. | 20 // ensures that all types implement the Type interface. |
21 type ImplementsType struct{} | 21 type ImplementsType struct{} |
22 | 22 |
23 func (t *ImplementsType) isType() {} | 23 func (t *ImplementsType) isType() {} |
24 | 24 |
25 | 25 |
26 // A Basic represents a (unnamed) basic type. | 26 // A Basic represents a (unnamed) basic type. |
27 // Predeclared types have a basic type as underlying type. | |
28 type Basic struct { | 27 type Basic struct { |
29 ImplementsType | 28 ImplementsType |
30 // TODO(gri) need a field specifying the exact basic type | 29 // TODO(gri) need a field specifying the exact basic type |
31 } | 30 } |
32 | 31 |
33 | 32 |
34 // An Array represents an array type [Len]Elt. | 33 // An Array represents an array type [Len]Elt. |
35 type Array struct { | 34 type Array struct { |
36 ImplementsType | 35 ImplementsType |
37 Len uint64 | 36 Len uint64 |
38 Elt Type | 37 Elt Type |
39 } | 38 } |
40 | 39 |
41 | 40 |
42 // A Slice represents a slice type []Elt or ...Elt. | 41 // A Slice represents a slice type []Elt. |
43 type Slice struct { | 42 type Slice struct { |
44 ImplementsType | 43 ImplementsType |
45 » Ellipsis bool // true for "...Elt" types | 44 » Elt Type |
46 » Elt Type | |
47 } | 45 } |
48 | 46 |
49 | 47 |
50 // A Struct represents a struct type struct{...}. | 48 // A Struct represents a struct type struct{...}. |
51 type Struct struct { | 49 type Struct struct { |
52 ImplementsType | 50 ImplementsType |
53 » Fields *ast.Scope // always present | 51 » // TODO(gri) need to remember fields. |
54 } | 52 } |
55 | 53 |
56 | 54 |
57 // A Pointer represents a pointer type *Base. | 55 // A Pointer represents a pointer type *Base. |
58 type Pointer struct { | 56 type Pointer struct { |
59 ImplementsType | 57 ImplementsType |
60 Base Type | 58 Base Type |
61 } | 59 } |
62 | 60 |
63 | 61 |
64 // A Func represents a function type func(...) (...). | 62 // A Func represents a function type func(...) (...). |
65 type Func struct { | 63 type Func struct { |
66 ImplementsType | 64 ImplementsType |
67 » Params *ast.Scope // always present | 65 » IsVariadic bool |
| 66 » // TODO(gri) need to remember parameters. |
68 } | 67 } |
69 | 68 |
70 | 69 |
71 // An Interface represents an interface type interface{...}. | 70 // An Interface represents an interface type interface{...}. |
72 type Interface struct { | 71 type Interface struct { |
73 ImplementsType | 72 ImplementsType |
74 » Methods *ast.Scope // always present | 73 » // TODO(gri) need to remember methods. |
75 } | 74 } |
76 | 75 |
77 | 76 |
78 // A Map represents a map type map[Key]Elt. | 77 // A Map represents a map type map[Key]Elt. |
79 type Map struct { | 78 type Map struct { |
80 ImplementsType | 79 ImplementsType |
81 Key, Elt Type | 80 Key, Elt Type |
82 } | 81 } |
83 | 82 |
84 | 83 |
85 // A Chan represents a channel type chan Elt, <-chan Elt, or chan<-Elt. | 84 // A Chan represents a channel type chan Elt, <-chan Elt, or chan<-Elt. |
86 type Chan struct { | 85 type Chan struct { |
87 ImplementsType | 86 ImplementsType |
88 Dir ast.ChanDir | 87 Dir ast.ChanDir |
89 Elt Type | 88 Elt Type |
90 } | 89 } |
91 | 90 |
92 | 91 |
93 // A Name represents a named type as declared in a type declaration. | 92 // A Name represents a named type as declared in a type declaration. |
94 type Name struct { | 93 type Name struct { |
95 ImplementsType | 94 ImplementsType |
96 Underlying Type // nil if not fully declared | 95 Underlying Type // nil if not fully declared |
97 Obj *ast.Object // corresponding declared object | 96 Obj *ast.Object // corresponding declared object |
98 » Scope *ast.Scope // associated methods (and fields, if any); or ni
l | 97 » // TODO(gri) need to remember fields and methods. |
| 98 } |
| 99 |
| 100 |
| 101 // If typ is a pointer type, Deref returns the pointer's base type; |
| 102 // otherwise it returns typ. |
| 103 func Deref(typ Type) Type { |
| 104 » if typ, ok := typ.(*Pointer); ok { |
| 105 » » return typ.Base |
| 106 » } |
| 107 » return typ |
99 } | 108 } |
100 | 109 |
101 | 110 |
102 // Underlying returns the underlying type of a type. | 111 // Underlying returns the underlying type of a type. |
103 func Underlying(typ Type) Type { | 112 func Underlying(typ Type) Type { |
104 » if typ, isName := typ.(*Name); isName { | 113 » if typ, ok := typ.(*Name); ok { |
105 » » return typ.Underlying | 114 » » utyp := typ.Underlying |
| 115 » » if _, ok := utyp.(*Basic); ok { |
| 116 » » » return typ |
| 117 » » } |
| 118 » » return utyp |
| 119 |
106 } | 120 } |
107 return typ | 121 return typ |
108 } | 122 } |
LEFT | RIGHT |