PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/compiler/main/Annotations.hs

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