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`
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)
93 mkNameSet (hsTyVarNames tvs)
94 get ty@(HsForAllTy Nothing _ _) = pprPanic "extractHsTyNames" (ppr ty)
96 extractHsTyNames_s :: [RenamedHsType] -> NameSet
97 extractHsTyNames_s tys = foldr (unionNameSets . extractHsTyNames) emptyNameSet tys
99 extractHsCtxtTyNames :: RenamedContext -> NameSet
100 extractHsCtxtTyNames ctxt = foldr (unionNameSets . extractHsPredTyNames) emptyNameSet ctxt
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
111 %************************************************************************
113 \subsection{Free variables of declarations}
115 %************************************************************************
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
124 *** See "THE NAMING STORY" in HsDecls ****
127 tyClDeclFVs :: RenamedTyClDecl -> NameSet
128 tyClDeclFVs (ForeignType {})
131 tyClDeclFVs (IfaceSig {tcdType = ty, tcdIdInfo = id_infos})
132 = extractHsTyNames ty `plusFV`
133 plusFVs (map hsIdInfoFVs id_infos)
135 tyClDeclFVs (TyData {tcdCtxt = context, tcdTyVars = tyvars, tcdCons = condecls})
136 = delFVs (map hsTyVarName tyvars) $
137 extractHsCtxtTyNames context `plusFV`
138 plusFVs (map conDeclFVs (visibleDataCons condecls))
140 tyClDeclFVs (TySynonym {tcdTyVars = tyvars, tcdSynRhs = ty})
141 = delFVs (map hsTyVarName tyvars) (extractHsTyNames ty)
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`
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*
160 tyClDeclFVs (CoreDecl {tcdType = ty, tcdRhs = rhs})
161 = extractHsTyNames ty `plusFV` ufExprFVs rhs
164 hsSigsFVs sigs = plusFVs (map hsSigFVs sigs)
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
173 instDeclFVs (InstDecl inst_ty _ _ maybe_dfun _)
174 = extractHsTyNames inst_ty `plusFV`
175 (case maybe_dfun of { Just n -> unitFV n; Nothing -> emptyFVs })
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)
185 conDeclFVs (ConDecl _ _ tyvars context details _)
186 = delFVs (map hsTyVarName tyvars) $
187 extractHsCtxtTyNames context `plusFV`
188 conDetailsFVs details
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]
194 bangTyFVs bty = extractHsTyNames (getBangType bty)
197 hsIdInfoFVs (HsUnfold _ unf) = ufExprFVs unf
198 hsIdInfoFVs (HsWorker n a) = unitFV n
199 hsIdInfoFVs other = emptyFVs
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)
217 ufBndrFVs (UfValBinder n ty) fvs = extractHsTyNames ty `plusFV` delFV n fvs
218 ufBndrFVs (UfTyBinder n k) fvs = delFV n fvs
220 ufAltFVs (con, vs, e) = ufConFVs con `plusFV` delFVs vs (ufExprFVs e)
222 ufConFVs (UfDataAlt n) = unitFV n
223 ufConFVs (UfTupleAlt t) = hsTupConFVs t
224 ufConFVs (UfLitLitAlt _ ty) = extractHsTyNames ty
225 ufConFVs other = emptyFVs
227 ufNoteFVs (UfCoerce ty) = extractHsTyNames ty
228 ufNoteFVs note = emptyFVs
230 hsTupConFVs (HsTupCon n _ _) = unitFV n
233 %************************************************************************
235 \subsection{A few functions on generic defintions
237 %************************************************************************
239 These functions on generics are defined over RenamedMatches, which is
240 why they are here and not in HsMatches.
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
247 maybeGenericMatch (Match (TypePatIn ty : pats) sig_ty grhss)
248 = Just (ty, Match pats sig_ty grhss)
250 maybeGenericMatch other_match = Nothing