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