LLVM  14.0.0git
Public Types | Public Member Functions | Friends | List of all members
llvm::Expected< T > Class Template Reference

Tagged union holding either a T or a Error. More...

#include "llvm/ADT/APFloat.h"

Inheritance diagram for llvm::Expected< T >:
Inheritance graph
[legend]
Collaboration diagram for llvm::Expected< T >:
Collaboration graph
[legend]

Public Types

using storage_type = std::conditional_t< isRef, wrap, T >
 
using value_type = T
 

Public Member Functions

 Expected (Error Err)
 Create an Expected<T> error value from the given Error. More...
 
 Expected (ErrorSuccess)=delete
 Forbid to convert from Error::success() implicitly, this avoids having Expected<T> foo() { return Error::success(); } which compiles otherwise but triggers the assertion above. More...
 
template<typename OtherT >
 Expected (OtherT &&Val, std::enable_if_t< std::is_convertible< OtherT, T >::value > *=nullptr)
 Create an Expected<T> success value from the given OtherT value, which must be convertible to T. More...
 
 Expected (Expected &&Other)
 Move construct an Expected<T> value. More...
 
template<class OtherT >
 Expected (Expected< OtherT > &&Other, std::enable_if_t< std::is_convertible< OtherT, T >::value > *=nullptr)
 Move construct an Expected<T> value from an Expected<OtherT>, where OtherT must be convertible to T. More...
 
template<class OtherT >
 Expected (Expected< OtherT > &&Other, std::enable_if_t<!std::is_convertible< OtherT, T >::value > *=nullptr)
 Move construct an Expected<T> value from an Expected<OtherT>, where OtherT isn't convertible to T. More...
 
Expectedoperator= (Expected &&Other)
 Move-assign from another Expected<T>. More...
 
 ~Expected ()
 Destroy an Expected<T>. More...
 
 operator bool ()
 Return false if there is an error. More...
 
reference get ()
 Returns a reference to the stored T value. More...
 
const_reference get () const
 Returns a const reference to the stored T value. More...
 
template<class OtherT >
Error moveInto (OtherT &Value, std::enable_if_t< std::is_assignable< OtherT &, T && >::value > *=nullptr) &&
 Returns takeError() after moving the held T (if any) into V. More...
 
template<typename ErrT >
bool errorIsA () const
 Check that this Expected<T> is an error of type ErrT. More...
 
Error takeError ()
 Take ownership of the stored error. More...
 
pointer operator-> ()
 Returns a pointer to the stored T value. More...
 
const_pointer operator-> () const
 Returns a const pointer to the stored T value. More...
 
reference operator* ()
 Returns a reference to the stored T value. More...
 
const_reference operator* () const
 Returns a const reference to the stored T value. More...
 

Friends

template<class T1 >
class ExpectedAsOutParameter
 
template<class OtherT >
class Expected
 

Detailed Description

template<class T>
class llvm::Expected< T >

Tagged union holding either a T or a Error.

This class parallels ErrorOr, but replaces error_code with Error. Since Error cannot be copied, this class replaces getError() with takeError(). It also adds an bool errorIsA<ErrT>() method for testing the error class type.

Example usage of 'Expected<T>' as a function return type:

Expected<int> myDivide(int A, int B) {
if (B == 0) {
// return an Error
"B must not be zero!");
}
// return an integer
return A / B;
}

Checking the results of to a function returning 'Expected<T>':

if (auto E = Result.takeError()) {
// We must consume the error. Typically one of:
// - return the error to our caller
// - toString(), when logging
// - consumeError(), to silently swallow the error
// - handleErrors(), to distinguish error types
errs() << "Problem with division " << toString(std::move(E)) << "\n";
return;
}
// use the result
outs() << "The answer is " << *Result << "\n";

For unit-testing a function returning an 'Expceted<T>', see the 'EXPECT_THAT_EXPECTED' macros in llvm/Testing/Support/Error.h

Definition at line 42 of file APFloat.h.

Member Typedef Documentation

◆ storage_type

template<class T >
using llvm::Expected< T >::storage_type = std::conditional_t<isRef, wrap, T>

Definition at line 482 of file Error.h.

◆ value_type

template<class T >
using llvm::Expected< T >::value_type = T

Definition at line 483 of file Error.h.

Constructor & Destructor Documentation

◆ Expected() [1/6]

template<class T >
llvm::Expected< T >::Expected ( Error  Err)
inline

Create an Expected<T> error value from the given Error.

Definition at line 493 of file Error.h.

◆ Expected() [2/6]

template<class T >
llvm::Expected< T >::Expected ( ErrorSuccess  )
delete

Forbid to convert from Error::success() implicitly, this avoids having Expected<T> foo() { return Error::success(); } which compiles otherwise but triggers the assertion above.

◆ Expected() [3/6]

template<class T >
template<typename OtherT >
llvm::Expected< T >::Expected ( OtherT &&  Val,
std::enable_if_t< std::is_convertible< OtherT, T >::value > *  = nullptr 
)
inline

Create an Expected<T> success value from the given OtherT value, which must be convertible to T.

Definition at line 512 of file Error.h.

◆ Expected() [4/6]

template<class T >
llvm::Expected< T >::Expected ( Expected< T > &&  Other)
inline

Move construct an Expected<T> value.

Definition at line 525 of file Error.h.

◆ Expected() [5/6]

template<class T >
template<class OtherT >
llvm::Expected< T >::Expected ( Expected< OtherT > &&  Other,
std::enable_if_t< std::is_convertible< OtherT, T >::value > *  = nullptr 
)
inline

Move construct an Expected<T> value from an Expected<OtherT>, where OtherT must be convertible to T.

Definition at line 530 of file Error.h.

◆ Expected() [6/6]

template<class T >
template<class OtherT >
llvm::Expected< T >::Expected ( Expected< OtherT > &&  Other,
std::enable_if_t<!std::is_convertible< OtherT, T >::value > *  = nullptr 
)
inlineexplicit

Move construct an Expected<T> value from an Expected<OtherT>, where OtherT isn't convertible to T.

Definition at line 539 of file Error.h.

◆ ~Expected()

template<class T >
llvm::Expected< T >::~Expected ( )
inline

Destroy an Expected<T>.

Definition at line 552 of file Error.h.

Member Function Documentation

◆ errorIsA()

template<class T >
template<typename ErrT >
bool llvm::Expected< T >::errorIsA ( ) const
inline

Check that this Expected<T> is an error of type ErrT.

Definition at line 591 of file Error.h.

◆ get() [1/2]

template<class T >
reference llvm::Expected< T >::get ( )
inline

Returns a reference to the stored T value.

Definition at line 569 of file Error.h.

Referenced by llvm::BitstreamCursor::advance(), llvm::BitcodeAnalyzer::analyze(), llvm::gsym::ObjectFileTransformer::convert(), llvm::object::Slice::create(), llvm::object::createBinary(), llvm::pdb::NativeSession::createFromExe(), decode(), llvm::DWARFLocationTable::dumpLocationList(), llvm::BitstreamCursor::EnterSubBlock(), llvm::object::IRObjectFile::findBitcodeInMemBuffer(), llvm::pdb::NativeInlineSiteSymbol::findInlineeLinesByVA(), llvm::MCJIT::findSymbol(), llvm::MCJIT::generateCodeForModule(), llvm::object::Archive::Child::getAsBinary(), llvm::object::Archive::Child::getBuffer(), llvm::object::XCOFFObjectFile::getCommonSymbolSizeImpl(), getDbiStreamPtr(), llvm::object::XCOFFObjectFile::getImportFileTable(), llvm::object::Archive::Child::getMemoryBufferRef(), llvm::object::ArchiveMemberHeader::getName(), llvm::object::Archive::Child::getName(), llvm::object::Archive::Child::getNext(), llvm::NewArchiveMember::getOldMember(), llvm::object::XCOFFObjectFile::getSymbolAlignment(), llvm::object::XCOFFObjectFile::getSymbolFlags(), llvm::object::XCOFFObjectFile::getSymbolSection(), llvm::object::XCOFFObjectFile::getSymbolSectionName(), llvm::object::XCOFFObjectFile::getSymbolSize(), llvm::object::XCOFFObjectFile::getSymbolType(), llvm::object::ELFObjectFile< ELFT >::isDebugSection(), llvm::object::MachOObjectFile::isDebugSection(), llvm::object::COFFObjectFile::isDebugSection(), llvm::object::XCOFFSymbolRef::isFunction(), jumpToValueSymbolTable(), LLVMCreateBinary(), LLVMCreateObjectFile(), LLVMMachOUniversalBinaryCopyObjectForArch(), llvm::pdb::loadDataForEXE(), parseInlineInfo(), llvm::BitstreamCursor::ReadAbbrevRecord(), readCoverageMappingData(), llvm::ifs::readELFFile(), llvm::BitstreamCursor::readRecord(), llvm::SimpleBitstreamCursor::ReadVBR(), llvm::SimpleBitstreamCursor::ReadVBR64(), llvm::pdb::NativeSession::searchForPdb(), llvm::BitstreamCursor::SkipBlock(), llvm::BitstreamCursor::skipRecord(), splitCodeGen(), and upgrade().

◆ get() [2/2]

template<class T >
const_reference llvm::Expected< T >::get ( ) const
inline

Returns a const reference to the stored T value.

Definition at line 575 of file Error.h.

◆ moveInto()

template<class T >
template<class OtherT >
Error llvm::Expected< T >::moveInto ( OtherT &  Value,
std::enable_if_t< std::is_assignable< OtherT &, T && >::value > *  = nullptr 
) &&
inline

Returns takeError() after moving the held T (if any) into V.

Definition at line 582 of file Error.h.

◆ operator bool()

template<class T >
llvm::Expected< T >::operator bool ( )
inlineexplicit

Return false if there is an error.

Definition at line 561 of file Error.h.

◆ operator*() [1/2]

template<class T >
reference llvm::Expected< T >::operator* ( )
inline

Returns a reference to the stored T value.

Definition at line 619 of file Error.h.

◆ operator*() [2/2]

template<class T >
const_reference llvm::Expected< T >::operator* ( ) const
inline

Returns a const reference to the stored T value.

Definition at line 625 of file Error.h.

◆ operator->() [1/2]

template<class T >
pointer llvm::Expected< T >::operator-> ( )
inline

Returns a pointer to the stored T value.

Definition at line 607 of file Error.h.

◆ operator->() [2/2]

template<class T >
const_pointer llvm::Expected< T >::operator-> ( ) const
inline

Returns a const pointer to the stored T value.

Definition at line 613 of file Error.h.

◆ operator=()

template<class T >
Expected& llvm::Expected< T >::operator= ( Expected< T > &&  Other)
inline

Move-assign from another Expected<T>.

Definition at line 546 of file Error.h.

Referenced by llvm::MSVCPExpected< T >::operator=().

◆ takeError()

template<class T >
Error llvm::Expected< T >::takeError ( )
inline

Take ownership of the stored error.

After calling this the Expected<T> is in an indeterminate state that can only be safely destructed. No further calls (beside the destructor) should be made on the Expected<T> value.

Definition at line 599 of file Error.h.

Referenced by llvm::pdb::PDBFileBuilder::addNamedStream(), llvm::BitstreamCursor::advance(), advanceToMetaBlock(), llvm::BitcodeAnalyzer::analyze(), llvm::object::ELFFile< ELFT >::android_relas(), llvm::lto::backend(), llvm::object::BindRebaseSegInfo::BindRebaseSegInfo(), llvm::ifs::buildStub(), llvm::pdb::PDBFileBuilder::commit(), llvm::msf::MSFBuilder::commit(), llvm::RuntimeDyldImpl::computeSectionStubBufSize(), llvm::object::computeSymbolSizes(), llvm::RuntimeDyldImpl::computeTotalAllocSize(), llvm::object::SectionRef::containsSymbol(), llvm::gsym::ObjectFileTransformer::convert(), llvm::object::MinidumpFile::create(), llvm::GlobPattern::create(), llvm::symbolize::SymbolizableObjectFile::create(), llvm::MinidumpYAML::Stream::create(), llvm::object::Slice::create(), llvm::object::IRObjectFile::create(), llvm::lto::InputFile::create(), llvm::object::createBinary(), llvm::pdb::NativeSession::createFromExe(), llvm::LTOModule::createInputFile(), llvm::mca::InstrBuilder::createInstruction(), llvm::jitlink::createLinkGraphFromELFObject(), createOnDiskBuffer(), llvm::remarks::createYAMLParserFromMeta(), decode(), llvm::object::ELFFile< ELFT >::decodeBBAddrMap(), doImportingForModule(), llvm::DWARFLocationTable::dumpLocationList(), llvm::object::ELFFile< ELFT >::dynamicEntries(), llvm::orc::RTDyldObjectLinkingLayer::emit(), llvm::RuntimeDyldImpl::emitSection(), llvm::BitstreamCursor::EnterSubBlock(), llvm::expectedToErrorOrAndEmitErrors(), llvm::RuntimeDyldELF::finalizeLoad(), llvm::RuntimeDyldCOFFX86_64::finalizeLoad(), llvm::pdb::GSIStreamBuilder::finalizeMsfLayout(), llvm::object::IRObjectFile::findBitcodeInMemBuffer(), llvm::object::IRObjectFile::findBitcodeInObject(), llvm::pdb::TpiStream::findFullDeclForForwardRef(), llvm::pdb::NativeInlineSiteSymbol::findInlineeLinesByVA(), llvm::pdb::NativeFunctionSymbol::findInlineFramesByVA(), llvm::pdb::SymbolCache::findLineNumbersByVA(), llvm::MCJIT::findSymbol(), llvm::lto::findThinLTOModule(), llvm::JITSymbolFlags::fromObjectSymbol(), llvm::ARMJITSymbolFlags::fromObjectSymbol(), llvm::DWARFDebugAranges::generate(), llvm::MCJIT::generateCodeForModule(), llvm::object::Archive::Child::getAsBinary(), llvm::getBitcodeFileContents(), getBitcodeFileMachine(), llvm::getBitcodeLTOInfo(), llvm::getBitcodeProducerString(), llvm::getBitcodeTargetTriple(), llvm::object::Archive::Child::getBuffer(), llvm::object::ObjectFile::getCommonSymbolSize(), llvm::object::XCOFFObjectFile::getCommonSymbolSizeImpl(), llvm::object::SectionRef::getContents(), getCUIdentifiers(), getDbiStreamPtr(), llvm::object::ELFFile< ELFT >::getDynSymtabSize(), llvm::object::getELFAddend(), llvm::orc::getELFObjectFileSymbolInfo(), llvm::object::getExtendedSymbolTableIndex(), llvm::MemoryBuffer::getFileAsStream(), getFileAux(), llvm::object::Archive::Child::getFullName(), llvm::gsym::GsymReader::getFunctionInfo(), llvm::orc::getGenericObjectFileSymbolInfo(), llvm::object::XCOFFObjectFile::getImportFileTable(), llvm::getLazyBitcodeModule(), llvm::DWARFContext::getLineTableForUnit(), llvm::object::ELFFile< ELFT >::getLinkAsStrtab(), llvm::orc::getMachOObjectFileSymbolInfo(), llvm::ExpressionFormat::getMatchingString(), getMemoryBufferForStream(), llvm::object::Archive::Child::getMemoryBufferRef(), llvm::getModuleSummaryIndex(), llvm::object::ArchiveMemberHeader::getName(), llvm::object::Archive::Child::getName(), llvm::object::Archive::Child::getNext(), llvm::getOffset(), llvm::NewArchiveMember::getOldMember(), getOpenFileImpl(), getPdbPathFromExe(), llvm::LTOModule::getProducerString(), getReadWriteFile(), llvm::object::ELFObjectFile< ELFT >::getRelocatedSection(), llvm::remarks::getRemarksSectionContents(), llvm::StringSubstitution::getResult(), llvm::NumericSubstitution::getResult(), llvm::object::ELFFile< ELFT >::getSectionIndex(), getSingleModule(), llvm::object::ELFFile< ELFT >::getStringTableForSymtab(), llvm::object::ELFObjectFile< ELFT >::getSymbolAddress(), llvm::object::ELFObjectFile< ELFT >::getSymbolAlignment(), llvm::object::XCOFFObjectFile::getSymbolAlignment(), llvm::object::ELFObjectFile< ELFT >::getSymbolBinding(), llvm::object::ELFObjectFile< ELFT >::getSymbolELFType(), llvm::object::ELFObjectFile< ELFT >::getSymbolFlags(), llvm::object::XCOFFObjectFile::getSymbolFlags(), getSymbolInfo(), llvm::object::ELFObjectFile< ELFT >::getSymbolName(), llvm::object::ELFObjectFile< ELFT >::getSymbolOther(), llvm::object::ELFObjectFile< ELFT >::getSymbolSection(), llvm::object::XCOFFObjectFile::getSymbolSection(), llvm::object::COFFObjectFile::getSymbolSection(), llvm::object::XCOFFObjectFile::getSymbolSectionName(), llvm::object::XCOFFObjectFile::getSymbolSize(), llvm::object::ELFObjectFile< ELFT >::getSymbolType(), llvm::object::XCOFFObjectFile::getSymbolType(), llvm::object::ELFObjectFile< ELFT >::getSymbolValueImpl(), getUUID(), llvm::object::ELFFile< ELFT >::getVersionDefinitions(), llvm::object::ELFFile< ELFT >::getVersionDependencies(), handleCompressedSection(), llvm::handleSection(), llvm::pdb::PDBFile::hasPDBInjectedSourceStream(), llvm::pdb::PDBFile::hasPDBStringTable(), llvm::FunctionImporter::importFunctions(), isArchiveSymbol(), llvm::isBitcodeContainingObjCCategory(), llvm::LTOModule::isBitcodeFile(), llvm::LTOModule::isBitcodeForTarget(), isBlock(), llvm::object::Decompressor::isCompressed(), llvm::object::ELFObjectFile< ELFT >::isDebugSection(), llvm::object::MachOObjectFile::isDebugSection(), llvm::object::COFFObjectFile::isDebugSection(), llvm::object::ObjectFile::isSectionBitcode(), llvm::isThumbFunc(), isVariableIndexable(), jumpToValueSymbolTable(), llvm::remarks::RemarkLinker::link(), llvm::jitlink::JITLinkerBase::linkPhase2(), llvm::jitlink::JITLinkerBase::linkPhase3(), llvm::jitlink::JITLinkerBase::linkPhase4(), LLVMCreateBinary(), LLVMCreateObjectFile(), LLVMMachOUniversalBinaryCopyObjectForArch(), LLVMMoveToContainingSection(), LLVMRemarkParserGetNext(), llvm::object::ResourceSectionRef::load(), llvm::pdb::loadDataForEXE(), llvm::xray::loadInstrumentationMap(), loadObj(), llvm::xray::loadProfile(), llvm::xray::loadTraceFile(), llvm::object::ELFFile< ELFT >::loadVersionMap(), llvm::localCache(), llvm::gsym::GsymReader::lookup(), lookupSections(), llvm::MSVCPExpected< T >::MSVCPExpected(), llvm::remarks::YAMLRemarkParser::next(), llvm::orc::DebugObjectManagerPlugin::notifyEmitted(), llvm::parseBitcodeFile(), parseBitcodeFileImpl(), parseBlock(), llvm::remarks::BitstreamParserHelper::parseBlockInfoBlock(), llvm::Pattern::parseNumericSubstitutionBlock(), parseRecord(), llvm::remarks::YAMLRemarkParser::parseRemark(), parseV5DirFileTables(), llvm::RuntimeDyldCOFFI386::processRelocationRef(), llvm::RuntimeDyldCOFFThumb::processRelocationRef(), llvm::RuntimeDyldCOFFAArch64::processRelocationRef(), llvm::RuntimeDyldCOFFX86_64::processRelocationRef(), llvm::RuntimeDyldELF::processRelocationRef(), llvm::BitstreamCursor::ReadAbbrevRecord(), readCoverageMappingData(), readDynsymVersionsImpl(), llvm::ifs::readELFFile(), llvm::object::readIRSymtab(), llvm::readModuleSummaryIndex(), llvm::BitstreamCursor::readRecord(), readTriple(), llvm::pdb::PDBFile::safelyCreateNamedStream(), llvm::pdb::NativeSession::searchForPdb(), llvm::remarks::RemarkLinker::serialize(), llvm::setupLLVMOptimizationRemarks(), llvm::BitstreamCursor::SkipBlock(), llvm::BitstreamCursor::skipRecord(), llvm::lto::thinBackend(), upgrade(), llvm::DWARFLocationTable::visitAbsoluteLocationList(), llvm::writeArchive(), llvm::writeArchiveToStream(), llvm::ifs::writeELFBinaryToFile(), llvm::writeToOutput(), llvm::object::writeUniversalBinary(), llvm::object::writeUniversalBinaryToStream(), and llvm::yaml::yaml2ObjectFile().

Friends And Related Function Documentation

◆ Expected

template<class T >
template<class OtherT >
friend class Expected
friend

Definition at line 473 of file Error.h.

◆ ExpectedAsOutParameter

template<class T >
template<class T1 >
friend class ExpectedAsOutParameter
friend

Definition at line 472 of file Error.h.

Member Data Documentation

◆ ErrorStorage

template<class T >
AlignedCharArrayUnion<error_type> llvm::Expected< T >::ErrorStorage

Definition at line 722 of file Error.h.

◆ TStorage

template<class T >
AlignedCharArrayUnion<storage_type> llvm::Expected< T >::TStorage

Definition at line 721 of file Error.h.


The documentation for this class was generated from the following files:
llvm::toString
std::string toString(Error E)
Write all error messages (if any) in E to a string.
Definition: Error.h:1030
llvm::errs
raw_fd_ostream & errs()
This returns a reference to a raw_ostream for standard error.
Definition: raw_ostream.cpp:893
llvm::outs
raw_fd_ostream & outs()
This returns a reference to a raw_fd_ostream for standard output.
Definition: raw_ostream.cpp:885
E
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
llvm::ARM_PROC::A
@ A
Definition: ARMBaseInfo.h:34
llvm::ms_demangle::QualifierMangleMode::Result
@ Result
B
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
move
compiles ldr LCPI1_0 ldr ldr mov lsr tst moveq r1 ldr LCPI1_1 and r0 bx lr It would be better to do something like to fold the shift into the conditional move
Definition: README.txt:546
llvm::createStringError
Error createStringError(std::error_code EC, char const *Fmt, const Ts &... Vals)
Create formatted StringError object.
Definition: Error.h:1241
llvm::inconvertibleErrorCode
std::error_code inconvertibleErrorCode()
The value returned by this function can be returned from convertToErrorCode for Error values where no...
Definition: Error.cpp:77