Fix CodingStyle#Warnings URLs
[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 -w #-}
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 mod_nm _ _ _ _) = mod_nm
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 data IE name
82   = IEVar               name
83   | IEThingAbs          name             -- Class/Type (can't tell)
84   | IEThingAll          name             -- Class/Type plus all methods/constructors
85   | IEThingWith         name [name]      -- Class/Type plus some methods/constructors
86   | IEModuleContents    ModuleName       -- (Export Only)
87   | IEGroup             Int (HsDoc name) -- Doc section heading
88   | IEDoc               (HsDoc name)     -- Some documentation
89   | IEDocNamed          String           -- Reference to named doc
90 \end{code}
91
92 \begin{code}
93 ieName :: IE name -> name
94 ieName (IEVar n)         = n
95 ieName (IEThingAbs  n)   = n
96 ieName (IEThingWith n _) = n
97 ieName (IEThingAll  n)   = n
98
99 ieNames :: IE a -> [a]
100 ieNames (IEVar            n   ) = [n]
101 ieNames (IEThingAbs       n   ) = [n]
102 ieNames (IEThingAll       n   ) = [n]
103 ieNames (IEThingWith      n ns) = n:ns
104 ieNames (IEModuleContents _   ) = []
105 ieNames (IEGroup          _ _ ) = []
106 ieNames (IEDoc            _   ) = []
107 ieNames (IEDocNamed       _   ) = []        
108 \end{code}
109
110 \begin{code}
111 instance (Outputable name) => Outputable (IE name) where
112     ppr (IEVar          var)    = pprHsVar var
113     ppr (IEThingAbs     thing)  = ppr thing
114     ppr (IEThingAll     thing)  = hcat [ppr thing, text "(..)"]
115     ppr (IEThingWith thing withs)
116         = ppr thing <> parens (fsep (punctuate comma (map pprHsVar withs)))
117     ppr (IEModuleContents mod)
118         = ptext SLIT("module") <+> ppr mod
119     ppr (IEGroup n doc)         = text ("<IEGroup: " ++ (show n) ++ ">") 
120     ppr (IEDoc doc)             = ppr doc
121     ppr (IEDocNamed string)     = text ("<IEDocNamed: " ++ string ++ ">")
122 \end{code}
123
124 \begin{code}
125 pprHsVar :: Outputable name => name -> SDoc
126 pprHsVar v | isOperator ppr_v = parens ppr_v
127            | otherwise        = ppr_v
128            where
129              ppr_v = ppr v
130
131 isOperator :: SDoc -> Bool
132 isOperator ppr_v 
133   = case showSDocUnqual ppr_v of
134         ('(':s)   -> False              -- (), (,) etc
135         ('[':s)   -> False              -- []
136         ('$':c:s) -> not (isAlpha c)    -- Don't treat $d as an operator
137         (':':c:s) -> not (isAlpha c)    -- Don't treat :T as an operator
138         ('_':s)   -> False              -- Not an operator
139         (c:s)     -> not (isAlpha c)    -- Starts with non-alpha
140         other     -> False
141     -- We use (showSDoc (ppr v)), rather than isSymOcc (getOccName v) simply so
142     -- that we don't need NamedThing in the context of all these functions.
143     -- Gruesome, but simple.
144 \end{code}
145