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