blob: 3b641e7c6d72e37437e1196ddc834ab271be8a0a [file] [log] [blame]
Chris Lattnerc951dae2006-08-10 04:23:57 +00001//===--- 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 Lattnercde626a2006-08-12 08:13:25 +000010// 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 Lattnerb7f1fc92006-08-12 16:45:01 +000016// operator (e.g. '/') or a ternary operator ("?:"). The unary leaves are
Chris Lattnercde626a2006-08-12 08:13:25 +000017// handled by ParseCastExpression, the higher level pieces are handled by
18// ParseBinaryExpression.
Chris Lattnerc951dae2006-08-10 04:23:57 +000019//
20//===----------------------------------------------------------------------===//
21
22#include "clang/Parse/Parser.h"
23#include "clang/Basic/Diagnostic.h"
Chris Lattner6d28d9b2006-08-24 03:51:22 +000024#include "llvm/ADT/SmallVector.h"
Chris Lattnerd3e98952006-10-06 05:22:26 +000025#include "llvm/ADT/StringExtras.h"
26#include "llvm/Config/Alloca.h"
Chris Lattnerc951dae2006-08-10 04:23:57 +000027using namespace llvm;
28using namespace clang;
29
Chris Lattnerb7f1fc92006-08-12 16:45:01 +000030/// PrecedenceLevels - These are precedences for the binary/ternary operators in
Chris Lattnercde626a2006-08-12 08:13:25 +000031/// the C99 grammar. These have been named to relate with the C99 grammar
32/// productions. Low precedences numbers bind more weakly than high numbers.
33namespace prec {
34 enum Level {
35 Unknown = 0, // Not binary operator.
36 Comma = 1, // ,
37 Assignment = 2, // =, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |=
38 Conditional = 3, // ?
39 LogicalOr = 4, // ||
40 LogicalAnd = 5, // &&
41 InclusiveOr = 6, // |
42 ExclusiveOr = 7, // ^
43 And = 8, // &
44 MinMax = 9, // <?, >? min, max (GCC extensions)
45 Equality = 10, // ==, !=
46 Relational = 11, // >=, <=, >, <
47 Shift = 12, // <<, >>
48 Additive = 13, // -, +
49 Multiplicative = 14 // *, /, %
50 };
51}
52
53
54/// getBinOpPrecedence - Return the precedence of the specified binary operator
55/// token. This returns:
56///
57static 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;
78 case tok::lessquestion:
79 case tok::greaterquestion: return prec::MinMax;
80 case tok::exclaimequal:
81 case tok::equalequal: return prec::Equality;
82 case tok::lessequal:
83 case tok::less:
84 case tok::greaterequal:
85 case tok::greater: return prec::Relational;
86 case tok::lessless:
87 case tok::greatergreater: return prec::Shift;
88 case tok::plus:
89 case tok::minus: return prec::Additive;
90 case tok::percent:
91 case tok::slash:
92 case tok::star: return prec::Multiplicative;
93 }
94}
95
96
Chris Lattnerce7e21d2006-08-12 17:22:40 +000097/// ParseExpression - Simple precedence-based parser for binary/ternary
Chris Lattnercde626a2006-08-12 08:13:25 +000098/// operators.
99///
Chris Lattnerb7f1fc92006-08-12 16:45:01 +0000100/// Note: we diverge from the C99 grammar when parsing the assignment-expression
101/// production. C99 specifies that the LHS of an assignment operator should be
102/// parsed as a unary-expression, but consistency dictates that it be a
103/// conditional-expession. In practice, the important thing here is that the
104/// LHS of an assignment has to be an l-value, which productions between
105/// unary-expression and conditional-expression don't produce. Because we want
106/// consistency, we parse the LHS as a conditional-expression, then check for
107/// l-value-ness in semantic analysis stages.
108///
Chris Lattnercde626a2006-08-12 08:13:25 +0000109/// multiplicative-expression: [C99 6.5.5]
110/// cast-expression
111/// multiplicative-expression '*' cast-expression
112/// multiplicative-expression '/' cast-expression
113/// multiplicative-expression '%' cast-expression
114///
115/// additive-expression: [C99 6.5.6]
116/// multiplicative-expression
117/// additive-expression '+' multiplicative-expression
118/// additive-expression '-' multiplicative-expression
119///
120/// shift-expression: [C99 6.5.7]
121/// additive-expression
122/// shift-expression '<<' additive-expression
123/// shift-expression '>>' additive-expression
124///
125/// relational-expression: [C99 6.5.8]
126/// shift-expression
127/// relational-expression '<' shift-expression
128/// relational-expression '>' shift-expression
129/// relational-expression '<=' shift-expression
130/// relational-expression '>=' shift-expression
131///
132/// equality-expression: [C99 6.5.9]
133/// relational-expression
134/// equality-expression '==' relational-expression
135/// equality-expression '!=' relational-expression
136///
137/// AND-expression: [C99 6.5.10]
138/// equality-expression
139/// AND-expression '&' equality-expression
140///
141/// exclusive-OR-expression: [C99 6.5.11]
142/// AND-expression
143/// exclusive-OR-expression '^' AND-expression
144///
145/// inclusive-OR-expression: [C99 6.5.12]
146/// exclusive-OR-expression
147/// inclusive-OR-expression '|' exclusive-OR-expression
148///
149/// logical-AND-expression: [C99 6.5.13]
150/// inclusive-OR-expression
151/// logical-AND-expression '&&' inclusive-OR-expression
152///
153/// logical-OR-expression: [C99 6.5.14]
154/// logical-AND-expression
155/// logical-OR-expression '||' logical-AND-expression
156///
157/// conditional-expression: [C99 6.5.15]
158/// logical-OR-expression
159/// logical-OR-expression '?' expression ':' conditional-expression
160/// [GNU] logical-OR-expression '?' ':' conditional-expression
161///
162/// assignment-expression: [C99 6.5.16]
163/// conditional-expression
164/// unary-expression assignment-operator assignment-expression
165///
166/// assignment-operator: one of
167/// = *= /= %= += -= <<= >>= &= ^= |=
168///
169/// expression: [C99 6.5.17]
170/// assignment-expression
171/// expression ',' assignment-expression
172///
Chris Lattnerd35c34f2006-08-12 17:04:50 +0000173Parser::ExprResult Parser::ParseExpression() {
Chris Lattnercde626a2006-08-12 08:13:25 +0000174 ExprResult LHS = ParseCastExpression(false);
175 if (LHS.isInvalid) return LHS;
176
177 return ParseRHSOfBinaryExpression(LHS, prec::Comma);
178}
179
Chris Lattner0c6c0342006-08-12 18:12:45 +0000180/// ParseAssignmentExpression - Parse an expr that doesn't include commas.
181///
Chris Lattnerce7e21d2006-08-12 17:22:40 +0000182Parser::ExprResult Parser::ParseAssignmentExpression() {
183 ExprResult LHS = ParseCastExpression(false);
184 if (LHS.isInvalid) return LHS;
185
186 return ParseRHSOfBinaryExpression(LHS, prec::Assignment);
187}
188
Chris Lattner3b561a32006-08-13 00:12:11 +0000189Parser::ExprResult Parser::ParseConstantExpression() {
190 ExprResult LHS = ParseCastExpression(false);
191 if (LHS.isInvalid) return LHS;
192
193 // TODO: Validate that this is a constant expr!
194 return ParseRHSOfBinaryExpression(LHS, prec::Conditional);
195}
196
Chris Lattner0c6c0342006-08-12 18:12:45 +0000197/// ParseExpressionWithLeadingIdentifier - This special purpose method is used
198/// in contexts where we have already consumed an identifier (which we saved in
199/// 'Tok'), then discovered that the identifier was really the leading token of
200/// part of an expression. For example, in "A[1]+B", we consumed "A" (which is
201/// now in 'Tok') and the current token is "[".
202Parser::ExprResult Parser::
203ParseExpressionWithLeadingIdentifier(const LexerToken &Tok) {
204 // We know that 'Tok' must correspond to this production:
205 // primary-expression: identifier
206
207 // TODO: Pass 'Tok' to the action.
208 ExprResult Res = ExprResult(false);
209
210 // Because we have to parse an entire cast-expression before starting the
211 // ParseRHSOfBinaryExpression method (which parses any trailing binops), we
212 // need to handle the 'postfix-expression' rules. We do this by invoking
213 // ParsePostfixExpressionSuffix to consume any postfix-expression suffixes:
214 Res = ParsePostfixExpressionSuffix(Res);
215 if (Res.isInvalid) return Res;
216
217 // At this point, the "A[1]" part of "A[1]+B" has been consumed. Once this is
218 // done, we know we don't have to do anything for cast-expression, because the
219 // only non-postfix-expression production starts with a '(' token, and we know
220 // we have an identifier. As such, we can invoke ParseRHSOfBinaryExpression
221 // to consume any trailing operators (e.g. "+" in this example) and connected
222 // chunks of the expression.
223 return ParseRHSOfBinaryExpression(Res, prec::Comma);
224}
225
Chris Lattner8693a512006-08-13 21:54:02 +0000226/// ParseExpressionWithLeadingIdentifier - This special purpose method is used
227/// in contexts where we have already consumed an identifier (which we saved in
228/// 'Tok'), then discovered that the identifier was really the leading token of
229/// part of an assignment-expression. For example, in "A[1]+B", we consumed "A"
230/// (which is now in 'Tok') and the current token is "[".
231Parser::ExprResult Parser::
232ParseAssignmentExprWithLeadingIdentifier(const LexerToken &Tok) {
233 // We know that 'Tok' must correspond to this production:
234 // primary-expression: identifier
235
236 // TODO: Pass 'Tok' to the action.
237 ExprResult Res = ExprResult(false);
238
239 // Because we have to parse an entire cast-expression before starting the
240 // ParseRHSOfBinaryExpression method (which parses any trailing binops), we
241 // need to handle the 'postfix-expression' rules. We do this by invoking
242 // ParsePostfixExpressionSuffix to consume any postfix-expression suffixes:
243 Res = ParsePostfixExpressionSuffix(Res);
244 if (Res.isInvalid) return Res;
245
246 // At this point, the "A[1]" part of "A[1]+B" has been consumed. Once this is
247 // done, we know we don't have to do anything for cast-expression, because the
248 // only non-postfix-expression production starts with a '(' token, and we know
249 // we have an identifier. As such, we can invoke ParseRHSOfBinaryExpression
250 // to consume any trailing operators (e.g. "+" in this example) and connected
251 // chunks of the expression.
252 return ParseRHSOfBinaryExpression(Res, prec::Assignment);
253}
254
255
Chris Lattner62591722006-08-12 18:40:58 +0000256/// ParseAssignmentExpressionWithLeadingStar - This special purpose method is
257/// used in contexts where we have already consumed a '*' (which we saved in
258/// 'Tok'), then discovered that the '*' was really the leading token of an
259/// expression. For example, in "*(int*)P+B", we consumed "*" (which is
260/// now in 'Tok') and the current token is "(".
261Parser::ExprResult Parser::
262ParseAssignmentExpressionWithLeadingStar(const LexerToken &Tok) {
263 // We know that 'Tok' must correspond to this production:
264 // unary-expression: unary-operator cast-expression
265 // where 'unary-operator' is '*'.
266
267 // Parse the cast-expression that follows the '*'. This will parse the
268 // "*(int*)P" part of "*(int*)P+B".
269 ExprResult Res = ParseCastExpression(false);
270 if (Res.isInvalid) return Res;
271
272 // TODO: Combine Tok + Res to get the new AST.
273
274 // We have to parse an entire cast-expression before starting the
275 // ParseRHSOfBinaryExpression method (which parses any trailing binops). Since
276 // we know that the only production above us is the cast-expression
277 // production, and because the only alternative productions start with a '('
278 // token (we know we had a '*'), there is no work to do to get a whole
279 // cast-expression.
280
281 // At this point, the "*(int*)P" part of "*(int*)P+B" has been consumed. Once
282 // this is done, we can invoke ParseRHSOfBinaryExpression to consume any
283 // trailing operators (e.g. "+" in this example) and connected chunks of the
284 // assignment-expression.
285 return ParseRHSOfBinaryExpression(Res, prec::Assignment);
286}
287
288
Chris Lattnercde626a2006-08-12 08:13:25 +0000289/// ParseRHSOfBinaryExpression - Parse a binary expression that starts with
290/// LHS and has a precedence of at least MinPrec.
291Parser::ExprResult
292Parser::ParseRHSOfBinaryExpression(ExprResult LHS, unsigned MinPrec) {
293 unsigned NextTokPrec = getBinOpPrecedence(Tok.getKind());
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000294 SourceLocation ColonLoc;
295
Chris Lattnercde626a2006-08-12 08:13:25 +0000296 while (1) {
297 // If this token has a lower precedence than we are allowed to parse (e.g.
298 // because we are called recursively, or because the token is not a binop),
299 // then we are done!
300 if (NextTokPrec < MinPrec)
301 return LHS;
302
303 // Consume the operator, saving the operator token for error reporting.
304 LexerToken OpToken = Tok;
305 ConsumeToken();
306
Chris Lattner96c3deb2006-08-12 17:13:08 +0000307 // Special case handling for the ternary operator.
308 ExprResult TernaryMiddle;
309 if (NextTokPrec == prec::Conditional) {
310 if (Tok.getKind() != tok::colon) {
311 // Handle this production specially:
312 // logical-OR-expression '?' expression ':' conditional-expression
313 // In particular, the RHS of the '?' is 'expression', not
314 // 'logical-OR-expression' as we might expect.
315 TernaryMiddle = ParseExpression();
316 if (TernaryMiddle.isInvalid) return TernaryMiddle;
317 } else {
318 // Special case handling of "X ? Y : Z" where Y is empty:
319 // logical-OR-expression '?' ':' conditional-expression [GNU]
320 TernaryMiddle = ExprResult(false);
321 Diag(Tok, diag::ext_gnu_conditional_expr);
322 }
323
324 if (Tok.getKind() != tok::colon) {
325 Diag(Tok, diag::err_expected_colon);
326 Diag(OpToken, diag::err_matching, "?");
327 return ExprResult(true);
328 }
329
330 // Eat the colon.
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000331 ColonLoc = Tok.getLocation();
Chris Lattner96c3deb2006-08-12 17:13:08 +0000332 ConsumeToken();
Chris Lattnercde626a2006-08-12 08:13:25 +0000333 }
Chris Lattner96c3deb2006-08-12 17:13:08 +0000334
335 // Parse another leaf here for the RHS of the operator.
336 ExprResult RHS = ParseCastExpression(false);
337 if (RHS.isInvalid) return RHS;
Chris Lattnercde626a2006-08-12 08:13:25 +0000338
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 Lattner89d53752006-08-12 17:18:19 +0000343
344 // Assignment and conditional expressions are right-associative.
345 bool isRightAssoc = NextTokPrec == prec::Conditional ||
346 NextTokPrec == prec::Assignment;
Chris Lattnercde626a2006-08-12 08:13:25 +0000347
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 Lattnercde626a2006-08-12 08:13:25 +0000350 if (ThisPrec < NextTokPrec ||
351 (ThisPrec == NextTokPrec && isRightAssoc)) {
Chris Lattner89d53752006-08-12 17:18:19 +0000352 // 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 Lattnercde626a2006-08-12 08:13:25 +0000357 if (RHS.isInvalid) return RHS;
358
359 NextTokPrec = getBinOpPrecedence(Tok.getKind());
360 }
361 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
362
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000363 // Combine the LHS and RHS into the LHS (e.g. build AST).
364 if (NextTokPrec != prec::Conditional)
365 LHS = Actions.ParseBinOp(OpToken, LHS.Val, RHS.Val);
366 else
367 LHS = Actions.ParseConditionalOp(OpToken.getLocation(), ColonLoc,
368 LHS.Val, TernaryMiddle.Val, RHS.Val);
Chris Lattnercde626a2006-08-12 08:13:25 +0000369 }
370}
371
Chris Lattnereaf06592006-08-11 02:02:23 +0000372/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
373/// true, parse a unary-expression.
374///
Chris Lattner4564bc12006-08-10 23:14:52 +0000375/// cast-expression: [C99 6.5.4]
376/// unary-expression
377/// '(' type-name ')' cast-expression
Chris Lattner81b576e2006-08-11 02:13:20 +0000378///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000379/// unary-expression: [C99 6.5.3]
380/// postfix-expression
381/// '++' unary-expression
382/// '--' unary-expression
383/// unary-operator cast-expression
384/// 'sizeof' unary-expression
385/// 'sizeof' '(' type-name ')'
386/// [GNU] '__alignof' unary-expression
387/// [GNU] '__alignof' '(' type-name ')'
388/// [GNU] '&&' identifier
Chris Lattner81b576e2006-08-11 02:13:20 +0000389///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000390/// unary-operator: one of
391/// '&' '*' '+' '-' '~' '!'
392/// [GNU] '__extension__' '__real' '__imag'
393///
Chris Lattner52a99e52006-08-10 20:56:00 +0000394/// primary-expression: [C99 6.5.1]
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000395/// identifier
396/// constant
397/// string-literal
398/// '(' expression ')'
Chris Lattner52a99e52006-08-10 20:56:00 +0000399/// '__func__' [C99 6.4.2.2]
400/// [GNU] '__FUNCTION__'
401/// [GNU] '__PRETTY_FUNCTION__'
402/// [GNU] '(' compound-statement ')'
403/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
404/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
405/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
406/// assign-expr ')'
407/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
408/// [OBC] '[' objc-receiver objc-message-args ']' [TODO]
409/// [OBC] '@selector' '(' objc-selector-arg ')' [TODO]
410/// [OBC] '@protocol' '(' identifier ')' [TODO]
411/// [OBC] '@encode' '(' type-name ')' [TODO]
412/// [OBC] objc-string-literal [TODO]
413///
414/// constant: [C99 6.4.4]
415/// integer-constant
416/// floating-constant
417/// enumeration-constant -> identifier
418/// character-constant
Chris Lattner52a99e52006-08-10 20:56:00 +0000419///
Chris Lattner89c50c62006-08-11 06:41:18 +0000420Parser::ExprResult Parser::ParseCastExpression(bool isUnaryExpression) {
421 ExprResult Res;
Chris Lattner1b926492006-08-23 06:42:10 +0000422 LexerToken SavedTok;
Chris Lattner89c50c62006-08-11 06:41:18 +0000423
Chris Lattner81b576e2006-08-11 02:13:20 +0000424 // This handles all of cast-expression, unary-expression, postfix-expression,
425 // and primary-expression. We handle them together like this for efficiency
426 // and to simplify handling of an expression starting with a '(' token: which
427 // may be one of a parenthesized expression, cast-expression, compound literal
428 // expression, or statement expression.
429 //
430 // If the parsed tokens consist of a primary-expression, the cases below
Chris Lattner20c6a452006-08-12 17:40:43 +0000431 // call ParsePostfixExpressionSuffix to handle the postfix expression
432 // suffixes. Cases that cannot be followed by postfix exprs should
433 // return without invoking ParsePostfixExpressionSuffix.
Chris Lattner52a99e52006-08-10 20:56:00 +0000434 switch (Tok.getKind()) {
Chris Lattnere550a4e2006-08-24 06:37:51 +0000435 case tok::l_paren: {
Chris Lattner81b576e2006-08-11 02:13:20 +0000436 // If this expression is limited to being a unary-expression, the parent can
437 // not start a cast expression.
438 ParenParseOption ParenExprType =
439 isUnaryExpression ? CompoundLiteral : CastExpr;
Chris Lattnere550a4e2006-08-24 06:37:51 +0000440 TypeTy *CastTy;
441 SourceLocation LParenLoc = Tok.getLocation();
442 SourceLocation RParenLoc;
443 Res = ParseParenExpression(ParenExprType, CastTy, RParenLoc);
Chris Lattner89c50c62006-08-11 06:41:18 +0000444 if (Res.isInvalid) return Res;
445
Chris Lattner81b576e2006-08-11 02:13:20 +0000446 switch (ParenExprType) {
447 case SimpleExpr: break; // Nothing else to do.
448 case CompoundStmt: break; // Nothing else to do.
449 case CompoundLiteral:
450 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
451 // postfix-expression exist, parse them now.
452 break;
453 case CastExpr:
454 // We parsed '(' type-name ')' and the thing after it wasn't a '{'. Parse
455 // the cast-expression that follows it next.
Chris Lattnere550a4e2006-08-24 06:37:51 +0000456 // TODO: For cast expression with CastTy.
457 Res = ParseCastExpression(false);
458 if (!Res.isInvalid)
459 Res = Actions.ParseCastExpr(LParenLoc, CastTy, RParenLoc, Res.Val);
460 return Res;
Chris Lattner81b576e2006-08-11 02:13:20 +0000461 }
Chris Lattner20c6a452006-08-12 17:40:43 +0000462
463 // These can be followed by postfix-expr pieces.
464 return ParsePostfixExpressionSuffix(Res);
Chris Lattnere550a4e2006-08-24 06:37:51 +0000465 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000466
Chris Lattner52a99e52006-08-10 20:56:00 +0000467 // primary-expression
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000468 case tok::numeric_constant:
469 // constant: integer-constant
470 // constant: floating-constant
471
472 // TODO: Validate whether this is an integer or floating-constant or
473 // neither.
474 if (1) {
475 Res = Actions.ParseIntegerConstant(Tok);
476 } else {
477 Res = Actions.ParseFloatingConstant(Tok);
478 }
479 ConsumeToken();
480
481 // These can be followed by postfix-expr pieces.
482 return ParsePostfixExpressionSuffix(Res);
483
Chris Lattner52a99e52006-08-10 20:56:00 +0000484 case tok::identifier: // primary-expression: identifier
485 // constant: enumeration-constant
Chris Lattner52a99e52006-08-10 20:56:00 +0000486 case tok::char_constant: // constant: character-constant
487 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
488 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
489 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattner879b9ad2006-08-24 04:53:44 +0000490 Res = Actions.ParseSimplePrimaryExpr(Tok);
Chris Lattner52a99e52006-08-10 20:56:00 +0000491 ConsumeToken();
Chris Lattner20c6a452006-08-12 17:40:43 +0000492 // These can be followed by postfix-expr pieces.
493 return ParsePostfixExpressionSuffix(Res);
Chris Lattner52a99e52006-08-10 20:56:00 +0000494 case tok::string_literal: // primary-expression: string-literal
Chris Lattnerd3e98952006-10-06 05:22:26 +0000495 case tok::wide_string_literal:
Chris Lattner89c50c62006-08-11 06:41:18 +0000496 Res = ParseStringLiteralExpression();
497 if (Res.isInvalid) return Res;
Chris Lattner20c6a452006-08-12 17:40:43 +0000498 // This can be followed by postfix-expr pieces (e.g. "foo"[1]).
499 return ParsePostfixExpressionSuffix(Res);
Chris Lattnerf8339772006-08-10 22:01:51 +0000500 case tok::kw___builtin_va_arg:
501 case tok::kw___builtin_offsetof:
502 case tok::kw___builtin_choose_expr:
503 case tok::kw___builtin_types_compatible_p:
Chris Lattner11124352006-08-12 19:16:08 +0000504 return ParseBuiltinPrimaryExpression();
Chris Lattner81b576e2006-08-11 02:13:20 +0000505 case tok::plusplus: // unary-expression: '++' unary-expression
506 case tok::minusminus: // unary-expression: '--' unary-expression
Chris Lattner1b926492006-08-23 06:42:10 +0000507 SavedTok = Tok;
Chris Lattner81b576e2006-08-11 02:13:20 +0000508 ConsumeToken();
Chris Lattner1b926492006-08-23 06:42:10 +0000509 Res = ParseCastExpression(true);
510 if (!Res.isInvalid)
511 Res = Actions.ParseUnaryOp(SavedTok, Res.Val);
512 return Res;
Chris Lattner81b576e2006-08-11 02:13:20 +0000513 case tok::amp: // unary-expression: '&' cast-expression
514 case tok::star: // unary-expression: '*' cast-expression
515 case tok::plus: // unary-expression: '+' cast-expression
516 case tok::minus: // unary-expression: '-' cast-expression
517 case tok::tilde: // unary-expression: '~' cast-expression
518 case tok::exclaim: // unary-expression: '!' cast-expression
519 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattner1b926492006-08-23 06:42:10 +0000520 case tok::kw___imag: // unary-expression: '__imag' cast-expression [GNU]
Chris Lattner81b576e2006-08-11 02:13:20 +0000521 //case tok::kw__extension__: [TODO]
Chris Lattner1b926492006-08-23 06:42:10 +0000522 SavedTok = Tok;
Chris Lattner81b576e2006-08-11 02:13:20 +0000523 ConsumeToken();
Chris Lattner1b926492006-08-23 06:42:10 +0000524 Res = ParseCastExpression(false);
525 if (!Res.isInvalid)
526 Res = Actions.ParseUnaryOp(SavedTok, Res.Val);
527 return Res;
Chris Lattner81b576e2006-08-11 02:13:20 +0000528
529 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
530 // unary-expression: 'sizeof' '(' type-name ')'
531 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
532 // unary-expression: '__alignof' '(' type-name ')'
Chris Lattner89c50c62006-08-11 06:41:18 +0000533 return ParseSizeofAlignofExpression();
Chris Lattner81b576e2006-08-11 02:13:20 +0000534 case tok::ampamp: // unary-expression: '&&' identifier
535 Diag(Tok, diag::ext_gnu_address_of_label);
536 ConsumeToken();
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000537 // TODO: Build AST.
Chris Lattner81b576e2006-08-11 02:13:20 +0000538 if (Tok.getKind() == tok::identifier) {
539 ConsumeToken();
540 } else {
541 Diag(Tok, diag::err_expected_ident);
Chris Lattner89c50c62006-08-11 06:41:18 +0000542 return ExprResult(true);
Chris Lattner81b576e2006-08-11 02:13:20 +0000543 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000544 return ExprResult(false);
Chris Lattner52a99e52006-08-10 20:56:00 +0000545 default:
546 Diag(Tok, diag::err_expected_expression);
Chris Lattner89c50c62006-08-11 06:41:18 +0000547 return ExprResult(true);
Chris Lattnerf8339772006-08-10 22:01:51 +0000548 }
549
Chris Lattner20c6a452006-08-12 17:40:43 +0000550 // unreachable.
551 abort();
552}
553
554/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
555/// is parsed, this method parses any suffixes that apply.
556///
557/// postfix-expression: [C99 6.5.2]
558/// primary-expression
559/// postfix-expression '[' expression ']'
560/// postfix-expression '(' argument-expression-list[opt] ')'
561/// postfix-expression '.' identifier
562/// postfix-expression '->' identifier
563/// postfix-expression '++'
564/// postfix-expression '--'
565/// '(' type-name ')' '{' initializer-list '}'
566/// '(' type-name ')' '{' initializer-list ',' '}'
567///
568/// argument-expression-list: [C99 6.5.2]
569/// argument-expression
570/// argument-expression-list ',' assignment-expression
571///
572Parser::ExprResult Parser::ParsePostfixExpressionSuffix(ExprResult LHS) {
Chris Lattner20c6a452006-08-12 17:40:43 +0000573
Chris Lattnerf8339772006-08-10 22:01:51 +0000574 // Now that the primary-expression piece of the postfix-expression has been
575 // parsed, see if there are any postfix-expression pieces here.
576 SourceLocation Loc;
577 while (1) {
578 switch (Tok.getKind()) {
Chris Lattner20c6a452006-08-12 17:40:43 +0000579 default: // Not a postfix-expression suffix.
580 return LHS;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000581 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
Chris Lattner89c50c62006-08-11 06:41:18 +0000582 Loc = Tok.getLocation();
583 ConsumeBracket();
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000584 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 Lattner89c50c62006-08-11 06:41:18 +0000593 // Match the ']'.
Chris Lattner04f80192006-08-15 04:55:54 +0000594 MatchRHSPunctuation(tok::r_square, Loc);
Chris Lattner89c50c62006-08-11 06:41:18 +0000595 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000596 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000597
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000598 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 Lattner89c50c62006-08-11 06:41:18 +0000603 Loc = Tok.getLocation();
604 ConsumeParen();
605
Chris Lattner0c6c0342006-08-12 18:12:45 +0000606 if (Tok.getKind() != tok::r_paren) {
607 while (1) {
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000608 ExprResult ArgExpr = ParseAssignmentExpression();
609 if (ArgExpr.isInvalid)
610 ArgExprsOk = false;
611 else
612 ArgExprs.push_back(ArgExpr.Val);
613
Chris Lattner0c6c0342006-08-12 18:12:45 +0000614 if (Tok.getKind() != tok::comma)
615 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000616 CommaLocs.push_back(Tok.getLocation());
Chris Lattner0c6c0342006-08-12 18:12:45 +0000617 ConsumeToken(); // Next argument.
618 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000619 }
Chris Lattner81b576e2006-08-11 02:13:20 +0000620
Chris Lattner89c50c62006-08-11 06:41:18 +0000621 // Match the ')'.
Chris Lattnere165d942006-08-24 04:40:38 +0000622 if (!LHS.isInvalid && ArgExprsOk && Tok.getKind() == tok::r_paren) {
623 assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&&
624 "Unexpected number of commas!");
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000625 LHS = Actions.ParseCallExpr(LHS.Val, Loc, &ArgExprs[0], ArgExprs.size(),
Chris Lattnere165d942006-08-24 04:40:38 +0000626 &CommaLocs[0], Tok.getLocation());
627 }
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000628
Chris Lattner04f80192006-08-15 04:55:54 +0000629 MatchRHSPunctuation(tok::r_paren, Loc);
Chris Lattner89c50c62006-08-11 06:41:18 +0000630 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000631 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000632 case tok::arrow: // postfix-expression: p-e '->' identifier
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000633 case tok::period: { // postfix-expression: p-e '.' identifier
634 SourceLocation OpLoc = Tok.getLocation();
635 tok::TokenKind OpKind = Tok.getKind();
636 ConsumeToken(); // Eat the "." or "->" token.
637
Chris Lattner89c50c62006-08-11 06:41:18 +0000638 if (Tok.getKind() != tok::identifier) {
639 Diag(Tok, diag::err_expected_ident);
640 return ExprResult(true);
641 }
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000642
643 if (!LHS.isInvalid)
644 LHS = Actions.ParseMemberReferenceExpr(LHS.Val, OpLoc, OpKind,
645 Tok.getLocation(),
646 *Tok.getIdentifierInfo());
Chris Lattner89c50c62006-08-11 06:41:18 +0000647 ConsumeToken();
648 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000649 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000650 case tok::plusplus: // postfix-expression: postfix-expression '++'
651 case tok::minusminus: // postfix-expression: postfix-expression '--'
Chris Lattner1b926492006-08-23 06:42:10 +0000652 if (!LHS.isInvalid)
653 LHS = Actions.ParsePostfixUnaryOp(Tok, LHS.Val);
Chris Lattner89c50c62006-08-11 06:41:18 +0000654 ConsumeToken();
655 break;
Chris Lattnerf8339772006-08-10 22:01:51 +0000656 }
657 }
Chris Lattner52a99e52006-08-10 20:56:00 +0000658}
659
Chris Lattner20c6a452006-08-12 17:40:43 +0000660
Chris Lattner81b576e2006-08-11 02:13:20 +0000661/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
662/// unary-expression: [C99 6.5.3]
663/// 'sizeof' unary-expression
664/// 'sizeof' '(' type-name ')'
665/// [GNU] '__alignof' unary-expression
666/// [GNU] '__alignof' '(' type-name ')'
Chris Lattner89c50c62006-08-11 06:41:18 +0000667Parser::ExprResult Parser::ParseSizeofAlignofExpression() {
Chris Lattner81b576e2006-08-11 02:13:20 +0000668 assert((Tok.getKind() == tok::kw_sizeof ||
669 Tok.getKind() == tok::kw___alignof) &&
670 "Not a sizeof/alignof expression!");
Chris Lattner26115ac2006-08-24 06:10:04 +0000671 LexerToken OpTok = Tok;
Chris Lattner81b576e2006-08-11 02:13:20 +0000672 ConsumeToken();
673
674 // If the operand doesn't start with an '(', it must be an expression.
Chris Lattner26115ac2006-08-24 06:10:04 +0000675 ExprResult Operand;
676 if (Tok.getKind() != tok::l_paren) {
677 Operand = ParseCastExpression(true);
678 } else {
679 // If it starts with a '(', we know that it is either a parenthesized
680 // type-name, or it is a unary-expression that starts with a compound
681 // literal, or starts with a primary-expression that is a parenthesized
682 // expression.
683 ParenParseOption ExprType = CastExpr;
Chris Lattnere550a4e2006-08-24 06:37:51 +0000684 TypeTy *CastTy;
Chris Lattner26da7302006-08-24 06:49:19 +0000685 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
Chris Lattnere550a4e2006-08-24 06:37:51 +0000686 Operand = ParseParenExpression(ExprType, CastTy, RParenLoc);
Chris Lattner26115ac2006-08-24 06:10:04 +0000687
688 // If ParseParenExpression parsed a '(typename)' sequence only, the this is
689 // sizeof/alignof a type. Otherwise, it is sizeof/alignof an expression.
690 if (ExprType == CastExpr) {
Chris Lattner26da7302006-08-24 06:49:19 +0000691 return Actions.ParseSizeOfAlignOfTypeExpr(OpTok.getLocation(),
692 OpTok.getKind() == tok::kw_sizeof,
693 LParenLoc, CastTy, RParenLoc);
Chris Lattner26115ac2006-08-24 06:10:04 +0000694 }
695 }
Chris Lattner81b576e2006-08-11 02:13:20 +0000696
Chris Lattner26115ac2006-08-24 06:10:04 +0000697 // If we get here, the operand to the sizeof/alignof was an expresion.
698 if (!Operand.isInvalid)
699 Operand = Actions.ParseUnaryOp(OpTok, Operand.Val);
700 return Operand;
Chris Lattner81b576e2006-08-11 02:13:20 +0000701}
702
Chris Lattner11124352006-08-12 19:16:08 +0000703/// 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///
717Parser::ExprResult Parser::ParseBuiltinPrimaryExpression() {
718 ExprResult Res(false);
719 SourceLocation StartLoc = Tok.getLocation();
720 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
721
722 tok::TokenKind T = Tok.getKind();
723 ConsumeToken(); // Eat the builtin identifier.
724
725 // All of these start with an open paren.
726 if (Tok.getKind() != tok::l_paren) {
727 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
728 return ExprResult(true);
729 }
730
731 SourceLocation LParenLoc = Tok.getLocation();
732 ConsumeParen();
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000733 // TODO: Build AST.
734
Chris Lattner11124352006-08-12 19:16:08 +0000735 switch (T) {
736 default: assert(0 && "Not a builtin primary expression!");
737 case tok::kw___builtin_va_arg:
738 Res = ParseAssignmentExpression();
739 if (Res.isInvalid) {
740 SkipUntil(tok::r_paren);
741 return Res;
742 }
Chris Lattner0be454e2006-08-12 19:30:51 +0000743
Chris Lattner6d7e6342006-08-15 03:41:14 +0000744 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000745 return ExprResult(true);
Chris Lattner0be454e2006-08-12 19:30:51 +0000746
Chris Lattner11124352006-08-12 19:16:08 +0000747 ParseTypeName();
748 break;
749
750 case tok::kw___builtin_offsetof:
751 ParseTypeName();
752
Chris Lattner6d7e6342006-08-15 03:41:14 +0000753 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000754 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000755
756 // We must have at least one identifier here.
Chris Lattner6d7e6342006-08-15 03:41:14 +0000757 if (ExpectAndConsume(tok::identifier, diag::err_expected_ident, "",
Chris Lattnerdbb2a462006-08-12 19:26:13 +0000758 tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000759 return ExprResult(true);
Chris Lattnerdbb2a462006-08-12 19:26:13 +0000760
Chris Lattner11124352006-08-12 19:16:08 +0000761 while (1) {
762 if (Tok.getKind() == tok::period) {
763 // offsetof-member-designator: offsetof-member-designator '.' identifier
764 ConsumeToken();
765
Chris Lattner6d7e6342006-08-15 03:41:14 +0000766 if (ExpectAndConsume(tok::identifier, diag::err_expected_ident, "",
Chris Lattnerdbb2a462006-08-12 19:26:13 +0000767 tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000768 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000769 } else if (Tok.getKind() == tok::l_square) {
770 // offsetof-member-designator: offsetof-member-design '[' expression ']'
771 SourceLocation LSquareLoc = Tok.getLocation();
772 ConsumeBracket();
773 Res = ParseExpression();
774 if (Res.isInvalid) {
775 SkipUntil(tok::r_paren);
776 return Res;
777 }
778
Chris Lattner04f80192006-08-15 04:55:54 +0000779 MatchRHSPunctuation(tok::r_square, LSquareLoc);
Chris Lattner11124352006-08-12 19:16:08 +0000780 } else {
781 break;
782 }
783 }
784 break;
785 case tok::kw___builtin_choose_expr:
786 Res = ParseAssignmentExpression();
787
Chris Lattner6d7e6342006-08-15 03:41:14 +0000788 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000789 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000790
791 Res = ParseAssignmentExpression();
792
Chris Lattner6d7e6342006-08-15 03:41:14 +0000793 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000794 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000795
796 Res = ParseAssignmentExpression();
797 break;
798 case tok::kw___builtin_types_compatible_p:
799 ParseTypeName();
800
Chris Lattner6d7e6342006-08-15 03:41:14 +0000801 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000802 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000803
804 ParseTypeName();
805 break;
806 }
807
Chris Lattner04f80192006-08-15 04:55:54 +0000808 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner11124352006-08-12 19:16:08 +0000809
810 // These can be followed by postfix-expr pieces because they are
811 // primary-expressions.
812 return ParsePostfixExpressionSuffix(Res);
813}
814
Chris Lattnerc951dae2006-08-10 04:23:57 +0000815
Chris Lattner4add4e62006-08-11 01:33:00 +0000816/// 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 Lattnerc951dae2006-08-10 04:23:57 +0000821/// '(' expression ')'
Chris Lattnerf8339772006-08-10 22:01:51 +0000822/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
823/// postfix-expression: [C99 6.5.2]
824/// '(' type-name ')' '{' initializer-list '}'
825/// '(' type-name ')' '{' initializer-list ',' '}'
Chris Lattner4add4e62006-08-11 01:33:00 +0000826/// cast-expression: [C99 6.5.4]
827/// '(' type-name ')' cast-expression
Chris Lattnerf8339772006-08-10 22:01:51 +0000828///
Chris Lattnere550a4e2006-08-24 06:37:51 +0000829Parser::ExprResult Parser::ParseParenExpression(ParenParseOption &ExprType,
830 TypeTy *&CastTy,
831 SourceLocation &RParenLoc) {
Chris Lattnerc951dae2006-08-10 04:23:57 +0000832 assert(Tok.getKind() == tok::l_paren && "Not a paren expr!");
833 SourceLocation OpenLoc = Tok.getLocation();
834 ConsumeParen();
Chris Lattner89c50c62006-08-11 06:41:18 +0000835 ExprResult Result(false);
Chris Lattnere550a4e2006-08-24 06:37:51 +0000836 CastTy = 0;
Chris Lattnerc951dae2006-08-10 04:23:57 +0000837
Chris Lattner4add4e62006-08-11 01:33:00 +0000838 if (ExprType >= CompoundStmt && Tok.getKind() == tok::l_brace &&
Chris Lattnerf8339772006-08-10 22:01:51 +0000839 !getLang().NoExtensions) {
840 Diag(Tok, diag::ext_gnu_statement_expr);
841 ParseCompoundStatement();
Chris Lattner4add4e62006-08-11 01:33:00 +0000842 ExprType = CompoundStmt;
Chris Lattner1b926492006-08-23 06:42:10 +0000843 // TODO: Build AST for GNU compound stmt.
Chris Lattner4add4e62006-08-11 01:33:00 +0000844 } else if (ExprType >= CompoundLiteral && isTypeSpecifierQualifier()) {
Chris Lattner6c3f05d2006-08-12 16:54:25 +0000845 // Otherwise, this is a compound literal expression or cast expression.
Chris Lattnere550a4e2006-08-24 06:37:51 +0000846 TypeTy *Ty = ParseTypeName();
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000847
848 // Match the ')'.
Chris Lattnere550a4e2006-08-24 06:37:51 +0000849 if (Tok.getKind() == tok::r_paren) {
850 RParenLoc = Tok.getLocation();
851 ConsumeParen();
852 } else {
853 MatchRHSPunctuation(tok::r_paren, OpenLoc);
854 }
855
Chris Lattner4add4e62006-08-11 01:33:00 +0000856 if (Tok.getKind() == tok::l_brace) {
Chris Lattner6c3f05d2006-08-12 16:54:25 +0000857 if (!getLang().C99) // Compound literals don't exist in C90.
858 Diag(OpenLoc, diag::ext_c99_compound_literal);
Chris Lattner89c50c62006-08-11 06:41:18 +0000859 Result = ParseInitializer();
Chris Lattner4add4e62006-08-11 01:33:00 +0000860 ExprType = CompoundLiteral;
Chris Lattner1b926492006-08-23 06:42:10 +0000861 // TODO: Build AST for compound literal.
Chris Lattner4add4e62006-08-11 01:33:00 +0000862 } else if (ExprType == CastExpr) {
Chris Lattnere550a4e2006-08-24 06:37:51 +0000863 // Note that this doesn't parse the subsequence cast-expression, it just
864 // returns the parsed type to the callee.
Chris Lattner4add4e62006-08-11 01:33:00 +0000865 ExprType = CastExpr;
Chris Lattnere550a4e2006-08-24 06:37:51 +0000866 CastTy = Ty;
867 return ExprResult(false);
Chris Lattner4add4e62006-08-11 01:33:00 +0000868 } else {
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000869 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
Chris Lattner89c50c62006-08-11 06:41:18 +0000870 return ExprResult(true);
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000871 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000872 return Result;
Chris Lattner4add4e62006-08-11 01:33:00 +0000873 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000874 Result = ParseExpression();
Chris Lattner4add4e62006-08-11 01:33:00 +0000875 ExprType = SimpleExpr;
Chris Lattner1b926492006-08-23 06:42:10 +0000876 if (!Result.isInvalid && Tok.getKind() == tok::r_paren)
877 Result = Actions.ParseParenExpr(OpenLoc, Tok.getLocation(), Result.Val);
Chris Lattnerf8339772006-08-10 22:01:51 +0000878 }
Chris Lattnerc951dae2006-08-10 04:23:57 +0000879
Chris Lattner4564bc12006-08-10 23:14:52 +0000880 // Match the ')'.
Chris Lattner89c50c62006-08-11 06:41:18 +0000881 if (Result.isInvalid)
882 SkipUntil(tok::r_paren);
Chris Lattnere550a4e2006-08-24 06:37:51 +0000883 else {
884 if (Tok.getKind() == tok::r_paren) {
885 RParenLoc = Tok.getLocation();
886 ConsumeParen();
887 } else {
888 MatchRHSPunctuation(tok::r_paren, OpenLoc);
889 }
890 }
Chris Lattner1b926492006-08-23 06:42:10 +0000891
Chris Lattner89c50c62006-08-11 06:41:18 +0000892 return Result;
Chris Lattnerc951dae2006-08-10 04:23:57 +0000893}
Chris Lattnerd3e98952006-10-06 05:22:26 +0000894
895/// HexDigitValue - Return the value of the specified hex digit, or -1 if it's
896/// not valid.
897static int HexDigitValue(char C) {
898 if (C >= '0' && C <= '9') return C-'0';
899 if (C >= 'a' && C <= 'f') return C-'a'+10;
900 if (C >= 'A' && C <= 'F') return C-'A'+10;
901 return -1;
902}
903
904/// ParseStringLiteralExpression - This handles the various token types that
905/// form string literals, and also handles string concatenation [C99 5.1.1.2,
906/// translation phase #6].
907///
908/// primary-expression: [C99 6.5.1]
909/// string-literal
910Parser::ExprResult Parser::ParseStringLiteralExpression() {
911 assert(isTokenStringLiteral() && "Not a string literal!");
912
913 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
914 // considered to be strings for concatenation purposes.
915 SmallVector<LexerToken, 4> StringToks;
916
917 // While we're looking at all of the string portions, remember the max
918 // individual token length, computing a bound on the concatenated string
919 // length, and see whether any piece is a wide-string. If any of the string
920 // portions is a wide-string literal, the result is also a wide-string literal
921 // [C99 6.4.5p4].
922 unsigned SizeBound = 0, MaxTokenLength = 0;
923 bool AnyWide = false;
924 do {
925 // The string could be shorter than this if it needs cleaning, but this is a
926 // reasonable bound, which is all we need.
927 SizeBound += Tok.getLength()-2; // -2 for "".
928
929 // Find maximum string piece length.
930 if (Tok.getLength() > MaxTokenLength)
931 MaxTokenLength = Tok.getLength();
932
933 // Remember if we see any wide strings.
934 AnyWide |= Tok.getKind() == tok::wide_string_literal;
935
936 // Remember the string token.
937 StringToks.push_back(Tok);
938 ConsumeStringToken();
939 } while (isTokenStringLiteral());
940
941 // Include space for the null terminator.
942 ++SizeBound;
943
944 // TODO: K&R warning: "traditional C rejects string constant concatenation"
945
946 // FIXME: Size of wchar_t should not be hardcoded!
947 unsigned wchar_tByteWidth = 4;
948
949 // The output buffer size needs to be large enough to hold wide characters.
950 // This is a worst-case assumption which basically corresponds to L"" "long".
951 if (AnyWide)
952 SizeBound *= wchar_tByteWidth;
953
954 // Create a temporary buffer to hold the result string data. If it is "big",
955 // use malloc, otherwise use alloca.
956 char *ResultBuf;
957 if (SizeBound > 512)
958 ResultBuf = (char*)malloc(SizeBound);
959 else
960 ResultBuf = (char*)alloca(SizeBound);
961
962 // Likewise, but for each string piece.
963 char *TokenBuf;
964 if (MaxTokenLength > 512)
965 TokenBuf = (char*)malloc(MaxTokenLength);
966 else
967 TokenBuf = (char*)alloca(MaxTokenLength);
968
969 // Loop over all the strings, getting their spelling, and expanding them to
970 // wide strings as appropriate.
971 char *ResultPtr = ResultBuf; // Next byte to fill in.
972
973 for (unsigned i = 0, e = StringToks.size(); i != e; ++i) {
974 const char *ThisTokBuf = TokenBuf;
975 // Get the spelling of the token, which eliminates trigraphs, etc. We know
976 // that ThisTokBuf points to a buffer that is big enough for the whole token
977 // and 'spelled' tokens can only shrink.
978 unsigned ThisTokLen = PP.getSpelling(StringToks[i], ThisTokBuf);
979 const char *ThisTokEnd = ThisTokBuf+ThisTokLen-1; // Skip end quote.
980
981 // TODO: Input character set mapping support.
982
983 // Skip L marker for wide strings.
984 if (ThisTokBuf[0] == 'L') ++ThisTokBuf;
985
986 assert(ThisTokBuf[0] == '"' && "Expected quote, lexer broken?");
987 ++ThisTokBuf;
988
989 while (ThisTokBuf != ThisTokEnd) {
990 // Is this a span of non-escape characters?
991 if (ThisTokBuf[0] != '\\') {
992 const char *InStart = ThisTokBuf;
993 do {
994 ++ThisTokBuf;
995 } while (ThisTokBuf != ThisTokEnd && ThisTokBuf[0] != '\\');
996
997 // Copy the character span over.
998 unsigned Len = ThisTokBuf-InStart;
999 if (!AnyWide) {
1000 memcpy(ResultPtr, InStart, Len);
1001 ResultPtr += Len;
1002 } else {
1003 // Note: our internal rep of wide char tokens is always little-endian.
1004 for (; Len; --Len, ++InStart) {
1005 *ResultPtr++ = InStart[0];
1006 // Add zeros at the end.
1007 for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
1008 *ResultPtr++ = 0;
1009 }
1010 }
1011 continue;
1012 }
1013
1014 // Otherwise, this is an escape character. Skip the '\' char.
1015 ++ThisTokBuf;
1016
1017 // We know that this character can't be off the end of the buffer, because
1018 // that would have been \", which would not have been the end of string.
1019 unsigned ResultChar = *ThisTokBuf++;
1020 switch (ResultChar) {
1021 // These map to themselves.
1022 case '\\': case '\'': case '"': case '?': break;
1023
1024 // These have fixed mappings.
1025 case 'a':
1026 // TODO: K&R: the meaning of '\\a' is different in traditional C
1027 ResultChar = 7;
1028 break;
1029 case 'b':
1030 ResultChar = 8;
1031 break;
1032 case 'e':
1033 PP.Diag(StringToks[i], diag::ext_nonstandard_escape, "e");
1034 ResultChar = 27;
1035 break;
1036 case 'f':
1037 ResultChar = 12;
1038 break;
1039 case 'n':
1040 ResultChar = 10;
1041 break;
1042 case 'r':
1043 ResultChar = 13;
1044 break;
1045 case 't':
1046 ResultChar = 9;
1047 break;
1048 case 'v':
1049 ResultChar = 11;
1050 break;
1051
1052 //case 'u': case 'U': // FIXME: UCNs.
1053 case 'x': // Hex escape.
1054 if (ThisTokBuf == ThisTokEnd ||
1055 (ResultChar = HexDigitValue(*ThisTokBuf)) == ~0U) {
1056 PP.Diag(StringToks[i], diag::err_hex_escape_no_digits);
1057 ResultChar = 0;
1058 break;
1059 }
1060 ++ThisTokBuf; // Consumed one hex digit.
1061
1062 assert(0 && "hex escape: unimp!");
1063 break;
1064 case '0': case '1': case '2': case '3':
1065 case '4': case '5': case '6': case '7':
1066 // Octal escapes.
1067 assert(0 && "octal escape: unimp!");
1068 break;
1069
1070 // Otherwise, these are not valid escapes.
1071 case '(': case '{': case '[': case '%':
1072 // GCC accepts these as extensions. We warn about them as such though.
1073 if (!PP.getLangOptions().NoExtensions) {
1074 PP.Diag(StringToks[i], diag::ext_nonstandard_escape,
1075 std::string()+(char)ResultChar);
1076 break;
1077 }
1078 // FALL THROUGH.
1079 default:
1080 if (isgraph(ThisTokBuf[0])) {
1081 PP.Diag(StringToks[i], diag::ext_unknown_escape,
1082 std::string()+(char)ResultChar);
1083 } else {
1084 PP.Diag(StringToks[i], diag::ext_unknown_escape,
1085 "x"+utohexstr(ResultChar));
1086 }
1087 }
1088
1089 // Note: our internal rep of wide char tokens is always little-endian.
1090 for (unsigned i = 0, e = wchar_tByteWidth; i != e; ++i)
1091 *ResultPtr++ = ResultChar >> i*8;
1092 }
1093 }
1094
1095 // Add zero terminator.
1096 *ResultPtr = 0;
1097 if (AnyWide) {
1098 for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
1099 *ResultPtr++ = 0;
1100 }
1101
1102 // Hand this off to the Actions.
1103 ExprResult Res = Actions.ParseStringExpr(ResultBuf, ResultPtr-ResultBuf,
1104 AnyWide,
1105 &StringToks[0], StringToks.size());
1106
1107 // If either buffer was heap allocated, release it now.
1108 if (MaxTokenLength > 512) free(TokenBuf);
1109 if (SizeBound > 512) free(ResultBuf);
1110
1111 return Res;
1112}
1113