1 // Copyright 2020 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.
8 "cmd/compile/internal/base"
9 "cmd/compile/internal/types"
14 // A Decl is a declaration of a const, type, or var. (A declared func is a Func.)
17 X *Name // the thing being declared
20 func NewDecl(pos src.XPos, op Op, x *Name) *Decl {
25 panic("invalid Decl op " + op.String())
32 func (*Decl) isStmt() {}
34 // A Stmt is a Node that can appear as a statement.
35 // This includes statement-like expressions such as f().
37 // (It's possible it should include <-c, but that would require
38 // splitting ORECV out of UnaryExpr, which hasn't yet been
39 // necessary. Maybe instead we will introduce ExprStmt at
46 // A miniStmt is a miniNode with extra fields common to statements.
47 type miniStmt struct {
52 func (*miniStmt) isStmt() {}
54 func (n *miniStmt) Init() Nodes { return n.init }
55 func (n *miniStmt) SetInit(x Nodes) { n.init = x }
56 func (n *miniStmt) PtrInit() *Nodes { return &n.init }
58 // An AssignListStmt is an assignment statement with
59 // more than one item on at least one side: Lhs = Rhs.
60 // If Def is true, the assignment is a :=.
61 type AssignListStmt struct {
68 func NewAssignListStmt(pos src.XPos, op Op, lhs, rhs []Node) *AssignListStmt {
69 n := &AssignListStmt{}
77 func (n *AssignListStmt) SetOp(op Op) {
80 panic(n.no("SetOp " + op.String()))
81 case OAS2, OAS2DOTTYPE, OAS2FUNC, OAS2MAPR, OAS2RECV, OSELRECV2:
86 // An AssignStmt is a simple assignment statement: X = Y.
87 // If Def is true, the assignment is a :=.
88 type AssignStmt struct {
95 func NewAssignStmt(pos src.XPos, x, y Node) *AssignStmt {
96 n := &AssignStmt{X: x, Y: y}
102 func (n *AssignStmt) SetOp(op Op) {
105 panic(n.no("SetOp " + op.String()))
111 // An AssignOpStmt is an AsOp= assignment statement: X AsOp= Y.
112 type AssignOpStmt struct {
117 IncDec bool // actually ++ or --
120 func NewAssignOpStmt(pos src.XPos, asOp Op, x, y Node) *AssignOpStmt {
121 n := &AssignOpStmt{AsOp: asOp, X: x, Y: y}
127 // A BlockStmt is a block: { List }.
128 type BlockStmt struct {
133 func NewBlockStmt(pos src.XPos, list []Node) *BlockStmt {
139 n.pos = list[0].Pos()
147 // A BranchStmt is a break, continue, fallthrough, or goto statement.
148 type BranchStmt struct {
150 Label *types.Sym // label if present
153 func NewBranchStmt(pos src.XPos, op Op, label *types.Sym) *BranchStmt {
155 case OBREAK, OCONTINUE, OFALL, OGOTO:
158 panic("NewBranch " + op.String())
160 n := &BranchStmt{Label: label}
166 func (n *BranchStmt) SetOp(op Op) {
169 panic(n.no("SetOp " + op.String()))
170 case OBREAK, OCONTINUE, OFALL, OGOTO:
175 func (n *BranchStmt) Sym() *types.Sym { return n.Label }
177 // A CaseClause is a case statement in a switch or select: case List: Body.
178 type CaseClause struct {
180 Var *Name // declared variable for this case in type switch
181 List Nodes // list of expressions for switch, early select
183 // RTypes is a list of RType expressions, which are copied to the
184 // corresponding OEQ nodes that are emitted when switch statements
185 // are desugared. RTypes[i] must be non-nil if the emitted
186 // comparison for List[i] will be a mixed interface/concrete
187 // comparison; see reflectdata.CompareRType for details.
189 // Because mixed interface/concrete switch cases are rare, we allow
190 // len(RTypes) < len(List). Missing entries are implicitly nil.
196 func NewCaseStmt(pos src.XPos, list, body []Node) *CaseClause {
197 n := &CaseClause{List: list, Body: body}
203 type CommClause struct {
205 Comm Node // communication case
209 func NewCommStmt(pos src.XPos, comm Node, body []Node) *CommClause {
210 n := &CommClause{Comm: comm, Body: body}
216 // A ForStmt is a non-range for loop: for Init; Cond; Post { Body }
217 type ForStmt struct {
226 func NewForStmt(pos src.XPos, init Node, cond, post Node, body []Node, distinctVars bool) *ForStmt {
227 n := &ForStmt{Cond: cond, Post: post}
231 n.init = []Node{init}
234 n.DistinctVars = distinctVars
238 // A GoDeferStmt is a go or defer statement: go Call / defer Call.
240 // The two opcodes use a single syntax because the implementations
241 // are very similar: both are concerned with saving Call and running it
242 // in a different context (a separate goroutine or a later time).
243 type GoDeferStmt struct {
249 func NewGoDeferStmt(pos src.XPos, op Op, call Node) *GoDeferStmt {
250 n := &GoDeferStmt{Call: call}
256 panic("NewGoDeferStmt " + op.String())
261 // An IfStmt is a return statement: if Init; Cond { Body } else { Else }.
267 Likely bool // code layout hint
270 func NewIfStmt(pos src.XPos, cond Node, body, els []Node) *IfStmt {
271 n := &IfStmt{Cond: cond}
279 // A JumpTableStmt is used to implement switches. Its semantics are:
282 // if tmp == Cases[0] goto Targets[0]
283 // if tmp == Cases[1] goto Targets[1]
285 // if tmp == Cases[n] goto Targets[n]
287 // Note that a JumpTableStmt is more like a multiway-goto than
288 // a multiway-if. In particular, the case bodies are just
289 // labels to jump to, not full Nodes lists.
290 type JumpTableStmt struct {
293 // Value used to index the jump table.
294 // We support only integer types that
295 // are at most the size of a uintptr.
298 // If Idx is equal to Cases[i], jump to Targets[i].
299 // Cases entries must be distinct and in increasing order.
300 // The length of Cases and Targets must be equal.
301 Cases []constant.Value
305 func NewJumpTableStmt(pos src.XPos, idx Node) *JumpTableStmt {
306 n := &JumpTableStmt{Idx: idx}
312 // An InlineMarkStmt is a marker placed just before an inlined body.
313 type InlineMarkStmt struct {
318 func NewInlineMarkStmt(pos src.XPos, index int64) *InlineMarkStmt {
319 n := &InlineMarkStmt{Index: index}
325 func (n *InlineMarkStmt) Offset() int64 { return n.Index }
326 func (n *InlineMarkStmt) SetOffset(x int64) { n.Index = x }
328 // A LabelStmt is a label statement (just the label, not including the statement it labels).
329 type LabelStmt struct {
331 Label *types.Sym // "Label:"
334 func NewLabelStmt(pos src.XPos, label *types.Sym) *LabelStmt {
335 n := &LabelStmt{Label: label}
341 func (n *LabelStmt) Sym() *types.Sym { return n.Label }
343 // A RangeStmt is a range loop: for Key, Value = range X { Body }
344 type RangeStmt struct {
349 RType Node `mknode:"-"` // see reflectdata/helpers.go
356 // When desugaring the RangeStmt during walk, the assignments to Key
357 // and Value may require OCONVIFACE operations. If so, these fields
358 // will be copied to their respective ConvExpr fields.
359 KeyTypeWord Node `mknode:"-"`
360 KeySrcRType Node `mknode:"-"`
361 ValueTypeWord Node `mknode:"-"`
362 ValueSrcRType Node `mknode:"-"`
365 func NewRangeStmt(pos src.XPos, key, value, x Node, body []Node, distinctVars bool) *RangeStmt {
366 n := &RangeStmt{X: x, Key: key, Value: value}
370 n.DistinctVars = distinctVars
374 // A ReturnStmt is a return statement.
375 type ReturnStmt struct {
377 Results Nodes // return list
380 func NewReturnStmt(pos src.XPos, results []Node) *ReturnStmt {
388 // A SelectStmt is a block: { Cases }.
389 type SelectStmt struct {
394 // TODO(rsc): Instead of recording here, replace with a block?
395 Compiled Nodes // compiled form, after walkSelect
398 func NewSelectStmt(pos src.XPos, cases []*CommClause) *SelectStmt {
399 n := &SelectStmt{Cases: cases}
405 // A SendStmt is a send statement: X <- Y.
406 type SendStmt struct {
412 func NewSendStmt(pos src.XPos, ch, value Node) *SendStmt {
413 n := &SendStmt{Chan: ch, Value: value}
419 // A SwitchStmt is a switch statement: switch Init; Tag { Cases }.
420 type SwitchStmt struct {
426 // TODO(rsc): Instead of recording here, replace with a block?
427 Compiled Nodes // compiled form, after walkSwitch
430 func NewSwitchStmt(pos src.XPos, tag Node, cases []*CaseClause) *SwitchStmt {
431 n := &SwitchStmt{Tag: tag, Cases: cases}
437 // A TailCallStmt is a tail call statement, which is used for back-end
438 // code generation to jump directly to another function entirely.
439 type TailCallStmt struct {
441 Call *CallExpr // the underlying call
444 func NewTailCallStmt(pos src.XPos, call *CallExpr) *TailCallStmt {
445 n := &TailCallStmt{Call: call}
451 // A TypeSwitchGuard is the [Name :=] X.(type) in a type switch.
452 type TypeSwitchGuard struct {
459 func NewTypeSwitchGuard(pos src.XPos, tag *Ident, x Node) *TypeSwitchGuard {
460 n := &TypeSwitchGuard{Tag: tag, X: x}