Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- Expr.cpp - Expression AST Node Implementation --------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the Expr class and subclasses. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 14 | #include "clang/AST/Expr.h" |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 15 | #include "clang/AST/APValue.h" |
Chris Lattner | 2eadfb6 | 2007-07-15 23:32:58 +0000 | [diff] [blame] | 16 | #include "clang/AST/ASTContext.h" |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 17 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclCXX.h" |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclTemplate.h" |
Daniel Dunbar | e91593e | 2008-08-11 04:54:23 +0000 | [diff] [blame] | 20 | #include "clang/AST/RecordLayout.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 21 | #include "clang/AST/StmtVisitor.h" |
Chris Lattner | da5a6b6 | 2007-11-27 18:22:04 +0000 | [diff] [blame] | 22 | #include "clang/Basic/TargetInfo.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 23 | using namespace clang; |
| 24 | |
| 25 | //===----------------------------------------------------------------------===// |
| 26 | // Primary Expressions. |
| 27 | //===----------------------------------------------------------------------===// |
| 28 | |
Chris Lattner | da8249e | 2008-06-07 22:13:43 +0000 | [diff] [blame] | 29 | /// getValueAsApproximateDouble - This returns the value as an inaccurate |
| 30 | /// double. Note that this may cause loss of precision, but is useful for |
| 31 | /// debugging dumps, etc. |
| 32 | double FloatingLiteral::getValueAsApproximateDouble() const { |
| 33 | llvm::APFloat V = getValue(); |
Dale Johannesen | ee5a700 | 2008-10-09 23:02:32 +0000 | [diff] [blame] | 34 | bool ignored; |
| 35 | V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven, |
| 36 | &ignored); |
Chris Lattner | da8249e | 2008-06-07 22:13:43 +0000 | [diff] [blame] | 37 | return V.convertToDouble(); |
| 38 | } |
| 39 | |
Chris Lattner | 2085fd6 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 40 | StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData, |
| 41 | unsigned ByteLength, bool Wide, |
| 42 | QualType Ty, |
| 43 | SourceLocation *Loc, unsigned NumStrs) { |
| 44 | // Allocate enough space for the StringLiteral plus an array of locations for |
| 45 | // any concatenated string tokens. |
| 46 | void *Mem = C.Allocate(sizeof(StringLiteral)+ |
| 47 | sizeof(SourceLocation)*(NumStrs-1), |
| 48 | llvm::alignof<StringLiteral>()); |
| 49 | StringLiteral *SL = new (Mem) StringLiteral(Ty); |
| 50 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 51 | // OPTIMIZE: could allocate this appended to the StringLiteral. |
Chris Lattner | 2085fd6 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 52 | char *AStrData = new (C, 1) char[ByteLength]; |
| 53 | memcpy(AStrData, StrData, ByteLength); |
| 54 | SL->StrData = AStrData; |
| 55 | SL->ByteLength = ByteLength; |
| 56 | SL->IsWide = Wide; |
| 57 | SL->TokLocs[0] = Loc[0]; |
| 58 | SL->NumConcatenated = NumStrs; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 59 | |
Chris Lattner | 726e168 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 60 | if (NumStrs != 1) |
Chris Lattner | 2085fd6 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 61 | memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1)); |
| 62 | return SL; |
Chris Lattner | 726e168 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | |
Ted Kremenek | 6e94ef5 | 2009-02-06 19:55:15 +0000 | [diff] [blame] | 66 | void StringLiteral::Destroy(ASTContext &C) { |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 67 | C.Deallocate(const_cast<char*>(StrData)); |
Ted Kremenek | 353ffce | 2009-02-09 17:10:09 +0000 | [diff] [blame] | 68 | this->~StringLiteral(); |
| 69 | C.Deallocate(this); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 70 | } |
| 71 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 72 | /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it |
| 73 | /// corresponds to, e.g. "sizeof" or "[pre]++". |
| 74 | const char *UnaryOperator::getOpcodeStr(Opcode Op) { |
| 75 | switch (Op) { |
| 76 | default: assert(0 && "Unknown unary operator"); |
| 77 | case PostInc: return "++"; |
| 78 | case PostDec: return "--"; |
| 79 | case PreInc: return "++"; |
| 80 | case PreDec: return "--"; |
| 81 | case AddrOf: return "&"; |
| 82 | case Deref: return "*"; |
| 83 | case Plus: return "+"; |
| 84 | case Minus: return "-"; |
| 85 | case Not: return "~"; |
| 86 | case LNot: return "!"; |
| 87 | case Real: return "__real"; |
| 88 | case Imag: return "__imag"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 89 | case Extension: return "__extension__"; |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 90 | case OffsetOf: return "__builtin_offsetof"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 91 | } |
| 92 | } |
| 93 | |
| 94 | //===----------------------------------------------------------------------===// |
| 95 | // Postfix Operators. |
| 96 | //===----------------------------------------------------------------------===// |
| 97 | |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 98 | CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 99 | unsigned numargs, QualType t, SourceLocation rparenloc) |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 100 | : Expr(SC, t, |
| 101 | fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs), |
Chris Lattner | d603eaa | 2009-02-16 22:33:34 +0000 | [diff] [blame] | 102 | fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)), |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 103 | NumArgs(numargs) { |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 104 | |
| 105 | SubExprs = new (C) Stmt*[numargs+1]; |
Douglas Gregor | b460980 | 2008-11-14 16:09:21 +0000 | [diff] [blame] | 106 | SubExprs[FN] = fn; |
| 107 | for (unsigned i = 0; i != numargs; ++i) |
| 108 | SubExprs[i+ARGS_START] = args[i]; |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 109 | |
Douglas Gregor | b460980 | 2008-11-14 16:09:21 +0000 | [diff] [blame] | 110 | RParenLoc = rparenloc; |
| 111 | } |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 112 | |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 113 | CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs, |
| 114 | QualType t, SourceLocation rparenloc) |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 115 | : Expr(CallExprClass, t, |
| 116 | fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs), |
Chris Lattner | d603eaa | 2009-02-16 22:33:34 +0000 | [diff] [blame] | 117 | fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)), |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 118 | NumArgs(numargs) { |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 119 | |
| 120 | SubExprs = new (C) Stmt*[numargs+1]; |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 121 | SubExprs[FN] = fn; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 122 | for (unsigned i = 0; i != numargs; ++i) |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 123 | SubExprs[i+ARGS_START] = args[i]; |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 124 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 125 | RParenLoc = rparenloc; |
| 126 | } |
| 127 | |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 128 | void CallExpr::Destroy(ASTContext& C) { |
| 129 | DestroyChildren(C); |
| 130 | if (SubExprs) C.Deallocate(SubExprs); |
| 131 | this->~CallExpr(); |
| 132 | C.Deallocate(this); |
| 133 | } |
| 134 | |
Chris Lattner | d18b329 | 2007-12-28 05:25:02 +0000 | [diff] [blame] | 135 | /// setNumArgs - This changes the number of arguments present in this call. |
| 136 | /// Any orphaned expressions are deleted by this, and any new operands are set |
| 137 | /// to null. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 138 | void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) { |
Chris Lattner | d18b329 | 2007-12-28 05:25:02 +0000 | [diff] [blame] | 139 | // No change, just return. |
| 140 | if (NumArgs == getNumArgs()) return; |
| 141 | |
| 142 | // If shrinking # arguments, just delete the extras and forgot them. |
| 143 | if (NumArgs < getNumArgs()) { |
| 144 | for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 145 | getArg(i)->Destroy(C); |
Chris Lattner | d18b329 | 2007-12-28 05:25:02 +0000 | [diff] [blame] | 146 | this->NumArgs = NumArgs; |
| 147 | return; |
| 148 | } |
| 149 | |
| 150 | // Otherwise, we are growing the # arguments. New an bigger argument array. |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 151 | Stmt **NewSubExprs = new Stmt*[NumArgs+1]; |
Chris Lattner | d18b329 | 2007-12-28 05:25:02 +0000 | [diff] [blame] | 152 | // Copy over args. |
| 153 | for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i) |
| 154 | NewSubExprs[i] = SubExprs[i]; |
| 155 | // Null out new args. |
| 156 | for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i) |
| 157 | NewSubExprs[i] = 0; |
| 158 | |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 159 | delete [] SubExprs; |
Chris Lattner | d18b329 | 2007-12-28 05:25:02 +0000 | [diff] [blame] | 160 | SubExprs = NewSubExprs; |
| 161 | this->NumArgs = NumArgs; |
| 162 | } |
| 163 | |
Chris Lattner | cb88896 | 2008-10-06 05:00:53 +0000 | [diff] [blame] | 164 | /// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If |
| 165 | /// not, return 0. |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 166 | unsigned CallExpr::isBuiltinCall(ASTContext &Context) const { |
Steve Naroff | c4f8e8b | 2008-01-31 01:07:12 +0000 | [diff] [blame] | 167 | // All simple function calls (e.g. func()) are implicitly cast to pointer to |
| 168 | // function. As a result, we try and obtain the DeclRefExpr from the |
| 169 | // ImplicitCastExpr. |
| 170 | const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee()); |
| 171 | if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()). |
Chris Lattner | cb88896 | 2008-10-06 05:00:53 +0000 | [diff] [blame] | 172 | return 0; |
| 173 | |
Steve Naroff | c4f8e8b | 2008-01-31 01:07:12 +0000 | [diff] [blame] | 174 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr()); |
| 175 | if (!DRE) |
Chris Lattner | cb88896 | 2008-10-06 05:00:53 +0000 | [diff] [blame] | 176 | return 0; |
| 177 | |
Anders Carlsson | bcba201 | 2008-01-31 02:13:57 +0000 | [diff] [blame] | 178 | const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl()); |
| 179 | if (!FDecl) |
Chris Lattner | cb88896 | 2008-10-06 05:00:53 +0000 | [diff] [blame] | 180 | return 0; |
| 181 | |
Douglas Gregor | 4fcd399 | 2008-11-21 15:30:19 +0000 | [diff] [blame] | 182 | if (!FDecl->getIdentifier()) |
| 183 | return 0; |
| 184 | |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 185 | return FDecl->getBuiltinID(Context); |
Chris Lattner | cb88896 | 2008-10-06 05:00:53 +0000 | [diff] [blame] | 186 | } |
Anders Carlsson | bcba201 | 2008-01-31 02:13:57 +0000 | [diff] [blame] | 187 | |
Chris Lattner | cb88896 | 2008-10-06 05:00:53 +0000 | [diff] [blame] | 188 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 189 | /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it |
| 190 | /// corresponds to, e.g. "<<=". |
| 191 | const char *BinaryOperator::getOpcodeStr(Opcode Op) { |
| 192 | switch (Op) { |
| 193 | default: assert(0 && "Unknown binary operator"); |
| 194 | case Mul: return "*"; |
| 195 | case Div: return "/"; |
| 196 | case Rem: return "%"; |
| 197 | case Add: return "+"; |
| 198 | case Sub: return "-"; |
| 199 | case Shl: return "<<"; |
| 200 | case Shr: return ">>"; |
| 201 | case LT: return "<"; |
| 202 | case GT: return ">"; |
| 203 | case LE: return "<="; |
| 204 | case GE: return ">="; |
| 205 | case EQ: return "=="; |
| 206 | case NE: return "!="; |
| 207 | case And: return "&"; |
| 208 | case Xor: return "^"; |
| 209 | case Or: return "|"; |
| 210 | case LAnd: return "&&"; |
| 211 | case LOr: return "||"; |
| 212 | case Assign: return "="; |
| 213 | case MulAssign: return "*="; |
| 214 | case DivAssign: return "/="; |
| 215 | case RemAssign: return "%="; |
| 216 | case AddAssign: return "+="; |
| 217 | case SubAssign: return "-="; |
| 218 | case ShlAssign: return "<<="; |
| 219 | case ShrAssign: return ">>="; |
| 220 | case AndAssign: return "&="; |
| 221 | case XorAssign: return "^="; |
| 222 | case OrAssign: return "|="; |
| 223 | case Comma: return ","; |
| 224 | } |
| 225 | } |
| 226 | |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 227 | InitListExpr::InitListExpr(SourceLocation lbraceloc, |
Chris Lattner | 418f6c7 | 2008-10-26 23:43:26 +0000 | [diff] [blame] | 228 | Expr **initExprs, unsigned numInits, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 229 | SourceLocation rbraceloc) |
Steve Naroff | c5ae899 | 2008-05-01 02:04:18 +0000 | [diff] [blame] | 230 | : Expr(InitListExprClass, QualType()), |
Douglas Gregor | 0bb7689 | 2009-01-29 16:53:55 +0000 | [diff] [blame] | 231 | LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0), |
Douglas Gregor | a9c8780 | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 232 | UnionFieldInit(0), HadArrayRangeDesignator(false) { |
Chris Lattner | 418f6c7 | 2008-10-26 23:43:26 +0000 | [diff] [blame] | 233 | |
| 234 | InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits); |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 235 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 236 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 237 | void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) { |
Chris Lattner | d603eaa | 2009-02-16 22:33:34 +0000 | [diff] [blame] | 238 | for (unsigned Idx = NumInits, LastIdx = InitExprs.size(); |
Daniel Dunbar | f592c92 | 2009-02-16 22:42:44 +0000 | [diff] [blame] | 239 | Idx < LastIdx; ++Idx) |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 240 | delete InitExprs[Idx]; |
| 241 | InitExprs.resize(NumInits, 0); |
| 242 | } |
| 243 | |
| 244 | Expr *InitListExpr::updateInit(unsigned Init, Expr *expr) { |
| 245 | if (Init >= InitExprs.size()) { |
| 246 | InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0); |
| 247 | InitExprs.back() = expr; |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | Expr *Result = cast_or_null<Expr>(InitExprs[Init]); |
| 252 | InitExprs[Init] = expr; |
| 253 | return Result; |
| 254 | } |
| 255 | |
Steve Naroff | bfdcae6 | 2008-09-04 15:31:07 +0000 | [diff] [blame] | 256 | /// getFunctionType - Return the underlying function type for this block. |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 257 | /// |
| 258 | const FunctionType *BlockExpr::getFunctionType() const { |
| 259 | return getType()->getAsBlockPointerType()-> |
| 260 | getPointeeType()->getAsFunctionType(); |
| 261 | } |
| 262 | |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 263 | SourceLocation BlockExpr::getCaretLocation() const { |
| 264 | return TheBlock->getCaretLocation(); |
| 265 | } |
| 266 | const Stmt *BlockExpr::getBody() const { return TheBlock->getBody(); } |
| 267 | Stmt *BlockExpr::getBody() { return TheBlock->getBody(); } |
| 268 | |
| 269 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 270 | //===----------------------------------------------------------------------===// |
| 271 | // Generic Expression Routines |
| 272 | //===----------------------------------------------------------------------===// |
| 273 | |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 274 | /// isUnusedResultAWarning - Return true if this immediate expression should |
| 275 | /// be warned about if the result is unused. If so, fill in Loc and Ranges |
| 276 | /// with location to warn on and the source range[s] to report with the |
| 277 | /// warning. |
| 278 | bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1, |
| 279 | SourceRange &R2) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 280 | switch (getStmtClass()) { |
| 281 | default: |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 282 | Loc = getExprLoc(); |
| 283 | R1 = getSourceRange(); |
| 284 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 285 | case ParenExprClass: |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 286 | return cast<ParenExpr>(this)->getSubExpr()-> |
| 287 | isUnusedResultAWarning(Loc, R1, R2); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 288 | case UnaryOperatorClass: { |
| 289 | const UnaryOperator *UO = cast<UnaryOperator>(this); |
| 290 | |
| 291 | switch (UO->getOpcode()) { |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 292 | default: break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 293 | case UnaryOperator::PostInc: |
| 294 | case UnaryOperator::PostDec: |
| 295 | case UnaryOperator::PreInc: |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 296 | case UnaryOperator::PreDec: // ++/-- |
| 297 | return false; // Not a warning. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 298 | case UnaryOperator::Deref: |
| 299 | // Dereferencing a volatile pointer is a side-effect. |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 300 | if (getType().isVolatileQualified()) |
| 301 | return false; |
| 302 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 303 | case UnaryOperator::Real: |
| 304 | case UnaryOperator::Imag: |
| 305 | // accessing a piece of a volatile complex is a side-effect. |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 306 | if (UO->getSubExpr()->getType().isVolatileQualified()) |
| 307 | return false; |
| 308 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 309 | case UnaryOperator::Extension: |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 310 | return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 311 | } |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 312 | Loc = UO->getOperatorLoc(); |
| 313 | R1 = UO->getSubExpr()->getSourceRange(); |
| 314 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 315 | } |
Chris Lattner | e7716e6 | 2007-12-01 06:07:34 +0000 | [diff] [blame] | 316 | case BinaryOperatorClass: { |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 317 | const BinaryOperator *BO = cast<BinaryOperator>(this); |
| 318 | // Consider comma to have side effects if the LHS or RHS does. |
| 319 | if (BO->getOpcode() == BinaryOperator::Comma) |
| 320 | return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2) || |
| 321 | BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2); |
Chris Lattner | e7716e6 | 2007-12-01 06:07:34 +0000 | [diff] [blame] | 322 | |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 323 | if (BO->isAssignmentOp()) |
| 324 | return false; |
| 325 | Loc = BO->getOperatorLoc(); |
| 326 | R1 = BO->getLHS()->getSourceRange(); |
| 327 | R2 = BO->getRHS()->getSourceRange(); |
| 328 | return true; |
Chris Lattner | e7716e6 | 2007-12-01 06:07:34 +0000 | [diff] [blame] | 329 | } |
Chris Lattner | eb14fe8 | 2007-08-25 02:00:02 +0000 | [diff] [blame] | 330 | case CompoundAssignOperatorClass: |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 331 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 332 | |
Fariborz Jahanian | ab38e4b | 2007-12-01 19:58:28 +0000 | [diff] [blame] | 333 | case ConditionalOperatorClass: { |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 334 | // The condition must be evaluated, but if either the LHS or RHS is a |
| 335 | // warning, warn about them. |
Fariborz Jahanian | ab38e4b | 2007-12-01 19:58:28 +0000 | [diff] [blame] | 336 | const ConditionalOperator *Exp = cast<ConditionalOperator>(this); |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 337 | if (Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2)) |
| 338 | return true; |
| 339 | return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2); |
Fariborz Jahanian | ab38e4b | 2007-12-01 19:58:28 +0000 | [diff] [blame] | 340 | } |
| 341 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 342 | case MemberExprClass: |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 343 | // If the base pointer or element is to a volatile pointer/field, accessing |
| 344 | // it is a side effect. |
| 345 | if (getType().isVolatileQualified()) |
| 346 | return false; |
| 347 | Loc = cast<MemberExpr>(this)->getMemberLoc(); |
| 348 | R1 = SourceRange(Loc, Loc); |
| 349 | R2 = cast<MemberExpr>(this)->getBase()->getSourceRange(); |
| 350 | return true; |
| 351 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 352 | case ArraySubscriptExprClass: |
| 353 | // If the base pointer or element is to a volatile pointer/field, accessing |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 354 | // it is a side effect. |
| 355 | if (getType().isVolatileQualified()) |
| 356 | return false; |
| 357 | Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc(); |
| 358 | R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange(); |
| 359 | R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange(); |
| 360 | return true; |
Eli Friedman | 211f6ad | 2008-05-27 15:24:04 +0000 | [diff] [blame] | 361 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 362 | case CallExprClass: |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 363 | case CXXOperatorCallExprClass: { |
| 364 | // If this is a direct call, get the callee. |
| 365 | const CallExpr *CE = cast<CallExpr>(this); |
| 366 | const Expr *CalleeExpr = CE->getCallee()->IgnoreParenCasts(); |
| 367 | if (const DeclRefExpr *CalleeDRE = dyn_cast<DeclRefExpr>(CalleeExpr)) { |
| 368 | // If the callee has attribute pure, const, or warn_unused_result, warn |
| 369 | // about it. void foo() { strlen("bar"); } should warn. |
| 370 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CalleeDRE->getDecl())) |
| 371 | if (FD->getAttr<WarnUnusedResultAttr>() || |
| 372 | FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) { |
| 373 | Loc = CE->getCallee()->getLocStart(); |
| 374 | R1 = CE->getCallee()->getSourceRange(); |
| 375 | |
| 376 | if (unsigned NumArgs = CE->getNumArgs()) |
| 377 | R2 = SourceRange(CE->getArg(0)->getLocStart(), |
| 378 | CE->getArg(NumArgs-1)->getLocEnd()); |
| 379 | return true; |
| 380 | } |
| 381 | } |
| 382 | return false; |
| 383 | } |
Chris Lattner | a9c0102 | 2007-09-26 22:06:30 +0000 | [diff] [blame] | 384 | case ObjCMessageExprClass: |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 385 | return false; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 386 | case StmtExprClass: { |
| 387 | // Statement exprs don't logically have side effects themselves, but are |
| 388 | // sometimes used in macros in ways that give them a type that is unused. |
| 389 | // For example ({ blah; foo(); }) will end up with a type if foo has a type. |
| 390 | // however, if the result of the stmt expr is dead, we don't want to emit a |
| 391 | // warning. |
| 392 | const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt(); |
| 393 | if (!CS->body_empty()) |
| 394 | if (const Expr *E = dyn_cast<Expr>(CS->body_back())) |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 395 | return E->isUnusedResultAWarning(Loc, R1, R2); |
| 396 | |
| 397 | Loc = cast<StmtExpr>(this)->getLParenLoc(); |
| 398 | R1 = getSourceRange(); |
| 399 | return true; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 400 | } |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 401 | case CStyleCastExprClass: |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 402 | // If this is a cast to void, check the operand. Otherwise, the result of |
| 403 | // the cast is unused. |
| 404 | if (getType()->isVoidType()) |
| 405 | return cast<CastExpr>(this)->getSubExpr()->isUnusedResultAWarning(Loc, |
| 406 | R1, R2); |
| 407 | Loc = cast<CStyleCastExpr>(this)->getLParenLoc(); |
| 408 | R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange(); |
| 409 | return true; |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 410 | case CXXFunctionalCastExprClass: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 411 | // If this is a cast to void, check the operand. Otherwise, the result of |
| 412 | // the cast is unused. |
| 413 | if (getType()->isVoidType()) |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 414 | return cast<CastExpr>(this)->getSubExpr()->isUnusedResultAWarning(Loc, |
| 415 | R1, R2); |
| 416 | Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc(); |
| 417 | R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange(); |
| 418 | return true; |
| 419 | |
Eli Friedman | 4be1f47 | 2008-05-19 21:24:43 +0000 | [diff] [blame] | 420 | case ImplicitCastExprClass: |
| 421 | // Check the operand, since implicit casts are inserted by Sema |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 422 | return cast<ImplicitCastExpr>(this) |
| 423 | ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2); |
Eli Friedman | 4be1f47 | 2008-05-19 21:24:43 +0000 | [diff] [blame] | 424 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 425 | case CXXDefaultArgExprClass: |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 426 | return cast<CXXDefaultArgExpr>(this) |
| 427 | ->getExpr()->isUnusedResultAWarning(Loc, R1, R2); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 428 | |
| 429 | case CXXNewExprClass: |
| 430 | // FIXME: In theory, there might be new expressions that don't have side |
| 431 | // effects (e.g. a placement new with an uninitialized POD). |
| 432 | case CXXDeleteExprClass: |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 433 | return false; |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 434 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Douglas Gregor | ba7e210 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 437 | /// DeclCanBeLvalue - Determine whether the given declaration can be |
| 438 | /// an lvalue. This is a helper routine for isLvalue. |
| 439 | static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) { |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 440 | // C++ [temp.param]p6: |
| 441 | // A non-type non-reference template-parameter is not an lvalue. |
| 442 | if (const NonTypeTemplateParmDecl *NTTParm |
| 443 | = dyn_cast<NonTypeTemplateParmDecl>(Decl)) |
| 444 | return NTTParm->getType()->isReferenceType(); |
| 445 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 446 | return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) || |
Douglas Gregor | ba7e210 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 447 | // C++ 3.10p2: An lvalue refers to an object or function. |
| 448 | (Ctx.getLangOptions().CPlusPlus && |
| 449 | (isa<FunctionDecl>(Decl) || isa<OverloadedFunctionDecl>(Decl))); |
| 450 | } |
| 451 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 452 | /// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an |
| 453 | /// incomplete type other than void. Nonarray expressions that can be lvalues: |
| 454 | /// - name, where name must be a variable |
| 455 | /// - e[i] |
| 456 | /// - (e), where e must be an lvalue |
| 457 | /// - e.name, where e must be an lvalue |
| 458 | /// - e->name |
| 459 | /// - *e, the type of e cannot be a function type |
| 460 | /// - string-constant |
Chris Lattner | 7da36f6 | 2007-10-30 22:53:42 +0000 | [diff] [blame] | 461 | /// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension] |
Bill Wendling | 08ad47c | 2007-07-17 03:52:31 +0000 | [diff] [blame] | 462 | /// - reference type [C++ [expr]] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 463 | /// |
Chris Lattner | 28be73f | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 464 | Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 465 | // first, check the type (C99 6.3.2.1). Expressions with function |
| 466 | // type in C are not lvalues, but they can be lvalues in C++. |
| 467 | if (!Ctx.getLangOptions().CPlusPlus && TR->isFunctionType()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 468 | return LV_NotObjectType; |
| 469 | |
Steve Naroff | acb818a | 2008-02-10 01:39:04 +0000 | [diff] [blame] | 470 | // Allow qualified void which is an incomplete type other than void (yuck). |
Chris Lattner | 28be73f | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 471 | if (TR->isVoidType() && !Ctx.getCanonicalType(TR).getCVRQualifiers()) |
Steve Naroff | acb818a | 2008-02-10 01:39:04 +0000 | [diff] [blame] | 472 | return LV_IncompleteVoidType; |
| 473 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 474 | /// FIXME: Expressions can't have reference type, so the following |
| 475 | /// isn't needed. |
Chris Lattner | cb4f9a6 | 2007-07-21 05:33:26 +0000 | [diff] [blame] | 476 | if (TR->isReferenceType()) // C++ [expr] |
Bill Wendling | 08ad47c | 2007-07-17 03:52:31 +0000 | [diff] [blame] | 477 | return LV_Valid; |
| 478 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 479 | // the type looks fine, now check the expression |
| 480 | switch (getStmtClass()) { |
| 481 | case StringLiteralClass: // C99 6.5.1p4 |
Anders Carlsson | 7323a62 | 2007-11-30 22:47:59 +0000 | [diff] [blame] | 482 | return LV_Valid; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 483 | case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2)))) |
| 484 | // For vectors, make sure base is an lvalue (i.e. not a function call). |
| 485 | if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType()) |
Chris Lattner | 28be73f | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 486 | return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 487 | return LV_Valid; |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 488 | case DeclRefExprClass: |
| 489 | case QualifiedDeclRefExprClass: { // C99 6.5.1p2 |
Douglas Gregor | ba7e210 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 490 | const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl(); |
| 491 | if (DeclCanBeLvalue(RefdDecl, Ctx)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 492 | return LV_Valid; |
| 493 | break; |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 494 | } |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 495 | case BlockDeclRefExprClass: { |
| 496 | const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this); |
Steve Naroff | 4f6a7d7 | 2008-09-26 14:41:28 +0000 | [diff] [blame] | 497 | if (isa<VarDecl>(BDR->getDecl())) |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 498 | return LV_Valid; |
| 499 | break; |
| 500 | } |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 501 | case MemberExprClass: { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 502 | const MemberExpr *m = cast<MemberExpr>(this); |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 503 | if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4: |
| 504 | NamedDecl *Member = m->getMemberDecl(); |
| 505 | // C++ [expr.ref]p4: |
| 506 | // If E2 is declared to have type "reference to T", then E1.E2 |
| 507 | // is an lvalue. |
| 508 | if (ValueDecl *Value = dyn_cast<ValueDecl>(Member)) |
| 509 | if (Value->getType()->isReferenceType()) |
| 510 | return LV_Valid; |
| 511 | |
| 512 | // -- If E2 is a static data member [...] then E1.E2 is an lvalue. |
| 513 | if (isa<CXXClassVarDecl>(Member)) |
| 514 | return LV_Valid; |
| 515 | |
| 516 | // -- If E2 is a non-static data member [...]. If E1 is an |
| 517 | // lvalue, then E1.E2 is an lvalue. |
| 518 | if (isa<FieldDecl>(Member)) |
| 519 | return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx); |
| 520 | |
| 521 | // -- If it refers to a static member function [...], then |
| 522 | // E1.E2 is an lvalue. |
| 523 | // -- Otherwise, if E1.E2 refers to a non-static member |
| 524 | // function [...], then E1.E2 is not an lvalue. |
| 525 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member)) |
| 526 | return Method->isStatic()? LV_Valid : LV_MemberFunction; |
| 527 | |
| 528 | // -- If E2 is a member enumerator [...], the expression E1.E2 |
| 529 | // is not an lvalue. |
| 530 | if (isa<EnumConstantDecl>(Member)) |
| 531 | return LV_InvalidExpression; |
| 532 | |
| 533 | // Not an lvalue. |
| 534 | return LV_InvalidExpression; |
| 535 | } |
| 536 | |
| 537 | // C99 6.5.2.3p4 |
Chris Lattner | 28be73f | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 538 | return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx); |
Anton Korobeynikov | fdd7566 | 2007-07-12 15:26:50 +0000 | [diff] [blame] | 539 | } |
Chris Lattner | 7da36f6 | 2007-10-30 22:53:42 +0000 | [diff] [blame] | 540 | case UnaryOperatorClass: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 541 | if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref) |
Chris Lattner | 7da36f6 | 2007-10-30 22:53:42 +0000 | [diff] [blame] | 542 | return LV_Valid; // C99 6.5.3p4 |
| 543 | |
| 544 | if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real || |
Chris Lattner | baf0d66 | 2008-07-25 18:07:19 +0000 | [diff] [blame] | 545 | cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag || |
| 546 | cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension) |
Chris Lattner | 28be73f | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 547 | return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU. |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 548 | |
| 549 | if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1 |
| 550 | (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc || |
| 551 | cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec)) |
| 552 | return LV_Valid; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 553 | break; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 554 | case ImplicitCastExprClass: |
| 555 | return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid |
| 556 | : LV_InvalidExpression; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 557 | case ParenExprClass: // C99 6.5.1p5 |
Chris Lattner | 28be73f | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 558 | return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 559 | case BinaryOperatorClass: |
| 560 | case CompoundAssignOperatorClass: { |
| 561 | const BinaryOperator *BinOp = cast<BinaryOperator>(this); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 562 | |
| 563 | if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1 |
| 564 | BinOp->getOpcode() == BinaryOperator::Comma) |
| 565 | return BinOp->getRHS()->isLvalue(Ctx); |
| 566 | |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 567 | // C++ [expr.mptr.oper]p6 |
| 568 | if ((BinOp->getOpcode() == BinaryOperator::PtrMemD || |
| 569 | BinOp->getOpcode() == BinaryOperator::PtrMemI) && |
| 570 | !BinOp->getType()->isFunctionType()) |
| 571 | return BinOp->getLHS()->isLvalue(Ctx); |
| 572 | |
Douglas Gregor | bf3af05 | 2008-11-13 20:12:29 +0000 | [diff] [blame] | 573 | if (!BinOp->isAssignmentOp()) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 574 | return LV_InvalidExpression; |
| 575 | |
Douglas Gregor | bf3af05 | 2008-11-13 20:12:29 +0000 | [diff] [blame] | 576 | if (Ctx.getLangOptions().CPlusPlus) |
| 577 | // C++ [expr.ass]p1: |
| 578 | // The result of an assignment operation [...] is an lvalue. |
| 579 | return LV_Valid; |
| 580 | |
| 581 | |
| 582 | // C99 6.5.16: |
| 583 | // An assignment expression [...] is not an lvalue. |
| 584 | return LV_InvalidExpression; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 585 | } |
Douglas Gregor | b460980 | 2008-11-14 16:09:21 +0000 | [diff] [blame] | 586 | case CallExprClass: |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 587 | case CXXOperatorCallExprClass: |
| 588 | case CXXMemberCallExprClass: { |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 589 | // C++ [expr.call]p10: |
| 590 | // A function call is an lvalue if and only if the result type |
| 591 | // is a reference. |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 592 | QualType CalleeType = cast<CallExpr>(this)->getCallee()->getType(); |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 593 | if (const PointerType *FnTypePtr = CalleeType->getAsPointerType()) |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 594 | CalleeType = FnTypePtr->getPointeeType(); |
| 595 | if (const FunctionType *FnType = CalleeType->getAsFunctionType()) |
| 596 | if (FnType->getResultType()->isReferenceType()) |
| 597 | return LV_Valid; |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 598 | |
| 599 | break; |
| 600 | } |
Steve Naroff | e638639 | 2007-12-05 04:00:10 +0000 | [diff] [blame] | 601 | case CompoundLiteralExprClass: // C99 6.5.2.5p5 |
| 602 | return LV_Valid; |
Chris Lattner | 670a62c | 2008-12-12 05:35:08 +0000 | [diff] [blame] | 603 | case ChooseExprClass: |
| 604 | // __builtin_choose_expr is an lvalue if the selected operand is. |
| 605 | if (cast<ChooseExpr>(this)->isConditionTrue(Ctx)) |
| 606 | return cast<ChooseExpr>(this)->getLHS()->isLvalue(Ctx); |
| 607 | else |
| 608 | return cast<ChooseExpr>(this)->getRHS()->isLvalue(Ctx); |
| 609 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 610 | case ExtVectorElementExprClass: |
| 611 | if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements()) |
Steve Naroff | fec0b49 | 2007-07-30 03:29:09 +0000 | [diff] [blame] | 612 | return LV_DuplicateVectorComponents; |
| 613 | return LV_Valid; |
Steve Naroff | 027282d | 2007-11-12 14:34:27 +0000 | [diff] [blame] | 614 | case ObjCIvarRefExprClass: // ObjC instance variables are lvalues. |
| 615 | return LV_Valid; |
Steve Naroff | 799a6a6 | 2008-05-30 23:23:16 +0000 | [diff] [blame] | 616 | case ObjCPropertyRefExprClass: // FIXME: check if read-only property. |
| 617 | return LV_Valid; |
Fariborz Jahanian | 5daf570 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 618 | case ObjCKVCRefExprClass: // FIXME: check if read-only property. |
Chris Lattner | 670a62c | 2008-12-12 05:35:08 +0000 | [diff] [blame] | 619 | return LV_Valid; |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 620 | case PredefinedExprClass: |
Douglas Gregor | 796da18 | 2008-11-04 14:32:21 +0000 | [diff] [blame] | 621 | return LV_Valid; |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 622 | case VAArgExprClass: |
Daniel Dunbar | adadd8d | 2009-02-12 09:21:08 +0000 | [diff] [blame] | 623 | return LV_NotObjectType; |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 624 | case CXXDefaultArgExprClass: |
Chris Lattner | 28be73f | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 625 | return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx); |
Argyrios Kyrtzidis | 24b41fa | 2008-09-11 04:22:26 +0000 | [diff] [blame] | 626 | case CXXConditionDeclExprClass: |
| 627 | return LV_Valid; |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 628 | case CStyleCastExprClass: |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 629 | case CXXFunctionalCastExprClass: |
| 630 | case CXXStaticCastExprClass: |
| 631 | case CXXDynamicCastExprClass: |
| 632 | case CXXReinterpretCastExprClass: |
| 633 | case CXXConstCastExprClass: |
| 634 | // The result of an explicit cast is an lvalue if the type we are |
| 635 | // casting to is a reference type. See C++ [expr.cast]p1, |
| 636 | // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2, |
| 637 | // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1. |
| 638 | if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()->isReferenceType()) |
| 639 | return LV_Valid; |
| 640 | break; |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 641 | case CXXTypeidExprClass: |
| 642 | // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ... |
| 643 | return LV_Valid; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 644 | default: |
| 645 | break; |
| 646 | } |
| 647 | return LV_InvalidExpression; |
| 648 | } |
| 649 | |
| 650 | /// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type, |
| 651 | /// does not have an incomplete type, does not have a const-qualified type, and |
| 652 | /// if it is a structure or union, does not have any member (including, |
| 653 | /// recursively, any member or element of all contained aggregates or unions) |
| 654 | /// with a const-qualified type. |
Chris Lattner | 28be73f | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 655 | Expr::isModifiableLvalueResult Expr::isModifiableLvalue(ASTContext &Ctx) const { |
| 656 | isLvalueResult lvalResult = isLvalue(Ctx); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 657 | |
| 658 | switch (lvalResult) { |
Douglas Gregor | ae8d467 | 2008-10-22 00:03:08 +0000 | [diff] [blame] | 659 | case LV_Valid: |
| 660 | // C++ 3.10p11: Functions cannot be modified, but pointers to |
| 661 | // functions can be modifiable. |
| 662 | if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType()) |
| 663 | return MLV_NotObjectType; |
| 664 | break; |
| 665 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 666 | case LV_NotObjectType: return MLV_NotObjectType; |
| 667 | case LV_IncompleteVoidType: return MLV_IncompleteVoidType; |
Steve Naroff | fec0b49 | 2007-07-30 03:29:09 +0000 | [diff] [blame] | 668 | case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents; |
Chris Lattner | ca354fa | 2008-11-17 19:51:54 +0000 | [diff] [blame] | 669 | case LV_InvalidExpression: |
| 670 | // If the top level is a C-style cast, and the subexpression is a valid |
| 671 | // lvalue, then this is probably a use of the old-school "cast as lvalue" |
| 672 | // GCC extension. We don't support it, but we want to produce good |
| 673 | // diagnostics when it happens so that the user knows why. |
| 674 | if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(this)) |
| 675 | if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) |
| 676 | return MLV_LValueCast; |
| 677 | return MLV_InvalidExpression; |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 678 | case LV_MemberFunction: return MLV_MemberFunction; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 679 | } |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 680 | |
| 681 | QualType CT = Ctx.getCanonicalType(getType()); |
| 682 | |
| 683 | if (CT.isConstQualified()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 684 | return MLV_ConstQualified; |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 685 | if (CT->isArrayType()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 686 | return MLV_ArrayType; |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 687 | if (CT->isIncompleteType()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 688 | return MLV_IncompleteType; |
| 689 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 690 | if (const RecordType *r = CT->getAsRecordType()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 691 | if (r->hasConstFields()) |
| 692 | return MLV_ConstQualified; |
| 693 | } |
Steve Naroff | 4f6a7d7 | 2008-09-26 14:41:28 +0000 | [diff] [blame] | 694 | // The following is illegal: |
| 695 | // void takeclosure(void (^C)(void)); |
| 696 | // void func() { int x = 1; takeclosure(^{ x = 7 }); } |
| 697 | // |
| 698 | if (getStmtClass() == BlockDeclRefExprClass) { |
| 699 | const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this); |
| 700 | if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl())) |
| 701 | return MLV_NotBlockQualified; |
| 702 | } |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 703 | |
Fariborz Jahanian | ba8d2d6 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 704 | // Assigning to an 'implicit' property? |
Fariborz Jahanian | 6669db9 | 2008-11-25 17:56:43 +0000 | [diff] [blame] | 705 | else if (getStmtClass() == ObjCKVCRefExprClass) { |
Fariborz Jahanian | ba8d2d6 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 706 | const ObjCKVCRefExpr* KVCExpr = cast<ObjCKVCRefExpr>(this); |
| 707 | if (KVCExpr->getSetterMethod() == 0) |
| 708 | return MLV_NoSetterProperty; |
| 709 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 710 | return MLV_Valid; |
| 711 | } |
| 712 | |
Ted Kremenek | 2e5f54a | 2008-02-27 18:39:48 +0000 | [diff] [blame] | 713 | /// hasGlobalStorage - Return true if this expression has static storage |
Chris Lattner | 4cc6271 | 2007-11-27 21:35:27 +0000 | [diff] [blame] | 714 | /// duration. This means that the address of this expression is a link-time |
| 715 | /// constant. |
Ted Kremenek | 2e5f54a | 2008-02-27 18:39:48 +0000 | [diff] [blame] | 716 | bool Expr::hasGlobalStorage() const { |
Chris Lattner | 1d09ecc | 2007-11-13 18:05:45 +0000 | [diff] [blame] | 717 | switch (getStmtClass()) { |
| 718 | default: |
| 719 | return false; |
Chris Lattner | 4cc6271 | 2007-11-27 21:35:27 +0000 | [diff] [blame] | 720 | case ParenExprClass: |
Ted Kremenek | 2e5f54a | 2008-02-27 18:39:48 +0000 | [diff] [blame] | 721 | return cast<ParenExpr>(this)->getSubExpr()->hasGlobalStorage(); |
Chris Lattner | 4cc6271 | 2007-11-27 21:35:27 +0000 | [diff] [blame] | 722 | case ImplicitCastExprClass: |
Ted Kremenek | 2e5f54a | 2008-02-27 18:39:48 +0000 | [diff] [blame] | 723 | return cast<ImplicitCastExpr>(this)->getSubExpr()->hasGlobalStorage(); |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 724 | case CompoundLiteralExprClass: |
| 725 | return cast<CompoundLiteralExpr>(this)->isFileScope(); |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 726 | case DeclRefExprClass: |
| 727 | case QualifiedDeclRefExprClass: { |
Chris Lattner | 1d09ecc | 2007-11-13 18:05:45 +0000 | [diff] [blame] | 728 | const Decl *D = cast<DeclRefExpr>(this)->getDecl(); |
| 729 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) |
Ted Kremenek | 2e5f54a | 2008-02-27 18:39:48 +0000 | [diff] [blame] | 730 | return VD->hasGlobalStorage(); |
Seo Sanghyeon | 63f067f | 2008-04-04 09:45:30 +0000 | [diff] [blame] | 731 | if (isa<FunctionDecl>(D)) |
| 732 | return true; |
Chris Lattner | 1d09ecc | 2007-11-13 18:05:45 +0000 | [diff] [blame] | 733 | return false; |
| 734 | } |
Chris Lattner | fb70806 | 2007-11-28 04:30:09 +0000 | [diff] [blame] | 735 | case MemberExprClass: { |
Chris Lattner | 1d09ecc | 2007-11-13 18:05:45 +0000 | [diff] [blame] | 736 | const MemberExpr *M = cast<MemberExpr>(this); |
Ted Kremenek | 2e5f54a | 2008-02-27 18:39:48 +0000 | [diff] [blame] | 737 | return !M->isArrow() && M->getBase()->hasGlobalStorage(); |
Chris Lattner | fb70806 | 2007-11-28 04:30:09 +0000 | [diff] [blame] | 738 | } |
Chris Lattner | 4cc6271 | 2007-11-27 21:35:27 +0000 | [diff] [blame] | 739 | case ArraySubscriptExprClass: |
Ted Kremenek | 2e5f54a | 2008-02-27 18:39:48 +0000 | [diff] [blame] | 740 | return cast<ArraySubscriptExpr>(this)->getBase()->hasGlobalStorage(); |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 741 | case PredefinedExprClass: |
Chris Lattner | fa28b30 | 2008-01-12 08:14:25 +0000 | [diff] [blame] | 742 | return true; |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 743 | case CXXDefaultArgExprClass: |
| 744 | return cast<CXXDefaultArgExpr>(this)->getExpr()->hasGlobalStorage(); |
Chris Lattner | 1d09ecc | 2007-11-13 18:05:45 +0000 | [diff] [blame] | 745 | } |
| 746 | } |
| 747 | |
Ted Kremenek | 4e99a5f | 2008-01-17 16:57:34 +0000 | [diff] [blame] | 748 | Expr* Expr::IgnoreParens() { |
| 749 | Expr* E = this; |
| 750 | while (ParenExpr* P = dyn_cast<ParenExpr>(E)) |
| 751 | E = P->getSubExpr(); |
| 752 | |
| 753 | return E; |
| 754 | } |
| 755 | |
Chris Lattner | 56f3494 | 2008-02-13 01:02:39 +0000 | [diff] [blame] | 756 | /// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr |
| 757 | /// or CastExprs or ImplicitCastExprs, returning their operand. |
| 758 | Expr *Expr::IgnoreParenCasts() { |
| 759 | Expr *E = this; |
| 760 | while (true) { |
| 761 | if (ParenExpr *P = dyn_cast<ParenExpr>(E)) |
| 762 | E = P->getSubExpr(); |
| 763 | else if (CastExpr *P = dyn_cast<CastExpr>(E)) |
| 764 | E = P->getSubExpr(); |
Chris Lattner | 56f3494 | 2008-02-13 01:02:39 +0000 | [diff] [blame] | 765 | else |
| 766 | return E; |
| 767 | } |
| 768 | } |
| 769 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 770 | /// hasAnyTypeDependentArguments - Determines if any of the expressions |
| 771 | /// in Exprs is type-dependent. |
| 772 | bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) { |
| 773 | for (unsigned I = 0; I < NumExprs; ++I) |
| 774 | if (Exprs[I]->isTypeDependent()) |
| 775 | return true; |
| 776 | |
| 777 | return false; |
| 778 | } |
| 779 | |
| 780 | /// hasAnyValueDependentArguments - Determines if any of the expressions |
| 781 | /// in Exprs is value-dependent. |
| 782 | bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) { |
| 783 | for (unsigned I = 0; I < NumExprs; ++I) |
| 784 | if (Exprs[I]->isValueDependent()) |
| 785 | return true; |
| 786 | |
| 787 | return false; |
| 788 | } |
| 789 | |
Eli Friedman | c9e8f60 | 2009-01-25 02:32:41 +0000 | [diff] [blame] | 790 | bool Expr::isConstantInitializer(ASTContext &Ctx) const { |
Eli Friedman | c39dc9a | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 791 | // This function is attempting whether an expression is an initializer |
| 792 | // which can be evaluated at compile-time. isEvaluatable handles most |
| 793 | // of the cases, but it can't deal with some initializer-specific |
| 794 | // expressions, and it can't deal with aggregates; we deal with those here, |
| 795 | // and fall back to isEvaluatable for the other cases. |
| 796 | |
Eli Friedman | 1f4a6db | 2009-02-20 02:36:22 +0000 | [diff] [blame] | 797 | // FIXME: This function assumes the variable being assigned to |
| 798 | // isn't a reference type! |
| 799 | |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 800 | switch (getStmtClass()) { |
Eli Friedman | c39dc9a | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 801 | default: break; |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 802 | case StringLiteralClass: |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 803 | return true; |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 804 | case CompoundLiteralExprClass: { |
Eli Friedman | 1f4a6db | 2009-02-20 02:36:22 +0000 | [diff] [blame] | 805 | // This handles gcc's extension that allows global initializers like |
| 806 | // "struct x {int x;} x = (struct x) {};". |
| 807 | // FIXME: This accepts other cases it shouldn't! |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 808 | const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer(); |
Eli Friedman | c9e8f60 | 2009-01-25 02:32:41 +0000 | [diff] [blame] | 809 | return Exp->isConstantInitializer(Ctx); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 810 | } |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 811 | case InitListExprClass: { |
Eli Friedman | 1f4a6db | 2009-02-20 02:36:22 +0000 | [diff] [blame] | 812 | // FIXME: This doesn't deal with fields with reference types correctly. |
| 813 | // FIXME: This incorrectly allows pointers cast to integers to be assigned |
| 814 | // to bitfields. |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 815 | const InitListExpr *Exp = cast<InitListExpr>(this); |
| 816 | unsigned numInits = Exp->getNumInits(); |
| 817 | for (unsigned i = 0; i < numInits; i++) { |
Eli Friedman | c9e8f60 | 2009-01-25 02:32:41 +0000 | [diff] [blame] | 818 | if (!Exp->getInit(i)->isConstantInitializer(Ctx)) |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 819 | return false; |
| 820 | } |
Eli Friedman | c39dc9a | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 821 | return true; |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 822 | } |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 823 | case ImplicitValueInitExprClass: |
| 824 | return true; |
Eli Friedman | c39dc9a | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 825 | case ParenExprClass: { |
| 826 | return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx); |
| 827 | } |
| 828 | case UnaryOperatorClass: { |
| 829 | const UnaryOperator* Exp = cast<UnaryOperator>(this); |
| 830 | if (Exp->getOpcode() == UnaryOperator::Extension) |
| 831 | return Exp->getSubExpr()->isConstantInitializer(Ctx); |
| 832 | break; |
| 833 | } |
| 834 | case CStyleCastExprClass: |
| 835 | // Handle casts with a destination that's a struct or union; this |
| 836 | // deals with both the gcc no-op struct cast extension and the |
| 837 | // cast-to-union extension. |
| 838 | if (getType()->isRecordType()) |
| 839 | return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx); |
| 840 | break; |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 841 | } |
| 842 | |
Eli Friedman | c39dc9a | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 843 | return isEvaluatable(Ctx); |
Steve Naroff | 38374b0 | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 844 | } |
| 845 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 846 | /// isIntegerConstantExpr - this recursive routine will test if an expression is |
| 847 | /// an integer constant expression. Note: With the introduction of VLA's in |
| 848 | /// C99 the result of the sizeof operator is no longer always a constant |
| 849 | /// expression. The generalization of the wording to include any subexpression |
| 850 | /// that is not evaluated (C99 6.6p3) means that nonconstant subexpressions |
| 851 | /// can appear as operands to other operators (e.g. &&, ||, ?:). For instance, |
Nuno Lopes | 5f6b632 | 2008-07-08 21:13:06 +0000 | [diff] [blame] | 852 | /// "0 || f()" can be treated as a constant expression. In C90 this expression, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 853 | /// occurring in a context requiring a constant, would have been a constraint |
| 854 | /// violation. FIXME: This routine currently implements C90 semantics. |
| 855 | /// To properly implement C99 semantics this routine will need to evaluate |
| 856 | /// expressions involving operators previously mentioned. |
| 857 | |
| 858 | /// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero, |
| 859 | /// comma, etc |
| 860 | /// |
| 861 | /// FIXME: This should ext-warn on overflow during evaluation! ISO C does not |
Chris Lattner | ccc213f | 2007-09-26 00:47:26 +0000 | [diff] [blame] | 862 | /// permit this. This includes things like (int)1e1000 |
Chris Lattner | ce0afc0 | 2007-07-18 05:21:20 +0000 | [diff] [blame] | 863 | /// |
| 864 | /// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof |
| 865 | /// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer |
| 866 | /// cast+dereference. |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 867 | bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx, |
| 868 | SourceLocation *Loc, bool isEvaluated) const { |
Daniel Dunbar | 2d6744f | 2009-02-18 00:47:45 +0000 | [diff] [blame] | 869 | if (!isIntegerConstantExprInternal(Result, Ctx, Loc, isEvaluated)) |
| 870 | return false; |
| 871 | assert(Result == EvaluateAsInt(Ctx) && "Inconsistent Evaluate() result!"); |
| 872 | return true; |
| 873 | } |
| 874 | |
| 875 | bool Expr::isIntegerConstantExprInternal(llvm::APSInt &Result, ASTContext &Ctx, |
| 876 | SourceLocation *Loc, bool isEvaluated) const { |
| 877 | |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 878 | // Pretest for integral type; some parts of the code crash for types that |
| 879 | // can't be sized. |
| 880 | if (!getType()->isIntegralType()) { |
| 881 | if (Loc) *Loc = getLocStart(); |
| 882 | return false; |
| 883 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 884 | switch (getStmtClass()) { |
| 885 | default: |
| 886 | if (Loc) *Loc = getLocStart(); |
| 887 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 888 | case ParenExprClass: |
| 889 | return cast<ParenExpr>(this)->getSubExpr()-> |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 890 | isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 891 | case IntegerLiteralClass: |
Daniel Dunbar | 2d6744f | 2009-02-18 00:47:45 +0000 | [diff] [blame] | 892 | // NOTE: getValue() returns an APInt, we must set sign. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 893 | Result = cast<IntegerLiteral>(this)->getValue(); |
Daniel Dunbar | a135975 | 2009-02-18 00:32:53 +0000 | [diff] [blame] | 894 | Result.setIsUnsigned(getType()->isUnsignedIntegerType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 895 | break; |
Chris Lattner | 2eadfb6 | 2007-07-15 23:32:58 +0000 | [diff] [blame] | 896 | case CharacterLiteralClass: { |
| 897 | const CharacterLiteral *CL = cast<CharacterLiteral>(this); |
Daniel Dunbar | a135975 | 2009-02-18 00:32:53 +0000 | [diff] [blame] | 898 | Result = Ctx.MakeIntValue(CL->getValue(), getType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 899 | break; |
Chris Lattner | 2eadfb6 | 2007-07-15 23:32:58 +0000 | [diff] [blame] | 900 | } |
Anders Carlsson | b88d45e | 2008-08-23 21:12:35 +0000 | [diff] [blame] | 901 | case CXXBoolLiteralExprClass: { |
| 902 | const CXXBoolLiteralExpr *BL = cast<CXXBoolLiteralExpr>(this); |
Daniel Dunbar | a135975 | 2009-02-18 00:32:53 +0000 | [diff] [blame] | 903 | Result = Ctx.MakeIntValue(BL->getValue(), getType()); |
Anders Carlsson | b88d45e | 2008-08-23 21:12:35 +0000 | [diff] [blame] | 904 | break; |
| 905 | } |
Argyrios Kyrtzidis | 7267f78 | 2008-08-23 19:35:47 +0000 | [diff] [blame] | 906 | case CXXZeroInitValueExprClass: |
Daniel Dunbar | a135975 | 2009-02-18 00:32:53 +0000 | [diff] [blame] | 907 | Result = Ctx.MakeIntValue(0, getType()); |
Argyrios Kyrtzidis | 7267f78 | 2008-08-23 19:35:47 +0000 | [diff] [blame] | 908 | break; |
Steve Naroff | 7b658aa | 2007-08-02 04:09:23 +0000 | [diff] [blame] | 909 | case TypesCompatibleExprClass: { |
| 910 | const TypesCompatibleExpr *TCE = cast<TypesCompatibleExpr>(this); |
Daniel Dunbar | ac620de | 2008-10-24 08:07:57 +0000 | [diff] [blame] | 911 | // Per gcc docs "this built-in function ignores top level |
| 912 | // qualifiers". We need to use the canonical version to properly |
| 913 | // be able to strip CRV qualifiers from the type. |
| 914 | QualType T0 = Ctx.getCanonicalType(TCE->getArgType1()); |
| 915 | QualType T1 = Ctx.getCanonicalType(TCE->getArgType2()); |
Daniel Dunbar | a135975 | 2009-02-18 00:32:53 +0000 | [diff] [blame] | 916 | Result = Ctx.MakeIntValue(Ctx.typesAreCompatible(T0.getUnqualifiedType(), |
| 917 | T1.getUnqualifiedType()), |
| 918 | getType()); |
Steve Naroff | 389cecc | 2007-08-02 00:13:27 +0000 | [diff] [blame] | 919 | break; |
Steve Naroff | 7b658aa | 2007-08-02 04:09:23 +0000 | [diff] [blame] | 920 | } |
Douglas Gregor | b460980 | 2008-11-14 16:09:21 +0000 | [diff] [blame] | 921 | case CallExprClass: |
| 922 | case CXXOperatorCallExprClass: { |
Steve Naroff | 13b7c5f | 2007-08-08 22:15:55 +0000 | [diff] [blame] | 923 | const CallExpr *CE = cast<CallExpr>(this); |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 924 | |
| 925 | // If this is a call to a builtin function, constant fold it otherwise |
| 926 | // reject it. |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 927 | if (CE->isBuiltinCall(Ctx)) { |
Anders Carlsson | 1c0cfd4 | 2008-12-19 20:58:05 +0000 | [diff] [blame] | 928 | EvalResult EvalResult; |
| 929 | if (CE->Evaluate(EvalResult, Ctx)) { |
| 930 | assert(!EvalResult.HasSideEffects && |
| 931 | "Foldable builtin call should not have side effects!"); |
| 932 | Result = EvalResult.Val.getInt(); |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 933 | break; // It is a constant, expand it. |
| 934 | } |
| 935 | } |
| 936 | |
Steve Naroff | 13b7c5f | 2007-08-08 22:15:55 +0000 | [diff] [blame] | 937 | if (Loc) *Loc = getLocStart(); |
| 938 | return false; |
| 939 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 940 | case DeclRefExprClass: |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 941 | case QualifiedDeclRefExprClass: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 942 | if (const EnumConstantDecl *D = |
| 943 | dyn_cast<EnumConstantDecl>(cast<DeclRefExpr>(this)->getDecl())) { |
| 944 | Result = D->getInitVal(); |
| 945 | break; |
| 946 | } |
Sebastian Redl | 4a4251b | 2009-02-07 13:06:23 +0000 | [diff] [blame] | 947 | if (Ctx.getLangOptions().CPlusPlus && |
| 948 | getType().getCVRQualifiers() == QualType::Const) { |
| 949 | // C++ 7.1.5.1p2 |
| 950 | // A variable of non-volatile const-qualified integral or enumeration |
| 951 | // type initialized by an ICE can be used in ICEs. |
| 952 | if (const VarDecl *Dcl = |
| 953 | dyn_cast<VarDecl>(cast<DeclRefExpr>(this)->getDecl())) { |
| 954 | if (const Expr *Init = Dcl->getInit()) |
| 955 | return Init->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated); |
| 956 | } |
| 957 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 958 | if (Loc) *Loc = getLocStart(); |
| 959 | return false; |
| 960 | case UnaryOperatorClass: { |
| 961 | const UnaryOperator *Exp = cast<UnaryOperator>(this); |
| 962 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 963 | // Get the operand value. If this is offsetof, do not evalute the |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 964 | // operand. This affects C99 6.6p3. |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 965 | if (!Exp->isOffsetOfOp() && !Exp->getSubExpr()-> |
| 966 | isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 967 | return false; |
| 968 | |
| 969 | switch (Exp->getOpcode()) { |
| 970 | // Address, indirect, pre/post inc/dec, etc are not valid constant exprs. |
| 971 | // See C99 6.6p3. |
| 972 | default: |
| 973 | if (Loc) *Loc = Exp->getOperatorLoc(); |
| 974 | return false; |
| 975 | case UnaryOperator::Extension: |
Chris Lattner | 76e773a | 2007-07-18 18:38:36 +0000 | [diff] [blame] | 976 | return true; // FIXME: this is wrong. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 977 | case UnaryOperator::LNot: { |
Daniel Dunbar | a135975 | 2009-02-18 00:32:53 +0000 | [diff] [blame] | 978 | Result = Ctx.MakeIntValue(Result == 0, getType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 979 | break; |
| 980 | } |
| 981 | case UnaryOperator::Plus: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 982 | break; |
| 983 | case UnaryOperator::Minus: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 984 | Result = -Result; |
| 985 | break; |
| 986 | case UnaryOperator::Not: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 987 | Result = ~Result; |
| 988 | break; |
Anders Carlsson | 5a1deb8 | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 989 | case UnaryOperator::OffsetOf: |
Daniel Dunbar | a135975 | 2009-02-18 00:32:53 +0000 | [diff] [blame] | 990 | Result = Ctx.MakeIntValue(Exp->evaluateOffsetOf(Ctx), getType()); |
| 991 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 992 | } |
| 993 | break; |
| 994 | } |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 995 | case SizeOfAlignOfExprClass: { |
| 996 | const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(this); |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 997 | QualType ArgTy = Exp->getTypeOfArgument(); |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 998 | |
| 999 | // alignof is always an ICE; sizeof is an ICE if and only if |
| 1000 | // the operand isn't a VLA |
| 1001 | if (Exp->isSizeOf() && ArgTy->isVariableArrayType()) { |
Chris Lattner | 6538347 | 2007-12-18 07:15:40 +0000 | [diff] [blame] | 1002 | if (Loc) *Loc = Exp->getOperatorLoc(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1003 | return false; |
Chris Lattner | 6538347 | 2007-12-18 07:15:40 +0000 | [diff] [blame] | 1004 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1005 | |
Eli Friedman | dcdafb6 | 2009-02-22 02:56:25 +0000 | [diff] [blame] | 1006 | // Use the Evaluate logic to calculate the value, since the |
| 1007 | // calculation is non-trivial. |
| 1008 | Result = EvaluateAsInt(Ctx); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1009 | break; |
| 1010 | } |
| 1011 | case BinaryOperatorClass: { |
| 1012 | const BinaryOperator *Exp = cast<BinaryOperator>(this); |
Daniel Dunbar | e1226d2 | 2008-09-22 23:53:24 +0000 | [diff] [blame] | 1013 | llvm::APSInt LHS, RHS; |
| 1014 | |
| 1015 | // Initialize result to have correct signedness and width. |
Daniel Dunbar | a135975 | 2009-02-18 00:32:53 +0000 | [diff] [blame] | 1016 | Result = Ctx.MakeIntValue(0, getType()); |
Eli Friedman | b11e778 | 2008-11-13 02:13:11 +0000 | [diff] [blame] | 1017 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1018 | // The LHS of a constant expr is always evaluated and needed. |
Daniel Dunbar | e1226d2 | 2008-09-22 23:53:24 +0000 | [diff] [blame] | 1019 | if (!Exp->getLHS()->isIntegerConstantExpr(LHS, Ctx, Loc, isEvaluated)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1020 | return false; |
| 1021 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1022 | // The short-circuiting &&/|| operators don't necessarily evaluate their |
| 1023 | // RHS. Make sure to pass isEvaluated down correctly. |
| 1024 | if (Exp->isLogicalOp()) { |
| 1025 | bool RHSEval; |
| 1026 | if (Exp->getOpcode() == BinaryOperator::LAnd) |
Daniel Dunbar | e1226d2 | 2008-09-22 23:53:24 +0000 | [diff] [blame] | 1027 | RHSEval = LHS != 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1028 | else { |
| 1029 | assert(Exp->getOpcode() == BinaryOperator::LOr &&"Unexpected logical"); |
Daniel Dunbar | e1226d2 | 2008-09-22 23:53:24 +0000 | [diff] [blame] | 1030 | RHSEval = LHS == 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 1033 | if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1034 | isEvaluated & RHSEval)) |
| 1035 | return false; |
| 1036 | } else { |
Chris Lattner | 590b664 | 2007-07-15 23:26:56 +0000 | [diff] [blame] | 1037 | if (!Exp->getRHS()->isIntegerConstantExpr(RHS, Ctx, Loc, isEvaluated)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1038 | return false; |
| 1039 | } |
| 1040 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1041 | switch (Exp->getOpcode()) { |
| 1042 | default: |
| 1043 | if (Loc) *Loc = getLocStart(); |
| 1044 | return false; |
| 1045 | case BinaryOperator::Mul: |
Daniel Dunbar | e1226d2 | 2008-09-22 23:53:24 +0000 | [diff] [blame] | 1046 | Result = LHS * RHS; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1047 | break; |
| 1048 | case BinaryOperator::Div: |
| 1049 | if (RHS == 0) { |
| 1050 | if (!isEvaluated) break; |
| 1051 | if (Loc) *Loc = getLocStart(); |
| 1052 | return false; |
| 1053 | } |
Daniel Dunbar | e1226d2 | 2008-09-22 23:53:24 +0000 | [diff] [blame] | 1054 | Result = LHS / RHS; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1055 | break; |
| 1056 | case BinaryOperator::Rem: |
| 1057 | if (RHS == 0) { |
| 1058 | if (!isEvaluated) break; |
| 1059 | if (Loc) *Loc = getLocStart(); |
| 1060 | return false; |
| 1061 | } |
Daniel Dunbar | e1226d2 | 2008-09-22 23:53:24 +0000 | [diff] [blame] | 1062 | Result = LHS % RHS; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1063 | break; |
Daniel Dunbar | e1226d2 | 2008-09-22 23:53:24 +0000 | [diff] [blame] | 1064 | case BinaryOperator::Add: Result = LHS + RHS; break; |
| 1065 | case BinaryOperator::Sub: Result = LHS - RHS; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1066 | case BinaryOperator::Shl: |
Daniel Dunbar | e1226d2 | 2008-09-22 23:53:24 +0000 | [diff] [blame] | 1067 | Result = LHS << |
| 1068 | static_cast<uint32_t>(RHS.getLimitedValue(LHS.getBitWidth()-1)); |
| 1069 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1070 | case BinaryOperator::Shr: |
Daniel Dunbar | e1226d2 | 2008-09-22 23:53:24 +0000 | [diff] [blame] | 1071 | Result = LHS >> |
| 1072 | static_cast<uint32_t>(RHS.getLimitedValue(LHS.getBitWidth()-1)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1073 | break; |
Daniel Dunbar | e1226d2 | 2008-09-22 23:53:24 +0000 | [diff] [blame] | 1074 | case BinaryOperator::LT: Result = LHS < RHS; break; |
| 1075 | case BinaryOperator::GT: Result = LHS > RHS; break; |
| 1076 | case BinaryOperator::LE: Result = LHS <= RHS; break; |
| 1077 | case BinaryOperator::GE: Result = LHS >= RHS; break; |
| 1078 | case BinaryOperator::EQ: Result = LHS == RHS; break; |
| 1079 | case BinaryOperator::NE: Result = LHS != RHS; break; |
| 1080 | case BinaryOperator::And: Result = LHS & RHS; break; |
| 1081 | case BinaryOperator::Xor: Result = LHS ^ RHS; break; |
| 1082 | case BinaryOperator::Or: Result = LHS | RHS; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1083 | case BinaryOperator::LAnd: |
Daniel Dunbar | e1226d2 | 2008-09-22 23:53:24 +0000 | [diff] [blame] | 1084 | Result = LHS != 0 && RHS != 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1085 | break; |
| 1086 | case BinaryOperator::LOr: |
Daniel Dunbar | e1226d2 | 2008-09-22 23:53:24 +0000 | [diff] [blame] | 1087 | Result = LHS != 0 || RHS != 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1088 | break; |
Eli Friedman | b11e778 | 2008-11-13 02:13:11 +0000 | [diff] [blame] | 1089 | |
| 1090 | case BinaryOperator::Comma: |
| 1091 | // C99 6.6p3: "shall not contain assignment, ..., or comma operators, |
| 1092 | // *except* when they are contained within a subexpression that is not |
| 1093 | // evaluated". Note that Assignment can never happen due to constraints |
| 1094 | // on the LHS subexpr, so we don't need to check it here. |
| 1095 | if (isEvaluated) { |
| 1096 | if (Loc) *Loc = getLocStart(); |
| 1097 | return false; |
| 1098 | } |
| 1099 | |
| 1100 | // The result of the constant expr is the RHS. |
| 1101 | Result = RHS; |
Daniel Dunbar | 2d6744f | 2009-02-18 00:47:45 +0000 | [diff] [blame] | 1102 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1103 | } |
Daniel Dunbar | a135975 | 2009-02-18 00:32:53 +0000 | [diff] [blame] | 1104 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1105 | assert(!Exp->isAssignmentOp() && "LHS can't be a constant expr!"); |
| 1106 | break; |
| 1107 | } |
Chris Lattner | 26dc7b3 | 2007-07-15 23:54:50 +0000 | [diff] [blame] | 1108 | case ImplicitCastExprClass: |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 1109 | case CStyleCastExprClass: |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 1110 | case CXXFunctionalCastExprClass: { |
Argyrios Kyrtzidis | 0835a3c | 2008-08-18 23:01:59 +0000 | [diff] [blame] | 1111 | const Expr *SubExpr = cast<CastExpr>(this)->getSubExpr(); |
| 1112 | SourceLocation CastLoc = getLocStart(); |
Chris Lattner | 26dc7b3 | 2007-07-15 23:54:50 +0000 | [diff] [blame] | 1113 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1114 | // C99 6.6p6: shall only convert arithmetic types to integer types. |
Chris Lattner | 26dc7b3 | 2007-07-15 23:54:50 +0000 | [diff] [blame] | 1115 | if (!SubExpr->getType()->isArithmeticType() || |
| 1116 | !getType()->isIntegerType()) { |
| 1117 | if (Loc) *Loc = SubExpr->getLocStart(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1118 | return false; |
| 1119 | } |
Chris Lattner | 987b15d | 2007-09-22 19:04:13 +0000 | [diff] [blame] | 1120 | |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1121 | uint32_t DestWidth = static_cast<uint32_t>(Ctx.getTypeSize(getType())); |
Chris Lattner | 987b15d | 2007-09-22 19:04:13 +0000 | [diff] [blame] | 1122 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1123 | // Handle simple integer->integer casts. |
Chris Lattner | 26dc7b3 | 2007-07-15 23:54:50 +0000 | [diff] [blame] | 1124 | if (SubExpr->getType()->isIntegerType()) { |
| 1125 | if (!SubExpr->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1126 | return false; |
Chris Lattner | 26dc7b3 | 2007-07-15 23:54:50 +0000 | [diff] [blame] | 1127 | |
| 1128 | // Figure out if this is a truncate, extend or noop cast. |
Chris Lattner | 26dc7b3 | 2007-07-15 23:54:50 +0000 | [diff] [blame] | 1129 | // If the input is signed, do a sign extend, noop, or truncate. |
Chris Lattner | c0a356b | 2008-01-09 18:59:34 +0000 | [diff] [blame] | 1130 | if (getType()->isBooleanType()) { |
| 1131 | // Conversion to bool compares against zero. |
Daniel Dunbar | a135975 | 2009-02-18 00:32:53 +0000 | [diff] [blame] | 1132 | Result = Ctx.MakeIntValue(Result != 0, getType()); |
| 1133 | } else if (SubExpr->getType()->isSignedIntegerType()) { |
Chris Lattner | 26dc7b3 | 2007-07-15 23:54:50 +0000 | [diff] [blame] | 1134 | Result.sextOrTrunc(DestWidth); |
Daniel Dunbar | a135975 | 2009-02-18 00:32:53 +0000 | [diff] [blame] | 1135 | Result.setIsUnsigned(getType()->isUnsignedIntegerType()); |
| 1136 | } else { // If the input is unsigned, do a zero extend, noop, |
| 1137 | // or truncate. |
Chris Lattner | 26dc7b3 | 2007-07-15 23:54:50 +0000 | [diff] [blame] | 1138 | Result.zextOrTrunc(DestWidth); |
Daniel Dunbar | a135975 | 2009-02-18 00:32:53 +0000 | [diff] [blame] | 1139 | Result.setIsUnsigned(getType()->isUnsignedIntegerType()); |
| 1140 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1141 | break; |
| 1142 | } |
| 1143 | |
| 1144 | // Allow floating constants that are the immediate operands of casts or that |
| 1145 | // are parenthesized. |
Daniel Dunbar | a135975 | 2009-02-18 00:32:53 +0000 | [diff] [blame] | 1146 | const Expr *Operand = SubExpr->IgnoreParens(); |
Chris Lattner | 987b15d | 2007-09-22 19:04:13 +0000 | [diff] [blame] | 1147 | |
| 1148 | // If this isn't a floating literal, we can't handle it. |
| 1149 | const FloatingLiteral *FL = dyn_cast<FloatingLiteral>(Operand); |
| 1150 | if (!FL) { |
| 1151 | if (Loc) *Loc = Operand->getLocStart(); |
| 1152 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1153 | } |
Chris Lattner | c0a356b | 2008-01-09 18:59:34 +0000 | [diff] [blame] | 1154 | |
| 1155 | // If the destination is boolean, compare against zero. |
| 1156 | if (getType()->isBooleanType()) { |
Daniel Dunbar | a135975 | 2009-02-18 00:32:53 +0000 | [diff] [blame] | 1157 | Result = Ctx.MakeIntValue(!FL->getValue().isZero(), getType()); |
Chris Lattner | c0a356b | 2008-01-09 18:59:34 +0000 | [diff] [blame] | 1158 | break; |
| 1159 | } |
Chris Lattner | 987b15d | 2007-09-22 19:04:13 +0000 | [diff] [blame] | 1160 | |
| 1161 | // Determine whether we are converting to unsigned or signed. |
| 1162 | bool DestSigned = getType()->isSignedIntegerType(); |
Chris Lattner | ccc213f | 2007-09-26 00:47:26 +0000 | [diff] [blame] | 1163 | |
| 1164 | // TODO: Warn on overflow, but probably not here: isIntegerConstantExpr can |
| 1165 | // be called multiple times per AST. |
Dale Johannesen | ee5a700 | 2008-10-09 23:02:32 +0000 | [diff] [blame] | 1166 | uint64_t Space[4]; |
| 1167 | bool ignored; |
Chris Lattner | ccc213f | 2007-09-26 00:47:26 +0000 | [diff] [blame] | 1168 | (void)FL->getValue().convertToInteger(Space, DestWidth, DestSigned, |
Dale Johannesen | ee5a700 | 2008-10-09 23:02:32 +0000 | [diff] [blame] | 1169 | llvm::APFloat::rmTowardZero, |
| 1170 | &ignored); |
Chris Lattner | 987b15d | 2007-09-22 19:04:13 +0000 | [diff] [blame] | 1171 | Result = llvm::APInt(DestWidth, 4, Space); |
Daniel Dunbar | a135975 | 2009-02-18 00:32:53 +0000 | [diff] [blame] | 1172 | Result.setIsUnsigned(getType()->isUnsignedIntegerType()); |
Chris Lattner | 987b15d | 2007-09-22 19:04:13 +0000 | [diff] [blame] | 1173 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1174 | } |
| 1175 | case ConditionalOperatorClass: { |
| 1176 | const ConditionalOperator *Exp = cast<ConditionalOperator>(this); |
| 1177 | |
Chris Lattner | 28daa53 | 2008-12-12 06:55:44 +0000 | [diff] [blame] | 1178 | const Expr *Cond = Exp->getCond(); |
| 1179 | |
| 1180 | if (!Cond->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1181 | return false; |
| 1182 | |
| 1183 | const Expr *TrueExp = Exp->getLHS(); |
| 1184 | const Expr *FalseExp = Exp->getRHS(); |
| 1185 | if (Result == 0) std::swap(TrueExp, FalseExp); |
| 1186 | |
Chris Lattner | 28daa53 | 2008-12-12 06:55:44 +0000 | [diff] [blame] | 1187 | // If the condition (ignoring parens) is a __builtin_constant_p call, |
| 1188 | // then only the true side is actually considered in an integer constant |
Chris Lattner | 42b83dd | 2008-12-12 18:00:51 +0000 | [diff] [blame] | 1189 | // expression, and it is fully evaluated. This is an important GNU |
| 1190 | // extension. See GCC PR38377 for discussion. |
Chris Lattner | 28daa53 | 2008-12-12 06:55:44 +0000 | [diff] [blame] | 1191 | if (const CallExpr *CallCE = dyn_cast<CallExpr>(Cond->IgnoreParenCasts())) |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1192 | if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) { |
Chris Lattner | 42b83dd | 2008-12-12 18:00:51 +0000 | [diff] [blame] | 1193 | EvalResult EVResult; |
| 1194 | if (!Evaluate(EVResult, Ctx) || EVResult.HasSideEffects) |
| 1195 | return false; |
| 1196 | assert(EVResult.Val.isInt() && "FP conditional expr not expected"); |
| 1197 | Result = EVResult.Val.getInt(); |
| 1198 | if (Loc) *Loc = EVResult.DiagLoc; |
| 1199 | return true; |
| 1200 | } |
Chris Lattner | 28daa53 | 2008-12-12 06:55:44 +0000 | [diff] [blame] | 1201 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1202 | // Evaluate the false one first, discard the result. |
Daniel Dunbar | 2d6744f | 2009-02-18 00:47:45 +0000 | [diff] [blame] | 1203 | llvm::APSInt Tmp; |
| 1204 | if (FalseExp && !FalseExp->isIntegerConstantExpr(Tmp, Ctx, Loc, false)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1205 | return false; |
Daniel Dunbar | 2d6744f | 2009-02-18 00:47:45 +0000 | [diff] [blame] | 1206 | // Evalute the true one, capture the result. Note that if TrueExp |
| 1207 | // is False then this is an instant of the gcc missing LHS |
| 1208 | // extension, and we will just reuse Result. |
Anders Carlsson | 3907323 | 2007-11-30 19:04:31 +0000 | [diff] [blame] | 1209 | if (TrueExp && |
| 1210 | !TrueExp->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1211 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1212 | break; |
| 1213 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1214 | case CXXDefaultArgExprClass: |
| 1215 | return cast<CXXDefaultArgExpr>(this) |
| 1216 | ->isIntegerConstantExpr(Result, Ctx, Loc, isEvaluated); |
Sebastian Redl | 64b45f7 | 2009-01-05 20:52:13 +0000 | [diff] [blame] | 1217 | |
| 1218 | case UnaryTypeTraitExprClass: |
Daniel Dunbar | a135975 | 2009-02-18 00:32:53 +0000 | [diff] [blame] | 1219 | Result = Ctx.MakeIntValue(cast<UnaryTypeTraitExpr>(this)->EvaluateTrait(), |
| 1220 | getType()); |
| 1221 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1224 | return true; |
| 1225 | } |
| 1226 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1227 | /// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an |
| 1228 | /// integer constant expression with the value zero, or if this is one that is |
| 1229 | /// cast to void*. |
Anders Carlsson | efa9b38 | 2008-12-01 02:13:57 +0000 | [diff] [blame] | 1230 | bool Expr::isNullPointerConstant(ASTContext &Ctx) const |
| 1231 | { |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1232 | // Strip off a cast to void*, if it exists. Except in C++. |
Argyrios Kyrtzidis | 0835a3c | 2008-08-18 23:01:59 +0000 | [diff] [blame] | 1233 | if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) { |
Sebastian Redl | 6215dee | 2008-11-04 11:45:54 +0000 | [diff] [blame] | 1234 | if (!Ctx.getLangOptions().CPlusPlus) { |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1235 | // Check that it is a cast to void*. |
| 1236 | if (const PointerType *PT = CE->getType()->getAsPointerType()) { |
| 1237 | QualType Pointee = PT->getPointeeType(); |
| 1238 | if (Pointee.getCVRQualifiers() == 0 && |
| 1239 | Pointee->isVoidType() && // to void* |
| 1240 | CE->getSubExpr()->getType()->isIntegerType()) // from int. |
Anders Carlsson | d265277 | 2008-12-01 06:28:23 +0000 | [diff] [blame] | 1241 | return CE->getSubExpr()->isNullPointerConstant(Ctx); |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1242 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1243 | } |
Steve Naroff | aa58f00 | 2008-01-14 16:10:57 +0000 | [diff] [blame] | 1244 | } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) { |
| 1245 | // Ignore the ImplicitCastExpr type entirely. |
Anders Carlsson | d265277 | 2008-12-01 06:28:23 +0000 | [diff] [blame] | 1246 | return ICE->getSubExpr()->isNullPointerConstant(Ctx); |
Steve Naroff | aa58f00 | 2008-01-14 16:10:57 +0000 | [diff] [blame] | 1247 | } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) { |
| 1248 | // Accept ((void*)0) as a null pointer constant, as many other |
| 1249 | // implementations do. |
Anders Carlsson | d265277 | 2008-12-01 06:28:23 +0000 | [diff] [blame] | 1250 | return PE->getSubExpr()->isNullPointerConstant(Ctx); |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1251 | } else if (const CXXDefaultArgExpr *DefaultArg |
| 1252 | = dyn_cast<CXXDefaultArgExpr>(this)) { |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1253 | // See through default argument expressions |
Anders Carlsson | d265277 | 2008-12-01 06:28:23 +0000 | [diff] [blame] | 1254 | return DefaultArg->getExpr()->isNullPointerConstant(Ctx); |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 1255 | } else if (isa<GNUNullExpr>(this)) { |
| 1256 | // The GNU __null extension is always a null pointer constant. |
| 1257 | return true; |
Steve Naroff | aaffbf7 | 2008-01-14 02:53:34 +0000 | [diff] [blame] | 1258 | } |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 1259 | |
Steve Naroff | aa58f00 | 2008-01-14 16:10:57 +0000 | [diff] [blame] | 1260 | // This expression must be an integer type. |
| 1261 | if (!getType()->isIntegerType()) |
| 1262 | return false; |
| 1263 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1264 | // If we have an integer constant expression, we need to *evaluate* it and |
| 1265 | // test for the value 0. |
Anders Carlsson | d265277 | 2008-12-01 06:28:23 +0000 | [diff] [blame] | 1266 | // FIXME: We should probably return false if we're compiling in strict mode |
| 1267 | // and Diag is not null (this indicates that the value was foldable but not |
| 1268 | // an ICE. |
| 1269 | EvalResult Result; |
Anders Carlsson | efa9b38 | 2008-12-01 02:13:57 +0000 | [diff] [blame] | 1270 | return Evaluate(Result, Ctx) && !Result.HasSideEffects && |
Anders Carlsson | d265277 | 2008-12-01 06:28:23 +0000 | [diff] [blame] | 1271 | Result.Val.isInt() && Result.Val.getInt() == 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1272 | } |
Steve Naroff | 31a4584 | 2007-07-28 23:10:27 +0000 | [diff] [blame] | 1273 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1274 | /// isBitField - Return true if this expression is a bit-field. |
| 1275 | bool Expr::isBitField() { |
| 1276 | Expr *E = this->IgnoreParenCasts(); |
| 1277 | if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E)) |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1278 | if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl())) |
| 1279 | return Field->isBitField(); |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1280 | return false; |
| 1281 | } |
| 1282 | |
Chris Lattner | 2140e90 | 2009-02-16 22:14:05 +0000 | [diff] [blame] | 1283 | /// isArrow - Return true if the base expression is a pointer to vector, |
| 1284 | /// return false if the base expression is a vector. |
| 1285 | bool ExtVectorElementExpr::isArrow() const { |
| 1286 | return getBase()->getType()->isPointerType(); |
| 1287 | } |
| 1288 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1289 | unsigned ExtVectorElementExpr::getNumElements() const { |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1290 | if (const VectorType *VT = getType()->getAsVectorType()) |
| 1291 | return VT->getNumElements(); |
| 1292 | return 1; |
Chris Lattner | 4d0ac88 | 2007-08-03 16:00:20 +0000 | [diff] [blame] | 1293 | } |
| 1294 | |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1295 | /// containsDuplicateElements - Return true if any element access is repeated. |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1296 | bool ExtVectorElementExpr::containsDuplicateElements() const { |
Steve Naroff | fec0b49 | 2007-07-30 03:29:09 +0000 | [diff] [blame] | 1297 | const char *compStr = Accessor.getName(); |
Chris Lattner | 7e3e9b1 | 2008-11-19 07:55:04 +0000 | [diff] [blame] | 1298 | unsigned length = Accessor.getLength(); |
Nate Begeman | 190d6a2 | 2009-01-18 02:01:21 +0000 | [diff] [blame] | 1299 | |
| 1300 | // Halving swizzles do not contain duplicate elements. |
| 1301 | if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") || |
| 1302 | !strcmp(compStr, "even") || !strcmp(compStr, "odd")) |
| 1303 | return false; |
| 1304 | |
| 1305 | // Advance past s-char prefix on hex swizzles. |
| 1306 | if (*compStr == 's') { |
| 1307 | compStr++; |
| 1308 | length--; |
| 1309 | } |
Steve Naroff | fec0b49 | 2007-07-30 03:29:09 +0000 | [diff] [blame] | 1310 | |
Chris Lattner | 7e3e9b1 | 2008-11-19 07:55:04 +0000 | [diff] [blame] | 1311 | for (unsigned i = 0; i != length-1; i++) { |
Steve Naroff | fec0b49 | 2007-07-30 03:29:09 +0000 | [diff] [blame] | 1312 | const char *s = compStr+i; |
| 1313 | for (const char c = *s++; *s; s++) |
| 1314 | if (c == *s) |
| 1315 | return true; |
| 1316 | } |
| 1317 | return false; |
| 1318 | } |
Chris Lattner | b8f849d | 2007-08-02 23:36:59 +0000 | [diff] [blame] | 1319 | |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1320 | /// getEncodedElementAccess - We encode the fields as a llvm ConstantArray. |
Nate Begeman | 3b8d116 | 2008-05-13 21:03:02 +0000 | [diff] [blame] | 1321 | void ExtVectorElementExpr::getEncodedElementAccess( |
| 1322 | llvm::SmallVectorImpl<unsigned> &Elts) const { |
Chris Lattner | 7e3e9b1 | 2008-11-19 07:55:04 +0000 | [diff] [blame] | 1323 | const char *compStr = Accessor.getName(); |
Nate Begeman | 353417a | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 1324 | if (*compStr == 's') |
| 1325 | compStr++; |
| 1326 | |
| 1327 | bool isHi = !strcmp(compStr, "hi"); |
| 1328 | bool isLo = !strcmp(compStr, "lo"); |
| 1329 | bool isEven = !strcmp(compStr, "even"); |
| 1330 | bool isOdd = !strcmp(compStr, "odd"); |
| 1331 | |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1332 | for (unsigned i = 0, e = getNumElements(); i != e; ++i) { |
| 1333 | uint64_t Index; |
| 1334 | |
| 1335 | if (isHi) |
| 1336 | Index = e + i; |
| 1337 | else if (isLo) |
| 1338 | Index = i; |
| 1339 | else if (isEven) |
| 1340 | Index = 2 * i; |
| 1341 | else if (isOdd) |
| 1342 | Index = 2 * i + 1; |
| 1343 | else |
| 1344 | Index = ExtVectorType::getAccessorIdx(compStr[i]); |
Chris Lattner | b8f849d | 2007-08-02 23:36:59 +0000 | [diff] [blame] | 1345 | |
Nate Begeman | 3b8d116 | 2008-05-13 21:03:02 +0000 | [diff] [blame] | 1346 | Elts.push_back(Index); |
Chris Lattner | b8f849d | 2007-08-02 23:36:59 +0000 | [diff] [blame] | 1347 | } |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1348 | } |
| 1349 | |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 1350 | // constructor for instance messages. |
Steve Naroff | bcfb06a | 2007-09-28 22:22:11 +0000 | [diff] [blame] | 1351 | ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo, |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1352 | QualType retType, ObjCMethodDecl *mproto, |
Steve Naroff | db611d5 | 2007-11-03 16:37:59 +0000 | [diff] [blame] | 1353 | SourceLocation LBrac, SourceLocation RBrac, |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 1354 | Expr **ArgExprs, unsigned nargs) |
Steve Naroff | db611d5 | 2007-11-03 16:37:59 +0000 | [diff] [blame] | 1355 | : Expr(ObjCMessageExprClass, retType), SelName(selInfo), |
Ted Kremenek | ea958e57 | 2008-05-01 17:26:20 +0000 | [diff] [blame] | 1356 | MethodProto(mproto) { |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 1357 | NumArgs = nargs; |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1358 | SubExprs = new Stmt*[NumArgs+1]; |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 1359 | SubExprs[RECEIVER] = receiver; |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 1360 | if (NumArgs) { |
| 1361 | for (unsigned i = 0; i != NumArgs; ++i) |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 1362 | SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]); |
| 1363 | } |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 1364 | LBracloc = LBrac; |
| 1365 | RBracloc = RBrac; |
| 1366 | } |
| 1367 | |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 1368 | // constructor for class messages. |
| 1369 | // FIXME: clsName should be typed to ObjCInterfaceType |
Steve Naroff | bcfb06a | 2007-09-28 22:22:11 +0000 | [diff] [blame] | 1370 | ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo, |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1371 | QualType retType, ObjCMethodDecl *mproto, |
Steve Naroff | db611d5 | 2007-11-03 16:37:59 +0000 | [diff] [blame] | 1372 | SourceLocation LBrac, SourceLocation RBrac, |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 1373 | Expr **ArgExprs, unsigned nargs) |
Steve Naroff | db611d5 | 2007-11-03 16:37:59 +0000 | [diff] [blame] | 1374 | : Expr(ObjCMessageExprClass, retType), SelName(selInfo), |
Ted Kremenek | ea958e57 | 2008-05-01 17:26:20 +0000 | [diff] [blame] | 1375 | MethodProto(mproto) { |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 1376 | NumArgs = nargs; |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1377 | SubExprs = new Stmt*[NumArgs+1]; |
Ted Kremenek | 4df728e | 2008-06-24 15:50:53 +0000 | [diff] [blame] | 1378 | SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown); |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 1379 | if (NumArgs) { |
| 1380 | for (unsigned i = 0; i != NumArgs; ++i) |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 1381 | SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]); |
| 1382 | } |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 1383 | LBracloc = LBrac; |
| 1384 | RBracloc = RBrac; |
| 1385 | } |
| 1386 | |
Ted Kremenek | 4df728e | 2008-06-24 15:50:53 +0000 | [diff] [blame] | 1387 | // constructor for class messages. |
| 1388 | ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo, |
| 1389 | QualType retType, ObjCMethodDecl *mproto, |
| 1390 | SourceLocation LBrac, SourceLocation RBrac, |
| 1391 | Expr **ArgExprs, unsigned nargs) |
| 1392 | : Expr(ObjCMessageExprClass, retType), SelName(selInfo), |
| 1393 | MethodProto(mproto) { |
| 1394 | NumArgs = nargs; |
| 1395 | SubExprs = new Stmt*[NumArgs+1]; |
| 1396 | SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown); |
| 1397 | if (NumArgs) { |
| 1398 | for (unsigned i = 0; i != NumArgs; ++i) |
| 1399 | SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]); |
| 1400 | } |
| 1401 | LBracloc = LBrac; |
| 1402 | RBracloc = RBrac; |
| 1403 | } |
| 1404 | |
| 1405 | ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const { |
| 1406 | uintptr_t x = (uintptr_t) SubExprs[RECEIVER]; |
| 1407 | switch (x & Flags) { |
| 1408 | default: |
| 1409 | assert(false && "Invalid ObjCMessageExpr."); |
| 1410 | case IsInstMeth: |
| 1411 | return ClassInfo(0, 0); |
| 1412 | case IsClsMethDeclUnknown: |
| 1413 | return ClassInfo(0, (IdentifierInfo*) (x & ~Flags)); |
| 1414 | case IsClsMethDeclKnown: { |
| 1415 | ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags); |
| 1416 | return ClassInfo(D, D->getIdentifier()); |
| 1417 | } |
| 1418 | } |
| 1419 | } |
| 1420 | |
Chris Lattner | 27437ca | 2007-10-25 00:29:32 +0000 | [diff] [blame] | 1421 | bool ChooseExpr::isConditionTrue(ASTContext &C) const { |
Daniel Dunbar | 32442bb | 2008-08-13 23:47:13 +0000 | [diff] [blame] | 1422 | return getCond()->getIntegerConstantExprValue(C) != 0; |
Chris Lattner | 27437ca | 2007-10-25 00:29:32 +0000 | [diff] [blame] | 1423 | } |
| 1424 | |
Chris Lattner | 670a62c | 2008-12-12 05:35:08 +0000 | [diff] [blame] | 1425 | static int64_t evaluateOffsetOf(ASTContext& C, const Expr *E) { |
Anders Carlsson | 5a1deb8 | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 1426 | if (const MemberExpr *ME = dyn_cast<MemberExpr>(E)) { |
| 1427 | QualType Ty = ME->getBase()->getType(); |
| 1428 | |
| 1429 | RecordDecl *RD = Ty->getAsRecordType()->getDecl(); |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1430 | const ASTRecordLayout &RL = C.getASTRecordLayout(RD); |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1431 | if (FieldDecl *FD = dyn_cast<FieldDecl>(ME->getMemberDecl())) { |
| 1432 | // FIXME: This is linear time. And the fact that we're indexing |
| 1433 | // into the layout by position in the record means that we're |
| 1434 | // either stuck numbering the fields in the AST or we have to keep |
| 1435 | // the linear search (yuck and yuck). |
| 1436 | unsigned i = 0; |
| 1437 | for (RecordDecl::field_iterator Field = RD->field_begin(), |
| 1438 | FieldEnd = RD->field_end(); |
| 1439 | Field != FieldEnd; (void)++Field, ++i) { |
| 1440 | if (*Field == FD) |
| 1441 | break; |
| 1442 | } |
| 1443 | |
| 1444 | return RL.getFieldOffset(i) + evaluateOffsetOf(C, ME->getBase()); |
Anders Carlsson | 5a1deb8 | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 1445 | } |
Anders Carlsson | 5a1deb8 | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 1446 | } else if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) { |
| 1447 | const Expr *Base = ASE->getBase(); |
Anders Carlsson | 5a1deb8 | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 1448 | |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1449 | int64_t size = C.getTypeSize(ASE->getType()); |
Daniel Dunbar | 32442bb | 2008-08-13 23:47:13 +0000 | [diff] [blame] | 1450 | size *= ASE->getIdx()->getIntegerConstantExprValue(C).getSExtValue(); |
Anders Carlsson | 5a1deb8 | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 1451 | |
| 1452 | return size + evaluateOffsetOf(C, Base); |
| 1453 | } else if (isa<CompoundLiteralExpr>(E)) |
| 1454 | return 0; |
| 1455 | |
| 1456 | assert(0 && "Unknown offsetof subexpression!"); |
| 1457 | return 0; |
| 1458 | } |
| 1459 | |
| 1460 | int64_t UnaryOperator::evaluateOffsetOf(ASTContext& C) const |
| 1461 | { |
| 1462 | assert(Opc == OffsetOf && "Unary operator not offsetof!"); |
| 1463 | |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 1464 | unsigned CharSize = C.Target.getCharWidth(); |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1465 | return ::evaluateOffsetOf(C, cast<Expr>(Val)) / CharSize; |
Anders Carlsson | 5a1deb8 | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 1466 | } |
| 1467 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1468 | void SizeOfAlignOfExpr::Destroy(ASTContext& C) { |
| 1469 | // Override default behavior of traversing children. If this has a type |
| 1470 | // operand and the type is a variable-length array, the child iteration |
| 1471 | // will iterate over the size expression. However, this expression belongs |
| 1472 | // to the type, not to this, so we don't want to delete it. |
| 1473 | // We still want to delete this expression. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 1474 | if (isArgumentType()) { |
| 1475 | this->~SizeOfAlignOfExpr(); |
| 1476 | C.Deallocate(this); |
| 1477 | } |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1478 | else |
| 1479 | Expr::Destroy(C); |
Daniel Dunbar | 9048891 | 2008-08-28 18:02:04 +0000 | [diff] [blame] | 1480 | } |
| 1481 | |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1482 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1483 | // DesignatedInitExpr |
| 1484 | //===----------------------------------------------------------------------===// |
| 1485 | |
| 1486 | IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() { |
| 1487 | assert(Kind == FieldDesignator && "Only valid on a field designator"); |
| 1488 | if (Field.NameOrField & 0x01) |
| 1489 | return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01); |
| 1490 | else |
| 1491 | return getField()->getIdentifier(); |
| 1492 | } |
| 1493 | |
| 1494 | DesignatedInitExpr * |
| 1495 | DesignatedInitExpr::Create(ASTContext &C, Designator *Designators, |
| 1496 | unsigned NumDesignators, |
| 1497 | Expr **IndexExprs, unsigned NumIndexExprs, |
| 1498 | SourceLocation ColonOrEqualLoc, |
| 1499 | bool UsesColonSyntax, Expr *Init) { |
Steve Naroff | c0ac492 | 2009-01-27 23:20:32 +0000 | [diff] [blame] | 1500 | void *Mem = C.Allocate(sizeof(DesignatedInitExpr) + |
| 1501 | sizeof(Designator) * NumDesignators + |
| 1502 | sizeof(Stmt *) * (NumIndexExprs + 1), 8); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1503 | DesignatedInitExpr *DIE |
| 1504 | = new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators, |
| 1505 | ColonOrEqualLoc, UsesColonSyntax, |
| 1506 | NumIndexExprs + 1); |
| 1507 | |
| 1508 | // Fill in the designators |
| 1509 | unsigned ExpectedNumSubExprs = 0; |
| 1510 | designators_iterator Desig = DIE->designators_begin(); |
| 1511 | for (unsigned Idx = 0; Idx < NumDesignators; ++Idx, ++Desig) { |
| 1512 | new (static_cast<void*>(Desig)) Designator(Designators[Idx]); |
| 1513 | if (Designators[Idx].isArrayDesignator()) |
| 1514 | ++ExpectedNumSubExprs; |
| 1515 | else if (Designators[Idx].isArrayRangeDesignator()) |
| 1516 | ExpectedNumSubExprs += 2; |
| 1517 | } |
| 1518 | assert(ExpectedNumSubExprs == NumIndexExprs && "Wrong number of indices!"); |
| 1519 | |
| 1520 | // Fill in the subexpressions, including the initializer expression. |
| 1521 | child_iterator Child = DIE->child_begin(); |
| 1522 | *Child++ = Init; |
| 1523 | for (unsigned Idx = 0; Idx < NumIndexExprs; ++Idx, ++Child) |
| 1524 | *Child = IndexExprs[Idx]; |
| 1525 | |
| 1526 | return DIE; |
| 1527 | } |
| 1528 | |
| 1529 | SourceRange DesignatedInitExpr::getSourceRange() const { |
| 1530 | SourceLocation StartLoc; |
Chris Lattner | d603eaa | 2009-02-16 22:33:34 +0000 | [diff] [blame] | 1531 | Designator &First = |
| 1532 | *const_cast<DesignatedInitExpr*>(this)->designators_begin(); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1533 | if (First.isFieldDesignator()) { |
| 1534 | if (UsesColonSyntax) |
| 1535 | StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc); |
| 1536 | else |
| 1537 | StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc); |
| 1538 | } else |
Chris Lattner | d603eaa | 2009-02-16 22:33:34 +0000 | [diff] [blame] | 1539 | StartLoc = |
| 1540 | SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1541 | return SourceRange(StartLoc, getInit()->getSourceRange().getEnd()); |
| 1542 | } |
| 1543 | |
Chris Lattner | d603eaa | 2009-02-16 22:33:34 +0000 | [diff] [blame] | 1544 | DesignatedInitExpr::designators_iterator |
| 1545 | DesignatedInitExpr::designators_begin() { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1546 | char* Ptr = static_cast<char*>(static_cast<void *>(this)); |
| 1547 | Ptr += sizeof(DesignatedInitExpr); |
| 1548 | return static_cast<Designator*>(static_cast<void*>(Ptr)); |
| 1549 | } |
| 1550 | |
| 1551 | DesignatedInitExpr::designators_iterator DesignatedInitExpr::designators_end() { |
| 1552 | return designators_begin() + NumDesignators; |
| 1553 | } |
| 1554 | |
| 1555 | Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) { |
| 1556 | assert(D.Kind == Designator::ArrayDesignator && "Requires array designator"); |
| 1557 | char* Ptr = static_cast<char*>(static_cast<void *>(this)); |
| 1558 | Ptr += sizeof(DesignatedInitExpr); |
| 1559 | Ptr += sizeof(Designator) * NumDesignators; |
| 1560 | Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr)); |
| 1561 | return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1)); |
| 1562 | } |
| 1563 | |
| 1564 | Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) { |
| 1565 | assert(D.Kind == Designator::ArrayRangeDesignator && |
| 1566 | "Requires array range designator"); |
| 1567 | char* Ptr = static_cast<char*>(static_cast<void *>(this)); |
| 1568 | Ptr += sizeof(DesignatedInitExpr); |
| 1569 | Ptr += sizeof(Designator) * NumDesignators; |
| 1570 | Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr)); |
| 1571 | return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1)); |
| 1572 | } |
| 1573 | |
| 1574 | Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) { |
| 1575 | assert(D.Kind == Designator::ArrayRangeDesignator && |
| 1576 | "Requires array range designator"); |
| 1577 | char* Ptr = static_cast<char*>(static_cast<void *>(this)); |
| 1578 | Ptr += sizeof(DesignatedInitExpr); |
| 1579 | Ptr += sizeof(Designator) * NumDesignators; |
| 1580 | Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr)); |
| 1581 | return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2)); |
| 1582 | } |
| 1583 | |
| 1584 | //===----------------------------------------------------------------------===// |
Ted Kremenek | ce2fc3a | 2008-10-27 18:40:21 +0000 | [diff] [blame] | 1585 | // ExprIterator. |
| 1586 | //===----------------------------------------------------------------------===// |
| 1587 | |
| 1588 | Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); } |
| 1589 | Expr* ExprIterator::operator*() const { return cast<Expr>(*I); } |
| 1590 | Expr* ExprIterator::operator->() const { return cast<Expr>(*I); } |
| 1591 | const Expr* ConstExprIterator::operator[](size_t idx) const { |
| 1592 | return cast<Expr>(I[idx]); |
| 1593 | } |
| 1594 | const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); } |
| 1595 | const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); } |
| 1596 | |
| 1597 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1598 | // Child Iterators for iterating over subexpressions/substatements |
| 1599 | //===----------------------------------------------------------------------===// |
| 1600 | |
| 1601 | // DeclRefExpr |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1602 | Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); } |
| 1603 | Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1604 | |
Steve Naroff | 7779db4 | 2007-11-12 14:29:37 +0000 | [diff] [blame] | 1605 | // ObjCIvarRefExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1606 | Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; } |
| 1607 | Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; } |
Steve Naroff | 7779db4 | 2007-11-12 14:29:37 +0000 | [diff] [blame] | 1608 | |
Steve Naroff | e3e9add | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 1609 | // ObjCPropertyRefExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1610 | Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; } |
| 1611 | Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; } |
Steve Naroff | ae78407 | 2008-05-30 00:40:33 +0000 | [diff] [blame] | 1612 | |
Fariborz Jahanian | 5daf570 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 1613 | // ObjCKVCRefExpr |
| 1614 | Stmt::child_iterator ObjCKVCRefExpr::child_begin() { return &Base; } |
| 1615 | Stmt::child_iterator ObjCKVCRefExpr::child_end() { return &Base+1; } |
| 1616 | |
Douglas Gregor | cd9b46e | 2008-11-04 14:56:14 +0000 | [diff] [blame] | 1617 | // ObjCSuperExpr |
| 1618 | Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); } |
| 1619 | Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); } |
| 1620 | |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 1621 | // PredefinedExpr |
| 1622 | Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); } |
| 1623 | Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1624 | |
| 1625 | // IntegerLiteral |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1626 | Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); } |
| 1627 | Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1628 | |
| 1629 | // CharacterLiteral |
Chris Lattner | d603eaa | 2009-02-16 22:33:34 +0000 | [diff] [blame] | 1630 | Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();} |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1631 | Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1632 | |
| 1633 | // FloatingLiteral |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1634 | Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); } |
| 1635 | Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1636 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1637 | // ImaginaryLiteral |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1638 | Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; } |
| 1639 | Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; } |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 1640 | |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1641 | // StringLiteral |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1642 | Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); } |
| 1643 | Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1644 | |
| 1645 | // ParenExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1646 | Stmt::child_iterator ParenExpr::child_begin() { return &Val; } |
| 1647 | Stmt::child_iterator ParenExpr::child_end() { return &Val+1; } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1648 | |
| 1649 | // UnaryOperator |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1650 | Stmt::child_iterator UnaryOperator::child_begin() { return &Val; } |
| 1651 | Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1652 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1653 | // SizeOfAlignOfExpr |
| 1654 | Stmt::child_iterator SizeOfAlignOfExpr::child_begin() { |
| 1655 | // If this is of a type and the type is a VLA type (and not a typedef), the |
| 1656 | // size expression of the VLA needs to be treated as an executable expression. |
| 1657 | // Why isn't this weirdness documented better in StmtIterator? |
| 1658 | if (isArgumentType()) { |
| 1659 | if (VariableArrayType* T = dyn_cast<VariableArrayType>( |
| 1660 | getArgumentType().getTypePtr())) |
| 1661 | return child_iterator(T); |
| 1662 | return child_iterator(); |
| 1663 | } |
Sebastian Redl | d457589 | 2008-12-03 23:17:54 +0000 | [diff] [blame] | 1664 | return child_iterator(&Argument.Ex); |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1665 | } |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 1666 | Stmt::child_iterator SizeOfAlignOfExpr::child_end() { |
| 1667 | if (isArgumentType()) |
| 1668 | return child_iterator(); |
Sebastian Redl | d457589 | 2008-12-03 23:17:54 +0000 | [diff] [blame] | 1669 | return child_iterator(&Argument.Ex + 1); |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1670 | } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1671 | |
| 1672 | // ArraySubscriptExpr |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1673 | Stmt::child_iterator ArraySubscriptExpr::child_begin() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1674 | return &SubExprs[0]; |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1675 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1676 | Stmt::child_iterator ArraySubscriptExpr::child_end() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1677 | return &SubExprs[0]+END_EXPR; |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1678 | } |
| 1679 | |
| 1680 | // CallExpr |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1681 | Stmt::child_iterator CallExpr::child_begin() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1682 | return &SubExprs[0]; |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1683 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1684 | Stmt::child_iterator CallExpr::child_end() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1685 | return &SubExprs[0]+NumArgs+ARGS_START; |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 1686 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1687 | |
| 1688 | // MemberExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1689 | Stmt::child_iterator MemberExpr::child_begin() { return &Base; } |
| 1690 | Stmt::child_iterator MemberExpr::child_end() { return &Base+1; } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1691 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1692 | // ExtVectorElementExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1693 | Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; } |
| 1694 | Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1695 | |
| 1696 | // CompoundLiteralExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1697 | Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; } |
| 1698 | Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1699 | |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1700 | // CastExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1701 | Stmt::child_iterator CastExpr::child_begin() { return &Op; } |
| 1702 | Stmt::child_iterator CastExpr::child_end() { return &Op+1; } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1703 | |
| 1704 | // BinaryOperator |
| 1705 | Stmt::child_iterator BinaryOperator::child_begin() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1706 | return &SubExprs[0]; |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1707 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1708 | Stmt::child_iterator BinaryOperator::child_end() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1709 | return &SubExprs[0]+END_EXPR; |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1710 | } |
| 1711 | |
| 1712 | // ConditionalOperator |
| 1713 | Stmt::child_iterator ConditionalOperator::child_begin() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1714 | return &SubExprs[0]; |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1715 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1716 | Stmt::child_iterator ConditionalOperator::child_end() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1717 | return &SubExprs[0]+END_EXPR; |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1718 | } |
| 1719 | |
| 1720 | // AddrLabelExpr |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1721 | Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); } |
| 1722 | Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1723 | |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1724 | // StmtExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1725 | Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; } |
| 1726 | Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1727 | |
| 1728 | // TypesCompatibleExpr |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1729 | Stmt::child_iterator TypesCompatibleExpr::child_begin() { |
| 1730 | return child_iterator(); |
| 1731 | } |
| 1732 | |
| 1733 | Stmt::child_iterator TypesCompatibleExpr::child_end() { |
| 1734 | return child_iterator(); |
| 1735 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1736 | |
| 1737 | // ChooseExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1738 | Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; } |
| 1739 | Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1740 | |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 1741 | // GNUNullExpr |
| 1742 | Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); } |
| 1743 | Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); } |
| 1744 | |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1745 | // ShuffleVectorExpr |
| 1746 | Stmt::child_iterator ShuffleVectorExpr::child_begin() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1747 | return &SubExprs[0]; |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1748 | } |
| 1749 | Stmt::child_iterator ShuffleVectorExpr::child_end() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1750 | return &SubExprs[0]+NumExprs; |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 1751 | } |
| 1752 | |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 1753 | // VAArgExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1754 | Stmt::child_iterator VAArgExpr::child_begin() { return &Val; } |
| 1755 | Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; } |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 1756 | |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 1757 | // InitListExpr |
| 1758 | Stmt::child_iterator InitListExpr::child_begin() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1759 | return InitExprs.size() ? &InitExprs[0] : 0; |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 1760 | } |
| 1761 | Stmt::child_iterator InitListExpr::child_end() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1762 | return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0; |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 1763 | } |
| 1764 | |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1765 | // DesignatedInitExpr |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 1766 | Stmt::child_iterator DesignatedInitExpr::child_begin() { |
| 1767 | char* Ptr = static_cast<char*>(static_cast<void *>(this)); |
| 1768 | Ptr += sizeof(DesignatedInitExpr); |
| 1769 | Ptr += sizeof(Designator) * NumDesignators; |
| 1770 | return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr)); |
| 1771 | } |
| 1772 | Stmt::child_iterator DesignatedInitExpr::child_end() { |
| 1773 | return child_iterator(&*child_begin() + NumSubExprs); |
| 1774 | } |
| 1775 | |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1776 | // ImplicitValueInitExpr |
| 1777 | Stmt::child_iterator ImplicitValueInitExpr::child_begin() { |
| 1778 | return child_iterator(); |
| 1779 | } |
| 1780 | |
| 1781 | Stmt::child_iterator ImplicitValueInitExpr::child_end() { |
| 1782 | return child_iterator(); |
| 1783 | } |
| 1784 | |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1785 | // ObjCStringLiteral |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1786 | Stmt::child_iterator ObjCStringLiteral::child_begin() { |
Chris Lattner | c6c16af | 2009-02-18 06:53:08 +0000 | [diff] [blame] | 1787 | return &String; |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1788 | } |
| 1789 | Stmt::child_iterator ObjCStringLiteral::child_end() { |
Chris Lattner | c6c16af | 2009-02-18 06:53:08 +0000 | [diff] [blame] | 1790 | return &String+1; |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1791 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1792 | |
| 1793 | // ObjCEncodeExpr |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1794 | Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); } |
| 1795 | Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 1796 | |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 1797 | // ObjCSelectorExpr |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1798 | Stmt::child_iterator ObjCSelectorExpr::child_begin() { |
| 1799 | return child_iterator(); |
| 1800 | } |
| 1801 | Stmt::child_iterator ObjCSelectorExpr::child_end() { |
| 1802 | return child_iterator(); |
| 1803 | } |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 1804 | |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 1805 | // ObjCProtocolExpr |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 1806 | Stmt::child_iterator ObjCProtocolExpr::child_begin() { |
| 1807 | return child_iterator(); |
| 1808 | } |
| 1809 | Stmt::child_iterator ObjCProtocolExpr::child_end() { |
| 1810 | return child_iterator(); |
| 1811 | } |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 1812 | |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 1813 | // ObjCMessageExpr |
Ted Kremenek | ea958e57 | 2008-05-01 17:26:20 +0000 | [diff] [blame] | 1814 | Stmt::child_iterator ObjCMessageExpr::child_begin() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1815 | return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START; |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 1816 | } |
| 1817 | Stmt::child_iterator ObjCMessageExpr::child_end() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1818 | return &SubExprs[0]+ARGS_START+getNumArgs(); |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 1819 | } |
| 1820 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 1821 | // Blocks |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 1822 | Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); } |
| 1823 | Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); } |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 1824 | |
Ted Kremenek | 9da13f9 | 2008-09-26 23:24:14 +0000 | [diff] [blame] | 1825 | Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();} |
| 1826 | Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); } |