+
+ qual_gre :: GlobalRdrElt -> GlobalRdrElt
+ -- Transform top-level GREs from the module being compiled
+ -- so that they are out of the way of new definitions in a Template
+ -- Haskell bracket
+ -- See Note [Top-level Names in Template Haskell decl quotes]
+ -- Seems like 5 times as much work as it deserves!
+ --
+ -- For a LocalDef we make a (fake) qualified imported GRE for a
+ -- local GRE so that the original *qualified* name is still in scope
+ -- but the *unqualified* one no longer is. What a hack!
+
+ qual_gre gre@(GRE { gre_prov = LocalDef, gre_name = name })
+ | isExternalName name = gre { gre_prov = Imported [imp_spec] }
+ | otherwise = gre
+ -- Do not shadow Internal (ie Template Haskell) Names
+ -- See Note [Top-level Names in Template Haskell decl quotes]
+ where
+ mod = ASSERT2( isExternalName name, ppr name) moduleName (nameModule name)
+ imp_spec = ImpSpec { is_item = ImpAll, is_decl = decl_spec }
+ decl_spec = ImpDeclSpec { is_mod = mod, is_as = mod,
+ is_qual = True, -- Qualified only!
+ is_dloc = srcLocSpan (nameSrcLoc name) }
+
+ qual_gre gre@(GRE { gre_prov = Imported specs })
+ = gre { gre_prov = Imported (map qual_spec specs) }
+
+ qual_spec spec@(ImpSpec { is_decl = decl_spec })
+ = spec { is_decl = decl_spec { is_qual = True } }