Black Lives Matter. Support the Equal Justice Initiative.

Source file src/cmd/compile/internal/typecheck/export.go

Documentation: cmd/compile/internal/typecheck

     1  // Copyright 2009 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.
     4  
     5  package typecheck
     6  
     7  import (
     8  	"go/constant"
     9  
    10  	"cmd/compile/internal/base"
    11  	"cmd/compile/internal/ir"
    12  	"cmd/compile/internal/types"
    13  	"cmd/internal/src"
    14  )
    15  
    16  // importalias declares symbol s as an imported type alias with type t.
    17  // ipkg is the package being imported
    18  func importalias(ipkg *types.Pkg, pos src.XPos, s *types.Sym, t *types.Type) *ir.Name {
    19  	return importobj(ipkg, pos, s, ir.OTYPE, ir.PEXTERN, t)
    20  }
    21  
    22  // importconst declares symbol s as an imported constant with type t and value val.
    23  // ipkg is the package being imported
    24  func importconst(ipkg *types.Pkg, pos src.XPos, s *types.Sym, t *types.Type, val constant.Value) *ir.Name {
    25  	n := importobj(ipkg, pos, s, ir.OLITERAL, ir.PEXTERN, t)
    26  	n.SetVal(val)
    27  	return n
    28  }
    29  
    30  // importfunc declares symbol s as an imported function with type t.
    31  // ipkg is the package being imported
    32  func importfunc(ipkg *types.Pkg, pos src.XPos, s *types.Sym, t *types.Type) *ir.Name {
    33  	n := importobj(ipkg, pos, s, ir.ONAME, ir.PFUNC, t)
    34  	n.Func = ir.NewFunc(pos)
    35  	n.Func.Nname = n
    36  	return n
    37  }
    38  
    39  // importobj declares symbol s as an imported object representable by op.
    40  // ipkg is the package being imported
    41  func importobj(ipkg *types.Pkg, pos src.XPos, s *types.Sym, op ir.Op, ctxt ir.Class, t *types.Type) *ir.Name {
    42  	n := importsym(ipkg, pos, s, op, ctxt)
    43  	n.SetType(t)
    44  	if ctxt == ir.PFUNC {
    45  		n.Sym().SetFunc(true)
    46  	}
    47  	return n
    48  }
    49  
    50  func importsym(ipkg *types.Pkg, pos src.XPos, s *types.Sym, op ir.Op, ctxt ir.Class) *ir.Name {
    51  	if n := s.PkgDef(); n != nil {
    52  		base.Fatalf("importsym of symbol that already exists: %v", n)
    53  	}
    54  
    55  	n := ir.NewDeclNameAt(pos, op, s)
    56  	n.Class = ctxt // TODO(mdempsky): Move this into NewDeclNameAt too?
    57  	s.SetPkgDef(n)
    58  	return n
    59  }
    60  
    61  // importtype returns the named type declared by symbol s.
    62  // If no such type has been declared yet, a forward declaration is returned.
    63  // ipkg is the package being imported
    64  func importtype(ipkg *types.Pkg, pos src.XPos, s *types.Sym) *ir.Name {
    65  	n := importsym(ipkg, pos, s, ir.OTYPE, ir.PEXTERN)
    66  	n.SetType(types.NewNamed(n))
    67  	return n
    68  }
    69  
    70  // importvar declares symbol s as an imported variable with type t.
    71  // ipkg is the package being imported
    72  func importvar(ipkg *types.Pkg, pos src.XPos, s *types.Sym, t *types.Type) *ir.Name {
    73  	return importobj(ipkg, pos, s, ir.ONAME, ir.PEXTERN, t)
    74  }
    75  

View as plain text