From 263d80f920133d4fe03df290bfdb333cd14bda3f Mon Sep 17 00:00:00 2001 From: Alex Lorenz Date: Thu, 6 Aug 2015 23:17:42 +0000 Subject: [PATCH] MIR Parser: Simplify the token's string value handling. This commit removes the 'StringOffset' and 'HasStringValue' fields from the MIToken struct and simplifies the 'stringValue' method which now returns the new 'StringValue' field. This commit also adopts a different way of initializing the lexed tokens - instead of constructing a new MIToken instance, the lexer resets the old token using the new 'reset' method and sets its attributes using the new 'setStringValue', 'setOwnedStringValue', and 'setIntegerValue' methods. Reviewers: Sean Silva Differential Revision: http://reviews.llvm.org/D11792 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@244295 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/CodeGen/MIRParser/MILexer.cpp | 75 ++++++++++++++++++++---------- lib/CodeGen/MIRParser/MILexer.h | 32 ++++--------- lib/CodeGen/MIRParser/MIParser.cpp | 16 +++---- 3 files changed, 67 insertions(+), 56 deletions(-) diff --git a/lib/CodeGen/MIRParser/MILexer.cpp b/lib/CodeGen/MIRParser/MILexer.cpp index 310ac7b4d71..99029a93c7b 100644 --- a/lib/CodeGen/MIRParser/MILexer.cpp +++ b/lib/CodeGen/MIRParser/MILexer.cpp @@ -55,6 +55,28 @@ public: } // end anonymous namespace +MIToken &MIToken::reset(TokenKind Kind, StringRef Range) { + this->Kind = Kind; + this->Range = Range; + return *this; +} + +MIToken &MIToken::setStringValue(StringRef StrVal) { + StringValue = StrVal; + return *this; +} + +MIToken &MIToken::setOwnedStringValue(std::string StrVal) { + StringValueStorage = std::move(StrVal); + StringValue = StringValueStorage; + return *this; +} + +MIToken &MIToken::setIntegerValue(APSInt IntVal) { + this->IntVal = std::move(IntVal); + return *this; +} + /// Skip the leading whitespace characters and return the updated cursor. static Cursor skipWhitespace(Cursor C) { while (isspace(C.peek())) @@ -124,17 +146,18 @@ static Cursor lexName( if (C.peek() == '"') { if (Cursor R = lexStringConstant(C, ErrorCallback)) { StringRef String = Range.upto(R); - Token = MIToken(Type, String, - unescapeQuotedString(String.drop_front(PrefixLength)), - PrefixLength); + Token.reset(Type, String) + .setOwnedStringValue( + unescapeQuotedString(String.drop_front(PrefixLength))); return R; } - Token = MIToken(MIToken::Error, Range.remaining()); + Token.reset(MIToken::Error, Range.remaining()); return Range; } while (isIdentifierChar(C.peek())) C.advance(); - Token = MIToken(Type, Range.upto(C), PrefixLength); + Token.reset(Type, Range.upto(C)) + .setStringValue(Range.upto(C).drop_front(PrefixLength)); return C; } @@ -145,7 +168,7 @@ static Cursor maybeLexIntegerType(Cursor C, MIToken &Token) { C.advance(); // Skip 'i' while (isdigit(C.peek())) C.advance(); - Token = MIToken(MIToken::IntegerType, Range.upto(C)); + Token.reset(MIToken::IntegerType, Range.upto(C)); return C; } @@ -187,7 +210,8 @@ static Cursor maybeLexIdentifier(Cursor C, MIToken &Token) { while (isIdentifierChar(C.peek())) C.advance(); auto Identifier = Range.upto(C); - Token = MIToken(getIdentifierKind(Identifier), Identifier); + Token.reset(getIdentifierKind(Identifier), Identifier) + .setStringValue(Identifier); return C; } @@ -199,7 +223,7 @@ static Cursor maybeLexMachineBasicBlock( auto Range = C; C.advance(4); // Skip '%bb.' if (!isdigit(C.peek())) { - Token = MIToken(MIToken::Error, C.remaining()); + Token.reset(MIToken::Error, C.remaining()); ErrorCallback(C.location(), "expected a number after '%bb.'"); return C; } @@ -214,8 +238,9 @@ static Cursor maybeLexMachineBasicBlock( while (isIdentifierChar(C.peek())) C.advance(); } - Token = MIToken(MIToken::MachineBasicBlock, Range.upto(C), APSInt(Number), - StringOffset); + Token.reset(MIToken::MachineBasicBlock, Range.upto(C)) + .setIntegerValue(APSInt(Number)) + .setStringValue(Range.upto(C).drop_front(StringOffset)); return C; } @@ -228,7 +253,7 @@ static Cursor maybeLexIndex(Cursor C, MIToken &Token, StringRef Rule, auto NumberRange = C; while (isdigit(C.peek())) C.advance(); - Token = MIToken(Kind, Range.upto(C), APSInt(NumberRange.upto(C))); + Token.reset(Kind, Range.upto(C)).setIntegerValue(APSInt(NumberRange.upto(C))); return C; } @@ -249,7 +274,9 @@ static Cursor maybeLexIndexAndName(Cursor C, MIToken &Token, StringRef Rule, while (isIdentifierChar(C.peek())) C.advance(); } - Token = MIToken(Kind, Range.upto(C), APSInt(Number), StringOffset); + Token.reset(Kind, Range.upto(C)) + .setIntegerValue(APSInt(Number)) + .setStringValue(Range.upto(C).drop_front(StringOffset)); return C; } @@ -295,8 +322,8 @@ static Cursor lexVirtualRegister(Cursor C, MIToken &Token) { auto NumberRange = C; while (isdigit(C.peek())) C.advance(); - Token = MIToken(MIToken::VirtualRegister, Range.upto(C), - APSInt(NumberRange.upto(C))); + Token.reset(MIToken::VirtualRegister, Range.upto(C)) + .setIntegerValue(APSInt(NumberRange.upto(C))); return C; } @@ -309,8 +336,8 @@ static Cursor maybeLexRegister(Cursor C, MIToken &Token) { C.advance(); // Skip '%' while (isIdentifierChar(C.peek())) C.advance(); - Token = MIToken(MIToken::NamedRegister, Range.upto(C), - /*StringOffset=*/1); // Drop the '%' + Token.reset(MIToken::NamedRegister, Range.upto(C)) + .setStringValue(Range.upto(C).drop_front(1)); // Drop the '%' return C; } @@ -327,8 +354,8 @@ static Cursor maybeLexGlobalValue( auto NumberRange = C; while (isdigit(C.peek())) C.advance(); - Token = - MIToken(MIToken::GlobalValue, Range.upto(C), APSInt(NumberRange.upto(C))); + Token.reset(MIToken::GlobalValue, Range.upto(C)) + .setIntegerValue(APSInt(NumberRange.upto(C))); return C; } @@ -354,7 +381,7 @@ static Cursor maybeLexHexFloatingPointLiteral(Cursor C, MIToken &Token) { C.advance(); while (isxdigit(C.peek())) C.advance(); - Token = MIToken(MIToken::FloatingPointLiteral, Range.upto(C)); + Token.reset(MIToken::FloatingPointLiteral, Range.upto(C)); return C; } @@ -370,7 +397,7 @@ static Cursor lexFloatingPointLiteral(Cursor Range, Cursor C, MIToken &Token) { while (isdigit(C.peek())) C.advance(); } - Token = MIToken(MIToken::FloatingPointLiteral, Range.upto(C)); + Token.reset(MIToken::FloatingPointLiteral, Range.upto(C)); return C; } @@ -384,7 +411,7 @@ static Cursor maybeLexNumericalLiteral(Cursor C, MIToken &Token) { if (C.peek() == '.') return lexFloatingPointLiteral(Range, C, Token); StringRef StrVal = Range.upto(C); - Token = MIToken(MIToken::IntegerLiteral, StrVal, APSInt(StrVal)); + Token.reset(MIToken::IntegerLiteral, StrVal).setIntegerValue(APSInt(StrVal)); return C; } @@ -423,7 +450,7 @@ static Cursor maybeLexSymbol(Cursor C, MIToken &Token) { return None; auto Range = C; C.advance(Length); - Token = MIToken(Kind, Range.upto(C)); + Token.reset(Kind, Range.upto(C)); return C; } @@ -432,7 +459,7 @@ StringRef llvm::lexMIToken( function_ref ErrorCallback) { auto C = skipWhitespace(Cursor(Source)); if (C.isEOF()) { - Token = MIToken(MIToken::Eof, C.remaining()); + Token.reset(MIToken::Eof, C.remaining()); return C.remaining(); } @@ -467,7 +494,7 @@ StringRef llvm::lexMIToken( if (Cursor R = maybeLexSymbol(C, Token)) return R.remaining(); - Token = MIToken(MIToken::Error, C.remaining()); + Token.reset(MIToken::Error, C.remaining()); ErrorCallback(C.location(), Twine("unexpected character '") + Twine(C.peek()) + "'"); return C.remaining(); diff --git a/lib/CodeGen/MIRParser/MILexer.h b/lib/CodeGen/MIRParser/MILexer.h index 79bcb7fce01..752344ed050 100644 --- a/lib/CodeGen/MIRParser/MILexer.h +++ b/lib/CodeGen/MIRParser/MILexer.h @@ -94,26 +94,19 @@ struct MIToken { private: TokenKind Kind; - unsigned StringOffset; - bool HasStringValue; StringRef Range; - std::string StringValue; + StringRef StringValue; + std::string StringValueStorage; APSInt IntVal; public: - MIToken(TokenKind Kind, StringRef Range, unsigned StringOffset = 0) - : Kind(Kind), StringOffset(StringOffset), HasStringValue(false), - Range(Range) {} + MIToken() : Kind(Error) {} - MIToken(TokenKind Kind, StringRef Range, std::string StringValue, - unsigned StringOffset = 0) - : Kind(Kind), StringOffset(StringOffset), HasStringValue(true), - Range(Range), StringValue(std::move(StringValue)) {} + MIToken &reset(TokenKind Kind, StringRef Range); - MIToken(TokenKind Kind, StringRef Range, const APSInt &IntVal, - unsigned StringOffset = 0) - : Kind(Kind), StringOffset(StringOffset), HasStringValue(false), - Range(Range), IntVal(IntVal) {} + MIToken &setStringValue(StringRef StrVal); + MIToken &setOwnedStringValue(std::string StrVal); + MIToken &setIntegerValue(APSInt IntVal); TokenKind kind() const { return Kind; } @@ -141,17 +134,10 @@ public: StringRef::iterator location() const { return Range.begin(); } - /// Return the token's raw string value. - /// - /// If the string value is quoted, this method returns that quoted string as - /// it is, without unescaping the string value. - StringRef rawStringValue() const { return Range.drop_front(StringOffset); } + StringRef range() const { return Range; } /// Return the token's string value. - StringRef stringValue() const { - return HasStringValue ? StringRef(StringValue) - : Range.drop_front(StringOffset); - } + StringRef stringValue() const { return StringValue; } const APSInt &integerValue() const { return IntVal; } diff --git a/lib/CodeGen/MIRParser/MIParser.cpp b/lib/CodeGen/MIRParser/MIParser.cpp index f8a6200cc67..e577458f8fc 100644 --- a/lib/CodeGen/MIRParser/MIParser.cpp +++ b/lib/CodeGen/MIRParser/MIParser.cpp @@ -200,7 +200,7 @@ MIParser::MIParser(SourceMgr &SM, MachineFunction &MF, SMDiagnostic &Error, StringRef Source, const PerFunctionMIParsingState &PFS, const SlotMapping &IRSlots) : SM(SM), MF(MF), Error(Error), Source(Source), CurrentSource(Source), - Token(MIToken::Error, StringRef()), PFS(PFS), IRSlots(IRSlots) {} + PFS(PFS), IRSlots(IRSlots) {} void MIParser::lex() { CurrentSource = lexMIToken( @@ -571,7 +571,7 @@ bool MIParser::parseImmediateOperand(MachineOperand &Dest) { } bool MIParser::parseIRConstant(StringRef::iterator Loc, const Constant *&C) { - auto Source = StringRef(Loc, Token.stringValue().end() - Loc).str(); + auto Source = StringRef(Loc, Token.range().end() - Loc).str(); lex(); SMDiagnostic Err; C = parseConstantValue(Source.c_str(), Err, *MF.getFunction()->getParent()); @@ -681,8 +681,8 @@ bool MIParser::parseGlobalValue(GlobalValue *&GV) { const Module *M = MF.getFunction()->getParent(); GV = M->getNamedValue(Token.stringValue()); if (!GV) - return error(Twine("use of undefined global value '@") + - Token.rawStringValue() + "'"); + return error(Twine("use of undefined global value '") + Token.range() + + "'"); break; } case MIToken::GlobalValue: { @@ -851,8 +851,7 @@ bool MIParser::parseIRBlock(BasicBlock *&BB, const Function &F) { BB = dyn_cast_or_null( F.getValueSymbolTable().lookup(Token.stringValue())); if (!BB) - return error(Twine("use of undefined IR block '%ir-block.") + - Token.rawStringValue() + "'"); + return error(Twine("use of undefined IR block '") + Token.range() + "'"); break; } case MIToken::IRBlock: { @@ -1019,7 +1018,7 @@ bool MIParser::parseMachineOperandAndTargetFlags(MachineOperand &Dest) { bool MIParser::parseOperandsOffset(MachineOperand &Op) { if (Token.isNot(MIToken::plus) && Token.isNot(MIToken::minus)) return false; - StringRef Sign = Token.stringValue(); + StringRef Sign = Token.range(); bool IsNegative = Token.is(MIToken::minus); lex(); if (Token.isNot(MIToken::IntegerLiteral)) @@ -1039,8 +1038,7 @@ bool MIParser::parseIRValue(Value *&V) { case MIToken::NamedIRValue: { V = MF.getFunction()->getValueSymbolTable().lookup(Token.stringValue()); if (!V) - return error(Twine("use of undefined IR value '%ir.") + - Token.rawStringValue() + "'"); + return error(Twine("use of undefined IR value '") + Token.range() + "'"); break; } // TODO: Parse unnamed IR value references. -- 2.34.1