compile

package
v0.1.20 Latest Latest
Warning

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

Go to latest
Published: Sep 25, 2021 License: BSD-3-Clause Imports: 14 Imported by: 38

Documentation

Overview

Package compile implements the VDL compiler, converting a parse tree into compiled results. The CompilePackage function is the main entry point.

Index

Constants

This section is empty.

Variables

View Source
var (
	// BuiltInPackage and BuiltInFile are used to hold the built-ins.
	BuiltInPackage = newPackage("", "_builtin", "_builtin", vdltool.Config{}, "")
	BuiltInFile    = &File{BaseName: "_builtin.vdl"}
)
View Source
var (
	// NilConst is defined by the compiler, as are TrueConst and FalseConst.
	NilConst   = vdl.ZeroValue(vdl.AnyType) // nil == any(nil)
	TrueConst  = vdl.BoolValue(nil, true)
	FalseConst = vdl.BoolValue(nil, false)
)

Functions

func CompileConfig

func CompileConfig(t *vdl.Type, pconfig *parse.Config, env *Env) *vdl.Value

CompileConfig compiles a parse.Config into a value. Returns the compiled value on success, or returns nil and guarantees !env.Errors.IsEmpty(). All imports that the parsed config depend on must already have been compiled and populated into env. If t is non-nil, the returned value will be of that type.

func CompileExpr

func CompileExpr(t *vdl.Type, expr parse.ConstExpr, env *Env) *vdl.Value

CompileExpr compiles expr into a value. Returns the compiled value on success, or returns nil and guarantees !env.Errors.IsEmpty(). All imports that expr depends on must already have been compiled and populated into env. If t is non-nil, the returned value will be of that type.

Types

type ConstDef

type ConstDef struct {
	NamePos             // name, parse position and docs
	Exported bool       // is this const definition exported?
	Value    *vdl.Value // const value
	File     *File      // parent file that this const is defined in
}

ConstDef represents a user-defined named const definition in the compiled results.

func (*ConstDef) String

func (x *ConstDef) String() string

type Env

type Env struct {
	Errors   *vdlutil.Errors
	Warnings *vdlutil.Errors
	// contains filtered or unexported fields
}

Env is the environment for compilation. It contains all errors that were collected during the execution - you can pass Errors to the parse phase to collect all errors together. As packages are compiled it also collects the output; after a sequence of dependent packages is compiled, all compiled output will be collected.

Always create a new Env via NewEnv; the zero Env is invalid.

func NewEnv

func NewEnv(maxErrors int) *Env

NewEnv creates a new Env, allowing up to maxErrors errors before we stop.

func NewEnvWithErrors

func NewEnvWithErrors(errs, warnings *vdlutil.Errors) *Env

NewEnvWithErrors creates a new Env, using the given errs to collect errors.

func (*Env) DisallowPathQualifiers

func (e *Env) DisallowPathQualifiers() *Env

DisallowPathQualifiers disables syntax like "a/b/c".Type.

func (*Env) Errorf

func (e *Env) Errorf(file *File, pos parse.Pos, format string, v ...interface{})

Errorf is a helper for error reporting, to consistently contain the file and position of the error when possible.

func (*Env) EvalConst

func (e *Env) EvalConst(name string, file *File) (opconst.Const, error)

EvalConst resolves and evaluates a name to a const.

func (*Env) FindConstDef

func (e *Env) FindConstDef(v *vdl.Value) *ConstDef

FindConstDef returns the const definition corresponding to v, or nil if v isn't a defined const. All user-defined named consts are considered defined; e.g. method tags don't have a corresponding const def.

func (*Env) FindTypeDef

func (e *Env) FindTypeDef(t *vdl.Type) *TypeDef

FindTypeDef returns the type definition corresponding to t, or nil if t isn't a defined type. All built-in and user-defined named types are considered defined; e.g. unnamed lists don't have a corresponding type def.

func (*Env) ResolveConst

func (e *Env) ResolveConst(name string, file *File) (cd *ConstDef, matched string)

ResolveConst resolves a name to a const definition. Returns the const def and the portion of name that was matched.

func (*Env) ResolveInterface

func (e *Env) ResolveInterface(name string, file *File) (i *Interface, matched string)

ResolveInterface resolves a name to an interface definition. Returns the interface and the portion of name that was matched.

func (*Env) ResolvePackage

func (e *Env) ResolvePackage(path string) *Package

ResolvePackage resolves a package path to its previous compiled results.

func (*Env) ResolvePackageGenPath

func (e *Env) ResolvePackageGenPath(genPath string) *Package

ResolvePackageGenPath resolves a package gen path to its previous compiled results.

func (*Env) ResolveType

func (e *Env) ResolveType(name string, file *File) (td *TypeDef, matched string)

ResolveType resolves a name to a type definition. Returns the type def and the portion of name that was matched.

func (*Env) Warningf added in v0.1.10

func (e *Env) Warningf(file *File, pos parse.Pos, format string, v ...interface{})

Warningf is like errorf but for warning messages that will not cause the compilation to fail.

type ErrorDef

type ErrorDef struct {
	NamePos                     // name, parse position and docs
	Exported  bool              // is this error definition exported?
	ID        string            // error ID
	RetryCode vdl.WireRetryCode // retry action to be performed by client
	Params    []*Field          // list of positional parameter names and types
	File      *File             // parent file that this error is defined in
}

ErrorDef represents a user-defined error definition in the compiled results.

func (*ErrorDef) String

func (x *ErrorDef) String() string

type Field

type Field struct {
	NamePos           // arg name, pos and doc
	Type    *vdl.Type // arg type, never nil
}

Field represents method arguments and error params.

func (*Field) String

func (x *Field) String() string

type File

type File struct {
	BaseName   string       // Base name of the vdl file, e.g. "foo.vdl"
	PackageDef NamePos      // Name, position and docs of the "package" clause
	TypeDefs   []*TypeDef   // Types defined in this file
	ConstDefs  []*ConstDef  // Consts defined in this file
	ErrorDefs  []*ErrorDef  // Errors defined in this file
	Interfaces []*Interface // Interfaces defined in this file
	Package    *Package     // Parent package

	TypeDeps    map[*vdl.Type]bool // Types the file depends on
	PackageDeps []*Package         // Packages the file depends on, sorted by path
	// contains filtered or unexported fields
}

File represents a compiled vdl file.

func (*File) DeclareIdent

func (f *File) DeclareIdent(ident, detail string) error

DeclareIdent declares ident with the given detail string. Returns an error if ident conflicts with an existing identifier in this file or package, where the error includes the the previous declaration detail.

func (*File) LookupImportPath

func (f *File) LookupImportPath(local string) string

LookupImportPath translates local into a package path name, based on the imports associated with the file. Returns the empty string "" if local couldn't be found; every valid package path is non-empty.

func (*File) String

func (f *File) String() string

func (*File) ValidateImportPackagePath

func (f *File) ValidateImportPackagePath(path string) bool

ValidateImportPackagePath returns true iff path is listed in the file's imports, and marks the import as used.

type Interface

type Interface struct {
	NamePos               // interface name, pos and doc
	Exported bool         // is this interface exported?
	Embeds   []*Interface // list of embedded interfaces
	Methods  []*Method    // list of methods
	File     *File        // parent file
}

Interface represents a set of embedded interfaces and methods.

func (*Interface) AllMethods

func (x *Interface) AllMethods() []*Method

func (*Interface) FindMethod

func (x *Interface) FindMethod(name string) *Method

func (*Interface) String

func (x *Interface) String() string

func (*Interface) TransitiveEmbeds

func (x *Interface) TransitiveEmbeds() []*Interface

type Method

type Method struct {
	NamePos                // method name, pos and doc
	InArgs    []*Field     // list of positional in-args
	OutArgs   []*Field     // list of positional out-args
	InStream  *vdl.Type    // in-stream type, may be nil
	OutStream *vdl.Type    // out-stream type, may be nil
	Tags      []*vdl.Value // list of method tags
	Interface *Interface   // parent interface
}

Method represents a method in an interface.

func (*Method) String

func (x *Method) String() string

type NamePos

type NamePos parse.NamePos

NamePos represents a name, its associated position and documentation.

func (*NamePos) String

func (x *NamePos) String() string

type Package

type Package struct {
	// Name is the name of the package, specified in the vdl files.
	// E.g. "bar"
	Name string
	// Path is the package path; the path used in VDL import clauses.
	// E.g. "foo/bar".
	Path string
	// GenPath is the package path to use for code generation.  It is typically
	// the same as Path, except for vdlroot standard packages.
	// E.g. "v.io/v23/vdlroot/time"
	GenPath string
	// Files holds the files contained in the package.
	Files []*File
	// FileDoc holds the top-level file documentation, which must be the same for
	// every file in the package.  This is typically used to hold boilerplate that
	// must appear in every generated file, e.g. a copyright notice.
	FileDoc string
	// Config holds the configuration for this package, specifying options used
	// during compilation and code generation.
	Config vdltool.Config

	// ConfigName is the name of the file from the vdl.config was read
	// since it may be configured as something other than 'vdl.config'.
	ConfigName string
	// contains filtered or unexported fields
}

Package represents a vdl package, containing a list of files.

func CompilePackage

func CompilePackage(pkgpath, genpath string, pfiles []*parse.File, config vdltool.Config, configName string, env *Env) *Package

CompilePackage compiles a list of parse.Files into a Package. Updates env with the compiled package and returns it on success, or returns nil and guarantees !env.Errors.IsEmpty(). All imports that the parsed package depend on must already have been compiled and populated into env.

func (*Package) ConstDefs

func (p *Package) ConstDefs() (x []*ConstDef)

ConstDefs returns all consts defined in this package, in dependency order. If const B refers to const A, B depends on A, and A will appear before B. Consts cannot have cyclic dependencies.

func (*Package) Doc

func (p *Package) Doc() string

Doc returns the package documentation, which should only exist in a single file in the package.

func (*Package) ErrorDefs

func (p *Package) ErrorDefs() (x []*ErrorDef)

ErrorDefs returns all errors defined in this package. Errors don't have dependencies.

func (*Package) Interfaces

func (p *Package) Interfaces() (x []*Interface)

Interfaces returns all interfaces defined in this package, in dependency order. If interface B embeds interface A, B depends on A, and A will appear before B. Interfaces cannot have cyclic dependencies.

func (*Package) QualifiedName

func (p *Package) QualifiedName(id string) string

QualifiedName returns the fully-qualified name of an identifier, by prepending the identifier with the package path.

func (*Package) ResolveConst

func (p *Package) ResolveConst(name string) *ConstDef

ResolveConst resolves the const name to its definition.

func (*Package) ResolveInterface

func (p *Package) ResolveInterface(name string) *Interface

ResolveInterface resolves the interface name to its definition.

func (*Package) ResolveType

func (p *Package) ResolveType(name string) *TypeDef

ResolveType resolves the type name to its definition.

func (*Package) String

func (p *Package) String() string

func (*Package) TypeDefs

func (p *Package) TypeDefs() []*TypeDef

TypeDefs returns all types defined in this package, in dependency order. If type B refers to type A, B depends on A, and A will appear before B. Types may have cyclic dependencies; the ordering of cyclicly dependent types is arbitrary.

type TypeDef

type TypeDef struct {
	NamePos            // name, parse position and docs
	Exported bool      // is this type definition exported?
	Type     *vdl.Type // type of this type definition

	// BaseType is the type that Type is based on.  The BaseType may be named or
	// unnamed.  E.g.
	//                                 BaseType
	//   type Bool    bool;            bool
	//   type Bool2   Bool;            Bool
	//   type List    []int32;         []int32
	//   type List2   List;            List
	//   type Struct  struct{A bool};  struct{A bool}
	//   type Struct2 Struct;          Struct
	BaseType *vdl.Type

	LabelDoc       []string // [valid for enum] docs for each label
	LabelDocSuffix []string // [valid for enum] suffix docs for each label
	FieldDoc       []string // [valid for struct, union] docs for each field
	FieldDocSuffix []string // [valid for struct, union] suffix docs for each field
	File           *File    // parent file that this type is defined in
}

TypeDef represents a user-defined named type definition in the compiled results.

func (*TypeDef) String

func (x *TypeDef) String() string

Jump to

Keyboard shortcuts

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