-
Notifications
You must be signed in to change notification settings - Fork 620
/
cxx_parser_block.c
888 lines (819 loc) · 26.1 KB
/
cxx_parser_block.c
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
/*
* Copyright (c) 2016, Szymon Tomasz Stefanek
*
* This source code is released for free distribution under the terms of the
* GNU General Public License version 2 or (at your option) any later version.
*
* This module contains functions for parsing and scanning C++ source files
*/
#include "cxx_parser.h"
#include "cxx_parser_internal.h"
#include "cxx_debug.h"
#include "cxx_keyword.h"
#include "cxx_token.h"
#include "cxx_token_chain.h"
#include "cxx_scope.h"
#include "cxx_tag.h"
#include "cxx_side_chain.h"
#include "parse.h"
#include "vstring.h"
#include "../x-cpreprocessor.h"
#include "debug.h"
#include "keyword.h"
#include "read.h"
#include "cxx_subparser_internal.h"
#include <string.h>
static bool cxxParserParseBlockFull(bool bExpectClosingBracket, bool bExported);
bool cxxParserParseBlockHandleOpeningBracket(void)
{
CXX_DEBUG_ENTER();
CXX_DEBUG_ASSERT(
cxxTokenTypeIs(g_cxx.pToken, CXXTokenTypeOpeningBracket),
"This must be called when pointing at an opening bracket!"
);
enum CXXScopeType eScopeType = cxxScopeGetType();
bool bIsCPP = cxxParserCurrentLanguageIsCPP();
CXXToken * pAux;
if(
(
// something = {...}
(g_cxx.pToken->pPrev) &&
cxxTokenTypeIs(g_cxx.pToken->pPrev,CXXTokenTypeAssignment) &&
(
(eScopeType == CXXScopeTypeFunction) ||
(eScopeType == CXXScopeTypeClass) ||
(eScopeType == CXXScopeTypeStruct) ||
(eScopeType == CXXScopeTypeUnion) ||
(eScopeType == CXXScopeTypeNamespace)
)
) || (
bIsCPP &&
(g_cxx.pToken->pPrev) &&
(
(
// T { arg1, arg2, ... } (1)
// T object { arg1, arg2, ... } (2)
// new T { arg1, arg2, ... } (3)
// Class::Class() : member { arg1, arg2, ... } { (4)
cxxTokenTypeIs(g_cxx.pToken->pPrev,CXXTokenTypeIdentifier) &&
(
// case 1
(!g_cxx.pToken->pPrev->pPrev) ||
// case 4
cxxTokenTypeIsOneOf(
g_cxx.pToken->pPrev->pPrev,
CXXTokenTypeSingleColon | CXXTokenTypeComma
) ||
// cases 1,2,3 but not 4
(
// more parts of typename or maybe the "new" keyword before the identifier
cxxTokenTypeIsOneOf(
g_cxx.pToken->pPrev->pPrev,
CXXTokenTypeIdentifier | CXXTokenTypeStar | CXXTokenTypeAnd |
CXXTokenTypeGreaterThanSign | CXXTokenTypeKeyword
) &&
// but no parenthesis (discard things like bool test() Q_DECL_NO_THROW { ... })
(!(pAux = cxxTokenChainPreviousTokenOfType(
g_cxx.pToken->pPrev->pPrev,
CXXTokenTypeParenthesisChain
))
)
)
) &&
// "override" is handled as identifier since it's a keyword only after function signatures
(strcmp(vStringValue(g_cxx.pToken->pPrev->pszWord),"override") != 0)
) || (
// type var[][][]..[] { ... }
// (but not '[] { ... }' which is a parameterless lambda)
cxxTokenTypeIs(g_cxx.pToken->pPrev,CXXTokenTypeSquareParenthesisChain) &&
(
pAux = cxxTokenChainPreviousTokenNotOfType(
g_cxx.pToken->pPrev,
CXXTokenTypeSquareParenthesisChain
)
) &&
cxxTokenTypeIs(pAux,CXXTokenTypeIdentifier)
)
)
) || (
// return { }
(!g_cxx.pToken->pPrev) &&
(g_cxx.uKeywordState & CXXParserKeywordStateSeenReturn)
)
)
{
// array or list-like initialisation
bool bRet = cxxParserParseAndCondenseCurrentSubchain(
CXXTokenTypeOpeningBracket | CXXTokenTypeOpeningParenthesis |
CXXTokenTypeOpeningSquareParenthesis,
false,
true
);
CXX_DEBUG_LEAVE_TEXT("Handled array or list-like initialisation or return");
return bRet;
}
// In C++ mode check for lambdas
CXXToken * pParenthesis;
if(
bIsCPP &&
(pParenthesis = cxxParserOpeningBracketIsLambda())
)
{
if(!cxxParserHandleLambda(pParenthesis))
{
CXX_DEBUG_LEAVE_TEXT("Lambda handling failed");
return false;
}
// Note that here we're leaving the token chain "alive" so further parsing can be performed.
CXX_DEBUG_LEAVE_TEXT("Lambda handling succeeded");
return true;
}
int iScopes;
int iCorkQueueIndex = CORK_NIL;
int iCorkQueueIndexFQ = CORK_NIL;
CXXFunctionSignatureInfo oInfo;
bool bExported = false;
if(eScopeType != CXXScopeTypeFunction)
{
// very likely a function definition
// (but may be also a toplevel block, like "extern "C" { ... }")
iScopes = cxxParserExtractFunctionSignatureBeforeOpeningBracket(&oInfo,&iCorkQueueIndex,&iCorkQueueIndexFQ);
// FIXME: Handle syntax (5) of list initialization:
// Class::Class() : member { arg1, arg2, ... } {...
bExported = (g_cxx.uKeywordState & CXXParserKeywordStateSeenExport);
} else {
// some kind of other block:
// - anonymous block
// - block after for(),while(),foreach(),if() and other similar stuff
// (note that {}-style initializers have been handled above and thus are excluded)
iScopes = 0;
}
cxxParserNewStatementFull(bExported);
if(!cxxParserParseBlockFull(true, bExported))
{
CXX_DEBUG_LEAVE_TEXT("Failed to parse nested block");
return false;
}
if(iScopes < 1)
{
CXX_DEBUG_LEAVE_TEXT("The block was not a function");
return true;
}
unsigned long uEndPosition = getInputLineNumber();
// If the function contained a "try" keyword before the opening bracket
// then it's likely to be a function-try-block and should be followed by a catch
// block that is in the same scope.
if(oInfo.uFlags & CXXFunctionSignatureInfoFunctionTryBlock)
{
// look for the catch blocks.
CXX_DEBUG_PRINT("The function is a function-try-block: looking for catch blocks");
for(;;)
{
CXX_DEBUG_PRINT("Looking ahead for a catch block...");
if(!cxxParserParseNextToken())
break; // EOF
if(!cxxTokenIsKeyword(g_cxx.pToken,CXXKeywordCATCH))
{
// No more catches. Unget and exit.
CXX_DEBUG_PRINT("No more catch blocks");
cxxParserUngetCurrentToken();
break;
}
// assume it's a catch block.
CXX_DEBUG_PRINT("Found catch block");
if(!cxxParserParseIfForWhileSwitchCatchParenthesis())
{
CXX_DEBUG_LEAVE_TEXT("Failed to parse the catch parenthesis");
return false;
}
// the standard requires a bracket here (catch block is always a compound statement).
cxxParserNewStatement();
if(!cxxParserParseNextToken())
{
CXX_DEBUG_LEAVE_TEXT("Found EOF while looking for catch() block: playing nice");
break; // EOF (would be a syntax error!)
}
if(!cxxTokenTypeIs(g_cxx.pToken,CXXTokenTypeOpeningBracket))
{
// Aargh...
CXX_DEBUG_LEAVE_TEXT("Found something unexpected while looking for catch() block: playing nice");
cxxParserUngetCurrentToken();
break; // (would be a syntax error!)
}
if(!cxxParserParseBlock(true))
return false;
uEndPosition = getInputLineNumber();
}
}
if(iCorkQueueIndex > CORK_NIL)
{
cxxParserSetEndLineForTagInCorkQueue(iCorkQueueIndex,uEndPosition);
if(iCorkQueueIndexFQ > CORK_NIL)
cxxParserSetEndLineForTagInCorkQueue(iCorkQueueIndexFQ,uEndPosition);
}
while(iScopes > 0)
{
cxxScopePop();
iScopes--;
}
CXX_DEBUG_LEAVE();
return true;
}
static bool cxxParserParseBlockInternal(bool bExpectClosingBracket, bool bExported)
{
CXX_DEBUG_ENTER();
//char * szScopeName = cxxScopeGetFullName();
//CXX_DEBUG_PRINT("Scope name is '%s'",szScopeName ? szScopeName : "");
cxxParserNewStatementFull(bExported);
if(bExpectClosingBracket)
{
// FIXME: this cpp handling is kind of broken:
// it works only because the moon is in the correct phase.
cppBeginStatement();
}
CXXTokenChain *pSideChain = NULL;
for(;;)
{
if(!cxxParserParseNextToken())
{
found_eof:
cxxTokenChainDestroy(pSideChain);
pSideChain = NULL;
if(bExpectClosingBracket)
{
CXX_DEBUG_LEAVE_TEXT(
"Syntax error: found EOF in block but a closing " \
"bracket was expected!"
);
return false;
}
CXX_DEBUG_LEAVE_TEXT("EOF in main block");
return true; // EOF
}
cxxSideChainAppendChain(pSideChain,g_cxx.pToken);
pSideChain = NULL;
process_token:
CXX_DEBUG_PRINT(
"Token '%s' of type 0x%02x",
vStringValue(g_cxx.pToken->pszWord),
g_cxx.pToken->eType
);
if (cxxTokenTypeIs(g_cxx.pToken,CXXTokenTypeIdentifier)
&& cxxScopeGetType() == CXXScopeTypeClass
&& cxxSubparserNewIdentifierAsHeadOfMemberNotify(g_cxx.pToken))
{
pSideChain = cxxSideChainEject(g_cxx.pToken);
cxxTokenChainDestroyLast(g_cxx.pTokenChain);
continue;
}
if (bExported)
g_cxx.uKeywordState |= CXXParserKeywordStateSeenExport;
switch(g_cxx.pToken->eType)
{
case CXXTokenTypeKeyword:
{
switch(g_cxx.pToken->eKeyword)
{
case CXXKeywordNAMESPACE:
{
enum CXXScopeType eScopeType = cxxScopeGetType();
if(
(
// toplevel or nested within a namespace
(eScopeType == CXXScopeTypeNamespace) ||
// namespace X = Y inside a function
(eScopeType == CXXScopeTypeFunction)
) && (
// either certainly C++
g_cxx.bConfirmedCPPLanguage ||
// or a "sane" namespace syntax
(
!cxxTokenChainPreviousTokenOfType(
g_cxx.pToken,
CXXTokenTypeStar |
CXXTokenTypeAnd |
CXXTokenTypeKeyword
)
)
)
)
{
if(!cxxParserParseNamespace())
{
CXX_DEBUG_LEAVE_TEXT("Failed to parse namespace");
return false;
}
} else {
// If we're pretty sure this is C++ then this is a syntax error.
// If we're not sure (namely when we're in a *.h file) then
// let's try to be flexible: treat the namespace keyword as an identifier.
if(!g_cxx.bConfirmedCPPLanguage)
{
CXX_DEBUG_LEAVE_TEXT(
"Found namespace in unexpected place, but we're not sure it's really C++ "
"so we'll treat it as an identifier instead"
);
g_cxx.pToken->eType = CXXTokenTypeIdentifier;
continue;
}
CXX_DEBUG_LEAVE_TEXT(
"Found namespace in a wrong place: we're probably out of sync"
);
return false;
}
cxxParserNewStatement();
}
break;
case CXXKeywordTEMPLATE:
if(
// beginning of the statement
(!g_cxx.pToken->pPrev) ||
// previous token is not "." or "->", syntax that is found in
// p.template func<int>();
(!cxxTokenTypeIsOneOf(
g_cxx.pToken->pPrev,
CXXTokenTypeDotOperator | CXXTokenTypePointerOperator
)
)
)
{
if(!cxxParserParseTemplatePrefix())
{
CXX_DEBUG_LEAVE_TEXT("Failed to parse template");
return false;
}
// Here we are just after the "template<parameters>" prefix.
} else {
CXX_DEBUG_LEAVE_TEXT("Template keyword that is not a prefix");
}
break;
case CXXKeywordTYPEDEF:
// Mark the next declaration as a typedef
g_cxx.uKeywordState |= CXXParserKeywordStateSeenTypedef;
cxxTokenChainClear(g_cxx.pTokenChain);
break;
case CXXKeywordENUM:
if(!cxxParserParseEnum())
{
CXX_DEBUG_LEAVE_TEXT("Failed to parse enum");
return false;
}
break;
case CXXKeywordCLASS:
if(
// do not trigger on X<class Y>
(!g_cxx.pToken->pPrev) ||
(!cxxTokenTypeIsOneOf(g_cxx.pToken->pPrev,CXXTokenTypeSmallerThanSign | CXXTokenTypeComma))
)
{
if(!cxxParserParseClassStructOrUnion(CXXKeywordCLASS,CXXTagCPPKindCLASS,CXXScopeTypeClass))
{
CXX_DEBUG_LEAVE_TEXT("Failed to parse class/struct/union");
return false;
}
}
break;
case CXXKeywordSTRUCT:
if(
// do not trigger on X<struct Y>
(!g_cxx.pToken->pPrev) ||
(!cxxTokenTypeIsOneOf(g_cxx.pToken->pPrev,CXXTokenTypeSmallerThanSign | CXXTokenTypeComma))
)
{
if(!cxxParserParseClassStructOrUnion(CXXKeywordSTRUCT,CXXTagKindSTRUCT,CXXScopeTypeStruct))
{
CXX_DEBUG_LEAVE_TEXT("Failed to parse class/struct/union");
return false;
}
}
break;
case CXXKeywordUNION:
if(
// do not trigger on X<union Y>
(!g_cxx.pToken->pPrev) ||
(!cxxTokenTypeIsOneOf(g_cxx.pToken->pPrev,CXXTokenTypeSmallerThanSign | CXXTokenTypeComma))
)
{
if(!cxxParserParseClassStructOrUnion(CXXKeywordUNION,CXXTagKindUNION,CXXScopeTypeUnion))
{
CXX_DEBUG_LEAVE_TEXT("Failed to parse class/struct/union");
return false;
}
}
break;
break;
case CXXKeywordPUBLIC:
case CXXKeywordPROTECTED:
case CXXKeywordPRIVATE:
// Note that the class keyword has its own handler
// so the only possibility here is an access specifier
if(!cxxParserParseAccessSpecifier())
{
CXX_DEBUG_LEAVE_TEXT("Failed to parse access specifier");
return false;
}
break;
case CXXKeywordUSING:
if(!cxxParserParseUsingClause())
{
CXX_DEBUG_LEAVE_TEXT("Failed to parse using clause");
return false;
}
cxxParserNewStatement();
break;
case CXXKeywordIF:
case CXXKeywordFOR:
case CXXKeywordWHILE:
case CXXKeywordSWITCH:
case CXXKeywordCATCH:
if(!cxxParserParseIfForWhileSwitchCatchParenthesis())
{
CXX_DEBUG_LEAVE_TEXT("Failed to parse if/for/while/switch/catch parenthesis");
return false;
}
// Now we're just before the block that follows the parenthesis.
cxxParserNewStatement();
// Force the cpp preprocessor to think that we're in the middle of a statement.
cppBeginStatement();
break;
case CXXKeywordTRY:
// We parse try in different ways depending on the context.
// Inside a function, and without preceding tokens it's assumed to be
// a plain try {} catch {} block. This is easy.
// Out of a function it's likely to be a function try block:
// int f(int n = 2) try { ... } catch { ... }
// Inside a function but with some preceding tokens it's likely to be a
// lambda expressed as function-try-block.
// auto f() -> void try { ... } catch { ... }
if((cxxScopeGetType() != CXXScopeTypeFunction) || g_cxx.pToken->pPrev)
{
CXX_DEBUG_PRINT("Found try that looks like a function-try-block");
// Maybe function-try-block.
// Keep in the chain and continue parsing.
continue;
}
// Fall through.
case CXXKeywordELSE:
case CXXKeywordDO:
// parse as normal statement/block
cxxParserNewStatement();
// Force the cpp preprocessor to think that we're in the middle of a statement.
cppBeginStatement();
break;
case CXXKeywordRETURN:
if(cxxParserCurrentLanguageIsCPP())
{
// may be followed by a lambda, otherwise it's not interesting.
cxxParserNewStatement();
g_cxx.uKeywordState |= CXXParserKeywordStateSeenReturn;
} else {
// ignore
if(!cxxParserParseUpToOneOf(CXXTokenTypeSemicolon | CXXTokenTypeEOF,
false))
{
CXX_DEBUG_LEAVE_TEXT("Failed to parse return");
return false;
}
cxxParserNewStatement();
}
break;
case CXXKeywordCONTINUE:
case CXXKeywordBREAK:
case CXXKeywordGOTO:
// ignore
if(!cxxParserParseUpToOneOf(CXXTokenTypeSemicolon | CXXTokenTypeEOF,
false))
{
CXX_DEBUG_LEAVE_TEXT("Failed to parse continue/break/goto");
return false;
}
cxxParserNewStatement();
break;
case CXXKeywordTHROW:
// We ignore whole "throw expressions" as they contain nothing useful
// and may confuse us. We keep "throw" when used as exception specification,
// and this is certainly outside of a function and when the token chain
// already contains at least a type, an identifier and a parenthesis.
// This check seems excessive but keep in mind that we deal with
// broken input and we might also be wrong about the current scope.
if((cxxScopeGetType() == CXXScopeTypeFunction) || (g_cxx.pTokenChain->iCount < 3))
{
CXX_DEBUG_PRINT("Skipping throw statement");
if(!cxxParserParseUpToOneOf(CXXTokenTypeSemicolon | CXXTokenTypeEOF,
false))
{
CXX_DEBUG_LEAVE_TEXT("Failed to skip throw statement");
return false;
}
cxxParserNewStatement();
}
break;
case CXXKeywordCASE:
// ignore
if(!cxxParserParseUpToOneOf(
CXXTokenTypeSemicolon | CXXTokenTypeEOF | CXXTokenTypeSingleColon,
false
))
{
CXX_DEBUG_LEAVE_TEXT("Failed to parse case keyword");
return false;
}
cxxParserNewStatement();
break;
case CXXKeywordEXTERN:
g_cxx.uKeywordState |= CXXParserKeywordStateSeenExtern;
pSideChain = cxxSideChainEject(g_cxx.pToken);
cxxTokenChainDestroyLast(g_cxx.pTokenChain);
if(!cxxParserParseNextToken())
goto found_eof;
cxxSideChainAppendChain(pSideChain,g_cxx.pToken);
pSideChain = NULL;
if(cxxTokenTypeIs(g_cxx.pToken,CXXTokenTypeStringConstant))
{
// assume extern "language"
// Strictly speaking this is a C++ only syntax.
// However we allow it also in C as it doesn't really hurt.
cxxTokenChainDestroyLast(g_cxx.pTokenChain);
// Note that extern "C" may be followed by a block with declarations
//
// extern "C" { ... }
//
// However in this case the declarations are ALSO definitions
// and extern "C" is used only to specify the name mangling mode.
//
// extern "C" int x; <-- a declaration and not a definition
// extern "C" { int x; } <-- a declaration and definition: x IS defined
// here and is NOT extern.
//
// A variable in an extern "C" block has to be re-declared extern again
// to be really treated as declaration only.
//
// extern "C" { extern int x; }
//
// So in this case we do NOT treat the inner declarations as extern
// and we don't need specific handling code for this case.
} else {
// something else: handle it the normal way
goto process_token;
}
break;
case CXXKeywordSTATIC:
g_cxx.uKeywordState |= CXXParserKeywordStateSeenStatic;
pSideChain = cxxSideChainEject(g_cxx.pToken);
cxxTokenChainDestroyLast(g_cxx.pTokenChain);
break;
case CXXKeywordINLINE:
case CXXKeyword__INLINE:
case CXXKeyword__INLINE__:
case CXXKeyword__FORCEINLINE:
case CXXKeyword__FORCEINLINE__:
g_cxx.uKeywordState |= CXXParserKeywordStateSeenInline;
pSideChain = cxxSideChainEject(g_cxx.pToken);
cxxTokenChainDestroyLast(g_cxx.pTokenChain);
break;
case CXXKeywordEXPLICIT:
g_cxx.uKeywordState |= CXXParserKeywordStateSeenExplicit;
pSideChain = cxxSideChainEject(g_cxx.pToken);
cxxTokenChainDestroyLast(g_cxx.pTokenChain);
break;
case CXXKeywordOPERATOR:
g_cxx.uKeywordState |= CXXParserKeywordStateSeenOperator;
break;
case CXXKeywordVIRTUAL:
g_cxx.uKeywordState |= CXXParserKeywordStateSeenVirtual;
pSideChain = cxxSideChainEject(g_cxx.pToken);
cxxTokenChainDestroyLast(g_cxx.pTokenChain);
break;
case CXXKeywordMUTABLE:
g_cxx.uKeywordState |= CXXParserKeywordStateSeenMutable;
pSideChain = cxxSideChainEject(g_cxx.pToken);
cxxTokenChainDestroyLast(g_cxx.pTokenChain);
break;
case CXXKeywordFRIEND:
g_cxx.uKeywordState |= CXXParserKeywordStateSeenFriend;
break;
// "const" and "volatile" are part of the type. Don't treat them specially
// and don't attempt to extract an eventual typedef yet,
// as there might be a struct/class/union keyword following.
case CXXKeywordVOLATILE:
g_cxx.uKeywordState |= CXXParserKeywordStateSeenVolatile;
break;
case CXXKeywordCONST:
g_cxx.uKeywordState |= CXXParserKeywordStateSeenConst;
break;
case CXXKeywordCONSTEXPR:
g_cxx.uKeywordState |= CXXParserKeywordStateSeenConstexpr;
break;
case CXXKeywordCONSTEVAL:
g_cxx.uKeywordState |= CXXParserKeywordStateSeenConsteval;
break;
case CXXKeywordCONSTINIT:
g_cxx.uKeywordState |= CXXParserKeywordStateSeenConstinit;
break;
case CXXKeywordTHREAD_LOCAL:
case CXXKeyword__THREAD:
case CXXKeyword_THREAD_LOCAL:
g_cxx.uKeywordState |= CXXParserKeywordStateSeenThreadLocal;
break;
case CXXKeywordEXPORT:
g_cxx.uKeywordState |= CXXParserKeywordStateSeenExport;
break;
default:
if(g_cxx.uKeywordState & CXXParserKeywordStateSeenTypedef)
{
g_cxx.uKeywordState &= ~CXXParserKeywordStateSeenTypedef;
if(!cxxParserParseGenericTypedef())
{
CXX_DEBUG_LEAVE_TEXT("Failed to parse generic typedef");
return false;
}
cxxParserNewStatement();
}
break;
}
}
break;
case CXXTokenTypeSemicolon:
{
if(
(cxxParserCurrentLanguageIsC()) &&
cxxScopeIsGlobal() &&
(!(g_cxx.uKeywordState & CXXParserKeywordStateSeenExtern)) &&
(!(g_cxx.uKeywordState & CXXParserKeywordStateSeenTypedef))
)
{
// Special handling of K&R style function declarations.
// We might be in the following situation:
//
// type whatever fname(par1,par2) int par1; int par2; {
// ^
//
switch(cxxParserMaybeParseKnRStyleFunctionDefinition())
{
case 1:
// K&R parser did the job and started a new statement
break;
case 0:
// something else
cxxParserAnalyzeOtherStatement();
break;
default:
CXX_DEBUG_LEAVE_TEXT("Failed to check for K&R style function definition");
return false;
break;
}
} else {
// K&R style function declarations not allowed here.
cxxParserAnalyzeOtherStatement();
}
cxxParserNewStatement();
}
break;
case CXXTokenTypeSingleColon:
{
// label ?
if(
(g_cxx.pTokenChain->iCount == 2) &&
cxxTokenTypeIs(
cxxTokenChainFirst(g_cxx.pTokenChain),
CXXTokenTypeIdentifier
)
)
{
CXXToken * pFirst = cxxTokenChainFirst(g_cxx.pTokenChain);
// assume it's label
tagEntryInfo * tag = cxxTagBegin(CXXTagKindLABEL,pFirst);
if(tag)
{
tag->isFileScope = true;
cxxTagCommit(NULL);
}
} else {
// what is this? (default: and similar things have been handled at keyword level)
}
}
break;
case CXXTokenTypeOpeningBracket:
if(!cxxParserParseBlockHandleOpeningBracket())
{
CXX_DEBUG_LEAVE_TEXT("Failed to handle opening bracket");
return false;
}
break;
case CXXTokenTypeClosingBracket:
// scope finished
if(!bExpectClosingBracket)
{
CXX_DEBUG_LEAVE_TEXT(
"Found unexpected closing bracket: probably preprocessing problem"
);
return false;
}
CXX_DEBUG_LEAVE_TEXT("Closing bracket!");
cxxParserNewStatement();
return true;
break;
case CXXTokenTypeOpeningParenthesis:
case CXXTokenTypeOpeningSquareParenthesis:
if(!cxxParserParseAndCondenseCurrentSubchain(
CXXTokenTypeOpeningBracket | CXXTokenTypeOpeningParenthesis |
CXXTokenTypeOpeningSquareParenthesis,
true,
false
))
{
CXX_DEBUG_LEAVE_TEXT("Parsing the parenthesis failed");
return false;
}
if(cxxTokenTypeIs(g_cxx.pToken,CXXTokenTypeEOF))
{
if(bExpectClosingBracket)
{
CXX_DEBUG_LEAVE_TEXT(
"Syntax error: found EOF in block but a closing bracket was expected!"
);
return false;
}
return true; // EOF
}
break;
case CXXTokenTypeIdentifier:
if(
/* Accept only "export" as a preceding token. */
(! (g_cxx.uKeywordState & ~CXXParserKeywordStateSeenExport))
&& (g_cxx.pToken->pPrev == NULL || cxxTokenIsKeyword(g_cxx.pToken->pPrev, CXXKeywordEXPORT))
/* C++ */
&& cxxParserCurrentLanguageIsCPP()
/* At the toplevel */
&& cxxScopeIsGlobal()
/* Handle this token as a keyword, not an identifier. */
&& (strcmp(vStringValue(g_cxx.pToken->pszWord), "module") == 0)
)
{
/* "module" introduced in C++20, can be a keyworkd in limited contexts.
* If the parsing is not in the context, the parser should handle it
* as an identifier. */
if(!cxxParserParseModule())
{
CXX_DEBUG_LEAVE_TEXT("Failed to parse module");
return false;
}
break;
}
else if(
(! (g_cxx.uKeywordState & ~CXXParserKeywordStateSeenExport))
&& (g_cxx.pToken->pPrev == NULL || cxxTokenIsKeyword(g_cxx.pToken->pPrev, CXXKeywordEXPORT))
&& cxxParserCurrentLanguageIsCPP()
&& cxxScopeIsGlobal()
&& (strcmp(vStringValue(g_cxx.pToken->pszWord), "import") == 0)
)
{
if(!cxxParserParseImport())
{
CXX_DEBUG_LEAVE_TEXT("Failed to parse import");
return false;
}
break;
}
else if(g_cxx.uKeywordState & CXXParserKeywordStateSeenTypedef)
{
g_cxx.uKeywordState &= ~CXXParserKeywordStateSeenTypedef;
if(!cxxParserParseGenericTypedef())
{
CXX_DEBUG_LEAVE_TEXT("Failed to parse generic typedef");
return false;
}
cxxParserNewStatement();
}
else if (cxxScopeGetType() == CXXScopeTypeClass)
cxxSubparserUnknownIdentifierInClassNotify(g_cxx.pToken);
break;
default:
// something else we didn't handle
break;
}
}
CXX_DEBUG_LEAVE_TEXT("WARNING: Not reached");
return true;
}
//
// This is the toplevel scanning function. It's a forward-only scanner that keeps
// accumulating tokens in the chain until either a characteristic token is found
// or the statement ends. When a characteristic token is found it usually enters
// a specialized scanning routine (e.g for classes, namespaces, structs...).
// When the statement ends without finding any characteristic token the chain
// is passed to an analysis routine which does a second scan pass.
//
static bool cxxParserParseBlockFull(bool bExpectClosingBracket, bool bExported)
{
cxxSubparserNotifyEnterBlock ();
cppPushExternalParserBlock();
bool bRet = cxxParserParseBlockInternal(bExpectClosingBracket, bExported);
cppPopExternalParserBlock();
cxxSubparserNotifyLeaveBlock ();
return bRet;
}
bool cxxParserParseBlock(bool bExpectClosingBracket)
{
return cxxParserParseBlockFull(bExpectClosingBracket, false);
}