diff options
author | Damian Gryski <[email protected]> | 2023-03-02 11:52:52 -0800 |
---|---|---|
committer | Damian Gryski <[email protected]> | 2023-03-08 09:38:49 -0800 |
commit | 2de64d3f4e16f0a3f0e2da7a158ddfc9e35cae0f (patch) | |
tree | 15bdb49f247cf14f732011c6f7aa197ff144ce77 /compiler/interface.go | |
parent | c192c7376ea9e5dec33610a250bafde227187f9d (diff) | |
download | tinygo-2de64d3f4e16f0a3f0e2da7a158ddfc9e35cae0f.tar.gz tinygo-2de64d3f4e16f0a3f0e2da7a158ddfc9e35cae0f.zip |
compiler, reflect: add Type.PkgPath
Diffstat (limited to 'compiler/interface.go')
-rw-r--r-- | compiler/interface.go | 29 |
1 files changed, 27 insertions, 2 deletions
diff --git a/compiler/interface.go b/compiler/interface.go index ca9fc9ba1..5eb939d15 100644 --- a/compiler/interface.go +++ b/compiler/interface.go @@ -112,6 +112,8 @@ func (c *compilerContext) getTypeCode(typ types.Type) llvm.Value { typeFieldTypes = append(typeFieldTypes, types.NewVar(token.NoPos, nil, "ptrTo", types.Typ[types.UnsafePointer]), types.NewVar(token.NoPos, nil, "underlying", types.Typ[types.UnsafePointer]), + types.NewVar(token.NoPos, nil, "pkglen", types.Typ[types.Uintptr]), + types.NewVar(token.NoPos, nil, "pkgpath", types.Typ[types.UnsafePointer]), types.NewVar(token.NoPos, nil, "len", types.Typ[types.Uintptr]), types.NewVar(token.NoPos, nil, "name", types.NewArray(types.Typ[types.Int8], int64(len(typ.Obj().Name())))), ) @@ -172,9 +174,32 @@ func (c *compilerContext) getTypeCode(typ types.Type) llvm.Value { typeFields = []llvm.Value{c.getTypeCode(types.NewPointer(typ))} case *types.Named: name := typ.Obj().Name() + + pkg := typ.Obj().Pkg() + var pkgpath string + pkgpathName := "reflect/types.type.pkgpath.empty" + if pkg != nil { + pkgpath = pkg.Path() + pkgpathName = "reflect/types.type.pkgpath:" + pkgpath + } + + pkgpathInitializer := c.ctx.ConstString(pkgpath, false) + pkgpathGlobal := llvm.AddGlobal(c.mod, pkgpathInitializer.Type(), pkgpathName) + pkgpathGlobal.SetInitializer(pkgpathInitializer) + pkgpathGlobal.SetAlignment(1) + pkgpathGlobal.SetUnnamedAddr(true) + pkgpathGlobal.SetLinkage(llvm.LinkOnceODRLinkage) + pkgpathGlobal.SetGlobalConstant(true) + pkgpathPtr := llvm.ConstGEP(pkgpathGlobal.GlobalValueType(), pkgpathGlobal, []llvm.Value{ + llvm.ConstInt(c.ctx.Int32Type(), 0, false), + llvm.ConstInt(c.ctx.Int32Type(), 0, false), + }) + typeFields = []llvm.Value{ - c.getTypeCode(types.NewPointer(typ)), // ptrTo - c.getTypeCode(typ.Underlying()), // underlying + c.getTypeCode(types.NewPointer(typ)), // ptrTo + c.getTypeCode(typ.Underlying()), // underlying + llvm.ConstInt(c.uintptrType, uint64(len(pkgpath)), false), // pkgpath length + pkgpathPtr, // pkgpath pointer llvm.ConstInt(c.uintptrType, uint64(len(name)), false), // length c.ctx.ConstString(name, false), // name } |