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