-
Notifications
You must be signed in to change notification settings - Fork 251
/
Copy pathparse.h
10241 lines (8788 loc) · 282 KB
/
parse.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2022-2024 Herb Sutter
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
// Part of the Cppfront Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://github.com/hsutter/cppfront/blob/main/LICENSE for license information.
//===========================================================================
// Parser
//===========================================================================
#ifndef CPP2_PARSE_H
#define CPP2_PARSE_H
#include "lex.h"
namespace cpp2 {
auto violates_lifetime_safety = false;
//-----------------------------------------------------------------------
// Operator categorization
//
//G prefix-operator:
//G one of '!' '-' '+'
//GT parameter-direction
//G
auto is_prefix_operator(token const& tok)
-> bool
{
switch (tok.type()) {
break;case lexeme::Not:
case lexeme::Minus:
case lexeme::Plus:
return true;
break;default:
return false;
}
}
//G postfix-operator:
//G one of '++' '--' '*' '&' '~' '$' '...'
//G
auto is_postfix_operator(lexeme l)
-> bool
{
switch (l) {
break;case lexeme::PlusPlus:
case lexeme::MinusMinus:
case lexeme::Multiply:
case lexeme::Ampersand:
case lexeme::Tilde:
case lexeme::Dollar:
case lexeme::Ellipsis:
case lexeme::EllipsisLess:
case lexeme::EllipsisEqual:
return true;
break;default:
return false;
}
}
//G assignment-operator:
//G one of '=' '*=' '/=' '%=' '+=' '-=' '>>=' '<<=' '&=' '^=' '|='
//G
auto is_assignment_operator(lexeme l)
-> bool
{
switch (l) {
break;case lexeme::Assignment:
case lexeme::MultiplyEq:
case lexeme::SlashEq:
case lexeme::ModuloEq:
case lexeme::PlusEq:
case lexeme::MinusEq:
case lexeme::RightShiftEq:
case lexeme::LeftShiftEq:
case lexeme::AmpersandEq:
case lexeme::CaretEq:
case lexeme::PipeEq:
return true;
break;default:
return false;
}
}
//-----------------------------------------------------------------------
//
// Parse tree node types
//
//-----------------------------------------------------------------------
//
//-----------------------------------------------------------------------
// try_visit
//
// Helper to visit whatever is in a variant where each
// alternative is a smart pointer
//
template <int I>
auto try_visit(auto& variant, auto& visitor, int depth)
-> void
{
if (variant.index() == I) {
auto const& s = std::get<I>(variant);
assert (s);
s->visit(visitor, depth+1);
}
}
struct expression_list_node;
struct id_expression_node;
struct declaration_node;
struct inspect_expression_node;
struct literal_node;
struct template_argument;
struct primary_expression_node
{
enum active : u8 { empty=0, identifier, expression_list, id_expression, declaration, inspect, literal };
std::variant<
std::monostate,
token const*,
std::unique_ptr<expression_list_node>,
std::unique_ptr<id_expression_node>,
std::unique_ptr<declaration_node>,
std::unique_ptr<inspect_expression_node>,
std::unique_ptr<literal_node>
> expr;
// Cache to work around <https://github.com/llvm/llvm-project/issues/73336>.
bool expression_list_is_fold_expression = false;
// Out-of-line definition of the dtor is necessary due to the forward-declared
// type(s) used in a std::unique_ptr as a member
~primary_expression_node();
// API
//
auto is_fold_expression() const
-> bool;
auto is_identifier() const
-> bool;
auto is_id_expression() const
-> bool;
auto is_unqualified_id() const
-> bool;
auto is_expression_list() const
-> bool;
auto get_expression_list() const
-> expression_list_node const*;
auto is_literal() const
-> bool;
auto get_literal() const
-> literal_node const*;
auto template_arguments() const -> std::vector<template_argument> const&;
auto get_token() const -> token const*;
auto to_string() const
-> std::string;
// Internals
//
auto position() const -> source_position;
auto visit(auto& v, int depth) -> void;
};
struct literal_node {
// A literal is represented as a sequence of tokens:
// - length 1: a literal (most common)
// - length 2: a literal and a user-defined suffix
// - length >= 2: a series of one or more of either of the above for string literals
std::vector <token const*> pieces = {};
// API
//
auto get_token() const
-> token const*
{
assert(!pieces.empty());
return pieces.front();
}
auto to_string() const
-> std::string
{
assert(!pieces.empty());
auto ret = std::string{};
for (bool first = true; auto p : pieces)
{
assert(p);
// Add a space to non-first pieces that start with " (i.e., not a UDL suffix)
if (
!std::exchange(first, false)
&& p->as_string_view().starts_with("\"")
)
{
ret += " ";
}
ret += *p;
}
return ret;
}
auto has_user_defined_suffix()
-> bool
{
return
std::ssize(pieces) > 1
&& !pieces[1]->as_string_view().starts_with("\"")
;
}
// Internals
//
auto position() const
-> source_position
{
assert(!pieces.empty());
return get_token()->position();
}
auto visit(auto& v, int depth) -> void
{
v.start(*this, depth);
for (auto p : pieces) {
assert(p);
p->visit(v, depth+1);
}
v.end(*this, depth);
}
};
struct postfix_expression_node;
struct prefix_expression_node
{
std::vector<token const*> ops;
std::unique_ptr<postfix_expression_node> expr;
// Out-of-line definition of the dtor is necessary due to the forward-declared
// type(s) used in a std::unique_ptr as a member
~prefix_expression_node();
// API
//
auto is_fold_expression() const
-> bool;
auto is_identifier() const
-> bool;
auto is_id_expression() const
-> bool;
auto is_unqualified_id() const
-> bool;
auto is_expression_list() const
-> bool;
auto get_expression_list() const
-> expression_list_node const*;
auto get_postfix_expression_node() const
-> postfix_expression_node *
{
assert(expr);
return expr.get();
}
auto get_if_only_a_postfix_expression_node() const
-> postfix_expression_node *
{
if (ops.empty()) {
return expr.get();
}
// Else
return {};
}
auto is_literal() const
-> bool;
auto get_literal() const
-> literal_node const*;
auto is_result_a_temporary_variable() const -> bool;
auto to_string() const
-> std::string;
// Internals
//
auto position() const -> source_position;
auto visit(auto& v, int depth) -> void;
};
struct expression_node;
template<
String Name,
typename Term
>
struct binary_expression_node
{
std::unique_ptr<Term> expr;
expression_node const* my_expression = {};
binary_expression_node();
// Out-of-line definition of the dtor is necessary due to the forward-declared
// type(s) used as Term in a std::unique_ptr as a member
~binary_expression_node();
struct term
{
token const* op;
std::unique_ptr<Term> expr;
};
std::vector<term> terms;
// API
//
auto is_fold_expression() const
-> bool
{
// This is a fold-expression if any subexpression
// has an identifier named "..."
auto ret = expr->is_fold_expression();
for (auto& x : terms) {
ret |= x.expr->is_fold_expression();
}
return ret;
}
auto lhs_is_id_expression() const
-> bool
{
return expr->is_id_expression();
}
auto is_standalone_expression() const
-> bool;
auto terms_size() const
-> int
{
return unchecked_narrow<int>(std::ssize(terms));
}
auto is_identifier() const
-> bool
{
return terms.empty() && expr->is_identifier();
}
auto is_id_expression() const
-> bool
{
return terms.empty() && expr->is_id_expression();
}
auto is_unqualified_id() const
-> bool
{
return terms.empty() && expr->is_unqualified_id();
}
auto is_expression_list() const
-> bool
{
return terms.empty() && expr->is_expression_list();
}
auto get_expression_list() const
-> expression_list_node const*
{
if (is_expression_list()) {
return expr->get_expression_list();
}
return {};
}
auto is_literal() const
-> bool
{
return get_literal();
}
auto get_literal() const
-> literal_node const*
{
if (!terms.empty()) {
return nullptr;
}
// Else
return expr->get_literal();
}
// Get left-hand postfix-expression
auto get_postfix_expression_node() const
-> postfix_expression_node *
{
assert(expr);
return expr->get_postfix_expression_node();
}
auto get_if_only_a_postfix_expression_node() const
-> postfix_expression_node *
{
if (terms.empty()) {
return expr->get_if_only_a_postfix_expression_node();
}
// Else
return {};
}
// Get first right-hand postfix-expression, if there is one
auto get_second_postfix_expression_node() const
-> postfix_expression_node *
{
if (!terms.empty()) {
assert(terms.front().expr);
return terms.front().expr->get_postfix_expression_node();
}
// else
return {};
}
// "Simple" means binary (size>0) and not chained (size<2)
struct get_lhs_rhs_if_simple_binary_expression_with_ret {
postfix_expression_node* lhs;
Term* rhs;
};
auto get_lhs_rhs_if_simple_binary_expression_with(lexeme op) const
-> get_lhs_rhs_if_simple_binary_expression_with_ret
{
if (
std::ssize(terms) == 1
&& terms[0].op->type() == op
)
{
return {
get_postfix_expression_node(),
terms.front().expr.get()
};
}
// Else
return { nullptr, nullptr };
}
auto is_result_a_temporary_variable() const -> bool {
if constexpr (std::string_view(Name.value) == "assignment") {
assert(expr);
return expr->is_result_a_temporary_variable();
} else {
if (terms.empty()) {
assert(expr);
return expr->is_result_a_temporary_variable();
} else {
return true;
}
}
}
auto to_string() const
-> std::string
{
assert (expr);
auto ret = expr->to_string();
for (auto const& x : terms) {
assert (x.op);
ret += " " + x.op->to_string();
assert (x.expr);
ret += " " + x.expr->to_string();
}
return ret;
}
// Internals
//
auto position() const
-> source_position
{
assert (expr);
return expr->position();
}
auto visit(auto& v, int depth)
-> void
{
v.start(*this, depth);
assert (expr);
expr->visit(v, depth+1);
for (auto const& x : terms) {
assert (x.op);
v.start(*x.op, depth+1);
assert (x.expr);
x.expr->visit(v, depth+1);
}
v.end(*this, depth);
}
};
struct is_as_expression_node;
using multiplicative_expression_node = binary_expression_node< "multiplicative" , is_as_expression_node >;
using additive_expression_node = binary_expression_node< "additive" , multiplicative_expression_node >;
using shift_expression_node = binary_expression_node< "shift" , additive_expression_node >;
using compare_expression_node = binary_expression_node< "compare" , shift_expression_node >;
using relational_expression_node = binary_expression_node< "relational" , compare_expression_node >;
using equality_expression_node = binary_expression_node< "equality" , relational_expression_node >;
using bit_and_expression_node = binary_expression_node< "bit-and" , equality_expression_node >;
using bit_xor_expression_node = binary_expression_node< "bit-xor" , bit_and_expression_node >;
using bit_or_expression_node = binary_expression_node< "bit-or" , bit_xor_expression_node >;
using logical_and_expression_node = binary_expression_node< "logical-and" , bit_or_expression_node >;
using logical_or_expression_node = binary_expression_node< "logical-or" , logical_and_expression_node >;
using assignment_expression_node = binary_expression_node< "assignment" , logical_or_expression_node >;
struct assignment_expression_lhs_rhs {
postfix_expression_node* lhs;
logical_or_expression_node* rhs;
};
struct expression_statement_node;
struct expression_node
{
static inline std::vector<expression_node*> current_expressions = {}; // TODO: static ?
std::unique_ptr<assignment_expression_node> expr;
int num_subexpressions = 0;
expression_statement_node const* my_statement = {};
expression_node();
// API
//
auto is_fold_expression() const
-> bool
{
// This is a fold-expression if any subexpression
// has an identifier named "..."
return expr->is_fold_expression();
}
auto is_standalone_expression() const
-> bool;
auto subexpression_count() const
-> int
{
return num_subexpressions;
}
auto is_identifier() const
-> bool
{
return expr->is_identifier();
}
auto is_id_expression() const
-> bool
{
return expr->is_id_expression();
}
auto is_unqualified_id() const
-> bool
{
return expr->is_unqualified_id();
}
auto is_expression_list() const
-> bool
{
return expr->is_expression_list();
}
auto get_expression_list() const
-> expression_list_node const*
{
if (is_expression_list()) {
return expr->get_expression_list();
}
return {};
}
auto is_empty_expression_list() const
-> bool;
auto is_literal() const
-> bool
{
return get_literal();
}
auto get_literal() const
-> literal_node const*
{
return expr->get_literal();
}
auto get_lhs_rhs_if_simple_assignment() const
-> assignment_expression_lhs_rhs;
auto to_string() const
-> std::string
{
assert (expr);
return expr->to_string();
}
// Internals
//
auto position() const -> source_position
{
assert (expr);
return expr->position();
}
auto visit(auto& v, int depth) -> void
{
v.start(*this, depth);
assert (expr);
expr->visit(v, depth+1);
v.end(*this, depth);
}
};
template<
String Name,
typename Term
>
binary_expression_node<Name, Term>::binary_expression_node() {
if (!expression_node::current_expressions.empty()) {
my_expression = expression_node::current_expressions.back();
}
}
template<
String Name,
typename Term
>
auto binary_expression_node<Name, Term>::is_standalone_expression() const
-> bool
{
return
my_expression
&& my_expression->is_standalone_expression()
;
}
enum class passing_style : u8 { in=0, in_ref, copy, inout, out, move, forward, forward_ref, invalid };
auto to_passing_style(token const& t) -> passing_style {
if (t.type() == lexeme::Identifier) {
if (t == "in" ) { return passing_style::in; }
if (t == "in_ref" ) { return passing_style::in_ref; }
if (t == "copy" ) { return passing_style::copy; }
if (t == "inout" ) { return passing_style::inout; }
if (t == "out" ) { return passing_style::out; }
if (t == "move" ) { return passing_style::move; }
if (t == "forward") { return passing_style::forward; }
if (t == "forward_ref") { return passing_style::forward_ref; }
}
return passing_style::invalid;
}
auto to_string_view(passing_style pass) -> std::string_view {
switch (pass) {
break;case passing_style::in : return "in";
break;case passing_style::in_ref : return "in_ref";
break;case passing_style::copy : return "copy";
break;case passing_style::inout : return "inout";
break;case passing_style::out : return "out";
break;case passing_style::move : return "move";
break;case passing_style::forward : return "forward";
break;case passing_style::forward_ref: return "forward_ref";
break;default : return "INVALID passing_style";
}
}
struct expression_list_node
{
token const* open_paren = {};
token const* close_paren = {};
bool inside_initializer = false;
bool default_initializer = false;
struct term {
passing_style pass = {};
std::unique_ptr<expression_node> expr;
auto visit(auto& v, int depth) -> void
{
v.start(*this, depth);
assert(expr);
expr->visit(v, depth+1);
v.end(*this, depth);
}
};
std::vector< term > expressions;
// API
//
auto is_empty() const
-> bool
{
return expressions.empty();
}
auto is_fold_expression() const
-> bool
{
// This is a fold-expression if any subexpression
// has an identifier named "..."
auto ret = false;
for (auto& x : expressions) {
ret |= x.expr->is_fold_expression();
}
return ret;
}
auto to_string() const
-> std::string
{
auto ret = std::string{};
if (open_paren) {
ret += *open_paren;
}
for (auto& term : expressions) {
ret += term.expr->to_string();
}
if (close_paren) {
ret += *close_paren;
}
return ret;
}
// Internals
//
auto position() const
-> source_position
{
// Make sure this got set
assert (open_paren);
return open_paren->position();
}
auto visit(auto& v, int depth)
-> void
{
v.start(*this, depth);
for (auto& x : expressions) {
x.visit(v, depth+1);
}
v.end(*this, depth);
}
};
auto primary_expression_node::is_identifier() const
-> bool
{
return expr.index() == identifier;
}
auto primary_expression_node::is_id_expression() const
-> bool
{
return expr.index() == id_expression;
}
auto primary_expression_node::is_expression_list() const
-> bool
{
return expr.index() == expression_list;
}
auto primary_expression_node::get_expression_list() const
-> expression_list_node const*
{
if (is_expression_list()) {
return std::get<expression_list>(expr).get();
}
return {};
}
auto primary_expression_node::is_literal() const
-> bool
{
return get_literal();
}
auto primary_expression_node::get_literal() const
-> literal_node const*
{
if (auto lit = std::get_if<literal>(&expr)) {
return (*lit).get();
}
// Else
return nullptr;
}
struct expression_statement_node
{
static inline std::vector<expression_statement_node*> current_expression_statements = {}; // TODO: static ?
std::unique_ptr<expression_node> expr;
bool has_semicolon = false;
// API
//
auto is_empty_expression_list() const
-> bool
{
assert(expr);
return expr->is_empty_expression_list();
}
auto subexpression_count() const
-> int
{
assert (expr);
return expr->subexpression_count();
}
auto to_string() const
-> std::string
{
assert (expr);
return expr->to_string();
}
// Internals
//
auto position() const
-> source_position
{
assert (expr);
return expr->position();
}
auto visit(auto& v, int depth)
-> void
{
v.start(*this, depth);
assert (expr);
expr->visit(v, depth+1);
v.end(*this, depth);
}
};
auto expression_node::is_standalone_expression() const
-> bool
{
return
my_statement
&& my_statement->subexpression_count() == subexpression_count()
;
}
auto expression_node::is_empty_expression_list() const
-> bool
{
auto expr_list = get_expression_list();
return
expr_list
&& expr_list->is_empty()
;
}
struct capture {
postfix_expression_node* capture_expr;
std::string cap_sym = {};
std::string str = {};
std::string str_suppressed_move = {};
auto operator==(postfix_expression_node* p) { return capture_expr == p; }
};
struct capture_group {
std::vector<capture> members;
auto add(postfix_expression_node* p)
-> void
{
members.push_back({p});
}
auto remove(postfix_expression_node* p)
-> void;
~capture_group();
};
struct postfix_expression_node
{
std::unique_ptr<primary_expression_node> expr;
struct term
{
token const* op;
// This is used if *op is . - can be null
std::unique_ptr<id_expression_node> id_expr = {};
// These are used if *op is [ or ( - can be null
std::unique_ptr<expression_list_node> expr_list = {};
token const* op_close = {};
// This is used if *op is ... to hold the 'last' expression
std::unique_ptr<expression_node> last_expr= {};
};
std::vector<term> ops;
capture_group* cap_grp = {};
~postfix_expression_node();
// API
//
auto is_fold_expression() const
-> bool
{
// This is a fold-expression if any subexpression
// has an identifier named "..."
return expr->is_fold_expression();
}
auto is_identifier() const
-> bool
{
return ops.empty() && expr->is_identifier();
}
auto is_id_expression() const
-> bool
{
return ops.empty() && expr->is_id_expression();
}
auto starts_with_function_call_with_n_parameters(int n) const
-> bool
{
return
std::ssize(ops) >= 1
&& ops.front().op->type() == lexeme::LeftParen
&& ops.front().expr_list
&& std::ssize(ops.front().expr_list->expressions) == n
;
}
auto is_unqualified_id() const
-> bool
{
return ops.empty() && expr->is_unqualified_id();
}