llvm-project/clang/Parse/ParseStmt.cpp

125 lines
3.7 KiB
C++
Raw Normal View History

//===--- Statement.cpp - Statement and Block Parser -----------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file was developed by Chris Lattner and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements the Statement and Block portions of the Parser
// interface.
//
//===----------------------------------------------------------------------===//
#include "clang/Parse/Parser.h"
#include "clang/Basic/Diagnostic.h"
using namespace llvm;
using namespace clang;
//===----------------------------------------------------------------------===//
// C99 6.8: Statements and Blocks.
//===----------------------------------------------------------------------===//
/// ParseStatementOrDeclaration - Read 'statement' or 'declaration'.
/// StatementOrDeclaration:
/// statement
/// declaration
///
/// statement:
/// labeled-statement
/// compound-statement
/// expression-statement
/// selection-statement
/// iteration-statement
/// jump-statement
/// [OBC] objc-throw-statement [TODO]
/// [OBC] objc-try-catch-statement [TODO]
/// [OBC] objc-synchronized-statement [TODO]
/// [GNU] asm-statement [TODO]
/// [OMP] openmp-construct [TODO]
///
/// labeled-statement:
/// identifier ':' statement
/// 'case' constant-expression ':' statement
/// 'default' ':' statement
///
/// expression-statement:
/// expression[opt] ';'
///
/// selection-statement:
/// if-statement
/// switch-statement
///
/// iteration-statement:
/// while-statement
/// do-statement
/// for-statement
///
/// jump-statement:
/// 'goto' identifier ';'
/// 'continue' ';'
/// 'break' ';'
/// 'return' expression[opt] ';'
/// [GNU] 'goto' '*' expression ';' [TODO]
///
/// [OBC] objc-throw-statement: [TODO]
/// [OBC] '@' 'throw' expression ';' [TODO]
/// [OBC] '@' 'throw' ';' [TODO]
///
void Parser::ParseStatementOrDeclaration() {
switch (Tok.getKind()) {
default:
Diag(Tok, diag::err_expected_statement_declaration);
SkipUntil(tok::semi);
break;
case tok::semi: // expression[opt] ';' -> expression isn't present.
ConsumeToken();
break;
case tok::l_brace: // compound-statement -> '{}' block
ParseCompoundStatement();
break;
}
}
/// ParseCompoundStatement - Parse a "{}" block.
///
/// compound-statement: [C99 6.8.2]
/// { block-item-list[opt] }
/// [GNU] { label-declarations block-item-list } [TODO]
///
/// block-item-list:
/// block-item
/// block-item-list block-item
///
/// block-item:
/// declaration
/// [GNU] '__extension__' declaration [TODO]
/// statement
/// [OMP] openmp-directive [TODO]
///
/// [GNU] label-declarations:
/// [GNU] label-declaration
/// [GNU] label-declarations label-declaration
///
/// [GNU] label-declaration:
/// [GNU] '__label__' identifier-list ';'
///
/// [OMP] openmp-directive: [TODO]
/// [OMP] barrier-directive
/// [OMP] flush-directive
void Parser::ParseCompoundStatement() {
assert(Tok.getKind() == tok::l_brace && "Not a compount stmt!");
ConsumeBrace(); // eat the '{'.
while (Tok.getKind() != tok::r_brace && Tok.getKind() != tok::eof)
ParseStatementOrDeclaration();
// We broke out of the while loop because we found a '}' or EOF.
if (Tok.getKind() == tok::r_brace)
ConsumeBrace();
else
Diag(Tok, diag::err_expected_rbrace);
}