Follow OccName changes in ParserCore
[ghc-hetmet.git] / compiler / parser / ParserCore.y
1 {
2 {-# OPTIONS -w #-}
3 -- The above warning supression flag is a temporary kludge.
4 -- While working on this module you are encouraged to remove it and fix
5 -- any warnings in the module. See
6 --     http://hackage.haskell.org/trac/ghc/wiki/Commentary/CodingStyle#Warnings
7 -- for details
8
9 module ParserCore ( parseCore ) where
10
11 import IfaceSyn
12 import ForeignCall
13 import RdrHsSyn
14 import HsSyn
15 import RdrName
16 import OccName
17 import Type ( Kind,
18               liftedTypeKindTyCon, openTypeKindTyCon, unliftedTypeKindTyCon,
19               argTypeKindTyCon, ubxTupleKindTyCon, mkArrowKind, mkTyConApp
20             )
21 import Name( Name, nameOccName, nameModule, mkExternalName )
22 import Module
23 import ParserCoreUtils
24 import LexCore
25 import Literal
26 import SrcLoc
27 import TysPrim( wordPrimTyCon, intPrimTyCon, charPrimTyCon, 
28                 floatPrimTyCon, doublePrimTyCon, addrPrimTyCon )
29 import TyCon ( TyCon, tyConName )
30 import FastString
31 import Outputable
32 import Char
33 import Unique
34
35 #include "../HsVersions.h"
36
37 }
38
39 %name parseCore
40 %tokentype { Token }
41
42 %token
43  '%module'      { TKmodule }
44  '%data'        { TKdata }
45  '%newtype'     { TKnewtype }
46  '%forall'      { TKforall }
47  '%rec'         { TKrec }
48  '%let'         { TKlet }
49  '%in'          { TKin }
50  '%case'        { TKcase }
51  '%of'          { TKof }
52  '%cast'        { TKcast }
53  '%note'        { TKnote }
54  '%external'    { TKexternal }
55  '%local'       { TKlocal }
56  '%_'           { TKwild }
57  '('            { TKoparen }
58  ')'            { TKcparen }
59  '{'            { TKobrace }
60  '}'            { TKcbrace }
61  '#'            { TKhash}
62  '='            { TKeq }
63  ':'            { TKcolon }
64  '::'           { TKcoloncolon }
65  ':=:'          { TKcoloneqcolon }
66  '*'            { TKstar }
67  '->'           { TKrarrow }
68  '\\'           { TKlambda}
69  '@'            { TKat }
70  '.'            { TKdot }
71  '?'            { TKquestion}
72  ';'            { TKsemicolon }
73  NAME           { TKname $$ }
74  CNAME          { TKcname $$ }
75  INTEGER        { TKinteger $$ }
76  RATIONAL       { TKrational $$ }
77  STRING         { TKstring $$ }
78  CHAR           { TKchar $$ }
79
80 %monad { P } { thenP } { returnP }
81 %lexer { lexer } { TKEOF }
82
83 %%
84
85 module  :: { HsExtCore RdrName }
86         -- : '%module' modid tdefs vdefgs       { HsExtCore $2 $3 $4 }
87         : '%module' modid tdefs vdefgs  { HsExtCore $2 [] [] }
88
89
90 -------------------------------------------------------------
91 --     Names: the trickiest bit in here
92
93 -- A name of the form A.B.C could be:
94 --   module A.B.C
95 --   dcon C in module A.B
96 --   tcon C in module A.B
97 modid   :: { Module }
98         : NAME ':' mparts               { undefined }
99
100 q_dc_name :: { Name }
101           : NAME ':' mparts             { undefined }
102
103 q_tc_name :: { Name }
104           : NAME ':' mparts             { undefined }
105
106 q_var_occ :: { Name }
107           : NAME ':' vparts             { undefined }
108
109 mparts  :: { [String] }
110         : CNAME                         { [$1] }
111         | CNAME '.' mparts              { $1:$3 }
112
113 vparts  :: { [String] }
114         : var_occ                       { [$1] }
115         | CNAME '.' vparts              { $1:$3 }
116
117 -------------------------------------------------------------
118 --     Type and newtype declarations are in HsSyn syntax
119
120 tdefs   :: { [TyClDecl RdrName] }
121         : {- empty -}   {[]}
122         | tdef tdefs    {$1:$2}
123
124 tdef    :: { TyClDecl RdrName }
125         : '%data' q_tc_name tv_bndrs '=' '{' cons '}' ';'
126                 { mkTyData DataType ( noLoc []
127                                     , noLoc (ifaceExtRdrName $2)
128                                     , map toHsTvBndr $3
129                                     , Nothing
130                                     ) Nothing $6 Nothing }
131         | '%newtype' q_tc_name tv_bndrs trep ';'
132                 { let tc_rdr = ifaceExtRdrName $2 in
133                   mkTyData NewType ( noLoc []
134                                    , noLoc tc_rdr
135                                    , map toHsTvBndr $3
136                                    , Nothing
137                                    ) Nothing ($4 (rdrNameOcc tc_rdr)) Nothing }
138
139 -- For a newtype we have to invent a fake data constructor name
140 -- It doesn't matter what it is, because it won't be used
141 trep    :: { OccName -> [LConDecl RdrName] }
142         : {- empty -}   { (\ tc_occ -> []) }
143         | '=' ty        { (\ tc_occ -> let { dc_name  = mkRdrUnqual (setOccNameSpace dataName tc_occ) ;
144                                              con_info = PrefixCon [toHsType $2] }
145                                         in [noLoc $ ConDecl (noLoc dc_name) Explicit []
146                                            (noLoc []) con_info ResTyH98 Nothing]) }
147
148 cons    :: { [LConDecl RdrName] }
149         : {- empty -}   { [] } -- 20060420 Empty data types allowed. jds
150         | con           { [$1] }
151         | con ';' cons  { $1:$3 }
152
153 con     :: { LConDecl RdrName }
154         : d_pat_occ attv_bndrs hs_atys 
155                 { noLoc $ ConDecl (noLoc (mkRdrUnqual $1)) Explicit $2 (noLoc []) (PrefixCon $3) ResTyH98 Nothing }
156         | d_pat_occ '::' ty
157                 -- XXX - audreyt - $3 needs to be split into argument and return types!
158                 -- also not sure whether the [] below (quantified vars) appears.
159                 -- also the "PrefixCon []" is wrong.
160                 -- also we want to munge $3 somehow.
161                 -- extractWhatEver to unpack ty into the parts to ConDecl
162                 -- XXX - define it somewhere in RdrHsSyn
163                 { noLoc $ ConDecl (noLoc (mkRdrUnqual $1)) Explicit [] (noLoc []) (PrefixCon []) (undefined $3) Nothing }
164
165 attv_bndrs :: { [LHsTyVarBndr RdrName] }
166         : {- empty -}            { [] }
167         | '@' tv_bndr attv_bndrs {  toHsTvBndr $2 : $3 }
168
169 hs_atys :: { [LHsType RdrName] }
170          : atys               { map toHsType $1 }
171
172
173 ---------------------------------------
174 --                 Types
175 ---------------------------------------
176
177 atys    :: { [IfaceType] }
178         : {- empty -}   { [] }
179         | aty atys      { $1:$2 }
180
181 aty     :: { IfaceType }
182         : fs_var_occ { IfaceTyVar $1 }
183         | q_tc_name  { IfaceTyConApp (IfaceTc $1) [] }
184         | '(' ty ')' { $2 }
185
186 bty     :: { IfaceType }
187         : fs_var_occ atys { foldl IfaceAppTy (IfaceTyVar $1) $2 }
188         | q_var_occ atys  { undefined }
189         | q_tc_name atys  { IfaceTyConApp (IfaceTc $1) $2 }
190         | '(' ty ')' { $2 }
191
192 ty      :: { IfaceType }
193         : bty                        { $1 }
194         | bty '->' ty                { IfaceFunTy $1 $3 }
195         | '%forall' tv_bndrs '.' ty  { foldr IfaceForAllTy $4 $2 }
196
197 ----------------------------------------------
198 --        Bindings are in Iface syntax
199
200 vdefgs  :: { [IfaceBinding] }
201         : {- empty -}           { [] }
202         | let_bind ';' vdefgs   { $1 : $3 }
203
204 let_bind :: { IfaceBinding }
205         : '%rec' '{' vdefs1 '}' { IfaceRec $3 } -- Can be empty. Do we care?
206         |  vdef                 { let (b,r) = $1
207                                   in IfaceNonRec b r }
208
209 vdefs1  :: { [(IfaceLetBndr, IfaceExpr)] }
210         : vdef                  { [$1] }
211         | vdef ';' vdefs1       { $1:$3 }
212
213 vdef    :: { (IfaceLetBndr, IfaceExpr) }
214         : fs_var_occ '::' ty '=' exp { (IfLetBndr $1 $3 NoInfo, $5) }
215         | '%local' vdef              { $2 }
216
217   -- NB: qd_occ includes data constructors, because
218   --     we allow data-constructor wrappers at top level
219   -- But we discard the module name, because it must be the
220   -- same as the module being compiled, and Iface syntax only
221   -- has OccNames in binding positions. Ah, but it has Names now!
222
223 ---------------------------------------
224 --  Binders
225 bndr    :: { IfaceBndr }
226         : '@' tv_bndr   { IfaceTvBndr $2 }
227         | id_bndr       { IfaceIdBndr $1 }
228
229 bndrs   :: { [IfaceBndr] }
230         : bndr          { [$1] }
231         | bndr bndrs    { $1:$2 }
232
233 id_bndr :: { IfaceIdBndr }
234         : '(' fs_var_occ '::' ty ')'    { ($2,$4) }
235
236 tv_bndr :: { IfaceTvBndr }
237         :  fs_var_occ                    { ($1, ifaceLiftedTypeKind) }
238         |  '(' fs_var_occ '::' akind ')' { ($2, $4) }
239
240 tv_bndrs        :: { [IfaceTvBndr] }
241         : {- empty -}   { [] }
242         | tv_bndr tv_bndrs      { $1:$2 }
243
244 akind   :: { IfaceKind }
245         : '*'              { ifaceLiftedTypeKind }      
246         | '#'              { ifaceUnliftedTypeKind }
247         | '?'              { ifaceOpenTypeKind }
248         | '(' kind ')'     { $2 }
249
250 kind    :: { IfaceKind }
251         : akind            { $1 }
252         | akind '->' kind  { ifaceArrow $1 $3 }
253         | ty ':=:' ty      { ifaceEq $1 $3 }
254
255 -----------------------------------------
256 --             Expressions
257
258 aexp    :: { IfaceExpr }
259         : fs_var_occ    { IfaceLcl $1 }
260         | q_var_occ     { IfaceExt $1 }
261         | q_dc_name     { IfaceExt $1 }
262         | lit           { IfaceLit $1 }
263         | '(' exp ')'   { $2 }
264
265 fexp    :: { IfaceExpr }
266         : fexp aexp     { IfaceApp $1 $2 }
267         | fexp '@' aty  { IfaceApp $1 (IfaceType $3) }
268         | aexp          { $1 }
269
270 exp     :: { IfaceExpr }
271         : fexp                        { $1 }
272         | '\\' bndrs '->' exp         { foldr IfaceLam $4 $2 }
273         | '%let' let_bind '%in' exp   { IfaceLet $2 $4 }
274 -- gaw 2004
275         | '%case' '(' ty ')' aexp '%of' id_bndr
276           '{' alts1 '}'               { IfaceCase $5 (fst $7) $3 $9 }
277         | '%cast' aexp aty { IfaceCast $2 $3 }
278         | '%note' STRING exp       
279             { case $2 of
280                --"SCC"      -> IfaceNote (IfaceSCC "scc") $3
281                "InlineMe"   -> IfaceNote IfaceInlineMe $3
282             }
283         | '%external' STRING aty   { IfaceFCall (ForeignCall.CCall 
284                                                     (CCallSpec (StaticTarget (mkFastString $2)) 
285                                                                CCallConv (PlaySafe False))) 
286                                                  $3 }
287
288 alts1   :: { [IfaceAlt] }
289         : alt           { [$1] }
290         | alt ';' alts1 { $1:$3 }
291
292 alt     :: { IfaceAlt }
293         : q_dc_name bndrs '->' exp 
294                 { (IfaceDataAlt $1, map ifaceBndrName $2, $4) } 
295                        -- The external syntax currently includes the types of the
296                        -- the args, but they aren't needed internally
297                        -- Nor is the module qualifier
298         | q_dc_name '->' exp 
299                 { (IfaceDataAlt $1, [], $3) } 
300         | lit '->' exp
301                 { (IfaceLitAlt $1, [], $3) }
302         | '%_' '->' exp
303                 { (IfaceDefault, [], $3) }
304
305 lit     :: { Literal }
306         : '(' INTEGER '::' aty ')'      { convIntLit $2 $4 }
307         | '(' RATIONAL '::' aty ')'     { convRatLit $2 $4 }
308         | '(' CHAR '::' aty ')'         { MachChar $2 }
309         | '(' STRING '::' aty ')'       { MachStr (mkFastString $2) }
310
311 fs_var_occ      :: { FastString }
312                 : NAME  { mkFastString $1 }
313
314 var_occ :: { String }
315         : NAME  { $1 }
316
317
318 -- Data constructor in a pattern or data type declaration; use the dataName, 
319 -- because that's what we expect in Core case patterns
320 d_pat_occ :: { OccName }
321         : CNAME      { mkOccName dataName $1 }
322
323 {
324
325 ifaceKind kc = IfaceTyConApp kc []
326
327 ifaceBndrName (IfaceIdBndr (n,_)) = n
328 ifaceBndrName (IfaceTvBndr (n,_)) = n
329
330 convIntLit :: Integer -> IfaceType -> Literal
331 convIntLit i (IfaceTyConApp tc [])
332   | tc `eqTc` intPrimTyCon  = MachInt  i  
333   | tc `eqTc` wordPrimTyCon = MachWord i
334   | tc `eqTc` charPrimTyCon = MachChar (chr (fromInteger i))
335   | tc `eqTc` addrPrimTyCon && i == 0 = MachNullAddr
336 convIntLit i aty
337   = pprPanic "Unknown integer literal type" (ppr aty)
338
339 convRatLit :: Rational -> IfaceType -> Literal
340 convRatLit r (IfaceTyConApp tc [])
341   | tc `eqTc` floatPrimTyCon  = MachFloat  r
342   | tc `eqTc` doublePrimTyCon = MachDouble r
343 convRatLit i aty
344   = pprPanic "Unknown rational literal type" (ppr aty)
345
346 eqTc :: IfaceTyCon -> TyCon -> Bool   -- Ugh!
347 eqTc (IfaceTc name) tycon = name == tyConName tycon
348
349 -- Tiresomely, we have to generate both HsTypes (in type/class decls) 
350 -- and IfaceTypes (in Core expressions).  So we parse them as IfaceTypes,
351 -- and convert to HsTypes here.  But the IfaceTypes we can see here
352 -- are very limited (see the productions for 'ty', so the translation
353 -- isn't hard
354 toHsType :: IfaceType -> LHsType RdrName
355 toHsType (IfaceTyVar v)                  = noLoc $ HsTyVar (mkRdrUnqual (mkTyVarOccFS v))
356 toHsType (IfaceAppTy t1 t2)              = noLoc $ HsAppTy (toHsType t1) (toHsType t2)
357 toHsType (IfaceFunTy t1 t2)              = noLoc $ HsFunTy (toHsType t1) (toHsType t2)
358 toHsType (IfaceTyConApp (IfaceTc tc) ts) = foldl mkHsAppTy (noLoc $ HsTyVar (ifaceExtRdrName tc)) (map toHsType ts) 
359 toHsType (IfaceForAllTy tv t)            = add_forall (toHsTvBndr tv) (toHsType t)
360
361 -- We also need to convert IfaceKinds to Kinds (now that they are different).
362 -- Only a limited form of kind will be encountered... hopefully
363 toKind :: IfaceKind -> Kind
364 toKind (IfaceFunTy ifK1 ifK2)  = mkArrowKind (toKind ifK1) (toKind ifK2)
365 toKind (IfaceTyConApp ifKc []) = mkTyConApp (toKindTc ifKc) []
366 toKind other                   = pprPanic "toKind" (ppr other)
367
368 toKindTc :: IfaceTyCon -> TyCon
369 toKindTc IfaceLiftedTypeKindTc   = liftedTypeKindTyCon
370 toKindTc IfaceOpenTypeKindTc     = openTypeKindTyCon
371 toKindTc IfaceUnliftedTypeKindTc = unliftedTypeKindTyCon
372 toKindTc IfaceUbxTupleKindTc     = ubxTupleKindTyCon
373 toKindTc IfaceArgTypeKindTc      = argTypeKindTyCon
374 toKindTc other                   = pprPanic "toKindTc" (ppr other)
375
376 ifaceTcType ifTc = IfaceTyConApp ifTc []
377
378 ifaceLiftedTypeKind   = ifaceTcType IfaceLiftedTypeKindTc
379 ifaceOpenTypeKind     = ifaceTcType IfaceOpenTypeKindTc
380 ifaceUnliftedTypeKind = ifaceTcType IfaceUnliftedTypeKindTc
381
382 ifaceArrow ifT1 ifT2 = IfaceFunTy ifT1 ifT2
383
384 ifaceEq ifT1 ifT2 = IfacePredTy (IfaceEqPred ifT1 ifT2)
385
386 toHsTvBndr :: IfaceTvBndr -> LHsTyVarBndr RdrName
387 toHsTvBndr (tv,k) = noLoc $ KindedTyVar (mkRdrUnqual (mkTyVarOccFS tv)) (toKind k)
388
389 ifaceExtRdrName :: Name -> RdrName
390 ifaceExtRdrName name = mkOrig (nameModule name) (nameOccName name)
391 ifaceExtRdrName other = pprPanic "ParserCore.ifaceExtRdrName" (ppr other)
392
393 add_forall tv (L _ (HsForAllTy exp tvs cxt t))
394   = noLoc $ HsForAllTy exp (tv:tvs) cxt t
395 add_forall tv t
396   = noLoc $ HsForAllTy Explicit [tv] (noLoc []) t
397   
398 happyError :: P a 
399 happyError s l = failP (show l ++ ": Parse error\n") (take 100 s) l
400 }
401