-
Notifications
You must be signed in to change notification settings - Fork 187
/
GNUCPPSourceParser.java
6053 lines (5510 loc) · 196 KB
/
GNUCPPSourceParser.java
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) 2002, 2019 IBM Corporation and others.
*
* This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
* which accompanies this distribution, and is available at
* https://www.eclipse.org/legal/epl-2.0/
*
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
* John Camelon (IBM Corporation) - initial API and implementation
* Markus Schorn (Wind River Systems)
* Bryan Wilkinson (QNX) - https://bugs.eclipse.org/bugs/show_bug.cgi?id=151207
* Ed Swartz (Nokia)
* Mike Kucera (IBM)
* Andrew Ferguson (Symbian)
* Sergey Prigogin (Google)
* Thomas Corbat (IFS)
* Anders Dahlberg (Ericsson) - bug 84144
* Nathan Ridge
* Richard Eames
* Alexander Nyßen (itemis AG) - bug 475908
*******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.cpp;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.eclipse.cdt.core.dom.ast.ASTCompletionNode;
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.IASTAlignmentSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTArrayDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTArrayModifier;
import org.eclipse.cdt.core.dom.ast.IASTArraySubscriptExpression;
import org.eclipse.cdt.core.dom.ast.IASTAttributeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTBinaryExpression;
import org.eclipse.cdt.core.dom.ast.IASTBinaryTypeIdExpression;
import org.eclipse.cdt.core.dom.ast.IASTCastExpression;
import org.eclipse.cdt.core.dom.ast.IASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTCompoundStatement;
import org.eclipse.cdt.core.dom.ast.IASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTElaboratedTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTEqualsInitializer;
import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTForStatement;
import org.eclipse.cdt.core.dom.ast.IASTFunctionCallExpression;
import org.eclipse.cdt.core.dom.ast.IASTFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTIdExpression;
import org.eclipse.cdt.core.dom.ast.IASTInitializer;
import org.eclipse.cdt.core.dom.ast.IASTInitializerClause;
import org.eclipse.cdt.core.dom.ast.IASTInitializerList;
import org.eclipse.cdt.core.dom.ast.IASTLiteralExpression;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNamedTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTPointer;
import org.eclipse.cdt.core.dom.ast.IASTPointerOperator;
import org.eclipse.cdt.core.dom.ast.IASTProblem;
import org.eclipse.cdt.core.dom.ast.IASTProblemDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTProblemExpression;
import org.eclipse.cdt.core.dom.ast.IASTProblemTypeId;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IASTToken;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.IASTTypeId;
import org.eclipse.cdt.core.dom.ast.IASTTypeIdExpression;
import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression;
import org.eclipse.cdt.core.dom.ast.IASTWhileStatement;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTAliasDeclaration;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTAlignmentSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTAmbiguousTemplateArgument;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTArrayDeclarator;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTArrayDesignator;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTAttribute;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTAttributeList;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCapture;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCastExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCatchHandler;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTClassVirtSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCompositeTypeSpecifier.ICPPASTBaseSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTConstructorChainInitializer;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTConstructorInitializer;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTConversionName;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTDeclarator;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTDecltypeSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTDeleteExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTDesignatedInitializer;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTDesignator;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTElaboratedTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTEnumerationSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTEnumerationSpecifier.ScopeStyle;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTExplicitTemplateInstantiation;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFieldDeclarator;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFieldDesignator;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFieldReference;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFoldExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTForStatement;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDeclarator.RefQualifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionDefinition;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFunctionWithTryBlock;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTIfStatement;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTInitCapture;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTInitializerClause;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTInitializerList;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTLambdaExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTLambdaExpression.CaptureDefault;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTLinkageSpecification;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTLiteralExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTName;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNameSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamedTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamespaceAlias;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNamespaceDefinition;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNaryTypeIdExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTNewExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTOperatorName;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTPackExpandable;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTParameterDeclaration;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTQualifiedName;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTRangeBasedForStatement;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTReferenceOperator;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSimpleDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSimpleTypeConstructorExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSimpleTypeTemplateParameter;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTStaticAssertDeclaration;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTStructuredBindingDeclaration;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTSwitchStatement;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateDeclaration;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateId;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateName;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateParameter;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplateSpecialization;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTemplatedTypeTemplateParameter;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTranslationUnit;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTryBlockStatement;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTypeId;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTypeIdExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTTypeTransformationSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTUnaryExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTUsingDeclaration;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTUsingDirective;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTVirtSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTVirtSpecifier.SpecifierKind;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTVisibilityLabel;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPNodeFactory;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPUnaryTypeTransformation;
import org.eclipse.cdt.core.dom.ast.gnu.cpp.IGPPASTArrayRangeDesignator;
import org.eclipse.cdt.core.dom.parser.IExtensionToken;
import org.eclipse.cdt.core.dom.parser.cpp.ICPPParserExtensionConfiguration;
import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.parser.EndOfFileException;
import org.eclipse.cdt.core.parser.IGCCToken;
import org.eclipse.cdt.core.parser.IParserLogService;
import org.eclipse.cdt.core.parser.IProblem;
import org.eclipse.cdt.core.parser.IScanner;
import org.eclipse.cdt.core.parser.IToken;
import org.eclipse.cdt.core.parser.IToken.ContextSensitiveTokenType;
import org.eclipse.cdt.core.parser.Keywords;
import org.eclipse.cdt.core.parser.ParserMode;
import org.eclipse.cdt.core.parser.util.ArrayUtil;
import org.eclipse.cdt.core.parser.util.CharArrayUtils;
import org.eclipse.cdt.core.parser.util.CollectionUtils;
import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
import org.eclipse.cdt.internal.core.dom.parser.ASTQueries;
import org.eclipse.cdt.internal.core.dom.parser.ASTTranslationUnit;
import org.eclipse.cdt.internal.core.dom.parser.AbstractGNUSourceCodeParser;
import org.eclipse.cdt.internal.core.dom.parser.BacktrackException;
import org.eclipse.cdt.internal.core.dom.parser.DeclarationOptions;
import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguityParent;
import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguousDeclarator;
import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguousExpression;
import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguousStatement;
import org.eclipse.cdt.internal.core.dom.parser.cpp.NameOrTemplateIDVariants.BranchPoint;
import org.eclipse.cdt.internal.core.dom.parser.cpp.NameOrTemplateIDVariants.Variant;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPVisitor;
/**
* This is our implementation of the IParser interface, serving as a parser for
* GNU C and C++. From time to time we will make reference to the ANSI ISO
* specifications.
*/
public class GNUCPPSourceParser extends AbstractGNUSourceCodeParser {
private static final int DEFAULT_PARM_LIST_SIZE = 4;
private static final int DEFAULT_CATCH_HANDLER_LIST_SIZE = 4;
private static final int TEMPLATE_ARGUMENT_NESTING_DEPTH_LIMIT = 192;
// This is a parameter to the protected function {@link #declarator(DtorStrategy, DeclarationOptions)}
// so it needs to be protected too.
protected static enum DtorStrategy {
PREFER_FUNCTION, PREFER_NESTED
}
private final boolean allowCPPRestrict;
private final boolean supportExtendedTemplateSyntax;
private final boolean supportAutoTypeSpecifier;
private final boolean supportUserDefinedLiterals;
private final boolean supportGCCStyleDesignators;
private final boolean supportFoldExpression;
private final IIndex index;
protected ICPPASTTranslationUnit translationUnit;
private int functionBodyCount;
private int templateArgumentNestingDepth = 0;
private char[] currentClassName;
private char[] additionalNumericalSuffixes;
private TemplateIdStrategy fTemplateParameterListStrategy;
private Map<String, ContextSensitiveTokenType> fContextSensitiveTokens;
public GNUCPPSourceParser(IScanner scanner, ParserMode mode, IParserLogService log,
ICPPParserExtensionConfiguration config) {
this(scanner, mode, log, config, null);
}
public GNUCPPSourceParser(IScanner scanner, ParserMode mode, IParserLogService log,
ICPPParserExtensionConfiguration config, IIndex index) {
super(scanner, log, mode, CPPNodeFactory.getDefault(), config.supportStatementsInExpressions(),
config.supportTypeofUnaryExpressions(), config.supportAlignOfUnaryExpression(), config.supportKnRC(),
config.supportAttributeSpecifiers(), config.supportDeclspecSpecifiers(),
config.getBuiltinBindingsProvider());
allowCPPRestrict = config.allowRestrictPointerOperators();
supportExtendedTemplateSyntax = config.supportExtendedTemplateSyntax();
supportParameterInfoBlock = config.supportParameterInfoBlock();
supportExtendedSizeofOperator = config.supportExtendedSizeofOperator();
supportFunctionStyleAsm = config.supportFunctionStyleAssembler();
supportGCCStyleDesignators = config.supportGCCStyleDesignators();
functionCallCanBeLValue = true;
supportAutoTypeSpecifier = true;
supportUserDefinedLiterals = config.supportUserDefinedLiterals();
this.index = index;
scanner.setSplitShiftROperator(true);
fContextSensitiveTokens = createContextSensitiveTokenMap(config);
additionalNumericalSuffixes = scanner.getAdditionalNumericLiteralSuffixes();
supportFoldExpression = true;
}
@Override
protected ICPPNodeFactory getNodeFactory() {
return (ICPPNodeFactory) super.getNodeFactory();
}
private Map<String, ContextSensitiveTokenType> createContextSensitiveTokenMap(
ICPPParserExtensionConfiguration config) {
Map<String, ContextSensitiveTokenType> result = new HashMap<>();
result.put(Keywords.OVERRIDE, ContextSensitiveTokenType.OVERRIDE);
result.put(Keywords.FINAL, ContextSensitiveTokenType.FINAL);
result.putAll(config.getAdditionalContextSensitiveKeywords());
return result;
}
private ContextSensitiveTokenType getContextSensitiveType(IToken token) {
if (!(token.getType() == IToken.tIDENTIFIER)) {
return null;
}
return fContextSensitiveTokens.get(new String(token.getCharImage()));
}
@Override
protected IASTName identifier() throws EndOfFileException, BacktrackException {
switch (LT(1)) {
case IToken.tIDENTIFIER:
case IToken.tCOMPLETION:
case IToken.tEOC:
return buildName(-1, consume(), false);
}
throw backtrack;
}
private ICPPASTNameSpecifier nameSpecifier() throws BacktrackException, EndOfFileException {
return ambiguousNameSpecifier(CastExprCtx.eNotInBExpr);
}
private ICPPASTNameSpecifier ambiguousNameSpecifier(CastExprCtx ctx) throws BacktrackException, EndOfFileException {
TemplateIdStrategy strat = new TemplateIdStrategy();
IToken m = mark();
while (true) {
try {
return nameSpecifier(ctx, strat);
} catch (BacktrackException e) {
if (e.isFatal()) {
throw e;
}
if (strat.setNextAlternative(true /* previous alternative failed to parse */)) {
backup(m);
} else {
throw e;
}
}
}
}
/**
* Parses a name specifier.
*/
private ICPPASTNameSpecifier nameSpecifier(CastExprCtx ctx, ITemplateIdStrategy strat)
throws BacktrackException, EndOfFileException {
if (strat == null)
return ambiguousNameSpecifier(ctx);
ICPPASTQualifiedName qname = null;
ICPPASTNameSpecifier nameSpec = null;
final int offset = LA(1).getOffset();
int endOffset = offset;
if (LT(1) == IToken.tCOLONCOLON) {
endOffset = consume().getEndOffset();
qname = getNodeFactory().newQualifiedName(null);
qname.setFullyQualified(true);
}
boolean mustBeLast = false;
boolean haveName = false;
loop: while (true) {
boolean keywordTemplate = false;
if (qname != null && LT(1) == IToken.t_template) {
consume();
keywordTemplate = true;
}
int destructorOffset = -1;
if (LT(1) == IToken.tBITCOMPLEMENT) {
destructorOffset = consume().getOffset();
mustBeLast = true;
}
switch (LT(1)) {
case IToken.tIDENTIFIER:
case IToken.tCOMPLETION:
case IToken.tEOC:
IToken nt = consume();
nameSpec = (ICPPASTName) buildName(destructorOffset, nt, keywordTemplate);
break;
case IToken.t_operator:
nameSpec = (ICPPASTName) operatorId();
break;
case IToken.t_decltype:
// A decltype-specifier must be the first component of a qualified name.
if (qname != null)
throwBacktrack(LA(1));
nameSpec = decltypeSpecifier();
break;
default:
if (!haveName || destructorOffset >= 0 || keywordTemplate) {
throwBacktrack(LA(1));
}
nameSpec = getNodeFactory().newName(CharArrayUtils.EMPTY);
if (qname != null) {
addNameSpecifier(qname, nameSpec);
}
break loop;
}
haveName = true;
// Check for template-id
if (nameSpec instanceof IASTName && LTcatchEOF(1) == IToken.tLT) {
IASTName name = (IASTName) nameSpec;
final boolean inBinaryExpression = ctx != CastExprCtx.eNotInBExpr;
final int haveArgs = haveTemplateArguments(inBinaryExpression);
boolean templateID = true;
if (!keywordTemplate) {
if (haveArgs == NO_TEMPLATE_ID) {
templateID = false;
} else if (haveArgs == AMBIGUOUS_TEMPLATE_ID) {
templateID = strat.shallParseAsTemplateID(name);
}
}
if (templateID) {
if (haveArgs == NO_TEMPLATE_ID)
throwBacktrack(LA(1));
nameSpec = (ICPPASTName) addTemplateArguments(name, strat);
}
}
endOffset = calculateEndOffset(nameSpec);
if (qname != null) {
addNameSpecifier(qname, nameSpec);
}
if (LTcatchEOF(1) != IToken.tCOLONCOLON)
break loop;
if (mustBeLast)
throwBacktrack(LA(1));
endOffset = consume().getEndOffset(); // ::
if (qname == null) {
qname = getNodeFactory().newQualifiedName(null);
addNameSpecifier(qname, nameSpec);
}
}
if (qname != null) {
setRange(qname, offset, endOffset);
nameSpec = qname;
}
return nameSpec;
}
private ICPPASTName qualifiedName() throws BacktrackException, EndOfFileException {
ICPPASTNameSpecifier nameSpec = nameSpecifier();
if (!(nameSpec instanceof ICPPASTName)) {
// decltype-specifier without following ::
throwBacktrack(nameSpec);
}
return (ICPPASTName) nameSpec;
}
/**
* Parses a qualified name.
*/
private ICPPASTName qualifiedName(CastExprCtx ctx, ITemplateIdStrategy strat)
throws BacktrackException, EndOfFileException {
ICPPASTNameSpecifier nameSpec = nameSpecifier(ctx, strat);
if (!(nameSpec instanceof ICPPASTName)) {
// decltype-specifier without following ::
throwBacktrack(nameSpec);
}
return (ICPPASTName) nameSpec;
}
private void addNameSpecifier(ICPPASTQualifiedName qname, ICPPASTNameSpecifier nameSpec) {
if (nameSpec instanceof IASTName)
qname.addName((IASTName) nameSpec);
else
qname.addNameSpecifier(nameSpec);
}
private IASTName buildName(int destructorOffset, IToken nt, boolean keywordTemplate) {
IASTName name;
if (destructorOffset < 0) {
if (keywordTemplate) {
name = getNodeFactory().newTemplateName(nt.getCharImage());
} else {
name = getNodeFactory().newName(nt.getCharImage());
}
setRange(name, nt.getOffset(), nt.getEndOffset());
} else {
char[] nchars = nt.getCharImage();
final int len = nchars.length;
char[] image = new char[len + 1];
image[0] = '~';
System.arraycopy(nchars, 0, image, 1, len);
name = getNodeFactory().newName(image);
setRange(name, destructorOffset, nt.getEndOffset());
}
switch (nt.getType()) {
case IToken.tEOC:
case IToken.tCOMPLETION:
ASTCompletionNode node = createCompletionNode(nt);
if (node != null)
node.addName(name);
break;
}
return name;
}
private IASTName addTemplateArguments(IASTName templateName, ITemplateIdStrategy strat)
throws EndOfFileException, BacktrackException {
// Parse for template arguments
consume(IToken.tLT);
List<IASTNode> list = templateArgumentList(strat);
IToken end = LA(1);
switch (end.getType()) {
case IToken.tGT_in_SHIFTR:
case IToken.tGT:
consume();
break;
case IToken.tEOC:
break;
default:
throw backtrack;
}
return buildTemplateID(templateName, end.getEndOffset(), list);
}
private ICPPASTTemplateId buildTemplateID(IASTName templateName, int endOffset, List<IASTNode> args) {
ICPPASTTemplateId result = getNodeFactory().newTemplateId(templateName);
setRange(result, ((ASTNode) templateName).getOffset(), endOffset);
for (IASTNode n : args) {
if (n instanceof IASTTypeId) {
result.addTemplateArgument((IASTTypeId) n);
} else if (n instanceof IASTExpression) {
result.addTemplateArgument((IASTExpression) n);
} else if (n instanceof ICPPASTAmbiguousTemplateArgument) {
result.addTemplateArgument((ICPPASTAmbiguousTemplateArgument) n);
}
}
return result;
}
/**
* Parses a decltype-specifier.
*/
private ICPPASTDecltypeSpecifier decltypeSpecifier() throws EndOfFileException, BacktrackException {
int start = consume(IToken.t_decltype).getOffset();
consume(IToken.tLPAREN);
ICPPASTExpression decltypeExpression = expression();
int end = consume(IToken.tRPAREN).getEndOffset();
ICPPASTDecltypeSpecifier decltypeSpec = getNodeFactory().newDecltypeSpecifier(decltypeExpression);
setRange(decltypeSpec, start, end);
return decltypeSpec;
}
/**
* Makes a fast check whether there could be template arguments.
* -1: no, 0: ambiguous, 1: yes
*/
private static final int NO_TEMPLATE_ID = -1;
private static final int AMBIGUOUS_TEMPLATE_ID = 0;
private static final int TEMPLATE_ID = 1;
private int haveTemplateArguments(boolean inBinaryExpression) throws EndOfFileException, BacktrackException {
final IToken mark = mark();
try {
consume();
int nk = 0;
int depth = 0;
int angleDepth = 1;
int limit = 10000;
while (--limit > 0) {
switch (consume().getType()) {
case IToken.tEOC:
case IToken.tCOMPLETION:
return AMBIGUOUS_TEMPLATE_ID;
case IToken.tLT:
if (nk == 0) {
angleDepth++;
}
break;
case IToken.tGT_in_SHIFTR:
case IToken.tGT:
if (nk == 0) {
--angleDepth;
if (!inBinaryExpression)
return angleDepth == 0 ? TEMPLATE_ID : AMBIGUOUS_TEMPLATE_ID;
int end = endsTemplateIDInBinaryExpression();
if (end == NO_TEMPLATE_ID) {
if (angleDepth == 0)
return NO_TEMPLATE_ID;
} else {
return AMBIGUOUS_TEMPLATE_ID;
}
}
break;
case IToken.tLBRACKET:
if (nk == 0) {
nk = IToken.tLBRACKET;
depth = 0;
} else if (nk == IToken.tLBRACKET) {
depth++;
}
break;
case IToken.tRBRACKET:
if (nk == IToken.tLBRACKET) {
if (--depth < 0) {
nk = 0;
}
}
break;
case IToken.tLPAREN:
if (nk == 0) {
nk = IToken.tLPAREN;
depth = 0;
} else if (nk == IToken.tLPAREN) {
depth++;
}
break;
case IToken.tRPAREN:
if (nk == IToken.tLPAREN) {
if (--depth < 0) {
nk = 0;
}
}
break;
// In C++11, braces can occur at the top level in a template-argument,
// if an object of class type is being created via uniform initialization,
// and that class type has a constexpr conversion operator to a type
// that's valid as the type of a non-type template parameter.
case IToken.tLBRACE:
if (nk == 0) {
nk = IToken.tLBRACE;
depth = 0;
} else if (nk == IToken.tLBRACE) {
depth++;
}
break;
case IToken.tRBRACE:
if (nk == 0) {
return NO_TEMPLATE_ID;
} else if (nk == IToken.tLBRACE) {
if (--depth < 0) {
nk = 0;
}
}
break;
case IToken.tSEMI:
if (nk == 0) {
return NO_TEMPLATE_ID;
}
break;
}
}
return AMBIGUOUS_TEMPLATE_ID;
} catch (EndOfFileException ignored) {
return NO_TEMPLATE_ID;
} finally {
backup(mark);
}
}
/**
* If '>' is followed by an expression, then it denotes the binary operator,
* else it is the end of a template-id, or special-cast.
*/
private int endsTemplateIDInBinaryExpression() {
int lt1 = LTcatchEOF(1);
switch (lt1) {
// Can be start of expression, or the scope operator applied to the template-id
case IToken.tCOLONCOLON: // 'CT<int>::member' or 'c<1 && 2 > ::g'
return AMBIGUOUS_TEMPLATE_ID;
// Can be start of expression or the function-call operator applied to a template-id
case IToken.tLPAREN: // 'ft<int>(args)' or 'c<1 && 2 > (x+y)'
return AMBIGUOUS_TEMPLATE_ID;
// Start of unary expression
case IToken.tMINUS:
case IToken.tPLUS:
case IToken.tAMPER:
case IToken.tSTAR:
case IToken.tNOT:
case IToken.tBITCOMPLEMENT:
case IToken.tINCR:
case IToken.tDECR:
case IToken.t_new:
case IToken.t_delete:
case IToken.t_sizeof:
case IToken.t_alignof:
case IGCCToken.t___alignof__:
return NO_TEMPLATE_ID;
// Start of a postfix expression
case IToken.t_typename:
case IToken.t_char:
case IToken.t_char16_t:
case IToken.t_char32_t:
case IToken.t_wchar_t:
case IToken.t_bool:
case IToken.t_short:
case IToken.t_int:
case IToken.t_long:
case IToken.t_signed:
case IToken.t_unsigned:
case IToken.t_float:
case IToken.t_double:
case IToken.t_dynamic_cast:
case IToken.t_static_cast:
case IToken.t_reinterpret_cast:
case IToken.t_const_cast:
case IToken.t_typeid:
return NO_TEMPLATE_ID;
// Start of a primary expression
case IToken.tINTEGER:
case IToken.tFLOATINGPT:
case IToken.tSTRING:
case IToken.tLSTRING:
case IToken.tUTF16STRING:
case IToken.tUTF32STRING:
case IToken.tCHAR:
case IToken.tLCHAR:
case IToken.tUTF16CHAR:
case IToken.tUTF32CHAR:
case IToken.t_false:
case IToken.t_true:
case IToken.t_this:
case IToken.tIDENTIFIER:
case IToken.t_operator:
case IToken.tCOMPLETION:
return NO_TEMPLATE_ID;
// Tokens that end an expression
case IToken.tSEMI:
case IToken.tCOMMA:
case IToken.tLBRACE:
case IToken.tRBRACE:
case IToken.tRBRACKET:
case IToken.tRPAREN:
case IToken.tELLIPSIS: // pack-expansion
case IToken.t_struct:
case IToken.t_class:
case IToken.t_template:
return TEMPLATE_ID;
// Binary operator
case IToken.tGT:
case IToken.tGT_in_SHIFTR:
case IToken.tEQUAL:
return TEMPLATE_ID;
default:
return AMBIGUOUS_TEMPLATE_ID;
}
}
private List<IASTNode> templateArgumentList(ITemplateIdStrategy strat)
throws EndOfFileException, BacktrackException {
if (templateArgumentNestingDepth >= TEMPLATE_ARGUMENT_NESTING_DEPTH_LIMIT) {
throwBacktrack(
createProblem(IProblem.TEMPLATE_ARGUMENT_NESTING_DEPTH_LIMIT_EXCEEDED, LA(1).getOffset(), 1));
}
++templateArgumentNestingDepth;
try {
int startingOffset = LA(1).getOffset();
int endOffset = 0;
List<IASTNode> list = null;
boolean needComma = false;
int lt1 = LT(1);
while (lt1 != IToken.tGT && lt1 != IToken.tGT_in_SHIFTR && lt1 != IToken.tEOC) {
if (needComma) {
if (lt1 != IToken.tCOMMA) {
throwBacktrack(startingOffset, endOffset - startingOffset);
}
consume();
} else {
needComma = true;
}
IASTNode node = templateArgument(strat);
if (list == null) {
list = new ArrayList<>();
}
list.add(node);
lt1 = LT(1);
}
if (list == null) {
return Collections.emptyList();
}
return list;
} finally {
--templateArgumentNestingDepth;
}
}
private IASTNode templateArgument(ITemplateIdStrategy strat) throws EndOfFileException, BacktrackException {
IToken argStart = mark();
int markBranchPoint = ((TemplateIdStrategy) strat).getCurrentBranchPoint();
ICPPASTTypeId typeId = null;
int lt1 = 0;
try {
typeId = typeId(DeclarationOptions.TYPEID, strat);
lt1 = LT(1);
} catch (BacktrackException e) {
if (e.isFatal()) {
throw e;
}
}
if (typeId != null && (lt1 == IToken.tCOMMA || lt1 == IToken.tGT || lt1 == IToken.tGT_in_SHIFTR
|| lt1 == IToken.tEOC || lt1 == IToken.tELLIPSIS)) {
// This is potentially a type-id, now check ambiguity with expression.
IToken typeIdEnd = mark();
IASTNamedTypeSpecifier namedTypeSpec = null;
IASTName name = null;
try {
// If the type-id consists of a name, that name could be or contain
// a template-id, with template arguments of its own, which can
// themselves be ambiguous. If we parse the name anew as an
// id-expression, our complexity becomes exponential in the nesting
// depth of template-ids (bug 316704). To avoid this, we do not
// re-parse the name, but instead synthesize an id-expression from
// it, and then continue parsing an expression from the id-expression
// onwards (as the id-expression could be the beginning of a larger
// expression).
IASTIdExpression idExpression = null;
IASTDeclSpecifier declSpec = typeId.getDeclSpecifier();
if (declSpec instanceof IASTNamedTypeSpecifier) {
namedTypeSpec = (IASTNamedTypeSpecifier) declSpec;
name = namedTypeSpec.getName();
if (name.contains(typeId)) {
idExpression = setRange(getNodeFactory().newIdExpression(name), name);
// If the name was one of the completion names, add it to the completion
// node again now that it has a new parent. This ensures that completion
// proposals are offered for both contexts that the name appears in.
ASTCompletionNode completionNode = (ASTCompletionNode) getCompletionNode();
if (completionNode != null && completionNode.containsName(name)) {
completionNode.addName(name);
}
}
}
// Parse an expression, starting with the id-expression synthesized
// above if there is one, otherwise starting from the beginning of
// the argument.
if (idExpression == null)
backup(argStart);
IASTExpression expression = expression(ExprKind.eAssignment, BinaryExprCtx.eInTemplateID, idExpression,
strat);
// At this point we have a valid type-id and a valid expression.
// We prefer the longer one.
if (!typeId.contains(expression)) {
// The expression is longer.
if (LT(1) == IToken.tELLIPSIS) {
expression = addPackExpansion(expression, consume());
}
return expression;
} else if (expression.contains(typeId)) {
// The two are of the same length - ambiguous.
if (LT(1) == IToken.tELLIPSIS) {
IToken ellipsis = consume();
addPackExpansion(typeId, ellipsis);
expression = addPackExpansion(expression, ellipsis);
}
ICPPASTAmbiguousTemplateArgument ambiguity = createAmbiguousTemplateArgument();
ambiguity.addTypeId(typeId);
ambiguity.addExpression(expression);
return ambiguity;
}
// The type-id is longer, use it.
} catch (BacktrackException e) {
if (e.isFatal()) {
throw e;
}
// Failed to parse an expression, use the type id.
}
// Clean up after our failed attempt to parse an expression.
backup(typeIdEnd);
if (name != null && namedTypeSpec != null) {
// When we synthesized the id-expression, it took ownership
// of the name. Give ownership back to the type-id.
namedTypeSpec.setName(name);
}
// Use the type-id.
if (LT(1) == IToken.tELLIPSIS) {
addPackExpansion(typeId, consume());
}
return typeId;
}
// Not a type-id, parse as expression.
backup(argStart);
((TemplateIdStrategy) strat).backupToBranchPoint(markBranchPoint);
IASTExpression expr = expression(ExprKind.eAssignment, BinaryExprCtx.eInTemplateID, null, strat);
if (LT(1) == IToken.tELLIPSIS) {
expr = addPackExpansion(expr, consume());
}
return expr;
}
private void addPackExpansion(ICPPASTTypeId typeId, IToken consume) {
final int endOffset = consume.getEndOffset();
adjustEndOffset(typeId, endOffset);
typeId.setIsPackExpansion(true);
}
private IASTExpression addPackExpansion(IASTExpression expr, IToken ellipsis) {
IASTExpression result = getNodeFactory().newPackExpansionExpression(expr);
return setRange(result, expr, ellipsis.getEndOffset());
}
private IASTName operatorId() throws BacktrackException, EndOfFileException {
final IToken firstToken = consume(IToken.t_operator);
int endOffset = firstToken.getEndOffset();
IASTTypeId typeId = null;
OverloadableOperator op = null;
final int lt1 = LT(1);
switch (lt1) {
case IToken.tLPAREN:
op = OverloadableOperator.PAREN; // operator ()
consume();
endOffset = consume(IToken.tRPAREN).getEndOffset();
break;
case IToken.tLBRACKET:
op = OverloadableOperator.BRACKET; // operator []
consume();
endOffset = consume(IToken.tRBRACKET).getEndOffset();
break;
case IToken.t_new:
case IToken.t_delete:
if (LT(2) == IToken.tLBRACKET) {
op = lt1 == IToken.t_new ? OverloadableOperator.NEW_ARRAY : OverloadableOperator.DELETE_ARRAY;
consume();
consume();
endOffset = consume(IToken.tRBRACKET).getEndOffset();
} else {
IToken t = consume();
endOffset = t.getEndOffset();
op = OverloadableOperator.valueOf(t);
}
break;
case IToken.tGT_in_SHIFTR:
consume();
endOffset = consume(IToken.tGT_in_SHIFTR).getEndOffset();
op = OverloadableOperator.SHIFTR;
break;
case IToken.tSTRING: // User defined literal T operator "" SUFFIX
{
IToken strOp = consume();
// Should be an empty string
if (strOp.getLength() == 2) {
endOffset = strOp.getEndOffset();
IToken udlOperator = identifierOrKeyword();
char[] operatorName = CharArrayUtils.concat(firstToken.getCharImage(), " ".toCharArray()); //$NON-NLS-1$
operatorName = CharArrayUtils.concat(operatorName, strOp.getCharImage());
operatorName = CharArrayUtils.concat(operatorName, udlOperator.getCharImage());
IASTName name = getNodeFactory().newOperatorName(operatorName);
setRange(name, firstToken.getOffset(), udlOperator.getEndOffset());
return name;
}
break;
}
case IToken.tUSER_DEFINED_STRING_LITERAL: // User defined literal T operator ""SUFFIX
{
IToken strOp = consume();
String image = strOp.getImage();
int startQuote = image.indexOf('"');
int endQuote = image.lastIndexOf('"');
if (startQuote != -1 && endQuote == startQuote + 1) {
char[] operatorName = CharArrayUtils.concat(firstToken.getCharImage(), " ".toCharArray()); //$NON-NLS-1$
operatorName = CharArrayUtils.concat(operatorName, strOp.getCharImage());
IASTName name = getNodeFactory().newOperatorName(operatorName);
setRange(name, firstToken.getOffset(), strOp.getEndOffset());
return name;
}
break;
}
default:
op = OverloadableOperator.valueOf(LA(1));
if (op != null) {
endOffset = consume().getEndOffset();
}
break;
}
if (op != null) {
IASTName name = getNodeFactory().newOperatorName(op.toCharArray());
setRange(name, firstToken.getOffset(), endOffset);
return name;
}
// must be a conversion function
typeId = typeId(DeclarationOptions.TYPEID_CONVERSION);
IASTName name = getNodeFactory().newConversionName(typeId);
setRange(name, firstToken.getOffset(), calculateEndOffset(typeId));
return name;
}
/**
* Information for the parser, whether a binary expression is parsed in the context of a
* template-id an ambiguous template-id (one where the '<' could be a greater sign) or
* else where.
*/
private enum BinaryExprCtx {
eInTemplateID, eNotInTemplateID, eInPrimaryExpression
}
@Override
protected ICPPASTExpression expression() throws BacktrackException, EndOfFileException {
return expression(ExprKind.eExpression, BinaryExprCtx.eNotInTemplateID, null, null);
}
@Override
protected ICPPASTExpression constantExpression() throws BacktrackException, EndOfFileException {