[project @ 2002-09-13 15:02:25 by simonpj]
[ghc-hetmet.git] / ghc / compiler / rename / RnHsSyn.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1996-1998
3 %
4 \section[RnHsSyn]{Specialisations of the @HsSyn@ syntax for the renamer}
5
6 \begin{code}
7 module RnHsSyn where
8
9 #include "HsVersions.h"
10
11 import HsSyn
12 import HsCore
13 import Class            ( FunDep, DefMeth(..) )
14 import TyCon            ( visibleDataCons, tyConName )
15 import TysWiredIn       ( tupleTyCon, listTyCon, parrTyCon, charTyCon )
16 import Name             ( Name, getName, isTyVarName )
17 import NameSet
18 import BasicTypes       ( Boxity, FixitySig )
19 import Outputable
20 \end{code}
21
22
23 \begin{code}
24 type RenamedHsDecl              = HsDecl                Name
25 type RenamedArithSeqInfo        = ArithSeqInfo          Name
26 type RenamedClassOpSig          = Sig                   Name
27 type RenamedConDecl             = ConDecl               Name
28 type RenamedContext             = HsContext             Name
29 type RenamedRuleDecl            = RuleDecl              Name
30 type RenamedTyClDecl            = TyClDecl              Name
31 type RenamedDefaultDecl         = DefaultDecl           Name
32 type RenamedForeignDecl         = ForeignDecl           Name
33 type RenamedCoreDecl            = CoreDecl              Name
34 type RenamedGRHS                = GRHS                  Name
35 type RenamedGRHSs               = GRHSs                 Name
36 type RenamedHsBinds             = HsBinds               Name
37 type RenamedHsExpr              = HsExpr                Name
38 type RenamedInstDecl            = InstDecl              Name
39 type RenamedMatchContext        = HsMatchContext        Name
40 type RenamedMatch               = Match                 Name
41 type RenamedMonoBinds           = MonoBinds             Name
42 type RenamedPat                 = InPat                 Name
43 type RenamedHsType              = HsType                Name
44 type RenamedHsPred              = HsPred                Name
45 type RenamedRecordBinds         = HsRecordBinds         Name
46 type RenamedSig                 = Sig                   Name
47 type RenamedStmt                = Stmt                  Name
48 type RenamedFixitySig           = FixitySig             Name
49 type RenamedDeprecation         = DeprecDecl            Name
50 \end{code}
51
52 %************************************************************************
53 %*                                                                      *
54 \subsection{Free variables}
55 %*                                                                      *
56 %************************************************************************
57
58 These free-variable finders returns tycons and classes too.
59
60 \begin{code}
61 charTyCon_name, listTyCon_name, parrTyCon_name :: Name
62 charTyCon_name    = getName charTyCon
63 listTyCon_name    = getName listTyCon
64 parrTyCon_name    = getName parrTyCon
65
66 tupleTyCon_name :: Boxity -> Int -> Name
67 tupleTyCon_name boxity n = getName (tupleTyCon boxity n)
68
69 extractHsTyVars :: RenamedHsType -> NameSet
70 extractHsTyVars x = filterNameSet isTyVarName (extractHsTyNames x)
71
72 extractFunDepNames :: FunDep Name -> NameSet
73 extractFunDepNames (ns1, ns2) = mkNameSet ns1 `unionNameSets` mkNameSet ns2
74
75 extractHsTyNames   :: RenamedHsType -> NameSet
76 extractHsTyNames ty
77   = get ty
78   where
79     get (HsAppTy ty1 ty2)      = get ty1 `unionNameSets` get ty2
80     get (HsListTy ty)          = unitNameSet listTyCon_name `unionNameSets` get ty
81     get (HsPArrTy ty)          = unitNameSet parrTyCon_name `unionNameSets` get ty
82     get (HsTupleTy con tys)    = hsTupConFVs con `unionNameSets` extractHsTyNames_s tys
83     get (HsFunTy ty1 ty2)      = get ty1 `unionNameSets` get ty2
84     get (HsPredTy p)           = extractHsPredTyNames p
85     get (HsOpTy ty1 tycon ty2) = get ty1 `unionNameSets` get ty2 `unionNameSets`
86                                  case tycon of { HsTyOp n -> unitNameSet n ; 
87                                                  HsArrow  -> emptyNameSet }
88     get (HsParTy ty)           = get ty
89     get (HsNumTy n)            = emptyNameSet
90     get (HsTyVar tv)           = unitNameSet tv
91     get (HsKindSig ty k)       = get ty
92     get (HsForAllTy (Just tvs) 
93                     ctxt ty)   = (extractHsCtxtTyNames ctxt `unionNameSets` get ty)
94                                             `minusNameSet`
95                                   mkNameSet (hsTyVarNames tvs)
96     get ty@(HsForAllTy Nothing _ _) = pprPanic "extractHsTyNames" (ppr ty)
97
98 extractHsTyNames_s  :: [RenamedHsType] -> NameSet
99 extractHsTyNames_s tys = foldr (unionNameSets . extractHsTyNames) emptyNameSet tys
100
101 extractHsCtxtTyNames :: RenamedContext -> NameSet
102 extractHsCtxtTyNames ctxt = foldr (unionNameSets . extractHsPredTyNames) emptyNameSet ctxt
103
104 -- You don't import or export implicit parameters,
105 -- so don't mention the IP names
106 extractHsPredTyNames (HsClassP cls tys)
107   = unitNameSet cls `unionNameSets` extractHsTyNames_s tys
108 extractHsPredTyNames (HsIParam n ty)
109   = extractHsTyNames ty
110 \end{code}
111
112
113 %************************************************************************
114 %*                                                                      *
115 \subsection{Free variables of declarations}
116 %*                                                                      *
117 %************************************************************************
118
119 Return the Names that must be in scope if we are to use this declaration.
120 In all cases this is set up for interface-file declarations:
121         - for class decls we ignore the bindings
122         - for instance decls likewise, plus the pragmas
123         - for rule decls, we ignore HsRules
124         - for data decls, we ignore derivings
125
126         *** See "THE NAMING STORY" in HsDecls ****
127
128 \begin{code}
129 ----------------
130 impDeclFVs :: RenamedHsDecl -> NameSet
131         -- Just the ones that come from imports
132 impDeclFVs (InstD d) = instDeclFVs d
133 impDeclFVs (TyClD d) = tyClDeclFVs d
134
135 ----------------
136 tyClDeclFVs :: RenamedTyClDecl -> NameSet
137 tyClDeclFVs (ForeignType {})
138   = emptyFVs
139
140 tyClDeclFVs (IfaceSig {tcdType = ty, tcdIdInfo = id_infos})
141   = extractHsTyNames ty                 `plusFV` 
142     plusFVs (map hsIdInfoFVs id_infos)
143
144 tyClDeclFVs (TyData {tcdCtxt = context, tcdTyVars = tyvars, tcdCons = condecls})
145   = delFVs (map hsTyVarName tyvars)     $
146     extractHsCtxtTyNames context        `plusFV`
147     plusFVs (map conDeclFVs (visibleDataCons condecls))
148
149 tyClDeclFVs (TySynonym {tcdTyVars = tyvars, tcdSynRhs = ty})
150   = delFVs (map hsTyVarName tyvars) (extractHsTyNames ty)
151
152 tyClDeclFVs (ClassDecl {tcdCtxt = context, tcdTyVars = tyvars, tcdFDs = fds, 
153                         tcdSigs = sigs, tcdMeths = maybe_meths})
154   = delFVs (map hsTyVarName tyvars) $
155     extractHsCtxtTyNames context          `plusFV`
156     plusFVs (map extractFunDepNames fds)  `plusFV`
157     hsSigsFVs sigs                        `plusFV`
158     dm_fvs
159   where
160     dm_fvs = case maybe_meths of
161                 Nothing -> mkFVs [v | ClassOpSig _ (DefMeth v) _ _ <- sigs]
162                   -- No method bindings, so this class decl comes from an interface file, 
163                   -- So we want to treat the default-method names as free (they should
164                   -- be defined somewhere else).  [In source code this is not so; the class
165                   -- decl will bind whatever default-methods are necessary.]
166                 Just _ -> emptyFVs      -- Source code, so the default methods
167                                         -- are *bound* not *free*
168
169 ----------------
170 hsSigsFVs sigs = plusFVs (map hsSigFVs sigs)
171
172 hsSigFVs (Sig v ty _)       = extractHsTyNames ty
173 hsSigFVs (SpecInstSig ty _) = extractHsTyNames ty
174 hsSigFVs (SpecSig v ty _)   = extractHsTyNames ty
175 hsSigFVs (ClassOpSig _ _ ty _) = extractHsTyNames ty
176 hsSigFVs other              = emptyFVs
177
178 ----------------
179 instDeclFVs (InstDecl inst_ty _ _ maybe_dfun _)
180   = extractHsTyNames inst_ty    `plusFV` 
181     (case maybe_dfun of { Just n -> unitFV n; Nothing -> emptyFVs })
182
183 ----------------
184 ruleDeclFVs (HsRule _ _ _ _ _ _) = emptyFVs
185 ruleDeclFVs (IfaceRuleOut _ _)   = emptyFVs
186 ruleDeclFVs (IfaceRule _ _ vars _ args rhs _)
187   = delFVs (map ufBinderName vars) $
188     ufExprFVs rhs `plusFV` plusFVs (map ufExprFVs args)
189
190 ----------------
191 conDeclFVs (ConDecl _ tyvars context details _)
192   = delFVs (map hsTyVarName tyvars) $
193     extractHsCtxtTyNames context          `plusFV`
194     conDetailsFVs details
195
196 conDetailsFVs (PrefixCon btys)    = plusFVs (map bangTyFVs btys)
197 conDetailsFVs (InfixCon bty1 bty2) = bangTyFVs bty1 `plusFV` bangTyFVs bty2
198 conDetailsFVs (RecCon flds)        = plusFVs [bangTyFVs bty | (_, bty) <- flds]
199
200 bangTyFVs bty = extractHsTyNames (getBangType bty)
201
202 ----------------
203 hsIdInfoFVs (HsUnfold _ unf) = ufExprFVs unf
204 hsIdInfoFVs (HsWorker n a)   = unitFV n
205 hsIdInfoFVs other            = emptyFVs
206
207 ----------------
208 ufExprFVs (UfVar n)       = unitFV n
209 ufExprFVs (UfLit l)       = emptyFVs
210 ufExprFVs (UfLitLit l ty) = extractHsTyNames ty
211 ufExprFVs (UfFCall cc ty) = extractHsTyNames ty
212 ufExprFVs (UfType ty)     = extractHsTyNames ty
213 ufExprFVs (UfTuple tc es) = hsTupConFVs tc `plusFV` plusFVs (map ufExprFVs es)
214 ufExprFVs (UfLam v e)     = ufBndrFVs v (ufExprFVs e)
215 ufExprFVs (UfApp e1 e2)   = ufExprFVs e1 `plusFV` ufExprFVs e2
216 ufExprFVs (UfCase e n as) = ufExprFVs e `plusFV` delFV n (plusFVs (map ufAltFVs as))
217 ufExprFVs (UfNote n e)    = ufNoteFVs n `plusFV` ufExprFVs e
218 ufExprFVs (UfLet (UfNonRec b r) e) = ufExprFVs r `plusFV` ufBndrFVs b (ufExprFVs e)
219 ufExprFVs (UfLet (UfRec prs)    e) = foldr ufBndrFVs 
220                                            (foldr (plusFV . ufExprFVs . snd) (ufExprFVs e) prs)
221                                            (map fst prs) 
222
223 ufBndrFVs (UfValBinder n ty) fvs = extractHsTyNames ty `plusFV` delFV n fvs
224 ufBndrFVs (UfTyBinder  n k)  fvs = delFV n fvs
225
226 ufAltFVs (con, vs, e) = ufConFVs con `plusFV` delFVs vs (ufExprFVs e)
227
228 ufConFVs (UfDataAlt n)      = unitFV n
229 ufConFVs (UfTupleAlt t)     = hsTupConFVs t
230 ufConFVs (UfLitLitAlt _ ty) = extractHsTyNames ty
231 ufConFVs other              = emptyFVs
232
233 ufNoteFVs (UfCoerce ty) = extractHsTyNames ty
234 ufNoteFVs note          = emptyFVs
235
236 hsTupConFVs (HsTupCon bx n) = unitFV (tyConName (tupleTyCon bx n))
237         -- Always return the TyCon; that'll suck in the data con
238 \end{code}
239
240
241 %************************************************************************
242 %*                                                                      *
243 \subsection{A few functions on generic defintions
244 %*                                                                      *
245 %************************************************************************
246
247 These functions on generics are defined over RenamedMatches, which is
248 why they are here and not in HsMatches.
249
250 \begin{code}
251 maybeGenericMatch :: RenamedMatch -> Maybe (RenamedHsType, RenamedMatch)
252   -- Tells whether a Match is for a generic definition
253   -- and extract the type from a generic match and put it at the front
254
255 maybeGenericMatch (Match (TypePat ty : pats) sig_ty grhss)
256   = Just (ty, Match pats sig_ty grhss)
257
258 maybeGenericMatch other_match = Nothing
259 \end{code}