+implicitImportProvenance = NonLocalDef ImplicitImport False
+
+newTopBinder :: Module -> OccName -> RnM d Name
+newTopBinder mod occ
+ = -- First check the cache
+ traceRn (text "newTopBinder" <+> ppr mod <+> ppr occ) `thenRn_`
+
+ getNameSupplyRn `thenRn` \ (us, inst_ns, cache, ipcache) ->
+ let
+ key = (moduleName mod, occ)
+ in
+ case lookupFM cache key of
+
+ -- A hit in the cache! We are at the binding site of the name, which is
+ -- the time we know all about the Name's host Module (in particular, which
+ -- package it comes from), so update the Module in the name.
+ -- But otherwise *leave the Provenance alone*:
+ --
+ -- * For imported names, the Provenance may already be correct.
+ -- e.g. We imported Prelude.hi, and set the provenance of PrelShow.Show
+ -- to 'UserImport from Prelude'. Note that we havn't yet opened PrelShow.hi
+ -- Later we find we really need PrelShow.Show, so we open PrelShow.hi, and
+ -- that's when we find the binding occurrence of Show.
+ --
+ -- * For locally defined names, we do a setProvenance on the Name
+ -- right after newTopBinder, and then use updateProveances to finally
+ -- set the provenances in the cache correctly.
+ --
+ -- NB: for wired-in names it's important not to
+ -- forget that they are wired in even when compiling that module
+ -- (else we spit out redundant defns into the interface file)
+
+ Just name -> let
+ new_name = setNameModule name mod
+ new_cache = addToFM cache key new_name
+ in
+ setNameSupplyRn (us, inst_ns, 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 implicitImportProvenance
+ new_cache = addToFM cache key new_name
+ in
+ setNameSupplyRn (us', inst_ns, new_cache, ipcache) `thenRn_`
+ traceRn (text "newTopBinder: new" <+> ppr new_name) `thenRn_`
+ returnRn new_name
+
+
+mkImportedGlobalName :: 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 (newImportedBinder)
+ -- The place-holder Name doesn't have the right Provenance, and its
+ -- Module won't have the right Package either
+ --
+ -- 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).
+mkImportedGlobalName mod_name occ