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