xref: /minix3/external/bsd/llvm/dist/clang/lib/Parse/ParseStmt.cpp (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1f4a2713aSLionel Sambuc //===--- ParseStmt.cpp - Statement and Block Parser -----------------------===//
2f4a2713aSLionel Sambuc //
3f4a2713aSLionel Sambuc //                     The LLVM Compiler Infrastructure
4f4a2713aSLionel Sambuc //
5f4a2713aSLionel Sambuc // This file is distributed under the University of Illinois Open Source
6f4a2713aSLionel Sambuc // License. See LICENSE.TXT for details.
7f4a2713aSLionel Sambuc //
8f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
9f4a2713aSLionel Sambuc //
10f4a2713aSLionel Sambuc // This file implements the Statement and Block portions of the Parser
11f4a2713aSLionel Sambuc // interface.
12f4a2713aSLionel Sambuc //
13f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
14f4a2713aSLionel Sambuc 
15f4a2713aSLionel Sambuc #include "clang/Parse/Parser.h"
16f4a2713aSLionel Sambuc #include "RAIIObjectsForParser.h"
17f4a2713aSLionel Sambuc #include "clang/AST/ASTContext.h"
18*0a6a1f1dSLionel Sambuc #include "clang/Basic/Attributes.h"
19f4a2713aSLionel Sambuc #include "clang/Basic/Diagnostic.h"
20f4a2713aSLionel Sambuc #include "clang/Basic/PrettyStackTrace.h"
21f4a2713aSLionel Sambuc #include "clang/Sema/DeclSpec.h"
22*0a6a1f1dSLionel Sambuc #include "clang/Sema/LoopHint.h"
23f4a2713aSLionel Sambuc #include "clang/Sema/PrettyDeclStackTrace.h"
24f4a2713aSLionel Sambuc #include "clang/Sema/Scope.h"
25f4a2713aSLionel Sambuc #include "clang/Sema/TypoCorrection.h"
26f4a2713aSLionel Sambuc #include "llvm/ADT/SmallString.h"
27f4a2713aSLionel Sambuc using namespace clang;
28f4a2713aSLionel Sambuc 
29f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
30f4a2713aSLionel Sambuc // C99 6.8: Statements and Blocks.
31f4a2713aSLionel Sambuc //===----------------------------------------------------------------------===//
32f4a2713aSLionel Sambuc 
33f4a2713aSLionel Sambuc /// \brief Parse a standalone statement (for instance, as the body of an 'if',
34f4a2713aSLionel Sambuc /// 'while', or 'for').
ParseStatement(SourceLocation * TrailingElseLoc)35f4a2713aSLionel Sambuc StmtResult Parser::ParseStatement(SourceLocation *TrailingElseLoc) {
36f4a2713aSLionel Sambuc   StmtResult Res;
37f4a2713aSLionel Sambuc 
38f4a2713aSLionel Sambuc   // We may get back a null statement if we found a #pragma. Keep going until
39f4a2713aSLionel Sambuc   // we get an actual statement.
40f4a2713aSLionel Sambuc   do {
41f4a2713aSLionel Sambuc     StmtVector Stmts;
42f4a2713aSLionel Sambuc     Res = ParseStatementOrDeclaration(Stmts, true, TrailingElseLoc);
43f4a2713aSLionel Sambuc   } while (!Res.isInvalid() && !Res.get());
44f4a2713aSLionel Sambuc 
45f4a2713aSLionel Sambuc   return Res;
46f4a2713aSLionel Sambuc }
47f4a2713aSLionel Sambuc 
48f4a2713aSLionel Sambuc /// ParseStatementOrDeclaration - Read 'statement' or 'declaration'.
49f4a2713aSLionel Sambuc ///       StatementOrDeclaration:
50f4a2713aSLionel Sambuc ///         statement
51f4a2713aSLionel Sambuc ///         declaration
52f4a2713aSLionel Sambuc ///
53f4a2713aSLionel Sambuc ///       statement:
54f4a2713aSLionel Sambuc ///         labeled-statement
55f4a2713aSLionel Sambuc ///         compound-statement
56f4a2713aSLionel Sambuc ///         expression-statement
57f4a2713aSLionel Sambuc ///         selection-statement
58f4a2713aSLionel Sambuc ///         iteration-statement
59f4a2713aSLionel Sambuc ///         jump-statement
60f4a2713aSLionel Sambuc /// [C++]   declaration-statement
61f4a2713aSLionel Sambuc /// [C++]   try-block
62f4a2713aSLionel Sambuc /// [MS]    seh-try-block
63f4a2713aSLionel Sambuc /// [OBC]   objc-throw-statement
64f4a2713aSLionel Sambuc /// [OBC]   objc-try-catch-statement
65f4a2713aSLionel Sambuc /// [OBC]   objc-synchronized-statement
66f4a2713aSLionel Sambuc /// [GNU]   asm-statement
67f4a2713aSLionel Sambuc /// [OMP]   openmp-construct             [TODO]
68f4a2713aSLionel Sambuc ///
69f4a2713aSLionel Sambuc ///       labeled-statement:
70f4a2713aSLionel Sambuc ///         identifier ':' statement
71f4a2713aSLionel Sambuc ///         'case' constant-expression ':' statement
72f4a2713aSLionel Sambuc ///         'default' ':' statement
73f4a2713aSLionel Sambuc ///
74f4a2713aSLionel Sambuc ///       selection-statement:
75f4a2713aSLionel Sambuc ///         if-statement
76f4a2713aSLionel Sambuc ///         switch-statement
77f4a2713aSLionel Sambuc ///
78f4a2713aSLionel Sambuc ///       iteration-statement:
79f4a2713aSLionel Sambuc ///         while-statement
80f4a2713aSLionel Sambuc ///         do-statement
81f4a2713aSLionel Sambuc ///         for-statement
82f4a2713aSLionel Sambuc ///
83f4a2713aSLionel Sambuc ///       expression-statement:
84f4a2713aSLionel Sambuc ///         expression[opt] ';'
85f4a2713aSLionel Sambuc ///
86f4a2713aSLionel Sambuc ///       jump-statement:
87f4a2713aSLionel Sambuc ///         'goto' identifier ';'
88f4a2713aSLionel Sambuc ///         'continue' ';'
89f4a2713aSLionel Sambuc ///         'break' ';'
90f4a2713aSLionel Sambuc ///         'return' expression[opt] ';'
91f4a2713aSLionel Sambuc /// [GNU]   'goto' '*' expression ';'
92f4a2713aSLionel Sambuc ///
93f4a2713aSLionel Sambuc /// [OBC] objc-throw-statement:
94f4a2713aSLionel Sambuc /// [OBC]   '@' 'throw' expression ';'
95f4a2713aSLionel Sambuc /// [OBC]   '@' 'throw' ';'
96f4a2713aSLionel Sambuc ///
97f4a2713aSLionel Sambuc StmtResult
ParseStatementOrDeclaration(StmtVector & Stmts,bool OnlyStatement,SourceLocation * TrailingElseLoc)98f4a2713aSLionel Sambuc Parser::ParseStatementOrDeclaration(StmtVector &Stmts, bool OnlyStatement,
99f4a2713aSLionel Sambuc                                     SourceLocation *TrailingElseLoc) {
100f4a2713aSLionel Sambuc 
101f4a2713aSLionel Sambuc   ParenBraceBracketBalancer BalancerRAIIObj(*this);
102f4a2713aSLionel Sambuc 
103f4a2713aSLionel Sambuc   ParsedAttributesWithRange Attrs(AttrFactory);
104*0a6a1f1dSLionel Sambuc   MaybeParseCXX11Attributes(Attrs, nullptr, /*MightBeObjCMessageSend*/ true);
105f4a2713aSLionel Sambuc 
106f4a2713aSLionel Sambuc   StmtResult Res = ParseStatementOrDeclarationAfterAttributes(Stmts,
107f4a2713aSLionel Sambuc                                  OnlyStatement, TrailingElseLoc, Attrs);
108f4a2713aSLionel Sambuc 
109f4a2713aSLionel Sambuc   assert((Attrs.empty() || Res.isInvalid() || Res.isUsable()) &&
110f4a2713aSLionel Sambuc          "attributes on empty statement");
111f4a2713aSLionel Sambuc 
112f4a2713aSLionel Sambuc   if (Attrs.empty() || Res.isInvalid())
113f4a2713aSLionel Sambuc     return Res;
114f4a2713aSLionel Sambuc 
115f4a2713aSLionel Sambuc   return Actions.ProcessStmtAttributes(Res.get(), Attrs.getList(), Attrs.Range);
116f4a2713aSLionel Sambuc }
117f4a2713aSLionel Sambuc 
118f4a2713aSLionel Sambuc namespace {
119f4a2713aSLionel Sambuc class StatementFilterCCC : public CorrectionCandidateCallback {
120f4a2713aSLionel Sambuc public:
StatementFilterCCC(Token nextTok)121f4a2713aSLionel Sambuc   StatementFilterCCC(Token nextTok) : NextToken(nextTok) {
122f4a2713aSLionel Sambuc     WantTypeSpecifiers = nextTok.is(tok::l_paren) || nextTok.is(tok::less) ||
123f4a2713aSLionel Sambuc                          nextTok.is(tok::identifier) || nextTok.is(tok::star) ||
124f4a2713aSLionel Sambuc                          nextTok.is(tok::amp) || nextTok.is(tok::l_square);
125f4a2713aSLionel Sambuc     WantExpressionKeywords = nextTok.is(tok::l_paren) ||
126f4a2713aSLionel Sambuc                              nextTok.is(tok::identifier) ||
127f4a2713aSLionel Sambuc                              nextTok.is(tok::arrow) || nextTok.is(tok::period);
128f4a2713aSLionel Sambuc     WantRemainingKeywords = nextTok.is(tok::l_paren) || nextTok.is(tok::semi) ||
129f4a2713aSLionel Sambuc                             nextTok.is(tok::identifier) ||
130f4a2713aSLionel Sambuc                             nextTok.is(tok::l_brace);
131f4a2713aSLionel Sambuc     WantCXXNamedCasts = false;
132f4a2713aSLionel Sambuc   }
133f4a2713aSLionel Sambuc 
ValidateCandidate(const TypoCorrection & candidate)134*0a6a1f1dSLionel Sambuc   bool ValidateCandidate(const TypoCorrection &candidate) override {
135f4a2713aSLionel Sambuc     if (FieldDecl *FD = candidate.getCorrectionDeclAs<FieldDecl>())
136f4a2713aSLionel Sambuc       return !candidate.getCorrectionSpecifier() || isa<ObjCIvarDecl>(FD);
137f4a2713aSLionel Sambuc     if (NextToken.is(tok::equal))
138f4a2713aSLionel Sambuc       return candidate.getCorrectionDeclAs<VarDecl>();
139f4a2713aSLionel Sambuc     if (NextToken.is(tok::period) &&
140f4a2713aSLionel Sambuc         candidate.getCorrectionDeclAs<NamespaceDecl>())
141f4a2713aSLionel Sambuc       return false;
142f4a2713aSLionel Sambuc     return CorrectionCandidateCallback::ValidateCandidate(candidate);
143f4a2713aSLionel Sambuc   }
144f4a2713aSLionel Sambuc 
145f4a2713aSLionel Sambuc private:
146f4a2713aSLionel Sambuc   Token NextToken;
147f4a2713aSLionel Sambuc };
148f4a2713aSLionel Sambuc }
149f4a2713aSLionel Sambuc 
150f4a2713aSLionel Sambuc StmtResult
ParseStatementOrDeclarationAfterAttributes(StmtVector & Stmts,bool OnlyStatement,SourceLocation * TrailingElseLoc,ParsedAttributesWithRange & Attrs)151f4a2713aSLionel Sambuc Parser::ParseStatementOrDeclarationAfterAttributes(StmtVector &Stmts,
152f4a2713aSLionel Sambuc           bool OnlyStatement, SourceLocation *TrailingElseLoc,
153f4a2713aSLionel Sambuc           ParsedAttributesWithRange &Attrs) {
154*0a6a1f1dSLionel Sambuc   const char *SemiError = nullptr;
155f4a2713aSLionel Sambuc   StmtResult Res;
156f4a2713aSLionel Sambuc 
157f4a2713aSLionel Sambuc   // Cases in this switch statement should fall through if the parser expects
158f4a2713aSLionel Sambuc   // the token to end in a semicolon (in which case SemiError should be set),
159f4a2713aSLionel Sambuc   // or they directly 'return;' if not.
160f4a2713aSLionel Sambuc Retry:
161f4a2713aSLionel Sambuc   tok::TokenKind Kind  = Tok.getKind();
162f4a2713aSLionel Sambuc   SourceLocation AtLoc;
163f4a2713aSLionel Sambuc   switch (Kind) {
164f4a2713aSLionel Sambuc   case tok::at: // May be a @try or @throw statement
165f4a2713aSLionel Sambuc     {
166f4a2713aSLionel Sambuc       ProhibitAttributes(Attrs); // TODO: is it correct?
167f4a2713aSLionel Sambuc       AtLoc = ConsumeToken();  // consume @
168f4a2713aSLionel Sambuc       return ParseObjCAtStatement(AtLoc);
169f4a2713aSLionel Sambuc     }
170f4a2713aSLionel Sambuc 
171f4a2713aSLionel Sambuc   case tok::code_completion:
172f4a2713aSLionel Sambuc     Actions.CodeCompleteOrdinaryName(getCurScope(), Sema::PCC_Statement);
173f4a2713aSLionel Sambuc     cutOffParsing();
174f4a2713aSLionel Sambuc     return StmtError();
175f4a2713aSLionel Sambuc 
176f4a2713aSLionel Sambuc   case tok::identifier: {
177f4a2713aSLionel Sambuc     Token Next = NextToken();
178f4a2713aSLionel Sambuc     if (Next.is(tok::colon)) { // C99 6.8.1: labeled-statement
179f4a2713aSLionel Sambuc       // identifier ':' statement
180f4a2713aSLionel Sambuc       return ParseLabeledStatement(Attrs);
181f4a2713aSLionel Sambuc     }
182f4a2713aSLionel Sambuc 
183f4a2713aSLionel Sambuc     // Look up the identifier, and typo-correct it to a keyword if it's not
184f4a2713aSLionel Sambuc     // found.
185f4a2713aSLionel Sambuc     if (Next.isNot(tok::coloncolon)) {
186f4a2713aSLionel Sambuc       // Try to limit which sets of keywords should be included in typo
187f4a2713aSLionel Sambuc       // correction based on what the next token is.
188*0a6a1f1dSLionel Sambuc       if (TryAnnotateName(/*IsAddressOfOperand*/ false,
189*0a6a1f1dSLionel Sambuc                           llvm::make_unique<StatementFilterCCC>(Next)) ==
190*0a6a1f1dSLionel Sambuc           ANK_Error) {
191f4a2713aSLionel Sambuc         // Handle errors here by skipping up to the next semicolon or '}', and
192f4a2713aSLionel Sambuc         // eat the semicolon if that's what stopped us.
193f4a2713aSLionel Sambuc         SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
194f4a2713aSLionel Sambuc         if (Tok.is(tok::semi))
195f4a2713aSLionel Sambuc           ConsumeToken();
196f4a2713aSLionel Sambuc         return StmtError();
197f4a2713aSLionel Sambuc       }
198f4a2713aSLionel Sambuc 
199f4a2713aSLionel Sambuc       // If the identifier was typo-corrected, try again.
200f4a2713aSLionel Sambuc       if (Tok.isNot(tok::identifier))
201f4a2713aSLionel Sambuc         goto Retry;
202f4a2713aSLionel Sambuc     }
203f4a2713aSLionel Sambuc 
204f4a2713aSLionel Sambuc     // Fall through
205f4a2713aSLionel Sambuc   }
206f4a2713aSLionel Sambuc 
207f4a2713aSLionel Sambuc   default: {
208f4a2713aSLionel Sambuc     if ((getLangOpts().CPlusPlus || !OnlyStatement) && isDeclarationStatement()) {
209f4a2713aSLionel Sambuc       SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
210*0a6a1f1dSLionel Sambuc       DeclGroupPtrTy Decl = ParseDeclaration(Declarator::BlockContext,
211f4a2713aSLionel Sambuc                                              DeclEnd, Attrs);
212f4a2713aSLionel Sambuc       return Actions.ActOnDeclStmt(Decl, DeclStart, DeclEnd);
213f4a2713aSLionel Sambuc     }
214f4a2713aSLionel Sambuc 
215f4a2713aSLionel Sambuc     if (Tok.is(tok::r_brace)) {
216f4a2713aSLionel Sambuc       Diag(Tok, diag::err_expected_statement);
217f4a2713aSLionel Sambuc       return StmtError();
218f4a2713aSLionel Sambuc     }
219f4a2713aSLionel Sambuc 
220f4a2713aSLionel Sambuc     return ParseExprStatement();
221f4a2713aSLionel Sambuc   }
222f4a2713aSLionel Sambuc 
223f4a2713aSLionel Sambuc   case tok::kw_case:                // C99 6.8.1: labeled-statement
224f4a2713aSLionel Sambuc     return ParseCaseStatement();
225f4a2713aSLionel Sambuc   case tok::kw_default:             // C99 6.8.1: labeled-statement
226f4a2713aSLionel Sambuc     return ParseDefaultStatement();
227f4a2713aSLionel Sambuc 
228f4a2713aSLionel Sambuc   case tok::l_brace:                // C99 6.8.2: compound-statement
229f4a2713aSLionel Sambuc     return ParseCompoundStatement();
230f4a2713aSLionel Sambuc   case tok::semi: {                 // C99 6.8.3p3: expression[opt] ';'
231f4a2713aSLionel Sambuc     bool HasLeadingEmptyMacro = Tok.hasLeadingEmptyMacro();
232f4a2713aSLionel Sambuc     return Actions.ActOnNullStmt(ConsumeToken(), HasLeadingEmptyMacro);
233f4a2713aSLionel Sambuc   }
234f4a2713aSLionel Sambuc 
235f4a2713aSLionel Sambuc   case tok::kw_if:                  // C99 6.8.4.1: if-statement
236f4a2713aSLionel Sambuc     return ParseIfStatement(TrailingElseLoc);
237f4a2713aSLionel Sambuc   case tok::kw_switch:              // C99 6.8.4.2: switch-statement
238f4a2713aSLionel Sambuc     return ParseSwitchStatement(TrailingElseLoc);
239f4a2713aSLionel Sambuc 
240f4a2713aSLionel Sambuc   case tok::kw_while:               // C99 6.8.5.1: while-statement
241f4a2713aSLionel Sambuc     return ParseWhileStatement(TrailingElseLoc);
242f4a2713aSLionel Sambuc   case tok::kw_do:                  // C99 6.8.5.2: do-statement
243f4a2713aSLionel Sambuc     Res = ParseDoStatement();
244f4a2713aSLionel Sambuc     SemiError = "do/while";
245f4a2713aSLionel Sambuc     break;
246f4a2713aSLionel Sambuc   case tok::kw_for:                 // C99 6.8.5.3: for-statement
247f4a2713aSLionel Sambuc     return ParseForStatement(TrailingElseLoc);
248f4a2713aSLionel Sambuc 
249f4a2713aSLionel Sambuc   case tok::kw_goto:                // C99 6.8.6.1: goto-statement
250f4a2713aSLionel Sambuc     Res = ParseGotoStatement();
251f4a2713aSLionel Sambuc     SemiError = "goto";
252f4a2713aSLionel Sambuc     break;
253f4a2713aSLionel Sambuc   case tok::kw_continue:            // C99 6.8.6.2: continue-statement
254f4a2713aSLionel Sambuc     Res = ParseContinueStatement();
255f4a2713aSLionel Sambuc     SemiError = "continue";
256f4a2713aSLionel Sambuc     break;
257f4a2713aSLionel Sambuc   case tok::kw_break:               // C99 6.8.6.3: break-statement
258f4a2713aSLionel Sambuc     Res = ParseBreakStatement();
259f4a2713aSLionel Sambuc     SemiError = "break";
260f4a2713aSLionel Sambuc     break;
261f4a2713aSLionel Sambuc   case tok::kw_return:              // C99 6.8.6.4: return-statement
262f4a2713aSLionel Sambuc     Res = ParseReturnStatement();
263f4a2713aSLionel Sambuc     SemiError = "return";
264f4a2713aSLionel Sambuc     break;
265f4a2713aSLionel Sambuc 
266f4a2713aSLionel Sambuc   case tok::kw_asm: {
267f4a2713aSLionel Sambuc     ProhibitAttributes(Attrs);
268f4a2713aSLionel Sambuc     bool msAsm = false;
269f4a2713aSLionel Sambuc     Res = ParseAsmStatement(msAsm);
270f4a2713aSLionel Sambuc     Res = Actions.ActOnFinishFullStmt(Res.get());
271f4a2713aSLionel Sambuc     if (msAsm) return Res;
272f4a2713aSLionel Sambuc     SemiError = "asm";
273f4a2713aSLionel Sambuc     break;
274f4a2713aSLionel Sambuc   }
275f4a2713aSLionel Sambuc 
276*0a6a1f1dSLionel Sambuc   case tok::kw___if_exists:
277*0a6a1f1dSLionel Sambuc   case tok::kw___if_not_exists:
278*0a6a1f1dSLionel Sambuc     ProhibitAttributes(Attrs);
279*0a6a1f1dSLionel Sambuc     ParseMicrosoftIfExistsStatement(Stmts);
280*0a6a1f1dSLionel Sambuc     // An __if_exists block is like a compound statement, but it doesn't create
281*0a6a1f1dSLionel Sambuc     // a new scope.
282*0a6a1f1dSLionel Sambuc     return StmtEmpty();
283*0a6a1f1dSLionel Sambuc 
284f4a2713aSLionel Sambuc   case tok::kw_try:                 // C++ 15: try-block
285f4a2713aSLionel Sambuc     return ParseCXXTryBlock();
286f4a2713aSLionel Sambuc 
287f4a2713aSLionel Sambuc   case tok::kw___try:
288f4a2713aSLionel Sambuc     ProhibitAttributes(Attrs); // TODO: is it correct?
289f4a2713aSLionel Sambuc     return ParseSEHTryBlock();
290f4a2713aSLionel Sambuc 
291*0a6a1f1dSLionel Sambuc   case tok::kw___leave:
292*0a6a1f1dSLionel Sambuc     Res = ParseSEHLeaveStatement();
293*0a6a1f1dSLionel Sambuc     SemiError = "__leave";
294*0a6a1f1dSLionel Sambuc     break;
295*0a6a1f1dSLionel Sambuc 
296f4a2713aSLionel Sambuc   case tok::annot_pragma_vis:
297f4a2713aSLionel Sambuc     ProhibitAttributes(Attrs);
298f4a2713aSLionel Sambuc     HandlePragmaVisibility();
299f4a2713aSLionel Sambuc     return StmtEmpty();
300f4a2713aSLionel Sambuc 
301f4a2713aSLionel Sambuc   case tok::annot_pragma_pack:
302f4a2713aSLionel Sambuc     ProhibitAttributes(Attrs);
303f4a2713aSLionel Sambuc     HandlePragmaPack();
304f4a2713aSLionel Sambuc     return StmtEmpty();
305f4a2713aSLionel Sambuc 
306f4a2713aSLionel Sambuc   case tok::annot_pragma_msstruct:
307f4a2713aSLionel Sambuc     ProhibitAttributes(Attrs);
308f4a2713aSLionel Sambuc     HandlePragmaMSStruct();
309f4a2713aSLionel Sambuc     return StmtEmpty();
310f4a2713aSLionel Sambuc 
311f4a2713aSLionel Sambuc   case tok::annot_pragma_align:
312f4a2713aSLionel Sambuc     ProhibitAttributes(Attrs);
313f4a2713aSLionel Sambuc     HandlePragmaAlign();
314f4a2713aSLionel Sambuc     return StmtEmpty();
315f4a2713aSLionel Sambuc 
316f4a2713aSLionel Sambuc   case tok::annot_pragma_weak:
317f4a2713aSLionel Sambuc     ProhibitAttributes(Attrs);
318f4a2713aSLionel Sambuc     HandlePragmaWeak();
319f4a2713aSLionel Sambuc     return StmtEmpty();
320f4a2713aSLionel Sambuc 
321f4a2713aSLionel Sambuc   case tok::annot_pragma_weakalias:
322f4a2713aSLionel Sambuc     ProhibitAttributes(Attrs);
323f4a2713aSLionel Sambuc     HandlePragmaWeakAlias();
324f4a2713aSLionel Sambuc     return StmtEmpty();
325f4a2713aSLionel Sambuc 
326f4a2713aSLionel Sambuc   case tok::annot_pragma_redefine_extname:
327f4a2713aSLionel Sambuc     ProhibitAttributes(Attrs);
328f4a2713aSLionel Sambuc     HandlePragmaRedefineExtname();
329f4a2713aSLionel Sambuc     return StmtEmpty();
330f4a2713aSLionel Sambuc 
331f4a2713aSLionel Sambuc   case tok::annot_pragma_fp_contract:
332f4a2713aSLionel Sambuc     ProhibitAttributes(Attrs);
333f4a2713aSLionel Sambuc     Diag(Tok, diag::err_pragma_fp_contract_scope);
334f4a2713aSLionel Sambuc     ConsumeToken();
335f4a2713aSLionel Sambuc     return StmtError();
336f4a2713aSLionel Sambuc 
337f4a2713aSLionel Sambuc   case tok::annot_pragma_opencl_extension:
338f4a2713aSLionel Sambuc     ProhibitAttributes(Attrs);
339f4a2713aSLionel Sambuc     HandlePragmaOpenCLExtension();
340f4a2713aSLionel Sambuc     return StmtEmpty();
341f4a2713aSLionel Sambuc 
342f4a2713aSLionel Sambuc   case tok::annot_pragma_captured:
343f4a2713aSLionel Sambuc     ProhibitAttributes(Attrs);
344f4a2713aSLionel Sambuc     return HandlePragmaCaptured();
345f4a2713aSLionel Sambuc 
346f4a2713aSLionel Sambuc   case tok::annot_pragma_openmp:
347f4a2713aSLionel Sambuc     ProhibitAttributes(Attrs);
348*0a6a1f1dSLionel Sambuc     return ParseOpenMPDeclarativeOrExecutableDirective(!OnlyStatement);
349f4a2713aSLionel Sambuc 
350*0a6a1f1dSLionel Sambuc   case tok::annot_pragma_ms_pointers_to_members:
351*0a6a1f1dSLionel Sambuc     ProhibitAttributes(Attrs);
352*0a6a1f1dSLionel Sambuc     HandlePragmaMSPointersToMembers();
353*0a6a1f1dSLionel Sambuc     return StmtEmpty();
354*0a6a1f1dSLionel Sambuc 
355*0a6a1f1dSLionel Sambuc   case tok::annot_pragma_ms_pragma:
356*0a6a1f1dSLionel Sambuc     ProhibitAttributes(Attrs);
357*0a6a1f1dSLionel Sambuc     HandlePragmaMSPragma();
358*0a6a1f1dSLionel Sambuc     return StmtEmpty();
359*0a6a1f1dSLionel Sambuc 
360*0a6a1f1dSLionel Sambuc   case tok::annot_pragma_loop_hint:
361*0a6a1f1dSLionel Sambuc     ProhibitAttributes(Attrs);
362*0a6a1f1dSLionel Sambuc     return ParsePragmaLoopHint(Stmts, OnlyStatement, TrailingElseLoc, Attrs);
363f4a2713aSLionel Sambuc   }
364f4a2713aSLionel Sambuc 
365f4a2713aSLionel Sambuc   // If we reached this code, the statement must end in a semicolon.
366*0a6a1f1dSLionel Sambuc   if (!TryConsumeToken(tok::semi) && !Res.isInvalid()) {
367f4a2713aSLionel Sambuc     // If the result was valid, then we do want to diagnose this.  Use
368f4a2713aSLionel Sambuc     // ExpectAndConsume to emit the diagnostic, even though we know it won't
369f4a2713aSLionel Sambuc     // succeed.
370f4a2713aSLionel Sambuc     ExpectAndConsume(tok::semi, diag::err_expected_semi_after_stmt, SemiError);
371f4a2713aSLionel Sambuc     // Skip until we see a } or ;, but don't eat it.
372f4a2713aSLionel Sambuc     SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
373f4a2713aSLionel Sambuc   }
374f4a2713aSLionel Sambuc 
375f4a2713aSLionel Sambuc   return Res;
376f4a2713aSLionel Sambuc }
377f4a2713aSLionel Sambuc 
378f4a2713aSLionel Sambuc /// \brief Parse an expression statement.
ParseExprStatement()379f4a2713aSLionel Sambuc StmtResult Parser::ParseExprStatement() {
380f4a2713aSLionel Sambuc   // If a case keyword is missing, this is where it should be inserted.
381f4a2713aSLionel Sambuc   Token OldToken = Tok;
382f4a2713aSLionel Sambuc 
383f4a2713aSLionel Sambuc   // expression[opt] ';'
384f4a2713aSLionel Sambuc   ExprResult Expr(ParseExpression());
385f4a2713aSLionel Sambuc   if (Expr.isInvalid()) {
386f4a2713aSLionel Sambuc     // If the expression is invalid, skip ahead to the next semicolon or '}'.
387f4a2713aSLionel Sambuc     // Not doing this opens us up to the possibility of infinite loops if
388f4a2713aSLionel Sambuc     // ParseExpression does not consume any tokens.
389f4a2713aSLionel Sambuc     SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
390f4a2713aSLionel Sambuc     if (Tok.is(tok::semi))
391f4a2713aSLionel Sambuc       ConsumeToken();
392f4a2713aSLionel Sambuc     return Actions.ActOnExprStmtError();
393f4a2713aSLionel Sambuc   }
394f4a2713aSLionel Sambuc 
395f4a2713aSLionel Sambuc   if (Tok.is(tok::colon) && getCurScope()->isSwitchScope() &&
396f4a2713aSLionel Sambuc       Actions.CheckCaseExpression(Expr.get())) {
397f4a2713aSLionel Sambuc     // If a constant expression is followed by a colon inside a switch block,
398f4a2713aSLionel Sambuc     // suggest a missing case keyword.
399f4a2713aSLionel Sambuc     Diag(OldToken, diag::err_expected_case_before_expression)
400f4a2713aSLionel Sambuc       << FixItHint::CreateInsertion(OldToken.getLocation(), "case ");
401f4a2713aSLionel Sambuc 
402f4a2713aSLionel Sambuc     // Recover parsing as a case statement.
403f4a2713aSLionel Sambuc     return ParseCaseStatement(/*MissingCase=*/true, Expr);
404f4a2713aSLionel Sambuc   }
405f4a2713aSLionel Sambuc 
406f4a2713aSLionel Sambuc   // Otherwise, eat the semicolon.
407f4a2713aSLionel Sambuc   ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
408f4a2713aSLionel Sambuc   return Actions.ActOnExprStmt(Expr);
409f4a2713aSLionel Sambuc }
410f4a2713aSLionel Sambuc 
ParseSEHTryBlock()411f4a2713aSLionel Sambuc StmtResult Parser::ParseSEHTryBlock() {
412f4a2713aSLionel Sambuc   assert(Tok.is(tok::kw___try) && "Expected '__try'");
413f4a2713aSLionel Sambuc   SourceLocation Loc = ConsumeToken();
414f4a2713aSLionel Sambuc   return ParseSEHTryBlockCommon(Loc);
415f4a2713aSLionel Sambuc }
416f4a2713aSLionel Sambuc 
417f4a2713aSLionel Sambuc /// ParseSEHTryBlockCommon
418f4a2713aSLionel Sambuc ///
419f4a2713aSLionel Sambuc /// seh-try-block:
420f4a2713aSLionel Sambuc ///   '__try' compound-statement seh-handler
421f4a2713aSLionel Sambuc ///
422f4a2713aSLionel Sambuc /// seh-handler:
423f4a2713aSLionel Sambuc ///   seh-except-block
424f4a2713aSLionel Sambuc ///   seh-finally-block
425f4a2713aSLionel Sambuc ///
ParseSEHTryBlockCommon(SourceLocation TryLoc)426f4a2713aSLionel Sambuc StmtResult Parser::ParseSEHTryBlockCommon(SourceLocation TryLoc) {
427f4a2713aSLionel Sambuc   if(Tok.isNot(tok::l_brace))
428*0a6a1f1dSLionel Sambuc     return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
429f4a2713aSLionel Sambuc 
430*0a6a1f1dSLionel Sambuc   StmtResult TryBlock(ParseCompoundStatement(/*isStmtExpr=*/false,
431*0a6a1f1dSLionel Sambuc                       Scope::DeclScope | Scope::SEHTryScope));
432f4a2713aSLionel Sambuc   if(TryBlock.isInvalid())
433f4a2713aSLionel Sambuc     return TryBlock;
434f4a2713aSLionel Sambuc 
435f4a2713aSLionel Sambuc   StmtResult Handler;
436f4a2713aSLionel Sambuc   if (Tok.is(tok::identifier) &&
437f4a2713aSLionel Sambuc       Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
438f4a2713aSLionel Sambuc     SourceLocation Loc = ConsumeToken();
439f4a2713aSLionel Sambuc     Handler = ParseSEHExceptBlock(Loc);
440f4a2713aSLionel Sambuc   } else if (Tok.is(tok::kw___finally)) {
441f4a2713aSLionel Sambuc     SourceLocation Loc = ConsumeToken();
442f4a2713aSLionel Sambuc     Handler = ParseSEHFinallyBlock(Loc);
443f4a2713aSLionel Sambuc   } else {
444f4a2713aSLionel Sambuc     return StmtError(Diag(Tok,diag::err_seh_expected_handler));
445f4a2713aSLionel Sambuc   }
446f4a2713aSLionel Sambuc 
447f4a2713aSLionel Sambuc   if(Handler.isInvalid())
448f4a2713aSLionel Sambuc     return Handler;
449f4a2713aSLionel Sambuc 
450f4a2713aSLionel Sambuc   return Actions.ActOnSEHTryBlock(false /* IsCXXTry */,
451f4a2713aSLionel Sambuc                                   TryLoc,
452*0a6a1f1dSLionel Sambuc                                   TryBlock.get(),
453*0a6a1f1dSLionel Sambuc                                   Handler.get());
454f4a2713aSLionel Sambuc }
455f4a2713aSLionel Sambuc 
456f4a2713aSLionel Sambuc /// ParseSEHExceptBlock - Handle __except
457f4a2713aSLionel Sambuc ///
458f4a2713aSLionel Sambuc /// seh-except-block:
459f4a2713aSLionel Sambuc ///   '__except' '(' seh-filter-expression ')' compound-statement
460f4a2713aSLionel Sambuc ///
ParseSEHExceptBlock(SourceLocation ExceptLoc)461f4a2713aSLionel Sambuc StmtResult Parser::ParseSEHExceptBlock(SourceLocation ExceptLoc) {
462f4a2713aSLionel Sambuc   PoisonIdentifierRAIIObject raii(Ident__exception_code, false),
463f4a2713aSLionel Sambuc     raii2(Ident___exception_code, false),
464f4a2713aSLionel Sambuc     raii3(Ident_GetExceptionCode, false);
465f4a2713aSLionel Sambuc 
466*0a6a1f1dSLionel Sambuc   if (ExpectAndConsume(tok::l_paren))
467f4a2713aSLionel Sambuc     return StmtError();
468f4a2713aSLionel Sambuc 
469f4a2713aSLionel Sambuc   ParseScope ExpectScope(this, Scope::DeclScope | Scope::ControlScope);
470f4a2713aSLionel Sambuc 
471f4a2713aSLionel Sambuc   if (getLangOpts().Borland) {
472f4a2713aSLionel Sambuc     Ident__exception_info->setIsPoisoned(false);
473f4a2713aSLionel Sambuc     Ident___exception_info->setIsPoisoned(false);
474f4a2713aSLionel Sambuc     Ident_GetExceptionInfo->setIsPoisoned(false);
475f4a2713aSLionel Sambuc   }
476f4a2713aSLionel Sambuc   ExprResult FilterExpr(ParseExpression());
477f4a2713aSLionel Sambuc 
478f4a2713aSLionel Sambuc   if (getLangOpts().Borland) {
479f4a2713aSLionel Sambuc     Ident__exception_info->setIsPoisoned(true);
480f4a2713aSLionel Sambuc     Ident___exception_info->setIsPoisoned(true);
481f4a2713aSLionel Sambuc     Ident_GetExceptionInfo->setIsPoisoned(true);
482f4a2713aSLionel Sambuc   }
483f4a2713aSLionel Sambuc 
484f4a2713aSLionel Sambuc   if(FilterExpr.isInvalid())
485f4a2713aSLionel Sambuc     return StmtError();
486f4a2713aSLionel Sambuc 
487*0a6a1f1dSLionel Sambuc   if (ExpectAndConsume(tok::r_paren))
488f4a2713aSLionel Sambuc     return StmtError();
489f4a2713aSLionel Sambuc 
490f4a2713aSLionel Sambuc   StmtResult Block(ParseCompoundStatement());
491f4a2713aSLionel Sambuc 
492f4a2713aSLionel Sambuc   if(Block.isInvalid())
493f4a2713aSLionel Sambuc     return Block;
494f4a2713aSLionel Sambuc 
495*0a6a1f1dSLionel Sambuc   return Actions.ActOnSEHExceptBlock(ExceptLoc, FilterExpr.get(), Block.get());
496f4a2713aSLionel Sambuc }
497f4a2713aSLionel Sambuc 
498f4a2713aSLionel Sambuc /// ParseSEHFinallyBlock - Handle __finally
499f4a2713aSLionel Sambuc ///
500f4a2713aSLionel Sambuc /// seh-finally-block:
501f4a2713aSLionel Sambuc ///   '__finally' compound-statement
502f4a2713aSLionel Sambuc ///
ParseSEHFinallyBlock(SourceLocation FinallyBlock)503f4a2713aSLionel Sambuc StmtResult Parser::ParseSEHFinallyBlock(SourceLocation FinallyBlock) {
504f4a2713aSLionel Sambuc   PoisonIdentifierRAIIObject raii(Ident__abnormal_termination, false),
505f4a2713aSLionel Sambuc     raii2(Ident___abnormal_termination, false),
506f4a2713aSLionel Sambuc     raii3(Ident_AbnormalTermination, false);
507f4a2713aSLionel Sambuc 
508f4a2713aSLionel Sambuc   StmtResult Block(ParseCompoundStatement());
509f4a2713aSLionel Sambuc   if(Block.isInvalid())
510f4a2713aSLionel Sambuc     return Block;
511f4a2713aSLionel Sambuc 
512*0a6a1f1dSLionel Sambuc   return Actions.ActOnSEHFinallyBlock(FinallyBlock,Block.get());
513*0a6a1f1dSLionel Sambuc }
514*0a6a1f1dSLionel Sambuc 
515*0a6a1f1dSLionel Sambuc /// Handle __leave
516*0a6a1f1dSLionel Sambuc ///
517*0a6a1f1dSLionel Sambuc /// seh-leave-statement:
518*0a6a1f1dSLionel Sambuc ///   '__leave' ';'
519*0a6a1f1dSLionel Sambuc ///
ParseSEHLeaveStatement()520*0a6a1f1dSLionel Sambuc StmtResult Parser::ParseSEHLeaveStatement() {
521*0a6a1f1dSLionel Sambuc   SourceLocation LeaveLoc = ConsumeToken();  // eat the '__leave'.
522*0a6a1f1dSLionel Sambuc   return Actions.ActOnSEHLeaveStmt(LeaveLoc, getCurScope());
523f4a2713aSLionel Sambuc }
524f4a2713aSLionel Sambuc 
525f4a2713aSLionel Sambuc /// ParseLabeledStatement - We have an identifier and a ':' after it.
526f4a2713aSLionel Sambuc ///
527f4a2713aSLionel Sambuc ///       labeled-statement:
528f4a2713aSLionel Sambuc ///         identifier ':' statement
529f4a2713aSLionel Sambuc /// [GNU]   identifier ':' attributes[opt] statement
530f4a2713aSLionel Sambuc ///
ParseLabeledStatement(ParsedAttributesWithRange & attrs)531f4a2713aSLionel Sambuc StmtResult Parser::ParseLabeledStatement(ParsedAttributesWithRange &attrs) {
532f4a2713aSLionel Sambuc   assert(Tok.is(tok::identifier) && Tok.getIdentifierInfo() &&
533f4a2713aSLionel Sambuc          "Not an identifier!");
534f4a2713aSLionel Sambuc 
535f4a2713aSLionel Sambuc   Token IdentTok = Tok;  // Save the whole token.
536f4a2713aSLionel Sambuc   ConsumeToken();  // eat the identifier.
537f4a2713aSLionel Sambuc 
538f4a2713aSLionel Sambuc   assert(Tok.is(tok::colon) && "Not a label!");
539f4a2713aSLionel Sambuc 
540f4a2713aSLionel Sambuc   // identifier ':' statement
541f4a2713aSLionel Sambuc   SourceLocation ColonLoc = ConsumeToken();
542f4a2713aSLionel Sambuc 
543f4a2713aSLionel Sambuc   // Read label attributes, if present.
544f4a2713aSLionel Sambuc   StmtResult SubStmt;
545f4a2713aSLionel Sambuc   if (Tok.is(tok::kw___attribute)) {
546f4a2713aSLionel Sambuc     ParsedAttributesWithRange TempAttrs(AttrFactory);
547f4a2713aSLionel Sambuc     ParseGNUAttributes(TempAttrs);
548f4a2713aSLionel Sambuc 
549f4a2713aSLionel Sambuc     // In C++, GNU attributes only apply to the label if they are followed by a
550f4a2713aSLionel Sambuc     // semicolon, to disambiguate label attributes from attributes on a labeled
551f4a2713aSLionel Sambuc     // declaration.
552f4a2713aSLionel Sambuc     //
553f4a2713aSLionel Sambuc     // This doesn't quite match what GCC does; if the attribute list is empty
554f4a2713aSLionel Sambuc     // and followed by a semicolon, GCC will reject (it appears to parse the
555f4a2713aSLionel Sambuc     // attributes as part of a statement in that case). That looks like a bug.
556f4a2713aSLionel Sambuc     if (!getLangOpts().CPlusPlus || Tok.is(tok::semi))
557f4a2713aSLionel Sambuc       attrs.takeAllFrom(TempAttrs);
558f4a2713aSLionel Sambuc     else if (isDeclarationStatement()) {
559f4a2713aSLionel Sambuc       StmtVector Stmts;
560f4a2713aSLionel Sambuc       // FIXME: We should do this whether or not we have a declaration
561f4a2713aSLionel Sambuc       // statement, but that doesn't work correctly (because ProhibitAttributes
562f4a2713aSLionel Sambuc       // can't handle GNU attributes), so only call it in the one case where
563f4a2713aSLionel Sambuc       // GNU attributes are allowed.
564f4a2713aSLionel Sambuc       SubStmt = ParseStatementOrDeclarationAfterAttributes(
565*0a6a1f1dSLionel Sambuc           Stmts, /*OnlyStmts*/ true, nullptr, TempAttrs);
566f4a2713aSLionel Sambuc       if (!TempAttrs.empty() && !SubStmt.isInvalid())
567f4a2713aSLionel Sambuc         SubStmt = Actions.ProcessStmtAttributes(
568f4a2713aSLionel Sambuc             SubStmt.get(), TempAttrs.getList(), TempAttrs.Range);
569f4a2713aSLionel Sambuc     } else {
570*0a6a1f1dSLionel Sambuc       Diag(Tok, diag::err_expected_after) << "__attribute__" << tok::semi;
571f4a2713aSLionel Sambuc     }
572f4a2713aSLionel Sambuc   }
573f4a2713aSLionel Sambuc 
574f4a2713aSLionel Sambuc   // If we've not parsed a statement yet, parse one now.
575f4a2713aSLionel Sambuc   if (!SubStmt.isInvalid() && !SubStmt.isUsable())
576f4a2713aSLionel Sambuc     SubStmt = ParseStatement();
577f4a2713aSLionel Sambuc 
578f4a2713aSLionel Sambuc   // Broken substmt shouldn't prevent the label from being added to the AST.
579f4a2713aSLionel Sambuc   if (SubStmt.isInvalid())
580f4a2713aSLionel Sambuc     SubStmt = Actions.ActOnNullStmt(ColonLoc);
581f4a2713aSLionel Sambuc 
582f4a2713aSLionel Sambuc   LabelDecl *LD = Actions.LookupOrCreateLabel(IdentTok.getIdentifierInfo(),
583f4a2713aSLionel Sambuc                                               IdentTok.getLocation());
584f4a2713aSLionel Sambuc   if (AttributeList *Attrs = attrs.getList()) {
585f4a2713aSLionel Sambuc     Actions.ProcessDeclAttributeList(Actions.CurScope, LD, Attrs);
586f4a2713aSLionel Sambuc     attrs.clear();
587f4a2713aSLionel Sambuc   }
588f4a2713aSLionel Sambuc 
589f4a2713aSLionel Sambuc   return Actions.ActOnLabelStmt(IdentTok.getLocation(), LD, ColonLoc,
590f4a2713aSLionel Sambuc                                 SubStmt.get());
591f4a2713aSLionel Sambuc }
592f4a2713aSLionel Sambuc 
593f4a2713aSLionel Sambuc /// ParseCaseStatement
594f4a2713aSLionel Sambuc ///       labeled-statement:
595f4a2713aSLionel Sambuc ///         'case' constant-expression ':' statement
596f4a2713aSLionel Sambuc /// [GNU]   'case' constant-expression '...' constant-expression ':' statement
597f4a2713aSLionel Sambuc ///
ParseCaseStatement(bool MissingCase,ExprResult Expr)598f4a2713aSLionel Sambuc StmtResult Parser::ParseCaseStatement(bool MissingCase, ExprResult Expr) {
599f4a2713aSLionel Sambuc   assert((MissingCase || Tok.is(tok::kw_case)) && "Not a case stmt!");
600f4a2713aSLionel Sambuc 
601f4a2713aSLionel Sambuc   // It is very very common for code to contain many case statements recursively
602f4a2713aSLionel Sambuc   // nested, as in (but usually without indentation):
603f4a2713aSLionel Sambuc   //  case 1:
604f4a2713aSLionel Sambuc   //    case 2:
605f4a2713aSLionel Sambuc   //      case 3:
606f4a2713aSLionel Sambuc   //         case 4:
607f4a2713aSLionel Sambuc   //           case 5: etc.
608f4a2713aSLionel Sambuc   //
609f4a2713aSLionel Sambuc   // Parsing this naively works, but is both inefficient and can cause us to run
610f4a2713aSLionel Sambuc   // out of stack space in our recursive descent parser.  As a special case,
611f4a2713aSLionel Sambuc   // flatten this recursion into an iterative loop.  This is complex and gross,
612f4a2713aSLionel Sambuc   // but all the grossness is constrained to ParseCaseStatement (and some
613f4a2713aSLionel Sambuc   // weirdness in the actions), so this is just local grossness :).
614f4a2713aSLionel Sambuc 
615f4a2713aSLionel Sambuc   // TopLevelCase - This is the highest level we have parsed.  'case 1' in the
616f4a2713aSLionel Sambuc   // example above.
617f4a2713aSLionel Sambuc   StmtResult TopLevelCase(true);
618f4a2713aSLionel Sambuc 
619f4a2713aSLionel Sambuc   // DeepestParsedCaseStmt - This is the deepest statement we have parsed, which
620f4a2713aSLionel Sambuc   // gets updated each time a new case is parsed, and whose body is unset so
621f4a2713aSLionel Sambuc   // far.  When parsing 'case 4', this is the 'case 3' node.
622*0a6a1f1dSLionel Sambuc   Stmt *DeepestParsedCaseStmt = nullptr;
623f4a2713aSLionel Sambuc 
624f4a2713aSLionel Sambuc   // While we have case statements, eat and stack them.
625f4a2713aSLionel Sambuc   SourceLocation ColonLoc;
626f4a2713aSLionel Sambuc   do {
627f4a2713aSLionel Sambuc     SourceLocation CaseLoc = MissingCase ? Expr.get()->getExprLoc() :
628f4a2713aSLionel Sambuc                                            ConsumeToken();  // eat the 'case'.
629*0a6a1f1dSLionel Sambuc     ColonLoc = SourceLocation();
630f4a2713aSLionel Sambuc 
631f4a2713aSLionel Sambuc     if (Tok.is(tok::code_completion)) {
632f4a2713aSLionel Sambuc       Actions.CodeCompleteCase(getCurScope());
633f4a2713aSLionel Sambuc       cutOffParsing();
634f4a2713aSLionel Sambuc       return StmtError();
635f4a2713aSLionel Sambuc     }
636f4a2713aSLionel Sambuc 
637f4a2713aSLionel Sambuc     /// We don't want to treat 'case x : y' as a potential typo for 'case x::y'.
638f4a2713aSLionel Sambuc     /// Disable this form of error recovery while we're parsing the case
639f4a2713aSLionel Sambuc     /// expression.
640f4a2713aSLionel Sambuc     ColonProtectionRAIIObject ColonProtection(*this);
641f4a2713aSLionel Sambuc 
642*0a6a1f1dSLionel Sambuc     ExprResult LHS;
643*0a6a1f1dSLionel Sambuc     if (!MissingCase) {
644*0a6a1f1dSLionel Sambuc       LHS = ParseConstantExpression();
645*0a6a1f1dSLionel Sambuc       if (!getLangOpts().CPlusPlus11) {
646*0a6a1f1dSLionel Sambuc         LHS = Actions.CorrectDelayedTyposInExpr(LHS, [this](class Expr *E) {
647*0a6a1f1dSLionel Sambuc           return Actions.VerifyIntegerConstantExpression(E);
648*0a6a1f1dSLionel Sambuc         });
649*0a6a1f1dSLionel Sambuc       }
650f4a2713aSLionel Sambuc       if (LHS.isInvalid()) {
651*0a6a1f1dSLionel Sambuc         // If constant-expression is parsed unsuccessfully, recover by skipping
652*0a6a1f1dSLionel Sambuc         // current case statement (moving to the colon that ends it).
653*0a6a1f1dSLionel Sambuc         if (SkipUntil(tok::colon, tok::r_brace, StopAtSemi | StopBeforeMatch)) {
654*0a6a1f1dSLionel Sambuc           TryConsumeToken(tok::colon, ColonLoc);
655*0a6a1f1dSLionel Sambuc           continue;
656*0a6a1f1dSLionel Sambuc         }
657f4a2713aSLionel Sambuc         return StmtError();
658f4a2713aSLionel Sambuc       }
659*0a6a1f1dSLionel Sambuc     } else {
660*0a6a1f1dSLionel Sambuc       LHS = Expr;
661*0a6a1f1dSLionel Sambuc       MissingCase = false;
662*0a6a1f1dSLionel Sambuc     }
663f4a2713aSLionel Sambuc 
664f4a2713aSLionel Sambuc     // GNU case range extension.
665f4a2713aSLionel Sambuc     SourceLocation DotDotDotLoc;
666f4a2713aSLionel Sambuc     ExprResult RHS;
667*0a6a1f1dSLionel Sambuc     if (TryConsumeToken(tok::ellipsis, DotDotDotLoc)) {
668*0a6a1f1dSLionel Sambuc       Diag(DotDotDotLoc, diag::ext_gnu_case_range);
669f4a2713aSLionel Sambuc       RHS = ParseConstantExpression();
670f4a2713aSLionel Sambuc       if (RHS.isInvalid()) {
671*0a6a1f1dSLionel Sambuc         if (SkipUntil(tok::colon, tok::r_brace, StopAtSemi | StopBeforeMatch)) {
672*0a6a1f1dSLionel Sambuc           TryConsumeToken(tok::colon, ColonLoc);
673*0a6a1f1dSLionel Sambuc           continue;
674*0a6a1f1dSLionel Sambuc         }
675f4a2713aSLionel Sambuc         return StmtError();
676f4a2713aSLionel Sambuc       }
677f4a2713aSLionel Sambuc     }
678f4a2713aSLionel Sambuc 
679f4a2713aSLionel Sambuc     ColonProtection.restore();
680f4a2713aSLionel Sambuc 
681*0a6a1f1dSLionel Sambuc     if (TryConsumeToken(tok::colon, ColonLoc)) {
682*0a6a1f1dSLionel Sambuc     } else if (TryConsumeToken(tok::semi, ColonLoc) ||
683*0a6a1f1dSLionel Sambuc                TryConsumeToken(tok::coloncolon, ColonLoc)) {
684*0a6a1f1dSLionel Sambuc       // Treat "case blah;" or "case blah::" as a typo for "case blah:".
685*0a6a1f1dSLionel Sambuc       Diag(ColonLoc, diag::err_expected_after)
686*0a6a1f1dSLionel Sambuc           << "'case'" << tok::colon
687f4a2713aSLionel Sambuc           << FixItHint::CreateReplacement(ColonLoc, ":");
688f4a2713aSLionel Sambuc     } else {
689f4a2713aSLionel Sambuc       SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
690*0a6a1f1dSLionel Sambuc       Diag(ExpectedLoc, diag::err_expected_after)
691*0a6a1f1dSLionel Sambuc           << "'case'" << tok::colon
692f4a2713aSLionel Sambuc           << FixItHint::CreateInsertion(ExpectedLoc, ":");
693f4a2713aSLionel Sambuc       ColonLoc = ExpectedLoc;
694f4a2713aSLionel Sambuc     }
695f4a2713aSLionel Sambuc 
696f4a2713aSLionel Sambuc     StmtResult Case =
697f4a2713aSLionel Sambuc       Actions.ActOnCaseStmt(CaseLoc, LHS.get(), DotDotDotLoc,
698f4a2713aSLionel Sambuc                             RHS.get(), ColonLoc);
699f4a2713aSLionel Sambuc 
700f4a2713aSLionel Sambuc     // If we had a sema error parsing this case, then just ignore it and
701f4a2713aSLionel Sambuc     // continue parsing the sub-stmt.
702f4a2713aSLionel Sambuc     if (Case.isInvalid()) {
703f4a2713aSLionel Sambuc       if (TopLevelCase.isInvalid())  // No parsed case stmts.
704f4a2713aSLionel Sambuc         return ParseStatement();
705f4a2713aSLionel Sambuc       // Otherwise, just don't add it as a nested case.
706f4a2713aSLionel Sambuc     } else {
707f4a2713aSLionel Sambuc       // If this is the first case statement we parsed, it becomes TopLevelCase.
708f4a2713aSLionel Sambuc       // Otherwise we link it into the current chain.
709f4a2713aSLionel Sambuc       Stmt *NextDeepest = Case.get();
710f4a2713aSLionel Sambuc       if (TopLevelCase.isInvalid())
711f4a2713aSLionel Sambuc         TopLevelCase = Case;
712f4a2713aSLionel Sambuc       else
713f4a2713aSLionel Sambuc         Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, Case.get());
714f4a2713aSLionel Sambuc       DeepestParsedCaseStmt = NextDeepest;
715f4a2713aSLionel Sambuc     }
716f4a2713aSLionel Sambuc 
717f4a2713aSLionel Sambuc     // Handle all case statements.
718f4a2713aSLionel Sambuc   } while (Tok.is(tok::kw_case));
719f4a2713aSLionel Sambuc 
720f4a2713aSLionel Sambuc   // If we found a non-case statement, start by parsing it.
721f4a2713aSLionel Sambuc   StmtResult SubStmt;
722f4a2713aSLionel Sambuc 
723f4a2713aSLionel Sambuc   if (Tok.isNot(tok::r_brace)) {
724f4a2713aSLionel Sambuc     SubStmt = ParseStatement();
725f4a2713aSLionel Sambuc   } else {
726f4a2713aSLionel Sambuc     // Nicely diagnose the common error "switch (X) { case 4: }", which is
727*0a6a1f1dSLionel Sambuc     // not valid.  If ColonLoc doesn't point to a valid text location, there was
728*0a6a1f1dSLionel Sambuc     // another parsing error, so avoid producing extra diagnostics.
729*0a6a1f1dSLionel Sambuc     if (ColonLoc.isValid()) {
730f4a2713aSLionel Sambuc       SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
731f4a2713aSLionel Sambuc       Diag(AfterColonLoc, diag::err_label_end_of_compound_statement)
732f4a2713aSLionel Sambuc         << FixItHint::CreateInsertion(AfterColonLoc, " ;");
733*0a6a1f1dSLionel Sambuc     }
734*0a6a1f1dSLionel Sambuc     SubStmt = StmtError();
735f4a2713aSLionel Sambuc   }
736f4a2713aSLionel Sambuc 
737*0a6a1f1dSLionel Sambuc   // Install the body into the most deeply-nested case.
738*0a6a1f1dSLionel Sambuc   if (DeepestParsedCaseStmt) {
739f4a2713aSLionel Sambuc     // Broken sub-stmt shouldn't prevent forming the case statement properly.
740f4a2713aSLionel Sambuc     if (SubStmt.isInvalid())
741f4a2713aSLionel Sambuc       SubStmt = Actions.ActOnNullStmt(SourceLocation());
742f4a2713aSLionel Sambuc     Actions.ActOnCaseStmtBody(DeepestParsedCaseStmt, SubStmt.get());
743*0a6a1f1dSLionel Sambuc   }
744f4a2713aSLionel Sambuc 
745f4a2713aSLionel Sambuc   // Return the top level parsed statement tree.
746f4a2713aSLionel Sambuc   return TopLevelCase;
747f4a2713aSLionel Sambuc }
748f4a2713aSLionel Sambuc 
749f4a2713aSLionel Sambuc /// ParseDefaultStatement
750f4a2713aSLionel Sambuc ///       labeled-statement:
751f4a2713aSLionel Sambuc ///         'default' ':' statement
752f4a2713aSLionel Sambuc /// Note that this does not parse the 'statement' at the end.
753f4a2713aSLionel Sambuc ///
ParseDefaultStatement()754f4a2713aSLionel Sambuc StmtResult Parser::ParseDefaultStatement() {
755f4a2713aSLionel Sambuc   assert(Tok.is(tok::kw_default) && "Not a default stmt!");
756f4a2713aSLionel Sambuc   SourceLocation DefaultLoc = ConsumeToken();  // eat the 'default'.
757f4a2713aSLionel Sambuc 
758f4a2713aSLionel Sambuc   SourceLocation ColonLoc;
759*0a6a1f1dSLionel Sambuc   if (TryConsumeToken(tok::colon, ColonLoc)) {
760*0a6a1f1dSLionel Sambuc   } else if (TryConsumeToken(tok::semi, ColonLoc)) {
761f4a2713aSLionel Sambuc     // Treat "default;" as a typo for "default:".
762*0a6a1f1dSLionel Sambuc     Diag(ColonLoc, diag::err_expected_after)
763*0a6a1f1dSLionel Sambuc         << "'default'" << tok::colon
764f4a2713aSLionel Sambuc         << FixItHint::CreateReplacement(ColonLoc, ":");
765f4a2713aSLionel Sambuc   } else {
766f4a2713aSLionel Sambuc     SourceLocation ExpectedLoc = PP.getLocForEndOfToken(PrevTokLocation);
767*0a6a1f1dSLionel Sambuc     Diag(ExpectedLoc, diag::err_expected_after)
768*0a6a1f1dSLionel Sambuc         << "'default'" << tok::colon
769f4a2713aSLionel Sambuc         << FixItHint::CreateInsertion(ExpectedLoc, ":");
770f4a2713aSLionel Sambuc     ColonLoc = ExpectedLoc;
771f4a2713aSLionel Sambuc   }
772f4a2713aSLionel Sambuc 
773f4a2713aSLionel Sambuc   StmtResult SubStmt;
774f4a2713aSLionel Sambuc 
775f4a2713aSLionel Sambuc   if (Tok.isNot(tok::r_brace)) {
776f4a2713aSLionel Sambuc     SubStmt = ParseStatement();
777f4a2713aSLionel Sambuc   } else {
778f4a2713aSLionel Sambuc     // Diagnose the common error "switch (X) {... default: }", which is
779f4a2713aSLionel Sambuc     // not valid.
780f4a2713aSLionel Sambuc     SourceLocation AfterColonLoc = PP.getLocForEndOfToken(ColonLoc);
781f4a2713aSLionel Sambuc     Diag(AfterColonLoc, diag::err_label_end_of_compound_statement)
782f4a2713aSLionel Sambuc       << FixItHint::CreateInsertion(AfterColonLoc, " ;");
783f4a2713aSLionel Sambuc     SubStmt = true;
784f4a2713aSLionel Sambuc   }
785f4a2713aSLionel Sambuc 
786f4a2713aSLionel Sambuc   // Broken sub-stmt shouldn't prevent forming the case statement properly.
787f4a2713aSLionel Sambuc   if (SubStmt.isInvalid())
788f4a2713aSLionel Sambuc     SubStmt = Actions.ActOnNullStmt(ColonLoc);
789f4a2713aSLionel Sambuc 
790f4a2713aSLionel Sambuc   return Actions.ActOnDefaultStmt(DefaultLoc, ColonLoc,
791f4a2713aSLionel Sambuc                                   SubStmt.get(), getCurScope());
792f4a2713aSLionel Sambuc }
793f4a2713aSLionel Sambuc 
ParseCompoundStatement(bool isStmtExpr)794f4a2713aSLionel Sambuc StmtResult Parser::ParseCompoundStatement(bool isStmtExpr) {
795f4a2713aSLionel Sambuc   return ParseCompoundStatement(isStmtExpr, Scope::DeclScope);
796f4a2713aSLionel Sambuc }
797f4a2713aSLionel Sambuc 
798f4a2713aSLionel Sambuc /// ParseCompoundStatement - Parse a "{}" block.
799f4a2713aSLionel Sambuc ///
800f4a2713aSLionel Sambuc ///       compound-statement: [C99 6.8.2]
801f4a2713aSLionel Sambuc ///         { block-item-list[opt] }
802f4a2713aSLionel Sambuc /// [GNU]   { label-declarations block-item-list } [TODO]
803f4a2713aSLionel Sambuc ///
804f4a2713aSLionel Sambuc ///       block-item-list:
805f4a2713aSLionel Sambuc ///         block-item
806f4a2713aSLionel Sambuc ///         block-item-list block-item
807f4a2713aSLionel Sambuc ///
808f4a2713aSLionel Sambuc ///       block-item:
809f4a2713aSLionel Sambuc ///         declaration
810f4a2713aSLionel Sambuc /// [GNU]   '__extension__' declaration
811f4a2713aSLionel Sambuc ///         statement
812f4a2713aSLionel Sambuc ///
813f4a2713aSLionel Sambuc /// [GNU] label-declarations:
814f4a2713aSLionel Sambuc /// [GNU]   label-declaration
815f4a2713aSLionel Sambuc /// [GNU]   label-declarations label-declaration
816f4a2713aSLionel Sambuc ///
817f4a2713aSLionel Sambuc /// [GNU] label-declaration:
818f4a2713aSLionel Sambuc /// [GNU]   '__label__' identifier-list ';'
819f4a2713aSLionel Sambuc ///
ParseCompoundStatement(bool isStmtExpr,unsigned ScopeFlags)820f4a2713aSLionel Sambuc StmtResult Parser::ParseCompoundStatement(bool isStmtExpr,
821f4a2713aSLionel Sambuc                                           unsigned ScopeFlags) {
822f4a2713aSLionel Sambuc   assert(Tok.is(tok::l_brace) && "Not a compount stmt!");
823f4a2713aSLionel Sambuc 
824f4a2713aSLionel Sambuc   // Enter a scope to hold everything within the compound stmt.  Compound
825f4a2713aSLionel Sambuc   // statements can always hold declarations.
826f4a2713aSLionel Sambuc   ParseScope CompoundScope(this, ScopeFlags);
827f4a2713aSLionel Sambuc 
828f4a2713aSLionel Sambuc   // Parse the statements in the body.
829f4a2713aSLionel Sambuc   return ParseCompoundStatementBody(isStmtExpr);
830f4a2713aSLionel Sambuc }
831f4a2713aSLionel Sambuc 
832f4a2713aSLionel Sambuc /// Parse any pragmas at the start of the compound expression. We handle these
833f4a2713aSLionel Sambuc /// separately since some pragmas (FP_CONTRACT) must appear before any C
834f4a2713aSLionel Sambuc /// statement in the compound, but may be intermingled with other pragmas.
ParseCompoundStatementLeadingPragmas()835f4a2713aSLionel Sambuc void Parser::ParseCompoundStatementLeadingPragmas() {
836f4a2713aSLionel Sambuc   bool checkForPragmas = true;
837f4a2713aSLionel Sambuc   while (checkForPragmas) {
838f4a2713aSLionel Sambuc     switch (Tok.getKind()) {
839f4a2713aSLionel Sambuc     case tok::annot_pragma_vis:
840f4a2713aSLionel Sambuc       HandlePragmaVisibility();
841f4a2713aSLionel Sambuc       break;
842f4a2713aSLionel Sambuc     case tok::annot_pragma_pack:
843f4a2713aSLionel Sambuc       HandlePragmaPack();
844f4a2713aSLionel Sambuc       break;
845f4a2713aSLionel Sambuc     case tok::annot_pragma_msstruct:
846f4a2713aSLionel Sambuc       HandlePragmaMSStruct();
847f4a2713aSLionel Sambuc       break;
848f4a2713aSLionel Sambuc     case tok::annot_pragma_align:
849f4a2713aSLionel Sambuc       HandlePragmaAlign();
850f4a2713aSLionel Sambuc       break;
851f4a2713aSLionel Sambuc     case tok::annot_pragma_weak:
852f4a2713aSLionel Sambuc       HandlePragmaWeak();
853f4a2713aSLionel Sambuc       break;
854f4a2713aSLionel Sambuc     case tok::annot_pragma_weakalias:
855f4a2713aSLionel Sambuc       HandlePragmaWeakAlias();
856f4a2713aSLionel Sambuc       break;
857f4a2713aSLionel Sambuc     case tok::annot_pragma_redefine_extname:
858f4a2713aSLionel Sambuc       HandlePragmaRedefineExtname();
859f4a2713aSLionel Sambuc       break;
860f4a2713aSLionel Sambuc     case tok::annot_pragma_opencl_extension:
861f4a2713aSLionel Sambuc       HandlePragmaOpenCLExtension();
862f4a2713aSLionel Sambuc       break;
863f4a2713aSLionel Sambuc     case tok::annot_pragma_fp_contract:
864f4a2713aSLionel Sambuc       HandlePragmaFPContract();
865f4a2713aSLionel Sambuc       break;
866*0a6a1f1dSLionel Sambuc     case tok::annot_pragma_ms_pointers_to_members:
867*0a6a1f1dSLionel Sambuc       HandlePragmaMSPointersToMembers();
868*0a6a1f1dSLionel Sambuc       break;
869*0a6a1f1dSLionel Sambuc     case tok::annot_pragma_ms_pragma:
870*0a6a1f1dSLionel Sambuc       HandlePragmaMSPragma();
871*0a6a1f1dSLionel Sambuc       break;
872f4a2713aSLionel Sambuc     default:
873f4a2713aSLionel Sambuc       checkForPragmas = false;
874f4a2713aSLionel Sambuc       break;
875f4a2713aSLionel Sambuc     }
876f4a2713aSLionel Sambuc   }
877f4a2713aSLionel Sambuc 
878f4a2713aSLionel Sambuc }
879f4a2713aSLionel Sambuc 
880f4a2713aSLionel Sambuc /// ParseCompoundStatementBody - Parse a sequence of statements and invoke the
881f4a2713aSLionel Sambuc /// ActOnCompoundStmt action.  This expects the '{' to be the current token, and
882f4a2713aSLionel Sambuc /// consume the '}' at the end of the block.  It does not manipulate the scope
883f4a2713aSLionel Sambuc /// stack.
ParseCompoundStatementBody(bool isStmtExpr)884f4a2713aSLionel Sambuc StmtResult Parser::ParseCompoundStatementBody(bool isStmtExpr) {
885f4a2713aSLionel Sambuc   PrettyStackTraceLoc CrashInfo(PP.getSourceManager(),
886f4a2713aSLionel Sambuc                                 Tok.getLocation(),
887f4a2713aSLionel Sambuc                                 "in compound statement ('{}')");
888f4a2713aSLionel Sambuc 
889f4a2713aSLionel Sambuc   // Record the state of the FP_CONTRACT pragma, restore on leaving the
890f4a2713aSLionel Sambuc   // compound statement.
891f4a2713aSLionel Sambuc   Sema::FPContractStateRAII SaveFPContractState(Actions);
892f4a2713aSLionel Sambuc 
893f4a2713aSLionel Sambuc   InMessageExpressionRAIIObject InMessage(*this, false);
894f4a2713aSLionel Sambuc   BalancedDelimiterTracker T(*this, tok::l_brace);
895f4a2713aSLionel Sambuc   if (T.consumeOpen())
896f4a2713aSLionel Sambuc     return StmtError();
897f4a2713aSLionel Sambuc 
898f4a2713aSLionel Sambuc   Sema::CompoundScopeRAII CompoundScope(Actions);
899f4a2713aSLionel Sambuc 
900f4a2713aSLionel Sambuc   // Parse any pragmas at the beginning of the compound statement.
901f4a2713aSLionel Sambuc   ParseCompoundStatementLeadingPragmas();
902f4a2713aSLionel Sambuc 
903f4a2713aSLionel Sambuc   StmtVector Stmts;
904f4a2713aSLionel Sambuc 
905f4a2713aSLionel Sambuc   // "__label__ X, Y, Z;" is the GNU "Local Label" extension.  These are
906f4a2713aSLionel Sambuc   // only allowed at the start of a compound stmt regardless of the language.
907f4a2713aSLionel Sambuc   while (Tok.is(tok::kw___label__)) {
908f4a2713aSLionel Sambuc     SourceLocation LabelLoc = ConsumeToken();
909f4a2713aSLionel Sambuc 
910f4a2713aSLionel Sambuc     SmallVector<Decl *, 8> DeclsInGroup;
911f4a2713aSLionel Sambuc     while (1) {
912f4a2713aSLionel Sambuc       if (Tok.isNot(tok::identifier)) {
913*0a6a1f1dSLionel Sambuc         Diag(Tok, diag::err_expected) << tok::identifier;
914f4a2713aSLionel Sambuc         break;
915f4a2713aSLionel Sambuc       }
916f4a2713aSLionel Sambuc 
917f4a2713aSLionel Sambuc       IdentifierInfo *II = Tok.getIdentifierInfo();
918f4a2713aSLionel Sambuc       SourceLocation IdLoc = ConsumeToken();
919f4a2713aSLionel Sambuc       DeclsInGroup.push_back(Actions.LookupOrCreateLabel(II, IdLoc, LabelLoc));
920f4a2713aSLionel Sambuc 
921*0a6a1f1dSLionel Sambuc       if (!TryConsumeToken(tok::comma))
922f4a2713aSLionel Sambuc         break;
923f4a2713aSLionel Sambuc     }
924f4a2713aSLionel Sambuc 
925f4a2713aSLionel Sambuc     DeclSpec DS(AttrFactory);
926f4a2713aSLionel Sambuc     DeclGroupPtrTy Res =
927f4a2713aSLionel Sambuc         Actions.FinalizeDeclaratorGroup(getCurScope(), DS, DeclsInGroup);
928f4a2713aSLionel Sambuc     StmtResult R = Actions.ActOnDeclStmt(Res, LabelLoc, Tok.getLocation());
929f4a2713aSLionel Sambuc 
930f4a2713aSLionel Sambuc     ExpectAndConsumeSemi(diag::err_expected_semi_declaration);
931f4a2713aSLionel Sambuc     if (R.isUsable())
932*0a6a1f1dSLionel Sambuc       Stmts.push_back(R.get());
933f4a2713aSLionel Sambuc   }
934f4a2713aSLionel Sambuc 
935*0a6a1f1dSLionel Sambuc   while (Tok.isNot(tok::r_brace) && !isEofOrEom()) {
936f4a2713aSLionel Sambuc     if (Tok.is(tok::annot_pragma_unused)) {
937f4a2713aSLionel Sambuc       HandlePragmaUnused();
938f4a2713aSLionel Sambuc       continue;
939f4a2713aSLionel Sambuc     }
940f4a2713aSLionel Sambuc 
941f4a2713aSLionel Sambuc     StmtResult R;
942f4a2713aSLionel Sambuc     if (Tok.isNot(tok::kw___extension__)) {
943f4a2713aSLionel Sambuc       R = ParseStatementOrDeclaration(Stmts, false);
944f4a2713aSLionel Sambuc     } else {
945f4a2713aSLionel Sambuc       // __extension__ can start declarations and it can also be a unary
946f4a2713aSLionel Sambuc       // operator for expressions.  Consume multiple __extension__ markers here
947f4a2713aSLionel Sambuc       // until we can determine which is which.
948f4a2713aSLionel Sambuc       // FIXME: This loses extension expressions in the AST!
949f4a2713aSLionel Sambuc       SourceLocation ExtLoc = ConsumeToken();
950f4a2713aSLionel Sambuc       while (Tok.is(tok::kw___extension__))
951f4a2713aSLionel Sambuc         ConsumeToken();
952f4a2713aSLionel Sambuc 
953f4a2713aSLionel Sambuc       ParsedAttributesWithRange attrs(AttrFactory);
954*0a6a1f1dSLionel Sambuc       MaybeParseCXX11Attributes(attrs, nullptr,
955*0a6a1f1dSLionel Sambuc                                 /*MightBeObjCMessageSend*/ true);
956f4a2713aSLionel Sambuc 
957f4a2713aSLionel Sambuc       // If this is the start of a declaration, parse it as such.
958f4a2713aSLionel Sambuc       if (isDeclarationStatement()) {
959f4a2713aSLionel Sambuc         // __extension__ silences extension warnings in the subdeclaration.
960f4a2713aSLionel Sambuc         // FIXME: Save the __extension__ on the decl as a node somehow?
961f4a2713aSLionel Sambuc         ExtensionRAIIObject O(Diags);
962f4a2713aSLionel Sambuc 
963f4a2713aSLionel Sambuc         SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
964*0a6a1f1dSLionel Sambuc         DeclGroupPtrTy Res = ParseDeclaration(Declarator::BlockContext, DeclEnd,
965f4a2713aSLionel Sambuc                                               attrs);
966f4a2713aSLionel Sambuc         R = Actions.ActOnDeclStmt(Res, DeclStart, DeclEnd);
967f4a2713aSLionel Sambuc       } else {
968f4a2713aSLionel Sambuc         // Otherwise this was a unary __extension__ marker.
969f4a2713aSLionel Sambuc         ExprResult Res(ParseExpressionWithLeadingExtension(ExtLoc));
970f4a2713aSLionel Sambuc 
971f4a2713aSLionel Sambuc         if (Res.isInvalid()) {
972f4a2713aSLionel Sambuc           SkipUntil(tok::semi);
973f4a2713aSLionel Sambuc           continue;
974f4a2713aSLionel Sambuc         }
975f4a2713aSLionel Sambuc 
976f4a2713aSLionel Sambuc         // FIXME: Use attributes?
977f4a2713aSLionel Sambuc         // Eat the semicolon at the end of stmt and convert the expr into a
978f4a2713aSLionel Sambuc         // statement.
979f4a2713aSLionel Sambuc         ExpectAndConsumeSemi(diag::err_expected_semi_after_expr);
980f4a2713aSLionel Sambuc         R = Actions.ActOnExprStmt(Res);
981f4a2713aSLionel Sambuc       }
982f4a2713aSLionel Sambuc     }
983f4a2713aSLionel Sambuc 
984f4a2713aSLionel Sambuc     if (R.isUsable())
985*0a6a1f1dSLionel Sambuc       Stmts.push_back(R.get());
986f4a2713aSLionel Sambuc   }
987f4a2713aSLionel Sambuc 
988f4a2713aSLionel Sambuc   SourceLocation CloseLoc = Tok.getLocation();
989f4a2713aSLionel Sambuc 
990f4a2713aSLionel Sambuc   // We broke out of the while loop because we found a '}' or EOF.
991f4a2713aSLionel Sambuc   if (!T.consumeClose())
992f4a2713aSLionel Sambuc     // Recover by creating a compound statement with what we parsed so far,
993f4a2713aSLionel Sambuc     // instead of dropping everything and returning StmtError();
994f4a2713aSLionel Sambuc     CloseLoc = T.getCloseLocation();
995f4a2713aSLionel Sambuc 
996f4a2713aSLionel Sambuc   return Actions.ActOnCompoundStmt(T.getOpenLocation(), CloseLoc,
997f4a2713aSLionel Sambuc                                    Stmts, isStmtExpr);
998f4a2713aSLionel Sambuc }
999f4a2713aSLionel Sambuc 
1000f4a2713aSLionel Sambuc /// ParseParenExprOrCondition:
1001f4a2713aSLionel Sambuc /// [C  ]     '(' expression ')'
1002f4a2713aSLionel Sambuc /// [C++]     '(' condition ')'       [not allowed if OnlyAllowCondition=true]
1003f4a2713aSLionel Sambuc ///
1004f4a2713aSLionel Sambuc /// This function parses and performs error recovery on the specified condition
1005f4a2713aSLionel Sambuc /// or expression (depending on whether we're in C++ or C mode).  This function
1006f4a2713aSLionel Sambuc /// goes out of its way to recover well.  It returns true if there was a parser
1007f4a2713aSLionel Sambuc /// error (the right paren couldn't be found), which indicates that the caller
1008f4a2713aSLionel Sambuc /// should try to recover harder.  It returns false if the condition is
1009f4a2713aSLionel Sambuc /// successfully parsed.  Note that a successful parse can still have semantic
1010f4a2713aSLionel Sambuc /// errors in the condition.
ParseParenExprOrCondition(ExprResult & ExprResult,Decl * & DeclResult,SourceLocation Loc,bool ConvertToBoolean)1011f4a2713aSLionel Sambuc bool Parser::ParseParenExprOrCondition(ExprResult &ExprResult,
1012f4a2713aSLionel Sambuc                                        Decl *&DeclResult,
1013f4a2713aSLionel Sambuc                                        SourceLocation Loc,
1014f4a2713aSLionel Sambuc                                        bool ConvertToBoolean) {
1015f4a2713aSLionel Sambuc   BalancedDelimiterTracker T(*this, tok::l_paren);
1016f4a2713aSLionel Sambuc   T.consumeOpen();
1017f4a2713aSLionel Sambuc 
1018f4a2713aSLionel Sambuc   if (getLangOpts().CPlusPlus)
1019f4a2713aSLionel Sambuc     ParseCXXCondition(ExprResult, DeclResult, Loc, ConvertToBoolean);
1020f4a2713aSLionel Sambuc   else {
1021f4a2713aSLionel Sambuc     ExprResult = ParseExpression();
1022*0a6a1f1dSLionel Sambuc     DeclResult = nullptr;
1023f4a2713aSLionel Sambuc 
1024f4a2713aSLionel Sambuc     // If required, convert to a boolean value.
1025f4a2713aSLionel Sambuc     if (!ExprResult.isInvalid() && ConvertToBoolean)
1026f4a2713aSLionel Sambuc       ExprResult
1027f4a2713aSLionel Sambuc         = Actions.ActOnBooleanCondition(getCurScope(), Loc, ExprResult.get());
1028f4a2713aSLionel Sambuc   }
1029f4a2713aSLionel Sambuc 
1030f4a2713aSLionel Sambuc   // If the parser was confused by the condition and we don't have a ')', try to
1031f4a2713aSLionel Sambuc   // recover by skipping ahead to a semi and bailing out.  If condexp is
1032f4a2713aSLionel Sambuc   // semantically invalid but we have well formed code, keep going.
1033f4a2713aSLionel Sambuc   if (ExprResult.isInvalid() && !DeclResult && Tok.isNot(tok::r_paren)) {
1034f4a2713aSLionel Sambuc     SkipUntil(tok::semi);
1035f4a2713aSLionel Sambuc     // Skipping may have stopped if it found the containing ')'.  If so, we can
1036f4a2713aSLionel Sambuc     // continue parsing the if statement.
1037f4a2713aSLionel Sambuc     if (Tok.isNot(tok::r_paren))
1038f4a2713aSLionel Sambuc       return true;
1039f4a2713aSLionel Sambuc   }
1040f4a2713aSLionel Sambuc 
1041f4a2713aSLionel Sambuc   // Otherwise the condition is valid or the rparen is present.
1042f4a2713aSLionel Sambuc   T.consumeClose();
1043f4a2713aSLionel Sambuc 
1044f4a2713aSLionel Sambuc   // Check for extraneous ')'s to catch things like "if (foo())) {".  We know
1045f4a2713aSLionel Sambuc   // that all callers are looking for a statement after the condition, so ")"
1046f4a2713aSLionel Sambuc   // isn't valid.
1047f4a2713aSLionel Sambuc   while (Tok.is(tok::r_paren)) {
1048f4a2713aSLionel Sambuc     Diag(Tok, diag::err_extraneous_rparen_in_condition)
1049f4a2713aSLionel Sambuc       << FixItHint::CreateRemoval(Tok.getLocation());
1050f4a2713aSLionel Sambuc     ConsumeParen();
1051f4a2713aSLionel Sambuc   }
1052f4a2713aSLionel Sambuc 
1053f4a2713aSLionel Sambuc   return false;
1054f4a2713aSLionel Sambuc }
1055f4a2713aSLionel Sambuc 
1056f4a2713aSLionel Sambuc 
1057f4a2713aSLionel Sambuc /// ParseIfStatement
1058f4a2713aSLionel Sambuc ///       if-statement: [C99 6.8.4.1]
1059f4a2713aSLionel Sambuc ///         'if' '(' expression ')' statement
1060f4a2713aSLionel Sambuc ///         'if' '(' expression ')' statement 'else' statement
1061f4a2713aSLionel Sambuc /// [C++]   'if' '(' condition ')' statement
1062f4a2713aSLionel Sambuc /// [C++]   'if' '(' condition ')' statement 'else' statement
1063f4a2713aSLionel Sambuc ///
ParseIfStatement(SourceLocation * TrailingElseLoc)1064f4a2713aSLionel Sambuc StmtResult Parser::ParseIfStatement(SourceLocation *TrailingElseLoc) {
1065f4a2713aSLionel Sambuc   assert(Tok.is(tok::kw_if) && "Not an if stmt!");
1066f4a2713aSLionel Sambuc   SourceLocation IfLoc = ConsumeToken();  // eat the 'if'.
1067f4a2713aSLionel Sambuc 
1068f4a2713aSLionel Sambuc   if (Tok.isNot(tok::l_paren)) {
1069f4a2713aSLionel Sambuc     Diag(Tok, diag::err_expected_lparen_after) << "if";
1070f4a2713aSLionel Sambuc     SkipUntil(tok::semi);
1071f4a2713aSLionel Sambuc     return StmtError();
1072f4a2713aSLionel Sambuc   }
1073f4a2713aSLionel Sambuc 
1074f4a2713aSLionel Sambuc   bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
1075f4a2713aSLionel Sambuc 
1076f4a2713aSLionel Sambuc   // C99 6.8.4p3 - In C99, the if statement is a block.  This is not
1077f4a2713aSLionel Sambuc   // the case for C90.
1078f4a2713aSLionel Sambuc   //
1079f4a2713aSLionel Sambuc   // C++ 6.4p3:
1080f4a2713aSLionel Sambuc   // A name introduced by a declaration in a condition is in scope from its
1081f4a2713aSLionel Sambuc   // point of declaration until the end of the substatements controlled by the
1082f4a2713aSLionel Sambuc   // condition.
1083f4a2713aSLionel Sambuc   // C++ 3.3.2p4:
1084f4a2713aSLionel Sambuc   // Names declared in the for-init-statement, and in the condition of if,
1085f4a2713aSLionel Sambuc   // while, for, and switch statements are local to the if, while, for, or
1086f4a2713aSLionel Sambuc   // switch statement (including the controlled statement).
1087f4a2713aSLionel Sambuc   //
1088f4a2713aSLionel Sambuc   ParseScope IfScope(this, Scope::DeclScope | Scope::ControlScope, C99orCXX);
1089f4a2713aSLionel Sambuc 
1090f4a2713aSLionel Sambuc   // Parse the condition.
1091f4a2713aSLionel Sambuc   ExprResult CondExp;
1092*0a6a1f1dSLionel Sambuc   Decl *CondVar = nullptr;
1093f4a2713aSLionel Sambuc   if (ParseParenExprOrCondition(CondExp, CondVar, IfLoc, true))
1094f4a2713aSLionel Sambuc     return StmtError();
1095f4a2713aSLionel Sambuc 
1096f4a2713aSLionel Sambuc   FullExprArg FullCondExp(Actions.MakeFullExpr(CondExp.get(), IfLoc));
1097f4a2713aSLionel Sambuc 
1098f4a2713aSLionel Sambuc   // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
1099f4a2713aSLionel Sambuc   // there is no compound stmt.  C90 does not have this clause.  We only do this
1100f4a2713aSLionel Sambuc   // if the body isn't a compound statement to avoid push/pop in common cases.
1101f4a2713aSLionel Sambuc   //
1102f4a2713aSLionel Sambuc   // C++ 6.4p1:
1103f4a2713aSLionel Sambuc   // The substatement in a selection-statement (each substatement, in the else
1104f4a2713aSLionel Sambuc   // form of the if statement) implicitly defines a local scope.
1105f4a2713aSLionel Sambuc   //
1106f4a2713aSLionel Sambuc   // For C++ we create a scope for the condition and a new scope for
1107f4a2713aSLionel Sambuc   // substatements because:
1108f4a2713aSLionel Sambuc   // -When the 'then' scope exits, we want the condition declaration to still be
1109f4a2713aSLionel Sambuc   //    active for the 'else' scope too.
1110f4a2713aSLionel Sambuc   // -Sema will detect name clashes by considering declarations of a
1111f4a2713aSLionel Sambuc   //    'ControlScope' as part of its direct subscope.
1112f4a2713aSLionel Sambuc   // -If we wanted the condition and substatement to be in the same scope, we
1113f4a2713aSLionel Sambuc   //    would have to notify ParseStatement not to create a new scope. It's
1114f4a2713aSLionel Sambuc   //    simpler to let it create a new scope.
1115f4a2713aSLionel Sambuc   //
1116*0a6a1f1dSLionel Sambuc   ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
1117f4a2713aSLionel Sambuc 
1118f4a2713aSLionel Sambuc   // Read the 'then' stmt.
1119f4a2713aSLionel Sambuc   SourceLocation ThenStmtLoc = Tok.getLocation();
1120f4a2713aSLionel Sambuc 
1121f4a2713aSLionel Sambuc   SourceLocation InnerStatementTrailingElseLoc;
1122f4a2713aSLionel Sambuc   StmtResult ThenStmt(ParseStatement(&InnerStatementTrailingElseLoc));
1123f4a2713aSLionel Sambuc 
1124f4a2713aSLionel Sambuc   // Pop the 'if' scope if needed.
1125f4a2713aSLionel Sambuc   InnerScope.Exit();
1126f4a2713aSLionel Sambuc 
1127f4a2713aSLionel Sambuc   // If it has an else, parse it.
1128f4a2713aSLionel Sambuc   SourceLocation ElseLoc;
1129f4a2713aSLionel Sambuc   SourceLocation ElseStmtLoc;
1130f4a2713aSLionel Sambuc   StmtResult ElseStmt;
1131f4a2713aSLionel Sambuc 
1132f4a2713aSLionel Sambuc   if (Tok.is(tok::kw_else)) {
1133f4a2713aSLionel Sambuc     if (TrailingElseLoc)
1134f4a2713aSLionel Sambuc       *TrailingElseLoc = Tok.getLocation();
1135f4a2713aSLionel Sambuc 
1136f4a2713aSLionel Sambuc     ElseLoc = ConsumeToken();
1137f4a2713aSLionel Sambuc     ElseStmtLoc = Tok.getLocation();
1138f4a2713aSLionel Sambuc 
1139f4a2713aSLionel Sambuc     // C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
1140f4a2713aSLionel Sambuc     // there is no compound stmt.  C90 does not have this clause.  We only do
1141f4a2713aSLionel Sambuc     // this if the body isn't a compound statement to avoid push/pop in common
1142f4a2713aSLionel Sambuc     // cases.
1143f4a2713aSLionel Sambuc     //
1144f4a2713aSLionel Sambuc     // C++ 6.4p1:
1145f4a2713aSLionel Sambuc     // The substatement in a selection-statement (each substatement, in the else
1146f4a2713aSLionel Sambuc     // form of the if statement) implicitly defines a local scope.
1147f4a2713aSLionel Sambuc     //
1148*0a6a1f1dSLionel Sambuc     ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
1149f4a2713aSLionel Sambuc 
1150f4a2713aSLionel Sambuc     ElseStmt = ParseStatement();
1151f4a2713aSLionel Sambuc 
1152f4a2713aSLionel Sambuc     // Pop the 'else' scope if needed.
1153f4a2713aSLionel Sambuc     InnerScope.Exit();
1154f4a2713aSLionel Sambuc   } else if (Tok.is(tok::code_completion)) {
1155f4a2713aSLionel Sambuc     Actions.CodeCompleteAfterIf(getCurScope());
1156f4a2713aSLionel Sambuc     cutOffParsing();
1157f4a2713aSLionel Sambuc     return StmtError();
1158f4a2713aSLionel Sambuc   } else if (InnerStatementTrailingElseLoc.isValid()) {
1159f4a2713aSLionel Sambuc     Diag(InnerStatementTrailingElseLoc, diag::warn_dangling_else);
1160f4a2713aSLionel Sambuc   }
1161f4a2713aSLionel Sambuc 
1162f4a2713aSLionel Sambuc   IfScope.Exit();
1163f4a2713aSLionel Sambuc 
1164f4a2713aSLionel Sambuc   // If the then or else stmt is invalid and the other is valid (and present),
1165f4a2713aSLionel Sambuc   // make turn the invalid one into a null stmt to avoid dropping the other
1166f4a2713aSLionel Sambuc   // part.  If both are invalid, return error.
1167f4a2713aSLionel Sambuc   if ((ThenStmt.isInvalid() && ElseStmt.isInvalid()) ||
1168*0a6a1f1dSLionel Sambuc       (ThenStmt.isInvalid() && ElseStmt.get() == nullptr) ||
1169*0a6a1f1dSLionel Sambuc       (ThenStmt.get() == nullptr && ElseStmt.isInvalid())) {
1170f4a2713aSLionel Sambuc     // Both invalid, or one is invalid and other is non-present: return error.
1171f4a2713aSLionel Sambuc     return StmtError();
1172f4a2713aSLionel Sambuc   }
1173f4a2713aSLionel Sambuc 
1174f4a2713aSLionel Sambuc   // Now if either are invalid, replace with a ';'.
1175f4a2713aSLionel Sambuc   if (ThenStmt.isInvalid())
1176f4a2713aSLionel Sambuc     ThenStmt = Actions.ActOnNullStmt(ThenStmtLoc);
1177f4a2713aSLionel Sambuc   if (ElseStmt.isInvalid())
1178f4a2713aSLionel Sambuc     ElseStmt = Actions.ActOnNullStmt(ElseStmtLoc);
1179f4a2713aSLionel Sambuc 
1180f4a2713aSLionel Sambuc   return Actions.ActOnIfStmt(IfLoc, FullCondExp, CondVar, ThenStmt.get(),
1181f4a2713aSLionel Sambuc                              ElseLoc, ElseStmt.get());
1182f4a2713aSLionel Sambuc }
1183f4a2713aSLionel Sambuc 
1184f4a2713aSLionel Sambuc /// ParseSwitchStatement
1185f4a2713aSLionel Sambuc ///       switch-statement:
1186f4a2713aSLionel Sambuc ///         'switch' '(' expression ')' statement
1187f4a2713aSLionel Sambuc /// [C++]   'switch' '(' condition ')' statement
ParseSwitchStatement(SourceLocation * TrailingElseLoc)1188f4a2713aSLionel Sambuc StmtResult Parser::ParseSwitchStatement(SourceLocation *TrailingElseLoc) {
1189f4a2713aSLionel Sambuc   assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
1190f4a2713aSLionel Sambuc   SourceLocation SwitchLoc = ConsumeToken();  // eat the 'switch'.
1191f4a2713aSLionel Sambuc 
1192f4a2713aSLionel Sambuc   if (Tok.isNot(tok::l_paren)) {
1193f4a2713aSLionel Sambuc     Diag(Tok, diag::err_expected_lparen_after) << "switch";
1194f4a2713aSLionel Sambuc     SkipUntil(tok::semi);
1195f4a2713aSLionel Sambuc     return StmtError();
1196f4a2713aSLionel Sambuc   }
1197f4a2713aSLionel Sambuc 
1198f4a2713aSLionel Sambuc   bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
1199f4a2713aSLionel Sambuc 
1200f4a2713aSLionel Sambuc   // C99 6.8.4p3 - In C99, the switch statement is a block.  This is
1201f4a2713aSLionel Sambuc   // not the case for C90.  Start the switch scope.
1202f4a2713aSLionel Sambuc   //
1203f4a2713aSLionel Sambuc   // C++ 6.4p3:
1204f4a2713aSLionel Sambuc   // A name introduced by a declaration in a condition is in scope from its
1205f4a2713aSLionel Sambuc   // point of declaration until the end of the substatements controlled by the
1206f4a2713aSLionel Sambuc   // condition.
1207f4a2713aSLionel Sambuc   // C++ 3.3.2p4:
1208f4a2713aSLionel Sambuc   // Names declared in the for-init-statement, and in the condition of if,
1209f4a2713aSLionel Sambuc   // while, for, and switch statements are local to the if, while, for, or
1210f4a2713aSLionel Sambuc   // switch statement (including the controlled statement).
1211f4a2713aSLionel Sambuc   //
1212*0a6a1f1dSLionel Sambuc   unsigned ScopeFlags = Scope::SwitchScope;
1213f4a2713aSLionel Sambuc   if (C99orCXX)
1214f4a2713aSLionel Sambuc     ScopeFlags |= Scope::DeclScope | Scope::ControlScope;
1215f4a2713aSLionel Sambuc   ParseScope SwitchScope(this, ScopeFlags);
1216f4a2713aSLionel Sambuc 
1217f4a2713aSLionel Sambuc   // Parse the condition.
1218f4a2713aSLionel Sambuc   ExprResult Cond;
1219*0a6a1f1dSLionel Sambuc   Decl *CondVar = nullptr;
1220f4a2713aSLionel Sambuc   if (ParseParenExprOrCondition(Cond, CondVar, SwitchLoc, false))
1221f4a2713aSLionel Sambuc     return StmtError();
1222f4a2713aSLionel Sambuc 
1223f4a2713aSLionel Sambuc   StmtResult Switch
1224f4a2713aSLionel Sambuc     = Actions.ActOnStartOfSwitchStmt(SwitchLoc, Cond.get(), CondVar);
1225f4a2713aSLionel Sambuc 
1226f4a2713aSLionel Sambuc   if (Switch.isInvalid()) {
1227f4a2713aSLionel Sambuc     // Skip the switch body.
1228f4a2713aSLionel Sambuc     // FIXME: This is not optimal recovery, but parsing the body is more
1229f4a2713aSLionel Sambuc     // dangerous due to the presence of case and default statements, which
1230f4a2713aSLionel Sambuc     // will have no place to connect back with the switch.
1231f4a2713aSLionel Sambuc     if (Tok.is(tok::l_brace)) {
1232f4a2713aSLionel Sambuc       ConsumeBrace();
1233f4a2713aSLionel Sambuc       SkipUntil(tok::r_brace);
1234f4a2713aSLionel Sambuc     } else
1235f4a2713aSLionel Sambuc       SkipUntil(tok::semi);
1236f4a2713aSLionel Sambuc     return Switch;
1237f4a2713aSLionel Sambuc   }
1238f4a2713aSLionel Sambuc 
1239f4a2713aSLionel Sambuc   // C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
1240f4a2713aSLionel Sambuc   // there is no compound stmt.  C90 does not have this clause.  We only do this
1241f4a2713aSLionel Sambuc   // if the body isn't a compound statement to avoid push/pop in common cases.
1242f4a2713aSLionel Sambuc   //
1243f4a2713aSLionel Sambuc   // C++ 6.4p1:
1244f4a2713aSLionel Sambuc   // The substatement in a selection-statement (each substatement, in the else
1245f4a2713aSLionel Sambuc   // form of the if statement) implicitly defines a local scope.
1246f4a2713aSLionel Sambuc   //
1247f4a2713aSLionel Sambuc   // See comments in ParseIfStatement for why we create a scope for the
1248f4a2713aSLionel Sambuc   // condition and a new scope for substatement in C++.
1249f4a2713aSLionel Sambuc   //
1250*0a6a1f1dSLionel Sambuc   getCurScope()->AddFlags(Scope::BreakScope);
1251*0a6a1f1dSLionel Sambuc   ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
1252*0a6a1f1dSLionel Sambuc 
1253*0a6a1f1dSLionel Sambuc   // We have incremented the mangling number for the SwitchScope and the
1254*0a6a1f1dSLionel Sambuc   // InnerScope, which is one too many.
1255*0a6a1f1dSLionel Sambuc   if (C99orCXX)
1256*0a6a1f1dSLionel Sambuc     getCurScope()->decrementMSLocalManglingNumber();
1257f4a2713aSLionel Sambuc 
1258f4a2713aSLionel Sambuc   // Read the body statement.
1259f4a2713aSLionel Sambuc   StmtResult Body(ParseStatement(TrailingElseLoc));
1260f4a2713aSLionel Sambuc 
1261f4a2713aSLionel Sambuc   // Pop the scopes.
1262f4a2713aSLionel Sambuc   InnerScope.Exit();
1263f4a2713aSLionel Sambuc   SwitchScope.Exit();
1264f4a2713aSLionel Sambuc 
1265f4a2713aSLionel Sambuc   return Actions.ActOnFinishSwitchStmt(SwitchLoc, Switch.get(), Body.get());
1266f4a2713aSLionel Sambuc }
1267f4a2713aSLionel Sambuc 
1268f4a2713aSLionel Sambuc /// ParseWhileStatement
1269f4a2713aSLionel Sambuc ///       while-statement: [C99 6.8.5.1]
1270f4a2713aSLionel Sambuc ///         'while' '(' expression ')' statement
1271f4a2713aSLionel Sambuc /// [C++]   'while' '(' condition ')' statement
ParseWhileStatement(SourceLocation * TrailingElseLoc)1272f4a2713aSLionel Sambuc StmtResult Parser::ParseWhileStatement(SourceLocation *TrailingElseLoc) {
1273f4a2713aSLionel Sambuc   assert(Tok.is(tok::kw_while) && "Not a while stmt!");
1274f4a2713aSLionel Sambuc   SourceLocation WhileLoc = Tok.getLocation();
1275f4a2713aSLionel Sambuc   ConsumeToken();  // eat the 'while'.
1276f4a2713aSLionel Sambuc 
1277f4a2713aSLionel Sambuc   if (Tok.isNot(tok::l_paren)) {
1278f4a2713aSLionel Sambuc     Diag(Tok, diag::err_expected_lparen_after) << "while";
1279f4a2713aSLionel Sambuc     SkipUntil(tok::semi);
1280f4a2713aSLionel Sambuc     return StmtError();
1281f4a2713aSLionel Sambuc   }
1282f4a2713aSLionel Sambuc 
1283f4a2713aSLionel Sambuc   bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
1284f4a2713aSLionel Sambuc 
1285f4a2713aSLionel Sambuc   // C99 6.8.5p5 - In C99, the while statement is a block.  This is not
1286f4a2713aSLionel Sambuc   // the case for C90.  Start the loop scope.
1287f4a2713aSLionel Sambuc   //
1288f4a2713aSLionel Sambuc   // C++ 6.4p3:
1289f4a2713aSLionel Sambuc   // A name introduced by a declaration in a condition is in scope from its
1290f4a2713aSLionel Sambuc   // point of declaration until the end of the substatements controlled by the
1291f4a2713aSLionel Sambuc   // condition.
1292f4a2713aSLionel Sambuc   // C++ 3.3.2p4:
1293f4a2713aSLionel Sambuc   // Names declared in the for-init-statement, and in the condition of if,
1294f4a2713aSLionel Sambuc   // while, for, and switch statements are local to the if, while, for, or
1295f4a2713aSLionel Sambuc   // switch statement (including the controlled statement).
1296f4a2713aSLionel Sambuc   //
1297f4a2713aSLionel Sambuc   unsigned ScopeFlags;
1298f4a2713aSLionel Sambuc   if (C99orCXX)
1299f4a2713aSLionel Sambuc     ScopeFlags = Scope::BreakScope | Scope::ContinueScope |
1300f4a2713aSLionel Sambuc                  Scope::DeclScope  | Scope::ControlScope;
1301f4a2713aSLionel Sambuc   else
1302f4a2713aSLionel Sambuc     ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1303f4a2713aSLionel Sambuc   ParseScope WhileScope(this, ScopeFlags);
1304f4a2713aSLionel Sambuc 
1305f4a2713aSLionel Sambuc   // Parse the condition.
1306f4a2713aSLionel Sambuc   ExprResult Cond;
1307*0a6a1f1dSLionel Sambuc   Decl *CondVar = nullptr;
1308f4a2713aSLionel Sambuc   if (ParseParenExprOrCondition(Cond, CondVar, WhileLoc, true))
1309f4a2713aSLionel Sambuc     return StmtError();
1310f4a2713aSLionel Sambuc 
1311f4a2713aSLionel Sambuc   FullExprArg FullCond(Actions.MakeFullExpr(Cond.get(), WhileLoc));
1312f4a2713aSLionel Sambuc 
1313*0a6a1f1dSLionel Sambuc   // C99 6.8.5p5 - In C99, the body of the while statement is a scope, even if
1314f4a2713aSLionel Sambuc   // there is no compound stmt.  C90 does not have this clause.  We only do this
1315f4a2713aSLionel Sambuc   // if the body isn't a compound statement to avoid push/pop in common cases.
1316f4a2713aSLionel Sambuc   //
1317f4a2713aSLionel Sambuc   // C++ 6.5p2:
1318f4a2713aSLionel Sambuc   // The substatement in an iteration-statement implicitly defines a local scope
1319f4a2713aSLionel Sambuc   // which is entered and exited each time through the loop.
1320f4a2713aSLionel Sambuc   //
1321f4a2713aSLionel Sambuc   // See comments in ParseIfStatement for why we create a scope for the
1322f4a2713aSLionel Sambuc   // condition and a new scope for substatement in C++.
1323f4a2713aSLionel Sambuc   //
1324*0a6a1f1dSLionel Sambuc   ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
1325f4a2713aSLionel Sambuc 
1326f4a2713aSLionel Sambuc   // Read the body statement.
1327f4a2713aSLionel Sambuc   StmtResult Body(ParseStatement(TrailingElseLoc));
1328f4a2713aSLionel Sambuc 
1329f4a2713aSLionel Sambuc   // Pop the body scope if needed.
1330f4a2713aSLionel Sambuc   InnerScope.Exit();
1331f4a2713aSLionel Sambuc   WhileScope.Exit();
1332f4a2713aSLionel Sambuc 
1333f4a2713aSLionel Sambuc   if ((Cond.isInvalid() && !CondVar) || Body.isInvalid())
1334f4a2713aSLionel Sambuc     return StmtError();
1335f4a2713aSLionel Sambuc 
1336f4a2713aSLionel Sambuc   return Actions.ActOnWhileStmt(WhileLoc, FullCond, CondVar, Body.get());
1337f4a2713aSLionel Sambuc }
1338f4a2713aSLionel Sambuc 
1339f4a2713aSLionel Sambuc /// ParseDoStatement
1340f4a2713aSLionel Sambuc ///       do-statement: [C99 6.8.5.2]
1341f4a2713aSLionel Sambuc ///         'do' statement 'while' '(' expression ')' ';'
1342f4a2713aSLionel Sambuc /// Note: this lets the caller parse the end ';'.
ParseDoStatement()1343f4a2713aSLionel Sambuc StmtResult Parser::ParseDoStatement() {
1344f4a2713aSLionel Sambuc   assert(Tok.is(tok::kw_do) && "Not a do stmt!");
1345f4a2713aSLionel Sambuc   SourceLocation DoLoc = ConsumeToken();  // eat the 'do'.
1346f4a2713aSLionel Sambuc 
1347f4a2713aSLionel Sambuc   // C99 6.8.5p5 - In C99, the do statement is a block.  This is not
1348f4a2713aSLionel Sambuc   // the case for C90.  Start the loop scope.
1349f4a2713aSLionel Sambuc   unsigned ScopeFlags;
1350f4a2713aSLionel Sambuc   if (getLangOpts().C99)
1351f4a2713aSLionel Sambuc     ScopeFlags = Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope;
1352f4a2713aSLionel Sambuc   else
1353f4a2713aSLionel Sambuc     ScopeFlags = Scope::BreakScope | Scope::ContinueScope;
1354f4a2713aSLionel Sambuc 
1355f4a2713aSLionel Sambuc   ParseScope DoScope(this, ScopeFlags);
1356f4a2713aSLionel Sambuc 
1357*0a6a1f1dSLionel Sambuc   // C99 6.8.5p5 - In C99, the body of the do statement is a scope, even if
1358f4a2713aSLionel Sambuc   // there is no compound stmt.  C90 does not have this clause. We only do this
1359f4a2713aSLionel Sambuc   // if the body isn't a compound statement to avoid push/pop in common cases.
1360f4a2713aSLionel Sambuc   //
1361f4a2713aSLionel Sambuc   // C++ 6.5p2:
1362f4a2713aSLionel Sambuc   // The substatement in an iteration-statement implicitly defines a local scope
1363f4a2713aSLionel Sambuc   // which is entered and exited each time through the loop.
1364f4a2713aSLionel Sambuc   //
1365*0a6a1f1dSLionel Sambuc   bool C99orCXX = getLangOpts().C99 || getLangOpts().CPlusPlus;
1366*0a6a1f1dSLionel Sambuc   ParseScope InnerScope(this, Scope::DeclScope, C99orCXX, Tok.is(tok::l_brace));
1367f4a2713aSLionel Sambuc 
1368f4a2713aSLionel Sambuc   // Read the body statement.
1369f4a2713aSLionel Sambuc   StmtResult Body(ParseStatement());
1370f4a2713aSLionel Sambuc 
1371f4a2713aSLionel Sambuc   // Pop the body scope if needed.
1372f4a2713aSLionel Sambuc   InnerScope.Exit();
1373f4a2713aSLionel Sambuc 
1374f4a2713aSLionel Sambuc   if (Tok.isNot(tok::kw_while)) {
1375f4a2713aSLionel Sambuc     if (!Body.isInvalid()) {
1376f4a2713aSLionel Sambuc       Diag(Tok, diag::err_expected_while);
1377*0a6a1f1dSLionel Sambuc       Diag(DoLoc, diag::note_matching) << "'do'";
1378f4a2713aSLionel Sambuc       SkipUntil(tok::semi, StopBeforeMatch);
1379f4a2713aSLionel Sambuc     }
1380f4a2713aSLionel Sambuc     return StmtError();
1381f4a2713aSLionel Sambuc   }
1382f4a2713aSLionel Sambuc   SourceLocation WhileLoc = ConsumeToken();
1383f4a2713aSLionel Sambuc 
1384f4a2713aSLionel Sambuc   if (Tok.isNot(tok::l_paren)) {
1385f4a2713aSLionel Sambuc     Diag(Tok, diag::err_expected_lparen_after) << "do/while";
1386f4a2713aSLionel Sambuc     SkipUntil(tok::semi, StopBeforeMatch);
1387f4a2713aSLionel Sambuc     return StmtError();
1388f4a2713aSLionel Sambuc   }
1389f4a2713aSLionel Sambuc 
1390f4a2713aSLionel Sambuc   // Parse the parenthesized expression.
1391f4a2713aSLionel Sambuc   BalancedDelimiterTracker T(*this, tok::l_paren);
1392f4a2713aSLionel Sambuc   T.consumeOpen();
1393f4a2713aSLionel Sambuc 
1394f4a2713aSLionel Sambuc   // A do-while expression is not a condition, so can't have attributes.
1395f4a2713aSLionel Sambuc   DiagnoseAndSkipCXX11Attributes();
1396f4a2713aSLionel Sambuc 
1397f4a2713aSLionel Sambuc   ExprResult Cond = ParseExpression();
1398f4a2713aSLionel Sambuc   T.consumeClose();
1399f4a2713aSLionel Sambuc   DoScope.Exit();
1400f4a2713aSLionel Sambuc 
1401f4a2713aSLionel Sambuc   if (Cond.isInvalid() || Body.isInvalid())
1402f4a2713aSLionel Sambuc     return StmtError();
1403f4a2713aSLionel Sambuc 
1404f4a2713aSLionel Sambuc   return Actions.ActOnDoStmt(DoLoc, Body.get(), WhileLoc, T.getOpenLocation(),
1405f4a2713aSLionel Sambuc                              Cond.get(), T.getCloseLocation());
1406f4a2713aSLionel Sambuc }
1407f4a2713aSLionel Sambuc 
isForRangeIdentifier()1408*0a6a1f1dSLionel Sambuc bool Parser::isForRangeIdentifier() {
1409*0a6a1f1dSLionel Sambuc   assert(Tok.is(tok::identifier));
1410*0a6a1f1dSLionel Sambuc 
1411*0a6a1f1dSLionel Sambuc   const Token &Next = NextToken();
1412*0a6a1f1dSLionel Sambuc   if (Next.is(tok::colon))
1413*0a6a1f1dSLionel Sambuc     return true;
1414*0a6a1f1dSLionel Sambuc 
1415*0a6a1f1dSLionel Sambuc   if (Next.is(tok::l_square) || Next.is(tok::kw_alignas)) {
1416*0a6a1f1dSLionel Sambuc     TentativeParsingAction PA(*this);
1417*0a6a1f1dSLionel Sambuc     ConsumeToken();
1418*0a6a1f1dSLionel Sambuc     SkipCXX11Attributes();
1419*0a6a1f1dSLionel Sambuc     bool Result = Tok.is(tok::colon);
1420*0a6a1f1dSLionel Sambuc     PA.Revert();
1421*0a6a1f1dSLionel Sambuc     return Result;
1422*0a6a1f1dSLionel Sambuc   }
1423*0a6a1f1dSLionel Sambuc 
1424*0a6a1f1dSLionel Sambuc   return false;
1425*0a6a1f1dSLionel Sambuc }
1426*0a6a1f1dSLionel Sambuc 
1427f4a2713aSLionel Sambuc /// ParseForStatement
1428f4a2713aSLionel Sambuc ///       for-statement: [C99 6.8.5.3]
1429f4a2713aSLionel Sambuc ///         'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
1430f4a2713aSLionel Sambuc ///         'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
1431f4a2713aSLionel Sambuc /// [C++]   'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
1432f4a2713aSLionel Sambuc /// [C++]       statement
1433f4a2713aSLionel Sambuc /// [C++0x] 'for' '(' for-range-declaration : for-range-initializer ) statement
1434f4a2713aSLionel Sambuc /// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
1435f4a2713aSLionel Sambuc /// [OBJC2] 'for' '(' expr 'in' expr ')' statement
1436f4a2713aSLionel Sambuc ///
1437f4a2713aSLionel Sambuc /// [C++] for-init-statement:
1438f4a2713aSLionel Sambuc /// [C++]   expression-statement
1439f4a2713aSLionel Sambuc /// [C++]   simple-declaration
1440f4a2713aSLionel Sambuc ///
1441f4a2713aSLionel Sambuc /// [C++0x] for-range-declaration:
1442f4a2713aSLionel Sambuc /// [C++0x]   attribute-specifier-seq[opt] type-specifier-seq declarator
1443f4a2713aSLionel Sambuc /// [C++0x] for-range-initializer:
1444f4a2713aSLionel Sambuc /// [C++0x]   expression
1445f4a2713aSLionel Sambuc /// [C++0x]   braced-init-list            [TODO]
ParseForStatement(SourceLocation * TrailingElseLoc)1446f4a2713aSLionel Sambuc StmtResult Parser::ParseForStatement(SourceLocation *TrailingElseLoc) {
1447f4a2713aSLionel Sambuc   assert(Tok.is(tok::kw_for) && "Not a for stmt!");
1448f4a2713aSLionel Sambuc   SourceLocation ForLoc = ConsumeToken();  // eat the 'for'.
1449f4a2713aSLionel Sambuc 
1450f4a2713aSLionel Sambuc   if (Tok.isNot(tok::l_paren)) {
1451f4a2713aSLionel Sambuc     Diag(Tok, diag::err_expected_lparen_after) << "for";
1452f4a2713aSLionel Sambuc     SkipUntil(tok::semi);
1453f4a2713aSLionel Sambuc     return StmtError();
1454f4a2713aSLionel Sambuc   }
1455f4a2713aSLionel Sambuc 
1456f4a2713aSLionel Sambuc   bool C99orCXXorObjC = getLangOpts().C99 || getLangOpts().CPlusPlus ||
1457f4a2713aSLionel Sambuc     getLangOpts().ObjC1;
1458f4a2713aSLionel Sambuc 
1459f4a2713aSLionel Sambuc   // C99 6.8.5p5 - In C99, the for statement is a block.  This is not
1460f4a2713aSLionel Sambuc   // the case for C90.  Start the loop scope.
1461f4a2713aSLionel Sambuc   //
1462f4a2713aSLionel Sambuc   // C++ 6.4p3:
1463f4a2713aSLionel Sambuc   // A name introduced by a declaration in a condition is in scope from its
1464f4a2713aSLionel Sambuc   // point of declaration until the end of the substatements controlled by the
1465f4a2713aSLionel Sambuc   // condition.
1466f4a2713aSLionel Sambuc   // C++ 3.3.2p4:
1467f4a2713aSLionel Sambuc   // Names declared in the for-init-statement, and in the condition of if,
1468f4a2713aSLionel Sambuc   // while, for, and switch statements are local to the if, while, for, or
1469f4a2713aSLionel Sambuc   // switch statement (including the controlled statement).
1470f4a2713aSLionel Sambuc   // C++ 6.5.3p1:
1471f4a2713aSLionel Sambuc   // Names declared in the for-init-statement are in the same declarative-region
1472f4a2713aSLionel Sambuc   // as those declared in the condition.
1473f4a2713aSLionel Sambuc   //
1474*0a6a1f1dSLionel Sambuc   unsigned ScopeFlags = 0;
1475f4a2713aSLionel Sambuc   if (C99orCXXorObjC)
1476*0a6a1f1dSLionel Sambuc     ScopeFlags = Scope::DeclScope | Scope::ControlScope;
1477f4a2713aSLionel Sambuc 
1478f4a2713aSLionel Sambuc   ParseScope ForScope(this, ScopeFlags);
1479f4a2713aSLionel Sambuc 
1480f4a2713aSLionel Sambuc   BalancedDelimiterTracker T(*this, tok::l_paren);
1481f4a2713aSLionel Sambuc   T.consumeOpen();
1482f4a2713aSLionel Sambuc 
1483f4a2713aSLionel Sambuc   ExprResult Value;
1484f4a2713aSLionel Sambuc 
1485f4a2713aSLionel Sambuc   bool ForEach = false, ForRange = false;
1486f4a2713aSLionel Sambuc   StmtResult FirstPart;
1487f4a2713aSLionel Sambuc   bool SecondPartIsInvalid = false;
1488f4a2713aSLionel Sambuc   FullExprArg SecondPart(Actions);
1489f4a2713aSLionel Sambuc   ExprResult Collection;
1490f4a2713aSLionel Sambuc   ForRangeInit ForRangeInit;
1491f4a2713aSLionel Sambuc   FullExprArg ThirdPart(Actions);
1492*0a6a1f1dSLionel Sambuc   Decl *SecondVar = nullptr;
1493f4a2713aSLionel Sambuc 
1494f4a2713aSLionel Sambuc   if (Tok.is(tok::code_completion)) {
1495f4a2713aSLionel Sambuc     Actions.CodeCompleteOrdinaryName(getCurScope(),
1496f4a2713aSLionel Sambuc                                      C99orCXXorObjC? Sema::PCC_ForInit
1497f4a2713aSLionel Sambuc                                                    : Sema::PCC_Expression);
1498f4a2713aSLionel Sambuc     cutOffParsing();
1499f4a2713aSLionel Sambuc     return StmtError();
1500f4a2713aSLionel Sambuc   }
1501f4a2713aSLionel Sambuc 
1502f4a2713aSLionel Sambuc   ParsedAttributesWithRange attrs(AttrFactory);
1503f4a2713aSLionel Sambuc   MaybeParseCXX11Attributes(attrs);
1504f4a2713aSLionel Sambuc 
1505f4a2713aSLionel Sambuc   // Parse the first part of the for specifier.
1506f4a2713aSLionel Sambuc   if (Tok.is(tok::semi)) {  // for (;
1507f4a2713aSLionel Sambuc     ProhibitAttributes(attrs);
1508f4a2713aSLionel Sambuc     // no first part, eat the ';'.
1509f4a2713aSLionel Sambuc     ConsumeToken();
1510*0a6a1f1dSLionel Sambuc   } else if (getLangOpts().CPlusPlus && Tok.is(tok::identifier) &&
1511*0a6a1f1dSLionel Sambuc              isForRangeIdentifier()) {
1512*0a6a1f1dSLionel Sambuc     ProhibitAttributes(attrs);
1513*0a6a1f1dSLionel Sambuc     IdentifierInfo *Name = Tok.getIdentifierInfo();
1514*0a6a1f1dSLionel Sambuc     SourceLocation Loc = ConsumeToken();
1515*0a6a1f1dSLionel Sambuc     MaybeParseCXX11Attributes(attrs);
1516*0a6a1f1dSLionel Sambuc 
1517*0a6a1f1dSLionel Sambuc     ForRangeInit.ColonLoc = ConsumeToken();
1518*0a6a1f1dSLionel Sambuc     if (Tok.is(tok::l_brace))
1519*0a6a1f1dSLionel Sambuc       ForRangeInit.RangeExpr = ParseBraceInitializer();
1520*0a6a1f1dSLionel Sambuc     else
1521*0a6a1f1dSLionel Sambuc       ForRangeInit.RangeExpr = ParseExpression();
1522*0a6a1f1dSLionel Sambuc 
1523*0a6a1f1dSLionel Sambuc     Diag(Loc, diag::err_for_range_identifier)
1524*0a6a1f1dSLionel Sambuc       << ((getLangOpts().CPlusPlus11 && !getLangOpts().CPlusPlus1z)
1525*0a6a1f1dSLionel Sambuc               ? FixItHint::CreateInsertion(Loc, "auto &&")
1526*0a6a1f1dSLionel Sambuc               : FixItHint());
1527*0a6a1f1dSLionel Sambuc 
1528*0a6a1f1dSLionel Sambuc     FirstPart = Actions.ActOnCXXForRangeIdentifier(getCurScope(), Loc, Name,
1529*0a6a1f1dSLionel Sambuc                                                    attrs, attrs.Range.getEnd());
1530*0a6a1f1dSLionel Sambuc     ForRange = true;
1531f4a2713aSLionel Sambuc   } else if (isForInitDeclaration()) {  // for (int X = 4;
1532f4a2713aSLionel Sambuc     // Parse declaration, which eats the ';'.
1533f4a2713aSLionel Sambuc     if (!C99orCXXorObjC)   // Use of C99-style for loops in C90 mode?
1534f4a2713aSLionel Sambuc       Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
1535f4a2713aSLionel Sambuc 
1536f4a2713aSLionel Sambuc     // In C++0x, "for (T NS:a" might not be a typo for ::
1537f4a2713aSLionel Sambuc     bool MightBeForRangeStmt = getLangOpts().CPlusPlus;
1538f4a2713aSLionel Sambuc     ColonProtectionRAIIObject ColonProtection(*this, MightBeForRangeStmt);
1539f4a2713aSLionel Sambuc 
1540f4a2713aSLionel Sambuc     SourceLocation DeclStart = Tok.getLocation(), DeclEnd;
1541*0a6a1f1dSLionel Sambuc     DeclGroupPtrTy DG = ParseSimpleDeclaration(
1542*0a6a1f1dSLionel Sambuc         Declarator::ForContext, DeclEnd, attrs, false,
1543*0a6a1f1dSLionel Sambuc         MightBeForRangeStmt ? &ForRangeInit : nullptr);
1544f4a2713aSLionel Sambuc     FirstPart = Actions.ActOnDeclStmt(DG, DeclStart, Tok.getLocation());
1545f4a2713aSLionel Sambuc     if (ForRangeInit.ParsedForRangeDecl()) {
1546f4a2713aSLionel Sambuc       Diag(ForRangeInit.ColonLoc, getLangOpts().CPlusPlus11 ?
1547f4a2713aSLionel Sambuc            diag::warn_cxx98_compat_for_range : diag::ext_for_range);
1548f4a2713aSLionel Sambuc 
1549f4a2713aSLionel Sambuc       ForRange = true;
1550f4a2713aSLionel Sambuc     } else if (Tok.is(tok::semi)) {  // for (int x = 4;
1551f4a2713aSLionel Sambuc       ConsumeToken();
1552f4a2713aSLionel Sambuc     } else if ((ForEach = isTokIdentifier_in())) {
1553f4a2713aSLionel Sambuc       Actions.ActOnForEachDeclStmt(DG);
1554f4a2713aSLionel Sambuc       // ObjC: for (id x in expr)
1555f4a2713aSLionel Sambuc       ConsumeToken(); // consume 'in'
1556f4a2713aSLionel Sambuc 
1557f4a2713aSLionel Sambuc       if (Tok.is(tok::code_completion)) {
1558f4a2713aSLionel Sambuc         Actions.CodeCompleteObjCForCollection(getCurScope(), DG);
1559f4a2713aSLionel Sambuc         cutOffParsing();
1560f4a2713aSLionel Sambuc         return StmtError();
1561f4a2713aSLionel Sambuc       }
1562f4a2713aSLionel Sambuc       Collection = ParseExpression();
1563f4a2713aSLionel Sambuc     } else {
1564f4a2713aSLionel Sambuc       Diag(Tok, diag::err_expected_semi_for);
1565f4a2713aSLionel Sambuc     }
1566f4a2713aSLionel Sambuc   } else {
1567f4a2713aSLionel Sambuc     ProhibitAttributes(attrs);
1568*0a6a1f1dSLionel Sambuc     Value = Actions.CorrectDelayedTyposInExpr(ParseExpression());
1569f4a2713aSLionel Sambuc 
1570f4a2713aSLionel Sambuc     ForEach = isTokIdentifier_in();
1571f4a2713aSLionel Sambuc 
1572f4a2713aSLionel Sambuc     // Turn the expression into a stmt.
1573f4a2713aSLionel Sambuc     if (!Value.isInvalid()) {
1574f4a2713aSLionel Sambuc       if (ForEach)
1575f4a2713aSLionel Sambuc         FirstPart = Actions.ActOnForEachLValueExpr(Value.get());
1576f4a2713aSLionel Sambuc       else
1577f4a2713aSLionel Sambuc         FirstPart = Actions.ActOnExprStmt(Value);
1578f4a2713aSLionel Sambuc     }
1579f4a2713aSLionel Sambuc 
1580f4a2713aSLionel Sambuc     if (Tok.is(tok::semi)) {
1581f4a2713aSLionel Sambuc       ConsumeToken();
1582f4a2713aSLionel Sambuc     } else if (ForEach) {
1583f4a2713aSLionel Sambuc       ConsumeToken(); // consume 'in'
1584f4a2713aSLionel Sambuc 
1585f4a2713aSLionel Sambuc       if (Tok.is(tok::code_completion)) {
1586f4a2713aSLionel Sambuc         Actions.CodeCompleteObjCForCollection(getCurScope(), DeclGroupPtrTy());
1587f4a2713aSLionel Sambuc         cutOffParsing();
1588f4a2713aSLionel Sambuc         return StmtError();
1589f4a2713aSLionel Sambuc       }
1590f4a2713aSLionel Sambuc       Collection = ParseExpression();
1591f4a2713aSLionel Sambuc     } else if (getLangOpts().CPlusPlus11 && Tok.is(tok::colon) && FirstPart.get()) {
1592f4a2713aSLionel Sambuc       // User tried to write the reasonable, but ill-formed, for-range-statement
1593f4a2713aSLionel Sambuc       //   for (expr : expr) { ... }
1594f4a2713aSLionel Sambuc       Diag(Tok, diag::err_for_range_expected_decl)
1595f4a2713aSLionel Sambuc         << FirstPart.get()->getSourceRange();
1596f4a2713aSLionel Sambuc       SkipUntil(tok::r_paren, StopBeforeMatch);
1597f4a2713aSLionel Sambuc       SecondPartIsInvalid = true;
1598f4a2713aSLionel Sambuc     } else {
1599f4a2713aSLionel Sambuc       if (!Value.isInvalid()) {
1600f4a2713aSLionel Sambuc         Diag(Tok, diag::err_expected_semi_for);
1601f4a2713aSLionel Sambuc       } else {
1602f4a2713aSLionel Sambuc         // Skip until semicolon or rparen, don't consume it.
1603f4a2713aSLionel Sambuc         SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
1604f4a2713aSLionel Sambuc         if (Tok.is(tok::semi))
1605f4a2713aSLionel Sambuc           ConsumeToken();
1606f4a2713aSLionel Sambuc       }
1607f4a2713aSLionel Sambuc     }
1608f4a2713aSLionel Sambuc   }
1609*0a6a1f1dSLionel Sambuc 
1610*0a6a1f1dSLionel Sambuc   // Parse the second part of the for specifier.
1611*0a6a1f1dSLionel Sambuc   getCurScope()->AddFlags(Scope::BreakScope | Scope::ContinueScope);
1612f4a2713aSLionel Sambuc   if (!ForEach && !ForRange) {
1613f4a2713aSLionel Sambuc     assert(!SecondPart.get() && "Shouldn't have a second expression yet.");
1614f4a2713aSLionel Sambuc     // Parse the second part of the for specifier.
1615f4a2713aSLionel Sambuc     if (Tok.is(tok::semi)) {  // for (...;;
1616f4a2713aSLionel Sambuc       // no second part.
1617f4a2713aSLionel Sambuc     } else if (Tok.is(tok::r_paren)) {
1618f4a2713aSLionel Sambuc       // missing both semicolons.
1619f4a2713aSLionel Sambuc     } else {
1620f4a2713aSLionel Sambuc       ExprResult Second;
1621f4a2713aSLionel Sambuc       if (getLangOpts().CPlusPlus)
1622f4a2713aSLionel Sambuc         ParseCXXCondition(Second, SecondVar, ForLoc, true);
1623f4a2713aSLionel Sambuc       else {
1624f4a2713aSLionel Sambuc         Second = ParseExpression();
1625f4a2713aSLionel Sambuc         if (!Second.isInvalid())
1626f4a2713aSLionel Sambuc           Second = Actions.ActOnBooleanCondition(getCurScope(), ForLoc,
1627f4a2713aSLionel Sambuc                                                  Second.get());
1628f4a2713aSLionel Sambuc       }
1629f4a2713aSLionel Sambuc       SecondPartIsInvalid = Second.isInvalid();
1630f4a2713aSLionel Sambuc       SecondPart = Actions.MakeFullExpr(Second.get(), ForLoc);
1631f4a2713aSLionel Sambuc     }
1632f4a2713aSLionel Sambuc 
1633f4a2713aSLionel Sambuc     if (Tok.isNot(tok::semi)) {
1634f4a2713aSLionel Sambuc       if (!SecondPartIsInvalid || SecondVar)
1635f4a2713aSLionel Sambuc         Diag(Tok, diag::err_expected_semi_for);
1636f4a2713aSLionel Sambuc       else
1637f4a2713aSLionel Sambuc         // Skip until semicolon or rparen, don't consume it.
1638f4a2713aSLionel Sambuc         SkipUntil(tok::r_paren, StopAtSemi | StopBeforeMatch);
1639f4a2713aSLionel Sambuc     }
1640f4a2713aSLionel Sambuc 
1641f4a2713aSLionel Sambuc     if (Tok.is(tok::semi)) {
1642f4a2713aSLionel Sambuc       ConsumeToken();
1643f4a2713aSLionel Sambuc     }
1644f4a2713aSLionel Sambuc 
1645f4a2713aSLionel Sambuc     // Parse the third part of the for specifier.
1646f4a2713aSLionel Sambuc     if (Tok.isNot(tok::r_paren)) {   // for (...;...;)
1647f4a2713aSLionel Sambuc       ExprResult Third = ParseExpression();
1648f4a2713aSLionel Sambuc       // FIXME: The C++11 standard doesn't actually say that this is a
1649f4a2713aSLionel Sambuc       // discarded-value expression, but it clearly should be.
1650*0a6a1f1dSLionel Sambuc       ThirdPart = Actions.MakeFullDiscardedValueExpr(Third.get());
1651f4a2713aSLionel Sambuc     }
1652f4a2713aSLionel Sambuc   }
1653f4a2713aSLionel Sambuc   // Match the ')'.
1654f4a2713aSLionel Sambuc   T.consumeClose();
1655f4a2713aSLionel Sambuc 
1656f4a2713aSLionel Sambuc   // We need to perform most of the semantic analysis for a C++0x for-range
1657f4a2713aSLionel Sambuc   // statememt before parsing the body, in order to be able to deduce the type
1658f4a2713aSLionel Sambuc   // of an auto-typed loop variable.
1659f4a2713aSLionel Sambuc   StmtResult ForRangeStmt;
1660f4a2713aSLionel Sambuc   StmtResult ForEachStmt;
1661f4a2713aSLionel Sambuc 
1662f4a2713aSLionel Sambuc   if (ForRange) {
1663*0a6a1f1dSLionel Sambuc     ForRangeStmt = Actions.ActOnCXXForRangeStmt(ForLoc, FirstPart.get(),
1664f4a2713aSLionel Sambuc                                                 ForRangeInit.ColonLoc,
1665f4a2713aSLionel Sambuc                                                 ForRangeInit.RangeExpr.get(),
1666f4a2713aSLionel Sambuc                                                 T.getCloseLocation(),
1667f4a2713aSLionel Sambuc                                                 Sema::BFRK_Build);
1668f4a2713aSLionel Sambuc 
1669f4a2713aSLionel Sambuc 
1670f4a2713aSLionel Sambuc   // Similarly, we need to do the semantic analysis for a for-range
1671f4a2713aSLionel Sambuc   // statement immediately in order to close over temporaries correctly.
1672f4a2713aSLionel Sambuc   } else if (ForEach) {
1673f4a2713aSLionel Sambuc     ForEachStmt = Actions.ActOnObjCForCollectionStmt(ForLoc,
1674*0a6a1f1dSLionel Sambuc                                                      FirstPart.get(),
1675*0a6a1f1dSLionel Sambuc                                                      Collection.get(),
1676f4a2713aSLionel Sambuc                                                      T.getCloseLocation());
1677f4a2713aSLionel Sambuc   }
1678f4a2713aSLionel Sambuc 
1679*0a6a1f1dSLionel Sambuc   // C99 6.8.5p5 - In C99, the body of the for statement is a scope, even if
1680f4a2713aSLionel Sambuc   // there is no compound stmt.  C90 does not have this clause.  We only do this
1681f4a2713aSLionel Sambuc   // if the body isn't a compound statement to avoid push/pop in common cases.
1682f4a2713aSLionel Sambuc   //
1683f4a2713aSLionel Sambuc   // C++ 6.5p2:
1684f4a2713aSLionel Sambuc   // The substatement in an iteration-statement implicitly defines a local scope
1685f4a2713aSLionel Sambuc   // which is entered and exited each time through the loop.
1686f4a2713aSLionel Sambuc   //
1687f4a2713aSLionel Sambuc   // See comments in ParseIfStatement for why we create a scope for
1688f4a2713aSLionel Sambuc   // for-init-statement/condition and a new scope for substatement in C++.
1689f4a2713aSLionel Sambuc   //
1690*0a6a1f1dSLionel Sambuc   ParseScope InnerScope(this, Scope::DeclScope, C99orCXXorObjC,
1691*0a6a1f1dSLionel Sambuc                         Tok.is(tok::l_brace));
1692*0a6a1f1dSLionel Sambuc 
1693*0a6a1f1dSLionel Sambuc   // The body of the for loop has the same local mangling number as the
1694*0a6a1f1dSLionel Sambuc   // for-init-statement.
1695*0a6a1f1dSLionel Sambuc   // It will only be incremented if the body contains other things that would
1696*0a6a1f1dSLionel Sambuc   // normally increment the mangling number (like a compound statement).
1697*0a6a1f1dSLionel Sambuc   if (C99orCXXorObjC)
1698*0a6a1f1dSLionel Sambuc     getCurScope()->decrementMSLocalManglingNumber();
1699f4a2713aSLionel Sambuc 
1700f4a2713aSLionel Sambuc   // Read the body statement.
1701f4a2713aSLionel Sambuc   StmtResult Body(ParseStatement(TrailingElseLoc));
1702f4a2713aSLionel Sambuc 
1703f4a2713aSLionel Sambuc   // Pop the body scope if needed.
1704f4a2713aSLionel Sambuc   InnerScope.Exit();
1705f4a2713aSLionel Sambuc 
1706f4a2713aSLionel Sambuc   // Leave the for-scope.
1707f4a2713aSLionel Sambuc   ForScope.Exit();
1708f4a2713aSLionel Sambuc 
1709f4a2713aSLionel Sambuc   if (Body.isInvalid())
1710f4a2713aSLionel Sambuc     return StmtError();
1711f4a2713aSLionel Sambuc 
1712f4a2713aSLionel Sambuc   if (ForEach)
1713*0a6a1f1dSLionel Sambuc    return Actions.FinishObjCForCollectionStmt(ForEachStmt.get(),
1714*0a6a1f1dSLionel Sambuc                                               Body.get());
1715f4a2713aSLionel Sambuc 
1716f4a2713aSLionel Sambuc   if (ForRange)
1717*0a6a1f1dSLionel Sambuc     return Actions.FinishCXXForRangeStmt(ForRangeStmt.get(), Body.get());
1718f4a2713aSLionel Sambuc 
1719*0a6a1f1dSLionel Sambuc   return Actions.ActOnForStmt(ForLoc, T.getOpenLocation(), FirstPart.get(),
1720f4a2713aSLionel Sambuc                               SecondPart, SecondVar, ThirdPart,
1721*0a6a1f1dSLionel Sambuc                               T.getCloseLocation(), Body.get());
1722f4a2713aSLionel Sambuc }
1723f4a2713aSLionel Sambuc 
1724f4a2713aSLionel Sambuc /// ParseGotoStatement
1725f4a2713aSLionel Sambuc ///       jump-statement:
1726f4a2713aSLionel Sambuc ///         'goto' identifier ';'
1727f4a2713aSLionel Sambuc /// [GNU]   'goto' '*' expression ';'
1728f4a2713aSLionel Sambuc ///
1729f4a2713aSLionel Sambuc /// Note: this lets the caller parse the end ';'.
1730f4a2713aSLionel Sambuc ///
ParseGotoStatement()1731f4a2713aSLionel Sambuc StmtResult Parser::ParseGotoStatement() {
1732f4a2713aSLionel Sambuc   assert(Tok.is(tok::kw_goto) && "Not a goto stmt!");
1733f4a2713aSLionel Sambuc   SourceLocation GotoLoc = ConsumeToken();  // eat the 'goto'.
1734f4a2713aSLionel Sambuc 
1735f4a2713aSLionel Sambuc   StmtResult Res;
1736f4a2713aSLionel Sambuc   if (Tok.is(tok::identifier)) {
1737f4a2713aSLionel Sambuc     LabelDecl *LD = Actions.LookupOrCreateLabel(Tok.getIdentifierInfo(),
1738f4a2713aSLionel Sambuc                                                 Tok.getLocation());
1739f4a2713aSLionel Sambuc     Res = Actions.ActOnGotoStmt(GotoLoc, Tok.getLocation(), LD);
1740f4a2713aSLionel Sambuc     ConsumeToken();
1741f4a2713aSLionel Sambuc   } else if (Tok.is(tok::star)) {
1742f4a2713aSLionel Sambuc     // GNU indirect goto extension.
1743f4a2713aSLionel Sambuc     Diag(Tok, diag::ext_gnu_indirect_goto);
1744f4a2713aSLionel Sambuc     SourceLocation StarLoc = ConsumeToken();
1745f4a2713aSLionel Sambuc     ExprResult R(ParseExpression());
1746f4a2713aSLionel Sambuc     if (R.isInvalid()) {  // Skip to the semicolon, but don't consume it.
1747f4a2713aSLionel Sambuc       SkipUntil(tok::semi, StopBeforeMatch);
1748f4a2713aSLionel Sambuc       return StmtError();
1749f4a2713aSLionel Sambuc     }
1750*0a6a1f1dSLionel Sambuc     Res = Actions.ActOnIndirectGotoStmt(GotoLoc, StarLoc, R.get());
1751f4a2713aSLionel Sambuc   } else {
1752*0a6a1f1dSLionel Sambuc     Diag(Tok, diag::err_expected) << tok::identifier;
1753f4a2713aSLionel Sambuc     return StmtError();
1754f4a2713aSLionel Sambuc   }
1755f4a2713aSLionel Sambuc 
1756f4a2713aSLionel Sambuc   return Res;
1757f4a2713aSLionel Sambuc }
1758f4a2713aSLionel Sambuc 
1759f4a2713aSLionel Sambuc /// ParseContinueStatement
1760f4a2713aSLionel Sambuc ///       jump-statement:
1761f4a2713aSLionel Sambuc ///         'continue' ';'
1762f4a2713aSLionel Sambuc ///
1763f4a2713aSLionel Sambuc /// Note: this lets the caller parse the end ';'.
1764f4a2713aSLionel Sambuc ///
ParseContinueStatement()1765f4a2713aSLionel Sambuc StmtResult Parser::ParseContinueStatement() {
1766f4a2713aSLionel Sambuc   SourceLocation ContinueLoc = ConsumeToken();  // eat the 'continue'.
1767f4a2713aSLionel Sambuc   return Actions.ActOnContinueStmt(ContinueLoc, getCurScope());
1768f4a2713aSLionel Sambuc }
1769f4a2713aSLionel Sambuc 
1770f4a2713aSLionel Sambuc /// ParseBreakStatement
1771f4a2713aSLionel Sambuc ///       jump-statement:
1772f4a2713aSLionel Sambuc ///         'break' ';'
1773f4a2713aSLionel Sambuc ///
1774f4a2713aSLionel Sambuc /// Note: this lets the caller parse the end ';'.
1775f4a2713aSLionel Sambuc ///
ParseBreakStatement()1776f4a2713aSLionel Sambuc StmtResult Parser::ParseBreakStatement() {
1777f4a2713aSLionel Sambuc   SourceLocation BreakLoc = ConsumeToken();  // eat the 'break'.
1778f4a2713aSLionel Sambuc   return Actions.ActOnBreakStmt(BreakLoc, getCurScope());
1779f4a2713aSLionel Sambuc }
1780f4a2713aSLionel Sambuc 
1781f4a2713aSLionel Sambuc /// ParseReturnStatement
1782f4a2713aSLionel Sambuc ///       jump-statement:
1783f4a2713aSLionel Sambuc ///         'return' expression[opt] ';'
ParseReturnStatement()1784f4a2713aSLionel Sambuc StmtResult Parser::ParseReturnStatement() {
1785f4a2713aSLionel Sambuc   assert(Tok.is(tok::kw_return) && "Not a return stmt!");
1786f4a2713aSLionel Sambuc   SourceLocation ReturnLoc = ConsumeToken();  // eat the 'return'.
1787f4a2713aSLionel Sambuc 
1788f4a2713aSLionel Sambuc   ExprResult R;
1789f4a2713aSLionel Sambuc   if (Tok.isNot(tok::semi)) {
1790f4a2713aSLionel Sambuc     if (Tok.is(tok::code_completion)) {
1791f4a2713aSLionel Sambuc       Actions.CodeCompleteReturn(getCurScope());
1792f4a2713aSLionel Sambuc       cutOffParsing();
1793f4a2713aSLionel Sambuc       return StmtError();
1794f4a2713aSLionel Sambuc     }
1795f4a2713aSLionel Sambuc 
1796f4a2713aSLionel Sambuc     if (Tok.is(tok::l_brace) && getLangOpts().CPlusPlus) {
1797f4a2713aSLionel Sambuc       R = ParseInitializer();
1798f4a2713aSLionel Sambuc       if (R.isUsable())
1799f4a2713aSLionel Sambuc         Diag(R.get()->getLocStart(), getLangOpts().CPlusPlus11 ?
1800f4a2713aSLionel Sambuc              diag::warn_cxx98_compat_generalized_initializer_lists :
1801f4a2713aSLionel Sambuc              diag::ext_generalized_initializer_lists)
1802f4a2713aSLionel Sambuc           << R.get()->getSourceRange();
1803f4a2713aSLionel Sambuc     } else
1804f4a2713aSLionel Sambuc       R = ParseExpression();
1805*0a6a1f1dSLionel Sambuc     if (R.isInvalid()) {
1806*0a6a1f1dSLionel Sambuc       SkipUntil(tok::r_brace, StopAtSemi | StopBeforeMatch);
1807f4a2713aSLionel Sambuc       return StmtError();
1808f4a2713aSLionel Sambuc     }
1809f4a2713aSLionel Sambuc   }
1810*0a6a1f1dSLionel Sambuc   return Actions.ActOnReturnStmt(ReturnLoc, R.get(), getCurScope());
1811f4a2713aSLionel Sambuc }
1812f4a2713aSLionel Sambuc 
ParsePragmaLoopHint(StmtVector & Stmts,bool OnlyStatement,SourceLocation * TrailingElseLoc,ParsedAttributesWithRange & Attrs)1813*0a6a1f1dSLionel Sambuc StmtResult Parser::ParsePragmaLoopHint(StmtVector &Stmts, bool OnlyStatement,
1814*0a6a1f1dSLionel Sambuc                                        SourceLocation *TrailingElseLoc,
1815*0a6a1f1dSLionel Sambuc                                        ParsedAttributesWithRange &Attrs) {
1816*0a6a1f1dSLionel Sambuc   // Create temporary attribute list.
1817*0a6a1f1dSLionel Sambuc   ParsedAttributesWithRange TempAttrs(AttrFactory);
1818f4a2713aSLionel Sambuc 
1819*0a6a1f1dSLionel Sambuc   // Get loop hints and consume annotated token.
1820*0a6a1f1dSLionel Sambuc   while (Tok.is(tok::annot_pragma_loop_hint)) {
1821*0a6a1f1dSLionel Sambuc     LoopHint Hint;
1822*0a6a1f1dSLionel Sambuc     if (!HandlePragmaLoopHint(Hint))
1823f4a2713aSLionel Sambuc       continue;
1824*0a6a1f1dSLionel Sambuc 
1825*0a6a1f1dSLionel Sambuc     ArgsUnion ArgHints[] = {Hint.PragmaNameLoc, Hint.OptionLoc, Hint.StateLoc,
1826*0a6a1f1dSLionel Sambuc                             ArgsUnion(Hint.ValueExpr)};
1827*0a6a1f1dSLionel Sambuc     TempAttrs.addNew(Hint.PragmaNameLoc->Ident, Hint.Range, nullptr,
1828*0a6a1f1dSLionel Sambuc                      Hint.PragmaNameLoc->Loc, ArgHints, 4,
1829*0a6a1f1dSLionel Sambuc                      AttributeList::AS_Pragma);
1830f4a2713aSLionel Sambuc   }
1831f4a2713aSLionel Sambuc 
1832*0a6a1f1dSLionel Sambuc   // Get the next statement.
1833*0a6a1f1dSLionel Sambuc   MaybeParseCXX11Attributes(Attrs);
1834f4a2713aSLionel Sambuc 
1835*0a6a1f1dSLionel Sambuc   StmtResult S = ParseStatementOrDeclarationAfterAttributes(
1836*0a6a1f1dSLionel Sambuc       Stmts, OnlyStatement, TrailingElseLoc, Attrs);
1837f4a2713aSLionel Sambuc 
1838*0a6a1f1dSLionel Sambuc   Attrs.takeAllFrom(TempAttrs);
1839*0a6a1f1dSLionel Sambuc   return S;
1840f4a2713aSLionel Sambuc }
1841f4a2713aSLionel Sambuc 
ParseFunctionStatementBody(Decl * Decl,ParseScope & BodyScope)1842f4a2713aSLionel Sambuc Decl *Parser::ParseFunctionStatementBody(Decl *Decl, ParseScope &BodyScope) {
1843f4a2713aSLionel Sambuc   assert(Tok.is(tok::l_brace));
1844f4a2713aSLionel Sambuc   SourceLocation LBraceLoc = Tok.getLocation();
1845f4a2713aSLionel Sambuc 
1846f4a2713aSLionel Sambuc   if (SkipFunctionBodies && (!Decl || Actions.canSkipFunctionBody(Decl)) &&
1847f4a2713aSLionel Sambuc       trySkippingFunctionBody()) {
1848f4a2713aSLionel Sambuc     BodyScope.Exit();
1849f4a2713aSLionel Sambuc     return Actions.ActOnSkippedFunctionBody(Decl);
1850f4a2713aSLionel Sambuc   }
1851f4a2713aSLionel Sambuc 
1852f4a2713aSLionel Sambuc   PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, LBraceLoc,
1853f4a2713aSLionel Sambuc                                       "parsing function body");
1854f4a2713aSLionel Sambuc 
1855f4a2713aSLionel Sambuc   // Do not enter a scope for the brace, as the arguments are in the same scope
1856f4a2713aSLionel Sambuc   // (the function body) as the body itself.  Instead, just read the statement
1857f4a2713aSLionel Sambuc   // list and put it into a CompoundStmt for safe keeping.
1858f4a2713aSLionel Sambuc   StmtResult FnBody(ParseCompoundStatementBody());
1859f4a2713aSLionel Sambuc 
1860f4a2713aSLionel Sambuc   // If the function body could not be parsed, make a bogus compoundstmt.
1861f4a2713aSLionel Sambuc   if (FnBody.isInvalid()) {
1862f4a2713aSLionel Sambuc     Sema::CompoundScopeRAII CompoundScope(Actions);
1863f4a2713aSLionel Sambuc     FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc, None, false);
1864f4a2713aSLionel Sambuc   }
1865f4a2713aSLionel Sambuc 
1866f4a2713aSLionel Sambuc   BodyScope.Exit();
1867*0a6a1f1dSLionel Sambuc   return Actions.ActOnFinishFunctionBody(Decl, FnBody.get());
1868f4a2713aSLionel Sambuc }
1869f4a2713aSLionel Sambuc 
1870f4a2713aSLionel Sambuc /// ParseFunctionTryBlock - Parse a C++ function-try-block.
1871f4a2713aSLionel Sambuc ///
1872f4a2713aSLionel Sambuc ///       function-try-block:
1873f4a2713aSLionel Sambuc ///         'try' ctor-initializer[opt] compound-statement handler-seq
1874f4a2713aSLionel Sambuc ///
ParseFunctionTryBlock(Decl * Decl,ParseScope & BodyScope)1875f4a2713aSLionel Sambuc Decl *Parser::ParseFunctionTryBlock(Decl *Decl, ParseScope &BodyScope) {
1876f4a2713aSLionel Sambuc   assert(Tok.is(tok::kw_try) && "Expected 'try'");
1877f4a2713aSLionel Sambuc   SourceLocation TryLoc = ConsumeToken();
1878f4a2713aSLionel Sambuc 
1879f4a2713aSLionel Sambuc   PrettyDeclStackTraceEntry CrashInfo(Actions, Decl, TryLoc,
1880f4a2713aSLionel Sambuc                                       "parsing function try block");
1881f4a2713aSLionel Sambuc 
1882f4a2713aSLionel Sambuc   // Constructor initializer list?
1883f4a2713aSLionel Sambuc   if (Tok.is(tok::colon))
1884f4a2713aSLionel Sambuc     ParseConstructorInitializer(Decl);
1885f4a2713aSLionel Sambuc   else
1886f4a2713aSLionel Sambuc     Actions.ActOnDefaultCtorInitializers(Decl);
1887f4a2713aSLionel Sambuc 
1888f4a2713aSLionel Sambuc   if (SkipFunctionBodies && Actions.canSkipFunctionBody(Decl) &&
1889f4a2713aSLionel Sambuc       trySkippingFunctionBody()) {
1890f4a2713aSLionel Sambuc     BodyScope.Exit();
1891f4a2713aSLionel Sambuc     return Actions.ActOnSkippedFunctionBody(Decl);
1892f4a2713aSLionel Sambuc   }
1893f4a2713aSLionel Sambuc 
1894f4a2713aSLionel Sambuc   SourceLocation LBraceLoc = Tok.getLocation();
1895f4a2713aSLionel Sambuc   StmtResult FnBody(ParseCXXTryBlockCommon(TryLoc, /*FnTry*/true));
1896f4a2713aSLionel Sambuc   // If we failed to parse the try-catch, we just give the function an empty
1897f4a2713aSLionel Sambuc   // compound statement as the body.
1898f4a2713aSLionel Sambuc   if (FnBody.isInvalid()) {
1899f4a2713aSLionel Sambuc     Sema::CompoundScopeRAII CompoundScope(Actions);
1900f4a2713aSLionel Sambuc     FnBody = Actions.ActOnCompoundStmt(LBraceLoc, LBraceLoc, None, false);
1901f4a2713aSLionel Sambuc   }
1902f4a2713aSLionel Sambuc 
1903f4a2713aSLionel Sambuc   BodyScope.Exit();
1904*0a6a1f1dSLionel Sambuc   return Actions.ActOnFinishFunctionBody(Decl, FnBody.get());
1905f4a2713aSLionel Sambuc }
1906f4a2713aSLionel Sambuc 
trySkippingFunctionBody()1907f4a2713aSLionel Sambuc bool Parser::trySkippingFunctionBody() {
1908f4a2713aSLionel Sambuc   assert(Tok.is(tok::l_brace));
1909f4a2713aSLionel Sambuc   assert(SkipFunctionBodies &&
1910f4a2713aSLionel Sambuc          "Should only be called when SkipFunctionBodies is enabled");
1911f4a2713aSLionel Sambuc 
1912f4a2713aSLionel Sambuc   if (!PP.isCodeCompletionEnabled()) {
1913f4a2713aSLionel Sambuc     ConsumeBrace();
1914f4a2713aSLionel Sambuc     SkipUntil(tok::r_brace);
1915f4a2713aSLionel Sambuc     return true;
1916f4a2713aSLionel Sambuc   }
1917f4a2713aSLionel Sambuc 
1918f4a2713aSLionel Sambuc   // We're in code-completion mode. Skip parsing for all function bodies unless
1919f4a2713aSLionel Sambuc   // the body contains the code-completion point.
1920f4a2713aSLionel Sambuc   TentativeParsingAction PA(*this);
1921f4a2713aSLionel Sambuc   ConsumeBrace();
1922f4a2713aSLionel Sambuc   if (SkipUntil(tok::r_brace, StopAtCodeCompletion)) {
1923f4a2713aSLionel Sambuc     PA.Commit();
1924f4a2713aSLionel Sambuc     return true;
1925f4a2713aSLionel Sambuc   }
1926f4a2713aSLionel Sambuc 
1927f4a2713aSLionel Sambuc   PA.Revert();
1928f4a2713aSLionel Sambuc   return false;
1929f4a2713aSLionel Sambuc }
1930f4a2713aSLionel Sambuc 
1931f4a2713aSLionel Sambuc /// ParseCXXTryBlock - Parse a C++ try-block.
1932f4a2713aSLionel Sambuc ///
1933f4a2713aSLionel Sambuc ///       try-block:
1934f4a2713aSLionel Sambuc ///         'try' compound-statement handler-seq
1935f4a2713aSLionel Sambuc ///
ParseCXXTryBlock()1936f4a2713aSLionel Sambuc StmtResult Parser::ParseCXXTryBlock() {
1937f4a2713aSLionel Sambuc   assert(Tok.is(tok::kw_try) && "Expected 'try'");
1938f4a2713aSLionel Sambuc 
1939f4a2713aSLionel Sambuc   SourceLocation TryLoc = ConsumeToken();
1940f4a2713aSLionel Sambuc   return ParseCXXTryBlockCommon(TryLoc);
1941f4a2713aSLionel Sambuc }
1942f4a2713aSLionel Sambuc 
1943f4a2713aSLionel Sambuc /// ParseCXXTryBlockCommon - Parse the common part of try-block and
1944f4a2713aSLionel Sambuc /// function-try-block.
1945f4a2713aSLionel Sambuc ///
1946f4a2713aSLionel Sambuc ///       try-block:
1947f4a2713aSLionel Sambuc ///         'try' compound-statement handler-seq
1948f4a2713aSLionel Sambuc ///
1949f4a2713aSLionel Sambuc ///       function-try-block:
1950f4a2713aSLionel Sambuc ///         'try' ctor-initializer[opt] compound-statement handler-seq
1951f4a2713aSLionel Sambuc ///
1952f4a2713aSLionel Sambuc ///       handler-seq:
1953f4a2713aSLionel Sambuc ///         handler handler-seq[opt]
1954f4a2713aSLionel Sambuc ///
1955f4a2713aSLionel Sambuc ///       [Borland] try-block:
1956f4a2713aSLionel Sambuc ///         'try' compound-statement seh-except-block
1957*0a6a1f1dSLionel Sambuc ///         'try' compound-statement seh-finally-block
1958f4a2713aSLionel Sambuc ///
ParseCXXTryBlockCommon(SourceLocation TryLoc,bool FnTry)1959f4a2713aSLionel Sambuc StmtResult Parser::ParseCXXTryBlockCommon(SourceLocation TryLoc, bool FnTry) {
1960f4a2713aSLionel Sambuc   if (Tok.isNot(tok::l_brace))
1961*0a6a1f1dSLionel Sambuc     return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
1962f4a2713aSLionel Sambuc 
1963f4a2713aSLionel Sambuc   StmtResult TryBlock(ParseCompoundStatement(/*isStmtExpr=*/false,
1964f4a2713aSLionel Sambuc                       Scope::DeclScope | Scope::TryScope |
1965f4a2713aSLionel Sambuc                         (FnTry ? Scope::FnTryCatchScope : 0)));
1966f4a2713aSLionel Sambuc   if (TryBlock.isInvalid())
1967f4a2713aSLionel Sambuc     return TryBlock;
1968f4a2713aSLionel Sambuc 
1969f4a2713aSLionel Sambuc   // Borland allows SEH-handlers with 'try'
1970f4a2713aSLionel Sambuc 
1971f4a2713aSLionel Sambuc   if ((Tok.is(tok::identifier) &&
1972f4a2713aSLionel Sambuc        Tok.getIdentifierInfo() == getSEHExceptKeyword()) ||
1973f4a2713aSLionel Sambuc       Tok.is(tok::kw___finally)) {
1974f4a2713aSLionel Sambuc     // TODO: Factor into common return ParseSEHHandlerCommon(...)
1975f4a2713aSLionel Sambuc     StmtResult Handler;
1976f4a2713aSLionel Sambuc     if(Tok.getIdentifierInfo() == getSEHExceptKeyword()) {
1977f4a2713aSLionel Sambuc       SourceLocation Loc = ConsumeToken();
1978f4a2713aSLionel Sambuc       Handler = ParseSEHExceptBlock(Loc);
1979f4a2713aSLionel Sambuc     }
1980f4a2713aSLionel Sambuc     else {
1981f4a2713aSLionel Sambuc       SourceLocation Loc = ConsumeToken();
1982f4a2713aSLionel Sambuc       Handler = ParseSEHFinallyBlock(Loc);
1983f4a2713aSLionel Sambuc     }
1984f4a2713aSLionel Sambuc     if(Handler.isInvalid())
1985f4a2713aSLionel Sambuc       return Handler;
1986f4a2713aSLionel Sambuc 
1987f4a2713aSLionel Sambuc     return Actions.ActOnSEHTryBlock(true /* IsCXXTry */,
1988f4a2713aSLionel Sambuc                                     TryLoc,
1989*0a6a1f1dSLionel Sambuc                                     TryBlock.get(),
1990*0a6a1f1dSLionel Sambuc                                     Handler.get());
1991f4a2713aSLionel Sambuc   }
1992f4a2713aSLionel Sambuc   else {
1993f4a2713aSLionel Sambuc     StmtVector Handlers;
1994f4a2713aSLionel Sambuc 
1995f4a2713aSLionel Sambuc     // C++11 attributes can't appear here, despite this context seeming
1996f4a2713aSLionel Sambuc     // statement-like.
1997f4a2713aSLionel Sambuc     DiagnoseAndSkipCXX11Attributes();
1998f4a2713aSLionel Sambuc 
1999f4a2713aSLionel Sambuc     if (Tok.isNot(tok::kw_catch))
2000f4a2713aSLionel Sambuc       return StmtError(Diag(Tok, diag::err_expected_catch));
2001f4a2713aSLionel Sambuc     while (Tok.is(tok::kw_catch)) {
2002f4a2713aSLionel Sambuc       StmtResult Handler(ParseCXXCatchBlock(FnTry));
2003f4a2713aSLionel Sambuc       if (!Handler.isInvalid())
2004*0a6a1f1dSLionel Sambuc         Handlers.push_back(Handler.get());
2005f4a2713aSLionel Sambuc     }
2006f4a2713aSLionel Sambuc     // Don't bother creating the full statement if we don't have any usable
2007f4a2713aSLionel Sambuc     // handlers.
2008f4a2713aSLionel Sambuc     if (Handlers.empty())
2009f4a2713aSLionel Sambuc       return StmtError();
2010f4a2713aSLionel Sambuc 
2011*0a6a1f1dSLionel Sambuc     return Actions.ActOnCXXTryBlock(TryLoc, TryBlock.get(), Handlers);
2012f4a2713aSLionel Sambuc   }
2013f4a2713aSLionel Sambuc }
2014f4a2713aSLionel Sambuc 
2015f4a2713aSLionel Sambuc /// ParseCXXCatchBlock - Parse a C++ catch block, called handler in the standard
2016f4a2713aSLionel Sambuc ///
2017f4a2713aSLionel Sambuc ///   handler:
2018f4a2713aSLionel Sambuc ///     'catch' '(' exception-declaration ')' compound-statement
2019f4a2713aSLionel Sambuc ///
2020f4a2713aSLionel Sambuc ///   exception-declaration:
2021f4a2713aSLionel Sambuc ///     attribute-specifier-seq[opt] type-specifier-seq declarator
2022f4a2713aSLionel Sambuc ///     attribute-specifier-seq[opt] type-specifier-seq abstract-declarator[opt]
2023f4a2713aSLionel Sambuc ///     '...'
2024f4a2713aSLionel Sambuc ///
ParseCXXCatchBlock(bool FnCatch)2025f4a2713aSLionel Sambuc StmtResult Parser::ParseCXXCatchBlock(bool FnCatch) {
2026f4a2713aSLionel Sambuc   assert(Tok.is(tok::kw_catch) && "Expected 'catch'");
2027f4a2713aSLionel Sambuc 
2028f4a2713aSLionel Sambuc   SourceLocation CatchLoc = ConsumeToken();
2029f4a2713aSLionel Sambuc 
2030f4a2713aSLionel Sambuc   BalancedDelimiterTracker T(*this, tok::l_paren);
2031*0a6a1f1dSLionel Sambuc   if (T.expectAndConsume())
2032f4a2713aSLionel Sambuc     return StmtError();
2033f4a2713aSLionel Sambuc 
2034f4a2713aSLionel Sambuc   // C++ 3.3.2p3:
2035f4a2713aSLionel Sambuc   // The name in a catch exception-declaration is local to the handler and
2036f4a2713aSLionel Sambuc   // shall not be redeclared in the outermost block of the handler.
2037f4a2713aSLionel Sambuc   ParseScope CatchScope(this, Scope::DeclScope | Scope::ControlScope |
2038f4a2713aSLionel Sambuc                           (FnCatch ? Scope::FnTryCatchScope : 0));
2039f4a2713aSLionel Sambuc 
2040f4a2713aSLionel Sambuc   // exception-declaration is equivalent to '...' or a parameter-declaration
2041f4a2713aSLionel Sambuc   // without default arguments.
2042*0a6a1f1dSLionel Sambuc   Decl *ExceptionDecl = nullptr;
2043f4a2713aSLionel Sambuc   if (Tok.isNot(tok::ellipsis)) {
2044f4a2713aSLionel Sambuc     ParsedAttributesWithRange Attributes(AttrFactory);
2045f4a2713aSLionel Sambuc     MaybeParseCXX11Attributes(Attributes);
2046f4a2713aSLionel Sambuc 
2047f4a2713aSLionel Sambuc     DeclSpec DS(AttrFactory);
2048f4a2713aSLionel Sambuc     DS.takeAttributesFrom(Attributes);
2049f4a2713aSLionel Sambuc 
2050f4a2713aSLionel Sambuc     if (ParseCXXTypeSpecifierSeq(DS))
2051f4a2713aSLionel Sambuc       return StmtError();
2052f4a2713aSLionel Sambuc 
2053f4a2713aSLionel Sambuc     Declarator ExDecl(DS, Declarator::CXXCatchContext);
2054f4a2713aSLionel Sambuc     ParseDeclarator(ExDecl);
2055f4a2713aSLionel Sambuc     ExceptionDecl = Actions.ActOnExceptionDeclarator(getCurScope(), ExDecl);
2056f4a2713aSLionel Sambuc   } else
2057f4a2713aSLionel Sambuc     ConsumeToken();
2058f4a2713aSLionel Sambuc 
2059f4a2713aSLionel Sambuc   T.consumeClose();
2060f4a2713aSLionel Sambuc   if (T.getCloseLocation().isInvalid())
2061f4a2713aSLionel Sambuc     return StmtError();
2062f4a2713aSLionel Sambuc 
2063f4a2713aSLionel Sambuc   if (Tok.isNot(tok::l_brace))
2064*0a6a1f1dSLionel Sambuc     return StmtError(Diag(Tok, diag::err_expected) << tok::l_brace);
2065f4a2713aSLionel Sambuc 
2066f4a2713aSLionel Sambuc   // FIXME: Possible draft standard bug: attribute-specifier should be allowed?
2067f4a2713aSLionel Sambuc   StmtResult Block(ParseCompoundStatement());
2068f4a2713aSLionel Sambuc   if (Block.isInvalid())
2069f4a2713aSLionel Sambuc     return Block;
2070f4a2713aSLionel Sambuc 
2071*0a6a1f1dSLionel Sambuc   return Actions.ActOnCXXCatchBlock(CatchLoc, ExceptionDecl, Block.get());
2072f4a2713aSLionel Sambuc }
2073f4a2713aSLionel Sambuc 
ParseMicrosoftIfExistsStatement(StmtVector & Stmts)2074f4a2713aSLionel Sambuc void Parser::ParseMicrosoftIfExistsStatement(StmtVector &Stmts) {
2075f4a2713aSLionel Sambuc   IfExistsCondition Result;
2076f4a2713aSLionel Sambuc   if (ParseMicrosoftIfExistsCondition(Result))
2077f4a2713aSLionel Sambuc     return;
2078f4a2713aSLionel Sambuc 
2079f4a2713aSLionel Sambuc   // Handle dependent statements by parsing the braces as a compound statement.
2080f4a2713aSLionel Sambuc   // This is not the same behavior as Visual C++, which don't treat this as a
2081f4a2713aSLionel Sambuc   // compound statement, but for Clang's type checking we can't have anything
2082f4a2713aSLionel Sambuc   // inside these braces escaping to the surrounding code.
2083f4a2713aSLionel Sambuc   if (Result.Behavior == IEB_Dependent) {
2084f4a2713aSLionel Sambuc     if (!Tok.is(tok::l_brace)) {
2085*0a6a1f1dSLionel Sambuc       Diag(Tok, diag::err_expected) << tok::l_brace;
2086f4a2713aSLionel Sambuc       return;
2087f4a2713aSLionel Sambuc     }
2088f4a2713aSLionel Sambuc 
2089f4a2713aSLionel Sambuc     StmtResult Compound = ParseCompoundStatement();
2090f4a2713aSLionel Sambuc     if (Compound.isInvalid())
2091f4a2713aSLionel Sambuc       return;
2092f4a2713aSLionel Sambuc 
2093f4a2713aSLionel Sambuc     StmtResult DepResult = Actions.ActOnMSDependentExistsStmt(Result.KeywordLoc,
2094f4a2713aSLionel Sambuc                                                               Result.IsIfExists,
2095f4a2713aSLionel Sambuc                                                               Result.SS,
2096f4a2713aSLionel Sambuc                                                               Result.Name,
2097f4a2713aSLionel Sambuc                                                               Compound.get());
2098f4a2713aSLionel Sambuc     if (DepResult.isUsable())
2099f4a2713aSLionel Sambuc       Stmts.push_back(DepResult.get());
2100f4a2713aSLionel Sambuc     return;
2101f4a2713aSLionel Sambuc   }
2102f4a2713aSLionel Sambuc 
2103f4a2713aSLionel Sambuc   BalancedDelimiterTracker Braces(*this, tok::l_brace);
2104f4a2713aSLionel Sambuc   if (Braces.consumeOpen()) {
2105*0a6a1f1dSLionel Sambuc     Diag(Tok, diag::err_expected) << tok::l_brace;
2106f4a2713aSLionel Sambuc     return;
2107f4a2713aSLionel Sambuc   }
2108f4a2713aSLionel Sambuc 
2109f4a2713aSLionel Sambuc   switch (Result.Behavior) {
2110f4a2713aSLionel Sambuc   case IEB_Parse:
2111f4a2713aSLionel Sambuc     // Parse the statements below.
2112f4a2713aSLionel Sambuc     break;
2113f4a2713aSLionel Sambuc 
2114f4a2713aSLionel Sambuc   case IEB_Dependent:
2115f4a2713aSLionel Sambuc     llvm_unreachable("Dependent case handled above");
2116f4a2713aSLionel Sambuc 
2117f4a2713aSLionel Sambuc   case IEB_Skip:
2118f4a2713aSLionel Sambuc     Braces.skipToEnd();
2119f4a2713aSLionel Sambuc     return;
2120f4a2713aSLionel Sambuc   }
2121f4a2713aSLionel Sambuc 
2122f4a2713aSLionel Sambuc   // Condition is true, parse the statements.
2123f4a2713aSLionel Sambuc   while (Tok.isNot(tok::r_brace)) {
2124f4a2713aSLionel Sambuc     StmtResult R = ParseStatementOrDeclaration(Stmts, false);
2125f4a2713aSLionel Sambuc     if (R.isUsable())
2126*0a6a1f1dSLionel Sambuc       Stmts.push_back(R.get());
2127f4a2713aSLionel Sambuc   }
2128f4a2713aSLionel Sambuc   Braces.consumeClose();
2129f4a2713aSLionel Sambuc }
2130