Fix lines that exceed 80 columns. There is no change in functionality.
[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 #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 LLVMContext::~LLVMContext() { delete pImpl; }
44
45 void LLVMContext::addModule(Module *M) {
46   pImpl->OwnedModules.insert(M);
47 }
48
49 void LLVMContext::removeModule(Module *M) {
50   pImpl->OwnedModules.erase(M);
51 }
52
53 //===----------------------------------------------------------------------===//
54 // Recoverable Backend Errors
55 //===----------------------------------------------------------------------===//
56
57 void LLVMContext::
58 setInlineAsmDiagnosticHandler(InlineAsmDiagHandlerTy DiagHandler,
59                               void *DiagContext) {
60   pImpl->InlineAsmDiagHandler = DiagHandler;
61   pImpl->InlineAsmDiagContext = DiagContext;
62 }
63
64 /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
65 /// setInlineAsmDiagnosticHandler.
66 LLVMContext::InlineAsmDiagHandlerTy
67 LLVMContext::getInlineAsmDiagnosticHandler() const {
68   return pImpl->InlineAsmDiagHandler;
69 }
70
71 /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
72 /// setInlineAsmDiagnosticHandler.
73 void *LLVMContext::getInlineAsmDiagnosticContext() const {
74   return pImpl->InlineAsmDiagContext;
75 }
76
77 void LLVMContext::emitError(StringRef ErrorStr) {
78   emitError(0U, ErrorStr);
79 }
80
81 void LLVMContext::emitError(const Instruction *I, StringRef ErrorStr) {
82   unsigned LocCookie = 0;
83   if (const MDNode *SrcLoc = I->getMetadata("srcloc")) {
84     if (SrcLoc->getNumOperands() != 0)
85       if (const ConstantInt *CI = dyn_cast<ConstantInt>(SrcLoc->getOperand(0)))
86         LocCookie = CI->getZExtValue();
87   }
88   return emitError(LocCookie, ErrorStr);
89 }
90
91 void LLVMContext::emitError(unsigned LocCookie, StringRef ErrorStr) {
92   // If there is no error handler installed, just print the error and exit.
93   if (pImpl->InlineAsmDiagHandler == 0) {
94     errs() << "error: " << ErrorStr << "\n";
95     exit(1);
96   }
97
98   // If we do have an error handler, we can report the error and keep going.
99   SMDiagnostic Diag("", "error: " + ErrorStr.str());
100
101   pImpl->InlineAsmDiagHandler(Diag, pImpl->InlineAsmDiagContext, LocCookie);
102 }
103
104 //===----------------------------------------------------------------------===//
105 // Metadata Kind Uniquing
106 //===----------------------------------------------------------------------===//
107
108 #ifndef NDEBUG
109 /// isValidName - Return true if Name is a valid custom metadata handler name.
110 static bool isValidName(StringRef MDName) {
111   if (MDName.empty())
112     return false;
113
114   if (!std::isalpha(MDName[0]))
115     return false;
116
117   for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
118        ++I) {
119     if (!std::isalnum(*I) && *I != '_' && *I != '-' && *I != '.')
120       return false;
121   }
122   return true;
123 }
124 #endif
125
126 /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
127 unsigned LLVMContext::getMDKindID(StringRef Name) const {
128   assert(isValidName(Name) && "Invalid MDNode name");
129
130   // If this is new, assign it its ID.
131   return
132     pImpl->CustomMDKindNames.GetOrCreateValue(
133       Name, pImpl->CustomMDKindNames.size()).second;
134 }
135
136 /// getHandlerNames - Populate client supplied smallvector using custome
137 /// metadata name and ID.
138 void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
139   Names.resize(pImpl->CustomMDKindNames.size());
140   for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
141        E = pImpl->CustomMDKindNames.end(); I != E; ++I)
142     Names[I->second] = I->first();
143 }