#include "clang/AST/ASTContext.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/Parse/Parser.h"
#include "clang/Parse/RAIIObjectsForParser.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/MC/MCAsmInfo.h"
#include "llvm/MC/MCContext.h"
#include "llvm/MC/MCInstPrinter.h"
#include "llvm/MC/MCInstrInfo.h"
#include "llvm/MC/MCObjectFileInfo.h"
#include "llvm/MC/MCParser/MCAsmParser.h"
#include "llvm/MC/MCParser/MCTargetAsmParser.h"
#include "llvm/MC/MCRegisterInfo.h"
#include "llvm/MC/MCStreamer.h"
#include "llvm/MC/MCSubtargetInfo.h"
#include "llvm/MC/MCTargetOptions.h"
#include "llvm/MC/TargetRegistry.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/TargetSelect.h"
using namespace clang;
namespace {
class ClangAsmParserCallback : public llvm::MCAsmParserSemaCallback {
Parser &TheParser;
SourceLocation AsmLoc;
StringRef AsmString;
ArrayRef<Token> AsmToks;
ArrayRef<unsigned> AsmTokOffsets;
public:
ClangAsmParserCallback(Parser &P, SourceLocation Loc, StringRef AsmString,
ArrayRef<Token> Toks, ArrayRef<unsigned> Offsets)
: TheParser(P), AsmLoc(Loc), AsmString(AsmString), AsmToks(Toks),
AsmTokOffsets(Offsets) {
assert(AsmToks.size() == AsmTokOffsets.size());
}
void LookupInlineAsmIdentifier(StringRef &LineBuf,
llvm::InlineAsmIdentifierInfo &Info,
bool IsUnevaluatedContext) override;
StringRef LookupInlineAsmLabel(StringRef Identifier, llvm::SourceMgr &LSM,
llvm::SMLoc Location,
bool Create) override;
bool LookupInlineAsmField(StringRef Base, StringRef Member,
unsigned &Offset) override {
return TheParser.getActions().LookupInlineAsmField(Base, Member, Offset,
AsmLoc);
}
static void DiagHandlerCallback(const llvm::SMDiagnostic &D, void *Context) {
((ClangAsmParserCallback *)Context)->handleDiagnostic(D);
}
private:
void findTokensForString(StringRef Str, SmallVectorImpl<Token> &TempToks,
const Token *&FirstOrigToken) const;
SourceLocation translateLocation(const llvm::SourceMgr &LSM,
llvm::SMLoc SMLoc);
void handleDiagnostic(const llvm::SMDiagnostic &D);
};
}
void ClangAsmParserCallback::LookupInlineAsmIdentifier(
StringRef &LineBuf, llvm::InlineAsmIdentifierInfo &Info,
bool IsUnevaluatedContext) {
SmallVector<Token, 16> LineToks;
const Token *FirstOrigToken = nullptr;
findTokensForString(LineBuf, LineToks, FirstOrigToken);
unsigned NumConsumedToks;
ExprResult Result = TheParser.ParseMSAsmIdentifier(LineToks, NumConsumedToks,
IsUnevaluatedContext);
if (NumConsumedToks == 0 || NumConsumedToks == LineToks.size()) {
} else {
assert(FirstOrigToken && "not using original tokens?");
assert(FirstOrigToken[NumConsumedToks].getLocation() ==
LineToks[NumConsumedToks].getLocation());
unsigned FirstIndex = FirstOrigToken - AsmToks.begin();
unsigned LastIndex = FirstIndex + NumConsumedToks - 1;
unsigned TotalOffset =
(AsmTokOffsets[LastIndex] + AsmToks[LastIndex].getLength() -
AsmTokOffsets[FirstIndex]);
LineBuf = LineBuf.substr(0, TotalOffset);
}
if (!Result.isUsable())
return;
TheParser.getActions().FillInlineAsmIdentifierInfo(Result.get(), Info);
}
StringRef ClangAsmParserCallback::LookupInlineAsmLabel(StringRef Identifier,
llvm::SourceMgr &LSM,
llvm::SMLoc Location,
bool Create) {
SourceLocation Loc = translateLocation(LSM, Location);
LabelDecl *Label =
TheParser.getActions().GetOrCreateMSAsmLabel(Identifier, Loc, Create);
return Label->getMSAsmLabel();
}
void ClangAsmParserCallback::findTokensForString(
StringRef Str, SmallVectorImpl<Token> &TempToks,
const Token *&FirstOrigToken) const {
assert(!std::less<const char *>()(Str.begin(), AsmString.begin()) &&
!std::less<const char *>()(AsmString.end(), Str.end()));
unsigned FirstCharOffset = Str.begin() - AsmString.begin();
const unsigned *FirstTokOffset =
llvm::lower_bound(AsmTokOffsets, FirstCharOffset);
assert(*FirstTokOffset == FirstCharOffset);
unsigned FirstTokIndex = FirstTokOffset - AsmTokOffsets.begin();
FirstOrigToken = &AsmToks[FirstTokIndex];
unsigned LastCharOffset = Str.end() - AsmString.begin();
for (unsigned i = FirstTokIndex, e = AsmTokOffsets.size(); i != e; ++i) {
if (AsmTokOffsets[i] >= LastCharOffset)
break;
TempToks.push_back(AsmToks[i]);
}
}
SourceLocation
ClangAsmParserCallback::translateLocation(const llvm::SourceMgr &LSM,
llvm::SMLoc SMLoc) {
const llvm::MemoryBuffer *LBuf =
LSM.getMemoryBuffer(LSM.FindBufferContainingLoc(SMLoc));
unsigned Offset = SMLoc.getPointer() - LBuf->getBufferStart();
const unsigned *TokOffsetPtr = llvm::lower_bound(AsmTokOffsets, Offset);
unsigned TokIndex = TokOffsetPtr - AsmTokOffsets.begin();
unsigned TokOffset = *TokOffsetPtr;
SourceLocation Loc = AsmLoc;
if (TokIndex < AsmToks.size()) {
const Token &Tok = AsmToks[TokIndex];
Loc = Tok.getLocation();
Loc = Loc.getLocWithOffset(Offset - TokOffset);
}
return Loc;
}
void ClangAsmParserCallback::handleDiagnostic(const llvm::SMDiagnostic &D) {
const llvm::SourceMgr &LSM = *D.getSourceMgr();
SourceLocation Loc = translateLocation(LSM, D.getLoc());
TheParser.Diag(Loc, diag::err_inline_ms_asm_parsing) << D.getMessage();
}
ExprResult Parser::ParseMSAsmIdentifier(llvm::SmallVectorImpl<Token> &LineToks,
unsigned &NumLineToksConsumed,
bool IsUnevaluatedContext) {
const tok::TokenKind EndOfStream = tok::semi;
Token EndOfStreamTok;
EndOfStreamTok.startToken();
EndOfStreamTok.setKind(EndOfStream);
LineToks.push_back(EndOfStreamTok);
LineToks.push_back(Tok);
PP.EnterTokenStream(LineToks, true,
true);
ConsumeAnyToken();
CXXScopeSpec SS;
if (getLangOpts().CPlusPlus)
ParseOptionalCXXScopeSpecifier(SS, nullptr,
false,
false);
SourceLocation TemplateKWLoc;
UnqualifiedId Id;
bool Invalid = true;
ExprResult Result;
if (Tok.is(tok::kw_this)) {
Result = ParseCXXThis();
Invalid = false;
} else {
Invalid =
ParseUnqualifiedId(SS, nullptr,
false,
false,
false,
false,
false, &TemplateKWLoc, Id);
Result = Actions.LookupInlineAsmIdentifier(SS, TemplateKWLoc, Id,
IsUnevaluatedContext);
}
while (Result.isUsable() && Tok.is(tok::period)) {
Token IdTok = PP.LookAhead(0);
if (IdTok.isNot(tok::identifier))
break;
ConsumeToken(); IdentifierInfo *Id = Tok.getIdentifierInfo();
ConsumeToken(); Result = Actions.LookupInlineAsmVarDeclField(Result.get(), Id->getName(),
Tok.getLocation());
}
unsigned LineIndex = 0;
if (Tok.is(EndOfStream)) {
LineIndex = LineToks.size() - 2;
} else {
while (LineToks[LineIndex].getLocation() != Tok.getLocation()) {
LineIndex++;
assert(LineIndex < LineToks.size() - 2); }
}
if (Invalid || Tok.is(EndOfStream)) {
NumLineToksConsumed = LineToks.size() - 2;
} else {
NumLineToksConsumed = LineIndex;
}
for (unsigned i = 0, e = LineToks.size() - LineIndex - 2; i != e; ++i) {
ConsumeAnyToken();
}
assert(Tok.is(EndOfStream));
ConsumeToken();
LineToks.pop_back();
LineToks.pop_back();
return Result;
}
static bool buildMSAsmString(Preprocessor &PP, SourceLocation AsmLoc,
ArrayRef<Token> AsmToks,
SmallVectorImpl<unsigned> &TokOffsets,
SmallString<512> &Asm) {
assert(!AsmToks.empty() && "Didn't expect an empty AsmToks!");
bool isNewStatement = true;
for (unsigned i = 0, e = AsmToks.size(); i < e; ++i) {
const Token &Tok = AsmToks[i];
if (!isNewStatement && (Tok.is(tok::kw_asm) || Tok.isAtStartOfLine())) {
Asm += "\n\t";
isNewStatement = true;
}
if (!isNewStatement && Tok.hasLeadingSpace())
Asm += ' ';
TokOffsets.push_back(Asm.size());
if (Tok.is(tok::kw_asm)) {
if (i + 1 == e) {
PP.Diag(AsmLoc, diag::err_asm_empty);
return true;
}
continue;
}
SmallString<32> SpellingBuffer;
bool SpellingInvalid = false;
Asm += PP.getSpelling(Tok, SpellingBuffer, &SpellingInvalid);
assert(!SpellingInvalid && "spelling was invalid after correct parse?");
isNewStatement = false;
}
Asm.push_back('\0');
Asm.pop_back();
assert(TokOffsets.size() == AsmToks.size());
return false;
}
bool Parser::isGCCAsmStatement(const Token &TokAfterAsm) const {
return TokAfterAsm.is(tok::l_paren) || isGNUAsmQualifier(TokAfterAsm);
}
bool Parser::isGNUAsmQualifier(const Token &TokAfterAsm) const {
return getGNUAsmQualifier(TokAfterAsm) != GNUAsmQualifiers::AQ_unspecified;
}
StmtResult Parser::ParseMicrosoftAsmStatement(SourceLocation AsmLoc) {
SourceManager &SrcMgr = PP.getSourceManager();
SourceLocation EndLoc = AsmLoc;
SmallVector<Token, 4> AsmToks;
bool SingleLineMode = true;
unsigned BraceNesting = 0;
unsigned short savedBraceCount = BraceCount;
bool InAsmComment = false;
FileID FID;
unsigned LineNo = 0;
unsigned NumTokensRead = 0;
SmallVector<SourceLocation, 4> LBraceLocs;
bool SkippedStartOfLine = false;
if (Tok.is(tok::l_brace)) {
SingleLineMode = false;
BraceNesting = 1;
EndLoc = ConsumeBrace();
LBraceLocs.push_back(EndLoc);
++NumTokensRead;
} else {
std::pair<FileID, unsigned> ExpAsmLoc =
SrcMgr.getDecomposedExpansionLoc(EndLoc);
FID = ExpAsmLoc.first;
LineNo = SrcMgr.getLineNumber(FID, ExpAsmLoc.second);
LBraceLocs.push_back(SourceLocation());
}
SourceLocation TokLoc = Tok.getLocation();
do {
if (isEofOrEom())
break;
if (!InAsmComment && Tok.is(tok::l_brace)) {
SkippedStartOfLine = Tok.isAtStartOfLine();
AsmToks.push_back(Tok);
EndLoc = ConsumeBrace();
BraceNesting++;
LBraceLocs.push_back(EndLoc);
TokLoc = Tok.getLocation();
++NumTokensRead;
continue;
} else if (!InAsmComment && Tok.is(tok::semi)) {
InAsmComment = true;
if (!SingleLineMode) {
std::pair<FileID, unsigned> ExpSemiLoc =
SrcMgr.getDecomposedExpansionLoc(TokLoc);
FID = ExpSemiLoc.first;
LineNo = SrcMgr.getLineNumber(FID, ExpSemiLoc.second);
}
} else if (SingleLineMode || InAsmComment) {
std::pair<FileID, unsigned> ExpLoc =
SrcMgr.getDecomposedExpansionLoc(TokLoc);
if (ExpLoc.first != FID ||
SrcMgr.getLineNumber(ExpLoc.first, ExpLoc.second) != LineNo) {
bool isAsm = Tok.is(tok::kw_asm);
if (SingleLineMode && (!isAsm || isGCCAsmStatement(NextToken())))
break;
InAsmComment = false;
if (isAsm) {
if (PP.LookAhead(0).is(tok::l_brace))
break;
LineNo = SrcMgr.getLineNumber(ExpLoc.first, ExpLoc.second);
SkippedStartOfLine = Tok.isAtStartOfLine();
} else if (Tok.is(tok::semi)) {
InAsmComment = true;
FID = ExpLoc.first;
LineNo = SrcMgr.getLineNumber(FID, ExpLoc.second);
}
} else if (!InAsmComment && Tok.is(tok::r_brace)) {
if (!BraceNesting)
break;
}
}
if (!InAsmComment && BraceNesting && Tok.is(tok::r_brace) &&
BraceCount == (savedBraceCount + BraceNesting)) {
SkippedStartOfLine = Tok.isAtStartOfLine();
if (SingleLineMode || BraceNesting > 1) {
Tok.clearFlag(Token::LeadingSpace);
AsmToks.push_back(Tok);
}
EndLoc = ConsumeBrace();
BraceNesting--;
if (BraceNesting == 0 && !SingleLineMode)
break;
else {
LBraceLocs.pop_back();
TokLoc = Tok.getLocation();
++NumTokensRead;
continue;
}
}
EndLoc = TokLoc;
if (InAsmComment)
PP.Lex(Tok);
else {
if (SkippedStartOfLine)
Tok.setFlag(Token::StartOfLine);
AsmToks.push_back(Tok);
ConsumeAnyToken();
}
TokLoc = Tok.getLocation();
++NumTokensRead;
SkippedStartOfLine = false;
} while (true);
if (BraceNesting && BraceCount != savedBraceCount) {
for (unsigned i = 0; i < BraceNesting; ++i) {
Diag(Tok, diag::err_expected) << tok::r_brace;
Diag(LBraceLocs.back(), diag::note_matching) << tok::l_brace;
LBraceLocs.pop_back();
}
return StmtError();
} else if (NumTokensRead == 0) {
Diag(Tok, diag::err_expected) << tok::l_brace;
return StmtError();
}
SmallVector<StringRef, 4> ConstraintRefs;
SmallVector<Expr *, 4> Exprs;
SmallVector<StringRef, 4> ClobberRefs;
const llvm::Triple &TheTriple = Actions.Context.getTargetInfo().getTriple();
const std::string &TT = TheTriple.getTriple();
const llvm::Target *TheTarget = nullptr;
if (!TheTriple.isX86()) {
Diag(AsmLoc, diag::err_msasm_unsupported_arch) << TheTriple.getArchName();
} else {
std::string Error;
TheTarget = llvm::TargetRegistry::lookupTarget(TT, Error);
if (!TheTarget)
Diag(AsmLoc, diag::err_msasm_unable_to_create_target) << Error;
}
assert(!LBraceLocs.empty() && "Should have at least one location here");
SmallString<512> AsmString;
auto EmptyStmt = [&] {
return Actions.ActOnMSAsmStmt(AsmLoc, LBraceLocs[0], AsmToks, AsmString,
0, 0,
ConstraintRefs, ClobberRefs, Exprs, EndLoc);
};
if (!TheTarget || AsmToks.empty()) {
return EmptyStmt();
}
SmallVector<unsigned, 8> TokOffsets;
if (buildMSAsmString(PP, AsmLoc, AsmToks, TokOffsets, AsmString))
return StmtError();
const TargetOptions &TO = Actions.Context.getTargetInfo().getTargetOpts();
std::string FeaturesStr =
llvm::join(TO.Features.begin(), TO.Features.end(), ",");
std::unique_ptr<llvm::MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TT));
if (!MRI) {
Diag(AsmLoc, diag::err_msasm_unable_to_create_target)
<< "target MC unavailable";
return EmptyStmt();
}
llvm::MCTargetOptions MCOptions;
std::unique_ptr<llvm::MCAsmInfo> MAI(
TheTarget->createMCAsmInfo(*MRI, TT, MCOptions));
std::unique_ptr<llvm::MCInstrInfo> MII(TheTarget->createMCInstrInfo());
std::unique_ptr<llvm::MCSubtargetInfo> STI(
TheTarget->createMCSubtargetInfo(TT, TO.CPU, FeaturesStr));
if (!MAI || !MII || !STI) {
Diag(AsmLoc, diag::err_msasm_unable_to_create_target)
<< "target MC unavailable";
return EmptyStmt();
}
llvm::SourceMgr TempSrcMgr;
llvm::MCContext Ctx(TheTriple, MAI.get(), MRI.get(), STI.get(), &TempSrcMgr);
std::unique_ptr<llvm::MCObjectFileInfo> MOFI(
TheTarget->createMCObjectFileInfo(Ctx, false));
Ctx.setObjectFileInfo(MOFI.get());
std::unique_ptr<llvm::MemoryBuffer> Buffer =
llvm::MemoryBuffer::getMemBuffer(AsmString, "<MS inline asm>");
TempSrcMgr.AddNewSourceBuffer(std::move(Buffer), llvm::SMLoc());
std::unique_ptr<llvm::MCStreamer> Str(createNullStreamer(Ctx));
std::unique_ptr<llvm::MCAsmParser> Parser(
createMCAsmParser(TempSrcMgr, Ctx, *Str.get(), *MAI));
std::unique_ptr<llvm::MCTargetAsmParser> TargetParser(
TheTarget->createMCAsmParser(*STI, *Parser, *MII, MCOptions));
if (!TargetParser) {
Diag(AsmLoc, diag::err_msasm_unable_to_create_target)
<< "target ASM parser unavailable";
return EmptyStmt();
}
std::unique_ptr<llvm::MCInstPrinter> IP(
TheTarget->createMCInstPrinter(llvm::Triple(TT), 1, *MAI, *MII, *MRI));
Parser->setAssemblerDialect(1);
Parser->setTargetParser(*TargetParser.get());
Parser->setParsingMSInlineAsm(true);
TargetParser->setParsingMSInlineAsm(true);
ClangAsmParserCallback Callback(*this, AsmLoc, AsmString, AsmToks,
TokOffsets);
TargetParser->setSemaCallback(&Callback);
TempSrcMgr.setDiagHandler(ClangAsmParserCallback::DiagHandlerCallback,
&Callback);
unsigned NumOutputs;
unsigned NumInputs;
std::string AsmStringIR;
SmallVector<std::pair<void *, bool>, 4> OpExprs;
SmallVector<std::string, 4> Constraints;
SmallVector<std::string, 4> Clobbers;
if (Parser->parseMSInlineAsm(AsmStringIR, NumOutputs, NumInputs, OpExprs,
Constraints, Clobbers, MII.get(), IP.get(),
Callback))
return StmtError();
llvm::erase_if(Clobbers, [](const std::string &C) {
return C == "fpsr" || C == "mxcsr";
});
ClobberRefs.insert(ClobberRefs.end(), Clobbers.begin(), Clobbers.end());
unsigned NumExprs = NumOutputs + NumInputs;
ConstraintRefs.resize(NumExprs);
Exprs.resize(NumExprs);
for (unsigned i = 0, e = NumExprs; i != e; ++i) {
Expr *OpExpr = static_cast<Expr *>(OpExprs[i].first);
if (!OpExpr)
return StmtError();
if (OpExprs[i].second)
OpExpr =
Actions.BuildUnaryOp(getCurScope(), AsmLoc, UO_AddrOf, OpExpr).get();
ConstraintRefs[i] = StringRef(Constraints[i]);
Exprs[i] = OpExpr;
}
return Actions.ActOnMSAsmStmt(AsmLoc, LBraceLocs[0], AsmToks, AsmStringIR,
NumOutputs, NumInputs, ConstraintRefs,
ClobberRefs, Exprs, EndLoc);
}
bool Parser::parseGNUAsmQualifierListOpt(GNUAsmQualifiers &AQ) {
while (true) {
const GNUAsmQualifiers::AQ A = getGNUAsmQualifier(Tok);
if (A == GNUAsmQualifiers::AQ_unspecified) {
if (Tok.isNot(tok::l_paren)) {
Diag(Tok.getLocation(), diag::err_asm_qualifier_ignored);
SkipUntil(tok::r_paren, StopAtSemi);
return true;
}
return false;
}
if (AQ.setAsmQualifier(A))
Diag(Tok.getLocation(), diag::err_asm_duplicate_qual)
<< GNUAsmQualifiers::getQualifierName(A);
ConsumeToken();
}
return false;
}
StmtResult Parser::ParseAsmStatement(bool &msAsm) {
assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
SourceLocation AsmLoc = ConsumeToken();
if (getLangOpts().AsmBlocks && !isGCCAsmStatement(Tok)) {
msAsm = true;
return ParseMicrosoftAsmStatement(AsmLoc);
}
SourceLocation Loc = Tok.getLocation();
GNUAsmQualifiers GAQ;
if (parseGNUAsmQualifierListOpt(GAQ))
return StmtError();
if (GAQ.isGoto() && getLangOpts().SpeculativeLoadHardening)
Diag(Loc, diag::warn_slh_does_not_support_asm_goto);
BalancedDelimiterTracker T(*this, tok::l_paren);
T.consumeOpen();
ExprResult AsmString(ParseAsmStringLiteral( false));
if (!(getLangOpts().GNUAsm || AsmString.isInvalid())) {
const auto *SL = cast<StringLiteral>(AsmString.get());
if (!SL->getString().trim().empty())
Diag(Loc, diag::err_gnu_inline_asm_disabled);
}
if (AsmString.isInvalid()) {
T.skipToEnd();
return StmtError();
}
SmallVector<IdentifierInfo *, 4> Names;
ExprVector Constraints;
ExprVector Exprs;
ExprVector Clobbers;
if (Tok.is(tok::r_paren)) {
T.consumeClose();
return Actions.ActOnGCCAsmStmt(
AsmLoc, true, GAQ.isVolatile(),
0, 0, nullptr, Constraints, Exprs,
AsmString.get(), Clobbers, 0, T.getCloseLocation());
}
bool AteExtraColon = false;
if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
AteExtraColon = Tok.is(tok::coloncolon);
ConsumeToken();
if (!AteExtraColon && ParseAsmOperandsOpt(Names, Constraints, Exprs))
return StmtError();
}
unsigned NumOutputs = Names.size();
if (AteExtraColon || Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
if (AteExtraColon)
AteExtraColon = false;
else {
AteExtraColon = Tok.is(tok::coloncolon);
ConsumeToken();
}
if (!AteExtraColon && ParseAsmOperandsOpt(Names, Constraints, Exprs))
return StmtError();
}
assert(Names.size() == Constraints.size() &&
Constraints.size() == Exprs.size() && "Input operand size mismatch!");
unsigned NumInputs = Names.size() - NumOutputs;
if (AteExtraColon || Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
if (AteExtraColon)
AteExtraColon = false;
else {
AteExtraColon = Tok.is(tok::coloncolon);
ConsumeToken();
}
if (!AteExtraColon && isTokenStringLiteral()) {
while (true) {
ExprResult Clobber(ParseAsmStringLiteral( false));
if (Clobber.isInvalid())
break;
Clobbers.push_back(Clobber.get());
if (!TryConsumeToken(tok::comma))
break;
}
}
}
if (!GAQ.isGoto() && (Tok.isNot(tok::r_paren) || AteExtraColon)) {
Diag(Tok, diag::err_expected) << tok::r_paren;
SkipUntil(tok::r_paren, StopAtSemi);
return StmtError();
}
unsigned NumLabels = 0;
if (AteExtraColon || Tok.is(tok::colon)) {
if (!AteExtraColon)
ConsumeToken();
while (true) {
if (Tok.isNot(tok::identifier)) {
Diag(Tok, diag::err_expected) << tok::identifier;
SkipUntil(tok::r_paren, StopAtSemi);
return StmtError();
}
LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
Tok.getLocation());
Names.push_back(Tok.getIdentifierInfo());
if (!LD) {
SkipUntil(tok::r_paren, StopAtSemi);
return StmtError();
}
ExprResult Res =
Actions.ActOnAddrLabel(Tok.getLocation(), Tok.getLocation(), LD);
Exprs.push_back(Res.get());
NumLabels++;
ConsumeToken();
if (!TryConsumeToken(tok::comma))
break;
}
} else if (GAQ.isGoto()) {
Diag(Tok, diag::err_expected) << tok::colon;
SkipUntil(tok::r_paren, StopAtSemi);
return StmtError();
}
T.consumeClose();
return Actions.ActOnGCCAsmStmt(AsmLoc, false, GAQ.isVolatile(), NumOutputs,
NumInputs, Names.data(), Constraints, Exprs,
AsmString.get(), Clobbers, NumLabels,
T.getCloseLocation());
}
bool Parser::ParseAsmOperandsOpt(SmallVectorImpl<IdentifierInfo *> &Names,
SmallVectorImpl<Expr *> &Constraints,
SmallVectorImpl<Expr *> &Exprs) {
if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
return false;
while (true) {
if (Tok.is(tok::l_square)) {
BalancedDelimiterTracker T(*this, tok::l_square);
T.consumeOpen();
if (Tok.isNot(tok::identifier)) {
Diag(Tok, diag::err_expected) << tok::identifier;
SkipUntil(tok::r_paren, StopAtSemi);
return true;
}
IdentifierInfo *II = Tok.getIdentifierInfo();
ConsumeToken();
Names.push_back(II);
T.consumeClose();
} else
Names.push_back(nullptr);
ExprResult Constraint(ParseAsmStringLiteral( false));
if (Constraint.isInvalid()) {
SkipUntil(tok::r_paren, StopAtSemi);
return true;
}
Constraints.push_back(Constraint.get());
if (Tok.isNot(tok::l_paren)) {
Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
SkipUntil(tok::r_paren, StopAtSemi);
return true;
}
BalancedDelimiterTracker T(*this, tok::l_paren);
T.consumeOpen();
ExprResult Res = Actions.CorrectDelayedTyposInExpr(ParseExpression());
T.consumeClose();
if (Res.isInvalid()) {
SkipUntil(tok::r_paren, StopAtSemi);
return true;
}
Exprs.push_back(Res.get());
if (!TryConsumeToken(tok::comma))
return false;
}
}
const char *Parser::GNUAsmQualifiers::getQualifierName(AQ Qualifier) {
switch (Qualifier) {
case AQ_volatile: return "volatile";
case AQ_inline: return "inline";
case AQ_goto: return "goto";
case AQ_unspecified: return "unspecified";
}
llvm_unreachable("Unknown GNUAsmQualifier");
}
Parser::GNUAsmQualifiers::AQ
Parser::getGNUAsmQualifier(const Token &Tok) const {
switch (Tok.getKind()) {
case tok::kw_volatile: return GNUAsmQualifiers::AQ_volatile;
case tok::kw_inline: return GNUAsmQualifiers::AQ_inline;
case tok::kw_goto: return GNUAsmQualifiers::AQ_goto;
default: return GNUAsmQualifiers::AQ_unspecified;
}
}
bool Parser::GNUAsmQualifiers::setAsmQualifier(AQ Qualifier) {
bool IsDuplicate = Qualifiers & Qualifier;
Qualifiers |= Qualifier;
return IsDuplicate;
}