2 % (c) The AQUA Project, Glasgow University, 1996-1998
4 \section[RnHsSyn]{Specialisations of the @HsSyn@ syntax for the renamer}
9 charTyCon_name, listTyCon_name, parrTyCon_name, tupleTyCon_name,
10 extractHsTyVars, extractHsTyNames, extractHsTyNames_s,
11 extractFunDepNames, extractHsCtxtTyNames, extractHsPredTyNames,
14 hsSigsFVs, hsSigFVs, conDeclFVs, bangTyFVs
17 #include "HsVersions.h"
20 import Class ( FunDep )
21 import TysWiredIn ( tupleTyCon, listTyCon, parrTyCon, charTyCon )
22 import Name ( Name, getName, isTyVarName )
24 import BasicTypes ( Boxity )
25 import SrcLoc ( Located(..), unLoc )
28 %************************************************************************
30 \subsection{Free variables}
32 %************************************************************************
34 These free-variable finders returns tycons and classes too.
37 charTyCon_name, listTyCon_name, parrTyCon_name :: Name
38 charTyCon_name = getName charTyCon
39 listTyCon_name = getName listTyCon
40 parrTyCon_name = getName parrTyCon
42 tupleTyCon_name :: Boxity -> Int -> Name
43 tupleTyCon_name boxity n = getName (tupleTyCon boxity n)
45 extractHsTyVars :: LHsType Name -> NameSet
46 extractHsTyVars x = filterNameSet isTyVarName (extractHsTyNames x)
48 extractFunDepNames :: FunDep Name -> NameSet
49 extractFunDepNames (ns1, ns2) = mkNameSet ns1 `unionNameSets` mkNameSet ns2
51 extractHsTyNames :: LHsType Name -> NameSet
55 getl (L _ ty) = get ty
57 get (HsAppTy ty1 ty2) = getl ty1 `unionNameSets` getl ty2
58 get (HsListTy ty) = unitNameSet listTyCon_name `unionNameSets` getl ty
59 get (HsPArrTy ty) = unitNameSet parrTyCon_name `unionNameSets` getl ty
60 get (HsTupleTy _ tys) = extractHsTyNames_s tys
61 get (HsFunTy ty1 ty2) = getl ty1 `unionNameSets` getl ty2
62 get (HsPredTy p) = extractHsPredTyNames p
63 get (HsOpTy ty1 op ty2) = getl ty1 `unionNameSets` getl ty2 `unionNameSets` unitNameSet (unLoc op)
64 get (HsParTy ty) = getl ty
65 get (HsBangTy _ ty) = getl ty
66 get (HsRecTy flds) = extractHsTyNames_s (map cd_fld_type flds)
67 get (HsTyVar tv) = unitNameSet tv
68 get (HsSpliceTy _ fvs _) = fvs
69 get (HsQuasiQuoteTy {}) = emptyNameSet
70 get (HsKindSig ty _) = getl ty
72 ctxt ty) = (extractHsCtxtTyNames ctxt
73 `unionNameSets` getl ty)
75 mkNameSet (hsLTyVarNames tvs)
76 get (HsDocTy ty _) = getl ty
77 get (HsCoreTy {}) = emptyNameSet -- This probably isn't quite right
78 -- but I don't think it matters
80 extractHsTyNames_s :: [LHsType Name] -> NameSet
81 extractHsTyNames_s tys = foldr (unionNameSets . extractHsTyNames) emptyNameSet tys
83 extractHsCtxtTyNames :: LHsContext Name -> NameSet
84 extractHsCtxtTyNames (L _ ctxt)
85 = foldr (unionNameSets . extractHsPredTyNames . unLoc) emptyNameSet ctxt
87 -- You don't import or export implicit parameters,
88 -- so don't mention the IP names
89 extractHsPredTyNames :: HsPred Name -> NameSet
90 extractHsPredTyNames (HsClassP cls tys)
91 = unitNameSet cls `unionNameSets` extractHsTyNames_s tys
92 extractHsPredTyNames (HsEqualP ty1 ty2)
93 = extractHsTyNames ty1 `unionNameSets` extractHsTyNames ty2
94 extractHsPredTyNames (HsIParam _ ty)
99 %************************************************************************
101 \subsection{Free variables of declarations}
103 %************************************************************************
105 Return the Names that must be in scope if we are to use this declaration.
106 In all cases this is set up for interface-file declarations:
107 - for class decls we ignore the bindings
108 - for instance decls likewise, plus the pragmas
109 - for rule decls, we ignore HsRules
110 - for data decls, we ignore derivings
112 *** See "THE NAMING STORY" in HsDecls ****
116 hsSigsFVs :: [LSig Name] -> FreeVars
117 hsSigsFVs sigs = plusFVs (map (hsSigFVs.unLoc) sigs)
119 hsSigFVs :: Sig Name -> FreeVars
120 hsSigFVs (TypeSig _ ty) = extractHsTyNames ty
121 hsSigFVs (GenericSig _ ty) = extractHsTyNames ty
122 hsSigFVs (SpecInstSig ty) = extractHsTyNames ty
123 hsSigFVs (SpecSig _ ty _) = extractHsTyNames ty
124 hsSigFVs _ = emptyFVs
127 conDeclFVs :: LConDecl Name -> FreeVars
128 conDeclFVs (L _ (ConDecl { con_qvars = tyvars, con_cxt = context,
129 con_details = details, con_res = res_ty}))
130 = delFVs (map hsLTyVarName tyvars) $
131 extractHsCtxtTyNames context `plusFV`
132 conDetailsFVs details `plusFV`
135 conResTyFVs :: ResType Name -> FreeVars
136 conResTyFVs ResTyH98 = emptyFVs
137 conResTyFVs (ResTyGADT ty) = extractHsTyNames ty
139 conDetailsFVs :: HsConDeclDetails Name -> FreeVars
140 conDetailsFVs details = plusFVs (map bangTyFVs (hsConDeclArgTys details))
142 bangTyFVs :: LHsType Name -> FreeVars
143 bangTyFVs bty = extractHsTyNames (getBangType bty)