+extractHsCtxtTyNames :: LHsContext Name -> NameSet
+extractHsCtxtTyNames (L _ ctxt)
+ = foldr (unionNameSets . extractHsPredTyNames . unLoc) emptyNameSet ctxt
+
+-- You don't import or export implicit parameters,
+-- so don't mention the IP names
+extractHsPredTyNames (HsClassP cls tys)
+ = unitNameSet cls `unionNameSets` extractHsTyNames_s tys
+extractHsPredTyNames (HsIParam n ty)
+ = extractHsTyNames ty
+\end{code}
+
+
+%************************************************************************
+%* *
+\subsection{Free variables of declarations}
+%* *
+%************************************************************************
+
+Return the Names that must be in scope if we are to use this declaration.
+In all cases this is set up for interface-file declarations:
+ - for class decls we ignore the bindings
+ - for instance decls likewise, plus the pragmas
+ - for rule decls, we ignore HsRules
+ - for data decls, we ignore derivings
+
+ *** See "THE NAMING STORY" in HsDecls ****
+
+\begin{code}
+----------------
+hsSigsFVs :: [LSig Name] -> FreeVars
+hsSigsFVs sigs = plusFVs (map (hsSigFVs.unLoc) sigs)
+
+hsSigFVs (TypeSig v ty) = extractHsTyNames ty
+hsSigFVs (SpecInstSig ty) = extractHsTyNames ty
+hsSigFVs (SpecSig v ty inl) = extractHsTyNames ty
+hsSigFVs other = emptyFVs
+
+----------------
+conDeclFVs (L _ (ConDecl { con_qvars = tyvars, con_cxt = context,
+ con_details = details, con_res = res_ty}))
+ = delFVs (map hsLTyVarName tyvars) $
+ extractHsCtxtTyNames context `plusFV`
+ conDetailsFVs details `plusFV`
+ conResTyFVs res_ty
+
+conResTyFVs ResTyH98 = emptyFVs
+conResTyFVs (ResTyGADT ty) = extractHsTyNames ty
+
+conDetailsFVs (PrefixCon btys) = plusFVs (map bangTyFVs btys)
+conDetailsFVs (InfixCon bty1 bty2) = bangTyFVs bty1 `plusFV` bangTyFVs bty2
+conDetailsFVs (RecCon flds) = plusFVs [bangTyFVs bty | (_, bty) <- flds]
+
+bangTyFVs bty = extractHsTyNames (getBangType bty)