lower

package
v0.146.0 Latest Latest
Warning

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

Go to latest
Published: Aug 21, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Overview

Package lower computes and applies the text edits that turn Go+ source into Go. Edits operate on original source bytes — the AST and type information decide where to edit, but bytes are the medium, so untouched regions (comments, formatting) pass through losslessly.

Index

Constants

View Source
const (
	// BareCarrierPrefix marks a pipeline segment awaiting member-vs-
	// function resolution: __gp_bare_Map(head, args…).
	BareCarrierPrefix = "__gp_bare_"
	// SegCarrierPrefix marks a direct-callee segment awaiting the flowing
	// type: __gp_seg1(head, callee, fixedArgs…) — the digit is the piped
	// value's insertion index among the final args.
	SegCarrierPrefix = "__gp_seg"
	// DotCarrier wraps a dot-segment's receiver: __gp_dot(head).Suffix…
	DotCarrier = "__gp_dot"
	// ComposeCarrier marks a one-track composition: __gp_comp(f, g, …).
	ComposeCarrier = "__gp_comp"
	// KleisliCarrierPrefix marks a mixed/railway composition with per-link
	// kinds encoded as letters: __gp_kcomp_kc(f, g, h).
	KleisliCarrierPrefix = "__gp_kcomp_"
)
View Source
const PatternCarrier = "//goplus:pattern "

PatternCarrier is the comment prefix threading pattern structure through the resolution fixpoint. The resolver deletes each carrier as it expands the arm — the fixpoint's progress signal for matches.

View Source
const TailPrefix = "//goplus:tail"

TailPrefix marks a function declared with the Go+ `tail` modifier.

View Source
const ValCarrierPrefix = "__gp_val"

ValCarrierPrefix defers an expression-form temp's type to resolution: `__gp_v0 := __gp_val0()`.

Variables

This section is empty.

Functions

func Apply

func Apply(src []byte, edits []Edit) ([]byte, error)

Apply applies edits to src. Edits must be non-overlapping; they may be given in any order. Insertions at the same offset are applied in the order given.

Types

type Edit

type Edit struct {
	Start, End int
	New        string
}

Edit replaces src[Start:End] with New. A zero-width edit (Start == End) is an insertion.

func ClassEdits

func ClassEdits(f *syntax.File, c *syntax.ClassDecl) []Edit

ClassEdits lowers one class declaration.

func Decl

func Decl(f *syntax.File, gm *syntax.GenericMethod, funcName string, recvTParams []TParam) []Edit

Decl computes the edits lowering one generic method declaration to a package-level function declaration named funcName:

func (s Stack[T]) Map[U any](f func(T) U) Stack[U] { … }
→ func StackMap[T any, U any](s Stack[T], f func(T) U) Stack[U] { … }

The body is untouched: the receiver keeps its name, now as the first parameter.

func DelegateEdits

func DelegateEdits(f *syntax.File, d *syntax.DelegateField) []Edit

DelegateEdits lowers one delegate field.

func EnumEdits

func EnumEdits(f *syntax.File, e *syntax.EnumDecl, spec *EnumSpec) []Edit

EnumEdits lowers one enum declaration: a marker line above the decl and a single replacement of the `type X enum { … }` span with the sealed interface, variant structs, and marker methods.

func InstanceEdits

func InstanceEdits(f *syntax.File, d *syntax.InstanceDecl) []Edit

InstanceEdits lowers one instance declaration.

func MarkerInsert

func MarkerInsert(f *syntax.File, gm *syntax.GenericMethod, marker string) Edit

MarkerInsert computes the edit inserting a marker comment line directly above a method's declaration (above its doc comment, if any).

func QuantityEdits

func QuantityEdits(f *syntax.File, q *syntax.QuantityParam) []Edit

QuantityEdits strips one parameter's quantity prefix.

func RefinementEdits added in v0.22.0

func RefinementEdits(f *syntax.File, d *syntax.RefinementDecl) []Edit

RefinementEdits erases a refinement declaration to a Go alias while retaining its proof contract in a machine-readable marker.

type EnumSpec

type EnumSpec struct {
	Name          string // enum (interface) type name
	TParamsSrc    string // full tparam list with constraints, e.g. "T any"; ""
	TParamNames   []string
	MarkerName    string // sealed marker method, e.g. "isOption"
	EnumMarker    string // "//goplus:enum Option[T any]"
	Variants      []EnumVariantSpec
	FoldText      string // derived Cases struct + Fold function (v0.6.0); "" = none
	TraversalText string // derived Children/Universe/Transform (v0.11.0); "" = none
	EqualText     string // derived Equal/EqualWith/EqOverrides (v0.11.0); "" = none
	ViewName      string // erased GADT view helper; empty when every case head is spellable
	ViewMethod    string // sealed per-variant payload method
	Transparent   bool   // concrete alias representation for one monomorphic variant
	BoxPointer    bool   // //goplus:box pointer — markers on pointer receivers
}

EnumSpec is a fully-resolved enum ready to render: names assigned, GADT result types analyzed, field types substituted. Computed by gen.

type EnumVariantSpec

type EnumVariantSpec struct {
	GoplusName  string   // constructor name as written, e.g. "Some"
	Doc         string   // variant doc comment, newline-terminated lines; "" = none
	TypeName    string   // lowered struct name
	TParamsSrc  string   // kept tparams with constraints; "" for ground variants
	TParamNames []string // kept tparam names
	MarkerArgs  []string // sealed-method parameter types (result type args)
	Fields      []FieldSpec
	ParamNames  []string // original parameter names, aligned with Fields
	Marker      string   // "//goplus:variant (Option[T]) Some(value T)"
	ViewTag     int      // stable declaration-order discriminator
}

EnumVariantSpec is one variant ready to render.

type FieldSpec

type FieldSpec struct {
	Name string
	Type string
}

FieldSpec is one struct field of a variant.

type Hoister

type Hoister struct {
	// contains filtered or unexported fields
}

Hoister renders a file's pass-1 flow and expression-form lowerings.

func NewHoister

func NewHoister(f *syntax.File, matchStmtCount int) *Hoister

NewHoister prepares pass-1 rendering; matchStmtCount seeds skeleton numbering past the statement matches.

func (*Hoister) FileEdits

func (h *Hoister) FileEdits() ([]Edit, []diag.Diagnostic)

FileEdits lowers all match statements and outermost extension expressions, returning the pass-1 edits.

type TParam

type TParam struct {
	Name       string
	Constraint string
}

TParam is a resolved receiver type parameter: its name as written on the receiver and its constraint (rendered in receiver-name terms).

type TailError added in v0.21.0

type TailError struct {
	Pos token.Position
	Msg string
}

TailError describes an invalid use of the recur tail-call intrinsic.

func LowerTailCalls added in v0.21.0

func LowerTailCalls(filename string, src []byte) ([]byte, []TailError)

LowerTailCalls lowers explicit recur(nextArgs...) tail statements to a labelled loop. It runs after the other Go+ source rewrites, so recur works uniformly inside authored Go control flow and lowered constructs such as match.

Jump to

Keyboard shortcuts

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