blob: 75f3ac396e1a4320b632a0925317d26ef9de804f [file] [log] [blame]
Alp Toker1b935a82014-06-08 05:40:04 +00001//===---- ParseStmtAsm.cpp - Assembly Statement Parser --------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Alp Toker1b935a82014-06-08 05:40:04 +00006//
7//===----------------------------------------------------------------------===//
8//
Fangrui Song6907ce22018-07-30 19:24:48 +00009// This file implements parsing for GCC and Microsoft inline assembly.
Alp Toker1b935a82014-06-08 05:40:04 +000010//
11//===----------------------------------------------------------------------===//
12
13#include "clang/Parse/Parser.h"
Alp Toker1b935a82014-06-08 05:40:04 +000014#include "clang/AST/ASTContext.h"
15#include "clang/Basic/Diagnostic.h"
16#include "clang/Basic/TargetInfo.h"
Vassil Vassilev11ad3392017-03-23 15:11:07 +000017#include "clang/Parse/RAIIObjectsForParser.h"
Alp Toker1b935a82014-06-08 05:40:04 +000018#include "llvm/ADT/SmallString.h"
Marina Yatsina41c45fa2016-02-03 11:32:08 +000019#include "llvm/ADT/StringExtras.h"
Alp Toker1b935a82014-06-08 05:40:04 +000020#include "llvm/MC/MCAsmInfo.h"
21#include "llvm/MC/MCContext.h"
22#include "llvm/MC/MCInstPrinter.h"
23#include "llvm/MC/MCInstrInfo.h"
24#include "llvm/MC/MCObjectFileInfo.h"
25#include "llvm/MC/MCParser/MCAsmParser.h"
Benjamin Kramer5e456302016-01-27 10:01:30 +000026#include "llvm/MC/MCParser/MCTargetAsmParser.h"
Alp Toker1b935a82014-06-08 05:40:04 +000027#include "llvm/MC/MCRegisterInfo.h"
28#include "llvm/MC/MCStreamer.h"
29#include "llvm/MC/MCSubtargetInfo.h"
Alp Toker1b935a82014-06-08 05:40:04 +000030#include "llvm/MC/MCTargetOptions.h"
31#include "llvm/Support/SourceMgr.h"
32#include "llvm/Support/TargetRegistry.h"
33#include "llvm/Support/TargetSelect.h"
34using namespace clang;
35
36namespace {
37class ClangAsmParserCallback : public llvm::MCAsmParserSemaCallback {
38 Parser &TheParser;
39 SourceLocation AsmLoc;
40 StringRef AsmString;
41
42 /// The tokens we streamed into AsmString and handed off to MC.
43 ArrayRef<Token> AsmToks;
44
45 /// The offset of each token in AsmToks within AsmString.
46 ArrayRef<unsigned> AsmTokOffsets;
47
48public:
49 ClangAsmParserCallback(Parser &P, SourceLocation Loc, StringRef AsmString,
50 ArrayRef<Token> Toks, ArrayRef<unsigned> Offsets)
51 : TheParser(P), AsmLoc(Loc), AsmString(AsmString), AsmToks(Toks),
52 AsmTokOffsets(Offsets) {
53 assert(AsmToks.size() == AsmTokOffsets.size());
54 }
55
Coby Tayree61504192017-09-29 07:02:49 +000056 void LookupInlineAsmIdentifier(StringRef &LineBuf,
57 llvm::InlineAsmIdentifierInfo &Info,
Reid Klecknercc087f62017-10-26 17:07:48 +000058 bool IsUnevaluatedContext) override;
Alp Toker1b935a82014-06-08 05:40:04 +000059
Ehsan Akhgari31097582014-09-22 02:21:54 +000060 StringRef LookupInlineAsmLabel(StringRef Identifier, llvm::SourceMgr &LSM,
61 llvm::SMLoc Location,
Reid Klecknercc087f62017-10-26 17:07:48 +000062 bool Create) override;
Ehsan Akhgari31097582014-09-22 02:21:54 +000063
Alp Toker1b935a82014-06-08 05:40:04 +000064 bool LookupInlineAsmField(StringRef Base, StringRef Member,
65 unsigned &Offset) override {
66 return TheParser.getActions().LookupInlineAsmField(Base, Member, Offset,
67 AsmLoc);
68 }
69
70 static void DiagHandlerCallback(const llvm::SMDiagnostic &D, void *Context) {
71 ((ClangAsmParserCallback *)Context)->handleDiagnostic(D);
72 }
73
74private:
75 /// Collect the appropriate tokens for the given string.
76 void findTokensForString(StringRef Str, SmallVectorImpl<Token> &TempToks,
Reid Klecknercc087f62017-10-26 17:07:48 +000077 const Token *&FirstOrigToken) const;
Alp Toker1b935a82014-06-08 05:40:04 +000078
Reid Klecknercc087f62017-10-26 17:07:48 +000079 SourceLocation translateLocation(const llvm::SourceMgr &LSM,
80 llvm::SMLoc SMLoc);
Alp Toker1b935a82014-06-08 05:40:04 +000081
Reid Klecknercc087f62017-10-26 17:07:48 +000082 void handleDiagnostic(const llvm::SMDiagnostic &D);
Alp Toker1b935a82014-06-08 05:40:04 +000083};
Alexander Kornienkoab9db512015-06-22 23:07:51 +000084}
Alp Toker1b935a82014-06-08 05:40:04 +000085
Reid Klecknercc087f62017-10-26 17:07:48 +000086void ClangAsmParserCallback::LookupInlineAsmIdentifier(
87 StringRef &LineBuf, llvm::InlineAsmIdentifierInfo &Info,
88 bool IsUnevaluatedContext) {
89 // Collect the desired tokens.
90 SmallVector<Token, 16> LineToks;
91 const Token *FirstOrigToken = nullptr;
92 findTokensForString(LineBuf, LineToks, FirstOrigToken);
93
94 unsigned NumConsumedToks;
95 ExprResult Result = TheParser.ParseMSAsmIdentifier(LineToks, NumConsumedToks,
96 IsUnevaluatedContext);
97
98 // If we consumed the entire line, tell MC that.
99 // Also do this if we consumed nothing as a way of reporting failure.
100 if (NumConsumedToks == 0 || NumConsumedToks == LineToks.size()) {
101 // By not modifying LineBuf, we're implicitly consuming it all.
102
103 // Otherwise, consume up to the original tokens.
104 } else {
105 assert(FirstOrigToken && "not using original tokens?");
106
107 // Since we're using original tokens, apply that offset.
108 assert(FirstOrigToken[NumConsumedToks].getLocation() ==
109 LineToks[NumConsumedToks].getLocation());
110 unsigned FirstIndex = FirstOrigToken - AsmToks.begin();
111 unsigned LastIndex = FirstIndex + NumConsumedToks - 1;
112
113 // The total length we've consumed is the relative offset
114 // of the last token we consumed plus its length.
115 unsigned TotalOffset =
116 (AsmTokOffsets[LastIndex] + AsmToks[LastIndex].getLength() -
117 AsmTokOffsets[FirstIndex]);
118 LineBuf = LineBuf.substr(0, TotalOffset);
119 }
120
121 // Initialize Info with the lookup result.
122 if (!Result.isUsable())
123 return;
124 TheParser.getActions().FillInlineAsmIdentifierInfo(Result.get(), Info);
125}
126
127StringRef ClangAsmParserCallback::LookupInlineAsmLabel(StringRef Identifier,
128 llvm::SourceMgr &LSM,
129 llvm::SMLoc Location,
130 bool Create) {
131 SourceLocation Loc = translateLocation(LSM, Location);
132 LabelDecl *Label =
133 TheParser.getActions().GetOrCreateMSAsmLabel(Identifier, Loc, Create);
134 return Label->getMSAsmLabel();
135}
136
137void ClangAsmParserCallback::findTokensForString(
138 StringRef Str, SmallVectorImpl<Token> &TempToks,
139 const Token *&FirstOrigToken) const {
140 // For now, assert that the string we're working with is a substring
141 // of what we gave to MC. This lets us use the original tokens.
142 assert(!std::less<const char *>()(Str.begin(), AsmString.begin()) &&
143 !std::less<const char *>()(AsmString.end(), Str.end()));
144
145 // Try to find a token whose offset matches the first token.
146 unsigned FirstCharOffset = Str.begin() - AsmString.begin();
147 const unsigned *FirstTokOffset = std::lower_bound(
148 AsmTokOffsets.begin(), AsmTokOffsets.end(), FirstCharOffset);
149
150 // For now, assert that the start of the string exactly
151 // corresponds to the start of a token.
152 assert(*FirstTokOffset == FirstCharOffset);
153
154 // Use all the original tokens for this line. (We assume the
155 // end of the line corresponds cleanly to a token break.)
156 unsigned FirstTokIndex = FirstTokOffset - AsmTokOffsets.begin();
157 FirstOrigToken = &AsmToks[FirstTokIndex];
158 unsigned LastCharOffset = Str.end() - AsmString.begin();
159 for (unsigned i = FirstTokIndex, e = AsmTokOffsets.size(); i != e; ++i) {
160 if (AsmTokOffsets[i] >= LastCharOffset)
161 break;
162 TempToks.push_back(AsmToks[i]);
163 }
164}
165
166SourceLocation
167ClangAsmParserCallback::translateLocation(const llvm::SourceMgr &LSM,
168 llvm::SMLoc SMLoc) {
169 // Compute an offset into the inline asm buffer.
170 // FIXME: This isn't right if .macro is involved (but hopefully, no
171 // real-world code does that).
172 const llvm::MemoryBuffer *LBuf =
173 LSM.getMemoryBuffer(LSM.FindBufferContainingLoc(SMLoc));
174 unsigned Offset = SMLoc.getPointer() - LBuf->getBufferStart();
175
176 // Figure out which token that offset points into.
177 const unsigned *TokOffsetPtr =
178 std::lower_bound(AsmTokOffsets.begin(), AsmTokOffsets.end(), Offset);
179 unsigned TokIndex = TokOffsetPtr - AsmTokOffsets.begin();
180 unsigned TokOffset = *TokOffsetPtr;
181
182 // If we come up with an answer which seems sane, use it; otherwise,
183 // just point at the __asm keyword.
184 // FIXME: Assert the answer is sane once we handle .macro correctly.
185 SourceLocation Loc = AsmLoc;
186 if (TokIndex < AsmToks.size()) {
187 const Token &Tok = AsmToks[TokIndex];
188 Loc = Tok.getLocation();
189 Loc = Loc.getLocWithOffset(Offset - TokOffset);
190 }
191 return Loc;
192}
193
194void ClangAsmParserCallback::handleDiagnostic(const llvm::SMDiagnostic &D) {
195 const llvm::SourceMgr &LSM = *D.getSourceMgr();
196 SourceLocation Loc = translateLocation(LSM, D.getLoc());
197 TheParser.Diag(Loc, diag::err_inline_ms_asm_parsing) << D.getMessage();
198}
199
Alp Toker1b935a82014-06-08 05:40:04 +0000200/// Parse an identifier in an MS-style inline assembly block.
Alp Toker1b935a82014-06-08 05:40:04 +0000201ExprResult Parser::ParseMSAsmIdentifier(llvm::SmallVectorImpl<Token> &LineToks,
202 unsigned &NumLineToksConsumed,
Alp Toker1b935a82014-06-08 05:40:04 +0000203 bool IsUnevaluatedContext) {
Alp Toker1b935a82014-06-08 05:40:04 +0000204 // Push a fake token on the end so that we don't overrun the token
205 // stream. We use ';' because it expression-parsing should never
206 // overrun it.
207 const tok::TokenKind EndOfStream = tok::semi;
208 Token EndOfStreamTok;
209 EndOfStreamTok.startToken();
210 EndOfStreamTok.setKind(EndOfStream);
211 LineToks.push_back(EndOfStreamTok);
212
213 // Also copy the current token over.
214 LineToks.push_back(Tok);
215
Ilya Biryukov929af672019-05-17 09:32:05 +0000216 PP.EnterTokenStream(LineToks, /*DisableMacroExpansions*/ true,
217 /*IsReinject*/ true);
Alp Toker1b935a82014-06-08 05:40:04 +0000218
219 // Clear the current token and advance to the first token in LineToks.
220 ConsumeAnyToken();
221
222 // Parse an optional scope-specifier if we're in C++.
223 CXXScopeSpec SS;
224 if (getLangOpts().CPlusPlus) {
David Blaikieefdccaa2016-01-15 23:43:34 +0000225 ParseOptionalCXXScopeSpecifier(SS, nullptr, /*EnteringContext=*/false);
Alp Toker1b935a82014-06-08 05:40:04 +0000226 }
227
228 // Require an identifier here.
229 SourceLocation TemplateKWLoc;
230 UnqualifiedId Id;
Michael Zuckerman229158c2015-12-15 14:04:18 +0000231 bool Invalid = true;
232 ExprResult Result;
233 if (Tok.is(tok::kw_this)) {
234 Result = ParseCXXThis();
235 Invalid = false;
236 } else {
David Blaikieefdccaa2016-01-15 23:43:34 +0000237 Invalid = ParseUnqualifiedId(SS,
238 /*EnteringContext=*/false,
239 /*AllowDestructorName=*/false,
240 /*AllowConstructorName=*/false,
Richard Smith35845152017-02-07 01:37:30 +0000241 /*AllowDeductionGuide=*/false,
Richard Smithc08b6932018-04-27 02:00:13 +0000242 /*ObjectType=*/nullptr, &TemplateKWLoc, Id);
Michael Zuckerman229158c2015-12-15 14:04:18 +0000243 // Perform the lookup.
Coby Tayree61504192017-09-29 07:02:49 +0000244 Result = Actions.LookupInlineAsmIdentifier(SS, TemplateKWLoc, Id,
Michael Zuckerman229158c2015-12-15 14:04:18 +0000245 IsUnevaluatedContext);
246 }
Reid Kleckner14e96b42015-08-26 21:57:20 +0000247 // While the next two tokens are 'period' 'identifier', repeatedly parse it as
248 // a field access. We have to avoid consuming assembler directives that look
249 // like '.' 'else'.
250 while (Result.isUsable() && Tok.is(tok::period)) {
251 Token IdTok = PP.LookAhead(0);
252 if (IdTok.isNot(tok::identifier))
253 break;
254 ConsumeToken(); // Consume the period.
255 IdentifierInfo *Id = Tok.getIdentifierInfo();
256 ConsumeToken(); // Consume the identifier.
David Majnemer758e7982016-01-05 00:08:41 +0000257 Result = Actions.LookupInlineAsmVarDeclField(Result.get(), Id->getName(),
Coby Tayree61504192017-09-29 07:02:49 +0000258 Tok.getLocation());
Reid Kleckner14e96b42015-08-26 21:57:20 +0000259 }
260
Alp Toker1b935a82014-06-08 05:40:04 +0000261 // Figure out how many tokens we are into LineToks.
262 unsigned LineIndex = 0;
263 if (Tok.is(EndOfStream)) {
264 LineIndex = LineToks.size() - 2;
265 } else {
266 while (LineToks[LineIndex].getLocation() != Tok.getLocation()) {
267 LineIndex++;
268 assert(LineIndex < LineToks.size() - 2); // we added two extra tokens
269 }
270 }
271
272 // If we've run into the poison token we inserted before, or there
273 // was a parsing error, then claim the entire line.
274 if (Invalid || Tok.is(EndOfStream)) {
275 NumLineToksConsumed = LineToks.size() - 2;
276 } else {
277 // Otherwise, claim up to the start of the next token.
278 NumLineToksConsumed = LineIndex;
279 }
280
281 // Finally, restore the old parsing state by consuming all the tokens we
282 // staged before, implicitly killing off the token-lexer we pushed.
283 for (unsigned i = 0, e = LineToks.size() - LineIndex - 2; i != e; ++i) {
284 ConsumeAnyToken();
285 }
286 assert(Tok.is(EndOfStream));
287 ConsumeToken();
288
289 // Leave LineToks in its original state.
290 LineToks.pop_back();
291 LineToks.pop_back();
292
Reid Kleckner14e96b42015-08-26 21:57:20 +0000293 return Result;
Alp Toker1b935a82014-06-08 05:40:04 +0000294}
295
296/// Turn a sequence of our tokens back into a string that we can hand
297/// to the MC asm parser.
298static bool buildMSAsmString(Preprocessor &PP, SourceLocation AsmLoc,
299 ArrayRef<Token> AsmToks,
300 SmallVectorImpl<unsigned> &TokOffsets,
301 SmallString<512> &Asm) {
302 assert(!AsmToks.empty() && "Didn't expect an empty AsmToks!");
303
304 // Is this the start of a new assembly statement?
305 bool isNewStatement = true;
306
307 for (unsigned i = 0, e = AsmToks.size(); i < e; ++i) {
308 const Token &Tok = AsmToks[i];
309
310 // Start each new statement with a newline and a tab.
311 if (!isNewStatement && (Tok.is(tok::kw_asm) || Tok.isAtStartOfLine())) {
312 Asm += "\n\t";
313 isNewStatement = true;
314 }
315
316 // Preserve the existence of leading whitespace except at the
317 // start of a statement.
318 if (!isNewStatement && Tok.hasLeadingSpace())
319 Asm += ' ';
320
321 // Remember the offset of this token.
322 TokOffsets.push_back(Asm.size());
323
324 // Don't actually write '__asm' into the assembly stream.
325 if (Tok.is(tok::kw_asm)) {
326 // Complain about __asm at the end of the stream.
327 if (i + 1 == e) {
328 PP.Diag(AsmLoc, diag::err_asm_empty);
329 return true;
330 }
331
332 continue;
333 }
334
335 // Append the spelling of the token.
336 SmallString<32> SpellingBuffer;
337 bool SpellingInvalid = false;
338 Asm += PP.getSpelling(Tok, SpellingBuffer, &SpellingInvalid);
339 assert(!SpellingInvalid && "spelling was invalid after correct parse?");
340
341 // We are no longer at the start of a statement.
342 isNewStatement = false;
343 }
344
345 // Ensure that the buffer is null-terminated.
346 Asm.push_back('\0');
347 Asm.pop_back();
348
349 assert(TokOffsets.size() == AsmToks.size());
350 return false;
351}
352
Denis Zobnin628b0222016-04-21 10:59:18 +0000353/// isTypeQualifier - Return true if the current token could be the
354/// start of a type-qualifier-list.
355static bool isTypeQualifier(const Token &Tok) {
356 switch (Tok.getKind()) {
357 default: return false;
358 // type-qualifier
359 case tok::kw_const:
360 case tok::kw_volatile:
361 case tok::kw_restrict:
362 case tok::kw___private:
363 case tok::kw___local:
364 case tok::kw___global:
365 case tok::kw___constant:
366 case tok::kw___generic:
367 case tok::kw___read_only:
368 case tok::kw___read_write:
369 case tok::kw___write_only:
370 return true;
371 }
372}
373
374// Determine if this is a GCC-style asm statement.
375static bool isGCCAsmStatement(const Token &TokAfterAsm) {
376 return TokAfterAsm.is(tok::l_paren) || TokAfterAsm.is(tok::kw_goto) ||
377 isTypeQualifier(TokAfterAsm);
378}
379
Alp Toker1b935a82014-06-08 05:40:04 +0000380/// ParseMicrosoftAsmStatement. When -fms-extensions/-fasm-blocks is enabled,
381/// this routine is called to collect the tokens for an MS asm statement.
382///
383/// [MS] ms-asm-statement:
384/// ms-asm-block
385/// ms-asm-block ms-asm-statement
386///
387/// [MS] ms-asm-block:
388/// '__asm' ms-asm-line '\n'
389/// '__asm' '{' ms-asm-instruction-block[opt] '}' ';'[opt]
390///
391/// [MS] ms-asm-instruction-block
392/// ms-asm-line
393/// ms-asm-line '\n' ms-asm-instruction-block
394///
395StmtResult Parser::ParseMicrosoftAsmStatement(SourceLocation AsmLoc) {
396 SourceManager &SrcMgr = PP.getSourceManager();
397 SourceLocation EndLoc = AsmLoc;
398 SmallVector<Token, 4> AsmToks;
399
Ehsan Akhgari2f93b442014-07-25 02:27:14 +0000400 bool SingleLineMode = true;
Ehsan Akhgari0f89fac2014-07-06 05:26:54 +0000401 unsigned BraceNesting = 0;
Ehsan Akhgari833ed942014-07-15 02:21:41 +0000402 unsigned short savedBraceCount = BraceCount;
Alp Toker1b935a82014-06-08 05:40:04 +0000403 bool InAsmComment = false;
404 FileID FID;
405 unsigned LineNo = 0;
406 unsigned NumTokensRead = 0;
Ehsan Akhgari0f89fac2014-07-06 05:26:54 +0000407 SmallVector<SourceLocation, 4> LBraceLocs;
408 bool SkippedStartOfLine = false;
Alp Toker1b935a82014-06-08 05:40:04 +0000409
410 if (Tok.is(tok::l_brace)) {
411 // Braced inline asm: consume the opening brace.
Ehsan Akhgari2f93b442014-07-25 02:27:14 +0000412 SingleLineMode = false;
Ehsan Akhgari0f89fac2014-07-06 05:26:54 +0000413 BraceNesting = 1;
Ehsan Akhgari0f89fac2014-07-06 05:26:54 +0000414 EndLoc = ConsumeBrace();
415 LBraceLocs.push_back(EndLoc);
Alp Toker1b935a82014-06-08 05:40:04 +0000416 ++NumTokensRead;
417 } else {
418 // Single-line inline asm; compute which line it is on.
419 std::pair<FileID, unsigned> ExpAsmLoc =
420 SrcMgr.getDecomposedExpansionLoc(EndLoc);
421 FID = ExpAsmLoc.first;
422 LineNo = SrcMgr.getLineNumber(FID, ExpAsmLoc.second);
Ehsan Akhgari0f89fac2014-07-06 05:26:54 +0000423 LBraceLocs.push_back(SourceLocation());
Alp Toker1b935a82014-06-08 05:40:04 +0000424 }
425
426 SourceLocation TokLoc = Tok.getLocation();
427 do {
428 // If we hit EOF, we're done, period.
429 if (isEofOrEom())
430 break;
431
Ehsan Akhgari0f89fac2014-07-06 05:26:54 +0000432 if (!InAsmComment && Tok.is(tok::l_brace)) {
433 // Consume the opening brace.
434 SkippedStartOfLine = Tok.isAtStartOfLine();
Marina Yatsina5f776792016-03-07 18:10:25 +0000435 AsmToks.push_back(Tok);
Ehsan Akhgari0f89fac2014-07-06 05:26:54 +0000436 EndLoc = ConsumeBrace();
437 BraceNesting++;
438 LBraceLocs.push_back(EndLoc);
439 TokLoc = Tok.getLocation();
440 ++NumTokensRead;
441 continue;
442 } else if (!InAsmComment && Tok.is(tok::semi)) {
Alp Toker1b935a82014-06-08 05:40:04 +0000443 // A semicolon in an asm is the start of a comment.
444 InAsmComment = true;
Ehsan Akhgari2f93b442014-07-25 02:27:14 +0000445 if (!SingleLineMode) {
Alp Toker1b935a82014-06-08 05:40:04 +0000446 // Compute which line the comment is on.
447 std::pair<FileID, unsigned> ExpSemiLoc =
448 SrcMgr.getDecomposedExpansionLoc(TokLoc);
449 FID = ExpSemiLoc.first;
450 LineNo = SrcMgr.getLineNumber(FID, ExpSemiLoc.second);
451 }
Ehsan Akhgari2f93b442014-07-25 02:27:14 +0000452 } else if (SingleLineMode || InAsmComment) {
Alp Toker1b935a82014-06-08 05:40:04 +0000453 // If end-of-line is significant, check whether this token is on a
454 // new line.
455 std::pair<FileID, unsigned> ExpLoc =
456 SrcMgr.getDecomposedExpansionLoc(TokLoc);
457 if (ExpLoc.first != FID ||
458 SrcMgr.getLineNumber(ExpLoc.first, ExpLoc.second) != LineNo) {
Ehsan Akhgari2f93b442014-07-25 02:27:14 +0000459 // If this is a single-line __asm, we're done, except if the next
Denis Zobnin628b0222016-04-21 10:59:18 +0000460 // line is MS-style asm too, in which case we finish a comment
Ehsan Akhgari2f93b442014-07-25 02:27:14 +0000461 // if needed and then keep processing the next line as a single
462 // line __asm.
463 bool isAsm = Tok.is(tok::kw_asm);
Denis Zobnin628b0222016-04-21 10:59:18 +0000464 if (SingleLineMode && (!isAsm || isGCCAsmStatement(NextToken())))
Alp Toker1b935a82014-06-08 05:40:04 +0000465 break;
466 // We're no longer in a comment.
467 InAsmComment = false;
Ehsan Akhgari2f93b442014-07-25 02:27:14 +0000468 if (isAsm) {
Simon Pilgrim2c518802017-03-30 14:13:19 +0000469 // If this is a new __asm {} block we want to process it separately
Marina Yatsina146d2ec2016-02-23 08:53:45 +0000470 // from the single-line __asm statements
471 if (PP.LookAhead(0).is(tok::l_brace))
472 break;
Ehsan Akhgari2f93b442014-07-25 02:27:14 +0000473 LineNo = SrcMgr.getLineNumber(ExpLoc.first, ExpLoc.second);
474 SkippedStartOfLine = Tok.isAtStartOfLine();
Coby Tayreec0fb36f2017-02-05 10:23:06 +0000475 } else if (Tok.is(tok::semi)) {
476 // A multi-line asm-statement, where next line is a comment
477 InAsmComment = true;
478 FID = ExpLoc.first;
479 LineNo = SrcMgr.getLineNumber(FID, ExpLoc.second);
Ehsan Akhgari2f93b442014-07-25 02:27:14 +0000480 }
Alp Toker1b935a82014-06-08 05:40:04 +0000481 } else if (!InAsmComment && Tok.is(tok::r_brace)) {
Ehsan Akhgari2f93b442014-07-25 02:27:14 +0000482 // In MSVC mode, braces only participate in brace matching and
483 // separating the asm statements. This is an intentional
484 // departure from the Apple gcc behavior.
485 if (!BraceNesting)
486 break;
Alp Toker1b935a82014-06-08 05:40:04 +0000487 }
488 }
Ehsan Akhgari0f89fac2014-07-06 05:26:54 +0000489 if (!InAsmComment && BraceNesting && Tok.is(tok::r_brace) &&
490 BraceCount == (savedBraceCount + BraceNesting)) {
491 // Consume the closing brace.
492 SkippedStartOfLine = Tok.isAtStartOfLine();
Marina Yatsina5f776792016-03-07 18:10:25 +0000493 // Don't want to add the closing brace of the whole asm block
494 if (SingleLineMode || BraceNesting > 1) {
495 Tok.clearFlag(Token::LeadingSpace);
496 AsmToks.push_back(Tok);
497 }
Alp Toker1b935a82014-06-08 05:40:04 +0000498 EndLoc = ConsumeBrace();
Ehsan Akhgari0f89fac2014-07-06 05:26:54 +0000499 BraceNesting--;
Nico Weber022e5072014-07-17 18:19:30 +0000500 // Finish if all of the opened braces in the inline asm section were
501 // consumed.
Ehsan Akhgari2f93b442014-07-25 02:27:14 +0000502 if (BraceNesting == 0 && !SingleLineMode)
Ehsan Akhgari0f89fac2014-07-06 05:26:54 +0000503 break;
504 else {
505 LBraceLocs.pop_back();
506 TokLoc = Tok.getLocation();
507 ++NumTokensRead;
508 continue;
509 }
Alp Toker1b935a82014-06-08 05:40:04 +0000510 }
511
512 // Consume the next token; make sure we don't modify the brace count etc.
513 // if we are in a comment.
514 EndLoc = TokLoc;
515 if (InAsmComment)
516 PP.Lex(Tok);
517 else {
Ehsan Akhgari0f89fac2014-07-06 05:26:54 +0000518 // Set the token as the start of line if we skipped the original start
519 // of line token in case it was a nested brace.
520 if (SkippedStartOfLine)
521 Tok.setFlag(Token::StartOfLine);
Alp Toker1b935a82014-06-08 05:40:04 +0000522 AsmToks.push_back(Tok);
523 ConsumeAnyToken();
524 }
525 TokLoc = Tok.getLocation();
526 ++NumTokensRead;
Ehsan Akhgari0f89fac2014-07-06 05:26:54 +0000527 SkippedStartOfLine = false;
Alp Toker1b935a82014-06-08 05:40:04 +0000528 } while (1);
529
Ehsan Akhgari0f89fac2014-07-06 05:26:54 +0000530 if (BraceNesting && BraceCount != savedBraceCount) {
Alp Toker1b935a82014-06-08 05:40:04 +0000531 // __asm without closing brace (this can happen at EOF).
Ehsan Akhgari0f89fac2014-07-06 05:26:54 +0000532 for (unsigned i = 0; i < BraceNesting; ++i) {
533 Diag(Tok, diag::err_expected) << tok::r_brace;
534 Diag(LBraceLocs.back(), diag::note_matching) << tok::l_brace;
535 LBraceLocs.pop_back();
536 }
Alp Toker1b935a82014-06-08 05:40:04 +0000537 return StmtError();
538 } else if (NumTokensRead == 0) {
539 // Empty __asm.
540 Diag(Tok, diag::err_expected) << tok::l_brace;
541 return StmtError();
542 }
543
544 // Okay, prepare to use MC to parse the assembly.
545 SmallVector<StringRef, 4> ConstraintRefs;
546 SmallVector<Expr *, 4> Exprs;
547 SmallVector<StringRef, 4> ClobberRefs;
548
549 // We need an actual supported target.
550 const llvm::Triple &TheTriple = Actions.Context.getTargetInfo().getTriple();
551 llvm::Triple::ArchType ArchTy = TheTriple.getArch();
552 const std::string &TT = TheTriple.getTriple();
553 const llvm::Target *TheTarget = nullptr;
554 bool UnsupportedArch =
555 (ArchTy != llvm::Triple::x86 && ArchTy != llvm::Triple::x86_64);
556 if (UnsupportedArch) {
557 Diag(AsmLoc, diag::err_msasm_unsupported_arch) << TheTriple.getArchName();
558 } else {
559 std::string Error;
560 TheTarget = llvm::TargetRegistry::lookupTarget(TT, Error);
561 if (!TheTarget)
562 Diag(AsmLoc, diag::err_msasm_unable_to_create_target) << Error;
563 }
564
Ehsan Akhgari0f89fac2014-07-06 05:26:54 +0000565 assert(!LBraceLocs.empty() && "Should have at least one location here");
566
Alp Toker1b935a82014-06-08 05:40:04 +0000567 // If we don't support assembly, or the assembly is empty, we don't
568 // need to instantiate the AsmParser, etc.
569 if (!TheTarget || AsmToks.empty()) {
Ehsan Akhgari0f89fac2014-07-06 05:26:54 +0000570 return Actions.ActOnMSAsmStmt(AsmLoc, LBraceLocs[0], AsmToks, StringRef(),
Alp Toker1b935a82014-06-08 05:40:04 +0000571 /*NumOutputs*/ 0, /*NumInputs*/ 0,
572 ConstraintRefs, ClobberRefs, Exprs, EndLoc);
573 }
574
575 // Expand the tokens into a string buffer.
576 SmallString<512> AsmString;
577 SmallVector<unsigned, 8> TokOffsets;
578 if (buildMSAsmString(PP, AsmLoc, AsmToks, TokOffsets, AsmString))
579 return StmtError();
580
Benjamin Kramer24952ce2017-10-22 20:16:28 +0000581 const TargetOptions &TO = Actions.Context.getTargetInfo().getTargetOpts();
Marina Yatsina41c45fa2016-02-03 11:32:08 +0000582 std::string FeaturesStr =
583 llvm::join(TO.Features.begin(), TO.Features.end(), ",");
584
Alp Toker1b935a82014-06-08 05:40:04 +0000585 std::unique_ptr<llvm::MCRegisterInfo> MRI(TheTarget->createMCRegInfo(TT));
586 std::unique_ptr<llvm::MCAsmInfo> MAI(TheTarget->createMCAsmInfo(*MRI, TT));
587 // Get the instruction descriptor.
588 std::unique_ptr<llvm::MCInstrInfo> MII(TheTarget->createMCInstrInfo());
589 std::unique_ptr<llvm::MCObjectFileInfo> MOFI(new llvm::MCObjectFileInfo());
590 std::unique_ptr<llvm::MCSubtargetInfo> STI(
Marina Yatsina41c45fa2016-02-03 11:32:08 +0000591 TheTarget->createMCSubtargetInfo(TT, TO.CPU, FeaturesStr));
Alp Toker1b935a82014-06-08 05:40:04 +0000592
593 llvm::SourceMgr TempSrcMgr;
594 llvm::MCContext Ctx(MAI.get(), MRI.get(), MOFI.get(), &TempSrcMgr);
Rafael Espindola2e8a7d32017-08-02 20:32:35 +0000595 MOFI->InitMCObjectFileInfo(TheTriple, /*PIC*/ false, Ctx);
Rafael Espindolad87f8d72014-08-27 20:03:29 +0000596 std::unique_ptr<llvm::MemoryBuffer> Buffer =
597 llvm::MemoryBuffer::getMemBuffer(AsmString, "<MS inline asm>");
Alp Toker1b935a82014-06-08 05:40:04 +0000598
599 // Tell SrcMgr about this buffer, which is what the parser will pick up.
David Blaikie9e095d92014-08-21 21:01:00 +0000600 TempSrcMgr.AddNewSourceBuffer(std::move(Buffer), llvm::SMLoc());
Alp Toker1b935a82014-06-08 05:40:04 +0000601
602 std::unique_ptr<llvm::MCStreamer> Str(createNullStreamer(Ctx));
603 std::unique_ptr<llvm::MCAsmParser> Parser(
604 createMCAsmParser(TempSrcMgr, Ctx, *Str.get(), *MAI));
605
606 // FIXME: init MCOptions from sanitizer flags here.
607 llvm::MCTargetOptions MCOptions;
608 std::unique_ptr<llvm::MCTargetAsmParser> TargetParser(
609 TheTarget->createMCAsmParser(*STI, *Parser, *MII, MCOptions));
610
Daniel Sanders50f17232015-09-15 16:17:27 +0000611 std::unique_ptr<llvm::MCInstPrinter> IP(
612 TheTarget->createMCInstPrinter(llvm::Triple(TT), 1, *MAI, *MII, *MRI));
Alp Toker1b935a82014-06-08 05:40:04 +0000613
614 // Change to the Intel dialect.
615 Parser->setAssemblerDialect(1);
616 Parser->setTargetParser(*TargetParser.get());
617 Parser->setParsingInlineAsm(true);
618 TargetParser->setParsingInlineAsm(true);
619
620 ClangAsmParserCallback Callback(*this, AsmLoc, AsmString, AsmToks,
621 TokOffsets);
622 TargetParser->setSemaCallback(&Callback);
623 TempSrcMgr.setDiagHandler(ClangAsmParserCallback::DiagHandlerCallback,
624 &Callback);
625
626 unsigned NumOutputs;
627 unsigned NumInputs;
628 std::string AsmStringIR;
629 SmallVector<std::pair<void *, bool>, 4> OpExprs;
630 SmallVector<std::string, 4> Constraints;
631 SmallVector<std::string, 4> Clobbers;
632 if (Parser->parseMSInlineAsm(AsmLoc.getPtrEncoding(), AsmStringIR, NumOutputs,
633 NumInputs, OpExprs, Constraints, Clobbers,
634 MII.get(), IP.get(), Callback))
635 return StmtError();
636
Reid Klecknerfb9f6472017-02-14 21:38:17 +0000637 // Filter out "fpsw" and "mxcsr". They aren't valid GCC asm clobber
638 // constraints. Clang always adds fpsr to the clobber list anyway.
639 llvm::erase_if(Clobbers, [](const std::string &C) {
Craig Topper879a4562019-02-05 06:13:14 +0000640 return C == "fpsr" || C == "mxcsr";
Reid Klecknerfb9f6472017-02-14 21:38:17 +0000641 });
Alp Toker1b935a82014-06-08 05:40:04 +0000642
643 // Build the vector of clobber StringRefs.
David Majnemer05c69862014-06-23 02:16:41 +0000644 ClobberRefs.insert(ClobberRefs.end(), Clobbers.begin(), Clobbers.end());
Alp Toker1b935a82014-06-08 05:40:04 +0000645
646 // Recast the void pointers and build the vector of constraint StringRefs.
647 unsigned NumExprs = NumOutputs + NumInputs;
648 ConstraintRefs.resize(NumExprs);
649 Exprs.resize(NumExprs);
650 for (unsigned i = 0, e = NumExprs; i != e; ++i) {
651 Expr *OpExpr = static_cast<Expr *>(OpExprs[i].first);
652 if (!OpExpr)
653 return StmtError();
654
655 // Need address of variable.
656 if (OpExprs[i].second)
657 OpExpr =
658 Actions.BuildUnaryOp(getCurScope(), AsmLoc, UO_AddrOf, OpExpr).get();
659
660 ConstraintRefs[i] = StringRef(Constraints[i]);
661 Exprs[i] = OpExpr;
662 }
663
664 // FIXME: We should be passing source locations for better diagnostics.
Ehsan Akhgari0f89fac2014-07-06 05:26:54 +0000665 return Actions.ActOnMSAsmStmt(AsmLoc, LBraceLocs[0], AsmToks, AsmStringIR,
Alp Toker1b935a82014-06-08 05:40:04 +0000666 NumOutputs, NumInputs, ConstraintRefs,
667 ClobberRefs, Exprs, EndLoc);
668}
669
670/// ParseAsmStatement - Parse a GNU extended asm statement.
671/// asm-statement:
672/// gnu-asm-statement
673/// ms-asm-statement
674///
675/// [GNU] gnu-asm-statement:
676/// 'asm' type-qualifier[opt] '(' asm-argument ')' ';'
677///
678/// [GNU] asm-argument:
679/// asm-string-literal
680/// asm-string-literal ':' asm-operands[opt]
681/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
682/// asm-string-literal ':' asm-operands[opt] ':' asm-operands[opt]
683/// ':' asm-clobbers
684///
685/// [GNU] asm-clobbers:
686/// asm-string-literal
687/// asm-clobbers ',' asm-string-literal
688///
689StmtResult Parser::ParseAsmStatement(bool &msAsm) {
690 assert(Tok.is(tok::kw_asm) && "Not an asm stmt");
691 SourceLocation AsmLoc = ConsumeToken();
692
Denis Zobnin628b0222016-04-21 10:59:18 +0000693 if (getLangOpts().AsmBlocks && !isGCCAsmStatement(Tok)) {
Alp Toker1b935a82014-06-08 05:40:04 +0000694 msAsm = true;
695 return ParseMicrosoftAsmStatement(AsmLoc);
696 }
Steven Wucb0d13f2015-01-16 23:05:28 +0000697
Alp Toker1b935a82014-06-08 05:40:04 +0000698 DeclSpec DS(AttrFactory);
699 SourceLocation Loc = Tok.getLocation();
Aaron Ballman08b06592014-07-22 12:44:22 +0000700 ParseTypeQualifierListOpt(DS, AR_VendorAttributesParsed);
Alp Toker1b935a82014-06-08 05:40:04 +0000701
702 // GNU asms accept, but warn, about type-qualifiers other than volatile.
703 if (DS.getTypeQualifiers() & DeclSpec::TQ_const)
Richard Smith01d96982016-12-02 23:00:28 +0000704 Diag(Loc, diag::warn_asm_qualifier_ignored) << "const";
Alp Toker1b935a82014-06-08 05:40:04 +0000705 if (DS.getTypeQualifiers() & DeclSpec::TQ_restrict)
Richard Smith01d96982016-12-02 23:00:28 +0000706 Diag(Loc, diag::warn_asm_qualifier_ignored) << "restrict";
Alp Toker1b935a82014-06-08 05:40:04 +0000707 // FIXME: Once GCC supports _Atomic, check whether it permits it here.
708 if (DS.getTypeQualifiers() & DeclSpec::TQ_atomic)
Richard Smith01d96982016-12-02 23:00:28 +0000709 Diag(Loc, diag::warn_asm_qualifier_ignored) << "_Atomic";
Alp Toker1b935a82014-06-08 05:40:04 +0000710
711 // Remember if this was a volatile asm.
712 bool isVolatile = DS.getTypeQualifiers() & DeclSpec::TQ_volatile;
Jennifer Yu954ec092019-05-30 01:05:46 +0000713 // Remember if this was a goto asm.
714 bool isGotoAsm = false;
Denis Zobnin628b0222016-04-21 10:59:18 +0000715
Denis Zobnin628b0222016-04-21 10:59:18 +0000716 if (Tok.is(tok::kw_goto)) {
Jennifer Yu954ec092019-05-30 01:05:46 +0000717 isGotoAsm = true;
718 ConsumeToken();
Denis Zobnin628b0222016-04-21 10:59:18 +0000719 }
720
Alp Toker1b935a82014-06-08 05:40:04 +0000721 if (Tok.isNot(tok::l_paren)) {
722 Diag(Tok, diag::err_expected_lparen_after) << "asm";
723 SkipUntil(tok::r_paren, StopAtSemi);
724 return StmtError();
725 }
726 BalancedDelimiterTracker T(*this, tok::l_paren);
727 T.consumeOpen();
728
729 ExprResult AsmString(ParseAsmStringLiteral());
Steven Wu18bbe192015-05-12 00:16:37 +0000730
731 // Check if GNU-style InlineAsm is disabled.
732 // Error on anything other than empty string.
733 if (!(getLangOpts().GNUAsm || AsmString.isInvalid())) {
734 const auto *SL = cast<StringLiteral>(AsmString.get());
735 if (!SL->getString().trim().empty())
736 Diag(Loc, diag::err_gnu_inline_asm_disabled);
737 }
738
Alp Toker1b935a82014-06-08 05:40:04 +0000739 if (AsmString.isInvalid()) {
740 // Consume up to and including the closing paren.
741 T.skipToEnd();
742 return StmtError();
743 }
744
745 SmallVector<IdentifierInfo *, 4> Names;
746 ExprVector Constraints;
747 ExprVector Exprs;
748 ExprVector Clobbers;
749
750 if (Tok.is(tok::r_paren)) {
751 // We have a simple asm expression like 'asm("foo")'.
752 T.consumeClose();
753 return Actions.ActOnGCCAsmStmt(AsmLoc, /*isSimple*/ true, isVolatile,
754 /*NumOutputs*/ 0, /*NumInputs*/ 0, nullptr,
755 Constraints, Exprs, AsmString.get(),
Jennifer Yu954ec092019-05-30 01:05:46 +0000756 Clobbers, /*NumLabels*/ 0,
757 T.getCloseLocation());
Alp Toker1b935a82014-06-08 05:40:04 +0000758 }
759
760 // Parse Outputs, if present.
761 bool AteExtraColon = false;
762 if (Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
763 // In C++ mode, parse "::" like ": :".
764 AteExtraColon = Tok.is(tok::coloncolon);
765 ConsumeToken();
766
Jennifer Yu954ec092019-05-30 01:05:46 +0000767 if (!AteExtraColon && isGotoAsm && Tok.isNot(tok::colon)) {
768 Diag(Tok, diag::err_asm_goto_cannot_have_output);
769 SkipUntil(tok::r_paren, StopAtSemi);
770 return StmtError();
771 }
772
Alp Toker1b935a82014-06-08 05:40:04 +0000773 if (!AteExtraColon && ParseAsmOperandsOpt(Names, Constraints, Exprs))
774 return StmtError();
775 }
776
777 unsigned NumOutputs = Names.size();
778
779 // Parse Inputs, if present.
780 if (AteExtraColon || Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
781 // In C++ mode, parse "::" like ": :".
782 if (AteExtraColon)
783 AteExtraColon = false;
784 else {
785 AteExtraColon = Tok.is(tok::coloncolon);
786 ConsumeToken();
787 }
788
789 if (!AteExtraColon && ParseAsmOperandsOpt(Names, Constraints, Exprs))
790 return StmtError();
791 }
792
793 assert(Names.size() == Constraints.size() &&
794 Constraints.size() == Exprs.size() && "Input operand size mismatch!");
795
796 unsigned NumInputs = Names.size() - NumOutputs;
797
798 // Parse the clobbers, if present.
Jennifer Yu954ec092019-05-30 01:05:46 +0000799 if (AteExtraColon || Tok.is(tok::colon) || Tok.is(tok::coloncolon)) {
800 if (AteExtraColon)
801 AteExtraColon = false;
802 else {
803 AteExtraColon = Tok.is(tok::coloncolon);
Alp Toker1b935a82014-06-08 05:40:04 +0000804 ConsumeToken();
Jennifer Yu954ec092019-05-30 01:05:46 +0000805 }
Alp Toker1b935a82014-06-08 05:40:04 +0000806 // Parse the asm-string list for clobbers if present.
Jennifer Yu954ec092019-05-30 01:05:46 +0000807 if (!AteExtraColon && isTokenStringLiteral()) {
Alp Toker1b935a82014-06-08 05:40:04 +0000808 while (1) {
809 ExprResult Clobber(ParseAsmStringLiteral());
810
811 if (Clobber.isInvalid())
812 break;
813
814 Clobbers.push_back(Clobber.get());
815
816 if (!TryConsumeToken(tok::comma))
817 break;
818 }
819 }
820 }
Jennifer Yu954ec092019-05-30 01:05:46 +0000821 if (!isGotoAsm && (Tok.isNot(tok::r_paren) || AteExtraColon)) {
822 Diag(Tok, diag::err_expected) << tok::r_paren;
823 SkipUntil(tok::r_paren, StopAtSemi);
824 return StmtError();
825 }
Alp Toker1b935a82014-06-08 05:40:04 +0000826
Jennifer Yu954ec092019-05-30 01:05:46 +0000827 // Parse the goto label, if present.
828 unsigned NumLabels = 0;
829 if (AteExtraColon || Tok.is(tok::colon)) {
830 if (!AteExtraColon)
831 ConsumeToken();
832
833 while (true) {
834 if (Tok.isNot(tok::identifier)) {
835 Diag(Tok, diag::err_expected) << tok::identifier;
836 SkipUntil(tok::r_paren, StopAtSemi);
837 return StmtError();
838 }
839 LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
840 Tok.getLocation());
841 Names.push_back(Tok.getIdentifierInfo());
842 if (!LD) {
843 SkipUntil(tok::r_paren, StopAtSemi);
844 return StmtError();
845 }
846 ExprResult Res =
847 Actions.ActOnAddrLabel(Tok.getLocation(), Tok.getLocation(), LD);
848 Exprs.push_back(Res.get());
849 NumLabels++;
850 ConsumeToken();
851 if (!TryConsumeToken(tok::comma))
852 break;
853 }
854 } else if (isGotoAsm) {
855 Diag(Tok, diag::err_expected) << tok::colon;
856 SkipUntil(tok::r_paren, StopAtSemi);
857 return StmtError();
858 }
Alp Toker1b935a82014-06-08 05:40:04 +0000859 T.consumeClose();
860 return Actions.ActOnGCCAsmStmt(
861 AsmLoc, false, isVolatile, NumOutputs, NumInputs, Names.data(),
Jennifer Yu954ec092019-05-30 01:05:46 +0000862 Constraints, Exprs, AsmString.get(), Clobbers, NumLabels,
863 T.getCloseLocation());
Alp Toker1b935a82014-06-08 05:40:04 +0000864}
865
866/// ParseAsmOperands - Parse the asm-operands production as used by
867/// asm-statement, assuming the leading ':' token was eaten.
868///
869/// [GNU] asm-operands:
870/// asm-operand
871/// asm-operands ',' asm-operand
872///
873/// [GNU] asm-operand:
874/// asm-string-literal '(' expression ')'
875/// '[' identifier ']' asm-string-literal '(' expression ')'
876///
877//
878// FIXME: Avoid unnecessary std::string trashing.
879bool Parser::ParseAsmOperandsOpt(SmallVectorImpl<IdentifierInfo *> &Names,
880 SmallVectorImpl<Expr *> &Constraints,
881 SmallVectorImpl<Expr *> &Exprs) {
882 // 'asm-operands' isn't present?
883 if (!isTokenStringLiteral() && Tok.isNot(tok::l_square))
884 return false;
885
886 while (1) {
887 // Read the [id] if present.
888 if (Tok.is(tok::l_square)) {
889 BalancedDelimiterTracker T(*this, tok::l_square);
890 T.consumeOpen();
891
892 if (Tok.isNot(tok::identifier)) {
893 Diag(Tok, diag::err_expected) << tok::identifier;
894 SkipUntil(tok::r_paren, StopAtSemi);
895 return true;
896 }
897
898 IdentifierInfo *II = Tok.getIdentifierInfo();
899 ConsumeToken();
900
901 Names.push_back(II);
902 T.consumeClose();
903 } else
904 Names.push_back(nullptr);
905
906 ExprResult Constraint(ParseAsmStringLiteral());
907 if (Constraint.isInvalid()) {
908 SkipUntil(tok::r_paren, StopAtSemi);
909 return true;
910 }
911 Constraints.push_back(Constraint.get());
912
913 if (Tok.isNot(tok::l_paren)) {
914 Diag(Tok, diag::err_expected_lparen_after) << "asm operand";
915 SkipUntil(tok::r_paren, StopAtSemi);
916 return true;
917 }
918
919 // Read the parenthesized expression.
920 BalancedDelimiterTracker T(*this, tok::l_paren);
921 T.consumeOpen();
Kaelyn Takata15867822014-11-21 18:48:04 +0000922 ExprResult Res = Actions.CorrectDelayedTyposInExpr(ParseExpression());
Alp Toker1b935a82014-06-08 05:40:04 +0000923 T.consumeClose();
924 if (Res.isInvalid()) {
925 SkipUntil(tok::r_paren, StopAtSemi);
926 return true;
927 }
928 Exprs.push_back(Res.get());
929 // Eat the comma and continue parsing if it exists.
930 if (!TryConsumeToken(tok::comma))
931 return false;
932 }
933}