{-# LANGUAGE OverloadedStrings #-}
module Tokstyle.Cimple.Analysis.GlobalFuncs (analyse) where

import qualified Control.Monad.State.Lazy    as State
import           Data.Text                   (Text)
import           Language.Cimple             (Lexeme (..), Node (..),
                                              Scope (..), lexemeText)
import           Language.Cimple.Diagnostics (warn)
import           System.FilePath             (takeExtension)


analyse :: (FilePath, [Node a (Lexeme Text)]) -> [Text]
analyse :: (FilePath, [Node a (Lexeme Text)]) -> [Text]
analyse (FilePath
file, [Node a (Lexeme Text)]
_) | FilePath -> FilePath
takeExtension FilePath
file FilePath -> FilePath -> Bool
forall a. Eq a => a -> a -> Bool
/= FilePath
".c" = []
analyse (FilePath
file, [Node a (Lexeme Text)]
ast) = [Text] -> [Text]
forall a. [a] -> [a]
reverse ([Text] -> [Text]) -> [Text] -> [Text]
forall a b. (a -> b) -> a -> b
$ State [Text] [()] -> [Text] -> [Text]
forall s a. State s a -> s -> s
State.execState ((Node a (Lexeme Text) -> StateT [Text] Identity ())
-> [Node a (Lexeme Text)] -> State [Text] [()]
forall (t :: * -> *) (m :: * -> *) a b.
(Traversable t, Monad m) =>
(a -> m b) -> t a -> m (t b)
mapM Node a (Lexeme Text) -> StateT [Text] Identity ()
forall diags attr.
HasDiagnostics diags =>
Node attr (Lexeme Text) -> DiagnosticsT diags ()
go [Node a (Lexeme Text)]
ast) []
  where
    go :: Node attr (Lexeme Text) -> DiagnosticsT diags ()
go (FunctionDecl Scope
Global (FunctionPrototype Node attr (Lexeme Text)
_ Lexeme Text
name [Node attr (Lexeme Text)]
_) Maybe (Node attr (Lexeme Text))
_) =
        FilePath -> Lexeme Text -> Text -> DiagnosticsT diags ()
forall diags.
HasDiagnostics diags =>
FilePath -> Lexeme Text -> Text -> DiagnosticsT diags ()
warn FilePath
file Lexeme Text
name (Text -> DiagnosticsT diags ()) -> Text -> DiagnosticsT diags ()
forall a b. (a -> b) -> a -> b
$
            Text
"global function `" Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Lexeme Text -> Text
forall text. Lexeme text -> text
lexemeText Lexeme Text
name Text -> Text -> Text
forall a. Semigroup a => a -> a -> a
<> Text
"' declared in .c file"
    go Node attr (Lexeme Text)
_ = () -> DiagnosticsT diags ()
forall (m :: * -> *) a. Monad m => a -> m a
return ()