-newTopBinder :: Module -> RdrName -> SrcLoc -> RnM d Name
- -- newTopBinder puts into the cache the binder with the
- -- module information set correctly. When the decl is later renamed,
- -- the binding site will thereby get the correct module.
- -- There maybe occurrences that don't have the correct Module, but
- -- by the typechecker will propagate the binding definition to all
- -- the occurrences, so that doesn't matter
-
-newTopBinder mod rdr_name loc
- = -- First check the cache
- -- traceRn (text "newTopBinder" <+> ppr mod <+> ppr loc) `thenRn_`
-
- -- There should never be a qualified name in a binding position (except in instance decls)
- -- The parser doesn't check this because the same parser parses instance decls
- (if isQual rdr_name then
- qualNameErr (text "its declaration") (rdr_name,loc)
- else
- returnRn ()
- ) `thenRn_`
-
- getNameSupplyRn `thenRn` \ (us, cache, ipcache) ->
- let
- occ = rdrNameOcc rdr_name
- key = (moduleName mod, occ)
- in
- case lookupFM cache key of
-
- -- A hit in the cache! We are at the binding site of the name, and
- -- this is the moment when we know all about
- -- a) the Name's host Module (in particular, which
- -- package it comes from)
- -- b) its defining SrcLoc
- -- So we update this info
-
- Just name -> let
- new_name = setNameModuleAndLoc name mod loc
- new_cache = addToFM cache key new_name
- in
- setNameSupplyRn (us, new_cache, ipcache) `thenRn_`
- -- traceRn (text "newTopBinder: overwrite" <+> ppr new_name) `thenRn_`
- returnRn new_name
-
- -- Miss in the cache!
- -- Build a completely new Name, and put it in the cache
- -- Even for locally-defined names we use implicitImportProvenance;
- -- updateProvenances will set it to rights
- Nothing -> let
- (us', us1) = splitUniqSupply us
- uniq = uniqFromSupply us1
- new_name = mkGlobalName uniq mod occ loc
- new_cache = addToFM cache key new_name
- in
- setNameSupplyRn (us', new_cache, ipcache) `thenRn_`
- -- traceRn (text "newTopBinder: new" <+> ppr new_name) `thenRn_`
- returnRn new_name
-
-
-newGlobalName :: ModuleName -> OccName -> RnM d Name
- -- Used for *occurrences*. We make a place-holder Name, really just
- -- to agree on its unique, which gets overwritten when we read in
- -- the binding occurence later (newTopBinder)
- -- The place-holder Name doesn't have the right SrcLoc, and its
- -- Module won't have the right Package either.
- --
- -- (We have to pass a ModuleName, not a Module, because we may be
- -- simply looking at an occurrence M.x in an interface file.)
- --
- -- This means that a renamed program may have incorrect info
- -- on implicitly-imported occurrences, but the correct info on the
- -- *binding* declaration. It's the type checker that propagates the
- -- correct information to all the occurrences.
- -- Since implicitly-imported names never occur in error messages,
- -- it doesn't matter that we get the correct info in place till later,
- -- (but since it affects DLL-ery it does matter that we get it right
- -- in the end).
-newGlobalName mod_name occ
- = getNameSupplyRn `thenRn` \ (us, cache, ipcache) ->
- let
- key = (mod_name, occ)
- in
- case lookupFM cache key of
- Just name -> -- traceRn (text "newGlobalName: hit" <+> ppr name) `thenRn_`
- returnRn name
-
- Nothing -> setNameSupplyRn (us', new_cache, ipcache) `thenRn_`
- -- traceRn (text "newGlobalName: new" <+> ppr name) `thenRn_`
- returnRn name
- where
- (us', us1) = splitUniqSupply us
- uniq = uniqFromSupply us1
- mod = mkVanillaModule mod_name
- name = mkGlobalName uniq mod occ noSrcLoc
- new_cache = addToFM cache key name
-
-newIPName rdr_name
- = getNameSupplyRn `thenRn` \ (us, cache, ipcache) ->
- case lookupFM ipcache key of
- Just name -> returnRn name
- Nothing -> setNameSupplyRn (us', cache, new_ipcache) `thenRn_`
- returnRn name
- where
- (us', us1) = splitUniqSupply us
- uniq = uniqFromSupply us1
- name = mkIPName uniq key
- new_ipcache = addToFM ipcache key name
- where key = (rdrNameOcc rdr_name)
+newTopSrcBinder :: Module -> Maybe Name -> Located RdrName -> RnM Name
+newTopSrcBinder mod mb_parent (L loc rdr_name)
+ | Just name <- isExact_maybe rdr_name
+ = returnM name
+
+ | isOrig rdr_name
+ = ASSERT( rdr_mod == moduleName mod || rdr_mod == rOOT_MAIN_Name )
+ -- When reading External Core we get Orig names as binders,
+ -- but they should agree with the module gotten from the monad
+ --
+ -- Except for the ":Main.main = ..." definition inserted into
+ -- the Main module
+ --
+ -- Because of this latter case, we take the module from the RdrName,
+ -- not from the environment. In principle, it'd be fine to have an
+ -- arbitrary mixture of external core definitions in a single module,
+ -- (apart from module-initialisation issues, perhaps).
+ newGlobalBinder (mkHomeModule rdr_mod) (rdrNameOcc rdr_name) mb_parent
+ (srcSpanStart loc) --TODO, should pass the whole span
+
+ | otherwise
+ = newGlobalBinder mod (rdrNameOcc rdr_name) mb_parent (srcSpanStart loc)
+ where
+ rdr_mod = rdrNameModule rdr_name