-
Notifications
You must be signed in to change notification settings - Fork 49
Support identifiers for namespace/template of C++ #83
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
Conversation
43525c9
to
a492ef5
Compare
Though cIdent parser of first commit is hackey, cppIdentParser and its test are added on last commit. |
I really want this, too! Could you please retry Travis tests? It seems to be a temporary network problem. |
ok、I try rebuild. |
Travis tests have been passed. |
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.
thanks for the contribution! i'll take a closer look today
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.
I'm not sure that parsing template applications into single identifiers is a good idea -- apart from being pretty opaque, it currently doesn't support anything but other identifiers as parameters.
There's also the problem that this addition is now present for all uses of inline-c, although I'm a bit less bothered by that.
I'd be happier with extending data Type i
with a CppTemplate P.CIdentifier [Type i]
, and then implementing the proper C++ grammar for template applications.
What do you think?
Thank you for your reply. I appreciate your reviewing.
I think so. I'm sorry to trouble you.
This is good. I will try it. |
@junjihashimoto oh, nothing to be sorry about! I think it's a very nice feature to add, we just need to add it right. And sorry about how long it took me to review this.
Yes, we have to extend our types a bit. I'd be pretty conservative, probably adding only numeric literals, since those are very useful in C++. |
Is something bad in adding something that can be a type in C++ but not in C? |
@cblp no, but the way it is added in the current pr is extremely ad-hoc -- only identifiers can be arguments of templates, and that property is not even checked. |
@bitonic long time no see. I have extended
|
Current implementation improves mapping of template-type. |
@junjihashimoto thanks! I'll take a look. |
@junjihashimoto First of all, the branch in this PR seems to contain a lot of unrelated changes. Could you isolate the changes related to the template arguments, or at the very least have them so that I can easily review commit-by-commit? Thanks! |
@bitonic |
This pr includes following items.
|
@@ -87,7 +87,50 @@ catchBlock = QuasiQuoter | |||
, quoteDec = unsupported | |||
} where | |||
unsupported _ = fail "Unsupported quasiquotation." | |||
|
|||
|
|||
exceptionalValue :: String -> String |
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.
Please comment on what warning this avoids and how you generated this list.
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.
Add the comment.
@@ -22,6 +26,7 @@ cppCtx :: Context | |||
cppCtx = baseCtx <> mempty | |||
{ ctxForeignSrcLang = Just TH.LangCxx | |||
, ctxOutput = Just $ \s -> "extern \"C\" {\n" ++ s ++ "\n}" | |||
, ctxEnableCpp = True |
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.
Is this necessary? Can't we just use LangCxx
to generate this boolean?
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.
It is okay. use langCxx
inline-c/src/Language/C/Types.hs
Outdated
@@ -100,6 +103,8 @@ data TypeSpecifier | |||
| TypeName P.CIdentifier | |||
| Struct P.CIdentifier | |||
| Enum P.CIdentifier | |||
| Template P.CIdentifier [TypeSpecifier] |
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.
Please prefix these two cases with Cxx
.
inline-c-cpp/test/tests.hs
Outdated
@@ -1,18 +1,48 @@ | |||
{-# LANGUAGE TemplateHaskell #-} | |||
{-# LANGUAGE CPP #-} |
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.
Pleaste add some tests where the context contains a mapping from a templated type to a haskell type. E.g. * vector<A, B>
to something in Haskell.
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.
The mapping of std::vector<Test::Test>
is included in the file.
inline-c-cpp/test/tests.hs
Outdated
@@ -1,18 +1,48 @@ | |||
{-# LANGUAGE TemplateHaskell #-} | |||
{-# LANGUAGE CPP #-} | |||
{-# LANGUAGE ConstraintKinds #-} |
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.
Are all these extensions really needed? At the very least remove the dangerous ones, such as UndecidableInstances
.
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.
No. So I am going to reduce the extensions.
inline-c/src/Language/C/Types.hs
Outdated
@@ -484,6 +509,8 @@ instance PP.Pretty TypeSpecifier where | |||
TypeName s -> PP.pretty s | |||
Struct s -> "struct" <+> PP.pretty s | |||
Enum s -> "enum" <+> PP.pretty s | |||
Template s args -> PP.pretty s <+> "<" <+> mconcat (intersperse "," (map PP.pretty args)) <+> ">" |
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.
I'd do " >"
so that this will work even without C++11.
@@ -212,13 +215,14 @@ quickCParser typeNames s p = case runCParser typeNames "quickCParser" s p of | |||
-- | Like 'quickCParser', but uses @'cCParserContext' ('const' 'False')@ as | |||
-- 'CParserContext'. | |||
quickCParser_ | |||
:: String | |||
:: Bool |
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.
Two things:
- To parse namespaces, just change the definition of
cpcParseIdent
for C++, and exportcxxidentifier_no_lex
- For templates, put a
Bool
(saycpcParseCxxTemplates
) in theCParserContext
.
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.
cCParserContext has The Bool to change the parser for C++.
cxxidentifier_no_lex is not exported, because identifier_no_lex reads and uses CParserContext' value to change the parser.
@@ -293,11 +298,14 @@ data TypeSpecifier | |||
| Struct CIdentifier | |||
| Enum CIdentifier | |||
| TypeName CIdentifier | |||
| Template CIdentifier [TypeSpecifier] | |||
| TemplateConst String |
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.
Right now this seems to only parse integral const expressions. That's OK, but can you document this, linking to some documentation on what is the full range of const template argument that you can have?
templateParser s = parse' | ||
where | ||
parse' = do | ||
id' <- cidentParserWithNamespace |
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.
Why do we need this rather than just using cpcParseIdent
? That should include namespaces now, right?
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.
It should include namespaces to parse the format like "std::vector".
try (concat <$> sequence [cidentParser, (string "::"), cidentParserWithNamespace]) <|> | ||
cidentParser | ||
templateArgType = try type_specifier <|> (TemplateConst <$> (many $ oneOf ['0'..'9'])) | ||
templateArgParser' = do |
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.
Please use sepBy templateArgType (symbol ",")
or similar.
@junjihashimoto did a first review pass. Overall the approach looks good, thanks a lot for the contribution! |
@bitonic Thank you for reviewing and good advice. I am going to update the pr soon. |
I think the modification is done and there may be some differences from your expectations. |
@junjihashimoto just wanted to let you know that I have not forgotten about this -- but I've been a bit busy and I'm not entirely sure that the template parsing is safe. Specifically I'm not sure whether we need to store the C++ type names too. I'll try to check and merge this soon. |
I can wait. |
@bitonic |
@junjihashimoto I'm very sorry about the delay, I'll try to review it in the next few days. |
I'm happy to hear that. |
My modification is done. |
@junjihashimoto I have rebased and merged your PR. Again, deep apologies about the delay, I got really busy and this slipped off my radar. Moreover, it wasn't an obvious merge -- I'm still not totally convinced about the new grammar. But it's a useful feature, and I think it's better to have it. Thank you so much for the change and the patience. |
Released now as inline-c 0.9.0.0 and inline-c-cpp 0.3.1.0. |
@bitonic |
@bitonic |
This PR supports identifiers for namespace/template of C++.
We want to use libtorch library which uses various Tensor classes(at::Tensor, torch::Tensor, caffe2::Tensor and so on).
I think cIdentLetter should support c types only and this modification is hackey.
But the implementation of identifier is not pluggable.
Could you merge this PR?