[project @ 1996-01-08 20:28:12 by partain]
[ghc-hetmet.git] / ghc / compiler / abstractSyn / HsImpExp.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1992-1995
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 FiniteMap
12 import HsDecls          ( FixityDecl, TyDecl, ClassDecl, InstDecl )
13 import HsBinds          ( Sig )
14 import HsPat            ( ProtoNamePat(..), RenamedPat(..), InPat )
15 import Id               ( Id )
16 import Name             ( Name )
17 import Outputable
18 import Pretty
19 import ProtoName        ( ProtoName(..) ) -- .. for pragmas only
20 import SrcLoc           ( SrcLoc )
21 import Unique           ( Unique )
22 import Util             -- pragmas only
23 \end{code}
24
25 %************************************************************************
26 %*                                                                      *
27 \subsection[AbsSyn-ImpExpDecls]{Import and export declaration lists}
28 %*                                                                      *
29 %************************************************************************
30
31 One per \tr{import} declaration in a module.
32 \begin{code}
33 data ImportedInterface name pat
34   = ImportAll     (Interface name pat)  -- the contents of the interface
35                                         -- (incl module name)
36                   [Renaming]
37
38   | ImportSome    (Interface name pat)
39                   [IE]          -- the only things being imported
40                   [Renaming]
41
42   | ImportButHide (Interface name pat)
43                   [IE]          -- import everything "but hide" these entities
44                   [Renaming]
45 \end{code}
46
47 Synonyms:
48 \begin{code}
49 type ProtoNameImportedInterface = ImportedInterface ProtoName ProtoNamePat
50 type RenamedImportedInterface   = ImportedInterface Name      RenamedPat
51 \end{code}
52
53 \begin{code}
54 instance (NamedThing name, Outputable name,
55           NamedThing pat, Outputable pat)
56            => Outputable (ImportedInterface name pat) where
57
58     ppr sty (ImportAll iface renamings)
59       = ppAbove (ppCat [ppStr "import", ppr sty iface])
60                 (pprRenamings sty renamings)
61
62     ppr sty (ImportSome iface imports renamings)
63       = ppAboves [ppCat [ppStr "import", ppr sty iface],
64                   ppNest 8 (ppBesides [ppStr " (", interpp'SP sty imports, ppStr ") "]),
65                   pprRenamings sty renamings]
66
67     ppr sty (ImportButHide iface imports renamings)
68       = ppAboves [ppCat [ppStr "import", ppr sty iface],
69                   ppNest 8 (ppBesides [ppStr "hiding (", interpp'SP sty imports, ppStr ") "]),
70                   pprRenamings sty renamings]
71 \end{code}
72
73 %************************************************************************
74 %*                                                                      *
75 \subsection[AbsSyn-entities]{Imported and exported entities}
76 %*                                                                      *
77 %************************************************************************
78 \begin{code}
79 data IE
80   = IEVar               FAST_STRING
81   | IEThingAbs          FAST_STRING     -- Constructor/Type/Class (can't tell)
82   | IEThingAll          FAST_STRING     -- Class/Type plus all methods/constructors
83   | IEConWithCons       FAST_STRING     -- import tycon w/ some cons
84                         [FAST_STRING]
85   | IEClsWithOps        FAST_STRING     -- import tycls w/ some methods
86                         [FAST_STRING]
87   | IEModuleContents    FAST_STRING     -- (Export Only)
88 \end{code}
89
90 \begin{code}
91 instance Outputable IE where
92     ppr sty (IEVar      var)    = ppPStr var
93     ppr sty (IEThingAbs thing)  = ppPStr thing
94     ppr sty (IEThingAll thing)  = ppBesides [ppPStr thing, ppStr "(..)"]
95     ppr sty (IEConWithCons tycon datacons)
96       = ppBesides [ppPStr tycon, ppLparen, ppInterleave ppComma (map ppPStr datacons), ppRparen]
97     ppr sty (IEClsWithOps cls methods)
98       = ppBesides [ppPStr cls, ppLparen, ppInterleave ppComma (map ppPStr methods), ppRparen]
99     ppr sty (IEModuleContents mod) = ppBesides [ppPStr mod, ppStr ".."]
100 \end{code}
101
102 We want to know what names are exported (the first list of the result)
103 and what modules are exported (the second list of the result).
104 \begin{code}
105 type ImExportListInfo
106   = ( FiniteMap FAST_STRING ExportFlag,
107                         -- Assoc list of im/exported things &
108                         -- their "export" flags (im/exported
109                         -- abstractly, concretely, etc.)
110                         -- Hmm... slight misnomer there (WDP 95/02)
111       FiniteSet FAST_STRING )
112                         -- List of modules to be exported
113                         -- entirely; NB: *not* everything with
114                         -- original names in these modules;
115                         -- but: everything that these modules'
116                         -- interfaces told us about.
117                         -- Note: This latter component can
118                         -- only arise on export lists.
119
120 getIEStrings    :: [IE] -> ImExportListInfo
121 getRawIEStrings :: [IE] -> ([(FAST_STRING, ExportFlag)], [FAST_STRING])
122   -- "Raw" gives the raw lists of things; we need this for
123   -- checking for duplicates.
124
125 getIEStrings exps
126   = case (getRawIEStrings exps) of { (pairs, mods) ->
127     (listToFM pairs, mkSet mods) }
128
129 getRawIEStrings exps
130   = foldr do_one ([],[]) exps
131   where
132     do_one (IEVar n) (prs, mods) 
133      = ((n, ExportAll):prs, mods)
134     do_one (IEThingAbs n) (prs, mods) 
135      = ((n, ExportAbs):prs, mods)
136     do_one (IEThingAll n) (prs, mods) 
137      = ((n, ExportAll):prs, mods)
138     do_one (IEConWithCons n ns) (prs, mods) -- needn't do anything
139      = ((n, ExportAll):prs, mods)           -- with the indiv cons/ops
140     do_one (IEClsWithOps n ns) (prs, mods) 
141      = ((n, ExportAll):prs, mods)
142     do_one (IEModuleContents n) (prs, mods)  
143      = (prs, n : mods)
144 \end{code}
145
146 %************************************************************************
147 %*                                                                      *
148 \subsection[AbsSyn-Renaming]{Renamings}
149 %*                                                                      *
150 %************************************************************************
151
152 \begin{code}
153 data Renaming = MkRenaming FAST_STRING FAST_STRING
154 \end{code}
155
156 \begin{code}
157 pprRenamings :: PprStyle -> [Renaming] -> Pretty
158 pprRenamings sty [] = ppNil
159 pprRenamings sty rs = ppBesides [ppStr "renaming (", interpp'SP sty rs, ppStr ")"]
160 \end{code}
161
162 \begin{code}
163 instance Outputable Renaming where
164     ppr sty (MkRenaming from too) = ppCat [ppPStr from, ppStr "to", ppPStr too]
165 \end{code}
166
167 %************************************************************************
168 %*                                                                      *
169 \subsection[AbsSyn-Interface]{Interfaces}
170 %*                                                                      *
171 %************************************************************************
172
173 \begin{code}
174 data Interface name pat
175   = MkInterface FAST_STRING                     -- module name
176                 [IfaceImportDecl]
177                 [FixityDecl name]       -- none yet (ToDo)
178                 [TyDecl name]           -- data decls may have no constructors
179                 [ClassDecl name pat]    -- Without default methods
180                 [InstDecl  name pat]    -- Without method defns
181                 [Sig name]
182                 SrcLoc
183 \end{code}
184
185 \begin{code}
186 type ProtoNameInterface = Interface ProtoName ProtoNamePat
187 type RenamedInterface = Interface Name RenamedPat
188 \end{code}
189
190 \begin{code}
191 instance (NamedThing name, Outputable name,
192            NamedThing pat, Outputable pat)
193              => Outputable (Interface name pat) where
194
195     ppr PprForUser (MkInterface name _ _ _ _ _ _ _) = ppPStr name
196
197     ppr sty (MkInterface name iimpdecls fixities tydecls classdecls instdecls sigs anns)
198       = ppHang (ppBeside (ppPStr name) (ppStr " {-"))
199              4 (ppAboves [
200                   ifPprShowAll sty (ppr sty anns),
201                   ppCat [ppStr "interface", ppPStr name, ppStr "where"],
202                   ppNest 4 (ppAboves [
203                       ppr sty iimpdecls,        ppr sty fixities,
204                       ppr sty tydecls,  ppr sty classdecls,
205                       ppr sty instdecls,  ppr sty sigs]),
206                   ppStr "-}"])
207 \end{code}
208
209 \begin{code}
210 data IfaceImportDecl
211   = IfaceImportDecl FAST_STRING     -- module we're being told about
212                     [IE]            -- things we're being told about
213                     [Renaming]      -- AAYYYYEEEEEEEEEE!!! (help)
214                     SrcLoc
215 \end{code}
216
217 \begin{code}
218 instance Outputable IfaceImportDecl where
219
220     ppr sty (IfaceImportDecl mod names renamings src_loc)
221       = ppHang (ppCat [ppStr "import", ppPStr mod, ppLparen])
222              4 (ppSep [ppCat [interpp'SP sty names, ppRparen],
223                        pprRenamings sty renamings])
224 \end{code}
225
226