aboutsummaryrefslogtreecommitdiff
path: root/nexgb/xgbgen/go.go
blob: ac3ed2ca7d2de2cb7af7b97ecd34ac580c350e9c (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
package main

import (
	"fmt"
	"log"
)

// xgbResourceIdName is the name of the type used for all resource identifiers.
// As of right now, it needs to be declared somewhere manually.
var xgbGenResourceIdName = "Id"

// BaseTypeMap is a map from X base types to Go types.
// X base types should correspond to the smallest set of X types
// that can be used to rewrite ALL X types in terms of Go types.
// That is, if you remove any of the following types, at least one
// XML protocol description will produce an invalid Go program.
// The types on the left *never* show themselves in the source.
var BaseTypeMap = map[string]string{
	"CARD8": "byte",
	"CARD16": "uint16",
	"CARD32": "uint32",
	"INT8": "int8",
	"INT16": "int16",
	"INT32": "int32",
	"BYTE": "byte",
	"BOOL": "bool",
	"float": "float64",
	"double": "float64",
	"char": "byte",
	"void": "byte",
	"Id": "Id",
}

// BaseTypeSizes should have precisely the same keys as in BaseTypeMap,
// and the values should correspond to the size of the type in bytes.
var BaseTypeSizes = map[string]uint{
	"CARD8": 1,
	"CARD16": 2,
	"CARD32": 4,
	"INT8": 1,
	"INT16": 2,
	"INT32": 4,
	"BYTE": 1,
	"BOOL": 1,
	"float": 4,
	"double": 8,
	"char": 1,
	"void": 1,
	"Id": 4,
}

// TypeMap is a map from types in the XML to type names that is used
// in the functions that follow. Basically, every occurrence of the key
// type is replaced with the value type.
var TypeMap = map[string]string{
	"VISUALTYPE": "VisualInfo",
	"DEPTH": "DepthInfo",
	"SCREEN": "ScreenInfo",
	"Setup": "SetupInfo",
}

// NameMap is the same as TypeMap, but for names.
var NameMap = map[string]string{ }

// Reading, writing and defining...

// Base types
func (b *Base) Define(c *Context) {
	c.Putln("// Skipping definition for base type '%s'", SrcName(b.XmlName()))
	c.Putln("")
}

// Enum types
func (enum *Enum) Define(c *Context) {
	c.Putln("const (")
	for _, item := range enum.Items {
		c.Putln("%s%s = %d", enum.SrcName(), item.srcName, item.Expr.Eval())
	}
	c.Putln(")")
	c.Putln("")
}

// Resource types
func (res *Resource) Define(c *Context) {
	c.Putln("// Skipping resource definition of '%s'", SrcName(res.XmlName()))
	c.Putln("")
}

// TypeDef types
func (td *TypeDef) Define(c *Context) {
	c.Putln("type %s %s", td.srcName, td.Old.SrcName())
	c.Putln("")
}

// Struct types
func (s *Struct) Define(c *Context) {
	c.Putln("// '%s' struct definition", s.SrcName())
	c.Putln("// Size: %s", s.Size())
	c.Putln("type %s struct {", s.SrcName())
	for _, field := range s.Fields {
		field.Define(c)
	}
	c.Putln("}")
	c.Putln("")

	// Write function that reads bytes and produces this struct.
	s.Read(c)

	// Write function that reads a list of this structs.
	s.ReadList(c)

	// Write function that writes bytes given this struct.
	s.Write(c)

	// Write function that writes a list of this struct.
	s.WriteList(c)
}

// Read for a struct creates a function 'NewStructName' that takes a byte
// slice and produces TWO values: an instance of 'StructName' and the number
// of bytes read from the byte slice.
// 'NewStructName' should only be used to read raw reply data from the wire.
func (s *Struct) Read(c *Context) {
	c.Putln("// Struct read %s", s.SrcName())
	c.Putln("func New%s(buf []byte) (%s, int) {", s.SrcName(), s.SrcName())

	c.Putln("v := %s{}", s.SrcName())
	c.Putln("b := 0")
	c.Putln("consumed := 0")
	c.Putln("consumed = 0 + consumed // no-op") // dirty hack for a no-op
	c.Putln("")
	for _, field := range s.Fields {
		field.Read(c)
	}
	c.Putln("return v, b")

	c.Putln("}")
	c.Putln("")
}

// ReadList for a struct creates a function 'ReadStructNameList' that takes
// a byte slice and a length and produces TWO values: an slice of StructName 
// and the number of bytes read from the byte slice.
func (s *Struct) ReadList(c *Context) {
	c.Putln("// Struct list read %s", s.SrcName())
	c.Putln("func Read%sList(buf []byte, length int) ([]%s, int) {",
		s.SrcName(), s.SrcName())

	c.Putln("v := make([]%s, length)", s.SrcName())
	c.Putln("b := 0")
	c.Putln("consumed := 0")
	c.Putln("consumed = 0 + consumed // no-op") // dirty hack for a no-op
	c.Putln("for i := 0; i < length; i++ {")
		c.Putln("v[i], consumed = New%s(buf[b:])", s.SrcName())
		c.Putln("b += consumed")
	c.Putln("}")

	c.Putln("return v, pad(b)")

	c.Putln("}")
	c.Putln("")
}

func (s *Struct) Write(c *Context) {
	c.Putln("// Struct write %s", s.SrcName())
	c.Putln("")
}

func (s *Struct) WriteList(c *Context) {
	c.Putln("// Write struct list %s", s.SrcName())
	c.Putln("")
}

// Union types
func (u *Union) Define(c *Context) {
	c.Putln("// Union definition %s", u.SrcName())
}

func (u *Union) Read(c *Context, prefix string) {
	c.Putln("// Union read %s", u.SrcName())
}

func (u *Union) Write(c *Context, prefix string) {
	c.Putln("// Union write %s", u.SrcName())
}

// Event types
func (e *Event) Define(c *Context) {
	c.Putln("// Event definition %s (%d)", e.SrcName(), e.Number)
}

func (e *Event) Read(c *Context, prefix string) {
	c.Putln("// Event read %s", e.SrcName())
}

func (e *Event) Write(c *Context, prefix string) {
	c.Putln("// Event write %s", e.SrcName())
}

// EventCopy types
func (e *EventCopy) Define(c *Context) {
	c.Putln("// EventCopy definition %s (%d)", e.SrcName(), e.Number)
	c.Putln("")
	c.Putln("const %s = %d", e.SrcName(), e.Number)
	c.Putln("")
	c.Putln("type %s %s", e.EvType(), e.Old.(*Event).EvType())
	c.Putln("")
	c.Putln("func New%s(buf []byte) %s {", e.SrcName(), e.EvType())
	c.Putln("return (%s)(New%s(buf))", e.EvType(), e.Old.SrcName())
	c.Putln("}")
	c.Putln("")
	c.Putln("func (ev %s) ImplementsEvent() { }", e.EvType())
	c.Putln("")
	c.Putln("func (ev %s) Bytes() []byte {", e.EvType())
	c.Putln("return (%s)(ev).Bytes()", e.Old.(*Event).EvType())
	c.Putln("}")
	c.Putln("")
	c.Putln("func init() {")
	c.Putln("newEventFuncs[%d] = New%s", e.Number, e.SrcName())
	c.Putln("}")
	c.Putln("")
}

// Error types
func (e *Error) Define(c *Context) {
	c.Putln("// Error definition %s (%d)", e.SrcName(), e.Number)
	c.Putln("")
}

func (e *Error) Read(c *Context, prefix string) {
	c.Putln("// Error read %s", e.SrcName())
}

func (e *Error) Write(c *Context, prefix string) {
	c.Putln("// Error write %s", e.SrcName())
}

// ErrorCopy types
func (e *ErrorCopy) Define(c *Context) {
	c.Putln("// ErrorCopy definition %s (%d)", e.SrcName(), e.Number)
	c.Putln("")
	c.Putln("const %s = %d", e.ErrConst(), e.Number)
	c.Putln("")
	c.Putln("type %s %s", e.ErrType(), e.Old.(*Error).ErrType())
	c.Putln("")
	c.Putln("func New%s(buf []byte) %s {", e.SrcName(), e.ErrType())
	c.Putln("return (%s)(New%s(buf))", e.ErrType(), e.Old.SrcName())
	c.Putln("}")
	c.Putln("")
	c.Putln("func (err %s) ImplementsError() { }", e.ErrType())
	c.Putln("")
	c.Putln("func (err %s) Bytes() []byte {", e.ErrType())
	c.Putln("return (%s)(err).Bytes()", e.Old.(*Error).ErrType())
	c.Putln("}")
	c.Putln("")
	c.Putln("func init() {")
	c.Putln("newErrorFuncs[%d] = New%s", e.Number, e.SrcName())
	c.Putln("}")
	c.Putln("")
}

// Field definitions, reads and writes.

// Pad fields
func (f *PadField) Define(c *Context) {
	c.Putln("// padding: %d bytes", f.Bytes)
}

func (f *PadField) Read(c *Context) {
	c.Putln("b += %s // padding", f.Size())
	c.Putln("")
}

// Single fields
func (f *SingleField) Define(c *Context) {
	c.Putln("%s %s", f.SrcName(), f.Type.SrcName())
}

func ReadSimpleSingleField(c *Context, name string, typ Type) {
	switch t := typ.(type) {
	case *Resource:
		c.Putln("%s = get32(buf[b:])", name)
	case *TypeDef:
		switch t.Size().Eval() {
		case 1:
			c.Putln("%s = %s(buf[b])", name, t.SrcName())
		case 2:
			c.Putln("%s = %s(get16(buf[b:]))", name, t.SrcName())
		case 4:
			c.Putln("%s = %s(get32(buf[b:]))", name, t.SrcName())
		case 8:
			c.Putln("%s = %s(get64(buf[b:]))", name, t.SrcName())
		}
	case *Base:
		var val string
		switch t.Size().Eval() {
		case 1:
			val = fmt.Sprintf("buf[b]")
		case 2:
			val = fmt.Sprintf("get16(buf[b:])")
		case 4:
			val = fmt.Sprintf("get32(buf[b:])")
		case 8:
			val = fmt.Sprintf("get64(buf[b:])")
		}

		// We need to convert base types if they aren't uintXX or byte
		ty := t.SrcName()
		if ty != "byte" && ty != "uint16" && ty != "uint32" && ty != "uint64" {
			val = fmt.Sprintf("%s(%s)", ty, val)
		}
		c.Putln("%s = %s", name, val)
	default:
		log.Fatalf("Cannot read field '%s' as a simple field with %T type.",
			name, typ)
	}

	c.Putln("b += %s", typ.Size())
}

func (f *SingleField) Read(c *Context) {
	switch t := f.Type.(type) {
	case *Resource:
		ReadSimpleSingleField(c, fmt.Sprintf("v.%s", f.SrcName()), t)
	case *TypeDef:
		ReadSimpleSingleField(c, fmt.Sprintf("v.%s", f.SrcName()), t)
	case *Base:
		ReadSimpleSingleField(c, fmt.Sprintf("v.%s", f.SrcName()), t)
	case *Struct:
		c.Putln("v.%s, consumed = New%s(buf[b:])", f.SrcName(), t.SrcName())
		c.Putln("b += consumed")
		c.Putln("")
	default:
		log.Fatalf("Cannot read field '%s' with %T type.", f.XmlName(), f.Type)
	}
}

// List fields
func (f *ListField) Define(c *Context) {
	c.Putln("%s []%s // length: %s",
		f.SrcName(), f.Type.SrcName(), f.Size())
}

func (f *ListField) Read(c *Context) {
	switch t := f.Type.(type) {
	case *Resource:
		length := f.LengthExpr.Reduce("v.", "")
		c.Putln("v.%s = make([]Id, %s)", f.SrcName(), length)
		c.Putln("for i := 0; i < %s; i++ {", length)
			ReadSimpleSingleField(c, fmt.Sprintf("v.%s[i]", f.SrcName()), t)
		c.Putln("}")
		c.Putln("")
	case *Base:
		length := f.LengthExpr.Reduce("v.", "")
		c.Putln("v.%s = make([]%s, %s)", f.SrcName(), t.SrcName(), length)
		c.Putln("for i := 0; i < %s; i++ {", length)
			ReadSimpleSingleField(c, fmt.Sprintf("v.%s[i]", f.SrcName()), t)
		c.Putln("}")
		c.Putln("")
	case *Struct:
		c.Putln("v.%s, consumed = Read%sList(buf[b:], %s)",
			f.SrcName(), t.SrcName(), f.LengthExpr.Reduce("v.", ""))
		c.Putln("b += consumed")
		c.Putln("")
	default:
		log.Fatalf("Cannot read list field '%s' with %T type.",
			f.XmlName(), f.Type)
	}
}

// Local fields
func (f *LocalField) Define(c *Context) {
	c.Putln("// local field: %s %s", f.SrcName(), f.Type.SrcName())
}

func (f *LocalField) Read(c *Context) {
	c.Putln("// reading local field: %s (%s) :: %s",
		f.SrcName(), f.Size(), f.Type.SrcName())
}

// Expr fields
func (f *ExprField) Define(c *Context) {
	c.Putln("// expression field: %s %s (%s)",
		f.SrcName(), f.Type.SrcName(), f.Expr)
}

func (f *ExprField) Read(c *Context) {
	c.Putln("// reading expression field: %s (%s) (%s) :: %s",
		f.SrcName(), f.Size(), f.Expr, f.Type.SrcName())
}

// Value field
func (f *ValueField) Define(c *Context) {
	c.Putln("// valueparam field: type: %s, mask name: %s, list name: %s",
		f.MaskType.SrcName(), f.MaskName, f.ListName)
}

func (f *ValueField) Read(c *Context) {
	c.Putln("// reading valueparam: type: %s, mask name: %s, list name: %s",
		f.MaskType.SrcName(), f.MaskName, f.ListName)
}

// Switch field
func (f *SwitchField) Define(c *Context) {
	c.Putln("// switch field: %s (%s)", f.Name, f.Expr)
}

func (f *SwitchField) Read(c *Context) {
	c.Putln("// reading switch field: %s (%s)", f.Name, f.Expr)
}