diff options
author | Alan Zimmerman <alan.zimm@gmail.com> | 2017-05-19 14:56:09 +0200 |
---|---|---|
committer | Alan Zimmerman <alan.zimm@gmail.com> | 2017-06-06 00:16:20 +0200 |
commit | 8e6ec0fa7431b0454b09c0011a615f0845df1198 (patch) | |
tree | d6b3604e0ceac3d81d0510669f7ccce9a2bf3ae2 /compiler/main | |
parent | c9eb4385aad248118650725b7b699bb97ee21c0d (diff) | |
download | haskell-8e6ec0fa7431b0454b09c0011a615f0845df1198.tar.gz |
Udate hsSyn AST to use Trees that Grow
Summary:
See https://ghc.haskell.org/trac/ghc/wiki/ImplementingTreesThatGrow
This commit prepares the ground for a full extensible AST, by replacing the type
parameter for the hsSyn data types with a set of indices into type families,
data GhcPs -- ^ Index for GHC parser output
data GhcRn -- ^ Index for GHC renamer output
data GhcTc -- ^ Index for GHC typechecker output
These are now used instead of `RdrName`, `Name` and `Id`/`TcId`/`Var`
Where the original name type is required in a polymorphic context, this is
accessible via the IdP type family, defined as
type family IdP p
type instance IdP GhcPs = RdrName
type instance IdP GhcRn = Name
type instance IdP GhcTc = Id
These types are declared in the new 'hsSyn/HsExtension.hs' module.
To gain a better understanding of the extension mechanism, it has been applied
to `HsLit` only, also replacing the `SourceText` fields in them with extension
types.
To preserve extension generality, a type class is introduced to capture the
`SourceText` interface, which must be honoured by all of the extension points
which originally had a `SourceText`. The class is defined as
class HasSourceText a where
-- Provide setters to mimic existing constructors
noSourceText :: a
sourceText :: String -> a
setSourceText :: SourceText -> a
getSourceText :: a -> SourceText
And the constraint is captured in `SourceTextX`, which is a constraint type
listing all the extension points that make use of the class.
Updating Haddock submodule to match.
Test Plan: ./validate
Reviewers: simonpj, shayan-najd, goldfire, austin, bgamari
Subscribers: rwbarton, thomie, mpickering
Differential Revision: https://phabricator.haskell.org/D3609
Diffstat (limited to 'compiler/main')
-rw-r--r-- | compiler/main/GHC.hs | 8 | ||||
-rw-r--r-- | compiler/main/HeaderInfo.hs | 7 | ||||
-rw-r--r-- | compiler/main/Hooks.hs | 28 | ||||
-rw-r--r-- | compiler/main/HscMain.hs | 16 | ||||
-rw-r--r-- | compiler/main/HscStats.hs | 3 | ||||
-rw-r--r-- | compiler/main/HscTypes.hs | 34 | ||||
-rw-r--r-- | compiler/main/InteractiveEval.hs | 10 |
7 files changed, 56 insertions, 50 deletions
diff --git a/compiler/main/GHC.hs b/compiler/main/GHC.hs index 48767bf12d..af00dab4f2 100644 --- a/compiler/main/GHC.hs +++ b/compiler/main/GHC.hs @@ -831,10 +831,10 @@ instance TypecheckedMod DesugaredModule where instance DesugaredMod DesugaredModule where coreModule m = dm_core_module m -type ParsedSource = Located (HsModule RdrName) -type RenamedSource = (HsGroup Name, [LImportDecl Name], Maybe [LIE Name], +type ParsedSource = Located (HsModule GhcPs) +type RenamedSource = (HsGroup GhcRn, [LImportDecl GhcRn], Maybe [LIE GhcRn], Maybe LHsDocString) -type TypecheckedSource = LHsBinds Id +type TypecheckedSource = LHsBinds GhcTc -- NOTE: -- - things that aren't in the output of the typechecker right now: @@ -1481,7 +1481,7 @@ lookupName name = parser :: String -- ^ Haskell module source text (full Unicode is supported) -> DynFlags -- ^ the flags -> FilePath -- ^ the filename (for source locations) - -> (WarningMessages, Either ErrorMessages (Located (HsModule RdrName))) + -> (WarningMessages, Either ErrorMessages (Located (HsModule GhcPs))) parser str dflags filename = let diff --git a/compiler/main/HeaderInfo.hs b/compiler/main/HeaderInfo.hs index c69e0f331d..be38e53f3d 100644 --- a/compiler/main/HeaderInfo.hs +++ b/compiler/main/HeaderInfo.hs @@ -18,7 +18,6 @@ module HeaderInfo ( getImports #include "HsVersions.h" -import RdrName import HscTypes import Parser ( parseHeader ) import Lexer @@ -99,8 +98,8 @@ getImports dflags buf filename source_filename = do mkPrelImports :: ModuleName -> SrcSpan -- Attribute the "import Prelude" to this location - -> Bool -> [LImportDecl RdrName] - -> [LImportDecl RdrName] + -> Bool -> [LImportDecl GhcPs] + -> [LImportDecl GhcPs] -- Construct the implicit declaration "import Prelude" (or not) -- -- NB: opt_NoImplicitPrelude is slightly different to import Prelude (); @@ -119,7 +118,7 @@ mkPrelImports this_mod loc implicit_prelude import_decls <- import_decls , unLoc mod == pRELUDE_NAME ] - preludeImportDecl :: LImportDecl RdrName + preludeImportDecl :: LImportDecl GhcPs preludeImportDecl = L loc $ ImportDecl { ideclSourceSrc = NoSourceText, ideclName = L loc pRELUDE_NAME, diff --git a/compiler/main/Hooks.hs b/compiler/main/Hooks.hs index eefdde4b88..59126e98d5 100644 --- a/compiler/main/Hooks.hs +++ b/compiler/main/Hooks.hs @@ -26,23 +26,24 @@ module Hooks ( Hooks ) where import DynFlags -import Name import PipelineMonad import HscTypes import HsDecls import HsBinds import HsExpr import OrdList -import Id import TcRnTypes import Bag import RdrName +import Name +import Id import CoreSyn import GHCi.RemoteTypes import SrcLoc import Type import System.Process import BasicTypes +import HsExtension import Data.Maybe @@ -75,17 +76,24 @@ emptyHooks = Hooks } data Hooks = Hooks - { dsForeignsHook :: Maybe ([LForeignDecl Id] -> DsM (ForeignStubs, OrdList (Id, CoreExpr))) - , tcForeignImportsHook :: Maybe ([LForeignDecl Name] -> TcM ([Id], [LForeignDecl Id], Bag GlobalRdrElt)) - , tcForeignExportsHook :: Maybe ([LForeignDecl Name] -> TcM (LHsBinds TcId, [LForeignDecl TcId], Bag GlobalRdrElt)) + { dsForeignsHook :: Maybe ([LForeignDecl GhcTc] + -> DsM (ForeignStubs, OrdList (Id, CoreExpr))) + , tcForeignImportsHook :: Maybe ([LForeignDecl GhcRn] + -> TcM ([Id], [LForeignDecl GhcTc], Bag GlobalRdrElt)) + , tcForeignExportsHook :: Maybe ([LForeignDecl GhcRn] + -> TcM (LHsBinds GhcTcId, [LForeignDecl GhcTcId], Bag GlobalRdrElt)) , hscFrontendHook :: Maybe (ModSummary -> Hsc FrontendResult) - , hscCompileCoreExprHook :: Maybe (HscEnv -> SrcSpan -> CoreExpr -> IO ForeignHValue) + , hscCompileCoreExprHook :: + Maybe (HscEnv -> SrcSpan -> CoreExpr -> IO ForeignHValue) , ghcPrimIfaceHook :: Maybe ModIface - , runPhaseHook :: Maybe (PhasePlus -> FilePath -> DynFlags -> CompPipeline (PhasePlus, FilePath)) + , runPhaseHook :: Maybe (PhasePlus -> FilePath -> DynFlags + -> CompPipeline (PhasePlus, FilePath)) , runMetaHook :: Maybe (MetaHook TcM) - , linkHook :: Maybe (GhcLink -> DynFlags -> Bool -> HomePackageTable -> IO SuccessFlag) - , runRnSpliceHook :: Maybe (HsSplice Name -> RnM (HsSplice Name)) - , getValueSafelyHook :: Maybe (HscEnv -> Name -> Type -> IO (Maybe HValue)) + , linkHook :: Maybe (GhcLink -> DynFlags -> Bool + -> HomePackageTable -> IO SuccessFlag) + , runRnSpliceHook :: Maybe (HsSplice GhcRn -> RnM (HsSplice GhcRn)) + , getValueSafelyHook :: Maybe (HscEnv -> Name -> Type + -> IO (Maybe HValue)) , createIservProcessHook :: Maybe (CreateProcess -> IO ProcessHandle) } diff --git a/compiler/main/HscMain.hs b/compiler/main/HscMain.hs index f4ca3a8b34..d2b6e5bd6e 100644 --- a/compiler/main/HscMain.hs +++ b/compiler/main/HscMain.hs @@ -292,7 +292,7 @@ hscGetModuleInterface hsc_env0 mod = runInteractiveHsc hsc_env0 $ do -- ----------------------------------------------------------------------------- -- | Rename some import declarations -hscRnImportDecls :: HscEnv -> [LImportDecl RdrName] -> IO GlobalRdrEnv +hscRnImportDecls :: HscEnv -> [LImportDecl GhcPs] -> IO GlobalRdrEnv hscRnImportDecls hsc_env0 import_decls = runInteractiveHsc hsc_env0 $ do hsc_env <- getHscEnv ioMsgMaybe $ tcRnImportDecls hsc_env import_decls @@ -382,7 +382,7 @@ hscParse' mod_summary -- can become a Nothing and decide whether this should instead throw an -- exception/signal an error. type RenamedStuff = - (Maybe (HsGroup Name, [LImportDecl Name], Maybe [LIE Name], + (Maybe (HsGroup GhcRn, [LImportDecl GhcRn], Maybe [LIE GhcRn], Maybe LHsDocString)) -- | Rename and typecheck a module, additionally returning the renamed syntax @@ -1495,7 +1495,7 @@ hscStmtWithLocation hsc_env0 stmt source linenumber = liftIO $ hscParsedStmt hsc_env parsed_stmt hscParsedStmt :: HscEnv - -> GhciLStmt RdrName -- ^ The parsed statement + -> GhciLStmt GhcPs -- ^ The parsed statement -> IO ( Maybe ([Id] , ForeignHValue {- IO [HValue] -} , FixityEnv)) @@ -1631,7 +1631,7 @@ hscAddSptEntries hsc_env entries = do -} -hscImport :: HscEnv -> String -> IO (ImportDecl RdrName) +hscImport :: HscEnv -> String -> IO (ImportDecl GhcPs) hscImport hsc_env str = runInteractiveHsc hsc_env $ do (L _ (HsModule{hsmodImports=is})) <- hscParseThing parseModule str @@ -1663,7 +1663,7 @@ hscKcType hsc_env0 normalise str = runInteractiveHsc hsc_env0 $ do ty <- hscParseType str ioMsgMaybe $ tcRnType hsc_env normalise ty -hscParseExpr :: String -> Hsc (LHsExpr RdrName) +hscParseExpr :: String -> Hsc (LHsExpr GhcPs) hscParseExpr expr = do hsc_env <- getHscEnv maybe_stmt <- hscParseStmt expr @@ -1672,15 +1672,15 @@ hscParseExpr expr = do _ -> throwErrors $ unitBag $ mkPlainErrMsg (hsc_dflags hsc_env) noSrcSpan (text "not an expression:" <+> quotes (text expr)) -hscParseStmt :: String -> Hsc (Maybe (GhciLStmt RdrName)) +hscParseStmt :: String -> Hsc (Maybe (GhciLStmt GhcPs)) hscParseStmt = hscParseThing parseStmt hscParseStmtWithLocation :: String -> Int -> String - -> Hsc (Maybe (GhciLStmt RdrName)) + -> Hsc (Maybe (GhciLStmt GhcPs)) hscParseStmtWithLocation source linenumber stmt = hscParseThingWithLocation source linenumber parseStmt stmt -hscParseType :: String -> Hsc (LHsType RdrName) +hscParseType :: String -> Hsc (LHsType GhcPs) hscParseType = hscParseThing parseType hscParseIdentifier :: HscEnv -> String -> IO (Located RdrName) diff --git a/compiler/main/HscStats.hs b/compiler/main/HscStats.hs index 241dfd8095..598cb5be0a 100644 --- a/compiler/main/HscStats.hs +++ b/compiler/main/HscStats.hs @@ -11,7 +11,6 @@ module HscStats ( ppSourceStats ) where import Bag import HsSyn import Outputable -import RdrName import SrcLoc import Util @@ -19,7 +18,7 @@ import Data.Char import Data.Foldable (foldl') -- | Source Statistics -ppSourceStats :: Bool -> Located (HsModule RdrName) -> SDoc +ppSourceStats :: Bool -> Located (HsModule GhcPs) -> SDoc ppSourceStats short (L _ (HsModule _ exports imports ldecls _ _)) = (if short then hcat else vcat) (map pp_val diff --git a/compiler/main/HscTypes.hs b/compiler/main/HscTypes.hs index 62ae8cce5a..70af19de9b 100644 --- a/compiler/main/HscTypes.hs +++ b/compiler/main/HscTypes.hs @@ -701,35 +701,35 @@ hptObjs hpt = concat (map (maybe [] linkableObjs . hm_linkable) (eltsHpt hpt)) -- | The supported metaprogramming result types data MetaRequest - = MetaE (LHsExpr RdrName -> MetaResult) - | MetaP (LPat RdrName -> MetaResult) - | MetaT (LHsType RdrName -> MetaResult) - | MetaD ([LHsDecl RdrName] -> MetaResult) - | MetaAW (Serialized -> MetaResult) + = MetaE (LHsExpr GhcPs -> MetaResult) + | MetaP (LPat GhcPs -> MetaResult) + | MetaT (LHsType GhcPs -> MetaResult) + | MetaD ([LHsDecl GhcPs] -> MetaResult) + | MetaAW (Serialized -> MetaResult) -- | data constructors not exported to ensure correct result type data MetaResult - = MetaResE { unMetaResE :: LHsExpr RdrName } - | MetaResP { unMetaResP :: LPat RdrName } - | MetaResT { unMetaResT :: LHsType RdrName } - | MetaResD { unMetaResD :: [LHsDecl RdrName] } + = MetaResE { unMetaResE :: LHsExpr GhcPs } + | MetaResP { unMetaResP :: LPat GhcPs } + | MetaResT { unMetaResT :: LHsType GhcPs } + | MetaResD { unMetaResD :: [LHsDecl GhcPs] } | MetaResAW { unMetaResAW :: Serialized } -type MetaHook f = MetaRequest -> LHsExpr Id -> f MetaResult +type MetaHook f = MetaRequest -> LHsExpr GhcTc -> f MetaResult -metaRequestE :: Functor f => MetaHook f -> LHsExpr Id -> f (LHsExpr RdrName) +metaRequestE :: Functor f => MetaHook f -> LHsExpr GhcTc -> f (LHsExpr GhcPs) metaRequestE h = fmap unMetaResE . h (MetaE MetaResE) -metaRequestP :: Functor f => MetaHook f -> LHsExpr Id -> f (LPat RdrName) +metaRequestP :: Functor f => MetaHook f -> LHsExpr GhcTc -> f (LPat GhcPs) metaRequestP h = fmap unMetaResP . h (MetaP MetaResP) -metaRequestT :: Functor f => MetaHook f -> LHsExpr Id -> f (LHsType RdrName) +metaRequestT :: Functor f => MetaHook f -> LHsExpr GhcTc -> f (LHsType GhcPs) metaRequestT h = fmap unMetaResT . h (MetaT MetaResT) -metaRequestD :: Functor f => MetaHook f -> LHsExpr Id -> f [LHsDecl RdrName] +metaRequestD :: Functor f => MetaHook f -> LHsExpr GhcTc -> f [LHsDecl GhcPs] metaRequestD h = fmap unMetaResD . h (MetaD MetaResD) -metaRequestAW :: Functor f => MetaHook f -> LHsExpr Id -> f Serialized +metaRequestAW :: Functor f => MetaHook f -> LHsExpr GhcTc -> f Serialized metaRequestAW h = fmap unMetaResAW . h (MetaAW MetaResAW) {- @@ -1545,7 +1545,7 @@ data InteractiveContext } data InteractiveImport - = IIDecl (ImportDecl RdrName) + = IIDecl (ImportDecl GhcPs) -- ^ Bring the exports of a particular module -- (filtered by an import decl) into scope @@ -2936,7 +2936,7 @@ instance Binary IfaceTrustInfo where -} data HsParsedModule = HsParsedModule { - hpm_module :: Located (HsModule RdrName), + hpm_module :: Located (HsModule GhcPs), hpm_src_files :: [FilePath], -- ^ extra source files (e.g. from #includes). The lexer collects -- these from '# <file> <line>' pragmas, which the C preprocessor diff --git a/compiler/main/InteractiveEval.hs b/compiler/main/InteractiveEval.hs index 8b5a6b6af7..8e396cc16a 100644 --- a/compiler/main/InteractiveEval.hs +++ b/compiler/main/InteractiveEval.hs @@ -246,7 +246,7 @@ withVirtualCWD m = do gbracket set_cwd reset_cwd $ \_ -> m -parseImportDecl :: GhcMonad m => String -> m (ImportDecl RdrName) +parseImportDecl :: GhcMonad m => String -> m (ImportDecl GhcPs) parseImportDecl expr = withSession $ \hsc_env -> liftIO $ hscImport hsc_env expr emptyHistory :: Int -> BoundedList History @@ -674,7 +674,7 @@ findGlobalRdrEnv hsc_env imports ([], imods_env) -> Right (foldr plusGlobalRdrEnv idecls_env imods_env) (err : _, _) -> Left err } where - idecls :: [LImportDecl RdrName] + idecls :: [LImportDecl GhcPs] idecls = [noLoc d | IIDecl d <- imports] imods :: [ModuleName] @@ -841,7 +841,7 @@ typeKind normalise str = withSession $ \hsc_env -> do -- | Parse an expression, the parsed expression can be further processed and -- passed to compileParsedExpr. -parseExpr :: GhcMonad m => String -> m (LHsExpr RdrName) +parseExpr :: GhcMonad m => String -> m (LHsExpr GhcPs) parseExpr expr = withSession $ \hsc_env -> do liftIO $ runInteractiveHsc hsc_env $ hscParseExpr expr @@ -859,7 +859,7 @@ compileExprRemote expr = do -- | Compile an parsed expression (before renaming), run it and deliver -- the resulting HValue. -compileParsedExprRemote :: GhcMonad m => LHsExpr RdrName -> m ForeignHValue +compileParsedExprRemote :: GhcMonad m => LHsExpr GhcPs -> m ForeignHValue compileParsedExprRemote expr@(L loc _) = withSession $ \hsc_env -> do -- > let _compileParsedExpr = expr -- Create let stmt from expr to make hscParsedStmt happy. @@ -879,7 +879,7 @@ compileParsedExprRemote expr@(L loc _) = withSession $ \hsc_env -> do liftIO $ throwIO (fromSerializableException e) _ -> panic "compileParsedExpr" -compileParsedExpr :: GhcMonad m => LHsExpr RdrName -> m HValue +compileParsedExpr :: GhcMonad m => LHsExpr GhcPs -> m HValue compileParsedExpr expr = do fhv <- compileParsedExprRemote expr dflags <- getDynFlags |