-
Notifications
You must be signed in to change notification settings - Fork 24
/
functions.go
52 lines (44 loc) · 1.12 KB
/
functions.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
package main
import "fmt"
// A Function definition.
type Function struct {
Name string // C name of the function
GoName string // Go name of the function with the API prefix stripped
Parameters []Parameter
Return Type
Overloads []Overload
}
// An Overload describes an alternative signature for the same function.
type Overload struct {
GoName string // Go name of the original function
OverloadName string // Go name of the overload
Parameters []Parameter
Return Type
}
// A Parameter to a Function.
type Parameter struct {
Name string
Type Type
}
// CName returns a C-safe parameter name.
func (p Parameter) CName() string {
return renameIfReservedCWord(p.Name)
}
// GoName returns a Go-safe parameter name.
func (p Parameter) GoName() string {
return renameIfReservedGoWord(p.Name)
}
func renameIfReservedCWord(word string) string {
switch word {
case "near", "far":
return fmt.Sprintf("x%s", word)
}
return word
}
func renameIfReservedGoWord(word string) string {
switch word {
case "func", "type", "struct", "range", "map", "string":
return fmt.Sprintf("x%s", word)
}
return word
}