2 module ParserCore ( parseCore ) where
10 import Kind( Kind(..) )
11 import Name( nameOccName, nameModuleName )
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
73 { HsExtCore (mkHomeModule $2) $3 $4 }
75 modid :: { ModuleName }
76 : CNAME { mkSysModuleNameFS (mkFastString $1) }
78 -------------------------------------------------------------
79 -- Type and newtype declarations are in HsSyn syntax
81 tdefs :: { [TyClDecl RdrName] }
83 | tdef ';' tdefs {$1:$3}
85 tdef :: { TyClDecl RdrName }
86 : '%data' q_tc_name tv_bndrs '=' '{' cons1 '}'
87 { mkTyData DataType (noLoc [], noLoc (ifaceExtRdrName $2), map toHsTvBndr $3) $6 Nothing }
88 | '%newtype' q_tc_name tv_bndrs trep
89 { let tc_rdr = ifaceExtRdrName $2 in
90 mkTyData NewType (noLoc [], noLoc tc_rdr, map toHsTvBndr $3) ($4 (rdrNameOcc tc_rdr)) Nothing }
92 -- For a newtype we have to invent a fake data constructor name
93 -- It doesn't matter what it is, because it won't be used
94 trep :: { OccName -> [LConDecl RdrName] }
95 : {- empty -} { (\ tc_occ -> []) }
96 | '=' ty { (\ tc_occ -> let { dc_name = mkRdrUnqual (setOccNameSpace dataName tc_occ) ;
97 con_info = PrefixCon [toHsType $2] }
98 in [noLoc $ ConDecl (noLoc dc_name) []
99 (noLoc []) con_info]) }
101 cons1 :: { [LConDecl RdrName] }
103 | con ';' cons1 { $1:$3 }
105 con :: { LConDecl RdrName }
106 : d_pat_occ attv_bndrs hs_atys
107 { noLoc $ ConDecl (noLoc (mkRdrUnqual $1)) $2 (noLoc []) (PrefixCon $3)}
109 { noLoc $ GadtDecl (noLoc (mkRdrUnqual $1)) (toHsType $3) }
111 attv_bndrs :: { [LHsTyVarBndr RdrName] }
113 | '@' tv_bndr attv_bndrs { toHsTvBndr $2 : $3 }
115 hs_atys :: { [LHsType RdrName] }
116 : atys { map toHsType $1 }
119 ---------------------------------------
121 ---------------------------------------
123 atys :: { [IfaceType] }
128 : tv_occ { IfaceTyVar $1 }
129 | q_tc_name { IfaceTyConApp (IfaceTc $1) [] }
133 : tv_occ atys { foldl IfaceAppTy (IfaceTyVar $1) $2 }
134 | q_tc_name atys { IfaceTyConApp (IfaceTc $1) $2 }
138 | bty '->' ty { IfaceFunTy $1 $3 }
139 | '%forall' tv_bndrs '.' ty { foldr IfaceForAllTy $4 $2 }
141 ----------------------------------------------
142 -- Bindings are in Iface syntax
144 vdefgs :: { [IfaceBinding] }
146 | let_bind ';' vdefgs { $1 : $3 }
148 let_bind :: { IfaceBinding }
149 : '%rec' '{' vdefs1 '}' { IfaceRec $3 }
150 | vdef { let (b,r) = $1
153 vdefs1 :: { [(IfaceIdBndr, IfaceExpr)] }
155 | vdef ';' vdefs1 { $1:$3 }
157 vdef :: { (IfaceIdBndr, IfaceExpr) }
158 : qd_occ '::' ty '=' exp { (($1, $3), $5) }
159 -- NB: qd_occ includes data constructors, because
160 -- we allow data-constructor wrappers at top level
161 -- But we discard the module name, because it must be the
162 -- same as the module being compiled, and Iface syntax only
163 -- has OccNames in binding positions
165 qd_occ :: { OccName }
169 ---------------------------------------
171 bndr :: { IfaceBndr }
172 : '@' tv_bndr { IfaceTvBndr $2 }
173 | id_bndr { IfaceIdBndr $1 }
175 bndrs :: { [IfaceBndr] }
177 | bndr bndrs { $1:$2 }
179 id_bndr :: { IfaceIdBndr }
180 : '(' var_occ '::' ty ')' { ($2,$4) }
182 id_bndrs :: { [IfaceIdBndr] }
184 | id_bndr id_bndrs { $1:$2 }
186 tv_bndr :: { IfaceTvBndr }
187 : tv_occ { ($1, LiftedTypeKind) }
188 | '(' tv_occ '::' akind ')' { ($2, $4) }
190 tv_bndrs :: { [IfaceTvBndr] }
192 | tv_bndr tv_bndrs { $1:$2 }
194 akind :: { IfaceKind }
195 : '*' { LiftedTypeKind }
196 | '#' { UnliftedTypeKind }
197 | '?' { OpenTypeKind }
198 | '(' kind ')' { $2 }
200 kind :: { IfaceKind }
202 | akind '->' kind { FunKind $1 $3 }
204 -----------------------------------------
207 aexp :: { IfaceExpr }
208 : var_occ { IfaceLcl $1 }
209 | modid '.' qd_occ { IfaceExt (ExtPkg $1 $3) }
210 | lit { IfaceLit $1 }
213 fexp :: { IfaceExpr }
214 : fexp aexp { IfaceApp $1 $2 }
215 | fexp '@' aty { IfaceApp $1 (IfaceType $3) }
220 | '\\' bndrs '->' exp { foldr IfaceLam $4 $2 }
221 | '%let' let_bind '%in' exp { IfaceLet $2 $4 }
223 | '%case' '(' ty ')' aexp '%of' id_bndr
224 '{' alts1 '}' { IfaceCase $5 (fst $7) $3 $9 }
225 | '%coerce' aty exp { IfaceNote (IfaceCoerce $2) $3 }
228 --"SCC" -> IfaceNote (IfaceSCC "scc") $3
229 "InlineCall" -> IfaceNote IfaceInlineCall $3
230 "InlineMe" -> IfaceNote IfaceInlineMe $3
232 | '%external' STRING aty { IfaceFCall (ForeignCall.CCall
233 (CCallSpec (StaticTarget (mkFastString $2))
234 CCallConv (PlaySafe False)))
237 alts1 :: { [IfaceAlt] }
239 | alt ';' alts1 { $1:$3 }
242 : modid '.' d_pat_occ bndrs '->' exp
243 { (IfaceDataAlt $3, map ifaceBndrName $4, $6) }
244 -- The external syntax currently includes the types of the
245 -- the args, but they aren't needed internally
246 -- Nor is the module qualifier
248 { (IfaceLitAlt $1, [], $3) }
250 { (IfaceDefault, [], $3) }
253 : '(' INTEGER '::' aty ')' { convIntLit $2 $4 }
254 | '(' RATIONAL '::' aty ')' { convRatLit $2 $4 }
255 | '(' CHAR '::' aty ')' { MachChar $2 }
256 | '(' STRING '::' aty ')' { MachStr (mkFastString $2) }
258 tv_occ :: { OccName }
259 : NAME { mkSysOcc tvName $1 }
261 var_occ :: { OccName }
262 : NAME { mkSysOcc varName $1 }
266 q_tc_name :: { IfaceExtName }
267 : modid '.' CNAME { ExtPkg $1 (mkSysOcc tcName $3) }
269 -- Data constructor in a pattern or data type declaration; use the dataName,
270 -- because that's what we expect in Core case patterns
271 d_pat_occ :: { OccName }
272 : CNAME { mkSysOcc dataName $1 }
274 -- Data constructor occurrence in an expression;
275 -- use the varName because that's the worker Id
277 : CNAME { mkSysOcc varName $1 }
281 ifaceBndrName (IfaceIdBndr (n,_)) = n
282 ifaceBndrName (IfaceTvBndr (n,_)) = n
284 convIntLit :: Integer -> IfaceType -> Literal
285 convIntLit i (IfaceTyConApp tc [])
286 | tc `eqTc` intPrimTyCon = MachInt i
287 | tc `eqTc` wordPrimTyCon = MachWord i
288 | tc `eqTc` charPrimTyCon = MachChar (chr (fromInteger i))
289 | tc `eqTc` addrPrimTyCon && i == 0 = MachNullAddr
291 = pprPanic "Unknown integer literal type" (ppr aty)
293 convRatLit :: Rational -> IfaceType -> Literal
294 convRatLit r (IfaceTyConApp tc [])
295 | tc `eqTc` floatPrimTyCon = MachFloat r
296 | tc `eqTc` doublePrimTyCon = MachDouble r
298 = pprPanic "Unknown rational literal type" (ppr aty)
300 eqTc :: IfaceTyCon -> TyCon -> Bool -- Ugh!
301 eqTc (IfaceTc (ExtPkg mod occ)) tycon
302 = mod == nameModuleName nm && occ == nameOccName nm
306 -- Tiresomely, we have to generate both HsTypes (in type/class decls)
307 -- and IfaceTypes (in Core expressions). So we parse them as IfaceTypes,
308 -- and convert to HsTypes here. But the IfaceTypes we can see here
309 -- are very limited (see the productions for 'ty', so the translation
311 toHsType :: IfaceType -> LHsType RdrName
312 toHsType (IfaceTyVar v) = noLoc $ HsTyVar (mkRdrUnqual v)
313 toHsType (IfaceAppTy t1 t2) = noLoc $ HsAppTy (toHsType t1) (toHsType t2)
314 toHsType (IfaceFunTy t1 t2) = noLoc $ HsFunTy (toHsType t1) (toHsType t2)
315 toHsType (IfaceTyConApp (IfaceTc tc) ts) = foldl mkHsAppTy (noLoc $ HsTyVar (ifaceExtRdrName tc)) (map toHsType ts)
316 toHsType (IfaceForAllTy tv t) = add_forall (toHsTvBndr tv) (toHsType t)
318 toHsTvBndr :: IfaceTvBndr -> LHsTyVarBndr RdrName
319 toHsTvBndr (tv,k) = noLoc $ KindedTyVar (mkRdrUnqual tv) k
321 ifaceExtRdrName :: IfaceExtName -> RdrName
322 ifaceExtRdrName (ExtPkg mod occ) = mkOrig mod occ
323 ifaceExtRdrName other = pprPanic "ParserCore.ifaceExtRdrName" (ppr other)
325 add_forall tv (L _ (HsForAllTy exp tvs cxt t))
326 = noLoc $ HsForAllTy exp (tv:tvs) cxt t
328 = noLoc $ HsForAllTy Explicit [tv] (noLoc []) t
331 happyError s l = failP (show l ++ ": Parse error\n") (take 100 s) l