packages feed

polytropos-plugin-0.1.2.1 / src / Polytropos / Plugin / Compat.hs

{-# LANGUAGE CPP #-}{-# LANGUAGE GADTs #-}{-# LANGUAGE PatternSynonyms #-}{-# LANGUAGE TypeFamilies #-}{-# LANGUAGE ViewPatterns #-}#if __GLASGOW_HASKELL__ >= 914{-# OPTIONS_GHC -Wno-pattern-namespace-specifier #-}#endif-- | Compatibility layer for GHC 9.6 through 9.14.---- Isolates all compiler AST and builtin namespace churn behind a uniform-- interface, keeping the solver and source desugaring logic clean.module Polytropos.Plugin.Compat  ( modBase  , modShow  , modRecords  , CompatRecUpdField  , matchRecordUpd  , extractRecUpdField  , pattern PrefixConPat  , pattern RecConPat  , stripHsPar  , mkHsPar  , polyLabel  , mkPolytroposImportDecl  , compatImportModName  , mkHsApp  , mkTupArg  , mkLambda  ) whereimport GHC.Unit.Types  ( mkModule  , moduleUnit  )import GHC.Plugins  ( Module  , FastString#if __GLASGOW_HASKELL__ >= 1001  , unpackFS#else  , moduleNameString#endif  , unLoc  , mkModuleName  , rdrNameOcc  , occNameFS  , RawPkgQual(..)  )import GHC.Hs.Extension  ( GhcPs  )import GHC.Hs  ( LHsExpr  , LHsRecUpdField  , LImportDecl  , ImportDecl(..)  , ImportDeclQualifiedStyle(..)  , IsBootInterface(..)#if __GLASGOW_HASKELL__ >= 1001  , NoExtField#endif  , noAnn  , noLocA#if __GLASGOW_HASKELL__ >= 910  , noExtField#endif  , HsExpr(..)  , HsFieldBind(..)  , HsConDetails(..)  , LPat  , mkHsLam#if __GLASGOW_HASKELL__ >= 908  , LHsRecUpdFields(..)#endif#if __GLASGOW_HASKELL__ >= 914  , ImportDeclLevelStyle(..)#endif#if __GLASGOW_HASKELL__ >= 912  , FieldOcc(..)#else  , AmbiguousFieldOcc(..)#endif  )import Language.Haskell.Syntax.Expr  ( HsTupArg(..)  )#if __GLASGOW_HASKELL__ >= 1001import Language.Haskell.Syntax.Type (XPrefixCon)import Language.Haskell.Syntax.Text (packHText)import Language.Haskell.Syntax.Module.Name (HsModuleName(..), hsModuleNameString)import GHC.Types.UnresolvedImport (ImportDeclOrigin(..))#endifimport GHC.Hs.ImpExp  ( XImportDeclPass(..)  )import GHC.Types.SourceText  ( SourceText(..)  )#if __GLASGOW_HASKELL__ >= 1001import GHC.Builtin.Modules  ( gHC_INTERNAL_BASE  )#elif __GLASGOW_HASKELL__ >= 910import GHC.Builtin.Names  ( gHC_INTERNAL_BASE  , gHC_INTERNAL_SHOW  )#elseimport GHC.Builtin.Names  ( gHC_BASE  , gHC_SHOW  )#endif--------------------------------------------------------------------------------- Module locations for base / show / records--------------------------------------------------------------------------------- | Location of GHC.Base (or GHC.Internal.Base in 9.10+)modBase :: Module#if __GLASGOW_HASKELL__ >= 910modBase = gHC_INTERNAL_BASE#elsemodBase = gHC_BASE#endif-- | Location of GHC.Show (or GHC.Internal.Show in 9.10+)modShow :: Module#if __GLASGOW_HASKELL__ >= 1001modShow = mkModule (moduleUnit modBase) (mkModuleName "GHC.Internal.Show")#elif __GLASGOW_HASKELL__ >= 910modShow = gHC_INTERNAL_SHOW#elsemodShow = gHC_SHOW#endif-- | Location of GHC.Records (or GHC.Internal.Records in 9.10+)modRecords :: Module#if __GLASGOW_HASKELL__ >= 910modRecords = mkModule (moduleUnit modBase) (mkModuleName "GHC.Internal.Records")#elsemodRecords = mkModule (moduleUnit modBase) (mkModuleName "GHC.Records")#endif--------------------------------------------------------------------------------- Record Update Compatibility (GHC 9.6 vs 9.8 vs 9.14)-------------------------------------------------------------------------------#if __GLASGOW_HASKELL__ >= 908type CompatRecUpdField = LHsRecUpdField GhcPs GhcPs#elsetype CompatRecUpdField = LHsRecUpdField GhcPs#endif-- | Extract the updated record expression and fields from RecordUpd.matchRecordUpd :: HsExpr GhcPs -> Maybe (LHsExpr GhcPs, [CompatRecUpdField])#if __GLASGOW_HASKELL__ >= 908matchRecordUpd (RecordUpd _ r (RegularRecUpdFields _ flds)) = Just (r, flds)matchRecordUpd _ = Nothing#elsematchRecordUpd (RecordUpd _ r (Left flds)) = Just (r, flds)matchRecordUpd _ = Nothing#endif-- | Extract the label FastString and value expression from a record update field.extractRecUpdField :: CompatRecUpdField -> (FastString, LHsExpr GhcPs)#if __GLASGOW_HASKELL__ >= 912extractRecUpdField (unLoc -> HsFieldBind _ (unLoc -> FieldOcc _ (unLoc -> lbl)) arg _) =  (occNameFS (rdrNameOcc lbl), arg)#elseextractRecUpdField (unLoc -> HsFieldBind _ (unLoc -> ambOcc) arg _) =  let lbl = case ambOcc of        Unambiguous _ (unLoc -> rdr) -> rdr        Ambiguous _ (unLoc -> rdr)   -> rdr  in (occNameFS (rdrNameOcc lbl), arg)#endif--------------------------------------------------------------------------------- Pattern & Expression AST Compatibility--------------------------------------------------------------------------------- | Pattern synonym matching PrefixCon constructor details across GHC versions.#if __GLASGOW_HASKELL__ >= 1001pattern PrefixConPat :: (XPrefixCon p ~ NoExtField) => [arg] -> HsConDetails p arg recpattern PrefixConPat args <- PrefixCon _ args where  PrefixConPat args = PrefixCon noExtField argspattern RecConPat :: rec -> HsConDetails p arg recpattern RecConPat r <- RecCon _ r#elif __GLASGOW_HASKELL__ >= 914pattern PrefixConPat :: [arg] -> HsConDetails arg recpattern PrefixConPat args = PrefixCon argspattern RecConPat :: rec -> HsConDetails arg recpattern RecConPat r <- RecCon r#elsepattern PrefixConPat :: [arg] -> HsConDetails tyarg arg recpattern PrefixConPat args <- PrefixCon _ args  where PrefixConPat args = PrefixCon [] argspattern RecConPat :: rec -> HsConDetails tyarg arg recpattern RecConPat r <- RecCon r#endif-- | Strip enclosing parenthesis expressions across GHC versions.stripHsPar :: LHsExpr GhcPs -> LHsExpr GhcPs#if __GLASGOW_HASKELL__ >= 910stripHsPar (unLoc -> HsPar _ e) = stripHsPar e#elsestripHsPar (unLoc -> HsPar _ _ e _) = stripHsPar e#endifstripHsPar e = e-- | Construct a parenthesized expression across GHC versions.mkHsPar :: LHsExpr GhcPs -> LHsExpr GhcPs#if __GLASGOW_HASKELL__ >= 910mkHsPar e = noLocA (HsPar noAnn e)#elsemkHsPar e = noLocA (HsPar noAnn noAnn e noAnn)#endif-- | Construct an overloaded label expression across GHC 9.6-9.14.polyLabel :: FastString -> LHsExpr GhcPs#if __GLASGOW_HASKELL__ >= 1001polyLabel fs = noLocA (HsOverLabel NoSourceText (packHText (unpackFS fs)))#elif __GLASGOW_HASKELL__ >= 912polyLabel fs = noLocA (HsOverLabel NoSourceText fs)#elif __GLASGOW_HASKELL__ >= 910polyLabel fs = noLocA (HsOverLabel noExtField NoSourceText fs)#elsepolyLabel fs = noLocA (HsOverLabel noAnn NoSourceText fs)#endif-- | Construct the 'import qualified Polytropos as Polytropos' declaration.mkPolytroposImportDecl :: LImportDecl GhcPs#if __GLASGOW_HASKELL__ >= 1001mkPolytroposImportDecl = noLocA $ ImportDecl  { ideclExt        = XImportDeclPass noAnn NoSourceText UserWrittenImport  , ideclName       = noLocA (HsModuleName (packHText "Polytropos"))  , ideclPkgQual    = NoRawPkgQual  , ideclSource     = NotBoot  , ideclLevelSpec  = NotLevelled  , ideclSafe       = False  , ideclQualified  = QualifiedPre  , ideclAs         = Just (noLocA (HsModuleName (packHText "Polytropos")))  , ideclImportList = Nothing  }#elif __GLASGOW_HASKELL__ >= 914mkPolytroposImportDecl = noLocA $ ImportDecl  { ideclExt       = XImportDeclPass noAnn NoSourceText False  , ideclName      = noLocA (mkModuleName "Polytropos")  , ideclPkgQual   = NoRawPkgQual  , ideclSource    = NotBoot  , ideclLevelSpec = NotLevelled  , ideclSafe      = False  , ideclQualified = QualifiedPre  , ideclAs        = Just (noLocA (mkModuleName "Polytropos"))  , ideclImportList = Nothing  }#elsemkPolytroposImportDecl = noLocA $ ImportDecl  { ideclExt       = XImportDeclPass noAnn NoSourceText False  , ideclName      = noLocA (mkModuleName "Polytropos")  , ideclPkgQual   = NoRawPkgQual  , ideclSource    = NotBoot  , ideclSafe      = False  , ideclQualified = QualifiedPre  , ideclAs        = Just (noLocA (mkModuleName "Polytropos"))  , ideclImportList = Nothing  }#endif-- | Extract the imported module name as a String across GHC versions.compatImportModName :: LImportDecl GhcPs -> String#if __GLASGOW_HASKELL__ >= 1001compatImportModName (unLoc -> ImportDecl{ ideclName = unLoc -> modNm }) =  hsModuleNameString modNm#elsecompatImportModName (unLoc -> ImportDecl{ ideclName = unLoc -> modNm }) =  moduleNameString modNm#endif-- | Construct a function application expression across GHC 9.6-9.14.mkHsApp :: LHsExpr GhcPs -> LHsExpr GhcPs -> LHsExpr GhcPs#if __GLASGOW_HASKELL__ >= 910mkHsApp f x = noLocA (HsApp noExtField f x)#elsemkHsApp f x = noLocA (HsApp noAnn f x)#endif-- | Construct a tuple present argument across GHC 9.6-9.14.mkTupArg :: LHsExpr GhcPs -> HsTupArg GhcPs#if __GLASGOW_HASKELL__ >= 910mkTupArg e = Present noExtField e#elsemkTupArg e = Present noAnn e#endif-- | Construct a lambda expression across GHC 9.6-9.14.mkLambda :: [LPat GhcPs] -> LHsExpr GhcPs -> LHsExpr GhcPs#if __GLASGOW_HASKELL__ >= 912mkLambda pats body = mkHsLam (noLocA pats) body#elsemkLambda pats body = mkHsLam pats body#endif