Parse named metadata.

llvm-svn: 77410
This commit is contained in:
Devang Patel 2009-07-29 00:34:02 +00:00
parent 8ed4662148
commit 127ab37209
5 changed files with 56 additions and 1 deletions

View File

@ -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]+

View File

@ -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();

View File

@ -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<MetadataBase *, 8> 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() {

View File

@ -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);

View File

@ -125,6 +125,7 @@ namespace lltok {
GlobalVar, // @foo @"foo"
LocalVar, // %foo %"foo"
StringConstant, // "foo"
NamedMD, // !foo
// Metadata valued tokens.
Metadata, // !"foo" !{i8 42}