blob: 2a870ec6cfd94d2b9f41c2636fd8664e752e6f81 [file] [log] [blame]
Owen Anderson8e66e0b2009-06-30 00:48:55 +00001//===-- 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 Anderson36f62e52009-06-30 17:06:46 +00009//
10// This file implements LLVMContext, as a wrapper around the opaque
Benjamin Kramer78c3bcb2009-08-11 17:45:13 +000011// class LLVMContextImpl.
Owen Anderson36f62e52009-06-30 17:06:46 +000012//
13//===----------------------------------------------------------------------===//
Owen Anderson8e66e0b2009-06-30 00:48:55 +000014
15#include "llvm/LLVMContext.h"
Devang Patelf7188322009-09-03 01:39:20 +000016#include "llvm/Metadata.h"
Owen Anderson8e66e0b2009-06-30 00:48:55 +000017#include "llvm/Constants.h"
Owen Anderson53a52212009-07-13 04:09:18 +000018#include "llvm/Instruction.h"
Owen Anderson1938fb12009-06-30 23:39:59 +000019#include "llvm/Support/ManagedStatic.h"
Owen Anderson8e66e0b2009-06-30 00:48:55 +000020#include "LLVMContextImpl.h"
Owen Anderson8e66e0b2009-06-30 00:48:55 +000021using namespace llvm;
22
Owen Anderson1938fb12009-06-30 23:39:59 +000023static ManagedStatic<LLVMContext> GlobalContext;
24
Owen Anderson2a154432009-07-01 23:13:44 +000025LLVMContext& llvm::getGlobalContext() {
Owen Anderson1cf085d2009-07-01 21:22:36 +000026 return *GlobalContext;
Owen Anderson1938fb12009-06-30 23:39:59 +000027}
28
Chris Lattnerc263b422010-03-30 23:03:27 +000029LLVMContext::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 Anderson8e66e0b2009-06-30 00:48:55 +000034LLVMContext::~LLVMContext() { delete pImpl; }
Owen Andersonafd0c4c2009-08-04 22:41:48 +000035
Chris Lattnera3b94ba2010-03-30 20:48:48 +000036
37#ifndef NDEBUG
38/// isValidName - Return true if Name is a valid custom metadata handler name.
39static 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 Andersonafd0c4c2009-08-04 22:41:48 +000052}
Chris Lattnera3b94ba2010-03-30 20:48:48 +000053#endif
54
55/// getMDKindID - Return a unique non-zero ID for the specified metadata kind.
56unsigned 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.
68void 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 Patelc5aa8c62009-08-11 06:31:57 +000077