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