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  // isdddArray reports whether atyp is of the form [...]E.
    40  func isdddArray(atyp *syntax.ArrayType) bool { return atyp.Len == nil }
    41  
    42  // argErrPos returns the node (poser) for reporting an invalid argument count.
    43  func argErrPos(call *syntax.CallExpr) *syntax.CallExpr { return call }
    44  
    45  // ExprString returns a string representation of x.
    46  func ExprString(x syntax.Node) string { return syntax.String(x) }
    47  
    48  // startPos returns the start position of node n.
    49  func startPos(n syntax.Node) syntax.Pos { return syntax.StartPos(n) }
    50  
    51  // endPos returns the position of the first character immediately after node n.
    52  func endPos(n syntax.Node) syntax.Pos { return syntax.EndPos(n) }
    53  
    54  // inNode is a dummy function returning pos.
    55  func inNode(_ syntax.Node, pos syntax.Pos) syntax.Pos { return pos }
    56  
    57  // makeFromLiteral returns the constant value for the given literal string and kind.
    58  func makeFromLiteral(lit string, kind syntax.LitKind) constant.Value {
    59  	return constant.MakeFromLiteral(lit, kind2tok[kind], 0)
    60  }
    61  
    62  var kind2tok = [...]token.Token{
    63  	syntax.IntLit:    token.INT,
    64  	syntax.FloatLit:  token.FLOAT,
    65  	syntax.ImagLit:   token.IMAG,
    66  	syntax.RuneLit:   token.CHAR,
    67  	syntax.StringLit: token.STRING,
    68  }
    69  
    70  // unTilde returns the operand of ~x and true, or x and false.
    71  func unTilde(x syntax.Expr) (syntax.Expr, bool) {
    72  	if op, _ := x.(*syntax.Operation); op != nil && op.Op == syntax.Tilde {
    73  		return op.X, true
    74  	}
    75  	return x, false
    76  }
    77  
    78  // splitOr returns the operands of x | y, or nil, nil.
    79  func splitOr(e syntax.Expr) (x, y syntax.Expr) {
    80  	if op, _ := e.(*syntax.Operation); op != nil && op.Op == syntax.Or {
    81  		return op.X, op.Y
    82  	}
    83  	return nil, nil
    84  }
    85  
    86  // newIdent returns a new identifier with the given position and name.
    87  func newIdent(pos syntax.Pos, name string) *syntax.Name {
    88  	return syntax.NewName(pos, name)
    89  }
    90  
    91  // If x represents an index expression, unpackIndexExpr returns the indexed expression
    92  // and, if unpackParams is set, its indices. If unpackParams is not set, indices is nil.
    93  // If x is not an index expression, unpackIndexExpr returns (nil, nil).
    94  func unpackIndexExpr(x syntax.Expr, unpackParams bool) (indexed syntax.Expr, indices []syntax.Expr) {
    95  	if ix, _ := x.(*syntax.IndexExpr); ix != nil {
    96  		indexed = ix.X
    97  		if unpackParams {
    98  			indices = syntax.UnpackListExpr(ix.Index)
    99  		}
   100  	}
   101  	return
   102  }
   103  

View as plain text