{-# 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" forall a b. (a -> b) -> a -> b
$ forall a. R () -> (a -> R ()) -> [a] -> R ()
sep R ()
breakpoint (R () -> R ()
sitcc forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall l a. HasSrcSpan l => (a -> R ()) -> GenLocated l a -> R ()
located' RuleDecl GhcPs -> R ()
p_ruleDecl) [LRuleDecl 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
  forall l a. HasSrcSpan l => GenLocated l a -> (a -> R ()) -> R ()
located XRec GhcPs (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 -> forall (m :: * -> *) a. Monad m => a -> m a
return ()
    Just [LHsTyVarBndr () (NoGhcTc GhcPs)]
xs -> do
      forall l a.
HasSrcSpan l =>
ForAllVisibility -> (a -> R ()) -> [GenLocated l a] -> R ()
p_forallBndrs ForAllVisibility
ForAllInvis forall flag.
IsInferredTyVarBndr flag =>
HsTyVarBndr flag GhcPs -> R ()
p_hsTyVarBndr [LHsTyVarBndr () (NoGhcTc 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.
  forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
unless (forall (t :: * -> *) a. Foldable t => t a -> Bool
null [LRuleBndr GhcPs]
ruleBndrs) forall a b. (a -> b) -> a -> b
$
    forall l a.
HasSrcSpan l =>
ForAllVisibility -> (a -> R ()) -> [GenLocated l a] -> R ()
p_forallBndrs ForAllVisibility
ForAllInvis RuleBndr GhcPs -> R ()
p_ruleBndr [LRuleBndr GhcPs]
ruleBndrs
  R ()
breakpoint
  R () -> R ()
inci forall a b. (a -> b) -> a -> b
$ do
    forall l a. HasSrcSpan l => GenLocated l a -> (a -> R ()) -> R ()
located XRec GhcPs (HsExpr GhcPs)
lhs HsExpr GhcPs -> R ()
p_hsExpr
    R ()
space
    R ()
equals
    R () -> R ()
inci forall a b. (a -> b) -> a -> b
$ do
      R ()
breakpoint
      forall l a. HasSrcSpan l => GenLocated l a -> (a -> R ()) -> R ()
located XRec GhcPs (HsExpr GhcPs)
rhs HsExpr GhcPs -> R ()
p_hsExpr

p_ruleName :: (SourceText, RuleName) -> R ()
p_ruleName :: (SourceText, RuleName) -> R ()
p_ruleName (SourceText
_, RuleName
name) = forall a. Outputable a => a -> R ()
atom forall a b. (a -> b) -> a -> b
$ (forall x. XHsString x -> RuleName -> HsLit x
HsString 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
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 forall a b. (a -> b) -> a -> b
$ do
    LocatedN RdrName -> R ()
p_rdrName LIdP GhcPs
x
    LHsSigType GhcPs -> R ()
p_typeAscription (LHsType GhcPs -> LHsSigType GhcPs
lhsTypeToSigType LHsType GhcPs
hsps_body)