2 {-# OPTIONS -Wwarn -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
9 module ParserCore ( parseCore ) where
18 liftedTypeKindTyCon, openTypeKindTyCon, unliftedTypeKindTyCon,
19 argTypeKindTyCon, ubxTupleKindTyCon, mkTyConApp
21 import Coercion( mkArrowKind )
22 import Name( Name, nameOccName, nameModule, mkExternalName )
24 import ParserCoreUtils
28 import TysPrim( wordPrimTyCon, intPrimTyCon, charPrimTyCon,
29 floatPrimTyCon, doublePrimTyCon, addrPrimTyCon )
30 import TyCon ( TyCon, tyConName )
36 #include "../HsVersions.h"
45 '%module' { TKmodule }
47 '%newtype' { TKnewtype }
48 '%forall' { TKforall }
56 '%external' { TKexternal }
67 ':=:' { TKcoloneqcolon }
77 INTEGER { TKinteger $$ }
78 RATIONAL { TKrational $$ }
79 STRING { TKstring $$ }
82 %monad { P } { thenP } { returnP }
83 %lexer { lexer } { TKEOF }
87 module :: { HsExtCore RdrName }
88 -- : '%module' modid tdefs vdefgs { HsExtCore $2 $3 $4 }
89 : '%module' modid tdefs vdefgs { HsExtCore $2 [] [] }
92 -------------------------------------------------------------
93 -- Names: the trickiest bit in here
95 -- A name of the form A.B.C could be:
97 -- dcon C in module A.B
98 -- tcon C in module A.B
100 : NAME ':' mparts { undefined }
102 q_dc_name :: { Name }
103 : NAME ':' mparts { undefined }
105 q_tc_name :: { Name }
106 : NAME ':' mparts { undefined }
108 q_var_occ :: { Name }
109 : NAME ':' vparts { undefined }
111 mparts :: { [String] }
113 | CNAME '.' mparts { $1:$3 }
115 vparts :: { [String] }
117 | CNAME '.' vparts { $1:$3 }
119 -------------------------------------------------------------
120 -- Type and newtype declarations are in HsSyn syntax
122 tdefs :: { [TyClDecl RdrName] }
126 tdef :: { TyClDecl RdrName }
127 : '%data' q_tc_name tv_bndrs '=' '{' cons '}' ';'
128 { TyData { tcdND = DataType, tcdCtxt = noLoc []
129 , tcdLName = noLoc (ifaceExtRdrName $2)
130 , tcdTyVars = map toHsTvBndr $3
131 , tcdTyPats = Nothing, tcdKindSig = Nothing
132 , tcdCons = $6, tcdDerivs = Nothing } }
133 | '%newtype' q_tc_name tv_bndrs trep ';'
134 { let tc_rdr = ifaceExtRdrName $2 in
135 TyData { tcdND = NewType, tcdCtxt = noLoc []
136 , tcdLName = noLoc tc_rdr
137 , tcdTyVars = map toHsTvBndr $3
138 , tcdTyPats = Nothing, tcdKindSig = Nothing
139 , tcdCons = $4 (rdrNameOcc tc_rdr), tcdDerivs = Nothing } }
141 -- For a newtype we have to invent a fake data constructor name
142 -- It doesn't matter what it is, because it won't be used
143 trep :: { OccName -> [LConDecl RdrName] }
144 : {- empty -} { (\ tc_occ -> []) }
145 | '=' ty { (\ tc_occ -> let { dc_name = mkRdrUnqual (setOccNameSpace dataName tc_occ) ;
146 con_info = PrefixCon [toHsType $2] }
147 in [noLoc $ mkSimpleConDecl (noLoc dc_name) []
148 (noLoc []) con_info]) }
150 cons :: { [LConDecl RdrName] }
151 : {- empty -} { [] } -- 20060420 Empty data types allowed. jds
153 | con ';' cons { $1:$3 }
155 con :: { LConDecl RdrName }
156 : d_pat_occ attv_bndrs hs_atys
157 { noLoc $ mkSimpleConDecl (noLoc (mkRdrUnqual $1)) $2 (noLoc []) (PrefixCon $3) }
158 -- ToDo: parse record-style declarations
160 attv_bndrs :: { [LHsTyVarBndr RdrName] }
162 | '@' tv_bndr attv_bndrs { toHsTvBndr $2 : $3 }
164 hs_atys :: { [LHsType RdrName] }
165 : atys { map toHsType $1 }
168 ---------------------------------------
170 ---------------------------------------
172 atys :: { [IfaceType] }
177 : fs_var_occ { IfaceTyVar $1 }
178 | q_tc_name { IfaceTyConApp (IfaceTc $1) [] }
182 : fs_var_occ atys { foldl IfaceAppTy (IfaceTyVar $1) $2 }
183 | q_var_occ atys { undefined }
184 | q_tc_name atys { IfaceTyConApp (IfaceTc $1) $2 }
189 | bty '->' ty { IfaceFunTy $1 $3 }
190 | '%forall' tv_bndrs '.' ty { foldr IfaceForAllTy $4 $2 }
192 ----------------------------------------------
193 -- Bindings are in Iface syntax
195 vdefgs :: { [IfaceBinding] }
197 | let_bind ';' vdefgs { $1 : $3 }
199 let_bind :: { IfaceBinding }
200 : '%rec' '{' vdefs1 '}' { IfaceRec $3 } -- Can be empty. Do we care?
201 | vdef { let (b,r) = $1
204 vdefs1 :: { [(IfaceLetBndr, IfaceExpr)] }
206 | vdef ';' vdefs1 { $1:$3 }
208 vdef :: { (IfaceLetBndr, IfaceExpr) }
209 : fs_var_occ '::' ty '=' exp { (IfLetBndr $1 $3 NoInfo, $5) }
210 | '%local' vdef { $2 }
212 -- NB: qd_occ includes data constructors, because
213 -- we allow data-constructor wrappers at top level
214 -- But we discard the module name, because it must be the
215 -- same as the module being compiled, and Iface syntax only
216 -- has OccNames in binding positions. Ah, but it has Names now!
218 ---------------------------------------
220 bndr :: { IfaceBndr }
221 : '@' tv_bndr { IfaceTvBndr $2 }
222 | id_bndr { IfaceIdBndr $1 }
224 bndrs :: { [IfaceBndr] }
226 | bndr bndrs { $1:$2 }
228 id_bndr :: { IfaceIdBndr }
229 : '(' fs_var_occ '::' ty ')' { ($2,$4) }
231 tv_bndr :: { IfaceTvBndr }
232 : fs_var_occ { ($1, ifaceLiftedTypeKind) }
233 | '(' fs_var_occ '::' akind ')' { ($2, $4) }
235 tv_bndrs :: { [IfaceTvBndr] }
237 | tv_bndr tv_bndrs { $1:$2 }
239 akind :: { IfaceKind }
240 : '*' { ifaceLiftedTypeKind }
241 | '#' { ifaceUnliftedTypeKind }
242 | '?' { ifaceOpenTypeKind }
243 | '(' kind ')' { $2 }
245 kind :: { IfaceKind }
247 | akind '->' kind { ifaceArrow $1 $3 }
248 | ty ':=:' ty { ifaceEq $1 $3 }
250 -----------------------------------------
253 aexp :: { IfaceExpr }
254 : fs_var_occ { IfaceLcl $1 }
255 | q_var_occ { IfaceExt $1 }
256 | q_dc_name { IfaceExt $1 }
257 | lit { IfaceLit $1 }
260 fexp :: { IfaceExpr }
261 : fexp aexp { IfaceApp $1 $2 }
262 | fexp '@' aty { IfaceApp $1 (IfaceType $3) }
267 | '\\' bndrs '->' exp { foldr IfaceLam $4 $2 }
268 | '%let' let_bind '%in' exp { IfaceLet $2 $4 }
270 | '%case' '(' ty ')' aexp '%of' id_bndr
271 '{' alts1 '}' { IfaceCase $5 (fst $7) $3 $9 }
272 | '%cast' aexp aty { IfaceCast $2 $3 }
273 -- No InlineMe any more
274 -- | '%note' STRING exp
276 -- --"SCC" -> IfaceNote (IfaceSCC "scc") $3
277 -- "InlineMe" -> IfaceNote IfaceInlineMe $3
279 | '%external' STRING aty { IfaceFCall (ForeignCall.CCall
280 (CCallSpec (StaticTarget (mkFastString $2))
281 CCallConv (PlaySafe False)))
284 alts1 :: { [IfaceAlt] }
286 | alt ';' alts1 { $1:$3 }
289 : q_dc_name bndrs '->' exp
290 { (IfaceDataAlt $1, map ifaceBndrName $2, $4) }
291 -- The external syntax currently includes the types of the
292 -- the args, but they aren't needed internally
293 -- Nor is the module qualifier
295 { (IfaceDataAlt $1, [], $3) }
297 { (IfaceLitAlt $1, [], $3) }
299 { (IfaceDefault, [], $3) }
302 : '(' INTEGER '::' aty ')' { convIntLit $2 $4 }
303 | '(' RATIONAL '::' aty ')' { convRatLit $2 $4 }
304 | '(' CHAR '::' aty ')' { MachChar $2 }
305 | '(' STRING '::' aty ')' { MachStr (mkFastString $2) }
307 fs_var_occ :: { FastString }
308 : NAME { mkFastString $1 }
310 var_occ :: { String }
314 -- Data constructor in a pattern or data type declaration; use the dataName,
315 -- because that's what we expect in Core case patterns
316 d_pat_occ :: { OccName }
317 : CNAME { mkOccName dataName $1 }
321 ifaceKind kc = IfaceTyConApp kc []
323 ifaceBndrName (IfaceIdBndr (n,_)) = n
324 ifaceBndrName (IfaceTvBndr (n,_)) = n
326 convIntLit :: Integer -> IfaceType -> Literal
327 convIntLit i (IfaceTyConApp tc [])
328 | tc `eqTc` intPrimTyCon = MachInt i
329 | tc `eqTc` wordPrimTyCon = MachWord i
330 | tc `eqTc` charPrimTyCon = MachChar (chr (fromInteger i))
331 | tc `eqTc` addrPrimTyCon && i == 0 = MachNullAddr
333 = pprPanic "Unknown integer literal type" (ppr aty)
335 convRatLit :: Rational -> IfaceType -> Literal
336 convRatLit r (IfaceTyConApp tc [])
337 | tc `eqTc` floatPrimTyCon = MachFloat r
338 | tc `eqTc` doublePrimTyCon = MachDouble r
340 = pprPanic "Unknown rational literal type" (ppr aty)
342 eqTc :: IfaceTyCon -> TyCon -> Bool -- Ugh!
343 eqTc (IfaceTc name) tycon = name == tyConName tycon
345 -- Tiresomely, we have to generate both HsTypes (in type/class decls)
346 -- and IfaceTypes (in Core expressions). So we parse them as IfaceTypes,
347 -- and convert to HsTypes here. But the IfaceTypes we can see here
348 -- are very limited (see the productions for 'ty', so the translation
350 toHsType :: IfaceType -> LHsType RdrName
351 toHsType (IfaceTyVar v) = noLoc $ HsTyVar (mkRdrUnqual (mkTyVarOccFS v))
352 toHsType (IfaceAppTy t1 t2) = noLoc $ HsAppTy (toHsType t1) (toHsType t2)
353 toHsType (IfaceFunTy t1 t2) = noLoc $ HsFunTy (toHsType t1) (toHsType t2)
354 toHsType (IfaceTyConApp (IfaceTc tc) ts) = foldl mkHsAppTy (noLoc $ HsTyVar (ifaceExtRdrName tc)) (map toHsType ts)
355 toHsType (IfaceForAllTy tv t) = add_forall (toHsTvBndr tv) (toHsType t)
357 -- We also need to convert IfaceKinds to Kinds (now that they are different).
358 -- Only a limited form of kind will be encountered... hopefully
359 toKind :: IfaceKind -> Kind
360 toKind (IfaceFunTy ifK1 ifK2) = mkArrowKind (toKind ifK1) (toKind ifK2)
361 toKind (IfaceTyConApp ifKc []) = mkTyConApp (toKindTc ifKc) []
362 toKind other = pprPanic "toKind" (ppr other)
364 toKindTc :: IfaceTyCon -> TyCon
365 toKindTc IfaceLiftedTypeKindTc = liftedTypeKindTyCon
366 toKindTc IfaceOpenTypeKindTc = openTypeKindTyCon
367 toKindTc IfaceUnliftedTypeKindTc = unliftedTypeKindTyCon
368 toKindTc IfaceUbxTupleKindTc = ubxTupleKindTyCon
369 toKindTc IfaceArgTypeKindTc = argTypeKindTyCon
370 toKindTc other = pprPanic "toKindTc" (ppr other)
372 ifaceTcType ifTc = IfaceTyConApp ifTc []
374 ifaceLiftedTypeKind = ifaceTcType IfaceLiftedTypeKindTc
375 ifaceOpenTypeKind = ifaceTcType IfaceOpenTypeKindTc
376 ifaceUnliftedTypeKind = ifaceTcType IfaceUnliftedTypeKindTc
378 ifaceArrow ifT1 ifT2 = IfaceFunTy ifT1 ifT2
380 ifaceEq ifT1 ifT2 = IfacePredTy (IfaceEqPred ifT1 ifT2)
382 toHsTvBndr :: IfaceTvBndr -> LHsTyVarBndr RdrName
383 toHsTvBndr (tv,k) = noLoc $ KindedTyVar (mkRdrUnqual (mkTyVarOccFS tv)) (toKind k)
385 ifaceExtRdrName :: Name -> RdrName
386 ifaceExtRdrName name = mkOrig (nameModule name) (nameOccName name)
387 ifaceExtRdrName other = pprPanic "ParserCore.ifaceExtRdrName" (ppr other)
389 add_forall tv (L _ (HsForAllTy exp tvs cxt t))
390 = noLoc $ HsForAllTy exp (tv:tvs) cxt t
392 = noLoc $ HsForAllTy Explicit [tv] (noLoc []) t
395 happyError s l = failP (show l ++ ": Parse error\n") (take 100 s) l