PageRenderTime 50ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/testsuite/tests/tcplugins/Common.hs

https://github.com/ghc/ghc
Haskell | 118 lines | 96 code | 11 blank | 11 comment | 0 complexity | 0d20aca7d45ee2a52c7faab95428c538 MD5 | raw file
  1. {-# LANGUAGE RecordWildCards #-}
  2. {-# LANGUAGE ViewPatterns #-}
  3. module Common
  4. ( PluginDefs(..)
  5. , mkPlugin
  6. , don'tSolve, don'tRewrite
  7. )
  8. where
  9. -- ghc
  10. import GHC.Core.Class
  11. ( Class )
  12. import GHC.Core.DataCon
  13. ( promoteDataCon )
  14. import GHC.Core.TyCon
  15. ( TyCon )
  16. import GHC.Core.Type
  17. ( Type
  18. , mkTyConApp
  19. )
  20. import GHC.Plugins
  21. ( Plugin(..)
  22. , defaultPlugin, purePlugin
  23. )
  24. import GHC.Tc.Plugin
  25. ( TcPluginM
  26. , findImportedModule, lookupOrig
  27. , tcLookupClass, tcLookupDataCon, tcLookupTyCon
  28. )
  29. import GHC.Tc.Types
  30. ( TcPlugin(..), TcPluginSolveResult(..), TcPluginRewriteResult(..)
  31. , TcPluginRewriter
  32. )
  33. import GHC.Tc.Types.Constraint
  34. ( Ct )
  35. import GHC.Tc.Types.Evidence
  36. ( EvBindsVar )
  37. import GHC.Types.Name.Occurrence
  38. ( mkClsOcc, mkDataOcc, mkTcOcc )
  39. import GHC.Types.Unique.FM
  40. ( UniqFM, emptyUFM )
  41. import GHC.Types.PkgQual
  42. import GHC.Unit.Finder
  43. ( FindResult(..) )
  44. import GHC.Unit.Module
  45. ( Module
  46. , mkModuleName
  47. )
  48. --------------------------------------------------------------------------------
  49. -- This module defines some common operations so that each individual plugin
  50. -- doesn't have to do the same work over again:
  51. --
  52. -- - lookup the names of things the plugins will use
  53. -- (the definitions are shared between most type-checking plugin tests)
  54. -- - create a type-checking plugin from a solver, taking care of passing
  55. -- the relevant data to the solver stage.
  56. data PluginDefs =
  57. PluginDefs
  58. { nullary :: !Class
  59. , myClass :: !Class
  60. , myTyFam :: !TyCon
  61. , nat :: !Type
  62. , zero :: !TyCon
  63. , succ :: !TyCon
  64. , add :: !TyCon
  65. , ctIdFam :: !TyCon
  66. }
  67. definitionsModule :: TcPluginM Module
  68. definitionsModule = do
  69. findResult <- findImportedModule ( mkModuleName "Definitions" ) NoPkgQual
  70. case findResult of
  71. Found _ res -> pure res
  72. FoundMultiple _ -> error $ "TcPlugin test: found multiple modules named 'Definitions'."
  73. _ -> error $ "TcPlugin test: could not find any module named 'Defintiions'."
  74. lookupDefs :: TcPluginM PluginDefs
  75. lookupDefs = do
  76. defs <- definitionsModule
  77. nullary <- tcLookupClass =<< lookupOrig defs ( mkClsOcc "Nullary" )
  78. myClass <- tcLookupClass =<< lookupOrig defs ( mkClsOcc "MyClass" )
  79. myTyFam <- tcLookupTyCon =<< lookupOrig defs ( mkTcOcc "MyTyFam" )
  80. ( (`mkTyConApp` []) -> nat ) <- tcLookupTyCon =<< lookupOrig defs ( mkTcOcc "Nat" )
  81. ( promoteDataCon -> zero ) <- tcLookupDataCon =<< lookupOrig defs ( mkDataOcc "Zero" )
  82. ( promoteDataCon -> succ ) <- tcLookupDataCon =<< lookupOrig defs ( mkDataOcc "Succ" )
  83. add <- tcLookupTyCon =<< lookupOrig defs ( mkTcOcc "Add" )
  84. ctIdFam <- tcLookupTyCon =<< lookupOrig defs ( mkTcOcc "CtId" )
  85. pure ( PluginDefs { .. } )
  86. mkPlugin :: ( [String] -> PluginDefs -> EvBindsVar -> [Ct] -> [Ct] -> TcPluginM TcPluginSolveResult )
  87. -> ( [String] -> PluginDefs -> UniqFM TyCon TcPluginRewriter )
  88. -> Plugin
  89. mkPlugin solve rewrite =
  90. defaultPlugin
  91. { tcPlugin = \ args -> Just $ mkTcPlugin ( solve args ) ( rewrite args )
  92. , pluginRecompile = purePlugin
  93. }
  94. mkTcPlugin :: ( PluginDefs -> EvBindsVar -> [Ct] -> [Ct] -> TcPluginM TcPluginSolveResult )
  95. -> ( PluginDefs -> UniqFM TyCon TcPluginRewriter )
  96. -> TcPlugin
  97. mkTcPlugin solve rewrite =
  98. TcPlugin
  99. { tcPluginInit = lookupDefs
  100. , tcPluginSolve = solve
  101. , tcPluginRewrite = rewrite
  102. , tcPluginStop = \ _ -> pure ()
  103. }
  104. don'tSolve :: [String] -> s -> EvBindsVar -> [Ct] -> [Ct] -> TcPluginM TcPluginSolveResult
  105. don'tSolve _ _ _ _ _ = pure $ TcPluginOk [] []
  106. don'tRewrite :: [String] -> s -> UniqFM TyCon TcPluginRewriter
  107. don'tRewrite _ _ = emptyUFM