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 TyCon ( visibleDataCons )
15 import TysWiredIn ( tupleTyCon, listTyCon, parrTyCon, charTyCon )
16 import Name ( Name, getName, isTyVarName )
18 import BasicTypes ( Boxity )
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
51 %************************************************************************
53 \subsection{Free variables}
55 %************************************************************************
57 These free-variable finders returns tycons and classes too.
60 charTyCon_name, listTyCon_name, parrTyCon_name :: Name
61 charTyCon_name = getName charTyCon
62 listTyCon_name = getName listTyCon
63 parrTyCon_name = getName parrTyCon
65 tupleTyCon_name :: Boxity -> Int -> Name
66 tupleTyCon_name boxity n = getName (tupleTyCon boxity n)
68 extractHsTyVars :: RenamedHsType -> NameSet
69 extractHsTyVars x = filterNameSet isTyVarName (extractHsTyNames x)
71 extractFunDepNames :: FunDep Name -> NameSet
72 extractFunDepNames (ns1, ns2) = mkNameSet ns1 `unionNameSets` mkNameSet ns2
74 extractHsTyNames :: RenamedHsType -> NameSet
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`
86 get (HsNumTy n) = emptyNameSet
87 get (HsTyVar tv) = unitNameSet tv
88 get (HsKindSig ty k) = get ty
89 get (HsForAllTy (Just tvs)
90 ctxt ty) = (extractHsCtxtTyNames ctxt `unionNameSets` get ty)
92 mkNameSet (hsTyVarNames tvs)
93 get ty@(HsForAllTy Nothing _ _) = pprPanic "extractHsTyNames" (ppr ty)
95 extractHsTyNames_s :: [RenamedHsType] -> NameSet
96 extractHsTyNames_s tys = foldr (unionNameSets . extractHsTyNames) emptyNameSet tys
98 extractHsCtxtTyNames :: RenamedContext -> NameSet
99 extractHsCtxtTyNames ctxt = foldr (unionNameSets . extractHsPredTyNames) emptyNameSet ctxt
101 -- You don't import or export implicit parameters,
102 -- so don't mention the IP names
103 extractHsPredTyNames (HsClassP cls tys)
104 = unitNameSet cls `unionNameSets` extractHsTyNames_s tys
105 extractHsPredTyNames (HsIParam n ty)
106 = extractHsTyNames ty
110 %************************************************************************
112 \subsection{Free variables of declarations}
114 %************************************************************************
116 Return the Names that must be in scope if we are to use this declaration.
117 In all cases this is set up for interface-file declarations:
118 - for class decls we ignore the bindings
119 - for instance decls likewise, plus the pragmas
120 - for rule decls, we ignore HsRules
121 - for data decls, we ignore derivings
123 *** See "THE NAMING STORY" in HsDecls ****
126 tyClDeclFVs :: RenamedTyClDecl -> NameSet
127 tyClDeclFVs (ForeignType {})
130 tyClDeclFVs (IfaceSig {tcdType = ty, tcdIdInfo = id_infos})
131 = extractHsTyNames ty `plusFV`
132 plusFVs (map hsIdInfoFVs id_infos)
134 tyClDeclFVs (TyData {tcdCtxt = context, tcdTyVars = tyvars, tcdCons = condecls})
135 = delFVs (map hsTyVarName tyvars) $
136 extractHsCtxtTyNames context `plusFV`
137 plusFVs (map conDeclFVs (visibleDataCons condecls))
139 tyClDeclFVs (TySynonym {tcdTyVars = tyvars, tcdSynRhs = ty})
140 = delFVs (map hsTyVarName tyvars) (extractHsTyNames ty)
142 tyClDeclFVs (ClassDecl {tcdCtxt = context, tcdTyVars = tyvars, tcdFDs = fds,
143 tcdSigs = sigs, tcdMeths = maybe_meths})
144 = delFVs (map hsTyVarName tyvars) $
145 extractHsCtxtTyNames context `plusFV`
146 plusFVs (map extractFunDepNames fds) `plusFV`
147 hsSigsFVs sigs `plusFV`
150 dm_fvs = case maybe_meths of
151 Nothing -> mkFVs [v | ClassOpSig _ (DefMeth v) _ _ <- sigs]
152 -- No method bindings, so this class decl comes from an interface file,
153 -- So we want to treat the default-method names as free (they should
154 -- be defined somewhere else). [In source code this is not so; the class
155 -- decl will bind whatever default-methods are necessary.]
156 Just _ -> emptyFVs -- Source code, so the default methods
157 -- are *bound* not *free*
159 tyClDeclFVs (CoreDecl {tcdType = ty, tcdRhs = rhs})
160 = extractHsTyNames ty `plusFV` ufExprFVs rhs
163 hsSigsFVs sigs = plusFVs (map hsSigFVs sigs)
165 hsSigFVs (Sig v ty _) = extractHsTyNames ty
166 hsSigFVs (SpecInstSig ty _) = extractHsTyNames ty
167 hsSigFVs (SpecSig v ty _) = extractHsTyNames ty
168 hsSigFVs (ClassOpSig _ _ ty _) = extractHsTyNames ty
169 hsSigFVs other = emptyFVs
172 instDeclFVs (InstDecl inst_ty _ _ maybe_dfun _)
173 = extractHsTyNames inst_ty `plusFV`
174 (case maybe_dfun of { Just n -> unitFV n; Nothing -> emptyFVs })
177 ruleDeclFVs (HsRule _ _ _ _ _ _) = emptyFVs
178 ruleDeclFVs (IfaceRuleOut _ _) = emptyFVs
179 ruleDeclFVs (IfaceRule _ _ vars _ args rhs _)
180 = delFVs (map ufBinderName vars) $
181 ufExprFVs rhs `plusFV` plusFVs (map ufExprFVs args)
184 conDeclFVs (ConDecl _ _ tyvars context details _)
185 = delFVs (map hsTyVarName tyvars) $
186 extractHsCtxtTyNames context `plusFV`
187 conDetailsFVs details
189 conDetailsFVs (VanillaCon btys) = plusFVs (map bangTyFVs btys)
190 conDetailsFVs (InfixCon bty1 bty2) = bangTyFVs bty1 `plusFV` bangTyFVs bty2
191 conDetailsFVs (RecCon flds) = plusFVs [bangTyFVs bty | (_, bty) <- flds]
193 bangTyFVs bty = extractHsTyNames (getBangType bty)
196 hsIdInfoFVs (HsUnfold _ unf) = ufExprFVs unf
197 hsIdInfoFVs (HsWorker n a) = unitFV n
198 hsIdInfoFVs other = emptyFVs
201 ufExprFVs (UfVar n) = unitFV n
202 ufExprFVs (UfLit l) = emptyFVs
203 ufExprFVs (UfLitLit l ty) = extractHsTyNames ty
204 ufExprFVs (UfFCall cc ty) = extractHsTyNames ty
205 ufExprFVs (UfType ty) = extractHsTyNames ty
206 ufExprFVs (UfTuple tc es) = hsTupConFVs tc `plusFV` plusFVs (map ufExprFVs es)
207 ufExprFVs (UfLam v e) = ufBndrFVs v (ufExprFVs e)
208 ufExprFVs (UfApp e1 e2) = ufExprFVs e1 `plusFV` ufExprFVs e2
209 ufExprFVs (UfCase e n as) = ufExprFVs e `plusFV` delFV n (plusFVs (map ufAltFVs as))
210 ufExprFVs (UfNote n e) = ufNoteFVs n `plusFV` ufExprFVs e
211 ufExprFVs (UfLet (UfNonRec b r) e) = ufExprFVs r `plusFV` ufBndrFVs b (ufExprFVs e)
212 ufExprFVs (UfLet (UfRec prs) e) = foldr ufBndrFVs
213 (foldr (plusFV . ufExprFVs . snd) (ufExprFVs e) prs)
216 ufBndrFVs (UfValBinder n ty) fvs = extractHsTyNames ty `plusFV` delFV n fvs
217 ufBndrFVs (UfTyBinder n k) fvs = delFV n fvs
219 ufAltFVs (con, vs, e) = ufConFVs con `plusFV` delFVs vs (ufExprFVs e)
221 ufConFVs (UfDataAlt n) = unitFV n
222 ufConFVs (UfTupleAlt t) = hsTupConFVs t
223 ufConFVs (UfLitLitAlt _ ty) = extractHsTyNames ty
224 ufConFVs other = emptyFVs
226 ufNoteFVs (UfCoerce ty) = extractHsTyNames ty
227 ufNoteFVs note = emptyFVs
229 hsTupConFVs (HsTupCon n _ _) = unitFV n
232 %************************************************************************
234 \subsection{A few functions on generic defintions
236 %************************************************************************
238 These functions on generics are defined over RenamedMatches, which is
239 why they are here and not in HsMatches.
242 maybeGenericMatch :: RenamedMatch -> Maybe (RenamedHsType, RenamedMatch)
243 -- Tells whether a Match is for a generic definition
244 -- and extract the type from a generic match and put it at the front
246 maybeGenericMatch (Match (TypePatIn ty : pats) sig_ty grhss)
247 = Just (ty, Match pats sig_ty grhss)
249 maybeGenericMatch other_match = Nothing