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" |
Douglas Gregor | 0979c80 | 2009-08-31 21:41:48 +0000 | [diff] [blame] | 15 | #include "clang/AST/ExprCXX.h" |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 16 | #include "clang/AST/APValue.h" |
Chris Lattner | 2eadfb6 | 2007-07-15 23:32:58 +0000 | [diff] [blame] | 17 | #include "clang/AST/ASTContext.h" |
Chris Lattner | a4d55d8 | 2008-10-06 06:40:35 +0000 | [diff] [blame] | 18 | #include "clang/AST/DeclObjC.h" |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 19 | #include "clang/AST/DeclCXX.h" |
Douglas Gregor | aaba5e3 | 2009-02-04 19:02:06 +0000 | [diff] [blame] | 20 | #include "clang/AST/DeclTemplate.h" |
Anders Carlsson | 19cc4ab | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 21 | #include "clang/AST/RecordLayout.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 22 | #include "clang/AST/StmtVisitor.h" |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 23 | #include "clang/Basic/Builtins.h" |
Chris Lattner | da5a6b6 | 2007-11-27 18:22:04 +0000 | [diff] [blame] | 24 | #include "clang/Basic/TargetInfo.h" |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 25 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 26 | #include <algorithm> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 27 | using namespace clang; |
| 28 | |
| 29 | //===----------------------------------------------------------------------===// |
| 30 | // Primary Expressions. |
| 31 | //===----------------------------------------------------------------------===// |
| 32 | |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 33 | DeclRefExpr::DeclRefExpr(NestedNameSpecifier *Qualifier, |
| 34 | SourceRange QualifierRange, |
| 35 | NamedDecl *D, SourceLocation NameLoc, |
| 36 | bool HasExplicitTemplateArgumentList, |
| 37 | SourceLocation LAngleLoc, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame^] | 38 | const TemplateArgumentLoc *ExplicitTemplateArgs, |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 39 | unsigned NumExplicitTemplateArgs, |
| 40 | SourceLocation RAngleLoc, |
| 41 | QualType T, bool TD, bool VD) |
| 42 | : Expr(DeclRefExprClass, T, TD, VD), |
| 43 | DecoratedD(D, |
| 44 | (Qualifier? HasQualifierFlag : 0) | |
| 45 | (HasExplicitTemplateArgumentList? |
| 46 | HasExplicitTemplateArgumentListFlag : 0)), |
| 47 | Loc(NameLoc) { |
| 48 | if (Qualifier) { |
| 49 | NameQualifier *NQ = getNameQualifier(); |
| 50 | NQ->NNS = Qualifier; |
| 51 | NQ->Range = QualifierRange; |
| 52 | } |
| 53 | |
| 54 | if (HasExplicitTemplateArgumentList) { |
| 55 | ExplicitTemplateArgumentList *ETemplateArgs |
| 56 | = getExplicitTemplateArgumentList(); |
| 57 | ETemplateArgs->LAngleLoc = LAngleLoc; |
| 58 | ETemplateArgs->RAngleLoc = RAngleLoc; |
| 59 | ETemplateArgs->NumTemplateArgs = NumExplicitTemplateArgs; |
| 60 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame^] | 61 | TemplateArgumentLoc *TemplateArgs = ETemplateArgs->getTemplateArgs(); |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 62 | for (unsigned I = 0; I < NumExplicitTemplateArgs; ++I) |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame^] | 63 | new (TemplateArgs + I) TemplateArgumentLoc(ExplicitTemplateArgs[I]); |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 64 | } |
| 65 | } |
| 66 | |
| 67 | DeclRefExpr *DeclRefExpr::Create(ASTContext &Context, |
| 68 | NestedNameSpecifier *Qualifier, |
| 69 | SourceRange QualifierRange, |
| 70 | NamedDecl *D, |
| 71 | SourceLocation NameLoc, |
| 72 | QualType T, bool TD, bool VD) { |
| 73 | return Create(Context, Qualifier, QualifierRange, D, NameLoc, |
| 74 | false, SourceLocation(), 0, 0, SourceLocation(), |
| 75 | T, TD, VD); |
| 76 | } |
| 77 | |
| 78 | DeclRefExpr *DeclRefExpr::Create(ASTContext &Context, |
| 79 | NestedNameSpecifier *Qualifier, |
| 80 | SourceRange QualifierRange, |
| 81 | NamedDecl *D, |
| 82 | SourceLocation NameLoc, |
| 83 | bool HasExplicitTemplateArgumentList, |
| 84 | SourceLocation LAngleLoc, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame^] | 85 | const TemplateArgumentLoc *ExplicitTemplateArgs, |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 86 | unsigned NumExplicitTemplateArgs, |
| 87 | SourceLocation RAngleLoc, |
| 88 | QualType T, bool TD, bool VD) { |
| 89 | std::size_t Size = sizeof(DeclRefExpr); |
| 90 | if (Qualifier != 0) |
| 91 | Size += sizeof(NameQualifier); |
| 92 | |
| 93 | if (HasExplicitTemplateArgumentList) |
| 94 | Size += sizeof(ExplicitTemplateArgumentList) + |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame^] | 95 | sizeof(TemplateArgumentLoc) * NumExplicitTemplateArgs; |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 96 | |
| 97 | void *Mem = Context.Allocate(Size, llvm::alignof<DeclRefExpr>()); |
| 98 | return new (Mem) DeclRefExpr(Qualifier, QualifierRange, D, NameLoc, |
| 99 | HasExplicitTemplateArgumentList, |
| 100 | LAngleLoc, |
| 101 | ExplicitTemplateArgs, |
| 102 | NumExplicitTemplateArgs, |
| 103 | RAngleLoc, |
| 104 | T, TD, VD); |
| 105 | } |
| 106 | |
| 107 | SourceRange DeclRefExpr::getSourceRange() const { |
| 108 | // FIXME: Does not handle multi-token names well, e.g., operator[]. |
| 109 | SourceRange R(Loc); |
| 110 | |
| 111 | if (hasQualifier()) |
| 112 | R.setBegin(getQualifierRange().getBegin()); |
| 113 | if (hasExplicitTemplateArgumentList()) |
| 114 | R.setEnd(getRAngleLoc()); |
| 115 | return R; |
| 116 | } |
| 117 | |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 118 | // FIXME: Maybe this should use DeclPrinter with a special "print predefined |
| 119 | // expr" policy instead. |
| 120 | std::string PredefinedExpr::ComputeName(ASTContext &Context, IdentType IT, |
| 121 | const Decl *CurrentDecl) { |
| 122 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) { |
| 123 | if (IT != PrettyFunction) |
| 124 | return FD->getNameAsString(); |
| 125 | |
| 126 | llvm::SmallString<256> Name; |
| 127 | llvm::raw_svector_ostream Out(Name); |
| 128 | |
| 129 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 130 | if (MD->isVirtual()) |
| 131 | Out << "virtual "; |
| 132 | } |
| 133 | |
| 134 | PrintingPolicy Policy(Context.getLangOptions()); |
| 135 | Policy.SuppressTagKind = true; |
| 136 | |
| 137 | std::string Proto = FD->getQualifiedNameAsString(Policy); |
| 138 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 139 | const FunctionType *AFT = FD->getType()->getAs<FunctionType>(); |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 140 | const FunctionProtoType *FT = 0; |
| 141 | if (FD->hasWrittenPrototype()) |
| 142 | FT = dyn_cast<FunctionProtoType>(AFT); |
| 143 | |
| 144 | Proto += "("; |
| 145 | if (FT) { |
| 146 | llvm::raw_string_ostream POut(Proto); |
| 147 | for (unsigned i = 0, e = FD->getNumParams(); i != e; ++i) { |
| 148 | if (i) POut << ", "; |
| 149 | std::string Param; |
| 150 | FD->getParamDecl(i)->getType().getAsStringInternal(Param, Policy); |
| 151 | POut << Param; |
| 152 | } |
| 153 | |
| 154 | if (FT->isVariadic()) { |
| 155 | if (FD->getNumParams()) POut << ", "; |
| 156 | POut << "..."; |
| 157 | } |
| 158 | } |
| 159 | Proto += ")"; |
| 160 | |
| 161 | AFT->getResultType().getAsStringInternal(Proto, Policy); |
| 162 | |
| 163 | Out << Proto; |
| 164 | |
| 165 | Out.flush(); |
| 166 | return Name.str().str(); |
| 167 | } |
| 168 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) { |
| 169 | llvm::SmallString<256> Name; |
| 170 | llvm::raw_svector_ostream Out(Name); |
| 171 | Out << (MD->isInstanceMethod() ? '-' : '+'); |
| 172 | Out << '['; |
| 173 | Out << MD->getClassInterface()->getNameAsString(); |
| 174 | if (const ObjCCategoryImplDecl *CID = |
| 175 | dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext())) { |
| 176 | Out << '('; |
| 177 | Out << CID->getNameAsString(); |
| 178 | Out << ')'; |
| 179 | } |
| 180 | Out << ' '; |
| 181 | Out << MD->getSelector().getAsString(); |
| 182 | Out << ']'; |
| 183 | |
| 184 | Out.flush(); |
| 185 | return Name.str().str(); |
| 186 | } |
| 187 | if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) { |
| 188 | // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string. |
| 189 | return "top level"; |
| 190 | } |
| 191 | return ""; |
| 192 | } |
| 193 | |
Chris Lattner | da8249e | 2008-06-07 22:13:43 +0000 | [diff] [blame] | 194 | /// getValueAsApproximateDouble - This returns the value as an inaccurate |
| 195 | /// double. Note that this may cause loss of precision, but is useful for |
| 196 | /// debugging dumps, etc. |
| 197 | double FloatingLiteral::getValueAsApproximateDouble() const { |
| 198 | llvm::APFloat V = getValue(); |
Dale Johannesen | ee5a700 | 2008-10-09 23:02:32 +0000 | [diff] [blame] | 199 | bool ignored; |
| 200 | V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven, |
| 201 | &ignored); |
Chris Lattner | da8249e | 2008-06-07 22:13:43 +0000 | [diff] [blame] | 202 | return V.convertToDouble(); |
| 203 | } |
| 204 | |
Chris Lattner | 2085fd6 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 205 | StringLiteral *StringLiteral::Create(ASTContext &C, const char *StrData, |
| 206 | unsigned ByteLength, bool Wide, |
| 207 | QualType Ty, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 208 | const SourceLocation *Loc, |
Anders Carlsson | a135fb4 | 2009-03-15 18:34:13 +0000 | [diff] [blame] | 209 | unsigned NumStrs) { |
Chris Lattner | 2085fd6 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 210 | // Allocate enough space for the StringLiteral plus an array of locations for |
| 211 | // any concatenated string tokens. |
| 212 | void *Mem = C.Allocate(sizeof(StringLiteral)+ |
| 213 | sizeof(SourceLocation)*(NumStrs-1), |
| 214 | llvm::alignof<StringLiteral>()); |
| 215 | StringLiteral *SL = new (Mem) StringLiteral(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 216 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 217 | // OPTIMIZE: could allocate this appended to the StringLiteral. |
Chris Lattner | 2085fd6 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 218 | char *AStrData = new (C, 1) char[ByteLength]; |
| 219 | memcpy(AStrData, StrData, ByteLength); |
| 220 | SL->StrData = AStrData; |
| 221 | SL->ByteLength = ByteLength; |
| 222 | SL->IsWide = Wide; |
| 223 | SL->TokLocs[0] = Loc[0]; |
| 224 | SL->NumConcatenated = NumStrs; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 225 | |
Chris Lattner | 726e168 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 226 | if (NumStrs != 1) |
Chris Lattner | 2085fd6 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 227 | memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1)); |
| 228 | return SL; |
Chris Lattner | 726e168 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Douglas Gregor | 673ecd6 | 2009-04-15 16:35:07 +0000 | [diff] [blame] | 231 | StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) { |
| 232 | void *Mem = C.Allocate(sizeof(StringLiteral)+ |
| 233 | sizeof(SourceLocation)*(NumStrs-1), |
| 234 | llvm::alignof<StringLiteral>()); |
| 235 | StringLiteral *SL = new (Mem) StringLiteral(QualType()); |
| 236 | SL->StrData = 0; |
| 237 | SL->ByteLength = 0; |
| 238 | SL->NumConcatenated = NumStrs; |
| 239 | return SL; |
| 240 | } |
| 241 | |
Douglas Gregor | 42602bb | 2009-08-07 06:08:38 +0000 | [diff] [blame] | 242 | void StringLiteral::DoDestroy(ASTContext &C) { |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 243 | C.Deallocate(const_cast<char*>(StrData)); |
Douglas Gregor | 42602bb | 2009-08-07 06:08:38 +0000 | [diff] [blame] | 244 | Expr::DoDestroy(C); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 245 | } |
| 246 | |
Daniel Dunbar | b648023 | 2009-09-22 03:27:33 +0000 | [diff] [blame] | 247 | void StringLiteral::setString(ASTContext &C, llvm::StringRef Str) { |
Douglas Gregor | 673ecd6 | 2009-04-15 16:35:07 +0000 | [diff] [blame] | 248 | if (StrData) |
| 249 | C.Deallocate(const_cast<char*>(StrData)); |
| 250 | |
Daniel Dunbar | b648023 | 2009-09-22 03:27:33 +0000 | [diff] [blame] | 251 | char *AStrData = new (C, 1) char[Str.size()]; |
| 252 | memcpy(AStrData, Str.data(), Str.size()); |
Douglas Gregor | 673ecd6 | 2009-04-15 16:35:07 +0000 | [diff] [blame] | 253 | StrData = AStrData; |
Daniel Dunbar | b648023 | 2009-09-22 03:27:33 +0000 | [diff] [blame] | 254 | ByteLength = Str.size(); |
Douglas Gregor | 673ecd6 | 2009-04-15 16:35:07 +0000 | [diff] [blame] | 255 | } |
| 256 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 257 | /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it |
| 258 | /// corresponds to, e.g. "sizeof" or "[pre]++". |
| 259 | const char *UnaryOperator::getOpcodeStr(Opcode Op) { |
| 260 | switch (Op) { |
| 261 | default: assert(0 && "Unknown unary operator"); |
| 262 | case PostInc: return "++"; |
| 263 | case PostDec: return "--"; |
| 264 | case PreInc: return "++"; |
| 265 | case PreDec: return "--"; |
| 266 | case AddrOf: return "&"; |
| 267 | case Deref: return "*"; |
| 268 | case Plus: return "+"; |
| 269 | case Minus: return "-"; |
| 270 | case Not: return "~"; |
| 271 | case LNot: return "!"; |
| 272 | case Real: return "__real"; |
| 273 | case Imag: return "__imag"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 274 | case Extension: return "__extension__"; |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 275 | case OffsetOf: return "__builtin_offsetof"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 279 | UnaryOperator::Opcode |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 280 | UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) { |
| 281 | switch (OO) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 282 | default: assert(false && "No unary operator for overloaded function"); |
Chris Lattner | b7beee9 | 2009-03-22 00:10:22 +0000 | [diff] [blame] | 283 | case OO_PlusPlus: return Postfix ? PostInc : PreInc; |
| 284 | case OO_MinusMinus: return Postfix ? PostDec : PreDec; |
| 285 | case OO_Amp: return AddrOf; |
| 286 | case OO_Star: return Deref; |
| 287 | case OO_Plus: return Plus; |
| 288 | case OO_Minus: return Minus; |
| 289 | case OO_Tilde: return Not; |
| 290 | case OO_Exclaim: return LNot; |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
| 294 | OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) { |
| 295 | switch (Opc) { |
| 296 | case PostInc: case PreInc: return OO_PlusPlus; |
| 297 | case PostDec: case PreDec: return OO_MinusMinus; |
| 298 | case AddrOf: return OO_Amp; |
| 299 | case Deref: return OO_Star; |
| 300 | case Plus: return OO_Plus; |
| 301 | case Minus: return OO_Minus; |
| 302 | case Not: return OO_Tilde; |
| 303 | case LNot: return OO_Exclaim; |
| 304 | default: return OO_None; |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 309 | //===----------------------------------------------------------------------===// |
| 310 | // Postfix Operators. |
| 311 | //===----------------------------------------------------------------------===// |
| 312 | |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 313 | CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, Expr **args, |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 314 | unsigned numargs, QualType t, SourceLocation rparenloc) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 315 | : Expr(SC, t, |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 316 | fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs), |
Chris Lattner | d603eaa | 2009-02-16 22:33:34 +0000 | [diff] [blame] | 317 | fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)), |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 318 | NumArgs(numargs) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 319 | |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 320 | SubExprs = new (C) Stmt*[numargs+1]; |
Douglas Gregor | b460980 | 2008-11-14 16:09:21 +0000 | [diff] [blame] | 321 | SubExprs[FN] = fn; |
| 322 | for (unsigned i = 0; i != numargs; ++i) |
| 323 | SubExprs[i+ARGS_START] = args[i]; |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 324 | |
Douglas Gregor | b460980 | 2008-11-14 16:09:21 +0000 | [diff] [blame] | 325 | RParenLoc = rparenloc; |
| 326 | } |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 327 | |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 328 | CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs, |
| 329 | QualType t, SourceLocation rparenloc) |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 330 | : Expr(CallExprClass, t, |
| 331 | fn->isTypeDependent() || hasAnyTypeDependentArguments(args, numargs), |
Chris Lattner | d603eaa | 2009-02-16 22:33:34 +0000 | [diff] [blame] | 332 | fn->isValueDependent() || hasAnyValueDependentArguments(args,numargs)), |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 333 | NumArgs(numargs) { |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 334 | |
| 335 | SubExprs = new (C) Stmt*[numargs+1]; |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 336 | SubExprs[FN] = fn; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 337 | for (unsigned i = 0; i != numargs; ++i) |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 338 | SubExprs[i+ARGS_START] = args[i]; |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 339 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 340 | RParenLoc = rparenloc; |
| 341 | } |
| 342 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 343 | CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty) |
| 344 | : Expr(SC, Empty), SubExprs(0), NumArgs(0) { |
Douglas Gregor | 1f0d013 | 2009-04-15 17:43:59 +0000 | [diff] [blame] | 345 | SubExprs = new (C) Stmt*[1]; |
| 346 | } |
| 347 | |
Douglas Gregor | 42602bb | 2009-08-07 06:08:38 +0000 | [diff] [blame] | 348 | void CallExpr::DoDestroy(ASTContext& C) { |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 349 | DestroyChildren(C); |
| 350 | if (SubExprs) C.Deallocate(SubExprs); |
| 351 | this->~CallExpr(); |
| 352 | C.Deallocate(this); |
| 353 | } |
| 354 | |
Zhongxing Xu | a004254 | 2009-07-17 07:29:51 +0000 | [diff] [blame] | 355 | FunctionDecl *CallExpr::getDirectCallee() { |
| 356 | Expr *CEE = getCallee()->IgnoreParenCasts(); |
Chris Lattner | 6346f96 | 2009-07-17 15:46:27 +0000 | [diff] [blame] | 357 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE)) |
Zhongxing Xu | a004254 | 2009-07-17 07:29:51 +0000 | [diff] [blame] | 358 | return dyn_cast<FunctionDecl>(DRE->getDecl()); |
Zhongxing Xu | a004254 | 2009-07-17 07:29:51 +0000 | [diff] [blame] | 359 | |
| 360 | return 0; |
| 361 | } |
| 362 | |
Chris Lattner | d18b329 | 2007-12-28 05:25:02 +0000 | [diff] [blame] | 363 | /// setNumArgs - This changes the number of arguments present in this call. |
| 364 | /// Any orphaned expressions are deleted by this, and any new operands are set |
| 365 | /// to null. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 366 | void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) { |
Chris Lattner | d18b329 | 2007-12-28 05:25:02 +0000 | [diff] [blame] | 367 | // No change, just return. |
| 368 | if (NumArgs == getNumArgs()) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 369 | |
Chris Lattner | d18b329 | 2007-12-28 05:25:02 +0000 | [diff] [blame] | 370 | // If shrinking # arguments, just delete the extras and forgot them. |
| 371 | if (NumArgs < getNumArgs()) { |
| 372 | for (unsigned i = NumArgs, e = getNumArgs(); i != e; ++i) |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 373 | getArg(i)->Destroy(C); |
Chris Lattner | d18b329 | 2007-12-28 05:25:02 +0000 | [diff] [blame] | 374 | this->NumArgs = NumArgs; |
| 375 | return; |
| 376 | } |
| 377 | |
| 378 | // Otherwise, we are growing the # arguments. New an bigger argument array. |
Daniel Dunbar | 68a049c | 2009-07-28 06:29:46 +0000 | [diff] [blame] | 379 | Stmt **NewSubExprs = new (C) Stmt*[NumArgs+1]; |
Chris Lattner | d18b329 | 2007-12-28 05:25:02 +0000 | [diff] [blame] | 380 | // Copy over args. |
| 381 | for (unsigned i = 0; i != getNumArgs()+ARGS_START; ++i) |
| 382 | NewSubExprs[i] = SubExprs[i]; |
| 383 | // Null out new args. |
| 384 | for (unsigned i = getNumArgs()+ARGS_START; i != NumArgs+ARGS_START; ++i) |
| 385 | NewSubExprs[i] = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 386 | |
Douglas Gregor | 88c9a46 | 2009-04-17 21:46:47 +0000 | [diff] [blame] | 387 | if (SubExprs) C.Deallocate(SubExprs); |
Chris Lattner | d18b329 | 2007-12-28 05:25:02 +0000 | [diff] [blame] | 388 | SubExprs = NewSubExprs; |
| 389 | this->NumArgs = NumArgs; |
| 390 | } |
| 391 | |
Chris Lattner | cb88896 | 2008-10-06 05:00:53 +0000 | [diff] [blame] | 392 | /// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If |
| 393 | /// not, return 0. |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 394 | unsigned CallExpr::isBuiltinCall(ASTContext &Context) const { |
Steve Naroff | c4f8e8b | 2008-01-31 01:07:12 +0000 | [diff] [blame] | 395 | // All simple function calls (e.g. func()) are implicitly cast to pointer to |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 396 | // function. As a result, we try and obtain the DeclRefExpr from the |
Steve Naroff | c4f8e8b | 2008-01-31 01:07:12 +0000 | [diff] [blame] | 397 | // ImplicitCastExpr. |
| 398 | const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee()); |
| 399 | if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()). |
Chris Lattner | cb88896 | 2008-10-06 05:00:53 +0000 | [diff] [blame] | 400 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 401 | |
Steve Naroff | c4f8e8b | 2008-01-31 01:07:12 +0000 | [diff] [blame] | 402 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr()); |
| 403 | if (!DRE) |
Chris Lattner | cb88896 | 2008-10-06 05:00:53 +0000 | [diff] [blame] | 404 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 405 | |
Anders Carlsson | bcba201 | 2008-01-31 02:13:57 +0000 | [diff] [blame] | 406 | const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl()); |
| 407 | if (!FDecl) |
Chris Lattner | cb88896 | 2008-10-06 05:00:53 +0000 | [diff] [blame] | 408 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 409 | |
Douglas Gregor | 4fcd399 | 2008-11-21 15:30:19 +0000 | [diff] [blame] | 410 | if (!FDecl->getIdentifier()) |
| 411 | return 0; |
| 412 | |
Douglas Gregor | 7814e6d | 2009-09-12 00:22:50 +0000 | [diff] [blame] | 413 | return FDecl->getBuiltinID(); |
Chris Lattner | cb88896 | 2008-10-06 05:00:53 +0000 | [diff] [blame] | 414 | } |
Anders Carlsson | bcba201 | 2008-01-31 02:13:57 +0000 | [diff] [blame] | 415 | |
Anders Carlsson | 6dde78f | 2009-05-26 04:57:27 +0000 | [diff] [blame] | 416 | QualType CallExpr::getCallReturnType() const { |
| 417 | QualType CalleeType = getCallee()->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 418 | if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>()) |
Anders Carlsson | 6dde78f | 2009-05-26 04:57:27 +0000 | [diff] [blame] | 419 | CalleeType = FnTypePtr->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 420 | else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>()) |
Anders Carlsson | 6dde78f | 2009-05-26 04:57:27 +0000 | [diff] [blame] | 421 | CalleeType = BPT->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 422 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 423 | const FunctionType *FnType = CalleeType->getAs<FunctionType>(); |
Anders Carlsson | 6dde78f | 2009-05-26 04:57:27 +0000 | [diff] [blame] | 424 | return FnType->getResultType(); |
| 425 | } |
Chris Lattner | cb88896 | 2008-10-06 05:00:53 +0000 | [diff] [blame] | 426 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 427 | MemberExpr::MemberExpr(Expr *base, bool isarrow, NestedNameSpecifier *qual, |
| 428 | SourceRange qualrange, NamedDecl *memberdecl, |
Douglas Gregor | c4bf26f | 2009-09-01 00:37:14 +0000 | [diff] [blame] | 429 | SourceLocation l, bool has_explicit, |
| 430 | SourceLocation langle, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame^] | 431 | const TemplateArgumentLoc *targs, unsigned numtargs, |
Douglas Gregor | c4bf26f | 2009-09-01 00:37:14 +0000 | [diff] [blame] | 432 | SourceLocation rangle, QualType ty) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 433 | : Expr(MemberExprClass, ty, |
Douglas Gregor | 83f6faf | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 434 | base->isTypeDependent() || (qual && qual->isDependent()), |
| 435 | base->isValueDependent() || (qual && qual->isDependent())), |
| 436 | Base(base), MemberDecl(memberdecl), MemberLoc(l), IsArrow(isarrow), |
Douglas Gregor | c4bf26f | 2009-09-01 00:37:14 +0000 | [diff] [blame] | 437 | HasQualifier(qual != 0), HasExplicitTemplateArgumentList(has_explicit) { |
Douglas Gregor | 83f6faf | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 438 | // Initialize the qualifier, if any. |
| 439 | if (HasQualifier) { |
| 440 | NameQualifier *NQ = getMemberQualifier(); |
| 441 | NQ->NNS = qual; |
| 442 | NQ->Range = qualrange; |
| 443 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 444 | |
Douglas Gregor | c4bf26f | 2009-09-01 00:37:14 +0000 | [diff] [blame] | 445 | // Initialize the explicit template argument list, if any. |
| 446 | if (HasExplicitTemplateArgumentList) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 447 | ExplicitTemplateArgumentList *ETemplateArgs |
Douglas Gregor | c4bf26f | 2009-09-01 00:37:14 +0000 | [diff] [blame] | 448 | = getExplicitTemplateArgumentList(); |
| 449 | ETemplateArgs->LAngleLoc = langle; |
| 450 | ETemplateArgs->RAngleLoc = rangle; |
| 451 | ETemplateArgs->NumTemplateArgs = numtargs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 452 | |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame^] | 453 | TemplateArgumentLoc *TemplateArgs = ETemplateArgs->getTemplateArgs(); |
Douglas Gregor | c4bf26f | 2009-09-01 00:37:14 +0000 | [diff] [blame] | 454 | for (unsigned I = 0; I < numtargs; ++I) |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame^] | 455 | new (TemplateArgs + I) TemplateArgumentLoc(targs[I]); |
Douglas Gregor | c4bf26f | 2009-09-01 00:37:14 +0000 | [diff] [blame] | 456 | } |
Douglas Gregor | 83f6faf | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 459 | MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow, |
| 460 | NestedNameSpecifier *qual, |
Douglas Gregor | 83f6faf | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 461 | SourceRange qualrange, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 462 | NamedDecl *memberdecl, |
| 463 | SourceLocation l, |
Douglas Gregor | c4bf26f | 2009-09-01 00:37:14 +0000 | [diff] [blame] | 464 | bool has_explicit, |
| 465 | SourceLocation langle, |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame^] | 466 | const TemplateArgumentLoc *targs, |
Douglas Gregor | c4bf26f | 2009-09-01 00:37:14 +0000 | [diff] [blame] | 467 | unsigned numtargs, |
| 468 | SourceLocation rangle, |
| 469 | QualType ty) { |
Douglas Gregor | 83f6faf | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 470 | std::size_t Size = sizeof(MemberExpr); |
| 471 | if (qual != 0) |
| 472 | Size += sizeof(NameQualifier); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 473 | |
Douglas Gregor | c4bf26f | 2009-09-01 00:37:14 +0000 | [diff] [blame] | 474 | if (has_explicit) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 475 | Size += sizeof(ExplicitTemplateArgumentList) + |
John McCall | 833ca99 | 2009-10-29 08:12:44 +0000 | [diff] [blame^] | 476 | sizeof(TemplateArgumentLoc) * numtargs; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 477 | |
Douglas Gregor | 83f6faf | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 478 | void *Mem = C.Allocate(Size, llvm::alignof<MemberExpr>()); |
Douglas Gregor | c4bf26f | 2009-09-01 00:37:14 +0000 | [diff] [blame] | 479 | return new (Mem) MemberExpr(base, isarrow, qual, qualrange, memberdecl, l, |
| 480 | has_explicit, langle, targs, numtargs, rangle, |
Douglas Gregor | 83f6faf | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 481 | ty); |
| 482 | } |
| 483 | |
Anders Carlsson | f8ec55a | 2009-09-03 00:59:21 +0000 | [diff] [blame] | 484 | const char *CastExpr::getCastKindName() const { |
| 485 | switch (getCastKind()) { |
| 486 | case CastExpr::CK_Unknown: |
| 487 | return "Unknown"; |
| 488 | case CastExpr::CK_BitCast: |
| 489 | return "BitCast"; |
| 490 | case CastExpr::CK_NoOp: |
| 491 | return "NoOp"; |
| 492 | case CastExpr::CK_DerivedToBase: |
| 493 | return "DerivedToBase"; |
| 494 | case CastExpr::CK_Dynamic: |
| 495 | return "Dynamic"; |
| 496 | case CastExpr::CK_ToUnion: |
| 497 | return "ToUnion"; |
| 498 | case CastExpr::CK_ArrayToPointerDecay: |
| 499 | return "ArrayToPointerDecay"; |
| 500 | case CastExpr::CK_FunctionToPointerDecay: |
| 501 | return "FunctionToPointerDecay"; |
| 502 | case CastExpr::CK_NullToMemberPointer: |
| 503 | return "NullToMemberPointer"; |
| 504 | case CastExpr::CK_BaseToDerivedMemberPointer: |
| 505 | return "BaseToDerivedMemberPointer"; |
| 506 | case CastExpr::CK_UserDefinedConversion: |
| 507 | return "UserDefinedConversion"; |
| 508 | case CastExpr::CK_ConstructorConversion: |
| 509 | return "ConstructorConversion"; |
Anders Carlsson | 7f9e646 | 2009-09-15 04:48:33 +0000 | [diff] [blame] | 510 | case CastExpr::CK_IntegralToPointer: |
| 511 | return "IntegralToPointer"; |
| 512 | case CastExpr::CK_PointerToIntegral: |
| 513 | return "PointerToIntegral"; |
Anders Carlsson | ebeaf20 | 2009-10-16 02:35:04 +0000 | [diff] [blame] | 514 | case CastExpr::CK_ToVoid: |
| 515 | return "ToVoid"; |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 516 | case CastExpr::CK_VectorSplat: |
| 517 | return "VectorSplat"; |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 518 | case CastExpr::CK_IntegralCast: |
| 519 | return "IntegralCast"; |
| 520 | case CastExpr::CK_IntegralToFloating: |
| 521 | return "IntegralToFloating"; |
| 522 | case CastExpr::CK_FloatingToIntegral: |
| 523 | return "FloatingToIntegral"; |
Benjamin Kramer | c6b2916 | 2009-10-18 19:02:15 +0000 | [diff] [blame] | 524 | case CastExpr::CK_FloatingCast: |
| 525 | return "FloatingCast"; |
Anders Carlsson | f8ec55a | 2009-09-03 00:59:21 +0000 | [diff] [blame] | 526 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 527 | |
Anders Carlsson | f8ec55a | 2009-09-03 00:59:21 +0000 | [diff] [blame] | 528 | assert(0 && "Unhandled cast kind!"); |
| 529 | return 0; |
| 530 | } |
| 531 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 532 | /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it |
| 533 | /// corresponds to, e.g. "<<=". |
| 534 | const char *BinaryOperator::getOpcodeStr(Opcode Op) { |
| 535 | switch (Op) { |
Douglas Gregor | baf5348 | 2009-03-12 22:51:37 +0000 | [diff] [blame] | 536 | case PtrMemD: return ".*"; |
| 537 | case PtrMemI: return "->*"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 538 | case Mul: return "*"; |
| 539 | case Div: return "/"; |
| 540 | case Rem: return "%"; |
| 541 | case Add: return "+"; |
| 542 | case Sub: return "-"; |
| 543 | case Shl: return "<<"; |
| 544 | case Shr: return ">>"; |
| 545 | case LT: return "<"; |
| 546 | case GT: return ">"; |
| 547 | case LE: return "<="; |
| 548 | case GE: return ">="; |
| 549 | case EQ: return "=="; |
| 550 | case NE: return "!="; |
| 551 | case And: return "&"; |
| 552 | case Xor: return "^"; |
| 553 | case Or: return "|"; |
| 554 | case LAnd: return "&&"; |
| 555 | case LOr: return "||"; |
| 556 | case Assign: return "="; |
| 557 | case MulAssign: return "*="; |
| 558 | case DivAssign: return "/="; |
| 559 | case RemAssign: return "%="; |
| 560 | case AddAssign: return "+="; |
| 561 | case SubAssign: return "-="; |
| 562 | case ShlAssign: return "<<="; |
| 563 | case ShrAssign: return ">>="; |
| 564 | case AndAssign: return "&="; |
| 565 | case XorAssign: return "^="; |
| 566 | case OrAssign: return "|="; |
| 567 | case Comma: return ","; |
| 568 | } |
Douglas Gregor | baf5348 | 2009-03-12 22:51:37 +0000 | [diff] [blame] | 569 | |
| 570 | return ""; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 571 | } |
| 572 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 573 | BinaryOperator::Opcode |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 574 | BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) { |
| 575 | switch (OO) { |
Chris Lattner | b7beee9 | 2009-03-22 00:10:22 +0000 | [diff] [blame] | 576 | default: assert(false && "Not an overloadable binary operator"); |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 577 | case OO_Plus: return Add; |
| 578 | case OO_Minus: return Sub; |
| 579 | case OO_Star: return Mul; |
| 580 | case OO_Slash: return Div; |
| 581 | case OO_Percent: return Rem; |
| 582 | case OO_Caret: return Xor; |
| 583 | case OO_Amp: return And; |
| 584 | case OO_Pipe: return Or; |
| 585 | case OO_Equal: return Assign; |
| 586 | case OO_Less: return LT; |
| 587 | case OO_Greater: return GT; |
| 588 | case OO_PlusEqual: return AddAssign; |
| 589 | case OO_MinusEqual: return SubAssign; |
| 590 | case OO_StarEqual: return MulAssign; |
| 591 | case OO_SlashEqual: return DivAssign; |
| 592 | case OO_PercentEqual: return RemAssign; |
| 593 | case OO_CaretEqual: return XorAssign; |
| 594 | case OO_AmpEqual: return AndAssign; |
| 595 | case OO_PipeEqual: return OrAssign; |
| 596 | case OO_LessLess: return Shl; |
| 597 | case OO_GreaterGreater: return Shr; |
| 598 | case OO_LessLessEqual: return ShlAssign; |
| 599 | case OO_GreaterGreaterEqual: return ShrAssign; |
| 600 | case OO_EqualEqual: return EQ; |
| 601 | case OO_ExclaimEqual: return NE; |
| 602 | case OO_LessEqual: return LE; |
| 603 | case OO_GreaterEqual: return GE; |
| 604 | case OO_AmpAmp: return LAnd; |
| 605 | case OO_PipePipe: return LOr; |
| 606 | case OO_Comma: return Comma; |
| 607 | case OO_ArrowStar: return PtrMemI; |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 608 | } |
| 609 | } |
| 610 | |
| 611 | OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) { |
| 612 | static const OverloadedOperatorKind OverOps[] = { |
| 613 | /* .* Cannot be overloaded */OO_None, OO_ArrowStar, |
| 614 | OO_Star, OO_Slash, OO_Percent, |
| 615 | OO_Plus, OO_Minus, |
| 616 | OO_LessLess, OO_GreaterGreater, |
| 617 | OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual, |
| 618 | OO_EqualEqual, OO_ExclaimEqual, |
| 619 | OO_Amp, |
| 620 | OO_Caret, |
| 621 | OO_Pipe, |
| 622 | OO_AmpAmp, |
| 623 | OO_PipePipe, |
| 624 | OO_Equal, OO_StarEqual, |
| 625 | OO_SlashEqual, OO_PercentEqual, |
| 626 | OO_PlusEqual, OO_MinusEqual, |
| 627 | OO_LessLessEqual, OO_GreaterGreaterEqual, |
| 628 | OO_AmpEqual, OO_CaretEqual, |
| 629 | OO_PipeEqual, |
| 630 | OO_Comma |
| 631 | }; |
| 632 | return OverOps[Opc]; |
| 633 | } |
| 634 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 635 | InitListExpr::InitListExpr(SourceLocation lbraceloc, |
Chris Lattner | 418f6c7 | 2008-10-26 23:43:26 +0000 | [diff] [blame] | 636 | Expr **initExprs, unsigned numInits, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 637 | SourceLocation rbraceloc) |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 638 | : Expr(InitListExprClass, QualType(), |
| 639 | hasAnyTypeDependentArguments(initExprs, numInits), |
| 640 | hasAnyValueDependentArguments(initExprs, numInits)), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 641 | LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0), |
Douglas Gregor | a9c8780 | 2009-01-29 19:42:23 +0000 | [diff] [blame] | 642 | UnionFieldInit(0), HadArrayRangeDesignator(false) { |
Chris Lattner | 418f6c7 | 2008-10-26 23:43:26 +0000 | [diff] [blame] | 643 | |
| 644 | InitExprs.insert(InitExprs.end(), initExprs, initExprs+numInits); |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 645 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 646 | |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 647 | void InitListExpr::reserveInits(unsigned NumInits) { |
| 648 | if (NumInits > InitExprs.size()) |
| 649 | InitExprs.reserve(NumInits); |
| 650 | } |
| 651 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 652 | void InitListExpr::resizeInits(ASTContext &Context, unsigned NumInits) { |
Chris Lattner | d603eaa | 2009-02-16 22:33:34 +0000 | [diff] [blame] | 653 | for (unsigned Idx = NumInits, LastIdx = InitExprs.size(); |
Daniel Dunbar | f592c92 | 2009-02-16 22:42:44 +0000 | [diff] [blame] | 654 | Idx < LastIdx; ++Idx) |
Douglas Gregor | 0686368 | 2009-03-20 23:38:03 +0000 | [diff] [blame] | 655 | InitExprs[Idx]->Destroy(Context); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 656 | InitExprs.resize(NumInits, 0); |
| 657 | } |
| 658 | |
| 659 | Expr *InitListExpr::updateInit(unsigned Init, Expr *expr) { |
| 660 | if (Init >= InitExprs.size()) { |
| 661 | InitExprs.insert(InitExprs.end(), Init - InitExprs.size() + 1, 0); |
| 662 | InitExprs.back() = expr; |
| 663 | return 0; |
| 664 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 665 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 666 | Expr *Result = cast_or_null<Expr>(InitExprs[Init]); |
| 667 | InitExprs[Init] = expr; |
| 668 | return Result; |
| 669 | } |
| 670 | |
Steve Naroff | bfdcae6 | 2008-09-04 15:31:07 +0000 | [diff] [blame] | 671 | /// getFunctionType - Return the underlying function type for this block. |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 672 | /// |
| 673 | const FunctionType *BlockExpr::getFunctionType() const { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 674 | return getType()->getAs<BlockPointerType>()-> |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 675 | getPointeeType()->getAs<FunctionType>(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 676 | } |
| 677 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 678 | SourceLocation BlockExpr::getCaretLocation() const { |
| 679 | return TheBlock->getCaretLocation(); |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 680 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 681 | const Stmt *BlockExpr::getBody() const { |
Douglas Gregor | 7297134 | 2009-04-18 00:02:19 +0000 | [diff] [blame] | 682 | return TheBlock->getBody(); |
| 683 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 684 | Stmt *BlockExpr::getBody() { |
| 685 | return TheBlock->getBody(); |
Douglas Gregor | 7297134 | 2009-04-18 00:02:19 +0000 | [diff] [blame] | 686 | } |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 687 | |
| 688 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 689 | //===----------------------------------------------------------------------===// |
| 690 | // Generic Expression Routines |
| 691 | //===----------------------------------------------------------------------===// |
| 692 | |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 693 | /// isUnusedResultAWarning - Return true if this immediate expression should |
| 694 | /// be warned about if the result is unused. If so, fill in Loc and Ranges |
| 695 | /// with location to warn on and the source range[s] to report with the |
| 696 | /// warning. |
| 697 | bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1, |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 698 | SourceRange &R2) const { |
Anders Carlsson | ffce2df | 2009-05-15 23:10:19 +0000 | [diff] [blame] | 699 | // Don't warn if the expr is type dependent. The type could end up |
| 700 | // instantiating to void. |
| 701 | if (isTypeDependent()) |
| 702 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 703 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 704 | switch (getStmtClass()) { |
| 705 | default: |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 706 | Loc = getExprLoc(); |
| 707 | R1 = getSourceRange(); |
| 708 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 709 | case ParenExprClass: |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 710 | return cast<ParenExpr>(this)->getSubExpr()-> |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 711 | isUnusedResultAWarning(Loc, R1, R2); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 712 | case UnaryOperatorClass: { |
| 713 | const UnaryOperator *UO = cast<UnaryOperator>(this); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 714 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 715 | switch (UO->getOpcode()) { |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 716 | default: break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 717 | case UnaryOperator::PostInc: |
| 718 | case UnaryOperator::PostDec: |
| 719 | case UnaryOperator::PreInc: |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 720 | case UnaryOperator::PreDec: // ++/-- |
| 721 | return false; // Not a warning. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 722 | case UnaryOperator::Deref: |
| 723 | // Dereferencing a volatile pointer is a side-effect. |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 724 | if (getType().isVolatileQualified()) |
| 725 | return false; |
| 726 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 727 | case UnaryOperator::Real: |
| 728 | case UnaryOperator::Imag: |
| 729 | // accessing a piece of a volatile complex is a side-effect. |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 730 | if (UO->getSubExpr()->getType().isVolatileQualified()) |
| 731 | return false; |
| 732 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 733 | case UnaryOperator::Extension: |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 734 | return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 735 | } |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 736 | Loc = UO->getOperatorLoc(); |
| 737 | R1 = UO->getSubExpr()->getSourceRange(); |
| 738 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 739 | } |
Chris Lattner | e7716e6 | 2007-12-01 06:07:34 +0000 | [diff] [blame] | 740 | case BinaryOperatorClass: { |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 741 | const BinaryOperator *BO = cast<BinaryOperator>(this); |
| 742 | // Consider comma to have side effects if the LHS or RHS does. |
| 743 | if (BO->getOpcode() == BinaryOperator::Comma) |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 744 | return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2) || |
| 745 | BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 746 | |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 747 | if (BO->isAssignmentOp()) |
| 748 | return false; |
| 749 | Loc = BO->getOperatorLoc(); |
| 750 | R1 = BO->getLHS()->getSourceRange(); |
| 751 | R2 = BO->getRHS()->getSourceRange(); |
| 752 | return true; |
Chris Lattner | e7716e6 | 2007-12-01 06:07:34 +0000 | [diff] [blame] | 753 | } |
Chris Lattner | eb14fe8 | 2007-08-25 02:00:02 +0000 | [diff] [blame] | 754 | case CompoundAssignOperatorClass: |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 755 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 756 | |
Fariborz Jahanian | ab38e4b | 2007-12-01 19:58:28 +0000 | [diff] [blame] | 757 | case ConditionalOperatorClass: { |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 758 | // The condition must be evaluated, but if either the LHS or RHS is a |
| 759 | // warning, warn about them. |
Fariborz Jahanian | ab38e4b | 2007-12-01 19:58:28 +0000 | [diff] [blame] | 760 | const ConditionalOperator *Exp = cast<ConditionalOperator>(this); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 761 | if (Exp->getLHS() && |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 762 | Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2)) |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 763 | return true; |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 764 | return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2); |
Fariborz Jahanian | ab38e4b | 2007-12-01 19:58:28 +0000 | [diff] [blame] | 765 | } |
| 766 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 767 | case MemberExprClass: |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 768 | // If the base pointer or element is to a volatile pointer/field, accessing |
| 769 | // it is a side effect. |
| 770 | if (getType().isVolatileQualified()) |
| 771 | return false; |
| 772 | Loc = cast<MemberExpr>(this)->getMemberLoc(); |
| 773 | R1 = SourceRange(Loc, Loc); |
| 774 | R2 = cast<MemberExpr>(this)->getBase()->getSourceRange(); |
| 775 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 776 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 777 | case ArraySubscriptExprClass: |
| 778 | // 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] | 779 | // it is a side effect. |
| 780 | if (getType().isVolatileQualified()) |
| 781 | return false; |
| 782 | Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc(); |
| 783 | R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange(); |
| 784 | R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange(); |
| 785 | return true; |
Eli Friedman | 211f6ad | 2008-05-27 15:24:04 +0000 | [diff] [blame] | 786 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 787 | case CallExprClass: |
Eli Friedman | 852871a | 2009-04-29 16:35:53 +0000 | [diff] [blame] | 788 | case CXXOperatorCallExprClass: |
| 789 | case CXXMemberCallExprClass: { |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 790 | // If this is a direct call, get the callee. |
| 791 | const CallExpr *CE = cast<CallExpr>(this); |
Chris Lattner | bc8d42c | 2009-10-13 04:53:48 +0000 | [diff] [blame] | 792 | if (const FunctionDecl *FD = CE->getDirectCallee()) { |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 793 | // If the callee has attribute pure, const, or warn_unused_result, warn |
| 794 | // about it. void foo() { strlen("bar"); } should warn. |
Chris Lattner | bc8d42c | 2009-10-13 04:53:48 +0000 | [diff] [blame] | 795 | // |
| 796 | // Note: If new cases are added here, DiagnoseUnusedExprResult should be |
| 797 | // updated to match for QoI. |
| 798 | if (FD->getAttr<WarnUnusedResultAttr>() || |
| 799 | FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) { |
| 800 | Loc = CE->getCallee()->getLocStart(); |
| 801 | R1 = CE->getCallee()->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 802 | |
Chris Lattner | bc8d42c | 2009-10-13 04:53:48 +0000 | [diff] [blame] | 803 | if (unsigned NumArgs = CE->getNumArgs()) |
| 804 | R2 = SourceRange(CE->getArg(0)->getLocStart(), |
| 805 | CE->getArg(NumArgs-1)->getLocEnd()); |
| 806 | return true; |
| 807 | } |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 808 | } |
| 809 | return false; |
| 810 | } |
Chris Lattner | a9c0102 | 2007-09-26 22:06:30 +0000 | [diff] [blame] | 811 | case ObjCMessageExprClass: |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 812 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 813 | |
Fariborz Jahanian | 09105f5 | 2009-08-20 17:02:02 +0000 | [diff] [blame] | 814 | case ObjCImplicitSetterGetterRefExprClass: { // Dot syntax for message send. |
Chris Lattner | a50089e | 2009-08-16 16:45:18 +0000 | [diff] [blame] | 815 | #if 0 |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 816 | const ObjCImplicitSetterGetterRefExpr *Ref = |
Fariborz Jahanian | 09105f5 | 2009-08-20 17:02:02 +0000 | [diff] [blame] | 817 | cast<ObjCImplicitSetterGetterRefExpr>(this); |
Chris Lattner | a50089e | 2009-08-16 16:45:18 +0000 | [diff] [blame] | 818 | // FIXME: We really want the location of the '.' here. |
Fariborz Jahanian | 154440e | 2009-08-18 20:50:23 +0000 | [diff] [blame] | 819 | Loc = Ref->getLocation(); |
| 820 | R1 = SourceRange(Ref->getLocation(), Ref->getLocation()); |
| 821 | if (Ref->getBase()) |
| 822 | R2 = Ref->getBase()->getSourceRange(); |
Chris Lattner | 5e94a0d | 2009-08-16 16:51:50 +0000 | [diff] [blame] | 823 | #else |
| 824 | Loc = getExprLoc(); |
| 825 | R1 = getSourceRange(); |
Chris Lattner | a50089e | 2009-08-16 16:45:18 +0000 | [diff] [blame] | 826 | #endif |
| 827 | return true; |
| 828 | } |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 829 | case StmtExprClass: { |
| 830 | // Statement exprs don't logically have side effects themselves, but are |
| 831 | // sometimes used in macros in ways that give them a type that is unused. |
| 832 | // For example ({ blah; foo(); }) will end up with a type if foo has a type. |
| 833 | // however, if the result of the stmt expr is dead, we don't want to emit a |
| 834 | // warning. |
| 835 | const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt(); |
| 836 | if (!CS->body_empty()) |
| 837 | if (const Expr *E = dyn_cast<Expr>(CS->body_back())) |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 838 | return E->isUnusedResultAWarning(Loc, R1, R2); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 839 | |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 840 | Loc = cast<StmtExpr>(this)->getLParenLoc(); |
| 841 | R1 = getSourceRange(); |
| 842 | return true; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 843 | } |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 844 | case CStyleCastExprClass: |
Chris Lattner | fb84664 | 2009-07-28 18:25:28 +0000 | [diff] [blame] | 845 | // If this is an explicit cast to void, allow it. People do this when they |
| 846 | // think they know what they're doing :). |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 847 | if (getType()->isVoidType()) |
Chris Lattner | fb84664 | 2009-07-28 18:25:28 +0000 | [diff] [blame] | 848 | return false; |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 849 | Loc = cast<CStyleCastExpr>(this)->getLParenLoc(); |
| 850 | R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange(); |
| 851 | return true; |
Argyrios Kyrtzidis | 987a14b | 2008-08-22 15:38:55 +0000 | [diff] [blame] | 852 | case CXXFunctionalCastExprClass: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 853 | // If this is a cast to void, check the operand. Otherwise, the result of |
| 854 | // the cast is unused. |
| 855 | if (getType()->isVoidType()) |
Douglas Gregor | 68584ed | 2009-06-18 16:11:24 +0000 | [diff] [blame] | 856 | return cast<CastExpr>(this)->getSubExpr() |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 857 | ->isUnusedResultAWarning(Loc, R1, R2); |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 858 | Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc(); |
| 859 | R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange(); |
| 860 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 861 | |
Eli Friedman | 4be1f47 | 2008-05-19 21:24:43 +0000 | [diff] [blame] | 862 | case ImplicitCastExprClass: |
| 863 | // Check the operand, since implicit casts are inserted by Sema |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 864 | return cast<ImplicitCastExpr>(this) |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 865 | ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2); |
Eli Friedman | 4be1f47 | 2008-05-19 21:24:43 +0000 | [diff] [blame] | 866 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 867 | case CXXDefaultArgExprClass: |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 868 | return cast<CXXDefaultArgExpr>(this) |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 869 | ->getExpr()->isUnusedResultAWarning(Loc, R1, R2); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 870 | |
| 871 | case CXXNewExprClass: |
| 872 | // FIXME: In theory, there might be new expressions that don't have side |
| 873 | // effects (e.g. a placement new with an uninitialized POD). |
| 874 | case CXXDeleteExprClass: |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 875 | return false; |
Anders Carlsson | 2d46eb2 | 2009-08-16 04:11:06 +0000 | [diff] [blame] | 876 | case CXXBindTemporaryExprClass: |
| 877 | return cast<CXXBindTemporaryExpr>(this) |
| 878 | ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2); |
Anders Carlsson | 6b1d283 | 2009-05-17 21:11:30 +0000 | [diff] [blame] | 879 | case CXXExprWithTemporariesClass: |
| 880 | return cast<CXXExprWithTemporaries>(this) |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 881 | ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 882 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 883 | } |
| 884 | |
Douglas Gregor | ba7e210 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 885 | /// DeclCanBeLvalue - Determine whether the given declaration can be |
| 886 | /// an lvalue. This is a helper routine for isLvalue. |
| 887 | static bool DeclCanBeLvalue(const NamedDecl *Decl, ASTContext &Ctx) { |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 888 | // C++ [temp.param]p6: |
| 889 | // A non-type non-reference template-parameter is not an lvalue. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 890 | if (const NonTypeTemplateParmDecl *NTTParm |
Douglas Gregor | 72c3f31 | 2008-12-05 18:15:24 +0000 | [diff] [blame] | 891 | = dyn_cast<NonTypeTemplateParmDecl>(Decl)) |
| 892 | return NTTParm->getType()->isReferenceType(); |
| 893 | |
Douglas Gregor | 44b4321 | 2008-12-11 16:49:14 +0000 | [diff] [blame] | 894 | return isa<VarDecl>(Decl) || isa<FieldDecl>(Decl) || |
Douglas Gregor | ba7e210 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 895 | // C++ 3.10p2: An lvalue refers to an object or function. |
| 896 | (Ctx.getLangOptions().CPlusPlus && |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 897 | (isa<FunctionDecl>(Decl) || isa<OverloadedFunctionDecl>(Decl) || |
| 898 | isa<FunctionTemplateDecl>(Decl))); |
Douglas Gregor | ba7e210 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 899 | } |
| 900 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 901 | /// isLvalue - C99 6.3.2.1: an lvalue is an expression with an object type or an |
| 902 | /// incomplete type other than void. Nonarray expressions that can be lvalues: |
| 903 | /// - name, where name must be a variable |
| 904 | /// - e[i] |
| 905 | /// - (e), where e must be an lvalue |
| 906 | /// - e.name, where e must be an lvalue |
| 907 | /// - e->name |
| 908 | /// - *e, the type of e cannot be a function type |
| 909 | /// - string-constant |
Chris Lattner | 7da36f6 | 2007-10-30 22:53:42 +0000 | [diff] [blame] | 910 | /// - (__real__ e) and (__imag__ e) where e is an lvalue [GNU extension] |
Bill Wendling | 08ad47c | 2007-07-17 03:52:31 +0000 | [diff] [blame] | 911 | /// - reference type [C++ [expr]] |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 912 | /// |
Chris Lattner | 28be73f | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 913 | Expr::isLvalueResult Expr::isLvalue(ASTContext &Ctx) const { |
Eli Friedman | 5320285 | 2009-05-03 22:36:05 +0000 | [diff] [blame] | 914 | assert(!TR->isReferenceType() && "Expressions can't have reference type."); |
| 915 | |
| 916 | isLvalueResult Res = isLvalueInternal(Ctx); |
| 917 | if (Res != LV_Valid || Ctx.getLangOptions().CPlusPlus) |
| 918 | return Res; |
| 919 | |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 920 | // first, check the type (C99 6.3.2.1). Expressions with function |
| 921 | // type in C are not lvalues, but they can be lvalues in C++. |
Douglas Gregor | 83314aa | 2009-07-08 20:55:45 +0000 | [diff] [blame] | 922 | if (TR->isFunctionType() || TR == Ctx.OverloadTy) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 923 | return LV_NotObjectType; |
| 924 | |
Steve Naroff | acb818a | 2008-02-10 01:39:04 +0000 | [diff] [blame] | 925 | // Allow qualified void which is an incomplete type other than void (yuck). |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 926 | if (TR->isVoidType() && !Ctx.getCanonicalType(TR).hasQualifiers()) |
Steve Naroff | acb818a | 2008-02-10 01:39:04 +0000 | [diff] [blame] | 927 | return LV_IncompleteVoidType; |
| 928 | |
Eli Friedman | 5320285 | 2009-05-03 22:36:05 +0000 | [diff] [blame] | 929 | return LV_Valid; |
| 930 | } |
Bill Wendling | 08ad47c | 2007-07-17 03:52:31 +0000 | [diff] [blame] | 931 | |
Eli Friedman | 5320285 | 2009-05-03 22:36:05 +0000 | [diff] [blame] | 932 | // Check whether the expression can be sanely treated like an l-value |
| 933 | Expr::isLvalueResult Expr::isLvalueInternal(ASTContext &Ctx) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 934 | switch (getStmtClass()) { |
Chris Lattner | eaf2bb8 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 935 | case StringLiteralClass: // C99 6.5.1p4 |
| 936 | case ObjCEncodeExprClass: // @encode behaves like its string in every way. |
Anders Carlsson | 7323a62 | 2007-11-30 22:47:59 +0000 | [diff] [blame] | 937 | return LV_Valid; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 938 | case ArraySubscriptExprClass: // C99 6.5.3p4 (e1[e2] == (*((e1)+(e2)))) |
| 939 | // For vectors, make sure base is an lvalue (i.e. not a function call). |
| 940 | if (cast<ArraySubscriptExpr>(this)->getBase()->getType()->isVectorType()) |
Chris Lattner | 28be73f | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 941 | return cast<ArraySubscriptExpr>(this)->getBase()->isLvalue(Ctx); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 942 | return LV_Valid; |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 943 | case DeclRefExprClass: { // C99 6.5.1p2 |
Douglas Gregor | ba7e210 | 2008-10-22 15:04:37 +0000 | [diff] [blame] | 944 | const NamedDecl *RefdDecl = cast<DeclRefExpr>(this)->getDecl(); |
| 945 | if (DeclCanBeLvalue(RefdDecl, Ctx)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 946 | return LV_Valid; |
| 947 | break; |
Chris Lattner | 4111024 | 2008-06-17 18:05:57 +0000 | [diff] [blame] | 948 | } |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 949 | case BlockDeclRefExprClass: { |
| 950 | const BlockDeclRefExpr *BDR = cast<BlockDeclRefExpr>(this); |
Steve Naroff | 4f6a7d7 | 2008-09-26 14:41:28 +0000 | [diff] [blame] | 951 | if (isa<VarDecl>(BDR->getDecl())) |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 952 | return LV_Valid; |
| 953 | break; |
| 954 | } |
Douglas Gregor | 83f6faf | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 955 | case MemberExprClass: { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 956 | const MemberExpr *m = cast<MemberExpr>(this); |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 957 | if (Ctx.getLangOptions().CPlusPlus) { // C++ [expr.ref]p4: |
| 958 | NamedDecl *Member = m->getMemberDecl(); |
| 959 | // C++ [expr.ref]p4: |
| 960 | // If E2 is declared to have type "reference to T", then E1.E2 |
| 961 | // is an lvalue. |
| 962 | if (ValueDecl *Value = dyn_cast<ValueDecl>(Member)) |
| 963 | if (Value->getType()->isReferenceType()) |
| 964 | return LV_Valid; |
| 965 | |
| 966 | // -- If E2 is a static data member [...] then E1.E2 is an lvalue. |
Douglas Gregor | 2d2e9cf | 2009-03-11 20:22:50 +0000 | [diff] [blame] | 967 | if (isa<VarDecl>(Member) && Member->getDeclContext()->isRecord()) |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 968 | return LV_Valid; |
| 969 | |
| 970 | // -- If E2 is a non-static data member [...]. If E1 is an |
| 971 | // lvalue, then E1.E2 is an lvalue. |
| 972 | if (isa<FieldDecl>(Member)) |
| 973 | return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx); |
| 974 | |
| 975 | // -- If it refers to a static member function [...], then |
| 976 | // E1.E2 is an lvalue. |
| 977 | // -- Otherwise, if E1.E2 refers to a non-static member |
| 978 | // function [...], then E1.E2 is not an lvalue. |
| 979 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member)) |
| 980 | return Method->isStatic()? LV_Valid : LV_MemberFunction; |
| 981 | |
| 982 | // -- If E2 is a member enumerator [...], the expression E1.E2 |
| 983 | // is not an lvalue. |
| 984 | if (isa<EnumConstantDecl>(Member)) |
| 985 | return LV_InvalidExpression; |
| 986 | |
| 987 | // Not an lvalue. |
| 988 | return LV_InvalidExpression; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 989 | } |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 990 | |
| 991 | // C99 6.5.2.3p4 |
Chris Lattner | 28be73f | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 992 | return m->isArrow() ? LV_Valid : m->getBase()->isLvalue(Ctx); |
Anton Korobeynikov | fdd7566 | 2007-07-12 15:26:50 +0000 | [diff] [blame] | 993 | } |
Chris Lattner | 7da36f6 | 2007-10-30 22:53:42 +0000 | [diff] [blame] | 994 | case UnaryOperatorClass: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 995 | if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Deref) |
Chris Lattner | 7da36f6 | 2007-10-30 22:53:42 +0000 | [diff] [blame] | 996 | return LV_Valid; // C99 6.5.3p4 |
| 997 | |
| 998 | if (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Real || |
Chris Lattner | baf0d66 | 2008-07-25 18:07:19 +0000 | [diff] [blame] | 999 | cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Imag || |
| 1000 | cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::Extension) |
Chris Lattner | 28be73f | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 1001 | return cast<UnaryOperator>(this)->getSubExpr()->isLvalue(Ctx); // GNU. |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 1002 | |
| 1003 | if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.pre.incr]p1 |
| 1004 | (cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreInc || |
| 1005 | cast<UnaryOperator>(this)->getOpcode() == UnaryOperator::PreDec)) |
| 1006 | return LV_Valid; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1007 | break; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1008 | case ImplicitCastExprClass: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1009 | return cast<ImplicitCastExpr>(this)->isLvalueCast()? LV_Valid |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1010 | : LV_InvalidExpression; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1011 | case ParenExprClass: // C99 6.5.1p5 |
Chris Lattner | 28be73f | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 1012 | return cast<ParenExpr>(this)->getSubExpr()->isLvalue(Ctx); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1013 | case BinaryOperatorClass: |
| 1014 | case CompoundAssignOperatorClass: { |
| 1015 | const BinaryOperator *BinOp = cast<BinaryOperator>(this); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 1016 | |
| 1017 | if (Ctx.getLangOptions().CPlusPlus && // C++ [expr.comma]p1 |
| 1018 | BinOp->getOpcode() == BinaryOperator::Comma) |
| 1019 | return BinOp->getRHS()->isLvalue(Ctx); |
| 1020 | |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 1021 | // C++ [expr.mptr.oper]p6 |
Fariborz Jahanian | 27d4be5 | 2009-10-08 18:00:39 +0000 | [diff] [blame] | 1022 | // The result of a .* expression is an lvalue only if its first operand is |
| 1023 | // an lvalue and its second operand is a pointer to data member. |
| 1024 | if (BinOp->getOpcode() == BinaryOperator::PtrMemD && |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 1025 | !BinOp->getType()->isFunctionType()) |
| 1026 | return BinOp->getLHS()->isLvalue(Ctx); |
| 1027 | |
Fariborz Jahanian | 27d4be5 | 2009-10-08 18:00:39 +0000 | [diff] [blame] | 1028 | // The result of an ->* expression is an lvalue only if its second operand |
| 1029 | // is a pointer to data member. |
| 1030 | if (BinOp->getOpcode() == BinaryOperator::PtrMemI && |
| 1031 | !BinOp->getType()->isFunctionType()) { |
| 1032 | QualType Ty = BinOp->getRHS()->getType(); |
| 1033 | if (Ty->isMemberPointerType() && !Ty->isMemberFunctionPointerType()) |
| 1034 | return LV_Valid; |
| 1035 | } |
| 1036 | |
Douglas Gregor | bf3af05 | 2008-11-13 20:12:29 +0000 | [diff] [blame] | 1037 | if (!BinOp->isAssignmentOp()) |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1038 | return LV_InvalidExpression; |
| 1039 | |
Douglas Gregor | bf3af05 | 2008-11-13 20:12:29 +0000 | [diff] [blame] | 1040 | if (Ctx.getLangOptions().CPlusPlus) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1041 | // C++ [expr.ass]p1: |
Douglas Gregor | bf3af05 | 2008-11-13 20:12:29 +0000 | [diff] [blame] | 1042 | // The result of an assignment operation [...] is an lvalue. |
| 1043 | return LV_Valid; |
| 1044 | |
| 1045 | |
| 1046 | // C99 6.5.16: |
| 1047 | // An assignment expression [...] is not an lvalue. |
| 1048 | return LV_InvalidExpression; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1049 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1050 | case CallExprClass: |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 1051 | case CXXOperatorCallExprClass: |
| 1052 | case CXXMemberCallExprClass: { |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1053 | // C++0x [expr.call]p10 |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 1054 | // A function call is an lvalue if and only if the result type |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1055 | // is an lvalue reference. |
Anders Carlsson | 6dde78f | 2009-05-26 04:57:27 +0000 | [diff] [blame] | 1056 | QualType ReturnType = cast<CallExpr>(this)->getCallReturnType(); |
| 1057 | if (ReturnType->isLValueReferenceType()) |
| 1058 | return LV_Valid; |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1059 | |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 1060 | break; |
| 1061 | } |
Steve Naroff | e638639 | 2007-12-05 04:00:10 +0000 | [diff] [blame] | 1062 | case CompoundLiteralExprClass: // C99 6.5.2.5p5 |
| 1063 | return LV_Valid; |
Chris Lattner | 670a62c | 2008-12-12 05:35:08 +0000 | [diff] [blame] | 1064 | case ChooseExprClass: |
| 1065 | // __builtin_choose_expr is an lvalue if the selected operand is. |
Eli Friedman | 7976932 | 2009-03-04 05:52:32 +0000 | [diff] [blame] | 1066 | return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx)->isLvalue(Ctx); |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1067 | case ExtVectorElementExprClass: |
| 1068 | if (cast<ExtVectorElementExpr>(this)->containsDuplicateElements()) |
Steve Naroff | fec0b49 | 2007-07-30 03:29:09 +0000 | [diff] [blame] | 1069 | return LV_DuplicateVectorComponents; |
| 1070 | return LV_Valid; |
Steve Naroff | 027282d | 2007-11-12 14:34:27 +0000 | [diff] [blame] | 1071 | case ObjCIvarRefExprClass: // ObjC instance variables are lvalues. |
| 1072 | return LV_Valid; |
Steve Naroff | 799a6a6 | 2008-05-30 23:23:16 +0000 | [diff] [blame] | 1073 | case ObjCPropertyRefExprClass: // FIXME: check if read-only property. |
| 1074 | return LV_Valid; |
Fariborz Jahanian | 09105f5 | 2009-08-20 17:02:02 +0000 | [diff] [blame] | 1075 | case ObjCImplicitSetterGetterRefExprClass: // FIXME: check if read-only property. |
Chris Lattner | 670a62c | 2008-12-12 05:35:08 +0000 | [diff] [blame] | 1076 | return LV_Valid; |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 1077 | case PredefinedExprClass: |
Douglas Gregor | 796da18 | 2008-11-04 14:32:21 +0000 | [diff] [blame] | 1078 | return LV_Valid; |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1079 | case CXXDefaultArgExprClass: |
Chris Lattner | 28be73f | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 1080 | return cast<CXXDefaultArgExpr>(this)->getExpr()->isLvalue(Ctx); |
Argyrios Kyrtzidis | 24b41fa | 2008-09-11 04:22:26 +0000 | [diff] [blame] | 1081 | case CXXConditionDeclExprClass: |
| 1082 | return LV_Valid; |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 1083 | case CStyleCastExprClass: |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 1084 | case CXXFunctionalCastExprClass: |
| 1085 | case CXXStaticCastExprClass: |
| 1086 | case CXXDynamicCastExprClass: |
| 1087 | case CXXReinterpretCastExprClass: |
| 1088 | case CXXConstCastExprClass: |
| 1089 | // The result of an explicit cast is an lvalue if the type we are |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1090 | // casting to is an lvalue reference type. See C++ [expr.cast]p1, |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 1091 | // C++ [expr.static.cast]p2, C++ [expr.dynamic.cast]p2, |
| 1092 | // C++ [expr.reinterpret.cast]p1, C++ [expr.const.cast]p1. |
Sebastian Redl | 7c80bd6 | 2009-03-16 23:22:08 +0000 | [diff] [blame] | 1093 | if (cast<ExplicitCastExpr>(this)->getTypeAsWritten()-> |
| 1094 | isLValueReferenceType()) |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 1095 | return LV_Valid; |
| 1096 | break; |
Sebastian Redl | c42e118 | 2008-11-11 11:37:55 +0000 | [diff] [blame] | 1097 | case CXXTypeidExprClass: |
| 1098 | // C++ 5.2.8p1: The result of a typeid expression is an lvalue of ... |
| 1099 | return LV_Valid; |
Anders Carlsson | 6f68027 | 2009-08-16 03:42:12 +0000 | [diff] [blame] | 1100 | case CXXBindTemporaryExprClass: |
| 1101 | return cast<CXXBindTemporaryExpr>(this)->getSubExpr()-> |
| 1102 | isLvalueInternal(Ctx); |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 1103 | case ConditionalOperatorClass: { |
| 1104 | // Complicated handling is only for C++. |
| 1105 | if (!Ctx.getLangOptions().CPlusPlus) |
| 1106 | return LV_InvalidExpression; |
| 1107 | |
| 1108 | // Sema should have taken care to ensure that a CXXTemporaryObjectExpr is |
| 1109 | // everywhere there's an object converted to an rvalue. Also, any other |
| 1110 | // casts should be wrapped by ImplicitCastExprs. There's just the special |
| 1111 | // case involving throws to work out. |
| 1112 | const ConditionalOperator *Cond = cast<ConditionalOperator>(this); |
Douglas Gregor | d5f3a0f | 2009-05-19 20:13:50 +0000 | [diff] [blame] | 1113 | Expr *True = Cond->getTrueExpr(); |
| 1114 | Expr *False = Cond->getFalseExpr(); |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 1115 | // C++0x 5.16p2 |
| 1116 | // If either the second or the third operand has type (cv) void, [...] |
| 1117 | // the result [...] is an rvalue. |
Douglas Gregor | d5f3a0f | 2009-05-19 20:13:50 +0000 | [diff] [blame] | 1118 | if (True->getType()->isVoidType() || False->getType()->isVoidType()) |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 1119 | return LV_InvalidExpression; |
| 1120 | |
| 1121 | // Both sides must be lvalues for the result to be an lvalue. |
Douglas Gregor | d5f3a0f | 2009-05-19 20:13:50 +0000 | [diff] [blame] | 1122 | if (True->isLvalue(Ctx) != LV_Valid || False->isLvalue(Ctx) != LV_Valid) |
Sebastian Redl | 7645850 | 2009-04-17 16:30:52 +0000 | [diff] [blame] | 1123 | return LV_InvalidExpression; |
| 1124 | |
| 1125 | // That's it. |
| 1126 | return LV_Valid; |
| 1127 | } |
| 1128 | |
Douglas Gregor | 3eefb1c | 2009-10-24 04:59:53 +0000 | [diff] [blame] | 1129 | case TemplateIdRefExprClass: { |
| 1130 | const TemplateIdRefExpr *TID = cast<TemplateIdRefExpr>(this); |
| 1131 | TemplateName Template = TID->getTemplateName(); |
| 1132 | NamedDecl *ND = Template.getAsTemplateDecl(); |
| 1133 | if (!ND) |
| 1134 | ND = Template.getAsOverloadedFunctionDecl(); |
| 1135 | if (ND && DeclCanBeLvalue(ND, Ctx)) |
| 1136 | return LV_Valid; |
| 1137 | |
| 1138 | break; |
| 1139 | } |
| 1140 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1141 | default: |
| 1142 | break; |
| 1143 | } |
| 1144 | return LV_InvalidExpression; |
| 1145 | } |
| 1146 | |
| 1147 | /// isModifiableLvalue - C99 6.3.2.1: an lvalue that does not have array type, |
| 1148 | /// does not have an incomplete type, does not have a const-qualified type, and |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1149 | /// if it is a structure or union, does not have any member (including, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1150 | /// recursively, any member or element of all contained aggregates or unions) |
| 1151 | /// with a const-qualified type. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1152 | Expr::isModifiableLvalueResult |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 1153 | Expr::isModifiableLvalue(ASTContext &Ctx, SourceLocation *Loc) const { |
Chris Lattner | 28be73f | 2008-07-26 21:30:36 +0000 | [diff] [blame] | 1154 | isLvalueResult lvalResult = isLvalue(Ctx); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1155 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1156 | switch (lvalResult) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1157 | case LV_Valid: |
Douglas Gregor | ae8d467 | 2008-10-22 00:03:08 +0000 | [diff] [blame] | 1158 | // C++ 3.10p11: Functions cannot be modified, but pointers to |
| 1159 | // functions can be modifiable. |
| 1160 | if (Ctx.getLangOptions().CPlusPlus && TR->isFunctionType()) |
| 1161 | return MLV_NotObjectType; |
| 1162 | break; |
| 1163 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1164 | case LV_NotObjectType: return MLV_NotObjectType; |
| 1165 | case LV_IncompleteVoidType: return MLV_IncompleteVoidType; |
Steve Naroff | fec0b49 | 2007-07-30 03:29:09 +0000 | [diff] [blame] | 1166 | case LV_DuplicateVectorComponents: return MLV_DuplicateVectorComponents; |
Chris Lattner | ca354fa | 2008-11-17 19:51:54 +0000 | [diff] [blame] | 1167 | case LV_InvalidExpression: |
| 1168 | // If the top level is a C-style cast, and the subexpression is a valid |
| 1169 | // lvalue, then this is probably a use of the old-school "cast as lvalue" |
| 1170 | // GCC extension. We don't support it, but we want to produce good |
| 1171 | // diagnostics when it happens so that the user knows why. |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 1172 | if (const CStyleCastExpr *CE = dyn_cast<CStyleCastExpr>(IgnoreParens())) { |
| 1173 | if (CE->getSubExpr()->isLvalue(Ctx) == LV_Valid) { |
| 1174 | if (Loc) |
| 1175 | *Loc = CE->getLParenLoc(); |
Chris Lattner | ca354fa | 2008-11-17 19:51:54 +0000 | [diff] [blame] | 1176 | return MLV_LValueCast; |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 1177 | } |
| 1178 | } |
Chris Lattner | ca354fa | 2008-11-17 19:51:54 +0000 | [diff] [blame] | 1179 | return MLV_InvalidExpression; |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1180 | case LV_MemberFunction: return MLV_MemberFunction; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1181 | } |
Eli Friedman | 04831aa | 2009-03-22 23:26:56 +0000 | [diff] [blame] | 1182 | |
| 1183 | // The following is illegal: |
| 1184 | // void takeclosure(void (^C)(void)); |
| 1185 | // void func() { int x = 1; takeclosure(^{ x = 7; }); } |
| 1186 | // |
Fariborz Jahanian | c3f48cd | 2009-09-14 16:40:48 +0000 | [diff] [blame] | 1187 | if (const BlockDeclRefExpr *BDR = dyn_cast<BlockDeclRefExpr>(this)) { |
Eli Friedman | 04831aa | 2009-03-22 23:26:56 +0000 | [diff] [blame] | 1188 | if (!BDR->isByRef() && isa<VarDecl>(BDR->getDecl())) |
| 1189 | return MLV_NotBlockQualified; |
| 1190 | } |
Daniel Dunbar | 7e88a60 | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 1191 | |
Fariborz Jahanian | c3f48cd | 2009-09-14 16:40:48 +0000 | [diff] [blame] | 1192 | // Assigning to an 'implicit' property? |
Daniel Dunbar | 7e88a60 | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 1193 | if (const ObjCImplicitSetterGetterRefExpr* Expr = |
Fariborz Jahanian | c3f48cd | 2009-09-14 16:40:48 +0000 | [diff] [blame] | 1194 | dyn_cast<ObjCImplicitSetterGetterRefExpr>(this)) { |
| 1195 | if (Expr->getSetterMethod() == 0) |
| 1196 | return MLV_NoSetterProperty; |
| 1197 | } |
Daniel Dunbar | 7e88a60 | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 1198 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1199 | QualType CT = Ctx.getCanonicalType(getType()); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1200 | |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1201 | if (CT.isConstQualified()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1202 | return MLV_ConstQualified; |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1203 | if (CT->isArrayType()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1204 | return MLV_ArrayType; |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 1205 | if (CT->isIncompleteType()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1206 | return MLV_IncompleteType; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1207 | |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1208 | if (const RecordType *r = CT->getAs<RecordType>()) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1209 | if (r->hasConstFields()) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1210 | return MLV_ConstQualified; |
| 1211 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1212 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1213 | return MLV_Valid; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1214 | } |
| 1215 | |
Fariborz Jahanian | 44baa8a | 2009-02-22 18:40:18 +0000 | [diff] [blame] | 1216 | /// isOBJCGCCandidate - Check if an expression is objc gc'able. |
Fariborz Jahanian | 7f4f86a | 2009-09-08 23:38:54 +0000 | [diff] [blame] | 1217 | /// returns true, if it is; false otherwise. |
Fariborz Jahanian | 102e390 | 2009-06-01 21:29:32 +0000 | [diff] [blame] | 1218 | bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const { |
Fariborz Jahanian | 44baa8a | 2009-02-22 18:40:18 +0000 | [diff] [blame] | 1219 | switch (getStmtClass()) { |
| 1220 | default: |
| 1221 | return false; |
| 1222 | case ObjCIvarRefExprClass: |
| 1223 | return true; |
Fariborz Jahanian | 207c521 | 2009-02-23 18:59:50 +0000 | [diff] [blame] | 1224 | case Expr::UnaryOperatorClass: |
Fariborz Jahanian | 102e390 | 2009-06-01 21:29:32 +0000 | [diff] [blame] | 1225 | return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx); |
Fariborz Jahanian | 44baa8a | 2009-02-22 18:40:18 +0000 | [diff] [blame] | 1226 | case ParenExprClass: |
Fariborz Jahanian | 102e390 | 2009-06-01 21:29:32 +0000 | [diff] [blame] | 1227 | return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx); |
Fariborz Jahanian | 44baa8a | 2009-02-22 18:40:18 +0000 | [diff] [blame] | 1228 | case ImplicitCastExprClass: |
Fariborz Jahanian | 102e390 | 2009-06-01 21:29:32 +0000 | [diff] [blame] | 1229 | return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx); |
Fariborz Jahanian | 06b8912 | 2009-05-05 23:28:21 +0000 | [diff] [blame] | 1230 | case CStyleCastExprClass: |
Fariborz Jahanian | 102e390 | 2009-06-01 21:29:32 +0000 | [diff] [blame] | 1231 | return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx); |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 1232 | case DeclRefExprClass: { |
Fariborz Jahanian | 44baa8a | 2009-02-22 18:40:18 +0000 | [diff] [blame] | 1233 | const Decl *D = cast<DeclRefExpr>(this)->getDecl(); |
Fariborz Jahanian | 102e390 | 2009-06-01 21:29:32 +0000 | [diff] [blame] | 1234 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 1235 | if (VD->hasGlobalStorage()) |
| 1236 | return true; |
| 1237 | QualType T = VD->getType(); |
Fariborz Jahanian | 59a53fa | 2009-09-16 18:09:18 +0000 | [diff] [blame] | 1238 | // dereferencing to a pointer is always a gc'able candidate, |
| 1239 | // unless it is __weak. |
Daniel Dunbar | 7e88a60 | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 1240 | return T->isPointerType() && |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1241 | (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak); |
Fariborz Jahanian | 102e390 | 2009-06-01 21:29:32 +0000 | [diff] [blame] | 1242 | } |
Fariborz Jahanian | 44baa8a | 2009-02-22 18:40:18 +0000 | [diff] [blame] | 1243 | return false; |
| 1244 | } |
Douglas Gregor | 83f6faf | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 1245 | case MemberExprClass: { |
Fariborz Jahanian | 44baa8a | 2009-02-22 18:40:18 +0000 | [diff] [blame] | 1246 | const MemberExpr *M = cast<MemberExpr>(this); |
Fariborz Jahanian | 102e390 | 2009-06-01 21:29:32 +0000 | [diff] [blame] | 1247 | return M->getBase()->isOBJCGCCandidate(Ctx); |
Fariborz Jahanian | 44baa8a | 2009-02-22 18:40:18 +0000 | [diff] [blame] | 1248 | } |
| 1249 | case ArraySubscriptExprClass: |
Fariborz Jahanian | 102e390 | 2009-06-01 21:29:32 +0000 | [diff] [blame] | 1250 | return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx); |
Fariborz Jahanian | 44baa8a | 2009-02-22 18:40:18 +0000 | [diff] [blame] | 1251 | } |
| 1252 | } |
Ted Kremenek | 4e99a5f | 2008-01-17 16:57:34 +0000 | [diff] [blame] | 1253 | Expr* Expr::IgnoreParens() { |
| 1254 | Expr* E = this; |
| 1255 | while (ParenExpr* P = dyn_cast<ParenExpr>(E)) |
| 1256 | E = P->getSubExpr(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1257 | |
Ted Kremenek | 4e99a5f | 2008-01-17 16:57:34 +0000 | [diff] [blame] | 1258 | return E; |
| 1259 | } |
| 1260 | |
Chris Lattner | 56f3494 | 2008-02-13 01:02:39 +0000 | [diff] [blame] | 1261 | /// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr |
| 1262 | /// or CastExprs or ImplicitCastExprs, returning their operand. |
| 1263 | Expr *Expr::IgnoreParenCasts() { |
| 1264 | Expr *E = this; |
| 1265 | while (true) { |
| 1266 | if (ParenExpr *P = dyn_cast<ParenExpr>(E)) |
| 1267 | E = P->getSubExpr(); |
| 1268 | else if (CastExpr *P = dyn_cast<CastExpr>(E)) |
| 1269 | E = P->getSubExpr(); |
Chris Lattner | 56f3494 | 2008-02-13 01:02:39 +0000 | [diff] [blame] | 1270 | else |
| 1271 | return E; |
| 1272 | } |
| 1273 | } |
| 1274 | |
Chris Lattner | ecdd841 | 2009-03-13 17:28:01 +0000 | [diff] [blame] | 1275 | /// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the |
| 1276 | /// value (including ptr->int casts of the same size). Strip off any |
| 1277 | /// ParenExpr or CastExprs, returning their operand. |
| 1278 | Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) { |
| 1279 | Expr *E = this; |
| 1280 | while (true) { |
| 1281 | if (ParenExpr *P = dyn_cast<ParenExpr>(E)) { |
| 1282 | E = P->getSubExpr(); |
| 1283 | continue; |
| 1284 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1285 | |
Chris Lattner | ecdd841 | 2009-03-13 17:28:01 +0000 | [diff] [blame] | 1286 | if (CastExpr *P = dyn_cast<CastExpr>(E)) { |
| 1287 | // We ignore integer <-> casts that are of the same width, ptr<->ptr and |
| 1288 | // ptr<->int casts of the same width. We also ignore all identify casts. |
| 1289 | Expr *SE = P->getSubExpr(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1290 | |
Chris Lattner | ecdd841 | 2009-03-13 17:28:01 +0000 | [diff] [blame] | 1291 | if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) { |
| 1292 | E = SE; |
| 1293 | continue; |
| 1294 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1295 | |
Chris Lattner | ecdd841 | 2009-03-13 17:28:01 +0000 | [diff] [blame] | 1296 | if ((E->getType()->isPointerType() || E->getType()->isIntegralType()) && |
| 1297 | (SE->getType()->isPointerType() || SE->getType()->isIntegralType()) && |
| 1298 | Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) { |
| 1299 | E = SE; |
| 1300 | continue; |
| 1301 | } |
| 1302 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1303 | |
Chris Lattner | ecdd841 | 2009-03-13 17:28:01 +0000 | [diff] [blame] | 1304 | return E; |
| 1305 | } |
| 1306 | } |
| 1307 | |
| 1308 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1309 | /// hasAnyTypeDependentArguments - Determines if any of the expressions |
| 1310 | /// in Exprs is type-dependent. |
| 1311 | bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) { |
| 1312 | for (unsigned I = 0; I < NumExprs; ++I) |
| 1313 | if (Exprs[I]->isTypeDependent()) |
| 1314 | return true; |
| 1315 | |
| 1316 | return false; |
| 1317 | } |
| 1318 | |
| 1319 | /// hasAnyValueDependentArguments - Determines if any of the expressions |
| 1320 | /// in Exprs is value-dependent. |
| 1321 | bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) { |
| 1322 | for (unsigned I = 0; I < NumExprs; ++I) |
| 1323 | if (Exprs[I]->isValueDependent()) |
| 1324 | return true; |
| 1325 | |
| 1326 | return false; |
| 1327 | } |
| 1328 | |
Eli Friedman | c9e8f60 | 2009-01-25 02:32:41 +0000 | [diff] [blame] | 1329 | bool Expr::isConstantInitializer(ASTContext &Ctx) const { |
Eli Friedman | c39dc9a | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 1330 | // This function is attempting whether an expression is an initializer |
| 1331 | // which can be evaluated at compile-time. isEvaluatable handles most |
| 1332 | // of the cases, but it can't deal with some initializer-specific |
| 1333 | // expressions, and it can't deal with aggregates; we deal with those here, |
| 1334 | // and fall back to isEvaluatable for the other cases. |
| 1335 | |
Eli Friedman | 1f4a6db | 2009-02-20 02:36:22 +0000 | [diff] [blame] | 1336 | // FIXME: This function assumes the variable being assigned to |
| 1337 | // isn't a reference type! |
| 1338 | |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 1339 | switch (getStmtClass()) { |
Eli Friedman | c39dc9a | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 1340 | default: break; |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 1341 | case StringLiteralClass: |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1342 | case ObjCStringLiteralClass: |
Chris Lattner | eaf2bb8 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 1343 | case ObjCEncodeExprClass: |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 1344 | return true; |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 1345 | case CompoundLiteralExprClass: { |
Eli Friedman | 1f4a6db | 2009-02-20 02:36:22 +0000 | [diff] [blame] | 1346 | // This handles gcc's extension that allows global initializers like |
| 1347 | // "struct x {int x;} x = (struct x) {};". |
| 1348 | // FIXME: This accepts other cases it shouldn't! |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 1349 | const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer(); |
Eli Friedman | c9e8f60 | 2009-01-25 02:32:41 +0000 | [diff] [blame] | 1350 | return Exp->isConstantInitializer(Ctx); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 1351 | } |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 1352 | case InitListExprClass: { |
Eli Friedman | 1f4a6db | 2009-02-20 02:36:22 +0000 | [diff] [blame] | 1353 | // FIXME: This doesn't deal with fields with reference types correctly. |
| 1354 | // FIXME: This incorrectly allows pointers cast to integers to be assigned |
| 1355 | // to bitfields. |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 1356 | const InitListExpr *Exp = cast<InitListExpr>(this); |
| 1357 | unsigned numInits = Exp->getNumInits(); |
| 1358 | for (unsigned i = 0; i < numInits; i++) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1359 | if (!Exp->getInit(i)->isConstantInitializer(Ctx)) |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 1360 | return false; |
| 1361 | } |
Eli Friedman | c39dc9a | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 1362 | return true; |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 1363 | } |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1364 | case ImplicitValueInitExprClass: |
| 1365 | return true; |
Chris Lattner | 3ae9f48 | 2009-10-13 07:14:16 +0000 | [diff] [blame] | 1366 | case ParenExprClass: |
Eli Friedman | c39dc9a | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 1367 | return cast<ParenExpr>(this)->getSubExpr()->isConstantInitializer(Ctx); |
Eli Friedman | c39dc9a | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 1368 | case UnaryOperatorClass: { |
| 1369 | const UnaryOperator* Exp = cast<UnaryOperator>(this); |
| 1370 | if (Exp->getOpcode() == UnaryOperator::Extension) |
| 1371 | return Exp->getSubExpr()->isConstantInitializer(Ctx); |
| 1372 | break; |
| 1373 | } |
Chris Lattner | 3ae9f48 | 2009-10-13 07:14:16 +0000 | [diff] [blame] | 1374 | case BinaryOperatorClass: { |
| 1375 | // Special case &&foo - &&bar. It would be nice to generalize this somehow |
| 1376 | // but this handles the common case. |
| 1377 | const BinaryOperator *Exp = cast<BinaryOperator>(this); |
| 1378 | if (Exp->getOpcode() == BinaryOperator::Sub && |
| 1379 | isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) && |
| 1380 | isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx))) |
| 1381 | return true; |
| 1382 | break; |
| 1383 | } |
Chris Lattner | 81045d8 | 2009-04-21 05:19:11 +0000 | [diff] [blame] | 1384 | case ImplicitCastExprClass: |
Eli Friedman | c39dc9a | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 1385 | case CStyleCastExprClass: |
| 1386 | // Handle casts with a destination that's a struct or union; this |
| 1387 | // deals with both the gcc no-op struct cast extension and the |
| 1388 | // cast-to-union extension. |
| 1389 | if (getType()->isRecordType()) |
| 1390 | return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx); |
Chris Lattner | 430656e | 2009-10-13 22:12:09 +0000 | [diff] [blame] | 1391 | |
| 1392 | // Integer->integer casts can be handled here, which is important for |
| 1393 | // things like (int)(&&x-&&y). Scary but true. |
| 1394 | if (getType()->isIntegerType() && |
| 1395 | cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType()) |
| 1396 | return cast<CastExpr>(this)->getSubExpr()->isConstantInitializer(Ctx); |
| 1397 | |
Eli Friedman | c39dc9a | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 1398 | break; |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 1399 | } |
Eli Friedman | c39dc9a | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 1400 | return isEvaluatable(Ctx); |
Steve Naroff | 38374b0 | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 1401 | } |
| 1402 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1403 | /// isIntegerConstantExpr - this recursive routine will test if an expression is |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1404 | /// an integer constant expression. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1405 | |
| 1406 | /// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero, |
| 1407 | /// comma, etc |
| 1408 | /// |
Chris Lattner | ce0afc0 | 2007-07-18 05:21:20 +0000 | [diff] [blame] | 1409 | /// FIXME: Handle offsetof. Two things to do: Handle GCC's __builtin_offsetof |
| 1410 | /// to support gcc 4.0+ and handle the idiom GCC recognizes with a null pointer |
| 1411 | /// cast+dereference. |
Daniel Dunbar | 2d6744f | 2009-02-18 00:47:45 +0000 | [diff] [blame] | 1412 | |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1413 | // CheckICE - This function does the fundamental ICE checking: the returned |
| 1414 | // ICEDiag contains a Val of 0, 1, or 2, and a possibly null SourceLocation. |
| 1415 | // Note that to reduce code duplication, this helper does no evaluation |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1416 | // itself; the caller checks whether the expression is evaluatable, and |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1417 | // in the rare cases where CheckICE actually cares about the evaluated |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1418 | // value, it calls into Evalute. |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1419 | // |
| 1420 | // Meanings of Val: |
| 1421 | // 0: This expression is an ICE if it can be evaluated by Evaluate. |
| 1422 | // 1: This expression is not an ICE, but if it isn't evaluated, it's |
| 1423 | // a legal subexpression for an ICE. This return value is used to handle |
| 1424 | // the comma operator in C99 mode. |
| 1425 | // 2: This expression is not an ICE, and is not a legal subexpression for one. |
| 1426 | |
| 1427 | struct ICEDiag { |
| 1428 | unsigned Val; |
| 1429 | SourceLocation Loc; |
| 1430 | |
| 1431 | public: |
| 1432 | ICEDiag(unsigned v, SourceLocation l) : Val(v), Loc(l) {} |
| 1433 | ICEDiag() : Val(0) {} |
| 1434 | }; |
| 1435 | |
| 1436 | ICEDiag NoDiag() { return ICEDiag(); } |
| 1437 | |
Eli Friedman | 60ce963 | 2009-02-27 04:07:58 +0000 | [diff] [blame] | 1438 | static ICEDiag CheckEvalInICE(const Expr* E, ASTContext &Ctx) { |
| 1439 | Expr::EvalResult EVResult; |
| 1440 | if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects || |
| 1441 | !EVResult.Val.isInt()) { |
| 1442 | return ICEDiag(2, E->getLocStart()); |
| 1443 | } |
| 1444 | return NoDiag(); |
| 1445 | } |
| 1446 | |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1447 | static ICEDiag CheckICE(const Expr* E, ASTContext &Ctx) { |
Anders Carlsson | c308241 | 2009-03-14 00:33:21 +0000 | [diff] [blame] | 1448 | assert(!E->isValueDependent() && "Should not see value dependent exprs!"); |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1449 | if (!E->getType()->isIntegralType()) { |
| 1450 | return ICEDiag(2, E->getLocStart()); |
Eli Friedman | a6afa76 | 2008-11-13 06:09:17 +0000 | [diff] [blame] | 1451 | } |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1452 | |
| 1453 | switch (E->getStmtClass()) { |
Douglas Gregor | f299124 | 2009-09-10 23:31:45 +0000 | [diff] [blame] | 1454 | #define STMT(Node, Base) case Expr::Node##Class: |
| 1455 | #define EXPR(Node, Base) |
| 1456 | #include "clang/AST/StmtNodes.def" |
| 1457 | case Expr::PredefinedExprClass: |
| 1458 | case Expr::FloatingLiteralClass: |
| 1459 | case Expr::ImaginaryLiteralClass: |
| 1460 | case Expr::StringLiteralClass: |
| 1461 | case Expr::ArraySubscriptExprClass: |
| 1462 | case Expr::MemberExprClass: |
| 1463 | case Expr::CompoundAssignOperatorClass: |
| 1464 | case Expr::CompoundLiteralExprClass: |
| 1465 | case Expr::ExtVectorElementExprClass: |
| 1466 | case Expr::InitListExprClass: |
| 1467 | case Expr::DesignatedInitExprClass: |
| 1468 | case Expr::ImplicitValueInitExprClass: |
| 1469 | case Expr::ParenListExprClass: |
| 1470 | case Expr::VAArgExprClass: |
| 1471 | case Expr::AddrLabelExprClass: |
| 1472 | case Expr::StmtExprClass: |
Douglas Gregor | f299124 | 2009-09-10 23:31:45 +0000 | [diff] [blame] | 1473 | case Expr::CXXMemberCallExprClass: |
| 1474 | case Expr::CXXDynamicCastExprClass: |
| 1475 | case Expr::CXXTypeidExprClass: |
| 1476 | case Expr::CXXNullPtrLiteralExprClass: |
| 1477 | case Expr::CXXThisExprClass: |
| 1478 | case Expr::CXXThrowExprClass: |
| 1479 | case Expr::CXXConditionDeclExprClass: // FIXME: is this correct? |
| 1480 | case Expr::CXXNewExprClass: |
| 1481 | case Expr::CXXDeleteExprClass: |
| 1482 | case Expr::CXXPseudoDestructorExprClass: |
| 1483 | case Expr::UnresolvedFunctionNameExprClass: |
| 1484 | case Expr::UnresolvedDeclRefExprClass: |
| 1485 | case Expr::TemplateIdRefExprClass: |
| 1486 | case Expr::CXXConstructExprClass: |
| 1487 | case Expr::CXXBindTemporaryExprClass: |
| 1488 | case Expr::CXXExprWithTemporariesClass: |
| 1489 | case Expr::CXXTemporaryObjectExprClass: |
| 1490 | case Expr::CXXUnresolvedConstructExprClass: |
| 1491 | case Expr::CXXUnresolvedMemberExprClass: |
| 1492 | case Expr::ObjCStringLiteralClass: |
| 1493 | case Expr::ObjCEncodeExprClass: |
| 1494 | case Expr::ObjCMessageExprClass: |
| 1495 | case Expr::ObjCSelectorExprClass: |
| 1496 | case Expr::ObjCProtocolExprClass: |
| 1497 | case Expr::ObjCIvarRefExprClass: |
| 1498 | case Expr::ObjCPropertyRefExprClass: |
| 1499 | case Expr::ObjCImplicitSetterGetterRefExprClass: |
| 1500 | case Expr::ObjCSuperExprClass: |
| 1501 | case Expr::ObjCIsaExprClass: |
| 1502 | case Expr::ShuffleVectorExprClass: |
| 1503 | case Expr::BlockExprClass: |
| 1504 | case Expr::BlockDeclRefExprClass: |
| 1505 | case Expr::NoStmtClass: |
| 1506 | case Expr::ExprClass: |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1507 | return ICEDiag(2, E->getLocStart()); |
Daniel Dunbar | 7e88a60 | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 1508 | |
Douglas Gregor | 043cad2 | 2009-09-11 00:18:58 +0000 | [diff] [blame] | 1509 | case Expr::GNUNullExprClass: |
| 1510 | // GCC considers the GNU __null value to be an integral constant expression. |
| 1511 | return NoDiag(); |
Daniel Dunbar | 7e88a60 | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 1512 | |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1513 | case Expr::ParenExprClass: |
| 1514 | return CheckICE(cast<ParenExpr>(E)->getSubExpr(), Ctx); |
| 1515 | case Expr::IntegerLiteralClass: |
| 1516 | case Expr::CharacterLiteralClass: |
| 1517 | case Expr::CXXBoolLiteralExprClass: |
| 1518 | case Expr::CXXZeroInitValueExprClass: |
| 1519 | case Expr::TypesCompatibleExprClass: |
| 1520 | case Expr::UnaryTypeTraitExprClass: |
| 1521 | return NoDiag(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1522 | case Expr::CallExprClass: |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1523 | case Expr::CXXOperatorCallExprClass: { |
| 1524 | const CallExpr *CE = cast<CallExpr>(E); |
Eli Friedman | 60ce963 | 2009-02-27 04:07:58 +0000 | [diff] [blame] | 1525 | if (CE->isBuiltinCall(Ctx)) |
| 1526 | return CheckEvalInICE(E, Ctx); |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1527 | return ICEDiag(2, E->getLocStart()); |
Chris Lattner | 2eadfb6 | 2007-07-15 23:32:58 +0000 | [diff] [blame] | 1528 | } |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1529 | case Expr::DeclRefExprClass: |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1530 | if (isa<EnumConstantDecl>(cast<DeclRefExpr>(E)->getDecl())) |
| 1531 | return NoDiag(); |
Sebastian Redl | 4a4251b | 2009-02-07 13:06:23 +0000 | [diff] [blame] | 1532 | if (Ctx.getLangOptions().CPlusPlus && |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1533 | E->getType().getCVRQualifiers() == Qualifiers::Const) { |
Sebastian Redl | 4a4251b | 2009-02-07 13:06:23 +0000 | [diff] [blame] | 1534 | // C++ 7.1.5.1p2 |
| 1535 | // A variable of non-volatile const-qualified integral or enumeration |
| 1536 | // type initialized by an ICE can be used in ICEs. |
| 1537 | if (const VarDecl *Dcl = |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1538 | dyn_cast<VarDecl>(cast<DeclRefExpr>(E)->getDecl())) { |
Douglas Gregor | 78d1583 | 2009-05-26 18:54:04 +0000 | [diff] [blame] | 1539 | if (Dcl->isInitKnownICE()) { |
| 1540 | // We have already checked whether this subexpression is an |
| 1541 | // integral constant expression. |
| 1542 | if (Dcl->isInitICE()) |
| 1543 | return NoDiag(); |
| 1544 | else |
| 1545 | return ICEDiag(2, E->getLocStart()); |
| 1546 | } |
| 1547 | |
| 1548 | if (const Expr *Init = Dcl->getInit()) { |
| 1549 | ICEDiag Result = CheckICE(Init, Ctx); |
| 1550 | // Cache the result of the ICE test. |
| 1551 | Dcl->setInitKnownICE(Ctx, Result.Val == 0); |
| 1552 | return Result; |
| 1553 | } |
Sebastian Redl | 4a4251b | 2009-02-07 13:06:23 +0000 | [diff] [blame] | 1554 | } |
| 1555 | } |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1556 | return ICEDiag(2, E->getLocStart()); |
| 1557 | case Expr::UnaryOperatorClass: { |
| 1558 | const UnaryOperator *Exp = cast<UnaryOperator>(E); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1559 | switch (Exp->getOpcode()) { |
Douglas Gregor | f299124 | 2009-09-10 23:31:45 +0000 | [diff] [blame] | 1560 | case UnaryOperator::PostInc: |
| 1561 | case UnaryOperator::PostDec: |
| 1562 | case UnaryOperator::PreInc: |
| 1563 | case UnaryOperator::PreDec: |
| 1564 | case UnaryOperator::AddrOf: |
| 1565 | case UnaryOperator::Deref: |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1566 | return ICEDiag(2, E->getLocStart()); |
Daniel Dunbar | 7e88a60 | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 1567 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1568 | case UnaryOperator::Extension: |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1569 | case UnaryOperator::LNot: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1570 | case UnaryOperator::Plus: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1571 | case UnaryOperator::Minus: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1572 | case UnaryOperator::Not: |
Eli Friedman | 60ce963 | 2009-02-27 04:07:58 +0000 | [diff] [blame] | 1573 | case UnaryOperator::Real: |
| 1574 | case UnaryOperator::Imag: |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1575 | return CheckICE(Exp->getSubExpr(), Ctx); |
Anders Carlsson | 5a1deb8 | 2008-01-29 15:56:48 +0000 | [diff] [blame] | 1576 | case UnaryOperator::OffsetOf: |
Eli Friedman | 60ce963 | 2009-02-27 04:07:58 +0000 | [diff] [blame] | 1577 | // Note that per C99, offsetof must be an ICE. And AFAIK, using |
| 1578 | // Evaluate matches the proposed gcc behavior for cases like |
| 1579 | // "offsetof(struct s{int x[4];}, x[!.0])". This doesn't affect |
| 1580 | // compliance: we should warn earlier for offsetof expressions with |
| 1581 | // array subscripts that aren't ICEs, and if the array subscripts |
| 1582 | // are ICEs, the value of the offsetof must be an integer constant. |
| 1583 | return CheckEvalInICE(E, Ctx); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1584 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1585 | } |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1586 | case Expr::SizeOfAlignOfExprClass: { |
| 1587 | const SizeOfAlignOfExpr *Exp = cast<SizeOfAlignOfExpr>(E); |
| 1588 | if (Exp->isSizeOf() && Exp->getTypeOfArgument()->isVariableArrayType()) |
| 1589 | return ICEDiag(2, E->getLocStart()); |
| 1590 | return NoDiag(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1591 | } |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1592 | case Expr::BinaryOperatorClass: { |
| 1593 | const BinaryOperator *Exp = cast<BinaryOperator>(E); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1594 | switch (Exp->getOpcode()) { |
Douglas Gregor | f299124 | 2009-09-10 23:31:45 +0000 | [diff] [blame] | 1595 | case BinaryOperator::PtrMemD: |
| 1596 | case BinaryOperator::PtrMemI: |
| 1597 | case BinaryOperator::Assign: |
| 1598 | case BinaryOperator::MulAssign: |
| 1599 | case BinaryOperator::DivAssign: |
| 1600 | case BinaryOperator::RemAssign: |
| 1601 | case BinaryOperator::AddAssign: |
| 1602 | case BinaryOperator::SubAssign: |
| 1603 | case BinaryOperator::ShlAssign: |
| 1604 | case BinaryOperator::ShrAssign: |
| 1605 | case BinaryOperator::AndAssign: |
| 1606 | case BinaryOperator::XorAssign: |
| 1607 | case BinaryOperator::OrAssign: |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1608 | return ICEDiag(2, E->getLocStart()); |
Daniel Dunbar | 7e88a60 | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 1609 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1610 | case BinaryOperator::Mul: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1611 | case BinaryOperator::Div: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1612 | case BinaryOperator::Rem: |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1613 | case BinaryOperator::Add: |
| 1614 | case BinaryOperator::Sub: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1615 | case BinaryOperator::Shl: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1616 | case BinaryOperator::Shr: |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1617 | case BinaryOperator::LT: |
| 1618 | case BinaryOperator::GT: |
| 1619 | case BinaryOperator::LE: |
| 1620 | case BinaryOperator::GE: |
| 1621 | case BinaryOperator::EQ: |
| 1622 | case BinaryOperator::NE: |
| 1623 | case BinaryOperator::And: |
| 1624 | case BinaryOperator::Xor: |
| 1625 | case BinaryOperator::Or: |
| 1626 | case BinaryOperator::Comma: { |
| 1627 | ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); |
| 1628 | ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); |
Eli Friedman | 60ce963 | 2009-02-27 04:07:58 +0000 | [diff] [blame] | 1629 | if (Exp->getOpcode() == BinaryOperator::Div || |
| 1630 | Exp->getOpcode() == BinaryOperator::Rem) { |
| 1631 | // Evaluate gives an error for undefined Div/Rem, so make sure |
| 1632 | // we don't evaluate one. |
| 1633 | if (LHSResult.Val != 2 && RHSResult.Val != 2) { |
| 1634 | llvm::APSInt REval = Exp->getRHS()->EvaluateAsInt(Ctx); |
| 1635 | if (REval == 0) |
| 1636 | return ICEDiag(1, E->getLocStart()); |
| 1637 | if (REval.isSigned() && REval.isAllOnesValue()) { |
| 1638 | llvm::APSInt LEval = Exp->getLHS()->EvaluateAsInt(Ctx); |
| 1639 | if (LEval.isMinSignedValue()) |
| 1640 | return ICEDiag(1, E->getLocStart()); |
| 1641 | } |
| 1642 | } |
| 1643 | } |
| 1644 | if (Exp->getOpcode() == BinaryOperator::Comma) { |
| 1645 | if (Ctx.getLangOptions().C99) { |
| 1646 | // C99 6.6p3 introduces a strange edge case: comma can be in an ICE |
| 1647 | // if it isn't evaluated. |
| 1648 | if (LHSResult.Val == 0 && RHSResult.Val == 0) |
| 1649 | return ICEDiag(1, E->getLocStart()); |
| 1650 | } else { |
| 1651 | // In both C89 and C++, commas in ICEs are illegal. |
| 1652 | return ICEDiag(2, E->getLocStart()); |
| 1653 | } |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1654 | } |
| 1655 | if (LHSResult.Val >= RHSResult.Val) |
| 1656 | return LHSResult; |
| 1657 | return RHSResult; |
| 1658 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1659 | case BinaryOperator::LAnd: |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1660 | case BinaryOperator::LOr: { |
| 1661 | ICEDiag LHSResult = CheckICE(Exp->getLHS(), Ctx); |
| 1662 | ICEDiag RHSResult = CheckICE(Exp->getRHS(), Ctx); |
| 1663 | if (LHSResult.Val == 0 && RHSResult.Val == 1) { |
| 1664 | // Rare case where the RHS has a comma "side-effect"; we need |
| 1665 | // to actually check the condition to see whether the side |
| 1666 | // with the comma is evaluated. |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1667 | if ((Exp->getOpcode() == BinaryOperator::LAnd) != |
Eli Friedman | 60ce963 | 2009-02-27 04:07:58 +0000 | [diff] [blame] | 1668 | (Exp->getLHS()->EvaluateAsInt(Ctx) == 0)) |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1669 | return RHSResult; |
| 1670 | return NoDiag(); |
Eli Friedman | b11e778 | 2008-11-13 02:13:11 +0000 | [diff] [blame] | 1671 | } |
Eli Friedman | 60ce963 | 2009-02-27 04:07:58 +0000 | [diff] [blame] | 1672 | |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1673 | if (LHSResult.Val >= RHSResult.Val) |
| 1674 | return LHSResult; |
| 1675 | return RHSResult; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1676 | } |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1677 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1678 | } |
Douglas Gregor | f299124 | 2009-09-10 23:31:45 +0000 | [diff] [blame] | 1679 | case Expr::CastExprClass: |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1680 | case Expr::ImplicitCastExprClass: |
Douglas Gregor | f299124 | 2009-09-10 23:31:45 +0000 | [diff] [blame] | 1681 | case Expr::ExplicitCastExprClass: |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1682 | case Expr::CStyleCastExprClass: |
Douglas Gregor | 59600d8 | 2009-09-10 17:44:23 +0000 | [diff] [blame] | 1683 | case Expr::CXXFunctionalCastExprClass: |
Douglas Gregor | f299124 | 2009-09-10 23:31:45 +0000 | [diff] [blame] | 1684 | case Expr::CXXNamedCastExprClass: |
Douglas Gregor | 59600d8 | 2009-09-10 17:44:23 +0000 | [diff] [blame] | 1685 | case Expr::CXXStaticCastExprClass: |
| 1686 | case Expr::CXXReinterpretCastExprClass: |
| 1687 | case Expr::CXXConstCastExprClass: { |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1688 | const Expr *SubExpr = cast<CastExpr>(E)->getSubExpr(); |
| 1689 | if (SubExpr->getType()->isIntegralType()) |
| 1690 | return CheckICE(SubExpr, Ctx); |
| 1691 | if (isa<FloatingLiteral>(SubExpr->IgnoreParens())) |
| 1692 | return NoDiag(); |
| 1693 | return ICEDiag(2, E->getLocStart()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1694 | } |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1695 | case Expr::ConditionalOperatorClass: { |
| 1696 | const ConditionalOperator *Exp = cast<ConditionalOperator>(E); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1697 | // If the condition (ignoring parens) is a __builtin_constant_p call, |
Chris Lattner | 28daa53 | 2008-12-12 06:55:44 +0000 | [diff] [blame] | 1698 | // then only the true side is actually considered in an integer constant |
Chris Lattner | 42b83dd | 2008-12-12 18:00:51 +0000 | [diff] [blame] | 1699 | // expression, and it is fully evaluated. This is an important GNU |
| 1700 | // extension. See GCC PR38377 for discussion. |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1701 | if (const CallExpr *CallCE = dyn_cast<CallExpr>(Exp->getCond()->IgnoreParenCasts())) |
Douglas Gregor | 3c385e5 | 2009-02-14 18:57:46 +0000 | [diff] [blame] | 1702 | if (CallCE->isBuiltinCall(Ctx) == Builtin::BI__builtin_constant_p) { |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1703 | Expr::EvalResult EVResult; |
| 1704 | if (!E->Evaluate(EVResult, Ctx) || EVResult.HasSideEffects || |
| 1705 | !EVResult.Val.isInt()) { |
Eli Friedman | 60ce963 | 2009-02-27 04:07:58 +0000 | [diff] [blame] | 1706 | return ICEDiag(2, E->getLocStart()); |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1707 | } |
| 1708 | return NoDiag(); |
Chris Lattner | 42b83dd | 2008-12-12 18:00:51 +0000 | [diff] [blame] | 1709 | } |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1710 | ICEDiag CondResult = CheckICE(Exp->getCond(), Ctx); |
| 1711 | ICEDiag TrueResult = CheckICE(Exp->getTrueExpr(), Ctx); |
| 1712 | ICEDiag FalseResult = CheckICE(Exp->getFalseExpr(), Ctx); |
| 1713 | if (CondResult.Val == 2) |
| 1714 | return CondResult; |
| 1715 | if (TrueResult.Val == 2) |
| 1716 | return TrueResult; |
| 1717 | if (FalseResult.Val == 2) |
| 1718 | return FalseResult; |
| 1719 | if (CondResult.Val == 1) |
| 1720 | return CondResult; |
| 1721 | if (TrueResult.Val == 0 && FalseResult.Val == 0) |
| 1722 | return NoDiag(); |
| 1723 | // Rare case where the diagnostics depend on which side is evaluated |
| 1724 | // Note that if we get here, CondResult is 0, and at least one of |
| 1725 | // TrueResult and FalseResult is non-zero. |
Eli Friedman | 60ce963 | 2009-02-27 04:07:58 +0000 | [diff] [blame] | 1726 | if (Exp->getCond()->EvaluateAsInt(Ctx) == 0) { |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1727 | return FalseResult; |
| 1728 | } |
| 1729 | return TrueResult; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1730 | } |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1731 | case Expr::CXXDefaultArgExprClass: |
| 1732 | return CheckICE(cast<CXXDefaultArgExpr>(E)->getExpr(), Ctx); |
Eli Friedman | 60ce963 | 2009-02-27 04:07:58 +0000 | [diff] [blame] | 1733 | case Expr::ChooseExprClass: { |
Eli Friedman | 7976932 | 2009-03-04 05:52:32 +0000 | [diff] [blame] | 1734 | return CheckICE(cast<ChooseExpr>(E)->getChosenSubExpr(Ctx), Ctx); |
Eli Friedman | 60ce963 | 2009-02-27 04:07:58 +0000 | [diff] [blame] | 1735 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1736 | } |
Daniel Dunbar | 7e88a60 | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 1737 | |
Douglas Gregor | f299124 | 2009-09-10 23:31:45 +0000 | [diff] [blame] | 1738 | // Silence a GCC warning |
| 1739 | return ICEDiag(2, E->getLocStart()); |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1740 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1741 | |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1742 | bool Expr::isIntegerConstantExpr(llvm::APSInt &Result, ASTContext &Ctx, |
| 1743 | SourceLocation *Loc, bool isEvaluated) const { |
| 1744 | ICEDiag d = CheckICE(this, Ctx); |
| 1745 | if (d.Val != 0) { |
| 1746 | if (Loc) *Loc = d.Loc; |
| 1747 | return false; |
| 1748 | } |
| 1749 | EvalResult EvalResult; |
Eli Friedman | 60ce963 | 2009-02-27 04:07:58 +0000 | [diff] [blame] | 1750 | if (!Evaluate(EvalResult, Ctx)) |
| 1751 | assert(0 && "ICE cannot be evaluated!"); |
| 1752 | assert(!EvalResult.HasSideEffects && "ICE with side effects!"); |
| 1753 | assert(EvalResult.Val.isInt() && "ICE that isn't integer!"); |
Eli Friedman | e28d719 | 2009-02-26 09:29:13 +0000 | [diff] [blame] | 1754 | Result = EvalResult.Val.getInt(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1755 | return true; |
| 1756 | } |
| 1757 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1758 | /// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an |
| 1759 | /// integer constant expression with the value zero, or if this is one that is |
| 1760 | /// cast to void*. |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1761 | bool Expr::isNullPointerConstant(ASTContext &Ctx, |
| 1762 | NullPointerConstantValueDependence NPC) const { |
| 1763 | if (isValueDependent()) { |
| 1764 | switch (NPC) { |
| 1765 | case NPC_NeverValueDependent: |
| 1766 | assert(false && "Unexpected value dependent expression!"); |
| 1767 | // If the unthinkable happens, fall through to the safest alternative. |
| 1768 | |
| 1769 | case NPC_ValueDependentIsNull: |
| 1770 | return isTypeDependent() || getType()->isIntegralType(); |
| 1771 | |
| 1772 | case NPC_ValueDependentIsNotNull: |
| 1773 | return false; |
| 1774 | } |
| 1775 | } |
Daniel Dunbar | f515b22 | 2009-09-18 08:46:16 +0000 | [diff] [blame] | 1776 | |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1777 | // Strip off a cast to void*, if it exists. Except in C++. |
Argyrios Kyrtzidis | 0835a3c | 2008-08-18 23:01:59 +0000 | [diff] [blame] | 1778 | if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) { |
Sebastian Redl | 6215dee | 2008-11-04 11:45:54 +0000 | [diff] [blame] | 1779 | if (!Ctx.getLangOptions().CPlusPlus) { |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1780 | // Check that it is a cast to void*. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1781 | if (const PointerType *PT = CE->getType()->getAs<PointerType>()) { |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1782 | QualType Pointee = PT->getPointeeType(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1783 | if (!Pointee.hasQualifiers() && |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1784 | Pointee->isVoidType() && // to void* |
| 1785 | CE->getSubExpr()->getType()->isIntegerType()) // from int. |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1786 | return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC); |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 1787 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1788 | } |
Steve Naroff | aa58f00 | 2008-01-14 16:10:57 +0000 | [diff] [blame] | 1789 | } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) { |
| 1790 | // Ignore the ImplicitCastExpr type entirely. |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1791 | return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC); |
Steve Naroff | aa58f00 | 2008-01-14 16:10:57 +0000 | [diff] [blame] | 1792 | } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) { |
| 1793 | // Accept ((void*)0) as a null pointer constant, as many other |
| 1794 | // implementations do. |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1795 | return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1796 | } else if (const CXXDefaultArgExpr *DefaultArg |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 1797 | = dyn_cast<CXXDefaultArgExpr>(this)) { |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1798 | // See through default argument expressions |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 1799 | return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC); |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 1800 | } else if (isa<GNUNullExpr>(this)) { |
| 1801 | // The GNU __null extension is always a null pointer constant. |
| 1802 | return true; |
Steve Naroff | aaffbf7 | 2008-01-14 02:53:34 +0000 | [diff] [blame] | 1803 | } |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 1804 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 1805 | // C++0x nullptr_t is always a null pointer constant. |
| 1806 | if (getType()->isNullPtrType()) |
| 1807 | return true; |
| 1808 | |
Steve Naroff | aa58f00 | 2008-01-14 16:10:57 +0000 | [diff] [blame] | 1809 | // This expression must be an integer type. |
Fariborz Jahanian | 56fc0d1 | 2009-10-06 00:09:31 +0000 | [diff] [blame] | 1810 | if (!getType()->isIntegerType() || |
| 1811 | (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType())) |
Steve Naroff | aa58f00 | 2008-01-14 16:10:57 +0000 | [diff] [blame] | 1812 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1813 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1814 | // If we have an integer constant expression, we need to *evaluate* it and |
| 1815 | // test for the value 0. |
Eli Friedman | 09de176 | 2009-04-25 22:37:12 +0000 | [diff] [blame] | 1816 | llvm::APSInt Result; |
| 1817 | return isIntegerConstantExpr(Result, Ctx) && Result == 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1818 | } |
Steve Naroff | 31a4584 | 2007-07-28 23:10:27 +0000 | [diff] [blame] | 1819 | |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1820 | FieldDecl *Expr::getBitField() { |
Douglas Gregor | 6f4a69a | 2009-07-06 15:38:40 +0000 | [diff] [blame] | 1821 | Expr *E = this->IgnoreParens(); |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1822 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1823 | if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E)) |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 1824 | if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl())) |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 1825 | if (Field->isBitField()) |
| 1826 | return Field; |
| 1827 | |
| 1828 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) |
| 1829 | if (BinOp->isAssignmentOp() && BinOp->getLHS()) |
| 1830 | return BinOp->getLHS()->getBitField(); |
| 1831 | |
| 1832 | return 0; |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 1833 | } |
| 1834 | |
Chris Lattner | 2140e90 | 2009-02-16 22:14:05 +0000 | [diff] [blame] | 1835 | /// isArrow - Return true if the base expression is a pointer to vector, |
| 1836 | /// return false if the base expression is a vector. |
| 1837 | bool ExtVectorElementExpr::isArrow() const { |
| 1838 | return getBase()->getType()->isPointerType(); |
| 1839 | } |
| 1840 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1841 | unsigned ExtVectorElementExpr::getNumElements() const { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1842 | if (const VectorType *VT = getType()->getAs<VectorType>()) |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1843 | return VT->getNumElements(); |
| 1844 | return 1; |
Chris Lattner | 4d0ac88 | 2007-08-03 16:00:20 +0000 | [diff] [blame] | 1845 | } |
| 1846 | |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1847 | /// containsDuplicateElements - Return true if any element access is repeated. |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 1848 | bool ExtVectorElementExpr::containsDuplicateElements() const { |
Daniel Dunbar | a2b34eb | 2009-10-18 02:09:09 +0000 | [diff] [blame] | 1849 | // FIXME: Refactor this code to an accessor on the AST node which returns the |
| 1850 | // "type" of component access, and share with code below and in Sema. |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 1851 | llvm::StringRef Comp = Accessor->getName(); |
Nate Begeman | 190d6a2 | 2009-01-18 02:01:21 +0000 | [diff] [blame] | 1852 | |
| 1853 | // Halving swizzles do not contain duplicate elements. |
Daniel Dunbar | 1502742 | 2009-10-17 23:53:04 +0000 | [diff] [blame] | 1854 | if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd") |
Nate Begeman | 190d6a2 | 2009-01-18 02:01:21 +0000 | [diff] [blame] | 1855 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1856 | |
Nate Begeman | 190d6a2 | 2009-01-18 02:01:21 +0000 | [diff] [blame] | 1857 | // Advance past s-char prefix on hex swizzles. |
Daniel Dunbar | 1502742 | 2009-10-17 23:53:04 +0000 | [diff] [blame] | 1858 | if (Comp[0] == 's' || Comp[0] == 'S') |
| 1859 | Comp = Comp.substr(1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1860 | |
Daniel Dunbar | 1502742 | 2009-10-17 23:53:04 +0000 | [diff] [blame] | 1861 | for (unsigned i = 0, e = Comp.size(); i != e; ++i) |
| 1862 | if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos) |
Steve Naroff | fec0b49 | 2007-07-30 03:29:09 +0000 | [diff] [blame] | 1863 | return true; |
Daniel Dunbar | 1502742 | 2009-10-17 23:53:04 +0000 | [diff] [blame] | 1864 | |
Steve Naroff | fec0b49 | 2007-07-30 03:29:09 +0000 | [diff] [blame] | 1865 | return false; |
| 1866 | } |
Chris Lattner | b8f849d | 2007-08-02 23:36:59 +0000 | [diff] [blame] | 1867 | |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1868 | /// getEncodedElementAccess - We encode the fields as a llvm ConstantArray. |
Nate Begeman | 3b8d116 | 2008-05-13 21:03:02 +0000 | [diff] [blame] | 1869 | void ExtVectorElementExpr::getEncodedElementAccess( |
| 1870 | llvm::SmallVectorImpl<unsigned> &Elts) const { |
Daniel Dunbar | 4b55b24 | 2009-10-18 02:09:31 +0000 | [diff] [blame] | 1871 | llvm::StringRef Comp = Accessor->getName(); |
| 1872 | if (Comp[0] == 's' || Comp[0] == 'S') |
| 1873 | Comp = Comp.substr(1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1874 | |
Daniel Dunbar | 4b55b24 | 2009-10-18 02:09:31 +0000 | [diff] [blame] | 1875 | bool isHi = Comp == "hi"; |
| 1876 | bool isLo = Comp == "lo"; |
| 1877 | bool isEven = Comp == "even"; |
| 1878 | bool isOdd = Comp == "odd"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1879 | |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1880 | for (unsigned i = 0, e = getNumElements(); i != e; ++i) { |
| 1881 | uint64_t Index; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1882 | |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1883 | if (isHi) |
| 1884 | Index = e + i; |
| 1885 | else if (isLo) |
| 1886 | Index = i; |
| 1887 | else if (isEven) |
| 1888 | Index = 2 * i; |
| 1889 | else if (isOdd) |
| 1890 | Index = 2 * i + 1; |
| 1891 | else |
Daniel Dunbar | 4b55b24 | 2009-10-18 02:09:31 +0000 | [diff] [blame] | 1892 | Index = ExtVectorType::getAccessorIdx(Comp[i]); |
Chris Lattner | b8f849d | 2007-08-02 23:36:59 +0000 | [diff] [blame] | 1893 | |
Nate Begeman | 3b8d116 | 2008-05-13 21:03:02 +0000 | [diff] [blame] | 1894 | Elts.push_back(Index); |
Chris Lattner | b8f849d | 2007-08-02 23:36:59 +0000 | [diff] [blame] | 1895 | } |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 1896 | } |
| 1897 | |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 1898 | // constructor for instance messages. |
Steve Naroff | bcfb06a | 2007-09-28 22:22:11 +0000 | [diff] [blame] | 1899 | ObjCMessageExpr::ObjCMessageExpr(Expr *receiver, Selector selInfo, |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1900 | QualType retType, ObjCMethodDecl *mproto, |
Steve Naroff | db611d5 | 2007-11-03 16:37:59 +0000 | [diff] [blame] | 1901 | SourceLocation LBrac, SourceLocation RBrac, |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 1902 | Expr **ArgExprs, unsigned nargs) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1903 | : Expr(ObjCMessageExprClass, retType), SelName(selInfo), |
Ted Kremenek | ea958e57 | 2008-05-01 17:26:20 +0000 | [diff] [blame] | 1904 | MethodProto(mproto) { |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 1905 | NumArgs = nargs; |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1906 | SubExprs = new Stmt*[NumArgs+1]; |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 1907 | SubExprs[RECEIVER] = receiver; |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 1908 | if (NumArgs) { |
| 1909 | for (unsigned i = 0; i != NumArgs; ++i) |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 1910 | SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]); |
| 1911 | } |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 1912 | LBracloc = LBrac; |
| 1913 | RBracloc = RBrac; |
| 1914 | } |
| 1915 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1916 | // constructor for class messages. |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 1917 | // FIXME: clsName should be typed to ObjCInterfaceType |
Steve Naroff | bcfb06a | 2007-09-28 22:22:11 +0000 | [diff] [blame] | 1918 | ObjCMessageExpr::ObjCMessageExpr(IdentifierInfo *clsName, Selector selInfo, |
Ted Kremenek | a526c5c | 2008-01-07 19:49:32 +0000 | [diff] [blame] | 1919 | QualType retType, ObjCMethodDecl *mproto, |
Steve Naroff | db611d5 | 2007-11-03 16:37:59 +0000 | [diff] [blame] | 1920 | SourceLocation LBrac, SourceLocation RBrac, |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 1921 | Expr **ArgExprs, unsigned nargs) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1922 | : Expr(ObjCMessageExprClass, retType), SelName(selInfo), |
Ted Kremenek | ea958e57 | 2008-05-01 17:26:20 +0000 | [diff] [blame] | 1923 | MethodProto(mproto) { |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 1924 | NumArgs = nargs; |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 1925 | SubExprs = new Stmt*[NumArgs+1]; |
Ted Kremenek | 4df728e | 2008-06-24 15:50:53 +0000 | [diff] [blame] | 1926 | SubExprs[RECEIVER] = (Expr*) ((uintptr_t) clsName | IsClsMethDeclUnknown); |
Steve Naroff | 49f109c | 2007-11-15 13:05:42 +0000 | [diff] [blame] | 1927 | if (NumArgs) { |
| 1928 | for (unsigned i = 0; i != NumArgs; ++i) |
Steve Naroff | 68d331a | 2007-09-27 14:38:14 +0000 | [diff] [blame] | 1929 | SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]); |
| 1930 | } |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 1931 | LBracloc = LBrac; |
| 1932 | RBracloc = RBrac; |
| 1933 | } |
| 1934 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1935 | // constructor for class messages. |
Ted Kremenek | 4df728e | 2008-06-24 15:50:53 +0000 | [diff] [blame] | 1936 | ObjCMessageExpr::ObjCMessageExpr(ObjCInterfaceDecl *cls, Selector selInfo, |
| 1937 | QualType retType, ObjCMethodDecl *mproto, |
| 1938 | SourceLocation LBrac, SourceLocation RBrac, |
| 1939 | Expr **ArgExprs, unsigned nargs) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1940 | : Expr(ObjCMessageExprClass, retType), SelName(selInfo), |
Ted Kremenek | 4df728e | 2008-06-24 15:50:53 +0000 | [diff] [blame] | 1941 | MethodProto(mproto) { |
| 1942 | NumArgs = nargs; |
| 1943 | SubExprs = new Stmt*[NumArgs+1]; |
| 1944 | SubExprs[RECEIVER] = (Expr*) ((uintptr_t) cls | IsClsMethDeclKnown); |
| 1945 | if (NumArgs) { |
| 1946 | for (unsigned i = 0; i != NumArgs; ++i) |
| 1947 | SubExprs[i+ARGS_START] = static_cast<Expr *>(ArgExprs[i]); |
| 1948 | } |
| 1949 | LBracloc = LBrac; |
| 1950 | RBracloc = RBrac; |
| 1951 | } |
| 1952 | |
| 1953 | ObjCMessageExpr::ClassInfo ObjCMessageExpr::getClassInfo() const { |
| 1954 | uintptr_t x = (uintptr_t) SubExprs[RECEIVER]; |
| 1955 | switch (x & Flags) { |
| 1956 | default: |
| 1957 | assert(false && "Invalid ObjCMessageExpr."); |
| 1958 | case IsInstMeth: |
| 1959 | return ClassInfo(0, 0); |
| 1960 | case IsClsMethDeclUnknown: |
| 1961 | return ClassInfo(0, (IdentifierInfo*) (x & ~Flags)); |
| 1962 | case IsClsMethDeclKnown: { |
| 1963 | ObjCInterfaceDecl* D = (ObjCInterfaceDecl*) (x & ~Flags); |
| 1964 | return ClassInfo(D, D->getIdentifier()); |
| 1965 | } |
| 1966 | } |
| 1967 | } |
| 1968 | |
Chris Lattner | 0389e6b | 2009-04-26 00:44:05 +0000 | [diff] [blame] | 1969 | void ObjCMessageExpr::setClassInfo(const ObjCMessageExpr::ClassInfo &CI) { |
| 1970 | if (CI.first == 0 && CI.second == 0) |
| 1971 | SubExprs[RECEIVER] = (Expr*)((uintptr_t)0 | IsInstMeth); |
| 1972 | else if (CI.first == 0) |
| 1973 | SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.second | IsClsMethDeclUnknown); |
| 1974 | else |
| 1975 | SubExprs[RECEIVER] = (Expr*)((uintptr_t)CI.first | IsClsMethDeclKnown); |
| 1976 | } |
| 1977 | |
| 1978 | |
Chris Lattner | 27437ca | 2007-10-25 00:29:32 +0000 | [diff] [blame] | 1979 | bool ChooseExpr::isConditionTrue(ASTContext &C) const { |
Eli Friedman | 9a901bb | 2009-04-26 19:19:15 +0000 | [diff] [blame] | 1980 | return getCond()->EvaluateAsInt(C) != 0; |
Chris Lattner | 27437ca | 2007-10-25 00:29:32 +0000 | [diff] [blame] | 1981 | } |
| 1982 | |
Nate Begeman | 888376a | 2009-08-12 02:28:50 +0000 | [diff] [blame] | 1983 | void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs, |
| 1984 | unsigned NumExprs) { |
| 1985 | if (SubExprs) C.Deallocate(SubExprs); |
| 1986 | |
| 1987 | SubExprs = new (C) Stmt* [NumExprs]; |
Douglas Gregor | 94cd5d1 | 2009-04-16 00:01:45 +0000 | [diff] [blame] | 1988 | this->NumExprs = NumExprs; |
| 1989 | memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1990 | } |
Nate Begeman | 888376a | 2009-08-12 02:28:50 +0000 | [diff] [blame] | 1991 | |
| 1992 | void ShuffleVectorExpr::DoDestroy(ASTContext& C) { |
| 1993 | DestroyChildren(C); |
| 1994 | if (SubExprs) C.Deallocate(SubExprs); |
| 1995 | this->~ShuffleVectorExpr(); |
| 1996 | C.Deallocate(this); |
Douglas Gregor | 94cd5d1 | 2009-04-16 00:01:45 +0000 | [diff] [blame] | 1997 | } |
| 1998 | |
Douglas Gregor | 42602bb | 2009-08-07 06:08:38 +0000 | [diff] [blame] | 1999 | void SizeOfAlignOfExpr::DoDestroy(ASTContext& C) { |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2000 | // Override default behavior of traversing children. If this has a type |
| 2001 | // operand and the type is a variable-length array, the child iteration |
| 2002 | // will iterate over the size expression. However, this expression belongs |
| 2003 | // to the type, not to this, so we don't want to delete it. |
| 2004 | // We still want to delete this expression. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 2005 | if (isArgumentType()) { |
| 2006 | this->~SizeOfAlignOfExpr(); |
| 2007 | C.Deallocate(this); |
| 2008 | } |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2009 | else |
Douglas Gregor | 42602bb | 2009-08-07 06:08:38 +0000 | [diff] [blame] | 2010 | Expr::DoDestroy(C); |
Daniel Dunbar | 9048891 | 2008-08-28 18:02:04 +0000 | [diff] [blame] | 2011 | } |
| 2012 | |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 2013 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2014 | // DesignatedInitExpr |
| 2015 | //===----------------------------------------------------------------------===// |
| 2016 | |
| 2017 | IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() { |
| 2018 | assert(Kind == FieldDesignator && "Only valid on a field designator"); |
| 2019 | if (Field.NameOrField & 0x01) |
| 2020 | return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01); |
| 2021 | else |
| 2022 | return getField()->getIdentifier(); |
| 2023 | } |
| 2024 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2025 | DesignatedInitExpr::DesignatedInitExpr(QualType Ty, unsigned NumDesignators, |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2026 | const Designator *Designators, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2027 | SourceLocation EqualOrColonLoc, |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2028 | bool GNUSyntax, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2029 | Expr **IndexExprs, |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2030 | unsigned NumIndexExprs, |
| 2031 | Expr *Init) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2032 | : Expr(DesignatedInitExprClass, Ty, |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2033 | Init->isTypeDependent(), Init->isValueDependent()), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2034 | EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax), |
| 2035 | NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) { |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2036 | this->Designators = new Designator[NumDesignators]; |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2037 | |
| 2038 | // Record the initializer itself. |
| 2039 | child_iterator Child = child_begin(); |
| 2040 | *Child++ = Init; |
| 2041 | |
| 2042 | // Copy the designators and their subexpressions, computing |
| 2043 | // value-dependence along the way. |
| 2044 | unsigned IndexIdx = 0; |
| 2045 | for (unsigned I = 0; I != NumDesignators; ++I) { |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2046 | this->Designators[I] = Designators[I]; |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2047 | |
| 2048 | if (this->Designators[I].isArrayDesignator()) { |
| 2049 | // Compute type- and value-dependence. |
| 2050 | Expr *Index = IndexExprs[IndexIdx]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2051 | ValueDependent = ValueDependent || |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2052 | Index->isTypeDependent() || Index->isValueDependent(); |
| 2053 | |
| 2054 | // Copy the index expressions into permanent storage. |
| 2055 | *Child++ = IndexExprs[IndexIdx++]; |
| 2056 | } else if (this->Designators[I].isArrayRangeDesignator()) { |
| 2057 | // Compute type- and value-dependence. |
| 2058 | Expr *Start = IndexExprs[IndexIdx]; |
| 2059 | Expr *End = IndexExprs[IndexIdx + 1]; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2060 | ValueDependent = ValueDependent || |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2061 | Start->isTypeDependent() || Start->isValueDependent() || |
| 2062 | End->isTypeDependent() || End->isValueDependent(); |
| 2063 | |
| 2064 | // Copy the start/end expressions into permanent storage. |
| 2065 | *Child++ = IndexExprs[IndexIdx++]; |
| 2066 | *Child++ = IndexExprs[IndexIdx++]; |
| 2067 | } |
| 2068 | } |
| 2069 | |
| 2070 | assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions"); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2071 | } |
| 2072 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2073 | DesignatedInitExpr * |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2074 | DesignatedInitExpr::Create(ASTContext &C, Designator *Designators, |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2075 | unsigned NumDesignators, |
| 2076 | Expr **IndexExprs, unsigned NumIndexExprs, |
| 2077 | SourceLocation ColonOrEqualLoc, |
| 2078 | bool UsesColonSyntax, Expr *Init) { |
Steve Naroff | c0ac492 | 2009-01-27 23:20:32 +0000 | [diff] [blame] | 2079 | void *Mem = C.Allocate(sizeof(DesignatedInitExpr) + |
Steve Naroff | c0ac492 | 2009-01-27 23:20:32 +0000 | [diff] [blame] | 2080 | sizeof(Stmt *) * (NumIndexExprs + 1), 8); |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2081 | return new (Mem) DesignatedInitExpr(C.VoidTy, NumDesignators, Designators, |
| 2082 | ColonOrEqualLoc, UsesColonSyntax, |
| 2083 | IndexExprs, NumIndexExprs, Init); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2084 | } |
| 2085 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2086 | DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C, |
Douglas Gregor | d077d75 | 2009-04-16 00:55:48 +0000 | [diff] [blame] | 2087 | unsigned NumIndexExprs) { |
| 2088 | void *Mem = C.Allocate(sizeof(DesignatedInitExpr) + |
| 2089 | sizeof(Stmt *) * (NumIndexExprs + 1), 8); |
| 2090 | return new (Mem) DesignatedInitExpr(NumIndexExprs + 1); |
| 2091 | } |
| 2092 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2093 | void DesignatedInitExpr::setDesignators(const Designator *Desigs, |
Douglas Gregor | d077d75 | 2009-04-16 00:55:48 +0000 | [diff] [blame] | 2094 | unsigned NumDesigs) { |
| 2095 | if (Designators) |
| 2096 | delete [] Designators; |
| 2097 | |
| 2098 | Designators = new Designator[NumDesigs]; |
| 2099 | NumDesignators = NumDesigs; |
| 2100 | for (unsigned I = 0; I != NumDesigs; ++I) |
| 2101 | Designators[I] = Desigs[I]; |
| 2102 | } |
| 2103 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2104 | SourceRange DesignatedInitExpr::getSourceRange() const { |
| 2105 | SourceLocation StartLoc; |
Chris Lattner | d603eaa | 2009-02-16 22:33:34 +0000 | [diff] [blame] | 2106 | Designator &First = |
| 2107 | *const_cast<DesignatedInitExpr*>(this)->designators_begin(); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2108 | if (First.isFieldDesignator()) { |
Douglas Gregor | eeae8f0 | 2009-03-28 00:41:23 +0000 | [diff] [blame] | 2109 | if (GNUSyntax) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2110 | StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc); |
| 2111 | else |
| 2112 | StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc); |
| 2113 | } else |
Chris Lattner | d603eaa | 2009-02-16 22:33:34 +0000 | [diff] [blame] | 2114 | StartLoc = |
| 2115 | SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2116 | return SourceRange(StartLoc, getInit()->getSourceRange().getEnd()); |
| 2117 | } |
| 2118 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2119 | Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) { |
| 2120 | assert(D.Kind == Designator::ArrayDesignator && "Requires array designator"); |
| 2121 | char* Ptr = static_cast<char*>(static_cast<void *>(this)); |
| 2122 | Ptr += sizeof(DesignatedInitExpr); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2123 | Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr)); |
| 2124 | return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1)); |
| 2125 | } |
| 2126 | |
| 2127 | Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2128 | assert(D.Kind == Designator::ArrayRangeDesignator && |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2129 | "Requires array range designator"); |
| 2130 | char* Ptr = static_cast<char*>(static_cast<void *>(this)); |
| 2131 | Ptr += sizeof(DesignatedInitExpr); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2132 | Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr)); |
| 2133 | return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1)); |
| 2134 | } |
| 2135 | |
| 2136 | Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2137 | assert(D.Kind == Designator::ArrayRangeDesignator && |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2138 | "Requires array range designator"); |
| 2139 | char* Ptr = static_cast<char*>(static_cast<void *>(this)); |
| 2140 | Ptr += sizeof(DesignatedInitExpr); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2141 | Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr)); |
| 2142 | return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2)); |
| 2143 | } |
| 2144 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2145 | /// \brief Replaces the designator at index @p Idx with the series |
| 2146 | /// of designators in [First, Last). |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2147 | void DesignatedInitExpr::ExpandDesignator(unsigned Idx, |
| 2148 | const Designator *First, |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2149 | const Designator *Last) { |
| 2150 | unsigned NumNewDesignators = Last - First; |
| 2151 | if (NumNewDesignators == 0) { |
| 2152 | std::copy_backward(Designators + Idx + 1, |
| 2153 | Designators + NumDesignators, |
| 2154 | Designators + Idx); |
| 2155 | --NumNewDesignators; |
| 2156 | return; |
| 2157 | } else if (NumNewDesignators == 1) { |
| 2158 | Designators[Idx] = *First; |
| 2159 | return; |
| 2160 | } |
| 2161 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2162 | Designator *NewDesignators |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2163 | = new Designator[NumDesignators - 1 + NumNewDesignators]; |
| 2164 | std::copy(Designators, Designators + Idx, NewDesignators); |
| 2165 | std::copy(First, Last, NewDesignators + Idx); |
| 2166 | std::copy(Designators + Idx + 1, Designators + NumDesignators, |
| 2167 | NewDesignators + Idx + NumNewDesignators); |
| 2168 | delete [] Designators; |
| 2169 | Designators = NewDesignators; |
| 2170 | NumDesignators = NumDesignators - 1 + NumNewDesignators; |
| 2171 | } |
| 2172 | |
Douglas Gregor | 42602bb | 2009-08-07 06:08:38 +0000 | [diff] [blame] | 2173 | void DesignatedInitExpr::DoDestroy(ASTContext &C) { |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2174 | delete [] Designators; |
Douglas Gregor | 42602bb | 2009-08-07 06:08:38 +0000 | [diff] [blame] | 2175 | Expr::DoDestroy(C); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2176 | } |
| 2177 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2178 | ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc, |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 2179 | Expr **exprs, unsigned nexprs, |
| 2180 | SourceLocation rparenloc) |
| 2181 | : Expr(ParenListExprClass, QualType(), |
| 2182 | hasAnyTypeDependentArguments(exprs, nexprs), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2183 | hasAnyValueDependentArguments(exprs, nexprs)), |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 2184 | NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2185 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 2186 | Exprs = new (C) Stmt*[nexprs]; |
| 2187 | for (unsigned i = 0; i != nexprs; ++i) |
| 2188 | Exprs[i] = exprs[i]; |
| 2189 | } |
| 2190 | |
| 2191 | void ParenListExpr::DoDestroy(ASTContext& C) { |
| 2192 | DestroyChildren(C); |
| 2193 | if (Exprs) C.Deallocate(Exprs); |
| 2194 | this->~ParenListExpr(); |
| 2195 | C.Deallocate(this); |
| 2196 | } |
| 2197 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2198 | //===----------------------------------------------------------------------===// |
Ted Kremenek | ce2fc3a | 2008-10-27 18:40:21 +0000 | [diff] [blame] | 2199 | // ExprIterator. |
| 2200 | //===----------------------------------------------------------------------===// |
| 2201 | |
| 2202 | Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); } |
| 2203 | Expr* ExprIterator::operator*() const { return cast<Expr>(*I); } |
| 2204 | Expr* ExprIterator::operator->() const { return cast<Expr>(*I); } |
| 2205 | const Expr* ConstExprIterator::operator[](size_t idx) const { |
| 2206 | return cast<Expr>(I[idx]); |
| 2207 | } |
| 2208 | const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); } |
| 2209 | const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); } |
| 2210 | |
| 2211 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 2212 | // Child Iterators for iterating over subexpressions/substatements |
| 2213 | //===----------------------------------------------------------------------===// |
| 2214 | |
| 2215 | // DeclRefExpr |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 2216 | Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); } |
| 2217 | Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 2218 | |
Steve Naroff | 7779db4 | 2007-11-12 14:29:37 +0000 | [diff] [blame] | 2219 | // ObjCIvarRefExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2220 | Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; } |
| 2221 | Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; } |
Steve Naroff | 7779db4 | 2007-11-12 14:29:37 +0000 | [diff] [blame] | 2222 | |
Steve Naroff | e3e9add | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 2223 | // ObjCPropertyRefExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2224 | Stmt::child_iterator ObjCPropertyRefExpr::child_begin() { return &Base; } |
| 2225 | Stmt::child_iterator ObjCPropertyRefExpr::child_end() { return &Base+1; } |
Steve Naroff | ae78407 | 2008-05-30 00:40:33 +0000 | [diff] [blame] | 2226 | |
Fariborz Jahanian | 09105f5 | 2009-08-20 17:02:02 +0000 | [diff] [blame] | 2227 | // ObjCImplicitSetterGetterRefExpr |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2228 | Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_begin() { |
| 2229 | return &Base; |
Fariborz Jahanian | 154440e | 2009-08-18 20:50:23 +0000 | [diff] [blame] | 2230 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2231 | Stmt::child_iterator ObjCImplicitSetterGetterRefExpr::child_end() { |
| 2232 | return &Base+1; |
Fariborz Jahanian | 154440e | 2009-08-18 20:50:23 +0000 | [diff] [blame] | 2233 | } |
Fariborz Jahanian | 5daf570 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 2234 | |
Douglas Gregor | cd9b46e | 2008-11-04 14:56:14 +0000 | [diff] [blame] | 2235 | // ObjCSuperExpr |
| 2236 | Stmt::child_iterator ObjCSuperExpr::child_begin() { return child_iterator(); } |
| 2237 | Stmt::child_iterator ObjCSuperExpr::child_end() { return child_iterator(); } |
| 2238 | |
Steve Naroff | f242b1b | 2009-07-24 17:54:45 +0000 | [diff] [blame] | 2239 | // ObjCIsaExpr |
| 2240 | Stmt::child_iterator ObjCIsaExpr::child_begin() { return &Base; } |
| 2241 | Stmt::child_iterator ObjCIsaExpr::child_end() { return &Base+1; } |
| 2242 | |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 2243 | // PredefinedExpr |
| 2244 | Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); } |
| 2245 | Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 2246 | |
| 2247 | // IntegerLiteral |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 2248 | Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); } |
| 2249 | Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 2250 | |
| 2251 | // CharacterLiteral |
Chris Lattner | d603eaa | 2009-02-16 22:33:34 +0000 | [diff] [blame] | 2252 | Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();} |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 2253 | Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 2254 | |
| 2255 | // FloatingLiteral |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 2256 | Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); } |
| 2257 | Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 2258 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2259 | // ImaginaryLiteral |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2260 | Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; } |
| 2261 | Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; } |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2262 | |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 2263 | // StringLiteral |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 2264 | Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); } |
| 2265 | Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 2266 | |
| 2267 | // ParenExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2268 | Stmt::child_iterator ParenExpr::child_begin() { return &Val; } |
| 2269 | Stmt::child_iterator ParenExpr::child_end() { return &Val+1; } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 2270 | |
| 2271 | // UnaryOperator |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2272 | Stmt::child_iterator UnaryOperator::child_begin() { return &Val; } |
| 2273 | Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 2274 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2275 | // SizeOfAlignOfExpr |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2276 | Stmt::child_iterator SizeOfAlignOfExpr::child_begin() { |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2277 | // If this is of a type and the type is a VLA type (and not a typedef), the |
| 2278 | // size expression of the VLA needs to be treated as an executable expression. |
| 2279 | // Why isn't this weirdness documented better in StmtIterator? |
| 2280 | if (isArgumentType()) { |
| 2281 | if (VariableArrayType* T = dyn_cast<VariableArrayType>( |
| 2282 | getArgumentType().getTypePtr())) |
| 2283 | return child_iterator(T); |
| 2284 | return child_iterator(); |
| 2285 | } |
Sebastian Redl | d457589 | 2008-12-03 23:17:54 +0000 | [diff] [blame] | 2286 | return child_iterator(&Argument.Ex); |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 2287 | } |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2288 | Stmt::child_iterator SizeOfAlignOfExpr::child_end() { |
| 2289 | if (isArgumentType()) |
| 2290 | return child_iterator(); |
Sebastian Redl | d457589 | 2008-12-03 23:17:54 +0000 | [diff] [blame] | 2291 | return child_iterator(&Argument.Ex + 1); |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 2292 | } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 2293 | |
| 2294 | // ArraySubscriptExpr |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 2295 | Stmt::child_iterator ArraySubscriptExpr::child_begin() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2296 | return &SubExprs[0]; |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 2297 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 2298 | Stmt::child_iterator ArraySubscriptExpr::child_end() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2299 | return &SubExprs[0]+END_EXPR; |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 2300 | } |
| 2301 | |
| 2302 | // CallExpr |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 2303 | Stmt::child_iterator CallExpr::child_begin() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2304 | return &SubExprs[0]; |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 2305 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 2306 | Stmt::child_iterator CallExpr::child_end() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2307 | return &SubExprs[0]+NumArgs+ARGS_START; |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 2308 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 2309 | |
| 2310 | // MemberExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2311 | Stmt::child_iterator MemberExpr::child_begin() { return &Base; } |
| 2312 | Stmt::child_iterator MemberExpr::child_end() { return &Base+1; } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 2313 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2314 | // ExtVectorElementExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2315 | Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; } |
| 2316 | Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 2317 | |
| 2318 | // CompoundLiteralExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2319 | Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; } |
| 2320 | Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 2321 | |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 2322 | // CastExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2323 | Stmt::child_iterator CastExpr::child_begin() { return &Op; } |
| 2324 | Stmt::child_iterator CastExpr::child_end() { return &Op+1; } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 2325 | |
| 2326 | // BinaryOperator |
| 2327 | Stmt::child_iterator BinaryOperator::child_begin() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2328 | return &SubExprs[0]; |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 2329 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 2330 | Stmt::child_iterator BinaryOperator::child_end() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2331 | return &SubExprs[0]+END_EXPR; |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 2332 | } |
| 2333 | |
| 2334 | // ConditionalOperator |
| 2335 | Stmt::child_iterator ConditionalOperator::child_begin() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2336 | return &SubExprs[0]; |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 2337 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 2338 | Stmt::child_iterator ConditionalOperator::child_end() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2339 | return &SubExprs[0]+END_EXPR; |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 2340 | } |
| 2341 | |
| 2342 | // AddrLabelExpr |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 2343 | Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); } |
| 2344 | Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 2345 | |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 2346 | // StmtExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2347 | Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; } |
| 2348 | Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 2349 | |
| 2350 | // TypesCompatibleExpr |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 2351 | Stmt::child_iterator TypesCompatibleExpr::child_begin() { |
| 2352 | return child_iterator(); |
| 2353 | } |
| 2354 | |
| 2355 | Stmt::child_iterator TypesCompatibleExpr::child_end() { |
| 2356 | return child_iterator(); |
| 2357 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 2358 | |
| 2359 | // ChooseExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2360 | Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; } |
| 2361 | Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 2362 | |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 2363 | // GNUNullExpr |
| 2364 | Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); } |
| 2365 | Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); } |
| 2366 | |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 2367 | // ShuffleVectorExpr |
| 2368 | Stmt::child_iterator ShuffleVectorExpr::child_begin() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2369 | return &SubExprs[0]; |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 2370 | } |
| 2371 | Stmt::child_iterator ShuffleVectorExpr::child_end() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2372 | return &SubExprs[0]+NumExprs; |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 2373 | } |
| 2374 | |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 2375 | // VAArgExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2376 | Stmt::child_iterator VAArgExpr::child_begin() { return &Val; } |
| 2377 | Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; } |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 2378 | |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 2379 | // InitListExpr |
| 2380 | Stmt::child_iterator InitListExpr::child_begin() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2381 | return InitExprs.size() ? &InitExprs[0] : 0; |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 2382 | } |
| 2383 | Stmt::child_iterator InitListExpr::child_end() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2384 | return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0; |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 2385 | } |
| 2386 | |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 2387 | // DesignatedInitExpr |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2388 | Stmt::child_iterator DesignatedInitExpr::child_begin() { |
| 2389 | char* Ptr = static_cast<char*>(static_cast<void *>(this)); |
| 2390 | Ptr += sizeof(DesignatedInitExpr); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2391 | return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr)); |
| 2392 | } |
| 2393 | Stmt::child_iterator DesignatedInitExpr::child_end() { |
| 2394 | return child_iterator(&*child_begin() + NumSubExprs); |
| 2395 | } |
| 2396 | |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 2397 | // ImplicitValueInitExpr |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2398 | Stmt::child_iterator ImplicitValueInitExpr::child_begin() { |
| 2399 | return child_iterator(); |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 2400 | } |
| 2401 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2402 | Stmt::child_iterator ImplicitValueInitExpr::child_end() { |
| 2403 | return child_iterator(); |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 2404 | } |
| 2405 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 2406 | // ParenListExpr |
| 2407 | Stmt::child_iterator ParenListExpr::child_begin() { |
| 2408 | return &Exprs[0]; |
| 2409 | } |
| 2410 | Stmt::child_iterator ParenListExpr::child_end() { |
| 2411 | return &Exprs[0]+NumExprs; |
| 2412 | } |
| 2413 | |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 2414 | // ObjCStringLiteral |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2415 | Stmt::child_iterator ObjCStringLiteral::child_begin() { |
Chris Lattner | c6c16af | 2009-02-18 06:53:08 +0000 | [diff] [blame] | 2416 | return &String; |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 2417 | } |
| 2418 | Stmt::child_iterator ObjCStringLiteral::child_end() { |
Chris Lattner | c6c16af | 2009-02-18 06:53:08 +0000 | [diff] [blame] | 2419 | return &String+1; |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 2420 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 2421 | |
| 2422 | // ObjCEncodeExpr |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 2423 | Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); } |
| 2424 | Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 2425 | |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 2426 | // ObjCSelectorExpr |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2427 | Stmt::child_iterator ObjCSelectorExpr::child_begin() { |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 2428 | return child_iterator(); |
| 2429 | } |
| 2430 | Stmt::child_iterator ObjCSelectorExpr::child_end() { |
| 2431 | return child_iterator(); |
| 2432 | } |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 2433 | |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 2434 | // ObjCProtocolExpr |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 2435 | Stmt::child_iterator ObjCProtocolExpr::child_begin() { |
| 2436 | return child_iterator(); |
| 2437 | } |
| 2438 | Stmt::child_iterator ObjCProtocolExpr::child_end() { |
| 2439 | return child_iterator(); |
| 2440 | } |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 2441 | |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 2442 | // ObjCMessageExpr |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2443 | Stmt::child_iterator ObjCMessageExpr::child_begin() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2444 | return getReceiver() ? &SubExprs[0] : &SubExprs[0] + ARGS_START; |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 2445 | } |
| 2446 | Stmt::child_iterator ObjCMessageExpr::child_end() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2447 | return &SubExprs[0]+ARGS_START+getNumArgs(); |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 2448 | } |
| 2449 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 2450 | // Blocks |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 2451 | Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); } |
| 2452 | Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); } |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 2453 | |
Ted Kremenek | 9da13f9 | 2008-09-26 23:24:14 +0000 | [diff] [blame] | 2454 | Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();} |
| 2455 | Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); } |