types

package
v0.24.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 17, 2018 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NullPointer = "NullPointerType *"

NullPointer - is look : (double *)(nil) or (FILE *)(nil) created only for transpiler.CStyleCastExpr

View Source
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:

  1. If fromType == toType (casting to the same type) OR toType == "void *", the original expression is returned unmodified.
  1. 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.

  1. 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.

  1. 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

func CleanCType(s string) (out string)

CleanCType - remove from C type not Go type

func GenerateCorrectType added in v0.19.6

func GenerateCorrectType(name string) string

GenerateCorrectType - generate correct type Example: 'union (anonymous union at tests/union.c:46:3)'

func GetAmountArraySize added in v0.21.1

func GetAmountArraySize(cType string) (size int, err error)

GetAmountArraySize - return amount array size Example : In : 'char [40]' Out : 40

func GetArrayTypeAndSize added in v0.11.0

func GetArrayTypeAndSize(s string) (string, int)

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 added in v0.21.13

func GetBaseType(s string) string

GetBaseType - return base type without pointera, array symbols Input: s = struct BSstructSatSShomeSlepriconSgoSsrcSgithubPcomSelliotchanceSc2goStestsSstructPcD260D18E [7]

func GetDereferenceType added in v0.9.0

func GetDereferenceType(cType string) (_ string, err error)

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 added in v0.21.2

func IsCInteger(p *program.Program, cType string) bool

IsCInteger - return true is C type integer

func IsDereferenceType added in v0.19.1

func IsDereferenceType(cType string) bool

IsDereferenceType - check is that type dereference

func IsFunction added in v0.16.14

func IsFunction(s string) bool

IsFunction - return true if string is function like "void (*)(void)"

func IsLastArray added in v0.21.2

func IsLastArray(s string) bool

IsLastArray - check type have array '[]'

func IsNullExpr added in v0.10.0

func IsNullExpr(n goast.Expr) bool

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 IsPointer added in v0.19.3

func IsPointer(s string) bool

IsPointer - check type is pointer

func IsTypedefFunction added in v0.19.2

func IsTypedefFunction(p *program.Program, s string) bool

IsTypedefFunction - return true if that type is typedef of function.

func ParseFunction added in v0.16.14

func ParseFunction(s string) (f []string, r []string, err error)

ParseFunction - parsing elements of C function

func ResolveType

func ResolveType(p *program.Program, s string) (_ string, err error)

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:

  1. 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.
  1. 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.
  1. 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.
  1. 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

func ResolveTypeForBinaryOperator(p *program.Program, operator, leftType, rightType string) string

ResolveTypeForBinaryOperator determines the result Go type when performing a binary expression.

func SeparateFunction added in v0.21.11

func SeparateFunction(p *program.Program, s string) (
	fields []string, returns []string, err error)

SeparateFunction separate a function C type to Go types parts.

func SizeOf added in v0.11.1

func SizeOf(p *program.Program, cType string) (size int, err error)

SizeOf returns the number of bytes for a type. This the same as using the sizeof operator/function in C.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL