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  // argErrPos returns positioner for reporting an invalid argument count.
    37  func argErrPos(call *ast.CallExpr) positioner { return inNode(call, call.Rparen) }
    38  
    39  // startPos returns the start position of node n.
    40  func startPos(n ast.Node) token.Pos { return n.Pos() }
    41  
    42  // endPos returns the position of the first character immediately after node n.
    43  func endPos(n ast.Node) token.Pos { return n.End() }
    44  
    45  // makeFromLiteral returns the constant value for the given literal string and kind.
    46  func makeFromLiteral(lit string, kind token.Token) constant.Value {
    47  	return constant.MakeFromLiteral(lit, kind, 0)
    48  }
    49  

View as plain text