Documentation ¶
Index ¶
- Variables
- func CastExpr(p *program.Program, expr goast.Expr, cFromType, cToType string) (_ goast.Expr, err2 error)
- func CleanCType(s string) (out string)
- func GenerateCorrectType(name string) string
- func GetArrayTypeAndSize(s string) (string, int)
- func GetDereferenceType(cType string) (_ string, err error)
- func IsDereferenceType(cType string) bool
- func IsFunction(s string) bool
- func IsNullExpr(n goast.Expr) bool
- func IsPointer(s string) bool
- func IsTypedefFunction(p *program.Program, s string) bool
- func ParseFunction(s string) (f []string, r []string, err error)
- func ResolveFunction(p *program.Program, s string) (fields []string, returns []string, err error)
- func ResolveType(p *program.Program, s string) (_ string, err error)
- func ResolveTypeForBinaryOperator(p *program.Program, operator, leftType, rightType string) string
- func SizeOf(p *program.Program, cType string) (size int, err error)
Constants ¶
This section is empty.
Variables ¶
var NullPointer = "NullPointerType *"
NullPointer - is look : (double *)(nil) or (FILE *)(nil) created only for transpiler.CStyleCastExpr
var ToVoid = "ToVoid"
ToVoid - specific type for ignore the cast
Functions ¶
func CastExpr ¶ added in v0.10.0
func CastExpr(p *program.Program, expr goast.Expr, cFromType, cToType string) (_ goast.Expr, err2 error)
CastExpr returns an expression that casts one type to another. For reliability and flexability the existing type (fromType) must be structly provided.
There are lots of rules about how an expression is cast, but here are some main points:
- If fromType == toType (casting to the same type) OR toType == "void *", the original expression is returned unmodified.
There is a special type called "null" which is not defined in C, but rather an estimate of the NULL macro which evaluates to: (0). We cannot guarantee that original C used the NULL macro but it is a safe assumption for now.
The reason why NULL is special (or at least seamingly) is that it is often used in different value contexts. As a number, testing pointers and strings. Being able to better understand the original purpose of the code helps to generate cleaner and more Go-like output.
There is a set of known primitive number types like "int", "float", etc. These we know can be safely cast between each other by using the data type as a function. For example, 3 (int) to a float would produce: "float32(3)".
There are also some platform specific types and types that are shared in Go packages that are common aliases kept in this list.
- If all else fails the fallback is to cast using a function. For example, Foo -> Bar, would return an expression similar to "noarch.FooToBar(expr)". This code would certainly fail with custom types, but that would likely be a bug. It is most useful to do this when dealing with compound types like FILE where those function probably exist (or should exist) in the noarch package.
func CleanCType ¶ added in v0.17.7
CleanCType - remove from C type not Go type
func GenerateCorrectType ¶ added in v0.19.6
GenerateCorrectType - generate correct type Example: 'union (anonymous union at tests/union.c:46:3)'
func GetArrayTypeAndSize ¶ added in v0.11.0
GetArrayTypeAndSize returns the size and type of a fixed array. If the type is not an array with a fixed size then the the size will be -1 and the returned type should be ignored.
func GetDereferenceType ¶ added in v0.9.0
GetDereferenceType returns the C type that would be the result of dereferencing (unary "*" operator or accessing a single array element on a pointer) a value.
For example if the input type is "char *", then dereferencing or accessing a single element would result in a "char".
If the dereferenced type cannot be determined or is impossible ("char" cannot be dereferenced, for example) then an error is returned.
func IsDereferenceType ¶ added in v0.19.1
IsDereferenceType - check is that type dereference
func IsFunction ¶ added in v0.16.14
IsFunction - return true if string is function like "void (*)(void)"
func IsNullExpr ¶ added in v0.10.0
IsNullExpr tries to determine if the expression is the result of the NULL macro. In C, NULL is actually a macro that produces an expression like "(0)".
There are no guarantees if the original C code used the NULL macro, but it is usually a pretty good guess when we see this specific exression signature.
Either way the return value from IsNullExpr should not change the functionality of the code but can lead to hints that allow the Go produced to be cleaner and more Go-like.
func ParseFunction ¶ added in v0.16.14
ParseFunction - parsing elements of C function
func ResolveFunction ¶ added in v0.16.14
ResolveFunction determines the Go type from a C type.
func ResolveType ¶
ResolveType determines the Go type from a C type.
Some basic examples are obvious, such as "float" in C would be "float32" in Go. But there are also much more complicated examples, such as compound types (structs and unions) and function pointers.
Some general rules:
- The Go type must be deterministic. The same C type will ALWAYS return the same Go type, in any condition. This is extremely important since the nature of C is that is may not have certain information available about the rest of the program or libraries when it is being compiled.
- Many C type modifiers and properties are lost as they have no sensible or valid translation to Go. Some example of those would be "const" and "volatile". It is left be up to the clang (or other compiler) to warn if types are being abused against the standards in which they are being compiled under. Go will make no assumptions about how you expect it act, only how it is used.
- New types are registered (discovered) throughout the transpiling of the program, so not all types are know at any given time. This works exactly the same way in a C compiler that will not let you use a type before it has been defined.
- If all else fails an error is returned. However, a type (which is almost certainly incorrect) "interface{}" is also returned. This is to allow the transpiler to step over type errors and put something as a placeholder until a more suitable solution is found for those cases.
func ResolveTypeForBinaryOperator ¶ added in v0.9.0
ResolveTypeForBinaryOperator determines the result Go type when performing a binary expression.
Types ¶
This section is empty.