mkPrintUnqualified :: DynFlags -> GlobalRdrEnv -> PrintUnqualified
mkPrintUnqualified dflags env = QueryQualify qual_name
                                             (mkQualModule dflags)
                                             (mkQualPackage dflags)
  where
  qual_name mod occ
        | [] <- unqual_gres
        , modulePackageKey mod `elem` [primPackageKey, basePackageKey, thPackageKey]
        , not (isDerivedOccName occ)
        = NameUnqual   -- For names from ubiquitous packages that come with GHC, if
                       -- there are no entities called unqualified 'occ', then
                       -- print unqualified.  Doing so does not cause ambiguity,
                       -- and it reduces the amount of qualification in error
                       -- messages.  We can't do this for all packages, because we
                       -- might get errors like "Can't unify T with T".  But the
                       -- ubiquitous packages don't contain any such gratuitous
                       -- name clashes.
                       --
                       -- A motivating example is 'Constraint'. It's often not in
                       -- scope, but printing GHC.Prim.Constraint seems overkill.

        | [gre] <- unqual_gres
        , right_name gre
        = NameUnqual   -- If there's a unique entity that's in scope
                       -- unqualified with 'occ' AND that entity is
                       -- the right one, then we can use the unqualified name

        | [gre] <- qual_gres
        = NameQual (get_qual_mod (gre_prov gre))

        | null qual_gres
        = if null (lookupGRE_RdrName (mkRdrQual (moduleName mod) occ) env)
          then NameNotInScope1
          else NameNotInScope2

        | otherwise
        = NameNotInScope1   -- Can happen if 'f' is bound twice in the module
                            -- Eg  f = True; g = 0; f = False
      where
        right_name gre = nameModule_maybe (gre_name gre) == Just mod

        unqual_gres = lookupGRE_RdrName (mkRdrUnqual occ) env
        qual_gres   = filter right_name (lookupGlobalRdrEnv env occ)

        get_qual_mod LocalDef      = moduleName mod
        get_qual_mod (Imported is) = ASSERT( not (null is) ) is_as (is_decl (head is))

    -- we can mention a module P:M without the P: qualifier iff
    -- "import M" would resolve unambiguously to P:M.  (if P is the
    -- current package we can just assume it is unqualified).

-- | Creates a function for formatting modules based on two heuristics:
-- (1) if the module is the current module, don't qualify, and (2) if there
-- is only one exposed package which exports this module, don't qualify.
mkPrintUnqualified :: DynFlags -> GlobalRdrEnv -> PrintUnqualified
mkPrintUnqualified dflags env = QueryQualify qual_name
                                             (mkQualModule dflags)
                                             (mkQualPackage dflags)
  where
  qual_name mod occ
        | [] <- unqual_gres
        , modulePackageKey mod `elem` [primPackageKey, basePackageKey, thPackageKey]
        , not (isDerivedOccName occ)
        = NameUnqual   -- For names from ubiquitous packages that come with GHC, if
                       -- there are no entities called unqualified 'occ', then
                       -- print unqualified.  Doing so does not cause ambiguity,
                       -- and it reduces the amount of qualification in error
                       -- messages.  We can't do this for all packages, because we
                       -- might get errors like "Can't unify T with T".  But the
                       -- ubiquitous packages don't contain any such gratuitous
                       -- name clashes.
                       --
                       -- A motivating example is 'Constraint'. It's often not in
                       -- scope, but printing GHC.Prim.Constraint seems overkill.

        | [gre] <- unqual_gres
        , right_name gre
        = NameUnqual   -- If there's a unique entity that's in scope
                       -- unqualified with 'occ' AND that entity is
                       -- the right one, then we can use the unqualified name

        | [gre] <- qual_gres
        = NameQual (get_qual_mod (gre_prov gre))

        | null qual_gres
        = if null (lookupGRE_RdrName (mkRdrQual (moduleName mod) occ) env)
          then NameNotInScope1
          else NameNotInScope2

        | otherwise
        = NameNotInScope1   -- Can happen if 'f' is bound twice in the module
                            -- Eg  f = True; g = 0; f = False
      where
        right_name gre = nameModule_maybe (gre_name gre) == Just mod

        unqual_gres = lookupGRE_RdrName (mkRdrUnqual occ) env
        qual_gres   = filter right_name (lookupGlobalRdrEnv env occ)

        get_qual_mod LocalDef      = moduleName mod
        get_qual_mod (Imported is) = ASSERT( not (null is) ) is_as (is_decl (head is))

    -- we can mention a module P:M without the P: qualifier iff
    -- "import M" would resolve unambiguously to P:M.  (if P is the
    -- current package we can just assume it is unqualified).

-- | Creates a function for formatting modules based on two heuristics:
-- (1) if the module is the current module, don't qualify, and (2) if there
-- is only one exposed package which exports this module, don't qualify.
mkPrintUnqualified dflags env = QueryQualify qual_name
                                             (mkQualModule dflags)
                                             (mkQualPackage dflags)
  where
  qual_name mod occ
        | [] <- unqual_gres
        , modulePackageKey mod `elem` [primPackageKey, basePackageKey, thPackageKey]
        , not (isDerivedOccName occ)
        = NameUnqual   -- For names from ubiquitous packages that come with GHC, if
                       -- there are no entities called unqualified 'occ', then
                       -- print unqualified.  Doing so does not cause ambiguity,
                       -- and it reduces the amount of qualification in error
                       -- messages.  We can't do this for all packages, because we
                       -- might get errors like "Can't unify T with T".  But the
                       -- ubiquitous packages don't contain any such gratuitous
                       -- name clashes.
                       --
                       -- A motivating example is 'Constraint'. It's often not in
                       -- scope, but printing GHC.Prim.Constraint seems overkill.

        | [gre] <- unqual_gres
        , right_name gre
        = NameUnqual   -- If there's a unique entity that's in scope
                       -- unqualified with 'occ' AND that entity is
                       -- the right one, then we can use the unqualified name

        | [gre] <- qual_gres
        = NameQual (get_qual_mod (gre_prov gre))

        | null qual_gres
        = if null (lookupGRE_RdrName (mkRdrQual (moduleName mod) occ) env)
          then NameNotInScope1
          else NameNotInScope2

        | otherwise
        = NameNotInScope1   -- Can happen if 'f' is bound twice in the module
                            -- Eg  f = True; g = 0; f = False
      where
        right_name gre = nameModule_maybe (gre_name gre) == Just mod

        unqual_gres = lookupGRE_RdrName (mkRdrUnqual occ) env
        qual_gres   = filter right_name (lookupGlobalRdrEnv env occ)

        get_qual_mod LocalDef      = moduleName mod
        get_qual_mod (Imported is) = ASSERT( not (null is) ) is_as (is_decl (head is))

    -- we can mention a module P:M without the P: qualifier iff
    -- "import M" would resolve unambiguously to P:M.  (if P is the
    -- current package we can just assume it is unqualified).

-- | Creates a function for formatting modules based on two heuristics:
-- (1) if the module is the current module, don't qualify, and (2) if there
-- is only one exposed package which exports this module, don't qualify.