2 module ParserCore ( parseCore ) where
10 import Kind( Kind(..) )
11 import Name( nameOccName, nameModule )
13 import ParserCoreUtils
17 import TysPrim( wordPrimTyCon, intPrimTyCon, charPrimTyCon,
18 floatPrimTyCon, doublePrimTyCon, addrPrimTyCon )
19 import TyCon ( TyCon, tyConName )
24 #include "../HsVersions.h"
32 '%module' { TKmodule }
34 '%newtype' { TKnewtype }
35 '%forall' { TKforall }
41 '%coerce' { TKcoerce }
43 '%external' { TKexternal }
61 INTEGER { TKinteger $$ }
62 RATIONAL { TKrational $$ }
63 STRING { TKstring $$ }
66 %monad { P } { thenP } { returnP }
67 %lexer { lexer } { TKEOF }
71 module :: { HsExtCore RdrName }
72 : '%module' modid tdefs vdefgs { HsExtCore $2 $3 $4 }
75 : CNAME { mkModuleFS (mkFastString $1) }
77 -------------------------------------------------------------
78 -- Type and newtype declarations are in HsSyn syntax
80 tdefs :: { [TyClDecl RdrName] }
82 | tdef ';' tdefs {$1:$3}
84 tdef :: { TyClDecl RdrName }
85 : '%data' q_tc_name tv_bndrs '=' '{' cons1 '}'
86 { mkTyData DataType (noLoc [], noLoc (ifaceExtRdrName $2), map toHsTvBndr $3) Nothing $6 Nothing }
87 | '%newtype' q_tc_name tv_bndrs trep
88 { let tc_rdr = ifaceExtRdrName $2 in
89 mkTyData NewType (noLoc [], noLoc tc_rdr, map toHsTvBndr $3) Nothing ($4 (rdrNameOcc tc_rdr)) Nothing }
91 -- For a newtype we have to invent a fake data constructor name
92 -- It doesn't matter what it is, because it won't be used
93 trep :: { OccName -> [LConDecl RdrName] }
94 : {- empty -} { (\ tc_occ -> []) }
95 | '=' ty { (\ tc_occ -> let { dc_name = mkRdrUnqual (setOccNameSpace dataName tc_occ) ;
96 con_info = PrefixCon [toHsType $2] }
97 in [noLoc $ ConDecl (noLoc dc_name) Explicit []
98 (noLoc []) con_info ResTyH98]) }
100 cons1 :: { [LConDecl RdrName] }
102 | con ';' cons1 { $1:$3 }
104 con :: { LConDecl RdrName }
105 : d_pat_occ attv_bndrs hs_atys
106 { noLoc $ ConDecl (noLoc (mkRdrUnqual $1)) Explicit $2 (noLoc []) (PrefixCon $3) ResTyH98}
108 -- XXX - autrijus - $3 needs to be split into argument and return types!
109 -- also not sure whether the [] below (quantified vars) appears.
110 -- also the "PrefixCon []" is wrong.
111 -- also we want to munge $3 somehow.
112 -- extractWhatEver to unpack ty into the parts to ConDecl
113 -- XXX - define it somewhere in RdrHsSyn
114 { noLoc $ ConDecl (noLoc (mkRdrUnqual $1)) Explicit [] (noLoc []) (PrefixCon []) (undefined $3) }
116 attv_bndrs :: { [LHsTyVarBndr RdrName] }
118 | '@' tv_bndr attv_bndrs { toHsTvBndr $2 : $3 }
120 hs_atys :: { [LHsType RdrName] }
121 : atys { map toHsType $1 }
124 ---------------------------------------
126 ---------------------------------------
128 atys :: { [IfaceType] }
133 : tv_occ { IfaceTyVar $1 }
134 | q_tc_name { IfaceTyConApp (IfaceTc $1) [] }
138 : tv_occ atys { foldl IfaceAppTy (IfaceTyVar $1) $2 }
139 | q_tc_name atys { IfaceTyConApp (IfaceTc $1) $2 }
144 | bty '->' ty { IfaceFunTy $1 $3 }
145 | '%forall' tv_bndrs '.' ty { foldr IfaceForAllTy $4 $2 }
147 ----------------------------------------------
148 -- Bindings are in Iface syntax
150 vdefgs :: { [IfaceBinding] }
152 | let_bind ';' vdefgs { $1 : $3 }
154 let_bind :: { IfaceBinding }
155 : '%rec' '{' vdefs1 '}' { IfaceRec $3 }
156 | vdef { let (b,r) = $1
159 vdefs1 :: { [(IfaceIdBndr, IfaceExpr)] }
161 | vdef ';' vdefs1 { $1:$3 }
163 vdef :: { (IfaceIdBndr, IfaceExpr) }
164 : qd_occ '::' ty '=' exp { (($1, $3), $5) }
165 -- NB: qd_occ includes data constructors, because
166 -- we allow data-constructor wrappers at top level
167 -- But we discard the module name, because it must be the
168 -- same as the module being compiled, and Iface syntax only
169 -- has OccNames in binding positions
171 qd_occ :: { OccName }
175 ---------------------------------------
177 bndr :: { IfaceBndr }
178 : '@' tv_bndr { IfaceTvBndr $2 }
179 | id_bndr { IfaceIdBndr $1 }
181 bndrs :: { [IfaceBndr] }
183 | bndr bndrs { $1:$2 }
185 id_bndr :: { IfaceIdBndr }
186 : '(' var_occ '::' ty ')' { ($2,$4) }
188 id_bndrs :: { [IfaceIdBndr] }
190 | id_bndr id_bndrs { $1:$2 }
192 tv_bndr :: { IfaceTvBndr }
193 : tv_occ { ($1, LiftedTypeKind) }
194 | '(' tv_occ '::' akind ')' { ($2, $4) }
196 tv_bndrs :: { [IfaceTvBndr] }
198 | tv_bndr tv_bndrs { $1:$2 }
200 akind :: { IfaceKind }
201 : '*' { LiftedTypeKind }
202 | '#' { UnliftedTypeKind }
203 | '?' { OpenTypeKind }
204 | '(' kind ')' { $2 }
206 kind :: { IfaceKind }
208 | akind '->' kind { FunKind $1 $3 }
210 -----------------------------------------
213 aexp :: { IfaceExpr }
214 : var_occ { IfaceLcl $1 }
215 | modid '.' qd_occ { IfaceExt (ExtPkg $1 $3) }
216 | lit { IfaceLit $1 }
219 fexp :: { IfaceExpr }
220 : fexp aexp { IfaceApp $1 $2 }
221 | fexp '@' aty { IfaceApp $1 (IfaceType $3) }
226 | '\\' bndrs '->' exp { foldr IfaceLam $4 $2 }
227 | '%let' let_bind '%in' exp { IfaceLet $2 $4 }
229 | '%case' '(' ty ')' aexp '%of' id_bndr
230 '{' alts1 '}' { IfaceCase $5 (fst $7) $3 $9 }
231 | '%coerce' aty exp { IfaceNote (IfaceCoerce $2) $3 }
234 --"SCC" -> IfaceNote (IfaceSCC "scc") $3
235 "InlineCall" -> IfaceNote IfaceInlineCall $3
236 "InlineMe" -> IfaceNote IfaceInlineMe $3
238 | '%external' STRING aty { IfaceFCall (ForeignCall.CCall
239 (CCallSpec (StaticTarget (mkFastString $2))
240 CCallConv (PlaySafe False)))
243 alts1 :: { [IfaceAlt] }
245 | alt ';' alts1 { $1:$3 }
248 : modid '.' d_pat_occ bndrs '->' exp
249 { (IfaceDataAlt $3, map ifaceBndrName $4, $6) }
250 -- The external syntax currently includes the types of the
251 -- the args, but they aren't needed internally
252 -- Nor is the module qualifier
254 { (IfaceLitAlt $1, [], $3) }
256 { (IfaceDefault, [], $3) }
259 : '(' INTEGER '::' aty ')' { convIntLit $2 $4 }
260 | '(' RATIONAL '::' aty ')' { convRatLit $2 $4 }
261 | '(' CHAR '::' aty ')' { MachChar $2 }
262 | '(' STRING '::' aty ')' { MachStr (mkFastString $2) }
264 tv_occ :: { OccName }
265 : NAME { mkOccName tvName $1 }
267 var_occ :: { OccName }
268 : NAME { mkVarOcc $1 }
272 q_tc_name :: { IfaceExtName }
273 : modid '.' CNAME { ExtPkg $1 (mkOccName tcName $3) }
275 -- Data constructor in a pattern or data type declaration; use the dataName,
276 -- because that's what we expect in Core case patterns
277 d_pat_occ :: { OccName }
278 : CNAME { mkOccName dataName $1 }
280 -- Data constructor occurrence in an expression;
281 -- use the varName because that's the worker Id
283 : CNAME { mkVarOcc $1 }
287 ifaceBndrName (IfaceIdBndr (n,_)) = n
288 ifaceBndrName (IfaceTvBndr (n,_)) = n
290 convIntLit :: Integer -> IfaceType -> Literal
291 convIntLit i (IfaceTyConApp tc [])
292 | tc `eqTc` intPrimTyCon = MachInt i
293 | tc `eqTc` wordPrimTyCon = MachWord i
294 | tc `eqTc` charPrimTyCon = MachChar (chr (fromInteger i))
295 | tc `eqTc` addrPrimTyCon && i == 0 = MachNullAddr
297 = pprPanic "Unknown integer literal type" (ppr aty)
299 convRatLit :: Rational -> IfaceType -> Literal
300 convRatLit r (IfaceTyConApp tc [])
301 | tc `eqTc` floatPrimTyCon = MachFloat r
302 | tc `eqTc` doublePrimTyCon = MachDouble r
304 = pprPanic "Unknown rational literal type" (ppr aty)
306 eqTc :: IfaceTyCon -> TyCon -> Bool -- Ugh!
307 eqTc (IfaceTc (ExtPkg mod occ)) tycon
308 = mod == nameModule nm && occ == nameOccName nm
312 -- Tiresomely, we have to generate both HsTypes (in type/class decls)
313 -- and IfaceTypes (in Core expressions). So we parse them as IfaceTypes,
314 -- and convert to HsTypes here. But the IfaceTypes we can see here
315 -- are very limited (see the productions for 'ty', so the translation
317 toHsType :: IfaceType -> LHsType RdrName
318 toHsType (IfaceTyVar v) = noLoc $ HsTyVar (mkRdrUnqual v)
319 toHsType (IfaceAppTy t1 t2) = noLoc $ HsAppTy (toHsType t1) (toHsType t2)
320 toHsType (IfaceFunTy t1 t2) = noLoc $ HsFunTy (toHsType t1) (toHsType t2)
321 toHsType (IfaceTyConApp (IfaceTc tc) ts) = foldl mkHsAppTy (noLoc $ HsTyVar (ifaceExtRdrName tc)) (map toHsType ts)
322 toHsType (IfaceForAllTy tv t) = add_forall (toHsTvBndr tv) (toHsType t)
324 toHsTvBndr :: IfaceTvBndr -> LHsTyVarBndr RdrName
325 toHsTvBndr (tv,k) = noLoc $ KindedTyVar (mkRdrUnqual tv) k
327 ifaceExtRdrName :: IfaceExtName -> RdrName
328 ifaceExtRdrName (ExtPkg mod occ) = mkOrig mod occ
329 ifaceExtRdrName other = pprPanic "ParserCore.ifaceExtRdrName" (ppr other)
331 add_forall tv (L _ (HsForAllTy exp tvs cxt t))
332 = noLoc $ HsForAllTy exp (tv:tvs) cxt t
334 = noLoc $ HsForAllTy Explicit [tv] (noLoc []) t
337 happyError s l = failP (show l ++ ": Parse error\n") (take 100 s) l