Skip to content

Commit

Permalink
Change gotk3/glib imports to externglib
Browse files Browse the repository at this point in the history
  • Loading branch information
diamondburned committed May 15, 2021
1 parent b0dc0cf commit 470cbd9
Show file tree
Hide file tree
Showing 24 changed files with 896 additions and 886 deletions.
5 changes: 3 additions & 2 deletions gir/girgen/gen_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import "github.com/diamondburned/gotk4/gir"

var initTmpl = newGoTemplate(`
func init() {
glib.RegisterGValueMarshalers([]glib.TypeMarshaler{
externglib.RegisterGValueMarshalers([]externglib.TypeMarshaler{
{{ if .Enums -}}
// Enums
{{- range .Enums }}
{{- if .GLibGetType }}
{T: glib.Type(C.{{.GLibGetType}}()), F: marshal{{.Name}}},
{T: externglib.Type(C.{{.GLibGetType}}()), F: marshal{{.Name}}},
{{- else }}
// Skipped {{.Name}}.
{{- end -}}
Expand All @@ -27,6 +27,7 @@ type initGenerator struct {
}

func (ng *NamespaceGenerator) generateInit() {
ng.addImportAlias("github.com/gotk3/gotk3/glib", "externglib")
ng.pen.BlockTmpl(initTmpl, initGenerator{
Namespace: *ng.current.Namespace,
Ng: ng,
Expand Down
8 changes: 5 additions & 3 deletions gir/girgen/girgen.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ func (g *Generator) UseNamespace(namespace string) *NamespaceGenerator {
body: &buf,

imports: map[string]string{},
pkgPath: g.ImportPath(gir.GoNamespace(res.Namespace)),
gen: g,
current: res,
}
Expand All @@ -140,6 +141,7 @@ type NamespaceGenerator struct {
pen *pen.Pen
body *bytes.Buffer

pkgPath string // package name
imports map[string]string // optional alias value

gen *Generator
Expand Down Expand Up @@ -181,10 +183,10 @@ func (ng *NamespaceGenerator) Generate(w io.Writer) error {
// Only use the import alias if it's provided and does not match the
// base name of the import path for idiomaticity.
if alias != "" && alias != path.Base(imp) {
pen.Words(alias, "")
pen.Words(alias, "", strconv.Quote(imp))
} else {
pen.Words(strconv.Quote(imp))
}

pen.Words(strconv.Quote(imp))
}
pen.Block(")")
pen.Line()
Expand Down
34 changes: 17 additions & 17 deletions gir/girgen/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ var (
typeFlight singleflight.Group
)

// countPtrs counts the number of nested pointers from the given gir.Type.
func countPtrs(typ gir.Type, result *gir.TypeFindResult) uint8 {
ptr := uint8(strings.Count(typ.CType, "*"))

Expand All @@ -55,8 +54,10 @@ func countPtrs(typ gir.Type, result *gir.TypeFindResult) uint8 {

// builtinType is a convenient function to make a new resolvedType.
func builtinType(imp, typ string, girType gir.Type) *ResolvedType {
// Create the actual type.
typ = path.Base(imp) + "." + typ
if imp != "" {
// Create the actual type if there's an import path.
typ = path.Base(imp) + "." + typ
}

return &ResolvedType{
Builtin: &typ,
Expand All @@ -69,14 +70,15 @@ func builtinType(imp, typ string, girType gir.Type) *ResolvedType {

// externGLibType returns an external GLib type from gotk3.
func externGLibType(glibType string, typ gir.Type) *ResolvedType {
glibType = "externglib." + glibType
ptrs := strings.Count(glibType, "*")
glibType = strings.Repeat("*", ptrs) + "externglib." + strings.TrimPrefix(glibType, "*")

return &ResolvedType{
Builtin: &glibType,
Import: "github.com/gotk3/gotk3/glib",
Package: "externglib",
CType: typ.CType,
Ptr: countPtrs(typ, nil),
Ptr: uint8(ptrs),
}
}

Expand Down Expand Up @@ -233,7 +235,7 @@ func (ng *NamespaceGenerator) ResolveType(typ gir.Type) *ResolvedType {

// Add the import in the same singleflight callback, but only if the
// namespace is not the current one.
if resolved.Extern != nil && !resolved.Extern.Result.Eq(ng.current) {
if resolved.Import != "" && resolved.Import != ng.pkgPath {
ng.addImportAlias(resolved.Import, resolved.Package)
}
}
Expand Down Expand Up @@ -293,25 +295,23 @@ func (ng *NamespaceGenerator) resolveTypeUncached(typ gir.Type) *ResolvedType {
case "gpointer":
return builtinType("unsafe", "Pointer", typ)
case "GLib.DestroyNotify", "DestroyNotify": // This should be handled externally.
return builtinType("unsafe.Pointer", typ)
return builtinType("unsafe", "Pointer", typ)
case "GType":
return builtinType("glib.Type", typ)
return externGLibType("Type", typ)
case "GObject.GValue", "GObject.Value": // inconsistency???
return builtinType("*glib.Value", typ)
return externGLibType("*Value", typ)
case "GObject.Object":
return builtinType("*glib.Object", typ)
return externGLibType("*Object", typ)
case "GObject.Closure":
return builtinType("*glib.Closure", typ)
return externGLibType("*Closure", typ)
case "GObject.Callback":
// Callback is a special func(Any) Any type, so we treat it as
// interface{} similarly to object.Connect(). We can use glib's Closure
// APIs to parse this interface{}.
return builtinType("interface{}", typ)
return builtinType("", "interface{}", typ)

case "GObject.InitiallyUnowned":
resolved := builtinType("glib.InitiallyUnowned", typ)
// resolved.Parent = "GObject.Object"
return resolved
return externGLibType("InitiallyUnowned", typ)

case "va_list":
// CGo cannot handle variadic argument lists.
Expand Down Expand Up @@ -345,7 +345,7 @@ func (ng *NamespaceGenerator) resolveTypeUncached(typ gir.Type) *ResolvedType {
return nil
}

return typeFromResult(typ, result)
return typeFromResult(ng.gen, typ, result)
}

// TODO: GoTypeConverter converts Go types to C with GIR type.
Expand Down Expand Up @@ -498,7 +498,7 @@ func (ng *NamespaceGenerator) _typeConverter(value, target string, typ gir.Type,
return ""
}

resolved := typeFromResult(typ, result)
resolved := typeFromResult(ng.gen, typ, result)
goType := resolved.GoType(false)

// Resolve alias.
Expand Down
48 changes: 24 additions & 24 deletions pkg/cairo/cairo.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions pkg/freetype2/freetype2.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 470cbd9

Please sign in to comment.