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