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

// 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("")
}

// 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())
}

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

// 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())
}

func (f *LocalField) Write(c *Context) {
	c.Putln("// writing 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())
}

func (f *ExprField) Write(c *Context) {
	c.Putln("// writing 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)
}

func (f *ValueField) Write(c *Context) {
	c.Putln("// writing 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)
	panic("todo")
}

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

func (f *SwitchField) Write(c *Context) {
	c.Putln("// writing switch field: %s (%s)", f.Name, f.Expr)
	panic("todo")
}