Another round of External Core fixes
[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 {-# OPTIONS -fno-warn-incomplete-patterns #-}
10 -- The above warning supression flag is a temporary kludge.
11 -- While working on this module you are encouraged to remove it and fix
12 -- any warnings in the module. See
13 --     http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
14 -- for details
15
16 module HsImpExp where
17
18 #include "HsVersions.h"
19
20 import Module           ( ModuleName )
21 import HsDoc            ( HsDoc )
22
23 import Outputable
24 import FastString
25 import SrcLoc           ( Located(..) )
26 import Char             ( isAlpha )
27 \end{code}
28
29 %************************************************************************
30 %*                                                                      *
31 \subsection{Import and export declaration lists}
32 %*                                                                      *
33 %************************************************************************
34
35 One per \tr{import} declaration in a module.
36 \begin{code}
37 type LImportDecl name = Located (ImportDecl name)
38
39 data ImportDecl name
40   = ImportDecl    (Located ModuleName)          -- module name
41                   Bool                          -- True <=> {-# SOURCE #-} import
42                   Bool                          -- True => qualified
43                   (Maybe ModuleName)            -- as Module
44                   (Maybe (Bool, [LIE name]))    -- (True => hiding, names)
45 \end{code}
46
47 \begin{code}
48 instance (Outputable name) => Outputable (ImportDecl name) where
49     ppr (ImportDecl mod from qual as spec)
50       = hang (hsep [ptext SLIT("import"), ppr_imp from, 
51                     pp_qual qual, ppr mod, pp_as as])
52              4 (pp_spec spec)
53       where
54         pp_qual False   = empty
55         pp_qual True    = ptext SLIT("qualified")
56
57         pp_as Nothing   = empty
58         pp_as (Just a)  = ptext SLIT("as ") <+> ppr a
59
60         ppr_imp True  = ptext SLIT("{-# SOURCE #-}")
61         ppr_imp False = empty
62
63         pp_spec Nothing = empty
64         pp_spec (Just (False, spec))
65                         = parens (interpp'SP spec)
66         pp_spec (Just (True, spec))
67                         = ptext SLIT("hiding") <+> parens (interpp'SP spec)
68
69 ideclName :: ImportDecl name -> Located ModuleName
70 ideclName (ImportDecl mod_nm _ _ _ _) = mod_nm
71 \end{code}
72
73 %************************************************************************
74 %*                                                                      *
75 \subsection{Imported and exported entities}
76 %*                                                                      *
77 %************************************************************************
78
79 \begin{code}
80 type LIE name = Located (IE name)
81
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 (HsDoc name) -- Doc section heading
89   | IEDoc               (HsDoc name)     -- Some documentation
90   | IEDocNamed          String           -- Reference to named doc
91 \end{code}
92
93 \begin{code}
94 ieName :: IE name -> name
95 ieName (IEVar n)         = n
96 ieName (IEThingAbs  n)   = n
97 ieName (IEThingWith n _) = n
98 ieName (IEThingAll  n)   = n
99
100 ieNames :: IE a -> [a]
101 ieNames (IEVar            n   ) = [n]
102 ieNames (IEThingAbs       n   ) = [n]
103 ieNames (IEThingAll       n   ) = [n]
104 ieNames (IEThingWith      n ns) = n:ns
105 ieNames (IEModuleContents _   ) = []
106 ieNames (IEGroup          _ _ ) = []
107 ieNames (IEDoc            _   ) = []
108 ieNames (IEDocNamed       _   ) = []        
109 \end{code}
110
111 \begin{code}
112 instance (Outputable name) => Outputable (IE name) where
113     ppr (IEVar          var)    = pprHsVar var
114     ppr (IEThingAbs     thing)  = ppr thing
115     ppr (IEThingAll     thing)  = hcat [ppr thing, text "(..)"]
116     ppr (IEThingWith thing withs)
117         = ppr thing <> parens (fsep (punctuate comma (map pprHsVar withs)))
118     ppr (IEModuleContents mod)
119         = ptext SLIT("module") <+> ppr mod
120     ppr (IEGroup n _)           = text ("<IEGroup: " ++ (show n) ++ ">")
121     ppr (IEDoc doc)             = ppr doc
122     ppr (IEDocNamed string)     = text ("<IEDocNamed: " ++ string ++ ">")
123 \end{code}
124
125 \begin{code}
126 pprHsVar :: Outputable name => name -> SDoc
127 pprHsVar v | isOperator ppr_v = parens ppr_v
128            | otherwise        = ppr_v
129            where
130              ppr_v = ppr v
131
132 isOperator :: SDoc -> Bool
133 isOperator ppr_v 
134   = case showSDocUnqual ppr_v of
135         ('(':_)   -> False              -- (), (,) etc
136         ('[':_)   -> False              -- []
137         ('$':c:_) -> not (isAlpha c)    -- Don't treat $d as an operator
138         (':':c:_) -> not (isAlpha c)    -- Don't treat :T as an operator
139         ('_':_)   -> False              -- Not an operator
140         (c:_)     -> not (isAlpha c)    -- Starts with non-alpha
141         _         -> False
142     -- We use (showSDoc (ppr v)), rather than isSymOcc (getOccName v) simply so
143     -- that we don't need NamedThing in the context of all these functions.
144     -- Gruesome, but simple.
145 \end{code}
146