1212{-# LANGUAGE ViewPatterns #-}
1313{-# LANGUAGE TypeOperators #-}
1414
15+ #if __GLASGOW_HASKELL__ <914
16+ #define data pattern
17+ #else
18+ {-# LANGUAGE ExplicitNamespaces #-}
19+ #endif
20+
1521-- | Thin compatibility layer around GHC
1622--
1723-- This should be the only module with GHC-specific CPP directives, and the
@@ -26,7 +32,7 @@ module Data.Record.Internal.GHC.Shim (
2632 , mkFunBind
2733 , HsModule
2834 , LHsModule
29- , pattern GHC. HsModule
35+ , data GHC. HsModule
3036
3137 -- * Annotations
3238#if __GLASGOW_HASKELL__ < 902
@@ -77,7 +83,14 @@ module Data.Record.Internal.GHC.Shim (
7783
7884 -- * Compat
7985#if __GLASGOW_HASKELL__ >= 910
80- , pattern LambdaExpr
86+ , data LambdaExpr
87+ #endif
88+ , wrapBangTy
89+ , Singleton (.. )
90+
91+ #if __GLASGOW_HASKELL__ >= 914
92+ , getBangType
93+ , getBangStrictness
8194#endif
8295
8396 -- * Re-exports
@@ -107,12 +120,15 @@ module Data.Record.Internal.GHC.Shim (
107120#endif
108121 ) where
109122
123+ #undef data
124+
110125import Control.Monad
111126import Data.List.NonEmpty (NonEmpty (.. ))
112127import Data.Generics (Data , GenericQ , cast , toConstr , gzipWithQ )
113128
114129import qualified Data.List.NonEmpty as NE
115130
131+ -- ATM only 8.10.7 is <900
116132#if __GLASGOW_HASKELL__ < 900
117133
118134import Data.IORef
@@ -122,14 +138,14 @@ import BasicTypes (SourceText (NoSourceText))
122138import ConLike (ConLike )
123139import ErrUtils (mkErrMsg , mkWarnMsg )
124140import GHC hiding (AnnKeywordId (.. ), HsModule , exprType , typeKind , mkFunBind , unLoc )
125- import GhcPlugins hiding ((<>) , getHscEnv , unLoc )
141+ import GhcPlugins hiding ((<>) , getHscEnv , unLoc , singleton )
126142import HscMain (getHscEnv )
127143import NameCache (NameCache (nsUniqs ))
128144import PatSyn (PatSyn )
129145import TcEvidence (HsWrapper (WpHole ))
130146
131- import qualified GHC hiding (unLoc )
132- import qualified GhcPlugins as GHC hiding (unLoc )
147+ import qualified GHC hiding (unLoc , singleton )
148+ import qualified GhcPlugins as GHC hiding (unLoc , singleton )
133149
134150#else
135151
@@ -144,7 +160,7 @@ import GHC.Driver.Main (getHscEnv)
144160import GHC.Tc.Types.Evidence (HsWrapper (WpHole ))
145161import GHC.Utils.Error (Severity (SevError , SevWarning ))
146162
147- import GHC.Plugins hiding ((<>) , getHscEnv , unLoc
163+ import GHC.Plugins hiding ((<>) , getHscEnv , unLoc , singleton
148164#if __GLASGOW_HASKELL__ >= 902
149165 , AnnType , AnnLet , AnnRec , AnnLam , AnnCase
150166 , Exception
@@ -298,6 +314,9 @@ importDecl qualified name = noLocA $ ImportDecl {
298314#endif
299315#if __GLASGOW_HASKELL__ >= 906
300316 , ideclImportList = Nothing
317+ #endif
318+ #if __GLASGOW_HASKELL__ >= 914
319+ , ideclLevelSpec = NotLevelled
301320#endif
302321 }
303322
@@ -483,8 +502,10 @@ hsFunTy = HsFunTy
483502hsFunTy ext = HsFunTy ext (HsUnrestrictedArrow NormalSyntax )
484503#elif __GLASGOW_HASKELL__ < 910
485504hsFunTy ext = HsFunTy ext (HsUnrestrictedArrow (L NoTokenLoc HsNormalTok ))
486- #else
505+ #elif __GLASGOW_HASKELL__ < 914
487506hsFunTy ext = HsFunTy ext (HsUnrestrictedArrow NoEpUniTok )
507+ #else
508+ hsFunTy ext = HsFunTy ext (HsUnannotated (EpArrow defExt))
488509#endif
489510
490511userTyVar :: LIdP GhcPs -> HsTyVarBndr GhcPs
@@ -726,7 +747,7 @@ simpleRecordUpdates =
726747 isSingleLabel (FieldLabelStrings labels) =
727748 case labels of
728749#if __GLASGOW_HASKELL__ >= 906
729- [ L _ (DotFieldOcc _ (L l (FieldLabelString label)))] ->
750+ (matchSingleton -> Just ( L _ (DotFieldOcc _ (L l (FieldLabelString label))))) ->
730751#else
731752 [L _ (DotFieldOcc _ (L l label))] ->
732753#endif
@@ -856,10 +877,54 @@ issueWarning l errMsg = do
856877 bag :: a -> Bag a
857878 bag = listToBag . (: [] )
858879
880+
881+ {- ------------------------------------------------------------------------------
882+ Strictness
883+ -------------------------------------------------------------------------------}
884+
885+ #if __GLASGOW_HASKELL__ >= 914
886+ getBangType :: LHsType GhcPs -> LHsType GhcPs
887+ getBangType (L _ (XHsType (HsBangTy _ _ lty))) = lty
888+ -- getBangType (L _ (HsDocTy x (L _ (HsBangTy _ _ lty)) lds)) =
889+ -- addCLocA lty lds (HsDocTy x lty lds)
890+ getBangType lty = lty
891+
892+ getBangStrictness :: LHsType GhcPs -> HsSrcBang
893+ getBangStrictness (L _ (XHsType (HsBangTy _ s _ty))) = s
894+ -- getBangStrictness (L _ (HsDocTy _ (L _ (HsBangTy (_, s) b _)) _)) = HsSrcBang s b
895+ getBangStrictness _ = HsSrcBang NoSourceText NoSrcUnpack NoSrcStrict
896+ #endif
897+
898+ {- ------------------------------------------------------------------------------
899+ Singleton
900+ -------------------------------------------------------------------------------}
901+
902+ class Singleton f where
903+ singleton :: a -> f a
904+ matchSingleton :: f a -> Maybe a
905+
906+ instance Singleton [] where
907+ singleton x = [x]
908+ matchSingleton [x] = Just x
909+ matchSingleton _ = Nothing
910+
911+ instance Singleton NonEmpty where
912+ singleton x = x :| []
913+ matchSingleton (x :| [] ) = Just x
914+ matchSingleton _ = Nothing
915+
859916{- ------------------------------------------------------------------------------
860917 Compat
861918-------------------------------------------------------------------------------}
862919
920+ #if __GLASGOW_HASKELL__ >= 914
921+ wrapBangTy :: HsTypeGhcPsExt -> HsType GhcPs
922+ wrapBangTy = XHsType
923+ #else
924+ wrapBangTy :: HsType GhcPs -> HsType GhcPs
925+ wrapBangTy = id
926+ #endif
927+
863928#if __GLASGOW_HASKELL__ >= 910
864929pattern LambdaExpr :: HsMatchContext fn
865930pattern LambdaExpr = LamAlt LamSingle
0 commit comments