module Data.GI.GIR.Callable
    ( Callable(..)
    , parseCallable
    ) where

import Data.GI.GIR.Arg (Arg(..), parseArg, parseTransfer)
import Data.GI.GIR.BasicTypes (Transfer(..), Type)
import Data.GI.GIR.Parser
import Data.GI.GIR.Type (parseOptionalType)

data Callable = Callable {
        Callable -> Maybe Type
returnType :: Maybe Type,
        Callable -> Bool
returnMayBeNull :: Bool,
        Callable -> Transfer
returnTransfer :: Transfer,
        Callable -> Documentation
returnDocumentation :: Documentation,
        Callable -> [Arg]
args :: [Arg],
        Callable -> Bool
skipReturn :: Bool,
        Callable -> Bool
callableThrows :: Bool,
        Callable -> Maybe DeprecationInfo
callableDeprecated :: Maybe DeprecationInfo,
        Callable -> Documentation
callableDocumentation :: Documentation,
        -- | Whether the symbol for this callable can be resolved in
        -- the dynamical library associated with the current
        -- introspection data. 'Nothing' means that we have not
        -- checked yet.
        Callable -> Maybe Bool
callableResolvable :: Maybe Bool
    } deriving (Int -> Callable -> ShowS
[Callable] -> ShowS
Callable -> String
(Int -> Callable -> ShowS)
-> (Callable -> String) -> ([Callable] -> ShowS) -> Show Callable
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [Callable] -> ShowS
$cshowList :: [Callable] -> ShowS
show :: Callable -> String
$cshow :: Callable -> String
showsPrec :: Int -> Callable -> ShowS
$cshowsPrec :: Int -> Callable -> ShowS
Show, Callable -> Callable -> Bool
(Callable -> Callable -> Bool)
-> (Callable -> Callable -> Bool) -> Eq Callable
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: Callable -> Callable -> Bool
$c/= :: Callable -> Callable -> Bool
== :: Callable -> Callable -> Bool
$c== :: Callable -> Callable -> Bool
Eq)

parseArgs :: Parser [Arg]
parseArgs :: Parser [Arg]
parseArgs = do
  [[Arg]]
paramSets <- Text -> Parser [Arg] -> Parser [[Arg]]
forall a. Text -> Parser a -> Parser [a]
parseChildrenWithLocalName Text
"parameters" Parser [Arg]
parseArgSet
  case [[Arg]]
paramSets of
    [] -> [Arg] -> Parser [Arg]
forall (m :: * -> *) a. Monad m => a -> m a
return []
    ([Arg]
ps:[]) -> [Arg] -> Parser [Arg]
forall (m :: * -> *) a. Monad m => a -> m a
return [Arg]
ps
    [[Arg]]
_ -> Text -> Parser [Arg]
forall a. Text -> Parser a
parseError (Text -> Parser [Arg]) -> Text -> Parser [Arg]
forall a b. (a -> b) -> a -> b
$ Text
"Unexpected multiple \"parameters\" tag"
  where parseArgSet :: Parser [Arg]
parseArgSet = Text -> Parser Arg -> Parser [Arg]
forall a. Text -> Parser a -> Parser [a]
parseChildrenWithLocalName Text
"parameter" Parser Arg
parseArg

parseOneReturn :: Parser (Maybe Type, Bool, Transfer, Bool, Documentation)
parseOneReturn :: Parser (Maybe Type, Bool, Transfer, Bool, Documentation)
parseOneReturn = do
  Maybe Type
returnType <- Parser (Maybe Type)
parseOptionalType
  Bool
allowNone <- Name -> Bool -> (Text -> Parser Bool) -> Parser Bool
forall a. Name -> a -> (Text -> Parser a) -> Parser a
optionalAttr Name
"allow-none" Bool
False Text -> Parser Bool
parseBool
  Bool
nullable <- Name -> Bool -> (Text -> Parser Bool) -> Parser Bool
forall a. Name -> a -> (Text -> Parser a) -> Parser a
optionalAttr Name
"nullable" Bool
False Text -> Parser Bool
parseBool
  Transfer
transfer <- Parser Transfer
parseTransfer
  Documentation
doc <- Parser Documentation
parseDocumentation
  Bool
skip <- Name -> Bool -> (Text -> Parser Bool) -> Parser Bool
forall a. Name -> a -> (Text -> Parser a) -> Parser a
optionalAttr Name
"skip" Bool
False Text -> Parser Bool
parseBool
  (Maybe Type, Bool, Transfer, Bool, Documentation)
-> Parser (Maybe Type, Bool, Transfer, Bool, Documentation)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe Type
returnType, Bool
allowNone Bool -> Bool -> Bool
|| Bool
nullable, Transfer
transfer, Bool
skip, Documentation
doc)

parseReturn :: Parser (Maybe Type, Bool, Transfer, Bool, Documentation)
parseReturn :: Parser (Maybe Type, Bool, Transfer, Bool, Documentation)
parseReturn = do
  [(Maybe Type, Bool, Transfer, Bool, Documentation)]
returnSets <- Text
-> Parser (Maybe Type, Bool, Transfer, Bool, Documentation)
-> Parser [(Maybe Type, Bool, Transfer, Bool, Documentation)]
forall a. Text -> Parser a -> Parser [a]
parseChildrenWithLocalName Text
"return-value" Parser (Maybe Type, Bool, Transfer, Bool, Documentation)
parseOneReturn
  case [(Maybe Type, Bool, Transfer, Bool, Documentation)]
returnSets of
    ((Maybe Type, Bool, Transfer, Bool, Documentation)
r:[]) -> (Maybe Type, Bool, Transfer, Bool, Documentation)
-> Parser (Maybe Type, Bool, Transfer, Bool, Documentation)
forall (m :: * -> *) a. Monad m => a -> m a
return (Maybe Type, Bool, Transfer, Bool, Documentation)
r
    [] -> Text -> Parser (Maybe Type, Bool, Transfer, Bool, Documentation)
forall a. Text -> Parser a
parseError (Text -> Parser (Maybe Type, Bool, Transfer, Bool, Documentation))
-> Text -> Parser (Maybe Type, Bool, Transfer, Bool, Documentation)
forall a b. (a -> b) -> a -> b
$ Text
"No return information found"
    [(Maybe Type, Bool, Transfer, Bool, Documentation)]
_ -> Text -> Parser (Maybe Type, Bool, Transfer, Bool, Documentation)
forall a. Text -> Parser a
parseError (Text -> Parser (Maybe Type, Bool, Transfer, Bool, Documentation))
-> Text -> Parser (Maybe Type, Bool, Transfer, Bool, Documentation)
forall a b. (a -> b) -> a -> b
$ Text
"Multiple return values found"

parseCallable :: Parser Callable
parseCallable :: Parser Callable
parseCallable = do
  [Arg]
args <- Parser [Arg]
parseArgs
  (Maybe Type
returnType, Bool
mayBeNull, Transfer
transfer, Bool
skip, Documentation
returnDoc) <- Parser (Maybe Type, Bool, Transfer, Bool, Documentation)
parseReturn
  Maybe DeprecationInfo
deprecated <- Parser (Maybe DeprecationInfo)
parseDeprecation
  Documentation
docs <- Parser Documentation
parseDocumentation
  Bool
throws <- Name -> Bool -> (Text -> Parser Bool) -> Parser Bool
forall a. Name -> a -> (Text -> Parser a) -> Parser a
optionalAttr Name
"throws" Bool
False Text -> Parser Bool
parseBool
  Callable -> Parser Callable
forall (m :: * -> *) a. Monad m => a -> m a
return (Callable -> Parser Callable) -> Callable -> Parser Callable
forall a b. (a -> b) -> a -> b
$ Callable :: Maybe Type
-> Bool
-> Transfer
-> Documentation
-> [Arg]
-> Bool
-> Bool
-> Maybe DeprecationInfo
-> Documentation
-> Maybe Bool
-> Callable
Callable {
                  returnType :: Maybe Type
returnType = Maybe Type
returnType
                , returnMayBeNull :: Bool
returnMayBeNull = Bool
mayBeNull
                , returnTransfer :: Transfer
returnTransfer = Transfer
transfer
                , returnDocumentation :: Documentation
returnDocumentation = Documentation
returnDoc
                , args :: [Arg]
args = [Arg]
args
                , skipReturn :: Bool
skipReturn = Bool
skip
                , callableThrows :: Bool
callableThrows = Bool
throws
                , callableDeprecated :: Maybe DeprecationInfo
callableDeprecated = Maybe DeprecationInfo
deprecated
                , callableDocumentation :: Documentation
callableDocumentation = Documentation
docs
                  -- Some symbols are present in the @.gir@ file, but
                  -- they are absent from the library
                  -- itself. Generating bindings for such symbols
                  -- could then lead to linker errors, so later on we
                  -- check whether the callables are actually
                  -- resolvable, and adjust the callable info
                  -- appropriately.
                , callableResolvable :: Maybe Bool
callableResolvable = Maybe Bool
forall a. Maybe a
Nothing
                }