-
Notifications
You must be signed in to change notification settings - Fork 7
/
constant.go
148 lines (143 loc) · 3.26 KB
/
constant.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
package gotype
import (
"fmt"
"go/ast"
"go/constant"
"go/token"
"strconv"
)
func constantEval(expr ast.Node, iota int64, info *infoFile) (r constant.Value, err error) {
switch t := expr.(type) {
case *ast.BasicLit:
switch t.Kind {
case token.STRING:
r = constant.MakeString(t.Value[1 : len(t.Value)-1])
return r, nil
case token.INT:
i, err := strconv.ParseInt(t.Value, 0, 0)
if err != nil {
return nil, err
}
r = constant.MakeInt64(i)
return r, nil
case token.FLOAT:
i, err := strconv.ParseFloat(t.Value, 0)
if err != nil {
return nil, err
}
r = constant.MakeFloat64(i)
return r, nil
}
case *ast.UnaryExpr:
x, err := constantEval(t.X, iota, info)
if err != nil {
return nil, err
}
r, err = constantUnaryOp(t.Op, x)
if err != nil {
return nil, err
}
return r, nil
case *ast.BinaryExpr:
x, err := constantEval(t.X, iota, info)
if err != nil {
return nil, err
}
y, err := constantEval(t.Y, iota, info)
if err != nil {
return nil, err
}
r, err = constantBinaryOp(t.Op, x, y)
if err != nil {
return nil, err
}
return r, nil
case *ast.Ident:
if t.Name == "iota" {
r = constant.MakeInt64(iota)
return r, nil
} else if val, ok := info.GetPkgOrType(t.Name); ok && val.Kind() == Declaration {
val = val.Declaration()
switch val.Kind() {
case Int, Int8, Int16, Int32, Int64:
i, err := strconv.ParseInt(val.Value(), 0, 0)
if err != nil {
return nil, err
}
r = constant.MakeInt64(i)
return r, nil
case Uint, Uint8, Uint16, Uint32, Uint64:
i, err := strconv.ParseUint(val.Value(), 0, 0)
if err != nil {
return nil, err
}
r = constant.MakeUint64(i)
return r, nil
case Float32, Float64:
i, err := strconv.ParseFloat(val.Value(), 0)
if err != nil {
return nil, err
}
r = constant.MakeFloat64(i)
return r, nil
case String:
str := val.Value()
r = constant.MakeString(str[1 : len(str)-1])
return r, nil
default:
// iota
i, err := strconv.ParseInt(val.Value(), 0, 0)
if err == nil {
r = constant.MakeInt64(i)
return r, nil
}
}
} else {
return nil, fmt.Errorf("undefined ident")
}
case *ast.ParenExpr:
r, err = constantEval(t.X, iota, info)
if err != nil {
return nil, err
}
return r, nil
case *ast.CallExpr:
if len(t.Args) == 0 {
return nil, fmt.Errorf("undefined call")
}
r, err = constantEval(t.Args[0], iota, info)
if err != nil {
return nil, err
}
return r, nil
default:
return nil, fmt.Errorf("undefined expr")
}
return constant.MakeUnknown(), nil
}
func constantUnaryOp(op token.Token, y constant.Value) (r constant.Value, err error) {
defer func() {
if x := recover(); x != nil {
err = fmt.Errorf("%v", x)
}
}()
r = constant.UnaryOp(op, y, 0)
return r, nil
}
func constantBinaryOp(op token.Token, x, y constant.Value) (r constant.Value, err error) {
defer func() {
if x := recover(); x != nil {
err = fmt.Errorf("%v", x)
}
}()
switch op {
case token.SHL, token.SHR:
n, _ := constant.Uint64Val(y)
r = constant.Shift(x, op, uint(n))
case token.EQL, token.NEQ, token.LSS, token.LEQ, token.GTR, token.GEQ:
r = constant.MakeBool(constant.Compare(x, op, y))
default:
r = constant.BinaryOp(x, op, y)
}
return r, nil
}