{-# LANGUAGE DeriveDataTypeable    #-}
{-# LANGUAGE DeriveFunctor         #-}
{-# LANGUAGE DeriveGeneric         #-}
{-# LANGUAGE FlexibleInstances     #-}
{-# LANGUAGE MultiParamTypeClasses #-}

module Language.Fortran.LValue where

import           Prelude                               hiding (exp)
import           Data.Data
import           GHC.Generics                          (Generic)

import           Language.Fortran.AST
import           Language.Fortran.Util.FirstParameter
import           Language.Fortran.Util.Position
import           Language.Fortran.Util.SecondParameter

-- | A subset of 'Expression' which can only contain values that can be assigned
-- to.
data LValue a
  = LvSimpleVar a SrcSpan Name
  | LvSubscript a SrcSpan (LValue a) (AList Index a)
  | LvDataRef a SrcSpan (LValue a) (LValue a)
  deriving (LValue a -> LValue a -> Bool
(LValue a -> LValue a -> Bool)
-> (LValue a -> LValue a -> Bool) -> Eq (LValue a)
forall a. Eq a => LValue a -> LValue a -> Bool
forall a. (a -> a -> Bool) -> (a -> a -> Bool) -> Eq a
/= :: LValue a -> LValue a -> Bool
$c/= :: forall a. Eq a => LValue a -> LValue a -> Bool
== :: LValue a -> LValue a -> Bool
$c== :: forall a. Eq a => LValue a -> LValue a -> Bool
Eq, Int -> LValue a -> ShowS
[LValue a] -> ShowS
LValue a -> String
(Int -> LValue a -> ShowS)
-> (LValue a -> String) -> ([LValue a] -> ShowS) -> Show (LValue a)
forall a. Show a => Int -> LValue a -> ShowS
forall a. Show a => [LValue a] -> ShowS
forall a. Show a => LValue a -> String
forall a.
(Int -> a -> ShowS) -> (a -> String) -> ([a] -> ShowS) -> Show a
showList :: [LValue a] -> ShowS
$cshowList :: forall a. Show a => [LValue a] -> ShowS
show :: LValue a -> String
$cshow :: forall a. Show a => LValue a -> String
showsPrec :: Int -> LValue a -> ShowS
$cshowsPrec :: forall a. Show a => Int -> LValue a -> ShowS
Show, Typeable (LValue a)
DataType
Constr
Typeable (LValue a)
-> (forall (c :: * -> *).
    (forall d b. Data d => c (d -> b) -> d -> c b)
    -> (forall g. g -> c g) -> LValue a -> c (LValue a))
-> (forall (c :: * -> *).
    (forall b r. Data b => c (b -> r) -> c r)
    -> (forall r. r -> c r) -> Constr -> c (LValue a))
-> (LValue a -> Constr)
-> (LValue a -> DataType)
-> (forall (t :: * -> *) (c :: * -> *).
    Typeable t =>
    (forall d. Data d => c (t d)) -> Maybe (c (LValue a)))
-> (forall (t :: * -> * -> *) (c :: * -> *).
    Typeable t =>
    (forall d e. (Data d, Data e) => c (t d e))
    -> Maybe (c (LValue a)))
-> ((forall b. Data b => b -> b) -> LValue a -> LValue a)
-> (forall r r'.
    (r -> r' -> r)
    -> r -> (forall d. Data d => d -> r') -> LValue a -> r)
-> (forall r r'.
    (r' -> r -> r)
    -> r -> (forall d. Data d => d -> r') -> LValue a -> r)
-> (forall u. (forall d. Data d => d -> u) -> LValue a -> [u])
-> (forall u. Int -> (forall d. Data d => d -> u) -> LValue a -> u)
-> (forall (m :: * -> *).
    Monad m =>
    (forall d. Data d => d -> m d) -> LValue a -> m (LValue a))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> LValue a -> m (LValue a))
-> (forall (m :: * -> *).
    MonadPlus m =>
    (forall d. Data d => d -> m d) -> LValue a -> m (LValue a))
-> Data (LValue a)
LValue a -> DataType
LValue a -> Constr
(forall d. Data d => c (t d)) -> Maybe (c (LValue a))
(forall b. Data b => b -> b) -> LValue a -> LValue a
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> LValue a -> c (LValue a)
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (LValue a)
forall a. Data a => Typeable (LValue a)
forall a. Data a => LValue a -> DataType
forall a. Data a => LValue a -> Constr
forall a.
Data a =>
(forall b. Data b => b -> b) -> LValue a -> LValue a
forall a u.
Data a =>
Int -> (forall d. Data d => d -> u) -> LValue a -> u
forall a u.
Data a =>
(forall d. Data d => d -> u) -> LValue a -> [u]
forall a r r'.
Data a =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> LValue a -> r
forall a r r'.
Data a =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> LValue a -> r
forall a (m :: * -> *).
(Data a, Monad m) =>
(forall d. Data d => d -> m d) -> LValue a -> m (LValue a)
forall a (m :: * -> *).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> LValue a -> m (LValue a)
forall a (c :: * -> *).
Data a =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (LValue a)
forall a (c :: * -> *).
Data a =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> LValue a -> c (LValue a)
forall a (t :: * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (LValue a))
forall a (t :: * -> * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (LValue 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) -> LValue a -> u
forall u. (forall d. Data d => d -> u) -> LValue a -> [u]
forall r r'.
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> LValue a -> r
forall r r'.
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> LValue a -> r
forall (m :: * -> *).
Monad m =>
(forall d. Data d => d -> m d) -> LValue a -> m (LValue a)
forall (m :: * -> *).
MonadPlus m =>
(forall d. Data d => d -> m d) -> LValue a -> m (LValue a)
forall (c :: * -> *).
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (LValue a)
forall (c :: * -> *).
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> LValue a -> c (LValue a)
forall (t :: * -> *) (c :: * -> *).
Typeable t =>
(forall d. Data d => c (t d)) -> Maybe (c (LValue a))
forall (t :: * -> * -> *) (c :: * -> *).
Typeable t =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (LValue a))
$cLvDataRef :: Constr
$cLvSubscript :: Constr
$cLvSimpleVar :: Constr
$tLValue :: DataType
gmapMo :: (forall d. Data d => d -> m d) -> LValue a -> m (LValue a)
$cgmapMo :: forall a (m :: * -> *).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> LValue a -> m (LValue a)
gmapMp :: (forall d. Data d => d -> m d) -> LValue a -> m (LValue a)
$cgmapMp :: forall a (m :: * -> *).
(Data a, MonadPlus m) =>
(forall d. Data d => d -> m d) -> LValue a -> m (LValue a)
gmapM :: (forall d. Data d => d -> m d) -> LValue a -> m (LValue a)
$cgmapM :: forall a (m :: * -> *).
(Data a, Monad m) =>
(forall d. Data d => d -> m d) -> LValue a -> m (LValue a)
gmapQi :: Int -> (forall d. Data d => d -> u) -> LValue a -> u
$cgmapQi :: forall a u.
Data a =>
Int -> (forall d. Data d => d -> u) -> LValue a -> u
gmapQ :: (forall d. Data d => d -> u) -> LValue a -> [u]
$cgmapQ :: forall a u.
Data a =>
(forall d. Data d => d -> u) -> LValue a -> [u]
gmapQr :: (r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> LValue a -> r
$cgmapQr :: forall a r r'.
Data a =>
(r' -> r -> r)
-> r -> (forall d. Data d => d -> r') -> LValue a -> r
gmapQl :: (r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> LValue a -> r
$cgmapQl :: forall a r r'.
Data a =>
(r -> r' -> r)
-> r -> (forall d. Data d => d -> r') -> LValue a -> r
gmapT :: (forall b. Data b => b -> b) -> LValue a -> LValue a
$cgmapT :: forall a.
Data a =>
(forall b. Data b => b -> b) -> LValue a -> LValue a
dataCast2 :: (forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (LValue a))
$cdataCast2 :: forall a (t :: * -> * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d e. (Data d, Data e) => c (t d e)) -> Maybe (c (LValue a))
dataCast1 :: (forall d. Data d => c (t d)) -> Maybe (c (LValue a))
$cdataCast1 :: forall a (t :: * -> *) (c :: * -> *).
(Data a, Typeable t) =>
(forall d. Data d => c (t d)) -> Maybe (c (LValue a))
dataTypeOf :: LValue a -> DataType
$cdataTypeOf :: forall a. Data a => LValue a -> DataType
toConstr :: LValue a -> Constr
$ctoConstr :: forall a. Data a => LValue a -> Constr
gunfold :: (forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (LValue a)
$cgunfold :: forall a (c :: * -> *).
Data a =>
(forall b r. Data b => c (b -> r) -> c r)
-> (forall r. r -> c r) -> Constr -> c (LValue a)
gfoldl :: (forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> LValue a -> c (LValue a)
$cgfoldl :: forall a (c :: * -> *).
Data a =>
(forall d b. Data d => c (d -> b) -> d -> c b)
-> (forall g. g -> c g) -> LValue a -> c (LValue a)
$cp1Data :: forall a. Data a => Typeable (LValue a)
Data, Typeable, (forall x. LValue a -> Rep (LValue a) x)
-> (forall x. Rep (LValue a) x -> LValue a) -> Generic (LValue a)
forall x. Rep (LValue a) x -> LValue a
forall x. LValue a -> Rep (LValue a) x
forall a.
(forall x. a -> Rep a x) -> (forall x. Rep a x -> a) -> Generic a
forall a x. Rep (LValue a) x -> LValue a
forall a x. LValue a -> Rep (LValue a) x
$cto :: forall a x. Rep (LValue a) x -> LValue a
$cfrom :: forall a x. LValue a -> Rep (LValue a) x
Generic, a -> LValue b -> LValue a
(a -> b) -> LValue a -> LValue b
(forall a b. (a -> b) -> LValue a -> LValue b)
-> (forall a b. a -> LValue b -> LValue a) -> Functor LValue
forall a b. a -> LValue b -> LValue a
forall a b. (a -> b) -> LValue a -> LValue b
forall (f :: * -> *).
(forall a b. (a -> b) -> f a -> f b)
-> (forall a b. a -> f b -> f a) -> Functor f
<$ :: a -> LValue b -> LValue a
$c<$ :: forall a b. a -> LValue b -> LValue a
fmap :: (a -> b) -> LValue a -> LValue b
$cfmap :: forall a b. (a -> b) -> LValue a -> LValue b
Functor)


-- | If the expression can be seen as an lvalue, convert it to an 'LValue'.
toLValue :: Expression a -> Maybe (LValue a)
toLValue :: Expression a -> Maybe (LValue a)
toLValue (ExpValue a
ann SrcSpan
sp (ValVariable String
nm)) = LValue a -> Maybe (LValue a)
forall a. a -> Maybe a
Just (a -> SrcSpan -> String -> LValue a
forall a. a -> SrcSpan -> String -> LValue a
LvSimpleVar a
ann SrcSpan
sp String
nm)
toLValue (ExpSubscript a
ann SrcSpan
sp Expression a
exp AList Index a
ixs) = a -> SrcSpan -> LValue a -> AList Index a -> LValue a
forall a. a -> SrcSpan -> LValue a -> AList Index a -> LValue a
LvSubscript a
ann SrcSpan
sp (LValue a -> AList Index a -> LValue a)
-> Maybe (LValue a) -> Maybe (AList Index a -> LValue a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Expression a -> Maybe (LValue a)
forall a. Expression a -> Maybe (LValue a)
toLValue Expression a
exp Maybe (AList Index a -> LValue a)
-> Maybe (AList Index a) -> Maybe (LValue a)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> AList Index a -> Maybe (AList Index a)
forall (f :: * -> *) a. Applicative f => a -> f a
pure AList Index a
ixs
toLValue (ExpDataRef a
ann SrcSpan
sp Expression a
lhs Expression a
rhs) = a -> SrcSpan -> LValue a -> LValue a -> LValue a
forall a. a -> SrcSpan -> LValue a -> LValue a -> LValue a
LvDataRef a
ann SrcSpan
sp (LValue a -> LValue a -> LValue a)
-> Maybe (LValue a) -> Maybe (LValue a -> LValue a)
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Expression a -> Maybe (LValue a)
forall a. Expression a -> Maybe (LValue a)
toLValue Expression a
lhs Maybe (LValue a -> LValue a)
-> Maybe (LValue a) -> Maybe (LValue a)
forall (f :: * -> *) a b. Applicative f => f (a -> b) -> f a -> f b
<*> Expression a -> Maybe (LValue a)
forall a. Expression a -> Maybe (LValue a)
toLValue Expression a
rhs
toLValue Expression a
_ = Maybe (LValue a)
forall a. Maybe a
Nothing

instance FirstParameter (LValue a) a
instance SecondParameter (LValue a) SrcSpan

instance Annotated LValue
instance Spanned (LValue a)