/src/Show.hs

https://github.com/mikeizbicki/HerbiePlugin · Haskell · 160 lines · 109 code · 29 blank · 22 comment · 1 complexity · fc866d92da2b55bbd3b0e9df9f1588c5 MD5 · raw file

  1. {-# LANGUAGE FlexibleInstances, MultiWayIf, StandaloneDeriving,
  2. TypeSynonymInstances #-}
  3. {-# OPTIONS_GHC -fno-warn-orphans #-}
  4. -- | We define lots of orphan Show instances here, for debugging and learning
  5. -- purposes.
  6. --
  7. -- Most of the time while trying to figure out when a constructor is used or how
  8. -- is a term compiled, it's easiest to just create an example and run the plugin
  9. -- on it.
  10. --
  11. -- Without Show instances though, we can't easily inspect compiled outputs.
  12. -- Outputable generated strings hide lots of details(especially constructors),
  13. -- but we still export a `showOutputable` here, for similar reasons.
  14. --
  15. module Show where
  16. import Data.IORef
  17. import Data.List (intercalate)
  18. import System.IO.Unsafe (unsafePerformIO)
  19. import Class
  20. import CostCentre
  21. import ForeignCall
  22. import Demand
  23. import GhcPlugins
  24. import IdInfo
  25. import PrimOp
  26. import TypeRep
  27. import Prelude
  28. --------------------------------------------------------------------------------
  29. dbg :: Outputable a => a -> String
  30. dbg a = showSDoc dynFlags (ppr a)
  31. {-# NOINLINE dynFlags_ref #-}
  32. dynFlags_ref :: IORef DynFlags
  33. dynFlags_ref = unsafePerformIO (newIORef undefined)
  34. {-# NOINLINE dynFlags #-}
  35. dynFlags :: DynFlags
  36. dynFlags = unsafePerformIO (readIORef dynFlags_ref)
  37. showOutputable :: Outputable a => a -> String
  38. showOutputable = showSDoc dynFlags . ppr
  39. --------------------------------------------------------------------------------
  40. -- Orphan Show instances
  41. deriving instance Show a => Show (Expr a)
  42. deriving instance Show Type
  43. deriving instance Show Literal
  44. deriving instance Show a => Show (Tickish a)
  45. deriving instance Show a => Show (Bind a)
  46. deriving instance Show AltCon
  47. deriving instance Show TyLit
  48. deriving instance Show FunctionOrData
  49. deriving instance Show Module
  50. deriving instance Show CostCentre
  51. deriving instance Show Role
  52. deriving instance Show LeftOrRight
  53. deriving instance Show IsCafCC
  54. instance Show Class where
  55. show _ = "<Class>"
  56. deriving instance Show IdDetails
  57. deriving instance Show PrimOp
  58. deriving instance Show ForeignCall
  59. deriving instance Show TickBoxOp
  60. deriving instance Show PrimOpVecCat
  61. deriving instance Show CCallSpec
  62. deriving instance Show CCallTarget
  63. deriving instance Show CCallConv
  64. deriving instance Show SpecInfo
  65. deriving instance Show OccInfo
  66. deriving instance Show InlinePragma
  67. deriving instance Show OneShotInfo
  68. deriving instance Show CafInfo
  69. deriving instance Show Unfolding
  70. deriving instance Show UnfoldingSource
  71. deriving instance Show UnfoldingGuidance
  72. deriving instance Show Activation
  73. deriving instance Show CoreRule
  74. -- deriving instance Show IsOrphan
  75. deriving instance Show StrictSig
  76. deriving instance Show DmdType
  77. instance Show RuleFun where
  78. show _ = "<RuleFun>"
  79. instance Show (UniqFM a) where
  80. show _ = "<UniqFM>"
  81. instance Show IdInfo where
  82. show info =
  83. "Info{" ++ intercalate "," [show arityInfo_, show specInfo_, show unfoldingInfo_,
  84. show cafInfo_, show oneShotInfo_, show inlinePragInfo_,
  85. show occInfo_, show strictnessInfo_, show demandInfo_,
  86. show callArityInfo_] ++ "}"
  87. where
  88. arityInfo_ = arityInfo info
  89. specInfo_ = specInfo info
  90. unfoldingInfo_ = unfoldingInfo info
  91. cafInfo_ = cafInfo info
  92. oneShotInfo_ = oneShotInfo info
  93. inlinePragInfo_ = inlinePragInfo info
  94. occInfo_ = occInfo info
  95. strictnessInfo_ = strictnessInfo info
  96. demandInfo_ = demandInfo info
  97. callArityInfo_ = callArityInfo info
  98. instance Show Var where
  99. show v =
  100. if | isId v ->
  101. let details = idDetails v
  102. info = idInfo v
  103. in "Id{" ++ intercalate "," [show name, show uniq, show ty, show details, show info] ++ "}"
  104. | isTyVar v -> "TyVar{" ++ show name ++ "}"
  105. | otherwise -> "TcTyVar{" ++ show name ++ "}"
  106. where
  107. name = varName v
  108. uniq = varUnique v
  109. ty = varType v
  110. instance Show DataCon where
  111. show = show . dataConName
  112. instance Show TyCon where
  113. show = show . tyConName
  114. instance Show ModuleName where
  115. show = show . moduleNameString
  116. instance Show PackageKey where
  117. show = show . packageKeyString
  118. instance Show Name where
  119. show = showOutputable . nameOccName
  120. -- deriving instance Show Name
  121. instance Show OccName where
  122. show = showOutputable
  123. instance Show Coercion where
  124. show _ = "<Coercion>"
  125. -- Instance for non-terms related stuff.
  126. deriving instance Show CoreToDo
  127. deriving instance Show SimplifierMode
  128. deriving instance Show CompilerPhase
  129. deriving instance Show FloatOutSwitches
  130. instance Show PluginPass where
  131. show _ = "PluginPass"