forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
base.ir.go
438 lines (360 loc) · 10.4 KB
/
base.ir.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
// Code generated by irgen/irgen base/base.tmpl.go example.def. DO NOT EDIT.
// GENERATED FILE DO NOT EDIT
// Copyright 2017 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package base
import "fmt"
// node is a generic ADT node type, with slots for references to other nodes and
// enumeration values. Values that don't fit and values of other types go in the
// extra field.
type node struct {
refs [1]*node
enums [2]enum
extra
}
type enum int32
type extra interface {
extraRefs() []*node
}
// Allocator allocates nodes in batches. Construct Allocators with NewAllocator
// and pass them by value.
type Allocator struct {
nodes *[]node
}
// NewAllocator constructs a new Allocator.
func NewAllocator() Allocator {
nodes := make([]node, 16)
return Allocator{&nodes}
}
// new allocates a new node. Users of this package should use the appropriate
// R() method, which is type safe.
func (a Allocator) new() *node {
nodes := *a.nodes
if len(nodes) == 0 {
nodes = make([]node, 256)
}
x := &nodes[0]
*a.nodes = nodes[1:]
return x
}
// ---- BinOp ---- //
// BinOp is an enumeration type.
type BinOp enum
// BinOp constants.
const (
BinOpAdd BinOp = 1
BinOpMul BinOp = 2
)
func (x BinOp) String() string {
switch x {
case BinOpAdd:
return "Add"
case BinOpMul:
return "Mul"
default:
return fmt.Sprintf("<unknown BinOp %d>", x)
}
}
// ---- ConstExpr ---- //
// ConstExpr is the type of a reference to an immutable record.
type ConstExpr struct{ ref *node }
// ConstExprValue is the logical type of a record. Immutable records are stored in
// nodes.
type ConstExprValue struct {
Datum int64 // 1
}
type extraConstExpr struct {
Datum int64
}
func (x extraConstExpr) extraRefs() []*node { return nil }
// R constructs a reference to an immutable record.
func (x ConstExprValue) R(a Allocator) ConstExpr {
ref := a.new()
extra := &extraConstExpr{}
ref.extra = extra
extra.Datum = x.Datum
return ConstExpr{ref}
}
// Datum returns the Datum field.
func (x ConstExpr) Datum() int64 { return x.ref.extra.(*extraConstExpr).Datum }
// V unpacks a reference into a value.
func (x ConstExpr) V() ConstExprValue {
return ConstExprValue{
x.ref.extra.(*extraConstExpr).Datum,
}
}
// WithDatum constructs a new value where the value of Datum has been replaced by
// the argument.
func (x ConstExprValue) WithDatum(y int64) ConstExprValue {
x.Datum = y
return x
}
// ---- BinExpr ---- //
// BinExpr is the type of a reference to an immutable record.
type BinExpr struct{ ref *node }
// BinExprValue is the logical type of a record. Immutable records are stored in
// nodes.
type BinExprValue struct {
Left Expr // 1
Op BinOp // 2
Right Expr // 3
}
type extraBinExpr struct {
Right ExprTag
refs [1]*node
}
func (x extraBinExpr) extraRefs() []*node { return x.refs[:] }
// R constructs a reference to an immutable record.
func (x BinExprValue) R(a Allocator) BinExpr {
ref := a.new()
extra := &extraBinExpr{}
ref.extra = extra
ref.enums[1] = enum(x.Left.tag)
ref.refs[0] = x.Left.ref
ref.enums[0] = enum(x.Op)
extra.Right = x.Right.tag
extra.refs[0] = x.Right.ref
return BinExpr{ref}
}
// Left returns the Left field.
func (x BinExpr) Left() Expr { return Expr{ExprTag(x.ref.enums[1]), x.ref.refs[0]} }
// Op returns the Op field.
func (x BinExpr) Op() BinOp { return BinOp(x.ref.enums[0]) }
// Right returns the Right field.
func (x BinExpr) Right() Expr {
return Expr{x.ref.extra.(*extraBinExpr).Right, x.ref.extra.(*extraBinExpr).refs[0]}
}
// V unpacks a reference into a value.
func (x BinExpr) V() BinExprValue {
return BinExprValue{
Expr{ExprTag(x.ref.enums[1]), x.ref.refs[0]},
BinOp(x.ref.enums[0]),
Expr{x.ref.extra.(*extraBinExpr).Right, x.ref.extra.(*extraBinExpr).refs[0]},
}
}
// WithLeft constructs a new value where the value of Left has been replaced by
// the argument.
func (x BinExprValue) WithLeft(y Expr) BinExprValue {
x.Left = y
return x
}
// WithOp constructs a new value where the value of Op has been replaced by
// the argument.
func (x BinExprValue) WithOp(y BinOp) BinExprValue {
x.Op = y
return x
}
// WithRight constructs a new value where the value of Right has been replaced by
// the argument.
func (x BinExprValue) WithRight(y Expr) BinExprValue {
x.Right = y
return x
}
// ---- All ---- //
// All is the type of a reference to an immutable record.
type All struct{ ref *node }
// AllValue is the logical type of a record. Immutable records are stored in
// nodes.
type AllValue struct {
E Expr // 1
CE ConstExpr // 2
BE BinExpr // 3
BO BinOp // 4
A AllOrExpr // 5
}
type extraAll struct {
A AllOrExprTag
refs [3]*node
}
func (x extraAll) extraRefs() []*node { return x.refs[:] }
// R constructs a reference to an immutable record.
func (x AllValue) R(a Allocator) All {
ref := a.new()
extra := &extraAll{}
ref.extra = extra
ref.enums[1] = enum(x.E.tag)
ref.refs[0] = x.E.ref
extra.refs[0] = x.CE.ref
extra.refs[1] = x.BE.ref
ref.enums[0] = enum(x.BO)
extra.A = x.A.tag
extra.refs[2] = x.A.ref
return All{ref}
}
// E returns the E field.
func (x All) E() Expr { return Expr{ExprTag(x.ref.enums[1]), x.ref.refs[0]} }
// CE returns the CE field.
func (x All) CE() ConstExpr { return ConstExpr{x.ref.extra.(*extraAll).refs[0]} }
// BE returns the BE field.
func (x All) BE() BinExpr { return BinExpr{x.ref.extra.(*extraAll).refs[1]} }
// BO returns the BO field.
func (x All) BO() BinOp { return BinOp(x.ref.enums[0]) }
// A returns the A field.
func (x All) A() AllOrExpr {
return AllOrExpr{x.ref.extra.(*extraAll).A, x.ref.extra.(*extraAll).refs[2]}
}
// V unpacks a reference into a value.
func (x All) V() AllValue {
return AllValue{
Expr{ExprTag(x.ref.enums[1]), x.ref.refs[0]},
ConstExpr{x.ref.extra.(*extraAll).refs[0]},
BinExpr{x.ref.extra.(*extraAll).refs[1]},
BinOp(x.ref.enums[0]),
AllOrExpr{x.ref.extra.(*extraAll).A, x.ref.extra.(*extraAll).refs[2]},
}
}
// WithE constructs a new value where the value of E has been replaced by
// the argument.
func (x AllValue) WithE(y Expr) AllValue {
x.E = y
return x
}
// WithCE constructs a new value where the value of CE has been replaced by
// the argument.
func (x AllValue) WithCE(y ConstExpr) AllValue {
x.CE = y
return x
}
// WithBE constructs a new value where the value of BE has been replaced by
// the argument.
func (x AllValue) WithBE(y BinExpr) AllValue {
x.BE = y
return x
}
// WithBO constructs a new value where the value of BO has been replaced by
// the argument.
func (x AllValue) WithBO(y BinOp) AllValue {
x.BO = y
return x
}
// WithA constructs a new value where the value of A has been replaced by
// the argument.
func (x AllValue) WithA(y AllOrExpr) AllValue {
x.A = y
return x
}
// ---- Expr ---- //
// Expr is the type of a tagged union of records.
type Expr struct {
tag ExprTag
ref *node
}
// ExprTag is the tag type.
type ExprTag enum
// ExprTag constants.
const (
ExprConstExpr ExprTag = 1
ExprBinExpr ExprTag = 2
)
func (x ExprTag) String() string {
switch x {
case ExprConstExpr:
return "ConstExpr"
case ExprBinExpr:
return "BinExpr"
default:
return fmt.Sprintf("<unknown ExprTag %d>", x)
}
}
// Tag returns the tag.
func (x Expr) Tag() ExprTag { return x.tag }
// Expr performs an upcast.
func (x ConstExpr) Expr() Expr { return Expr{ExprConstExpr, x.ref} }
// ConstExpr performs a downcast. If the downcast fails, return false.
func (x Expr) ConstExpr() (ConstExpr, bool) {
if x.tag != ExprConstExpr {
return ConstExpr{}, false
}
return ConstExpr{x.ref}, true
}
// MustBeConstExpr performs a downcast. If the downcast fails, panic.
func (x Expr) MustBeConstExpr() ConstExpr {
if x.tag != ExprConstExpr {
panic(fmt.Sprintf("type assertion failed: expected ConstExpr but got %s", x.tag))
}
return ConstExpr{x.ref}
}
// Expr performs an upcast.
func (x BinExpr) Expr() Expr { return Expr{ExprBinExpr, x.ref} }
// BinExpr performs a downcast. If the downcast fails, return false.
func (x Expr) BinExpr() (BinExpr, bool) {
if x.tag != ExprBinExpr {
return BinExpr{}, false
}
return BinExpr{x.ref}, true
}
// MustBeBinExpr performs a downcast. If the downcast fails, panic.
func (x Expr) MustBeBinExpr() BinExpr {
if x.tag != ExprBinExpr {
panic(fmt.Sprintf("type assertion failed: expected BinExpr but got %s", x.tag))
}
return BinExpr{x.ref}
}
// ---- AllOrExpr ---- //
// AllOrExpr is the type of a tagged union of records.
type AllOrExpr struct {
tag AllOrExprTag
ref *node
}
// AllOrExprTag is the tag type.
type AllOrExprTag enum
// AllOrExprTag constants.
const (
AllOrExprAll AllOrExprTag = 1
AllOrExprBinExpr AllOrExprTag = 2
)
func (x AllOrExprTag) String() string {
switch x {
case AllOrExprAll:
return "All"
case AllOrExprBinExpr:
return "BinExpr"
default:
return fmt.Sprintf("<unknown AllOrExprTag %d>", x)
}
}
// Tag returns the tag.
func (x AllOrExpr) Tag() AllOrExprTag { return x.tag }
// AllOrExpr performs an upcast.
func (x All) AllOrExpr() AllOrExpr { return AllOrExpr{AllOrExprAll, x.ref} }
// All performs a downcast. If the downcast fails, return false.
func (x AllOrExpr) All() (All, bool) {
if x.tag != AllOrExprAll {
return All{}, false
}
return All{x.ref}, true
}
// MustBeAll performs a downcast. If the downcast fails, panic.
func (x AllOrExpr) MustBeAll() All {
if x.tag != AllOrExprAll {
panic(fmt.Sprintf("type assertion failed: expected All but got %s", x.tag))
}
return All{x.ref}
}
// AllOrExpr performs an upcast.
func (x BinExpr) AllOrExpr() AllOrExpr { return AllOrExpr{AllOrExprBinExpr, x.ref} }
// BinExpr performs a downcast. If the downcast fails, return false.
func (x AllOrExpr) BinExpr() (BinExpr, bool) {
if x.tag != AllOrExprBinExpr {
return BinExpr{}, false
}
return BinExpr{x.ref}, true
}
// MustBeBinExpr performs a downcast. If the downcast fails, panic.
func (x AllOrExpr) MustBeBinExpr() BinExpr {
if x.tag != AllOrExprBinExpr {
panic(fmt.Sprintf("type assertion failed: expected BinExpr but got %s", x.tag))
}
return BinExpr{x.ref}
}