compiler

package
v0.105.1 Latest Latest
Warning

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

Go to latest
Published: Jan 12, 2024 License: MIT Imports: 38 Imported by: 1

Documentation

Overview

Package compiler implements Go to NEF smart contract compiler.

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrMissingExportedParamName is returned when exported contract method has unnamed parameter.
	ErrMissingExportedParamName = errors.New("exported method is not allowed to have unnamed parameter")
	// ErrInvalidExportedRetCount is returned when exported contract method has invalid return values count.
	ErrInvalidExportedRetCount = errors.New("exported method is not allowed to have more than one return value")
	// ErrGenericsUnsuppored is returned when generics-related tokens are encountered.
	ErrGenericsUnsuppored = errors.New("generics are currently unsupported, please, see the https://github.com/nspcc-dev/neo-go/issues/2376")
)

Various exported functions usage errors.

View Source
var ErrUnsupportedTypeAssertion = errors.New("type assertion with two return values is not supported")

ErrUnsupportedTypeAssertion is returned when type assertion statement is not supported by the compiler.

Functions

func Compile

func Compile(name string, r io.Reader) ([]byte, error)

Compile compiles a Go program into a bytecode that can run on the Neo virtual machine. If `r != nil`, `name` is interpreted as a filename, and `r` as file contents. Otherwise `name` is either a file name or a name of the directory containing source files.

func CompileAndSave

func CompileAndSave(src string, o *Options) ([]byte, error)

CompileAndSave will compile and save the file to disk in the NEF format.

func CreateManifest added in v0.94.0

func CreateManifest(di *DebugInfo, o *Options) (*manifest.Manifest, error)

CreateManifest creates manifest and checks that is is valid.

Types

type DebugInfo added in v0.75.0

type DebugInfo struct {
	MainPkg   string            `json:"-"`
	Hash      util.Uint160      `json:"hash"`
	Documents []string          `json:"documents"`
	Methods   []MethodDebugInfo `json:"methods"`
	// NamedTypes are exported structured types that have some name (even
	// if the original structure doesn't) and a number of internal fields.
	NamedTypes map[string]binding.ExtendedType `json:"-"`
	// Events are the events that contract is allowed to emit and that have to
	// be presented in the resulting contract manifest and debug info file.
	Events []EventDebugInfo `json:"events"`
	// EmittedEvents contains events occurring in code, i.e. events emitted
	// via runtime.Notify(...) call in the contract code if they have constant
	// names and doesn't have ellipsis arguments. EmittedEvents are not related
	// to the debug info and are aimed to serve bindings generation.
	EmittedEvents map[string][]EmittedEventInfo `json:"-"`
	// InvokedContracts contains foreign contract invocations.
	InvokedContracts map[util.Uint160][]string `json:"-"`
	// StaticVariables contains a list of static variable names and types.
	StaticVariables []string `json:"static-variables"`
}

DebugInfo represents smart-contract debug information.

func CompileWithOptions added in v0.95.2

func CompileWithOptions(name string, r io.Reader, o *Options) (*nef.File, *DebugInfo, error)

CompileWithOptions compiles a Go program into bytecode with the provided compiler options.

func (*DebugInfo) ConvertToManifest added in v0.91.0

func (di *DebugInfo) ConvertToManifest(o *Options) (*manifest.Manifest, error)

ConvertToManifest converts a contract to the manifest.Manifest struct for debugger. Note: manifest is taken from the external source, however it can be generated ad-hoc. See #1038.

type DebugMethodName added in v0.75.0

type DebugMethodName struct {
	Namespace string
	Name      string
}

DebugMethodName is a combination of a namespace and name.

func (*DebugMethodName) MarshalJSON added in v0.75.0

func (d *DebugMethodName) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (*DebugMethodName) UnmarshalJSON added in v0.75.0

func (d *DebugMethodName) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

type DebugParam added in v0.75.0

type DebugParam struct {
	Name         string                  `json:"name"`
	Type         string                  `json:"type"`
	RealType     binding.Override        `json:"-"`
	ExtendedType *binding.ExtendedType   `json:"-"`
	TypeSC       smartcontract.ParamType `json:"-"`
}

DebugParam represents the variable's name and type.

func (*DebugParam) MarshalJSON added in v0.75.0

func (d *DebugParam) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (*DebugParam) ToManifestParameter added in v0.90.0

func (d *DebugParam) ToManifestParameter() manifest.Parameter

ToManifestParameter converts DebugParam to manifest.Parameter.

func (*DebugParam) UnmarshalJSON added in v0.75.0

func (d *DebugParam) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

type DebugRange added in v0.75.0

type DebugRange struct {
	Start uint16
	End   uint16
}

DebugRange represents the method's section in bytecode.

func (*DebugRange) MarshalJSON added in v0.75.0

func (d *DebugRange) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (*DebugRange) UnmarshalJSON added in v0.75.0

func (d *DebugRange) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

type DebugSeqPoint added in v0.75.0

type DebugSeqPoint struct {
	// Opcode is an opcode's address.
	Opcode int
	// Document is an index of file where sequence point occurs.
	Document int
	// StartLine is the first line of the break-pointed statement.
	StartLine int
	// StartCol is the first column of the break-pointed statement.
	StartCol int
	// EndLine is the last line of the break-pointed statement.
	EndLine int
	// EndCol is the last column of the break-pointed statement.
	EndCol int
}

DebugSeqPoint represents break-point for debugger.

func (*DebugSeqPoint) MarshalJSON added in v0.75.0

func (d *DebugSeqPoint) MarshalJSON() ([]byte, error)

MarshalJSON implements the json.Marshaler interface.

func (*DebugSeqPoint) UnmarshalJSON added in v0.75.0

func (d *DebugSeqPoint) UnmarshalJSON(data []byte) error

UnmarshalJSON implements the json.Unmarshaler interface.

type EmittedEventInfo added in v0.102.0

type EmittedEventInfo struct {
	ExtTypes map[string]binding.ExtendedType
	Params   []DebugParam
}

EmittedEventInfo describes information about single emitted event got from the contract code. It has the map of extended types used as the parameters to runtime.Notify(...) call (if any) and the parameters info itself.

type EventDebugInfo added in v0.75.0

type EventDebugInfo struct {
	ID string `json:"id"`
	// Name is a human-readable event name in a format "{namespace},{name}".
	Name       string       `json:"name"`
	Parameters []DebugParam `json:"params"`
}

EventDebugInfo represents smart-contract's event debug information.

type HybridEvent added in v0.102.0

type HybridEvent struct {
	Name       string            `json:"name"`
	Parameters []HybridParameter `json:"parameters"`
}

HybridEvent represents the description of event emitted by the contract squashed with extended event's parameters description. We have it as a separate type for the user's convenience. It is applied for the smart contract configuration file only.

type HybridParameter added in v0.102.0

type HybridParameter struct {
	manifest.Parameter `yaml:",inline"`
	ExtendedType       *binding.ExtendedType `yaml:"extendedtype,omitempty"`
}

HybridParameter contains the manifest's event parameter description united with the extended type description for this parameter. It is applied for the smart contract configuration file only.

type MethodDebugInfo added in v0.75.0

type MethodDebugInfo struct {
	// ID is the actual name of the method.
	ID string `json:"id"`
	// Name is the name of the method with the first letter in a lowercase
	// together with the namespace it belongs to. We need to keep the first letter
	// lowercased to match manifest standards.
	Name DebugMethodName `json:"name"`
	// IsExported defines whether the method is exported.
	IsExported bool `json:"-"`
	// IsFunction defines whether the method has no receiver.
	IsFunction bool `json:"-"`
	// Range is the range of smart-contract's opcodes corresponding to the method.
	Range DebugRange `json:"range"`
	// Parameters is a list of the method's parameters.
	Parameters []DebugParam `json:"params"`
	// ReturnType is the method's return type.
	ReturnType string `json:"return"`
	// ReturnTypeReal is the method's return type as specified in Go code.
	ReturnTypeReal binding.Override `json:"-"`
	// ReturnTypeExtended is the method's return type with additional data.
	ReturnTypeExtended *binding.ExtendedType `json:"-"`
	// ReturnTypeSC is a return type to use in manifest.
	ReturnTypeSC smartcontract.ParamType `json:"-"`
	Variables    []string                `json:"variables"`
	// SeqPoints is a map between source lines and byte-code instruction offsets.
	SeqPoints []DebugSeqPoint `json:"sequence-points"`
}

MethodDebugInfo represents smart-contract's method debug information.

func (*MethodDebugInfo) ToManifestMethod added in v0.90.0

func (m *MethodDebugInfo) ToManifestMethod() manifest.Method

ToManifestMethod converts MethodDebugInfo to manifest.Method.

type Options

type Options struct {
	// The extension of the output file default set to .nef
	Ext string

	// The name of the output file.
	Outfile string

	// The name of the output for debug info.
	DebugInfo string

	// The name of the output for contract manifest file.
	ManifestFile string

	// NoEventsCheck specifies if events emitted by contract needs to be present in manifest.
	// This setting has effect only if manifest is emitted.
	NoEventsCheck bool

	// NoStandardCheck specifies if supported standards compliance needs to be checked.
	// This setting has effect only if manifest is emitted.
	NoStandardCheck bool

	// NoPermissionsCheck specifies if permissions in YAML config need to be checked
	// against invocations performed by the contract.
	// This setting has effect only if manifest is emitted.
	NoPermissionsCheck bool

	// GuessEventTypes specifies if types of runtime notifications need to be guessed
	// from the usage context. These types are used for RPC binding generation only and
	// can be defined for events with name known at the compilation time and without
	// variadic args usages. If some type is specified via config file, then the config's
	// one is preferable. Currently, event's parameter type is defined from the first
	// occurrence of event call.
	GuessEventTypes bool

	// Name is a contract's name to be written to manifest.
	Name string

	// SourceURL is a contract's source URL to be written to manifest.
	SourceURL string

	// Runtime notifications declared in the contract configuration file.
	ContractEvents []HybridEvent

	// DeclaredNamedTypes is the set of named types that were declared in the
	// contract configuration type and are the part of manifest events.
	DeclaredNamedTypes map[string]binding.ExtendedType

	// The list of standards supported by the contract.
	ContractSupportedStandards []string

	// SafeMethods contains a list of methods which will be marked as safe in manifest.
	SafeMethods []string

	// Overloads contains mapping from the compiled method name to the name emitted in manifest.
	// It can be used to provide method overloads as Go doesn't have such capability.
	Overloads map[string]string

	// Permissions is a list of permissions for every contract method.
	Permissions []manifest.Permission

	// BindingsFile contains configuration for smart-contract bindings generator.
	BindingsFile string
}

Options contains all the parameters that affect the behavior of the compiler.

Jump to

Keyboard shortcuts

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