-- Copyright (c) 2020, Shayne Fletcher. All rights reserved.
-- SPDX-License-Identifier: BSD-3-Clause.

{-# LANGUAGE CPP #-}
{-# LANGUAGE ViewPatterns #-}
#include "ghclib_api.h"
module Language.Haskell.GhclibParserEx.GHC.Hs.Pat(
    patToStr, strToPat
  , fromPChar
  , hasPFieldsDotDot
  , isPFieldWildcard, isPWildcard, isPFieldPun, isPatTypeSig, isPBangPat, isPViewPat
 ) where

#if defined (GHCLIB_API_HEAD) || defined (GHCLIB_API_900) || defined (GHCLIB_API_810)
import GHC.Hs
#else
import HsSyn
#endif
#if defined (GHCLIB_API_HEAD) || defined (GHCLIB_API_900)
import GHC.Types.SrcLoc
import GHC.Builtin.Types
import GHC.Types.Name.Reader
import GHC.Types.Name
import GHC.Data.FastString
#else
import SrcLoc
import TysWiredIn
import RdrName
import OccName
import FastString
#endif

patToStr :: LPat GhcPs -> String
#if defined (GHCLIB_API_HEAD)
patToStr (L _ (ConPat _ (L _ x) (PrefixCon [] []))) | occNameString (rdrNameOcc x) == "True" = "True"
patToStr (L _ (ConPat _ (L _ x) (PrefixCon [] []))) | occNameString (rdrNameOcc x) == "False" = "False"
patToStr (L _ (ConPat _ (L _ x) (PrefixCon [] []))) | occNameString (rdrNameOcc x) == "[]" = "[]"
patToStr _ = ""
#elif defined (GHCLIB_API_900)
patToStr :: LPat GhcPs -> String
patToStr (L _ (ConPat _ (L _ x) (PrefixCon []))) | OccName -> String
occNameString (RdrName -> OccName
rdrNameOcc ConLikeP GhcPs
RdrName
x) String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
"True" = String
"True"
patToStr (L _ (ConPat _ (L _ x) (PrefixCon []))) | OccName -> String
occNameString (RdrName -> OccName
rdrNameOcc ConLikeP GhcPs
RdrName
x) String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
"False" = String
"False"
patToStr (L _ (ConPat _ (L _ x) (PrefixCon []))) | OccName -> String
occNameString (RdrName -> OccName
rdrNameOcc ConLikeP GhcPs
RdrName
x) String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
"[]" = String
"[]"
patToStr LPat GhcPs
_ = String
""
#elif defined (GHCLIB_API_810)
patToStr (L _ (ConPatIn (L _ x) (PrefixCon []))) | occNameString (rdrNameOcc x) == "True" = "True"
patToStr (L _ (ConPatIn (L _ x) (PrefixCon []))) | occNameString (rdrNameOcc x) == "False" = "False"
patToStr (L _ (ConPatIn (L _ x) (PrefixCon []))) | occNameString (rdrNameOcc x) == "[]" = "[]"
patToStr _ = ""
#else
patToStr (dL -> L _ (ConPatIn (L _ x) (PrefixCon []))) | occNameString (rdrNameOcc x) == "True" = "True"
patToStr (dL -> L _ (ConPatIn (L _ x) (PrefixCon []))) | occNameString (rdrNameOcc x) == "False" = "False"
patToStr (dL -> L _ (ConPatIn (L _ x) (PrefixCon []))) | occNameString (rdrNameOcc x) == "[]" = "[]"
patToStr _ = ""
#endif

strToPat :: String -> LPat GhcPs
strToPat :: String -> LPat GhcPs
strToPat String
z
  | String
z String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
"True"  =
#if defined (GHCLIB_API_HEAD) || defined (GHCLIB_API_900) || defined (GHCLIB_API_810)
  Pat GhcPs -> Located (Pat GhcPs)
forall e. e -> Located e
noLoc (Pat GhcPs -> Located (Pat GhcPs))
-> Pat GhcPs -> Located (Pat GhcPs)
forall a b. (a -> b) -> a -> b
$
#endif
#if defined (GHCLIB_API_HEAD)
    ConPat noExtField (noLoc true_RDR) (PrefixCon [] [])
#elif defined (GHCLIB_API_900)
    XConPat GhcPs
-> Located (ConLikeP GhcPs) -> HsConPatDetails GhcPs -> Pat GhcPs
forall p.
XConPat p -> Located (ConLikeP p) -> HsConPatDetails p -> Pat p
ConPat NoExtField
XConPat GhcPs
noExtField (RdrName -> Located RdrName
forall e. e -> Located e
noLoc RdrName
true_RDR) ([Located (Pat GhcPs)]
-> HsConDetails
     (Located (Pat GhcPs)) (HsRecFields GhcPs (Located (Pat GhcPs)))
forall arg rec. [arg] -> HsConDetails arg rec
PrefixCon [])
#else
    ConPatIn (noLoc true_RDR) (PrefixCon [])
#endif
  | String
z String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
"False" =
#if defined (GHCLIB_API_HEAD) || defined (GHCLIB_API_900) || defined (GHCLIB_API_810)
  Pat GhcPs -> Located (Pat GhcPs)
forall e. e -> Located e
noLoc (Pat GhcPs -> Located (Pat GhcPs))
-> Pat GhcPs -> Located (Pat GhcPs)
forall a b. (a -> b) -> a -> b
$
#endif
#if defined (GHCLIB_API_HEAD)
    ConPat noExtField (noLoc false_RDR) (PrefixCon [] [])
#elif defined (GHCLIB_API_900)
    XConPat GhcPs
-> Located (ConLikeP GhcPs) -> HsConPatDetails GhcPs -> Pat GhcPs
forall p.
XConPat p -> Located (ConLikeP p) -> HsConPatDetails p -> Pat p
ConPat NoExtField
XConPat GhcPs
noExtField (RdrName -> Located RdrName
forall e. e -> Located e
noLoc RdrName
false_RDR) ([Located (Pat GhcPs)]
-> HsConDetails
     (Located (Pat GhcPs)) (HsRecFields GhcPs (Located (Pat GhcPs)))
forall arg rec. [arg] -> HsConDetails arg rec
PrefixCon [])
#else
    ConPatIn (noLoc false_RDR) (PrefixCon [])
#endif
  | String
z String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
"[]"    =
#if defined (GHCLIB_API_HEAD) || defined (GHCLIB_API_900) || defined (GHCLIB_API_810)
  Pat GhcPs -> Located (Pat GhcPs)
forall e. e -> Located e
noLoc (Pat GhcPs -> Located (Pat GhcPs))
-> Pat GhcPs -> Located (Pat GhcPs)
forall a b. (a -> b) -> a -> b
$
#endif
#if defined (GHCLIB_API_HEAD)
    ConPat noExtField (noLoc $ nameRdrName nilDataConName) (PrefixCon [] [])
#elif defined (GHCLIB_API_900)
    XConPat GhcPs
-> Located (ConLikeP GhcPs) -> HsConPatDetails GhcPs -> Pat GhcPs
forall p.
XConPat p -> Located (ConLikeP p) -> HsConPatDetails p -> Pat p
ConPat NoExtField
XConPat GhcPs
noExtField (RdrName -> Located RdrName
forall e. e -> Located e
noLoc (RdrName -> Located RdrName) -> RdrName -> Located RdrName
forall a b. (a -> b) -> a -> b
$ Name -> RdrName
nameRdrName Name
nilDataConName) ([Located (Pat GhcPs)]
-> HsConDetails
     (Located (Pat GhcPs)) (HsRecFields GhcPs (Located (Pat GhcPs)))
forall arg rec. [arg] -> HsConDetails arg rec
PrefixCon [])
#else
    ConPatIn (noLoc $ nameRdrName nilDataConName) (PrefixCon [])
#endif
  | Bool
otherwise =
#if defined (GHCLIB_API_HEAD) || defined (GHCLIB_API_900) || defined (GHCLIB_API_810)
      Pat GhcPs -> Located (Pat GhcPs)
forall e. e -> Located e
noLoc (Pat GhcPs -> Located (Pat GhcPs))
-> Pat GhcPs -> Located (Pat GhcPs)
forall a b. (a -> b) -> a -> b
$ XVarPat GhcPs -> Located (IdP GhcPs) -> Pat GhcPs
forall p. XVarPat p -> Located (IdP p) -> Pat p
VarPat NoExtField
XVarPat GhcPs
noExtField (RdrName -> Located RdrName
forall e. e -> Located e
noLoc (RdrName -> Located RdrName) -> RdrName -> Located RdrName
forall a b. (a -> b) -> a -> b
$ FastString -> RdrName
mkVarUnqual (String -> FastString
fsLit String
z))
#else
      VarPat noExt (noLoc $ mkVarUnqual (fsLit z))
#endif

fromPChar :: LPat GhcPs -> Maybe Char
#if defined (GHCLIB_API_HEAD) || defined (GHCLIB_API_900) || defined (GHCLIB_API_810)
fromPChar :: LPat GhcPs -> Maybe Char
fromPChar (L _ (LitPat _ (HsChar _ x))) = Char -> Maybe Char
forall a. a -> Maybe a
Just Char
x
#else
fromPChar (dL -> L _ (LitPat _ (HsChar _ x))) = Just x
#endif
fromPChar LPat GhcPs
_ = Maybe Char
forall a. Maybe a
Nothing

-- Contains a '..' as in 'Foo{..}'
hasPFieldsDotDot :: HsRecFields GhcPs (LPat GhcPs) -> Bool
hasPFieldsDotDot :: HsRecFields GhcPs (LPat GhcPs) -> Bool
hasPFieldsDotDot HsRecFields {rec_dotdot :: forall p arg. HsRecFields p arg -> Maybe (Located Int)
rec_dotdot=Just Located Int
_} = Bool
True
hasPFieldsDotDot HsRecFields GhcPs (LPat GhcPs)
_ = Bool
False

-- Field has a '_' as in '{foo=_} or is punned e.g. '{foo}'.
isPFieldWildcard :: LHsRecField GhcPs (LPat GhcPs) -> Bool
#if defined (GHCLIB_API_HEAD) || defined (GHCLIB_API_900) || defined (GHCLIB_API_810)
isPFieldWildcard :: LHsRecField GhcPs (LPat GhcPs) -> Bool
isPFieldWildcard (L SrcSpan
_ HsRecField {hsRecFieldArg :: forall id arg. HsRecField' id arg -> arg
hsRecFieldArg=L _ WildPat {}}) = Bool
True
isPFieldWildcard (L SrcSpan
_ HsRecField {hsRecPun :: forall id arg. HsRecField' id arg -> Bool
hsRecPun=Bool
True}) = Bool
True
isPFieldWildcard (L SrcSpan
_ HsRecField {}) = Bool
False
#else
isPFieldWildcard (dL -> L _ HsRecField {hsRecFieldArg=LL _ WildPat {}}) = True
isPFieldWildcard (dL -> L _ HsRecField {hsRecPun=True}) = True
isPFieldWildcard (dL -> L _ HsRecField {}) = False
#endif

isPWildcard :: LPat GhcPs -> Bool
#if defined (GHCLIB_API_HEAD) || defined (GHCLIB_API_900) || defined (GHCLIB_API_810)
isPWildcard :: LPat GhcPs -> Bool
isPWildcard (L _ (WildPat _)) = Bool
True
#else
isPWildcard (dL -> L _ (WildPat _)) = True
#endif
isPWildcard LPat GhcPs
_ = Bool
False

isPFieldPun :: LHsRecField GhcPs (LPat GhcPs) -> Bool
#if defined (GHCLIB_API_HEAD) || defined (GHCLIB_API_900) || defined (GHCLIB_API_810)
isPFieldPun :: LHsRecField GhcPs (LPat GhcPs) -> Bool
isPFieldPun (L SrcSpan
_ HsRecField {hsRecPun :: forall id arg. HsRecField' id arg -> Bool
hsRecPun=Bool
True}) = Bool
True
#else
isPFieldPun (dL -> L _ HsRecField {hsRecPun=True}) = True
#endif
isPFieldPun LHsRecField GhcPs (LPat GhcPs)
_ = Bool
False

isPatTypeSig, isPBangPat, isPViewPat :: LPat GhcPs -> Bool
#if defined (GHCLIB_API_HEAD) || defined (GHCLIB_API_900) || defined (GHCLIB_API_810)
isPatTypeSig :: LPat GhcPs -> Bool
isPatTypeSig (L _ SigPat{}) = Bool
True; isPatTypeSig LPat GhcPs
_ = Bool
False
isPBangPat :: LPat GhcPs -> Bool
isPBangPat (L _ BangPat{}) = Bool
True; isPBangPat LPat GhcPs
_ = Bool
False
isPViewPat :: LPat GhcPs -> Bool
isPViewPat (L _ ViewPat{}) = Bool
True; isPViewPat LPat GhcPs
_ = Bool
False
#else
isPatTypeSig (dL -> L _ SigPat{}) = True; isPatTypeSig _ = False
isPBangPat (dL -> L _ BangPat{}) = True; isPBangPat _ = False
isPViewPat (dL -> L _ ViewPat{}) = True; isPViewPat _ = False
#endif