Chris Lattner | 0ccd51e | 2006-08-09 05:47:47 +0000 | [diff] [blame^] | 1 | //===--- Statement.cpp - Statement and Block Parser -----------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Statement and Block portions of the Parser |
| 11 | // interface. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/Parse/Parser.h" |
| 16 | #include "clang/Basic/Diagnostic.h" |
| 17 | using namespace llvm; |
| 18 | using namespace clang; |
| 19 | |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | // C99 6.8: Statements and Blocks. |
| 22 | //===----------------------------------------------------------------------===// |
| 23 | |
| 24 | /// ParseStatementOrDeclaration - Read 'statement' or 'declaration'. |
| 25 | /// StatementOrDeclaration: |
| 26 | /// statement |
| 27 | /// declaration |
| 28 | /// |
| 29 | /// statement: |
| 30 | /// labeled-statement |
| 31 | /// compound-statement |
| 32 | /// expression-statement |
| 33 | /// selection-statement |
| 34 | /// iteration-statement |
| 35 | /// jump-statement |
| 36 | /// [OBC] objc-throw-statement [TODO] |
| 37 | /// [OBC] objc-try-catch-statement [TODO] |
| 38 | /// [OBC] objc-synchronized-statement [TODO] |
| 39 | /// [GNU] asm-statement [TODO] |
| 40 | /// [OMP] openmp-construct [TODO] |
| 41 | /// |
| 42 | /// labeled-statement: |
| 43 | /// identifier ':' statement |
| 44 | /// 'case' constant-expression ':' statement |
| 45 | /// 'default' ':' statement |
| 46 | /// |
| 47 | /// expression-statement: |
| 48 | /// expression[opt] ';' |
| 49 | /// |
| 50 | /// selection-statement: |
| 51 | /// if-statement |
| 52 | /// switch-statement |
| 53 | /// |
| 54 | /// iteration-statement: |
| 55 | /// while-statement |
| 56 | /// do-statement |
| 57 | /// for-statement |
| 58 | /// |
| 59 | /// jump-statement: |
| 60 | /// 'goto' identifier ';' |
| 61 | /// 'continue' ';' |
| 62 | /// 'break' ';' |
| 63 | /// 'return' expression[opt] ';' |
| 64 | /// [GNU] 'goto' '*' expression ';' [TODO] |
| 65 | /// |
| 66 | /// [OBC] objc-throw-statement: [TODO] |
| 67 | /// [OBC] '@' 'throw' expression ';' [TODO] |
| 68 | /// [OBC] '@' 'throw' ';' [TODO] |
| 69 | /// |
| 70 | void Parser::ParseStatementOrDeclaration() { |
| 71 | switch (Tok.getKind()) { |
| 72 | default: |
| 73 | Diag(Tok, diag::err_expected_statement_declaration); |
| 74 | SkipUntil(tok::semi); |
| 75 | break; |
| 76 | |
| 77 | case tok::semi: // expression[opt] ';' -> expression isn't present. |
| 78 | ConsumeToken(); |
| 79 | break; |
| 80 | case tok::l_brace: // compound-statement -> '{}' block |
| 81 | ParseCompoundStatement(); |
| 82 | break; |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | /// ParseCompoundStatement - Parse a "{}" block. |
| 87 | /// |
| 88 | /// compound-statement: [C99 6.8.2] |
| 89 | /// { block-item-list[opt] } |
| 90 | /// [GNU] { label-declarations block-item-list } [TODO] |
| 91 | /// |
| 92 | /// block-item-list: |
| 93 | /// block-item |
| 94 | /// block-item-list block-item |
| 95 | /// |
| 96 | /// block-item: |
| 97 | /// declaration |
| 98 | /// [GNU] '__extension__' declaration [TODO] |
| 99 | /// statement |
| 100 | /// [OMP] openmp-directive [TODO] |
| 101 | /// |
| 102 | /// [GNU] label-declarations: |
| 103 | /// [GNU] label-declaration |
| 104 | /// [GNU] label-declarations label-declaration |
| 105 | /// |
| 106 | /// [GNU] label-declaration: |
| 107 | /// [GNU] '__label__' identifier-list ';' |
| 108 | /// |
| 109 | /// [OMP] openmp-directive: [TODO] |
| 110 | /// [OMP] barrier-directive |
| 111 | /// [OMP] flush-directive |
| 112 | void Parser::ParseCompoundStatement() { |
| 113 | assert(Tok.getKind() == tok::l_brace && "Not a compount stmt!"); |
| 114 | ConsumeBrace(); // eat the '{'. |
| 115 | |
| 116 | while (Tok.getKind() != tok::r_brace && Tok.getKind() != tok::eof) |
| 117 | ParseStatementOrDeclaration(); |
| 118 | |
| 119 | // We broke out of the while loop because we found a '}' or EOF. |
| 120 | if (Tok.getKind() == tok::r_brace) |
| 121 | ConsumeBrace(); |
| 122 | else |
| 123 | Diag(Tok, diag::err_expected_rbrace); |
| 124 | } |