types

package
v0.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 29, 2022 License: AGPL-3.0 Imports: 13 Imported by: 13

Documentation

Index

Constants

View Source
const InternalImportPath = "github.com/diamondburned/gotk4/pkg/core"

InternalImportPath is the path to the core import path.

Variables

View Source
var BuiltinHandledTypes = []FilterMatcher{

	AbsoluteFilter("GLib.Error"),

	RegexFilter("GObject..*"),

	AbsoluteFilter("*.long double"),

	AbsoluteFilter("C.intern"),

	RegexFilter(`C.g_byte_array_.*`),

	AbsoluteFilter("GLib.Type"),
}

BuiltinHandledTypes contains types manually handled by Resolve and typeconv.Converter, as well as types that are never supposed to be handled.

View Source
var UnsupportedCTypes = []string{
	"tm",
	"va_list",
}

UnsupportedCTypes is the list of unsupported C types, either because it is not yet supported or will never be supported due to redundancy or else.

Functions

func AnyTypeC

func AnyTypeC(any gir.AnyType) string

AnyTypeC returns the C type for a GIR AnyType. An empty string is returned if none is made.

func AnyTypeCGo

func AnyTypeCGo(any gir.AnyType) string

AnyTypeCGo returns the CGo type for a GIR AnyType. An empty string is returned if none is made.

func AnyTypeIsPtr

func AnyTypeIsPtr(any gir.AnyType) bool

AnyTypeIsPtr returns true if the AnyType contains a pointer.

func AnyTypeIsVoid

func AnyTypeIsVoid(any gir.AnyType) bool

AnyTypeIsVoid returns true if AnyType is a void type.

func ApplyPreprocessors

func ApplyPreprocessors(repos gir.Repositories, preprocs []Preprocessor)

ApplyPreprocessors applies the given list of preprocessors onto the given list of GIR repositories.

func CGoTypeFromC

func CGoTypeFromC(cType string) string

CGoTypeFromC converts a C type to a CGo type.

func CTypeFallback

func CTypeFallback(c, gir string) string

CTypeFallback returns the C type OR the GIR type if it's empty.

func CleanCType

func CleanCType(cType string, stripPtr bool) string

CleanCType cleans the underlying C type of and special keywords for comparison.

func CountPtr

func CountPtr(typ string) int

CountPtr counts the number of pointers in the given type string. If the string contains "[]", then the pointer is counted up to that.

func EnsureNamespace

func EnsureNamespace(nsp *gir.NamespaceFindResult, girType string) string

EnsureNamespace ensures that exported, non-primitive types have the namespace prepended. This is useful for matching hard-coded types.

func EqNamespace

func EqNamespace(nsp, girType string) (typ string, ok bool)

EqNamespace is used for FilterMatchers to compare types and namespaces.

func Filter

func Filter(gen FileGenerator, gir, c string) (omit bool)

Filter returns true if the given GIR and/or C type should be omitted from the given generator.

func FilterCType

func FilterCType(gen FileGenerator, c string) (omit bool)

FilterCType filters only the C type or identifier. It is useful for filtering C functions and such.

func FilterField

func FilterField(gen FileGenerator, parent string, field *gir.Field) (omit bool)

FilterField filters a field similarly to Filter.

func FilterMethod

func FilterMethod(gen FileGenerator, parent string, method *gir.Method) (omit bool)

FilterMethod filters a method similarly to Filter.

func FilterSub

func FilterSub(gen FileGenerator, parent, sub, cType string) (omit bool)

FilterSub filters a field or method inside a parent.

func Find

func Find(gen FileGenerator, girType string) *gir.TypeFindResult

Find finds the given GIR type from the given generator.

func FindMethodName added in v0.0.3

func FindMethodName(methods []gir.Method, name string) *gir.Method

FindMethodName finds from the method the given name.

func FindParameter

func FindParameter(c *gir.CallableAttrs, paramName string) *gir.ParameterAttrs

FindParameter finds a parameter.

func FindParameterFromSlice added in v0.0.3

func FindParameterFromSlice(params []gir.Parameter, paramName string) *gir.ParameterAttrs

FindParameter finds a parameter from a slice of gir.Parameters.

func GIRBuiltinGo

func GIRBuiltinGo(typ string) string

GIRPrimitiveGo returns Go built-in types (primitive types and string). It returns an empty string if there's none.

func GIRPrimitiveGo

func GIRPrimitiveGo(typ string) string

GIRPrimitiveGo returns Go primitive types that can be copied by-value without doing any pointer work. It returns an empty string if there's none.

func GoAnyType

func GoAnyType(gen FileGenerator, any gir.AnyType, pub bool) (string, bool)

GoAnyType generates the Go type signature for the AnyType union. An empty string returned is an invalid type. If pub is true, then the returned string will use public interface types for classes instead of implementation types.

func GoImplType

func GoImplType(gen FileGenerator, resolved *Resolved) string

GoImplType is a convenient function around ResolvedType.ImplType.

func GoPublicType

func GoPublicType(gen FileGenerator, resolved *Resolved) string

GoPublicType is a convenient function around ResolvedType.ImplType.

func GoType

func GoType(gen FileGenerator, typ gir.Type, pub bool) (string, bool)

GoType is a convenient function that wraps around ResolveType and returns the Go type.

func GuessParameterOutput

func GuessParameterOutput(param *gir.Parameter) string

GuessParameterOutput guesses the parameter output using various clues to make up for GIR's painful shortcomings.

func IsGpointer

func IsGpointer(ctype string) bool

IsGpointer returns true if the given type is a gpointer or a pointer to it.

func IsObjectorMethod

func IsObjectorMethod(goName string) bool

func MakePathMatcher added in v0.0.3

func MakePathMatcher(inputs []string) func(string) bool

MakePathMatcher makes a matcher from the given file name inputs.

func MoveCPtr

func MoveCPtr(orig, into string) string

MoveCPtr moves the same number of pointers from the given orig string into another string as prefix, for C types.

func MovePtr

func MovePtr(orig, into string) string

MovePtr moves the same number of pointers from the given orig string into another string.

func MoveTypePtr

func MoveTypePtr(src, dst gir.Type) *gir.Type

MoveTypePtr moves the pointer from src to dst. It overrides dst's pointer. A copy of dst is returned.

func RecordHasFree

func RecordHasFree(record *gir.Record) *gir.Method

RecordHasFree returns the free/unref method if it has one.

func RecordHasRef

func RecordHasRef(record *gir.Record) *gir.Method

RecordHasRef returns the ref method if it has one.

func RecordHasUnref

func RecordHasUnref(record *gir.Record) *gir.Method

RecordHasUnref returns the unref method if it has one.

func RecordIsOpaque

func RecordIsOpaque(rec gir.Record) bool

RecordIsOpaque returns true if the record has no fields in the GIR schema. These records must always be referenced using a pointer.

func RecordPrintFree added in v0.0.3

func RecordPrintFree(record *gir.Record, value string) string

RecordPrintFree prints the call to the record's free function OR an empty string.

func RecordPrintFreeMethod added in v0.0.3

func RecordPrintFreeMethod(method *gir.Method, value string) string

RecordPrintFreeMethod generates a call with 1 argument for either free or unref. If method is nil, then a C.free call is generated. Value is assumed to be an unsafe.Pointer.

func ResolveAnyType added in v0.0.3

func ResolveAnyType(gen FileGenerator, any gir.AnyType) gir.AnyType

ResolveAnyType returns a copy of gir.AnyType containg the C type fields filled.

func ResolveParameters added in v0.0.3

func ResolveParameters(gen FileGenerator, params []gir.Parameter) []gir.Parameter

ResolveParameters puts each parameter through the type resolver and fill up any missing CType using ResolveAnyType. The returned slice is a copy.

func ReturnIsVoid

func ReturnIsVoid(ret *gir.ReturnValue) bool

ReturnIsVoid returns true if the return type is void.

func TypeCGo

func TypeCGo(typ *gir.Type) string

TypeCGo is a helper function that invokes AnyTypeCGo.

func TypeIsInFile

func TypeIsInFile(typ interface{}, file string) bool

TypeIsInFile returns true if the given type was declared in the given filename. The filename shouldn't contain the file extension.

Types

type FileGenerator

type FileGenerator interface {
	logger.LineLogger
	// CanGenerate checks if a type is going to be generated or not. It is used
	// primarily during type resolving.
	CanGenerate(*Resolved) bool
	// Filters returns the list of matchers that the current generator has.
	Filters() []FilterMatcher
	// ModPath crafts an import path from the given GIR namespace. The import
	// path is assumed to have the same package name as the base file, but major
	// versions are exempted as an edge case.
	ModPath(*gir.Namespace) string
	// Repositories returns the list of known repositories inside the generator.
	Repositories() gir.Repositories
	// Namespace returns the generator's current namespace.
	Namespace() *gir.NamespaceFindResult
}

FileGenerator defines a generator instance.

func OverrideNamespace

func OverrideNamespace(gen FileGenerator, nsp *gir.NamespaceFindResult) FileGenerator

OverrideNamespace returns a new generator that overrides a generator's current namespace.

type FilterMatcher

type FilterMatcher interface {
	// Filter matches for the girType within the given namespace from the
	// namespace generator. The GIR type will never have a namespace prefix.
	Filter(gen FileGenerator, gir, c string) (omit bool)
}

FilterMatcher describes a filter for a GIR type.

func AbsoluteFilter

func AbsoluteFilter(abs string) FilterMatcher

AbsoluteFilter matches the names absolutely.

func FileFilter

func FileFilter(contains string) FilterMatcher

FileFilter filters based on the source position.

func RegexFilter

func RegexFilter(matcher string) FilterMatcher

RegexFilter returns a regex filter for FilterMatcher. A regex filter's format is a string consisting of two parts joined by a period: a namespace and a matcher. The only regex part is the matcher.

type ImplImporter

type ImplImporter interface {
	ImportImpl(*Resolved)
}

ImplImporter is an interface that describes file.Header.

type ModulePathFunc

type ModulePathFunc func(*gir.Namespace) string

ModulePathFunc returns the Go module import path from the given namespace. See Generator.ModPath for mroe information.

type Preprocessor

type Preprocessor interface {
	// Preprocess goes over the given list of repos, changing what's necessary.
	Preprocess(repos gir.Repositories)
}

Preprocessor describes something that can preprocess anything in the given list of repositories. This is useful for renaming functions, classes or anything else.

func ModifyCallable

func ModifyCallable(girType string, f func(c *gir.CallableAttrs)) Preprocessor

ModifyCallable is a preprocessor that modifies an existing callable. It only does Function or Callback.

func ModifyParamDirections

func ModifyParamDirections(girType string, dirOverrides map[string]string) Preprocessor

ModifyParamDirections wraps ModifyCallable to conveniently override the parameters' directions.

func ModifySignal added in v0.0.3

func ModifySignal(girType string, f func(c *gir.Signal)) Preprocessor

ModifySignal is like ModifyCallable, except it only works on signals from classes and interfaces. The GIR type must be "package.class::signal-name".

func MustIntrospect added in v0.0.3

func MustIntrospect(girType string) Preprocessor

MustIntrospect forces the given type to be introspectable.

func PreserveGetName

func PreserveGetName(girType string) Preprocessor

PreserveGetName matches a type and prepends "get_" or "Get" into it to preserve the getter name in case of collision.

func RemoveCIncludes

func RemoveCIncludes(girFile string, cincls ...string) Preprocessor

RemoveCIncludes removes the given C includes from the given repository.

func RemovePkgconfig added in v0.0.3

func RemovePkgconfig(girFile string, pkgs ...string) Preprocessor

RemovePkgconfig removes the given pkgconfig packages from the given repository.

func RemoveRecordFields

func RemoveRecordFields(girType string, fields ...string) Preprocessor

RemoveRecordFields removes the given fields from the record with the given full GIR type. The fields must be cased as they appear in the GIR file.

func RenameCallable

func RenameCallable(girType, newName string) Preprocessor

RenameCallable renames a callable using ModifyCallable.

func RenameEnumMembers

func RenameEnumMembers(enum, regex, replace string) Preprocessor

RenameEnumMembers renames all members of the matched enums. It is primarily used to avoid collisions.

func TypeRenamer

func TypeRenamer(girType, newName string) Preprocessor

TypeRenamer creates a new filter matcher that renames a type. The given GIR type must contain the versioned namespace, like "Gtk3.Widget" but the given name must not. The GIR type is absolutely matched, similarly to AbsoluteFilter.

type PreprocessorFunc

type PreprocessorFunc func(gir.Repositories)

PreprocessorFunc is a helper function to satisfy the Preprocessor interface.

func (PreprocessorFunc) Preprocess

func (f PreprocessorFunc) Preprocess(repos gir.Repositories)

Preprocess calls f.

type Resolved

type Resolved struct {
	// either or
	Extern  *gir.TypeFindResult // optional
	Builtin *string             // optional
	Aliased *Resolved           // optional

	PublImport ResolvedImport
	ImplImport ResolvedImport

	CType string
	GType string
	Ptr   uint8 // used ONLY for the Go type.
}

Resolved is a resolved type from a given gir.Type.

func BuiltinType

func BuiltinType(imp, typ string, girType gir.Type) *Resolved

BuiltinType is a convenient function to make a new built-in *Resolved.

func Resolve

func Resolve(gen FileGenerator, typ gir.Type) *Resolved

Resolve resolves the given type from the GIR type field. It returns nil if the type is not known. It does not recursively traverse the type.

func ResolveName

func ResolveName(gen FileGenerator, girType string) *Resolved

ResolveName resolves the given GIR type name. The resolved type will always have no pointer.

func TypeFromResult

func TypeFromResult(gen FileGenerator, v interface{}) *Resolved

TypeFromResult is meant to be used by an external package to generate a Resolved from existing type information.

func (*Resolved) CGoType

func (typ *Resolved) CGoType() string

CGoType returns the CGo type. Its pointer count does not follow Ptr.

func (*Resolved) CanCast

func (typ *Resolved) CanCast(gen FileGenerator) bool

CanCast returns true if the resolved type is a builtin type that can be directly casted to an equivalent C type OR a record..

func (*Resolved) CanNil

func (typ *Resolved) CanNil() bool

CanNil returns true if the Go type can be nil.

func (*Resolved) FullGType deprecated

func (typ *Resolved) FullGType() string

FullGType returns the GType with the namespace.

Deprecated: Use typ.GType.

func (*Resolved) HasImport

func (typ *Resolved) HasImport() bool

HasImport returns true if the ResolvedType has an import.

func (*Resolved) HasPointer

func (typ *Resolved) HasPointer(gen FileGenerator) bool

HasPointer returns true if the type being resolved has a pointer. This is useful for array passing from Go memory to C memory.

func (*Resolved) ImplName

func (typ *Resolved) ImplName() string

ImplName returns the implementation type name.

func (*Resolved) ImplType

func (typ *Resolved) ImplType(needsNamespace bool) string

ImplType returns the implementation type. This is only different to PublicType as far as classes go: the returned type is the unexported implementation type.

func (*Resolved) IsAbstract

func (typ *Resolved) IsAbstract() bool

IsAbstract returns true if the resolved type is an interface or an abstract class.

func (*Resolved) IsAbstractClass added in v0.0.3

func (typ *Resolved) IsAbstractClass() bool

IsAbstractClass returns true if the resolved type is an abstract class.

func (*Resolved) IsBuiltin

func (typ *Resolved) IsBuiltin(builtin string) bool

IsBuiltin is a convenient function to compare the builtin type.

func (*Resolved) IsCallback

func (typ *Resolved) IsCallback() bool

IsCallback returns true if the current ResolvedType is a callback.

func (*Resolved) IsClass

func (typ *Resolved) IsClass() bool

IsClass returns true if the current ResolvedType is a class.

func (*Resolved) IsContainerBuiltin

func (typ *Resolved) IsContainerBuiltin() bool

IsContainerBuiltin returns true if the resolved type is a built-in Go container type (like string, error or interface{}).

func (*Resolved) IsEnumOrBitfield added in v0.0.3

func (typ *Resolved) IsEnumOrBitfield() bool

IsEnumOrBitfield returns true if the resolved type is an external enum or bitfield type.

func (*Resolved) IsExternGLib

func (typ *Resolved) IsExternGLib(glibType string) bool

IsExternGLib checks that the ResolvedType is exactly the gotk3/glib type with the given name. Pointers are not compared.

func (*Resolved) IsForeignRecord added in v0.0.3

func (typ *Resolved) IsForeignRecord() bool

IsForeignRecord returns true if the current ResolvedType is a foreign record.

func (*Resolved) IsGpointer

func (typ *Resolved) IsGpointer() bool

IsGpointer returns true if the given type is a gpointer type.

func (*Resolved) IsInterface

func (typ *Resolved) IsInterface() bool

IsInterface returns true if the current ResolvedType is an interface.

func (*Resolved) IsPrimitive

func (typ *Resolved) IsPrimitive() bool

IsPrimitive returns true if the resolved type is a builtin type that can be directly casted to an equivalent C type OR a record.

func (*Resolved) IsRecord

func (typ *Resolved) IsRecord() bool

IsRecord returns true if the current ResolvedType is a record.

func (*Resolved) IsUnion added in v0.0.3

func (typ *Resolved) IsUnion() bool

IsUnion returns true if the current ResolvedType is a union.

func (*Resolved) Name

func (typ *Resolved) Name() string

Name returns the type name without the namespace or pointer.

func (*Resolved) NeedsNamespace

func (typ *Resolved) NeedsNamespace(current *gir.NamespaceFindResult) bool

NeedsNamespace returns true if the returned Go type needs a namespace to be referenced properly.

func (*Resolved) PublicIsInterface

func (typ *Resolved) PublicIsInterface() bool

func (*Resolved) PublicName

func (typ *Resolved) PublicName() string

PublicName returns the public type name.

func (*Resolved) PublicType

func (typ *Resolved) PublicType(needsNamespace bool) string

PublicType returns the public type. If the resolved type is a class, then the interface type is returned.

func (*Resolved) Underlying added in v0.0.3

func (typ *Resolved) Underlying() *Resolved

Underlying returns itself OR the alias' resolved type iff there's one.

func (*Resolved) UnderlyingExtern added in v0.0.3

func (typ *Resolved) UnderlyingExtern() *gir.TypeFindResult

UnderlyingExtern returns the extern type OR the alias' extern type iff there's one. Nil is returned if neither.

func (*Resolved) WrapName

func (typ *Resolved) WrapName(needsNamespace bool) string

WrapName returns the name of the wrapper function. It only works for external types; calling this on a built-in ResolvedType will return an empty string.

type ResolvedImport

type ResolvedImport struct {
	Path    string // full path
	Package string // package name, import alias
}

ResolvedImport is a single import for the resolved type.

type Tree

type Tree struct {
	*Resolved

	// Requires contains the direct dependencies of the current type. It may
	// contain interfaces that are also in other interfaces, which will not
	// build.
	Requires []Tree
	// contains filtered or unexported fields
}

Tree is a structure for a type that is resolved to the lowest level of inheritance.

func NewTree

func NewTree(gen FileGenerator) Tree

NewTree creates a new empty type tree for resolving.

func (*Tree) AmbiguousSelectorTypes added in v0.0.3

func (tree *Tree) AmbiguousSelectorTypes() []*Resolved

AmbiguousSelectorTypes scans the whole tree and finds all types whose selectors are ambiguous if directly referenced from the top-level type.

func (*Tree) FirstGObjectSelector

func (tree *Tree) FirstGObjectSelector(v string) string

FirstGObjectSelector returns the selector path to the firts GObject field. The returning selector should have the *Object type. The selectors will all use the implementation type for the name.

If the object does not contain the Object field somewhere, then "nil" is returned.

func (*Tree) ImplInterfaces added in v0.0.3

func (tree *Tree) ImplInterfaces() []*Resolved

ImplInterfaces returns the sorted list of all Go interfaces that this type implements. The namespaces are appropriately prepended if needed.

At the moment, the method will only care about abstract classes.

func (*Tree) ImplTypes

func (tree *Tree) ImplTypes() []string

ImplTypes returns the sorted list of the toplevel type's children as Go implementation type names. The namespaces are appropriately prepended if needed.

func (*Tree) Reset

func (tree *Tree) Reset()

func (*Tree) Resolve

func (tree *Tree) Resolve(toplevel string) bool

Resolve resolves the given toplevel type into the Tree, overriding the Resolved and Requires fields. True is returned if the tree is successfully resolved.

func (*Tree) ResolveFromType

func (tree *Tree) ResolveFromType(toplevel *Resolved) bool

ResolveFromType is like Resolve, but the caller directly supplies the resolved top-level type.

func (*Tree) Walk

func (tree *Tree) Walk(f func(t *Tree, root bool) (traversed []Tree))

Walk walks the tree recursively on what the callback returns. The callback will be called on the root (receiver) tree; it should then return the list of parents of that tree. This process is then repeated for each tree returned.

Example:

tree.Walk(func(t *Tree, root bool) []Tree {
    log.Println("currently at", t.Resolved.PublName())
    return t.Requires
})

func (*Tree) WithoutGObject

func (tree *Tree) WithoutGObject() []Tree

WithoutGObject returns the Requires list without the GObject item.

func (*Tree) Wrap

func (tree *Tree) Wrap(obj string, h ImplImporter) string

Wrap generates the wrapper for the implementation struct.

func (*Tree) WrapInNamespace

func (tree *Tree) WrapInNamespace(obj string, h ImplImporter, n *gir.NamespaceFindResult) string

WrapInNamespace wraps with the given current namespace.

Directories

Path Synopsis
Package typeconv provides conversions between C and Go types.
Package typeconv provides conversions between C and Go types.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL