<commit_before>{-# LANGUAGE RelaxedPolyRec #-}
module Language.C.Analysis.ConstEval where

import Control.Monad
import Data.Bits
import Data.Maybe
import Language.C.Syntax.AST
import Language.C.Syntax.Constants
import {-# SOURCE #-} Language.C.Analysis.AstAnalysis (tExpr, ExprSide(..))
import Language.C.Analysis.Debug
import Language.C.Analysis.DeclAnalysis
import Language.C.Analysis.DefTable
import Language.C.Data
import Language.C.Pretty
import Language.C.Analysis.SemRep
import Language.C.Analysis.TravMonad
import Text.PrettyPrint.HughesPJ

data MachineDesc =
  MachineDesc
  { boolSize       :: Integer
  , charSize       :: Integer
  , shortSize      :: Integer
  , intSize        :: Integer
  , longSize       :: Integer
  , longLongSize   :: Integer
  , floatSize      :: Integer
  , doubleSize     :: Integer
  , longDoubleSize :: Integer
  , ptrSize        :: Integer
  }

iSize :: MachineDesc -> IntType -> Integer
iSize md TyBool = boolSize md
iSize md TyChar = charSize md
iSize md TySChar = charSize md
iSize md TyUChar = charSize md
iSize md TyShort = shortSize md
iSize md TyUShort = shortSize md
iSize md TyInt = intSize md
iSize md TyUInt = intSize md
iSize md TyLong = longSize md
iSize md TyULong = longSize md
iSize md TyLLong = longLongSize md
iSize md TyULLong = longLongSize md

fSize :: MachineDesc -> FloatType -> Integer
fSize md TyFloat = floatSize md
fSize md TyDouble = doubleSize md
fSize md TyLDouble = longDoubleSize md

builtinSize :: MachineDesc -> BuiltinType -> Integer
builtinSize md TyVaList = ptrSize md
builtinSize md TyAny    = ptrSize md

intExpr :: (Pos n, MonadName m) => n -> Integer -> m CExpr
intExpr n i =
  genName >>= \name ->
    return $ CConst $ CIntConst (cInteger i) (mkNodeInfo (posOf n) name)

fieldOffset :: MonadTrav m => MachineDesc -> Type -> Ident -> m Integer
fieldOffset md (DirectType (TyComp ctr) _) m =
  do dt <- getDefTable
     case lookupTag (sueRef ctr) dt of
       Just (Left _)   -> astError (nodeInfo ctr)
                          "composite declared but not defined"
       Just (Right (CompDef (CompType _ UnionTag _ _ _))) -> return 0
       Just (Right (CompDef (CompType _ StructTag ms _ ni))) ->
         do let before = takeWhile ((== m) . declIdent) ms
            -- XXX: handle padding
            sizes <- mapM (sizeofType md ni) (map declType before)
            return $ sum sizes
       Just (Right (EnumDef _)) -> astError (nodeInfo ctr) "field of enum?"
       Nothing         -> astError (nodeInfo ctr) "unknown composite"
fieldOffset _ t i = astError (nodeInfo i) $
                    "field of non-composite: " ++ render (pretty t)

sizeofType :: (MonadTrav m, CNode n) => MachineDesc -> n -> Type -> m Integer<commit_msg>Remove unused, buggy fieldOffset function.<commit_after>{-# LANGUAGE RelaxedPolyRec #-}
module Language.C.Analysis.ConstEval where

import Control.Monad
import Data.Bits
import Data.Maybe
import Language.C.Syntax.AST
import Language.C.Syntax.Constants
import {-# SOURCE #-} Language.C.Analysis.AstAnalysis (tExpr, ExprSide(..))
import Language.C.Analysis.Debug
import Language.C.Analysis.DeclAnalysis
import Language.C.Analysis.DefTable
import Language.C.Data
import Language.C.Pretty
import Language.C.Analysis.SemRep
import Language.C.Analysis.TravMonad
import Text.PrettyPrint.HughesPJ

data MachineDesc =
  MachineDesc
  { boolSize       :: Integer
  , charSize       :: Integer
  , shortSize      :: Integer
  , intSize        :: Integer
  , longSize       :: Integer
  , longLongSize   :: Integer
  , floatSize      :: Integer
  , doubleSize     :: Integer
  , longDoubleSize :: Integer
  , ptrSize        :: Integer
  }

iSize :: MachineDesc -> IntType -> Integer
iSize md TyBool = boolSize md
iSize md TyChar = charSize md
iSize md TySChar = charSize md
iSize md TyUChar = charSize md
iSize md TyShort = shortSize md
iSize md TyUShort = shortSize md
iSize md TyInt = intSize md
iSize md TyUInt = intSize md
iSize md TyLong = longSize md
iSize md TyULong = longSize md
iSize md TyLLong = longLongSize md
iSize md TyULLong = longLongSize md

fSize :: MachineDesc -> FloatType -> Integer
fSize md TyFloat = floatSize md
fSize md TyDouble = doubleSize md
fSize md TyLDouble = longDoubleSize md

builtinSize :: MachineDesc -> BuiltinType -> Integer
builtinSize md TyVaList = ptrSize md
builtinSize md TyAny    = ptrSize md

intExpr :: (Pos n, MonadName m) => n -> Integer -> m CExpr
intExpr n i =
  genName >>= \name ->
    return $ CConst $ CIntConst (cInteger i) (mkNodeInfo (posOf n) name)

sizeofType :: (MonadTrav m, CNode n) => MachineDesc -> n -> Type -> m Integer
{'fraction_non_alphanumeric': 0.09134406263592866, 'fraction_numerical': 0.00021748586341887777, 'mean_word_length': 3.238709677419355, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '957721', 'n_tokens_mistral': 1419, 'n_tokens_neox': 1408, 'n_words': 478}