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