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

import (
	"encoding/xml"
	"io/ioutil"
	"log"
	"time"
)

type XML struct {
	// Root 'xcb' element properties.
	XMLName xml.Name `xml:"xcb"`
	Header string `xml:"header,attr"`
	ExtensionXName string `xml:"extension-xname,attr"`
	ExtensionName string `xml:"extension-name,attr"`
	MajorVersion string `xml:"major-version,attr"`
	MinorVersion string `xml:"minor-version,attr"`

	// Types for all top-level elements.
	// First are the simple ones.
	Imports Imports `xml:"import"`
	Enums Enums `xml:"enum"`
	Xids Xids `xml:"xidtype"`
	XidUnions Xids `xml:"xidunion"`
	TypeDefs TypeDefs `xml:"typedef"`
	EventCopies EventCopies `xml:"eventcopy"`
	ErrorCopies ErrorCopies `xml:"errorcopy"`

	// Here are the complex ones, i.e., anything with "structure contents"
	Structs Structs `xml:"struct"`
	Unions Unions `xml:"union"`
	Requests Requests `xml:"request"`
	Events Events `xml:"event"`
	Errors Errors `xml:"error"`
}

// Morph cascades down all of the XML and calls each type's corresponding
// Morph function with itself as an argument (the context).
func (x *XML) Morph(c *Context) {
	// Start the header...
	c.Putln("package xgb")
	c.Putln("/*")
	c.Putln("\tX protocol API for '%s.xml'.", c.xml.Header)
	c.Putln("\tThis file is automatically generated. Edit at your own peril!")
	c.Putln("\tGenerated on %s",
		time.Now().Format("Jan 2, 2006 at 3:04:05pm MST"))
	c.Putln("*/")
	c.Putln("")

	x.Imports.Morph(c)
	c.Putln("")

	x.Enums.Morph(c)
	c.Putln("")

	x.Xids.Morph(c)
	c.Putln("")

	x.XidUnions.Morph(c)
	c.Putln("")

	x.TypeDefs.Morph(c)
	c.Putln("")

	x.Structs.Morph(c)
	c.Putln("")

	x.Unions.Morph(c)
	c.Putln("")

	x.Requests.Morph(c)
	c.Putln("")

	x.Errors.Morph(c)
	c.Putln("")

	x.ErrorCopies.Morph(c)
	c.Putln("")

	x.Events.Morph(c)
	c.Putln("")

	x.EventCopies.Morph(c)
	c.Putln("")
}

// IsResource returns true if the 'needle' type is a resource type.
// i.e., an "xid"
func (x *XML) IsResource(needle Type) bool {
	for _, xid := range x.Xids {
		if needle == xid.Name {
			return true
		}
	}
	for _, xidunion := range x.XidUnions {
		if needle == xidunion.Name {
			return true
		}
	}
	for _, imp := range x.Imports {
		if imp.xml.IsResource(needle) {
			return true
		}
	}
	return false
}

// HasType returns true if the 'needle' type can be found in the protocol
// description represented by 'x'.
func (x *XML) HasType(needle Type) bool {
	for _, enum := range x.Enums {
		if needle == enum.Name {
			return true
		}
	}
	for _, xid := range x.Xids {
		if needle == xid.Name {
			return true
		}
	}
	for _, xidunion := range x.XidUnions {
		if needle == xidunion.Name {
			return true
		}
	}
	for _, typedef := range x.TypeDefs {
		if needle == typedef.New {
			return true
		}
	}
	for _, evcopy := range x.EventCopies {
		if needle == evcopy.Name {
			return true
		}
	}
	for _, errcopy := range x.ErrorCopies {
		if needle == errcopy.Name {
			return true
		}
	}
	for _, strct := range x.Structs {
		if needle == strct.Name {
			return true
		}
	}
	for _, union := range x.Unions {
		if needle == union.Name {
			return true
		}
	}
	for _, ev := range x.Events {
		if needle == ev.Name {
			return true
		}
	}
	for _, err := range x.Errors {
		if needle == err.Name {
			return true
		}
	}

	return false
}

type Name string

type Type string

// Size is a nifty function that takes any type and digs until it finds
// its underlying base type. At which point, the size can be determined.
func (typ Type) Size(c *Context) uint {
	// If this is a base type, we're done.
	if size, ok := BaseTypeSizes[string(typ)]; ok {
		return size
	}

	// If it's a resource, we're also done.
	if c.xml.IsResource(typ) {
		return BaseTypeSizes["Id"]
	}

	// It's not, so that implies there is *some* typedef declaring it
	// in terms of another type. Just follow that chain until we get to
	// a base type. We also need to check imported stuff.
	for _, typedef := range c.xml.TypeDefs {
		if typ == typedef.New {
			return typedef.Old.Size(c)
		}
	}
	for _, imp := range c.xml.Imports {
		for _, typedef := range imp.xml.TypeDefs {
			if typ == typedef.New {
				return typedef.Old.Size(c)
			}
		}
	}
	log.Panicf("Could not find base size of type '%s'.", typ)
	panic("unreachable")
}

type Imports []*Import

func (imports Imports) Eval() {
	for _, imp := range imports {
		xmlBytes, err := ioutil.ReadFile(*protoPath + "/" + imp.Name + ".xml")
		if err != nil {
			log.Fatalf("Could not read X protocol description for import " +
				"'%s' because: %s", imp.Name, err)
		}

		imp.xml = &XML{}
		err = xml.Unmarshal(xmlBytes, imp.xml)
		if err != nil {
			log.Fatal("Could not parse X protocol description for import " +
				"'%s' because: %s", imp.Name, err)
		}
	}
}

type Import struct {
	Name string `xml:",chardata"`
	xml *XML `xml:"-"`
}

type Enums []Enum

// Eval on the list of all enum types goes through and forces every enum
// item to have a valid expression.
// This is necessary because when an item is empty, it is defined to have
// the value of "one more than that of the previous item, or 0 for the first
// item".
func (enums Enums) Eval() {
	for _, enum := range enums {
		nextValue := uint(0)
		for _, item := range enum.Items {
			if item.Expr == nil {
				item.Expr = newValueExpression(nextValue)
				nextValue++
			} else {
				nextValue = item.Expr.Eval() + 1
			}
		}
	}
}

type Enum struct {
	Name Type `xml:"name,attr"`
	Items []*EnumItem `xml:"item"`
}

type EnumItem struct {
	Name Name `xml:"name,attr"`
	Expr *Expression `xml:",any"`
}

type Xids []*Xid

type Xid struct {
	XMLName xml.Name
	Name Type `xml:"name,attr"`
}

type TypeDefs []*TypeDef

type TypeDef struct {
	Old Type `xml:"oldname,attr"`
	New Type `xml:"newname,attr"`
}

type EventCopies []*EventCopy

type EventCopy struct {
	Name Type `xml:"name,attr"`
	Number int `xml:"number,attr"`
	Ref Type `xml:"ref,attr"`
}

type ErrorCopies []*ErrorCopy

type ErrorCopy struct {
	Name Type `xml:"name,attr"`
	Number int `xml:"number,attr"`
	Ref Type `xml:"ref,attr"`
}

type Structs []*Struct

type Struct struct {
	Name Type `xml:"name,attr"`
	Fields Fields `xml:",any"`
}

type Unions []*Union

type Union struct {
	Name Type `xml:"name,attr"`
	Fields Fields `xml:",any"`
}

type Requests []*Request

type Request struct {
	Name Type `xml:"name,attr"`
	Opcode int `xml:"opcode,attr"`
	Combine bool `xml:"combine-adjacent,attr"`
	Fields Fields `xml:",any"`
	Reply *Reply `xml:"reply"`
}

type Reply struct {
	Fields Fields `xml:",any"`
}

type Events []*Event

type Event struct {
	Name Type `xml:"name,attr"`
	Number int `xml:"number,attr"`
	NoSequence bool `xml:"no-sequence-number,true"`
	Fields Fields `xml:",any"`
}

type Errors []*Error

type Error struct {
	Name Type `xml:"name,attr"`
	Number int `xml:"number,attr"`
	Fields Fields `xml:",any"`
}