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