program

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

Overview

Package program contains high-level orchestration and state of the input and output program during transpilation.

Index

Constants

This section is empty.

Variables

View Source
var DefinitionType = map[string]string{

	"time_t":                              "github.com/FTwOoO/c4go/noarch.TimeT",
	"github.com/FTwOoO/c4go/noarch.TimeT": "int32",
	"__time_t":                            "int32",
	"__suseconds_t":                       "int32",

	"fpos_t": "int32",

	"ssize_t":                              "github.com/FTwOoO/c4go/noarch.SsizeT",
	"github.com/FTwOoO/c4go/noarch.SsizeT": "long int",

	"bool":                                   "bool",
	"char *":                                 "[]byte",
	"char":                                   "byte",
	"char*":                                  "[]byte",
	"double":                                 "float64",
	"float":                                  "float32",
	"int":                                    "int32",
	"long double":                            "float64",
	"long int":                               "int32",
	"long long":                              "int64",
	"long long int":                          "int64",
	"long long unsigned int":                 "uint64",
	"long unsigned int":                      "uint32",
	"long":                                   "int32",
	"short":                                  "int16",
	"signed char":                            "int8",
	"unsigned char":                          "uint8",
	"unsigned int":                           "uint32",
	"unsigned long long":                     "uint64",
	"unsigned long":                          "uint32",
	"unsigned short":                         "uint16",
	"unsigned short int":                     "uint16",
	"void":                                   "",
	"_Bool":                                  "int32",
	"size_t":                                 "uint32",
	"ptrdiff_t":                              "github.com/FTwOoO/c4go/noarch.PtrdiffT",
	"github.com/FTwOoO/c4go/noarch.PtrdiffT": "uint64",
	"wchar_t":                                "github.com/FTwOoO/c4go/noarch.WcharT",
	"github.com/FTwOoO/c4go/noarch.WcharT":   "rune",

	"void*":  "interface{}",
	"void *": "interface{}",

	"null": "null",

	"mode_t":   "uint16",
	"__mode_t": "uint16",

	"FILE": "github.com/FTwOoO/c4go/noarch.File",

	"tcflag_t": "uint32",
	"cc_t":     "uint8",

	"__rusage_who":   "int",
	"__rusage_who_t": "int",

	"clock_t":                              "github.com/FTwOoO/c4go/noarch.ClockT",
	"github.com/FTwOoO/c4go/noarch.ClockT": "int64",

	"sig_atomic_t": "int64",

	"off_t":   "int64",
	"__off_t": "int64",
}

DefinitionType - conversion map from C standard library structures to c4go structures

View Source
var DefinitionVariable = map[string]string{

	"stdin":  "github.com/FTwOoO/c4go/noarch.Stdin",
	"stdout": "github.com/FTwOoO/c4go/noarch.Stdout",
	"stderr": "github.com/FTwOoO/c4go/noarch.Stderr",

	"_ISupper":  "github.com/FTwOoO/c4go/noarch.ISupper",
	"_ISlower":  "github.com/FTwOoO/c4go/noarch.ISlower",
	"_ISalpha":  "github.com/FTwOoO/c4go/noarch.ISalpha",
	"_ISdigit":  "github.com/FTwOoO/c4go/noarch.ISdigit",
	"_ISxdigit": "github.com/FTwOoO/c4go/noarch.ISxdigit",
	"_ISspace":  "github.com/FTwOoO/c4go/noarch.ISspace",
	"_ISprint":  "github.com/FTwOoO/c4go/noarch.ISprint",
	"_ISgraph":  "github.com/FTwOoO/c4go/noarch.ISgraph",
	"_ISblank":  "github.com/FTwOoO/c4go/noarch.ISblank",
	"_IScntrl":  "github.com/FTwOoO/c4go/noarch.IScntrl",
	"_ISpunct":  "github.com/FTwOoO/c4go/noarch.ISpunct",
	"_ISalnum":  "github.com/FTwOoO/c4go/noarch.ISalnum",

	"TCSANOW":   "github.com/pkg/term/termios.TCSANOW",
	"TCSADRAIN": "github.com/pkg/term/termios.TCSADRAIN",
	"TCSAFLUSH": "github.com/pkg/term/termios.TCSAFLUSH",

	"TCIFLUSH":  "github.com/pkg/term/termios.TCIFLUSH",
	"TCOFLUSH":  "github.com/pkg/term/termios.TCOFLUSH",
	"TCIOFLUSH": "github.com/pkg/term/termios.TCIOFLUSH",

	"TCSETS":  "github.com/pkg/term/termios.TCSETS",
	"TCSETSW": "github.com/pkg/term/termios.TCSETSW",
	"TCSETSF": "github.com/pkg/term/termios.TCSETSF",
	"TCFLSH":  "github.com/pkg/term/termios.TCFLSH",
	"TCSBRK":  "github.com/pkg/term/termios.TCSBRK",
	"TCSBRKP": "github.com/pkg/term/termios.TCSBRKP",

	"IXON":    "github.com/pkg/term/termios.IXON",
	"IXANY":   "github.com/pkg/term/termios.IXANY",
	"IXOFF":   "github.com/pkg/term/termios.IXOFF",
	"CRTSCTS": "github.com/pkg/term/termios.CRTSCTS",

	"TIOCGWINSZ": "golang.org/x/sys/unix.TIOCGWINSZ",
}

DefinitionVariable is map of conversion from C var to C4go variable

View Source
var WarningMessage string = "// Warning "

Functions

func PathSimplification

func PathSimplification(message string) string

Types

type DefinitionFunction

type DefinitionFunction struct {
	// The name of the function, like "printf".
	Name string

	// The C return type, like "int".
	ReturnType string

	// The C argument types, like ["bool", "int"]. There is currently no way
	// to represent a varargs.
	ArgumentTypes []string

	// Each function from some source. For example: "stdio.h"
	IncludeFile string
	// If function called, then true.
	IsCalled bool

	// If function from some C standard library, then true.
	IsCstdFunction bool

	// If this is not empty then this function name should be used instead
	// of the Name. Many low level functions have an exact match with a Go
	// function. For example, "sin()".
	Substitution string

	// Can be overridden with the substitution to rearrange the return variables
	// and parameters. When either of these are nil the behavior is to keep the
	// single return value and parameters the same.
	ReturnParameters []int
	Parameters       []int
	// contains filtered or unexported fields
}

DefinitionFunction contains the prototype definition for a function.

type Program

type Program struct {

	// These are for the output Go AST.
	FileSet *token.FileSet
	File    *goast.File

	// Contains the current function name during the transpilation.
	Function *ast.FunctionDecl

	// The definitions for defined structs.
	// TODO: This field should be protected through proper getters and setters.
	Structs StructRegistry
	Unions  StructRegistry

	// If verbose is on progress messages will be printed immediately as code
	// comments (so that they do not interfere with the program output).
	Verbose bool

	// A map of all the global variables (variables that exist outside of a
	// function) and their types.
	GlobalVariables map[string]string

	// EnumConstantToEnum - a map with key="EnumConstant" and value="enum type"
	// clang don`t show enum constant with enum type,
	// so we have to use hack for repair the type
	EnumConstantToEnum map[string]string

	// EnumTypedefName - a map with key="Name of typedef enum" and
	// value="exist ot not"
	EnumTypedefName map[string]bool

	// TypedefType - map for type alias, for example:
	// C  : typedef int INT;
	// Map: key = INT, value = int
	// Important: key and value are C types
	TypedefType map[string]string

	// preprocessor file
	PreprocessorFile preprocessor.FilePP

	// UnsafeConvertValueToPointer - simplification for convert value to pointer
	UnsafeConvertValueToPointer map[string]bool

	// UnsafeConvertPointerArith - simplification for pointer arithmetic
	UnsafeConvertPointerArith map[string]bool

	// IsHaveVaList
	IsHaveVaList bool

	DoNotAddComments bool
	// contains filtered or unexported fields
}

Program contains all of the input, output and transpition state of a C program to a Go program.

func NewProgram

func NewProgram() (p *Program)

NewProgram creates a new blank program.

func (*Program) AddFunctionDefinition

func (p *Program) AddFunctionDefinition(f DefinitionFunction)

AddFunctionDefinition registers a function definition. If the definition already exists it will be replaced.

func (*Program) AddImport

func (p *Program) AddImport(importPath string)

AddImport will append an absolute import if it is unique to the list of imports for this program.

func (*Program) AddImports

func (p *Program) AddImports(importPaths ...string)

AddImports is a convenience method for adding multiple imports.

func (*Program) AddMessage

func (p *Program) AddMessage(message string) bool

AddMessage adds a message (such as a warning or error) comment to the output file. Usually the message is generated from one of the Generate functions in the ast package.

It is expected that the message already have the comment ("//") prefix.

The message will not be appended if it is blank. This is because the Generate functions return a blank string conditionally when there is no error.

The return value will be true if a message was added, otherwise false.

func (*Program) DefineType

func (p *Program) DefineType(typeName string)

DefineType will record a type as having already been defined. The purpose for this is to not generate Go for a type more than once. C allows variables and other entities (such as function prototypes) to be defined more than once in some cases. An example of this would be static variables or functions.

func (*Program) GenerateWarningMessage

func (p *Program) GenerateWarningMessage(e error, n ast.Node) string

GenerateWarningMessage - generate warning message

func (*Program) GetBaseTypeOfTypedef

func (p *Program) GetBaseTypeOfTypedef(cTypedef string) (
	cBase string, ok bool)

GetBaseTypeOfTypedef - return typedef type

func (*Program) GetComments

func (p *Program) GetComments(n ast.Position) (out []*goast.Comment)

GetComments - return comments

func (*Program) GetCstdFunction

func (p *Program) GetCstdFunction() (src string)

func (*Program) GetFunctionDefinition

func (p *Program) GetFunctionDefinition(functionName string) *DefinitionFunction

GetFunctionDefinition will return nil if the function does not exist (is not registered).

func (*Program) GetIncludeFileNameByFunctionSignature

func (p *Program) GetIncludeFileNameByFunctionSignature(
	functionName, cType string) (includeFileName string, err error)

GetIncludeFileNameByFunctionSignature - return name of C include header in according to function name and type signature

func (*Program) GetMessageComments

func (p *Program) GetMessageComments() (_ *goast.CommentGroup)

GetMessageComments - get messages "Warnings", "Error" like a comment Location of comments only NEAR of error or warning and don't show directly location

func (*Program) GetNextIdentifier

func (p *Program) GetNextIdentifier(prefix string) string

GetNextIdentifier generates a new globally unique identifier name. This can be used for variables and functions in generated code.

The value of prefix is only useful for readability in the code. If the prefix is an empty string then the prefix "__temp" will be used.

func (*Program) GetOutsideCalledFunctions

func (p *Program) GetOutsideCalledFunctions() (ds []DefinitionFunction)

func (*Program) GetStruct

func (p *Program) GetStruct(name string) *Struct

GetStruct returns a struct object (representing struct type or union type) or nil if doesn't exist. This method can get struct or union in the same way and distinguish only by the IsUnion field. `name` argument is the C like `struct a_struct`, it allow pointer type like `union a_union *`. Pointer types used in a DeclRefExpr in the case a deferenced structure by using `->` operator to access to a field like this: a_struct->member .

This method is used in collaboration with the field "c4go/program".*Struct.IsUnion to simplify the code like in function "c4go/transpiler".transpileMemberExpr() where the same *Struct value returned by this method is used in the 2 cases, in the case where the value has a struct type and in the case where the value has an union type.

func (*Program) ImportType

func (p *Program) ImportType(name string) string

ImportType imports a package for a fully qualified type and returns the local type name. For example:

t := p.ImportType("github.com/FTwOoO/c4go/noarch.CtRuneT")

Will import "github.com/FTwOoO/c4go/noarch" and return (value of t) "noarch.CtRuneT".

func (*Program) Imports

func (p *Program) Imports() []string

Imports returns all of the Go imports for this program.

func (*Program) IncludeHeaderIsExists

func (p *Program) IncludeHeaderIsExists(includeHeader string) bool

IncludeHeaderIsExists return true if C #include header is inside list

func (*Program) IsTypeAlreadyDefined

func (p *Program) IsTypeAlreadyDefined(typeName string) bool

IsTypeAlreadyDefined will return true if the typeName has already been defined.

A type could be defined:

1. Initially. That is, before the transpilation starts (hard-coded). 2. By calling DefineType throughout the transpilation.

func (*Program) IsUnion

func (p *Program) IsUnion(cType string) bool

IsUnion - return true if the cType is 'union' or typedef of union

func (*Program) SetCalled

func (p *Program) SetCalled(name string)

func (*Program) String

func (p *Program) String() string

String generates the whole output Go file as a string. This will include the messages at the top of the file and all the rendered Go code.

func (*Program) UndefineType

func (p *Program) UndefineType(typeName string)

UndefineType undefine defined type

type Struct

type Struct struct {
	// The name of the struct.
	Name string

	// IsGlobal is true for case global struct
	IsGlobal bool

	// This field is used to avoid to duplicate code for union case the type is the same.
	Type TypeOfStruct

	// Each of the fields and their C type. The field may be a string or an
	// instance of Struct for nested structures.
	Fields map[string]interface{}

	// int    - position of field
	// string - name of field
	FieldNames map[int]string
}

Struct represents the definition for a C struct.

func NewStruct

func NewStruct(p *Program, n *ast.RecordDecl) (st *Struct, err error)

NewStruct creates a new Struct definition from an ast.RecordDecl.

type StructRegistry

type StructRegistry map[string]*Struct

StructRegistry is a map of Struct for struct types and union type

func (StructRegistry) HasType

func (sr StructRegistry) HasType(typename string) bool

HasType method check if type exists

type TypeOfStruct

type TypeOfStruct int
const (
	UnionType TypeOfStruct = iota
	StructType
	ClassType
)

Jump to

Keyboard shortcuts

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