Index: include/llvm/AsmParser/Parser.h =================================================================== --- include/llvm/AsmParser/Parser.h +++ include/llvm/AsmParser/Parser.h @@ -21,6 +21,7 @@ class Module; class SMDiagnostic; class LLVMContext; +class Value; /// This function is the main interface to the LLVM Assembly Parser. It parses /// an ASCII file that (presumably) contains LLVM Assembly code. It returns a @@ -67,6 +68,12 @@ /// @return true on error. bool parseAssemblyInto(MemoryBufferRef F, Module &M, SMDiagnostic &Err); +/// Parses a type and a value in the given string. +/// +/// @return null on error. +const Value *parseTypeAndValue(StringRef AsmString, SMDiagnostic &Err, + const Module &M); + } // End llvm namespace #endif Index: lib/AsmParser/LLParser.h =================================================================== --- lib/AsmParser/LLParser.h +++ lib/AsmParser/LLParser.h @@ -140,6 +140,8 @@ BlockAddressPFS(nullptr) {} bool Run(); + const Value *parseStandaloneTypeAndValue(); + LLVMContext &getContext() { return Context; } private: Index: lib/AsmParser/LLParser.cpp =================================================================== --- lib/AsmParser/LLParser.cpp +++ lib/AsmParser/LLParser.cpp @@ -47,6 +47,19 @@ ValidateEndOfModule(); } +const Value *LLParser::parseStandaloneTypeAndValue() { + Lex.Lex(); + + Value *V; + if (ParseTypeAndValue(V, /*PFS=*/nullptr)) + return nullptr; + if (Lex.getKind() != lltok::Eof) { + Error(Lex.getLoc(), "expected end of string"); + return nullptr; + } + return V; +} + /// ValidateEndOfModule - Do final validity and sanity checks at the end of the /// module. bool LLParser::ValidateEndOfModule() { Index: lib/AsmParser/Parser.cpp =================================================================== --- lib/AsmParser/Parser.cpp +++ lib/AsmParser/Parser.cpp @@ -62,3 +62,12 @@ MemoryBufferRef F(AsmString, ""); return parseAssembly(F, Err, Context); } + +const Value *llvm::parseTypeAndValue(StringRef AsmString, SMDiagnostic &Err, + const Module &M) { + SourceMgr SM; + std::unique_ptr Buf = MemoryBuffer::getMemBuffer(AsmString); + SM.AddNewSourceBuffer(std::move(Buf), SMLoc()); + return LLParser(AsmString, SM, Err, const_cast(&M)) + .parseStandaloneTypeAndValue(); +} Index: unittests/AsmParser/AsmParserTest.cpp =================================================================== --- unittests/AsmParser/AsmParserTest.cpp +++ unittests/AsmParser/AsmParserTest.cpp @@ -9,6 +9,7 @@ #include "llvm/ADT/StringRef.h" #include "llvm/AsmParser/Parser.h" +#include "llvm/IR/Constants.h" #include "llvm/IR/LLVMContext.h" #include "llvm/IR/Module.h" #include "llvm/Support/SourceMgr.h" @@ -44,4 +45,33 @@ #endif #endif +TEST(AsmParserTest, TypeAndValueParsing) { + LLVMContext &Ctx = getGlobalContext(); + Module M("test", Ctx); + SMDiagnostic Error; + const Value *V; + + V = parseTypeAndValue("double 3.5", Error, M); + EXPECT_TRUE(V != nullptr); + EXPECT_TRUE(V->getType()->isDoubleTy()); + EXPECT_TRUE(isa(V)); + EXPECT_TRUE(cast(V)->isExactlyValue(3.5)); + + V = parseTypeAndValue("i32 42", Error, M); + EXPECT_TRUE(V != nullptr); + EXPECT_TRUE(V->getType()->isIntegerTy()); + EXPECT_TRUE(isa(V)); + EXPECT_TRUE(cast(V)->equalsInt(42)); + + V = parseTypeAndValue("duble 3.25", Error, M); + EXPECT_FALSE(V != nullptr); + + V = parseTypeAndValue("i32 3.25", Error, M); + EXPECT_FALSE(V != nullptr); + + V = parseTypeAndValue("i32 3, ", Error, M); + EXPECT_FALSE(V != nullptr); + EXPECT_EQ(Error.getMessage(), "expected end of string"); +} + } // end anonymous namespace