Skip to content
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

feat: allow authoring module units #569

Closed
wants to merge 6 commits into from

Conversation

JohelEGP
Copy link
Contributor

@JohelEGP JohelEGP commented Aug 3, 2023

Partially addresses #269.
Resolves #1190.

  • Emit the module directive, global module fragment, and module declaration at the top.
  • Allow exporting declarations.

The above means that module units aren't affected by #470.

I started including an expensive header.
So I isolated the problem to a module.
And timeliness was restored.

Test it on a recent Clang version.

hello.cpp2:

export module hello;
export hello: () -> std::string = "Hello";

main.cpp2:

#include <iostream>
import hello;
t: @struct type = {}
main: () = std::cout << hello() << ", modules!\n";

Commands:

cppfront hello.cpp2
cppfront main.cpp2
clang++ -std=c++20 -stdlib=libc++ -fmodules -fbuiltin-module-map -fmodule-output=hello.pcm -I /path/to/cpp2util/ -c -x c++-module hello.cpp
clang++ -std=c++20 -stdlib=libc++ -fmodules -fbuiltin-module-map -fprebuilt-module-path=. -I /path/to/cpp2util/ main.cpp hello.o
./a.out
Hello, modules!

hello.cpp

module;

#include "cpp2util.h"

export module hello;

//=== Cpp2 type declarations ====================================================



//=== Cpp2 type definitions and function declarations ===========================


#line 2 "hello.cpp2"
export [[nodiscard]] auto hello() -> std::string;


//=== Cpp2 function definitions =================================================


#line 2 "hello.cpp2"
[[nodiscard]] auto hello() -> std::string { return "Hello";  }

main.cpp

#include "cpp2util.h"


//=== Cpp2 type declarations ====================================================


#line 3 "main.cpp2"
class t;


//=== Cpp2 type definitions and function declarations ===========================

#include <iostream>
import hello;
#line 3 "main.cpp2"
class t {};
auto main() -> int;


//=== Cpp2 function definitions =================================================


#line 4 "main.cpp2"
auto main() -> int { std::cout << hello() << ", modules!\n";  }

Testing summary:

100% tests passed, 0 tests failed out of 692

Total Test time (real) =  34.15 sec

Acknowledgements:

@JohelEGP
Copy link
Contributor Author

JohelEGP commented Aug 3, 2023

Check the generated program on Compiler Explorer: https://cpp2.godbolt.org/z/csGKWqxfh.

@JohelEGP
Copy link
Contributor Author

JohelEGP commented Aug 3, 2023

On GCC: https://cpp2.godbolt.org/z/n9TPeh58Y.
I had to use std::string_view for it to work.
Opened https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110893
and https://gcc.gnu.org/bugzilla/show_bug.cgi?id=110894
for now.

@hsutter
Copy link
Owner

hsutter commented Aug 11, 2023

I appreciate this and I very much want to support authoring and consuming modules (*), but I don't think modules are ready for us yet. The support on the three major compilers is still very brittle, and I keep expecting that to change imminently but I want to keep holding off until that changes. If cppfront starts supporting modules authoring now, I fear it'll lead to lots of bug reports that will mostly be compiler bugs beyond our control, and so will be a distraction for cppfront.

So, with appreciation for the work that went into this PR, I'll still close this one for now. Sorry! I do appreciate it and I do look forward to when we can do this on all the major compilers.

(*) beyond the implicit as-if-import-std that already works in -pure-cpp2 mode, but even that has a big workaround to "fake it till [they] make it" in cpp2util.h

@hsutter hsutter closed this Aug 11, 2023
@JohelEGP
Copy link
Contributor Author

That's fair.

Let's keep watching the growth of C++ modules.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[BUG] cppfront fails on valid cpp1 syntax for importing module partitions
2 participants