Owen Anderson | 8e66e0b | 2009-06-30 00:48:55 +0000 | [diff] [blame] | 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 | //===----------------------------------------------------------------------===// |
Owen Anderson | 36f62e5 | 2009-06-30 17:06:46 +0000 | [diff] [blame] | 9 | // |
| 10 | // This file implements LLVMContext, as a wrapper around the opaque |
Benjamin Kramer | 78c3bcb | 2009-08-11 17:45:13 +0000 | [diff] [blame] | 11 | // class LLVMContextImpl. |
Owen Anderson | 36f62e5 | 2009-06-30 17:06:46 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
Owen Anderson | 8e66e0b | 2009-06-30 00:48:55 +0000 | [diff] [blame] | 14 | |
| 15 | #include "llvm/LLVMContext.h" |
Devang Patel | f718832 | 2009-09-03 01:39:20 +0000 | [diff] [blame] | 16 | #include "llvm/Metadata.h" |
Owen Anderson | 8e66e0b | 2009-06-30 00:48:55 +0000 | [diff] [blame] | 17 | #include "llvm/Constants.h" |
Owen Anderson | 53a5221 | 2009-07-13 04:09:18 +0000 | [diff] [blame] | 18 | #include "llvm/Instruction.h" |
Owen Anderson | 1938fb1 | 2009-06-30 23:39:59 +0000 | [diff] [blame] | 19 | #include "llvm/Support/ManagedStatic.h" |
Owen Anderson | 8e66e0b | 2009-06-30 00:48:55 +0000 | [diff] [blame] | 20 | #include "LLVMContextImpl.h" |
Owen Anderson | 8e66e0b | 2009-06-30 00:48:55 +0000 | [diff] [blame] | 21 | using namespace llvm; |
| 22 | |
Owen Anderson | 1938fb1 | 2009-06-30 23:39:59 +0000 | [diff] [blame] | 23 | static ManagedStatic<LLVMContext> GlobalContext; |
| 24 | |
Owen Anderson | 2a15443 | 2009-07-01 23:13:44 +0000 | [diff] [blame] | 25 | LLVMContext& llvm::getGlobalContext() { |
Owen Anderson | 1cf085d | 2009-07-01 21:22:36 +0000 | [diff] [blame] | 26 | return *GlobalContext; |
Owen Anderson | 1938fb1 | 2009-06-30 23:39:59 +0000 | [diff] [blame] | 27 | } |
| 28 | |
Chris Lattner | c263b42 | 2010-03-30 23:03:27 +0000 | [diff] [blame^] | 29 | LLVMContext::LLVMContext() : pImpl(new LLVMContextImpl(*this)) { |
| 30 | // Create the first metadata kind, which is always 'dbg'. |
| 31 | unsigned DbgID = getMDKindID("dbg"); |
| 32 | assert(DbgID == MD_dbg && "dbg kind id drifted"); (void)DbgID; |
| 33 | } |
Owen Anderson | 8e66e0b | 2009-06-30 00:48:55 +0000 | [diff] [blame] | 34 | LLVMContext::~LLVMContext() { delete pImpl; } |
Owen Anderson | afd0c4c | 2009-08-04 22:41:48 +0000 | [diff] [blame] | 35 | |
Chris Lattner | a3b94ba | 2010-03-30 20:48:48 +0000 | [diff] [blame] | 36 | |
| 37 | #ifndef NDEBUG |
| 38 | /// isValidName - Return true if Name is a valid custom metadata handler name. |
| 39 | static bool isValidName(StringRef MDName) { |
| 40 | if (MDName.empty()) |
| 41 | return false; |
| 42 | |
| 43 | if (!isalpha(MDName[0])) |
| 44 | return false; |
| 45 | |
| 46 | for (StringRef::iterator I = MDName.begin() + 1, E = MDName.end(); I != E; |
| 47 | ++I) { |
| 48 | if (!isalnum(*I) && *I != '_' && *I != '-' && *I != '.') |
| 49 | return false; |
| 50 | } |
| 51 | return true; |
Owen Anderson | afd0c4c | 2009-08-04 22:41:48 +0000 | [diff] [blame] | 52 | } |
Chris Lattner | a3b94ba | 2010-03-30 20:48:48 +0000 | [diff] [blame] | 53 | #endif |
| 54 | |
| 55 | /// getMDKindID - Return a unique non-zero ID for the specified metadata kind. |
| 56 | unsigned LLVMContext::getMDKindID(StringRef Name) const { |
| 57 | assert(isValidName(Name) && "Invalid MDNode name"); |
| 58 | |
| 59 | unsigned &Entry = pImpl->CustomMDKindNames[Name]; |
| 60 | |
| 61 | // If this is new, assign it its ID. |
| 62 | if (Entry == 0) Entry = pImpl->CustomMDKindNames.size(); |
| 63 | return Entry; |
| 64 | } |
| 65 | |
| 66 | /// getHandlerNames - Populate client supplied smallvector using custome |
| 67 | /// metadata name and ID. |
| 68 | void LLVMContext::getMDKindNames(SmallVectorImpl<StringRef> &Names) const { |
| 69 | Names.resize(pImpl->CustomMDKindNames.size()+1); |
| 70 | Names[0] = ""; |
| 71 | for (StringMap<unsigned>::const_iterator I = pImpl->CustomMDKindNames.begin(), |
| 72 | E = pImpl->CustomMDKindNames.end(); I != E; ++I) |
| 73 | // MD Handlers are numbered from 1. |
| 74 | Names[I->second] = I->first(); |
| 75 | } |
| 76 | |
Devang Patel | c5aa8c6 | 2009-08-11 06:31:57 +0000 | [diff] [blame] | 77 | |