blob: a2b8a7df7ed55b3d8ec40ade67582e671d8f3629 [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 Lattner02dffbd2006-10-14 07:50:21 +000024#include "clang/Basic/TargetInfo.h"
Chris Lattner6d28d9b2006-08-24 03:51:22 +000025#include "llvm/ADT/SmallVector.h"
Chris Lattnerd3e98952006-10-06 05:22:26 +000026#include "llvm/ADT/StringExtras.h"
27#include "llvm/Config/Alloca.h"
Chris Lattnerc951dae2006-08-10 04:23:57 +000028using namespace llvm;
29using namespace clang;
30
Chris Lattnerb7f1fc92006-08-12 16:45:01 +000031/// PrecedenceLevels - These are precedences for the binary/ternary operators in
Chris Lattnercde626a2006-08-12 08:13:25 +000032/// the C99 grammar. These have been named to relate with the C99 grammar
33/// productions. Low precedences numbers bind more weakly than high numbers.
34namespace prec {
35 enum Level {
36 Unknown = 0, // Not binary operator.
37 Comma = 1, // ,
38 Assignment = 2, // =, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |=
39 Conditional = 3, // ?
40 LogicalOr = 4, // ||
41 LogicalAnd = 5, // &&
42 InclusiveOr = 6, // |
43 ExclusiveOr = 7, // ^
44 And = 8, // &
Chris Lattner9916c5c2006-10-27 05:24:37 +000045 Equality = 9, // ==, !=
46 Relational = 10, // >=, <=, >, <
47 Shift = 11, // <<, >>
48 Additive = 12, // -, +
49 Multiplicative = 13 // *, /, %
Chris Lattnercde626a2006-08-12 08:13:25 +000050 };
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;
Chris Lattnercde626a2006-08-12 08:13:25 +000078 case tok::exclaimequal:
79 case tok::equalequal: return prec::Equality;
80 case tok::lessequal:
81 case tok::less:
82 case tok::greaterequal:
83 case tok::greater: return prec::Relational;
84 case tok::lessless:
85 case tok::greatergreater: return prec::Shift;
86 case tok::plus:
87 case tok::minus: return prec::Additive;
88 case tok::percent:
89 case tok::slash:
90 case tok::star: return prec::Multiplicative;
91 }
92}
93
94
Chris Lattnerce7e21d2006-08-12 17:22:40 +000095/// ParseExpression - Simple precedence-based parser for binary/ternary
Chris Lattnercde626a2006-08-12 08:13:25 +000096/// operators.
97///
Chris Lattnerb7f1fc92006-08-12 16:45:01 +000098/// Note: we diverge from the C99 grammar when parsing the assignment-expression
99/// production. C99 specifies that the LHS of an assignment operator should be
100/// parsed as a unary-expression, but consistency dictates that it be a
101/// conditional-expession. In practice, the important thing here is that the
102/// LHS of an assignment has to be an l-value, which productions between
103/// unary-expression and conditional-expression don't produce. Because we want
104/// consistency, we parse the LHS as a conditional-expression, then check for
105/// l-value-ness in semantic analysis stages.
106///
Chris Lattnercde626a2006-08-12 08:13:25 +0000107/// multiplicative-expression: [C99 6.5.5]
108/// cast-expression
109/// multiplicative-expression '*' cast-expression
110/// multiplicative-expression '/' cast-expression
111/// multiplicative-expression '%' cast-expression
112///
113/// additive-expression: [C99 6.5.6]
114/// multiplicative-expression
115/// additive-expression '+' multiplicative-expression
116/// additive-expression '-' multiplicative-expression
117///
118/// shift-expression: [C99 6.5.7]
119/// additive-expression
120/// shift-expression '<<' additive-expression
121/// shift-expression '>>' additive-expression
122///
123/// relational-expression: [C99 6.5.8]
124/// shift-expression
125/// relational-expression '<' shift-expression
126/// relational-expression '>' shift-expression
127/// relational-expression '<=' shift-expression
128/// relational-expression '>=' shift-expression
129///
130/// equality-expression: [C99 6.5.9]
131/// relational-expression
132/// equality-expression '==' relational-expression
133/// equality-expression '!=' relational-expression
134///
135/// AND-expression: [C99 6.5.10]
136/// equality-expression
137/// AND-expression '&' equality-expression
138///
139/// exclusive-OR-expression: [C99 6.5.11]
140/// AND-expression
141/// exclusive-OR-expression '^' AND-expression
142///
143/// inclusive-OR-expression: [C99 6.5.12]
144/// exclusive-OR-expression
145/// inclusive-OR-expression '|' exclusive-OR-expression
146///
147/// logical-AND-expression: [C99 6.5.13]
148/// inclusive-OR-expression
149/// logical-AND-expression '&&' inclusive-OR-expression
150///
151/// logical-OR-expression: [C99 6.5.14]
152/// logical-AND-expression
153/// logical-OR-expression '||' logical-AND-expression
154///
155/// conditional-expression: [C99 6.5.15]
156/// logical-OR-expression
157/// logical-OR-expression '?' expression ':' conditional-expression
158/// [GNU] logical-OR-expression '?' ':' conditional-expression
159///
160/// assignment-expression: [C99 6.5.16]
161/// conditional-expression
162/// unary-expression assignment-operator assignment-expression
163///
164/// assignment-operator: one of
165/// = *= /= %= += -= <<= >>= &= ^= |=
166///
167/// expression: [C99 6.5.17]
168/// assignment-expression
169/// expression ',' assignment-expression
170///
Chris Lattnerd35c34f2006-08-12 17:04:50 +0000171Parser::ExprResult Parser::ParseExpression() {
Chris Lattnercde626a2006-08-12 08:13:25 +0000172 ExprResult LHS = ParseCastExpression(false);
173 if (LHS.isInvalid) return LHS;
174
175 return ParseRHSOfBinaryExpression(LHS, prec::Comma);
176}
177
Chris Lattner0c6c0342006-08-12 18:12:45 +0000178/// ParseAssignmentExpression - Parse an expr that doesn't include commas.
179///
Chris Lattnerce7e21d2006-08-12 17:22:40 +0000180Parser::ExprResult Parser::ParseAssignmentExpression() {
181 ExprResult LHS = ParseCastExpression(false);
182 if (LHS.isInvalid) return LHS;
183
184 return ParseRHSOfBinaryExpression(LHS, prec::Assignment);
185}
186
Chris Lattner3b561a32006-08-13 00:12:11 +0000187Parser::ExprResult Parser::ParseConstantExpression() {
188 ExprResult LHS = ParseCastExpression(false);
189 if (LHS.isInvalid) return LHS;
190
191 // TODO: Validate that this is a constant expr!
192 return ParseRHSOfBinaryExpression(LHS, prec::Conditional);
193}
194
Chris Lattner0c6c0342006-08-12 18:12:45 +0000195/// ParseExpressionWithLeadingIdentifier - This special purpose method is used
196/// in contexts where we have already consumed an identifier (which we saved in
197/// 'Tok'), then discovered that the identifier was really the leading token of
198/// part of an expression. For example, in "A[1]+B", we consumed "A" (which is
199/// now in 'Tok') and the current token is "[".
200Parser::ExprResult Parser::
201ParseExpressionWithLeadingIdentifier(const LexerToken &Tok) {
202 // We know that 'Tok' must correspond to this production:
203 // primary-expression: identifier
204
205 // TODO: Pass 'Tok' to the action.
206 ExprResult Res = ExprResult(false);
207
208 // Because we have to parse an entire cast-expression before starting the
209 // ParseRHSOfBinaryExpression method (which parses any trailing binops), we
210 // need to handle the 'postfix-expression' rules. We do this by invoking
211 // ParsePostfixExpressionSuffix to consume any postfix-expression suffixes:
212 Res = ParsePostfixExpressionSuffix(Res);
213 if (Res.isInvalid) return Res;
214
215 // At this point, the "A[1]" part of "A[1]+B" has been consumed. Once this is
216 // done, we know we don't have to do anything for cast-expression, because the
217 // only non-postfix-expression production starts with a '(' token, and we know
218 // we have an identifier. As such, we can invoke ParseRHSOfBinaryExpression
219 // to consume any trailing operators (e.g. "+" in this example) and connected
220 // chunks of the expression.
221 return ParseRHSOfBinaryExpression(Res, prec::Comma);
222}
223
Chris Lattner8693a512006-08-13 21:54:02 +0000224/// ParseExpressionWithLeadingIdentifier - This special purpose method is used
225/// in contexts where we have already consumed an identifier (which we saved in
226/// 'Tok'), then discovered that the identifier was really the leading token of
227/// part of an assignment-expression. For example, in "A[1]+B", we consumed "A"
228/// (which is now in 'Tok') and the current token is "[".
229Parser::ExprResult Parser::
230ParseAssignmentExprWithLeadingIdentifier(const LexerToken &Tok) {
231 // We know that 'Tok' must correspond to this production:
232 // primary-expression: identifier
233
234 // TODO: Pass 'Tok' to the action.
235 ExprResult Res = ExprResult(false);
236
237 // Because we have to parse an entire cast-expression before starting the
238 // ParseRHSOfBinaryExpression method (which parses any trailing binops), we
239 // need to handle the 'postfix-expression' rules. We do this by invoking
240 // ParsePostfixExpressionSuffix to consume any postfix-expression suffixes:
241 Res = ParsePostfixExpressionSuffix(Res);
242 if (Res.isInvalid) return Res;
243
244 // At this point, the "A[1]" part of "A[1]+B" has been consumed. Once this is
245 // done, we know we don't have to do anything for cast-expression, because the
246 // only non-postfix-expression production starts with a '(' token, and we know
247 // we have an identifier. As such, we can invoke ParseRHSOfBinaryExpression
248 // to consume any trailing operators (e.g. "+" in this example) and connected
249 // chunks of the expression.
250 return ParseRHSOfBinaryExpression(Res, prec::Assignment);
251}
252
253
Chris Lattner62591722006-08-12 18:40:58 +0000254/// ParseAssignmentExpressionWithLeadingStar - This special purpose method is
255/// used in contexts where we have already consumed a '*' (which we saved in
256/// 'Tok'), then discovered that the '*' was really the leading token of an
257/// expression. For example, in "*(int*)P+B", we consumed "*" (which is
258/// now in 'Tok') and the current token is "(".
259Parser::ExprResult Parser::
260ParseAssignmentExpressionWithLeadingStar(const LexerToken &Tok) {
261 // We know that 'Tok' must correspond to this production:
262 // unary-expression: unary-operator cast-expression
263 // where 'unary-operator' is '*'.
264
265 // Parse the cast-expression that follows the '*'. This will parse the
266 // "*(int*)P" part of "*(int*)P+B".
267 ExprResult Res = ParseCastExpression(false);
268 if (Res.isInvalid) return Res;
269
270 // TODO: Combine Tok + Res to get the new AST.
271
272 // We have to parse an entire cast-expression before starting the
273 // ParseRHSOfBinaryExpression method (which parses any trailing binops). Since
274 // we know that the only production above us is the cast-expression
275 // production, and because the only alternative productions start with a '('
276 // token (we know we had a '*'), there is no work to do to get a whole
277 // cast-expression.
278
279 // At this point, the "*(int*)P" part of "*(int*)P+B" has been consumed. Once
280 // this is done, we can invoke ParseRHSOfBinaryExpression to consume any
281 // trailing operators (e.g. "+" in this example) and connected chunks of the
282 // assignment-expression.
283 return ParseRHSOfBinaryExpression(Res, prec::Assignment);
284}
285
286
Chris Lattnercde626a2006-08-12 08:13:25 +0000287/// ParseRHSOfBinaryExpression - Parse a binary expression that starts with
288/// LHS and has a precedence of at least MinPrec.
289Parser::ExprResult
290Parser::ParseRHSOfBinaryExpression(ExprResult LHS, unsigned MinPrec) {
291 unsigned NextTokPrec = getBinOpPrecedence(Tok.getKind());
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000292 SourceLocation ColonLoc;
293
Chris Lattnercde626a2006-08-12 08:13:25 +0000294 while (1) {
295 // If this token has a lower precedence than we are allowed to parse (e.g.
296 // because we are called recursively, or because the token is not a binop),
297 // then we are done!
298 if (NextTokPrec < MinPrec)
299 return LHS;
300
301 // Consume the operator, saving the operator token for error reporting.
302 LexerToken OpToken = Tok;
303 ConsumeToken();
304
Chris Lattner96c3deb2006-08-12 17:13:08 +0000305 // Special case handling for the ternary operator.
Chris Lattnerb5600a62006-10-06 05:40:05 +0000306 ExprResult TernaryMiddle(true);
Chris Lattner96c3deb2006-08-12 17:13:08 +0000307 if (NextTokPrec == prec::Conditional) {
308 if (Tok.getKind() != tok::colon) {
309 // Handle this production specially:
310 // logical-OR-expression '?' expression ':' conditional-expression
311 // In particular, the RHS of the '?' is 'expression', not
312 // 'logical-OR-expression' as we might expect.
313 TernaryMiddle = ParseExpression();
314 if (TernaryMiddle.isInvalid) return TernaryMiddle;
315 } else {
316 // Special case handling of "X ? Y : Z" where Y is empty:
317 // logical-OR-expression '?' ':' conditional-expression [GNU]
318 TernaryMiddle = ExprResult(false);
319 Diag(Tok, diag::ext_gnu_conditional_expr);
320 }
321
322 if (Tok.getKind() != tok::colon) {
323 Diag(Tok, diag::err_expected_colon);
324 Diag(OpToken, diag::err_matching, "?");
325 return ExprResult(true);
326 }
327
328 // Eat the colon.
Chris Lattneraf635312006-10-16 06:06:51 +0000329 ColonLoc = ConsumeToken();
Chris Lattnercde626a2006-08-12 08:13:25 +0000330 }
Chris Lattner96c3deb2006-08-12 17:13:08 +0000331
332 // Parse another leaf here for the RHS of the operator.
333 ExprResult RHS = ParseCastExpression(false);
334 if (RHS.isInvalid) return RHS;
Chris Lattnercde626a2006-08-12 08:13:25 +0000335
336 // Remember the precedence of this operator and get the precedence of the
337 // operator immediately to the right of the RHS.
338 unsigned ThisPrec = NextTokPrec;
339 NextTokPrec = getBinOpPrecedence(Tok.getKind());
Chris Lattner89d53752006-08-12 17:18:19 +0000340
341 // Assignment and conditional expressions are right-associative.
342 bool isRightAssoc = NextTokPrec == prec::Conditional ||
343 NextTokPrec == prec::Assignment;
Chris Lattnercde626a2006-08-12 08:13:25 +0000344
345 // Get the precedence of the operator to the right of the RHS. If it binds
346 // more tightly with RHS than we do, evaluate it completely first.
Chris Lattnercde626a2006-08-12 08:13:25 +0000347 if (ThisPrec < NextTokPrec ||
348 (ThisPrec == NextTokPrec && isRightAssoc)) {
Chris Lattner89d53752006-08-12 17:18:19 +0000349 // If this is left-associative, only parse things on the RHS that bind
350 // more tightly than the current operator. If it is left-associative, it
351 // is okay, to bind exactly as tightly. For example, compile A=B=C=D as
352 // A=(B=(C=D)), where each paren is a level of recursion here.
353 RHS = ParseRHSOfBinaryExpression(RHS, ThisPrec + !isRightAssoc);
Chris Lattnercde626a2006-08-12 08:13:25 +0000354 if (RHS.isInvalid) return RHS;
355
356 NextTokPrec = getBinOpPrecedence(Tok.getKind());
357 }
358 assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
359
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000360 // Combine the LHS and RHS into the LHS (e.g. build AST).
Chris Lattnerb5600a62006-10-06 05:40:05 +0000361 if (TernaryMiddle.isInvalid)
Chris Lattnerae319692006-10-25 03:49:28 +0000362 LHS = Actions.ParseBinOp(OpToken.getLocation(), OpToken.getKind(),
363 LHS.Val, RHS.Val);
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000364 else
365 LHS = Actions.ParseConditionalOp(OpToken.getLocation(), ColonLoc,
366 LHS.Val, TernaryMiddle.Val, RHS.Val);
Chris Lattnercde626a2006-08-12 08:13:25 +0000367 }
368}
369
Chris Lattnereaf06592006-08-11 02:02:23 +0000370/// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
371/// true, parse a unary-expression.
372///
Chris Lattner4564bc12006-08-10 23:14:52 +0000373/// cast-expression: [C99 6.5.4]
374/// unary-expression
375/// '(' type-name ')' cast-expression
Chris Lattner81b576e2006-08-11 02:13:20 +0000376///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000377/// unary-expression: [C99 6.5.3]
378/// postfix-expression
379/// '++' unary-expression
380/// '--' unary-expression
381/// unary-operator cast-expression
382/// 'sizeof' unary-expression
383/// 'sizeof' '(' type-name ')'
384/// [GNU] '__alignof' unary-expression
385/// [GNU] '__alignof' '(' type-name ')'
386/// [GNU] '&&' identifier
Chris Lattner81b576e2006-08-11 02:13:20 +0000387///
Chris Lattnerc2dd85a2006-08-10 22:57:16 +0000388/// unary-operator: one of
389/// '&' '*' '+' '-' '~' '!'
390/// [GNU] '__extension__' '__real' '__imag'
391///
Chris Lattner52a99e52006-08-10 20:56:00 +0000392/// primary-expression: [C99 6.5.1]
Chris Lattnerc5e0d4a2006-08-10 19:06:03 +0000393/// identifier
394/// constant
395/// string-literal
396/// '(' expression ')'
Chris Lattner52a99e52006-08-10 20:56:00 +0000397/// '__func__' [C99 6.4.2.2]
398/// [GNU] '__FUNCTION__'
399/// [GNU] '__PRETTY_FUNCTION__'
400/// [GNU] '(' compound-statement ')'
401/// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
402/// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
403/// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
404/// assign-expr ')'
405/// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
406/// [OBC] '[' objc-receiver objc-message-args ']' [TODO]
407/// [OBC] '@selector' '(' objc-selector-arg ')' [TODO]
408/// [OBC] '@protocol' '(' identifier ')' [TODO]
409/// [OBC] '@encode' '(' type-name ')' [TODO]
410/// [OBC] objc-string-literal [TODO]
411///
412/// constant: [C99 6.4.4]
413/// integer-constant
414/// floating-constant
415/// enumeration-constant -> identifier
416/// character-constant
Chris Lattner52a99e52006-08-10 20:56:00 +0000417///
Chris Lattner89c50c62006-08-11 06:41:18 +0000418Parser::ExprResult Parser::ParseCastExpression(bool isUnaryExpression) {
419 ExprResult Res;
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000420 tok::TokenKind SavedKind = Tok.getKind();
Chris Lattner89c50c62006-08-11 06:41:18 +0000421
Chris Lattner81b576e2006-08-11 02:13:20 +0000422 // This handles all of cast-expression, unary-expression, postfix-expression,
423 // and primary-expression. We handle them together like this for efficiency
424 // and to simplify handling of an expression starting with a '(' token: which
425 // may be one of a parenthesized expression, cast-expression, compound literal
426 // expression, or statement expression.
427 //
428 // If the parsed tokens consist of a primary-expression, the cases below
Chris Lattner20c6a452006-08-12 17:40:43 +0000429 // call ParsePostfixExpressionSuffix to handle the postfix expression
430 // suffixes. Cases that cannot be followed by postfix exprs should
431 // return without invoking ParsePostfixExpressionSuffix.
Chris Lattnerae319692006-10-25 03:49:28 +0000432 switch (SavedKind) {
Chris Lattnere550a4e2006-08-24 06:37:51 +0000433 case tok::l_paren: {
Chris Lattner81b576e2006-08-11 02:13:20 +0000434 // If this expression is limited to being a unary-expression, the parent can
435 // not start a cast expression.
436 ParenParseOption ParenExprType =
437 isUnaryExpression ? CompoundLiteral : CastExpr;
Chris Lattnere550a4e2006-08-24 06:37:51 +0000438 TypeTy *CastTy;
439 SourceLocation LParenLoc = Tok.getLocation();
440 SourceLocation RParenLoc;
441 Res = ParseParenExpression(ParenExprType, CastTy, RParenLoc);
Chris Lattner89c50c62006-08-11 06:41:18 +0000442 if (Res.isInvalid) return Res;
443
Chris Lattner81b576e2006-08-11 02:13:20 +0000444 switch (ParenExprType) {
445 case SimpleExpr: break; // Nothing else to do.
446 case CompoundStmt: break; // Nothing else to do.
447 case CompoundLiteral:
448 // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
449 // postfix-expression exist, parse them now.
450 break;
451 case CastExpr:
452 // We parsed '(' type-name ')' and the thing after it wasn't a '{'. Parse
453 // the cast-expression that follows it next.
Chris Lattnere550a4e2006-08-24 06:37:51 +0000454 // TODO: For cast expression with CastTy.
455 Res = ParseCastExpression(false);
456 if (!Res.isInvalid)
457 Res = Actions.ParseCastExpr(LParenLoc, CastTy, RParenLoc, Res.Val);
458 return Res;
Chris Lattner81b576e2006-08-11 02:13:20 +0000459 }
Chris Lattner20c6a452006-08-12 17:40:43 +0000460
461 // These can be followed by postfix-expr pieces.
462 return ParsePostfixExpressionSuffix(Res);
Chris Lattnere550a4e2006-08-24 06:37:51 +0000463 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000464
Chris Lattner52a99e52006-08-10 20:56:00 +0000465 // primary-expression
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000466 case tok::numeric_constant:
467 // constant: integer-constant
468 // constant: floating-constant
469
470 // TODO: Validate whether this is an integer or floating-constant or
471 // neither.
472 if (1) {
Chris Lattnerae319692006-10-25 03:49:28 +0000473 Res = Actions.ParseIntegerConstant(Tok.getLocation());
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000474 } else {
Chris Lattnerae319692006-10-25 03:49:28 +0000475 Res = Actions.ParseFloatingConstant(Tok.getLocation());
Chris Lattner9b6d4cb2006-08-23 05:17:46 +0000476 }
477 ConsumeToken();
478
479 // These can be followed by postfix-expr pieces.
480 return ParsePostfixExpressionSuffix(Res);
481
Chris Lattner52a99e52006-08-10 20:56:00 +0000482 case tok::identifier: // primary-expression: identifier
483 // constant: enumeration-constant
Chris Lattner52a99e52006-08-10 20:56:00 +0000484 case tok::char_constant: // constant: character-constant
485 case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
486 case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
487 case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
Chris Lattnerae319692006-10-25 03:49:28 +0000488 Res = Actions.ParseSimplePrimaryExpr(Tok.getLocation(), SavedKind);
Chris Lattner52a99e52006-08-10 20:56:00 +0000489 ConsumeToken();
Chris Lattner20c6a452006-08-12 17:40:43 +0000490 // These can be followed by postfix-expr pieces.
491 return ParsePostfixExpressionSuffix(Res);
Chris Lattner52a99e52006-08-10 20:56:00 +0000492 case tok::string_literal: // primary-expression: string-literal
Chris Lattnerd3e98952006-10-06 05:22:26 +0000493 case tok::wide_string_literal:
Chris Lattner89c50c62006-08-11 06:41:18 +0000494 Res = ParseStringLiteralExpression();
495 if (Res.isInvalid) return Res;
Chris Lattner20c6a452006-08-12 17:40:43 +0000496 // This can be followed by postfix-expr pieces (e.g. "foo"[1]).
497 return ParsePostfixExpressionSuffix(Res);
Chris Lattnerf8339772006-08-10 22:01:51 +0000498 case tok::kw___builtin_va_arg:
499 case tok::kw___builtin_offsetof:
500 case tok::kw___builtin_choose_expr:
501 case tok::kw___builtin_types_compatible_p:
Chris Lattner11124352006-08-12 19:16:08 +0000502 return ParseBuiltinPrimaryExpression();
Chris Lattner81b576e2006-08-11 02:13:20 +0000503 case tok::plusplus: // unary-expression: '++' unary-expression
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000504 case tok::minusminus: { // unary-expression: '--' unary-expression
505 SourceLocation SavedLoc = ConsumeToken();
Chris Lattner1b926492006-08-23 06:42:10 +0000506 Res = ParseCastExpression(true);
507 if (!Res.isInvalid)
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000508 Res = Actions.ParseUnaryOp(SavedLoc, SavedKind, Res.Val);
Chris Lattner1b926492006-08-23 06:42:10 +0000509 return Res;
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000510 }
Chris Lattner81b576e2006-08-11 02:13:20 +0000511 case tok::amp: // unary-expression: '&' cast-expression
512 case tok::star: // unary-expression: '*' cast-expression
513 case tok::plus: // unary-expression: '+' cast-expression
514 case tok::minus: // unary-expression: '-' cast-expression
515 case tok::tilde: // unary-expression: '~' cast-expression
516 case tok::exclaim: // unary-expression: '!' cast-expression
517 case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
Chris Lattner1b926492006-08-23 06:42:10 +0000518 case tok::kw___imag: // unary-expression: '__imag' cast-expression [GNU]
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000519 case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
Chris Lattner4daa0772006-10-20 05:03:44 +0000520 // FIXME: Extension not handled correctly here!
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000521 SourceLocation SavedLoc = ConsumeToken();
Chris Lattner1b926492006-08-23 06:42:10 +0000522 Res = ParseCastExpression(false);
523 if (!Res.isInvalid)
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000524 Res = Actions.ParseUnaryOp(SavedLoc, SavedKind, Res.Val);
Chris Lattner1b926492006-08-23 06:42:10 +0000525 return Res;
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000526 }
Chris Lattner81b576e2006-08-11 02:13:20 +0000527 case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
528 // unary-expression: 'sizeof' '(' type-name ')'
529 case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
530 // unary-expression: '__alignof' '(' type-name ')'
Chris Lattner89c50c62006-08-11 06:41:18 +0000531 return ParseSizeofAlignofExpression();
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000532 case tok::ampamp: { // unary-expression: '&&' identifier
Chris Lattner81b576e2006-08-11 02:13:20 +0000533 Diag(Tok, diag::ext_gnu_address_of_label);
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000534 SourceLocation SavedLoc = ConsumeToken();
Chris Lattner14a1b642006-10-15 22:33:58 +0000535
536 if (Tok.getKind() != tok::identifier) {
Chris Lattner81b576e2006-08-11 02:13:20 +0000537 Diag(Tok, diag::err_expected_ident);
Chris Lattner89c50c62006-08-11 06:41:18 +0000538 return ExprResult(true);
Chris Lattner81b576e2006-08-11 02:13:20 +0000539 }
Chris Lattner14a1b642006-10-15 22:33:58 +0000540 // FIXME: Create a label ref for Tok.Ident.
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000541 Res = Actions.ParseUnaryOp(SavedLoc, SavedKind, 0);
Chris Lattner14a1b642006-10-15 22:33:58 +0000542 ConsumeToken();
543
544 return Res;
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000545 }
Chris Lattner52a99e52006-08-10 20:56:00 +0000546 default:
547 Diag(Tok, diag::err_expected_expression);
Chris Lattner89c50c62006-08-11 06:41:18 +0000548 return ExprResult(true);
Chris Lattnerf8339772006-08-10 22:01:51 +0000549 }
550
Chris Lattner20c6a452006-08-12 17:40:43 +0000551 // unreachable.
552 abort();
553}
554
555/// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
556/// is parsed, this method parses any suffixes that apply.
557///
558/// postfix-expression: [C99 6.5.2]
559/// primary-expression
560/// postfix-expression '[' expression ']'
561/// postfix-expression '(' argument-expression-list[opt] ')'
562/// postfix-expression '.' identifier
563/// postfix-expression '->' identifier
564/// postfix-expression '++'
565/// postfix-expression '--'
566/// '(' type-name ')' '{' initializer-list '}'
567/// '(' type-name ')' '{' initializer-list ',' '}'
568///
569/// argument-expression-list: [C99 6.5.2]
570/// argument-expression
571/// argument-expression-list ',' assignment-expression
572///
573Parser::ExprResult Parser::ParsePostfixExpressionSuffix(ExprResult LHS) {
Chris Lattner20c6a452006-08-12 17:40:43 +0000574
Chris Lattnerf8339772006-08-10 22:01:51 +0000575 // Now that the primary-expression piece of the postfix-expression has been
576 // parsed, see if there are any postfix-expression pieces here.
577 SourceLocation Loc;
578 while (1) {
579 switch (Tok.getKind()) {
Chris Lattner20c6a452006-08-12 17:40:43 +0000580 default: // Not a postfix-expression suffix.
581 return LHS;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000582 case tok::l_square: { // postfix-expression: p-e '[' expression ']'
Chris Lattner04132372006-10-16 06:12:55 +0000583 Loc = 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 Lattner04132372006-10-16 06:12:55 +0000603 Loc = ConsumeParen();
Chris Lattner89c50c62006-08-11 06:41:18 +0000604
Chris Lattner0c6c0342006-08-12 18:12:45 +0000605 if (Tok.getKind() != tok::r_paren) {
606 while (1) {
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000607 ExprResult ArgExpr = ParseAssignmentExpression();
608 if (ArgExpr.isInvalid)
609 ArgExprsOk = false;
610 else
611 ArgExprs.push_back(ArgExpr.Val);
612
Chris Lattner0c6c0342006-08-12 18:12:45 +0000613 if (Tok.getKind() != tok::comma)
614 break;
Chris Lattneraf635312006-10-16 06:06:51 +0000615 // Move to the next argument, remember where the comma was.
616 CommaLocs.push_back(ConsumeToken());
Chris Lattner0c6c0342006-08-12 18:12:45 +0000617 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000618 }
Chris Lattner81b576e2006-08-11 02:13:20 +0000619
Chris Lattner89c50c62006-08-11 06:41:18 +0000620 // Match the ')'.
Chris Lattnere165d942006-08-24 04:40:38 +0000621 if (!LHS.isInvalid && ArgExprsOk && Tok.getKind() == tok::r_paren) {
622 assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&&
623 "Unexpected number of commas!");
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000624 LHS = Actions.ParseCallExpr(LHS.Val, Loc, &ArgExprs[0], ArgExprs.size(),
Chris Lattnere165d942006-08-24 04:40:38 +0000625 &CommaLocs[0], Tok.getLocation());
626 }
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000627
Chris Lattner04f80192006-08-15 04:55:54 +0000628 MatchRHSPunctuation(tok::r_paren, Loc);
Chris Lattner89c50c62006-08-11 06:41:18 +0000629 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000630 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000631 case tok::arrow: // postfix-expression: p-e '->' identifier
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000632 case tok::period: { // postfix-expression: p-e '.' identifier
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000633 tok::TokenKind OpKind = Tok.getKind();
Chris Lattneraf635312006-10-16 06:06:51 +0000634 SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000635
Chris Lattner89c50c62006-08-11 06:41:18 +0000636 if (Tok.getKind() != tok::identifier) {
637 Diag(Tok, diag::err_expected_ident);
638 return ExprResult(true);
639 }
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000640
641 if (!LHS.isInvalid)
642 LHS = Actions.ParseMemberReferenceExpr(LHS.Val, OpLoc, OpKind,
643 Tok.getLocation(),
644 *Tok.getIdentifierInfo());
Chris Lattner89c50c62006-08-11 06:41:18 +0000645 ConsumeToken();
646 break;
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000647 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000648 case tok::plusplus: // postfix-expression: postfix-expression '++'
649 case tok::minusminus: // postfix-expression: postfix-expression '--'
Chris Lattner1b926492006-08-23 06:42:10 +0000650 if (!LHS.isInvalid)
Chris Lattnerae319692006-10-25 03:49:28 +0000651 LHS = Actions.ParsePostfixUnaryOp(Tok.getLocation(), Tok.getKind(),
652 LHS.Val);
Chris Lattner89c50c62006-08-11 06:41:18 +0000653 ConsumeToken();
654 break;
Chris Lattnerf8339772006-08-10 22:01:51 +0000655 }
656 }
Chris Lattner52a99e52006-08-10 20:56:00 +0000657}
658
Chris Lattner20c6a452006-08-12 17:40:43 +0000659
Chris Lattner81b576e2006-08-11 02:13:20 +0000660/// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
661/// unary-expression: [C99 6.5.3]
662/// 'sizeof' unary-expression
663/// 'sizeof' '(' type-name ')'
664/// [GNU] '__alignof' unary-expression
665/// [GNU] '__alignof' '(' type-name ')'
Chris Lattner89c50c62006-08-11 06:41:18 +0000666Parser::ExprResult Parser::ParseSizeofAlignofExpression() {
Chris Lattner81b576e2006-08-11 02:13:20 +0000667 assert((Tok.getKind() == tok::kw_sizeof ||
668 Tok.getKind() == tok::kw___alignof) &&
669 "Not a sizeof/alignof expression!");
Chris Lattner26115ac2006-08-24 06:10:04 +0000670 LexerToken OpTok = Tok;
Chris Lattner81b576e2006-08-11 02:13:20 +0000671 ConsumeToken();
672
673 // If the operand doesn't start with an '(', it must be an expression.
Chris Lattner26115ac2006-08-24 06:10:04 +0000674 ExprResult Operand;
675 if (Tok.getKind() != tok::l_paren) {
676 Operand = ParseCastExpression(true);
677 } else {
678 // If it starts with a '(', we know that it is either a parenthesized
679 // type-name, or it is a unary-expression that starts with a compound
680 // literal, or starts with a primary-expression that is a parenthesized
681 // expression.
682 ParenParseOption ExprType = CastExpr;
Chris Lattnere550a4e2006-08-24 06:37:51 +0000683 TypeTy *CastTy;
Chris Lattner26da7302006-08-24 06:49:19 +0000684 SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
Chris Lattnere550a4e2006-08-24 06:37:51 +0000685 Operand = ParseParenExpression(ExprType, CastTy, RParenLoc);
Chris Lattner26115ac2006-08-24 06:10:04 +0000686
687 // If ParseParenExpression parsed a '(typename)' sequence only, the this is
688 // sizeof/alignof a type. Otherwise, it is sizeof/alignof an expression.
689 if (ExprType == CastExpr) {
Chris Lattner26da7302006-08-24 06:49:19 +0000690 return Actions.ParseSizeOfAlignOfTypeExpr(OpTok.getLocation(),
691 OpTok.getKind() == tok::kw_sizeof,
692 LParenLoc, CastTy, RParenLoc);
Chris Lattner26115ac2006-08-24 06:10:04 +0000693 }
694 }
Chris Lattner81b576e2006-08-11 02:13:20 +0000695
Chris Lattner26115ac2006-08-24 06:10:04 +0000696 // If we get here, the operand to the sizeof/alignof was an expresion.
697 if (!Operand.isInvalid)
Chris Lattner0ba3dc42006-10-25 03:38:23 +0000698 Operand = Actions.ParseUnaryOp(OpTok.getLocation(), OpTok.getKind(),
699 Operand.Val);
Chris Lattner26115ac2006-08-24 06:10:04 +0000700 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);
Chris Lattner11124352006-08-12 19:16:08 +0000719 const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
720
721 tok::TokenKind T = Tok.getKind();
Chris Lattneraf635312006-10-16 06:06:51 +0000722 SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
Chris Lattner11124352006-08-12 19:16:08 +0000723
724 // All of these start with an open paren.
725 if (Tok.getKind() != tok::l_paren) {
726 Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
727 return ExprResult(true);
728 }
729
Chris Lattner04132372006-10-16 06:12:55 +0000730 SourceLocation LParenLoc = ConsumeParen();
Chris Lattner6d28d9b2006-08-24 03:51:22 +0000731 // TODO: Build AST.
732
Chris Lattner11124352006-08-12 19:16:08 +0000733 switch (T) {
734 default: assert(0 && "Not a builtin primary expression!");
735 case tok::kw___builtin_va_arg:
736 Res = ParseAssignmentExpression();
737 if (Res.isInvalid) {
738 SkipUntil(tok::r_paren);
739 return Res;
740 }
Chris Lattner0be454e2006-08-12 19:30:51 +0000741
Chris Lattner6d7e6342006-08-15 03:41:14 +0000742 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000743 return ExprResult(true);
Chris Lattner0be454e2006-08-12 19:30:51 +0000744
Chris Lattner11124352006-08-12 19:16:08 +0000745 ParseTypeName();
746 break;
747
748 case tok::kw___builtin_offsetof:
749 ParseTypeName();
750
Chris Lattner6d7e6342006-08-15 03:41:14 +0000751 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000752 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000753
754 // We must have at least one identifier here.
Chris Lattner6d7e6342006-08-15 03:41:14 +0000755 if (ExpectAndConsume(tok::identifier, diag::err_expected_ident, "",
Chris Lattnerdbb2a462006-08-12 19:26:13 +0000756 tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000757 return ExprResult(true);
Chris Lattnerdbb2a462006-08-12 19:26:13 +0000758
Chris Lattner11124352006-08-12 19:16:08 +0000759 while (1) {
760 if (Tok.getKind() == tok::period) {
761 // offsetof-member-designator: offsetof-member-designator '.' identifier
762 ConsumeToken();
763
Chris Lattner6d7e6342006-08-15 03:41:14 +0000764 if (ExpectAndConsume(tok::identifier, diag::err_expected_ident, "",
Chris Lattnerdbb2a462006-08-12 19:26:13 +0000765 tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000766 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000767 } else if (Tok.getKind() == tok::l_square) {
768 // offsetof-member-designator: offsetof-member-design '[' expression ']'
Chris Lattner04132372006-10-16 06:12:55 +0000769 SourceLocation LSquareLoc = ConsumeBracket();
Chris Lattner11124352006-08-12 19:16:08 +0000770 Res = ParseExpression();
771 if (Res.isInvalid) {
772 SkipUntil(tok::r_paren);
773 return Res;
774 }
775
Chris Lattner04f80192006-08-15 04:55:54 +0000776 MatchRHSPunctuation(tok::r_square, LSquareLoc);
Chris Lattner11124352006-08-12 19:16:08 +0000777 } else {
778 break;
779 }
780 }
781 break;
782 case tok::kw___builtin_choose_expr:
783 Res = ParseAssignmentExpression();
784
Chris Lattner6d7e6342006-08-15 03:41:14 +0000785 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000786 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000787
788 Res = ParseAssignmentExpression();
789
Chris Lattner6d7e6342006-08-15 03:41:14 +0000790 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000791 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000792
793 Res = ParseAssignmentExpression();
794 break;
795 case tok::kw___builtin_types_compatible_p:
796 ParseTypeName();
797
Chris Lattner6d7e6342006-08-15 03:41:14 +0000798 if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
Chris Lattner11124352006-08-12 19:16:08 +0000799 return ExprResult(true);
Chris Lattner11124352006-08-12 19:16:08 +0000800
801 ParseTypeName();
802 break;
803 }
804
Chris Lattner04f80192006-08-15 04:55:54 +0000805 MatchRHSPunctuation(tok::r_paren, LParenLoc);
Chris Lattner11124352006-08-12 19:16:08 +0000806
807 // These can be followed by postfix-expr pieces because they are
808 // primary-expressions.
809 return ParsePostfixExpressionSuffix(Res);
810}
811
Chris Lattnerc951dae2006-08-10 04:23:57 +0000812
Chris Lattner4add4e62006-08-11 01:33:00 +0000813/// ParseParenExpression - This parses the unit that starts with a '(' token,
814/// based on what is allowed by ExprType. The actual thing parsed is returned
815/// in ExprType.
816///
817/// primary-expression: [C99 6.5.1]
Chris Lattnerc951dae2006-08-10 04:23:57 +0000818/// '(' expression ')'
Chris Lattnerf8339772006-08-10 22:01:51 +0000819/// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
820/// postfix-expression: [C99 6.5.2]
821/// '(' type-name ')' '{' initializer-list '}'
822/// '(' type-name ')' '{' initializer-list ',' '}'
Chris Lattner4add4e62006-08-11 01:33:00 +0000823/// cast-expression: [C99 6.5.4]
824/// '(' type-name ')' cast-expression
Chris Lattnerf8339772006-08-10 22:01:51 +0000825///
Chris Lattnere550a4e2006-08-24 06:37:51 +0000826Parser::ExprResult Parser::ParseParenExpression(ParenParseOption &ExprType,
827 TypeTy *&CastTy,
828 SourceLocation &RParenLoc) {
Chris Lattnerc951dae2006-08-10 04:23:57 +0000829 assert(Tok.getKind() == tok::l_paren && "Not a paren expr!");
Chris Lattner04132372006-10-16 06:12:55 +0000830 SourceLocation OpenLoc = ConsumeParen();
Chris Lattner89c50c62006-08-11 06:41:18 +0000831 ExprResult Result(false);
Chris Lattnere550a4e2006-08-24 06:37:51 +0000832 CastTy = 0;
Chris Lattnerc951dae2006-08-10 04:23:57 +0000833
Chris Lattner4add4e62006-08-11 01:33:00 +0000834 if (ExprType >= CompoundStmt && Tok.getKind() == tok::l_brace &&
Chris Lattnerf8339772006-08-10 22:01:51 +0000835 !getLang().NoExtensions) {
836 Diag(Tok, diag::ext_gnu_statement_expr);
837 ParseCompoundStatement();
Chris Lattner4add4e62006-08-11 01:33:00 +0000838 ExprType = CompoundStmt;
Chris Lattner1b926492006-08-23 06:42:10 +0000839 // TODO: Build AST for GNU compound stmt.
Chris Lattner4add4e62006-08-11 01:33:00 +0000840 } else if (ExprType >= CompoundLiteral && isTypeSpecifierQualifier()) {
Chris Lattner6c3f05d2006-08-12 16:54:25 +0000841 // Otherwise, this is a compound literal expression or cast expression.
Chris Lattnere550a4e2006-08-24 06:37:51 +0000842 TypeTy *Ty = ParseTypeName();
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000843
844 // Match the ')'.
Chris Lattner04132372006-10-16 06:12:55 +0000845 if (Tok.getKind() == tok::r_paren)
846 RParenLoc = ConsumeParen();
847 else
Chris Lattnere550a4e2006-08-24 06:37:51 +0000848 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Chris Lattnere550a4e2006-08-24 06:37:51 +0000849
Chris Lattner4add4e62006-08-11 01:33:00 +0000850 if (Tok.getKind() == tok::l_brace) {
Chris Lattner6c3f05d2006-08-12 16:54:25 +0000851 if (!getLang().C99) // Compound literals don't exist in C90.
852 Diag(OpenLoc, diag::ext_c99_compound_literal);
Chris Lattner89c50c62006-08-11 06:41:18 +0000853 Result = ParseInitializer();
Chris Lattner4add4e62006-08-11 01:33:00 +0000854 ExprType = CompoundLiteral;
Chris Lattner1b926492006-08-23 06:42:10 +0000855 // TODO: Build AST for compound literal.
Chris Lattner4add4e62006-08-11 01:33:00 +0000856 } else if (ExprType == CastExpr) {
Chris Lattnere550a4e2006-08-24 06:37:51 +0000857 // Note that this doesn't parse the subsequence cast-expression, it just
858 // returns the parsed type to the callee.
Chris Lattner4add4e62006-08-11 01:33:00 +0000859 ExprType = CastExpr;
Chris Lattnere550a4e2006-08-24 06:37:51 +0000860 CastTy = Ty;
861 return ExprResult(false);
Chris Lattner4add4e62006-08-11 01:33:00 +0000862 } else {
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000863 Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
Chris Lattner89c50c62006-08-11 06:41:18 +0000864 return ExprResult(true);
Chris Lattnerf5fbd792006-08-10 23:56:11 +0000865 }
Chris Lattner89c50c62006-08-11 06:41:18 +0000866 return Result;
Chris Lattner4add4e62006-08-11 01:33:00 +0000867 } else {
Chris Lattner89c50c62006-08-11 06:41:18 +0000868 Result = ParseExpression();
Chris Lattner4add4e62006-08-11 01:33:00 +0000869 ExprType = SimpleExpr;
Chris Lattner1b926492006-08-23 06:42:10 +0000870 if (!Result.isInvalid && Tok.getKind() == tok::r_paren)
871 Result = Actions.ParseParenExpr(OpenLoc, Tok.getLocation(), Result.Val);
Chris Lattnerf8339772006-08-10 22:01:51 +0000872 }
Chris Lattnerc951dae2006-08-10 04:23:57 +0000873
Chris Lattner4564bc12006-08-10 23:14:52 +0000874 // Match the ')'.
Chris Lattner89c50c62006-08-11 06:41:18 +0000875 if (Result.isInvalid)
876 SkipUntil(tok::r_paren);
Chris Lattnere550a4e2006-08-24 06:37:51 +0000877 else {
Chris Lattner04132372006-10-16 06:12:55 +0000878 if (Tok.getKind() == tok::r_paren)
879 RParenLoc = ConsumeParen();
880 else
Chris Lattnere550a4e2006-08-24 06:37:51 +0000881 MatchRHSPunctuation(tok::r_paren, OpenLoc);
Chris Lattnere550a4e2006-08-24 06:37:51 +0000882 }
Chris Lattner1b926492006-08-23 06:42:10 +0000883
Chris Lattner89c50c62006-08-11 06:41:18 +0000884 return Result;
Chris Lattnerc951dae2006-08-10 04:23:57 +0000885}
Chris Lattnerd3e98952006-10-06 05:22:26 +0000886
887/// HexDigitValue - Return the value of the specified hex digit, or -1 if it's
888/// not valid.
889static int HexDigitValue(char C) {
890 if (C >= '0' && C <= '9') return C-'0';
891 if (C >= 'a' && C <= 'f') return C-'a'+10;
892 if (C >= 'A' && C <= 'F') return C-'A'+10;
893 return -1;
894}
895
896/// ParseStringLiteralExpression - This handles the various token types that
897/// form string literals, and also handles string concatenation [C99 5.1.1.2,
898/// translation phase #6].
899///
900/// primary-expression: [C99 6.5.1]
901/// string-literal
902Parser::ExprResult Parser::ParseStringLiteralExpression() {
903 assert(isTokenStringLiteral() && "Not a string literal!");
904
905 // String concat. Note that keywords like __func__ and __FUNCTION__ are not
906 // considered to be strings for concatenation purposes.
907 SmallVector<LexerToken, 4> StringToks;
908
909 // While we're looking at all of the string portions, remember the max
910 // individual token length, computing a bound on the concatenated string
911 // length, and see whether any piece is a wide-string. If any of the string
912 // portions is a wide-string literal, the result is also a wide-string literal
913 // [C99 6.4.5p4].
914 unsigned SizeBound = 0, MaxTokenLength = 0;
915 bool AnyWide = false;
916 do {
917 // The string could be shorter than this if it needs cleaning, but this is a
918 // reasonable bound, which is all we need.
919 SizeBound += Tok.getLength()-2; // -2 for "".
920
921 // Find maximum string piece length.
922 if (Tok.getLength() > MaxTokenLength)
923 MaxTokenLength = Tok.getLength();
924
925 // Remember if we see any wide strings.
926 AnyWide |= Tok.getKind() == tok::wide_string_literal;
927
928 // Remember the string token.
929 StringToks.push_back(Tok);
930 ConsumeStringToken();
931 } while (isTokenStringLiteral());
932
933 // Include space for the null terminator.
934 ++SizeBound;
935
936 // TODO: K&R warning: "traditional C rejects string constant concatenation"
937
Chris Lattner02dffbd2006-10-14 07:50:21 +0000938 // Get the width in bytes of wchar_t. If no wchar_t strings are used, do not
939 // query the target. As such, wchar_tByteWidth is only valid if AnyWide=true.
940 unsigned wchar_tByteWidth = ~0U;
941 if (AnyWide)
942 wchar_tByteWidth=getTargetInfo().getWCharWidth(StringToks[0].getLocation());
Chris Lattnerd3e98952006-10-06 05:22:26 +0000943
944 // The output buffer size needs to be large enough to hold wide characters.
945 // This is a worst-case assumption which basically corresponds to L"" "long".
946 if (AnyWide)
947 SizeBound *= wchar_tByteWidth;
948
949 // Create a temporary buffer to hold the result string data. If it is "big",
950 // use malloc, otherwise use alloca.
951 char *ResultBuf;
952 if (SizeBound > 512)
953 ResultBuf = (char*)malloc(SizeBound);
954 else
955 ResultBuf = (char*)alloca(SizeBound);
956
957 // Likewise, but for each string piece.
958 char *TokenBuf;
959 if (MaxTokenLength > 512)
960 TokenBuf = (char*)malloc(MaxTokenLength);
961 else
962 TokenBuf = (char*)alloca(MaxTokenLength);
963
964 // Loop over all the strings, getting their spelling, and expanding them to
965 // wide strings as appropriate.
966 char *ResultPtr = ResultBuf; // Next byte to fill in.
967
968 for (unsigned i = 0, e = StringToks.size(); i != e; ++i) {
969 const char *ThisTokBuf = TokenBuf;
970 // Get the spelling of the token, which eliminates trigraphs, etc. We know
971 // that ThisTokBuf points to a buffer that is big enough for the whole token
972 // and 'spelled' tokens can only shrink.
973 unsigned ThisTokLen = PP.getSpelling(StringToks[i], ThisTokBuf);
974 const char *ThisTokEnd = ThisTokBuf+ThisTokLen-1; // Skip end quote.
975
976 // TODO: Input character set mapping support.
977
978 // Skip L marker for wide strings.
979 if (ThisTokBuf[0] == 'L') ++ThisTokBuf;
980
981 assert(ThisTokBuf[0] == '"' && "Expected quote, lexer broken?");
982 ++ThisTokBuf;
983
984 while (ThisTokBuf != ThisTokEnd) {
985 // Is this a span of non-escape characters?
986 if (ThisTokBuf[0] != '\\') {
987 const char *InStart = ThisTokBuf;
988 do {
989 ++ThisTokBuf;
990 } while (ThisTokBuf != ThisTokEnd && ThisTokBuf[0] != '\\');
991
992 // Copy the character span over.
993 unsigned Len = ThisTokBuf-InStart;
994 if (!AnyWide) {
995 memcpy(ResultPtr, InStart, Len);
996 ResultPtr += Len;
997 } else {
998 // Note: our internal rep of wide char tokens is always little-endian.
999 for (; Len; --Len, ++InStart) {
1000 *ResultPtr++ = InStart[0];
1001 // Add zeros at the end.
1002 for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
1003 *ResultPtr++ = 0;
1004 }
1005 }
1006 continue;
1007 }
1008
1009 // Otherwise, this is an escape character. Skip the '\' char.
1010 ++ThisTokBuf;
1011
1012 // We know that this character can't be off the end of the buffer, because
1013 // that would have been \", which would not have been the end of string.
1014 unsigned ResultChar = *ThisTokBuf++;
1015 switch (ResultChar) {
1016 // These map to themselves.
1017 case '\\': case '\'': case '"': case '?': break;
1018
1019 // These have fixed mappings.
1020 case 'a':
1021 // TODO: K&R: the meaning of '\\a' is different in traditional C
1022 ResultChar = 7;
1023 break;
1024 case 'b':
1025 ResultChar = 8;
1026 break;
1027 case 'e':
1028 PP.Diag(StringToks[i], diag::ext_nonstandard_escape, "e");
1029 ResultChar = 27;
1030 break;
1031 case 'f':
1032 ResultChar = 12;
1033 break;
1034 case 'n':
1035 ResultChar = 10;
1036 break;
1037 case 'r':
1038 ResultChar = 13;
1039 break;
1040 case 't':
1041 ResultChar = 9;
1042 break;
1043 case 'v':
1044 ResultChar = 11;
1045 break;
1046
1047 //case 'u': case 'U': // FIXME: UCNs.
1048 case 'x': // Hex escape.
1049 if (ThisTokBuf == ThisTokEnd ||
1050 (ResultChar = HexDigitValue(*ThisTokBuf)) == ~0U) {
1051 PP.Diag(StringToks[i], diag::err_hex_escape_no_digits);
1052 ResultChar = 0;
1053 break;
1054 }
1055 ++ThisTokBuf; // Consumed one hex digit.
1056
1057 assert(0 && "hex escape: unimp!");
1058 break;
1059 case '0': case '1': case '2': case '3':
1060 case '4': case '5': case '6': case '7':
1061 // Octal escapes.
1062 assert(0 && "octal escape: unimp!");
1063 break;
1064
1065 // Otherwise, these are not valid escapes.
1066 case '(': case '{': case '[': case '%':
1067 // GCC accepts these as extensions. We warn about them as such though.
1068 if (!PP.getLangOptions().NoExtensions) {
1069 PP.Diag(StringToks[i], diag::ext_nonstandard_escape,
1070 std::string()+(char)ResultChar);
1071 break;
1072 }
1073 // FALL THROUGH.
1074 default:
1075 if (isgraph(ThisTokBuf[0])) {
1076 PP.Diag(StringToks[i], diag::ext_unknown_escape,
1077 std::string()+(char)ResultChar);
1078 } else {
1079 PP.Diag(StringToks[i], diag::ext_unknown_escape,
1080 "x"+utohexstr(ResultChar));
1081 }
1082 }
1083
1084 // Note: our internal rep of wide char tokens is always little-endian.
Chris Lattner02dffbd2006-10-14 07:50:21 +00001085 *ResultPtr++ = ResultChar & 0xFF;
1086
1087 if (AnyWide) {
1088 for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
1089 *ResultPtr++ = ResultChar >> i*8;
1090 }
Chris Lattnerd3e98952006-10-06 05:22:26 +00001091 }
1092 }
1093
1094 // Add zero terminator.
1095 *ResultPtr = 0;
1096 if (AnyWide) {
1097 for (unsigned i = 1, e = wchar_tByteWidth; i != e; ++i)
1098 *ResultPtr++ = 0;
1099 }
1100
Chris Lattnerae319692006-10-25 03:49:28 +00001101 SmallVector<SourceLocation, 4> StringTokLocs;
1102 for (unsigned i = 0; i != StringToks.size(); ++i)
1103 StringTokLocs.push_back(StringToks[i].getLocation());
1104
Chris Lattnerd3e98952006-10-06 05:22:26 +00001105 // Hand this off to the Actions.
1106 ExprResult Res = Actions.ParseStringExpr(ResultBuf, ResultPtr-ResultBuf,
Chris Lattnerae319692006-10-25 03:49:28 +00001107 AnyWide, &StringTokLocs[0],
1108 StringTokLocs.size());
Chris Lattnerd3e98952006-10-06 05:22:26 +00001109
1110 // If either buffer was heap allocated, release it now.
1111 if (MaxTokenLength > 512) free(TokenBuf);
1112 if (SizeBound > 512) free(ResultBuf);
1113
1114 return Res;
1115}
1116