summaryrefslogtreecommitdiff
path: root/compiler/GHC/Hs/Utils.hs
diff options
context:
space:
mode:
authorAlan Zimmerman <alan.zimm@gmail.com>2021-02-21 21:23:40 +0000
committerMarge Bot <ben+marge-bot@smart-cactus.org>2021-03-20 07:48:38 -0400
commit95275a5f25a2e70b71240d4756109180486af1b1 (patch)
treeeb4801bb0e00098b8b9d513479de4fbbd779ddac /compiler/GHC/Hs/Utils.hs
parentf940fd466a86c2f8e93237b36835797be3f3c898 (diff)
downloadhaskell-95275a5f25a2e70b71240d4756109180486af1b1.tar.gz
GHC Exactprint main commit
Metric Increase: T10370 parsing001 Updates haddock submodule
Diffstat (limited to 'compiler/GHC/Hs/Utils.hs')
-rw-r--r--compiler/GHC/Hs/Utils.hs499
1 files changed, 293 insertions, 206 deletions
diff --git a/compiler/GHC/Hs/Utils.hs b/compiler/GHC/Hs/Utils.hs
index 2745a5944e..7e298b8978 100644
--- a/compiler/GHC/Hs/Utils.hs
+++ b/compiler/GHC/Hs/Utils.hs
@@ -1,3 +1,4 @@
+{-# LANGUAGE ConstraintKinds #-}
{-|
Module : GHC.Hs.Utils
Description : Generic helpers for the HsSyn type.
@@ -41,7 +42,7 @@ module GHC.Hs.Utils(
mkMatchGroup, mkMatch, mkPrefixFunRhs, mkHsLam, mkHsIf,
mkHsWrap, mkLHsWrap, mkHsWrapCo, mkHsWrapCoR, mkLHsWrapCo,
mkHsDictLet, mkHsLams,
- mkHsOpApp, mkHsDo, mkHsComp, mkHsWrapPat, mkHsWrapPatCo,
+ mkHsOpApp, mkHsDo, mkHsDoAnns, mkHsComp, mkHsCompAnns, mkHsWrapPat, mkHsWrapPatCo,
mkLHsPar, mkHsCmdWrap, mkLHsCmdWrap,
mkHsCmdIf,
@@ -50,6 +51,7 @@ module GHC.Hs.Utils(
nlHsIntLit, nlHsVarApps,
nlHsDo, nlHsOpApp, nlHsLam, nlHsPar, nlHsIf, nlHsCase, nlList,
mkLHsTupleExpr, mkLHsVarTuple, missingTupArg,
+ mkLocatedList,
-- * Constructing general big tuples
-- $big_tuples
@@ -59,6 +61,7 @@ module GHC.Hs.Utils(
mkFunBind, mkVarBind, mkHsVarBind, mkSimpleGeneratedFunBind, mkTopFunBind,
mkPatSynBind,
isInfixFunBind,
+ spanHsLocaLBinds,
-- * Literals
mkHsIntegral, mkHsFractional, mkHsIsString, mkHsString, mkHsStringPrimLit,
@@ -82,6 +85,7 @@ module GHC.Hs.Utils(
emptyTransStmt, mkGroupUsingStmt, mkGroupByUsingStmt,
emptyRecStmt, emptyRecStmtName, emptyRecStmtId, mkRecStmt,
unitRecStmtTc,
+ mkLetStmt,
-- * Template Haskell
mkUntypedSplice, mkTypedSplice,
@@ -119,6 +123,7 @@ import GHC.Hs.Type
import GHC.Hs.Lit
import Language.Haskell.Syntax.Extension
import GHC.Hs.Extension
+import GHC.Parser.Annotation
import GHC.Tc.Types.Evidence
import GHC.Core.TyCo.Rep
@@ -140,7 +145,6 @@ import GHC.Types.SourceText
import GHC.Data.FastString
import GHC.Data.Bag
import GHC.Settings.Constants
-import GHC.Parser.Annotation
import GHC.Utils.Misc
import GHC.Utils.Outputable
@@ -150,6 +154,7 @@ import Data.Either
import Data.Function
import Data.List ( partition, deleteBy )
import Data.Proxy
+import Data.Data (Data)
{-
************************************************************************
@@ -165,53 +170,68 @@ just attach 'noSrcSpan' to everything.
-- | @e => (e)@
mkHsPar :: LHsExpr (GhcPass id) -> LHsExpr (GhcPass id)
-mkHsPar e = L (getLoc e) (HsPar noExtField e)
-
-mkSimpleMatch :: HsMatchContext (NoGhcTc (GhcPass p))
- -> [LPat (GhcPass p)] -> Located (body (GhcPass p))
- -> LMatch (GhcPass p) (Located (body (GhcPass p)))
+mkHsPar e = L (getLoc e) (HsPar noAnn e)
+
+mkSimpleMatch :: (Anno (Match (GhcPass p) (LocatedA (body (GhcPass p))))
+ ~ SrcSpanAnnA,
+ Anno (GRHS (GhcPass p) (LocatedA (body (GhcPass p))))
+ ~ SrcSpan)
+ => HsMatchContext (NoGhcTc (GhcPass p))
+ -> [LPat (GhcPass p)] -> LocatedA (body (GhcPass p))
+ -> LMatch (GhcPass p) (LocatedA (body (GhcPass p)))
mkSimpleMatch ctxt pats rhs
= L loc $
- Match { m_ext = noExtField, m_ctxt = ctxt, m_pats = pats
- , m_grhss = unguardedGRHSs rhs }
+ Match { m_ext = noAnn, m_ctxt = ctxt, m_pats = pats
+ , m_grhss = unguardedGRHSs (locA loc) rhs noAnn }
where
loc = case pats of
[] -> getLoc rhs
- (pat:_) -> combineSrcSpans (getLoc pat) (getLoc rhs)
-
-unguardedGRHSs :: Located (body (GhcPass p))
- -> GRHSs (GhcPass p) (Located (body (GhcPass p)))
-unguardedGRHSs rhs@(L loc _)
- = GRHSs noExtField (unguardedRHS loc rhs) (noLoc emptyLocalBinds)
-
-unguardedRHS :: SrcSpan -> Located (body (GhcPass p))
- -> [LGRHS (GhcPass p) (Located (body (GhcPass p)))]
-unguardedRHS loc rhs = [L loc (GRHS noExtField [] rhs)]
-
-mkMatchGroup :: ( XMG (GhcPass p) (Located (body (GhcPass p))) ~ NoExtField )
- => Origin -> [Located (Match (GhcPass p) (Located (body (GhcPass p))))]
- -> MatchGroup (GhcPass p) (Located (body (GhcPass p)))
+ (pat:_) -> combineSrcSpansA (getLoc pat) (getLoc rhs)
+
+unguardedGRHSs :: Anno (GRHS (GhcPass p) (LocatedA (body (GhcPass p))))
+ ~ SrcSpan
+ => SrcSpan -> LocatedA (body (GhcPass p)) -> ApiAnn' GrhsAnn
+ -> GRHSs (GhcPass p) (LocatedA (body (GhcPass p)))
+unguardedGRHSs loc rhs an
+ = GRHSs noExtField (unguardedRHS an loc rhs) emptyLocalBinds
+
+unguardedRHS :: Anno (GRHS (GhcPass p) (LocatedA (body (GhcPass p))))
+ ~ SrcSpan
+ => ApiAnn' GrhsAnn -> SrcSpan -> LocatedA (body (GhcPass p))
+ -> [LGRHS (GhcPass p) (LocatedA (body (GhcPass p)))]
+unguardedRHS an loc rhs = [L loc (GRHS an [] rhs)]
+
+type AnnoBody p body
+ = ( XMG (GhcPass p) (LocatedA (body (GhcPass p))) ~ NoExtField
+ , Anno [LocatedA (Match (GhcPass p) (LocatedA (body (GhcPass p))))] ~ SrcSpanAnnL
+ , Anno (Match (GhcPass p) (LocatedA (body (GhcPass p)))) ~ SrcSpanAnnA
+ )
+
+mkMatchGroup :: AnnoBody p body
+ => Origin
+ -> LocatedL [LocatedA (Match (GhcPass p) (LocatedA (body (GhcPass p))))]
+ -> MatchGroup (GhcPass p) (LocatedA (body (GhcPass p)))
mkMatchGroup origin matches = MG { mg_ext = noExtField
- , mg_alts = mkLocatedList matches
+ , mg_alts = matches
, mg_origin = origin }
-mkLocatedList :: [Located a] -> Located [Located a]
-mkLocatedList [] = noLoc []
-mkLocatedList ms = L (combineLocs (head ms) (last ms)) ms
+mkLocatedList :: Semigroup a => [GenLocated (SrcSpanAnn' a) e2] -> LocatedAn an [GenLocated (SrcSpanAnn' a) e2]
+mkLocatedList [] = noLocA []
+mkLocatedList ms = L (noAnnSrcSpan $ locA $ combineLocsA (head ms) (last ms)) ms
mkHsApp :: LHsExpr (GhcPass id) -> LHsExpr (GhcPass id) -> LHsExpr (GhcPass id)
-mkHsApp = mkHsAppWith addCLoc
+mkHsApp e1 e2 = addCLocAA e1 e2 (HsApp noComments e1 e2)
mkHsAppWith
:: (LHsExpr (GhcPass id) -> LHsExpr (GhcPass id) -> HsExpr (GhcPass id) -> LHsExpr (GhcPass id))
-> LHsExpr (GhcPass id)
-> LHsExpr (GhcPass id)
-> LHsExpr (GhcPass id)
-mkHsAppWith mkLocated e1 e2 = mkLocated e1 e2 (HsApp noExtField e1 e2)
+mkHsAppWith mkLocated e1 e2 = mkLocated e1 e2 (HsApp noAnn e1 e2)
mkHsApps
:: LHsExpr (GhcPass id) -> [LHsExpr (GhcPass id)] -> LHsExpr (GhcPass id)
-mkHsApps = mkHsAppsWith addCLoc
+mkHsApps = mkHsAppsWith addCLocAA
mkHsAppsWith
:: (LHsExpr (GhcPass id) -> LHsExpr (GhcPass id) -> HsExpr (GhcPass id) -> LHsExpr (GhcPass id))
@@ -221,7 +241,7 @@ mkHsAppsWith
mkHsAppsWith mkLocated = foldl' (mkHsAppWith mkLocated)
mkHsAppType :: LHsExpr GhcRn -> LHsWcType GhcRn -> LHsExpr GhcRn
-mkHsAppType e t = addCLoc e t_body (HsAppType noExtField e paren_wct)
+mkHsAppType e t = addCLocAA t_body e (HsAppType noExtField e paren_wct)
where
t_body = hswc_body t
paren_wct = t { hswc_body = parenthesizeHsType appPrec t_body }
@@ -229,15 +249,14 @@ mkHsAppType e t = addCLoc e t_body (HsAppType noExtField e paren_wct)
mkHsAppTypes :: LHsExpr GhcRn -> [LHsWcType GhcRn] -> LHsExpr GhcRn
mkHsAppTypes = foldl' mkHsAppType
-mkHsLam :: IsPass p
- => (XMG (GhcPass p) (LHsExpr (GhcPass p)) ~ NoExtField)
+mkHsLam :: (IsPass p, XMG (GhcPass p) (LHsExpr (GhcPass p)) ~ NoExtField)
=> [LPat (GhcPass p)]
-> LHsExpr (GhcPass p)
-> LHsExpr (GhcPass p)
mkHsLam pats body = mkHsPar (L (getLoc body) (HsLam noExtField matches))
where
matches = mkMatchGroup Generated
- [mkSimpleMatch LambdaExpr pats' body]
+ (noLocA [mkSimpleMatch LambdaExpr pats' body])
pats' = map (parenthesizePat appPrec) pats
mkHsLams :: [TyVar] -> [EvVar] -> LHsExpr GhcTc -> LHsExpr GhcTc
@@ -246,14 +265,18 @@ mkHsLams tyvars dicts expr = mkLHsWrap (mkWpTyLams tyvars
-- |A simple case alternative with a single pattern, no binds, no guards;
-- pre-typechecking
-mkHsCaseAlt :: LPat (GhcPass p) -> (Located (body (GhcPass p)))
- -> LMatch (GhcPass p) (Located (body (GhcPass p)))
+mkHsCaseAlt :: (Anno (GRHS (GhcPass p) (LocatedA (body (GhcPass p))))
+ ~ SrcSpan,
+ Anno (Match (GhcPass p) (LocatedA (body (GhcPass p))))
+ ~ SrcSpanAnnA)
+ => LPat (GhcPass p) -> (LocatedA (body (GhcPass p)))
+ -> LMatch (GhcPass p) (LocatedA (body (GhcPass p)))
mkHsCaseAlt pat expr
= mkSimpleMatch CaseAlt [pat] expr
nlHsTyApp :: Id -> [Type] -> LHsExpr GhcTc
nlHsTyApp fun_id tys
- = noLoc (mkHsWrap (mkWpTyApps tys) (HsVar noExtField (noLoc fun_id)))
+ = noLocA (mkHsWrap (mkWpTyApps tys) (HsVar noExtField (noLocA fun_id)))
nlHsTyApps :: Id -> [Type] -> [LHsExpr GhcTc] -> LHsExpr GhcTc
nlHsTyApps fun_id tys xs = foldl' nlHsApp (nlHsTyApp fun_id tys) xs
@@ -263,16 +286,16 @@ nlHsTyApps fun_id tys xs = foldl' nlHsApp (nlHsTyApp fun_id tys) xs
-- So @f x@ becomes @(f x)@, but @3@ stays as @3@.
mkLHsPar :: IsPass id => LHsExpr (GhcPass id) -> LHsExpr (GhcPass id)
mkLHsPar le@(L loc e)
- | hsExprNeedsParens appPrec e = L loc (HsPar noExtField le)
+ | hsExprNeedsParens appPrec e = L loc (HsPar noAnn le)
| otherwise = le
mkParPat :: IsPass p => LPat (GhcPass p) -> LPat (GhcPass p)
mkParPat lp@(L loc p)
- | patNeedsParens appPrec p = L loc (ParPat noExtField lp)
+ | patNeedsParens appPrec p = L loc (ParPat noAnn lp)
| otherwise = lp
nlParPat :: LPat (GhcPass name) -> LPat (GhcPass name)
-nlParPat p = noLoc (ParPat noExtField p)
+nlParPat p = noLocA (ParPat noAnn p)
-------------------------------
-- These are the bits of syntax that contain rebindable names
@@ -281,31 +304,49 @@ nlParPat p = noLoc (ParPat noExtField p)
mkHsIntegral :: IntegralLit -> HsOverLit GhcPs
mkHsFractional :: FractionalLit -> HsOverLit GhcPs
mkHsIsString :: SourceText -> FastString -> HsOverLit GhcPs
-mkHsDo :: HsStmtContext GhcRn -> [ExprLStmt GhcPs] -> HsExpr GhcPs
+mkHsDo :: HsStmtContext GhcRn -> LocatedL [ExprLStmt GhcPs] -> HsExpr GhcPs
+mkHsDoAnns :: HsStmtContext GhcRn -> LocatedL [ExprLStmt GhcPs] -> ApiAnn' AnnList -> HsExpr GhcPs
mkHsComp :: HsStmtContext GhcRn -> [ExprLStmt GhcPs] -> LHsExpr GhcPs
-> HsExpr GhcPs
+mkHsCompAnns :: HsStmtContext GhcRn -> [ExprLStmt GhcPs] -> LHsExpr GhcPs
+ -> ApiAnn' AnnList
+ -> HsExpr GhcPs
-mkNPat :: Located (HsOverLit GhcPs) -> Maybe (SyntaxExpr GhcPs)
+mkNPat :: Located (HsOverLit GhcPs) -> Maybe (SyntaxExpr GhcPs) -> ApiAnn
+ -> Pat GhcPs
+mkNPlusKPat :: LocatedN RdrName -> Located (HsOverLit GhcPs) -> ApiAnn
-> Pat GhcPs
-mkNPlusKPat :: Located RdrName -> Located (HsOverLit GhcPs) -> Pat GhcPs
-- NB: The following functions all use noSyntaxExpr: the generated expressions
-- will not work with rebindable syntax if used after the renamer
-mkLastStmt :: IsPass idR => Located (bodyR (GhcPass idR))
- -> StmtLR (GhcPass idL) (GhcPass idR) (Located (bodyR (GhcPass idR)))
-mkBodyStmt :: Located (bodyR GhcPs)
- -> StmtLR (GhcPass idL) GhcPs (Located (bodyR GhcPs))
-mkPsBindStmt :: LPat GhcPs -> Located (bodyR GhcPs)
- -> StmtLR GhcPs GhcPs (Located (bodyR GhcPs))
-mkRnBindStmt :: LPat GhcRn -> Located (bodyR GhcRn)
- -> StmtLR GhcRn GhcRn (Located (bodyR GhcRn))
-mkTcBindStmt :: LPat GhcTc -> Located (bodyR GhcTc)
- -> StmtLR GhcTc GhcTc (Located (bodyR GhcTc))
-
-emptyRecStmt :: StmtLR (GhcPass idL) GhcPs bodyR
-emptyRecStmtName :: StmtLR GhcRn GhcRn bodyR
-emptyRecStmtId :: StmtLR GhcTc GhcTc bodyR
-mkRecStmt :: [LStmtLR (GhcPass idL) GhcPs bodyR]
+mkLastStmt :: IsPass idR => LocatedA (bodyR (GhcPass idR))
+ -> StmtLR (GhcPass idL) (GhcPass idR) (LocatedA (bodyR (GhcPass idR)))
+mkBodyStmt :: LocatedA (bodyR GhcPs)
+ -> StmtLR (GhcPass idL) GhcPs (LocatedA (bodyR GhcPs))
+mkPsBindStmt :: ApiAnn -> LPat GhcPs -> LocatedA (bodyR GhcPs)
+ -> StmtLR GhcPs GhcPs (LocatedA (bodyR GhcPs))
+mkRnBindStmt :: LPat GhcRn -> LocatedA (bodyR GhcRn)
+ -> StmtLR GhcRn GhcRn (LocatedA (bodyR GhcRn))
+mkTcBindStmt :: LPat GhcTc -> LocatedA (bodyR GhcTc)
+ -> StmtLR GhcTc GhcTc (LocatedA (bodyR GhcTc))
+
+emptyRecStmt :: (Anno [GenLocated
+ (Anno (StmtLR (GhcPass idL) GhcPs bodyR))
+ (StmtLR (GhcPass idL) GhcPs bodyR)]
+ ~ SrcSpanAnnL)
+ => StmtLR (GhcPass idL) GhcPs bodyR
+emptyRecStmtName :: (Anno [GenLocated
+ (Anno (StmtLR GhcRn GhcRn bodyR))
+ (StmtLR GhcRn GhcRn bodyR)]
+ ~ SrcSpanAnnL)
+ => StmtLR GhcRn GhcRn bodyR
+emptyRecStmtId :: Stmt GhcTc (LocatedA (HsCmd GhcTc))
+mkRecStmt :: (Anno [GenLocated
+ (Anno (StmtLR (GhcPass idL) GhcPs bodyR))
+ (StmtLR (GhcPass idL) GhcPs bodyR)]
+ ~ SrcSpanAnnL)
+ => ApiAnn' AnnList
+ -> LocatedL [LStmtLR (GhcPass idL) GhcPs bodyR]
-> StmtLR (GhcPass idL) GhcPs bodyR
@@ -313,49 +354,54 @@ mkHsIntegral i = OverLit noExtField (HsIntegral i) noExpr
mkHsFractional f = OverLit noExtField (HsFractional f) noExpr
mkHsIsString src s = OverLit noExtField (HsIsString src s) noExpr
-mkHsDo ctxt stmts = HsDo noExtField ctxt (mkLocatedList stmts)
-mkHsComp ctxt stmts expr = mkHsDo ctxt (stmts ++ [last_stmt])
+mkHsDo ctxt stmts = HsDo noAnn ctxt stmts
+mkHsDoAnns ctxt stmts anns = HsDo anns ctxt stmts
+mkHsComp ctxt stmts expr = mkHsCompAnns ctxt stmts expr noAnn
+mkHsCompAnns ctxt stmts expr anns = mkHsDoAnns ctxt (mkLocatedList (stmts ++ [last_stmt])) anns
where
- last_stmt = L (getLoc expr) $ mkLastStmt expr
+ -- Strip the annotations from the location, they are in the embedded expr
+ last_stmt = L (noAnnSrcSpan $ getLocA expr) $ mkLastStmt expr
-- restricted to GhcPs because other phases might need a SyntaxExpr
-mkHsIf :: LHsExpr GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs -> HsExpr GhcPs
-mkHsIf c a b = HsIf noExtField c a b
+mkHsIf :: LHsExpr GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs -> ApiAnn
+ -> HsExpr GhcPs
+mkHsIf c a b anns = HsIf anns c a b
-- restricted to GhcPs because other phases might need a SyntaxExpr
-mkHsCmdIf :: LHsExpr GhcPs -> LHsCmd GhcPs -> LHsCmd GhcPs -> HsCmd GhcPs
-mkHsCmdIf c a b = HsCmdIf noExtField noSyntaxExpr c a b
+mkHsCmdIf :: LHsExpr GhcPs -> LHsCmd GhcPs -> LHsCmd GhcPs -> ApiAnn
+ -> HsCmd GhcPs
+mkHsCmdIf c a b anns = HsCmdIf anns noSyntaxExpr c a b
-mkNPat lit neg = NPat noExtField lit neg noSyntaxExpr
-mkNPlusKPat id lit
- = NPlusKPat noExtField id lit (unLoc lit) noSyntaxExpr noSyntaxExpr
+mkNPat lit neg anns = NPat anns lit neg noSyntaxExpr
+mkNPlusKPat id lit anns
+ = NPlusKPat anns id lit (unLoc lit) noSyntaxExpr noSyntaxExpr
-mkTransformStmt :: [ExprLStmt GhcPs] -> LHsExpr GhcPs
+mkTransformStmt :: ApiAnn -> [ExprLStmt GhcPs] -> LHsExpr GhcPs
-> StmtLR GhcPs GhcPs (LHsExpr GhcPs)
-mkTransformByStmt :: [ExprLStmt GhcPs] -> LHsExpr GhcPs
+mkTransformByStmt :: ApiAnn -> [ExprLStmt GhcPs] -> LHsExpr GhcPs
-> LHsExpr GhcPs -> StmtLR GhcPs GhcPs (LHsExpr GhcPs)
-mkGroupUsingStmt :: [ExprLStmt GhcPs] -> LHsExpr GhcPs
+mkGroupUsingStmt :: ApiAnn -> [ExprLStmt GhcPs] -> LHsExpr GhcPs
-> StmtLR GhcPs GhcPs (LHsExpr GhcPs)
-mkGroupByUsingStmt :: [ExprLStmt GhcPs] -> LHsExpr GhcPs
+mkGroupByUsingStmt :: ApiAnn -> [ExprLStmt GhcPs] -> LHsExpr GhcPs
-> LHsExpr GhcPs
-> StmtLR GhcPs GhcPs (LHsExpr GhcPs)
-emptyTransStmt :: StmtLR GhcPs GhcPs (LHsExpr GhcPs)
-emptyTransStmt = TransStmt { trS_ext = noExtField
- , trS_form = panic "emptyTransStmt: form"
- , trS_stmts = [], trS_bndrs = []
- , trS_by = Nothing, trS_using = noLoc noExpr
- , trS_ret = noSyntaxExpr, trS_bind = noSyntaxExpr
- , trS_fmap = noExpr }
-mkTransformStmt ss u = emptyTransStmt { trS_form = ThenForm, trS_stmts = ss, trS_using = u }
-mkTransformByStmt ss u b = emptyTransStmt { trS_form = ThenForm, trS_stmts = ss, trS_using = u, trS_by = Just b }
-mkGroupUsingStmt ss u = emptyTransStmt { trS_form = GroupForm, trS_stmts = ss, trS_using = u }
-mkGroupByUsingStmt ss b u = emptyTransStmt { trS_form = GroupForm, trS_stmts = ss, trS_using = u, trS_by = Just b }
+emptyTransStmt :: ApiAnn -> StmtLR GhcPs GhcPs (LHsExpr GhcPs)
+emptyTransStmt anns = TransStmt { trS_ext = anns
+ , trS_form = panic "emptyTransStmt: form"
+ , trS_stmts = [], trS_bndrs = []
+ , trS_by = Nothing, trS_using = noLocA noExpr
+ , trS_ret = noSyntaxExpr, trS_bind = noSyntaxExpr
+ , trS_fmap = noExpr }
+mkTransformStmt a ss u = (emptyTransStmt a) { trS_form = ThenForm, trS_stmts = ss, trS_using = u }
+mkTransformByStmt a ss u b = (emptyTransStmt a) { trS_form = ThenForm, trS_stmts = ss, trS_using = u, trS_by = Just b }
+mkGroupUsingStmt a ss u = (emptyTransStmt a) { trS_form = GroupForm, trS_stmts = ss, trS_using = u }
+mkGroupByUsingStmt a ss b u = (emptyTransStmt a) { trS_form = GroupForm, trS_stmts = ss, trS_using = u, trS_by = Just b }
mkLastStmt body = LastStmt noExtField body Nothing noSyntaxExpr
mkBodyStmt body
= BodyStmt noExtField body noSyntaxExpr noSyntaxExpr
-mkPsBindStmt pat body = BindStmt noExtField pat body
+mkPsBindStmt ann pat body = BindStmt ann pat body
mkRnBindStmt pat body = BindStmt (XBindStmtRn { xbsrn_bindOp = noSyntaxExpr, xbsrn_failOp = Nothing }) pat body
mkTcBindStmt pat body = BindStmt (XBindStmtTc { xbstc_bindOp = noSyntaxExpr,
xbstc_boundResultType = unitTy,
@@ -364,12 +410,14 @@ mkTcBindStmt pat body = BindStmt (XBindStmtTc { xbstc_bindOp = noSyntaxExpr,
xbstc_boundResultMult = Many,
xbstc_failOp = Nothing }) pat body
-emptyRecStmt' :: forall idL idR body. IsPass idR
+emptyRecStmt' :: forall idL idR body .
+ (WrapXRec (GhcPass idR) [LStmtLR (GhcPass idL) (GhcPass idR) body], IsPass idR)
=> XRecStmt (GhcPass idL) (GhcPass idR) body
-> StmtLR (GhcPass idL) (GhcPass idR) body
emptyRecStmt' tyVal =
RecStmt
- { recS_stmts = [], recS_later_ids = []
+ { recS_stmts = wrapXRec @(GhcPass idR) []
+ , recS_later_ids = []
, recS_rec_ids = []
, recS_ret_fn = noSyntaxExpr
, recS_mfix_fn = noSyntaxExpr
@@ -382,26 +430,29 @@ unitRecStmtTc = RecStmtTc { recS_bind_ty = unitTy
, recS_rec_rets = []
, recS_ret_ty = unitTy }
-emptyRecStmt = emptyRecStmt' noExtField
+emptyRecStmt = emptyRecStmt' noAnn
emptyRecStmtName = emptyRecStmt' noExtField
emptyRecStmtId = emptyRecStmt' unitRecStmtTc
-- a panic might trigger during zonking
-mkRecStmt stmts = emptyRecStmt { recS_stmts = stmts }
+mkRecStmt anns stmts = (emptyRecStmt' anns) { recS_stmts = stmts }
+
+mkLetStmt :: ApiAnn -> HsLocalBinds GhcPs -> StmtLR GhcPs GhcPs (LocatedA b)
+mkLetStmt anns binds = LetStmt anns binds
-------------------------------
-- | A useful function for building @OpApps@. The operator is always a
-- variable, and we don't know the fixity yet.
mkHsOpApp :: LHsExpr GhcPs -> IdP GhcPs -> LHsExpr GhcPs -> HsExpr GhcPs
-mkHsOpApp e1 op e2 = OpApp noExtField e1 (noLoc (HsVar noExtField (noLoc op))) e2
+mkHsOpApp e1 op e2 = OpApp noAnn e1 (noLocA (HsVar noExtField (noLocA op))) e2
unqualSplice :: RdrName
unqualSplice = mkRdrUnqual (mkVarOccFS (fsLit "splice"))
-mkUntypedSplice :: SpliceDecoration -> LHsExpr GhcPs -> HsSplice GhcPs
-mkUntypedSplice hasParen e = HsUntypedSplice noExtField hasParen unqualSplice e
+mkUntypedSplice :: ApiAnn -> SpliceDecoration -> LHsExpr GhcPs -> HsSplice GhcPs
+mkUntypedSplice ann hasParen e = HsUntypedSplice ann hasParen unqualSplice e
-mkTypedSplice :: SpliceDecoration -> LHsExpr GhcPs -> HsSplice GhcPs
-mkTypedSplice hasParen e = HsTypedSplice noExtField hasParen unqualSplice e
+mkTypedSplice :: ApiAnn -> SpliceDecoration -> LHsExpr GhcPs -> HsSplice GhcPs
+mkTypedSplice ann hasParen e = HsTypedSplice ann hasParen unqualSplice e
mkHsQuasiQuote :: RdrName -> SrcSpan -> FastString -> HsSplice GhcPs
mkHsQuasiQuote quoter span quote
@@ -425,50 +476,55 @@ mkHsCharPrimLit c = HsChar NoSourceText c
************************************************************************
-}
-nlHsVar :: IdP (GhcPass id) -> LHsExpr (GhcPass id)
-nlHsVar n = noLoc (HsVar noExtField (noLoc n))
+nlHsVar :: IsSrcSpanAnn p a
+ => IdP (GhcPass p) -> LHsExpr (GhcPass p)
+nlHsVar n = noLocA (HsVar noExtField (noLocA n))
-nl_HsVar :: IdP (GhcPass id) -> HsExpr (GhcPass id)
-nl_HsVar n = HsVar noExtField (noLoc n)
+nl_HsVar :: IsSrcSpanAnn p a
+ => IdP (GhcPass p) -> HsExpr (GhcPass p)
+nl_HsVar n = HsVar noExtField (noLocA n)
-- | NB: Only for 'LHsExpr' 'Id'.
nlHsDataCon :: DataCon -> LHsExpr GhcTc
-nlHsDataCon con = noLoc (HsConLikeOut noExtField (RealDataCon con))
+nlHsDataCon con = noLocA (HsConLikeOut noExtField (RealDataCon con))
nlHsLit :: HsLit (GhcPass p) -> LHsExpr (GhcPass p)
-nlHsLit n = noLoc (HsLit noExtField n)
+nlHsLit n = noLocA (HsLit noComments n)
nlHsIntLit :: Integer -> LHsExpr (GhcPass p)
-nlHsIntLit n = noLoc (HsLit noExtField (HsInt noExtField (mkIntegralLit n)))
+nlHsIntLit n = noLocA (HsLit noComments (HsInt noExtField (mkIntegralLit n)))
-nlVarPat :: IdP (GhcPass id) -> LPat (GhcPass id)
-nlVarPat n = noLoc (VarPat noExtField (noLoc n))
+nlVarPat :: IsSrcSpanAnn p a
+ => IdP (GhcPass p) -> LPat (GhcPass p)
+nlVarPat n = noLocA (VarPat noExtField (noLocA n))
nlLitPat :: HsLit GhcPs -> LPat GhcPs
-nlLitPat l = noLoc (LitPat noExtField l)
+nlLitPat l = noLocA (LitPat noExtField l)
nlHsApp :: IsPass id => LHsExpr (GhcPass id) -> LHsExpr (GhcPass id) -> LHsExpr (GhcPass id)
-nlHsApp f x = noLoc (HsApp noExtField f (mkLHsPar x))
+nlHsApp f x = noLocA (HsApp noComments f (mkLHsPar x))
nlHsSyntaxApps :: SyntaxExprTc -> [LHsExpr GhcTc]
-> LHsExpr GhcTc
nlHsSyntaxApps (SyntaxExprTc { syn_expr = fun
, syn_arg_wraps = arg_wraps
, syn_res_wrap = res_wrap }) args
- = mkLHsWrap res_wrap (foldl' nlHsApp (noLoc fun) (zipWithEqual "nlHsSyntaxApps"
+ = mkLHsWrap res_wrap (foldl' nlHsApp (noLocA fun) (zipWithEqual "nlHsSyntaxApps"
mkLHsWrap arg_wraps args))
nlHsSyntaxApps NoSyntaxExprTc args = pprPanic "nlHsSyntaxApps" (ppr args)
-- this function should never be called in scenarios where there is no
-- syntax expr
-nlHsApps :: IsPass id => IdP (GhcPass id) -> [LHsExpr (GhcPass id)] -> LHsExpr (GhcPass id)
+nlHsApps :: IsSrcSpanAnn p a
+ => IdP (GhcPass p) -> [LHsExpr (GhcPass p)] -> LHsExpr (GhcPass p)
nlHsApps f xs = foldl' nlHsApp (nlHsVar f) xs
-nlHsVarApps :: IdP (GhcPass id) -> [IdP (GhcPass id)] -> LHsExpr (GhcPass id)
-nlHsVarApps f xs = noLoc (foldl' mk (HsVar noExtField (noLoc f))
- (map ((HsVar noExtField) . noLoc) xs))
+nlHsVarApps :: IsSrcSpanAnn p a
+ => IdP (GhcPass p) -> [IdP (GhcPass p)] -> LHsExpr (GhcPass p)
+nlHsVarApps f xs = noLocA (foldl' mk (HsVar noExtField (noLocA f))
+ (map ((HsVar noExtField) . noLocA) xs))
where
- mk f a = HsApp noExtField (noLoc f) (noLoc a)
+ mk f a = HsApp noComments (noLocA f) (noLocA a)
nlConVarPat :: RdrName -> [RdrName] -> LPat GhcPs
nlConVarPat con vars = nlConPat con (map nlVarPat vars)
@@ -477,38 +533,38 @@ nlConVarPatName :: Name -> [Name] -> LPat GhcRn
nlConVarPatName con vars = nlConPatName con (map nlVarPat vars)
nlInfixConPat :: RdrName -> LPat GhcPs -> LPat GhcPs -> LPat GhcPs
-nlInfixConPat con l r = noLoc $ ConPat
- { pat_con = noLoc con
+nlInfixConPat con l r = noLocA $ ConPat
+ { pat_con = noLocA con
, pat_args = InfixCon (parenthesizePat opPrec l)
(parenthesizePat opPrec r)
- , pat_con_ext = noExtField
+ , pat_con_ext = noAnn
}
nlConPat :: RdrName -> [LPat GhcPs] -> LPat GhcPs
-nlConPat con pats = noLoc $ ConPat
- { pat_con_ext = noExtField
- , pat_con = noLoc con
+nlConPat con pats = noLocA $ ConPat
+ { pat_con_ext = noAnn
+ , pat_con = noLocA con
, pat_args = PrefixCon [] (map (parenthesizePat appPrec) pats)
}
nlConPatName :: Name -> [LPat GhcRn] -> LPat GhcRn
-nlConPatName con pats = noLoc $ ConPat
+nlConPatName con pats = noLocA $ ConPat
{ pat_con_ext = noExtField
- , pat_con = noLoc con
+ , pat_con = noLocA con
, pat_args = PrefixCon [] (map (parenthesizePat appPrec) pats)
}
nlNullaryConPat :: RdrName -> LPat GhcPs
-nlNullaryConPat con = noLoc $ ConPat
- { pat_con_ext = noExtField
- , pat_con = noLoc con
+nlNullaryConPat con = noLocA $ ConPat
+ { pat_con_ext = noAnn
+ , pat_con = noLocA con
, pat_args = PrefixCon [] []
}
nlWildConPat :: DataCon -> LPat GhcPs
-nlWildConPat con = noLoc $ ConPat
- { pat_con_ext = noExtField
- , pat_con = noLoc $ getRdrName con
+nlWildConPat con = noLocA $ ConPat
+ { pat_con_ext = noAnn
+ , pat_con = noLocA $ getRdrName con
, pat_args = PrefixCon [] $
replicate (dataConSourceArity con)
nlWildPat
@@ -516,18 +572,18 @@ nlWildConPat con = noLoc $ ConPat
-- | Wildcard pattern - after parsing
nlWildPat :: LPat GhcPs
-nlWildPat = noLoc (WildPat noExtField )
+nlWildPat = noLocA (WildPat noExtField )
-- | Wildcard pattern - after renaming
nlWildPatName :: LPat GhcRn
-nlWildPatName = noLoc (WildPat noExtField )
+nlWildPatName = noLocA (WildPat noExtField )
nlHsDo :: HsStmtContext GhcRn -> [LStmt GhcPs (LHsExpr GhcPs)]
-> LHsExpr GhcPs
-nlHsDo ctxt stmts = noLoc (mkHsDo ctxt stmts)
+nlHsDo ctxt stmts = noLocA (mkHsDo ctxt (noLocA stmts))
nlHsOpApp :: LHsExpr GhcPs -> IdP GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs
-nlHsOpApp e1 op e2 = noLoc (mkHsOpApp e1 op e2)
+nlHsOpApp e1 op e2 = noLocA (mkHsOpApp e1 op e2)
nlHsLam :: LMatch GhcPs (LHsExpr GhcPs) -> LHsExpr GhcPs
nlHsPar :: LHsExpr (GhcPass id) -> LHsExpr (GhcPass id)
@@ -535,80 +591,89 @@ nlHsCase :: LHsExpr GhcPs -> [LMatch GhcPs (LHsExpr GhcPs)]
-> LHsExpr GhcPs
nlList :: [LHsExpr GhcPs] -> LHsExpr GhcPs
-nlHsLam match = noLoc (HsLam noExtField (mkMatchGroup Generated [match]))
-nlHsPar e = noLoc (HsPar noExtField e)
+-- AZ:Is this used?
+nlHsLam match = noLocA (HsLam noExtField (mkMatchGroup Generated (noLocA [match])))
+nlHsPar e = noLocA (HsPar noAnn e)
-- nlHsIf should generate if-expressions which are NOT subject to
-- RebindableSyntax, so the first field of HsIf is False. (#12080)
nlHsIf :: LHsExpr GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs
-nlHsIf cond true false = noLoc (HsIf noExtField cond true false)
+nlHsIf cond true false = noLocA (HsIf noAnn cond true false)
nlHsCase expr matches
- = noLoc (HsCase noExtField expr (mkMatchGroup Generated matches))
-nlList exprs = noLoc (ExplicitList noExtField exprs)
+ = noLocA (HsCase noAnn expr (mkMatchGroup Generated (noLocA matches)))
+nlList exprs = noLocA (ExplicitList noAnn exprs)
nlHsAppTy :: LHsType (GhcPass p) -> LHsType (GhcPass p) -> LHsType (GhcPass p)
-nlHsTyVar :: IdP (GhcPass p) -> LHsType (GhcPass p)
+nlHsTyVar :: IsSrcSpanAnn p a
+ => IdP (GhcPass p) -> LHsType (GhcPass p)
nlHsFunTy :: LHsType (GhcPass p) -> LHsType (GhcPass p) -> LHsType (GhcPass p)
nlHsParTy :: LHsType (GhcPass p) -> LHsType (GhcPass p)
-nlHsAppTy f t = noLoc (HsAppTy noExtField f (parenthesizeHsType appPrec t))
-nlHsTyVar x = noLoc (HsTyVar noExtField NotPromoted (noLoc x))
-nlHsFunTy a b = noLoc (HsFunTy noExtField (HsUnrestrictedArrow NormalSyntax) (parenthesizeHsType funPrec a) b)
-nlHsParTy t = noLoc (HsParTy noExtField t)
+nlHsAppTy f t = noLocA (HsAppTy noExtField f (parenthesizeHsType appPrec t))
+nlHsTyVar x = noLocA (HsTyVar noAnn NotPromoted (noLocA x))
+nlHsFunTy a b = noLocA (HsFunTy noAnn (HsUnrestrictedArrow NormalSyntax) (parenthesizeHsType funPrec a) b)
+nlHsParTy t = noLocA (HsParTy noAnn t)
-nlHsTyConApp :: LexicalFixity -> IdP (GhcPass p)
+nlHsTyConApp :: IsSrcSpanAnn p a
+ => LexicalFixity -> IdP (GhcPass p)
-> [LHsTypeArg (GhcPass p)] -> LHsType (GhcPass p)
nlHsTyConApp fixity tycon tys
| Infix <- fixity
, HsValArg ty1 : HsValArg ty2 : rest <- tys
- = foldl' mk_app (noLoc $ HsOpTy noExtField ty1 (noLoc tycon) ty2) rest
+ = foldl' mk_app (noLocA $ HsOpTy noExtField ty1 (noLocA tycon) ty2) rest
| otherwise
= foldl' mk_app (nlHsTyVar tycon) tys
where
mk_app :: LHsType (GhcPass p) -> LHsTypeArg (GhcPass p) -> LHsType (GhcPass p)
- mk_app fun@(L _ (HsOpTy {})) arg = mk_app (noLoc $ HsParTy noExtField fun) arg
+ mk_app fun@(L _ (HsOpTy {})) arg = mk_app (noLocA $ HsParTy noAnn fun) arg
-- parenthesize things like `(A + B) C`
- mk_app fun (HsValArg ty) = noLoc (HsAppTy noExtField fun (parenthesizeHsType appPrec ty))
- mk_app fun (HsTypeArg _ ki) = noLoc (HsAppKindTy noSrcSpan fun (parenthesizeHsType appPrec ki))
- mk_app fun (HsArgPar _) = noLoc (HsParTy noExtField fun)
+ mk_app fun (HsValArg ty) = noLocA (HsAppTy noExtField fun (parenthesizeHsType appPrec ty))
+ mk_app fun (HsTypeArg _ ki) = noLocA (HsAppKindTy noSrcSpan fun (parenthesizeHsType appPrec ki))
+ mk_app fun (HsArgPar _) = noLocA (HsParTy noAnn fun)
nlHsAppKindTy ::
LHsType (GhcPass p) -> LHsKind (GhcPass p) -> LHsType (GhcPass p)
nlHsAppKindTy f k
- = noLoc (HsAppKindTy noSrcSpan f (parenthesizeHsType appPrec k))
+ = noLocA (HsAppKindTy noSrcSpan f (parenthesizeHsType appPrec k))
{-
Tuples. All these functions are *pre-typechecker* because they lack
types on the tuple.
-}
-mkLHsTupleExpr :: [LHsExpr (GhcPass a)] -> LHsExpr (GhcPass a)
+mkLHsTupleExpr :: [LHsExpr (GhcPass p)] -> XExplicitTuple (GhcPass p)
+ -> LHsExpr (GhcPass p)
-- Makes a pre-typechecker boxed tuple, deals with 1 case
-mkLHsTupleExpr [e] = e
-mkLHsTupleExpr es
- = noLoc $ ExplicitTuple noExtField (map (noLoc . (Present noExtField)) es) Boxed
+mkLHsTupleExpr [e] _ = e
+mkLHsTupleExpr es ext
+ = noLocA $ ExplicitTuple ext (map (Present noAnn) es) Boxed
-mkLHsVarTuple :: [IdP (GhcPass a)] -> LHsExpr (GhcPass a)
-mkLHsVarTuple ids = mkLHsTupleExpr (map nlHsVar ids)
+mkLHsVarTuple :: IsSrcSpanAnn p a
+ => [IdP (GhcPass p)] -> XExplicitTuple (GhcPass p)
+ -> LHsExpr (GhcPass p)
+mkLHsVarTuple ids ext = mkLHsTupleExpr (map nlHsVar ids) ext
nlTuplePat :: [LPat GhcPs] -> Boxity -> LPat GhcPs
-nlTuplePat pats box = noLoc (TuplePat noExtField pats box)
+nlTuplePat pats box = noLocA (TuplePat noAnn pats box)
-missingTupArg :: HsTupArg GhcPs
-missingTupArg = Missing noExtField
+missingTupArg :: ApiAnn' AnnAnchor -> HsTupArg GhcPs
+missingTupArg ann = Missing ann
mkLHsPatTup :: [LPat GhcRn] -> LPat GhcRn
-mkLHsPatTup [] = noLoc $ TuplePat noExtField [] Boxed
+mkLHsPatTup [] = noLocA $ TuplePat noExtField [] Boxed
mkLHsPatTup [lpat] = lpat
mkLHsPatTup lpats = L (getLoc (head lpats)) $ TuplePat noExtField lpats Boxed
-- | The Big equivalents for the source tuple expressions
-mkBigLHsVarTup :: [IdP (GhcPass id)] -> LHsExpr (GhcPass id)
-mkBigLHsVarTup ids = mkBigLHsTup (map nlHsVar ids)
+mkBigLHsVarTup :: IsSrcSpanAnn p a
+ => [IdP (GhcPass p)] -> XExplicitTuple (GhcPass p)
+ -> LHsExpr (GhcPass p)
+mkBigLHsVarTup ids anns = mkBigLHsTup (map nlHsVar ids) anns
-mkBigLHsTup :: [LHsExpr (GhcPass id)] -> LHsExpr (GhcPass id)
-mkBigLHsTup = mkChunkified mkLHsTupleExpr
+mkBigLHsTup :: [LHsExpr (GhcPass id)] -> XExplicitTuple (GhcPass id)
+ -> LHsExpr (GhcPass id)
+mkBigLHsTup es anns = mkChunkified (\e -> mkLHsTupleExpr e anns) es
-- | The Big equivalents for the source tuple patterns
mkBigLHsVarPatTup :: [IdP GhcRn] -> LPat GhcRn
@@ -668,16 +733,17 @@ chunkify xs
-- | Convert an 'LHsType' to an 'LHsSigType'.
hsTypeToHsSigType :: LHsType GhcPs -> LHsSigType GhcPs
hsTypeToHsSigType lty@(L loc ty) = L loc $ case ty of
- HsForAllTy { hst_tele = HsForAllInvis { hsf_invis_bndrs = bndrs }
+ HsForAllTy { hst_tele = HsForAllInvis { hsf_xinvis = an
+ , hsf_invis_bndrs = bndrs }
, hst_body = body }
- -> mkHsExplicitSigType bndrs body
+ -> mkHsExplicitSigType an bndrs body
_ -> mkHsImplicitSigType lty
-- | Convert an 'LHsType' to an 'LHsSigWcType'.
hsTypeToHsSigWcType :: LHsType GhcPs -> LHsSigWcType GhcPs
hsTypeToHsSigWcType = mkHsWildCardBndrs . hsTypeToHsSigType
-mkHsSigEnv :: forall a. (LSig GhcRn -> Maybe ([Located Name], a))
+mkHsSigEnv :: forall a. (LSig GhcRn -> Maybe ([LocatedN Name], a))
-> [LSig GhcRn]
-> NameEnv a
mkHsSigEnv get_info sigs
@@ -710,8 +776,8 @@ mkClassOpSigs :: [LSig GhcPs] -> [LSig GhcPs]
mkClassOpSigs sigs
= map fiddle sigs
where
- fiddle (L loc (TypeSig _ nms ty))
- = L loc (ClassOpSig noExtField False nms (dropWildCards ty))
+ fiddle (L loc (TypeSig anns nms ty))
+ = L loc (ClassOpSig anns False nms (dropWildCards ty))
fiddle sig = sig
{- *********************************************************************
@@ -769,20 +835,20 @@ l
************************************************************************
-}
-mkFunBind :: Origin -> Located RdrName -> [LMatch GhcPs (LHsExpr GhcPs)]
+mkFunBind :: Origin -> LocatedN RdrName -> [LMatch GhcPs (LHsExpr GhcPs)]
-> HsBind GhcPs
-- ^ Not infix, with place holders for coercion and free vars
mkFunBind origin fn ms
= FunBind { fun_id = fn
- , fun_matches = mkMatchGroup origin ms
+ , fun_matches = mkMatchGroup origin (noLocA ms)
, fun_ext = noExtField
, fun_tick = [] }
-mkTopFunBind :: Origin -> Located Name -> [LMatch GhcRn (LHsExpr GhcRn)]
+mkTopFunBind :: Origin -> LocatedN Name -> [LMatch GhcRn (LHsExpr GhcRn)]
-> HsBind GhcRn
-- ^ In Name-land, with empty bind_fvs
mkTopFunBind origin fn ms = FunBind { fun_id = fn
- , fun_matches = mkMatchGroup origin ms
+ , fun_matches = mkMatchGroup origin (noLocA ms)
, fun_ext = emptyNameSet -- NB: closed
-- binding
, fun_tick = [] }
@@ -795,11 +861,11 @@ mkVarBind var rhs = L (getLoc rhs) $
VarBind { var_ext = noExtField,
var_id = var, var_rhs = rhs }
-mkPatSynBind :: Located RdrName -> HsPatSynDetails GhcPs
- -> LPat GhcPs -> HsPatSynDir GhcPs -> HsBind GhcPs
-mkPatSynBind name details lpat dir = PatSynBind noExtField psb
+mkPatSynBind :: LocatedN RdrName -> HsPatSynDetails GhcPs
+ -> LPat GhcPs -> HsPatSynDir GhcPs -> ApiAnn -> HsBind GhcPs
+mkPatSynBind name details lpat dir anns = PatSynBind noExtField psb
where
- psb = PSB{ psb_ext = noExtField
+ psb = PSB{ psb_ext = anns
, psb_id = name
, psb_args = details
, psb_def = lpat
@@ -812,6 +878,25 @@ isInfixFunBind (FunBind { fun_matches = MG _ matches _ })
= any (isInfixMatch . unXRec @id2) (unXRec @id2 matches)
isInfixFunBind _ = False
+-- |Return the 'SrcSpan' encompassing the contents of any enclosed binds
+spanHsLocaLBinds :: (Data (HsLocalBinds (GhcPass p))) => HsLocalBinds (GhcPass p) -> SrcSpan
+spanHsLocaLBinds (EmptyLocalBinds _) = noSrcSpan
+spanHsLocaLBinds (HsValBinds _ (ValBinds _ bs sigs))
+ = foldr combineSrcSpans noSrcSpan (bsSpans ++ sigsSpans)
+ where
+ bsSpans :: [SrcSpan]
+ bsSpans = map getLocA $ bagToList bs
+ sigsSpans :: [SrcSpan]
+ sigsSpans = map getLocA sigs
+spanHsLocaLBinds (HsValBinds _ (XValBindsLR (NValBinds bs sigs)))
+ = foldr combineSrcSpans noSrcSpan (bsSpans ++ sigsSpans)
+ where
+ bsSpans :: [SrcSpan]
+ bsSpans = map getLocA $ concatMap (bagToList . snd) bs
+ sigsSpans :: [SrcSpan]
+ sigsSpans = map getLocA sigs
+spanHsLocaLBinds (HsIPBinds _ (IPBinds _ bs))
+ = foldr combineSrcSpans noSrcSpan (map getLocA bs)
------------
-- | Convenience function using 'mkFunBind'.
@@ -819,9 +904,9 @@ isInfixFunBind _ = False
mkSimpleGeneratedFunBind :: SrcSpan -> RdrName -> [LPat GhcPs]
-> LHsExpr GhcPs -> LHsBind GhcPs
mkSimpleGeneratedFunBind loc fun pats expr
- = L loc $ mkFunBind Generated (L loc fun)
- [mkMatch (mkPrefixFunRhs (L loc fun)) pats expr
- (noLoc emptyLocalBinds)]
+ = L (noAnnSrcSpan loc) $ mkFunBind Generated (L (noAnnSrcSpan loc) fun)
+ [mkMatch (mkPrefixFunRhs (L (noAnnSrcSpan loc) fun)) pats expr
+ emptyLocalBinds]
-- | Make a prefix, non-strict function 'HsMatchContext'
mkPrefixFunRhs :: LIdP p -> HsMatchContext p
@@ -834,17 +919,17 @@ mkMatch :: forall p. IsPass p
=> HsMatchContext (NoGhcTc (GhcPass p))
-> [LPat (GhcPass p)]
-> LHsExpr (GhcPass p)
- -> Located (HsLocalBinds (GhcPass p))
+ -> HsLocalBinds (GhcPass p)
-> LMatch (GhcPass p) (LHsExpr (GhcPass p))
-mkMatch ctxt pats expr lbinds
- = noLoc (Match { m_ext = noExtField
- , m_ctxt = ctxt
- , m_pats = map paren pats
- , m_grhss = GRHSs noExtField (unguardedRHS noSrcSpan expr) lbinds })
+mkMatch ctxt pats expr binds
+ = noLocA (Match { m_ext = noAnn
+ , m_ctxt = ctxt
+ , m_pats = map paren pats
+ , m_grhss = GRHSs noExtField (unguardedRHS noAnn noSrcSpan expr) binds })
where
- paren :: Located (Pat (GhcPass p)) -> Located (Pat (GhcPass p))
+ paren :: LPat (GhcPass p) -> LPat (GhcPass p)
paren lp@(L l p)
- | patNeedsParens appPrec p = L l (ParPat noExtField lp)
+ | patNeedsParens appPrec p = L l (ParPat noAnn lp)
| otherwise = lp
{-
@@ -1059,12 +1144,12 @@ collectStmtBinders
-- Id Binders for a Stmt... [but what about pattern-sig type vars]?
collectStmtBinders flag = \case
BindStmt _ pat _ -> collectPatBinders flag pat
- LetStmt _ binds -> collectLocalBinders flag (unLoc binds)
+ LetStmt _ binds -> collectLocalBinders flag binds
BodyStmt {} -> []
LastStmt {} -> []
ParStmt _ xs _ _ -> collectLStmtsBinders flag [s | ParStmtBlock _ ss _ _ <- xs, s <- ss]
TransStmt { trS_stmts = stmts } -> collectLStmtsBinders flag stmts
- RecStmt { recS_stmts = ss } -> collectLStmtsBinders flag ss
+ RecStmt { recS_stmts = L _ ss } -> collectLStmtsBinders flag ss
ApplicativeStmt _ args _ -> concatMap collectArgBinders args
where
collectArgBinders = \case
@@ -1255,13 +1340,13 @@ hsTyClForeignBinders tycl_decls foreign_decls
`mappend`
foldMap (foldMap hsLInstDeclBinders . group_instds) tycl_decls)
where
- getSelectorNames :: ([Located Name], [LFieldOcc GhcRn]) -> [Name]
+ getSelectorNames :: ([LocatedA Name], [LFieldOcc GhcRn]) -> [Name]
getSelectorNames (ns, fs) = map unLoc ns ++ map (extFieldOcc . unLoc) fs
-------------------
hsLTyClDeclBinders :: IsPass p
- => Located (TyClDecl (GhcPass p))
- -> ([Located (IdP (GhcPass p))], [LFieldOcc (GhcPass p)])
+ => LocatedA (TyClDecl (GhcPass p))
+ -> ([LocatedA (IdP (GhcPass p))], [LFieldOcc (GhcPass p)])
-- ^ 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
@@ -1285,7 +1370,8 @@ hsLTyClDeclBinders (L loc (ClassDecl
[ L fam_loc fam_name | (L fam_loc (FamilyDecl
{ fdLName = L _ fam_name })) <- ats ]
++
- [ L mem_loc mem_name | (L mem_loc (ClassOpSig _ False ns _)) <- sigs
+ [ L mem_loc mem_name
+ | (L mem_loc (ClassOpSig _ False ns _)) <- sigs
, (L _ mem_name) <- ns ]
, [])
hsLTyClDeclBinders (L loc (DataDecl { tcdLName = (L _ name)
@@ -1294,11 +1380,12 @@ hsLTyClDeclBinders (L loc (DataDecl { tcdLName = (L _ name)
-------------------
-hsForeignDeclsBinders :: forall pass. (UnXRec pass, MapXRec pass) => [LForeignDecl pass] -> [LIdP pass]
+hsForeignDeclsBinders :: forall p a. (UnXRec (GhcPass p), IsSrcSpanAnn p a)
+ => [LForeignDecl (GhcPass p)] -> [LIdP (GhcPass p)]
-- ^ See Note [SrcSpan for binders]
hsForeignDeclsBinders foreign_decls
- = [ mapXRec @pass (const $ unXRec @pass n) fi
- | fi@(unXRec @pass -> ForeignImport { fd_name = n })
+ = [ L (noAnnSrcSpan (locA decl_loc)) n
+ | L decl_loc (ForeignImport { fd_name = L _ n })
<- foreign_decls]
@@ -1325,7 +1412,7 @@ getPatSynBinds binds
-------------------
hsLInstDeclBinders :: IsPass p
=> LInstDecl (GhcPass p)
- -> ([Located (IdP (GhcPass p))], [LFieldOcc (GhcPass p)])
+ -> ([LocatedA (IdP (GhcPass p))], [LFieldOcc (GhcPass p)])
hsLInstDeclBinders (L _ (ClsInstD
{ cid_inst = ClsInstDecl
{ cid_datafam_insts = dfis }}))
@@ -1338,7 +1425,7 @@ hsLInstDeclBinders (L _ (TyFamInstD {})) = mempty
-- | the 'SrcLoc' returned are for the whole declarations, not just the names
hsDataFamInstBinders :: IsPass p
=> DataFamInstDecl (GhcPass p)
- -> ([Located (IdP (GhcPass p))], [LFieldOcc (GhcPass p)])
+ -> ([LocatedA (IdP (GhcPass p))], [LFieldOcc (GhcPass p)])
hsDataFamInstBinders (DataFamInstDecl { dfid_eqn = FamEqn { feqn_rhs = defn }})
= hsDataDefnBinders defn
-- There can't be repeated symbols because only data instances have binders
@@ -1347,7 +1434,7 @@ hsDataFamInstBinders (DataFamInstDecl { dfid_eqn = FamEqn { feqn_rhs = defn }})
-- | the 'SrcLoc' returned are for the whole declarations, not just the names
hsDataDefnBinders :: IsPass p
=> HsDataDefn (GhcPass p)
- -> ([Located (IdP (GhcPass p))], [LFieldOcc (GhcPass p)])
+ -> ([LocatedA (IdP (GhcPass p))], [LFieldOcc (GhcPass p)])
hsDataDefnBinders (HsDataDefn { dd_cons = cons })
= hsConDeclsBinders cons
-- See Note [Binders in family instances]
@@ -1358,7 +1445,7 @@ type Seen p = [LFieldOcc (GhcPass p)] -> [LFieldOcc (GhcPass p)]
hsConDeclsBinders :: forall p. IsPass p
=> [LConDecl (GhcPass p)]
- -> ([Located (IdP (GhcPass p))], [LFieldOcc (GhcPass p)])
+ -> ([LocatedA (IdP (GhcPass p))], [LFieldOcc (GhcPass p)])
-- See hsLTyClDeclBinders for what this does
-- The function is boringly complicated because of the records
-- And since we only have equality, we have to be a little careful
@@ -1366,7 +1453,7 @@ hsConDeclsBinders cons
= go id cons
where
go :: Seen p -> [LConDecl (GhcPass p)]
- -> ([Located (IdP (GhcPass p))], [LFieldOcc (GhcPass p)])
+ -> ([LocatedA (IdP (GhcPass p))], [LFieldOcc (GhcPass p)])
go _ [] = ([], [])
go remSeen (r:rs)
-- Don't re-mangle the location of field names, because we don't
@@ -1397,7 +1484,7 @@ hsConDeclsBinders cons
get_flds_gadt remSeen (RecConGADT flds) = get_flds remSeen flds
get_flds_gadt remSeen _ = (remSeen, [])
- get_flds :: Seen p -> Located [LConDeclField (GhcPass p)]
+ get_flds :: Seen p -> LocatedL [LConDeclField (GhcPass p)]
-> (Seen p, [LFieldOcc (GhcPass p)])
get_flds remSeen flds = (remSeen', fld_names)
where
@@ -1447,27 +1534,27 @@ is used but it's only used for one specific purpose in one place so it seemed
easier.
-}
-lStmtsImplicits :: [LStmtLR GhcRn (GhcPass idR) (Located (body (GhcPass idR)))]
+lStmtsImplicits :: [LStmtLR GhcRn (GhcPass idR) (LocatedA (body (GhcPass idR)))]
-> [(SrcSpan, [Name])]
lStmtsImplicits = hs_lstmts
where
- hs_lstmts :: [LStmtLR GhcRn (GhcPass idR) (Located (body (GhcPass idR)))]
+ hs_lstmts :: [LStmtLR GhcRn (GhcPass idR) (LocatedA (body (GhcPass idR)))]
-> [(SrcSpan, [Name])]
hs_lstmts = concatMap (hs_stmt . unLoc)
- hs_stmt :: StmtLR GhcRn (GhcPass idR) (Located (body (GhcPass idR)))
+ hs_stmt :: StmtLR GhcRn (GhcPass idR) (LocatedA (body (GhcPass idR)))
-> [(SrcSpan, [Name])]
hs_stmt (BindStmt _ pat _) = lPatImplicits pat
hs_stmt (ApplicativeStmt _ args _) = concatMap do_arg args
where do_arg (_, ApplicativeArgOne { app_arg_pattern = pat }) = lPatImplicits pat
- do_arg (_, ApplicativeArgMany { app_stmts = stmts }) = hs_lstmts stmts
- hs_stmt (LetStmt _ binds) = hs_local_binds (unLoc binds)
+ do_arg (_, ApplicativeArgMany { app_stmts = stmts }) = hs_lstmts stmts
+ hs_stmt (LetStmt _ binds) = hs_local_binds binds
hs_stmt (BodyStmt {}) = []
hs_stmt (LastStmt {}) = []
hs_stmt (ParStmt _ xs _ _) = hs_lstmts [s | ParStmtBlock _ ss _ _ <- xs
, s <- ss]
hs_stmt (TransStmt { trS_stmts = stmts }) = hs_lstmts stmts
- hs_stmt (RecStmt { recS_stmts = ss }) = hs_lstmts ss
+ hs_stmt (RecStmt { recS_stmts = L _ ss }) = hs_lstmts ss
hs_local_binds (HsValBinds _ val_binds) = hsValBindsImplicits val_binds
hs_local_binds (HsIPBinds {}) = []
@@ -1506,7 +1593,7 @@ lPatImplicits = hs_lpat
hs_pat _ = []
- details :: Located Name -> HsConPatDetails GhcRn -> [(SrcSpan, [Name])]
+ details :: LocatedN Name -> HsConPatDetails GhcRn -> [(SrcSpan, [Name])]
details _ (PrefixCon _ ps) = hs_lpats ps
details n (RecCon fs) =
[(err_loc, collectPatsBinders CollNoDictBinders implicit_pats) | Just{} <- [rec_dotdot fs] ]
@@ -1521,6 +1608,6 @@ lPatImplicits = hs_lpat
, let pat_explicit =
maybe True ((i<) . unLoc)
(rec_dotdot fs)]
- err_loc = maybe (getLoc n) getLoc (rec_dotdot fs)
+ err_loc = maybe (getLocA n) getLoc (rec_dotdot fs)
details _ (InfixCon p1 p2) = hs_lpat p1 ++ hs_lpat p2