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