2015-02-13 14:40:36 -05:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
package gc
|
|
|
|
|
|
2016-10-18 14:17:05 -07:00
|
|
|
// evalunsafe evaluates a package unsafe operation and returns the result.
|
|
|
|
|
func evalunsafe(n *Node) int64 {
|
|
|
|
|
switch n.Op {
|
|
|
|
|
case OALIGNOF, OSIZEOF:
|
2018-11-18 08:34:38 -08:00
|
|
|
n.Left = typecheck(n.Left, ctxExpr)
|
2016-10-18 14:17:05 -07:00
|
|
|
n.Left = defaultlit(n.Left, nil)
|
|
|
|
|
tr := n.Left.Type
|
2015-02-13 14:40:36 -05:00
|
|
|
if tr == nil {
|
2016-10-18 14:17:05 -07:00
|
|
|
return 0
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
dowidth(tr)
|
2016-10-18 14:17:05 -07:00
|
|
|
if n.Op == OALIGNOF {
|
|
|
|
|
return int64(tr.Align)
|
2016-03-10 18:23:03 -08:00
|
|
|
}
|
2016-10-18 14:17:05 -07:00
|
|
|
return tr.Width
|
2015-02-13 14:40:36 -05:00
|
|
|
|
2016-10-18 14:17:05 -07:00
|
|
|
case OOFFSETOF:
|
2015-02-13 14:40:36 -05:00
|
|
|
// must be a selector.
|
2016-10-18 14:17:05 -07:00
|
|
|
if n.Left.Op != OXDOT {
|
|
|
|
|
yyerror("invalid expression %v", n)
|
|
|
|
|
return 0
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Remember base of selector to find it back after dot insertion.
|
|
|
|
|
// Since r->left may be mutated by typechecking, check it explicitly
|
|
|
|
|
// first to track it correctly.
|
2018-11-18 08:34:38 -08:00
|
|
|
n.Left.Left = typecheck(n.Left.Left, ctxExpr)
|
2020-11-16 11:36:13 -05:00
|
|
|
sbase := n.Left.Left
|
2016-03-10 18:23:03 -08:00
|
|
|
|
2018-11-18 08:34:38 -08:00
|
|
|
n.Left = typecheck(n.Left, ctxExpr)
|
2017-10-20 11:01:43 -07:00
|
|
|
if n.Left.Type == nil {
|
|
|
|
|
return 0
|
|
|
|
|
}
|
2016-10-18 14:17:05 -07:00
|
|
|
switch n.Left.Op {
|
2015-04-01 09:38:44 -07:00
|
|
|
case ODOT, ODOTPTR:
|
2015-02-13 14:40:36 -05:00
|
|
|
break
|
|
|
|
|
case OCALLPART:
|
2016-10-18 14:17:05 -07:00
|
|
|
yyerror("invalid expression %v: argument is a method value", n)
|
|
|
|
|
return 0
|
2015-02-13 14:40:36 -05:00
|
|
|
default:
|
2016-10-18 14:17:05 -07:00
|
|
|
yyerror("invalid expression %v", n)
|
|
|
|
|
return 0
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2020-11-16 11:36:13 -05:00
|
|
|
// Sum offsets for dots until we reach sbase.
|
2016-10-18 14:17:05 -07:00
|
|
|
var v int64
|
2020-11-16 11:36:13 -05:00
|
|
|
for r := n.Left; r != sbase; r = r.Left {
|
2016-10-18 14:17:05 -07:00
|
|
|
switch r.Op {
|
2016-03-10 18:23:03 -08:00
|
|
|
case ODOTPTR:
|
|
|
|
|
// For Offsetof(s.f), s may itself be a pointer,
|
|
|
|
|
// but accessing f must not otherwise involve
|
|
|
|
|
// indirection via embedded pointer types.
|
2020-11-16 11:36:13 -05:00
|
|
|
if r.Left != sbase {
|
2016-10-18 14:17:05 -07:00
|
|
|
yyerror("invalid expression %v: selector implies indirection of embedded %v", n, r.Left)
|
|
|
|
|
return 0
|
2016-03-10 18:23:03 -08:00
|
|
|
}
|
|
|
|
|
fallthrough
|
2015-02-13 14:40:36 -05:00
|
|
|
case ODOT:
|
2016-10-18 14:17:05 -07:00
|
|
|
v += r.Xoffset
|
2015-02-13 14:40:36 -05:00
|
|
|
default:
|
2016-10-18 14:17:05 -07:00
|
|
|
Dump("unsafenmagic", n.Left)
|
|
|
|
|
Fatalf("impossible %#v node after dot insertion", r.Op)
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
}
|
2016-10-18 14:17:05 -07:00
|
|
|
return v
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|
|
|
|
|
|
2016-10-18 14:17:05 -07:00
|
|
|
Fatalf("unexpected op %v", n.Op)
|
|
|
|
|
return 0
|
2015-02-13 14:40:36 -05:00
|
|
|
}
|