1{-# OPTIONS_GHC -fno-warn-duplicate-exports -fno-warn-orphans #-}
2
3-- | This module is not used by GHC itself.  Rather, it exports all of
4-- the functions and types you are likely to need when writing a
5-- plugin for GHC. So authors of plugins can probably get away simply
6-- with saying "import GhcPlugins".
7--
8-- Particularly interesting modules for plugin writers include
9-- "CoreSyn" and "CoreMonad".
10module GhcPlugins(
11        module Plugins,
12        module RdrName, module OccName, module Name, module Var, module Id, module IdInfo,
13        module CoreMonad, module CoreSyn, module Literal, module DataCon,
14        module CoreUtils, module MkCore, module CoreFVs, module CoreSubst,
15        module Rules, module Annotations,
16        module DynFlags, module Packages,
17        module Module, module Type, module TyCon, module Coercion,
18        module TysWiredIn, module HscTypes, module BasicTypes,
19        module VarSet, module VarEnv, module NameSet, module NameEnv,
20        module UniqSet, module UniqFM, module FiniteMap,
21        module Util, module GHC.Serialized, module SrcLoc, module Outputable,
22        module UniqSupply, module Unique, module FastString,
23
24        -- * Getting 'Name's
25        thNameToGhcName
26    ) where
27
28-- Plugin stuff itself
29import Plugins
30
31-- Variable naming
32import RdrName
33import OccName  hiding  ( varName {- conflicts with Var.varName -} )
34import Name     hiding  ( varName {- reexport from OccName, conflicts with Var.varName -} )
35import Var
36import Id       hiding  ( lazySetIdInfo, setIdExported, setIdNotExported {- all three conflict with Var -} )
37import IdInfo
38
39-- Core
40import CoreMonad
41import CoreSyn
42import Literal
43import DataCon
44import CoreUtils
45import MkCore
46import CoreFVs
47import CoreSubst hiding( substTyVarBndr, substCoVarBndr, extendCvSubst )
48       -- These names are also exported by Type
49
50-- Core "extras"
51import Rules
52import Annotations
53
54-- Pipeline-related stuff
55import DynFlags
56import Packages
57
58-- Important GHC types
59import Module
60import Type     hiding {- conflict with CoreSubst -}
61                ( substTy, extendTvSubst, extendTvSubstList, isInScope )
62import Coercion hiding {- conflict with CoreSubst -}
63                ( substCo )
64import TyCon
65import TysWiredIn
66import HscTypes
67import BasicTypes hiding ( Version {- conflicts with Packages.Version -} )
68
69-- Collections and maps
70import VarSet
71import VarEnv
72import NameSet
73import NameEnv
74import UniqSet
75import UniqFM
76-- Conflicts with UniqFM:
77--import LazyUniqFM
78import FiniteMap
79
80-- Common utilities
81import Util
82import GHC.Serialized
83import SrcLoc
84import Outputable
85import UniqSupply
86import Unique           ( Unique, Uniquable(..) )
87import FastString
88import Data.Maybe
89
90import IfaceEnv         ( lookupOrigIO )
91import GhcPrelude
92import MonadUtils       ( mapMaybeM )
93import GHC.ThToHs       ( thRdrNameGuesses )
94import TcEnv            ( lookupGlobal )
95
96import qualified Language.Haskell.TH as TH
97
98{- This instance is defined outside CoreMonad.hs so that
99   CoreMonad does not depend on TcEnv -}
100instance MonadThings CoreM where
101    lookupThing name = do { hsc_env <- getHscEnv
102                          ; liftIO $ lookupGlobal hsc_env name }
103
104{-
105************************************************************************
106*                                                                      *
107               Template Haskell interoperability
108*                                                                      *
109************************************************************************
110-}
111
112-- | Attempt to convert a Template Haskell name to one that GHC can
113-- understand. Original TH names such as those you get when you use
114-- the @'foo@ syntax will be translated to their equivalent GHC name
115-- exactly. Qualified or unqualified TH names will be dynamically bound
116-- to names in the module being compiled, if possible. Exact TH names
117-- will be bound to the name they represent, exactly.
118thNameToGhcName :: TH.Name -> CoreM (Maybe Name)
119thNameToGhcName th_name
120  =  do { names <- mapMaybeM lookup (thRdrNameGuesses th_name)
121          -- Pick the first that works
122          -- E.g. reify (mkName "A") will pick the class A in preference
123          -- to the data constructor A
124        ; return (listToMaybe names) }
125  where
126    lookup rdr_name
127      | Just n <- isExact_maybe rdr_name   -- This happens in derived code
128      = return $ if isExternalName n then Just n else Nothing
129      | Just (rdr_mod, rdr_occ) <- isOrig_maybe rdr_name
130      = do { hsc_env <- getHscEnv
131           ; Just <$> liftIO (lookupOrigIO hsc_env rdr_mod rdr_occ) }
132      | otherwise = return Nothing
133