Left: | ||
Right: |
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. |
rsc
2011/04/07 16:27:19
Is the intent to expand this? If so would the isT
gri
2011/04/08 02:41:32
For now this is just here to ensure that we only s
| |
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 |
rsc
2011/04/07 16:27:19
The ... is a property of the function type, not a
gri
2011/04/08 02:41:32
I am not convinced. ... types behave likes slices
rsc
2011/04/08 03:25:44
Right. These are arguments for making it a proper
gri
2011/04/08 04:08:59
ah yes. the assignment property makes it obvious.
| |
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. |
rsc
2011/04/07 16:27:19
I'm surprised to see the ast mixed in here.
I thin
gri
2011/04/08 02:41:32
It's a placeholder. Removed for now.
| |
54 » // TODO(gri) need to remember order of fields for | |
55 » // struct type comparisons. | |
56 } | 52 } |
57 | 53 |
58 | 54 |
59 // A Pointer represents a pointer type *Base. | 55 // A Pointer represents a pointer type *Base. |
60 type Pointer struct { | 56 type Pointer struct { |
61 ImplementsType | 57 ImplementsType |
62 Base Type | 58 Base Type |
rsc
2011/04/07 16:27:19
can this be Elt?
Reflect uses the same name for a
gri
2011/04/08 02:41:32
It's really not an element. It's base type in the
rsc
2011/04/08 03:25:44
No, just a would be nice. Not a big deal.
| |
63 } | 59 } |
64 | 60 |
65 | 61 |
66 // A Func represents a function type func(...) (...). | 62 // A Func represents a function type func(...) (...). |
67 type Func struct { | 63 type Func struct { |
68 ImplementsType | 64 ImplementsType |
69 » Params *ast.Scope // always present | 65 » IsVariadic bool |
rsc
2011/04/07 16:27:19
This is even more surprising as a scope, since
the
gri
2011/04/08 02:41:32
There's a TODO below. This was a placeholder. remo
| |
70 » // TODO(gri) need to remember parameter order, | 66 » // TODO(gri) need to remember parameters. |
71 » // number of results, receiver, etc. | |
72 } | 67 } |
73 | 68 |
74 | 69 |
75 // An Interface represents an interface type interface{...}. | 70 // An Interface represents an interface type interface{...}. |
76 type Interface struct { | 71 type Interface struct { |
77 ImplementsType | 72 ImplementsType |
78 » Methods *ast.Scope // always present | 73 » // TODO(gri) need to remember methods. |
rsc
2011/04/07 16:27:19
Again.
gri
2011/04/08 02:41:32
Done.
| |
79 } | 74 } |
80 | 75 |
81 | 76 |
82 // A Map represents a map type map[Key]Elt. | 77 // A Map represents a map type map[Key]Elt. |
83 type Map struct { | 78 type Map struct { |
84 ImplementsType | 79 ImplementsType |
85 Key, Elt Type | 80 Key, Elt Type |
86 } | 81 } |
87 | 82 |
88 | 83 |
89 // 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. |
90 type Chan struct { | 85 type Chan struct { |
91 ImplementsType | 86 ImplementsType |
92 Dir ast.ChanDir | 87 Dir ast.ChanDir |
93 Elt Type | 88 Elt Type |
94 } | 89 } |
95 | 90 |
96 | 91 |
97 // 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. |
98 type Name struct { | 93 type Name struct { |
99 ImplementsType | 94 ImplementsType |
100 Underlying Type // nil if not fully declared | 95 Underlying Type // nil if not fully declared |
101 Obj *ast.Object // corresponding declared object | 96 Obj *ast.Object // corresponding declared object |
102 » Scope *ast.Scope // associated methods (and fields, if any); or ni l | 97 » // TODO(gri) need to remember fields and methods. |
rsc
2011/04/07 16:27:19
Again.
gri
2011/04/08 02:41:32
Done.
| |
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 | |
103 } | 108 } |
104 | 109 |
105 | 110 |
106 // Underlying returns the underlying type of a type. | 111 // Underlying returns the underlying type of a type. |
107 func Underlying(typ Type) Type { | 112 func Underlying(typ Type) Type { |
108 » if typ, isName := typ.(*Name); isName { | 113 » if typ, ok := typ.(*Name); ok { |
109 » » return typ.Underlying | 114 » » utyp := typ.Underlying |
115 » » if _, ok := utyp.(*Basic); ok { | |
116 » » » return typ | |
117 » » } | |
118 » » return utyp | |
119 | |
110 } | 120 } |
111 return typ | 121 return typ |
112 } | 122 } |
LEFT | RIGHT |