Types.hs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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 80 $
  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 80 $
  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 . PrimType)
  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. typeIvoryType :: Type -> String
  71. typeIvoryType (StructType tn _) = "Struct \"" ++ userTypeStructName tn ++ "\""
  72. typeIvoryType (PrimType (Newtype tn _)) = userTypeModuleName tn
  73. typeIvoryType (PrimType (EnumType tn _ _)) = userTypeModuleName tn
  74. typeIvoryType (PrimType (AtomType a)) = case a of
  75. AtomInt Bits8 -> "Sint8"
  76. AtomInt Bits16 -> "Sint16"
  77. AtomInt Bits32 -> "Sint32"
  78. AtomInt Bits64 -> "Sint64"
  79. AtomWord Bits8 -> "Uint8"
  80. AtomWord Bits16 -> "Uint16"
  81. AtomWord Bits32 -> "Uint32"
  82. AtomWord Bits64 -> "Uint64"
  83. AtomFloat -> "IFloat"
  84. AtomDouble -> "IDouble"
  85. typeIvoryType (PrimType VoidType) = "()"
  86. typeModuleName :: Type -> String
  87. typeModuleName (StructType tn _) = userTypeModuleName tn
  88. typeModuleName (PrimType (Newtype tn _)) = userTypeModuleName tn
  89. typeModuleName (PrimType (EnumType tn _ _)) = userTypeModuleName tn
  90. typeModuleName (PrimType (AtomType _)) = error "do not take typeModuleName of an AtomType"
  91. typeModuleName (PrimType VoidType) = error "do not take typeModuleName of a VoidType"
  92. userTypeModuleName :: String -> String
  93. userTypeModuleName = first_cap . userEnumValueName
  94. where
  95. first_cap (s:ss) = (toUpper s) : ss
  96. first_cap [] = []
  97. userEnumValueName :: String -> String
  98. userEnumValueName = first_lower . u_to_camel
  99. where
  100. first_lower (s:ss) = (toLower s) : ss
  101. first_lower [] = []
  102. u_to_camel ('_':'t':[]) = []
  103. u_to_camel ('_':[]) = []
  104. u_to_camel ('_':a:as) = (toUpper a) : u_to_camel as
  105. u_to_camel (a:as) = a : u_to_camel as
  106. u_to_camel [] = []
  107. userTypeStructName :: String -> String
  108. userTypeStructName = first_lower . drop_t_suffix
  109. where
  110. first_lower (s:ss) = (toLower s) : ss
  111. first_lower [] = []
  112. drop_t_suffix [] = []
  113. drop_t_suffix ('_':'t':[]) = []
  114. drop_t_suffix (a:as) = a : drop_t_suffix as
  115. typeDecl :: Type -> Doc
  116. typeDecl (StructType tname ss) = stack
  117. [ text "[ivory|"
  118. , text "struct" <+> structname
  119. , indent 2 $ encloseStack lbrace rbrace semi
  120. [ text i <+> colon <> colon
  121. <+> text "Stored" <+> text (typeImportedIvoryType (PrimType t))
  122. | (i,t) <- ss ]
  123. , text "|]"
  124. , empty
  125. , packRep <+> colon <> colon <+> text "WrappedPackRep" <+> storedType
  126. , packRep <+> equals <+> text "wrapPackRep" <+> dquotes structname <+> text "$"
  127. , indent 2 $ text "packStruct" <+> encloseStack lbracket rbracket comma
  128. [ text "packLabel" <+> text i
  129. | (i,_) <- ss]
  130. , empty
  131. , text "instance Packable" <+> storedType <+> text "where"
  132. , indent 2 $ text "packRep" <+> equals <+> text "wrappedPackRep" <+> packRep
  133. , empty
  134. , text (userEnumValueName tname) <> text "TypesModule :: Module"
  135. , text (userEnumValueName tname) <> text "TypesModule" <+> equals
  136. <+> text "package" <+> dquotes (structname <> text "_types") <+> text "$ do"
  137. , indent 2 $ stack
  138. [ text "defStruct"
  139. <+> parens (text "Proxy :: Proxy" <+> dquotes structname)
  140. , text "depend serializeModule"
  141. , text "wrappedPackMod" <+> packRep
  142. ]
  143. ]
  144. where
  145. storedType = parens (text "Struct" <+> dquotes structname)
  146. structname = text (userTypeStructName tname)
  147. packRep = text "pack" <> text (userTypeModuleName tname)
  148. typeDecl (PrimType (Newtype tname n)) = stack
  149. [ text "newtype" <+> text typename <+> equals
  150. , indent 2 $ text typename <+> align
  151. (lbrace <+> text ("un" ++ typename ) <+> text "::"
  152. <+> text (typeImportedIvoryType (PrimType n))
  153. </> rbrace <+> typeDeriving (words ("IvoryType IvoryVar IvoryExpr " ++
  154. "IvoryEq IvoryStore IvoryInit IvoryZeroVal Num")))
  155. , empty
  156. , packRep <+> colon <> colon <+> text "WrappedPackRep" <+> storedType
  157. , packRep <+> equals <+> text "wrapPackRep" <+> dquotes (text typename) <+> text "$"
  158. , indent 2 $ text "repackV" <+> text typename <+> text ("un" ++ typename) <+> text "packRep"
  159. , empty
  160. , text "instance Packable" <+> storedType <+> text "where"
  161. , indent 2 $ text "packRep" <+> equals <+> text "wrappedPackRep" <+> packRep
  162. , empty
  163. , text (userEnumValueName tname) <> text "TypesModule :: Module"
  164. , text (userEnumValueName tname) <> text "TypesModule" <+> equals
  165. <+> text "package"
  166. <+> dquotes (text (userTypeStructName tname) <> text "_types")
  167. <+> text "$ do"
  168. , indent 2 $ stack
  169. [ text "depend serializeModule"
  170. , text "wrappedPackMod" <+> packRep
  171. ]
  172. ]
  173. where
  174. typename = userTypeModuleName tname
  175. storedType = parens (text "Stored" <+> text typename)
  176. packRep = text "pack" <> text typename
  177. typeDecl (PrimType (EnumType tname s es)) = stack
  178. [ text "newtype" <+> text typename <+> equals
  179. , indent 2 $ text typename <+> align
  180. (lbrace <+> text ("un" ++ typename) <+> text "::" <+>
  181. text bt </>
  182. rbrace <+> typeDeriving (words ("IvoryType IvoryVar IvoryExpr IvoryEq "
  183. ++ "IvoryStore IvoryInit IvoryZeroVal")))
  184. , empty
  185. , stack
  186. [ stack
  187. [ empty
  188. , text (userEnumValueName i) <+> colon <> colon <+> text typename
  189. , text (userEnumValueName i) <+> equals <+> text typename <+> ppr e
  190. ]
  191. | (i,e) <- es ]
  192. , empty
  193. , packRep <+> colon <> colon <+> text "WrappedPackRep" <+> storedType
  194. , packRep <+> equals <+> text "wrapPackRep" <+> dquotes (text typename) <+> text "$"
  195. , indent 2 $ text "repackV" <+> text typename <+> text ("un" ++ typename) <+> text "packRep"
  196. , empty
  197. , text "instance Packable" <+> storedType <+> text "where"
  198. , indent 2 $ text "packRep" <+> equals <+> text "wrappedPackRep" <+> packRep
  199. , empty
  200. , text (userEnumValueName tname) <> text "TypesModule :: Module"
  201. , text (userEnumValueName tname) <> text "TypesModule" <+> equals
  202. <+> text "package"
  203. <+> dquotes (text (userTypeStructName tname) <> text "_types")
  204. <+> text "$ do"
  205. , indent 2 $ stack
  206. [ text "depend serializeModule"
  207. , text "wrappedPackMod" <+> packRep
  208. ]
  209. ]
  210. where
  211. typename = userTypeModuleName tname
  212. packRep = text "pack" <> text typename
  213. storedType = parens (text "Stored" <+> text typename)
  214. bt = case s of
  215. Bits8 -> "Uint8"
  216. Bits16 -> "Uint16"
  217. Bits32 -> "Uint32"
  218. Bits64 -> "Uint64"
  219. typeDecl a = error ("typeDecl: broken invariant, cannot create type for " ++ show a)
  220. typeDeriving :: [String] -> Doc
  221. typeDeriving cs = text "deriving" <+> parens (commasep (map text cs))
  222. data ImportType = LibraryType String
  223. | UserType String
  224. | NoImport
  225. deriving (Eq, Show)
  226. importType :: Type -> ImportType
  227. importType (StructType n _) = UserType n
  228. importType (PrimType (EnumType n _ _)) = UserType n
  229. importType (PrimType (Newtype n _)) = UserType n
  230. importType (PrimType (AtomType _)) = NoImport
  231. importType (PrimType VoidType) = NoImport
  232. isUserDefined :: Type -> Bool
  233. isUserDefined t = case importType t of
  234. UserType _ -> True
  235. _ -> False
  236. importDecl :: (String -> Doc) -> ImportType -> Doc
  237. importDecl _ (LibraryType p) =
  238. text "import" <+> text p
  239. importDecl mkpath (UserType t) =
  240. text "import qualified" <+> mkpath (userTypeModuleName t)
  241. <+> text "as" <+> text (userTypeModuleName t)
  242. importDecl _ NoImport = empty
  243. encloseStack :: Doc -> Doc -> Doc -> [Doc] -> Doc
  244. encloseStack l r p ds = case ds of
  245. [] -> empty -- l </> r
  246. [d] -> l <+> d </> r
  247. _ -> align (l <+> (folddoc (\a b -> a </> p <+> b) ds) </> r)