diff options
Diffstat (limited to 'contrib/llvm/tools/clang/lib/Frontend')
33 files changed, 3219 insertions, 9758 deletions
diff --git a/contrib/llvm/tools/clang/lib/Frontend/ASTConsumers.cpp b/contrib/llvm/tools/clang/lib/Frontend/ASTConsumers.cpp index 7b8ebf9..87b01d4 100644 --- a/contrib/llvm/tools/clang/lib/Frontend/ASTConsumers.cpp +++ b/contrib/llvm/tools/clang/lib/Frontend/ASTConsumers.cpp @@ -13,7 +13,6 @@ #include "clang/Frontend/ASTConsumers.h" #include "clang/Frontend/DocumentXML.h" -#include "clang/Frontend/PathDiagnosticClients.h" #include "clang/Basic/Diagnostic.h" #include "clang/Basic/SourceManager.h" #include "clang/Basic/FileManager.h" @@ -22,7 +21,6 @@ #include "clang/AST/ASTContext.h" #include "clang/AST/RecordLayout.h" #include "clang/AST/PrettyPrinter.h" -#include "clang/CodeGen/ModuleBuilder.h" #include "llvm/Module.h" #include "llvm/Support/Timer.h" #include "llvm/Support/raw_ostream.h" @@ -111,25 +109,14 @@ namespace { } void ASTViewer::HandleTopLevelSingleDecl(Decl *D) { - if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { - FD->print(llvm::errs()); - - if (Stmt *Body = FD->getBody()) { + if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) { + D->print(llvm::errs()); + + if (Stmt *Body = D->getBody()) { llvm::errs() << '\n'; Body->viewAST(); llvm::errs() << '\n'; } - return; - } - - if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { - MD->print(llvm::errs()); - - if (MD->getBody()) { - llvm::errs() << '\n'; - MD->getBody()->viewAST(); - llvm::errs() << '\n'; - } } } diff --git a/contrib/llvm/tools/clang/lib/Frontend/ASTMerge.cpp b/contrib/llvm/tools/clang/lib/Frontend/ASTMerge.cpp index b0faf0a..e916e20 100644 --- a/contrib/llvm/tools/clang/lib/Frontend/ASTMerge.cpp +++ b/contrib/llvm/tools/clang/lib/Frontend/ASTMerge.cpp @@ -26,7 +26,8 @@ bool ASTMergeAction::BeginSourceFileAction(CompilerInstance &CI, // FIXME: This is a hack. We need a better way to communicate the // AST file, compiler instance, and file name than member variables // of FrontendAction. - AdaptedAction->setCurrentFile(getCurrentFile(), takeCurrentASTUnit()); + AdaptedAction->setCurrentFile(getCurrentFile(), getCurrentFileKind(), + takeCurrentASTUnit()); AdaptedAction->setCompilerInstance(&CI); return AdaptedAction->BeginSourceFileAction(CI, Filename); } @@ -95,8 +96,8 @@ bool ASTMergeAction::hasPCHSupport() const { return AdaptedAction->hasPCHSupport(); } -bool ASTMergeAction::hasASTSupport() const { - return AdaptedAction->hasASTSupport(); +bool ASTMergeAction::hasASTFileSupport() const { + return AdaptedAction->hasASTFileSupport(); } bool ASTMergeAction::hasCodeCompletionSupport() const { diff --git a/contrib/llvm/tools/clang/lib/Frontend/ASTUnit.cpp b/contrib/llvm/tools/clang/lib/Frontend/ASTUnit.cpp index 4730bdc..88f0037 100644 --- a/contrib/llvm/tools/clang/lib/Frontend/ASTUnit.cpp +++ b/contrib/llvm/tools/clang/lib/Frontend/ASTUnit.cpp @@ -74,11 +74,13 @@ public: return false; } - virtual bool ReadPredefinesBuffer(llvm::StringRef PCHPredef, - FileID PCHBufferID, + virtual bool ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers, llvm::StringRef OriginalFileName, std::string &SuggestedPredefines) { - Predefines = PCHPredef; + Predefines = Buffers[0].Data; + for (unsigned I = 1, N = Buffers.size(); I != N; ++I) { + Predefines += Buffers[I].Data; + } return false; } @@ -219,6 +221,7 @@ ASTUnit *ASTUnit::LoadFromPCHFile(const std::string &Filename, // FIXME: This is broken, we should store the TargetOptions in the PCH. TargetOptions TargetOpts; TargetOpts.ABI = ""; + TargetOpts.CXXABI = "itanium"; TargetOpts.CPU = ""; TargetOpts.Features.clear(); TargetOpts.Triple = TargetTriple; @@ -332,8 +335,10 @@ ASTUnit *ASTUnit::LoadFromCompilerInvocation(CompilerInvocation *CI, assert(Clang.getFrontendOpts().Inputs.size() == 1 && "Invocation must have exactly one source file!"); - assert(Clang.getFrontendOpts().Inputs[0].first != FrontendOptions::IK_AST && + assert(Clang.getFrontendOpts().Inputs[0].first != IK_AST && "FIXME: AST inputs not yet supported here!"); + assert(Clang.getFrontendOpts().Inputs[0].first != IK_LLVM_IR && + "IR inputs not support here!"); // Create the AST unit. AST.reset(new ASTUnit(false)); @@ -354,12 +359,9 @@ ASTUnit *ASTUnit::LoadFromCompilerInvocation(CompilerInvocation *CI, // Create the source manager. Clang.setSourceManager(&AST->getSourceManager()); - // Create the preprocessor. - Clang.createPreprocessor(); - Act.reset(new TopLevelDeclTrackerAction(*AST)); if (!Act->BeginSourceFile(Clang, Clang.getFrontendOpts().Inputs[0].second, - /*IsAST=*/false)) + Clang.getFrontendOpts().Inputs[0].first)) goto error; Act->Execute(); diff --git a/contrib/llvm/tools/clang/lib/Frontend/AnalysisConsumer.cpp b/contrib/llvm/tools/clang/lib/Frontend/AnalysisConsumer.cpp deleted file mode 100644 index 6a47279..0000000 --- a/contrib/llvm/tools/clang/lib/Frontend/AnalysisConsumer.cpp +++ /dev/null @@ -1,587 +0,0 @@ -//===--- AnalysisConsumer.cpp - ASTConsumer for running Analyses ----------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// "Meta" ASTConsumer for running different source analyses. -// -//===----------------------------------------------------------------------===// - -#include "clang/Frontend/AnalysisConsumer.h" -#include "clang/AST/ASTConsumer.h" -#include "clang/AST/Decl.h" -#include "clang/AST/DeclCXX.h" -#include "clang/AST/DeclObjC.h" -#include "clang/AST/ParentMap.h" -#include "clang/Analysis/Analyses/LiveVariables.h" -#include "clang/Analysis/Analyses/UninitializedValues.h" -#include "clang/Analysis/CFG.h" -#include "clang/Checker/Checkers/LocalCheckers.h" -#include "clang/Checker/ManagerRegistry.h" -#include "clang/Checker/BugReporter/PathDiagnostic.h" -#include "clang/Checker/PathSensitive/AnalysisManager.h" -#include "clang/Checker/BugReporter/BugReporter.h" -#include "clang/Checker/PathSensitive/GRExprEngine.h" -#include "clang/Checker/PathSensitive/GRTransferFuncs.h" -#include "clang/Basic/FileManager.h" -#include "clang/Basic/SourceManager.h" -#include "clang/Frontend/PathDiagnosticClients.h" -#include "clang/Lex/Preprocessor.h" -#include "llvm/Support/raw_ostream.h" -#include "llvm/System/Path.h" -#include "llvm/System/Program.h" -#include "llvm/ADT/OwningPtr.h" - -using namespace clang; - -static ExplodedNode::Auditor* CreateUbiViz(); - -//===----------------------------------------------------------------------===// -// Special PathDiagnosticClients. -//===----------------------------------------------------------------------===// - -static PathDiagnosticClient* -CreatePlistHTMLDiagnosticClient(const std::string& prefix, - const Preprocessor &PP) { - llvm::sys::Path F(prefix); - PathDiagnosticClient *PD = CreateHTMLDiagnosticClient(F.getDirname(), PP); - return CreatePlistDiagnosticClient(prefix, PP, PD); -} - -//===----------------------------------------------------------------------===// -// AnalysisConsumer declaration. -//===----------------------------------------------------------------------===// - -namespace { - -class AnalysisConsumer : public ASTConsumer { -public: - typedef void (*CodeAction)(AnalysisConsumer &C, AnalysisManager &M, Decl *D); - typedef void (*TUAction)(AnalysisConsumer &C, AnalysisManager &M, - TranslationUnitDecl &TU); - -private: - typedef std::vector<CodeAction> Actions; - typedef std::vector<TUAction> TUActions; - - Actions FunctionActions; - Actions ObjCMethodActions; - Actions ObjCImplementationActions; - Actions CXXMethodActions; - TUActions TranslationUnitActions; // Remove this. - -public: - ASTContext* Ctx; - const Preprocessor &PP; - const std::string OutDir; - AnalyzerOptions Opts; - bool declDisplayed; - - - // PD is owned by AnalysisManager. - PathDiagnosticClient *PD; - - StoreManagerCreator CreateStoreMgr; - ConstraintManagerCreator CreateConstraintMgr; - - llvm::OwningPtr<AnalysisManager> Mgr; - - AnalysisConsumer(const Preprocessor& pp, - const std::string& outdir, - const AnalyzerOptions& opts) - : Ctx(0), PP(pp), OutDir(outdir), - Opts(opts), declDisplayed(false), PD(0) { - DigestAnalyzerOptions(); - } - - void DigestAnalyzerOptions() { - // Create the PathDiagnosticClient. - if (!OutDir.empty()) { - switch (Opts.AnalysisDiagOpt) { - default: -#define ANALYSIS_DIAGNOSTICS(NAME, CMDFLAG, DESC, CREATEFN, AUTOCREATE) \ - case PD_##NAME: PD = CREATEFN(OutDir, PP); break; -#include "clang/Frontend/Analyses.def" - } - } - - // Create the analyzer component creators. - if (ManagerRegistry::StoreMgrCreator != 0) { - CreateStoreMgr = ManagerRegistry::StoreMgrCreator; - } - else { - switch (Opts.AnalysisStoreOpt) { - default: - assert(0 && "Unknown store manager."); -#define ANALYSIS_STORE(NAME, CMDFLAG, DESC, CREATEFN) \ - case NAME##Model: CreateStoreMgr = CREATEFN; break; -#include "clang/Frontend/Analyses.def" - } - } - - if (ManagerRegistry::ConstraintMgrCreator != 0) - CreateConstraintMgr = ManagerRegistry::ConstraintMgrCreator; - else { - switch (Opts.AnalysisConstraintsOpt) { - default: - assert(0 && "Unknown store manager."); -#define ANALYSIS_CONSTRAINTS(NAME, CMDFLAG, DESC, CREATEFN) \ - case NAME##Model: CreateConstraintMgr = CREATEFN; break; -#include "clang/Frontend/Analyses.def" - } - } - } - - void DisplayFunction(const Decl *D) { - if (!Opts.AnalyzerDisplayProgress || declDisplayed) - return; - - declDisplayed = true; - SourceManager &SM = Mgr->getASTContext().getSourceManager(); - PresumedLoc Loc = SM.getPresumedLoc(D->getLocation()); - llvm::errs() << "ANALYZE: " << Loc.getFilename(); - - if (isa<FunctionDecl>(D) || isa<ObjCMethodDecl>(D)) { - const NamedDecl *ND = cast<NamedDecl>(D); - llvm::errs() << ' ' << ND << '\n'; - } - else if (isa<BlockDecl>(D)) { - llvm::errs() << ' ' << "block(line:" << Loc.getLine() << ",col:" - << Loc.getColumn() << '\n'; - } - } - - void addCodeAction(CodeAction action) { - FunctionActions.push_back(action); - ObjCMethodActions.push_back(action); - CXXMethodActions.push_back(action); - } - - void addTranslationUnitAction(TUAction action) { - TranslationUnitActions.push_back(action); - } - - void addObjCImplementationAction(CodeAction action) { - ObjCImplementationActions.push_back(action); - } - - virtual void Initialize(ASTContext &Context) { - Ctx = &Context; - Mgr.reset(new AnalysisManager(*Ctx, PP.getDiagnostics(), - PP.getLangOptions(), PD, - CreateStoreMgr, CreateConstraintMgr, - Opts.MaxNodes, Opts.MaxLoop, - Opts.VisualizeEGDot, Opts.VisualizeEGUbi, - Opts.PurgeDead, Opts.EagerlyAssume, - Opts.TrimGraph, Opts.InlineCall)); - } - - virtual void HandleTranslationUnit(ASTContext &C); - void HandleCode(Decl *D, Stmt* Body, Actions& actions); -}; -} // end anonymous namespace - -//===----------------------------------------------------------------------===// -// AnalysisConsumer implementation. -//===----------------------------------------------------------------------===// - -void AnalysisConsumer::HandleTranslationUnit(ASTContext &C) { - - TranslationUnitDecl *TU = C.getTranslationUnitDecl(); - - for (DeclContext::decl_iterator I = TU->decls_begin(), E = TU->decls_end(); - I != E; ++I) { - Decl *D = *I; - - switch (D->getKind()) { - case Decl::CXXConstructor: - case Decl::CXXDestructor: - case Decl::CXXConversion: - case Decl::CXXMethod: - case Decl::Function: { - FunctionDecl* FD = cast<FunctionDecl>(D); - - if (FD->isThisDeclarationADefinition()) { - if (!Opts.AnalyzeSpecificFunction.empty() && - FD->getDeclName().getAsString() != Opts.AnalyzeSpecificFunction) - break; - HandleCode(FD, FD->getBody(), FunctionActions); - } - break; - } - - case Decl::ObjCMethod: { - ObjCMethodDecl* MD = cast<ObjCMethodDecl>(D); - - if (MD->isThisDeclarationADefinition()) { - if (!Opts.AnalyzeSpecificFunction.empty() && - Opts.AnalyzeSpecificFunction != MD->getSelector().getAsString()) - break; - HandleCode(MD, MD->getBody(), ObjCMethodActions); - } - break; - } - - case Decl::ObjCImplementation: { - ObjCImplementationDecl* ID = cast<ObjCImplementationDecl>(*I); - HandleCode(ID, 0, ObjCImplementationActions); - - for (ObjCImplementationDecl::method_iterator MI = ID->meth_begin(), - ME = ID->meth_end(); MI != ME; ++MI) { - if ((*MI)->isThisDeclarationADefinition()) { - if (!Opts.AnalyzeSpecificFunction.empty() && - Opts.AnalyzeSpecificFunction != (*MI)->getSelector().getAsString()) - break; - HandleCode(*MI, (*MI)->getBody(), ObjCMethodActions); - } - } - break; - } - - default: - break; - } - } - - for (TUActions::iterator I = TranslationUnitActions.begin(), - E = TranslationUnitActions.end(); I != E; ++I) { - (*I)(*this, *Mgr, *TU); - } - - // Explicitly destroy the PathDiagnosticClient. This will flush its output. - // FIXME: This should be replaced with something that doesn't rely on - // side-effects in PathDiagnosticClient's destructor. This is required when - // used with option -disable-free. - Mgr.reset(NULL); -} - -static void FindBlocks(DeclContext *D, llvm::SmallVectorImpl<Decl*> &WL) { - if (BlockDecl *BD = dyn_cast<BlockDecl>(D)) - WL.push_back(BD); - - for (DeclContext::decl_iterator I = D->decls_begin(), E = D->decls_end(); - I!=E; ++I) - if (DeclContext *DC = dyn_cast<DeclContext>(*I)) - FindBlocks(DC, WL); -} - -void AnalysisConsumer::HandleCode(Decl *D, Stmt* Body, Actions& actions) { - - // Don't run the actions if an error has occured with parsing the file. - Diagnostic &Diags = PP.getDiagnostics(); - if (Diags.hasErrorOccurred() || Diags.hasFatalErrorOccurred()) - return; - - // Don't run the actions on declarations in header files unless - // otherwise specified. - if (!Opts.AnalyzeAll && - !Ctx->getSourceManager().isFromMainFile(D->getLocation())) - return; - - // Clear the AnalysisManager of old AnalysisContexts. - Mgr->ClearContexts(); - - // Dispatch on the actions. - llvm::SmallVector<Decl*, 10> WL; - WL.push_back(D); - - if (Body && Opts.AnalyzeNestedBlocks) - FindBlocks(cast<DeclContext>(D), WL); - - for (Actions::iterator I = actions.begin(), E = actions.end(); I != E; ++I) - for (llvm::SmallVectorImpl<Decl*>::iterator WI=WL.begin(), WE=WL.end(); - WI != WE; ++WI) - (*I)(*this, *Mgr, *WI); -} - -//===----------------------------------------------------------------------===// -// Analyses -//===----------------------------------------------------------------------===// - -static void ActionWarnDeadStores(AnalysisConsumer &C, AnalysisManager& mgr, - Decl *D) { - if (LiveVariables *L = mgr.getLiveVariables(D)) { - BugReporter BR(mgr); - CheckDeadStores(*mgr.getCFG(D), *L, mgr.getParentMap(D), BR); - } -} - -static void ActionWarnUninitVals(AnalysisConsumer &C, AnalysisManager& mgr, - Decl *D) { - if (CFG* c = mgr.getCFG(D)) { - CheckUninitializedValues(*c, mgr.getASTContext(), mgr.getDiagnostic()); - } -} - - -static void ActionGRExprEngine(AnalysisConsumer &C, AnalysisManager& mgr, - Decl *D, - GRTransferFuncs* tf) { - - llvm::OwningPtr<GRTransferFuncs> TF(tf); - - // Construct the analysis engine. We first query for the LiveVariables - // information to see if the CFG is valid. - // FIXME: Inter-procedural analysis will need to handle invalid CFGs. - if (!mgr.getLiveVariables(D)) - return; - GRExprEngine Eng(mgr, TF.take()); - - if (C.Opts.EnableExperimentalInternalChecks) - RegisterExperimentalInternalChecks(Eng); - - RegisterAppleChecks(Eng, *D); - - if (C.Opts.EnableExperimentalChecks) - RegisterExperimentalChecks(Eng); - - // Set the graph auditor. - llvm::OwningPtr<ExplodedNode::Auditor> Auditor; - if (mgr.shouldVisualizeUbigraph()) { - Auditor.reset(CreateUbiViz()); - ExplodedNode::SetAuditor(Auditor.get()); - } - - // Execute the worklist algorithm. - Eng.ExecuteWorkList(mgr.getStackFrame(D), mgr.getMaxNodes()); - - // Release the auditor (if any) so that it doesn't monitor the graph - // created BugReporter. - ExplodedNode::SetAuditor(0); - - // Visualize the exploded graph. - if (mgr.shouldVisualizeGraphviz()) - Eng.ViewGraph(mgr.shouldTrimGraph()); - - // Display warnings. - Eng.getBugReporter().FlushReports(); -} - -static void ActionObjCMemCheckerAux(AnalysisConsumer &C, AnalysisManager& mgr, - Decl *D, bool GCEnabled) { - - GRTransferFuncs* TF = MakeCFRefCountTF(mgr.getASTContext(), - GCEnabled, - mgr.getLangOptions()); - - ActionGRExprEngine(C, mgr, D, TF); -} - -static void ActionObjCMemChecker(AnalysisConsumer &C, AnalysisManager& mgr, - Decl *D) { - - switch (mgr.getLangOptions().getGCMode()) { - default: - assert (false && "Invalid GC mode."); - case LangOptions::NonGC: - ActionObjCMemCheckerAux(C, mgr, D, false); - break; - - case LangOptions::GCOnly: - ActionObjCMemCheckerAux(C, mgr, D, true); - break; - - case LangOptions::HybridGC: - ActionObjCMemCheckerAux(C, mgr, D, false); - ActionObjCMemCheckerAux(C, mgr, D, true); - break; - } -} - -static void ActionDisplayLiveVariables(AnalysisConsumer &C, - AnalysisManager& mgr, Decl *D) { - if (LiveVariables* L = mgr.getLiveVariables(D)) { - L->dumpBlockLiveness(mgr.getSourceManager()); - } -} - -static void ActionCFGDump(AnalysisConsumer &C, AnalysisManager& mgr, Decl *D) { - if (CFG *cfg = mgr.getCFG(D)) { - cfg->dump(mgr.getLangOptions()); - } -} - -static void ActionCFGView(AnalysisConsumer &C, AnalysisManager& mgr, Decl *D) { - if (CFG *cfg = mgr.getCFG(D)) { - cfg->viewCFG(mgr.getLangOptions()); - } -} - -static void ActionSecuritySyntacticChecks(AnalysisConsumer &C, - AnalysisManager &mgr, Decl *D) { - BugReporter BR(mgr); - CheckSecuritySyntaxOnly(D, BR); -} - -static void ActionLLVMConventionChecker(AnalysisConsumer &C, - AnalysisManager &mgr, - TranslationUnitDecl &TU) { - BugReporter BR(mgr); - CheckLLVMConventions(TU, BR); -} - -static void ActionWarnObjCDealloc(AnalysisConsumer &C, AnalysisManager& mgr, - Decl *D) { - if (mgr.getLangOptions().getGCMode() == LangOptions::GCOnly) - return; - BugReporter BR(mgr); - CheckObjCDealloc(cast<ObjCImplementationDecl>(D), mgr.getLangOptions(), BR); -} - -static void ActionWarnObjCUnusedIvars(AnalysisConsumer &C, AnalysisManager& mgr, - Decl *D) { - BugReporter BR(mgr); - CheckObjCUnusedIvar(cast<ObjCImplementationDecl>(D), BR); -} - -static void ActionWarnObjCMethSigs(AnalysisConsumer &C, AnalysisManager& mgr, - Decl *D) { - BugReporter BR(mgr); - CheckObjCInstMethSignature(cast<ObjCImplementationDecl>(D), BR); -} - -static void ActionWarnSizeofPointer(AnalysisConsumer &C, AnalysisManager &mgr, - Decl *D) { - BugReporter BR(mgr); - CheckSizeofPointer(D, BR); -} - -//===----------------------------------------------------------------------===// -// AnalysisConsumer creation. -//===----------------------------------------------------------------------===// - -ASTConsumer* clang::CreateAnalysisConsumer(const Preprocessor& pp, - const std::string& OutDir, - const AnalyzerOptions& Opts) { - llvm::OwningPtr<AnalysisConsumer> C(new AnalysisConsumer(pp, OutDir, Opts)); - - for (unsigned i = 0; i < Opts.AnalysisList.size(); ++i) - switch (Opts.AnalysisList[i]) { -#define ANALYSIS(NAME, CMD, DESC, SCOPE)\ - case NAME:\ - C->add ## SCOPE ## Action(&Action ## NAME);\ - break; -#include "clang/Frontend/Analyses.def" - default: break; - } - - // Last, disable the effects of '-Werror' when using the AnalysisConsumer. - pp.getDiagnostics().setWarningsAsErrors(false); - - return C.take(); -} - -//===----------------------------------------------------------------------===// -// Ubigraph Visualization. FIXME: Move to separate file. -//===----------------------------------------------------------------------===// - -namespace { - -class UbigraphViz : public ExplodedNode::Auditor { - llvm::OwningPtr<llvm::raw_ostream> Out; - llvm::sys::Path Dir, Filename; - unsigned Cntr; - - typedef llvm::DenseMap<void*,unsigned> VMap; - VMap M; - -public: - UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir, - llvm::sys::Path& filename); - - ~UbigraphViz(); - - virtual void AddEdge(ExplodedNode* Src, ExplodedNode* Dst); -}; - -} // end anonymous namespace - -static ExplodedNode::Auditor* CreateUbiViz() { - std::string ErrMsg; - - llvm::sys::Path Dir = llvm::sys::Path::GetTemporaryDirectory(&ErrMsg); - if (!ErrMsg.empty()) - return 0; - - llvm::sys::Path Filename = Dir; - Filename.appendComponent("llvm_ubi"); - Filename.makeUnique(true,&ErrMsg); - - if (!ErrMsg.empty()) - return 0; - - llvm::errs() << "Writing '" << Filename.str() << "'.\n"; - - llvm::OwningPtr<llvm::raw_fd_ostream> Stream; - Stream.reset(new llvm::raw_fd_ostream(Filename.c_str(), ErrMsg)); - - if (!ErrMsg.empty()) - return 0; - - return new UbigraphViz(Stream.take(), Dir, Filename); -} - -void UbigraphViz::AddEdge(ExplodedNode* Src, ExplodedNode* Dst) { - - assert (Src != Dst && "Self-edges are not allowed."); - - // Lookup the Src. If it is a new node, it's a root. - VMap::iterator SrcI= M.find(Src); - unsigned SrcID; - - if (SrcI == M.end()) { - M[Src] = SrcID = Cntr++; - *Out << "('vertex', " << SrcID << ", ('color','#00ff00'))\n"; - } - else - SrcID = SrcI->second; - - // Lookup the Dst. - VMap::iterator DstI= M.find(Dst); - unsigned DstID; - - if (DstI == M.end()) { - M[Dst] = DstID = Cntr++; - *Out << "('vertex', " << DstID << ")\n"; - } - else { - // We have hit DstID before. Change its style to reflect a cache hit. - DstID = DstI->second; - *Out << "('change_vertex_style', " << DstID << ", 1)\n"; - } - - // Add the edge. - *Out << "('edge', " << SrcID << ", " << DstID - << ", ('arrow','true'), ('oriented', 'true'))\n"; -} - -UbigraphViz::UbigraphViz(llvm::raw_ostream* out, llvm::sys::Path& dir, - llvm::sys::Path& filename) - : Out(out), Dir(dir), Filename(filename), Cntr(0) { - - *Out << "('vertex_style_attribute', 0, ('shape', 'icosahedron'))\n"; - *Out << "('vertex_style', 1, 0, ('shape', 'sphere'), ('color', '#ffcc66')," - " ('size', '1.5'))\n"; -} - -UbigraphViz::~UbigraphViz() { - Out.reset(0); - llvm::errs() << "Running 'ubiviz' program... "; - std::string ErrMsg; - llvm::sys::Path Ubiviz = llvm::sys::Program::FindProgramByName("ubiviz"); - std::vector<const char*> args; - args.push_back(Ubiviz.c_str()); - args.push_back(Filename.c_str()); - args.push_back(0); - - if (llvm::sys::Program::ExecuteAndWait(Ubiviz, &args[0],0,0,0,0,&ErrMsg)) { - llvm::errs() << "Error viewing graph: " << ErrMsg << "\n"; - } - - // Delete the directory. - Dir.eraseFromDisk(true); -} diff --git a/contrib/llvm/tools/clang/lib/Frontend/BoostConAction.cpp b/contrib/llvm/tools/clang/lib/Frontend/BoostConAction.cpp index ae150c6..4a12ff2 100644 --- a/contrib/llvm/tools/clang/lib/Frontend/BoostConAction.cpp +++ b/contrib/llvm/tools/clang/lib/Frontend/BoostConAction.cpp @@ -14,17 +14,17 @@ using namespace clang; namespace { - class BoostConASTConsumer : public ASTConsumer, + class BoostConASTConsumer : public ASTConsumer, public RecursiveASTVisitor<BoostConASTConsumer> { public: /// HandleTranslationUnit - This method is called when the ASTs for entire /// translation unit have been parsed. virtual void HandleTranslationUnit(ASTContext &Ctx); - + bool VisitCXXRecordDecl(CXXRecordDecl *D) { std::cout << D->getNameAsString() << std::endl; - return false; - } + return true; + } }; } @@ -35,5 +35,5 @@ ASTConsumer *BoostConAction::CreateASTConsumer(CompilerInstance &CI, void BoostConASTConsumer::HandleTranslationUnit(ASTContext &Ctx) { fprintf(stderr, "Welcome to BoostCon!\n"); - Visit(Ctx.getTranslationUnitDecl()); + TraverseDecl(Ctx.getTranslationUnitDecl()); } diff --git a/contrib/llvm/tools/clang/lib/Frontend/CMakeLists.txt b/contrib/llvm/tools/clang/lib/Frontend/CMakeLists.txt index 01592d1..8757e2c 100644 --- a/contrib/llvm/tools/clang/lib/Frontend/CMakeLists.txt +++ b/contrib/llvm/tools/clang/lib/Frontend/CMakeLists.txt @@ -4,23 +4,18 @@ add_clang_library(clangFrontend ASTConsumers.cpp ASTMerge.cpp ASTUnit.cpp - AnalysisConsumer.cpp BoostConAction.cpp CacheTokens.cpp - CodeGenAction.cpp CompilerInstance.cpp CompilerInvocation.cpp DeclXML.cpp DependencyFile.cpp DiagChecker.cpp DocumentXML.cpp - FixItRewriter.cpp FrontendAction.cpp FrontendActions.cpp FrontendOptions.cpp GeneratePCH.cpp - HTMLDiagnostics.cpp - HTMLPrint.cpp InitHeaderSearch.cpp InitPreprocessor.cpp LangStandards.cpp @@ -30,12 +25,8 @@ add_clang_library(clangFrontend PCHWriter.cpp PCHWriterDecl.cpp PCHWriterStmt.cpp - PlistDiagnostics.cpp PrintParserCallbacks.cpp PrintPreprocessedOutput.cpp - RewriteMacros.cpp - RewriteObjC.cpp - RewriteTest.cpp StmtXML.cpp TextDiagnosticBuffer.cpp TextDiagnosticPrinter.cpp @@ -53,7 +44,10 @@ IF(MSVC) ENDIF(MSVC) add_dependencies(clangFrontend + ClangAttrClasses + ClangAttrList ClangDiagnosticFrontend ClangDiagnosticLex ClangDiagnosticSema + ClangDeclNodes ClangStmtNodes) diff --git a/contrib/llvm/tools/clang/lib/Frontend/CodeGenAction.cpp b/contrib/llvm/tools/clang/lib/Frontend/CodeGenAction.cpp deleted file mode 100644 index 3416aa8..0000000 --- a/contrib/llvm/tools/clang/lib/Frontend/CodeGenAction.cpp +++ /dev/null @@ -1,593 +0,0 @@ -//===--- CodeGenAction.cpp - LLVM Code Generation Frontend Action ---------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// - -#include "clang/Frontend/CodeGenAction.h" -#include "clang/Basic/SourceManager.h" -#include "clang/Basic/TargetInfo.h" -#include "clang/Basic/TargetOptions.h" -#include "clang/AST/ASTConsumer.h" -#include "clang/AST/ASTContext.h" -#include "clang/AST/DeclGroup.h" -#include "clang/CodeGen/CodeGenOptions.h" -#include "clang/CodeGen/ModuleBuilder.h" -#include "clang/Frontend/ASTConsumers.h" -#include "clang/Frontend/CompilerInstance.h" -#include "clang/Frontend/FrontendDiagnostic.h" -#include "llvm/LLVMContext.h" -#include "llvm/Module.h" -#include "llvm/PassManager.h" -#include "llvm/ADT/OwningPtr.h" -#include "llvm/Assembly/PrintModulePass.h" -#include "llvm/Analysis/CallGraph.h" -#include "llvm/Analysis/Verifier.h" -#include "llvm/Bitcode/ReaderWriter.h" -#include "llvm/CodeGen/RegAllocRegistry.h" -#include "llvm/CodeGen/SchedulerRegistry.h" -#include "llvm/Support/FormattedStream.h" -#include "llvm/Support/MemoryBuffer.h" -#include "llvm/Support/SourceMgr.h" -#include "llvm/Support/StandardPasses.h" -#include "llvm/Support/Timer.h" -#include "llvm/Target/SubtargetFeature.h" -#include "llvm/Target/TargetData.h" -#include "llvm/Target/TargetMachine.h" -#include "llvm/Target/TargetOptions.h" -#include "llvm/Target/TargetRegistry.h" -using namespace clang; -using namespace llvm; - -namespace { - enum BackendAction { - Backend_EmitAssembly, ///< Emit native assembly files - Backend_EmitBC, ///< Emit LLVM bitcode files - Backend_EmitLL, ///< Emit human-readable LLVM assembly - Backend_EmitNothing, ///< Don't emit anything (benchmarking mode) - Backend_EmitMCNull, ///< Run CodeGen, but don't emit anything - Backend_EmitObj ///< Emit native object files - }; - - class BackendConsumer : public ASTConsumer { - Diagnostic &Diags; - BackendAction Action; - const CodeGenOptions &CodeGenOpts; - const LangOptions &LangOpts; - const TargetOptions &TargetOpts; - llvm::raw_ostream *AsmOutStream; - llvm::formatted_raw_ostream FormattedOutStream; - ASTContext *Context; - - Timer LLVMIRGeneration; - Timer CodeGenerationTime; - - llvm::OwningPtr<CodeGenerator> Gen; - - llvm::OwningPtr<llvm::Module> TheModule; - llvm::TargetData *TheTargetData; - - mutable FunctionPassManager *CodeGenPasses; - mutable PassManager *PerModulePasses; - mutable FunctionPassManager *PerFunctionPasses; - - FunctionPassManager *getCodeGenPasses() const; - PassManager *getPerModulePasses() const; - FunctionPassManager *getPerFunctionPasses() const; - - void CreatePasses(); - - /// AddEmitPasses - Add passes necessary to emit assembly or LLVM IR. - /// - /// \return True on success. - bool AddEmitPasses(); - - void EmitAssembly(); - - public: - BackendConsumer(BackendAction action, Diagnostic &_Diags, - const LangOptions &langopts, const CodeGenOptions &compopts, - const TargetOptions &targetopts, bool TimePasses, - const std::string &infile, llvm::raw_ostream *OS, - LLVMContext &C) : - Diags(_Diags), - Action(action), - CodeGenOpts(compopts), - LangOpts(langopts), - TargetOpts(targetopts), - AsmOutStream(OS), - LLVMIRGeneration("LLVM IR Generation Time"), - CodeGenerationTime("Code Generation Time"), - Gen(CreateLLVMCodeGen(Diags, infile, compopts, C)), - TheTargetData(0), - CodeGenPasses(0), PerModulePasses(0), PerFunctionPasses(0) { - - if (AsmOutStream) - FormattedOutStream.setStream(*AsmOutStream, - formatted_raw_ostream::PRESERVE_STREAM); - - llvm::TimePassesIsEnabled = TimePasses; - } - - ~BackendConsumer() { - delete TheTargetData; - delete CodeGenPasses; - delete PerModulePasses; - delete PerFunctionPasses; - } - - llvm::Module *takeModule() { return TheModule.take(); } - - virtual void Initialize(ASTContext &Ctx) { - Context = &Ctx; - - if (llvm::TimePassesIsEnabled) - LLVMIRGeneration.startTimer(); - - Gen->Initialize(Ctx); - - TheModule.reset(Gen->GetModule()); - TheTargetData = new llvm::TargetData(Ctx.Target.getTargetDescription()); - - if (llvm::TimePassesIsEnabled) - LLVMIRGeneration.stopTimer(); - } - - virtual void HandleTopLevelDecl(DeclGroupRef D) { - PrettyStackTraceDecl CrashInfo(*D.begin(), SourceLocation(), - Context->getSourceManager(), - "LLVM IR generation of declaration"); - - if (llvm::TimePassesIsEnabled) - LLVMIRGeneration.startTimer(); - - Gen->HandleTopLevelDecl(D); - - if (llvm::TimePassesIsEnabled) - LLVMIRGeneration.stopTimer(); - } - - virtual void HandleTranslationUnit(ASTContext &C) { - { - PrettyStackTraceString CrashInfo("Per-file LLVM IR generation"); - if (llvm::TimePassesIsEnabled) - LLVMIRGeneration.startTimer(); - - Gen->HandleTranslationUnit(C); - - if (llvm::TimePassesIsEnabled) - LLVMIRGeneration.stopTimer(); - } - - // EmitAssembly times and registers crash info itself. - EmitAssembly(); - - // Force a flush here in case we never get released. - if (AsmOutStream) - FormattedOutStream.flush(); - } - - virtual void HandleTagDeclDefinition(TagDecl *D) { - PrettyStackTraceDecl CrashInfo(D, SourceLocation(), - Context->getSourceManager(), - "LLVM IR generation of declaration"); - Gen->HandleTagDeclDefinition(D); - } - - virtual void CompleteTentativeDefinition(VarDecl *D) { - Gen->CompleteTentativeDefinition(D); - } - - virtual void HandleVTable(CXXRecordDecl *RD, bool DefinitionRequired) { - Gen->HandleVTable(RD, DefinitionRequired); - } - - static void InlineAsmDiagHandler(const llvm::SMDiagnostic &SM,void *Context, - unsigned LocCookie) { - SourceLocation Loc = SourceLocation::getFromRawEncoding(LocCookie); - ((BackendConsumer*)Context)->InlineAsmDiagHandler2(SM, Loc); - } - - void InlineAsmDiagHandler2(const llvm::SMDiagnostic &, - SourceLocation LocCookie); - }; -} - -FunctionPassManager *BackendConsumer::getCodeGenPasses() const { - if (!CodeGenPasses) { - CodeGenPasses = new FunctionPassManager(&*TheModule); - CodeGenPasses->add(new TargetData(*TheTargetData)); - } - - return CodeGenPasses; -} - -PassManager *BackendConsumer::getPerModulePasses() const { - if (!PerModulePasses) { - PerModulePasses = new PassManager(); - PerModulePasses->add(new TargetData(*TheTargetData)); - } - - return PerModulePasses; -} - -FunctionPassManager *BackendConsumer::getPerFunctionPasses() const { - if (!PerFunctionPasses) { - PerFunctionPasses = new FunctionPassManager(&*TheModule); - PerFunctionPasses->add(new TargetData(*TheTargetData)); - } - - return PerFunctionPasses; -} - -bool BackendConsumer::AddEmitPasses() { - if (Action == Backend_EmitNothing) - return true; - - if (Action == Backend_EmitBC) { - getPerModulePasses()->add(createBitcodeWriterPass(FormattedOutStream)); - return true; - } - - if (Action == Backend_EmitLL) { - getPerModulePasses()->add(createPrintModulePass(&FormattedOutStream)); - return true; - } - - bool Fast = CodeGenOpts.OptimizationLevel == 0; - - // Create the TargetMachine for generating code. - std::string Error; - std::string Triple = TheModule->getTargetTriple(); - const llvm::Target *TheTarget = TargetRegistry::lookupTarget(Triple, Error); - if (!TheTarget) { - Diags.Report(diag::err_fe_unable_to_create_target) << Error; - return false; - } - - // FIXME: Expose these capabilities via actual APIs!!!! Aside from just - // being gross, this is also totally broken if we ever care about - // concurrency. - llvm::NoFramePointerElim = CodeGenOpts.DisableFPElim; - if (CodeGenOpts.FloatABI == "soft") - llvm::FloatABIType = llvm::FloatABI::Soft; - else if (CodeGenOpts.FloatABI == "hard") - llvm::FloatABIType = llvm::FloatABI::Hard; - else { - assert(CodeGenOpts.FloatABI.empty() && "Invalid float abi!"); - llvm::FloatABIType = llvm::FloatABI::Default; - } - NoZerosInBSS = CodeGenOpts.NoZeroInitializedInBSS; - llvm::UseSoftFloat = CodeGenOpts.SoftFloat; - UnwindTablesMandatory = CodeGenOpts.UnwindTables; - - TargetMachine::setAsmVerbosityDefault(CodeGenOpts.AsmVerbose); - - TargetMachine::setFunctionSections(CodeGenOpts.FunctionSections); - TargetMachine::setDataSections (CodeGenOpts.DataSections); - - // FIXME: Parse this earlier. - if (CodeGenOpts.RelocationModel == "static") { - TargetMachine::setRelocationModel(llvm::Reloc::Static); - } else if (CodeGenOpts.RelocationModel == "pic") { - TargetMachine::setRelocationModel(llvm::Reloc::PIC_); - } else { - assert(CodeGenOpts.RelocationModel == "dynamic-no-pic" && - "Invalid PIC model!"); - TargetMachine::setRelocationModel(llvm::Reloc::DynamicNoPIC); - } - // FIXME: Parse this earlier. - if (CodeGenOpts.CodeModel == "small") { - TargetMachine::setCodeModel(llvm::CodeModel::Small); - } else if (CodeGenOpts.CodeModel == "kernel") { - TargetMachine::setCodeModel(llvm::CodeModel::Kernel); - } else if (CodeGenOpts.CodeModel == "medium") { - TargetMachine::setCodeModel(llvm::CodeModel::Medium); - } else if (CodeGenOpts.CodeModel == "large") { - TargetMachine::setCodeModel(llvm::CodeModel::Large); - } else { - assert(CodeGenOpts.CodeModel.empty() && "Invalid code model!"); - TargetMachine::setCodeModel(llvm::CodeModel::Default); - } - - std::vector<const char *> BackendArgs; - BackendArgs.push_back("clang"); // Fake program name. - if (!CodeGenOpts.DebugPass.empty()) { - BackendArgs.push_back("-debug-pass"); - BackendArgs.push_back(CodeGenOpts.DebugPass.c_str()); - } - if (!CodeGenOpts.LimitFloatPrecision.empty()) { - BackendArgs.push_back("-limit-float-precision"); - BackendArgs.push_back(CodeGenOpts.LimitFloatPrecision.c_str()); - } - if (llvm::TimePassesIsEnabled) - BackendArgs.push_back("-time-passes"); - BackendArgs.push_back(0); - llvm::cl::ParseCommandLineOptions(BackendArgs.size() - 1, - const_cast<char **>(&BackendArgs[0])); - - std::string FeaturesStr; - if (TargetOpts.CPU.size() || TargetOpts.Features.size()) { - SubtargetFeatures Features; - Features.setCPU(TargetOpts.CPU); - for (std::vector<std::string>::const_iterator - it = TargetOpts.Features.begin(), - ie = TargetOpts.Features.end(); it != ie; ++it) - Features.AddFeature(*it); - FeaturesStr = Features.getString(); - } - TargetMachine *TM = TheTarget->createTargetMachine(Triple, FeaturesStr); - - if (CodeGenOpts.RelaxAll) - TM->setMCRelaxAll(true); - - // Set register scheduler & allocation policy. - RegisterScheduler::setDefault(createDefaultScheduler); - RegisterRegAlloc::setDefault(Fast ? createLocalRegisterAllocator : - createLinearScanRegisterAllocator); - - // Create the code generator passes. - FunctionPassManager *PM = getCodeGenPasses(); - CodeGenOpt::Level OptLevel = CodeGenOpt::Default; - - switch (CodeGenOpts.OptimizationLevel) { - default: break; - case 0: OptLevel = CodeGenOpt::None; break; - case 3: OptLevel = CodeGenOpt::Aggressive; break; - } - - // Normal mode, emit a .s or .o file by running the code generator. Note, - // this also adds codegenerator level optimization passes. - TargetMachine::CodeGenFileType CGFT = TargetMachine::CGFT_AssemblyFile; - if (Action == Backend_EmitObj) - CGFT = TargetMachine::CGFT_ObjectFile; - else if (Action == Backend_EmitMCNull) - CGFT = TargetMachine::CGFT_Null; - else - assert(Action == Backend_EmitAssembly && "Invalid action!"); - if (TM->addPassesToEmitFile(*PM, FormattedOutStream, CGFT, OptLevel, - /*DisableVerify=*/!CodeGenOpts.VerifyModule)) { - Diags.Report(diag::err_fe_unable_to_interface_with_target); - return false; - } - - return true; -} - -void BackendConsumer::CreatePasses() { - unsigned OptLevel = CodeGenOpts.OptimizationLevel; - CodeGenOptions::InliningMethod Inlining = CodeGenOpts.Inlining; - - // Handle disabling of LLVM optimization, where we want to preserve the - // internal module before any optimization. - if (CodeGenOpts.DisableLLVMOpts) { - OptLevel = 0; - Inlining = CodeGenOpts.NoInlining; - } - - // In -O0 if checking is disabled, we don't even have per-function passes. - if (CodeGenOpts.VerifyModule) - getPerFunctionPasses()->add(createVerifierPass()); - - // Assume that standard function passes aren't run for -O0. - if (OptLevel > 0) - llvm::createStandardFunctionPasses(getPerFunctionPasses(), OptLevel); - - llvm::Pass *InliningPass = 0; - switch (Inlining) { - case CodeGenOptions::NoInlining: break; - case CodeGenOptions::NormalInlining: { - // Set the inline threshold following llvm-gcc. - // - // FIXME: Derive these constants in a principled fashion. - unsigned Threshold = 225; - if (CodeGenOpts.OptimizeSize) - Threshold = 75; - else if (OptLevel > 2) - Threshold = 275; - InliningPass = createFunctionInliningPass(Threshold); - break; - } - case CodeGenOptions::OnlyAlwaysInlining: - InliningPass = createAlwaysInlinerPass(); // Respect always_inline - break; - } - - // For now we always create per module passes. - PassManager *PM = getPerModulePasses(); - llvm::createStandardModulePasses(PM, OptLevel, CodeGenOpts.OptimizeSize, - CodeGenOpts.UnitAtATime, - CodeGenOpts.UnrollLoops, - /*SimplifyLibCalls=*/!LangOpts.NoBuiltin, - /*HaveExceptions=*/true, - InliningPass); -} - -/// EmitAssembly - Handle interaction with LLVM backend to generate -/// actual machine code. -void BackendConsumer::EmitAssembly() { - // Silently ignore if we weren't initialized for some reason. - if (!TheModule || !TheTargetData) - return; - - TimeRegion Region(llvm::TimePassesIsEnabled ? &CodeGenerationTime : 0); - - // Make sure IR generation is happy with the module. This is - // released by the module provider. - Module *M = Gen->ReleaseModule(); - if (!M) { - // The module has been released by IR gen on failures, do not - // double free. - TheModule.take(); - return; - } - - assert(TheModule.get() == M && - "Unexpected module change during IR generation"); - - CreatePasses(); - if (!AddEmitPasses()) - return; - - // Run passes. For now we do all passes at once, but eventually we - // would like to have the option of streaming code generation. - - if (PerFunctionPasses) { - PrettyStackTraceString CrashInfo("Per-function optimization"); - - PerFunctionPasses->doInitialization(); - for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) - if (!I->isDeclaration()) - PerFunctionPasses->run(*I); - PerFunctionPasses->doFinalization(); - } - - if (PerModulePasses) { - PrettyStackTraceString CrashInfo("Per-module optimization passes"); - PerModulePasses->run(*M); - } - - if (CodeGenPasses) { - PrettyStackTraceString CrashInfo("Code generation"); - - // Install an inline asm handler so that diagnostics get printed through our - // diagnostics hooks. - LLVMContext &Ctx = TheModule->getContext(); - void *OldHandler = Ctx.getInlineAsmDiagnosticHandler(); - void *OldContext = Ctx.getInlineAsmDiagnosticContext(); - Ctx.setInlineAsmDiagnosticHandler((void*)(intptr_t)InlineAsmDiagHandler, - this); - - CodeGenPasses->doInitialization(); - for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I) - if (!I->isDeclaration()) - CodeGenPasses->run(*I); - CodeGenPasses->doFinalization(); - - Ctx.setInlineAsmDiagnosticHandler(OldHandler, OldContext); - } -} - -/// ConvertBackendLocation - Convert a location in a temporary llvm::SourceMgr -/// buffer to be a valid FullSourceLoc. -static FullSourceLoc ConvertBackendLocation(const llvm::SMDiagnostic &D, - SourceManager &CSM) { - // Get both the clang and llvm source managers. The location is relative to - // a memory buffer that the LLVM Source Manager is handling, we need to add - // a copy to the Clang source manager. - const llvm::SourceMgr &LSM = *D.getSourceMgr(); - - // We need to copy the underlying LLVM memory buffer because llvm::SourceMgr - // already owns its one and clang::SourceManager wants to own its one. - const MemoryBuffer *LBuf = - LSM.getMemoryBuffer(LSM.FindBufferContainingLoc(D.getLoc())); - - // Create the copy and transfer ownership to clang::SourceManager. - llvm::MemoryBuffer *CBuf = - llvm::MemoryBuffer::getMemBufferCopy(LBuf->getBuffer(), - LBuf->getBufferIdentifier()); - FileID FID = CSM.createFileIDForMemBuffer(CBuf); - - // Translate the offset into the file. - unsigned Offset = D.getLoc().getPointer() - LBuf->getBufferStart(); - SourceLocation NewLoc = - CSM.getLocForStartOfFile(FID).getFileLocWithOffset(Offset); - return FullSourceLoc(NewLoc, CSM); -} - - -/// InlineAsmDiagHandler2 - This function is invoked when the backend hits an -/// error parsing inline asm. The SMDiagnostic indicates the error relative to -/// the temporary memory buffer that the inline asm parser has set up. -void BackendConsumer::InlineAsmDiagHandler2(const llvm::SMDiagnostic &D, - SourceLocation LocCookie) { - // There are a couple of different kinds of errors we could get here. First, - // we re-format the SMDiagnostic in terms of a clang diagnostic. - - // Strip "error: " off the start of the message string. - llvm::StringRef Message = D.getMessage(); - if (Message.startswith("error: ")) - Message = Message.substr(7); - - // There are two cases: the SMDiagnostic could have a inline asm source - // location or it might not. If it does, translate the location. - FullSourceLoc Loc; - if (D.getLoc() != SMLoc()) - Loc = ConvertBackendLocation(D, Context->getSourceManager()); - Diags.Report(Loc, diag::err_fe_inline_asm).AddString(Message); - - // This could be a problem with no clang-level source location information. - // In this case, LocCookie is invalid. If there is source level information, - // print an "generated from" note. - if (LocCookie.isValid()) - Diags.Report(FullSourceLoc(LocCookie, Context->getSourceManager()), - diag::note_fe_inline_asm_here); -} - -// - -CodeGenAction::CodeGenAction(unsigned _Act) : Act(_Act) {} - -CodeGenAction::~CodeGenAction() {} - -void CodeGenAction::EndSourceFileAction() { - // If the consumer creation failed, do nothing. - if (!getCompilerInstance().hasASTConsumer()) - return; - - // Steal the module from the consumer. - BackendConsumer *Consumer = static_cast<BackendConsumer*>( - &getCompilerInstance().getASTConsumer()); - - TheModule.reset(Consumer->takeModule()); -} - -llvm::Module *CodeGenAction::takeModule() { - return TheModule.take(); -} - -ASTConsumer *CodeGenAction::CreateASTConsumer(CompilerInstance &CI, - llvm::StringRef InFile) { - BackendAction BA = static_cast<BackendAction>(Act); - llvm::OwningPtr<llvm::raw_ostream> OS; - switch (BA) { - case Backend_EmitAssembly: - OS.reset(CI.createDefaultOutputFile(false, InFile, "s")); - break; - case Backend_EmitLL: - OS.reset(CI.createDefaultOutputFile(false, InFile, "ll")); - break; - case Backend_EmitBC: - OS.reset(CI.createDefaultOutputFile(true, InFile, "bc")); - break; - case Backend_EmitNothing: - break; - case Backend_EmitMCNull: - case Backend_EmitObj: - OS.reset(CI.createDefaultOutputFile(true, InFile, "o")); - break; - } - if (BA != Backend_EmitNothing && !OS) - return 0; - - return new BackendConsumer(BA, CI.getDiagnostics(), CI.getLangOpts(), - CI.getCodeGenOpts(), CI.getTargetOpts(), - CI.getFrontendOpts().ShowTimers, InFile, OS.take(), - CI.getLLVMContext()); -} - -EmitAssemblyAction::EmitAssemblyAction() - : CodeGenAction(Backend_EmitAssembly) {} - -EmitBCAction::EmitBCAction() : CodeGenAction(Backend_EmitBC) {} - -EmitLLVMAction::EmitLLVMAction() : CodeGenAction(Backend_EmitLL) {} - -EmitLLVMOnlyAction::EmitLLVMOnlyAction() : CodeGenAction(Backend_EmitNothing) {} - -EmitCodeGenOnlyAction::EmitCodeGenOnlyAction() : CodeGenAction(Backend_EmitMCNull) {} - -EmitObjAction::EmitObjAction() : CodeGenAction(Backend_EmitObj) {} diff --git a/contrib/llvm/tools/clang/lib/Frontend/CompilerInstance.cpp b/contrib/llvm/tools/clang/lib/Frontend/CompilerInstance.cpp index 2b25168..5037c83 100644 --- a/contrib/llvm/tools/clang/lib/Frontend/CompilerInstance.cpp +++ b/contrib/llvm/tools/clang/lib/Frontend/CompilerInstance.cpp @@ -37,7 +37,7 @@ using namespace clang; CompilerInstance::CompilerInstance() - : Invocation(new CompilerInvocation()) { + : Invocation(new CompilerInvocation()), Reader(0) { } CompilerInstance::~CompilerInstance() { @@ -255,6 +255,8 @@ void CompilerInstance::createPCHExternalASTSource(llvm::StringRef Path) { llvm::OwningPtr<ExternalASTSource> Source; Source.reset(createPCHExternalASTSource(Path, getHeaderSearchOpts().Sysroot, getPreprocessor(), getASTContext())); + // Remember the PCHReader, but in a non-owning way. + Reader = static_cast<PCHReader*>(Source.get()); getASTContext().setExternalSource(Source); } @@ -442,7 +444,7 @@ bool CompilerInstance::InitializeSourceManager(llvm::StringRef InputFile, } } else { llvm::MemoryBuffer *SB = llvm::MemoryBuffer::getSTDIN(); - SourceMgr.createMainFileIDForMemBuffer(SB); + if (SB) SourceMgr.createMainFileIDForMemBuffer(SB); if (SourceMgr.getMainFileID().isInvalid()) { Diags.Report(diag::err_fe_error_reading_stdin); return false; @@ -489,27 +491,11 @@ bool CompilerInstance::ExecuteAction(FrontendAction &Act) { for (unsigned i = 0, e = getFrontendOpts().Inputs.size(); i != e; ++i) { const std::string &InFile = getFrontendOpts().Inputs[i].second; - // If we aren't using an AST file, setup the file and source managers and - // the preprocessor. - bool IsAST = getFrontendOpts().Inputs[i].first == FrontendOptions::IK_AST; - if (!IsAST) { - if (!i) { - // Create a file manager object to provide access to and cache the - // filesystem. - createFileManager(); - - // Create the source manager. - createSourceManager(); - } else { - // Reset the ID tables if we are reusing the SourceManager. - getSourceManager().clearIDTables(); - } - - // Create the preprocessor. - createPreprocessor(); - } + // Reset the ID tables if we are reusing the SourceManager. + if (hasSourceManager()) + getSourceManager().clearIDTables(); - if (Act.BeginSourceFile(*this, InFile, IsAST)) { + if (Act.BeginSourceFile(*this, InFile, getFrontendOpts().Inputs[i].first)) { Act.Execute(); Act.EndSourceFile(); } @@ -530,7 +516,7 @@ bool CompilerInstance::ExecuteAction(FrontendAction &Act) { OS << " generated.\n"; } - if (getFrontendOpts().ShowStats) { + if (getFrontendOpts().ShowStats && hasFileManager()) { getFileManager().PrintStats(); OS << "\n"; } diff --git a/contrib/llvm/tools/clang/lib/Frontend/CompilerInvocation.cpp b/contrib/llvm/tools/clang/lib/Frontend/CompilerInvocation.cpp index ff372e1..53debdb 100644 --- a/contrib/llvm/tools/clang/lib/Frontend/CompilerInvocation.cpp +++ b/contrib/llvm/tools/clang/lib/Frontend/CompilerInvocation.cpp @@ -112,6 +112,8 @@ static void AnalyzerOptsToArgs(const AnalyzerOptions &Opts, Res.push_back("-analyzer-experimental-checks"); if (Opts.EnableExperimentalInternalChecks) Res.push_back("-analyzer-experimental-internal-checks"); + if (Opts.EnableIdempotentOperationChecker) + Res.push_back("-analyzer-idempotent-operation"); } static void CodeGenOptsToArgs(const CodeGenOptions &Opts, @@ -132,6 +134,8 @@ static void CodeGenOptsToArgs(const CodeGenOptions &Opts, Res.push_back("-fno-common"); if (Opts.NoImplicitFloat) Res.push_back("-no-implicit-float"); + if (Opts.OmitLeafFramePointer) + Res.push_back("-momit-leaf-frame-pointer"); if (Opts.OptimizeSize) { assert(Opts.OptimizationLevel == 2 && "Invalid options!"); Res.push_back("-Os"); @@ -280,20 +284,21 @@ static void DiagnosticOptsToArgs(const DiagnosticOptions &Opts, Res.push_back("-W" + Opts.Warnings[i]); } -static const char *getInputKindName(FrontendOptions::InputKind Kind) { +static const char *getInputKindName(InputKind Kind) { switch (Kind) { - case FrontendOptions::IK_None: break; - case FrontendOptions::IK_AST: return "ast"; - case FrontendOptions::IK_Asm: return "assembler-with-cpp"; - case FrontendOptions::IK_C: return "c"; - case FrontendOptions::IK_CXX: return "c++"; - case FrontendOptions::IK_ObjC: return "objective-c"; - case FrontendOptions::IK_ObjCXX: return "objective-c++"; - case FrontendOptions::IK_OpenCL: return "cl"; - case FrontendOptions::IK_PreprocessedC: return "cpp-output"; - case FrontendOptions::IK_PreprocessedCXX: return "c++-cpp-output"; - case FrontendOptions::IK_PreprocessedObjC: return "objective-c-cpp-output"; - case FrontendOptions::IK_PreprocessedObjCXX:return "objective-c++-cpp-output"; + case IK_None: break; + case IK_AST: return "ast"; + case IK_Asm: return "assembler-with-cpp"; + case IK_C: return "c"; + case IK_CXX: return "c++"; + case IK_LLVM_IR: return "ir"; + case IK_ObjC: return "objective-c"; + case IK_ObjCXX: return "objective-c++"; + case IK_OpenCL: return "cl"; + case IK_PreprocessedC: return "cpp-output"; + case IK_PreprocessedCXX: return "c++-cpp-output"; + case IK_PreprocessedObjC: return "objective-c-cpp-output"; + case IK_PreprocessedObjCXX:return "objective-c++-cpp-output"; } llvm_unreachable("Unexpected language kind!"); @@ -348,6 +353,8 @@ static void FrontendOptsToArgs(const FrontendOptions &Opts, Res.push_back("-disable-free"); if (Opts.RelocatablePCH) Res.push_back("-relocatable-pch"); + if (Opts.ChainedPCH) + Res.push_back("-chained-pch"); if (Opts.ShowHelp) Res.push_back("-help"); if (Opts.ShowMacrosInCodeCompletion) @@ -396,6 +403,10 @@ static void FrontendOptsToArgs(const FrontendOptions &Opts, if (!Opts.ActionName.empty()) { Res.push_back("-plugin"); Res.push_back(Opts.ActionName); + for(unsigned i = 0, e = Opts.PluginArgs.size(); i != e; ++i) { + Res.push_back("-plugin-arg-" + Opts.ActionName); + Res.push_back(Opts.PluginArgs[i]); + } } for (unsigned i = 0, e = Opts.Plugins.size(); i != e; ++i) { Res.push_back("-load"); @@ -546,8 +557,11 @@ static void LangOptsToArgs(const LangOptions &Opts, Res.push_back("-femit-all-decls"); if (Opts.MathErrno) Res.push_back("-fmath-errno"); - if (Opts.OverflowChecking) - Res.push_back("-ftrapv"); + switch (Opts.getSignedOverflowBehavior()) { + case LangOptions::SOB_Undefined: break; + case LangOptions::SOB_Defined: Res.push_back("-fwrapv"); break; + case LangOptions::SOB_Trapping: Res.push_back("-ftrapv"); break; + } if (Opts.HeinousExtensions) Res.push_back("-fheinous-gnu-extensions"); // Optimize is implicit. @@ -596,6 +610,9 @@ static void LangOptsToArgs(const LangOptions &Opts, Res.push_back("protected"); } } + if (Opts.InlineVisibilityHidden) + Res.push_back("-fvisibility-inlines-hidden"); + if (Opts.getStackProtectorMode() != 0) { Res.push_back("-stack-protector"); Res.push_back(llvm::utostr(Opts.getStackProtectorMode())); @@ -681,6 +698,8 @@ static void TargetOptsToArgs(const TargetOptions &Opts, Res.push_back("-target-abi"); Res.push_back(Opts.ABI); } + Res.push_back("-cxx-abi"); + Res.push_back(Opts.CXXABI); for (unsigned i = 0, e = Opts.Features.size(); i != e; ++i) { Res.push_back("-target-feature"); Res.push_back(Opts.Features[i]); @@ -728,7 +747,7 @@ static void ParseAnalyzerArgs(AnalyzerOptions &Opts, ArgList &Args, // FIXME: Error handling. if (Value == NumStores) Diags.Report(diag::err_drv_invalid_value) - << Args.getLastArg(OPT_O)->getAsString(Args) << Name; + << A->getAsString(Args) << Name; else Opts.AnalysisStoreOpt = Value; } @@ -743,7 +762,7 @@ static void ParseAnalyzerArgs(AnalyzerOptions &Opts, ArgList &Args, // FIXME: Error handling. if (Value == NumConstraints) Diags.Report(diag::err_drv_invalid_value) - << Args.getLastArg(OPT_O)->getAsString(Args) << Name; + << A->getAsString(Args) << Name; else Opts.AnalysisConstraintsOpt = Value; } @@ -758,7 +777,7 @@ static void ParseAnalyzerArgs(AnalyzerOptions &Opts, ArgList &Args, // FIXME: Error handling. if (Value == NUM_ANALYSIS_DIAG_CLIENTS) Diags.Report(diag::err_drv_invalid_value) - << Args.getLastArg(OPT_O)->getAsString(Args) << Name; + << A->getAsString(Args) << Name; else Opts.AnalysisDiagOpt = Value; } @@ -775,6 +794,8 @@ static void ParseAnalyzerArgs(AnalyzerOptions &Opts, ArgList &Args, Opts.EnableExperimentalChecks = Args.hasArg(OPT_analyzer_experimental_checks); Opts.EnableExperimentalInternalChecks = Args.hasArg(OPT_analyzer_experimental_internal_checks); + Opts.EnableIdempotentOperationChecker = + Args.hasArg(OPT_analyzer_idempotent_operation); Opts.TrimGraph = Args.hasArg(OPT_trim_egraph); Opts.MaxNodes = Args.getLastArgIntValue(OPT_analyzer_max_nodes, 150000,Diags); Opts.MaxLoop = Args.getLastArgIntValue(OPT_analyzer_max_loop, 3, Diags); @@ -808,6 +829,8 @@ static void ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args, Opts.NoCommon = Args.hasArg(OPT_fno_common); Opts.NoImplicitFloat = Args.hasArg(OPT_no_implicit_float); Opts.OptimizeSize = Args.hasArg(OPT_Os); + Opts.SimplifyLibCalls = !(Args.hasArg(OPT_fno_builtin) || + Args.hasArg(OPT_ffreestanding)); Opts.UnrollLoops = (Opts.OptimizationLevel > 1 && !Opts.OptimizeSize); Opts.AsmVerbose = Args.hasArg(OPT_masm_verbose); @@ -820,6 +843,7 @@ static void ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args, Opts.LimitFloatPrecision = Args.getLastArgValue(OPT_mlimit_float_precision); Opts.NoZeroInitializedInBSS = Args.hasArg(OPT_mno_zero_initialized_in_bss); Opts.RelaxAll = Args.hasArg(OPT_mrelax_all); + Opts.OmitLeafFramePointer = Args.hasArg(OPT_momit_leaf_frame_pointer); Opts.SoftFloat = Args.hasArg(OPT_msoft_float); Opts.UnwindTables = Args.hasArg(OPT_munwind_tables); Opts.RelocationModel = Args.getLastArgValue(OPT_mrelocation_model, "pic"); @@ -830,6 +854,8 @@ static void ParseCodeGenArgs(CodeGenOptions &Opts, ArgList &Args, Opts.MainFileName = Args.getLastArgValue(OPT_main_file_name); Opts.VerifyModule = !Args.hasArg(OPT_disable_llvm_verifier); + Opts.InstrumentFunctions = Args.hasArg(OPT_finstrument_functions); + if (Arg *A = Args.getLastArg(OPT_fobjc_dispatch_method_EQ)) { llvm::StringRef Name = A->getValue(Args); unsigned Method = llvm::StringSwitch<unsigned>(Name) @@ -866,7 +892,18 @@ static void ParseDiagnosticArgs(DiagnosticOptions &Opts, ArgList &Args, Opts.ShowFixits = !Args.hasArg(OPT_fno_diagnostics_fixit_info); Opts.ShowLocation = !Args.hasArg(OPT_fno_show_source_location); Opts.ShowOptionNames = Args.hasArg(OPT_fdiagnostics_show_option); - + + llvm::StringRef ShowOverloads = + Args.getLastArgValue(OPT_fshow_overloads_EQ, "all"); + if (ShowOverloads == "best") + Opts.ShowOverloads = Diagnostic::Ovl_Best; + else if (ShowOverloads == "all") + Opts.ShowOverloads = Diagnostic::Ovl_All; + else + Diags.Report(diag::err_drv_invalid_value) + << Args.getLastArg(OPT_fshow_overloads_EQ)->getAsString(Args) + << ShowOverloads; + llvm::StringRef ShowCategory = Args.getLastArgValue(OPT_fdiagnostics_show_category, "none"); if (ShowCategory == "none") @@ -903,8 +940,8 @@ static void ParseDiagnosticArgs(DiagnosticOptions &Opts, ArgList &Args, Opts.Warnings = Args.getAllArgValues(OPT_W); } -static FrontendOptions::InputKind -ParseFrontendArgs(FrontendOptions &Opts, ArgList &Args, Diagnostic &Diags) { +static InputKind ParseFrontendArgs(FrontendOptions &Opts, ArgList &Args, + Diagnostic &Diags) { using namespace cc1options; Opts.ProgramAction = frontend::ParseSyntaxOnly; if (const Arg *A = Args.getLastArg(OPT_Action_Group)) { @@ -972,9 +1009,17 @@ ParseFrontendArgs(FrontendOptions &Opts, ArgList &Args, Diagnostic &Diags) { Opts.ProgramAction = frontend::RunPreprocessorOnly; break; } } - if (const Arg *A = Args.getLastArg(OPT_plugin)) { + + if (const Arg* A = Args.getLastArg(OPT_plugin)) { + Opts.Plugins.push_back(A->getValue(Args,0)); Opts.ProgramAction = frontend::PluginAction; Opts.ActionName = A->getValue(Args); + + for (arg_iterator it = Args.filtered_begin(OPT_plugin_arg), + end = Args.filtered_end(); it != end; ++it) { + if ((*it)->getValue(Args, 0) == Opts.ActionName) + Opts.PluginArgs.push_back((*it)->getValue(Args, 1)); + } } if (const Arg *A = Args.getLastArg(OPT_code_completion_at)) { @@ -991,6 +1036,7 @@ ParseFrontendArgs(FrontendOptions &Opts, ArgList &Args, Diagnostic &Diags) { Opts.OutputFile = Args.getLastArgValue(OPT_o); Opts.Plugins = Args.getAllArgValues(OPT_load); Opts.RelocatablePCH = Args.hasArg(OPT_relocatable_pch); + Opts.ChainedPCH = Args.hasArg(OPT_chained_pch); Opts.ShowHelp = Args.hasArg(OPT_help); Opts.ShowMacrosInCodeCompletion = Args.hasArg(OPT_code_completion_macros); Opts.ShowCodePatternsInCodeCompletion @@ -1002,28 +1048,29 @@ ParseFrontendArgs(FrontendOptions &Opts, ArgList &Args, Diagnostic &Diags) { Opts.ASTMergeFiles = Args.getAllArgValues(OPT_ast_merge); Opts.LLVMArgs = Args.getAllArgValues(OPT_mllvm); - FrontendOptions::InputKind DashX = FrontendOptions::IK_None; + InputKind DashX = IK_None; if (const Arg *A = Args.getLastArg(OPT_x)) { - DashX = llvm::StringSwitch<FrontendOptions::InputKind>(A->getValue(Args)) - .Case("c", FrontendOptions::IK_C) - .Case("cl", FrontendOptions::IK_OpenCL) - .Case("c", FrontendOptions::IK_C) - .Case("cl", FrontendOptions::IK_OpenCL) - .Case("c++", FrontendOptions::IK_CXX) - .Case("objective-c", FrontendOptions::IK_ObjC) - .Case("objective-c++", FrontendOptions::IK_ObjCXX) - .Case("cpp-output", FrontendOptions::IK_PreprocessedC) - .Case("assembler-with-cpp", FrontendOptions::IK_Asm) - .Case("c++-cpp-output", FrontendOptions::IK_PreprocessedCXX) - .Case("objective-c-cpp-output", FrontendOptions::IK_PreprocessedObjC) - .Case("objective-c++-cpp-output", FrontendOptions::IK_PreprocessedObjCXX) - .Case("c-header", FrontendOptions::IK_C) - .Case("objective-c-header", FrontendOptions::IK_ObjC) - .Case("c++-header", FrontendOptions::IK_CXX) - .Case("objective-c++-header", FrontendOptions::IK_ObjCXX) - .Case("ast", FrontendOptions::IK_AST) - .Default(FrontendOptions::IK_None); - if (DashX == FrontendOptions::IK_None) + DashX = llvm::StringSwitch<InputKind>(A->getValue(Args)) + .Case("c", IK_C) + .Case("cl", IK_OpenCL) + .Case("c", IK_C) + .Case("cl", IK_OpenCL) + .Case("c++", IK_CXX) + .Case("objective-c", IK_ObjC) + .Case("objective-c++", IK_ObjCXX) + .Case("cpp-output", IK_PreprocessedC) + .Case("assembler-with-cpp", IK_Asm) + .Case("c++-cpp-output", IK_PreprocessedCXX) + .Case("objective-c-cpp-output", IK_PreprocessedObjC) + .Case("objective-c++-cpp-output", IK_PreprocessedObjCXX) + .Case("c-header", IK_C) + .Case("objective-c-header", IK_ObjC) + .Case("c++-header", IK_CXX) + .Case("objective-c++-header", IK_ObjCXX) + .Case("ast", IK_AST) + .Case("ir", IK_LLVM_IR) + .Default(IK_None); + if (DashX == IK_None) Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args) << A->getValue(Args); } @@ -1034,8 +1081,8 @@ ParseFrontendArgs(FrontendOptions &Opts, ArgList &Args, Diagnostic &Diags) { if (Inputs.empty()) Inputs.push_back("-"); for (unsigned i = 0, e = Inputs.size(); i != e; ++i) { - FrontendOptions::InputKind IK = DashX; - if (IK == FrontendOptions::IK_None) { + InputKind IK = DashX; + if (IK == IK_None) { IK = FrontendOptions::getInputKindForExtension( llvm::StringRef(Inputs[i]).rsplit('.').second); // FIXME: Remove this hack. @@ -1077,51 +1124,51 @@ static void ParseHeaderSearchArgs(HeaderSearchOptions &Opts, ArgList &Args) { // Add -I... and -F... options in order. for (arg_iterator it = Args.filtered_begin(OPT_I, OPT_F), ie = Args.filtered_end(); it != ie; ++it) - Opts.AddPath(it->getValue(Args), frontend::Angled, true, - /*IsFramework=*/ it->getOption().matches(OPT_F)); + Opts.AddPath((*it)->getValue(Args), frontend::Angled, true, + /*IsFramework=*/ (*it)->getOption().matches(OPT_F)); // Add -iprefix/-iwith-prefix/-iwithprefixbefore options. llvm::StringRef Prefix = ""; // FIXME: This isn't the correct default prefix. for (arg_iterator it = Args.filtered_begin(OPT_iprefix, OPT_iwithprefix, OPT_iwithprefixbefore), ie = Args.filtered_end(); it != ie; ++it) { - if (it->getOption().matches(OPT_iprefix)) - Prefix = it->getValue(Args); - else if (it->getOption().matches(OPT_iwithprefix)) - Opts.AddPath(Prefix.str() + it->getValue(Args), + const Arg *A = *it; + if (A->getOption().matches(OPT_iprefix)) + Prefix = A->getValue(Args); + else if (A->getOption().matches(OPT_iwithprefix)) + Opts.AddPath(Prefix.str() + A->getValue(Args), frontend::System, false, false); else - Opts.AddPath(Prefix.str() + it->getValue(Args), + Opts.AddPath(Prefix.str() + A->getValue(Args), frontend::Angled, false, false); } for (arg_iterator it = Args.filtered_begin(OPT_idirafter), ie = Args.filtered_end(); it != ie; ++it) - Opts.AddPath(it->getValue(Args), frontend::After, true, false); + Opts.AddPath((*it)->getValue(Args), frontend::After, true, false); for (arg_iterator it = Args.filtered_begin(OPT_iquote), ie = Args.filtered_end(); it != ie; ++it) - Opts.AddPath(it->getValue(Args), frontend::Quoted, true, false); + Opts.AddPath((*it)->getValue(Args), frontend::Quoted, true, false); for (arg_iterator it = Args.filtered_begin(OPT_isystem), ie = Args.filtered_end(); it != ie; ++it) - Opts.AddPath(it->getValue(Args), frontend::System, true, false); + Opts.AddPath((*it)->getValue(Args), frontend::System, true, false); // FIXME: Need options for the various environment variables! } -static void ParseLangArgs(LangOptions &Opts, ArgList &Args, - FrontendOptions::InputKind IK, +static void ParseLangArgs(LangOptions &Opts, ArgList &Args, InputKind IK, Diagnostic &Diags) { // FIXME: Cleanup per-file based stuff. // Set some properties which depend soley on the input kind; it would be nice // to move these to the language standard, and have the driver resolve the // input kind + language standard. - if (IK == FrontendOptions::IK_Asm) { + if (IK == IK_Asm) { Opts.AsmPreprocessor = 1; - } else if (IK == FrontendOptions::IK_ObjC || - IK == FrontendOptions::IK_ObjCXX || - IK == FrontendOptions::IK_PreprocessedObjC || - IK == FrontendOptions::IK_PreprocessedObjCXX) { + } else if (IK == IK_ObjC || + IK == IK_ObjCXX || + IK == IK_PreprocessedObjC || + IK == IK_PreprocessedObjCXX) { Opts.ObjC1 = Opts.ObjC2 = 1; } @@ -1140,23 +1187,24 @@ static void ParseLangArgs(LangOptions &Opts, ArgList &Args, if (LangStd == LangStandard::lang_unspecified) { // Based on the base language, pick one. switch (IK) { - case FrontendOptions::IK_None: - case FrontendOptions::IK_AST: + case IK_None: + case IK_AST: + case IK_LLVM_IR: assert(0 && "Invalid input kind!"); - case FrontendOptions::IK_OpenCL: + case IK_OpenCL: LangStd = LangStandard::lang_opencl; break; - case FrontendOptions::IK_Asm: - case FrontendOptions::IK_C: - case FrontendOptions::IK_PreprocessedC: - case FrontendOptions::IK_ObjC: - case FrontendOptions::IK_PreprocessedObjC: + case IK_Asm: + case IK_C: + case IK_PreprocessedC: + case IK_ObjC: + case IK_PreprocessedObjC: LangStd = LangStandard::lang_gnu99; break; - case FrontendOptions::IK_CXX: - case FrontendOptions::IK_PreprocessedCXX: - case FrontendOptions::IK_ObjCXX: - case FrontendOptions::IK_PreprocessedObjCXX: + case IK_CXX: + case IK_PreprocessedCXX: + case IK_ObjCXX: + case IK_PreprocessedObjCXX: LangStd = LangStandard::lang_gnucxx98; break; } @@ -1222,7 +1270,13 @@ static void ParseLangArgs(LangOptions &Opts, ArgList &Args, Diags.Report(diag::err_drv_invalid_value) << Args.getLastArg(OPT_fvisibility)->getAsString(Args) << Vis; - Opts.OverflowChecking = Args.hasArg(OPT_ftrapv); + if (Args.hasArg(OPT_fvisibility_inlines_hidden)) + Opts.InlineVisibilityHidden = 1; + + if (Args.hasArg(OPT_ftrapv)) + Opts.setSignedOverflowBehavior(LangOptions::SOB_Trapping); + else if (Args.hasArg(OPT_fwrapv)) + Opts.setSignedOverflowBehavior(LangOptions::SOB_Defined); // Mimicing gcc's behavior, trigraphs are only enabled if -trigraphs // is specified, or -std is set to a conforming mode. @@ -1270,6 +1324,7 @@ static void ParseLangArgs(LangOptions &Opts, ArgList &Args, Opts.Static = Args.hasArg(OPT_static_define); Opts.DumpRecordLayouts = Args.hasArg(OPT_fdump_record_layouts); Opts.DumpVTableLayouts = Args.hasArg(OPT_fdump_vtable_layouts); + Opts.SpellChecking = !Args.hasArg(OPT_fno_spell_checking); Opts.NoBitFieldTypeAlign = Args.hasArg(OPT_fno_bitfield_type_align); Opts.OptimizeSize = 0; @@ -1311,10 +1366,10 @@ static void ParsePreprocessorArgs(PreprocessorOptions &Opts, ArgList &Args, // Add macros from the command line. for (arg_iterator it = Args.filtered_begin(OPT_D, OPT_U), ie = Args.filtered_end(); it != ie; ++it) { - if (it->getOption().matches(OPT_D)) - Opts.addMacroDef(it->getValue(Args)); + if ((*it)->getOption().matches(OPT_D)) + Opts.addMacroDef((*it)->getValue(Args)); else - Opts.addMacroUndef(it->getValue(Args)); + Opts.addMacroUndef((*it)->getValue(Args)); } Opts.MacroIncludes = Args.getAllArgValues(OPT_imacros); @@ -1323,16 +1378,17 @@ static void ParsePreprocessorArgs(PreprocessorOptions &Opts, ArgList &Args, for (arg_iterator it = Args.filtered_begin(OPT_include, OPT_include_pch, OPT_include_pth), ie = Args.filtered_end(); it != ie; ++it) { + const Arg *A = *it; // PCH is handled specially, we need to extra the original include path. - if (it->getOption().matches(OPT_include_pch)) { + if (A->getOption().matches(OPT_include_pch)) { std::string OriginalFile = - PCHReader::getOriginalSourceFile(it->getValue(Args), Diags); + PCHReader::getOriginalSourceFile(A->getValue(Args), Diags); if (OriginalFile.empty()) continue; Opts.Includes.push_back(OriginalFile); } else - Opts.Includes.push_back(it->getValue(Args)); + Opts.Includes.push_back(A->getValue(Args)); } // Include 'altivec.h' if -faltivec option present @@ -1341,11 +1397,12 @@ static void ParsePreprocessorArgs(PreprocessorOptions &Opts, ArgList &Args, for (arg_iterator it = Args.filtered_begin(OPT_remap_file), ie = Args.filtered_end(); it != ie; ++it) { + const Arg *A = *it; std::pair<llvm::StringRef,llvm::StringRef> Split = - llvm::StringRef(it->getValue(Args)).split(';'); + llvm::StringRef(A->getValue(Args)).split(';'); if (Split.second.empty()) { - Diags.Report(diag::err_drv_invalid_remap_file) << it->getAsString(Args); + Diags.Report(diag::err_drv_invalid_remap_file) << A->getAsString(Args); continue; } @@ -1366,6 +1423,7 @@ static void ParsePreprocessorOutputArgs(PreprocessorOutputOptions &Opts, static void ParseTargetArgs(TargetOptions &Opts, ArgList &Args) { using namespace cc1options; Opts.ABI = Args.getLastArgValue(OPT_target_abi); + Opts.CXXABI = Args.getLastArgValue(OPT_cxx_abi); Opts.CPU = Args.getLastArgValue(OPT_target_cpu); Opts.Triple = Args.getLastArgValue(OPT_triple); Opts.Features = Args.getAllArgValues(OPT_target_feature); @@ -1373,6 +1431,10 @@ static void ParseTargetArgs(TargetOptions &Opts, ArgList &Args) { // Use the host triple if unspecified. if (Opts.Triple.empty()) Opts.Triple = llvm::sys::getHostTriple(); + + // Use the Itanium C++ ABI if unspecified. + if (Opts.CXXABI.empty()) + Opts.CXXABI = "itanium"; } // @@ -1395,16 +1457,15 @@ void CompilerInvocation::CreateFromArgs(CompilerInvocation &Res, // Issue errors on unknown arguments. for (arg_iterator it = Args->filtered_begin(OPT_UNKNOWN), ie = Args->filtered_end(); it != ie; ++it) - Diags.Report(diag::err_drv_unknown_argument) << it->getAsString(*Args); + Diags.Report(diag::err_drv_unknown_argument) << (*it)->getAsString(*Args); ParseAnalyzerArgs(Res.getAnalyzerOpts(), *Args, Diags); ParseCodeGenArgs(Res.getCodeGenOpts(), *Args, Diags); ParseDependencyOutputArgs(Res.getDependencyOutputOpts(), *Args); ParseDiagnosticArgs(Res.getDiagnosticOpts(), *Args, Diags); - FrontendOptions::InputKind DashX = - ParseFrontendArgs(Res.getFrontendOpts(), *Args, Diags); + InputKind DashX = ParseFrontendArgs(Res.getFrontendOpts(), *Args, Diags); ParseHeaderSearchArgs(Res.getHeaderSearchOpts(), *Args); - if (DashX != FrontendOptions::IK_AST) + if (DashX != IK_AST && DashX != IK_LLVM_IR) ParseLangArgs(Res.getLangOpts(), *Args, DashX, Diags); ParsePreprocessorArgs(Res.getPreprocessorOpts(), *Args, Diags); ParsePreprocessorOutputArgs(Res.getPreprocessorOutputOpts(), *Args); diff --git a/contrib/llvm/tools/clang/lib/Frontend/FixItRewriter.cpp b/contrib/llvm/tools/clang/lib/Frontend/FixItRewriter.cpp deleted file mode 100644 index 7c9a566..0000000 --- a/contrib/llvm/tools/clang/lib/Frontend/FixItRewriter.cpp +++ /dev/null @@ -1,167 +0,0 @@ -//===--- FixItRewriter.cpp - Fix-It Rewriter Diagnostic Client --*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This is a diagnostic client adaptor that performs rewrites as -// suggested by code modification hints attached to diagnostics. It -// then forwards any diagnostics to the adapted diagnostic client. -// -//===----------------------------------------------------------------------===// - -#include "clang/Frontend/FixItRewriter.h" -#include "clang/Basic/FileManager.h" -#include "clang/Basic/SourceLocation.h" -#include "clang/Basic/SourceManager.h" -#include "clang/Frontend/FrontendDiagnostic.h" -#include "llvm/Support/raw_ostream.h" -#include "llvm/System/Path.h" -#include "llvm/ADT/OwningPtr.h" -#include <cstdio> - -using namespace clang; - -FixItRewriter::FixItRewriter(Diagnostic &Diags, SourceManager &SourceMgr, - const LangOptions &LangOpts, - FixItPathRewriter *PathRewriter) - : Diags(Diags), - Rewrite(SourceMgr, LangOpts), - PathRewriter(PathRewriter), - NumFailures(0) { - Client = Diags.getClient(); - Diags.setClient(this); -} - -FixItRewriter::~FixItRewriter() { - Diags.setClient(Client); -} - -bool FixItRewriter::WriteFixedFile(FileID ID, llvm::raw_ostream &OS) { - const RewriteBuffer *RewriteBuf = Rewrite.getRewriteBufferFor(ID); - if (!RewriteBuf) return true; - RewriteBuf->write(OS); - OS.flush(); - return false; -} - -bool FixItRewriter::WriteFixedFiles() { - if (NumFailures > 0) { - Diag(FullSourceLoc(), diag::warn_fixit_no_changes); - return true; - } - - for (iterator I = buffer_begin(), E = buffer_end(); I != E; ++I) { - const FileEntry *Entry = Rewrite.getSourceMgr().getFileEntryForID(I->first); - std::string Filename = Entry->getName(); - if (PathRewriter) - Filename = PathRewriter->RewriteFilename(Filename); - std::string Err; - llvm::raw_fd_ostream OS(Filename.c_str(), Err, - llvm::raw_fd_ostream::F_Binary); - if (!Err.empty()) { - Diags.Report(clang::diag::err_fe_unable_to_open_output) - << Filename << Err; - continue; - } - RewriteBuffer &RewriteBuf = I->second; - RewriteBuf.write(OS); - OS.flush(); - } - - return false; -} - -bool FixItRewriter::IncludeInDiagnosticCounts() const { - return Client ? Client->IncludeInDiagnosticCounts() : true; -} - -void FixItRewriter::HandleDiagnostic(Diagnostic::Level DiagLevel, - const DiagnosticInfo &Info) { - Client->HandleDiagnostic(DiagLevel, Info); - - // Skip over any diagnostics that are ignored or notes. - if (DiagLevel <= Diagnostic::Note) - return; - - // Make sure that we can perform all of the modifications we - // in this diagnostic. - bool CanRewrite = Info.getNumFixItHints() > 0; - for (unsigned Idx = 0, Last = Info.getNumFixItHints(); - Idx < Last; ++Idx) { - const FixItHint &Hint = Info.getFixItHint(Idx); - if (Hint.RemoveRange.isValid() && - Rewrite.getRangeSize(Hint.RemoveRange) == -1) { - CanRewrite = false; - break; - } - - if (Hint.InsertionLoc.isValid() && - !Rewrite.isRewritable(Hint.InsertionLoc)) { - CanRewrite = false; - break; - } - } - - if (!CanRewrite) { - if (Info.getNumFixItHints() > 0) - Diag(Info.getLocation(), diag::note_fixit_in_macro); - - // If this was an error, refuse to perform any rewriting. - if (DiagLevel == Diagnostic::Error || DiagLevel == Diagnostic::Fatal) { - if (++NumFailures == 1) - Diag(Info.getLocation(), diag::note_fixit_unfixed_error); - } - return; - } - - bool Failed = false; - for (unsigned Idx = 0, Last = Info.getNumFixItHints(); - Idx < Last; ++Idx) { - const FixItHint &Hint = Info.getFixItHint(Idx); - if (!Hint.RemoveRange.isValid()) { - // We're adding code. - if (Rewrite.InsertTextBefore(Hint.InsertionLoc, Hint.CodeToInsert)) - Failed = true; - continue; - } - - if (Hint.CodeToInsert.empty()) { - // We're removing code. - if (Rewrite.RemoveText(Hint.RemoveRange.getBegin(), - Rewrite.getRangeSize(Hint.RemoveRange))) - Failed = true; - continue; - } - - // We're replacing code. - if (Rewrite.ReplaceText(Hint.RemoveRange.getBegin(), - Rewrite.getRangeSize(Hint.RemoveRange), - Hint.CodeToInsert)) - Failed = true; - } - - if (Failed) { - ++NumFailures; - Diag(Info.getLocation(), diag::note_fixit_failed); - return; - } - - Diag(Info.getLocation(), diag::note_fixit_applied); -} - -/// \brief Emit a diagnostic via the adapted diagnostic client. -void FixItRewriter::Diag(FullSourceLoc Loc, unsigned DiagID) { - // When producing this diagnostic, we temporarily bypass ourselves, - // clear out any current diagnostic, and let the downstream client - // format the diagnostic. - Diags.setClient(Client); - Diags.Clear(); - Diags.Report(Loc, DiagID); - Diags.setClient(this); -} - -FixItPathRewriter::~FixItPathRewriter() {} diff --git a/contrib/llvm/tools/clang/lib/Frontend/FrontendAction.cpp b/contrib/llvm/tools/clang/lib/Frontend/FrontendAction.cpp index 87fc122..dbbf69c 100644 --- a/contrib/llvm/tools/clang/lib/Frontend/FrontendAction.cpp +++ b/contrib/llvm/tools/clang/lib/Frontend/FrontendAction.cpp @@ -25,25 +25,28 @@ FrontendAction::FrontendAction() : Instance(0) {} FrontendAction::~FrontendAction() {} -void FrontendAction::setCurrentFile(llvm::StringRef Value, ASTUnit *AST) { +void FrontendAction::setCurrentFile(llvm::StringRef Value, InputKind Kind, + ASTUnit *AST) { CurrentFile = Value; + CurrentFileKind = Kind; CurrentASTUnit.reset(AST); } bool FrontendAction::BeginSourceFile(CompilerInstance &CI, llvm::StringRef Filename, - bool IsAST) { + InputKind InputKind) { assert(!Instance && "Already processing a source file!"); assert(!Filename.empty() && "Unexpected empty filename!"); - setCurrentFile(Filename); + setCurrentFile(Filename, InputKind); setCompilerInstance(&CI); // AST files follow a very different path, since they share objects via the // AST unit. - if (IsAST) { + if (InputKind == IK_AST) { assert(!usesPreprocessorOnly() && "Attempt to pass AST file to preprocessor only action!"); - assert(hasASTSupport() && "This action does not have AST support!"); + assert(hasASTFileSupport() && + "This action does not have AST file support!"); llvm::IntrusiveRefCntPtr<Diagnostic> Diags(&CI.getDiagnostics()); std::string Error; @@ -51,7 +54,7 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI, if (!AST) goto failure; - setCurrentFile(Filename, AST); + setCurrentFile(Filename, InputKind, AST); // Set the shared objects, these are reset when we finish processing the // file, otherwise the CompilerInstance will happily destroy them. @@ -72,6 +75,30 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI, return true; } + // Set up the file and source managers, if needed. + if (!CI.hasFileManager()) + CI.createFileManager(); + if (!CI.hasSourceManager()) + CI.createSourceManager(); + + // IR files bypass the rest of initialization. + if (InputKind == IK_LLVM_IR) { + assert(hasIRSupport() && + "This action does not have IR file support!"); + + // Inform the diagnostic client we are processing a source file. + CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), 0); + + // Initialize the action. + if (!BeginSourceFileAction(CI, Filename)) + goto failure; + + return true; + } + + // Set up the preprocessor. + CI.createPreprocessor(); + // Inform the diagnostic client we are processing a source file. CI.getDiagnosticClient().BeginSourceFile(CI.getLangOpts(), &CI.getPreprocessor()); @@ -84,11 +111,10 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI, /// action. if (!usesPreprocessorOnly()) { CI.createASTContext(); - CI.setASTConsumer(CreateASTConsumer(CI, Filename)); - if (!CI.hasASTConsumer()) - goto failure; - /// Use PCH? + /// Use PCH? If so, we want the PCHReader active before the consumer + /// is created, because the consumer might be interested in the reader + /// (e.g. the PCH writer for chaining). if (!CI.getPreprocessorOpts().ImplicitPCHInclude.empty()) { assert(hasPCHSupport() && "This action does not have PCH support!"); CI.createPCHExternalASTSource( @@ -96,6 +122,10 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI, if (!CI.getASTContext().getExternalSource()) goto failure; } + + CI.setASTConsumer(CreateASTConsumer(CI, Filename)); + if (!CI.hasASTConsumer()) + goto failure; } // Initialize builtin info as long as we aren't using an external AST @@ -119,7 +149,7 @@ bool FrontendAction::BeginSourceFile(CompilerInstance &CI, } CI.getDiagnosticClient().EndSourceFile(); - setCurrentFile(""); + setCurrentFile("", IK_None); setCompilerInstance(0); return false; } @@ -198,7 +228,7 @@ void FrontendAction::EndSourceFile() { } setCompilerInstance(0); - setCurrentFile(""); + setCurrentFile("", IK_None); } //===----------------------------------------------------------------------===// diff --git a/contrib/llvm/tools/clang/lib/Frontend/FrontendActions.cpp b/contrib/llvm/tools/clang/lib/Frontend/FrontendActions.cpp index 6cd960b..3a53dee 100644 --- a/contrib/llvm/tools/clang/lib/Frontend/FrontendActions.cpp +++ b/contrib/llvm/tools/clang/lib/Frontend/FrontendActions.cpp @@ -9,14 +9,13 @@ #include "clang/Frontend/FrontendActions.h" #include "clang/AST/ASTConsumer.h" +#include "clang/Lex/Pragma.h" #include "clang/Lex/Preprocessor.h" #include "clang/Parse/Parser.h" #include "clang/Basic/FileManager.h" -#include "clang/Frontend/AnalysisConsumer.h" #include "clang/Frontend/ASTConsumers.h" #include "clang/Frontend/ASTUnit.h" #include "clang/Frontend/CompilerInstance.h" -#include "clang/Frontend/FixItRewriter.h" #include "clang/Frontend/FrontendDiagnostic.h" #include "clang/Frontend/Utils.h" #include "llvm/ADT/OwningPtr.h" @@ -39,13 +38,6 @@ void InitOnlyAction::ExecuteAction() { // AST Consumer Actions //===----------------------------------------------------------------------===// -ASTConsumer *AnalysisAction::CreateASTConsumer(CompilerInstance &CI, - llvm::StringRef InFile) { - return CreateAnalysisConsumer(CI.getPreprocessor(), - CI.getFrontendOpts().OutputFile, - CI.getAnalyzerOpts()); -} - ASTConsumer *ASTPrintAction::CreateASTConsumer(CompilerInstance &CI, llvm::StringRef InFile) { if (llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, InFile)) @@ -88,17 +80,11 @@ ASTConsumer *GeneratePCHAction::CreateASTConsumer(CompilerInstance &CI, if (!OS) return 0; - if (CI.getFrontendOpts().RelocatablePCH) - return CreatePCHGenerator(CI.getPreprocessor(), OS, Sysroot.c_str()); - - return CreatePCHGenerator(CI.getPreprocessor(), OS); -} - -ASTConsumer *HTMLPrintAction::CreateASTConsumer(CompilerInstance &CI, - llvm::StringRef InFile) { - if (llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, InFile)) - return CreateHTMLPrinter(OS, CI.getPreprocessor()); - return 0; + PCHReader *Chain = CI.getInvocation().getFrontendOpts().ChainedPCH ? + CI.getPCHReader() : 0; + const char *isysroot = CI.getFrontendOpts().RelocatablePCH ? + Sysroot.c_str() : 0; + return CreatePCHGenerator(CI.getPreprocessor(), OS, Chain, isysroot); } ASTConsumer *InheritanceViewAction::CreateASTConsumer(CompilerInstance &CI, @@ -106,57 +92,6 @@ ASTConsumer *InheritanceViewAction::CreateASTConsumer(CompilerInstance &CI, return CreateInheritanceViewer(CI.getFrontendOpts().ViewClassInheritance); } -FixItAction::FixItAction() {} -FixItAction::~FixItAction() {} - -ASTConsumer *FixItAction::CreateASTConsumer(CompilerInstance &CI, - llvm::StringRef InFile) { - return new ASTConsumer(); -} - -class FixItActionSuffixInserter : public FixItPathRewriter { - std::string NewSuffix; - -public: - explicit FixItActionSuffixInserter(std::string NewSuffix) - : NewSuffix(NewSuffix) {} - - std::string RewriteFilename(const std::string &Filename) { - llvm::sys::Path Path(Filename); - std::string Suffix = Path.getSuffix(); - Path.eraseSuffix(); - Path.appendSuffix(NewSuffix + "." + Suffix); - return Path.c_str(); - } -}; - -bool FixItAction::BeginSourceFileAction(CompilerInstance &CI, - llvm::StringRef Filename) { - const FrontendOptions &FEOpts = getCompilerInstance().getFrontendOpts(); - if (!FEOpts.FixItSuffix.empty()) { - PathRewriter.reset(new FixItActionSuffixInserter(FEOpts.FixItSuffix)); - } else { - PathRewriter.reset(); - } - Rewriter.reset(new FixItRewriter(CI.getDiagnostics(), CI.getSourceManager(), - CI.getLangOpts(), PathRewriter.get())); - return true; -} - -void FixItAction::EndSourceFileAction() { - // Otherwise rewrite all files. - Rewriter->WriteFixedFiles(); -} - -ASTConsumer *RewriteObjCAction::CreateASTConsumer(CompilerInstance &CI, - llvm::StringRef InFile) { - if (llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, InFile, "cpp")) - return CreateObjCRewriter(InFile, OS, - CI.getDiagnostics(), CI.getLangOpts(), - CI.getDiagnosticOpts().NoRewriteMacros); - return 0; -} - ASTConsumer *SyntaxOnlyAction::CreateASTConsumer(CompilerInstance &CI, llvm::StringRef InFile) { return new ASTConsumer(); @@ -223,6 +158,9 @@ void ParseOnlyAction::ExecuteAction() { void PreprocessOnlyAction::ExecuteAction() { Preprocessor &PP = getCompilerInstance().getPreprocessor(); + // Ignore unknown pragmas. + PP.AddPragmaHandler(new EmptyPragmaHandler()); + Token Tok; // Start parsing the specified input file. PP.EnterMainSourceFile(); @@ -254,19 +192,3 @@ void PrintPreprocessedAction::ExecuteAction() { DoPrintPreprocessedInput(CI.getPreprocessor(), OS, CI.getPreprocessorOutputOpts()); } - -void RewriteMacrosAction::ExecuteAction() { - CompilerInstance &CI = getCompilerInstance(); - llvm::raw_ostream *OS = CI.createDefaultOutputFile(true, getCurrentFile()); - if (!OS) return; - - RewriteMacrosInInput(CI.getPreprocessor(), OS); -} - -void RewriteTestAction::ExecuteAction() { - CompilerInstance &CI = getCompilerInstance(); - llvm::raw_ostream *OS = CI.createDefaultOutputFile(false, getCurrentFile()); - if (!OS) return; - - DoRewriteTest(CI.getPreprocessor(), OS); -} diff --git a/contrib/llvm/tools/clang/lib/Frontend/FrontendOptions.cpp b/contrib/llvm/tools/clang/lib/Frontend/FrontendOptions.cpp index bd91638..9dfee24 100644 --- a/contrib/llvm/tools/clang/lib/Frontend/FrontendOptions.cpp +++ b/contrib/llvm/tools/clang/lib/Frontend/FrontendOptions.cpp @@ -11,8 +11,7 @@ #include "llvm/ADT/StringSwitch.h" using namespace clang; -FrontendOptions::InputKind -FrontendOptions::getInputKindForExtension(llvm::StringRef Extension) { +InputKind FrontendOptions::getInputKindForExtension(llvm::StringRef Extension) { return llvm::StringSwitch<InputKind>(Extension) .Case("ast", IK_AST) .Case("c", IK_C) @@ -27,5 +26,6 @@ FrontendOptions::getInputKindForExtension(llvm::StringRef Extension) { .Cases("C", "cc", "cp", IK_CXX) .Cases("cpp", "CPP", "c++", "cxx", "hpp", IK_CXX) .Case("cl", IK_OpenCL) + .Cases("ll", "bc", IK_LLVM_IR) .Default(IK_C); } diff --git a/contrib/llvm/tools/clang/lib/Frontend/GeneratePCH.cpp b/contrib/llvm/tools/clang/lib/Frontend/GeneratePCH.cpp index 6251bac..2f3df94 100644 --- a/contrib/llvm/tools/clang/lib/Frontend/GeneratePCH.cpp +++ b/contrib/llvm/tools/clang/lib/Frontend/GeneratePCH.cpp @@ -32,20 +32,24 @@ namespace { llvm::raw_ostream *Out; Sema *SemaPtr; MemorizeStatCalls *StatCalls; // owned by the FileManager + std::vector<unsigned char> Buffer; + llvm::BitstreamWriter Stream; + PCHWriter Writer; public: - explicit PCHGenerator(const Preprocessor &PP, - const char *isysroot, - llvm::raw_ostream *Out); + PCHGenerator(const Preprocessor &PP, PCHReader *Chain, + const char *isysroot, llvm::raw_ostream *Out); virtual void InitializeSema(Sema &S) { SemaPtr = &S; } virtual void HandleTranslationUnit(ASTContext &Ctx); }; } PCHGenerator::PCHGenerator(const Preprocessor &PP, + PCHReader *Chain, const char *isysroot, llvm::raw_ostream *OS) - : PP(PP), isysroot(isysroot), Out(OS), SemaPtr(0), StatCalls(0) { + : PP(PP), isysroot(isysroot), Out(OS), SemaPtr(0), StatCalls(0), + Stream(Buffer), Writer(Stream, Chain) { // Install a stat() listener to keep track of all of the stat() // calls. @@ -57,11 +61,6 @@ void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) { if (PP.getDiagnostics().hasErrorOccurred()) return; - // Write the PCH contents into a buffer - std::vector<unsigned char> Buffer; - llvm::BitstreamWriter Stream(Buffer); - PCHWriter Writer(Stream); - // Emit the PCH file assert(SemaPtr && "No Sema?"); Writer.WritePCH(*SemaPtr, StatCalls, isysroot); @@ -71,10 +70,14 @@ void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) { // Make sure it hits disk now. Out->flush(); + + // Free up some memory, in case the process is kept alive. + Buffer.clear(); } ASTConsumer *clang::CreatePCHGenerator(const Preprocessor &PP, llvm::raw_ostream *OS, + PCHReader *Chain, const char *isysroot) { - return new PCHGenerator(PP, isysroot, OS); + return new PCHGenerator(PP, Chain, isysroot, OS); } diff --git a/contrib/llvm/tools/clang/lib/Frontend/HTMLDiagnostics.cpp b/contrib/llvm/tools/clang/lib/Frontend/HTMLDiagnostics.cpp deleted file mode 100644 index 022a34d..0000000 --- a/contrib/llvm/tools/clang/lib/Frontend/HTMLDiagnostics.cpp +++ /dev/null @@ -1,577 +0,0 @@ -//===--- HTMLDiagnostics.cpp - HTML Diagnostics for Paths ----*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This file defines the HTMLDiagnostics object. -// -//===----------------------------------------------------------------------===// - -#include "clang/Frontend/PathDiagnosticClients.h" -#include "clang/Checker/BugReporter/PathDiagnostic.h" -#include "clang/AST/ASTContext.h" -#include "clang/AST/Decl.h" -#include "clang/Basic/SourceManager.h" -#include "clang/Basic/FileManager.h" -#include "clang/Rewrite/Rewriter.h" -#include "clang/Rewrite/HTMLRewrite.h" -#include "clang/Lex/Lexer.h" -#include "clang/Lex/Preprocessor.h" -#include "llvm/Support/MemoryBuffer.h" -#include "llvm/Support/raw_ostream.h" -#include "llvm/System/Path.h" - -using namespace clang; - -//===----------------------------------------------------------------------===// -// Boilerplate. -//===----------------------------------------------------------------------===// - -namespace { - -class HTMLDiagnostics : public PathDiagnosticClient { - llvm::sys::Path Directory, FilePrefix; - bool createdDir, noDir; - const Preprocessor &PP; - std::vector<const PathDiagnostic*> BatchedDiags; -public: - HTMLDiagnostics(const std::string& prefix, const Preprocessor &pp); - - virtual ~HTMLDiagnostics() { FlushDiagnostics(NULL); } - - virtual void FlushDiagnostics(llvm::SmallVectorImpl<std::string> *FilesMade); - - virtual void HandlePathDiagnostic(const PathDiagnostic* D); - - virtual llvm::StringRef getName() const { - return "HTMLDiagnostics"; - } - - unsigned ProcessMacroPiece(llvm::raw_ostream& os, - const PathDiagnosticMacroPiece& P, - unsigned num); - - void HandlePiece(Rewriter& R, FileID BugFileID, - const PathDiagnosticPiece& P, unsigned num, unsigned max); - - void HighlightRange(Rewriter& R, FileID BugFileID, SourceRange Range, - const char *HighlightStart = "<span class=\"mrange\">", - const char *HighlightEnd = "</span>"); - - void ReportDiag(const PathDiagnostic& D, - llvm::SmallVectorImpl<std::string> *FilesMade); -}; - -} // end anonymous namespace - -HTMLDiagnostics::HTMLDiagnostics(const std::string& prefix, - const Preprocessor &pp) - : Directory(prefix), FilePrefix(prefix), createdDir(false), noDir(false), - PP(pp) { - // All html files begin with "report" - FilePrefix.appendComponent("report"); -} - -PathDiagnosticClient* -clang::CreateHTMLDiagnosticClient(const std::string& prefix, - const Preprocessor &PP) { - return new HTMLDiagnostics(prefix, PP); -} - -//===----------------------------------------------------------------------===// -// Report processing. -//===----------------------------------------------------------------------===// - -void HTMLDiagnostics::HandlePathDiagnostic(const PathDiagnostic* D) { - if (!D) - return; - - if (D->empty()) { - delete D; - return; - } - - const_cast<PathDiagnostic*>(D)->flattenLocations(); - BatchedDiags.push_back(D); -} - -void -HTMLDiagnostics::FlushDiagnostics(llvm::SmallVectorImpl<std::string> *FilesMade) -{ - while (!BatchedDiags.empty()) { - const PathDiagnostic* D = BatchedDiags.back(); - BatchedDiags.pop_back(); - ReportDiag(*D, FilesMade); - delete D; - } - - BatchedDiags.clear(); -} - -void HTMLDiagnostics::ReportDiag(const PathDiagnostic& D, - llvm::SmallVectorImpl<std::string> *FilesMade){ - // Create the HTML directory if it is missing. - if (!createdDir) { - createdDir = true; - std::string ErrorMsg; - Directory.createDirectoryOnDisk(true, &ErrorMsg); - - if (!Directory.isDirectory()) { - llvm::errs() << "warning: could not create directory '" - << Directory.str() << "'\n" - << "reason: " << ErrorMsg << '\n'; - - noDir = true; - - return; - } - } - - if (noDir) - return; - - const SourceManager &SMgr = D.begin()->getLocation().getManager(); - FileID FID; - - // Verify that the entire path is from the same FileID. - for (PathDiagnostic::const_iterator I = D.begin(), E = D.end(); I != E; ++I) { - FullSourceLoc L = I->getLocation().asLocation().getInstantiationLoc(); - - if (FID.isInvalid()) { - FID = SMgr.getFileID(L); - } else if (SMgr.getFileID(L) != FID) - return; // FIXME: Emit a warning? - - // Check the source ranges. - for (PathDiagnosticPiece::range_iterator RI=I->ranges_begin(), - RE=I->ranges_end(); RI!=RE; ++RI) { - - SourceLocation L = SMgr.getInstantiationLoc(RI->getBegin()); - - if (!L.isFileID() || SMgr.getFileID(L) != FID) - return; // FIXME: Emit a warning? - - L = SMgr.getInstantiationLoc(RI->getEnd()); - - if (!L.isFileID() || SMgr.getFileID(L) != FID) - return; // FIXME: Emit a warning? - } - } - - if (FID.isInvalid()) - return; // FIXME: Emit a warning? - - // Create a new rewriter to generate HTML. - Rewriter R(const_cast<SourceManager&>(SMgr), PP.getLangOptions()); - - // Process the path. - unsigned n = D.size(); - unsigned max = n; - - for (PathDiagnostic::const_reverse_iterator I=D.rbegin(), E=D.rend(); - I!=E; ++I, --n) - HandlePiece(R, FID, *I, n, max); - - // Add line numbers, header, footer, etc. - - // unsigned FID = R.getSourceMgr().getMainFileID(); - html::EscapeText(R, FID); - html::AddLineNumbers(R, FID); - - // If we have a preprocessor, relex the file and syntax highlight. - // We might not have a preprocessor if we come from a deserialized AST file, - // for example. - - html::SyntaxHighlight(R, FID, PP); - html::HighlightMacros(R, FID, PP); - - // Get the full directory name of the analyzed file. - - const FileEntry* Entry = SMgr.getFileEntryForID(FID); - - // This is a cludge; basically we want to append either the full - // working directory if we have no directory information. This is - // a work in progress. - - std::string DirName = ""; - - if (!llvm::sys::Path(Entry->getName()).isAbsolute()) { - llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory(); - DirName = P.str() + "/"; - } - - // Add the name of the file as an <h1> tag. - - { - std::string s; - llvm::raw_string_ostream os(s); - - os << "<!-- REPORTHEADER -->\n" - << "<h3>Bug Summary</h3>\n<table class=\"simpletable\">\n" - "<tr><td class=\"rowname\">File:</td><td>" - << html::EscapeText(DirName) - << html::EscapeText(Entry->getName()) - << "</td></tr>\n<tr><td class=\"rowname\">Location:</td><td>" - "<a href=\"#EndPath\">line " - << (*D.rbegin()).getLocation().asLocation().getInstantiationLineNumber() - << ", column " - << (*D.rbegin()).getLocation().asLocation().getInstantiationColumnNumber() - << "</a></td></tr>\n" - "<tr><td class=\"rowname\">Description:</td><td>" - << D.getDescription() << "</td></tr>\n"; - - // Output any other meta data. - - for (PathDiagnostic::meta_iterator I=D.meta_begin(), E=D.meta_end(); - I!=E; ++I) { - os << "<tr><td></td><td>" << html::EscapeText(*I) << "</td></tr>\n"; - } - - os << "</table>\n<!-- REPORTSUMMARYEXTRA -->\n" - "<h3>Annotated Source Code</h3>\n"; - - R.InsertTextBefore(SMgr.getLocForStartOfFile(FID), os.str()); - } - - // Embed meta-data tags. - { - std::string s; - llvm::raw_string_ostream os(s); - - const std::string& BugDesc = D.getDescription(); - if (!BugDesc.empty()) - os << "\n<!-- BUGDESC " << BugDesc << " -->\n"; - - const std::string& BugType = D.getBugType(); - if (!BugType.empty()) - os << "\n<!-- BUGTYPE " << BugType << " -->\n"; - - const std::string& BugCategory = D.getCategory(); - if (!BugCategory.empty()) - os << "\n<!-- BUGCATEGORY " << BugCategory << " -->\n"; - - os << "\n<!-- BUGFILE " << DirName << Entry->getName() << " -->\n"; - - os << "\n<!-- BUGLINE " - << D.back()->getLocation().asLocation().getInstantiationLineNumber() - << " -->\n"; - - os << "\n<!-- BUGPATHLENGTH " << D.size() << " -->\n"; - - // Mark the end of the tags. - os << "\n<!-- BUGMETAEND -->\n"; - - // Insert the text. - R.InsertTextBefore(SMgr.getLocForStartOfFile(FID), os.str()); - } - - // Add CSS, header, and footer. - - html::AddHeaderFooterInternalBuiltinCSS(R, FID, Entry->getName()); - - // Get the rewrite buffer. - const RewriteBuffer *Buf = R.getRewriteBufferFor(FID); - - if (!Buf) { - llvm::errs() << "warning: no diagnostics generated for main file.\n"; - return; - } - - // Create a path for the target HTML file. - llvm::sys::Path F(FilePrefix); - F.makeUnique(false, NULL); - - // Rename the file with an HTML extension. - llvm::sys::Path H(F); - H.appendSuffix("html"); - F.renamePathOnDisk(H, NULL); - - std::string ErrorMsg; - llvm::raw_fd_ostream os(H.c_str(), ErrorMsg); - - if (!ErrorMsg.empty()) { - (llvm::errs() << "warning: could not create file '" << F.str() - << "'\n").flush(); - return; - } - - if (FilesMade) - FilesMade->push_back(H.getLast()); - - // Emit the HTML to disk. - for (RewriteBuffer::iterator I = Buf->begin(), E = Buf->end(); I!=E; ++I) - os << *I; -} - -void HTMLDiagnostics::HandlePiece(Rewriter& R, FileID BugFileID, - const PathDiagnosticPiece& P, - unsigned num, unsigned max) { - - // For now, just draw a box above the line in question, and emit the - // warning. - FullSourceLoc Pos = P.getLocation().asLocation(); - - if (!Pos.isValid()) - return; - - SourceManager &SM = R.getSourceMgr(); - assert(&Pos.getManager() == &SM && "SourceManagers are different!"); - std::pair<FileID, unsigned> LPosInfo = SM.getDecomposedInstantiationLoc(Pos); - - if (LPosInfo.first != BugFileID) - return; - - const llvm::MemoryBuffer *Buf = SM.getBuffer(LPosInfo.first); - const char* FileStart = Buf->getBufferStart(); - - // Compute the column number. Rewind from the current position to the start - // of the line. - unsigned ColNo = SM.getColumnNumber(LPosInfo.first, LPosInfo.second); - const char *TokInstantiationPtr =Pos.getInstantiationLoc().getCharacterData(); - const char *LineStart = TokInstantiationPtr-ColNo; - - // Compute LineEnd. - const char *LineEnd = TokInstantiationPtr; - const char* FileEnd = Buf->getBufferEnd(); - while (*LineEnd != '\n' && LineEnd != FileEnd) - ++LineEnd; - - // Compute the margin offset by counting tabs and non-tabs. - unsigned PosNo = 0; - for (const char* c = LineStart; c != TokInstantiationPtr; ++c) - PosNo += *c == '\t' ? 8 : 1; - - // Create the html for the message. - - const char *Kind = 0; - switch (P.getKind()) { - case PathDiagnosticPiece::Event: Kind = "Event"; break; - case PathDiagnosticPiece::ControlFlow: Kind = "Control"; break; - // Setting Kind to "Control" is intentional. - case PathDiagnosticPiece::Macro: Kind = "Control"; break; - } - - std::string sbuf; - llvm::raw_string_ostream os(sbuf); - - os << "\n<tr><td class=\"num\"></td><td class=\"line\"><div id=\""; - - if (num == max) - os << "EndPath"; - else - os << "Path" << num; - - os << "\" class=\"msg"; - if (Kind) - os << " msg" << Kind; - os << "\" style=\"margin-left:" << PosNo << "ex"; - - // Output a maximum size. - if (!isa<PathDiagnosticMacroPiece>(P)) { - // Get the string and determining its maximum substring. - const std::string& Msg = P.getString(); - unsigned max_token = 0; - unsigned cnt = 0; - unsigned len = Msg.size(); - - for (std::string::const_iterator I=Msg.begin(), E=Msg.end(); I!=E; ++I) - switch (*I) { - default: - ++cnt; - continue; - case ' ': - case '\t': - case '\n': - if (cnt > max_token) max_token = cnt; - cnt = 0; - } - - if (cnt > max_token) - max_token = cnt; - - // Determine the approximate size of the message bubble in em. - unsigned em; - const unsigned max_line = 120; - - if (max_token >= max_line) - em = max_token / 2; - else { - unsigned characters = max_line; - unsigned lines = len / max_line; - - if (lines > 0) { - for (; characters > max_token; --characters) - if (len / characters > lines) { - ++characters; - break; - } - } - - em = characters / 2; - } - - if (em < max_line/2) - os << "; max-width:" << em << "em"; - } - else - os << "; max-width:100em"; - - os << "\">"; - - if (max > 1) { - os << "<table class=\"msgT\"><tr><td valign=\"top\">"; - os << "<div class=\"PathIndex"; - if (Kind) os << " PathIndex" << Kind; - os << "\">" << num << "</div>"; - os << "</td><td>"; - } - - if (const PathDiagnosticMacroPiece *MP = - dyn_cast<PathDiagnosticMacroPiece>(&P)) { - - os << "Within the expansion of the macro '"; - - // Get the name of the macro by relexing it. - { - FullSourceLoc L = MP->getLocation().asLocation().getInstantiationLoc(); - assert(L.isFileID()); - llvm::StringRef BufferInfo = L.getBufferData(); - const char* MacroName = L.getDecomposedLoc().second + BufferInfo.data(); - Lexer rawLexer(L, PP.getLangOptions(), BufferInfo.begin(), - MacroName, BufferInfo.end()); - - Token TheTok; - rawLexer.LexFromRawLexer(TheTok); - for (unsigned i = 0, n = TheTok.getLength(); i < n; ++i) - os << MacroName[i]; - } - - os << "':\n"; - - if (max > 1) - os << "</td></tr></table>"; - - // Within a macro piece. Write out each event. - ProcessMacroPiece(os, *MP, 0); - } - else { - os << html::EscapeText(P.getString()); - - if (max > 1) - os << "</td></tr></table>"; - } - - os << "</div></td></tr>"; - - // Insert the new html. - unsigned DisplayPos = LineEnd - FileStart; - SourceLocation Loc = - SM.getLocForStartOfFile(LPosInfo.first).getFileLocWithOffset(DisplayPos); - - R.InsertTextBefore(Loc, os.str()); - - // Now highlight the ranges. - for (const SourceRange *I = P.ranges_begin(), *E = P.ranges_end(); - I != E; ++I) - HighlightRange(R, LPosInfo.first, *I); - -#if 0 - // If there is a code insertion hint, insert that code. - // FIXME: This code is disabled because it seems to mangle the HTML - // output. I'm leaving it here because it's generally the right idea, - // but needs some help from someone more familiar with the rewriter. - for (const FixItHint *Hint = P.fixit_begin(), *HintEnd = P.fixit_end(); - Hint != HintEnd; ++Hint) { - if (Hint->RemoveRange.isValid()) { - HighlightRange(R, LPosInfo.first, Hint->RemoveRange, - "<span class=\"CodeRemovalHint\">", "</span>"); - } - if (Hint->InsertionLoc.isValid()) { - std::string EscapedCode = html::EscapeText(Hint->CodeToInsert, true); - EscapedCode = "<span class=\"CodeInsertionHint\">" + EscapedCode - + "</span>"; - R.InsertTextBefore(Hint->InsertionLoc, EscapedCode); - } - } -#endif -} - -static void EmitAlphaCounter(llvm::raw_ostream& os, unsigned n) { - unsigned x = n % ('z' - 'a'); - n /= 'z' - 'a'; - - if (n > 0) - EmitAlphaCounter(os, n); - - os << char('a' + x); -} - -unsigned HTMLDiagnostics::ProcessMacroPiece(llvm::raw_ostream& os, - const PathDiagnosticMacroPiece& P, - unsigned num) { - - for (PathDiagnosticMacroPiece::const_iterator I=P.begin(), E=P.end(); - I!=E; ++I) { - - if (const PathDiagnosticMacroPiece *MP = - dyn_cast<PathDiagnosticMacroPiece>(*I)) { - num = ProcessMacroPiece(os, *MP, num); - continue; - } - - if (PathDiagnosticEventPiece *EP = dyn_cast<PathDiagnosticEventPiece>(*I)) { - os << "<div class=\"msg msgEvent\" style=\"width:94%; " - "margin-left:5px\">" - "<table class=\"msgT\"><tr>" - "<td valign=\"top\"><div class=\"PathIndex PathIndexEvent\">"; - EmitAlphaCounter(os, num++); - os << "</div></td><td valign=\"top\">" - << html::EscapeText(EP->getString()) - << "</td></tr></table></div>\n"; - } - } - - return num; -} - -void HTMLDiagnostics::HighlightRange(Rewriter& R, FileID BugFileID, - SourceRange Range, - const char *HighlightStart, - const char *HighlightEnd) { - SourceManager &SM = R.getSourceMgr(); - const LangOptions &LangOpts = R.getLangOpts(); - - SourceLocation InstantiationStart = SM.getInstantiationLoc(Range.getBegin()); - unsigned StartLineNo = SM.getInstantiationLineNumber(InstantiationStart); - - SourceLocation InstantiationEnd = SM.getInstantiationLoc(Range.getEnd()); - unsigned EndLineNo = SM.getInstantiationLineNumber(InstantiationEnd); - - if (EndLineNo < StartLineNo) - return; - - if (SM.getFileID(InstantiationStart) != BugFileID || - SM.getFileID(InstantiationEnd) != BugFileID) - return; - - // Compute the column number of the end. - unsigned EndColNo = SM.getInstantiationColumnNumber(InstantiationEnd); - unsigned OldEndColNo = EndColNo; - - if (EndColNo) { - // Add in the length of the token, so that we cover multi-char tokens. - EndColNo += Lexer::MeasureTokenLength(Range.getEnd(), SM, LangOpts)-1; - } - - // Highlight the range. Make the span tag the outermost tag for the - // selected range. - - SourceLocation E = - InstantiationEnd.getFileLocWithOffset(EndColNo - OldEndColNo); - - html::HighlightRange(R, InstantiationStart, E, HighlightStart, HighlightEnd); -} diff --git a/contrib/llvm/tools/clang/lib/Frontend/HTMLPrint.cpp b/contrib/llvm/tools/clang/lib/Frontend/HTMLPrint.cpp deleted file mode 100644 index 9ea8cb3..0000000 --- a/contrib/llvm/tools/clang/lib/Frontend/HTMLPrint.cpp +++ /dev/null @@ -1,94 +0,0 @@ -//===--- HTMLPrint.cpp - Source code -> HTML pretty-printing --------------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// Pretty-printing of source code to HTML. -// -//===----------------------------------------------------------------------===// - -#include "clang/Frontend/ASTConsumers.h" -#include "clang/AST/ASTConsumer.h" -#include "clang/AST/ASTContext.h" -#include "clang/AST/Decl.h" -#include "clang/Basic/Diagnostic.h" -#include "clang/Basic/FileManager.h" -#include "clang/Basic/SourceManager.h" -#include "clang/Lex/Preprocessor.h" -#include "clang/Rewrite/HTMLRewrite.h" -#include "clang/Rewrite/Rewriter.h" -#include "llvm/Support/MemoryBuffer.h" -#include "llvm/Support/raw_ostream.h" -using namespace clang; - -//===----------------------------------------------------------------------===// -// Functional HTML pretty-printing. -//===----------------------------------------------------------------------===// - -namespace { - class HTMLPrinter : public ASTConsumer { - Rewriter R; - llvm::raw_ostream *Out; - Preprocessor &PP; - bool SyntaxHighlight, HighlightMacros; - - public: - HTMLPrinter(llvm::raw_ostream *OS, Preprocessor &pp, - bool _SyntaxHighlight, bool _HighlightMacros) - : Out(OS), PP(pp), SyntaxHighlight(_SyntaxHighlight), - HighlightMacros(_HighlightMacros) {} - - void Initialize(ASTContext &context); - void HandleTranslationUnit(ASTContext &Ctx); - }; -} - -ASTConsumer* clang::CreateHTMLPrinter(llvm::raw_ostream *OS, - Preprocessor &PP, - bool SyntaxHighlight, - bool HighlightMacros) { - return new HTMLPrinter(OS, PP, SyntaxHighlight, HighlightMacros); -} - -void HTMLPrinter::Initialize(ASTContext &context) { - R.setSourceMgr(context.getSourceManager(), context.getLangOptions()); -} - -void HTMLPrinter::HandleTranslationUnit(ASTContext &Ctx) { - if (PP.getDiagnostics().hasErrorOccurred()) - return; - - // Format the file. - FileID FID = R.getSourceMgr().getMainFileID(); - const FileEntry* Entry = R.getSourceMgr().getFileEntryForID(FID); - const char* Name; - // In some cases, in particular the case where the input is from stdin, - // there is no entry. Fall back to the memory buffer for a name in those - // cases. - if (Entry) - Name = Entry->getName(); - else - Name = R.getSourceMgr().getBuffer(FID)->getBufferIdentifier(); - - html::AddLineNumbers(R, FID); - html::AddHeaderFooterInternalBuiltinCSS(R, FID, Name); - - // If we have a preprocessor, relex the file and syntax highlight. - // We might not have a preprocessor if we come from a deserialized AST file, - // for example. - - if (SyntaxHighlight) html::SyntaxHighlight(R, FID, PP); - if (HighlightMacros) html::HighlightMacros(R, FID, PP); - html::EscapeText(R, FID, false, true); - - // Emit the HTML. - const RewriteBuffer &RewriteBuf = R.getEditBuffer(FID); - char *Buffer = (char*)malloc(RewriteBuf.size()); - std::copy(RewriteBuf.begin(), RewriteBuf.end(), Buffer); - Out->write(Buffer, RewriteBuf.size()); - free(Buffer); -} diff --git a/contrib/llvm/tools/clang/lib/Frontend/InitHeaderSearch.cpp b/contrib/llvm/tools/clang/lib/Frontend/InitHeaderSearch.cpp index 9490705..fcfee712 100644 --- a/contrib/llvm/tools/clang/lib/Frontend/InitHeaderSearch.cpp +++ b/contrib/llvm/tools/clang/lib/Frontend/InitHeaderSearch.cpp @@ -550,6 +550,8 @@ AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple) { System, true, false, false); AddPath("/lib/gcc/i686-pc-cygwin/3.4.4/include/c++", System, true, false, false); + AddPath("/lib/gcc/i686-pc-cygwin/3.4.4/include/c++/i686-pc-cygwin", + System, true, false, false); break; case llvm::Triple::MinGW64: // Try gcc 4.4.0 @@ -564,10 +566,35 @@ AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple) { AddMinGWCPlusPlusIncludePaths("c:/MinGW/lib/gcc", "mingw32", "4.3.0"); break; case llvm::Triple::Darwin: - AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1", - "i686-apple-darwin10", "", "x86_64", triple); - AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0", - "i686-apple-darwin8", "", "", triple); + switch (triple.getArch()) { + default: break; + + case llvm::Triple::ppc: + case llvm::Triple::ppc64: + AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1", + "powerpc-apple-darwin10", "", "ppc64", + triple); + AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0", + "powerpc-apple-darwin10", "", "ppc64", + triple); + break; + + case llvm::Triple::x86: + case llvm::Triple::x86_64: + AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1", + "i686-apple-darwin10", "", "x86_64", triple); + AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.0.0", + "i686-apple-darwin8", "", "", triple); + break; + + case llvm::Triple::arm: + case llvm::Triple::thumb: + AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1", + "arm-apple-darwin10", "v7", "", triple); + AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2.1", + "arm-apple-darwin10", "v6", "", triple); + break; + } break; case llvm::Triple::DragonFly: AddPath("/usr/include/c++/4.1", System, true, false, false); @@ -591,6 +618,8 @@ AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple) { "x86_64-linux-gnu", "32", "", triple); AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3", "i486-linux-gnu", "", "64", triple); + AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.3", + "arm-linux-gnueabi", "", "", triple); // Ubuntu 8.04.4 LTS "Hardy Heron" -- gcc-4.2.4 // Ubuntu 8.04.[0-3] LTS "Hardy Heron" -- gcc-4.2.3 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2", @@ -607,6 +636,10 @@ AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple) { // Redhat based distros. //===------------------------------------------------------------------===// // Fedora 13 + AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.4", + "x86_64-redhat-linux", "32", "", triple); + AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.4", + "i686-redhat-linux","", "", triple); // Fedora 12 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.4.3", "x86_64-redhat-linux", "32", "", triple); @@ -694,6 +727,11 @@ AddDefaultCPlusPlusIncludePaths(const llvm::Triple &triple) { // FreeBSD 8.0 // FreeBSD 7.3 AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2", "", "", "", triple); + AddGnuCPlusPlusIncludePaths("/usr/include/c++/4.2/backward", "", "", "", triple); + break; + case llvm::Triple::Minix: + AddGnuCPlusPlusIncludePaths("/usr/gnu/include/c++/4.4.3", + "", "", "", triple); break; case llvm::Triple::Solaris: // Solaris - Fall though.. diff --git a/contrib/llvm/tools/clang/lib/Frontend/InitPreprocessor.cpp b/contrib/llvm/tools/clang/lib/Frontend/InitPreprocessor.cpp index 2b35c8e..889b6e5 100644 --- a/contrib/llvm/tools/clang/lib/Frontend/InitPreprocessor.cpp +++ b/contrib/llvm/tools/clang/lib/Frontend/InitPreprocessor.cpp @@ -83,8 +83,8 @@ static void AddImplicitIncludeMacros(MacroBuilder &Builder, static void AddImplicitIncludePTH(MacroBuilder &Builder, Preprocessor &PP, llvm::StringRef ImplicitIncludePTH) { PTHManager *P = PP.getPTHManager(); - assert(P && "No PTHManager."); - const char *OriginalFile = P->getOriginalSourceFile(); + // Null check 'P' in the corner case where it couldn't be created. + const char *OriginalFile = P ? P->getOriginalSourceFile() : 0; if (!OriginalFile) { PP.getDiagnostics().Report(diag::err_fe_pth_file_has_no_source_header) @@ -195,9 +195,21 @@ static void DefineTypeWidth(llvm::StringRef MacroName, TargetInfo::IntType Ty, Builder.defineMacro(MacroName, llvm::Twine(TI.getTypeWidth(Ty))); } +static void DefineTypeSizeof(llvm::StringRef MacroName, unsigned BitWidth, + const TargetInfo &TI, MacroBuilder &Builder) { + Builder.defineMacro(MacroName, + llvm::Twine(BitWidth / TI.getCharWidth())); +} + static void DefineExactWidthIntType(TargetInfo::IntType Ty, const TargetInfo &TI, MacroBuilder &Builder) { int TypeWidth = TI.getTypeWidth(Ty); + + // Use the target specified int64 type, when appropriate, so that [u]int64_t + // ends up being defined in terms of the correct type. + if (TypeWidth == 64) + Ty = TI.getInt64Type(); + DefineType("__INT" + llvm::Twine(TypeWidth) + "_TYPE__", Ty, Builder); llvm::StringRef ConstSuffix(TargetInfo::getTypeConstantSuffix(Ty)); @@ -293,6 +305,8 @@ static void InitializePredefinedMacros(const TargetInfo &TI, if (LangOpts.Exceptions) Builder.defineMacro("__EXCEPTIONS"); + if (LangOpts.RTTI) + Builder.defineMacro("__GXX_RTTI"); if (LangOpts.SjLjExceptions) Builder.defineMacro("__USING_SJLJ_EXCEPTIONS__"); @@ -350,6 +364,23 @@ static void InitializePredefinedMacros(const TargetInfo &TI, DefineTypeSize("__WCHAR_MAX__", TI.getWCharType(), TI, Builder); DefineTypeSize("__INTMAX_MAX__", TI.getIntMaxType(), TI, Builder); + DefineTypeSizeof("__SIZEOF_DOUBLE__", TI.getDoubleWidth(), TI, Builder); + DefineTypeSizeof("__SIZEOF_FLOAT__", TI.getFloatWidth(), TI, Builder); + DefineTypeSizeof("__SIZEOF_INT__", TI.getIntWidth(), TI, Builder); + DefineTypeSizeof("__SIZEOF_LONG__", TI.getLongWidth(), TI, Builder); + DefineTypeSizeof("__SIZEOF_LONG_DOUBLE__",TI.getLongDoubleWidth(),TI,Builder); + DefineTypeSizeof("__SIZEOF_LONG_LONG__", TI.getLongLongWidth(), TI, Builder); + DefineTypeSizeof("__SIZEOF_POINTER__", TI.getPointerWidth(0), TI, Builder); + DefineTypeSizeof("__SIZEOF_SHORT__", TI.getShortWidth(), TI, Builder); + DefineTypeSizeof("__SIZEOF_PTRDIFF_T__", + TI.getTypeWidth(TI.getPtrDiffType(0)), TI, Builder); + DefineTypeSizeof("__SIZEOF_SIZE_T__", + TI.getTypeWidth(TI.getSizeType()), TI, Builder); + DefineTypeSizeof("__SIZEOF_WCHAR_T__", + TI.getTypeWidth(TI.getWCharType()), TI, Builder); + DefineTypeSizeof("__SIZEOF_WINT_T__", + TI.getTypeWidth(TI.getWIntType()), TI, Builder); + DefineType("__INTMAX_TYPE__", TI.getIntMaxType(), Builder); DefineType("__UINTMAX_TYPE__", TI.getUIntMaxType(), Builder); DefineTypeWidth("__INTMAX_WIDTH__", TI.getIntMaxType(), TI, Builder); @@ -364,6 +395,8 @@ static void InitializePredefinedMacros(const TargetInfo &TI, DefineType("__WINT_TYPE__", TI.getWIntType(), Builder); DefineTypeWidth("__WINT_WIDTH__", TI.getWIntType(), TI, Builder); DefineTypeWidth("__SIG_ATOMIC_WIDTH__", TI.getSigAtomicType(), TI, Builder); + DefineType("__CHAR16_TYPE__", TI.getChar16Type(), Builder); + DefineType("__CHAR32_TYPE__", TI.getChar32Type(), Builder); DefineFloatMacros(Builder, "FLT", &TI.getFloatFormat()); DefineFloatMacros(Builder, "DBL", &TI.getDoubleFormat()); diff --git a/contrib/llvm/tools/clang/lib/Frontend/Makefile b/contrib/llvm/tools/clang/lib/Frontend/Makefile index 9e1a864..3eb4bc9 100644 --- a/contrib/llvm/tools/clang/lib/Frontend/Makefile +++ b/contrib/llvm/tools/clang/lib/Frontend/Makefile @@ -7,11 +7,9 @@ # ##===----------------------------------------------------------------------===## -LEVEL = ../../../.. +CLANG_LEVEL := ../.. LIBRARYNAME := clangFrontend BUILD_ARCHIVE = 1 -CPP.Flags += -I$(PROJ_SRC_DIR)/../../include -I$(PROJ_OBJ_DIR)/../../include - -include $(LEVEL)/Makefile.common +include $(CLANG_LEVEL)/Makefile diff --git a/contrib/llvm/tools/clang/lib/Frontend/PCHReader.cpp b/contrib/llvm/tools/clang/lib/Frontend/PCHReader.cpp index 88e9b9d..00aee49 100644 --- a/contrib/llvm/tools/clang/lib/Frontend/PCHReader.cpp +++ b/contrib/llvm/tools/clang/lib/Frontend/PCHReader.cpp @@ -13,6 +13,7 @@ #include "clang/Frontend/PCHReader.h" #include "clang/Frontend/FrontendDiagnostic.h" +#include "clang/Frontend/PCHDeserializationListener.h" #include "clang/Frontend/Utils.h" #include "../Sema/Sema.h" // FIXME: move Sema headers elsewhere #include "clang/AST/ASTConsumer.h" @@ -93,7 +94,7 @@ PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) { PARSE_LANGOPT_IMPORTANT(Blocks, diag::warn_pch_blocks); PARSE_LANGOPT_BENIGN(EmitAllDecls); PARSE_LANGOPT_IMPORTANT(MathErrno, diag::warn_pch_math_errno); - PARSE_LANGOPT_IMPORTANT(OverflowChecking, diag::warn_pch_overflow_checking); + PARSE_LANGOPT_BENIGN(getSignedOverflowBehavior()); PARSE_LANGOPT_IMPORTANT(HeinousExtensions, diag::warn_pch_heinous_extensions); // FIXME: Most of the options below are benign if the macro wasn't @@ -124,6 +125,7 @@ PCHValidator::ReadLanguageOptions(const LangOptions &LangOpts) { PARSE_LANGOPT_IMPORTANT(OpenCL, diag::warn_pch_opencl); PARSE_LANGOPT_BENIGN(CatchUndefined); PARSE_LANGOPT_IMPORTANT(ElideConstructors, diag::warn_pch_elide_constructors); + PARSE_LANGOPT_BENIGN(SpellChecking); #undef PARSE_LANGOPT_IMPORTANT #undef PARSE_LANGOPT_BENIGN @@ -139,8 +141,86 @@ bool PCHValidator::ReadTargetTriple(llvm::StringRef Triple) { return true; } -bool PCHValidator::ReadPredefinesBuffer(llvm::StringRef PCHPredef, - FileID PCHBufferID, +struct EmptyStringRef { + bool operator ()(llvm::StringRef r) const { return r.empty(); } +}; +struct EmptyBlock { + bool operator ()(const PCHPredefinesBlock &r) const { return r.Data.empty(); } +}; + +static bool EqualConcatenations(llvm::SmallVector<llvm::StringRef, 2> L, + PCHPredefinesBlocks R) { + // First, sum up the lengths. + unsigned LL = 0, RL = 0; + for (unsigned I = 0, N = L.size(); I != N; ++I) { + LL += L[I].size(); + } + for (unsigned I = 0, N = R.size(); I != N; ++I) { + RL += R[I].Data.size(); + } + if (LL != RL) + return false; + if (LL == 0 && RL == 0) + return true; + + // Kick out empty parts, they confuse the algorithm below. + L.erase(std::remove_if(L.begin(), L.end(), EmptyStringRef()), L.end()); + R.erase(std::remove_if(R.begin(), R.end(), EmptyBlock()), R.end()); + + // Do it the hard way. At this point, both vectors must be non-empty. + llvm::StringRef LR = L[0], RR = R[0].Data; + unsigned LI = 0, RI = 0, LN = L.size(), RN = R.size(); + for (;;) { + // Compare the current pieces. + if (LR.size() == RR.size()) { + // If they're the same length, it's pretty easy. + if (LR != RR) + return false; + // Both pieces are done, advance. + ++LI; + ++RI; + // If either string is done, they're both done, since they're the same + // length. + if (LI == LN) { + assert(RI == RN && "Strings not the same length after all?"); + return true; + } + LR = L[LI]; + RR = R[RI].Data; + } else if (LR.size() < RR.size()) { + // Right piece is longer. + if (!RR.startswith(LR)) + return false; + ++LI; + assert(LI != LN && "Strings not the same length after all?"); + RR = RR.substr(LR.size()); + LR = L[LI]; + } else { + // Left piece is longer. + if (!LR.startswith(RR)) + return false; + ++RI; + assert(RI != RN && "Strings not the same length after all?"); + LR = LR.substr(RR.size()); + RR = R[RI].Data; + } + } +} + +static std::pair<FileID, llvm::StringRef::size_type> +FindMacro(const PCHPredefinesBlocks &Buffers, llvm::StringRef MacroDef) { + std::pair<FileID, llvm::StringRef::size_type> Res; + for (unsigned I = 0, N = Buffers.size(); I != N; ++I) { + Res.second = Buffers[I].Data.find(MacroDef); + if (Res.second != llvm::StringRef::npos) { + Res.first = Buffers[I].BufferID; + break; + } + } + return Res; +} + +bool PCHValidator::ReadPredefinesBuffer(const PCHPredefinesBlocks &Buffers, llvm::StringRef OriginalFileName, std::string &SuggestedPredefines) { // We are in the context of an implicit include, so the predefines buffer will @@ -159,9 +239,15 @@ bool PCHValidator::ReadPredefinesBuffer(llvm::StringRef PCHPredef, return true; } - // If the predefines is equal to the joined left and right halves, we're done! - if (Left.size() + Right.size() == PCHPredef.size() && - PCHPredef.startswith(Left) && PCHPredef.endswith(Right)) + // If the concatenation of all the PCH buffers is equal to the adjusted + // command line, we're done. + // We build a SmallVector of the command line here, because we'll eventually + // need to support an arbitrary amount of pieces anyway (when we have chained + // PCH reading). + llvm::SmallVector<llvm::StringRef, 2> CommandLine; + CommandLine.push_back(Left); + CommandLine.push_back(Right); + if (EqualConcatenations(CommandLine, Buffers)) return false; SourceManager &SourceMgr = PP.getSourceManager(); @@ -169,7 +255,8 @@ bool PCHValidator::ReadPredefinesBuffer(llvm::StringRef PCHPredef, // The predefines buffers are different. Determine what the differences are, // and whether they require us to reject the PCH file. llvm::SmallVector<llvm::StringRef, 8> PCHLines; - PCHPredef.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false); + for (unsigned I = 0, N = Buffers.size(); I != N; ++I) + Buffers[I].Data.split(PCHLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false); llvm::SmallVector<llvm::StringRef, 8> CmdLineLines; Left.split(CmdLineLines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false); @@ -234,10 +321,12 @@ bool PCHValidator::ReadPredefinesBuffer(llvm::StringRef PCHPredef, << MacroName; // Show the definition of this macro within the PCH file. - llvm::StringRef::size_type Offset = PCHPredef.find(Missing); - assert(Offset != llvm::StringRef::npos && "Unable to find macro!"); - SourceLocation PCHMissingLoc = SourceMgr.getLocForStartOfFile(PCHBufferID) - .getFileLocWithOffset(Offset); + std::pair<FileID, llvm::StringRef::size_type> MacroLoc = + FindMacro(Buffers, Missing); + assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!"); + SourceLocation PCHMissingLoc = + SourceMgr.getLocForStartOfFile(MacroLoc.first) + .getFileLocWithOffset(MacroLoc.second); Reader.Diag(PCHMissingLoc, diag::note_pch_macro_defined_as) << MacroName; ConflictingDefines = true; @@ -255,10 +344,12 @@ bool PCHValidator::ReadPredefinesBuffer(llvm::StringRef PCHPredef, } // Show the definition of this macro within the PCH file. - llvm::StringRef::size_type Offset = PCHPredef.find(Missing); - assert(Offset != llvm::StringRef::npos && "Unable to find macro!"); - SourceLocation PCHMissingLoc = SourceMgr.getLocForStartOfFile(PCHBufferID) - .getFileLocWithOffset(Offset); + std::pair<FileID, llvm::StringRef::size_type> MacroLoc = + FindMacro(Buffers, Missing); + assert(MacroLoc.second!=llvm::StringRef::npos && "Unable to find macro!"); + SourceLocation PCHMissingLoc = + SourceMgr.getLocForStartOfFile(MacroLoc.first) + .getFileLocWithOffset(MacroLoc.second); Reader.Diag(PCHMissingLoc, diag::note_using_macro_def_from_pch); } @@ -323,10 +414,10 @@ void PCHValidator::ReadCounter(unsigned Value) { PCHReader::PCHReader(Preprocessor &PP, ASTContext *Context, const char *isysroot) - : Listener(new PCHValidator(PP, *this)), SourceMgr(PP.getSourceManager()), - FileMgr(PP.getFileManager()), Diags(PP.getDiagnostics()), - SemaObj(0), PP(&PP), Context(Context), StatCache(0), Consumer(0), - IdentifierTableData(0), IdentifierLookupTable(0), + : Listener(new PCHValidator(PP, *this)), DeserializationListener(0), + SourceMgr(PP.getSourceManager()), FileMgr(PP.getFileManager()), + Diags(PP.getDiagnostics()), SemaObj(0), PP(&PP), Context(Context), + StatCache(0), Consumer(0), IdentifierTableData(0), IdentifierLookupTable(0), IdentifierOffsets(0), MethodPoolLookupTable(0), MethodPoolLookupTableData(0), TotalSelectorsInMethodPool(0), SelectorOffsets(0), @@ -342,8 +433,8 @@ PCHReader::PCHReader(Preprocessor &PP, ASTContext *Context, PCHReader::PCHReader(SourceManager &SourceMgr, FileManager &FileMgr, Diagnostic &Diags, const char *isysroot) - : SourceMgr(SourceMgr), FileMgr(FileMgr), Diags(Diags), - SemaObj(0), PP(0), Context(0), StatCache(0), Consumer(0), + : DeserializationListener(0), SourceMgr(SourceMgr), FileMgr(FileMgr), + Diags(Diags), SemaObj(0), PP(0), Context(0), StatCache(0), Consumer(0), IdentifierTableData(0), IdentifierLookupTable(0), IdentifierOffsets(0), MethodPoolLookupTable(0), MethodPoolLookupTableData(0), @@ -360,14 +451,6 @@ PCHReader::PCHReader(SourceManager &SourceMgr, FileManager &FileMgr, PCHReader::~PCHReader() {} -Expr *PCHReader::ReadDeclExpr() { - return dyn_cast_or_null<Expr>(ReadStmt(DeclsCursor)); -} - -Expr *PCHReader::ReadTypeExpr() { - return dyn_cast_or_null<Expr>(ReadStmt(DeclsCursor)); -} - namespace { class PCHMethodPoolLookupTrait { @@ -616,27 +699,18 @@ void PCHReader::Error(const char *Msg) { Diag(diag::err_fe_pch_malformed) << Msg; } -/// \brief Check the contents of the predefines buffer against the -/// contents of the predefines buffer used to build the PCH file. -/// -/// The contents of the two predefines buffers should be the same. If -/// not, then some command-line option changed the preprocessor state -/// and we must reject the PCH file. -/// -/// \param PCHPredef The start of the predefines buffer in the PCH -/// file. -/// -/// \param PCHPredefLen The length of the predefines buffer in the PCH -/// file. +/// \brief Check the contents of the concatenation of all predefines buffers in +/// the PCH chain against the contents of the predefines buffer of the current +/// compiler invocation. /// -/// \param PCHBufferID The FileID for the PCH predefines buffer. +/// The contents should be the same. If not, then some command-line option +/// changed the preprocessor state and we must probably reject the PCH file. /// /// \returns true if there was a mismatch (in which case the PCH file /// should be ignored), or false otherwise. -bool PCHReader::CheckPredefinesBuffer(llvm::StringRef PCHPredef, - FileID PCHBufferID) { +bool PCHReader::CheckPredefinesBuffers() { if (Listener) - return Listener->ReadPredefinesBuffer(PCHPredef, PCHBufferID, + return Listener->ReadPredefinesBuffer(PCHPredefinesBuffers, ActualOriginalFileName, SuggestedPredefines); return false; @@ -667,16 +741,17 @@ bool PCHReader::ParseLineTable(llvm::SmallVectorImpl<uint64_t> &Record) { // Parse the line entries std::vector<LineEntry> Entries; while (Idx < Record.size()) { - int FID = FileIDs[Record[Idx++]]; + int FID = Record[Idx++]; // Extract the line entries unsigned NumEntries = Record[Idx++]; + assert(NumEntries && "Numentries is 00000"); Entries.clear(); Entries.reserve(NumEntries); for (unsigned I = 0; I != NumEntries; ++I) { unsigned FileOffset = Record[Idx++]; unsigned LineNo = Record[Idx++]; - int FilenameID = Record[Idx++]; + int FilenameID = FileIDs[Record[Idx++]]; SrcMgr::CharacteristicKind FileKind = (SrcMgr::CharacteristicKind)Record[Idx++]; unsigned IncludeOffset = Record[Idx++]; @@ -964,9 +1039,11 @@ PCHReader::PCHReadResult PCHReader::ReadSLocEntryRecord(unsigned ID) { FileID BufferID = SourceMgr.createFileIDForMemBuffer(Buffer, ID, Offset); if (strcmp(Name, "<built-in>") == 0) { - PCHPredefinesBufferID = BufferID; - PCHPredefines = BlobStart; - PCHPredefinesLen = BlobLen - 1; + PCHPredefinesBlock Block = { + BufferID, + llvm::StringRef(BlobStart, BlobLen - 1) + }; + PCHPredefinesBuffers.push_back(Block); } break; @@ -1512,6 +1589,22 @@ PCHReader::ReadPCHBlock() { ExtVectorDecls.swap(Record); break; + case pch::VTABLE_USES: + if (!VTableUses.empty()) { + Error("duplicate VTABLE_USES record in PCH file"); + return Failure; + } + VTableUses.swap(Record); + break; + + case pch::DYNAMIC_CLASSES: + if (!DynamicClasses.empty()) { + Error("duplicate DYNAMIC_CLASSES record in PCH file"); + return Failure; + } + DynamicClasses.swap(Record); + break; + case pch::ORIGINAL_FILE_NAME: ActualOriginalFileName.assign(BlobStart, BlobLen); OriginalFileName = ActualOriginalFileName; @@ -1626,8 +1719,7 @@ PCHReader::PCHReadResult PCHReader::ReadPCH(const std::string &FileName) { } // Check the predefines buffer. - if (CheckPredefinesBuffer(llvm::StringRef(PCHPredefines, PCHPredefinesLen), - PCHPredefinesBufferID)) + if (CheckPredefinesBuffers()) return IgnorePCH; if (PP) { @@ -1693,7 +1785,7 @@ void PCHReader::InitializeContext(ASTContext &Ctx) { PP->setExternalSource(this); // Load the translation unit declaration - ReadDeclRecord(DeclOffsets[0], 0); + GetTranslationUnitDecl(); // Load the special types. Context->setBuiltinVaListType( @@ -1776,6 +1868,9 @@ void PCHReader::InitializeContext(ASTContext &Ctx) { Context->ObjCSelRedefinitionType = GetType(ObjCSelRedef); if (unsigned String = SpecialTypes[pch::SPECIAL_TYPE_NS_CONSTANT_STRING]) Context->setNSConstantStringType(GetType(String)); + + if (SpecialTypes[pch::SPECIAL_TYPE_INT128_INSTALLED]) + Context->setInt128Installed(); } /// \brief Retrieve the name of the original source file name @@ -1915,7 +2010,8 @@ bool PCHReader::ParseLanguageOptions( PARSE_LANGOPT(Blocks); PARSE_LANGOPT(EmitAllDecls); PARSE_LANGOPT(MathErrno); - PARSE_LANGOPT(OverflowChecking); + LangOpts.setSignedOverflowBehavior((LangOptions::SignedOverflowBehaviorTy) + Record[Idx++]); PARSE_LANGOPT(HeinousExtensions); PARSE_LANGOPT(Optimize); PARSE_LANGOPT(OptimizeSize); @@ -1926,13 +2022,10 @@ bool PCHReader::ParseLanguageOptions( PARSE_LANGOPT(AccessControl); PARSE_LANGOPT(CharIsSigned); PARSE_LANGOPT(ShortWChar); - LangOpts.setGCMode((LangOptions::GCMode)Record[Idx]); - ++Idx; - LangOpts.setVisibilityMode((LangOptions::VisibilityMode)Record[Idx]); - ++Idx; + LangOpts.setGCMode((LangOptions::GCMode)Record[Idx++]); + LangOpts.setVisibilityMode((LangOptions::VisibilityMode)Record[Idx++]); LangOpts.setStackProtectorMode((LangOptions::StackProtectorMode) - Record[Idx]); - ++Idx; + Record[Idx++]); PARSE_LANGOPT(InstantiationDepth); PARSE_LANGOPT(OpenCL); PARSE_LANGOPT(CatchUndefined); @@ -1959,6 +2052,8 @@ QualType PCHReader::ReadTypeRecord(uint64_t Offset) { // after reading this type. SavedStreamPosition SavedPosition(DeclsCursor); + ReadingKindTracker ReadingKind(Read_Type, *this); + // Note that we are loading a type record. LoadingTypeOrDecl Loading(*this); @@ -2022,7 +2117,7 @@ QualType PCHReader::ReadTypeRecord(uint64_t Offset) { } case pch::TYPE_MEMBER_POINTER: { - if (Record.size() != 1) { + if (Record.size() != 2) { Error("Incorrect encoding of member pointer type"); return QualType(); } @@ -2054,26 +2149,26 @@ QualType PCHReader::ReadTypeRecord(uint64_t Offset) { unsigned IndexTypeQuals = Record[2]; SourceLocation LBLoc = SourceLocation::getFromRawEncoding(Record[3]); SourceLocation RBLoc = SourceLocation::getFromRawEncoding(Record[4]); - return Context->getVariableArrayType(ElementType, ReadTypeExpr(), + return Context->getVariableArrayType(ElementType, ReadExpr(), ASM, IndexTypeQuals, SourceRange(LBLoc, RBLoc)); } case pch::TYPE_VECTOR: { - if (Record.size() != 4) { + if (Record.size() != 3) { Error("incorrect encoding of vector type in PCH file"); return QualType(); } QualType ElementType = GetType(Record[0]); unsigned NumElements = Record[1]; - bool AltiVec = Record[2]; - bool Pixel = Record[3]; - return Context->getVectorType(ElementType, NumElements, AltiVec, Pixel); + unsigned AltiVecSpec = Record[2]; + return Context->getVectorType(ElementType, NumElements, + (VectorType::AltiVecSpecific)AltiVecSpec); } case pch::TYPE_EXT_VECTOR: { - if (Record.size() != 4) { + if (Record.size() != 3) { Error("incorrect encoding of extended vector type in PCH file"); return QualType(); } @@ -2123,15 +2218,18 @@ QualType PCHReader::ReadTypeRecord(uint64_t Offset) { return Context->getTypeDeclType( cast<UnresolvedUsingTypenameDecl>(GetDecl(Record[0]))); - case pch::TYPE_TYPEDEF: - if (Record.size() != 1) { + case pch::TYPE_TYPEDEF: { + if (Record.size() != 2) { Error("incorrect encoding of typedef type"); return QualType(); } - return Context->getTypeDeclType(cast<TypedefDecl>(GetDecl(Record[0]))); + TypedefDecl *Decl = cast<TypedefDecl>(GetDecl(Record[0])); + QualType Canonical = GetType(Record[1]); + return Context->getTypedefType(Decl, Canonical); + } case pch::TYPE_TYPEOF_EXPR: - return Context->getTypeOfExprType(ReadTypeExpr()); + return Context->getTypeOfExprType(ReadExpr()); case pch::TYPE_TYPEOF: { if (Record.size() != 1) { @@ -2143,32 +2241,36 @@ QualType PCHReader::ReadTypeRecord(uint64_t Offset) { } case pch::TYPE_DECLTYPE: - return Context->getDecltypeType(ReadTypeExpr()); + return Context->getDecltypeType(ReadExpr()); - case pch::TYPE_RECORD: - if (Record.size() != 1) { + case pch::TYPE_RECORD: { + if (Record.size() != 2) { Error("incorrect encoding of record type"); return QualType(); } - return Context->getTypeDeclType(cast<RecordDecl>(GetDecl(Record[0]))); + bool IsDependent = Record[0]; + QualType T = Context->getRecordType(cast<RecordDecl>(GetDecl(Record[1]))); + T->Dependent = IsDependent; + return T; + } - case pch::TYPE_ENUM: - if (Record.size() != 1) { + case pch::TYPE_ENUM: { + if (Record.size() != 2) { Error("incorrect encoding of enum type"); return QualType(); } - return Context->getTypeDeclType(cast<EnumDecl>(GetDecl(Record[0]))); + bool IsDependent = Record[0]; + QualType T = Context->getEnumType(cast<EnumDecl>(GetDecl(Record[1]))); + T->Dependent = IsDependent; + return T; + } case pch::TYPE_ELABORATED: { - if (Record.size() != 2) { - Error("incorrect encoding of elaborated type"); - return QualType(); - } - unsigned Tag = Record[1]; - // FIXME: Deserialize the qualifier (C++ only) - return Context->getElaboratedType((ElaboratedTypeKeyword) Tag, - /* NNS */ 0, - GetType(Record[0])); + unsigned Idx = 0; + ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++]; + NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx); + QualType NamedType = GetType(Record[Idx++]); + return Context->getElaboratedType(Keyword, NNS, NamedType); } case pch::TYPE_OBJC_INTERFACE: { @@ -2205,7 +2307,77 @@ QualType PCHReader::ReadTypeRecord(uint64_t Offset) { case pch::TYPE_INJECTED_CLASS_NAME: { CXXRecordDecl *D = cast<CXXRecordDecl>(GetDecl(Record[0])); QualType TST = GetType(Record[1]); // probably derivable - return Context->getInjectedClassNameType(D, TST); + // FIXME: ASTContext::getInjectedClassNameType is not currently suitable + // for PCH reading, too much interdependencies. + return + QualType(new (*Context, TypeAlignment) InjectedClassNameType(D, TST), 0); + } + + case pch::TYPE_TEMPLATE_TYPE_PARM: { + unsigned Idx = 0; + unsigned Depth = Record[Idx++]; + unsigned Index = Record[Idx++]; + bool Pack = Record[Idx++]; + IdentifierInfo *Name = GetIdentifierInfo(Record, Idx); + return Context->getTemplateTypeParmType(Depth, Index, Pack, Name); + } + + case pch::TYPE_DEPENDENT_NAME: { + unsigned Idx = 0; + ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++]; + NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx); + const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx); + QualType Canon = GetType(Record[Idx++]); + return Context->getDependentNameType(Keyword, NNS, Name, Canon); + } + + case pch::TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION: { + unsigned Idx = 0; + ElaboratedTypeKeyword Keyword = (ElaboratedTypeKeyword)Record[Idx++]; + NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx); + const IdentifierInfo *Name = this->GetIdentifierInfo(Record, Idx); + unsigned NumArgs = Record[Idx++]; + llvm::SmallVector<TemplateArgument, 8> Args; + Args.reserve(NumArgs); + while (NumArgs--) + Args.push_back(ReadTemplateArgument(Record, Idx)); + return Context->getDependentTemplateSpecializationType(Keyword, NNS, Name, + Args.size(), Args.data()); + } + + case pch::TYPE_DEPENDENT_SIZED_ARRAY: { + unsigned Idx = 0; + + // ArrayType + QualType ElementType = GetType(Record[Idx++]); + ArrayType::ArraySizeModifier ASM + = (ArrayType::ArraySizeModifier)Record[Idx++]; + unsigned IndexTypeQuals = Record[Idx++]; + + // DependentSizedArrayType + Expr *NumElts = ReadExpr(); + SourceRange Brackets = ReadSourceRange(Record, Idx); + + return Context->getDependentSizedArrayType(ElementType, NumElts, ASM, + IndexTypeQuals, Brackets); + } + + case pch::TYPE_TEMPLATE_SPECIALIZATION: { + unsigned Idx = 0; + bool IsDependent = Record[Idx++]; + TemplateName Name = ReadTemplateName(Record, Idx); + llvm::SmallVector<TemplateArgument, 8> Args; + ReadTemplateArgumentList(Args, Record, Idx); + QualType Canon = GetType(Record[Idx++]); + QualType T; + if (Canon.isNull()) + T = Context->getCanonicalTemplateSpecializationType(Name, Args.data(), + Args.size()); + else + T = Context->getTemplateSpecializationType(Name, Args.data(), + Args.size(), Canon); + T->Dependent = IsDependent; + return T; } } // Suppress a GCC warning @@ -2272,7 +2444,7 @@ void TypeLocReader::VisitArrayTypeLoc(ArrayTypeLoc TL) { TL.setLBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); TL.setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); if (Record[Idx++]) - TL.setSizeExpr(Reader.ReadDeclExpr()); + TL.setSizeExpr(Reader.ReadExpr()); else TL.setSizeExpr(0); } @@ -2367,6 +2539,18 @@ void TypeLocReader::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { TL.setQualifierRange(Reader.ReadSourceRange(Record, Idx)); TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); } +void TypeLocReader::VisitDependentTemplateSpecializationTypeLoc( + DependentTemplateSpecializationTypeLoc TL) { + TL.setKeywordLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); + TL.setQualifierRange(Reader.ReadSourceRange(Record, Idx)); + TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); + TL.setLAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); + TL.setRAngleLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); + for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) + TL.setArgLocInfo(I, + Reader.GetTemplateArgumentLocInfo(TL.getTypePtr()->getArg(I).getKind(), + Record, Idx)); +} void TypeLocReader::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { TL.setNameLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); } @@ -2443,8 +2627,12 @@ QualType PCHReader::GetType(pch::TypeID ID) { Index -= pch::NUM_PREDEF_TYPE_IDS; //assert(Index < TypesLoaded.size() && "Type index out-of-range"); - if (TypesLoaded[Index].isNull()) + if (TypesLoaded[Index].isNull()) { TypesLoaded[Index] = ReadTypeRecord(TypeOffsets[Index]); + TypesLoaded[Index]->setFromPCH(); + if (DeserializationListener) + DeserializationListener->TypeRead(ID, TypesLoaded[Index]); + } return TypesLoaded[Index].withFastQualifiers(FastQuals); } @@ -2455,16 +2643,13 @@ PCHReader::GetTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind, unsigned &Index) { switch (Kind) { case TemplateArgument::Expression: - return ReadDeclExpr(); + return ReadExpr(); case TemplateArgument::Type: return GetTypeSourceInfo(Record, Index); case TemplateArgument::Template: { - SourceLocation - QualStart = SourceLocation::getFromRawEncoding(Record[Index++]), - QualEnd = SourceLocation::getFromRawEncoding(Record[Index++]), - TemplateNameLoc = SourceLocation::getFromRawEncoding(Record[Index++]); - return TemplateArgumentLocInfo(SourceRange(QualStart, QualEnd), - TemplateNameLoc); + SourceRange QualifierRange = ReadSourceRange(Record, Index); + SourceLocation TemplateNameLoc = ReadSourceLocation(Record, Index); + return TemplateArgumentLocInfo(QualifierRange, TemplateNameLoc); } case TemplateArgument::Null: case TemplateArgument::Integral: @@ -2476,6 +2661,32 @@ PCHReader::GetTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind, return TemplateArgumentLocInfo(); } +TemplateArgumentLoc +PCHReader::ReadTemplateArgumentLoc(const RecordData &Record, unsigned &Index) { + TemplateArgument Arg = ReadTemplateArgument(Record, Index); + + if (Arg.getKind() == TemplateArgument::Expression) { + if (Record[Index++]) // bool InfoHasSameExpr. + return TemplateArgumentLoc(Arg, TemplateArgumentLocInfo(Arg.getAsExpr())); + } + return TemplateArgumentLoc(Arg, GetTemplateArgumentLocInfo(Arg.getKind(), + Record, Index)); +} + +Decl *PCHReader::GetExternalDecl(uint32_t ID) { + return GetDecl(ID); +} + +TranslationUnitDecl *PCHReader::GetTranslationUnitDecl() { + if (!DeclsLoaded[0]) { + ReadDeclRecord(DeclOffsets[0], 0); + if (DeserializationListener) + DeserializationListener->DeclRead(0, DeclsLoaded[0]); + } + + return cast<TranslationUnitDecl>(DeclsLoaded[0]); +} + Decl *PCHReader::GetDecl(pch::DeclID ID) { if (ID == 0) return 0; @@ -2486,8 +2697,11 @@ Decl *PCHReader::GetDecl(pch::DeclID ID) { } unsigned Index = ID - 1; - if (!DeclsLoaded[Index]) + if (!DeclsLoaded[Index]) { ReadDeclRecord(DeclOffsets[Index], Index); + if (DeserializationListener) + DeserializationListener->DeclRead(ID, DeclsLoaded[Index]); + } return DeclsLoaded[Index]; } @@ -2497,15 +2711,15 @@ Decl *PCHReader::GetDecl(pch::DeclID ID) { /// This operation will read a new statement from the external /// source each time it is called, and is meant to be used via a /// LazyOffsetPtr (which is used by Decls for the body of functions, etc). -Stmt *PCHReader::GetDeclStmt(uint64_t Offset) { +Stmt *PCHReader::GetExternalDeclStmt(uint64_t Offset) { // Since we know tha this statement is part of a decl, make sure to use the // decl cursor to read it. DeclsCursor.JumpToBit(Offset); - return ReadStmt(DeclsCursor); + return ReadStmtFromStream(DeclsCursor); } -bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC, - llvm::SmallVectorImpl<pch::DeclID> &Decls) { +bool PCHReader::FindExternalLexicalDecls(const DeclContext *DC, + llvm::SmallVectorImpl<Decl*> &Decls) { assert(DC->hasExternalLexicalStorage() && "DeclContext has no lexical decls in storage"); @@ -2531,20 +2745,22 @@ bool PCHReader::ReadDeclsLexicallyInContext(DeclContext *DC, } // Load all of the declaration IDs - Decls.clear(); - Decls.insert(Decls.end(), Record.begin(), Record.end()); + for (RecordData::iterator I = Record.begin(), E = Record.end(); I != E; ++I) + Decls.push_back(GetDecl(*I)); ++NumLexicalDeclContextsRead; return false; } -bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC, - llvm::SmallVectorImpl<VisibleDeclaration> &Decls) { +DeclContext::lookup_result +PCHReader::FindExternalVisibleDeclsByName(const DeclContext *DC, + DeclarationName Name) { assert(DC->hasExternalVisibleStorage() && "DeclContext has no visible decls in storage"); uint64_t Offset = DeclContextOffsets[DC].second; if (Offset == 0) { Error("DeclContext has no visible decls in storage"); - return true; + return DeclContext::lookup_result(DeclContext::lookup_iterator(), + DeclContext::lookup_iterator()); } // Keep track of where we are in the stream, then jump back there @@ -2559,13 +2775,16 @@ bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC, unsigned RecCode = DeclsCursor.ReadRecord(Code, Record); if (RecCode != pch::DECL_CONTEXT_VISIBLE) { Error("Expected visible block"); - return true; + return DeclContext::lookup_result(DeclContext::lookup_iterator(), + DeclContext::lookup_iterator()); } - if (Record.size() == 0) - return false; - - Decls.clear(); + llvm::SmallVector<VisibleDeclaration, 64> Decls; + if (Record.empty()) { + SetExternalVisibleDecls(DC, Decls); + return DeclContext::lookup_result(DeclContext::lookup_iterator(), + DeclContext::lookup_iterator()); + } unsigned Idx = 0; while (Idx < Record.size()) { @@ -2580,7 +2799,18 @@ bool PCHReader::ReadDeclsVisibleInContext(DeclContext *DC, } ++NumVisibleDeclContextsRead; - return false; + + SetExternalVisibleDecls(DC, Decls); + return const_cast<DeclContext*>(DC)->lookup(Name); +} + +void PCHReader::PassInterestingDeclsToConsumer() { + assert(Consumer); + while (!InterestingDecls.empty()) { + DeclGroupRef DG(InterestingDecls.front()); + InterestingDecls.pop_front(); + Consumer->HandleTopLevelDecl(DG); + } } void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) { @@ -2590,15 +2820,12 @@ void PCHReader::StartTranslationUnit(ASTConsumer *Consumer) { return; for (unsigned I = 0, N = ExternalDefinitions.size(); I != N; ++I) { - // Force deserialization of this decl, which will cause it to be passed to - // the consumer (or queued). + // Force deserialization of this decl, which will cause it to be queued for + // passing to the consumer. GetDecl(ExternalDefinitions[I]); } - for (unsigned I = 0, N = InterestingDecls.size(); I != N; ++I) { - DeclGroupRef DG(InterestingDecls[I]); - Consumer->HandleTopLevelDecl(DG); - } + PassInterestingDeclsToConsumer(); } void PCHReader::PrintStats() { @@ -2708,6 +2935,26 @@ void PCHReader::InitializeSema(Sema &S) { for (unsigned I = 0, N = ExtVectorDecls.size(); I != N; ++I) SemaObj->ExtVectorDecls.push_back( cast<TypedefDecl>(GetDecl(ExtVectorDecls[I]))); + + // FIXME: Do VTable uses and dynamic classes deserialize too much ? + // Can we cut them down before writing them ? + + // If there were any VTable uses, deserialize the information and add it + // to Sema's vector and map of VTable uses. + unsigned Idx = 0; + for (unsigned I = 0, N = VTableUses[Idx++]; I != N; ++I) { + CXXRecordDecl *Class = cast<CXXRecordDecl>(GetDecl(VTableUses[Idx++])); + SourceLocation Loc = ReadSourceLocation(VTableUses, Idx); + bool DefinitionRequired = VTableUses[Idx++]; + SemaObj->VTableUses.push_back(std::make_pair(Class, Loc)); + SemaObj->VTablesUsed[Class] = DefinitionRequired; + } + + // If there were any dynamic classes declarations, deserialize them + // and add them to Sema's vector of such declarations. + for (unsigned I = 0, N = DynamicClasses.size(); I != N; ++I) + SemaObj->DynamicClasses.push_back( + cast<CXXRecordDecl>(GetDecl(DynamicClasses[I]))); } IdentifierInfo* PCHReader::get(const char *NameStart, const char *NameEnd) { @@ -2853,11 +3100,11 @@ Selector PCHReader::DecodeSelector(unsigned ID) { return SelectorsLoaded[Index]; } -Selector PCHReader::GetSelector(uint32_t ID) { +Selector PCHReader::GetExternalSelector(uint32_t ID) { return DecodeSelector(ID); } -uint32_t PCHReader::GetNumKnownSelectors() { +uint32_t PCHReader::GetNumExternalSelectors() { return TotalNumSelectors + 1; } @@ -2901,6 +3148,126 @@ PCHReader::ReadDeclarationName(const RecordData &Record, unsigned &Idx) { return DeclarationName(); } +TemplateName +PCHReader::ReadTemplateName(const RecordData &Record, unsigned &Idx) { + TemplateName::NameKind Kind = (TemplateName::NameKind)Record[Idx++]; + switch (Kind) { + case TemplateName::Template: + return TemplateName(cast_or_null<TemplateDecl>(GetDecl(Record[Idx++]))); + + case TemplateName::OverloadedTemplate: { + unsigned size = Record[Idx++]; + UnresolvedSet<8> Decls; + while (size--) + Decls.addDecl(cast<NamedDecl>(GetDecl(Record[Idx++]))); + + return Context->getOverloadedTemplateName(Decls.begin(), Decls.end()); + } + + case TemplateName::QualifiedTemplate: { + NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx); + bool hasTemplKeyword = Record[Idx++]; + TemplateDecl *Template = cast<TemplateDecl>(GetDecl(Record[Idx++])); + return Context->getQualifiedTemplateName(NNS, hasTemplKeyword, Template); + } + + case TemplateName::DependentTemplate: { + NestedNameSpecifier *NNS = ReadNestedNameSpecifier(Record, Idx); + if (Record[Idx++]) // isIdentifier + return Context->getDependentTemplateName(NNS, + GetIdentifierInfo(Record, Idx)); + return Context->getDependentTemplateName(NNS, + (OverloadedOperatorKind)Record[Idx++]); + } + } + + assert(0 && "Unhandled template name kind!"); + return TemplateName(); +} + +TemplateArgument +PCHReader::ReadTemplateArgument(const RecordData &Record, unsigned &Idx) { + switch ((TemplateArgument::ArgKind)Record[Idx++]) { + case TemplateArgument::Null: + return TemplateArgument(); + case TemplateArgument::Type: + return TemplateArgument(GetType(Record[Idx++])); + case TemplateArgument::Declaration: + return TemplateArgument(GetDecl(Record[Idx++])); + case TemplateArgument::Integral: { + llvm::APSInt Value = ReadAPSInt(Record, Idx); + QualType T = GetType(Record[Idx++]); + return TemplateArgument(Value, T); + } + case TemplateArgument::Template: + return TemplateArgument(ReadTemplateName(Record, Idx)); + case TemplateArgument::Expression: + return TemplateArgument(ReadExpr()); + case TemplateArgument::Pack: { + unsigned NumArgs = Record[Idx++]; + llvm::SmallVector<TemplateArgument, 8> Args; + Args.reserve(NumArgs); + while (NumArgs--) + Args.push_back(ReadTemplateArgument(Record, Idx)); + TemplateArgument TemplArg; + TemplArg.setArgumentPack(Args.data(), Args.size(), /*CopyArgs=*/true); + return TemplArg; + } + } + + assert(0 && "Unhandled template argument kind!"); + return TemplateArgument(); +} + +TemplateParameterList * +PCHReader::ReadTemplateParameterList(const RecordData &Record, unsigned &Idx) { + SourceLocation TemplateLoc = ReadSourceLocation(Record, Idx); + SourceLocation LAngleLoc = ReadSourceLocation(Record, Idx); + SourceLocation RAngleLoc = ReadSourceLocation(Record, Idx); + + unsigned NumParams = Record[Idx++]; + llvm::SmallVector<NamedDecl *, 16> Params; + Params.reserve(NumParams); + while (NumParams--) + Params.push_back(cast<NamedDecl>(GetDecl(Record[Idx++]))); + + TemplateParameterList* TemplateParams = + TemplateParameterList::Create(*Context, TemplateLoc, LAngleLoc, + Params.data(), Params.size(), RAngleLoc); + return TemplateParams; +} + +void +PCHReader:: +ReadTemplateArgumentList(llvm::SmallVector<TemplateArgument, 8> &TemplArgs, + const RecordData &Record, unsigned &Idx) { + unsigned NumTemplateArgs = Record[Idx++]; + TemplArgs.reserve(NumTemplateArgs); + while (NumTemplateArgs--) + TemplArgs.push_back(ReadTemplateArgument(Record, Idx)); +} + +/// \brief Read a UnresolvedSet structure. +void PCHReader::ReadUnresolvedSet(UnresolvedSetImpl &Set, + const RecordData &Record, unsigned &Idx) { + unsigned NumDecls = Record[Idx++]; + while (NumDecls--) { + NamedDecl *D = cast<NamedDecl>(GetDecl(Record[Idx++])); + AccessSpecifier AS = (AccessSpecifier)Record[Idx++]; + Set.addDecl(D, AS); + } +} + +CXXBaseSpecifier +PCHReader::ReadCXXBaseSpecifier(const RecordData &Record, unsigned &Idx) { + bool isVirtual = static_cast<bool>(Record[Idx++]); + bool isBaseOfClass = static_cast<bool>(Record[Idx++]); + AccessSpecifier AS = static_cast<AccessSpecifier>(Record[Idx++]); + QualType T = GetType(Record[Idx++]); + SourceRange Range = ReadSourceRange(Record, Idx); + return CXXBaseSpecifier(Range, isVirtual, isBaseOfClass, AS, T); +} + NestedNameSpecifier * PCHReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) { unsigned N = Record[Idx++]; @@ -2934,16 +3301,17 @@ PCHReader::ReadNestedNameSpecifier(const RecordData &Record, unsigned &Idx) { // No associated value, and there can't be a prefix. break; } - Prev = NNS; } + Prev = NNS; } return NNS; } SourceRange PCHReader::ReadSourceRange(const RecordData &Record, unsigned &Idx) { - return SourceRange(SourceLocation::getFromRawEncoding(Record[Idx++]), - SourceLocation::getFromRawEncoding(Record[Idx++])); + SourceLocation beg = SourceLocation::getFromRawEncoding(Record[Idx++]); + SourceLocation end = SourceLocation::getFromRawEncoding(Record[Idx++]); + return SourceRange(beg, end); } /// \brief Read an integral value @@ -3090,6 +3458,11 @@ PCHReader::LoadingTypeOrDecl::~LoadingTypeOrDecl() { true); Reader.PendingIdentifierInfos.pop_front(); } + + // We are not in recursive loading, so it's safe to pass the "interesting" + // decls to the consumer. + if (Reader.Consumer) + Reader.PassInterestingDeclsToConsumer(); } Reader.CurrentlyLoadingTypeOrDecl = Parent; diff --git a/contrib/llvm/tools/clang/lib/Frontend/PCHReaderDecl.cpp b/contrib/llvm/tools/clang/lib/Frontend/PCHReaderDecl.cpp index 1ef0441..742f0e4 100644 --- a/contrib/llvm/tools/clang/lib/Frontend/PCHReaderDecl.cpp +++ b/contrib/llvm/tools/clang/lib/Frontend/PCHReaderDecl.cpp @@ -27,16 +27,19 @@ using namespace clang; // Declaration deserialization //===----------------------------------------------------------------------===// -namespace { +namespace clang { class PCHDeclReader : public DeclVisitor<PCHDeclReader, void> { PCHReader &Reader; const PCHReader::RecordData &Record; unsigned &Idx; + pch::TypeID TypeIDForTypeDecl; public: PCHDeclReader(PCHReader &Reader, const PCHReader::RecordData &Record, unsigned &Idx) - : Reader(Reader), Record(Record), Idx(Idx) { } + : Reader(Reader), Record(Record), Idx(Idx), TypeIDForTypeDecl(0) { } + + void Visit(Decl *D); void VisitDecl(Decl *D); void VisitTranslationUnitDecl(TranslationUnitDecl *TU); @@ -46,7 +49,7 @@ namespace { void VisitNamespaceAliasDecl(NamespaceAliasDecl *D); void VisitTypeDecl(TypeDecl *TD); void VisitTypedefDecl(TypedefDecl *TD); - void VisitUnresolvedUsingTypename(UnresolvedUsingTypenameDecl *D); + void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D); void VisitTagDecl(TagDecl *TD); void VisitEnumDecl(EnumDecl *ED); void VisitRecordDecl(RecordDecl *RD); @@ -58,7 +61,7 @@ namespace { void VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D); void VisitValueDecl(ValueDecl *VD); void VisitEnumConstantDecl(EnumConstantDecl *ECD); - void VisitUnresolvedUsingValue(UnresolvedUsingValueDecl *D); + void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D); void VisitDeclaratorDecl(DeclaratorDecl *DD); void VisitFunctionDecl(FunctionDecl *FD); void VisitCXXMethodDecl(CXXMethodDecl *D); @@ -72,19 +75,21 @@ namespace { void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D); void VisitTemplateDecl(TemplateDecl *D); void VisitClassTemplateDecl(ClassTemplateDecl *D); - void visitFunctionTemplateDecl(FunctionTemplateDecl *D); + void VisitFunctionTemplateDecl(FunctionTemplateDecl *D); void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D); - void VisitUsing(UsingDecl *D); - void VisitUsingShadow(UsingShadowDecl *D); + void VisitUsingDecl(UsingDecl *D); + void VisitUsingShadowDecl(UsingShadowDecl *D); void VisitLinkageSpecDecl(LinkageSpecDecl *D); void VisitFileScopeAsmDecl(FileScopeAsmDecl *AD); + void VisitAccessSpecDecl(AccessSpecDecl *D); + void VisitFriendDecl(FriendDecl *D); void VisitFriendTemplateDecl(FriendTemplateDecl *D); void VisitStaticAssertDecl(StaticAssertDecl *D); void VisitBlockDecl(BlockDecl *BD); std::pair<uint64_t, uint64_t> VisitDeclContext(DeclContext *DC); - // FIXME: Reorder according to DeclNodes.def? + // FIXME: Reorder according to DeclNodes.td? void VisitObjCMethodDecl(ObjCMethodDecl *D); void VisitObjCContainerDecl(ObjCContainerDecl *D); void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); @@ -103,6 +108,19 @@ namespace { }; } +void PCHDeclReader::Visit(Decl *D) { + DeclVisitor<PCHDeclReader, void>::Visit(D); + + if (TypeDecl *TD = dyn_cast<TypeDecl>(D)) { + // if we have a fully initialized TypeDecl, we can safely read its type now. + TD->setTypeForDecl(Reader.GetType(TypeIDForTypeDecl).getTypePtr()); + } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { + // FunctionDecl's body was written last after all other Stmts/Exprs. + if (Record[Idx++]) + FD->setLazyBody(Reader.getDeclsCursor().GetCurrentBitNo()); + } +} + void PCHDeclReader::VisitDecl(Decl *D) { D->setDeclContext(cast_or_null<DeclContext>(Reader.GetDecl(Record[Idx++]))); D->setLexicalDeclContext( @@ -110,7 +128,7 @@ void PCHDeclReader::VisitDecl(Decl *D) { D->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++])); D->setInvalidDecl(Record[Idx++]); if (Record[Idx++]) - D->addAttr(Reader.ReadAttributes()); + D->initAttrs(Reader.ReadAttributes()); D->setImplicit(Record[Idx++]); D->setUsed(Record[Idx++]); D->setAccess((AccessSpecifier)Record[Idx++]); @@ -130,21 +148,18 @@ void PCHDeclReader::VisitNamedDecl(NamedDecl *ND) { void PCHDeclReader::VisitTypeDecl(TypeDecl *TD) { VisitNamedDecl(TD); - TD->setTypeForDecl(Reader.GetType(Record[Idx++]).getTypePtr()); + // Delay type reading until after we have fully initialized the decl. + TypeIDForTypeDecl = Record[Idx++]; } void PCHDeclReader::VisitTypedefDecl(TypedefDecl *TD) { - // Note that we cannot use VisitTypeDecl here, because we need to - // set the underlying type of the typedef *before* we try to read - // the type associated with the TypedefDecl. - VisitNamedDecl(TD); - uint64_t TypeData = Record[Idx++]; + VisitTypeDecl(TD); TD->setTypeSourceInfo(Reader.GetTypeSourceInfo(Record, Idx)); - TD->setTypeForDecl(Reader.GetType(TypeData).getTypePtr()); } void PCHDeclReader::VisitTagDecl(TagDecl *TD) { VisitTypeDecl(TD); + TD->IdentifierNamespace = Record[Idx++]; TD->setPreviousDeclaration( cast_or_null<TagDecl>(Reader.GetDecl(Record[Idx++]))); TD->setTagKind((TagDecl::TagKind)Record[Idx++]); @@ -163,7 +178,8 @@ void PCHDeclReader::VisitEnumDecl(EnumDecl *ED) { ED->setPromotionType(Reader.GetType(Record[Idx++])); ED->setNumPositiveBits(Record[Idx++]); ED->setNumNegativeBits(Record[Idx++]); - // FIXME: C++ InstantiatedFrom + ED->setInstantiationOfMemberEnum( + cast_or_null<EnumDecl>(Reader.GetDecl(Record[Idx++]))); } void PCHDeclReader::VisitRecordDecl(RecordDecl *RD) { @@ -181,7 +197,7 @@ void PCHDeclReader::VisitValueDecl(ValueDecl *VD) { void PCHDeclReader::VisitEnumConstantDecl(EnumConstantDecl *ECD) { VisitValueDecl(ECD); if (Record[Idx++]) - ECD->setInitExpr(Reader.ReadDeclExpr()); + ECD->setInitExpr(Reader.ReadExpr()); ECD->setInitVal(Reader.ReadAPSInt(Record, Idx)); } @@ -195,9 +211,83 @@ void PCHDeclReader::VisitDeclaratorDecl(DeclaratorDecl *DD) { void PCHDeclReader::VisitFunctionDecl(FunctionDecl *FD) { VisitDeclaratorDecl(FD); - if (Record[Idx++]) - FD->setLazyBody(Reader.getDeclsCursor().GetCurrentBitNo()); - FD->setPreviousDeclaration( + + FD->IdentifierNamespace = Record[Idx++]; + switch ((FunctionDecl::TemplatedKind)Record[Idx++]) { + default: assert(false && "Unhandled TemplatedKind!"); + break; + case FunctionDecl::TK_NonTemplate: + break; + case FunctionDecl::TK_FunctionTemplate: + FD->setDescribedFunctionTemplate( + cast<FunctionTemplateDecl>(Reader.GetDecl(Record[Idx++]))); + break; + case FunctionDecl::TK_MemberSpecialization: { + FunctionDecl *InstFD = cast<FunctionDecl>(Reader.GetDecl(Record[Idx++])); + TemplateSpecializationKind TSK = (TemplateSpecializationKind)Record[Idx++]; + SourceLocation POI = Reader.ReadSourceLocation(Record, Idx); + FD->setInstantiationOfMemberFunction(InstFD, TSK); + FD->getMemberSpecializationInfo()->setPointOfInstantiation(POI); + break; + } + case FunctionDecl::TK_FunctionTemplateSpecialization: { + FunctionTemplateDecl *Template + = cast<FunctionTemplateDecl>(Reader.GetDecl(Record[Idx++])); + TemplateSpecializationKind TSK = (TemplateSpecializationKind)Record[Idx++]; + + // Template arguments. + llvm::SmallVector<TemplateArgument, 8> TemplArgs; + Reader.ReadTemplateArgumentList(TemplArgs, Record, Idx); + + // Template args as written. + llvm::SmallVector<TemplateArgumentLoc, 8> TemplArgLocs; + SourceLocation LAngleLoc, RAngleLoc; + if (Record[Idx++]) { // TemplateArgumentsAsWritten != 0 + unsigned NumTemplateArgLocs = Record[Idx++]; + TemplArgLocs.reserve(NumTemplateArgLocs); + for (unsigned i=0; i != NumTemplateArgLocs; ++i) + TemplArgLocs.push_back(Reader.ReadTemplateArgumentLoc(Record, Idx)); + + LAngleLoc = Reader.ReadSourceLocation(Record, Idx); + RAngleLoc = Reader.ReadSourceLocation(Record, Idx); + } + + SourceLocation POI = Reader.ReadSourceLocation(Record, Idx); + + FD->setFunctionTemplateSpecialization(Template, TemplArgs.size(), + TemplArgs.data(), TSK, + TemplArgLocs.size(), + TemplArgLocs.data(), + LAngleLoc, RAngleLoc, POI); + break; + } + case FunctionDecl::TK_DependentFunctionTemplateSpecialization: { + // Templates. + UnresolvedSet<8> TemplDecls; + unsigned NumTemplates = Record[Idx++]; + while (NumTemplates--) + TemplDecls.addDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++]))); + + // Templates args. + TemplateArgumentListInfo TemplArgs; + unsigned NumArgs = Record[Idx++]; + while (NumArgs--) + TemplArgs.addArgument(Reader.ReadTemplateArgumentLoc(Record, Idx)); + TemplArgs.setLAngleLoc(Reader.ReadSourceLocation(Record, Idx)); + TemplArgs.setRAngleLoc(Reader.ReadSourceLocation(Record, Idx)); + + FD->setDependentTemplateSpecialization(*Reader.getContext(), + TemplDecls, TemplArgs); + break; + } + } + + // FunctionDecl's body is handled last at PCHReaderDecl::Visit, + // after everything else is read. + + // Avoid side effects and invariant checking of FunctionDecl's + // setPreviousDeclaration. + FD->redeclarable_base::setPreviousDeclaration( cast_or_null<FunctionDecl>(Reader.GetDecl(Record[Idx++]))); FD->setStorageClass((FunctionDecl::StorageClass)Record[Idx++]); FD->setStorageClassAsWritten((FunctionDecl::StorageClass)Record[Idx++]); @@ -211,7 +301,6 @@ void PCHDeclReader::VisitFunctionDecl(FunctionDecl *FD) { FD->setCopyAssignment(Record[Idx++]); FD->setHasImplicitReturnZero(Record[Idx++]); FD->setLocEnd(SourceLocation::getFromRawEncoding(Record[Idx++])); - // FIXME: C++ TemplateOrInstantiation // Read in the parameters. unsigned NumParams = Record[Idx++]; @@ -220,11 +309,6 @@ void PCHDeclReader::VisitFunctionDecl(FunctionDecl *FD) { for (unsigned I = 0; I != NumParams; ++I) Params.push_back(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++]))); FD->setParams(Params.data(), NumParams); - - // FIXME: order this properly w.r.t. friendness - // FIXME: this same thing needs to happen for function templates - if (FD->isOverloadedOperator() && !FD->getDeclContext()->isRecord()) - FD->setNonMemberOperator(); } void PCHDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) { @@ -232,7 +316,7 @@ void PCHDeclReader::VisitObjCMethodDecl(ObjCMethodDecl *MD) { if (Record[Idx++]) { // In practice, this won't be executed (since method definitions // don't occur in header files). - MD->setBody(Reader.ReadDeclStmt()); + MD->setBody(Reader.ReadStmt()); MD->setSelfDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++]))); MD->setCmdDecl(cast<ImplicitParamDecl>(Reader.GetDecl(Record[Idx++]))); } @@ -374,10 +458,12 @@ void PCHDeclReader::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *CAD) { void PCHDeclReader::VisitObjCPropertyDecl(ObjCPropertyDecl *D) { VisitNamedDecl(D); D->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); - D->setType(Reader.GetType(Record[Idx++])); + D->setType(Reader.GetTypeSourceInfo(Record, Idx)); // FIXME: stable encoding D->setPropertyAttributes( (ObjCPropertyDecl::PropertyAttributeKind)Record[Idx++]); + D->setPropertyAttributesAsWritten( + (ObjCPropertyDecl::PropertyAttributeKind)Record[Idx++]); // FIXME: stable encoding D->setPropertyImplementation( (ObjCPropertyDecl::PropertyControl)Record[Idx++]); @@ -424,7 +510,12 @@ void PCHDeclReader::VisitFieldDecl(FieldDecl *FD) { VisitDeclaratorDecl(FD); FD->setMutable(Record[Idx++]); if (Record[Idx++]) - FD->setBitWidth(Reader.ReadDeclExpr()); + FD->setBitWidth(Reader.ReadExpr()); + if (!FD->getDeclName()) { + FieldDecl *Tmpl = cast_or_null<FieldDecl>(Reader.GetDecl(Record[Idx++])); + if (Tmpl) + Reader.getContext()->setInstantiatedFromUnnamedFieldDecl(FD, Tmpl); + } } void PCHDeclReader::VisitVarDecl(VarDecl *VD) { @@ -439,7 +530,14 @@ void PCHDeclReader::VisitVarDecl(VarDecl *VD) { VD->setPreviousDeclaration( cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++]))); if (Record[Idx++]) - VD->setInit(Reader.ReadDeclExpr()); + VD->setInit(Reader.ReadExpr()); + + if (Record[Idx++]) { // HasMemberSpecializationInfo. + VarDecl *Tmpl = cast<VarDecl>(Reader.GetDecl(Record[Idx++])); + TemplateSpecializationKind TSK = (TemplateSpecializationKind)Record[Idx++]; + SourceLocation POI = Reader.ReadSourceLocation(Record, Idx); + Reader.getContext()->setInstantiatedFromStaticDataMember(VD, Tmpl, TSK,POI); + } } void PCHDeclReader::VisitImplicitParamDecl(ImplicitParamDecl *PD) { @@ -450,16 +548,19 @@ void PCHDeclReader::VisitParmVarDecl(ParmVarDecl *PD) { VisitVarDecl(PD); PD->setObjCDeclQualifier((Decl::ObjCDeclQualifier)Record[Idx++]); PD->setHasInheritedDefaultArg(Record[Idx++]); + if (Record[Idx++]) // hasUninstantiatedDefaultArg. + PD->setUninstantiatedDefaultArg(Reader.ReadExpr()); } void PCHDeclReader::VisitFileScopeAsmDecl(FileScopeAsmDecl *AD) { VisitDecl(AD); - AD->setAsmString(cast<StringLiteral>(Reader.ReadDeclExpr())); + AD->setAsmString(cast<StringLiteral>(Reader.ReadExpr())); } void PCHDeclReader::VisitBlockDecl(BlockDecl *BD) { VisitDecl(BD); - BD->setBody(cast_or_null<CompoundStmt>(Reader.ReadDeclStmt())); + BD->setBody(cast_or_null<CompoundStmt>(Reader.ReadStmt())); + BD->setSignatureAsWritten(Reader.GetTypeSourceInfo(Record, Idx)); unsigned NumParams = Record[Idx++]; llvm::SmallVector<ParmVarDecl *, 16> Params; Params.reserve(NumParams); @@ -481,13 +582,9 @@ void PCHDeclReader::VisitNamespaceDecl(NamespaceDecl *D) { D->setNextNamespace( cast_or_null<NamespaceDecl>(Reader.GetDecl(Record[Idx++]))); - // Only read one reference--the original or anonymous namespace. bool IsOriginal = Record[Idx++]; - if (IsOriginal) - D->setAnonymousNamespace( - cast_or_null<NamespaceDecl>(Reader.GetDecl(Record[Idx++]))); - else - D->setOriginalNamespace( + D->OrigOrAnonNamespace.setInt(IsOriginal); + D->OrigOrAnonNamespace.setPointer( cast_or_null<NamespaceDecl>(Reader.GetDecl(Record[Idx++]))); } @@ -501,7 +598,7 @@ void PCHDeclReader::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { D->setAliasedNamespace(cast<NamedDecl>(Reader.GetDecl(Record[Idx++]))); } -void PCHDeclReader::VisitUsing(UsingDecl *D) { +void PCHDeclReader::VisitUsingDecl(UsingDecl *D) { VisitNamedDecl(D); D->setUsingLocation(Reader.ReadSourceLocation(Record, Idx)); D->setNestedNameRange(Reader.ReadSourceRange(Record, Idx)); @@ -512,15 +609,24 @@ void PCHDeclReader::VisitUsing(UsingDecl *D) { // would avoid existence checks. unsigned NumShadows = Record[Idx++]; for(unsigned I = 0; I != NumShadows; ++I) { - D->addShadowDecl(cast<UsingShadowDecl>(Reader.GetDecl(Record[Idx++]))); + // Avoid invariant checking of UsingDecl::addShadowDecl, the decl may still + // be initializing. + D->Shadows.insert(cast<UsingShadowDecl>(Reader.GetDecl(Record[Idx++]))); } D->setTypeName(Record[Idx++]); + NamedDecl *Pattern = cast_or_null<NamedDecl>(Reader.GetDecl(Record[Idx++])); + if (Pattern) + Reader.getContext()->setInstantiatedFromUsingDecl(D, Pattern); } -void PCHDeclReader::VisitUsingShadow(UsingShadowDecl *D) { +void PCHDeclReader::VisitUsingShadowDecl(UsingShadowDecl *D) { VisitNamedDecl(D); D->setTargetDecl(cast<NamedDecl>(Reader.GetDecl(Record[Idx++]))); D->setUsingDecl(cast<UsingDecl>(Reader.GetDecl(Record[Idx++]))); + UsingShadowDecl *Pattern + = cast_or_null<UsingShadowDecl>(Reader.GetDecl(Record[Idx++])); + if (Pattern) + Reader.getContext()->setInstantiatedFromUsingShadowDecl(D, Pattern); } void PCHDeclReader::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { @@ -534,14 +640,14 @@ void PCHDeclReader::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { Reader.GetDecl(Record[Idx++]))); } -void PCHDeclReader::VisitUnresolvedUsingValue(UnresolvedUsingValueDecl *D) { +void PCHDeclReader::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) { VisitValueDecl(D); D->setTargetNestedNameRange(Reader.ReadSourceRange(Record, Idx)); D->setUsingLoc(Reader.ReadSourceLocation(Record, Idx)); D->setTargetNestedNameSpecifier(Reader.ReadNestedNameSpecifier(Record, Idx)); } -void PCHDeclReader::VisitUnresolvedUsingTypename( +void PCHDeclReader::VisitUnresolvedUsingTypenameDecl( UnresolvedUsingTypenameDecl *D) { VisitTypeDecl(D); D->setTargetNestedNameRange(Reader.ReadSourceRange(Record, Idx)); @@ -551,28 +657,196 @@ void PCHDeclReader::VisitUnresolvedUsingTypename( } void PCHDeclReader::VisitCXXRecordDecl(CXXRecordDecl *D) { - // assert(false && "cannot read CXXRecordDecl"); + ASTContext &C = *Reader.getContext(); + + // We need to allocate the DefinitionData struct ahead of VisitRecordDecl + // so that the other CXXRecordDecls can get a pointer even when the owner + // is still initializing. + bool OwnsDefinitionData = false; + enum DataOwnership { Data_NoDefData, Data_Owner, Data_NotOwner }; + switch ((DataOwnership)Record[Idx++]) { + default: + assert(0 && "Out of sync with PCHDeclWriter or messed up reading"); + case Data_NoDefData: + break; + case Data_Owner: + OwnsDefinitionData = true; + D->DefinitionData = new (C) struct CXXRecordDecl::DefinitionData(D); + break; + case Data_NotOwner: + D->DefinitionData + = cast<CXXRecordDecl>(Reader.GetDecl(Record[Idx++]))->DefinitionData; + break; + } + VisitRecordDecl(D); + + if (OwnsDefinitionData) { + assert(D->DefinitionData); + struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData; + + Data.UserDeclaredConstructor = Record[Idx++]; + Data.UserDeclaredCopyConstructor = Record[Idx++]; + Data.UserDeclaredCopyAssignment = Record[Idx++]; + Data.UserDeclaredDestructor = Record[Idx++]; + Data.Aggregate = Record[Idx++]; + Data.PlainOldData = Record[Idx++]; + Data.Empty = Record[Idx++]; + Data.Polymorphic = Record[Idx++]; + Data.Abstract = Record[Idx++]; + Data.HasTrivialConstructor = Record[Idx++]; + Data.HasTrivialCopyConstructor = Record[Idx++]; + Data.HasTrivialCopyAssignment = Record[Idx++]; + Data.HasTrivialDestructor = Record[Idx++]; + Data.ComputedVisibleConversions = Record[Idx++]; + Data.DeclaredDefaultConstructor = Record[Idx++]; + Data.DeclaredCopyConstructor = Record[Idx++]; + Data.DeclaredCopyAssignment = Record[Idx++]; + Data.DeclaredDestructor = Record[Idx++]; + + // setBases() is unsuitable since it may try to iterate the bases of an + // unitialized base. + Data.NumBases = Record[Idx++]; + Data.Bases = new(C) CXXBaseSpecifier [Data.NumBases]; + for (unsigned i = 0; i != Data.NumBases; ++i) + Data.Bases[i] = Reader.ReadCXXBaseSpecifier(Record, Idx); + + // FIXME: Make VBases lazily computed when needed to avoid storing them. + Data.NumVBases = Record[Idx++]; + Data.VBases = new(C) CXXBaseSpecifier [Data.NumVBases]; + for (unsigned i = 0; i != Data.NumVBases; ++i) + Data.VBases[i] = Reader.ReadCXXBaseSpecifier(Record, Idx); + + Reader.ReadUnresolvedSet(Data.Conversions, Record, Idx); + Reader.ReadUnresolvedSet(Data.VisibleConversions, Record, Idx); + assert(Data.Definition && "Data.Definition should be already set!"); + Data.FirstFriend + = cast_or_null<FriendDecl>(Reader.GetDecl(Record[Idx++])); + } + + enum CXXRecKind { + CXXRecNotTemplate = 0, CXXRecTemplate, CXXRecMemberSpecialization + }; + switch ((CXXRecKind)Record[Idx++]) { + default: + assert(false && "Out of sync with PCHDeclWriter::VisitCXXRecordDecl?"); + case CXXRecNotTemplate: + break; + case CXXRecTemplate: + D->setDescribedClassTemplate( + cast<ClassTemplateDecl>(Reader.GetDecl(Record[Idx++]))); + break; + case CXXRecMemberSpecialization: { + CXXRecordDecl *RD = cast<CXXRecordDecl>(Reader.GetDecl(Record[Idx++])); + TemplateSpecializationKind TSK = (TemplateSpecializationKind)Record[Idx++]; + SourceLocation POI = Reader.ReadSourceLocation(Record, Idx); + D->setInstantiationOfMemberClass(RD, TSK); + D->getMemberSpecializationInfo()->setPointOfInstantiation(POI); + break; + } + } } void PCHDeclReader::VisitCXXMethodDecl(CXXMethodDecl *D) { - // assert(false && "cannot read CXXMethodDecl"); VisitFunctionDecl(D); + unsigned NumOverridenMethods = Record[Idx++]; + while (NumOverridenMethods--) { + CXXMethodDecl *MD = cast<CXXMethodDecl>(Reader.GetDecl(Record[Idx++])); + // Avoid invariant checking of CXXMethodDecl::addOverriddenMethod, + // MD may be initializing. + Reader.getContext()->addOverriddenMethod(D, MD); + } } void PCHDeclReader::VisitCXXConstructorDecl(CXXConstructorDecl *D) { - // assert(false && "cannot read CXXConstructorDecl"); VisitCXXMethodDecl(D); + + D->IsExplicitSpecified = Record[Idx++]; + D->ImplicitlyDefined = Record[Idx++]; + + unsigned NumInitializers = Record[Idx++]; + D->NumBaseOrMemberInitializers = NumInitializers; + if (NumInitializers) { + ASTContext &C = *Reader.getContext(); + + D->BaseOrMemberInitializers + = new (C) CXXBaseOrMemberInitializer*[NumInitializers]; + for (unsigned i=0; i != NumInitializers; ++i) { + TypeSourceInfo *BaseClassInfo = 0; + bool IsBaseVirtual = false; + FieldDecl *Member = 0; + + bool IsBaseInitializer = Record[Idx++]; + if (IsBaseInitializer) { + BaseClassInfo = Reader.GetTypeSourceInfo(Record, Idx); + IsBaseVirtual = Record[Idx++]; + } else { + Member = cast<FieldDecl>(Reader.GetDecl(Record[Idx++])); + } + SourceLocation MemberLoc = Reader.ReadSourceLocation(Record, Idx); + Expr *Init = Reader.ReadExpr(); + FieldDecl *AnonUnionMember + = cast_or_null<FieldDecl>(Reader.GetDecl(Record[Idx++])); + SourceLocation LParenLoc = Reader.ReadSourceLocation(Record, Idx); + SourceLocation RParenLoc = Reader.ReadSourceLocation(Record, Idx); + bool IsWritten = Record[Idx++]; + unsigned SourceOrderOrNumArrayIndices; + llvm::SmallVector<VarDecl *, 8> Indices; + if (IsWritten) { + SourceOrderOrNumArrayIndices = Record[Idx++]; + } else { + SourceOrderOrNumArrayIndices = Record[Idx++]; + Indices.reserve(SourceOrderOrNumArrayIndices); + for (unsigned i=0; i != SourceOrderOrNumArrayIndices; ++i) + Indices.push_back(cast<VarDecl>(Reader.GetDecl(Record[Idx++]))); + } + + CXXBaseOrMemberInitializer *BOMInit; + if (IsBaseInitializer) { + BOMInit = new (C) CXXBaseOrMemberInitializer(C, BaseClassInfo, + IsBaseVirtual, LParenLoc, + Init, RParenLoc); + } else if (IsWritten) { + BOMInit = new (C) CXXBaseOrMemberInitializer(C, Member, MemberLoc, + LParenLoc, Init, RParenLoc); + } else { + BOMInit = CXXBaseOrMemberInitializer::Create(C, Member, MemberLoc, + LParenLoc, Init, RParenLoc, + Indices.data(), + Indices.size()); + } + + BOMInit->setAnonUnionMember(AnonUnionMember); + D->BaseOrMemberInitializers[i] = BOMInit; + } + } } void PCHDeclReader::VisitCXXDestructorDecl(CXXDestructorDecl *D) { - // assert(false && "cannot read CXXDestructorDecl"); VisitCXXMethodDecl(D); + + D->ImplicitlyDefined = Record[Idx++]; + D->OperatorDelete = cast_or_null<FunctionDecl>(Reader.GetDecl(Record[Idx++])); } void PCHDeclReader::VisitCXXConversionDecl(CXXConversionDecl *D) { - // assert(false && "cannot read CXXConversionDecl"); VisitCXXMethodDecl(D); + D->IsExplicitSpecified = Record[Idx++]; +} + +void PCHDeclReader::VisitAccessSpecDecl(AccessSpecDecl *D) { + VisitDecl(D); + D->setColonLoc(Reader.ReadSourceLocation(Record, Idx)); +} + +void PCHDeclReader::VisitFriendDecl(FriendDecl *D) { + VisitDecl(D); + if (Record[Idx++]) + D->Friend = Reader.GetTypeSourceInfo(Record, Idx); + else + D->Friend = cast<NamedDecl>(Reader.GetDecl(Record[Idx++])); + D->NextFriend = cast_or_null<FriendDecl>(Reader.GetDecl(Record[Idx++])); + D->FriendLoc = Reader.ReadSourceLocation(Record, Idx); } void PCHDeclReader::VisitFriendTemplateDecl(FriendTemplateDecl *D) { @@ -580,37 +854,171 @@ void PCHDeclReader::VisitFriendTemplateDecl(FriendTemplateDecl *D) { } void PCHDeclReader::VisitTemplateDecl(TemplateDecl *D) { - assert(false && "cannot read TemplateDecl"); + VisitNamedDecl(D); + + NamedDecl *TemplatedDecl + = cast_or_null<NamedDecl>(Reader.GetDecl(Record[Idx++])); + TemplateParameterList* TemplateParams + = Reader.ReadTemplateParameterList(Record, Idx); + D->init(TemplatedDecl, TemplateParams); } void PCHDeclReader::VisitClassTemplateDecl(ClassTemplateDecl *D) { - assert(false && "cannot read ClassTemplateDecl"); + VisitTemplateDecl(D); + + D->IdentifierNamespace = Record[Idx++]; + ClassTemplateDecl *PrevDecl = + cast_or_null<ClassTemplateDecl>(Reader.GetDecl(Record[Idx++])); + D->setPreviousDeclaration(PrevDecl); + if (PrevDecl == 0) { + // This ClassTemplateDecl owns a CommonPtr; read it. + + // FoldingSets are filled in VisitClassTemplateSpecializationDecl. + unsigned size = Record[Idx++]; + while (size--) + cast<ClassTemplateSpecializationDecl>(Reader.GetDecl(Record[Idx++])); + + size = Record[Idx++]; + while (size--) + cast<ClassTemplatePartialSpecializationDecl>( + Reader.GetDecl(Record[Idx++])); + + // InjectedClassNameType is computed. + + if (ClassTemplateDecl *CTD + = cast_or_null<ClassTemplateDecl>(Reader.GetDecl(Record[Idx++]))) { + D->setInstantiatedFromMemberTemplate(CTD); + if (Record[Idx++]) + D->setMemberSpecialization(); + } + } } void PCHDeclReader::VisitClassTemplateSpecializationDecl( ClassTemplateSpecializationDecl *D) { - assert(false && "cannot read ClassTemplateSpecializationDecl"); + VisitCXXRecordDecl(D); + + if (Decl *InstD = Reader.GetDecl(Record[Idx++])) { + if (ClassTemplateDecl *CTD = dyn_cast<ClassTemplateDecl>(InstD)) { + D->setInstantiationOf(CTD); + } else { + llvm::SmallVector<TemplateArgument, 8> TemplArgs; + Reader.ReadTemplateArgumentList(TemplArgs, Record, Idx); + D->setInstantiationOf(cast<ClassTemplatePartialSpecializationDecl>(InstD), + TemplArgs.data(), TemplArgs.size()); + } + } + + // Explicit info. + if (TypeSourceInfo *TyInfo = Reader.GetTypeSourceInfo(Record, Idx)) { + D->setTypeAsWritten(TyInfo); + D->setExternLoc(Reader.ReadSourceLocation(Record, Idx)); + D->setTemplateKeywordLoc(Reader.ReadSourceLocation(Record, Idx)); + } + + llvm::SmallVector<TemplateArgument, 8> TemplArgs; + Reader.ReadTemplateArgumentList(TemplArgs, Record, Idx); + D->initTemplateArgs(TemplArgs.data(), TemplArgs.size()); + SourceLocation POI = Reader.ReadSourceLocation(Record, Idx); + if (POI.isValid()) + D->setPointOfInstantiation(POI); + D->setSpecializationKind((TemplateSpecializationKind)Record[Idx++]); + + if (Record[Idx++]) { // IsKeptInFoldingSet. + ClassTemplateDecl *CanonPattern + = cast<ClassTemplateDecl>(Reader.GetDecl(Record[Idx++])); + if (ClassTemplatePartialSpecializationDecl *Partial + = dyn_cast<ClassTemplatePartialSpecializationDecl>(D)) { + CanonPattern->getPartialSpecializations().InsertNode(Partial); + } else { + CanonPattern->getSpecializations().InsertNode(D); + } + } } void PCHDeclReader::VisitClassTemplatePartialSpecializationDecl( ClassTemplatePartialSpecializationDecl *D) { - assert(false && "cannot read ClassTemplatePartialSpecializationDecl"); + VisitClassTemplateSpecializationDecl(D); + + D->initTemplateParameters(Reader.ReadTemplateParameterList(Record, Idx)); + + TemplateArgumentListInfo ArgInfos; + unsigned NumArgs = Record[Idx++]; + while (NumArgs--) + ArgInfos.addArgument(Reader.ReadTemplateArgumentLoc(Record, Idx)); + D->initTemplateArgsAsWritten(ArgInfos); + + D->setSequenceNumber(Record[Idx++]); + + // These are read/set from/to the first declaration. + if (D->getPreviousDeclaration() == 0) { + D->setInstantiatedFromMember( + cast_or_null<ClassTemplatePartialSpecializationDecl>( + Reader.GetDecl(Record[Idx++]))); + if (Record[Idx++]) + D->setMemberSpecialization(); + } } -void PCHDeclReader::visitFunctionTemplateDecl(FunctionTemplateDecl *D) { - assert(false && "cannot read FunctionTemplateDecl"); +void PCHDeclReader::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { + VisitTemplateDecl(D); + + D->IdentifierNamespace = Record[Idx++]; + FunctionTemplateDecl *PrevDecl = + cast_or_null<FunctionTemplateDecl>(Reader.GetDecl(Record[Idx++])); + D->setPreviousDeclaration(PrevDecl); + if (PrevDecl == 0) { + // This FunctionTemplateDecl owns a CommonPtr; read it. + + // Read the function specialization declarations. + // FunctionTemplateDecl's FunctionTemplateSpecializationInfos are filled + // through the specialized FunctionDecl's setFunctionTemplateSpecialization. + unsigned NumSpecs = Record[Idx++]; + while (NumSpecs--) + Reader.GetDecl(Record[Idx++]); + + if (FunctionTemplateDecl *CTD + = cast_or_null<FunctionTemplateDecl>(Reader.GetDecl(Record[Idx++]))) { + D->setInstantiatedFromMemberTemplate(CTD); + if (Record[Idx++]) + D->setMemberSpecialization(); + } + } } void PCHDeclReader::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) { - assert(false && "cannot read TemplateTypeParmDecl"); + VisitTypeDecl(D); + + D->setDeclaredWithTypename(Record[Idx++]); + D->setParameterPack(Record[Idx++]); + + bool Inherited = Record[Idx++]; + TypeSourceInfo *DefArg = Reader.GetTypeSourceInfo(Record, Idx); + D->setDefaultArgument(DefArg, Inherited); } void PCHDeclReader::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) { - assert(false && "cannot read NonTypeTemplateParmDecl"); + VisitVarDecl(D); + // TemplateParmPosition. + D->setDepth(Record[Idx++]); + D->setPosition(Record[Idx++]); + // Rest of NonTypeTemplateParmDecl. + if (Record[Idx++]) { + Expr *DefArg = Reader.ReadExpr(); + bool Inherited = Record[Idx++]; + D->setDefaultArgument(DefArg, Inherited); + } } void PCHDeclReader::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) { - assert(false && "cannot read TemplateTemplateParmDecl"); + VisitTemplateDecl(D); + // TemplateParmPosition. + D->setDepth(Record[Idx++]); + D->setPosition(Record[Idx++]); + // Rest of TemplateTemplateParmDecl. + TemplateArgumentLoc Arg = Reader.ReadTemplateArgumentLoc(Record, Idx); + bool IsInherited = Record[Idx++]; + D->setDefaultArgument(Arg, IsInherited); } void PCHDeclReader::VisitStaticAssertDecl(StaticAssertDecl *D) { @@ -641,24 +1049,24 @@ Attr *PCHReader::ReadAttributes() { (void)RecCode; #define SIMPLE_ATTR(Name) \ - case Attr::Name: \ + case attr::Name: \ New = ::new (*Context) Name##Attr(); \ break #define STRING_ATTR(Name) \ - case Attr::Name: \ + case attr::Name: \ New = ::new (*Context) Name##Attr(*Context, ReadString(Record, Idx)); \ break #define UNSIGNED_ATTR(Name) \ - case Attr::Name: \ + case attr::Name: \ New = ::new (*Context) Name##Attr(Record[Idx++]); \ break Attr *Attrs = 0; while (Idx < Record.size()) { Attr *New = 0; - Attr::Kind Kind = (Attr::Kind)Record[Idx++]; + attr::Kind Kind = (attr::Kind)Record[Idx++]; bool IsInherited = Record[Idx++]; switch (Kind) { @@ -674,14 +1082,14 @@ Attr *PCHReader::ReadAttributes() { STRING_ATTR(AsmLabel); SIMPLE_ATTR(BaseCheck); - case Attr::Blocks: + case attr::Blocks: New = ::new (*Context) BlocksAttr( (BlocksAttr::BlocksAttrTypes)Record[Idx++]); break; SIMPLE_ATTR(CDecl); - case Attr::Cleanup: + case attr::Cleanup: New = ::new (*Context) CleanupAttr( cast<FunctionDecl>(GetDecl(Record[Idx++]))); break; @@ -695,7 +1103,7 @@ Attr *PCHReader::ReadAttributes() { SIMPLE_ATTR(FastCall); SIMPLE_ATTR(Final); - case Attr::Format: { + case attr::Format: { std::string Type = ReadString(Record, Idx); unsigned FormatIdx = Record[Idx++]; unsigned FirstArg = Record[Idx++]; @@ -703,13 +1111,13 @@ Attr *PCHReader::ReadAttributes() { break; } - case Attr::FormatArg: { + case attr::FormatArg: { unsigned FormatIdx = Record[Idx++]; New = ::new (*Context) FormatArgAttr(FormatIdx); break; } - case Attr::Sentinel: { + case attr::Sentinel: { int sentinel = Record[Idx++]; int nullPos = Record[Idx++]; New = ::new (*Context) SentinelAttr(sentinel, nullPos); @@ -719,15 +1127,15 @@ Attr *PCHReader::ReadAttributes() { SIMPLE_ATTR(GNUInline); SIMPLE_ATTR(Hiding); - case Attr::IBActionKind: + case attr::IBAction: New = ::new (*Context) IBActionAttr(); break; - case Attr::IBOutletKind: + case attr::IBOutlet: New = ::new (*Context) IBOutletAttr(); break; - case Attr::IBOutletCollectionKind: { + case attr::IBOutletCollection: { ObjCInterfaceDecl *D = cast_or_null<ObjCInterfaceDecl>(GetDecl(Record[Idx++])); New = ::new (*Context) IBOutletCollectionAttr(D); @@ -740,7 +1148,7 @@ Attr *PCHReader::ReadAttributes() { SIMPLE_ATTR(NoReturn); SIMPLE_ATTR(NoThrow); - case Attr::NonNull: { + case attr::NonNull: { unsigned Size = Record[Idx++]; llvm::SmallVector<unsigned, 16> ArgNums; ArgNums.insert(ArgNums.end(), &Record[Idx], &Record[Idx] + Size); @@ -749,7 +1157,7 @@ Attr *PCHReader::ReadAttributes() { break; } - case Attr::ReqdWorkGroupSize: { + case attr::ReqdWorkGroupSize: { unsigned X = Record[Idx++]; unsigned Y = Record[Idx++]; unsigned Z = Record[Idx++]; @@ -777,7 +1185,7 @@ Attr *PCHReader::ReadAttributes() { SIMPLE_ATTR(Unused); SIMPLE_ATTR(Used); - case Attr::Visibility: + case attr::Visibility: New = ::new (*Context) VisibilityAttr( (VisibilityAttr::VisibilityTypes)Record[Idx++]); break; @@ -848,6 +1256,8 @@ Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) { // after reading this declaration. SavedStreamPosition SavedPosition(DeclsCursor); + ReadingKindTracker ReadingKind(Read_Decl, *this); + // Note that we are loading a declaration record. LoadingTypeOrDecl Loading(*this); @@ -872,11 +1282,10 @@ Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) { D = TypedefDecl::Create(*Context, 0, SourceLocation(), 0, 0); break; case pch::DECL_ENUM: - D = EnumDecl::Create(*Context, 0, SourceLocation(), 0, SourceLocation(), 0); + D = EnumDecl::Create(*Context, Decl::EmptyShell()); break; case pch::DECL_RECORD: - D = RecordDecl::Create(*Context, TTK_Struct, 0, SourceLocation(), - 0, SourceLocation(), 0); + D = RecordDecl::Create(*Context, Decl::EmptyShell()); break; case pch::DECL_ENUM_CONSTANT: D = EnumConstantDecl::Create(*Context, 0, SourceLocation(), 0, QualType(), @@ -923,8 +1332,7 @@ Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) { DeclarationName()); break; case pch::DECL_CXX_RECORD: - D = CXXRecordDecl::Create(*Context, TTK_Struct, 0, - SourceLocation(), 0, SourceLocation(), 0); + D = CXXRecordDecl::Create(*Context, Decl::EmptyShell()); break; case pch::DECL_CXX_METHOD: D = CXXMethodDecl::Create(*Context, 0, SourceLocation(), DeclarationName(), @@ -939,36 +1347,40 @@ Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) { case pch::DECL_CXX_CONVERSION: D = CXXConversionDecl::Create(*Context, Decl::EmptyShell()); break; + case pch::DECL_ACCESS_SPEC: + D = AccessSpecDecl::Create(*Context, AS_none, 0, SourceLocation(), + SourceLocation()); + break; case pch::DECL_FRIEND: - assert(false && "cannot read FriendDecl"); + D = FriendDecl::Create(*Context, Decl::EmptyShell()); break; case pch::DECL_FRIEND_TEMPLATE: assert(false && "cannot read FriendTemplateDecl"); break; - case pch::DECL_TEMPLATE: - // FIXME: Should TemplateDecl be ABSTRACT_DECL??? - assert(false && "TemplateDecl should be abstract!"); - break; case pch::DECL_CLASS_TEMPLATE: - assert(false && "cannot read ClassTemplateDecl"); + D = ClassTemplateDecl::Create(*Context, 0, SourceLocation(), + DeclarationName(), 0, 0, 0); break; case pch::DECL_CLASS_TEMPLATE_SPECIALIZATION: - assert(false && "cannot read ClasstemplateSpecializationDecl"); + D = ClassTemplateSpecializationDecl::Create(*Context, Decl::EmptyShell()); break; case pch::DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION: - assert(false && "cannot read ClassTemplatePartialSpecializationDecl"); + D = ClassTemplatePartialSpecializationDecl::Create(*Context, + Decl::EmptyShell()); break; case pch::DECL_FUNCTION_TEMPLATE: - assert(false && "cannot read FunctionTemplateDecl"); + D = FunctionTemplateDecl::Create(*Context, 0, SourceLocation(), + DeclarationName(), 0, 0); break; case pch::DECL_TEMPLATE_TYPE_PARM: - assert(false && "cannot read TemplateTypeParmDecl"); + D = TemplateTypeParmDecl::Create(*Context, Decl::EmptyShell()); break; case pch::DECL_NON_TYPE_TEMPLATE_PARM: - assert(false && "cannot read NonTypeTemplateParmDecl"); + D = NonTypeTemplateParmDecl::Create(*Context, 0, SourceLocation(), 0,0,0, + QualType(),0); break; case pch::DECL_TEMPLATE_TEMPLATE_PARM: - assert(false && "cannot read TemplateTemplateParmDecl"); + D = TemplateTemplateParmDecl::Create(*Context, 0, SourceLocation(),0,0,0,0); break; case pch::DECL_STATIC_ASSERT: assert(false && "cannot read StaticAssertDecl"); @@ -1013,7 +1425,7 @@ Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) { break; case pch::DECL_OBJC_PROPERTY: D = ObjCPropertyDecl::Create(*Context, 0, SourceLocation(), 0, SourceLocation(), - QualType()); + 0); break; case pch::DECL_OBJC_PROPERTY_IMPL: D = ObjCPropertyImplDecl::Create(*Context, 0, SourceLocation(), @@ -1062,16 +1474,11 @@ Decl *PCHReader::ReadDeclRecord(uint64_t Offset, unsigned Index) { assert(Idx == Record.size()); // If we have deserialized a declaration that has a definition the - // AST consumer might need to know about, notify the consumer - // about that definition now or queue it for later. - if (isConsumerInterestedIn(D)) { - if (Consumer) { - DeclGroupRef DG(D); - Consumer->HandleTopLevelDecl(DG); - } else { - InterestingDecls.push_back(D); - } - } + // AST consumer might need to know about, queue it. + // We don't pass it to the consumer immediately because we may be in recursive + // loading, and some declarations may still be initializing. + if (isConsumerInterestedIn(D)) + InterestingDecls.push_back(D); return D; } diff --git a/contrib/llvm/tools/clang/lib/Frontend/PCHReaderStmt.cpp b/contrib/llvm/tools/clang/lib/Frontend/PCHReaderStmt.cpp index 3931adb..ace62d7 100644 --- a/contrib/llvm/tools/clang/lib/Frontend/PCHReaderStmt.cpp +++ b/contrib/llvm/tools/clang/lib/Frontend/PCHReaderStmt.cpp @@ -17,17 +17,17 @@ #include "clang/AST/StmtVisitor.h" using namespace clang; -namespace { - class PCHStmtReader : public StmtVisitor<PCHStmtReader, unsigned> { +namespace clang { + + class PCHStmtReader : public StmtVisitor<PCHStmtReader> { PCHReader &Reader; const PCHReader::RecordData &Record; unsigned &Idx; - llvm::SmallVectorImpl<Stmt *> &StmtStack; public: PCHStmtReader(PCHReader &Reader, const PCHReader::RecordData &Record, - unsigned &Idx, llvm::SmallVectorImpl<Stmt *> &StmtStack) - : Reader(Reader), Record(Record), Idx(Idx), StmtStack(StmtStack) { } + unsigned &Idx) + : Reader(Reader), Record(Record), Idx(Idx) { } /// \brief The number of record fields required for the Stmt class /// itself. @@ -36,180 +36,201 @@ namespace { /// \brief The number of record fields required for the Expr class /// itself. static const unsigned NumExprFields = NumStmtFields + 3; - - // Each of the Visit* functions reads in part of the expression - // from the given record and the current expression stack, then - // return the total number of operands that it read from the - // expression stack. - - unsigned VisitStmt(Stmt *S); - unsigned VisitNullStmt(NullStmt *S); - unsigned VisitCompoundStmt(CompoundStmt *S); - unsigned VisitSwitchCase(SwitchCase *S); - unsigned VisitCaseStmt(CaseStmt *S); - unsigned VisitDefaultStmt(DefaultStmt *S); - unsigned VisitLabelStmt(LabelStmt *S); - unsigned VisitIfStmt(IfStmt *S); - unsigned VisitSwitchStmt(SwitchStmt *S); - unsigned VisitWhileStmt(WhileStmt *S); - unsigned VisitDoStmt(DoStmt *S); - unsigned VisitForStmt(ForStmt *S); - unsigned VisitGotoStmt(GotoStmt *S); - unsigned VisitIndirectGotoStmt(IndirectGotoStmt *S); - unsigned VisitContinueStmt(ContinueStmt *S); - unsigned VisitBreakStmt(BreakStmt *S); - unsigned VisitReturnStmt(ReturnStmt *S); - unsigned VisitDeclStmt(DeclStmt *S); - unsigned VisitAsmStmt(AsmStmt *S); - unsigned VisitExpr(Expr *E); - unsigned VisitPredefinedExpr(PredefinedExpr *E); - unsigned VisitDeclRefExpr(DeclRefExpr *E); - unsigned VisitIntegerLiteral(IntegerLiteral *E); - unsigned VisitFloatingLiteral(FloatingLiteral *E); - unsigned VisitImaginaryLiteral(ImaginaryLiteral *E); - unsigned VisitStringLiteral(StringLiteral *E); - unsigned VisitCharacterLiteral(CharacterLiteral *E); - unsigned VisitParenExpr(ParenExpr *E); - unsigned VisitUnaryOperator(UnaryOperator *E); - unsigned VisitOffsetOfExpr(OffsetOfExpr *E); - unsigned VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E); - unsigned VisitArraySubscriptExpr(ArraySubscriptExpr *E); - unsigned VisitCallExpr(CallExpr *E); - unsigned VisitMemberExpr(MemberExpr *E); - unsigned VisitCastExpr(CastExpr *E); - unsigned VisitBinaryOperator(BinaryOperator *E); - unsigned VisitCompoundAssignOperator(CompoundAssignOperator *E); - unsigned VisitConditionalOperator(ConditionalOperator *E); - unsigned VisitImplicitCastExpr(ImplicitCastExpr *E); - unsigned VisitExplicitCastExpr(ExplicitCastExpr *E); - unsigned VisitCStyleCastExpr(CStyleCastExpr *E); - unsigned VisitCompoundLiteralExpr(CompoundLiteralExpr *E); - unsigned VisitExtVectorElementExpr(ExtVectorElementExpr *E); - unsigned VisitInitListExpr(InitListExpr *E); - unsigned VisitDesignatedInitExpr(DesignatedInitExpr *E); - unsigned VisitImplicitValueInitExpr(ImplicitValueInitExpr *E); - unsigned VisitVAArgExpr(VAArgExpr *E); - unsigned VisitAddrLabelExpr(AddrLabelExpr *E); - unsigned VisitStmtExpr(StmtExpr *E); - unsigned VisitTypesCompatibleExpr(TypesCompatibleExpr *E); - unsigned VisitChooseExpr(ChooseExpr *E); - unsigned VisitGNUNullExpr(GNUNullExpr *E); - unsigned VisitShuffleVectorExpr(ShuffleVectorExpr *E); - unsigned VisitBlockExpr(BlockExpr *E); - unsigned VisitBlockDeclRefExpr(BlockDeclRefExpr *E); - unsigned VisitObjCStringLiteral(ObjCStringLiteral *E); - unsigned VisitObjCEncodeExpr(ObjCEncodeExpr *E); - unsigned VisitObjCSelectorExpr(ObjCSelectorExpr *E); - unsigned VisitObjCProtocolExpr(ObjCProtocolExpr *E); - unsigned VisitObjCIvarRefExpr(ObjCIvarRefExpr *E); - unsigned VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E); - unsigned VisitObjCImplicitSetterGetterRefExpr( + + /// \brief Read and initialize a ExplicitTemplateArgumentList structure. + void ReadExplicitTemplateArgumentList(ExplicitTemplateArgumentList &ArgList, + unsigned NumTemplateArgs); + + void VisitStmt(Stmt *S); + void VisitNullStmt(NullStmt *S); + void VisitCompoundStmt(CompoundStmt *S); + void VisitSwitchCase(SwitchCase *S); + void VisitCaseStmt(CaseStmt *S); + void VisitDefaultStmt(DefaultStmt *S); + void VisitLabelStmt(LabelStmt *S); + void VisitIfStmt(IfStmt *S); + void VisitSwitchStmt(SwitchStmt *S); + void VisitWhileStmt(WhileStmt *S); + void VisitDoStmt(DoStmt *S); + void VisitForStmt(ForStmt *S); + void VisitGotoStmt(GotoStmt *S); + void VisitIndirectGotoStmt(IndirectGotoStmt *S); + void VisitContinueStmt(ContinueStmt *S); + void VisitBreakStmt(BreakStmt *S); + void VisitReturnStmt(ReturnStmt *S); + void VisitDeclStmt(DeclStmt *S); + void VisitAsmStmt(AsmStmt *S); + void VisitExpr(Expr *E); + void VisitPredefinedExpr(PredefinedExpr *E); + void VisitDeclRefExpr(DeclRefExpr *E); + void VisitIntegerLiteral(IntegerLiteral *E); + void VisitFloatingLiteral(FloatingLiteral *E); + void VisitImaginaryLiteral(ImaginaryLiteral *E); + void VisitStringLiteral(StringLiteral *E); + void VisitCharacterLiteral(CharacterLiteral *E); + void VisitParenExpr(ParenExpr *E); + void VisitParenListExpr(ParenListExpr *E); + void VisitUnaryOperator(UnaryOperator *E); + void VisitOffsetOfExpr(OffsetOfExpr *E); + void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E); + void VisitArraySubscriptExpr(ArraySubscriptExpr *E); + void VisitCallExpr(CallExpr *E); + void VisitMemberExpr(MemberExpr *E); + void VisitCastExpr(CastExpr *E); + void VisitBinaryOperator(BinaryOperator *E); + void VisitCompoundAssignOperator(CompoundAssignOperator *E); + void VisitConditionalOperator(ConditionalOperator *E); + void VisitImplicitCastExpr(ImplicitCastExpr *E); + void VisitExplicitCastExpr(ExplicitCastExpr *E); + void VisitCStyleCastExpr(CStyleCastExpr *E); + void VisitCompoundLiteralExpr(CompoundLiteralExpr *E); + void VisitExtVectorElementExpr(ExtVectorElementExpr *E); + void VisitInitListExpr(InitListExpr *E); + void VisitDesignatedInitExpr(DesignatedInitExpr *E); + void VisitImplicitValueInitExpr(ImplicitValueInitExpr *E); + void VisitVAArgExpr(VAArgExpr *E); + void VisitAddrLabelExpr(AddrLabelExpr *E); + void VisitStmtExpr(StmtExpr *E); + void VisitTypesCompatibleExpr(TypesCompatibleExpr *E); + void VisitChooseExpr(ChooseExpr *E); + void VisitGNUNullExpr(GNUNullExpr *E); + void VisitShuffleVectorExpr(ShuffleVectorExpr *E); + void VisitBlockExpr(BlockExpr *E); + void VisitBlockDeclRefExpr(BlockDeclRefExpr *E); + void VisitObjCStringLiteral(ObjCStringLiteral *E); + void VisitObjCEncodeExpr(ObjCEncodeExpr *E); + void VisitObjCSelectorExpr(ObjCSelectorExpr *E); + void VisitObjCProtocolExpr(ObjCProtocolExpr *E); + void VisitObjCIvarRefExpr(ObjCIvarRefExpr *E); + void VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E); + void VisitObjCImplicitSetterGetterRefExpr( ObjCImplicitSetterGetterRefExpr *E); - unsigned VisitObjCMessageExpr(ObjCMessageExpr *E); - unsigned VisitObjCSuperExpr(ObjCSuperExpr *E); - unsigned VisitObjCIsaExpr(ObjCIsaExpr *E); - - unsigned VisitObjCForCollectionStmt(ObjCForCollectionStmt *); - unsigned VisitObjCAtCatchStmt(ObjCAtCatchStmt *); - unsigned VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *); - unsigned VisitObjCAtTryStmt(ObjCAtTryStmt *); - unsigned VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *); - unsigned VisitObjCAtThrowStmt(ObjCAtThrowStmt *); - - unsigned VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E); - unsigned VisitCXXConstructExpr(CXXConstructExpr *E); - unsigned VisitCXXNamedCastExpr(CXXNamedCastExpr *E); - unsigned VisitCXXStaticCastExpr(CXXStaticCastExpr *E); - unsigned VisitCXXDynamicCastExpr(CXXDynamicCastExpr *E); - unsigned VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *E); - unsigned VisitCXXConstCastExpr(CXXConstCastExpr *E); - unsigned VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E); - unsigned VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E); - unsigned VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E); - unsigned VisitCXXTypeidExpr(CXXTypeidExpr *E); - unsigned VisitCXXThisExpr(CXXThisExpr *E); - unsigned VisitCXXThrowExpr(CXXThrowExpr *E); - unsigned VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E); - unsigned VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E); + void VisitObjCMessageExpr(ObjCMessageExpr *E); + void VisitObjCSuperExpr(ObjCSuperExpr *E); + void VisitObjCIsaExpr(ObjCIsaExpr *E); + + void VisitObjCForCollectionStmt(ObjCForCollectionStmt *); + void VisitObjCAtCatchStmt(ObjCAtCatchStmt *); + void VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *); + void VisitObjCAtTryStmt(ObjCAtTryStmt *); + void VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *); + void VisitObjCAtThrowStmt(ObjCAtThrowStmt *); + + void VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E); + void VisitCXXConstructExpr(CXXConstructExpr *E); + void VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E); + void VisitCXXNamedCastExpr(CXXNamedCastExpr *E); + void VisitCXXStaticCastExpr(CXXStaticCastExpr *E); + void VisitCXXDynamicCastExpr(CXXDynamicCastExpr *E); + void VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *E); + void VisitCXXConstCastExpr(CXXConstCastExpr *E); + void VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E); + void VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E); + void VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E); + void VisitCXXTypeidExpr(CXXTypeidExpr *E); + void VisitCXXThisExpr(CXXThisExpr *E); + void VisitCXXThrowExpr(CXXThrowExpr *E); + void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E); + void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E); + void VisitCXXBindReferenceExpr(CXXBindReferenceExpr *E); - unsigned VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E); - unsigned VisitCXXNewExpr(CXXNewExpr *E); + void VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E); + void VisitCXXNewExpr(CXXNewExpr *E); + void VisitCXXDeleteExpr(CXXDeleteExpr *E); + void VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E); - unsigned VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E); + void VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E); + + void VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E); + void VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E); + void VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *E); + + void VisitOverloadExpr(OverloadExpr *E); + void VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E); + void VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E); + + void VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E); }; } -unsigned PCHStmtReader::VisitStmt(Stmt *S) { +void PCHStmtReader:: +ReadExplicitTemplateArgumentList(ExplicitTemplateArgumentList &ArgList, + unsigned NumTemplateArgs) { + TemplateArgumentListInfo ArgInfo; + ArgInfo.setLAngleLoc(Reader.ReadSourceLocation(Record, Idx)); + ArgInfo.setRAngleLoc(Reader.ReadSourceLocation(Record, Idx)); + for (unsigned i = 0; i != NumTemplateArgs; ++i) + ArgInfo.addArgument(Reader.ReadTemplateArgumentLoc(Record, Idx)); + ArgList.initializeFrom(ArgInfo); +} + +void PCHStmtReader::VisitStmt(Stmt *S) { assert(Idx == NumStmtFields && "Incorrect statement field count"); - return 0; } -unsigned PCHStmtReader::VisitNullStmt(NullStmt *S) { +void PCHStmtReader::VisitNullStmt(NullStmt *S) { VisitStmt(S); S->setSemiLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); - return 0; } -unsigned PCHStmtReader::VisitCompoundStmt(CompoundStmt *S) { +void PCHStmtReader::VisitCompoundStmt(CompoundStmt *S) { VisitStmt(S); + llvm::SmallVector<Stmt *, 16> Stmts; unsigned NumStmts = Record[Idx++]; - S->setStmts(*Reader.getContext(), - StmtStack.data() + StmtStack.size() - NumStmts, NumStmts); + while (NumStmts--) + Stmts.push_back(Reader.ReadSubStmt()); + S->setStmts(*Reader.getContext(), Stmts.data(), Stmts.size()); S->setLBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); S->setRBracLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); - return NumStmts; } -unsigned PCHStmtReader::VisitSwitchCase(SwitchCase *S) { +void PCHStmtReader::VisitSwitchCase(SwitchCase *S) { VisitStmt(S); Reader.RecordSwitchCaseID(S, Record[Idx++]); - return 0; } -unsigned PCHStmtReader::VisitCaseStmt(CaseStmt *S) { +void PCHStmtReader::VisitCaseStmt(CaseStmt *S) { VisitSwitchCase(S); - S->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 3])); - S->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2])); - S->setSubStmt(StmtStack.back()); + S->setLHS(Reader.ReadSubExpr()); + S->setRHS(Reader.ReadSubExpr()); + S->setSubStmt(Reader.ReadSubStmt()); S->setCaseLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); S->setEllipsisLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); S->setColonLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); - return 3; } -unsigned PCHStmtReader::VisitDefaultStmt(DefaultStmt *S) { +void PCHStmtReader::VisitDefaultStmt(DefaultStmt *S) { VisitSwitchCase(S); - S->setSubStmt(StmtStack.back()); + S->setSubStmt(Reader.ReadSubStmt()); S->setDefaultLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); S->setColonLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); - return 1; } -unsigned PCHStmtReader::VisitLabelStmt(LabelStmt *S) { +void PCHStmtReader::VisitLabelStmt(LabelStmt *S) { VisitStmt(S); S->setID(Reader.GetIdentifierInfo(Record, Idx)); - S->setSubStmt(StmtStack.back()); + S->setSubStmt(Reader.ReadSubStmt()); S->setIdentLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); Reader.RecordLabelStmt(S, Record[Idx++]); - return 1; } -unsigned PCHStmtReader::VisitIfStmt(IfStmt *S) { +void PCHStmtReader::VisitIfStmt(IfStmt *S) { VisitStmt(S); - S->setConditionVariable(cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++]))); - S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3])); - S->setThen(StmtStack[StmtStack.size() - 2]); - S->setElse(StmtStack[StmtStack.size() - 1]); + S->setConditionVariable(*Reader.getContext(), + cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++]))); + S->setCond(Reader.ReadSubExpr()); + S->setThen(Reader.ReadSubStmt()); + S->setElse(Reader.ReadSubStmt()); S->setIfLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); S->setElseLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); - return 3; } -unsigned PCHStmtReader::VisitSwitchStmt(SwitchStmt *S) { +void PCHStmtReader::VisitSwitchStmt(SwitchStmt *S) { VisitStmt(S); - S->setConditionVariable(cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++]))); - S->setCond(cast<Expr>(StmtStack[StmtStack.size() - 2])); - S->setBody(StmtStack.back()); + S->setConditionVariable(*Reader.getContext(), + cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++]))); + S->setCond(Reader.ReadSubExpr()); + S->setBody(Reader.ReadSubStmt()); S->setSwitchLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); SwitchCase *PrevSC = 0; for (unsigned N = Record.size(); Idx != N; ++Idx) { @@ -224,78 +245,71 @@ unsigned PCHStmtReader::VisitSwitchStmt(SwitchStmt *S) { SC->Retain(); PrevSC = SC; } - return 2; } -unsigned PCHStmtReader::VisitWhileStmt(WhileStmt *S) { +void PCHStmtReader::VisitWhileStmt(WhileStmt *S) { VisitStmt(S); - S->setConditionVariable(cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++]))); - S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2])); - S->setBody(StmtStack.back()); + S->setConditionVariable(*Reader.getContext(), + cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++]))); + S->setCond(Reader.ReadSubExpr()); + S->setBody(Reader.ReadSubStmt()); S->setWhileLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); - return 2; } -unsigned PCHStmtReader::VisitDoStmt(DoStmt *S) { +void PCHStmtReader::VisitDoStmt(DoStmt *S) { VisitStmt(S); - S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2])); - S->setBody(StmtStack.back()); + S->setCond(Reader.ReadSubExpr()); + S->setBody(Reader.ReadSubStmt()); S->setDoLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); S->setWhileLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); S->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); - return 2; } -unsigned PCHStmtReader::VisitForStmt(ForStmt *S) { +void PCHStmtReader::VisitForStmt(ForStmt *S) { VisitStmt(S); - S->setInit(StmtStack[StmtStack.size() - 4]); - S->setCond(cast_or_null<Expr>(StmtStack[StmtStack.size() - 3])); - S->setConditionVariable(cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++]))); - S->setInc(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2])); - S->setBody(StmtStack.back()); + S->setInit(Reader.ReadSubStmt()); + S->setCond(Reader.ReadSubExpr()); + S->setConditionVariable(*Reader.getContext(), + cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++]))); + S->setInc(Reader.ReadSubExpr()); + S->setBody(Reader.ReadSubStmt()); S->setForLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); S->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); S->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); - return 4; } -unsigned PCHStmtReader::VisitGotoStmt(GotoStmt *S) { +void PCHStmtReader::VisitGotoStmt(GotoStmt *S) { VisitStmt(S); Reader.SetLabelOf(S, Record[Idx++]); S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); S->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); - return 0; } -unsigned PCHStmtReader::VisitIndirectGotoStmt(IndirectGotoStmt *S) { +void PCHStmtReader::VisitIndirectGotoStmt(IndirectGotoStmt *S) { VisitStmt(S); S->setGotoLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); S->setStarLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); - S->setTarget(cast_or_null<Expr>(StmtStack.back())); - return 1; + S->setTarget(Reader.ReadSubExpr()); } -unsigned PCHStmtReader::VisitContinueStmt(ContinueStmt *S) { +void PCHStmtReader::VisitContinueStmt(ContinueStmt *S) { VisitStmt(S); S->setContinueLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); - return 0; } -unsigned PCHStmtReader::VisitBreakStmt(BreakStmt *S) { +void PCHStmtReader::VisitBreakStmt(BreakStmt *S) { VisitStmt(S); S->setBreakLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); - return 0; } -unsigned PCHStmtReader::VisitReturnStmt(ReturnStmt *S) { +void PCHStmtReader::VisitReturnStmt(ReturnStmt *S) { VisitStmt(S); - S->setRetValue(cast_or_null<Expr>(StmtStack.back())); + S->setRetValue(Reader.ReadSubExpr()); S->setReturnLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); S->setNRVOCandidate(cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++]))); - return 1; } -unsigned PCHStmtReader::VisitDeclStmt(DeclStmt *S) { +void PCHStmtReader::VisitDeclStmt(DeclStmt *S) { VisitStmt(S); S->setStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); S->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); @@ -312,10 +326,9 @@ unsigned PCHStmtReader::VisitDeclStmt(DeclStmt *S) { Decls.data(), Decls.size()))); } - return 0; } -unsigned PCHStmtReader::VisitAsmStmt(AsmStmt *S) { +void PCHStmtReader::VisitAsmStmt(AsmStmt *S) { VisitStmt(S); unsigned NumOutputs = Record[Idx++]; unsigned NumInputs = Record[Idx++]; @@ -326,9 +339,7 @@ unsigned PCHStmtReader::VisitAsmStmt(AsmStmt *S) { S->setSimple(Record[Idx++]); S->setMSAsm(Record[Idx++]); - unsigned StackIdx - = StmtStack.size() - (NumOutputs*2 + NumInputs*2 + NumClobbers + 1); - S->setAsmString(cast_or_null<StringLiteral>(StmtStack[StackIdx++])); + S->setAsmString(cast_or_null<StringLiteral>(Reader.ReadSubStmt())); // Outputs and inputs llvm::SmallVector<IdentifierInfo *, 16> Names; @@ -336,71 +347,76 @@ unsigned PCHStmtReader::VisitAsmStmt(AsmStmt *S) { llvm::SmallVector<Stmt*, 16> Exprs; for (unsigned I = 0, N = NumOutputs + NumInputs; I != N; ++I) { Names.push_back(Reader.GetIdentifierInfo(Record, Idx)); - Constraints.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++])); - Exprs.push_back(StmtStack[StackIdx++]); + Constraints.push_back(cast_or_null<StringLiteral>(Reader.ReadSubStmt())); + Exprs.push_back(Reader.ReadSubStmt()); } // Constraints llvm::SmallVector<StringLiteral*, 16> Clobbers; for (unsigned I = 0; I != NumClobbers; ++I) - Clobbers.push_back(cast_or_null<StringLiteral>(StmtStack[StackIdx++])); + Clobbers.push_back(cast_or_null<StringLiteral>(Reader.ReadSubStmt())); S->setOutputsAndInputsAndClobbers(*Reader.getContext(), Names.data(), Constraints.data(), Exprs.data(), NumOutputs, NumInputs, Clobbers.data(), NumClobbers); - - assert(StackIdx == StmtStack.size() && "Error deserializing AsmStmt"); - return NumOutputs*2 + NumInputs*2 + NumClobbers + 1; } -unsigned PCHStmtReader::VisitExpr(Expr *E) { +void PCHStmtReader::VisitExpr(Expr *E) { VisitStmt(E); E->setType(Reader.GetType(Record[Idx++])); E->setTypeDependent(Record[Idx++]); E->setValueDependent(Record[Idx++]); assert(Idx == NumExprFields && "Incorrect expression field count"); - return 0; } -unsigned PCHStmtReader::VisitPredefinedExpr(PredefinedExpr *E) { +void PCHStmtReader::VisitPredefinedExpr(PredefinedExpr *E) { VisitExpr(E); E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++])); E->setIdentType((PredefinedExpr::IdentType)Record[Idx++]); - return 0; } -unsigned PCHStmtReader::VisitDeclRefExpr(DeclRefExpr *E) { +void PCHStmtReader::VisitDeclRefExpr(DeclRefExpr *E) { VisitExpr(E); + + bool HasQualifier = Record[Idx++]; + unsigned NumTemplateArgs = Record[Idx++]; + + E->DecoratedD.setInt((HasQualifier? DeclRefExpr::HasQualifierFlag : 0) | + (NumTemplateArgs ? DeclRefExpr::HasExplicitTemplateArgumentListFlag : 0)); + + if (HasQualifier) { + E->getNameQualifier()->NNS = Reader.ReadNestedNameSpecifier(Record, Idx); + E->getNameQualifier()->Range = Reader.ReadSourceRange(Record, Idx); + } + + if (NumTemplateArgs) + ReadExplicitTemplateArgumentList(*E->getExplicitTemplateArgumentList(), + NumTemplateArgs); + E->setDecl(cast<ValueDecl>(Reader.GetDecl(Record[Idx++]))); - E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++])); - // FIXME: read qualifier - // FIXME: read explicit template arguments - return 0; + E->setLocation(Reader.ReadSourceLocation(Record, Idx)); } -unsigned PCHStmtReader::VisitIntegerLiteral(IntegerLiteral *E) { +void PCHStmtReader::VisitIntegerLiteral(IntegerLiteral *E) { VisitExpr(E); E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++])); E->setValue(Reader.ReadAPInt(Record, Idx)); - return 0; } -unsigned PCHStmtReader::VisitFloatingLiteral(FloatingLiteral *E) { +void PCHStmtReader::VisitFloatingLiteral(FloatingLiteral *E) { VisitExpr(E); E->setValue(Reader.ReadAPFloat(Record, Idx)); E->setExact(Record[Idx++]); E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++])); - return 0; } -unsigned PCHStmtReader::VisitImaginaryLiteral(ImaginaryLiteral *E) { +void PCHStmtReader::VisitImaginaryLiteral(ImaginaryLiteral *E) { VisitExpr(E); - E->setSubExpr(cast<Expr>(StmtStack.back())); - return 1; + E->setSubExpr(Reader.ReadSubExpr()); } -unsigned PCHStmtReader::VisitStringLiteral(StringLiteral *E) { +void PCHStmtReader::VisitStringLiteral(StringLiteral *E) { VisitExpr(E); unsigned Len = Record[Idx++]; assert(Record[Idx] == E->getNumConcatenated() && @@ -416,35 +432,41 @@ unsigned PCHStmtReader::VisitStringLiteral(StringLiteral *E) { // Read source locations for (unsigned I = 0, N = E->getNumConcatenated(); I != N; ++I) E->setStrTokenLoc(I, SourceLocation::getFromRawEncoding(Record[Idx++])); - - return 0; } -unsigned PCHStmtReader::VisitCharacterLiteral(CharacterLiteral *E) { +void PCHStmtReader::VisitCharacterLiteral(CharacterLiteral *E) { VisitExpr(E); E->setValue(Record[Idx++]); E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++])); E->setWide(Record[Idx++]); - return 0; } -unsigned PCHStmtReader::VisitParenExpr(ParenExpr *E) { +void PCHStmtReader::VisitParenExpr(ParenExpr *E) { VisitExpr(E); E->setLParen(SourceLocation::getFromRawEncoding(Record[Idx++])); E->setRParen(SourceLocation::getFromRawEncoding(Record[Idx++])); - E->setSubExpr(cast<Expr>(StmtStack.back())); - return 1; + E->setSubExpr(Reader.ReadSubExpr()); +} + +void PCHStmtReader::VisitParenListExpr(ParenListExpr *E) { + VisitExpr(E); + unsigned NumExprs = Record[Idx++]; + E->Exprs = new (*Reader.getContext()) Stmt*[NumExprs]; + for (unsigned i = 0; i != NumExprs; ++i) + E->Exprs[i] = Reader.ReadSubStmt(); + E->NumExprs = NumExprs; + E->LParenLoc = Reader.ReadSourceLocation(Record, Idx); + E->RParenLoc = Reader.ReadSourceLocation(Record, Idx); } -unsigned PCHStmtReader::VisitUnaryOperator(UnaryOperator *E) { +void PCHStmtReader::VisitUnaryOperator(UnaryOperator *E) { VisitExpr(E); - E->setSubExpr(cast<Expr>(StmtStack.back())); + E->setSubExpr(Reader.ReadSubExpr()); E->setOpcode((UnaryOperator::Opcode)Record[Idx++]); E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); - return 1; } -unsigned PCHStmtReader::VisitOffsetOfExpr(OffsetOfExpr *E) { +void PCHStmtReader::VisitOffsetOfExpr(OffsetOfExpr *E) { typedef OffsetOfExpr::OffsetOfNode Node; VisitExpr(E); assert(E->getNumComponents() == Record[Idx]); @@ -482,153 +504,141 @@ unsigned PCHStmtReader::VisitOffsetOfExpr(OffsetOfExpr *E) { } for (unsigned I = 0, N = E->getNumExpressions(); I != N; ++I) - E->setIndexExpr(I, cast_or_null<Expr>(StmtStack[StmtStack.size() - N + I])); - - return E->getNumExpressions(); + E->setIndexExpr(I, Reader.ReadSubExpr()); } -unsigned PCHStmtReader::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) { +void PCHStmtReader::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) { VisitExpr(E); E->setSizeof(Record[Idx++]); if (Record[Idx] == 0) { - E->setArgument(cast<Expr>(StmtStack.back())); + E->setArgument(Reader.ReadSubExpr()); ++Idx; } else { E->setArgument(Reader.GetTypeSourceInfo(Record, Idx)); } E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); - return E->isArgumentType()? 0 : 1; } -unsigned PCHStmtReader::VisitArraySubscriptExpr(ArraySubscriptExpr *E) { +void PCHStmtReader::VisitArraySubscriptExpr(ArraySubscriptExpr *E) { VisitExpr(E); - E->setLHS(cast<Expr>(StmtStack[StmtStack.size() - 2])); - E->setRHS(cast<Expr>(StmtStack[StmtStack.size() - 1])); + E->setLHS(Reader.ReadSubExpr()); + E->setRHS(Reader.ReadSubExpr()); E->setRBracketLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); - return 2; } -unsigned PCHStmtReader::VisitCallExpr(CallExpr *E) { +void PCHStmtReader::VisitCallExpr(CallExpr *E) { VisitExpr(E); E->setNumArgs(*Reader.getContext(), Record[Idx++]); E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); - E->setCallee(cast<Expr>(StmtStack[StmtStack.size() - E->getNumArgs() - 1])); + E->setCallee(Reader.ReadSubExpr()); for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) - E->setArg(I, cast<Expr>(StmtStack[StmtStack.size() - N + I])); - return E->getNumArgs() + 1; + E->setArg(I, Reader.ReadSubExpr()); } -unsigned PCHStmtReader::VisitMemberExpr(MemberExpr *E) { - VisitExpr(E); - E->setBase(cast<Expr>(StmtStack.back())); - E->setMemberDecl(cast<ValueDecl>(Reader.GetDecl(Record[Idx++]))); - E->setMemberLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); - E->setArrow(Record[Idx++]); - return 1; +void PCHStmtReader::VisitMemberExpr(MemberExpr *E) { + // Don't call VisitExpr, this is fully initialized at creation. + assert(E->getStmtClass() == Stmt::MemberExprClass && + "It's a subclass, we must advance Idx!"); } -unsigned PCHStmtReader::VisitObjCIsaExpr(ObjCIsaExpr *E) { +void PCHStmtReader::VisitObjCIsaExpr(ObjCIsaExpr *E) { VisitExpr(E); - E->setBase(cast<Expr>(StmtStack.back())); + E->setBase(Reader.ReadSubExpr()); E->setIsaMemberLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); E->setArrow(Record[Idx++]); - return 1; } -unsigned PCHStmtReader::VisitCastExpr(CastExpr *E) { +void PCHStmtReader::VisitCastExpr(CastExpr *E) { VisitExpr(E); - E->setSubExpr(cast<Expr>(StmtStack.back())); + E->setSubExpr(Reader.ReadSubExpr()); E->setCastKind((CastExpr::CastKind)Record[Idx++]); - return 1; + CXXBaseSpecifierArray &BasePath = E->getBasePath(); + unsigned NumBaseSpecs = Record[Idx++]; + while (NumBaseSpecs--) { + // FIXME: These gets leaked. + CXXBaseSpecifier *BaseSpec = new (*Reader.getContext()) CXXBaseSpecifier; + *BaseSpec = Reader.ReadCXXBaseSpecifier(Record, Idx); + BasePath.push_back(BaseSpec); + } } -unsigned PCHStmtReader::VisitBinaryOperator(BinaryOperator *E) { +void PCHStmtReader::VisitBinaryOperator(BinaryOperator *E) { VisitExpr(E); - E->setLHS(cast<Expr>(StmtStack.end()[-2])); - E->setRHS(cast<Expr>(StmtStack.end()[-1])); + E->setLHS(Reader.ReadSubExpr()); + E->setRHS(Reader.ReadSubExpr()); E->setOpcode((BinaryOperator::Opcode)Record[Idx++]); E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); - return 2; } -unsigned PCHStmtReader::VisitCompoundAssignOperator(CompoundAssignOperator *E) { +void PCHStmtReader::VisitCompoundAssignOperator(CompoundAssignOperator *E) { VisitBinaryOperator(E); E->setComputationLHSType(Reader.GetType(Record[Idx++])); E->setComputationResultType(Reader.GetType(Record[Idx++])); - return 2; } -unsigned PCHStmtReader::VisitConditionalOperator(ConditionalOperator *E) { +void PCHStmtReader::VisitConditionalOperator(ConditionalOperator *E) { VisitExpr(E); - E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3])); - E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2])); - E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1])); + E->setCond(Reader.ReadSubExpr()); + E->setLHS(Reader.ReadSubExpr()); + E->setRHS(Reader.ReadSubExpr()); E->setQuestionLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); E->setColonLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); - return 3; } -unsigned PCHStmtReader::VisitImplicitCastExpr(ImplicitCastExpr *E) { +void PCHStmtReader::VisitImplicitCastExpr(ImplicitCastExpr *E) { VisitCastExpr(E); E->setLvalueCast(Record[Idx++]); - return 1; } -unsigned PCHStmtReader::VisitExplicitCastExpr(ExplicitCastExpr *E) { +void PCHStmtReader::VisitExplicitCastExpr(ExplicitCastExpr *E) { VisitCastExpr(E); E->setTypeInfoAsWritten(Reader.GetTypeSourceInfo(Record, Idx)); - return 1; } -unsigned PCHStmtReader::VisitCStyleCastExpr(CStyleCastExpr *E) { +void PCHStmtReader::VisitCStyleCastExpr(CStyleCastExpr *E) { VisitExplicitCastExpr(E); E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); - return 1; } -unsigned PCHStmtReader::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { +void PCHStmtReader::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { VisitExpr(E); E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); E->setTypeSourceInfo(Reader.GetTypeSourceInfo(Record, Idx)); - E->setInitializer(cast<Expr>(StmtStack.back())); + E->setInitializer(Reader.ReadSubExpr()); E->setFileScope(Record[Idx++]); - return 1; } -unsigned PCHStmtReader::VisitExtVectorElementExpr(ExtVectorElementExpr *E) { +void PCHStmtReader::VisitExtVectorElementExpr(ExtVectorElementExpr *E) { VisitExpr(E); - E->setBase(cast<Expr>(StmtStack.back())); + E->setBase(Reader.ReadSubExpr()); E->setAccessor(Reader.GetIdentifierInfo(Record, Idx)); E->setAccessorLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); - return 1; } -unsigned PCHStmtReader::VisitInitListExpr(InitListExpr *E) { +void PCHStmtReader::VisitInitListExpr(InitListExpr *E) { VisitExpr(E); unsigned NumInits = Record[Idx++]; E->reserveInits(*Reader.getContext(), NumInits); for (unsigned I = 0; I != NumInits; ++I) - E->updateInit(*Reader.getContext(), I, - cast<Expr>(StmtStack[StmtStack.size() - NumInits - 1 + I])); - E->setSyntacticForm(cast_or_null<InitListExpr>(StmtStack.back())); + E->updateInit(*Reader.getContext(), I, Reader.ReadSubExpr()); + E->setSyntacticForm(cast_or_null<InitListExpr>(Reader.ReadSubStmt())); E->setLBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); E->setRBraceLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); E->setInitializedFieldInUnion( cast_or_null<FieldDecl>(Reader.GetDecl(Record[Idx++]))); E->sawArrayRangeDesignator(Record[Idx++]); - return NumInits + 1; } -unsigned PCHStmtReader::VisitDesignatedInitExpr(DesignatedInitExpr *E) { +void PCHStmtReader::VisitDesignatedInitExpr(DesignatedInitExpr *E) { typedef DesignatedInitExpr::Designator Designator; VisitExpr(E); unsigned NumSubExprs = Record[Idx++]; assert(NumSubExprs == E->getNumSubExprs() && "Wrong number of subexprs"); for (unsigned I = 0; I != NumSubExprs; ++I) - E->setSubExpr(I, cast<Expr>(StmtStack[StmtStack.size() - NumSubExprs + I])); + E->setSubExpr(I, Reader.ReadSubExpr()); E->setEqualOrColonLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); E->setGNUSyntax(Record[Idx++]); @@ -683,143 +693,128 @@ unsigned PCHStmtReader::VisitDesignatedInitExpr(DesignatedInitExpr *E) { } E->setDesignators(*Reader.getContext(), Designators.data(), Designators.size()); - - return NumSubExprs; } -unsigned PCHStmtReader::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) { +void PCHStmtReader::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) { VisitExpr(E); - return 0; } -unsigned PCHStmtReader::VisitVAArgExpr(VAArgExpr *E) { +void PCHStmtReader::VisitVAArgExpr(VAArgExpr *E) { VisitExpr(E); - E->setSubExpr(cast<Expr>(StmtStack.back())); + E->setSubExpr(Reader.ReadSubExpr()); E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); - return 1; } -unsigned PCHStmtReader::VisitAddrLabelExpr(AddrLabelExpr *E) { +void PCHStmtReader::VisitAddrLabelExpr(AddrLabelExpr *E) { VisitExpr(E); E->setAmpAmpLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); E->setLabelLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); Reader.SetLabelOf(E, Record[Idx++]); - return 0; } -unsigned PCHStmtReader::VisitStmtExpr(StmtExpr *E) { +void PCHStmtReader::VisitStmtExpr(StmtExpr *E) { VisitExpr(E); E->setLParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); - E->setSubStmt(cast_or_null<CompoundStmt>(StmtStack.back())); - return 1; + E->setSubStmt(cast_or_null<CompoundStmt>(Reader.ReadSubStmt())); } -unsigned PCHStmtReader::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) { +void PCHStmtReader::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) { VisitExpr(E); E->setArgType1(Reader.GetType(Record[Idx++])); E->setArgType2(Reader.GetType(Record[Idx++])); E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); - return 0; } -unsigned PCHStmtReader::VisitChooseExpr(ChooseExpr *E) { +void PCHStmtReader::VisitChooseExpr(ChooseExpr *E) { VisitExpr(E); - E->setCond(cast<Expr>(StmtStack[StmtStack.size() - 3])); - E->setLHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2])); - E->setRHS(cast_or_null<Expr>(StmtStack[StmtStack.size() - 1])); + E->setCond(Reader.ReadSubExpr()); + E->setLHS(Reader.ReadSubExpr()); + E->setRHS(Reader.ReadSubExpr()); E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); - return 3; } -unsigned PCHStmtReader::VisitGNUNullExpr(GNUNullExpr *E) { +void PCHStmtReader::VisitGNUNullExpr(GNUNullExpr *E) { VisitExpr(E); E->setTokenLocation(SourceLocation::getFromRawEncoding(Record[Idx++])); - return 0; } -unsigned PCHStmtReader::VisitShuffleVectorExpr(ShuffleVectorExpr *E) { +void PCHStmtReader::VisitShuffleVectorExpr(ShuffleVectorExpr *E) { VisitExpr(E); + llvm::SmallVector<Expr *, 16> Exprs; unsigned NumExprs = Record[Idx++]; - E->setExprs(*Reader.getContext(), - (Expr **)&StmtStack[StmtStack.size() - NumExprs], NumExprs); + while (NumExprs--) + Exprs.push_back(Reader.ReadSubExpr()); + E->setExprs(*Reader.getContext(), Exprs.data(), Exprs.size()); E->setBuiltinLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); - return NumExprs; } -unsigned PCHStmtReader::VisitBlockExpr(BlockExpr *E) { +void PCHStmtReader::VisitBlockExpr(BlockExpr *E) { VisitExpr(E); E->setBlockDecl(cast_or_null<BlockDecl>(Reader.GetDecl(Record[Idx++]))); E->setHasBlockDeclRefExprs(Record[Idx++]); - return 0; } -unsigned PCHStmtReader::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) { +void PCHStmtReader::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) { VisitExpr(E); E->setDecl(cast<ValueDecl>(Reader.GetDecl(Record[Idx++]))); E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++])); E->setByRef(Record[Idx++]); E->setConstQualAdded(Record[Idx++]); - return 0; + E->setCopyConstructorExpr(Reader.ReadSubExpr()); } //===----------------------------------------------------------------------===// // Objective-C Expressions and Statements -unsigned PCHStmtReader::VisitObjCStringLiteral(ObjCStringLiteral *E) { +void PCHStmtReader::VisitObjCStringLiteral(ObjCStringLiteral *E) { VisitExpr(E); - E->setString(cast<StringLiteral>(StmtStack.back())); + E->setString(cast<StringLiteral>(Reader.ReadSubStmt())); E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); - return 1; } -unsigned PCHStmtReader::VisitObjCEncodeExpr(ObjCEncodeExpr *E) { +void PCHStmtReader::VisitObjCEncodeExpr(ObjCEncodeExpr *E) { VisitExpr(E); E->setEncodedTypeSourceInfo(Reader.GetTypeSourceInfo(Record, Idx)); E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); - return 0; } -unsigned PCHStmtReader::VisitObjCSelectorExpr(ObjCSelectorExpr *E) { +void PCHStmtReader::VisitObjCSelectorExpr(ObjCSelectorExpr *E) { VisitExpr(E); E->setSelector(Reader.GetSelector(Record, Idx)); E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); - return 0; } -unsigned PCHStmtReader::VisitObjCProtocolExpr(ObjCProtocolExpr *E) { +void PCHStmtReader::VisitObjCProtocolExpr(ObjCProtocolExpr *E) { VisitExpr(E); E->setProtocol(cast<ObjCProtocolDecl>(Reader.GetDecl(Record[Idx++]))); E->setAtLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); - return 0; } -unsigned PCHStmtReader::VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) { +void PCHStmtReader::VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) { VisitExpr(E); E->setDecl(cast<ObjCIvarDecl>(Reader.GetDecl(Record[Idx++]))); E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++])); - E->setBase(cast<Expr>(StmtStack.back())); + E->setBase(Reader.ReadSubExpr()); E->setIsArrow(Record[Idx++]); E->setIsFreeIvar(Record[Idx++]); - return 1; } -unsigned PCHStmtReader::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) { +void PCHStmtReader::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) { VisitExpr(E); E->setProperty(cast<ObjCPropertyDecl>(Reader.GetDecl(Record[Idx++]))); E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++])); - E->setBase(cast<Expr>(StmtStack.back())); - return 1; + E->setBase(Reader.ReadSubExpr()); } -unsigned PCHStmtReader::VisitObjCImplicitSetterGetterRefExpr( +void PCHStmtReader::VisitObjCImplicitSetterGetterRefExpr( ObjCImplicitSetterGetterRefExpr *E) { VisitExpr(E); E->setGetterMethod( @@ -828,13 +823,12 @@ unsigned PCHStmtReader::VisitObjCImplicitSetterGetterRefExpr( cast_or_null<ObjCMethodDecl>(Reader.GetDecl(Record[Idx++]))); E->setInterfaceDecl( cast_or_null<ObjCInterfaceDecl>(Reader.GetDecl(Record[Idx++]))); - E->setBase(cast_or_null<Expr>(StmtStack.back())); + E->setBase(Reader.ReadSubExpr()); E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++])); E->setClassLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); - return 1; } -unsigned PCHStmtReader::VisitObjCMessageExpr(ObjCMessageExpr *E) { +void PCHStmtReader::VisitObjCMessageExpr(ObjCMessageExpr *E) { VisitExpr(E); assert(Record[Idx] == E->getNumArgs()); ++Idx; @@ -842,8 +836,7 @@ unsigned PCHStmtReader::VisitObjCMessageExpr(ObjCMessageExpr *E) { = static_cast<ObjCMessageExpr::ReceiverKind>(Record[Idx++]); switch (Kind) { case ObjCMessageExpr::Instance: - E->setInstanceReceiver( - cast_or_null<Expr>(StmtStack[StmtStack.size() - E->getNumArgs() - 1])); + E->setInstanceReceiver(Reader.ReadSubExpr()); break; case ObjCMessageExpr::Class: @@ -870,193 +863,185 @@ unsigned PCHStmtReader::VisitObjCMessageExpr(ObjCMessageExpr *E) { E->setRightLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) - E->setArg(I, cast<Expr>(StmtStack[StmtStack.size() - N + I])); - return E->getNumArgs() + (Kind == ObjCMessageExpr::Instance); + E->setArg(I, Reader.ReadSubExpr()); } -unsigned PCHStmtReader::VisitObjCSuperExpr(ObjCSuperExpr *E) { +void PCHStmtReader::VisitObjCSuperExpr(ObjCSuperExpr *E) { VisitExpr(E); E->setLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); - return 0; } -unsigned PCHStmtReader::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) { +void PCHStmtReader::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) { VisitStmt(S); - S->setElement(cast_or_null<Stmt>(StmtStack[StmtStack.size() - 3])); - S->setCollection(cast_or_null<Expr>(StmtStack[StmtStack.size() - 2])); - S->setBody(cast_or_null<Stmt>(StmtStack[StmtStack.size() - 1])); + S->setElement(Reader.ReadSubStmt()); + S->setCollection(Reader.ReadSubExpr()); + S->setBody(Reader.ReadSubStmt()); S->setForLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); S->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); - return 3; } -unsigned PCHStmtReader::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) { +void PCHStmtReader::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) { VisitStmt(S); - S->setCatchBody(cast_or_null<Stmt>(StmtStack.back())); + S->setCatchBody(Reader.ReadSubStmt()); S->setCatchParamDecl(cast_or_null<VarDecl>(Reader.GetDecl(Record[Idx++]))); S->setAtCatchLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); S->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); - return 1; } -unsigned PCHStmtReader::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) { +void PCHStmtReader::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) { VisitStmt(S); - S->setFinallyBody(StmtStack.back()); + S->setFinallyBody(Reader.ReadSubStmt()); S->setAtFinallyLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); - return 1; } -unsigned PCHStmtReader::VisitObjCAtTryStmt(ObjCAtTryStmt *S) { +void PCHStmtReader::VisitObjCAtTryStmt(ObjCAtTryStmt *S) { VisitStmt(S); assert(Record[Idx] == S->getNumCatchStmts()); ++Idx; bool HasFinally = Record[Idx++]; - for (unsigned I = 0, N = S->getNumCatchStmts(); I != N; ++I) { - unsigned Offset = StmtStack.size() - N - HasFinally + I; - S->setCatchStmt(I, cast_or_null<ObjCAtCatchStmt>(StmtStack[Offset])); - } + S->setTryBody(Reader.ReadSubStmt()); + for (unsigned I = 0, N = S->getNumCatchStmts(); I != N; ++I) + S->setCatchStmt(I, cast_or_null<ObjCAtCatchStmt>(Reader.ReadSubStmt())); - unsigned TryOffset - = StmtStack.size() - S->getNumCatchStmts() - HasFinally - 1; - S->setTryBody(cast_or_null<Stmt>(StmtStack[TryOffset])); if (HasFinally) - S->setFinallyStmt(cast_or_null<Stmt>(StmtStack[StmtStack.size() - 1])); + S->setFinallyStmt(Reader.ReadSubStmt()); S->setAtTryLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); - return 1 + S->getNumCatchStmts() + HasFinally; } -unsigned PCHStmtReader::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S) { +void PCHStmtReader::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S) { VisitStmt(S); - S->setSynchExpr(cast_or_null<Stmt>(StmtStack[StmtStack.size() - 2])); - S->setSynchBody(cast_or_null<Stmt>(StmtStack[StmtStack.size() - 1])); + S->setSynchExpr(Reader.ReadSubStmt()); + S->setSynchBody(Reader.ReadSubStmt()); S->setAtSynchronizedLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); - return 2; } -unsigned PCHStmtReader::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) { +void PCHStmtReader::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) { VisitStmt(S); - S->setThrowExpr(StmtStack.back()); + S->setThrowExpr(Reader.ReadSubStmt()); S->setThrowLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); - return 1; } //===----------------------------------------------------------------------===// // C++ Expressions and Statements -unsigned PCHStmtReader::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E) { - unsigned num = VisitCallExpr(E); +void PCHStmtReader::VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E) { + VisitCallExpr(E); E->setOperator((OverloadedOperatorKind)Record[Idx++]); - return num; } -unsigned PCHStmtReader::VisitCXXConstructExpr(CXXConstructExpr *E) { +void PCHStmtReader::VisitCXXConstructExpr(CXXConstructExpr *E) { VisitExpr(E); + E->NumArgs = Record[Idx++]; + if (E->NumArgs) + E->Args = new (*Reader.getContext()) Stmt*[E->NumArgs]; + for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) + E->setArg(I, Reader.ReadSubExpr()); E->setConstructor(cast<CXXConstructorDecl>(Reader.GetDecl(Record[Idx++]))); E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++])); E->setElidable(Record[Idx++]); E->setRequiresZeroInitialization(Record[Idx++]); - for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) - E->setArg(I, cast<Expr>(StmtStack[StmtStack.size() - N + I])); E->setConstructionKind((CXXConstructExpr::ConstructionKind)Record[Idx++]); - return E->getNumArgs(); } -unsigned PCHStmtReader::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) { - unsigned num = VisitExplicitCastExpr(E); +void PCHStmtReader::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) { + VisitCXXConstructExpr(E); + E->TyBeginLoc = Reader.ReadSourceLocation(Record, Idx); + E->RParenLoc = Reader.ReadSourceLocation(Record, Idx); +} + +void PCHStmtReader::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) { + VisitExplicitCastExpr(E); E->setOperatorLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); - return num; } -unsigned PCHStmtReader::VisitCXXStaticCastExpr(CXXStaticCastExpr *E) { +void PCHStmtReader::VisitCXXStaticCastExpr(CXXStaticCastExpr *E) { return VisitCXXNamedCastExpr(E); } -unsigned PCHStmtReader::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *E) { +void PCHStmtReader::VisitCXXDynamicCastExpr(CXXDynamicCastExpr *E) { return VisitCXXNamedCastExpr(E); } -unsigned PCHStmtReader::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *E) { +void PCHStmtReader::VisitCXXReinterpretCastExpr(CXXReinterpretCastExpr *E) { return VisitCXXNamedCastExpr(E); } -unsigned PCHStmtReader::VisitCXXConstCastExpr(CXXConstCastExpr *E) { +void PCHStmtReader::VisitCXXConstCastExpr(CXXConstCastExpr *E) { return VisitCXXNamedCastExpr(E); } -unsigned PCHStmtReader::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E) { - unsigned num = VisitExplicitCastExpr(E); +void PCHStmtReader::VisitCXXFunctionalCastExpr(CXXFunctionalCastExpr *E) { + VisitExplicitCastExpr(E); E->setTypeBeginLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); - return num; } -unsigned PCHStmtReader::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) { +void PCHStmtReader::VisitCXXBoolLiteralExpr(CXXBoolLiteralExpr *E) { VisitExpr(E); E->setValue(Record[Idx++]); E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++])); - return 0; } -unsigned PCHStmtReader::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) { +void PCHStmtReader::VisitCXXNullPtrLiteralExpr(CXXNullPtrLiteralExpr *E) { VisitExpr(E); E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++])); - return 0; } -unsigned PCHStmtReader::VisitCXXTypeidExpr(CXXTypeidExpr *E) { +void PCHStmtReader::VisitCXXTypeidExpr(CXXTypeidExpr *E) { VisitExpr(E); E->setSourceRange(Reader.ReadSourceRange(Record, Idx)); if (E->isTypeOperand()) { // typeid(int) E->setTypeOperandSourceInfo(Reader.GetTypeSourceInfo(Record, Idx)); - return 0; + return; } // typeid(42+2) - E->setExprOperand(cast<Expr>(StmtStack.back())); - return 1; + E->setExprOperand(Reader.ReadSubExpr()); } -unsigned PCHStmtReader::VisitCXXThisExpr(CXXThisExpr *E) { +void PCHStmtReader::VisitCXXThisExpr(CXXThisExpr *E) { VisitExpr(E); E->setLocation(SourceLocation::getFromRawEncoding(Record[Idx++])); E->setImplicit(Record[Idx++]); - return 0; } -unsigned PCHStmtReader::VisitCXXThrowExpr(CXXThrowExpr *E) { +void PCHStmtReader::VisitCXXThrowExpr(CXXThrowExpr *E) { VisitExpr(E); E->setThrowLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); - E->setSubExpr(cast<Expr>(StmtStack.back())); - return 1; + E->setSubExpr(Reader.ReadSubExpr()); } -unsigned PCHStmtReader::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { +void PCHStmtReader::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { VisitExpr(E); - E->setUsedLocation(SourceLocation::getFromRawEncoding(Record[Idx++])); - bool HasStoredExpr = Record[Idx++]; - if (!HasStoredExpr) return 0; - E->setExpr(cast<Expr>(StmtStack.back())); - return 1; + + assert(Record[Idx] == E->Param.getInt() && "We messed up at creation ?"); + ++Idx; // HasOtherExprStored and SubExpr was handled during creation. + E->Param.setPointer(cast<ParmVarDecl>(Reader.GetDecl(Record[Idx++]))); + E->Loc = Reader.ReadSourceLocation(Record, Idx); } -unsigned PCHStmtReader::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) { +void PCHStmtReader::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) { VisitExpr(E); E->setTemporary(Reader.ReadCXXTemporary(Record, Idx)); - E->setSubExpr(cast<Expr>(StmtStack.back())); - return 1; + E->setSubExpr(Reader.ReadSubExpr()); } -unsigned PCHStmtReader::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) { +void PCHStmtReader::VisitCXXBindReferenceExpr(CXXBindReferenceExpr *E) { + VisitExpr(E); + E->SubExpr = Reader.ReadSubExpr(); + E->ExtendsLifetime = Record[Idx++]; + E->RequiresTemporaryCopy = Record[Idx++]; +} + +void PCHStmtReader::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) { VisitExpr(E); E->setTypeBeginLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); E->setRParenLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); - return 0; } -unsigned PCHStmtReader::VisitCXXNewExpr(CXXNewExpr *E) { +void PCHStmtReader::VisitCXXNewExpr(CXXNewExpr *E) { VisitExpr(E); E->setGlobalNew(Record[Idx++]); - E->setParenTypeId(Record[Idx++]); E->setHasInitializer(Record[Idx++]); bool isArray = Record[Idx++]; unsigned NumPlacementArgs = Record[Idx++]; @@ -1066,6 +1051,10 @@ unsigned PCHStmtReader::VisitCXXNewExpr(CXXNewExpr *E) { cast_or_null<FunctionDecl>(Reader.GetDecl(Record[Idx++]))); E->setConstructor( cast_or_null<CXXConstructorDecl>(Reader.GetDecl(Record[Idx++]))); + SourceRange TypeIdParens; + TypeIdParens.setBegin(SourceLocation::getFromRawEncoding(Record[Idx++])); + TypeIdParens.setEnd(SourceLocation::getFromRawEncoding(Record[Idx++])); + E->TypeIdParens = TypeIdParens; E->setStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); E->setEndLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); @@ -1073,17 +1062,41 @@ unsigned PCHStmtReader::VisitCXXNewExpr(CXXNewExpr *E) { NumCtorArgs); // Install all the subexpressions. - unsigned TotalSubExprs = E->raw_arg_end()-E->raw_arg_begin(); - unsigned SSIdx = StmtStack.size()-TotalSubExprs; for (CXXNewExpr::raw_arg_iterator I = E->raw_arg_begin(),e = E->raw_arg_end(); I != e; ++I) - *I = StmtStack[SSIdx++]; - - return TotalSubExprs; + *I = Reader.ReadSubStmt(); +} + +void PCHStmtReader::VisitCXXDeleteExpr(CXXDeleteExpr *E) { + VisitExpr(E); + E->setGlobalDelete(Record[Idx++]); + E->setArrayForm(Record[Idx++]); + E->setOperatorDelete( + cast_or_null<FunctionDecl>(Reader.GetDecl(Record[Idx++]))); + E->setArgument(Reader.ReadSubExpr()); + E->setStartLoc(SourceLocation::getFromRawEncoding(Record[Idx++])); } +void PCHStmtReader::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) { + VisitExpr(E); -unsigned PCHStmtReader::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) { + E->setBase(Reader.ReadSubExpr()); + E->setArrow(Record[Idx++]); + E->setOperatorLoc(Reader.ReadSourceLocation(Record, Idx)); + E->setQualifier(Reader.ReadNestedNameSpecifier(Record, Idx)); + E->setQualifierRange(Reader.ReadSourceRange(Record, Idx)); + E->setScopeTypeInfo(Reader.GetTypeSourceInfo(Record, Idx)); + E->setColonColonLoc(Reader.ReadSourceLocation(Record, Idx)); + E->setTildeLoc(Reader.ReadSourceLocation(Record, Idx)); + + IdentifierInfo *II = Reader.GetIdentifierInfo(Record, Idx); + if (II) + E->setDestroyedType(II, Reader.ReadSourceLocation(Record, Idx)); + else + E->setDestroyedType(Reader.GetTypeSourceInfo(Record, Idx)); +} + +void PCHStmtReader::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) { VisitExpr(E); unsigned NumTemps = Record[Idx++]; if (NumTemps) { @@ -1091,23 +1104,152 @@ unsigned PCHStmtReader::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) { for (unsigned i = 0; i != NumTemps; ++i) E->setTemporary(i, Reader.ReadCXXTemporary(Record, Idx)); } - E->setSubExpr(cast<Expr>(StmtStack.back())); - return 1; + E->setSubExpr(Reader.ReadSubExpr()); +} + +void +PCHStmtReader::VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E){ + VisitExpr(E); + + unsigned NumTemplateArgs = Record[Idx++]; + assert((NumTemplateArgs != 0) == E->hasExplicitTemplateArgs() && + "Read wrong record during creation ?"); + if (E->hasExplicitTemplateArgs()) + ReadExplicitTemplateArgumentList(*E->getExplicitTemplateArgumentList(), + NumTemplateArgs); + + E->setBase(Reader.ReadSubExpr()); + E->setBaseType(Reader.GetType(Record[Idx++])); + E->setArrow(Record[Idx++]); + E->setOperatorLoc(Reader.ReadSourceLocation(Record, Idx)); + E->setQualifier(Reader.ReadNestedNameSpecifier(Record, Idx)); + E->setQualifierRange(Reader.ReadSourceRange(Record, Idx)); + E->setFirstQualifierFoundInScope( + cast_or_null<NamedDecl>(Reader.GetDecl(Record[Idx++]))); + E->setMember(Reader.ReadDeclarationName(Record, Idx)); + E->setMemberLoc(Reader.ReadSourceLocation(Record, Idx)); +} + +void +PCHStmtReader::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) { + VisitExpr(E); + + unsigned NumTemplateArgs = Record[Idx++]; + assert((NumTemplateArgs != 0) == E->hasExplicitTemplateArgs() && + "Read wrong record during creation ?"); + if (E->hasExplicitTemplateArgs()) + ReadExplicitTemplateArgumentList(E->getExplicitTemplateArgs(), + NumTemplateArgs); + + E->setDeclName(Reader.ReadDeclarationName(Record, Idx)); + E->setLocation(Reader.ReadSourceLocation(Record, Idx)); + E->setQualifierRange(Reader.ReadSourceRange(Record, Idx)); + E->setQualifier(Reader.ReadNestedNameSpecifier(Record, Idx)); +} + +void +PCHStmtReader::VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *E) { + VisitExpr(E); + assert(Record[Idx] == E->arg_size() && "Read wrong record during creation ?"); + ++Idx; // NumArgs; + for (unsigned I = 0, N = E->arg_size(); I != N; ++I) + E->setArg(I, Reader.ReadSubExpr()); + E->setTypeBeginLoc(Reader.ReadSourceLocation(Record, Idx)); + E->setTypeAsWritten(Reader.GetType(Record[Idx++])); + E->setLParenLoc(Reader.ReadSourceLocation(Record, Idx)); + E->setRParenLoc(Reader.ReadSourceLocation(Record, Idx)); +} + +void PCHStmtReader::VisitOverloadExpr(OverloadExpr *E) { + VisitExpr(E); + + unsigned NumTemplateArgs = Record[Idx++]; + assert((NumTemplateArgs != 0) == E->hasExplicitTemplateArgs() && + "Read wrong record during creation ?"); + if (E->hasExplicitTemplateArgs()) + ReadExplicitTemplateArgumentList(E->getExplicitTemplateArgs(), + NumTemplateArgs); + + unsigned NumDecls = Record[Idx++]; + UnresolvedSet<8> Decls; + for (unsigned i = 0; i != NumDecls; ++i) { + NamedDecl *D = cast<NamedDecl>(Reader.GetDecl(Record[Idx++])); + AccessSpecifier AS = (AccessSpecifier)Record[Idx++]; + Decls.addDecl(D, AS); + } + E->initializeResults(*Reader.getContext(), Decls.begin(), Decls.end()); + + E->setName(Reader.ReadDeclarationName(Record, Idx)); + E->setQualifier(Reader.ReadNestedNameSpecifier(Record, Idx)); + E->setQualifierRange(Reader.ReadSourceRange(Record, Idx)); + E->setNameLoc(Reader.ReadSourceLocation(Record, Idx)); +} + +void PCHStmtReader::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E) { + VisitOverloadExpr(E); + E->setArrow(Record[Idx++]); + E->setHasUnresolvedUsing(Record[Idx++]); + E->setBase(Reader.ReadSubExpr()); + E->setBaseType(Reader.GetType(Record[Idx++])); + E->setOperatorLoc(Reader.ReadSourceLocation(Record, Idx)); +} + +void PCHStmtReader::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) { + VisitOverloadExpr(E); + E->setRequiresADL(Record[Idx++]); + E->setOverloaded(Record[Idx++]); + E->setNamingClass(cast_or_null<CXXRecordDecl>(Reader.GetDecl(Record[Idx++]))); +} + +void PCHStmtReader::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) { + VisitExpr(E); + E->UTT = (UnaryTypeTrait)Record[Idx++]; + SourceRange Range = Reader.ReadSourceRange(Record, Idx); + E->Loc = Range.getBegin(); + E->RParen = Range.getEnd(); + E->QueriedType = Reader.GetType(Record[Idx++]); +} + +Stmt *PCHReader::ReadStmt() { + switch (ReadingKind) { + case Read_Decl: + case Read_Type: + // Read a statement from the current DeclCursor. + return ReadStmtFromStream(DeclsCursor); + case Read_Stmt: + return ReadSubStmt(); + } + + llvm_unreachable("ReadingKind not set ?"); + return 0; } +Expr *PCHReader::ReadExpr() { + return cast_or_null<Expr>(ReadStmt()); +} + +Expr *PCHReader::ReadSubExpr() { + return cast_or_null<Expr>(ReadSubStmt()); +} // Within the bitstream, expressions are stored in Reverse Polish // Notation, with each of the subexpressions preceding the -// expression they are stored in. To evaluate expressions, we -// continue reading expressions and placing them on the stack, with -// expressions having operands removing those operands from the +// expression they are stored in. Subexpressions are stored from last to first. +// To evaluate expressions, we continue reading expressions and placing them on +// the stack, with expressions having operands removing those operands from the // stack. Evaluation terminates when we see a STMT_STOP record, and // the single remaining expression on the stack is our result. -Stmt *PCHReader::ReadStmt(llvm::BitstreamCursor &Cursor) { +Stmt *PCHReader::ReadStmtFromStream(llvm::BitstreamCursor &Cursor) { + + ReadingKindTracker ReadingKind(Read_Stmt, *this); + +#ifndef NDEBUG + unsigned PrevNumStmts = StmtStack.size(); +#endif + RecordData Record; unsigned Idx; - llvm::SmallVector<Stmt *, 16> StmtStack; - PCHStmtReader Reader(*this, Record, Idx, StmtStack); + PCHStmtReader Reader(*this, Record, Idx); Stmt::EmptyShell Empty; while (true) { @@ -1221,7 +1363,9 @@ Stmt *PCHReader::ReadStmt(llvm::BitstreamCursor &Cursor) { break; case pch::EXPR_DECL_REF: - S = new (Context) DeclRefExpr(Empty); + S = DeclRefExpr::CreateEmpty(*Context, + /*HasQualifier=*/Record[PCHStmtReader::NumExprFields], + /*NumTemplateArgs=*/Record[PCHStmtReader::NumExprFields + 1]); break; case pch::EXPR_INTEGER_LITERAL: @@ -1249,6 +1393,10 @@ Stmt *PCHReader::ReadStmt(llvm::BitstreamCursor &Cursor) { S = new (Context) ParenExpr(Empty); break; + case pch::EXPR_PAREN_LIST: + S = new (Context) ParenListExpr(Empty); + break; + case pch::EXPR_UNARY_OPERATOR: S = new (Context) UnaryOperator(Empty); break; @@ -1271,9 +1419,43 @@ Stmt *PCHReader::ReadStmt(llvm::BitstreamCursor &Cursor) { S = new (Context) CallExpr(*Context, Stmt::CallExprClass, Empty); break; - case pch::EXPR_MEMBER: - S = new (Context) MemberExpr(Empty); + case pch::EXPR_MEMBER: { + // We load everything here and fully initialize it at creation. + // That way we can use MemberExpr::Create and don't have to duplicate its + // logic with a MemberExpr::CreateEmpty. + + assert(Idx == 0); + NestedNameSpecifier *NNS = 0; + SourceRange QualifierRange; + if (Record[Idx++]) { // HasQualifier. + NNS = ReadNestedNameSpecifier(Record, Idx); + QualifierRange = ReadSourceRange(Record, Idx); + } + + TemplateArgumentListInfo ArgInfo; + unsigned NumTemplateArgs = Record[Idx++]; + if (NumTemplateArgs) { + ArgInfo.setLAngleLoc(ReadSourceLocation(Record, Idx)); + ArgInfo.setRAngleLoc(ReadSourceLocation(Record, Idx)); + for (unsigned i = 0; i != NumTemplateArgs; ++i) + ArgInfo.addArgument(ReadTemplateArgumentLoc(Record, Idx)); + } + + NamedDecl *FoundD = cast_or_null<NamedDecl>(GetDecl(Record[Idx++])); + AccessSpecifier AS = (AccessSpecifier)Record[Idx++]; + DeclAccessPair FoundDecl = DeclAccessPair::make(FoundD, AS); + + QualType T = GetType(Record[Idx++]); + Expr *Base = ReadSubExpr(); + ValueDecl *MemberD = cast<ValueDecl>(GetDecl(Record[Idx++])); + SourceLocation MemberLoc = ReadSourceLocation(Record, Idx); + bool IsArrow = Record[Idx++]; + + S = MemberExpr::Create(*Context, Base, IsArrow, NNS, QualifierRange, + MemberD, FoundDecl, MemberLoc, + NumTemplateArgs ? &ArgInfo : 0, T); break; + } case pch::EXPR_BINARY_OPERATOR: S = new (Context) BinaryOperator(Empty); @@ -1414,8 +1596,11 @@ Stmt *PCHReader::ReadStmt(llvm::BitstreamCursor &Cursor) { break; case pch::EXPR_CXX_CONSTRUCT: - S = new (Context) CXXConstructExpr(Empty, *Context, - Record[PCHStmtReader::NumExprFields + 2]); + S = new (Context) CXXConstructExpr(Empty); + break; + + case pch::EXPR_CXX_TEMPORARY_OBJECT: + S = new (Context) CXXTemporaryObjectExpr(Empty); break; case pch::EXPR_CXX_STATIC_CAST: @@ -1457,43 +1642,86 @@ Stmt *PCHReader::ReadStmt(llvm::BitstreamCursor &Cursor) { case pch::EXPR_CXX_THROW: S = new (Context) CXXThrowExpr(Empty); break; - case pch::EXPR_CXX_DEFAULT_ARG: - S = new (Context) CXXDefaultArgExpr(Empty); + case pch::EXPR_CXX_DEFAULT_ARG: { + bool HasOtherExprStored = Record[PCHStmtReader::NumExprFields]; + if (HasOtherExprStored) { + Expr *SubExpr = ReadSubExpr(); + S = CXXDefaultArgExpr::Create(*Context, SourceLocation(), 0, SubExpr); + } else + S = new (Context) CXXDefaultArgExpr(Empty); break; + } case pch::EXPR_CXX_BIND_TEMPORARY: S = new (Context) CXXBindTemporaryExpr(Empty); break; + case pch::EXPR_CXX_BIND_REFERENCE: + S = new (Context) CXXBindReferenceExpr(Empty); + break; - case pch::EXPR_CXX_ZERO_INIT_VALUE: - S = new (Context) CXXZeroInitValueExpr(Empty); + case pch::EXPR_CXX_SCALAR_VALUE_INIT: + S = new (Context) CXXScalarValueInitExpr(Empty); break; case pch::EXPR_CXX_NEW: S = new (Context) CXXNewExpr(Empty); break; - + case pch::EXPR_CXX_DELETE: + S = new (Context) CXXDeleteExpr(Empty); + break; + case pch::EXPR_CXX_PSEUDO_DESTRUCTOR: + S = new (Context) CXXPseudoDestructorExpr(Empty); + break; case pch::EXPR_CXX_EXPR_WITH_TEMPORARIES: S = new (Context) CXXExprWithTemporaries(Empty); break; + + case pch::EXPR_CXX_DEPENDENT_SCOPE_MEMBER: + S = CXXDependentScopeMemberExpr::CreateEmpty(*Context, + /*NumTemplateArgs=*/Record[PCHStmtReader::NumExprFields]); + break; + + case pch::EXPR_CXX_DEPENDENT_SCOPE_DECL_REF: + S = DependentScopeDeclRefExpr::CreateEmpty(*Context, + /*NumTemplateArgs=*/Record[PCHStmtReader::NumExprFields]); + break; + + case pch::EXPR_CXX_UNRESOLVED_CONSTRUCT: + S = CXXUnresolvedConstructExpr::CreateEmpty(*Context, + /*NumArgs=*/Record[PCHStmtReader::NumExprFields]); + break; + + case pch::EXPR_CXX_UNRESOLVED_MEMBER: + S = UnresolvedMemberExpr::CreateEmpty(*Context, + /*NumTemplateArgs=*/Record[PCHStmtReader::NumExprFields]); + break; + + case pch::EXPR_CXX_UNRESOLVED_LOOKUP: + S = UnresolvedLookupExpr::CreateEmpty(*Context, + /*NumTemplateArgs=*/Record[PCHStmtReader::NumExprFields]); + break; + + case pch::EXPR_CXX_UNARY_TYPE_TRAIT: + S = new (Context) UnaryTypeTraitExpr(Empty); + break; } - + // We hit a STMT_STOP, so we're done with this expression. if (Finished) break; ++NumStatementsRead; - if (S) { - unsigned NumSubStmts = Reader.Visit(S); - while (NumSubStmts > 0) { - StmtStack.pop_back(); - --NumSubStmts; - } - } + if (S) + Reader.Visit(S); assert(Idx == Record.size() && "Invalid deserialization of statement"); StmtStack.push_back(S); } - assert(StmtStack.size() == 1 && "Extra expressions on stack!"); - return StmtStack.back(); + +#ifndef NDEBUG + assert(StmtStack.size() > PrevNumStmts && "Read too many sub stmts!"); + assert(StmtStack.size() == PrevNumStmts + 1 && "Extra expressions on stack!"); +#endif + + return StmtStack.pop_back_val(); } diff --git a/contrib/llvm/tools/clang/lib/Frontend/PCHWriter.cpp b/contrib/llvm/tools/clang/lib/Frontend/PCHWriter.cpp index 3d5b7d8..093c1e3 100644 --- a/contrib/llvm/tools/clang/lib/Frontend/PCHWriter.cpp +++ b/contrib/llvm/tools/clang/lib/Frontend/PCHWriter.cpp @@ -20,6 +20,7 @@ #include "clang/AST/Expr.h" #include "clang/AST/Type.h" #include "clang/AST/TypeLocVisitor.h" +#include "clang/Frontend/PCHReader.h" #include "clang/Lex/MacroInfo.h" #include "clang/Lex/PreprocessingRecord.h" #include "clang/Lex/Preprocessor.h" @@ -61,9 +62,7 @@ namespace { #define TYPE(Class, Base) void Visit##Class##Type(const Class##Type *T); #define ABSTRACT_TYPE(Class, Base) -#define DEPENDENT_TYPE(Class, Base) #include "clang/AST/TypeNodes.def" - void VisitInjectedClassNameType(const InjectedClassNameType *T); }; } @@ -130,8 +129,7 @@ void PCHTypeWriter::VisitVariableArrayType(const VariableArrayType *T) { void PCHTypeWriter::VisitVectorType(const VectorType *T) { Writer.AddTypeRef(T->getElementType(), Record); Record.push_back(T->getNumElements()); - Record.push_back(T->isAltiVec()); - Record.push_back(T->isPixel()); + Record.push_back(T->getAltiVecSpecific()); Code = pch::TYPE_VECTOR; } @@ -169,16 +167,15 @@ void PCHTypeWriter::VisitFunctionProtoType(const FunctionProtoType *T) { Code = pch::TYPE_FUNCTION_PROTO; } -#if 0 -// For when we want it.... void PCHTypeWriter::VisitUnresolvedUsingType(const UnresolvedUsingType *T) { Writer.AddDeclRef(T->getDecl(), Record); Code = pch::TYPE_UNRESOLVED_USING; } -#endif void PCHTypeWriter::VisitTypedefType(const TypedefType *T) { Writer.AddDeclRef(T->getDecl(), Record); + assert(!T->isCanonicalUnqualified() && "Invalid typedef ?"); + Writer.AddTypeRef(T->getCanonicalTypeInternal(), Record); Code = pch::TYPE_TYPEDEF; } @@ -198,6 +195,7 @@ void PCHTypeWriter::VisitDecltypeType(const DecltypeType *T) { } void PCHTypeWriter::VisitTagType(const TagType *T) { + Record.push_back(T->isDependentType()); Writer.AddDeclRef(T->getDecl(), Record); assert(!T->isBeingDefined() && "Cannot serialize in the middle of a type definition"); @@ -224,15 +222,70 @@ PCHTypeWriter::VisitSubstTemplateTypeParmType( void PCHTypeWriter::VisitTemplateSpecializationType( const TemplateSpecializationType *T) { + Record.push_back(T->isDependentType()); + Writer.AddTemplateName(T->getTemplateName(), Record); + Record.push_back(T->getNumArgs()); + for (TemplateSpecializationType::iterator ArgI = T->begin(), ArgE = T->end(); + ArgI != ArgE; ++ArgI) + Writer.AddTemplateArgument(*ArgI, Record); + Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType() + : T->getCanonicalTypeInternal(), + Record); + Code = pch::TYPE_TEMPLATE_SPECIALIZATION; +} + +void +PCHTypeWriter::VisitDependentSizedArrayType(const DependentSizedArrayType *T) { + VisitArrayType(T); + Writer.AddStmt(T->getSizeExpr()); + Writer.AddSourceRange(T->getBracketsRange(), Record); + Code = pch::TYPE_DEPENDENT_SIZED_ARRAY; +} + +void +PCHTypeWriter::VisitDependentSizedExtVectorType( + const DependentSizedExtVectorType *T) { // FIXME: Serialize this type (C++ only) - assert(false && "Cannot serialize template specialization types"); + assert(false && "Cannot serialize dependent sized extended vector types"); +} + +void +PCHTypeWriter::VisitTemplateTypeParmType(const TemplateTypeParmType *T) { + Record.push_back(T->getDepth()); + Record.push_back(T->getIndex()); + Record.push_back(T->isParameterPack()); + Writer.AddIdentifierRef(T->getName(), Record); + Code = pch::TYPE_TEMPLATE_TYPE_PARM; +} + +void +PCHTypeWriter::VisitDependentNameType(const DependentNameType *T) { + Record.push_back(T->getKeyword()); + Writer.AddNestedNameSpecifier(T->getQualifier(), Record); + Writer.AddIdentifierRef(T->getIdentifier(), Record); + Writer.AddTypeRef(T->isCanonicalUnqualified() ? QualType() + : T->getCanonicalTypeInternal(), + Record); + Code = pch::TYPE_DEPENDENT_NAME; +} + +void +PCHTypeWriter::VisitDependentTemplateSpecializationType( + const DependentTemplateSpecializationType *T) { + Record.push_back(T->getKeyword()); + Writer.AddNestedNameSpecifier(T->getQualifier(), Record); + Writer.AddIdentifierRef(T->getIdentifier(), Record); + Record.push_back(T->getNumArgs()); + for (DependentTemplateSpecializationType::iterator + I = T->begin(), E = T->end(); I != E; ++I) + Writer.AddTemplateArgument(*I, Record); + Code = pch::TYPE_DEPENDENT_TEMPLATE_SPECIALIZATION; } void PCHTypeWriter::VisitElaboratedType(const ElaboratedType *T) { - Writer.AddTypeRef(T->getNamedType(), Record); Record.push_back(T->getKeyword()); - // FIXME: Serialize the qualifier (C++ only) - assert(T->getQualifier() == 0 && "Cannot serialize qualified name types"); + Writer.AddNestedNameSpecifier(T->getQualifier(), Record); + Writer.AddTypeRef(T->getNamedType(), Record); Code = pch::TYPE_ELABORATED; } @@ -394,7 +447,8 @@ void TypeLocWriter::VisitTemplateSpecializationTypeLoc( Writer.AddSourceLocation(TL.getLAngleLoc(), Record); Writer.AddSourceLocation(TL.getRAngleLoc(), Record); for (unsigned i = 0, e = TL.getNumArgs(); i != e; ++i) - Writer.AddTemplateArgumentLoc(TL.getArgLoc(i), Record); + Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(i).getArgument().getKind(), + TL.getArgLoc(i).getLocInfo(), Record); } void TypeLocWriter::VisitElaboratedTypeLoc(ElaboratedTypeLoc TL) { Writer.AddSourceLocation(TL.getKeywordLoc(), Record); @@ -408,6 +462,17 @@ void TypeLocWriter::VisitDependentNameTypeLoc(DependentNameTypeLoc TL) { Writer.AddSourceRange(TL.getQualifierRange(), Record); Writer.AddSourceLocation(TL.getNameLoc(), Record); } +void TypeLocWriter::VisitDependentTemplateSpecializationTypeLoc( + DependentTemplateSpecializationTypeLoc TL) { + Writer.AddSourceLocation(TL.getKeywordLoc(), Record); + Writer.AddSourceRange(TL.getQualifierRange(), Record); + Writer.AddSourceLocation(TL.getNameLoc(), Record); + Writer.AddSourceLocation(TL.getLAngleLoc(), Record); + Writer.AddSourceLocation(TL.getRAngleLoc(), Record); + for (unsigned I = 0, E = TL.getNumArgs(); I != E; ++I) + Writer.AddTemplateArgumentLocInfo(TL.getArgLoc(I).getArgument().getKind(), + TL.getArgLoc(I).getLocInfo(), Record); +} void TypeLocWriter::VisitObjCInterfaceTypeLoc(ObjCInterfaceTypeLoc TL) { Writer.AddSourceLocation(TL.getNameLoc(), Record); } @@ -564,6 +629,7 @@ void PCHWriter::WriteBlockInfoBlock() { RECORD(VERSION_CONTROL_BRANCH_REVISION); RECORD(UNUSED_STATIC_FUNCS); RECORD(MACRO_DEFINITION_OFFSETS); + RECORD(CHAINED_METADATA); // SourceManager Block. BLOCK(SOURCE_MANAGER_BLOCK); @@ -683,24 +749,27 @@ void PCHWriter::WriteMetadata(ASTContext &Context, const char *isysroot) { // Metadata const TargetInfo &Target = Context.Target; BitCodeAbbrev *MetaAbbrev = new BitCodeAbbrev(); - MetaAbbrev->Add(BitCodeAbbrevOp(pch::METADATA)); + MetaAbbrev->Add(BitCodeAbbrevOp( + Chain ? pch::CHAINED_METADATA : pch::METADATA)); MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH major MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // PCH minor MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang major MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 16)); // Clang minor MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Fixed, 1)); // Relocatable - MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); // Target triple + // Target triple or chained PCH name + MetaAbbrev->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::Blob)); unsigned MetaAbbrevCode = Stream.EmitAbbrev(MetaAbbrev); RecordData Record; - Record.push_back(pch::METADATA); + Record.push_back(Chain ? pch::CHAINED_METADATA : pch::METADATA); Record.push_back(pch::VERSION_MAJOR); Record.push_back(pch::VERSION_MINOR); Record.push_back(CLANG_VERSION_MAJOR); Record.push_back(CLANG_VERSION_MINOR); Record.push_back(isysroot != 0); - const std::string &TripleStr = Target.getTriple().getTriple(); - Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, TripleStr); + // FIXME: This writes the absolute path for chained headers. + const std::string &BlobStr = Chain ? Chain->getFileName() : Target.getTriple().getTriple(); + Stream.EmitRecordWithBlob(MetaAbbrevCode, Record, BlobStr); // Original file name SourceManager &SM = Context.getSourceManager(); @@ -779,11 +848,8 @@ void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) { Record.push_back(LangOpts.MathErrno); // Math functions must respect errno // (modulo the platform support). - Record.push_back(LangOpts.OverflowChecking); // Extension to call a handler function when - // signed integer arithmetic overflows. - - Record.push_back(LangOpts.HeinousExtensions); // Extensions that we really don't like and - // may be ripped out at any time. + Record.push_back(LangOpts.getSignedOverflowBehavior()); + Record.push_back(LangOpts.HeinousExtensions); Record.push_back(LangOpts.Optimize); // Whether __OPTIMIZE__ should be defined. Record.push_back(LangOpts.OptimizeSize); // Whether __OPTIMIZE_SIZE__ should be @@ -807,6 +873,7 @@ void PCHWriter::WriteLanguageOptions(const LangOptions &LangOpts) { Record.push_back(LangOpts.OpenCL); Record.push_back(LangOpts.CatchUndefined); Record.push_back(LangOpts.ElideConstructors); + Record.push_back(LangOpts.SpellChecking); Stream.EmitRecord(pch::LANGUAGE_OPTIONS, Record); } @@ -866,8 +933,7 @@ public: } // end anonymous namespace /// \brief Write the stat() system call cache to the PCH file. -void PCHWriter::WriteStatCache(MemorizeStatCalls &StatCalls, - const char *isysroot) { +void PCHWriter::WriteStatCache(MemorizeStatCalls &StatCalls) { // Build the on-disk hash table containing information about every // stat() call. OnDiskChainedHashTableGenerator<PCHStatCacheTrait> Generator; @@ -876,7 +942,6 @@ void PCHWriter::WriteStatCache(MemorizeStatCalls &StatCalls, StatEnd = StatCalls.end(); Stat != StatEnd; ++Stat, ++NumStatEntries) { const char *Filename = Stat->first(); - Filename = adjustFilenameForRelocatablePCH(Filename, isysroot); Generator.insert(Filename, Stat->second); } @@ -1347,16 +1412,7 @@ void PCHWriter::WriteType(QualType T) { #define TYPE(Class, Base) \ case Type::Class: W.Visit##Class##Type(cast<Class##Type>(T)); break; #define ABSTRACT_TYPE(Class, Base) -#define DEPENDENT_TYPE(Class, Base) #include "clang/AST/TypeNodes.def" - - // For all of the dependent type nodes (which only occur in C++ - // templates), produce an error. -#define TYPE(Class, Base) -#define DEPENDENT_TYPE(Class, Base) case Type::Class: -#include "clang/AST/TypeNodes.def" - assert(false && "Cannot serialize dependent type nodes"); - break; } } @@ -1402,11 +1458,16 @@ uint64_t PCHWriter::WriteDeclContextVisibleBlock(ASTContext &Context, if (DC->getPrimaryContext() != DC) return 0; - // Since there is no name lookup into functions or methods, and we - // perform name lookup for the translation unit via the - // IdentifierInfo chains, don't bother to build a - // visible-declarations table for these entities. - if (DC->isFunctionOrMethod() || DC->isTranslationUnit()) + // Since there is no name lookup into functions or methods, don't bother to + // build a visible-declarations table for these entities. + if (DC->isFunctionOrMethod()) + return 0; + + // If not in C++, we perform name lookup for the translation unit via the + // IdentifierInfo chains, don't bother to build a visible-declarations table. + // FIXME: In C++ we need the visible declarations in order to "see" the + // friend declarations, is there a way to do this without writing the table ? + if (DC->isTranslationUnit() && !Context.getLangOptions().CPlusPlus) return 0; // Force the DeclContext to build a its name-lookup table. @@ -1832,66 +1893,66 @@ void PCHWriter::WriteAttributeRecord(const Attr *Attr) { default: assert(0 && "Does not support PCH writing for this attribute yet!"); break; - case Attr::Alias: + case attr::Alias: AddString(cast<AliasAttr>(Attr)->getAliasee(), Record); break; - case Attr::AlignMac68k: + case attr::AlignMac68k: break; - case Attr::Aligned: + case attr::Aligned: Record.push_back(cast<AlignedAttr>(Attr)->getAlignment()); break; - case Attr::AlwaysInline: + case attr::AlwaysInline: break; - case Attr::AnalyzerNoReturn: + case attr::AnalyzerNoReturn: break; - case Attr::Annotate: + case attr::Annotate: AddString(cast<AnnotateAttr>(Attr)->getAnnotation(), Record); break; - case Attr::AsmLabel: + case attr::AsmLabel: AddString(cast<AsmLabelAttr>(Attr)->getLabel(), Record); break; - case Attr::BaseCheck: + case attr::BaseCheck: break; - case Attr::Blocks: + case attr::Blocks: Record.push_back(cast<BlocksAttr>(Attr)->getType()); // FIXME: stable break; - case Attr::CDecl: + case attr::CDecl: break; - case Attr::Cleanup: + case attr::Cleanup: AddDeclRef(cast<CleanupAttr>(Attr)->getFunctionDecl(), Record); break; - case Attr::Const: + case attr::Const: break; - case Attr::Constructor: + case attr::Constructor: Record.push_back(cast<ConstructorAttr>(Attr)->getPriority()); break; - case Attr::DLLExport: - case Attr::DLLImport: - case Attr::Deprecated: + case attr::DLLExport: + case attr::DLLImport: + case attr::Deprecated: break; - case Attr::Destructor: + case attr::Destructor: Record.push_back(cast<DestructorAttr>(Attr)->getPriority()); break; - case Attr::FastCall: - case Attr::Final: + case attr::FastCall: + case attr::Final: break; - case Attr::Format: { + case attr::Format: { const FormatAttr *Format = cast<FormatAttr>(Attr); AddString(Format->getType(), Record); Record.push_back(Format->getFormatIdx()); @@ -1899,93 +1960,93 @@ void PCHWriter::WriteAttributeRecord(const Attr *Attr) { break; } - case Attr::FormatArg: { + case attr::FormatArg: { const FormatArgAttr *Format = cast<FormatArgAttr>(Attr); Record.push_back(Format->getFormatIdx()); break; } - case Attr::Sentinel : { + case attr::Sentinel : { const SentinelAttr *Sentinel = cast<SentinelAttr>(Attr); Record.push_back(Sentinel->getSentinel()); Record.push_back(Sentinel->getNullPos()); break; } - case Attr::GNUInline: - case Attr::Hiding: - case Attr::IBActionKind: - case Attr::IBOutletKind: - case Attr::Malloc: - case Attr::NoDebug: - case Attr::NoInline: - case Attr::NoReturn: - case Attr::NoThrow: + case attr::GNUInline: + case attr::Hiding: + case attr::IBAction: + case attr::IBOutlet: + case attr::Malloc: + case attr::NoDebug: + case attr::NoInline: + case attr::NoReturn: + case attr::NoThrow: break; - case Attr::IBOutletCollectionKind: { + case attr::IBOutletCollection: { const IBOutletCollectionAttr *ICA = cast<IBOutletCollectionAttr>(Attr); AddDeclRef(ICA->getClass(), Record); break; } - case Attr::NonNull: { + case attr::NonNull: { const NonNullAttr *NonNull = cast<NonNullAttr>(Attr); Record.push_back(NonNull->size()); Record.insert(Record.end(), NonNull->begin(), NonNull->end()); break; } - case Attr::CFReturnsNotRetained: - case Attr::CFReturnsRetained: - case Attr::NSReturnsNotRetained: - case Attr::NSReturnsRetained: - case Attr::ObjCException: - case Attr::ObjCNSObject: - case Attr::Overloadable: - case Attr::Override: + case attr::CFReturnsNotRetained: + case attr::CFReturnsRetained: + case attr::NSReturnsNotRetained: + case attr::NSReturnsRetained: + case attr::ObjCException: + case attr::ObjCNSObject: + case attr::Overloadable: + case attr::Override: break; - case Attr::MaxFieldAlignment: + case attr::MaxFieldAlignment: Record.push_back(cast<MaxFieldAlignmentAttr>(Attr)->getAlignment()); break; - case Attr::Packed: + case attr::Packed: break; - case Attr::Pure: + case attr::Pure: break; - case Attr::Regparm: + case attr::Regparm: Record.push_back(cast<RegparmAttr>(Attr)->getNumParams()); break; - case Attr::ReqdWorkGroupSize: + case attr::ReqdWorkGroupSize: Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getXDim()); Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getYDim()); Record.push_back(cast<ReqdWorkGroupSizeAttr>(Attr)->getZDim()); break; - case Attr::Section: + case attr::Section: AddString(cast<SectionAttr>(Attr)->getName(), Record); break; - case Attr::StdCall: - case Attr::TransparentUnion: - case Attr::Unavailable: - case Attr::Unused: - case Attr::Used: + case attr::StdCall: + case attr::TransparentUnion: + case attr::Unavailable: + case attr::Unused: + case attr::Used: break; - case Attr::Visibility: + case attr::Visibility: // FIXME: stable encoding Record.push_back(cast<VisibilityAttr>(Attr)->getVisibility()); break; - case Attr::WarnUnusedResult: - case Attr::Weak: - case Attr::WeakRef: - case Attr::WeakImport: + case attr::WarnUnusedResult: + case attr::Weak: + case attr::WeakRef: + case attr::WeakImport: break; } } @@ -2012,18 +2073,16 @@ void PCHWriter::SetSelectorOffset(Selector Sel, uint32_t Offset) { SelectorOffsets[ID - 1] = Offset; } -PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream) - : Stream(Stream), NextTypeID(pch::NUM_PREDEF_TYPE_IDS), - NumStatements(0), NumMacros(0), NumLexicalDeclContexts(0), - NumVisibleDeclContexts(0) { } +PCHWriter::PCHWriter(llvm::BitstreamWriter &Stream, PCHReader *Chain) + : Stream(Stream), Chain(Chain), NextTypeID(pch::NUM_PREDEF_TYPE_IDS), + CollectedStmts(&StmtsToEmit), NumStatements(0), NumMacros(0), + NumLexicalDeclContexts(0), NumVisibleDeclContexts(0) { + if (Chain) + Chain->setDeserializationListener(this); +} void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls, const char *isysroot) { - using namespace llvm; - - ASTContext &Context = SemaRef.Context; - Preprocessor &PP = SemaRef.PP; - // Emit the file header. Stream.Emit((unsigned)'C', 8); Stream.Emit((unsigned)'P', 8); @@ -2032,6 +2091,19 @@ void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls, WriteBlockInfoBlock(); + if (Chain) + WritePCHChain(SemaRef, StatCalls, isysroot); + else + WritePCHCore(SemaRef, StatCalls, isysroot); +} + +void PCHWriter::WritePCHCore(Sema &SemaRef, MemorizeStatCalls *StatCalls, + const char *isysroot) { + using namespace llvm; + + ASTContext &Context = SemaRef.Context; + Preprocessor &PP = SemaRef.PP; + // The translation unit is the first declaration we'll emit. DeclIDs[Context.getTranslationUnitDecl()] = 1; DeclTypesToEmit.push(Context.getTranslationUnitDecl()); @@ -2077,13 +2149,27 @@ void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls, for (unsigned I = 0, N = SemaRef.ExtVectorDecls.size(); I != N; ++I) AddDeclRef(SemaRef.ExtVectorDecls[I], ExtVectorDecls); + // Build a record containing all of the VTable uses information. + RecordData VTableUses; + VTableUses.push_back(SemaRef.VTableUses.size()); + for (unsigned I = 0, N = SemaRef.VTableUses.size(); I != N; ++I) { + AddDeclRef(SemaRef.VTableUses[I].first, VTableUses); + AddSourceLocation(SemaRef.VTableUses[I].second, VTableUses); + VTableUses.push_back(SemaRef.VTablesUsed[SemaRef.VTableUses[I].first]); + } + + // Build a record containing all of dynamic classes declarations. + RecordData DynamicClasses; + for (unsigned I = 0, N = SemaRef.DynamicClasses.size(); I != N; ++I) + AddDeclRef(SemaRef.DynamicClasses[I], DynamicClasses); + // Write the remaining PCH contents. RecordData Record; Stream.EnterSubblock(pch::PCH_BLOCK_ID, 5); WriteMetadata(Context, isysroot); WriteLanguageOptions(Context.getLangOptions()); if (StatCalls && !isysroot) - WriteStatCache(*StatCalls, isysroot); + WriteStatCache(*StatCalls); WriteSourceManagerBlock(Context.getSourceManager(), PP, isysroot); // Write the record of special types. Record.clear(); @@ -2104,6 +2190,7 @@ void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls, AddTypeRef(Context.getRawBlockdescriptorExtendedType(), Record); AddTypeRef(Context.ObjCSelRedefinitionType, Record); AddTypeRef(Context.getRawNSConstantStringType(), Record); + Record.push_back(Context.isInt128Installed()); Stream.EmitRecord(pch::SPECIAL_TYPES, Record); // Keep writing types and declarations until all types and @@ -2171,6 +2258,14 @@ void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls, if (!ExtVectorDecls.empty()) Stream.EmitRecord(pch::EXT_VECTOR_DECLS, ExtVectorDecls); + // Write the record containing VTable uses information. + if (!VTableUses.empty()) + Stream.EmitRecord(pch::VTABLE_USES, VTableUses); + + // Write the record containing dynamic classes declarations. + if (!DynamicClasses.empty()) + Stream.EmitRecord(pch::DYNAMIC_CLASSES, DynamicClasses); + // Some simple statistics Record.clear(); Record.push_back(NumStatements); @@ -2181,6 +2276,64 @@ void PCHWriter::WritePCH(Sema &SemaRef, MemorizeStatCalls *StatCalls, Stream.ExitBlock(); } +void PCHWriter::WritePCHChain(Sema &SemaRef, MemorizeStatCalls *StatCalls, + const char *isysroot) { + using namespace llvm; + + ASTContext &Context = SemaRef.Context; + Preprocessor &PP = SemaRef.PP; + (void)PP; + + RecordData Record; + Stream.EnterSubblock(pch::PCH_BLOCK_ID, 5); + WriteMetadata(Context, isysroot); + // FIXME: StatCache + // FIXME: Source manager block + + // The special types are in the chained PCH. + + // We don't start with the translation unit, but with its decls that + // don't come from the other PCH. + const TranslationUnitDecl *TU = Context.getTranslationUnitDecl(); + // FIXME: We don't want to iterate over everything here, because it needlessly + // deserializes the entire original PCH. Instead we only want to iterate over + // the stuff that's already there. + // All in good time, though. + for (DeclContext::decl_iterator I = TU->decls_begin(), E = TU->decls_end(); + I != E; ++I) { + if ((*I)->getPCHLevel() == 0) { + (*I)->dump(); + DeclTypesToEmit.push(*I); + } + } + + Stream.EnterSubblock(pch::DECLTYPES_BLOCK_ID, 3); + WriteDeclsBlockAbbrevs(); + while (!DeclTypesToEmit.empty()) { + DeclOrType DOT = DeclTypesToEmit.front(); + DeclTypesToEmit.pop(); + if (DOT.isType()) + WriteType(DOT.getType()); + else + WriteDecl(Context, DOT.getDecl()); + } + Stream.ExitBlock(); + + // FIXME: Preprocessor + // FIXME: Method pool + // FIXME: Identifier table + // FIXME: Type offsets + // FIXME: Declaration offsets + // FIXME: External unnamed definitions + // FIXME: Tentative definitions + // FIXME: Unused static functions + // FIXME: Locally-scoped external definitions + // FIXME: ext_vector type names + // FIXME: Dynamic classes declarations + // FIXME: Statistics + Stream.ExitBlock(); +} + void PCHWriter::AddSourceLocation(SourceLocation Loc, RecordData &Record) { Record.push_back(Loc.getRawEncoding()); } @@ -2249,20 +2402,19 @@ void PCHWriter::AddCXXTemporary(const CXXTemporary *Temp, RecordData &Record) { AddDeclRef(Temp->getDestructor(), Record); } -void PCHWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg, - RecordData &Record) { - switch (Arg.getArgument().getKind()) { +void PCHWriter::AddTemplateArgumentLocInfo(TemplateArgument::ArgKind Kind, + const TemplateArgumentLocInfo &Arg, + RecordData &Record) { + switch (Kind) { case TemplateArgument::Expression: - AddStmt(Arg.getLocInfo().getAsExpr()); + AddStmt(Arg.getAsExpr()); break; case TemplateArgument::Type: - AddTypeSourceInfo(Arg.getLocInfo().getAsTypeSourceInfo(), Record); + AddTypeSourceInfo(Arg.getAsTypeSourceInfo(), Record); break; case TemplateArgument::Template: - Record.push_back( - Arg.getTemplateQualifierRange().getBegin().getRawEncoding()); - Record.push_back(Arg.getTemplateQualifierRange().getEnd().getRawEncoding()); - Record.push_back(Arg.getTemplateNameLoc().getRawEncoding()); + AddSourceRange(Arg.getTemplateQualifierRange(), Record); + AddSourceLocation(Arg.getTemplateNameLoc(), Record); break; case TemplateArgument::Null: case TemplateArgument::Integral: @@ -2272,6 +2424,21 @@ void PCHWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg, } } +void PCHWriter::AddTemplateArgumentLoc(const TemplateArgumentLoc &Arg, + RecordData &Record) { + AddTemplateArgument(Arg.getArgument(), Record); + + if (Arg.getArgument().getKind() == TemplateArgument::Expression) { + bool InfoHasSameExpr + = Arg.getArgument().getAsExpr() == Arg.getLocInfo().getAsExpr(); + Record.push_back(InfoHasSameExpr); + if (InfoHasSameExpr) + return; // Avoid storing the same expr twice. + } + AddTemplateArgumentLocInfo(Arg.getArgument().getKind(), Arg.getLocInfo(), + Record); +} + void PCHWriter::AddTypeSourceInfo(TypeSourceInfo *TInfo, RecordData &Record) { if (TInfo == 0) { AddTypeRef(QualType(), Record); @@ -2459,3 +2626,123 @@ void PCHWriter::AddNestedNameSpecifier(NestedNameSpecifier *NNS, } } } + +void PCHWriter::AddTemplateName(TemplateName Name, RecordData &Record) { + TemplateName::NameKind Kind = Name.getKind(); + Record.push_back(Kind); + switch (Kind) { + case TemplateName::Template: + AddDeclRef(Name.getAsTemplateDecl(), Record); + break; + + case TemplateName::OverloadedTemplate: { + OverloadedTemplateStorage *OvT = Name.getAsOverloadedTemplate(); + Record.push_back(OvT->size()); + for (OverloadedTemplateStorage::iterator I = OvT->begin(), E = OvT->end(); + I != E; ++I) + AddDeclRef(*I, Record); + break; + } + + case TemplateName::QualifiedTemplate: { + QualifiedTemplateName *QualT = Name.getAsQualifiedTemplateName(); + AddNestedNameSpecifier(QualT->getQualifier(), Record); + Record.push_back(QualT->hasTemplateKeyword()); + AddDeclRef(QualT->getTemplateDecl(), Record); + break; + } + + case TemplateName::DependentTemplate: { + DependentTemplateName *DepT = Name.getAsDependentTemplateName(); + AddNestedNameSpecifier(DepT->getQualifier(), Record); + Record.push_back(DepT->isIdentifier()); + if (DepT->isIdentifier()) + AddIdentifierRef(DepT->getIdentifier(), Record); + else + Record.push_back(DepT->getOperator()); + break; + } + } +} + +void PCHWriter::AddTemplateArgument(const TemplateArgument &Arg, + RecordData &Record) { + Record.push_back(Arg.getKind()); + switch (Arg.getKind()) { + case TemplateArgument::Null: + break; + case TemplateArgument::Type: + AddTypeRef(Arg.getAsType(), Record); + break; + case TemplateArgument::Declaration: + AddDeclRef(Arg.getAsDecl(), Record); + break; + case TemplateArgument::Integral: + AddAPSInt(*Arg.getAsIntegral(), Record); + AddTypeRef(Arg.getIntegralType(), Record); + break; + case TemplateArgument::Template: + AddTemplateName(Arg.getAsTemplate(), Record); + break; + case TemplateArgument::Expression: + AddStmt(Arg.getAsExpr()); + break; + case TemplateArgument::Pack: + Record.push_back(Arg.pack_size()); + for (TemplateArgument::pack_iterator I=Arg.pack_begin(), E=Arg.pack_end(); + I != E; ++I) + AddTemplateArgument(*I, Record); + break; + } +} + +void +PCHWriter::AddTemplateParameterList(const TemplateParameterList *TemplateParams, + RecordData &Record) { + assert(TemplateParams && "No TemplateParams!"); + AddSourceLocation(TemplateParams->getTemplateLoc(), Record); + AddSourceLocation(TemplateParams->getLAngleLoc(), Record); + AddSourceLocation(TemplateParams->getRAngleLoc(), Record); + Record.push_back(TemplateParams->size()); + for (TemplateParameterList::const_iterator + P = TemplateParams->begin(), PEnd = TemplateParams->end(); + P != PEnd; ++P) + AddDeclRef(*P, Record); +} + +/// \brief Emit a template argument list. +void +PCHWriter::AddTemplateArgumentList(const TemplateArgumentList *TemplateArgs, + RecordData &Record) { + assert(TemplateArgs && "No TemplateArgs!"); + Record.push_back(TemplateArgs->flat_size()); + for (int i=0, e = TemplateArgs->flat_size(); i != e; ++i) + AddTemplateArgument(TemplateArgs->get(i), Record); +} + + +void +PCHWriter::AddUnresolvedSet(const UnresolvedSetImpl &Set, RecordData &Record) { + Record.push_back(Set.size()); + for (UnresolvedSetImpl::const_iterator + I = Set.begin(), E = Set.end(); I != E; ++I) { + AddDeclRef(I.getDecl(), Record); + Record.push_back(I.getAccess()); + } +} + +void PCHWriter::AddCXXBaseSpecifier(const CXXBaseSpecifier &Base, + RecordData &Record) { + Record.push_back(Base.isVirtual()); + Record.push_back(Base.isBaseOfClass()); + Record.push_back(Base.getAccessSpecifierAsWritten()); + AddTypeRef(Base.getType(), Record); + AddSourceRange(Base.getSourceRange(), Record); +} + +void PCHWriter::TypeRead(pch::TypeID ID, QualType T) { +} + +void PCHWriter::DeclRead(pch::DeclID ID, const Decl *D) { +} + diff --git a/contrib/llvm/tools/clang/lib/Frontend/PCHWriterDecl.cpp b/contrib/llvm/tools/clang/lib/Frontend/PCHWriterDecl.cpp index cc58e8e..bc4452e 100644 --- a/contrib/llvm/tools/clang/lib/Frontend/PCHWriterDecl.cpp +++ b/contrib/llvm/tools/clang/lib/Frontend/PCHWriterDecl.cpp @@ -25,7 +25,7 @@ using namespace clang; // Declaration serialization //===----------------------------------------------------------------------===// -namespace { +namespace clang { class PCHDeclWriter : public DeclVisitor<PCHDeclWriter, void> { PCHWriter &Writer; @@ -40,6 +40,8 @@ namespace { PCHWriter::RecordData &Record) : Writer(Writer), Context(Context), Record(Record) { } + + void Visit(Decl *D); void VisitDecl(Decl *D); void VisitTranslationUnitDecl(TranslationUnitDecl *D); @@ -49,7 +51,7 @@ namespace { void VisitNamespaceAliasDecl(NamespaceAliasDecl *D); void VisitTypeDecl(TypeDecl *D); void VisitTypedefDecl(TypedefDecl *D); - void VisitUnresolvedUsingTypename(UnresolvedUsingTypenameDecl *D); + void VisitUnresolvedUsingTypenameDecl(UnresolvedUsingTypenameDecl *D); void VisitTagDecl(TagDecl *D); void VisitEnumDecl(EnumDecl *D); void VisitRecordDecl(RecordDecl *D); @@ -61,7 +63,7 @@ namespace { void VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D); void VisitValueDecl(ValueDecl *D); void VisitEnumConstantDecl(EnumConstantDecl *D); - void VisitUnresolvedUsingValue(UnresolvedUsingValueDecl *D); + void VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D); void VisitDeclaratorDecl(DeclaratorDecl *D); void VisitFunctionDecl(FunctionDecl *D); void VisitCXXMethodDecl(CXXMethodDecl *D); @@ -75,12 +77,14 @@ namespace { void VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D); void VisitTemplateDecl(TemplateDecl *D); void VisitClassTemplateDecl(ClassTemplateDecl *D); - void visitFunctionTemplateDecl(FunctionTemplateDecl *D); + void VisitFunctionTemplateDecl(FunctionTemplateDecl *D); void VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D); - void VisitUsing(UsingDecl *D); - void VisitUsingShadow(UsingShadowDecl *D); + void VisitUsingDecl(UsingDecl *D); + void VisitUsingShadowDecl(UsingShadowDecl *D); void VisitLinkageSpecDecl(LinkageSpecDecl *D); void VisitFileScopeAsmDecl(FileScopeAsmDecl *D); + void VisitAccessSpecDecl(AccessSpecDecl *D); + void VisitFriendDecl(FriendDecl *D); void VisitFriendTemplateDecl(FriendTemplateDecl *D); void VisitStaticAssertDecl(StaticAssertDecl *D); void VisitBlockDecl(BlockDecl *D); @@ -89,7 +93,7 @@ namespace { uint64_t VisibleOffset); - // FIXME: Put in the same order is DeclNodes.def? + // FIXME: Put in the same order is DeclNodes.td? void VisitObjCMethodDecl(ObjCMethodDecl *D); void VisitObjCContainerDecl(ObjCContainerDecl *D); void VisitObjCInterfaceDecl(ObjCInterfaceDecl *D); @@ -108,6 +112,19 @@ namespace { }; } +void PCHDeclWriter::Visit(Decl *D) { + DeclVisitor<PCHDeclWriter>::Visit(D); + + // Handle FunctionDecl's body here and write it after all other Stmts/Exprs + // have been written. We want it last because we will not read it back when + // retrieving it from the PCH, we'll just lazily set the offset. + if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { + Record.push_back(FD->isThisDeclarationADefinition()); + if (FD->isThisDeclarationADefinition()) + Writer.AddStmt(FD->getBody()); + } +} + void PCHDeclWriter::VisitDecl(Decl *D) { Writer.AddDeclRef(cast_or_null<Decl>(D->getDeclContext()), Record); Writer.AddDeclRef(cast_or_null<Decl>(D->getLexicalDeclContext()), Record); @@ -115,7 +132,7 @@ void PCHDeclWriter::VisitDecl(Decl *D) { Record.push_back(D->isInvalidDecl()); Record.push_back(D->hasAttrs()); Record.push_back(D->isImplicit()); - Record.push_back(D->isUsed()); + Record.push_back(D->isUsed(false)); Record.push_back(D->getAccess()); Record.push_back(D->getPCHLevel()); } @@ -144,6 +161,7 @@ void PCHDeclWriter::VisitTypedefDecl(TypedefDecl *D) { void PCHDeclWriter::VisitTagDecl(TagDecl *D) { VisitTypeDecl(D); + Record.push_back(D->getIdentifierNamespace()); Writer.AddDeclRef(D->getPreviousDeclaration(), Record); Record.push_back((unsigned)D->getTagKind()); // FIXME: stable encoding Record.push_back(D->isDefinition()); @@ -160,7 +178,7 @@ void PCHDeclWriter::VisitEnumDecl(EnumDecl *D) { Writer.AddTypeRef(D->getPromotionType(), Record); Record.push_back(D->getNumPositiveBits()); Record.push_back(D->getNumNegativeBits()); - // FIXME: C++ InstantiatedFrom + Writer.AddDeclRef(D->getInstantiatedFromMemberEnum(), Record); Code = pch::DECL_ENUM; } @@ -195,9 +213,70 @@ void PCHDeclWriter::VisitDeclaratorDecl(DeclaratorDecl *D) { void PCHDeclWriter::VisitFunctionDecl(FunctionDecl *D) { VisitDeclaratorDecl(D); - Record.push_back(D->isThisDeclarationADefinition()); - if (D->isThisDeclarationADefinition()) - Writer.AddStmt(D->getBody()); + Record.push_back(D->getIdentifierNamespace()); + Record.push_back(D->getTemplatedKind()); + switch (D->getTemplatedKind()) { + default: assert(false && "Unhandled TemplatedKind!"); + break; + case FunctionDecl::TK_NonTemplate: + break; + case FunctionDecl::TK_FunctionTemplate: + Writer.AddDeclRef(D->getDescribedFunctionTemplate(), Record); + break; + case FunctionDecl::TK_MemberSpecialization: { + MemberSpecializationInfo *MemberInfo = D->getMemberSpecializationInfo(); + Writer.AddDeclRef(MemberInfo->getInstantiatedFrom(), Record); + Record.push_back(MemberInfo->getTemplateSpecializationKind()); + Writer.AddSourceLocation(MemberInfo->getPointOfInstantiation(), Record); + break; + } + case FunctionDecl::TK_FunctionTemplateSpecialization: { + FunctionTemplateSpecializationInfo * + FTSInfo = D->getTemplateSpecializationInfo(); + // We want it canonical to guarantee that it has a Common*. + Writer.AddDeclRef(FTSInfo->getTemplate()->getCanonicalDecl(), Record); + Record.push_back(FTSInfo->getTemplateSpecializationKind()); + + // Template arguments. + Writer.AddTemplateArgumentList(FTSInfo->TemplateArguments, Record); + + // Template args as written. + Record.push_back(FTSInfo->TemplateArgumentsAsWritten != 0); + if (FTSInfo->TemplateArgumentsAsWritten) { + Record.push_back(FTSInfo->TemplateArgumentsAsWritten->size()); + for (int i=0, e = FTSInfo->TemplateArgumentsAsWritten->size(); i!=e; ++i) + Writer.AddTemplateArgumentLoc((*FTSInfo->TemplateArgumentsAsWritten)[i], + Record); + Writer.AddSourceLocation(FTSInfo->TemplateArgumentsAsWritten->getLAngleLoc(), + Record); + Writer.AddSourceLocation(FTSInfo->TemplateArgumentsAsWritten->getRAngleLoc(), + Record); + } + + Writer.AddSourceLocation(FTSInfo->getPointOfInstantiation(), Record); + break; + } + case FunctionDecl::TK_DependentFunctionTemplateSpecialization: { + DependentFunctionTemplateSpecializationInfo * + DFTSInfo = D->getDependentSpecializationInfo(); + + // Templates. + Record.push_back(DFTSInfo->getNumTemplates()); + for (int i=0, e = DFTSInfo->getNumTemplates(); i != e; ++i) + Writer.AddDeclRef(DFTSInfo->getTemplate(i), Record); + + // Templates args. + Record.push_back(DFTSInfo->getNumTemplateArgs()); + for (int i=0, e = DFTSInfo->getNumTemplateArgs(); i != e; ++i) + Writer.AddTemplateArgumentLoc(DFTSInfo->getTemplateArg(i), Record); + Writer.AddSourceLocation(DFTSInfo->getLAngleLoc(), Record); + Writer.AddSourceLocation(DFTSInfo->getRAngleLoc(), Record); + break; + } + } + + // FunctionDecl's body is handled last at PCHWriterDecl::Visit, + // after everything else is written. Writer.AddDeclRef(D->getPreviousDeclaration(), Record); Record.push_back(D->getStorageClass()); // FIXME: stable encoding @@ -211,7 +290,6 @@ void PCHDeclWriter::VisitFunctionDecl(FunctionDecl *D) { Record.push_back(D->isTrivial()); Record.push_back(D->isCopyAssignment()); Record.push_back(D->hasImplicitReturnZero()); - // FIXME: C++ TemplateOrInstantiation??? Writer.AddSourceLocation(D->getLocEnd(), Record); Record.push_back(D->param_size()); @@ -357,9 +435,10 @@ void PCHDeclWriter::VisitObjCCompatibleAliasDecl(ObjCCompatibleAliasDecl *D) { void PCHDeclWriter::VisitObjCPropertyDecl(ObjCPropertyDecl *D) { VisitNamedDecl(D); Writer.AddSourceLocation(D->getAtLoc(), Record); - Writer.AddTypeRef(D->getType(), Record); + Writer.AddTypeSourceInfo(D->getTypeSourceInfo(), Record); // FIXME: stable encoding Record.push_back((unsigned)D->getPropertyAttributes()); + Record.push_back((unsigned)D->getPropertyAttributesAsWritten()); // FIXME: stable encoding Record.push_back((unsigned)D->getPropertyImplementation()); Writer.AddDeclarationName(D->getGetterName(), Record); @@ -404,6 +483,8 @@ void PCHDeclWriter::VisitFieldDecl(FieldDecl *D) { Record.push_back(D->getBitWidth()? 1 : 0); if (D->getBitWidth()) Writer.AddStmt(D->getBitWidth()); + if (!D->getDeclName()) + Writer.AddDeclRef(Context.getInstantiatedFromUnnamedFieldDecl(D), Record); Code = pch::DECL_FIELD; } @@ -420,6 +501,16 @@ void PCHDeclWriter::VisitVarDecl(VarDecl *D) { Record.push_back(D->getInit() ? 1 : 0); if (D->getInit()) Writer.AddStmt(D->getInit()); + + MemberSpecializationInfo *SpecInfo + = D->isStaticDataMember() ? D->getMemberSpecializationInfo() : 0; + Record.push_back(SpecInfo != 0); + if (SpecInfo) { + Writer.AddDeclRef(SpecInfo->getInstantiatedFrom(), Record); + Record.push_back(SpecInfo->getTemplateSpecializationKind()); + Writer.AddSourceLocation(SpecInfo->getPointOfInstantiation(), Record); + } + Code = pch::DECL_VAR; } @@ -432,6 +523,9 @@ void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) { VisitVarDecl(D); Record.push_back(D->getObjCDeclQualifier()); // FIXME: stable encoding Record.push_back(D->hasInheritedDefaultArg()); + Record.push_back(D->hasUninstantiatedDefaultArg()); + if (D->hasUninstantiatedDefaultArg()) + Writer.AddStmt(D->getUninstantiatedDefaultArg()); Code = pch::DECL_PARM_VAR; // If the assumptions about the DECL_PARM_VAR abbrev are true, use it. Here @@ -440,14 +534,15 @@ void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) { if (!D->getTypeSourceInfo() && !D->hasAttrs() && !D->isImplicit() && - !D->isUsed() && + !D->isUsed(false) && D->getAccess() == AS_none && D->getPCHLevel() == 0 && D->getStorageClass() == 0 && !D->hasCXXDirectInitializer() && // Can params have this ever? D->getObjCDeclQualifier() == 0 && !D->hasInheritedDefaultArg() && - D->getInit() == 0) // No default expr. + D->getInit() == 0 && + !D->hasUninstantiatedDefaultArg()) // No default expr. AbbrevToUse = Writer.getParmVarDeclAbbrev(); // Check things we know are true of *every* PARM_VAR_DECL, which is more than @@ -458,6 +553,8 @@ void PCHDeclWriter::VisitParmVarDecl(ParmVarDecl *D) { assert(!D->isDeclaredInCondition() && "PARM_VAR_DECL can't be in condition"); assert(!D->isExceptionVariable() && "PARM_VAR_DECL can't be exception var"); assert(D->getPreviousDeclaration() == 0 && "PARM_VAR_DECL can't be redecl"); + assert(!D->isStaticDataMember() && + "PARM_VAR_DECL can't be static data member"); } void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) { @@ -469,6 +566,7 @@ void PCHDeclWriter::VisitFileScopeAsmDecl(FileScopeAsmDecl *D) { void PCHDeclWriter::VisitBlockDecl(BlockDecl *D) { VisitDecl(D); Writer.AddStmt(D->getBody()); + Writer.AddTypeSourceInfo(D->getSignatureAsWritten(), Record); Record.push_back(D->param_size()); for (FunctionDecl::param_iterator P = D->param_begin(), PEnd = D->param_end(); P != PEnd; ++P) @@ -510,7 +608,7 @@ void PCHDeclWriter::VisitNamespaceAliasDecl(NamespaceAliasDecl *D) { Code = pch::DECL_NAMESPACE_ALIAS; } -void PCHDeclWriter::VisitUsing(UsingDecl *D) { +void PCHDeclWriter::VisitUsingDecl(UsingDecl *D) { VisitNamedDecl(D); Writer.AddSourceRange(D->getNestedNameRange(), Record); Writer.AddSourceLocation(D->getUsingLocation(), Record); @@ -520,13 +618,15 @@ void PCHDeclWriter::VisitUsing(UsingDecl *D) { PEnd = D->shadow_end(); P != PEnd; ++P) Writer.AddDeclRef(*P, Record); Record.push_back(D->isTypeName()); + Writer.AddDeclRef(Context.getInstantiatedFromUsingDecl(D), Record); Code = pch::DECL_USING; } -void PCHDeclWriter::VisitUsingShadow(UsingShadowDecl *D) { +void PCHDeclWriter::VisitUsingShadowDecl(UsingShadowDecl *D) { VisitNamedDecl(D); Writer.AddDeclRef(D->getTargetDecl(), Record); Writer.AddDeclRef(D->getUsingDecl(), Record); + Writer.AddDeclRef(Context.getInstantiatedFromUsingShadowDecl(D), Record); Code = pch::DECL_USING_SHADOW; } @@ -541,7 +641,7 @@ void PCHDeclWriter::VisitUsingDirectiveDecl(UsingDirectiveDecl *D) { Code = pch::DECL_USING_DIRECTIVE; } -void PCHDeclWriter::VisitUnresolvedUsingValue(UnresolvedUsingValueDecl *D) { +void PCHDeclWriter::VisitUnresolvedUsingValueDecl(UnresolvedUsingValueDecl *D) { VisitValueDecl(D); Writer.AddSourceRange(D->getTargetNestedNameRange(), Record); Writer.AddSourceLocation(D->getUsingLoc(), Record); @@ -549,7 +649,7 @@ void PCHDeclWriter::VisitUnresolvedUsingValue(UnresolvedUsingValueDecl *D) { Code = pch::DECL_UNRESOLVED_USING_VALUE; } -void PCHDeclWriter::VisitUnresolvedUsingTypename( +void PCHDeclWriter::VisitUnresolvedUsingTypenameDecl( UnresolvedUsingTypenameDecl *D) { VisitTypeDecl(D); Writer.AddSourceRange(D->getTargetNestedNameRange(), Record); @@ -560,71 +660,324 @@ void PCHDeclWriter::VisitUnresolvedUsingTypename( } void PCHDeclWriter::VisitCXXRecordDecl(CXXRecordDecl *D) { - // assert(false && "cannot write CXXRecordDecl"); + // See comments at PCHDeclReader::VisitCXXRecordDecl about why this happens + // before VisitRecordDecl. + enum { Data_NoDefData, Data_Owner, Data_NotOwner }; + bool OwnsDefinitionData = false; + if (D->DefinitionData) { + assert(D->DefinitionData->Definition && + "DefinitionData don't point to a definition decl!"); + OwnsDefinitionData = D->DefinitionData->Definition == D; + if (OwnsDefinitionData) { + Record.push_back(Data_Owner); + } else { + Record.push_back(Data_NotOwner); + Writer.AddDeclRef(D->DefinitionData->Definition, Record); + } + } else + Record.push_back(Data_NoDefData); + VisitRecordDecl(D); + + if (OwnsDefinitionData) { + assert(D->DefinitionData); + struct CXXRecordDecl::DefinitionData &Data = *D->DefinitionData; + + Record.push_back(Data.UserDeclaredConstructor); + Record.push_back(Data.UserDeclaredCopyConstructor); + Record.push_back(Data.UserDeclaredCopyAssignment); + Record.push_back(Data.UserDeclaredDestructor); + Record.push_back(Data.Aggregate); + Record.push_back(Data.PlainOldData); + Record.push_back(Data.Empty); + Record.push_back(Data.Polymorphic); + Record.push_back(Data.Abstract); + Record.push_back(Data.HasTrivialConstructor); + Record.push_back(Data.HasTrivialCopyConstructor); + Record.push_back(Data.HasTrivialCopyAssignment); + Record.push_back(Data.HasTrivialDestructor); + Record.push_back(Data.ComputedVisibleConversions); + Record.push_back(Data.DeclaredDefaultConstructor); + Record.push_back(Data.DeclaredCopyConstructor); + Record.push_back(Data.DeclaredCopyAssignment); + Record.push_back(Data.DeclaredDestructor); + + Record.push_back(D->getNumBases()); + for (CXXRecordDecl::base_class_iterator I = D->bases_begin(), + E = D->bases_end(); I != E; ++I) + Writer.AddCXXBaseSpecifier(*I, Record); + + // FIXME: Make VBases lazily computed when needed to avoid storing them. + Record.push_back(D->getNumVBases()); + for (CXXRecordDecl::base_class_iterator I = D->vbases_begin(), + E = D->vbases_end(); I != E; ++I) + Writer.AddCXXBaseSpecifier(*I, Record); + + Writer.AddUnresolvedSet(Data.Conversions, Record); + Writer.AddUnresolvedSet(Data.VisibleConversions, Record); + // Data.Definition is written at the top. + Writer.AddDeclRef(Data.FirstFriend, Record); + } + + enum { + CXXRecNotTemplate = 0, CXXRecTemplate, CXXRecMemberSpecialization + }; + if (ClassTemplateDecl *TemplD = D->getDescribedClassTemplate()) { + Record.push_back(CXXRecTemplate); + Writer.AddDeclRef(TemplD, Record); + } else if (MemberSpecializationInfo *MSInfo + = D->getMemberSpecializationInfo()) { + Record.push_back(CXXRecMemberSpecialization); + Writer.AddDeclRef(MSInfo->getInstantiatedFrom(), Record); + Record.push_back(MSInfo->getTemplateSpecializationKind()); + Writer.AddSourceLocation(MSInfo->getPointOfInstantiation(), Record); + } else { + Record.push_back(CXXRecNotTemplate); + } + Code = pch::DECL_CXX_RECORD; } void PCHDeclWriter::VisitCXXMethodDecl(CXXMethodDecl *D) { - // assert(false && "cannot write CXXMethodDecl"); VisitFunctionDecl(D); + Record.push_back(D->size_overridden_methods()); + for (CXXMethodDecl::method_iterator + I = D->begin_overridden_methods(), E = D->end_overridden_methods(); + I != E; ++I) + Writer.AddDeclRef(*I, Record); Code = pch::DECL_CXX_METHOD; } void PCHDeclWriter::VisitCXXConstructorDecl(CXXConstructorDecl *D) { - // assert(false && "cannot write CXXConstructorDecl"); VisitCXXMethodDecl(D); + + Record.push_back(D->IsExplicitSpecified); + Record.push_back(D->ImplicitlyDefined); + + Record.push_back(D->NumBaseOrMemberInitializers); + for (unsigned i=0; i != D->NumBaseOrMemberInitializers; ++i) { + CXXBaseOrMemberInitializer *Init = D->BaseOrMemberInitializers[i]; + + Record.push_back(Init->isBaseInitializer()); + if (Init->isBaseInitializer()) { + Writer.AddTypeSourceInfo(Init->getBaseClassInfo(), Record); + Record.push_back(Init->isBaseVirtual()); + } else { + Writer.AddDeclRef(Init->getMember(), Record); + } + Writer.AddSourceLocation(Init->getMemberLocation(), Record); + Writer.AddStmt(Init->getInit()); + Writer.AddDeclRef(Init->getAnonUnionMember(), Record); + Writer.AddSourceLocation(Init->getLParenLoc(), Record); + Writer.AddSourceLocation(Init->getRParenLoc(), Record); + Record.push_back(Init->isWritten()); + if (Init->isWritten()) { + Record.push_back(Init->getSourceOrder()); + } else { + Record.push_back(Init->getNumArrayIndices()); + for (unsigned i=0, e=Init->getNumArrayIndices(); i != e; ++i) + Writer.AddDeclRef(Init->getArrayIndex(i), Record); + } + } + Code = pch::DECL_CXX_CONSTRUCTOR; } void PCHDeclWriter::VisitCXXDestructorDecl(CXXDestructorDecl *D) { - // assert(false && "cannot write CXXDestructorDecl"); VisitCXXMethodDecl(D); + + Record.push_back(D->ImplicitlyDefined); + Writer.AddDeclRef(D->OperatorDelete, Record); + Code = pch::DECL_CXX_DESTRUCTOR; } void PCHDeclWriter::VisitCXXConversionDecl(CXXConversionDecl *D) { - // assert(false && "cannot write CXXConversionDecl"); VisitCXXMethodDecl(D); + Record.push_back(D->IsExplicitSpecified); Code = pch::DECL_CXX_CONVERSION; } +void PCHDeclWriter::VisitAccessSpecDecl(AccessSpecDecl *D) { + VisitDecl(D); + Writer.AddSourceLocation(D->getColonLoc(), Record); + Code = pch::DECL_ACCESS_SPEC; +} + +void PCHDeclWriter::VisitFriendDecl(FriendDecl *D) { + VisitDecl(D); + Record.push_back(D->Friend.is<TypeSourceInfo*>()); + if (D->Friend.is<TypeSourceInfo*>()) + Writer.AddTypeSourceInfo(D->Friend.get<TypeSourceInfo*>(), Record); + else + Writer.AddDeclRef(D->Friend.get<NamedDecl*>(), Record); + Writer.AddDeclRef(D->NextFriend, Record); + Writer.AddSourceLocation(D->FriendLoc, Record); + Code = pch::DECL_FRIEND; +} + void PCHDeclWriter::VisitFriendTemplateDecl(FriendTemplateDecl *D) { assert(false && "cannot write FriendTemplateDecl"); } void PCHDeclWriter::VisitTemplateDecl(TemplateDecl *D) { - assert(false && "cannot write TemplateDecl"); + VisitNamedDecl(D); + + Writer.AddDeclRef(D->getTemplatedDecl(), Record); + Writer.AddTemplateParameterList(D->getTemplateParameters(), Record); +} + +static bool IsKeptInFoldingSet(ClassTemplateSpecializationDecl *D) { + return D->getTypeForDecl()->getAsCXXRecordDecl() == D; } void PCHDeclWriter::VisitClassTemplateDecl(ClassTemplateDecl *D) { - assert(false && "cannot write ClassTemplateDecl"); + VisitTemplateDecl(D); + + Record.push_back(D->getIdentifierNamespace()); + Writer.AddDeclRef(D->getPreviousDeclaration(), Record); + if (D->getPreviousDeclaration() == 0) { + // This ClassTemplateDecl owns the CommonPtr; write it. + assert(D->isCanonicalDecl()); + + typedef llvm::FoldingSet<ClassTemplateSpecializationDecl> CTSDSetTy; + CTSDSetTy &CTSDSet = D->getSpecializations(); + Record.push_back(CTSDSet.size()); + for (CTSDSetTy::iterator I=CTSDSet.begin(), E = CTSDSet.end(); I!=E; ++I) { + assert(IsKeptInFoldingSet(&*I)); + Writer.AddDeclRef(&*I, Record); + } + + typedef llvm::FoldingSet<ClassTemplatePartialSpecializationDecl> CTPSDSetTy; + CTPSDSetTy &CTPSDSet = D->getPartialSpecializations(); + Record.push_back(CTPSDSet.size()); + for (CTPSDSetTy::iterator I=CTPSDSet.begin(), E=CTPSDSet.end(); I!=E; ++I) { + assert(IsKeptInFoldingSet(&*I)); + Writer.AddDeclRef(&*I, Record); + } + + // InjectedClassNameType is computed, no need to write it. + + Writer.AddDeclRef(D->getInstantiatedFromMemberTemplate(), Record); + if (D->getInstantiatedFromMemberTemplate()) + Record.push_back(D->isMemberSpecialization()); + } + Code = pch::DECL_CLASS_TEMPLATE; } void PCHDeclWriter::VisitClassTemplateSpecializationDecl( ClassTemplateSpecializationDecl *D) { - assert(false && "cannot write ClassTemplateSpecializationDecl"); + VisitCXXRecordDecl(D); + + llvm::PointerUnion<ClassTemplateDecl *, + ClassTemplatePartialSpecializationDecl *> InstFrom + = D->getSpecializedTemplateOrPartial(); + if (InstFrom.is<ClassTemplateDecl *>()) { + Writer.AddDeclRef(InstFrom.get<ClassTemplateDecl *>(), Record); + } else { + Writer.AddDeclRef(InstFrom.get<ClassTemplatePartialSpecializationDecl *>(), + Record); + Writer.AddTemplateArgumentList(&D->getTemplateInstantiationArgs(), Record); + } + + // Explicit info. + Writer.AddTypeSourceInfo(D->getTypeAsWritten(), Record); + if (D->getTypeAsWritten()) { + Writer.AddSourceLocation(D->getExternLoc(), Record); + Writer.AddSourceLocation(D->getTemplateKeywordLoc(), Record); + } + + Writer.AddTemplateArgumentList(&D->getTemplateArgs(), Record); + Writer.AddSourceLocation(D->getPointOfInstantiation(), Record); + Record.push_back(D->getSpecializationKind()); + + bool IsInInFoldingSet = IsKeptInFoldingSet(D); + Record.push_back(IsInInFoldingSet); + if (IsInInFoldingSet) { + // When reading, we'll add it to the folding set of this one. + Writer.AddDeclRef(D->getSpecializedTemplate()->getCanonicalDecl(), Record); + } + + Code = pch::DECL_CLASS_TEMPLATE_SPECIALIZATION; } void PCHDeclWriter::VisitClassTemplatePartialSpecializationDecl( ClassTemplatePartialSpecializationDecl *D) { - assert(false && "cannot write ClassTemplatePartialSpecializationDecl"); + VisitClassTemplateSpecializationDecl(D); + + Writer.AddTemplateParameterList(D->getTemplateParameters(), Record); + + Record.push_back(D->getNumTemplateArgsAsWritten()); + for (int i = 0, e = D->getNumTemplateArgsAsWritten(); i != e; ++i) + Writer.AddTemplateArgumentLoc(D->getTemplateArgsAsWritten()[i], Record); + + Record.push_back(D->getSequenceNumber()); + + // These are read/set from/to the first declaration. + if (D->getPreviousDeclaration() == 0) { + Writer.AddDeclRef(D->getInstantiatedFromMember(), Record); + Record.push_back(D->isMemberSpecialization()); + } + + Code = pch::DECL_CLASS_TEMPLATE_PARTIAL_SPECIALIZATION; } -void PCHDeclWriter::visitFunctionTemplateDecl(FunctionTemplateDecl *D) { - assert(false && "cannot write FunctionTemplateDecl"); +void PCHDeclWriter::VisitFunctionTemplateDecl(FunctionTemplateDecl *D) { + VisitTemplateDecl(D); + + Record.push_back(D->getIdentifierNamespace()); + Writer.AddDeclRef(D->getPreviousDeclaration(), Record); + if (D->getPreviousDeclaration() == 0) { + // This FunctionTemplateDecl owns the CommonPtr; write it. + + // Write the function specialization declarations. + Record.push_back(D->getSpecializations().size()); + for (llvm::FoldingSet<FunctionTemplateSpecializationInfo>::iterator + I = D->getSpecializations().begin(), + E = D->getSpecializations().end() ; I != E; ++I) + Writer.AddDeclRef(I->Function, Record); + + Writer.AddDeclRef(D->getInstantiatedFromMemberTemplate(), Record); + if (D->getInstantiatedFromMemberTemplate()) + Record.push_back(D->isMemberSpecialization()); + } + Code = pch::DECL_FUNCTION_TEMPLATE; } void PCHDeclWriter::VisitTemplateTypeParmDecl(TemplateTypeParmDecl *D) { - assert(false && "cannot write TemplateTypeParmDecl"); + VisitTypeDecl(D); + + Record.push_back(D->wasDeclaredWithTypename()); + Record.push_back(D->isParameterPack()); + Record.push_back(D->defaultArgumentWasInherited()); + Writer.AddTypeSourceInfo(D->getDefaultArgumentInfo(), Record); + + Code = pch::DECL_TEMPLATE_TYPE_PARM; } void PCHDeclWriter::VisitNonTypeTemplateParmDecl(NonTypeTemplateParmDecl *D) { - assert(false && "cannot write NonTypeTemplateParmDecl"); + VisitVarDecl(D); + // TemplateParmPosition. + Record.push_back(D->getDepth()); + Record.push_back(D->getPosition()); + // Rest of NonTypeTemplateParmDecl. + Record.push_back(D->getDefaultArgument() != 0); + if (D->getDefaultArgument()) { + Writer.AddStmt(D->getDefaultArgument()); + Record.push_back(D->defaultArgumentWasInherited()); + } + Code = pch::DECL_NON_TYPE_TEMPLATE_PARM; } void PCHDeclWriter::VisitTemplateTemplateParmDecl(TemplateTemplateParmDecl *D) { - assert(false && "cannot write TemplateTemplateParmDecl"); + VisitTemplateDecl(D); + // TemplateParmPosition. + Record.push_back(D->getDepth()); + Record.push_back(D->getPosition()); + // Rest of TemplateTemplateParmDecl. + Writer.AddTemplateArgumentLoc(D->getDefaultArgument(), Record); + Record.push_back(D->defaultArgumentWasInherited()); + Code = pch::DECL_TEMPLATE_TEMPLATE_PARM; } void PCHDeclWriter::VisitStaticAssertDecl(StaticAssertDecl *D) { @@ -687,9 +1040,11 @@ void PCHWriter::WriteDeclsBlockAbbrevs() { Abv->Add(BitCodeAbbrevOp(0)); // isNRVOVariable Abv->Add(BitCodeAbbrevOp(0)); // PrevDecl Abv->Add(BitCodeAbbrevOp(0)); // HasInit + Abv->Add(BitCodeAbbrevOp(0)); // HasMemberSpecializationInfo // ParmVarDecl Abv->Add(BitCodeAbbrevOp(0)); // ObjCDeclQualifier Abv->Add(BitCodeAbbrevOp(0)); // HasInheritedDefaultArg + Abv->Add(BitCodeAbbrevOp(0)); // HasUninstantiatedDefaultArg ParmVarDeclAbbrev = Stream.EmitAbbrev(Abv); } diff --git a/contrib/llvm/tools/clang/lib/Frontend/PCHWriterStmt.cpp b/contrib/llvm/tools/clang/lib/Frontend/PCHWriterStmt.cpp index a9ee435..7537728 100644 --- a/contrib/llvm/tools/clang/lib/Frontend/PCHWriterStmt.cpp +++ b/contrib/llvm/tools/clang/lib/Frontend/PCHWriterStmt.cpp @@ -22,7 +22,7 @@ using namespace clang; // Statement/expression serialization //===----------------------------------------------------------------------===// -namespace { +namespace clang { class PCHStmtWriter : public StmtVisitor<PCHStmtWriter, void> { PCHWriter &Writer; PCHWriter::RecordData &Record; @@ -32,6 +32,9 @@ namespace { PCHStmtWriter(PCHWriter &Writer, PCHWriter::RecordData &Record) : Writer(Writer), Record(Record) { } + + void + AddExplicitTemplateArgumentList(const ExplicitTemplateArgumentList &Args); void VisitStmt(Stmt *S); void VisitNullStmt(NullStmt *S); @@ -61,6 +64,7 @@ namespace { void VisitStringLiteral(StringLiteral *E); void VisitCharacterLiteral(CharacterLiteral *E); void VisitParenExpr(ParenExpr *E); + void VisitParenListExpr(ParenListExpr *E); void VisitUnaryOperator(UnaryOperator *E); void VisitOffsetOfExpr(OffsetOfExpr *E); void VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E); @@ -114,6 +118,7 @@ namespace { void VisitCXXOperatorCallExpr(CXXOperatorCallExpr *E); void VisitCXXMemberCallExpr(CXXMemberCallExpr *E); void VisitCXXConstructExpr(CXXConstructExpr *E); + void VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E); void VisitCXXNamedCastExpr(CXXNamedCastExpr *E); void VisitCXXStaticCastExpr(CXXStaticCastExpr *E); void VisitCXXDynamicCastExpr(CXXDynamicCastExpr *E); @@ -127,14 +132,34 @@ namespace { void VisitCXXThrowExpr(CXXThrowExpr *E); void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E); void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E); - - void VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E); + void VisitCXXBindReferenceExpr(CXXBindReferenceExpr *E); + + void VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E); void VisitCXXNewExpr(CXXNewExpr *E); - + void VisitCXXDeleteExpr(CXXDeleteExpr *E); + void VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E); + void VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E); + void VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E); + void VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E); + void VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *E); + + void VisitOverloadExpr(OverloadExpr *E); + void VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E); + void VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E); + + void VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E); }; } +void PCHStmtWriter:: +AddExplicitTemplateArgumentList(const ExplicitTemplateArgumentList &Args) { + Writer.AddSourceLocation(Args.LAngleLoc, Record); + Writer.AddSourceLocation(Args.RAngleLoc, Record); + for (unsigned i=0; i != Args.NumTemplateArgs; ++i) + Writer.AddTemplateArgumentLoc(Args.getTemplateArgs()[i], Record); +} + void PCHStmtWriter::VisitStmt(Stmt *S) { } @@ -149,7 +174,7 @@ void PCHStmtWriter::VisitCompoundStmt(CompoundStmt *S) { Record.push_back(S->size()); for (CompoundStmt::body_iterator CS = S->body_begin(), CSEnd = S->body_end(); CS != CSEnd; ++CS) - Writer.WriteSubStmt(*CS); + Writer.AddStmt(*CS); Writer.AddSourceLocation(S->getLBracLoc(), Record); Writer.AddSourceLocation(S->getRBracLoc(), Record); Code = pch::STMT_COMPOUND; @@ -157,14 +182,14 @@ void PCHStmtWriter::VisitCompoundStmt(CompoundStmt *S) { void PCHStmtWriter::VisitSwitchCase(SwitchCase *S) { VisitStmt(S); - Record.push_back(Writer.RecordSwitchCaseID(S)); + Record.push_back(Writer.getSwitchCaseID(S)); } void PCHStmtWriter::VisitCaseStmt(CaseStmt *S) { VisitSwitchCase(S); - Writer.WriteSubStmt(S->getLHS()); - Writer.WriteSubStmt(S->getRHS()); - Writer.WriteSubStmt(S->getSubStmt()); + Writer.AddStmt(S->getLHS()); + Writer.AddStmt(S->getRHS()); + Writer.AddStmt(S->getSubStmt()); Writer.AddSourceLocation(S->getCaseLoc(), Record); Writer.AddSourceLocation(S->getEllipsisLoc(), Record); Writer.AddSourceLocation(S->getColonLoc(), Record); @@ -173,7 +198,7 @@ void PCHStmtWriter::VisitCaseStmt(CaseStmt *S) { void PCHStmtWriter::VisitDefaultStmt(DefaultStmt *S) { VisitSwitchCase(S); - Writer.WriteSubStmt(S->getSubStmt()); + Writer.AddStmt(S->getSubStmt()); Writer.AddSourceLocation(S->getDefaultLoc(), Record); Writer.AddSourceLocation(S->getColonLoc(), Record); Code = pch::STMT_DEFAULT; @@ -182,7 +207,7 @@ void PCHStmtWriter::VisitDefaultStmt(DefaultStmt *S) { void PCHStmtWriter::VisitLabelStmt(LabelStmt *S) { VisitStmt(S); Writer.AddIdentifierRef(S->getID(), Record); - Writer.WriteSubStmt(S->getSubStmt()); + Writer.AddStmt(S->getSubStmt()); Writer.AddSourceLocation(S->getIdentLoc(), Record); Record.push_back(Writer.GetLabelID(S)); Code = pch::STMT_LABEL; @@ -191,9 +216,9 @@ void PCHStmtWriter::VisitLabelStmt(LabelStmt *S) { void PCHStmtWriter::VisitIfStmt(IfStmt *S) { VisitStmt(S); Writer.AddDeclRef(S->getConditionVariable(), Record); - Writer.WriteSubStmt(S->getCond()); - Writer.WriteSubStmt(S->getThen()); - Writer.WriteSubStmt(S->getElse()); + Writer.AddStmt(S->getCond()); + Writer.AddStmt(S->getThen()); + Writer.AddStmt(S->getElse()); Writer.AddSourceLocation(S->getIfLoc(), Record); Writer.AddSourceLocation(S->getElseLoc(), Record); Code = pch::STMT_IF; @@ -202,28 +227,28 @@ void PCHStmtWriter::VisitIfStmt(IfStmt *S) { void PCHStmtWriter::VisitSwitchStmt(SwitchStmt *S) { VisitStmt(S); Writer.AddDeclRef(S->getConditionVariable(), Record); - Writer.WriteSubStmt(S->getCond()); - Writer.WriteSubStmt(S->getBody()); + Writer.AddStmt(S->getCond()); + Writer.AddStmt(S->getBody()); Writer.AddSourceLocation(S->getSwitchLoc(), Record); for (SwitchCase *SC = S->getSwitchCaseList(); SC; SC = SC->getNextSwitchCase()) - Record.push_back(Writer.getSwitchCaseID(SC)); + Record.push_back(Writer.RecordSwitchCaseID(SC)); Code = pch::STMT_SWITCH; } void PCHStmtWriter::VisitWhileStmt(WhileStmt *S) { VisitStmt(S); Writer.AddDeclRef(S->getConditionVariable(), Record); - Writer.WriteSubStmt(S->getCond()); - Writer.WriteSubStmt(S->getBody()); + Writer.AddStmt(S->getCond()); + Writer.AddStmt(S->getBody()); Writer.AddSourceLocation(S->getWhileLoc(), Record); Code = pch::STMT_WHILE; } void PCHStmtWriter::VisitDoStmt(DoStmt *S) { VisitStmt(S); - Writer.WriteSubStmt(S->getCond()); - Writer.WriteSubStmt(S->getBody()); + Writer.AddStmt(S->getCond()); + Writer.AddStmt(S->getBody()); Writer.AddSourceLocation(S->getDoLoc(), Record); Writer.AddSourceLocation(S->getWhileLoc(), Record); Writer.AddSourceLocation(S->getRParenLoc(), Record); @@ -232,11 +257,11 @@ void PCHStmtWriter::VisitDoStmt(DoStmt *S) { void PCHStmtWriter::VisitForStmt(ForStmt *S) { VisitStmt(S); - Writer.WriteSubStmt(S->getInit()); - Writer.WriteSubStmt(S->getCond()); + Writer.AddStmt(S->getInit()); + Writer.AddStmt(S->getCond()); Writer.AddDeclRef(S->getConditionVariable(), Record); - Writer.WriteSubStmt(S->getInc()); - Writer.WriteSubStmt(S->getBody()); + Writer.AddStmt(S->getInc()); + Writer.AddStmt(S->getBody()); Writer.AddSourceLocation(S->getForLoc(), Record); Writer.AddSourceLocation(S->getLParenLoc(), Record); Writer.AddSourceLocation(S->getRParenLoc(), Record); @@ -255,7 +280,7 @@ void PCHStmtWriter::VisitIndirectGotoStmt(IndirectGotoStmt *S) { VisitStmt(S); Writer.AddSourceLocation(S->getGotoLoc(), Record); Writer.AddSourceLocation(S->getStarLoc(), Record); - Writer.WriteSubStmt(S->getTarget()); + Writer.AddStmt(S->getTarget()); Code = pch::STMT_INDIRECT_GOTO; } @@ -273,7 +298,7 @@ void PCHStmtWriter::VisitBreakStmt(BreakStmt *S) { void PCHStmtWriter::VisitReturnStmt(ReturnStmt *S) { VisitStmt(S); - Writer.WriteSubStmt(S->getRetValue()); + Writer.AddStmt(S->getRetValue()); Writer.AddSourceLocation(S->getReturnLoc(), Record); Writer.AddDeclRef(S->getNRVOCandidate(), Record); Code = pch::STMT_RETURN; @@ -299,25 +324,25 @@ void PCHStmtWriter::VisitAsmStmt(AsmStmt *S) { Record.push_back(S->isVolatile()); Record.push_back(S->isSimple()); Record.push_back(S->isMSAsm()); - Writer.WriteSubStmt(S->getAsmString()); + Writer.AddStmt(S->getAsmString()); // Outputs for (unsigned I = 0, N = S->getNumOutputs(); I != N; ++I) { Writer.AddIdentifierRef(S->getOutputIdentifier(I), Record); - Writer.WriteSubStmt(S->getOutputConstraintLiteral(I)); - Writer.WriteSubStmt(S->getOutputExpr(I)); + Writer.AddStmt(S->getOutputConstraintLiteral(I)); + Writer.AddStmt(S->getOutputExpr(I)); } // Inputs for (unsigned I = 0, N = S->getNumInputs(); I != N; ++I) { Writer.AddIdentifierRef(S->getInputIdentifier(I), Record); - Writer.WriteSubStmt(S->getInputConstraintLiteral(I)); - Writer.WriteSubStmt(S->getInputExpr(I)); + Writer.AddStmt(S->getInputConstraintLiteral(I)); + Writer.AddStmt(S->getInputExpr(I)); } // Clobbers for (unsigned I = 0, N = S->getNumClobbers(); I != N; ++I) - Writer.WriteSubStmt(S->getClobber(I)); + Writer.AddStmt(S->getClobber(I)); Code = pch::STMT_ASM; } @@ -338,10 +363,23 @@ void PCHStmtWriter::VisitPredefinedExpr(PredefinedExpr *E) { void PCHStmtWriter::VisitDeclRefExpr(DeclRefExpr *E) { VisitExpr(E); + + Record.push_back(E->hasQualifier()); + unsigned NumTemplateArgs = E->getNumTemplateArgs(); + assert((NumTemplateArgs != 0) == E->hasExplicitTemplateArgumentList() && + "Template args list with no args ?"); + Record.push_back(NumTemplateArgs); + + if (E->hasQualifier()) { + Writer.AddNestedNameSpecifier(E->getQualifier(), Record); + Writer.AddSourceRange(E->getQualifierRange(), Record); + } + + if (NumTemplateArgs) + AddExplicitTemplateArgumentList(*E->getExplicitTemplateArgumentList()); + Writer.AddDeclRef(E->getDecl(), Record); Writer.AddSourceLocation(E->getLocation(), Record); - // FIXME: write qualifier - // FIXME: write explicit template arguments Code = pch::EXPR_DECL_REF; } @@ -362,7 +400,7 @@ void PCHStmtWriter::VisitFloatingLiteral(FloatingLiteral *E) { void PCHStmtWriter::VisitImaginaryLiteral(ImaginaryLiteral *E) { VisitExpr(E); - Writer.WriteSubStmt(E->getSubExpr()); + Writer.AddStmt(E->getSubExpr()); Code = pch::EXPR_IMAGINARY_LITERAL; } @@ -394,13 +432,23 @@ void PCHStmtWriter::VisitParenExpr(ParenExpr *E) { VisitExpr(E); Writer.AddSourceLocation(E->getLParen(), Record); Writer.AddSourceLocation(E->getRParen(), Record); - Writer.WriteSubStmt(E->getSubExpr()); + Writer.AddStmt(E->getSubExpr()); Code = pch::EXPR_PAREN; } +void PCHStmtWriter::VisitParenListExpr(ParenListExpr *E) { + VisitExpr(E); + Record.push_back(E->NumExprs); + for (unsigned i=0; i != E->NumExprs; ++i) + Writer.AddStmt(E->Exprs[i]); + Writer.AddSourceLocation(E->LParenLoc, Record); + Writer.AddSourceLocation(E->RParenLoc, Record); + Code = pch::EXPR_PAREN_LIST; +} + void PCHStmtWriter::VisitUnaryOperator(UnaryOperator *E) { VisitExpr(E); - Writer.WriteSubStmt(E->getSubExpr()); + Writer.AddStmt(E->getSubExpr()); Record.push_back(E->getOpcode()); // FIXME: stable encoding Writer.AddSourceLocation(E->getOperatorLoc(), Record); Code = pch::EXPR_UNARY_OPERATOR; @@ -438,7 +486,7 @@ void PCHStmtWriter::VisitOffsetOfExpr(OffsetOfExpr *E) { } } for (unsigned I = 0, N = E->getNumExpressions(); I != N; ++I) - Writer.WriteSubStmt(E->getIndexExpr(I)); + Writer.AddStmt(E->getIndexExpr(I)); Code = pch::EXPR_OFFSETOF; } @@ -449,7 +497,7 @@ void PCHStmtWriter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) { Writer.AddTypeSourceInfo(E->getArgumentTypeInfo(), Record); else { Record.push_back(0); - Writer.WriteSubStmt(E->getArgumentExpr()); + Writer.AddStmt(E->getArgumentExpr()); } Writer.AddSourceLocation(E->getOperatorLoc(), Record); Writer.AddSourceLocation(E->getRParenLoc(), Record); @@ -458,8 +506,8 @@ void PCHStmtWriter::VisitSizeOfAlignOfExpr(SizeOfAlignOfExpr *E) { void PCHStmtWriter::VisitArraySubscriptExpr(ArraySubscriptExpr *E) { VisitExpr(E); - Writer.WriteSubStmt(E->getLHS()); - Writer.WriteSubStmt(E->getRHS()); + Writer.AddStmt(E->getLHS()); + Writer.AddStmt(E->getRHS()); Writer.AddSourceLocation(E->getRBracketLoc(), Record); Code = pch::EXPR_ARRAY_SUBSCRIPT; } @@ -468,27 +516,48 @@ void PCHStmtWriter::VisitCallExpr(CallExpr *E) { VisitExpr(E); Record.push_back(E->getNumArgs()); Writer.AddSourceLocation(E->getRParenLoc(), Record); - Writer.WriteSubStmt(E->getCallee()); + Writer.AddStmt(E->getCallee()); for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end(); Arg != ArgEnd; ++Arg) - Writer.WriteSubStmt(*Arg); + Writer.AddStmt(*Arg); Code = pch::EXPR_CALL; } void PCHStmtWriter::VisitMemberExpr(MemberExpr *E) { - VisitExpr(E); - Writer.WriteSubStmt(E->getBase()); + // Don't call VisitExpr, we'll write everything here. + + Record.push_back(E->hasQualifier()); + if (E->hasQualifier()) { + Writer.AddNestedNameSpecifier(E->getQualifier(), Record); + Writer.AddSourceRange(E->getQualifierRange(), Record); + } + + unsigned NumTemplateArgs = E->getNumTemplateArgs(); + assert((NumTemplateArgs != 0) == E->hasExplicitTemplateArgumentList() && + "Template args list with no args ?"); + Record.push_back(NumTemplateArgs); + if (NumTemplateArgs) { + Writer.AddSourceLocation(E->getLAngleLoc(), Record); + Writer.AddSourceLocation(E->getRAngleLoc(), Record); + for (unsigned i=0; i != NumTemplateArgs; ++i) + Writer.AddTemplateArgumentLoc(E->getTemplateArgs()[i], Record); + } + + DeclAccessPair FoundDecl = E->getFoundDecl(); + Writer.AddDeclRef(FoundDecl.getDecl(), Record); + Record.push_back(FoundDecl.getAccess()); + + Writer.AddTypeRef(E->getType(), Record); + Writer.AddStmt(E->getBase()); Writer.AddDeclRef(E->getMemberDecl(), Record); Writer.AddSourceLocation(E->getMemberLoc(), Record); Record.push_back(E->isArrow()); - // FIXME: C++ nested-name-specifier - // FIXME: C++ template argument list Code = pch::EXPR_MEMBER; } void PCHStmtWriter::VisitObjCIsaExpr(ObjCIsaExpr *E) { VisitExpr(E); - Writer.WriteSubStmt(E->getBase()); + Writer.AddStmt(E->getBase()); Writer.AddSourceLocation(E->getIsaMemberLoc(), Record); Record.push_back(E->isArrow()); Code = pch::EXPR_OBJC_ISA; @@ -496,14 +565,19 @@ void PCHStmtWriter::VisitObjCIsaExpr(ObjCIsaExpr *E) { void PCHStmtWriter::VisitCastExpr(CastExpr *E) { VisitExpr(E); - Writer.WriteSubStmt(E->getSubExpr()); + Writer.AddStmt(E->getSubExpr()); Record.push_back(E->getCastKind()); // FIXME: stable encoding + CXXBaseSpecifierArray &BasePath = E->getBasePath(); + Record.push_back(BasePath.size()); + for (CXXBaseSpecifierArray::iterator I = BasePath.begin(), E = BasePath.end(); + I != E; ++I) + Writer.AddCXXBaseSpecifier(**I, Record); } void PCHStmtWriter::VisitBinaryOperator(BinaryOperator *E) { VisitExpr(E); - Writer.WriteSubStmt(E->getLHS()); - Writer.WriteSubStmt(E->getRHS()); + Writer.AddStmt(E->getLHS()); + Writer.AddStmt(E->getRHS()); Record.push_back(E->getOpcode()); // FIXME: stable encoding Writer.AddSourceLocation(E->getOperatorLoc(), Record); Code = pch::EXPR_BINARY_OPERATOR; @@ -518,9 +592,9 @@ void PCHStmtWriter::VisitCompoundAssignOperator(CompoundAssignOperator *E) { void PCHStmtWriter::VisitConditionalOperator(ConditionalOperator *E) { VisitExpr(E); - Writer.WriteSubStmt(E->getCond()); - Writer.WriteSubStmt(E->getLHS()); - Writer.WriteSubStmt(E->getRHS()); + Writer.AddStmt(E->getCond()); + Writer.AddStmt(E->getLHS()); + Writer.AddStmt(E->getRHS()); Writer.AddSourceLocation(E->getQuestionLoc(), Record); Writer.AddSourceLocation(E->getColonLoc(), Record); Code = pch::EXPR_CONDITIONAL_OPERATOR; @@ -548,14 +622,14 @@ void PCHStmtWriter::VisitCompoundLiteralExpr(CompoundLiteralExpr *E) { VisitExpr(E); Writer.AddSourceLocation(E->getLParenLoc(), Record); Writer.AddTypeSourceInfo(E->getTypeSourceInfo(), Record); - Writer.WriteSubStmt(E->getInitializer()); + Writer.AddStmt(E->getInitializer()); Record.push_back(E->isFileScope()); Code = pch::EXPR_COMPOUND_LITERAL; } void PCHStmtWriter::VisitExtVectorElementExpr(ExtVectorElementExpr *E) { VisitExpr(E); - Writer.WriteSubStmt(E->getBase()); + Writer.AddStmt(E->getBase()); Writer.AddIdentifierRef(&E->getAccessor(), Record); Writer.AddSourceLocation(E->getAccessorLoc(), Record); Code = pch::EXPR_EXT_VECTOR_ELEMENT; @@ -565,8 +639,8 @@ void PCHStmtWriter::VisitInitListExpr(InitListExpr *E) { VisitExpr(E); Record.push_back(E->getNumInits()); for (unsigned I = 0, N = E->getNumInits(); I != N; ++I) - Writer.WriteSubStmt(E->getInit(I)); - Writer.WriteSubStmt(E->getSyntacticForm()); + Writer.AddStmt(E->getInit(I)); + Writer.AddStmt(E->getSyntacticForm()); Writer.AddSourceLocation(E->getLBraceLoc(), Record); Writer.AddSourceLocation(E->getRBraceLoc(), Record); Writer.AddDeclRef(E->getInitializedFieldInUnion(), Record); @@ -578,7 +652,7 @@ void PCHStmtWriter::VisitDesignatedInitExpr(DesignatedInitExpr *E) { VisitExpr(E); Record.push_back(E->getNumSubExprs()); for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I) - Writer.WriteSubStmt(E->getSubExpr(I)); + Writer.AddStmt(E->getSubExpr(I)); Writer.AddSourceLocation(E->getEqualOrColonLoc(), Record); Record.push_back(E->usesGNUSyntax()); for (DesignatedInitExpr::designators_iterator D = E->designators_begin(), @@ -618,7 +692,7 @@ void PCHStmtWriter::VisitImplicitValueInitExpr(ImplicitValueInitExpr *E) { void PCHStmtWriter::VisitVAArgExpr(VAArgExpr *E) { VisitExpr(E); - Writer.WriteSubStmt(E->getSubExpr()); + Writer.AddStmt(E->getSubExpr()); Writer.AddSourceLocation(E->getBuiltinLoc(), Record); Writer.AddSourceLocation(E->getRParenLoc(), Record); Code = pch::EXPR_VA_ARG; @@ -634,7 +708,7 @@ void PCHStmtWriter::VisitAddrLabelExpr(AddrLabelExpr *E) { void PCHStmtWriter::VisitStmtExpr(StmtExpr *E) { VisitExpr(E); - Writer.WriteSubStmt(E->getSubStmt()); + Writer.AddStmt(E->getSubStmt()); Writer.AddSourceLocation(E->getLParenLoc(), Record); Writer.AddSourceLocation(E->getRParenLoc(), Record); Code = pch::EXPR_STMT; @@ -651,9 +725,9 @@ void PCHStmtWriter::VisitTypesCompatibleExpr(TypesCompatibleExpr *E) { void PCHStmtWriter::VisitChooseExpr(ChooseExpr *E) { VisitExpr(E); - Writer.WriteSubStmt(E->getCond()); - Writer.WriteSubStmt(E->getLHS()); - Writer.WriteSubStmt(E->getRHS()); + Writer.AddStmt(E->getCond()); + Writer.AddStmt(E->getLHS()); + Writer.AddStmt(E->getRHS()); Writer.AddSourceLocation(E->getBuiltinLoc(), Record); Writer.AddSourceLocation(E->getRParenLoc(), Record); Code = pch::EXPR_CHOOSE; @@ -669,7 +743,7 @@ void PCHStmtWriter::VisitShuffleVectorExpr(ShuffleVectorExpr *E) { VisitExpr(E); Record.push_back(E->getNumSubExprs()); for (unsigned I = 0, N = E->getNumSubExprs(); I != N; ++I) - Writer.WriteSubStmt(E->getExpr(I)); + Writer.AddStmt(E->getExpr(I)); Writer.AddSourceLocation(E->getBuiltinLoc(), Record); Writer.AddSourceLocation(E->getRParenLoc(), Record); Code = pch::EXPR_SHUFFLE_VECTOR; @@ -688,6 +762,7 @@ void PCHStmtWriter::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) { Writer.AddSourceLocation(E->getLocation(), Record); Record.push_back(E->isByRef()); Record.push_back(E->isConstQualAdded()); + Writer.AddStmt(E->getCopyConstructorExpr()); Code = pch::EXPR_BLOCK_DECL_REF; } @@ -697,7 +772,7 @@ void PCHStmtWriter::VisitBlockDeclRefExpr(BlockDeclRefExpr *E) { void PCHStmtWriter::VisitObjCStringLiteral(ObjCStringLiteral *E) { VisitExpr(E); - Writer.WriteSubStmt(E->getString()); + Writer.AddStmt(E->getString()); Writer.AddSourceLocation(E->getAtLoc(), Record); Code = pch::EXPR_OBJC_STRING_LITERAL; } @@ -730,7 +805,7 @@ void PCHStmtWriter::VisitObjCIvarRefExpr(ObjCIvarRefExpr *E) { VisitExpr(E); Writer.AddDeclRef(E->getDecl(), Record); Writer.AddSourceLocation(E->getLocation(), Record); - Writer.WriteSubStmt(E->getBase()); + Writer.AddStmt(E->getBase()); Record.push_back(E->isArrow()); Record.push_back(E->isFreeIvar()); Code = pch::EXPR_OBJC_IVAR_REF_EXPR; @@ -740,7 +815,7 @@ void PCHStmtWriter::VisitObjCPropertyRefExpr(ObjCPropertyRefExpr *E) { VisitExpr(E); Writer.AddDeclRef(E->getProperty(), Record); Writer.AddSourceLocation(E->getLocation(), Record); - Writer.WriteSubStmt(E->getBase()); + Writer.AddStmt(E->getBase()); Code = pch::EXPR_OBJC_PROPERTY_REF_EXPR; } @@ -752,7 +827,7 @@ void PCHStmtWriter::VisitObjCImplicitSetterGetterRefExpr( // NOTE: InterfaceDecl and Base are mutually exclusive. Writer.AddDeclRef(E->getInterfaceDecl(), Record); - Writer.WriteSubStmt(E->getBase()); + Writer.AddStmt(E->getBase()); Writer.AddSourceLocation(E->getLocation(), Record); Writer.AddSourceLocation(E->getClassLoc(), Record); Code = pch::EXPR_OBJC_KVC_REF_EXPR; @@ -764,7 +839,7 @@ void PCHStmtWriter::VisitObjCMessageExpr(ObjCMessageExpr *E) { Record.push_back((unsigned)E->getReceiverKind()); // FIXME: stable encoding switch (E->getReceiverKind()) { case ObjCMessageExpr::Instance: - Writer.WriteSubStmt(E->getInstanceReceiver()); + Writer.AddStmt(E->getInstanceReceiver()); break; case ObjCMessageExpr::Class: @@ -791,7 +866,7 @@ void PCHStmtWriter::VisitObjCMessageExpr(ObjCMessageExpr *E) { for (CallExpr::arg_iterator Arg = E->arg_begin(), ArgEnd = E->arg_end(); Arg != ArgEnd; ++Arg) - Writer.WriteSubStmt(*Arg); + Writer.AddStmt(*Arg); Code = pch::EXPR_OBJC_MESSAGE_EXPR; } @@ -803,16 +878,16 @@ void PCHStmtWriter::VisitObjCSuperExpr(ObjCSuperExpr *E) { void PCHStmtWriter::VisitObjCForCollectionStmt(ObjCForCollectionStmt *S) { VisitStmt(S); - Writer.WriteSubStmt(S->getElement()); - Writer.WriteSubStmt(S->getCollection()); - Writer.WriteSubStmt(S->getBody()); + Writer.AddStmt(S->getElement()); + Writer.AddStmt(S->getCollection()); + Writer.AddStmt(S->getBody()); Writer.AddSourceLocation(S->getForLoc(), Record); Writer.AddSourceLocation(S->getRParenLoc(), Record); Code = pch::STMT_OBJC_FOR_COLLECTION; } void PCHStmtWriter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) { - Writer.WriteSubStmt(S->getCatchBody()); + Writer.AddStmt(S->getCatchBody()); Writer.AddDeclRef(S->getCatchParamDecl(), Record); Writer.AddSourceLocation(S->getAtCatchLoc(), Record); Writer.AddSourceLocation(S->getRParenLoc(), Record); @@ -820,7 +895,7 @@ void PCHStmtWriter::VisitObjCAtCatchStmt(ObjCAtCatchStmt *S) { } void PCHStmtWriter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) { - Writer.WriteSubStmt(S->getFinallyBody()); + Writer.AddStmt(S->getFinallyBody()); Writer.AddSourceLocation(S->getAtFinallyLoc(), Record); Code = pch::STMT_OBJC_FINALLY; } @@ -828,24 +903,24 @@ void PCHStmtWriter::VisitObjCAtFinallyStmt(ObjCAtFinallyStmt *S) { void PCHStmtWriter::VisitObjCAtTryStmt(ObjCAtTryStmt *S) { Record.push_back(S->getNumCatchStmts()); Record.push_back(S->getFinallyStmt() != 0); - Writer.WriteSubStmt(S->getTryBody()); + Writer.AddStmt(S->getTryBody()); for (unsigned I = 0, N = S->getNumCatchStmts(); I != N; ++I) - Writer.WriteSubStmt(S->getCatchStmt(I)); + Writer.AddStmt(S->getCatchStmt(I)); if (S->getFinallyStmt()) - Writer.WriteSubStmt(S->getFinallyStmt()); + Writer.AddStmt(S->getFinallyStmt()); Writer.AddSourceLocation(S->getAtTryLoc(), Record); Code = pch::STMT_OBJC_AT_TRY; } void PCHStmtWriter::VisitObjCAtSynchronizedStmt(ObjCAtSynchronizedStmt *S) { - Writer.WriteSubStmt(S->getSynchExpr()); - Writer.WriteSubStmt(S->getSynchBody()); + Writer.AddStmt(S->getSynchExpr()); + Writer.AddStmt(S->getSynchBody()); Writer.AddSourceLocation(S->getAtSynchronizedLoc(), Record); Code = pch::STMT_OBJC_AT_SYNCHRONIZED; } void PCHStmtWriter::VisitObjCAtThrowStmt(ObjCAtThrowStmt *S) { - Writer.WriteSubStmt(S->getThrowExpr()); + Writer.AddStmt(S->getThrowExpr()); Writer.AddSourceLocation(S->getThrowLoc(), Record); Code = pch::STMT_OBJC_AT_THROW; } @@ -867,17 +942,24 @@ void PCHStmtWriter::VisitCXXMemberCallExpr(CXXMemberCallExpr *E) { void PCHStmtWriter::VisitCXXConstructExpr(CXXConstructExpr *E) { VisitExpr(E); + Record.push_back(E->getNumArgs()); + for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) + Writer.AddStmt(E->getArg(I)); Writer.AddDeclRef(E->getConstructor(), Record); Writer.AddSourceLocation(E->getLocation(), Record); Record.push_back(E->isElidable()); Record.push_back(E->requiresZeroInitialization()); - Record.push_back(E->getNumArgs()); - for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I) - Writer.WriteSubStmt(E->getArg(I)); Record.push_back(E->getConstructionKind()); // FIXME: stable encoding Code = pch::EXPR_CXX_CONSTRUCT; } +void PCHStmtWriter::VisitCXXTemporaryObjectExpr(CXXTemporaryObjectExpr *E) { + VisitCXXConstructExpr(E); + Writer.AddSourceLocation(E->getTypeBeginLoc(), Record); + Writer.AddSourceLocation(E->getRParenLoc(), Record); + Code = pch::EXPR_CXX_TEMPORARY_OBJECT; +} + void PCHStmtWriter::VisitCXXNamedCastExpr(CXXNamedCastExpr *E) { VisitExplicitCastExpr(E); Writer.AddSourceLocation(E->getOperatorLoc(), Record); @@ -930,7 +1012,7 @@ void PCHStmtWriter::VisitCXXTypeidExpr(CXXTypeidExpr *E) { Writer.AddTypeSourceInfo(E->getTypeOperandSourceInfo(), Record); Code = pch::EXPR_CXX_TYPEID_TYPE; } else { - Writer.WriteSubStmt(E->getExprOperand()); + Writer.AddStmt(E->getExprOperand()); Code = pch::EXPR_CXX_TYPEID_EXPR; } } @@ -945,19 +1027,20 @@ void PCHStmtWriter::VisitCXXThisExpr(CXXThisExpr *E) { void PCHStmtWriter::VisitCXXThrowExpr(CXXThrowExpr *E) { VisitExpr(E); Writer.AddSourceLocation(E->getThrowLoc(), Record); - Writer.WriteSubStmt(E->getSubExpr()); + Writer.AddStmt(E->getSubExpr()); Code = pch::EXPR_CXX_THROW; } void PCHStmtWriter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { VisitExpr(E); + + bool HasOtherExprStored = E->Param.getInt(); + // Store these first, the reader reads them before creation. + Record.push_back(HasOtherExprStored); + if (HasOtherExprStored) + Writer.AddStmt(E->getExpr()); + Writer.AddDeclRef(E->getParam(), Record); Writer.AddSourceLocation(E->getUsedLocation(), Record); - if (E->isExprStored()) { - Record.push_back(1); - Writer.WriteSubStmt(E->getExpr()); - } else { - Record.push_back(0); - } Code = pch::EXPR_CXX_DEFAULT_ARG; } @@ -965,21 +1048,28 @@ void PCHStmtWriter::VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { void PCHStmtWriter::VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) { VisitExpr(E); Writer.AddCXXTemporary(E->getTemporary(), Record); - Writer.WriteSubStmt(E->getSubExpr()); + Writer.AddStmt(E->getSubExpr()); Code = pch::EXPR_CXX_BIND_TEMPORARY; } -void PCHStmtWriter::VisitCXXZeroInitValueExpr(CXXZeroInitValueExpr *E) { +void PCHStmtWriter::VisitCXXBindReferenceExpr(CXXBindReferenceExpr *E) { + VisitExpr(E); + Writer.AddStmt(E->getSubExpr()); + Record.push_back(E->extendsLifetime()); + Record.push_back(E->requiresTemporaryCopy()); + Code = pch::EXPR_CXX_BIND_REFERENCE; +} + +void PCHStmtWriter::VisitCXXScalarValueInitExpr(CXXScalarValueInitExpr *E) { VisitExpr(E); Writer.AddSourceLocation(E->getTypeBeginLoc(), Record); Writer.AddSourceLocation(E->getRParenLoc(), Record); - Code = pch::EXPR_CXX_ZERO_INIT_VALUE; + Code = pch::EXPR_CXX_SCALAR_VALUE_INIT; } void PCHStmtWriter::VisitCXXNewExpr(CXXNewExpr *E) { VisitExpr(E); Record.push_back(E->isGlobalNew()); - Record.push_back(E->isParenTypeId()); Record.push_back(E->hasInitializer()); Record.push_back(E->isArray()); Record.push_back(E->getNumPlacementArgs()); @@ -987,15 +1077,48 @@ void PCHStmtWriter::VisitCXXNewExpr(CXXNewExpr *E) { Writer.AddDeclRef(E->getOperatorNew(), Record); Writer.AddDeclRef(E->getOperatorDelete(), Record); Writer.AddDeclRef(E->getConstructor(), Record); + Writer.AddSourceRange(E->getTypeIdParens(), Record); Writer.AddSourceLocation(E->getStartLoc(), Record); Writer.AddSourceLocation(E->getEndLoc(), Record); for (CXXNewExpr::arg_iterator I = E->raw_arg_begin(), e = E->raw_arg_end(); I != e; ++I) - Writer.WriteSubStmt(*I); + Writer.AddStmt(*I); Code = pch::EXPR_CXX_NEW; } +void PCHStmtWriter::VisitCXXDeleteExpr(CXXDeleteExpr *E) { + VisitExpr(E); + Record.push_back(E->isGlobalDelete()); + Record.push_back(E->isArrayForm()); + Writer.AddDeclRef(E->getOperatorDelete(), Record); + Writer.AddStmt(E->getArgument()); + Writer.AddSourceLocation(E->getSourceRange().getBegin(), Record); + + Code = pch::EXPR_CXX_DELETE; +} + +void PCHStmtWriter::VisitCXXPseudoDestructorExpr(CXXPseudoDestructorExpr *E) { + VisitExpr(E); + + Writer.AddStmt(E->getBase()); + Record.push_back(E->isArrow()); + Writer.AddSourceLocation(E->getOperatorLoc(), Record); + Writer.AddNestedNameSpecifier(E->getQualifier(), Record); + Writer.AddSourceRange(E->getQualifierRange(), Record); + Writer.AddTypeSourceInfo(E->getScopeTypeInfo(), Record); + Writer.AddSourceLocation(E->getColonColonLoc(), Record); + Writer.AddSourceLocation(E->getTildeLoc(), Record); + + // PseudoDestructorTypeStorage. + Writer.AddIdentifierRef(E->getDestroyedTypeIdentifier(), Record); + if (E->getDestroyedTypeIdentifier()) + Writer.AddSourceLocation(E->getDestroyedTypeLoc(), Record); + else + Writer.AddTypeSourceInfo(E->getDestroyedTypeInfo(), Record); + + Code = pch::EXPR_CXX_PSEUDO_DESTRUCTOR; +} void PCHStmtWriter::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) { VisitExpr(E); @@ -1003,10 +1126,132 @@ void PCHStmtWriter::VisitCXXExprWithTemporaries(CXXExprWithTemporaries *E) { for (unsigned i = 0, e = E->getNumTemporaries(); i != e; ++i) Writer.AddCXXTemporary(E->getTemporary(i), Record); - Writer.WriteSubStmt(E->getSubExpr()); + Writer.AddStmt(E->getSubExpr()); Code = pch::EXPR_CXX_EXPR_WITH_TEMPORARIES; } +void +PCHStmtWriter::VisitCXXDependentScopeMemberExpr(CXXDependentScopeMemberExpr *E){ + VisitExpr(E); + + // Don't emit anything here, NumTemplateArgs must be emitted first. + + if (E->hasExplicitTemplateArgs()) { + const ExplicitTemplateArgumentList &Args + = *E->getExplicitTemplateArgumentList(); + assert(Args.NumTemplateArgs && + "Num of template args was zero! PCH reading will mess up!"); + Record.push_back(Args.NumTemplateArgs); + AddExplicitTemplateArgumentList(Args); + } else { + Record.push_back(0); + } + + if (!E->isImplicitAccess()) + Writer.AddStmt(E->getBase()); + else + Writer.AddStmt(0); + Writer.AddTypeRef(E->getBaseType(), Record); + Record.push_back(E->isArrow()); + Writer.AddSourceLocation(E->getOperatorLoc(), Record); + Writer.AddNestedNameSpecifier(E->getQualifier(), Record); + Writer.AddSourceRange(E->getQualifierRange(), Record); + Writer.AddDeclRef(E->getFirstQualifierFoundInScope(), Record); + Writer.AddDeclarationName(E->getMember(), Record); + Writer.AddSourceLocation(E->getMemberLoc(), Record); + Code = pch::EXPR_CXX_DEPENDENT_SCOPE_MEMBER; +} + +void +PCHStmtWriter::VisitDependentScopeDeclRefExpr(DependentScopeDeclRefExpr *E) { + VisitExpr(E); + + // Don't emit anything here, NumTemplateArgs must be emitted first. + + if (E->hasExplicitTemplateArgs()) { + const ExplicitTemplateArgumentList &Args = E->getExplicitTemplateArgs(); + assert(Args.NumTemplateArgs && + "Num of template args was zero! PCH reading will mess up!"); + Record.push_back(Args.NumTemplateArgs); + AddExplicitTemplateArgumentList(Args); + } else { + Record.push_back(0); + } + + Writer.AddDeclarationName(E->getDeclName(), Record); + Writer.AddSourceLocation(E->getLocation(), Record); + Writer.AddSourceRange(E->getQualifierRange(), Record); + Writer.AddNestedNameSpecifier(E->getQualifier(), Record); + Code = pch::EXPR_CXX_DEPENDENT_SCOPE_DECL_REF; +} + +void +PCHStmtWriter::VisitCXXUnresolvedConstructExpr(CXXUnresolvedConstructExpr *E) { + VisitExpr(E); + Record.push_back(E->arg_size()); + for (CXXUnresolvedConstructExpr::arg_iterator + ArgI = E->arg_begin(), ArgE = E->arg_end(); ArgI != ArgE; ++ArgI) + Writer.AddStmt(*ArgI); + Writer.AddSourceLocation(E->getTypeBeginLoc(), Record); + Writer.AddTypeRef(E->getTypeAsWritten(), Record); + Writer.AddSourceLocation(E->getLParenLoc(), Record); + Writer.AddSourceLocation(E->getRParenLoc(), Record); + Code = pch::EXPR_CXX_UNRESOLVED_CONSTRUCT; +} + +void PCHStmtWriter::VisitOverloadExpr(OverloadExpr *E) { + VisitExpr(E); + + // Don't emit anything here, NumTemplateArgs must be emitted first. + + if (E->hasExplicitTemplateArgs()) { + const ExplicitTemplateArgumentList &Args = E->getExplicitTemplateArgs(); + assert(Args.NumTemplateArgs && + "Num of template args was zero! PCH reading will mess up!"); + Record.push_back(Args.NumTemplateArgs); + AddExplicitTemplateArgumentList(Args); + } else { + Record.push_back(0); + } + + Record.push_back(E->getNumDecls()); + for (OverloadExpr::decls_iterator + OvI = E->decls_begin(), OvE = E->decls_end(); OvI != OvE; ++OvI) { + Writer.AddDeclRef(OvI.getDecl(), Record); + Record.push_back(OvI.getAccess()); + } + + Writer.AddDeclarationName(E->getName(), Record); + Writer.AddNestedNameSpecifier(E->getQualifier(), Record); + Writer.AddSourceRange(E->getQualifierRange(), Record); + Writer.AddSourceLocation(E->getNameLoc(), Record); +} + +void PCHStmtWriter::VisitUnresolvedMemberExpr(UnresolvedMemberExpr *E) { + VisitOverloadExpr(E); + Record.push_back(E->isArrow()); + Record.push_back(E->hasUnresolvedUsing()); + Writer.AddStmt(!E->isImplicitAccess() ? E->getBase() : 0); + Writer.AddTypeRef(E->getBaseType(), Record); + Writer.AddSourceLocation(E->getOperatorLoc(), Record); + Code = pch::EXPR_CXX_UNRESOLVED_MEMBER; +} + +void PCHStmtWriter::VisitUnresolvedLookupExpr(UnresolvedLookupExpr *E) { + VisitOverloadExpr(E); + Record.push_back(E->requiresADL()); + Record.push_back(E->isOverloaded()); + Writer.AddDeclRef(E->getNamingClass(), Record); + Code = pch::EXPR_CXX_UNRESOLVED_LOOKUP; +} + +void PCHStmtWriter::VisitUnaryTypeTraitExpr(UnaryTypeTraitExpr *E) { + VisitExpr(E); + Record.push_back(E->getTrait()); + Writer.AddSourceRange(E->getSourceRange(), Record); + Writer.AddTypeRef(E->getQueriedType(), Record); + Code = pch::EXPR_CXX_UNARY_TYPE_TRAIT; +} //===----------------------------------------------------------------------===// // PCHWriter Implementation @@ -1044,21 +1289,38 @@ void PCHWriter::WriteSubStmt(Stmt *S) { RecordData Record; PCHStmtWriter Writer(*this, Record); ++NumStatements; - + if (!S) { Stream.EmitRecord(pch::STMT_NULL_PTR, Record); return; } + // Redirect PCHWriter::AddStmt to collect sub stmts. + llvm::SmallVector<Stmt *, 16> SubStmts; + CollectedStmts = &SubStmts; + Writer.Code = pch::STMT_NULL_PTR; Writer.Visit(S); #ifndef NDEBUG if (Writer.Code == pch::STMT_NULL_PTR) { - S->dump(); + SourceManager &SrcMgr + = DeclIDs.begin()->first->getASTContext().getSourceManager(); + S->dump(SrcMgr); assert(0 && "Unhandled sub statement writing PCH file"); } #endif + + // Revert PCHWriter::AddStmt. + CollectedStmts = &StmtsToEmit; + + // Write the sub stmts in reverse order, last to first. When reading them back + // we will read them in correct order by "pop"ing them from the Stmts stack. + // This simplifies reading and allows to store a variable number of sub stmts + // without knowing it in advance. + while (!SubStmts.empty()) + WriteSubStmt(SubStmts.pop_back_val()); + Stream.EmitRecord(Writer.Code, Record); } @@ -1066,34 +1328,16 @@ void PCHWriter::WriteSubStmt(Stmt *S) { /// queue via AddStmt(). void PCHWriter::FlushStmts() { RecordData Record; - PCHStmtWriter Writer(*this, Record); for (unsigned I = 0, N = StmtsToEmit.size(); I != N; ++I) { - ++NumStatements; - Stmt *S = StmtsToEmit[I]; - - if (!S) { - Stream.EmitRecord(pch::STMT_NULL_PTR, Record); - continue; - } - - Writer.Code = pch::STMT_NULL_PTR; - Writer.Visit(S); -#ifndef NDEBUG - if (Writer.Code == pch::STMT_NULL_PTR) { - S->dump(); - assert(0 && "Unhandled expression writing PCH file"); - } -#endif - Stream.EmitRecord(Writer.Code, Record); - + WriteSubStmt(StmtsToEmit[I]); + assert(N == StmtsToEmit.size() && "Substatement writen via AddStmt rather than WriteSubStmt!"); // Note that we are at the end of a full expression. Any // expression records that follow this one are part of a different // expression. - Record.clear(); Stream.EmitRecord(pch::STMT_STOP, Record); } diff --git a/contrib/llvm/tools/clang/lib/Frontend/PlistDiagnostics.cpp b/contrib/llvm/tools/clang/lib/Frontend/PlistDiagnostics.cpp deleted file mode 100644 index 5706a07..0000000 --- a/contrib/llvm/tools/clang/lib/Frontend/PlistDiagnostics.cpp +++ /dev/null @@ -1,471 +0,0 @@ -//===--- PlistDiagnostics.cpp - Plist Diagnostics for Paths -----*- C++ -*-===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This file defines the PlistDiagnostics object. -// -//===----------------------------------------------------------------------===// - -#include "clang/Frontend/PathDiagnosticClients.h" -#include "clang/Checker/BugReporter/PathDiagnostic.h" -#include "clang/Basic/SourceManager.h" -#include "clang/Basic/FileManager.h" -#include "clang/Lex/Preprocessor.h" -#include "llvm/Support/raw_ostream.h" -#include "llvm/Support/Casting.h" -#include "llvm/ADT/DenseMap.h" -#include "llvm/ADT/SmallVector.h" -using namespace clang; -using llvm::cast; - -typedef llvm::DenseMap<FileID, unsigned> FIDMap; - -namespace clang { - class Preprocessor; -} - -namespace { -struct CompareDiagnostics { - // Compare if 'X' is "<" than 'Y'. - bool operator()(const PathDiagnostic *X, const PathDiagnostic *Y) const { - // First compare by location - const FullSourceLoc &XLoc = X->getLocation().asLocation(); - const FullSourceLoc &YLoc = Y->getLocation().asLocation(); - if (XLoc < YLoc) - return true; - if (XLoc != YLoc) - return false; - - // Next, compare by bug type. - llvm::StringRef XBugType = X->getBugType(); - llvm::StringRef YBugType = Y->getBugType(); - if (XBugType < YBugType) - return true; - if (XBugType != YBugType) - return false; - - // Next, compare by bug description. - llvm::StringRef XDesc = X->getDescription(); - llvm::StringRef YDesc = Y->getDescription(); - if (XDesc < YDesc) - return true; - if (XDesc != YDesc) - return false; - - // FIXME: Further refine by comparing PathDiagnosticPieces? - return false; - } -}; -} - -namespace { - class PlistDiagnostics : public PathDiagnosticClient { - std::vector<const PathDiagnostic*> BatchedDiags; - const std::string OutputFile; - const LangOptions &LangOpts; - llvm::OwningPtr<PathDiagnosticClient> SubPD; - bool flushed; - public: - PlistDiagnostics(const std::string& prefix, const LangOptions &LangOpts, - PathDiagnosticClient *subPD); - - ~PlistDiagnostics() { FlushDiagnostics(NULL); } - - void FlushDiagnostics(llvm::SmallVectorImpl<std::string> *FilesMade); - - void HandlePathDiagnostic(const PathDiagnostic* D); - - virtual llvm::StringRef getName() const { - return "PlistDiagnostics"; - } - - PathGenerationScheme getGenerationScheme() const; - bool supportsLogicalOpControlFlow() const { return true; } - bool supportsAllBlockEdges() const { return true; } - virtual bool useVerboseDescription() const { return false; } - }; -} // end anonymous namespace - -PlistDiagnostics::PlistDiagnostics(const std::string& output, - const LangOptions &LO, - PathDiagnosticClient *subPD) - : OutputFile(output), LangOpts(LO), SubPD(subPD), flushed(false) {} - -PathDiagnosticClient* -clang::CreatePlistDiagnosticClient(const std::string& s, const Preprocessor &PP, - PathDiagnosticClient *subPD) { - return new PlistDiagnostics(s, PP.getLangOptions(), subPD); -} - -PathDiagnosticClient::PathGenerationScheme -PlistDiagnostics::getGenerationScheme() const { - if (const PathDiagnosticClient *PD = SubPD.get()) - return PD->getGenerationScheme(); - - return Extensive; -} - -static void AddFID(FIDMap &FIDs, llvm::SmallVectorImpl<FileID> &V, - const SourceManager* SM, SourceLocation L) { - - FileID FID = SM->getFileID(SM->getInstantiationLoc(L)); - FIDMap::iterator I = FIDs.find(FID); - if (I != FIDs.end()) return; - FIDs[FID] = V.size(); - V.push_back(FID); -} - -static unsigned GetFID(const FIDMap& FIDs, const SourceManager &SM, - SourceLocation L) { - FileID FID = SM.getFileID(SM.getInstantiationLoc(L)); - FIDMap::const_iterator I = FIDs.find(FID); - assert(I != FIDs.end()); - return I->second; -} - -static llvm::raw_ostream& Indent(llvm::raw_ostream& o, const unsigned indent) { - for (unsigned i = 0; i < indent; ++i) o << ' '; - return o; -} - -static void EmitLocation(llvm::raw_ostream& o, const SourceManager &SM, - const LangOptions &LangOpts, - SourceLocation L, const FIDMap &FM, - unsigned indent, bool extend = false) { - - FullSourceLoc Loc(SM.getInstantiationLoc(L), const_cast<SourceManager&>(SM)); - - // Add in the length of the token, so that we cover multi-char tokens. - unsigned offset = - extend ? Lexer::MeasureTokenLength(Loc, SM, LangOpts) - 1 : 0; - - Indent(o, indent) << "<dict>\n"; - Indent(o, indent) << " <key>line</key><integer>" - << Loc.getInstantiationLineNumber() << "</integer>\n"; - Indent(o, indent) << " <key>col</key><integer>" - << Loc.getInstantiationColumnNumber() + offset << "</integer>\n"; - Indent(o, indent) << " <key>file</key><integer>" - << GetFID(FM, SM, Loc) << "</integer>\n"; - Indent(o, indent) << "</dict>\n"; -} - -static void EmitLocation(llvm::raw_ostream& o, const SourceManager &SM, - const LangOptions &LangOpts, - const PathDiagnosticLocation &L, const FIDMap& FM, - unsigned indent, bool extend = false) { - EmitLocation(o, SM, LangOpts, L.asLocation(), FM, indent, extend); -} - -static void EmitRange(llvm::raw_ostream& o, const SourceManager &SM, - const LangOptions &LangOpts, - PathDiagnosticRange R, const FIDMap &FM, - unsigned indent) { - Indent(o, indent) << "<array>\n"; - EmitLocation(o, SM, LangOpts, R.getBegin(), FM, indent+1); - EmitLocation(o, SM, LangOpts, R.getEnd(), FM, indent+1, !R.isPoint); - Indent(o, indent) << "</array>\n"; -} - -static llvm::raw_ostream& EmitString(llvm::raw_ostream& o, - const std::string& s) { - o << "<string>"; - for (std::string::const_iterator I=s.begin(), E=s.end(); I!=E; ++I) { - char c = *I; - switch (c) { - default: o << c; break; - case '&': o << "&"; break; - case '<': o << "<"; break; - case '>': o << ">"; break; - case '\'': o << "'"; break; - case '\"': o << """; break; - } - } - o << "</string>"; - return o; -} - -static void ReportControlFlow(llvm::raw_ostream& o, - const PathDiagnosticControlFlowPiece& P, - const FIDMap& FM, - const SourceManager &SM, - const LangOptions &LangOpts, - unsigned indent) { - - Indent(o, indent) << "<dict>\n"; - ++indent; - - Indent(o, indent) << "<key>kind</key><string>control</string>\n"; - - // Emit edges. - Indent(o, indent) << "<key>edges</key>\n"; - ++indent; - Indent(o, indent) << "<array>\n"; - ++indent; - for (PathDiagnosticControlFlowPiece::const_iterator I=P.begin(), E=P.end(); - I!=E; ++I) { - Indent(o, indent) << "<dict>\n"; - ++indent; - Indent(o, indent) << "<key>start</key>\n"; - EmitRange(o, SM, LangOpts, I->getStart().asRange(), FM, indent+1); - Indent(o, indent) << "<key>end</key>\n"; - EmitRange(o, SM, LangOpts, I->getEnd().asRange(), FM, indent+1); - --indent; - Indent(o, indent) << "</dict>\n"; - } - --indent; - Indent(o, indent) << "</array>\n"; - --indent; - - // Output any helper text. - const std::string& s = P.getString(); - if (!s.empty()) { - Indent(o, indent) << "<key>alternate</key>"; - EmitString(o, s) << '\n'; - } - - --indent; - Indent(o, indent) << "</dict>\n"; -} - -static void ReportEvent(llvm::raw_ostream& o, const PathDiagnosticPiece& P, - const FIDMap& FM, - const SourceManager &SM, - const LangOptions &LangOpts, - unsigned indent) { - - Indent(o, indent) << "<dict>\n"; - ++indent; - - Indent(o, indent) << "<key>kind</key><string>event</string>\n"; - - // Output the location. - FullSourceLoc L = P.getLocation().asLocation(); - - Indent(o, indent) << "<key>location</key>\n"; - EmitLocation(o, SM, LangOpts, L, FM, indent); - - // Output the ranges (if any). - PathDiagnosticPiece::range_iterator RI = P.ranges_begin(), - RE = P.ranges_end(); - - if (RI != RE) { - Indent(o, indent) << "<key>ranges</key>\n"; - Indent(o, indent) << "<array>\n"; - ++indent; - for (; RI != RE; ++RI) - EmitRange(o, SM, LangOpts, *RI, FM, indent+1); - --indent; - Indent(o, indent) << "</array>\n"; - } - - // Output the text. - assert(!P.getString().empty()); - Indent(o, indent) << "<key>extended_message</key>\n"; - Indent(o, indent); - EmitString(o, P.getString()) << '\n'; - - // Output the short text. - // FIXME: Really use a short string. - Indent(o, indent) << "<key>message</key>\n"; - EmitString(o, P.getString()) << '\n'; - - // Finish up. - --indent; - Indent(o, indent); o << "</dict>\n"; -} - -static void ReportMacro(llvm::raw_ostream& o, - const PathDiagnosticMacroPiece& P, - const FIDMap& FM, const SourceManager &SM, - const LangOptions &LangOpts, - unsigned indent) { - - for (PathDiagnosticMacroPiece::const_iterator I=P.begin(), E=P.end(); - I!=E; ++I) { - - switch ((*I)->getKind()) { - default: - break; - case PathDiagnosticPiece::Event: - ReportEvent(o, cast<PathDiagnosticEventPiece>(**I), FM, SM, LangOpts, - indent); - break; - case PathDiagnosticPiece::Macro: - ReportMacro(o, cast<PathDiagnosticMacroPiece>(**I), FM, SM, LangOpts, - indent); - break; - } - } -} - -static void ReportDiag(llvm::raw_ostream& o, const PathDiagnosticPiece& P, - const FIDMap& FM, const SourceManager &SM, - const LangOptions &LangOpts) { - - unsigned indent = 4; - - switch (P.getKind()) { - case PathDiagnosticPiece::ControlFlow: - ReportControlFlow(o, cast<PathDiagnosticControlFlowPiece>(P), FM, SM, - LangOpts, indent); - break; - case PathDiagnosticPiece::Event: - ReportEvent(o, cast<PathDiagnosticEventPiece>(P), FM, SM, LangOpts, - indent); - break; - case PathDiagnosticPiece::Macro: - ReportMacro(o, cast<PathDiagnosticMacroPiece>(P), FM, SM, LangOpts, - indent); - break; - } -} - -void PlistDiagnostics::HandlePathDiagnostic(const PathDiagnostic* D) { - if (!D) - return; - - if (D->empty()) { - delete D; - return; - } - - // We need to flatten the locations (convert Stmt* to locations) because - // the referenced statements may be freed by the time the diagnostics - // are emitted. - const_cast<PathDiagnostic*>(D)->flattenLocations(); - BatchedDiags.push_back(D); -} - -void PlistDiagnostics::FlushDiagnostics(llvm::SmallVectorImpl<std::string> - *FilesMade) { - - if (flushed) - return; - - flushed = true; - - // Sort the diagnostics so that they are always emitted in a deterministic - // order. - if (!BatchedDiags.empty()) - std::sort(BatchedDiags.begin(), BatchedDiags.end(), CompareDiagnostics()); - - // Build up a set of FIDs that we use by scanning the locations and - // ranges of the diagnostics. - FIDMap FM; - llvm::SmallVector<FileID, 10> Fids; - const SourceManager* SM = 0; - - if (!BatchedDiags.empty()) - SM = &(*BatchedDiags.begin())->begin()->getLocation().getManager(); - - for (std::vector<const PathDiagnostic*>::iterator DI = BatchedDiags.begin(), - DE = BatchedDiags.end(); DI != DE; ++DI) { - - const PathDiagnostic *D = *DI; - - for (PathDiagnostic::const_iterator I=D->begin(), E=D->end(); I!=E; ++I) { - AddFID(FM, Fids, SM, I->getLocation().asLocation()); - - for (PathDiagnosticPiece::range_iterator RI=I->ranges_begin(), - RE=I->ranges_end(); RI!=RE; ++RI) { - AddFID(FM, Fids, SM, RI->getBegin()); - AddFID(FM, Fids, SM, RI->getEnd()); - } - } - } - - // Open the file. - std::string ErrMsg; - llvm::raw_fd_ostream o(OutputFile.c_str(), ErrMsg); - if (!ErrMsg.empty()) { - llvm::errs() << "warning: could not creat file: " << OutputFile << '\n'; - return; - } - - // Write the plist header. - o << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" - "<!DOCTYPE plist PUBLIC \"-//Apple Computer//DTD PLIST 1.0//EN\" " - "\"http://www.apple.com/DTDs/PropertyList-1.0.dtd\">\n" - "<plist version=\"1.0\">\n"; - - // Write the root object: a <dict> containing... - // - "files", an <array> mapping from FIDs to file names - // - "diagnostics", an <array> containing the path diagnostics - o << "<dict>\n" - " <key>files</key>\n" - " <array>\n"; - - for (llvm::SmallVectorImpl<FileID>::iterator I=Fids.begin(), E=Fids.end(); - I!=E; ++I) { - o << " "; - EmitString(o, SM->getFileEntryForID(*I)->getName()) << '\n'; - } - - o << " </array>\n" - " <key>diagnostics</key>\n" - " <array>\n"; - - for (std::vector<const PathDiagnostic*>::iterator DI=BatchedDiags.begin(), - DE = BatchedDiags.end(); DI!=DE; ++DI) { - - o << " <dict>\n" - " <key>path</key>\n"; - - const PathDiagnostic *D = *DI; - // Create an owning smart pointer for 'D' just so that we auto-free it - // when we exit this method. - llvm::OwningPtr<PathDiagnostic> OwnedD(const_cast<PathDiagnostic*>(D)); - - o << " <array>\n"; - - for (PathDiagnostic::const_iterator I=D->begin(), E=D->end(); I != E; ++I) - ReportDiag(o, *I, FM, *SM, LangOpts); - - o << " </array>\n"; - - // Output the bug type and bug category. - o << " <key>description</key>"; - EmitString(o, D->getDescription()) << '\n'; - o << " <key>category</key>"; - EmitString(o, D->getCategory()) << '\n'; - o << " <key>type</key>"; - EmitString(o, D->getBugType()) << '\n'; - - // Output the location of the bug. - o << " <key>location</key>\n"; - EmitLocation(o, *SM, LangOpts, D->getLocation(), FM, 2); - - // Output the diagnostic to the sub-diagnostic client, if any. - if (SubPD) { - SubPD->HandlePathDiagnostic(OwnedD.take()); - llvm::SmallVector<std::string, 1> SubFilesMade; - SubPD->FlushDiagnostics(SubFilesMade); - - if (!SubFilesMade.empty()) { - o << " <key>" << SubPD->getName() << "_files</key>\n"; - o << " <array>\n"; - for (size_t i = 0, n = SubFilesMade.size(); i < n ; ++i) - o << " <string>" << SubFilesMade[i] << "</string>\n"; - o << " </array>\n"; - } - } - - // Close up the entry. - o << " </dict>\n"; - } - - o << " </array>\n"; - - // Finish. - o << "</dict>\n</plist>"; - - if (FilesMade) - FilesMade->push_back(OutputFile); - - BatchedDiags.clear(); -} diff --git a/contrib/llvm/tools/clang/lib/Frontend/PrintParserCallbacks.cpp b/contrib/llvm/tools/clang/lib/Frontend/PrintParserCallbacks.cpp index b032233..9220677 100644 --- a/contrib/llvm/tools/clang/lib/Frontend/PrintParserCallbacks.cpp +++ b/contrib/llvm/tools/clang/lib/Frontend/PrintParserCallbacks.cpp @@ -819,7 +819,8 @@ namespace { SourceLocation PlacementLParen, MultiExprArg PlacementArgs, SourceLocation PlacementRParen, - bool ParenTypeId, Declarator &D, + SourceRange TypeIdParens, + Declarator &D, SourceLocation ConstructorLParen, MultiExprArg ConstructorArgs, SourceLocation ConstructorRParen) { diff --git a/contrib/llvm/tools/clang/lib/Frontend/PrintPreprocessedOutput.cpp b/contrib/llvm/tools/clang/lib/Frontend/PrintPreprocessedOutput.cpp index b6c18b7..73bca9a 100644 --- a/contrib/llvm/tools/clang/lib/Frontend/PrintPreprocessedOutput.cpp +++ b/contrib/llvm/tools/clang/lib/Frontend/PrintPreprocessedOutput.cpp @@ -23,6 +23,7 @@ #include "clang/Lex/TokenConcatenation.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/STLExtras.h" +#include "llvm/ADT/StringRef.h" #include "llvm/Config/config.h" #include "llvm/Support/raw_ostream.h" #include <cstdio> @@ -117,7 +118,7 @@ public: virtual void Ident(SourceLocation Loc, const std::string &str); virtual void PragmaComment(SourceLocation Loc, const IdentifierInfo *Kind, const std::string &Str); - + virtual void PragmaMessage(SourceLocation Loc, llvm::StringRef Str); bool HandleFirstTokOnLine(Token &Tok); bool MoveToLine(SourceLocation Loc) { @@ -174,20 +175,6 @@ void PrintPPOutputPPCallbacks::WriteLineInfo(unsigned LineNo, /// #line directive. This returns false if already at the specified line, true /// if some newlines were emitted. bool PrintPPOutputPPCallbacks::MoveToLine(unsigned LineNo) { - if (DisableLineMarkers) { - if (LineNo == CurLine) return false; - - CurLine = LineNo; - - if (!EmittedTokensOnThisLine && !EmittedMacroOnThisLine) - return true; - - OS << '\n'; - EmittedTokensOnThisLine = false; - EmittedMacroOnThisLine = false; - return true; - } - // If this line is "close enough" to the original line, just print newlines, // otherwise print a #line directive. if (LineNo-CurLine <= 8) { @@ -199,8 +186,17 @@ bool PrintPPOutputPPCallbacks::MoveToLine(unsigned LineNo) { const char *NewLines = "\n\n\n\n\n\n\n\n"; OS.write(NewLines, LineNo-CurLine); } - } else { + } else if (!DisableLineMarkers) { + // Emit a #line or line marker. WriteLineInfo(LineNo, 0, 0); + } else { + // Okay, we're in -P mode, which turns off line markers. However, we still + // need to emit a newline between tokens on different lines. + if (EmittedTokensOnThisLine || EmittedMacroOnThisLine) { + OS << '\n'; + EmittedTokensOnThisLine = false; + EmittedMacroOnThisLine = false; + } } CurLine = LineNo; @@ -311,6 +307,29 @@ void PrintPPOutputPPCallbacks::PragmaComment(SourceLocation Loc, EmittedTokensOnThisLine = true; } +void PrintPPOutputPPCallbacks::PragmaMessage(SourceLocation Loc, + llvm::StringRef Str) { + MoveToLine(Loc); + OS << "#pragma message("; + + OS << '"'; + + for (unsigned i = 0, e = Str.size(); i != e; ++i) { + unsigned char Char = Str[i]; + if (isprint(Char) && Char != '\\' && Char != '"') + OS << (char)Char; + else // Output anything hard as an octal escape. + OS << '\\' + << (char)('0'+ ((Char >> 6) & 7)) + << (char)('0'+ ((Char >> 3) & 7)) + << (char)('0'+ ((Char >> 0) & 7)); + } + OS << '"'; + + OS << ')'; + EmittedTokensOnThisLine = true; +} + /// HandleFirstTokOnLine - When emitting a preprocessed file in -E mode, this /// is called for the first token on each new line. If this really is the start @@ -372,7 +391,7 @@ struct UnknownPragmaHandler : public PragmaHandler { PrintPPOutputPPCallbacks *Callbacks; UnknownPragmaHandler(const char *prefix, PrintPPOutputPPCallbacks *callbacks) - : PragmaHandler(0), Prefix(prefix), Callbacks(callbacks) {} + : Prefix(prefix), Callbacks(callbacks) {} virtual void HandlePragma(Preprocessor &PP, Token &PragmaTok) { // Figure out what line we went to and insert the appropriate number of // newline characters. @@ -397,8 +416,9 @@ static void PrintPreprocessedTokens(Preprocessor &PP, Token &Tok, PrintPPOutputPPCallbacks *Callbacks, llvm::raw_ostream &OS) { char Buffer[256]; - Token PrevPrevTok; - Token PrevTok; + Token PrevPrevTok, PrevTok; + PrevPrevTok.startToken(); + PrevTok.startToken(); while (1) { // If this token is at the start of a line, emit newlines if needed. @@ -454,6 +474,9 @@ static int MacroIDCompare(const void* a, const void* b) { } static void DoPrintMacros(Preprocessor &PP, llvm::raw_ostream *OS) { + // Ignore unknown pragmas. + PP.AddPragmaHandler(new EmptyPragmaHandler()); + // -dM mode just scans and ignores all tokens in the files, then dumps out // the macro table at the end. PP.EnterMainSourceFile(); @@ -494,7 +517,7 @@ void clang::DoPrintPreprocessedInput(Preprocessor &PP, llvm::raw_ostream *OS, PrintPPOutputPPCallbacks *Callbacks = new PrintPPOutputPPCallbacks(PP, *OS, !Opts.ShowLineMarkers, Opts.ShowMacros); - PP.AddPragmaHandler(0, new UnknownPragmaHandler("#pragma", Callbacks)); + PP.AddPragmaHandler(new UnknownPragmaHandler("#pragma", Callbacks)); PP.AddPragmaHandler("GCC", new UnknownPragmaHandler("#pragma GCC", Callbacks)); diff --git a/contrib/llvm/tools/clang/lib/Frontend/RewriteMacros.cpp b/contrib/llvm/tools/clang/lib/Frontend/RewriteMacros.cpp deleted file mode 100644 index 954e8e2..0000000 --- a/contrib/llvm/tools/clang/lib/Frontend/RewriteMacros.cpp +++ /dev/null @@ -1,217 +0,0 @@ -//===--- RewriteMacros.cpp - Rewrite macros into their expansions ---------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This code rewrites macro invocations into their expansions. This gives you -// a macro expanded file that retains comments and #includes. -// -//===----------------------------------------------------------------------===// - -#include "clang/Frontend/Utils.h" -#include "clang/Rewrite/Rewriter.h" -#include "clang/Lex/Preprocessor.h" -#include "clang/Basic/SourceManager.h" -#include "llvm/Support/raw_ostream.h" -#include "llvm/System/Path.h" -#include "llvm/ADT/OwningPtr.h" -#include <cstdio> - -using namespace clang; - -/// isSameToken - Return true if the two specified tokens start have the same -/// content. -static bool isSameToken(Token &RawTok, Token &PPTok) { - // If two tokens have the same kind and the same identifier info, they are - // obviously the same. - if (PPTok.getKind() == RawTok.getKind() && - PPTok.getIdentifierInfo() == RawTok.getIdentifierInfo()) - return true; - - // Otherwise, if they are different but have the same identifier info, they - // are also considered to be the same. This allows keywords and raw lexed - // identifiers with the same name to be treated the same. - if (PPTok.getIdentifierInfo() && - PPTok.getIdentifierInfo() == RawTok.getIdentifierInfo()) - return true; - - return false; -} - - -/// GetNextRawTok - Return the next raw token in the stream, skipping over -/// comments if ReturnComment is false. -static const Token &GetNextRawTok(const std::vector<Token> &RawTokens, - unsigned &CurTok, bool ReturnComment) { - assert(CurTok < RawTokens.size() && "Overran eof!"); - - // If the client doesn't want comments and we have one, skip it. - if (!ReturnComment && RawTokens[CurTok].is(tok::comment)) - ++CurTok; - - return RawTokens[CurTok++]; -} - - -/// LexRawTokensFromMainFile - Lets all the raw tokens from the main file into -/// the specified vector. -static void LexRawTokensFromMainFile(Preprocessor &PP, - std::vector<Token> &RawTokens) { - SourceManager &SM = PP.getSourceManager(); - - // Create a lexer to lex all the tokens of the main file in raw mode. Even - // though it is in raw mode, it will not return comments. - const llvm::MemoryBuffer *FromFile = SM.getBuffer(SM.getMainFileID()); - Lexer RawLex(SM.getMainFileID(), FromFile, SM, PP.getLangOptions()); - - // Switch on comment lexing because we really do want them. - RawLex.SetCommentRetentionState(true); - - Token RawTok; - do { - RawLex.LexFromRawLexer(RawTok); - - // If we have an identifier with no identifier info for our raw token, look - // up the indentifier info. This is important for equality comparison of - // identifier tokens. - if (RawTok.is(tok::identifier) && !RawTok.getIdentifierInfo()) - PP.LookUpIdentifierInfo(RawTok); - - RawTokens.push_back(RawTok); - } while (RawTok.isNot(tok::eof)); -} - - -/// RewriteMacrosInInput - Implement -rewrite-macros mode. -void clang::RewriteMacrosInInput(Preprocessor &PP, llvm::raw_ostream *OS) { - SourceManager &SM = PP.getSourceManager(); - - Rewriter Rewrite; - Rewrite.setSourceMgr(SM, PP.getLangOptions()); - RewriteBuffer &RB = Rewrite.getEditBuffer(SM.getMainFileID()); - - std::vector<Token> RawTokens; - LexRawTokensFromMainFile(PP, RawTokens); - unsigned CurRawTok = 0; - Token RawTok = GetNextRawTok(RawTokens, CurRawTok, false); - - - // Get the first preprocessing token. - PP.EnterMainSourceFile(); - Token PPTok; - PP.Lex(PPTok); - - // Preprocess the input file in parallel with raw lexing the main file. Ignore - // all tokens that are preprocessed from a file other than the main file (e.g. - // a header). If we see tokens that are in the preprocessed file but not the - // lexed file, we have a macro expansion. If we see tokens in the lexed file - // that aren't in the preprocessed view, we have macros that expand to no - // tokens, or macro arguments etc. - while (RawTok.isNot(tok::eof) || PPTok.isNot(tok::eof)) { - SourceLocation PPLoc = SM.getInstantiationLoc(PPTok.getLocation()); - - // If PPTok is from a different source file, ignore it. - if (!SM.isFromMainFile(PPLoc)) { - PP.Lex(PPTok); - continue; - } - - // If the raw file hits a preprocessor directive, they will be extra tokens - // in the raw file that don't exist in the preprocsesed file. However, we - // choose to preserve them in the output file and otherwise handle them - // specially. - if (RawTok.is(tok::hash) && RawTok.isAtStartOfLine()) { - // If this is a #warning directive or #pragma mark (GNU extensions), - // comment the line out. - if (RawTokens[CurRawTok].is(tok::identifier)) { - const IdentifierInfo *II = RawTokens[CurRawTok].getIdentifierInfo(); - if (II->getName() == "warning") { - // Comment out #warning. - RB.InsertTextAfter(SM.getFileOffset(RawTok.getLocation()), "//"); - } else if (II->getName() == "pragma" && - RawTokens[CurRawTok+1].is(tok::identifier) && - (RawTokens[CurRawTok+1].getIdentifierInfo()->getName() == - "mark")) { - // Comment out #pragma mark. - RB.InsertTextAfter(SM.getFileOffset(RawTok.getLocation()), "//"); - } - } - - // Otherwise, if this is a #include or some other directive, just leave it - // in the file by skipping over the line. - RawTok = GetNextRawTok(RawTokens, CurRawTok, false); - while (!RawTok.isAtStartOfLine() && RawTok.isNot(tok::eof)) - RawTok = GetNextRawTok(RawTokens, CurRawTok, false); - continue; - } - - // Okay, both tokens are from the same file. Get their offsets from the - // start of the file. - unsigned PPOffs = SM.getFileOffset(PPLoc); - unsigned RawOffs = SM.getFileOffset(RawTok.getLocation()); - - // If the offsets are the same and the token kind is the same, ignore them. - if (PPOffs == RawOffs && isSameToken(RawTok, PPTok)) { - RawTok = GetNextRawTok(RawTokens, CurRawTok, false); - PP.Lex(PPTok); - continue; - } - - // If the PP token is farther along than the raw token, something was - // deleted. Comment out the raw token. - if (RawOffs <= PPOffs) { - // Comment out a whole run of tokens instead of bracketing each one with - // comments. Add a leading space if RawTok didn't have one. - bool HasSpace = RawTok.hasLeadingSpace(); - RB.InsertTextAfter(RawOffs, " /*"+HasSpace); - unsigned EndPos; - - do { - EndPos = RawOffs+RawTok.getLength(); - - RawTok = GetNextRawTok(RawTokens, CurRawTok, true); - RawOffs = SM.getFileOffset(RawTok.getLocation()); - - if (RawTok.is(tok::comment)) { - // Skip past the comment. - RawTok = GetNextRawTok(RawTokens, CurRawTok, false); - break; - } - - } while (RawOffs <= PPOffs && !RawTok.isAtStartOfLine() && - (PPOffs != RawOffs || !isSameToken(RawTok, PPTok))); - - RB.InsertTextBefore(EndPos, "*/"); - continue; - } - - // Otherwise, there was a replacement an expansion. Insert the new token - // in the output buffer. Insert the whole run of new tokens at once to get - // them in the right order. - unsigned InsertPos = PPOffs; - std::string Expansion; - while (PPOffs < RawOffs) { - Expansion += ' ' + PP.getSpelling(PPTok); - PP.Lex(PPTok); - PPLoc = SM.getInstantiationLoc(PPTok.getLocation()); - PPOffs = SM.getFileOffset(PPLoc); - } - Expansion += ' '; - RB.InsertTextBefore(InsertPos, Expansion); - } - - // Get the buffer corresponding to MainFileID. If we haven't changed it, then - // we are done. - if (const RewriteBuffer *RewriteBuf = - Rewrite.getRewriteBufferFor(SM.getMainFileID())) { - //printf("Changed:\n"); - *OS << std::string(RewriteBuf->begin(), RewriteBuf->end()); - } else { - fprintf(stderr, "No changes\n"); - } - OS->flush(); -} diff --git a/contrib/llvm/tools/clang/lib/Frontend/RewriteObjC.cpp b/contrib/llvm/tools/clang/lib/Frontend/RewriteObjC.cpp deleted file mode 100644 index 5dd7bdf..0000000 --- a/contrib/llvm/tools/clang/lib/Frontend/RewriteObjC.cpp +++ /dev/null @@ -1,5778 +0,0 @@ -//===--- RewriteObjC.cpp - Playground for the code rewriter ---------------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// Hacks and fun related to the code rewriter. -// -//===----------------------------------------------------------------------===// - -#include "clang/Frontend/ASTConsumers.h" -#include "clang/Rewrite/Rewriter.h" -#include "clang/AST/AST.h" -#include "clang/AST/ASTConsumer.h" -#include "clang/AST/ParentMap.h" -#include "clang/Basic/SourceManager.h" -#include "clang/Basic/IdentifierTable.h" -#include "clang/Basic/Diagnostic.h" -#include "clang/Lex/Lexer.h" -#include "llvm/Support/MemoryBuffer.h" -#include "llvm/Support/raw_ostream.h" -#include "llvm/ADT/StringExtras.h" -#include "llvm/ADT/SmallPtrSet.h" -#include "llvm/ADT/OwningPtr.h" -#include "llvm/ADT/DenseSet.h" - -using namespace clang; -using llvm::utostr; - -namespace { - class RewriteObjC : public ASTConsumer { - enum { - BLOCK_FIELD_IS_OBJECT = 3, /* id, NSObject, __attribute__((NSObject)), - block, ... */ - BLOCK_FIELD_IS_BLOCK = 7, /* a block variable */ - BLOCK_FIELD_IS_BYREF = 8, /* the on stack structure holding the - __block variable */ - BLOCK_FIELD_IS_WEAK = 16, /* declared __weak, only used in byref copy - helpers */ - BLOCK_BYREF_CALLER = 128, /* called from __block (byref) copy/dispose - support routines */ - BLOCK_BYREF_CURRENT_MAX = 256 - }; - - enum { - BLOCK_NEEDS_FREE = (1 << 24), - BLOCK_HAS_COPY_DISPOSE = (1 << 25), - BLOCK_HAS_CXX_OBJ = (1 << 26), - BLOCK_IS_GC = (1 << 27), - BLOCK_IS_GLOBAL = (1 << 28), - BLOCK_HAS_DESCRIPTOR = (1 << 29) - }; - - Rewriter Rewrite; - Diagnostic &Diags; - const LangOptions &LangOpts; - unsigned RewriteFailedDiag; - unsigned TryFinallyContainsReturnDiag; - - ASTContext *Context; - SourceManager *SM; - TranslationUnitDecl *TUDecl; - FileID MainFileID; - const char *MainFileStart, *MainFileEnd; - SourceLocation LastIncLoc; - - llvm::SmallVector<ObjCImplementationDecl *, 8> ClassImplementation; - llvm::SmallVector<ObjCCategoryImplDecl *, 8> CategoryImplementation; - llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCSynthesizedStructs; - llvm::SmallPtrSet<ObjCProtocolDecl*, 8> ObjCSynthesizedProtocols; - llvm::SmallPtrSet<ObjCInterfaceDecl*, 8> ObjCForwardDecls; - llvm::DenseMap<ObjCMethodDecl*, std::string> MethodInternalNames; - llvm::SmallVector<Stmt *, 32> Stmts; - llvm::SmallVector<int, 8> ObjCBcLabelNo; - // Remember all the @protocol(<expr>) expressions. - llvm::SmallPtrSet<ObjCProtocolDecl *, 32> ProtocolExprDecls; - - llvm::DenseSet<uint64_t> CopyDestroyCache; - - unsigned NumObjCStringLiterals; - - FunctionDecl *MsgSendFunctionDecl; - FunctionDecl *MsgSendSuperFunctionDecl; - FunctionDecl *MsgSendStretFunctionDecl; - FunctionDecl *MsgSendSuperStretFunctionDecl; - FunctionDecl *MsgSendFpretFunctionDecl; - FunctionDecl *GetClassFunctionDecl; - FunctionDecl *GetMetaClassFunctionDecl; - FunctionDecl *GetSuperClassFunctionDecl; - FunctionDecl *SelGetUidFunctionDecl; - FunctionDecl *CFStringFunctionDecl; - FunctionDecl *SuperContructorFunctionDecl; - - // ObjC string constant support. - VarDecl *ConstantStringClassReference; - RecordDecl *NSStringRecord; - - // ObjC foreach break/continue generation support. - int BcLabelCount; - - // Needed for super. - ObjCMethodDecl *CurMethodDef; - RecordDecl *SuperStructDecl; - RecordDecl *ConstantStringDecl; - - TypeDecl *ProtocolTypeDecl; - QualType getProtocolType(); - - // Needed for header files being rewritten - bool IsHeader; - - std::string InFileName; - llvm::raw_ostream* OutFile; - - bool SilenceRewriteMacroWarning; - bool objc_impl_method; - - std::string Preamble; - - // Block expressions. - llvm::SmallVector<BlockExpr *, 32> Blocks; - llvm::SmallVector<int, 32> InnerDeclRefsCount; - llvm::SmallVector<BlockDeclRefExpr *, 32> InnerDeclRefs; - - llvm::SmallVector<BlockDeclRefExpr *, 32> BlockDeclRefs; - - // Block related declarations. - llvm::SmallVector<ValueDecl *, 8> BlockByCopyDecls; - llvm::SmallPtrSet<ValueDecl *, 8> BlockByCopyDeclsPtrSet; - llvm::SmallVector<ValueDecl *, 8> BlockByRefDecls; - llvm::SmallPtrSet<ValueDecl *, 8> BlockByRefDeclsPtrSet; - llvm::DenseMap<ValueDecl *, unsigned> BlockByRefDeclNo; - llvm::SmallPtrSet<ValueDecl *, 8> ImportedBlockDecls; - llvm::SmallPtrSet<VarDecl *, 8> ImportedLocalExternalDecls; - - llvm::DenseMap<BlockExpr *, std::string> RewrittenBlockExprs; - - // This maps a property to it's assignment statement. - llvm::DenseMap<ObjCPropertyRefExpr *, BinaryOperator *> PropSetters; - // This maps a property to it's synthesied message expression. - // This allows us to rewrite chained getters (e.g. o.a.b.c). - llvm::DenseMap<ObjCPropertyRefExpr *, Stmt *> PropGetters; - - // This maps an original source AST to it's rewritten form. This allows - // us to avoid rewriting the same node twice (which is very uncommon). - // This is needed to support some of the exotic property rewriting. - llvm::DenseMap<Stmt *, Stmt *> ReplacedNodes; - - FunctionDecl *CurFunctionDef; - FunctionDecl *CurFunctionDeclToDeclareForBlock; - VarDecl *GlobalVarDecl; - - bool DisableReplaceStmt; - - static const int OBJC_ABI_VERSION =7 ; - public: - virtual void Initialize(ASTContext &context); - - // Top Level Driver code. - virtual void HandleTopLevelDecl(DeclGroupRef D) { - for (DeclGroupRef::iterator I = D.begin(), E = D.end(); I != E; ++I) - HandleTopLevelSingleDecl(*I); - } - void HandleTopLevelSingleDecl(Decl *D); - void HandleDeclInMainFile(Decl *D); - RewriteObjC(std::string inFile, llvm::raw_ostream *OS, - Diagnostic &D, const LangOptions &LOpts, - bool silenceMacroWarn); - - ~RewriteObjC() {} - - virtual void HandleTranslationUnit(ASTContext &C); - - void ReplaceStmt(Stmt *Old, Stmt *New) { - Stmt *ReplacingStmt = ReplacedNodes[Old]; - - if (ReplacingStmt) - return; // We can't rewrite the same node twice. - - if (DisableReplaceStmt) - return; // Used when rewriting the assignment of a property setter. - - // If replacement succeeded or warning disabled return with no warning. - if (!Rewrite.ReplaceStmt(Old, New)) { - ReplacedNodes[Old] = New; - return; - } - if (SilenceRewriteMacroWarning) - return; - Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) - << Old->getSourceRange(); - } - - void ReplaceStmtWithRange(Stmt *Old, Stmt *New, SourceRange SrcRange) { - // Measaure the old text. - int Size = Rewrite.getRangeSize(SrcRange); - if (Size == -1) { - Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) - << Old->getSourceRange(); - return; - } - // Get the new text. - std::string SStr; - llvm::raw_string_ostream S(SStr); - New->printPretty(S, *Context, 0, PrintingPolicy(LangOpts)); - const std::string &Str = S.str(); - - // If replacement succeeded or warning disabled return with no warning. - if (!Rewrite.ReplaceText(SrcRange.getBegin(), Size, Str)) { - ReplacedNodes[Old] = New; - return; - } - if (SilenceRewriteMacroWarning) - return; - Diags.Report(Context->getFullLoc(Old->getLocStart()), RewriteFailedDiag) - << Old->getSourceRange(); - } - - void InsertText(SourceLocation Loc, llvm::StringRef Str, - bool InsertAfter = true) { - // If insertion succeeded or warning disabled return with no warning. - if (!Rewrite.InsertText(Loc, Str, InsertAfter) || - SilenceRewriteMacroWarning) - return; - - Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag); - } - - void RemoveText(SourceLocation Loc, unsigned StrLen) { - // If removal succeeded or warning disabled return with no warning. - if (!Rewrite.RemoveText(Loc, StrLen) || SilenceRewriteMacroWarning) - return; - - Diags.Report(Context->getFullLoc(Loc), RewriteFailedDiag); - } - - void ReplaceText(SourceLocation Start, unsigned OrigLength, - llvm::StringRef Str) { - // If removal succeeded or warning disabled return with no warning. - if (!Rewrite.ReplaceText(Start, OrigLength, Str) || - SilenceRewriteMacroWarning) - return; - - Diags.Report(Context->getFullLoc(Start), RewriteFailedDiag); - } - - // Syntactic Rewriting. - void RewritePrologue(SourceLocation Loc); - void RewriteInclude(); - void RewriteTabs(); - void RewriteForwardClassDecl(ObjCClassDecl *Dcl); - void RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, - ObjCImplementationDecl *IMD, - ObjCCategoryImplDecl *CID); - void RewriteInterfaceDecl(ObjCInterfaceDecl *Dcl); - void RewriteImplementationDecl(Decl *Dcl); - void RewriteObjCMethodDecl(ObjCMethodDecl *MDecl, std::string &ResultStr); - void RewriteTypeIntoString(QualType T, std::string &ResultStr, - const FunctionType *&FPRetType); - void RewriteByRefString(std::string &ResultStr, const std::string &Name, - ValueDecl *VD); - void RewriteCategoryDecl(ObjCCategoryDecl *Dcl); - void RewriteProtocolDecl(ObjCProtocolDecl *Dcl); - void RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *Dcl); - void RewriteMethodDeclaration(ObjCMethodDecl *Method); - void RewriteProperty(ObjCPropertyDecl *prop); - void RewriteFunctionDecl(FunctionDecl *FD); - void RewriteBlockLiteralFunctionDecl(FunctionDecl *FD); - void RewriteObjCQualifiedInterfaceTypes(Decl *Dcl); - void RewriteTypeOfDecl(VarDecl *VD); - void RewriteObjCQualifiedInterfaceTypes(Expr *E); - bool needToScanForQualifiers(QualType T); - bool isSuperReceiver(Expr *recExpr); - QualType getSuperStructType(); - QualType getConstantStringStructType(); - QualType convertFunctionTypeOfBlocks(const FunctionType *FT); - bool BufferContainsPPDirectives(const char *startBuf, const char *endBuf); - - // Expression Rewriting. - Stmt *RewriteFunctionBodyOrGlobalInitializer(Stmt *S); - void CollectPropertySetters(Stmt *S); - - Stmt *CurrentBody; - ParentMap *PropParentMap; // created lazily. - - Stmt *RewriteAtEncode(ObjCEncodeExpr *Exp); - Stmt *RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV, SourceLocation OrigStart, - bool &replaced); - Stmt *RewriteObjCNestedIvarRefExpr(Stmt *S, bool &replaced); - Stmt *RewritePropertyGetter(ObjCPropertyRefExpr *PropRefExpr); - Stmt *RewritePropertySetter(BinaryOperator *BinOp, Expr *newStmt, - SourceRange SrcRange); - Stmt *RewriteAtSelector(ObjCSelectorExpr *Exp); - Stmt *RewriteMessageExpr(ObjCMessageExpr *Exp); - Stmt *RewriteObjCStringLiteral(ObjCStringLiteral *Exp); - Stmt *RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp); - void WarnAboutReturnGotoStmts(Stmt *S); - void HasReturnStmts(Stmt *S, bool &hasReturns); - void RewriteTryReturnStmts(Stmt *S); - void RewriteSyncReturnStmts(Stmt *S, std::string buf); - Stmt *RewriteObjCTryStmt(ObjCAtTryStmt *S); - Stmt *RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S); - Stmt *RewriteObjCCatchStmt(ObjCAtCatchStmt *S); - Stmt *RewriteObjCFinallyStmt(ObjCAtFinallyStmt *S); - Stmt *RewriteObjCThrowStmt(ObjCAtThrowStmt *S); - Stmt *RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, - SourceLocation OrigEnd); - CallExpr *SynthesizeCallToFunctionDecl(FunctionDecl *FD, - Expr **args, unsigned nargs, - SourceLocation StartLoc=SourceLocation(), - SourceLocation EndLoc=SourceLocation()); - Stmt *SynthMessageExpr(ObjCMessageExpr *Exp, - SourceLocation StartLoc=SourceLocation(), - SourceLocation EndLoc=SourceLocation()); - Stmt *RewriteBreakStmt(BreakStmt *S); - Stmt *RewriteContinueStmt(ContinueStmt *S); - void SynthCountByEnumWithState(std::string &buf); - - void SynthMsgSendFunctionDecl(); - void SynthMsgSendSuperFunctionDecl(); - void SynthMsgSendStretFunctionDecl(); - void SynthMsgSendFpretFunctionDecl(); - void SynthMsgSendSuperStretFunctionDecl(); - void SynthGetClassFunctionDecl(); - void SynthGetMetaClassFunctionDecl(); - void SynthGetSuperClassFunctionDecl(); - void SynthSelGetUidFunctionDecl(); - void SynthSuperContructorFunctionDecl(); - - // Metadata emission. - void RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, - std::string &Result); - - void RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *CDecl, - std::string &Result); - - template<typename MethodIterator> - void RewriteObjCMethodsMetaData(MethodIterator MethodBegin, - MethodIterator MethodEnd, - bool IsInstanceMethod, - const char *prefix, - const char *ClassName, - std::string &Result); - - void RewriteObjCProtocolMetaData(ObjCProtocolDecl *Protocol, - const char *prefix, - const char *ClassName, - std::string &Result); - void RewriteObjCProtocolListMetaData(const ObjCList<ObjCProtocolDecl> &Prots, - const char *prefix, - const char *ClassName, - std::string &Result); - void SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl, - std::string &Result); - void SynthesizeIvarOffsetComputation(ObjCContainerDecl *IDecl, - ObjCIvarDecl *ivar, - std::string &Result); - void RewriteImplementations(); - void SynthesizeMetaDataIntoBuffer(std::string &Result); - - // Block rewriting. - void RewriteBlocksInFunctionProtoType(QualType funcType, NamedDecl *D); - void CheckFunctionPointerDecl(QualType dType, NamedDecl *ND); - - void InsertBlockLiteralsWithinFunction(FunctionDecl *FD); - void InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD); - - // Block specific rewrite rules. - void RewriteBlockCall(CallExpr *Exp); - void RewriteBlockPointerDecl(NamedDecl *VD); - void RewriteByRefVar(VarDecl *VD); - std::string SynthesizeByrefCopyDestroyHelper(VarDecl *VD, int flag); - Stmt *RewriteBlockDeclRefExpr(Expr *VD); - Stmt *RewriteLocalVariableExternalStorage(DeclRefExpr *DRE); - void RewriteBlockPointerFunctionArgs(FunctionDecl *FD); - - std::string SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, - const char *funcName, std::string Tag); - std::string SynthesizeBlockFunc(BlockExpr *CE, int i, - const char *funcName, std::string Tag); - std::string SynthesizeBlockImpl(BlockExpr *CE, - std::string Tag, std::string Desc); - std::string SynthesizeBlockDescriptor(std::string DescTag, - std::string ImplTag, - int i, const char *funcName, - unsigned hasCopy); - Stmt *SynthesizeBlockCall(CallExpr *Exp, const Expr* BlockExp); - void SynthesizeBlockLiterals(SourceLocation FunLocStart, - const char *FunName); - void RewriteRecordBody(RecordDecl *RD); - - void CollectBlockDeclRefInfo(BlockExpr *Exp); - void GetBlockDeclRefExprs(Stmt *S); - void GetInnerBlockDeclRefExprs(Stmt *S, - llvm::SmallVector<BlockDeclRefExpr *, 8> &InnerBlockDeclRefs, - llvm::SmallPtrSet<const DeclContext *, 8> &InnerContexts); - - // We avoid calling Type::isBlockPointerType(), since it operates on the - // canonical type. We only care if the top-level type is a closure pointer. - bool isTopLevelBlockPointerType(QualType T) { - return isa<BlockPointerType>(T); - } - - /// convertBlockPointerToFunctionPointer - Converts a block-pointer type - /// to a function pointer type and upon success, returns true; false - /// otherwise. - bool convertBlockPointerToFunctionPointer(QualType &T) { - if (isTopLevelBlockPointerType(T)) { - const BlockPointerType *BPT = T->getAs<BlockPointerType>(); - T = Context->getPointerType(BPT->getPointeeType()); - return true; - } - return false; - } - - // FIXME: This predicate seems like it would be useful to add to ASTContext. - bool isObjCType(QualType T) { - if (!LangOpts.ObjC1 && !LangOpts.ObjC2) - return false; - - QualType OCT = Context->getCanonicalType(T).getUnqualifiedType(); - - if (OCT == Context->getCanonicalType(Context->getObjCIdType()) || - OCT == Context->getCanonicalType(Context->getObjCClassType())) - return true; - - if (const PointerType *PT = OCT->getAs<PointerType>()) { - if (isa<ObjCInterfaceType>(PT->getPointeeType()) || - PT->getPointeeType()->isObjCQualifiedIdType()) - return true; - } - return false; - } - bool PointerTypeTakesAnyBlockArguments(QualType QT); - void GetExtentOfArgList(const char *Name, const char *&LParen, - const char *&RParen); - void RewriteCastExpr(CStyleCastExpr *CE); - - FunctionDecl *SynthBlockInitFunctionDecl(const char *name); - Stmt *SynthBlockInitExpr(BlockExpr *Exp, - const llvm::SmallVector<BlockDeclRefExpr *, 8> &InnerBlockDeclRefs); - - void QuoteDoublequotes(std::string &From, std::string &To) { - for (unsigned i = 0; i < From.length(); i++) { - if (From[i] == '"') - To += "\\\""; - else - To += From[i]; - } - } - }; - - // Helper function: create a CStyleCastExpr with trivial type source info. - CStyleCastExpr* NoTypeInfoCStyleCastExpr(ASTContext *Ctx, QualType Ty, - CastExpr::CastKind Kind, Expr *E) { - TypeSourceInfo *TInfo = Ctx->getTrivialTypeSourceInfo(Ty, SourceLocation()); - return new (Ctx) CStyleCastExpr(Ty, Kind, E, CXXBaseSpecifierArray(), TInfo, - SourceLocation(), SourceLocation()); - } -} - -void RewriteObjC::RewriteBlocksInFunctionProtoType(QualType funcType, - NamedDecl *D) { - if (FunctionProtoType *fproto = dyn_cast<FunctionProtoType>(funcType)) { - for (FunctionProtoType::arg_type_iterator I = fproto->arg_type_begin(), - E = fproto->arg_type_end(); I && (I != E); ++I) - if (isTopLevelBlockPointerType(*I)) { - // All the args are checked/rewritten. Don't call twice! - RewriteBlockPointerDecl(D); - break; - } - } -} - -void RewriteObjC::CheckFunctionPointerDecl(QualType funcType, NamedDecl *ND) { - const PointerType *PT = funcType->getAs<PointerType>(); - if (PT && PointerTypeTakesAnyBlockArguments(funcType)) - RewriteBlocksInFunctionProtoType(PT->getPointeeType(), ND); -} - -static bool IsHeaderFile(const std::string &Filename) { - std::string::size_type DotPos = Filename.rfind('.'); - - if (DotPos == std::string::npos) { - // no file extension - return false; - } - - std::string Ext = std::string(Filename.begin()+DotPos+1, Filename.end()); - // C header: .h - // C++ header: .hh or .H; - return Ext == "h" || Ext == "hh" || Ext == "H"; -} - -RewriteObjC::RewriteObjC(std::string inFile, llvm::raw_ostream* OS, - Diagnostic &D, const LangOptions &LOpts, - bool silenceMacroWarn) - : Diags(D), LangOpts(LOpts), InFileName(inFile), OutFile(OS), - SilenceRewriteMacroWarning(silenceMacroWarn) { - IsHeader = IsHeaderFile(inFile); - RewriteFailedDiag = Diags.getCustomDiagID(Diagnostic::Warning, - "rewriting sub-expression within a macro (may not be correct)"); - TryFinallyContainsReturnDiag = Diags.getCustomDiagID(Diagnostic::Warning, - "rewriter doesn't support user-specified control flow semantics " - "for @try/@finally (code may not execute properly)"); -} - -ASTConsumer *clang::CreateObjCRewriter(const std::string& InFile, - llvm::raw_ostream* OS, - Diagnostic &Diags, - const LangOptions &LOpts, - bool SilenceRewriteMacroWarning) { - return new RewriteObjC(InFile, OS, Diags, LOpts, SilenceRewriteMacroWarning); -} - -void RewriteObjC::Initialize(ASTContext &context) { - Context = &context; - SM = &Context->getSourceManager(); - TUDecl = Context->getTranslationUnitDecl(); - MsgSendFunctionDecl = 0; - MsgSendSuperFunctionDecl = 0; - MsgSendStretFunctionDecl = 0; - MsgSendSuperStretFunctionDecl = 0; - MsgSendFpretFunctionDecl = 0; - GetClassFunctionDecl = 0; - GetMetaClassFunctionDecl = 0; - GetSuperClassFunctionDecl = 0; - SelGetUidFunctionDecl = 0; - CFStringFunctionDecl = 0; - ConstantStringClassReference = 0; - NSStringRecord = 0; - CurMethodDef = 0; - CurFunctionDef = 0; - CurFunctionDeclToDeclareForBlock = 0; - GlobalVarDecl = 0; - SuperStructDecl = 0; - ProtocolTypeDecl = 0; - ConstantStringDecl = 0; - BcLabelCount = 0; - SuperContructorFunctionDecl = 0; - NumObjCStringLiterals = 0; - PropParentMap = 0; - CurrentBody = 0; - DisableReplaceStmt = false; - objc_impl_method = false; - - // Get the ID and start/end of the main file. - MainFileID = SM->getMainFileID(); - const llvm::MemoryBuffer *MainBuf = SM->getBuffer(MainFileID); - MainFileStart = MainBuf->getBufferStart(); - MainFileEnd = MainBuf->getBufferEnd(); - - Rewrite.setSourceMgr(Context->getSourceManager(), Context->getLangOptions()); - - // declaring objc_selector outside the parameter list removes a silly - // scope related warning... - if (IsHeader) - Preamble = "#pragma once\n"; - Preamble += "struct objc_selector; struct objc_class;\n"; - Preamble += "struct __rw_objc_super { struct objc_object *object; "; - Preamble += "struct objc_object *superClass; "; - if (LangOpts.Microsoft) { - // Add a constructor for creating temporary objects. - Preamble += "__rw_objc_super(struct objc_object *o, struct objc_object *s) " - ": "; - Preamble += "object(o), superClass(s) {} "; - } - Preamble += "};\n"; - Preamble += "#ifndef _REWRITER_typedef_Protocol\n"; - Preamble += "typedef struct objc_object Protocol;\n"; - Preamble += "#define _REWRITER_typedef_Protocol\n"; - Preamble += "#endif\n"; - if (LangOpts.Microsoft) { - Preamble += "#define __OBJC_RW_DLLIMPORT extern \"C\" __declspec(dllimport)\n"; - Preamble += "#define __OBJC_RW_STATICIMPORT extern \"C\"\n"; - } else - Preamble += "#define __OBJC_RW_DLLIMPORT extern\n"; - Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSend"; - Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; - Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSendSuper"; - Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; - Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSend_stret"; - Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; - Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_msgSendSuper_stret"; - Preamble += "(struct objc_super *, struct objc_selector *, ...);\n"; - Preamble += "__OBJC_RW_DLLIMPORT double objc_msgSend_fpret"; - Preamble += "(struct objc_object *, struct objc_selector *, ...);\n"; - Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getClass"; - Preamble += "(const char *);\n"; - Preamble += "__OBJC_RW_DLLIMPORT struct objc_class *class_getSuperclass"; - Preamble += "(struct objc_class *);\n"; - Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_getMetaClass"; - Preamble += "(const char *);\n"; - Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_throw(struct objc_object *);\n"; - Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_enter(void *);\n"; - Preamble += "__OBJC_RW_DLLIMPORT void objc_exception_try_exit(void *);\n"; - Preamble += "__OBJC_RW_DLLIMPORT struct objc_object *objc_exception_extract(void *);\n"; - Preamble += "__OBJC_RW_DLLIMPORT int objc_exception_match"; - Preamble += "(struct objc_class *, struct objc_object *);\n"; - // @synchronized hooks. - Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_enter(struct objc_object *);\n"; - Preamble += "__OBJC_RW_DLLIMPORT void objc_sync_exit(struct objc_object *);\n"; - Preamble += "__OBJC_RW_DLLIMPORT Protocol *objc_getProtocol(const char *);\n"; - Preamble += "#ifndef __FASTENUMERATIONSTATE\n"; - Preamble += "struct __objcFastEnumerationState {\n\t"; - Preamble += "unsigned long state;\n\t"; - Preamble += "void **itemsPtr;\n\t"; - Preamble += "unsigned long *mutationsPtr;\n\t"; - Preamble += "unsigned long extra[5];\n};\n"; - Preamble += "__OBJC_RW_DLLIMPORT void objc_enumerationMutation(struct objc_object *);\n"; - Preamble += "#define __FASTENUMERATIONSTATE\n"; - Preamble += "#endif\n"; - Preamble += "#ifndef __NSCONSTANTSTRINGIMPL\n"; - Preamble += "struct __NSConstantStringImpl {\n"; - Preamble += " int *isa;\n"; - Preamble += " int flags;\n"; - Preamble += " char *str;\n"; - Preamble += " long length;\n"; - Preamble += "};\n"; - Preamble += "#ifdef CF_EXPORT_CONSTANT_STRING\n"; - Preamble += "extern \"C\" __declspec(dllexport) int __CFConstantStringClassReference[];\n"; - Preamble += "#else\n"; - Preamble += "__OBJC_RW_DLLIMPORT int __CFConstantStringClassReference[];\n"; - Preamble += "#endif\n"; - Preamble += "#define __NSCONSTANTSTRINGIMPL\n"; - Preamble += "#endif\n"; - // Blocks preamble. - Preamble += "#ifndef BLOCK_IMPL\n"; - Preamble += "#define BLOCK_IMPL\n"; - Preamble += "struct __block_impl {\n"; - Preamble += " void *isa;\n"; - Preamble += " int Flags;\n"; - Preamble += " int Reserved;\n"; - Preamble += " void *FuncPtr;\n"; - Preamble += "};\n"; - Preamble += "// Runtime copy/destroy helper functions (from Block_private.h)\n"; - Preamble += "#ifdef __OBJC_EXPORT_BLOCKS\n"; - Preamble += "extern \"C\" __declspec(dllexport) " - "void _Block_object_assign(void *, const void *, const int);\n"; - Preamble += "extern \"C\" __declspec(dllexport) void _Block_object_dispose(const void *, const int);\n"; - Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteGlobalBlock[32];\n"; - Preamble += "extern \"C\" __declspec(dllexport) void *_NSConcreteStackBlock[32];\n"; - Preamble += "#else\n"; - Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_assign(void *, const void *, const int);\n"; - Preamble += "__OBJC_RW_DLLIMPORT void _Block_object_dispose(const void *, const int);\n"; - Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteGlobalBlock[32];\n"; - Preamble += "__OBJC_RW_DLLIMPORT void *_NSConcreteStackBlock[32];\n"; - Preamble += "#endif\n"; - Preamble += "#endif\n"; - if (LangOpts.Microsoft) { - Preamble += "#undef __OBJC_RW_DLLIMPORT\n"; - Preamble += "#undef __OBJC_RW_STATICIMPORT\n"; - Preamble += "#ifndef KEEP_ATTRIBUTES\n"; // We use this for clang tests. - Preamble += "#define __attribute__(X)\n"; - Preamble += "#endif\n"; - Preamble += "#define __weak\n"; - } - else { - Preamble += "#define __block\n"; - Preamble += "#define __weak\n"; - } - // NOTE! Windows uses LLP64 for 64bit mode. So, cast pointer to long long - // as this avoids warning in any 64bit/32bit compilation model. - Preamble += "\n#define __OFFSETOFIVAR__(TYPE, MEMBER) ((long long) &((TYPE *)0)->MEMBER)\n"; -} - - -//===----------------------------------------------------------------------===// -// Top Level Driver Code -//===----------------------------------------------------------------------===// - -void RewriteObjC::HandleTopLevelSingleDecl(Decl *D) { - if (Diags.hasErrorOccurred()) - return; - - // Two cases: either the decl could be in the main file, or it could be in a - // #included file. If the former, rewrite it now. If the later, check to see - // if we rewrote the #include/#import. - SourceLocation Loc = D->getLocation(); - Loc = SM->getInstantiationLoc(Loc); - - // If this is for a builtin, ignore it. - if (Loc.isInvalid()) return; - - // Look for built-in declarations that we need to refer during the rewrite. - if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { - RewriteFunctionDecl(FD); - } else if (VarDecl *FVD = dyn_cast<VarDecl>(D)) { - // declared in <Foundation/NSString.h> - if (strcmp(FVD->getNameAsCString(), "_NSConstantStringClassReference") == 0) { - ConstantStringClassReference = FVD; - return; - } - } else if (ObjCInterfaceDecl *MD = dyn_cast<ObjCInterfaceDecl>(D)) { - RewriteInterfaceDecl(MD); - } else if (ObjCCategoryDecl *CD = dyn_cast<ObjCCategoryDecl>(D)) { - RewriteCategoryDecl(CD); - } else if (ObjCProtocolDecl *PD = dyn_cast<ObjCProtocolDecl>(D)) { - RewriteProtocolDecl(PD); - } else if (ObjCForwardProtocolDecl *FP = - dyn_cast<ObjCForwardProtocolDecl>(D)){ - RewriteForwardProtocolDecl(FP); - } else if (LinkageSpecDecl *LSD = dyn_cast<LinkageSpecDecl>(D)) { - // Recurse into linkage specifications - for (DeclContext::decl_iterator DI = LSD->decls_begin(), - DIEnd = LSD->decls_end(); - DI != DIEnd; ++DI) - HandleTopLevelSingleDecl(*DI); - } - // If we have a decl in the main file, see if we should rewrite it. - if (SM->isFromMainFile(Loc)) - return HandleDeclInMainFile(D); -} - -//===----------------------------------------------------------------------===// -// Syntactic (non-AST) Rewriting Code -//===----------------------------------------------------------------------===// - -void RewriteObjC::RewriteInclude() { - SourceLocation LocStart = SM->getLocForStartOfFile(MainFileID); - llvm::StringRef MainBuf = SM->getBufferData(MainFileID); - const char *MainBufStart = MainBuf.begin(); - const char *MainBufEnd = MainBuf.end(); - size_t ImportLen = strlen("import"); - - // Loop over the whole file, looking for includes. - for (const char *BufPtr = MainBufStart; BufPtr < MainBufEnd; ++BufPtr) { - if (*BufPtr == '#') { - if (++BufPtr == MainBufEnd) - return; - while (*BufPtr == ' ' || *BufPtr == '\t') - if (++BufPtr == MainBufEnd) - return; - if (!strncmp(BufPtr, "import", ImportLen)) { - // replace import with include - SourceLocation ImportLoc = - LocStart.getFileLocWithOffset(BufPtr-MainBufStart); - ReplaceText(ImportLoc, ImportLen, "include"); - BufPtr += ImportLen; - } - } - } -} - -void RewriteObjC::RewriteTabs() { - llvm::StringRef MainBuf = SM->getBufferData(MainFileID); - const char *MainBufStart = MainBuf.begin(); - const char *MainBufEnd = MainBuf.end(); - - // Loop over the whole file, looking for tabs. - for (const char *BufPtr = MainBufStart; BufPtr != MainBufEnd; ++BufPtr) { - if (*BufPtr != '\t') - continue; - - // Okay, we found a tab. This tab will turn into at least one character, - // but it depends on which 'virtual column' it is in. Compute that now. - unsigned VCol = 0; - while (BufPtr-VCol != MainBufStart && BufPtr[-VCol-1] != '\t' && - BufPtr[-VCol-1] != '\n' && BufPtr[-VCol-1] != '\r') - ++VCol; - - // Okay, now that we know the virtual column, we know how many spaces to - // insert. We assume 8-character tab-stops. - unsigned Spaces = 8-(VCol & 7); - - // Get the location of the tab. - SourceLocation TabLoc = SM->getLocForStartOfFile(MainFileID); - TabLoc = TabLoc.getFileLocWithOffset(BufPtr-MainBufStart); - - // Rewrite the single tab character into a sequence of spaces. - ReplaceText(TabLoc, 1, llvm::StringRef(" ", Spaces)); - } -} - -static std::string getIvarAccessString(ObjCInterfaceDecl *ClassDecl, - ObjCIvarDecl *OID) { - std::string S; - S = "((struct "; - S += ClassDecl->getIdentifier()->getName(); - S += "_IMPL *)self)->"; - S += OID->getName(); - return S; -} - -void RewriteObjC::RewritePropertyImplDecl(ObjCPropertyImplDecl *PID, - ObjCImplementationDecl *IMD, - ObjCCategoryImplDecl *CID) { - static bool objcGetPropertyDefined = false; - static bool objcSetPropertyDefined = false; - SourceLocation startLoc = PID->getLocStart(); - InsertText(startLoc, "// "); - const char *startBuf = SM->getCharacterData(startLoc); - assert((*startBuf == '@') && "bogus @synthesize location"); - const char *semiBuf = strchr(startBuf, ';'); - assert((*semiBuf == ';') && "@synthesize: can't find ';'"); - SourceLocation onePastSemiLoc = - startLoc.getFileLocWithOffset(semiBuf-startBuf+1); - - if (PID->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) - return; // FIXME: is this correct? - - // Generate the 'getter' function. - ObjCPropertyDecl *PD = PID->getPropertyDecl(); - ObjCInterfaceDecl *ClassDecl = PD->getGetterMethodDecl()->getClassInterface(); - ObjCIvarDecl *OID = PID->getPropertyIvarDecl(); - - if (!OID) - return; - unsigned Attributes = PD->getPropertyAttributes(); - bool GenGetProperty = !(Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) && - (Attributes & (ObjCPropertyDecl::OBJC_PR_retain | - ObjCPropertyDecl::OBJC_PR_copy)); - std::string Getr; - if (GenGetProperty && !objcGetPropertyDefined) { - objcGetPropertyDefined = true; - // FIXME. Is this attribute correct in all cases? - Getr = "\nextern \"C\" __declspec(dllimport) " - "id objc_getProperty(id, SEL, long, bool);\n"; - } - RewriteObjCMethodDecl(PD->getGetterMethodDecl(), Getr); - Getr += "{ "; - // Synthesize an explicit cast to gain access to the ivar. - // See objc-act.c:objc_synthesize_new_getter() for details. - if (GenGetProperty) { - // return objc_getProperty(self, _cmd, offsetof(ClassDecl, OID), 1) - Getr += "typedef "; - const FunctionType *FPRetType = 0; - RewriteTypeIntoString(PD->getGetterMethodDecl()->getResultType(), Getr, - FPRetType); - Getr += " _TYPE"; - if (FPRetType) { - Getr += ")"; // close the precedence "scope" for "*". - - // Now, emit the argument types (if any). - if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)) { - Getr += "("; - for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) { - if (i) Getr += ", "; - std::string ParamStr = FT->getArgType(i).getAsString(); - Getr += ParamStr; - } - if (FT->isVariadic()) { - if (FT->getNumArgs()) Getr += ", "; - Getr += "..."; - } - Getr += ")"; - } else - Getr += "()"; - } - Getr += ";\n"; - Getr += "return (_TYPE)"; - Getr += "objc_getProperty(self, _cmd, "; - SynthesizeIvarOffsetComputation(ClassDecl, OID, Getr); - Getr += ", 1)"; - } - else - Getr += "return " + getIvarAccessString(ClassDecl, OID); - Getr += "; }"; - InsertText(onePastSemiLoc, Getr); - if (PD->isReadOnly()) - return; - - // Generate the 'setter' function. - std::string Setr; - bool GenSetProperty = Attributes & (ObjCPropertyDecl::OBJC_PR_retain | - ObjCPropertyDecl::OBJC_PR_copy); - if (GenSetProperty && !objcSetPropertyDefined) { - objcSetPropertyDefined = true; - // FIXME. Is this attribute correct in all cases? - Setr = "\nextern \"C\" __declspec(dllimport) " - "void objc_setProperty (id, SEL, long, id, bool, bool);\n"; - } - - RewriteObjCMethodDecl(PD->getSetterMethodDecl(), Setr); - Setr += "{ "; - // Synthesize an explicit cast to initialize the ivar. - // See objc-act.c:objc_synthesize_new_setter() for details. - if (GenSetProperty) { - Setr += "objc_setProperty (self, _cmd, "; - SynthesizeIvarOffsetComputation(ClassDecl, OID, Setr); - Setr += ", (id)"; - Setr += PD->getNameAsCString(); - Setr += ", "; - if (Attributes & ObjCPropertyDecl::OBJC_PR_nonatomic) - Setr += "0, "; - else - Setr += "1, "; - if (Attributes & ObjCPropertyDecl::OBJC_PR_copy) - Setr += "1)"; - else - Setr += "0)"; - } - else { - Setr += getIvarAccessString(ClassDecl, OID) + " = "; - Setr += PD->getNameAsCString(); - } - Setr += "; }"; - InsertText(onePastSemiLoc, Setr); -} - -void RewriteObjC::RewriteForwardClassDecl(ObjCClassDecl *ClassDecl) { - // Get the start location and compute the semi location. - SourceLocation startLoc = ClassDecl->getLocation(); - const char *startBuf = SM->getCharacterData(startLoc); - const char *semiPtr = strchr(startBuf, ';'); - - // Translate to typedef's that forward reference structs with the same name - // as the class. As a convenience, we include the original declaration - // as a comment. - std::string typedefString; - typedefString += "// @class "; - for (ObjCClassDecl::iterator I = ClassDecl->begin(), E = ClassDecl->end(); - I != E; ++I) { - ObjCInterfaceDecl *ForwardDecl = I->getInterface(); - typedefString += ForwardDecl->getNameAsString(); - if (I+1 != E) - typedefString += ", "; - else - typedefString += ";\n"; - } - - for (ObjCClassDecl::iterator I = ClassDecl->begin(), E = ClassDecl->end(); - I != E; ++I) { - ObjCInterfaceDecl *ForwardDecl = I->getInterface(); - typedefString += "#ifndef _REWRITER_typedef_"; - typedefString += ForwardDecl->getNameAsString(); - typedefString += "\n"; - typedefString += "#define _REWRITER_typedef_"; - typedefString += ForwardDecl->getNameAsString(); - typedefString += "\n"; - typedefString += "typedef struct objc_object "; - typedefString += ForwardDecl->getNameAsString(); - typedefString += ";\n#endif\n"; - } - - // Replace the @class with typedefs corresponding to the classes. - ReplaceText(startLoc, semiPtr-startBuf+1, typedefString); -} - -void RewriteObjC::RewriteMethodDeclaration(ObjCMethodDecl *Method) { - // When method is a synthesized one, such as a getter/setter there is - // nothing to rewrite. - if (Method->isSynthesized()) - return; - SourceLocation LocStart = Method->getLocStart(); - SourceLocation LocEnd = Method->getLocEnd(); - - if (SM->getInstantiationLineNumber(LocEnd) > - SM->getInstantiationLineNumber(LocStart)) { - InsertText(LocStart, "#if 0\n"); - ReplaceText(LocEnd, 1, ";\n#endif\n"); - } else { - InsertText(LocStart, "// "); - } -} - -void RewriteObjC::RewriteProperty(ObjCPropertyDecl *prop) { - SourceLocation Loc = prop->getAtLoc(); - - ReplaceText(Loc, 0, "// "); - // FIXME: handle properties that are declared across multiple lines. -} - -void RewriteObjC::RewriteCategoryDecl(ObjCCategoryDecl *CatDecl) { - SourceLocation LocStart = CatDecl->getLocStart(); - - // FIXME: handle category headers that are declared across multiple lines. - ReplaceText(LocStart, 0, "// "); - - for (ObjCCategoryDecl::prop_iterator I = CatDecl->prop_begin(), - E = CatDecl->prop_end(); I != E; ++I) - RewriteProperty(*I); - - for (ObjCCategoryDecl::instmeth_iterator - I = CatDecl->instmeth_begin(), E = CatDecl->instmeth_end(); - I != E; ++I) - RewriteMethodDeclaration(*I); - for (ObjCCategoryDecl::classmeth_iterator - I = CatDecl->classmeth_begin(), E = CatDecl->classmeth_end(); - I != E; ++I) - RewriteMethodDeclaration(*I); - - // Lastly, comment out the @end. - ReplaceText(CatDecl->getAtEndRange().getBegin(), - strlen("@end"), "/* @end */"); -} - -void RewriteObjC::RewriteProtocolDecl(ObjCProtocolDecl *PDecl) { - SourceLocation LocStart = PDecl->getLocStart(); - - // FIXME: handle protocol headers that are declared across multiple lines. - ReplaceText(LocStart, 0, "// "); - - for (ObjCProtocolDecl::instmeth_iterator - I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); - I != E; ++I) - RewriteMethodDeclaration(*I); - for (ObjCProtocolDecl::classmeth_iterator - I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); - I != E; ++I) - RewriteMethodDeclaration(*I); - - // Lastly, comment out the @end. - SourceLocation LocEnd = PDecl->getAtEndRange().getBegin(); - ReplaceText(LocEnd, strlen("@end"), "/* @end */"); - - // Must comment out @optional/@required - const char *startBuf = SM->getCharacterData(LocStart); - const char *endBuf = SM->getCharacterData(LocEnd); - for (const char *p = startBuf; p < endBuf; p++) { - if (*p == '@' && !strncmp(p+1, "optional", strlen("optional"))) { - SourceLocation OptionalLoc = LocStart.getFileLocWithOffset(p-startBuf); - ReplaceText(OptionalLoc, strlen("@optional"), "/* @optional */"); - - } - else if (*p == '@' && !strncmp(p+1, "required", strlen("required"))) { - SourceLocation OptionalLoc = LocStart.getFileLocWithOffset(p-startBuf); - ReplaceText(OptionalLoc, strlen("@required"), "/* @required */"); - - } - } -} - -void RewriteObjC::RewriteForwardProtocolDecl(ObjCForwardProtocolDecl *PDecl) { - SourceLocation LocStart = PDecl->getLocation(); - if (LocStart.isInvalid()) - assert(false && "Invalid SourceLocation"); - // FIXME: handle forward protocol that are declared across multiple lines. - ReplaceText(LocStart, 0, "// "); -} - -void RewriteObjC::RewriteTypeIntoString(QualType T, std::string &ResultStr, - const FunctionType *&FPRetType) { - if (T->isObjCQualifiedIdType()) - ResultStr += "id"; - else if (T->isFunctionPointerType() || - T->isBlockPointerType()) { - // needs special handling, since pointer-to-functions have special - // syntax (where a decaration models use). - QualType retType = T; - QualType PointeeTy; - if (const PointerType* PT = retType->getAs<PointerType>()) - PointeeTy = PT->getPointeeType(); - else if (const BlockPointerType *BPT = retType->getAs<BlockPointerType>()) - PointeeTy = BPT->getPointeeType(); - if ((FPRetType = PointeeTy->getAs<FunctionType>())) { - ResultStr += FPRetType->getResultType().getAsString(); - ResultStr += "(*"; - } - } else - ResultStr += T.getAsString(); -} - -void RewriteObjC::RewriteObjCMethodDecl(ObjCMethodDecl *OMD, - std::string &ResultStr) { - //fprintf(stderr,"In RewriteObjCMethodDecl\n"); - const FunctionType *FPRetType = 0; - ResultStr += "\nstatic "; - RewriteTypeIntoString(OMD->getResultType(), ResultStr, FPRetType); - ResultStr += " "; - - // Unique method name - std::string NameStr; - - if (OMD->isInstanceMethod()) - NameStr += "_I_"; - else - NameStr += "_C_"; - - NameStr += OMD->getClassInterface()->getNameAsString(); - NameStr += "_"; - - if (ObjCCategoryImplDecl *CID = - dyn_cast<ObjCCategoryImplDecl>(OMD->getDeclContext())) { - NameStr += CID->getNameAsString(); - NameStr += "_"; - } - // Append selector names, replacing ':' with '_' - { - std::string selString = OMD->getSelector().getAsString(); - int len = selString.size(); - for (int i = 0; i < len; i++) - if (selString[i] == ':') - selString[i] = '_'; - NameStr += selString; - } - // Remember this name for metadata emission - MethodInternalNames[OMD] = NameStr; - ResultStr += NameStr; - - // Rewrite arguments - ResultStr += "("; - - // invisible arguments - if (OMD->isInstanceMethod()) { - QualType selfTy = Context->getObjCInterfaceType(OMD->getClassInterface()); - selfTy = Context->getPointerType(selfTy); - if (!LangOpts.Microsoft) { - if (ObjCSynthesizedStructs.count(OMD->getClassInterface())) - ResultStr += "struct "; - } - // When rewriting for Microsoft, explicitly omit the structure name. - ResultStr += OMD->getClassInterface()->getNameAsString(); - ResultStr += " *"; - } - else - ResultStr += Context->getObjCClassType().getAsString(); - - ResultStr += " self, "; - ResultStr += Context->getObjCSelType().getAsString(); - ResultStr += " _cmd"; - - // Method arguments. - for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), - E = OMD->param_end(); PI != E; ++PI) { - ParmVarDecl *PDecl = *PI; - ResultStr += ", "; - if (PDecl->getType()->isObjCQualifiedIdType()) { - ResultStr += "id "; - ResultStr += PDecl->getNameAsString(); - } else { - std::string Name = PDecl->getNameAsString(); - QualType QT = PDecl->getType(); - // Make sure we convert "t (^)(...)" to "t (*)(...)". - if (convertBlockPointerToFunctionPointer(QT)) - QT.getAsStringInternal(Name, Context->PrintingPolicy); - else - PDecl->getType().getAsStringInternal(Name, Context->PrintingPolicy); - ResultStr += Name; - } - } - if (OMD->isVariadic()) - ResultStr += ", ..."; - ResultStr += ") "; - - if (FPRetType) { - ResultStr += ")"; // close the precedence "scope" for "*". - - // Now, emit the argument types (if any). - if (const FunctionProtoType *FT = dyn_cast<FunctionProtoType>(FPRetType)) { - ResultStr += "("; - for (unsigned i = 0, e = FT->getNumArgs(); i != e; ++i) { - if (i) ResultStr += ", "; - std::string ParamStr = FT->getArgType(i).getAsString(); - ResultStr += ParamStr; - } - if (FT->isVariadic()) { - if (FT->getNumArgs()) ResultStr += ", "; - ResultStr += "..."; - } - ResultStr += ")"; - } else { - ResultStr += "()"; - } - } -} -void RewriteObjC::RewriteImplementationDecl(Decl *OID) { - ObjCImplementationDecl *IMD = dyn_cast<ObjCImplementationDecl>(OID); - ObjCCategoryImplDecl *CID = dyn_cast<ObjCCategoryImplDecl>(OID); - - InsertText(IMD ? IMD->getLocStart() : CID->getLocStart(), "// "); - - for (ObjCCategoryImplDecl::instmeth_iterator - I = IMD ? IMD->instmeth_begin() : CID->instmeth_begin(), - E = IMD ? IMD->instmeth_end() : CID->instmeth_end(); - I != E; ++I) { - std::string ResultStr; - ObjCMethodDecl *OMD = *I; - RewriteObjCMethodDecl(OMD, ResultStr); - SourceLocation LocStart = OMD->getLocStart(); - SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); - - const char *startBuf = SM->getCharacterData(LocStart); - const char *endBuf = SM->getCharacterData(LocEnd); - ReplaceText(LocStart, endBuf-startBuf, ResultStr); - } - - for (ObjCCategoryImplDecl::classmeth_iterator - I = IMD ? IMD->classmeth_begin() : CID->classmeth_begin(), - E = IMD ? IMD->classmeth_end() : CID->classmeth_end(); - I != E; ++I) { - std::string ResultStr; - ObjCMethodDecl *OMD = *I; - RewriteObjCMethodDecl(OMD, ResultStr); - SourceLocation LocStart = OMD->getLocStart(); - SourceLocation LocEnd = OMD->getCompoundBody()->getLocStart(); - - const char *startBuf = SM->getCharacterData(LocStart); - const char *endBuf = SM->getCharacterData(LocEnd); - ReplaceText(LocStart, endBuf-startBuf, ResultStr); - } - for (ObjCCategoryImplDecl::propimpl_iterator - I = IMD ? IMD->propimpl_begin() : CID->propimpl_begin(), - E = IMD ? IMD->propimpl_end() : CID->propimpl_end(); - I != E; ++I) { - RewritePropertyImplDecl(*I, IMD, CID); - } - - InsertText(IMD ? IMD->getLocEnd() : CID->getLocEnd(), "// "); -} - -void RewriteObjC::RewriteInterfaceDecl(ObjCInterfaceDecl *ClassDecl) { - std::string ResultStr; - if (!ObjCForwardDecls.count(ClassDecl)) { - // we haven't seen a forward decl - generate a typedef. - ResultStr = "#ifndef _REWRITER_typedef_"; - ResultStr += ClassDecl->getNameAsString(); - ResultStr += "\n"; - ResultStr += "#define _REWRITER_typedef_"; - ResultStr += ClassDecl->getNameAsString(); - ResultStr += "\n"; - ResultStr += "typedef struct objc_object "; - ResultStr += ClassDecl->getNameAsString(); - ResultStr += ";\n#endif\n"; - // Mark this typedef as having been generated. - ObjCForwardDecls.insert(ClassDecl); - } - SynthesizeObjCInternalStruct(ClassDecl, ResultStr); - - for (ObjCInterfaceDecl::prop_iterator I = ClassDecl->prop_begin(), - E = ClassDecl->prop_end(); I != E; ++I) - RewriteProperty(*I); - for (ObjCInterfaceDecl::instmeth_iterator - I = ClassDecl->instmeth_begin(), E = ClassDecl->instmeth_end(); - I != E; ++I) - RewriteMethodDeclaration(*I); - for (ObjCInterfaceDecl::classmeth_iterator - I = ClassDecl->classmeth_begin(), E = ClassDecl->classmeth_end(); - I != E; ++I) - RewriteMethodDeclaration(*I); - - // Lastly, comment out the @end. - ReplaceText(ClassDecl->getAtEndRange().getBegin(), strlen("@end"), - "/* @end */"); -} - -Stmt *RewriteObjC::RewritePropertySetter(BinaryOperator *BinOp, Expr *newStmt, - SourceRange SrcRange) { - // Synthesize a ObjCMessageExpr from a ObjCPropertyRefExpr. - // This allows us to reuse all the fun and games in SynthMessageExpr(). - ObjCPropertyRefExpr *PropRefExpr = dyn_cast<ObjCPropertyRefExpr>(BinOp->getLHS()); - ObjCMessageExpr *MsgExpr; - ObjCPropertyDecl *PDecl = PropRefExpr->getProperty(); - llvm::SmallVector<Expr *, 1> ExprVec; - ExprVec.push_back(newStmt); - - Stmt *Receiver = PropRefExpr->getBase(); - ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(Receiver); - if (PRE && PropGetters[PRE]) { - // This allows us to handle chain/nested property getters. - Receiver = PropGetters[PRE]; - } - if (isa<ObjCSuperExpr>(Receiver)) - MsgExpr = ObjCMessageExpr::Create(*Context, - PDecl->getType().getNonReferenceType(), - /*FIXME?*/SourceLocation(), - Receiver->getLocStart(), - /*IsInstanceSuper=*/true, - cast<Expr>(Receiver)->getType(), - PDecl->getSetterName(), - PDecl->getSetterMethodDecl(), - &ExprVec[0], 1, - /*FIXME:*/SourceLocation()); - else - MsgExpr = ObjCMessageExpr::Create(*Context, - PDecl->getType().getNonReferenceType(), - /*FIXME: */SourceLocation(), - cast<Expr>(Receiver), - PDecl->getSetterName(), - PDecl->getSetterMethodDecl(), - &ExprVec[0], 1, - /*FIXME:*/SourceLocation()); - Stmt *ReplacingStmt = SynthMessageExpr(MsgExpr); - - // Now do the actual rewrite. - ReplaceStmtWithRange(BinOp, ReplacingStmt, SrcRange); - //delete BinOp; - // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references - // to things that stay around. - Context->Deallocate(MsgExpr); - return ReplacingStmt; -} - -Stmt *RewriteObjC::RewritePropertyGetter(ObjCPropertyRefExpr *PropRefExpr) { - // Synthesize a ObjCMessageExpr from a ObjCPropertyRefExpr. - // This allows us to reuse all the fun and games in SynthMessageExpr(). - ObjCMessageExpr *MsgExpr; - ObjCPropertyDecl *PDecl = PropRefExpr->getProperty(); - - Stmt *Receiver = PropRefExpr->getBase(); - - ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(Receiver); - if (PRE && PropGetters[PRE]) { - // This allows us to handle chain/nested property getters. - Receiver = PropGetters[PRE]; - } - - if (isa<ObjCSuperExpr>(Receiver)) - MsgExpr = ObjCMessageExpr::Create(*Context, - PDecl->getType().getNonReferenceType(), - /*FIXME:*/SourceLocation(), - Receiver->getLocStart(), - /*IsInstanceSuper=*/true, - cast<Expr>(Receiver)->getType(), - PDecl->getGetterName(), - PDecl->getGetterMethodDecl(), - 0, 0, - /*FIXME:*/SourceLocation()); - else - MsgExpr = ObjCMessageExpr::Create(*Context, - PDecl->getType().getNonReferenceType(), - /*FIXME:*/SourceLocation(), - cast<Expr>(Receiver), - PDecl->getGetterName(), - PDecl->getGetterMethodDecl(), - 0, 0, - /*FIXME:*/SourceLocation()); - - Stmt *ReplacingStmt = SynthMessageExpr(MsgExpr); - - if (!PropParentMap) - PropParentMap = new ParentMap(CurrentBody); - - Stmt *Parent = PropParentMap->getParent(PropRefExpr); - if (Parent && isa<ObjCPropertyRefExpr>(Parent)) { - // We stash away the ReplacingStmt since actually doing the - // replacement/rewrite won't work for nested getters (e.g. obj.p.i) - PropGetters[PropRefExpr] = ReplacingStmt; - // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references - // to things that stay around. - Context->Deallocate(MsgExpr); - return PropRefExpr; // return the original... - } else { - ReplaceStmt(PropRefExpr, ReplacingStmt); - // delete PropRefExpr; elsewhere... - // NOTE: We don't want to call MsgExpr->Destroy(), as it holds references - // to things that stay around. - Context->Deallocate(MsgExpr); - return ReplacingStmt; - } -} - -Stmt *RewriteObjC::RewriteObjCIvarRefExpr(ObjCIvarRefExpr *IV, - SourceLocation OrigStart, - bool &replaced) { - ObjCIvarDecl *D = IV->getDecl(); - const Expr *BaseExpr = IV->getBase(); - if (CurMethodDef) { - if (BaseExpr->getType()->isObjCObjectPointerType()) { - ObjCInterfaceType *iFaceDecl = - dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType()); - assert(iFaceDecl && "RewriteObjCIvarRefExpr - iFaceDecl is null"); - // lookup which class implements the instance variable. - ObjCInterfaceDecl *clsDeclared = 0; - iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), - clsDeclared); - assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); - - // Synthesize an explicit cast to gain access to the ivar. - std::string RecName = clsDeclared->getIdentifier()->getName(); - RecName += "_IMPL"; - IdentifierInfo *II = &Context->Idents.get(RecName); - RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, - SourceLocation(), II); - assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); - QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); - CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, castT, - CastExpr::CK_Unknown, - IV->getBase()); - // Don't forget the parens to enforce the proper binding. - ParenExpr *PE = new (Context) ParenExpr(IV->getBase()->getLocStart(), - IV->getBase()->getLocEnd(), - castExpr); - replaced = true; - if (IV->isFreeIvar() && - CurMethodDef->getClassInterface() == iFaceDecl->getDecl()) { - MemberExpr *ME = new (Context) MemberExpr(PE, true, D, - IV->getLocation(), - D->getType()); - // delete IV; leak for now, see RewritePropertySetter() usage for more info. - return ME; - } - // Get the new text - // Cannot delete IV->getBase(), since PE points to it. - // Replace the old base with the cast. This is important when doing - // embedded rewrites. For example, [newInv->_container addObject:0]. - IV->setBase(PE); - return IV; - } - } else { // we are outside a method. - assert(!IV->isFreeIvar() && "Cannot have a free standing ivar outside a method"); - - // Explicit ivar refs need to have a cast inserted. - // FIXME: consider sharing some of this code with the code above. - if (BaseExpr->getType()->isObjCObjectPointerType()) { - ObjCInterfaceType *iFaceDecl = - dyn_cast<ObjCInterfaceType>(BaseExpr->getType()->getPointeeType()); - // lookup which class implements the instance variable. - ObjCInterfaceDecl *clsDeclared = 0; - iFaceDecl->getDecl()->lookupInstanceVariable(D->getIdentifier(), - clsDeclared); - assert(clsDeclared && "RewriteObjCIvarRefExpr(): Can't find class"); - - // Synthesize an explicit cast to gain access to the ivar. - std::string RecName = clsDeclared->getIdentifier()->getName(); - RecName += "_IMPL"; - IdentifierInfo *II = &Context->Idents.get(RecName); - RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, - SourceLocation(), II); - assert(RD && "RewriteObjCIvarRefExpr(): Can't find RecordDecl"); - QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); - CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, castT, - CastExpr::CK_Unknown, - IV->getBase()); - // Don't forget the parens to enforce the proper binding. - ParenExpr *PE = new (Context) ParenExpr(IV->getBase()->getLocStart(), - IV->getBase()->getLocEnd(), castExpr); - replaced = true; - // Cannot delete IV->getBase(), since PE points to it. - // Replace the old base with the cast. This is important when doing - // embedded rewrites. For example, [newInv->_container addObject:0]. - IV->setBase(PE); - return IV; - } - } - return IV; -} - -Stmt *RewriteObjC::RewriteObjCNestedIvarRefExpr(Stmt *S, bool &replaced) { - for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); - CI != E; ++CI) { - if (*CI) { - Stmt *newStmt = RewriteObjCNestedIvarRefExpr(*CI, replaced); - if (newStmt) - *CI = newStmt; - } - } - if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S)) { - SourceRange OrigStmtRange = S->getSourceRange(); - Stmt *newStmt = RewriteObjCIvarRefExpr(IvarRefExpr, OrigStmtRange.getBegin(), - replaced); - return newStmt; - } - if (ObjCMessageExpr *MsgRefExpr = dyn_cast<ObjCMessageExpr>(S)) { - Stmt *newStmt = SynthMessageExpr(MsgRefExpr); - return newStmt; - } - return S; -} - -/// SynthCountByEnumWithState - To print: -/// ((unsigned int (*) -/// (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) -/// (void *)objc_msgSend)((id)l_collection, -/// sel_registerName( -/// "countByEnumeratingWithState:objects:count:"), -/// &enumState, -/// (id *)items, (unsigned int)16) -/// -void RewriteObjC::SynthCountByEnumWithState(std::string &buf) { - buf += "((unsigned int (*) (id, SEL, struct __objcFastEnumerationState *, " - "id *, unsigned int))(void *)objc_msgSend)"; - buf += "\n\t\t"; - buf += "((id)l_collection,\n\t\t"; - buf += "sel_registerName(\"countByEnumeratingWithState:objects:count:\"),"; - buf += "\n\t\t"; - buf += "&enumState, " - "(id *)items, (unsigned int)16)"; -} - -/// RewriteBreakStmt - Rewrite for a break-stmt inside an ObjC2's foreach -/// statement to exit to its outer synthesized loop. -/// -Stmt *RewriteObjC::RewriteBreakStmt(BreakStmt *S) { - if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) - return S; - // replace break with goto __break_label - std::string buf; - - SourceLocation startLoc = S->getLocStart(); - buf = "goto __break_label_"; - buf += utostr(ObjCBcLabelNo.back()); - ReplaceText(startLoc, strlen("break"), buf); - - return 0; -} - -/// RewriteContinueStmt - Rewrite for a continue-stmt inside an ObjC2's foreach -/// statement to continue with its inner synthesized loop. -/// -Stmt *RewriteObjC::RewriteContinueStmt(ContinueStmt *S) { - if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) - return S; - // replace continue with goto __continue_label - std::string buf; - - SourceLocation startLoc = S->getLocStart(); - buf = "goto __continue_label_"; - buf += utostr(ObjCBcLabelNo.back()); - ReplaceText(startLoc, strlen("continue"), buf); - - return 0; -} - -/// RewriteObjCForCollectionStmt - Rewriter for ObjC2's foreach statement. -/// It rewrites: -/// for ( type elem in collection) { stmts; } - -/// Into: -/// { -/// type elem; -/// struct __objcFastEnumerationState enumState = { 0 }; -/// id items[16]; -/// id l_collection = (id)collection; -/// unsigned long limit = [l_collection countByEnumeratingWithState:&enumState -/// objects:items count:16]; -/// if (limit) { -/// unsigned long startMutations = *enumState.mutationsPtr; -/// do { -/// unsigned long counter = 0; -/// do { -/// if (startMutations != *enumState.mutationsPtr) -/// objc_enumerationMutation(l_collection); -/// elem = (type)enumState.itemsPtr[counter++]; -/// stmts; -/// __continue_label: ; -/// } while (counter < limit); -/// } while (limit = [l_collection countByEnumeratingWithState:&enumState -/// objects:items count:16]); -/// elem = nil; -/// __break_label: ; -/// } -/// else -/// elem = nil; -/// } -/// -Stmt *RewriteObjC::RewriteObjCForCollectionStmt(ObjCForCollectionStmt *S, - SourceLocation OrigEnd) { - assert(!Stmts.empty() && "ObjCForCollectionStmt - Statement stack empty"); - assert(isa<ObjCForCollectionStmt>(Stmts.back()) && - "ObjCForCollectionStmt Statement stack mismatch"); - assert(!ObjCBcLabelNo.empty() && - "ObjCForCollectionStmt - Label No stack empty"); - - SourceLocation startLoc = S->getLocStart(); - const char *startBuf = SM->getCharacterData(startLoc); - const char *elementName; - std::string elementTypeAsString; - std::string buf; - buf = "\n{\n\t"; - if (DeclStmt *DS = dyn_cast<DeclStmt>(S->getElement())) { - // type elem; - NamedDecl* D = cast<NamedDecl>(DS->getSingleDecl()); - QualType ElementType = cast<ValueDecl>(D)->getType(); - if (ElementType->isObjCQualifiedIdType() || - ElementType->isObjCQualifiedInterfaceType()) - // Simply use 'id' for all qualified types. - elementTypeAsString = "id"; - else - elementTypeAsString = ElementType.getAsString(); - buf += elementTypeAsString; - buf += " "; - elementName = D->getNameAsCString(); - buf += elementName; - buf += ";\n\t"; - } - else { - DeclRefExpr *DR = cast<DeclRefExpr>(S->getElement()); - elementName = DR->getDecl()->getNameAsCString(); - ValueDecl *VD = cast<ValueDecl>(DR->getDecl()); - if (VD->getType()->isObjCQualifiedIdType() || - VD->getType()->isObjCQualifiedInterfaceType()) - // Simply use 'id' for all qualified types. - elementTypeAsString = "id"; - else - elementTypeAsString = VD->getType().getAsString(); - } - - // struct __objcFastEnumerationState enumState = { 0 }; - buf += "struct __objcFastEnumerationState enumState = { 0 };\n\t"; - // id items[16]; - buf += "id items[16];\n\t"; - // id l_collection = (id) - buf += "id l_collection = (id)"; - // Find start location of 'collection' the hard way! - const char *startCollectionBuf = startBuf; - startCollectionBuf += 3; // skip 'for' - startCollectionBuf = strchr(startCollectionBuf, '('); - startCollectionBuf++; // skip '(' - // find 'in' and skip it. - while (*startCollectionBuf != ' ' || - *(startCollectionBuf+1) != 'i' || *(startCollectionBuf+2) != 'n' || - (*(startCollectionBuf+3) != ' ' && - *(startCollectionBuf+3) != '[' && *(startCollectionBuf+3) != '(')) - startCollectionBuf++; - startCollectionBuf += 3; - - // Replace: "for (type element in" with string constructed thus far. - ReplaceText(startLoc, startCollectionBuf - startBuf, buf); - // Replace ')' in for '(' type elem in collection ')' with ';' - SourceLocation rightParenLoc = S->getRParenLoc(); - const char *rparenBuf = SM->getCharacterData(rightParenLoc); - SourceLocation lparenLoc = startLoc.getFileLocWithOffset(rparenBuf-startBuf); - buf = ";\n\t"; - - // unsigned long limit = [l_collection countByEnumeratingWithState:&enumState - // objects:items count:16]; - // which is synthesized into: - // unsigned int limit = - // ((unsigned int (*) - // (id, SEL, struct __objcFastEnumerationState *, id *, unsigned int)) - // (void *)objc_msgSend)((id)l_collection, - // sel_registerName( - // "countByEnumeratingWithState:objects:count:"), - // (struct __objcFastEnumerationState *)&state, - // (id *)items, (unsigned int)16); - buf += "unsigned long limit =\n\t\t"; - SynthCountByEnumWithState(buf); - buf += ";\n\t"; - /// if (limit) { - /// unsigned long startMutations = *enumState.mutationsPtr; - /// do { - /// unsigned long counter = 0; - /// do { - /// if (startMutations != *enumState.mutationsPtr) - /// objc_enumerationMutation(l_collection); - /// elem = (type)enumState.itemsPtr[counter++]; - buf += "if (limit) {\n\t"; - buf += "unsigned long startMutations = *enumState.mutationsPtr;\n\t"; - buf += "do {\n\t\t"; - buf += "unsigned long counter = 0;\n\t\t"; - buf += "do {\n\t\t\t"; - buf += "if (startMutations != *enumState.mutationsPtr)\n\t\t\t\t"; - buf += "objc_enumerationMutation(l_collection);\n\t\t\t"; - buf += elementName; - buf += " = ("; - buf += elementTypeAsString; - buf += ")enumState.itemsPtr[counter++];"; - // Replace ')' in for '(' type elem in collection ')' with all of these. - ReplaceText(lparenLoc, 1, buf); - - /// __continue_label: ; - /// } while (counter < limit); - /// } while (limit = [l_collection countByEnumeratingWithState:&enumState - /// objects:items count:16]); - /// elem = nil; - /// __break_label: ; - /// } - /// else - /// elem = nil; - /// } - /// - buf = ";\n\t"; - buf += "__continue_label_"; - buf += utostr(ObjCBcLabelNo.back()); - buf += ": ;"; - buf += "\n\t\t"; - buf += "} while (counter < limit);\n\t"; - buf += "} while (limit = "; - SynthCountByEnumWithState(buf); - buf += ");\n\t"; - buf += elementName; - buf += " = (("; - buf += elementTypeAsString; - buf += ")0);\n\t"; - buf += "__break_label_"; - buf += utostr(ObjCBcLabelNo.back()); - buf += ": ;\n\t"; - buf += "}\n\t"; - buf += "else\n\t\t"; - buf += elementName; - buf += " = (("; - buf += elementTypeAsString; - buf += ")0);\n\t"; - buf += "}\n"; - - // Insert all these *after* the statement body. - // FIXME: If this should support Obj-C++, support CXXTryStmt - if (isa<CompoundStmt>(S->getBody())) { - SourceLocation endBodyLoc = OrigEnd.getFileLocWithOffset(1); - InsertText(endBodyLoc, buf); - } else { - /* Need to treat single statements specially. For example: - * - * for (A *a in b) if (stuff()) break; - * for (A *a in b) xxxyy; - * - * The following code simply scans ahead to the semi to find the actual end. - */ - const char *stmtBuf = SM->getCharacterData(OrigEnd); - const char *semiBuf = strchr(stmtBuf, ';'); - assert(semiBuf && "Can't find ';'"); - SourceLocation endBodyLoc = OrigEnd.getFileLocWithOffset(semiBuf-stmtBuf+1); - InsertText(endBodyLoc, buf); - } - Stmts.pop_back(); - ObjCBcLabelNo.pop_back(); - return 0; -} - -/// RewriteObjCSynchronizedStmt - -/// This routine rewrites @synchronized(expr) stmt; -/// into: -/// objc_sync_enter(expr); -/// @try stmt @finally { objc_sync_exit(expr); } -/// -Stmt *RewriteObjC::RewriteObjCSynchronizedStmt(ObjCAtSynchronizedStmt *S) { - // Get the start location and compute the semi location. - SourceLocation startLoc = S->getLocStart(); - const char *startBuf = SM->getCharacterData(startLoc); - - assert((*startBuf == '@') && "bogus @synchronized location"); - - std::string buf; - buf = "objc_sync_enter((id)"; - const char *lparenBuf = startBuf; - while (*lparenBuf != '(') lparenBuf++; - ReplaceText(startLoc, lparenBuf-startBuf+1, buf); - // We can't use S->getSynchExpr()->getLocEnd() to find the end location, since - // the sync expression is typically a message expression that's already - // been rewritten! (which implies the SourceLocation's are invalid). - SourceLocation endLoc = S->getSynchBody()->getLocStart(); - const char *endBuf = SM->getCharacterData(endLoc); - while (*endBuf != ')') endBuf--; - SourceLocation rparenLoc = startLoc.getFileLocWithOffset(endBuf-startBuf); - buf = ");\n"; - // declare a new scope with two variables, _stack and _rethrow. - buf += "/* @try scope begin */ \n{ struct _objc_exception_data {\n"; - buf += "int buf[18/*32-bit i386*/];\n"; - buf += "char *pointers[4];} _stack;\n"; - buf += "id volatile _rethrow = 0;\n"; - buf += "objc_exception_try_enter(&_stack);\n"; - buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; - ReplaceText(rparenLoc, 1, buf); - startLoc = S->getSynchBody()->getLocEnd(); - startBuf = SM->getCharacterData(startLoc); - - assert((*startBuf == '}') && "bogus @synchronized block"); - SourceLocation lastCurlyLoc = startLoc; - buf = "}\nelse {\n"; - buf += " _rethrow = objc_exception_extract(&_stack);\n"; - buf += "}\n"; - buf += "{ /* implicit finally clause */\n"; - buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; - - std::string syncBuf; - syncBuf += " objc_sync_exit("; - Expr *syncExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), - CastExpr::CK_Unknown, - S->getSynchExpr()); - std::string syncExprBufS; - llvm::raw_string_ostream syncExprBuf(syncExprBufS); - syncExpr->printPretty(syncExprBuf, *Context, 0, - PrintingPolicy(LangOpts)); - syncBuf += syncExprBuf.str(); - syncBuf += ");"; - - buf += syncBuf; - buf += "\n if (_rethrow) objc_exception_throw(_rethrow);\n"; - buf += "}\n"; - buf += "}"; - - ReplaceText(lastCurlyLoc, 1, buf); - - bool hasReturns = false; - HasReturnStmts(S->getSynchBody(), hasReturns); - if (hasReturns) - RewriteSyncReturnStmts(S->getSynchBody(), syncBuf); - - return 0; -} - -void RewriteObjC::WarnAboutReturnGotoStmts(Stmt *S) -{ - // Perform a bottom up traversal of all children. - for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); - CI != E; ++CI) - if (*CI) - WarnAboutReturnGotoStmts(*CI); - - if (isa<ReturnStmt>(S) || isa<GotoStmt>(S)) { - Diags.Report(Context->getFullLoc(S->getLocStart()), - TryFinallyContainsReturnDiag); - } - return; -} - -void RewriteObjC::HasReturnStmts(Stmt *S, bool &hasReturns) -{ - // Perform a bottom up traversal of all children. - for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); - CI != E; ++CI) - if (*CI) - HasReturnStmts(*CI, hasReturns); - - if (isa<ReturnStmt>(S)) - hasReturns = true; - return; -} - -void RewriteObjC::RewriteTryReturnStmts(Stmt *S) { - // Perform a bottom up traversal of all children. - for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); - CI != E; ++CI) - if (*CI) { - RewriteTryReturnStmts(*CI); - } - if (isa<ReturnStmt>(S)) { - SourceLocation startLoc = S->getLocStart(); - const char *startBuf = SM->getCharacterData(startLoc); - - const char *semiBuf = strchr(startBuf, ';'); - assert((*semiBuf == ';') && "RewriteTryReturnStmts: can't find ';'"); - SourceLocation onePastSemiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf+1); - - std::string buf; - buf = "{ objc_exception_try_exit(&_stack); return"; - - ReplaceText(startLoc, 6, buf); - InsertText(onePastSemiLoc, "}"); - } - return; -} - -void RewriteObjC::RewriteSyncReturnStmts(Stmt *S, std::string syncExitBuf) { - // Perform a bottom up traversal of all children. - for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); - CI != E; ++CI) - if (*CI) { - RewriteSyncReturnStmts(*CI, syncExitBuf); - } - if (isa<ReturnStmt>(S)) { - SourceLocation startLoc = S->getLocStart(); - const char *startBuf = SM->getCharacterData(startLoc); - - const char *semiBuf = strchr(startBuf, ';'); - assert((*semiBuf == ';') && "RewriteSyncReturnStmts: can't find ';'"); - SourceLocation onePastSemiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf+1); - - std::string buf; - buf = "{ objc_exception_try_exit(&_stack);"; - buf += syncExitBuf; - buf += " return"; - - ReplaceText(startLoc, 6, buf); - InsertText(onePastSemiLoc, "}"); - } - return; -} - -Stmt *RewriteObjC::RewriteObjCTryStmt(ObjCAtTryStmt *S) { - // Get the start location and compute the semi location. - SourceLocation startLoc = S->getLocStart(); - const char *startBuf = SM->getCharacterData(startLoc); - - assert((*startBuf == '@') && "bogus @try location"); - - std::string buf; - // declare a new scope with two variables, _stack and _rethrow. - buf = "/* @try scope begin */ { struct _objc_exception_data {\n"; - buf += "int buf[18/*32-bit i386*/];\n"; - buf += "char *pointers[4];} _stack;\n"; - buf += "id volatile _rethrow = 0;\n"; - buf += "objc_exception_try_enter(&_stack);\n"; - buf += "if (!_setjmp(_stack.buf)) /* @try block continue */\n"; - - ReplaceText(startLoc, 4, buf); - - startLoc = S->getTryBody()->getLocEnd(); - startBuf = SM->getCharacterData(startLoc); - - assert((*startBuf == '}') && "bogus @try block"); - - SourceLocation lastCurlyLoc = startLoc; - if (S->getNumCatchStmts()) { - startLoc = startLoc.getFileLocWithOffset(1); - buf = " /* @catch begin */ else {\n"; - buf += " id _caught = objc_exception_extract(&_stack);\n"; - buf += " objc_exception_try_enter (&_stack);\n"; - buf += " if (_setjmp(_stack.buf))\n"; - buf += " _rethrow = objc_exception_extract(&_stack);\n"; - buf += " else { /* @catch continue */"; - - InsertText(startLoc, buf); - } else { /* no catch list */ - buf = "}\nelse {\n"; - buf += " _rethrow = objc_exception_extract(&_stack);\n"; - buf += "}"; - ReplaceText(lastCurlyLoc, 1, buf); - } - bool sawIdTypedCatch = false; - Stmt *lastCatchBody = 0; - for (unsigned I = 0, N = S->getNumCatchStmts(); I != N; ++I) { - ObjCAtCatchStmt *Catch = S->getCatchStmt(I); - VarDecl *catchDecl = Catch->getCatchParamDecl(); - - if (I == 0) - buf = "if ("; // we are generating code for the first catch clause - else - buf = "else if ("; - startLoc = Catch->getLocStart(); - startBuf = SM->getCharacterData(startLoc); - - assert((*startBuf == '@') && "bogus @catch location"); - - const char *lParenLoc = strchr(startBuf, '('); - - if (Catch->hasEllipsis()) { - // Now rewrite the body... - lastCatchBody = Catch->getCatchBody(); - SourceLocation bodyLoc = lastCatchBody->getLocStart(); - const char *bodyBuf = SM->getCharacterData(bodyLoc); - assert(*SM->getCharacterData(Catch->getRParenLoc()) == ')' && - "bogus @catch paren location"); - assert((*bodyBuf == '{') && "bogus @catch body location"); - - buf += "1) { id _tmp = _caught;"; - Rewrite.ReplaceText(startLoc, bodyBuf-startBuf+1, buf); - } else if (catchDecl) { - QualType t = catchDecl->getType(); - if (t == Context->getObjCIdType()) { - buf += "1) { "; - ReplaceText(startLoc, lParenLoc-startBuf+1, buf); - sawIdTypedCatch = true; - } else if (const ObjCObjectPointerType *Ptr = - t->getAs<ObjCObjectPointerType>()) { - // Should be a pointer to a class. - ObjCInterfaceDecl *IDecl = Ptr->getObjectType()->getInterface(); - if (IDecl) { - buf += "objc_exception_match((struct objc_class *)objc_getClass(\""; - buf += IDecl->getNameAsString(); - buf += "\"), (struct objc_object *)_caught)) { "; - ReplaceText(startLoc, lParenLoc-startBuf+1, buf); - } - } - // Now rewrite the body... - lastCatchBody = Catch->getCatchBody(); - SourceLocation rParenLoc = Catch->getRParenLoc(); - SourceLocation bodyLoc = lastCatchBody->getLocStart(); - const char *bodyBuf = SM->getCharacterData(bodyLoc); - const char *rParenBuf = SM->getCharacterData(rParenLoc); - assert((*rParenBuf == ')') && "bogus @catch paren location"); - assert((*bodyBuf == '{') && "bogus @catch body location"); - - // Here we replace ") {" with "= _caught;" (which initializes and - // declares the @catch parameter). - ReplaceText(rParenLoc, bodyBuf-rParenBuf+1, " = _caught;"); - } else { - assert(false && "@catch rewrite bug"); - } - } - // Complete the catch list... - if (lastCatchBody) { - SourceLocation bodyLoc = lastCatchBody->getLocEnd(); - assert(*SM->getCharacterData(bodyLoc) == '}' && - "bogus @catch body location"); - - // Insert the last (implicit) else clause *before* the right curly brace. - bodyLoc = bodyLoc.getFileLocWithOffset(-1); - buf = "} /* last catch end */\n"; - buf += "else {\n"; - buf += " _rethrow = _caught;\n"; - buf += " objc_exception_try_exit(&_stack);\n"; - buf += "} } /* @catch end */\n"; - if (!S->getFinallyStmt()) - buf += "}\n"; - InsertText(bodyLoc, buf); - - // Set lastCurlyLoc - lastCurlyLoc = lastCatchBody->getLocEnd(); - } - if (ObjCAtFinallyStmt *finalStmt = S->getFinallyStmt()) { - startLoc = finalStmt->getLocStart(); - startBuf = SM->getCharacterData(startLoc); - assert((*startBuf == '@') && "bogus @finally start"); - - ReplaceText(startLoc, 8, "/* @finally */"); - - Stmt *body = finalStmt->getFinallyBody(); - SourceLocation startLoc = body->getLocStart(); - SourceLocation endLoc = body->getLocEnd(); - assert(*SM->getCharacterData(startLoc) == '{' && - "bogus @finally body location"); - assert(*SM->getCharacterData(endLoc) == '}' && - "bogus @finally body location"); - - startLoc = startLoc.getFileLocWithOffset(1); - InsertText(startLoc, " if (!_rethrow) objc_exception_try_exit(&_stack);\n"); - endLoc = endLoc.getFileLocWithOffset(-1); - InsertText(endLoc, " if (_rethrow) objc_exception_throw(_rethrow);\n"); - - // Set lastCurlyLoc - lastCurlyLoc = body->getLocEnd(); - - // Now check for any return/continue/go statements within the @try. - WarnAboutReturnGotoStmts(S->getTryBody()); - } else { /* no finally clause - make sure we synthesize an implicit one */ - buf = "{ /* implicit finally clause */\n"; - buf += " if (!_rethrow) objc_exception_try_exit(&_stack);\n"; - buf += " if (_rethrow) objc_exception_throw(_rethrow);\n"; - buf += "}"; - ReplaceText(lastCurlyLoc, 1, buf); - - // Now check for any return/continue/go statements within the @try. - // The implicit finally clause won't called if the @try contains any - // jump statements. - bool hasReturns = false; - HasReturnStmts(S->getTryBody(), hasReturns); - if (hasReturns) - RewriteTryReturnStmts(S->getTryBody()); - } - // Now emit the final closing curly brace... - lastCurlyLoc = lastCurlyLoc.getFileLocWithOffset(1); - InsertText(lastCurlyLoc, " } /* @try scope end */\n"); - return 0; -} - -Stmt *RewriteObjC::RewriteObjCCatchStmt(ObjCAtCatchStmt *S) { - return 0; -} - -Stmt *RewriteObjC::RewriteObjCFinallyStmt(ObjCAtFinallyStmt *S) { - return 0; -} - -// This can't be done with ReplaceStmt(S, ThrowExpr), since -// the throw expression is typically a message expression that's already -// been rewritten! (which implies the SourceLocation's are invalid). -Stmt *RewriteObjC::RewriteObjCThrowStmt(ObjCAtThrowStmt *S) { - // Get the start location and compute the semi location. - SourceLocation startLoc = S->getLocStart(); - const char *startBuf = SM->getCharacterData(startLoc); - - assert((*startBuf == '@') && "bogus @throw location"); - - std::string buf; - /* void objc_exception_throw(id) __attribute__((noreturn)); */ - if (S->getThrowExpr()) - buf = "objc_exception_throw("; - else // add an implicit argument - buf = "objc_exception_throw(_caught"; - - // handle "@ throw" correctly. - const char *wBuf = strchr(startBuf, 'w'); - assert((*wBuf == 'w') && "@throw: can't find 'w'"); - ReplaceText(startLoc, wBuf-startBuf+1, buf); - - const char *semiBuf = strchr(startBuf, ';'); - assert((*semiBuf == ';') && "@throw: can't find ';'"); - SourceLocation semiLoc = startLoc.getFileLocWithOffset(semiBuf-startBuf); - ReplaceText(semiLoc, 1, ");"); - return 0; -} - -Stmt *RewriteObjC::RewriteAtEncode(ObjCEncodeExpr *Exp) { - // Create a new string expression. - QualType StrType = Context->getPointerType(Context->CharTy); - std::string StrEncoding; - Context->getObjCEncodingForType(Exp->getEncodedType(), StrEncoding); - Expr *Replacement = StringLiteral::Create(*Context,StrEncoding.c_str(), - StrEncoding.length(), false,StrType, - SourceLocation()); - ReplaceStmt(Exp, Replacement); - - // Replace this subexpr in the parent. - // delete Exp; leak for now, see RewritePropertySetter() usage for more info. - return Replacement; -} - -Stmt *RewriteObjC::RewriteAtSelector(ObjCSelectorExpr *Exp) { - if (!SelGetUidFunctionDecl) - SynthSelGetUidFunctionDecl(); - assert(SelGetUidFunctionDecl && "Can't find sel_registerName() decl"); - // Create a call to sel_registerName("selName"). - llvm::SmallVector<Expr*, 8> SelExprs; - QualType argType = Context->getPointerType(Context->CharTy); - SelExprs.push_back(StringLiteral::Create(*Context, - Exp->getSelector().getAsString().c_str(), - Exp->getSelector().getAsString().size(), - false, argType, SourceLocation())); - CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, - &SelExprs[0], SelExprs.size()); - ReplaceStmt(Exp, SelExp); - // delete Exp; leak for now, see RewritePropertySetter() usage for more info. - return SelExp; -} - -CallExpr *RewriteObjC::SynthesizeCallToFunctionDecl( - FunctionDecl *FD, Expr **args, unsigned nargs, SourceLocation StartLoc, - SourceLocation EndLoc) { - // Get the type, we will need to reference it in a couple spots. - QualType msgSendType = FD->getType(); - - // Create a reference to the objc_msgSend() declaration. - DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, msgSendType, SourceLocation()); - - // Now, we cast the reference to a pointer to the objc_msgSend type. - QualType pToFunc = Context->getPointerType(msgSendType); - ImplicitCastExpr *ICE = - new (Context) ImplicitCastExpr(pToFunc, CastExpr::CK_Unknown, - DRE, CXXBaseSpecifierArray(), - /*isLvalue=*/false); - - const FunctionType *FT = msgSendType->getAs<FunctionType>(); - - CallExpr *Exp = - new (Context) CallExpr(*Context, ICE, args, nargs, FT->getResultType(), - EndLoc); - return Exp; -} - -static bool scanForProtocolRefs(const char *startBuf, const char *endBuf, - const char *&startRef, const char *&endRef) { - while (startBuf < endBuf) { - if (*startBuf == '<') - startRef = startBuf; // mark the start. - if (*startBuf == '>') { - if (startRef && *startRef == '<') { - endRef = startBuf; // mark the end. - return true; - } - return false; - } - startBuf++; - } - return false; -} - -static void scanToNextArgument(const char *&argRef) { - int angle = 0; - while (*argRef != ')' && (*argRef != ',' || angle > 0)) { - if (*argRef == '<') - angle++; - else if (*argRef == '>') - angle--; - argRef++; - } - assert(angle == 0 && "scanToNextArgument - bad protocol type syntax"); -} - -bool RewriteObjC::needToScanForQualifiers(QualType T) { - if (T->isObjCQualifiedIdType()) - return true; - if (const PointerType *PT = T->getAs<PointerType>()) { - if (PT->getPointeeType()->isObjCQualifiedIdType()) - return true; - } - if (T->isObjCObjectPointerType()) { - T = T->getPointeeType(); - return T->isObjCQualifiedInterfaceType(); - } - return false; -} - -void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Expr *E) { - QualType Type = E->getType(); - if (needToScanForQualifiers(Type)) { - SourceLocation Loc, EndLoc; - - if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) { - Loc = ECE->getLParenLoc(); - EndLoc = ECE->getRParenLoc(); - } else { - Loc = E->getLocStart(); - EndLoc = E->getLocEnd(); - } - // This will defend against trying to rewrite synthesized expressions. - if (Loc.isInvalid() || EndLoc.isInvalid()) - return; - - const char *startBuf = SM->getCharacterData(Loc); - const char *endBuf = SM->getCharacterData(EndLoc); - const char *startRef = 0, *endRef = 0; - if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { - // Get the locations of the startRef, endRef. - SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-startBuf); - SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-startBuf+1); - // Comment out the protocol references. - InsertText(LessLoc, "/*"); - InsertText(GreaterLoc, "*/"); - } - } -} - -void RewriteObjC::RewriteObjCQualifiedInterfaceTypes(Decl *Dcl) { - SourceLocation Loc; - QualType Type; - const FunctionProtoType *proto = 0; - if (VarDecl *VD = dyn_cast<VarDecl>(Dcl)) { - Loc = VD->getLocation(); - Type = VD->getType(); - } - else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(Dcl)) { - Loc = FD->getLocation(); - // Check for ObjC 'id' and class types that have been adorned with protocol - // information (id<p>, C<p>*). The protocol references need to be rewritten! - const FunctionType *funcType = FD->getType()->getAs<FunctionType>(); - assert(funcType && "missing function type"); - proto = dyn_cast<FunctionProtoType>(funcType); - if (!proto) - return; - Type = proto->getResultType(); - } - else if (FieldDecl *FD = dyn_cast<FieldDecl>(Dcl)) { - Loc = FD->getLocation(); - Type = FD->getType(); - } - else - return; - - if (needToScanForQualifiers(Type)) { - // Since types are unique, we need to scan the buffer. - - const char *endBuf = SM->getCharacterData(Loc); - const char *startBuf = endBuf; - while (*startBuf != ';' && *startBuf != '<' && startBuf != MainFileStart) - startBuf--; // scan backward (from the decl location) for return type. - const char *startRef = 0, *endRef = 0; - if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { - // Get the locations of the startRef, endRef. - SourceLocation LessLoc = Loc.getFileLocWithOffset(startRef-endBuf); - SourceLocation GreaterLoc = Loc.getFileLocWithOffset(endRef-endBuf+1); - // Comment out the protocol references. - InsertText(LessLoc, "/*"); - InsertText(GreaterLoc, "*/"); - } - } - if (!proto) - return; // most likely, was a variable - // Now check arguments. - const char *startBuf = SM->getCharacterData(Loc); - const char *startFuncBuf = startBuf; - for (unsigned i = 0; i < proto->getNumArgs(); i++) { - if (needToScanForQualifiers(proto->getArgType(i))) { - // Since types are unique, we need to scan the buffer. - - const char *endBuf = startBuf; - // scan forward (from the decl location) for argument types. - scanToNextArgument(endBuf); - const char *startRef = 0, *endRef = 0; - if (scanForProtocolRefs(startBuf, endBuf, startRef, endRef)) { - // Get the locations of the startRef, endRef. - SourceLocation LessLoc = - Loc.getFileLocWithOffset(startRef-startFuncBuf); - SourceLocation GreaterLoc = - Loc.getFileLocWithOffset(endRef-startFuncBuf+1); - // Comment out the protocol references. - InsertText(LessLoc, "/*"); - InsertText(GreaterLoc, "*/"); - } - startBuf = ++endBuf; - } - else { - // If the function name is derived from a macro expansion, then the - // argument buffer will not follow the name. Need to speak with Chris. - while (*startBuf && *startBuf != ')' && *startBuf != ',') - startBuf++; // scan forward (from the decl location) for argument types. - startBuf++; - } - } -} - -void RewriteObjC::RewriteTypeOfDecl(VarDecl *ND) { - QualType QT = ND->getType(); - const Type* TypePtr = QT->getAs<Type>(); - if (!isa<TypeOfExprType>(TypePtr)) - return; - while (isa<TypeOfExprType>(TypePtr)) { - const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr); - QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType(); - TypePtr = QT->getAs<Type>(); - } - // FIXME. This will not work for multiple declarators; as in: - // __typeof__(a) b,c,d; - std::string TypeAsString(QT.getAsString()); - SourceLocation DeclLoc = ND->getTypeSpecStartLoc(); - const char *startBuf = SM->getCharacterData(DeclLoc); - if (ND->getInit()) { - std::string Name(ND->getNameAsString()); - TypeAsString += " " + Name + " = "; - Expr *E = ND->getInit(); - SourceLocation startLoc; - if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) - startLoc = ECE->getLParenLoc(); - else - startLoc = E->getLocStart(); - startLoc = SM->getInstantiationLoc(startLoc); - const char *endBuf = SM->getCharacterData(startLoc); - ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString); - } - else { - SourceLocation X = ND->getLocEnd(); - X = SM->getInstantiationLoc(X); - const char *endBuf = SM->getCharacterData(X); - ReplaceText(DeclLoc, endBuf-startBuf-1, TypeAsString); - } -} - -// SynthSelGetUidFunctionDecl - SEL sel_registerName(const char *str); -void RewriteObjC::SynthSelGetUidFunctionDecl() { - IdentifierInfo *SelGetUidIdent = &Context->Idents.get("sel_registerName"); - llvm::SmallVector<QualType, 16> ArgTys; - ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); - QualType getFuncType = Context->getFunctionType(Context->getObjCSelType(), - &ArgTys[0], ArgTys.size(), - false /*isVariadic*/, 0, - false, false, 0, 0, - FunctionType::ExtInfo()); - SelGetUidFunctionDecl = FunctionDecl::Create(*Context, TUDecl, - SourceLocation(), - SelGetUidIdent, getFuncType, 0, - FunctionDecl::Extern, - FunctionDecl::None, false); -} - -void RewriteObjC::RewriteFunctionDecl(FunctionDecl *FD) { - // declared in <objc/objc.h> - if (FD->getIdentifier() && - strcmp(FD->getNameAsCString(), "sel_registerName") == 0) { - SelGetUidFunctionDecl = FD; - return; - } - RewriteObjCQualifiedInterfaceTypes(FD); -} - -static void RewriteBlockPointerType(std::string& Str, QualType Type) { - std::string TypeString(Type.getAsString()); - const char *argPtr = TypeString.c_str(); - if (!strchr(argPtr, '^')) { - Str += TypeString; - return; - } - while (*argPtr) { - Str += (*argPtr == '^' ? '*' : *argPtr); - argPtr++; - } -} - -// FIXME. Consolidate this routine with RewriteBlockPointerType. -static void RewriteBlockPointerTypeVariable(std::string& Str, ValueDecl *VD) { - QualType Type = VD->getType(); - std::string TypeString(Type.getAsString()); - const char *argPtr = TypeString.c_str(); - int paren = 0; - while (*argPtr) { - switch (*argPtr) { - case '(': - Str += *argPtr; - paren++; - break; - case ')': - Str += *argPtr; - paren--; - break; - case '^': - Str += '*'; - if (paren == 1) - Str += VD->getNameAsString(); - break; - default: - Str += *argPtr; - break; - } - argPtr++; - } -} - - -void RewriteObjC::RewriteBlockLiteralFunctionDecl(FunctionDecl *FD) { - SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); - const FunctionType *funcType = FD->getType()->getAs<FunctionType>(); - const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(funcType); - if (!proto) - return; - QualType Type = proto->getResultType(); - std::string FdStr = Type.getAsString(); - FdStr += " "; - FdStr += FD->getNameAsCString(); - FdStr += "("; - unsigned numArgs = proto->getNumArgs(); - for (unsigned i = 0; i < numArgs; i++) { - QualType ArgType = proto->getArgType(i); - RewriteBlockPointerType(FdStr, ArgType); - if (i+1 < numArgs) - FdStr += ", "; - } - FdStr += ");\n"; - InsertText(FunLocStart, FdStr); - CurFunctionDeclToDeclareForBlock = 0; -} - -// SynthSuperContructorFunctionDecl - id objc_super(id obj, id super); -void RewriteObjC::SynthSuperContructorFunctionDecl() { - if (SuperContructorFunctionDecl) - return; - IdentifierInfo *msgSendIdent = &Context->Idents.get("__rw_objc_super"); - llvm::SmallVector<QualType, 16> ArgTys; - QualType argT = Context->getObjCIdType(); - assert(!argT.isNull() && "Can't find 'id' type"); - ArgTys.push_back(argT); - ArgTys.push_back(argT); - QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), - &ArgTys[0], ArgTys.size(), - false, 0, - false, false, 0, 0, - FunctionType::ExtInfo()); - SuperContructorFunctionDecl = FunctionDecl::Create(*Context, TUDecl, - SourceLocation(), - msgSendIdent, msgSendType, 0, - FunctionDecl::Extern, - FunctionDecl::None, false); -} - -// SynthMsgSendFunctionDecl - id objc_msgSend(id self, SEL op, ...); -void RewriteObjC::SynthMsgSendFunctionDecl() { - IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend"); - llvm::SmallVector<QualType, 16> ArgTys; - QualType argT = Context->getObjCIdType(); - assert(!argT.isNull() && "Can't find 'id' type"); - ArgTys.push_back(argT); - argT = Context->getObjCSelType(); - assert(!argT.isNull() && "Can't find 'SEL' type"); - ArgTys.push_back(argT); - QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), - &ArgTys[0], ArgTys.size(), - true /*isVariadic*/, 0, - false, false, 0, 0, - FunctionType::ExtInfo()); - MsgSendFunctionDecl = FunctionDecl::Create(*Context, TUDecl, - SourceLocation(), - msgSendIdent, msgSendType, 0, - FunctionDecl::Extern, - FunctionDecl::None, false); -} - -// SynthMsgSendSuperFunctionDecl - id objc_msgSendSuper(struct objc_super *, SEL op, ...); -void RewriteObjC::SynthMsgSendSuperFunctionDecl() { - IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSendSuper"); - llvm::SmallVector<QualType, 16> ArgTys; - RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, - SourceLocation(), - &Context->Idents.get("objc_super")); - QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); - assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); - ArgTys.push_back(argT); - argT = Context->getObjCSelType(); - assert(!argT.isNull() && "Can't find 'SEL' type"); - ArgTys.push_back(argT); - QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), - &ArgTys[0], ArgTys.size(), - true /*isVariadic*/, 0, - false, false, 0, 0, - FunctionType::ExtInfo()); - MsgSendSuperFunctionDecl = FunctionDecl::Create(*Context, TUDecl, - SourceLocation(), - msgSendIdent, msgSendType, 0, - FunctionDecl::Extern, - FunctionDecl::None, false); -} - -// SynthMsgSendStretFunctionDecl - id objc_msgSend_stret(id self, SEL op, ...); -void RewriteObjC::SynthMsgSendStretFunctionDecl() { - IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_stret"); - llvm::SmallVector<QualType, 16> ArgTys; - QualType argT = Context->getObjCIdType(); - assert(!argT.isNull() && "Can't find 'id' type"); - ArgTys.push_back(argT); - argT = Context->getObjCSelType(); - assert(!argT.isNull() && "Can't find 'SEL' type"); - ArgTys.push_back(argT); - QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), - &ArgTys[0], ArgTys.size(), - true /*isVariadic*/, 0, - false, false, 0, 0, - FunctionType::ExtInfo()); - MsgSendStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, - SourceLocation(), - msgSendIdent, msgSendType, 0, - FunctionDecl::Extern, - FunctionDecl::None, false); -} - -// SynthMsgSendSuperStretFunctionDecl - -// id objc_msgSendSuper_stret(struct objc_super *, SEL op, ...); -void RewriteObjC::SynthMsgSendSuperStretFunctionDecl() { - IdentifierInfo *msgSendIdent = - &Context->Idents.get("objc_msgSendSuper_stret"); - llvm::SmallVector<QualType, 16> ArgTys; - RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, - SourceLocation(), - &Context->Idents.get("objc_super")); - QualType argT = Context->getPointerType(Context->getTagDeclType(RD)); - assert(!argT.isNull() && "Can't build 'struct objc_super *' type"); - ArgTys.push_back(argT); - argT = Context->getObjCSelType(); - assert(!argT.isNull() && "Can't find 'SEL' type"); - ArgTys.push_back(argT); - QualType msgSendType = Context->getFunctionType(Context->getObjCIdType(), - &ArgTys[0], ArgTys.size(), - true /*isVariadic*/, 0, - false, false, 0, 0, - FunctionType::ExtInfo()); - MsgSendSuperStretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, - SourceLocation(), - msgSendIdent, msgSendType, 0, - FunctionDecl::Extern, - FunctionDecl::None, false); -} - -// SynthMsgSendFpretFunctionDecl - double objc_msgSend_fpret(id self, SEL op, ...); -void RewriteObjC::SynthMsgSendFpretFunctionDecl() { - IdentifierInfo *msgSendIdent = &Context->Idents.get("objc_msgSend_fpret"); - llvm::SmallVector<QualType, 16> ArgTys; - QualType argT = Context->getObjCIdType(); - assert(!argT.isNull() && "Can't find 'id' type"); - ArgTys.push_back(argT); - argT = Context->getObjCSelType(); - assert(!argT.isNull() && "Can't find 'SEL' type"); - ArgTys.push_back(argT); - QualType msgSendType = Context->getFunctionType(Context->DoubleTy, - &ArgTys[0], ArgTys.size(), - true /*isVariadic*/, 0, - false, false, 0, 0, - FunctionType::ExtInfo()); - MsgSendFpretFunctionDecl = FunctionDecl::Create(*Context, TUDecl, - SourceLocation(), - msgSendIdent, msgSendType, 0, - FunctionDecl::Extern, - FunctionDecl::None, false); -} - -// SynthGetClassFunctionDecl - id objc_getClass(const char *name); -void RewriteObjC::SynthGetClassFunctionDecl() { - IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getClass"); - llvm::SmallVector<QualType, 16> ArgTys; - ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); - QualType getClassType = Context->getFunctionType(Context->getObjCIdType(), - &ArgTys[0], ArgTys.size(), - false /*isVariadic*/, 0, - false, false, 0, 0, - FunctionType::ExtInfo()); - GetClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, - SourceLocation(), - getClassIdent, getClassType, 0, - FunctionDecl::Extern, - FunctionDecl::None, false); -} - -// SynthGetSuperClassFunctionDecl - Class class_getSuperclass(Class cls); -void RewriteObjC::SynthGetSuperClassFunctionDecl() { - IdentifierInfo *getSuperClassIdent = - &Context->Idents.get("class_getSuperclass"); - llvm::SmallVector<QualType, 16> ArgTys; - ArgTys.push_back(Context->getObjCClassType()); - QualType getClassType = Context->getFunctionType(Context->getObjCClassType(), - &ArgTys[0], ArgTys.size(), - false /*isVariadic*/, 0, - false, false, 0, 0, - FunctionType::ExtInfo()); - GetSuperClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, - SourceLocation(), - getSuperClassIdent, - getClassType, 0, - FunctionDecl::Extern, - FunctionDecl::None, - false); -} - -// SynthGetMetaClassFunctionDecl - id objc_getClass(const char *name); -void RewriteObjC::SynthGetMetaClassFunctionDecl() { - IdentifierInfo *getClassIdent = &Context->Idents.get("objc_getMetaClass"); - llvm::SmallVector<QualType, 16> ArgTys; - ArgTys.push_back(Context->getPointerType(Context->CharTy.withConst())); - QualType getClassType = Context->getFunctionType(Context->getObjCIdType(), - &ArgTys[0], ArgTys.size(), - false /*isVariadic*/, 0, - false, false, 0, 0, - FunctionType::ExtInfo()); - GetMetaClassFunctionDecl = FunctionDecl::Create(*Context, TUDecl, - SourceLocation(), - getClassIdent, getClassType, 0, - FunctionDecl::Extern, - FunctionDecl::None, false); -} - -Stmt *RewriteObjC::RewriteObjCStringLiteral(ObjCStringLiteral *Exp) { - QualType strType = getConstantStringStructType(); - - std::string S = "__NSConstantStringImpl_"; - - std::string tmpName = InFileName; - unsigned i; - for (i=0; i < tmpName.length(); i++) { - char c = tmpName.at(i); - // replace any non alphanumeric characters with '_'. - if (!isalpha(c) && (c < '0' || c > '9')) - tmpName[i] = '_'; - } - S += tmpName; - S += "_"; - S += utostr(NumObjCStringLiterals++); - - Preamble += "static __NSConstantStringImpl " + S; - Preamble += " __attribute__ ((section (\"__DATA, __cfstring\"))) = {__CFConstantStringClassReference,"; - Preamble += "0x000007c8,"; // utf8_str - // The pretty printer for StringLiteral handles escape characters properly. - std::string prettyBufS; - llvm::raw_string_ostream prettyBuf(prettyBufS); - Exp->getString()->printPretty(prettyBuf, *Context, 0, - PrintingPolicy(LangOpts)); - Preamble += prettyBuf.str(); - Preamble += ","; - Preamble += utostr(Exp->getString()->getByteLength()) + "};\n"; - - VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), - &Context->Idents.get(S), strType, 0, - VarDecl::Static, VarDecl::None); - DeclRefExpr *DRE = new (Context) DeclRefExpr(NewVD, strType, SourceLocation()); - Expr *Unop = new (Context) UnaryOperator(DRE, UnaryOperator::AddrOf, - Context->getPointerType(DRE->getType()), - SourceLocation()); - // cast to NSConstantString * - CastExpr *cast = NoTypeInfoCStyleCastExpr(Context, Exp->getType(), - CastExpr::CK_Unknown, Unop); - ReplaceStmt(Exp, cast); - // delete Exp; leak for now, see RewritePropertySetter() usage for more info. - return cast; -} - -bool RewriteObjC::isSuperReceiver(Expr *recExpr) { - // check if we are sending a message to 'super' - if (!CurMethodDef || !CurMethodDef->isInstanceMethod()) return false; - return isa<ObjCSuperExpr>(recExpr); -} - -// struct objc_super { struct objc_object *receiver; struct objc_class *super; }; -QualType RewriteObjC::getSuperStructType() { - if (!SuperStructDecl) { - SuperStructDecl = RecordDecl::Create(*Context, TTK_Struct, TUDecl, - SourceLocation(), - &Context->Idents.get("objc_super")); - QualType FieldTypes[2]; - - // struct objc_object *receiver; - FieldTypes[0] = Context->getObjCIdType(); - // struct objc_class *super; - FieldTypes[1] = Context->getObjCClassType(); - - // Create fields - for (unsigned i = 0; i < 2; ++i) { - SuperStructDecl->addDecl(FieldDecl::Create(*Context, SuperStructDecl, - SourceLocation(), 0, - FieldTypes[i], 0, - /*BitWidth=*/0, - /*Mutable=*/false)); - } - - SuperStructDecl->completeDefinition(); - } - return Context->getTagDeclType(SuperStructDecl); -} - -QualType RewriteObjC::getConstantStringStructType() { - if (!ConstantStringDecl) { - ConstantStringDecl = RecordDecl::Create(*Context, TTK_Struct, TUDecl, - SourceLocation(), - &Context->Idents.get("__NSConstantStringImpl")); - QualType FieldTypes[4]; - - // struct objc_object *receiver; - FieldTypes[0] = Context->getObjCIdType(); - // int flags; - FieldTypes[1] = Context->IntTy; - // char *str; - FieldTypes[2] = Context->getPointerType(Context->CharTy); - // long length; - FieldTypes[3] = Context->LongTy; - - // Create fields - for (unsigned i = 0; i < 4; ++i) { - ConstantStringDecl->addDecl(FieldDecl::Create(*Context, - ConstantStringDecl, - SourceLocation(), 0, - FieldTypes[i], 0, - /*BitWidth=*/0, - /*Mutable=*/true)); - } - - ConstantStringDecl->completeDefinition(); - } - return Context->getTagDeclType(ConstantStringDecl); -} - -Stmt *RewriteObjC::SynthMessageExpr(ObjCMessageExpr *Exp, - SourceLocation StartLoc, - SourceLocation EndLoc) { - if (!SelGetUidFunctionDecl) - SynthSelGetUidFunctionDecl(); - if (!MsgSendFunctionDecl) - SynthMsgSendFunctionDecl(); - if (!MsgSendSuperFunctionDecl) - SynthMsgSendSuperFunctionDecl(); - if (!MsgSendStretFunctionDecl) - SynthMsgSendStretFunctionDecl(); - if (!MsgSendSuperStretFunctionDecl) - SynthMsgSendSuperStretFunctionDecl(); - if (!MsgSendFpretFunctionDecl) - SynthMsgSendFpretFunctionDecl(); - if (!GetClassFunctionDecl) - SynthGetClassFunctionDecl(); - if (!GetSuperClassFunctionDecl) - SynthGetSuperClassFunctionDecl(); - if (!GetMetaClassFunctionDecl) - SynthGetMetaClassFunctionDecl(); - - // default to objc_msgSend(). - FunctionDecl *MsgSendFlavor = MsgSendFunctionDecl; - // May need to use objc_msgSend_stret() as well. - FunctionDecl *MsgSendStretFlavor = 0; - if (ObjCMethodDecl *mDecl = Exp->getMethodDecl()) { - QualType resultType = mDecl->getResultType(); - if (resultType->isRecordType()) - MsgSendStretFlavor = MsgSendStretFunctionDecl; - else if (resultType->isRealFloatingType()) - MsgSendFlavor = MsgSendFpretFunctionDecl; - } - - // Synthesize a call to objc_msgSend(). - llvm::SmallVector<Expr*, 8> MsgExprs; - switch (Exp->getReceiverKind()) { - case ObjCMessageExpr::SuperClass: { - MsgSendFlavor = MsgSendSuperFunctionDecl; - if (MsgSendStretFlavor) - MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; - assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); - - ObjCInterfaceDecl *ClassDecl = CurMethodDef->getClassInterface(); - - llvm::SmallVector<Expr*, 4> InitExprs; - - // set the receiver to self, the first argument to all methods. - InitExprs.push_back( - NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), - CastExpr::CK_Unknown, - new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), - Context->getObjCIdType(), - SourceLocation())) - ); // set the 'receiver'. - - // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) - llvm::SmallVector<Expr*, 8> ClsExprs; - QualType argType = Context->getPointerType(Context->CharTy); - ClsExprs.push_back(StringLiteral::Create(*Context, - ClassDecl->getIdentifier()->getNameStart(), - ClassDecl->getIdentifier()->getLength(), - false, argType, SourceLocation())); - CallExpr *Cls = SynthesizeCallToFunctionDecl(GetMetaClassFunctionDecl, - &ClsExprs[0], - ClsExprs.size(), - StartLoc, - EndLoc); - // (Class)objc_getClass("CurrentClass") - CastExpr *ArgExpr = NoTypeInfoCStyleCastExpr(Context, - Context->getObjCClassType(), - CastExpr::CK_Unknown, Cls); - ClsExprs.clear(); - ClsExprs.push_back(ArgExpr); - Cls = SynthesizeCallToFunctionDecl(GetSuperClassFunctionDecl, - &ClsExprs[0], ClsExprs.size(), - StartLoc, EndLoc); - - // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) - // To turn off a warning, type-cast to 'id' - InitExprs.push_back( // set 'super class', using class_getSuperclass(). - NoTypeInfoCStyleCastExpr(Context, - Context->getObjCIdType(), - CastExpr::CK_Unknown, Cls)); - // struct objc_super - QualType superType = getSuperStructType(); - Expr *SuperRep; - - if (LangOpts.Microsoft) { - SynthSuperContructorFunctionDecl(); - // Simulate a contructor call... - DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, - superType, SourceLocation()); - SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], - InitExprs.size(), - superType, SourceLocation()); - // The code for super is a little tricky to prevent collision with - // the structure definition in the header. The rewriter has it's own - // internal definition (__rw_objc_super) that is uses. This is why - // we need the cast below. For example: - // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) - // - SuperRep = new (Context) UnaryOperator(SuperRep, UnaryOperator::AddrOf, - Context->getPointerType(SuperRep->getType()), - SourceLocation()); - SuperRep = NoTypeInfoCStyleCastExpr(Context, - Context->getPointerType(superType), - CastExpr::CK_Unknown, SuperRep); - } else { - // (struct objc_super) { <exprs from above> } - InitListExpr *ILE = - new (Context) InitListExpr(*Context, SourceLocation(), - &InitExprs[0], InitExprs.size(), - SourceLocation()); - TypeSourceInfo *superTInfo - = Context->getTrivialTypeSourceInfo(superType); - SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo, - superType, ILE, false); - // struct objc_super * - SuperRep = new (Context) UnaryOperator(SuperRep, UnaryOperator::AddrOf, - Context->getPointerType(SuperRep->getType()), - SourceLocation()); - } - MsgExprs.push_back(SuperRep); - break; - } - - case ObjCMessageExpr::Class: { - llvm::SmallVector<Expr*, 8> ClsExprs; - QualType argType = Context->getPointerType(Context->CharTy); - ObjCInterfaceDecl *Class - = Exp->getClassReceiver()->getAs<ObjCObjectType>()->getInterface(); - IdentifierInfo *clsName = Class->getIdentifier(); - ClsExprs.push_back(StringLiteral::Create(*Context, - clsName->getNameStart(), - clsName->getLength(), - false, argType, - SourceLocation())); - CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, - &ClsExprs[0], - ClsExprs.size(), - StartLoc, EndLoc); - MsgExprs.push_back(Cls); - break; - } - - case ObjCMessageExpr::SuperInstance:{ - MsgSendFlavor = MsgSendSuperFunctionDecl; - if (MsgSendStretFlavor) - MsgSendStretFlavor = MsgSendSuperStretFunctionDecl; - assert(MsgSendFlavor && "MsgSendFlavor is NULL!"); - ObjCInterfaceDecl *ClassDecl = CurMethodDef->getClassInterface(); - llvm::SmallVector<Expr*, 4> InitExprs; - - InitExprs.push_back( - NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), - CastExpr::CK_Unknown, - new (Context) DeclRefExpr(CurMethodDef->getSelfDecl(), - Context->getObjCIdType(), - SourceLocation())) - ); // set the 'receiver'. - - // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) - llvm::SmallVector<Expr*, 8> ClsExprs; - QualType argType = Context->getPointerType(Context->CharTy); - ClsExprs.push_back(StringLiteral::Create(*Context, - ClassDecl->getIdentifier()->getNameStart(), - ClassDecl->getIdentifier()->getLength(), - false, argType, SourceLocation())); - CallExpr *Cls = SynthesizeCallToFunctionDecl(GetClassFunctionDecl, - &ClsExprs[0], - ClsExprs.size(), - StartLoc, EndLoc); - // (Class)objc_getClass("CurrentClass") - CastExpr *ArgExpr = NoTypeInfoCStyleCastExpr(Context, - Context->getObjCClassType(), - CastExpr::CK_Unknown, Cls); - ClsExprs.clear(); - ClsExprs.push_back(ArgExpr); - Cls = SynthesizeCallToFunctionDecl(GetSuperClassFunctionDecl, - &ClsExprs[0], ClsExprs.size(), - StartLoc, EndLoc); - - // (id)class_getSuperclass((Class)objc_getClass("CurrentClass")) - // To turn off a warning, type-cast to 'id' - InitExprs.push_back( - // set 'super class', using class_getSuperclass(). - NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), - CastExpr::CK_Unknown, Cls)); - // struct objc_super - QualType superType = getSuperStructType(); - Expr *SuperRep; - - if (LangOpts.Microsoft) { - SynthSuperContructorFunctionDecl(); - // Simulate a contructor call... - DeclRefExpr *DRE = new (Context) DeclRefExpr(SuperContructorFunctionDecl, - superType, SourceLocation()); - SuperRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], - InitExprs.size(), - superType, SourceLocation()); - // The code for super is a little tricky to prevent collision with - // the structure definition in the header. The rewriter has it's own - // internal definition (__rw_objc_super) that is uses. This is why - // we need the cast below. For example: - // (struct objc_super *)&__rw_objc_super((id)self, (id)objc_getClass("SUPER")) - // - SuperRep = new (Context) UnaryOperator(SuperRep, UnaryOperator::AddrOf, - Context->getPointerType(SuperRep->getType()), - SourceLocation()); - SuperRep = NoTypeInfoCStyleCastExpr(Context, - Context->getPointerType(superType), - CastExpr::CK_Unknown, SuperRep); - } else { - // (struct objc_super) { <exprs from above> } - InitListExpr *ILE = - new (Context) InitListExpr(*Context, SourceLocation(), - &InitExprs[0], InitExprs.size(), - SourceLocation()); - TypeSourceInfo *superTInfo - = Context->getTrivialTypeSourceInfo(superType); - SuperRep = new (Context) CompoundLiteralExpr(SourceLocation(), superTInfo, - superType, ILE, false); - } - MsgExprs.push_back(SuperRep); - break; - } - - case ObjCMessageExpr::Instance: { - // Remove all type-casts because it may contain objc-style types; e.g. - // Foo<Proto> *. - Expr *recExpr = Exp->getInstanceReceiver(); - while (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(recExpr)) - recExpr = CE->getSubExpr(); - recExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), - CastExpr::CK_Unknown, recExpr); - MsgExprs.push_back(recExpr); - break; - } - } - - // Create a call to sel_registerName("selName"), it will be the 2nd argument. - llvm::SmallVector<Expr*, 8> SelExprs; - QualType argType = Context->getPointerType(Context->CharTy); - SelExprs.push_back(StringLiteral::Create(*Context, - Exp->getSelector().getAsString().c_str(), - Exp->getSelector().getAsString().size(), - false, argType, SourceLocation())); - CallExpr *SelExp = SynthesizeCallToFunctionDecl(SelGetUidFunctionDecl, - &SelExprs[0], SelExprs.size(), - StartLoc, - EndLoc); - MsgExprs.push_back(SelExp); - - // Now push any user supplied arguments. - for (unsigned i = 0; i < Exp->getNumArgs(); i++) { - Expr *userExpr = Exp->getArg(i); - // Make all implicit casts explicit...ICE comes in handy:-) - if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(userExpr)) { - // Reuse the ICE type, it is exactly what the doctor ordered. - QualType type = ICE->getType()->isObjCQualifiedIdType() - ? Context->getObjCIdType() - : ICE->getType(); - // Make sure we convert "type (^)(...)" to "type (*)(...)". - (void)convertBlockPointerToFunctionPointer(type); - userExpr = NoTypeInfoCStyleCastExpr(Context, type, CastExpr::CK_Unknown, - userExpr); - } - // Make id<P...> cast into an 'id' cast. - else if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(userExpr)) { - if (CE->getType()->isObjCQualifiedIdType()) { - while ((CE = dyn_cast<CStyleCastExpr>(userExpr))) - userExpr = CE->getSubExpr(); - userExpr = NoTypeInfoCStyleCastExpr(Context, Context->getObjCIdType(), - CastExpr::CK_Unknown, userExpr); - } - } - MsgExprs.push_back(userExpr); - // We've transferred the ownership to MsgExprs. For now, we *don't* null - // out the argument in the original expression (since we aren't deleting - // the ObjCMessageExpr). See RewritePropertySetter() usage for more info. - //Exp->setArg(i, 0); - } - // Generate the funky cast. - CastExpr *cast; - llvm::SmallVector<QualType, 8> ArgTypes; - QualType returnType; - - // Push 'id' and 'SEL', the 2 implicit arguments. - if (MsgSendFlavor == MsgSendSuperFunctionDecl) - ArgTypes.push_back(Context->getPointerType(getSuperStructType())); - else - ArgTypes.push_back(Context->getObjCIdType()); - ArgTypes.push_back(Context->getObjCSelType()); - if (ObjCMethodDecl *OMD = Exp->getMethodDecl()) { - // Push any user argument types. - for (ObjCMethodDecl::param_iterator PI = OMD->param_begin(), - E = OMD->param_end(); PI != E; ++PI) { - QualType t = (*PI)->getType()->isObjCQualifiedIdType() - ? Context->getObjCIdType() - : (*PI)->getType(); - // Make sure we convert "t (^)(...)" to "t (*)(...)". - (void)convertBlockPointerToFunctionPointer(t); - ArgTypes.push_back(t); - } - returnType = OMD->getResultType()->isObjCQualifiedIdType() - ? Context->getObjCIdType() : OMD->getResultType(); - (void)convertBlockPointerToFunctionPointer(returnType); - } else { - returnType = Context->getObjCIdType(); - } - // Get the type, we will need to reference it in a couple spots. - QualType msgSendType = MsgSendFlavor->getType(); - - // Create a reference to the objc_msgSend() declaration. - DeclRefExpr *DRE = new (Context) DeclRefExpr(MsgSendFlavor, msgSendType, - SourceLocation()); - - // Need to cast objc_msgSend to "void *" (to workaround a GCC bandaid). - // If we don't do this cast, we get the following bizarre warning/note: - // xx.m:13: warning: function called through a non-compatible type - // xx.m:13: note: if this code is reached, the program will abort - cast = NoTypeInfoCStyleCastExpr(Context, - Context->getPointerType(Context->VoidTy), - CastExpr::CK_Unknown, DRE); - - // Now do the "normal" pointer to function cast. - QualType castType = Context->getFunctionType(returnType, - &ArgTypes[0], ArgTypes.size(), - // If we don't have a method decl, force a variadic cast. - Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : true, 0, - false, false, 0, 0, - FunctionType::ExtInfo()); - castType = Context->getPointerType(castType); - cast = NoTypeInfoCStyleCastExpr(Context, castType, CastExpr::CK_Unknown, - cast); - - // Don't forget the parens to enforce the proper binding. - ParenExpr *PE = new (Context) ParenExpr(StartLoc, EndLoc, cast); - - const FunctionType *FT = msgSendType->getAs<FunctionType>(); - CallExpr *CE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], - MsgExprs.size(), - FT->getResultType(), EndLoc); - Stmt *ReplacingStmt = CE; - if (MsgSendStretFlavor) { - // We have the method which returns a struct/union. Must also generate - // call to objc_msgSend_stret and hang both varieties on a conditional - // expression which dictate which one to envoke depending on size of - // method's return type. - - // Create a reference to the objc_msgSend_stret() declaration. - DeclRefExpr *STDRE = new (Context) DeclRefExpr(MsgSendStretFlavor, msgSendType, - SourceLocation()); - // Need to cast objc_msgSend_stret to "void *" (see above comment). - cast = NoTypeInfoCStyleCastExpr(Context, - Context->getPointerType(Context->VoidTy), - CastExpr::CK_Unknown, STDRE); - // Now do the "normal" pointer to function cast. - castType = Context->getFunctionType(returnType, - &ArgTypes[0], ArgTypes.size(), - Exp->getMethodDecl() ? Exp->getMethodDecl()->isVariadic() : false, 0, - false, false, 0, 0, - FunctionType::ExtInfo()); - castType = Context->getPointerType(castType); - cast = NoTypeInfoCStyleCastExpr(Context, castType, CastExpr::CK_Unknown, - cast); - - // Don't forget the parens to enforce the proper binding. - PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), cast); - - FT = msgSendType->getAs<FunctionType>(); - CallExpr *STCE = new (Context) CallExpr(*Context, PE, &MsgExprs[0], - MsgExprs.size(), - FT->getResultType(), SourceLocation()); - - // Build sizeof(returnType) - SizeOfAlignOfExpr *sizeofExpr = new (Context) SizeOfAlignOfExpr(true, - Context->getTrivialTypeSourceInfo(returnType), - Context->getSizeType(), - SourceLocation(), SourceLocation()); - // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) - // FIXME: Value of 8 is base on ppc32/x86 ABI for the most common cases. - // For X86 it is more complicated and some kind of target specific routine - // is needed to decide what to do. - unsigned IntSize = - static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); - IntegerLiteral *limit = new (Context) IntegerLiteral(llvm::APInt(IntSize, 8), - Context->IntTy, - SourceLocation()); - BinaryOperator *lessThanExpr = new (Context) BinaryOperator(sizeofExpr, limit, - BinaryOperator::LE, - Context->IntTy, - SourceLocation()); - // (sizeof(returnType) <= 8 ? objc_msgSend(...) : objc_msgSend_stret(...)) - ConditionalOperator *CondExpr = - new (Context) ConditionalOperator(lessThanExpr, - SourceLocation(), CE, - SourceLocation(), STCE, returnType); - ReplacingStmt = new (Context) ParenExpr(SourceLocation(), SourceLocation(), CondExpr); - } - // delete Exp; leak for now, see RewritePropertySetter() usage for more info. - return ReplacingStmt; -} - -Stmt *RewriteObjC::RewriteMessageExpr(ObjCMessageExpr *Exp) { - Stmt *ReplacingStmt = SynthMessageExpr(Exp, Exp->getLocStart(), - Exp->getLocEnd()); - - // Now do the actual rewrite. - ReplaceStmt(Exp, ReplacingStmt); - - // delete Exp; leak for now, see RewritePropertySetter() usage for more info. - return ReplacingStmt; -} - -// typedef struct objc_object Protocol; -QualType RewriteObjC::getProtocolType() { - if (!ProtocolTypeDecl) { - TypeSourceInfo *TInfo - = Context->getTrivialTypeSourceInfo(Context->getObjCIdType()); - ProtocolTypeDecl = TypedefDecl::Create(*Context, TUDecl, - SourceLocation(), - &Context->Idents.get("Protocol"), - TInfo); - } - return Context->getTypeDeclType(ProtocolTypeDecl); -} - -/// RewriteObjCProtocolExpr - Rewrite a protocol expression into -/// a synthesized/forward data reference (to the protocol's metadata). -/// The forward references (and metadata) are generated in -/// RewriteObjC::HandleTranslationUnit(). -Stmt *RewriteObjC::RewriteObjCProtocolExpr(ObjCProtocolExpr *Exp) { - std::string Name = "_OBJC_PROTOCOL_" + Exp->getProtocol()->getNameAsString(); - IdentifierInfo *ID = &Context->Idents.get(Name); - VarDecl *VD = VarDecl::Create(*Context, TUDecl, SourceLocation(), - ID, getProtocolType(), 0, - VarDecl::Extern, VarDecl::None); - DeclRefExpr *DRE = new (Context) DeclRefExpr(VD, getProtocolType(), SourceLocation()); - Expr *DerefExpr = new (Context) UnaryOperator(DRE, UnaryOperator::AddrOf, - Context->getPointerType(DRE->getType()), - SourceLocation()); - CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, DerefExpr->getType(), - CastExpr::CK_Unknown, - DerefExpr); - ReplaceStmt(Exp, castExpr); - ProtocolExprDecls.insert(Exp->getProtocol()); - // delete Exp; leak for now, see RewritePropertySetter() usage for more info. - return castExpr; - -} - -bool RewriteObjC::BufferContainsPPDirectives(const char *startBuf, - const char *endBuf) { - while (startBuf < endBuf) { - if (*startBuf == '#') { - // Skip whitespace. - for (++startBuf; startBuf[0] == ' ' || startBuf[0] == '\t'; ++startBuf) - ; - if (!strncmp(startBuf, "if", strlen("if")) || - !strncmp(startBuf, "ifdef", strlen("ifdef")) || - !strncmp(startBuf, "ifndef", strlen("ifndef")) || - !strncmp(startBuf, "define", strlen("define")) || - !strncmp(startBuf, "undef", strlen("undef")) || - !strncmp(startBuf, "else", strlen("else")) || - !strncmp(startBuf, "elif", strlen("elif")) || - !strncmp(startBuf, "endif", strlen("endif")) || - !strncmp(startBuf, "pragma", strlen("pragma")) || - !strncmp(startBuf, "include", strlen("include")) || - !strncmp(startBuf, "import", strlen("import")) || - !strncmp(startBuf, "include_next", strlen("include_next"))) - return true; - } - startBuf++; - } - return false; -} - -/// SynthesizeObjCInternalStruct - Rewrite one internal struct corresponding to -/// an objective-c class with ivars. -void RewriteObjC::SynthesizeObjCInternalStruct(ObjCInterfaceDecl *CDecl, - std::string &Result) { - assert(CDecl && "Class missing in SynthesizeObjCInternalStruct"); - assert(CDecl->getNameAsCString() && - "Name missing in SynthesizeObjCInternalStruct"); - // Do not synthesize more than once. - if (ObjCSynthesizedStructs.count(CDecl)) - return; - ObjCInterfaceDecl *RCDecl = CDecl->getSuperClass(); - int NumIvars = CDecl->ivar_size(); - SourceLocation LocStart = CDecl->getLocStart(); - SourceLocation LocEnd = CDecl->getLocEnd(); - - const char *startBuf = SM->getCharacterData(LocStart); - const char *endBuf = SM->getCharacterData(LocEnd); - - // If no ivars and no root or if its root, directly or indirectly, - // have no ivars (thus not synthesized) then no need to synthesize this class. - if ((CDecl->isForwardDecl() || NumIvars == 0) && - (!RCDecl || !ObjCSynthesizedStructs.count(RCDecl))) { - endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); - ReplaceText(LocStart, endBuf-startBuf, Result); - return; - } - - // FIXME: This has potential of causing problem. If - // SynthesizeObjCInternalStruct is ever called recursively. - Result += "\nstruct "; - Result += CDecl->getNameAsString(); - if (LangOpts.Microsoft) - Result += "_IMPL"; - - if (NumIvars > 0) { - const char *cursor = strchr(startBuf, '{'); - assert((cursor && endBuf) - && "SynthesizeObjCInternalStruct - malformed @interface"); - // If the buffer contains preprocessor directives, we do more fine-grained - // rewrites. This is intended to fix code that looks like (which occurs in - // NSURL.h, for example): - // - // #ifdef XYZ - // @interface Foo : NSObject - // #else - // @interface FooBar : NSObject - // #endif - // { - // int i; - // } - // @end - // - // This clause is segregated to avoid breaking the common case. - if (BufferContainsPPDirectives(startBuf, cursor)) { - SourceLocation L = RCDecl ? CDecl->getSuperClassLoc() : - CDecl->getClassLoc(); - const char *endHeader = SM->getCharacterData(L); - endHeader += Lexer::MeasureTokenLength(L, *SM, LangOpts); - - if (CDecl->protocol_begin() != CDecl->protocol_end()) { - // advance to the end of the referenced protocols. - while (endHeader < cursor && *endHeader != '>') endHeader++; - endHeader++; - } - // rewrite the original header - ReplaceText(LocStart, endHeader-startBuf, Result); - } else { - // rewrite the original header *without* disturbing the '{' - ReplaceText(LocStart, cursor-startBuf, Result); - } - if (RCDecl && ObjCSynthesizedStructs.count(RCDecl)) { - Result = "\n struct "; - Result += RCDecl->getNameAsString(); - Result += "_IMPL "; - Result += RCDecl->getNameAsString(); - Result += "_IVARS;\n"; - - // insert the super class structure definition. - SourceLocation OnePastCurly = - LocStart.getFileLocWithOffset(cursor-startBuf+1); - InsertText(OnePastCurly, Result); - } - cursor++; // past '{' - - // Now comment out any visibility specifiers. - while (cursor < endBuf) { - if (*cursor == '@') { - SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); - // Skip whitespace. - for (++cursor; cursor[0] == ' ' || cursor[0] == '\t'; ++cursor) - /*scan*/; - - // FIXME: presence of @public, etc. inside comment results in - // this transformation as well, which is still correct c-code. - if (!strncmp(cursor, "public", strlen("public")) || - !strncmp(cursor, "private", strlen("private")) || - !strncmp(cursor, "package", strlen("package")) || - !strncmp(cursor, "protected", strlen("protected"))) - InsertText(atLoc, "// "); - } - // FIXME: If there are cases where '<' is used in ivar declaration part - // of user code, then scan the ivar list and use needToScanForQualifiers - // for type checking. - else if (*cursor == '<') { - SourceLocation atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); - InsertText(atLoc, "/* "); - cursor = strchr(cursor, '>'); - cursor++; - atLoc = LocStart.getFileLocWithOffset(cursor-startBuf); - InsertText(atLoc, " */"); - } else if (*cursor == '^') { // rewrite block specifier. - SourceLocation caretLoc = LocStart.getFileLocWithOffset(cursor-startBuf); - ReplaceText(caretLoc, 1, "*"); - } - cursor++; - } - // Don't forget to add a ';'!! - InsertText(LocEnd.getFileLocWithOffset(1), ";"); - } else { // we don't have any instance variables - insert super struct. - endBuf += Lexer::MeasureTokenLength(LocEnd, *SM, LangOpts); - Result += " {\n struct "; - Result += RCDecl->getNameAsString(); - Result += "_IMPL "; - Result += RCDecl->getNameAsString(); - Result += "_IVARS;\n};\n"; - ReplaceText(LocStart, endBuf-startBuf, Result); - } - // Mark this struct as having been generated. - if (!ObjCSynthesizedStructs.insert(CDecl)) - assert(false && "struct already synthesize- SynthesizeObjCInternalStruct"); -} - -// RewriteObjCMethodsMetaData - Rewrite methods metadata for instance or -/// class methods. -template<typename MethodIterator> -void RewriteObjC::RewriteObjCMethodsMetaData(MethodIterator MethodBegin, - MethodIterator MethodEnd, - bool IsInstanceMethod, - const char *prefix, - const char *ClassName, - std::string &Result) { - if (MethodBegin == MethodEnd) return; - - if (!objc_impl_method) { - /* struct _objc_method { - SEL _cmd; - char *method_types; - void *_imp; - } - */ - Result += "\nstruct _objc_method {\n"; - Result += "\tSEL _cmd;\n"; - Result += "\tchar *method_types;\n"; - Result += "\tvoid *_imp;\n"; - Result += "};\n"; - - objc_impl_method = true; - } - - // Build _objc_method_list for class's methods if needed - - /* struct { - struct _objc_method_list *next_method; - int method_count; - struct _objc_method method_list[]; - } - */ - unsigned NumMethods = std::distance(MethodBegin, MethodEnd); - Result += "\nstatic struct {\n"; - Result += "\tstruct _objc_method_list *next_method;\n"; - Result += "\tint method_count;\n"; - Result += "\tstruct _objc_method method_list["; - Result += utostr(NumMethods); - Result += "];\n} _OBJC_"; - Result += prefix; - Result += IsInstanceMethod ? "INSTANCE" : "CLASS"; - Result += "_METHODS_"; - Result += ClassName; - Result += " __attribute__ ((used, section (\"__OBJC, __"; - Result += IsInstanceMethod ? "inst" : "cls"; - Result += "_meth\")))= "; - Result += "{\n\t0, " + utostr(NumMethods) + "\n"; - - Result += "\t,{{(SEL)\""; - Result += (*MethodBegin)->getSelector().getAsString().c_str(); - std::string MethodTypeString; - Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); - Result += "\", \""; - Result += MethodTypeString; - Result += "\", (void *)"; - Result += MethodInternalNames[*MethodBegin]; - Result += "}\n"; - for (++MethodBegin; MethodBegin != MethodEnd; ++MethodBegin) { - Result += "\t ,{(SEL)\""; - Result += (*MethodBegin)->getSelector().getAsString().c_str(); - std::string MethodTypeString; - Context->getObjCEncodingForMethodDecl(*MethodBegin, MethodTypeString); - Result += "\", \""; - Result += MethodTypeString; - Result += "\", (void *)"; - Result += MethodInternalNames[*MethodBegin]; - Result += "}\n"; - } - Result += "\t }\n};\n"; -} - -/// RewriteObjCProtocolMetaData - Rewrite protocols meta-data. -void RewriteObjC:: -RewriteObjCProtocolMetaData(ObjCProtocolDecl *PDecl, const char *prefix, - const char *ClassName, std::string &Result) { - static bool objc_protocol_methods = false; - - // Output struct protocol_methods holder of method selector and type. - if (!objc_protocol_methods && !PDecl->isForwardDecl()) { - /* struct protocol_methods { - SEL _cmd; - char *method_types; - } - */ - Result += "\nstruct _protocol_methods {\n"; - Result += "\tstruct objc_selector *_cmd;\n"; - Result += "\tchar *method_types;\n"; - Result += "};\n"; - - objc_protocol_methods = true; - } - // Do not synthesize the protocol more than once. - if (ObjCSynthesizedProtocols.count(PDecl)) - return; - - if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { - unsigned NumMethods = std::distance(PDecl->instmeth_begin(), - PDecl->instmeth_end()); - /* struct _objc_protocol_method_list { - int protocol_method_count; - struct protocol_methods protocols[]; - } - */ - Result += "\nstatic struct {\n"; - Result += "\tint protocol_method_count;\n"; - Result += "\tstruct _protocol_methods protocol_methods["; - Result += utostr(NumMethods); - Result += "];\n} _OBJC_PROTOCOL_INSTANCE_METHODS_"; - Result += PDecl->getNameAsString(); - Result += " __attribute__ ((used, section (\"__OBJC, __cat_inst_meth\")))= " - "{\n\t" + utostr(NumMethods) + "\n"; - - // Output instance methods declared in this protocol. - for (ObjCProtocolDecl::instmeth_iterator - I = PDecl->instmeth_begin(), E = PDecl->instmeth_end(); - I != E; ++I) { - if (I == PDecl->instmeth_begin()) - Result += "\t ,{{(struct objc_selector *)\""; - else - Result += "\t ,{(struct objc_selector *)\""; - Result += (*I)->getSelector().getAsString().c_str(); - std::string MethodTypeString; - Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); - Result += "\", \""; - Result += MethodTypeString; - Result += "\"}\n"; - } - Result += "\t }\n};\n"; - } - - // Output class methods declared in this protocol. - unsigned NumMethods = std::distance(PDecl->classmeth_begin(), - PDecl->classmeth_end()); - if (NumMethods > 0) { - /* struct _objc_protocol_method_list { - int protocol_method_count; - struct protocol_methods protocols[]; - } - */ - Result += "\nstatic struct {\n"; - Result += "\tint protocol_method_count;\n"; - Result += "\tstruct _protocol_methods protocol_methods["; - Result += utostr(NumMethods); - Result += "];\n} _OBJC_PROTOCOL_CLASS_METHODS_"; - Result += PDecl->getNameAsString(); - Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " - "{\n\t"; - Result += utostr(NumMethods); - Result += "\n"; - - // Output instance methods declared in this protocol. - for (ObjCProtocolDecl::classmeth_iterator - I = PDecl->classmeth_begin(), E = PDecl->classmeth_end(); - I != E; ++I) { - if (I == PDecl->classmeth_begin()) - Result += "\t ,{{(struct objc_selector *)\""; - else - Result += "\t ,{(struct objc_selector *)\""; - Result += (*I)->getSelector().getAsString().c_str(); - std::string MethodTypeString; - Context->getObjCEncodingForMethodDecl((*I), MethodTypeString); - Result += "\", \""; - Result += MethodTypeString; - Result += "\"}\n"; - } - Result += "\t }\n};\n"; - } - - // Output: - /* struct _objc_protocol { - // Objective-C 1.0 extensions - struct _objc_protocol_extension *isa; - char *protocol_name; - struct _objc_protocol **protocol_list; - struct _objc_protocol_method_list *instance_methods; - struct _objc_protocol_method_list *class_methods; - }; - */ - static bool objc_protocol = false; - if (!objc_protocol) { - Result += "\nstruct _objc_protocol {\n"; - Result += "\tstruct _objc_protocol_extension *isa;\n"; - Result += "\tchar *protocol_name;\n"; - Result += "\tstruct _objc_protocol **protocol_list;\n"; - Result += "\tstruct _objc_protocol_method_list *instance_methods;\n"; - Result += "\tstruct _objc_protocol_method_list *class_methods;\n"; - Result += "};\n"; - - objc_protocol = true; - } - - Result += "\nstatic struct _objc_protocol _OBJC_PROTOCOL_"; - Result += PDecl->getNameAsString(); - Result += " __attribute__ ((used, section (\"__OBJC, __protocol\")))= " - "{\n\t0, \""; - Result += PDecl->getNameAsString(); - Result += "\", 0, "; - if (PDecl->instmeth_begin() != PDecl->instmeth_end()) { - Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_INSTANCE_METHODS_"; - Result += PDecl->getNameAsString(); - Result += ", "; - } - else - Result += "0, "; - if (PDecl->classmeth_begin() != PDecl->classmeth_end()) { - Result += "(struct _objc_protocol_method_list *)&_OBJC_PROTOCOL_CLASS_METHODS_"; - Result += PDecl->getNameAsString(); - Result += "\n"; - } - else - Result += "0\n"; - Result += "};\n"; - - // Mark this protocol as having been generated. - if (!ObjCSynthesizedProtocols.insert(PDecl)) - assert(false && "protocol already synthesized"); - -} - -void RewriteObjC:: -RewriteObjCProtocolListMetaData(const ObjCList<ObjCProtocolDecl> &Protocols, - const char *prefix, const char *ClassName, - std::string &Result) { - if (Protocols.empty()) return; - - for (unsigned i = 0; i != Protocols.size(); i++) - RewriteObjCProtocolMetaData(Protocols[i], prefix, ClassName, Result); - - // Output the top lovel protocol meta-data for the class. - /* struct _objc_protocol_list { - struct _objc_protocol_list *next; - int protocol_count; - struct _objc_protocol *class_protocols[]; - } - */ - Result += "\nstatic struct {\n"; - Result += "\tstruct _objc_protocol_list *next;\n"; - Result += "\tint protocol_count;\n"; - Result += "\tstruct _objc_protocol *class_protocols["; - Result += utostr(Protocols.size()); - Result += "];\n} _OBJC_"; - Result += prefix; - Result += "_PROTOCOLS_"; - Result += ClassName; - Result += " __attribute__ ((used, section (\"__OBJC, __cat_cls_meth\")))= " - "{\n\t0, "; - Result += utostr(Protocols.size()); - Result += "\n"; - - Result += "\t,{&_OBJC_PROTOCOL_"; - Result += Protocols[0]->getNameAsString(); - Result += " \n"; - - for (unsigned i = 1; i != Protocols.size(); i++) { - Result += "\t ,&_OBJC_PROTOCOL_"; - Result += Protocols[i]->getNameAsString(); - Result += "\n"; - } - Result += "\t }\n};\n"; -} - - -/// RewriteObjCCategoryImplDecl - Rewrite metadata for each category -/// implementation. -void RewriteObjC::RewriteObjCCategoryImplDecl(ObjCCategoryImplDecl *IDecl, - std::string &Result) { - ObjCInterfaceDecl *ClassDecl = IDecl->getClassInterface(); - // Find category declaration for this implementation. - ObjCCategoryDecl *CDecl; - for (CDecl = ClassDecl->getCategoryList(); CDecl; - CDecl = CDecl->getNextClassCategory()) - if (CDecl->getIdentifier() == IDecl->getIdentifier()) - break; - - std::string FullCategoryName = ClassDecl->getNameAsString(); - FullCategoryName += '_'; - FullCategoryName += IDecl->getNameAsString(); - - // Build _objc_method_list for class's instance methods if needed - llvm::SmallVector<ObjCMethodDecl *, 32> - InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end()); - - // If any of our property implementations have associated getters or - // setters, produce metadata for them as well. - for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), - PropEnd = IDecl->propimpl_end(); - Prop != PropEnd; ++Prop) { - if ((*Prop)->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) - continue; - if (!(*Prop)->getPropertyIvarDecl()) - continue; - ObjCPropertyDecl *PD = (*Prop)->getPropertyDecl(); - if (!PD) - continue; - if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) - InstanceMethods.push_back(Getter); - if (PD->isReadOnly()) - continue; - if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) - InstanceMethods.push_back(Setter); - } - RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(), - true, "CATEGORY_", FullCategoryName.c_str(), - Result); - - // Build _objc_method_list for class's class methods if needed - RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), - false, "CATEGORY_", FullCategoryName.c_str(), - Result); - - // Protocols referenced in class declaration? - // Null CDecl is case of a category implementation with no category interface - if (CDecl) - RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(), "CATEGORY", - FullCategoryName.c_str(), Result); - /* struct _objc_category { - char *category_name; - char *class_name; - struct _objc_method_list *instance_methods; - struct _objc_method_list *class_methods; - struct _objc_protocol_list *protocols; - // Objective-C 1.0 extensions - uint32_t size; // sizeof (struct _objc_category) - struct _objc_property_list *instance_properties; // category's own - // @property decl. - }; - */ - - static bool objc_category = false; - if (!objc_category) { - Result += "\nstruct _objc_category {\n"; - Result += "\tchar *category_name;\n"; - Result += "\tchar *class_name;\n"; - Result += "\tstruct _objc_method_list *instance_methods;\n"; - Result += "\tstruct _objc_method_list *class_methods;\n"; - Result += "\tstruct _objc_protocol_list *protocols;\n"; - Result += "\tunsigned int size;\n"; - Result += "\tstruct _objc_property_list *instance_properties;\n"; - Result += "};\n"; - objc_category = true; - } - Result += "\nstatic struct _objc_category _OBJC_CATEGORY_"; - Result += FullCategoryName; - Result += " __attribute__ ((used, section (\"__OBJC, __category\")))= {\n\t\""; - Result += IDecl->getNameAsString(); - Result += "\"\n\t, \""; - Result += ClassDecl->getNameAsString(); - Result += "\"\n"; - - if (IDecl->instmeth_begin() != IDecl->instmeth_end()) { - Result += "\t, (struct _objc_method_list *)" - "&_OBJC_CATEGORY_INSTANCE_METHODS_"; - Result += FullCategoryName; - Result += "\n"; - } - else - Result += "\t, 0\n"; - if (IDecl->classmeth_begin() != IDecl->classmeth_end()) { - Result += "\t, (struct _objc_method_list *)" - "&_OBJC_CATEGORY_CLASS_METHODS_"; - Result += FullCategoryName; - Result += "\n"; - } - else - Result += "\t, 0\n"; - - if (CDecl && CDecl->protocol_begin() != CDecl->protocol_end()) { - Result += "\t, (struct _objc_protocol_list *)&_OBJC_CATEGORY_PROTOCOLS_"; - Result += FullCategoryName; - Result += "\n"; - } - else - Result += "\t, 0\n"; - Result += "\t, sizeof(struct _objc_category), 0\n};\n"; -} - -/// SynthesizeIvarOffsetComputation - This rutine synthesizes computation of -/// ivar offset. -void RewriteObjC::SynthesizeIvarOffsetComputation(ObjCContainerDecl *IDecl, - ObjCIvarDecl *ivar, - std::string &Result) { - if (ivar->isBitField()) { - // FIXME: The hack below doesn't work for bitfields. For now, we simply - // place all bitfields at offset 0. - Result += "0"; - } else { - Result += "__OFFSETOFIVAR__(struct "; - Result += IDecl->getNameAsString(); - if (LangOpts.Microsoft) - Result += "_IMPL"; - Result += ", "; - Result += ivar->getNameAsString(); - Result += ")"; - } -} - -//===----------------------------------------------------------------------===// -// Meta Data Emission -//===----------------------------------------------------------------------===// - -void RewriteObjC::RewriteObjCClassMetaData(ObjCImplementationDecl *IDecl, - std::string &Result) { - ObjCInterfaceDecl *CDecl = IDecl->getClassInterface(); - - // Explictly declared @interface's are already synthesized. - if (CDecl->isImplicitInterfaceDecl()) { - // FIXME: Implementation of a class with no @interface (legacy) doese not - // produce correct synthesis as yet. - SynthesizeObjCInternalStruct(CDecl, Result); - } - - // Build _objc_ivar_list metadata for classes ivars if needed - unsigned NumIvars = !IDecl->ivar_empty() - ? IDecl->ivar_size() - : (CDecl ? CDecl->ivar_size() : 0); - if (NumIvars > 0) { - static bool objc_ivar = false; - if (!objc_ivar) { - /* struct _objc_ivar { - char *ivar_name; - char *ivar_type; - int ivar_offset; - }; - */ - Result += "\nstruct _objc_ivar {\n"; - Result += "\tchar *ivar_name;\n"; - Result += "\tchar *ivar_type;\n"; - Result += "\tint ivar_offset;\n"; - Result += "};\n"; - - objc_ivar = true; - } - - /* struct { - int ivar_count; - struct _objc_ivar ivar_list[nIvars]; - }; - */ - Result += "\nstatic struct {\n"; - Result += "\tint ivar_count;\n"; - Result += "\tstruct _objc_ivar ivar_list["; - Result += utostr(NumIvars); - Result += "];\n} _OBJC_INSTANCE_VARIABLES_"; - Result += IDecl->getNameAsString(); - Result += " __attribute__ ((used, section (\"__OBJC, __instance_vars\")))= " - "{\n\t"; - Result += utostr(NumIvars); - Result += "\n"; - - ObjCInterfaceDecl::ivar_iterator IVI, IVE; - llvm::SmallVector<ObjCIvarDecl *, 8> IVars; - if (!IDecl->ivar_empty()) { - for (ObjCInterfaceDecl::ivar_iterator - IV = IDecl->ivar_begin(), IVEnd = IDecl->ivar_end(); - IV != IVEnd; ++IV) - IVars.push_back(*IV); - IVI = IDecl->ivar_begin(); - IVE = IDecl->ivar_end(); - } else { - IVI = CDecl->ivar_begin(); - IVE = CDecl->ivar_end(); - } - Result += "\t,{{\""; - Result += (*IVI)->getNameAsString(); - Result += "\", \""; - std::string TmpString, StrEncoding; - Context->getObjCEncodingForType((*IVI)->getType(), TmpString, *IVI); - QuoteDoublequotes(TmpString, StrEncoding); - Result += StrEncoding; - Result += "\", "; - SynthesizeIvarOffsetComputation(IDecl, *IVI, Result); - Result += "}\n"; - for (++IVI; IVI != IVE; ++IVI) { - Result += "\t ,{\""; - Result += (*IVI)->getNameAsString(); - Result += "\", \""; - std::string TmpString, StrEncoding; - Context->getObjCEncodingForType((*IVI)->getType(), TmpString, *IVI); - QuoteDoublequotes(TmpString, StrEncoding); - Result += StrEncoding; - Result += "\", "; - SynthesizeIvarOffsetComputation(IDecl, (*IVI), Result); - Result += "}\n"; - } - - Result += "\t }\n};\n"; - } - - // Build _objc_method_list for class's instance methods if needed - llvm::SmallVector<ObjCMethodDecl *, 32> - InstanceMethods(IDecl->instmeth_begin(), IDecl->instmeth_end()); - - // If any of our property implementations have associated getters or - // setters, produce metadata for them as well. - for (ObjCImplDecl::propimpl_iterator Prop = IDecl->propimpl_begin(), - PropEnd = IDecl->propimpl_end(); - Prop != PropEnd; ++Prop) { - if ((*Prop)->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic) - continue; - if (!(*Prop)->getPropertyIvarDecl()) - continue; - ObjCPropertyDecl *PD = (*Prop)->getPropertyDecl(); - if (!PD) - continue; - if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) - InstanceMethods.push_back(Getter); - if (PD->isReadOnly()) - continue; - if (ObjCMethodDecl *Setter = PD->getSetterMethodDecl()) - InstanceMethods.push_back(Setter); - } - RewriteObjCMethodsMetaData(InstanceMethods.begin(), InstanceMethods.end(), - true, "", IDecl->getNameAsCString(), Result); - - // Build _objc_method_list for class's class methods if needed - RewriteObjCMethodsMetaData(IDecl->classmeth_begin(), IDecl->classmeth_end(), - false, "", IDecl->getNameAsCString(), Result); - - // Protocols referenced in class declaration? - RewriteObjCProtocolListMetaData(CDecl->getReferencedProtocols(), - "CLASS", CDecl->getNameAsCString(), Result); - - // Declaration of class/meta-class metadata - /* struct _objc_class { - struct _objc_class *isa; // or const char *root_class_name when metadata - const char *super_class_name; - char *name; - long version; - long info; - long instance_size; - struct _objc_ivar_list *ivars; - struct _objc_method_list *methods; - struct objc_cache *cache; - struct objc_protocol_list *protocols; - const char *ivar_layout; - struct _objc_class_ext *ext; - }; - */ - static bool objc_class = false; - if (!objc_class) { - Result += "\nstruct _objc_class {\n"; - Result += "\tstruct _objc_class *isa;\n"; - Result += "\tconst char *super_class_name;\n"; - Result += "\tchar *name;\n"; - Result += "\tlong version;\n"; - Result += "\tlong info;\n"; - Result += "\tlong instance_size;\n"; - Result += "\tstruct _objc_ivar_list *ivars;\n"; - Result += "\tstruct _objc_method_list *methods;\n"; - Result += "\tstruct objc_cache *cache;\n"; - Result += "\tstruct _objc_protocol_list *protocols;\n"; - Result += "\tconst char *ivar_layout;\n"; - Result += "\tstruct _objc_class_ext *ext;\n"; - Result += "};\n"; - objc_class = true; - } - - // Meta-class metadata generation. - ObjCInterfaceDecl *RootClass = 0; - ObjCInterfaceDecl *SuperClass = CDecl->getSuperClass(); - while (SuperClass) { - RootClass = SuperClass; - SuperClass = SuperClass->getSuperClass(); - } - SuperClass = CDecl->getSuperClass(); - - Result += "\nstatic struct _objc_class _OBJC_METACLASS_"; - Result += CDecl->getNameAsString(); - Result += " __attribute__ ((used, section (\"__OBJC, __meta_class\")))= " - "{\n\t(struct _objc_class *)\""; - Result += (RootClass ? RootClass->getNameAsString() : CDecl->getNameAsString()); - Result += "\""; - - if (SuperClass) { - Result += ", \""; - Result += SuperClass->getNameAsString(); - Result += "\", \""; - Result += CDecl->getNameAsString(); - Result += "\""; - } - else { - Result += ", 0, \""; - Result += CDecl->getNameAsString(); - Result += "\""; - } - // Set 'ivars' field for root class to 0. ObjC1 runtime does not use it. - // 'info' field is initialized to CLS_META(2) for metaclass - Result += ", 0,2, sizeof(struct _objc_class), 0"; - if (IDecl->classmeth_begin() != IDecl->classmeth_end()) { - Result += "\n\t, (struct _objc_method_list *)&_OBJC_CLASS_METHODS_"; - Result += IDecl->getNameAsString(); - Result += "\n"; - } - else - Result += ", 0\n"; - if (CDecl->protocol_begin() != CDecl->protocol_end()) { - Result += "\t,0, (struct _objc_protocol_list *)&_OBJC_CLASS_PROTOCOLS_"; - Result += CDecl->getNameAsString(); - Result += ",0,0\n"; - } - else - Result += "\t,0,0,0,0\n"; - Result += "};\n"; - - // class metadata generation. - Result += "\nstatic struct _objc_class _OBJC_CLASS_"; - Result += CDecl->getNameAsString(); - Result += " __attribute__ ((used, section (\"__OBJC, __class\")))= " - "{\n\t&_OBJC_METACLASS_"; - Result += CDecl->getNameAsString(); - if (SuperClass) { - Result += ", \""; - Result += SuperClass->getNameAsString(); - Result += "\", \""; - Result += CDecl->getNameAsString(); - Result += "\""; - } - else { - Result += ", 0, \""; - Result += CDecl->getNameAsString(); - Result += "\""; - } - // 'info' field is initialized to CLS_CLASS(1) for class - Result += ", 0,1"; - if (!ObjCSynthesizedStructs.count(CDecl)) - Result += ",0"; - else { - // class has size. Must synthesize its size. - Result += ",sizeof(struct "; - Result += CDecl->getNameAsString(); - if (LangOpts.Microsoft) - Result += "_IMPL"; - Result += ")"; - } - if (NumIvars > 0) { - Result += ", (struct _objc_ivar_list *)&_OBJC_INSTANCE_VARIABLES_"; - Result += CDecl->getNameAsString(); - Result += "\n\t"; - } - else - Result += ",0"; - if (IDecl->instmeth_begin() != IDecl->instmeth_end()) { - Result += ", (struct _objc_method_list *)&_OBJC_INSTANCE_METHODS_"; - Result += CDecl->getNameAsString(); - Result += ", 0\n\t"; - } - else - Result += ",0,0"; - if (CDecl->protocol_begin() != CDecl->protocol_end()) { - Result += ", (struct _objc_protocol_list*)&_OBJC_CLASS_PROTOCOLS_"; - Result += CDecl->getNameAsString(); - Result += ", 0,0\n"; - } - else - Result += ",0,0,0\n"; - Result += "};\n"; -} - -/// RewriteImplementations - This routine rewrites all method implementations -/// and emits meta-data. - -void RewriteObjC::RewriteImplementations() { - int ClsDefCount = ClassImplementation.size(); - int CatDefCount = CategoryImplementation.size(); - - // Rewrite implemented methods - for (int i = 0; i < ClsDefCount; i++) - RewriteImplementationDecl(ClassImplementation[i]); - - for (int i = 0; i < CatDefCount; i++) - RewriteImplementationDecl(CategoryImplementation[i]); -} - -void RewriteObjC::SynthesizeMetaDataIntoBuffer(std::string &Result) { - int ClsDefCount = ClassImplementation.size(); - int CatDefCount = CategoryImplementation.size(); - - // For each implemented class, write out all its meta data. - for (int i = 0; i < ClsDefCount; i++) - RewriteObjCClassMetaData(ClassImplementation[i], Result); - - // For each implemented category, write out all its meta data. - for (int i = 0; i < CatDefCount; i++) - RewriteObjCCategoryImplDecl(CategoryImplementation[i], Result); - - // Write objc_symtab metadata - /* - struct _objc_symtab - { - long sel_ref_cnt; - SEL *refs; - short cls_def_cnt; - short cat_def_cnt; - void *defs[cls_def_cnt + cat_def_cnt]; - }; - */ - - Result += "\nstruct _objc_symtab {\n"; - Result += "\tlong sel_ref_cnt;\n"; - Result += "\tSEL *refs;\n"; - Result += "\tshort cls_def_cnt;\n"; - Result += "\tshort cat_def_cnt;\n"; - Result += "\tvoid *defs[" + utostr(ClsDefCount + CatDefCount)+ "];\n"; - Result += "};\n\n"; - - Result += "static struct _objc_symtab " - "_OBJC_SYMBOLS __attribute__((used, section (\"__OBJC, __symbols\")))= {\n"; - Result += "\t0, 0, " + utostr(ClsDefCount) - + ", " + utostr(CatDefCount) + "\n"; - for (int i = 0; i < ClsDefCount; i++) { - Result += "\t,&_OBJC_CLASS_"; - Result += ClassImplementation[i]->getNameAsString(); - Result += "\n"; - } - - for (int i = 0; i < CatDefCount; i++) { - Result += "\t,&_OBJC_CATEGORY_"; - Result += CategoryImplementation[i]->getClassInterface()->getNameAsString(); - Result += "_"; - Result += CategoryImplementation[i]->getNameAsString(); - Result += "\n"; - } - - Result += "};\n\n"; - - // Write objc_module metadata - - /* - struct _objc_module { - long version; - long size; - const char *name; - struct _objc_symtab *symtab; - } - */ - - Result += "\nstruct _objc_module {\n"; - Result += "\tlong version;\n"; - Result += "\tlong size;\n"; - Result += "\tconst char *name;\n"; - Result += "\tstruct _objc_symtab *symtab;\n"; - Result += "};\n\n"; - Result += "static struct _objc_module " - "_OBJC_MODULES __attribute__ ((used, section (\"__OBJC, __module_info\")))= {\n"; - Result += "\t" + utostr(OBJC_ABI_VERSION) + - ", sizeof(struct _objc_module), \"\", &_OBJC_SYMBOLS\n"; - Result += "};\n\n"; - - if (LangOpts.Microsoft) { - if (ProtocolExprDecls.size()) { - Result += "#pragma section(\".objc_protocol$B\",long,read,write)\n"; - Result += "#pragma data_seg(push, \".objc_protocol$B\")\n"; - for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(), - E = ProtocolExprDecls.end(); I != E; ++I) { - Result += "static struct _objc_protocol *_POINTER_OBJC_PROTOCOL_"; - Result += (*I)->getNameAsString(); - Result += " = &_OBJC_PROTOCOL_"; - Result += (*I)->getNameAsString(); - Result += ";\n"; - } - Result += "#pragma data_seg(pop)\n\n"; - } - Result += "#pragma section(\".objc_module_info$B\",long,read,write)\n"; - Result += "#pragma data_seg(push, \".objc_module_info$B\")\n"; - Result += "static struct _objc_module *_POINTER_OBJC_MODULES = "; - Result += "&_OBJC_MODULES;\n"; - Result += "#pragma data_seg(pop)\n\n"; - } -} - -void RewriteObjC::RewriteByRefString(std::string &ResultStr, - const std::string &Name, - ValueDecl *VD) { - assert(BlockByRefDeclNo.count(VD) && - "RewriteByRefString: ByRef decl missing"); - ResultStr += "struct __Block_byref_" + Name + - "_" + utostr(BlockByRefDeclNo[VD]) ; -} - -static bool HasLocalVariableExternalStorage(ValueDecl *VD) { - if (VarDecl *Var = dyn_cast<VarDecl>(VD)) - return (Var->isFunctionOrMethodVarDecl() && !Var->hasLocalStorage()); - return false; -} - -std::string RewriteObjC::SynthesizeBlockFunc(BlockExpr *CE, int i, - const char *funcName, - std::string Tag) { - const FunctionType *AFT = CE->getFunctionType(); - QualType RT = AFT->getResultType(); - std::string StructRef = "struct " + Tag; - std::string S = "static " + RT.getAsString() + " __" + - funcName + "_" + "block_func_" + utostr(i); - - BlockDecl *BD = CE->getBlockDecl(); - - if (isa<FunctionNoProtoType>(AFT)) { - // No user-supplied arguments. Still need to pass in a pointer to the - // block (to reference imported block decl refs). - S += "(" + StructRef + " *__cself)"; - } else if (BD->param_empty()) { - S += "(" + StructRef + " *__cself)"; - } else { - const FunctionProtoType *FT = cast<FunctionProtoType>(AFT); - assert(FT && "SynthesizeBlockFunc: No function proto"); - S += '('; - // first add the implicit argument. - S += StructRef + " *__cself, "; - std::string ParamStr; - for (BlockDecl::param_iterator AI = BD->param_begin(), - E = BD->param_end(); AI != E; ++AI) { - if (AI != BD->param_begin()) S += ", "; - ParamStr = (*AI)->getNameAsString(); - QualType QT = (*AI)->getType(); - if (convertBlockPointerToFunctionPointer(QT)) - QT.getAsStringInternal(ParamStr, Context->PrintingPolicy); - else - QT.getAsStringInternal(ParamStr, Context->PrintingPolicy); - S += ParamStr; - } - if (FT->isVariadic()) { - if (!BD->param_empty()) S += ", "; - S += "..."; - } - S += ')'; - } - S += " {\n"; - - // Create local declarations to avoid rewriting all closure decl ref exprs. - // First, emit a declaration for all "by ref" decls. - for (llvm::SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), - E = BlockByRefDecls.end(); I != E; ++I) { - S += " "; - std::string Name = (*I)->getNameAsString(); - std::string TypeString; - RewriteByRefString(TypeString, Name, (*I)); - TypeString += " *"; - Name = TypeString + Name; - S += Name + " = __cself->" + (*I)->getNameAsString() + "; // bound by ref\n"; - } - // Next, emit a declaration for all "by copy" declarations. - for (llvm::SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), - E = BlockByCopyDecls.end(); I != E; ++I) { - S += " "; - // Handle nested closure invocation. For example: - // - // void (^myImportedClosure)(void); - // myImportedClosure = ^(void) { setGlobalInt(x + y); }; - // - // void (^anotherClosure)(void); - // anotherClosure = ^(void) { - // myImportedClosure(); // import and invoke the closure - // }; - // - if (isTopLevelBlockPointerType((*I)->getType())) { - RewriteBlockPointerTypeVariable(S, (*I)); - S += " = ("; - RewriteBlockPointerType(S, (*I)->getType()); - S += ")"; - S += "__cself->" + (*I)->getNameAsString() + "; // bound by copy\n"; - } - else { - std::string Name = (*I)->getNameAsString(); - QualType QT = (*I)->getType(); - if (HasLocalVariableExternalStorage(*I)) - QT = Context->getPointerType(QT); - QT.getAsStringInternal(Name, Context->PrintingPolicy); - S += Name + " = __cself->" + - (*I)->getNameAsString() + "; // bound by copy\n"; - } - } - std::string RewrittenStr = RewrittenBlockExprs[CE]; - const char *cstr = RewrittenStr.c_str(); - while (*cstr++ != '{') ; - S += cstr; - S += "\n"; - return S; -} - -std::string RewriteObjC::SynthesizeBlockHelperFuncs(BlockExpr *CE, int i, - const char *funcName, - std::string Tag) { - std::string StructRef = "struct " + Tag; - std::string S = "static void __"; - - S += funcName; - S += "_block_copy_" + utostr(i); - S += "(" + StructRef; - S += "*dst, " + StructRef; - S += "*src) {"; - for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), - E = ImportedBlockDecls.end(); I != E; ++I) { - S += "_Block_object_assign((void*)&dst->"; - S += (*I)->getNameAsString(); - S += ", (void*)src->"; - S += (*I)->getNameAsString(); - if (BlockByRefDeclsPtrSet.count((*I))) - S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; - else - S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);"; - } - S += "}\n"; - - S += "\nstatic void __"; - S += funcName; - S += "_block_dispose_" + utostr(i); - S += "(" + StructRef; - S += "*src) {"; - for (llvm::SmallPtrSet<ValueDecl*,8>::iterator I = ImportedBlockDecls.begin(), - E = ImportedBlockDecls.end(); I != E; ++I) { - S += "_Block_object_dispose((void*)src->"; - S += (*I)->getNameAsString(); - if (BlockByRefDeclsPtrSet.count((*I))) - S += ", " + utostr(BLOCK_FIELD_IS_BYREF) + "/*BLOCK_FIELD_IS_BYREF*/);"; - else - S += ", " + utostr(BLOCK_FIELD_IS_OBJECT) + "/*BLOCK_FIELD_IS_OBJECT*/);"; - } - S += "}\n"; - return S; -} - -std::string RewriteObjC::SynthesizeBlockImpl(BlockExpr *CE, std::string Tag, - std::string Desc) { - std::string S = "\nstruct " + Tag; - std::string Constructor = " " + Tag; - - S += " {\n struct __block_impl impl;\n"; - S += " struct " + Desc; - S += "* Desc;\n"; - - Constructor += "(void *fp, "; // Invoke function pointer. - Constructor += "struct " + Desc; // Descriptor pointer. - Constructor += " *desc"; - - if (BlockDeclRefs.size()) { - // Output all "by copy" declarations. - for (llvm::SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), - E = BlockByCopyDecls.end(); I != E; ++I) { - S += " "; - std::string FieldName = (*I)->getNameAsString(); - std::string ArgName = "_" + FieldName; - // Handle nested closure invocation. For example: - // - // void (^myImportedBlock)(void); - // myImportedBlock = ^(void) { setGlobalInt(x + y); }; - // - // void (^anotherBlock)(void); - // anotherBlock = ^(void) { - // myImportedBlock(); // import and invoke the closure - // }; - // - if (isTopLevelBlockPointerType((*I)->getType())) { - S += "struct __block_impl *"; - Constructor += ", void *" + ArgName; - } else { - QualType QT = (*I)->getType(); - if (HasLocalVariableExternalStorage(*I)) - QT = Context->getPointerType(QT); - QT.getAsStringInternal(FieldName, Context->PrintingPolicy); - QT.getAsStringInternal(ArgName, Context->PrintingPolicy); - Constructor += ", " + ArgName; - } - S += FieldName + ";\n"; - } - // Output all "by ref" declarations. - for (llvm::SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), - E = BlockByRefDecls.end(); I != E; ++I) { - S += " "; - std::string FieldName = (*I)->getNameAsString(); - std::string ArgName = "_" + FieldName; - // Handle nested closure invocation. For example: - // - // void (^myImportedBlock)(void); - // myImportedBlock = ^(void) { setGlobalInt(x + y); }; - // - // void (^anotherBlock)(void); - // anotherBlock = ^(void) { - // myImportedBlock(); // import and invoke the closure - // }; - // - if (isTopLevelBlockPointerType((*I)->getType())) { - S += "struct __block_impl *"; - Constructor += ", void *" + ArgName; - } else { - std::string TypeString; - RewriteByRefString(TypeString, FieldName, (*I)); - TypeString += " *"; - FieldName = TypeString + FieldName; - ArgName = TypeString + ArgName; - Constructor += ", " + ArgName; - } - S += FieldName + "; // by ref\n"; - } - // Finish writing the constructor. - Constructor += ", int flags=0) {\n"; - if (GlobalVarDecl) - Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; - else - Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; - Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; - - Constructor += " Desc = desc;\n"; - - // Initialize all "by copy" arguments. - for (llvm::SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), - E = BlockByCopyDecls.end(); I != E; ++I) { - std::string Name = (*I)->getNameAsString(); - Constructor += " "; - if (isTopLevelBlockPointerType((*I)->getType())) - Constructor += Name + " = (struct __block_impl *)_"; - else - Constructor += Name + " = _"; - Constructor += Name + ";\n"; - } - // Initialize all "by ref" arguments. - for (llvm::SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), - E = BlockByRefDecls.end(); I != E; ++I) { - std::string Name = (*I)->getNameAsString(); - Constructor += " "; - if (isTopLevelBlockPointerType((*I)->getType())) - Constructor += Name + " = (struct __block_impl *)_"; - else - Constructor += Name + " = _"; - Constructor += Name + "->__forwarding;\n"; - } - } else { - // Finish writing the constructor. - Constructor += ", int flags=0) {\n"; - if (GlobalVarDecl) - Constructor += " impl.isa = &_NSConcreteGlobalBlock;\n"; - else - Constructor += " impl.isa = &_NSConcreteStackBlock;\n"; - Constructor += " impl.Flags = flags;\n impl.FuncPtr = fp;\n"; - Constructor += " Desc = desc;\n"; - } - Constructor += " "; - Constructor += "}\n"; - S += Constructor; - S += "};\n"; - return S; -} - -std::string RewriteObjC::SynthesizeBlockDescriptor(std::string DescTag, - std::string ImplTag, int i, - const char *FunName, - unsigned hasCopy) { - std::string S = "\nstatic struct " + DescTag; - - S += " {\n unsigned long reserved;\n"; - S += " unsigned long Block_size;\n"; - if (hasCopy) { - S += " void (*copy)(struct "; - S += ImplTag; S += "*, struct "; - S += ImplTag; S += "*);\n"; - - S += " void (*dispose)(struct "; - S += ImplTag; S += "*);\n"; - } - S += "} "; - - S += DescTag + "_DATA = { 0, sizeof(struct "; - S += ImplTag + ")"; - if (hasCopy) { - S += ", __" + std::string(FunName) + "_block_copy_" + utostr(i); - S += ", __" + std::string(FunName) + "_block_dispose_" + utostr(i); - } - S += "};\n"; - return S; -} - -void RewriteObjC::SynthesizeBlockLiterals(SourceLocation FunLocStart, - const char *FunName) { - // Insert declaration for the function in which block literal is used. - if (CurFunctionDeclToDeclareForBlock && !Blocks.empty()) - RewriteBlockLiteralFunctionDecl(CurFunctionDeclToDeclareForBlock); - bool RewriteSC = (GlobalVarDecl && - !Blocks.empty() && - GlobalVarDecl->getStorageClass() == VarDecl::Static && - GlobalVarDecl->getType().getCVRQualifiers()); - if (RewriteSC) { - std::string SC(" void __"); - SC += GlobalVarDecl->getNameAsString(); - SC += "() {}"; - InsertText(FunLocStart, SC); - } - - // Insert closures that were part of the function. - for (unsigned i = 0, count=0; i < Blocks.size(); i++) { - CollectBlockDeclRefInfo(Blocks[i]); - // Need to copy-in the inner copied-in variables not actually used in this - // block. - for (int j = 0; j < InnerDeclRefsCount[i]; j++) { - BlockDeclRefExpr *Exp = InnerDeclRefs[count++]; - ValueDecl *VD = Exp->getDecl(); - BlockDeclRefs.push_back(Exp); - if (!Exp->isByRef() && !BlockByCopyDeclsPtrSet.count(VD)) { - BlockByCopyDeclsPtrSet.insert(VD); - BlockByCopyDecls.push_back(VD); - } - if (Exp->isByRef() && !BlockByRefDeclsPtrSet.count(VD)) { - BlockByRefDeclsPtrSet.insert(VD); - BlockByRefDecls.push_back(VD); - } - } - - std::string ImplTag = "__" + std::string(FunName) + "_block_impl_" + utostr(i); - std::string DescTag = "__" + std::string(FunName) + "_block_desc_" + utostr(i); - - std::string CI = SynthesizeBlockImpl(Blocks[i], ImplTag, DescTag); - - InsertText(FunLocStart, CI); - - std::string CF = SynthesizeBlockFunc(Blocks[i], i, FunName, ImplTag); - - InsertText(FunLocStart, CF); - - if (ImportedBlockDecls.size()) { - std::string HF = SynthesizeBlockHelperFuncs(Blocks[i], i, FunName, ImplTag); - InsertText(FunLocStart, HF); - } - std::string BD = SynthesizeBlockDescriptor(DescTag, ImplTag, i, FunName, - ImportedBlockDecls.size() > 0); - InsertText(FunLocStart, BD); - - BlockDeclRefs.clear(); - BlockByRefDecls.clear(); - BlockByRefDeclsPtrSet.clear(); - BlockByCopyDecls.clear(); - BlockByCopyDeclsPtrSet.clear(); - ImportedBlockDecls.clear(); - } - if (RewriteSC) { - // Must insert any 'const/volatile/static here. Since it has been - // removed as result of rewriting of block literals. - std::string SC; - if (GlobalVarDecl->getStorageClass() == VarDecl::Static) - SC = "static "; - if (GlobalVarDecl->getType().isConstQualified()) - SC += "const "; - if (GlobalVarDecl->getType().isVolatileQualified()) - SC += "volatile "; - if (GlobalVarDecl->getType().isRestrictQualified()) - SC += "restrict "; - InsertText(FunLocStart, SC); - } - - Blocks.clear(); - InnerDeclRefsCount.clear(); - InnerDeclRefs.clear(); - RewrittenBlockExprs.clear(); -} - -void RewriteObjC::InsertBlockLiteralsWithinFunction(FunctionDecl *FD) { - SourceLocation FunLocStart = FD->getTypeSpecStartLoc(); - const char *FuncName = FD->getNameAsCString(); - - SynthesizeBlockLiterals(FunLocStart, FuncName); -} - -static void BuildUniqueMethodName(std::string &Name, - ObjCMethodDecl *MD) { - ObjCInterfaceDecl *IFace = MD->getClassInterface(); - Name = IFace->getNameAsCString(); - Name += "__" + MD->getSelector().getAsString(); - // Convert colons to underscores. - std::string::size_type loc = 0; - while ((loc = Name.find(":", loc)) != std::string::npos) - Name.replace(loc, 1, "_"); -} - -void RewriteObjC::InsertBlockLiteralsWithinMethod(ObjCMethodDecl *MD) { - //fprintf(stderr,"In InsertBlockLiteralsWitinMethod\n"); - //SourceLocation FunLocStart = MD->getLocStart(); - SourceLocation FunLocStart = MD->getLocStart(); - std::string FuncName; - BuildUniqueMethodName(FuncName, MD); - SynthesizeBlockLiterals(FunLocStart, FuncName.c_str()); -} - -void RewriteObjC::GetBlockDeclRefExprs(Stmt *S) { - for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); - CI != E; ++CI) - if (*CI) { - if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) - GetBlockDeclRefExprs(CBE->getBody()); - else - GetBlockDeclRefExprs(*CI); - } - // Handle specific things. - if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(S)) { - // FIXME: Handle enums. - if (!isa<FunctionDecl>(CDRE->getDecl())) - BlockDeclRefs.push_back(CDRE); - } - else if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) - if (HasLocalVariableExternalStorage(DRE->getDecl())) { - BlockDeclRefExpr *BDRE = - new (Context)BlockDeclRefExpr(DRE->getDecl(), DRE->getType(), - DRE->getLocation(), false); - BlockDeclRefs.push_back(BDRE); - } - - return; -} - -void RewriteObjC::GetInnerBlockDeclRefExprs(Stmt *S, - llvm::SmallVector<BlockDeclRefExpr *, 8> &InnerBlockDeclRefs, - llvm::SmallPtrSet<const DeclContext *, 8> &InnerContexts) { - for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); - CI != E; ++CI) - if (*CI) { - if (BlockExpr *CBE = dyn_cast<BlockExpr>(*CI)) { - InnerContexts.insert(cast<DeclContext>(CBE->getBlockDecl())); - GetInnerBlockDeclRefExprs(CBE->getBody(), - InnerBlockDeclRefs, - InnerContexts); - } - else - GetInnerBlockDeclRefExprs(*CI, - InnerBlockDeclRefs, - InnerContexts); - - } - // Handle specific things. - if (BlockDeclRefExpr *CDRE = dyn_cast<BlockDeclRefExpr>(S)) { - if (!isa<FunctionDecl>(CDRE->getDecl()) && - !InnerContexts.count(CDRE->getDecl()->getDeclContext())) - InnerBlockDeclRefs.push_back(CDRE); - } - else if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { - if (VarDecl *Var = dyn_cast<VarDecl>(DRE->getDecl())) - if (Var->isFunctionOrMethodVarDecl()) - ImportedLocalExternalDecls.insert(Var); - } - - return; -} - -/// convertFunctionTypeOfBlocks - This routine converts a function type -/// whose result type may be a block pointer or whose argument type(s) -/// might be block pointers to an equivalent funtion type replacing -/// all block pointers to function pointers. -QualType RewriteObjC::convertFunctionTypeOfBlocks(const FunctionType *FT) { - const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); - // FTP will be null for closures that don't take arguments. - // Generate a funky cast. - llvm::SmallVector<QualType, 8> ArgTypes; - QualType Res = FT->getResultType(); - bool HasBlockType = convertBlockPointerToFunctionPointer(Res); - - if (FTP) { - for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), - E = FTP->arg_type_end(); I && (I != E); ++I) { - QualType t = *I; - // Make sure we convert "t (^)(...)" to "t (*)(...)". - if (convertBlockPointerToFunctionPointer(t)) - HasBlockType = true; - ArgTypes.push_back(t); - } - } - QualType FuncType; - // FIXME. Does this work if block takes no argument but has a return type - // which is of block type? - if (HasBlockType) - FuncType = Context->getFunctionType(Res, - &ArgTypes[0], ArgTypes.size(), false/*no variadic*/, 0, - false, false, 0, 0, FunctionType::ExtInfo()); - else FuncType = QualType(FT, 0); - return FuncType; -} - -Stmt *RewriteObjC::SynthesizeBlockCall(CallExpr *Exp, const Expr *BlockExp) { - // Navigate to relevant type information. - const BlockPointerType *CPT = 0; - - if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(BlockExp)) { - CPT = DRE->getType()->getAs<BlockPointerType>(); - } else if (const BlockDeclRefExpr *CDRE = - dyn_cast<BlockDeclRefExpr>(BlockExp)) { - CPT = CDRE->getType()->getAs<BlockPointerType>(); - } else if (const MemberExpr *MExpr = dyn_cast<MemberExpr>(BlockExp)) { - CPT = MExpr->getType()->getAs<BlockPointerType>(); - } - else if (const ParenExpr *PRE = dyn_cast<ParenExpr>(BlockExp)) { - return SynthesizeBlockCall(Exp, PRE->getSubExpr()); - } - else if (const ImplicitCastExpr *IEXPR = dyn_cast<ImplicitCastExpr>(BlockExp)) - CPT = IEXPR->getType()->getAs<BlockPointerType>(); - else if (const ConditionalOperator *CEXPR = - dyn_cast<ConditionalOperator>(BlockExp)) { - Expr *LHSExp = CEXPR->getLHS(); - Stmt *LHSStmt = SynthesizeBlockCall(Exp, LHSExp); - Expr *RHSExp = CEXPR->getRHS(); - Stmt *RHSStmt = SynthesizeBlockCall(Exp, RHSExp); - Expr *CONDExp = CEXPR->getCond(); - ConditionalOperator *CondExpr = - new (Context) ConditionalOperator(CONDExp, - SourceLocation(), cast<Expr>(LHSStmt), - SourceLocation(), cast<Expr>(RHSStmt), - Exp->getType()); - return CondExpr; - } else if (const ObjCIvarRefExpr *IRE = dyn_cast<ObjCIvarRefExpr>(BlockExp)) { - CPT = IRE->getType()->getAs<BlockPointerType>(); - } else { - assert(1 && "RewriteBlockClass: Bad type"); - } - assert(CPT && "RewriteBlockClass: Bad type"); - const FunctionType *FT = CPT->getPointeeType()->getAs<FunctionType>(); - assert(FT && "RewriteBlockClass: Bad type"); - const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(FT); - // FTP will be null for closures that don't take arguments. - - RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, - SourceLocation(), - &Context->Idents.get("__block_impl")); - QualType PtrBlock = Context->getPointerType(Context->getTagDeclType(RD)); - - // Generate a funky cast. - llvm::SmallVector<QualType, 8> ArgTypes; - - // Push the block argument type. - ArgTypes.push_back(PtrBlock); - if (FTP) { - for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), - E = FTP->arg_type_end(); I && (I != E); ++I) { - QualType t = *I; - // Make sure we convert "t (^)(...)" to "t (*)(...)". - (void)convertBlockPointerToFunctionPointer(t); - ArgTypes.push_back(t); - } - } - // Now do the pointer to function cast. - QualType PtrToFuncCastType = Context->getFunctionType(Exp->getType(), - &ArgTypes[0], ArgTypes.size(), false/*no variadic*/, 0, - false, false, 0, 0, - FunctionType::ExtInfo()); - - PtrToFuncCastType = Context->getPointerType(PtrToFuncCastType); - - CastExpr *BlkCast = NoTypeInfoCStyleCastExpr(Context, PtrBlock, - CastExpr::CK_Unknown, - const_cast<Expr*>(BlockExp)); - // Don't forget the parens to enforce the proper binding. - ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), - BlkCast); - //PE->dump(); - - FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), - &Context->Idents.get("FuncPtr"), Context->VoidPtrTy, 0, - /*BitWidth=*/0, /*Mutable=*/true); - MemberExpr *ME = new (Context) MemberExpr(PE, true, FD, SourceLocation(), - FD->getType()); - - CastExpr *FunkCast = NoTypeInfoCStyleCastExpr(Context, PtrToFuncCastType, - CastExpr::CK_Unknown, ME); - PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), FunkCast); - - llvm::SmallVector<Expr*, 8> BlkExprs; - // Add the implicit argument. - BlkExprs.push_back(BlkCast); - // Add the user arguments. - for (CallExpr::arg_iterator I = Exp->arg_begin(), - E = Exp->arg_end(); I != E; ++I) { - BlkExprs.push_back(*I); - } - CallExpr *CE = new (Context) CallExpr(*Context, PE, &BlkExprs[0], - BlkExprs.size(), - Exp->getType(), SourceLocation()); - return CE; -} - -void RewriteObjC::RewriteBlockCall(CallExpr *Exp) { - Stmt *BlockCall = SynthesizeBlockCall(Exp, Exp->getCallee()); - ReplaceStmt(Exp, BlockCall); -} - -// We need to return the rewritten expression to handle cases where the -// BlockDeclRefExpr is embedded in another expression being rewritten. -// For example: -// -// int main() { -// __block Foo *f; -// __block int i; -// -// void (^myblock)() = ^() { -// [f test]; // f is a BlockDeclRefExpr embedded in a message (which is being rewritten). -// i = 77; -// }; -//} -Stmt *RewriteObjC::RewriteBlockDeclRefExpr(Expr *DeclRefExp) { - // Rewrite the byref variable into BYREFVAR->__forwarding->BYREFVAR - // for each DeclRefExp where BYREFVAR is name of the variable. - ValueDecl *VD; - bool isArrow = true; - if (BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(DeclRefExp)) - VD = BDRE->getDecl(); - else { - VD = cast<DeclRefExpr>(DeclRefExp)->getDecl(); - isArrow = false; - } - - FieldDecl *FD = FieldDecl::Create(*Context, 0, SourceLocation(), - &Context->Idents.get("__forwarding"), - Context->VoidPtrTy, 0, - /*BitWidth=*/0, /*Mutable=*/true); - MemberExpr *ME = new (Context) MemberExpr(DeclRefExp, isArrow, - FD, SourceLocation(), - FD->getType()); - - const char *Name = VD->getNameAsCString(); - FD = FieldDecl::Create(*Context, 0, SourceLocation(), - &Context->Idents.get(Name), - Context->VoidPtrTy, 0, - /*BitWidth=*/0, /*Mutable=*/true); - ME = new (Context) MemberExpr(ME, true, FD, SourceLocation(), - DeclRefExp->getType()); - - - - // Need parens to enforce precedence. - ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), - ME); - ReplaceStmt(DeclRefExp, PE); - return PE; -} - -// Rewrites the imported local variable V with external storage -// (static, extern, etc.) as *V -// -Stmt *RewriteObjC::RewriteLocalVariableExternalStorage(DeclRefExpr *DRE) { - ValueDecl *VD = DRE->getDecl(); - if (VarDecl *Var = dyn_cast<VarDecl>(VD)) - if (!ImportedLocalExternalDecls.count(Var)) - return DRE; - Expr *Exp = new (Context) UnaryOperator(DRE, UnaryOperator::Deref, - DRE->getType(), DRE->getLocation()); - // Need parens to enforce precedence. - ParenExpr *PE = new (Context) ParenExpr(SourceLocation(), SourceLocation(), - Exp); - ReplaceStmt(DRE, PE); - return PE; -} - -void RewriteObjC::RewriteCastExpr(CStyleCastExpr *CE) { - SourceLocation LocStart = CE->getLParenLoc(); - SourceLocation LocEnd = CE->getRParenLoc(); - - // Need to avoid trying to rewrite synthesized casts. - if (LocStart.isInvalid()) - return; - // Need to avoid trying to rewrite casts contained in macros. - if (!Rewriter::isRewritable(LocStart) || !Rewriter::isRewritable(LocEnd)) - return; - - const char *startBuf = SM->getCharacterData(LocStart); - const char *endBuf = SM->getCharacterData(LocEnd); - QualType QT = CE->getType(); - const Type* TypePtr = QT->getAs<Type>(); - if (isa<TypeOfExprType>(TypePtr)) { - const TypeOfExprType *TypeOfExprTypePtr = cast<TypeOfExprType>(TypePtr); - QT = TypeOfExprTypePtr->getUnderlyingExpr()->getType(); - std::string TypeAsString = "("; - RewriteBlockPointerType(TypeAsString, QT); - TypeAsString += ")"; - ReplaceText(LocStart, endBuf-startBuf+1, TypeAsString); - return; - } - // advance the location to startArgList. - const char *argPtr = startBuf; - - while (*argPtr++ && (argPtr < endBuf)) { - switch (*argPtr) { - case '^': - // Replace the '^' with '*'. - LocStart = LocStart.getFileLocWithOffset(argPtr-startBuf); - ReplaceText(LocStart, 1, "*"); - break; - } - } - return; -} - -void RewriteObjC::RewriteBlockPointerFunctionArgs(FunctionDecl *FD) { - SourceLocation DeclLoc = FD->getLocation(); - unsigned parenCount = 0; - - // We have 1 or more arguments that have closure pointers. - const char *startBuf = SM->getCharacterData(DeclLoc); - const char *startArgList = strchr(startBuf, '('); - - assert((*startArgList == '(') && "Rewriter fuzzy parser confused"); - - parenCount++; - // advance the location to startArgList. - DeclLoc = DeclLoc.getFileLocWithOffset(startArgList-startBuf); - assert((DeclLoc.isValid()) && "Invalid DeclLoc"); - - const char *argPtr = startArgList; - - while (*argPtr++ && parenCount) { - switch (*argPtr) { - case '^': - // Replace the '^' with '*'. - DeclLoc = DeclLoc.getFileLocWithOffset(argPtr-startArgList); - ReplaceText(DeclLoc, 1, "*"); - break; - case '(': - parenCount++; - break; - case ')': - parenCount--; - break; - } - } - return; -} - -bool RewriteObjC::PointerTypeTakesAnyBlockArguments(QualType QT) { - const FunctionProtoType *FTP; - const PointerType *PT = QT->getAs<PointerType>(); - if (PT) { - FTP = PT->getPointeeType()->getAs<FunctionProtoType>(); - } else { - const BlockPointerType *BPT = QT->getAs<BlockPointerType>(); - assert(BPT && "BlockPointerTypeTakeAnyBlockArguments(): not a block pointer type"); - FTP = BPT->getPointeeType()->getAs<FunctionProtoType>(); - } - if (FTP) { - for (FunctionProtoType::arg_type_iterator I = FTP->arg_type_begin(), - E = FTP->arg_type_end(); I != E; ++I) - if (isTopLevelBlockPointerType(*I)) - return true; - } - return false; -} - -void RewriteObjC::GetExtentOfArgList(const char *Name, const char *&LParen, - const char *&RParen) { - const char *argPtr = strchr(Name, '('); - assert((*argPtr == '(') && "Rewriter fuzzy parser confused"); - - LParen = argPtr; // output the start. - argPtr++; // skip past the left paren. - unsigned parenCount = 1; - - while (*argPtr && parenCount) { - switch (*argPtr) { - case '(': parenCount++; break; - case ')': parenCount--; break; - default: break; - } - if (parenCount) argPtr++; - } - assert((*argPtr == ')') && "Rewriter fuzzy parser confused"); - RParen = argPtr; // output the end -} - -void RewriteObjC::RewriteBlockPointerDecl(NamedDecl *ND) { - if (FunctionDecl *FD = dyn_cast<FunctionDecl>(ND)) { - RewriteBlockPointerFunctionArgs(FD); - return; - } - // Handle Variables and Typedefs. - SourceLocation DeclLoc = ND->getLocation(); - QualType DeclT; - if (VarDecl *VD = dyn_cast<VarDecl>(ND)) - DeclT = VD->getType(); - else if (TypedefDecl *TDD = dyn_cast<TypedefDecl>(ND)) - DeclT = TDD->getUnderlyingType(); - else if (FieldDecl *FD = dyn_cast<FieldDecl>(ND)) - DeclT = FD->getType(); - else - assert(0 && "RewriteBlockPointerDecl(): Decl type not yet handled"); - - const char *startBuf = SM->getCharacterData(DeclLoc); - const char *endBuf = startBuf; - // scan backward (from the decl location) for the end of the previous decl. - while (*startBuf != '^' && *startBuf != ';' && startBuf != MainFileStart) - startBuf--; - - // *startBuf != '^' if we are dealing with a pointer to function that - // may take block argument types (which will be handled below). - if (*startBuf == '^') { - // Replace the '^' with '*', computing a negative offset. - DeclLoc = DeclLoc.getFileLocWithOffset(startBuf-endBuf); - ReplaceText(DeclLoc, 1, "*"); - } - if (PointerTypeTakesAnyBlockArguments(DeclT)) { - // Replace the '^' with '*' for arguments. - DeclLoc = ND->getLocation(); - startBuf = SM->getCharacterData(DeclLoc); - const char *argListBegin, *argListEnd; - GetExtentOfArgList(startBuf, argListBegin, argListEnd); - while (argListBegin < argListEnd) { - if (*argListBegin == '^') { - SourceLocation CaretLoc = DeclLoc.getFileLocWithOffset(argListBegin-startBuf); - ReplaceText(CaretLoc, 1, "*"); - } - argListBegin++; - } - } - return; -} - - -/// SynthesizeByrefCopyDestroyHelper - This routine synthesizes: -/// void __Block_byref_id_object_copy(struct Block_byref_id_object *dst, -/// struct Block_byref_id_object *src) { -/// _Block_object_assign (&_dest->object, _src->object, -/// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT -/// [|BLOCK_FIELD_IS_WEAK]) // object -/// _Block_object_assign(&_dest->object, _src->object, -/// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK -/// [|BLOCK_FIELD_IS_WEAK]) // block -/// } -/// And: -/// void __Block_byref_id_object_dispose(struct Block_byref_id_object *_src) { -/// _Block_object_dispose(_src->object, -/// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_OBJECT -/// [|BLOCK_FIELD_IS_WEAK]) // object -/// _Block_object_dispose(_src->object, -/// BLOCK_BYREF_CALLER | BLOCK_FIELD_IS_BLOCK -/// [|BLOCK_FIELD_IS_WEAK]) // block -/// } - -std::string RewriteObjC::SynthesizeByrefCopyDestroyHelper(VarDecl *VD, - int flag) { - std::string S; - if (CopyDestroyCache.count(flag)) - return S; - CopyDestroyCache.insert(flag); - S = "static void __Block_byref_id_object_copy_"; - S += utostr(flag); - S += "(void *dst, void *src) {\n"; - - // offset into the object pointer is computed as: - // void * + void* + int + int + void* + void * - unsigned IntSize = - static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); - unsigned VoidPtrSize = - static_cast<unsigned>(Context->getTypeSize(Context->VoidPtrTy)); - - unsigned offset = (VoidPtrSize*4 + IntSize + IntSize)/8; - S += " _Block_object_assign((char*)dst + "; - S += utostr(offset); - S += ", *(void * *) ((char*)src + "; - S += utostr(offset); - S += "), "; - S += utostr(flag); - S += ");\n}\n"; - - S += "static void __Block_byref_id_object_dispose_"; - S += utostr(flag); - S += "(void *src) {\n"; - S += " _Block_object_dispose(*(void * *) ((char*)src + "; - S += utostr(offset); - S += "), "; - S += utostr(flag); - S += ");\n}\n"; - return S; -} - -/// RewriteByRefVar - For each __block typex ND variable this routine transforms -/// the declaration into: -/// struct __Block_byref_ND { -/// void *__isa; // NULL for everything except __weak pointers -/// struct __Block_byref_ND *__forwarding; -/// int32_t __flags; -/// int32_t __size; -/// void *__Block_byref_id_object_copy; // If variable is __block ObjC object -/// void *__Block_byref_id_object_dispose; // If variable is __block ObjC object -/// typex ND; -/// }; -/// -/// It then replaces declaration of ND variable with: -/// struct __Block_byref_ND ND = {__isa=0B, __forwarding=&ND, __flags=some_flag, -/// __size=sizeof(struct __Block_byref_ND), -/// ND=initializer-if-any}; -/// -/// -void RewriteObjC::RewriteByRefVar(VarDecl *ND) { - // Insert declaration for the function in which block literal is - // used. - if (CurFunctionDeclToDeclareForBlock) - RewriteBlockLiteralFunctionDecl(CurFunctionDeclToDeclareForBlock); - int flag = 0; - int isa = 0; - SourceLocation DeclLoc = ND->getTypeSpecStartLoc(); - if (DeclLoc.isInvalid()) - // If type location is missing, it is because of missing type (a warning). - // Use variable's location which is good for this case. - DeclLoc = ND->getLocation(); - const char *startBuf = SM->getCharacterData(DeclLoc); - SourceLocation X = ND->getLocEnd(); - X = SM->getInstantiationLoc(X); - const char *endBuf = SM->getCharacterData(X); - std::string Name(ND->getNameAsString()); - std::string ByrefType; - RewriteByRefString(ByrefType, Name, ND); - ByrefType += " {\n"; - ByrefType += " void *__isa;\n"; - RewriteByRefString(ByrefType, Name, ND); - ByrefType += " *__forwarding;\n"; - ByrefType += " int __flags;\n"; - ByrefType += " int __size;\n"; - // Add void *__Block_byref_id_object_copy; - // void *__Block_byref_id_object_dispose; if needed. - QualType Ty = ND->getType(); - bool HasCopyAndDispose = Context->BlockRequiresCopying(Ty); - if (HasCopyAndDispose) { - ByrefType += " void (*__Block_byref_id_object_copy)(void*, void*);\n"; - ByrefType += " void (*__Block_byref_id_object_dispose)(void*);\n"; - } - - Ty.getAsStringInternal(Name, Context->PrintingPolicy); - ByrefType += " " + Name + ";\n"; - ByrefType += "};\n"; - // Insert this type in global scope. It is needed by helper function. - SourceLocation FunLocStart; - if (CurFunctionDef) - FunLocStart = CurFunctionDef->getTypeSpecStartLoc(); - else { - assert(CurMethodDef && "RewriteByRefVar - CurMethodDef is null"); - FunLocStart = CurMethodDef->getLocStart(); - } - InsertText(FunLocStart, ByrefType); - if (Ty.isObjCGCWeak()) { - flag |= BLOCK_FIELD_IS_WEAK; - isa = 1; - } - - if (HasCopyAndDispose) { - flag = BLOCK_BYREF_CALLER; - QualType Ty = ND->getType(); - // FIXME. Handle __weak variable (BLOCK_FIELD_IS_WEAK) as well. - if (Ty->isBlockPointerType()) - flag |= BLOCK_FIELD_IS_BLOCK; - else - flag |= BLOCK_FIELD_IS_OBJECT; - std::string HF = SynthesizeByrefCopyDestroyHelper(ND, flag); - if (!HF.empty()) - InsertText(FunLocStart, HF); - } - - // struct __Block_byref_ND ND = - // {0, &ND, some_flag, __size=sizeof(struct __Block_byref_ND), - // initializer-if-any}; - bool hasInit = (ND->getInit() != 0); - unsigned flags = 0; - if (HasCopyAndDispose) - flags |= BLOCK_HAS_COPY_DISPOSE; - Name = ND->getNameAsString(); - ByrefType.clear(); - RewriteByRefString(ByrefType, Name, ND); - std::string ForwardingCastType("("); - ForwardingCastType += ByrefType + " *)"; - if (!hasInit) { - ByrefType += " " + Name + " = {(void*)"; - ByrefType += utostr(isa); - ByrefType += "," + ForwardingCastType + "&" + Name + ", "; - ByrefType += utostr(flags); - ByrefType += ", "; - ByrefType += "sizeof("; - RewriteByRefString(ByrefType, Name, ND); - ByrefType += ")"; - if (HasCopyAndDispose) { - ByrefType += ", __Block_byref_id_object_copy_"; - ByrefType += utostr(flag); - ByrefType += ", __Block_byref_id_object_dispose_"; - ByrefType += utostr(flag); - } - ByrefType += "};\n"; - ReplaceText(DeclLoc, endBuf-startBuf+Name.size(), ByrefType); - } - else { - SourceLocation startLoc; - Expr *E = ND->getInit(); - if (const CStyleCastExpr *ECE = dyn_cast<CStyleCastExpr>(E)) - startLoc = ECE->getLParenLoc(); - else - startLoc = E->getLocStart(); - startLoc = SM->getInstantiationLoc(startLoc); - endBuf = SM->getCharacterData(startLoc); - - ByrefType += " " + Name; - ByrefType += " = {(void*)"; - ByrefType += utostr(isa); - ByrefType += "," + ForwardingCastType + "&" + Name + ", "; - ByrefType += utostr(flags); - ByrefType += ", "; - ByrefType += "sizeof("; - RewriteByRefString(ByrefType, Name, ND); - ByrefType += "), "; - if (HasCopyAndDispose) { - ByrefType += "__Block_byref_id_object_copy_"; - ByrefType += utostr(flag); - ByrefType += ", __Block_byref_id_object_dispose_"; - ByrefType += utostr(flag); - ByrefType += ", "; - } - ReplaceText(DeclLoc, endBuf-startBuf, ByrefType); - - // Complete the newly synthesized compound expression by inserting a right - // curly brace before the end of the declaration. - // FIXME: This approach avoids rewriting the initializer expression. It - // also assumes there is only one declarator. For example, the following - // isn't currently supported by this routine (in general): - // - // double __block BYREFVAR = 1.34, BYREFVAR2 = 1.37; - // - const char *startBuf = SM->getCharacterData(startLoc); - const char *semiBuf = strchr(startBuf, ';'); - assert((*semiBuf == ';') && "RewriteByRefVar: can't find ';'"); - SourceLocation semiLoc = - startLoc.getFileLocWithOffset(semiBuf-startBuf); - - InsertText(semiLoc, "}"); - } - return; -} - -void RewriteObjC::CollectBlockDeclRefInfo(BlockExpr *Exp) { - // Add initializers for any closure decl refs. - GetBlockDeclRefExprs(Exp->getBody()); - if (BlockDeclRefs.size()) { - // Unique all "by copy" declarations. - for (unsigned i = 0; i < BlockDeclRefs.size(); i++) - if (!BlockDeclRefs[i]->isByRef()) { - if (!BlockByCopyDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) { - BlockByCopyDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl()); - BlockByCopyDecls.push_back(BlockDeclRefs[i]->getDecl()); - } - } - // Unique all "by ref" declarations. - for (unsigned i = 0; i < BlockDeclRefs.size(); i++) - if (BlockDeclRefs[i]->isByRef()) { - if (!BlockByRefDeclsPtrSet.count(BlockDeclRefs[i]->getDecl())) { - BlockByRefDeclsPtrSet.insert(BlockDeclRefs[i]->getDecl()); - BlockByRefDecls.push_back(BlockDeclRefs[i]->getDecl()); - } - } - // Find any imported blocks...they will need special attention. - for (unsigned i = 0; i < BlockDeclRefs.size(); i++) - if (BlockDeclRefs[i]->isByRef() || - BlockDeclRefs[i]->getType()->isObjCObjectPointerType() || - BlockDeclRefs[i]->getType()->isBlockPointerType()) - ImportedBlockDecls.insert(BlockDeclRefs[i]->getDecl()); - } -} - -FunctionDecl *RewriteObjC::SynthBlockInitFunctionDecl(const char *name) { - IdentifierInfo *ID = &Context->Idents.get(name); - QualType FType = Context->getFunctionNoProtoType(Context->VoidPtrTy); - return FunctionDecl::Create(*Context, TUDecl,SourceLocation(), - ID, FType, 0, FunctionDecl::Extern, - FunctionDecl::None, false, false); -} - -Stmt *RewriteObjC::SynthBlockInitExpr(BlockExpr *Exp, - const llvm::SmallVector<BlockDeclRefExpr *, 8> &InnerBlockDeclRefs) { - Blocks.push_back(Exp); - - CollectBlockDeclRefInfo(Exp); - - // Add inner imported variables now used in current block. - int countOfInnerDecls = 0; - if (!InnerBlockDeclRefs.empty()) { - for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++) { - BlockDeclRefExpr *Exp = InnerBlockDeclRefs[i]; - ValueDecl *VD = Exp->getDecl(); - if (!Exp->isByRef() && !BlockByCopyDeclsPtrSet.count(VD)) { - // We need to save the copied-in variables in nested - // blocks because it is needed at the end for some of the API generations. - // See SynthesizeBlockLiterals routine. - InnerDeclRefs.push_back(Exp); countOfInnerDecls++; - BlockDeclRefs.push_back(Exp); - BlockByCopyDeclsPtrSet.insert(VD); - BlockByCopyDecls.push_back(VD); - } - if (Exp->isByRef() && !BlockByRefDeclsPtrSet.count(VD)) { - InnerDeclRefs.push_back(Exp); countOfInnerDecls++; - BlockDeclRefs.push_back(Exp); - BlockByRefDeclsPtrSet.insert(VD); - BlockByRefDecls.push_back(VD); - } - } - // Find any imported blocks...they will need special attention. - for (unsigned i = 0; i < InnerBlockDeclRefs.size(); i++) - if (InnerBlockDeclRefs[i]->isByRef() || - InnerBlockDeclRefs[i]->getType()->isObjCObjectPointerType() || - InnerBlockDeclRefs[i]->getType()->isBlockPointerType()) - ImportedBlockDecls.insert(InnerBlockDeclRefs[i]->getDecl()); - } - InnerDeclRefsCount.push_back(countOfInnerDecls); - - std::string FuncName; - - if (CurFunctionDef) - FuncName = CurFunctionDef->getNameAsString(); - else if (CurMethodDef) - BuildUniqueMethodName(FuncName, CurMethodDef); - else if (GlobalVarDecl) - FuncName = std::string(GlobalVarDecl->getNameAsString()); - - std::string BlockNumber = utostr(Blocks.size()-1); - - std::string Tag = "__" + FuncName + "_block_impl_" + BlockNumber; - std::string Func = "__" + FuncName + "_block_func_" + BlockNumber; - - // Get a pointer to the function type so we can cast appropriately. - QualType BFT = convertFunctionTypeOfBlocks(Exp->getFunctionType()); - QualType FType = Context->getPointerType(BFT); - - FunctionDecl *FD; - Expr *NewRep; - - // Simulate a contructor call... - FD = SynthBlockInitFunctionDecl(Tag.c_str()); - DeclRefExpr *DRE = new (Context) DeclRefExpr(FD, FType, SourceLocation()); - - llvm::SmallVector<Expr*, 4> InitExprs; - - // Initialize the block function. - FD = SynthBlockInitFunctionDecl(Func.c_str()); - DeclRefExpr *Arg = new (Context) DeclRefExpr(FD, FD->getType(), - SourceLocation()); - CastExpr *castExpr = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy, - CastExpr::CK_Unknown, Arg); - InitExprs.push_back(castExpr); - - // Initialize the block descriptor. - std::string DescData = "__" + FuncName + "_block_desc_" + BlockNumber + "_DATA"; - - VarDecl *NewVD = VarDecl::Create(*Context, TUDecl, SourceLocation(), - &Context->Idents.get(DescData.c_str()), - Context->VoidPtrTy, 0, - VarDecl::Static, VarDecl::None); - UnaryOperator *DescRefExpr = new (Context) UnaryOperator( - new (Context) DeclRefExpr(NewVD, - Context->VoidPtrTy, SourceLocation()), - UnaryOperator::AddrOf, - Context->getPointerType(Context->VoidPtrTy), - SourceLocation()); - InitExprs.push_back(DescRefExpr); - - // Add initializers for any closure decl refs. - if (BlockDeclRefs.size()) { - Expr *Exp; - // Output all "by copy" declarations. - for (llvm::SmallVector<ValueDecl*,8>::iterator I = BlockByCopyDecls.begin(), - E = BlockByCopyDecls.end(); I != E; ++I) { - if (isObjCType((*I)->getType())) { - // FIXME: Conform to ABI ([[obj retain] autorelease]). - FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); - Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); - if (HasLocalVariableExternalStorage(*I)) { - QualType QT = (*I)->getType(); - QT = Context->getPointerType(QT); - Exp = new (Context) UnaryOperator(Exp, UnaryOperator::AddrOf, QT, - SourceLocation()); - } - } else if (isTopLevelBlockPointerType((*I)->getType())) { - FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); - Arg = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); - Exp = NoTypeInfoCStyleCastExpr(Context, Context->VoidPtrTy, - CastExpr::CK_Unknown, Arg); - } else { - FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); - Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); - if (HasLocalVariableExternalStorage(*I)) { - QualType QT = (*I)->getType(); - QT = Context->getPointerType(QT); - Exp = new (Context) UnaryOperator(Exp, UnaryOperator::AddrOf, QT, - SourceLocation()); - } - - } - InitExprs.push_back(Exp); - } - // Output all "by ref" declarations. - for (llvm::SmallVector<ValueDecl*,8>::iterator I = BlockByRefDecls.begin(), - E = BlockByRefDecls.end(); I != E; ++I) { - ValueDecl *ND = (*I); - std::string Name(ND->getNameAsString()); - std::string RecName; - RewriteByRefString(RecName, Name, ND); - IdentifierInfo *II = &Context->Idents.get(RecName.c_str() - + sizeof("struct")); - RecordDecl *RD = RecordDecl::Create(*Context, TTK_Struct, TUDecl, - SourceLocation(), II); - assert(RD && "SynthBlockInitExpr(): Can't find RecordDecl"); - QualType castT = Context->getPointerType(Context->getTagDeclType(RD)); - - FD = SynthBlockInitFunctionDecl((*I)->getNameAsCString()); - Exp = new (Context) DeclRefExpr(FD, FD->getType(), SourceLocation()); - Exp = new (Context) UnaryOperator(Exp, UnaryOperator::AddrOf, - Context->getPointerType(Exp->getType()), - SourceLocation()); - Exp = NoTypeInfoCStyleCastExpr(Context, castT, CastExpr::CK_Unknown, Exp); - InitExprs.push_back(Exp); - } - } - if (ImportedBlockDecls.size()) { - // generate BLOCK_HAS_COPY_DISPOSE(have helper funcs) | BLOCK_HAS_DESCRIPTOR - int flag = (BLOCK_HAS_COPY_DISPOSE | BLOCK_HAS_DESCRIPTOR); - unsigned IntSize = - static_cast<unsigned>(Context->getTypeSize(Context->IntTy)); - Expr *FlagExp = new (Context) IntegerLiteral(llvm::APInt(IntSize, flag), - Context->IntTy, SourceLocation()); - InitExprs.push_back(FlagExp); - } - NewRep = new (Context) CallExpr(*Context, DRE, &InitExprs[0], InitExprs.size(), - FType, SourceLocation()); - NewRep = new (Context) UnaryOperator(NewRep, UnaryOperator::AddrOf, - Context->getPointerType(NewRep->getType()), - SourceLocation()); - NewRep = NoTypeInfoCStyleCastExpr(Context, FType, CastExpr::CK_Unknown, - NewRep); - BlockDeclRefs.clear(); - BlockByRefDecls.clear(); - BlockByRefDeclsPtrSet.clear(); - BlockByCopyDecls.clear(); - BlockByCopyDeclsPtrSet.clear(); - ImportedBlockDecls.clear(); - return NewRep; -} - -//===----------------------------------------------------------------------===// -// Function Body / Expression rewriting -//===----------------------------------------------------------------------===// - -// This is run as a first "pass" prior to RewriteFunctionBodyOrGlobalInitializer(). -// The allows the main rewrite loop to associate all ObjCPropertyRefExprs with -// their respective BinaryOperator. Without this knowledge, we'd need to rewrite -// the ObjCPropertyRefExpr twice (once as a getter, and later as a setter). -// Since the rewriter isn't capable of rewriting rewritten code, it's important -// we get this right. -void RewriteObjC::CollectPropertySetters(Stmt *S) { - // Perform a bottom up traversal of all children. - for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); - CI != E; ++CI) - if (*CI) - CollectPropertySetters(*CI); - - if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(S)) { - if (BinOp->isAssignmentOp()) { - if (ObjCPropertyRefExpr *PRE = dyn_cast<ObjCPropertyRefExpr>(BinOp->getLHS())) - PropSetters[PRE] = BinOp; - } - } -} - -Stmt *RewriteObjC::RewriteFunctionBodyOrGlobalInitializer(Stmt *S) { - if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || - isa<DoStmt>(S) || isa<ForStmt>(S)) - Stmts.push_back(S); - else if (isa<ObjCForCollectionStmt>(S)) { - Stmts.push_back(S); - ObjCBcLabelNo.push_back(++BcLabelCount); - } - - SourceRange OrigStmtRange = S->getSourceRange(); - - // Perform a bottom up rewrite of all children. - for (Stmt::child_iterator CI = S->child_begin(), E = S->child_end(); - CI != E; ++CI) - if (*CI) { - Stmt *newStmt; - Stmt *S = (*CI); - if (ObjCIvarRefExpr *IvarRefExpr = dyn_cast<ObjCIvarRefExpr>(S)) { - Expr *OldBase = IvarRefExpr->getBase(); - bool replaced = false; - newStmt = RewriteObjCNestedIvarRefExpr(S, replaced); - if (replaced) { - if (ObjCIvarRefExpr *IRE = dyn_cast<ObjCIvarRefExpr>(newStmt)) - ReplaceStmt(OldBase, IRE->getBase()); - else - ReplaceStmt(S, newStmt); - } - } - else - newStmt = RewriteFunctionBodyOrGlobalInitializer(S); - if (newStmt) - *CI = newStmt; - } - - if (BlockExpr *BE = dyn_cast<BlockExpr>(S)) { - llvm::SmallVector<BlockDeclRefExpr *, 8> InnerBlockDeclRefs; - llvm::SmallPtrSet<const DeclContext *, 8> InnerContexts; - InnerContexts.insert(BE->getBlockDecl()); - ImportedLocalExternalDecls.clear(); - GetInnerBlockDeclRefExprs(BE->getBody(), - InnerBlockDeclRefs, InnerContexts); - // Rewrite the block body in place. - RewriteFunctionBodyOrGlobalInitializer(BE->getBody()); - ImportedLocalExternalDecls.clear(); - // Now we snarf the rewritten text and stash it away for later use. - std::string Str = Rewrite.getRewrittenText(BE->getSourceRange()); - RewrittenBlockExprs[BE] = Str; - - Stmt *blockTranscribed = SynthBlockInitExpr(BE, InnerBlockDeclRefs); - - //blockTranscribed->dump(); - ReplaceStmt(S, blockTranscribed); - return blockTranscribed; - } - // Handle specific things. - if (ObjCEncodeExpr *AtEncode = dyn_cast<ObjCEncodeExpr>(S)) - return RewriteAtEncode(AtEncode); - - if (ObjCPropertyRefExpr *PropRefExpr = dyn_cast<ObjCPropertyRefExpr>(S)) { - BinaryOperator *BinOp = PropSetters[PropRefExpr]; - if (BinOp) { - // Because the rewriter doesn't allow us to rewrite rewritten code, - // we need to rewrite the right hand side prior to rewriting the setter. - DisableReplaceStmt = true; - // Save the source range. Even if we disable the replacement, the - // rewritten node will have been inserted into the tree. If the synthesized - // node is at the 'end', the rewriter will fail. Consider this: - // self.errorHandler = handler ? handler : - // ^(NSURL *errorURL, NSError *error) { return (BOOL)1; }; - SourceRange SrcRange = BinOp->getSourceRange(); - Stmt *newStmt = RewriteFunctionBodyOrGlobalInitializer(BinOp->getRHS()); - DisableReplaceStmt = false; - // - // Unlike the main iterator, we explicily avoid changing 'BinOp'. If - // we changed the RHS of BinOp, the rewriter would fail (since it needs - // to see the original expression). Consider this example: - // - // Foo *obj1, *obj2; - // - // obj1.i = [obj2 rrrr]; - // - // 'BinOp' for the previous expression looks like: - // - // (BinaryOperator 0x231ccf0 'int' '=' - // (ObjCPropertyRefExpr 0x231cc70 'int' Kind=PropertyRef Property="i" - // (DeclRefExpr 0x231cc50 'Foo *' Var='obj1' 0x231cbb0)) - // (ObjCMessageExpr 0x231ccb0 'int' selector=rrrr - // (DeclRefExpr 0x231cc90 'Foo *' Var='obj2' 0x231cbe0))) - // - // 'newStmt' represents the rewritten message expression. For example: - // - // (CallExpr 0x231d300 'id':'struct objc_object *' - // (ParenExpr 0x231d2e0 'int (*)(id, SEL)' - // (CStyleCastExpr 0x231d2c0 'int (*)(id, SEL)' - // (CStyleCastExpr 0x231d220 'void *' - // (DeclRefExpr 0x231d200 'id (id, SEL, ...)' FunctionDecl='objc_msgSend' 0x231cdc0)))) - // - // Note that 'newStmt' is passed to RewritePropertySetter so that it - // can be used as the setter argument. ReplaceStmt() will still 'see' - // the original RHS (since we haven't altered BinOp). - // - // This implies the Rewrite* routines can no longer delete the original - // node. As a result, we now leak the original AST nodes. - // - return RewritePropertySetter(BinOp, dyn_cast<Expr>(newStmt), SrcRange); - } else { - return RewritePropertyGetter(PropRefExpr); - } - } - if (ObjCSelectorExpr *AtSelector = dyn_cast<ObjCSelectorExpr>(S)) - return RewriteAtSelector(AtSelector); - - if (ObjCStringLiteral *AtString = dyn_cast<ObjCStringLiteral>(S)) - return RewriteObjCStringLiteral(AtString); - - if (ObjCMessageExpr *MessExpr = dyn_cast<ObjCMessageExpr>(S)) { -#if 0 - // Before we rewrite it, put the original message expression in a comment. - SourceLocation startLoc = MessExpr->getLocStart(); - SourceLocation endLoc = MessExpr->getLocEnd(); - - const char *startBuf = SM->getCharacterData(startLoc); - const char *endBuf = SM->getCharacterData(endLoc); - - std::string messString; - messString += "// "; - messString.append(startBuf, endBuf-startBuf+1); - messString += "\n"; - - // FIXME: Missing definition of - // InsertText(clang::SourceLocation, char const*, unsigned int). - // InsertText(startLoc, messString.c_str(), messString.size()); - // Tried this, but it didn't work either... - // ReplaceText(startLoc, 0, messString.c_str(), messString.size()); -#endif - return RewriteMessageExpr(MessExpr); - } - - if (ObjCAtTryStmt *StmtTry = dyn_cast<ObjCAtTryStmt>(S)) - return RewriteObjCTryStmt(StmtTry); - - if (ObjCAtSynchronizedStmt *StmtTry = dyn_cast<ObjCAtSynchronizedStmt>(S)) - return RewriteObjCSynchronizedStmt(StmtTry); - - if (ObjCAtThrowStmt *StmtThrow = dyn_cast<ObjCAtThrowStmt>(S)) - return RewriteObjCThrowStmt(StmtThrow); - - if (ObjCProtocolExpr *ProtocolExp = dyn_cast<ObjCProtocolExpr>(S)) - return RewriteObjCProtocolExpr(ProtocolExp); - - if (ObjCForCollectionStmt *StmtForCollection = - dyn_cast<ObjCForCollectionStmt>(S)) - return RewriteObjCForCollectionStmt(StmtForCollection, - OrigStmtRange.getEnd()); - if (BreakStmt *StmtBreakStmt = - dyn_cast<BreakStmt>(S)) - return RewriteBreakStmt(StmtBreakStmt); - if (ContinueStmt *StmtContinueStmt = - dyn_cast<ContinueStmt>(S)) - return RewriteContinueStmt(StmtContinueStmt); - - // Need to check for protocol refs (id <P>, Foo <P> *) in variable decls - // and cast exprs. - if (DeclStmt *DS = dyn_cast<DeclStmt>(S)) { - // FIXME: What we're doing here is modifying the type-specifier that - // precedes the first Decl. In the future the DeclGroup should have - // a separate type-specifier that we can rewrite. - // NOTE: We need to avoid rewriting the DeclStmt if it is within - // the context of an ObjCForCollectionStmt. For example: - // NSArray *someArray; - // for (id <FooProtocol> index in someArray) ; - // This is because RewriteObjCForCollectionStmt() does textual rewriting - // and it depends on the original text locations/positions. - if (Stmts.empty() || !isa<ObjCForCollectionStmt>(Stmts.back())) - RewriteObjCQualifiedInterfaceTypes(*DS->decl_begin()); - - // Blocks rewrite rules. - for (DeclStmt::decl_iterator DI = DS->decl_begin(), DE = DS->decl_end(); - DI != DE; ++DI) { - Decl *SD = *DI; - if (ValueDecl *ND = dyn_cast<ValueDecl>(SD)) { - if (isTopLevelBlockPointerType(ND->getType())) - RewriteBlockPointerDecl(ND); - else if (ND->getType()->isFunctionPointerType()) - CheckFunctionPointerDecl(ND->getType(), ND); - if (VarDecl *VD = dyn_cast<VarDecl>(SD)) { - if (VD->hasAttr<BlocksAttr>()) { - static unsigned uniqueByrefDeclCount = 0; - assert(!BlockByRefDeclNo.count(ND) && - "RewriteFunctionBodyOrGlobalInitializer: Duplicate byref decl"); - BlockByRefDeclNo[ND] = uniqueByrefDeclCount++; - RewriteByRefVar(VD); - } - else - RewriteTypeOfDecl(VD); - } - } - if (TypedefDecl *TD = dyn_cast<TypedefDecl>(SD)) { - if (isTopLevelBlockPointerType(TD->getUnderlyingType())) - RewriteBlockPointerDecl(TD); - else if (TD->getUnderlyingType()->isFunctionPointerType()) - CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); - } - } - } - - if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) - RewriteObjCQualifiedInterfaceTypes(CE); - - if (isa<SwitchStmt>(S) || isa<WhileStmt>(S) || - isa<DoStmt>(S) || isa<ForStmt>(S)) { - assert(!Stmts.empty() && "Statement stack is empty"); - assert ((isa<SwitchStmt>(Stmts.back()) || isa<WhileStmt>(Stmts.back()) || - isa<DoStmt>(Stmts.back()) || isa<ForStmt>(Stmts.back())) - && "Statement stack mismatch"); - Stmts.pop_back(); - } - // Handle blocks rewriting. - if (BlockDeclRefExpr *BDRE = dyn_cast<BlockDeclRefExpr>(S)) { - if (BDRE->isByRef()) - return RewriteBlockDeclRefExpr(BDRE); - } - if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(S)) { - ValueDecl *VD = DRE->getDecl(); - if (VD->hasAttr<BlocksAttr>()) - return RewriteBlockDeclRefExpr(DRE); - if (HasLocalVariableExternalStorage(VD)) - return RewriteLocalVariableExternalStorage(DRE); - } - - if (CallExpr *CE = dyn_cast<CallExpr>(S)) { - if (CE->getCallee()->getType()->isBlockPointerType()) { - Stmt *BlockCall = SynthesizeBlockCall(CE, CE->getCallee()); - ReplaceStmt(S, BlockCall); - return BlockCall; - } - } - if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(S)) { - RewriteCastExpr(CE); - } -#if 0 - if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(S)) { - CastExpr *Replacement = new (Context) CastExpr(ICE->getType(), ICE->getSubExpr(), SourceLocation()); - // Get the new text. - std::string SStr; - llvm::raw_string_ostream Buf(SStr); - Replacement->printPretty(Buf, *Context); - const std::string &Str = Buf.str(); - - printf("CAST = %s\n", &Str[0]); - InsertText(ICE->getSubExpr()->getLocStart(), &Str[0], Str.size()); - delete S; - return Replacement; - } -#endif - // Return this stmt unmodified. - return S; -} - -void RewriteObjC::RewriteRecordBody(RecordDecl *RD) { - for (RecordDecl::field_iterator i = RD->field_begin(), - e = RD->field_end(); i != e; ++i) { - FieldDecl *FD = *i; - if (isTopLevelBlockPointerType(FD->getType())) - RewriteBlockPointerDecl(FD); - if (FD->getType()->isObjCQualifiedIdType() || - FD->getType()->isObjCQualifiedInterfaceType()) - RewriteObjCQualifiedInterfaceTypes(FD); - } -} - -/// HandleDeclInMainFile - This is called for each top-level decl defined in the -/// main file of the input. -void RewriteObjC::HandleDeclInMainFile(Decl *D) { - if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { - if (FD->isOverloadedOperator()) - return; - - // Since function prototypes don't have ParmDecl's, we check the function - // prototype. This enables us to rewrite function declarations and - // definitions using the same code. - RewriteBlocksInFunctionProtoType(FD->getType(), FD); - - // FIXME: If this should support Obj-C++, support CXXTryStmt - if (CompoundStmt *Body = FD->getCompoundBody()) { - CurFunctionDef = FD; - CurFunctionDeclToDeclareForBlock = FD; - CollectPropertySetters(Body); - CurrentBody = Body; - Body = - cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); - FD->setBody(Body); - CurrentBody = 0; - if (PropParentMap) { - delete PropParentMap; - PropParentMap = 0; - } - // This synthesizes and inserts the block "impl" struct, invoke function, - // and any copy/dispose helper functions. - InsertBlockLiteralsWithinFunction(FD); - CurFunctionDef = 0; - CurFunctionDeclToDeclareForBlock = 0; - } - return; - } - if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { - if (CompoundStmt *Body = MD->getCompoundBody()) { - CurMethodDef = MD; - CollectPropertySetters(Body); - CurrentBody = Body; - Body = - cast_or_null<CompoundStmt>(RewriteFunctionBodyOrGlobalInitializer(Body)); - MD->setBody(Body); - CurrentBody = 0; - if (PropParentMap) { - delete PropParentMap; - PropParentMap = 0; - } - InsertBlockLiteralsWithinMethod(MD); - CurMethodDef = 0; - } - } - if (ObjCImplementationDecl *CI = dyn_cast<ObjCImplementationDecl>(D)) - ClassImplementation.push_back(CI); - else if (ObjCCategoryImplDecl *CI = dyn_cast<ObjCCategoryImplDecl>(D)) - CategoryImplementation.push_back(CI); - else if (ObjCClassDecl *CD = dyn_cast<ObjCClassDecl>(D)) - RewriteForwardClassDecl(CD); - else if (VarDecl *VD = dyn_cast<VarDecl>(D)) { - RewriteObjCQualifiedInterfaceTypes(VD); - if (isTopLevelBlockPointerType(VD->getType())) - RewriteBlockPointerDecl(VD); - else if (VD->getType()->isFunctionPointerType()) { - CheckFunctionPointerDecl(VD->getType(), VD); - if (VD->getInit()) { - if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { - RewriteCastExpr(CE); - } - } - } else if (VD->getType()->isRecordType()) { - RecordDecl *RD = VD->getType()->getAs<RecordType>()->getDecl(); - if (RD->isDefinition()) - RewriteRecordBody(RD); - } - if (VD->getInit()) { - GlobalVarDecl = VD; - CollectPropertySetters(VD->getInit()); - CurrentBody = VD->getInit(); - RewriteFunctionBodyOrGlobalInitializer(VD->getInit()); - CurrentBody = 0; - if (PropParentMap) { - delete PropParentMap; - PropParentMap = 0; - } - SynthesizeBlockLiterals(VD->getTypeSpecStartLoc(), - VD->getNameAsCString()); - GlobalVarDecl = 0; - - // This is needed for blocks. - if (CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(VD->getInit())) { - RewriteCastExpr(CE); - } - } - return; - } - if (TypedefDecl *TD = dyn_cast<TypedefDecl>(D)) { - if (isTopLevelBlockPointerType(TD->getUnderlyingType())) - RewriteBlockPointerDecl(TD); - else if (TD->getUnderlyingType()->isFunctionPointerType()) - CheckFunctionPointerDecl(TD->getUnderlyingType(), TD); - return; - } - if (RecordDecl *RD = dyn_cast<RecordDecl>(D)) { - if (RD->isDefinition()) - RewriteRecordBody(RD); - return; - } - // Nothing yet. -} - -void RewriteObjC::HandleTranslationUnit(ASTContext &C) { - if (Diags.hasErrorOccurred()) - return; - - RewriteInclude(); - - // Here's a great place to add any extra declarations that may be needed. - // Write out meta data for each @protocol(<expr>). - for (llvm::SmallPtrSet<ObjCProtocolDecl *,8>::iterator I = ProtocolExprDecls.begin(), - E = ProtocolExprDecls.end(); I != E; ++I) - RewriteObjCProtocolMetaData(*I, "", "", Preamble); - - InsertText(SM->getLocForStartOfFile(MainFileID), Preamble, false); - if (ClassImplementation.size() || CategoryImplementation.size()) - RewriteImplementations(); - - // Get the buffer corresponding to MainFileID. If we haven't changed it, then - // we are done. - if (const RewriteBuffer *RewriteBuf = - Rewrite.getRewriteBufferFor(MainFileID)) { - //printf("Changed:\n"); - *OutFile << std::string(RewriteBuf->begin(), RewriteBuf->end()); - } else { - llvm::errs() << "No changes\n"; - } - - if (ClassImplementation.size() || CategoryImplementation.size() || - ProtocolExprDecls.size()) { - // Rewrite Objective-c meta data* - std::string ResultStr; - SynthesizeMetaDataIntoBuffer(ResultStr); - // Emit metadata. - *OutFile << ResultStr; - } - OutFile->flush(); -} diff --git a/contrib/llvm/tools/clang/lib/Frontend/RewriteTest.cpp b/contrib/llvm/tools/clang/lib/Frontend/RewriteTest.cpp deleted file mode 100644 index 0414678..0000000 --- a/contrib/llvm/tools/clang/lib/Frontend/RewriteTest.cpp +++ /dev/null @@ -1,39 +0,0 @@ -//===--- RewriteTest.cpp - Rewriter playground ----------------------------===// -// -// The LLVM Compiler Infrastructure -// -// This file is distributed under the University of Illinois Open Source -// License. See LICENSE.TXT for details. -// -//===----------------------------------------------------------------------===// -// -// This is a testbed. -// -//===----------------------------------------------------------------------===// - -#include "clang/Frontend/Utils.h" -#include "clang/Lex/Preprocessor.h" -#include "clang/Rewrite/TokenRewriter.h" -#include "llvm/Support/raw_ostream.h" - -void clang::DoRewriteTest(Preprocessor &PP, llvm::raw_ostream* OS) { - SourceManager &SM = PP.getSourceManager(); - const LangOptions &LangOpts = PP.getLangOptions(); - - TokenRewriter Rewriter(SM.getMainFileID(), SM, LangOpts); - - // Throw <i> </i> tags around comments. - for (TokenRewriter::token_iterator I = Rewriter.token_begin(), - E = Rewriter.token_end(); I != E; ++I) { - if (I->isNot(tok::comment)) continue; - - Rewriter.AddTokenBefore(I, "<i>"); - Rewriter.AddTokenAfter(I, "</i>"); - } - - - // Print out the output. - for (TokenRewriter::token_iterator I = Rewriter.token_begin(), - E = Rewriter.token_end(); I != E; ++I) - *OS << PP.getSpelling(*I); -} diff --git a/contrib/llvm/tools/clang/lib/Frontend/TextDiagnosticPrinter.cpp b/contrib/llvm/tools/clang/lib/Frontend/TextDiagnosticPrinter.cpp index 6ccf4f1..1b5b7e2 100644 --- a/contrib/llvm/tools/clang/lib/Frontend/TextDiagnosticPrinter.cpp +++ b/contrib/llvm/tools/clang/lib/Frontend/TextDiagnosticPrinter.cpp @@ -70,7 +70,7 @@ PrintIncludeStack(SourceLocation Loc, const SourceManager &SM) { /// HighlightRange - Given a SourceRange and a line number, highlight (with ~'s) /// any characters in LineNo that intersect the SourceRange. -void TextDiagnosticPrinter::HighlightRange(const SourceRange &R, +void TextDiagnosticPrinter::HighlightRange(const CharSourceRange &R, const SourceManager &SM, unsigned LineNo, FileID FID, std::string &CaretLine, @@ -112,8 +112,10 @@ void TextDiagnosticPrinter::HighlightRange(const SourceRange &R, if (EndColNo) { --EndColNo; // Zero base the col #. - // Add in the length of the token, so that we cover multi-char tokens. - EndColNo += Lexer::MeasureTokenLength(End, SM, *LangOpts); + // Add in the length of the token, so that we cover multi-char tokens if + // this is a token range. + if (R.isTokenRange()) + EndColNo += Lexer::MeasureTokenLength(End, SM, *LangOpts); } else { EndColNo = CaretLine.size(); } @@ -121,21 +123,24 @@ void TextDiagnosticPrinter::HighlightRange(const SourceRange &R, assert(StartColNo <= EndColNo && "Invalid range!"); - // Pick the first non-whitespace column. - while (StartColNo < SourceLine.size() && - (SourceLine[StartColNo] == ' ' || SourceLine[StartColNo] == '\t')) - ++StartColNo; - - // Pick the last non-whitespace column. - if (EndColNo > SourceLine.size()) - EndColNo = SourceLine.size(); - while (EndColNo-1 && - (SourceLine[EndColNo-1] == ' ' || SourceLine[EndColNo-1] == '\t')) - --EndColNo; - - // If the start/end passed each other, then we are trying to highlight a range - // that just exists in whitespace, which must be some sort of other bug. - assert(StartColNo <= EndColNo && "Trying to highlight whitespace??"); + // Check that a token range does not highlight only whitespace. + if (R.isTokenRange()) { + // Pick the first non-whitespace column. + while (StartColNo < SourceLine.size() && + (SourceLine[StartColNo] == ' ' || SourceLine[StartColNo] == '\t')) + ++StartColNo; + + // Pick the last non-whitespace column. + if (EndColNo > SourceLine.size()) + EndColNo = SourceLine.size(); + while (EndColNo-1 && + (SourceLine[EndColNo-1] == ' ' || SourceLine[EndColNo-1] == '\t')) + --EndColNo; + + // If the start/end passed each other, then we are trying to highlight a range + // that just exists in whitespace, which must be some sort of other bug. + assert(StartColNo <= EndColNo && "Trying to highlight whitespace??"); + } // Fill the range with ~'s. for (unsigned i = StartColNo; i < EndColNo; ++i) @@ -281,7 +286,7 @@ static void SelectInterestingSourceRegion(std::string &SourceLine, } void TextDiagnosticPrinter::EmitCaretDiagnostic(SourceLocation Loc, - SourceRange *Ranges, + CharSourceRange *Ranges, unsigned NumRanges, const SourceManager &SM, const FixItHint *Hints, @@ -312,10 +317,12 @@ void TextDiagnosticPrinter::EmitCaretDiagnostic(SourceLocation Loc, // Map the ranges. for (unsigned i = 0; i != NumRanges; ++i) { - SourceLocation S = Ranges[i].getBegin(), E = Ranges[i].getEnd(); - if (S.isMacroID()) S = SM.getImmediateSpellingLoc(S); - if (E.isMacroID()) E = SM.getImmediateSpellingLoc(E); - Ranges[i] = SourceRange(S, E); + CharSourceRange &R = Ranges[i]; + SourceLocation S = R.getBegin(), E = R.getEnd(); + if (S.isMacroID()) + R.setBegin(SM.getImmediateSpellingLoc(S)); + if (E.isMacroID()) + R.setEnd(SM.getImmediateSpellingLoc(E)); } if (!Suppressed) { @@ -777,7 +784,9 @@ void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level, continue; // Add in the length of the token, so that we cover multi-char tokens. - unsigned TokSize = Lexer::MeasureTokenLength(E, SM, *LangOpts); + unsigned TokSize = 0; + if (Info.getRange(i).isTokenRange()) + TokSize = Lexer::MeasureTokenLength(E, SM, *LangOpts); OS << '{' << SM.getLineNumber(BInfo.first, BInfo.second) << ':' << SM.getColumnNumber(BInfo.first, BInfo.second) << '-' @@ -904,15 +913,15 @@ void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level, LastCaretDiagnosticWasNote = (Level == Diagnostic::Note); // Get the ranges into a local array we can hack on. - SourceRange Ranges[20]; + CharSourceRange Ranges[20]; unsigned NumRanges = Info.getNumRanges(); assert(NumRanges < 20 && "Out of space"); for (unsigned i = 0; i != NumRanges; ++i) Ranges[i] = Info.getRange(i); unsigned NumHints = Info.getNumFixItHints(); - for (unsigned idx = 0; idx < NumHints; ++idx) { - const FixItHint &Hint = Info.getFixItHint(idx); + for (unsigned i = 0; i != NumHints; ++i) { + const FixItHint &Hint = Info.getFixItHint(i); if (Hint.RemoveRange.isValid()) { assert(NumRanges < 20 && "Out of space"); Ranges[NumRanges++] = Hint.RemoveRange; diff --git a/contrib/llvm/tools/clang/lib/Frontend/Warnings.cpp b/contrib/llvm/tools/clang/lib/Frontend/Warnings.cpp index 84c4f5d..8cc5616 100644 --- a/contrib/llvm/tools/clang/lib/Frontend/Warnings.cpp +++ b/contrib/llvm/tools/clang/lib/Frontend/Warnings.cpp @@ -35,6 +35,8 @@ void clang::ProcessWarningOptions(Diagnostic &Diags, const DiagnosticOptions &Opts) { Diags.setSuppressSystemWarnings(true); // Default to -Wno-system-headers Diags.setIgnoreAllWarnings(Opts.IgnoreWarnings); + Diags.setShowOverloads( + static_cast<Diagnostic::OverloadsShown>(Opts.ShowOverloads)); // Handle -ferror-limit if (Opts.ErrorLimit) |