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