types

package
v0.0.0-...-78f7ddb Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2020 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

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:

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

  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 GetAmountArraySize

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

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

func GetArrayTypeAndSize

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

func GetBaseType(s string) string

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

func GetDereferenceType

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 IsCArray

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

IsCArray - check C type is array

func IsCFloat

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

IsCInteger - return true is C type integer

func IsCInteger

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

IsCInteger - return true is C type integer

func IsCPointer

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

IsCPointer - check C type is pointer

func IsCUnsignedType

func IsCUnsignedType(s string) bool

func IsDereferenceType

func IsDereferenceType(cType string) bool

IsDereferenceType - check is that type dereference

func IsGoBaseType

func IsGoBaseType(ctype string) bool

func IsNullExpr

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 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 IsPointer

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

IsPointer - check type is pointer

func IsSigned

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

func IsTypedefFunction

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

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

func ResolveType

func ResolveType(p *program.Program, s string) (resolveResult 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

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

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

func SeparateFunction

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

SeparateFunction separate a function C type to Go types parts.

func SizeOf

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