Source file src/cmd/compile/internal/types2/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 types2
    11  
    12  import (
    13  	"cmd/compile/internal/syntax"
    14  	"go/constant"
    15  	"go/token"
    16  )
    17  
    18  const isTypes2 = true
    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 syntax.Pos) int { return p.Cmp(q) }
    29  
    30  // hasDots reports whether the last argument in the call is followed by ...
    31  func hasDots(call *syntax.CallExpr) bool { return call.HasDots }
    32  
    33  // dddErrPos returns the node (poser) for reporting an invalid ... use in a call.
    34  func dddErrPos(call *syntax.CallExpr) *syntax.CallExpr {
    35  	// TODO(gri) should use "..." instead of call position
    36  	return call
    37  }
    38  
    39  // argErrPos returns the node (poser) for reportign an invalid argument count.
    40  func argErrPos(call *syntax.CallExpr) *syntax.CallExpr { return call }
    41  
    42  // ExprString returns a string representation of x.
    43  func ExprString(x syntax.Node) string { return syntax.String(x) }
    44  
    45  // startPos returns the start position of node n.
    46  func startPos(n syntax.Node) syntax.Pos { return syntax.StartPos(n) }
    47  
    48  // endPos returns the position of the first character immediately after node n.
    49  func endPos(n syntax.Node) syntax.Pos { return syntax.EndPos(n) }
    50  
    51  // makeFromLiteral returns the constant value for the given literal string and kind.
    52  func makeFromLiteral(lit string, kind syntax.LitKind) constant.Value {
    53  	return constant.MakeFromLiteral(lit, kind2tok[kind], 0)
    54  }
    55  
    56  var kind2tok = [...]token.Token{
    57  	syntax.IntLit:    token.INT,
    58  	syntax.FloatLit:  token.FLOAT,
    59  	syntax.ImagLit:   token.IMAG,
    60  	syntax.RuneLit:   token.CHAR,
    61  	syntax.StringLit: token.STRING,
    62  }
    63  

View as plain text