Index: lib/CodeGen/MIRParser/MILexer.h =================================================================== --- lib/CodeGen/MIRParser/MILexer.h +++ lib/CodeGen/MIRParser/MILexer.h @@ -94,26 +94,19 @@ 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 @@ 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; } Index: lib/CodeGen/MIRParser/MILexer.cpp =================================================================== --- lib/CodeGen/MIRParser/MILexer.cpp +++ lib/CodeGen/MIRParser/MILexer.cpp @@ -55,6 +55,28 @@ } // 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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 @@ 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(); Index: lib/CodeGen/MIRParser/MIParser.cpp =================================================================== --- lib/CodeGen/MIRParser/MIParser.cpp +++ lib/CodeGen/MIRParser/MIParser.cpp @@ -200,7 +200,7 @@ 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::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 @@ 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 @@ 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::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 @@ 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.