[project @ 2002-05-27 16:13:42 by simonpj]
[ghc-hetmet.git] / ghc / compiler / parser / ParseUtil.lhs
1 %
2 % (c) The GRASP/AQUA Project, Glasgow University, 1999
3 %
4 \section[ParseUtil]{Parser Utilities}
5
6 \begin{code}
7 module ParseUtil (
8           parseError          -- String -> Pa
9         , mkVanillaCon, mkRecCon,
10
11         , mkRecConstrOrUpdate -- HsExp -> [HsFieldUpdate] -> P HsExp
12         , groupBindings
13         
14         , mkIfaceExports      -- :: [RdrNameTyClDecl] -> [RdrExportItem]
15
16         , CallConv(..)
17         , mkImport            -- CallConv -> Safety 
18                               -- -> (FastString, RdrName, RdrNameHsType)
19                               -- -> SrcLoc 
20                               -- -> P RdrNameHsDecl
21         , mkExport            -- CallConv
22                               -- -> (FastString, RdrName, RdrNameHsType)
23                               -- -> SrcLoc 
24                               -- -> P RdrNameHsDecl
25         , mkExtName           -- RdrName -> CLabelString
26                               
27         , checkPrec           -- String -> P String
28         , checkContext        -- HsType -> P HsContext
29         , checkPred           -- HsType -> P HsPred
30         , checkTyVars         -- [HsTyVar] -> P [HsType]
31         , checkTyClHdr        -- HsType -> (name,[tyvar])
32         , checkInstType       -- HsType -> P HsType
33         , checkPattern        -- HsExp -> P HsPat
34         , checkPatterns       -- SrcLoc -> [HsExp] -> P [HsPat]
35         , checkDo             -- [Stmt] -> P [Stmt]
36         , checkValDef         -- (SrcLoc, HsExp, HsRhs, [HsDecl]) -> P HsDecl
37         , checkValSig         -- (SrcLoc, HsExp, HsRhs, [HsDecl]) -> P HsDecl
38  ) where
39
40 #include "HsVersions.h"
41
42 import List             ( isSuffixOf )
43
44 import Lex
45 import HscTypes         ( RdrAvailInfo, GenAvailInfo(..) )
46 import HsSyn            -- Lots of it
47 import ForeignCall      ( CCallConv, Safety, CCallTarget(..), CExportSpec(..),
48                           DNCallSpec(..))
49 import SrcLoc
50 import RdrHsSyn
51 import RdrName
52 import PrelNames        ( unitTyCon_RDR )
53 import OccName          ( dataName, varName, tcClsName, isDataOcc,
54                           occNameSpace, setOccNameSpace, occNameUserString )
55 import CStrings         ( CLabelString )
56 import FastString
57 import Outputable
58
59 -----------------------------------------------------------------------------
60 -- Misc utils
61
62 parseError :: String -> P a
63 parseError s = 
64   getSrcLocP `thenP` \ loc ->
65   failMsgP (hcat [ppr loc, text ": ", text s])
66
67
68 -----------------------------------------------------------------------------
69 -- mkVanillaCon
70
71 -- When parsing data declarations, we sometimes inadvertently parse
72 -- a constructor application as a type (eg. in data T a b = C a b `D` E a b)
73 -- This function splits up the type application, adds any pending
74 -- arguments, and converts the type constructor back into a data constructor.
75
76 mkVanillaCon :: RdrNameHsType -> [RdrNameBangType] -> P (RdrName, RdrNameConDetails)
77
78 mkVanillaCon ty tys
79  = split ty tys
80  where
81    split (HsAppTy t u)  ts = split t (unbangedType u : ts)
82    split (HsTyVar tc)   ts = tyConToDataCon tc  `thenP` \ data_con ->
83                              returnP (data_con, VanillaCon ts)
84    split _               _ = parseError "Illegal data/newtype declaration"
85
86 mkRecCon :: RdrName -> [([RdrName],RdrNameBangType)] -> P (RdrName, RdrNameConDetails)
87 mkRecCon con fields
88   = tyConToDataCon con  `thenP` \ data_con ->
89     returnP (data_con, RecCon fields)
90
91 tyConToDataCon :: RdrName -> P RdrName
92 tyConToDataCon tc
93   | occNameSpace tc_occ == tcClsName
94   = returnP (setRdrNameOcc tc (setOccNameSpace tc_occ dataName))
95   | otherwise
96   = parseError (showSDoc (text "Not a constructor:" <+> quotes (ppr tc)))
97   where 
98     tc_occ   = rdrNameOcc tc
99
100
101 ----------------------------------------------------------------------------
102 -- Various Syntactic Checks
103
104 checkInstType :: RdrNameHsType -> P RdrNameHsType
105 checkInstType t 
106   = case t of
107         HsForAllTy tvs ctxt ty ->
108                 checkDictTy ty [] `thenP` \ dict_ty ->
109                 returnP (HsForAllTy tvs ctxt dict_ty)
110
111         ty ->   checkDictTy ty [] `thenP` \ dict_ty->
112                 returnP (HsForAllTy Nothing [] dict_ty)
113
114 checkTyVars :: [RdrNameHsType] -> P [RdrNameHsTyVar]
115 checkTyVars tvs = mapP chk tvs
116                 where
117                   chk (HsKindSig (HsTyVar tv) k) = returnP (IfaceTyVar tv k)
118                   chk (HsTyVar tv)               = returnP (UserTyVar tv)
119                   chk other                      = parseError "Type found where type variable expected"
120
121 checkTyClHdr :: RdrNameHsType -> P (RdrName, [RdrNameHsTyVar])
122 -- The header of a type or class decl should look like
123 --      (C a, D b) => T a b
124 -- or   T a b
125 -- or   a + b
126 -- etc
127 checkTyClHdr ty
128   = go ty []
129   where
130     go (HsTyVar tc) acc 
131         | not (isRdrTyVar tc) = checkTyVars acc         `thenP` \ tvs ->
132                                 returnP (tc, tvs)
133     go (HsOpTy t1 (HsTyOp tc) t2) acc  = checkTyVars (t1:t2:acc)        `thenP` \ tvs ->
134                                          returnP (tc, tvs)
135     go (HsAppTy t1 t2) acc    = go t1 (t2:acc)
136     go other           acc    = parseError "Malformed LHS to type of class declaration"
137
138 checkContext :: RdrNameHsType -> P RdrNameContext
139 checkContext (HsTupleTy _ ts)   -- (Eq a, Ord b) shows up as a tuple type
140   = mapP checkPred ts
141
142 checkContext (HsTyVar t)        -- Empty context shows up as a unit type ()
143   | t == unitTyCon_RDR = returnP []
144
145 checkContext t 
146   = checkPred t `thenP` \p ->
147     returnP [p]
148
149 checkPred :: RdrNameHsType -> P (HsPred RdrName)
150 -- Watch out.. in ...deriving( Show )... we use checkPred on 
151 -- the list of partially applied predicates in the deriving,
152 -- so there can be zero args.
153 checkPred (HsPredTy (HsIParam n ty)) = returnP (HsIParam n ty)
154 checkPred ty
155   = go ty []
156   where
157     go (HsTyVar t) args   | not (isRdrTyVar t) 
158                           = returnP (HsClassP t args)
159     go (HsAppTy l r) args = go l (r:args)
160     go _             _    = parseError "Illegal class assertion"
161
162 checkDictTy :: RdrNameHsType -> [RdrNameHsType] -> P RdrNameHsType
163 checkDictTy (HsTyVar t) args@(_:_) | not (isRdrTyVar t) 
164         = returnP (mkHsDictTy t args)
165 checkDictTy (HsAppTy l r) args = checkDictTy l (r:args)
166 checkDictTy _ _ = parseError "Malformed context in instance header"
167
168
169 ---------------------------------------------------------------------------
170 -- Checking statements in a do-expression
171 --      We parse   do { e1 ; e2 ; }
172 --      as [ExprStmt e1, ExprStmt e2]
173 -- checkDo (a) checks that the last thing is an ExprStmt
174 --         (b) transforms it to a ResultStmt
175
176 checkDo []               = parseError "Empty 'do' construct"
177 checkDo [ExprStmt e _ l] = returnP [ResultStmt e l]
178 checkDo [s]              = parseError "The last statement in a 'do' construct must be an expression"
179 checkDo (s:ss)           = checkDo ss   `thenP` \ ss' ->
180                            returnP (s:ss')
181
182 ---------------------------------------------------------------------------
183 -- Checking Patterns.
184
185 -- We parse patterns as expressions and check for valid patterns below,
186 -- converting the expression into a pattern at the same time.
187
188 checkPattern :: SrcLoc -> RdrNameHsExpr -> P RdrNamePat
189 checkPattern loc e = setSrcLocP loc (checkPat e [])
190
191 checkPatterns :: SrcLoc -> [RdrNameHsExpr] -> P [RdrNamePat]
192 checkPatterns loc es = mapP (checkPattern loc) es
193
194 checkPat :: RdrNameHsExpr -> [RdrNamePat] -> P RdrNamePat
195 checkPat (HsVar c) args | isRdrDataCon c = returnP (ConPatIn c args)
196 checkPat (HsApp f x) args = 
197         checkPat x [] `thenP` \x ->
198         checkPat f (x:args)
199 checkPat e [] = case e of
200         EWildPat           -> returnP WildPatIn
201         HsVar x            -> returnP (VarPatIn x)
202         HsLit l            -> returnP (LitPatIn l)
203         HsOverLit l        -> returnP (NPatIn l Nothing)
204         ELazyPat e         -> checkPat e [] `thenP` (returnP . LazyPatIn)
205         EAsPat n e         -> checkPat e [] `thenP` (returnP . AsPatIn n)
206         ExprWithTySig e t  -> checkPat e [] `thenP` \e ->
207                               -- Pattern signatures are parsed as sigtypes,
208                               -- but they aren't explicit forall points.  Hence
209                               -- we have to remove the implicit forall here.
210                               let t' = case t of 
211                                           HsForAllTy Nothing [] ty -> ty
212                                           other -> other
213                               in
214                               returnP (SigPatIn e t')
215
216         -- Translate out NegApps of literals in patterns. We negate
217         -- the Integer here, and add back the call to 'negate' when
218         -- we typecheck the pattern.
219         -- NB. Negative *primitive* literals are already handled by
220         --     RdrHsSyn.mkHsNegApp
221         NegApp (HsOverLit lit) neg -> returnP (NPatIn lit (Just neg))
222
223         OpApp (HsVar n) (HsVar plus) _ (HsOverLit lit@(HsIntegral _ _)) 
224                            | plus == plus_RDR
225                            -> returnP (mkNPlusKPat n lit)
226                            where
227                               plus_RDR = mkUnqual varName FSLIT("+")    -- Hack
228
229         OpApp l op fix r   -> checkPat l [] `thenP` \l ->
230                               checkPat r [] `thenP` \r ->
231                               case op of
232                                  HsVar c | isDataOcc (rdrNameOcc c)
233                                         -> returnP (ConOpPatIn l c fix r)
234                                  _ -> patFail
235
236         HsPar e            -> checkPat e [] `thenP` (returnP . ParPatIn)
237         ExplicitList _ es  -> mapP (\e -> checkPat e []) es `thenP` \ps ->
238                               returnP (ListPatIn ps)
239         ExplicitPArr _ es  -> mapP (\e -> checkPat e []) es `thenP` \ps ->
240                               returnP (PArrPatIn ps)
241
242         ExplicitTuple es b -> mapP (\e -> checkPat e []) es `thenP` \ps ->
243                               returnP (TuplePatIn ps b)
244
245         RecordCon c fs     -> mapP checkPatField fs `thenP` \fs ->
246                               returnP (RecPatIn c fs)
247 -- Generics 
248         HsType ty          -> returnP (TypePatIn ty) 
249         _ -> patFail
250
251 checkPat _ _ = patFail
252
253 checkPatField :: (RdrName, RdrNameHsExpr, Bool) 
254         -> P (RdrName, RdrNamePat, Bool)
255 checkPatField (n,e,b) =
256         checkPat e [] `thenP` \p ->
257         returnP (n,p,b)
258
259 patFail = parseError "Parse error in pattern"
260
261
262 ---------------------------------------------------------------------------
263 -- Check Equation Syntax
264
265 checkValDef 
266         :: RdrNameHsExpr
267         -> Maybe RdrNameHsType
268         -> RdrNameGRHSs
269         -> SrcLoc
270         -> P RdrBinding
271
272 checkValDef lhs opt_sig grhss loc
273  = case isFunLhs lhs [] of
274            Just (f,inf,es) -> 
275                 checkPatterns loc es `thenP` \ps ->
276                 returnP (RdrValBinding (FunMonoBind f inf [Match ps opt_sig grhss] loc))
277
278            Nothing ->
279                 checkPattern loc lhs `thenP` \lhs ->
280                 returnP (RdrValBinding (PatMonoBind lhs grhss loc))
281
282 checkValSig
283         :: RdrNameHsExpr
284         -> RdrNameHsType
285         -> SrcLoc
286         -> P RdrBinding
287 checkValSig (HsVar v) ty loc = returnP (RdrSig (Sig v ty loc))
288 checkValSig other     ty loc = parseError "Type signature given for an expression"
289
290
291 -- A variable binding is parsed as an RdrNameFunMonoBind.
292 -- See comments with HsBinds.MonoBinds
293
294 isFunLhs :: RdrNameHsExpr -> [RdrNameHsExpr] -> Maybe (RdrName, Bool, [RdrNameHsExpr])
295 isFunLhs (OpApp l (HsVar op) fix r) es  | not (isRdrDataCon op)
296                                 = Just (op, True, (l:r:es))
297                                         | otherwise
298                                 = case isFunLhs l es of
299                                     Just (op', True, j : k : es') ->
300                                       Just (op', True, j : OpApp k (HsVar op) fix r : es')
301                                     _ -> Nothing
302 isFunLhs (HsVar f) es | not (isRdrDataCon f)
303                                 = Just (f,False,es)
304 isFunLhs (HsApp f e) es         = isFunLhs f (e:es)
305 isFunLhs (HsPar e)   es@(_:_)   = isFunLhs e es
306 isFunLhs _ _                    = Nothing
307
308 ---------------------------------------------------------------------------
309 -- Miscellaneous utilities
310
311 checkPrec :: Integer -> P ()
312 checkPrec i | 0 <= i && i <= 9 = returnP ()
313             | otherwise        = parseError "Precedence out of range"
314
315 mkRecConstrOrUpdate 
316         :: RdrNameHsExpr 
317         -> RdrNameHsRecordBinds
318         -> P RdrNameHsExpr
319
320 mkRecConstrOrUpdate (HsVar c) fs | isRdrDataCon c
321   = returnP (RecordCon c fs)
322 mkRecConstrOrUpdate exp fs@(_:_) 
323   = returnP (RecordUpd exp fs)
324 mkRecConstrOrUpdate _ _
325   = parseError "Empty record update"
326
327 -----------------------------------------------------------------------------
328 -- utilities for foreign declarations
329
330 -- supported calling conventions
331 --
332 data CallConv = CCall  CCallConv        -- ccall or stdcall
333               | DNCall                  -- .NET
334
335 -- construct a foreign import declaration
336 --
337 mkImport :: CallConv 
338          -> Safety 
339          -> (FastString, RdrName, RdrNameHsType) 
340          -> SrcLoc 
341          -> P RdrNameHsDecl
342 mkImport (CCall  cconv) safety (entity, v, ty) loc =
343   parseCImport entity cconv safety v                     `thenP` \importSpec ->
344   returnP $ ForD (ForeignImport v ty importSpec                     False loc)
345 mkImport (DNCall      ) _      (entity, v, ty) loc =
346   returnP $ ForD (ForeignImport v ty (DNImport (DNCallSpec entity)) False loc)
347
348 -- parse the entity string of a foreign import declaration for the `ccall' or
349 -- `stdcall' calling convention'
350 --
351 parseCImport :: FastString 
352              -> CCallConv 
353              -> Safety 
354              -> RdrName 
355              -> P ForeignImport
356 parseCImport entity cconv safety v
357   -- FIXME: we should allow white space around `dynamic' and `wrapper' -=chak
358   | entity == FSLIT ("dynamic") = 
359     returnP $ CImport cconv safety nilFS nilFS (CFunction DynamicTarget)
360   | entity == FSLIT ("wrapper") =
361     returnP $ CImport cconv safety nilFS nilFS CWrapper
362   | otherwise                  = parse0 (unpackFS entity)
363     where
364       -- using the static keyword?
365       parse0 (' ':                    rest) = parse0 rest
366       parse0 ('s':'t':'a':'t':'i':'c':rest) = parse1 rest
367       parse0                          rest  = parse1 rest
368       -- check for header file name
369       parse1     ""               = parse4 ""    nilFS        False nilFS
370       parse1     (' ':rest)       = parse1 rest
371       parse1 str@('&':_   )       = parse2 str   nilFS
372       parse1 str@('[':_   )       = parse3 str   nilFS        False
373       parse1 str
374         | ".h" `isSuffixOf` first = parse2 rest  (mkFastString first)
375         | otherwise               = parse4 str   nilFS        False nilFS
376         where
377           (first, rest) = break (\c -> c == ' ' || c == '&' || c == '[') str
378       -- check for address operator (indicating a label import)
379       parse2     ""         header = parse4 ""   header False nilFS
380       parse2     (' ':rest) header = parse2 rest header
381       parse2     ('&':rest) header = parse3 rest header True
382       parse2 str@('[':_   ) header = parse3 str  header False
383       parse2 str            header = parse4 str  header False nilFS
384       -- check for library object name
385       parse3 (' ':rest) header isLbl = parse3 rest header isLbl
386       parse3 ('[':rest) header isLbl = 
387         case break (== ']') rest of 
388           (lib, ']':rest)           -> parse4 rest header isLbl (mkFastString lib)
389           _                         -> parseError "Missing ']' in entity"
390       parse3 str        header isLbl = parse4 str  header isLbl nilFS
391       -- check for name of C function
392       parse4 ""         header isLbl lib = build (mkExtName v) header isLbl lib
393       parse4 (' ':rest) header isLbl lib = parse4 rest         header isLbl lib
394       parse4 str        header isLbl lib
395         | all (== ' ') rest              = build (mkFastString first)  header isLbl lib
396         | otherwise                      = parseError "Malformed entity string"
397         where
398           (first, rest) = break (== ' ') str
399       --
400       build cid header False lib = returnP $
401         CImport cconv safety header lib (CFunction (StaticTarget cid))
402       build cid header True  lib = returnP $
403         CImport cconv safety header lib (CLabel                  cid )
404
405 -- construct a foreign export declaration
406 --
407 mkExport :: CallConv
408          -> (FastString, RdrName, RdrNameHsType) 
409          -> SrcLoc 
410          -> P RdrNameHsDecl
411 mkExport (CCall  cconv) (entity, v, ty) loc = returnP $ 
412   ForD (ForeignExport v ty (CExport (CExportStatic entity' cconv)) False loc)
413   where
414     entity' | nullFastString entity = mkExtName v
415             | otherwise             = entity
416 mkExport DNCall (entity, v, ty) loc =
417   parseError "Foreign export is not yet supported for .NET"
418
419 -- Supplying the ext_name in a foreign decl is optional; if it
420 -- isn't there, the Haskell name is assumed. Note that no transformation
421 -- of the Haskell name is then performed, so if you foreign export (++),
422 -- it's external name will be "++". Too bad; it's important because we don't
423 -- want z-encoding (e.g. names with z's in them shouldn't be doubled)
424 -- (This is why we use occNameUserString.)
425 --
426 mkExtName :: RdrName -> CLabelString
427 mkExtName rdrNm = mkFastString (occNameUserString (rdrNameOcc rdrNm))
428
429 -----------------------------------------------------------------------------
430 -- group function bindings into equation groups
431
432 -- we assume the bindings are coming in reverse order, so we take the srcloc
433 -- from the *last* binding in the group as the srcloc for the whole group.
434
435 groupBindings :: [RdrBinding] -> RdrBinding
436 groupBindings binds = group Nothing binds
437   where group :: Maybe RdrNameMonoBinds -> [RdrBinding] -> RdrBinding
438         group (Just bind) [] = RdrValBinding bind
439         group Nothing [] = RdrNullBind
440
441                 -- don't group together FunMonoBinds if they have
442                 -- no arguments.  This is necessary now that variable bindings
443                 -- with no arguments are now treated as FunMonoBinds rather
444                 -- than pattern bindings (tests/rename/should_fail/rnfail002).
445         group (Just (FunMonoBind f inf1 mtchs ignore_srcloc))
446                     (RdrValBinding (FunMonoBind f' _ 
447                                         [mtch@(Match (_:_) _ _)] loc)
448                         : binds)
449             | f == f' = group (Just (FunMonoBind f inf1 (mtch:mtchs) loc)) binds
450
451         group (Just so_far) binds
452             = RdrValBinding so_far `RdrAndBindings` group Nothing binds
453         group Nothing (bind:binds)
454             = case bind of
455                 RdrValBinding b@(FunMonoBind _ _ _ _) -> group (Just b) binds
456                 other -> bind `RdrAndBindings` group Nothing binds
457
458 -- ---------------------------------------------------------------------------
459 -- Make the export list for an interface
460
461 mkIfaceExports :: [RdrNameTyClDecl] -> [RdrAvailInfo]
462 mkIfaceExports decls = map getExport decls
463   where getExport d = case d of
464                         TyData{}    -> tc_export
465                         ClassDecl{} -> tc_export
466                         _other      -> var_export
467           where 
468                 tc_export  = AvailTC (rdrNameOcc (tcdName d)) 
469                                 (map (rdrNameOcc.fst) (tyClDeclNames d))
470                 var_export = Avail (rdrNameOcc (tcdName d))
471 \end{code}