-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathTClingClassInfo.cxx
More file actions
1498 lines (1355 loc) · 47.4 KB
/
Copy pathTClingClassInfo.cxx
File metadata and controls
1498 lines (1355 loc) · 47.4 KB
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
// @(#)root/core/meta:$Id$
// Author: Paul Russo 30/07/2012
/*************************************************************************
* Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
* All rights reserved. *
* *
* For the licensing terms see $ROOTSYS/LICENSE. *
* For the list of contributors see $ROOTSYS/README/CREDITS. *
*************************************************************************/
/** \class TClingClassInfo
Emulation of the CINT ClassInfo class.
The CINT C++ interpreter provides an interface to metadata about
a class through the ClassInfo class. This class provides the same
functionality, using an interface as close as possible to ClassInfo
but the class metadata comes from the Clang C++ compiler, not CINT.
*/
#include "TClingClassInfo.h"
#include "TClassEdit.h"
#include "TClingBaseClassInfo.h"
#include "TClingCallFunc.h"
#include "TClingMethodInfo.h"
#include "TDictionary.h"
#include "TClingTypeInfo.h"
#include "TError.h"
#include "TClingUtils.h"
#include "ThreadLocalStorage.h"
#include "cling/Interpreter/Interpreter.h"
#include "cling/Interpreter/LookupHelper.h"
#include "cling/Utils/AST.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Decl.h"
#include "clang/AST/DeclCXX.h"
#include "clang/AST/DeclTemplate.h"
#include "clang/AST/GlobalDecl.h"
#include "clang/AST/PrettyPrinter.h"
#include "clang/AST/RecordLayout.h"
#include "clang/AST/Type.h"
#include "clang/Basic/Specifiers.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Sema/Sema.h"
#include "llvm/ExecutionEngine/GenericValue.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/raw_ostream.h"
#include "ROOT/BitUtils.hxx"
#include <sstream>
#include <string>
using namespace clang;
using namespace ROOT;
static std::string FullyQualifiedName(const Decl *decl) {
// Return the fully qualified name without worrying about normalizing it.
std::string buf;
if (const NamedDecl* ND = llvm::dyn_cast<NamedDecl>(decl)) {
PrintingPolicy Policy(decl->getASTContext().getPrintingPolicy());
llvm::raw_string_ostream stream(buf);
ND->getNameForDiagnostic(stream, Policy, /*Qualified=*/true);
}
return buf;
}
TClingClassInfo::TClingClassInfo(cling::Interpreter *interp, Bool_t all)
: TClingDeclInfo(nullptr), fInterp(interp), fFirstTime(true), fDescend(false), fIterAll(all),
fIsIter(true), fOffsetCache(0)
{
TranslationUnitDecl *TU =
interp->getCI()->getASTContext().getTranslationUnitDecl();
fFirstTime = true;
SetDecl(TU);
}
TClingClassInfo::TClingClassInfo(cling::Interpreter *interp, const char *name, bool intantiateTemplate /* = true */)
: TClingDeclInfo(nullptr), fInterp(interp), fFirstTime(true), fDescend(false), fIterAll(kTRUE), fIsIter(false),
fOffsetCache(0)
{
const cling::LookupHelper& lh = fInterp->getLookupHelper();
const Type *type = nullptr;
const Decl *decl = lh.findScope(name,
gDebug > 5 ? cling::LookupHelper::WithDiagnostics
: cling::LookupHelper::NoDiagnostics,
&type, intantiateTemplate);
if (!decl) {
std::string buf = TClassEdit::InsertStd(name);
if (buf != name) {
decl = lh.findScope(buf,
gDebug > 5 ? cling::LookupHelper::WithDiagnostics
: cling::LookupHelper::NoDiagnostics,
&type, intantiateTemplate);
}
}
if (!decl && type) {
if (const auto *TD = type->getAsTagDecl()) {
decl = TD;
}
}
SetDecl(decl);
fType = type;
if (decl && decl->isInvalidDecl()) {
Error("TClingClassInfo", "Found an invalid decl for %s.",name);
SetDecl(nullptr);
fType = nullptr;
}
}
TClingClassInfo::TClingClassInfo(cling::Interpreter *interp,
const Type &tag)
: TClingDeclInfo(nullptr), fInterp(interp), fFirstTime(true), fDescend(false), fIterAll(kTRUE),
fIsIter(false), fOffsetCache(0)
{
Init(tag);
}
TClingClassInfo::TClingClassInfo(cling::Interpreter *interp,
const Decl *D)
: TClingDeclInfo(nullptr), fInterp(interp), fFirstTime(true), fDescend(false), fIterAll(kTRUE),
fIsIter(false), fOffsetCache(0)
{
Init(D);
}
void TClingClassInfo::AddBaseOffsetValue(const clang::Decl* decl, ptrdiff_t offset)
{
// Add the offset value from this class to the non-virtual base class
// determined by the parameter decl.
OffsetPtrFunc_t executableFunc = nullptr;
std::unique_lock<std::mutex> lock(fOffsetCacheMutex);
fOffsetCache[decl] = std::make_pair(offset, executableFunc);
}
long TClingClassInfo::ClassProperty() const
{
if (!IsValid()) {
return 0L;
}
long property = 0L;
const RecordDecl *RD = llvm::dyn_cast<RecordDecl>(GetDecl());
// isAbstract and other calls can trigger deserialization
cling::Interpreter::PushTransactionRAII RAII(fInterp);
if (!RD) {
// We are an enum or namespace.
// The cint interface always returns 0L for these guys.
return property;
}
if (RD->isUnion()) {
// The cint interface always returns 0L for these guys.
return property;
}
// We now have a class or a struct.
const CXXRecordDecl *CRD =
llvm::dyn_cast<CXXRecordDecl>(GetDecl());
if (!CRD)
return property;
property |= kClassIsValid;
if (CRD->isAbstract()) {
property |= kClassIsAbstract;
}
if (CRD->hasUserDeclaredConstructor()) {
property |= kClassHasExplicitCtor;
}
if (
!CRD->hasUserDeclaredConstructor() &&
!CRD->hasTrivialDefaultConstructor()
) {
property |= kClassHasImplicitCtor;
}
if (
CRD->hasUserProvidedDefaultConstructor() ||
!CRD->hasTrivialDefaultConstructor()
) {
property |= kClassHasDefaultCtor;
}
if (CRD->hasUserDeclaredDestructor()) {
property |= kClassHasExplicitDtor;
}
else if (!CRD->hasTrivialDestructor()) {
property |= kClassHasImplicitDtor;
}
if (CRD->hasUserDeclaredCopyAssignment()) {
property |= kClassHasAssignOpr;
}
if (CRD->isPolymorphic()) {
property |= kClassHasVirtual;
}
if (CRD->isAggregate() || CRD->isPOD()) {
// according to the C++ standard, being a POD implies being an aggregate
property |= kClassIsAggregate;
}
return property;
}
void TClingClassInfo::Delete(void *arena, const ROOT::TMetaUtils::TNormalizedCtxt &normCtxt) const
{
// Invoke operator delete on a pointer to an object
// of this class type.
if (!IsValid()) {
Error("TClingClassInfo::Delete()", "Called while invalid!");
return;
}
if (!IsLoaded()) {
Error("TClingClassInfo::Delete()", "Class is not loaded: %s",
FullyQualifiedName(GetDecl()).c_str());
return;
}
TClingCallFunc cf(fInterp);
cf.ExecDestructor(this, arena, /*nary=*/0, /*withFree=*/true);
}
void TClingClassInfo::DeleteArray(void *arena, bool dtorOnly, const ROOT::TMetaUtils::TNormalizedCtxt &normCtxt) const
{
// Invoke operator delete[] on a pointer to an array object
// of this class type.
if (!IsLoaded()) {
return;
}
if (dtorOnly) {
// There is no syntax in C++ for invoking the placement delete array
// operator, so we have to placement destroy each element by hand.
// Unfortunately we do not know how many elements to delete.
//TClingCallFunc cf(fInterp);
//cf.ExecDestructor(this, arena, nary, /*withFree=*/false);
Error("DeleteArray", "Placement delete of an array is unsupported!\n");
return;
}
TClingCallFunc cf(fInterp);
cf.ExecDestructor(this, arena, /*nary=*/1, /*withFree=*/true);
}
void TClingClassInfo::Destruct(void *arena, const ROOT::TMetaUtils::TNormalizedCtxt &normCtxt) const
{
// Invoke placement operator delete on a pointer to an array object
// of this class type.
if (!IsLoaded()) {
return;
}
TClingCallFunc cf(fInterp);
cf.ExecDestructor(this, arena, /*nary=*/0, /*withFree=*/false);
}
const FunctionTemplateDecl *TClingClassInfo::GetFunctionTemplate(const char *fname) const
{
// Return any method or function in this scope with the name 'fname'.
if (!IsLoaded()) {
return nullptr;
}
if (fType) {
const TypedefType *TT = llvm::dyn_cast<TypedefType>(fType);
if (TT) {
llvm::StringRef tname(TT->getDecl()->getName());
if (tname == fname) {
const NamedDecl *ndecl = llvm::dyn_cast<NamedDecl>(GetDecl());
if (ndecl && ndecl->getName() != fname) {
// Constructor name matching the typedef type, use the decl name instead.
return GetFunctionTemplate(ndecl->getName().str().c_str());
}
}
}
}
const cling::LookupHelper &lh = fInterp->getLookupHelper();
const FunctionTemplateDecl *fd
= lh.findFunctionTemplate(GetDecl(), fname,
gDebug > 5 ? cling::LookupHelper::WithDiagnostics
: cling::LookupHelper::NoDiagnostics, false);
if (fd) return fd->getCanonicalDecl();
return nullptr;
}
const clang::ValueDecl *TClingClassInfo::GetDataMember(const char *name) const
{
// Return the value decl (if any) corresponding to a data member which
// the given name declared in this scope.
const cling::LookupHelper &lh = fInterp->getLookupHelper();
const ValueDecl *vd
= lh.findDataMember(GetDecl(), name,
gDebug > 5 ? cling::LookupHelper::WithDiagnostics
: cling::LookupHelper::NoDiagnostics);
if (vd) return llvm::dyn_cast<ValueDecl>(vd->getCanonicalDecl());
else return nullptr;
}
TClingMethodInfo TClingClassInfo::GetMethod(const char *fname) const
{
// Return any method or function in this scope with the name 'fname'.
if (!IsLoaded()) {
TClingMethodInfo tmi(fInterp);
return tmi;
}
R__LOCKGUARD(gInterpreterMutex);
if (fType) {
const TypedefType *TT = llvm::dyn_cast<TypedefType>(fType);
if (TT) {
llvm::StringRef tname(TT->getDecl()->getName());
if (tname == fname) {
const NamedDecl *ndecl = llvm::dyn_cast<NamedDecl>(GetDecl());
if (ndecl && ndecl->getName() != fname) {
// Constructor name matching the typedef type, use the decl name instead.
return GetMethod(ndecl->getName().str().c_str());
}
}
}
}
const cling::LookupHelper &lh = fInterp->getLookupHelper();
const FunctionDecl *fd
= lh.findAnyFunction(GetDecl(), fname,
gDebug > 5 ? cling::LookupHelper::WithDiagnostics
: cling::LookupHelper::NoDiagnostics,
false);
if (!fd) {
// Function not found.
TClingMethodInfo tmi(fInterp);
return tmi;
}
TClingMethodInfo tmi(fInterp);
tmi.Init(fd);
return tmi;
}
TClingMethodInfo TClingClassInfo::GetMethod(const char *fname,
const char *proto, Longptr_t *poffset, EFunctionMatchMode mode /*= kConversionMatch*/,
EInheritanceMode imode /*= kWithInheritance*/) const
{
return GetMethod(fname,proto,false,poffset,mode,imode);
}
TClingMethodInfo TClingClassInfo::GetMethod(const char *fname,
const char *proto, bool objectIsConst,
Longptr_t *poffset, EFunctionMatchMode mode /*= kConversionMatch*/,
EInheritanceMode imode /*= kWithInheritance*/) const
{
if (poffset) {
*poffset = 0L;
}
if (!IsLoaded()) {
TClingMethodInfo tmi(fInterp);
return tmi;
}
R__LOCKGUARD(gInterpreterMutex);
if (fType) {
const TypedefType *TT = llvm::dyn_cast<TypedefType>(fType);
if (TT) {
llvm::StringRef tname(TT->getDecl()->getName());
if (tname == fname) {
const NamedDecl *ndecl = llvm::dyn_cast<NamedDecl>(GetDecl());
if (ndecl && ndecl->getName() != fname) {
// Constructor name matching the typedef type, use the decl name instead.
return GetMethod(ndecl->getName().str().c_str(),proto,
objectIsConst,poffset,
mode,imode);
}
}
}
}
const cling::LookupHelper& lh = fInterp->getLookupHelper();
const FunctionDecl *fd;
if (mode == kConversionMatch) {
fd = lh.findFunctionProto(GetDecl(), fname, proto,
gDebug > 5 ? cling::LookupHelper::WithDiagnostics
: cling::LookupHelper::NoDiagnostics,
objectIsConst);
} else if (mode == kExactMatch) {
fd = lh.matchFunctionProto(GetDecl(), fname, proto,
gDebug > 5 ? cling::LookupHelper::WithDiagnostics
: cling::LookupHelper::NoDiagnostics,
objectIsConst);
} else {
Error("TClingClassInfo::GetMethod",
"The MatchMode %d is not supported.", mode);
TClingMethodInfo tmi(fInterp);
return tmi;
}
if (!fd) {
// Function not found.
TClingMethodInfo tmi(fInterp);
return tmi;
}
if (imode == TClingClassInfo::kInThisScope) {
// If requested, check whether fd is a member function of this class.
// Even though this seems to be the wrong order (we should not allow the
// lookup to even collect candidates from the base) it does the right
// thing: if any function overload exists in the derived class, all
// (but explicitly used) will be hidden. Thus we will only find the
// derived class's function overloads (or used, which is fine). Only
// if there is none will we find those from the base, in which case
// we will reject them here:
const clang::DeclContext* ourDC = llvm::dyn_cast<clang::DeclContext>(GetDecl());
if (!fd->getDeclContext()->Equals(ourDC)
&& !(fd->getDeclContext()->isTransparentContext()
&& fd->getDeclContext()->getParent()->Equals(ourDC)))
return TClingMethodInfo(fInterp);
// The offset must be 0 - the function must be ours.
if (poffset) *poffset = 0;
} else {
if (poffset) {
// We have been asked to return a this pointer adjustment.
if (const CXXMethodDecl *md =
llvm::dyn_cast<CXXMethodDecl>(fd)) {
// This is a class member function.
*poffset = GetOffset(md);
}
}
}
TClingMethodInfo tmi(fInterp);
tmi.Init(fd);
return tmi;
}
TClingMethodInfo TClingClassInfo::GetMethod(const char *fname,
const llvm::SmallVectorImpl<clang::QualType> &proto,
Longptr_t *poffset, EFunctionMatchMode mode /*= kConversionMatch*/,
EInheritanceMode imode /*= kWithInheritance*/) const
{
return GetMethod(fname,proto,false,poffset,mode,imode);
}
TClingMethodInfo TClingClassInfo::GetMethod(const char *fname,
const llvm::SmallVectorImpl<clang::QualType> &proto, bool objectIsConst,
Longptr_t *poffset, EFunctionMatchMode mode /*= kConversionMatch*/,
EInheritanceMode imode /*= kWithInheritance*/) const
{
if (poffset) {
*poffset = 0L;
}
if (!IsLoaded()) {
TClingMethodInfo tmi(fInterp);
return tmi;
}
R__LOCKGUARD(gInterpreterMutex);
if (fType) {
const TypedefType *TT = llvm::dyn_cast<TypedefType>(fType);
if (TT) {
llvm::StringRef tname(TT->getDecl()->getName());
if (tname == fname) {
const NamedDecl *ndecl = llvm::dyn_cast<NamedDecl>(GetDecl());
if (ndecl && ndecl->getName() != fname) {
// Constructor name matching the typedef type, use the decl name instead.
return GetMethod(ndecl->getName().str().c_str(),proto,objectIsConst,poffset,
mode,imode);
}
}
}
}
const cling::LookupHelper& lh = fInterp->getLookupHelper();
const FunctionDecl *fd;
if (mode == kConversionMatch) {
fd = lh.findFunctionProto(GetDecl(), fname, proto,
gDebug > 5 ? cling::LookupHelper::WithDiagnostics
: cling::LookupHelper::NoDiagnostics,
objectIsConst);
} else if (mode == kExactMatch) {
fd = lh.matchFunctionProto(GetDecl(), fname, proto,
gDebug > 5 ? cling::LookupHelper::WithDiagnostics
: cling::LookupHelper::NoDiagnostics,
objectIsConst);
} else {
Error("TClingClassInfo::GetMethod",
"The MatchMode %d is not supported.", mode);
TClingMethodInfo tmi(fInterp);
return tmi;
}
if (!fd) {
// Function not found.
TClingMethodInfo tmi(fInterp);
return tmi;
}
if (poffset) {
// We have been asked to return a this pointer adjustment.
if (const CXXMethodDecl *md =
llvm::dyn_cast<CXXMethodDecl>(fd)) {
// This is a class member function.
*poffset = GetOffset(md);
}
}
TClingMethodInfo tmi(fInterp);
tmi.Init(fd);
return tmi;
}
TClingMethodInfo TClingClassInfo::GetMethodWithArgs(const char *fname,
const char *arglist, Longptr_t *poffset, EFunctionMatchMode mode /* = kConversionMatch*/,
EInheritanceMode imode /* = kWithInheritance*/) const
{
return GetMethodWithArgs(fname,arglist,false,poffset,mode,imode);
}
TClingMethodInfo TClingClassInfo::GetMethodWithArgs(const char *fname,
const char *arglist, bool objectIsConst,
Longptr_t *poffset, EFunctionMatchMode /*mode = kConversionMatch*/,
EInheritanceMode /* imode = kWithInheritance*/) const
{
R__LOCKGUARD(gInterpreterMutex);
if (fType) {
const TypedefType *TT = llvm::dyn_cast<TypedefType>(fType);
if (TT) {
llvm::StringRef tname(TT->getDecl()->getName());
if (tname == fname) {
const NamedDecl *ndecl = llvm::dyn_cast<NamedDecl>(GetDecl());
if (ndecl && ndecl->getName() != fname) {
// Constructor name matching the typedef type, use the decl name instead.
return GetMethod(ndecl->getName().str().c_str(),arglist,
objectIsConst,poffset
/* ,mode,imode */);
}
}
}
}
if (poffset) {
*poffset = 0L;
}
if (!IsLoaded()) {
TClingMethodInfo tmi(fInterp);
return tmi;
}
if (!strcmp(arglist, ")")) {
// CINT accepted a single right paren as meaning no arguments.
arglist = "";
}
const cling::LookupHelper &lh = fInterp->getLookupHelper();
const FunctionDecl *fd
= lh.findFunctionArgs(GetDecl(), fname, arglist,
gDebug > 5 ? cling::LookupHelper::WithDiagnostics
: cling::LookupHelper::NoDiagnostics,
objectIsConst);
if (!fd) {
// Function not found.
TClingMethodInfo tmi(fInterp);
return tmi;
}
if (poffset) {
// We have been asked to return a this pointer adjustment.
if (const CXXMethodDecl *md =
llvm::dyn_cast<CXXMethodDecl>(fd)) {
// This is a class member function.
*poffset = GetOffset(md);
}
}
TClingMethodInfo tmi(fInterp);
tmi.Init(fd);
return tmi;
}
int TClingClassInfo::GetMethodNArg(const char *method, const char *proto,
Bool_t objectIsConst,
EFunctionMatchMode mode /*= kConversionMatch*/) const
{
// Note: Used only by TQObject.cxx:170 and only for interpreted classes.
if (!IsLoaded()) {
return -1;
}
R__LOCKGUARD(gInterpreterMutex);
TClingMethodInfo mi = GetMethod(method, proto, objectIsConst, nullptr, mode);
int clang_val = -1;
if (mi.IsValid()) {
unsigned num_params = mi.GetTargetFunctionDecl()->getNumParams();
clang_val = static_cast<int>(num_params);
}
return clang_val;
}
Longptr_t TClingClassInfo::GetOffset(const CXXMethodDecl* md) const
{
R__LOCKGUARD(gInterpreterMutex);
Longptr_t offset = 0L;
const CXXRecordDecl* definer = md->getParent();
const CXXRecordDecl* accessor =
llvm::cast<CXXRecordDecl>(GetDecl());
if (definer != accessor) {
// This function may not be accessible using a pointer
// to the declaring class, get the adjustment necessary
// to convert that to a pointer to the defining class.
TClingBaseClassInfo bi(fInterp, const_cast<TClingClassInfo*>(this));
while (bi.Next(0)) {
TClingClassInfo* bci = bi.GetBase();
if (bci->GetDecl() == definer) {
// We have found the right base class, now get the
// necessary adjustment.
offset = bi.Offset();
break;
}
}
}
return offset;
}
ptrdiff_t TClingClassInfo::GetBaseOffset(TClingClassInfo* base, void* address, bool isDerivedObject)
{
{
std::unique_lock<std::mutex> lock(fOffsetCacheMutex);
// Check for the offset in the cache.
auto iter = fOffsetCache.find(base->GetDecl());
if (iter != fOffsetCache.end()) {
std::pair<ptrdiff_t, OffsetPtrFunc_t> offsetCache = (*iter).second;
if (OffsetPtrFunc_t executableFunc = offsetCache.second) {
if (address) {
return (*executableFunc)(address, isDerivedObject);
}
else {
Error("TClingBaseClassInfo::Offset", "The address of the object for virtual base offset calculation is not valid.");
return -1;
}
}
else {
return offsetCache.first;
}
}
}
// Compute the offset.
R__WRITE_LOCKGUARD(ROOT::gCoreMutex);
TClingBaseClassInfo binfo(fInterp, this, base);
return binfo.Offset(address, isDerivedObject);
}
std::vector<std::string> TClingClassInfo::GetUsingNamespaces()
{
// Find and return all 'using' declarations of namespaces.
std::vector<std::string> res;
R__LOCKGUARD(gInterpreterMutex);
cling::Interpreter::PushTransactionRAII RAII(fInterp);
const auto DC = dyn_cast<DeclContext>(fDecl);
if (!DC)
return res;
clang::PrintingPolicy policy(fDecl->getASTContext().getPrintingPolicy());
for (auto UD : DC->using_directives()) {
NamespaceDecl *NS = UD->getNominatedNamespace();
if (NS) {
std::string nsName;
llvm::raw_string_ostream stream(nsName);
NS->getNameForDiagnostic(stream, policy, /*Qualified=*/true);
stream.flush();
res.push_back(nsName);
}
}
return res;
}
ROOT::TMetaUtils::EIOCtorCategory TClingClassInfo::HasDefaultConstructor(bool checkio, std::string *type_name) const
{
// Return true if there a constructor taking no arguments (including
// a constructor that has defaults for all of its arguments) which
// is callable. Either it has a body, or it is trivial and the
// compiler elides it.
//
// Note: This is could enhanced to also know about the ROOT ioctor
// but this was not the case in CINT.
//
using namespace ROOT::TMetaUtils;
if (!IsLoaded())
return EIOCtorCategory::kAbsent;
auto CRD = llvm::dyn_cast<CXXRecordDecl>(GetDecl());
// Namespaces do not have constructors.
if (!CRD)
return EIOCtorCategory::kAbsent;
if (checkio) {
auto kind = CheckIOConstructor(CRD, "TRootIOCtor", nullptr, *fInterp);
if ((kind == EIOCtorCategory::kIORefType) || (kind == EIOCtorCategory::kIOPtrType)) {
if (type_name) *type_name = "TRootIOCtor";
return kind;
}
kind = CheckIOConstructor(CRD, "__void__", nullptr, *fInterp);
if (kind == EIOCtorCategory::kIORefType) {
if (type_name) *type_name = "__void__";
return kind;
}
}
return CheckDefaultConstructor(CRD, *fInterp) ? EIOCtorCategory::kDefault : EIOCtorCategory::kAbsent;
}
bool TClingClassInfo::HasMethod(const char *name) const
{
R__LOCKGUARD(gInterpreterMutex);
if (IsLoaded() && !llvm::isa<EnumDecl>(GetDecl())) {
return fInterp->getLookupHelper()
.hasFunction(GetDecl(), name,
gDebug > 5 ? cling::LookupHelper::WithDiagnostics
: cling::LookupHelper::NoDiagnostics);
}
return false;
}
void TClingClassInfo::Init(const char *name)
{
fFirstTime = true;
fDescend = false;
fIsIter = false;
fIter = DeclContext::decl_iterator();
SetDecl(nullptr);
fType = nullptr;
fIterStack.clear();
const cling::LookupHelper& lh = fInterp->getLookupHelper();
SetDecl(lh.findScope(name, gDebug > 5 ? cling::LookupHelper::WithDiagnostics
: cling::LookupHelper::NoDiagnostics,
&fType, /* intantiateTemplate= */ true ));
if (!GetDecl()) {
std::string buf = TClassEdit::InsertStd(name);
if (buf != name) {
SetDecl(lh.findScope(buf, gDebug > 5 ? cling::LookupHelper::WithDiagnostics
: cling::LookupHelper::NoDiagnostics,
&fType, /* intantiateTemplate= */ true ));
}
}
if (!GetDecl() && fType) {
if (const auto *TD = fType->getAsTagDecl()) {
SetDecl(TD);
}
}
}
void TClingClassInfo::Init(const Decl* decl)
{
fFirstTime = true;
fDescend = false;
fIsIter = false;
fIter = DeclContext::decl_iterator();
SetDecl(decl);
fType = nullptr;
fIterStack.clear();
}
void TClingClassInfo::Init(int tagnum)
{
Fatal("TClingClassInfo::Init(tagnum)", "Should no longer be called");
return;
}
void TClingClassInfo::Init(const Type &tag)
{
fType = &tag;
R__LOCKGUARD(gInterpreterMutex);
if (const auto *TD = fType->getAsTagDecl()) {
SetDecl(TD);
} else {
SetDecl(nullptr);
}
if (!GetDecl()) {
QualType qType(fType,0);
static PrintingPolicy printPol(fInterp->getCI()->getLangOpts());
printPol.SuppressScope = false;
Error("TClingClassInfo::Init(const Type&)",
"The given type %s does not point to a Decl",
qType.getAsString(printPol).c_str());
}
}
bool TClingClassInfo::IsBase(const char *name) const
{
if (!IsLoaded()) {
return false;
}
TClingClassInfo base(fInterp, name);
if (!base.IsValid()) {
return false;
}
R__LOCKGUARD(gInterpreterMutex);
const CXXRecordDecl *CRD =
llvm::dyn_cast<CXXRecordDecl>(GetDecl());
if (!CRD) {
// We are an enum, namespace, or translation unit,
// we cannot be the base of anything.
return false;
}
const CXXRecordDecl *baseCRD =
llvm::dyn_cast<CXXRecordDecl>(base.GetDecl());
return CRD->isDerivedFrom(baseCRD);
}
bool TClingClassInfo::IsEnum(cling::Interpreter *interp, const char *name)
{
R__LOCKGUARD(gInterpreterMutex);
// Note: This is a static member function.
TClingClassInfo info(interp, name);
if (info.IsValid() && (info.Property() & kIsEnum)) {
return true;
}
return false;
}
bool TClingClassInfo::IsScopedEnum() const
{
if (auto *ED = llvm::dyn_cast<clang::EnumDecl>(GetDecl()))
return ED->isScoped();
return false;
}
EDataType TClingClassInfo::GetUnderlyingType() const
{
if (!IsValid())
return kNumDataTypes;
if (GetDecl() == nullptr)
return kNumDataTypes;
if (auto ED = llvm::dyn_cast<EnumDecl>(GetDecl())) {
R__LOCKGUARD(gInterpreterMutex);
auto Ty = ED->getIntegerType().getTypePtrOrNull();
if (Ty)
Ty = Ty->getUnqualifiedDesugaredType();
if (auto BTy = llvm::dyn_cast_or_null<BuiltinType>(Ty)) {
switch (BTy->getKind()) {
case BuiltinType::Bool:
return kBool_t;
case BuiltinType::Char_U:
case BuiltinType::UChar:
return kUChar_t;
case BuiltinType::Char_S:
case BuiltinType::SChar:
return kChar_t;
case BuiltinType::UShort:
return kUShort_t;
case BuiltinType::Short:
return kShort_t;
case BuiltinType::UInt:
return kUInt_t;
case BuiltinType::Int:
return kInt_t;
case BuiltinType::ULong:
return kULong_t;
case BuiltinType::Long:
return kLong_t;
case BuiltinType::ULongLong:
return kULong64_t;
case BuiltinType::LongLong:
return kLong64_t;
default:
return kNumDataTypes;
};
}
}
return kNumDataTypes;
}
bool TClingClassInfo::IsLoaded() const
{
// IsLoaded in CINT was meaning is known to the interpreter
// and has a complete definition.
// IsValid in Cling (as in CING) means 'just' is known to the
// interpreter.
if (!IsValid()) {
return false;
}
if (GetDecl() == nullptr) {
return false;
}
R__LOCKGUARD(gInterpreterMutex);
const CXXRecordDecl *CRD = llvm::dyn_cast<CXXRecordDecl>(GetDecl());
if ( CRD ) {
if (!CRD->hasDefinition()) {
return false;
}
} else {
const TagDecl *TD = llvm::dyn_cast<TagDecl>(GetDecl());
if (TD && TD->getDefinition() == nullptr) {
return false;
}
}
// All clang classes are considered loaded.
return true;
}
bool TClingClassInfo::IsValidMethod(const char *method, const char *proto,
Bool_t objectIsConst,
Longptr_t *offset,
EFunctionMatchMode mode /*= kConversionMatch*/) const
{
// Check if the method with the given prototype exist.
if (!IsLoaded()) {
return false;
}
if (offset) {
*offset = 0L;
}
TClingMethodInfo mi = GetMethod(method, proto, offset, mode);
return mi.IsValid();
}
int TClingClassInfo::InternalNext()
{
R__LOCKGUARD(gInterpreterMutex);
fDeclFileName.clear(); // invalidate decl file name.
fNameCache.clear(); // invalidate the cache.
cling::Interpreter::PushTransactionRAII RAII(fInterp);
if (fFirstTime) {
// GetDecl() must be a DeclContext in order to iterate.
const clang::DeclContext *DC = cast<DeclContext>(GetDecl());
if (fIterAll)
fIter = DC->decls_begin();
else
fIter = DC->noload_decls_begin();
}
if (!fIsIter) {
// Object was not setup for iteration.
if (GetDecl()) {
std::string buf;
if (const NamedDecl* ND =
llvm::dyn_cast<NamedDecl>(GetDecl())) {
PrintingPolicy Policy(GetDecl()->getASTContext().
getPrintingPolicy());
llvm::raw_string_ostream stream(buf);
ND->getNameForDiagnostic(stream, Policy, /*Qualified=*/false);
}
Error("TClingClassInfo::InternalNext",
"Next called but iteration not prepared for %s!", buf.c_str());
} else {
Error("TClingClassInfo::InternalNext",
"Next called but iteration not prepared!");
}
return 0;
}
while (true) {
// Advance to next usable decl, or return if there is no next usable decl.
if (fFirstTime) {
// The cint semantics are strange.
fFirstTime = false;
if (!*fIter) {
return 0;
}
}
else {
// Advance the iterator one decl, descending into the current decl
// context if necessary.
if (!fDescend) {
// Do not need to scan the decl context of the current decl,
// move on to the next decl.
++fIter;
}
else {
// Descend into the decl context of the current decl.
fDescend = false;
//fprintf(stderr,
// "TClingClassInfo::InternalNext: "
// "pushing ...\n");
fIterStack.push_back(fIter);
DeclContext *DC = llvm::cast<DeclContext>(*fIter);
if (fIterAll)
fIter = DC->decls_begin();
else
fIter = DC->noload_decls_begin();
}
// Fix it if we went past the end.
while (!*fIter && fIterStack.size()) {
//fprintf(stderr,
// "TClingClassInfo::InternalNext: "