blob: 4258d8b8a638a8cfb9903857ead52ce2fbac6873 [file] [log] [blame]
Chris Lattner22eb9722006-06-18 05:43:12 +00001//===--- PPExpressions.cpp - Preprocessor Expression Evaluation -----------===//
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 Lattner80965422006-07-04 18:03:19 +000010// This file implements the Preprocessor::EvaluateDirectiveExpression method,
11// which parses and evaluates integer constant expressions for #if directives.
Chris Lattner22eb9722006-06-18 05:43:12 +000012//
13//===----------------------------------------------------------------------===//
14//
Chris Lattner6df79752007-04-04 06:54:19 +000015// FIXME: implement testing for #assert's.
Chris Lattnera9eac7f2007-04-05 05:24:00 +000016// FIXME: Detect and report overflow in expression (e.g. (1 << 62)*2)
Chris Lattner22eb9722006-06-18 05:43:12 +000017//
18//===----------------------------------------------------------------------===//
19
20#include "clang/Lex/Preprocessor.h"
Chris Lattnera78a97e2006-07-03 05:42:18 +000021#include "clang/Lex/MacroInfo.h"
Steve Naroff451d8f162007-03-12 23:22:38 +000022#include "clang/Lex/LiteralSupport.h"
Chris Lattner81278c62006-10-14 19:03:49 +000023#include "clang/Basic/TargetInfo.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000024#include "clang/Basic/TokenKinds.h"
25#include "clang/Basic/Diagnostic.h"
Chris Lattnera9eac7f2007-04-05 05:24:00 +000026#include "llvm/ADT/APSInt.h"
Steve Naroff451d8f162007-03-12 23:22:38 +000027#include "llvm/ADT/SmallString.h"
Chris Lattner22eb9722006-06-18 05:43:12 +000028using namespace llvm;
29using namespace clang;
30
Chris Lattnera9eac7f2007-04-05 05:24:00 +000031static bool EvaluateDirectiveSubExpr(APSInt &LHS, unsigned MinPrec,
Chris Lattnere3519cc2006-07-04 18:11:39 +000032 LexerToken &PeekTok, Preprocessor &PP);
Chris Lattner22eb9722006-06-18 05:43:12 +000033
Chris Lattnerb9d90f72006-07-04 18:32:03 +000034/// DefinedTracker - This struct is used while parsing expressions to keep track
35/// of whether !defined(X) has been seen.
36///
37/// With this simple scheme, we handle the basic forms:
38/// !defined(X) and !defined X
39/// but we also trivially handle (silly) stuff like:
40/// !!!defined(X) and +!defined(X) and !+!+!defined(X) and !(defined(X)).
41struct DefinedTracker {
42 /// Each time a Value is evaluated, it returns information about whether the
43 /// parsed value is of the form defined(X), !defined(X) or is something else.
44 enum TrackerState {
45 DefinedMacro, // defined(X)
46 NotDefinedMacro, // !defined(X)
47 Unknown // Something else.
48 } State;
49 /// TheMacro - When the state is DefinedMacro or NotDefinedMacro, this
50 /// indicates the macro that was checked.
51 IdentifierInfo *TheMacro;
52};
53
54
Chris Lattner22eb9722006-06-18 05:43:12 +000055
56/// EvaluateValue - Evaluate the token PeekTok (and any others needed) and
57/// return the computed value in Result. Return true if there was an error
Chris Lattnerb9d90f72006-07-04 18:32:03 +000058/// parsing. This function also returns information about the form of the
59/// expression in DT. See above for information on what DT means.
Chris Lattnera9eac7f2007-04-05 05:24:00 +000060static bool EvaluateValue(APSInt &Result, LexerToken &PeekTok,
Chris Lattnerce5dc8a2007-04-04 06:46:55 +000061 DefinedTracker &DT, Preprocessor &PP) {
Chris Lattner22eb9722006-06-18 05:43:12 +000062 Result = 0;
Chris Lattnerb9d90f72006-07-04 18:32:03 +000063 DT.State = DefinedTracker::Unknown;
Chris Lattner22eb9722006-06-18 05:43:12 +000064
65 // If this token's spelling is a pp-identifier, check to see if it is
66 // 'defined' or if it is a macro. Note that we check here because many
67 // keywords are pp-identifiers, so we can't check the kind.
Chris Lattnerb9d90f72006-07-04 18:32:03 +000068 if (IdentifierInfo *II = PeekTok.getIdentifierInfo()) {
Chris Lattner22eb9722006-06-18 05:43:12 +000069 // If this identifier isn't 'defined' and it wasn't macro expanded, it turns
70 // into a simple 0.
Chris Lattner2bb8a952006-11-21 22:24:17 +000071 if (II->getPPKeywordID() != tok::pp_defined) {
Chris Lattner22eb9722006-06-18 05:43:12 +000072 Result = 0;
Chris Lattnera9eac7f2007-04-05 05:24:00 +000073 Result.setIsUnsigned(false); // "0" is signed intmax_t 0.
Chris Lattnerbcb416b2006-10-27 05:43:50 +000074 PP.LexNonComment(PeekTok);
Chris Lattnercb283342006-06-18 06:48:37 +000075 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +000076 }
77
78 // Handle "defined X" and "defined(X)".
Chris Lattner22eb9722006-06-18 05:43:12 +000079
Chris Lattnere3519cc2006-07-04 18:11:39 +000080 // Get the next token, don't expand it.
81 PP.LexUnexpandedToken(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +000082
83 // Two options, it can either be a pp-identifier or a (.
84 bool InParens = false;
85 if (PeekTok.getKind() == tok::l_paren) {
86 // Found a paren, remember we saw it and skip it.
87 InParens = true;
Chris Lattnere3519cc2006-07-04 18:11:39 +000088 PP.LexUnexpandedToken(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +000089 }
90
91 // If we don't have a pp-identifier now, this is an error.
92 if ((II = PeekTok.getIdentifierInfo()) == 0) {
Chris Lattnere3519cc2006-07-04 18:11:39 +000093 PP.Diag(PeekTok, diag::err_pp_defined_requires_identifier);
Chris Lattner22eb9722006-06-18 05:43:12 +000094 return true;
95 }
96
97 // Otherwise, we got an identifier, is it defined to something?
98 Result = II->getMacroInfo() != 0;
Chris Lattnera9eac7f2007-04-05 05:24:00 +000099 Result.setIsUnsigned(false); // Result is signed intmax_t.
100
Chris Lattnera78a97e2006-07-03 05:42:18 +0000101 // If there is a macro, mark it used.
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000102 if (Result != 0) {
Chris Lattner81278c62006-10-14 19:03:49 +0000103 II->getMacroInfo()->setIsUsed(true);
104
105 // If this is the first use of a target-specific macro, warn about it.
106 if (II->getMacroInfo()->isTargetSpecific()) {
107 // Don't warn on second use.
108 II->getMacroInfo()->setIsTargetSpecific(false);
109 PP.getTargetInfo().DiagnoseNonPortability(PeekTok.getLocation(),
110 diag::port_target_macro_use);
111 }
Chris Lattner063400e2006-10-14 19:54:15 +0000112 } else {
113 // Use of a target-specific macro for some other target? If so, warn.
114 if (II->isOtherTargetMacro()) {
115 II->setIsOtherTargetMacro(false); // Don't warn on second use.
116 PP.getTargetInfo().DiagnoseNonPortability(PeekTok.getLocation(),
117 diag::port_target_macro_use);
118 }
Chris Lattner81278c62006-10-14 19:03:49 +0000119 }
Chris Lattner22eb9722006-06-18 05:43:12 +0000120
121 // Consume identifier.
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000122 PP.LexNonComment(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000123
124 // If we are in parens, ensure we have a trailing ).
125 if (InParens) {
126 if (PeekTok.getKind() != tok::r_paren) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000127 PP.Diag(PeekTok, diag::err_pp_missing_rparen);
Chris Lattner22eb9722006-06-18 05:43:12 +0000128 return true;
129 }
130 // Consume the ).
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000131 PP.LexNonComment(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000132 }
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000133
134 // Success, remember that we saw defined(X).
135 DT.State = DefinedTracker::DefinedMacro;
136 DT.TheMacro = II;
Chris Lattner22eb9722006-06-18 05:43:12 +0000137 return false;
138 }
139
140 switch (PeekTok.getKind()) {
141 default: // Non-value token.
Chris Lattnere3519cc2006-07-04 18:11:39 +0000142 PP.Diag(PeekTok, diag::err_pp_expr_bad_token);
Chris Lattner22eb9722006-06-18 05:43:12 +0000143 return true;
144 case tok::eom:
145 case tok::r_paren:
146 // If there is no expression, report and exit.
Chris Lattnere3519cc2006-07-04 18:11:39 +0000147 PP.Diag(PeekTok, diag::err_pp_expected_value_in_expr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000148 return true;
149 case tok::numeric_constant: {
Chris Lattner6df79752007-04-04 06:54:19 +0000150 SmallString<64> IntegerBuffer;
Steve Naroff451d8f162007-03-12 23:22:38 +0000151 IntegerBuffer.resize(PeekTok.getLength());
152 const char *ThisTokBegin = &IntegerBuffer[0];
153 unsigned ActualLength = PP.getSpelling(PeekTok, ThisTokBegin);
154 NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength,
155 PeekTok.getLocation(), PP);
Chris Lattner6df79752007-04-04 06:54:19 +0000156 if (Literal.hadError)
Steve Narofff2fb89e2007-03-13 20:29:44 +0000157 return true; // a diagnostic was already reported.
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000158
159 if (Literal.isFloatingLiteral()) {
Steve Naroff451d8f162007-03-12 23:22:38 +0000160 PP.Diag(PeekTok, diag::err_pp_illegal_floating_literal);
Steve Narofff2fb89e2007-03-13 20:29:44 +0000161 return true;
Steve Naroff451d8f162007-03-12 23:22:38 +0000162 }
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000163 assert(Literal.isIntegerLiteral() && "Unknown ppnumber");
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000164
165 // Parse the integer literal into Result.
166 if (Literal.GetIntegerValue(Result)) {
167 // Overflow parsing integer literal.
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000168 PP.Diag(PeekTok, diag::warn_integer_too_large);
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000169 Result.setIsUnsigned(true);
170 } else {
171 // Set the signedness of the result to match whether there was a U suffix
172 // or not.
173 Result.setIsUnsigned(Literal.isUnsigned);
174
175 // Detect overflow based on whether the value is signed. If signed
176 // and if the value is too large, emit a warning "integer constant is so
177 // large that it is unsigned" e.g. on 12345678901234567890 where intmax_t
178 // is 64-bits.
179 if (!Literal.isUnsigned && Result.isNegative()) {
180 PP.Diag(PeekTok, diag::warn_integer_too_large_for_signed);
181 Result.setIsUnsigned(true);
182 }
183 }
184
185 // Consume the token.
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000186 PP.LexNonComment(PeekTok);
187 return false;
Chris Lattner22eb9722006-06-18 05:43:12 +0000188 }
189 case tok::l_paren:
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000190 PP.LexNonComment(PeekTok); // Eat the (.
Chris Lattnercb283342006-06-18 06:48:37 +0000191 // Parse the value and if there are any binary operators involved, parse
192 // them.
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000193 if (EvaluateValue(Result, PeekTok, DT, PP)) return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000194
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000195 // If this is a silly value like (X), which doesn't need parens, check for
196 // !(defined X).
197 if (PeekTok.getKind() == tok::r_paren) {
198 // Just use DT unmodified as our result.
199 } else {
200 if (EvaluateDirectiveSubExpr(Result, 1, PeekTok, PP)) return true;
201
202 if (PeekTok.getKind() != tok::r_paren) {
203 PP.Diag(PeekTok, diag::err_pp_expected_rparen);
204 return true;
205 }
206 DT.State = DefinedTracker::Unknown;
Chris Lattner22eb9722006-06-18 05:43:12 +0000207 }
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000208 PP.LexNonComment(PeekTok); // Eat the ).
Chris Lattner22eb9722006-06-18 05:43:12 +0000209 return false;
210
211 case tok::plus:
212 // Unary plus doesn't modify the value.
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000213 PP.LexNonComment(PeekTok);
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000214 return EvaluateValue(Result, PeekTok, DT, PP);
Chris Lattner22eb9722006-06-18 05:43:12 +0000215 case tok::minus:
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000216 PP.LexNonComment(PeekTok);
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000217 if (EvaluateValue(Result, PeekTok, DT, PP)) return true;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000218 // C99 6.5.3.3p3: The sign of the result matches the sign of the operand.
Chris Lattner22eb9722006-06-18 05:43:12 +0000219 Result = -Result;
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000220 DT.State = DefinedTracker::Unknown;
Chris Lattner22eb9722006-06-18 05:43:12 +0000221 return false;
222
223 case tok::tilde:
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000224 PP.LexNonComment(PeekTok);
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000225 if (EvaluateValue(Result, PeekTok, DT, PP)) return true;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000226 // C99 6.5.3.3p4: The sign of the result matches the sign of the operand.
Chris Lattner22eb9722006-06-18 05:43:12 +0000227 Result = ~Result;
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000228 DT.State = DefinedTracker::Unknown;
Chris Lattner22eb9722006-06-18 05:43:12 +0000229 return false;
230
231 case tok::exclaim:
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000232 PP.LexNonComment(PeekTok);
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000233 if (EvaluateValue(Result, PeekTok, DT, PP)) return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000234 Result = !Result;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000235 // C99 6.5.3.3p5: The sign of the result is 'int', aka it is signed.
236 Result.setIsUnsigned(false);
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000237
238 if (DT.State == DefinedTracker::DefinedMacro)
239 DT.State = DefinedTracker::NotDefinedMacro;
240 else if (DT.State == DefinedTracker::NotDefinedMacro)
241 DT.State = DefinedTracker::DefinedMacro;
Chris Lattner22eb9722006-06-18 05:43:12 +0000242 return false;
243
244 // FIXME: Handle #assert
245 }
246}
247
248
249
250/// getPrecedence - Return the precedence of the specified binary operator
251/// token. This returns:
252/// ~0 - Invalid token.
Chris Lattner9916c5c2006-10-27 05:24:37 +0000253/// 14 - *,/,%
254/// 13 - -,+
255/// 12 - <<,>>
256/// 11 - >=, <=, >, <
257/// 10 - ==, !=
Chris Lattner22eb9722006-06-18 05:43:12 +0000258/// 9 - &
259/// 8 - ^
260/// 7 - |
261/// 6 - &&
262/// 5 - ||
263/// 4 - ?
264/// 3 - :
265/// 0 - eom, )
266static unsigned getPrecedence(tok::TokenKind Kind) {
267 switch (Kind) {
268 default: return ~0U;
269 case tok::percent:
270 case tok::slash:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000271 case tok::star: return 14;
Chris Lattner22eb9722006-06-18 05:43:12 +0000272 case tok::plus:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000273 case tok::minus: return 13;
Chris Lattner22eb9722006-06-18 05:43:12 +0000274 case tok::lessless:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000275 case tok::greatergreater: return 12;
Chris Lattner22eb9722006-06-18 05:43:12 +0000276 case tok::lessequal:
277 case tok::less:
278 case tok::greaterequal:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000279 case tok::greater: return 11;
Chris Lattner22eb9722006-06-18 05:43:12 +0000280 case tok::exclaimequal:
Chris Lattner9916c5c2006-10-27 05:24:37 +0000281 case tok::equalequal: return 10;
Chris Lattner22eb9722006-06-18 05:43:12 +0000282 case tok::amp: return 9;
283 case tok::caret: return 8;
284 case tok::pipe: return 7;
285 case tok::ampamp: return 6;
286 case tok::pipepipe: return 5;
287 case tok::question: return 4;
288 case tok::colon: return 3;
289 case tok::comma: return 2;
290 case tok::r_paren: return 0; // Lowest priority, end of expr.
291 case tok::eom: return 0; // Lowest priority, end of macro.
292 }
293}
294
295
296/// EvaluateDirectiveSubExpr - Evaluate the subexpression whose first token is
297/// PeekTok, and whose precedence is PeekPrec.
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000298static bool EvaluateDirectiveSubExpr(APSInt &LHS, unsigned MinPrec,
Chris Lattnere3519cc2006-07-04 18:11:39 +0000299 LexerToken &PeekTok, Preprocessor &PP) {
Chris Lattner22eb9722006-06-18 05:43:12 +0000300 unsigned PeekPrec = getPrecedence(PeekTok.getKind());
301 // If this token isn't valid, report the error.
302 if (PeekPrec == ~0U) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000303 PP.Diag(PeekTok, diag::err_pp_expr_bad_token);
Chris Lattner22eb9722006-06-18 05:43:12 +0000304 return true;
305 }
306
307 while (1) {
308 // If this token has a lower precedence than we are allowed to parse, return
309 // it so that higher levels of the recursion can parse it.
310 if (PeekPrec < MinPrec)
311 return false;
312
313 tok::TokenKind Operator = PeekTok.getKind();
314
315 // Consume the operator, saving the operator token for error reporting.
316 LexerToken OpToken = PeekTok;
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000317 PP.LexNonComment(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000318
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000319 APSInt RHS(LHS.getBitWidth());
Chris Lattner22eb9722006-06-18 05:43:12 +0000320 // Parse the RHS of the operator.
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000321 DefinedTracker DT;
322 if (EvaluateValue(RHS, PeekTok, DT, PP)) return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000323
324 // Remember the precedence of this operator and get the precedence of the
325 // operator immediately to the right of the RHS.
326 unsigned ThisPrec = PeekPrec;
327 PeekPrec = getPrecedence(PeekTok.getKind());
328
329 // If this token isn't valid, report the error.
330 if (PeekPrec == ~0U) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000331 PP.Diag(PeekTok, diag::err_pp_expr_bad_token);
Chris Lattner22eb9722006-06-18 05:43:12 +0000332 return true;
333 }
334
335 bool isRightAssoc = Operator == tok::question;
336
337 // Get the precedence of the operator to the right of the RHS. If it binds
338 // more tightly with RHS than we do, evaluate it completely first.
339 if (ThisPrec < PeekPrec ||
340 (ThisPrec == PeekPrec && isRightAssoc)) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000341 if (EvaluateDirectiveSubExpr(RHS, ThisPrec+1, PeekTok, PP))
Chris Lattner22eb9722006-06-18 05:43:12 +0000342 return true;
343 PeekPrec = getPrecedence(PeekTok.getKind());
344 }
345 assert(PeekPrec <= ThisPrec && "Recursion didn't work!");
346
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000347 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
348 // either operand is unsigned. Don't do this for x and y in "x ? y : z".
349 if (Operator != tok::question) {
350 if (RHS.isUnsigned()) LHS.setIsUnsigned(true);
351 RHS.setIsUnsigned(LHS.isUnsigned());
352 }
353
354 // FIXME: All of these should detect and report overflow??
Chris Lattner22eb9722006-06-18 05:43:12 +0000355 switch (Operator) {
356 default: assert(0 && "Unknown operator token!");
357 case tok::percent:
358 if (RHS == 0) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000359 PP.Diag(OpToken, diag::err_pp_remainder_by_zero);
Chris Lattner22eb9722006-06-18 05:43:12 +0000360 return true;
361 }
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000362 LHS %= RHS;
Chris Lattner22eb9722006-06-18 05:43:12 +0000363 break;
364 case tok::slash:
365 if (RHS == 0) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000366 PP.Diag(OpToken, diag::err_pp_division_by_zero);
Chris Lattner22eb9722006-06-18 05:43:12 +0000367 return true;
368 }
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000369 LHS /= RHS;
Chris Lattner22eb9722006-06-18 05:43:12 +0000370 break;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000371 case tok::star:
372 LHS *= RHS;
373 break;
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000374 case tok::lessless:
375 // FIXME: shift amt overflow?
376 // FIXME: Don't use getZExtValue.
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000377 LHS <<= RHS.getZExtValue();
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000378 break;
379 case tok::greatergreater:
380 // FIXME: signed vs unsigned
381 // FIXME: Don't use getZExtValue.
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000382 LHS >>= RHS.getZExtValue();
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000383 break;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000384 case tok::plus:
385 LHS += RHS;
386 break;
387 case tok::minus:
388 LHS -= RHS;
389 break;
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000390 case tok::lessequal:
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000391 LHS = LHS <= RHS;
392 LHS.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000393 break;
394 case tok::less:
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000395 LHS = LHS < RHS;
396 LHS.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000397 break;
398 case tok::greaterequal:
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000399 LHS = LHS >= RHS;
400 LHS.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000401 break;
402 case tok::greater:
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000403 LHS = LHS > RHS;
404 LHS.setIsUnsigned(false); // C99 6.5.8p6, result is always int (signed)
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000405 break;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000406 case tok::exclaimequal:
407 LHS = LHS != RHS;
408 LHS.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
409 break;
410 case tok::equalequal:
411 LHS = LHS == RHS;
412 LHS.setIsUnsigned(false); // C99 6.5.9p3, result is always int (signed)
413 break;
Chris Lattner22eb9722006-06-18 05:43:12 +0000414 case tok::amp: LHS &= RHS; break;
415 case tok::caret: LHS ^= RHS; break;
416 case tok::pipe: LHS |= RHS; break;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000417 case tok::ampamp:
418 LHS = LHS != 0 && RHS != 0;
419 LHS.setIsUnsigned(false); // C99 6.5.13p3, result is always int (signed)
420 break;
421 case tok::pipepipe:
422 LHS = LHS != 0 || RHS != 0;
423 LHS.setIsUnsigned(false); // C99 6.5.14p3, result is always int (signed)
424 break;
Chris Lattner22eb9722006-06-18 05:43:12 +0000425 case tok::comma:
Chris Lattnere3519cc2006-07-04 18:11:39 +0000426 PP.Diag(OpToken, diag::ext_pp_comma_expr);
Chris Lattner22eb9722006-06-18 05:43:12 +0000427 LHS = RHS; // LHS = LHS,RHS -> RHS.
428 break;
429 case tok::question: {
430 // Parse the : part of the expression.
431 if (PeekTok.getKind() != tok::colon) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000432 PP.Diag(OpToken, diag::err_pp_question_without_colon);
Chris Lattner22eb9722006-06-18 05:43:12 +0000433 return true;
434 }
435 // Consume the :.
Chris Lattnerbcb416b2006-10-27 05:43:50 +0000436 PP.LexNonComment(PeekTok);
Chris Lattner22eb9722006-06-18 05:43:12 +0000437
438 // Evaluate the value after the :.
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000439 APSInt AfterColonVal(LHS.getBitWidth());
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000440 DefinedTracker DT;
441 if (EvaluateValue(AfterColonVal, PeekTok, DT, PP)) return true;
Chris Lattner22eb9722006-06-18 05:43:12 +0000442
443 // Parse anything after the : RHS that has a higher precedence than ?.
444 if (EvaluateDirectiveSubExpr(AfterColonVal, ThisPrec+1,
Chris Lattnere3519cc2006-07-04 18:11:39 +0000445 PeekTok, PP))
Chris Lattner22eb9722006-06-18 05:43:12 +0000446 return true;
447
448 // Now that we have the condition, the LHS and the RHS of the :, evaluate.
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000449 LHS = LHS != 0 ? RHS : AfterColonVal;
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000450
451 // Usual arithmetic conversions (C99 6.3.1.8p1): result is unsigned if
452 // either operand is unsigned.
453 LHS.setIsUnsigned(RHS.isUnsigned() | AfterColonVal.isUnsigned());
Chris Lattner22eb9722006-06-18 05:43:12 +0000454
455 // Figure out the precedence of the token after the : part.
456 PeekPrec = getPrecedence(PeekTok.getKind());
457 break;
458 }
459 case tok::colon:
460 // Don't allow :'s to float around without being part of ?: exprs.
Chris Lattnere3519cc2006-07-04 18:11:39 +0000461 PP.Diag(OpToken, diag::err_pp_colon_without_question);
Chris Lattner22eb9722006-06-18 05:43:12 +0000462 return true;
463 }
464 }
465
466 return false;
467}
Chris Lattnere3519cc2006-07-04 18:11:39 +0000468
469/// EvaluateDirectiveExpression - Evaluate an integer constant expression that
470/// may occur after a #if or #elif directive. If the expression is equivalent
471/// to "!defined(X)" return X in IfNDefMacro.
472bool Preprocessor::
473EvaluateDirectiveExpression(IdentifierInfo *&IfNDefMacro) {
474 // Peek ahead one token.
475 LexerToken Tok;
476 Lex(Tok);
477
Chris Lattnerce5dc8a2007-04-04 06:46:55 +0000478 // C99 6.10.1p3 - All expressions are evaluated as intmax_t or uintmax_t.
479 unsigned BitWidth = getTargetInfo().getIntMaxTWidth(Tok.getLocation());
Chris Lattnera9eac7f2007-04-05 05:24:00 +0000480 APSInt ResVal(BitWidth);
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000481 DefinedTracker DT;
482 if (EvaluateValue(ResVal, Tok, DT, *this)) {
Chris Lattnere3519cc2006-07-04 18:11:39 +0000483 // Parse error, skip the rest of the macro line.
484 if (Tok.getKind() != tok::eom)
485 DiscardUntilEndOfDirective();
486 return false;
487 }
488
489 // If we are at the end of the expression after just parsing a value, there
490 // must be no (unparenthesized) binary operators involved, so we can exit
491 // directly.
492 if (Tok.getKind() == tok::eom) {
Chris Lattnerb9d90f72006-07-04 18:32:03 +0000493 // If the expression we parsed was of the form !defined(macro), return the
494 // macro in IfNDefMacro.
495 if (DT.State == DefinedTracker::NotDefinedMacro)
496 IfNDefMacro = DT.TheMacro;
497
Chris Lattnere3519cc2006-07-04 18:11:39 +0000498 return ResVal != 0;
499 }
500
501 // Otherwise, we must have a binary operator (e.g. "#if 1 < 2"), so parse the
502 // operator and the stuff after it.
503 if (EvaluateDirectiveSubExpr(ResVal, 1, Tok, *this)) {
504 // Parse error, skip the rest of the macro line.
505 if (Tok.getKind() != tok::eom)
506 DiscardUntilEndOfDirective();
507 return false;
508 }
509
510 // If we aren't at the tok::eom token, something bad happened, like an extra
511 // ')' token.
512 if (Tok.getKind() != tok::eom) {
513 Diag(Tok, diag::err_pp_expected_eol);
514 DiscardUntilEndOfDirective();
515 }
516
517 return ResVal != 0;
518}
519