ParseExpr.cpp 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935
  1. //===--- ParseExpr.cpp - Expression Parsing -------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file was developed by Chris Lattner and is distributed under
  6. // the University of Illinois Open Source License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This file implements the Expression parsing implementation. 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
  16. // operator (e.g. '/') or a ternary operator ("?:"). The unary leaves are
  17. // handled by ParseCastExpression, the higher level pieces are handled by
  18. // ParseBinaryExpression.
  19. //
  20. //===----------------------------------------------------------------------===//
  21. #include "clang/Parse/Parser.h"
  22. #include "clang/Basic/Diagnostic.h"
  23. #include "llvm/ADT/SmallVector.h"
  24. #include "llvm/ADT/SmallString.h"
  25. using namespace clang;
  26. /// PrecedenceLevels - These are precedences for the binary/ternary operators in
  27. /// the C99 grammar. These have been named to relate with the C99 grammar
  28. /// productions. Low precedences numbers bind more weakly than high numbers.
  29. namespace prec {
  30. enum Level {
  31. Unknown = 0, // Not binary operator.
  32. Comma = 1, // ,
  33. Assignment = 2, // =, *=, /=, %=, +=, -=, <<=, >>=, &=, ^=, |=
  34. Conditional = 3, // ?
  35. LogicalOr = 4, // ||
  36. LogicalAnd = 5, // &&
  37. InclusiveOr = 6, // |
  38. ExclusiveOr = 7, // ^
  39. And = 8, // &
  40. Equality = 9, // ==, !=
  41. Relational = 10, // >=, <=, >, <
  42. Shift = 11, // <<, >>
  43. Additive = 12, // -, +
  44. Multiplicative = 13 // *, /, %
  45. };
  46. }
  47. /// getBinOpPrecedence - Return the precedence of the specified binary operator
  48. /// token. This returns:
  49. ///
  50. static prec::Level getBinOpPrecedence(tok::TokenKind Kind) {
  51. switch (Kind) {
  52. default: return prec::Unknown;
  53. case tok::comma: return prec::Comma;
  54. case tok::equal:
  55. case tok::starequal:
  56. case tok::slashequal:
  57. case tok::percentequal:
  58. case tok::plusequal:
  59. case tok::minusequal:
  60. case tok::lesslessequal:
  61. case tok::greatergreaterequal:
  62. case tok::ampequal:
  63. case tok::caretequal:
  64. case tok::pipeequal: return prec::Assignment;
  65. case tok::question: return prec::Conditional;
  66. case tok::pipepipe: return prec::LogicalOr;
  67. case tok::ampamp: return prec::LogicalAnd;
  68. case tok::pipe: return prec::InclusiveOr;
  69. case tok::caret: return prec::ExclusiveOr;
  70. case tok::amp: return prec::And;
  71. case tok::exclaimequal:
  72. case tok::equalequal: return prec::Equality;
  73. case tok::lessequal:
  74. case tok::less:
  75. case tok::greaterequal:
  76. case tok::greater: return prec::Relational;
  77. case tok::lessless:
  78. case tok::greatergreater: return prec::Shift;
  79. case tok::plus:
  80. case tok::minus: return prec::Additive;
  81. case tok::percent:
  82. case tok::slash:
  83. case tok::star: return prec::Multiplicative;
  84. }
  85. }
  86. /// ParseExpression - Simple precedence-based parser for binary/ternary
  87. /// operators.
  88. ///
  89. /// Note: we diverge from the C99 grammar when parsing the assignment-expression
  90. /// production. C99 specifies that the LHS of an assignment operator should be
  91. /// parsed as a unary-expression, but consistency dictates that it be a
  92. /// conditional-expession. In practice, the important thing here is that the
  93. /// LHS of an assignment has to be an l-value, which productions between
  94. /// unary-expression and conditional-expression don't produce. Because we want
  95. /// consistency, we parse the LHS as a conditional-expression, then check for
  96. /// l-value-ness in semantic analysis stages.
  97. ///
  98. /// multiplicative-expression: [C99 6.5.5]
  99. /// cast-expression
  100. /// multiplicative-expression '*' cast-expression
  101. /// multiplicative-expression '/' cast-expression
  102. /// multiplicative-expression '%' cast-expression
  103. ///
  104. /// additive-expression: [C99 6.5.6]
  105. /// multiplicative-expression
  106. /// additive-expression '+' multiplicative-expression
  107. /// additive-expression '-' multiplicative-expression
  108. ///
  109. /// shift-expression: [C99 6.5.7]
  110. /// additive-expression
  111. /// shift-expression '<<' additive-expression
  112. /// shift-expression '>>' additive-expression
  113. ///
  114. /// relational-expression: [C99 6.5.8]
  115. /// shift-expression
  116. /// relational-expression '<' shift-expression
  117. /// relational-expression '>' shift-expression
  118. /// relational-expression '<=' shift-expression
  119. /// relational-expression '>=' shift-expression
  120. ///
  121. /// equality-expression: [C99 6.5.9]
  122. /// relational-expression
  123. /// equality-expression '==' relational-expression
  124. /// equality-expression '!=' relational-expression
  125. ///
  126. /// AND-expression: [C99 6.5.10]
  127. /// equality-expression
  128. /// AND-expression '&' equality-expression
  129. ///
  130. /// exclusive-OR-expression: [C99 6.5.11]
  131. /// AND-expression
  132. /// exclusive-OR-expression '^' AND-expression
  133. ///
  134. /// inclusive-OR-expression: [C99 6.5.12]
  135. /// exclusive-OR-expression
  136. /// inclusive-OR-expression '|' exclusive-OR-expression
  137. ///
  138. /// logical-AND-expression: [C99 6.5.13]
  139. /// inclusive-OR-expression
  140. /// logical-AND-expression '&&' inclusive-OR-expression
  141. ///
  142. /// logical-OR-expression: [C99 6.5.14]
  143. /// logical-AND-expression
  144. /// logical-OR-expression '||' logical-AND-expression
  145. ///
  146. /// conditional-expression: [C99 6.5.15]
  147. /// logical-OR-expression
  148. /// logical-OR-expression '?' expression ':' conditional-expression
  149. /// [GNU] logical-OR-expression '?' ':' conditional-expression
  150. ///
  151. /// assignment-expression: [C99 6.5.16]
  152. /// conditional-expression
  153. /// unary-expression assignment-operator assignment-expression
  154. ///
  155. /// assignment-operator: one of
  156. /// = *= /= %= += -= <<= >>= &= ^= |=
  157. ///
  158. /// expression: [C99 6.5.17]
  159. /// assignment-expression
  160. /// expression ',' assignment-expression
  161. ///
  162. Parser::ExprResult Parser::ParseExpression() {
  163. ExprResult LHS = ParseCastExpression(false);
  164. if (LHS.isInvalid) return LHS;
  165. return ParseRHSOfBinaryExpression(LHS, prec::Comma);
  166. }
  167. /// ParseAssignmentExpression - Parse an expr that doesn't include commas.
  168. ///
  169. Parser::ExprResult Parser::ParseAssignmentExpression() {
  170. ExprResult LHS = ParseCastExpression(false);
  171. if (LHS.isInvalid) return LHS;
  172. return ParseRHSOfBinaryExpression(LHS, prec::Assignment);
  173. }
  174. Parser::ExprResult Parser::ParseConstantExpression() {
  175. ExprResult LHS = ParseCastExpression(false);
  176. if (LHS.isInvalid) return LHS;
  177. // TODO: Validate that this is a constant expr!
  178. return ParseRHSOfBinaryExpression(LHS, prec::Conditional);
  179. }
  180. /// ParseExpressionWithLeadingIdentifier - This special purpose method is used
  181. /// in contexts where we have already consumed an identifier (which we saved in
  182. /// 'IdTok'), then discovered that the identifier was really the leading token
  183. /// of part of an expression. For example, in "A[1]+B", we consumed "A" (which
  184. /// is now in 'IdTok') and the current token is "[".
  185. Parser::ExprResult Parser::
  186. ParseExpressionWithLeadingIdentifier(const LexerToken &IdTok) {
  187. // We know that 'IdTok' must correspond to this production:
  188. // primary-expression: identifier
  189. // Let the actions module handle the identifier.
  190. ExprResult Res = Actions.ParseIdentifierExpr(CurScope, IdTok.getLocation(),
  191. *IdTok.getIdentifierInfo(),
  192. Tok.getKind() == tok::l_paren);
  193. // Because we have to parse an entire cast-expression before starting the
  194. // ParseRHSOfBinaryExpression method (which parses any trailing binops), we
  195. // need to handle the 'postfix-expression' rules. We do this by invoking
  196. // ParsePostfixExpressionSuffix to consume any postfix-expression suffixes:
  197. Res = ParsePostfixExpressionSuffix(Res);
  198. if (Res.isInvalid) return Res;
  199. // At this point, the "A[1]" part of "A[1]+B" has been consumed. Once this is
  200. // done, we know we don't have to do anything for cast-expression, because the
  201. // only non-postfix-expression production starts with a '(' token, and we know
  202. // we have an identifier. As such, we can invoke ParseRHSOfBinaryExpression
  203. // to consume any trailing operators (e.g. "+" in this example) and connected
  204. // chunks of the expression.
  205. return ParseRHSOfBinaryExpression(Res, prec::Comma);
  206. }
  207. /// ParseExpressionWithLeadingIdentifier - This special purpose method is used
  208. /// in contexts where we have already consumed an identifier (which we saved in
  209. /// 'IdTok'), then discovered that the identifier was really the leading token
  210. /// of part of an assignment-expression. For example, in "A[1]+B", we consumed
  211. /// "A" (which is now in 'IdTok') and the current token is "[".
  212. Parser::ExprResult Parser::
  213. ParseAssignmentExprWithLeadingIdentifier(const LexerToken &IdTok) {
  214. // We know that 'IdTok' must correspond to this production:
  215. // primary-expression: identifier
  216. // Let the actions module handle the identifier.
  217. ExprResult Res = Actions.ParseIdentifierExpr(CurScope, IdTok.getLocation(),
  218. *IdTok.getIdentifierInfo(),
  219. Tok.getKind() == tok::l_paren);
  220. // Because we have to parse an entire cast-expression before starting the
  221. // ParseRHSOfBinaryExpression method (which parses any trailing binops), we
  222. // need to handle the 'postfix-expression' rules. We do this by invoking
  223. // ParsePostfixExpressionSuffix to consume any postfix-expression suffixes:
  224. Res = ParsePostfixExpressionSuffix(Res);
  225. if (Res.isInvalid) return Res;
  226. // At this point, the "A[1]" part of "A[1]+B" has been consumed. Once this is
  227. // done, we know we don't have to do anything for cast-expression, because the
  228. // only non-postfix-expression production starts with a '(' token, and we know
  229. // we have an identifier. As such, we can invoke ParseRHSOfBinaryExpression
  230. // to consume any trailing operators (e.g. "+" in this example) and connected
  231. // chunks of the expression.
  232. return ParseRHSOfBinaryExpression(Res, prec::Assignment);
  233. }
  234. /// ParseAssignmentExpressionWithLeadingStar - This special purpose method is
  235. /// used in contexts where we have already consumed a '*' (which we saved in
  236. /// 'StarTok'), then discovered that the '*' was really the leading token of an
  237. /// expression. For example, in "*(int*)P+B", we consumed "*" (which is
  238. /// now in 'StarTok') and the current token is "(".
  239. Parser::ExprResult Parser::
  240. ParseAssignmentExpressionWithLeadingStar(const LexerToken &StarTok) {
  241. // We know that 'StarTok' must correspond to this production:
  242. // unary-expression: unary-operator cast-expression
  243. // where 'unary-operator' is '*'.
  244. // Parse the cast-expression that follows the '*'. This will parse the
  245. // "*(int*)P" part of "*(int*)P+B".
  246. ExprResult Res = ParseCastExpression(false);
  247. if (Res.isInvalid) return Res;
  248. // Combine StarTok + Res to get the new AST for the combined expression..
  249. Res = Actions.ParseUnaryOp(StarTok.getLocation(), tok::star, Res.Val);
  250. if (Res.isInvalid) return Res;
  251. // We have to parse an entire cast-expression before starting the
  252. // ParseRHSOfBinaryExpression method (which parses any trailing binops). Since
  253. // we know that the only production above us is the cast-expression
  254. // production, and because the only alternative productions start with a '('
  255. // token (we know we had a '*'), there is no work to do to get a whole
  256. // cast-expression.
  257. // At this point, the "*(int*)P" part of "*(int*)P+B" has been consumed. Once
  258. // this is done, we can invoke ParseRHSOfBinaryExpression to consume any
  259. // trailing operators (e.g. "+" in this example) and connected chunks of the
  260. // assignment-expression.
  261. return ParseRHSOfBinaryExpression(Res, prec::Assignment);
  262. }
  263. /// ParseRHSOfBinaryExpression - Parse a binary expression that starts with
  264. /// LHS and has a precedence of at least MinPrec.
  265. Parser::ExprResult
  266. Parser::ParseRHSOfBinaryExpression(ExprResult LHS, unsigned MinPrec) {
  267. unsigned NextTokPrec = getBinOpPrecedence(Tok.getKind());
  268. SourceLocation ColonLoc;
  269. while (1) {
  270. // If this token has a lower precedence than we are allowed to parse (e.g.
  271. // because we are called recursively, or because the token is not a binop),
  272. // then we are done!
  273. if (NextTokPrec < MinPrec)
  274. return LHS;
  275. // Consume the operator, saving the operator token for error reporting.
  276. LexerToken OpToken = Tok;
  277. ConsumeToken();
  278. // Special case handling for the ternary operator.
  279. ExprResult TernaryMiddle(true);
  280. if (NextTokPrec == prec::Conditional) {
  281. if (Tok.getKind() != tok::colon) {
  282. // Handle this production specially:
  283. // logical-OR-expression '?' expression ':' conditional-expression
  284. // In particular, the RHS of the '?' is 'expression', not
  285. // 'logical-OR-expression' as we might expect.
  286. TernaryMiddle = ParseExpression();
  287. if (TernaryMiddle.isInvalid) return TernaryMiddle;
  288. } else {
  289. // Special case handling of "X ? Y : Z" where Y is empty:
  290. // logical-OR-expression '?' ':' conditional-expression [GNU]
  291. TernaryMiddle = ExprResult(false);
  292. Diag(Tok, diag::ext_gnu_conditional_expr);
  293. }
  294. if (Tok.getKind() != tok::colon) {
  295. Diag(Tok, diag::err_expected_colon);
  296. Diag(OpToken, diag::err_matching, "?");
  297. return ExprResult(true);
  298. }
  299. // Eat the colon.
  300. ColonLoc = ConsumeToken();
  301. }
  302. // Parse another leaf here for the RHS of the operator.
  303. ExprResult RHS = ParseCastExpression(false);
  304. if (RHS.isInvalid) return RHS;
  305. // Remember the precedence of this operator and get the precedence of the
  306. // operator immediately to the right of the RHS.
  307. unsigned ThisPrec = NextTokPrec;
  308. NextTokPrec = getBinOpPrecedence(Tok.getKind());
  309. // Assignment and conditional expressions are right-associative.
  310. bool isRightAssoc = NextTokPrec == prec::Conditional ||
  311. NextTokPrec == prec::Assignment;
  312. // Get the precedence of the operator to the right of the RHS. If it binds
  313. // more tightly with RHS than we do, evaluate it completely first.
  314. if (ThisPrec < NextTokPrec ||
  315. (ThisPrec == NextTokPrec && isRightAssoc)) {
  316. // If this is left-associative, only parse things on the RHS that bind
  317. // more tightly than the current operator. If it is left-associative, it
  318. // is okay, to bind exactly as tightly. For example, compile A=B=C=D as
  319. // A=(B=(C=D)), where each paren is a level of recursion here.
  320. RHS = ParseRHSOfBinaryExpression(RHS, ThisPrec + !isRightAssoc);
  321. if (RHS.isInvalid) return RHS;
  322. NextTokPrec = getBinOpPrecedence(Tok.getKind());
  323. }
  324. assert(NextTokPrec <= ThisPrec && "Recursion didn't work!");
  325. // Combine the LHS and RHS into the LHS (e.g. build AST).
  326. if (TernaryMiddle.isInvalid)
  327. LHS = Actions.ParseBinOp(OpToken.getLocation(), OpToken.getKind(),
  328. LHS.Val, RHS.Val);
  329. else
  330. LHS = Actions.ParseConditionalOp(OpToken.getLocation(), ColonLoc,
  331. LHS.Val, TernaryMiddle.Val, RHS.Val);
  332. }
  333. }
  334. /// ParseCastExpression - Parse a cast-expression, or, if isUnaryExpression is
  335. /// true, parse a unary-expression.
  336. ///
  337. /// cast-expression: [C99 6.5.4]
  338. /// unary-expression
  339. /// '(' type-name ')' cast-expression
  340. ///
  341. /// unary-expression: [C99 6.5.3]
  342. /// postfix-expression
  343. /// '++' unary-expression
  344. /// '--' unary-expression
  345. /// unary-operator cast-expression
  346. /// 'sizeof' unary-expression
  347. /// 'sizeof' '(' type-name ')'
  348. /// [GNU] '__alignof' unary-expression
  349. /// [GNU] '__alignof' '(' type-name ')'
  350. /// [GNU] '&&' identifier
  351. ///
  352. /// unary-operator: one of
  353. /// '&' '*' '+' '-' '~' '!'
  354. /// [GNU] '__extension__' '__real' '__imag'
  355. ///
  356. /// primary-expression: [C99 6.5.1]
  357. /// identifier
  358. /// constant
  359. /// string-literal
  360. /// [C++] boolean-literal [C++ 2.13.5]
  361. /// '(' expression ')'
  362. /// '__func__' [C99 6.4.2.2]
  363. /// [GNU] '__FUNCTION__'
  364. /// [GNU] '__PRETTY_FUNCTION__'
  365. /// [GNU] '(' compound-statement ')'
  366. /// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
  367. /// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
  368. /// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
  369. /// assign-expr ')'
  370. /// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
  371. /// [OBC] '[' objc-receiver objc-message-args ']' [TODO]
  372. /// [OBC] '@selector' '(' objc-selector-arg ')' [TODO]
  373. /// [OBC] '@protocol' '(' identifier ')' [TODO]
  374. /// [OBC] '@encode' '(' type-name ')' [TODO]
  375. /// [OBC] objc-string-literal [TODO]
  376. /// [C++] 'const_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
  377. /// [C++] 'dynamic_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
  378. /// [C++] 'reinterpret_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
  379. /// [C++] 'static_cast' '<' type-name '>' '(' expression ')' [C++ 5.2p1]
  380. ///
  381. /// constant: [C99 6.4.4]
  382. /// integer-constant
  383. /// floating-constant
  384. /// enumeration-constant -> identifier
  385. /// character-constant
  386. ///
  387. Parser::ExprResult Parser::ParseCastExpression(bool isUnaryExpression) {
  388. ExprResult Res;
  389. tok::TokenKind SavedKind = Tok.getKind();
  390. // This handles all of cast-expression, unary-expression, postfix-expression,
  391. // and primary-expression. We handle them together like this for efficiency
  392. // and to simplify handling of an expression starting with a '(' token: which
  393. // may be one of a parenthesized expression, cast-expression, compound literal
  394. // expression, or statement expression.
  395. //
  396. // If the parsed tokens consist of a primary-expression, the cases below
  397. // call ParsePostfixExpressionSuffix to handle the postfix expression
  398. // suffixes. Cases that cannot be followed by postfix exprs should
  399. // return without invoking ParsePostfixExpressionSuffix.
  400. switch (SavedKind) {
  401. case tok::l_paren: {
  402. // If this expression is limited to being a unary-expression, the parent can
  403. // not start a cast expression.
  404. ParenParseOption ParenExprType =
  405. isUnaryExpression ? CompoundLiteral : CastExpr;
  406. TypeTy *CastTy;
  407. SourceLocation LParenLoc = Tok.getLocation();
  408. SourceLocation RParenLoc;
  409. Res = ParseParenExpression(ParenExprType, CastTy, RParenLoc);
  410. if (Res.isInvalid) return Res;
  411. switch (ParenExprType) {
  412. case SimpleExpr: break; // Nothing else to do.
  413. case CompoundStmt: break; // Nothing else to do.
  414. case CompoundLiteral:
  415. // We parsed '(' type-name ')' '{' ... '}'. If any suffixes of
  416. // postfix-expression exist, parse them now.
  417. break;
  418. case CastExpr:
  419. // We parsed '(' type-name ')' and the thing after it wasn't a '{'. Parse
  420. // the cast-expression that follows it next.
  421. // TODO: For cast expression with CastTy.
  422. Res = ParseCastExpression(false);
  423. if (!Res.isInvalid)
  424. Res = Actions.ParseCastExpr(LParenLoc, CastTy, RParenLoc, Res.Val);
  425. return Res;
  426. }
  427. // These can be followed by postfix-expr pieces.
  428. return ParsePostfixExpressionSuffix(Res);
  429. }
  430. // primary-expression
  431. case tok::numeric_constant:
  432. // constant: integer-constant
  433. // constant: floating-constant
  434. Res = Actions.ParseNumericConstant(Tok);
  435. ConsumeToken();
  436. // These can be followed by postfix-expr pieces.
  437. return ParsePostfixExpressionSuffix(Res);
  438. case tok::kw_true:
  439. case tok::kw_false:
  440. return ParseCXXBoolLiteral();
  441. case tok::identifier: { // primary-expression: identifier
  442. // constant: enumeration-constant
  443. // Consume the identifier so that we can see if it is followed by a '('.
  444. // Function designators are allowed to be undeclared (C99 6.5.1p2), so we
  445. // need to know whether or not this identifier is a function designator or
  446. // not.
  447. IdentifierInfo &II = *Tok.getIdentifierInfo();
  448. SourceLocation L = ConsumeToken();
  449. Res = Actions.ParseIdentifierExpr(CurScope, L, II,
  450. Tok.getKind() == tok::l_paren);
  451. // These can be followed by postfix-expr pieces.
  452. return ParsePostfixExpressionSuffix(Res);
  453. }
  454. case tok::char_constant: // constant: character-constant
  455. Res = Actions.ParseCharacterConstant(Tok);
  456. ConsumeToken();
  457. // These can be followed by postfix-expr pieces.
  458. return ParsePostfixExpressionSuffix(Res);
  459. case tok::kw___func__: // primary-expression: __func__ [C99 6.4.2.2]
  460. case tok::kw___FUNCTION__: // primary-expression: __FUNCTION__ [GNU]
  461. case tok::kw___PRETTY_FUNCTION__: // primary-expression: __P..Y_F..N__ [GNU]
  462. Res = Actions.ParseSimplePrimaryExpr(Tok.getLocation(), SavedKind);
  463. ConsumeToken();
  464. // These can be followed by postfix-expr pieces.
  465. return ParsePostfixExpressionSuffix(Res);
  466. case tok::string_literal: // primary-expression: string-literal
  467. case tok::wide_string_literal:
  468. Res = ParseStringLiteralExpression();
  469. if (Res.isInvalid) return Res;
  470. // This can be followed by postfix-expr pieces (e.g. "foo"[1]).
  471. return ParsePostfixExpressionSuffix(Res);
  472. case tok::kw___builtin_va_arg:
  473. case tok::kw___builtin_offsetof:
  474. case tok::kw___builtin_choose_expr:
  475. case tok::kw___builtin_types_compatible_p:
  476. return ParseBuiltinPrimaryExpression();
  477. case tok::plusplus: // unary-expression: '++' unary-expression
  478. case tok::minusminus: { // unary-expression: '--' unary-expression
  479. SourceLocation SavedLoc = ConsumeToken();
  480. Res = ParseCastExpression(true);
  481. if (!Res.isInvalid)
  482. Res = Actions.ParseUnaryOp(SavedLoc, SavedKind, Res.Val);
  483. return Res;
  484. }
  485. case tok::amp: // unary-expression: '&' cast-expression
  486. case tok::star: // unary-expression: '*' cast-expression
  487. case tok::plus: // unary-expression: '+' cast-expression
  488. case tok::minus: // unary-expression: '-' cast-expression
  489. case tok::tilde: // unary-expression: '~' cast-expression
  490. case tok::exclaim: // unary-expression: '!' cast-expression
  491. case tok::kw___real: // unary-expression: '__real' cast-expression [GNU]
  492. case tok::kw___imag: // unary-expression: '__imag' cast-expression [GNU]
  493. case tok::kw___extension__:{//unary-expression:'__extension__' cast-expr [GNU]
  494. // FIXME: Extension not handled correctly here!
  495. SourceLocation SavedLoc = ConsumeToken();
  496. Res = ParseCastExpression(false);
  497. if (!Res.isInvalid)
  498. Res = Actions.ParseUnaryOp(SavedLoc, SavedKind, Res.Val);
  499. return Res;
  500. }
  501. case tok::kw_sizeof: // unary-expression: 'sizeof' unary-expression
  502. // unary-expression: 'sizeof' '(' type-name ')'
  503. case tok::kw___alignof: // unary-expression: '__alignof' unary-expression
  504. // unary-expression: '__alignof' '(' type-name ')'
  505. return ParseSizeofAlignofExpression();
  506. case tok::ampamp: { // unary-expression: '&&' identifier
  507. SourceLocation AmpAmpLoc = ConsumeToken();
  508. if (Tok.getKind() != tok::identifier) {
  509. Diag(Tok, diag::err_expected_ident);
  510. return ExprResult(true);
  511. }
  512. Diag(AmpAmpLoc, diag::ext_gnu_address_of_label);
  513. Res = Actions.ParseAddrLabel(AmpAmpLoc, Tok.getLocation(),
  514. Tok.getIdentifierInfo());
  515. ConsumeToken();
  516. return Res;
  517. }
  518. case tok::kw_const_cast:
  519. case tok::kw_dynamic_cast:
  520. case tok::kw_reinterpret_cast:
  521. case tok::kw_static_cast:
  522. return ParseCXXCasts();
  523. default:
  524. Diag(Tok, diag::err_expected_expression);
  525. return ExprResult(true);
  526. }
  527. // unreachable.
  528. abort();
  529. }
  530. /// ParsePostfixExpressionSuffix - Once the leading part of a postfix-expression
  531. /// is parsed, this method parses any suffixes that apply.
  532. ///
  533. /// postfix-expression: [C99 6.5.2]
  534. /// primary-expression
  535. /// postfix-expression '[' expression ']'
  536. /// postfix-expression '(' argument-expression-list[opt] ')'
  537. /// postfix-expression '.' identifier
  538. /// postfix-expression '->' identifier
  539. /// postfix-expression '++'
  540. /// postfix-expression '--'
  541. /// '(' type-name ')' '{' initializer-list '}'
  542. /// '(' type-name ')' '{' initializer-list ',' '}'
  543. ///
  544. /// argument-expression-list: [C99 6.5.2]
  545. /// argument-expression
  546. /// argument-expression-list ',' assignment-expression
  547. ///
  548. Parser::ExprResult Parser::ParsePostfixExpressionSuffix(ExprResult LHS) {
  549. // Now that the primary-expression piece of the postfix-expression has been
  550. // parsed, see if there are any postfix-expression pieces here.
  551. SourceLocation Loc;
  552. while (1) {
  553. switch (Tok.getKind()) {
  554. default: // Not a postfix-expression suffix.
  555. return LHS;
  556. case tok::l_square: { // postfix-expression: p-e '[' expression ']'
  557. Loc = ConsumeBracket();
  558. ExprResult Idx = ParseExpression();
  559. SourceLocation RLoc = Tok.getLocation();
  560. if (!LHS.isInvalid && !Idx.isInvalid && Tok.getKind() == tok::r_square)
  561. LHS = Actions.ParseArraySubscriptExpr(LHS.Val, Loc, Idx.Val, RLoc);
  562. else
  563. LHS = ExprResult(true);
  564. // Match the ']'.
  565. MatchRHSPunctuation(tok::r_square, Loc);
  566. break;
  567. }
  568. case tok::l_paren: { // p-e: p-e '(' argument-expression-list[opt] ')'
  569. llvm::SmallVector<ExprTy*, 8> ArgExprs;
  570. llvm::SmallVector<SourceLocation, 8> CommaLocs;
  571. bool ArgExprsOk = true;
  572. Loc = ConsumeParen();
  573. if (Tok.getKind() != tok::r_paren) {
  574. while (1) {
  575. ExprResult ArgExpr = ParseAssignmentExpression();
  576. if (ArgExpr.isInvalid) {
  577. ArgExprsOk = false;
  578. SkipUntil(tok::r_paren);
  579. break;
  580. } else
  581. ArgExprs.push_back(ArgExpr.Val);
  582. if (Tok.getKind() != tok::comma)
  583. break;
  584. // Move to the next argument, remember where the comma was.
  585. CommaLocs.push_back(ConsumeToken());
  586. }
  587. }
  588. // Match the ')'.
  589. if (!LHS.isInvalid && ArgExprsOk && Tok.getKind() == tok::r_paren) {
  590. assert((ArgExprs.size() == 0 || ArgExprs.size()-1 == CommaLocs.size())&&
  591. "Unexpected number of commas!");
  592. LHS = Actions.ParseCallExpr(LHS.Val, Loc, &ArgExprs[0], ArgExprs.size(),
  593. &CommaLocs[0], Tok.getLocation());
  594. }
  595. if (ArgExprsOk)
  596. MatchRHSPunctuation(tok::r_paren, Loc);
  597. break;
  598. }
  599. case tok::arrow: // postfix-expression: p-e '->' identifier
  600. case tok::period: { // postfix-expression: p-e '.' identifier
  601. tok::TokenKind OpKind = Tok.getKind();
  602. SourceLocation OpLoc = ConsumeToken(); // Eat the "." or "->" token.
  603. if (Tok.getKind() != tok::identifier) {
  604. Diag(Tok, diag::err_expected_ident);
  605. return ExprResult(true);
  606. }
  607. if (!LHS.isInvalid)
  608. LHS = Actions.ParseMemberReferenceExpr(LHS.Val, OpLoc, OpKind,
  609. Tok.getLocation(),
  610. *Tok.getIdentifierInfo());
  611. ConsumeToken();
  612. break;
  613. }
  614. case tok::plusplus: // postfix-expression: postfix-expression '++'
  615. case tok::minusminus: // postfix-expression: postfix-expression '--'
  616. if (!LHS.isInvalid)
  617. LHS = Actions.ParsePostfixUnaryOp(Tok.getLocation(), Tok.getKind(),
  618. LHS.Val);
  619. ConsumeToken();
  620. break;
  621. }
  622. }
  623. }
  624. /// ParseSizeofAlignofExpression - Parse a sizeof or alignof expression.
  625. /// unary-expression: [C99 6.5.3]
  626. /// 'sizeof' unary-expression
  627. /// 'sizeof' '(' type-name ')'
  628. /// [GNU] '__alignof' unary-expression
  629. /// [GNU] '__alignof' '(' type-name ')'
  630. Parser::ExprResult Parser::ParseSizeofAlignofExpression() {
  631. assert((Tok.getKind() == tok::kw_sizeof ||
  632. Tok.getKind() == tok::kw___alignof) &&
  633. "Not a sizeof/alignof expression!");
  634. LexerToken OpTok = Tok;
  635. ConsumeToken();
  636. // If the operand doesn't start with an '(', it must be an expression.
  637. ExprResult Operand;
  638. if (Tok.getKind() != tok::l_paren) {
  639. Operand = ParseCastExpression(true);
  640. } else {
  641. // If it starts with a '(', we know that it is either a parenthesized
  642. // type-name, or it is a unary-expression that starts with a compound
  643. // literal, or starts with a primary-expression that is a parenthesized
  644. // expression.
  645. ParenParseOption ExprType = CastExpr;
  646. TypeTy *CastTy;
  647. SourceLocation LParenLoc = Tok.getLocation(), RParenLoc;
  648. Operand = ParseParenExpression(ExprType, CastTy, RParenLoc);
  649. // If ParseParenExpression parsed a '(typename)' sequence only, the this is
  650. // sizeof/alignof a type. Otherwise, it is sizeof/alignof an expression.
  651. if (ExprType == CastExpr) {
  652. return Actions.ParseSizeOfAlignOfTypeExpr(OpTok.getLocation(),
  653. OpTok.getKind() == tok::kw_sizeof,
  654. LParenLoc, CastTy, RParenLoc);
  655. }
  656. }
  657. // If we get here, the operand to the sizeof/alignof was an expresion.
  658. if (!Operand.isInvalid)
  659. Operand = Actions.ParseUnaryOp(OpTok.getLocation(), OpTok.getKind(),
  660. Operand.Val);
  661. return Operand;
  662. }
  663. /// ParseBuiltinPrimaryExpression
  664. ///
  665. /// primary-expression: [C99 6.5.1]
  666. /// [GNU] '__builtin_va_arg' '(' assignment-expression ',' type-name ')'
  667. /// [GNU] '__builtin_offsetof' '(' type-name ',' offsetof-member-designator')'
  668. /// [GNU] '__builtin_choose_expr' '(' assign-expr ',' assign-expr ','
  669. /// assign-expr ')'
  670. /// [GNU] '__builtin_types_compatible_p' '(' type-name ',' type-name ')'
  671. ///
  672. /// [GNU] offsetof-member-designator:
  673. /// [GNU] identifier
  674. /// [GNU] offsetof-member-designator '.' identifier
  675. /// [GNU] offsetof-member-designator '[' expression ']'
  676. ///
  677. Parser::ExprResult Parser::ParseBuiltinPrimaryExpression() {
  678. ExprResult Res(false);
  679. const IdentifierInfo *BuiltinII = Tok.getIdentifierInfo();
  680. tok::TokenKind T = Tok.getKind();
  681. SourceLocation StartLoc = ConsumeToken(); // Eat the builtin identifier.
  682. // All of these start with an open paren.
  683. if (Tok.getKind() != tok::l_paren) {
  684. Diag(Tok, diag::err_expected_lparen_after, BuiltinII->getName());
  685. return ExprResult(true);
  686. }
  687. SourceLocation LParenLoc = ConsumeParen();
  688. // TODO: Build AST.
  689. switch (T) {
  690. default: assert(0 && "Not a builtin primary expression!");
  691. case tok::kw___builtin_va_arg:
  692. Res = ParseAssignmentExpression();
  693. if (Res.isInvalid) {
  694. SkipUntil(tok::r_paren);
  695. return Res;
  696. }
  697. if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
  698. return ExprResult(true);
  699. ParseTypeName();
  700. break;
  701. case tok::kw___builtin_offsetof:
  702. ParseTypeName();
  703. if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
  704. return ExprResult(true);
  705. // We must have at least one identifier here.
  706. if (ExpectAndConsume(tok::identifier, diag::err_expected_ident, "",
  707. tok::r_paren))
  708. return ExprResult(true);
  709. while (1) {
  710. if (Tok.getKind() == tok::period) {
  711. // offsetof-member-designator: offsetof-member-designator '.' identifier
  712. ConsumeToken();
  713. if (ExpectAndConsume(tok::identifier, diag::err_expected_ident, "",
  714. tok::r_paren))
  715. return ExprResult(true);
  716. } else if (Tok.getKind() == tok::l_square) {
  717. // offsetof-member-designator: offsetof-member-design '[' expression ']'
  718. SourceLocation LSquareLoc = ConsumeBracket();
  719. Res = ParseExpression();
  720. if (Res.isInvalid) {
  721. SkipUntil(tok::r_paren);
  722. return Res;
  723. }
  724. MatchRHSPunctuation(tok::r_square, LSquareLoc);
  725. } else {
  726. break;
  727. }
  728. }
  729. break;
  730. case tok::kw___builtin_choose_expr:
  731. Res = ParseAssignmentExpression();
  732. if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
  733. return ExprResult(true);
  734. Res = ParseAssignmentExpression();
  735. if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
  736. return ExprResult(true);
  737. Res = ParseAssignmentExpression();
  738. break;
  739. case tok::kw___builtin_types_compatible_p:
  740. ParseTypeName();
  741. if (ExpectAndConsume(tok::comma, diag::err_expected_comma, "",tok::r_paren))
  742. return ExprResult(true);
  743. ParseTypeName();
  744. break;
  745. }
  746. MatchRHSPunctuation(tok::r_paren, LParenLoc);
  747. // These can be followed by postfix-expr pieces because they are
  748. // primary-expressions.
  749. return ParsePostfixExpressionSuffix(Res);
  750. }
  751. /// ParseParenExpression - This parses the unit that starts with a '(' token,
  752. /// based on what is allowed by ExprType. The actual thing parsed is returned
  753. /// in ExprType.
  754. ///
  755. /// primary-expression: [C99 6.5.1]
  756. /// '(' expression ')'
  757. /// [GNU] '(' compound-statement ')' (if !ParenExprOnly)
  758. /// postfix-expression: [C99 6.5.2]
  759. /// '(' type-name ')' '{' initializer-list '}'
  760. /// '(' type-name ')' '{' initializer-list ',' '}'
  761. /// cast-expression: [C99 6.5.4]
  762. /// '(' type-name ')' cast-expression
  763. ///
  764. Parser::ExprResult Parser::ParseParenExpression(ParenParseOption &ExprType,
  765. TypeTy *&CastTy,
  766. SourceLocation &RParenLoc) {
  767. assert(Tok.getKind() == tok::l_paren && "Not a paren expr!");
  768. SourceLocation OpenLoc = ConsumeParen();
  769. ExprResult Result(false);
  770. CastTy = 0;
  771. if (ExprType >= CompoundStmt && Tok.getKind() == tok::l_brace &&
  772. !getLang().NoExtensions) {
  773. Diag(Tok, diag::ext_gnu_statement_expr);
  774. ParseCompoundStatement();
  775. ExprType = CompoundStmt;
  776. // TODO: Build AST for GNU compound stmt.
  777. } else if (ExprType >= CompoundLiteral && isTypeSpecifierQualifier()) {
  778. // Otherwise, this is a compound literal expression or cast expression.
  779. TypeTy *Ty = ParseTypeName();
  780. // Match the ')'.
  781. if (Tok.getKind() == tok::r_paren)
  782. RParenLoc = ConsumeParen();
  783. else
  784. MatchRHSPunctuation(tok::r_paren, OpenLoc);
  785. if (Tok.getKind() == tok::l_brace) {
  786. if (!getLang().C99) // Compound literals don't exist in C90.
  787. Diag(OpenLoc, diag::ext_c99_compound_literal);
  788. Result = ParseInitializer();
  789. ExprType = CompoundLiteral;
  790. // TODO: Build AST for compound literal.
  791. } else if (ExprType == CastExpr) {
  792. // Note that this doesn't parse the subsequence cast-expression, it just
  793. // returns the parsed type to the callee.
  794. ExprType = CastExpr;
  795. CastTy = Ty;
  796. return ExprResult(false);
  797. } else {
  798. Diag(Tok, diag::err_expected_lbrace_in_compound_literal);
  799. return ExprResult(true);
  800. }
  801. return Result;
  802. } else {
  803. Result = ParseExpression();
  804. ExprType = SimpleExpr;
  805. if (!Result.isInvalid && Tok.getKind() == tok::r_paren)
  806. Result = Actions.ParseParenExpr(OpenLoc, Tok.getLocation(), Result.Val);
  807. }
  808. // Match the ')'.
  809. if (Result.isInvalid)
  810. SkipUntil(tok::r_paren);
  811. else {
  812. if (Tok.getKind() == tok::r_paren)
  813. RParenLoc = ConsumeParen();
  814. else
  815. MatchRHSPunctuation(tok::r_paren, OpenLoc);
  816. }
  817. return Result;
  818. }
  819. /// ParseStringLiteralExpression - This handles the various token types that
  820. /// form string literals, and also handles string concatenation [C99 5.1.1.2,
  821. /// translation phase #6].
  822. ///
  823. /// primary-expression: [C99 6.5.1]
  824. /// string-literal
  825. Parser::ExprResult Parser::ParseStringLiteralExpression() {
  826. assert(isTokenStringLiteral() && "Not a string literal!");
  827. // String concat. Note that keywords like __func__ and __FUNCTION__ are not
  828. // considered to be strings for concatenation purposes.
  829. llvm::SmallVector<LexerToken, 4> StringToks;
  830. do {
  831. StringToks.push_back(Tok);
  832. ConsumeStringToken();
  833. } while (isTokenStringLiteral());
  834. // Pass the set of string tokens, ready for concatenation, to the actions.
  835. return Actions.ParseStringLiteral(&StringToks[0], StringToks.size());
  836. }