-- SPDX-FileCopyrightText: 2020 Tocqueville Group
--
-- SPDX-License-Identifier: LicenseRef-MIT-TQ

-- | Measuring operation size of typed stuff.
module Morley.Michelson.Typed.OpSize
  ( OpSize (..)
  , U.opSizeHardLimit
  , U.smallTransferOpSize

  , instrOpSize
  , contractOpSize
  , valueOpSize
  ) where

import Morley.Michelson.Typed.Aliases
import Morley.Michelson.Typed.Contract
import Morley.Michelson.Typed.Convert
import Morley.Michelson.Typed.Instr
import Morley.Michelson.Typed.Scope
import Morley.Michelson.Untyped (OpSize)
import qualified Morley.Michelson.Untyped as U

-- | Estimate instruction operation size.
instrOpSize :: Instr inp out -> OpSize
instrOpSize :: Instr inp out -> OpSize
instrOpSize = [ExpandedOp] -> OpSize
U.expandedInstrsOpSize ([ExpandedOp] -> OpSize)
-> (Instr inp out -> [ExpandedOp]) -> Instr inp out -> OpSize
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Instr inp out -> [ExpandedOp]
forall (inp :: [T]) (out :: [T]).
HasCallStack =>
Instr inp out -> [ExpandedOp]
instrToOps

-- | Estimate contract code operation size.
contractOpSize :: Contract cp st -> OpSize
contractOpSize :: Contract cp st -> OpSize
contractOpSize = Instr (ContractInp cp st) (ContractOut st) -> OpSize
forall (inp :: [T]) (out :: [T]). Instr inp out -> OpSize
instrOpSize (Instr (ContractInp cp st) (ContractOut st) -> OpSize)
-> (Contract cp st -> Instr (ContractInp cp st) (ContractOut st))
-> Contract cp st
-> OpSize
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Contract cp st -> Instr (ContractInp cp st) (ContractOut st)
forall (instr :: [T] -> [T] -> *) (cp :: T) (st :: T).
Contract' instr cp st -> ContractCode' instr cp st
cCode

-- | Estimate value operation size.
valueOpSize :: (UntypedValScope t) => Value t -> OpSize
valueOpSize :: Value t -> OpSize
valueOpSize = Value -> OpSize
U.valueOpSize (Value -> OpSize) -> (Value t -> Value) -> Value t -> OpSize
forall b c a. (b -> c) -> (a -> b) -> a -> c
. Value t -> Value
forall (t :: T). HasNoOp t => Value' Instr t -> Value
untypeValue