-
Notifications
You must be signed in to change notification settings - Fork 21
/
gir.go
630 lines (527 loc) · 15.3 KB
/
gir.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
// Package gir provides GIR types, as well as functions that parse GIR files.
//
// For reference, see
// https://gitlab.gnome.org/GNOME/gobject-introspection/-/blob/HEAD/docs/gir-1.2.rnc.
package gir
import (
"fmt"
"log"
"os"
"path"
"path/filepath"
"strconv"
"strings"
"sync"
"unicode"
"github.com/diamondburned/gotk4/gir/pkgconfig"
"golang.org/x/sync/singleflight"
)
// ImportPath generates the full import path from the package root.
func ImportPath(root, pkgPath string) string {
return path.Join(root, pkgPath)
}
// goPackageNameTrans describes the transformation checks to filter out runes.
var goPackageNameTrans = []func(r rune) bool{
unicode.IsLetter,
unicode.IsDigit,
}
// GoPackageName converts a GIR package name to a Go package name. It's only
// tested against a known set of GIR files.
func GoPackageName(girPkgName string) string {
return strings.Map(func(r rune) rune {
for _, trans := range goPackageNameTrans {
if trans(r) {
return unicode.ToLower(r)
}
}
return -1
}, girPkgName)
}
// GoNamespace converts a namespace's name to a Go package name.
func GoNamespace(namespace *Namespace) string {
return GoPackageName(namespace.Name)
}
// SplitGIRType splits the given GIR type string into 2 parts: the namespace,
// which preceeds the period, and the type name. If there is no period, then an
// empty string is returned for namespace.
func SplitGIRType(typ string) (namespace, typeName string) {
parts := strings.SplitN(typ, ".", 2)
if len(parts) == 2 {
return parts[0], parts[1]
}
return "", parts[0]
}
// MajorVersion returns the major version number only.
func MajorVersion(version string) string {
parts := strings.SplitN(version, ".", 2)
return parts[0]
}
// EqVersion compares major versions.
func EqVersion(v1, v2 string) bool { return MajorVersion(v1) == MajorVersion(v2) }
// VersionedNamespace returns the versioned name for the given namespace.
func VersionedNamespace(namespace *Namespace) string {
return VersionedName(namespace.Name, namespace.Version)
}
// VersionedName returns the name appended with the version suffix.
func VersionedName(name, version string) string {
return name + "-" + MajorVersion(version)
}
// ParseVersionName parses the given full namespace to return the original name
// and the version separately. If no versions are available, then the empty
// string is returned.
func ParseVersionName(fullNamespace string) (name, majorVersion string) {
parts := strings.SplitN(fullNamespace, "-", 2)
if len(parts) == 0 {
return "", ""
}
if len(parts) == 1 {
return parts[0], ""
}
// Trim the minor versions off.
version := MajorVersion(parts[1])
// Verify the number is valid.
_, err := strconv.Atoi(version)
if err != nil {
log.Panicf("version %q is invalid int", version)
}
return parts[0], version
}
// Repositories contains a list of known repositories.
type Repositories []PkgRepository
// PkgRepository wraps a Repository to add additional information.
type PkgRepository struct {
Repository
Pkg string // arg for pkg-config
Path string // gir file path
}
// FindGIRFiles finds gir files from the given list of pkgs.
func FindGIRFiles(pkgs ...string) ([]string, error) {
girDirs, err := pkgconfig.GIRDirs(pkgs...)
if err != nil {
return nil, err
}
visited := make(map[string]struct{}, len(girDirs))
var girFiles []string
// Iterate over `pkgs` instead of over `girDirs` directly, so
// that the order of `girFiles` is predictable based on order
// of the input arguments. At least this was important to me
// for debugability, but I feel like I had another reason that
// I no longer remember.
for _, pkg := range pkgs {
girDir := girDirs[pkg]
// Avoid visiting the same directory twice. Sorting +
// slices.Compact(pkgs) can't save us because multiple
// pkgs might have the same girDir.
if _, ok := visited[girDir]; ok {
continue
}
visited[girDir] = struct{}{}
dirEnts, err := os.ReadDir(girDir)
if err != nil {
return nil, err
}
for _, dirEnt := range dirEnts {
if filepath.Ext(dirEnt.Name()) == ".gir" {
girFiles = append(girFiles, filepath.Join(girDir, dirEnt.Name()))
}
}
}
return girFiles, nil
}
// AddSelected adds a single package but only searches for the given list of
// GIR files.
func (repos *Repositories) AddSelected(pkg string, wantedNames []string) error {
found := make([]string, 0, len(wantedNames))
filter := func(r *Repository) bool {
namespaces := r.Namespaces
r.Namespaces = namespaces[:0]
for _, namespace := range namespaces {
vname := VersionedNamespace(&namespace)
for _, wantedName := range wantedNames {
if wantedName != vname {
continue
}
r.Namespaces = append(r.Namespaces, namespace)
found = append(found, vname)
break
}
}
return len(r.Namespaces) > 0
}
girs, err := FindGIRFiles(pkg)
if err != nil {
return fmt.Errorf("failed to get gir files for %q: %w", pkg, err)
}
for _, gir := range girs {
repo, err := ParseRepository(gir)
if err != nil {
return fmt.Errorf("failed to parse file %q: %w", gir, err)
}
if !filter(repo) {
continue
}
if err := repos.add(*repo, pkg, gir); err != nil {
return fmt.Errorf("failed to add file %q: %w", gir, err)
}
}
if len(found) != len(wantedNames) {
return fmt.Errorf("wanted %v but only got %v", wantedNames, found)
}
return nil
}
// Add finds the given pkg name to be searched using pkg-config and added into
// the list of repositories.
func (repos *Repositories) Add(pkg string) error {
girs, err := FindGIRFiles(pkg)
if err != nil {
return fmt.Errorf("failed to get gir files for %q: %w", pkg, err)
}
for _, gir := range girs {
repo, err := ParseRepository(gir)
if err != nil {
return fmt.Errorf("failed to parse file %q: %w", gir, err)
}
if err := repos.add(*repo, pkg, gir); err != nil {
return fmt.Errorf("failed to add file %q: %w", gir, err)
}
}
return nil
}
// add adds the given PkgRepository.
func (repos *Repositories) add(r Repository, pkg, path string) error {
for _, repo := range *repos {
for _, repoNsp := range repo.Namespaces {
for _, addingNsp := range r.Namespaces {
if addingNsp.Name == repoNsp.Name && EqVersion(addingNsp.Version, repoNsp.Version) {
return nil
}
}
}
}
*repos = append(*repos, PkgRepository{
Repository: r,
Pkg: pkg,
Path: path,
})
return nil
}
// FromGIRFile finds the repository from the given GIR filename.
func (repos Repositories) FromGIRFile(girFile string) *PkgRepository {
for i, repo := range repos {
if filepath.Base(repo.Path) == girFile {
return &repos[i]
}
}
return nil
}
// FromPkg finds the repository from the given package name.
func (repos Repositories) FromPkg(pkg string) []PkgRepository {
var found []PkgRepository
for i, repo := range repos {
if repo.Pkg == pkg {
found = append(found, repos[i])
}
}
return found
}
// NamespaceFindResult is the result returned from FindNamespace.
type NamespaceFindResult struct {
Repository *PkgRepository
Namespace *Namespace
}
// Versioned returns the versioned namespace name.
func (res *NamespaceFindResult) Versioned() string {
return VersionedNamespace(res.Namespace)
}
// Eq compares that the resulting namespace's name and version match.
func (res *NamespaceFindResult) Eq(other *NamespaceFindResult) bool {
if other == res {
return true
}
return true &&
res.Namespace.Name == other.Namespace.Name &&
res.Namespace.Version == other.Namespace.Version &&
res.Repository.Pkg == other.Repository.Pkg &&
res.Repository.Version == other.Repository.Version
}
// FindNamespace finds the repository and namespace with the given name and
// version. If name doesn't have the version bits, then it panics.
func (repos Repositories) FindNamespace(name string) *NamespaceFindResult {
name, version := ParseVersionName(name)
if version == "" {
panic("FindNamespace given namespace unversioned: " + name)
}
for i := range repos {
repository := &repos[i]
for j := range repository.Namespaces {
namespace := &repository.Namespaces[j]
if namespace.Name != name || MajorVersion(namespace.Version) != version {
continue
}
return &NamespaceFindResult{
Repository: repository,
Namespace: namespace,
}
}
}
return nil
}
// TypeFindResult is the result
type TypeFindResult struct {
*NamespaceFindResult
// Types:
// *Alias
// *Class
// *Interface
// *Record
// *Enum
// *Function
// *Union
// *Bitfield
// *Callback
Type interface{}
// TODO: Constant, Annotations, Boxed
// TODO: Methods
}
// CType returns the resulting type's C type identifier.
func (res *TypeFindResult) CType() string {
return *res.cTypePtr()
}
// SetCType sets the type's name. Note that this will change the type inside the
// repositories as well.
func (res *TypeFindResult) SetCType(ctype string) {
*res.cTypePtr() = ctype
}
func (res *TypeFindResult) cTypePtr() *string {
switch v := res.Type.(type) {
case *Class:
if v.CType != "" {
return &v.CType
}
return &v.GLibTypeName
case *Interface:
if v.CType != "" {
return &v.CType
}
return &v.GLibTypeName
case *Alias:
return &v.CType
case *Record:
return &v.CType
case *Enum:
return &v.CType
case *Function:
return &v.CIdentifier
case *Union:
return &v.CType
case *Bitfield:
return &v.CType
case *Callback:
return &v.CIdentifier
}
panic("TypeFindResult has all fields nil")
}
func (res *TypeFindResult) namePtr() *string {
var typ *string
switch v := res.Type.(type) {
case *Alias:
typ = &v.Name
case *Class:
typ = &v.Name
case *Interface:
typ = &v.Name
case *Record:
typ = &v.Name
case *Enum:
typ = &v.Name
case *Function:
typ = &v.Name
case *Union:
typ = &v.Name
case *Bitfield:
typ = &v.Name
case *Callback:
typ = &v.Name
}
if typ == nil {
panic("TypeFindResult has all fields nil")
}
return typ
}
// Name returns the copy of the type's name.
func (res *TypeFindResult) Name() string {
return *res.namePtr()
}
// SetName sets the type's name. Note that this will change the type inside the
// repositories as well.
func (res *TypeFindResult) SetName(name string) {
*res.namePtr() = name
}
// NamespacedType returns the copy of the type's name with the namespace
// prepended to it.
func (res *TypeFindResult) NamespacedType() string {
return res.Namespace.Name + "." + res.Name()
}
// VersionedNamespaceType is like NamespacedType, but the returned type has the
// namespace and its version.
func (res *TypeFindResult) VersionedNamespaceType() string {
return VersionedNamespace(res.Namespace) + "." + res.Name()
}
// IsIntrospectable returns true if the type inside the result is
// introspectable.
func (res *TypeFindResult) IsIntrospectable() bool {
if res.Type == nil {
panic("TypeFindResult has all fields nil")
}
type isIntrospectabler interface {
IsIntrospectable() bool
}
return res.Type.(isIntrospectabler).IsIntrospectable()
}
// MustFindGIR generates a girepository.MustFind call for the TypeFindResult.
func (res *TypeFindResult) MustFindGIR() string {
return fmt.Sprintf("girepository.MustFind(%q, %q)", res.Namespace.Name, res.Name())
}
// FindInclude returns the namespace that the given namespace includes. It
// resolves imports recursively. This function is primarily used to ensure that
// proper versions are imported.
func (repos Repositories) FindInclude(
res *NamespaceFindResult, includes string,
) *NamespaceFindResult {
for _, incl := range res.Repository.Includes {
if incl.Name != includes {
continue
}
nspIncl := repos.FindNamespace(VersionedName(incl.Name, incl.Version))
if nspIncl == nil {
// Include found but not the namespace, so it's probably not added
// at all.
return nil
}
return nspIncl
}
for _, incl := range res.Repository.Includes {
nspIncl := repos.FindNamespace(VersionedName(incl.Name, incl.Version))
if nspIncl == nil {
continue
}
if res := repos.FindInclude(nspIncl, includes); res != nil {
return res
}
}
return nil
}
var (
typeResultCache sync.Map // full GIR type -> *TypeFindResult
typeResultFlight singleflight.Group
)
// FindType finds a type in the repositories from the given current namespace
// and the GIR type name. The function will cache the returned TypeFindResult.
func (repos Repositories) FindType(nsp *NamespaceFindResult, typ string) *TypeFindResult {
// Build the full type name for cache querying.
namespace, typName := SplitGIRType(typ)
// Ensure the new fullType string has the version.
if namespace != "" {
n, version := ParseVersionName(namespace)
if version != "" {
goto gotNamespace
}
// No versions provided; check if the namespace is the same (redundant).
if n == nsp.Namespace.Name {
namespace = VersionedNamespace(nsp.Namespace)
goto gotNamespace
}
if result := repos.FindInclude(nsp, n); result != nil {
namespace = VersionedNamespace(result.Namespace)
goto gotNamespace
}
// log.Panicf("namespace %q of type %q in %q not found", namespace, typ, nsp.Namespace.Name)
return nil
}
// Type comes from the same namespace. Append the current one.
namespace = VersionedNamespace(nsp.Namespace)
gotNamespace:
return repos.FindFullType(namespace + "." + typName)
}
// FindFullType finds a type from the given fullType string. The fullType string
// MUST have the namespace version, suck as Gdk-2.Item.
func (repos Repositories) FindFullType(fullType string) *TypeFindResult {
v, ok := typeResultCache.Load(fullType)
if ok {
return v.(*TypeFindResult)
}
v, _, _ = typeResultFlight.Do(fullType, func() (interface{}, error) {
result := repos.findFullType(fullType)
if result != nil {
typeResultCache.Store(fullType, result)
}
return result, nil
})
return v.(*TypeFindResult)
}
func (repos Repositories) findFullType(fullType string) *TypeFindResult {
vNamespace, typ := SplitGIRType(fullType)
if vNamespace == "" {
log.Panicf("type %q empty namespace", fullType)
}
r := TypeFindResult{NamespaceFindResult: repos.FindNamespace(vNamespace)}
if r.NamespaceFindResult == nil {
return nil
}
v := SearchNamespace(r.Namespace, func(name, _ string) bool { return name == typ })
if v == nil {
return nil
}
r.Type = v
return &r
}
// SearchNamespace searches the namespace for the given type name. The returned
// interface may be any of the types in TypeFindResult.
func SearchNamespace(namespace *Namespace, f func(typ, ctyp string) bool) interface{} {
for i, alias := range namespace.Aliases {
if f(alias.Name, alias.CType) {
return &namespace.Aliases[i]
}
}
for i, class := range namespace.Classes {
if f(class.Name, class.GLibTypeName) {
return &namespace.Classes[i]
}
}
for i, enum := range namespace.Enums {
if f(enum.Name, enum.CType) {
return &namespace.Enums[i]
}
}
for i, record := range namespace.Records {
if f(record.Name, record.CType) {
return &namespace.Records[i]
}
}
for i, function := range namespace.Functions {
if f(function.Name, function.CIdentifier) {
return &namespace.Functions[i]
}
}
for i, union := range namespace.Unions {
if f(union.Name, union.CType) {
return &namespace.Unions[i]
}
}
for i, bitfield := range namespace.Bitfields {
if f(bitfield.Name, bitfield.CType) {
return &namespace.Bitfields[i]
}
}
for i, callback := range namespace.Callbacks {
if f(callback.Name, callback.CIdentifier) {
return &namespace.Callbacks[i]
}
}
for i, iface := range namespace.Interfaces {
if f(iface.Name, iface.CType) {
return &namespace.Interfaces[i]
}
}
return nil
}