[project @ 2000-10-24 15:55:35 by simonpj]
[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 TysWiredIn       ( tupleTyCon, listTyCon, charTyCon )
13 import Name             ( Name, getName, isTyVarName )
14 import NameSet
15 import BasicTypes       ( Boxity )
16 import Outputable
17 \end{code}
18
19
20 \begin{code}
21 type RenamedArithSeqInfo        = ArithSeqInfo          Name RenamedPat
22 type RenamedClassOpSig          = Sig                   Name
23 type RenamedConDecl             = ConDecl               Name
24 type RenamedContext             = HsContext             Name
25 type RenamedHsDecl              = HsDecl                Name RenamedPat
26 type RenamedRuleDecl            = RuleDecl              Name RenamedPat
27 type RenamedTyClDecl            = TyClDecl              Name RenamedPat
28 type RenamedDefaultDecl         = DefaultDecl           Name
29 type RenamedForeignDecl         = ForeignDecl           Name
30 type RenamedGRHS                = GRHS                  Name RenamedPat
31 type RenamedGRHSs               = GRHSs                 Name RenamedPat
32 type RenamedHsBinds             = HsBinds               Name RenamedPat
33 type RenamedHsExpr              = HsExpr                Name RenamedPat
34 type RenamedHsModule            = HsModule              Name RenamedPat
35 type RenamedInstDecl            = InstDecl              Name RenamedPat
36 type RenamedMatch               = Match                 Name RenamedPat
37 type RenamedMonoBinds           = MonoBinds             Name RenamedPat
38 type RenamedPat                 = InPat                 Name
39 type RenamedHsType              = HsType                Name
40 type RenamedHsPred              = HsPred                Name
41 type RenamedRecordBinds         = HsRecordBinds         Name RenamedPat
42 type RenamedSig                 = Sig                   Name
43 type RenamedStmt                = Stmt                  Name RenamedPat
44 type RenamedFixitySig           = FixitySig             Name
45 type RenamedDeprecation         = DeprecDecl            Name
46 type RenamedHsOverLit           = HsOverLit             Name
47 \end{code}
48
49 %************************************************************************
50 %*                                                                      *
51 \subsection{Free variables}
52 %*                                                                      *
53 %************************************************************************
54
55 These free-variable finders returns tycons and classes too.
56
57 \begin{code}
58 charTyCon_name, listTyCon_name :: Name
59 charTyCon_name    = getName charTyCon
60 listTyCon_name    = getName listTyCon
61
62 tupleTyCon_name :: Boxity -> Int -> Name
63 tupleTyCon_name boxity n = getName (tupleTyCon boxity n)
64
65 extractHsTyVars :: RenamedHsType -> NameSet
66 extractHsTyVars x = filterNameSet isTyVarName (extractHsTyNames x)
67
68 extractHsTyNames   :: RenamedHsType -> NameSet
69 extractHsTyNames ty
70   = get ty
71   where
72     get (HsAppTy ty1 ty2)      = get ty1 `unionNameSets` get ty2
73     get (HsListTy ty)          = unitNameSet listTyCon_name `unionNameSets` get ty
74     get (HsTupleTy (HsTupCon n _) tys) = unitNameSet n
75                                          `unionNameSets` extractHsTyNames_s tys
76     get (HsFunTy ty1 ty2)      = get ty1 `unionNameSets` get ty2
77     get (HsPredTy p)           = extractHsPredTyNames p
78     get (HsUsgForAllTy uv ty)  = get ty
79     get (HsUsgTy u ty)         = get ty
80     get (HsOpTy ty1 tycon ty2) = get ty1 `unionNameSets` get ty2 `unionNameSets`
81                                  unitNameSet tycon
82     get (HsNumTy n)            = emptyNameSet
83     get (HsTyVar tv)           = unitNameSet tv
84     get (HsForAllTy (Just tvs) 
85                     ctxt ty)   = (extractHsCtxtTyNames ctxt `unionNameSets` get ty)
86                                             `minusNameSet`
87                                   mkNameSet (hsTyVarNames tvs)
88     get ty@(HsForAllTy Nothing _ _) = pprPanic "extractHsTyNames" (ppr ty)
89
90 extractHsTyNames_s  :: [RenamedHsType] -> NameSet
91 extractHsTyNames_s tys = foldr (unionNameSets . extractHsTyNames) emptyNameSet tys
92
93 extractHsCtxtTyNames :: RenamedContext -> NameSet
94 extractHsCtxtTyNames ctxt = foldr (unionNameSets . extractHsPredTyNames) emptyNameSet ctxt
95
96 -- You don't import or export implicit parameters,
97 -- so don't mention the IP names
98 extractHsPredTyNames (HsPClass cls tys)
99   = unitNameSet cls `unionNameSets` extractHsTyNames_s tys
100 extractHsPredTyNames (HsPIParam n ty)
101   = extractHsTyNames ty
102 \end{code}
103
104
105 %************************************************************************
106 %*                                                                      *
107 \subsection{A few functions on generic defintions
108 %*                                                                      *
109 %************************************************************************
110
111 These functions on generics are defined over RenamedMatches, which is
112 why they are here and not in HsMatches.
113
114 \begin{code}
115 maybeGenericMatch :: RenamedMatch -> Maybe (RenamedHsType, RenamedMatch)
116   -- Tells whether a Match is for a generic definition
117   -- and extract the type from a generic match and put it at the front
118
119 maybeGenericMatch (Match tvs (TypePatIn ty : pats) sig_ty grhss)
120   = Just (ty, Match tvs pats sig_ty grhss)
121
122 maybeGenericMatch other_match = Nothing
123 \end{code}