/compiler/GHC/Plugins.hs

https://github.com/bgamari/ghc · Haskell · 187 lines · 136 code · 16 blank · 35 comment · 6 complexity · c7cc61d12dbe6f6dd662244d23afdd7c MD5 · raw file

  1. {-# OPTIONS_GHC -fno-warn-duplicate-exports -fno-warn-orphans #-}
  2. -- | This module is not used by GHC itself. Rather, it exports all of
  3. -- the functions and types you are likely to need when writing a
  4. -- plugin for GHC. So authors of plugins can probably get away simply
  5. -- with saying "import GHC.Plugins".
  6. --
  7. -- Particularly interesting modules for plugin writers include
  8. -- "GHC.Core" and "GHC.Core.Opt.Monad".
  9. module GHC.Plugins
  10. ( module GHC.Driver.Plugins
  11. , module GHC.Types.Name.Reader
  12. , module GHC.Types.Name.Occurrence
  13. , module GHC.Types.Name
  14. , module GHC.Types.Var
  15. , module GHC.Types.Id
  16. , module GHC.Types.Id.Info
  17. , module GHC.Types.PkgQual
  18. , module GHC.Core.Opt.Monad
  19. , module GHC.Core
  20. , module GHC.Types.Literal
  21. , module GHC.Core.DataCon
  22. , module GHC.Core.Utils
  23. , module GHC.Core.Make
  24. , module GHC.Core.FVs
  25. , module GHC.Core.Subst
  26. , module GHC.Core.Rules
  27. , module GHC.Types.Annotations
  28. , module GHC.Driver.Session
  29. , module GHC.Driver.Ppr
  30. , module GHC.Unit.State
  31. , module GHC.Unit.Module
  32. , module GHC.Unit.Home
  33. , module GHC.Core.Type
  34. , module GHC.Core.TyCon
  35. , module GHC.Core.Coercion
  36. , module GHC.Builtin.Types
  37. , module GHC.Driver.Env
  38. , module GHC.Types.Basic
  39. , module GHC.Types.Var.Set
  40. , module GHC.Types.Var.Env
  41. , module GHC.Types.Name.Set
  42. , module GHC.Types.Name.Env
  43. , module GHC.Types.Unique
  44. , module GHC.Types.Unique.Set
  45. , module GHC.Types.Unique.FM
  46. , module GHC.Data.FiniteMap
  47. , module GHC.Utils.Misc
  48. , module GHC.Serialized
  49. , module GHC.Types.SrcLoc
  50. , module GHC.Utils.Outputable
  51. , module GHC.Utils.Panic
  52. , module GHC.Types.Unique.Supply
  53. , module GHC.Data.FastString
  54. , module GHC.Tc.Errors.Hole.FitTypes -- for hole-fit plugins
  55. , module GHC.Unit.Module.ModGuts
  56. , module GHC.Unit.Module.ModSummary
  57. , module GHC.Unit.Module.ModIface
  58. , module GHC.Types.Meta
  59. , module GHC.Types.SourceError
  60. , module GHC.Parser.Errors.Types
  61. , module GHC.Types.Error
  62. , module GHC.Hs
  63. , -- * Getting 'Name's
  64. thNameToGhcName
  65. )
  66. where
  67. -- Plugin stuff itself
  68. import GHC.Driver.Plugins
  69. -- Variable naming
  70. import GHC.Types.TyThing
  71. import GHC.Types.PkgQual
  72. import GHC.Types.SourceError
  73. import GHC.Types.Name.Reader
  74. import GHC.Types.Name.Occurrence hiding ( varName {- conflicts with Var.varName -} )
  75. import GHC.Types.Name hiding ( varName {- reexport from OccName, conflicts with Var.varName -} )
  76. import GHC.Types.Var
  77. import GHC.Types.Id hiding ( lazySetIdInfo, setIdExported, setIdNotExported {- all three conflict with Var -} )
  78. import GHC.Types.Id.Info
  79. -- Core
  80. import GHC.Core.Opt.Monad
  81. import GHC.Core
  82. import GHC.Types.Literal
  83. import GHC.Core.DataCon
  84. import GHC.Core.Utils
  85. import GHC.Core.Make
  86. import GHC.Core.FVs
  87. import GHC.Core.Subst hiding( substTyVarBndr, substCoVarBndr, extendCvSubst )
  88. -- These names are also exported by Type
  89. import GHC.Core.Rules
  90. import GHC.Types.Annotations
  91. import GHC.Types.Meta
  92. import GHC.Driver.Session
  93. import GHC.Unit.State
  94. import GHC.Unit.Home
  95. import GHC.Unit.Module
  96. import GHC.Unit.Module.ModGuts
  97. import GHC.Unit.Module.ModSummary
  98. import GHC.Unit.Module.ModIface
  99. import GHC.Core.Type hiding {- conflict with GHC.Core.Subst -}
  100. ( substTy, extendTvSubst, extendTvSubstList, isInScope )
  101. import GHC.Core.Coercion hiding {- conflict with GHC.Core.Subst -}
  102. ( substCo )
  103. import GHC.Core.TyCon
  104. import GHC.Builtin.Types
  105. import GHC.Driver.Env
  106. import GHC.Types.Basic
  107. -- Collections and maps
  108. import GHC.Types.Var.Set
  109. import GHC.Types.Var.Env
  110. import GHC.Types.Name.Set
  111. import GHC.Types.Name.Env
  112. import GHC.Types.Unique.Set
  113. import GHC.Types.Unique.FM
  114. -- Conflicts with UniqFM:
  115. --import LazyUniqFM
  116. import GHC.Data.FiniteMap
  117. -- Common utilities
  118. import GHC.Utils.Misc
  119. import GHC.Serialized
  120. import GHC.Types.SrcLoc
  121. import GHC.Utils.Outputable
  122. import GHC.Utils.Panic
  123. import GHC.Driver.Ppr
  124. import GHC.Types.Unique.Supply
  125. import GHC.Types.Unique ( Unique, Uniquable(..) )
  126. import GHC.Data.FastString
  127. import Data.Maybe
  128. import GHC.Iface.Env ( lookupOrigIO )
  129. import GHC.Prelude
  130. import GHC.Utils.Monad ( mapMaybeM )
  131. import GHC.ThToHs ( thRdrNameGuesses )
  132. import GHC.Tc.Utils.Env ( lookupGlobal )
  133. import GHC.Tc.Errors.Hole.FitTypes
  134. -- For parse result plugins
  135. import GHC.Parser.Errors.Types ( PsWarning, PsError )
  136. import GHC.Types.Error ( Messages )
  137. import GHC.Hs ( HsParsedModule )
  138. import qualified Language.Haskell.TH as TH
  139. {- This instance is defined outside GHC.Core.Opt.Monad so that
  140. GHC.Core.Opt.Monad does not depend on GHC.Tc.Utils.Env -}
  141. instance MonadThings CoreM where
  142. lookupThing name = do { hsc_env <- getHscEnv
  143. ; liftIO $ lookupGlobal hsc_env name }
  144. {-
  145. ************************************************************************
  146. * *
  147. Template Haskell interoperability
  148. * *
  149. ************************************************************************
  150. -}
  151. -- | Attempt to convert a Template Haskell name to one that GHC can
  152. -- understand. Original TH names such as those you get when you use
  153. -- the @'foo@ syntax will be translated to their equivalent GHC name
  154. -- exactly. Qualified or unqualified TH names will be dynamically bound
  155. -- to names in the module being compiled, if possible. Exact TH names
  156. -- will be bound to the name they represent, exactly.
  157. thNameToGhcName :: TH.Name -> CoreM (Maybe Name)
  158. thNameToGhcName th_name
  159. = do { names <- mapMaybeM lookup (thRdrNameGuesses th_name)
  160. -- Pick the first that works
  161. -- E.g. reify (mkName "A") will pick the class A in preference
  162. -- to the data constructor A
  163. ; return (listToMaybe names) }
  164. where
  165. lookup rdr_name
  166. | Just n <- isExact_maybe rdr_name -- This happens in derived code
  167. = return $ if isExternalName n then Just n else Nothing
  168. | Just (rdr_mod, rdr_occ) <- isOrig_maybe rdr_name
  169. = do { hsc_env <- getHscEnv
  170. ; Just <$> liftIO (lookupOrigIO hsc_env rdr_mod rdr_occ) }
  171. | otherwise = return Nothing