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