program

package
v0.26.10 Latest Latest
Warning

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

Go to latest
Published: Aug 14, 2021 License: MIT Imports: 10 Imported by: 32

Documentation

Overview

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

Index

Constants

View Source
const Version = "v0.26.0 Erbium 2020-03-17"

Version can be requested through the command line with:

c2go -v

See https://github.com/elliotchance/c2go/wiki/Release-Process

Variables

This section is empty.

Functions

This section is empty.

Types

type Comment added in v0.21.0

type Comment struct {
	File    string
	Line    int
	Comment string
}

Comment - position of line comment '//...'

type FunctionDefinition added in v0.9.2

type FunctionDefinition 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

	// 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
}

FunctionDefinition contains the prototype definition for a function.

type IncludeHeader added in v0.21.10

type IncludeHeader struct {
	HeaderName   string
	IsUserSource bool
}

IncludeHeader - struct for C include header

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

	// This option is not available through the command line. It is to allow the
	// internal integration testing to generate the output in the form of a
	// Go-test rather than a standalone Go file.
	OutputAsTest bool

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

	// Comments
	Comments []Comment

	// IncludeHeaders - list of C header
	IncludeHeaders []IncludeHeader

	// NodeMap - a map containing all the program's nodes with:
	// key    - the node address
	// value  - the node
	NodeMap map[ast.Address]ast.Node
	// 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 added in v0.21.10

func (p *Program) AddFunctionDefinition(f FunctionDefinition)

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

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

AddImports is a convienience method for adding multiple imports.

func (*Program) AddMessage added in v0.12.3

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) AppendStartupExpr added in v0.11.0

func (p *Program) AppendStartupExpr(e goast.Expr)

AppendStartupExpr is a convienience method for adding a new startup statement that is in the form of an expression.

func (*Program) AppendStartupStatement added in v0.11.0

func (p *Program) AppendStartupStatement(stmt goast.Stmt)

AppendStartupStatement adds a new statement that must be executed when the program starts up before any other code. These are required to setup state for global variables like STDIN that might be referenced by the program.

func (*Program) DeclareType added in v0.21.17

func (p *Program) DeclareType(n *ast.RecordDecl, correctName string) (err error)

DeclareType defines a type without adding the real definition of the type to the list of declarations. This is useful for types which are defined in a header file, but are implemented in another *.c file. This way they can already be used for variable declarations and sizeof.

func (*Program) DefineType added in v0.13.7

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) GenerateErrorMessage added in v0.15.0

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

GenerateErrorMessage - generate error message

func (*Program) GenerateWarningMessage added in v0.15.0

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

GenerateWarningMessage - generate warning message

func (*Program) GenerateWarningOrErrorMessage added in v0.15.0

func (p *Program) GenerateWarningOrErrorMessage(e error, n ast.Node, isError bool) string

GenerateWarningOrErrorMessage - generate error if it happen

func (*Program) GetBaseTypeOfTypedef added in v0.21.11

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

GetBaseTypeOfTypedef - return typedef type

func (*Program) GetComments added in v0.21.0

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

GetComments - return comments

func (*Program) GetFunctionDefinition added in v0.21.10

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

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

func (*Program) GetMessageComments added in v0.18.4

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

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) GetStruct added in v0.13.3

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 "c2go/program".*Struct.IsUnion to simplify the code like in function "c2go/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/elliotchance/c2go/darwin.CtRuneT")

Will import "github.com/elliotchance/c2go/darwin" and return (value of t) "darwin.CtRuneT".

func (*Program) Imports

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

Imports returns all of the Go imports for this program.

func (*Program) IncludeHeaderIsExists added in v0.21.13

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

IncludeHeaderIsExists - return true if C #include header is inside list

func (*Program) IsTypeAlreadyDefined added in v0.13.7

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

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

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

func (*Program) SetNodes added in v0.21.17

func (p *Program) SetNodes(nodes []ast.Node)

SetNodes will add the given nodes and all their children to the program's node map.

func (*Program) StartupStatements added in v0.11.0

func (p *Program) StartupStatements() []goast.Stmt

StartupStatements returns the statements that will be executed before the program starts. These are required to setup state for global variables like STDIN that might be referenced by the program.

func (*Program) String added in v0.12.3

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.

type Struct added in v0.11.1

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

	// True if the struct kind is an union.
	// This field is used to avoid to dupplicate code for union case the type is the same.
	// Plus, this field is used in collaboration with the method "c2go/program".*Program.GetStruct()
	IsUnion bool

	// 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{}

	// Each of the field names in the order they were defined.
	FieldNames []string
}

Struct represents the definition for a C struct.

func NewStruct added in v0.11.1

func NewStruct(n *ast.RecordDecl) *Struct

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

type StructRegistry added in v0.13.3

type StructRegistry map[string]*Struct

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

func (StructRegistry) HasType added in v0.13.3

func (sr StructRegistry) HasType(typename string) bool

HasType method check if type exists

Jump to

Keyboard shortcuts

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