1 //===-- LLVMContext.cpp - Implement LLVMContext -----------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements LLVMContext, as a wrapper around the opaque
11 // class LLVMContextImpl.
13 //===----------------------------------------------------------------------===//
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"
24 static ManagedStatic<LLVMContext> GlobalContext;
26 LLVMContext& llvm::getGlobalContext() {
27 return *GlobalContext;
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;
35 LLVMContext::~LLVMContext() { delete pImpl; }
37 //===----------------------------------------------------------------------===//
38 // Recoverable Backend Errors
39 //===----------------------------------------------------------------------===//
41 void LLVMContext::setInlineAsmDiagnosticHandler(void *DiagHandler,
43 pImpl->InlineAsmDiagHandler = DiagHandler;
44 pImpl->InlineAsmDiagContext = DiagContext;
47 /// getInlineAsmDiagnosticHandler - Return the diagnostic handler set by
48 /// setInlineAsmDiagnosticHandler.
49 void *LLVMContext::getInlineAsmDiagnosticHandler() const {
50 return pImpl->InlineAsmDiagHandler;
53 /// getInlineAsmDiagnosticContext - Return the diagnostic context set by
54 /// setInlineAsmDiagnosticHandler.
55 void *LLVMContext::getInlineAsmDiagnosticContext() const {
56 return pImpl->InlineAsmDiagContext;
59 void LLVMContext::emitError(StringRef ErrorStr) {
60 emitError(0U, ErrorStr);
63 void LLVMContext::emitError(const Instruction *I, StringRef ErrorStr) {
64 unsigned LocCookie = 0;
65 if (const MDNode *SrcLoc = I->getMetadata("srcloc")) {
66 if (SrcLoc->getNumOperands() != 0)
67 if (const ConstantInt *CI = dyn_cast<ConstantInt>(SrcLoc->getOperand(0)))
68 LocCookie = CI->getZExtValue();
70 return emitError(LocCookie, ErrorStr);
73 void LLVMContext::emitError(unsigned LocCookie, StringRef ErrorStr) {
74 // If there is no error handler installed, just print the error and exit.
75 if (pImpl->InlineAsmDiagHandler == 0) {
76 errs() << "error: " << ErrorStr << "\n";
80 // If we do have an error handler, we can report the error and keep going.
81 SMDiagnostic Diag("", "error: " + ErrorStr.str());
83 ((SourceMgr::DiagHandlerTy)(intptr_t)pImpl->InlineAsmDiagHandler)
84 (Diag, pImpl->InlineAsmDiagContext, LocCookie);
88 //===----------------------------------------------------------------------===//
89 // Metadata Kind Uniquing
90 //===----------------------------------------------------------------------===//
93 /// isValidName - Return true if Name is a valid custom metadata handler name.
94 static bool isValidName(StringRef MDName) {
98 if (!isalpha(MDName[0]))
101 for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E;
103 if (!isalnum(*I) && *I != '_' && *I != '-' && *I != '.')
110 /// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
111 unsigned LLVMContext::getMDKindID(StringRef Name) const {
112 assert(isValidName(Name) && "Invalid MDNode name");
114 unsigned &Entry = pImpl->CustomMDKindNames[Name];
116 // If this is new, assign it its ID.
117 if (Entry == 0) Entry = pImpl->CustomMDKindNames.size();
121 /// getHandlerNames - Populate client supplied smallvector using custome
122 /// metadata name and ID.
123 void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const {
124 Names.resize(pImpl->CustomMDKindNames.size()+1);
126 for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(),
127 E = pImpl->CustomMDKindNames.end(); I != E; ++I)
128 // MD Handlers are numbered from 1.
129 Names[I->second] = I->first();