From 66bf4bfe0c44bea2d2677b5da81849ca31e07357 Mon Sep 17 00:00:00 2001 From: ulic-youthlic Date: Mon, 28 Apr 2025 13:58:27 +0800 Subject: [PATCH] use divided module --- .clangd | 1 + .helix/languages.toml | 28 ++++++++++++++++++++++ Makefile | 30 ++++++++++++++++------- builder.cxxm | 56 +++++++++++++++++++++++++++++++++++++++++++ main.cxx | 56 +------------------------------------------ 5 files changed, 108 insertions(+), 63 deletions(-) create mode 100644 .helix/languages.toml create mode 100644 builder.cxxm diff --git a/.clangd b/.clangd index d839602..24663fb 100644 --- a/.clangd +++ b/.clangd @@ -1,5 +1,6 @@ CompileFlags: Add: + - -std=c++26 - -Xclang - -fno-validate-pch - -fretain-comments-from-system-headers diff --git a/.helix/languages.toml b/.helix/languages.toml new file mode 100644 index 0000000..a85688c --- /dev/null +++ b/.helix/languages.toml @@ -0,0 +1,28 @@ +[[language]] +name = "cpp" +file-types = [ + "cc", + "hh", + "c++", + "cpp", + "hpp", + "h", + "ipp", + "tpp", + "cxx", + "hxx", + "ixx", + "txx", + "ino", + "C", + "H", + "cu", + "cuh", + "cppm", + "cxxm", + "h++", + "ii", + "inl", + { glob = ".hpp.in" }, + { glob = ".h.in" }, +] diff --git a/Makefile b/Makefile index dfd2d4e..f389692 100644 --- a/Makefile +++ b/Makefile @@ -1,24 +1,38 @@ .RECIPEPREFIX = > .DEFAULT_GOAL = build/main.out CXX = clang++ -CXXFLAGS += -std=c++26 -Wno-experimental-header-units +CXXFLAGS += -std=c++26 -Wno-experimental-header-units -Wno-unused-command-line-argument LDFLAGS += -std=c++26 SYSTEMHEADER_FLAGS = -Wno-deprecated-builtins -Wno-pragma-system-header-outside-header -Wno-keyword-compat -build_module_path = $(let first rest,$1,$(if $(rest),-fmodule-file=$(first) $(call build_module_path,$(rest)),-fmodule-file=$(first))) +module_pair = $(if $(findstring user,$1),$(patsubst build/user/%.pcm,%=,$1))$1 +build_module_path = $(if $1,$(let first rest,$1,$(if $(rest),-fmodule-file=$(call module_pair,$(first)) $(call build_module_path,$(rest)),-fmodule-file=$(call module_pair,$(first))))) +object_build = $(filter-out %.pcm,$^) $(call build_module_path,$(filter %.pcm,$^)) +autodir = @mkdir -p $(@D) -build/main.out: build/main.o +build/main.out: build/main.o build/builder.o +> $(call autodir) > $(CXX) $(LDFLAGS) $^ -o $@ -build/main.o: main.cxx build/print.pcm build/type_traits.pcm -> $(CXX) $(CXXFLAGS) -c $(filter-out %.pcm,$^) $(call build_module_path,$(filter %.pcm,$^)) +build/main.o:: main.cxx build/system/print.pcm build/user/builder.pcm +build/builder.o:: builder.cxxm build/system/type_traits.pcm +build/user/builder.pcm:: build/system/type_traits.pcm + +build/%.o:: +> $(call autodir) +> $(CXX) $(CXXFLAGS) -c $(call object_build) > @mv $(patsubst build/%,%,$@) $@ -build/%.pcm: -> $(CXX) $(CXXFLAGS) -xc++-system-header --precompile $(patsubst build/%.pcm,%,$@) -o $@ $(SYSTEMHEADER_FLAGS) +build/user/%.pcm:: %.cxxm +> $(call autodir) +> $(CXX) $(CXXFLAGS) --precompile -o $@ $(call object_build) + +build/system/%.pcm: +> $(call autodir) +> $(CXX) $(CXXFLAGS) -xc++-system-header --precompile $(patsubst build/system/%.pcm,%,$@) -o $@ $(SYSTEMHEADER_FLAGS) .PHONY: clean run clean: -> @-rm -rf ./build/* +> @-rm -rf ./build bear: clean > bear -- $(MAKE) diff --git a/builder.cxxm b/builder.cxxm new file mode 100644 index 0000000..7d91a94 --- /dev/null +++ b/builder.cxxm @@ -0,0 +1,56 @@ +export module builder; +import ; + +export template +struct Builder; +template +struct BuildMixin {}; + +template <> +struct BuildMixin { + auto build(this auto&& self) + requires requires { self.p1 + self.p2; } + { + return self.p1 + self.p2; + } +}; + +template +struct Param1Mixin {}; + +template <> +struct Param1Mixin { + auto fill_param1(this auto&& self) + requires requires { self.p1; } + { + self.p1 = 1; + return Builder::param2_v>( + self); + } +}; + +template +struct Param2Mixin {}; + +template <> +struct Param2Mixin { + auto fill_param2(this auto&& self) + requires requires { self.p2; } + { + self.p2 = 1; + return Builder::param1_v, true>( + self); + } +}; + +template +struct Builder : BuildMixin, + Param1Mixin, + Param2Mixin { + const constinit static auto param1_v = param1; + const constinit static auto param2_v = param2; + int p1; + int p2; + Builder() : p1(0), p2(0) {} + Builder(auto&& other) : p1(other.p1), p2(other.p2) {} +}; diff --git a/main.cxx b/main.cxx index 540e1f8..b87f7c4 100644 --- a/main.cxx +++ b/main.cxx @@ -1,59 +1,5 @@ import ; -import ; - -template -struct Builder; -template -struct BuildMixin {}; - -template <> -struct BuildMixin { - auto build(this auto&& self) - requires requires { self.p1 + self.p2; } - { - return self.p1 + self.p2; - } -}; - -template -struct Param1Mixin {}; - -template <> -struct Param1Mixin { - auto fill_param1(this auto&& self) - requires requires { self.p1; } - { - self.p1 = 1; - return Builder::param2_v>( - self); - } -}; - -template -struct Param2Mixin {}; - -template <> -struct Param2Mixin { - auto fill_param2(this auto&& self) - requires requires { self.p2; } - { - self.p2 = 1; - return Builder::param1_v, true>( - self); - } -}; - -template -struct Builder : BuildMixin, - Param1Mixin, - Param2Mixin { - const constinit static auto param1_v = param1; - const constinit static auto param2_v = param2; - int p1; - int p2; - Builder() : p1(0), p2(0) {} - Builder(auto&& other) : p1(other.p1), p2(other.p2) {} -}; +import builder; auto main() -> int { Builder builder;