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