{-# OPTIONS_GHC -fno-warn-duplicate-exports -fno-warn-orphans #-}

-- | This module is not used by GHC itself.  Rather, it exports all of
-- the functions and types you are likely to need when writing a
-- plugin for GHC. So authors of plugins can probably get away simply
-- with saying "import GhcPlugins".
--
-- Particularly interesting modules for plugin writers include
-- "CoreSyn" and "CoreMonad".
module GhcPlugins(
        module Plugins,
        module RdrName, module OccName, module Name, module Var, module Id, module IdInfo,
        module CoreMonad, module CoreSyn, module Literal, module DataCon,
        module CoreUtils, module MkCore, module CoreFVs, module CoreSubst,
        module Rules, module Annotations,
        module DynFlags, module Packages,
        module Module, module Type, module TyCon, module Coercion,
        module TysWiredIn, module HscTypes, module BasicTypes,
        module VarSet, module VarEnv, module NameSet, module NameEnv,
        module UniqSet, module UniqFM, module FiniteMap,
        module Util, module GHC.Serialized, module SrcLoc, module Outputable,
        module UniqSupply, module Unique, module FastString,

        -- * Getting 'Name's
        thNameToGhcName
    ) where

-- Plugin stuff itself
import Plugins

-- Variable naming
import RdrName
import OccName  hiding  ( varName {- conflicts with Var.varName -} )
import Name     hiding  ( varName {- reexport from OccName, conflicts with Var.varName -} )
import Var
import Id       hiding  ( lazySetIdInfo, setIdExported, setIdNotExported {- all three conflict with Var -} )
import IdInfo

-- Core
import CoreMonad
import CoreSyn
import Literal
import DataCon
import CoreUtils
import MkCore
import CoreFVs
import CoreSubst hiding( substTyVarBndr, substCoVarBndr, extendCvSubst )
       -- These names are also exported by Type

-- Core "extras"
import Rules
import Annotations

-- Pipeline-related stuff
import DynFlags
import Packages

-- Important GHC types
import Module
import Type     hiding {- conflict with CoreSubst -}
                ( substTy, extendTvSubst, extendTvSubstList, isInScope )
import Coercion hiding {- conflict with CoreSubst -}
                ( substCo )
import TyCon
import TysWiredIn
import HscTypes
import BasicTypes hiding ( Version {- conflicts with Packages.Version -} )

-- Collections and maps
import VarSet
import VarEnv
import NameSet
import NameEnv
import UniqSet
import UniqFM
-- Conflicts with UniqFM:
--import LazyUniqFM
import FiniteMap

-- Common utilities
import Util
import GHC.Serialized
import SrcLoc
import Outputable
import UniqSupply
import Unique           ( Unique, Uniquable(..) )
import FastString
import Data.Maybe

import IfaceEnv         ( lookupOrigIO )
import GhcPrelude
import MonadUtils       ( mapMaybeM )
import GHC.ThToHs       ( thRdrNameGuesses )
import TcEnv            ( lookupGlobal )

import qualified Language.Haskell.TH as TH

{- This instance is defined outside CoreMonad.hs so that
   CoreMonad does not depend on TcEnv -}
instance MonadThings CoreM where
    lookupThing :: Name -> CoreM TyThing
lookupThing Name
name = do { HscEnv
hsc_env <- CoreM HscEnv
getHscEnv
                          ; IO TyThing -> CoreM TyThing
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (IO TyThing -> CoreM TyThing) -> IO TyThing -> CoreM TyThing
forall a b. (a -> b) -> a -> b
$ HscEnv -> Name -> IO TyThing
lookupGlobal HscEnv
hsc_env Name
name }

{-
************************************************************************
*                                                                      *
               Template Haskell interoperability
*                                                                      *
************************************************************************
-}

-- | Attempt to convert a Template Haskell name to one that GHC can
-- understand. Original TH names such as those you get when you use
-- the @'foo@ syntax will be translated to their equivalent GHC name
-- exactly. Qualified or unqualified TH names will be dynamically bound
-- to names in the module being compiled, if possible. Exact TH names
-- will be bound to the name they represent, exactly.
thNameToGhcName :: TH.Name -> CoreM (Maybe Name)
thNameToGhcName :: Name -> CoreM (Maybe Name)
thNameToGhcName Name
th_name
  =  do { [Name]
names <- (RdrName -> CoreM (Maybe Name)) -> [RdrName] -> CoreM [Name]
forall (m :: * -> *) a b.
Applicative m =>
(a -> m (Maybe b)) -> [a] -> m [b]
mapMaybeM RdrName -> CoreM (Maybe Name)
lookup (Name -> [RdrName]
thRdrNameGuesses Name
th_name)
          -- Pick the first that works
          -- E.g. reify (mkName "A") will pick the class A in preference
          -- to the data constructor A
        ; Maybe Name -> CoreM (Maybe Name)
forall (m :: * -> *) a. Monad m => a -> m a
return ([Name] -> Maybe Name
forall a. [a] -> Maybe a
listToMaybe [Name]
names) }
  where
    lookup :: RdrName -> CoreM (Maybe Name)
lookup RdrName
rdr_name
      | Just Name
n <- RdrName -> Maybe Name
isExact_maybe RdrName
rdr_name   -- This happens in derived code
      = Maybe Name -> CoreM (Maybe Name)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe Name -> CoreM (Maybe Name))
-> Maybe Name -> CoreM (Maybe Name)
forall a b. (a -> b) -> a -> b
$ if Name -> Bool
isExternalName Name
n then Name -> Maybe Name
forall a. a -> Maybe a
Just Name
n else Maybe Name
forall a. Maybe a
Nothing
      | Just (Module
rdr_mod, OccName
rdr_occ) <- RdrName -> Maybe (Module, OccName)
isOrig_maybe RdrName
rdr_name
      = do { HscEnv
hsc_env <- CoreM HscEnv
getHscEnv
           ; Name -> Maybe Name
forall a. a -> Maybe a
Just (Name -> Maybe Name) -> CoreM Name -> CoreM (Maybe Name)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> IO Name -> CoreM Name
forall (m :: * -> *) a. MonadIO m => IO a -> m a
liftIO (HscEnv -> Module -> OccName -> IO Name
lookupOrigIO HscEnv
hsc_env Module
rdr_mod OccName
rdr_occ) }
      | Bool
otherwise = Maybe Name -> CoreM (Maybe Name)
forall (m :: * -> *) a. Monad m => a -> m a
return Maybe Name
forall a. Maybe a
Nothing