PageRenderTime 34ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/ghc-7.0.4/compiler/main/Annotations.lhs

http://picorec.googlecode.com/
Haskell | 101 lines | 63 code | 20 blank | 18 comment | 0 complexity | 93dfae0842622db118df10e2abfeccb4 MD5 | raw file
Possible License(s): BSD-3-Clause, BSD-2-Clause
  1. %
  2. % (c) The University of Glasgow 2006
  3. % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
  4. %
  5. \begin{code}
  6. module Annotations (
  7. -- * Main Annotation data types
  8. Annotation(..),
  9. AnnTarget(..), CoreAnnTarget,
  10. getAnnTargetName_maybe,
  11. -- * AnnEnv for collecting and querying Annotations
  12. AnnEnv,
  13. mkAnnEnv, extendAnnEnvList, plusAnnEnv, emptyAnnEnv, findAnns,
  14. deserializeAnns
  15. ) where
  16. import Name
  17. import Module ( Module )
  18. import Outputable
  19. import UniqFM
  20. import Serialized
  21. import Unique
  22. import Data.Typeable
  23. import Data.Maybe
  24. import Data.Word ( Word8 )
  25. -- | Represents an annotation after it has been sufficiently desugared from
  26. -- it's initial form of 'HsDecls.AnnDecl'
  27. data Annotation = Annotation {
  28. ann_target :: CoreAnnTarget, -- ^ The target of the annotation
  29. ann_value :: Serialized -- ^ 'Serialized' version of the annotation that
  30. -- allows recovery of its value or can
  31. -- be persisted to an interface file
  32. }
  33. -- | An annotation target
  34. data AnnTarget name
  35. = NamedTarget name -- ^ We are annotating something with a name:
  36. -- a type or identifier
  37. | ModuleTarget Module -- ^ We are annotating a particular module
  38. -- | The kind of annotation target found in the middle end of the compiler
  39. type CoreAnnTarget = AnnTarget Name
  40. instance Functor AnnTarget where
  41. fmap f (NamedTarget nm) = NamedTarget (f nm)
  42. fmap _ (ModuleTarget mod) = ModuleTarget mod
  43. getAnnTargetName_maybe :: AnnTarget name -> Maybe name
  44. getAnnTargetName_maybe (NamedTarget nm) = Just nm
  45. getAnnTargetName_maybe _ = Nothing
  46. instance Uniquable name => Uniquable (AnnTarget name) where
  47. getUnique (NamedTarget nm) = getUnique nm
  48. getUnique (ModuleTarget mod) = deriveUnique (getUnique mod) 0
  49. -- deriveUnique prevents OccName uniques clashing with NamedTarget
  50. instance Outputable name => Outputable (AnnTarget name) where
  51. ppr (NamedTarget nm) = text "Named target" <+> ppr nm
  52. ppr (ModuleTarget mod) = text "Module target" <+> ppr mod
  53. instance Outputable Annotation where
  54. ppr ann = ppr (ann_target ann)
  55. -- | A collection of annotations
  56. newtype AnnEnv = MkAnnEnv (UniqFM [Serialized])
  57. -- Can't use a type synonym or we hit bug #2412 due to source import
  58. emptyAnnEnv :: AnnEnv
  59. emptyAnnEnv = MkAnnEnv emptyUFM
  60. mkAnnEnv :: [Annotation] -> AnnEnv
  61. mkAnnEnv = extendAnnEnvList emptyAnnEnv
  62. extendAnnEnvList :: AnnEnv -> [Annotation] -> AnnEnv
  63. extendAnnEnvList (MkAnnEnv env) anns
  64. = MkAnnEnv $ addListToUFM_C (++) env $
  65. map (\ann -> (getUnique (ann_target ann), [ann_value ann])) anns
  66. plusAnnEnv :: AnnEnv -> AnnEnv -> AnnEnv
  67. plusAnnEnv (MkAnnEnv env1) (MkAnnEnv env2) = MkAnnEnv $ plusUFM_C (++) env1 env2
  68. -- | Find the annotations attached to the given target as 'Typeable'
  69. -- values of your choice. If no deserializer is specified,
  70. -- only transient annotations will be returned.
  71. findAnns :: Typeable a => ([Word8] -> a) -> AnnEnv -> CoreAnnTarget -> [a]
  72. findAnns deserialize (MkAnnEnv ann_env)
  73. = (mapMaybe (fromSerialized deserialize))
  74. . (lookupWithDefaultUFM ann_env [])
  75. -- | Deserialize all annotations of a given type. This happens lazily, that is
  76. -- no deserialization will take place until the [a] is actually demanded and
  77. -- the [a] can also be empty (the UniqFM is not filtered).
  78. deserializeAnns :: Typeable a => ([Word8] -> a) -> AnnEnv -> UniqFM [a]
  79. deserializeAnns deserialize (MkAnnEnv ann_env)
  80. = mapUFM (mapMaybe (fromSerialized deserialize)) ann_env
  81. \end{code}