Path: blob/main/contrib/llvm-project/llvm/lib/Bitcode/Reader/ValueList.cpp
35291 views
//===- ValueList.cpp - Internal BitcodeReader implementation --------------===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#include "ValueList.h"9#include "llvm/IR/Argument.h"10#include "llvm/IR/Constant.h"11#include "llvm/IR/Constants.h"12#include "llvm/IR/GlobalValue.h"13#include "llvm/IR/Instruction.h"14#include "llvm/IR/Type.h"15#include "llvm/IR/User.h"16#include "llvm/IR/Value.h"17#include "llvm/Support/Casting.h"18#include "llvm/Support/Error.h"19#include "llvm/Support/ErrorHandling.h"20#include <cstddef>2122using namespace llvm;2324Error BitcodeReaderValueList::assignValue(unsigned Idx, Value *V,25unsigned TypeID) {26if (Idx == size()) {27push_back(V, TypeID);28return Error::success();29}3031if (Idx >= size())32resize(Idx + 1);3334auto &Old = ValuePtrs[Idx];35if (!Old.first) {36Old.first = V;37Old.second = TypeID;38return Error::success();39}4041assert(!isa<Constant>(&*Old.first) && "Shouldn't update constant");42// If there was a forward reference to this value, replace it.43Value *PrevVal = Old.first;44if (PrevVal->getType() != V->getType())45return createStringError(46std::errc::illegal_byte_sequence,47"Assigned value does not match type of forward declaration");48Old.first->replaceAllUsesWith(V);49PrevVal->deleteValue();50return Error::success();51}5253Value *BitcodeReaderValueList::getValueFwdRef(unsigned Idx, Type *Ty,54unsigned TyID,55BasicBlock *ConstExprInsertBB) {56// Bail out for a clearly invalid value.57if (Idx >= RefsUpperBound)58return nullptr;5960if (Idx >= size())61resize(Idx + 1);6263if (Value *V = ValuePtrs[Idx].first) {64// If the types don't match, it's invalid.65if (Ty && Ty != V->getType())66return nullptr;6768Expected<Value *> MaybeV = MaterializeValueFn(Idx, ConstExprInsertBB);69if (!MaybeV) {70// TODO: We might want to propagate the precise error message here.71consumeError(MaybeV.takeError());72return nullptr;73}74return MaybeV.get();75}7677// No type specified, must be invalid reference.78if (!Ty)79return nullptr;8081// Create and return a placeholder, which will later be RAUW'd.82Value *V = new Argument(Ty);83ValuePtrs[Idx] = {V, TyID};84return V;85}868788