Formatting fixups.
[oota-llvm.git] / lib / IR / LLVMContext.cpp
1 //===-- LLVMContext.cpp - Implement LLVMContext ---------------------------===//
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 //  This file implements LLVMContext, as a wrapper around the opaque
11 //  class LLVMContextImpl.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/IR/LLVMContext.h"
16 #include "LLVMContextImpl.h"
17 #include "llvm/IR/Constants.h"
18 #include "llvm/IR/DiagnosticInfo.h"
19 #include "llvm/IR/DiagnosticPrinter.h"
20 #include "llvm/IR/Instruction.h"
21 #include "llvm/IR/Metadata.h"
22 #include "llvm/Support/ManagedStatic.h"
23 #include "llvm/Support/SourceMgr.h"
24 #include <cctype>
25 using namespace llvm;
26
27 static ManagedStatic<LLVMContext> GlobalContext;
28
29 LLVMContext& llvm::getGlobalContext() {
30   return *GlobalContext;
31 }
32
33 LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) {
34   // Create the fixed metadata kinds. This is done in the same order as the
35   // MD_* enum values so that they correspond.
36
37   // Create the 'dbg' metadata kind.
38   unsigned DbgID = getMDKindID("dbg");
39   assert(DbgID == MD_dbg && "dbg kind id drifted"); (void)DbgID;
40
41   // Create the 'tbaa' metadata kind.
42   unsigned TBAAID = getMDKindID("tbaa");
43   assert(TBAAID == MD_tbaa && "tbaa kind id drifted"); (void)TBAAID;
44
45   // Create the 'prof' metadata kind.
46   unsigned ProfID = getMDKindID("prof");
47   assert(ProfID == MD_prof && "prof kind id drifted"); (void)ProfID;
48
49   // Create the 'fpmath' metadata kind.
50   unsigned FPAccuracyID = getMDKindID("fpmath");
51   assert(FPAccuracyID == MD_fpmath && "fpmath kind id drifted");
52   (void)FPAccuracyID;
53
54   // Create the 'range' metadata kind.
55   unsigned RangeID = getMDKindID("range");
56   assert(RangeID == MD_range && "range kind id drifted");
57   (void)RangeID;
58
59   // Create the 'tbaa.struct' metadata kind.
60   unsigned TBAAStructID = getMDKindID("tbaa.struct");
61   assert(TBAAStructID == MD_tbaa_struct && "tbaa.struct kind id drifted");
62   (void)TBAAStructID;
63
64   // Create the 'invariant.load' metadata kind.
65   unsigned InvariantLdId = getMDKindID("invariant.load");
66   assert(InvariantLdId == MD_invariant_load && "invariant.load kind id drifted");
67   (void)InvariantLdId;
68 }
69 LLVMContext::~LLVMContext() { delete pImpl; }
70
71 void LLVMContext::addModule(Module *M) {
72   pImpl->OwnedModules.insert(M);
73 }
74
75 void LLVMContext::removeModule(Module *M) {
76   pImpl->OwnedModules.erase(M);
77 }
78
79 //===----------------------------------------------------------------------===//
80 // Recoverable Backend Errors
81 //===----------------------------------------------------------------------===//
82
83 void LLVMContext::
84 setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
85                               void *DiagContext) {
86   pImpl->InlineAsmDiagHandler = DiagHandler;
87   pImpl->InlineAsmDiagContext = DiagContext;
88 }
89
90 /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
91 /// setInlineAsmDiagnosticHandler.
92 LLVMContext::InlineAsmDiagHandlerTy
93 LLVMContext::getInlineAsmDiagnosticHandler() const {
94   return pImpl->InlineAsmDiagHandler;
95 }
96
97 /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
98 /// setInlineAsmDiagnosticHandler.
99 void *LLVMContext::getInlineAsmDiagnosticContext() const {
100   return pImpl->InlineAsmDiagContext;
101 }
102
103 void LLVMContext::setDiagnosticHandler(DiagnosticHandlerTy DiagnosticHandler,
104                                        void *DiagnosticContext) {
105   pImpl->DiagnosticHandler = DiagnosticHandler;
106   pImpl->DiagnosticContext = DiagnosticContext;
107 }
108
109 LLVMContext::DiagnosticHandlerTy LLVMContext::getDiagnosticHandler() const {
110   return pImpl->DiagnosticHandler;
111 }
112
113 void *LLVMContext::getDiagnosticContext() const {
114   return pImpl->DiagnosticContext;
115 }
116
117 void LLVMContext::emitError(const Twine &ErrorStr) {
118   diagnose(DiagnosticInfoInlineAsm(ErrorStr));
119 }
120
121 void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) {
122   assert (I && "Invalid instruction");
123   diagnose(DiagnosticInfoInlineAsm(*I, ErrorStr));
124 }
125
126 void LLVMContext::diagnose(const DiagnosticInfo &DI) {
127   // If there is a report handler, use it.
128   if (pImpl->DiagnosticHandler != 0) {
129     pImpl->DiagnosticHandler(DI, pImpl->DiagnosticContext);
130     return;
131   }
132   // Otherwise, print the message with a prefix based on the severity.
133   std::string MsgStorage;
134   raw_string_ostream Stream(MsgStorage);
135   DiagnosticPrinterRawOStream DP(Stream);
136   DI.print(DP);
137   Stream.flush();
138   switch (DI.getSeverity()) {
139   case DS_Error:
140     errs() << "error: " << MsgStorage << "\n";
141     exit(1);
142   case DS_Warning:
143     errs() << "warning: " << MsgStorage << "\n";
144     break;
145   case DS_Note:
146     errs() << "note: " << MsgStorage << "\n";
147     break;
148   }
149 }
150
151 void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) {
152   diagnose(DiagnosticInfoInlineAsm(LocCookie, ErrorStr));
153 }
154
155 //===----------------------------------------------------------------------===//
156 // Metadata Kind Uniquing
157 //===----------------------------------------------------------------------===//
158
159 #ifndef NDEBUG
160 /// isValidName - Return true if Name is a valid custom metadata handler name.
161 static bool isValidName(StringRef MDName) {
162   if (MDName.empty())
163     return false;
164
165   if (!std::isalpha(static_cast<unsigned char>(MDName[0])))
166     return false;
167
168   for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
169        ++I) {
170     if (!std::isalnum(static_cast<unsigned char>(*I)) && *I != '_' &&
171         *I != '-' && *I != '.')
172       return false;
173   }
174   return true;
175 }
176 #endif
177
178 /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
179 unsigned LLVMContext::getMDKindID(StringRef Name) const {
180   assert(isValidName(Name) && "Invalid MDNode name");
181
182   // If this is new, assign it its ID.
183   return
184     pImpl->CustomMDKindNames.GetOrCreateValue(
185       Name, pImpl->CustomMDKindNames.size()).second;
186 }
187
188 /// getHandlerNames - Populate client supplied smallvector using custome
189 /// metadata name and ID.
190 void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
191   Names.resize(pImpl->CustomMDKindNames.size());
192   for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
193        E = pImpl->CustomMDKindNames.end(); I != E; ++I)
194     Names[I->second] = I->first();
195 }