-
Notifications
You must be signed in to change notification settings - Fork 187
/
GNUCSourceParser.java
2227 lines (2023 loc) · 72.3 KB
/
GNUCSourceParser.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) 2005, 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 Rational Software) - Initial API and implementation
* Markus Schorn (Wind River Systems)
* Ed Swartz (Nokia)
* Mike Kucera (IBM) - bug #206952
* Sergey Prigogin (Google)
* Thomas Corbat (IFS)
* Anders Dahlberg (Ericsson) - bug 84144
* Alexander Nyßen (itemis AG) - bug 475908
*******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.c;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
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.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.IASTFieldDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTFieldReference;
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.IASTFunctionDefinition;
import org.eclipse.cdt.core.dom.ast.IASTIdExpression;
import org.eclipse.cdt.core.dom.ast.IASTIfStatement;
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.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration;
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.IASTSimpleDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTStandardFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
import org.eclipse.cdt.core.dom.ast.IASTSwitchStatement;
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.c.ICASTArrayDesignator;
import org.eclipse.cdt.core.dom.ast.c.ICASTArrayModifier;
import org.eclipse.cdt.core.dom.ast.c.ICASTCompositeTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.c.ICASTDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.c.ICASTDesignatedInitializer;
import org.eclipse.cdt.core.dom.ast.c.ICASTDesignator;
import org.eclipse.cdt.core.dom.ast.c.ICASTElaboratedTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.c.ICASTEnumerationSpecifier;
import org.eclipse.cdt.core.dom.ast.c.ICASTFieldDesignator;
import org.eclipse.cdt.core.dom.ast.c.ICASTPointer;
import org.eclipse.cdt.core.dom.ast.c.ICASTSimpleDeclSpecifier;
import org.eclipse.cdt.core.dom.ast.c.ICASTTypedefNameSpecifier;
import org.eclipse.cdt.core.dom.ast.c.ICNodeFactory;
import org.eclipse.cdt.core.dom.ast.gnu.c.ICASTKnRFunctionDeclarator;
import org.eclipse.cdt.core.dom.ast.gnu.c.IGCCASTArrayRangeDesignator;
import org.eclipse.cdt.core.dom.parser.IExtensionToken;
import org.eclipse.cdt.core.dom.parser.c.ICParserExtensionConfiguration;
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.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.IASTAmbiguousExpression;
import org.eclipse.cdt.internal.core.dom.parser.IASTAmbiguousStatement;
/**
* Source parser for gnu-c syntax.
*/
public class GNUCSourceParser extends AbstractGNUSourceCodeParser {
private static final int DEFAULT_POINTEROPS_LIST_SIZE = 4;
private static final int DEFAULT_PARAMETERS_LIST_SIZE = 4;
private final boolean supportGCCStyleDesignators;
private IIndex index;
protected IASTTranslationUnit translationUnit;
private int fPreventKnrCheck = 0;
public GNUCSourceParser(IScanner scanner, ParserMode parserMode, IParserLogService logService,
ICParserExtensionConfiguration config) {
this(scanner, parserMode, logService, config, null);
}
public GNUCSourceParser(IScanner scanner, ParserMode parserMode, IParserLogService logService,
ICParserExtensionConfiguration config, IIndex index) {
super(scanner, logService, parserMode, CNodeFactory.getDefault(), config.supportStatementsInExpressions(),
config.supportTypeofUnaryExpressions(), config.supportAlignOfUnaryExpression(), config.supportKnRC(),
config.supportAttributeSpecifiers(), config.supportDeclspecSpecifiers(),
config.getBuiltinBindingsProvider());
supportGCCStyleDesignators = config.supportGCCStyleDesignators();
supportParameterInfoBlock = config.supportParameterInfoBlock();
supportExtendedSizeofOperator = config.supportExtendedSizeofOperator();
supportFunctionStyleAsm = config.supportFunctionStyleAssembler();
this.index = index;
}
@Override
protected ICNodeFactory getNodeFactory() {
return (ICNodeFactory) super.getNodeFactory();
}
@Override
protected IASTInitializer optionalInitializer(IASTDeclarator dtor, DeclarationOptions options)
throws EndOfFileException, BacktrackException {
if (options.fAllowInitializer && LTcatchEOF(1) == IToken.tASSIGN) {
final int offset = consume().getOffset();
IASTInitializerClause initClause = initClause();
IASTEqualsInitializer result = getNodeFactory().newEqualsInitializer(initClause);
return setRange(result, offset, calculateEndOffset(initClause));
}
return null;
}
private IASTInitializerClause initClause() throws EndOfFileException, BacktrackException {
final int offset = LA(1).getOffset();
if (LT(1) != IToken.tLBRACE)
return expression(ExprKind.eAssignment);
// it's an aggregate initializer
consume(IToken.tLBRACE);
IASTInitializerList result = getNodeFactory().newInitializerList();
// bug 196468, gcc accepts empty braces.
if (supportGCCStyleDesignators && LT(1) == IToken.tRBRACE) {
int endOffset = consume().getEndOffset();
setRange(result, offset, endOffset);
return result;
}
for (;;) {
final int checkOffset = LA(1).getOffset();
// required at least one initializer list
// get designator list
List<? extends ICASTDesignator> designator = designatorList();
if (designator == null) {
IASTInitializerClause clause = initClause();
if (result.getSize() >= maximumTrivialExpressionsInAggregateInitializers
&& !ASTQueries.canContainName(clause)) {
translationUnit.setHasNodesOmitted(true);
clause = null;
}
// depending on value of skipTrivialItemsInCompoundInitializers initializer may be null
// in any way add the initializer such that the actual size can be tracked.
result.addClause(clause);
} else {
ICASTDesignatedInitializer desigInitializer = getNodeFactory()
.newDesignatedInitializer((IASTInitializerClause) null);
setRange(desigInitializer, designator.get(0));
for (ICASTDesignator d : designator) {
desigInitializer.addDesignator(d);
}
if (LT(1) != IToken.tEOC) {
// Gnu extension: the assign operator is optional
if (LT(1) == IToken.tASSIGN)
consume(IToken.tASSIGN);
IASTInitializerClause clause = initClause();
desigInitializer.setOperand(clause);
adjustLength(desigInitializer, clause);
}
result.addClause(desigInitializer);
}
// can end with ", }" or "}"
boolean canContinue = LT(1) == IToken.tCOMMA;
if (canContinue)
consume();
switch (LT(1)) {
case IToken.tRBRACE:
int lastOffset = consume().getEndOffset();
setRange(result, offset, lastOffset);
return result;
case IToken.tEOC:
setRange(result, offset, LA(1).getOffset());
return result;
}
if (!canContinue || LA(1).getOffset() == checkOffset) {
throwBacktrack(offset, LA(1).getEndOffset() - offset);
}
}
// consume the closing brace
}
private List<? extends ICASTDesignator> designatorList() throws EndOfFileException, BacktrackException {
final int lt1 = LT(1);
if (lt1 == IToken.tDOT || lt1 == IToken.tLBRACKET) {
List<ICASTDesignator> designatorList = null;
while (true) {
switch (LT(1)) {
case IToken.tDOT:
int offset = consume().getOffset();
IASTName n = identifier();
ICASTFieldDesignator fieldDesignator = getNodeFactory().newFieldDesignator(n);
setRange(fieldDesignator, offset, calculateEndOffset(n));
if (designatorList == null)
designatorList = new ArrayList<>(DEFAULT_DESIGNATOR_LIST_SIZE);
designatorList.add(fieldDesignator);
break;
case IToken.tLBRACKET:
offset = consume().getOffset();
IASTExpression constantExpression = expression();
if (supportGCCStyleDesignators && LT(1) == IToken.tELLIPSIS) {
consume(IToken.tELLIPSIS);
IASTExpression constantExpression2 = expression();
int lastOffset = consume(IToken.tRBRACKET).getEndOffset();
IGCCASTArrayRangeDesignator designator = getNodeFactory()
.newArrayRangeDesignatorGCC(constantExpression, constantExpression2);
setRange(designator, offset, lastOffset);
if (designatorList == null)
designatorList = new ArrayList<>(DEFAULT_DESIGNATOR_LIST_SIZE);
designatorList.add(designator);
} else {
int lastOffset = consume(IToken.tRBRACKET).getEndOffset();
ICASTArrayDesignator designator = getNodeFactory().newArrayDesignator(constantExpression);
setRange(designator, offset, lastOffset);
if (designatorList == null)
designatorList = new ArrayList<>(DEFAULT_DESIGNATOR_LIST_SIZE);
designatorList.add(designator);
}
break;
default:
return designatorList;
}
}
}
// fix for 84176: if reach identifier and it's not a designator then return empty designator list
if (supportGCCStyleDesignators && lt1 == IToken.tIDENTIFIER && LT(2) == IToken.tCOLON) {
int offset = LA(1).getOffset();
IASTName n = identifier();
int lastOffset = consume(IToken.tCOLON).getEndOffset();
ICASTFieldDesignator designator = getNodeFactory().newFieldDesignator(n);
setRange(designator, offset, lastOffset);
return Collections.singletonList(designator);
}
return null;
}
@Override
protected IASTDeclaration declaration(final DeclarationOptions declOption)
throws EndOfFileException, BacktrackException {
switch (LT(1)) {
case IToken.t_asm:
return asmDeclaration();
case IToken.tSEMI:
IToken semi = consume();
IASTDeclSpecifier declspec = getNodeFactory().newSimpleDeclSpecifier();
IASTSimpleDeclaration decl = getNodeFactory().newSimpleDeclaration(declspec);
decl.setDeclSpecifier(declspec);
((ASTNode) declspec).setOffsetAndLength(semi.getOffset(), 0);
((ASTNode) decl).setOffsetAndLength(semi.getOffset(), semi.getLength());
return decl;
}
return simpleDeclaration(declOption);
}
private IASTDeclaration simpleDeclaration(final DeclarationOptions declOption)
throws BacktrackException, EndOfFileException {
if (LT(1) == IToken.tLBRACE)
throwBacktrack(LA(1));
final int firstOffset = LA(1).getOffset();
int endOffset = firstOffset;
boolean insertSemi = false;
IASTDeclSpecifier declSpec = null;
IASTDeclarator dtor = null;
IASTDeclSpecifier altDeclSpec = null;
IASTDeclarator altDtor = null;
IToken markBeforDtor = null;
try {
Decl decl = declSpecifierSequence_initDeclarator(declOption, true);
markBeforDtor = decl.fDtorToken1;
declSpec = decl.fDeclSpec1;
dtor = decl.fDtor1;
altDeclSpec = decl.fDeclSpec2;
altDtor = decl.fDtor2;
} catch (FoundAggregateInitializer lie) {
declSpec = lie.fDeclSpec;
// scalability: don't keep references to tokens, initializer may be large
declarationMark = null;
dtor = addInitializer(lie, declOption);
} catch (BacktrackException e) {
IASTNode node = e.getNodeBeforeProblem();
if (node instanceof IASTDeclSpecifier) {
IASTSimpleDeclaration d = getNodeFactory().newSimpleDeclaration((IASTDeclSpecifier) node);
setRange(d, node);
throwBacktrack(e.getProblem(), d);
}
throw e;
}
IASTDeclarator[] declarators = IASTDeclarator.EMPTY_DECLARATOR_ARRAY;
if (dtor != null) {
declarators = new IASTDeclarator[] { dtor };
while (LTcatchEOF(1) == IToken.tCOMMA) {
consume();
try {
dtor = initDeclarator(declSpec, declOption);
} catch (FoundAggregateInitializer e) {
// scalability: don't keep references to tokens, initializer may be large
declarationMark = null;
markBeforDtor = null;
dtor = addInitializer(e, declOption);
}
declarators = ArrayUtil.append(IASTDeclarator.class, declarators, dtor);
}
declarators = ArrayUtil.removeNulls(IASTDeclarator.class, declarators);
}
final int lt1 = LTcatchEOF(1);
switch (lt1) {
case IToken.tEOC:
endOffset = figureEndOffset(declSpec, declarators);
break;
case IToken.tSEMI:
endOffset = consume().getEndOffset();
break;
case IToken.tLBRACE:
return functionDefinition(firstOffset, declSpec, declarators);
default:
insertSemi = true;
if (declOption == DeclarationOptions.LOCAL) {
endOffset = figureEndOffset(declSpec, declarators);
if (firstOffset != endOffset) {
break;
}
} else {
if (markBeforDtor != null) {
endOffset = calculateEndOffset(declSpec);
if (firstOffset != endOffset && !isOnSameLine(endOffset, markBeforDtor.getOffset())) {
backup(markBeforDtor);
declarators = IASTDeclarator.EMPTY_DECLARATOR_ARRAY;
break;
}
}
endOffset = figureEndOffset(declSpec, declarators);
if (lt1 == 0) {
break;
}
if (firstOffset != endOffset) {
if (!isOnSameLine(endOffset, LA(1).getOffset())) {
break;
}
if (declarators.length == 1 && declarators[0] instanceof IASTFunctionDeclarator) {
break;
}
}
}
throwBacktrack(LA(1));
}
// no function body
IASTSimpleDeclaration simpleDeclaration = getNodeFactory().newSimpleDeclaration(declSpec);
for (IASTDeclarator declarator : declarators)
simpleDeclaration.addDeclarator(declarator);
setRange(simpleDeclaration, firstOffset, endOffset);
if (altDeclSpec != null && altDtor != null) {
simpleDeclaration = new CASTAmbiguousSimpleDeclaration(simpleDeclaration, altDeclSpec, altDtor);
setRange(simpleDeclaration, firstOffset, endOffset);
}
if (insertSemi) {
IASTProblem problem = createProblem(IProblem.MISSING_SEMICOLON, endOffset - 1, 1);
throwBacktrack(problem, simpleDeclaration);
}
return simpleDeclaration;
}
private IASTDeclaration functionDefinition(int firstOffset, IASTDeclSpecifier declSpec,
IASTDeclarator[] declarators) throws BacktrackException, EndOfFileException {
if (declarators.length != 1)
throwBacktrack(firstOffset, LA(1).getEndOffset());
final IASTDeclarator outerDtor = declarators[0];
final IASTDeclarator fdtor = ASTQueries.findTypeRelevantDeclarator(outerDtor);
if (!(fdtor instanceof IASTFunctionDeclarator))
throwBacktrack(firstOffset, LA(1).getEndOffset() - firstOffset);
IASTFunctionDefinition funcDefinition = getNodeFactory().newFunctionDefinition(declSpec,
(IASTFunctionDeclarator) fdtor, null);
try {
IASTStatement s = handleFunctionBody();
funcDefinition.setBody(s);
((ASTNode) funcDefinition).setOffsetAndLength(firstOffset, calculateEndOffset(s) - firstOffset);
return funcDefinition;
} catch (BacktrackException bt) {
final IASTNode n = bt.getNodeBeforeProblem();
if (n instanceof IASTCompoundStatement) {
funcDefinition.setBody((IASTCompoundStatement) n);
((ASTNode) funcDefinition).setOffsetAndLength(firstOffset, calculateEndOffset(n) - firstOffset);
throwBacktrack(bt.getProblem(), funcDefinition);
}
throw bt;
}
}
@Override
protected void setupTranslationUnit() throws DOMException {
translationUnit = getNodeFactory().newTranslationUnit(scanner);
translationUnit.setIndex(index);
// add built-in names to the scope
if (builtinBindingsProvider != null) {
if (translationUnit instanceof ASTTranslationUnit) {
((ASTTranslationUnit) translationUnit).setupBuiltinBindings(builtinBindingsProvider);
}
}
}
@Override
protected IASTExpression expression() throws BacktrackException, EndOfFileException {
return expression(ExprKind.eExpression);
}
@Override
protected IASTExpression constantExpression() throws BacktrackException, EndOfFileException {
return expression(ExprKind.eConstant);
}
private IASTExpression expression(final ExprKind kind) throws EndOfFileException, BacktrackException {
final boolean allowComma = kind == ExprKind.eExpression;
boolean allowAssignment = kind != ExprKind.eConstant;
int lt1;
int conditionCount = 0;
BinaryOperator lastOperator = null;
IASTExpression lastExpression = castExpression(CastExprCtx.eDirectlyInBExpr, null);
loop: while (true) {
lt1 = LT(1);
switch (lt1) {
case IToken.tQUESTION:
conditionCount++;
// <logical-or> ? <expression> : <assignment-expression>
// Precedence: 25 is lower than precedence of logical or; 0 is lower than precedence of expression
lastOperator = new BinaryOperator(lastOperator, lastExpression, lt1, 25, 0);
if (LT(2) == IToken.tCOLON) {
// Gnu extension: The expression after '?' can be omitted.
consume(); // Consume operator
lastExpression = null; // Next cast expression is just null
continue;
}
allowAssignment = true; // assignment expressions will be subsumed by the conditional expression
break;
case IToken.tCOLON:
if (--conditionCount < 0)
break loop;
// <logical-or> ? <expression> : <assignment-expression>
// Precedence: 0 is lower than precedence of expression; 15 is lower than precedence of assignment;
lastOperator = new BinaryOperator(lastOperator, lastExpression, lt1, 0, 15);
allowAssignment = true; // assignment expressions will be subsumed by the conditional expression
break;
case IToken.tCOMMA:
if (!allowComma && conditionCount == 0)
break loop;
// Lowest precedence except inside the conditional expression
lastOperator = new BinaryOperator(lastOperator, lastExpression, lt1, 10, 11);
break;
case IToken.tASSIGN:
case IToken.tSTARASSIGN:
case IToken.tDIVASSIGN:
case IToken.tMODASSIGN:
case IToken.tPLUSASSIGN:
case IToken.tMINUSASSIGN:
case IToken.tSHIFTRASSIGN:
case IToken.tSHIFTLASSIGN:
case IToken.tAMPERASSIGN:
case IToken.tXORASSIGN:
case IToken.tBITORASSIGN:
if (!allowAssignment && conditionCount == 0)
break loop;
// Assignments group right to left
lastOperator = new BinaryOperator(lastOperator, lastExpression, lt1, 21, 20);
break;
case IToken.tOR:
lastOperator = new BinaryOperator(lastOperator, lastExpression, lt1, 30, 31);
break;
case IToken.tAND:
lastOperator = new BinaryOperator(lastOperator, lastExpression, lt1, 40, 41);
break;
case IToken.tBITOR:
lastOperator = new BinaryOperator(lastOperator, lastExpression, lt1, 50, 51);
break;
case IToken.tXOR:
lastOperator = new BinaryOperator(lastOperator, lastExpression, lt1, 60, 61);
break;
case IToken.tAMPER:
lastOperator = new BinaryOperator(lastOperator, lastExpression, lt1, 70, 71);
break;
case IToken.tEQUAL:
case IToken.tNOTEQUAL:
lastOperator = new BinaryOperator(lastOperator, lastExpression, lt1, 80, 81);
break;
case IToken.tGT:
case IToken.tLT:
case IToken.tLTEQUAL:
case IToken.tGTEQUAL:
case IGCCToken.tMAX:
case IGCCToken.tMIN:
lastOperator = new BinaryOperator(lastOperator, lastExpression, lt1, 90, 91);
break;
case IToken.tSHIFTL:
case IToken.tSHIFTR:
lastOperator = new BinaryOperator(lastOperator, lastExpression, lt1, 100, 101);
break;
case IToken.tPLUS:
case IToken.tMINUS:
lastOperator = new BinaryOperator(lastOperator, lastExpression, lt1, 110, 111);
break;
case IToken.tSTAR:
case IToken.tDIV:
case IToken.tMOD:
lastOperator = new BinaryOperator(lastOperator, lastExpression, lt1, 120, 121);
break;
default:
break loop;
}
consume(); // consume operator
lastExpression = castExpression(CastExprCtx.eDirectlyInBExpr, null); // next cast expression
}
// Check for incomplete conditional expression
if (lt1 != IToken.tEOC && conditionCount > 0)
throwBacktrack(LA(1));
return buildExpression(lastOperator, lastExpression);
}
@Override
protected IASTExpression buildBinaryExpression(int operator, IASTExpression expr1, IASTInitializerClause expr2,
int lastOffset) {
IASTBinaryExpression result = getNodeFactory().newBinaryExpression(operator, expr1, (IASTExpression) expr2);
int o = ((ASTNode) expr1).getOffset();
((ASTNode) result).setOffsetAndLength(o, lastOffset - o);
return result;
}
@Override
protected IASTExpression unaryExpression(CastExprCtx ctx, ITemplateIdStrategy strat)
throws EndOfFileException, BacktrackException {
switch (LT(1)) {
case IToken.tSTAR:
return unaryExpression(IASTUnaryExpression.op_star, ctx, strat);
case IToken.tAMPER:
return unaryExpression(IASTUnaryExpression.op_amper, ctx, strat);
case IToken.tAND:
return unaryExpression(IASTUnaryExpression.op_labelReference, ctx, strat);
case IToken.tPLUS:
return unaryExpression(IASTUnaryExpression.op_plus, ctx, strat);
case IToken.tMINUS:
return unaryExpression(IASTUnaryExpression.op_minus, ctx, strat);
case IToken.tNOT:
return unaryExpression(IASTUnaryExpression.op_not, ctx, strat);
case IToken.tBITCOMPLEMENT:
return unaryExpression(IASTUnaryExpression.op_tilde, ctx, strat);
case IToken.tINCR:
return unaryExpression(IASTUnaryExpression.op_prefixIncr, ctx, strat);
case IToken.tDECR:
return unaryExpression(IASTUnaryExpression.op_prefixDecr, ctx, strat);
case IToken.t_sizeof:
return parseTypeidInParenthesisOrUnaryExpression(false, consume().getOffset(),
IASTTypeIdExpression.op_sizeof, IASTUnaryExpression.op_sizeof, ctx, strat);
case IToken.t__Alignof:
case IGCCToken.t___alignof__:
return parseTypeidInParenthesisOrUnaryExpression(false, consume().getOffset(),
IASTTypeIdExpression.op_alignof, IASTUnaryExpression.op_alignOf, ctx, strat);
default:
return postfixExpression(ctx, strat);
}
}
private IASTExpression postfixExpression(CastExprCtx ctx, ITemplateIdStrategy strat)
throws EndOfFileException, BacktrackException {
IASTExpression firstExpression = null;
switch (LT(1)) {
case IToken.tLPAREN:
// (type-name) { initializer-list }
// (type-name) { initializer-list , }
IToken m = mark();
try {
int offset = consume().getOffset();
IASTTypeId t = typeId(DeclarationOptions.TYPEID);
consume(IToken.tRPAREN);
if (LT(1) == IToken.tLBRACE) {
IASTInitializer i = (IASTInitializerList) initClause();
firstExpression = getNodeFactory().newTypeIdInitializerExpression(t, i);
setRange(firstExpression, offset, calculateEndOffset(i));
break;
}
} catch (BacktrackException bt) {
}
backup(m);
firstExpression = primaryExpression(ctx, strat);
break;
default:
firstExpression = primaryExpression(ctx, strat);
break;
}
IASTExpression secondExpression = null;
for (;;) {
switch (LT(1)) {
case IToken.tLBRACKET:
// array access
consume();
secondExpression = expression();
int last;
switch (LT(1)) {
case IToken.tRBRACKET:
last = consume().getEndOffset();
break;
case IToken.tEOC:
last = Integer.MAX_VALUE;
break;
default:
throw backtrack;
}
IASTArraySubscriptExpression s = getNodeFactory().newArraySubscriptExpression(firstExpression,
secondExpression);
((ASTNode) s).setOffsetAndLength(((ASTNode) firstExpression).getOffset(),
last - ((ASTNode) firstExpression).getOffset());
firstExpression = s;
break;
case IToken.tLPAREN:
// function call
int endOffset;
List<IASTExpression> argList = null;
consume(IToken.tLPAREN);
boolean isFirst = true;
while (true) {
final int lt1 = LT(1);
if (lt1 == IToken.tRPAREN) {
endOffset = consume().getEndOffset();
break;
} else if (lt1 == IToken.tEOC) {
endOffset = LA(1).getEndOffset();
break;
}
if (isFirst) {
isFirst = false;
} else {
consume(IToken.tCOMMA);
}
IASTExpression expr = expression(ExprKind.eAssignment);
if (argList == null) {
argList = new ArrayList<>();
}
argList.add(expr);
}
final IASTExpression[] args;
if (argList == null) {
args = IASTExpression.EMPTY_EXPRESSION_ARRAY;
} else {
args = argList.toArray(new IASTExpression[argList.size()]);
}
IASTFunctionCallExpression f = getNodeFactory().newFunctionCallExpression(firstExpression, args);
firstExpression = setRange(f, firstExpression, endOffset);
break;
case IToken.tINCR:
int offset = consume().getEndOffset();
firstExpression = buildUnaryExpression(IASTUnaryExpression.op_postFixIncr, firstExpression,
((ASTNode) firstExpression).getOffset(), offset);
break;
case IToken.tDECR:
offset = consume().getEndOffset();
firstExpression = buildUnaryExpression(IASTUnaryExpression.op_postFixDecr, firstExpression,
((ASTNode) firstExpression).getOffset(), offset);
break;
case IToken.tDOT:
// member access
IToken dot = consume();
IASTName name = identifier();
if (name == null)
throwBacktrack(((ASTNode) firstExpression).getOffset(),
((ASTNode) firstExpression).getLength() + dot.getLength());
IASTFieldReference result = getNodeFactory().newFieldReference(name, firstExpression);
result.setIsPointerDereference(false);
((ASTNode) result).setOffsetAndLength(((ASTNode) firstExpression).getOffset(),
calculateEndOffset(name) - ((ASTNode) firstExpression).getOffset());
firstExpression = result;
break;
case IToken.tARROW:
// member access
IToken arrow = consume();
name = identifier();
if (name == null)
throwBacktrack(((ASTNode) firstExpression).getOffset(),
((ASTNode) firstExpression).getLength() + arrow.getLength());
result = getNodeFactory().newFieldReference(name, firstExpression);
result.setIsPointerDereference(true);
((ASTNode) result).setOffsetAndLength(((ASTNode) firstExpression).getOffset(),
calculateEndOffset(name) - ((ASTNode) firstExpression).getOffset());
firstExpression = result;
break;
default:
return firstExpression;
}
}
}
@Override
protected IASTExpression primaryExpression(CastExprCtx ctx, ITemplateIdStrategy strat)
throws EndOfFileException, BacktrackException {
IToken t = null;
IASTLiteralExpression literalExpression = null;
switch (LT(1)) {
// TO DO: we need more literals...
case IToken.tINTEGER:
t = consume();
literalExpression = getNodeFactory().newLiteralExpression(IASTLiteralExpression.lk_integer_constant,
t.getImage());
((ASTNode) literalExpression).setOffsetAndLength(t.getOffset(), t.getEndOffset() - t.getOffset());
return literalExpression;
case IToken.tFLOATINGPT:
t = consume();
literalExpression = getNodeFactory().newLiteralExpression(IASTLiteralExpression.lk_float_constant,
t.getImage());
((ASTNode) literalExpression).setOffsetAndLength(t.getOffset(), t.getEndOffset() - t.getOffset());
return literalExpression;
case IToken.tSTRING:
case IToken.tLSTRING:
case IToken.tUTF16STRING:
case IToken.tUTF32STRING:
t = consume();
literalExpression = getNodeFactory().newLiteralExpression(IASTLiteralExpression.lk_string_literal,
t.getImage());
((ASTNode) literalExpression).setOffsetAndLength(t.getOffset(), t.getEndOffset() - t.getOffset());
return literalExpression;
case IToken.tCHAR:
case IToken.tLCHAR:
case IToken.tUTF16CHAR:
case IToken.tUTF32CHAR:
t = consume();
literalExpression = getNodeFactory().newLiteralExpression(IASTLiteralExpression.lk_char_constant,
t.getImage());
((ASTNode) literalExpression).setOffsetAndLength(t.getOffset(), t.getLength());
return literalExpression;
case IToken.tLPAREN:
if (supportStatementsInExpressions && LT(2) == IToken.tLBRACE) {
return compoundStatementExpression();
}
t = consume();
IASTExpression lhs = expression(ExprKind.eExpression); // instead of expression(), to keep the stack smaller
int finalOffset = 0;
switch (LT(1)) {
case IToken.tRPAREN:
case IToken.tEOC:
finalOffset = consume().getEndOffset();
break;
default:
throwBacktrack(LA(1));
}
return buildUnaryExpression(IASTUnaryExpression.op_bracketedPrimary, lhs, t.getOffset(), finalOffset);
case IToken.tIDENTIFIER:
case IToken.tCOMPLETION:
case IToken.tEOC:
int startingOffset = LA(1).getOffset();
IASTName name = identifier();
IASTIdExpression idExpression = getNodeFactory().newIdExpression(name);
((ASTNode) idExpression).setOffsetAndLength((ASTNode) name);
return idExpression;
default:
IToken la = LA(1);
startingOffset = la.getOffset();
throwBacktrack(startingOffset, la.getLength());
return null;
}
}
@Override
protected IASTTypeId typeId(DeclarationOptions option) throws EndOfFileException, BacktrackException {
if (!canBeTypeSpecifier()) {
return null;
}
final int offset = mark().getOffset();
IASTDeclSpecifier declSpecifier = null;
IASTDeclarator declarator = null;
fPreventKnrCheck++;
try {
Decl decl = declSpecifierSequence_initDeclarator(option, false);
declSpecifier = decl.fDeclSpec1;
declarator = decl.fDtor1;
} catch (FoundAggregateInitializer lie) {
// type-ids have not compound initializers
throwBacktrack(lie.fDeclarator);
} finally {
fPreventKnrCheck--;
}
IASTTypeId result = getNodeFactory().newTypeId(declSpecifier, declarator);
setRange(result, offset, figureEndOffset(declSpecifier, declarator));
return result;
}
/**
* Parse a Pointer Operator.
*
* ptrOperator : "*" (cvQualifier)* | "&" | ::? nestedNameSpecifier "*"
* (cvQualifier)*
*
* @throws BacktrackException to request a backtrack
*/
protected void consumePointerOperators(List<IASTPointerOperator> pointerOps)
throws EndOfFileException, BacktrackException {
for (;;) {
// __attribute__ in-between pointers
__attribute_decl_seq(supportAttributeSpecifiers, false);
IToken mark = mark();
IToken last = null;
boolean isConst = false, isVolatile = false, isRestrict = false;
if (LT(1) != IToken.tSTAR) {
backup(mark);
break;
}
last = consume();
int startOffset = mark.getOffset();
for (;;) {
IToken t = LA(1);
switch (LT(1)) {
case IToken.t_const:
last = consume();
isConst = true;
break;
case IToken.t_volatile:
last = consume();
isVolatile = true;
break;
case IToken.t_restrict:
last = consume();
isRestrict = true;
break;
}
if (t == LA(1))
break;
}
ICASTPointer po = getNodeFactory().newPointer();
((ASTNode) po).setOffsetAndLength(startOffset, last.getEndOffset() - startOffset);
po.setConst(isConst);
po.setVolatile(isVolatile);
po.setRestrict(isRestrict);
pointerOps.add(po);
}
}
private final static int INLINE = 0x1, CONST = 0x2, RESTRICT = 0x4, VOLATILE = 0x8, SHORT = 0x10, UNSIGNED = 0x20,
SIGNED = 0x40, COMPLEX = 0x80, IMAGINARY = 0x100;
@Override
protected Decl declSpecifierSeq(final DeclarationOptions declOption, ITemplateIdStrategy strat)
throws BacktrackException, EndOfFileException {
int storageClass = IASTDeclSpecifier.sc_unspecified;
int simpleType = IASTSimpleDeclSpecifier.t_unspecified;
int options = 0;
int isLong = 0;
IToken returnToken = null;
ICASTDeclSpecifier result = null;
ICASTDeclSpecifier altResult = null;
IASTAlignmentSpecifier[] alignmentSpecifiers = IASTAlignmentSpecifier.EMPTY_ALIGNMENT_SPECIFIER_ARRAY;
List<IASTAttributeSpecifier> attributes = null;
try {
IASTName identifier = null;
IASTExpression typeofExpression = null;
IASTProblem problem = null;
boolean encounteredRawType = false;
boolean encounteredTypename = false;
final int offset = LA(1).getOffset();
int endOffset = offset;
declSpecifiers: for (;;) {
final int lt1 = LTcatchEOF(1);
switch (lt1) {
case 0: // eof
break declSpecifiers;
// storage class specifiers
case IToken.t_auto:
storageClass = IASTDeclSpecifier.sc_auto;
endOffset = consume().getEndOffset();
break;
case IToken.t_register:
storageClass = IASTDeclSpecifier.sc_register;
endOffset = consume().getEndOffset();
break;
case IToken.t_static:
storageClass = IASTDeclSpecifier.sc_static;
endOffset = consume().getEndOffset();
break;
case IToken.t_extern:
storageClass = IASTDeclSpecifier.sc_extern;
endOffset = consume().getEndOffset();
break;
case IToken.t_typedef:
storageClass = IASTDeclSpecifier.sc_typedef;
endOffset = consume().getEndOffset();
break;
// Function Specifier
case IToken.t_inline:
options |= INLINE;
endOffset = consume().getEndOffset();
break;
// Type Qualifiers
case IToken.t_const:
options |= CONST;
endOffset = consume().getEndOffset();
break;
case IToken.t_volatile:
options |= VOLATILE;
endOffset = consume().getEndOffset();
break;
case IToken.t_restrict:
options |= RESTRICT;
endOffset = consume().getEndOffset();
break;
// Type Specifiers
case IToken.t_void:
if (encounteredTypename)
break declSpecifiers;
simpleType = IASTSimpleDeclSpecifier.t_void;
encounteredRawType = true;
endOffset = consume().getEndOffset();
break;
case IToken.t_char:
if (encounteredTypename)