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