[project @ 1997-05-19 00:12:10 by sof]
[ghc-hetmet.git] / ghc / compiler / reader / RdrHsSyn.lhs
1 %
2 % (c) The AQUA Project, Glasgow University, 1996
3 %
4 \section[RdrHsSyn]{Specialisations of the @HsSyn@ syntax for the reader}
5
6 (Well, really, for specialisations involving @RdrName@s, even if
7 they are used somewhat later on in the compiler...)
8
9 \begin{code}
10 #include "HsVersions.h"
11
12 module RdrHsSyn (
13         SYN_IE(RdrNameArithSeqInfo),
14         SYN_IE(RdrNameBangType),
15         SYN_IE(RdrNameClassDecl),
16         SYN_IE(RdrNameClassOpSig),
17         SYN_IE(RdrNameConDecl),
18         SYN_IE(RdrNameContext),
19         SYN_IE(RdrNameSpecDataSig),
20         SYN_IE(RdrNameDefaultDecl),
21         SYN_IE(RdrNameFixityDecl),
22         SYN_IE(RdrNameGRHS),
23         SYN_IE(RdrNameGRHSsAndBinds),
24         SYN_IE(RdrNameHsBinds),
25         SYN_IE(RdrNameHsDecl),
26         SYN_IE(RdrNameHsExpr),
27         SYN_IE(RdrNameHsModule),
28         SYN_IE(RdrNameIE),
29         SYN_IE(RdrNameImportDecl),
30         SYN_IE(RdrNameInstDecl),
31         SYN_IE(RdrNameMatch),
32         SYN_IE(RdrNameMonoBinds),
33         SYN_IE(RdrNamePat),
34         SYN_IE(RdrNameHsType),
35         SYN_IE(RdrNameSig),
36         SYN_IE(RdrNameSpecInstSig),
37         SYN_IE(RdrNameStmt),
38         SYN_IE(RdrNameTyDecl),
39
40         SYN_IE(RdrNameClassOpPragmas),
41         SYN_IE(RdrNameClassPragmas),
42         SYN_IE(RdrNameDataPragmas),
43         SYN_IE(RdrNameGenPragmas),
44         SYN_IE(RdrNameInstancePragmas),
45         SYN_IE(RdrNameCoreExpr),
46         extractHsTyVars,
47
48         RdrName(..),
49         qual, varQual, tcQual, varUnqual,
50         dummyRdrVarName, dummyRdrTcName,
51         isUnqual, isQual,
52         showRdr, rdrNameOcc, ieOcc,
53         cmpRdr, prefixRdrName,
54         mkOpApp
55
56     ) where
57
58 IMP_Ubiq()
59
60 import HsSyn
61 import Lex
62 import PrelMods         ( pRELUDE )
63 import Name     {-      ( ExportFlag(..), Module(..), pprModule,
64                           OccName(..), pprOccName, prefixOccName ) -}
65 import Pretty           
66 import PprStyle         ( PprStyle(..) )
67 import Util             --( cmpPString, panic, thenCmp )
68 import Outputable
69 #if __GLASGOW_HASKELL__ >= 202
70 import CoreSyn   ( GenCoreExpr )
71 import HsPragmas ( GenPragmas, ClassPragmas, DataPragmas, ClassOpPragmas, InstancePragmas )
72 #endif
73 \end{code}
74
75 \begin{code}
76 type RdrNameArithSeqInfo        = ArithSeqInfo          Fake Fake RdrName RdrNamePat
77 type RdrNameBangType            = BangType              RdrName
78 type RdrNameClassDecl           = ClassDecl             Fake Fake RdrName RdrNamePat
79 type RdrNameClassOpSig          = Sig                   RdrName
80 type RdrNameConDecl             = ConDecl               RdrName
81 type RdrNameContext             = Context               RdrName
82 type RdrNameHsDecl              = HsDecl                Fake Fake RdrName RdrNamePat
83 type RdrNameSpecDataSig         = SpecDataSig           RdrName
84 type RdrNameDefaultDecl         = DefaultDecl           RdrName
85 type RdrNameFixityDecl          = FixityDecl            RdrName
86 type RdrNameGRHS                = GRHS                  Fake Fake RdrName RdrNamePat
87 type RdrNameGRHSsAndBinds       = GRHSsAndBinds         Fake Fake RdrName RdrNamePat
88 type RdrNameHsBinds             = HsBinds               Fake Fake RdrName RdrNamePat
89 type RdrNameHsExpr              = HsExpr                Fake Fake RdrName RdrNamePat
90 type RdrNameHsModule            = HsModule              Fake Fake RdrName RdrNamePat
91 type RdrNameIE                  = IE                    RdrName
92 type RdrNameImportDecl          = ImportDecl            RdrName
93 type RdrNameInstDecl            = InstDecl              Fake Fake RdrName RdrNamePat
94 type RdrNameMatch               = Match                 Fake Fake RdrName RdrNamePat
95 type RdrNameMonoBinds           = MonoBinds             Fake Fake RdrName RdrNamePat
96 type RdrNamePat                 = InPat                 RdrName
97 type RdrNameHsType              = HsType                RdrName
98 type RdrNameSig                 = Sig                   RdrName
99 type RdrNameSpecInstSig         = SpecInstSig           RdrName
100 type RdrNameStmt                = Stmt                  Fake Fake RdrName RdrNamePat
101 type RdrNameTyDecl              = TyDecl                RdrName
102
103 type RdrNameClassOpPragmas      = ClassOpPragmas        RdrName
104 type RdrNameClassPragmas        = ClassPragmas          RdrName
105 type RdrNameDataPragmas         = DataPragmas           RdrName
106 type RdrNameGenPragmas          = GenPragmas            RdrName
107 type RdrNameInstancePragmas     = InstancePragmas       RdrName
108 type RdrNameCoreExpr            = GenCoreExpr           RdrName RdrName RdrName RdrName 
109 \end{code}
110
111 @extractHsTyVars@ looks just for things that could be type variables.
112 It's used when making the for-alls explicit.
113
114 \begin{code}
115 extractHsTyVars :: HsType RdrName -> [RdrName]
116 extractHsTyVars ty
117   = get ty []
118   where
119     get (MonoTyApp ty1 ty2)      acc = get ty1 (get ty2 acc)
120     get (MonoListTy tc ty)       acc = get ty acc
121     get (MonoTupleTy tc tys)     acc = foldr get acc tys
122     get (MonoFunTy ty1 ty2)      acc = get ty1 (get ty2 acc)
123     get (MonoDictTy cls ty)      acc = get ty acc
124     get (MonoTyVar tv)           acc = insert tv acc
125     get (HsPreForAllTy ctxt ty)  acc = foldr (get . snd) (get ty acc) ctxt
126     get (HsForAllTy tvs ctxt ty) acc = filter (`notElem` locals) $
127                                        foldr (get . snd) (get ty acc) ctxt
128                                      where
129                                        locals = map getTyVarName tvs
130
131     insert (Qual _ _)         acc = acc
132     insert (Unqual (TCOcc _)) acc = acc
133     insert other              acc | other `elem` acc = acc
134                                   | otherwise        = other : acc
135 \end{code}
136
137
138 A useful function for building @OpApps@.  The operator is always a variable,
139 and we don't know the fixity yet.
140
141 \begin{code}
142 mkOpApp e1 op e2 = OpApp e1 (HsVar op) (error "mkOpApp:fixity") e2
143 \end{code}
144
145
146 %************************************************************************
147 %*                                                                      *
148 \subsection[RdrName]{The @RdrName@ datatype; names read from files}
149 %*                                                                      *
150 %************************************************************************
151
152 \begin{code}
153 data RdrName
154   = Unqual OccName
155   | Qual   Module OccName
156
157 qual     (m,n) = Qual m n
158 tcQual   (m,n) = Qual m (TCOcc n)
159 varQual  (m,n) = Qual m (VarOcc n)
160
161         -- This guy is used by the reader when HsSyn has a slot for
162         -- an implicit name that's going to be filled in by
163         -- the renamer.  We can't just put "error..." because
164         -- we sometimes want to print out stuff after reading but
165         -- before renaming
166 dummyRdrVarName = Unqual (VarOcc SLIT("V-DUMMY"))
167 dummyRdrTcName = Unqual (VarOcc SLIT("TC-DUMMY"))
168
169 varUnqual n = Unqual (VarOcc n)
170
171 isUnqual (Unqual _) = True
172 isUnqual (Qual _ _) = False
173
174 isQual (Unqual _) = False
175 isQual (Qual _ _) = True
176
177         -- Used for adding a prefix to a RdrName
178 prefixRdrName :: FAST_STRING -> RdrName -> RdrName
179 prefixRdrName prefix (Qual m n) = Qual m (prefixOccName prefix n)
180 prefixRdrName prefix (Unqual n) = Unqual (prefixOccName prefix n)
181
182 cmpRdr (Unqual  n1) (Unqual  n2) = n1 `cmp` n2
183 cmpRdr (Unqual  n1) (Qual m2 n2) = LT_
184 cmpRdr (Qual m1 n1) (Unqual  n2) = GT_
185 cmpRdr (Qual m1 n1) (Qual m2 n2) = (n1 `cmp` n2) `thenCmp` (_CMP_STRING_ m1 m2)
186                                    -- always compare module-names *second*
187
188 rdrNameOcc :: RdrName -> OccName
189 rdrNameOcc (Unqual occ) = occ
190 rdrNameOcc (Qual _ occ) = occ
191
192 ieOcc :: RdrNameIE -> OccName
193 ieOcc ie = rdrNameOcc (ieName ie)
194
195 instance Text RdrName where -- debugging
196     showsPrec _ rn = showString (show (ppr PprDebug rn))
197
198 instance Eq RdrName where
199     a == b = case (a `cmp` b) of { EQ_ -> True;  _ -> False }
200     a /= b = case (a `cmp` b) of { EQ_ -> False; _ -> True }
201
202 instance Ord RdrName where
203     a <= b = case (a `cmp` b) of { LT_ -> True;  EQ_ -> True;  GT__ -> False }
204     a <  b = case (a `cmp` b) of { LT_ -> True;  EQ_ -> False; GT__ -> False }
205     a >= b = case (a `cmp` b) of { LT_ -> False; EQ_ -> True;  GT__ -> True  }
206     a >  b = case (a `cmp` b) of { LT_ -> False; EQ_ -> False; GT__ -> True  }
207
208 instance Ord3 RdrName where
209     cmp = cmpRdr
210
211 instance Outputable RdrName where
212     ppr sty (Unqual n) = pprQuote sty $ \ sty -> pprOccName sty n
213     ppr sty (Qual m n) = pprQuote sty $ \ sty -> hcat [pprModule sty m, char '.', pprOccName sty n]
214
215 instance NamedThing RdrName where               -- Just so that pretty-printing of expressions works
216     getOccName = rdrNameOcc
217     getName = panic "no getName for RdrNames"
218
219 showRdr sty rdr = render (ppr sty rdr)
220 \end{code}
221