Fix CodingStyle#Warnings URLs
[ghc-hetmet.git] / 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 {-# OPTIONS -w #-}
8 -- The above warning supression flag is a temporary kludge.
9 -- While working on this module you are encouraged to remove it and fix
10 -- any warnings in the module. See
11 --     http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
12 -- for details
13
14 module RnHsSyn( 
15         -- Names
16         charTyCon_name, listTyCon_name, parrTyCon_name, tupleTyCon_name,
17         extractHsTyVars, extractHsTyNames, extractHsTyNames_s, 
18         extractFunDepNames, extractHsCtxtTyNames, extractHsPredTyNames,
19
20         -- Free variables
21         hsSigsFVs, hsSigFVs, conDeclFVs, bangTyFVs,
22         
23         maybeGenericMatch
24   ) where
25
26 #include "HsVersions.h"
27
28 import HsSyn
29 import Class            ( FunDep )
30 import TysWiredIn       ( tupleTyCon, listTyCon, parrTyCon, charTyCon )
31 import Name             ( Name, getName, isTyVarName )
32 import NameSet
33 import BasicTypes       ( Boxity )
34 import SrcLoc           ( Located(..), unLoc )
35 \end{code}
36
37 %************************************************************************
38 %*                                                                      *
39 \subsection{Free variables}
40 %*                                                                      *
41 %************************************************************************
42
43 These free-variable finders returns tycons and classes too.
44
45 \begin{code}
46 charTyCon_name, listTyCon_name, parrTyCon_name :: Name
47 charTyCon_name    = getName charTyCon
48 listTyCon_name    = getName listTyCon
49 parrTyCon_name    = getName parrTyCon
50
51 tupleTyCon_name :: Boxity -> Int -> Name
52 tupleTyCon_name boxity n = getName (tupleTyCon boxity n)
53
54 extractHsTyVars :: LHsType Name -> NameSet
55 extractHsTyVars x = filterNameSet isTyVarName (extractHsTyNames x)
56
57 extractFunDepNames :: FunDep Name -> NameSet
58 extractFunDepNames (ns1, ns2) = mkNameSet ns1 `unionNameSets` mkNameSet ns2
59
60 extractHsTyNames   :: LHsType Name -> NameSet
61 extractHsTyNames ty
62   = getl ty
63   where
64     getl (L _ ty) = get ty
65
66     get (HsAppTy ty1 ty2)      = getl ty1 `unionNameSets` getl ty2
67     get (HsListTy ty)          = unitNameSet listTyCon_name `unionNameSets` getl ty
68     get (HsPArrTy ty)          = unitNameSet parrTyCon_name `unionNameSets` getl ty
69     get (HsTupleTy con tys)    = extractHsTyNames_s tys
70     get (HsFunTy ty1 ty2)      = getl ty1 `unionNameSets` getl ty2
71     get (HsPredTy p)           = extractHsPredTyNames p
72     get (HsOpTy ty1 op ty2)    = getl ty1 `unionNameSets` getl ty2 `unionNameSets` unitNameSet (unLoc op)
73     get (HsParTy ty)           = getl ty
74     get (HsBangTy _ ty)        = getl ty
75     get (HsNumTy n)            = emptyNameSet
76     get (HsTyVar tv)           = unitNameSet tv
77     get (HsSpliceTy _)         = emptyNameSet   -- Type splices mention no type variables
78     get (HsKindSig ty k)       = getl ty
79     get (HsForAllTy _ tvs 
80                     ctxt ty)   = (extractHsCtxtTyNames ctxt
81                                          `unionNameSets` getl ty)
82                                             `minusNameSet`
83                                   mkNameSet (hsLTyVarNames tvs)
84     get (HsDocTy ty _)         = getl ty
85
86 extractHsTyNames_s  :: [LHsType Name] -> NameSet
87 extractHsTyNames_s tys = foldr (unionNameSets . extractHsTyNames) emptyNameSet tys
88
89 extractHsCtxtTyNames :: LHsContext Name -> NameSet
90 extractHsCtxtTyNames (L _ ctxt)
91   = foldr (unionNameSets . extractHsPredTyNames . unLoc) emptyNameSet ctxt
92
93 -- You don't import or export implicit parameters,
94 -- so don't mention the IP names
95 extractHsPredTyNames (HsClassP cls tys)
96   = unitNameSet cls `unionNameSets` extractHsTyNames_s tys
97 extractHsPredTyNames (HsEqualP ty1 ty2)
98   = extractHsTyNames ty1 `unionNameSets` extractHsTyNames ty2
99 extractHsPredTyNames (HsIParam n ty)
100   = extractHsTyNames ty
101 \end{code}
102
103
104 %************************************************************************
105 %*                                                                      *
106 \subsection{Free variables of declarations}
107 %*                                                                      *
108 %************************************************************************
109
110 Return the Names that must be in scope if we are to use this declaration.
111 In all cases this is set up for interface-file declarations:
112         - for class decls we ignore the bindings
113         - for instance decls likewise, plus the pragmas
114         - for rule decls, we ignore HsRules
115         - for data decls, we ignore derivings
116
117         *** See "THE NAMING STORY" in HsDecls ****
118
119 \begin{code}
120 ----------------
121 hsSigsFVs :: [LSig Name] -> FreeVars
122 hsSigsFVs sigs = plusFVs (map (hsSigFVs.unLoc) sigs)
123
124 hsSigFVs (TypeSig v ty)     = extractHsTyNames ty
125 hsSigFVs (SpecInstSig ty)   = extractHsTyNames ty
126 hsSigFVs (SpecSig v ty inl) = extractHsTyNames ty
127 hsSigFVs other              = emptyFVs
128
129 ----------------
130 conDeclFVs (L _ (ConDecl { con_qvars = tyvars, con_cxt = context, 
131                            con_details = details, con_res = res_ty}))
132   = delFVs (map hsLTyVarName tyvars) $
133     extractHsCtxtTyNames context  `plusFV`
134     conDetailsFVs details         `plusFV`
135     conResTyFVs res_ty
136
137 conResTyFVs ResTyH98       = emptyFVs
138 conResTyFVs (ResTyGADT ty) = extractHsTyNames ty
139
140 conDetailsFVs :: HsConDeclDetails Name -> FreeVars
141 conDetailsFVs details = plusFVs (map bangTyFVs (hsConDeclArgTys details))
142
143 bangTyFVs bty = extractHsTyNames (getBangType bty)
144 \end{code}
145
146
147 %************************************************************************
148 %*                                                                      *
149 \subsection{A few functions on generic defintions
150 %*                                                                      *
151 %************************************************************************
152
153 These functions on generics are defined over Matches Name, which is
154 why they are here and not in HsMatches.
155
156 \begin{code}
157 maybeGenericMatch :: LMatch Name -> Maybe (HsType Name, LMatch Name)
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
160
161 maybeGenericMatch (L loc (Match (L _ (TypePat (L _ ty)) : pats) sig_ty grhss))
162   = Just (ty, L loc (Match pats sig_ty grhss))
163
164 maybeGenericMatch other_match = Nothing
165 \end{code}