Types.hs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. module Gidl.Backend.Ivory.Types where
  2. import Data.Monoid
  3. import Data.List (intercalate, nub)
  4. import Data.Char (toUpper, toLower)
  5. import Gidl.Types
  6. import Ivory.Artifact
  7. import Text.PrettyPrint.Mainland
  8. typeUmbrella :: [String] -> [Type] -> Artifact
  9. typeUmbrella modulepath ts =
  10. artifactPath (intercalate "/" modulepath) $
  11. artifactText ("Types.hs") $
  12. prettyLazyText 1000 $
  13. stack
  14. [ text "module" <+> typeModulePath modulepath "Types" <+> text "where"
  15. , empty
  16. , text "import Ivory.Language"
  17. , stack
  18. [ importDecl (typeModulePath (modulepath ++ ["Types"])) (importType t)
  19. | t <- ts ]
  20. , empty
  21. , text "typeModules :: [Module]"
  22. , text "typeModules ="
  23. , indent 2 $ encloseStack lbracket rbracket comma
  24. [ text tname <> dot
  25. <> text (userEnumValueName tname) <> text "TypesModule"
  26. | t <- ts
  27. , let tname = typeModuleName t
  28. ]
  29. ]
  30. -- invariant: only make a typeModule from a StructType, Newtype, or EnumType
  31. -- i.e. when isUserDefined is true.
  32. typeModule :: [String] -> Type -> Artifact
  33. typeModule modulepath t =
  34. artifactPath (intercalate "/" modulepath) $
  35. artifactText ((typeModuleName t) ++ ".hs") $
  36. prettyLazyText 1000 $
  37. stack
  38. [ text "{-# LANGUAGE DataKinds #-}"
  39. , text "{-# LANGUAGE TypeOperators #-}"
  40. , text "{-# LANGUAGE QuasiQuotes #-}"
  41. , text "{-# LANGUAGE GeneralizedNewtypeDeriving #-}"
  42. , text "{-# LANGUAGE FlexibleInstances #-}"
  43. , text "{-# OPTIONS_GHC -fno-warn-orphans #-}"
  44. , empty
  45. , text "module"
  46. <+> typeModulePath modulepath (typeModuleName t)
  47. <+> text "where"
  48. , empty
  49. , stack (imports ++
  50. [ text "import Ivory.Language"
  51. , text "import Ivory.Serialize"
  52. ])
  53. , empty
  54. , typeDecl t
  55. ]
  56. where
  57. imports = map (importDecl (typeModulePath modulepath))
  58. $ nub
  59. $ map importType
  60. $ typeLeaves t
  61. typeModulePath :: [String] -> String -> Doc
  62. typeModulePath modulepath mname = mconcat $ punctuate dot
  63. $ map text (modulepath ++ [mname])
  64. typeImportedIvoryType :: Type -> String
  65. typeImportedIvoryType t@(PrimType (Newtype tn _)) =
  66. userTypeModuleName tn ++ "." ++ typeIvoryType t
  67. typeImportedIvoryType t@(PrimType (EnumType tn _ _)) =
  68. userTypeModuleName tn ++ "." ++ typeIvoryType t
  69. typeImportedIvoryType t = typeIvoryType t
  70. typeIvoryArea :: Type -> Doc
  71. typeIvoryArea t@(StructType _ _) = parens (text (typeIvoryType t))
  72. typeIvoryArea t@(PrimType (AtomType _)) = parens (text "Stored" <+> text (typeIvoryType t))
  73. typeIvoryArea t@(PrimType _) = parens (text "Stored" <+> text (typeIvoryType t) <> dot <> text (typeIvoryType t))
  74. typeIvoryAreaStructQQ :: Type -> Doc
  75. typeIvoryAreaStructQQ (StructType n _) = text "Struct" <+> text (userTypeStructName n)
  76. typeIvoryAreaStructQQ t = typeIvoryArea t
  77. typeIvoryType :: Type -> String
  78. typeIvoryType (StructType tn _) = "Struct \"" ++ userTypeStructName tn ++ "\""
  79. typeIvoryType (PrimType (Newtype tn _)) = userTypeModuleName tn
  80. typeIvoryType (PrimType (EnumType tn _ _)) = userTypeModuleName tn
  81. typeIvoryType (PrimType (AtomType a)) = case a of
  82. AtomInt Bits8 -> "Sint8"
  83. AtomInt Bits16 -> "Sint16"
  84. AtomInt Bits32 -> "Sint32"
  85. AtomInt Bits64 -> "Sint64"
  86. AtomWord Bits8 -> "Uint8"
  87. AtomWord Bits16 -> "Uint16"
  88. AtomWord Bits32 -> "Uint32"
  89. AtomWord Bits64 -> "Uint64"
  90. AtomFloat -> "IFloat"
  91. AtomDouble -> "IDouble"
  92. typeModuleName :: Type -> String
  93. typeModuleName (StructType tn _) = userTypeModuleName tn
  94. typeModuleName (PrimType (Newtype tn _)) = userTypeModuleName tn
  95. typeModuleName (PrimType (EnumType tn _ _)) = userTypeModuleName tn
  96. typeModuleName (PrimType (AtomType _)) = error "do not take typeModuleName of an AtomType"
  97. userTypeModuleName :: String -> String
  98. userTypeModuleName = first_cap . userEnumValueName
  99. where
  100. first_cap (s:ss) = (toUpper s) : ss
  101. first_cap [] = []
  102. userEnumValueName :: String -> String
  103. userEnumValueName = first_lower . u_to_camel
  104. where
  105. first_lower (s:ss) = (toLower s) : ss
  106. first_lower [] = []
  107. u_to_camel ('_':'t':[]) = []
  108. u_to_camel ('_':[]) = []
  109. u_to_camel ('_':a:as) = (toUpper a) : u_to_camel as
  110. u_to_camel (a:as) = a : u_to_camel as
  111. u_to_camel [] = []
  112. userTypeStructName :: String -> String
  113. userTypeStructName = first_lower . drop_t_suffix
  114. where
  115. first_lower (s:ss) = (toLower s) : ss
  116. first_lower [] = []
  117. drop_t_suffix [] = []
  118. drop_t_suffix ('_':'t':[]) = []
  119. drop_t_suffix (a:as) = a : drop_t_suffix as
  120. ivoryPackageName :: Type -> String
  121. ivoryPackageName (StructType tname _) = userEnumValueName tname ++ "TypesModule"
  122. ivoryPackageName (PrimType (Newtype tname _)) = userEnumValueName tname ++ "TypesModule"
  123. ivoryPackageName (PrimType (EnumType tname _ _)) = userEnumValueName tname ++ "TypesModule"
  124. ivoryPackageName _ = error "can't take ivoryPackageName of builtin type"
  125. qualifiedIvoryPackageName :: Type -> String
  126. qualifiedIvoryPackageName t = typeModuleName t ++ "." ++ ivoryPackageName t
  127. typeDecl :: Type -> Doc
  128. typeDecl t@(StructType tname ss) = stack
  129. [ text "[ivory|"
  130. , text "struct" <+> structname
  131. , indent 2 $ encloseStack lbrace rbrace semi
  132. [ text i <+> colon <> colon
  133. <+> typeIvoryAreaStructQQ st
  134. | (i,st) <- ss ]
  135. , text "|]"
  136. , empty
  137. , packRep <+> colon <> colon <+> text "WrappedPackRep" <+> storedType
  138. , packRep <+> equals <+> text "wrapPackRep" <+> dquotes structname <+> text "$"
  139. , indent 2 $ text "packStruct" <+> encloseStack lbracket rbracket comma
  140. [ text "packLabel" <+> text i
  141. | (i,_) <- ss]
  142. , empty
  143. , text "instance Packable" <+> storedType <+> text "where"
  144. , indent 2 $ text "packRep" <+> equals <+> text "wrappedPackRep" <+> packRep
  145. , empty
  146. , text (ivoryPackageName t) <+> text ":: Module"
  147. , text (ivoryPackageName t) <+> equals
  148. <+> text "package" <+> dquotes (structname <> text "_types") <+> text "$ do"
  149. , indent 2 $ stack $
  150. [ text "defStruct"
  151. <+> parens (text "Proxy :: Proxy" <+> dquotes structname)
  152. , text "depend serializeModule"
  153. , text "wrappedPackMod" <+> packRep
  154. ] ++
  155. [ text "depend" <+> text (qualifiedIvoryPackageName dt)
  156. | dt <- typeLeaves t
  157. , isUserDefined dt
  158. ]
  159. ]
  160. where
  161. storedType = parens (text "Struct" <+> dquotes structname)
  162. structname = text (userTypeStructName tname)
  163. packRep = text "pack" <> text (userTypeModuleName tname)
  164. typeDecl t@(PrimType (Newtype tname n)) = stack
  165. [ text "newtype" <+> text typename <+> equals
  166. , indent 2 $ text typename <+> align
  167. (lbrace <+> text ("un" ++ typename ) <+> text "::"
  168. <+> text (typeImportedIvoryType (PrimType n))
  169. </> rbrace <+> typeDeriving (words ("IvoryType IvoryVar IvoryExpr " ++
  170. "IvoryEq IvoryStore IvoryInit IvoryZeroVal Num")))
  171. , empty
  172. , packRep <+> colon <> colon <+> text "WrappedPackRep" <+> storedType
  173. , packRep <+> equals <+> text "wrapPackRep" <+> dquotes (text tname) <+> text "$"
  174. , indent 2 $ text "repackV" <+> text typename <+> text ("un" ++ typename) <+> text "packRep"
  175. , empty
  176. , text "instance Packable" <+> storedType <+> text "where"
  177. , indent 2 $ text "packRep" <+> equals <+> text "wrappedPackRep" <+> packRep
  178. , empty
  179. , text (ivoryPackageName t) <+> text ":: Module"
  180. , text (ivoryPackageName t) <+> equals
  181. <+> text "package"
  182. <+> dquotes (text (userTypeStructName tname) <> text "_types")
  183. <+> text "$ do"
  184. , indent 2 $ stack $
  185. [ text "depend serializeModule"
  186. , text "wrappedPackMod" <+> packRep
  187. ] ++
  188. [ text "depend" <+> text (qualifiedIvoryPackageName dt)
  189. | dt <- typeLeaves t
  190. , isUserDefined dt
  191. ]
  192. ]
  193. where
  194. typename = userTypeModuleName tname
  195. storedType = parens (text "Stored" <+> text typename)
  196. packRep = text "pack" <> text typename
  197. typeDecl t@(PrimType (EnumType tname s es)) = stack
  198. [ text "newtype" <+> text typename <+> equals
  199. , indent 2 $ text typename <+> align
  200. (lbrace <+> text ("un" ++ typename) <+> text "::" <+>
  201. text bt </>
  202. rbrace <+> typeDeriving (words ("IvoryType IvoryVar IvoryExpr IvoryEq "
  203. ++ "IvoryStore IvoryInit IvoryZeroVal")))
  204. , empty
  205. , stack
  206. [ stack
  207. [ empty
  208. , text (userEnumValueName i) <+> colon <> colon <+> text typename
  209. , text (userEnumValueName i) <+> equals <+> text typename <+> ppr e
  210. ]
  211. | (i,e) <- es ]
  212. , empty
  213. , packRep <+> colon <> colon <+> text "WrappedPackRep" <+> storedType
  214. , packRep <+> equals <+> text "wrapPackRep" <+> dquotes (text tname) <+> text "$"
  215. , indent 2 $ text "repackV" <+> text typename <+> text ("un" ++ typename) <+> text "packRep"
  216. , empty
  217. , text "instance Packable" <+> storedType <+> text "where"
  218. , indent 2 $ text "packRep" <+> equals <+> text "wrappedPackRep" <+> packRep
  219. , empty
  220. , text (ivoryPackageName t) <+> text ":: Module"
  221. , text (ivoryPackageName t) <+> equals
  222. <+> text "package"
  223. <+> dquotes (text (userTypeStructName tname) <> text "_types")
  224. <+> text "$ do"
  225. , indent 2 $ stack
  226. [ text "depend serializeModule"
  227. , text "wrappedPackMod" <+> packRep
  228. ]
  229. ]
  230. where
  231. typename = userTypeModuleName tname
  232. packRep = text "pack" <> text typename
  233. storedType = parens (text "Stored" <+> text typename)
  234. bt = case s of
  235. Bits8 -> "Uint8"
  236. Bits16 -> "Uint16"
  237. Bits32 -> "Uint32"
  238. Bits64 -> "Uint64"
  239. typeDecl a = error ("typeDecl: broken invariant, cannot create type for " ++ show a)
  240. typeDeriving :: [String] -> Doc
  241. typeDeriving cs = text "deriving" <+> parens (commasep (map text cs))
  242. data ImportType = LibraryType String
  243. | UserType String
  244. | NoImport
  245. deriving (Eq, Show)
  246. importType :: Type -> ImportType
  247. importType (StructType n _) = UserType n
  248. importType (PrimType (EnumType n _ _)) = UserType n
  249. importType (PrimType (Newtype n _)) = UserType n
  250. importType (PrimType (AtomType _)) = NoImport
  251. isUserDefined :: Type -> Bool
  252. isUserDefined t = case importType t of
  253. UserType _ -> True
  254. _ -> False
  255. importPrefix :: ImportType -> Doc
  256. importPrefix (UserType t) = text (userTypeModuleName t)
  257. importPrefix _ = empty
  258. importDecl :: (String -> Doc) -> ImportType -> Doc
  259. importDecl _ (LibraryType p) =
  260. text "import" <+> text p
  261. importDecl mkpath (UserType t) =
  262. text "import qualified" <+> mkpath (userTypeModuleName t)
  263. <+> text "as" <+> text (userTypeModuleName t)
  264. importDecl _ NoImport = empty
  265. encloseStack :: Doc -> Doc -> Doc -> [Doc] -> Doc
  266. encloseStack l r p ds = case ds of
  267. [] -> empty -- l </> r
  268. [d] -> align (l <+> d </> r)
  269. _ -> align (l <+> (folddoc (\a b -> a </> p <+> b) ds) </> r)