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" 54 using namespace clang;
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();
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;
113 if (!CRD->isDerivedFrom(BaseCRD, Paths)) {
119 fIter = CRD->bases_end();
164 void* address,
bool isDerivedObject)
const 172 if (
fInterp->isInSyntaxOnlyMode())
176 const clang::RecordDecl* fromDerivedDecl
177 = dyn_cast<clang::RecordDecl>(fromDerivedClass->
GetDecl());
178 if (!fromDerivedDecl) {
179 ::Error(
"TClingBaseClassInfo::GenerateBaseOffsetFunction",
180 "Offset of non-class %s is ill-defined!", fromDerivedClass->
Name());
183 const clang::RecordDecl* toBaseDecl
184 = dyn_cast<clang::RecordDecl>(toBaseClass->
GetDecl());
186 ::Error(
"TClingBaseClassInfo::GenerateBaseOffsetFunction",
187 "Offset of non-class %s is ill-defined!", toBaseClass->
Name());
195 buf <<
"h" << fromDerivedDecl;
197 buf <<
"h" << toBaseDecl;
198 wrapper_name = buf.str();
202 if (!
fInterp->getAddressOfGlobal(wrapper_name)) {
204 string fromDerivedClassName;
205 clang::QualType QTDerived(fromDerivedClass->
GetType(), 0);
208 string toBase_class_name;
209 clang::QualType QTtoBase(toBaseClass->
GetType(), 0);
213 llvm::raw_string_ostream buf(code);
214 buf <<
"extern \"C\" long " + wrapper_name +
"(void* address, bool isDerivedObject) {\n" 216 <<
" " << fromDerivedClassName <<
" *fromDerived;" 217 <<
" if (isDerivedObject) {" 218 <<
" fromDerived = (" << fromDerivedClassName <<
"*)address;\n" 220 <<
" fromDerived = dynamic_cast<" << fromDerivedClassName <<
"*>((" << toBase_class_name <<
"*)address);\n" 222 <<
" if (!fromDerived) {\n" 225 <<
" " << toBase_class_name <<
" *toBase = fromDerived;\n" 226 <<
" return ((long)toBase - (long)fromDerived);\n}\n";
230 void*
f =
fInterp->compileFunction(wrapper_name, code,
true ,
233 ::Error(
"TClingBaseClassInfo::GenerateBaseOffsetFunction",
234 "Compilation failed!");
258 (
fIter == llvm::dyn_cast<clang::CXXRecordDecl>(
fDecl)->bases_end())) {
273 cling::Interpreter::PushTransactionRAII RAII(
fInterp);
275 const clang::RecordType *Ty =
fIter->getType()->
276 getAs<clang::RecordType>();
279 clang::CXXRecordDecl *Base = llvm::cast<clang::CXXRecordDecl>(
280 Ty->getDecl()->getDefinition());
281 clang::ASTContext &
Context = Base->getASTContext();
282 const clang::RecordDecl *RD = llvm::dyn_cast<clang::RecordDecl>(
fDecl);
283 const clang::ASTRecordLayout &
Layout = Context.getASTRecordLayout(RD);
284 int64_t offset = Layout.getBaseClassOffset(Base).getQuantity();
285 fOffset +=
static_cast<long>(offset);
287 static_cast<long>(offset)));
289 fIter = Base->bases_begin();
297 (
fIter == llvm::dyn_cast<clang::CXXRecordDecl>(
fDecl)->bases_end()) &&
308 if (
fIter == llvm::dyn_cast<clang::CXXRecordDecl>(
fDecl)->bases_end()) {
317 const clang::TagType *Ty =
fIter->getType()->getAs<clang::TagType>();
323 const clang::CXXRecordDecl *Base =
324 llvm::cast_or_null<clang::CXXRecordDecl>(Ty->getDecl()->
332 if (!onlyDirect && Base->getNumBases()) {
357 const clang::CXXRecordDecl *Src,
358 const clang::CXXRecordDecl *Dst,
359 cling::Interpreter* interp)
361 clang::CXXBasePaths Paths(
true,
true,
366 if (!Dst->isDerivedFrom(Src, Paths))
367 return clang::CharUnits::fromQuantity(-2ULL);
369 unsigned NumPublicPaths = 0;
373 for (clang::CXXBasePaths::paths_iterator
I = Paths.begin(),
E = Paths.end();
378 for (clang::CXXBasePath::iterator J =
I->begin(), JE =
I->end(); J != JE; ++J) {
381 if (J->Base->isVirtual())
382 return clang::CharUnits::fromQuantity(-1ULL);
384 if (NumPublicPaths > 1)
388 cling::Interpreter::PushTransactionRAII RAII(interp);
389 const clang::ASTRecordLayout &
L = Context.getASTRecordLayout(J->Class);
390 Offset += L.getBaseClassOffset(J->Base->getType()->getAsCXXRecordDecl());
395 if (NumPublicPaths == 0)
396 return clang::CharUnits::fromQuantity(-2ULL);
399 if (NumPublicPaths > 1)
400 return clang::CharUnits::fromQuantity(-3ULL);
415 const clang::CXXRecordDecl* Base =
423 clang::ASTContext&
Context = Base->getASTContext();
424 const clang::CXXRecordDecl* RD = llvm::dyn_cast<clang::CXXRecordDecl>(
fDecl);
430 if (clang_val == -2 || clang_val == -3) {
439 getPrintingPolicy());
440 llvm::raw_string_ostream stream(buf);
442 ->getNameForDiagnostic(stream, Policy,
true);
448 ->getNameForDiagnostic(stream, Policy,
true);
452 if (clang_val == -2) {
453 ::Error(
"TClingBaseClassInfo::Offset",
454 "The class %s does not derive from the base %s.",
455 derivedName.Data(), baseName.Data());
458 ::Error(
"TClingBaseClassInfo::Offset",
459 "There are multiple paths from derived class %s to base class %s.",
460 derivedName.Data(), baseName.Data());
469 ::Error(
"TClingBaseClassInfo::Offset",
"The address of the object for virtual base offset calculation is not valid.");
475 if (executableFunc) {
477 return (*executableFunc)(address, isDerivedObject);
495 const clang::CXXRecordDecl* CRD
496 = llvm::dyn_cast<CXXRecordDecl>(
fDecl);
497 const clang::CXXRecordDecl* BaseCRD
499 if (!CRD || !BaseCRD) {
500 ::Error(
"TClingBaseClassInfo::Property",
501 "The derived class or the base class do not have a CXXRecordDecl.");
505 clang::CXXBasePaths Paths(
false,
true,
507 if (!CRD->isDerivedFrom(BaseCRD, Paths)) {
510 ::Error(
"TClingBaseClassInfo",
"Class not derived from given base.");
512 if (Paths.getDetectedVirtual()) {
516 clang::AccessSpecifier AS = clang::AS_public;
518 for (clang::CXXBasePaths::const_paths_iterator IB = Paths.begin(), EB = Paths.end();
519 AS != clang::AS_private && IB != EB; ++IB) {
520 switch (IB->Access) {
522 case clang::AS_public:
break;
523 case clang::AS_protected: AS = clang::AS_protected;
break;
524 case clang::AS_private: AS = clang::AS_private;
break;
525 case clang::AS_none:
break;
529 case clang::AS_public:
532 case clang::AS_protected:
535 case clang::AS_private:
void AddBaseOffsetValue(const clang::Decl *decl, ptrdiff_t offset)
const char * TmpltName() const
RooArgList L(const RooAbsArg &v1)
const clang::Decl * GetDecl() const
std::vector< std::pair< std::pair< const clang::Decl *, clang::CXXRecordDecl::base_class_const_iterator >, long > > fIterStack
Small helper to keep current directory context.
static clang::CharUnits computeOffsetHint(clang::ASTContext &Context, const clang::CXXRecordDecl *Src, const clang::CXXRecordDecl *Dst, cling::Interpreter *interp)
TClingBaseClassInfo & operator=(const TClingBaseClassInfo &)
TClingBaseClassInfo(cling::Interpreter *, TClingClassInfo *)
ptrdiff_t(* OffsetPtrFunc_t)(void *, bool)
TClingClassInfo * fBaseInfo
int InternalNext(int onlyDirect)
void Error(const char *location, const char *msgfmt,...)
const char * Name() const
const char * TmpltName() const
Emulation of the CINT BaseClassInfo class.
void FullName(std::string &output, const ROOT::TMetaUtils::TNormalizedCtxt &normCtxt) const
TClingClassInfo * GetBase() const
const clang::Decl * fDecl
Emulation of the CINT ClassInfo class.
OffsetPtrFunc_t GenerateBaseOffsetFunction(const TClingClassInfo *derivedClass, TClingClassInfo *targetClass, void *address, bool isDerivedObject) const
ptrdiff_t Offset(void *address=0, bool isDerivedObject=true) const
cling::Interpreter * fInterp
RooCmdArg Layout(Double_t xmin, Double_t xmax=0.99, Double_t ymin=0.95)
clang::CXXRecordDecl::base_class_const_iterator fIter
const char * Name() const
void FullName(std::string &output, const ROOT::TMetaUtils::TNormalizedCtxt &normCtxt) const
TClingClassInfo * fClassInfo
void AddBaseOffsetFunction(const clang::Decl *decl, OffsetPtrFunc_t func)
const clang::Type * GetType() const