PageRenderTime 44ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/compiler/main/Annotations.hs

http://github.com/ghc/ghc
Haskell | 132 lines | 77 code | 22 blank | 33 comment | 1 complexity | 317300693484a2b7386916fdc5c61871 MD5 | raw file
Possible License(s): MIT, BSD-3-Clause, GPL-3.0
  1. -- |
  2. -- Support for source code annotation feature of GHC. That is the ANN pragma.
  3. --
  4. -- (c) The University of Glasgow 2006
  5. -- (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
  6. --
  7. module Annotations (
  8. -- * Main Annotation data types
  9. Annotation(..), AnnPayload,
  10. AnnTarget(..), CoreAnnTarget,
  11. getAnnTargetName_maybe,
  12. -- * AnnEnv for collecting and querying Annotations
  13. AnnEnv,
  14. mkAnnEnv, extendAnnEnvList, plusAnnEnv, emptyAnnEnv,
  15. findAnns, findAnnsByTypeRep,
  16. deserializeAnns
  17. ) where
  18. import Binary
  19. import Module ( Module )
  20. import Name
  21. import Outputable
  22. import GHC.Serialized
  23. import UniqFM
  24. import Unique
  25. import Control.Monad
  26. import Data.Maybe
  27. import Data.Typeable
  28. import Data.Word ( Word8 )
  29. -- | Represents an annotation after it has been sufficiently desugared from
  30. -- it's initial form of 'HsDecls.AnnDecl'
  31. data Annotation = Annotation {
  32. ann_target :: CoreAnnTarget, -- ^ The target of the annotation
  33. ann_value :: AnnPayload
  34. }
  35. type AnnPayload = Serialized -- ^ The "payload" of an annotation
  36. -- allows recovery of its value at a given type,
  37. -- and can be persisted to an interface file
  38. -- | An annotation target
  39. data AnnTarget name
  40. = NamedTarget name -- ^ We are annotating something with a name:
  41. -- a type or identifier
  42. | ModuleTarget Module -- ^ We are annotating a particular module
  43. -- | The kind of annotation target found in the middle end of the compiler
  44. type CoreAnnTarget = AnnTarget Name
  45. instance Functor AnnTarget where
  46. fmap f (NamedTarget nm) = NamedTarget (f nm)
  47. fmap _ (ModuleTarget mod) = ModuleTarget mod
  48. -- | Get the 'name' of an annotation target if it exists.
  49. getAnnTargetName_maybe :: AnnTarget name -> Maybe name
  50. getAnnTargetName_maybe (NamedTarget nm) = Just nm
  51. getAnnTargetName_maybe _ = Nothing
  52. instance Uniquable name => Uniquable (AnnTarget name) where
  53. getUnique (NamedTarget nm) = getUnique nm
  54. getUnique (ModuleTarget mod) = deriveUnique (getUnique mod) 0
  55. -- deriveUnique prevents OccName uniques clashing with NamedTarget
  56. instance Outputable name => Outputable (AnnTarget name) where
  57. ppr (NamedTarget nm) = text "Named target" <+> ppr nm
  58. ppr (ModuleTarget mod) = text "Module target" <+> ppr mod
  59. instance Binary name => Binary (AnnTarget name) where
  60. put_ bh (NamedTarget a) = do
  61. putByte bh 0
  62. put_ bh a
  63. put_ bh (ModuleTarget a) = do
  64. putByte bh 1
  65. put_ bh a
  66. get bh = do
  67. h <- getByte bh
  68. case h of
  69. 0 -> liftM NamedTarget $ get bh
  70. _ -> liftM ModuleTarget $ get bh
  71. instance Outputable Annotation where
  72. ppr ann = ppr (ann_target ann)
  73. -- | A collection of annotations
  74. -- Can't use a type synonym or we hit bug #2412 due to source import
  75. newtype AnnEnv = MkAnnEnv (UniqFM [AnnPayload])
  76. -- | An empty annotation environment.
  77. emptyAnnEnv :: AnnEnv
  78. emptyAnnEnv = MkAnnEnv emptyUFM
  79. -- | Construct a new annotation environment that contains the list of
  80. -- annotations provided.
  81. mkAnnEnv :: [Annotation] -> AnnEnv
  82. mkAnnEnv = extendAnnEnvList emptyAnnEnv
  83. -- | Add the given annotation to the environment.
  84. extendAnnEnvList :: AnnEnv -> [Annotation] -> AnnEnv
  85. extendAnnEnvList (MkAnnEnv env) anns
  86. = MkAnnEnv $ addListToUFM_C (++) env $
  87. map (\ann -> (getUnique (ann_target ann), [ann_value ann])) anns
  88. -- | Union two annotation environments.
  89. plusAnnEnv :: AnnEnv -> AnnEnv -> AnnEnv
  90. plusAnnEnv (MkAnnEnv env1) (MkAnnEnv env2) = MkAnnEnv $ plusUFM_C (++) env1 env2
  91. -- | Find the annotations attached to the given target as 'Typeable'
  92. -- values of your choice. If no deserializer is specified,
  93. -- only transient annotations will be returned.
  94. findAnns :: Typeable a => ([Word8] -> a) -> AnnEnv -> CoreAnnTarget -> [a]
  95. findAnns deserialize (MkAnnEnv ann_env)
  96. = (mapMaybe (fromSerialized deserialize))
  97. . (lookupWithDefaultUFM ann_env [])
  98. -- | Find the annotations attached to the given target as 'Typeable'
  99. -- values of your choice. If no deserializer is specified,
  100. -- only transient annotations will be returned.
  101. findAnnsByTypeRep :: AnnEnv -> CoreAnnTarget -> TypeRep -> [[Word8]]
  102. findAnnsByTypeRep (MkAnnEnv ann_env) target tyrep
  103. = [ ws | Serialized tyrep' ws <- lookupWithDefaultUFM ann_env [] target
  104. , tyrep' == tyrep ]
  105. -- | Deserialize all annotations of a given type. This happens lazily, that is
  106. -- no deserialization will take place until the [a] is actually demanded and
  107. -- the [a] can also be empty (the UniqFM is not filtered).
  108. deserializeAnns :: Typeable a => ([Word8] -> a) -> AnnEnv -> UniqFM [a]
  109. deserializeAnns deserialize (MkAnnEnv ann_env)
  110. = mapUFM (mapMaybe (fromSerialized deserialize)) ann_env