1 //===-- Bitcode/Reader/ValueList.h - Number values --------------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This class gives values and types Unique ID's. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #ifndef LLVM_LIB_BITCODE_READER_VALUELIST_H 14 #define LLVM_LIB_BITCODE_READER_VALUELIST_H 15 16 #include "llvm/IR/ValueHandle.h" 17 #include <cassert> 18 #include <utility> 19 #include <vector> 20 21 namespace llvm { 22 23 class Constant; 24 class Error; 25 class LLVMContext; 26 class Type; 27 class Value; 28 29 class BitcodeReaderValueList { 30 /// Maps Value ID to pair of Value* and Type ID. 31 std::vector<std::pair<WeakTrackingVH, unsigned>> ValuePtrs; 32 33 /// As we resolve forward-referenced constants, we add information about them 34 /// to this vector. This allows us to resolve them in bulk instead of 35 /// resolving each reference at a time. See the code in 36 /// ResolveConstantForwardRefs for more information about this. 37 /// 38 /// The key of this vector is the placeholder constant, the value is the slot 39 /// number that holds the resolved value. 40 using ResolveConstantsTy = std::vector<std::pair<Constant *, unsigned>>; 41 ResolveConstantsTy ResolveConstants; 42 LLVMContext &Context; 43 44 /// Maximum number of valid references. Forward references exceeding the 45 /// maximum must be invalid. 46 unsigned RefsUpperBound; 47 48 public: 49 BitcodeReaderValueList(LLVMContext &C, size_t RefsUpperBound) 50 : Context(C), 51 RefsUpperBound(std::min((size_t)std::numeric_limits<unsigned>::max(), 52 RefsUpperBound)) {} 53 54 ~BitcodeReaderValueList() { 55 assert(ResolveConstants.empty() && "Constants not resolved?"); 56 } 57 58 // vector compatibility methods 59 unsigned size() const { return ValuePtrs.size(); } 60 void resize(unsigned N) { 61 ValuePtrs.resize(N); 62 } 63 void push_back(Value *V, unsigned TypeID) { 64 ValuePtrs.emplace_back(V, TypeID); 65 } 66 67 void clear() { 68 assert(ResolveConstants.empty() && "Constants not resolved?"); 69 ValuePtrs.clear(); 70 } 71 72 Value *operator[](unsigned i) const { 73 assert(i < ValuePtrs.size()); 74 return ValuePtrs[i].first; 75 } 76 77 unsigned getTypeID(unsigned ValNo) const { 78 assert(ValNo < ValuePtrs.size()); 79 return ValuePtrs[ValNo].second; 80 } 81 82 Value *back() const { return ValuePtrs.back().first; } 83 void pop_back() { 84 ValuePtrs.pop_back(); 85 } 86 bool empty() const { return ValuePtrs.empty(); } 87 88 void shrinkTo(unsigned N) { 89 assert(N <= size() && "Invalid shrinkTo request!"); 90 ValuePtrs.resize(N); 91 } 92 93 Constant *getConstantFwdRef(unsigned Idx, Type *Ty, unsigned TyID); 94 Value *getValueFwdRef(unsigned Idx, Type *Ty, unsigned TyID); 95 96 Error assignValue(unsigned Idx, Value *V, unsigned TypeID); 97 98 /// Once all constants are read, this method bulk resolves any forward 99 /// references. 100 void resolveConstantForwardRefs(); 101 }; 102 103 } // end namespace llvm 104 105 #endif // LLVM_LIB_BITCODE_READER_VALUELIST_H 106