From b37f29b6f316ece56e3879c1e2560eb2c46ec372 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Fri, 31 Jul 2009 16:43:49 +0000 Subject: [PATCH] create sections with MCSection::Create instead of Context->getOrCreateSection. This is needed to allow polymorphic sections. llvm-svn: 77680 --- llvm/include/llvm/MC/MCContext.h | 6 ++++-- llvm/include/llvm/MC/MCSection.h | 6 +++--- llvm/lib/MC/MCContext.cpp | 25 ++++++++++++++++--------- 3 files changed, 23 insertions(+), 14 deletions(-) diff --git a/llvm/include/llvm/MC/MCContext.h b/llvm/include/llvm/MC/MCContext.h index 24dbc40479bc..a8187cce6f5f 100644 --- a/llvm/include/llvm/MC/MCContext.h +++ b/llvm/include/llvm/MC/MCContext.h @@ -42,12 +42,14 @@ namespace llvm { /// objects. BumpPtrAllocator Allocator; + friend class MCSection; public: MCContext(); ~MCContext(); - /// GetSection - Get or create a new section with the given @param Name. - MCSection *GetSection(const StringRef &Name); + /// GetSection - Look up a section with the given @param Name, returning + /// null if it doesn't exist. + MCSection *GetSection(const StringRef &Name) const; /// CreateSymbol - Create a new symbol with the specified @param Name. /// diff --git a/llvm/include/llvm/MC/MCSection.h b/llvm/include/llvm/MC/MCSection.h index 48ffa2facd6b..024bc6c33a88 100644 --- a/llvm/include/llvm/MC/MCSection.h +++ b/llvm/include/llvm/MC/MCSection.h @@ -25,13 +25,13 @@ namespace llvm { class MCSection { std::string Name; private: - friend class MCContext; - MCSection(const StringRef &_Name) : Name(_Name) {} - MCSection(const MCSection&); // DO NOT IMPLEMENT void operator=(const MCSection&); // DO NOT IMPLEMENT + MCSection(const StringRef &_Name, MCContext &Ctx); public: + static MCSection *Create(const StringRef &_Name, MCContext &Ctx); + const std::string &getName() const { return Name; } }; diff --git a/llvm/lib/MC/MCContext.cpp b/llvm/lib/MC/MCContext.cpp index 6c74dcd9ecfd..5f3f12516960 100644 --- a/llvm/lib/MC/MCContext.cpp +++ b/llvm/lib/MC/MCContext.cpp @@ -14,22 +14,29 @@ #include "llvm/MC/MCValue.h" using namespace llvm; -MCContext::MCContext() -{ +MCContext::MCContext() { } MCContext::~MCContext() { } -MCSection *MCContext::GetSection(const StringRef &Name) { - MCSection *&Entry = Sections[Name]; - - if (!Entry) - Entry = new (*this) MCSection(Name); - - return Entry; +MCSection *MCContext::GetSection(const StringRef &Name) const { + StringMap::const_iterator I = Sections.find(Name); + return I != Sections.end() ? I->second : 0; } + +MCSection::MCSection(const StringRef &_Name, MCContext &Ctx) : Name(_Name) { + MCSection *&Entry = Ctx.Sections[Name]; + assert(Entry == 0 && "Multiple sections with the same name created"); + Entry = this; +} + +MCSection *MCSection::Create(const StringRef &Name, MCContext &Ctx) { + return new (Ctx) MCSection(Name, Ctx); +} + + MCSymbol *MCContext::CreateSymbol(const StringRef &Name) { assert(Name[0] != '\0' && "Normal symbols cannot be unnamed!");