1 // Copyright 2011 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.
5 // Package types declares the types used to represent Go types
6 // (UNDER CONSTRUCTION). ANY AND ALL PARTS MAY CHANGE.
15 // All types implement the Type interface.
20 // All concrete types embed ImplementsType which
21 // ensures that all types implement the Type interface.
22 type ImplementsType struct{}
24 func (t *ImplementsType) isType() {}
26 // A Bad type is a non-nil placeholder type when we don't know a type.
29 Msg string // for better error reporting/debugging
32 // A Basic represents a (unnamed) basic type.
35 // TODO(gri) need a field specifying the exact basic type
38 // An Array represents an array type [Len]Elt.
45 // A Slice represents a slice type []Elt.
51 // A Struct represents a struct type struct{...}.
52 // Anonymous fields are represented by objects with empty names.
55 Fields ObjList // struct fields; or nil
56 Tags []string // corresponding tags; or nil
57 // TODO(gri) This type needs some rethinking:
58 // - at the moment anonymous fields are marked with "" object names,
59 // and their names have to be reconstructed
60 // - there is no scope for fast lookup (but the parser creates one)
63 // A Pointer represents a pointer type *Base.
69 // A Func represents a function type func(...) (...).
70 // Unnamed parameters are represented by objects with empty names.
73 Recv *ast.Object // nil if not a method
74 Params ObjList // (incoming) parameters from left to right; or nil
75 Results ObjList // (outgoing) results from left to right; or nil
76 IsVariadic bool // true if the last parameter's type is of the form ...T
79 // An Interface represents an interface type interface{...}.
80 type Interface struct {
82 Methods ObjList // interface methods sorted by name; or nil
85 // A Map represents a map type map[Key]Elt.
91 // A Chan represents a channel type chan Elt, <-chan Elt, or chan<-Elt.
98 // A Name represents a named type as declared in a type declaration.
101 Underlying Type // nil if not fully declared
102 Obj *ast.Object // corresponding declared object
103 // TODO(gri) need to remember fields and methods.
106 // If typ is a pointer type, Deref returns the pointer's base type;
107 // otherwise it returns typ.
108 func Deref(typ Type) Type {
109 if typ, ok := typ.(*Pointer); ok {
115 // Underlying returns the underlying type of a type.
116 func Underlying(typ Type) Type {
117 if typ, ok := typ.(*Name); ok {
118 utyp := typ.Underlying
119 if _, ok := utyp.(*Basic); !ok {
122 // the underlying type of a type name referring
123 // to an (untyped) basic type is the basic type
129 // An ObjList represents an ordered (in some fashion) list of objects.
130 type ObjList []*ast.Object
132 // ObjList implements sort.Interface.
133 func (list ObjList) Len() int { return len(list) }
134 func (list ObjList) Less(i, j int) bool { return list[i].Name < list[j].Name }
135 func (list ObjList) Swap(i, j int) { list[i], list[j] = list[j], list[i] }
137 // Sort sorts an object list by object name.
138 func (list ObjList) Sort() { sort.Sort(list) }
140 // identicalTypes returns true if both lists a and b have the
141 // same length and corresponding objects have identical types.
142 func identicalTypes(a, b ObjList) bool {
143 if len(a) == len(b) {
144 for i, x := range a {
146 if !Identical(x.Type.(Type), y.Type.(Type)) {
155 // Identical returns true if two types are identical.
156 func Identical(x, y Type) bool {
161 switch x := x.(type) {
163 // A Bad type is always identical to any other type
164 // (to avoid spurious follow-up errors).
168 if y, ok := y.(*Basic); ok {
169 panic("unimplemented")
174 // Two array types are identical if they have identical element types
175 // and the same array length.
176 if y, ok := y.(*Array); ok {
177 return x.Len == y.Len && Identical(x.Elt, y.Elt)
181 // Two slice types are identical if they have identical element types.
182 if y, ok := y.(*Slice); ok {
183 return Identical(x.Elt, y.Elt)
187 // Two struct types are identical if they have the same sequence of fields,
188 // and if corresponding fields have the same names, and identical types,
189 // and identical tags. Two anonymous fields are considered to have the same
190 // name. Lower-case field names from different packages are always different.
191 if y, ok := y.(*Struct); ok {
192 // TODO(gri) handle structs from different packages
193 if identicalTypes(x.Fields, y.Fields) {
194 for i, f := range x.Fields {
196 if f.Name != g.Name || x.Tags[i] != y.Tags[i] {
205 // Two pointer types are identical if they have identical base types.
206 if y, ok := y.(*Pointer); ok {
207 return Identical(x.Base, y.Base)
211 // Two function types are identical if they have the same number of parameters
212 // and result values, corresponding parameter and result types are identical,
213 // and either both functions are variadic or neither is. Parameter and result
214 // names are not required to match.
215 if y, ok := y.(*Func); ok {
216 return identicalTypes(x.Params, y.Params) &&
217 identicalTypes(x.Results, y.Results) &&
218 x.IsVariadic == y.IsVariadic
222 // Two interface types are identical if they have the same set of methods with
223 // the same names and identical function types. Lower-case method names from
224 // different packages are always different. The order of the methods is irrelevant.
225 if y, ok := y.(*Interface); ok {
226 return identicalTypes(x.Methods, y.Methods) // methods are sorted
230 // Two map types are identical if they have identical key and value types.
231 if y, ok := y.(*Map); ok {
232 return Identical(x.Key, y.Key) && Identical(x.Elt, y.Elt)
236 // Two channel types are identical if they have identical value types
237 // and the same direction.
238 if y, ok := y.(*Chan); ok {
239 return x.Dir == y.Dir && Identical(x.Elt, y.Elt)
243 // Two named types are identical if their type names originate
244 // in the same type declaration.
245 if y, ok := y.(*Name); ok {
246 return x.Obj == y.Obj ||
247 // permit bad objects to be equal to avoid
249 x.Obj != nil && x.Obj.Kind == ast.Bad ||
250 y.Obj != nil && y.Obj.Kind == ast.Bad