modifier

package
v0.0.0-...-e6aa6c5 Latest Latest
Warning

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

Go to latest
Published: Apr 10, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Overview

Package modifier implements modifier-based code generation for cff directives.

Index

Constants

View Source
const (
	// TaskName is the prefix for the name that replaces a cff.Task.
	TaskName = "_cffTask"
	// ResultsName is the prefix for the name that replaces a cff.Results.
	ResultsName = "_cffResults"
	// ParamsName is the prefix for the name that replaces a cff.Params.
	ParamsName = "_cffParams"
	// WithEmitterName is the prefix for the name that replaces a cff.WithEmitter.
	WithEmitterName = "_cffWithEmitter"
	// InstrumentFlowName is the prefix for the name that replaces a cff.InstrumentFlow.
	InstrumentFlowName = "_cffInstrumentFlow"
)
View Source
const (
	// TmplDir is the path pattern for modifier templates.
	TmplDir = "templates/*"
)

Variables

View Source
var ModifierTmplFS embed.FS

ModifierTmplFS embeds templates for generating modifier implementations.

Functions

func ExprHash

func ExprHash(fset *token.FileSet, n ast.Expr) string

ExprHash returns a unique identifier for an Expr.

func TrimFilename

func TrimFilename(path string) string

TrimFilename is a utility function used for trimming just the file name without ".go" suffix to guarantee uniqueness of generated cff functions.

Types

type Arg

type Arg struct {
	Name    string
	Type    types.Type
	LastIdx bool
}

Arg is a parameter of a Modifier function.

type GenParams

type GenParams struct {
	Writer  io.Writer
	FuncMap template.FuncMap
}

GenParams is the parameter for Modifiers to generate the body of their corresponding methods.

type Modifier

type Modifier interface {
	Expr() ast.Expr            // The ast Expr that produced this modifier.
	FuncExpr() string          // The name of the modifier-generated function.
	Provides() []ast.Expr      // The expressions that are provided to and returned by this modifier function.
	GenImpl(p GenParams) error // Generates the function body of the modifier-generated function.
}

Modifier changes the existing code by doing two things.

  1. It generates a function that corresponds to what the cff "API"s do by inspecting the arguments.
  2. It inline replaces the cff "API" call with calls to corrresponding generated function.

Each call to cff "API" is translates to a modifier.

func NewConcurrencyModifier

func NewConcurrencyModifier(fset *token.FileSet, n ast.Expr, concurrency ast.Expr) Modifier

NewConcurrencyModifier returns a Modifier that corresponds to a cff.Concurrency call.

func NewModifier

func NewModifier(p Params) Modifier

NewModifier creates a basic modifier from the arguments of a cff directive.

func Placeholder

func Placeholder(n ast.Expr) Modifier

Placeholder constructs a placeholder modifier that will be incrementally replaced by real modifier implementations. This should be removed when all modifiers are implemented.

type Params

type Params struct {
	// Name prefix that will replace the modified cff directive in generated
	// code.
	Name string
	// Modified is the ast.Expr that is replaced by the modifier.
	Modified ast.Expr
	// Provided are arguments of modified cff directive. These values are the
	// same as the values returned by the replacement modifier function.
	Provided []ast.Expr
	Fset     *token.FileSet
	Info     *types.Info
}

Params are the inputs to generating a modifier function.

Jump to

Keyboard shortcuts

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