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