forked from go-gl/glow
-
Notifications
You must be signed in to change notification settings - Fork 1
/
spec.go
653 lines (585 loc) · 16.5 KB
/
spec.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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
package main
import (
"bytes"
"encoding/xml"
"fmt"
"io"
"os"
"regexp"
"strings"
)
type xmlRegistry struct {
Types []xmlType `xml:"types>type"`
Enums []xmlEnumSet `xml:"enums"`
Commands []xmlCommand `xml:"commands>command"`
Features []xmlFeature `xml:"feature"`
Extensions []xmlExtension `xml:"extensions>extension"`
}
type xmlType struct {
Name string `xml:"name,attr"`
API string `xml:"api,attr"`
Requires string `xml:"requires,attr"`
Raw []byte `xml:",innerxml"`
}
type xmlEnumSet struct {
Enums []xmlEnum `xml:"enum"`
}
type xmlEnum struct {
Name string `xml:"name,attr"`
Value string `xml:"value,attr"`
API string `xml:"api,attr"`
}
type xmlCommand struct {
Prototype xmlProto `xml:"proto"`
API string `xml:"api"`
Params []xmlParam `xml:"param"`
}
type xmlSignature []byte
type xmlProto struct {
Raw xmlSignature `xml:",innerxml"`
}
type xmlParam struct {
Raw xmlSignature `xml:",innerxml"`
}
type xmlFeature struct {
API string `xml:"api,attr"`
Number string `xml:"number,attr"`
Requires []xmlRequire `xml:"require"`
Removes []xmlRemove `xml:"remove"`
}
type xmlRequire struct {
Enums []xmlEnumRef `xml:"enum"`
Commands []xmlCommandRef `xml:"command"`
Profile string `xml:"profile,attr"`
}
type xmlRemove struct {
Enums []xmlEnumRef `xml:"enum"`
Commands []xmlCommandRef `xml:"command"`
Profile string `xml:"profile,attr"`
}
type xmlEnumRef struct {
Name string `xml:"name,attr"`
}
type xmlCommandRef struct {
Name string `xml:"name,attr"`
}
type xmlExtension struct {
Name string `xml:"name,attr"`
Supported string `xml:"supported,attr"`
Requires []xmlRequire `xml:"require"`
Removes []xmlRemove `xml:"remove"`
}
type specRef struct {
name string
api string
}
type specTypedef struct {
typedef *Typedef
ordinal int // Relative declaration order of the typedef
requires string // Optional name of the typedef required for this typedef
}
type specFunctions map[specRef]*Function
type specEnums map[specRef]*Enum
type specTypedefs map[specRef]*specTypedef
type specAddRemSet struct {
addedCommands []string
addedEnums []string
removedCommands []string
removedEnums []string
profile string
}
// A Specification is a parsed version of an XML registry.
type Specification struct {
Functions specFunctions
Enums specEnums
Typedefs specTypedefs
Features []SpecificationFeature
Extensions []SpecificationExtension
}
// A SpecificationFeature describes a set of commands and enums added and/or
// removed in the context of a particular API and version.
type SpecificationFeature struct {
API string
Version Version
AddRem []*specAddRemSet
}
// A SpecificationExtension describes a set of commands and enums added to
// implement an extension.
type SpecificationExtension struct {
Name string
APIRegexp *regexp.Regexp
AddRem []*specAddRemSet
}
func readSpecFile(file string) (*xmlRegistry, error) {
var registry xmlRegistry
f, err := os.Open(file)
if err != nil {
return nil, err
}
defer f.Close()
err = xml.NewDecoder(f).Decode(®istry)
if err != nil {
return nil, err
}
return ®istry, nil
}
func parseFunctions(commands []xmlCommand) (specFunctions, error) {
functions := make(specFunctions)
for _, cmd := range commands {
cmdName, cmdReturnType, err := parseSignature(cmd.Prototype.Raw)
if err != nil {
return functions, err
}
parameters := make([]Parameter, 0, len(cmd.Params))
for _, param := range cmd.Params {
paramName, paramType, err := parseSignature(param.Raw)
if err != nil {
return functions, err
}
parameter := Parameter{
Name: paramName,
Type: paramType}
parameters = append(parameters, parameter)
}
fnRef := specRef{cmdName, cmd.API}
functions[fnRef] = &Function{
Name: cmdName,
GoName: TrimAPIPrefix(cmdName),
Parameters: parameters,
Return: cmdReturnType}
}
return functions, nil
}
func parseOverloads(functions specFunctions, overloads xmlOverloads) (specFunctions, error) {
for _, overloadInfo := range overloads.Overloads {
function := functions.getByName(overloadInfo.Name)
if function == nil {
return nil, fmt.Errorf("function <%s> not found to overload", overloadInfo.Name)
}
err := overloadFunction(function, overloadInfo)
if err != nil {
return nil, err
}
}
return functions, nil
}
func overloadFunction(function *Function, info xmlOverload) error {
overload := Overload{
GoName: function.GoName,
OverloadName: info.OverloadName,
Parameters: make([]Parameter, len(function.Parameters)),
Return: function.Return,
}
copy(overload.Parameters, function.Parameters)
for _, change := range info.ParameterChanges {
if (change.Index < 0) || (change.Index >= len(function.Parameters)) {
return fmt.Errorf("overload for <%s> has invalid parameter index", info.Name)
}
param := &overload.Parameters[change.Index]
if change.Type != nil {
_, ctype, err := parseSignature(xmlSignature(change.Type.Signature))
if err != nil {
return fmt.Errorf("failed to parse signature of overload for <%s>: %v", info.Name, err)
}
// store original type definition as a cast, as this most likely will be needed.
param.Type.Cast = param.Type.CDefinition
param.Type.PointerLevel = ctype.PointerLevel
param.Type.Name = ctype.Name
param.Type.CDefinition = ctype.CDefinition
}
if change.Name != nil {
param.Name = change.Name.Value
}
}
function.Overloads = append(function.Overloads, overload)
return nil
}
func parseSignature(signature xmlSignature) (name string, ctype Type, err error) {
readingName := false
readingType := false
decoder := xml.NewDecoder(bytes.NewBuffer(signature))
for {
token, err := decoder.Token()
if err == io.EOF {
break
}
if err != nil {
return name, ctype, err
}
switch t := token.(type) {
case xml.CharData:
raw := strings.Trim(string(t), " ")
if readingName {
name = raw
} else if readingType {
ctype.Name = raw
}
// Even if we're not explicitly reading the type we're doing so implicitly
if !readingName {
ctype.CDefinition += string(t)
ctype.PointerLevel += strings.Count(raw, "*")
}
case xml.StartElement:
if t.Name.Local == "ptype" {
readingType = true
} else if t.Name.Local == "name" {
readingName = true
} else {
return name, ctype, fmt.Errorf("unexpected signature XML: %s", signature)
}
case xml.EndElement:
if t.Name.Local == "ptype" {
readingType = false
} else if t.Name.Local == "name" {
readingName = false
}
}
}
// If the XML did not call out the name then parse it out
if ctype.Name == "" {
cTypeName := ctype.CDefinition
cTypeName = strings.Replace(cTypeName, "const", "", -1)
cTypeName = strings.Replace(cTypeName, "*", "", -1)
cTypeName = strings.Replace(cTypeName, " ", "", -1)
ctype.Name = cTypeName
}
// Convert statically sized arrays to pointers
// FIXME: Preserve this type information
arrayRegexp := regexp.MustCompile("\\[.*]")
ctype.CDefinition = arrayRegexp.ReplaceAllStringFunc(
ctype.CDefinition,
func(_ string) string {
ctype.PointerLevel += 1
return "*"
})
return name, ctype, nil
}
func parseEnums(enumSets []xmlEnumSet) (specEnums, error) {
enums := make(specEnums)
for _, set := range enumSets {
for _, enum := range set.Enums {
enumRef := specRef{enum.Name, enum.API}
enums[enumRef] = &Enum{
Name: enum.Name,
GoName: TrimAPIPrefix(enum.Name),
Value: enum.Value}
}
}
return enums, nil
}
func parseTypedefs(types []xmlType) (specTypedefs, error) {
typedefs := make(specTypedefs)
for i, xtype := range types {
typedef, err := parseTypedef(xtype)
if err != nil {
return nil, err
}
typedefRef := specRef{typedef.Name, xtype.API}
typedefs[typedefRef] = &specTypedef{
typedef: typedef,
ordinal: i,
requires: xtype.Requires}
}
return typedefs, nil
}
func parseTypedef(xmlType xmlType) (*Typedef, error) {
typedef := &Typedef{
Name: xmlType.Name,
CDefinition: ""}
readingName := false
decoder := xml.NewDecoder(bytes.NewBuffer(xmlType.Raw))
for {
token, err := decoder.Token()
if err == io.EOF {
break
}
if err != nil {
return typedef, err
}
switch t := token.(type) {
case xml.CharData:
raw := string(t)
typedef.CDefinition += raw
if readingName {
typedef.Name = raw
}
case xml.StartElement:
if t.Name.Local == "name" {
readingName = true
} else if t.Name.Local == "apientry" {
typedef.CDefinition += "APIENTRY"
} else {
return typedef, fmt.Errorf("unexpected typedef XML: %s", xmlType.Raw)
}
case xml.EndElement:
if t.Name.Local == "name" {
readingName = false
}
default:
return typedef, fmt.Errorf("unexpected typedef XML: %s", xmlType.Raw)
}
}
return typedef, nil
}
func parseFeatures(xmlFeatures []xmlFeature) ([]SpecificationFeature, error) {
features := make([]SpecificationFeature, 0, len(xmlFeatures))
for _, xmlFeature := range xmlFeatures {
version, err := ParseVersion(xmlFeature.Number)
if err != nil {
return features, err
}
feature := SpecificationFeature{
API: xmlFeature.API,
Version: version,
AddRem: parseAddRem(xmlFeature.Requires, xmlFeature.Removes),
}
features = append(features, feature)
}
return features, nil
}
func parseAddRem(requires []xmlRequire, removes []xmlRemove) []*specAddRemSet {
addRemByProfile := make(map[string]*specAddRemSet)
addRemForProfile := func(profile string) *specAddRemSet {
addRem, ok := addRemByProfile[profile]
if !ok {
addRem = &specAddRemSet{
profile: profile,
addedEnums: make([]string, 0),
addedCommands: make([]string, 0),
removedEnums: make([]string, 0),
removedCommands: make([]string, 0),
}
addRemByProfile[profile] = addRem
}
return addRem
}
for _, req := range requires {
addRem := addRemForProfile(req.Profile)
for _, cmd := range req.Commands {
addRem.addedCommands = append(addRem.addedCommands, cmd.Name)
}
for _, enum := range req.Enums {
addRem.addedEnums = append(addRem.addedEnums, enum.Name)
}
}
for _, rem := range removes {
addRem := addRemForProfile(rem.Profile)
for _, cmd := range rem.Commands {
addRem.removedCommands = append(addRem.removedCommands, cmd.Name)
}
for _, enum := range rem.Enums {
addRem.removedEnums = append(addRem.removedEnums, enum.Name)
}
}
addRems := make([]*specAddRemSet, 0, len(addRemByProfile))
for _, addRem := range addRemByProfile {
addRems = append(addRems, addRem)
}
return addRems
}
func parseExtensions(xmlExtensions []xmlExtension) ([]SpecificationExtension, error) {
extensions := make([]SpecificationExtension, 0, len(xmlExtensions))
for _, xmlExtension := range xmlExtensions {
if len(xmlExtension.Removes) > 0 {
return nil, fmt.Errorf("unexpected extension with removal requirement: %s", xmlExtension)
}
extension := SpecificationExtension{
Name: xmlExtension.Name,
APIRegexp: regexp.MustCompile("^(" + xmlExtension.Supported + ")$"),
AddRem: parseAddRem(xmlExtension.Requires, xmlExtension.Removes),
}
extensions = append(extensions, extension)
}
return extensions, nil
}
func (functions specFunctions) get(name, api string) *Function {
function, ok := functions[specRef{name, api}]
if ok {
return function
}
return functions[specRef{name, ""}]
}
func (functions specFunctions) getByName(name string) *Function {
for key, function := range functions {
if key.name == name {
return function
}
}
return nil
}
func (enums specEnums) get(name, api string) *Enum {
enum, ok := enums[specRef{name, api}]
if ok {
return enum
}
return enums[specRef{name, ""}]
}
func (typedefs specTypedefs) selectRequired(name, api string, requiredTypedefs []*Typedef) {
specTypedef, ok := typedefs[specRef{name, api}]
if !ok {
specTypedef = typedefs[specRef{name, ""}]
}
if specTypedef != nil {
requiredTypedefs[specTypedef.ordinal] = specTypedef.typedef
if specTypedef.requires != "" {
typedefs.selectRequired(specTypedef.requires, api, requiredTypedefs)
}
}
}
func (feature SpecificationFeature) shouldInclude(pkgSpec *PackageSpec) bool {
// Ignore mismatched APIs
if pkgSpec.API != feature.API {
return false
}
// Ignore future versions (unless the version is not relevant)
if pkgSpec.Version.Compare(feature.Version) < 0 {
return false
}
return true
}
func (extension SpecificationExtension) shouldInclude(pkgSpec *PackageSpec) bool {
// User extension overrides take precedence
if pkgSpec.AddExtRegexp != nil && pkgSpec.AddExtRegexp.MatchString(extension.Name) {
return true
}
if pkgSpec.RemExtRegexp != nil && pkgSpec.RemExtRegexp.MatchString(extension.Name) {
return false
}
extensionAPI := pkgSpec.API
// Special case for GL core profile extension inclusion which uses a pseudo-API
if pkgSpec.API == "gl" && pkgSpec.Profile == "core" {
extensionAPI = "glcore"
}
return extension.APIRegexp.MatchString(extensionAPI)
}
func (addRem *specAddRemSet) shouldInclude(pkgSpec *PackageSpec) bool {
// Ignore mismatched profiles (unless the profile is not relevant)
if addRem.profile != pkgSpec.Profile && addRem.profile != "" {
return false
}
return true
}
// NewSpecification creates a new specification based on an XML registry.
func NewSpecification(registry xmlRegistry, overloads xmlOverloads) (*Specification, error) {
functions, err := parseFunctions(registry.Commands)
if err != nil {
return nil, err
}
functions, err = parseOverloads(functions, overloads)
if err != nil {
return nil, err
}
enums, err := parseEnums(registry.Enums)
if err != nil {
return nil, err
}
typedefs, err := parseTypedefs(registry.Types)
if err != nil {
return nil, err
}
features, err := parseFeatures(registry.Features)
if err != nil {
return nil, err
}
extensions, err := parseExtensions(registry.Extensions)
if err != nil {
return nil, err
}
spec := &Specification{
Functions: functions,
Enums: enums,
Typedefs: typedefs,
Features: features,
Extensions: extensions,
}
return spec, nil
}
// HasPackage determines whether the specification can generate the specified package.
func (spec *Specification) HasPackage(pkgSpec *PackageSpec) bool {
for _, feature := range spec.Features {
if pkgSpec.API == feature.API && pkgSpec.Version.Compare(feature.Version) == 0 {
return true
}
}
return false
}
// ToPackage generates a package from the specification.
func (spec *Specification) ToPackage(pkgSpec *PackageSpec) *Package {
pkg := &Package{
API: pkgSpec.API,
Name: pkgSpec.API,
Version: pkgSpec.Version,
Profile: pkgSpec.Profile,
TmplDir: pkgSpec.TmplDir,
Typedefs: make([]*Typedef, len(spec.Typedefs)),
Enums: make(map[string]*Enum),
Functions: make(map[string]*PackageFunction),
}
// Select the commands and enums relevant to the specified API version
for _, feature := range spec.Features {
if !feature.shouldInclude(pkgSpec) {
continue
}
for _, addRem := range feature.AddRem {
if !addRem.shouldInclude(pkgSpec) {
continue
}
for _, cmd := range addRem.addedCommands {
pkg.Functions[cmd] = &PackageFunction{
Function: *spec.Functions.get(cmd, pkg.API),
Required: !pkgSpec.LenientInit,
}
}
for _, enum := range addRem.addedEnums {
pkg.Enums[enum] = spec.Enums.get(enum, pkg.API)
}
if !pkg.Version.IsAll() {
for _, cmd := range addRem.removedCommands {
delete(pkg.Functions, cmd)
}
for _, enum := range addRem.removedEnums {
delete(pkg.Enums, enum)
}
}
}
}
// Select the extensions compatible with the specified API version
for _, extension := range spec.Extensions {
if !extension.shouldInclude(pkgSpec) {
continue
}
for _, addRem := range extension.AddRem {
if !addRem.shouldInclude(pkgSpec) {
continue
}
for _, cmd := range addRem.addedCommands {
_, ok := pkg.Functions[cmd]
if !ok {
pkg.Functions[cmd] = &PackageFunction{
Function: *spec.Functions.get(cmd, pkg.API),
Required: false,
}
}
}
for _, enum := range addRem.addedEnums {
pkg.Enums[enum] = spec.Enums.get(enum, pkg.API)
}
}
}
// Add the types necessary to declare the functions
for _, fn := range pkg.Functions {
spec.Typedefs.selectRequired(fn.Function.Return.Name, pkg.API, pkg.Typedefs)
for _, param := range fn.Function.Parameters {
spec.Typedefs.selectRequired(param.Type.Name, pkg.API, pkg.Typedefs)
}
}
typedefCount := 0
for _, typedef := range pkg.Typedefs {
if typedef != nil {
pkg.Typedefs[typedefCount] = typedef
typedefCount++
}
}
pkg.Typedefs = pkg.Typedefs[:typedefCount]
return pkg
}