{-# LANGUAGE CPP #-}

-- Imports we need to write this module tend to move around in later
-- versions of GHC.  This module uses the CPP extension to import the
-- right values dependent on the version of GHC.

module NriPrelude.Plugin.GhcVersionDependent
  ( setIDeclImplicit,
    withParsedResult,
  )
where

import qualified GHC.Driver.Plugins
import qualified GHC.Hs
import qualified GHC.Hs.ImpExp
import Prelude

withParsedResult :: GHC.Driver.Plugins.ParsedResult -> (GHC.Hs.HsParsedModule -> GHC.Hs.HsParsedModule) -> GHC.Driver.Plugins.ParsedResult
withParsedResult :: ParsedResult -> (HsParsedModule -> HsParsedModule) -> ParsedResult
withParsedResult ParsedResult
parsed HsParsedModule -> HsParsedModule
f =
  ParsedResult
parsed
    { GHC.Driver.Plugins.parsedResultModule = f (GHC.Driver.Plugins.parsedResultModule parsed)
    }

setIDeclImplicit :: Bool -> GHC.Hs.ImpExp.ImportDecl GHC.Hs.GhcPs -> GHC.Hs.ImpExp.ImportDecl GHC.Hs.GhcPs
setIDeclImplicit :: Bool -> ImportDecl GhcPs -> ImportDecl GhcPs
setIDeclImplicit Bool
isImplicit ImportDecl GhcPs
importDecl =
  -- no idea what `XImportDeclPass` _is_, btw.  just following types
  -- ref: https://hackage.haskell.org/package/ghc-9.6.5/docs/Language-Haskell-Syntax-ImpExp.html#t:ImportDecl
  --      https://hackage.haskell.org/package/ghc-9.6.5/docs/Language-Haskell-Syntax-Extension.html#t:XCImportDecl
  --      https://hackage.haskell.org/package/ghc-9.6.5/docs/GHC-Hs-ImpExp.html#t:XImportDeclPass
  let xImportDeclPass :: XCImportDecl GhcPs
xImportDeclPass = ImportDecl GhcPs -> XCImportDecl GhcPs
forall pass. ImportDecl pass -> XCImportDecl pass
GHC.Hs.ImpExp.ideclExt ImportDecl GhcPs
importDecl
   in ImportDecl GhcPs
importDecl
        { GHC.Hs.ImpExp.ideclExt =
            xImportDeclPass {GHC.Hs.ImpExp.ideclImplicit = isImplicit}
        }