{-# LANGUAGE DeriveDataTypeable    #-}
{-# LANGUAGE DeriveFunctor         #-}
{-# LANGUAGE FlexibleInstances     #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE TemplateHaskell       #-}
{-# LANGUAGE TypeSynonymInstances  #-}
{-# OPTIONS_GHC -Wall #-}

{-|

Fortran AST annotations used for Hoare logic checking.

-}
module Camfort.Specification.Hoare.Annotation where

import           Data.Data

import           Control.Lens

import qualified Language.Fortran.Analysis          as F
import qualified Language.Fortran.AST               as F

import qualified Camfort.Analysis.Annotations       as Ann
import           Camfort.Analysis.CommentAnnotator

import           Camfort.Specification.Hoare.Syntax


-- | Annotations meant to appear on the main annotated program's AST.
type HA = F.Analysis (HoareAnnotation Ann.A)


-- | Annotations meant to appear on the AST inside those Fortran expressions
-- that have been parsed from inside logical expression annotations.
type InnerHA = F.Analysis Ann.A

data HoareAnnotation a =
  HoareAnnotation
  { HoareAnnotation a -> a
_hoarePrevAnnotation  :: a
  , HoareAnnotation a -> Maybe (SpecOrDecl InnerHA)
_hoareSod :: Maybe (SpecOrDecl InnerHA)
  -- ^ A @static_assert@ specification or @decl_aux@ declaration.
  , HoareAnnotation a -> Maybe ProgramUnitName
_hoarePUName :: Maybe F.ProgramUnitName
  -- ^ The name of the program unit that the spec or decl is attached to.
  }
  deriving (Int -> HoareAnnotation a -> ShowS
[HoareAnnotation a] -> ShowS
HoareAnnotation a -> String
(Int -> HoareAnnotation a -> ShowS)
-> (HoareAnnotation a -> String)
-> ([HoareAnnotation a] -> ShowS)
-> Show (HoareAnnotation a)
forall a. Show a => Int -> HoareAnnotation a -> ShowS
forall a. Show a => [HoareAnnotation a] -> ShowS
forall a. Show a => HoareAnnotation a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [HoareAnnotation a] -> ShowS
$cshowList :: forall a. Show a => [HoareAnnotation a] -> ShowS
show :: HoareAnnotation a -> String
$cshow :: forall a. Show a => HoareAnnotation a -> String
showsPrec :: Int -> HoareAnnotation a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> HoareAnnotation a -> ShowS
Show, HoareAnnotation a -> HoareAnnotation a -> Bool
(HoareAnnotation a -> HoareAnnotation a -> Bool)
-> (HoareAnnotation a -> HoareAnnotation a -> Bool)
-> Eq (HoareAnnotation a)
forall a. Eq a => HoareAnnotation a -> HoareAnnotation a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: HoareAnnotation a -> HoareAnnotation a -> Bool
$c/= :: forall a. Eq a => HoareAnnotation a -> HoareAnnotation a -> Bool
== :: HoareAnnotation a -> HoareAnnotation a -> Bool
$c== :: forall a. Eq a => HoareAnnotation a -> HoareAnnotation a -> Bool
Eq, Typeable, Typeable (HoareAnnotation a)
DataType
Constr
Typeable (HoareAnnotation a)
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g)
    -> HoareAnnotation a
    -> c (HoareAnnotation a))
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c (HoareAnnotation a))
-> (HoareAnnotation a -> Constr)
-> (HoareAnnotation a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c (HoareAnnotation a)))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c (HoareAnnotation a)))
-> ((forall b. Data b => b -> b)
    -> HoareAnnotation a -> HoareAnnotation a)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> HoareAnnotation a -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> HoareAnnotation a -> r)
-> (forall u.
    (forall d. Data d => d -> u) -> HoareAnnotation a -> [u])
-> (forall u.
    Int -> (forall d. Data d => d -> u) -> HoareAnnotation a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d)
    -> HoareAnnotation a -> m (HoareAnnotation a))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> HoareAnnotation a -> m (HoareAnnotation a))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d)
    -> HoareAnnotation a -> m (HoareAnnotation a))
-> Data (HoareAnnotation a)
HoareAnnotation a -> DataType
HoareAnnotation a -> Constr
(forall d. Data d => c (t d)) -> Maybe (c (HoareAnnotation a))
(forall b. Data b => b -> b)
-> HoareAnnotation a -> HoareAnnotation a
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> HoareAnnotation a
-> c (HoareAnnotation a)
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (HoareAnnotation a)
forall a. Data a => Typeable (HoareAnnotation a)
forall a. Data a => HoareAnnotation a -> DataType
forall a. Data a => HoareAnnotation a -> Constr
forall a.
Data a =>
(forall b. Data b => b -> b)
-> HoareAnnotation a -> HoareAnnotation a
forall a u.
Data a =>
Int -> (forall d. Data d => d -> u) -> HoareAnnotation a -> u
forall a u.
Data a =>
(forall d. Data d => d -> u) -> HoareAnnotation a -> [u]
forall a r r'.
Data a =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> HoareAnnotation a -> r
forall a r r'.
Data a =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> HoareAnnotation a -> r
forall a (m :: * -> *).
(Data a, Monad m) =>
(forall d. Data d => d -> m d)
-> HoareAnnotation a -> m (HoareAnnotation a)
forall a (m :: * -> *).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d)
-> HoareAnnotation a -> m (HoareAnnotation a)
forall a (c :: * -> *).
Data a =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (HoareAnnotation a)
forall a (c :: * -> *).
Data a =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> HoareAnnotation a
-> c (HoareAnnotation a)
forall a (t :: * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (HoareAnnotation a))
forall a (t :: * -> * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (HoareAnnotation a))
forall a.
Typeable a
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> a -> c a)
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c a)
-> (a -> Constr)
-> (a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c a))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c a))
-> ((forall b. Data b => b -> b) -> a -> a)
-> (forall r r'.
    (r -> r' -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall r r'.
    (r' -> r -> r) -> r -> (forall d. Data d => d -> r') -> a -> r)
-> (forall u. (forall d. Data d => d -> u) -> a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> a -> m a)
-> Data a
forall u.
Int -> (forall d. Data d => d -> u) -> HoareAnnotation a -> u
forall u. (forall d. Data d => d -> u) -> HoareAnnotation a -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> HoareAnnotation a -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> HoareAnnotation a -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d)
-> HoareAnnotation a -> m (HoareAnnotation a)
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d)
-> HoareAnnotation a -> m (HoareAnnotation a)
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (HoareAnnotation a)
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> HoareAnnotation a
-> c (HoareAnnotation a)
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (HoareAnnotation a))
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (HoareAnnotation a))
$cHoareAnnotation :: Constr
$tHoareAnnotation :: DataType
gmapMo :: (forall d. Data d => d -> m d)
-> HoareAnnotation a -> m (HoareAnnotation a)
$cgmapMo :: forall a (m :: * -> *).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d)
-> HoareAnnotation a -> m (HoareAnnotation a)
gmapMp :: (forall d. Data d => d -> m d)
-> HoareAnnotation a -> m (HoareAnnotation a)
$cgmapMp :: forall a (m :: * -> *).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d)
-> HoareAnnotation a -> m (HoareAnnotation a)
gmapM :: (forall d. Data d => d -> m d)
-> HoareAnnotation a -> m (HoareAnnotation a)
$cgmapM :: forall a (m :: * -> *).
(Data a, Monad m) =>
(forall d. Data d => d -> m d)
-> HoareAnnotation a -> m (HoareAnnotation a)
gmapQi :: Int -> (forall d. Data d => d -> u) -> HoareAnnotation a -> u
$cgmapQi :: forall a u.
Data a =>
Int -> (forall d. Data d => d -> u) -> HoareAnnotation a -> u
gmapQ :: (forall d. Data d => d -> u) -> HoareAnnotation a -> [u]
$cgmapQ :: forall a u.
Data a =>
(forall d. Data d => d -> u) -> HoareAnnotation a -> [u]
gmapQr :: (r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> HoareAnnotation a -> r
$cgmapQr :: forall a r r'.
Data a =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> HoareAnnotation a -> r
gmapQl :: (r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> HoareAnnotation a -> r
$cgmapQl :: forall a r r'.
Data a =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> HoareAnnotation a -> r
gmapT :: (forall b. Data b => b -> b)
-> HoareAnnotation a -> HoareAnnotation a
$cgmapT :: forall a.
Data a =>
(forall b. Data b => b -> b)
-> HoareAnnotation a -> HoareAnnotation a
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (HoareAnnotation a))
$cdataCast2 :: forall a (t :: * -> * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e))
-> Maybe (c (HoareAnnotation a))
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c (HoareAnnotation a))
$cdataCast1 :: forall a (t :: * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (HoareAnnotation a))
dataTypeOf :: HoareAnnotation a -> DataType
$cdataTypeOf :: forall a. Data a => HoareAnnotation a -> DataType
toConstr :: HoareAnnotation a -> Constr
$ctoConstr :: forall a. Data a => HoareAnnotation a -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (HoareAnnotation a)
$cgunfold :: forall a (c :: * -> *).
Data a =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (HoareAnnotation a)
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> HoareAnnotation a
-> c (HoareAnnotation a)
$cgfoldl :: forall a (c :: * -> *).
Data a =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g)
-> HoareAnnotation a
-> c (HoareAnnotation a)
$cp1Data :: forall a. Data a => Typeable (HoareAnnotation a)
Data)

makeLenses ''HoareAnnotation

instance Linkable HA where
  link :: HA -> Block HA -> HA
link HA
ann Block HA
_ = HA
ann

  linkPU :: HA -> ProgramUnit HA -> HA
linkPU HA
ann ProgramUnit HA
pu = (HoareAnnotation A -> HoareAnnotation A) -> HA -> HA
forall a. (a -> a) -> Analysis a -> Analysis a
Ann.onPrev ((Maybe ProgramUnitName -> Identity (Maybe ProgramUnitName))
-> HoareAnnotation A -> Identity (HoareAnnotation A)
forall a. Lens' (HoareAnnotation a) (Maybe ProgramUnitName)
hoarePUName ((Maybe ProgramUnitName -> Identity (Maybe ProgramUnitName))
 -> HoareAnnotation A -> Identity (HoareAnnotation A))
-> Maybe ProgramUnitName -> HoareAnnotation A -> HoareAnnotation A
forall s t a b. ASetter s t a b -> b -> s -> t
.~ ProgramUnitName -> Maybe ProgramUnitName
forall a. a -> Maybe a
Just (ProgramUnit HA -> ProgramUnitName
forall a. ProgramUnit (Analysis a) -> ProgramUnitName
F.puName ProgramUnit HA
pu)) HA
ann

instance ASTEmbeddable HA (SpecOrDecl InnerHA) where
  annotateWithAST :: HA -> SpecOrDecl InnerHA -> HA
annotateWithAST HA
ann SpecOrDecl InnerHA
ast =
    (HoareAnnotation A -> HoareAnnotation A) -> HA -> HA
forall a. (a -> a) -> Analysis a -> Analysis a
Ann.onPrev ((Maybe (SpecOrDecl InnerHA)
 -> Identity (Maybe (SpecOrDecl InnerHA)))
-> HoareAnnotation A -> Identity (HoareAnnotation A)
forall a. Lens' (HoareAnnotation a) (Maybe (SpecOrDecl InnerHA))
hoareSod ((Maybe (SpecOrDecl InnerHA)
  -> Identity (Maybe (SpecOrDecl InnerHA)))
 -> HoareAnnotation A -> Identity (HoareAnnotation A))
-> Maybe (SpecOrDecl InnerHA)
-> HoareAnnotation A
-> HoareAnnotation A
forall s t a b. ASetter s t a b -> b -> s -> t
.~ SpecOrDecl InnerHA -> Maybe (SpecOrDecl InnerHA)
forall a. a -> Maybe a
Just SpecOrDecl InnerHA
ast) HA
ann


hoareAnn0 :: a -> HoareAnnotation a
hoareAnn0 :: a -> HoareAnnotation a
hoareAnn0 a
x = HoareAnnotation :: forall a.
a
-> Maybe (SpecOrDecl InnerHA)
-> Maybe ProgramUnitName
-> HoareAnnotation a
HoareAnnotation { _hoarePrevAnnotation :: a
_hoarePrevAnnotation = a
x, _hoareSod :: Maybe (SpecOrDecl InnerHA)
_hoareSod = Maybe (SpecOrDecl InnerHA)
forall a. Maybe a
Nothing, _hoarePUName :: Maybe ProgramUnitName
_hoarePUName = Maybe ProgramUnitName
forall a. Maybe a
Nothing }