Source file src/go/types/methodlist.go

     1  // Copyright 2022 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  package types
     6  
     7  import "sync"
     8  
     9  // methodList holds a list of methods that may be lazily resolved by a provided
    10  // resolution method.
    11  type methodList struct {
    12  	methods []*Func
    13  
    14  	// guards synchronizes the instantiation of lazy methods. For lazy method
    15  	// lists, guards is non-nil and of the length passed to newLazyMethodList.
    16  	// For non-lazy method lists, guards is nil.
    17  	guards *[]sync.Once
    18  }
    19  
    20  // newMethodList creates a non-lazy method list holding the given methods.
    21  func newMethodList(methods []*Func) *methodList {
    22  	return &methodList{methods: methods}
    23  }
    24  
    25  // newLazyMethodList creates a lazy method list of the given length. Methods
    26  // may be resolved lazily for a given index by providing a resolver function.
    27  func newLazyMethodList(length int) *methodList {
    28  	guards := make([]sync.Once, length)
    29  	return &methodList{
    30  		methods: make([]*Func, length),
    31  		guards:  &guards,
    32  	}
    33  }
    34  
    35  // isLazy reports whether the receiver is a lazy method list.
    36  func (l *methodList) isLazy() bool {
    37  	return l != nil && l.guards != nil
    38  }
    39  
    40  // Add appends a method to the method list if not not already present. Add
    41  // panics if the receiver is lazy.
    42  func (l *methodList) Add(m *Func) {
    43  	assert(!l.isLazy())
    44  	if i, _ := lookupMethod(l.methods, m.pkg, m.name, false); i < 0 {
    45  		l.methods = append(l.methods, m)
    46  	}
    47  }
    48  
    49  // Lookup looks up the method identified by pkg and name in the receiver.
    50  // Lookup panics if the receiver is lazy. If foldCase is true, method names
    51  // are considered equal if they are equal with case folding.
    52  func (l *methodList) Lookup(pkg *Package, name string, foldCase bool) (int, *Func) {
    53  	assert(!l.isLazy())
    54  	if l == nil {
    55  		return -1, nil
    56  	}
    57  	return lookupMethod(l.methods, pkg, name, foldCase)
    58  }
    59  
    60  // Len returns the length of the method list.
    61  func (l *methodList) Len() int {
    62  	if l == nil {
    63  		return 0
    64  	}
    65  	return len(l.methods)
    66  }
    67  
    68  // At returns the i'th method of the method list. At panics if i is out of
    69  // bounds, or if the receiver is lazy and resolve is nil.
    70  func (l *methodList) At(i int, resolve func() *Func) *Func {
    71  	if !l.isLazy() {
    72  		return l.methods[i]
    73  	}
    74  	assert(resolve != nil)
    75  	(*l.guards)[i].Do(func() {
    76  		l.methods[i] = resolve()
    77  	})
    78  	return l.methods[i]
    79  }
    80  

View as plain text