types

package
v0.16.8 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2017 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CastExpr added in v0.10.0

func CastExpr(p *program.Program, expr goast.Expr, fromType, toType string) (goast.Expr, 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 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 type return will be an empty string, and the size will be -1.

func GetDereferenceType added in v0.9.0

func GetDereferenceType(cType string) (string, 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 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 ResolveType

func ResolveType(p *program.Program, s string) (string, 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 avilable 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 SizeOf added in v0.11.1

func SizeOf(p *program.Program, cType string) (int, 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