[project @ 1997-10-07 14:51:49 by simonm]
[ghc-hetmet.git] / ghc / compiler / reader / ReadPrefix.lhs
index 2d10052..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
@@ -121,7 +137,7 @@ rdModule
     wlkBinding         hmodlist `thenUgn` \ binding    ->
 
     let
-       val_decl    = ValD (add_main_sig modname (cvBinds srcfile cvValSig binding))
+       val_decl    = ValD (cvBinds srcfile cvValSig binding)
        other_decls = cvOtherDecls binding
     in
     returnUgn (modname,
@@ -133,28 +149,6 @@ rdModule
                          (val_decl: other_decls)
                          src_loc
                        )
-  where
-    add_main_sig modname binds
-      = if modname == mAIN then
-           let
-              s = Sig (varUnqual SLIT("main")) (io_ty SLIT("IO")) mkGeneratedSrcLoc
-           in
-           add_sig binds s
-
-       else if modname == gHC_MAIN then
-           let
-              s = Sig (varUnqual SLIT("mainPrimIO")) (io_ty SLIT("PrimIO"))  mkGeneratedSrcLoc
-           in
-           add_sig binds s
-
-       else -- add nothing
-           binds
-      where
-       add_sig (SingleBind b)  s = BindWith b [s]
-       add_sig (BindWith b ss) s = BindWith b (s:ss)
-       add_sig _               _ = panic "rdModule:add_sig"
-
-       io_ty t = MonoTyApp (Unqual (TCOcc t)) [MonoTupleTy dummyRdrTcName []]
 \end{code}
 
 %************************************************************************
@@ -244,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 ->
@@ -270,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  ->
@@ -335,7 +307,7 @@ wlkExpr expr
        wlkVarId  fun   `thenUgn` \ op    ->
        wlkExpr arg1    `thenUgn` \ expr1 ->
        wlkExpr arg2    `thenUgn` \ expr2 ->
-       returnUgn (OpApp expr1 (HsVar op) expr2)
+       returnUgn (mkOpApp expr1 op expr2)
 
       U_negate nexp ->                 -- prefix negation
        wlkExpr nexp    `thenUgn` \ expr ->
@@ -352,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 ->
@@ -382,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
@@ -408,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
@@ -426,18 +440,21 @@ wlkPat pat
        wlkPat r                `thenUgn` \ rpat         ->
        collect_pats l [rpat]   `thenUgn` \ (lpat,lpats) ->
        (case lpat of
-           VarPatIn x        -> returnUgn (x,  lpats)
-           ConPatIn x []     -> returnUgn (x,  lpats)
-           ConOpPatIn x op y -> returnUgn (op, x:y:lpats)
+           VarPatIn x          -> returnUgn (x,  lpats)
+           ConPatIn x []       -> returnUgn (x,  lpats)
+           ConOpPatIn x op _ y -> returnUgn (op, x:y:lpats)
            _ -> 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` \ _ ->
@@ -460,7 +477,7 @@ wlkPat pat
        wlkVarId fun    `thenUgn` \ op   ->
        wlkPat arg1     `thenUgn` \ pat1 ->
        wlkPat arg2     `thenUgn` \ pat2 ->
-       returnUgn (ConOpPatIn pat1 op pat2)
+       returnUgn (ConOpPatIn pat1 op (error "ConOpPatIn:fixity") pat2)
 
       U_negate npat ->                 -- negated pattern
        wlkPat npat     `thenUgn` \ pat ->
@@ -510,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
@@ -546,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 ->               
@@ -661,7 +683,7 @@ wlk_sig_thing (U_dspec_uprag itycon dspec_tys srcline)
   = mkSrcLocUgn srcline                         $ \ src_loc ->
     wlkTCId    itycon           `thenUgn` \ tycon   ->
     wlkList rdMonoType dspec_tys `thenUgn` \ tys     ->
-    returnUgn (RdrSpecDataSig (SpecDataSig tycon (MonoTyApp tycon tys) src_loc))
+    returnUgn (RdrSpecDataSig (SpecDataSig tycon (foldl MonoTyApp (MonoTyVar tycon) tys) src_loc))
 
        -- value inlining user-pragma
 wlk_sig_thing (U_inline_uprag ivar srcline)
@@ -669,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 ->
@@ -711,33 +727,24 @@ 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)
 
       U_tname tcon -> -- type constructor
        wlkTCId tcon    `thenUgn` \ tycon ->
-       returnUgn (MonoTyApp tycon [])
+       returnUgn (MonoTyVar tycon)
 
       U_tapp t1 t2 ->
+       wlkMonoType t1          `thenUgn` \ ty1 ->
        wlkMonoType t2          `thenUgn` \ ty2 ->
-       collect t1 [ty2]        `thenUgn` \ (tycon, tys) ->
-       returnUgn (MonoTyApp tycon tys)
-       where
-       collect t acc
-         = case t of
-             U_tapp t1 t2   -> wlkMonoType t2  `thenUgn` \ ty2 ->
-                               collect t1 (ty2:acc)
-             U_tname tcon   -> wlkTCId tcon    `thenUgn` \ tycon ->
-                               returnUgn (tycon, acc)
-             U_namedtvar tv -> wlkTvId tv      `thenUgn` \ tyvar ->
-                               returnUgn (tyvar, acc)
-             U_tllist _ -> panic "tlist"
-             U_ttuple _ -> panic "ttuple"
-             U_tfun _ _ -> panic "tfun"
-             U_tbang _  -> panic "tbang"
-             U_context _ _ -> panic "context"
-             _ -> panic "something else"
+       returnUgn (MonoTyApp ty1 ty2)
              
       U_tllist tlist -> -- list type
        wlkMonoType tlist       `thenUgn` \ ty ->
@@ -760,11 +767,12 @@ wlkContext          :: U_list  -> UgnM RdrNameContext
 wlkClassAssertTy  :: U_ttype -> UgnM (RdrName, HsTyVar RdrName)
 
 wlkTyConAndTyVars ttype
-  = wlkMonoType ttype  `thenUgn` \ (MonoTyApp tycon ty_args) ->
+  = wlkMonoType ttype  `thenUgn` \ ty ->
     let
-       args = [ UserTyVar a | (MonoTyVar a) <- ty_args ]
+       split (MonoTyApp fun (MonoTyVar arg)) args = split fun (UserTyVar arg : args)
+       split (MonoTyVar tycon)               args = (tycon,args)
     in
-    returnUgn (tycon, args)
+    returnUgn (split ty [])
 
 wlkContext list
   = wlkList rdMonoType list `thenUgn` \ tys ->
@@ -778,9 +786,9 @@ wlkClassAssertTy xs
 
 mk_class_assertion :: RdrNameHsType -> (RdrName, RdrNameHsType)
 
-mk_class_assertion (MonoTyApp name [ty@(MonoTyVar tyname)]) = (name, ty)
+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}
@@ -793,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
@@ -864,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}
@@ -902,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
@@ -914,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}