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

{-# OPTIONS_GHC -Wno-missing-fields #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE LambdaCase #-}
#include "ghclib_api.h"
module Language.Haskell.GhclibParserEx.GHC.Hs.Expr(
  isTag, isDol, isDot, isReturn, isSection, isRecConstr, isRecUpdate,
  isVar, isPar, isApp, isOpApp, isAnyApp, isLexeme, isLambda, isQuasiQuote,
  isDotApp, isTypeApp, isWHNF, isLCase,
  isFieldPun, isFieldPunUpdate, isRecStmt, isParComp, isMDo, isTupleSection, isString, isPrimLiteral,
  isSpliceDecl, isFieldWildcard, isUnboxed, isWholeFrac, isStrictMatch, isMultiIf, isProc, isTransStmt,
  hasFieldsDotDot,
  varToStr, strToVar,
  fromChar
  ) 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)
#if defined (GHCLIB_API_HEAD)
import GHC.Types.SourceText
#endif
import GHC.Types.SrcLoc
import GHC.Types.Name.Reader
import GHC.Types.Name
import GHC.Types.Basic
import GHC.Builtin.Types
#else
import SrcLoc
import RdrName
import OccName
import Name
import BasicTypes
import TysWiredIn
#endif
import Data.Ratio

-- 'True' if the provided expression is a variable with name 'tag'.
isTag :: String -> LHsExpr GhcPs -> Bool
isTag :: String -> LHsExpr GhcPs -> Bool
isTag String
tag = \case (L SrcSpan
_ (HsVar XVar GhcPs
_ (L SrcSpan
_ IdP GhcPs
s))) -> OccName -> String
occNameString (RdrName -> OccName
rdrNameOcc IdP GhcPs
RdrName
s) String -> String -> Bool
forall a. Eq a => a -> a -> Bool
== String
tag; LHsExpr GhcPs
_ -> Bool
False

isDot, isDol, isReturn, isSection, isRecConstr, isRecUpdate,
  isVar, isPar, isApp, isOpApp, isAnyApp, isLexeme, isQuasiQuote,
  isLambda, isDotApp, isTypeApp, isWHNF, isLCase :: LHsExpr GhcPs -> Bool
isDol :: LHsExpr GhcPs -> Bool
isDol = String -> LHsExpr GhcPs -> Bool
isTag String
"$"
isDot :: LHsExpr GhcPs -> Bool
isDot = String -> LHsExpr GhcPs -> Bool
isTag String
"."
isReturn :: LHsExpr GhcPs -> Bool
isReturn LHsExpr GhcPs
x = String -> LHsExpr GhcPs -> Bool
isTag String
"return" LHsExpr GhcPs
x Bool -> Bool -> Bool
|| String -> LHsExpr GhcPs -> Bool
isTag String
"pure" LHsExpr GhcPs
x -- Allow both 'pure' and 'return' as they have the same semantics.
isSection :: LHsExpr GhcPs -> Bool
isSection = \case (L SrcSpan
_ SectionL{}) -> Bool
True ; (L SrcSpan
_ SectionR{}) -> Bool
True; LHsExpr GhcPs
_ -> Bool
False
isRecConstr :: LHsExpr GhcPs -> Bool
isRecConstr = \case (L SrcSpan
_ RecordCon{}) -> Bool
True; LHsExpr GhcPs
_ -> Bool
False
isRecUpdate :: LHsExpr GhcPs -> Bool
isRecUpdate = \case (L SrcSpan
_ RecordUpd{}) -> Bool
True; LHsExpr GhcPs
_ -> Bool
False
isVar :: LHsExpr GhcPs -> Bool
isVar = \case (L SrcSpan
_ HsVar{}) -> Bool
True; LHsExpr GhcPs
_ -> Bool
False
isPar :: LHsExpr GhcPs -> Bool
isPar = \case (L SrcSpan
_ HsPar{}) -> Bool
True; LHsExpr GhcPs
_ -> Bool
False
isApp :: LHsExpr GhcPs -> Bool
isApp = \case (L SrcSpan
_ HsApp{}) -> Bool
True; LHsExpr GhcPs
_ -> Bool
False
isOpApp :: LHsExpr GhcPs -> Bool
isOpApp = \case (L SrcSpan
_ OpApp{}) -> Bool
True; LHsExpr GhcPs
_ -> Bool
False
isAnyApp :: LHsExpr GhcPs -> Bool
isAnyApp LHsExpr GhcPs
x = LHsExpr GhcPs -> Bool
isApp LHsExpr GhcPs
x Bool -> Bool -> Bool
|| LHsExpr GhcPs -> Bool
isOpApp LHsExpr GhcPs
x
isLexeme :: LHsExpr GhcPs -> Bool
isLexeme = \case (L SrcSpan
_ HsVar{}) -> Bool
True; (L SrcSpan
_ HsOverLit{}) -> Bool
True; (L SrcSpan
_ HsLit{}) -> Bool
True; LHsExpr GhcPs
_ -> Bool
False
isLambda :: LHsExpr GhcPs -> Bool
isLambda = \case (L SrcSpan
_ HsLam{}) -> Bool
True; LHsExpr GhcPs
_ -> Bool
False
isQuasiQuote :: LHsExpr GhcPs -> Bool
isQuasiQuote = \case (L SrcSpan
_ (HsSpliceE XSpliceE GhcPs
_ HsQuasiQuote{})) -> Bool
True; LHsExpr GhcPs
_ -> Bool
False
isDotApp :: LHsExpr GhcPs -> Bool
isDotApp = \case (L SrcSpan
_ (OpApp XOpApp GhcPs
_ LHsExpr GhcPs
_ LHsExpr GhcPs
op LHsExpr GhcPs
_)) -> LHsExpr GhcPs -> Bool
isDot LHsExpr GhcPs
op; LHsExpr GhcPs
_ -> Bool
False
isTypeApp :: LHsExpr GhcPs -> Bool
isTypeApp = \case (L SrcSpan
_ HsAppType{}) -> Bool
True; LHsExpr GhcPs
_ -> Bool
False
isWHNF :: LHsExpr GhcPs -> Bool
isWHNF = \case
  (L SrcSpan
_ (HsVar XVar GhcPs
_ (L SrcSpan
_ IdP GhcPs
x))) -> RdrName -> Bool
isRdrDataCon IdP GhcPs
RdrName
x
  (L SrcSpan
_ (HsLit XLitE GhcPs
_ HsLit GhcPs
x)) -> case HsLit GhcPs
x of HsString{} -> Bool
False; HsInt{} -> Bool
False; HsRat{} -> Bool
False; HsLit GhcPs
_ -> Bool
True
  (L SrcSpan
_ HsLam{}) -> Bool
True
  (L SrcSpan
_ ExplicitTuple{}) -> Bool
True
  (L SrcSpan
_ ExplicitList{}) -> Bool
True
  (L SrcSpan
_ (HsPar XPar GhcPs
_ LHsExpr GhcPs
x)) -> LHsExpr GhcPs -> Bool
isWHNF LHsExpr GhcPs
x
  (L SrcSpan
_ (ExprWithTySig XExprWithTySig GhcPs
_ LHsExpr GhcPs
x LHsSigWcType (NoGhcTc GhcPs)
_)) -> LHsExpr GhcPs -> Bool
isWHNF LHsExpr GhcPs
x
  -- Other (unknown) constructors may have bang patterns in them, so
  -- approximate.
  (L SrcSpan
_ (HsApp XApp GhcPs
_ (L SrcSpan
_ (HsVar XVar GhcPs
_ (L SrcSpan
_ IdP GhcPs
x))) LHsExpr GhcPs
_))
    | OccName -> String
occNameString (RdrName -> OccName
rdrNameOcc IdP GhcPs
RdrName
x) String -> [String] -> Bool
forall (t :: * -> *) a. (Foldable t, Eq a) => a -> t a -> Bool
`elem` [String
"Just", String
"Left", String
"Right"] -> Bool
True
  LHsExpr GhcPs
_ -> Bool
False
isLCase :: LHsExpr GhcPs -> Bool
isLCase = \case (L SrcSpan
_ HsLamCase{}) -> Bool
True; LHsExpr GhcPs
_ -> Bool
False

#if defined (GHCLIB_API_HEAD) || defined (GHCLIB_API_901)
isStrictMatch :: HsMatchContext GhcPs -> Bool
#else
isStrictMatch :: HsMatchContext RdrName -> Bool
#endif
isStrictMatch :: HsMatchContext RdrName -> Bool
isStrictMatch = \case FunRhs{mc_strictness :: forall id. HsMatchContext id -> SrcStrictness
mc_strictness=SrcStrictness
SrcStrict} -> Bool
True; HsMatchContext RdrName
_ -> Bool
False

-- Field is punned e.g. '{foo}'.
isFieldPun :: LHsRecField GhcPs (LHsExpr GhcPs) -> Bool
isFieldPun :: LHsRecField GhcPs (LHsExpr GhcPs) -> Bool
isFieldPun = \case (L SrcSpan
_ HsRecField {hsRecPun :: forall id arg. HsRecField' id arg -> Bool
hsRecPun=Bool
True}) -> Bool
True; LHsRecField GhcPs (LHsExpr GhcPs)
_ -> Bool
False

-- Field puns in updates have a different type to field puns in
-- constructions.
isFieldPunUpdate :: HsRecField' (AmbiguousFieldOcc GhcPs) (LHsExpr GhcPs) -> Bool
isFieldPunUpdate :: HsRecField' (AmbiguousFieldOcc GhcPs) (LHsExpr GhcPs) -> Bool
isFieldPunUpdate = \case HsRecField {hsRecPun :: forall id arg. HsRecField' id arg -> Bool
hsRecPun=Bool
True} -> Bool
True; HsRecField' (AmbiguousFieldOcc GhcPs) (LHsExpr GhcPs)
_ -> Bool
False

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

isRecStmt :: StmtLR GhcPs GhcPs (LHsExpr GhcPs) -> Bool
isRecStmt :: StmtLR GhcPs GhcPs (LHsExpr GhcPs) -> Bool
isRecStmt = \case RecStmt{} -> Bool
True; StmtLR GhcPs GhcPs (LHsExpr GhcPs)
_ -> Bool
False

isParComp :: StmtLR GhcPs GhcPs (LHsExpr GhcPs) -> Bool
isParComp :: StmtLR GhcPs GhcPs (LHsExpr GhcPs) -> Bool
isParComp = \case ParStmt{} -> Bool
True; StmtLR GhcPs GhcPs (LHsExpr GhcPs)
_ -> Bool
False

#if defined (GHCLIB_API_HEAD) || defined (GHCLIB_API_900)
isMDo :: HsStmtContext GhcRn -> Bool
isMDo = \case MDoExpr _ -> True; _ -> False
#else
isMDo :: HsStmtContext Name -> Bool
isMDo :: HsStmtContext Name -> Bool
isMDo = \case HsStmtContext Name
MDoExpr -> Bool
True; HsStmtContext Name
_ -> Bool
False
#endif

isTupleSection :: HsTupArg GhcPs -> Bool
isTupleSection :: HsTupArg GhcPs -> Bool
isTupleSection = \case Missing{} -> Bool
True; HsTupArg GhcPs
_ -> Bool
False

isString :: HsLit GhcPs -> Bool
isString :: HsLit GhcPs -> Bool
isString = \case HsString{} -> Bool
True; HsLit GhcPs
_ -> Bool
False

isPrimLiteral :: HsLit GhcPs -> Bool
isPrimLiteral :: HsLit GhcPs -> Bool
isPrimLiteral = \case
  HsCharPrim{} -> Bool
True
  HsStringPrim{} -> Bool
True
  HsIntPrim{} -> Bool
True
  HsWordPrim{} -> Bool
True
  HsInt64Prim{} -> Bool
True
  HsWord64Prim{} -> Bool
True
  HsFloatPrim{} -> Bool
True
  HsDoublePrim{} -> Bool
True
  HsLit GhcPs
_ -> Bool
False

isSpliceDecl :: HsExpr GhcPs -> Bool
isSpliceDecl :: HsExpr GhcPs -> Bool
isSpliceDecl = \case HsSpliceE{} -> Bool
True; HsExpr GhcPs
_ -> Bool
False

isMultiIf :: HsExpr GhcPs -> Bool
isMultiIf :: HsExpr GhcPs -> Bool
isMultiIf = \case HsMultiIf{} -> Bool
True; HsExpr GhcPs
_ -> Bool
False

isProc :: HsExpr GhcPs -> Bool
isProc :: HsExpr GhcPs -> Bool
isProc = \case HsProc{} -> Bool
True; HsExpr GhcPs
_ -> Bool
False

isTransStmt :: StmtLR GhcPs GhcPs (LHsExpr GhcPs) -> Bool
isTransStmt :: StmtLR GhcPs GhcPs (LHsExpr GhcPs) -> Bool
isTransStmt = \case TransStmt{} -> Bool
True; StmtLR GhcPs GhcPs (LHsExpr GhcPs)
_ -> Bool
False

-- Field has a '_' as in '{foo=_} or is punned e.g. '{foo}'.
isFieldWildcard :: LHsRecField GhcPs (LHsExpr GhcPs) -> Bool
isFieldWildcard :: LHsRecField GhcPs (LHsExpr GhcPs) -> Bool
isFieldWildcard = \case
#if defined (GHCLIB_API_HEAD) || defined (GHCLIB_API_900)
  (L _ HsRecField {hsRecFieldArg=(L _ (HsUnboundVar _ s))}) -> occNameString s == "_"
#elif defined (GHCLIB_API_810)
  (L SrcSpan
_ HsRecField {hsRecFieldArg :: forall id arg. HsRecField' id arg -> arg
hsRecFieldArg=(L SrcSpan
_ (HsUnboundVar XUnboundVar GhcPs
_ UnboundVar
_))}) -> Bool
True
#else
  (L _ HsRecField {hsRecFieldArg=(L _ (EWildPat _))}) -> True
#endif
  (L SrcSpan
_ HsRecField {hsRecPun :: forall id arg. HsRecField' id arg -> Bool
hsRecPun=Bool
True}) -> Bool
True
  (L SrcSpan
_ HsRecField {}) -> Bool
False

isUnboxed :: Boxity -> Bool
isUnboxed :: Boxity -> Bool
isUnboxed = \case Boxity
Unboxed -> Bool
True; Boxity
_ -> Bool
False

isWholeFrac :: HsExpr GhcPs -> Bool
isWholeFrac :: HsExpr GhcPs -> Bool
isWholeFrac (HsLit XLitE GhcPs
_ (HsRat XHsRat GhcPs
_ (FL SourceText
_ Bool
_ Rational
v) Type
_)) = Rational -> Integer
forall a. Ratio a -> a
denominator Rational
v Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
1
isWholeFrac (HsOverLit XOverLitE GhcPs
_ (OverLit XOverLit GhcPs
_ (HsFractional (FL SourceText
_ Bool
_ Rational
v)) HsExpr GhcPs
_)) = Rational -> Integer
forall a. Ratio a -> a
denominator Rational
v Integer -> Integer -> Bool
forall a. Eq a => a -> a -> Bool
== Integer
1
isWholeFrac HsExpr GhcPs
_ = Bool
False

varToStr :: LHsExpr GhcPs -> String
varToStr :: LHsExpr GhcPs -> String
varToStr (L SrcSpan
_ (HsVar XVar GhcPs
_ (L SrcSpan
_ IdP GhcPs
n)))
  | IdP GhcPs
RdrName
n RdrName -> RdrName -> Bool
forall a. Eq a => a -> a -> Bool
== RdrName
consDataCon_RDR = String
":"
  | IdP GhcPs
RdrName
n RdrName -> RdrName -> Bool
forall a. Eq a => a -> a -> Bool
== Name -> RdrName
nameRdrName Name
nilDataConName = String
"[]"
  | IdP GhcPs
RdrName
n RdrName -> RdrName -> Bool
forall a. Eq a => a -> a -> Bool
== Name -> RdrName
nameRdrName (DataCon -> Name
forall a. NamedThing a => a -> Name
getName (Boxity -> Int -> DataCon
tupleDataCon Boxity
Boxed Int
0)) = String
"()"
  | Bool
otherwise = OccName -> String
occNameString (RdrName -> OccName
rdrNameOcc IdP GhcPs
RdrName
n)
varToStr LHsExpr GhcPs
_ = String
""

strToVar :: String -> LHsExpr GhcPs
#if defined (GHCLIB_API_HEAD) || defined (GHCLIB_API_900) || defined (GHCLIB_API_810)
strToVar :: String -> LHsExpr GhcPs
strToVar String
x = SrcSpanLess (LHsExpr GhcPs) -> LHsExpr GhcPs
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (SrcSpanLess (LHsExpr GhcPs) -> LHsExpr GhcPs)
-> SrcSpanLess (LHsExpr GhcPs) -> LHsExpr GhcPs
forall a b. (a -> b) -> a -> b
$ XVar GhcPs -> GenLocated SrcSpan (IdP GhcPs) -> HsExpr GhcPs
forall p. XVar p -> Located (IdP p) -> HsExpr p
HsVar NoExtField
XVar GhcPs
noExtField (SrcSpanLess (Located RdrName) -> Located RdrName
forall a. HasSrcSpan a => SrcSpanLess a -> a
noLoc (SrcSpanLess (Located RdrName) -> Located RdrName)
-> SrcSpanLess (Located RdrName) -> Located RdrName
forall a b. (a -> b) -> a -> b
$ OccName -> RdrName
mkRdrUnqual (String -> OccName
mkVarOcc String
x))
#else
strToVar x = noLoc $ HsVar noExt (noLoc $ mkRdrUnqual (mkVarOcc x))
#endif

fromChar :: LHsExpr GhcPs -> Maybe Char
fromChar :: LHsExpr GhcPs -> Maybe Char
fromChar = \case (L SrcSpan
_ (HsLit XLitE GhcPs
_ (HsChar XHsChar GhcPs
_ Char
x))) -> Char -> Maybe Char
forall a. a -> Maybe a
Just Char
x; LHsExpr GhcPs
_ -> Maybe Char
forall a. Maybe a
Nothing