Move all of the header files which are involved in modelling the LLVM IR
[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/Instruction.h"
19 #include "llvm/IR/Metadata.h"
20 #include "llvm/Support/ManagedStatic.h"
21 #include "llvm/Support/SourceMgr.h"
22 #include <cctype>
23 using namespace llvm;
24
25 static ManagedStatic<LLVMContext> GlobalContext;
26
27 LLVMContext& llvm::getGlobalContext() {
28   return *GlobalContext;
29 }
30
31 LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) {
32   // Create the fixed metadata kinds. This is done in the same order as the
33   // MD_* enum values so that they correspond.
34
35   // Create the 'dbg' metadata kind.
36   unsigned DbgID = getMDKindID("dbg");
37   assert(DbgID == MD_dbg && "dbg kind id drifted"); (void)DbgID;
38
39   // Create the 'tbaa' metadata kind.
40   unsigned TBAAID = getMDKindID("tbaa");
41   assert(TBAAID == MD_tbaa && "tbaa kind id drifted"); (void)TBAAID;
42
43   // Create the 'prof' metadata kind.
44   unsigned ProfID = getMDKindID("prof");
45   assert(ProfID == MD_prof && "prof kind id drifted"); (void)ProfID;
46
47   // Create the 'fpmath' metadata kind.
48   unsigned FPAccuracyID = getMDKindID("fpmath");
49   assert(FPAccuracyID == MD_fpmath && "fpmath kind id drifted");
50   (void)FPAccuracyID;
51
52   // Create the 'range' metadata kind.
53   unsigned RangeID = getMDKindID("range");
54   assert(RangeID == MD_range && "range kind id drifted");
55   (void)RangeID;
56
57   // Create the 'tbaa.struct' metadata kind.
58   unsigned TBAAStructID = getMDKindID("tbaa.struct");
59   assert(TBAAStructID == MD_tbaa_struct && "tbaa.struct kind id drifted");
60   (void)TBAAStructID;
61 }
62 LLVMContext::~LLVMContext() { delete pImpl; }
63
64 void LLVMContext::addModule(Module *M) {
65   pImpl->OwnedModules.insert(M);
66 }
67
68 void LLVMContext::removeModule(Module *M) {
69   pImpl->OwnedModules.erase(M);
70 }
71
72 //===----------------------------------------------------------------------===//
73 // Recoverable Backend Errors
74 //===----------------------------------------------------------------------===//
75
76 void LLVMContext::setDiagnosticHandler(DiagHandlerTy DiagHandler,
77                                        void *DiagContext) {
78   pImpl->DiagHandler = DiagHandler;
79   pImpl->DiagContext = DiagContext;
80 }
81
82 /// getDiagnosticHandler - Return the diagnostic handler set by
83 /// setDiagnosticHandler.
84 LLVMContext::DiagHandlerTy LLVMContext::getDiagnosticHandler() const {
85   return pImpl->DiagHandler;
86 }
87
88 /// getDiagnosticContext - Return the diagnostic context set by
89 /// setDiagnosticHandler.
90 void *LLVMContext::getDiagnosticContext() const {
91   return pImpl->DiagContext;
92 }
93
94 void LLVMContext::emitError(const Twine &ErrorStr) {
95   emitError(0U, ErrorStr);
96 }
97
98 void LLVMContext::emitWarning(const Twine &ErrorStr) {
99   emitWarning(0U, ErrorStr);
100 }
101
102 static unsigned getSrcLocation(const Instruction *I) {
103   unsigned LocCookie = 0;
104   if (const MDNode *SrcLoc = I->getMetadata("srcloc")) {
105     if (SrcLoc->getNumOperands() != 0)
106       if (const ConstantInt *CI = dyn_cast<ConstantInt>(SrcLoc->getOperand(0)))
107         LocCookie = CI->getZExtValue();
108   }
109   return LocCookie;
110 }
111
112 void LLVMContext::emitError(const Instruction *I, const Twine &ErrorStr) {
113   unsigned LocCookie = getSrcLocation(I);
114   return emitError(LocCookie, ErrorStr);
115 }
116
117 void LLVMContext::emitWarning(const Instruction *I, const Twine &ErrorStr) {
118   unsigned LocCookie = getSrcLocation(I);
119   return emitWarning(LocCookie, ErrorStr);
120 }
121
122 void LLVMContext::emitError(unsigned LocCookie, const Twine &ErrorStr) {
123   // If there is no error handler installed, just print the error and exit.
124   if (pImpl->DiagHandler == 0) {
125     errs() << "error: " << ErrorStr << "\n";
126     exit(1);
127   }
128
129   // If we do have an error handler, we can report the error and keep going.
130   SMDiagnostic Diag("", SourceMgr::DK_Error, ErrorStr.str());
131
132   pImpl->DiagHandler(Diag, pImpl->DiagContext, LocCookie);
133 }
134
135 void LLVMContext::emitWarning(unsigned LocCookie, const Twine &ErrorStr) {
136   // If there is no handler installed, just print the warning.
137   if (pImpl->DiagHandler == 0) {
138     errs() << "warning: " << ErrorStr << "\n";
139     return;
140   }
141
142   // If we do have a handler, we can report the warning.
143   SMDiagnostic Diag("", SourceMgr::DK_Warning, ErrorStr.str());
144
145   pImpl->DiagHandler(Diag, pImpl->DiagContext, LocCookie);
146 }
147
148 //===----------------------------------------------------------------------===//
149 // Metadata Kind Uniquing
150 //===----------------------------------------------------------------------===//
151
152 #ifndef NDEBUG
153 /// isValidName - Return true if Name is a valid custom metadata handler name.
154 static bool isValidName(StringRef MDName) {
155   if (MDName.empty())
156     return false;
157
158   if (!std::isalpha(MDName[0]))
159     return false;
160
161   for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
162        ++I) {
163     if (!std::isalnum(*I) && *I != '_' && *I != '-' && *I != '.')
164       return false;
165   }
166   return true;
167 }
168 #endif
169
170 /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
171 unsigned LLVMContext::getMDKindID(StringRef Name) const {
172   assert(isValidName(Name) && "Invalid MDNode name");
173
174   // If this is new, assign it its ID.
175   return
176     pImpl->CustomMDKindNames.GetOrCreateValue(
177       Name, pImpl->CustomMDKindNames.size()).second;
178 }
179
180 /// getHandlerNames - Populate client supplied smallvector using custome
181 /// metadata name and ID.
182 void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
183   Names.resize(pImpl->CustomMDKindNames.size());
184   for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
185        E = pImpl->CustomMDKindNames.end(); I != E; ++I)
186     Names[I->second] = I->first();
187 }