aboutsummaryrefslogtreecommitdiff
path: root/nexgb/xgbgen/translation.go
blob: 534a3d4918af3c22a8218ca8a25f11c21f565b58 (plain)
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
package main

/*
translation.go provides a 'Translate' method on every XML type that converts
the XML type into our "better" representation.

i.e., the representation of Fields and Expressions is just too general.
We end up losing a lot of the advantages of static typing if we keep
the types that encoding/xml forces us into.
*/

import (
	"log"
	"regexp"
	"strconv"
	"strings"
)

func (xml *XML) Translate(parent *Protocol) *Protocol {
	protocol := &Protocol{
		Parent:       parent,
		Name:         xml.Header,
		ExtXName:     xml.ExtensionXName,
		ExtName:      xml.ExtensionName,
		MajorVersion: xml.MajorVersion,
		MinorVersion: xml.MinorVersion,

		Imports:  make([]*Protocol, 0),
		Types:    make([]Type, 0),
		Requests: make([]*Request, len(xml.Requests)),
	}

	for _, imp := range xml.Imports {
		if imp.xml != nil {
			protocol.Imports = append(protocol.Imports,
				imp.xml.Translate(protocol))
		}
	}

	for xmlName, srcName := range BaseTypeMap {
		newBaseType := &Base{
			srcName: srcName,
			xmlName: xmlName,
			size:    newFixedSize(BaseTypeSizes[xmlName], true),
		}
		protocol.Types = append(protocol.Types, newBaseType)
	}
	for _, enum := range xml.Enums {
		protocol.Types = append(protocol.Types, enum.Translate())
	}
	for _, xid := range xml.Xids {
		protocol.Types = append(protocol.Types, xid.Translate())
	}
	for _, xidunion := range xml.XidUnions {
		protocol.Types = append(protocol.Types, xidunion.Translate())
	}
	for _, typedef := range xml.TypeDefs {
		protocol.Types = append(protocol.Types, typedef.Translate())
	}
	for _, s := range xml.Structs {
		protocol.Types = append(protocol.Types, s.Translate())
	}
	for _, union := range xml.Unions {
		protocol.Types = append(protocol.Types, union.Translate())
	}
	for _, ev := range xml.Events {
		protocol.Types = append(protocol.Types, ev.Translate())
	}
	for _, evcopy := range xml.EventCopies {
		protocol.Types = append(protocol.Types, evcopy.Translate())
	}
	for _, err := range xml.Errors {
		protocol.Types = append(protocol.Types, err.Translate())
	}
	for _, errcopy := range xml.ErrorCopies {
		protocol.Types = append(protocol.Types, errcopy.Translate())
	}

	for i, request := range xml.Requests {
		protocol.Requests[i] = request.Translate()
	}

	// Now load all of the type and source name information.
	protocol.Initialize()

	// Make sure all enums have concrete values.
	for _, typ := range protocol.Types {
		enum, ok := typ.(*Enum)
		if !ok {
			continue
		}
		nextValue := 0
		for _, item := range enum.Items {
			if item.Expr == nil {
				item.Expr = &Value{v: nextValue}
				nextValue++
			} else {
				nextValue = item.Expr.Eval() + 1
			}
		}
	}

	return protocol
}

func (x *XMLEnum) Translate() *Enum {
	enum := &Enum{
		xmlName: x.Name,
		Items:   make([]*EnumItem, len(x.Items)),
	}
	for i, item := range x.Items {
		enum.Items[i] = &EnumItem{
			xmlName: item.Name,
			Expr:    item.Expr.Translate(),
		}
	}
	return enum
}

func (x *XMLXid) Translate() *Resource {
	return &Resource{
		xmlName: x.Name,
	}
}

func (x *XMLTypeDef) Translate() *TypeDef {
	return &TypeDef{
		xmlName: x.New,
		Old:     newTranslation(x.Old),
	}
}

func (x *XMLEvent) Translate() *Event {
	ev := &Event{
		xmlName:    x.Name,
		Number:     x.Number,
		NoSequence: x.NoSequence,
		Fields:     make([]Field, 0, len(x.Fields)),
		Doc:        x.Doc.Translate(),
	}
	for _, field := range x.Fields {
		ev.Fields = append(ev.Fields, field.Translate(ev, &ev.Doc))
	}
	return ev
}

func (x *XMLEventCopy) Translate() *EventCopy {
	return &EventCopy{
		xmlName: x.Name,
		Number:  x.Number,
		Old:     newTranslation(x.Ref),
	}
}

func (x *XMLError) Translate() *Error {
	err := &Error{
		xmlName: x.Name,
		Number:  x.Number,
		Fields:  make([]Field, len(x.Fields)),
	}
	for i, field := range x.Fields {
		err.Fields[i] = field.Translate(err, nil)
	}
	return err
}

func (x *XMLErrorCopy) Translate() *ErrorCopy {
	return &ErrorCopy{
		xmlName: x.Name,
		Number:  x.Number,
		Old:     newTranslation(x.Ref),
	}
}

// XCB documentation is positively stuffed with TODOs. We'd like to make it
// look a bit less shitty, so remove those as they don't convey information.
//
//  ^TODO
//  ^TODO:
//  ^TODO: question?
//  ^TODO: Some words
//  ^TODO: some words
//  ^TODO: some words with full stop.
//  ^TODO: some words with full stop. and a question?
//  ... (TODO),
//  ... (TODO).
//  ... (TODO: a question?).
//  ... TODO: a question?
//  ... (word TODO) ...
var todoRE = regexp.MustCompile(`(?m:^TODO.*| \([^)]*TODO[^)]*\)| TODO:.*)`)
var paraRE = regexp.MustCompile(`\n{3,}`)

var backticksRE = regexp.MustCompile("`(?:xcb_|XCB_)?(.*?)(?:_t)?`")

// fixDocumentation tries to translate XCB documentation to match XGB.
// Note that <doc> blocks only appear in xproto, so this doesn't have that much
// of a value--users still need to read Xlib or X11 protocol docs.
// Despite that, it's better to have something than nothing.
//
// We don't attempt to add proper prefixes to enum values or guess at
// specific XCB_NONE types (the latter is undecidable).
//
// We can't decide whether `fields_len` should be converted to len(Fields)
// or FieldsLen because e.g. StringLen is retained in ImageText8/16 but
// PointsLen is implied by the length of the Points slice in PolyLine.
func fixDocumentation(xcb string) string {
	last, result := 0, make([]byte, 0, len(xcb))
	for _, m := range backticksRE.FindAllStringSubmatchIndex(xcb, -1) {
		result = append(result, xcb[last:m[0]]...)
		inner := xcb[m[2]:m[3]]
		last = m[1]

		// Do not convert atom names to identifiers, mainly _NET_WM_*.
		if strings.Contains(inner, "WM_") {
			result = append(result, inner...)
		} else {
			result = append(result, splitAndTitle(inner)...)
		}
	}
	result = todoRE.ReplaceAllLiteral(append(result, xcb[last:]...), nil)
	result = paraRE.ReplaceAllLiteral(result, []byte("\n\n"))
	return strings.TrimSpace(string(result))
}

func (x *XMLDoc) Translate() Doc {
	if x == nil {
		return Doc{}
	}
	d := Doc{
		Brief:       fixDocumentation(x.Brief),
		Description: fixDocumentation(x.Description),
		Fields:      make(map[string]string),
		Errors:      make(map[string]string),
	}
	for _, x := range x.Fields {
		d.Fields[x.Name] = fixDocumentation(x.Description)
	}
	for _, x := range x.Errors {
		d.Errors[x.Type] = fixDocumentation(x.Description)
	}
	return d
}

func (x *XMLStruct) Translate() *Struct {
	s := &Struct{
		xmlName: x.Name,
		Fields:  make([]Field, len(x.Fields)),
	}
	for i, field := range x.Fields {
		s.Fields[i] = field.Translate(s, nil)
	}
	return s
}

func (x *XMLUnion) Translate() *Union {
	u := &Union{
		xmlName: x.Name,
		Fields:  make([]Field, len(x.Fields)),
	}
	for i, field := range x.Fields {
		u.Fields[i] = field.Translate(u, nil)
	}
	return u
}

func (x *XMLRequest) Translate() *Request {
	r := &Request{
		xmlName: x.Name,
		Opcode:  x.Opcode,
		Combine: x.Combine,
		Fields:  make([]Field, 0, len(x.Fields)),
		Reply:   x.Reply.Translate(),
		Doc:     x.Doc.Translate(),
	}
	for _, field := range x.Fields {
		if field.XMLName.Local == "fd" {
			continue
		}
		r.Fields = append(r.Fields, field.Translate(r, &r.Doc))
	}

	// Address bug (or legacy code) in QueryTextExtents.
	// The XML protocol description references 'string_len' in the
	// computation of the 'odd_length' field. However, 'string_len' is not
	// defined. Therefore, let's forcefully add it as a 'local field'.
	// (i.e., a parameter in the caller but does not get sent over the wire.)
	if x.Name == "QueryTextExtents" {
		stringLenLocal := &LocalField{&SingleField{
			xmlName: "string_len",
			Type:    newTranslation("CARD16"),
		}}
		r.Fields = append(r.Fields, stringLenLocal)
	}

	return r
}

func (x *XMLReply) Translate() *Reply {
	if x == nil {
		return nil
	}

	r := &Reply{
		Fields: make([]Field, 0, len(x.Fields)),
		Doc:    x.Doc.Translate(),
	}
	for _, field := range x.Fields {
		if field.XMLName.Local == "fd" {
			continue
		}
		r.Fields = append(r.Fields, field.Translate(r, &r.Doc))
	}
	return r
}

func (x *XMLExpression) Translate() Expression {
	if x == nil {
		return nil
	}

	switch x.XMLName.Local {
	case "op":
		if len(x.Exprs) != 2 {
			log.Panicf("'op' found %d expressions; expected 2.", len(x.Exprs))
		}
		return &BinaryOp{
			Op:    x.Op,
			Expr1: x.Exprs[0].Translate(),
			Expr2: x.Exprs[1].Translate(),
		}
	case "unop":
		if len(x.Exprs) != 1 {
			log.Panicf("'unop' found %d expressions; expected 1.", len(x.Exprs))
		}
		return &UnaryOp{
			Op:   x.Op,
			Expr: x.Exprs[0].Translate(),
		}
	case "popcount":
		if len(x.Exprs) != 1 {
			log.Panicf("'popcount' found %d expressions; expected 1.",
				len(x.Exprs))
		}
		return &PopCount{
			Expr: x.Exprs[0].Translate(),
		}
	case "value":
		val, err := strconv.Atoi(strings.TrimSpace(x.Data))
		if err != nil {
			log.Panicf("Could not convert '%s' in 'value' expression to int.",
				x.Data)
		}
		return &Value{
			v: val,
		}
	case "bit":
		bit, err := strconv.Atoi(strings.TrimSpace(x.Data))
		if err != nil {
			log.Panicf("Could not convert '%s' in 'bit' expression to int.",
				x.Data)
		}
		if bit < 0 || bit > 31 {
			log.Panicf("A 'bit' literal must be in the range [0, 31], but "+
				" is %d", bit)
		}
		return &Bit{
			b: bit,
		}
	case "fieldref":
		return &FieldRef{
			Name: x.Data,
		}
	case "enumref":
		return &EnumRef{
			EnumKind: newTranslation(x.Ref),
			EnumItem: x.Data,
		}
	case "sumof":
		return &SumOf{
			Name: x.Ref,
		}
	}

	log.Panicf("Unrecognized tag '%s' in expression context. Expected one of "+
		"op, fieldref, value, bit, enumref, unop, sumof or popcount.",
		x.XMLName.Local)
	panic("unreachable")
}

func (x *XMLField) Translate(parent interface{}, doc *Doc) Field {
	switch x.XMLName.Local {
	case "pad":
		return &PadField{
			Bytes: x.Bytes,
			Align: x.Align,
		}
	case "field":
		s := &SingleField{
			xmlName: x.Name,
			Type:    newTranslation(x.Type),
			Comment: doc.DescribeField(x.Name),
		}
		return s
	case "list":
		return &ListField{
			xmlName:    x.Name,
			Type:       newTranslation(x.Type),
			LengthExpr: x.Expr.Translate(),
		}
	case "localfield":
		return &LocalField{&SingleField{
			xmlName: x.Name,
			Type:    newTranslation(x.Type),
		}}
	case "exprfield":
		return &ExprField{
			xmlName: x.Name,
			Type:    newTranslation(x.Type),
			Expr:    x.Expr.Translate(),
		}
	case "valueparam":
		return &ValueField{
			Parent:      parent,
			MaskType:    newTranslation(x.ValueMaskType),
			MaskName:    x.ValueMaskName,
			ListName:    x.ValueListName,
			MaskComment: doc.DescribeField(x.ValueMaskName),
			ListComment: doc.DescribeField(x.ValueListName),
		}
	case "switch":
		swtch := &SwitchField{
			Name:     x.Name,
			Expr:     x.Expr.Translate(),
			Bitcases: make([]*Bitcase, len(x.Bitcases)),
			Comment:  doc.DescribeField(x.Name),
		}
		for i, bitcase := range x.Bitcases {
			swtch.Bitcases[i] = bitcase.Translate()
		}
		return swtch
	case "required_start_align":
		return &RequiredStartAlign{}
	}

	log.Panicf("Unrecognized field element: %s", x.XMLName.Local)
	panic("unreachable")
}

func (x *XMLBitcase) Translate() *Bitcase {
	b := &Bitcase{
		Expr:   x.Expr().Translate(),
		Fields: make([]Field, len(x.Fields)),
	}
	for i, field := range x.Fields {
		b.Fields[i] = field.Translate(b, nil)
	}
	return b
}

// SrcName is used to translate any identifier into a Go name.
// Mostly used for fields, but used in a couple other places too (enum items).
func SrcName(p *Protocol, name string) string {
	// If it's in the name map, use that translation.
	if newn, ok := NameMap[name]; ok {
		return newn
	}
	return splitAndTitle(name)
}

func TypeSrcName(p *Protocol, typ Type) string {
	t := typ.XmlName()

	// If this is a base type, then write the raw Go type.
	if baseType, ok := typ.(*Base); ok {
		return baseType.SrcName()
	}

	// If it's in the type map, use that translation.
	if newt, ok := TypeMap[t]; ok {
		return newt
	}

	// If there's a namespace to this type, just use it and be done.
	if colon := strings.Index(t, ":"); colon > -1 {
		namespace := t[:colon]
		rest := t[colon+1:]
		return p.ProtocolFind(namespace).PkgName() + "." + splitAndTitle(rest)
	}

	// Since there's no namespace, we're left with the raw type name.
	// If the type is part of the source we're generating (i.e., there is
	// no parent protocol), then just return that type name.
	// Otherwise, we must qualify it with a package name.
	if p.Parent == nil {
		return splitAndTitle(t)
	}
	return p.PkgName() + "." + splitAndTitle(t)
}