Skip to content

Commit 7fd706d

Browse files
committed
Add impl subnamespace in cpp2util.h
All features that are documented and programs might use directly stay in cpp2:: (e.g., cpp2::i32) Internal language support implementation features that programs shouldn't use directly go into cpp2::impl:: (e.g., cpp2::out<T>) I tried to not destablize other PRs by keeping the contents of the file in their original order and just adding comments and namespace { ... } lines -- my hope is that this will avoid merge conflicts and need for rebasing
1 parent 82cecba commit 7fd706d

File tree

87 files changed

+889
-836
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+889
-836
lines changed

include/cpp2util.h

Lines changed: 56 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,27 @@
1515
// Cpp2 utilities:
1616
// Language support implementations
1717
// #include'd by generated Cpp1 code
18+
19+
// There are two kinds of entities in this file.
20+
//
21+
// 1) Entities in namespace cpp2:: itself, and documented at /cppfront/docs
22+
//
23+
// These are intended for programs to use directly, to the extent
24+
// described in the documentation. Using any parts not described in the
25+
// documentation is not supported.
26+
//
27+
// 2) Entities in namespace cpp2::impl::, and macros
28+
//
29+
// These should not be used by the program. They form the language
30+
// support library intended to be called only from generated Cpp2 code.
31+
//
32+
// For example, if a Cpp2 function leaves a local variable
33+
// uninitialized, cppfront will generate uses of impl::deferred_init<>
34+
// under the covers and guarantee it is constructed exactly once, so
35+
// the implementation here doesn't need to check for double construction
36+
// because it can't happen; using the name impl::deferred_init directly
37+
// from program code is not supported.
38+
//
1839
//===========================================================================
1940

2041
#ifndef CPP2_UTIL_H
@@ -267,6 +288,12 @@
267288
#endif
268289

269290

291+
//-----------------------------------------------------------------------
292+
//
293+
// Macros
294+
//
295+
//-----------------------------------------------------------------------
296+
//
270297
#define CPP2_TYPEOF(x) std::remove_cvref_t<decltype(x)>
271298
#if __cplusplus >= 202302L && \
272299
( \
@@ -354,27 +381,6 @@ template <typename T>
354381
using deref_t = decltype(*std::declval<T&>());
355382

356383

357-
//-----------------------------------------------------------------------
358-
//
359-
// String: A helper workaround for passing a string literal as a
360-
// template argument
361-
//
362-
//-----------------------------------------------------------------------
363-
//
364-
template<std::size_t N>
365-
struct String
366-
{
367-
constexpr String(const char (&str)[N])
368-
{
369-
std::copy_n(str, N, value);
370-
}
371-
372-
auto operator<=>(String const&) const = default;
373-
374-
char value[N] = {};
375-
};
376-
377-
378384
//-----------------------------------------------------------------------
379385
//
380386
// contract_group
@@ -461,6 +467,10 @@ auto inline testing = contract_group(
461467
);
462468

463469

470+
namespace impl {
471+
472+
//-----------------------------------------------------------------------
473+
//
464474
// Check for invalid dereference or indirection which would result in undefined behavior.
465475
//
466476
// - Null pointer
@@ -565,8 +575,8 @@ auto assert_in_bounds(auto&& x, auto&& arg CPP2_SOURCE_LOCATION_PARAM_WITH_DEFAU
565575
return CPP2_FORWARD(x) [ CPP2_FORWARD(arg) ];
566576
}
567577

568-
#define CPP2_ASSERT_IN_BOUNDS(x,arg) (cpp2::assert_in_bounds((x),(arg)))
569-
#define CPP2_ASSERT_IN_BOUNDS_LITERAL(x,arg) (cpp2::assert_in_bounds<(arg)>(x))
578+
#define CPP2_ASSERT_IN_BOUNDS(x,arg) (cpp2::impl::assert_in_bounds((x),(arg)))
579+
#define CPP2_ASSERT_IN_BOUNDS_LITERAL(x,arg) (cpp2::impl::assert_in_bounds<(arg)>(x))
570580

571581

572582
//-----------------------------------------------------------------------
@@ -632,6 +642,8 @@ auto Typeid( [[maybe_unused]] auto&& x ) -> decltype(auto) {
632642
#endif
633643
}
634644

645+
} // impl
646+
635647

636648
//-----------------------------------------------------------------------
637649
//
@@ -692,6 +704,9 @@ template<typename T>
692704
}
693705

694706

707+
708+
namespace impl {
709+
695710
//-----------------------------------------------------------------------
696711
//
697712
// in<T> For "in" parameter
@@ -947,6 +962,9 @@ class out {
947962
#define CPP2_UFCS_TEMPLATE_NONLOCAL(...) CPP2_UFCS_(,CPP2_UFCS_IDENTITY,(),template,__VA_ARGS__)
948963
#define CPP2_UFCS_QUALIFIED_TEMPLATE_NONLOCAL(QUALID,...) CPP2_UFCS_(,CPP2_UFCS_IDENTITY,QUALID,template,__VA_ARGS__)
949964

965+
} // impl
966+
967+
950968

951969
//-----------------------------------------------------------------------
952970
//
@@ -1079,6 +1097,8 @@ inline auto to_string(auto&& value, std::string_view) -> std::string
10791097
#endif
10801098

10811099

1100+
namespace impl {
1101+
10821102
//-----------------------------------------------------------------------
10831103
//
10841104
// is and as
@@ -1624,6 +1644,9 @@ constexpr auto as( X const& x ) -> decltype(auto)
16241644
{ return x.value(); }
16251645

16261646

1647+
} // impl
1648+
1649+
16271650
//-----------------------------------------------------------------------
16281651
//
16291652
// A variation of GSL's final_action_success / finally
@@ -1739,6 +1762,8 @@ constexpr auto unsafe_narrow( X&& x ) noexcept -> decltype(auto)
17391762
}
17401763

17411764

1765+
namespace impl {
1766+
17421767
//-----------------------------------------------------------------------
17431768
//
17441769
// args: see main() arguments as a container of string_views
@@ -1800,6 +1825,8 @@ inline auto make_args(int argc, char** argv) -> args_t
18001825
return args_t{argc, argv};
18011826
}
18021827

1828+
} // impl
1829+
18031830

18041831
//-----------------------------------------------------------------------
18051832
//
@@ -1877,7 +1904,7 @@ inline auto fopen( const char* filename, const char* mode ) {
18771904
#endif
18781905

18791906
if (!x) {
1880-
Throw( std::make_error_condition(std::errc::no_such_file_or_directory), "'fopen' attempt failed");
1907+
impl::Throw(std::make_error_condition(std::errc::no_such_file_or_directory), "'fopen' attempt failed");
18811908
}
18821909
return c_raii( x, &std::fclose );
18831910
}
@@ -1893,6 +1920,9 @@ inline auto fopen( const char* filename, const char* mode ) {
18931920
// with cpp2::fopen as a starting example.
18941921

18951922

1923+
1924+
namespace impl {
1925+
18961926
//-----------------------------------------------------------------------
18971927
//
18981928
// Signed/unsigned comparison checks
@@ -2056,6 +2086,8 @@ inline constexpr auto as_() -> decltype(auto)
20562086
return as<C,x>();
20572087
}
20582088

2089+
} // impl
2090+
20592091

20602092
}
20612093

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,118 +1,118 @@
11
mixed-bugfix-for-ufcs-non-local.cpp2:13:12: error: a lambda expression cannot appear in this context
22
template<t<CPP2_UFCS_NONLOCAL(f)(o)> UnnamedTypeParam1_1> bool inline constexpr v0 = false;// Fails on GCC ([GCC109781][]) and Clang 12 (a lambda expression cannot appear in this context)
33
^
4-
../../../include/cpp2util.h:946:59: note: expanded from macro 'CPP2_UFCS_NONLOCAL'
4+
../../../include/cpp2util.h:961:59: note: expanded from macro 'CPP2_UFCS_NONLOCAL'
55
#define CPP2_UFCS_NONLOCAL(...) CPP2_UFCS_(,CPP2_UFCS_IDENTITY,(),,__VA_ARGS__)
66
^
7-
../../../include/cpp2util.h:925:59: note: expanded from macro 'CPP2_UFCS_'
7+
../../../include/cpp2util.h:940:59: note: expanded from macro 'CPP2_UFCS_'
88
#define CPP2_UFCS_(LAMBDADEFCAPT,MVFWD,QUALID,TEMPKW,...) \
99
^
1010
mixed-bugfix-for-ufcs-non-local.cpp2:15:3: error: a lambda expression cannot appear in this context
1111
t<CPP2_UFCS_NONLOCAL(f)(o)> inline constexpr v1 = t<true>();// Fails on Clang 12 (lambda in unevaluated context).
1212
^
13-
../../../include/cpp2util.h:946:59: note: expanded from macro 'CPP2_UFCS_NONLOCAL'
13+
../../../include/cpp2util.h:961:59: note: expanded from macro 'CPP2_UFCS_NONLOCAL'
1414
#define CPP2_UFCS_NONLOCAL(...) CPP2_UFCS_(,CPP2_UFCS_IDENTITY,(),,__VA_ARGS__)
1515
^
16-
../../../include/cpp2util.h:925:59: note: expanded from macro 'CPP2_UFCS_'
16+
../../../include/cpp2util.h:940:59: note: expanded from macro 'CPP2_UFCS_'
1717
#define CPP2_UFCS_(LAMBDADEFCAPT,MVFWD,QUALID,TEMPKW,...) \
1818
^
1919
mixed-bugfix-for-ufcs-non-local.cpp2:21:12: error: a lambda expression cannot appear in this context
2020
template<t<CPP2_UFCS_NONLOCAL(f)(o)> UnnamedTypeParam1_2> auto g() -> void;
2121
^
22-
../../../include/cpp2util.h:946:59: note: expanded from macro 'CPP2_UFCS_NONLOCAL'
22+
../../../include/cpp2util.h:961:59: note: expanded from macro 'CPP2_UFCS_NONLOCAL'
2323
#define CPP2_UFCS_NONLOCAL(...) CPP2_UFCS_(,CPP2_UFCS_IDENTITY,(),,__VA_ARGS__)
2424
^
25-
../../../include/cpp2util.h:925:59: note: expanded from macro 'CPP2_UFCS_'
25+
../../../include/cpp2util.h:940:59: note: expanded from macro 'CPP2_UFCS_'
2626
#define CPP2_UFCS_(LAMBDADEFCAPT,MVFWD,QUALID,TEMPKW,...) \
2727
^
28-
mixed-bugfix-for-ufcs-non-local.cpp2:23:36: error: a lambda expression cannot appear in this context
29-
auto g([[maybe_unused]] cpp2::in<t<CPP2_UFCS_NONLOCAL(f)(o)>> unnamed_param_1) -> void;
30-
^
31-
../../../include/cpp2util.h:946:59: note: expanded from macro 'CPP2_UFCS_NONLOCAL'
28+
mixed-bugfix-for-ufcs-non-local.cpp2:23:42: error: a lambda expression cannot appear in this context
29+
auto g([[maybe_unused]] cpp2::impl::in<t<CPP2_UFCS_NONLOCAL(f)(o)>> unnamed_param_1) -> void;
30+
^
31+
../../../include/cpp2util.h:961:59: note: expanded from macro 'CPP2_UFCS_NONLOCAL'
3232
#define CPP2_UFCS_NONLOCAL(...) CPP2_UFCS_(,CPP2_UFCS_IDENTITY,(),,__VA_ARGS__)
3333
^
34-
../../../include/cpp2util.h:925:59: note: expanded from macro 'CPP2_UFCS_'
34+
../../../include/cpp2util.h:940:59: note: expanded from macro 'CPP2_UFCS_'
3535
#define CPP2_UFCS_(LAMBDADEFCAPT,MVFWD,QUALID,TEMPKW,...) \
3636
^
3737
mixed-bugfix-for-ufcs-non-local.cpp2:27:29: error: a lambda expression cannot appear in this context
3838
[[nodiscard]] auto h() -> t<CPP2_UFCS_NONLOCAL(f)(o)>;
3939
^
40-
../../../include/cpp2util.h:946:59: note: expanded from macro 'CPP2_UFCS_NONLOCAL'
40+
../../../include/cpp2util.h:961:59: note: expanded from macro 'CPP2_UFCS_NONLOCAL'
4141
#define CPP2_UFCS_NONLOCAL(...) CPP2_UFCS_(,CPP2_UFCS_IDENTITY,(),,__VA_ARGS__)
4242
^
43-
../../../include/cpp2util.h:925:59: note: expanded from macro 'CPP2_UFCS_'
43+
../../../include/cpp2util.h:940:59: note: expanded from macro 'CPP2_UFCS_'
4444
#define CPP2_UFCS_(LAMBDADEFCAPT,MVFWD,QUALID,TEMPKW,...) \
4545
^
4646
mixed-bugfix-for-ufcs-non-local.cpp2:31:12: error: a lambda expression cannot appear in this context
4747
template<t<CPP2_UFCS_NONLOCAL(f)(o)> UnnamedTypeParam1_3> using a = bool;// Fails on GCC ([GCC109781][]) and Clang 12 (a lambda expression cannot appear in this context)
4848
^
49-
../../../include/cpp2util.h:946:59: note: expanded from macro 'CPP2_UFCS_NONLOCAL'
49+
../../../include/cpp2util.h:961:59: note: expanded from macro 'CPP2_UFCS_NONLOCAL'
5050
#define CPP2_UFCS_NONLOCAL(...) CPP2_UFCS_(,CPP2_UFCS_IDENTITY,(),,__VA_ARGS__)
5151
^
52-
../../../include/cpp2util.h:925:59: note: expanded from macro 'CPP2_UFCS_'
52+
../../../include/cpp2util.h:940:59: note: expanded from macro 'CPP2_UFCS_'
5353
#define CPP2_UFCS_(LAMBDADEFCAPT,MVFWD,QUALID,TEMPKW,...) \
5454
^
5555
mixed-bugfix-for-ufcs-non-local.cpp2:33:12: error: a lambda expression cannot appear in this context
5656
template<t<CPP2_UFCS_NONLOCAL(f)(o)> UnnamedTypeParam1_4> auto inline constexpr b = false;// Fails on GCC ([GCC109781][]).
5757
^
58-
../../../include/cpp2util.h:946:59: note: expanded from macro 'CPP2_UFCS_NONLOCAL'
58+
../../../include/cpp2util.h:961:59: note: expanded from macro 'CPP2_UFCS_NONLOCAL'
5959
#define CPP2_UFCS_NONLOCAL(...) CPP2_UFCS_(,CPP2_UFCS_IDENTITY,(),,__VA_ARGS__)
6060
^
61-
../../../include/cpp2util.h:925:59: note: expanded from macro 'CPP2_UFCS_'
61+
../../../include/cpp2util.h:940:59: note: expanded from macro 'CPP2_UFCS_'
6262
#define CPP2_UFCS_(LAMBDADEFCAPT,MVFWD,QUALID,TEMPKW,...) \
6363
^
6464
mixed-bugfix-for-ufcs-non-local.cpp2:35:13: error: a lambda expression cannot appear in this context
6565
using c = t<CPP2_UFCS_NONLOCAL(f)(o)>;// Fails on Clang 12 (lambda in unevaluated context) and Clang 12 (a lambda expression cannot appear in this context)
6666
^
67-
../../../include/cpp2util.h:946:59: note: expanded from macro 'CPP2_UFCS_NONLOCAL'
67+
../../../include/cpp2util.h:961:59: note: expanded from macro 'CPP2_UFCS_NONLOCAL'
6868
#define CPP2_UFCS_NONLOCAL(...) CPP2_UFCS_(,CPP2_UFCS_IDENTITY,(),,__VA_ARGS__)
6969
^
70-
../../../include/cpp2util.h:925:59: note: expanded from macro 'CPP2_UFCS_'
70+
../../../include/cpp2util.h:940:59: note: expanded from macro 'CPP2_UFCS_'
7171
#define CPP2_UFCS_(LAMBDADEFCAPT,MVFWD,QUALID,TEMPKW,...) \
7272
^
7373
mixed-bugfix-for-ufcs-non-local.cpp2:37:29: error: a lambda expression cannot appear in this context
7474
auto inline constexpr d = t<CPP2_UFCS_NONLOCAL(f)(o)>();// Fails on Clang 12 (lambda in unevaluated context).
7575
^
76-
../../../include/cpp2util.h:946:59: note: expanded from macro 'CPP2_UFCS_NONLOCAL'
76+
../../../include/cpp2util.h:961:59: note: expanded from macro 'CPP2_UFCS_NONLOCAL'
7777
#define CPP2_UFCS_NONLOCAL(...) CPP2_UFCS_(,CPP2_UFCS_IDENTITY,(),,__VA_ARGS__)
7878
^
79-
../../../include/cpp2util.h:925:59: note: expanded from macro 'CPP2_UFCS_'
79+
../../../include/cpp2util.h:940:59: note: expanded from macro 'CPP2_UFCS_'
8080
#define CPP2_UFCS_(LAMBDADEFCAPT,MVFWD,QUALID,TEMPKW,...) \
8181
^
8282
mixed-bugfix-for-ufcs-non-local.cpp2:21:12: error: a lambda expression cannot appear in this context
8383
template<t<CPP2_UFCS_NONLOCAL(f)(o)> UnnamedTypeParam1_2> auto g() -> void{}// Fails on GCC ([GCC109781][]) and Clang 12 (a lambda expression cannot appear in this context)
8484
^
85-
../../../include/cpp2util.h:946:59: note: expanded from macro 'CPP2_UFCS_NONLOCAL'
85+
../../../include/cpp2util.h:961:59: note: expanded from macro 'CPP2_UFCS_NONLOCAL'
8686
#define CPP2_UFCS_NONLOCAL(...) CPP2_UFCS_(,CPP2_UFCS_IDENTITY,(),,__VA_ARGS__)
8787
^
88-
../../../include/cpp2util.h:925:59: note: expanded from macro 'CPP2_UFCS_'
88+
../../../include/cpp2util.h:940:59: note: expanded from macro 'CPP2_UFCS_'
8989
#define CPP2_UFCS_(LAMBDADEFCAPT,MVFWD,QUALID,TEMPKW,...) \
9090
^
91-
mixed-bugfix-for-ufcs-non-local.cpp2:23:36: error: a lambda expression cannot appear in this context
92-
auto g([[maybe_unused]] cpp2::in<t<CPP2_UFCS_NONLOCAL(f)(o)>> unnamed_param_1) -> void{}// Fails on Clang 12 (lambda in unevaluated context).
93-
^
94-
../../../include/cpp2util.h:946:59: note: expanded from macro 'CPP2_UFCS_NONLOCAL'
91+
mixed-bugfix-for-ufcs-non-local.cpp2:23:42: error: a lambda expression cannot appear in this context
92+
auto g([[maybe_unused]] cpp2::impl::in<t<CPP2_UFCS_NONLOCAL(f)(o)>> unnamed_param_1) -> void{}// Fails on Clang 12 (lambda in unevaluated context).
93+
^
94+
../../../include/cpp2util.h:961:59: note: expanded from macro 'CPP2_UFCS_NONLOCAL'
9595
#define CPP2_UFCS_NONLOCAL(...) CPP2_UFCS_(,CPP2_UFCS_IDENTITY,(),,__VA_ARGS__)
9696
^
97-
../../../include/cpp2util.h:925:59: note: expanded from macro 'CPP2_UFCS_'
97+
../../../include/cpp2util.h:940:59: note: expanded from macro 'CPP2_UFCS_'
9898
#define CPP2_UFCS_(LAMBDADEFCAPT,MVFWD,QUALID,TEMPKW,...) \
9999
^
100100
mixed-bugfix-for-ufcs-non-local.cpp2:27:29: error: a lambda expression cannot appear in this context
101101
[[nodiscard]] auto h() -> t<CPP2_UFCS_NONLOCAL(f)(o)> { return o; }// Fails on Clang 12 (lambda in unevaluated context).
102102
^
103-
../../../include/cpp2util.h:946:59: note: expanded from macro 'CPP2_UFCS_NONLOCAL'
103+
../../../include/cpp2util.h:961:59: note: expanded from macro 'CPP2_UFCS_NONLOCAL'
104104
#define CPP2_UFCS_NONLOCAL(...) CPP2_UFCS_(,CPP2_UFCS_IDENTITY,(),,__VA_ARGS__)
105105
^
106-
../../../include/cpp2util.h:925:59: note: expanded from macro 'CPP2_UFCS_'
106+
../../../include/cpp2util.h:940:59: note: expanded from macro 'CPP2_UFCS_'
107107
#define CPP2_UFCS_(LAMBDADEFCAPT,MVFWD,QUALID,TEMPKW,...) \
108108
^
109-
mixed-bugfix-for-ufcs-non-local.cpp2:41:79: error: lambda expression in an unevaluated operand
110-
inline CPP2_CONSTEXPR bool u::c = [](cpp2::in<std::type_identity_t<decltype(CPP2_UFCS_NONLOCAL(f)(o))>> x) mutable -> auto { return x; }(true);// Fails on Clang 12 (lambda in unevaluated context).
111-
^
112-
../../../include/cpp2util.h:946:59: note: expanded from macro 'CPP2_UFCS_NONLOCAL'
109+
mixed-bugfix-for-ufcs-non-local.cpp2:41:85: error: lambda expression in an unevaluated operand
110+
inline CPP2_CONSTEXPR bool u::c = [](cpp2::impl::in<std::type_identity_t<decltype(CPP2_UFCS_NONLOCAL(f)(o))>> x) mutable -> auto { return x; }(true);// Fails on Clang 12 (lambda in unevaluated context).
111+
^
112+
../../../include/cpp2util.h:961:59: note: expanded from macro 'CPP2_UFCS_NONLOCAL'
113113
#define CPP2_UFCS_NONLOCAL(...) CPP2_UFCS_(,CPP2_UFCS_IDENTITY,(),,__VA_ARGS__)
114114
^
115-
../../../include/cpp2util.h:925:59: note: expanded from macro 'CPP2_UFCS_'
115+
../../../include/cpp2util.h:940:59: note: expanded from macro 'CPP2_UFCS_'
116116
#define CPP2_UFCS_(LAMBDADEFCAPT,MVFWD,QUALID,TEMPKW,...) \
117117
^
118118
13 errors generated.

regression-tests/test-results/clang-12/pure2-assert-expected-not-null.cpp.output

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ pure2-assert-expected-not-null.cpp2:7:10: error: no member named 'expected' in n
88
/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/exception:92:8: note: 'unexpected' declared here
99
void unexpected() __attribute__ ((__noreturn__));
1010
^
11-
pure2-assert-expected-not-null.cpp2:9:165: error: use of undeclared identifier 'ex'
12-
return *cpp2::assert_not_null(std::move(up)) + *cpp2::assert_not_null(std::move(sp)) + *cpp2::assert_not_null(std::move(op)) + *cpp2::assert_not_null(std::move(ex));
13-
^
11+
pure2-assert-expected-not-null.cpp2:9:189: error: use of undeclared identifier 'ex'
12+
return *cpp2::impl::assert_not_null(std::move(up)) + *cpp2::impl::assert_not_null(std::move(sp)) + *cpp2::impl::assert_not_null(std::move(op)) + *cpp2::impl::assert_not_null(std::move(ex));
13+
^
1414
pure2-assert-expected-not-null.cpp2:14:22: error: expected '(' for function-style cast or type construction
1515
std::expected<int,bool> ex {std::unexpected(false)};
1616
~~~^
@@ -21,7 +21,7 @@ pure2-assert-expected-not-null.cpp2:14:10: error: no member named 'expected' in
2121
/usr/bin/../lib/gcc/x86_64-linux-gnu/10/../../../../include/c++/10/exception:92:8: note: 'unexpected' declared here
2222
void unexpected() __attribute__ ((__noreturn__));
2323
^
24-
pure2-assert-expected-not-null.cpp2:15:45: error: use of undeclared identifier 'ex'
25-
return *cpp2::assert_not_null(std::move(ex));
26-
^
24+
pure2-assert-expected-not-null.cpp2:15:51: error: use of undeclared identifier 'ex'
25+
return *cpp2::impl::assert_not_null(std::move(ex));
26+
^
2727
6 errors generated.

0 commit comments

Comments
 (0)