util

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 24, 2024 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Overview

Package util implements utility functions for AST and types.

Index

Constants

This section is empty.

Variables

View Source
var BoolType = types.Universe.Lookup("bool").Type()

BoolType is the type of the builtin "bool" interface.

View Source
var BuiltinLen = types.Universe.Lookup("len")

BuiltinLen is the builtin "len" function object.

View Source
var ErrorType = types.Universe.Lookup("error").Type()

ErrorType is the type of the builtin "error" interface.

Functions

func ExprBarsNilness

func ExprBarsNilness(pass *analysis.Pass, expr ast.Expr) bool

ExprBarsNilness returns if the expression can never be nil for the simple reason that nil does not inhabit its type.

func ExprIsAuthentic

func ExprIsAuthentic(pass *analysis.Pass, expr ast.Expr) bool

ExprIsAuthentic aims to return true iff the passed expression is an AST node found in the source program of this pass - not one that we created as an intermediate value. There is no fully sound way to do this - but returning whether it is present in the `Types` map map is a good approximation. Right now, this is used only to decide whether to print the location of the producer expression in a full trigger.

func FuncIdentFromCallExpr

func FuncIdentFromCallExpr(expr *ast.CallExpr) *ast.Ident

FuncIdentFromCallExpr return a function identified from a call expression, nil otherwise nilable(result 0)

func FuncIsErrReturning

func FuncIsErrReturning(fdecl *types.Func) bool

FuncIsErrReturning encodes the conditions that a function is deemed "error-returning". This guards its results to require an `err` check before use as nonnil. A function is deemed "error-returning" iff it has a single result of type `error`, and that result is the last in the list of results.

func FuncIsOkReturning

func FuncIsOkReturning(fdecl *types.Func) bool

FuncIsOkReturning encodes the conditions that a function is deemed "ok-returning". This guards its results to require an `ok` check before use as nonnil. A function is deemed "ok-returning" iff it has a single result of type `bool`, and that result is the last in the list of results.

func FuncNumResults

func FuncNumResults(decl *types.Func) int

FuncNumResults looks at a function declaration and returns the number of results of that function

func GetFieldVal

func GetFieldVal(compElts []ast.Expr, fieldName string, numFields int, index int) ast.Expr

GetFieldVal returns the assigned value for the field at index. compElts holds the elements of the composite literal expression for struct initialization

func GetFunctionParamNode

func GetFunctionParamNode(funcDecl *ast.FuncDecl, searchParam *types.Var) ast.Expr

GetFunctionParamNode returns the ast param node matching the variable searchParam

func GetParamObjFromIndex

func GetParamObjFromIndex(functionType *types.Func, argIdx int) *types.Var

GetParamObjFromIndex get the variable corresponding to the parameter from the function functionType

func GetSelectorExprHeadIdent

func GetSelectorExprHeadIdent(selExpr *ast.SelectorExpr) *ast.Ident

GetSelectorExprHeadIdent gets the head of the chained selector expression if it is an ident. Returns nil otherwise

func IsEmptyExpr

func IsEmptyExpr(expr ast.Expr) bool

IsEmptyExpr checks if an expression is the empty identifier

func IsFieldSelectorChain

func IsFieldSelectorChain(expr ast.Expr) bool

IsFieldSelectorChain returns true if the expr is chain of idents. e.g, x.y.z It returns for false for expressions such as x.y().z

func IsLiteral

func IsLiteral(expr ast.Expr, literals ...string) bool

IsLiteral returns true if `expr` is a literal that matches with one of the given literal values (e.g., "nil", "true", "false)

func IsSliceAppendCall

func IsSliceAppendCall(node *ast.CallExpr, pass *analysis.Pass) (*types.Slice, bool)

IsSliceAppendCall checks if `node` represents the builtin append(slice []Type, elems ...Type) []Type call on a slice. The function checks 2 things, 1) Name of the called function is "builtin append" 2) The first argument to the function is a slice

func PartiallyQualifiedFuncName

func PartiallyQualifiedFuncName(f *types.Func) string

PartiallyQualifiedFuncName returns the name of the passed function, with the name of its receiver if defined

func PortionAfterSep

func PortionAfterSep(input, sep string, occ int) string

PortionAfterSep returns the suffix of the passed string `input` containing at most `occ` occurrences of the separator `sep`

func PosToLocation

func PosToLocation(pos token.Pos, pass *analysis.Pass) token.Position

PosToLocation converts a token.Pos as a real code location, of token.Position.

func PrettyPrintErrorMessage

func PrettyPrintErrorMessage(msg string) string

PrettyPrintErrorMessage is used in error reporting to post process and pretty print the output with colors

func StripParens

func StripParens(expr ast.Node) ast.Node

StripParens takes an ast node and strips it of any outmost parentheses

func TruncatePosition

func TruncatePosition(position token.Position) token.Position

TruncatePosition truncates the prefix of the filename to keep it at the given depth (config.DirLevelsToPrintForTriggers)

func TypeAsDeepType

func TypeAsDeepType(t types.Type) (types.Type, bool)

TypeAsDeepType checks if a type is an expression that directly admits a deep nilability annotation, returning true as its boolean param if so, along with the element type as its `types.Type` param nilable(result 0)

func TypeAsDeeplyStruct

func TypeAsDeeplyStruct(typ types.Type) *types.Struct

TypeAsDeeplyStruct returns underlying struct type if the type is struct type or a pointer to a struct type returns nil otherwise

func TypeBarsNilness

func TypeBarsNilness(t types.Type) bool

TypeBarsNilness returns false iff the type `t` is inhabited by nil.

func TypeIsDeep

func TypeIsDeep(t types.Type) bool

TypeIsDeep checks if a type is an expression that directly admits a deep nilability annotation - deep nilability annotations on all other types are ignored

func TypeIsDeeplyArray

func TypeIsDeeplyArray(t types.Type) bool

TypeIsDeeplyArray returns true if `t` is of array type, including transitively through Named types

func TypeIsDeeplyChan

func TypeIsDeeplyChan(t types.Type) bool

TypeIsDeeplyChan returns true if `t` is of channel type, including transitively through Named types

func TypeIsDeeplyInterface

func TypeIsDeeplyInterface(t types.Type) bool

TypeIsDeeplyInterface returns true if `t` is of struct type, including transitively through Named types

func TypeIsDeeplyMap

func TypeIsDeeplyMap(t types.Type) bool

TypeIsDeeplyMap returns true if `t` is of map type, including transitively through Named types

func TypeIsDeeplyPtr

func TypeIsDeeplyPtr(t types.Type) bool

TypeIsDeeplyPtr returns true if `t` is of pointer type, including transitively through Named types

func TypeIsDeeplySlice

func TypeIsDeeplySlice(t types.Type) bool

TypeIsDeeplySlice returns true if `t` is of slice type, including transitively through Named types

func TypeIsSlice

func TypeIsSlice(t types.Type) bool

TypeIsSlice returns true if `t` is of slice type

func TypeOf

func TypeOf(pass *analysis.Pass, expr ast.Expr) types.Type

TypeOf returns the type of the passed AST expression

func UnwrapPtr

func UnwrapPtr(t types.Type) types.Type

UnwrapPtr unwraps a pointer type and returns the element type. For all other types it returns the type unmodified.

Types

type ExprNonceMap

type ExprNonceMap = map[ast.Expr]GuardNonce

An ExprNonceMap maps AST nodes to nonces, to establish their canonical interpretation.

type GuardNonce

type GuardNonce int

A GuardNonce is a unique token used to identify contracts that arise through the RichCheckEffect mechanism. GuardNonces are canonically tied to an AST node through the ExprNonceMap accumulated in their generator.

func (GuardNonce) Eq

func (g GuardNonce) Eq(g2 GuardNonce) bool

Eq compares two GuardNonces for equality

type GuardNonceGenerator

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

A GuardNonceGenerator is a stateful object used to ensure unique obtainment of nonces. It also keeps track of the expression with which they are associate, in a map of type ExprNonceMap to be later embedded into a RootAssertionNode nonnil(exprNonceMap)

func NewGuardNonceGenerator

func NewGuardNonceGenerator() *GuardNonceGenerator

NewGuardNonceGenerator returns a fresh instance of GuardNonceGenerator that can be subsequently used to identify and track RichCheckEffects.

func (*GuardNonceGenerator) GetExprNonceMap

func (g *GuardNonceGenerator) GetExprNonceMap() ExprNonceMap

GetExprNonceMap for a GuardNonceGenerator returns the underlying ExprNonceMap

func (*GuardNonceGenerator) Next

func (g *GuardNonceGenerator) Next(expr ast.Expr) GuardNonce

Next for a GuardNonceGenerator returns the first nonce that has not already been output. The method also takes an AST expression, to be tied to this nonce as its interpretation.

type GuardNonceSet

type GuardNonceSet map[GuardNonce]bool

GuardNonceSet is a set of GuardNonces

func NoGuards

func NoGuards() GuardNonceSet

NoGuards returns an empty GuardNonceSet - to be used to indicate no guards

func (GuardNonceSet) Add

func (g GuardNonceSet) Add(guards ...GuardNonce) GuardNonceSet

Add statefully adds one or more new GuardNonces to a GuardNonceSet nonnil(result 0)

func (GuardNonceSet) Contains

func (g GuardNonceSet) Contains(n GuardNonce) bool

Contains returns true iff a given GuardNonceSet contains a passed GuardNonce

func (GuardNonceSet) Copy

func (g GuardNonceSet) Copy() GuardNonceSet

Copy returns a copy of the passed GuardNonceSet without modifying the original nonnil(result 0)

func (GuardNonceSet) Eq

func (g GuardNonceSet) Eq(other GuardNonceSet) bool

Eq returns true iff a given GuardNonceSet contains the same elements as a passed GuardNonceSet nonnil(other)

func (GuardNonceSet) Intersection

func (g GuardNonceSet) Intersection(others ...GuardNonceSet) GuardNonceSet

Intersection returns a new GuardNonceSet that is the intersection of its two parameters without modifying either nonnil(others)

func (GuardNonceSet) IsEmpty

func (g GuardNonceSet) IsEmpty() bool

IsEmpty returns true if a given GuardNonceSet is empty

func (GuardNonceSet) Remove

func (g GuardNonceSet) Remove(guards ...GuardNonce) GuardNonceSet

Remove statefully removes one or more GuardNonces from a GuardNonceSet nonnil(result 0)

func (GuardNonceSet) SubsetOf

func (g GuardNonceSet) SubsetOf(other GuardNonceSet) bool

SubsetOf returns true iff a given GuardNonceSet is a subset of a passed GuardNonceSet nonnil(other)

func (GuardNonceSet) Union

func (g GuardNonceSet) Union(others ...GuardNonceSet) GuardNonceSet

Union returns a new GuardNonceSet that is the union of its two parameters without modifying either nonnil(result 0)

Directories

Path Synopsis
Package analysishelper provides helper functions for the `go/analysis` package.
Package analysishelper provides helper functions for the `go/analysis` package.
Package asthelper implements utility functions for AST.
Package asthelper implements utility functions for AST.
Package orderedmap implements a generic ordered map that supports iteration in insertion order.
Package orderedmap implements a generic ordered map that supports iteration in insertion order.

Jump to

Keyboard shortcuts

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