Commit 75da700d authored by Cuong Manh Le's avatar Cuong Manh Le Committed by Daniel Martí

cmd/compile: consistently use strlit to access constants string values

Passes toolstash-check.

Change-Id: Ieaef20b7649787727b69469f93ffc942022bc079
Reviewed-on: https://go-review.googlesource.com/c/go/+/195198
Run-TryBot: Cuong Manh Le <cuong.manhle.vn@gmail.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: default avatarMatthew Dempsky <mdempsky@google.com>
parent 142c002e
......@@ -612,7 +612,7 @@ func evconst(n *Node) {
var strs []string
i2 := i1
for i2 < len(s) && Isconst(s[i2], CTSTR) {
strs = append(strs, s[i2].Val().U.(string))
strs = append(strs, strlit(s[i2]))
i2++
}
......@@ -635,7 +635,7 @@ func evconst(n *Node) {
switch nl.Type.Etype {
case TSTRING:
if Isconst(nl, CTSTR) {
setintconst(n, int64(len(nl.Val().U.(string))))
setintconst(n, int64(len(strlit(nl))))
}
case TARRAY:
if !hascallchan(nl) {
......
......@@ -762,7 +762,7 @@ func (p *noder) sum(x syntax.Expr) *Node {
n := p.expr(x)
if Isconst(n, CTSTR) && n.Sym == nil {
nstr = n
chunks = append(chunks, nstr.Val().U.(string))
chunks = append(chunks, strlit(nstr))
}
for i := len(adds) - 1; i >= 0; i-- {
......@@ -772,12 +772,12 @@ func (p *noder) sum(x syntax.Expr) *Node {
if Isconst(r, CTSTR) && r.Sym == nil {
if nstr != nil {
// Collapse r into nstr instead of adding to n.
chunks = append(chunks, r.Val().U.(string))
chunks = append(chunks, strlit(r))
continue
}
nstr = r
chunks = append(chunks, nstr.Val().U.(string))
chunks = append(chunks, strlit(nstr))
} else {
if len(chunks) > 1 {
nstr.SetVal(Val{U: strings.Join(chunks, "")})
......
......@@ -1017,7 +1017,7 @@ func (o *Order) expr(n, lhs *Node) *Node {
haslit := false
for _, n1 := range n.List.Slice() {
hasbyte = hasbyte || n1.Op == OBYTES2STR
haslit = haslit || n1.Op == OLITERAL && len(n1.Val().U.(string)) != 0
haslit = haslit || n1.Op == OLITERAL && len(strlit(n1)) != 0
}
if haslit && hasbyte {
......
......@@ -211,7 +211,7 @@ func (s *InitSchedule) staticassign(l *Node, r *Node) bool {
case OSTR2BYTES:
if l.Class() == PEXTERN && r.Left.Op == OLITERAL {
sval := r.Left.Val().U.(string)
sval := strlit(r.Left)
slicebytes(l, sval, len(sval))
return true
}
......
......@@ -2336,7 +2336,7 @@ func (s *state) expr(n *Node) *ssa.Value {
// Replace "abc"[1] with 'b'.
// Delayed until now because "abc"[1] is not an ideal constant.
// See test/fixedbugs/issue11370.go.
return s.newValue0I(ssa.OpConst8, types.Types[TUINT8], int64(int8(n.Left.Val().U.(string)[n.Right.Int64()])))
return s.newValue0I(ssa.OpConst8, types.Types[TUINT8], int64(int8(strlit(n.Left)[n.Right.Int64()])))
}
a := s.expr(n.Left)
i := s.expr(n.Right)
......
......@@ -1043,8 +1043,8 @@ func typecheck1(n *Node, top int) (res *Node) {
yyerror("invalid %s index %v (index must be non-negative)", why, n.Right)
} else if t.IsArray() && x >= t.NumElem() {
yyerror("invalid array index %v (out of bounds for %d-element array)", n.Right, t.NumElem())
} else if Isconst(n.Left, CTSTR) && x >= int64(len(n.Left.Val().U.(string))) {
yyerror("invalid string index %v (out of bounds for %d-byte string)", n.Right, len(n.Left.Val().U.(string)))
} else if Isconst(n.Left, CTSTR) && x >= int64(len(strlit(n.Left))) {
yyerror("invalid string index %v (out of bounds for %d-byte string)", n.Right, len(strlit(n.Left)))
} else if n.Right.Val().U.(*Mpint).Cmp(maxintval[TINT]) > 0 {
yyerror("invalid %s index %v (index too large)", why, n.Right)
}
......@@ -2148,8 +2148,8 @@ func checksliceindex(l *Node, r *Node, tp *types.Type) bool {
} else if tp != nil && tp.NumElem() >= 0 && r.Int64() > tp.NumElem() {
yyerror("invalid slice index %v (out of bounds for %d-element array)", r, tp.NumElem())
return false
} else if Isconst(l, CTSTR) && r.Int64() > int64(len(l.Val().U.(string))) {
yyerror("invalid slice index %v (out of bounds for %d-byte string)", r, len(l.Val().U.(string)))
} else if Isconst(l, CTSTR) && r.Int64() > int64(len(strlit(l))) {
yyerror("invalid slice index %v (out of bounds for %d-byte string)", r, len(strlit(l)))
return false
} else if r.Val().U.(*Mpint).Cmp(maxintval[TINT]) > 0 {
yyerror("invalid slice index %v (index too large)", r)
......@@ -3409,7 +3409,7 @@ func stringtoruneslit(n *Node) *Node {
}
var l []*Node
s := n.Left.Val().U.(string)
s := strlit(n.Left)
i := 0
for _, r := range s {
l = append(l, nod(OKEY, nodintconst(int64(i)), nodintconst(int64(r))))
......
......@@ -1054,7 +1054,7 @@ opswitch:
yyerror("index out of bounds")
}
} else if Isconst(n.Left, CTSTR) {
n.SetBounded(bounded(r, int64(len(n.Left.Val().U.(string)))))
n.SetBounded(bounded(r, int64(len(strlit(n.Left)))))
if Debug['m'] != 0 && n.Bounded() && !Isconst(n.Right, CTINT) {
Warn("index bounds check elided")
}
......@@ -1389,7 +1389,7 @@ opswitch:
case OSTR2BYTES:
s := n.Left
if Isconst(s, CTSTR) {
sc := s.Val().U.(string)
sc := strlit(s)
// Allocate a [n]byte of the right size.
t := types.NewArray(types.Types[TUINT8], int64(len(sc)))
......@@ -1792,7 +1792,7 @@ func walkprint(nn *Node, init *Nodes) *Node {
for i := 0; i < len(s); {
var strs []string
for i < len(s) && Isconst(s[i], CTSTR) {
strs = append(strs, s[i].Val().U.(string))
strs = append(strs, strlit(s[i]))
i++
}
if len(strs) > 0 {
......@@ -1861,7 +1861,7 @@ func walkprint(nn *Node, init *Nodes) *Node {
case TSTRING:
cs := ""
if Isconst(n, CTSTR) {
cs = n.Val().U.(string)
cs = strlit(n)
}
switch cs {
case " ":
......@@ -2510,7 +2510,7 @@ func addstr(n *Node, init *Nodes) *Node {
sz := int64(0)
for _, n1 := range n.List.Slice() {
if n1.Op == OLITERAL {
sz += int64(len(n1.Val().U.(string)))
sz += int64(len(strlit(n1)))
}
}
......@@ -3350,7 +3350,7 @@ func walkcompareString(n *Node, init *Nodes) *Node {
// Length-only checks are ok, though.
maxRewriteLen = 0
}
if s := cs.Val().U.(string); len(s) <= maxRewriteLen {
if s := strlit(cs); len(s) <= maxRewriteLen {
if len(s) > 0 {
ncs = safeexpr(ncs, init)
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment