[project @ 1996-03-19 08:58:34 by partain]
[ghc-hetmet.git] / ghc / compiler / hsSyn / HsImpExp.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1996
3 %
4 \section[HsImpExp]{Abstract syntax: imports, exports, interfaces}
5
6 \begin{code}
7 #include "HsVersions.h"
8
9 module HsImpExp where
10
11 import Ubiq{-uitous-}
12
13 -- friends:
14 import HsDecls          ( FixityDecl, TyDecl, ClassDecl, InstDecl )
15 import HsBinds          ( Sig )
16
17 -- others:
18 import Outputable
19 import PprStyle         ( PprStyle(..) )
20 import Pretty
21 import SrcLoc           ( SrcLoc{-instances-} )
22 \end{code}
23
24 %************************************************************************
25 %*                                                                      *
26 \subsection{Import and export declaration lists}
27 %*                                                                      *
28 %************************************************************************
29
30 One per \tr{import} declaration in a module.
31 \begin{code}
32 data ImportedInterface tyvar uvar name pat
33   = ImportMod     (Interface tyvar uvar name pat)
34                   Bool                          -- qualified?
35                   (Maybe FAST_STRING)           -- as Modid
36                   (Maybe (Bool, [IE name]))     -- (hiding?, names)
37 \end{code}
38
39 \begin{code}
40 instance (NamedThing name, Outputable name, Outputable pat,
41           Eq tyvar, Outputable tyvar, Eq uvar, Outputable uvar)
42            => Outputable (ImportedInterface tyvar uvar name pat) where
43
44     ppr sty (ImportMod iface qual as spec)
45       = ppAbove (ppHang (ppCat [ppStr "import", pp_qual qual, ppr PprForUser iface, pp_as as])
46                       4 (pp_spec spec))
47                 (case sty of {PprForUser -> ppNil; _ -> ppr sty iface})
48       where
49         pp_qual False   = ppNil
50         pp_qual True    = ppStr "qualified"
51
52         pp_as Nothing   = ppNil
53         pp_as (Just a)  = ppCat [ppStr "as", ppPStr a]
54
55         pp_spec Nothing = ppNil
56         pp_spec (Just (False, spec))
57                         = ppBesides [ppStr "(", interpp'SP sty spec, ppStr ")"]
58         pp_spec (Just (True, spec))
59                         = ppBesides [ppStr "hiding (", interpp'SP sty spec, ppStr ")"]
60
61 \end{code}
62
63 %************************************************************************
64 %*                                                                      *
65 \subsection{Imported and exported entities}
66 %*                                                                      *
67 %************************************************************************
68 \begin{code}
69 data IE name
70   = IEVar               name
71   | IEThingAbs          name            -- Constructor/Type/Class (can't tell)
72   | IEThingAll          name            -- Class/Type plus all methods/constructors
73   | IEThingWith         name [name]     -- Class/Type plus some methods/constructors
74   | IEModuleContents    FAST_STRING     -- (Export Only)
75 \end{code}
76
77 \begin{code}
78 instance (Outputable name) => Outputable (IE name) where
79     ppr sty (IEVar      var)    = ppr sty var
80     ppr sty (IEThingAbs thing)  = ppr sty thing
81     ppr sty (IEThingAll thing)
82         = ppBesides [ppr sty thing, ppStr "(..)"]
83     ppr sty (IEThingWith thing withs)
84         = ppBesides [ppr sty thing, ppLparen, ppInterleave ppComma (map (ppr sty) withs), ppRparen]
85     ppr sty (IEModuleContents mod)
86         = ppBeside (ppPStr SLIT("module ")) (ppPStr mod)
87 \end{code}
88
89 %************************************************************************
90 %*                                                                      *
91 \subsection{Interfaces}
92 %*                                                                      *
93 %************************************************************************
94
95 \begin{code}
96 data Interface tyvar uvar name pat
97   = Interface   FAST_STRING                     -- module name
98                 [IfaceImportDecl name]
99                 [FixityDecl name]
100                 [TyDecl name]                   -- data decls may have no constructors
101                 [ClassDecl tyvar uvar name pat] -- without default methods
102                 [InstDecl  tyvar uvar name pat] -- without method defns
103                 [Sig name]
104                 SrcLoc
105 \end{code}
106
107 \begin{code}
108 instance (NamedThing name, Outputable name, Outputable pat,
109           Eq tyvar, Outputable tyvar, Eq uvar, Outputable uvar)
110              => Outputable (Interface tyvar uvar name pat) where
111
112     ppr PprForUser (Interface name _ _ _ _ _ _ _) = ppPStr name
113
114     ppr sty (Interface name iimpdecls fixities tydecls classdecls instdecls sigs anns)
115       = ppAboves [ppStr "{-",
116                   ifPprShowAll sty (ppr sty anns),
117                   ppCat [ppStr "interface", ppPStr name, ppStr "where"],
118                   ppNest 4 (ppAboves [
119                       pp_nonnull iimpdecls,
120                       pp_nonnull fixities,
121                       pp_nonnull tydecls,
122                       pp_nonnull classdecls,
123                       pp_nonnull instdecls,
124                       pp_nonnull sigs]),
125                   ppStr "-}"]
126       where
127         pp_nonnull [] = ppNil
128         pp_nonnull xs = ppAboves (map (ppr sty) xs)
129 \end{code}
130
131 \begin{code}
132 data IfaceImportDecl name
133   = IfaceImportDecl FAST_STRING     -- module we're being told about
134                     [IE name]       -- things we're being told about
135                     SrcLoc
136 \end{code}
137
138 \begin{code}
139 instance Outputable name => Outputable (IfaceImportDecl name) where
140
141     ppr sty (IfaceImportDecl mod names src_loc)
142       = ppHang (ppCat [ppPStr SLIT("import"), ppPStr mod, ppLparen])
143              4 (ppSep [ppCat [interpp'SP sty names, ppRparen]])
144 \end{code}