[project @ 2003-12-10 17:25:12 by simonmar]
[ghc-hetmet.git] / ghc / compiler / rename / RnHsSyn.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1996-1998
3 %
4 \section[RnHsSyn]{Specialisations of the @HsSyn@ syntax for the renamer}
5
6 \begin{code}
7 module RnHsSyn where
8
9 #include "HsVersions.h"
10
11 import HsSyn
12 import Class            ( FunDep )
13 import TysWiredIn       ( tupleTyCon, listTyCon, parrTyCon, charTyCon )
14 import Name             ( Name, getName, isTyVarName )
15 import NameSet
16 import BasicTypes       ( Boxity )
17 import SrcLoc           ( Located(..), unLoc )
18 \end{code}
19
20 %************************************************************************
21 %*                                                                      *
22 \subsection{Free variables}
23 %*                                                                      *
24 %************************************************************************
25
26 These free-variable finders returns tycons and classes too.
27
28 \begin{code}
29 charTyCon_name, listTyCon_name, parrTyCon_name :: Name
30 charTyCon_name    = getName charTyCon
31 listTyCon_name    = getName listTyCon
32 parrTyCon_name    = getName parrTyCon
33
34 tupleTyCon_name :: Boxity -> Int -> Name
35 tupleTyCon_name boxity n = getName (tupleTyCon boxity n)
36
37 extractHsTyVars :: LHsType Name -> NameSet
38 extractHsTyVars x = filterNameSet isTyVarName (extractHsTyNames x)
39
40 extractFunDepNames :: FunDep Name -> NameSet
41 extractFunDepNames (ns1, ns2) = mkNameSet ns1 `unionNameSets` mkNameSet ns2
42
43 extractHsTyNames   :: LHsType Name -> NameSet
44 extractHsTyNames ty
45   = getl ty
46   where
47     getl (L _ ty) = get ty
48
49     get (HsAppTy ty1 ty2)      = getl ty1 `unionNameSets` getl ty2
50     get (HsListTy ty)          = unitNameSet listTyCon_name `unionNameSets` getl ty
51     get (HsPArrTy ty)          = unitNameSet parrTyCon_name `unionNameSets` getl ty
52     get (HsTupleTy con tys)    = extractHsTyNames_s tys
53     get (HsFunTy ty1 ty2)      = getl ty1 `unionNameSets` getl ty2
54     get (HsPredTy p)           = extractHsPredTyNames (unLoc p)
55     get (HsOpTy ty1 op ty2)    = getl ty1 `unionNameSets` getl ty2 `unionNameSets` unitNameSet (unLoc op)
56     get (HsParTy ty)           = getl ty
57     get (HsNumTy n)            = emptyNameSet
58     get (HsTyVar tv)           = unitNameSet tv
59     get (HsKindSig ty k)       = getl ty
60     get (HsForAllTy _ tvs 
61                     ctxt ty)   = (extractHsCtxtTyNames ctxt
62                                          `unionNameSets` getl ty)
63                                             `minusNameSet`
64                                   mkNameSet (hsLTyVarNames tvs)
65
66 extractHsTyNames_s  :: [LHsType Name] -> NameSet
67 extractHsTyNames_s tys = foldr (unionNameSets . extractHsTyNames) emptyNameSet tys
68
69 extractHsCtxtTyNames :: LHsContext Name -> NameSet
70 extractHsCtxtTyNames (L _ ctxt)
71   = foldr (unionNameSets . extractHsPredTyNames . unLoc) emptyNameSet ctxt
72
73 -- You don't import or export implicit parameters,
74 -- so don't mention the IP names
75 extractHsPredTyNames (HsClassP cls tys)
76   = unitNameSet cls `unionNameSets` extractHsTyNames_s tys
77 extractHsPredTyNames (HsIParam n ty)
78   = extractHsTyNames ty
79 \end{code}
80
81
82 %************************************************************************
83 %*                                                                      *
84 \subsection{Free variables of declarations}
85 %*                                                                      *
86 %************************************************************************
87
88 Return the Names that must be in scope if we are to use this declaration.
89 In all cases this is set up for interface-file declarations:
90         - for class decls we ignore the bindings
91         - for instance decls likewise, plus the pragmas
92         - for rule decls, we ignore HsRules
93         - for data decls, we ignore derivings
94
95         *** See "THE NAMING STORY" in HsDecls ****
96
97 \begin{code}
98 ----------------
99 hsSigsFVs :: [LSig Name] -> FreeVars
100 hsSigsFVs sigs = plusFVs (map (hsSigFVs.unLoc) sigs)
101
102 hsSigFVs (Sig v ty)         = extractHsTyNames ty
103 hsSigFVs (SpecInstSig ty)   = extractHsTyNames ty
104 hsSigFVs (SpecSig v ty)     = extractHsTyNames ty
105 hsSigFVs other              = emptyFVs
106
107 ----------------
108 conDeclFVs (L _ (ConDecl _ tyvars context details))
109   = delFVs (map hsLTyVarName tyvars) $
110     extractHsCtxtTyNames context          `plusFV`
111     conDetailsFVs details
112
113 conDetailsFVs (PrefixCon btys)    = plusFVs (map bangTyFVs btys)
114 conDetailsFVs (InfixCon bty1 bty2) = bangTyFVs bty1 `plusFV` bangTyFVs bty2
115 conDetailsFVs (RecCon flds)        = plusFVs [bangTyFVs bty | (_, bty) <- flds]
116
117 bangTyFVs bty = extractHsTyNames (getBangType (unLoc bty))
118 \end{code}
119
120
121 %************************************************************************
122 %*                                                                      *
123 \subsection{A few functions on generic defintions
124 %*                                                                      *
125 %************************************************************************
126
127 These functions on generics are defined over Matches Name, which is
128 why they are here and not in HsMatches.
129
130 \begin{code}
131 maybeGenericMatch :: LMatch Name -> Maybe (HsType Name, LMatch Name)
132   -- Tells whether a Match is for a generic definition
133   -- and extract the type from a generic match and put it at the front
134
135 maybeGenericMatch (L loc (Match (L _ (TypePat (L _ ty)) : pats) sig_ty grhss))
136   = Just (ty, L loc (Match pats sig_ty grhss))
137
138 maybeGenericMatch other_match = Nothing
139 \end{code}