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 | // |
Chris Lattner | cde626a | 2006-08-12 08:13:25 +0000 | [diff] [blame] | 10 | // This file implements the Expression parsing implementation. Expressions in |
| 11 | // C99 basically consist of a bunch of binary operators with unary operators and |
| 12 | // other random stuff at the leaves. |
| 13 | // |
| 14 | // In the C99 grammar, these unary operators bind tightest and are represented |
| 15 | // as the 'cast-expression' production. Everything else is either a binary |
Chris Lattner | b7f1fc9 | 2006-08-12 16:45:01 +0000 | [diff] [blame] | 16 | // operator (e.g. '/') or a ternary operator ("?:"). The unary leaves are |
Chris Lattner | cde626a | 2006-08-12 08:13:25 +0000 | [diff] [blame] | 17 | // handled by ParseCastExpression, the higher level pieces are handled by |
| 18 | // ParseBinaryExpression. |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 19 | // |
| 20 | //===----------------------------------------------------------------------===// |
| 21 | |
| 22 | #include "clang/Parse/Parser.h" |
| 23 | #include "clang/Basic/Diagnostic.h" |
Chris Lattner | 02dffbd | 2006-10-14 07:50:21 +0000 | [diff] [blame] | 24 | #include "clang/Basic/TargetInfo.h" |
Chris Lattner | 6d28d9b | 2006-08-24 03:51:22 +0000 | [diff] [blame] | 25 | #include "llvm/ADT/SmallVector.h" |
Chris Lattner | d3e9895 | 2006-10-06 05:22:26 +0000 | [diff] [blame] | 26 | #include "llvm/ADT/StringExtras.h" |
| 27 | #include "llvm/Config/Alloca.h" |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 28 | using namespace llvm; |
| 29 | using namespace clang; |
| 30 | |
Chris Lattner | b7f1fc9 | 2006-08-12 16:45:01 +0000 | [diff] [blame] | 31 | /// PrecedenceLevels - These are precedences for the binary/ternary operators in |
Chris Lattner | cde626a | 2006-08-12 08:13:25 +0000 | [diff] [blame] | 32 | /// the C99 grammar. These have been named to relate with the C99 grammar |
| 33 | /// productions. Low precedences numbers bind more weakly than high numbers. |
| 34 | namespace prec { |
| 35 | enum Level { |
| 36 | Unknown = 0, // Not binary operator. |
| 37 | Comma = 1, // , |
| 38 | Assignment = 2, // =, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |= |
| 39 | Conditional = 3, // ? |
| 40 | LogicalOr = 4, // || |
| 41 | LogicalAnd = 5, // && |
| 42 | InclusiveOr = 6, // | |
| 43 | ExclusiveOr = 7, // ^ |
| 44 | And = 8, // & |
Chris Lattner | 9916c5c | 2006-10-27 05:24:37 +0000 | [diff] [blame^] | 45 | Equality = 9, // ==, != |
| 46 | Relational = 10, // >=, <=, >, < |
| 47 | Shift = 11, // <<, >> |
| 48 | Additive = 12, // -, + |
| 49 | Multiplicative = 13 // *, /, % |
Chris Lattner | cde626a | 2006-08-12 08:13:25 +0000 | [diff] [blame] | 50 | }; |
| 51 | } |
| 52 | |
| 53 | |
| 54 | /// getBinOpPrecedence - Return the precedence of the specified binary operator |
| 55 | /// token. This returns: |
| 56 | /// |
| 57 | static prec::Level getBinOpPrecedence(tok::TokenKind Kind) { |
| 58 | switch (Kind) { |
| 59 | default: return prec::Unknown; |
| 60 | case tok::comma: return prec::Comma; |
| 61 | case tok::equal: |
| 62 | case tok::starequal: |
| 63 | case tok::slashequal: |
| 64 | case tok::percentequal: |
| 65 | case tok::plusequal: |
| 66 | case tok::minusequal: |
| 67 | case tok::lesslessequal: |
| 68 | case tok::greatergreaterequal: |
| 69 | case tok::ampequal: |
| 70 | case tok::caretequal: |
| 71 | case tok::pipeequal: return prec::Assignment; |
| 72 | case tok::question: return prec::Conditional; |
| 73 | case tok::pipepipe: return prec::LogicalOr; |
| 74 | case tok::ampamp: return prec::LogicalAnd; |
| 75 | case tok::pipe: return prec::InclusiveOr; |
| 76 | case tok::caret: return prec::ExclusiveOr; |
| 77 | case tok::amp: return prec::And; |
Chris Lattner | cde626a | 2006-08-12 08:13:25 +0000 | [diff] [blame] | 78 | case tok::exclaimequal: |
| 79 | case tok::equalequal: return prec::Equality; |
| 80 | case tok::lessequal: |
| 81 | case tok::less: |
| 82 | case tok::greaterequal: |
| 83 | case tok::greater: return prec::Relational; |
| 84 | case tok::lessless: |
| 85 | case tok::greatergreater: return prec::Shift; |
| 86 | case tok::plus: |
| 87 | case tok::minus: return prec::Additive; |
| 88 | case tok::percent: |
| 89 | case tok::slash: |
| 90 | case tok::star: return prec::Multiplicative; |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | |
Chris Lattner | ce7e21d | 2006-08-12 17:22:40 +0000 | [diff] [blame] | 95 | /// ParseExpression - Simple precedence-based parser for binary/ternary |
Chris Lattner | cde626a | 2006-08-12 08:13:25 +0000 | [diff] [blame] | 96 | /// operators. |
| 97 | /// |
Chris Lattner | b7f1fc9 | 2006-08-12 16:45:01 +0000 | [diff] [blame] | 98 | /// Note: we diverge from the C99 grammar when parsing the assignment-expression |
| 99 | /// production. C99 specifies that the LHS of an assignment operator should be |
| 100 | /// parsed as a unary-expression, but consistency dictates that it be a |
| 101 | /// conditional-expession. In practice, the important thing here is that the |
| 102 | /// LHS of an assignment has to be an l-value, which productions between |
| 103 | /// unary-expression and conditional-expression don't produce. Because we want |
| 104 | /// consistency, we parse the LHS as a conditional-expression, then check for |
| 105 | /// l-value-ness in semantic analysis stages. |
| 106 | /// |
Chris Lattner | cde626a | 2006-08-12 08:13:25 +0000 | [diff] [blame] | 107 | /// multiplicative-expression: [C99 6.5.5] |
| 108 | /// cast-expression |
| 109 | /// multiplicative-expression '*' cast-expression |
| 110 | /// multiplicative-expression '/' cast-expression |
| 111 | /// multiplicative-expression '%' cast-expression |
| 112 | /// |
| 113 | /// additive-expression: [C99 6.5.6] |
| 114 | /// multiplicative-expression |
| 115 | /// additive-expression '+' multiplicative-expression |
| 116 | /// additive-expression '-' multiplicative-expression |
| 117 | /// |
| 118 | /// shift-expression: [C99 6.5.7] |
| 119 | /// additive-expression |
| 120 | /// shift-expression '<<' additive-expression |
| 121 | /// shift-expression '>>' additive-expression |
| 122 | /// |
| 123 | /// relational-expression: [C99 6.5.8] |
| 124 | /// shift-expression |
| 125 | /// relational-expression '<' shift-expression |
| 126 | /// relational-expression '>' shift-expression |
| 127 | /// relational-expression '<=' shift-expression |
| 128 | /// relational-expression '>=' shift-expression |
| 129 | /// |
| 130 | /// equality-expression: [C99 6.5.9] |
| 131 | /// relational-expression |
| 132 | /// equality-expression '==' relational-expression |
| 133 | /// equality-expression '!=' relational-expression |
| 134 | /// |
| 135 | /// AND-expression: [C99 6.5.10] |
| 136 | /// equality-expression |
| 137 | /// AND-expression '&' equality-expression |
| 138 | /// |
| 139 | /// exclusive-OR-expression: [C99 6.5.11] |
| 140 | /// AND-expression |
| 141 | /// exclusive-OR-expression '^' AND-expression |
| 142 | /// |
| 143 | /// inclusive-OR-expression: [C99 6.5.12] |
| 144 | /// exclusive-OR-expression |
| 145 | /// inclusive-OR-expression '|' exclusive-OR-expression |
| 146 | /// |
| 147 | /// logical-AND-expression: [C99 6.5.13] |
| 148 | /// inclusive-OR-expression |
| 149 | /// logical-AND-expression '&&' inclusive-OR-expression |
| 150 | /// |
| 151 | /// logical-OR-expression: [C99 6.5.14] |
| 152 | /// logical-AND-expression |
| 153 | /// logical-OR-expression '||' logical-AND-expression |
| 154 | /// |
| 155 | /// conditional-expression: [C99 6.5.15] |
| 156 | /// logical-OR-expression |
| 157 | /// logical-OR-expression '?' expression ':' conditional-expression |
| 158 | /// [GNU] logical-OR-expression '?' ':' conditional-expression |
| 159 | /// |
| 160 | /// assignment-expression: [C99 6.5.16] |
| 161 | /// conditional-expression |
| 162 | /// unary-expression assignment-operator assignment-expression |
| 163 | /// |
| 164 | /// assignment-operator: one of |
| 165 | /// = *= /= %= += -= <<= >>= &= ^= |= |
| 166 | /// |
| 167 | /// expression: [C99 6.5.17] |
| 168 | /// assignment-expression |
| 169 | /// expression ',' assignment-expression |
| 170 | /// |
Chris Lattner | d35c34f | 2006-08-12 17:04:50 +0000 | [diff] [blame] | 171 | Parser::ExprResult Parser::ParseExpression() { |
Chris Lattner | cde626a | 2006-08-12 08:13:25 +0000 | [diff] [blame] | 172 | ExprResult LHS = ParseCastExpression(false); |
| 173 | if (LHS.isInvalid) return LHS; |
| 174 | |
| 175 | return ParseRHSOfBinaryExpression(LHS, prec::Comma); |
| 176 | } |
| 177 | |
Chris Lattner | 0c6c034 | 2006-08-12 18:12:45 +0000 | [diff] [blame] | 178 | /// ParseAssignmentExpression - Parse an expr that doesn't include commas. |
| 179 | /// |
Chris Lattner | ce7e21d | 2006-08-12 17:22:40 +0000 | [diff] [blame] | 180 | Parser::ExprResult Parser::ParseAssignmentExpression() { |
| 181 | ExprResult LHS = ParseCastExpression(false); |
| 182 | if (LHS.isInvalid) return LHS; |
| 183 | |
| 184 | return ParseRHSOfBinaryExpression(LHS, prec::Assignment); |
| 185 | } |
| 186 | |
Chris Lattner | 3b561a3 | 2006-08-13 00:12:11 +0000 | [diff] [blame] | 187 | Parser::ExprResult Parser::ParseConstantExpression() { |
| 188 | ExprResult LHS = ParseCastExpression(false); |
| 189 | if (LHS.isInvalid) return LHS; |
| 190 | |
| 191 | // TODO: Validate that this is a constant expr! |
| 192 | return ParseRHSOfBinaryExpression(LHS, prec::Conditional); |
| 193 | } |
| 194 | |
Chris Lattner | 0c6c034 | 2006-08-12 18:12:45 +0000 | [diff] [blame] | 195 | /// ParseExpressionWithLeadingIdentifier - This special purpose method is used |
| 196 | /// in contexts where we have already consumed an identifier (which we saved in |
| 197 | /// 'Tok'), then discovered that the identifier was really the leading token of |
| 198 | /// part of an expression. For example, in "A[1]+B", we consumed "A" (which is |
| 199 | /// now in 'Tok') and the current token is "[". |
| 200 | Parser::ExprResult Parser:: |
| 201 | ParseExpressionWithLeadingIdentifier(const LexerToken &Tok) { |
| 202 | // We know that 'Tok' must correspond to this production: |
| 203 | // primary-expression: identifier |
| 204 | |
| 205 | // TODO: Pass 'Tok' to the action. |
| 206 | ExprResult Res = ExprResult(false); |
| 207 | |
| 208 | // Because we have to parse an entire cast-expression before starting the |
| 209 | // ParseRHSOfBinaryExpression method (which parses any trailing binops), we |
| 210 | // need to handle the 'postfix-expression' rules. We do this by invoking |
| 211 | // ParsePostfixExpressionSuffix to consume any postfix-expression suffixes: |
| 212 | Res = ParsePostfixExpressionSuffix(Res); |
| 213 | if (Res.isInvalid) return Res; |
| 214 | |
| 215 | // At this point, the "A[1]" part of "A[1]+B" has been consumed. Once this is |
| 216 | // done, we know we don't have to do anything for cast-expression, because the |
| 217 | // only non-postfix-expression production starts with a '(' token, and we know |
| 218 | // we have an identifier. As such, we can invoke ParseRHSOfBinaryExpression |
| 219 | // to consume any trailing operators (e.g. "+" in this example) and connected |
| 220 | // chunks of the expression. |
| 221 | return ParseRHSOfBinaryExpression(Res, prec::Comma); |
| 222 | } |
| 223 | |
Chris Lattner | 8693a51 | 2006-08-13 21:54:02 +0000 | [diff] [blame] | 224 | /// ParseExpressionWithLeadingIdentifier - This special purpose method is used |
| 225 | /// in contexts where we have already consumed an identifier (which we saved in |
| 226 | /// 'Tok'), then discovered that the identifier was really the leading token of |
| 227 | /// part of an assignment-expression. For example, in "A[1]+B", we consumed "A" |
| 228 | /// (which is now in 'Tok') and the current token is "[". |
| 229 | Parser::ExprResult Parser:: |
| 230 | ParseAssignmentExprWithLeadingIdentifier(const LexerToken &Tok) { |
| 231 | // We know that 'Tok' must correspond to this production: |
| 232 | // primary-expression: identifier |
| 233 | |
| 234 | // TODO: Pass 'Tok' to the action. |
| 235 | ExprResult Res = ExprResult(false); |
| 236 | |
| 237 | // Because we have to parse an entire cast-expression before starting the |
| 238 | // ParseRHSOfBinaryExpression method (which parses any trailing binops), we |
| 239 | // need to handle the 'postfix-expression' rules. We do this by invoking |
| 240 | // ParsePostfixExpressionSuffix to consume any postfix-expression suffixes: |
| 241 | Res = ParsePostfixExpressionSuffix(Res); |
| 242 | if (Res.isInvalid) return Res; |
| 243 | |
| 244 | // At this point, the "A[1]" part of "A[1]+B" has been consumed. Once this is |
| 245 | // done, we know we don't have to do anything for cast-expression, because the |
| 246 | // only non-postfix-expression production starts with a '(' token, and we know |
| 247 | // we have an identifier. As such, we can invoke ParseRHSOfBinaryExpression |
| 248 | // to consume any trailing operators (e.g. "+" in this example) and connected |
| 249 | // chunks of the expression. |
| 250 | return ParseRHSOfBinaryExpression(Res, prec::Assignment); |
| 251 | } |
| 252 | |
| 253 | |
Chris Lattner | 6259172 | 2006-08-12 18:40:58 +0000 | [diff] [blame] | 254 | /// ParseAssignmentExpressionWithLeadingStar - This special purpose method is |
| 255 | /// used in contexts where we have already consumed a '*' (which we saved in |
| 256 | /// 'Tok'), then discovered that the '*' was really the leading token of an |
| 257 | /// expression. For example, in "*(int*)P+B", we consumed "*" (which is |
| 258 | /// now in 'Tok') and the current token is "(". |
| 259 | Parser::ExprResult Parser:: |
| 260 | ParseAssignmentExpressionWithLeadingStar(const LexerToken &Tok) { |
| 261 | // We know that 'Tok' must correspond to this production: |
| 262 | // unary-expression: unary-operator cast-expression |
| 263 | // where 'unary-operator' is '*'. |
| 264 | |
| 265 | // Parse the cast-expression that follows the '*'. This will parse the |
| 266 | // "*(int*)P" part of "*(int*)P+B". |
| 267 | ExprResult Res = ParseCastExpression(false); |
| 268 | if (Res.isInvalid) return Res; |
| 269 | |
| 270 | // TODO: Combine Tok + Res to get the new AST. |
| 271 | |
| 272 | // We have to parse an entire cast-expression before starting the |
| 273 | // ParseRHSOfBinaryExpression method (which parses any trailing binops). Since |
| 274 | // we know that the only production above us is the cast-expression |
| 275 | // production, and because the only alternative productions start with a '(' |
| 276 | // token (we know we had a '*'), there is no work to do to get a whole |
| 277 | // cast-expression. |
| 278 | |
| 279 | // At this point, the "*(int*)P" part of "*(int*)P+B" has been consumed. Once |
| 280 | // this is done, we can invoke ParseRHSOfBinaryExpression to consume any |
| 281 | // trailing operators (e.g. "+" in this example) and connected chunks of the |
| 282 | // assignment-expression. |
| 283 | return ParseRHSOfBinaryExpression(Res, prec::Assignment); |
| 284 | } |
| 285 | |
| 286 | |
Chris Lattner | cde626a | 2006-08-12 08:13:25 +0000 | [diff] [blame] | 287 | /// ParseRHSOfBinaryExpression - Parse a binary expression that starts with |
| 288 | /// LHS and has a precedence of at least MinPrec. |
| 289 | Parser::ExprResult |
| 290 | Parser::ParseRHSOfBinaryExpression(ExprResult LHS, unsigned MinPrec) { |
| 291 | unsigned NextTokPrec = getBinOpPrecedence(Tok.getKind()); |
Chris Lattner | 9b6d4cb | 2006-08-23 05:17:46 +0000 | [diff] [blame] | 292 | SourceLocation ColonLoc; |
| 293 | |
Chris Lattner | cde626a | 2006-08-12 08:13:25 +0000 | [diff] [blame] | 294 | while (1) { |
| 295 | // If this token has a lower precedence than we are allowed to parse (e.g. |
| 296 | // because we are called recursively, or because the token is not a binop), |
| 297 | // then we are done! |
| 298 | if (NextTokPrec < MinPrec) |
| 299 | return LHS; |
| 300 | |
| 301 | // Consume the operator, saving the operator token for error reporting. |
| 302 | LexerToken OpToken = Tok; |
| 303 | ConsumeToken(); |
| 304 | |
Chris Lattner | 96c3deb | 2006-08-12 17:13:08 +0000 | [diff] [blame] | 305 | // Special case handling for the ternary operator. |
Chris Lattner | b5600a6 | 2006-10-06 05:40:05 +0000 | [diff] [blame] | 306 | ExprResult TernaryMiddle(true); |
Chris Lattner | 96c3deb | 2006-08-12 17:13:08 +0000 | [diff] [blame] | 307 | if (NextTokPrec == prec::Conditional) { |
| 308 | if (Tok.getKind() != tok::colon) { |
| 309 | // Handle this production specially: |
| 310 | // logical-OR-expression '?' expression ':' conditional-expression |
| 311 | // In particular, the RHS of the '?' is 'expression', not |
| 312 | // 'logical-OR-expression' as we might expect. |
| 313 | TernaryMiddle = ParseExpression(); |
| 314 | if (TernaryMiddle.isInvalid) return TernaryMiddle; |
| 315 | } else { |
| 316 | // Special case handling of "X ? Y : Z" where Y is empty: |
| 317 | // logical-OR-expression '?' ':' conditional-expression [GNU] |
| 318 | TernaryMiddle = ExprResult(false); |
| 319 | Diag(Tok, diag::ext_gnu_conditional_expr); |
| 320 | } |
| 321 | |
| 322 | if (Tok.getKind() != tok::colon) { |
| 323 | Diag(Tok, diag::err_expected_colon); |
| 324 | Diag(OpToken, diag::err_matching, "?"); |
| 325 | return ExprResult(true); |
| 326 | } |
| 327 | |
| 328 | // Eat the colon. |
Chris Lattner | af63531 | 2006-10-16 06:06:51 +0000 | [diff] [blame] | 329 | ColonLoc = ConsumeToken(); |
Chris Lattner | cde626a | 2006-08-12 08:13:25 +0000 | [diff] [blame] | 330 | } |
Chris Lattner | 96c3deb | 2006-08-12 17:13:08 +0000 | [diff] [blame] | 331 | |
| 332 | // Parse another leaf here for the RHS of the operator. |
| 333 | ExprResult RHS = ParseCastExpression(false); |
| 334 | if (RHS.isInvalid) return RHS; |
Chris Lattner | cde626a | 2006-08-12 08:13:25 +0000 | [diff] [blame] | 335 | |
| 336 | // Remember the precedence of this operator and get the precedence of the |
| 337 | // operator immediately to the right of the RHS. |
| 338 | unsigned ThisPrec = NextTokPrec; |
| 339 | NextTokPrec = getBinOpPrecedence(Tok.getKind()); |
Chris Lattner | 89d5375 | 2006-08-12 17:18:19 +0000 | [diff] [blame] | 340 | |
| 341 | // Assignment and conditional expressions are right-associative. |
| 342 | bool isRightAssoc = NextTokPrec == prec::Conditional || |
| 343 | NextTokPrec == prec::Assignment; |
Chris Lattner | cde626a | 2006-08-12 08:13:25 +0000 | [diff] [blame] | 344 | |
| 345 | // Get the precedence of the operator to the right of the RHS. If it binds |
| 346 | // more tightly with RHS than we do, evaluate it completely first. |
Chris Lattner | cde626a | 2006-08-12 08:13:25 +0000 | [diff] [blame] | 347 | if (ThisPrec < NextTokPrec || |
| 348 | (ThisPrec == NextTokPrec && isRightAssoc)) { |
Chris Lattner | 89d5375 | 2006-08-12 17:18:19 +0000 | [diff] [blame] | 349 | // If this is left-associative, only parse things on the RHS that bind |
| 350 | // more tightly than the current operator. If it is left-associative, it |
| 351 | // is okay, to bind exactly as tightly. For example, compile A=B=C=D as |
| 352 | // A=(B=(C=D)), where each paren is a level of recursion here. |
| 353 | RHS = ParseRHSOfBinaryExpression(RHS, ThisPrec + !isRightAssoc); |
Chris Lattner | cde626a | 2006-08-12 08:13:25 +0000 | [diff] [blame] | 354 | if (RHS.isInvalid) return RHS; |
| 355 | |
| 356 | NextTokPrec = getBinOpPrecedence(Tok.getKind()); |
| 357 | } |
| 358 | assert(NextTokPrec <= ThisPrec && "Recursion didn't work!"); |
| 359 | |
Chris Lattner | 9b6d4cb | 2006-08-23 05:17:46 +0000 | [diff] [blame] | 360 | // Combine the LHS and RHS into the LHS (e.g. build AST). |
Chris Lattner | b5600a6 | 2006-10-06 05:40:05 +0000 | [diff] [blame] | 361 | if (TernaryMiddle.isInvalid) |
Chris Lattner | ae31969 | 2006-10-25 03:49:28 +0000 | [diff] [blame] | 362 | LHS = Actions.ParseBinOp(OpToken.getLocation(), OpToken.getKind(), |
| 363 | LHS.Val, RHS.Val); |
Chris Lattner | 9b6d4cb | 2006-08-23 05:17:46 +0000 | [diff] [blame] | 364 | else |
| 365 | LHS = Actions.ParseConditionalOp(OpToken.getLocation(), ColonLoc, |
| 366 | LHS.Val, TernaryMiddle.Val, RHS.Val); |
Chris Lattner | cde626a | 2006-08-12 08:13:25 +0000 | [diff] [blame] | 367 | } |
| 368 | } |
| 369 | |
Chris Lattner | eaf0659 | 2006-08-11 02:02:23 +0000 | [diff] [blame] | 370 | /// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is |
| 371 | /// true, parse a unary-expression. |
| 372 | /// |
Chris Lattner | 4564bc1 | 2006-08-10 23:14:52 +0000 | [diff] [blame] | 373 | /// cast-expression: [C99 6.5.4] |
| 374 | /// unary-expression |
| 375 | /// '(' type-name ')' cast-expression |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 376 | /// |
Chris Lattner | c2dd85a | 2006-08-10 22:57:16 +0000 | [diff] [blame] | 377 | /// unary-expression: [C99 6.5.3] |
| 378 | /// postfix-expression |
| 379 | /// '++' unary-expression |
| 380 | /// '--' unary-expression |
| 381 | /// unary-operator cast-expression |
| 382 | /// 'sizeof' unary-expression |
| 383 | /// 'sizeof' '(' type-name ')' |
| 384 | /// [GNU] '__alignof' unary-expression |
| 385 | /// [GNU] '__alignof' '(' type-name ')' |
| 386 | /// [GNU] '&&' identifier |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 387 | /// |
Chris Lattner | c2dd85a | 2006-08-10 22:57:16 +0000 | [diff] [blame] | 388 | /// unary-operator: one of |
| 389 | /// '&' '*' '+' '-' '~' '!' |
| 390 | /// [GNU] '__extension__' '__real' '__imag' |
| 391 | /// |
Chris Lattner | 52a99e5 | 2006-08-10 20:56:00 +0000 | [diff] [blame] | 392 | /// primary-expression: [C99 6.5.1] |
Chris Lattner | c5e0d4a | 2006-08-10 19:06:03 +0000 | [diff] [blame] | 393 | /// identifier |
| 394 | /// constant |
| 395 | /// string-literal |
| 396 | /// '(' expression ')' |
Chris Lattner | 52a99e5 | 2006-08-10 20:56:00 +0000 | [diff] [blame] | 397 | /// '__func__' [C99 6.4.2.2] |
| 398 | /// [GNU] '__FUNCTION__' |
| 399 | /// [GNU] '__PRETTY_FUNCTION__' |
| 400 | /// [GNU] '(' compound-statement ')' |
| 401 | /// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')' |
| 402 | /// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')' |
| 403 | /// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ',' |
| 404 | /// assign-expr ')' |
| 405 | /// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')' |
| 406 | /// [OBC] '[' objc-receiver objc-message-args ']' [TODO] |
| 407 | /// [OBC] '@selector' '(' objc-selector-arg ')' [TODO] |
| 408 | /// [OBC] '@protocol' '(' identifier ')' [TODO] |
| 409 | /// [OBC] '@encode' '(' type-name ')' [TODO] |
| 410 | /// [OBC] objc-string-literal [TODO] |
| 411 | /// |
| 412 | /// constant: [C99 6.4.4] |
| 413 | /// integer-constant |
| 414 | /// floating-constant |
| 415 | /// enumeration-constant -> identifier |
| 416 | /// character-constant |
Chris Lattner | 52a99e5 | 2006-08-10 20:56:00 +0000 | [diff] [blame] | 417 | /// |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 418 | Parser::ExprResult Parser::ParseCastExpression(bool isUnaryExpression) { |
| 419 | ExprResult Res; |
Chris Lattner | 0ba3dc4 | 2006-10-25 03:38:23 +0000 | [diff] [blame] | 420 | tok::TokenKind SavedKind = Tok.getKind(); |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 421 | |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 422 | // This handles all of cast-expression, unary-expression, postfix-expression, |
| 423 | // and primary-expression. We handle them together like this for efficiency |
| 424 | // and to simplify handling of an expression starting with a '(' token: which |
| 425 | // may be one of a parenthesized expression, cast-expression, compound literal |
| 426 | // expression, or statement expression. |
| 427 | // |
| 428 | // If the parsed tokens consist of a primary-expression, the cases below |
Chris Lattner | 20c6a45 | 2006-08-12 17:40:43 +0000 | [diff] [blame] | 429 | // call ParsePostfixExpressionSuffix to handle the postfix expression |
| 430 | // suffixes. Cases that cannot be followed by postfix exprs should |
| 431 | // return without invoking ParsePostfixExpressionSuffix. |
Chris Lattner | ae31969 | 2006-10-25 03:49:28 +0000 | [diff] [blame] | 432 | switch (SavedKind) { |
Chris Lattner | e550a4e | 2006-08-24 06:37:51 +0000 | [diff] [blame] | 433 | case tok::l_paren: { |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 434 | // If this expression is limited to being a unary-expression, the parent can |
| 435 | // not start a cast expression. |
| 436 | ParenParseOption ParenExprType = |
| 437 | isUnaryExpression ? CompoundLiteral : CastExpr; |
Chris Lattner | e550a4e | 2006-08-24 06:37:51 +0000 | [diff] [blame] | 438 | TypeTy *CastTy; |
| 439 | SourceLocation LParenLoc = Tok.getLocation(); |
| 440 | SourceLocation RParenLoc; |
| 441 | Res = ParseParenExpression(ParenExprType, CastTy, RParenLoc); |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 442 | if (Res.isInvalid) return Res; |
| 443 | |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 444 | switch (ParenExprType) { |
| 445 | case SimpleExpr: break; // Nothing else to do. |
| 446 | case CompoundStmt: break; // Nothing else to do. |
| 447 | case CompoundLiteral: |
| 448 | // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of |
| 449 | // postfix-expression exist, parse them now. |
| 450 | break; |
| 451 | case CastExpr: |
| 452 | // We parsed '(' type-name ')' and the thing after it wasn't a '{'. Parse |
| 453 | // the cast-expression that follows it next. |
Chris Lattner | e550a4e | 2006-08-24 06:37:51 +0000 | [diff] [blame] | 454 | // TODO: For cast expression with CastTy. |
| 455 | Res = ParseCastExpression(false); |
| 456 | if (!Res.isInvalid) |
| 457 | Res = Actions.ParseCastExpr(LParenLoc, CastTy, RParenLoc, Res.Val); |
| 458 | return Res; |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 459 | } |
Chris Lattner | 20c6a45 | 2006-08-12 17:40:43 +0000 | [diff] [blame] | 460 | |
| 461 | // These can be followed by postfix-expr pieces. |
| 462 | return ParsePostfixExpressionSuffix(Res); |
Chris Lattner | e550a4e | 2006-08-24 06:37:51 +0000 | [diff] [blame] | 463 | } |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 464 | |
Chris Lattner | 52a99e5 | 2006-08-10 20:56:00 +0000 | [diff] [blame] | 465 | // primary-expression |
Chris Lattner | 9b6d4cb | 2006-08-23 05:17:46 +0000 | [diff] [blame] | 466 | case tok::numeric_constant: |
| 467 | // constant: integer-constant |
| 468 | // constant: floating-constant |
| 469 | |
| 470 | // TODO: Validate whether this is an integer or floating-constant or |
| 471 | // neither. |
| 472 | if (1) { |
Chris Lattner | ae31969 | 2006-10-25 03:49:28 +0000 | [diff] [blame] | 473 | Res = Actions.ParseIntegerConstant(Tok.getLocation()); |
Chris Lattner | 9b6d4cb | 2006-08-23 05:17:46 +0000 | [diff] [blame] | 474 | } else { |
Chris Lattner | ae31969 | 2006-10-25 03:49:28 +0000 | [diff] [blame] | 475 | Res = Actions.ParseFloatingConstant(Tok.getLocation()); |
Chris Lattner | 9b6d4cb | 2006-08-23 05:17:46 +0000 | [diff] [blame] | 476 | } |
| 477 | ConsumeToken(); |
| 478 | |
| 479 | // These can be followed by postfix-expr pieces. |
| 480 | return ParsePostfixExpressionSuffix(Res); |
| 481 | |
Chris Lattner | 52a99e5 | 2006-08-10 20:56:00 +0000 | [diff] [blame] | 482 | case tok::identifier: // primary-expression: identifier |
| 483 | // constant: enumeration-constant |
Chris Lattner | 52a99e5 | 2006-08-10 20:56:00 +0000 | [diff] [blame] | 484 | case tok::char_constant: // constant: character-constant |
| 485 | case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2] |
| 486 | case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU] |
| 487 | case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU] |
Chris Lattner | ae31969 | 2006-10-25 03:49:28 +0000 | [diff] [blame] | 488 | Res = Actions.ParseSimplePrimaryExpr(Tok.getLocation(), SavedKind); |
Chris Lattner | 52a99e5 | 2006-08-10 20:56:00 +0000 | [diff] [blame] | 489 | ConsumeToken(); |
Chris Lattner | 20c6a45 | 2006-08-12 17:40:43 +0000 | [diff] [blame] | 490 | // These can be followed by postfix-expr pieces. |
| 491 | return ParsePostfixExpressionSuffix(Res); |
Chris Lattner | 52a99e5 | 2006-08-10 20:56:00 +0000 | [diff] [blame] | 492 | case tok::string_literal: // primary-expression: string-literal |
Chris Lattner | d3e9895 | 2006-10-06 05:22:26 +0000 | [diff] [blame] | 493 | case tok::wide_string_literal: |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 494 | Res = ParseStringLiteralExpression(); |
| 495 | if (Res.isInvalid) return Res; |
Chris Lattner | 20c6a45 | 2006-08-12 17:40:43 +0000 | [diff] [blame] | 496 | // This can be followed by postfix-expr pieces (e.g. "foo"[1]). |
| 497 | return ParsePostfixExpressionSuffix(Res); |
Chris Lattner | f833977 | 2006-08-10 22:01:51 +0000 | [diff] [blame] | 498 | case tok::kw___builtin_va_arg: |
| 499 | case tok::kw___builtin_offsetof: |
| 500 | case tok::kw___builtin_choose_expr: |
| 501 | case tok::kw___builtin_types_compatible_p: |
Chris Lattner | 1112435 | 2006-08-12 19:16:08 +0000 | [diff] [blame] | 502 | return ParseBuiltinPrimaryExpression(); |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 503 | case tok::plusplus: // unary-expression: '++' unary-expression |
Chris Lattner | 0ba3dc4 | 2006-10-25 03:38:23 +0000 | [diff] [blame] | 504 | case tok::minusminus: { // unary-expression: '--' unary-expression |
| 505 | SourceLocation SavedLoc = ConsumeToken(); |
Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame] | 506 | Res = ParseCastExpression(true); |
| 507 | if (!Res.isInvalid) |
Chris Lattner | 0ba3dc4 | 2006-10-25 03:38:23 +0000 | [diff] [blame] | 508 | Res = Actions.ParseUnaryOp(SavedLoc, SavedKind, Res.Val); |
Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame] | 509 | return Res; |
Chris Lattner | 0ba3dc4 | 2006-10-25 03:38:23 +0000 | [diff] [blame] | 510 | } |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 511 | case tok::amp: // unary-expression: '&' cast-expression |
| 512 | case tok::star: // unary-expression: '*' cast-expression |
| 513 | case tok::plus: // unary-expression: '+' cast-expression |
| 514 | case tok::minus: // unary-expression: '-' cast-expression |
| 515 | case tok::tilde: // unary-expression: '~' cast-expression |
| 516 | case tok::exclaim: // unary-expression: '!' cast-expression |
| 517 | case tok::kw___real: // unary-expression: '__real' cast-expression [GNU] |
Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame] | 518 | case tok::kw___imag: // unary-expression: '__imag' cast-expression [GNU] |
Chris Lattner | 0ba3dc4 | 2006-10-25 03:38:23 +0000 | [diff] [blame] | 519 | case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU] |
Chris Lattner | 4daa077 | 2006-10-20 05:03:44 +0000 | [diff] [blame] | 520 | // FIXME: Extension not handled correctly here! |
Chris Lattner | 0ba3dc4 | 2006-10-25 03:38:23 +0000 | [diff] [blame] | 521 | SourceLocation SavedLoc = ConsumeToken(); |
Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame] | 522 | Res = ParseCastExpression(false); |
| 523 | if (!Res.isInvalid) |
Chris Lattner | 0ba3dc4 | 2006-10-25 03:38:23 +0000 | [diff] [blame] | 524 | Res = Actions.ParseUnaryOp(SavedLoc, SavedKind, Res.Val); |
Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame] | 525 | return Res; |
Chris Lattner | 0ba3dc4 | 2006-10-25 03:38:23 +0000 | [diff] [blame] | 526 | } |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 527 | case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression |
| 528 | // unary-expression: 'sizeof' '(' type-name ')' |
| 529 | case tok::kw___alignof: // unary-expression: '__alignof' unary-expression |
| 530 | // unary-expression: '__alignof' '(' type-name ')' |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 531 | return ParseSizeofAlignofExpression(); |
Chris Lattner | 0ba3dc4 | 2006-10-25 03:38:23 +0000 | [diff] [blame] | 532 | case tok::ampamp: { // unary-expression: '&&' identifier |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 533 | Diag(Tok, diag::ext_gnu_address_of_label); |
Chris Lattner | 0ba3dc4 | 2006-10-25 03:38:23 +0000 | [diff] [blame] | 534 | SourceLocation SavedLoc = ConsumeToken(); |
Chris Lattner | 14a1b64 | 2006-10-15 22:33:58 +0000 | [diff] [blame] | 535 | |
| 536 | if (Tok.getKind() != tok::identifier) { |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 537 | Diag(Tok, diag::err_expected_ident); |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 538 | return ExprResult(true); |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 539 | } |
Chris Lattner | 14a1b64 | 2006-10-15 22:33:58 +0000 | [diff] [blame] | 540 | // FIXME: Create a label ref for Tok.Ident. |
Chris Lattner | 0ba3dc4 | 2006-10-25 03:38:23 +0000 | [diff] [blame] | 541 | Res = Actions.ParseUnaryOp(SavedLoc, SavedKind, 0); |
Chris Lattner | 14a1b64 | 2006-10-15 22:33:58 +0000 | [diff] [blame] | 542 | ConsumeToken(); |
| 543 | |
| 544 | return Res; |
Chris Lattner | 0ba3dc4 | 2006-10-25 03:38:23 +0000 | [diff] [blame] | 545 | } |
Chris Lattner | 52a99e5 | 2006-08-10 20:56:00 +0000 | [diff] [blame] | 546 | default: |
| 547 | Diag(Tok, diag::err_expected_expression); |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 548 | return ExprResult(true); |
Chris Lattner | f833977 | 2006-08-10 22:01:51 +0000 | [diff] [blame] | 549 | } |
| 550 | |
Chris Lattner | 20c6a45 | 2006-08-12 17:40:43 +0000 | [diff] [blame] | 551 | // unreachable. |
| 552 | abort(); |
| 553 | } |
| 554 | |
| 555 | /// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression |
| 556 | /// is parsed, this method parses any suffixes that apply. |
| 557 | /// |
| 558 | /// postfix-expression: [C99 6.5.2] |
| 559 | /// primary-expression |
| 560 | /// postfix-expression '[' expression ']' |
| 561 | /// postfix-expression '(' argument-expression-list[opt] ')' |
| 562 | /// postfix-expression '.' identifier |
| 563 | /// postfix-expression '->' identifier |
| 564 | /// postfix-expression '++' |
| 565 | /// postfix-expression '--' |
| 566 | /// '(' type-name ')' '{' initializer-list '}' |
| 567 | /// '(' type-name ')' '{' initializer-list ',' '}' |
| 568 | /// |
| 569 | /// argument-expression-list: [C99 6.5.2] |
| 570 | /// argument-expression |
| 571 | /// argument-expression-list ',' assignment-expression |
| 572 | /// |
| 573 | Parser::ExprResult Parser::ParsePostfixExpressionSuffix(ExprResult LHS) { |
Chris Lattner | 20c6a45 | 2006-08-12 17:40:43 +0000 | [diff] [blame] | 574 | |
Chris Lattner | f833977 | 2006-08-10 22:01:51 +0000 | [diff] [blame] | 575 | // Now that the primary-expression piece of the postfix-expression has been |
| 576 | // parsed, see if there are any postfix-expression pieces here. |
| 577 | SourceLocation Loc; |
| 578 | while (1) { |
| 579 | switch (Tok.getKind()) { |
Chris Lattner | 20c6a45 | 2006-08-12 17:40:43 +0000 | [diff] [blame] | 580 | default: // Not a postfix-expression suffix. |
| 581 | return LHS; |
Chris Lattner | 6d28d9b | 2006-08-24 03:51:22 +0000 | [diff] [blame] | 582 | case tok::l_square: { // postfix-expression: p-e '[' expression ']' |
Chris Lattner | 0413237 | 2006-10-16 06:12:55 +0000 | [diff] [blame] | 583 | Loc = ConsumeBracket(); |
Chris Lattner | 6d28d9b | 2006-08-24 03:51:22 +0000 | [diff] [blame] | 584 | ExprResult Idx = ParseExpression(); |
| 585 | |
| 586 | SourceLocation RLoc = Tok.getLocation(); |
| 587 | |
| 588 | if (!LHS.isInvalid && !Idx.isInvalid && Tok.getKind() == tok::r_square) |
| 589 | LHS = Actions.ParseArraySubscriptExpr(LHS.Val, Loc, Idx.Val, RLoc); |
| 590 | else |
| 591 | LHS = ExprResult(true); |
| 592 | |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 593 | // Match the ']'. |
Chris Lattner | 04f8019 | 2006-08-15 04:55:54 +0000 | [diff] [blame] | 594 | MatchRHSPunctuation(tok::r_square, Loc); |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 595 | break; |
Chris Lattner | 6d28d9b | 2006-08-24 03:51:22 +0000 | [diff] [blame] | 596 | } |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 597 | |
Chris Lattner | 6d28d9b | 2006-08-24 03:51:22 +0000 | [diff] [blame] | 598 | case tok::l_paren: { // p-e: p-e '(' argument-expression-list[opt] ')' |
| 599 | SmallVector<ExprTy*, 8> ArgExprs; |
| 600 | SmallVector<SourceLocation, 8> CommaLocs; |
| 601 | bool ArgExprsOk = true; |
| 602 | |
Chris Lattner | 0413237 | 2006-10-16 06:12:55 +0000 | [diff] [blame] | 603 | Loc = ConsumeParen(); |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 604 | |
Chris Lattner | 0c6c034 | 2006-08-12 18:12:45 +0000 | [diff] [blame] | 605 | if (Tok.getKind() != tok::r_paren) { |
| 606 | while (1) { |
Chris Lattner | 6d28d9b | 2006-08-24 03:51:22 +0000 | [diff] [blame] | 607 | ExprResult ArgExpr = ParseAssignmentExpression(); |
| 608 | if (ArgExpr.isInvalid) |
| 609 | ArgExprsOk = false; |
| 610 | else |
| 611 | ArgExprs.push_back(ArgExpr.Val); |
| 612 | |
Chris Lattner | 0c6c034 | 2006-08-12 18:12:45 +0000 | [diff] [blame] | 613 | if (Tok.getKind() != tok::comma) |
| 614 | break; |
Chris Lattner | af63531 | 2006-10-16 06:06:51 +0000 | [diff] [blame] | 615 | // Move to the next argument, remember where the comma was. |
| 616 | CommaLocs.push_back(ConsumeToken()); |
Chris Lattner | 0c6c034 | 2006-08-12 18:12:45 +0000 | [diff] [blame] | 617 | } |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 618 | } |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 619 | |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 620 | // Match the ')'. |
Chris Lattner | e165d94 | 2006-08-24 04:40:38 +0000 | [diff] [blame] | 621 | if (!LHS.isInvalid && ArgExprsOk && Tok.getKind() == tok::r_paren) { |
| 622 | assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&& |
| 623 | "Unexpected number of commas!"); |
Chris Lattner | 6d28d9b | 2006-08-24 03:51:22 +0000 | [diff] [blame] | 624 | LHS = Actions.ParseCallExpr(LHS.Val, Loc, &ArgExprs[0], ArgExprs.size(), |
Chris Lattner | e165d94 | 2006-08-24 04:40:38 +0000 | [diff] [blame] | 625 | &CommaLocs[0], Tok.getLocation()); |
| 626 | } |
Chris Lattner | 6d28d9b | 2006-08-24 03:51:22 +0000 | [diff] [blame] | 627 | |
Chris Lattner | 04f8019 | 2006-08-15 04:55:54 +0000 | [diff] [blame] | 628 | MatchRHSPunctuation(tok::r_paren, Loc); |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 629 | break; |
Chris Lattner | 6d28d9b | 2006-08-24 03:51:22 +0000 | [diff] [blame] | 630 | } |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 631 | case tok::arrow: // postfix-expression: p-e '->' identifier |
Chris Lattner | 6d28d9b | 2006-08-24 03:51:22 +0000 | [diff] [blame] | 632 | case tok::period: { // postfix-expression: p-e '.' identifier |
Chris Lattner | 6d28d9b | 2006-08-24 03:51:22 +0000 | [diff] [blame] | 633 | tok::TokenKind OpKind = Tok.getKind(); |
Chris Lattner | af63531 | 2006-10-16 06:06:51 +0000 | [diff] [blame] | 634 | SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token. |
Chris Lattner | 6d28d9b | 2006-08-24 03:51:22 +0000 | [diff] [blame] | 635 | |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 636 | if (Tok.getKind() != tok::identifier) { |
| 637 | Diag(Tok, diag::err_expected_ident); |
| 638 | return ExprResult(true); |
| 639 | } |
Chris Lattner | 6d28d9b | 2006-08-24 03:51:22 +0000 | [diff] [blame] | 640 | |
| 641 | if (!LHS.isInvalid) |
| 642 | LHS = Actions.ParseMemberReferenceExpr(LHS.Val, OpLoc, OpKind, |
| 643 | Tok.getLocation(), |
| 644 | *Tok.getIdentifierInfo()); |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 645 | ConsumeToken(); |
| 646 | break; |
Chris Lattner | 6d28d9b | 2006-08-24 03:51:22 +0000 | [diff] [blame] | 647 | } |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 648 | case tok::plusplus: // postfix-expression: postfix-expression '++' |
| 649 | case tok::minusminus: // postfix-expression: postfix-expression '--' |
Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame] | 650 | if (!LHS.isInvalid) |
Chris Lattner | ae31969 | 2006-10-25 03:49:28 +0000 | [diff] [blame] | 651 | LHS = Actions.ParsePostfixUnaryOp(Tok.getLocation(), Tok.getKind(), |
| 652 | LHS.Val); |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 653 | ConsumeToken(); |
| 654 | break; |
Chris Lattner | f833977 | 2006-08-10 22:01:51 +0000 | [diff] [blame] | 655 | } |
| 656 | } |
Chris Lattner | 52a99e5 | 2006-08-10 20:56:00 +0000 | [diff] [blame] | 657 | } |
| 658 | |
Chris Lattner | 20c6a45 | 2006-08-12 17:40:43 +0000 | [diff] [blame] | 659 | |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 660 | /// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression. |
| 661 | /// unary-expression: [C99 6.5.3] |
| 662 | /// 'sizeof' unary-expression |
| 663 | /// 'sizeof' '(' type-name ')' |
| 664 | /// [GNU] '__alignof' unary-expression |
| 665 | /// [GNU] '__alignof' '(' type-name ')' |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 666 | Parser::ExprResult Parser::ParseSizeofAlignofExpression() { |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 667 | assert((Tok.getKind() == tok::kw_sizeof || |
| 668 | Tok.getKind() == tok::kw___alignof) && |
| 669 | "Not a sizeof/alignof expression!"); |
Chris Lattner | 26115ac | 2006-08-24 06:10:04 +0000 | [diff] [blame] | 670 | LexerToken OpTok = Tok; |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 671 | ConsumeToken(); |
| 672 | |
| 673 | // If the operand doesn't start with an '(', it must be an expression. |
Chris Lattner | 26115ac | 2006-08-24 06:10:04 +0000 | [diff] [blame] | 674 | ExprResult Operand; |
| 675 | if (Tok.getKind() != tok::l_paren) { |
| 676 | Operand = ParseCastExpression(true); |
| 677 | } else { |
| 678 | // If it starts with a '(', we know that it is either a parenthesized |
| 679 | // type-name, or it is a unary-expression that starts with a compound |
| 680 | // literal, or starts with a primary-expression that is a parenthesized |
| 681 | // expression. |
| 682 | ParenParseOption ExprType = CastExpr; |
Chris Lattner | e550a4e | 2006-08-24 06:37:51 +0000 | [diff] [blame] | 683 | TypeTy *CastTy; |
Chris Lattner | 26da730 | 2006-08-24 06:49:19 +0000 | [diff] [blame] | 684 | SourceLocation LParenLoc = Tok.getLocation(), RParenLoc; |
Chris Lattner | e550a4e | 2006-08-24 06:37:51 +0000 | [diff] [blame] | 685 | Operand = ParseParenExpression(ExprType, CastTy, RParenLoc); |
Chris Lattner | 26115ac | 2006-08-24 06:10:04 +0000 | [diff] [blame] | 686 | |
| 687 | // If ParseParenExpression parsed a '(typename)' sequence only, the this is |
| 688 | // sizeof/alignof a type. Otherwise, it is sizeof/alignof an expression. |
| 689 | if (ExprType == CastExpr) { |
Chris Lattner | 26da730 | 2006-08-24 06:49:19 +0000 | [diff] [blame] | 690 | return Actions.ParseSizeOfAlignOfTypeExpr(OpTok.getLocation(), |
| 691 | OpTok.getKind() == tok::kw_sizeof, |
| 692 | LParenLoc, CastTy, RParenLoc); |
Chris Lattner | 26115ac | 2006-08-24 06:10:04 +0000 | [diff] [blame] | 693 | } |
| 694 | } |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 695 | |
Chris Lattner | 26115ac | 2006-08-24 06:10:04 +0000 | [diff] [blame] | 696 | // If we get here, the operand to the sizeof/alignof was an expresion. |
| 697 | if (!Operand.isInvalid) |
Chris Lattner | 0ba3dc4 | 2006-10-25 03:38:23 +0000 | [diff] [blame] | 698 | Operand = Actions.ParseUnaryOp(OpTok.getLocation(), OpTok.getKind(), |
| 699 | Operand.Val); |
Chris Lattner | 26115ac | 2006-08-24 06:10:04 +0000 | [diff] [blame] | 700 | return Operand; |
Chris Lattner | 81b576e | 2006-08-11 02:13:20 +0000 | [diff] [blame] | 701 | } |
| 702 | |
Chris Lattner | 1112435 | 2006-08-12 19:16:08 +0000 | [diff] [blame] | 703 | /// ParseBuiltinPrimaryExpression |
| 704 | /// |
| 705 | /// primary-expression: [C99 6.5.1] |
| 706 | /// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')' |
| 707 | /// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')' |
| 708 | /// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ',' |
| 709 | /// assign-expr ')' |
| 710 | /// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')' |
| 711 | /// |
| 712 | /// [GNU] offsetof-member-designator: |
| 713 | /// [GNU] identifier |
| 714 | /// [GNU] offsetof-member-designator '.' identifier |
| 715 | /// [GNU] offsetof-member-designator '[' expression ']' |
| 716 | /// |
| 717 | Parser::ExprResult Parser::ParseBuiltinPrimaryExpression() { |
| 718 | ExprResult Res(false); |
Chris Lattner | 1112435 | 2006-08-12 19:16:08 +0000 | [diff] [blame] | 719 | const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo(); |
| 720 | |
| 721 | tok::TokenKind T = Tok.getKind(); |
Chris Lattner | af63531 | 2006-10-16 06:06:51 +0000 | [diff] [blame] | 722 | SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier. |
Chris Lattner | 1112435 | 2006-08-12 19:16:08 +0000 | [diff] [blame] | 723 | |
| 724 | // All of these start with an open paren. |
| 725 | if (Tok.getKind() != tok::l_paren) { |
| 726 | Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName()); |
| 727 | return ExprResult(true); |
| 728 | } |
| 729 | |
Chris Lattner | 0413237 | 2006-10-16 06:12:55 +0000 | [diff] [blame] | 730 | SourceLocation LParenLoc = ConsumeParen(); |
Chris Lattner | 6d28d9b | 2006-08-24 03:51:22 +0000 | [diff] [blame] | 731 | // TODO: Build AST. |
| 732 | |
Chris Lattner | 1112435 | 2006-08-12 19:16:08 +0000 | [diff] [blame] | 733 | switch (T) { |
| 734 | default: assert(0 && "Not a builtin primary expression!"); |
| 735 | case tok::kw___builtin_va_arg: |
| 736 | Res = ParseAssignmentExpression(); |
| 737 | if (Res.isInvalid) { |
| 738 | SkipUntil(tok::r_paren); |
| 739 | return Res; |
| 740 | } |
Chris Lattner | 0be454e | 2006-08-12 19:30:51 +0000 | [diff] [blame] | 741 | |
Chris Lattner | 6d7e634 | 2006-08-15 03:41:14 +0000 | [diff] [blame] | 742 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) |
Chris Lattner | 1112435 | 2006-08-12 19:16:08 +0000 | [diff] [blame] | 743 | return ExprResult(true); |
Chris Lattner | 0be454e | 2006-08-12 19:30:51 +0000 | [diff] [blame] | 744 | |
Chris Lattner | 1112435 | 2006-08-12 19:16:08 +0000 | [diff] [blame] | 745 | ParseTypeName(); |
| 746 | break; |
| 747 | |
| 748 | case tok::kw___builtin_offsetof: |
| 749 | ParseTypeName(); |
| 750 | |
Chris Lattner | 6d7e634 | 2006-08-15 03:41:14 +0000 | [diff] [blame] | 751 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) |
Chris Lattner | 1112435 | 2006-08-12 19:16:08 +0000 | [diff] [blame] | 752 | return ExprResult(true); |
Chris Lattner | 1112435 | 2006-08-12 19:16:08 +0000 | [diff] [blame] | 753 | |
| 754 | // We must have at least one identifier here. |
Chris Lattner | 6d7e634 | 2006-08-15 03:41:14 +0000 | [diff] [blame] | 755 | if (ExpectAndConsume(tok::identifier, diag::err_expected_ident, "", |
Chris Lattner | dbb2a46 | 2006-08-12 19:26:13 +0000 | [diff] [blame] | 756 | tok::r_paren)) |
Chris Lattner | 1112435 | 2006-08-12 19:16:08 +0000 | [diff] [blame] | 757 | return ExprResult(true); |
Chris Lattner | dbb2a46 | 2006-08-12 19:26:13 +0000 | [diff] [blame] | 758 | |
Chris Lattner | 1112435 | 2006-08-12 19:16:08 +0000 | [diff] [blame] | 759 | while (1) { |
| 760 | if (Tok.getKind() == tok::period) { |
| 761 | // offsetof-member-designator: offsetof-member-designator '.' identifier |
| 762 | ConsumeToken(); |
| 763 | |
Chris Lattner | 6d7e634 | 2006-08-15 03:41:14 +0000 | [diff] [blame] | 764 | if (ExpectAndConsume(tok::identifier, diag::err_expected_ident, "", |
Chris Lattner | dbb2a46 | 2006-08-12 19:26:13 +0000 | [diff] [blame] | 765 | tok::r_paren)) |
Chris Lattner | 1112435 | 2006-08-12 19:16:08 +0000 | [diff] [blame] | 766 | return ExprResult(true); |
Chris Lattner | 1112435 | 2006-08-12 19:16:08 +0000 | [diff] [blame] | 767 | } else if (Tok.getKind() == tok::l_square) { |
| 768 | // offsetof-member-designator: offsetof-member-design '[' expression ']' |
Chris Lattner | 0413237 | 2006-10-16 06:12:55 +0000 | [diff] [blame] | 769 | SourceLocation LSquareLoc = ConsumeBracket(); |
Chris Lattner | 1112435 | 2006-08-12 19:16:08 +0000 | [diff] [blame] | 770 | Res = ParseExpression(); |
| 771 | if (Res.isInvalid) { |
| 772 | SkipUntil(tok::r_paren); |
| 773 | return Res; |
| 774 | } |
| 775 | |
Chris Lattner | 04f8019 | 2006-08-15 04:55:54 +0000 | [diff] [blame] | 776 | MatchRHSPunctuation(tok::r_square, LSquareLoc); |
Chris Lattner | 1112435 | 2006-08-12 19:16:08 +0000 | [diff] [blame] | 777 | } else { |
| 778 | break; |
| 779 | } |
| 780 | } |
| 781 | break; |
| 782 | case tok::kw___builtin_choose_expr: |
| 783 | Res = ParseAssignmentExpression(); |
| 784 | |
Chris Lattner | 6d7e634 | 2006-08-15 03:41:14 +0000 | [diff] [blame] | 785 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) |
Chris Lattner | 1112435 | 2006-08-12 19:16:08 +0000 | [diff] [blame] | 786 | return ExprResult(true); |
Chris Lattner | 1112435 | 2006-08-12 19:16:08 +0000 | [diff] [blame] | 787 | |
| 788 | Res = ParseAssignmentExpression(); |
| 789 | |
Chris Lattner | 6d7e634 | 2006-08-15 03:41:14 +0000 | [diff] [blame] | 790 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) |
Chris Lattner | 1112435 | 2006-08-12 19:16:08 +0000 | [diff] [blame] | 791 | return ExprResult(true); |
Chris Lattner | 1112435 | 2006-08-12 19:16:08 +0000 | [diff] [blame] | 792 | |
| 793 | Res = ParseAssignmentExpression(); |
| 794 | break; |
| 795 | case tok::kw___builtin_types_compatible_p: |
| 796 | ParseTypeName(); |
| 797 | |
Chris Lattner | 6d7e634 | 2006-08-15 03:41:14 +0000 | [diff] [blame] | 798 | if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren)) |
Chris Lattner | 1112435 | 2006-08-12 19:16:08 +0000 | [diff] [blame] | 799 | return ExprResult(true); |
Chris Lattner | 1112435 | 2006-08-12 19:16:08 +0000 | [diff] [blame] | 800 | |
| 801 | ParseTypeName(); |
| 802 | break; |
| 803 | } |
| 804 | |
Chris Lattner | 04f8019 | 2006-08-15 04:55:54 +0000 | [diff] [blame] | 805 | MatchRHSPunctuation(tok::r_paren, LParenLoc); |
Chris Lattner | 1112435 | 2006-08-12 19:16:08 +0000 | [diff] [blame] | 806 | |
| 807 | // These can be followed by postfix-expr pieces because they are |
| 808 | // primary-expressions. |
| 809 | return ParsePostfixExpressionSuffix(Res); |
| 810 | } |
| 811 | |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 812 | |
Chris Lattner | 4add4e6 | 2006-08-11 01:33:00 +0000 | [diff] [blame] | 813 | /// ParseParenExpression - This parses the unit that starts with a '(' token, |
| 814 | /// based on what is allowed by ExprType. The actual thing parsed is returned |
| 815 | /// in ExprType. |
| 816 | /// |
| 817 | /// primary-expression: [C99 6.5.1] |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 818 | /// '(' expression ')' |
Chris Lattner | f833977 | 2006-08-10 22:01:51 +0000 | [diff] [blame] | 819 | /// [GNU] '(' compound-statement ')' (if !ParenExprOnly) |
| 820 | /// postfix-expression: [C99 6.5.2] |
| 821 | /// '(' type-name ')' '{' initializer-list '}' |
| 822 | /// '(' type-name ')' '{' initializer-list ',' '}' |
Chris Lattner | 4add4e6 | 2006-08-11 01:33:00 +0000 | [diff] [blame] | 823 | /// cast-expression: [C99 6.5.4] |
| 824 | /// '(' type-name ')' cast-expression |
Chris Lattner | f833977 | 2006-08-10 22:01:51 +0000 | [diff] [blame] | 825 | /// |
Chris Lattner | e550a4e | 2006-08-24 06:37:51 +0000 | [diff] [blame] | 826 | Parser::ExprResult Parser::ParseParenExpression(ParenParseOption &ExprType, |
| 827 | TypeTy *&CastTy, |
| 828 | SourceLocation &RParenLoc) { |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 829 | assert(Tok.getKind() == tok::l_paren && "Not a paren expr!"); |
Chris Lattner | 0413237 | 2006-10-16 06:12:55 +0000 | [diff] [blame] | 830 | SourceLocation OpenLoc = ConsumeParen(); |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 831 | ExprResult Result(false); |
Chris Lattner | e550a4e | 2006-08-24 06:37:51 +0000 | [diff] [blame] | 832 | CastTy = 0; |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 833 | |
Chris Lattner | 4add4e6 | 2006-08-11 01:33:00 +0000 | [diff] [blame] | 834 | if (ExprType >= CompoundStmt && Tok.getKind() == tok::l_brace && |
Chris Lattner | f833977 | 2006-08-10 22:01:51 +0000 | [diff] [blame] | 835 | !getLang().NoExtensions) { |
| 836 | Diag(Tok, diag::ext_gnu_statement_expr); |
| 837 | ParseCompoundStatement(); |
Chris Lattner | 4add4e6 | 2006-08-11 01:33:00 +0000 | [diff] [blame] | 838 | ExprType = CompoundStmt; |
Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame] | 839 | // TODO: Build AST for GNU compound stmt. |
Chris Lattner | 4add4e6 | 2006-08-11 01:33:00 +0000 | [diff] [blame] | 840 | } else if (ExprType >= CompoundLiteral && isTypeSpecifierQualifier()) { |
Chris Lattner | 6c3f05d | 2006-08-12 16:54:25 +0000 | [diff] [blame] | 841 | // Otherwise, this is a compound literal expression or cast expression. |
Chris Lattner | e550a4e | 2006-08-24 06:37:51 +0000 | [diff] [blame] | 842 | TypeTy *Ty = ParseTypeName(); |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 843 | |
| 844 | // Match the ')'. |
Chris Lattner | 0413237 | 2006-10-16 06:12:55 +0000 | [diff] [blame] | 845 | if (Tok.getKind() == tok::r_paren) |
| 846 | RParenLoc = ConsumeParen(); |
| 847 | else |
Chris Lattner | e550a4e | 2006-08-24 06:37:51 +0000 | [diff] [blame] | 848 | MatchRHSPunctuation(tok::r_paren, OpenLoc); |
Chris Lattner | e550a4e | 2006-08-24 06:37:51 +0000 | [diff] [blame] | 849 | |
Chris Lattner | 4add4e6 | 2006-08-11 01:33:00 +0000 | [diff] [blame] | 850 | if (Tok.getKind() == tok::l_brace) { |
Chris Lattner | 6c3f05d | 2006-08-12 16:54:25 +0000 | [diff] [blame] | 851 | if (!getLang().C99) // Compound literals don't exist in C90. |
| 852 | Diag(OpenLoc, diag::ext_c99_compound_literal); |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 853 | Result = ParseInitializer(); |
Chris Lattner | 4add4e6 | 2006-08-11 01:33:00 +0000 | [diff] [blame] | 854 | ExprType = CompoundLiteral; |
Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame] | 855 | // TODO: Build AST for compound literal. |
Chris Lattner | 4add4e6 | 2006-08-11 01:33:00 +0000 | [diff] [blame] | 856 | } else if (ExprType == CastExpr) { |
Chris Lattner | e550a4e | 2006-08-24 06:37:51 +0000 | [diff] [blame] | 857 | // Note that this doesn't parse the subsequence cast-expression, it just |
| 858 | // returns the parsed type to the callee. |
Chris Lattner | 4add4e6 | 2006-08-11 01:33:00 +0000 | [diff] [blame] | 859 | ExprType = CastExpr; |
Chris Lattner | e550a4e | 2006-08-24 06:37:51 +0000 | [diff] [blame] | 860 | CastTy = Ty; |
| 861 | return ExprResult(false); |
Chris Lattner | 4add4e6 | 2006-08-11 01:33:00 +0000 | [diff] [blame] | 862 | } else { |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 863 | Diag(Tok, diag::err_expected_lbrace_in_compound_literal); |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 864 | return ExprResult(true); |
Chris Lattner | f5fbd79 | 2006-08-10 23:56:11 +0000 | [diff] [blame] | 865 | } |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 866 | return Result; |
Chris Lattner | 4add4e6 | 2006-08-11 01:33:00 +0000 | [diff] [blame] | 867 | } else { |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 868 | Result = ParseExpression(); |
Chris Lattner | 4add4e6 | 2006-08-11 01:33:00 +0000 | [diff] [blame] | 869 | ExprType = SimpleExpr; |
Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame] | 870 | if (!Result.isInvalid && Tok.getKind() == tok::r_paren) |
| 871 | Result = Actions.ParseParenExpr(OpenLoc, Tok.getLocation(), Result.Val); |
Chris Lattner | f833977 | 2006-08-10 22:01:51 +0000 | [diff] [blame] | 872 | } |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 873 | |
Chris Lattner | 4564bc1 | 2006-08-10 23:14:52 +0000 | [diff] [blame] | 874 | // Match the ')'. |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 875 | if (Result.isInvalid) |
| 876 | SkipUntil(tok::r_paren); |
Chris Lattner | e550a4e | 2006-08-24 06:37:51 +0000 | [diff] [blame] | 877 | else { |
Chris Lattner | 0413237 | 2006-10-16 06:12:55 +0000 | [diff] [blame] | 878 | if (Tok.getKind() == tok::r_paren) |
| 879 | RParenLoc = ConsumeParen(); |
| 880 | else |
Chris Lattner | e550a4e | 2006-08-24 06:37:51 +0000 | [diff] [blame] | 881 | MatchRHSPunctuation(tok::r_paren, OpenLoc); |
Chris Lattner | e550a4e | 2006-08-24 06:37:51 +0000 | [diff] [blame] | 882 | } |
Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame] | 883 | |
Chris Lattner | 89c50c6 | 2006-08-11 06:41:18 +0000 | [diff] [blame] | 884 | return Result; |
Chris Lattner | c951dae | 2006-08-10 04:23:57 +0000 | [diff] [blame] | 885 | } |
Chris Lattner | d3e9895 | 2006-10-06 05:22:26 +0000 | [diff] [blame] | 886 | |
| 887 | /// HexDigitValue - Return the value of the specified hex digit, or -1 if it's |
| 888 | /// not valid. |
| 889 | static int HexDigitValue(char C) { |
| 890 | if (C >= '0' && C <= '9') return C-'0'; |
| 891 | if (C >= 'a' && C <= 'f') return C-'a'+10; |
| 892 | if (C >= 'A' && C <= 'F') return C-'A'+10; |
| 893 | return -1; |
| 894 | } |
| 895 | |
| 896 | /// ParseStringLiteralExpression - This handles the various token types that |
| 897 | /// form string literals, and also handles string concatenation [C99 5.1.1.2, |
| 898 | /// translation phase #6]. |
| 899 | /// |
| 900 | /// primary-expression: [C99 6.5.1] |
| 901 | /// string-literal |
| 902 | Parser::ExprResult Parser::ParseStringLiteralExpression() { |
| 903 | assert(isTokenStringLiteral() && "Not a string literal!"); |
| 904 | |
| 905 | // String concat. Note that keywords like __func__ and __FUNCTION__ are not |
| 906 | // considered to be strings for concatenation purposes. |
| 907 | SmallVector<LexerToken, 4> StringToks; |
| 908 | |
| 909 | // While we're looking at all of the string portions, remember the max |
| 910 | // individual token length, computing a bound on the concatenated string |
| 911 | // length, and see whether any piece is a wide-string. If any of the string |
| 912 | // portions is a wide-string literal, the result is also a wide-string literal |
| 913 | // [C99 6.4.5p4]. |
| 914 | unsigned SizeBound = 0, MaxTokenLength = 0; |
| 915 | bool AnyWide = false; |
| 916 | do { |
| 917 | // The string could be shorter than this if it needs cleaning, but this is a |
| 918 | // reasonable bound, which is all we need. |
| 919 | SizeBound += Tok.getLength()-2; // -2 for "". |
| 920 | |
| 921 | // Find maximum string piece length. |
| 922 | if (Tok.getLength() > MaxTokenLength) |
| 923 | MaxTokenLength = Tok.getLength(); |
| 924 | |
| 925 | // Remember if we see any wide strings. |
| 926 | AnyWide |= Tok.getKind() == tok::wide_string_literal; |
| 927 | |
| 928 | // Remember the string token. |
| 929 | StringToks.push_back(Tok); |
| 930 | ConsumeStringToken(); |
| 931 | } while (isTokenStringLiteral()); |
| 932 | |
| 933 | // Include space for the null terminator. |
| 934 | ++SizeBound; |
| 935 | |
| 936 | // TODO: K&R warning: "traditional C rejects string constant concatenation" |
| 937 | |
Chris Lattner | 02dffbd | 2006-10-14 07:50:21 +0000 | [diff] [blame] | 938 | // Get the width in bytes of wchar_t. If no wchar_t strings are used, do not |
| 939 | // query the target. As such, wchar_tByteWidth is only valid if AnyWide=true. |
| 940 | unsigned wchar_tByteWidth = ~0U; |
| 941 | if (AnyWide) |
| 942 | wchar_tByteWidth=getTargetInfo().getWCharWidth(StringToks[0].getLocation()); |
Chris Lattner | d3e9895 | 2006-10-06 05:22:26 +0000 | [diff] [blame] | 943 | |
| 944 | // The output buffer size needs to be large enough to hold wide characters. |
| 945 | // This is a worst-case assumption which basically corresponds to L"" "long". |
| 946 | if (AnyWide) |
| 947 | SizeBound *= wchar_tByteWidth; |
| 948 | |
| 949 | // Create a temporary buffer to hold the result string data. If it is "big", |
| 950 | // use malloc, otherwise use alloca. |
| 951 | char *ResultBuf; |
| 952 | if (SizeBound > 512) |
| 953 | ResultBuf = (char*)malloc(SizeBound); |
| 954 | else |
| 955 | ResultBuf = (char*)alloca(SizeBound); |
| 956 | |
| 957 | // Likewise, but for each string piece. |
| 958 | char *TokenBuf; |
| 959 | if (MaxTokenLength > 512) |
| 960 | TokenBuf = (char*)malloc(MaxTokenLength); |
| 961 | else |
| 962 | TokenBuf = (char*)alloca(MaxTokenLength); |
| 963 | |
| 964 | // Loop over all the strings, getting their spelling, and expanding them to |
| 965 | // wide strings as appropriate. |
| 966 | char *ResultPtr = ResultBuf; // Next byte to fill in. |
| 967 | |
| 968 | for (unsigned i = 0, e = StringToks.size(); i != e; ++i) { |
| 969 | const char *ThisTokBuf = TokenBuf; |
| 970 | // Get the spelling of the token, which eliminates trigraphs, etc. We know |
| 971 | // that ThisTokBuf points to a buffer that is big enough for the whole token |
| 972 | // and 'spelled' tokens can only shrink. |
| 973 | unsigned ThisTokLen = PP.getSpelling(StringToks[i], ThisTokBuf); |
| 974 | const char *ThisTokEnd = ThisTokBuf+ThisTokLen-1; // Skip end quote. |
| 975 | |
| 976 | // TODO: Input character set mapping support. |
| 977 | |
| 978 | // Skip L marker for wide strings. |
| 979 | if (ThisTokBuf[0] == 'L') ++ThisTokBuf; |
| 980 | |
| 981 | assert(ThisTokBuf[0] == '"' && "Expected quote, lexer broken?"); |
| 982 | ++ThisTokBuf; |
| 983 | |
| 984 | while (ThisTokBuf != ThisTokEnd) { |
| 985 | // Is this a span of non-escape characters? |
| 986 | if (ThisTokBuf[0] != '\\') { |
| 987 | const char *InStart = ThisTokBuf; |
| 988 | do { |
| 989 | ++ThisTokBuf; |
| 990 | } while (ThisTokBuf != ThisTokEnd && ThisTokBuf[0] != '\\'); |
| 991 | |
| 992 | // Copy the character span over. |
| 993 | unsigned Len = ThisTokBuf-InStart; |
| 994 | if (!AnyWide) { |
| 995 | memcpy(ResultPtr, InStart, Len); |
| 996 | ResultPtr += Len; |
| 997 | } else { |
| 998 | // Note: our internal rep of wide char tokens is always little-endian. |
| 999 | for (; Len; --Len, ++InStart) { |
| 1000 | *ResultPtr++ = InStart[0]; |
| 1001 | // Add zeros at the end. |
| 1002 | for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i) |
| 1003 | *ResultPtr++ = 0; |
| 1004 | } |
| 1005 | } |
| 1006 | continue; |
| 1007 | } |
| 1008 | |
| 1009 | // Otherwise, this is an escape character. Skip the '\' char. |
| 1010 | ++ThisTokBuf; |
| 1011 | |
| 1012 | // We know that this character can't be off the end of the buffer, because |
| 1013 | // that would have been \", which would not have been the end of string. |
| 1014 | unsigned ResultChar = *ThisTokBuf++; |
| 1015 | switch (ResultChar) { |
| 1016 | // These map to themselves. |
| 1017 | case '\\': case '\'': case '"': case '?': break; |
| 1018 | |
| 1019 | // These have fixed mappings. |
| 1020 | case 'a': |
| 1021 | // TODO: K&R: the meaning of '\\a' is different in traditional C |
| 1022 | ResultChar = 7; |
| 1023 | break; |
| 1024 | case 'b': |
| 1025 | ResultChar = 8; |
| 1026 | break; |
| 1027 | case 'e': |
| 1028 | PP.Diag(StringToks[i], diag::ext_nonstandard_escape, "e"); |
| 1029 | ResultChar = 27; |
| 1030 | break; |
| 1031 | case 'f': |
| 1032 | ResultChar = 12; |
| 1033 | break; |
| 1034 | case 'n': |
| 1035 | ResultChar = 10; |
| 1036 | break; |
| 1037 | case 'r': |
| 1038 | ResultChar = 13; |
| 1039 | break; |
| 1040 | case 't': |
| 1041 | ResultChar = 9; |
| 1042 | break; |
| 1043 | case 'v': |
| 1044 | ResultChar = 11; |
| 1045 | break; |
| 1046 | |
| 1047 | //case 'u': case 'U': // FIXME: UCNs. |
| 1048 | case 'x': // Hex escape. |
| 1049 | if (ThisTokBuf == ThisTokEnd || |
| 1050 | (ResultChar = HexDigitValue(*ThisTokBuf)) == ~0U) { |
| 1051 | PP.Diag(StringToks[i], diag::err_hex_escape_no_digits); |
| 1052 | ResultChar = 0; |
| 1053 | break; |
| 1054 | } |
| 1055 | ++ThisTokBuf; // Consumed one hex digit. |
| 1056 | |
| 1057 | assert(0 && "hex escape: unimp!"); |
| 1058 | break; |
| 1059 | case '0': case '1': case '2': case '3': |
| 1060 | case '4': case '5': case '6': case '7': |
| 1061 | // Octal escapes. |
| 1062 | assert(0 && "octal escape: unimp!"); |
| 1063 | break; |
| 1064 | |
| 1065 | // Otherwise, these are not valid escapes. |
| 1066 | case '(': case '{': case '[': case '%': |
| 1067 | // GCC accepts these as extensions. We warn about them as such though. |
| 1068 | if (!PP.getLangOptions().NoExtensions) { |
| 1069 | PP.Diag(StringToks[i], diag::ext_nonstandard_escape, |
| 1070 | std::string()+(char)ResultChar); |
| 1071 | break; |
| 1072 | } |
| 1073 | // FALL THROUGH. |
| 1074 | default: |
| 1075 | if (isgraph(ThisTokBuf[0])) { |
| 1076 | PP.Diag(StringToks[i], diag::ext_unknown_escape, |
| 1077 | std::string()+(char)ResultChar); |
| 1078 | } else { |
| 1079 | PP.Diag(StringToks[i], diag::ext_unknown_escape, |
| 1080 | "x"+utohexstr(ResultChar)); |
| 1081 | } |
| 1082 | } |
| 1083 | |
| 1084 | // Note: our internal rep of wide char tokens is always little-endian. |
Chris Lattner | 02dffbd | 2006-10-14 07:50:21 +0000 | [diff] [blame] | 1085 | *ResultPtr++ = ResultChar & 0xFF; |
| 1086 | |
| 1087 | if (AnyWide) { |
| 1088 | for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i) |
| 1089 | *ResultPtr++ = ResultChar >> i*8; |
| 1090 | } |
Chris Lattner | d3e9895 | 2006-10-06 05:22:26 +0000 | [diff] [blame] | 1091 | } |
| 1092 | } |
| 1093 | |
| 1094 | // Add zero terminator. |
| 1095 | *ResultPtr = 0; |
| 1096 | if (AnyWide) { |
| 1097 | for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i) |
| 1098 | *ResultPtr++ = 0; |
| 1099 | } |
| 1100 | |
Chris Lattner | ae31969 | 2006-10-25 03:49:28 +0000 | [diff] [blame] | 1101 | SmallVector<SourceLocation, 4> StringTokLocs; |
| 1102 | for (unsigned i = 0; i != StringToks.size(); ++i) |
| 1103 | StringTokLocs.push_back(StringToks[i].getLocation()); |
| 1104 | |
Chris Lattner | d3e9895 | 2006-10-06 05:22:26 +0000 | [diff] [blame] | 1105 | // Hand this off to the Actions. |
| 1106 | ExprResult Res = Actions.ParseStringExpr(ResultBuf, ResultPtr-ResultBuf, |
Chris Lattner | ae31969 | 2006-10-25 03:49:28 +0000 | [diff] [blame] | 1107 | AnyWide, &StringTokLocs[0], |
| 1108 | StringTokLocs.size()); |
Chris Lattner | d3e9895 | 2006-10-06 05:22:26 +0000 | [diff] [blame] | 1109 | |
| 1110 | // If either buffer was heap allocated, release it now. |
| 1111 | if (MaxTokenLength > 512) free(TokenBuf); |
| 1112 | if (SizeBound > 512) free(ResultBuf); |
| 1113 | |
| 1114 | return Res; |
| 1115 | } |
| 1116 | |