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