1 //===--------------- LLVMContextImpl.cpp - Implementation ------*- C++ -*--===//
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 LLVMContextImpl, the opaque implementation
13 //===----------------------------------------------------------------------===//
15 #include "LLVMContextImpl.h"
16 #include "llvm/Constants.h"
17 #include "llvm/DerivedTypes.h"
18 #include "llvm/LLVMContext.h"
19 #include "llvm/Metadata.h"
22 static char getValType(ConstantAggregateZero *CPZ) { return 0; }
24 LLVMContextImpl::LLVMContextImpl(LLVMContext &C) :
25 Context(C), TheTrueVal(0), TheFalseVal(0) { }
27 MDString *LLVMContextImpl::getMDString(const char *StrBegin,
29 sys::SmartScopedWriter<true> Writer(ConstantsLock);
30 StringMapEntry<MDString *> &Entry =
31 MDStringCache.GetOrCreateValue(StringRef(StrBegin, StrLength));
32 MDString *&S = Entry.getValue();
33 if (!S) S = new MDString(Entry.getKeyData(),
34 Entry.getKeyLength());
39 MDNode *LLVMContextImpl::getMDNode(Value*const* Vals, unsigned NumVals) {
41 for (unsigned i = 0; i != NumVals; ++i)
42 ID.AddPointer(Vals[i]);
44 ConstantsLock.reader_acquire();
46 MDNode *N = MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
47 ConstantsLock.reader_release();
50 sys::SmartScopedWriter<true> Writer(ConstantsLock);
51 N = MDNodeSet.FindNodeOrInsertPos(ID, InsertPoint);
53 // InsertPoint will have been set by the FindNodeOrInsertPos call.
54 N = new MDNode(Vals, NumVals);
55 MDNodeSet.InsertNode(N, InsertPoint);
62 ConstantAggregateZero*
63 LLVMContextImpl::getConstantAggregateZero(const Type *Ty) {
64 assert((isa<StructType>(Ty) || isa<ArrayType>(Ty) || isa<VectorType>(Ty)) &&
65 "Cannot create an aggregate zero of non-aggregate type!");
68 return AggZeroConstants.getOrCreate(Ty, 0);
71 // *** erase methods ***
73 void LLVMContextImpl::erase(MDString *M) {
74 sys::SmartScopedWriter<true> Writer(ConstantsLock);
75 MDStringCache.erase(MDStringCache.find(M->getString()));
78 void LLVMContextImpl::erase(MDNode *M) {
79 sys::SmartScopedWriter<true> Writer(ConstantsLock);
80 MDNodeSet.RemoveNode(M);
83 void LLVMContextImpl::erase(ConstantAggregateZero *Z) {
84 AggZeroConstants.remove(Z);