util

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: 11 Imported by: 0

Documentation

Index

Constants

View Source
const C4GoPostFixForAvoid string = "_c4go_postfix"

Variables

This section is empty.

Functions

func Atoi

func Atoi(s string) int

Atoi converts a string to an integer in cases where we are sure that s will be a valid integer, otherwise it will panic.

func CleanCType

func CleanCType(s string) (out string)

CleanCType - remove from C type not Go type

func ConvertFunctionNameFromCtoGo

func ConvertFunctionNameFromCtoGo(name string) string

ConvertFunctionNameFromCtoGo - convert function name fromC to Go

func ConvertToUnsigned

func ConvertToUnsigned(expr goast.Expr, returnType string) goast.Expr

ConvertToUnsigned - return convertion from signed to unsigned type

s := func() uint {
		var x int64
		x = -1
		return uint(x)
	}()

func GenerateCorrectType

func GenerateCorrectType(name string) (result string)

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

func GetExportedName

func GetExportedName(field string) string

GetExportedName returns a deterministic and Go safe name for a C type. For example, "*__foo[]" will return "FooSlice".

func GetRegex

func GetRegex(rx string) *regexp.Regexp

GetRegex return regexp added for minimaze regexp compilation

func InStrings

func InStrings(item string, items []string) bool

InStrings returns true if item exists in items. It must be an exact string match.

func IsAValidFunctionName

func IsAValidFunctionName(s string) bool

IsAValidFunctionName performs a check to see if a string would make a valid function name in Go. Go allows unicode characters, but C doesn't.

func IsFunction

func IsFunction(s string) bool

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

func IsGoKeyword

func IsGoKeyword(w string) bool

IsGoKeyword will return true if a word is one of the reserved words in Go. This means that it cannot be used as an identifier, function name, etc.

The list of reserved words has been taken from the spec at https://golang.org/ref/spec#Keywords

func IsGoPackage

func IsGoPackage(w string) bool

IsGoPackage Go packages

func IsLastArray

func IsLastArray(s string) bool

IsLastArray - check type have array '[]'

func NewAnonymousFunction

func NewAnonymousFunction(body, deferBody []goast.Stmt,
	returnValue goast.Expr,
	returnType string) *goast.CallExpr

NewAnonymousFunction - create a new anonymous function. Example:

func() returnType{
		defer func(){
			deferBody
		}()
		body
		return returnValue
}

func NewBinaryExpr

func NewBinaryExpr(left goast.Expr, operator token.Token, right goast.Expr,
	returnType string, stmt bool) goast.Expr

NewBinaryExpr create a new Go AST binary expression with a left, operator and right operand.

You should use this instead of BinaryExpr directly so that nil left and right operands can be caught (and panic) before Go tried to render the source - which would result in a very hard to debug error.

Assignment operators in C can be nested inside other expressions, like:

a + (b += 3)

In Go this is not allowed. Since the operators mutate variables it is not possible in some cases to move the statements before or after. The only safe (and generic) way around this is to create an immediately executing closure, like:

a + (func () int { b += 3; return b }())

In a lot of cases this may be unnecessary and obfuscate the Go output but these will have to be optimised over time and be strict about the situation they are simplifying.

If stmt is true then the binary expression is the whole statement. This means that the closure above does not need to applied. This makes the output code much neater.

func NewCallExpr

func NewCallExpr(functionName string, args ...goast.Expr) *goast.CallExpr

NewCallExpr creates a new *"go/ast".CallExpr with each of the arguments (after the function name) being each of the expressions that represent the individual arguments.

The function name is checked with IsAValidFunctionName and will panic if the function name is deemed to be not valid.

func NewExprStmt

func NewExprStmt(expr goast.Expr) *goast.ExprStmt

NewExprStmt returns a new ExprStmt from an expression. It is used when converting a single expression into a statement for another receiver.

It is recommended you use this method of instantiating the ExprStmt yourself because NewExprStmt will check that the expr is not nil (or panic). This is much more helpful when trying to debug why the Go source build crashes because of a nil pointer - which eventually leads back to a nil expr.

func NewFloatLit

func NewFloatLit(value float64) *goast.BasicLit

NewFloatLit creates a new Float Literal.

func NewFuncClosure

func NewFuncClosure(returnType string, stmts ...goast.Stmt) *goast.CallExpr

NewFuncClosure creates a new *"go/ast".CallExpr that calls a function literal closure. The first argument is the Go return type of the closure, and the remainder of the arguments are the statements of the closure body.

func NewFuncType

func NewFuncType(fieldList *goast.FieldList, returnType string, addDefaultReturn bool) *goast.FuncType

NewFuncType - create a new function type, example: func ...(fieldList)(returnType)

func NewGoExpr

func NewGoExpr(expr string) goast.Expr

NewGoExpr is used to facilitate the creation of go AST

It should not be used to transpile user code.

func NewIdent

func NewIdent(name string) *goast.Ident

NewIdent - create a new Go ast Ident

func NewIntLit

func NewIntLit(value int) *goast.BasicLit

NewIntLit - create a Go ast BasicLit for `INT` value

func NewNil

func NewNil() *goast.Ident

NewNil returns a Go AST identity that can be used to represent "nil".

func NewStringLit

func NewStringLit(value string) *goast.BasicLit

NewStringLit returns a new Go basic literal with a string value.

func NewTypeIdent

func NewTypeIdent(name string) goast.Expr

NewTypeIdent created a new Go identity that is to be used for a Go type. This is different from NewIdent in how the input string is validated.

func NewUnaryExpr

func NewUnaryExpr(expr goast.Expr, operator token.Token) *goast.UnaryExpr

NewUnaryExpr creates a new Go unary expression. You should use this function instead of instantiating the UnaryExpr directly because this function has extra error checking.

func PanicIfNil

func PanicIfNil(check interface{}, message string)

PanicIfNil will panic with the message provided if the check is nil. This is a convenience method to avoid many similar if statements.

func PanicOnError

func PanicOnError(err error, message string)

PanicOnError will panic with the message and error if the error is not nil. If the error is nil (no error) then nothing happens.

func ParseFunction

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

ParseFunction - parsing elements of C function

func ShowDiff

func ShowDiff(a, b string) string

ShowDiff will print two strings vertically next to each other so that line differences are easier to read.

func Ucfirst

func Ucfirst(word string) string

Ucfirst returns the word with the first letter uppercased; none of the other letters in the word are modified. For example "fooBar" would return "FooBar".

Types

This section is empty.

Jump to

Keyboard shortcuts

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