FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

[SUGGESTION] Add inspect alternatives for literals by filipsajdak · Pull Request #79 · hsutter/cppfront · GitHub

Repository navigation

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .cpp  (1) .h  (2) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
88 changes: 88 additions & 0 deletions include/cpp2util.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,94 @@ auto is( X const& x ) -> bool {
return x == X();
}

//-------------------------------------------------------------------------------------------------------------
// Built-in is (literals)
//

template< auto value, typename X, typename V = CPP2_TYPEOF(value)>
// This requires are needed for gcc compiler to avoid ambiguity when value is double
requires (
(std::floating_point<V> && !std::floating_point<X>) ||
(std::integral<V> && !std::integral<X>) ||
(std::is_enum_v<V> && !std::is_same_v<V, X> )
)
constexpr auto is( X const& x ) -> bool {
return false;
}

template< auto value, typename X >
requires (std::is_enum_v<CPP2_TYPEOF(value)> && std::is_same_v<X,CPP2_TYPEOF(value)>)
constexpr auto is( X const& x ) -> bool {
return x == value;
}

template< auto value, typename X >
requires std::integral<CPP2_TYPEOF(value)> && std::integral<X>
constexpr auto is( X const& x ) -> bool {
return x == value;
}

// Workaroud for lack of support for floating point template argument by clang and MSVC
struct double_wrapper {
double value = {};

template <typename T>
requires std::is_floating_point_v<T>
constexpr double_wrapper(T d) : value(d) {}

constexpr operator double() const {
return value;
}

template <typename T>
constexpr bool operator==(T rhs) const {
return value == rhs;
}
};

// This is needed to solve some issue with clang - probably clang bug.
// Without it clang is not able to match with default specialisation:
// template< auto value, typename X > auto is( X const& x ) -> bool;
#if defined(__clang__)

template< double_wrapper value, typename X >
constexpr auto is( X const& x ) -> bool {
if constexpr (std::is_floating_point_v<X>)
return x == value;
else
return false;
}

#else

template< double_wrapper value, typename X >
requires std::is_floating_point_v<X>
constexpr auto is( X const& x ) -> bool {
return x == value;
}

#endif

template <auto N>
struct cstring_wrapper {
char cs[N];

constexpr cstring_wrapper(const char (&s)[N]) noexcept {
std::copy(s, s+N, cs);
}

constexpr bool operator==(const std::string_view& sv) const noexcept {
return std::equal(cs, cs+N-1, sv.begin(), sv.end()); // N-1 as sv is not null-terminated
}
};

template< cstring_wrapper value, typename X >
constexpr auto is( X const& x ) -> bool {
if constexpr (std::is_convertible_v<X, std::string_view>)
return value == x;
else
return false;
}

//-------------------------------------------------------------------------------------------------------------
// Built-in as (partial)
Expand Down
7 changes: 5 additions & 2 deletions source/cppfront.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -1282,8 +1282,11 @@ class cppfront

auto id = std::string{};
printer.emit_to_string(&id);
assert(alt->id_expression);
emit(*alt->id_expression);
assert(alt->id_expression || alt->literal);
if (alt->id_expression)
emit(*alt->id_expression);
else if (alt->literal)
emit(*alt->literal);
printer.emit_to_string();

assert (*alt->is_as_keyword == "is" || *alt->is_as_keyword == "as");
Expand Down
14 changes: 11 additions & 3 deletions source/parse.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,7 @@ struct alternative_node
std::unique_ptr<id_expression_node> id_expression;
source_position equal_sign;
std::unique_ptr<statement_node> statement;
token const* literal;

auto position() const -> source_position
{
Expand Down Expand Up @@ -776,8 +777,11 @@ auto alternative_node::visit(auto& v, int depth) -> void
}
assert (is_as_keyword);
v.start(*is_as_keyword, depth+1);
assert (id_expression);
id_expression->visit(v, depth+1);
assert (id_expression || literal);
if (id_expression)
id_expression->visit(v, depth+1);
else if (literal)
literal->visit(v, depth+1);
assert (statement);
statement->visit(v, depth+1);
v.end(*this, depth);
Expand Down Expand Up @@ -2346,8 +2350,12 @@ class parser
if (auto id = id_expression()) {
n->id_expression = std::move(id);
}
else if (is_literal(curr().type())) {
n->literal = &curr();
next();
}
else {
error("expected id-expression after 'is' in inspect alternative");
error("expected id-expression or literal after 'is' in inspect alternative");
return {};
}

Expand Down

Back | FazBrowse Home | New Git URL