| // Copyright 2021 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 noder | |
| import ( | |
| "go/constant" | |
| "cmd/compile/internal/base" | |
| "cmd/compile/internal/ir" | |
| "cmd/compile/internal/syntax" | |
| "cmd/compile/internal/typecheck" | |
| "cmd/compile/internal/types" | |
| "cmd/compile/internal/types2" | |
| "cmd/internal/src" | |
| ) | |
| // Helpers for constructing typed IR nodes. | |
| // | |
| // TODO(mdempsky): Move into their own package so they can be easily | |
| // reused by iimport and frontend optimizations. | |
| type ImplicitNode interface { | |
| ir.Node | |
| SetImplicit(x bool) | |
| } | |
| // Implicit returns n after marking it as Implicit. | |
| func Implicit(n ImplicitNode) ImplicitNode { | |
| n.SetImplicit(true) | |
| return n | |
| } | |
| // typed returns n after setting its type to typ. | |
| func typed(typ *types.Type, n ir.Node) ir.Node { | |
| n.SetType(typ) | |
| n.SetTypecheck(1) | |
| return n | |
| } | |
| // Values | |
| func OrigConst(pos src.XPos, typ *types.Type, val constant.Value, op ir.Op, raw string) ir.Node { | |
| orig := ir.NewRawOrigExpr(pos, op, raw) | |
| return ir.NewConstExpr(val, typed(typ, orig)) | |
| } | |
| // FixValue returns val after converting and truncating it as | |
| // appropriate for typ. | |
| func FixValue(typ *types.Type, val constant.Value) constant.Value { | |
| assert(typ.Kind() != types.TFORW) | |
| switch { | |
| case typ.IsInteger(): | |
| val = constant.ToInt(val) | |
| case typ.IsFloat(): | |
| val = constant.ToFloat(val) | |
| case typ.IsComplex(): | |
| val = constant.ToComplex(val) | |
| } | |
| if !typ.IsUntyped() { | |
| val = typecheck.DefaultLit(ir.NewBasicLit(src.NoXPos, val), typ).Val() | |
| } | |
| ir.AssertValidTypeForConst(typ, val) | |
| return val | |
| } | |
| func Nil(pos src.XPos, typ *types.Type) ir.Node { | |
| return typed(typ, ir.NewNilExpr(pos)) | |
| } | |
| // Expressions | |
| func Addr(pos src.XPos, x ir.Node) *ir.AddrExpr { | |
| n := typecheck.NodAddrAt(pos, x) | |
| typed(types.NewPtr(x.Type()), n) | |
| return n | |
| } | |
| func Deref(pos src.XPos, typ *types.Type, x ir.Node) *ir.StarExpr { | |
| n := ir.NewStarExpr(pos, x) | |
| typed(typ, n) | |
| return n | |
| } | |
| func DotField(pos src.XPos, x ir.Node, index int) *ir.SelectorExpr { | |
| op, typ := ir.ODOT, x.Type() | |
| if typ.IsPtr() { | |
| op, typ = ir.ODOTPTR, typ.Elem() | |
| } | |
| if !typ.IsStruct() { | |
| base.FatalfAt(pos, "DotField of non-struct: %L", x) | |
| } | |
| // TODO(mdempsky): This is the backend's responsibility. | |
| types.CalcSize(typ) | |
| field := typ.Field(index) | |
| return dot(pos, field.Type, op, x, field) | |
| } | |
| func dot(pos src.XPos, typ *types.Type, op ir.Op, x ir.Node, selection *types.Field) *ir.SelectorExpr { | |
| n := ir.NewSelectorExpr(pos, op, x, selection.Sym) | |
| n.Selection = selection | |
| typed(typ, n) | |
| return n | |
| } | |
| // Statements | |
| var one = constant.MakeInt64(1) | |
| func idealType(tv syntax.TypeAndValue) types2.Type { | |
| // The gc backend expects all expressions to have a concrete type, and | |
| // types2 mostly satisfies this expectation already. But there are a few | |
| // cases where the Go spec doesn't require converting to concrete type, | |
| // and so types2 leaves them untyped. So we need to fix those up here. | |
| typ := tv.Type | |
| if basic, ok := typ.(*types2.Basic); ok && basic.Info()&types2.IsUntyped != 0 { | |
| switch basic.Kind() { | |
| case types2.UntypedNil: | |
| // ok; can appear in type switch case clauses | |
| // TODO(mdempsky): Handle as part of type switches instead? | |
| case types2.UntypedInt, types2.UntypedFloat, types2.UntypedComplex: | |
| // Untyped rhs of non-constant shift, e.g. x << 1.0. | |
| // If we have a constant value, it must be an int >= 0. | |
| if tv.Value != nil { | |
| s := constant.ToInt(tv.Value) | |
| assert(s.Kind() == constant.Int && constant.Sign(s) >= 0) | |
| } | |
| typ = types2.Typ[types2.Uint] | |
| case types2.UntypedBool: | |
| typ = types2.Typ[types2.Bool] // expression in "if" or "for" condition | |
| case types2.UntypedString: | |
| typ = types2.Typ[types2.String] // argument to "append" or "copy" calls | |
| default: | |
| return nil | |
| } | |
| } | |
| return typ | |
| } | |
| func isTypeParam(t types2.Type) bool { | |
| _, ok := t.(*types2.TypeParam) | |
| return ok | |
| } | |
| // isNotInHeap reports whether typ is or contains an element of type | |
| // runtime/internal/sys.NotInHeap. | |
| func isNotInHeap(typ types2.Type) bool { | |
| if named, ok := typ.(*types2.Named); ok { | |
| if obj := named.Obj(); obj.Name() == "nih" && obj.Pkg().Path() == "runtime/internal/sys" { | |
| return true | |
| } | |
| typ = named.Underlying() | |
| } | |
| switch typ := typ.(type) { | |
| case *types2.Array: | |
| return isNotInHeap(typ.Elem()) | |
| case *types2.Struct: | |
| for i := 0; i < typ.NumFields(); i++ { | |
| if isNotInHeap(typ.Field(i).Type()) { | |
| return true | |
| } | |
| } | |
| return false | |
| default: | |
| return false | |
| } | |
| } |