forked from go-llvm/llgo
/
types.go
210 lines (190 loc) · 5.52 KB
/
types.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
/*
Copyright (c) 2011, 2012 Andrew Wilkins <axwalk@gmail.com>
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
package llgo
import (
"fmt"
"github.com/axw/llgo/types"
"go/ast"
"math/big"
)
// Get a Type from an ast object.
func (c *compiler) ObjGetType(obj *ast.Object) types.Type {
if obj != nil {
switch type_ := obj.Type.(type) {
case types.Type:
return type_
}
switch x := (obj.Decl).(type) {
case *ast.TypeSpec:
c.VisitTypeSpec(x)
type_, _ := (obj.Type).(types.Type)
return type_
}
}
return nil
}
func (c *compiler) GetType(expr ast.Expr) types.Type {
switch x := (expr).(type) {
case *ast.Ellipsis:
return c.GetType(x.Elt)
default:
value := c.VisitExpr(expr)
return value.(TypeValue).typ
}
return nil
}
func (c *compiler) VisitArrayType(a *ast.ArrayType) TypeValue {
elttype := c.GetType(a.Elt)
if a.Len == nil {
return TypeValue{&types.Slice{Elt: elttype}}
}
result := &types.Array{Elt: elttype}
_, isellipsis := a.Len.(*ast.Ellipsis)
if !isellipsis {
lenvalue := c.VisitExpr(a.Len)
constval, isconst := lenvalue.(ConstValue)
if !isconst {
panic("Array length must be a constant integer expression")
}
intval, isint := (constval.Val).(*big.Int)
if !isint {
panic("Array length must be a constant integer expression")
}
result.Len = uint64(intval.Int64())
}
return TypeValue{result}
}
func (c *compiler) VisitFuncType(f *ast.FuncType) TypeValue {
var fn_type types.Func
if f.Params != nil && len(f.Params.List) > 0 {
final_param_type := f.Params.List[len(f.Params.List)-1].Type
if _, varargs := final_param_type.(*ast.Ellipsis); varargs {
fn_type.IsVariadic = true
}
for i := 0; i < len(f.Params.List); i++ {
namecount := len(f.Params.List[i].Names)
typ := c.GetType(f.Params.List[i].Type)
if namecount == 0 {
arg := ast.NewObj(ast.Var, "_")
arg.Type = typ
fn_type.Params = append(fn_type.Params, arg)
} else {
args := make([]*ast.Object, namecount)
for j := 0; j < namecount; j++ {
ident := f.Params.List[i].Names[j]
if ident != nil {
args[j] = ident.Obj
} else {
args[j] = ast.NewObj(ast.Var, "_")
}
args[j].Type = typ
}
fn_type.Params = append(fn_type.Params, args...)
}
}
}
if f.Results != nil {
for i := 0; i < len(f.Results.List); i++ {
namecount := len(f.Results.List[i].Names)
typ := c.GetType(f.Results.List[i].Type)
if namecount > 0 {
results := make([]*ast.Object, namecount)
for j := 0; j < namecount; j++ {
ident := f.Results.List[i].Names[j]
if ident != nil {
results[j] = ident.Obj
} else {
results[j] = ast.NewObj(ast.Var, "_")
}
results[j].Type = typ
}
fn_type.Results = append(fn_type.Results, results...)
} else {
result := ast.NewObj(ast.Var, "_")
result.Type = typ
fn_type.Results = append(fn_type.Results, result)
}
}
}
return TypeValue{&fn_type}
}
func (c *compiler) VisitStructType(s *ast.StructType) TypeValue {
var typ = new(types.Struct)
if s.Fields != nil && s.Fields.List != nil {
tags := make(map[*ast.Object]string)
var i int = 0
for _, field := range s.Fields.List {
fieldtype := c.GetType(field.Type)
if field.Names != nil {
for _, name := range field.Names {
obj := name.Obj
if obj == nil {
obj = ast.NewObj(ast.Var, "_")
}
obj.Type = fieldtype
typ.Fields = append(typ.Fields, obj)
if field.Tag != nil {
tags[obj] = field.Tag.Value
}
}
i += len(field.Names)
} else {
obj := ast.NewObj(ast.Var, "_")
obj.Type = fieldtype
typ.Fields = append(typ.Fields, obj)
if field.Tag != nil {
tags[obj] = field.Tag.Value
}
i++
}
}
typ.Tags = make([]string, len(typ.Fields))
for i, field := range typ.Fields {
// TODO unquote string?
typ.Tags[i] = tags[field]
}
}
return TypeValue{typ}
}
func (c *compiler) VisitInterfaceType(i *ast.InterfaceType) TypeValue {
var iface types.Interface
if i.Methods != nil && i.Methods.List != nil {
for _, field := range i.Methods.List {
if field.Names == nil {
// If field.Names is nil, then we have an embedded interface.
fmt.Println("==nil")
embedded := c.GetType(field.Type)
embedded_iface, isiface := embedded.(*types.Interface)
if isiface {
iface.Methods = append(iface.Methods,
embedded_iface.Methods...)
}
} else {
// TODO
fmt.Println("!=nil")
}
}
}
return TypeValue{&iface}
}
func (c *compiler) VisitMapType(m *ast.MapType) TypeValue {
k, v := c.GetType(m.Key), c.GetType(m.Value)
return TypeValue{&types.Map{Key: k, Elt: v}}
}
// vim: set ft=go :