[project @ 1997-10-07 14:51:49 by simonm]
[ghc-hetmet.git] / ghc / compiler / reader / ReadPrefix.lhs
index 2098692..6f72409 100644 (file)
@@ -10,22 +10,30 @@ module ReadPrefix ( rdModule )  where
 
 IMP_Ubiq()
 IMPORT_1_3(IO(hPutStr, stderr))
-IMPORT_1_3(GHCio(stThen))
+#if __GLASGOW_HASKELL__ == 201
+import GHCio(stThen)
+#elif __GLASGOW_HASKELL__ >= 202
+import GlaExts
+import IOBase
+import PrelRead
+#endif
 
 import UgenAll         -- all Yacc parser gumpff...
 import PrefixSyn       -- and various syntaxen.
 import HsSyn
 import HsTypes         ( HsTyVar(..) )
 import HsPragmas       ( noDataPragmas, noClassPragmas, noInstancePragmas, noGenPragmas )
-import RdrHsSyn
+import RdrHsSyn         
+import BasicTypes      ( Fixity(..), FixityDirection(..), NewOrData(..), IfaceFlavour(..) )
 import PrefixToHs
 
+import CmdLineOpts      ( opt_PprUserLength )
 import ErrUtils                ( addErrLoc, ghcExit )
 import FiniteMap       ( elemFM, FiniteMap )
-import Name            ( RdrName(..), OccName(..) )
+import Name            ( OccName(..), SYN_IE(Module) )
 import Lex             ( isLexConId )
-import PprStyle                ( PprStyle(..) )
-import PrelMods
+import Outputable      ( Outputable(..), PprStyle(..) )
+import PrelMods                ( pRELUDE )
 import Pretty
 import SrcLoc          ( mkGeneratedSrcLoc, noSrcLoc, SrcLoc )
 import Util            ( nOfThem, pprError, panic )
@@ -67,14 +75,22 @@ wlkEntId = wlkQid (\occ -> if isLexConId occ
                           else VarOcc occ)
 
 wlkQid :: (FAST_STRING -> OccName) -> U_qid -> UgnM RdrName
+
+-- There are three kinds of qid:
+--     qualified name (noqual)         A.x
+--     unqualified name (aqual)        x
+--     special name (gid)              [], (), ->, (,,,)
+-- The special names always mean "Prelude.whatever"; that's why
+-- they are distinct.  So if you write "()", it's just as if  you
+-- had written "Prelude.()".  NB: The (qualified) prelude is always in scope,
+-- so the renamer will find it.
+
 wlkQid mk_occ_name (U_noqual name)
   = returnUgn (Unqual (mk_occ_name name))
 wlkQid mk_occ_name (U_aqual  mod name)
-  = returnUgn (Qual mod (mk_occ_name name))
-
-       -- I don't understand this one!  It is what shows up when we meet (), [], or (,,,).
+  = returnUgn (Qual mod (mk_occ_name name) HiFile)
 wlkQid mk_occ_name (U_gid n name)
-  = returnUgn (Unqual (mk_occ_name name))
+  = returnUgn (Qual pRELUDE (mk_occ_name name) HiFile)
 
 rdTCId  pt = rdU_qid pt `thenUgn` \ qid -> wlkTCId qid
 rdVarId pt = rdU_qid pt `thenUgn` \ qid -> wlkVarId qid
@@ -91,19 +107,19 @@ cvFlag 1 = True
 %************************************************************************
 
 \begin{code}
-#if __GLASGOW_HASKELL__ >= 200
+#if __GLASGOW_HASKELL__ == 201
 # define PACK_STR packCString
-# define CCALL_THEN `stThen`
+#elif __GLASGOW_HASKELL__ >= 202
+# define PACK_STR mkFastCharString
 #else
-# define PACK_STR _packCString
-# define CCALL_THEN `thenPrimIO`
+# define PACK_STR mkFastCharString
 #endif
 
 rdModule :: IO (Module,                    -- this module's name
                RdrNameHsModule)    -- the main goods
 
 rdModule
-  = _ccall_ hspmain CCALL_THEN \ pt -> -- call the Yacc parser!
+  = _ccall_ hspmain `CCALL_THEN` \ pt -> -- call the Yacc parser!
     let
        srcfile  = PACK_STR ``input_filename'' -- What A Great Hack! (TM)
     in
@@ -222,7 +238,7 @@ wlkExpr expr
       U_doe gdo srcline ->                     -- do expression
        mkSrcLocUgn srcline             $ \ src_loc ->
        wlkList rd_stmt gdo     `thenUgn` \ stmts ->
-       returnUgn (HsDo stmts src_loc)
+       returnUgn (HsDo DoStmt stmts src_loc)
         where
        rd_stmt pt
          = rdU_tree pt `thenUgn` \ bind ->
@@ -248,31 +264,9 @@ wlkExpr expr
 
       U_comprh cexp cquals -> -- list comprehension
        wlkExpr cexp            `thenUgn` \ expr  ->
-       wlkList rd_qual cquals  `thenUgn` \ quals ->
-       returnUgn (ListComp expr quals)
-       where
-         rd_qual pt
-           = rdU_tree pt       `thenUgn` \ qual ->
-             wlk_qual qual
-
-         wlk_qual qual
-           = case qual of
-               U_guard exp ->
-                 wlkExpr exp   `thenUgn` \ expr ->
-                 returnUgn (FilterQual expr)
-
-               U_qual qpat qexp ->
-                 wlkPat  qpat  `thenUgn` \ pat  ->
-                 wlkExpr qexp  `thenUgn` \ expr ->
-                 returnUgn (GeneratorQual pat expr)
-
-               U_seqlet seqlet ->
-                 wlkBinding seqlet     `thenUgn` \ bs ->
-                 getSrcFileUgn         `thenUgn` \ sf ->
-                 let
-                     binds = cvBinds sf cvValSig bs
-                 in
-                 returnUgn (LetQual binds)
+       wlkQuals cquals         `thenUgn` \ quals ->
+       getSrcLocUgn            `thenUgn` \ loc ->
+       returnUgn (HsDo ListComp (quals ++ [ReturnStmt expr]) loc)
 
       U_eenum efrom estep eto -> -- arithmetic sequence
        wlkExpr efrom           `thenUgn` \ e1  ->
@@ -330,7 +324,7 @@ wlkExpr expr
       U_record con rbinds -> -- record construction
        wlkDataId  con          `thenUgn` \ rcon     ->
        wlkList rdRbind rbinds  `thenUgn` \ recbinds ->
-       returnUgn (RecordCon (HsVar rcon) recbinds)
+       returnUgn (RecordCon rcon recbinds)
 
       U_rupdate updexp updbinds -> -- record update
        wlkExpr updexp           `thenUgn` \ aexp ->
@@ -360,6 +354,43 @@ rdRbind pt
        Nothing -> (rvar, HsVar rvar, True{-pun-})
        Just re -> (rvar, re,         False)
     )
+
+wlkQuals cquals
+  = wlkList rd_qual cquals
+  where
+         rd_qual pt
+           = rdU_tree pt       `thenUgn` \ qual ->
+             wlk_qual qual
+
+         wlk_qual qual
+           = case qual of
+               U_guard exp ->
+                 wlkExpr exp   `thenUgn` \ expr ->
+                 getSrcLocUgn  `thenUgn` \ loc ->
+                 returnUgn (GuardStmt expr loc)
+
+               U_qual qpat qexp ->
+                 wlkPat  qpat  `thenUgn` \ pat  ->
+                 wlkExpr qexp  `thenUgn` \ expr ->
+                 getSrcLocUgn  `thenUgn` \ loc ->
+                 returnUgn (BindStmt pat expr loc)
+
+               U_seqlet seqlet ->
+                 wlkBinding seqlet     `thenUgn` \ bs ->
+                 getSrcFileUgn         `thenUgn` \ sf ->
+                 let
+                     binds = cvBinds sf cvValSig bs
+                 in
+                 returnUgn (LetStmt binds)
+               U_let letvdefs letvexpr -> 
+                   wlkBinding letvdefs `thenUgn` \ binding ->
+                   wlkExpr    letvexpr `thenUgn` \ expr    ->
+                   getSrcLocUgn        `thenUgn` \ loc ->
+                   getSrcFileUgn       `thenUgn` \ sf      ->
+                   let
+                    binds = cvBinds sf cvValSig binding
+                   in
+                   returnUgn (GuardStmt (HsLet binds expr) loc)
 \end{code}
 
 Patterns: just bear in mind that lists of patterns are represented as
@@ -386,6 +417,11 @@ wlkPat pat
        wlkPat lazyp    `thenUgn` \ pat ->
        returnUgn (LazyPatIn pat)
 
+      U_plusp avar lit ->
+       wlkVarId avar   `thenUgn` \ var ->
+       wlkLiteral lit  `thenUgn` \ lit ->
+       returnUgn (NPlusKPatIn var lit)
+
       U_wildp -> returnUgn WildPatIn   -- wildcard pattern
 
       U_lit lit ->                     -- literal pattern
@@ -410,12 +446,15 @@ wlkPat pat
            _ -> getSrcLocUgn   `thenUgn` \ loc ->
                 let
                     err = addErrLoc loc "Illegal pattern `application'"
-                                    (\sty -> ppInterleave ppSP (map (ppr sty) (lpat:lpats)))
-                    msg = ppShow 100 (err PprForUser)
+                                    (\sty -> hsep (map (ppr sty) (lpat:lpats)))
+                    msg = show (err (PprForUser opt_PprUserLength))
                 in
-#if __GLASGOW_HASKELL__ >= 200
+#if __GLASGOW_HASKELL__ == 201
                 ioToUgnM  (GHCbase.ioToPrimIO (hPutStr stderr msg)) `thenUgn` \ _ ->
                 ioToUgnM  (GHCbase.ioToPrimIO (ghcExit 1))          `thenUgn` \ _ ->
+#elif __GLASGOW_HASKELL__ >= 202
+                ioToUgnM  (IOBase.ioToPrimIO (hPutStr stderr msg)) `thenUgn` \ _ ->
+                ioToUgnM  (IOBase.ioToPrimIO (ghcExit 1))           `thenUgn` \ _ ->
 #else
                 ioToUgnM  (hPutStr stderr msg) `thenUgn` \ _ ->
                 ioToUgnM  (ghcExit 1)          `thenUgn` \ _ ->
@@ -488,8 +527,13 @@ wlkLiteral ulit
   where
     as_char s     = _HEAD_ s
     as_integer s  = readInteger (_UNPK_ s)
-#if __GLASGOW_HASKELL__ >= 200
+#if __GLASGOW_HASKELL__ == 201
     as_rational s = GHCbase.readRational__ (_UNPK_ s) -- non-std
+#elif __GLASGOW_HASKELL__ == 202
+    as_rational s = case readRational (_UNPK_ s) of { [(a,_)] -> a }
+#elif __GLASGOW_HASKELL__ >= 203
+    as_rational s = readRational__ (_UNPK_ s) -- use non-std readRational__ 
+                                             -- to handle rationals with leading '-'
 #else
     as_rational s = _readRational (_UNPK_ s) -- non-std
 #endif
@@ -524,16 +568,16 @@ wlkBinding binding
        wlkTyConAndTyVars  ttype    `thenUgn` \ (tycon, tyvars) ->
        wlkList rdConDecl  tcons    `thenUgn` \ cons        ->
        wlkDerivings       tderivs  `thenUgn` \ derivings   ->
-       returnUgn (RdrTyDecl (TyData ctxt tycon tyvars cons derivings noDataPragmas src_loc))
+       returnUgn (RdrTyDecl (TyData DataType ctxt tycon tyvars cons derivings noDataPragmas src_loc))
 
        -- "newtype" declaration
       U_ntbind ntctxt nttype ntcon ntderivs srcline ->
        mkSrcLocUgn        srcline          $ \ src_loc     ->
        wlkContext         ntctxt   `thenUgn` \ ctxt        ->
        wlkTyConAndTyVars  nttype   `thenUgn` \ (tycon, tyvars) ->
-       wlkList rdConDecl  ntcon    `thenUgn` \ [con]       ->
+       wlkList rdConDecl  ntcon    `thenUgn` \ cons        ->
        wlkDerivings       ntderivs `thenUgn` \ derivings   ->
-       returnUgn (RdrTyDecl (TyNew ctxt tycon tyvars con derivings noDataPragmas src_loc))
+       returnUgn (RdrTyDecl (TyData NewType ctxt tycon tyvars cons derivings noDataPragmas src_loc))
 
        -- "type" declaration
       U_nbind nbindid nbindas srcline ->               
@@ -647,12 +691,6 @@ wlk_sig_thing (U_inline_uprag ivar srcline)
     wlkVarId   ivar            `thenUgn` \ var     ->
     returnUgn (RdrInlineValSig (InlineSig var src_loc))
 
-       -- "deforest me" user-pragma
-wlk_sig_thing (U_deforest_uprag ivar srcline)
-  = mkSrcLocUgn srcline                        $ \ src_loc ->
-    wlkVarId   ivar            `thenUgn` \ var     ->
-    returnUgn (RdrDeforestSig (DeforestSig var src_loc))
-
        -- "magic" unfolding user-pragma
 wlk_sig_thing (U_magicuf_uprag ivar str srcline)
   = mkSrcLocUgn srcline                        $ \ src_loc ->
@@ -689,6 +727,12 @@ wlkHsType ttype
 
 wlkMonoType ttype
   = case ttype of
+               -- Glasgow extension: nested polymorhism
+      U_context tcontextl tcontextt -> -- context
+       wlkContext  tcontextl   `thenUgn` \ ctxt ->
+       wlkMonoType tcontextt   `thenUgn` \ ty   ->
+       returnUgn (HsPreForAllTy ctxt ty)
+
       U_namedtvar tv -> -- type variable
        wlkTvId tv      `thenUgn` \ tyvar ->
        returnUgn (MonoTyVar tyvar)
@@ -744,7 +788,7 @@ mk_class_assertion :: RdrNameHsType -> (RdrName, RdrNameHsType)
 
 mk_class_assertion (MonoTyApp (MonoTyVar name) ty@(MonoTyVar tyname)) = (name, ty)
 mk_class_assertion other
-  = pprError "ERROR: malformed type context: " (ppr PprForUser other)
+  = pprError "ERROR: malformed type context: " (ppr (PprForUser opt_PprUserLength) other)
     -- regrettably, the parser does let some junk past
     -- e.g., f :: Num {-nothing-} => a -> ...
 \end{code}
@@ -757,30 +801,35 @@ rdConDecl pt
 
 wlkConDecl :: U_constr -> UgnM RdrNameConDecl
 
+wlkConDecl (U_constrcxt ccxt ccdecl)
+  = wlkContext ccxt            `thenUgn` \ theta ->
+    wlkConDecl ccdecl          `thenUgn` \ (ConDecl con _ details loc) ->
+    returnUgn (ConDecl con theta details loc)
+
 wlkConDecl (U_constrpre ccon ctys srcline)
   = mkSrcLocUgn srcline                        $ \ src_loc ->
     wlkDataId  ccon            `thenUgn` \ con     ->
     wlkList     rdBangType ctys        `thenUgn` \ tys     ->
-    returnUgn (ConDecl con tys src_loc)
+    returnUgn (ConDecl con [] (VanillaCon tys) src_loc)
 
 wlkConDecl (U_constrinf cty1 cop cty2 srcline)
   = mkSrcLocUgn srcline                        $ \ src_loc ->
     wlkBangType cty1           `thenUgn` \ ty1     ->
     wlkDataId  cop             `thenUgn` \ op      ->
     wlkBangType cty2           `thenUgn` \ ty2     ->
-    returnUgn (ConOpDecl ty1 op ty2 src_loc)
+    returnUgn (ConDecl op [] (InfixCon ty1 ty2) src_loc)
 
 wlkConDecl (U_constrnew ccon cty srcline)
   = mkSrcLocUgn srcline                        $ \ src_loc ->
     wlkDataId  ccon            `thenUgn` \ con     ->
     wlkMonoType cty            `thenUgn` \ ty      ->
-    returnUgn (NewConDecl con ty src_loc)
+    returnUgn (ConDecl con [] (NewCon ty) src_loc)
 
 wlkConDecl (U_constrrec ccon cfields srcline)
   = mkSrcLocUgn srcline                        $ \ src_loc      ->
     wlkDataId  ccon            `thenUgn` \ con          ->
     wlkList rd_field cfields   `thenUgn` \ fields_lists ->
-    returnUgn (RecConDecl con fields_lists src_loc)
+    returnUgn (ConDecl con [] (RecCon fields_lists) src_loc)
   where
     rd_field :: ParseTree -> UgnM ([RdrName], BangType RdrName)
     rd_field pt
@@ -828,7 +877,7 @@ rdMatch pt
   where
     rd_gd_expr pt
       = rdU_pbinding pt `thenUgn` \ (U_pgdexp g e) ->
-       wlkExpr      g  `thenUgn` \ guard ->
+       wlkQuals     g  `thenUgn` \ guard ->
        wlkExpr      e  `thenUgn` \ expr  ->
        returnUgn (guard, expr)
 \end{code}
@@ -866,11 +915,11 @@ rdImport :: ParseTree
         -> UgnM RdrNameImportDecl
 
 rdImport pt
-  = rdU_binding pt `thenUgn` \ (U_import imod iqual ias ispec srcline) ->
+  = rdU_binding pt `thenUgn` \ (U_import imod iqual ias ispec isrc srcline) ->
     mkSrcLocUgn srcline                                $ \ src_loc      ->
     wlkMaybe rdU_stringId ias          `thenUgn` \ maybe_as    ->
     wlkMaybe rd_spec ispec             `thenUgn` \ maybe_spec  ->
-    returnUgn (ImportDecl imod (cvFlag iqual) maybe_as maybe_spec src_loc)
+    returnUgn (ImportDecl imod (cvFlag iqual) (cvIfaceFlavour isrc) maybe_as maybe_spec src_loc)
   where
     rd_spec pt = rdU_either pt                 `thenUgn` \ spec ->
       case spec of
@@ -878,6 +927,9 @@ rdImport pt
                      returnUgn (False, ents)
        U_right pt -> rdEntities pt     `thenUgn` \ ents ->
                      returnUgn (True, ents)
+
+cvIfaceFlavour 0 = HiFile      -- No pragam
+cvIfaceFlavour 1 = HiBootFile  -- {-# SOURCE #-}
 \end{code}
 
 \begin{code}