-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Macros: support for character and string literals #409
base: main
Are you sure you want to change the base?
Conversation
0a5e856
to
d225e6d
Compare
Add examples (tests) |
Yes, that's the next step. This is a draft. |
This commit adds support for C character literals and string literals in macros. Some remarks: - The implementation assumes that the source file is encoded in UTF-8. I believe that's the only encoding that the Clang library supports anyway. - The implementation doesn't support wide character types, such as 'uint16_t'. - C character literals are of type 'int', not 'char'. We distinguish two types of such literals, as per the C standard: * code unit: a specific 'int' value, whose interpretation as a linguistic or symbolic character depends on the choice of a text encoding, * Unicode code point: a unique numeric identifier for a specific character, whose value (e.g. in a 'char *' array) depends on the text encoding chosen. - We translate character literals to 'CInt', and use unboxed 'Addr#' literals to translate string literals to 'CStringLen' (allowing for the possibility of inner null bytes).
@@ -142,6 +145,7 @@ data SExpr ctx = | |||
| EIntegral Integer (Maybe HsPrimType) | |||
| EFloat Float HsPrimType -- ^ Type annotation to distinguish Float/CFLoat | |||
| EDouble Double HsPrimType | |||
| EString [Word8] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't we use ByteArray
, or some better representation. [Word8]
doesn't feel good.
goChar (C.CharLiteral { charLiteralValue = c }) = | ||
( `Hs.VarDeclIntegral` HsPrimCInt ) <$> | ||
case c of | ||
C.CodeUnit u |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
extract into separate functions. Preferably don't invent UTF8 parsing validation, there are plenty in the libs.
This commit adds support for C character literals and string literals in macros.
Some remarks:
char16_t
.int
, notchar
. We distinguish two types of such literals, as per the C standard:int
value, whose interpretation as a linguistic or symbolic character depends on the choice of a text encoding,char *
array) depends on the text encoding chosen.CInt
, and use unboxedAddr#
literals to translate string literals toCStringLen
(allowing for the possibility of inner null bytes).