void EvaluateTSynthesizer::Transform() { if (!getTransaction()->getCompilationOpts().DynamicScoping) return; // include the DynamicLookup specific builtins if (!m_EvalDecl) { TemplateDecl* D = cast_or_null<TemplateDecl>(m_Interpreter->LookupDecl("cling"). LookupDecl("runtime"). LookupDecl("internal"). LookupDecl("EvaluateT"). getSingleDecl()); assert(D && "Cannot find EvaluateT TemplateDecl!\n"); m_EvalDecl = dyn_cast<FunctionDecl>(D->getTemplatedDecl()); assert (m_EvalDecl && "The Eval function not found!"); } if (m_NoRange.isInvalid()) { NamespaceDecl* NSD = utils::Lookup::Namespace(m_Sema, "cling"); NSD = utils::Lookup::Namespace(m_Sema, "runtime", NSD); NSD = utils::Lookup::Namespace(m_Sema, "internal", NSD); DeclarationName Name = &m_Context->Idents.get( "InterpreterGeneratedCodeDiagnosticsMaybeIncorrect"); LookupResult R(*m_Sema, Name, SourceLocation(), Sema::LookupOrdinaryName, Sema::ForRedeclaration); m_Sema->LookupQualifiedName(R, NSD); assert(!R.empty() && "Cannot find PrintValue(...)"); NamedDecl* ND = R.getFoundDecl(); m_NoRange = ND->getSourceRange(); m_NoSLoc = m_NoRange.getBegin(); m_NoELoc = m_NoRange.getEnd(); } for (Transaction::const_iterator I = getTransaction()->decls_begin(), E = getTransaction()->decls_end(); I != E; ++I) for (DeclGroupRef::const_iterator J = (*I).begin(), JE = (*I).end(); J != JE; ++J) if (ShouldVisit(*J) && (*J)->hasBody()) { if (FunctionDecl* FD = dyn_cast<FunctionDecl>(*J)) { // Set the decl context, which is needed by Evaluate. m_CurDeclContext = FD->getDeclContext(); ASTNodeInfo NewBody = Visit((*J)->getBody()); FD->setBody(NewBody.getAsSingleNode()); } assert ((!isa<BlockDecl>(*J) || !isa<ObjCMethodDecl>(*J)) && "Not implemented yet!"); } //TODO: Check for error before returning. }
void EvaluateTSynthesizer::Initialize() { // Most of the declaration we are looking up are in cling::runtime::internal NamespaceDecl* NSD = utils::Lookup::Namespace(m_Sema, "cling"); NamespaceDecl* clingRuntimeNSD = utils::Lookup::Namespace(m_Sema, "runtime", NSD); NSD = utils::Lookup::Namespace(m_Sema, "internal", clingRuntimeNSD); // Find and set up EvaluateT DeclarationName Name = &m_Context->Idents.get("EvaluateT"); LookupResult R(*m_Sema, Name, SourceLocation(), Sema::LookupOrdinaryName, Sema::ForRedeclaration); assert(NSD && "There must be a valid namespace."); m_Sema->LookupQualifiedName(R, NSD); // We have specialized EvaluateT but we don't care because the templated // decl is needed. TemplateDecl* TplD = dyn_cast_or_null<TemplateDecl>(*R.begin()); m_EvalDecl = dyn_cast<FunctionDecl>(TplD->getTemplatedDecl()); assert(m_EvalDecl && "The Eval function not found!"); // Find the LifetimeHandler declaration R.clear(); Name = &m_Context->Idents.get("LifetimeHandler"); R.setLookupName(Name); m_Sema->LookupQualifiedName(R, NSD); m_LifetimeHandlerDecl = R.getAsSingle<CXXRecordDecl>(); assert(m_LifetimeHandlerDecl && "LifetimeHandler could not be found."); // Find the LifetimeHandler::getMemory declaration R.clear(); Name = &m_Context->Idents.get("getMemory"); R.setLookupName(Name); m_Sema->LookupQualifiedName(R, m_LifetimeHandlerDecl); m_LHgetMemoryDecl = R.getAsSingle<CXXMethodDecl>(); assert(m_LHgetMemoryDecl && "LifetimeHandler::getMemory couldn't be found."); // Find the DynamicExprInfo declaration R.clear(); Name = &m_Context->Idents.get("DynamicExprInfo"); R.setLookupName(Name); m_Sema->LookupQualifiedName(R, NSD); m_DynamicExprInfoDecl = R.getAsSingle<CXXRecordDecl>(); assert(m_DynamicExprInfoDecl && "DynExprInfo could not be found."); // Find the DeclContext declaration R.clear(); Name = &m_Context->Idents.get("DeclContext"); R.setLookupName(Name); NamespaceDecl* clangNSD = utils::Lookup::Namespace(m_Sema, "clang"); m_Sema->LookupQualifiedName(R, clangNSD); m_DeclContextDecl = R.getAsSingle<CXXRecordDecl>(); assert(m_DeclContextDecl && "clang::DeclContext decl could not be found."); // Find the gCling declaration R.clear(); Name = &m_Context->Idents.get("gCling"); R.setLookupName(Name); m_Sema->LookupQualifiedName(R, clingRuntimeNSD); m_gCling = R.getAsSingle<VarDecl>(); assert(m_gCling && "gCling decl could not be found."); // Find and set the source locations to valid ones. R.clear(); Name = &m_Context->Idents.get( "InterpreterGeneratedCodeDiagnosticsMaybeIncorrect"); R.setLookupName(Name); m_Sema->LookupQualifiedName(R, NSD); assert(!R.empty() && "Cannot find InterpreterGeneratedCodeDiagnosticsMaybeIncorrect"); NamedDecl* ND = R.getFoundDecl(); m_NoRange = ND->getSourceRange(); m_NoSLoc = m_NoRange.getBegin(); m_NoELoc = m_NoRange.getEnd(); }