<commit_before>{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE LambdaCase            #-}
{-# LANGUAGE NamedFieldPuns        #-}
{-# LANGUAGE OverloadedStrings     #-}
module Trans.RawToHaskell (fileToModules) where

import Data.Word

import Data.Maybe  (fromJust)
import Data.String (fromString)
import GHC.Exts    (fromList)

import qualified Data.Text as T

import IR.Haskell
import Trans.ToHaskellCommon

import qualified Capnp
import qualified Capnp.Untyped.Pure as Untyped

import qualified IR.Common as C
import qualified IR.Name   as Name
import qualified IR.Raw    as Raw

tMutMsg :: Type
tMutMsg = TApp (tgName ["Message"] "MutMsg" ) [TVar "s"]

tConstMsg :: Type
tConstMsg = tgName ["Message"] "ConstMsg"

fileToModules :: Raw.File -> [Module]
fileToModules file =
    [ fileToMainModule file
    , fileToModuleAlias file
    ]

fileToModuleAlias :: Raw.File -> Module
fileToModuleAlias Raw.File{fileName, fileId} =
    let reExport = ["Capnp", "Gen"] ++ makeModName fileName
    in Module
        { modName = idToModule fileId
        , modLangPragmas = []
        , modExports = Just [ExportMod reExport]
        , modImports = [ ImportAll { parts = reExport } ]
        , modDecls = []
        }

fileToMainModule :: Raw.File -> Module
fileToMainModule Raw.File{fileName, fileId, decls} =
    fixImports $ Module
        { modName = ["Capnp", "Gen"] ++ makeModName fileName
        , modLangPragmas =
            [ "FlexibleContexts"
            , "FlexibleInstances"
            , "MultiParamTypeClasses"
            , "TypeFamilies"
            , "DeriveGeneric"
            , "OverloadedStrings"
            ]
        , modExports = Nothing
        , modImports =
            [ imp ["Capnp", "Message"] "Message"
            , imp ["Capnp", "Untyped"] "Untyped"
            , imp ["Capnp", "Basics"] "Basics"
            , imp ["Capnp", "GenHelpers"] "GenHelpers"
            , imp ["Capnp", "Classes"] "Classes"
            , imp ["GHC", "Generics"] "Generics"

            -- So we can treat 'Word1' the same as 'Word16' etc.
            , imp ["Capnp", "Bits"] "Std_"

            , imp ["Data", "Maybe"] "Std_"
            , imp ["Capnp", "GenHelpers", "ReExports", "Data", "ByteString"] "BS"
            ]
        , modDecls = concatMap (declToDecls fileId) decls
        }
  where
    imp parts importAs = ImportAs {parts, importAs}

declToDecls :: Word64 -> Raw.Decl -> [Decl]
declToDecls thisMod Raw.UnionVariant{parentTypeCtor, typeParams, tagOffset, unionDataCtors} =
    let unknownCtor = Name.mkSub parentTypeCtor "unknown'"
        typeCtor = Name.mkSub parentTypeCtor ""
        containerTypeType = containerTypeToType typeCtor typeParams
        typeParamNames = map (TVar . Name.typeVarName) typeParams
    in
    [ DcData Data
        { dataName = Name.localToUnQ typeCtor
        , dataNewtype = False
        , typeArgs = typeParamNames ++ [TVar "msg"]
        , dataVariants =
            [ DataVariant
                { dvCtorName = Name.localToUnQ dataCtor
                , dvArgs = APos $
                    case locType of
                        C.VoidField ->
                            []
                        _ ->
                            [ typeToType thisMod (C.fieldType locType) (TVar "msg") ]
                }
            | Raw.Variant{name=dataCtor, locType} <- unionDataCtors
            ]
            ++
            [ DataVariant
                { dvCtorName = Name.localToUnQ unknownCtor
                , dvArgs = APos [ TPrim $ C.PrimInt $ C.IntType C.Unsigned C.Sz16 ]
                }
            ]
        , derives = []
        }
    , instance_ [] ["Classes"] "FromStruct" [TVar "msg", containerTypeType (TVar "msg")]
        [ iValue "fromStruct" [PVar "struct"] $ EDo
            [ DoBind "tag"
                (EApp<commit_msg>Generate missing FromPtr constraint in context of FromStruct instances.<commit_after>{-# LANGUAGE DuplicateRecordFields #-}
{-# LANGUAGE LambdaCase            #-}
{-# LANGUAGE NamedFieldPuns        #-}
{-# LANGUAGE OverloadedStrings     #-}
module Trans.RawToHaskell (fileToModules) where

import Data.Word

import Data.Maybe  (fromJust)
import Data.String (fromString)
import GHC.Exts    (fromList)

import qualified Data.Text as T

import IR.Haskell
import Trans.ToHaskellCommon

import qualified Capnp
import qualified Capnp.Untyped.Pure as Untyped

import qualified IR.Common as C
import qualified IR.Name   as Name
import qualified IR.Raw    as Raw

tMutMsg :: Type
tMutMsg = TApp (tgName ["Message"] "MutMsg" ) [TVar "s"]

tConstMsg :: Type
tConstMsg = tgName ["Message"] "ConstMsg"

fileToModules :: Raw.File -> [Module]
fileToModules file =
    [ fileToMainModule file
    , fileToModuleAlias file
    ]

fileToModuleAlias :: Raw.File -> Module
fileToModuleAlias Raw.File{fileName, fileId} =
    let reExport = ["Capnp", "Gen"] ++ makeModName fileName
    in Module
        { modName = idToModule fileId
        , modLangPragmas = []
        , modExports = Just [ExportMod reExport]
        , modImports = [ ImportAll { parts = reExport } ]
        , modDecls = []
        }

fileToMainModule :: Raw.File -> Module
fileToMainModule Raw.File{fileName, fileId, decls} =
    fixImports $ Module
        { modName = ["Capnp", "Gen"] ++ makeModName fileName
        , modLangPragmas =
            [ "FlexibleContexts"
            , "FlexibleInstances"
            , "MultiParamTypeClasses"
            , "TypeFamilies"
            , "DeriveGeneric"
            , "OverloadedStrings"
            ]
        , modExports = Nothing
        , modImports =
            [ imp ["Capnp", "Message"] "Message"
            , imp ["Capnp", "Untyped"] "Untyped"
            , imp ["Capnp", "Basics"] "Basics"
            , imp ["Capnp", "GenHelpers"] "GenHelpers"
            , imp ["Capnp", "Classes"] "Classes"
            , imp ["GHC", "Generics"] "Generics"

            -- So we can treat 'Word1' the same as 'Word16' etc.
            , imp ["Capnp", "Bits"] "Std_"

            , imp ["Data", "Maybe"] "Std_"
            , imp ["Capnp", "GenHelpers", "ReExports", "Data", "ByteString"] "BS"
            ]
        , modDecls = concatMap (declToDecls fileId) decls
        }
  where
    imp parts importAs = ImportAs {parts, importAs}

declToDecls :: Word64 -> Raw.Decl -> [Decl]
declToDecls thisMod Raw.UnionVariant{parentTypeCtor, typeParams, tagOffset, unionDataCtors} =
    let unknownCtor = Name.mkSub parentTypeCtor "unknown'"
        typeCtor = Name.mkSub parentTypeCtor ""
        containerTypeType = containerTypeToType typeCtor typeParams
        typeParamNames = map (TVar . Name.typeVarName) typeParams
    in
    [ DcData Data
        { dataName = Name.localToUnQ typeCtor
        , dataNewtype = False
        , typeArgs = typeParamNames ++ [TVar "msg"]
        , dataVariants =
            [ DataVariant
                { dvCtorName = Name.localToUnQ dataCtor
                , dvArgs = APos $
                    case locType of
                        C.VoidField ->
                            []
                        _ ->
                            [ typeToType thisMod (C.fieldType locType) (TVar "msg") ]
                }
            | Raw.Variant{name=dataCtor, locType} <- unionDataCtors
            ]
            ++
            [ DataVariant
                { dvCtorName = Name.localToUnQ unknownCtor
                , dvArgs = APos [ TPrim $ C.PrimInt $ C.IntType C.Unsigned C.Sz16 ]
                }
            ]
        , derives = []
        }
    , let ctx =
            [ TApp (tgName ["Classes"] "FromPtr") [TVar "msg", v]
            | v <- typeParamNames
            ]
      in
      instance_ ctx ["Classes"] "FromStruct" [TVar "msg", containerTypeType (TVar "msg")]
        [ iValue "fromStruct" [PVar "struct"] $ EDo
            [ DoBind "tag"
                (EApp
{'fraction_non_alphanumeric': 0.11042787599282602, 'fraction_numerical': 0.0017934921854983346, 'mean_word_length': 1.9911877394636015, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 17, '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': '3299375', 'n_tokens_mistral': 2396, 'n_tokens_neox': 2352, 'n_words': 578}