2 % (c) The AQUA Project, Glasgow University, 1996-1998
4 \section[RnHsSyn]{Specialisations of the @HsSyn@ syntax for the renamer}
9 #include "HsVersions.h"
12 import Class ( FunDep )
13 import TysWiredIn ( tupleTyCon, listTyCon, parrTyCon, charTyCon )
14 import Name ( Name, getName, isTyVarName )
16 import BasicTypes ( Boxity )
22 type RenamedHsDecl = HsDecl Name
23 type RenamedArithSeqInfo = ArithSeqInfo Name
24 type RenamedClassOpSig = Sig Name
25 type RenamedConDecl = ConDecl Name
26 type RenamedContext = HsContext Name
27 type RenamedRuleDecl = RuleDecl Name
28 type RenamedTyClDecl = TyClDecl Name
29 type RenamedDefaultDecl = DefaultDecl Name
30 type RenamedForeignDecl = ForeignDecl Name
31 type RenamedGRHS = GRHS Name
32 type RenamedGRHSs = GRHSs Name
33 type RenamedHsBinds = HsBinds Name
34 type RenamedHsExpr = HsExpr Name
35 type RenamedInstDecl = InstDecl Name
36 type RenamedMatchContext = HsMatchContext Name
37 type RenamedMatch = Match Name
38 type RenamedMonoBinds = MonoBinds Name
39 type RenamedPat = InPat Name
40 type RenamedHsType = HsType Name
41 type RenamedHsPred = HsPred Name
42 type RenamedRecordBinds = HsRecordBinds Name
43 type RenamedSig = Sig Name
44 type RenamedStmt = Stmt Name
45 type RenamedFixitySig = FixitySig Name
46 type RenamedDeprecation = DeprecDecl Name
47 type RenamedHsCmd = HsCmd Name
48 type RenamedHsCmdTop = HsCmdTop 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) = extractHsTyNames_s tys
82 get (HsFunTy ty1 ty2) = get ty1 `unionNameSets` get ty2
83 get (HsPredTy p) = extractHsPredTyNames p
84 get (HsOpTy ty1 op ty2) = get ty1 `unionNameSets` get ty2 `unionNameSets` unitNameSet op
85 get (HsParTy ty) = get ty
86 get (HsNumTy n) = emptyNameSet
87 get (HsTyVar tv) = unitNameSet tv
88 get (HsKindSig ty k) = get ty
90 ctxt ty) = (extractHsCtxtTyNames ctxt `unionNameSets` get ty)
92 mkNameSet (hsTyVarNames tvs)
94 extractHsTyNames_s :: [RenamedHsType] -> NameSet
95 extractHsTyNames_s tys = foldr (unionNameSets . extractHsTyNames) emptyNameSet tys
97 extractHsCtxtTyNames :: RenamedContext -> NameSet
98 extractHsCtxtTyNames ctxt = foldr (unionNameSets . extractHsPredTyNames) emptyNameSet ctxt
100 -- You don't import or export implicit parameters,
101 -- so don't mention the IP names
102 extractHsPredTyNames (HsClassP cls tys)
103 = unitNameSet cls `unionNameSets` extractHsTyNames_s tys
104 extractHsPredTyNames (HsIParam n ty)
105 = extractHsTyNames ty
109 %************************************************************************
111 \subsection{Free variables of declarations}
113 %************************************************************************
115 Return the Names that must be in scope if we are to use this declaration.
116 In all cases this is set up for interface-file declarations:
117 - for class decls we ignore the bindings
118 - for instance decls likewise, plus the pragmas
119 - for rule decls, we ignore HsRules
120 - for data decls, we ignore derivings
122 *** See "THE NAMING STORY" in HsDecls ****
126 hsSigsFVs sigs = plusFVs (map hsSigFVs sigs)
128 hsSigFVs (Sig v ty _) = extractHsTyNames ty
129 hsSigFVs (SpecInstSig ty _) = extractHsTyNames ty
130 hsSigFVs (SpecSig v ty _) = extractHsTyNames ty
131 hsSigFVs other = emptyFVs
134 conDeclFVs (ConDecl _ tyvars context details _)
135 = delFVs (map hsTyVarName tyvars) $
136 extractHsCtxtTyNames context `plusFV`
137 conDetailsFVs details
139 conDetailsFVs (PrefixCon btys) = plusFVs (map bangTyFVs btys)
140 conDetailsFVs (InfixCon bty1 bty2) = bangTyFVs bty1 `plusFV` bangTyFVs bty2
141 conDetailsFVs (RecCon flds) = plusFVs [bangTyFVs bty | (_, bty) <- flds]
143 bangTyFVs bty = extractHsTyNames (getBangType bty)
147 %************************************************************************
149 \subsection{A few functions on generic defintions
151 %************************************************************************
153 These functions on generics are defined over RenamedMatches, which is
154 why they are here and not in HsMatches.
157 maybeGenericMatch :: RenamedMatch -> Maybe (RenamedHsType, RenamedMatch)
158 -- Tells whether a Match is for a generic definition
159 -- and extract the type from a generic match and put it at the front
161 maybeGenericMatch (Match (TypePat ty : pats) sig_ty grhss)
162 = Just (ty, Match pats sig_ty grhss)
164 maybeGenericMatch other_match = Nothing