]> Cypherpunks.ru repositories - gostls13.git/blob - src/go/types/lookup.go
[dev.typeparams] all: merge master (37f9a8f) into dev.typeparams
[gostls13.git] / src / go / types / lookup.go
1 // Copyright 2013 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.
4
5 // This file implements various field and method lookup functions.
6
7 package types
8
9 import "go/token"
10
11 // LookupFieldOrMethod looks up a field or method with given package and name
12 // in T and returns the corresponding *Var or *Func, an index sequence, and a
13 // bool indicating if there were any pointer indirections on the path to the
14 // field or method. If addressable is set, T is the type of an addressable
15 // variable (only matters for method lookups).
16 //
17 // The last index entry is the field or method index in the (possibly embedded)
18 // type where the entry was found, either:
19 //
20 //      1) the list of declared methods of a named type; or
21 //      2) the list of all methods (method set) of an interface type; or
22 //      3) the list of fields of a struct type.
23 //
24 // The earlier index entries are the indices of the embedded struct fields
25 // traversed to get to the found entry, starting at depth 0.
26 //
27 // If no entry is found, a nil object is returned. In this case, the returned
28 // index and indirect values have the following meaning:
29 //
30 //      - If index != nil, the index sequence points to an ambiguous entry
31 //      (the same name appeared more than once at the same embedding level).
32 //
33 //      - If indirect is set, a method with a pointer receiver type was found
34 //      but there was no pointer on the path from the actual receiver type to
35 //      the method's formal receiver base type, nor was the receiver addressable.
36 //
37 func LookupFieldOrMethod(T Type, addressable bool, pkg *Package, name string) (obj Object, index []int, indirect bool) {
38         return (*Checker)(nil).lookupFieldOrMethod(T, addressable, pkg, name)
39 }
40
41 // Internal use of Checker.lookupFieldOrMethod: If the obj result is a method
42 // associated with a concrete (non-interface) type, the method's signature
43 // may not be fully set up. Call Checker.objDecl(obj, nil) before accessing
44 // the method's type.
45 // TODO(gri) Now that we provide the *Checker, we can probably remove this
46 // caveat by calling Checker.objDecl from lookupFieldOrMethod. Investigate.
47
48 // lookupFieldOrMethod is like the external version but completes interfaces
49 // as necessary.
50 func (check *Checker) lookupFieldOrMethod(T Type, addressable bool, pkg *Package, name string) (obj Object, index []int, indirect bool) {
51         // Methods cannot be associated to a named pointer type
52         // (spec: "The type denoted by T is called the receiver base type;
53         // it must not be a pointer or interface type and it must be declared
54         // in the same package as the method.").
55         // Thus, if we have a named pointer type, proceed with the underlying
56         // pointer type but discard the result if it is a method since we would
57         // not have found it for T (see also issue 8590).
58         if t := asNamed(T); t != nil {
59                 if p, _ := t.underlying.(*Pointer); p != nil {
60                         obj, index, indirect = check.rawLookupFieldOrMethod(p, false, pkg, name)
61                         if _, ok := obj.(*Func); ok {
62                                 return nil, nil, false
63                         }
64                         return
65                 }
66         }
67
68         return check.rawLookupFieldOrMethod(T, addressable, pkg, name)
69 }
70
71 // TODO(gri) The named type consolidation and seen maps below must be
72 //           indexed by unique keys for a given type. Verify that named
73 //           types always have only one representation (even when imported
74 //           indirectly via different packages.)
75
76 // rawLookupFieldOrMethod should only be called by lookupFieldOrMethod and missingMethod.
77 func (check *Checker) rawLookupFieldOrMethod(T Type, addressable bool, pkg *Package, name string) (obj Object, index []int, indirect bool) {
78         // WARNING: The code in this function is extremely subtle - do not modify casually!
79         //          This function and NewMethodSet should be kept in sync.
80
81         if name == "_" {
82                 return // blank fields/methods are never found
83         }
84
85         typ, isPtr := deref(T)
86
87         // *typ where typ is an interface has no methods.
88         // Be cautious: typ may be nil (issue 39634, crash #3).
89         if typ == nil || isPtr && IsInterface(typ) {
90                 return
91         }
92
93         // Start with typ as single entry at shallowest depth.
94         current := []embeddedType{{typ, nil, isPtr, false}}
95
96         // Named types that we have seen already, allocated lazily.
97         // Used to avoid endless searches in case of recursive types.
98         // Since only Named types can be used for recursive types, we
99         // only need to track those.
100         // (If we ever allow type aliases to construct recursive types,
101         // we must use type identity rather than pointer equality for
102         // the map key comparison, as we do in consolidateMultiples.)
103         var seen map[*Named]bool
104
105         // search current depth
106         for len(current) > 0 {
107                 var next []embeddedType // embedded types found at current depth
108
109                 // look for (pkg, name) in all types at current depth
110                 var tpar *_TypeParam // set if obj receiver is a type parameter
111                 for _, e := range current {
112                         typ := e.typ
113
114                         // If we have a named type, we may have associated methods.
115                         // Look for those first.
116                         if named := asNamed(typ); named != nil {
117                                 if seen[named] {
118                                         // We have seen this type before, at a more shallow depth
119                                         // (note that multiples of this type at the current depth
120                                         // were consolidated before). The type at that depth shadows
121                                         // this same type at the current depth, so we can ignore
122                                         // this one.
123                                         continue
124                                 }
125                                 if seen == nil {
126                                         seen = make(map[*Named]bool)
127                                 }
128                                 seen[named] = true
129
130                                 // look for a matching attached method
131                                 if i, m := lookupMethod(named.methods, pkg, name); m != nil {
132                                         // potential match
133                                         // caution: method may not have a proper signature yet
134                                         index = concat(e.index, i)
135                                         if obj != nil || e.multiples {
136                                                 return nil, index, false // collision
137                                         }
138                                         obj = m
139                                         indirect = e.indirect
140                                         continue // we can't have a matching field or interface method
141                                 }
142
143                                 // continue with underlying type, but only if it's not a type parameter
144                                 // TODO(gri) is this what we want to do for type parameters? (spec question)
145                                 // TODO(#45639) the error message produced as a result of skipping an
146                                 //              underlying type parameter should be improved.
147                                 typ = named.under()
148                                 if asTypeParam(typ) != nil {
149                                         continue
150                                 }
151                         }
152
153                         tpar = nil
154                         switch t := typ.(type) {
155                         case *Struct:
156                                 // look for a matching field and collect embedded types
157                                 for i, f := range t.fields {
158                                         if f.sameId(pkg, name) {
159                                                 assert(f.typ != nil)
160                                                 index = concat(e.index, i)
161                                                 if obj != nil || e.multiples {
162                                                         return nil, index, false // collision
163                                                 }
164                                                 obj = f
165                                                 indirect = e.indirect
166                                                 continue // we can't have a matching interface method
167                                         }
168                                         // Collect embedded struct fields for searching the next
169                                         // lower depth, but only if we have not seen a match yet
170                                         // (if we have a match it is either the desired field or
171                                         // we have a name collision on the same depth; in either
172                                         // case we don't need to look further).
173                                         // Embedded fields are always of the form T or *T where
174                                         // T is a type name. If e.typ appeared multiple times at
175                                         // this depth, f.typ appears multiple times at the next
176                                         // depth.
177                                         if obj == nil && f.embedded {
178                                                 typ, isPtr := deref(f.typ)
179                                                 // TODO(gri) optimization: ignore types that can't
180                                                 // have fields or methods (only Named, Struct, and
181                                                 // Interface types need to be considered).
182                                                 next = append(next, embeddedType{typ, concat(e.index, i), e.indirect || isPtr, e.multiples})
183                                         }
184                                 }
185
186                         case *Interface:
187                                 // look for a matching method
188                                 // TODO(gri) t.allMethods is sorted - use binary search
189                                 check.completeInterface(token.NoPos, t)
190                                 if i, m := lookupMethod(t.allMethods, pkg, name); m != nil {
191                                         assert(m.typ != nil)
192                                         index = concat(e.index, i)
193                                         if obj != nil || e.multiples {
194                                                 return nil, index, false // collision
195                                         }
196                                         obj = m
197                                         indirect = e.indirect
198                                 }
199
200                         case *_TypeParam:
201                                 // only consider explicit methods in the type parameter bound, not
202                                 // methods that may be common to all types in the type list.
203                                 if i, m := lookupMethod(t.Bound().allMethods, pkg, name); m != nil {
204                                         assert(m.typ != nil)
205                                         index = concat(e.index, i)
206                                         if obj != nil || e.multiples {
207                                                 return nil, index, false // collision
208                                         }
209                                         tpar = t
210                                         obj = m
211                                         indirect = e.indirect
212                                 }
213                         }
214                 }
215
216                 if obj != nil {
217                         // found a potential match
218                         // spec: "A method call x.m() is valid if the method set of (the type of) x
219                         //        contains m and the argument list can be assigned to the parameter
220                         //        list of m. If x is addressable and &x's method set contains m, x.m()
221                         //        is shorthand for (&x).m()".
222                         if f, _ := obj.(*Func); f != nil {
223                                 // determine if method has a pointer receiver
224                                 hasPtrRecv := tpar == nil && ptrRecv(f)
225                                 if hasPtrRecv && !indirect && !addressable {
226                                         return nil, nil, true // pointer/addressable receiver required
227                                 }
228                         }
229                         return
230                 }
231
232                 current = check.consolidateMultiples(next)
233         }
234
235         return nil, nil, false // not found
236 }
237
238 // embeddedType represents an embedded type
239 type embeddedType struct {
240         typ       Type
241         index     []int // embedded field indices, starting with index at depth 0
242         indirect  bool  // if set, there was a pointer indirection on the path to this field
243         multiples bool  // if set, typ appears multiple times at this depth
244 }
245
246 // consolidateMultiples collects multiple list entries with the same type
247 // into a single entry marked as containing multiples. The result is the
248 // consolidated list.
249 func (check *Checker) consolidateMultiples(list []embeddedType) []embeddedType {
250         if len(list) <= 1 {
251                 return list // at most one entry - nothing to do
252         }
253
254         n := 0                     // number of entries w/ unique type
255         prev := make(map[Type]int) // index at which type was previously seen
256         for _, e := range list {
257                 if i, found := check.lookupType(prev, e.typ); found {
258                         list[i].multiples = true
259                         // ignore this entry
260                 } else {
261                         prev[e.typ] = n
262                         list[n] = e
263                         n++
264                 }
265         }
266         return list[:n]
267 }
268
269 func (check *Checker) lookupType(m map[Type]int, typ Type) (int, bool) {
270         // fast path: maybe the types are equal
271         if i, found := m[typ]; found {
272                 return i, true
273         }
274
275         for t, i := range m {
276                 if check.identical(t, typ) {
277                         return i, true
278                 }
279         }
280
281         return 0, false
282 }
283
284 // MissingMethod returns (nil, false) if V implements T, otherwise it
285 // returns a missing method required by T and whether it is missing or
286 // just has the wrong type.
287 //
288 // For non-interface types V, or if static is set, V implements T if all
289 // methods of T are present in V. Otherwise (V is an interface and static
290 // is not set), MissingMethod only checks that methods of T which are also
291 // present in V have matching types (e.g., for a type assertion x.(T) where
292 // x is of interface type V).
293 //
294 func MissingMethod(V Type, T *Interface, static bool) (method *Func, wrongType bool) {
295         m, typ := (*Checker)(nil).missingMethod(V, T, static)
296         return m, typ != nil
297 }
298
299 // missingMethod is like MissingMethod but accepts a *Checker as
300 // receiver and an addressable flag.
301 // The receiver may be nil if missingMethod is invoked through
302 // an exported API call (such as MissingMethod), i.e., when all
303 // methods have been type-checked.
304 // If the type has the correctly named method, but with the wrong
305 // signature, the existing method is returned as well.
306 // To improve error messages, also report the wrong signature
307 // when the method exists on *V instead of V.
308 func (check *Checker) missingMethod(V Type, T *Interface, static bool) (method, wrongType *Func) {
309         check.completeInterface(token.NoPos, T)
310
311         // fast path for common case
312         if T.Empty() {
313                 return
314         }
315
316         if ityp := asInterface(V); ityp != nil {
317                 check.completeInterface(token.NoPos, ityp)
318                 // TODO(gri) allMethods is sorted - can do this more efficiently
319                 for _, m := range T.allMethods {
320                         _, f := lookupMethod(ityp.allMethods, m.pkg, m.name)
321
322                         if f == nil {
323                                 // if m is the magic method == we're ok (interfaces are comparable)
324                                 if m.name == "==" || !static {
325                                         continue
326                                 }
327                                 return m, f
328                         }
329
330                         // both methods must have the same number of type parameters
331                         ftyp := f.typ.(*Signature)
332                         mtyp := m.typ.(*Signature)
333                         if len(ftyp.tparams) != len(mtyp.tparams) {
334                                 return m, f
335                         }
336                         if len(ftyp.tparams) > 0 {
337                                 panic("internal error: method with type parameters")
338                         }
339
340                         // If the methods have type parameters we don't care whether they
341                         // are the same or not, as long as they match up. Use unification
342                         // to see if they can be made to match.
343                         // TODO(gri) is this always correct? what about type bounds?
344                         // (Alternative is to rename/subst type parameters and compare.)
345                         u := newUnifier(check, true)
346                         u.x.init(ftyp.tparams)
347                         if !u.unify(ftyp, mtyp) {
348                                 return m, f
349                         }
350                 }
351
352                 return
353         }
354
355         // A concrete type implements T if it implements all methods of T.
356         Vd, _ := deref(V)
357         Vn := asNamed(Vd)
358         for _, m := range T.allMethods {
359                 // TODO(gri) should this be calling lookupFieldOrMethod instead (and why not)?
360                 obj, _, _ := check.rawLookupFieldOrMethod(V, false, m.pkg, m.name)
361
362                 // Check if *V implements this method of T.
363                 if obj == nil {
364                         ptr := NewPointer(V)
365                         obj, _, _ = check.rawLookupFieldOrMethod(ptr, false, m.pkg, m.name)
366                         if obj != nil {
367                                 return m, obj.(*Func)
368                         }
369                 }
370
371                 // we must have a method (not a field of matching function type)
372                 f, _ := obj.(*Func)
373                 if f == nil {
374                         // if m is the magic method == and V is comparable, we're ok
375                         if m.name == "==" && Comparable(V) {
376                                 continue
377                         }
378                         return m, nil
379                 }
380
381                 // methods may not have a fully set up signature yet
382                 if check != nil {
383                         check.objDecl(f, nil)
384                 }
385
386                 // both methods must have the same number of type parameters
387                 ftyp := f.typ.(*Signature)
388                 mtyp := m.typ.(*Signature)
389                 if len(ftyp.tparams) != len(mtyp.tparams) {
390                         return m, f
391                 }
392                 if len(ftyp.tparams) > 0 {
393                         panic("internal error: method with type parameters")
394                 }
395
396                 // If V is a (instantiated) generic type, its methods are still
397                 // parameterized using the original (declaration) receiver type
398                 // parameters (subst simply copies the existing method list, it
399                 // does not instantiate the methods).
400                 // In order to compare the signatures, substitute the receiver
401                 // type parameters of ftyp with V's instantiation type arguments.
402                 // This lazily instantiates the signature of method f.
403                 if Vn != nil && len(Vn.tparams) > 0 {
404                         // Be careful: The number of type arguments may not match
405                         // the number of receiver parameters. If so, an error was
406                         // reported earlier but the length discrepancy is still
407                         // here. Exit early in this case to prevent an assertion
408                         // failure in makeSubstMap.
409                         // TODO(gri) Can we avoid this check by fixing the lengths?
410                         if len(ftyp.rparams) != len(Vn.targs) {
411                                 return
412                         }
413                         ftyp = check.subst(token.NoPos, ftyp, makeSubstMap(ftyp.rparams, Vn.targs)).(*Signature)
414                 }
415
416                 // If the methods have type parameters we don't care whether they
417                 // are the same or not, as long as they match up. Use unification
418                 // to see if they can be made to match.
419                 // TODO(gri) is this always correct? what about type bounds?
420                 // (Alternative is to rename/subst type parameters and compare.)
421                 u := newUnifier(check, true)
422                 u.x.init(ftyp.rparams)
423                 if !u.unify(ftyp, mtyp) {
424                         return m, f
425                 }
426         }
427
428         return
429 }
430
431 // assertableTo reports whether a value of type V can be asserted to have type T.
432 // It returns (nil, false) as affirmative answer. Otherwise it returns a missing
433 // method required by V and whether it is missing or just has the wrong type.
434 // The receiver may be nil if assertableTo is invoked through an exported API call
435 // (such as AssertableTo), i.e., when all methods have been type-checked.
436 // If the global constant forceStrict is set, assertions that are known to fail
437 // are not permitted.
438 func (check *Checker) assertableTo(V *Interface, T Type) (method, wrongType *Func) {
439         // no static check is required if T is an interface
440         // spec: "If T is an interface type, x.(T) asserts that the
441         //        dynamic type of x implements the interface T."
442         if asInterface(T) != nil && !forceStrict {
443                 return
444         }
445         return check.missingMethod(T, V, false)
446 }
447
448 // deref dereferences typ if it is a *Pointer and returns its base and true.
449 // Otherwise it returns (typ, false).
450 func deref(typ Type) (Type, bool) {
451         if p, _ := typ.(*Pointer); p != nil {
452                 return p.base, true
453         }
454         return typ, false
455 }
456
457 // derefStructPtr dereferences typ if it is a (named or unnamed) pointer to a
458 // (named or unnamed) struct and returns its base. Otherwise it returns typ.
459 func derefStructPtr(typ Type) Type {
460         if p := asPointer(typ); p != nil {
461                 if asStruct(p.base) != nil {
462                         return p.base
463                 }
464         }
465         return typ
466 }
467
468 // concat returns the result of concatenating list and i.
469 // The result does not share its underlying array with list.
470 func concat(list []int, i int) []int {
471         var t []int
472         t = append(t, list...)
473         return append(t, i)
474 }
475
476 // fieldIndex returns the index for the field with matching package and name, or a value < 0.
477 func fieldIndex(fields []*Var, pkg *Package, name string) int {
478         if name != "_" {
479                 for i, f := range fields {
480                         if f.sameId(pkg, name) {
481                                 return i
482                         }
483                 }
484         }
485         return -1
486 }
487
488 // lookupMethod returns the index of and method with matching package and name, or (-1, nil).
489 func lookupMethod(methods []*Func, pkg *Package, name string) (int, *Func) {
490         if name != "_" {
491                 for i, m := range methods {
492                         if m.sameId(pkg, name) {
493                                 return i, m
494                         }
495                 }
496         }
497         return -1, nil
498 }