forked from encoredev/encore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
literals.go
402 lines (354 loc) · 11.1 KB
/
literals.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
package parser
import (
"errors"
"go/ast"
"go/constant"
"go/token"
"reflect"
"strconv"
"strings"
"encr.dev/parser/est"
"encr.dev/parser/internal/runtimeconstants"
"encr.dev/pkg/errinsrc"
"encr.dev/pkg/errinsrc/srcerrors"
)
var noOpCasts = map[string][]string{
"encore.dev/cron": {"Duration"},
"time": {"Duration"},
}
// litString will return the string value of a given node
//
// If the given node isn't a string literal, it will return an empty string and false
func litString(node ast.Node) (string, bool) {
if lit, ok := node.(*ast.BasicLit); ok && lit.Kind == token.STRING {
return lit.Value[1 : len(lit.Value)-1], true
}
return "", false
}
// parseStructLit parses struct literal and returns a LiterialStruct object
//
// If there is a nested struct literal, then it's values will be nested in a dot syntax; i.e. `parentStructFieldName.childStructFieldName`
func (p *parser) parseStructLit(file *est.File, expectedType string, node ast.Expr) (lit *LiteralStruct, ok bool) {
cl, ok := node.(*ast.CompositeLit)
if !ok {
p.errf(node.Pos(), "Expected a literal instance of `%s`, got %s.", expectedType, prettyPrint(node))
return nil, false
}
lit = &LiteralStruct{
ast: cl,
constantFields: make(map[string]constant.Value),
allFields: make(map[string]ast.Expr),
childStructs: make(map[string]*LiteralStruct),
}
ok = true
elemLoop:
for _, elem := range cl.Elts {
switch elem := elem.(type) {
case *ast.KeyValueExpr:
ident, ok := elem.Key.(*ast.Ident)
if !ok {
p.errf(elem.Key.Pos(), "Expected a key to be an identifier, got a %v", reflect.TypeOf(elem.Key))
continue elemLoop
}
if ident == nil {
p.err(elem.Key.Pos(), "Expected a key to be an identifier, got a nil")
continue elemLoop
}
// Parse any sub data structures
var subStruct *ast.CompositeLit
switch value := elem.Value.(type) {
case *ast.UnaryExpr:
if value.Op == token.AND {
if compositeLiteral, ok := value.X.(*ast.CompositeLit); ok {
subStruct = compositeLiteral
}
}
case *ast.CompositeLit:
subStruct = value
}
if subStruct != nil {
subLit, subOk := p.parseStructLit(file, "struct", subStruct)
ok = ok && subOk
lit.childStructs[ident.Name] = subLit
} else if valueIdent, ok := elem.Value.(*ast.Ident); ok && valueIdent.Name == "nil" {
// no-op for nil's
} else {
// Parse the value
lit.allFields[ident.Name] = elem.Value
value := p.parseConstantValue(file, elem.Value)
if value.Kind() != constant.Unknown {
lit.constantFields[ident.Name] = value
}
}
default:
p.errf(elem.Pos(), "Expected a key-value pair, got a %v", reflect.TypeOf(elem))
}
}
return
}
func (p *parser) parseConstantValue(file *est.File, value ast.Expr) (rtn constant.Value) {
defer func() {
if r := recover(); r != nil {
// Recover the panic
err := srcerrors.UnhandledPanic(r)
errinsrc.AddHintFromGo(err, p.fset, value, "panic occurred processing this expression")
p.errInSrc(err)
rtn = constant.MakeUnknown()
}
}()
switch value := value.(type) {
case *ast.FuncLit:
// Functions are not literal constant values
return constant.MakeUnknown()
case *ast.Ident:
switch value.Name {
case "true":
return constant.MakeBool(true)
case "false":
return constant.MakeBool(false)
default:
return constant.MakeUnknown()
}
case *ast.BasicLit:
v, err := basicLit(value)
if err != nil {
p.errf(value.Pos(), "Unable to parse the basic literal: %v", err)
return constant.MakeUnknown()
} else {
return v
}
case *ast.SelectorExpr:
pkg, obj := pkgObj(p.names[file.Pkg].Files[file], value)
if pkg != "" {
if v, found := runtimeconstants.Get(pkg, obj); found {
return v
}
}
return constant.MakeUnknown()
case *ast.BinaryExpr:
lhs := p.parseConstantValue(file, value.X)
rhs := p.parseConstantValue(file, value.Y)
if lhs.Kind() == constant.Unknown || rhs.Kind() == constant.Unknown {
return constant.MakeUnknown()
}
switch value.Op {
case token.MUL, token.ADD, token.SUB, token.REM, token.AND, token.OR, token.XOR, token.AND_NOT:
return constant.BinaryOp(lhs, value.Op, rhs)
case token.QUO:
// constant.BinaryOp panics when dividing by zero
if floatValue, _ := constant.Float64Val(constant.ToFloat(rhs)); floatValue <= 0.000000001 && floatValue >= -0.000000001 {
p.errf(value.Pos(), "cannot divide by zero")
return constant.MakeUnknown()
}
return constant.BinaryOp(lhs, value.Op, rhs)
case token.EQL, token.NEQ, token.LSS, token.LEQ, token.GTR, token.GEQ:
return constant.MakeBool(constant.Compare(lhs, value.Op, rhs))
case token.SHL, token.SHR:
shiftValue, ok := constant.Uint64Val(constant.ToInt(rhs))
if !ok {
p.errf(value.Pos(), "shift count must be an unsigned integer")
}
return constant.Shift(lhs, value.Op, uint(shiftValue))
default:
p.errf(value.Pos(), "%s is an unsupported operation here", value.Op)
return constant.MakeUnknown()
}
case *ast.UnaryExpr:
x := p.parseConstantValue(file, value.X)
return constant.UnaryOp(value.Op, x, 0)
case *ast.CallExpr:
// We allow casts like "time.Duration(143)" or "cron.Duration(143)"
// so we transparently go through them
if sel, ok := value.Fun.(*ast.SelectorExpr); ok && len(value.Args) == 1 {
pkg, obj := pkgObj(p.names[file.Pkg].Files[file], sel)
if pkgFuncs, found := noOpCasts[pkg]; found {
for _, allowed := range pkgFuncs {
if allowed == obj {
return p.parseConstantValue(file, value.Args[0])
}
}
}
}
return constant.MakeUnknown()
case *ast.ParenExpr:
return p.parseConstantValue(file, value.X)
default:
p.errf(value.Pos(), "Unable to parse constant value, unknown data type: %v", reflect.TypeOf(value))
return constant.MakeUnknown()
}
}
func basicLit(value *ast.BasicLit) (constant.Value, error) {
switch value.Kind {
case token.IDENT:
return constant.MakeString(value.Value), nil
case token.INT:
v, err := strconv.ParseInt(value.Value, 10, 64)
if err != nil {
return constant.MakeUnknown(), err
}
return constant.MakeInt64(v), nil
case token.FLOAT:
v, err := strconv.ParseFloat(value.Value, 64)
if err != nil {
return constant.MakeUnknown(), err
}
return constant.MakeFloat64(v), nil
case token.CHAR:
c, _, _, err := strconv.UnquoteChar(value.Value, value.Value[0])
return constant.MakeFromBytes([]byte{byte(c)}), err
case token.STRING:
return constant.MakeString(value.Value[1 : len(value.Value)-1]), nil
default:
return nil, errors.New("unsupported literal type")
}
}
// LiteralStruct represents a struct literal at compile time
type LiteralStruct struct {
ast *ast.CompositeLit // The AST node which presents the literal
constantFields map[string]constant.Value // All found constant expressions
allFields map[string]ast.Expr // All field expressions (constant or otherwise)
childStructs map[string]*LiteralStruct // Any child struct literals
}
func (l *LiteralStruct) Lit() *ast.CompositeLit {
return l.ast
}
// FullyConstant returns true if every value in this struct and the child structs fully known as compile time
// as a constant value
func (l *LiteralStruct) FullyConstant() bool {
for _, sub := range l.childStructs {
if !sub.FullyConstant() {
return false
}
}
return len(l.constantFields) == len(l.allFields)
}
// DynamicFields returns the names of the fields and ast.Expr that are not constant
//
// Child structs will be included with the field name prefixed with the struct name;
// i.e. `parentField.childField`
func (l *LiteralStruct) DynamicFields() map[string]ast.Expr {
fields := make(map[string]ast.Expr)
for name, expr := range l.allFields {
if _, found := l.constantFields[name]; !found {
fields[name] = expr
}
}
for name, sub := range l.childStructs {
for k, v := range sub.DynamicFields() {
fields[name+"."+k] = v
}
}
return fields
}
// IsSet returns true if the given field is set in this struct
//
// You can reference a child struct field with `.`; i.e. `parent.child`
func (l *LiteralStruct) IsSet(fieldName string) bool {
// Recurse into child fields
before, after, found := strings.Cut(fieldName, ".")
if found {
if child, found := l.childStructs[before]; found {
return child.IsSet(after)
} else {
return false
}
}
_, found = l.allFields[fieldName]
return found
}
// Pos returns the position of the field in the source code
//
// If the field is not found, the closest position to where
// the field should have been will be returned
//
// You can reference a child struct field with `.`; i.e. `parent.child`
func (l *LiteralStruct) Pos(fieldName string) token.Pos {
before, after, found := strings.Cut(fieldName, ".")
if found {
if child, found := l.childStructs[before]; found {
return child.Pos(after)
} else {
return l.ast.Pos()
}
}
value, found := l.allFields[fieldName]
if found {
return value.Pos()
} else {
return l.ast.Pos()
}
}
// Expr returns ast.Expr for the given field name.
//
// If the field is known, it returns the ast.Expr
// If the field is not known, it returns nil
//
// You can reference a child struct field with `.`; i.e. `parent.child`
func (l *LiteralStruct) Expr(fieldName string) ast.Expr {
// Recurse into child fields
before, after, found := strings.Cut(fieldName, ".")
if found {
if child, found := l.childStructs[before]; found {
return child.Expr(after)
} else {
return nil
}
}
value, found := l.allFields[fieldName]
if found {
return value
} else {
return nil
}
}
// Value returns the value of the field as a constant.Value. If the field is not constant or
// does not exist, an unknown value will be returned
//
// You can reference a child struct field with `.`; i.e. `parent.child`
func (l *LiteralStruct) Value(fieldName string) constant.Value {
// Recurse into child fields
before, after, found := strings.Cut(fieldName, ".")
if found {
if child, found := l.childStructs[before]; found {
return child.Value(after)
} else {
return constant.MakeUnknown()
}
}
value, found := l.constantFields[fieldName]
if !found {
return constant.MakeUnknown()
}
return value
}
// Int64 returns the value of the field as an int64
//
// This function will convert other number types into an Int64, but will not convert strings.
// If after conversion the value is 0, the defaultValue will be returned
//
// You can reference a child struct field with `.`; i.e. `parent.child`
func (l *LiteralStruct) Int64(fieldName string, defaultValue int64) int64 {
realValue, ok := constant.Int64Val(constant.ToInt(l.Value(fieldName)))
if !ok || realValue == 0 {
return defaultValue
}
return realValue
}
// Str returns the value of the field as an string
//
// This function will convert all types to a string
// If after conversion the value is "", the defaultValue will be returned
//
// You can reference a child struct field with `.`; i.e. `parent.child`
func (l *LiteralStruct) Str(fieldName string, defaultValue string) string {
value := l.Value(fieldName)
str := value.ExactString()
if value.Kind() == constant.String || value.Kind() == constant.Unknown {
str = constant.StringVal(value)
}
if str == "" {
return defaultValue
} else {
return str
}
}