FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
cppfront/source/cppfront.cpp at main · richardsonjf/cppfront · GitHub
richardsonjf
/
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
/
cppfront.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
2641 lines (2251 loc) · 93.8 KB
Breadcrumbs
cppfront
/
source
/
cppfront.cpp
Copy path
File metadata and controls
2641 lines (2251 loc) · 93.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 (c) Herb Sutter
//
SPDX-License-Identifier: CC-BY-NC-ND-4.0
//
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//
THE SOFTWARE.
//
===========================================================================
//
cppfront
//
===========================================================================
#
include
"
sema.h
"
#
include
<
iostream
>
#
include
<
cstdio
>
namespace
cpp2
{
//
Defined out of line here just to avoid bringing <iostream> into the headers,
//
so that we can't accidentally start depending on iostreams in the compiler body
auto
cmdline_processor::print
(std::string_view s,
int
width) -> void
{
if
(width >
0
) {
std::cout <<
std::setw
(width) << std::left;
}
std::cout << s;
}
//
-----------------------------------------------------------------------
//
//
Stringingizing helpers
//
//
-----------------------------------------------------------------------
auto
pad
(
int
padding) -> std::string_view
{
static
std::string indent_str =
std::string
(
1024
,
'
'
);
//
"1K should be enough for everyone"
if
(padding <
1
) {
return
"
"
;
}
return
{
indent_str.
c_str
(),
as<
size_t
>(
std::min
( padding, as<
int
>(
std::ssize
(indent_str))) )
};
}
//
-----------------------------------------------------------------------
//
//
positional_printer: a Syntax 1 pretty printer
//
//
-----------------------------------------------------------------------
//
static
auto
flag_clean_cpp1 =
false
;
static
cmdline_processor::register_flag
cmd_noline
(
1
,
"
clean-cpp1
"
,
"
Emit clean Cpp1 without #line directives
"
,
[]{ flag_clean_cpp1 =
true
; }
);
static
auto
flag_cpp2_only =
false
;
static
cmdline_processor::register_flag
cmd_cpp2_only
(
1
,
"
pure-cpp2
"
,
"
Allow Cpp2 syntax only
"
,
[]{ flag_cpp2_only =
true
; }
);
static
auto
flag_safe_null_pointers =
false
;
static
cmdline_processor::register_flag
cmd_safe_null_pointers
(
2
,
"
null-checks
"
,
"
Enable null safety contract checks
"
,
[]{ flag_safe_null_pointers =
true
; }
);
static
auto
flag_safe_subscripts =
false
;
static
cmdline_processor::register_flag
cmd_safe_subscripts
(
2
,
"
subscript-checks
"
,
"
Enable subscript bounds safety contract checks
"
,
[]{ flag_safe_subscripts =
true
; }
);
static
auto
flag_use_source_location =
false
;
static
cmdline_processor::register_flag
cmd_enable_source_info
(
2
,
"
add-source-info
"
,
"
Enable source locations for contract checks
"
,
[]{ flag_use_source_location =
true
; }
);
struct
text_with_pos
{
std::string text;
source_position pos;
text_with_pos
(std::string
const
& t, source_position p) : text{t}, pos{p} { }
};
class
positional_printer
{
//
Core information
std::ofstream out = {};
//
Cpp1 syntax output file
std::string filename = {};
std::vector<comment>
const
* pcomments = {};
//
Cpp2 comments data
source_position curr_pos = {};
//
current (line,col) in output
int
next_comment =
0
;
//
index of the next comment not yet printed
bool
last_was_cpp2 =
false
;
struct
req_act_info
{
colno_t
requested;
colno_t
offset;
req_act_info
(
colno_t
r,
colno_t
o) : requested{r}, offset{o} { }
};
struct
{
lineno_t
line = {};
std::vector<req_act_info> requests = {};
} prev_line_info = {};
//
Position override information
source_position preempt_pos = {};
//
use this position instead of the next supplied one
int
pad_for_this_line =
0
;
//
extra padding to add/subtract for this line only
bool
ignore_align =
false
;
int
ignore_align_indent =
0
;
lineno_t
ignore_align_lineno =
0
;
bool
enable_indent_heuristic =
true
;
//
Modal information
bool
declarations_only =
true
;
//
print only declarations in the first pass
std::vector<std::string*> emit_string_targets;
//
option to emit to string instead of out file
std::vector<std::vector<text_with_pos>*> emit_text_chunks_targets;
//
similar for vector<text_pos>
enum
class
target_type
{ string, chunks };
std::vector<target_type> emit_target_stack;
//
to interleave them sensibly
//
-----------------------------------------------------------------------
//
Print text
//
auto
print
( std::string_view s, source_position pos = source_position{}) -> void
{
//
If the caller is capturing this output, emit to the
//
current target instead and skip most positioning logic
if
(!emit_target_stack.
empty
())
{
//
If capturing to a string, emit to the specified string
if
(emit_target_stack.
back
() == target_type::string) {
assert
(!emit_string_targets.
empty
());
*emit_string_targets.
back
() += s;
}
//
If capturing to a vector of chunks, emit to that
else
{
assert
(!emit_text_chunks_targets.
empty
());
emit_text_chunks_targets.
back
()->
insert
( emit_text_chunks_targets.
back
()->
begin
(),
text_with_pos
(
std::string
(s), pos) );
}
return
;
}
//
Otherwise, we'll actually print the string to the output file
//
and update our curr_pos position
//
Output the string
out << s;
//
Update curr_pos by finding how many line breaks s contained,
//
and where the last one was which determines our current colno
auto
last_newline = std::string::npos;
//
the last newline we found in the string
auto
newline_pos =
0
;
//
the current newline we found in the string
while
((newline_pos = s.
find
(
'
\n
'
, newline_pos)) != std::string::npos)
{
//
For each line break we find, reset pad and inc current lineno
pad_for_this_line =
0
;
++curr_pos.
lineno
;
last_newline = newline_pos;
++newline_pos;
}
//
Now also adjust the colno
if
(last_newline != std::string::npos) {
//
If we found a newline, it's the distance from the last newline to EOL
curr_pos.
colno
= s.
length
() - last_newline;
}
else
{
//
Else add the length of the string
curr_pos.
colno
+= s.
length
();
}
}
//
-----------------------------------------------------------------------
//
Internal helpers
//
Start a new line if we're not in col 1 already
//
auto
ensure_at_start_of_new_line
() -> void
{
if
(curr_pos.
colno
>
1
) {
auto
old_pos = curr_pos;
print
(
"
\n
"
);
assert
(curr_pos.
lineno
== old_pos.
lineno
+
1
);
assert
(curr_pos.
colno
==
1
);
}
}
//
Print a #line directive
//
auto
print_line_directive
(
lineno_t
line ) -> void
{
prev_line_info = { curr_pos.
lineno
, { } };
ensure_at_start_of_new_line
();
//
Not using print() here because this is transparent to the curr_pos
if
(!flag_clean_cpp1) {
out <<
"
#line
"
<< line <<
"
\"
"
<< filename <<
"
2
\"\n
"
;
}
}
//
Catch up with comment/blank lines
//
auto
flush_comments
( source_position pos ) -> void
{
assert
(pcomments);
//
For convenience
auto
& comments = *pcomments;
//
Don't emit comments while in the first declarations-only pass
if
(declarations_only) {
return
;
}
//
Add unprinted comments and blank lines as needed to catch up vertically
//
while
(curr_pos.
lineno
< pos.
lineno
)
{
//
If a comment goes on this line, print it
if
(next_comment <
std::ssize
(comments) && comments[next_comment].
start
.
lineno
== curr_pos.
lineno
)
{
//
For a line comment, start it at the right indentation and print it
//
with a newline end
if
(comments[next_comment].
kind
== comment::comment_kind::line_comment) {
print
(
pad
( comments[next_comment].
start
.
colno
- curr_pos.
colno
+
1
) );
print
( comments[next_comment].
text
);
assert
( comments[next_comment].
text
.
find
(
"
\n
"
) == std::string::npos );
//
we shouldn't have newlines
print
(
"
\n
"
);
}
//
For a stream comment, pad out to its column (if we haven't passed it already)
//
and emit it there
else
{
print
(
pad
( comments[next_comment].
start
.
colno
- curr_pos.
colno
) );
print
( comments[next_comment].
text
);
assert
(curr_pos.
lineno
<= pos.
lineno
);
//
we shouldn't have overshot
}
++next_comment;
}
//
Otherwise, just print a blank line
else
{
print
(
"
\n
"
);
}
}
}
//
Position ourselves as close to pos as possible,
//
and catch up with displaying comments
//
auto
align_to
( source_position pos ) -> void
{
//
Ignoring this logic is used when we're generating new code sections,
//
such as return value structs
if
(ignore_align) {
print
(
pad
( ignore_align_indent - curr_pos.
colno
) );
return
;
}
//
Otherwise, we need to apply our usual alignment logic
//
Catch up with displaying comments
flush_comments
( pos );
//
If we're not on the right line, move forward one line
if
(curr_pos.
lineno
< pos.
lineno
) {
ensure_at_start_of_new_line
();
assert
(curr_pos.
lineno
<= pos.
lineno
);
curr_pos.
lineno
= pos.
lineno
;
//
re-sync
}
//
Finally, align to the target column
if
(curr_pos.
lineno
== pos.
lineno
) {
pos.
colno
=
std::max
(
1
, pos.
colno
+ pad_for_this_line );
print
(
pad
( pos.
colno
- curr_pos.
colno
) );
}
}
public:
//
-----------------------------------------------------------------------
//
Open
//
auto
open
(
std::string cpp1_filename,
std::vector<comment>
const
& comments
)
-> void
{
assert
(!out.
is_open
() && !pcomments &&
"
ICE: tried to call .open twice
"
);
filename = cpp1_filename;
out.
open
(filename);
pcomments = &comments;
}
auto
is_open
() -> bool {
if
(out.
is_open
()) {
assert
(pcomments &&
"
ICE: if out.is_open, pcomments should also be set
"
);
}
return
out.
is_open
();
}
//
-----------------------------------------------------------------------
//
Abandon: close and delete
//
auto
abandon
() -> void
{
if
(!out.
is_open
()) {
return
;
}
out.
close
();
std::remove
(filename.
c_str
());
}
//
-----------------------------------------------------------------------
//
Print extra text and don't track positions
//
Used for Cpp2 boundary comment and prelude
//
auto
print_extra
( std::string_view s ) -> void
{
assert
(
is_open
() &&
"
ICE: printer must be open before printing
"
);
print
( s );
}
//
-----------------------------------------------------------------------
//
Print a Cpp1 line, which should be at lineno
//
auto
print_cpp1
( std::string_view s,
lineno_t
line ) -> void
{
assert
(
is_open
() &&
"
ICE: printer must be open before printing
"
);
//
Keep track of whether the last thing we printed was Cpp2
last_was_cpp2 =
false
;
//
Always start a Cpp1 line on its own new line
ensure_at_start_of_new_line
();
//
If we are out of sync with the current logical line number,
//
emit a #line directive to re-sync
if
(curr_pos.
lineno
!= line) {
print_line_directive
( line );
curr_pos.
lineno
= line;
}
//
Print the line
assert
(curr_pos.
colno
==
1
);
print
( s );
print
(
"
\n
"
);
}
//
-----------------------------------------------------------------------
//
Start a new Cpp2 section, which should start at lineno
//
auto
start_cpp2
(
lineno_t
line) -> void
{
assert
(
is_open
() &&
"
ICE: printer must be open before printing
"
);
//
Because the blank/comment lines before a Cpp2 code section are part
//
of the Cpp2 section, and not printed in thedeclarations-only pass
if
(!last_was_cpp2 && declarations_only) {
print
(
"
\n
"
);
}
//
Keep track of whether the last thing we printed was Cpp2
last_was_cpp2 =
true
;
//
Always start a Cpp2 section on its own new line
ensure_at_start_of_new_line
();
//
If we are out of sync with the current logical line number,
//
emit a #line directive to re-sync
if
(curr_pos.
lineno
!= line) {
print_line_directive
( line );
curr_pos.
lineno
= line;
}
assert
(curr_pos.
colno
==
1
);
}
//
-----------------------------------------------------------------------
//
Print a Cpp2 item, which should be at pos
//
auto
print_cpp2
(std::string_view s, source_position pos) -> void
{
assert
(
is_open
() &&
"
ICE: printer must be open before printing
"
);
//
Keep track of whether the last thing we printed was Cpp2
//
Note: We should have been switched to Cpp2 with a `start_cpp2` call
assert
(last_was_cpp2 &&
"
ICE: didn't call start_cpp2 to begin a Cpp2 section
"
);
last_was_cpp2 =
true
;
//
Skip alignment work if we're capturing emitted text
if
(emit_target_stack.
empty
())
{
//
Remember where we are
auto
last_pos = curr_pos;
//
We may want to adjust the position based on (1) a position preemption request
//
or else (2) to repeat a similar adjustment we discovered on the previous line
auto
adjusted_pos = pos;
//
(1) See if there's a position preemption request, if so use it up
//
For now, the preempt position use cases are about overriding colno
//
and only on the same line. In the future, we might have more use cases.
if
(preempt_pos != source_position{}) {
if
(preempt_pos.
lineno
== pos.
lineno
) {
adjusted_pos.
colno
= preempt_pos.
colno
;
}
preempt_pos = {};
assert
(preempt_pos == source_position{});
}
//
(2) Otherwise, see if there's a previous line's offset to repeat
//
If we moved to a new line, then this is the first
//
non-comment non-whitespace text on the new line
else
if
(last_pos.
lineno
== pos.
lineno
-
1
&& enable_indent_heuristic) {
//
If the last line had a request for this colno, remember its actual offset
constexpr
int
sentinel = -
100
;
auto
last_line_offset = sentinel;
for
(
auto
i =
0
;
i <
std::ssize
(prev_line_info.
requests
) && prev_line_info.
requests
[i].
requested
<= pos.
colno
;
++i
)
{
if
(prev_line_info.
requests
[i].
requested
== pos.
colno
) {
last_line_offset = prev_line_info.
requests
[i].
offset
;
break
;
}
}
//
If there was one, apply the actual column number offset
if
(last_line_offset > sentinel) {
adjusted_pos.
colno
+= last_line_offset;
}
}
enable_indent_heuristic =
true
;
//
If we're changing lines, start accumulating this new line's request/actual adjustment info
if
(last_pos.
lineno
< adjusted_pos.
lineno
) {
prev_line_info = { curr_pos.
lineno
, { } };
}
align_to
(adjusted_pos);
//
Remember the requested and actual offset columns for this item
prev_line_info.
requests
.
push_back
(
req_act_info
( pos.
colno
/*
requested
*/
, curr_pos.
colno
/*
actual
*/
- pos.
colno
) );
}
print
(s, pos);
}
//
-----------------------------------------------------------------------
//
Position override control functions
//
//
Use this position instead of the next supplied one
//
Useful when Cpp1 syntax is emitted in a different order/verbosity
//
than Cpp2 such as with declarations
//
auto
preempt_position
(source_position pos) -> void
{
preempt_pos = pos;
}
//
Add (or, if negative, subtract) padding for the current line only
//
auto
add_pad_in_this_line
(
colno_t
extra) -> void
{
pad_for_this_line += extra;
}
//
Enable indent heuristic for just this line
//
auto
disable_indent_heuristic_for_next_text
() -> void {
enable_indent_heuristic =
false
;
}
//
Ignore position information, usually when emitting generated code
//
such as generated multi-return type structs
//
auto
ignore_alignment
(
bool
ignore,
int
indent =
0
) -> void
{
//
We'll only ever call this in local non-nested true/false pairs.
//
If we ever want to generalize (support nesting, or make it non-brittle),
//
wrap this in a push/pop stack.
if
(ignore) {
ignore_align =
true
;
ignore_align_indent = indent;
ignore_align_lineno = curr_pos.
lineno
;
//
push state
}
else
{
ignore_align =
false
;
ignore_align_indent =
0
;
if
(ignore_align_lineno != curr_pos.
lineno
) {
ensure_at_start_of_new_line
();
print_line_directive
(ignore_align_lineno+
1
);
}
curr_pos.
lineno
= ignore_align_lineno+
1
;
//
pop state
}
}
//
-----------------------------------------------------------------------
//
Modal state control functions
//
//
In the first pass we will print only declarations (the default)
//
For the second pass this function enables printing definitions
//
auto
enable_definitions
() -> void {
declarations_only =
false
;
}
auto
doing_declarations_only
()
const
-> bool {
return
declarations_only;
}
//
Provide an option to store to a given string instead, which is
//
useful for capturing Cpp1-formatted output for generated code
//
auto
emit_to_string
( std::string* target =
nullptr
) -> void {
if
(target) {
emit_string_targets.
push_back
( target );
emit_target_stack.
push_back
(target_type::string);
}
else
{
emit_string_targets.
pop_back
();
emit_target_stack.
pop_back
();
}
}
//
Provide an option to store to a vector<text_with_pos>, which is
//
useful for postfix expression which have to mix unwrapping operators
//
with emitting sub-elements such as expression lists
//
auto
emit_to_text_chunks
( std::vector<text_with_pos>* target =
nullptr
) -> void {
if
(target) {
emit_text_chunks_targets.
push_back
( target );
emit_target_stack.
push_back
(target_type::chunks);
}
else
{
emit_text_chunks_targets.
pop_back
();
emit_target_stack.
pop_back
();
}
}
};
//
-----------------------------------------------------------------------
//
//
cppfront: a compiler instance
//
//
-----------------------------------------------------------------------
//
class
cppfront
{
std::string sourcefile;
std::vector<error> errors;
//
For building
//
cpp2::source source;
cpp2::tokens tokens;
cpp2::parser parser;
cpp2::sema sema;
bool
source_loaded =
true
;
bool
last_postfix_expr_was_pointer =
false
;
bool
violates_bounds_safety =
false
;
bool
violates_initialization_safety =
false
;
bool
suppress_move_from_last_use =
false
;
//
For lowering
//
positional_printer printer;
bool
in_definite_init =
false
;
bool
in_parameter_list =
false
;
std::string function_return_name;
std::vector<parameter_declaration_list_node*> function_returns;
parameter_declaration_list_node single_anon;
//
special value - hack for now to note single-anon-return type kind in this function_returns working list
std::vector<std::string> function_requires_conditions;
public:
//
-----------------------------------------------------------------------
//
Constructor
//
//
filename the source file to be processed
//
cppfront
(std::string
const
& filename)
: sourcefile{ filename }
, source{ errors }
, tokens{ errors }
, parser{ errors }
, sema{ errors }
{
//
"Constraints enable creativity in the right directions"
//
sort of applies here
//
if
(!sourcefile.
ends_with
(
"
.cpp2
"
))
{
errors.
emplace_back
(
source_position
(-
1
, -
1
),
"
source filename must end with .cpp2:
"
+ sourcefile
);
}
//
Load the program file into memory
//
else
if
(!source.
load
(sourcefile))
{
if
(errors.
empty
()) {
errors.
emplace_back
(
source_position
(-
1
, -
1
),
"
file not found:
"
+ sourcefile
);
}
source_loaded =
false
;
}
else
{
//
Tokenize
//
tokens.
lex
(source.
get_lines
());
//
Parse
//
try
{
for
(
auto
const
& [line, entry] : tokens.
get_map
()) {
if
(!parser.
parse
(entry)) {
errors.
emplace_back
(
source_position
(line,
0
),
"
parse failed for section starting here
"
);
}
}
//
Sema
parser.
visit
(sema);
if
(!sema.
apply_local_rules
()) {
violates_initialization_safety =
true
;
}
}
catch
(std::runtime_error& e) {
errors.
emplace_back
(
source_position
(-
1
, -
1
),
e.
what
()
);
}
}
}
//
-----------------------------------------------------------------------
//
lower_to_cpp1
//
//
Emits the target file with the last '2' stripped -> .cpp
//
auto
lower_to_cpp1
() -> void
{
//
Only lower to Cpp1 if we haven't already encountered errors
if
(!errors.
empty
()) {
return
;
}
//
Now we'll open the .cpp file
printer.
open
(
sourcefile.
substr
(
0
,
std::ssize
(sourcefile) -
1
),
tokens.
get_comments
()
);
//
Only emit extra lines if we actually have Cpp2, because
//
we want pure-Cpp1 files to pass through with zero changes
if
(source.
has_cpp2
()) {
if
(!flag_clean_cpp1) {
printer.
print_extra
(
"
// ----- Cpp2 support -----
\n
"
);
}
if
(flag_use_source_location) {
printer.
print_extra
(
"
#define CPP2_USE_SOURCE_LOCATION Yes
\n
"
);
}
if
(flag_cpp2_only) {
printer.
print_extra
(
"
#define CPP2_USE_MODULES Yes
\n
"
);
}
printer.
print_extra
(
"
#include
\"
cpp2util.h
\"\n\n
"
);
}
auto
map_iter = tokens.
get_map
().
cbegin
();
//
First, echo the non-Cpp2 parts
//
auto
cpp2_found =
false
;
for
(
lineno_t
curr_lineno =
0
;
auto
const
& line : source.
get_lines
()
)
{
//
Skip dummy line we added to make 0-vs-1-based offsets readable
if
(curr_lineno !=
0
)
{
//
If it's a Cpp1 line, emit it
if
(line.
cat
!= source_line::category::cpp2)
{
if
(flag_cpp2_only &&
!line.
text
.
empty
() &&
line.
cat
!= source_line::category::comment &&
line.
cat
!= source_line::category::
import
)
{
if
(line.
cat
== source_line::category::preprocessor) {
errors.
emplace_back
(
source_position
(curr_lineno,
1
),
"
pure-cpp2 switch disables the preprocessor, including #include - use import instead (note: 'import std;' is implicit in -pure-cpp2)
"
);
}
else
{
errors.
emplace_back
(
source_position
(curr_lineno,
1
),
"
pure-cpp2 switch disables Cpp1 syntax
"
);
}
return
;
}
printer.
print_cpp1
( line.
text
, curr_lineno );
}
//
If it's a Cpp2 line...
else
{
cpp2_found =
true
;
//
We should be in a position to emit a set of Cpp2 declarations
if
(map_iter != tokens.
get_map
().
cend
() && map_iter->
first
/*
line
*/
<= curr_lineno)
{
//
We should be here only when we're at exactly the first line of a Cpp2 section
assert
(map_iter->
first
== curr_lineno);
assert
(!map_iter->
second
.
empty
());
//
Get the parse tree for this section and emit each forward declaration
auto
decls = parser.
get_parse_tree
(map_iter->
second
);
for
(
auto
& decl : decls) {
assert
(decl);
//
Treat each declaration as the start of a Cpp2 section (so we get #line)
printer.
start_cpp2
( decl->
position
().
lineno
);
emit
(*decl);
}
++map_iter;
}
}
}
++curr_lineno;
}
//
We can stop here if there's no Cpp2 code -- a file with no Cpp2
//
should have perfect passthrough verifiable with diff, including
//
that we didn't misidentify anything as Cpp2 (even in the
//
presence of nonstandard vendor extensions)
//
if
(!cpp2_found) {
return
;
}
//
If there is Cpp2 code, we have more to do...
//
Next, bring in the Cpp2 helpers
//
if
(!flag_clean_cpp1) {
printer.
print_extra
(
"
\n
//=== Cpp2 definitions ==========================================================
\n\n
"
);
}
//
Next, emit the Cpp2 definitions
//
printer.
enable_definitions
();
for
(
auto
& section : tokens.
get_map
())
{
assert
(!section.
second
.
empty
());
//
Tell the printer that we're starting a Cpp2 section
//
This time, we use the actual first start line of the section which includes
//
the blank/comment lines at the beginning if any (whereas in the first pass
//
we used the line of the first Cpp2 token in the section to skip blanks/comments)
printer.
start_cpp2
( section.
first
/*
lineno
*/
);
//
Get the parse tree for this section and emit each forward declaration
auto
decls = parser.
get_parse_tree
(section.
second
);
for
(
auto
& decl : decls) {
assert
(decl);
emit
(*decl);
}
}
if
(source.
has_cpp2
()) {
//
Always make sure the last line ends with a newline
//
(not really necessary but makes some tools quieter)
//
-- but only if there's any Cpp2, otherwise don't
//
because passing through all-Cpp1 code should always
//
remain diff-identical
printer.
print_extra
(
"
\n
"
);
}
}
//
-----------------------------------------------------------------------
//
//
emit() functions - each emits a kind of node
//
//
The body often mirrors the node's visit() function, unless customization
//
is needed where Cpp1 and Cpp2 have different grammar orders
//
//
-----------------------------------------------------------------------
//
try_emit
//
//
Helper to emit whatever is in a variant where each
//
alternative is a smart pointer
//
template
<
int
I>
auto
try_emit
(
auto
& v,
auto
... more) -> void {
if
(v.
index
() == I) {
auto
const
& alt = std::get<I>(v);
assert
(alt);
emit
(*alt, more...);
}
}
//
-----------------------------------------------------------------------
//
//
A StringLiteral could include captures
//
auto
expand_string_literal
(token
const
& n) -> std::string
{
assert
(n.
type
() == lexeme::StringLiteral);
auto
text =
std::string_view
(n);
auto
length = n.
length
();
assert
(
std::ssize
(text) == length);
assert
(length >=
2
);
assert
(text.
back
() ==
'
"
'
);
auto
pos =
0
;
auto
ret = std::string{};
//
the return string we're going to build
auto
current_start =
0
;
//
the current offset before which the string has been into ret
//
Skip prefix to first non-" character
while
(pos < length && text[pos] !=
'
"
'
) {
++pos;
}
assert
(pos < length && text[pos] ==
'
"
'
);
++pos;
//
Now we're on the first character of the string itself
for
( ; pos < length && (text[pos] !=
'
"
'
|| text[pos-
1
] ==
'
\\
'
); ++pos)
{
//
Find the next )$
if
(text[pos] ==
'
$
'
&& text[pos-
1
] ==
'
)
'
)
{
//
Scan back to find the matching (
auto
paren_depth =
1
;
auto
open = pos -
2
;
//
"next" in the string is the "last" one encountered in the backwards scan
auto
last_nonwhitespace =
'
\0
'
;
for
( ; text[open] !=
'
"
'
|| text[open-
1
] !=
'
\\
'
; --open)
{
if
(text[open] ==
'
)
'
) {
++paren_depth;
}
else
if
(text[open] ==
'
(
'
) {
--paren_depth;
if
(paren_depth ==
0
) {
break
;
}
}
else
if
(
(text[open] ==
'
+
'
&& text[open -
1
] ==
'
+
'
) ||
(text[open] ==
'
-
'
&& text[open -
1
] ==
'
-
'
)
)
{
errors.
emplace_back
(
source_position
( n.
position
().
lineno
, n.
position
().
colno
+ pos ),
"
a string interpolation expression may not contain ++ or --
"
);
return
"
"
;
}
else
if
(
(text[open] ==
'
*
'
|| text[open] ==
'
&
'
|| text[open] ==
'
~
'
) &&
!
isspace
(text[open -
1
]) &&
!
isalnum
(last_nonwhitespace) && last_nonwhitespace !=
'
(
'
)
{
errors.
emplace_back
(
source_position
( n.
position
().
lineno
, n.
position
().
colno
+ pos ),
"
a string interpolation expression may not contain unary & * or ~
"
);
return
"
"
;
}
if
(!
std::isspace
(text[open])) {
last_nonwhitespace = text[open];
}
}
if
(text[open] ==
'
"
'
)
{
errors.
emplace_back
(
source_position
( n.
position
().
lineno
, n.
position
().
colno
+ pos ),
"
no matching ( for string interpolation ending in )$
"
);
return
"
"
;
}
assert
(text[open] ==
'
(
'
);
//
'open' is now at the matching (
//
Put the next non-interpolated chunk straight into ret
if
(current_start >
0
) {
ret +=
'
"
'
;
}
ret += text.
substr
(current_start, open - current_start);
ret +=
'
"
'
;
//
Then put interpolated chunk into ret
ret +=
"
+ cpp2::to_string
"
;
ret += text.
substr
(open, pos - open);
ret +=
"
+
"
;
current_start = pos+
1
;
}
}
//
Now we should be on the the final " closing the string
assert
(pos == length-
1
&& text[pos] ==
'
"
'
);
//
Put the final non-interpolated chunk straight into ret
if
(current_start >
0
) {
ret +=
'
"
'
;
}
ret += text.
substr
(current_start);
return
ret;
}
//
-----------------------------------------------------------------------
//
auto
emit
(token
const
& n) -> void
{
if
(n ==
"
new
"
) {
printer.
print_cpp2
(
"
cpp2_new
"
, n.
position
());
}
else
if
(n.
type
() == lexeme::StringLiteral) {
printer.
print_cpp2
(
expand_string_literal
(n), n.
position
() );
}
else
{
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL