module Data.GI.CodeGen.OverloadedMethods
    ( genMethodList
    , genMethodInfo
    , genUnsupportedMethodInfo
    ) where

import Control.Monad (forM, forM_, when)
#if !MIN_VERSION_base(4,11,0)
import Data.Monoid ((<>))
#endif
import Data.Text (Text)
import qualified Data.Text as T

import Data.GI.CodeGen.API
import Data.GI.CodeGen.Conversions (ExposeClosures(..))
import Data.GI.CodeGen.Callable (callableSignature, Signature(..),
                                 ForeignSymbol(..), fixupCallerAllocates)
import Data.GI.CodeGen.Code
import Data.GI.CodeGen.ModulePath (dotModulePath)
import Data.GI.CodeGen.SymbolNaming (lowerName, upperName, qualifiedSymbol,
                                     moduleLocation, hackageModuleLink)
import Data.GI.CodeGen.Util (ucFirst)

-- | Qualified name for the info for a given method.
methodInfoName :: Name -> Method -> CodeGen e Text
methodInfoName :: forall e. Name -> Method -> CodeGen e Text
methodInfoName Name
n Method
method =
    let infoName :: Text
infoName = Name -> Text
upperName Name
n Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> (Text -> Text
ucFirst (Text -> Text) -> (Method -> Text) -> Method -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Name -> Text
lowerName (Name -> Text) -> (Method -> Name) -> Method -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Method -> Name
methodName) Method
method
                   Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"MethodInfo"
    in Text -> Name -> CodeGen e Text
forall e. Text -> Name -> CodeGen e Text
qualifiedSymbol Text
infoName Name
n

-- | Appropriate instances so overloaded labels are properly resolved.
genMethodResolver :: Text -> CodeGen e ()
genMethodResolver :: forall e. Text -> CodeGen e ()
genMethodResolver Text
n = do
  Text -> CodeGen e ()
forall e. Text -> CodeGen e ()
addLanguagePragma Text
"TypeApplications"
  CodeGen e () -> CodeGen e ()
forall e a. CodeGen e a -> CodeGen e a
group (CodeGen e () -> CodeGen e ()) -> CodeGen e () -> CodeGen e ()
forall a b. (a -> b) -> a -> b
$ do
    Text -> CodeGen e ()
forall e. Text -> CodeGen e ()
line (Text -> CodeGen e ()) -> Text -> CodeGen e ()
forall a b. (a -> b) -> a -> b
$ Text
"instance (info ~ Resolve" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
n Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"Method t " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
n Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
", "
          Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"O.OverloadedMethod info " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
n Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" p) => OL.IsLabel t ("
          Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
n Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" -> p) where"
    Text -> CodeGen e ()
forall e. Text -> CodeGen e ()
line (Text -> CodeGen e ()) -> Text -> CodeGen e ()
forall a b. (a -> b) -> a -> b
$ Text
"#if MIN_VERSION_base(4,10,0)"
    CodeGen e () -> CodeGen e ()
forall e a. CodeGen e a -> CodeGen e a
indent (CodeGen e () -> CodeGen e ()) -> CodeGen e () -> CodeGen e ()
forall a b. (a -> b) -> a -> b
$ Text -> CodeGen e ()
forall e. Text -> CodeGen e ()
line (Text -> CodeGen e ()) -> Text -> CodeGen e ()
forall a b. (a -> b) -> a -> b
$ Text
"fromLabel = O.overloadedMethod @info"
    Text -> CodeGen e ()
forall e. Text -> CodeGen e ()
line (Text -> CodeGen e ()) -> Text -> CodeGen e ()
forall a b. (a -> b) -> a -> b
$ Text
"#else"
    CodeGen e () -> CodeGen e ()
forall e a. CodeGen e a -> CodeGen e a
indent (CodeGen e () -> CodeGen e ()) -> CodeGen e () -> CodeGen e ()
forall a b. (a -> b) -> a -> b
$ Text -> CodeGen e ()
forall e. Text -> CodeGen e ()
line (Text -> CodeGen e ()) -> Text -> CodeGen e ()
forall a b. (a -> b) -> a -> b
$ Text
"fromLabel _ = O.overloadedMethod @info"
    Text -> CodeGen e ()
forall e. Text -> CodeGen e ()
line (Text -> CodeGen e ()) -> Text -> CodeGen e ()
forall a b. (a -> b) -> a -> b
$ Text
"#endif"

  -- The circular instance trick is to avoid the liberal coverage
  -- condition. We should be using DYSFUNCTIONAL pragmas instead, once
  -- those are implemented:
  -- https://github.com/ghc-proposals/ghc-proposals/pull/374
  CPPGuard -> CodeGen e () -> CodeGen e ()
forall e a. CPPGuard -> CodeGen e a -> CodeGen e a
cppIf (Text -> (Integer, Integer, Integer) -> CPPGuard
CPPMinVersion Text
"base" (Integer
4,Integer
13,Integer
0)) (CodeGen e () -> CodeGen e ()) -> CodeGen e () -> CodeGen e ()
forall a b. (a -> b) -> a -> b
$ CodeGen e () -> CodeGen e ()
forall e a. CodeGen e a -> CodeGen e a
group (CodeGen e () -> CodeGen e ()) -> CodeGen e () -> CodeGen e ()
forall a b. (a -> b) -> a -> b
$ do
    Text -> CodeGen e ()
forall e. Text -> CodeGen e ()
line (Text -> CodeGen e ()) -> Text -> CodeGen e ()
forall a b. (a -> b) -> a -> b
$ Text
"instance (info ~ Resolve" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
n Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"Method t " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
n Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
", "
          Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"O.OverloadedMethod info " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
n Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" p, "
          Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"R.HasField t " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
n Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" p) => "
          Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"R.HasField t " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
n Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" p where"
    CodeGen e () -> CodeGen e ()
forall e a. CodeGen e a -> CodeGen e a
indent (CodeGen e () -> CodeGen e ()) -> CodeGen e () -> CodeGen e ()
forall a b. (a -> b) -> a -> b
$ Text -> CodeGen e ()
forall e. Text -> CodeGen e ()
line (Text -> CodeGen e ()) -> Text -> CodeGen e ()
forall a b. (a -> b) -> a -> b
$ Text
"getField = O.overloadedMethod @info"

  CodeGen e () -> CodeGen e ()
forall e a. CodeGen e a -> CodeGen e a
group (CodeGen e () -> CodeGen e ()) -> CodeGen e () -> CodeGen e ()
forall a b. (a -> b) -> a -> b
$ do
    Text -> CodeGen e ()
forall e. Text -> CodeGen e ()
line (Text -> CodeGen e ()) -> Text -> CodeGen e ()
forall a b. (a -> b) -> a -> b
$ Text
"instance (info ~ Resolve" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
n Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"Method t " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
n Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
", "
          Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"O.OverloadedMethodInfo info " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
n Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
") => "
          Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"OL.IsLabel t (O.MethodProxy info "
          Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
n Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
") where"
    Text -> CodeGen e ()
forall e. Text -> CodeGen e ()
line (Text -> CodeGen e ()) -> Text -> CodeGen e ()
forall a b. (a -> b) -> a -> b
$ Text
"#if MIN_VERSION_base(4,10,0)"
    CodeGen e () -> CodeGen e ()
forall e a. CodeGen e a -> CodeGen e a
indent (CodeGen e () -> CodeGen e ()) -> CodeGen e () -> CodeGen e ()
forall a b. (a -> b) -> a -> b
$ Text -> CodeGen e ()
forall e. Text -> CodeGen e ()
line (Text -> CodeGen e ()) -> Text -> CodeGen e ()
forall a b. (a -> b) -> a -> b
$ Text
"fromLabel = O.MethodProxy"
    Text -> CodeGen e ()
forall e. Text -> CodeGen e ()
line (Text -> CodeGen e ()) -> Text -> CodeGen e ()
forall a b. (a -> b) -> a -> b
$ Text
"#else"
    CodeGen e () -> CodeGen e ()
forall e a. CodeGen e a -> CodeGen e a
indent (CodeGen e () -> CodeGen e ()) -> CodeGen e () -> CodeGen e ()
forall a b. (a -> b) -> a -> b
$ Text -> CodeGen e ()
forall e. Text -> CodeGen e ()
line (Text -> CodeGen e ()) -> Text -> CodeGen e ()
forall a b. (a -> b) -> a -> b
$ Text
"fromLabel _ = O.MethodProxy"
    Text -> CodeGen e ()
forall e. Text -> CodeGen e ()
line (Text -> CodeGen e ()) -> Text -> CodeGen e ()
forall a b. (a -> b) -> a -> b
$ Text
"#endif"

-- | Generate the `MethodList` instance given the list of methods for
-- the given named type. Returns a Haddock comment summarizing the
-- list of methods available.
genMethodList :: Name -> [(Name, Method)] -> CodeGen e ()
genMethodList :: forall e. Name -> [(Name, Method)] -> CodeGen e ()
genMethodList Name
n [(Name, Method)]
methods = do
  let name :: Text
name = Name -> Text
upperName Name
n
  let filteredMethods :: [(Name, Method)]
filteredMethods = ((Name, Method) -> Bool) -> [(Name, Method)] -> [(Name, Method)]
forall a. (a -> Bool) -> [a] -> [a]
filter (Name, Method) -> Bool
isOrdinaryMethod [(Name, Method)]
methods
      gets :: [(Name, Method)]
gets = ((Name, Method) -> Bool) -> [(Name, Method)] -> [(Name, Method)]
forall a. (a -> Bool) -> [a] -> [a]
filter (Name, Method) -> Bool
isGet [(Name, Method)]
filteredMethods
      sets :: [(Name, Method)]
sets = ((Name, Method) -> Bool) -> [(Name, Method)] -> [(Name, Method)]
forall a. (a -> Bool) -> [a] -> [a]
filter (Name, Method) -> Bool
isSet [(Name, Method)]
filteredMethods
      others :: [(Name, Method)]
others = ((Name, Method) -> Bool) -> [(Name, Method)] -> [(Name, Method)]
forall a. (a -> Bool) -> [a] -> [a]
filter (\(Name, Method)
m -> Bool -> Bool
not ((Name, Method) -> Bool
isSet (Name, Method)
m Bool -> Bool -> Bool
|| (Name, Method) -> Bool
isGet (Name, Method)
m)) [(Name, Method)]
filteredMethods
      orderedMethods :: [(Name, Method)]
orderedMethods = [(Name, Method)]
others [(Name, Method)] -> [(Name, Method)] -> [(Name, Method)]
forall a. [a] -> [a] -> [a]
++ [(Name, Method)]
gets [(Name, Method)] -> [(Name, Method)] -> [(Name, Method)]
forall a. [a] -> [a] -> [a]
++ [(Name, Method)]
sets
  [(Text, Text)]
infos <- [(Name, Method)]
-> ((Name, Method)
    -> ReaderT
         CodeGenConfig
         (StateT (CGState, ModuleInfo) (Except e))
         (Text, Text))
-> ReaderT
     CodeGenConfig
     (StateT (CGState, ModuleInfo) (Except e))
     [(Text, Text)]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [(Name, Method)]
orderedMethods (((Name, Method)
  -> ReaderT
       CodeGenConfig
       (StateT (CGState, ModuleInfo) (Except e))
       (Text, Text))
 -> ReaderT
      CodeGenConfig
      (StateT (CGState, ModuleInfo) (Except e))
      [(Text, Text)])
-> ((Name, Method)
    -> ReaderT
         CodeGenConfig
         (StateT (CGState, ModuleInfo) (Except e))
         (Text, Text))
-> ReaderT
     CodeGenConfig
     (StateT (CGState, ModuleInfo) (Except e))
     [(Text, Text)]
forall a b. (a -> b) -> a -> b
$ \(Name
owner, Method
method) ->
           do Text
mi <- Name -> Method -> CodeGen e Text
forall e. Name -> Method -> CodeGen e Text
methodInfoName Name
owner Method
method
              (Text, Text)
-> ReaderT
     CodeGenConfig
     (StateT (CGState, ModuleInfo) (Except e))
     (Text, Text)
forall a.
a
-> ReaderT
     CodeGenConfig (StateT (CGState, ModuleInfo) (Except e)) a
forall (m :: * -> *) a. Monad m => a -> m a
return ((Name -> Text
lowerName (Name -> Text) -> (Method -> Name) -> Method -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Method -> Name
methodName) Method
method, Text
mi)
  CodeGen e () -> CodeGen e ()
forall e a. CodeGen e a -> CodeGen e a
group (CodeGen e () -> CodeGen e ()) -> CodeGen e () -> CodeGen e ()
forall a b. (a -> b) -> a -> b
$ do
    let resolver :: Text
resolver = Text
"Resolve" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
name Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"Method"
    HaddockSection -> Text -> CodeGen e ()
forall e. HaddockSection -> Text -> CodeGen e ()
export (NamedSection -> HaddockSection
Section NamedSection
MethodSection) Text
resolver
    Text -> CodeGen e ()
forall e. Text -> CodeGen e ()
line (Text -> CodeGen e ()) -> Text -> CodeGen e ()
forall a b. (a -> b) -> a -> b
$ Text
"type family " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
resolver Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" (t :: Symbol) (o :: *) :: * where"
    CodeGen e () -> CodeGen e ()
forall e a. CodeGen e a -> CodeGen e a
indent (CodeGen e () -> CodeGen e ()) -> CodeGen e () -> CodeGen e ()
forall a b. (a -> b) -> a -> b
$ [(Text, Text)] -> ((Text, Text) -> CodeGen e ()) -> CodeGen e ()
forall (t :: * -> *) (m :: * -> *) a b.
(Foldable t, Monad m) =>
t a -> (a -> m b) -> m ()
forM_ [(Text, Text)]
infos (((Text, Text) -> CodeGen e ()) -> CodeGen e ())
-> ((Text, Text) -> CodeGen e ()) -> CodeGen e ()
forall a b. (a -> b) -> a -> b
$ \(Text
label, Text
info) -> do
        Text -> CodeGen e ()
forall e. Text -> CodeGen e ()
line (Text -> CodeGen e ()) -> Text -> CodeGen e ()
forall a b. (a -> b) -> a -> b
$ Text
resolver Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" \"" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
label Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"\" o = " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
info
    CodeGen e () -> CodeGen e ()
forall e a. CodeGen e a -> CodeGen e a
indent (CodeGen e () -> CodeGen e ()) -> CodeGen e () -> CodeGen e ()
forall a b. (a -> b) -> a -> b
$ Text -> CodeGen e ()
forall e. Text -> CodeGen e ()
line (Text -> CodeGen e ()) -> Text -> CodeGen e ()
forall a b. (a -> b) -> a -> b
$ Text
resolver Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" l o = O.MethodResolutionFailed l o"

  Text -> CodeGen e ()
forall e. Text -> CodeGen e ()
genMethodResolver Text
name

  Text
docs <- [(Name, Method)]
-> [(Name, Method)] -> [(Name, Method)] -> CodeGen e Text
forall e.
[(Name, Method)]
-> [(Name, Method)] -> [(Name, Method)] -> CodeGen e Text
methodListDocumentation [(Name, Method)]
others [(Name, Method)]
gets [(Name, Method)]
sets
  HaddockSection -> Text -> CodeGen e ()
forall e. HaddockSection -> Text -> CodeGen e ()
prependSectionFormattedDocs (NamedSection -> HaddockSection
Section NamedSection
MethodSection) Text
docs

  where isOrdinaryMethod :: (Name, Method) -> Bool
        isOrdinaryMethod :: (Name, Method) -> Bool
isOrdinaryMethod (Name
_, Method
m) = Method -> MethodType
methodType Method
m MethodType -> MethodType -> Bool
forall a. Eq a => a -> a -> Bool
== MethodType
OrdinaryMethod

        isGet :: (Name, Method) -> Bool
        isGet :: (Name, Method) -> Bool
isGet (Name
_, Method
m) = Text
"get_" Text -> Text -> Bool
`T.isPrefixOf` (Name -> Text
name (Name -> Text) -> (Method -> Name) -> Method -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Method -> Name
methodName) Method
m

        isSet :: (Name, Method) -> Bool
        isSet :: (Name, Method) -> Bool
isSet (Name
_, Method
m) = Text
"set_" Text -> Text -> Bool
`T.isPrefixOf` (Name -> Text
name (Name -> Text) -> (Method -> Name) -> Method -> Text
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Method -> Name
methodName) Method
m

-- | Format a haddock comment with the information about available
-- methods.
methodListDocumentation :: [(Name, Method)] -> [(Name, Method)]
                           -> [(Name, Method)] -> CodeGen e Text
methodListDocumentation :: forall e.
[(Name, Method)]
-> [(Name, Method)] -> [(Name, Method)] -> CodeGen e Text
methodListDocumentation [] [] [] = Text
-> ReaderT
     CodeGenConfig (StateT (CGState, ModuleInfo) (Except e)) Text
forall a.
a
-> ReaderT
     CodeGenConfig (StateT (CGState, ModuleInfo) (Except e)) a
forall (m :: * -> *) a. Monad m => a -> m a
return Text
""
methodListDocumentation [(Name, Method)]
ordinary [(Name, Method)]
getters [(Name, Method)]
setters = do
  Text
ordinaryFormatted <- [(Name, Method)]
-> ReaderT
     CodeGenConfig (StateT (CGState, ModuleInfo) (Except e)) Text
forall e. [(Name, Method)] -> CodeGen e Text
formatMethods [(Name, Method)]
ordinary
  Text
gettersFormatted <- [(Name, Method)]
-> ReaderT
     CodeGenConfig (StateT (CGState, ModuleInfo) (Except e)) Text
forall e. [(Name, Method)] -> CodeGen e Text
formatMethods [(Name, Method)]
getters
  Text
settersFormatted <- [(Name, Method)]
-> ReaderT
     CodeGenConfig (StateT (CGState, ModuleInfo) (Except e)) Text
forall e. [(Name, Method)] -> CodeGen e Text
formatMethods [(Name, Method)]
setters

  Text
-> ReaderT
     CodeGenConfig (StateT (CGState, ModuleInfo) (Except e)) Text
forall a.
a
-> ReaderT
     CodeGenConfig (StateT (CGState, ModuleInfo) (Except e)) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Text
 -> ReaderT
      CodeGenConfig (StateT (CGState, ModuleInfo) (Except e)) Text)
-> Text
-> ReaderT
     CodeGenConfig (StateT (CGState, ModuleInfo) (Except e)) Text
forall a b. (a -> b) -> a -> b
$ Text
"\n\n === __Click to display all available methods, including inherited ones__\n"
    Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"==== Methods\n" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
ordinaryFormatted
    Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"\n==== Getters\n" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
gettersFormatted
    Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"\n==== Setters\n" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
settersFormatted

  where formatMethods :: [(Name, Method)] -> CodeGen e Text
        formatMethods :: forall e. [(Name, Method)] -> CodeGen e Text
formatMethods [] = Text
-> ReaderT
     CodeGenConfig (StateT (CGState, ModuleInfo) (Except e)) Text
forall a.
a
-> ReaderT
     CodeGenConfig (StateT (CGState, ModuleInfo) (Except e)) a
forall (m :: * -> *) a. Monad m => a -> m a
return Text
"/None/.\n"
        formatMethods [(Name, Method)]
methods = do
          [Text]
qualifiedMethods <- [(Name, Method)]
-> ((Name, Method)
    -> ReaderT
         CodeGenConfig (StateT (CGState, ModuleInfo) (Except e)) Text)
-> ReaderT
     CodeGenConfig (StateT (CGState, ModuleInfo) (Except e)) [Text]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
t a -> (a -> m b) -> m (t b)
forM [(Name, Method)]
methods (((Name, Method)
  -> ReaderT
       CodeGenConfig (StateT (CGState, ModuleInfo) (Except e)) Text)
 -> ReaderT
      CodeGenConfig (StateT (CGState, ModuleInfo) (Except e)) [Text])
-> ((Name, Method)
    -> ReaderT
         CodeGenConfig (StateT (CGState, ModuleInfo) (Except e)) Text)
-> ReaderT
     CodeGenConfig (StateT (CGState, ModuleInfo) (Except e)) [Text]
forall a b. (a -> b) -> a -> b
$ \(Name
owner, Method
m) -> do
            API
api <- Name -> CodeGen e API
forall e. HasCallStack => Name -> CodeGen e API
findAPIByName Name
owner
            let mn :: Text
mn = Name -> Text
lowerName (Method -> Name
methodName Method
m)
            Text
-> ReaderT
     CodeGenConfig (StateT (CGState, ModuleInfo) (Except e)) Text
forall a.
a
-> ReaderT
     CodeGenConfig (StateT (CGState, ModuleInfo) (Except e)) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Text
 -> ReaderT
      CodeGenConfig (StateT (CGState, ModuleInfo) (Except e)) Text)
-> Text
-> ReaderT
     CodeGenConfig (StateT (CGState, ModuleInfo) (Except e)) Text
forall a b. (a -> b) -> a -> b
$ Text
"[" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
mn Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>
              Text
"](\"" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> ModulePath -> Text
dotModulePath (Name -> API -> ModulePath
moduleLocation Name
owner API
api)
              Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"#g:method:" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
mn Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"\")"
          Text
-> ReaderT
     CodeGenConfig (StateT (CGState, ModuleInfo) (Except e)) Text
forall a.
a
-> ReaderT
     CodeGenConfig (StateT (CGState, ModuleInfo) (Except e)) a
forall (m :: * -> *) a. Monad m => a -> m a
return (Text
 -> ReaderT
      CodeGenConfig (StateT (CGState, ModuleInfo) (Except e)) Text)
-> Text
-> ReaderT
     CodeGenConfig (StateT (CGState, ModuleInfo) (Except e)) Text
forall a b. (a -> b) -> a -> b
$ Text -> [Text] -> Text
T.intercalate Text
", " [Text]
qualifiedMethods Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
".\n"

-- | Generate the `MethodInfo` type and instance for the given method.
genMethodInfo :: Name -> Method -> ExcCodeGen ()
genMethodInfo :: Name -> Method -> ExcCodeGen ()
genMethodInfo Name
n Method
m =
    Bool -> ExcCodeGen () -> ExcCodeGen ()
forall (f :: * -> *). Applicative f => Bool -> f () -> f ()
when (Method -> MethodType
methodType Method
m MethodType -> MethodType -> Bool
forall a. Eq a => a -> a -> Bool
== MethodType
OrdinaryMethod) (ExcCodeGen () -> ExcCodeGen ()) -> ExcCodeGen () -> ExcCodeGen ()
forall a b. (a -> b) -> a -> b
$
      ExcCodeGen () -> ExcCodeGen ()
forall e a. CodeGen e a -> CodeGen e a
group (ExcCodeGen () -> ExcCodeGen ()) -> ExcCodeGen () -> ExcCodeGen ()
forall a b. (a -> b) -> a -> b
$ do
        API
api <- Name -> CodeGen CGError API
forall e. HasCallStack => Name -> CodeGen e API
findAPIByName Name
n
        Text
infoName <- Name -> Method -> CodeGen CGError Text
forall e. Name -> Method -> CodeGen e Text
methodInfoName Name
n Method
m
        let callable :: Callable
callable = Callable -> Callable
fixupCallerAllocates (Method -> Callable
methodCallable Method
m)
        Signature
sig <- Callable -> ForeignSymbol -> ExposeClosures -> ExcCodeGen Signature
callableSignature Callable
callable (Text -> ForeignSymbol
KnownForeignSymbol Text
forall a. HasCallStack => a
undefined) ExposeClosures
WithoutClosures
        Text -> ExcCodeGen ()
forall e. Text -> CodeGen e ()
bline (Text -> ExcCodeGen ()) -> Text -> ExcCodeGen ()
forall a b. (a -> b) -> a -> b
$ Text
"data " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
infoName
        let (Text
obj, [Text]
otherTypes) = case ((Maybe Arg, Text) -> Text) -> [(Maybe Arg, Text)] -> [Text]
forall a b. (a -> b) -> [a] -> [b]
map (Maybe Arg, Text) -> Text
forall a b. (a, b) -> b
snd (Signature -> [(Maybe Arg, Text)]
signatureArgTypes Signature
sig) of
              -- This should not happen, since ordinary methods always
              -- have the instance as first argument.
              [] -> [Char] -> (Text, [Text])
forall a. HasCallStack => [Char] -> a
error ([Char] -> (Text, [Text])) -> [Char] -> (Text, [Text])
forall a b. (a -> b) -> a -> b
$ [Char]
"Internal error: too few parameters! " [Char] -> [Char] -> [Char]
forall a. [a] -> [a] -> [a]
++ Method -> [Char]
forall a. Show a => a -> [Char]
show Method
m
              (Text
obj':[Text]
otherTypes') -> (Text
obj', [Text]
otherTypes')
            sigConstraint :: Text
sigConstraint = Text
"signature ~ (" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> [Text] -> Text
T.intercalate Text
" -> "
              ([Text]
otherTypes [Text] -> [Text] -> [Text]
forall a. [a] -> [a] -> [a]
++ [Signature -> Text
signatureReturnType Signature
sig]) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
")"

        Text
hackageLink <- Name -> CodeGen CGError Text
forall e. Name -> CodeGen e Text
hackageModuleLink Name
n
        let mn :: Name
mn = Method -> Name
methodName Method
m
            mangled :: Text
mangled = Name -> Text
lowerName (Name
mn {name :: Text
name = Name -> Text
name Name
n Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"_" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Name -> Text
name Name
mn})
            dbgInfo :: Text
dbgInfo = ModulePath -> Text
dotModulePath (Name -> API -> ModulePath
moduleLocation Name
n API
api) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"." Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
mangled

        ExcCodeGen () -> ExcCodeGen ()
forall e a. CodeGen e a -> CodeGen e a
group (ExcCodeGen () -> ExcCodeGen ()) -> ExcCodeGen () -> ExcCodeGen ()
forall a b. (a -> b) -> a -> b
$ do
          Text -> ExcCodeGen ()
forall e. Text -> CodeGen e ()
line (Text -> ExcCodeGen ()) -> Text -> ExcCodeGen ()
forall a b. (a -> b) -> a -> b
$ Text
"instance ("
            Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text -> [Text] -> Text
T.intercalate Text
", " (Text
sigConstraint Text -> [Text] -> [Text]
forall a. a -> [a] -> [a]
: Signature -> [Text]
signatureConstraints Signature
sig)
            Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
") => O.OverloadedMethod " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
infoName Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
obj
            Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" signature where"
          ExcCodeGen () -> ExcCodeGen ()
forall e a. CodeGen e a -> CodeGen e a
indent (ExcCodeGen () -> ExcCodeGen ()) -> ExcCodeGen () -> ExcCodeGen ()
forall a b. (a -> b) -> a -> b
$ Text -> ExcCodeGen ()
forall e. Text -> CodeGen e ()
line (Text -> ExcCodeGen ()) -> Text -> ExcCodeGen ()
forall a b. (a -> b) -> a -> b
$ Text
"overloadedMethod = " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
mangled

        ExcCodeGen () -> ExcCodeGen ()
forall e a. CodeGen e a -> CodeGen e a
group (ExcCodeGen () -> ExcCodeGen ()) -> ExcCodeGen () -> ExcCodeGen ()
forall a b. (a -> b) -> a -> b
$ do
          Text -> ExcCodeGen ()
forall e. Text -> CodeGen e ()
line (Text -> ExcCodeGen ()) -> Text -> ExcCodeGen ()
forall a b. (a -> b) -> a -> b
$ Text
"instance O.OverloadedMethodInfo " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
infoName Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
obj
            Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" where"
          ExcCodeGen () -> ExcCodeGen ()
forall e a. CodeGen e a -> CodeGen e a
indent (ExcCodeGen () -> ExcCodeGen ()) -> ExcCodeGen () -> ExcCodeGen ()
forall a b. (a -> b) -> a -> b
$ do
            Text -> ExcCodeGen ()
forall e. Text -> CodeGen e ()
line (Text -> ExcCodeGen ()) -> Text -> ExcCodeGen ()
forall a b. (a -> b) -> a -> b
$ Text
"overloadedMethodInfo = P.Just (O.ResolvedSymbolInfo {"
            ExcCodeGen () -> ExcCodeGen ()
forall e a. CodeGen e a -> CodeGen e a
indent (ExcCodeGen () -> ExcCodeGen ()) -> ExcCodeGen () -> ExcCodeGen ()
forall a b. (a -> b) -> a -> b
$ do
              Text -> ExcCodeGen ()
forall e. Text -> CodeGen e ()
line (Text -> ExcCodeGen ()) -> Text -> ExcCodeGen ()
forall a b. (a -> b) -> a -> b
$ Text
"O.resolvedSymbolName = \"" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
dbgInfo Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"\","
              Text -> ExcCodeGen ()
forall e. Text -> CodeGen e ()
line (Text -> ExcCodeGen ()) -> Text -> ExcCodeGen ()
forall a b. (a -> b) -> a -> b
$ Text
"O.resolvedSymbolURL = \"" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<>
                Text
hackageLink Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"#v:" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
mangled Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"\""
              Text -> ExcCodeGen ()
forall e. Text -> CodeGen e ()
line (Text -> ExcCodeGen ()) -> Text -> ExcCodeGen ()
forall a b. (a -> b) -> a -> b
$ Text
"})"

        HaddockSection -> Text -> ExcCodeGen ()
forall e. HaddockSection -> Text -> CodeGen e ()
export (NamedSection -> Text -> HaddockSection
NamedSubsection NamedSection
MethodSection (Text -> HaddockSection) -> Text -> HaddockSection
forall a b. (a -> b) -> a -> b
$ Name -> Text
lowerName Name
mn) Text
infoName

-- | Generate a method info that is not actually callable, but rather
-- gives a type error when trying to use it.
genUnsupportedMethodInfo :: Name -> Method -> CodeGen e ()
genUnsupportedMethodInfo :: forall e. Name -> Method -> CodeGen e ()
genUnsupportedMethodInfo Name
n Method
m = do
  Text
infoName <- Name -> Method -> CodeGen e Text
forall e. Name -> Method -> CodeGen e Text
methodInfoName Name
n Method
m
  Text -> CodeGen e ()
forall e. Text -> CodeGen e ()
line (Text -> CodeGen e ()) -> Text -> CodeGen e ()
forall a b. (a -> b) -> a -> b
$ Text
"-- XXX: Dummy instance, since code generation failed.\n"
           Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"-- Please file a bug at http://github.com/haskell-gi/haskell-gi."
  Text -> CodeGen e ()
forall e. Text -> CodeGen e ()
bline (Text -> CodeGen e ()) -> Text -> CodeGen e ()
forall a b. (a -> b) -> a -> b
$ Text
"data " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
infoName
  CodeGen e () -> CodeGen e ()
forall e a. CodeGen e a -> CodeGen e a
group (CodeGen e () -> CodeGen e ()) -> CodeGen e () -> CodeGen e ()
forall a b. (a -> b) -> a -> b
$ do
    Text -> CodeGen e ()
forall e. Text -> CodeGen e ()
line (Text -> CodeGen e ()) -> Text -> CodeGen e ()
forall a b. (a -> b) -> a -> b
$ Text
"instance (p ~ (), o ~ O.UnsupportedMethodError \""
      Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Name -> Text
lowerName (Method -> Name
methodName Method
m) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"\" " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Name -> Text
name Name
n
      Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
") => O.OverloadedMethod " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
infoName Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" o p where"
    CodeGen e () -> CodeGen e ()
forall e a. CodeGen e a -> CodeGen e a
indent (CodeGen e () -> CodeGen e ()) -> CodeGen e () -> CodeGen e ()
forall a b. (a -> b) -> a -> b
$ Text -> CodeGen e ()
forall e. Text -> CodeGen e ()
line (Text -> CodeGen e ()) -> Text -> CodeGen e ()
forall a b. (a -> b) -> a -> b
$ Text
"overloadedMethod = undefined"

  CodeGen e () -> CodeGen e ()
forall e a. CodeGen e a -> CodeGen e a
group (CodeGen e () -> CodeGen e ()) -> CodeGen e () -> CodeGen e ()
forall a b. (a -> b) -> a -> b
$ do
    Text -> CodeGen e ()
forall e. Text -> CodeGen e ()
line (Text -> CodeGen e ()) -> Text -> CodeGen e ()
forall a b. (a -> b) -> a -> b
$ Text
"instance (o ~ O.UnsupportedMethodError \""
      Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Name -> Text
lowerName (Method -> Name
methodName Method
m) Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"\" " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Name -> Text
name Name
n
      Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
") => O.OverloadedMethodInfo " Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
infoName Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
" o where"
    CodeGen e () -> CodeGen e ()
forall e a. CodeGen e a -> CodeGen e a
indent (CodeGen e () -> CodeGen e ()) -> CodeGen e () -> CodeGen e ()
forall a b. (a -> b) -> a -> b
$ Text -> CodeGen e ()
forall e. Text -> CodeGen e ()
line (Text -> CodeGen e ()) -> Text -> CodeGen e ()
forall a b. (a -> b) -> a -> b
$ Text
"overloadedMethodInfo = undefined"

  HaddockSection -> Text -> CodeGen e ()
forall e. HaddockSection -> Text -> CodeGen e ()
export HaddockSection
ToplevelSection Text
infoName