ghc-9.4.0.20220623: The GHC API
Copyright(c) The University of Glasgow 1992-2006
Safe HaskellSafe-Inferred
LanguageHaskell2010

GHC.Hs.Utils

Description

Here we collect a variety of helper functions that construct or analyse HsSyn. All these functions deal with generic HsSyn; functions which deal with the instantiated versions are located elsewhere:

Parameterised by Module ---------------- ------------- GhcPs/RdrName GHC.Parser.PostProcess GhcRn/Name GHC.Rename.* GhcTc/Id GHC.Tc.Utils.Zonk

The mk* functions attempt to construct a not-completely-useless SrcSpan from their components, compared with the nl* functions which just attach noSrcSpan to everything.

Synopsis

Terms

mkHsPar :: LHsExpr (GhcPass id) -> LHsExpr (GhcPass id) Source #

e => (e)

mkHsCaseAlt :: (Anno (GRHS (GhcPass p) (LocatedA (body (GhcPass p)))) ~ SrcAnn NoEpAnns, Anno (Match (GhcPass p) (LocatedA (body (GhcPass p)))) ~ SrcSpanAnnA) => LPat (GhcPass p) -> LocatedA (body (GhcPass p)) -> LMatch (GhcPass p) (LocatedA (body (GhcPass p))) Source #

A simple case alternative with a single pattern, no binds, no guards; pre-typechecking

mkMatchGroup :: AnnoBody p body => Origin -> LocatedL [LocatedA (Match (GhcPass p) (LocatedA (body (GhcPass p))))] -> MatchGroup (GhcPass p) (LocatedA (body (GhcPass p))) Source #

mkLamCaseMatchGroup :: AnnoBody p body => Origin -> LamCaseVariant -> LocatedL [LocatedA (Match (GhcPass p) (LocatedA (body (GhcPass p))))] -> MatchGroup (GhcPass p) (LocatedA (body (GhcPass p))) Source #

mkPrefixFunRhs :: LIdP p -> HsMatchContext p Source #

Make a prefix, non-strict function HsMatchContext

mkHsOpApp :: LHsExpr GhcPs -> IdP GhcPs -> LHsExpr GhcPs -> HsExpr GhcPs Source #

A useful function for building OpApps. The operator is always a variable, and we don't know the fixity yet.

mkLHsPar :: IsPass id => LHsExpr (GhcPass id) -> LHsExpr (GhcPass id) Source #

Wrap in parens if hsExprNeedsParens appPrec says it needs them So f x becomes (f x), but 3 stays as 3.

Constructing general big tuples

GHCs built in tuples can only go up to mAX_TUPLE_SIZE in arity, but we might conceivably want to build such a massive tuple as part of the output of a desugaring stage (notably that for list comprehensions).

We call tuples above this size "big tuples", and emulate them by creating and pattern matching on >nested< tuples that are expressible by GHC.

Nesting policy: it's better to have a 2-tuple of 10-tuples (3 objects) than a 10-tuple of 2-tuples (11 objects), so we want the leaves of any construction to be big.

If you just use the mkBigCoreTup, mkBigCoreVarTupTy, mkTupleSelector and mkTupleCase functions to do all your work with tuples you should be fine, and not have to worry about the arity limitation at all.

mkChunkified Source #

Arguments

:: ([a] -> a)

"Small" constructor function, of maximum input arity mAX_TUPLE_SIZE

-> [a]

Possible "big" list of things to construct from

-> a

Constructed thing made possible by recursive decomposition

Lifts a "small" constructor into a "big" constructor by recursive decomposition

chunkify :: [a] -> [[a]] Source #

Split a list into lists that are small enough to have a corresponding tuple arity. The sub-lists of the result all have length <= mAX_TUPLE_SIZE But there may be more than mAX_TUPLE_SIZE sub-lists

Bindings

mkFunBind :: Origin -> LocatedN RdrName -> [LMatch GhcPs (LHsExpr GhcPs)] -> HsBind GhcPs Source #

Not infix, with place holders for coercion and free vars

mkSimpleGeneratedFunBind :: SrcSpan -> RdrName -> [LPat GhcPs] -> LHsExpr GhcPs -> LHsBind GhcPs Source #

Convenience function using mkFunBind. This is for generated bindings only, do not use for user-written code.

mkTopFunBind :: Origin -> LocatedN Name -> [LMatch GhcRn (LHsExpr GhcRn)] -> HsBind GhcRn Source #

In Name-land, with empty bind_fvs

isInfixFunBind :: forall id1 id2. UnXRec id2 => HsBindLR id1 id2 -> Bool Source #

If any of the matches in the FunBind are infix, the FunBind is considered infix.

spanHsLocaLBinds :: HsLocalBinds (GhcPass p) -> SrcSpan Source #

Return the SrcSpan encompassing the contents of any enclosed binds

Literals

Patterns

nlWildPat :: LPat GhcPs Source #

Wildcard pattern - after parsing

nlWildPatName :: LPat GhcRn Source #

Wildcard pattern - after renaming

nlParPat :: LPat (GhcPass name) -> LPat (GhcPass name) Source #

mkBigLHsVarTup :: IsSrcSpanAnn p a => [IdP (GhcPass p)] -> XExplicitTuple (GhcPass p) -> LHsExpr (GhcPass p) Source #

The Big equivalents for the source tuple expressions

mkBigLHsVarPatTup :: [IdP GhcRn] -> LPat GhcRn Source #

The Big equivalents for the source tuple patterns

Types

mkClassOpSigs :: [LSig GhcPs] -> [LSig GhcPs] Source #

Convert TypeSig to ClassOpSig. The former is what is parsed, but the latter is what we need in class/instance declarations

mkHsSigEnv :: forall a. (LSig GhcRn -> Maybe ([LocatedN Name], a)) -> [LSig GhcRn] -> NameEnv a Source #

Stmts

mkLastStmt :: IsPass idR => LocatedA (bodyR (GhcPass idR)) -> StmtLR (GhcPass idL) (GhcPass idR) (LocatedA (bodyR (GhcPass idR))) Source #

Template Haskell

Collecting binders

isUnliftedHsBind :: HsBind GhcTc -> Bool Source #

Should we treat this as an unlifted bind? This will be true for any bind that binds an unlifted variable, but we must be careful around AbsBinds. See Note [Unlifted id check in isUnliftedHsBind]. For usage information, see Note [Strict binds checks] is GHC.HsToCore.Binds.

isBangedHsBind :: HsBind GhcTc -> Bool Source #

Is a binding a strict variable or pattern bind (e.g. !x = ...)?

collectHsBindListBinders :: forall p idR. CollectPass p => CollectFlag p -> [LHsBindLR p idR] -> [IdP p] Source #

Same as collectHsBindsBinders, but works over a list of bindings

collectHsIdBinders :: CollectPass (GhcPass idL) => CollectFlag (GhcPass idL) -> HsValBindsLR (GhcPass idL) (GhcPass idR) -> [IdP (GhcPass idL)] Source #

Collect Id binders only, or Ids + pattern synonyms, respectively

collectHsBindBinders :: CollectPass p => CollectFlag p -> HsBindLR p idR -> [IdP p] Source #

Collect both Ids and pattern-synonym binders

collectMethodBinders :: forall idL idR. UnXRec idL => LHsBindsLR idL idR -> [LIdP idL] Source #

Used exclusively for the bindings of an instance decl which are all FunBinds

collectStmtsBinders :: CollectPass (GhcPass idL) => CollectFlag (GhcPass idL) -> [StmtLR (GhcPass idL) (GhcPass idR) body] -> [IdP (GhcPass idL)] Source #

class UnXRec p => CollectPass p where Source #

This class specifies how to collect variable identifiers from extension patterns in the given pass. Consumers of the GHC API that define their own passes should feel free to implement instances in order to make use of functions which depend on it.

In particular, Haddock already makes use of this, with an instance for its DocNameI pass so that it can reuse the code in GHC for collecting binders.

Methods

collectXXPat :: CollectFlag p -> XXPat p -> [IdP p] -> [IdP p] Source #

collectXXHsBindsLR :: forall pR. XXHsBindsLR p pR -> [IdP p] -> [IdP p] Source #

Instances

Instances details
IsPass p => CollectPass (GhcPass p) Source # 
Instance details

Defined in GHC.Hs.Utils

data CollectFlag p where Source #

Indicate if evidence binders have to be collected.

This type is used as a boolean (should we collect evidence binders or not?) but also to pass an evidence that the AST has been typechecked when we do want to collect evidence binders, otherwise these binders are not available.

See Note [Dictionary binders in ConPatOut]

Constructors

CollNoDictBinders :: CollectFlag p

Don't collect evidence binders

CollWithDictBinders :: CollectFlag GhcTc

Collect evidence binders

hsLTyClDeclBinders :: IsPass p => LocatedA (TyClDecl (GhcPass p)) -> ([LocatedA (IdP (GhcPass p))], [LFieldOcc (GhcPass p)]) Source #

Returns all the binding names of the decl. The first one is guaranteed to be the name of the decl. The first component represents all binding names except record fields; the second represents field occurrences. For record fields mentioned in multiple constructors, the SrcLoc will be from the first occurrence.

Each returned (Located name) has a SrcSpan for the whole declaration. See Note [SrcSpan for binders]

hsPatSynSelectors :: IsPass p => HsValBinds (GhcPass p) -> [FieldOcc (GhcPass p)] Source #

Collects record pattern-synonym selectors only; the pattern synonym names are collected by collectHsValBinders.

getPatSynBinds :: forall id. UnXRec id => [(RecFlag, LHsBinds id)] -> [PatSynBind id id] Source #

hsForeignDeclsBinders :: forall p a. (UnXRec (GhcPass p), IsSrcSpanAnn p a) => [LForeignDecl (GhcPass p)] -> [LIdP (GhcPass p)] Source #

See Note [SrcSpan for binders]

hsDataFamInstBinders :: IsPass p => DataFamInstDecl (GhcPass p) -> ([LocatedA (IdP (GhcPass p))], [LFieldOcc (GhcPass p)]) Source #

the SrcLoc returned are for the whole declarations, not just the names

Collecting implicit binders

lStmtsImplicits :: [LStmtLR GhcRn (GhcPass idR) (LocatedA (body (GhcPass idR)))] -> [(SrcSpan, [Name])] Source #