} // 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()))
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
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;
}
C.advance();
while (isxdigit(C.peek()))
C.advance();
- Token = MIToken(MIToken::FloatingPointLiteral, Range.upto(C));
+ Token.reset(MIToken::FloatingPointLiteral, Range.upto(C));
return C;
}
while (isdigit(C.peek()))
C.advance();
}
- Token = MIToken(MIToken::FloatingPointLiteral, Range.upto(C));
+ Token.reset(MIToken::FloatingPointLiteral, Range.upto(C));
return C;
}
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;
}
return None;
auto Range = C;
C.advance(Length);
- Token = MIToken(Kind, Range.upto(C));
+ Token.reset(Kind, Range.upto(C));
return C;
}
function_ref<void(StringRef::iterator Loc, const Twine &)> ErrorCallback) {
auto C = skipWhitespace(Cursor(Source));
if (C.isEOF()) {
- Token = MIToken(MIToken::Eof, C.remaining());
+ Token.reset(MIToken::Eof, C.remaining());
return C.remaining();
}
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();
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; }
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; }
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(
}
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());
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: {
BB = dyn_cast_or_null<BasicBlock>(
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: {
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))
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.