mirror of
https://github.com/golang/go.git
synced 2025-12-08 06:10:04 +00:00
This automated CL adds type assertions on the true branches of
n.Op() equality tests, to redeclare n with a more specific type, when
it is safe to do so. (That is, when n is not reassigned with a more
general type, when n is not reassigned and then used outside the
scope, and so on.) All the "unsafe" times that the automated tool
would avoid have been removed or rewritten in earlier CLs, so that
after this CL and the next one, which removes the use of ir.Nod,
every use of the Left, Right, and so on methods is done using concrete
types, never the Node interface.
Having done that, the CL locks in the progress by deleting many of
the access methods, including Left, SetLeft and so on, from the
Node interface.
There are still uses of Name, Func, Sym, some of the tracking
bits, and a few other miscellaneous fields, but all the main access
methods are gone from the Node interface. The others will be cleaned
up in smaller CLs.
Passes buildall w/ toolstash -cmp.
[git-generate]
cd src/cmd/compile/internal/gc
rf 'typeassert {
import "cmd/compile/internal/ir"
var n ir.Node
n.Op() == ir.OADD -> n.(*ir.BinaryExpr)
n.Op() == ir.OADDR -> n.(*ir.AddrExpr)
n.Op() == ir.OADDSTR -> n.(*ir.AddStringExpr)
n.Op() == ir.OALIGNOF -> n.(*ir.UnaryExpr)
n.Op() == ir.OAND -> n.(*ir.BinaryExpr)
n.Op() == ir.OANDAND -> n.(*ir.LogicalExpr)
n.Op() == ir.OANDNOT -> n.(*ir.BinaryExpr)
n.Op() == ir.OAPPEND -> n.(*ir.CallExpr)
n.Op() == ir.OARRAYLIT -> n.(*ir.CompLitExpr)
n.Op() == ir.OAS -> n.(*ir.AssignStmt)
n.Op() == ir.OAS2 -> n.(*ir.AssignListStmt)
n.Op() == ir.OAS2DOTTYPE -> n.(*ir.AssignListStmt)
n.Op() == ir.OAS2FUNC -> n.(*ir.AssignListStmt)
n.Op() == ir.OAS2MAPR -> n.(*ir.AssignListStmt)
n.Op() == ir.OAS2RECV -> n.(*ir.AssignListStmt)
n.Op() == ir.OASOP -> n.(*ir.AssignOpStmt)
n.Op() == ir.OBITNOT -> n.(*ir.UnaryExpr)
n.Op() == ir.OBLOCK -> n.(*ir.BlockStmt)
n.Op() == ir.OBREAK -> n.(*ir.BranchStmt)
n.Op() == ir.OBYTES2STR -> n.(*ir.ConvExpr)
n.Op() == ir.OBYTES2STRTMP -> n.(*ir.ConvExpr)
n.Op() == ir.OCALL -> n.(*ir.CallExpr)
n.Op() == ir.OCALLFUNC -> n.(*ir.CallExpr)
n.Op() == ir.OCALLINTER -> n.(*ir.CallExpr)
n.Op() == ir.OCALLMETH -> n.(*ir.CallExpr)
n.Op() == ir.OCALLPART -> n.(*ir.CallPartExpr)
n.Op() == ir.OCAP -> n.(*ir.UnaryExpr)
n.Op() == ir.OCASE -> n.(*ir.CaseStmt)
n.Op() == ir.OCFUNC -> n.(*ir.UnaryExpr)
n.Op() == ir.OCHECKNIL -> n.(*ir.UnaryExpr)
n.Op() == ir.OCLOSE -> n.(*ir.UnaryExpr)
n.Op() == ir.OCOMPLEX -> n.(*ir.BinaryExpr)
n.Op() == ir.OCOMPLIT -> n.(*ir.CompLitExpr)
n.Op() == ir.OCONTINUE -> n.(*ir.BranchStmt)
n.Op() == ir.OCONV -> n.(*ir.ConvExpr)
n.Op() == ir.OCONVIFACE -> n.(*ir.ConvExpr)
n.Op() == ir.OCONVNOP -> n.(*ir.ConvExpr)
n.Op() == ir.OCOPY -> n.(*ir.BinaryExpr)
n.Op() == ir.ODCL -> n.(*ir.Decl)
n.Op() == ir.ODCLCONST -> n.(*ir.Decl)
n.Op() == ir.ODCLFUNC -> n.(*ir.Func)
n.Op() == ir.ODCLTYPE -> n.(*ir.Decl)
n.Op() == ir.ODEFER -> n.(*ir.GoDeferStmt)
n.Op() == ir.ODELETE -> n.(*ir.CallExpr)
n.Op() == ir.ODEREF -> n.(*ir.StarExpr)
n.Op() == ir.ODIV -> n.(*ir.BinaryExpr)
n.Op() == ir.ODOT -> n.(*ir.SelectorExpr)
n.Op() == ir.ODOTINTER -> n.(*ir.SelectorExpr)
n.Op() == ir.ODOTMETH -> n.(*ir.SelectorExpr)
n.Op() == ir.ODOTPTR -> n.(*ir.SelectorExpr)
n.Op() == ir.ODOTTYPE -> n.(*ir.TypeAssertExpr)
n.Op() == ir.ODOTTYPE2 -> n.(*ir.TypeAssertExpr)
n.Op() == ir.OEFACE -> n.(*ir.BinaryExpr)
n.Op() == ir.OEQ -> n.(*ir.BinaryExpr)
n.Op() == ir.OFALL -> n.(*ir.BranchStmt)
n.Op() == ir.OFOR -> n.(*ir.ForStmt)
n.Op() == ir.OFORUNTIL -> n.(*ir.ForStmt)
n.Op() == ir.OGE -> n.(*ir.BinaryExpr)
n.Op() == ir.OGETG -> n.(*ir.CallExpr)
n.Op() == ir.OGO -> n.(*ir.GoDeferStmt)
n.Op() == ir.OGOTO -> n.(*ir.BranchStmt)
n.Op() == ir.OGT -> n.(*ir.BinaryExpr)
n.Op() == ir.OIDATA -> n.(*ir.UnaryExpr)
n.Op() == ir.OIF -> n.(*ir.IfStmt)
n.Op() == ir.OIMAG -> n.(*ir.UnaryExpr)
n.Op() == ir.OINDEX -> n.(*ir.IndexExpr)
n.Op() == ir.OINDEXMAP -> n.(*ir.IndexExpr)
n.Op() == ir.OINLCALL -> n.(*ir.InlinedCallExpr)
n.Op() == ir.OINLMARK -> n.(*ir.InlineMarkStmt)
n.Op() == ir.OITAB -> n.(*ir.UnaryExpr)
n.Op() == ir.OKEY -> n.(*ir.KeyExpr)
n.Op() == ir.OLABEL -> n.(*ir.LabelStmt)
n.Op() == ir.OLE -> n.(*ir.BinaryExpr)
n.Op() == ir.OLEN -> n.(*ir.UnaryExpr)
n.Op() == ir.OLSH -> n.(*ir.BinaryExpr)
n.Op() == ir.OLT -> n.(*ir.BinaryExpr)
n.Op() == ir.OMAKE -> n.(*ir.CallExpr)
n.Op() == ir.OMAKECHAN -> n.(*ir.MakeExpr)
n.Op() == ir.OMAKEMAP -> n.(*ir.MakeExpr)
n.Op() == ir.OMAKESLICE -> n.(*ir.MakeExpr)
n.Op() == ir.OMAKESLICECOPY -> n.(*ir.MakeExpr)
n.Op() == ir.OMAPLIT -> n.(*ir.CompLitExpr)
n.Op() == ir.OMETHEXPR -> n.(*ir.MethodExpr)
n.Op() == ir.OMOD -> n.(*ir.BinaryExpr)
n.Op() == ir.OMUL -> n.(*ir.BinaryExpr)
n.Op() == ir.ONAME -> n.(*ir.Name)
n.Op() == ir.ONE -> n.(*ir.BinaryExpr)
n.Op() == ir.ONEG -> n.(*ir.UnaryExpr)
n.Op() == ir.ONEW -> n.(*ir.UnaryExpr)
n.Op() == ir.ONEWOBJ -> n.(*ir.UnaryExpr)
n.Op() == ir.ONIL -> n.(*ir.NilExpr)
n.Op() == ir.ONOT -> n.(*ir.UnaryExpr)
n.Op() == ir.OOFFSETOF -> n.(*ir.UnaryExpr)
n.Op() == ir.OOR -> n.(*ir.BinaryExpr)
n.Op() == ir.OOROR -> n.(*ir.LogicalExpr)
n.Op() == ir.OPACK -> n.(*ir.PkgName)
n.Op() == ir.OPANIC -> n.(*ir.UnaryExpr)
n.Op() == ir.OPAREN -> n.(*ir.ParenExpr)
n.Op() == ir.OPLUS -> n.(*ir.UnaryExpr)
n.Op() == ir.OPRINT -> n.(*ir.CallExpr)
n.Op() == ir.OPRINTN -> n.(*ir.CallExpr)
n.Op() == ir.OPTRLIT -> n.(*ir.AddrExpr)
n.Op() == ir.ORANGE -> n.(*ir.RangeStmt)
n.Op() == ir.OREAL -> n.(*ir.UnaryExpr)
n.Op() == ir.ORECOVER -> n.(*ir.CallExpr)
n.Op() == ir.ORECV -> n.(*ir.UnaryExpr)
n.Op() == ir.ORESULT -> n.(*ir.ResultExpr)
n.Op() == ir.ORETJMP -> n.(*ir.BranchStmt)
n.Op() == ir.ORETURN -> n.(*ir.ReturnStmt)
n.Op() == ir.ORSH -> n.(*ir.BinaryExpr)
n.Op() == ir.ORUNES2STR -> n.(*ir.ConvExpr)
n.Op() == ir.ORUNESTR -> n.(*ir.ConvExpr)
n.Op() == ir.OSELECT -> n.(*ir.SelectStmt)
n.Op() == ir.OSELRECV2 -> n.(*ir.AssignListStmt)
n.Op() == ir.OSEND -> n.(*ir.SendStmt)
n.Op() == ir.OSIZEOF -> n.(*ir.UnaryExpr)
n.Op() == ir.OSLICE -> n.(*ir.SliceExpr)
n.Op() == ir.OSLICE3 -> n.(*ir.SliceExpr)
n.Op() == ir.OSLICE3ARR -> n.(*ir.SliceExpr)
n.Op() == ir.OSLICEARR -> n.(*ir.SliceExpr)
n.Op() == ir.OSLICEHEADER -> n.(*ir.SliceHeaderExpr)
n.Op() == ir.OSLICELIT -> n.(*ir.CompLitExpr)
n.Op() == ir.OSLICESTR -> n.(*ir.SliceExpr)
n.Op() == ir.OSPTR -> n.(*ir.UnaryExpr)
n.Op() == ir.OSTR2BYTES -> n.(*ir.ConvExpr)
n.Op() == ir.OSTR2BYTESTMP -> n.(*ir.ConvExpr)
n.Op() == ir.OSTR2RUNES -> n.(*ir.ConvExpr)
n.Op() == ir.OSTRUCTLIT -> n.(*ir.CompLitExpr)
n.Op() == ir.OSUB -> n.(*ir.BinaryExpr)
n.Op() == ir.OSWITCH -> n.(*ir.SwitchStmt)
n.Op() == ir.OTYPESW -> n.(*ir.TypeSwitchGuard)
n.Op() == ir.OVARDEF -> n.(*ir.UnaryExpr)
n.Op() == ir.OVARKILL -> n.(*ir.UnaryExpr)
n.Op() == ir.OVARLIVE -> n.(*ir.UnaryExpr)
n.Op() == ir.OXDOT -> n.(*ir.SelectorExpr)
n.Op() == ir.OXOR -> n.(*ir.BinaryExpr)
}
'
cd ../ir
rf '
rm \
Node.SetOp \
miniNode.SetOp \
Node.Func \
miniNode.Func \
Node.Left Node.SetLeft \
miniNode.Left miniNode.SetLeft \
Node.Right Node.SetRight \
miniNode.Right miniNode.SetRight \
Node.List Node.PtrList Node.SetList \
miniNode.List miniNode.PtrList miniNode.SetList \
Node.Rlist Node.PtrRlist Node.SetRlist \
miniNode.Rlist miniNode.PtrRlist miniNode.SetRlist \
Node.Body Node.PtrBody Node.SetBody \
miniNode.Body miniNode.PtrBody miniNode.SetBody \
Node.SubOp Node.SetSubOp \
miniNode.SubOp miniNode.SetSubOp \
Node.SetSym \
miniNode.SetSym \
Node.Offset Node.SetOffset \
miniNode.Offset miniNode.SetOffset \
Node.Class Node.SetClass \
miniNode.Class miniNode.SetClass \
Node.Iota Node.SetIota \
miniNode.Iota miniNode.SetIota \
Node.Colas Node.SetColas \
miniNode.Colas miniNode.SetColas \
Node.Transient Node.SetTransient \
miniNode.Transient miniNode.SetTransient \
Node.Implicit Node.SetImplicit \
miniNode.Implicit miniNode.SetImplicit \
Node.IsDDD Node.SetIsDDD \
miniNode.IsDDD miniNode.SetIsDDD \
Node.MarkReadonly \
miniNode.MarkReadonly \
Node.Likely Node.SetLikely \
miniNode.Likely miniNode.SetLikely \
Node.SliceBounds Node.SetSliceBounds \
miniNode.SliceBounds miniNode.SetSliceBounds \
Node.NoInline Node.SetNoInline \
miniNode.NoInline miniNode.SetNoInline \
Node.IndexMapLValue Node.SetIndexMapLValue \
miniNode.IndexMapLValue miniNode.SetIndexMapLValue \
Node.ResetAux \
miniNode.ResetAux \
Node.HasBreak Node.SetHasBreak \
miniNode.HasBreak miniNode.SetHasBreak \
Node.Bounded Node.SetBounded \
miniNode.Bounded miniNode.SetBounded \
miniNode.Embedded miniNode.SetEmbedded \
miniNode.Int64Val miniNode.Uint64Val miniNode.CanInt64 \
miniNode.BoolVal miniNode.StringVal \
miniNode.TChanDir miniNode.SetTChanDir \
miniNode.Format \
miniNode.copy miniNode.doChildren miniNode.editChildren \
'
Change-Id: I2a05b535963b43f83b1849fcf653f82b99af6035
Reviewed-on: https://go-review.googlesource.com/c/go/+/277934
Trust: Russ Cox <rsc@golang.org>
Run-TryBot: Russ Cox <rsc@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
596 lines
18 KiB
Go
596 lines
18 KiB
Go
// 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.
|
|
|
|
// “Abstract” syntax representation.
|
|
|
|
package ir
|
|
|
|
import (
|
|
"fmt"
|
|
"go/constant"
|
|
"sort"
|
|
|
|
"cmd/compile/internal/base"
|
|
"cmd/compile/internal/types"
|
|
"cmd/internal/src"
|
|
)
|
|
|
|
// A Node is the abstract interface to an IR node.
|
|
type Node interface {
|
|
// Formatting
|
|
Format(s fmt.State, verb rune)
|
|
|
|
// Source position.
|
|
Pos() src.XPos
|
|
SetPos(x src.XPos)
|
|
|
|
// For making copies. For Copy and SepCopy.
|
|
copy() Node
|
|
|
|
doChildren(func(Node) error) error
|
|
editChildren(func(Node) Node)
|
|
|
|
// Abstract graph structure, for generic traversals.
|
|
Op() Op
|
|
Init() Nodes
|
|
PtrInit() *Nodes
|
|
SetInit(x Nodes)
|
|
|
|
// Fields specific to certain Ops only.
|
|
Type() *types.Type
|
|
SetType(t *types.Type)
|
|
Name() *Name
|
|
Sym() *types.Sym
|
|
Val() constant.Value
|
|
SetVal(v constant.Value)
|
|
|
|
// Storage for analysis passes.
|
|
Esc() uint16
|
|
SetEsc(x uint16)
|
|
Walkdef() uint8
|
|
SetWalkdef(x uint8)
|
|
Opt() interface{}
|
|
SetOpt(x interface{})
|
|
Diag() bool
|
|
SetDiag(x bool)
|
|
Typecheck() uint8
|
|
SetTypecheck(x uint8)
|
|
NonNil() bool
|
|
MarkNonNil()
|
|
HasCall() bool
|
|
SetHasCall(x bool)
|
|
}
|
|
|
|
// Line returns n's position as a string. If n has been inlined,
|
|
// it uses the outermost position where n has been inlined.
|
|
func Line(n Node) string {
|
|
return base.FmtPos(n.Pos())
|
|
}
|
|
|
|
func IsSynthetic(n Node) bool {
|
|
name := n.Sym().Name
|
|
return name[0] == '.' || name[0] == '~'
|
|
}
|
|
|
|
// IsAutoTmp indicates if n was created by the compiler as a temporary,
|
|
// based on the setting of the .AutoTemp flag in n's Name.
|
|
func IsAutoTmp(n Node) bool {
|
|
if n == nil || n.Op() != ONAME {
|
|
return false
|
|
}
|
|
return n.Name().AutoTemp()
|
|
}
|
|
|
|
// mayBeShared reports whether n may occur in multiple places in the AST.
|
|
// Extra care must be taken when mutating such a node.
|
|
func MayBeShared(n Node) bool {
|
|
switch n.Op() {
|
|
case ONAME, OLITERAL, ONIL, OTYPE:
|
|
return true
|
|
}
|
|
return false
|
|
}
|
|
|
|
//go:generate stringer -type=Op -trimprefix=O
|
|
|
|
type Op uint8
|
|
|
|
// Node ops.
|
|
const (
|
|
OXXX Op = iota
|
|
|
|
// names
|
|
ONAME // var or func name
|
|
// Unnamed arg or return value: f(int, string) (int, error) { etc }
|
|
// Also used for a qualified package identifier that hasn't been resolved yet.
|
|
ONONAME
|
|
OTYPE // type name
|
|
OPACK // import
|
|
OLITERAL // literal
|
|
ONIL // nil
|
|
|
|
// expressions
|
|
OADD // Left + Right
|
|
OSUB // Left - Right
|
|
OOR // Left | Right
|
|
OXOR // Left ^ Right
|
|
OADDSTR // +{List} (string addition, list elements are strings)
|
|
OADDR // &Left
|
|
OANDAND // Left && Right
|
|
OAPPEND // append(List); after walk, Left may contain elem type descriptor
|
|
OBYTES2STR // Type(Left) (Type is string, Left is a []byte)
|
|
OBYTES2STRTMP // Type(Left) (Type is string, Left is a []byte, ephemeral)
|
|
ORUNES2STR // Type(Left) (Type is string, Left is a []rune)
|
|
OSTR2BYTES // Type(Left) (Type is []byte, Left is a string)
|
|
OSTR2BYTESTMP // Type(Left) (Type is []byte, Left is a string, ephemeral)
|
|
OSTR2RUNES // Type(Left) (Type is []rune, Left is a string)
|
|
// Left = Right or (if Colas=true) Left := Right
|
|
// If Colas, then Ninit includes a DCL node for Left.
|
|
OAS
|
|
// List = Rlist (x, y, z = a, b, c) or (if Colas=true) List := Rlist
|
|
// If Colas, then Ninit includes DCL nodes for List
|
|
OAS2
|
|
OAS2DOTTYPE // List = Right (x, ok = I.(int))
|
|
OAS2FUNC // List = Right (x, y = f())
|
|
OAS2MAPR // List = Right (x, ok = m["foo"])
|
|
OAS2RECV // List = Right (x, ok = <-c)
|
|
OASOP // Left Etype= Right (x += y)
|
|
OCALL // Left(List) (function call, method call or type conversion)
|
|
|
|
// OCALLFUNC, OCALLMETH, and OCALLINTER have the same structure.
|
|
// Prior to walk, they are: Left(List), where List is all regular arguments.
|
|
// After walk, List is a series of assignments to temporaries,
|
|
// and Rlist is an updated set of arguments.
|
|
// Nbody is all OVARLIVE nodes that are attached to OCALLxxx.
|
|
// TODO(josharian/khr): Use Ninit instead of List for the assignments to temporaries. See CL 114797.
|
|
OCALLFUNC // Left(List/Rlist) (function call f(args))
|
|
OCALLMETH // Left(List/Rlist) (direct method call x.Method(args))
|
|
OCALLINTER // Left(List/Rlist) (interface method call x.Method(args))
|
|
OCALLPART // Left.Right (method expression x.Method, not called)
|
|
OCAP // cap(Left)
|
|
OCLOSE // close(Left)
|
|
OCLOSURE // func Type { Func.Closure.Nbody } (func literal)
|
|
OCOMPLIT // Right{List} (composite literal, not yet lowered to specific form)
|
|
OMAPLIT // Type{List} (composite literal, Type is map)
|
|
OSTRUCTLIT // Type{List} (composite literal, Type is struct)
|
|
OARRAYLIT // Type{List} (composite literal, Type is array)
|
|
OSLICELIT // Type{List} (composite literal, Type is slice) Right.Int64() = slice length.
|
|
OPTRLIT // &Left (left is composite literal)
|
|
OCONV // Type(Left) (type conversion)
|
|
OCONVIFACE // Type(Left) (type conversion, to interface)
|
|
OCONVNOP // Type(Left) (type conversion, no effect)
|
|
OCOPY // copy(Left, Right)
|
|
ODCL // var Left (declares Left of type Left.Type)
|
|
|
|
// Used during parsing but don't last.
|
|
ODCLFUNC // func f() or func (r) f()
|
|
ODCLCONST // const pi = 3.14
|
|
ODCLTYPE // type Int int or type Int = int
|
|
|
|
ODELETE // delete(List)
|
|
ODOT // Left.Sym (Left is of struct type)
|
|
ODOTPTR // Left.Sym (Left is of pointer to struct type)
|
|
ODOTMETH // Left.Sym (Left is non-interface, Right is method name)
|
|
ODOTINTER // Left.Sym (Left is interface, Right is method name)
|
|
OXDOT // Left.Sym (before rewrite to one of the preceding)
|
|
ODOTTYPE // Left.Right or Left.Type (.Right during parsing, .Type once resolved); after walk, .Right contains address of interface type descriptor and .Right.Right contains address of concrete type descriptor
|
|
ODOTTYPE2 // Left.Right or Left.Type (.Right during parsing, .Type once resolved; on rhs of OAS2DOTTYPE); after walk, .Right contains address of interface type descriptor
|
|
OEQ // Left == Right
|
|
ONE // Left != Right
|
|
OLT // Left < Right
|
|
OLE // Left <= Right
|
|
OGE // Left >= Right
|
|
OGT // Left > Right
|
|
ODEREF // *Left
|
|
OINDEX // Left[Right] (index of array or slice)
|
|
OINDEXMAP // Left[Right] (index of map)
|
|
OKEY // Left:Right (key:value in struct/array/map literal)
|
|
OSTRUCTKEY // Sym:Left (key:value in struct literal, after type checking)
|
|
OLEN // len(Left)
|
|
OMAKE // make(List) (before type checking converts to one of the following)
|
|
OMAKECHAN // make(Type, Left) (type is chan)
|
|
OMAKEMAP // make(Type, Left) (type is map)
|
|
OMAKESLICE // make(Type, Left, Right) (type is slice)
|
|
OMAKESLICECOPY // makeslicecopy(Type, Left, Right) (type is slice; Left is length and Right is the copied from slice)
|
|
// OMAKESLICECOPY is created by the order pass and corresponds to:
|
|
// s = make(Type, Left); copy(s, Right)
|
|
//
|
|
// Bounded can be set on the node when Left == len(Right) is known at compile time.
|
|
//
|
|
// This node is created so the walk pass can optimize this pattern which would
|
|
// otherwise be hard to detect after the order pass.
|
|
OMUL // Left * Right
|
|
ODIV // Left / Right
|
|
OMOD // Left % Right
|
|
OLSH // Left << Right
|
|
ORSH // Left >> Right
|
|
OAND // Left & Right
|
|
OANDNOT // Left &^ Right
|
|
ONEW // new(Left); corresponds to calls to new in source code
|
|
ONEWOBJ // runtime.newobject(n.Type); introduced by walk; Left is type descriptor
|
|
ONOT // !Left
|
|
OBITNOT // ^Left
|
|
OPLUS // +Left
|
|
ONEG // -Left
|
|
OOROR // Left || Right
|
|
OPANIC // panic(Left)
|
|
OPRINT // print(List)
|
|
OPRINTN // println(List)
|
|
OPAREN // (Left)
|
|
OSEND // Left <- Right
|
|
OSLICE // Left[List[0] : List[1]] (Left is untypechecked or slice)
|
|
OSLICEARR // Left[List[0] : List[1]] (Left is array)
|
|
OSLICESTR // Left[List[0] : List[1]] (Left is string)
|
|
OSLICE3 // Left[List[0] : List[1] : List[2]] (Left is untypedchecked or slice)
|
|
OSLICE3ARR // Left[List[0] : List[1] : List[2]] (Left is array)
|
|
OSLICEHEADER // sliceheader{Left, List[0], List[1]} (Left is unsafe.Pointer, List[0] is length, List[1] is capacity)
|
|
ORECOVER // recover()
|
|
ORECV // <-Left
|
|
ORUNESTR // Type(Left) (Type is string, Left is rune)
|
|
OSELRECV2 // like OAS2: List = Rlist where len(List)=2, len(Rlist)=1, Rlist[0].Op = ORECV (appears as .Left of OCASE)
|
|
OIOTA // iota
|
|
OREAL // real(Left)
|
|
OIMAG // imag(Left)
|
|
OCOMPLEX // complex(Left, Right) or complex(List[0]) where List[0] is a 2-result function call
|
|
OALIGNOF // unsafe.Alignof(Left)
|
|
OOFFSETOF // unsafe.Offsetof(Left)
|
|
OSIZEOF // unsafe.Sizeof(Left)
|
|
OMETHEXPR // method expression
|
|
OSTMTEXPR // statement expression (Init; Left)
|
|
|
|
// statements
|
|
OBLOCK // { List } (block of code)
|
|
OBREAK // break [Sym]
|
|
// OCASE: case List: Nbody (List==nil means default)
|
|
// For OTYPESW, List is a OTYPE node for the specified type (or OLITERAL
|
|
// for nil), and, if a type-switch variable is specified, Rlist is an
|
|
// ONAME for the version of the type-switch variable with the specified
|
|
// type.
|
|
OCASE
|
|
OCONTINUE // continue [Sym]
|
|
ODEFER // defer Left (Left must be call)
|
|
OFALL // fallthrough
|
|
OFOR // for Ninit; Left; Right { Nbody }
|
|
// OFORUNTIL is like OFOR, but the test (Left) is applied after the body:
|
|
// Ninit
|
|
// top: { Nbody } // Execute the body at least once
|
|
// cont: Right
|
|
// if Left { // And then test the loop condition
|
|
// List // Before looping to top, execute List
|
|
// goto top
|
|
// }
|
|
// OFORUNTIL is created by walk. There's no way to write this in Go code.
|
|
OFORUNTIL
|
|
OGOTO // goto Sym
|
|
OIF // if Ninit; Left { Nbody } else { Rlist }
|
|
OLABEL // Sym:
|
|
OGO // go Left (Left must be call)
|
|
ORANGE // for List = range Right { Nbody }
|
|
ORETURN // return List
|
|
OSELECT // select { List } (List is list of OCASE)
|
|
OSWITCH // switch Ninit; Left { List } (List is a list of OCASE)
|
|
// OTYPESW: Left := Right.(type) (appears as .Left of OSWITCH)
|
|
// Left is nil if there is no type-switch variable
|
|
OTYPESW
|
|
|
|
// types
|
|
OTCHAN // chan int
|
|
OTMAP // map[string]int
|
|
OTSTRUCT // struct{}
|
|
OTINTER // interface{}
|
|
// OTFUNC: func() - Left is receiver field, List is list of param fields, Rlist is
|
|
// list of result fields.
|
|
OTFUNC
|
|
OTARRAY // [8]int or [...]int
|
|
OTSLICE // []int
|
|
|
|
// misc
|
|
OINLCALL // intermediary representation of an inlined call.
|
|
OEFACE // itable and data words of an empty-interface value.
|
|
OITAB // itable word of an interface value.
|
|
OIDATA // data word of an interface value in Left
|
|
OSPTR // base pointer of a slice or string.
|
|
OCLOSUREREAD // read from inside closure struct at beginning of closure function
|
|
OCFUNC // reference to c function pointer (not go func value)
|
|
OCHECKNIL // emit code to ensure pointer/interface not nil
|
|
OVARDEF // variable is about to be fully initialized
|
|
OVARKILL // variable is dead
|
|
OVARLIVE // variable is alive
|
|
ORESULT // result of a function call; Xoffset is stack offset
|
|
OINLMARK // start of an inlined body, with file/line of caller. Xoffset is an index into the inline tree.
|
|
ONAMEOFFSET // offset within a name
|
|
|
|
// arch-specific opcodes
|
|
ORETJMP // return to other function
|
|
OGETG // runtime.getg() (read g pointer)
|
|
|
|
OEND
|
|
)
|
|
|
|
// Nodes is a pointer to a slice of *Node.
|
|
// For fields that are not used in most nodes, this is used instead of
|
|
// a slice to save space.
|
|
type Nodes []Node
|
|
|
|
// immutableEmptyNodes is an immutable, empty Nodes list.
|
|
// The methods that would modify it panic instead.
|
|
var immutableEmptyNodes = Nodes{}
|
|
|
|
// asNodes returns a slice of *Node as a Nodes value.
|
|
func AsNodes(s []Node) Nodes {
|
|
return s
|
|
}
|
|
|
|
// Slice returns the entries in Nodes as a slice.
|
|
// Changes to the slice entries (as in s[i] = n) will be reflected in
|
|
// the Nodes.
|
|
func (n Nodes) Slice() []Node {
|
|
return n
|
|
}
|
|
|
|
// Len returns the number of entries in Nodes.
|
|
func (n Nodes) Len() int {
|
|
return len(n)
|
|
}
|
|
|
|
// Index returns the i'th element of Nodes.
|
|
// It panics if n does not have at least i+1 elements.
|
|
func (n Nodes) Index(i int) Node {
|
|
return n[i]
|
|
}
|
|
|
|
// First returns the first element of Nodes (same as n.Index(0)).
|
|
// It panics if n has no elements.
|
|
func (n Nodes) First() Node {
|
|
return n[0]
|
|
}
|
|
|
|
// Second returns the second element of Nodes (same as n.Index(1)).
|
|
// It panics if n has fewer than two elements.
|
|
func (n Nodes) Second() Node {
|
|
return n[1]
|
|
}
|
|
|
|
func (n *Nodes) mutate() {
|
|
if n == &immutableEmptyNodes {
|
|
panic("immutable Nodes.Set")
|
|
}
|
|
}
|
|
|
|
// Set sets n to a slice.
|
|
// This takes ownership of the slice.
|
|
func (n *Nodes) Set(s []Node) {
|
|
if n == &immutableEmptyNodes {
|
|
if len(s) == 0 {
|
|
// Allow immutableEmptyNodes.Set(nil) (a no-op).
|
|
return
|
|
}
|
|
n.mutate()
|
|
}
|
|
*n = s
|
|
}
|
|
|
|
// Set1 sets n to a slice containing a single node.
|
|
func (n *Nodes) Set1(n1 Node) {
|
|
n.mutate()
|
|
*n = []Node{n1}
|
|
}
|
|
|
|
// Set2 sets n to a slice containing two nodes.
|
|
func (n *Nodes) Set2(n1, n2 Node) {
|
|
n.mutate()
|
|
*n = []Node{n1, n2}
|
|
}
|
|
|
|
// Set3 sets n to a slice containing three nodes.
|
|
func (n *Nodes) Set3(n1, n2, n3 Node) {
|
|
n.mutate()
|
|
*n = []Node{n1, n2, n3}
|
|
}
|
|
|
|
// MoveNodes sets n to the contents of n2, then clears n2.
|
|
func (n *Nodes) MoveNodes(n2 *Nodes) {
|
|
n.mutate()
|
|
*n = *n2
|
|
*n2 = nil
|
|
}
|
|
|
|
// SetIndex sets the i'th element of Nodes to node.
|
|
// It panics if n does not have at least i+1 elements.
|
|
func (n Nodes) SetIndex(i int, node Node) {
|
|
n[i] = node
|
|
}
|
|
|
|
// SetFirst sets the first element of Nodes to node.
|
|
// It panics if n does not have at least one elements.
|
|
func (n Nodes) SetFirst(node Node) {
|
|
n[0] = node
|
|
}
|
|
|
|
// SetSecond sets the second element of Nodes to node.
|
|
// It panics if n does not have at least two elements.
|
|
func (n Nodes) SetSecond(node Node) {
|
|
n[1] = node
|
|
}
|
|
|
|
// Addr returns the address of the i'th element of Nodes.
|
|
// It panics if n does not have at least i+1 elements.
|
|
func (n Nodes) Addr(i int) *Node {
|
|
return &n[i]
|
|
}
|
|
|
|
// Append appends entries to Nodes.
|
|
func (n *Nodes) Append(a ...Node) {
|
|
if len(a) == 0 {
|
|
return
|
|
}
|
|
n.mutate()
|
|
*n = append(*n, a...)
|
|
}
|
|
|
|
// Prepend prepends entries to Nodes.
|
|
// If a slice is passed in, this will take ownership of it.
|
|
func (n *Nodes) Prepend(a ...Node) {
|
|
if len(a) == 0 {
|
|
return
|
|
}
|
|
n.mutate()
|
|
*n = append(a, *n...)
|
|
}
|
|
|
|
// Take clears n, returning its former contents.
|
|
func (n *Nodes) Take() []Node {
|
|
ret := *n
|
|
*n = nil
|
|
return ret
|
|
}
|
|
|
|
// AppendNodes appends the contents of *n2 to n, then clears n2.
|
|
func (n *Nodes) AppendNodes(n2 *Nodes) {
|
|
n.mutate()
|
|
*n = append(*n, n2.Take()...)
|
|
}
|
|
|
|
// Copy returns a copy of the content of the slice.
|
|
func (n Nodes) Copy() Nodes {
|
|
if n == nil {
|
|
return nil
|
|
}
|
|
c := make(Nodes, n.Len())
|
|
copy(c, n)
|
|
return c
|
|
}
|
|
|
|
// NameQueue is a FIFO queue of *Name. The zero value of NameQueue is
|
|
// a ready-to-use empty queue.
|
|
type NameQueue struct {
|
|
ring []*Name
|
|
head, tail int
|
|
}
|
|
|
|
// Empty reports whether q contains no Names.
|
|
func (q *NameQueue) Empty() bool {
|
|
return q.head == q.tail
|
|
}
|
|
|
|
// PushRight appends n to the right of the queue.
|
|
func (q *NameQueue) PushRight(n *Name) {
|
|
if len(q.ring) == 0 {
|
|
q.ring = make([]*Name, 16)
|
|
} else if q.head+len(q.ring) == q.tail {
|
|
// Grow the ring.
|
|
nring := make([]*Name, len(q.ring)*2)
|
|
// Copy the old elements.
|
|
part := q.ring[q.head%len(q.ring):]
|
|
if q.tail-q.head <= len(part) {
|
|
part = part[:q.tail-q.head]
|
|
copy(nring, part)
|
|
} else {
|
|
pos := copy(nring, part)
|
|
copy(nring[pos:], q.ring[:q.tail%len(q.ring)])
|
|
}
|
|
q.ring, q.head, q.tail = nring, 0, q.tail-q.head
|
|
}
|
|
|
|
q.ring[q.tail%len(q.ring)] = n
|
|
q.tail++
|
|
}
|
|
|
|
// PopLeft pops a Name from the left of the queue. It panics if q is
|
|
// empty.
|
|
func (q *NameQueue) PopLeft() *Name {
|
|
if q.Empty() {
|
|
panic("dequeue empty")
|
|
}
|
|
n := q.ring[q.head%len(q.ring)]
|
|
q.head++
|
|
return n
|
|
}
|
|
|
|
// NameSet is a set of Names.
|
|
type NameSet map[*Name]struct{}
|
|
|
|
// Has reports whether s contains n.
|
|
func (s NameSet) Has(n *Name) bool {
|
|
_, isPresent := s[n]
|
|
return isPresent
|
|
}
|
|
|
|
// Add adds n to s.
|
|
func (s *NameSet) Add(n *Name) {
|
|
if *s == nil {
|
|
*s = make(map[*Name]struct{})
|
|
}
|
|
(*s)[n] = struct{}{}
|
|
}
|
|
|
|
// Sorted returns s sorted according to less.
|
|
func (s NameSet) Sorted(less func(*Name, *Name) bool) []*Name {
|
|
var res []*Name
|
|
for n := range s {
|
|
res = append(res, n)
|
|
}
|
|
sort.Slice(res, func(i, j int) bool { return less(res[i], res[j]) })
|
|
return res
|
|
}
|
|
|
|
type PragmaFlag int16
|
|
|
|
const (
|
|
// Func pragmas.
|
|
Nointerface PragmaFlag = 1 << iota
|
|
Noescape // func parameters don't escape
|
|
Norace // func must not have race detector annotations
|
|
Nosplit // func should not execute on separate stack
|
|
Noinline // func should not be inlined
|
|
NoCheckPtr // func should not be instrumented by checkptr
|
|
CgoUnsafeArgs // treat a pointer to one arg as a pointer to them all
|
|
UintptrEscapes // pointers converted to uintptr escape
|
|
|
|
// Runtime-only func pragmas.
|
|
// See ../../../../runtime/README.md for detailed descriptions.
|
|
Systemstack // func must run on system stack
|
|
Nowritebarrier // emit compiler error instead of write barrier
|
|
Nowritebarrierrec // error on write barrier in this or recursive callees
|
|
Yeswritebarrierrec // cancels Nowritebarrierrec in this function and callees
|
|
|
|
// Runtime and cgo type pragmas
|
|
NotInHeap // values of this type must not be heap allocated
|
|
|
|
// Go command pragmas
|
|
GoBuildPragma
|
|
)
|
|
|
|
func AsNode(n types.Object) Node {
|
|
if n == nil {
|
|
return nil
|
|
}
|
|
return n.(Node)
|
|
}
|
|
|
|
var BlankNode Node
|
|
|
|
func IsConst(n Node, ct constant.Kind) bool {
|
|
return ConstType(n) == ct
|
|
}
|
|
|
|
// isNil reports whether n represents the universal untyped zero value "nil".
|
|
func IsNil(n Node) bool {
|
|
// Check n.Orig because constant propagation may produce typed nil constants,
|
|
// which don't exist in the Go spec.
|
|
return n != nil && Orig(n).Op() == ONIL
|
|
}
|
|
|
|
func IsBlank(n Node) bool {
|
|
if n == nil {
|
|
return false
|
|
}
|
|
return n.Sym().IsBlank()
|
|
}
|
|
|
|
// IsMethod reports whether n is a method.
|
|
// n must be a function or a method.
|
|
func IsMethod(n Node) bool {
|
|
return n.Type().Recv() != nil
|
|
}
|