--- to a module. 'compileToCore' first invokes 'checkModule' to parse and
--- typecheck the module, then desugars it and returns the resulting list
--- of Core bindings if successful.
-compileToCore :: Session -> ModuleName -> FilePath -> IO (Maybe [CoreBind])
-compileToCore session@(Session ref) mod fn = do
- hsc_env <- readIORef ref
- maybeCheckedModule <- checkModule session mod
- case maybeCheckedModule of
- Nothing -> return Nothing
- Just checkedMod -> do
- let parsedMod = parsedSource checkedMod
- -- Note: this typechecks the module twice (because checkModule
- -- also calls tcRnModule), but arranging for checkModule to
- -- return the type env would require changing a lot of data
- -- structures, so I'm leaving it like that for now.
- (_, maybe_tc_result) <- tcRnModule hsc_env HsSrcFile False parsedMod
- case maybe_tc_result of
- -- TODO: this ignores the type error messages and just returns Nothing
- Nothing -> return Nothing
- Just tcgEnv -> do
- let dflags = hsc_dflags hsc_env
- location <- mkHomeModLocation dflags mod fn
- maybeModGuts <- deSugar hsc_env location tcgEnv
- case maybeModGuts of
- Nothing -> return Nothing
- Just mg -> return $ Just $ mg_binds mg
+-- to a module. 'compileToCore' invokes 'checkModule' to parse, typecheck, and
+-- desugar the module, then returns the resulting list of Core bindings if
+-- successful.
+compileToCore :: Session -> FilePath -> IO (Maybe [CoreBind])
+compileToCore session fn = do
+ -- First, set the target to the desired filename
+ target <- guessTarget fn Nothing
+ addTarget session target
+ load session LoadAllTargets
+ -- Then find dependencies
+ maybeModGraph <- depanal session [] True
+ case maybeModGraph of
+ Nothing -> return Nothing
+ Just modGraph -> do
+ let modSummary = expectJust "compileToCore" $
+ find ((== fn) . msHsFilePath) modGraph
+ -- Now we have the module name;
+ -- parse, typecheck and desugar the module
+ let mod = ms_mod_name modSummary
+ maybeCheckedModule <- checkModule session mod True
+ case maybeCheckedModule of
+ Nothing -> return Nothing
+ Just checkedMod -> return $ coreBinds checkedMod