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