Source file src/go/types/util.go

     1  // Copyright 2023 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 contains various functionality that is
     6  // different between go/types and types2. Factoring
     7  // out this code allows more of the rest of the code
     8  // to be shared.
     9  
    10  package types
    11  
    12  import (
    13  	"go/ast"
    14  	"go/constant"
    15  	"go/token"
    16  )
    17  
    18  const isTypes2 = false
    19  
    20  // cmpPos compares the positions p and q and returns a result r as follows:
    21  //
    22  // r <  0: p is before q
    23  // r == 0: p and q are the same position (but may not be identical)
    24  // r >  0: p is after q
    25  //
    26  // If p and q are in different files, p is before q if the filename
    27  // of p sorts lexicographically before the filename of q.
    28  func cmpPos(p, q token.Pos) int { return int(p - q) }
    29  
    30  // hasDots reports whether the last argument in the call is followed by ...
    31  func hasDots(call *ast.CallExpr) bool { return call.Ellipsis.IsValid() }
    32  
    33  // dddErrPos returns the positioner for reporting an invalid ... use in a call.
    34  func dddErrPos(call *ast.CallExpr) positioner { return atPos(call.Ellipsis) }
    35  
    36  // isdddArray reports whether atyp is of the form [...]E.
    37  func isdddArray(atyp *ast.ArrayType) bool {
    38  	if atyp.Len != nil {
    39  		if ddd, _ := atyp.Len.(*ast.Ellipsis); ddd != nil && ddd.Elt == nil {
    40  			return true
    41  		}
    42  	}
    43  	return false
    44  }
    45  
    46  // argErrPos returns positioner for reporting an invalid argument count.
    47  func argErrPos(call *ast.CallExpr) positioner { return inNode(call, call.Rparen) }
    48  
    49  // startPos returns the start position of node n.
    50  func startPos(n ast.Node) token.Pos { return n.Pos() }
    51  
    52  // endPos returns the position of the first character immediately after node n.
    53  func endPos(n ast.Node) token.Pos { return n.End() }
    54  
    55  // makeFromLiteral returns the constant value for the given literal string and kind.
    56  func makeFromLiteral(lit string, kind token.Token) constant.Value {
    57  	return constant.MakeFromLiteral(lit, kind, 0)
    58  }
    59  

View as plain text