FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cppfront/source/sema.h at bugfix/regex_file · MaxSagebaum/cppfront · GitHub
MaxSagebaum
/
cppfront
Public
forked from
hsutter/cppfront
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
cppfront
/
source
/
sema.h
Copy path
More file actions
More file actions
Latest commit
History
History
History
2907 lines (2601 loc) · 96.8 KB
Breadcrumbs
cppfront
/
source
/
sema.h
Copy path
File metadata and controls
2907 lines (2601 loc) · 96.8 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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-2025 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.
//
===========================================================================
//
Semantic analysis
//
===========================================================================
#
ifndef
CPP2_SEMA_H
#
define
CPP2_SEMA_H
#
include
"
reflect.h
"
namespace
cpp2
{
auto
parser::apply_type_metafunctions
( declaration_node& n )
-> bool
{
assert
(n.
is_type
());
//
Get the reflection state ready to pass to the function
auto
cs = meta::compiler_services{ &errors, &includes, generated_tokens };
auto
rtype = meta::type_declaration{ &n, cs };
return
apply_metafunctions
(
n,
rtype,
[&](std::string
const
& msg) {
error
( msg,
false
); }
);
}
//
-----------------------------------------------------------------------
//
//
Symbol/scope table
//
//
-----------------------------------------------------------------------
//
struct
declaration_sym
{
bool
start =
false
;
declaration_node
const
* declaration = {};
token
const
* identifier = {};
statement_node
const
* initializer = {};
parameter_declaration_node
const
* parameter = {};
bool
member =
false
;
bool
return_param =
false
;
declaration_sym
(
bool
s =
false
,
declaration_node
const
* decl = {},
token
const
* id = {},
statement_node
const
* init = {},
parameter_declaration_node
const
* param = {},
bool
mem =
false
,
bool
ret =
false
)
: start{s}
, declaration{decl}
, identifier{id}
, initializer{init}
, parameter{param}
, member{mem}
, return_param{ret}
{ }
auto
position
()
const
-> source_position
{
assert
(declaration);
return
declaration->
position
();
}
auto
get_token
()
const
-> token
const
*
{
return
identifier;
}
};
struct
identifier_sym
{
enum
kind:
u8
{ use, using_declaration, deactivation } kind_ = use;
bool
standalone_assignment_to =
false
;
bool
is_captured =
false
;
bool
is_after_dot =
false
;
bool
safe_to_move =
true
;
int
safe_to_move_context =
0
;
token
const
* identifier = {};
identifier_sym
(
bool
a,
token
const
* id,
kind k = use,
bool
mv =
true
,
int
mvc =
0
,
bool
after_dot =
false
)
: kind_{k}
, standalone_assignment_to{a}
, is_after_dot{after_dot}
, safe_to_move{mv}
, safe_to_move_context{mvc}
, identifier{id}
{ }
auto
position
()
const
-> source_position
{
assert
(identifier);
return
identifier->
position
();
}
auto
get_token
()
const
-> token
const
*
{
return
identifier;
}
auto
is_use
()
const
-> bool
{
return
kind_ == use;
}
auto
is_using_declaration
()
const
-> bool
{
return
kind_ == using_declaration;
}
auto
is_deactivation
()
const
-> bool
{
return
kind_ == deactivation;
}
};
struct
selection_sym
{
bool
start =
false
;
selection_statement_node
const
* selection = {};
selection_sym
(
bool
s,
selection_statement_node
const
* sel
)
: start{s}
, selection{sel}
{ }
auto
position
()
const
-> source_position
{
assert
(selection);
return
selection->
position
();
}
auto
get_token
()
const
-> token
const
*
{
assert
(selection);
return
selection->
identifier
;
}
};
struct
compound_sym
{
bool
start =
false
;
compound_statement_node
const
* compound = {};
enum
kind :
u8
{ is_scope, is_true, is_false, is_loop } kind_ = is_scope;
compound_sym
(
bool
s,
compound_statement_node
const
* c,
kind k
)
: start{s}
, compound{c}
, kind_{k}
{ }
auto
position
()
const
-> source_position
{
assert
(compound);
return
compound->
position
();
}
auto
get_token
()
const
-> token
const
*
{
return
nullptr
;
}
};
struct
symbol
{
enum
active :
u8
{ declaration=
0
, identifier, selection, compound };
std::variant <
declaration_sym,
identifier_sym,
selection_sym,
compound_sym
> sym;
int
depth = -
1
;
bool
start =
true
;
symbol
(
int
depth, declaration_sym
const
& sym) : sym{sym}, depth{depth}, start{sym.
start
} { }
symbol
(
int
depth, identifier_sym
const
& sym) : sym{sym}, depth{depth}, start{!sym.
is_deactivation
()} { }
symbol
(
int
depth, selection_sym
const
& sym) : sym{sym}, depth{depth}, start{sym.
start
} { }
symbol
(
int
depth, compound_sym
const
& sym) : sym{sym}, depth{depth}, start{sym.
start
} { }
auto
is_declaration
()
const
-> bool {
return
sym.
index
() == declaration; }
auto
is_identifier
()
const
-> bool {
return
sym.
index
() == identifier ; }
auto
is_selection
()
const
-> bool {
return
sym.
index
() == selection ; }
auto
is_compound
()
const
-> bool {
return
sym.
index
() == compound ; }
auto
as_declaration
()
const
-> auto& {
return
std::get<declaration>(sym); }
auto
as_identifier
()
const
-> auto& {
return
std::get<identifier >(sym); }
auto
as_selection
()
const
-> auto& {
return
std::get<selection >(sym); }
auto
as_compound
()
const
-> auto& {
return
std::get<compound >(sym); }
auto
position
()
const
-> source_position
{
switch
(sym.
index
())
{
break
;
case
declaration: {
auto
const
& s = std::get<declaration>(sym);
return
s.
position
();
}
break
;
case
identifier: {
auto
const
& s = std::get<identifier>(sym);
return
s.
position
();
}
break
;
case
selection: {
auto
const
& s = std::get<selection>(sym);
return
s.
position
();
}
break
;
case
compound: {
auto
const
& s = std::get<compound>(sym);
return
s.
position
();
}
break
;
default
:
assert
(
false
&&
"
ICE: illegal symbol state
"
);
return
{
0
,
0
};
}
}
auto
get_token
()
const
-> token
const
*
{
switch
(sym.
index
())
{
break
;
case
declaration: {
auto
const
& s = std::get<declaration>(sym);
return
s.
get_token
();
}
break
;
case
identifier: {
auto
const
& s = std::get<identifier>(sym);
return
s.
get_token
();
}
break
;
case
selection: {
auto
const
& s = std::get<selection>(sym);
return
s.
get_token
();
}
break
;
case
compound: {
auto
const
& s = std::get<compound>(sym);
return
s.
get_token
();
}
break
;
default
:
assert
(
false
&&
"
ICE: illegal symbol state
"
);
return
nullptr
;
}
}
auto
get_global_token_order
()
const
-> index_t
{
if
(
auto
t =
get_token
()) {
return
t->
get_global_token_order
();
}
return
0
;
}
};
//
Keep a list of all token*'s found that are definite first uses
//
of the form "x = expr;" for an uninitialized local variable x,
//
which we will rewrite to construct the local variable.
//
std::vector<token
const
*> definite_initializations;
auto
is_definite_initialization
(token
const
* t)
-> bool
{
return
std::find
(
definite_initializations.
begin
(),
definite_initializations.
end
(),
t
)
!= definite_initializations.
end
();
}
//
Keep a list of all token*'s found that are definite last uses
//
for a local variable or copy or forward parameter x, which we
//
will rewrite to move or forward from the variable.
//
struct
last_use
{
token
const
* t;
bool
is_forward;
bool
safe_to_move;
last_use
(
token
const
* t_,
bool
is_forward_ =
false
,
bool
safe_to_move_ =
true
)
: t{t_}
, is_forward{is_forward_}
, safe_to_move{safe_to_move_}
{ }
bool
operator
==(last_use
const
& that) {
return
t == that.
t
; }
};
std::vector<last_use> definite_last_uses;
auto
is_definite_last_use
(token
const
* t)
-> last_use
const
*
{
auto
iter =
std::find
(
definite_last_uses.
begin
(),
definite_last_uses.
end
(),
t
);
if
(iter != definite_last_uses.
end
()) {
return
&*iter;
}
else
{
return
{};
}
}
//
-----------------------------------------------------------------------
//
//
sema: Semantic analysis
//
//
-----------------------------------------------------------------------
//
class
sema
{
public:
std::vector<error_entry>& errors;
stable_vector<symbol> symbols;
index_t
global_token_counter =
1
;
std::vector<selection_statement_node
const
*> active_selections;
std::vector<iteration_statement_node
const
*> active_iterations;
std::vector<declaration_sym
const
*> current_declarations;
struct
declaration_of_t
{
declaration_sym
const
* sym;
bool
in_current_function;
bool
prev_token_was_this =
false
;
declaration_sym
const
* this_param_sym = {};
};
std::unordered_map< token
const
*,
declaration_of_t
> declaration_of;
public:
//
-----------------------------------------------------------------------
//
Constructor
//
//
errors error list
//
sema
(
std::vector<error_entry>& errors_
)
: errors{ errors_ }
{
}
//
Get the declaration of t within the same named function or beyond it
//
For a this parameter, optionally include uses of implicit this
//
auto
get_declaration_of
(
token
const
* t,
bool
look_beyond_current_function =
false
,
bool
include_implicit_this =
false
)
const
-> declaration_sym
const
*
{
if
(!t) {
return
{};
}
return
get_declaration_of
(*t, look_beyond_current_function, include_implicit_this );
}
auto
get_declaration_of
(
token
const
& t,
bool
look_beyond_current_function =
false
,
bool
include_implicit_this =
false
)
const
-> declaration_sym
const
*
{
//
Calculate result using declaration_of[]
auto
result =
static_cast
<declaration_sym
const
*>(
nullptr
);
{
auto
d = declaration_of.
find
(&t);
if
(d != declaration_of.
cend
())
{
//
If we're asked to include implicit this,
//
and t itself is not 'this' and not qualified with 'this.`,
//
and there's a 'this' result available, then use that
if
(
include_implicit_this
&& t !=
"
this
"
&& !d->
second
.
prev_token_was_this
&& d->
second
.
this_param_sym
)
{
result = d->
second
.
this_param_sym
;
}
//
Otherwise just use the main result
else
{
//
assert( d->second.sym && d->second.sym->declaration->has_name(t) );
result = d->
second
.
sym
;
}
}
//
Now we have the lookup result, but based on lookup constraints flags
//
we may decide it's unsuitable for this lookup and not use it...
if
(
result
//
If we were told not to look beyond the current function
&& !look_beyond_current_function
//
And we're not already using the 'this' parameter which is local
&& result != d->
second
.
this_param_sym
//
And this result isn't our own function-local object declaration
&& !(
d->
second
.
sym
->
declaration
->
identifier
->
get_token
() == &t
&& d->
second
.
sym
->
declaration
->
is_object
()
&& d->
second
.
sym
->
declaration
->
parent_is_function
()
)
//
And this result is not in the current function
//
or we weren't in a function to begin with
&& (
!d->
second
.
in_current_function
|| !d->
second
.
sym
->
declaration
->
parent_is_function
()
)
)
{
//
Then don't use the result, return 'not found' instead
result =
nullptr
;
}
}
return
result;
}
auto
is_captured
(token
const
& t)
const
-> bool
{
//
TODO Use 'std::lower_bound' by filtering final positions of 0.
auto
it =
std::find_if
(
symbols.
begin
(),
symbols.
end
(),
[&](symbol
const
& s) ->
bool
{
return
s.
get_global_token_order
() == t.
get_global_token_order
();
});
if
(identifier_sym
const
* sym =
nullptr
;
it != symbols.
end
()
&& (sym = std::get_if<symbol::active::identifier>(&it->
sym
))
&& sym->
is_use
()
)
{
return
sym->
is_captured
;
}
return
false
;
}
//
-----------------------------------------------------------------------
//
Factor out the uninitialized var decl test
//
auto
is_uninitialized_decl
(declaration_sym
const
& sym)
const
-> bool
{
return
sym.
start
&& !(sym.
identifier
&& *sym.
identifier
==
"
this
"
)
&& !sym.
initializer
&& !(sym.
parameter
&& sym.
parameter
->
pass
!= passing_style::out)
;
}
auto
debug_print
(std::ostream& o)
const
-> void
{
o <<
"
---------------------------------------------------------------------------
\n
"
;
o <<
"
declaration_of: size
"
<< declaration_of.
size
() <<
"
\n
"
;
o <<
"
& tok #tok & sym identifier #tok in_curr_fn prev_was_this & this_param_sym
\n
"
;
for
(
auto
& e : declaration_of) {
o <<
"
"
<<
static_cast
<
void
const
*>(e.
first
)
<<
"
"
<<
std::setw
(
4
) << std::right << e.
first
->
get_global_token_order
()
<<
"
->
"
<<
static_cast
<
void
const
*>(e.
second
.
sym
)
<<
"
"
<<
std::setw
(
16
) << (e.
second
.
sym
&& e.
second
.
sym
->
identifier
? e.
second
.
sym
->
identifier
->
as_string_view
() :
"
(null)
"
)
<<
std::setw
(
4
) << std::right << (e.
second
.
sym
&& e.
second
.
sym
->
identifier
? e.
second
.
sym
->
identifier
->
get_global_token_order
() :
0
)
<<
"
"
<<
std::setw
(
10
) << std::left << e.
second
.
in_current_function
<<
"
"
<<
std::setw
(
13
) << e.
second
.
prev_token_was_this
<<
"
"
<<
static_cast
<
void
const
*>(e.
second
.
this_param_sym
)
<<
"
\n
"
;
}
o <<
"
\n
---------------------------------------------------------------------------
\n
"
;
o <<
"
symbols: size
"
<< symbols.
size
() <<
"
\n
"
;
o <<
"
idx tok# & symbol dep
\n
"
;
for
(
auto
i =
0
;
auto
const
& s : symbols)
{
o <<
std::setw
(
6
) << std::right << i++ <<
"
|
"
;
o <<
std::setw
(
6
) << std::right << s.
get_global_token_order
() <<
"
|
"
;
o <<
std::setw
(
16
) << std::right <<
static_cast
<
void
const
*>(&s) <<
"
|
"
;
o <<
std::setw
(
3
) << std::right << s.
depth
<<
"
|
"
;
o <<
std::setw
(s.
depth
*
2
+
1
) <<
"
"
;
switch
(s.
sym
.
index
()) {
break
;
case
symbol::active::declaration: {
auto
const
& sym = std::get<symbol::active::declaration>(s.
sym
);
assert
(sym.
declaration
);
if
(sym.
declaration
->
is_function
()) {
if
(sym.
start
) {
o <<
"
function
"
;
}
else
{
o <<
"
/function
"
;
}
}
else
if
(sym.
declaration
->
is_object
()) {
if
(sym.
start
) {
o <<
"
var
"
;
}
else
{
o <<
"
/var
"
;
}
}
else
if
(sym.
declaration
->
is_type
()) {
if
(sym.
start
) {
o <<
"
type
"
;
}
else
{
o <<
"
/type
"
;
}
}
else
if
(sym.
declaration
->
is_namespace
()) {
if
(sym.
start
) {
o <<
"
namespace
"
;
}
else
{
o <<
"
/namespace
"
;
}
}
if
(
/*
sym.start &&
*/
sym.
identifier
) {
o << sym.
identifier
->
to_string
();
}
if
(
is_uninitialized_decl
(sym)) {
o <<
"
*** UNINITIALIZED
"
;
}
}
break
;
case
symbol::active::identifier: {
auto
const
& sym = std::get<symbol::active::identifier>(s.
sym
);
assert
(sym.
identifier
);
if
(last_use
const
* use =
nullptr
;
sym.
is_use
()
&& (use =
is_definite_last_use
(sym.
identifier
))
)
{
o <<
"
***
"
<< sym.
identifier
->
position
().
to_string
()
<<
"
DEFINITE LAST
"
<< (use->
is_forward
?
"
FORWARDING
"
:
"
POTENTIALLY MOVING
"
)
<<
"
USE OF
"
;
}
if
(
sym.
is_use
()
&&
is_definite_initialization
(sym.
identifier
)
)
{
o <<
"
***
"
<< sym.
identifier
->
position
().
to_string
()
<<
"
DEFINITE INITIALIZATION OF
"
;
}
else
if
(sym.
standalone_assignment_to
) {
o <<
"
*** assignment to
"
;
}
else
{
o <<
"
*** use of
"
;
}
o << sym.
identifier
->
to_string
();
}
break
;
case
symbol::active::selection: {
auto
const
& sym = std::get<symbol::active::selection>(s.
sym
);
if
(!sym.
start
) {
o <<
"
/
"
;
}
o <<
"
selection
"
;
}
break
;
case
symbol::active::compound: {
auto
const
& sym = std::get<symbol::active::compound>(s.
sym
);
if
(!sym.
start
) {
o <<
"
/
"
;
//
--scope_depth;
}
if
(sym.
kind_
== sym.
is_true
) {
o <<
"
true branch
"
;
}
else
if
(sym.
kind_
== sym.
is_false
) {
o <<
"
false branch
"
;
}
else
if
(sym.
kind_
== sym.
is_loop
) {
o <<
"
loop
"
;
}
else
{
o <<
"
scope
"
;
}
}
break
;
default
:
o <<
"
ERROR
"
;
}
o <<
"
\n
"
;
}
}
//
-----------------------------------------------------------------------
//
Apply local first- and last-use rules
//
auto
apply_local_rules
()
-> bool
{
auto
ret =
true
;
//
-----------------------------------------------------------------------
//
Helpers for readability
//
It's an uninitialized variable (incl. named return values) if it's
//
a non-namespace-scope non-parameter object with no initializer
//
auto
is_uninitialized_variable_decl = [&](symbol
const
& s)
-> declaration_sym
const
*
{
if
(
auto
const
* sym = std::get_if<symbol::active::declaration>(&s.
sym
)) {
assert
(sym);
if
(
is_uninitialized_decl
(*sym)) {
if
(
sym->
declaration
->
is_object
()
&& !sym->
declaration
->
parent_is_namespace
()
)
{
return
sym;
}
else
{
return
{};
}
}
}
return
{};
};
//
It's a local (incl. named return value or copy or move or forward parameter)
//
auto
is_local_declaration = [&](symbol
const
& s)
-> declaration_sym
const
*
{
if
(
auto
const
* sym = std::get_if<symbol::active::declaration>(&s.
sym
)) {
if
(
sym->
start
&& sym->
declaration
->
is_object
()
)
{
//
Must be in function scope
if
(sym->
declaration
->
parent_is_function
()) {
return
sym;
}
else
{
return
{};
}
}
}
return
{};
};
//
-----------------------------------------------------------------------
//
Function logic: For each entry in the table...
//
for
(
auto
sympos = unchecked_narrow<
int
>(
std::ssize
(symbols) -
1
); sympos >=
0
; --sympos)
{
//
If this is an uninitialized local variable,
//
ensure it is definitely initialized and tag those initializations
//
if
(
auto
decl =
is_uninitialized_variable_decl
(symbols[sympos])) {
assert
(
decl->
identifier
&& !decl->
initializer
);
ret = ret
&&
ensure_definitely_initialized
(decl, sympos+
1
, symbols[sympos].
depth
)
;
}
//
If this is a copy, move, or forward parameter or a local variable,
//
identify and tag its definite last uses to `std::move` from them
//
If it's some other parameter, just check that it is used
//
if
(
auto
decl =
is_local_declaration
(symbols[sympos])) {
assert
(decl->
identifier
);
find_definite_last_uses
(
decl->
identifier
,
sympos,
decl->
parameter
? std::optional{decl->
parameter
->
pass
} : std::optional<passing_style>{},
decl->
parameter
);
}
}
return
ret;
}
private:
//
Find the definite last uses for local variable *id starting at the
//
given position and depth in the symbol/scope table
//
auto
find_definite_last_uses
(
token
const
* id,
int
pos,
std::optional<passing_style> pass,
bool
is_parameter
)
const
-> void
{
auto
is_a_use = [&](identifier_sym
const
* sym) ->
bool
{
assert
(!sym || sym->
identifier
);
declaration_sym
const
* decl =
nullptr
;
return
sym
&& sym->
is_use
()
&& (
*sym->
identifier
== *id
//
For 'this', do include member names with implicit 'this.'
|| (
*id ==
"
this
"
&& (decl =
get_declaration_of
(sym->
get_token
(),
false
,
true
))
&& decl->
identifier
&& *decl->
identifier
==
"
this
"
)
);
};
auto
i = pos +
1
;
struct
pos_range
{
bool
is_loop;
int
first;
int
last =
0
;
pos_range
(
bool
l,
int
f) : is_loop{l}, first{f} { }
bool
within
(
int
x)
const
{
return
first <= x && x <= last; }
bool
skip
()
const
{
return
!is_loop; }
};
//
Ranges of positions which includes non-nested
//
1. Iteration statements (a use isn't a last use)
//
2. Ranges to skip (a last use can't be found in these)
//
- Function expressions (except in a capture)
//
- Where id is hidden by another declaration
auto
pos_ranges = std::vector<pos_range>{{
false
,
0
}};
//
Keep sentinel for simpler code
auto
skip_hidden_name = [&](
bool
record_pos_range) ->
bool
{
auto
skip_to = [&](token
const
* identifier_end)
{
//
Can afford to just skip id and not member names
//
because in Cpp2 you can't shadow member names
//
//
TODO When local types are supported
//
consider where 'this' is implicitly declared
//
For example, see https://cpp2.godbolt.org/z/onfW6hns1
if
(record_pos_range) {
pos_ranges.
emplace_back
(
false
, i -
1
);
}
++i;
identifier_sym
const
* sym =
nullptr
;
while
(
i <
std::ssize
(symbols)
&& (
!(sym = std::get_if<symbol::active::identifier>(&symbols[i].
sym
))
|| sym->
identifier
!= identifier_end
)
)
{
++i;
}
assert
(sym->
identifier
== identifier_end && sym->
is_deactivation
());
if
(record_pos_range) {
pos_ranges.
back
().
last
= i;
}
};
if
(
auto
decl = std::get_if<symbol::active::declaration>(&symbols[i].
sym
);
decl
&& decl->
start
&& decl->
identifier
&& *decl->
identifier
== *id
&& *decl->
identifier
!=
"
_
"
)
{
skip_to
(decl->
identifier
);
return
true
;
}
else
if
(
auto
sym = std::get_if<symbol::active::identifier>(&symbols[i].
sym
);
sym
&& sym->
is_using_declaration
()
&& sym->
identifier
&& *sym->
identifier
== *id
)
{
skip_to
(sym->
identifier
);
return
true
;
}
return
false
;
};
auto
skip_function_expression = [&]() ->
bool
{
if
(
auto
decl = std::get_if<symbol::active::declaration>(&symbols[i].
sym
);
decl
&& decl->
start
&& decl->
declaration
->
is_function_expression
()
)
{
//
Record the skipped subranges without captures
auto
function_expression_end = decl->
declaration
;
pos_ranges.
emplace_back
(
false
, i -
1
);
++i;
while
(
i <
std::ssize
(symbols)
&& (
!(decl = std::get_if<symbol::active::declaration>(&symbols[i].
sym
))
|| decl->
declaration
!= function_expression_end
)
)
{
if
(
skip_hidden_name
(
false
)) {
continue
;
}
else
if
(
auto
sym = std::get_if<symbol::active::identifier>(&symbols[i].
sym
);
is_a_use
(sym)
&& sym->
is_captured
)
{
pos_ranges.
back
().
last
= i -
1
;
pos_ranges.
emplace_back
(
false
, i +
1
);
}
++i;
}
assert
(decl && decl->
declaration
== function_expression_end && !decl->
start
);
pos_ranges.
back
().
last
= i;
return
true
;
}
return
false
;
};
//
Scan forward to the end of this scope
auto
found_end_of_our_initialization =
false
;
for
(
auto
start_depth = symbols[pos].
depth
;
i <
std::ssize
(symbols)
&& symbols[i].
depth
>= start_depth;
++i
)
{
//
While we're here, if this is a non-parameter local, check for
//
any uses before the end of the initializer
if
(
!is_parameter
&& !found_end_of_our_initialization
)
{
if
(symbols[i].
depth
== start_depth)
{
if
(
auto
decl = std::get_if<symbol::active::declaration>(&symbols[i].
sym
);
decl
&& decl->
declaration
->
is_object
()
&& decl->
declaration
->
has_name
(*id)
&& !decl->
start
)
{
found_end_of_our_initialization =
true
;
}
}
if
(
auto
sym = std::get_if<symbol::active::identifier>(&symbols[i].
sym
);
sym
&& !sym->
is_after_dot
&&
is_a_use
(sym)
)
{
assert
(sym->
identifier
);
errors.
emplace_back
(
sym->
identifier
->
position
(),
"
local variable
"
+ sym->
identifier
->
to_string
()
+
"
cannot be used in its own initializer
"
);
}
}
if
(
skip_function_expression
()
||
skip_hidden_name
(
true
)
)
{
continue
;
}
//
Record the loops
else
if
(
auto
sym = std::get_if<symbol::active::identifier>(&symbols[i].
sym
);
sym
&& sym->
identifier
&& (
*sym->
identifier
==
"
for
"
|| *sym->
identifier
==
"
while
"
|| *sym->
identifier
==
"
do
"
)
)
{
auto
loop_depth = symbols[i].
depth
;
auto
loop_id = sym->
identifier
;
//
If id is the loop parameter, this is its end
if
(
*loop_id ==
"
for
"
&& sym->
is_deactivation
()
)
{
assert
(symbols[i].
depth
== start_depth &&
"
Messed up in a nested loop
"
);
++i;
break
;
}
assert
(symbols[i].
start
);
pos_ranges.
emplace_back
(
true
, i);
//
Scan forward to the end of this loop
++i;
while
(
i <
std::ssize
(symbols)
&& (
symbols[i].
depth
> loop_depth
|| !(sym = std::get_if<symbol::active::identifier>(&symbols[i].
sym
))
|| sym->
identifier
!= loop_id
)
)
{
if
(
skip_function_expression
()
||
skip_hidden_name
(
true
)
)
{
continue
;
}
++i;
}
assert
(sym && sym->
identifier
== loop_id && sym->
is_deactivation
());
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL