Skip to content

Commit 21a4d04

Browse files
committed
Added support for cpp2::-unqualified new
For `unique.new` and `shared.new` And made default `new` available globally via `cpp2_new` wartification (the only thing we've injected outside the `cpp2` namespace so far...)
1 parent 2b61ccf commit 21a4d04

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

cpp2util.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -71,20 +71,20 @@ namespace cpp2 {
7171
//
7272
struct {
7373
template<typename T, typename... Args>
74-
[[nodiscard]] auto new_(auto ...args) const -> std::unique_ptr<T> {
74+
[[nodiscard]] auto cpp2_new(auto ...args) const -> std::unique_ptr<T> {
7575
return std::make_unique<T>(CPP2_FORWARD(args)...);
7676
}
7777
} unique;
7878

7979
struct {
8080
template<typename T, typename... Args>
81-
[[nodiscard]] auto new_(auto ...args) const -> std::unique_ptr<T> {
81+
[[nodiscard]] auto cpp2_new(auto ...args) const -> std::shared_ptr<T> {
8282
return std::make_shared<T>(CPP2_FORWARD(args)...);
8383
}
8484
} shared;
8585

8686
template<typename T, typename... Args>
87-
[[nodiscard]] auto new_(auto ...args) -> std::unique_ptr<T> {
87+
[[nodiscard]] auto cpp2_new(auto ...args) -> std::unique_ptr<T> {
8888
return std::make_unique<T>(CPP2_FORWARD(args)...);
8989
}
9090

@@ -389,7 +389,9 @@ int main() {
389389
390390
*/
391391

392-
393392
}
394393

394+
using cpp2::cpp2_new;
395+
396+
395397
#endif

cppfront.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -826,7 +826,7 @@ class cppfront
826826
auto emit(token const& n) -> void
827827
{
828828
if (n == "new") {
829-
printer.print_cpp2("new_", n.position());
829+
printer.print_cpp2("cpp2_new", n.position());
830830
}
831831
else {
832832
printer.print_cpp2(n, n.position());
@@ -893,6 +893,16 @@ class cppfront
893893
//
894894
auto emit(qualified_id_node const& n) -> void
895895
{
896+
// Implicit "cpp2::" qualification of "unique.new" and "shared.new"
897+
if (n.ids.size() == 2 &&
898+
(*n.ids[0].id->identifier == "unique" || *n.ids[0].id->identifier == "shared") &&
899+
*n.ids[1].scope_op == "." &&
900+
*n.ids[1].id->identifier == "new"
901+
)
902+
{
903+
printer.print_cpp2("cpp2::", n.position());
904+
}
905+
896906
auto ident = std::string{};
897907
printer.emit_to_string(&ident);
898908

0 commit comments

Comments
 (0)