]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: construct more IR nodes as typed
authorMatthew Dempsky <mdempsky@google.com>
Fri, 18 Aug 2023 07:54:28 +0000 (00:54 -0700)
committerGopher Robot <gobot@golang.org>
Fri, 18 Aug 2023 22:42:15 +0000 (22:42 +0000)
As of this CL, all OLITERAL, OLINKSYMOFFSET, ONIL, and OTYPE nodes are
constructed as typed and typechecked.

Change-Id: I39b2ad772a9b0419c701890a505a0949f9ea456e
Reviewed-on: https://go-review.googlesource.com/c/go/+/520795
Reviewed-by: Dmitri Shuralyov <dmitshur@google.com>
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
Auto-Submit: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Cuong Manh Le <cuong.manhle.vn@gmail.com>
src/cmd/compile/internal/ir/expr.go
src/cmd/compile/internal/noder/helpers.go
src/cmd/compile/internal/typecheck/func.go
src/cmd/compile/internal/typecheck/subr.go
src/cmd/compile/internal/typecheck/typecheck.go
src/cmd/compile/internal/typecheck/universe.go

index 37e2689b97ef8448fc2632d95f257f1be3156ca8..63a6a3eed2749f5d88c20a6719a0d541b28a7d6e 100644 (file)
@@ -106,9 +106,8 @@ func NewBasicLit(pos src.XPos, val constant.Value) Node {
        n := &BasicLit{val: val}
        n.op = OLITERAL
        n.pos = pos
-       if k := val.Kind(); k != constant.Unknown {
-               n.SetType(idealType(k))
-       }
+       n.SetType(idealType(val.Kind()))
+       n.SetTypecheck(1)
        return n
 }
 
@@ -432,15 +431,19 @@ func (n *MakeExpr) SetOp(op Op) {
 }
 
 // A NilExpr represents the predefined untyped constant nil.
-// (It may be copied and assigned a type, though.)
 type NilExpr struct {
        miniExpr
 }
 
-func NewNilExpr(pos src.XPos) *NilExpr {
+func NewNilExpr(pos src.XPos, typ *types.Type) *NilExpr {
+       if typ == nil {
+               base.FatalfAt(pos, "missing type")
+       }
        n := &NilExpr{}
        n.pos = pos
        n.op = ONIL
+       n.SetType(typ)
+       n.SetTypecheck(1)
        return n
 }
 
@@ -498,9 +501,13 @@ type LinksymOffsetExpr struct {
 }
 
 func NewLinksymOffsetExpr(pos src.XPos, lsym *obj.LSym, offset int64, typ *types.Type) *LinksymOffsetExpr {
+       if typ == nil {
+               base.FatalfAt(pos, "nil type")
+       }
        n := &LinksymOffsetExpr{Linksym: lsym, Offset_: offset}
        n.typ = typ
        n.op = OLINKSYMOFFSET
+       n.SetTypecheck(1)
        return n
 }
 
index ce63e6fafcadbd20d4df93d864b523045dcf913e..628719a92264a76209548e038f04557596c24deb 100644 (file)
@@ -66,7 +66,7 @@ func FixValue(typ *types.Type, val constant.Value) constant.Value {
 }
 
 func Nil(pos src.XPos, typ *types.Type) ir.Node {
-       return typed(typ, ir.NewNilExpr(pos))
+       return ir.NewNilExpr(pos, typ)
 }
 
 // Expressions
index 3084ac8f343734d53d0feb5c66e011ed25057c49..b72715059630bbcd0309f7791564a1d988e873cf 100644 (file)
@@ -17,18 +17,15 @@ import (
 
 // MakeDotArgs package all the arguments that match a ... T parameter into a []T.
 func MakeDotArgs(pos src.XPos, typ *types.Type, args []ir.Node) ir.Node {
-       var n ir.Node
        if len(args) == 0 {
-               n = ir.NewNilExpr(pos)
-               n.SetType(typ)
-       } else {
-               args = append([]ir.Node(nil), args...)
-               lit := ir.NewCompLitExpr(pos, ir.OCOMPLIT, typ, args)
-               lit.SetImplicit(true)
-               n = lit
+               return ir.NewNilExpr(pos, typ)
        }
 
-       n = Expr(n)
+       args = append([]ir.Node(nil), args...)
+       lit := ir.NewCompLitExpr(pos, ir.OCOMPLIT, typ, args)
+       lit.SetImplicit(true)
+
+       n := Expr(lit)
        if n.Type() == nil {
                base.FatalfAt(pos, "mkdotargslice: typecheck failed")
        }
index 75b5d58feef15ad5613ffef15d273d0634e604da..91d05778f138d260332516452e91aa2607c337d5 100644 (file)
@@ -120,9 +120,7 @@ func LinksymAddr(pos src.XPos, lsym *obj.LSym, typ *types.Type) *ir.AddrExpr {
 }
 
 func NodNil() ir.Node {
-       n := ir.NewNilExpr(base.Pos)
-       n.SetType(types.Types[types.TNIL])
-       return n
+       return ir.NewNilExpr(base.Pos, types.Types[types.TNIL])
 }
 
 // AddImplicitDots finds missing fields in obj.field that
index 16e6db6a25a8efbb39c31dbcbf9b289d12f5d88a..384295b55f35eaf8dd890c6023ad028b631f779b 100644 (file)
@@ -177,7 +177,7 @@ func typecheck(n ir.Node, top int) (res ir.Node) {
        // But re-typecheck ONAME/OTYPE/OLITERAL/OPACK node in case context has changed.
        if n.Typecheck() == 1 || n.Typecheck() == 3 {
                switch n.Op() {
-               case ir.ONAME, ir.OTYPE, ir.OLITERAL:
+               case ir.ONAME:
                        break
 
                default:
@@ -230,22 +230,6 @@ func typecheck1(n ir.Node, top int) ir.Node {
                base.Fatalf("typecheck %v", n.Op())
                panic("unreachable")
 
-       case ir.OLITERAL:
-               if n.Sym() == nil && n.Type() == nil {
-                       base.Fatalf("literal missing type: %v", n)
-               }
-               return n
-
-       case ir.ONIL:
-               return n
-
-       // names
-       case ir.ONONAME:
-               // Note: adderrorname looks for this string and
-               // adds context about the outer expression
-               base.FatalfAt(n.Pos(), "undefined: %v", n.Sym())
-               panic("unreachable")
-
        case ir.ONAME:
                n := n.(*ir.Name)
                if n.BuiltinOp != 0 {
@@ -267,14 +251,6 @@ func typecheck1(n ir.Node, top int) ir.Node {
                }
                return n
 
-       case ir.OLINKSYMOFFSET:
-               // type already set
-               return n
-
-       // types (ODEREF is with exprs)
-       case ir.OTYPE:
-               return n
-
        // type or expr
        case ir.ODEREF:
                n := n.(*ir.StarExpr)
index a5bfca2157bdc9f9c6590fe2dbffa9f77ff491e9..62f5b628dd86ec19edda865d1f84ec97154aa312 100644 (file)
@@ -70,6 +70,7 @@ func InitUniverse() {
        types.InitTypes(func(sym *types.Sym, typ *types.Type) types.Object {
                n := ir.NewDeclNameAt(src.NoXPos, ir.OTYPE, sym)
                n.SetType(typ)
+               n.SetTypecheck(1)
                sym.Def = n
                return n
        })