27#include "TClingUtils.h"
31#include "cling/Interpreter/Interpreter.h"
32#include "cling/Interpreter/Transaction.h"
35#include "clang/AST/ASTContext.h"
36#include "clang/AST/Decl.h"
37#include "clang/AST/DeclCXX.h"
38#include "clang/AST/PrettyPrinter.h"
39#include "clang/AST/RecordLayout.h"
40#include "clang/AST/Type.h"
41#include "clang/AST/CXXInheritance.h"
44#include "llvm/Support/Casting.h"
45#include "llvm/Support/raw_ostream.h"
46#include "llvm/ExecutionEngine/ExecutionEngine.h"
47#include "llvm/IR/Module.h"
59 : fInterp(interp), fClassInfo(0), fFirstTime(true), fDescend(false),
60 fDecl(0), fIter(0), fBaseInfo(0), fOffset(0
L), fClassInfoOwnership(true)
72 const clang::CXXRecordDecl* CRD =
82 cling::Interpreter::PushTransactionRAII RAII(
fInterp);
83 fIter = CRD->bases_begin();
90 : fInterp(interp), fClassInfo(0), fFirstTime(true), fDescend(false),
91 fDecl(0), fIter(0), fBaseInfo(0), fOffset(0
L), fClassInfoOwnership(false)
98 const clang::CXXRecordDecl* CRD =
99 llvm::dyn_cast<clang::CXXRecordDecl>(derived->
GetDecl());
100 const clang::CXXRecordDecl* BaseCRD =
101 llvm::dyn_cast<clang::CXXRecordDecl>(base->
GetDecl());
102 if (!CRD || !BaseCRD) {
112 clang::CXXBasePaths Paths;
115 cling::Interpreter::PushTransactionRAII RAII(
fInterp);
117 if (!CRD->isDerivedFrom(BaseCRD, Paths)) {
123 fIter = CRD->bases_end();
127 : fInterp(rhs.fInterp), fClassInfo(0), fFirstTime(rhs.fFirstTime),
128 fDescend(rhs.fDescend), fDecl(rhs.fDecl), fIter(rhs.fIter), fBaseInfo(0),
129 fIterStack(rhs.fIterStack), fOffset(rhs.fOffset), fClassInfoOwnership(true)
168 void* address,
bool isDerivedObject)
const
176 if (
fInterp->isInSyntaxOnlyMode())
180 const clang::RecordDecl* fromDerivedDecl
181 = dyn_cast<clang::RecordDecl>(fromDerivedClass->
GetDecl());
182 if (!fromDerivedDecl) {
183 ::Error(
"TClingBaseClassInfo::GenerateBaseOffsetFunction",
184 "Offset of non-class %s is ill-defined!", fromDerivedClass->
Name());
187 const clang::RecordDecl* toBaseDecl
188 = dyn_cast<clang::RecordDecl>(toBaseClass->
GetDecl());
190 ::Error(
"TClingBaseClassInfo::GenerateBaseOffsetFunction",
191 "Offset of non-class %s is ill-defined!", toBaseClass->
Name());
199 buf <<
"h" << fromDerivedDecl;
201 buf <<
"h" << toBaseDecl;
202 wrapper_name = buf.str();
206 if (!
fInterp->getAddressOfGlobal(wrapper_name)) {
208 string fromDerivedClassName;
209 clang::QualType QTDerived(fromDerivedClass->
GetType(), 0);
210 ROOT::TMetaUtils::GetFullyQualifiedTypeName(fromDerivedClassName,
212 string toBase_class_name;
213 clang::QualType QTtoBase(toBaseClass->
GetType(), 0);
214 ROOT::TMetaUtils::GetFullyQualifiedTypeName(toBase_class_name,
217 llvm::raw_string_ostream buf(code);
218 buf <<
"extern \"C\" long " + wrapper_name +
"(void* address, bool isDerivedObject) {\n"
220 <<
" " << fromDerivedClassName <<
" *fromDerived;"
221 <<
" if (isDerivedObject) {"
222 <<
" fromDerived = (" << fromDerivedClassName <<
"*)address;\n"
224 <<
" fromDerived = dynamic_cast<" << fromDerivedClassName <<
"*>((" << toBase_class_name <<
"*)address);\n"
226 <<
" if (!fromDerived) {\n"
229 <<
" " << toBase_class_name <<
" *toBase = fromDerived;\n"
230 <<
" return ((long)toBase - (long)fromDerived);\n}\n";
234 void*
f =
fInterp->compileFunction(wrapper_name, code,
true ,
237 ::Error(
"TClingBaseClassInfo::GenerateBaseOffsetFunction",
238 "Compilation failed!");
262 (
fIter == llvm::dyn_cast<clang::CXXRecordDecl>(
fDecl)->bases_end())) {
278 cling::Interpreter::PushTransactionRAII RAII(
fInterp);
280 const clang::RecordType *Ty =
fIter->getType()->
281 getAs<clang::RecordType>();
284 clang::CXXRecordDecl *Base = llvm::cast<clang::CXXRecordDecl>(
285 Ty->getDecl()->getDefinition());
286 clang::ASTContext &Context = Base->getASTContext();
287 const clang::RecordDecl *RD = llvm::dyn_cast<clang::RecordDecl>(
fDecl);
288 const clang::ASTRecordLayout &
Layout = Context.getASTRecordLayout(RD);
289 int64_t offset =
Layout.getBaseClassOffset(Base).getQuantity();
290 fOffset +=
static_cast<long>(offset);
292 static_cast<long>(offset)));
294 fIter = Base->bases_begin();
302 (
fIter == llvm::dyn_cast<clang::CXXRecordDecl>(
fDecl)->bases_end()) &&
313 if (
fIter == llvm::dyn_cast<clang::CXXRecordDecl>(
fDecl)->bases_end()) {
322 const clang::TagType *Ty =
fIter->getType()->getAs<clang::TagType>();
328 const clang::CXXRecordDecl *Base =
329 llvm::cast_or_null<clang::CXXRecordDecl>(Ty->getDecl()->
337 if (!onlyDirect && Base->getNumBases()) {
362 const clang::CXXRecordDecl *Src,
363 const clang::CXXRecordDecl *
Dst,
364 cling::Interpreter* interp)
366 clang::CXXBasePaths Paths(
true,
true,
371 if (!
Dst->isDerivedFrom(Src, Paths))
372 return clang::CharUnits::fromQuantity(-2);
374 unsigned NumPublicPaths = 0;
378 for (clang::CXXBasePaths::paths_iterator
I = Paths.begin(),
E = Paths.end();
383 for (clang::CXXBasePath::iterator J =
I->begin(), JE =
I->end(); J != JE; ++J) {
386 if (J->Base->isVirtual())
387 return clang::CharUnits::fromQuantity(-1);
389 if (NumPublicPaths > 1)
393 cling::Interpreter::PushTransactionRAII RAII(interp);
394 const clang::ASTRecordLayout &
L = Context.getASTRecordLayout(J->Class);
395 Offset +=
L.getBaseClassOffset(J->Base->getType()->getAsCXXRecordDecl());
400 if (NumPublicPaths == 0)
401 return clang::CharUnits::fromQuantity(-2);
404 if (NumPublicPaths > 1)
405 return clang::CharUnits::fromQuantity(-3);
420 const clang::CXXRecordDecl* Base =
428 clang::ASTContext& Context = Base->getASTContext();
429 const clang::CXXRecordDecl* RD = llvm::dyn_cast<clang::CXXRecordDecl>(
fDecl);
435 if (clang_val == -2 || clang_val == -3) {
444 getPrintingPolicy());
445 llvm::raw_string_ostream stream(buf);
447 ->getNameForDiagnostic(stream, Policy,
true);
453 ->getNameForDiagnostic(stream, Policy,
true);
457 if (clang_val == -2) {
458 ::Error(
"TClingBaseClassInfo::Offset",
459 "The class %s does not derive from the base %s.",
460 derivedName.
Data(), baseName.
Data());
463 ::Error(
"TClingBaseClassInfo::Offset",
464 "There are multiple paths from derived class %s to base class %s.",
465 derivedName.
Data(), baseName.
Data());
474 ::Error(
"TClingBaseClassInfo::Offset",
"The address of the object for virtual base offset calculation is not valid.");
480 if (executableFunc) {
482 return (*executableFunc)(address, isDerivedObject);
500 const clang::CXXRecordDecl* CRD
501 = llvm::dyn_cast<CXXRecordDecl>(
fDecl);
502 const clang::CXXRecordDecl* BaseCRD
504 if (!CRD || !BaseCRD) {
505 ::Error(
"TClingBaseClassInfo::Property",
506 "The derived class or the base class do not have a CXXRecordDecl.");
510 clang::CXXBasePaths Paths(
false,
true,
512 if (!CRD->isDerivedFrom(BaseCRD, Paths)) {
515 ::Error(
"TClingBaseClassInfo",
"Class not derived from given base.");
517 if (Paths.getDetectedVirtual()) {
521 clang::AccessSpecifier AS = clang::AS_public;
523 for (clang::CXXBasePaths::const_paths_iterator IB = Paths.begin(), EB = Paths.end();
524 AS != clang::AS_private && IB != EB; ++IB) {
525 switch (IB->Access) {
527 case clang::AS_public:
break;
528 case clang::AS_protected: AS = clang::AS_protected;
break;
529 case clang::AS_private: AS = clang::AS_private;
break;
530 case clang::AS_none:
break;
534 case clang::AS_public:
537 case clang::AS_protected:
540 case clang::AS_private:
static clang::CharUnits computeOffsetHint(clang::ASTContext &Context, const clang::CXXRecordDecl *Src, const clang::CXXRecordDecl *Dst, cling::Interpreter *interp)
ptrdiff_t(* OffsetPtrFunc_t)(void *, bool)
void Error(const char *location, const char *msgfmt,...)
Emulation of the CINT BaseClassInfo class.
const char * TmpltName() const
const char * Name() const
const clang::Decl * fDecl
ptrdiff_t Offset(void *address=0, bool isDerivedObject=true) const
TClingClassInfo * fBaseInfo
TClingClassInfo * fClassInfo
OffsetPtrFunc_t GenerateBaseOffsetFunction(TClingClassInfo *derivedClass, TClingClassInfo *targetClass, void *address, bool isDerivedObject) const
TClingBaseClassInfo(cling::Interpreter *, TClingClassInfo *)
void FullName(std::string &output, const ROOT::TMetaUtils::TNormalizedCtxt &normCtxt) const
TClingBaseClassInfo & operator=(const TClingBaseClassInfo &)
cling::Interpreter * fInterp
std::vector< std::pair< std::pair< const clang::Decl *, clang::CXXRecordDecl::base_class_const_iterator >, long > > fIterStack
int InternalNext(int onlyDirect)
clang::CXXRecordDecl::base_class_const_iterator fIter
TClingClassInfo * GetBase() const
Emulation of the CINT ClassInfo class.
void FullName(std::string &output, const ROOT::TMetaUtils::TNormalizedCtxt &normCtxt) const
const char * TmpltName() const
void AddBaseOffsetValue(const clang::Decl *decl, ptrdiff_t offset)
const clang::Type * GetType() const
void AddBaseOffsetFunction(const clang::Decl *decl, OffsetPtrFunc_t func)
virtual const char * Name()
virtual bool IsValid() const
virtual const clang::Decl * GetDecl() const
const char * Data() const
RooCmdArg Layout(Double_t xmin, Double_t xmax=0.99, Double_t ymin=0.95)
RooCmdArg Offset(Bool_t flag=kTRUE)
static constexpr double L
constexpr Double_t E()
Base of natural log:
static void output(int code)