Documentation ¶
Index ¶
- Variables
- func CastExpr(p *program.Program, expr goast.Expr, cFromType, cToType string) (_ goast.Expr, err2 error)
- func GetAmountArraySize(cType string, p *program.Program) (size int, err error)
- func GetArrayTypeAndSize(s string) (string, int)
- func GetBaseType(s string) string
- func GetDereferenceType(cType string) (_ string, err error)
- func IsCArray(s string, p *program.Program) bool
- func IsCFloat(p *program.Program, cType string) bool
- func IsCInteger(p *program.Program, cType string) bool
- func IsCPointer(s string, p *program.Program) bool
- func IsCUnsignedType(s string) bool
- func IsDereferenceType(cType string) bool
- func IsGoBaseType(ctype string) bool
- func IsNullExpr(n goast.Expr) bool
- func IsPointer(s string, p *program.Program) bool
- func IsSigned(p *program.Program, cType string) bool
- func IsTypedefFunction(p *program.Program, s string) bool
- func ResolveType(p *program.Program, s string) (resolveResult string, err error)
- func ResolveTypeForBinaryOperator(p *program.Program, operator, leftType, rightType string) string
- func SeparateFunction(p *program.Program, s string) (prefix string, fields []string, returns []string, err error)
- 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 ¶
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 flexibility 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 seemingly) 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 GetAmountArraySize ¶
GetAmountArraySize - return amount array size Example : In : 'char [40]' Out : 40
func GetArrayTypeAndSize ¶
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 GetBaseType ¶
GetBaseType - return base type without pointera, array symbols Input: s = struct BSstructSatSShomeSlepriconSgoSsrcSgithubPcomSD260D18E [7]
func GetDereferenceType ¶
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 IsCInteger ¶
IsCInteger - return true is C type integer
func IsCPointer ¶
IsCPointer - check C type is pointer
func IsCUnsignedType ¶
func IsDereferenceType ¶
IsDereferenceType - check is that type dereference
func IsGoBaseType ¶
func IsNullExpr ¶
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 expression 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 IsTypedefFunction ¶
IsTypedefFunction - return true if that type is typedef of function.
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 ¶
ResolveTypeForBinaryOperator determines the result Go type when performing a binary expression.
Types ¶
This section is empty.