-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtype.go
179 lines (159 loc) · 3.94 KB
/
type.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
package main
import (
"bytes"
"fmt"
"strings"
"github.com/llir/llvm/ir/types"
)
// TypeDefinition returns the definition (not just the name) of t.
func TypeDefinition(t types.Type) (string, error) {
switch t := t.(type) {
case *types.ArrayType:
elemType, err := TypeSpec(t.ElemType)
if err != nil {
return "", err
}
return fmt.Sprintf("[%d]%s", t.Len, elemType), nil
case *types.FloatType:
switch t.Kind {
case types.FloatKindFloat:
return "float32", nil
case types.FloatKindDouble, types.FloatKindX86_FP80:
return "float64", nil
default:
return "", fmt.Errorf("unsupported floating-point type: %v", t.Kind)
}
case *types.FuncType:
b := new(bytes.Buffer)
b.WriteString("func(")
for i, p := range t.Params {
if i != 0 {
b.WriteString(", ")
}
pt, err := TypeSpec(p)
if err != nil {
return "", fmt.Errorf("error converting type of parameter %d (%v): %v", i, p, err)
}
b.WriteString(pt)
}
b.WriteString(")")
if !types.Equal(t.RetType, types.Void) {
b.WriteString(" ")
rt, err := TypeSpec(t.RetType)
if err != nil {
return "", fmt.Errorf("error converting return type (%v): %v", t.RetType, err)
}
b.WriteString(rt)
}
return b.String(), nil
case *types.IntType:
switch {
case t.BitSize == 1:
return "bool", nil
case t.BitSize <= 8:
return "byte", nil
default:
return fmt.Sprintf("int%d", t.BitSize), nil
}
case *types.PointerType:
if _, ok := t.ElemType.(*types.FuncType); ok {
// Translate a C function pointer type as a Go function type.
return TypeDefinition(t.ElemType)
}
elemType, err := TypeSpec(t.ElemType)
if err != nil {
return "", err
}
return "*" + elemType, nil
case *types.StructType:
b := new(bytes.Buffer)
b.WriteString("struct {\n")
for i, field := range t.Fields {
fieldType, err := TypeSpec(field)
if err != nil {
return "", fmt.Errorf("error converting type of field %d: %v", i, err)
}
fmt.Fprintf(b, "\tF%d %s\n", i, fieldType)
}
b.WriteString("}")
return b.String(), nil
case *types.VectorType:
elemType, err := TypeSpec(t.ElemType)
if err != nil {
return "", err
}
return fmt.Sprintf("[%d]%s", t.Len, elemType), nil
default:
return "", fmt.Errorf("unsupported type %T", t)
}
}
// TypeSpec returns the name (if it has one) or the definition of t.
func TypeSpec(t types.Type) (string, error) {
if name := TypeName(t); name != "" {
return name, nil
}
return TypeDefinition(t)
}
// TypeName returns t's name, or the empty string if t is not a named type.
func TypeName(t types.Type) string {
name := t.Name()
name = strings.TrimPrefix(name, "struct.")
name = strings.TrimPrefix(name, "union.")
if name == "anon" {
return ""
}
if renamed, ok := libraryTypes[name]; ok {
return renamed
}
return name
}
var libraryTypes = map[string]string{
"FILE": "os.File",
}
// compatiblePointerTypes returns whether casting t1 to t2 can be allowed without
// causing too many problems for the garbage collector.
func compatiblePointerTypes(t1, t2 types.Type) bool {
var e1, e2 types.Type
if t1, ok := t1.(*types.PointerType); ok {
e1 = t1.ElemType
} else {
return false
}
if t2, ok := t2.(*types.PointerType); ok {
e2 = t2.ElemType
} else {
return false
}
if types.Equal(e1, e2) {
return true
}
return !hasPointers(e1) && !hasPointers(e2)
}
// hasPointers returns whether t contains pointers.
func hasPointers(t types.Type) bool {
switch t := t.(type) {
case *types.ArrayType:
return hasPointers(t.ElemType)
case *types.FloatType:
return false
case *types.FuncType:
return true
case *types.IntType:
return false
case *types.PointerType:
return true
case *types.StructType:
for _, f := range t.Fields {
if hasPointers(f) {
return true
}
}
return false
case *types.VectorType:
return hasPointers(t.ElemType)
default:
// We don't know if it contains pointers, so we assume it does,
// since that means we'll be more careful with it.
return true
}
}