From eff2ab61b5d411fe64ba601d402b7c549644b590 Mon Sep 17 00:00:00 2001 From: Devang Patel Date: Wed, 29 Jul 2009 00:34:02 +0000 Subject: [PATCH] Parse named metadata. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@77410 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/AsmParser/LLLexer.cpp | 18 +++++++++++++++++- lib/AsmParser/LLLexer.h | 1 + lib/AsmParser/LLParser.cpp | 36 ++++++++++++++++++++++++++++++++++++ lib/AsmParser/LLParser.h | 1 + lib/AsmParser/LLToken.h | 1 + 5 files changed, 56 insertions(+), 1 deletion(-) diff --git a/lib/AsmParser/LLLexer.cpp b/lib/AsmParser/LLLexer.cpp index c9b28212435..4a7e1230938 100644 --- a/lib/AsmParser/LLLexer.cpp +++ b/lib/AsmParser/LLLexer.cpp @@ -253,7 +253,7 @@ lltok::Kind LLLexer::LexToken() { case ';': SkipLineComment(); return LexToken(); - case '!': return lltok::Metadata; + case '!': return LexMetadata(); case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '-': @@ -421,7 +421,23 @@ static bool JustWhitespaceNewLine(const char *&Ptr) { return false; } +/// LexMetadata: +/// !{...} +/// !42 +/// !foo +lltok::Kind LLLexer::LexMetadata() { + if (isalpha(CurPtr[0])) { + ++CurPtr; + while (isalnum(CurPtr[0]) || CurPtr[0] == '-' || CurPtr[0] == '$' || + CurPtr[0] == '.' || CurPtr[0] == '_') + ++CurPtr; + StrVal.assign(TokStart+1, CurPtr); // Skip ! + return lltok::NamedMD; + } + return lltok::Metadata; +} + /// LexIdentifier: Handle several related productions: /// Label [-a-zA-Z$._0-9]+: /// IntegerType i[0-9]+ diff --git a/lib/AsmParser/LLLexer.h b/lib/AsmParser/LLLexer.h index 4e054639414..de39272f45e 100644 --- a/lib/AsmParser/LLLexer.h +++ b/lib/AsmParser/LLLexer.h @@ -75,6 +75,7 @@ namespace llvm { lltok::Kind LexDigitOrNegative(); lltok::Kind LexPositive(); lltok::Kind LexAt(); + lltok::Kind LexMetadata(); lltok::Kind LexPercent(); lltok::Kind LexQuote(); lltok::Kind Lex0x(); diff --git a/lib/AsmParser/LLParser.cpp b/lib/AsmParser/LLParser.cpp index b2d2b51b6c7..36f714b38e3 100644 --- a/lib/AsmParser/LLParser.cpp +++ b/lib/AsmParser/LLParser.cpp @@ -121,6 +121,7 @@ bool LLParser::ParseTopLevelEntities() { case lltok::LocalVar: if (ParseNamedType()) return true; break; case lltok::GlobalVar: if (ParseNamedGlobal()) return true; break; case lltok::Metadata: if (ParseStandaloneMetadata()) return true; break; + case lltok::NamedMD: if (ParseNamedMetadata()) return true; break; // The Global variable production with no name can have many different // optional leading prefixes, the production is: @@ -409,6 +410,41 @@ bool LLParser::ParseMDNode(MetadataBase *&Node) { return false; } +///ParseNamedMetadata: +/// !foo = !{ !1, !2 } +bool LLParser::ParseNamedMetadata() { + assert(Lex.getKind() == lltok::NamedMD); + Lex.Lex(); + std::string Name = Lex.getStrVal(); + + if (ParseToken(lltok::equal, "expected '=' here")) + return true; + + if (Lex.getKind() != lltok::Metadata) + return TokError("Expected '!' here"); + Lex.Lex(); + + if (Lex.getKind() != lltok::lbrace) + return TokError("Expected '{' here"); + Lex.Lex(); + SmallVector Elts; + do { + if (Lex.getKind() != lltok::Metadata) + return TokError("Expected '!' here"); + Lex.Lex(); + MetadataBase *N = 0; + if (ParseMDNode(N)) return true; + Elts.push_back(N); + } while (EatIfPresent(lltok::comma)); + + if (ParseToken(lltok::rbrace, "expected end of metadata node")) + return true; + + NamedMDNode::Create(Name.c_str(), Name.length(), + Elts.data(), Elts.size(), M); + return false; +} + /// ParseStandaloneMetadata: /// !42 = !{...} bool LLParser::ParseStandaloneMetadata() { diff --git a/lib/AsmParser/LLParser.h b/lib/AsmParser/LLParser.h index c77ef9d6d11..40848cfe77c 100644 --- a/lib/AsmParser/LLParser.h +++ b/lib/AsmParser/LLParser.h @@ -148,6 +148,7 @@ namespace llvm { bool HasLinkage, unsigned Visibility); bool ParseAlias(const std::string &Name, LocTy Loc, unsigned Visibility); bool ParseStandaloneMetadata(); + bool ParseNamedMetadata(); bool ParseMDString(MetadataBase *&S); bool ParseMDNode(MetadataBase *&N); diff --git a/lib/AsmParser/LLToken.h b/lib/AsmParser/LLToken.h index 75cc1db8ad9..a49d05442bc 100644 --- a/lib/AsmParser/LLToken.h +++ b/lib/AsmParser/LLToken.h @@ -125,6 +125,7 @@ namespace lltok { GlobalVar, // @foo @"foo" LocalVar, // %foo %"foo" StringConstant, // "foo" + NamedMD, // !foo // Metadata valued tokens. Metadata, // !"foo" !{i8 42} -- 2.34.1