{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}

module Ormolu.Printer.Meat.Declaration.Rule
  ( p_ruleDecls,
  )
where

import Control.Monad (unless)
import GHC.Hs
import GHC.Types.Basic
import GHC.Types.SourceText
import Ormolu.Printer.Combinators
import Ormolu.Printer.Meat.Common
import Ormolu.Printer.Meat.Declaration.Signature
import Ormolu.Printer.Meat.Declaration.Value
import Ormolu.Printer.Meat.Type

p_ruleDecls :: RuleDecls GhcPs -> R ()
p_ruleDecls :: RuleDecls GhcPs -> R ()
p_ruleDecls (HsRules XCRuleDecls GhcPs
_ SourceText
_ [LRuleDecl GhcPs]
xs) =
  Text -> R () -> R ()
pragma Text
"RULES" (R () -> R ()) -> R () -> R ()
forall a b. (a -> b) -> a -> b
$ R ()
-> (GenLocated SrcSpanAnnA (RuleDecl GhcPs) -> R ())
-> [GenLocated SrcSpanAnnA (RuleDecl GhcPs)]
-> R ()
forall a. R () -> (a -> R ()) -> [a] -> R ()
sep R ()
breakpoint (R () -> R ()
sitcc (R () -> R ())
-> (GenLocated SrcSpanAnnA (RuleDecl GhcPs) -> R ())
-> GenLocated SrcSpanAnnA (RuleDecl GhcPs)
-> R ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. (RuleDecl GhcPs -> R ())
-> GenLocated SrcSpanAnnA (RuleDecl GhcPs) -> R ()
forall l a. HasSrcSpan l => (a -> R ()) -> GenLocated l a -> R ()
located' RuleDecl GhcPs -> R ()
p_ruleDecl) [LRuleDecl GhcPs]
[GenLocated SrcSpanAnnA (RuleDecl GhcPs)]
xs

p_ruleDecl :: RuleDecl GhcPs -> R ()
p_ruleDecl :: RuleDecl GhcPs -> R ()
p_ruleDecl (HsRule XHsRule GhcPs
_ XRec GhcPs (SourceText, RuleName)
ruleName Activation
activation Maybe [LHsTyVarBndr () (NoGhcTc GhcPs)]
tyvars [LRuleBndr GhcPs]
ruleBndrs XRec GhcPs (HsExpr GhcPs)
lhs XRec GhcPs (HsExpr GhcPs)
rhs) = do
  GenLocated SrcSpan (SourceText, RuleName)
-> ((SourceText, RuleName) -> R ()) -> R ()
forall l a. HasSrcSpan l => GenLocated l a -> (a -> R ()) -> R ()
located XRec GhcPs (SourceText, RuleName)
GenLocated SrcSpan (SourceText, RuleName)
ruleName (SourceText, RuleName) -> R ()
p_ruleName
  R ()
space
  Activation -> R ()
p_activation Activation
activation
  R ()
space
  case Maybe [LHsTyVarBndr () (NoGhcTc GhcPs)]
tyvars of
    Maybe [LHsTyVarBndr () (NoGhcTc GhcPs)]
Nothing -> () -> R ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()
    Just [LHsTyVarBndr () (NoGhcTc GhcPs)]
xs -> do
      ForAllVisibility
-> (HsTyVarBndr () GhcPs -> R ())
-> [LocatedA (HsTyVarBndr () GhcPs)]
-> R ()
forall a. ForAllVisibility -> (a -> R ()) -> [LocatedA a] -> R ()
p_forallBndrs ForAllVisibility
ForAllInvis HsTyVarBndr () GhcPs -> R ()
forall flag.
IsInferredTyVarBndr flag =>
HsTyVarBndr flag GhcPs -> R ()
p_hsTyVarBndr [LHsTyVarBndr () (NoGhcTc GhcPs)]
[LocatedA (HsTyVarBndr () GhcPs)]
xs
      R ()
space
  -- It appears that there is no way to tell if there was an empty forall
  -- in the input or no forall at all. We do not want to add redundant
  -- foralls, so let's just skip the empty ones.
  Bool -> R () -> R ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless ([GenLocated SrcSpan (RuleBndr GhcPs)] -> Bool
forall (t :: * -> *) a. Foldable t => t a -> Bool
null [LRuleBndr GhcPs]
[GenLocated SrcSpan (RuleBndr GhcPs)]
ruleBndrs) (R () -> R ()) -> R () -> R ()
forall a b. (a -> b) -> a -> b
$
    ForAllVisibility
-> (RuleBndr GhcPs -> R ()) -> [LocatedA (RuleBndr GhcPs)] -> R ()
forall a. ForAllVisibility -> (a -> R ()) -> [LocatedA a] -> R ()
p_forallBndrs ForAllVisibility
ForAllInvis RuleBndr GhcPs -> R ()
p_ruleBndr (GenLocated SrcSpan (RuleBndr GhcPs) -> LocatedA (RuleBndr GhcPs)
forall e ann. Located e -> LocatedAn ann e
reLocA (GenLocated SrcSpan (RuleBndr GhcPs) -> LocatedA (RuleBndr GhcPs))
-> [GenLocated SrcSpan (RuleBndr GhcPs)]
-> [LocatedA (RuleBndr GhcPs)]
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> [LRuleBndr GhcPs]
[GenLocated SrcSpan (RuleBndr GhcPs)]
ruleBndrs)
  R ()
breakpoint
  R () -> R ()
inci (R () -> R ()) -> R () -> R ()
forall a b. (a -> b) -> a -> b
$ do
    GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> (HsExpr GhcPs -> R ()) -> R ()
forall l a. HasSrcSpan l => GenLocated l a -> (a -> R ()) -> R ()
located XRec GhcPs (HsExpr GhcPs)
GenLocated SrcSpanAnnA (HsExpr GhcPs)
lhs HsExpr GhcPs -> R ()
p_hsExpr
    R ()
space
    R ()
equals
    R () -> R ()
inci (R () -> R ()) -> R () -> R ()
forall a b. (a -> b) -> a -> b
$ do
      R ()
breakpoint
      GenLocated SrcSpanAnnA (HsExpr GhcPs)
-> (HsExpr GhcPs -> R ()) -> R ()
forall l a. HasSrcSpan l => GenLocated l a -> (a -> R ()) -> R ()
located XRec GhcPs (HsExpr GhcPs)
GenLocated SrcSpanAnnA (HsExpr GhcPs)
rhs HsExpr GhcPs -> R ()
p_hsExpr

p_ruleName :: (SourceText, RuleName) -> R ()
p_ruleName :: (SourceText, RuleName) -> R ()
p_ruleName (SourceText
_, RuleName
name) = HsLit GhcPs -> R ()
forall a. Outputable a => a -> R ()
atom (HsLit GhcPs -> R ()) -> HsLit GhcPs -> R ()
forall a b. (a -> b) -> a -> b
$ (XHsString GhcPs -> RuleName -> HsLit GhcPs
forall x. XHsString x -> RuleName -> HsLit x
HsString XHsString GhcPs
SourceText
NoSourceText RuleName
name :: HsLit GhcPs)

p_ruleBndr :: RuleBndr GhcPs -> R ()
p_ruleBndr :: RuleBndr GhcPs -> R ()
p_ruleBndr = \case
  RuleBndr XCRuleBndr GhcPs
_ LIdP GhcPs
x -> LocatedN RdrName -> R ()
p_rdrName LIdP GhcPs
LocatedN RdrName
x
  RuleBndrSig XRuleBndrSig GhcPs
_ LIdP GhcPs
x HsPS {LHsType GhcPs
XHsPS GhcPs
hsps_ext :: forall pass. HsPatSigType pass -> XHsPS pass
hsps_body :: forall pass. HsPatSigType pass -> LHsType pass
hsps_body :: LHsType GhcPs
hsps_ext :: XHsPS GhcPs
..} -> BracketStyle -> R () -> R ()
parens BracketStyle
N (R () -> R ()) -> (R () -> R ()) -> R () -> R ()
forall b c a. (b -> c) -> (a -> b) -> a -> c
. R () -> R ()
sitcc (R () -> R ()) -> R () -> R ()
forall a b. (a -> b) -> a -> b
$ do
    LocatedN RdrName -> R ()
p_rdrName LIdP GhcPs
LocatedN RdrName
x
    LHsSigType GhcPs -> R ()
p_typeAscription (LHsType GhcPs -> LHsSigType GhcPs
lhsTypeToSigType LHsType GhcPs
hsps_body)