merge GHC HEAD
[ghc-hetmet.git] / compiler / hsSyn / HsImpExp.lhs
1 %
2 % (c) The University of Glasgow 2006
3 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1998
4 %
5
6 HsImpExp: Abstract syntax: imports, exports, interfaces
7
8 \begin{code}
9 {-# LANGUAGE DeriveDataTypeable #-}
10
11 module HsImpExp where
12
13 import Module           ( ModuleName )
14 import HsDoc            ( HsDocString )
15
16 import Outputable
17 import FastString
18 import SrcLoc           ( Located(..) )
19
20 import Data.Data
21 \end{code}
22
23 %************************************************************************
24 %*                                                                      *
25 \subsection{Import and export declaration lists}
26 %*                                                                      *
27 %************************************************************************
28
29 One per \tr{import} declaration in a module.
30 \begin{code}
31 type LImportDecl name = Located (ImportDecl name)
32
33 -- | A single Haskell @import@ declaration.
34 data ImportDecl name
35   = ImportDecl {
36       ideclName      :: Located ModuleName, -- ^ Module name.
37       ideclPkgQual   :: Maybe FastString,   -- ^ Package qualifier.
38       ideclSource    :: Bool,               -- ^ True <=> {-# SOURCE #-} import
39       ideclQualified :: Bool,               -- ^ True => qualified
40       ideclAs        :: Maybe ModuleName,   -- ^ as Module
41       ideclHiding    :: Maybe (Bool, [LIE name]) -- ^ (True => hiding, names)
42     } deriving (Data, Typeable)
43 \end{code}
44
45 \begin{code}
46 instance (Outputable name) => Outputable (ImportDecl name) where
47     ppr (ImportDecl mod pkg from qual as spec)
48       = hang (hsep [ptext (sLit "import"), ppr_imp from, 
49                     pp_qual qual, pp_pkg pkg, ppr mod, pp_as as])
50              4 (pp_spec spec)
51       where
52         pp_pkg Nothing  = empty
53         pp_pkg (Just p) = doubleQuotes (ftext p)
54
55         pp_qual False   = empty
56         pp_qual True    = ptext (sLit "qualified")
57
58         pp_as Nothing   = empty
59         pp_as (Just a)  = ptext (sLit "as") <+> ppr a
60
61         ppr_imp True  = ptext (sLit "{-# SOURCE #-}")
62         ppr_imp False = empty
63
64         pp_spec Nothing             = empty
65         pp_spec (Just (False, ies)) = ppr_ies ies
66         pp_spec (Just (True,  ies)) = ptext (sLit "hiding") <+> ppr_ies ies
67
68         ppr_ies []  = ptext (sLit "()")
69         ppr_ies ies = char '(' <+> interpp'SP ies <+> char ')'
70 \end{code}
71
72 %************************************************************************
73 %*                                                                      *
74 \subsection{Imported and exported entities}
75 %*                                                                      *
76 %************************************************************************
77
78 \begin{code}
79 type LIE name = Located (IE name)
80
81 -- | Imported or exported entity.
82 data IE name
83   = IEVar               name
84   | IEThingAbs          name             -- ^ Class/Type (can't tell)
85   | IEThingAll          name             -- ^ Class/Type plus all methods/constructors
86   | IEThingWith         name [name]      -- ^ Class/Type plus some methods/constructors
87   | IEModuleContents    ModuleName       -- ^ (Export Only)
88   | IEGroup             Int HsDocString  -- ^ Doc section heading
89   | IEDoc               HsDocString      -- ^ Some documentation
90   | IEDocNamed          String           -- ^ Reference to named doc
91   deriving (Data, Typeable)
92 \end{code}
93
94 \begin{code}
95 ieName :: IE name -> name
96 ieName (IEVar n)         = n
97 ieName (IEThingAbs  n)   = n
98 ieName (IEThingWith n _) = n
99 ieName (IEThingAll  n)   = n
100 ieName _ = panic "ieName failed pattern match!"
101
102 ieNames :: IE a -> [a]
103 ieNames (IEVar            n   ) = [n]
104 ieNames (IEThingAbs       n   ) = [n]
105 ieNames (IEThingAll       n   ) = [n]
106 ieNames (IEThingWith      n ns) = n:ns
107 ieNames (IEModuleContents _   ) = []
108 ieNames (IEGroup          _ _ ) = []
109 ieNames (IEDoc            _   ) = []
110 ieNames (IEDocNamed       _   ) = []        
111 \end{code}
112
113 \begin{code}
114 instance (Outputable name) => Outputable (IE name) where
115     ppr (IEVar          var)    = pprHsVar var
116     ppr (IEThingAbs     thing)  = ppr thing
117     ppr (IEThingAll     thing)  = hcat [ppr thing, text "(..)"]
118     ppr (IEThingWith thing withs)
119         = ppr thing <> parens (fsep (punctuate comma (map pprHsVar withs)))
120     ppr (IEModuleContents mod')
121         = ptext (sLit "module") <+> ppr mod'
122     ppr (IEGroup n _)           = text ("<IEGroup: " ++ (show n) ++ ">")
123     ppr (IEDoc doc)             = ppr doc
124     ppr (IEDocNamed string)     = text ("<IEDocNamed: " ++ string ++ ">")
125 \end{code}
126
127