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,
19 #include "HsVersions.h"
22 import Class ( FunDep )
23 import TysWiredIn ( tupleTyCon, listTyCon, parrTyCon, charTyCon )
24 import Name ( Name, getName, isTyVarName )
26 import BasicTypes ( Boxity )
27 import SrcLoc ( Located(..), unLoc )
30 %************************************************************************
32 \subsection{Free variables}
34 %************************************************************************
36 These free-variable finders returns tycons and classes too.
39 charTyCon_name, listTyCon_name, parrTyCon_name :: Name
40 charTyCon_name = getName charTyCon
41 listTyCon_name = getName listTyCon
42 parrTyCon_name = getName parrTyCon
44 tupleTyCon_name :: Boxity -> Int -> Name
45 tupleTyCon_name boxity n = getName (tupleTyCon boxity n)
47 extractHsTyVars :: LHsType Name -> NameSet
48 extractHsTyVars x = filterNameSet isTyVarName (extractHsTyNames x)
50 extractFunDepNames :: FunDep Name -> NameSet
51 extractFunDepNames (ns1, ns2) = mkNameSet ns1 `unionNameSets` mkNameSet ns2
53 extractHsTyNames :: LHsType Name -> NameSet
57 getl (L _ ty) = get ty
59 get (HsAppTy ty1 ty2) = getl ty1 `unionNameSets` getl ty2
60 get (HsListTy ty) = unitNameSet listTyCon_name `unionNameSets` getl ty
61 get (HsPArrTy ty) = unitNameSet parrTyCon_name `unionNameSets` getl ty
62 get (HsTupleTy con tys) = extractHsTyNames_s tys
63 get (HsFunTy ty1 ty2) = getl ty1 `unionNameSets` getl ty2
64 get (HsPredTy p) = extractHsPredTyNames p
65 get (HsOpTy ty1 op ty2) = getl ty1 `unionNameSets` getl ty2 `unionNameSets` unitNameSet (unLoc op)
66 get (HsParTy ty) = getl ty
67 get (HsBangTy _ ty) = getl ty
68 get (HsNumTy n) = emptyNameSet
69 get (HsTyVar tv) = unitNameSet tv
70 get (HsSpliceTy _) = emptyNameSet -- Type splices mention no type variables
71 get (HsKindSig ty k) = getl ty
73 ctxt ty) = (extractHsCtxtTyNames ctxt
74 `unionNameSets` getl ty)
76 mkNameSet (hsLTyVarNames tvs)
78 extractHsTyNames_s :: [LHsType Name] -> NameSet
79 extractHsTyNames_s tys = foldr (unionNameSets . extractHsTyNames) emptyNameSet tys
81 extractHsCtxtTyNames :: LHsContext Name -> NameSet
82 extractHsCtxtTyNames (L _ ctxt)
83 = foldr (unionNameSets . extractHsPredTyNames . unLoc) emptyNameSet ctxt
85 -- You don't import or export implicit parameters,
86 -- so don't mention the IP names
87 extractHsPredTyNames (HsClassP cls tys)
88 = unitNameSet cls `unionNameSets` extractHsTyNames_s tys
89 extractHsPredTyNames (HsIParam n ty)
94 %************************************************************************
96 \subsection{Free variables of declarations}
98 %************************************************************************
100 Return the Names that must be in scope if we are to use this declaration.
101 In all cases this is set up for interface-file declarations:
102 - for class decls we ignore the bindings
103 - for instance decls likewise, plus the pragmas
104 - for rule decls, we ignore HsRules
105 - for data decls, we ignore derivings
107 *** See "THE NAMING STORY" in HsDecls ****
111 hsSigsFVs :: [LSig Name] -> FreeVars
112 hsSigsFVs sigs = plusFVs (map (hsSigFVs.unLoc) sigs)
114 hsSigFVs (TypeSig v ty) = extractHsTyNames ty
115 hsSigFVs (SpecInstSig ty) = extractHsTyNames ty
116 hsSigFVs (SpecSig v ty inl) = extractHsTyNames ty
117 hsSigFVs other = emptyFVs
120 conDeclFVs (L _ (ConDecl { con_qvars = tyvars, con_cxt = context,
121 con_details = details, con_res = res_ty}))
122 = delFVs (map hsLTyVarName tyvars) $
123 extractHsCtxtTyNames context `plusFV`
124 conDetailsFVs details `plusFV`
127 conResTyFVs ResTyH98 = emptyFVs
128 conResTyFVs (ResTyGADT ty) = extractHsTyNames ty
130 conDetailsFVs (PrefixCon btys) = plusFVs (map bangTyFVs btys)
131 conDetailsFVs (InfixCon bty1 bty2) = bangTyFVs bty1 `plusFV` bangTyFVs bty2
132 conDetailsFVs (RecCon flds) = plusFVs [bangTyFVs bty | (_, bty) <- flds]
134 bangTyFVs bty = extractHsTyNames (getBangType bty)
138 %************************************************************************
140 \subsection{A few functions on generic defintions
142 %************************************************************************
144 These functions on generics are defined over Matches Name, which is
145 why they are here and not in HsMatches.
148 maybeGenericMatch :: LMatch Name -> Maybe (HsType Name, LMatch Name)
149 -- Tells whether a Match is for a generic definition
150 -- and extract the type from a generic match and put it at the front
152 maybeGenericMatch (L loc (Match (L _ (TypePat (L _ ty)) : pats) sig_ty grhss))
153 = Just (ty, L loc (Match pats sig_ty grhss))
155 maybeGenericMatch other_match = Nothing