60fb830e9b57f3c896c32f00db1d7c5f2d89d55a
[oota-llvm.git] / lib / VMCore / 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/LLVMContext.h"
16 #include "llvm/Metadata.h"
17 #include "llvm/Constants.h"
18 #include "llvm/Instruction.h"
19 #include "llvm/Support/ManagedStatic.h"
20 #include "llvm/Support/SourceMgr.h"
21 #include "LLVMContextImpl.h"
22 using namespace llvm;
23
24 static ManagedStatic<LLVMContext> GlobalContext;
25
26 LLVMContext& llvm::getGlobalContext() {
27   return *GlobalContext;
28 }
29
30 LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) {
31   // Create the first metadata kind, which is always 'dbg'.
32   unsigned DbgID = getMDKindID("dbg");
33   assert(DbgID == MD_dbg && "dbg kind id drifted"); (void)DbgID;
34 }
35 LLVMContext::~LLVMContext() { delete pImpl; }
36
37 void LLVMContext::addModule(Module *M) {
38   pImpl->OwnedModules.insert(M);
39 }
40
41 void LLVMContext::removeModule(Module *M) {
42   pImpl->OwnedModules.erase(M);
43 }
44
45 //===----------------------------------------------------------------------===//
46 // Recoverable Backend Errors
47 //===----------------------------------------------------------------------===//
48
49 void LLVMContext::setInlineAsmDiagnosticHandler(void *DiagHandler, 
50                                                 void *DiagContext) {
51   pImpl->InlineAsmDiagHandler = DiagHandler;
52   pImpl->InlineAsmDiagContext = DiagContext;
53 }
54
55 /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
56 /// setInlineAsmDiagnosticHandler.
57 void *LLVMContext::getInlineAsmDiagnosticHandler() const {
58   return pImpl->InlineAsmDiagHandler;
59 }
60
61 /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
62 /// setInlineAsmDiagnosticHandler.
63 void *LLVMContext::getInlineAsmDiagnosticContext() const {
64   return pImpl->InlineAsmDiagContext;
65 }
66
67 void LLVMContext::emitError(StringRef ErrorStr) {
68   emitError(0U, ErrorStr);
69 }
70
71 void LLVMContext::emitError(const Instruction *I, StringRef ErrorStr) {
72   unsigned LocCookie = 0;
73   if (const MDNode *SrcLoc = I->getMetadata("srcloc")) {
74     if (SrcLoc->getNumOperands() != 0)
75       if (const ConstantInt *CI = dyn_cast<ConstantInt>(SrcLoc->getOperand(0)))
76         LocCookie = CI->getZExtValue();
77   }
78   return emitError(LocCookie, ErrorStr);
79 }
80
81 void LLVMContext::emitError(unsigned LocCookie, StringRef ErrorStr) {
82   // If there is no error handler installed, just print the error and exit.
83   if (pImpl->InlineAsmDiagHandler == 0) {
84     errs() << "error: " << ErrorStr << "\n";
85     exit(1);
86   }
87   
88   // If we do have an error handler, we can report the error and keep going.
89   SMDiagnostic Diag("", "error: " + ErrorStr.str());
90   
91   ((SourceMgr::DiagHandlerTy)(intptr_t)pImpl->InlineAsmDiagHandler)
92       (Diag, pImpl->InlineAsmDiagContext, LocCookie);
93   
94 }
95
96 //===----------------------------------------------------------------------===//
97 // Metadata Kind Uniquing
98 //===----------------------------------------------------------------------===//
99
100 #ifndef NDEBUG
101 /// isValidName - Return true if Name is a valid custom metadata handler name.
102 static bool isValidName(StringRef MDName) {
103   if (MDName.empty())
104     return false;
105   
106   if (!isalpha(MDName[0]))
107     return false;
108   
109   for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
110        ++I) {
111     if (!isalnum(*I) && *I != '_' && *I != '-' && *I != '.')
112       return false;
113   }
114   return true;
115 }
116 #endif
117
118 /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
119 unsigned LLVMContext::getMDKindID(StringRef Name) const {
120   assert(isValidName(Name) && "Invalid MDNode name");
121
122   // If this is new, assign it its ID.
123   return
124     pImpl->CustomMDKindNames.GetOrCreateValue(
125       Name, pImpl->CustomMDKindNames.size()).second;
126 }
127
128 /// getHandlerNames - Populate client supplied smallvector using custome
129 /// metadata name and ID.
130 void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
131   Names.resize(pImpl->CustomMDKindNames.size());
132   for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
133        E = pImpl->CustomMDKindNames.end(); I != E; ++I)
134     Names[I->second] = I->first();
135 }