compiler: Write .o files atomically. See #14533
[ghc.git] / compiler / main / GhcPlugins.hs
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".
10 module 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
29 import Plugins
30
31 -- Variable naming
32 import RdrName
33 import OccName hiding ( varName {- conflicts with Var.varName -} )
34 import Name hiding ( varName {- reexport from OccName, conflicts with Var.varName -} )
35 import Var
36 import Id hiding ( lazySetIdInfo, setIdExported, setIdNotExported {- all three conflict with Var -} )
37 import IdInfo
38
39 -- Core
40 import CoreMonad
41 import CoreSyn
42 import Literal
43 import DataCon
44 import CoreUtils
45 import MkCore
46 import CoreFVs
47 import CoreSubst hiding( substTyVarBndr, substCoVarBndr, extendCvSubst )
48 -- These names are also exported by Type
49
50 -- Core "extras"
51 import Rules
52 import Annotations
53
54 -- Pipeline-related stuff
55 import DynFlags
56 import Packages
57
58 -- Important GHC types
59 import Module
60 import Type hiding {- conflict with CoreSubst -}
61 ( substTy, extendTvSubst, extendTvSubstList, isInScope )
62 import Coercion hiding {- conflict with CoreSubst -}
63 ( substCo )
64 import TyCon
65 import TysWiredIn
66 import HscTypes
67 import BasicTypes hiding ( Version {- conflicts with Packages.Version -} )
68
69 -- Collections and maps
70 import VarSet
71 import VarEnv
72 import NameSet
73 import NameEnv
74 import UniqSet
75 import UniqFM
76 -- Conflicts with UniqFM:
77 --import LazyUniqFM
78 import FiniteMap
79
80 -- Common utilities
81 import Util
82 import GHC.Serialized
83 import SrcLoc
84 import Outputable
85 import UniqSupply
86 import Unique ( Unique, Uniquable(..) )
87 import FastString
88 import Data.Maybe
89
90 import IfaceEnv ( lookupOrigIO )
91 import GhcPrelude
92 import MonadUtils ( mapMaybeM )
93 import Convert ( thRdrNameGuesses )
94 import TcEnv ( lookupGlobal )
95
96 import qualified Language.Haskell.TH as TH
97
98 {- This instance is defined outside CoreMonad.hs so that
99 CoreMonad does not depend on TcEnv -}
100 instance 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.
118 thNameToGhcName :: TH.Name -> CoreM (Maybe Name)
119 thNameToGhcName 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