digitalmars.D - built in import c header files
- 12345swordy (5/5) Oct 06 2020 I remember reading a reddit comment saying that if d were to
- Meta (2/7) Oct 06 2020 You're in luck: https://github.com/atilaneves/dpp
- 12345swordy (3/12) Oct 06 2020 What part of "built in and not part of a 3rd party library" do
- norm (5/18) Oct 06 2020 You could easily work it out for yourself by looking at any C
- Atila Neves (7/26) Oct 07 2020 It really isn't. Can it be easy? Yes. Is it easy in the general
- norm (11/33) Oct 07 2020 Really? I have not had any issues before automatically creating C
- Atila Neves (33/69) Oct 08 2020 Imagine this and extrapolate to hundreds of headers:
- Walter Bright (7/7) Apr 17 2021 There's another huge problem with C files.
- Max Haughton (6/11) Oct 06 2020 Not impossible (it's worth saying that you can literally do this
- Walter Bright (3/8) Apr 17 2021 You'll also have problems with all the nutburger C extensions implemente...
- 12345swordy (5/14) Apr 17 2021 There is no need for the dmd frontend to implement that. The ldc2
- Max Haughton (3/18) Apr 17 2021 No they can't or at least GCC *maybe* could but LDC is a separate
- 12345swordy (7/28) Apr 17 2021 Actually they can. It is a question of money/time/resources not
- Alexis Kyaw (32/37) Apr 17 2021 This would indeed be a total game changer.
- Imperatorn (27/67) Apr 17 2021 +1
- Atila Neves (3/12) Apr 21 2021 Why would one implement a C parser (never mind a C++ one) when
- FeepingCreature (28/33) Apr 18 2021 I've been doing this in both of my compiled languages. (fcc
- evilrat (4/21) Apr 19 2021 Yeah, just until it fails to convert something and blocks
- FeepingCreature (12/18) Apr 19 2021 Yeah I think it differs between using it for system headers and
I remember reading a reddit comment saying that if d were to import c header files directly without any 3rd party libraries it would be a game changer. The question is: how difficult to implement a C AST parser for the dmd front end? -Alex
Oct 06 2020
On Tuesday, 6 October 2020 at 20:59:54 UTC, 12345swordy wrote:I remember reading a reddit comment saying that if d were to import c header files directly without any 3rd party libraries it would be a game changer. The question is: how difficult to implement a C AST parser for the dmd front end? -AlexYou're in luck: https://github.com/atilaneves/dpp
Oct 06 2020
On Tuesday, 6 October 2020 at 21:04:20 UTC, Meta wrote:On Tuesday, 6 October 2020 at 20:59:54 UTC, 12345swordy wrote:What part of "built in and not part of a 3rd party library" do you not understand?I remember reading a reddit comment saying that if d were to import c header files directly without any 3rd party libraries it would be a game changer. The question is: how difficult to implement a C AST parser for the dmd front end? -AlexYou're in luck: https://github.com/atilaneves/dpp
Oct 06 2020
On Tuesday, 6 October 2020 at 22:42:11 UTC, 12345swordy wrote:On Tuesday, 6 October 2020 at 21:04:20 UTC, Meta wrote:You could easily work it out for yourself by looking at any C parser and the DMD front. But why bother with C, it is trivial to generate C bindings from headers. C++ is where it becomes complicated and where it would be useful.On Tuesday, 6 October 2020 at 20:59:54 UTC, 12345swordy wrote:What part of "built in and not part of a 3rd party library" do you not understand?I remember reading a reddit comment saying that if d were to import c header files directly without any 3rd party libraries it would be a game changer. The question is: how difficult to implement a C AST parser for the dmd front end? -AlexYou're in luck: https://github.com/atilaneves/dpp
Oct 06 2020
On Wednesday, 7 October 2020 at 01:29:05 UTC, norm wrote:On Tuesday, 6 October 2020 at 22:42:11 UTC, 12345swordy wrote:On Tuesday, 6 October 2020 at 21:04:20 UTC, Meta wrote:You could easily work it out for yourself by looking at any C parser and the DMD front.On Tuesday, 6 October 2020 at 20:59:54 UTC, 12345swordy wrote:What part of "built in and not part of a 3rd party library" do you not understand?I remember reading a reddit comment saying that if d were to import c header files directly without any 3rd party libraries it would be a game changer. The question is: how difficult to implement a C AST parser for the dmd front end? -AlexYou're in luck: https://github.com/atilaneves/dppBut why bother with C, it is trivial to generate C bindings from headers.It really isn't. Can it be easy? Yes. Is it easy in the general case? No. I've tried calling a large C codebase from D before. It was a nightmare. It's very hard to do manual wrapping when you have thousands of headers in hundreds of directories and everything depends on everything.
Oct 07 2020
On Wednesday, 7 October 2020 at 10:04:57 UTC, Atila Neves wrote:On Wednesday, 7 October 2020 at 01:29:05 UTC, norm wrote:Really? I have not had any issues before automatically creating C bindings for D using codegen. Sure, occasionally a macro will bite but it isn't that often nor difficult to fix. They were not the kernel but they were not small code bases without macros either. TBH though now I just use dpp so I don't see the value of bolting on a "C" parser to the D front end. C++ I have struggled with that and it requires an annoying amount of manual editing. DPP works well but even it falls over for some C++ code bases I've tried.On Tuesday, 6 October 2020 at 22:42:11 UTC, 12345swordy wrote:On Tuesday, 6 October 2020 at 21:04:20 UTC, Meta wrote:You could easily work it out for yourself by looking at any C parser and the DMD front.On Tuesday, 6 October 2020 at 20:59:54 UTC, 12345swordy wrote:What part of "built in and not part of a 3rd party library" do you not understand?I remember reading a reddit comment saying that if d were to import c header files directly without any 3rd party libraries it would be a game changer. The question is: how difficult to implement a C AST parser for the dmd front end? -AlexYou're in luck: https://github.com/atilaneves/dppBut why bother with C, it is trivial to generate C bindings from headers.
Oct 07 2020
On Wednesday, 7 October 2020 at 11:14:02 UTC, norm wrote:On Wednesday, 7 October 2020 at 10:04:57 UTC, Atila Neves wrote:Imagine this and extrapolate to hundreds of headers: --- // hdr0.h struct struct0 { struct1* s1; }; --- --- // hdr1.h #define SILLY_MACRO struct2; struct struct1 { SILLY_MACRO* s2; } ---- --- // hdr2.h struct struct2 { struct3* s3; } ---On Wednesday, 7 October 2020 at 01:29:05 UTC, norm wrote:Really? I have not had any issues before automatically creating C bindings for D using codegen. Sure, occasionally a macro will bite but it isn't that often nor difficult to fix. They were not the kernel but they were not small code bases without macros either.On Tuesday, 6 October 2020 at 22:42:11 UTC, 12345swordy wrote:On Tuesday, 6 October 2020 at 21:04:20 UTC, Meta wrote:You could easily work it out for yourself by looking at any C parser and the DMD front.On Tuesday, 6 October 2020 at 20:59:54 UTC, 12345swordy wrote:What part of "built in and not part of a 3rd party library" do you not understand?I remember reading a reddit comment saying that if d were to import c header files directly without any 3rd party libraries it would be a game changer. The question is: how difficult to implement a C AST parser for the dmd front end? -AlexYou're in luck: https://github.com/atilaneves/dppBut why bother with C, it is trivial to generate C bindings from headers.TBH though now I just use dpp so I don't see the value of bolting on a "C" parser to the D front end. C++ I have struggled with that and it requires an annoying amount of manual editing. DPP works well but even it falls over for some C++ code bases I've tried.dpp doesn't officially support C++ yet, because... it doesn't work yet. There are several reasons for this. The first, and most annoying, is that libclang isn't all it's cracked up to be. There's a *lot* of functionality missing for C++ that the compiler frontend itself knows about but doesn't expose via the C API. The second, and maybe unfixable reason, is how to translate C++-only concepts to D. Consider std::is_reference_v. That can't be written in D, not even by hand. What if that gets used in SFINAE? What about expression templates given how operator overloading in D is a lot different? I still don't know how to get around that.
Oct 08 2020
There's another huge problem with C files. It's the predefined macros. Every compiler generates a different set of them, and in each compiler the set varies by compiler version, target, switch settings, etc. Most are undocumented. I think gcc has somewhere near a thousand of them. Naturally, every non-trivial C program winds up totally dependent on them.
Apr 17 2021
On Tuesday, 6 October 2020 at 20:59:54 UTC, 12345swordy wrote:I remember reading a reddit comment saying that if d were to import c header files directly without any 3rd party libraries it would be a game changer. The question is: how difficult to implement a C AST parser for the dmd front end? -AlexNot impossible (it's worth saying that you can literally do this as a library via mixins), but having a c parser in the compiler seems like too much work when it's already fairly trivial to do. If D had a team of full time staff on payroll, then it could be made to work but it adds so much surface area to the compiler.
Oct 06 2020
On 10/6/2020 7:56 PM, Max Haughton wrote:Not impossible (it's worth saying that you can literally do this as a library via mixins), but having a c parser in the compiler seems like too much work when it's already fairly trivial to do. If D had a team of full time staff on payroll, then it could be made to work but it adds so much surface area to the compiler.You'll also have problems with all the nutburger C extensions implemented by various compilers.
Apr 17 2021
On Saturday, 17 April 2021 at 21:32:24 UTC, Walter Bright wrote:On 10/6/2020 7:56 PM, Max Haughton wrote:There is no need for the dmd frontend to implement that. The ldc2 and the gdc compilers can relay on their c++ counterparts to provided the c extensions. -AlexNot impossible (it's worth saying that you can literally do this as a library via mixins), but having a c parser in the compiler seems like too much work when it's already fairly trivial to do. If D had a team of full time staff on payroll, then it could be made to work but it adds so much surface area to the compiler.You'll also have problems with all the nutburger C extensions implemented by various compilers.
Apr 17 2021
On Saturday, 17 April 2021 at 22:12:41 UTC, 12345swordy wrote:On Saturday, 17 April 2021 at 21:32:24 UTC, Walter Bright wrote:No they can't or at least GCC *maybe* could but LDC is a separate project entirely from clang.On 10/6/2020 7:56 PM, Max Haughton wrote:There is no need for the dmd frontend to implement that. The ldc2 and the gdc compilers can relay on their c++ counterparts to provided the c extensions. -AlexNot impossible (it's worth saying that you can literally do this as a library via mixins), but having a c parser in the compiler seems like too much work when it's already fairly trivial to do. If D had a team of full time staff on payroll, then it could be made to work but it adds so much surface area to the compiler.You'll also have problems with all the nutburger C extensions implemented by various compilers.
Apr 17 2021
On Saturday, 17 April 2021 at 23:22:47 UTC, Max Haughton wrote:On Saturday, 17 April 2021 at 22:12:41 UTC, 12345swordy wrote:Actually they can. It is a question of money/time/resources not programming ability here. Regardless the dmd front-end just need to strictly implement the c99 standard(no language extension by the compiler of any sorts) to get the ball rolling. -AlexOn Saturday, 17 April 2021 at 21:32:24 UTC, Walter Bright wrote:No they can't or at least GCC *maybe* could but LDC is a separate project entirely from clang.On 10/6/2020 7:56 PM, Max Haughton wrote:There is no need for the dmd frontend to implement that. The ldc2 and the gdc compilers can relay on their c++ counterparts to provided the c extensions. -AlexNot impossible (it's worth saying that you can literally do this as a library via mixins), but having a c parser in the compiler seems like too much work when it's already fairly trivial to do. If D had a team of full time staff on payroll, then it could be made to work but it adds so much surface area to the compiler.You'll also have problems with all the nutburger C extensions implemented by various compilers.
Apr 17 2021
On Tuesday, 6 October 2020 at 20:59:54 UTC, 12345swordy wrote:I remember reading a reddit comment saying that if d were to import c header files directly without any 3rd party libraries it would be a game changer. The question is: how difficult to implement a C AST parser for the dmd front end? -AlexThis would indeed be a total game changer. I just looked at D in the course of checking out newer programming languages and it turns out that I think none of them will be really successful replacing C (or C++) because there's no direct compatibility with C (Priority 1) and C++ (Priority 2) and ideally not only using the C (and potentially C++) binaries directly, but also creating header files to allow using the D binaries smoothly "on the other side". There is just some much existing code that you would totally on the winner side with this and your totally out in the desert without it. Just like the biggest argument for Java is it's ecosystem, this would make all C (and maybe even C++) libraries part of the D ecosystem. For this to be really an argument, it must be an in-built feature of the language toolchain and a priority in all future developments. Just imagine what this would mean: a clean way forward from existing C code to write bulletproof applications on top of all the existing code. And a way to use the new code from new libraries written in D without any hassle. A potential external tool would a C to D converter which also allows easy migration of C code. Just imagine converting a complete Linux implementation to D, compile it and having a full D-based Linux. Then you could start migrating the code base to use the advanced features of D and start a new age for the whole industry. This would make the world listen. Actually it looks as if this is not such an ambitious goal for D, so I'm surprised it's not under way. If had time I'd maybe look at it myself, unfortunately I have project work and 3 children, leaving really very little time for extra projects.
Apr 17 2021
On Saturday, 17 April 2021 at 08:36:41 UTC, Alexis Kyaw wrote:On Tuesday, 6 October 2020 at 20:59:54 UTC, 12345swordy wrote:+1 Have been experimenting with converting C(++) to D. It's possible to some extent. *dream hat on* I've thought about this, and what would be best for D is if code could be converted. Why? Because otherwise we still just have built a lot of stuff upon the old, meaning that the % of D in the world hasn't really increased much. Also, any modifications would be hard etc etc. What's lacking tho is good tooling around D, like static analysis etc etc. But, if C(++) could be converted to D, *some* parts of the tools might be reusable, and you would get a compounding effect after a while. For instance, even *if* C(++) and D could be seamlessly mixed in the same source file, you would still end up with much C(++) code. I still think dpp, dstep etc are 100% needed, and more work should definitely be put in it, but the endgame would be to somehow increase the amount of D, right? (Btw, what's the state of cpp2d?) Yes, that would mean you end up with two code bases, but that's kind of the point in this case. There are obviously + and - with this solution. But maybe the endgame deluxe would be to be able to reuse the parts that work well and convert the parts that doesn't. Best of both worlds. *dream hat off* <- noteI remember reading a reddit comment saying that if d were to import c header files directly without any 3rd party libraries it would be a game changer. The question is: how difficult to implement a C AST parser for the dmd front end? -AlexThis would indeed be a total game changer. I just looked at D in the course of checking out newer programming languages and it turns out that I think none of them will be really successful replacing C (or C++) because there's no direct compatibility with C (Priority 1) and C++ (Priority 2) and ideally not only using the C (and potentially C++) binaries directly, but also creating header files to allow using the D binaries smoothly "on the other side". There is just some much existing code that you would totally on the winner side with this and your totally out in the desert without it. Just like the biggest argument for Java is it's ecosystem, this would make all C (and maybe even C++) libraries part of the D ecosystem. For this to be really an argument, it must be an in-built feature of the language toolchain and a priority in all future developments. Just imagine what this would mean: a clean way forward from existing C code to write bulletproof applications on top of all the existing code. And a way to use the new code from new libraries written in D without any hassle. A potential external tool would a C to D converter which also allows easy migration of C code. Just imagine converting a complete Linux implementation to D, compile it and having a full D-based Linux. Then you could start migrating the code base to use the advanced features of D and start a new age for the whole industry. This would make the world listen. Actually it looks as if this is not such an ambitious goal for D, so I'm surprised it's not under way. If had time I'd maybe look at it myself, unfortunately I have project work and 3 children, leaving really very little time for extra projects.
Apr 17 2021
On Saturday, 17 April 2021 at 08:36:41 UTC, Alexis Kyaw wrote:On Tuesday, 6 October 2020 at 20:59:54 UTC, 12345swordy wrote:Why would one implement a C parser (never mind a C++ one) when libclang exists?I remember reading a reddit comment saying that if d were to import c header files directly without any 3rd party libraries it would be a game changer. The question is: how difficult to implement a C AST parser for the dmd front end? -AlexThis would indeed be a total game changer.
Apr 21 2021
On Tuesday, 6 October 2020 at 20:59:54 UTC, 12345swordy wrote:I remember reading a reddit comment saying that if d were to import c header files directly without any 3rd party libraries it would be a game changer. The question is: how difficult to implement a C AST parser for the dmd front end? -AlexI've been doing this in both of my compiled languages. (fcc first, built-in, and now in cx as a macro.) For an example of it in action, check out my glfw sample https://github.com/FeepingCreature/Cx/blob/master/demos/glfw.cx , or the macro code for it https://github.com/FeepingCreature/Cx/blob/master/src/cx/macros/cimport.cx . Three things that jump out: 1. preprocessed headers with `gcc -dD -E` are a lot easier to work with, because gcc helpfully unrolls *most* of #define definitions, but leaves enough standing so that you can generate enums for all the #defines. This is why it's a useful thing to have unrestricted IO support, including arbitrary command execution, in CTFE. :) 2. "Header C" is actually pretty easy to parse if you just studiously ignore all the weird things, weird inside-out type format aside. 3. 10% of effort to parse C gets you 99% of value: because the vast majority of typing work involved in making a C header binding is trivially simple to parse from the header, and the rest you can always just do by hand, or (if your language has macros) expand as you need it. I presume glibc headers are more complicated, but I have been massively positively surprised how much of GLFW and GL I could get running with a day or two of coding work on this. To a first approximation, once I added function pointers, it pretty much all just worked. I can wholeheartedly recommend any language do this. It unlocks a lot of value and removes a lot of manual work.
Apr 18 2021
On Monday, 19 April 2021 at 06:51:54 UTC, FeepingCreature wrote:On Tuesday, 6 October 2020 at 20:59:54 UTC, 12345swordy wrote:Yeah, just until it fails to convert something and blocks compilation, where no manual fixes is possible or not even allowed by design.I remember reading a reddit comment saying that if d were to import c header files directly without any 3rd party libraries it would be a game changer. The question is: how difficult to implement a C AST parser for the dmd front end? -AlexI've been doing this in both of my compiled languages. (fcc first, built-in, and now in cx as a macro.) For an example of it in action, check out my glfw sample https://github.com/FeepingCreature/Cx/blob/master/demos/glfw.cx , or the macro code for it https://github.com/FeepingCreature/Cx/blob/master/src/cx/macros/cimport.cx . ... I can wholeheartedly recommend any language do this. It unlocks a lot of value and removes a lot of manual work.
Apr 19 2021
On Monday, 19 April 2021 at 09:09:21 UTC, evilrat wrote:On Monday, 19 April 2021 at 06:51:54 UTC, FeepingCreature wrote:Yeah I think it differs between using it for system headers and using it for library headers, that are probably going to be relatively standardized across distributions and compiler versions. That said, at least the p much worst thing that can happen is a compiler error. With manual headers, you can end up on a weird system where the layout of a header struct is different, or a function is implemented as a header macro that calls a different function, and then you get linker errors or miscompilation. If anything, the headers are probably closer to the "source of truth" about the API.I can wholeheartedly recommend any language do this. It unlocks a lot of value and removes a lot of manual work.Yeah, just until it fails to convert something and blocks compilation, where no manual fixes is possible or not even allowed by design.
Apr 19 2021