-
Notifications
You must be signed in to change notification settings - Fork 187
/
CPPSemantics.java
4728 lines (4358 loc) · 177 KB
/
CPPSemantics.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) 2004, 2016 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:
* Andrew Niefer (IBM) - Initial API and implementation
* Markus Schorn (Wind River Systems)
* Bryan Wilkinson (QNX)
* Andrew Ferguson (Symbian)
* Sergey Prigogin (Google)
* Mike Kucera (IBM)
* Thomas Corbat (IFS)
* Nathan Ridge
* Richard Eames
*******************************************************************************/
package org.eclipse.cdt.internal.core.dom.parser.cpp.semantics;
import static org.eclipse.cdt.core.dom.ast.IASTExpression.ValueCategory.LVALUE;
import static org.eclipse.cdt.core.dom.ast.IASTExpression.ValueCategory.PRVALUE;
import static org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil.ALLCVQ;
import static org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil.ARRAY;
import static org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil.CVTYPE;
import static org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil.MPTR;
import static org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil.PTR;
import static org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil.REF;
import static org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil.TDEF;
import static org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil.calculateInheritanceDepth;
import static org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil.getNestedType;
import static org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil.getUltimateTypeUptoPointers;
import static org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.SemanticUtil.isConversionOperator;
import java.util.ArrayDeque;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Deque;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.IName;
import org.eclipse.cdt.core.dom.ast.ASTNodeProperty;
import org.eclipse.cdt.core.dom.ast.ASTVisitor;
import org.eclipse.cdt.core.dom.ast.DOMException;
import org.eclipse.cdt.core.dom.ast.EScopeKind;
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.IASTDeclarationStatement;
import org.eclipse.cdt.core.dom.ast.IASTDeclarator;
import org.eclipse.cdt.core.dom.ast.IASTElaboratedTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTEnumerationSpecifier.IASTEnumerator;
import org.eclipse.cdt.core.dom.ast.IASTEqualsInitializer;
import org.eclipse.cdt.core.dom.ast.IASTExpression;
import org.eclipse.cdt.core.dom.ast.IASTExpression.ValueCategory;
import org.eclipse.cdt.core.dom.ast.IASTFieldReference;
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.IASTImplicitNameOwner;
import org.eclipse.cdt.core.dom.ast.IASTInitializer;
import org.eclipse.cdt.core.dom.ast.IASTInitializerClause;
import org.eclipse.cdt.core.dom.ast.IASTLabelStatement;
import org.eclipse.cdt.core.dom.ast.IASTLiteralExpression;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNameOwner;
import org.eclipse.cdt.core.dom.ast.IASTNamedTypeSpecifier;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTParameterDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTReturnStatement;
import org.eclipse.cdt.core.dom.ast.IASTSimpleDeclaration;
import org.eclipse.cdt.core.dom.ast.IASTStatement;
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.IASTTypeIdInitializerExpression;
import org.eclipse.cdt.core.dom.ast.IASTUnaryExpression;
import org.eclipse.cdt.core.dom.ast.IBasicType;
import org.eclipse.cdt.core.dom.ast.IBasicType.Kind;
import org.eclipse.cdt.core.dom.ast.IBinding;
import org.eclipse.cdt.core.dom.ast.ICompositeType;
import org.eclipse.cdt.core.dom.ast.IEnumeration;
import org.eclipse.cdt.core.dom.ast.IEnumerator;
import org.eclipse.cdt.core.dom.ast.IFunction;
import org.eclipse.cdt.core.dom.ast.IFunctionType;
import org.eclipse.cdt.core.dom.ast.IPointerType;
import org.eclipse.cdt.core.dom.ast.IProblemBinding;
import org.eclipse.cdt.core.dom.ast.IScope;
import org.eclipse.cdt.core.dom.ast.IScope.ScopeLookupData;
import org.eclipse.cdt.core.dom.ast.ISemanticProblem;
import org.eclipse.cdt.core.dom.ast.IType;
import org.eclipse.cdt.core.dom.ast.ITypedef;
import org.eclipse.cdt.core.dom.ast.IVariable;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTAliasDeclaration;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCapture;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTCatchHandler;
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.ICPPASTEnumerationSpecifier;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTExpression;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTFieldReference;
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.ICPPASTFunctionDefinition;
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.ICPPASTLinkageSpecification;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPASTLiteralExpression;
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.ICPPASTNewExpression;
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.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.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.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.ICPPASTTypeId;
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.ICPPASTWhileStatement;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPAliasTemplate;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPBase;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPBlockScope;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassScope;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassSpecialization;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassTemplate;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassTemplatePartialSpecialization;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPClassType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPConstructor;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPField;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunction;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunctionInstance;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunctionSpecialization;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunctionTemplate;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPFunctionType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMember;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPMethod;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespace;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPNamespaceScope;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPParameter;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPPartialSpecialization;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPPointerToMemberType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPReferenceType;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPScope;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPSpecialization;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateArgument;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateDefinition;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateInstance;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateNonTypeParameter;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateParameter;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateScope;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateTemplateParameter;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPTemplateTypeParameter;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPUsingDeclaration;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPUsingDirective;
import org.eclipse.cdt.core.dom.ast.cpp.ICPPVariable;
import org.eclipse.cdt.core.index.IIndex;
import org.eclipse.cdt.core.index.IIndexBinding;
import org.eclipse.cdt.core.index.IIndexFile;
import org.eclipse.cdt.core.index.IIndexFileSet;
import org.eclipse.cdt.core.index.IIndexName;
import org.eclipse.cdt.core.parser.util.ArrayUtil;
import org.eclipse.cdt.core.parser.util.CharArrayObjectMap;
import org.eclipse.cdt.core.parser.util.CharArrayUtils;
import org.eclipse.cdt.core.parser.util.CollectionUtils;
import org.eclipse.cdt.core.parser.util.DebugUtil;
import org.eclipse.cdt.core.parser.util.IUnaryPredicate;
import org.eclipse.cdt.core.parser.util.ObjectSet;
import org.eclipse.cdt.internal.core.dom.parser.ASTAmbiguousNode;
import org.eclipse.cdt.internal.core.dom.parser.ASTInternal;
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.IASTAmbiguousDeclarator;
import org.eclipse.cdt.internal.core.dom.parser.IASTInternalScope;
import org.eclipse.cdt.internal.core.dom.parser.IRecursionResolvingBinding;
import org.eclipse.cdt.internal.core.dom.parser.ITypeContainer;
import org.eclipse.cdt.internal.core.dom.parser.IntegralValue;
import org.eclipse.cdt.internal.core.dom.parser.ProblemBinding;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTIdExpression;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTLiteralExpression;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTName;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTNameBase;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPASTTranslationUnit;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPBasicType;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPBuiltinParameter;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPCompositeBinding;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPDeferredConstructor;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPDeferredFunction;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPFunctionType;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPImplicitFunction;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPNamespace;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPNamespaceScope;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPPointerType;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPReferenceType;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPScope;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPTemplateNonTypeArgument;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPTemplateParameterMap;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPTemplateTypeArgument;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPUnknownField;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPUnknownMemberClass;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPUnknownMethod;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPUsingDeclaration;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPUsingDirective;
import org.eclipse.cdt.internal.core.dom.parser.cpp.CPPVariable;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPASTInternalScope;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPClassSpecializationScope;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPDeferredClassInstance;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPEvaluation;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalBinding;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPInternalNamespaceScope;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPUnknownBinding;
import org.eclipse.cdt.internal.core.dom.parser.cpp.ICPPUnknownType;
import org.eclipse.cdt.internal.core.dom.parser.cpp.OverloadableOperator;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.CPPTemplates.TypeSelection;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.Conversions.Context;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.Conversions.UDCMode;
import org.eclipse.cdt.internal.core.dom.parser.cpp.semantics.Cost.Rank;
import org.eclipse.cdt.internal.core.pdom.dom.IPDOMAdaptedASTNode;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IStatus;
/**
* Name resolution.
*/
public class CPPSemantics {
/**
* The maximum depth to search ancestors before assuming infinite looping.
*/
public static final int MAX_INHERITANCE_DEPTH = 40;
public static final String EMPTY_NAME = ""; //$NON-NLS-1$
public static final char[] OPERATOR_ = new char[] { 'o', 'p', 'e', 'r', 'a', 't', 'o', 'r', ' ' };
public static final IType VOID_TYPE = new CPPBasicType(Kind.eVoid, 0);
public static final IType INT_TYPE = new CPPBasicType(Kind.eInt, 0);
private static final char[] CALL_FUNCTION = "call-function".toCharArray(); //$NON-NLS-1$
private static final ICPPEvaluation[] NO_INITCLAUSE_EVALUATION = {};
// Set to true for debugging.
public static boolean traceBindingResolution = false;
public static int traceIndent = 0;
// special return value for costForFunctionCall
private static final FunctionCost CONTAINS_DEPENDENT_TYPES = new FunctionCost(null, 0);
// A regular expression for matching qualified names. This allows for optional global qualification
// (leading ::) and then separates the first part of the name from the rest (if present). There are
// three capture groups:
// (1) If the input name specifies the global namespace (leading ::) then capture group 1 will
// be ::. Group 1 will be null otherwise.
// (2) The text of the first component of the qualified name, including leading :: if present in
// the input string. Leading and trailing whitespace is trimmed. There is no effort to check
// that the name contains valid C++ identifier characters.
// (3) The text of everything after the first component of the qualified name.
//
// E.g., -- Input Name -- ---- Capture Groups ----
// "::nsA::nsB::b" => { "::", "nsA", "nsB::b" }
// "a" => { null, "a", null }
// ":: i" => { "::", "i", null }
private static final Pattern QUALNAME_REGEX = Pattern.compile("^\\s*(::)?\\s*([^\\s:]+)\\s*(?:::(.*))?$"); //$NON-NLS-1$
// This flag controls whether name lookup is allowed to find bindings in headers
// that are not reachable via includes from the file containing the name.
// Generally this is not allowed, but certain consumers, such as IncludeOrganizer,
// need it (since the whole point of IncludeOrganizer is to find missing headers).
private static final ThreadLocal<Boolean> fAllowPromiscuousBindingResolution = new ThreadLocal<>() {
@Override
protected Boolean initialValue() {
return false;
}
};
private static final ThreadLocal<Deque<IASTNode>> fLookupPoints = new ThreadLocal<>() {
@Override
protected Deque<IASTNode> initialValue() {
return new ArrayDeque<>();
}
};
public static void pushLookupPoint(IASTNode point) {
fLookupPoints.get().push(point);
}
public static void popLookupPoint() {
fLookupPoints.get().pop();
}
/**
* Get the current point of instantiation / point of lookup for name lookups.
*
* NOTE: This is meant to be used primarily for "declaredBefore" purposes, that is,
* for determining whether something was declared before or after the point
* of lookup. It is NOT meant to be used as a general mechanism for accessing
* information about a call site without having to pass that information along
* the usual way (via function arguments).
*/
public static IASTNode getCurrentLookupPoint() {
Deque<IASTNode> lookupPoints = fLookupPoints.get();
return lookupPoints.isEmpty() ? null : lookupPoints.peek();
}
static protected IBinding resolveBinding(IASTName name) {
if (traceBindingResolution) {
for (int i = 0; i < traceIndent; i++) {
System.out.print(" "); //$NON-NLS-1$
}
System.out.println("Resolving " + name + ':' + ((ASTNode) name).getOffset()); //$NON-NLS-1$
traceIndent++;
}
if (name instanceof CPPASTNameBase) {
((CPPASTNameBase) name).incResolutionDepth();
}
// 1: Get some context info off of the name to figure out what kind of lookup we want.
LookupData data = createLookupData(name);
IBinding binding;
pushLookupPoint(name);
try {
try {
// 2: Lookup
lookup(data, null);
// Perform argument dependent lookup
if (data.checkAssociatedScopes() && !data.hasTypeOrMemberFunctionOrVariableResult()) {
doArgumentDependentLookup(data);
}
} catch (DOMException e) {
data.problem = (ProblemBinding) e.getProblem();
}
if (data.problem != null)
return data.problem;
// 3: Resolve ambiguities
try {
binding = resolveAmbiguities(data);
} catch (DOMException e) {
binding = e.getProblem();
}
// 4: Post processing
binding = postResolution(binding, data);
} finally {
popLookupPoint();
}
if (traceBindingResolution) {
traceIndent--;
for (int i = 0; i < traceIndent; i++) {
System.out.print(" "); //$NON-NLS-1$
}
System.out.println("Resolved " + name + ':' + ((ASTNode) name).getOffset() + //$NON-NLS-1$
" to " + DebugUtil.toStringWithClass(binding) + ':' + System.identityHashCode(binding)); //$NON-NLS-1$
}
return binding;
}
protected static IBinding postResolution(IBinding binding, IASTName name) {
LookupData data = createLookupData(name);
return postResolution(binding, data);
}
private static IBinding postResolution(IBinding binding, LookupData data) {
final IASTName lookupName = data.getLookupName();
if (lookupName == null)
return binding;
// If this is the unqualified name of a function in a function call in a template and some
// of the function arguments are dependent, a matching function could be found via
// argument-dependent lookup at the point of instantiation.
if (binding == null || binding instanceof IProblemBinding) {
if (!data.qualified && data.isFunctionCall()
&& CPPTemplates.containsDependentType(data.getFunctionArgumentTypes())) {
binding = CPPDeferredFunction.createForName(lookupName.getSimpleID());
}
}
// If any name in the initializer refers to introduced names then declaration is ill-formed.
if (binding instanceof ICPPInternalBinding internalBinding && internalBinding.getDefinition() != null
&& internalBinding.getDefinition()
.getParent() instanceof ICPPASTStructuredBindingDeclaration declaration) {
IASTNode parent = lookupName.getParent();
while (parent != null) {
if (parent == declaration) {
binding = new ProblemBinding(lookupName, data.getLookupPoint(),
IProblemBinding.SEMANTIC_INVALID_STRUCTURED_BINDING_INITIALIZER, data.getFoundBindings());
break;
}
parent = parent.getParent();
}
}
if (binding instanceof IProblemBinding)
return binding;
IASTNode lookupPoint = data.getLookupPoint();
if (binding == null && data.checkClassContainingFriend()) {
// 3.4.1-10 If we don't find a name used in a friend declaration in the member
// declaration's class, we should look in the class granting friendship.
IASTNode parent = lookupName.getParent();
while (parent != null && !(parent instanceof ICPPASTCompositeTypeSpecifier)) {
parent = parent.getParent();
}
if (parent instanceof ICPPASTCompositeTypeSpecifier) {
IScope scope = ((ICPPASTCompositeTypeSpecifier) parent).getScope();
try {
lookup(data, scope);
binding = resolveAmbiguities(data);
} catch (DOMException e) {
binding = e.getProblem();
}
}
}
// Explicit type conversion in functional notation.
if (binding instanceof ICPPClassTemplate && lookupName instanceof ICPPASTTemplateId) {
final IASTNode parent = lookupName.getParent();
if (parent instanceof IASTIdExpression
&& parent.getPropertyInParent() == IASTFunctionCallExpression.FUNCTION_NAME) {
return binding;
}
}
/* 14.6.1-1:
* Within the scope of a class template, when the name of the template is neither qualified
* nor followed by <, it is equivalent to the name followed by the template arguments
* enclosed in <>.
*/
if (binding instanceof ICPPClassTemplate && !(binding instanceof ICPPClassSpecialization)
&& !(binding instanceof ICPPTemplateParameter) && !(lookupName instanceof ICPPASTTemplateId)) {
ASTNodeProperty prop = lookupName.getPropertyInParent();
if (prop != ICPPASTTemplateId.TEMPLATE_NAME && !lookupName.isQualified()) {
// You cannot use a class template name outside of the class template scope,
// mark it as a problem.
IBinding user = CPPTemplates.isUsedInClassTemplateScope((ICPPClassTemplate) binding, lookupName);
if (user instanceof ICPPClassTemplate) {
binding = ((ICPPClassTemplate) user).asDeferredInstance();
} else if (user != null) {
binding = user;
} else {
boolean ok = false;
IASTNode node = lookupName.getParent();
while (node != null && !ok) {
if (node instanceof ICPPASTTemplateId
|| node instanceof ICPPASTTemplatedTypeTemplateParameter) {
ok = true; // Can be argument or default-value for template template parameter
break;
} else if (node instanceof IASTElaboratedTypeSpecifier) {
IASTNode parent = node.getParent();
if (parent instanceof IASTSimpleDeclaration) {
IASTDeclSpecifier declspec = ((IASTSimpleDeclaration) parent).getDeclSpecifier();
if (declspec instanceof ICPPASTDeclSpecifier) {
if (((ICPPASTDeclSpecifier) declspec).isFriend()) {
ok = true; // A friend class template declarations uses resolution.
break;
}
}
}
}
node = node.getParent();
}
if (!ok) {
binding = new ProblemBinding(lookupName, lookupPoint, IProblemBinding.SEMANTIC_INVALID_TYPE,
data.getFoundBindings());
}
}
}
} else if (binding instanceof ICPPDeferredClassInstance) {
// Try to replace binding by the one pointing to the enclosing template declaration.
ICPPDeferredClassInstance dcl = (ICPPDeferredClassInstance) binding;
IBinding usedHere = CPPTemplates.isUsedInClassTemplateScope(dcl.getClassTemplate(), lookupName);
if (usedHere instanceof ICPPClassTemplatePartialSpecialization) {
if (CPPTemplates.areSameArguments(
((ICPPClassTemplatePartialSpecialization) usedHere).getTemplateArguments(),
dcl.getTemplateArguments()))
binding = ((ICPPClassTemplatePartialSpecialization) usedHere).asDeferredInstance();
} else if (usedHere instanceof ICPPClassTemplate) {
if (CPPTemplates.areSameArguments(
CPPTemplates.templateParametersAsArguments((ICPPClassTemplate) usedHere),
dcl.getTemplateArguments())) {
binding = ((ICPPClassTemplate) usedHere).asDeferredInstance();
}
}
}
if (binding instanceof IType) {
IType t = getNestedType((IType) binding, TDEF);
if (t instanceof ICPPClassType && convertClassToConstructor(lookupName)) {
ICPPClassType cls = (ICPPClassType) t;
if (cls instanceof IIndexBinding) {
cls = data.getTranslationUnit().mapToAST(cls);
}
try {
if (lookupName instanceof ICPPASTTemplateId && cls instanceof ICPPClassTemplate) {
if (data.getTranslationUnit() != null) {
ICPPASTTemplateId id = (ICPPASTTemplateId) lookupName;
ICPPTemplateArgument[] args = CPPTemplates.createTemplateArgumentArray(id);
IBinding inst = CPPTemplates.instantiate((ICPPClassTemplate) cls, args);
if (inst instanceof ICPPClassType) {
cls = (ICPPClassType) inst;
}
}
}
if (cls instanceof ICPPUnknownBinding) {
binding = new CPPDeferredConstructor(cls);
} else {
// Do not interpret template arguments to a template class as being
// explicit template arguments to its templated constructor.
data.setTemplateArguments(null);
binding = resolveFunction(data, cls.getConstructors(), true, false);
}
} catch (DOMException e) {
return e.getProblem();
}
}
}
IASTName name = lookupName;
IASTNode nameParent = name.getParent();
if (nameParent instanceof ICPPASTTemplateId) {
if (binding instanceof ICPPTemplateInstance) {
final ICPPTemplateInstance instance = (ICPPTemplateInstance) binding;
binding = instance.getSpecializedBinding();
name.setBinding(binding);
((ICPPASTTemplateId) nameParent).setBinding(instance);
}
name = (ICPPASTTemplateId) nameParent;
nameParent = name.getParent();
}
boolean isNestedNameSpecifier = false;
if (nameParent instanceof ICPPASTQualifiedName) {
if (name == ((ICPPASTQualifiedName) nameParent).getLastName()) {
name = (IASTName) nameParent;
nameParent = name.getParent();
} else {
isNestedNameSpecifier = true;
}
}
// If the lookup in base-classes ran into a deferred instance, use the computed unknown
// binding.
final ASTNodeProperty namePropertyInParent = name.getPropertyInParent();
if (binding == null && data.skippedScope != null) {
if (isNestedNameSpecifier || namePropertyInParent == IASTNamedTypeSpecifier.NAME) {
binding = new CPPUnknownMemberClass(data.skippedScope, name.getSimpleID());
} else if (data.isFunctionCall()) {
binding = new CPPUnknownMethod(data.skippedScope, name.getSimpleID());
} else {
binding = new CPPUnknownField(data.skippedScope, name.getSimpleID());
}
}
if (binding != null) {
if (namePropertyInParent == IASTNamedTypeSpecifier.NAME) {
if (!(binding instanceof IType || binding instanceof ICPPConstructor)) {
IASTNode parent = name.getParent().getParent();
if (parent instanceof IASTTypeId
&& parent.getPropertyInParent() == ICPPASTTemplateId.TEMPLATE_ID_ARGUMENT) {
if (!(binding instanceof IType)) {
// A type id needs to hold a type.
binding = new ProblemBinding(lookupName, lookupPoint, IProblemBinding.SEMANTIC_INVALID_TYPE,
data.getFoundBindings());
}
// Don't create a problem here.
} else {
binding = new ProblemBinding(lookupName, lookupPoint, IProblemBinding.SEMANTIC_INVALID_TYPE,
data.getFoundBindings());
}
}
} else if (namePropertyInParent == IASTIdExpression.ID_NAME) {
if (binding instanceof IType) {
final IASTNode idExpr = name.getParent();
ASTNodeProperty pip = idExpr.getPropertyInParent();
if (pip == ICPPASTTemplatedTypeTemplateParameter.DEFAULT_VALUE) {
// Default for template template parameter is a type.
} else if (pip == IASTFunctionCallExpression.FUNCTION_NAME) {
// Explicit type conversion in functional notation.
} else if (pip == IASTUnaryExpression.OPERAND && ((ICPPASTUnaryExpression) idExpr.getParent())
.getOperator() == IASTUnaryExpression.op_sizeofParameterPack) {
// Argument of sizeof... can be a type
} else {
binding = new ProblemBinding(lookupName, lookupPoint, IProblemBinding.SEMANTIC_INVALID_TYPE,
data.getFoundBindings());
}
}
}
}
// Some declarations are found via name resolution (e.g. when using a qualified name),
// add name as definition and check the declaration specifier.
final IASTDeclaration declaration = data.forDeclaration();
if (declaration != null) {
// Functions
if (binding instanceof IFunction) {
binding = checkDeclSpecifier(binding, lookupName, declaration);
if (!(binding instanceof IProblemBinding)) {
if (declaration instanceof ICPPASTFunctionDefinition) {
ASTInternal.addDefinition(binding, lookupName);
}
}
}
// Definitions of static fields.
if (binding instanceof ICPPField && lookupName.isDefinition()) {
if (declaration.getPropertyInParent() != IASTCompositeTypeSpecifier.MEMBER_DECLARATION) {
ASTInternal.addDefinition(binding, lookupName);
}
}
}
// If the result is a virtual method called without explicit qualification, and we can determine a
// unique final overrider for it in the hierarchy of the method call's implied object type, replace
// the method with its final overrider.
if (!(lookupName.getParent() instanceof ICPPASTQualifiedName) && binding instanceof ICPPMethod
&& ((ICPPMethod) binding).isVirtual()) {
IType impliedObjectType = data.getImpliedObjectType();
if (impliedObjectType instanceof ICPPClassType) {
ICPPMethod finalOverrider = CPPInheritance.getFinalOverrider((ICPPMethod) binding,
(ICPPClassType) impliedObjectType);
if (finalOverrider != null) {
binding = finalOverrider;
}
}
}
// If we're still null...
if (binding == null) {
if (name instanceof ICPPASTQualifiedName && declaration != null) {
binding = new ProblemBinding(lookupName, lookupPoint,
IProblemBinding.SEMANTIC_MEMBER_DECLARATION_NOT_FOUND, data.getFoundBindings());
} else {
binding = new ProblemBinding(lookupName, lookupPoint, IProblemBinding.SEMANTIC_NAME_NOT_FOUND,
data.getFoundBindings());
}
}
return binding;
}
private static boolean convertClassToConstructor(IASTName name) {
if (name == null)
return false;
final ASTNodeProperty propertyInParent = name.getPropertyInParent();
if (propertyInParent == null)
return false;
if (propertyInParent == ICPPASTTemplateId.TEMPLATE_NAME)
return false;
IASTNode parent = name.getParent();
if (parent instanceof ICPPASTQualifiedName) {
if (((ICPPASTQualifiedName) parent).getLastName() != name)
return false;
parent = parent.getParent();
}
if (parent instanceof ICPPASTConstructorChainInitializer) {
return true;
}
return false;
}
public static void doArgumentDependentLookup(LookupData data) throws DOMException {
data.ignoreUsingDirectives = true;
// Set 'qualified' to true for the duration of this function call so the calls to lookup()
// don't ascend into enclosing scopes.
boolean originalQualified = data.qualified;
data.qualified = true;
data.setArgumentDependent(true);
Set<ICPPFunction> friendFns = new HashSet<>(2);
Set<ICPPNamespaceScope> associated = getAssociatedScopes(data, friendFns);
for (ICPPNamespaceScope scope : associated) {
if (!data.visited.containsKey(scope)) {
lookup(data, scope);
}
}
Object[] matchingFriendFns = CollectionUtils.filter(friendFns, new NameMatcherPredicate(data.getLookupKey()))
.toArray();
mergeResults(data, matchingFriendFns, false);
data.qualified = originalQualified;
data.setArgumentDependent(false);
}
private static class NameMatcherPredicate implements IUnaryPredicate<ICPPFunction> {
private char[] fKey;
public NameMatcherPredicate(char[] key) {
fKey = key;
}
@Override
public boolean apply(ICPPFunction argument) {
return Arrays.equals(argument.getNameCharArray(), fKey);
}
}
static IBinding checkDeclSpecifier(IBinding binding, IASTName name, IASTNode decl) {
// Check for empty declaration specifiers.
if (!isCtorOrConversionOperator(binding)) {
IASTDeclSpecifier declspec = null;
if (decl instanceof IASTSimpleDeclaration) {
declspec = ((IASTSimpleDeclaration) decl).getDeclSpecifier();
} else if (decl instanceof IASTFunctionDefinition) {
declspec = ((IASTFunctionDefinition) decl).getDeclSpecifier();
}
if (declspec != null && CPPVisitor.doesNotSpecifyType(declspec)) {
binding = new ProblemBinding(name, IProblemBinding.SEMANTIC_INVALID_TYPE);
}
}
return binding;
}
private static boolean isCtorOrConversionOperator(IBinding binding) {
if (binding instanceof ICPPConstructor)
return true;
if (binding instanceof ICPPMethod) {
ICPPMethod m = (ICPPMethod) binding;
if (m.isDestructor())
return true;
return isConversionOperator(m);
}
return false;
}
public static LookupData createLookupData(IASTName name) {
LookupData data = new LookupData(name);
IASTNode parent = name.getParent();
if (parent instanceof ICPPASTTemplateId)
parent = parent.getParent();
if (parent instanceof ICPPASTQualifiedName)
parent = parent.getParent();
if (parent instanceof IASTDeclarator && parent.getPropertyInParent() == IASTSimpleDeclaration.DECLARATOR) {
IASTSimpleDeclaration simple = (IASTSimpleDeclaration) parent.getParent();
if (simple.getDeclSpecifier().getStorageClass() == IASTDeclSpecifier.sc_typedef)
data.qualified = true;
}
if (parent instanceof IASTIdExpression) {
IASTNode grand = parent.getParent();
while (grand instanceof IASTUnaryExpression
&& ((IASTUnaryExpression) grand).getOperator() == IASTUnaryExpression.op_bracketedPrimary) {
parent = grand;
grand = grand.getParent();
}
if (parent.getPropertyInParent() == IASTFunctionCallExpression.FUNCTION_NAME) {
parent = parent.getParent();
IASTInitializerClause[] args = ((IASTFunctionCallExpression) parent).getArguments();
data.setFunctionArguments(false, args);
}
} else if (parent instanceof ICPPASTFieldReference) {
IASTNode grand = parent.getParent();
while (grand instanceof IASTUnaryExpression
&& ((IASTUnaryExpression) grand).getOperator() == IASTUnaryExpression.op_bracketedPrimary) {
parent = grand;
grand = grand.getParent();
}
if (parent.getPropertyInParent() == IASTFunctionCallExpression.FUNCTION_NAME) {
IASTInitializerClause[] exp = ((IASTFunctionCallExpression) parent.getParent()).getArguments();
data.setFunctionArguments(false, exp);
}
} else if (parent instanceof ICPPASTNamedTypeSpecifier && parent.getParent() instanceof IASTTypeId) {
IASTTypeId typeId = (IASTTypeId) parent.getParent();
if (typeId.getParent() instanceof ICPPASTNewExpression) {
ICPPASTNewExpression newExp = (ICPPASTNewExpression) typeId.getParent();
IASTInitializer init = newExp.getInitializer();
if (init == null) {
data.setFunctionArguments(false, NO_INITCLAUSE_EVALUATION);
} else if (init instanceof ICPPASTConstructorInitializer) {
data.setFunctionArguments(false, ((ICPPASTConstructorInitializer) init).getArguments());
} else if (init instanceof ICPPASTInitializerList) {
data.setFunctionArguments(false, (ICPPASTInitializerList) init);
}
}
} else if (parent instanceof ICPPASTConstructorChainInitializer) {
ICPPASTConstructorChainInitializer ctorinit = (ICPPASTConstructorChainInitializer) parent;
IASTInitializer init = ctorinit.getInitializer();
if (init instanceof ICPPASTConstructorInitializer) {
data.setFunctionArguments(false, ((ICPPASTConstructorInitializer) init).getArguments());
} else if (init instanceof ICPPASTInitializerList) {
data.setFunctionArguments(false, (ICPPASTInitializerList) init);
}
}
return data;
}
private static Set<ICPPNamespaceScope> getAssociatedScopes(LookupData data, Set<ICPPFunction> friendFns) {
if (!data.hasFunctionArguments())
return Collections.emptySet();
IType[] ps = data.getFunctionArgumentTypes();
Set<ICPPNamespaceScope> namespaces = new HashSet<>(2);
ObjectSet<IType> handled = new ObjectSet<>(2);
for (IType p : ps) {
try {
getAssociatedScopes(p, namespaces, friendFns, handled, data.getTranslationUnit(), true);
} catch (DOMException e) {
}
}
IASTName lookupName = data.getLookupName();
if (lookupName != null) {
final char[] simpleID = lookupName.getSimpleID();
if (CharArrayUtils.equals(CPPVisitor.BEGIN, simpleID) || CharArrayUtils.equals(CPPVisitor.END, simpleID)) {
IASTNode parent = lookupName.getParent(); // id-expression
if (parent != null)
parent = parent.getParent(); // function call
if (parent != null)
parent = parent.getParent(); // the loop
if (parent instanceof ICPPASTRangeBasedForStatement) {
IASTTranslationUnit tu = parent.getTranslationUnit();
IBinding[] std = tu.getScope().find(CPPVisitor.STD, tu);
for (IBinding binding : std) {
if (binding instanceof ICPPNamespace) {
namespaces.add(((ICPPNamespace) binding).getNamespaceScope());
}
}
}
}
}
return namespaces;
}
// 3.4.2-2
private static void getAssociatedScopes(IType t, Set<ICPPNamespaceScope> namespaces, Set<ICPPFunction> friendFns,
ObjectSet<IType> handled, CPPASTTranslationUnit tu, boolean lookInBaseClasses) throws DOMException {
t = getNestedType(t, TDEF | CVTYPE | PTR | ARRAY | REF);
// No point getting namespaces associated with a dependent type - we don't know what they
// are yet.
if (CPPTemplates.isDependentType(t))
return;
if (t instanceof IBinding) {
if (handled.containsKey(t))
return;
handled.put(t);
if (t instanceof IEnumeration) {
// [basic.lookup.argdep] p2.3: an enumeration's only associated namespace
// is the innermost enclosing namespace of its declaration.
getAssociatedNamespaceScopes(getContainingNamespaceScope((IBinding) t, tu), namespaces);
} else {
IBinding owner = ((IBinding) t).getOwner();
if (owner instanceof ICPPClassType) {
getAssociatedScopes((IType) owner, namespaces, friendFns, handled, tu,
false /* do not look at base classes of the enclosing class */);
} else {
getAssociatedNamespaceScopes(getContainingNamespaceScope((IBinding) t, tu), namespaces);
}
}
}
if (t instanceof ICPPClassType && !(t instanceof ICPPClassTemplate)) {
ICPPClassType ct = (ICPPClassType) t;
if (lookInBaseClasses) {
ICPPBase[] bases = ct.getBases();
for (ICPPBase base : bases) {
IBinding b = base.getBaseClass();
if (b instanceof IType)
getAssociatedScopes((IType) b, namespaces, friendFns, handled, tu, true);
}
}
// Furthermore, if T is a class template ...
// * ... types of the template arguments for template type parameters
// (excluding template template parameters);
// * ... owners of which any template template arguments are members;
if (ct instanceof ICPPSpecialization) {
for (IBinding friend : ct.getFriends()) {
if (friend instanceof ICPPFunction) {
friendFns.add((ICPPFunction) friend);
}
}
if (ct instanceof ICPPTemplateInstance) {
ICPPTemplateArgument[] args = ((ICPPTemplateInstance) ct).getTemplateArguments();
for (ICPPTemplateArgument arg : args) {
if (arg.isTypeValue()) {
getAssociatedScopes(arg.getTypeValue(), namespaces, friendFns, handled, tu, true);
}
}
}
}
} else if (t instanceof IFunctionType) {
IFunctionType ft = (IFunctionType) t;
getAssociatedScopes(ft.getReturnType(), namespaces, friendFns, handled, tu, true);
IType[] ps = ft.getParameterTypes();
for (IType pt : ps) {
getAssociatedScopes(pt, namespaces, friendFns, handled, tu, true);
}
} else if (t instanceof ICPPPointerToMemberType) {
final ICPPPointerToMemberType pmt = (ICPPPointerToMemberType) t;
getAssociatedScopes(pmt.getMemberOfClass(), namespaces, friendFns, handled, tu, true);
getAssociatedScopes(pmt.getType(), namespaces, friendFns, handled, tu, true);
} else if (t instanceof FunctionSetType) {
FunctionSetType fst = (FunctionSetType) t;
for (ICPPFunction fn : fst.getFunctionSet().getBindings()) {
getAssociatedScopes(fn.getType(), namespaces, friendFns, handled, tu, true);
}
}
}
private static ICPPNamespaceScope getContainingNamespaceScope(IBinding binding, CPPASTTranslationUnit tu)
throws DOMException {
if (binding == null)
return null;
IScope scope = binding.getScope();
scope = SemanticUtil.mapToAST(scope, tu);
while (scope != null && !(scope instanceof ICPPNamespaceScope)) {
scope = getParentScope(scope, tu);
}
return (ICPPNamespaceScope) scope;
}
public static void getAssociatedNamespaceScopes(ICPPNamespaceScope scope, Set<ICPPNamespaceScope> namespaces) {
if (scope == null || !namespaces.add(scope))
return;
if (scope instanceof ICPPInternalNamespaceScope) {
final ICPPInternalNamespaceScope internalScope = (ICPPInternalNamespaceScope) scope;
for (ICPPNamespaceScope mem : internalScope.getEnclosingNamespaceSet()) {
namespaces.add(mem);
}
}
}
public static ICPPScope getLookupScope(IASTName name) {
IASTNode parent = name.getParent();
IScope scope = null;
if (parent instanceof ICPPASTBaseSpecifier) {
ICPPASTCompositeTypeSpecifier compSpec = (ICPPASTCompositeTypeSpecifier) parent.getParent();
IASTName n = compSpec.getName();
if (n instanceof ICPPASTQualifiedName) {
n = n.getLastName();
}
scope = CPPVisitor.getContainingScope(n);
} else {
scope = CPPVisitor.getContainingScope(name);
}
if (scope instanceof ICPPScope) {
return (ICPPScope) scope;
} else if (scope instanceof IProblemBinding) {
return new CPPScope.CPPScopeProblem(((IProblemBinding) scope).getASTNode(),
IProblemBinding.SEMANTIC_BAD_SCOPE, ((IProblemBinding) scope).getNameCharArray());
}
return new CPPScope.CPPScopeProblem(name, IProblemBinding.SEMANTIC_BAD_SCOPE);
}
private static void mergeResults(LookupData data, Object results, boolean scoped) {
if (!data.contentAssist) {
if (results instanceof IBinding) {
data.foundItems = ArrayUtil.append(Object.class, (Object[]) data.foundItems, results);
} else if (results instanceof Object[]) {
data.foundItems = ArrayUtil.addAll(Object.class, (Object[]) data.foundItems, (Object[]) results);
}
} else {
@SuppressWarnings("unchecked")
final CharArrayObjectMap<Object> oldItems = (CharArrayObjectMap<Object>) data.foundItems;
data.foundItems = mergePrefixResults(oldItems, results, scoped);
}
}
/**
* @param dest
* @param source : either Object[] or CharArrayObjectMap
* @param scoped
* @return
*/
static CharArrayObjectMap<Object> mergePrefixResults(CharArrayObjectMap<Object> dest, Object source,