Skip to content

Implement sizeof<T> #574

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions regression-tests/mixed-fixed-type-aliases.cpp2
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <typeinfo>
#include <iostream>
#include <cassert>

namespace my {
using u16 = float;
Expand All @@ -13,9 +14,11 @@ test: (x:_) = {
}

main: (args) -> int = {
assert(sizeof<my::u16> == 4);
y: my::u16 = 42;
test(y);

assert(sizeof<u16> == 2);
z: u16 = 42;
test(z);

Expand Down
6 changes: 6 additions & 0 deletions regression-tests/pure2-sizeof-new-error.cpp2
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
main: () = {
p := new i32;
p = new(i32);
i := sizeof(i32);
i = sizeof i32;
}
11 changes: 7 additions & 4 deletions regression-tests/test-results/mixed-fixed-type-aliases.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,24 @@

#include <typeinfo>
#include <iostream>
#include <cassert>

namespace my {
using u16 = float;
}

#line 8 "mixed-fixed-type-aliases.cpp2"
#line 9 "mixed-fixed-type-aliases.cpp2"
auto test(auto const& x) -> void;


#line 15 "mixed-fixed-type-aliases.cpp2"
#line 16 "mixed-fixed-type-aliases.cpp2"
[[nodiscard]] auto main(int const argc_, char const* const* const argv_) -> int;


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


#line 8 "mixed-fixed-type-aliases.cpp2"
#line 9 "mixed-fixed-type-aliases.cpp2"
auto test(auto const& x) -> void{
std::cout
<< std::boolalpha
Expand All @@ -37,10 +38,12 @@ auto test(auto const& x) -> void{

[[nodiscard]] auto main(int const argc_, char const* const* const argv_) -> int{
auto args = cpp2::make_args(argc_, argv_);
#line 16 "mixed-fixed-type-aliases.cpp2"
#line 17 "mixed-fixed-type-aliases.cpp2"
assert(sizeof(my::u16)==4);
my::u16 y {42};
test(std::move(y));

assert(sizeof(cpp2::u16)==2);
cpp2::u16 z {42};
test(std::move(z));

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pure2-sizeof-new-error.cpp2...
pure2-sizeof-new-error.cpp2(2,14): error: use 'new<i32>', not 'new i32'
pure2-sizeof-new-error.cpp2(3,12): error: use 'new<i32>', not 'new i32'
pure2-sizeof-new-error.cpp2(4,16): error: use 'sizeof<i32>', not 'sizeof i32'
pure2-sizeof-new-error.cpp2(5,16): error: use 'sizeof<i32>', not 'sizeof i32'
9 changes: 8 additions & 1 deletion source/cppfront.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1666,7 +1666,14 @@ class cppfront
assert(n.identifier);
emit(*n.identifier, is_qualified); // inform the identifier if we know this is qualified

if (n.open_angle != source_position{}) {
if (*n.identifier == "sizeof") {
printer.print_cpp2("(", n.open_angle);
auto& a = n.template_args[0];
try_emit<unqualified_id_node::expression>(a.arg);
try_emit<unqualified_id_node::type_id >(a.arg);
printer.print_cpp2(")", n.close_angle);
}
else if (n.open_angle != source_position{}) {
printer.print_cpp2("<", n.open_angle);
auto first = true;
for (auto& a : n.template_args) {
Expand Down
10 changes: 4 additions & 6 deletions source/parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -4880,12 +4880,10 @@ class parser
}

else {
if (*n->identifier == "new") {
error( "use 'new<" + curr().to_string(true) + ">', not 'new " + curr().to_string(true) + "'", false);
return {};
}
if (*n->identifier == "sizeof") {
error( "use 'sizeof(" + curr().to_string(true) + ")', not 'sizeof " + curr().to_string(true) + "'", false);
if (*n->identifier == "new" || *n->identifier == "sizeof") {
auto id = n->identifier->to_string(true);
auto& arg = (curr().type() == lexeme::LeftParen) ? *peek(1) : curr();
error( "use '" + id + "<" + arg.to_string(true) + ">', not '" + id + " " + arg.to_string(true) + "'", false);
return {};
}
if (*n->identifier == "co_await" || *n->identifier == "co_yield") {
Expand Down