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