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