program

package
v0.14.3 Latest Latest
Warning

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

Go to latest
Published: Aug 12, 2017 License: MIT Imports: 10 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

This section is empty.

Functions

func AddFunctionDefinition added in v0.9.2

func AddFunctionDefinition(f FunctionDefinition)

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

Types

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

func GetFunctionDefinition added in v0.9.2

func GetFunctionDefinition(functionName string) *FunctionDefinition

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

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 intefere 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
	// 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() *Program

NewProgram creates a new blank program.

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

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

GetNextIdentifier generates a new gloablly 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) 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) 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{}
}

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