{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}

module Ormolu.Printer.Meat.Declaration.Foreign
  ( p_foreignDecl,
  )
where

import Control.Monad
import GHC.Hs
import GHC.Types.ForeignCall
import GHC.Types.SrcLoc
import Ormolu.Printer.Combinators
import Ormolu.Printer.Meat.Common
import Ormolu.Printer.Meat.Declaration.Signature

p_foreignDecl :: ForeignDecl GhcPs -> R ()
p_foreignDecl :: ForeignDecl GhcPs -> R ()
p_foreignDecl = \case
  fd :: ForeignDecl GhcPs
fd@ForeignImport {ForeignImport
fd_fi :: forall pass. ForeignDecl pass -> ForeignImport
fd_fi :: ForeignImport
fd_fi} -> do
    ForeignImport -> R ()
p_foreignImport ForeignImport
fd_fi
    ForeignDecl GhcPs -> R ()
p_foreignTypeSig ForeignDecl GhcPs
fd
  fd :: ForeignDecl GhcPs
fd@ForeignExport {ForeignExport
fd_fe :: forall pass. ForeignDecl pass -> ForeignExport
fd_fe :: ForeignExport
fd_fe} -> do
    ForeignExport -> R ()
p_foreignExport ForeignExport
fd_fe
    ForeignDecl GhcPs -> R ()
p_foreignTypeSig ForeignDecl GhcPs
fd

-- | Printer for the last part of an import\/export, which is function name
-- and type signature.
p_foreignTypeSig :: ForeignDecl GhcPs -> R ()
p_foreignTypeSig :: ForeignDecl GhcPs -> R ()
p_foreignTypeSig ForeignDecl GhcPs
fd = do
  R ()
breakpoint
  R () -> R ()
inci
    forall b c a. (b -> c) -> (a -> b) -> a -> c
. [SrcSpan] -> R () -> R ()
switchLayout
      [ forall a e. GenLocated (SrcSpanAnn' a) e -> SrcSpan
getLocA (forall pass. ForeignDecl pass -> LIdP pass
fd_name ForeignDecl GhcPs
fd),
        (forall a e. GenLocated (SrcSpanAnn' a) e -> SrcSpan
getLocA forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall pass. ForeignDecl pass -> LHsSigType pass
fd_sig_ty) ForeignDecl GhcPs
fd
      ]
    forall a b. (a -> b) -> a -> b
$ do
      LocatedN RdrName -> R ()
p_rdrName (forall pass. ForeignDecl pass -> LIdP pass
fd_name ForeignDecl GhcPs
fd)
      LHsSigType GhcPs -> R ()
p_typeAscription (forall pass. ForeignDecl pass -> LHsSigType pass
fd_sig_ty ForeignDecl GhcPs
fd)

-- | Printer for 'ForeignImport'.
--
-- These have the form:
--
-- > foreign import callingConvention [safety] [identifier]
--
-- We need to check whether the safety has a good source, span, as it
-- defaults to 'PlaySafe' if you don't have anything in the source.
--
-- We also layout the identifier using the 'SourceText', because printing
-- with the other two fields of 'CImport' is very complicated. See the
-- 'Outputable' instance of 'ForeignImport' for details.
p_foreignImport :: ForeignImport -> R ()
p_foreignImport :: ForeignImport -> R ()
p_foreignImport (CImport Located CCallConv
cCallConv Located Safety
safety Maybe Header
_ CImportSpec
_ Located SourceText
sourceText) = do
  Text -> R ()
txt Text
"foreign import"
  R ()
space
  forall l a. HasSrcSpan l => GenLocated l a -> (a -> R ()) -> R ()
located Located CCallConv
cCallConv forall a. Outputable a => a -> R ()
atom
  -- Need to check for 'noLoc' for the 'safe' annotation
  forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (SrcSpan -> Bool
isGoodSrcSpan forall a b. (a -> b) -> a -> b
$ forall l e. GenLocated l e -> l
getLoc Located Safety
safety) (R ()
space forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> forall a. Outputable a => a -> R ()
atom Located Safety
safety)
  forall l a. HasSrcSpan l => GenLocated l a -> (a -> R ()) -> R ()
located Located SourceText
sourceText SourceText -> R ()
p_sourceText

p_foreignExport :: ForeignExport -> R ()
p_foreignExport :: ForeignExport -> R ()
p_foreignExport (CExport (L SrcSpan
loc (CExportStatic SourceText
_ CLabelString
_ CCallConv
cCallConv)) Located SourceText
sourceText) = do
  Text -> R ()
txt Text
"foreign export"
  R ()
space
  forall l a. HasSrcSpan l => GenLocated l a -> (a -> R ()) -> R ()
located (forall l e. l -> e -> GenLocated l e
L SrcSpan
loc CCallConv
cCallConv) forall a. Outputable a => a -> R ()
atom
  forall l a. HasSrcSpan l => GenLocated l a -> (a -> R ()) -> R ()
located Located SourceText
sourceText SourceText -> R ()
p_sourceText