...
Run Format

Source file src/go/types/lookup.go

Documentation: go/types

     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  // LookupFieldOrMethod looks up a field or method with given package and name
    10  // in T and returns the corresponding *Var or *Func, an index sequence, and a
    11  // bool indicating if there were any pointer indirections on the path to the
    12  // field or method. If addressable is set, T is the type of an addressable
    13  // variable (only matters for method lookups).
    14  //
    15  // The last index entry is the field or method index in the (possibly embedded)
    16  // type where the entry was found, either:
    17  //
    18  //	1) the list of declared methods of a named type; or
    19  //	2) the list of all methods (method set) of an interface type; or
    20  //	3) the list of fields of a struct type.
    21  //
    22  // The earlier index entries are the indices of the anonymous struct fields
    23  // traversed to get to the found entry, starting at depth 0.
    24  //
    25  // If no entry is found, a nil object is returned. In this case, the returned
    26  // index and indirect values have the following meaning:
    27  //
    28  //	- If index != nil, the index sequence points to an ambiguous entry
    29  //	(the same name appeared more than once at the same embedding level).
    30  //
    31  //	- If indirect is set, a method with a pointer receiver type was found
    32  //      but there was no pointer on the path from the actual receiver type to
    33  //	the method's formal receiver base type, nor was the receiver addressable.
    34  //
    35  func LookupFieldOrMethod(T Type, addressable bool, pkg *Package, name string) (obj Object, index []int, indirect bool) {
    36  	// Methods cannot be associated to a named pointer type
    37  	// (spec: "The type denoted by T is called the receiver base type;
    38  	// it must not be a pointer or interface type and it must be declared
    39  	// in the same package as the method.").
    40  	// Thus, if we have a named pointer type, proceed with the underlying
    41  	// pointer type but discard the result if it is a method since we would
    42  	// not have found it for T (see also issue 8590).
    43  	if t, _ := T.(*Named); t != nil {
    44  		if p, _ := t.underlying.(*Pointer); p != nil {
    45  			obj, index, indirect = lookupFieldOrMethod(p, false, pkg, name)
    46  			if _, ok := obj.(*Func); ok {
    47  				return nil, nil, false
    48  			}
    49  			return
    50  		}
    51  	}
    52  
    53  	return lookupFieldOrMethod(T, addressable, pkg, name)
    54  }
    55  
    56  // TODO(gri) The named type consolidation and seen maps below must be
    57  //           indexed by unique keys for a given type. Verify that named
    58  //           types always have only one representation (even when imported
    59  //           indirectly via different packages.)
    60  
    61  func lookupFieldOrMethod(T Type, addressable bool, pkg *Package, name string) (obj Object, index []int, indirect bool) {
    62  	// WARNING: The code in this function is extremely subtle - do not modify casually!
    63  	//          This function and NewMethodSet should be kept in sync.
    64  
    65  	if name == "_" {
    66  		return // blank fields/methods are never found
    67  	}
    68  
    69  	typ, isPtr := deref(T)
    70  
    71  	// *typ where typ is an interface has no methods.
    72  	if isPtr && IsInterface(typ) {
    73  		return
    74  	}
    75  
    76  	// Start with typ as single entry at shallowest depth.
    77  	current := []embeddedType{{typ, nil, isPtr, false}}
    78  
    79  	// Named types that we have seen already, allocated lazily.
    80  	// Used to avoid endless searches in case of recursive types.
    81  	// Since only Named types can be used for recursive types, we
    82  	// only need to track those.
    83  	// (If we ever allow type aliases to construct recursive types,
    84  	// we must use type identity rather than pointer equality for
    85  	// the map key comparison, as we do in consolidateMultiples.)
    86  	var seen map[*Named]bool
    87  
    88  	// search current depth
    89  	for len(current) > 0 {
    90  		var next []embeddedType // embedded types found at current depth
    91  
    92  		// look for (pkg, name) in all types at current depth
    93  		for _, e := range current {
    94  			typ := e.typ
    95  
    96  			// If we have a named type, we may have associated methods.
    97  			// Look for those first.
    98  			if named, _ := typ.(*Named); named != nil {
    99  				if seen[named] {
   100  					// We have seen this type before, at a more shallow depth
   101  					// (note that multiples of this type at the current depth
   102  					// were consolidated before). The type at that depth shadows
   103  					// this same type at the current depth, so we can ignore
   104  					// this one.
   105  					continue
   106  				}
   107  				if seen == nil {
   108  					seen = make(map[*Named]bool)
   109  				}
   110  				seen[named] = true
   111  
   112  				// look for a matching attached method
   113  				if i, m := lookupMethod(named.methods, pkg, name); m != nil {
   114  					// potential match
   115  					assert(m.typ != nil)
   116  					index = concat(e.index, i)
   117  					if obj != nil || e.multiples {
   118  						return nil, index, false // collision
   119  					}
   120  					obj = m
   121  					indirect = e.indirect
   122  					continue // we can't have a matching field or interface method
   123  				}
   124  
   125  				// continue with underlying type
   126  				typ = named.underlying
   127  			}
   128  
   129  			switch t := typ.(type) {
   130  			case *Struct:
   131  				// look for a matching field and collect embedded types
   132  				for i, f := range t.fields {
   133  					if f.sameId(pkg, name) {
   134  						assert(f.typ != nil)
   135  						index = concat(e.index, i)
   136  						if obj != nil || e.multiples {
   137  							return nil, index, false // collision
   138  						}
   139  						obj = f
   140  						indirect = e.indirect
   141  						continue // we can't have a matching interface method
   142  					}
   143  					// Collect embedded struct fields for searching the next
   144  					// lower depth, but only if we have not seen a match yet
   145  					// (if we have a match it is either the desired field or
   146  					// we have a name collision on the same depth; in either
   147  					// case we don't need to look further).
   148  					// Embedded fields are always of the form T or *T where
   149  					// T is a type name. If e.typ appeared multiple times at
   150  					// this depth, f.typ appears multiple times at the next
   151  					// depth.
   152  					if obj == nil && f.anonymous {
   153  						typ, isPtr := deref(f.typ)
   154  						// TODO(gri) optimization: ignore types that can't
   155  						// have fields or methods (only Named, Struct, and
   156  						// Interface types need to be considered).
   157  						next = append(next, embeddedType{typ, concat(e.index, i), e.indirect || isPtr, e.multiples})
   158  					}
   159  				}
   160  
   161  			case *Interface:
   162  				// look for a matching method
   163  				// TODO(gri) t.allMethods is sorted - use binary search
   164  				if i, m := lookupMethod(t.allMethods, pkg, name); m != nil {
   165  					assert(m.typ != nil)
   166  					index = concat(e.index, i)
   167  					if obj != nil || e.multiples {
   168  						return nil, index, false // collision
   169  					}
   170  					obj = m
   171  					indirect = e.indirect
   172  				}
   173  			}
   174  		}
   175  
   176  		if obj != nil {
   177  			// found a potential match
   178  			// spec: "A method call x.m() is valid if the method set of (the type of) x
   179  			//        contains m and the argument list can be assigned to the parameter
   180  			//        list of m. If x is addressable and &x's method set contains m, x.m()
   181  			//        is shorthand for (&x).m()".
   182  			if f, _ := obj.(*Func); f != nil && ptrRecv(f) && !indirect && !addressable {
   183  				return nil, nil, true // pointer/addressable receiver required
   184  			}
   185  			return
   186  		}
   187  
   188  		current = consolidateMultiples(next)
   189  	}
   190  
   191  	return nil, nil, false // not found
   192  }
   193  
   194  // embeddedType represents an embedded type
   195  type embeddedType struct {
   196  	typ       Type
   197  	index     []int // embedded field indices, starting with index at depth 0
   198  	indirect  bool  // if set, there was a pointer indirection on the path to this field
   199  	multiples bool  // if set, typ appears multiple times at this depth
   200  }
   201  
   202  // consolidateMultiples collects multiple list entries with the same type
   203  // into a single entry marked as containing multiples. The result is the
   204  // consolidated list.
   205  func consolidateMultiples(list []embeddedType) []embeddedType {
   206  	if len(list) <= 1 {
   207  		return list // at most one entry - nothing to do
   208  	}
   209  
   210  	n := 0                     // number of entries w/ unique type
   211  	prev := make(map[Type]int) // index at which type was previously seen
   212  	for _, e := range list {
   213  		if i, found := lookupType(prev, e.typ); found {
   214  			list[i].multiples = true
   215  			// ignore this entry
   216  		} else {
   217  			prev[e.typ] = n
   218  			list[n] = e
   219  			n++
   220  		}
   221  	}
   222  	return list[:n]
   223  }
   224  
   225  func lookupType(m map[Type]int, typ Type) (int, bool) {
   226  	// fast path: maybe the types are equal
   227  	if i, found := m[typ]; found {
   228  		return i, true
   229  	}
   230  
   231  	for t, i := range m {
   232  		if Identical(t, typ) {
   233  			return i, true
   234  		}
   235  	}
   236  
   237  	return 0, false
   238  }
   239  
   240  // MissingMethod returns (nil, false) if V implements T, otherwise it
   241  // returns a missing method required by T and whether it is missing or
   242  // just has the wrong type.
   243  //
   244  // For non-interface types V, or if static is set, V implements T if all
   245  // methods of T are present in V. Otherwise (V is an interface and static
   246  // is not set), MissingMethod only checks that methods of T which are also
   247  // present in V have matching types (e.g., for a type assertion x.(T) where
   248  // x is of interface type V).
   249  //
   250  func MissingMethod(V Type, T *Interface, static bool) (method *Func, wrongType bool) {
   251  	// fast path for common case
   252  	if T.Empty() {
   253  		return
   254  	}
   255  
   256  	// TODO(gri) Consider using method sets here. Might be more efficient.
   257  
   258  	if ityp, _ := V.Underlying().(*Interface); ityp != nil {
   259  		// TODO(gri) allMethods is sorted - can do this more efficiently
   260  		for _, m := range T.allMethods {
   261  			_, obj := lookupMethod(ityp.allMethods, m.pkg, m.name)
   262  			switch {
   263  			case obj == nil:
   264  				if static {
   265  					return m, false
   266  				}
   267  			case !Identical(obj.Type(), m.typ):
   268  				return m, true
   269  			}
   270  		}
   271  		return
   272  	}
   273  
   274  	// A concrete type implements T if it implements all methods of T.
   275  	for _, m := range T.allMethods {
   276  		obj, _, _ := lookupFieldOrMethod(V, false, m.pkg, m.name)
   277  
   278  		f, _ := obj.(*Func)
   279  		if f == nil {
   280  			return m, false
   281  		}
   282  
   283  		if !Identical(f.typ, m.typ) {
   284  			return m, true
   285  		}
   286  	}
   287  
   288  	return
   289  }
   290  
   291  // assertableTo reports whether a value of type V can be asserted to have type T.
   292  // It returns (nil, false) as affirmative answer. Otherwise it returns a missing
   293  // method required by V and whether it is missing or just has the wrong type.
   294  func assertableTo(V *Interface, T Type) (method *Func, wrongType bool) {
   295  	// no static check is required if T is an interface
   296  	// spec: "If T is an interface type, x.(T) asserts that the
   297  	//        dynamic type of x implements the interface T."
   298  	if _, ok := T.Underlying().(*Interface); ok && !strict {
   299  		return
   300  	}
   301  	return MissingMethod(T, V, false)
   302  }
   303  
   304  // deref dereferences typ if it is a *Pointer and returns its base and true.
   305  // Otherwise it returns (typ, false).
   306  func deref(typ Type) (Type, bool) {
   307  	if p, _ := typ.(*Pointer); p != nil {
   308  		return p.base, true
   309  	}
   310  	return typ, false
   311  }
   312  
   313  // derefStructPtr dereferences typ if it is a (named or unnamed) pointer to a
   314  // (named or unnamed) struct and returns its base. Otherwise it returns typ.
   315  func derefStructPtr(typ Type) Type {
   316  	if p, _ := typ.Underlying().(*Pointer); p != nil {
   317  		if _, ok := p.base.Underlying().(*Struct); ok {
   318  			return p.base
   319  		}
   320  	}
   321  	return typ
   322  }
   323  
   324  // concat returns the result of concatenating list and i.
   325  // The result does not share its underlying array with list.
   326  func concat(list []int, i int) []int {
   327  	var t []int
   328  	t = append(t, list...)
   329  	return append(t, i)
   330  }
   331  
   332  // fieldIndex returns the index for the field with matching package and name, or a value < 0.
   333  func fieldIndex(fields []*Var, pkg *Package, name string) int {
   334  	if name != "_" {
   335  		for i, f := range fields {
   336  			if f.sameId(pkg, name) {
   337  				return i
   338  			}
   339  		}
   340  	}
   341  	return -1
   342  }
   343  
   344  // lookupMethod returns the index of and method with matching package and name, or (-1, nil).
   345  func lookupMethod(methods []*Func, pkg *Package, name string) (int, *Func) {
   346  	if name != "_" {
   347  		for i, m := range methods {
   348  			if m.sameId(pkg, name) {
   349  				return i, m
   350  			}
   351  		}
   352  	}
   353  	return -1, nil
   354  }
   355  

View as plain text