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 '=' '{' cons '}'
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 cons :: { [LConDecl RdrName] }
101 : {- empty -} { [] } -- 20060420 Empty data types allowed. jds
102 | con ';' cons { $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 "InlineMe" -> IfaceNote IfaceInlineMe $3
237 | '%external' STRING aty { IfaceFCall (ForeignCall.CCall
238 (CCallSpec (StaticTarget (mkFastString $2))
239 CCallConv (PlaySafe False)))
242 alts1 :: { [IfaceAlt] }
244 | alt ';' alts1 { $1:$3 }
247 : modid '.' d_pat_occ bndrs '->' exp
248 { (IfaceDataAlt $3, map ifaceBndrName $4, $6) }
249 -- The external syntax currently includes the types of the
250 -- the args, but they aren't needed internally
251 -- Nor is the module qualifier
253 { (IfaceLitAlt $1, [], $3) }
255 { (IfaceDefault, [], $3) }
258 : '(' INTEGER '::' aty ')' { convIntLit $2 $4 }
259 | '(' RATIONAL '::' aty ')' { convRatLit $2 $4 }
260 | '(' CHAR '::' aty ')' { MachChar $2 }
261 | '(' STRING '::' aty ')' { MachStr (mkFastString $2) }
263 tv_occ :: { OccName }
264 : NAME { mkOccName tvName $1 }
266 var_occ :: { OccName }
267 : NAME { mkVarOcc $1 }
271 q_tc_name :: { IfaceExtName }
272 : modid '.' CNAME { ExtPkg $1 (mkOccName tcName $3) }
274 -- Data constructor in a pattern or data type declaration; use the dataName,
275 -- because that's what we expect in Core case patterns
276 d_pat_occ :: { OccName }
277 : CNAME { mkOccName dataName $1 }
279 -- Data constructor occurrence in an expression;
280 -- use the varName because that's the worker Id
282 : CNAME { mkVarOcc $1 }
286 ifaceBndrName (IfaceIdBndr (n,_)) = n
287 ifaceBndrName (IfaceTvBndr (n,_)) = n
289 convIntLit :: Integer -> IfaceType -> Literal
290 convIntLit i (IfaceTyConApp tc [])
291 | tc `eqTc` intPrimTyCon = MachInt i
292 | tc `eqTc` wordPrimTyCon = MachWord i
293 | tc `eqTc` charPrimTyCon = MachChar (chr (fromInteger i))
294 | tc `eqTc` addrPrimTyCon && i == 0 = MachNullAddr
296 = pprPanic "Unknown integer literal type" (ppr aty)
298 convRatLit :: Rational -> IfaceType -> Literal
299 convRatLit r (IfaceTyConApp tc [])
300 | tc `eqTc` floatPrimTyCon = MachFloat r
301 | tc `eqTc` doublePrimTyCon = MachDouble r
303 = pprPanic "Unknown rational literal type" (ppr aty)
305 eqTc :: IfaceTyCon -> TyCon -> Bool -- Ugh!
306 eqTc (IfaceTc (ExtPkg mod occ)) tycon
307 = mod == nameModule nm && occ == nameOccName nm
311 -- Tiresomely, we have to generate both HsTypes (in type/class decls)
312 -- and IfaceTypes (in Core expressions). So we parse them as IfaceTypes,
313 -- and convert to HsTypes here. But the IfaceTypes we can see here
314 -- are very limited (see the productions for 'ty', so the translation
316 toHsType :: IfaceType -> LHsType RdrName
317 toHsType (IfaceTyVar v) = noLoc $ HsTyVar (mkRdrUnqual v)
318 toHsType (IfaceAppTy t1 t2) = noLoc $ HsAppTy (toHsType t1) (toHsType t2)
319 toHsType (IfaceFunTy t1 t2) = noLoc $ HsFunTy (toHsType t1) (toHsType t2)
320 toHsType (IfaceTyConApp (IfaceTc tc) ts) = foldl mkHsAppTy (noLoc $ HsTyVar (ifaceExtRdrName tc)) (map toHsType ts)
321 toHsType (IfaceForAllTy tv t) = add_forall (toHsTvBndr tv) (toHsType t)
323 toHsTvBndr :: IfaceTvBndr -> LHsTyVarBndr RdrName
324 toHsTvBndr (tv,k) = noLoc $ KindedTyVar (mkRdrUnqual tv) k
326 ifaceExtRdrName :: IfaceExtName -> RdrName
327 ifaceExtRdrName (ExtPkg mod occ) = mkOrig mod occ
328 ifaceExtRdrName other = pprPanic "ParserCore.ifaceExtRdrName" (ppr other)
330 add_forall tv (L _ (HsForAllTy exp tvs cxt t))
331 = noLoc $ HsForAllTy exp (tv:tvs) cxt t
333 = noLoc $ HsForAllTy Explicit [tv] (noLoc []) t
336 happyError s l = failP (show l ++ ": Parse error\n") (take 100 s) l