Skip to content

Commit 98ae2b9

Browse files
committed
Update multi-word-type diagnostic message, start roughing in udt support
1 parent b972313 commit 98ae2b9

File tree

4 files changed

+92
-37
lines changed

4 files changed

+92
-37
lines changed

regression-tests/test-results/pure2-cpp1-multitoken-fundamental-types-error.cpp2.output

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,30 +3,30 @@ pure2-cpp1-multitoken-fundamental-types-error.cpp2(3,8): error: 'signed short in
33
pure2-cpp1-multitoken-fundamental-types-error.cpp2(3,8): error: 'signed short int' is an old-style C/C++ multi-word keyword type
44
- most such types should be used only for interoperability with older code
55
- using those when you need them is fine, but name them with these short names instead:
6-
ushort, uint, ulong, longlong, ulonglong, __schar, __uchar
6+
short, ushort, int, uint, long, ulong, longlong, ulonglong, longdouble, __schar, __uchar
77
- see also cpp2util.h > "Convenience names for integer types"
88
pure2-cpp1-multitoken-fundamental-types-error.cpp2(4,8): error: 'short int signed' - did you mean 'short'?
99
pure2-cpp1-multitoken-fundamental-types-error.cpp2(4,8): error: 'short int signed' is an old-style C/C++ multi-word keyword type
1010
- most such types should be used only for interoperability with older code
1111
- using those when you need them is fine, but name them with these short names instead:
12-
ushort, uint, ulong, longlong, ulonglong, __schar, __uchar
12+
short, ushort, int, uint, long, ulong, longlong, ulonglong, longdouble, __schar, __uchar
1313
- see also cpp2util.h > "Convenience names for integer types"
1414
pure2-cpp1-multitoken-fundamental-types-error.cpp2(5,8): error: 'long long unsigned int' - did you mean 'ulonglong'?
1515
pure2-cpp1-multitoken-fundamental-types-error.cpp2(5,8): error: 'long long unsigned int' is an old-style C/C++ multi-word keyword type
1616
- most such types should be used only for interoperability with older code
1717
- using those when you need them is fine, but name them with these short names instead:
18-
ushort, uint, ulong, longlong, ulonglong, __schar, __uchar
18+
short, ushort, int, uint, long, ulong, longlong, ulonglong, longdouble, __schar, __uchar
1919
- see also cpp2util.h > "Convenience names for integer types"
2020
pure2-cpp1-multitoken-fundamental-types-error.cpp2(6,8): error: 'long double' - did you mean 'long'?
2121
pure2-cpp1-multitoken-fundamental-types-error.cpp2(6,8): error: 'long double' is an old-style C/C++ multi-word keyword type
2222
- most such types should be used only for interoperability with older code
2323
- using those when you need them is fine, but name them with these short names instead:
24-
ushort, uint, ulong, longlong, ulonglong, __schar, __uchar
24+
short, ushort, int, uint, long, ulong, longlong, ulonglong, longdouble, __schar, __uchar
2525
- see also cpp2util.h > "Convenience names for integer types"
2626
pure2-cpp1-multitoken-fundamental-types-error.cpp2(7,8): error: 'unsigned char' - did you mean 'u8' (usually best) or '__uchar'?
2727
pure2-cpp1-multitoken-fundamental-types-error.cpp2(7,8): error: 'unsigned char' is an old-style C/C++ multi-word keyword type
2828
- most such types should be used only for interoperability with older code
2929
- using those when you need them is fine, but name them with these short names instead:
30-
ushort, uint, ulong, longlong, ulonglong, __schar, __uchar
30+
short, ushort, int, uint, long, ulong, longlong, ulonglong, longdouble, __schar, __uchar
3131
- see also cpp2util.h > "Convenience names for integer types"
3232

source/cppfront.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2493,7 +2493,7 @@ class cppfront
24932493

24942494
//-----------------------------------------------------------------------
24952495
//
2496-
auto emit(declaration_node const& n, std::string const& in_expression_intro = {}) -> void
2496+
auto emit(declaration_node const& n, std::string const& capture_intro = {}) -> void
24972497
{
24982498
// If this is a function that has multiple return values,
24992499
// first we need to emit the struct that contains the returns
@@ -2529,9 +2529,9 @@ class cppfront
25292529

25302530
// If this is at expression scope, we can't emit "[[nodiscard]] auto name"
25312531
// so print the provided intro instead, which will be a lambda-capture-list
2532-
if (in_expression_intro != "") {
2532+
if (capture_intro != "") {
25332533
assert (!n.identifier);
2534-
printer.print_cpp2(in_expression_intro, n.position());
2534+
printer.print_cpp2(capture_intro, n.position());
25352535
emit( *func, nullptr );
25362536
}
25372537
else {

source/lex.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -413,7 +413,7 @@ auto lex_line(
413413
"'" + tokens.back().to_string(true) + "' is an old-style C/C++ multi-word keyword type\n"
414414
" - most such types should be used only for interoperability with older code\n"
415415
" - using those when you need them is fine, but name them with these short names instead:\n"
416-
" ushort, uint, ulong, longlong, ulonglong, __schar, __uchar\n"
416+
" short, ushort, int, uint, long, ulong, longlong, ulonglong, longdouble, __schar, __uchar\n"
417417
" - see also cpp2util.h > \"Convenience names for integer types\""
418418
);
419419
}
@@ -990,7 +990,7 @@ auto lex_line(
990990
}
991991

992992
//G string-literal:
993-
//G encoding-prefix-opt '"' s-char-seq-opt '"'
993+
//G encoding-prefix? '"' s-char-seq? '"'
994994
//G
995995
//G s-char-seq:
996996
//G s-char
@@ -1009,7 +1009,7 @@ auto lex_line(
10091009
}
10101010

10111011
//G character-literal:
1012-
//G encoding-prefix-opt ''' c-char-seq-opt '''
1012+
//G encoding-prefix? ''' c-char-seq? '''
10131013
//G
10141014
//G c-char-seq:
10151015
//G c-char

source/parse.h

Lines changed: 81 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,6 +1008,29 @@ struct function_type_node
10081008
};
10091009

10101010

1011+
//struct udt_type_node
1012+
//{
1013+
// source_position pos;
1014+
// std::vector<id_expression_node> metaclass_names;
1015+
//
1016+
// udt_type_node( source_position pos ) : pos{pos} { }
1017+
//
1018+
// auto position() const -> source_position
1019+
// {
1020+
// return pos;
1021+
// }
1022+
//
1023+
// auto visit(auto& v, int depth) -> void
1024+
// {
1025+
// v.start(*this, depth);
1026+
// for (auto const& m : metaclass_names) {
1027+
// m->visit(v, depth+1);
1028+
// }
1029+
// v.end(*this, depth);
1030+
// }
1031+
//};
1032+
1033+
10111034
struct declaration_node
10121035
{
10131036
// Declared first, because it should outlive any owned
@@ -1021,6 +1044,7 @@ struct declaration_node
10211044
std::variant<
10221045
std::unique_ptr<function_type_node>,
10231046
std::unique_ptr<type_id_node>
1047+
//std::unique_ptr<udt_type_node>
10241048
> type;
10251049

10261050
source_position equal_sign = {};
@@ -1570,7 +1594,7 @@ class parser
15701594
//G primary-expression
15711595
//G postfix-expression postfix-operator [Note: without whitespace before the operator]
15721596
//G postfix-expression '[' expression-list ']'
1573-
//G postfix-expression '(' expression-list-opt ')'
1597+
//G postfix-expression '(' expression-list? ')'
15741598
//G postfix-expression '.' id-expression
15751599
//G
15761600
auto postfix_expression()
@@ -1981,7 +2005,7 @@ class parser
19812005
}
19822006

19832007
//G expression-list:
1984-
//G parameter-direction-opt expression
2008+
//G parameter-direction? expression
19852009
//G expression-list ',' expression
19862010
//G
19872011
auto expression_list(source_position open_paren, bool inside_initializer = false) -> std::unique_ptr<expression_list_node> {
@@ -2035,8 +2059,8 @@ class parser
20352059

20362060

20372061
//G type-id:
2038-
//G type-qualifier-seq-opt qualified-id
2039-
//G type-qualifier-seq-opt unqualified-id
2062+
//G type-qualifier-seq? qualified-id
2063+
//G type-qualifier-seq? unqualified-id
20402064
//G
20412065
//G type-qualifier-seq:
20422066
//G type-qualifier
@@ -2170,7 +2194,7 @@ class parser
21702194
//GTODO operator-function-id
21712195
//G
21722196
//G template-id:
2173-
//G identifier '<' template-argument-list-opt '>'
2197+
//G identifier '<' template-argument-list? '>'
21742198
//G
21752199
//G template-argument-list:
21762200
//G template-argument-list ',' template-argument
@@ -2379,8 +2403,8 @@ class parser
23792403

23802404

23812405
//G selection-statement:
2382-
//G 'if' 'constexpr'-opt expression compound-statement
2383-
//G 'if' 'constexpr'-opt expression compound-statement 'else' compound-statement
2406+
//G 'if' 'constexpr'? expression compound-statement
2407+
//G 'if' 'constexpr'? expression compound-statement 'else' compound-statement
23842408
//G
23852409
auto selection_statement() -> std::unique_ptr<selection_statement_node>
23862410
{
@@ -2436,7 +2460,7 @@ class parser
24362460

24372461

24382462
//G return-statement:
2439-
//G return expression-opt ';'
2463+
//G return expression? ';'
24402464
//G
24412465
auto return_statement() -> std::unique_ptr<return_statement_node>
24422466
{
@@ -2475,9 +2499,9 @@ class parser
24752499

24762500

24772501
//G iteration-statement:
2478-
//G 'while' logical-or-expression next-clause-opt compound-statement
2479-
//G 'do' compound-statement 'while' logical-or-expression next-clause-opt ';'
2480-
//G 'for' expression next-clause-opt 'do' unnamed-declaration
2502+
//G 'while' logical-or-expression next-clause? compound-statement
2503+
//G 'do' compound-statement 'while' logical-or-expression next-clause? ';'
2504+
//G 'for' expression next-clause? 'do' unnamed-declaration
24812505
//G
24822506
//G next-clause:
24832507
//G 'next' assignment-expression
@@ -2602,9 +2626,9 @@ class parser
26022626

26032627

26042628
//G alternative:
2605-
//G alt-name-opt is-type-constraint '=' statement
2606-
//G alt-name-opt is-value-constraint '=' statement
2607-
//G alt-name-opt as-type-cast '=' statement
2629+
//G alt-name? is-type-constraint '=' statement
2630+
//G alt-name? is-value-constraint '=' statement
2631+
//G alt-name? as-type-cast '=' statement
26082632
//G
26092633
//G alt-name:
26102634
//G unqualified-id :
@@ -2653,8 +2677,8 @@ class parser
26532677

26542678

26552679
//G inspect-expression:
2656-
//G 'inspect' 'constexpr'-opt expression '{' alternative-seq-opt '}'
2657-
//G 'inspect' 'constexpr'-opt expression '->' type-id '{' alternative-seq-opt '}'
2680+
//G 'inspect' 'constexpr'? expression '{' alternative-seq? '}'
2681+
//G 'inspect' 'constexpr'? expression '->' type-id '{' alternative-seq? '}'
26582682
//G
26592683
//G alternative-seq:
26602684
//G alternative
@@ -2831,7 +2855,7 @@ class parser
28312855

28322856

28332857
//G compound-statement:
2834-
//G '{' statement-seq-opt '}'
2858+
//G '{' statement-seq? '}'
28352859
//G
28362860
//G statement-seq:
28372861
//G statement
@@ -2875,7 +2899,7 @@ class parser
28752899

28762900

28772901
//G parameter-declaration:
2878-
//G parameter-direction-opt declaration
2902+
//G parameter-direction? declaration
28792903
//G
28802904
//G parameter-direction: one of
28812905
//G 'in' 'copy' 'inout' 'out' 'move' 'forward'
@@ -2971,7 +2995,7 @@ class parser
29712995

29722996

29732997
//G parameter-declaration-list
2974-
//G '(' parameter-declaration-seq-opt ')'
2998+
//G '(' parameter-declaration-seq? ')'
29752999
//G
29763000
//G parameter-declaration-seq:
29773001
//G parameter-declaration
@@ -3018,8 +3042,8 @@ class parser
30183042

30193043

30203044
//G contract:
3021-
//G '[' '[' contract-kind id-expression-opt ':' logical-or-expression ']' ']'
3022-
//G '[' '[' contract-kind id-expression-opt ':' logical-or-expression ',' string-literal ']' ']'
3045+
//G '[' '[' contract-kind id-expression? ':' logical-or-expression ']' ']'
3046+
//G '[' '[' contract-kind id-expression? ':' logical-or-expression ',' string-literal ']' ']'
30233047
//G
30243048
//G contract-kind: one of
30253049
//G 'pre' 'post' 'assert'
@@ -3088,7 +3112,7 @@ class parser
30883112

30893113

30903114
//G function-type:
3091-
//G parameter-declaration-list throws-specifier-opt return-list-opt contract-seq-opt
3115+
//G parameter-declaration-list throws-specifier? return-list? contract-seq?
30923116
//G
30933117
//G throws-specifier:
30943118
//G 'throws'
@@ -3154,8 +3178,13 @@ class parser
31543178

31553179
//G unnamed-declaration:
31563180
//G ':' function-type '=' statement
3157-
//G ':' type-id-opt '=' statement
3181+
//G ':' type-id? '=' statement
31583182
//G ':' type-id
3183+
//G ':' 'type' meta-constraints? '=' statement
3184+
//G
3185+
//G meta-constraints:
3186+
//G 'is' id-expression
3187+
//G meta-constraints ',' id-expression
31593188
//G
31603189
auto unnamed_declaration(source_position start, bool semicolon_required = true, bool captures_allowed = false) -> std::unique_ptr<declaration_node>
31613190
{
@@ -3179,7 +3208,13 @@ class parser
31793208

31803209
// Next is an an optional type
31813210

3182-
// It could be a function type, declaring a function
3211+
//// It could be "type," declaring a user-defined type
3212+
//if (auto t = udt_type()) {
3213+
// n->type = std::move(t);
3214+
// assert (n->type.index() == declaration_node::udt);
3215+
//}
3216+
3217+
// Or a function type, declaring a function
31833218
if (auto t = function_type()) {
31843219
n->type = std::move(t);
31853220
assert (n->type.index() == declaration_node::function);
@@ -3316,6 +3351,15 @@ class parser
33163351
return {};
33173352
}
33183353

3354+
if (n->parent_scope && n->is(declaration_node::function) && n->parent_scope->is(declaration_node::function)) {
3355+
assert (id->get_token());
3356+
auto name = id->get_token()->to_string(true);
3357+
errors.emplace_back(
3358+
curr().position(),
3359+
"(temporary alpha limitation) local functions like '" + name + ": (/*params*/) = {/*body*/}' are not currently supported - write a local variable initialized with an unnamed function like '" + name + " := :(/*params*/) = {/*body*/};' instead (add ':=' and ';')"
3360+
);
3361+
}
3362+
33193363
n->identifier = std::move(id);
33203364
return n;
33213365
}
@@ -3326,7 +3370,7 @@ class parser
33263370
//G declaration-seq declaration
33273371
//G
33283372
//G translation-unit:
3329-
//G declaration-seq-opt
3373+
//G declaration-seq?
33303374
//
33313375
auto translation_unit() -> std::unique_ptr<translation_unit_node>
33323376
{
@@ -3416,6 +3460,11 @@ class parse_tree_printer : printing_visitor
34163460
o << pre(indent) << "prefix-expression\n";
34173461
}
34183462

3463+
auto start(is_as_expression_node const&, int indent) -> void
3464+
{
3465+
o << pre(indent) << "is-as-expression\n";
3466+
}
3467+
34193468
template<String Name, typename Term>
34203469
auto start(binary_expression_node<Name, Term> const&, int indent) -> void
34213470
{
@@ -3504,6 +3553,11 @@ class parse_tree_printer : printing_visitor
35043553
}
35053554
}
35063555

3556+
//auto start(udt_type_node const&, int indent) -> void
3557+
//{
3558+
// o << pre(indent) << "user-defined type\n";
3559+
//}
3560+
35073561
auto start(function_type_node const& n, int indent) -> void
35083562
{
35093563
o << pre(indent) << "function\n";
@@ -3522,7 +3576,8 @@ class parse_tree_printer : printing_visitor
35223576

35233577
auto start(declaration_node const& n, int indent) -> void
35243578
{
3525-
o << pre(indent) << "declaration\n";
3579+
o << pre(indent) << "declaration [" << &n << "]\n";
3580+
o << pre(indent+1) << "parent: [" << n.parent_scope << "]\n";
35263581
if (!n.captures.members.empty()) {
35273582
o << pre(indent+1) << "captures: " << n.captures.members.size() << "\n";
35283583
}

0 commit comments

Comments
 (0)