&CommaLocs[0], RParenLoc);
}
+/// ParseCXXCondition - if/switch/while/for condition expression.
+///
+/// condition:
+/// expression
+/// type-specifier-seq declarator '=' assignment-expression
+/// [GNU] type-specifier-seq declarator simple-asm-expr[opt] attributes[opt]
+/// '=' assignment-expression
+///
+Parser::ExprResult Parser::ParseCXXCondition() {
+ if (!isDeclarationSpecifier())
+ return ParseExpression(); // expression
+
+ SourceLocation StartLoc = Tok.getLocation();
+
+ // type-specifier-seq
+ DeclSpec DS;
+ ParseSpecifierQualifierList(DS);
+
+ // declarator
+ Declarator DeclaratorInfo(DS, Declarator::ConditionContext);
+ ParseDeclarator(DeclaratorInfo);
+
+ // simple-asm-expr[opt]
+ if (Tok.is(tok::kw_asm)) {
+ ExprResult AsmLabel = ParseSimpleAsm();
+ if (AsmLabel.isInvalid) {
+ SkipUntil(tok::semi);
+ return true;
+ }
+ DeclaratorInfo.setAsmLabel(AsmLabel.Val);
+ }
+
+ // If attributes are present, parse them.
+ if (Tok.is(tok::kw___attribute))
+ DeclaratorInfo.AddAttributes(ParseAttributes());
+
+ // '=' assignment-expression
+ if (Tok.isNot(tok::equal))
+ return Diag(Tok, diag::err_expected_equal_after_declarator);
+ SourceLocation EqualLoc = ConsumeToken();
+ ExprResult AssignExpr = ParseAssignmentExpression();
+ if (AssignExpr.isInvalid)
+ return true;
+
+ return Actions.ActOnCXXConditionDeclarationExpr(CurScope, StartLoc,
+ DeclaratorInfo,
+ EqualLoc, AssignExpr.Val);
+}
+
/// ParseCXXSimpleTypeSpecifier - [C++ 7.1.5.2] Simple type specifiers.
/// This should only be called when the current token is known to be part of
/// simple-type-specifier.
/// if-statement: [C99 6.8.4.1]
/// 'if' '(' expression ')' statement
/// 'if' '(' expression ')' statement 'else' statement
+/// [C++] 'if' '(' condition ')' statement
+/// [C++] 'if' '(' condition ')' statement 'else' statement
///
Parser::StmtResult Parser::ParseIfStatement() {
assert(Tok.is(tok::kw_if) && "Not an if stmt!");
SkipUntil(tok::semi);
return true;
}
-
+
+ bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
+
// C99 6.8.4p3 - In C99, the if statement is a block. This is not
// the case for C90.
- if (getLang().C99)
- EnterScope(Scope::DeclScope);
+ if (C99orCXX)
+ EnterScope(Scope::DeclScope | Scope::ControlScope);
// Parse the condition.
- ExprResult CondExp = ParseSimpleParenExpression();
+ ExprResult CondExp;
+ if (getLang().CPlusPlus) {
+ SourceLocation LParenLoc = ConsumeParen();
+ CondExp = ParseCXXCondition();
+ MatchRHSPunctuation(tok::r_paren, LParenLoc);
+ } else {
+ CondExp = ParseSimpleParenExpression();
+ }
+
if (CondExp.isInvalid) {
SkipUntil(tok::semi);
- if (getLang().C99)
+ if (C99orCXX)
ExitScope();
return true;
}
// C99 6.8.4p3 - In C99, the body of the if statement is a scope, even if
// there is no compound stmt. C90 does not have this clause. We only do this
// if the body isn't a compound statement to avoid push/pop in common cases.
- bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
+ bool NeedsInnerScope = C99orCXX && Tok.isNot(tok::l_brace);
if (NeedsInnerScope) EnterScope(Scope::DeclScope);
-
+
// Read the 'then' stmt.
SourceLocation ThenStmtLoc = Tok.getLocation();
StmtResult ThenStmt = ParseStatement();
// there is no compound stmt. C90 does not have this clause. We only do
// this if the body isn't a compound statement to avoid push/pop in common
// cases.
- NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
+ NeedsInnerScope = C99orCXX && Tok.isNot(tok::l_brace);
if (NeedsInnerScope) EnterScope(Scope::DeclScope);
ElseStmtLoc = Tok.getLocation();
if (NeedsInnerScope) ExitScope();
}
- if (getLang().C99)
+ if (C99orCXX)
ExitScope();
// If the then or else stmt is invalid and the other is valid (and present),
/// ParseSwitchStatement
/// switch-statement:
/// 'switch' '(' expression ')' statement
+/// [C++] 'switch' '(' condition ')' statement
Parser::StmtResult Parser::ParseSwitchStatement() {
assert(Tok.is(tok::kw_switch) && "Not a switch stmt!");
SourceLocation SwitchLoc = ConsumeToken(); // eat the 'switch'.
return true;
}
+ bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
+
// C99 6.8.4p3 - In C99, the switch statement is a block. This is
// not the case for C90. Start the switch scope.
- if (getLang().C99)
- EnterScope(Scope::BreakScope|Scope::DeclScope);
+ if (C99orCXX)
+ EnterScope(Scope::BreakScope | Scope::DeclScope | Scope::ControlScope);
else
EnterScope(Scope::BreakScope);
// Parse the condition.
- ExprResult Cond = ParseSimpleParenExpression();
+ ExprResult Cond;
+ if (getLang().CPlusPlus) {
+ SourceLocation LParenLoc = ConsumeParen();
+ Cond = ParseCXXCondition();
+ MatchRHSPunctuation(tok::r_paren, LParenLoc);
+ } else {
+ Cond = ParseSimpleParenExpression();
+ }
if (Cond.isInvalid) {
ExitScope();
// C99 6.8.4p3 - In C99, the body of the switch statement is a scope, even if
// there is no compound stmt. C90 does not have this clause. We only do this
// if the body isn't a compound statement to avoid push/pop in common cases.
- bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
+ bool NeedsInnerScope = C99orCXX && Tok.isNot(tok::l_brace);
if (NeedsInnerScope) EnterScope(Scope::DeclScope);
// Read the body statement.
/// ParseWhileStatement
/// while-statement: [C99 6.8.5.1]
/// 'while' '(' expression ')' statement
+/// [C++] 'while' '(' condition ')' statement
Parser::StmtResult Parser::ParseWhileStatement() {
assert(Tok.is(tok::kw_while) && "Not a while stmt!");
SourceLocation WhileLoc = Tok.getLocation();
return true;
}
+ bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
+
// C99 6.8.5p5 - In C99, the while statement is a block. This is not
// the case for C90. Start the loop scope.
- if (getLang().C99)
- EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
+ if (C99orCXX)
+ EnterScope(Scope::BreakScope | Scope::ContinueScope |
+ Scope::DeclScope | Scope::ControlScope);
else
EnterScope(Scope::BreakScope | Scope::ContinueScope);
// Parse the condition.
- ExprResult Cond = ParseSimpleParenExpression();
+ ExprResult Cond;
+ if (getLang().CPlusPlus) {
+ SourceLocation LParenLoc = ConsumeParen();
+ Cond = ParseCXXCondition();
+ MatchRHSPunctuation(tok::r_paren, LParenLoc);
+ } else {
+ Cond = ParseSimpleParenExpression();
+ }
// C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
// there is no compound stmt. C90 does not have this clause. We only do this
// if the body isn't a compound statement to avoid push/pop in common cases.
- bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
+ bool NeedsInnerScope = C99orCXX && Tok.isNot(tok::l_brace);
if (NeedsInnerScope) EnterScope(Scope::DeclScope);
// Read the body statement.
/// for-statement: [C99 6.8.5.3]
/// 'for' '(' expr[opt] ';' expr[opt] ';' expr[opt] ')' statement
/// 'for' '(' declaration expr[opt] ';' expr[opt] ')' statement
+/// [C++] 'for' '(' for-init-statement condition[opt] ';' expression[opt] ')'
+/// [C++] statement
/// [OBJC2] 'for' '(' declaration 'in' expr ')' statement
/// [OBJC2] 'for' '(' expr 'in' expr ')' statement
+///
+/// [C++] for-init-statement:
+/// [C++] expression-statement
+/// [C++] simple-declaration
+///
Parser::StmtResult Parser::ParseForStatement() {
assert(Tok.is(tok::kw_for) && "Not a for stmt!");
SourceLocation ForLoc = ConsumeToken(); // eat the 'for'.
return true;
}
+ bool C99orCXX = getLang().C99 || getLang().CPlusPlus;
+
// C99 6.8.5p5 - In C99, the for statement is a block. This is not
// the case for C90. Start the loop scope.
- if (getLang().C99)
- EnterScope(Scope::BreakScope | Scope::ContinueScope | Scope::DeclScope);
+ if (C99orCXX)
+ EnterScope(Scope::BreakScope | Scope::ContinueScope |
+ Scope::DeclScope | Scope::ControlScope);
else
EnterScope(Scope::BreakScope | Scope::ContinueScope);
ConsumeToken();
} else if (isDeclarationSpecifier()) { // for (int X = 4;
// Parse declaration, which eats the ';'.
- if (!getLang().C99) // Use of C99-style for loops in C90 mode?
+ if (!C99orCXX) // Use of C99-style for loops in C90 mode?
Diag(Tok, diag::ext_c99_variable_decl_in_for_loop);
SourceLocation DeclStart = Tok.getLocation();
- DeclTy *aBlockVarDecl = ParseDeclaration(Declarator::ForContext);
+ DeclTy *aBlockVarDecl = ParseSimpleDeclaration(Declarator::ForContext);
// FIXME: Pass in the right location for the end of the declstmt.
StmtResult stmtResult = Actions.ActOnDeclStmt(aBlockVarDecl, DeclStart,
DeclStart);
// no second part.
Value = ExprResult();
} else {
- Value = ParseExpression();
+ Value = getLang().CPlusPlus ? ParseCXXCondition()
+ : ParseExpression();
if (!Value.isInvalid)
SecondPart = Value.Val;
}
// C99 6.8.5p5 - In C99, the body of the if statement is a scope, even if
// there is no compound stmt. C90 does not have this clause. We only do this
// if the body isn't a compound statement to avoid push/pop in common cases.
- bool NeedsInnerScope = getLang().C99 && Tok.isNot(tok::l_brace);
+ bool NeedsInnerScope = C99orCXX && Tok.isNot(tok::l_brace);
if (NeedsInnerScope) EnterScope(Scope::DeclScope);
// Read the body statement.