14 #include "cling/Interpreter/Interpreter.h"
15 #include "cling/Interpreter/InterpreterCallbacks.h"
16 #include "cling/Interpreter/Transaction.h"
17 #include "cling/Utils/AST.h"
19 #include "clang/AST/ASTConsumer.h"
20 #include "clang/AST/ASTContext.h"
21 #include "clang/AST/DeclBase.h"
22 #include "clang/AST/DeclTemplate.h"
23 #include "clang/AST/GlobalDecl.h"
24 #include "clang/Lex/Preprocessor.h"
25 #include "clang/Parse/Parser.h"
26 #include "clang/Sema/Lookup.h"
27 #include "clang/Sema/Scope.h"
29 #include "clang/Frontend/CompilerInstance.h"
30 #include "clang/Lex/PPCallbacks.h"
31 #include "llvm/Support/FileSystem.h"
35 using namespace clang;
36 using namespace cling;
56 std::string &args, std::string &io, std::string &fname);
58 llvm::StringRef canonicalName);
60 llvm::StringRef canonicalName);
64 : InterpreterCallbacks(interp),
65 fLastLookupCtx(0), fROOTSpecialNamespace(0),
66 fFirstRun(true), fIsAutoloading(
false), fIsAutoloadingRecursively(
false),
69 m_Interpreter->declare(
"namespace __ROOT_SpecialObjects{}", &T);
78 llvm::StringRef FileName,
80 clang::CharSourceRange ,
81 const clang::FileEntry *FE,
84 const clang::Module *) {
98 std::string localString(FileName.str());
100 Sema &SemaR = m_Interpreter->getSema();
101 DeclarationName Name = &SemaR.getASTContext().Idents.get(localString.c_str());
102 LookupResult RHeader(SemaR, Name, sLoc, Sema::LookupOrdinaryName);
111 llvm::SmallVectorImpl<char> &RecoveryPath) {
119 Preprocessor& PP = m_Interpreter->getCI()->getPreprocessor();
122 std::string
filename(FileName.str().substr(0,FileName.str().find_last_of(
'"')));
123 std::string fname, mode, arguments, io;
126 if (mode.length() > 0) {
127 if (llvm::sys::fs::exists(fname)) {
129 std::string options =
"k";
130 if (mode.find(
"++") != std::string::npos) options +=
"f";
131 if (mode.find(
"g") != std::string::npos) options +=
"g";
132 if (mode.find(
"O") != std::string::npos) options +=
"O";
135 Preprocessor::CleanupAndRestoreCacheRAII cleanupRAII(PP);
136 Parser&
P =
const_cast<Parser&
>(m_Interpreter->getParser());
139 Token& Tok =
const_cast<Token&
>(P.getCurToken());
144 Tok.setKind(tok::semi);
156 Sema& SemaR = m_Interpreter->getSema();
157 ASTContext&
C = SemaR.getASTContext();
158 Sema::ContextAndScopeRAII pushedDCAndS(SemaR, C.getTranslationUnitDecl(),
165 fPPOldFlag = PP.GetSuppressIncludeNotFoundError();
166 PP.SetSuppressIncludeNotFoundError(
true);
175 PP.SetSuppressIncludeNotFoundError(
fPPOldFlag);
191 if (m_Interpreter->getSema().getDiagnostics().hasErrorOccurred())
224 if (Name.getNameKind() != DeclarationName::Identifier)
return false;
231 NamespaceDecl* NSD = dyn_cast<NamespaceDecl>(
const_cast<DeclContext*
>(DC));
238 const DeclContext* primaryDC = NSD->getPrimaryContext();
242 Sema &SemaR = m_Interpreter->getSema();
243 LookupResult
R(SemaR, Name, SourceLocation(), Sema::LookupOrdinaryName);
244 R.suppressDiagnostics();
247 = NSD->getQualifiedNameAsString() +
"::" + Name.getAsString();
253 clang::Scope
S(SemaR.TUScope, clang::Scope::DeclScope, SemaR.getDiagnostics());
254 S.setEntity(const_cast<DeclContext*>(DC));
255 Sema::ContextAndScopeRAII pushedDCAndS(SemaR, const_cast<DeclContext*>(DC), &S);
258 llvm::SmallVector<NamedDecl*, 4> lookupResults;
259 for(LookupResult::iterator
I = R.begin(), E = R.end();
I <
E; ++
I)
260 lookupResults.push_back(*
I);
261 UpdateWithNewDecls(DC, Name, llvm::makeArrayRef(lookupResults.data(),
262 lookupResults.size()));
272 if (RecordDecl* RD = dyn_cast<RecordDecl>(Tag)) {
273 Sema &SemaR = m_Interpreter->getSema();
274 ASTContext&
C = SemaR.getASTContext();
275 Preprocessor &PP = SemaR.getPreprocessor();
276 Parser&
P =
const_cast<Parser&
>(m_Interpreter->getParser());
277 Preprocessor::CleanupAndRestoreCacheRAII cleanupRAII(PP);
278 Parser::ParserCurTokRestoreRAII savedCurToken(P);
281 Token& Tok =
const_cast<Token&
>(P.getCurToken());
282 Tok.setKind(tok::semi);
288 Sema::ContextAndScopeRAII pushedDCAndS(SemaR, C.getTranslationUnitDecl(),
296 C.getTypeDeclType(RD),
302 Tag->setHasExternalLexicalStorage(
false);
318 Scope *S,
const FileEntry* FE ) {
319 Sema &SemaR = m_Interpreter->getSema();
328 Sema::LookupNameKind kind = R.getLookupKind();
329 if (!(kind == Sema::LookupTagName || kind == Sema::LookupOrdinaryName
330 || kind == Sema::LookupNestedNameSpecifierName
331 || kind == Sema::LookupNamespaceName))
336 bool lookupSuccess =
false;
337 if (getenv(
"ROOT_MODULES")) {
339 lookupSuccess = FE || SemaR.LookupName(R, S);
344 ASTContext&
C = SemaR.getASTContext();
345 Preprocessor &PP = SemaR.getPreprocessor();
346 Parser&
P =
const_cast<Parser&
>(m_Interpreter->getParser());
347 Preprocessor::CleanupAndRestoreCacheRAII cleanupRAII(PP);
348 Parser::ParserCurTokRestoreRAII savedCurToken(P);
351 Token& Tok =
const_cast<Token&
>(P.getCurToken());
352 Tok.setKind(tok::semi);
358 Sema::ContextAndScopeRAII pushedDCAndS(SemaR, C.getTranslationUnitDecl(),
364 lookupSuccess = SemaR.LookupName(R, S);
366 if (R.isSingleResult()) {
367 if (isa<clang::RecordDecl>(R.getFoundDecl())) {
382 lookupSuccess = FE || SemaR.LookupName(R, S);
388 std::string incl =
"#include \"";
389 incl += FE->getName();
391 m_Interpreter->declare(incl);
423 if (R.isForRedeclaration())
428 const Sema::LookupNameKind LookupKind = R.getLookupKind();
429 if (LookupKind != Sema::LookupOrdinaryName)
433 Sema &SemaR = m_Interpreter->getSema();
434 ASTContext&
C = SemaR.getASTContext();
435 Preprocessor &PP = SemaR.getPreprocessor();
436 DeclContext *CurDC = SemaR.CurContext;
437 DeclarationName Name = R.getLookupName();
440 if(!CurDC || !CurDC->isFunctionOrMethod())
445 Preprocessor::CleanupAndRestoreCacheRAII cleanupPPRAII(PP);
452 #if defined(R__MUST_REVISIT)
453 #if R__MUST_REVISIT(6,2)
458 if (!fgSetOfSpecials) {
459 fgSetOfSpecials =
new std::set<TObject*>;
461 ((std::set<TObject*>*)fgSetOfSpecials)->insert((
TObject*)*obj);
465 VarDecl *VD = cast_or_null<VarDecl>(utils::Lookup::Named(&SemaR, Name,
470 TObject **address = (
TObject**)m_Interpreter->getAddressOfGlobal(GD);
474 CStyleCastExpr *CStyleCast = cast<CStyleCastExpr>(VD->getInit());
475 Expr* newInit = utils::Synthesize::IntegerLiteralExpr(C, (uint64_t)obj);
476 CStyleCast->setSubExpr(newInit);
483 Preprocessor::CleanupAndRestoreCacheRAII cleanupRAII(PP);
487 QualType QT = C.getPointerType(C.getTypeDeclType(cast<TypeDecl>(TD)));
490 SourceLocation(), Name.getAsIdentifierInfo(), QT,
494 = utils::Synthesize::CStyleCastPtrExpr(&SemaR, QT, (uint64_t)obj);
499 cling::CompilationOptions CO;
500 CO.DeclarationExtraction = 0;
501 CO.ValuePrinting = CompilationOptions::VPDisabled;
502 CO.ResultEvaluation = 0;
503 CO.DynamicScoping = 0;
505 CO.CodeGeneration = 1;
507 cling::Transaction*
T =
new cling::Transaction(CO, SemaR);
509 T->setState(cling::Transaction::kCompleted);
511 m_Interpreter->emitAllDecls(T);
513 assert(VD &&
"Cannot be null!");
525 DeclarationName Name = R.getLookupName();
526 IdentifierInfo* II = Name.getAsIdentifierInfo();
527 SourceLocation Loc = R.getNameLoc();
528 Sema& SemaRef = R.getSema();
529 ASTContext&
C = SemaRef.getASTContext();
530 DeclContext* DC = C.getTranslationUnitDecl();
531 assert(DC &&
"Must not be null.");
532 VarDecl* Result = VarDecl::Create(C, DC, Loc, Loc, II, C.DependentTy,
539 SourceRange invalidRange;
540 Result->addAttr(
new (C) AnnotateAttr(invalidRange, C,
"__ResolveAtRuntime", 0));
544 Sema::ContextRAII pushedDC(SemaRef, DC);
558 if (R.getLookupKind() != Sema::LookupOrdinaryName)
561 if (R.isForRedeclaration())
567 const Transaction*
T = getInterpreter()->getCurrentTransaction();
570 const cling::CompilationOptions& COpts = T->getCompilationOpts();
571 if (!COpts.DynamicScoping)
590 for (Scope* DepScope = S; DepScope; DepScope = DepScope->getParent()) {
591 if (DeclContext* Ctx = static_cast<DeclContext*>(DepScope->getEntity())) {
592 if (!Ctx->isDependentContext())
594 if (isa<FunctionDecl>(Ctx))
610 if (R.isForRedeclaration())
613 if (R.getLookupKind() != Sema::LookupOrdinaryName)
616 if (!isa<FunctionDecl>(R.getSema().CurContext))
619 Sema& SemaRef = R.getSema();
620 ASTContext&
C = SemaRef.getASTContext();
621 DeclContext* DC = SemaRef.CurContext;
622 assert(DC &&
"Must not be null.");
625 Preprocessor& PP = R.getSema().getPreprocessor();
634 DeclarationName Name = R.getLookupName();
635 IdentifierInfo* II = Name.getAsIdentifierInfo();
636 SourceLocation Loc = R.getNameLoc();
637 VarDecl* Result = VarDecl::Create(C, DC, Loc, Loc, II,
638 C.getAutoType(QualType(),
647 SourceRange invalidRange;
648 Result->addAttr(
new (C) AnnotateAttr(invalidRange, C,
"__Auto", 0));
662 Sema& SemaR = m_Interpreter->getSema();
663 cling::Transaction TPrev((cling::CompilationOptions(), SemaR));
664 TPrev.append(SemaR.getASTContext().getTranslationUnitDecl());
699 if (
const RecordDecl* RD = dyn_cast<RecordDecl>(D)) {
712 llvm::StringRef canonicalName) {
717 llvm::StringRef canonicalName) {
virtual void TransactionUnloaded(const cling::Transaction &T)
clang::NamespaceDecl * fROOTSpecialNamespace
bool equal(double d1, double d2, double stol=10000)
static const char * filename()
void TCling__GetNormalizedContext(const ROOT::TMetaUtils::TNormalizedCtxt *&)
int TCling__AutoLoadCallback(const char *className)
Decl * TCling__GetObjectDecl(TObject *obj)
int TCling__CompileMacro(const char *fileName, const char *options)
ClassImp(TIterator) Bool_t TIterator return false
Compare two iterator objects.
virtual void DeclDeserialized(const clang::Decl *D)
bool tryAutoParseInternal(llvm::StringRef Name, clang::LookupResult &R, clang::Scope *S, const clang::FileEntry *FE=0)
virtual bool FileNotFound(llvm::StringRef FileName, llvm::SmallVectorImpl< char > &RecoveryPath)
void Init(TClassEdit::TInterpreterLookupHelper *helper)
bool tryFindROOTSpecialInternal(clang::LookupResult &R, clang::Scope *S)
int TCling__AutoParseCallback(const char *className)
virtual void LibraryLoaded(const void *dyLibHandle, llvm::StringRef canonicalName)
virtual void LibraryUnloaded(const void *dyLibHandle, llvm::StringRef canonicalName)
const char * TCling__GetClassSharedLibs(const char *className)
bool tryResolveAtRuntimeInternal(clang::LookupResult &R, clang::Scope *S)
void TCling__SplitAclicMode(const char *fileName, std::string &mode, std::string &args, std::string &io, std::string &fname)
bool tryInjectImplicitAutoKeyword(clang::LookupResult &R, clang::Scope *S)
bool fIsAutoParsingSuspended
bool fIsAutoloadingRecursively
virtual void TransactionCommitted(const cling::Transaction &T)
bool IsAutoloadingEnabled()
void TCling__LibraryUnloadedRTTI(const void *dyLibHandle, llvm::StringRef canonicalName)
virtual void InclusionDirective(clang::SourceLocation, const clang::Token &, llvm::StringRef FileName, bool, clang::CharSourceRange, const clang::FileEntry *, llvm::StringRef, llvm::StringRef, const clang::Module *)
MyComplex< T > P(MyComplex< T > z, T c_real, T c_imag)
[MyComplex]
Mother of all ROOT objects.
typedef void((*Func_t)())
void TCling__UpdateListsOnUnloaded(const cling::Transaction &)
TClingCallbacks(cling::Interpreter *interp)
bool shouldResolveAtRuntime(clang::LookupResult &R, clang::Scope *S)
int TCling__IsAutoLoadNamespaceCandidate(const clang::NamespaceDecl *name)
void TCling__TransactionRollback(const cling::Transaction &)
void TCling__LibraryLoadedRTTI(const void *dyLibHandle, llvm::StringRef canonicalName)
TObject * TCling__GetObjectAddress(const char *Name, void *&LookupCtx)
void TCling__UpdateListsOnCommitted(const cling::Transaction &, Interpreter *)
virtual void TransactionRollback(const cling::Transaction &T)
virtual bool LookupObject(clang::LookupResult &R, clang::Scope *S)