fix a memory leak yjasskin pointed out: MCSymbol is bump pointer
[oota-llvm.git] / lib / MC / MCContext.cpp
1 //===- lib/MC/MCContext.cpp - Machine Code Context ------------------------===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/MC/MCContext.h"
11 #include "llvm/MC/MCAsmInfo.h"
12 #include "llvm/MC/MCSection.h"
13 #include "llvm/MC/MCSymbol.h"
14 #include "llvm/ADT/SmallString.h"
15 #include "llvm/ADT/Twine.h"
16 using namespace llvm;
17
18 MCContext::MCContext(const MCAsmInfo &mai) : MAI(mai), NextUniqueID(0) {
19 }
20
21 MCContext::~MCContext() {
22   // NOTE: The sections are all allocated out of a bump pointer allocator,
23   // we don't need to free them here.
24 }
25
26 MCSymbol *MCContext::GetOrCreateSymbol(StringRef Name, bool isTemporary) {
27   assert(!Name.empty() && "Normal symbols cannot be unnamed!");
28   
29   // Do the lookup and get the entire StringMapEntry.  We want access to the
30   // key if we are creating the entry.
31   StringMapEntry<MCSymbol*> &Entry = Symbols.GetOrCreateValue(Name);
32   if (Entry.getValue()) return Entry.getValue();
33
34   // Ok, the entry doesn't already exist.  Have the MCSymbol object itself refer
35   // to the copy of the string that is embedded in the StringMapEntry.
36   MCSymbol *Result = new (*this) MCSymbol(Entry.getKey(), isTemporary);
37   Entry.setValue(Result);
38   return Result; 
39 }
40
41 MCSymbol *MCContext::GetOrCreateSymbol(const Twine &Name, bool isTemporary) {
42   SmallString<128> NameSV;
43   Name.toVector(NameSV);
44   return GetOrCreateSymbol(NameSV.str(), isTemporary);
45 }
46
47 MCSymbol *MCContext::CreateTempSymbol() {
48   return GetOrCreateTemporarySymbol(Twine(MAI.getPrivateGlobalPrefix()) +
49                                     "tmp" + Twine(NextUniqueID++));
50 }
51
52
53 MCSymbol *MCContext::GetOrCreateTemporarySymbol(StringRef Name) {
54   // If there is no name, create a new anonymous symbol.
55   // FIXME: Remove this.  This form of the method should always take a name.
56   if (Name.empty())
57     return GetOrCreateTemporarySymbol(Twine(MAI.getPrivateGlobalPrefix()) +
58                                       "tmp" + Twine(NextUniqueID++));
59   
60   return GetOrCreateSymbol(Name, true);
61 }
62
63 MCSymbol *MCContext::GetOrCreateTemporarySymbol(const Twine &Name) {
64   SmallString<128> NameSV;
65   Name.toVector(NameSV);
66   return GetOrCreateTemporarySymbol(NameSV.str());
67 }
68
69
70 MCSymbol *MCContext::LookupSymbol(StringRef Name) const {
71   return Symbols.lookup(Name);
72 }