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