Skip to content

Auto register metafunctions (POC) #2

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

Draft
wants to merge 82 commits into
base: program-defined_metafunctions_v2
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
82 commits
Select commit Hold shift + click to select a range
568294b
feat: load program-defined metafunctions (based on #797)
JohelEGP Nov 1, 2023
9bd77f0
docs(reflect): document the protocol for `load_metafunction`
JohelEGP Dec 27, 2023
845c35b
refactor(reflect): make `vposition` `private` according to GotW18
JohelEGP Dec 27, 2023
aac721c
docs(reflect): clarify lookup process
JohelEGP Dec 27, 2023
b4a7727
refactor(reflect): make `compiler_services_data` an `struct`
JohelEGP Dec 27, 2023
fbdbaba
remove Boost.DLL dependency by doing it ourselves
DyXel Dec 27, 2023
bc363e5
Don't pass string_views to functions expecting c strings
edo9300 Dec 27, 2023
32063ad
Cleanup windows specific code and restore mingw compatibility
edo9300 Dec 27, 2023
bf0b2f3
make dll unmovable as well
DyXel Dec 27, 2023
6e6e4a0
Add 2 macros to mark functions to be exported
DyXel Dec 27, 2023
c956096
Add ad-hoc error reporting for DDL load/symbol search
DyXel Dec 27, 2023
066a051
fix(reflect): guard clean up
DyXel Dec 27, 2023
c6217f9
Merge pull request #1 from DyXel/program-defined-metafunctions-no-boo…
JohelEGP Dec 27, 2023
cb1a3ed
fix(reflect): cast argument to correct type
JohelEGP Dec 27, 2023
cdb0019
docs: reapply updated comments
JohelEGP Dec 28, 2023
2ee7301
refactor(reflect): follow cppfront conventions
JohelEGP Dec 28, 2023
1bc6506
refactor(reflect): integrate error reporting
JohelEGP Dec 28, 2023
2a4baf0
refactor(reflect): include missing header `<memory>`
JohelEGP Dec 28, 2023
de82a98
refactor(reflect): move files around for easier to parse diff
JohelEGP Dec 28, 2023
0ddac5f
refactor: move `source_position` to `reflect.h2`
JohelEGP Dec 28, 2023
a98be86
refactor(reflect): avoid `std::any` for a pointer
JohelEGP Dec 28, 2023
c3fa988
refactor: move `passing_style` to `reflect.h2`
JohelEGP Dec 28, 2023
0dd27c9
refactor: adjust whitespace
JohelEGP Dec 28, 2023
96ac894
test: regenerate test results
JohelEGP Dec 28, 2023
7107644
feat: add `@visible`, the first function metafunction
JohelEGP Dec 28, 2023
cfc98f8
feat(reflect): overload `@visible` for types
JohelEGP Dec 28, 2023
01e61a0
fix(reflect): make symbols visible
JohelEGP Dec 28, 2023
7f3b2bf
fix(to_cpp1): emit `CPPFRONTAPI` on declaration for VS
JohelEGP Dec 28, 2023
241b730
refactor(reflect): remove `void` from empty parameter list
JohelEGP Dec 28, 2023
d5faac8
refactor: move `source_order_name_lookup` to `parse.h`
JohelEGP Dec 29, 2023
8108e54
refactor: do source order name lookup during parse
JohelEGP Dec 29, 2023
80deaf5
refactor(reflect): remove redundant _access-specifier_
JohelEGP Dec 29, 2023
d5d163d
refactor(reflect)!: start doing name lookup
JohelEGP Dec 30, 2023
eada211
refactor(reflect): avoid parenthesized aggregate initialization
JohelEGP Dec 30, 2023
bc32f97
refactor: return diagnostics with `expected`
JohelEGP Dec 30, 2023
9c520c4
refactor(reflect): use list initialization
JohelEGP Dec 30, 2023
958ae5d
refactor(reflect): change return type for Windows CI
JohelEGP Dec 30, 2023
9816d1b
refactor(reflect): avoid parenthesized aggregate initialization
JohelEGP Dec 30, 2023
22496c9
feat: try to check that the lookup by Cpp1 and cppfront match
JohelEGP Dec 30, 2023
1597351
feat: ensure that the lookup by Cpp1 and cppfront match
JohelEGP Dec 30, 2023
3adbc28
fix(sema): add check on successful lookup
JohelEGP Dec 31, 2023
e0022fd
fix: give metafunction symbols accessor a unique name
JohelEGP Dec 31, 2023
c229145
fix(common): check for underbar before dropping
JohelEGP Dec 31, 2023
976ea13
fix(to_cpp1): emit metafunction symbol in phase 1
JohelEGP Dec 31, 2023
0bf9eb3
fix(to_cpp1): emit metafunction symbol in the global namespace
JohelEGP Dec 31, 2023
b70a261
refactor(reflect): avoid parenthesized aggregate initialization
JohelEGP Dec 31, 2023
07f6ae3
feat(sema): lookup unqualified metafunction on loadable symbols
JohelEGP Dec 31, 2023
9f7e95f
refactor(sema): avoid constructor not on Apple Clang 13
JohelEGP Dec 31, 2023
592cba0
feat(sema): lookup fully qualified symbol
JohelEGP Dec 31, 2023
900d2db
refactor(to_cpp1): emit lookup check with correct `#line` number
JohelEGP Dec 31, 2023
f832e7a
fix(sema): build scopes from current declarations
JohelEGP Jan 1, 2024
d217b23
refactor: stub local using declaration lookup support dependent on #470
JohelEGP Jan 1, 2024
bbcb73f
Revert "refactor(to_cpp1): emit lookup check with correct `#line` num…
JohelEGP Jan 1, 2024
121b60c
refactor: enable looking up using statement
JohelEGP Jan 1, 2024
8e5a7ab
refactor: restore comments removed on revert
JohelEGP Jan 1, 2024
eb653ab
feat: author metafunction in `.cpp2` by omitting sanity check
JohelEGP Jan 1, 2024
9bc97da
refactor: do not pass member as parameter
JohelEGP Jan 1, 2024
0cca39c
refactor: avoid `forward` parameter for GCC 10
JohelEGP Jan 1, 2024
9981eb8
refactor(reflect): avoid `requires` for GCC 10
JohelEGP Jan 1, 2024
ffb0fbd
refacor: add type `meta::dll_symbol`
JohelEGP Jan 2, 2024
fced43d
refactor(reflect): avoid single named return
JohelEGP Jan 2, 2024
f0398f8
fix: avoid using uninitialized member
JohelEGP Jan 2, 2024
f205262
refactor: avoid parenthesized aggregate initialization
JohelEGP Jan 2, 2024
ac4d735
ci: disable C4251
JohelEGP Jan 2, 2024
51f3984
refactor(reflect): move comment to its function
JohelEGP Jan 2, 2024
4ada5e6
ci: correctly disable C4251
JohelEGP Jan 2, 2024
769b917
ci: disable C4996
JohelEGP Jan 2, 2024
206ba6c
refactor(util): work around "unreachable code" warning
JohelEGP Jan 2, 2024
2583d41
refactor(reflect): rename `reflect.h2` to `cpp2reflect.h2`
JohelEGP Jan 2, 2024
1817e9c
refactor(reflect): rename `reflect_impl.h2` to `reflect.h2`
JohelEGP Jan 2, 2024
57df123
Revert "refactor(reflect): rename `reflect_impl.h2` to `reflect.h2`"
JohelEGP Jan 2, 2024
0df0901
Revert "refactor(reflect): rename `reflect.h2` to `cpp2reflect.h2`"
JohelEGP Jan 2, 2024
815bc1c
refactor(reflect): rename to `dll_visible`
JohelEGP Jan 2, 2024
bc8a724
refactor(reflect): rename to `this_execution::get_reachable_metafunct…
JohelEGP Jan 2, 2024
537e66a
refactor(reflect): remove unused _capture-default_
JohelEGP Jan 2, 2024
210f022
refactor(reflect): move `shared_ptr` to `dll` implementation
JohelEGP Jan 2, 2024
ace163c
refactor(reflect): update parameter name
JohelEGP Jan 2, 2024
6dbecd7
refactor(reflect): fix indentation
JohelEGP Jan 2, 2024
5b9fedc
refactor(to_cpp1): remove empty line
JohelEGP Jan 2, 2024
89308d1
refactor(sema): diagnose limitations of metafunction lookup
JohelEGP Jan 2, 2024
4daa88b
feat: allow metafunction to have an `in` parameter
JohelEGP Jan 2, 2024
2ad2db5
auto register metafunctions
DyXel Feb 19, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
833 changes: 833 additions & 0 deletions include/cpp2reflect.h

Large diffs are not rendered by default.

54 changes: 51 additions & 3 deletions include/cpp2util.h
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,15 @@
#endif


#if defined(_WIN32)
#define CPPFRONTAPI __declspec(dllexport)
#else
#define CPPFRONTAPI __attribute__ ((visibility ("default")))
#endif

#define CPP2_C_API extern "C" CPPFRONTAPI


namespace cpp2 {


Expand Down Expand Up @@ -388,8 +397,8 @@ struct String
#define CPP2_MESSAGE_PARAM char const*
#define CPP2_CONTRACT_MSG cpp2::message_to_cstr_adapter

auto message_to_cstr_adapter( CPP2_MESSAGE_PARAM msg ) -> CPP2_MESSAGE_PARAM { return msg ? msg : ""; }
auto message_to_cstr_adapter( std::string const& msg ) -> CPP2_MESSAGE_PARAM { return msg.c_str(); }
inline auto message_to_cstr_adapter( CPP2_MESSAGE_PARAM msg ) -> CPP2_MESSAGE_PARAM { return msg ? msg : ""; }
inline auto message_to_cstr_adapter( std::string const& msg ) -> CPP2_MESSAGE_PARAM { return msg.c_str(); }

class contract_group {
public:
Expand Down Expand Up @@ -1111,7 +1120,9 @@ inline constexpr auto is( auto const& x, auto&& value ) -> bool
else if constexpr (requires{ bool{x == value}; }) {
return x == value;
}
return false;
else {
return false;
}
}


Expand Down Expand Up @@ -1996,4 +2007,41 @@ using cpp2::cpp2_new;
#define CPP2_REQUIRES_(...) requires (__VA_ARGS__)
#endif

#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4251)
#endif

#include "cpp2reflect.h"

// FIXME: Should be moved to somewhere inside `cpp2reflect.h` but idk how yet.
namespace cpp2::meta {

using mf_signature_in = void(cpp2::in<type_declaration>);
using mf_signature_inout = void(type_declaration&);

struct register_function {
register_function(const char* name, mf_signature_in* f);
register_function(const char* name, mf_signature_inout* f);
~register_function(); // TODO: Remove, used for testing.
};

// FIXME: These are *internal* to cppfront, and should never be exposed to users.
using mf = std::variant<mf_signature_in*, mf_signature_inout*>;

struct record
{
std::string fully_qualified_name;
mf function;
// TODO: Extra info to give better diagnostics to the user?
// std::string where_its_defined;
// int line, column;
};

}

#ifdef _MSC_VER
#pragma warning(pop)
#endif

#endif
1 change: 1 addition & 0 deletions regression-tests/test-results/mixed-allcpp1-hello.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#line 1 "mixed-allcpp1-hello.cpp2"

// Step 1: rename .cpp to .cpp2

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#line 1 "mixed-intro-example-three-loops.cpp2"
#include <iostream>
#include <iomanip>
#include <vector>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ struct make_strings_ret { std::string a; std::string b; };
[[nodiscard]] auto make_strings() -> make_strings_ret;
#line 38 "mixed-postexpression-with-capture.cpp2"

#line 1 "mixed-postexpression-with-capture.cpp2"

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

Expand Down
1 change: 0 additions & 1 deletion regression-tests/test-results/pure2-bounds-safety-span.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
auto print_and_decorate(auto const& thing) -> void;
#line 17 "pure2-bounds-safety-span.cpp2"

#line 1 "pure2-bounds-safety-span.cpp2"

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ auto decorate_and_print(auto& thing) -> void;
[[nodiscard]] auto main() -> int;
#line 30 "pure2-intro-example-three-loops.cpp2"

#line 1 "pure2-intro-example-three-loops.cpp2"

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

Expand Down
1 change: 0 additions & 1 deletion regression-tests/test-results/pure2-stdio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
[[nodiscard]] auto main() -> int;
#line 10 "pure2-stdio.cpp2"

#line 1 "pure2-stdio.cpp2"

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

Expand Down
1 change: 0 additions & 1 deletion regression-tests/test-results/pure2-type-safety-1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ auto test_generic(auto const& x, auto const& msg) -> void;
auto print(cpp2::in<std::string> msg, cpp2::in<bool> b) -> void;
#line 35 "pure2-type-safety-1.cpp2"

#line 1 "pure2-type-safety-1.cpp2"

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

Expand Down
83 changes: 54 additions & 29 deletions source/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@


#ifdef _MSC_VER
#pragma warning(disable: 4456)
#pragma warning(disable: 4456 4996)
#endif

#include "cpp2util.h"
Expand Down Expand Up @@ -86,27 +86,10 @@ struct source_line
};


using lineno_t = int32_t;
using colno_t = int32_t; // not int16_t... encountered >80,000 char line during testing
using cpp2::meta::lineno_t;
using cpp2::meta::colno_t;

struct source_position
{
lineno_t lineno; // one-based offset into program source
colno_t colno; // one-based offset into line

source_position(lineno_t l = 1, colno_t c = 1 )
: lineno{ l }, colno{ c }
{
}

auto operator<=>(source_position const&) const = default;

auto to_string() const
-> std::string
{
return "(" + std::to_string(lineno) + "," + std::to_string(colno) + ")";
}
};
using cpp2::meta::source_position;

struct comment
{
Expand All @@ -127,7 +110,7 @@ struct string_parts {

string_parts(const std::string& beginseq,
const std::string& endseq,
adds_sequences strateg)
adds_sequences strateg)
: begin_seq{beginseq}
, end_seq{endseq}
, strategy{strateg}
Expand All @@ -144,16 +127,16 @@ struct string_parts {
void clear() { parts.clear(); }

auto generate() const -> std::string {
if (parts.empty()) {
return (strategy & on_the_beginning ? begin_seq : std::string{})
+ (strategy & on_the_end ? end_seq : std::string{});

if (parts.empty()) {
return (strategy & on_the_beginning ? begin_seq : std::string{})
+ (strategy & on_the_end ? end_seq : std::string{});
}

auto result = std::visit(begin_visit{begin_seq, strategy},
auto result = std::visit(begin_visit{begin_seq, strategy},
parts.front());

if (std::ssize(parts) > 1) {
if (std::ssize(parts) > 1) {
auto it1 = parts.cbegin();
auto it2 = parts.cbegin()+1;
for(;it2 != parts.cend(); ++it1, ++it2) {
Expand Down Expand Up @@ -466,6 +449,18 @@ auto to_upper(char c)
}


auto to_lower(char c)
-> char
{
// C toupper is only not-UB in [0,127] and returns the wrong type,
// so wrap the range check and the type cast here in one place...
// note the 126 (not 127) is intentional to avoid a GCC warning
if (0 <= c && c <= 126) { return static_cast<char>(std::tolower(c)); }
// else
return c;
}


auto to_upper_and_underbar(std::string_view s)
-> std::string
{
Expand All @@ -478,6 +473,36 @@ auto to_upper_and_underbar(std::string_view s)
}


auto to_lower_and_collapsed_underbar(
std::string_view s,
bool prev_was_underbar = false,
bool drop_back_underbar = false
)
-> std::string
{
auto ret = std::string{};
for (char c : s) {
if (std::isalnum(c)) {
ret.push_back(to_lower(c));
prev_was_underbar = false;
}
else if (!prev_was_underbar) {
ret.push_back('_');
prev_was_underbar = true;
}
}
if (
drop_back_underbar
&& !ret.empty()
&& ret.back() == '_'
)
{
ret.pop_back();
}
return ret;
}


auto is_empty_or_a_decimal_number(std::string_view s)
-> bool
{
Expand Down Expand Up @@ -918,7 +943,7 @@ class stackinstr

static auto print(auto&& ee, std::string_view label) {
std::cout << "\n=== Stack debug information: " << label << " stack ===\n";
for (auto& e: ee)
for (auto& e: ee)
if (e.ptr) {
std::cout
<< " " << std::setw(6)
Expand Down
Loading