Skip to content

Commit c72ac86

Browse files
committed
Allow copy local params, and allow reclaimed keywords as identifiers
Allow `copy` statement/block parameter, closes #393 Allow `class` and `struct` and other reclaimed words as declared names, closes #394
1 parent e85612d commit c72ac86

11 files changed

+104
-97
lines changed
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
pure2-return-tuple-no-identifier-error.cpp2...
12
pure2-return-tuple-no-identifier-error.cpp2(1,11): error: expected identifier, not 'int'
23
pure2-return-tuple-no-identifier-error.cpp2(1,16): error: expected identifier, not 'int'
4+
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
pure2-return-tuple-no-type-error.cpp2...
12
pure2-return-tuple-no-type-error.cpp2(1,11): error: return parameter 'a' must have a type
23
pure2-return-tuple-no-type-error.cpp2(1,14): error: return parameter 'b' must have a type
4+

regression-tests/test-results/pure2-types-inheritance.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,14 @@ auto main() -> int;
9292
//=== Cpp2 function definitions =================================================
9393

9494

95-
95+
9696
Human::~Human(){}
9797
#line 6 "pure2-types-inheritance.cpp2"
9898
namespace N {
9999

100100
template <int I> Machine<I>::Machine(cpp2::in<std::string> id){}
101-
template <int I>
102-
Machine<I>::~Machine(){}
101+
102+
template <int I> Machine<I>::~Machine(){}
103103

104104
#line 11 "pure2-types-inheritance.cpp2"
105105
}

regression-tests/test-results/pure2-types-ordering-via-meta-functions.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ auto main() -> int;
9696
#line 4 "pure2-types-ordering-via-meta-functions.cpp2"
9797
}
9898

99+
99100
#line 9 "pure2-types-ordering-via-meta-functions.cpp2"
100101
case_insensitive_string::case_insensitive_string(cpp2::in<std::string> val)
101102
: v{ val }
@@ -108,6 +109,7 @@ auto main() -> int;
108109
#line 9 "pure2-types-ordering-via-meta-functions.cpp2"
109110
}
110111

112+
111113
#line 14 "pure2-types-ordering-via-meta-functions.cpp2"
112114
person_in_family_tree::person_in_family_tree(cpp2::in<int> parents)
113115
: dummy_data{ parents }
@@ -119,20 +121,20 @@ auto main() -> int;
119121
return *this;
120122
#line 14 "pure2-types-ordering-via-meta-functions.cpp2"
121123
}
122-
124+
125+
123126
mystruct::mystruct(mystruct const& that)
124127
: val{ that.val }{}
125128

126-
auto mystruct::operator=(mystruct const& that) -> mystruct& {
129+
auto mystruct::operator=(mystruct const& that) -> mystruct& {
127130
val = that.val;
128131
return *this;}
129-
mystruct::mystruct(mystruct&& that)
132+
mystruct::mystruct(mystruct&& that)
130133
: val{ std::move(that).val }{}
131-
auto mystruct::operator=(mystruct&& that) -> mystruct& {
134+
auto mystruct::operator=(mystruct&& that) -> mystruct& {
132135
val = std::move(that).val;
133136
return *this;}
134-
135-
mystruct::mystruct(){}
137+
mystruct::mystruct(){}
136138
#line 21 "pure2-types-ordering-via-meta-functions.cpp2"
137139
auto main() -> int{
138140
my_integer a {1};

regression-tests/test-results/pure2-types-value-types-via-meta-functions.cpp

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -92,20 +92,20 @@ template<typename T> auto test() -> void;
9292
return *this;
9393
#line 4 "pure2-types-value-types-via-meta-functions.cpp2"
9494
}
95-
95+
96+
9697
widget::widget(widget const& that)
9798
: val{ that.val }{}
9899

99-
auto widget::operator=(widget const& that) -> widget& {
100+
auto widget::operator=(widget const& that) -> widget& {
100101
val = that.val;
101102
return *this;}
102-
widget::widget(widget&& that)
103+
widget::widget(widget&& that)
103104
: val{ std::move(that).val }{}
104-
auto widget::operator=(widget&& that) -> widget& {
105+
auto widget::operator=(widget&& that) -> widget& {
105106
val = std::move(that).val;
106107
return *this;}
107-
108-
widget::widget(){}
108+
widget::widget(){}
109109
#line 9 "pure2-types-value-types-via-meta-functions.cpp2"
110110
w_widget::w_widget(cpp2::in<int> i)
111111
: val{ i }
@@ -117,20 +117,20 @@ template<typename T> auto test() -> void;
117117
return *this;
118118
#line 9 "pure2-types-value-types-via-meta-functions.cpp2"
119119
}
120-
120+
121+
121122
w_widget::w_widget(w_widget const& that)
122123
: val{ that.val }{}
123124

124-
auto w_widget::operator=(w_widget const& that) -> w_widget& {
125+
auto w_widget::operator=(w_widget const& that) -> w_widget& {
125126
val = that.val;
126127
return *this;}
127-
w_widget::w_widget(w_widget&& that)
128+
w_widget::w_widget(w_widget&& that)
128129
: val{ std::move(that).val }{}
129-
auto w_widget::operator=(w_widget&& that) -> w_widget& {
130+
auto w_widget::operator=(w_widget&& that) -> w_widget& {
130131
val = std::move(that).val;
131132
return *this;}
132-
133-
w_widget::w_widget(){}
133+
w_widget::w_widget(){}
134134
#line 14 "pure2-types-value-types-via-meta-functions.cpp2"
135135
p_widget::p_widget(cpp2::in<int> i)
136136
: val{ i }
@@ -142,20 +142,20 @@ template<typename T> auto test() -> void;
142142
return *this;
143143
#line 14 "pure2-types-value-types-via-meta-functions.cpp2"
144144
}
145-
145+
146+
146147
p_widget::p_widget(p_widget const& that)
147148
: val{ that.val }{}
148149

149-
auto p_widget::operator=(p_widget const& that) -> p_widget& {
150+
auto p_widget::operator=(p_widget const& that) -> p_widget& {
150151
val = that.val;
151152
return *this;}
152-
p_widget::p_widget(p_widget&& that)
153+
p_widget::p_widget(p_widget&& that)
153154
: val{ std::move(that).val }{}
154-
auto p_widget::operator=(p_widget&& that) -> p_widget& {
155+
auto p_widget::operator=(p_widget&& that) -> p_widget& {
155156
val = std::move(that).val;
156157
return *this;}
157-
158-
p_widget::p_widget(){}
158+
p_widget::p_widget(){}
159159
#line 17 "pure2-types-value-types-via-meta-functions.cpp2"
160160
auto main() -> int{
161161
test<widget>();

regression-tests/test-results/run-tests.bat

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
@echo off
2+
cppfront.exe -version > version
23
del *.cpp *.output
34
copy ..\*.cpp2 .
45
set count=0

regression-tests/test-results/version

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
cppfront compiler - v0.2.1, build 8427:1621
3+
Copyright(c) Herb Sutter, all rights reserved
4+
5+
SPDX-License-Identifier: CC-BY-NC-ND-4.0
6+
No commercial use
7+
No forks/derivatives
8+
9+
Absolutely no warranty - try at your own risk

source/common.h

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -744,17 +744,30 @@ class cmdline_processor
744744
auto print_version()
745745
-> void
746746
{
747+
auto stamp = [](
748+
std::string_view a = __DATE__,
749+
std::string_view b = __TIME__
750+
)
751+
-> std::string
752+
{
753+
std::unordered_map<std::string_view, char> m = { {"Jan",'1'}, {"Feb",'2'}, {"Mar",'3'}, {"Apr",'4'}, {"May",'5'}, {"Jun",'6'}, {"Jul",'7'}, {"Aug",'8'}, {"Sep",'9'}, {"Oct",'A'}, {"Nov",'B'}, {"Dec",'C'} };
754+
755+
auto ret = std::to_string(atoi(&a[9])-15);
756+
ret += m[a.substr(0, 3)];
757+
ret += a.substr(4,2);
758+
ret += ":";
759+
ret += b.substr(0,2);
760+
ret += b.substr(3,2);
761+
return ret;
762+
};
763+
747764
help_requested = true;
748-
print("cppfront 0.1.1 compiler\nCopyright (C) Herb Sutter\n");
749-
print("All rights reserved\n");
765+
print("\ncppfront compiler - v0.2.1, build " + stamp());
766+
print("\nCopyright(c) Herb Sutter, all rights reserved\n");
750767
print("\nSPDX-License-Identifier: CC-BY-NC-ND-4.0");
751768
print("\n No commercial use");
752-
print("\n No forks/derivatives");
753-
print("\n");
754-
print("\nNote: This is a hilariously incomplete personal project.");
755-
print("\nIt's known to be incomplet and incorrekt, and it has lots of b");
756-
print("\nad formAt ting. Absolutely no warranty - try at your own risk.");
757-
print("\n");
769+
print("\n No forks/derivatives\n");
770+
print("\nAbsolutely no warranty - try at your own risk\n");
758771
}
759772

760773
} cmdline;

source/cppfront.cpp

Lines changed: 34 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -725,19 +725,24 @@ class positional_printer
725725
)
726726
-> void
727727
{
728-
// If we are in a generated text region (signified by negative
729-
// line numbers) shunt this call to print_extra instead
730-
if (pos.lineno < 0) {
731-
if (generated_pos_line != pos.lineno) {
732-
*out << "\n" + std::string(last_line_indentation, ' ');
733-
generated_pos_line = pos.lineno;
734-
}
735-
print_extra(s);
736-
return;
737-
}
728+
// If we're printing for real (not to a string target)
729+
if (emit_target_stack.empty())
730+
{
731+
// If we're in a generated text region (signified by negative
732+
// line numbers), then shunt this call to print_extra instead
733+
if (pos.lineno < 1) {
734+
if (generated_pos_line != pos.lineno) {
735+
*out << "\n" + std::string(last_line_indentation, ' ');
736+
generated_pos_line = pos.lineno;
737+
}
738+
print_extra(s);
739+
return;
740+
}
738741

739-
// No longer in generated code, so reset the generated code counter
740-
generated_pos_line = {};
742+
// Otherwise, we're no longer in generated code, so reset the
743+
// generated code counter
744+
generated_pos_line = {};
745+
}
741746

742747
assert(
743748
is_open()
@@ -1549,6 +1554,7 @@ class cppfront
15491554
|| n == "xor"
15501555
|| n == "xor_eq"
15511556
|| n == "new"
1557+
|| n == "class"
15521558
|| n == "struct"
15531559
)
15541560
{
@@ -4625,46 +4631,6 @@ class cppfront
46254631
++object;
46264632
}
46274633

4628-
//// Each remaining object is required to have a default initializer,
4629-
//// since it had no explicit initialization statement
4630-
//for (
4631-
// ;
4632-
// object != objects.end();
4633-
// ++object
4634-
// )
4635-
//{
4636-
// auto object_name = canonize_object_name(*object);
4637-
4638-
// if ((*object)->initializer)
4639-
// {
4640-
// // Good. Entering here avoids emitting the error on the 'else'
4641-
4642-
// auto initializer = print_to_string( *(*object)->initializer, false );
4643-
// if (initializer.empty()) {
4644-
// initializer = "{}";
4645-
// }
4646-
4647-
// // Need to actually emit the initializer in an assignment operator
4648-
// if (is_assignment)
4649-
// {
4650-
// current_functions.back().prolog.statements.push_back(
4651-
// object_name +
4652-
// " = " +
4653-
// initializer +
4654-
// ";"
4655-
// );
4656-
// }
4657-
// }
4658-
// else
4659-
// {
4660-
// errors.emplace_back(
4661-
// n.position(),
4662-
// object_name + " was not initialized in the operator= body and has no default initializer - " + error_msg
4663-
// );
4664-
// return;
4665-
// }
4666-
//}
4667-
46684634
// Now no data members should be left over
46694635
if (object != objects.end())
46704636
{
@@ -4687,8 +4653,8 @@ class cppfront
46874653
{
46884654
printer.print_cpp2( prefix, n.position() );
46894655
printer.print_cpp2( type_qualification_if_any_for(n), n.position() );
4690-
printer.print_cpp2( *n.parent_declaration->name(), n.position() );
4691-
emit( *func, n.name(), false, true);
4656+
printer.print_cpp2( print_to_string( *n.parent_declaration->name() ), n.position() );
4657+
emit( *func, n.name(), false, true );
46924658
}
46934659
// For an assignment operator, similar to emitting an ordinary function
46944660
else
@@ -4698,9 +4664,8 @@ class cppfront
46984664
&& !current_functions.empty()
46994665
);
47004666
current_functions.back().epilog.push_back( "return *this;");
4701-
printer.print_cpp2( "auto " + type_qualification_if_any_for(n), n.position() );
4702-
printer.print_cpp2( *n.name(), n.identifier->position());
4703-
emit( *func, n.name());
4667+
printer.print_cpp2( "auto " + type_qualification_if_any_for(n) + print_to_string( *n.name() ), n.position());
4668+
emit( *func, n.name() );
47044669
}
47054670
}
47064671

@@ -4728,6 +4693,15 @@ class cppfront
47284693
return;
47294694
}
47304695

4696+
// If this is a generated declaration (negative source line number),
4697+
// add a line break before
4698+
if (
4699+
printer.get_phase() == printer.phase2_func_defs
4700+
&& n.position().lineno < 1
4701+
)
4702+
{
4703+
printer.print_extra("\n");
4704+
}
47314705

47324706
// Handle aliases
47334707

@@ -4894,7 +4868,7 @@ class cppfront
48944868
if (emit_as_base) {
48954869
printer.print_extra(
48964870
"\nstruct "
4897-
+ decl->parent_declaration->name()->to_string(true)
4871+
+ print_to_string(*decl->parent_declaration->name())
48984872
+ "_"
48994873
+ decl->name()->to_string(true)
49004874
+ "_as_base { "
@@ -5074,7 +5048,7 @@ class cppfront
50745048
printer.print_cpp2(
50755049
separator
50765050
+ " public "
5077-
+ decl->parent_declaration->name()->to_string(true)
5051+
+ print_to_string(*decl->parent_declaration->name())
50785052
+ "_"
50795053
+ decl->name()->to_string(true)
50805054
+ "_as_base",
@@ -5420,7 +5394,7 @@ class cppfront
54205394
printer.print_cpp2(
54215395
prefix
54225396
+ type_qualification_if_any_for(n)
5423-
+ "~" + n.parent_declaration->name()->to_string(true),
5397+
+ "~" + print_to_string(*n.parent_declaration->name()),
54245398
n.position()
54255399
);
54265400
emit( *func, n.name(), false, true);

source/lex.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -929,7 +929,7 @@ auto lex_line(
929929
const auto keys = std::regex(
930930
"^alignas|^alignof|^asm|^as|^auto|"
931931
"^bool|^break|"
932-
"^case|^catch|^char16_t|^char32_t|^char8_t|^char|^class|^co_await|^co_return|"
932+
"^case|^catch|^char16_t|^char32_t|^char8_t|^char|^co_await|^co_return|"
933933
"^co_yield|^concept|^const_cast|^consteval|^constexpr|^constinit|^const|^continue|"
934934
"^decltype|^default|^double|^do|^dynamic_cast|"
935935
"^else|^enum|^explicit|^export|^extern|"

0 commit comments

Comments
 (0)