Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 1 | //===--- Expression.cpp - Expression Parsing ------------------------------===// |
| 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 Expression parsing implementation. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "clang/Parse/Parser.h" |
| 15 | #include "clang/Basic/Diagnostic.h" |
| 16 | using namespace llvm; |
| 17 | using namespace clang; |
| 18 | |
Chris Lattner | c5e0d4a | 2006-08-10 19:06:03 +0000 | [diff] [blame] | 19 | // C99 6.7.8 |
| 20 | void Parser::ParseInitializer() { |
| 21 | // FIXME: STUB. |
| 22 | ParseAssignmentExpression(); |
| 23 | } |
| 24 | |
| 25 | |
| 26 | |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 27 | Parser::ExprTy Parser::ParseExpression() { |
Chris Lattner | 52a99e5 | 2006-08-10 20:56:00 +0000 | [diff] [blame^] | 28 | ParsePostfixExpression(); |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 29 | return 0; |
| 30 | } |
| 31 | |
Chris Lattner | c5e0d4a | 2006-08-10 19:06:03 +0000 | [diff] [blame] | 32 | // Expr that doesn't include commas. |
| 33 | void Parser::ParseAssignmentExpression() { |
| 34 | ParseExpression(); |
| 35 | } |
| 36 | |
Chris Lattner | 52a99e5 | 2006-08-10 20:56:00 +0000 | [diff] [blame^] | 37 | /// ParsePostfixExpression |
| 38 | /// postfix-expression: [C99 6.5.2] |
| 39 | /// primary-expression |
| 40 | /// postfix-expression '[' expression ']' |
| 41 | /// postfix-expression '(' argument-expression-list[opt] ')' |
| 42 | /// postfix-expression '.' identifier |
| 43 | /// postfix-expression '->' identifier |
| 44 | /// postfix-expression '++' |
| 45 | /// postfix-expression '--' |
| 46 | /// '(' type-name ')' '{' initializer-list '}' |
| 47 | /// '(' type-name ')' '{' initializer-list ',' '}' |
| 48 | /// |
| 49 | /// argument-expression-list: [C99 6.5.2] |
| 50 | /// argument-expression |
| 51 | /// argument-expression-list ',' argument-expression |
| 52 | /// |
| 53 | /// primary-expression: [C99 6.5.1] |
Chris Lattner | c5e0d4a | 2006-08-10 19:06:03 +0000 | [diff] [blame] | 54 | /// identifier |
| 55 | /// constant |
| 56 | /// string-literal |
| 57 | /// '(' expression ')' |
Chris Lattner | 52a99e5 | 2006-08-10 20:56:00 +0000 | [diff] [blame^] | 58 | /// '__func__' [C99 6.4.2.2] |
| 59 | /// [GNU] '__FUNCTION__' |
| 60 | /// [GNU] '__PRETTY_FUNCTION__' |
| 61 | /// [GNU] '(' compound-statement ')' |
| 62 | /// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')' |
| 63 | /// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')' |
| 64 | /// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ',' |
| 65 | /// assign-expr ')' |
| 66 | /// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')' |
| 67 | /// [OBC] '[' objc-receiver objc-message-args ']' [TODO] |
| 68 | /// [OBC] '@selector' '(' objc-selector-arg ')' [TODO] |
| 69 | /// [OBC] '@protocol' '(' identifier ')' [TODO] |
| 70 | /// [OBC] '@encode' '(' type-name ')' [TODO] |
| 71 | /// [OBC] objc-string-literal [TODO] |
| 72 | /// |
| 73 | /// constant: [C99 6.4.4] |
| 74 | /// integer-constant |
| 75 | /// floating-constant |
| 76 | /// enumeration-constant -> identifier |
| 77 | /// character-constant |
| 78 | /// |
| 79 | /// [GNU] offsetof-member-designator: |
| 80 | /// [GNU] identifier |
| 81 | /// [GNU] offsetof-member-designator '.' identifier |
| 82 | /// [GNU] offsetof-member-designator '[' expression ']' |
| 83 | /// |
| 84 | void Parser::ParsePostfixExpression() { |
| 85 | // First step, parse the primary expression. |
| 86 | switch (Tok.getKind()) { |
| 87 | // primary-expression |
| 88 | case tok::identifier: // primary-expression: identifier |
| 89 | // constant: enumeration-constant |
| 90 | case tok::numeric_constant: // constant: integer-constant |
| 91 | // constant: floating-constant |
| 92 | case tok::char_constant: // constant: character-constant |
| 93 | case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2] |
| 94 | case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU] |
| 95 | case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU] |
| 96 | ConsumeToken(); |
| 97 | break; |
| 98 | case tok::string_literal: // primary-expression: string-literal |
| 99 | ParseStringLiteralExpression(); |
| 100 | break; |
| 101 | case tok::l_paren: // primary-expression: '(' expression ')' |
| 102 | ParseParenExpression(); |
| 103 | break; |
| 104 | |
| 105 | default: |
| 106 | Diag(Tok, diag::err_expected_expression); |
| 107 | return; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | /// ParseStringLiteralExpression - This handles the various token types that |
| 112 | /// form string literals, and also handles string concatenation [C99 5.1.1.2, |
| 113 | /// translation phase #6]. |
| 114 | /// |
| 115 | /// primary-expression: [C99 6.5.1] |
| 116 | /// string-literal |
| 117 | void Parser::ParseStringLiteralExpression() { |
| 118 | assert(isStringLiteral() && "Not a string literal!"); |
| 119 | ConsumeStringToken(); |
| 120 | |
| 121 | // String concat. Note that keywords like __func__ and __FUNCTION__ aren't |
| 122 | // considered to be strings. |
| 123 | while (isStringLiteral()) |
| 124 | ConsumeStringToken(); |
| 125 | } |
Chris Lattner | c5e0d4a | 2006-08-10 19:06:03 +0000 | [diff] [blame] | 126 | |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 127 | |
| 128 | /// ParseParenExpression - C99 c.5.1p5 |
| 129 | /// primary-expression: |
| 130 | /// '(' expression ')' |
| 131 | void Parser::ParseParenExpression() { |
| 132 | assert(Tok.getKind() == tok::l_paren && "Not a paren expr!"); |
| 133 | SourceLocation OpenLoc = Tok.getLocation(); |
| 134 | ConsumeParen(); |
| 135 | |
| 136 | ParseExpression(); |
| 137 | |
| 138 | if (Tok.getKind() == tok::r_paren) { |
| 139 | ConsumeParen(); |
| 140 | } else { |
| 141 | Diag(Tok, diag::err_expected_rparen); |
| 142 | Diag(OpenLoc, diag::err_matching); |
| 143 | } |
| 144 | } |