This repository has been archived by the owner on Jul 7, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 39
/
array.go
104 lines (93 loc) · 2.89 KB
/
array.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
// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package main
import "rsc.io/c2go/cc"
// fixArrays rewrites uses of the untyped "Array" container defined in cmd/gc
// to use native Go slices.
// It has nothing to do with standard C arrays.
func fixArray(fn *cc.Decl, x *cc.Expr) {
// arraynew(n, sizeof(T)) becomes Go make([]T, 0, n).
if isCall(x, "arraynew") {
if len(x.List) != 2 {
fprintf(x.Span, "wrong number of arguments to arraynew")
return
}
if x.List[1].Op != cc.SizeofType {
fprintf(x.Span, "second argument to arraynew must be sizeof(T)")
return
}
x.Left.Text = "make"
x.Left.XDecl = nil
typ := &cc.Type{Kind: Slice, Base: x.List[1].Type}
x.XType = typ
x.List = append(x.List, x.List[0])
x.List[1] = &cc.Expr{Op: cc.Number, Text: "0"}
x.List[0] = &cc.Expr{Op: ExprType, Type: typ}
return
}
// arraylength(x) becomes len(x)
if isCall(x, "arraylength") {
x.Left.Text = "len"
x.Left.XDecl = nil
return
}
// arrayset is unused in practice!
// arrayadd(x, &elem) becomes x = append(x, elem).
// Strictly speaking, this is not a complete translation,
// because in the C code x might be a pointer taken from
// another place, and arrayadd changes the len at that
// other place too. In cmd/gc this does not happen.
if isCall(x, "arrayadd") {
if len(x.List) != 2 {
fprintf(x.Span, "wrong number of arguments to arrayadd")
return
}
if x.List[1].Op != cc.Addr {
fprintf(x.Span, "second argument to arrayadd must be &x, have %v", x.List[1])
return
}
append := copyExpr(x)
append.Left.Text = "append"
append.Left.XDecl = nil
x.Op = cc.Eq
x.Left = append.List[0]
x.Right = append
append.List[1] = append.List[1].Left
return
}
// *(T**)(arrayget(x, i)) turns into x[i].
// Record that x should have translated type T*.
if x.Op == cc.Indir && x.Left.Op == cc.Cast && x.Left.Type.Kind == cc.Ptr && x.Left.Type.Base.Kind == cc.Ptr && isCall(x.Left.Left, "arrayget") {
call := x.Left.Left
x.Op = cc.Index
x.XType = x.Left.Type.Base
x.Left = call.List[0]
x.Right = call.List[1]
saveSliceType(x.Left, x.XType)
return
}
// TODO: arraysort
}
func fixArrayStmt(fn *cc.Decl, x *cc.Stmt) {
// Turn call to arrayfree into empty statement.
// This is the only statment-level operation.
// All other rewrites are done at the expression level
// (or pseudo-expression, since assignments count as
// expressions in our representation).
if x.Op == cc.StmtExpr && isCall(x.Expr, "arrayfree") {
x.Op = cc.Empty
x.Expr = nil
}
}
func isCall(x *cc.Expr, name string) bool {
return x != nil && x.Op == cc.Call && x.Left.Op == cc.Name && x.Left.Text == name
}
func saveSliceType(x *cc.Expr, elem *cc.Type) {
switch x.Op {
case cc.Name, cc.Arrow, cc.Dot:
if x.XDecl != nil {
x.XDecl.Type = &cc.Type{Kind: Slice, Base: elem}
}
}
}