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" |
Douglas Gregor | 25d0a0f | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 21 | #include "clang/AST/EvaluatedExprVisitor.h" |
Anders Carlsson | 19cc4ab | 2009-07-18 19:43:29 +0000 | [diff] [blame] | 22 | #include "clang/AST/RecordLayout.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 23 | #include "clang/AST/StmtVisitor.h" |
Chris Lattner | 08f92e3 | 2010-11-17 07:37:15 +0000 | [diff] [blame] | 24 | #include "clang/Lex/LiteralSupport.h" |
| 25 | #include "clang/Lex/Lexer.h" |
Richard Smith | 7a614d8 | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 26 | #include "clang/Sema/SemaDiagnostic.h" |
Chris Lattner | 1b63e4f | 2009-06-14 01:54:56 +0000 | [diff] [blame] | 27 | #include "clang/Basic/Builtins.h" |
Chris Lattner | 08f92e3 | 2010-11-17 07:37:15 +0000 | [diff] [blame] | 28 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | da5a6b6 | 2007-11-27 18:22:04 +0000 | [diff] [blame] | 29 | #include "clang/Basic/TargetInfo.h" |
Douglas Gregor | cf3293e | 2009-11-01 20:32:48 +0000 | [diff] [blame] | 30 | #include "llvm/Support/ErrorHandling.h" |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 31 | #include "llvm/Support/raw_ostream.h" |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 32 | #include <algorithm> |
Eli Friedman | 64f45a2 | 2011-11-01 02:23:42 +0000 | [diff] [blame] | 33 | #include <cstring> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 34 | using namespace clang; |
| 35 | |
Chris Lattner | 2b334bb | 2010-04-16 23:34:13 +0000 | [diff] [blame] | 36 | /// isKnownToHaveBooleanValue - Return true if this is an integer expression |
| 37 | /// that is known to return 0 or 1. This happens for _Bool/bool expressions |
| 38 | /// but also int expressions which are produced by things like comparisons in |
| 39 | /// C. |
| 40 | bool Expr::isKnownToHaveBooleanValue() const { |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 41 | const Expr *E = IgnoreParens(); |
| 42 | |
Chris Lattner | 2b334bb | 2010-04-16 23:34:13 +0000 | [diff] [blame] | 43 | // If this value has _Bool type, it is obvious 0/1. |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 44 | if (E->getType()->isBooleanType()) return true; |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 45 | // 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] | 46 | if (!E->getType()->isIntegralOrEnumerationType()) return false; |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 47 | |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 48 | if (const UnaryOperator *UO = dyn_cast<UnaryOperator>(E)) { |
Chris Lattner | 2b334bb | 2010-04-16 23:34:13 +0000 | [diff] [blame] | 49 | switch (UO->getOpcode()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 50 | case UO_Plus: |
Chris Lattner | 2b334bb | 2010-04-16 23:34:13 +0000 | [diff] [blame] | 51 | return UO->getSubExpr()->isKnownToHaveBooleanValue(); |
| 52 | default: |
| 53 | return false; |
| 54 | } |
| 55 | } |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 56 | |
John McCall | 6907fbe | 2010-06-12 01:56:02 +0000 | [diff] [blame] | 57 | // Only look through implicit casts. If the user writes |
| 58 | // '(int) (a && b)' treat it as an arbitrary int. |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 59 | if (const ImplicitCastExpr *CE = dyn_cast<ImplicitCastExpr>(E)) |
Chris Lattner | 2b334bb | 2010-04-16 23:34:13 +0000 | [diff] [blame] | 60 | return CE->getSubExpr()->isKnownToHaveBooleanValue(); |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 61 | |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 62 | if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
Chris Lattner | 2b334bb | 2010-04-16 23:34:13 +0000 | [diff] [blame] | 63 | switch (BO->getOpcode()) { |
| 64 | default: return false; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 65 | case BO_LT: // Relational operators. |
| 66 | case BO_GT: |
| 67 | case BO_LE: |
| 68 | case BO_GE: |
| 69 | case BO_EQ: // Equality operators. |
| 70 | case BO_NE: |
| 71 | case BO_LAnd: // AND operator. |
| 72 | case BO_LOr: // Logical OR operator. |
Chris Lattner | 2b334bb | 2010-04-16 23:34:13 +0000 | [diff] [blame] | 73 | return true; |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 74 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 75 | case BO_And: // Bitwise AND operator. |
| 76 | case BO_Xor: // Bitwise XOR operator. |
| 77 | case BO_Or: // Bitwise OR operator. |
Chris Lattner | 2b334bb | 2010-04-16 23:34:13 +0000 | [diff] [blame] | 78 | // Handle things like (x==2)|(y==12). |
| 79 | return BO->getLHS()->isKnownToHaveBooleanValue() && |
| 80 | BO->getRHS()->isKnownToHaveBooleanValue(); |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 81 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 82 | case BO_Comma: |
| 83 | case BO_Assign: |
Chris Lattner | 2b334bb | 2010-04-16 23:34:13 +0000 | [diff] [blame] | 84 | return BO->getRHS()->isKnownToHaveBooleanValue(); |
| 85 | } |
| 86 | } |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 87 | |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 88 | if (const ConditionalOperator *CO = dyn_cast<ConditionalOperator>(E)) |
Chris Lattner | 2b334bb | 2010-04-16 23:34:13 +0000 | [diff] [blame] | 89 | return CO->getTrueExpr()->isKnownToHaveBooleanValue() && |
| 90 | CO->getFalseExpr()->isKnownToHaveBooleanValue(); |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 91 | |
Chris Lattner | 2b334bb | 2010-04-16 23:34:13 +0000 | [diff] [blame] | 92 | return false; |
| 93 | } |
| 94 | |
John McCall | 63c00d7 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 95 | // Amusing macro metaprogramming hack: check whether a class provides |
| 96 | // a more specific implementation of getExprLoc(). |
Daniel Dunbar | 90e25a8 | 2012-03-09 15:39:19 +0000 | [diff] [blame] | 97 | // |
| 98 | // See also Stmt.cpp:{getLocStart(),getLocEnd()}. |
John McCall | 63c00d7 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 99 | namespace { |
| 100 | /// This implementation is used when a class provides a custom |
| 101 | /// implementation of getExprLoc. |
| 102 | template <class E, class T> |
| 103 | SourceLocation getExprLocImpl(const Expr *expr, |
| 104 | SourceLocation (T::*v)() const) { |
| 105 | return static_cast<const E*>(expr)->getExprLoc(); |
| 106 | } |
| 107 | |
| 108 | /// This implementation is used when a class doesn't provide |
| 109 | /// a custom implementation of getExprLoc. Overload resolution |
| 110 | /// should pick it over the implementation above because it's |
| 111 | /// more specialized according to function template partial ordering. |
| 112 | template <class E> |
| 113 | SourceLocation getExprLocImpl(const Expr *expr, |
| 114 | SourceLocation (Expr::*v)() const) { |
Daniel Dunbar | 90e25a8 | 2012-03-09 15:39:19 +0000 | [diff] [blame] | 115 | return static_cast<const E*>(expr)->getLocStart(); |
John McCall | 63c00d7 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 116 | } |
| 117 | } |
| 118 | |
| 119 | SourceLocation Expr::getExprLoc() const { |
| 120 | switch (getStmtClass()) { |
| 121 | case Stmt::NoStmtClass: llvm_unreachable("statement without class"); |
| 122 | #define ABSTRACT_STMT(type) |
| 123 | #define STMT(type, base) \ |
| 124 | case Stmt::type##Class: llvm_unreachable(#type " is not an Expr"); break; |
| 125 | #define EXPR(type, base) \ |
| 126 | case Stmt::type##Class: return getExprLocImpl<type>(this, &type::getExprLoc); |
| 127 | #include "clang/AST/StmtNodes.inc" |
| 128 | } |
| 129 | llvm_unreachable("unknown statement kind"); |
John McCall | 63c00d7 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 132 | //===----------------------------------------------------------------------===// |
| 133 | // Primary Expressions. |
| 134 | //===----------------------------------------------------------------------===// |
| 135 | |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 136 | /// \brief Compute the type-, value-, and instantiation-dependence of a |
| 137 | /// declaration reference |
Douglas Gregor | d967e31 | 2011-01-19 21:52:31 +0000 | [diff] [blame] | 138 | /// based on the declaration being referenced. |
Daniel Dunbar | 3d13c5a | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 139 | static void computeDeclRefDependence(ASTContext &Ctx, NamedDecl *D, QualType T, |
Douglas Gregor | d967e31 | 2011-01-19 21:52:31 +0000 | [diff] [blame] | 140 | bool &TypeDependent, |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 141 | bool &ValueDependent, |
| 142 | bool &InstantiationDependent) { |
Douglas Gregor | d967e31 | 2011-01-19 21:52:31 +0000 | [diff] [blame] | 143 | TypeDependent = false; |
| 144 | ValueDependent = false; |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 145 | InstantiationDependent = false; |
Douglas Gregor | 0da76df | 2009-11-23 11:41:28 +0000 | [diff] [blame] | 146 | |
| 147 | // (TD) C++ [temp.dep.expr]p3: |
| 148 | // An id-expression is type-dependent if it contains: |
| 149 | // |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 150 | // and |
Douglas Gregor | 0da76df | 2009-11-23 11:41:28 +0000 | [diff] [blame] | 151 | // |
| 152 | // (VD) C++ [temp.dep.constexpr]p2: |
| 153 | // An identifier is value-dependent if it is: |
Douglas Gregor | d967e31 | 2011-01-19 21:52:31 +0000 | [diff] [blame] | 154 | |
Douglas Gregor | 0da76df | 2009-11-23 11:41:28 +0000 | [diff] [blame] | 155 | // (TD) - an identifier that was declared with dependent type |
| 156 | // (VD) - a name declared with a dependent type, |
Douglas Gregor | d967e31 | 2011-01-19 21:52:31 +0000 | [diff] [blame] | 157 | if (T->isDependentType()) { |
| 158 | TypeDependent = true; |
| 159 | ValueDependent = true; |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 160 | InstantiationDependent = true; |
Douglas Gregor | d967e31 | 2011-01-19 21:52:31 +0000 | [diff] [blame] | 161 | return; |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 162 | } else if (T->isInstantiationDependentType()) { |
| 163 | InstantiationDependent = true; |
Douglas Gregor | 0da76df | 2009-11-23 11:41:28 +0000 | [diff] [blame] | 164 | } |
Douglas Gregor | d967e31 | 2011-01-19 21:52:31 +0000 | [diff] [blame] | 165 | |
Douglas Gregor | 0da76df | 2009-11-23 11:41:28 +0000 | [diff] [blame] | 166 | // (TD) - a conversion-function-id that specifies a dependent type |
Douglas Gregor | d967e31 | 2011-01-19 21:52:31 +0000 | [diff] [blame] | 167 | if (D->getDeclName().getNameKind() |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 168 | == DeclarationName::CXXConversionFunctionName) { |
| 169 | QualType T = D->getDeclName().getCXXNameType(); |
| 170 | if (T->isDependentType()) { |
| 171 | TypeDependent = true; |
| 172 | ValueDependent = true; |
| 173 | InstantiationDependent = true; |
| 174 | return; |
| 175 | } |
| 176 | |
| 177 | if (T->isInstantiationDependentType()) |
| 178 | InstantiationDependent = true; |
Douglas Gregor | 0da76df | 2009-11-23 11:41:28 +0000 | [diff] [blame] | 179 | } |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 180 | |
Douglas Gregor | 0da76df | 2009-11-23 11:41:28 +0000 | [diff] [blame] | 181 | // (VD) - the name of a non-type template parameter, |
Douglas Gregor | d967e31 | 2011-01-19 21:52:31 +0000 | [diff] [blame] | 182 | if (isa<NonTypeTemplateParmDecl>(D)) { |
| 183 | ValueDependent = true; |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 184 | InstantiationDependent = true; |
Douglas Gregor | d967e31 | 2011-01-19 21:52:31 +0000 | [diff] [blame] | 185 | return; |
| 186 | } |
| 187 | |
Douglas Gregor | 0da76df | 2009-11-23 11:41:28 +0000 | [diff] [blame] | 188 | // (VD) - a constant with integral or enumeration type and is |
| 189 | // initialized with an expression that is value-dependent. |
Richard Smith | db1822c | 2011-11-08 01:31:09 +0000 | [diff] [blame] | 190 | // (VD) - a constant with literal type and is initialized with an |
| 191 | // expression that is value-dependent [C++11]. |
| 192 | // (VD) - FIXME: Missing from the standard: |
| 193 | // - an entity with reference type and is initialized with an |
| 194 | // expression that is value-dependent [C++11] |
Douglas Gregor | d967e31 | 2011-01-19 21:52:31 +0000 | [diff] [blame] | 195 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 196 | if ((Ctx.getLangOpts().CPlusPlus0x ? |
Richard Smith | db1822c | 2011-11-08 01:31:09 +0000 | [diff] [blame] | 197 | Var->getType()->isLiteralType() : |
| 198 | Var->getType()->isIntegralOrEnumerationType()) && |
| 199 | (Var->getType().getCVRQualifiers() == Qualifiers::Const || |
| 200 | Var->getType()->isReferenceType())) { |
Sebastian Redl | 31310a2 | 2010-02-01 20:16:42 +0000 | [diff] [blame] | 201 | if (const Expr *Init = Var->getAnyInitializer()) |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 202 | if (Init->isValueDependent()) { |
Douglas Gregor | d967e31 | 2011-01-19 21:52:31 +0000 | [diff] [blame] | 203 | ValueDependent = true; |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 204 | InstantiationDependent = true; |
| 205 | } |
Richard Smith | db1822c | 2011-11-08 01:31:09 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Douglas Gregor | bb6e73f | 2010-05-11 08:41:30 +0000 | [diff] [blame] | 208 | // (VD) - FIXME: Missing from the standard: |
| 209 | // - a member function or a static data member of the current |
| 210 | // instantiation |
Richard Smith | db1822c | 2011-11-08 01:31:09 +0000 | [diff] [blame] | 211 | if (Var->isStaticDataMember() && |
| 212 | Var->getDeclContext()->isDependentContext()) { |
Douglas Gregor | d967e31 | 2011-01-19 21:52:31 +0000 | [diff] [blame] | 213 | ValueDependent = true; |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 214 | InstantiationDependent = true; |
| 215 | } |
Douglas Gregor | d967e31 | 2011-01-19 21:52:31 +0000 | [diff] [blame] | 216 | |
| 217 | return; |
| 218 | } |
| 219 | |
Douglas Gregor | bb6e73f | 2010-05-11 08:41:30 +0000 | [diff] [blame] | 220 | // (VD) - FIXME: Missing from the standard: |
| 221 | // - a member function or a static data member of the current |
| 222 | // instantiation |
Douglas Gregor | d967e31 | 2011-01-19 21:52:31 +0000 | [diff] [blame] | 223 | if (isa<CXXMethodDecl>(D) && D->getDeclContext()->isDependentContext()) { |
| 224 | ValueDependent = true; |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 225 | InstantiationDependent = true; |
Richard Smith | db1822c | 2011-11-08 01:31:09 +0000 | [diff] [blame] | 226 | } |
Douglas Gregor | d967e31 | 2011-01-19 21:52:31 +0000 | [diff] [blame] | 227 | } |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 228 | |
Daniel Dunbar | 3d13c5a | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 229 | void DeclRefExpr::computeDependence(ASTContext &Ctx) { |
Douglas Gregor | d967e31 | 2011-01-19 21:52:31 +0000 | [diff] [blame] | 230 | bool TypeDependent = false; |
| 231 | bool ValueDependent = false; |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 232 | bool InstantiationDependent = false; |
Daniel Dunbar | 3d13c5a | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 233 | computeDeclRefDependence(Ctx, getDecl(), getType(), TypeDependent, |
| 234 | ValueDependent, InstantiationDependent); |
Douglas Gregor | d967e31 | 2011-01-19 21:52:31 +0000 | [diff] [blame] | 235 | |
| 236 | // (TD) C++ [temp.dep.expr]p3: |
| 237 | // An id-expression is type-dependent if it contains: |
| 238 | // |
| 239 | // and |
| 240 | // |
| 241 | // (VD) C++ [temp.dep.constexpr]p2: |
| 242 | // An identifier is value-dependent if it is: |
| 243 | if (!TypeDependent && !ValueDependent && |
| 244 | hasExplicitTemplateArgs() && |
| 245 | TemplateSpecializationType::anyDependentTemplateArguments( |
| 246 | getTemplateArgs(), |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 247 | getNumTemplateArgs(), |
| 248 | InstantiationDependent)) { |
Douglas Gregor | d967e31 | 2011-01-19 21:52:31 +0000 | [diff] [blame] | 249 | TypeDependent = true; |
| 250 | ValueDependent = true; |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 251 | InstantiationDependent = true; |
Douglas Gregor | d967e31 | 2011-01-19 21:52:31 +0000 | [diff] [blame] | 252 | } |
| 253 | |
| 254 | ExprBits.TypeDependent = TypeDependent; |
| 255 | ExprBits.ValueDependent = ValueDependent; |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 256 | ExprBits.InstantiationDependent = InstantiationDependent; |
Douglas Gregor | d967e31 | 2011-01-19 21:52:31 +0000 | [diff] [blame] | 257 | |
Douglas Gregor | 10738d3 | 2010-12-23 23:51:58 +0000 | [diff] [blame] | 258 | // Is the declaration a parameter pack? |
Douglas Gregor | d967e31 | 2011-01-19 21:52:31 +0000 | [diff] [blame] | 259 | if (getDecl()->isParameterPack()) |
Douglas Gregor | 1fe85ea | 2011-01-05 21:11:38 +0000 | [diff] [blame] | 260 | ExprBits.ContainsUnexpandedParameterPack = true; |
Douglas Gregor | 0da76df | 2009-11-23 11:41:28 +0000 | [diff] [blame] | 261 | } |
| 262 | |
Daniel Dunbar | 3d13c5a | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 263 | DeclRefExpr::DeclRefExpr(ASTContext &Ctx, |
| 264 | NestedNameSpecifierLoc QualifierLoc, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 265 | SourceLocation TemplateKWLoc, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 266 | ValueDecl *D, bool RefersToEnclosingLocal, |
| 267 | const DeclarationNameInfo &NameInfo, |
Chandler Carruth | 3aa8140 | 2011-05-01 23:48:14 +0000 | [diff] [blame] | 268 | NamedDecl *FoundD, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 269 | const TemplateArgumentListInfo *TemplateArgs, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 270 | QualType T, ExprValueKind VK) |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 271 | : Expr(DeclRefExprClass, T, VK, OK_Ordinary, false, false, false, false), |
Chandler Carruth | cb66cff | 2011-05-01 21:29:53 +0000 | [diff] [blame] | 272 | D(D), Loc(NameInfo.getLoc()), DNLoc(NameInfo.getInfo()) { |
| 273 | DeclRefExprBits.HasQualifier = QualifierLoc ? 1 : 0; |
Chandler Carruth | 7e740bd | 2011-05-01 21:55:21 +0000 | [diff] [blame] | 274 | if (QualifierLoc) |
Chandler Carruth | 6857c3e | 2011-05-01 22:14:37 +0000 | [diff] [blame] | 275 | getInternalQualifierLoc() = QualifierLoc; |
Chandler Carruth | 3aa8140 | 2011-05-01 23:48:14 +0000 | [diff] [blame] | 276 | DeclRefExprBits.HasFoundDecl = FoundD ? 1 : 0; |
| 277 | if (FoundD) |
| 278 | getInternalFoundDecl() = FoundD; |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 279 | DeclRefExprBits.HasTemplateKWAndArgsInfo |
| 280 | = (TemplateArgs || TemplateKWLoc.isValid()) ? 1 : 0; |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 281 | DeclRefExprBits.RefersToEnclosingLocal = RefersToEnclosingLocal; |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 282 | if (TemplateArgs) { |
| 283 | bool Dependent = false; |
| 284 | bool InstantiationDependent = false; |
| 285 | bool ContainsUnexpandedParameterPack = false; |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 286 | getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *TemplateArgs, |
| 287 | Dependent, |
| 288 | InstantiationDependent, |
| 289 | ContainsUnexpandedParameterPack); |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 290 | if (InstantiationDependent) |
| 291 | setInstantiationDependent(true); |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 292 | } else if (TemplateKWLoc.isValid()) { |
| 293 | getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc); |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 294 | } |
Benjamin Kramer | b8da98a | 2011-10-10 12:54:05 +0000 | [diff] [blame] | 295 | DeclRefExprBits.HadMultipleCandidates = 0; |
| 296 | |
Daniel Dunbar | 3d13c5a | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 297 | computeDependence(Ctx); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 298 | } |
| 299 | |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 300 | DeclRefExpr *DeclRefExpr::Create(ASTContext &Context, |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 301 | NestedNameSpecifierLoc QualifierLoc, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 302 | SourceLocation TemplateKWLoc, |
John McCall | dbd872f | 2009-12-08 09:08:17 +0000 | [diff] [blame] | 303 | ValueDecl *D, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 304 | bool RefersToEnclosingLocal, |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 305 | SourceLocation NameLoc, |
Douglas Gregor | 0da76df | 2009-11-23 11:41:28 +0000 | [diff] [blame] | 306 | QualType T, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 307 | ExprValueKind VK, |
Chandler Carruth | 3aa8140 | 2011-05-01 23:48:14 +0000 | [diff] [blame] | 308 | NamedDecl *FoundD, |
Douglas Gregor | 0da76df | 2009-11-23 11:41:28 +0000 | [diff] [blame] | 309 | const TemplateArgumentListInfo *TemplateArgs) { |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 310 | return Create(Context, QualifierLoc, TemplateKWLoc, D, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 311 | RefersToEnclosingLocal, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 312 | DeclarationNameInfo(D->getDeclName(), NameLoc), |
Chandler Carruth | 3aa8140 | 2011-05-01 23:48:14 +0000 | [diff] [blame] | 313 | T, VK, FoundD, TemplateArgs); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | DeclRefExpr *DeclRefExpr::Create(ASTContext &Context, |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 317 | NestedNameSpecifierLoc QualifierLoc, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 318 | SourceLocation TemplateKWLoc, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 319 | ValueDecl *D, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 320 | bool RefersToEnclosingLocal, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 321 | const DeclarationNameInfo &NameInfo, |
| 322 | QualType T, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 323 | ExprValueKind VK, |
Chandler Carruth | 3aa8140 | 2011-05-01 23:48:14 +0000 | [diff] [blame] | 324 | NamedDecl *FoundD, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 325 | const TemplateArgumentListInfo *TemplateArgs) { |
Chandler Carruth | 3aa8140 | 2011-05-01 23:48:14 +0000 | [diff] [blame] | 326 | // Filter out cases where the found Decl is the same as the value refenenced. |
| 327 | if (D == FoundD) |
| 328 | FoundD = 0; |
| 329 | |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 330 | std::size_t Size = sizeof(DeclRefExpr); |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 331 | if (QualifierLoc != 0) |
Chandler Carruth | 6857c3e | 2011-05-01 22:14:37 +0000 | [diff] [blame] | 332 | Size += sizeof(NestedNameSpecifierLoc); |
Chandler Carruth | 3aa8140 | 2011-05-01 23:48:14 +0000 | [diff] [blame] | 333 | if (FoundD) |
| 334 | Size += sizeof(NamedDecl *); |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 335 | if (TemplateArgs) |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 336 | Size += ASTTemplateKWAndArgsInfo::sizeFor(TemplateArgs->size()); |
| 337 | else if (TemplateKWLoc.isValid()) |
| 338 | Size += ASTTemplateKWAndArgsInfo::sizeFor(0); |
Chandler Carruth | 3aa8140 | 2011-05-01 23:48:14 +0000 | [diff] [blame] | 339 | |
Chris Lattner | 3248854 | 2010-10-30 05:14:06 +0000 | [diff] [blame] | 340 | void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>()); |
Daniel Dunbar | 3d13c5a | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 341 | return new (Mem) DeclRefExpr(Context, QualifierLoc, TemplateKWLoc, D, |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 342 | RefersToEnclosingLocal, |
Daniel Dunbar | 3d13c5a | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 343 | NameInfo, FoundD, TemplateArgs, T, VK); |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Chandler Carruth | 3aa8140 | 2011-05-01 23:48:14 +0000 | [diff] [blame] | 346 | DeclRefExpr *DeclRefExpr::CreateEmpty(ASTContext &Context, |
Douglas Gregor | def0354 | 2011-02-04 12:01:24 +0000 | [diff] [blame] | 347 | bool HasQualifier, |
Chandler Carruth | 3aa8140 | 2011-05-01 23:48:14 +0000 | [diff] [blame] | 348 | bool HasFoundDecl, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 349 | bool HasTemplateKWAndArgsInfo, |
Argyrios Kyrtzidis | 663e380 | 2010-07-08 13:09:47 +0000 | [diff] [blame] | 350 | unsigned NumTemplateArgs) { |
| 351 | std::size_t Size = sizeof(DeclRefExpr); |
| 352 | if (HasQualifier) |
Chandler Carruth | 6857c3e | 2011-05-01 22:14:37 +0000 | [diff] [blame] | 353 | Size += sizeof(NestedNameSpecifierLoc); |
Chandler Carruth | 3aa8140 | 2011-05-01 23:48:14 +0000 | [diff] [blame] | 354 | if (HasFoundDecl) |
| 355 | Size += sizeof(NamedDecl *); |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 356 | if (HasTemplateKWAndArgsInfo) |
| 357 | Size += ASTTemplateKWAndArgsInfo::sizeFor(NumTemplateArgs); |
Chandler Carruth | 3aa8140 | 2011-05-01 23:48:14 +0000 | [diff] [blame] | 358 | |
Chris Lattner | 3248854 | 2010-10-30 05:14:06 +0000 | [diff] [blame] | 359 | void *Mem = Context.Allocate(Size, llvm::alignOf<DeclRefExpr>()); |
Argyrios Kyrtzidis | 663e380 | 2010-07-08 13:09:47 +0000 | [diff] [blame] | 360 | return new (Mem) DeclRefExpr(EmptyShell()); |
| 361 | } |
| 362 | |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 363 | SourceRange DeclRefExpr::getSourceRange() const { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 364 | SourceRange R = getNameInfo().getSourceRange(); |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 365 | if (hasQualifier()) |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 366 | R.setBegin(getQualifierLoc().getBeginLoc()); |
John McCall | 096832c | 2010-08-19 23:49:38 +0000 | [diff] [blame] | 367 | if (hasExplicitTemplateArgs()) |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 368 | R.setEnd(getRAngleLoc()); |
| 369 | return R; |
| 370 | } |
Daniel Dunbar | 396ec67 | 2012-03-09 15:39:15 +0000 | [diff] [blame] | 371 | SourceLocation DeclRefExpr::getLocStart() const { |
| 372 | if (hasQualifier()) |
| 373 | return getQualifierLoc().getBeginLoc(); |
| 374 | return getNameInfo().getLocStart(); |
| 375 | } |
| 376 | SourceLocation DeclRefExpr::getLocEnd() const { |
| 377 | if (hasExplicitTemplateArgs()) |
| 378 | return getRAngleLoc(); |
| 379 | return getNameInfo().getLocEnd(); |
| 380 | } |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 381 | |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 382 | // FIXME: Maybe this should use DeclPrinter with a special "print predefined |
| 383 | // expr" policy instead. |
Anders Carlsson | 848fa64 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 384 | std::string PredefinedExpr::ComputeName(IdentType IT, const Decl *CurrentDecl) { |
| 385 | ASTContext &Context = CurrentDecl->getASTContext(); |
| 386 | |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 387 | if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(CurrentDecl)) { |
Anders Carlsson | 848fa64 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 388 | if (IT != PrettyFunction && IT != PrettyFunctionNoVirtual) |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 389 | return FD->getNameAsString(); |
| 390 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 391 | SmallString<256> Name; |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 392 | llvm::raw_svector_ostream Out(Name); |
| 393 | |
| 394 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
Anders Carlsson | 848fa64 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 395 | if (MD->isVirtual() && IT != PrettyFunctionNoVirtual) |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 396 | Out << "virtual "; |
Sam Weinig | 4eadcc5 | 2009-12-27 01:38:20 +0000 | [diff] [blame] | 397 | if (MD->isStatic()) |
| 398 | Out << "static "; |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 399 | } |
| 400 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 401 | PrintingPolicy Policy(Context.getLangOpts()); |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 402 | std::string Proto = FD->getQualifiedNameAsString(Policy); |
Douglas Gregor | abf65ce | 2012-04-10 20:14:15 +0000 | [diff] [blame] | 403 | llvm::raw_string_ostream POut(Proto); |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 404 | |
Douglas Gregor | abf65ce | 2012-04-10 20:14:15 +0000 | [diff] [blame] | 405 | const FunctionDecl *Decl = FD; |
| 406 | if (const FunctionDecl* Pattern = FD->getTemplateInstantiationPattern()) |
| 407 | Decl = Pattern; |
| 408 | const FunctionType *AFT = Decl->getType()->getAs<FunctionType>(); |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 409 | const FunctionProtoType *FT = 0; |
| 410 | if (FD->hasWrittenPrototype()) |
| 411 | FT = dyn_cast<FunctionProtoType>(AFT); |
| 412 | |
Douglas Gregor | abf65ce | 2012-04-10 20:14:15 +0000 | [diff] [blame] | 413 | POut << "("; |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 414 | if (FT) { |
Douglas Gregor | abf65ce | 2012-04-10 20:14:15 +0000 | [diff] [blame] | 415 | for (unsigned i = 0, e = Decl->getNumParams(); i != e; ++i) { |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 416 | if (i) POut << ", "; |
Argyrios Kyrtzidis | 7ad5c99 | 2012-05-05 04:20:37 +0000 | [diff] [blame] | 417 | POut << Decl->getParamDecl(i)->getType().stream(Policy); |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | if (FT->isVariadic()) { |
| 421 | if (FD->getNumParams()) POut << ", "; |
| 422 | POut << "..."; |
| 423 | } |
| 424 | } |
Douglas Gregor | abf65ce | 2012-04-10 20:14:15 +0000 | [diff] [blame] | 425 | POut << ")"; |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 426 | |
Sam Weinig | 4eadcc5 | 2009-12-27 01:38:20 +0000 | [diff] [blame] | 427 | if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) { |
| 428 | Qualifiers ThisQuals = Qualifiers::fromCVRMask(MD->getTypeQualifiers()); |
| 429 | if (ThisQuals.hasConst()) |
Douglas Gregor | abf65ce | 2012-04-10 20:14:15 +0000 | [diff] [blame] | 430 | POut << " const"; |
Sam Weinig | 4eadcc5 | 2009-12-27 01:38:20 +0000 | [diff] [blame] | 431 | if (ThisQuals.hasVolatile()) |
Douglas Gregor | abf65ce | 2012-04-10 20:14:15 +0000 | [diff] [blame] | 432 | POut << " volatile"; |
| 433 | RefQualifierKind Ref = MD->getRefQualifier(); |
| 434 | if (Ref == RQ_LValue) |
| 435 | POut << " &"; |
| 436 | else if (Ref == RQ_RValue) |
| 437 | POut << " &&"; |
Sam Weinig | 4eadcc5 | 2009-12-27 01:38:20 +0000 | [diff] [blame] | 438 | } |
| 439 | |
Douglas Gregor | abf65ce | 2012-04-10 20:14:15 +0000 | [diff] [blame] | 440 | typedef SmallVector<const ClassTemplateSpecializationDecl *, 8> SpecsTy; |
| 441 | SpecsTy Specs; |
| 442 | const DeclContext *Ctx = FD->getDeclContext(); |
| 443 | while (Ctx && isa<NamedDecl>(Ctx)) { |
| 444 | const ClassTemplateSpecializationDecl *Spec |
| 445 | = dyn_cast<ClassTemplateSpecializationDecl>(Ctx); |
| 446 | if (Spec && !Spec->isExplicitSpecialization()) |
| 447 | Specs.push_back(Spec); |
| 448 | Ctx = Ctx->getParent(); |
| 449 | } |
| 450 | |
| 451 | std::string TemplateParams; |
| 452 | llvm::raw_string_ostream TOut(TemplateParams); |
| 453 | for (SpecsTy::reverse_iterator I = Specs.rbegin(), E = Specs.rend(); |
| 454 | I != E; ++I) { |
| 455 | const TemplateParameterList *Params |
| 456 | = (*I)->getSpecializedTemplate()->getTemplateParameters(); |
| 457 | const TemplateArgumentList &Args = (*I)->getTemplateArgs(); |
| 458 | assert(Params->size() == Args.size()); |
| 459 | for (unsigned i = 0, numParams = Params->size(); i != numParams; ++i) { |
| 460 | StringRef Param = Params->getParam(i)->getName(); |
| 461 | if (Param.empty()) continue; |
| 462 | TOut << Param << " = "; |
| 463 | Args.get(i).print(Policy, TOut); |
| 464 | TOut << ", "; |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | FunctionTemplateSpecializationInfo *FSI |
| 469 | = FD->getTemplateSpecializationInfo(); |
| 470 | if (FSI && !FSI->isExplicitSpecialization()) { |
| 471 | const TemplateParameterList* Params |
| 472 | = FSI->getTemplate()->getTemplateParameters(); |
| 473 | const TemplateArgumentList* Args = FSI->TemplateArguments; |
| 474 | assert(Params->size() == Args->size()); |
| 475 | for (unsigned i = 0, e = Params->size(); i != e; ++i) { |
| 476 | StringRef Param = Params->getParam(i)->getName(); |
| 477 | if (Param.empty()) continue; |
| 478 | TOut << Param << " = "; |
| 479 | Args->get(i).print(Policy, TOut); |
| 480 | TOut << ", "; |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | TOut.flush(); |
| 485 | if (!TemplateParams.empty()) { |
| 486 | // remove the trailing comma and space |
| 487 | TemplateParams.resize(TemplateParams.size() - 2); |
| 488 | POut << " [" << TemplateParams << "]"; |
| 489 | } |
| 490 | |
| 491 | POut.flush(); |
| 492 | |
Sam Weinig | 3a1ce1e | 2009-12-06 23:55:13 +0000 | [diff] [blame] | 493 | if (!isa<CXXConstructorDecl>(FD) && !isa<CXXDestructorDecl>(FD)) |
| 494 | AFT->getResultType().getAsStringInternal(Proto, Policy); |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 495 | |
| 496 | Out << Proto; |
| 497 | |
| 498 | Out.flush(); |
| 499 | return Name.str().str(); |
| 500 | } |
| 501 | if (const ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(CurrentDecl)) { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 502 | SmallString<256> Name; |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 503 | llvm::raw_svector_ostream Out(Name); |
| 504 | Out << (MD->isInstanceMethod() ? '-' : '+'); |
| 505 | Out << '['; |
Ted Kremenek | b03d33e | 2010-03-18 21:23:08 +0000 | [diff] [blame] | 506 | |
| 507 | // For incorrect code, there might not be an ObjCInterfaceDecl. Do |
| 508 | // a null check to avoid a crash. |
| 509 | if (const ObjCInterfaceDecl *ID = MD->getClassInterface()) |
Benjamin Kramer | b8989f2 | 2011-10-14 18:45:37 +0000 | [diff] [blame] | 510 | Out << *ID; |
Ted Kremenek | b03d33e | 2010-03-18 21:23:08 +0000 | [diff] [blame] | 511 | |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 512 | if (const ObjCCategoryImplDecl *CID = |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 513 | dyn_cast<ObjCCategoryImplDecl>(MD->getDeclContext())) |
Benjamin Kramer | f978059 | 2012-02-07 11:57:45 +0000 | [diff] [blame] | 514 | Out << '(' << *CID << ')'; |
Benjamin Kramer | 900fc63 | 2010-04-17 09:33:03 +0000 | [diff] [blame] | 515 | |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 516 | Out << ' '; |
| 517 | Out << MD->getSelector().getAsString(); |
| 518 | Out << ']'; |
| 519 | |
| 520 | Out.flush(); |
| 521 | return Name.str().str(); |
| 522 | } |
| 523 | if (isa<TranslationUnitDecl>(CurrentDecl) && IT == PrettyFunction) { |
| 524 | // __PRETTY_FUNCTION__ -> "top level", the others produce an empty string. |
| 525 | return "top level"; |
| 526 | } |
| 527 | return ""; |
| 528 | } |
| 529 | |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 530 | void APNumericStorage::setIntValue(ASTContext &C, const llvm::APInt &Val) { |
| 531 | if (hasAllocation()) |
| 532 | C.Deallocate(pVal); |
| 533 | |
| 534 | BitWidth = Val.getBitWidth(); |
| 535 | unsigned NumWords = Val.getNumWords(); |
| 536 | const uint64_t* Words = Val.getRawData(); |
| 537 | if (NumWords > 1) { |
| 538 | pVal = new (C) uint64_t[NumWords]; |
| 539 | std::copy(Words, Words + NumWords, pVal); |
| 540 | } else if (NumWords == 1) |
| 541 | VAL = Words[0]; |
| 542 | else |
| 543 | VAL = 0; |
| 544 | } |
| 545 | |
| 546 | IntegerLiteral * |
| 547 | IntegerLiteral::Create(ASTContext &C, const llvm::APInt &V, |
| 548 | QualType type, SourceLocation l) { |
| 549 | return new (C) IntegerLiteral(C, V, type, l); |
| 550 | } |
| 551 | |
| 552 | IntegerLiteral * |
| 553 | IntegerLiteral::Create(ASTContext &C, EmptyShell Empty) { |
| 554 | return new (C) IntegerLiteral(Empty); |
| 555 | } |
| 556 | |
| 557 | FloatingLiteral * |
| 558 | FloatingLiteral::Create(ASTContext &C, const llvm::APFloat &V, |
| 559 | bool isexact, QualType Type, SourceLocation L) { |
| 560 | return new (C) FloatingLiteral(C, V, isexact, Type, L); |
| 561 | } |
| 562 | |
| 563 | FloatingLiteral * |
| 564 | FloatingLiteral::Create(ASTContext &C, EmptyShell Empty) { |
Akira Hatanaka | 31dfd64 | 2012-01-10 22:40:09 +0000 | [diff] [blame] | 565 | return new (C) FloatingLiteral(C, Empty); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 566 | } |
| 567 | |
Chris Lattner | da8249e | 2008-06-07 22:13:43 +0000 | [diff] [blame] | 568 | /// getValueAsApproximateDouble - This returns the value as an inaccurate |
| 569 | /// double. Note that this may cause loss of precision, but is useful for |
| 570 | /// debugging dumps, etc. |
| 571 | double FloatingLiteral::getValueAsApproximateDouble() const { |
| 572 | llvm::APFloat V = getValue(); |
Dale Johannesen | ee5a700 | 2008-10-09 23:02:32 +0000 | [diff] [blame] | 573 | bool ignored; |
| 574 | V.convert(llvm::APFloat::IEEEdouble, llvm::APFloat::rmNearestTiesToEven, |
| 575 | &ignored); |
Chris Lattner | da8249e | 2008-06-07 22:13:43 +0000 | [diff] [blame] | 576 | return V.convertToDouble(); |
| 577 | } |
| 578 | |
Nick Lewycky | 0fd7f4d | 2012-02-24 09:07:53 +0000 | [diff] [blame] | 579 | int StringLiteral::mapCharByteWidth(TargetInfo const &target,StringKind k) { |
Eli Friedman | fd81978 | 2012-02-29 20:59:56 +0000 | [diff] [blame] | 580 | int CharByteWidth = 0; |
Nick Lewycky | 0fd7f4d | 2012-02-24 09:07:53 +0000 | [diff] [blame] | 581 | switch(k) { |
Eli Friedman | 64f45a2 | 2011-11-01 02:23:42 +0000 | [diff] [blame] | 582 | case Ascii: |
| 583 | case UTF8: |
Nick Lewycky | 0fd7f4d | 2012-02-24 09:07:53 +0000 | [diff] [blame] | 584 | CharByteWidth = target.getCharWidth(); |
Eli Friedman | 64f45a2 | 2011-11-01 02:23:42 +0000 | [diff] [blame] | 585 | break; |
| 586 | case Wide: |
Nick Lewycky | 0fd7f4d | 2012-02-24 09:07:53 +0000 | [diff] [blame] | 587 | CharByteWidth = target.getWCharWidth(); |
Eli Friedman | 64f45a2 | 2011-11-01 02:23:42 +0000 | [diff] [blame] | 588 | break; |
| 589 | case UTF16: |
Nick Lewycky | 0fd7f4d | 2012-02-24 09:07:53 +0000 | [diff] [blame] | 590 | CharByteWidth = target.getChar16Width(); |
Eli Friedman | 64f45a2 | 2011-11-01 02:23:42 +0000 | [diff] [blame] | 591 | break; |
| 592 | case UTF32: |
Nick Lewycky | 0fd7f4d | 2012-02-24 09:07:53 +0000 | [diff] [blame] | 593 | CharByteWidth = target.getChar32Width(); |
Eli Friedman | fd81978 | 2012-02-29 20:59:56 +0000 | [diff] [blame] | 594 | break; |
Eli Friedman | 64f45a2 | 2011-11-01 02:23:42 +0000 | [diff] [blame] | 595 | } |
| 596 | assert((CharByteWidth & 7) == 0 && "Assumes character size is byte multiple"); |
| 597 | CharByteWidth /= 8; |
Nick Lewycky | 0fd7f4d | 2012-02-24 09:07:53 +0000 | [diff] [blame] | 598 | assert((CharByteWidth==1 || CharByteWidth==2 || CharByteWidth==4) |
Eli Friedman | 64f45a2 | 2011-11-01 02:23:42 +0000 | [diff] [blame] | 599 | && "character byte widths supported are 1, 2, and 4 only"); |
| 600 | return CharByteWidth; |
| 601 | } |
| 602 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 603 | StringLiteral *StringLiteral::Create(ASTContext &C, StringRef Str, |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 604 | StringKind Kind, bool Pascal, QualType Ty, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 605 | const SourceLocation *Loc, |
Anders Carlsson | a135fb4 | 2009-03-15 18:34:13 +0000 | [diff] [blame] | 606 | unsigned NumStrs) { |
Chris Lattner | 2085fd6 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 607 | // Allocate enough space for the StringLiteral plus an array of locations for |
| 608 | // any concatenated string tokens. |
| 609 | void *Mem = C.Allocate(sizeof(StringLiteral)+ |
| 610 | sizeof(SourceLocation)*(NumStrs-1), |
Chris Lattner | 3248854 | 2010-10-30 05:14:06 +0000 | [diff] [blame] | 611 | llvm::alignOf<StringLiteral>()); |
Chris Lattner | 2085fd6 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 612 | StringLiteral *SL = new (Mem) StringLiteral(Ty); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 613 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 614 | // OPTIMIZE: could allocate this appended to the StringLiteral. |
Eli Friedman | 64f45a2 | 2011-11-01 02:23:42 +0000 | [diff] [blame] | 615 | SL->setString(C,Str,Kind,Pascal); |
| 616 | |
Chris Lattner | 2085fd6 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 617 | SL->TokLocs[0] = Loc[0]; |
| 618 | SL->NumConcatenated = NumStrs; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 619 | |
Chris Lattner | 726e168 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 620 | if (NumStrs != 1) |
Chris Lattner | 2085fd6 | 2009-02-18 06:40:38 +0000 | [diff] [blame] | 621 | memcpy(&SL->TokLocs[1], Loc+1, sizeof(SourceLocation)*(NumStrs-1)); |
| 622 | return SL; |
Chris Lattner | 726e168 | 2009-02-18 05:49:11 +0000 | [diff] [blame] | 623 | } |
| 624 | |
Douglas Gregor | 673ecd6 | 2009-04-15 16:35:07 +0000 | [diff] [blame] | 625 | StringLiteral *StringLiteral::CreateEmpty(ASTContext &C, unsigned NumStrs) { |
| 626 | void *Mem = C.Allocate(sizeof(StringLiteral)+ |
| 627 | sizeof(SourceLocation)*(NumStrs-1), |
Chris Lattner | 3248854 | 2010-10-30 05:14:06 +0000 | [diff] [blame] | 628 | llvm::alignOf<StringLiteral>()); |
Douglas Gregor | 673ecd6 | 2009-04-15 16:35:07 +0000 | [diff] [blame] | 629 | StringLiteral *SL = new (Mem) StringLiteral(QualType()); |
Eli Friedman | 64f45a2 | 2011-11-01 02:23:42 +0000 | [diff] [blame] | 630 | SL->CharByteWidth = 0; |
| 631 | SL->Length = 0; |
Douglas Gregor | 673ecd6 | 2009-04-15 16:35:07 +0000 | [diff] [blame] | 632 | SL->NumConcatenated = NumStrs; |
| 633 | return SL; |
| 634 | } |
| 635 | |
Eli Friedman | 64f45a2 | 2011-11-01 02:23:42 +0000 | [diff] [blame] | 636 | void StringLiteral::setString(ASTContext &C, StringRef Str, |
| 637 | StringKind Kind, bool IsPascal) { |
| 638 | //FIXME: we assume that the string data comes from a target that uses the same |
| 639 | // code unit size and endianess for the type of string. |
| 640 | this->Kind = Kind; |
| 641 | this->IsPascal = IsPascal; |
| 642 | |
Nick Lewycky | 0fd7f4d | 2012-02-24 09:07:53 +0000 | [diff] [blame] | 643 | CharByteWidth = mapCharByteWidth(C.getTargetInfo(),Kind); |
Eli Friedman | 64f45a2 | 2011-11-01 02:23:42 +0000 | [diff] [blame] | 644 | assert((Str.size()%CharByteWidth == 0) |
| 645 | && "size of data must be multiple of CharByteWidth"); |
| 646 | Length = Str.size()/CharByteWidth; |
| 647 | |
| 648 | switch(CharByteWidth) { |
| 649 | case 1: { |
| 650 | char *AStrData = new (C) char[Length]; |
| 651 | std::memcpy(AStrData,Str.data(),Str.size()); |
| 652 | StrData.asChar = AStrData; |
| 653 | break; |
| 654 | } |
| 655 | case 2: { |
| 656 | uint16_t *AStrData = new (C) uint16_t[Length]; |
| 657 | std::memcpy(AStrData,Str.data(),Str.size()); |
| 658 | StrData.asUInt16 = AStrData; |
| 659 | break; |
| 660 | } |
| 661 | case 4: { |
| 662 | uint32_t *AStrData = new (C) uint32_t[Length]; |
| 663 | std::memcpy(AStrData,Str.data(),Str.size()); |
| 664 | StrData.asUInt32 = AStrData; |
| 665 | break; |
| 666 | } |
| 667 | default: |
| 668 | assert(false && "unsupported CharByteWidth"); |
| 669 | } |
Douglas Gregor | 673ecd6 | 2009-04-15 16:35:07 +0000 | [diff] [blame] | 670 | } |
| 671 | |
Chris Lattner | 08f92e3 | 2010-11-17 07:37:15 +0000 | [diff] [blame] | 672 | /// getLocationOfByte - Return a source location that points to the specified |
| 673 | /// byte of this string literal. |
| 674 | /// |
| 675 | /// Strings are amazingly complex. They can be formed from multiple tokens and |
| 676 | /// can have escape sequences in them in addition to the usual trigraph and |
| 677 | /// escaped newline business. This routine handles this complexity. |
| 678 | /// |
| 679 | SourceLocation StringLiteral:: |
| 680 | getLocationOfByte(unsigned ByteNo, const SourceManager &SM, |
| 681 | const LangOptions &Features, const TargetInfo &Target) const { |
Richard Smith | df9ef1b | 2012-06-13 05:37:23 +0000 | [diff] [blame^] | 682 | assert((Kind == StringLiteral::Ascii || Kind == StringLiteral::UTF8) && |
| 683 | "Only narrow string literals are currently supported"); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 684 | |
Chris Lattner | 08f92e3 | 2010-11-17 07:37:15 +0000 | [diff] [blame] | 685 | // Loop over all of the tokens in this string until we find the one that |
| 686 | // contains the byte we're looking for. |
| 687 | unsigned TokNo = 0; |
| 688 | while (1) { |
| 689 | assert(TokNo < getNumConcatenated() && "Invalid byte number!"); |
| 690 | SourceLocation StrTokLoc = getStrTokenLoc(TokNo); |
| 691 | |
| 692 | // Get the spelling of the string so that we can get the data that makes up |
| 693 | // the string literal, not the identifier for the macro it is potentially |
| 694 | // expanded through. |
| 695 | SourceLocation StrTokSpellingLoc = SM.getSpellingLoc(StrTokLoc); |
| 696 | |
| 697 | // Re-lex the token to get its length and original spelling. |
| 698 | std::pair<FileID, unsigned> LocInfo =SM.getDecomposedLoc(StrTokSpellingLoc); |
| 699 | bool Invalid = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 700 | StringRef Buffer = SM.getBufferData(LocInfo.first, &Invalid); |
Chris Lattner | 08f92e3 | 2010-11-17 07:37:15 +0000 | [diff] [blame] | 701 | if (Invalid) |
| 702 | return StrTokSpellingLoc; |
| 703 | |
| 704 | const char *StrData = Buffer.data()+LocInfo.second; |
| 705 | |
Chris Lattner | 08f92e3 | 2010-11-17 07:37:15 +0000 | [diff] [blame] | 706 | // Create a lexer starting at the beginning of this token. |
Argyrios Kyrtzidis | df87558 | 2012-05-11 21:39:18 +0000 | [diff] [blame] | 707 | Lexer TheLexer(SM.getLocForStartOfFile(LocInfo.first), Features, |
| 708 | Buffer.begin(), StrData, Buffer.end()); |
Chris Lattner | 08f92e3 | 2010-11-17 07:37:15 +0000 | [diff] [blame] | 709 | Token TheTok; |
| 710 | TheLexer.LexFromRawLexer(TheTok); |
| 711 | |
| 712 | // Use the StringLiteralParser to compute the length of the string in bytes. |
| 713 | StringLiteralParser SLP(&TheTok, 1, SM, Features, Target); |
| 714 | unsigned TokNumBytes = SLP.GetStringLength(); |
| 715 | |
| 716 | // If the byte is in this token, return the location of the byte. |
| 717 | if (ByteNo < TokNumBytes || |
Hans Wennborg | 935a70c | 2011-06-30 20:17:41 +0000 | [diff] [blame] | 718 | (ByteNo == TokNumBytes && TokNo == getNumConcatenated() - 1)) { |
Chris Lattner | 08f92e3 | 2010-11-17 07:37:15 +0000 | [diff] [blame] | 719 | unsigned Offset = SLP.getOffsetOfStringByte(TheTok, ByteNo); |
| 720 | |
| 721 | // Now that we know the offset of the token in the spelling, use the |
| 722 | // preprocessor to get the offset in the original source. |
| 723 | return Lexer::AdvanceToTokenCharacter(StrTokLoc, Offset, SM, Features); |
| 724 | } |
| 725 | |
| 726 | // Move to the next string token. |
| 727 | ++TokNo; |
| 728 | ByteNo -= TokNumBytes; |
| 729 | } |
| 730 | } |
| 731 | |
| 732 | |
| 733 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 734 | /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it |
| 735 | /// corresponds to, e.g. "sizeof" or "[pre]++". |
| 736 | const char *UnaryOperator::getOpcodeStr(Opcode Op) { |
| 737 | switch (Op) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 738 | case UO_PostInc: return "++"; |
| 739 | case UO_PostDec: return "--"; |
| 740 | case UO_PreInc: return "++"; |
| 741 | case UO_PreDec: return "--"; |
| 742 | case UO_AddrOf: return "&"; |
| 743 | case UO_Deref: return "*"; |
| 744 | case UO_Plus: return "+"; |
| 745 | case UO_Minus: return "-"; |
| 746 | case UO_Not: return "~"; |
| 747 | case UO_LNot: return "!"; |
| 748 | case UO_Real: return "__real"; |
| 749 | case UO_Imag: return "__imag"; |
| 750 | case UO_Extension: return "__extension__"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 751 | } |
David Blaikie | 561d3ab | 2012-01-17 02:30:50 +0000 | [diff] [blame] | 752 | llvm_unreachable("Unknown unary operator"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 753 | } |
| 754 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 755 | UnaryOperatorKind |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 756 | UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) { |
| 757 | switch (OO) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 758 | default: llvm_unreachable("No unary operator for overloaded function"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 759 | case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc; |
| 760 | case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec; |
| 761 | case OO_Amp: return UO_AddrOf; |
| 762 | case OO_Star: return UO_Deref; |
| 763 | case OO_Plus: return UO_Plus; |
| 764 | case OO_Minus: return UO_Minus; |
| 765 | case OO_Tilde: return UO_Not; |
| 766 | case OO_Exclaim: return UO_LNot; |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 767 | } |
| 768 | } |
| 769 | |
| 770 | OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) { |
| 771 | switch (Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 772 | case UO_PostInc: case UO_PreInc: return OO_PlusPlus; |
| 773 | case UO_PostDec: case UO_PreDec: return OO_MinusMinus; |
| 774 | case UO_AddrOf: return OO_Amp; |
| 775 | case UO_Deref: return OO_Star; |
| 776 | case UO_Plus: return OO_Plus; |
| 777 | case UO_Minus: return OO_Minus; |
| 778 | case UO_Not: return OO_Tilde; |
| 779 | case UO_LNot: return OO_Exclaim; |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 780 | default: return OO_None; |
| 781 | } |
| 782 | } |
| 783 | |
| 784 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 785 | //===----------------------------------------------------------------------===// |
| 786 | // Postfix Operators. |
| 787 | //===----------------------------------------------------------------------===// |
| 788 | |
Peter Collingbourne | cc324ad | 2011-02-08 21:18:02 +0000 | [diff] [blame] | 789 | CallExpr::CallExpr(ASTContext& C, StmtClass SC, Expr *fn, unsigned NumPreArgs, |
| 790 | Expr **args, unsigned numargs, QualType t, ExprValueKind VK, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 791 | SourceLocation rparenloc) |
| 792 | : Expr(SC, t, VK, OK_Ordinary, |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 793 | fn->isTypeDependent(), |
| 794 | fn->isValueDependent(), |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 795 | fn->isInstantiationDependent(), |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 796 | fn->containsUnexpandedParameterPack()), |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 797 | NumArgs(numargs) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 798 | |
Peter Collingbourne | cc324ad | 2011-02-08 21:18:02 +0000 | [diff] [blame] | 799 | SubExprs = new (C) Stmt*[numargs+PREARGS_START+NumPreArgs]; |
Douglas Gregor | b460980 | 2008-11-14 16:09:21 +0000 | [diff] [blame] | 800 | SubExprs[FN] = fn; |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 801 | for (unsigned i = 0; i != numargs; ++i) { |
| 802 | if (args[i]->isTypeDependent()) |
| 803 | ExprBits.TypeDependent = true; |
| 804 | if (args[i]->isValueDependent()) |
| 805 | ExprBits.ValueDependent = true; |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 806 | if (args[i]->isInstantiationDependent()) |
| 807 | ExprBits.InstantiationDependent = true; |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 808 | if (args[i]->containsUnexpandedParameterPack()) |
| 809 | ExprBits.ContainsUnexpandedParameterPack = true; |
| 810 | |
Peter Collingbourne | cc324ad | 2011-02-08 21:18:02 +0000 | [diff] [blame] | 811 | SubExprs[i+PREARGS_START+NumPreArgs] = args[i]; |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 812 | } |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 813 | |
Peter Collingbourne | cc324ad | 2011-02-08 21:18:02 +0000 | [diff] [blame] | 814 | CallExprBits.NumPreArgs = NumPreArgs; |
Douglas Gregor | b460980 | 2008-11-14 16:09:21 +0000 | [diff] [blame] | 815 | RParenLoc = rparenloc; |
| 816 | } |
Nate Begeman | e2ce1d9 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 817 | |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 818 | CallExpr::CallExpr(ASTContext& C, Expr *fn, Expr **args, unsigned numargs, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 819 | QualType t, ExprValueKind VK, SourceLocation rparenloc) |
| 820 | : Expr(CallExprClass, t, VK, OK_Ordinary, |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 821 | fn->isTypeDependent(), |
| 822 | fn->isValueDependent(), |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 823 | fn->isInstantiationDependent(), |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 824 | fn->containsUnexpandedParameterPack()), |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 825 | NumArgs(numargs) { |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 826 | |
Peter Collingbourne | cc324ad | 2011-02-08 21:18:02 +0000 | [diff] [blame] | 827 | SubExprs = new (C) Stmt*[numargs+PREARGS_START]; |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 828 | SubExprs[FN] = fn; |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 829 | for (unsigned i = 0; i != numargs; ++i) { |
| 830 | if (args[i]->isTypeDependent()) |
| 831 | ExprBits.TypeDependent = true; |
| 832 | if (args[i]->isValueDependent()) |
| 833 | ExprBits.ValueDependent = true; |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 834 | if (args[i]->isInstantiationDependent()) |
| 835 | ExprBits.InstantiationDependent = true; |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 836 | if (args[i]->containsUnexpandedParameterPack()) |
| 837 | ExprBits.ContainsUnexpandedParameterPack = true; |
| 838 | |
Peter Collingbourne | cc324ad | 2011-02-08 21:18:02 +0000 | [diff] [blame] | 839 | SubExprs[i+PREARGS_START] = args[i]; |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 840 | } |
Ted Kremenek | 668bf91 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 841 | |
Peter Collingbourne | cc324ad | 2011-02-08 21:18:02 +0000 | [diff] [blame] | 842 | CallExprBits.NumPreArgs = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 843 | RParenLoc = rparenloc; |
| 844 | } |
| 845 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 846 | CallExpr::CallExpr(ASTContext &C, StmtClass SC, EmptyShell Empty) |
| 847 | : Expr(SC, Empty), SubExprs(0), NumArgs(0) { |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 848 | // FIXME: Why do we allocate this? |
Peter Collingbourne | cc324ad | 2011-02-08 21:18:02 +0000 | [diff] [blame] | 849 | SubExprs = new (C) Stmt*[PREARGS_START]; |
| 850 | CallExprBits.NumPreArgs = 0; |
| 851 | } |
| 852 | |
| 853 | CallExpr::CallExpr(ASTContext &C, StmtClass SC, unsigned NumPreArgs, |
| 854 | EmptyShell Empty) |
| 855 | : Expr(SC, Empty), SubExprs(0), NumArgs(0) { |
| 856 | // FIXME: Why do we allocate this? |
| 857 | SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs]; |
| 858 | CallExprBits.NumPreArgs = NumPreArgs; |
Douglas Gregor | 1f0d013 | 2009-04-15 17:43:59 +0000 | [diff] [blame] | 859 | } |
| 860 | |
Nuno Lopes | d20254f | 2009-12-20 23:11:08 +0000 | [diff] [blame] | 861 | Decl *CallExpr::getCalleeDecl() { |
John McCall | e8683d6 | 2011-09-13 23:08:34 +0000 | [diff] [blame] | 862 | Expr *CEE = getCallee()->IgnoreParenImpCasts(); |
Douglas Gregor | 1ddc9c4 | 2011-09-06 21:41:04 +0000 | [diff] [blame] | 863 | |
| 864 | while (SubstNonTypeTemplateParmExpr *NTTP |
| 865 | = dyn_cast<SubstNonTypeTemplateParmExpr>(CEE)) { |
| 866 | CEE = NTTP->getReplacement()->IgnoreParenCasts(); |
| 867 | } |
| 868 | |
Sebastian Redl | 2001215 | 2010-09-10 20:55:30 +0000 | [diff] [blame] | 869 | // If we're calling a dereference, look at the pointer instead. |
| 870 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) { |
| 871 | if (BO->isPtrMemOp()) |
| 872 | CEE = BO->getRHS()->IgnoreParenCasts(); |
| 873 | } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) { |
| 874 | if (UO->getOpcode() == UO_Deref) |
| 875 | CEE = UO->getSubExpr()->IgnoreParenCasts(); |
| 876 | } |
Chris Lattner | 6346f96 | 2009-07-17 15:46:27 +0000 | [diff] [blame] | 877 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE)) |
Nuno Lopes | d20254f | 2009-12-20 23:11:08 +0000 | [diff] [blame] | 878 | return DRE->getDecl(); |
Nuno Lopes | cb1c77f | 2009-12-24 00:28:18 +0000 | [diff] [blame] | 879 | if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE)) |
| 880 | return ME->getMemberDecl(); |
Zhongxing Xu | a004254 | 2009-07-17 07:29:51 +0000 | [diff] [blame] | 881 | |
| 882 | return 0; |
| 883 | } |
| 884 | |
Nuno Lopes | d20254f | 2009-12-20 23:11:08 +0000 | [diff] [blame] | 885 | FunctionDecl *CallExpr::getDirectCallee() { |
Chris Lattner | caabf9b | 2009-12-21 01:10:56 +0000 | [diff] [blame] | 886 | return dyn_cast_or_null<FunctionDecl>(getCalleeDecl()); |
Nuno Lopes | d20254f | 2009-12-20 23:11:08 +0000 | [diff] [blame] | 887 | } |
| 888 | |
Chris Lattner | d18b329 | 2007-12-28 05:25:02 +0000 | [diff] [blame] | 889 | /// setNumArgs - This changes the number of arguments present in this call. |
| 890 | /// Any orphaned expressions are deleted by this, and any new operands are set |
| 891 | /// to null. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 892 | void CallExpr::setNumArgs(ASTContext& C, unsigned NumArgs) { |
Chris Lattner | d18b329 | 2007-12-28 05:25:02 +0000 | [diff] [blame] | 893 | // No change, just return. |
| 894 | if (NumArgs == getNumArgs()) return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 895 | |
Chris Lattner | d18b329 | 2007-12-28 05:25:02 +0000 | [diff] [blame] | 896 | // If shrinking # arguments, just delete the extras and forgot them. |
| 897 | if (NumArgs < getNumArgs()) { |
Chris Lattner | d18b329 | 2007-12-28 05:25:02 +0000 | [diff] [blame] | 898 | this->NumArgs = NumArgs; |
| 899 | return; |
| 900 | } |
| 901 | |
| 902 | // Otherwise, we are growing the # arguments. New an bigger argument array. |
Peter Collingbourne | cc324ad | 2011-02-08 21:18:02 +0000 | [diff] [blame] | 903 | unsigned NumPreArgs = getNumPreArgs(); |
| 904 | Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs]; |
Chris Lattner | d18b329 | 2007-12-28 05:25:02 +0000 | [diff] [blame] | 905 | // Copy over args. |
Peter Collingbourne | cc324ad | 2011-02-08 21:18:02 +0000 | [diff] [blame] | 906 | for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i) |
Chris Lattner | d18b329 | 2007-12-28 05:25:02 +0000 | [diff] [blame] | 907 | NewSubExprs[i] = SubExprs[i]; |
| 908 | // Null out new args. |
Peter Collingbourne | cc324ad | 2011-02-08 21:18:02 +0000 | [diff] [blame] | 909 | for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs; |
| 910 | i != NumArgs+PREARGS_START+NumPreArgs; ++i) |
Chris Lattner | d18b329 | 2007-12-28 05:25:02 +0000 | [diff] [blame] | 911 | NewSubExprs[i] = 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 912 | |
Douglas Gregor | 88c9a46 | 2009-04-17 21:46:47 +0000 | [diff] [blame] | 913 | if (SubExprs) C.Deallocate(SubExprs); |
Chris Lattner | d18b329 | 2007-12-28 05:25:02 +0000 | [diff] [blame] | 914 | SubExprs = NewSubExprs; |
| 915 | this->NumArgs = NumArgs; |
| 916 | } |
| 917 | |
Chris Lattner | cb88896 | 2008-10-06 05:00:53 +0000 | [diff] [blame] | 918 | /// isBuiltinCall - If this is a call to a builtin, return the builtin ID. If |
| 919 | /// not, return 0. |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 920 | unsigned CallExpr::isBuiltinCall() const { |
Steve Naroff | c4f8e8b | 2008-01-31 01:07:12 +0000 | [diff] [blame] | 921 | // 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] | 922 | // function. As a result, we try and obtain the DeclRefExpr from the |
Steve Naroff | c4f8e8b | 2008-01-31 01:07:12 +0000 | [diff] [blame] | 923 | // ImplicitCastExpr. |
| 924 | const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee()); |
| 925 | if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()). |
Chris Lattner | cb88896 | 2008-10-06 05:00:53 +0000 | [diff] [blame] | 926 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 927 | |
Steve Naroff | c4f8e8b | 2008-01-31 01:07:12 +0000 | [diff] [blame] | 928 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr()); |
| 929 | if (!DRE) |
Chris Lattner | cb88896 | 2008-10-06 05:00:53 +0000 | [diff] [blame] | 930 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 931 | |
Anders Carlsson | bcba201 | 2008-01-31 02:13:57 +0000 | [diff] [blame] | 932 | const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl()); |
| 933 | if (!FDecl) |
Chris Lattner | cb88896 | 2008-10-06 05:00:53 +0000 | [diff] [blame] | 934 | return 0; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 935 | |
Douglas Gregor | 4fcd399 | 2008-11-21 15:30:19 +0000 | [diff] [blame] | 936 | if (!FDecl->getIdentifier()) |
| 937 | return 0; |
| 938 | |
Douglas Gregor | 7814e6d | 2009-09-12 00:22:50 +0000 | [diff] [blame] | 939 | return FDecl->getBuiltinID(); |
Chris Lattner | cb88896 | 2008-10-06 05:00:53 +0000 | [diff] [blame] | 940 | } |
Anders Carlsson | bcba201 | 2008-01-31 02:13:57 +0000 | [diff] [blame] | 941 | |
Anders Carlsson | 6dde78f | 2009-05-26 04:57:27 +0000 | [diff] [blame] | 942 | QualType CallExpr::getCallReturnType() const { |
| 943 | QualType CalleeType = getCallee()->getType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 944 | if (const PointerType *FnTypePtr = CalleeType->getAs<PointerType>()) |
Anders Carlsson | 6dde78f | 2009-05-26 04:57:27 +0000 | [diff] [blame] | 945 | CalleeType = FnTypePtr->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 946 | else if (const BlockPointerType *BPT = CalleeType->getAs<BlockPointerType>()) |
Anders Carlsson | 6dde78f | 2009-05-26 04:57:27 +0000 | [diff] [blame] | 947 | CalleeType = BPT->getPointeeType(); |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 948 | else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember)) |
| 949 | // This should never be overloaded and so should never return null. |
| 950 | CalleeType = Expr::findBoundMemberType(getCallee()); |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 951 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 952 | const FunctionType *FnType = CalleeType->castAs<FunctionType>(); |
Anders Carlsson | 6dde78f | 2009-05-26 04:57:27 +0000 | [diff] [blame] | 953 | return FnType->getResultType(); |
| 954 | } |
Chris Lattner | cb88896 | 2008-10-06 05:00:53 +0000 | [diff] [blame] | 955 | |
John McCall | 2882eca | 2011-02-21 06:23:05 +0000 | [diff] [blame] | 956 | SourceRange CallExpr::getSourceRange() const { |
| 957 | if (isa<CXXOperatorCallExpr>(this)) |
| 958 | return cast<CXXOperatorCallExpr>(this)->getSourceRange(); |
| 959 | |
| 960 | SourceLocation begin = getCallee()->getLocStart(); |
| 961 | if (begin.isInvalid() && getNumArgs() > 0) |
| 962 | begin = getArg(0)->getLocStart(); |
| 963 | SourceLocation end = getRParenLoc(); |
| 964 | if (end.isInvalid() && getNumArgs() > 0) |
| 965 | end = getArg(getNumArgs() - 1)->getLocEnd(); |
| 966 | return SourceRange(begin, end); |
| 967 | } |
Daniel Dunbar | 8fbc6d2 | 2012-03-09 15:39:24 +0000 | [diff] [blame] | 968 | SourceLocation CallExpr::getLocStart() const { |
| 969 | if (isa<CXXOperatorCallExpr>(this)) |
| 970 | return cast<CXXOperatorCallExpr>(this)->getSourceRange().getBegin(); |
| 971 | |
| 972 | SourceLocation begin = getCallee()->getLocStart(); |
| 973 | if (begin.isInvalid() && getNumArgs() > 0) |
| 974 | begin = getArg(0)->getLocStart(); |
| 975 | return begin; |
| 976 | } |
| 977 | SourceLocation CallExpr::getLocEnd() const { |
| 978 | if (isa<CXXOperatorCallExpr>(this)) |
| 979 | return cast<CXXOperatorCallExpr>(this)->getSourceRange().getEnd(); |
| 980 | |
| 981 | SourceLocation end = getRParenLoc(); |
| 982 | if (end.isInvalid() && getNumArgs() > 0) |
| 983 | end = getArg(getNumArgs() - 1)->getLocEnd(); |
| 984 | return end; |
| 985 | } |
John McCall | 2882eca | 2011-02-21 06:23:05 +0000 | [diff] [blame] | 986 | |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 987 | OffsetOfExpr *OffsetOfExpr::Create(ASTContext &C, QualType type, |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 988 | SourceLocation OperatorLoc, |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 989 | TypeSourceInfo *tsi, |
| 990 | OffsetOfNode* compsPtr, unsigned numComps, |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 991 | Expr** exprsPtr, unsigned numExprs, |
| 992 | SourceLocation RParenLoc) { |
| 993 | void *Mem = C.Allocate(sizeof(OffsetOfExpr) + |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 994 | sizeof(OffsetOfNode) * numComps + |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 995 | sizeof(Expr*) * numExprs); |
| 996 | |
| 997 | return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, compsPtr, numComps, |
| 998 | exprsPtr, numExprs, RParenLoc); |
| 999 | } |
| 1000 | |
| 1001 | OffsetOfExpr *OffsetOfExpr::CreateEmpty(ASTContext &C, |
| 1002 | unsigned numComps, unsigned numExprs) { |
| 1003 | void *Mem = C.Allocate(sizeof(OffsetOfExpr) + |
| 1004 | sizeof(OffsetOfNode) * numComps + |
| 1005 | sizeof(Expr*) * numExprs); |
| 1006 | return new (Mem) OffsetOfExpr(numComps, numExprs); |
| 1007 | } |
| 1008 | |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 1009 | OffsetOfExpr::OffsetOfExpr(ASTContext &C, QualType type, |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 1010 | SourceLocation OperatorLoc, TypeSourceInfo *tsi, |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 1011 | OffsetOfNode* compsPtr, unsigned numComps, |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 1012 | Expr** exprsPtr, unsigned numExprs, |
| 1013 | SourceLocation RParenLoc) |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1014 | : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary, |
| 1015 | /*TypeDependent=*/false, |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 1016 | /*ValueDependent=*/tsi->getType()->isDependentType(), |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 1017 | tsi->getType()->isInstantiationDependentType(), |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 1018 | tsi->getType()->containsUnexpandedParameterPack()), |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 1019 | OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi), |
| 1020 | NumComps(numComps), NumExprs(numExprs) |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 1021 | { |
| 1022 | for(unsigned i = 0; i < numComps; ++i) { |
| 1023 | setComponent(i, compsPtr[i]); |
| 1024 | } |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 1025 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 1026 | for(unsigned i = 0; i < numExprs; ++i) { |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 1027 | if (exprsPtr[i]->isTypeDependent() || exprsPtr[i]->isValueDependent()) |
| 1028 | ExprBits.ValueDependent = true; |
| 1029 | if (exprsPtr[i]->containsUnexpandedParameterPack()) |
| 1030 | ExprBits.ContainsUnexpandedParameterPack = true; |
| 1031 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 1032 | setIndexExpr(i, exprsPtr[i]); |
| 1033 | } |
| 1034 | } |
| 1035 | |
| 1036 | IdentifierInfo *OffsetOfExpr::OffsetOfNode::getFieldName() const { |
| 1037 | assert(getKind() == Field || getKind() == Identifier); |
| 1038 | if (getKind() == Field) |
| 1039 | return getField()->getIdentifier(); |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 1040 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 1041 | return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask); |
| 1042 | } |
| 1043 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1044 | MemberExpr *MemberExpr::Create(ASTContext &C, Expr *base, bool isarrow, |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 1045 | NestedNameSpecifierLoc QualifierLoc, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1046 | SourceLocation TemplateKWLoc, |
Eli Friedman | f595cc4 | 2009-12-04 06:40:45 +0000 | [diff] [blame] | 1047 | ValueDecl *memberdecl, |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1048 | DeclAccessPair founddecl, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1049 | DeclarationNameInfo nameinfo, |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1050 | const TemplateArgumentListInfo *targs, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1051 | QualType ty, |
| 1052 | ExprValueKind vk, |
| 1053 | ExprObjectKind ok) { |
Douglas Gregor | 83f6faf | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 1054 | std::size_t Size = sizeof(MemberExpr); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1055 | |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 1056 | bool hasQualOrFound = (QualifierLoc || |
John McCall | 161755a | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1057 | founddecl.getDecl() != memberdecl || |
| 1058 | founddecl.getAccess() != memberdecl->getAccess()); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1059 | if (hasQualOrFound) |
| 1060 | Size += sizeof(MemberNameQualifier); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1061 | |
John McCall | d5532b6 | 2009-11-23 01:53:49 +0000 | [diff] [blame] | 1062 | if (targs) |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1063 | Size += ASTTemplateKWAndArgsInfo::sizeFor(targs->size()); |
| 1064 | else if (TemplateKWLoc.isValid()) |
| 1065 | Size += ASTTemplateKWAndArgsInfo::sizeFor(0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1066 | |
Chris Lattner | 3248854 | 2010-10-30 05:14:06 +0000 | [diff] [blame] | 1067 | void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>()); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1068 | MemberExpr *E = new (Mem) MemberExpr(base, isarrow, memberdecl, nameinfo, |
| 1069 | ty, vk, ok); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1070 | |
| 1071 | if (hasQualOrFound) { |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 1072 | // FIXME: Wrong. We should be looking at the member declaration we found. |
| 1073 | if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1074 | E->setValueDependent(true); |
| 1075 | E->setTypeDependent(true); |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 1076 | E->setInstantiationDependent(true); |
| 1077 | } |
| 1078 | else if (QualifierLoc && |
| 1079 | QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent()) |
| 1080 | E->setInstantiationDependent(true); |
| 1081 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1082 | E->HasQualifierOrFoundDecl = true; |
| 1083 | |
| 1084 | MemberNameQualifier *NQ = E->getMemberQualifier(); |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 1085 | NQ->QualifierLoc = QualifierLoc; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1086 | NQ->FoundDecl = founddecl; |
| 1087 | } |
| 1088 | |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1089 | E->HasTemplateKWAndArgsInfo = (targs || TemplateKWLoc.isValid()); |
| 1090 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1091 | if (targs) { |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 1092 | bool Dependent = false; |
| 1093 | bool InstantiationDependent = false; |
| 1094 | bool ContainsUnexpandedParameterPack = false; |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1095 | E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc, *targs, |
| 1096 | Dependent, |
| 1097 | InstantiationDependent, |
| 1098 | ContainsUnexpandedParameterPack); |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 1099 | if (InstantiationDependent) |
| 1100 | E->setInstantiationDependent(true); |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1101 | } else if (TemplateKWLoc.isValid()) { |
| 1102 | E->getTemplateKWAndArgsInfo()->initializeFrom(TemplateKWLoc); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | return E; |
Douglas Gregor | 83f6faf | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 1106 | } |
| 1107 | |
Douglas Gregor | 75e8504 | 2011-03-02 21:06:53 +0000 | [diff] [blame] | 1108 | SourceRange MemberExpr::getSourceRange() const { |
Daniel Dunbar | 396ec67 | 2012-03-09 15:39:15 +0000 | [diff] [blame] | 1109 | return SourceRange(getLocStart(), getLocEnd()); |
| 1110 | } |
| 1111 | SourceLocation MemberExpr::getLocStart() const { |
Douglas Gregor | 75e8504 | 2011-03-02 21:06:53 +0000 | [diff] [blame] | 1112 | if (isImplicitAccess()) { |
| 1113 | if (hasQualifier()) |
Daniel Dunbar | 396ec67 | 2012-03-09 15:39:15 +0000 | [diff] [blame] | 1114 | return getQualifierLoc().getBeginLoc(); |
| 1115 | return MemberLoc; |
Douglas Gregor | 75e8504 | 2011-03-02 21:06:53 +0000 | [diff] [blame] | 1116 | } |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1117 | |
Daniel Dunbar | 396ec67 | 2012-03-09 15:39:15 +0000 | [diff] [blame] | 1118 | // FIXME: We don't want this to happen. Rather, we should be able to |
| 1119 | // detect all kinds of implicit accesses more cleanly. |
| 1120 | SourceLocation BaseStartLoc = getBase()->getLocStart(); |
| 1121 | if (BaseStartLoc.isValid()) |
| 1122 | return BaseStartLoc; |
| 1123 | return MemberLoc; |
| 1124 | } |
| 1125 | SourceLocation MemberExpr::getLocEnd() const { |
| 1126 | if (hasExplicitTemplateArgs()) |
| 1127 | return getRAngleLoc(); |
| 1128 | return getMemberNameInfo().getEndLoc(); |
Douglas Gregor | 75e8504 | 2011-03-02 21:06:53 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 1131 | void CastExpr::CheckCastConsistency() const { |
| 1132 | switch (getCastKind()) { |
| 1133 | case CK_DerivedToBase: |
| 1134 | case CK_UncheckedDerivedToBase: |
| 1135 | case CK_DerivedToBaseMemberPointer: |
| 1136 | case CK_BaseToDerived: |
| 1137 | case CK_BaseToDerivedMemberPointer: |
| 1138 | assert(!path_empty() && "Cast kind should have a base path!"); |
| 1139 | break; |
| 1140 | |
| 1141 | case CK_CPointerToObjCPointerCast: |
| 1142 | assert(getType()->isObjCObjectPointerType()); |
| 1143 | assert(getSubExpr()->getType()->isPointerType()); |
| 1144 | goto CheckNoBasePath; |
| 1145 | |
| 1146 | case CK_BlockPointerToObjCPointerCast: |
| 1147 | assert(getType()->isObjCObjectPointerType()); |
| 1148 | assert(getSubExpr()->getType()->isBlockPointerType()); |
| 1149 | goto CheckNoBasePath; |
| 1150 | |
John McCall | 4d4e5c1 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 1151 | case CK_ReinterpretMemberPointer: |
| 1152 | assert(getType()->isMemberPointerType()); |
| 1153 | assert(getSubExpr()->getType()->isMemberPointerType()); |
| 1154 | goto CheckNoBasePath; |
| 1155 | |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 1156 | case CK_BitCast: |
| 1157 | // Arbitrary casts to C pointer types count as bitcasts. |
| 1158 | // Otherwise, we should only have block and ObjC pointer casts |
| 1159 | // here if they stay within the type kind. |
| 1160 | if (!getType()->isPointerType()) { |
| 1161 | assert(getType()->isObjCObjectPointerType() == |
| 1162 | getSubExpr()->getType()->isObjCObjectPointerType()); |
| 1163 | assert(getType()->isBlockPointerType() == |
| 1164 | getSubExpr()->getType()->isBlockPointerType()); |
| 1165 | } |
| 1166 | goto CheckNoBasePath; |
| 1167 | |
| 1168 | case CK_AnyPointerToBlockPointerCast: |
| 1169 | assert(getType()->isBlockPointerType()); |
| 1170 | assert(getSubExpr()->getType()->isAnyPointerType() && |
| 1171 | !getSubExpr()->getType()->isBlockPointerType()); |
| 1172 | goto CheckNoBasePath; |
| 1173 | |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 1174 | case CK_CopyAndAutoreleaseBlockObject: |
| 1175 | assert(getType()->isBlockPointerType()); |
| 1176 | assert(getSubExpr()->getType()->isBlockPointerType()); |
| 1177 | goto CheckNoBasePath; |
| 1178 | |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 1179 | // These should not have an inheritance path. |
| 1180 | case CK_Dynamic: |
| 1181 | case CK_ToUnion: |
| 1182 | case CK_ArrayToPointerDecay: |
| 1183 | case CK_FunctionToPointerDecay: |
| 1184 | case CK_NullToMemberPointer: |
| 1185 | case CK_NullToPointer: |
| 1186 | case CK_ConstructorConversion: |
| 1187 | case CK_IntegralToPointer: |
| 1188 | case CK_PointerToIntegral: |
| 1189 | case CK_ToVoid: |
| 1190 | case CK_VectorSplat: |
| 1191 | case CK_IntegralCast: |
| 1192 | case CK_IntegralToFloating: |
| 1193 | case CK_FloatingToIntegral: |
| 1194 | case CK_FloatingCast: |
| 1195 | case CK_ObjCObjectLValueCast: |
| 1196 | case CK_FloatingRealToComplex: |
| 1197 | case CK_FloatingComplexToReal: |
| 1198 | case CK_FloatingComplexCast: |
| 1199 | case CK_FloatingComplexToIntegralComplex: |
| 1200 | case CK_IntegralRealToComplex: |
| 1201 | case CK_IntegralComplexToReal: |
| 1202 | case CK_IntegralComplexCast: |
| 1203 | case CK_IntegralComplexToFloatingComplex: |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 1204 | case CK_ARCProduceObject: |
| 1205 | case CK_ARCConsumeObject: |
| 1206 | case CK_ARCReclaimReturnedObject: |
| 1207 | case CK_ARCExtendBlockObject: |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 1208 | assert(!getType()->isBooleanType() && "unheralded conversion to bool"); |
| 1209 | goto CheckNoBasePath; |
| 1210 | |
| 1211 | case CK_Dependent: |
| 1212 | case CK_LValueToRValue: |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 1213 | case CK_NoOp: |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 1214 | case CK_AtomicToNonAtomic: |
| 1215 | case CK_NonAtomicToAtomic: |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 1216 | case CK_PointerToBoolean: |
| 1217 | case CK_IntegralToBoolean: |
| 1218 | case CK_FloatingToBoolean: |
| 1219 | case CK_MemberPointerToBoolean: |
| 1220 | case CK_FloatingComplexToBoolean: |
| 1221 | case CK_IntegralComplexToBoolean: |
| 1222 | case CK_LValueBitCast: // -> bool& |
| 1223 | case CK_UserDefinedConversion: // operator bool() |
| 1224 | CheckNoBasePath: |
| 1225 | assert(path_empty() && "Cast kind should not have a base path!"); |
| 1226 | break; |
| 1227 | } |
| 1228 | } |
| 1229 | |
Anders Carlsson | f8ec55a | 2009-09-03 00:59:21 +0000 | [diff] [blame] | 1230 | const char *CastExpr::getCastKindName() const { |
| 1231 | switch (getCastKind()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1232 | case CK_Dependent: |
| 1233 | return "Dependent"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1234 | case CK_BitCast: |
Anders Carlsson | f8ec55a | 2009-09-03 00:59:21 +0000 | [diff] [blame] | 1235 | return "BitCast"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1236 | case CK_LValueBitCast: |
Douglas Gregor | e39a389 | 2010-07-13 23:17:26 +0000 | [diff] [blame] | 1237 | return "LValueBitCast"; |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 1238 | case CK_LValueToRValue: |
| 1239 | return "LValueToRValue"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1240 | case CK_NoOp: |
Anders Carlsson | f8ec55a | 2009-09-03 00:59:21 +0000 | [diff] [blame] | 1241 | return "NoOp"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1242 | case CK_BaseToDerived: |
Anders Carlsson | 11de6de | 2009-11-12 16:43:42 +0000 | [diff] [blame] | 1243 | return "BaseToDerived"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1244 | case CK_DerivedToBase: |
Anders Carlsson | f8ec55a | 2009-09-03 00:59:21 +0000 | [diff] [blame] | 1245 | return "DerivedToBase"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1246 | case CK_UncheckedDerivedToBase: |
John McCall | 23cba80 | 2010-03-30 23:58:03 +0000 | [diff] [blame] | 1247 | return "UncheckedDerivedToBase"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1248 | case CK_Dynamic: |
Anders Carlsson | f8ec55a | 2009-09-03 00:59:21 +0000 | [diff] [blame] | 1249 | return "Dynamic"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1250 | case CK_ToUnion: |
Anders Carlsson | f8ec55a | 2009-09-03 00:59:21 +0000 | [diff] [blame] | 1251 | return "ToUnion"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1252 | case CK_ArrayToPointerDecay: |
Anders Carlsson | f8ec55a | 2009-09-03 00:59:21 +0000 | [diff] [blame] | 1253 | return "ArrayToPointerDecay"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1254 | case CK_FunctionToPointerDecay: |
Anders Carlsson | f8ec55a | 2009-09-03 00:59:21 +0000 | [diff] [blame] | 1255 | return "FunctionToPointerDecay"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1256 | case CK_NullToMemberPointer: |
Anders Carlsson | f8ec55a | 2009-09-03 00:59:21 +0000 | [diff] [blame] | 1257 | return "NullToMemberPointer"; |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 1258 | case CK_NullToPointer: |
| 1259 | return "NullToPointer"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1260 | case CK_BaseToDerivedMemberPointer: |
Anders Carlsson | f8ec55a | 2009-09-03 00:59:21 +0000 | [diff] [blame] | 1261 | return "BaseToDerivedMemberPointer"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1262 | case CK_DerivedToBaseMemberPointer: |
Anders Carlsson | 1a31a18 | 2009-10-30 00:46:35 +0000 | [diff] [blame] | 1263 | return "DerivedToBaseMemberPointer"; |
John McCall | 4d4e5c1 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 1264 | case CK_ReinterpretMemberPointer: |
| 1265 | return "ReinterpretMemberPointer"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1266 | case CK_UserDefinedConversion: |
Anders Carlsson | f8ec55a | 2009-09-03 00:59:21 +0000 | [diff] [blame] | 1267 | return "UserDefinedConversion"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1268 | case CK_ConstructorConversion: |
Anders Carlsson | f8ec55a | 2009-09-03 00:59:21 +0000 | [diff] [blame] | 1269 | return "ConstructorConversion"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1270 | case CK_IntegralToPointer: |
Anders Carlsson | 7f9e646 | 2009-09-15 04:48:33 +0000 | [diff] [blame] | 1271 | return "IntegralToPointer"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1272 | case CK_PointerToIntegral: |
Anders Carlsson | 7f9e646 | 2009-09-15 04:48:33 +0000 | [diff] [blame] | 1273 | return "PointerToIntegral"; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1274 | case CK_PointerToBoolean: |
| 1275 | return "PointerToBoolean"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1276 | case CK_ToVoid: |
Anders Carlsson | ebeaf20 | 2009-10-16 02:35:04 +0000 | [diff] [blame] | 1277 | return "ToVoid"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1278 | case CK_VectorSplat: |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 1279 | return "VectorSplat"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1280 | case CK_IntegralCast: |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 1281 | return "IntegralCast"; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1282 | case CK_IntegralToBoolean: |
| 1283 | return "IntegralToBoolean"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1284 | case CK_IntegralToFloating: |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 1285 | return "IntegralToFloating"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1286 | case CK_FloatingToIntegral: |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 1287 | return "FloatingToIntegral"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1288 | case CK_FloatingCast: |
Benjamin Kramer | c6b2916 | 2009-10-18 19:02:15 +0000 | [diff] [blame] | 1289 | return "FloatingCast"; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 1290 | case CK_FloatingToBoolean: |
| 1291 | return "FloatingToBoolean"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1292 | case CK_MemberPointerToBoolean: |
Anders Carlsson | bc0e078 | 2009-11-23 20:04:44 +0000 | [diff] [blame] | 1293 | return "MemberPointerToBoolean"; |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 1294 | case CK_CPointerToObjCPointerCast: |
| 1295 | return "CPointerToObjCPointerCast"; |
| 1296 | case CK_BlockPointerToObjCPointerCast: |
| 1297 | return "BlockPointerToObjCPointerCast"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1298 | case CK_AnyPointerToBlockPointerCast: |
Fariborz Jahanian | 3b27f1a | 2009-12-11 22:40:48 +0000 | [diff] [blame] | 1299 | return "AnyPointerToBlockPointerCast"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1300 | case CK_ObjCObjectLValueCast: |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 1301 | return "ObjCObjectLValueCast"; |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 1302 | case CK_FloatingRealToComplex: |
| 1303 | return "FloatingRealToComplex"; |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 1304 | case CK_FloatingComplexToReal: |
| 1305 | return "FloatingComplexToReal"; |
| 1306 | case CK_FloatingComplexToBoolean: |
| 1307 | return "FloatingComplexToBoolean"; |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 1308 | case CK_FloatingComplexCast: |
| 1309 | return "FloatingComplexCast"; |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 1310 | case CK_FloatingComplexToIntegralComplex: |
| 1311 | return "FloatingComplexToIntegralComplex"; |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 1312 | case CK_IntegralRealToComplex: |
| 1313 | return "IntegralRealToComplex"; |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 1314 | case CK_IntegralComplexToReal: |
| 1315 | return "IntegralComplexToReal"; |
| 1316 | case CK_IntegralComplexToBoolean: |
| 1317 | return "IntegralComplexToBoolean"; |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 1318 | case CK_IntegralComplexCast: |
| 1319 | return "IntegralComplexCast"; |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 1320 | case CK_IntegralComplexToFloatingComplex: |
| 1321 | return "IntegralComplexToFloatingComplex"; |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 1322 | case CK_ARCConsumeObject: |
| 1323 | return "ARCConsumeObject"; |
| 1324 | case CK_ARCProduceObject: |
| 1325 | return "ARCProduceObject"; |
| 1326 | case CK_ARCReclaimReturnedObject: |
| 1327 | return "ARCReclaimReturnedObject"; |
| 1328 | case CK_ARCExtendBlockObject: |
| 1329 | return "ARCCExtendBlockObject"; |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 1330 | case CK_AtomicToNonAtomic: |
| 1331 | return "AtomicToNonAtomic"; |
| 1332 | case CK_NonAtomicToAtomic: |
| 1333 | return "NonAtomicToAtomic"; |
Douglas Gregor | ac1303e | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 1334 | case CK_CopyAndAutoreleaseBlockObject: |
| 1335 | return "CopyAndAutoreleaseBlockObject"; |
Anders Carlsson | f8ec55a | 2009-09-03 00:59:21 +0000 | [diff] [blame] | 1336 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1337 | |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 1338 | llvm_unreachable("Unhandled cast kind!"); |
Anders Carlsson | f8ec55a | 2009-09-03 00:59:21 +0000 | [diff] [blame] | 1339 | } |
| 1340 | |
Douglas Gregor | 6eef519 | 2009-12-14 19:27:10 +0000 | [diff] [blame] | 1341 | Expr *CastExpr::getSubExprAsWritten() { |
| 1342 | Expr *SubExpr = 0; |
| 1343 | CastExpr *E = this; |
| 1344 | do { |
| 1345 | SubExpr = E->getSubExpr(); |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 1346 | |
| 1347 | // Skip through reference binding to temporary. |
| 1348 | if (MaterializeTemporaryExpr *Materialize |
| 1349 | = dyn_cast<MaterializeTemporaryExpr>(SubExpr)) |
| 1350 | SubExpr = Materialize->GetTemporaryExpr(); |
| 1351 | |
Douglas Gregor | 6eef519 | 2009-12-14 19:27:10 +0000 | [diff] [blame] | 1352 | // Skip any temporary bindings; they're implicit. |
| 1353 | if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr)) |
| 1354 | SubExpr = Binder->getSubExpr(); |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 1355 | |
Douglas Gregor | 6eef519 | 2009-12-14 19:27:10 +0000 | [diff] [blame] | 1356 | // Conversions by constructor and conversion functions have a |
| 1357 | // subexpression describing the call; strip it off. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1358 | if (E->getCastKind() == CK_ConstructorConversion) |
Douglas Gregor | 6eef519 | 2009-12-14 19:27:10 +0000 | [diff] [blame] | 1359 | SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1360 | else if (E->getCastKind() == CK_UserDefinedConversion) |
Douglas Gregor | 6eef519 | 2009-12-14 19:27:10 +0000 | [diff] [blame] | 1361 | SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument(); |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 1362 | |
Douglas Gregor | 6eef519 | 2009-12-14 19:27:10 +0000 | [diff] [blame] | 1363 | // If the subexpression we're left with is an implicit cast, look |
| 1364 | // through that, too. |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 1365 | } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr))); |
| 1366 | |
Douglas Gregor | 6eef519 | 2009-12-14 19:27:10 +0000 | [diff] [blame] | 1367 | return SubExpr; |
| 1368 | } |
| 1369 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1370 | CXXBaseSpecifier **CastExpr::path_buffer() { |
| 1371 | switch (getStmtClass()) { |
| 1372 | #define ABSTRACT_STMT(x) |
| 1373 | #define CASTEXPR(Type, Base) \ |
| 1374 | case Stmt::Type##Class: \ |
| 1375 | return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1); |
| 1376 | #define STMT(Type, Base) |
| 1377 | #include "clang/AST/StmtNodes.inc" |
| 1378 | default: |
| 1379 | llvm_unreachable("non-cast expressions not possible here"); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1380 | } |
| 1381 | } |
| 1382 | |
| 1383 | void CastExpr::setCastPath(const CXXCastPath &Path) { |
| 1384 | assert(Path.size() == path_size()); |
| 1385 | memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*)); |
| 1386 | } |
| 1387 | |
| 1388 | ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T, |
| 1389 | CastKind Kind, Expr *Operand, |
| 1390 | const CXXCastPath *BasePath, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 1391 | ExprValueKind VK) { |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1392 | unsigned PathSize = (BasePath ? BasePath->size() : 0); |
| 1393 | void *Buffer = |
| 1394 | C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*)); |
| 1395 | ImplicitCastExpr *E = |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 1396 | new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1397 | if (PathSize) E->setCastPath(*BasePath); |
| 1398 | return E; |
| 1399 | } |
| 1400 | |
| 1401 | ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C, |
| 1402 | unsigned PathSize) { |
| 1403 | void *Buffer = |
| 1404 | C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*)); |
| 1405 | return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize); |
| 1406 | } |
| 1407 | |
| 1408 | |
| 1409 | CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1410 | ExprValueKind VK, CastKind K, Expr *Op, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1411 | const CXXCastPath *BasePath, |
| 1412 | TypeSourceInfo *WrittenTy, |
| 1413 | SourceLocation L, SourceLocation R) { |
| 1414 | unsigned PathSize = (BasePath ? BasePath->size() : 0); |
| 1415 | void *Buffer = |
| 1416 | C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*)); |
| 1417 | CStyleCastExpr *E = |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1418 | new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1419 | if (PathSize) E->setCastPath(*BasePath); |
| 1420 | return E; |
| 1421 | } |
| 1422 | |
| 1423 | CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) { |
| 1424 | void *Buffer = |
| 1425 | C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*)); |
| 1426 | return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize); |
| 1427 | } |
| 1428 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1429 | /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it |
| 1430 | /// corresponds to, e.g. "<<=". |
| 1431 | const char *BinaryOperator::getOpcodeStr(Opcode Op) { |
| 1432 | switch (Op) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1433 | case BO_PtrMemD: return ".*"; |
| 1434 | case BO_PtrMemI: return "->*"; |
| 1435 | case BO_Mul: return "*"; |
| 1436 | case BO_Div: return "/"; |
| 1437 | case BO_Rem: return "%"; |
| 1438 | case BO_Add: return "+"; |
| 1439 | case BO_Sub: return "-"; |
| 1440 | case BO_Shl: return "<<"; |
| 1441 | case BO_Shr: return ">>"; |
| 1442 | case BO_LT: return "<"; |
| 1443 | case BO_GT: return ">"; |
| 1444 | case BO_LE: return "<="; |
| 1445 | case BO_GE: return ">="; |
| 1446 | case BO_EQ: return "=="; |
| 1447 | case BO_NE: return "!="; |
| 1448 | case BO_And: return "&"; |
| 1449 | case BO_Xor: return "^"; |
| 1450 | case BO_Or: return "|"; |
| 1451 | case BO_LAnd: return "&&"; |
| 1452 | case BO_LOr: return "||"; |
| 1453 | case BO_Assign: return "="; |
| 1454 | case BO_MulAssign: return "*="; |
| 1455 | case BO_DivAssign: return "/="; |
| 1456 | case BO_RemAssign: return "%="; |
| 1457 | case BO_AddAssign: return "+="; |
| 1458 | case BO_SubAssign: return "-="; |
| 1459 | case BO_ShlAssign: return "<<="; |
| 1460 | case BO_ShrAssign: return ">>="; |
| 1461 | case BO_AndAssign: return "&="; |
| 1462 | case BO_XorAssign: return "^="; |
| 1463 | case BO_OrAssign: return "|="; |
| 1464 | case BO_Comma: return ","; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1465 | } |
Douglas Gregor | baf5348 | 2009-03-12 22:51:37 +0000 | [diff] [blame] | 1466 | |
David Blaikie | 3026348 | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 1467 | llvm_unreachable("Invalid OpCode!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1468 | } |
| 1469 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1470 | BinaryOperatorKind |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 1471 | BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) { |
| 1472 | switch (OO) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1473 | default: llvm_unreachable("Not an overloadable binary operator"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1474 | case OO_Plus: return BO_Add; |
| 1475 | case OO_Minus: return BO_Sub; |
| 1476 | case OO_Star: return BO_Mul; |
| 1477 | case OO_Slash: return BO_Div; |
| 1478 | case OO_Percent: return BO_Rem; |
| 1479 | case OO_Caret: return BO_Xor; |
| 1480 | case OO_Amp: return BO_And; |
| 1481 | case OO_Pipe: return BO_Or; |
| 1482 | case OO_Equal: return BO_Assign; |
| 1483 | case OO_Less: return BO_LT; |
| 1484 | case OO_Greater: return BO_GT; |
| 1485 | case OO_PlusEqual: return BO_AddAssign; |
| 1486 | case OO_MinusEqual: return BO_SubAssign; |
| 1487 | case OO_StarEqual: return BO_MulAssign; |
| 1488 | case OO_SlashEqual: return BO_DivAssign; |
| 1489 | case OO_PercentEqual: return BO_RemAssign; |
| 1490 | case OO_CaretEqual: return BO_XorAssign; |
| 1491 | case OO_AmpEqual: return BO_AndAssign; |
| 1492 | case OO_PipeEqual: return BO_OrAssign; |
| 1493 | case OO_LessLess: return BO_Shl; |
| 1494 | case OO_GreaterGreater: return BO_Shr; |
| 1495 | case OO_LessLessEqual: return BO_ShlAssign; |
| 1496 | case OO_GreaterGreaterEqual: return BO_ShrAssign; |
| 1497 | case OO_EqualEqual: return BO_EQ; |
| 1498 | case OO_ExclaimEqual: return BO_NE; |
| 1499 | case OO_LessEqual: return BO_LE; |
| 1500 | case OO_GreaterEqual: return BO_GE; |
| 1501 | case OO_AmpAmp: return BO_LAnd; |
| 1502 | case OO_PipePipe: return BO_LOr; |
| 1503 | case OO_Comma: return BO_Comma; |
| 1504 | case OO_ArrowStar: return BO_PtrMemI; |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 1505 | } |
| 1506 | } |
| 1507 | |
| 1508 | OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) { |
| 1509 | static const OverloadedOperatorKind OverOps[] = { |
| 1510 | /* .* Cannot be overloaded */OO_None, OO_ArrowStar, |
| 1511 | OO_Star, OO_Slash, OO_Percent, |
| 1512 | OO_Plus, OO_Minus, |
| 1513 | OO_LessLess, OO_GreaterGreater, |
| 1514 | OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual, |
| 1515 | OO_EqualEqual, OO_ExclaimEqual, |
| 1516 | OO_Amp, |
| 1517 | OO_Caret, |
| 1518 | OO_Pipe, |
| 1519 | OO_AmpAmp, |
| 1520 | OO_PipePipe, |
| 1521 | OO_Equal, OO_StarEqual, |
| 1522 | OO_SlashEqual, OO_PercentEqual, |
| 1523 | OO_PlusEqual, OO_MinusEqual, |
| 1524 | OO_LessLessEqual, OO_GreaterGreaterEqual, |
| 1525 | OO_AmpEqual, OO_CaretEqual, |
| 1526 | OO_PipeEqual, |
| 1527 | OO_Comma |
| 1528 | }; |
| 1529 | return OverOps[Opc]; |
| 1530 | } |
| 1531 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1532 | InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc, |
Chris Lattner | 418f6c7 | 2008-10-26 23:43:26 +0000 | [diff] [blame] | 1533 | Expr **initExprs, unsigned numInits, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1534 | SourceLocation rbraceloc) |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 1535 | : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false, |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 1536 | false, false), |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1537 | InitExprs(C, numInits), |
Sebastian Redl | 32cf1f2 | 2012-02-17 08:42:25 +0000 | [diff] [blame] | 1538 | LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0) |
| 1539 | { |
| 1540 | sawArrayRangeDesignator(false); |
| 1541 | setInitializesStdInitializerList(false); |
Ted Kremenek | ba7bc55 | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 1542 | for (unsigned I = 0; I != numInits; ++I) { |
| 1543 | if (initExprs[I]->isTypeDependent()) |
John McCall | 8e6285a | 2010-10-26 08:39:16 +0000 | [diff] [blame] | 1544 | ExprBits.TypeDependent = true; |
Ted Kremenek | ba7bc55 | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 1545 | if (initExprs[I]->isValueDependent()) |
John McCall | 8e6285a | 2010-10-26 08:39:16 +0000 | [diff] [blame] | 1546 | ExprBits.ValueDependent = true; |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 1547 | if (initExprs[I]->isInstantiationDependent()) |
| 1548 | ExprBits.InstantiationDependent = true; |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 1549 | if (initExprs[I]->containsUnexpandedParameterPack()) |
| 1550 | ExprBits.ContainsUnexpandedParameterPack = true; |
Douglas Gregor | 73460a3 | 2009-11-19 23:25:22 +0000 | [diff] [blame] | 1551 | } |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 1552 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1553 | InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits); |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 1554 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1555 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1556 | void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) { |
Ted Kremenek | ba7bc55 | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 1557 | if (NumInits > InitExprs.size()) |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1558 | InitExprs.reserve(C, NumInits); |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1559 | } |
| 1560 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1561 | void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) { |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1562 | InitExprs.resize(C, NumInits, 0); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1563 | } |
| 1564 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1565 | Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) { |
Ted Kremenek | ba7bc55 | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 1566 | if (Init >= InitExprs.size()) { |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1567 | InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0); |
Ted Kremenek | ba7bc55 | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 1568 | InitExprs.back() = expr; |
| 1569 | return 0; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1570 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1571 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1572 | Expr *Result = cast_or_null<Expr>(InitExprs[Init]); |
| 1573 | InitExprs[Init] = expr; |
| 1574 | return Result; |
| 1575 | } |
| 1576 | |
Argyrios Kyrtzidis | 3e8dc2a | 2011-04-21 20:03:38 +0000 | [diff] [blame] | 1577 | void InitListExpr::setArrayFiller(Expr *filler) { |
Argyrios Kyrtzidis | 21f77cd | 2011-10-21 23:02:22 +0000 | [diff] [blame] | 1578 | assert(!hasArrayFiller() && "Filler already set!"); |
Argyrios Kyrtzidis | 3e8dc2a | 2011-04-21 20:03:38 +0000 | [diff] [blame] | 1579 | ArrayFillerOrUnionFieldInit = filler; |
| 1580 | // Fill out any "holes" in the array due to designated initializers. |
| 1581 | Expr **inits = getInits(); |
| 1582 | for (unsigned i = 0, e = getNumInits(); i != e; ++i) |
| 1583 | if (inits[i] == 0) |
| 1584 | inits[i] = filler; |
| 1585 | } |
| 1586 | |
Richard Smith | fe58720 | 2012-04-15 02:50:59 +0000 | [diff] [blame] | 1587 | bool InitListExpr::isStringLiteralInit() const { |
| 1588 | if (getNumInits() != 1) |
| 1589 | return false; |
| 1590 | const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(getType()); |
| 1591 | if (!CAT || !CAT->getElementType()->isIntegerType()) |
| 1592 | return false; |
| 1593 | const Expr *Init = getInit(0)->IgnoreParenImpCasts(); |
| 1594 | return isa<StringLiteral>(Init) || isa<ObjCEncodeExpr>(Init); |
| 1595 | } |
| 1596 | |
Ted Kremenek | c4ba51f | 2010-11-09 02:11:40 +0000 | [diff] [blame] | 1597 | SourceRange InitListExpr::getSourceRange() const { |
| 1598 | if (SyntacticForm) |
| 1599 | return SyntacticForm->getSourceRange(); |
| 1600 | SourceLocation Beg = LBraceLoc, End = RBraceLoc; |
| 1601 | if (Beg.isInvalid()) { |
| 1602 | // Find the first non-null initializer. |
| 1603 | for (InitExprsTy::const_iterator I = InitExprs.begin(), |
| 1604 | E = InitExprs.end(); |
| 1605 | I != E; ++I) { |
| 1606 | if (Stmt *S = *I) { |
| 1607 | Beg = S->getLocStart(); |
| 1608 | break; |
| 1609 | } |
| 1610 | } |
| 1611 | } |
| 1612 | if (End.isInvalid()) { |
| 1613 | // Find the first non-null initializer from the end. |
| 1614 | for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(), |
| 1615 | E = InitExprs.rend(); |
| 1616 | I != E; ++I) { |
| 1617 | if (Stmt *S = *I) { |
| 1618 | End = S->getSourceRange().getEnd(); |
| 1619 | break; |
| 1620 | } |
| 1621 | } |
| 1622 | } |
| 1623 | return SourceRange(Beg, End); |
| 1624 | } |
| 1625 | |
Steve Naroff | bfdcae6 | 2008-09-04 15:31:07 +0000 | [diff] [blame] | 1626 | /// getFunctionType - Return the underlying function type for this block. |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 1627 | /// |
John McCall | a345edb | 2012-02-17 03:32:35 +0000 | [diff] [blame] | 1628 | const FunctionProtoType *BlockExpr::getFunctionType() const { |
| 1629 | // The block pointer is never sugared, but the function type might be. |
| 1630 | return cast<BlockPointerType>(getType()) |
| 1631 | ->getPointeeType()->castAs<FunctionProtoType>(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 1632 | } |
| 1633 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1634 | SourceLocation BlockExpr::getCaretLocation() const { |
| 1635 | return TheBlock->getCaretLocation(); |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 1636 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1637 | const Stmt *BlockExpr::getBody() const { |
Douglas Gregor | 7297134 | 2009-04-18 00:02:19 +0000 | [diff] [blame] | 1638 | return TheBlock->getBody(); |
| 1639 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1640 | Stmt *BlockExpr::getBody() { |
| 1641 | return TheBlock->getBody(); |
Douglas Gregor | 7297134 | 2009-04-18 00:02:19 +0000 | [diff] [blame] | 1642 | } |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 1643 | |
| 1644 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1645 | //===----------------------------------------------------------------------===// |
| 1646 | // Generic Expression Routines |
| 1647 | //===----------------------------------------------------------------------===// |
| 1648 | |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1649 | /// isUnusedResultAWarning - Return true if this immediate expression should |
| 1650 | /// be warned about if the result is unused. If so, fill in Loc and Ranges |
| 1651 | /// with location to warn on and the source range[s] to report with the |
| 1652 | /// warning. |
Eli Friedman | a611506 | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 1653 | bool Expr::isUnusedResultAWarning(const Expr *&WarnE, SourceLocation &Loc, |
| 1654 | SourceRange &R1, SourceRange &R2, |
| 1655 | ASTContext &Ctx) const { |
Anders Carlsson | ffce2df | 2009-05-15 23:10:19 +0000 | [diff] [blame] | 1656 | // Don't warn if the expr is type dependent. The type could end up |
| 1657 | // instantiating to void. |
| 1658 | if (isTypeDependent()) |
| 1659 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1660 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1661 | switch (getStmtClass()) { |
| 1662 | default: |
John McCall | 0faede6 | 2010-03-12 07:11:26 +0000 | [diff] [blame] | 1663 | if (getType()->isVoidType()) |
| 1664 | return false; |
Eli Friedman | a611506 | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 1665 | WarnE = this; |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1666 | Loc = getExprLoc(); |
| 1667 | R1 = getSourceRange(); |
| 1668 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1669 | case ParenExprClass: |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1670 | return cast<ParenExpr>(this)->getSubExpr()-> |
Eli Friedman | a611506 | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 1671 | isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx); |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1672 | case GenericSelectionExprClass: |
| 1673 | return cast<GenericSelectionExpr>(this)->getResultExpr()-> |
Eli Friedman | a611506 | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 1674 | isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1675 | case UnaryOperatorClass: { |
| 1676 | const UnaryOperator *UO = cast<UnaryOperator>(this); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1677 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1678 | switch (UO->getOpcode()) { |
Eli Friedman | a611506 | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 1679 | case UO_Plus: |
| 1680 | case UO_Minus: |
| 1681 | case UO_AddrOf: |
| 1682 | case UO_Not: |
| 1683 | case UO_LNot: |
| 1684 | case UO_Deref: |
| 1685 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1686 | case UO_PostInc: |
| 1687 | case UO_PostDec: |
| 1688 | case UO_PreInc: |
| 1689 | case UO_PreDec: // ++/-- |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1690 | return false; // Not a warning. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1691 | case UO_Real: |
| 1692 | case UO_Imag: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1693 | // accessing a piece of a volatile complex is a side-effect. |
Mike Stump | df317bf | 2009-11-03 23:25:48 +0000 | [diff] [blame] | 1694 | if (Ctx.getCanonicalType(UO->getSubExpr()->getType()) |
| 1695 | .isVolatileQualified()) |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1696 | return false; |
| 1697 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1698 | case UO_Extension: |
Eli Friedman | a611506 | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 1699 | return UO->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1700 | } |
Eli Friedman | a611506 | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 1701 | WarnE = this; |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1702 | Loc = UO->getOperatorLoc(); |
| 1703 | R1 = UO->getSubExpr()->getSourceRange(); |
| 1704 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1705 | } |
Chris Lattner | e7716e6 | 2007-12-01 06:07:34 +0000 | [diff] [blame] | 1706 | case BinaryOperatorClass: { |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1707 | const BinaryOperator *BO = cast<BinaryOperator>(this); |
Ted Kremenek | c46a246 | 2010-04-07 18:49:21 +0000 | [diff] [blame] | 1708 | switch (BO->getOpcode()) { |
| 1709 | default: |
| 1710 | break; |
Argyrios Kyrtzidis | 2597345 | 2010-06-30 10:53:14 +0000 | [diff] [blame] | 1711 | // Consider the RHS of comma for side effects. LHS was checked by |
| 1712 | // Sema::CheckCommaOperands. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1713 | case BO_Comma: |
Ted Kremenek | c46a246 | 2010-04-07 18:49:21 +0000 | [diff] [blame] | 1714 | // ((foo = <blah>), 0) is an idiom for hiding the result (and |
| 1715 | // lvalue-ness) of an assignment written in a macro. |
| 1716 | if (IntegerLiteral *IE = |
| 1717 | dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens())) |
| 1718 | if (IE->getValue() == 0) |
| 1719 | return false; |
Eli Friedman | a611506 | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 1720 | return BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx); |
Argyrios Kyrtzidis | 2597345 | 2010-06-30 10:53:14 +0000 | [diff] [blame] | 1721 | // Consider '||', '&&' to have side effects if the LHS or RHS does. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1722 | case BO_LAnd: |
| 1723 | case BO_LOr: |
Eli Friedman | a611506 | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 1724 | if (!BO->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx) || |
| 1725 | !BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx)) |
Argyrios Kyrtzidis | 2597345 | 2010-06-30 10:53:14 +0000 | [diff] [blame] | 1726 | return false; |
| 1727 | break; |
John McCall | bf0ee35 | 2010-02-16 04:10:53 +0000 | [diff] [blame] | 1728 | } |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1729 | if (BO->isAssignmentOp()) |
| 1730 | return false; |
Eli Friedman | a611506 | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 1731 | WarnE = this; |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1732 | Loc = BO->getOperatorLoc(); |
| 1733 | R1 = BO->getLHS()->getSourceRange(); |
| 1734 | R2 = BO->getRHS()->getSourceRange(); |
| 1735 | return true; |
Chris Lattner | e7716e6 | 2007-12-01 06:07:34 +0000 | [diff] [blame] | 1736 | } |
Chris Lattner | eb14fe8 | 2007-08-25 02:00:02 +0000 | [diff] [blame] | 1737 | case CompoundAssignOperatorClass: |
Douglas Gregor | c6dfe19 | 2010-05-08 22:41:50 +0000 | [diff] [blame] | 1738 | case VAArgExprClass: |
Eli Friedman | 276b061 | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1739 | case AtomicExprClass: |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1740 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1741 | |
Fariborz Jahanian | ab38e4b | 2007-12-01 19:58:28 +0000 | [diff] [blame] | 1742 | case ConditionalOperatorClass: { |
Ted Kremenek | fb7cb35 | 2011-03-01 20:34:48 +0000 | [diff] [blame] | 1743 | // If only one of the LHS or RHS is a warning, the operator might |
| 1744 | // be being used for control flow. Only warn if both the LHS and |
| 1745 | // RHS are warnings. |
Fariborz Jahanian | ab38e4b | 2007-12-01 19:58:28 +0000 | [diff] [blame] | 1746 | const ConditionalOperator *Exp = cast<ConditionalOperator>(this); |
Eli Friedman | a611506 | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 1747 | if (!Exp->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx)) |
Ted Kremenek | fb7cb35 | 2011-03-01 20:34:48 +0000 | [diff] [blame] | 1748 | return false; |
| 1749 | if (!Exp->getLHS()) |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1750 | return true; |
Eli Friedman | a611506 | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 1751 | return Exp->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx); |
Fariborz Jahanian | ab38e4b | 2007-12-01 19:58:28 +0000 | [diff] [blame] | 1752 | } |
| 1753 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1754 | case MemberExprClass: |
Eli Friedman | a611506 | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 1755 | WarnE = this; |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1756 | Loc = cast<MemberExpr>(this)->getMemberLoc(); |
| 1757 | R1 = SourceRange(Loc, Loc); |
| 1758 | R2 = cast<MemberExpr>(this)->getBase()->getSourceRange(); |
| 1759 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1760 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1761 | case ArraySubscriptExprClass: |
Eli Friedman | a611506 | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 1762 | WarnE = this; |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1763 | Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc(); |
| 1764 | R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange(); |
| 1765 | R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange(); |
| 1766 | return true; |
Eli Friedman | 211f6ad | 2008-05-27 15:24:04 +0000 | [diff] [blame] | 1767 | |
Chandler Carruth | 9b10683 | 2011-08-17 09:49:44 +0000 | [diff] [blame] | 1768 | case CXXOperatorCallExprClass: { |
| 1769 | // We warn about operator== and operator!= even when user-defined operator |
| 1770 | // overloads as there is no reasonable way to define these such that they |
| 1771 | // have non-trivial, desirable side-effects. See the -Wunused-comparison |
| 1772 | // warning: these operators are commonly typo'ed, and so warning on them |
| 1773 | // provides additional value as well. If this list is updated, |
| 1774 | // DiagnoseUnusedComparison should be as well. |
| 1775 | const CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(this); |
| 1776 | if (Op->getOperator() == OO_EqualEqual || |
Matt Beaumont-Gay | 6e52183 | 2011-09-19 18:51:20 +0000 | [diff] [blame] | 1777 | Op->getOperator() == OO_ExclaimEqual) { |
Eli Friedman | a611506 | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 1778 | WarnE = this; |
Matt Beaumont-Gay | 6e52183 | 2011-09-19 18:51:20 +0000 | [diff] [blame] | 1779 | Loc = Op->getOperatorLoc(); |
| 1780 | R1 = Op->getSourceRange(); |
Chandler Carruth | 9b10683 | 2011-08-17 09:49:44 +0000 | [diff] [blame] | 1781 | return true; |
Matt Beaumont-Gay | 6e52183 | 2011-09-19 18:51:20 +0000 | [diff] [blame] | 1782 | } |
Chandler Carruth | 9b10683 | 2011-08-17 09:49:44 +0000 | [diff] [blame] | 1783 | |
| 1784 | // Fallthrough for generic call handling. |
| 1785 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1786 | case CallExprClass: |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 1787 | case CXXMemberCallExprClass: |
| 1788 | case UserDefinedLiteralClass: { |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1789 | // If this is a direct call, get the callee. |
| 1790 | const CallExpr *CE = cast<CallExpr>(this); |
Nuno Lopes | d20254f | 2009-12-20 23:11:08 +0000 | [diff] [blame] | 1791 | if (const Decl *FD = CE->getCalleeDecl()) { |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1792 | // If the callee has attribute pure, const, or warn_unused_result, warn |
| 1793 | // about it. void foo() { strlen("bar"); } should warn. |
Chris Lattner | bc8d42c | 2009-10-13 04:53:48 +0000 | [diff] [blame] | 1794 | // |
| 1795 | // Note: If new cases are added here, DiagnoseUnusedExprResult should be |
| 1796 | // updated to match for QoI. |
| 1797 | if (FD->getAttr<WarnUnusedResultAttr>() || |
| 1798 | FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) { |
Eli Friedman | a611506 | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 1799 | WarnE = this; |
Chris Lattner | bc8d42c | 2009-10-13 04:53:48 +0000 | [diff] [blame] | 1800 | Loc = CE->getCallee()->getLocStart(); |
| 1801 | R1 = CE->getCallee()->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1802 | |
Chris Lattner | bc8d42c | 2009-10-13 04:53:48 +0000 | [diff] [blame] | 1803 | if (unsigned NumArgs = CE->getNumArgs()) |
| 1804 | R2 = SourceRange(CE->getArg(0)->getLocStart(), |
| 1805 | CE->getArg(NumArgs-1)->getLocEnd()); |
| 1806 | return true; |
| 1807 | } |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1808 | } |
| 1809 | return false; |
| 1810 | } |
Anders Carlsson | 58beed9 | 2009-11-17 17:11:23 +0000 | [diff] [blame] | 1811 | |
| 1812 | case CXXTemporaryObjectExprClass: |
| 1813 | case CXXConstructExprClass: |
| 1814 | return false; |
| 1815 | |
Fariborz Jahanian | f031774 | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 1816 | case ObjCMessageExprClass: { |
| 1817 | const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1818 | if (Ctx.getLangOpts().ObjCAutoRefCount && |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1819 | ME->isInstanceMessage() && |
| 1820 | !ME->getType()->isVoidType() && |
| 1821 | ME->getSelector().getIdentifierInfoForSlot(0) && |
| 1822 | ME->getSelector().getIdentifierInfoForSlot(0) |
| 1823 | ->getName().startswith("init")) { |
Eli Friedman | a611506 | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 1824 | WarnE = this; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1825 | Loc = getExprLoc(); |
| 1826 | R1 = ME->getSourceRange(); |
| 1827 | return true; |
| 1828 | } |
| 1829 | |
Fariborz Jahanian | f031774 | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 1830 | const ObjCMethodDecl *MD = ME->getMethodDecl(); |
| 1831 | if (MD && MD->getAttr<WarnUnusedResultAttr>()) { |
Eli Friedman | a611506 | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 1832 | WarnE = this; |
Fariborz Jahanian | f031774 | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 1833 | Loc = getExprLoc(); |
| 1834 | return true; |
| 1835 | } |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1836 | return false; |
Fariborz Jahanian | f031774 | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 1837 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1838 | |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 1839 | case ObjCPropertyRefExprClass: |
Eli Friedman | a611506 | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 1840 | WarnE = this; |
Chris Lattner | 5e94a0d | 2009-08-16 16:51:50 +0000 | [diff] [blame] | 1841 | Loc = getExprLoc(); |
| 1842 | R1 = getSourceRange(); |
Chris Lattner | a50089e | 2009-08-16 16:45:18 +0000 | [diff] [blame] | 1843 | return true; |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 1844 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1845 | case PseudoObjectExprClass: { |
| 1846 | const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this); |
| 1847 | |
| 1848 | // Only complain about things that have the form of a getter. |
| 1849 | if (isa<UnaryOperator>(PO->getSyntacticForm()) || |
| 1850 | isa<BinaryOperator>(PO->getSyntacticForm())) |
| 1851 | return false; |
| 1852 | |
Eli Friedman | a611506 | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 1853 | WarnE = this; |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 1854 | Loc = getExprLoc(); |
| 1855 | R1 = getSourceRange(); |
| 1856 | return true; |
| 1857 | } |
| 1858 | |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 1859 | case StmtExprClass: { |
| 1860 | // Statement exprs don't logically have side effects themselves, but are |
| 1861 | // sometimes used in macros in ways that give them a type that is unused. |
| 1862 | // For example ({ blah; foo(); }) will end up with a type if foo has a type. |
| 1863 | // however, if the result of the stmt expr is dead, we don't want to emit a |
| 1864 | // warning. |
| 1865 | const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt(); |
Argyrios Kyrtzidis | d2827af | 2010-09-19 21:21:10 +0000 | [diff] [blame] | 1866 | if (!CS->body_empty()) { |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 1867 | if (const Expr *E = dyn_cast<Expr>(CS->body_back())) |
Eli Friedman | a611506 | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 1868 | return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx); |
Argyrios Kyrtzidis | d2827af | 2010-09-19 21:21:10 +0000 | [diff] [blame] | 1869 | if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back())) |
| 1870 | if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt())) |
Eli Friedman | a611506 | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 1871 | return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx); |
Argyrios Kyrtzidis | d2827af | 2010-09-19 21:21:10 +0000 | [diff] [blame] | 1872 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1873 | |
John McCall | 0faede6 | 2010-03-12 07:11:26 +0000 | [diff] [blame] | 1874 | if (getType()->isVoidType()) |
| 1875 | return false; |
Eli Friedman | a611506 | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 1876 | WarnE = this; |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1877 | Loc = cast<StmtExpr>(this)->getLParenLoc(); |
| 1878 | R1 = getSourceRange(); |
| 1879 | return true; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 1880 | } |
Eli Friedman | a611506 | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 1881 | case CStyleCastExprClass: { |
Eli Friedman | 4059da8 | 2012-05-24 21:05:41 +0000 | [diff] [blame] | 1882 | // Ignore an explicit cast to void unless the operand is a non-trivial |
Eli Friedman | a611506 | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 1883 | // volatile lvalue. |
Eli Friedman | 4059da8 | 2012-05-24 21:05:41 +0000 | [diff] [blame] | 1884 | const CastExpr *CE = cast<CastExpr>(this); |
Eli Friedman | a611506 | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 1885 | if (CE->getCastKind() == CK_ToVoid) { |
| 1886 | if (CE->getSubExpr()->isGLValue() && |
Eli Friedman | 4059da8 | 2012-05-24 21:05:41 +0000 | [diff] [blame] | 1887 | CE->getSubExpr()->getType().isVolatileQualified()) { |
| 1888 | const DeclRefExpr *DRE = |
| 1889 | dyn_cast<DeclRefExpr>(CE->getSubExpr()->IgnoreParens()); |
| 1890 | if (!(DRE && isa<VarDecl>(DRE->getDecl()) && |
| 1891 | cast<VarDecl>(DRE->getDecl())->hasLocalStorage())) { |
| 1892 | return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, |
| 1893 | R1, R2, Ctx); |
| 1894 | } |
| 1895 | } |
Chris Lattner | fb84664 | 2009-07-28 18:25:28 +0000 | [diff] [blame] | 1896 | return false; |
Eli Friedman | a611506 | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 1897 | } |
Eli Friedman | 4059da8 | 2012-05-24 21:05:41 +0000 | [diff] [blame] | 1898 | |
Eli Friedman | a611506 | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 1899 | // If this is a cast to a constructor conversion, check the operand. |
Anders Carlsson | 58beed9 | 2009-11-17 17:11:23 +0000 | [diff] [blame] | 1900 | // Otherwise, the result of the cast is unused. |
Eli Friedman | a611506 | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 1901 | if (CE->getCastKind() == CK_ConstructorConversion) |
| 1902 | return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx); |
Eli Friedman | 4059da8 | 2012-05-24 21:05:41 +0000 | [diff] [blame] | 1903 | |
Eli Friedman | a611506 | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 1904 | WarnE = this; |
Eli Friedman | 4059da8 | 2012-05-24 21:05:41 +0000 | [diff] [blame] | 1905 | if (const CXXFunctionalCastExpr *CXXCE = |
| 1906 | dyn_cast<CXXFunctionalCastExpr>(this)) { |
| 1907 | Loc = CXXCE->getTypeBeginLoc(); |
| 1908 | R1 = CXXCE->getSubExpr()->getSourceRange(); |
| 1909 | } else { |
| 1910 | const CStyleCastExpr *CStyleCE = cast<CStyleCastExpr>(this); |
| 1911 | Loc = CStyleCE->getLParenLoc(); |
| 1912 | R1 = CStyleCE->getSubExpr()->getSourceRange(); |
| 1913 | } |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1914 | return true; |
Anders Carlsson | 58beed9 | 2009-11-17 17:11:23 +0000 | [diff] [blame] | 1915 | } |
Eli Friedman | a611506 | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 1916 | case ImplicitCastExprClass: { |
| 1917 | const CastExpr *ICE = cast<ImplicitCastExpr>(this); |
Eli Friedman | 4be1f47 | 2008-05-19 21:24:43 +0000 | [diff] [blame] | 1918 | |
Eli Friedman | a611506 | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 1919 | // lvalue-to-rvalue conversion on a volatile lvalue is a side-effect. |
| 1920 | if (ICE->getCastKind() == CK_LValueToRValue && |
| 1921 | ICE->getSubExpr()->getType().isVolatileQualified()) |
| 1922 | return false; |
| 1923 | |
| 1924 | return ICE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx); |
| 1925 | } |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1926 | case CXXDefaultArgExprClass: |
Mike Stump | df317bf | 2009-11-03 23:25:48 +0000 | [diff] [blame] | 1927 | return (cast<CXXDefaultArgExpr>(this) |
Eli Friedman | a611506 | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 1928 | ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx)); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1929 | |
| 1930 | case CXXNewExprClass: |
| 1931 | // FIXME: In theory, there might be new expressions that don't have side |
| 1932 | // effects (e.g. a placement new with an uninitialized POD). |
| 1933 | case CXXDeleteExprClass: |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1934 | return false; |
Anders Carlsson | 2d46eb2 | 2009-08-16 04:11:06 +0000 | [diff] [blame] | 1935 | case CXXBindTemporaryExprClass: |
Mike Stump | df317bf | 2009-11-03 23:25:48 +0000 | [diff] [blame] | 1936 | return (cast<CXXBindTemporaryExpr>(this) |
Eli Friedman | a611506 | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 1937 | ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx)); |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 1938 | case ExprWithCleanupsClass: |
| 1939 | return (cast<ExprWithCleanups>(this) |
Eli Friedman | a611506 | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 1940 | ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx)); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1941 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1942 | } |
| 1943 | |
Fariborz Jahanian | 44baa8a | 2009-02-22 18:40:18 +0000 | [diff] [blame] | 1944 | /// isOBJCGCCandidate - Check if an expression is objc gc'able. |
Fariborz Jahanian | 7f4f86a | 2009-09-08 23:38:54 +0000 | [diff] [blame] | 1945 | /// returns true, if it is; false otherwise. |
Fariborz Jahanian | 102e390 | 2009-06-01 21:29:32 +0000 | [diff] [blame] | 1946 | bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const { |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1947 | const Expr *E = IgnoreParens(); |
| 1948 | switch (E->getStmtClass()) { |
Fariborz Jahanian | 44baa8a | 2009-02-22 18:40:18 +0000 | [diff] [blame] | 1949 | default: |
| 1950 | return false; |
| 1951 | case ObjCIvarRefExprClass: |
| 1952 | return true; |
Fariborz Jahanian | 207c521 | 2009-02-23 18:59:50 +0000 | [diff] [blame] | 1953 | case Expr::UnaryOperatorClass: |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1954 | return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx); |
Fariborz Jahanian | 44baa8a | 2009-02-22 18:40:18 +0000 | [diff] [blame] | 1955 | case ImplicitCastExprClass: |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1956 | return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx); |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 1957 | case MaterializeTemporaryExprClass: |
| 1958 | return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr() |
| 1959 | ->isOBJCGCCandidate(Ctx); |
Fariborz Jahanian | 06b8912 | 2009-05-05 23:28:21 +0000 | [diff] [blame] | 1960 | case CStyleCastExprClass: |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1961 | return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx); |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 1962 | case DeclRefExprClass: { |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 1963 | const Decl *D = cast<DeclRefExpr>(E)->getDecl(); |
Fariborz Jahanian | e3f8349 | 2011-09-23 18:57:30 +0000 | [diff] [blame] | 1964 | |
Fariborz Jahanian | 102e390 | 2009-06-01 21:29:32 +0000 | [diff] [blame] | 1965 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 1966 | if (VD->hasGlobalStorage()) |
| 1967 | return true; |
| 1968 | QualType T = VD->getType(); |
Fariborz Jahanian | 59a53fa | 2009-09-16 18:09:18 +0000 | [diff] [blame] | 1969 | // dereferencing to a pointer is always a gc'able candidate, |
| 1970 | // unless it is __weak. |
Daniel Dunbar | 7e88a60 | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 1971 | return T->isPointerType() && |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1972 | (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak); |
Fariborz Jahanian | 102e390 | 2009-06-01 21:29:32 +0000 | [diff] [blame] | 1973 | } |
Fariborz Jahanian | 44baa8a | 2009-02-22 18:40:18 +0000 | [diff] [blame] | 1974 | return false; |
| 1975 | } |
Douglas Gregor | 83f6faf | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 1976 | case MemberExprClass: { |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1977 | const MemberExpr *M = cast<MemberExpr>(E); |
Fariborz Jahanian | 102e390 | 2009-06-01 21:29:32 +0000 | [diff] [blame] | 1978 | return M->getBase()->isOBJCGCCandidate(Ctx); |
Fariborz Jahanian | 44baa8a | 2009-02-22 18:40:18 +0000 | [diff] [blame] | 1979 | } |
| 1980 | case ArraySubscriptExprClass: |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1981 | return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx); |
Fariborz Jahanian | 44baa8a | 2009-02-22 18:40:18 +0000 | [diff] [blame] | 1982 | } |
| 1983 | } |
Sebastian Redl | 369e51f | 2010-09-10 20:55:33 +0000 | [diff] [blame] | 1984 | |
Argyrios Kyrtzidis | 11ab790 | 2010-11-01 18:49:26 +0000 | [diff] [blame] | 1985 | bool Expr::isBoundMemberFunction(ASTContext &Ctx) const { |
| 1986 | if (isTypeDependent()) |
| 1987 | return false; |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1988 | return ClassifyLValue(Ctx) == Expr::LV_MemberFunction; |
Argyrios Kyrtzidis | 11ab790 | 2010-11-01 18:49:26 +0000 | [diff] [blame] | 1989 | } |
| 1990 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 1991 | QualType Expr::findBoundMemberType(const Expr *expr) { |
John McCall | e0a22d0 | 2011-10-18 21:02:43 +0000 | [diff] [blame] | 1992 | assert(expr->hasPlaceholderType(BuiltinType::BoundMember)); |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 1993 | |
| 1994 | // Bound member expressions are always one of these possibilities: |
| 1995 | // x->m x.m x->*y x.*y |
| 1996 | // (possibly parenthesized) |
| 1997 | |
| 1998 | expr = expr->IgnoreParens(); |
| 1999 | if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) { |
| 2000 | assert(isa<CXXMethodDecl>(mem->getMemberDecl())); |
| 2001 | return mem->getMemberDecl()->getType(); |
| 2002 | } |
| 2003 | |
| 2004 | if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) { |
| 2005 | QualType type = op->getRHS()->getType()->castAs<MemberPointerType>() |
| 2006 | ->getPointeeType(); |
| 2007 | assert(type->isFunctionType()); |
| 2008 | return type; |
| 2009 | } |
| 2010 | |
| 2011 | assert(isa<UnresolvedMemberExpr>(expr)); |
| 2012 | return QualType(); |
| 2013 | } |
| 2014 | |
Ted Kremenek | 4e99a5f | 2008-01-17 16:57:34 +0000 | [diff] [blame] | 2015 | Expr* Expr::IgnoreParens() { |
| 2016 | Expr* E = this; |
Abramo Bagnara | b9eb35c | 2010-10-15 07:51:18 +0000 | [diff] [blame] | 2017 | while (true) { |
| 2018 | if (ParenExpr* P = dyn_cast<ParenExpr>(E)) { |
| 2019 | E = P->getSubExpr(); |
| 2020 | continue; |
| 2021 | } |
| 2022 | if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) { |
| 2023 | if (P->getOpcode() == UO_Extension) { |
| 2024 | E = P->getSubExpr(); |
| 2025 | continue; |
| 2026 | } |
| 2027 | } |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 2028 | if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) { |
| 2029 | if (!P->isResultDependent()) { |
| 2030 | E = P->getResultExpr(); |
| 2031 | continue; |
| 2032 | } |
| 2033 | } |
Abramo Bagnara | b9eb35c | 2010-10-15 07:51:18 +0000 | [diff] [blame] | 2034 | return E; |
| 2035 | } |
Ted Kremenek | 4e99a5f | 2008-01-17 16:57:34 +0000 | [diff] [blame] | 2036 | } |
| 2037 | |
Chris Lattner | 56f3494 | 2008-02-13 01:02:39 +0000 | [diff] [blame] | 2038 | /// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr |
| 2039 | /// or CastExprs or ImplicitCastExprs, returning their operand. |
| 2040 | Expr *Expr::IgnoreParenCasts() { |
| 2041 | Expr *E = this; |
| 2042 | while (true) { |
Abramo Bagnara | b9eb35c | 2010-10-15 07:51:18 +0000 | [diff] [blame] | 2043 | if (ParenExpr* P = dyn_cast<ParenExpr>(E)) { |
Chris Lattner | 56f3494 | 2008-02-13 01:02:39 +0000 | [diff] [blame] | 2044 | E = P->getSubExpr(); |
Abramo Bagnara | b9eb35c | 2010-10-15 07:51:18 +0000 | [diff] [blame] | 2045 | continue; |
| 2046 | } |
| 2047 | if (CastExpr *P = dyn_cast<CastExpr>(E)) { |
Chris Lattner | 56f3494 | 2008-02-13 01:02:39 +0000 | [diff] [blame] | 2048 | E = P->getSubExpr(); |
Abramo Bagnara | b9eb35c | 2010-10-15 07:51:18 +0000 | [diff] [blame] | 2049 | continue; |
| 2050 | } |
| 2051 | if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) { |
| 2052 | if (P->getOpcode() == UO_Extension) { |
| 2053 | E = P->getSubExpr(); |
| 2054 | continue; |
| 2055 | } |
| 2056 | } |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 2057 | if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) { |
| 2058 | if (!P->isResultDependent()) { |
| 2059 | E = P->getResultExpr(); |
| 2060 | continue; |
| 2061 | } |
| 2062 | } |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 2063 | if (MaterializeTemporaryExpr *Materialize |
| 2064 | = dyn_cast<MaterializeTemporaryExpr>(E)) { |
| 2065 | E = Materialize->GetTemporaryExpr(); |
| 2066 | continue; |
| 2067 | } |
Douglas Gregor | c0244c5 | 2011-09-08 17:56:33 +0000 | [diff] [blame] | 2068 | if (SubstNonTypeTemplateParmExpr *NTTP |
| 2069 | = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) { |
| 2070 | E = NTTP->getReplacement(); |
| 2071 | continue; |
| 2072 | } |
Abramo Bagnara | b9eb35c | 2010-10-15 07:51:18 +0000 | [diff] [blame] | 2073 | return E; |
Chris Lattner | 56f3494 | 2008-02-13 01:02:39 +0000 | [diff] [blame] | 2074 | } |
| 2075 | } |
| 2076 | |
John McCall | 9c5d70c | 2010-12-04 08:24:19 +0000 | [diff] [blame] | 2077 | /// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue |
| 2078 | /// casts. This is intended purely as a temporary workaround for code |
| 2079 | /// that hasn't yet been rewritten to do the right thing about those |
| 2080 | /// casts, and may disappear along with the last internal use. |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 2081 | Expr *Expr::IgnoreParenLValueCasts() { |
| 2082 | Expr *E = this; |
John McCall | 9c5d70c | 2010-12-04 08:24:19 +0000 | [diff] [blame] | 2083 | while (true) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 2084 | if (ParenExpr *P = dyn_cast<ParenExpr>(E)) { |
| 2085 | E = P->getSubExpr(); |
| 2086 | continue; |
John McCall | 9c5d70c | 2010-12-04 08:24:19 +0000 | [diff] [blame] | 2087 | } else if (CastExpr *P = dyn_cast<CastExpr>(E)) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 2088 | if (P->getCastKind() == CK_LValueToRValue) { |
| 2089 | E = P->getSubExpr(); |
| 2090 | continue; |
| 2091 | } |
John McCall | 9c5d70c | 2010-12-04 08:24:19 +0000 | [diff] [blame] | 2092 | } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) { |
| 2093 | if (P->getOpcode() == UO_Extension) { |
| 2094 | E = P->getSubExpr(); |
| 2095 | continue; |
| 2096 | } |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 2097 | } else if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) { |
| 2098 | if (!P->isResultDependent()) { |
| 2099 | E = P->getResultExpr(); |
| 2100 | continue; |
| 2101 | } |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 2102 | } else if (MaterializeTemporaryExpr *Materialize |
| 2103 | = dyn_cast<MaterializeTemporaryExpr>(E)) { |
| 2104 | E = Materialize->GetTemporaryExpr(); |
| 2105 | continue; |
Douglas Gregor | c0244c5 | 2011-09-08 17:56:33 +0000 | [diff] [blame] | 2106 | } else if (SubstNonTypeTemplateParmExpr *NTTP |
| 2107 | = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) { |
| 2108 | E = NTTP->getReplacement(); |
| 2109 | continue; |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 2110 | } |
| 2111 | break; |
| 2112 | } |
| 2113 | return E; |
| 2114 | } |
| 2115 | |
John McCall | 2fc46bf | 2010-05-05 22:59:52 +0000 | [diff] [blame] | 2116 | Expr *Expr::IgnoreParenImpCasts() { |
| 2117 | Expr *E = this; |
| 2118 | while (true) { |
Abramo Bagnara | b9eb35c | 2010-10-15 07:51:18 +0000 | [diff] [blame] | 2119 | if (ParenExpr *P = dyn_cast<ParenExpr>(E)) { |
John McCall | 2fc46bf | 2010-05-05 22:59:52 +0000 | [diff] [blame] | 2120 | E = P->getSubExpr(); |
Abramo Bagnara | b9eb35c | 2010-10-15 07:51:18 +0000 | [diff] [blame] | 2121 | continue; |
| 2122 | } |
| 2123 | if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | 2fc46bf | 2010-05-05 22:59:52 +0000 | [diff] [blame] | 2124 | E = P->getSubExpr(); |
Abramo Bagnara | b9eb35c | 2010-10-15 07:51:18 +0000 | [diff] [blame] | 2125 | continue; |
| 2126 | } |
| 2127 | if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) { |
| 2128 | if (P->getOpcode() == UO_Extension) { |
| 2129 | E = P->getSubExpr(); |
| 2130 | continue; |
| 2131 | } |
| 2132 | } |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 2133 | if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) { |
| 2134 | if (!P->isResultDependent()) { |
| 2135 | E = P->getResultExpr(); |
| 2136 | continue; |
| 2137 | } |
| 2138 | } |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 2139 | if (MaterializeTemporaryExpr *Materialize |
| 2140 | = dyn_cast<MaterializeTemporaryExpr>(E)) { |
| 2141 | E = Materialize->GetTemporaryExpr(); |
| 2142 | continue; |
| 2143 | } |
Douglas Gregor | c0244c5 | 2011-09-08 17:56:33 +0000 | [diff] [blame] | 2144 | if (SubstNonTypeTemplateParmExpr *NTTP |
| 2145 | = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) { |
| 2146 | E = NTTP->getReplacement(); |
| 2147 | continue; |
| 2148 | } |
Abramo Bagnara | b9eb35c | 2010-10-15 07:51:18 +0000 | [diff] [blame] | 2149 | return E; |
John McCall | 2fc46bf | 2010-05-05 22:59:52 +0000 | [diff] [blame] | 2150 | } |
| 2151 | } |
| 2152 | |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 2153 | Expr *Expr::IgnoreConversionOperator() { |
| 2154 | if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) { |
Chandler Carruth | 14d251c | 2011-06-21 17:22:09 +0000 | [diff] [blame] | 2155 | if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl())) |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 2156 | return MCE->getImplicitObjectArgument(); |
| 2157 | } |
| 2158 | return this; |
| 2159 | } |
| 2160 | |
Chris Lattner | ecdd841 | 2009-03-13 17:28:01 +0000 | [diff] [blame] | 2161 | /// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the |
| 2162 | /// value (including ptr->int casts of the same size). Strip off any |
| 2163 | /// ParenExpr or CastExprs, returning their operand. |
| 2164 | Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) { |
| 2165 | Expr *E = this; |
| 2166 | while (true) { |
| 2167 | if (ParenExpr *P = dyn_cast<ParenExpr>(E)) { |
| 2168 | E = P->getSubExpr(); |
| 2169 | continue; |
| 2170 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2171 | |
Chris Lattner | ecdd841 | 2009-03-13 17:28:01 +0000 | [diff] [blame] | 2172 | if (CastExpr *P = dyn_cast<CastExpr>(E)) { |
| 2173 | // 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] | 2174 | // 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] | 2175 | Expr *SE = P->getSubExpr(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2176 | |
Chris Lattner | ecdd841 | 2009-03-13 17:28:01 +0000 | [diff] [blame] | 2177 | if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) { |
| 2178 | E = SE; |
| 2179 | continue; |
| 2180 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2181 | |
Abramo Bagnara | b9eb35c | 2010-10-15 07:51:18 +0000 | [diff] [blame] | 2182 | if ((E->getType()->isPointerType() || |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 2183 | E->getType()->isIntegralType(Ctx)) && |
Abramo Bagnara | b9eb35c | 2010-10-15 07:51:18 +0000 | [diff] [blame] | 2184 | (SE->getType()->isPointerType() || |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 2185 | SE->getType()->isIntegralType(Ctx)) && |
Chris Lattner | ecdd841 | 2009-03-13 17:28:01 +0000 | [diff] [blame] | 2186 | Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) { |
| 2187 | E = SE; |
| 2188 | continue; |
| 2189 | } |
| 2190 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2191 | |
Abramo Bagnara | b9eb35c | 2010-10-15 07:51:18 +0000 | [diff] [blame] | 2192 | if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) { |
| 2193 | if (P->getOpcode() == UO_Extension) { |
| 2194 | E = P->getSubExpr(); |
| 2195 | continue; |
| 2196 | } |
| 2197 | } |
| 2198 | |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 2199 | if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) { |
| 2200 | if (!P->isResultDependent()) { |
| 2201 | E = P->getResultExpr(); |
| 2202 | continue; |
| 2203 | } |
| 2204 | } |
| 2205 | |
Douglas Gregor | c0244c5 | 2011-09-08 17:56:33 +0000 | [diff] [blame] | 2206 | if (SubstNonTypeTemplateParmExpr *NTTP |
| 2207 | = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) { |
| 2208 | E = NTTP->getReplacement(); |
| 2209 | continue; |
| 2210 | } |
| 2211 | |
Chris Lattner | ecdd841 | 2009-03-13 17:28:01 +0000 | [diff] [blame] | 2212 | return E; |
| 2213 | } |
| 2214 | } |
| 2215 | |
Douglas Gregor | 6eef519 | 2009-12-14 19:27:10 +0000 | [diff] [blame] | 2216 | bool Expr::isDefaultArgument() const { |
| 2217 | const Expr *E = this; |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 2218 | if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E)) |
| 2219 | E = M->GetTemporaryExpr(); |
| 2220 | |
Douglas Gregor | 6eef519 | 2009-12-14 19:27:10 +0000 | [diff] [blame] | 2221 | while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) |
| 2222 | E = ICE->getSubExprAsWritten(); |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2223 | |
Douglas Gregor | 6eef519 | 2009-12-14 19:27:10 +0000 | [diff] [blame] | 2224 | return isa<CXXDefaultArgExpr>(E); |
| 2225 | } |
Chris Lattner | ecdd841 | 2009-03-13 17:28:01 +0000 | [diff] [blame] | 2226 | |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 2227 | /// \brief Skip over any no-op casts and any temporary-binding |
| 2228 | /// expressions. |
Anders Carlsson | f8b3015 | 2010-11-28 16:40:49 +0000 | [diff] [blame] | 2229 | static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) { |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 2230 | if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E)) |
| 2231 | E = M->GetTemporaryExpr(); |
| 2232 | |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 2233 | while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2234 | if (ICE->getCastKind() == CK_NoOp) |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 2235 | E = ICE->getSubExpr(); |
| 2236 | else |
| 2237 | break; |
| 2238 | } |
| 2239 | |
| 2240 | while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E)) |
| 2241 | E = BE->getSubExpr(); |
| 2242 | |
| 2243 | while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2244 | if (ICE->getCastKind() == CK_NoOp) |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 2245 | E = ICE->getSubExpr(); |
| 2246 | else |
| 2247 | break; |
| 2248 | } |
Anders Carlsson | f8b3015 | 2010-11-28 16:40:49 +0000 | [diff] [blame] | 2249 | |
| 2250 | return E->IgnoreParens(); |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 2251 | } |
| 2252 | |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 2253 | /// isTemporaryObject - Determines if this expression produces a |
| 2254 | /// temporary of the given class type. |
| 2255 | bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const { |
| 2256 | if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy))) |
| 2257 | return false; |
| 2258 | |
Anders Carlsson | f8b3015 | 2010-11-28 16:40:49 +0000 | [diff] [blame] | 2259 | const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this); |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 2260 | |
John McCall | 58277b5 | 2010-09-15 20:59:13 +0000 | [diff] [blame] | 2261 | // Temporaries are by definition pr-values of class type. |
Fariborz Jahanian | db148be | 2010-09-27 17:30:38 +0000 | [diff] [blame] | 2262 | if (!E->Classify(C).isPRValue()) { |
| 2263 | // 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] | 2264 | if (!isa<ObjCPropertyRefExpr>(E)) |
Fariborz Jahanian | db148be | 2010-09-27 17:30:38 +0000 | [diff] [blame] | 2265 | return false; |
| 2266 | } |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 2267 | |
John McCall | 19e60ad | 2010-09-16 06:57:56 +0000 | [diff] [blame] | 2268 | // Black-list a few cases which yield pr-values of class type that don't |
| 2269 | // refer to temporaries of that type: |
| 2270 | |
| 2271 | // - implicit derived-to-base conversions |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 2272 | if (isa<ImplicitCastExpr>(E)) { |
| 2273 | switch (cast<ImplicitCastExpr>(E)->getCastKind()) { |
| 2274 | case CK_DerivedToBase: |
| 2275 | case CK_UncheckedDerivedToBase: |
| 2276 | return false; |
| 2277 | default: |
| 2278 | break; |
| 2279 | } |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 2280 | } |
| 2281 | |
John McCall | 19e60ad | 2010-09-16 06:57:56 +0000 | [diff] [blame] | 2282 | // - member expressions (all) |
| 2283 | if (isa<MemberExpr>(E)) |
| 2284 | return false; |
| 2285 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2286 | // - opaque values (all) |
| 2287 | if (isa<OpaqueValueExpr>(E)) |
| 2288 | return false; |
| 2289 | |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 2290 | return true; |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 2291 | } |
| 2292 | |
Douglas Gregor | 75e8504 | 2011-03-02 21:06:53 +0000 | [diff] [blame] | 2293 | bool Expr::isImplicitCXXThis() const { |
| 2294 | const Expr *E = this; |
| 2295 | |
| 2296 | // Strip away parentheses and casts we don't care about. |
| 2297 | while (true) { |
| 2298 | if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) { |
| 2299 | E = Paren->getSubExpr(); |
| 2300 | continue; |
| 2301 | } |
| 2302 | |
| 2303 | if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
| 2304 | if (ICE->getCastKind() == CK_NoOp || |
| 2305 | ICE->getCastKind() == CK_LValueToRValue || |
| 2306 | ICE->getCastKind() == CK_DerivedToBase || |
| 2307 | ICE->getCastKind() == CK_UncheckedDerivedToBase) { |
| 2308 | E = ICE->getSubExpr(); |
| 2309 | continue; |
| 2310 | } |
| 2311 | } |
| 2312 | |
| 2313 | if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) { |
| 2314 | if (UnOp->getOpcode() == UO_Extension) { |
| 2315 | E = UnOp->getSubExpr(); |
| 2316 | continue; |
| 2317 | } |
| 2318 | } |
| 2319 | |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 2320 | if (const MaterializeTemporaryExpr *M |
| 2321 | = dyn_cast<MaterializeTemporaryExpr>(E)) { |
| 2322 | E = M->GetTemporaryExpr(); |
| 2323 | continue; |
| 2324 | } |
| 2325 | |
Douglas Gregor | 75e8504 | 2011-03-02 21:06:53 +0000 | [diff] [blame] | 2326 | break; |
| 2327 | } |
| 2328 | |
| 2329 | if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E)) |
| 2330 | return This->isImplicit(); |
| 2331 | |
| 2332 | return false; |
| 2333 | } |
| 2334 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2335 | /// hasAnyTypeDependentArguments - Determines if any of the expressions |
| 2336 | /// in Exprs is type-dependent. |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 2337 | bool Expr::hasAnyTypeDependentArguments(llvm::ArrayRef<Expr *> Exprs) { |
| 2338 | for (unsigned I = 0; I < Exprs.size(); ++I) |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2339 | if (Exprs[I]->isTypeDependent()) |
| 2340 | return true; |
| 2341 | |
| 2342 | return false; |
| 2343 | } |
| 2344 | |
John McCall | 4204f07 | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 2345 | bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const { |
Eli Friedman | c39dc9a | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 2346 | // This function is attempting whether an expression is an initializer |
| 2347 | // which can be evaluated at compile-time. isEvaluatable handles most |
| 2348 | // of the cases, but it can't deal with some initializer-specific |
| 2349 | // expressions, and it can't deal with aggregates; we deal with those here, |
| 2350 | // and fall back to isEvaluatable for the other cases. |
| 2351 | |
John McCall | 4204f07 | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 2352 | // If we ever capture reference-binding directly in the AST, we can |
| 2353 | // kill the second parameter. |
| 2354 | |
| 2355 | if (IsForRef) { |
| 2356 | EvalResult Result; |
| 2357 | return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects; |
| 2358 | } |
Eli Friedman | 1f4a6db | 2009-02-20 02:36:22 +0000 | [diff] [blame] | 2359 | |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 2360 | switch (getStmtClass()) { |
Eli Friedman | c39dc9a | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 2361 | default: break; |
Richard Smith | 4ec4089 | 2011-12-09 06:47:34 +0000 | [diff] [blame] | 2362 | case IntegerLiteralClass: |
| 2363 | case FloatingLiteralClass: |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 2364 | case StringLiteralClass: |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2365 | case ObjCStringLiteralClass: |
Chris Lattner | eaf2bb8 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 2366 | case ObjCEncodeExprClass: |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 2367 | return true; |
John McCall | b4b9b15 | 2010-08-01 21:51:45 +0000 | [diff] [blame] | 2368 | case CXXTemporaryObjectExprClass: |
| 2369 | case CXXConstructExprClass: { |
| 2370 | const CXXConstructExpr *CE = cast<CXXConstructExpr>(this); |
John McCall | 4204f07 | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 2371 | |
| 2372 | // Only if it's |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2373 | if (CE->getConstructor()->isTrivial()) { |
| 2374 | // 1) an application of the trivial default constructor or |
| 2375 | if (!CE->getNumArgs()) return true; |
John McCall | 4204f07 | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 2376 | |
Richard Smith | 180f479 | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2377 | // 2) an elidable trivial copy construction of an operand which is |
| 2378 | // itself a constant initializer. Note that we consider the |
| 2379 | // operand on its own, *not* as a reference binding. |
| 2380 | if (CE->isElidable() && |
| 2381 | CE->getArg(0)->isConstantInitializer(Ctx, false)) |
| 2382 | return true; |
| 2383 | } |
| 2384 | |
| 2385 | // 3) a foldable constexpr constructor. |
| 2386 | break; |
John McCall | b4b9b15 | 2010-08-01 21:51:45 +0000 | [diff] [blame] | 2387 | } |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2388 | case CompoundLiteralExprClass: { |
Eli Friedman | 1f4a6db | 2009-02-20 02:36:22 +0000 | [diff] [blame] | 2389 | // This handles gcc's extension that allows global initializers like |
| 2390 | // "struct x {int x;} x = (struct x) {};". |
| 2391 | // FIXME: This accepts other cases it shouldn't! |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2392 | const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer(); |
John McCall | 4204f07 | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 2393 | return Exp->isConstantInitializer(Ctx, false); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2394 | } |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 2395 | case InitListExprClass: { |
Eli Friedman | 1f4a6db | 2009-02-20 02:36:22 +0000 | [diff] [blame] | 2396 | // FIXME: This doesn't deal with fields with reference types correctly. |
| 2397 | // FIXME: This incorrectly allows pointers cast to integers to be assigned |
| 2398 | // to bitfields. |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 2399 | const InitListExpr *Exp = cast<InitListExpr>(this); |
| 2400 | unsigned numInits = Exp->getNumInits(); |
| 2401 | for (unsigned i = 0; i < numInits; i++) { |
John McCall | 4204f07 | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 2402 | if (!Exp->getInit(i)->isConstantInitializer(Ctx, false)) |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 2403 | return false; |
| 2404 | } |
Eli Friedman | c39dc9a | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 2405 | return true; |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 2406 | } |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 2407 | case ImplicitValueInitExprClass: |
| 2408 | return true; |
Chris Lattner | 3ae9f48 | 2009-10-13 07:14:16 +0000 | [diff] [blame] | 2409 | case ParenExprClass: |
John McCall | 4204f07 | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 2410 | return cast<ParenExpr>(this)->getSubExpr() |
| 2411 | ->isConstantInitializer(Ctx, IsForRef); |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 2412 | case GenericSelectionExprClass: |
| 2413 | if (cast<GenericSelectionExpr>(this)->isResultDependent()) |
| 2414 | return false; |
| 2415 | return cast<GenericSelectionExpr>(this)->getResultExpr() |
| 2416 | ->isConstantInitializer(Ctx, IsForRef); |
Abramo Bagnara | 5cadfab | 2010-09-27 07:13:32 +0000 | [diff] [blame] | 2417 | case ChooseExprClass: |
| 2418 | return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx) |
| 2419 | ->isConstantInitializer(Ctx, IsForRef); |
Eli Friedman | c39dc9a | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 2420 | case UnaryOperatorClass: { |
| 2421 | const UnaryOperator* Exp = cast<UnaryOperator>(this); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2422 | if (Exp->getOpcode() == UO_Extension) |
John McCall | 4204f07 | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 2423 | return Exp->getSubExpr()->isConstantInitializer(Ctx, false); |
Eli Friedman | c39dc9a | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 2424 | break; |
| 2425 | } |
John McCall | 4204f07 | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 2426 | case CXXFunctionalCastExprClass: |
John McCall | b4b9b15 | 2010-08-01 21:51:45 +0000 | [diff] [blame] | 2427 | case CXXStaticCastExprClass: |
Chris Lattner | 81045d8 | 2009-04-21 05:19:11 +0000 | [diff] [blame] | 2428 | case ImplicitCastExprClass: |
Richard Smith | d62ca37 | 2011-12-06 22:44:34 +0000 | [diff] [blame] | 2429 | case CStyleCastExprClass: { |
| 2430 | const CastExpr *CE = cast<CastExpr>(this); |
| 2431 | |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 2432 | // If we're promoting an integer to an _Atomic type then this is constant |
| 2433 | // if the integer is constant. We also need to check the converse in case |
| 2434 | // someone does something like: |
| 2435 | // |
| 2436 | // int a = (_Atomic(int))42; |
| 2437 | // |
| 2438 | // I doubt anyone would write code like this directly, but it's quite |
| 2439 | // possible as the result of macro expansions. |
| 2440 | if (CE->getCastKind() == CK_NonAtomicToAtomic || |
| 2441 | CE->getCastKind() == CK_AtomicToNonAtomic) |
| 2442 | return CE->getSubExpr()->isConstantInitializer(Ctx, false); |
| 2443 | |
Richard Smith | d62ca37 | 2011-12-06 22:44:34 +0000 | [diff] [blame] | 2444 | // Handle bitcasts of vector constants. |
| 2445 | if (getType()->isVectorType() && CE->getCastKind() == CK_BitCast) |
| 2446 | return CE->getSubExpr()->isConstantInitializer(Ctx, false); |
| 2447 | |
Eli Friedman | 6bd9719 | 2011-12-21 00:43:02 +0000 | [diff] [blame] | 2448 | // Handle misc casts we want to ignore. |
| 2449 | // FIXME: Is it really safe to ignore all these? |
| 2450 | if (CE->getCastKind() == CK_NoOp || |
| 2451 | CE->getCastKind() == CK_LValueToRValue || |
| 2452 | CE->getCastKind() == CK_ToUnion || |
| 2453 | CE->getCastKind() == CK_ConstructorConversion) |
Richard Smith | d62ca37 | 2011-12-06 22:44:34 +0000 | [diff] [blame] | 2454 | return CE->getSubExpr()->isConstantInitializer(Ctx, false); |
| 2455 | |
Eli Friedman | c39dc9a | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 2456 | break; |
Richard Smith | d62ca37 | 2011-12-06 22:44:34 +0000 | [diff] [blame] | 2457 | } |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 2458 | case MaterializeTemporaryExprClass: |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2459 | return cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr() |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 2460 | ->isConstantInitializer(Ctx, false); |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 2461 | } |
Eli Friedman | c39dc9a | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 2462 | return isEvaluatable(Ctx); |
Steve Naroff | 38374b0 | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 2463 | } |
| 2464 | |
Douglas Gregor | 25d0a0f | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 2465 | namespace { |
| 2466 | /// \brief Look for a call to a non-trivial function within an expression. |
| 2467 | class NonTrivialCallFinder : public EvaluatedExprVisitor<NonTrivialCallFinder> |
| 2468 | { |
| 2469 | typedef EvaluatedExprVisitor<NonTrivialCallFinder> Inherited; |
| 2470 | |
| 2471 | bool NonTrivial; |
| 2472 | |
| 2473 | public: |
| 2474 | explicit NonTrivialCallFinder(ASTContext &Context) |
Douglas Gregor | b11e525 | 2012-02-23 07:44:18 +0000 | [diff] [blame] | 2475 | : Inherited(Context), NonTrivial(false) { } |
Douglas Gregor | 25d0a0f | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 2476 | |
| 2477 | bool hasNonTrivialCall() const { return NonTrivial; } |
| 2478 | |
| 2479 | void VisitCallExpr(CallExpr *E) { |
| 2480 | if (CXXMethodDecl *Method |
| 2481 | = dyn_cast_or_null<CXXMethodDecl>(E->getCalleeDecl())) { |
| 2482 | if (Method->isTrivial()) { |
| 2483 | // Recurse to children of the call. |
| 2484 | Inherited::VisitStmt(E); |
| 2485 | return; |
| 2486 | } |
| 2487 | } |
| 2488 | |
| 2489 | NonTrivial = true; |
| 2490 | } |
| 2491 | |
| 2492 | void VisitCXXConstructExpr(CXXConstructExpr *E) { |
| 2493 | if (E->getConstructor()->isTrivial()) { |
| 2494 | // Recurse to children of the call. |
| 2495 | Inherited::VisitStmt(E); |
| 2496 | return; |
| 2497 | } |
| 2498 | |
| 2499 | NonTrivial = true; |
| 2500 | } |
| 2501 | |
| 2502 | void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) { |
| 2503 | if (E->getTemporary()->getDestructor()->isTrivial()) { |
| 2504 | Inherited::VisitStmt(E); |
| 2505 | return; |
| 2506 | } |
| 2507 | |
| 2508 | NonTrivial = true; |
| 2509 | } |
| 2510 | }; |
| 2511 | } |
| 2512 | |
| 2513 | bool Expr::hasNonTrivialCall(ASTContext &Ctx) { |
| 2514 | NonTrivialCallFinder Finder(Ctx); |
| 2515 | Finder.Visit(this); |
| 2516 | return Finder.hasNonTrivialCall(); |
| 2517 | } |
| 2518 | |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 2519 | /// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null |
| 2520 | /// pointer constant or not, as well as the specific kind of constant detected. |
| 2521 | /// Null pointer constants can be integer constant expressions with the |
| 2522 | /// value zero, casts of zero to void*, nullptr (C++0X), or __null |
| 2523 | /// (a GNU extension). |
| 2524 | Expr::NullPointerConstantKind |
| 2525 | Expr::isNullPointerConstant(ASTContext &Ctx, |
| 2526 | NullPointerConstantValueDependence NPC) const { |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2527 | if (isValueDependent()) { |
| 2528 | switch (NPC) { |
| 2529 | case NPC_NeverValueDependent: |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2530 | llvm_unreachable("Unexpected value dependent expression!"); |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2531 | case NPC_ValueDependentIsNull: |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 2532 | if (isTypeDependent() || getType()->isIntegralType(Ctx)) |
| 2533 | return NPCK_ZeroInteger; |
| 2534 | else |
| 2535 | return NPCK_NotNull; |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2536 | |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2537 | case NPC_ValueDependentIsNotNull: |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 2538 | return NPCK_NotNull; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2539 | } |
| 2540 | } |
Daniel Dunbar | f515b22 | 2009-09-18 08:46:16 +0000 | [diff] [blame] | 2541 | |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 2542 | // Strip off a cast to void*, if it exists. Except in C++. |
Argyrios Kyrtzidis | 0835a3c | 2008-08-18 23:01:59 +0000 | [diff] [blame] | 2543 | if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2544 | if (!Ctx.getLangOpts().CPlusPlus) { |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 2545 | // Check that it is a cast to void*. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2546 | if (const PointerType *PT = CE->getType()->getAs<PointerType>()) { |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 2547 | QualType Pointee = PT->getPointeeType(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2548 | if (!Pointee.hasQualifiers() && |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 2549 | Pointee->isVoidType() && // to void* |
| 2550 | CE->getSubExpr()->getType()->isIntegerType()) // from int. |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2551 | return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC); |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 2552 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2553 | } |
Steve Naroff | aa58f00 | 2008-01-14 16:10:57 +0000 | [diff] [blame] | 2554 | } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) { |
| 2555 | // Ignore the ImplicitCastExpr type entirely. |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2556 | return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC); |
Steve Naroff | aa58f00 | 2008-01-14 16:10:57 +0000 | [diff] [blame] | 2557 | } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) { |
| 2558 | // Accept ((void*)0) as a null pointer constant, as many other |
| 2559 | // implementations do. |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2560 | return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC); |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 2561 | } else if (const GenericSelectionExpr *GE = |
| 2562 | dyn_cast<GenericSelectionExpr>(this)) { |
| 2563 | return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2564 | } else if (const CXXDefaultArgExpr *DefaultArg |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 2565 | = dyn_cast<CXXDefaultArgExpr>(this)) { |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2566 | // See through default argument expressions |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2567 | return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC); |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 2568 | } else if (isa<GNUNullExpr>(this)) { |
| 2569 | // The GNU __null extension is always a null pointer constant. |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 2570 | return NPCK_GNUNull; |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 2571 | } else if (const MaterializeTemporaryExpr *M |
| 2572 | = dyn_cast<MaterializeTemporaryExpr>(this)) { |
| 2573 | return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC); |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 2574 | } else if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(this)) { |
| 2575 | if (const Expr *Source = OVE->getSourceExpr()) |
| 2576 | return Source->isNullPointerConstant(Ctx, NPC); |
Steve Naroff | aaffbf7 | 2008-01-14 02:53:34 +0000 | [diff] [blame] | 2577 | } |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 2578 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2579 | // C++0x nullptr_t is always a null pointer constant. |
| 2580 | if (getType()->isNullPtrType()) |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 2581 | return NPCK_CXX0X_nullptr; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2582 | |
Fariborz Jahanian | ff3a078 | 2010-09-27 22:42:37 +0000 | [diff] [blame] | 2583 | if (const RecordType *UT = getType()->getAsUnionType()) |
| 2584 | if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
| 2585 | if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){ |
| 2586 | const Expr *InitExpr = CLE->getInitializer(); |
| 2587 | if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr)) |
| 2588 | return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC); |
| 2589 | } |
Steve Naroff | aa58f00 | 2008-01-14 16:10:57 +0000 | [diff] [blame] | 2590 | // This expression must be an integer type. |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2591 | if (!getType()->isIntegerType() || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2592 | (Ctx.getLangOpts().CPlusPlus && getType()->isEnumeralType())) |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 2593 | return NPCK_NotNull; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2594 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2595 | // If we have an integer constant expression, we need to *evaluate* it and |
Richard Smith | 70488e2 | 2012-02-14 21:38:30 +0000 | [diff] [blame] | 2596 | // test for the value 0. Don't use the C++11 constant expression semantics |
| 2597 | // for this, for now; once the dust settles on core issue 903, we might only |
| 2598 | // allow a literal 0 here in C++11 mode. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2599 | if (Ctx.getLangOpts().CPlusPlus0x) { |
Richard Smith | 70488e2 | 2012-02-14 21:38:30 +0000 | [diff] [blame] | 2600 | if (!isCXX98IntegralConstantExpr(Ctx)) |
| 2601 | return NPCK_NotNull; |
| 2602 | } else { |
| 2603 | if (!isIntegerConstantExpr(Ctx)) |
| 2604 | return NPCK_NotNull; |
| 2605 | } |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 2606 | |
Richard Smith | 70488e2 | 2012-02-14 21:38:30 +0000 | [diff] [blame] | 2607 | return (EvaluateKnownConstInt(Ctx) == 0) ? NPCK_ZeroInteger : NPCK_NotNull; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2608 | } |
Steve Naroff | 31a4584 | 2007-07-28 23:10:27 +0000 | [diff] [blame] | 2609 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 2610 | /// \brief If this expression is an l-value for an Objective C |
| 2611 | /// property, find the underlying property reference expression. |
| 2612 | const ObjCPropertyRefExpr *Expr::getObjCProperty() const { |
| 2613 | const Expr *E = this; |
| 2614 | while (true) { |
| 2615 | assert((E->getValueKind() == VK_LValue && |
| 2616 | E->getObjectKind() == OK_ObjCProperty) && |
| 2617 | "expression is not a property reference"); |
| 2618 | E = E->IgnoreParenCasts(); |
| 2619 | if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
| 2620 | if (BO->getOpcode() == BO_Comma) { |
| 2621 | E = BO->getRHS(); |
| 2622 | continue; |
| 2623 | } |
| 2624 | } |
| 2625 | |
| 2626 | break; |
| 2627 | } |
| 2628 | |
| 2629 | return cast<ObjCPropertyRefExpr>(E); |
| 2630 | } |
| 2631 | |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2632 | FieldDecl *Expr::getBitField() { |
Douglas Gregor | 6f4a69a | 2009-07-06 15:38:40 +0000 | [diff] [blame] | 2633 | Expr *E = this->IgnoreParens(); |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2634 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 2635 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 2636 | if (ICE->getCastKind() == CK_LValueToRValue || |
| 2637 | (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp)) |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 2638 | E = ICE->getSubExpr()->IgnoreParens(); |
| 2639 | else |
| 2640 | break; |
| 2641 | } |
| 2642 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2643 | if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E)) |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 2644 | if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl())) |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2645 | if (Field->isBitField()) |
| 2646 | return Field; |
| 2647 | |
Argyrios Kyrtzidis | 0f279e7 | 2010-10-30 19:52:22 +0000 | [diff] [blame] | 2648 | if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E)) |
| 2649 | if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl())) |
| 2650 | if (Field->isBitField()) |
| 2651 | return Field; |
| 2652 | |
Eli Friedman | 42068e9 | 2011-07-13 02:05:57 +0000 | [diff] [blame] | 2653 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) { |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2654 | if (BinOp->isAssignmentOp() && BinOp->getLHS()) |
| 2655 | return BinOp->getLHS()->getBitField(); |
| 2656 | |
Eli Friedman | 42068e9 | 2011-07-13 02:05:57 +0000 | [diff] [blame] | 2657 | if (BinOp->getOpcode() == BO_Comma && BinOp->getRHS()) |
| 2658 | return BinOp->getRHS()->getBitField(); |
| 2659 | } |
| 2660 | |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2661 | return 0; |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2662 | } |
| 2663 | |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 2664 | bool Expr::refersToVectorElement() const { |
| 2665 | const Expr *E = this->IgnoreParens(); |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2666 | |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 2667 | while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2668 | if (ICE->getValueKind() != VK_RValue && |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2669 | ICE->getCastKind() == CK_NoOp) |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 2670 | E = ICE->getSubExpr()->IgnoreParens(); |
| 2671 | else |
| 2672 | break; |
| 2673 | } |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2674 | |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 2675 | if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) |
| 2676 | return ASE->getBase()->getType()->isVectorType(); |
| 2677 | |
| 2678 | if (isa<ExtVectorElementExpr>(E)) |
| 2679 | return true; |
| 2680 | |
| 2681 | return false; |
| 2682 | } |
| 2683 | |
Chris Lattner | 2140e90 | 2009-02-16 22:14:05 +0000 | [diff] [blame] | 2684 | /// isArrow - Return true if the base expression is a pointer to vector, |
| 2685 | /// return false if the base expression is a vector. |
| 2686 | bool ExtVectorElementExpr::isArrow() const { |
| 2687 | return getBase()->getType()->isPointerType(); |
| 2688 | } |
| 2689 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2690 | unsigned ExtVectorElementExpr::getNumElements() const { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2691 | if (const VectorType *VT = getType()->getAs<VectorType>()) |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2692 | return VT->getNumElements(); |
| 2693 | return 1; |
Chris Lattner | 4d0ac88 | 2007-08-03 16:00:20 +0000 | [diff] [blame] | 2694 | } |
| 2695 | |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2696 | /// containsDuplicateElements - Return true if any element access is repeated. |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2697 | bool ExtVectorElementExpr::containsDuplicateElements() const { |
Daniel Dunbar | a2b34eb | 2009-10-18 02:09:09 +0000 | [diff] [blame] | 2698 | // FIXME: Refactor this code to an accessor on the AST node which returns the |
| 2699 | // "type" of component access, and share with code below and in Sema. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2700 | StringRef Comp = Accessor->getName(); |
Nate Begeman | 190d6a2 | 2009-01-18 02:01:21 +0000 | [diff] [blame] | 2701 | |
| 2702 | // Halving swizzles do not contain duplicate elements. |
Daniel Dunbar | 1502742 | 2009-10-17 23:53:04 +0000 | [diff] [blame] | 2703 | if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd") |
Nate Begeman | 190d6a2 | 2009-01-18 02:01:21 +0000 | [diff] [blame] | 2704 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2705 | |
Nate Begeman | 190d6a2 | 2009-01-18 02:01:21 +0000 | [diff] [blame] | 2706 | // Advance past s-char prefix on hex swizzles. |
Daniel Dunbar | 1502742 | 2009-10-17 23:53:04 +0000 | [diff] [blame] | 2707 | if (Comp[0] == 's' || Comp[0] == 'S') |
| 2708 | Comp = Comp.substr(1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2709 | |
Daniel Dunbar | 1502742 | 2009-10-17 23:53:04 +0000 | [diff] [blame] | 2710 | for (unsigned i = 0, e = Comp.size(); i != e; ++i) |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2711 | if (Comp.substr(i + 1).find(Comp[i]) != StringRef::npos) |
Steve Naroff | fec0b49 | 2007-07-30 03:29:09 +0000 | [diff] [blame] | 2712 | return true; |
Daniel Dunbar | 1502742 | 2009-10-17 23:53:04 +0000 | [diff] [blame] | 2713 | |
Steve Naroff | fec0b49 | 2007-07-30 03:29:09 +0000 | [diff] [blame] | 2714 | return false; |
| 2715 | } |
Chris Lattner | b8f849d | 2007-08-02 23:36:59 +0000 | [diff] [blame] | 2716 | |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2717 | /// getEncodedElementAccess - We encode the fields as a llvm ConstantArray. |
Nate Begeman | 3b8d116 | 2008-05-13 21:03:02 +0000 | [diff] [blame] | 2718 | void ExtVectorElementExpr::getEncodedElementAccess( |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2719 | SmallVectorImpl<unsigned> &Elts) const { |
| 2720 | StringRef Comp = Accessor->getName(); |
Daniel Dunbar | 4b55b24 | 2009-10-18 02:09:31 +0000 | [diff] [blame] | 2721 | if (Comp[0] == 's' || Comp[0] == 'S') |
| 2722 | Comp = Comp.substr(1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2723 | |
Daniel Dunbar | 4b55b24 | 2009-10-18 02:09:31 +0000 | [diff] [blame] | 2724 | bool isHi = Comp == "hi"; |
| 2725 | bool isLo = Comp == "lo"; |
| 2726 | bool isEven = Comp == "even"; |
| 2727 | bool isOdd = Comp == "odd"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2728 | |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2729 | for (unsigned i = 0, e = getNumElements(); i != e; ++i) { |
| 2730 | uint64_t Index; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2731 | |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2732 | if (isHi) |
| 2733 | Index = e + i; |
| 2734 | else if (isLo) |
| 2735 | Index = i; |
| 2736 | else if (isEven) |
| 2737 | Index = 2 * i; |
| 2738 | else if (isOdd) |
| 2739 | Index = 2 * i + 1; |
| 2740 | else |
Daniel Dunbar | 4b55b24 | 2009-10-18 02:09:31 +0000 | [diff] [blame] | 2741 | Index = ExtVectorType::getAccessorIdx(Comp[i]); |
Chris Lattner | b8f849d | 2007-08-02 23:36:59 +0000 | [diff] [blame] | 2742 | |
Nate Begeman | 3b8d116 | 2008-05-13 21:03:02 +0000 | [diff] [blame] | 2743 | Elts.push_back(Index); |
Chris Lattner | b8f849d | 2007-08-02 23:36:59 +0000 | [diff] [blame] | 2744 | } |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2745 | } |
| 2746 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2747 | ObjCMessageExpr::ObjCMessageExpr(QualType T, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2748 | ExprValueKind VK, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2749 | SourceLocation LBracLoc, |
| 2750 | SourceLocation SuperLoc, |
| 2751 | bool IsInstanceSuper, |
| 2752 | QualType SuperType, |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2753 | Selector Sel, |
Argyrios Kyrtzidis | 2071808 | 2011-10-03 06:36:51 +0000 | [diff] [blame] | 2754 | ArrayRef<SourceLocation> SelLocs, |
| 2755 | SelectorLocationsKind SelLocsK, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2756 | ObjCMethodDecl *Method, |
Argyrios Kyrtzidis | 8d9ed79 | 2011-10-03 06:36:45 +0000 | [diff] [blame] | 2757 | ArrayRef<Expr *> Args, |
Argyrios Kyrtzidis | 746f5bc | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 2758 | SourceLocation RBracLoc, |
| 2759 | bool isImplicit) |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2760 | : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2761 | /*TypeDependent=*/false, /*ValueDependent=*/false, |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 2762 | /*InstantiationDependent=*/false, |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2763 | /*ContainsUnexpandedParameterPack=*/false), |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2764 | SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method |
| 2765 | : Sel.getAsOpaquePtr())), |
Argyrios Kyrtzidis | b994e6c | 2011-10-03 06:36:55 +0000 | [diff] [blame] | 2766 | Kind(IsInstanceSuper? SuperInstance : SuperClass), |
Argyrios Kyrtzidis | 746f5bc | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 2767 | HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit), |
| 2768 | SuperLoc(SuperLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc) |
Douglas Gregor | c2350e5 | 2010-03-08 16:40:19 +0000 | [diff] [blame] | 2769 | { |
Argyrios Kyrtzidis | 2071808 | 2011-10-03 06:36:51 +0000 | [diff] [blame] | 2770 | initArgsAndSelLocs(Args, SelLocs, SelLocsK); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2771 | setReceiverPointer(SuperType.getAsOpaquePtr()); |
Ted Kremenek | 4df728e | 2008-06-24 15:50:53 +0000 | [diff] [blame] | 2772 | } |
| 2773 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2774 | ObjCMessageExpr::ObjCMessageExpr(QualType T, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2775 | ExprValueKind VK, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2776 | SourceLocation LBracLoc, |
| 2777 | TypeSourceInfo *Receiver, |
Argyrios Kyrtzidis | f40f0d5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 2778 | Selector Sel, |
Argyrios Kyrtzidis | 2071808 | 2011-10-03 06:36:51 +0000 | [diff] [blame] | 2779 | ArrayRef<SourceLocation> SelLocs, |
| 2780 | SelectorLocationsKind SelLocsK, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2781 | ObjCMethodDecl *Method, |
Argyrios Kyrtzidis | 8d9ed79 | 2011-10-03 06:36:45 +0000 | [diff] [blame] | 2782 | ArrayRef<Expr *> Args, |
Argyrios Kyrtzidis | 746f5bc | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 2783 | SourceLocation RBracLoc, |
| 2784 | bool isImplicit) |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2785 | : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(), |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 2786 | T->isDependentType(), T->isInstantiationDependentType(), |
| 2787 | T->containsUnexpandedParameterPack()), |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2788 | SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method |
| 2789 | : Sel.getAsOpaquePtr())), |
Argyrios Kyrtzidis | b994e6c | 2011-10-03 06:36:55 +0000 | [diff] [blame] | 2790 | Kind(Class), |
Argyrios Kyrtzidis | 746f5bc | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 2791 | HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit), |
Argyrios Kyrtzidis | 2071808 | 2011-10-03 06:36:51 +0000 | [diff] [blame] | 2792 | LBracLoc(LBracLoc), RBracLoc(RBracLoc) |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2793 | { |
Argyrios Kyrtzidis | 2071808 | 2011-10-03 06:36:51 +0000 | [diff] [blame] | 2794 | initArgsAndSelLocs(Args, SelLocs, SelLocsK); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2795 | setReceiverPointer(Receiver); |
Ted Kremenek | 4df728e | 2008-06-24 15:50:53 +0000 | [diff] [blame] | 2796 | } |
| 2797 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2798 | ObjCMessageExpr::ObjCMessageExpr(QualType T, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2799 | ExprValueKind VK, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2800 | SourceLocation LBracLoc, |
| 2801 | Expr *Receiver, |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2802 | Selector Sel, |
Argyrios Kyrtzidis | 2071808 | 2011-10-03 06:36:51 +0000 | [diff] [blame] | 2803 | ArrayRef<SourceLocation> SelLocs, |
| 2804 | SelectorLocationsKind SelLocsK, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2805 | ObjCMethodDecl *Method, |
Argyrios Kyrtzidis | 8d9ed79 | 2011-10-03 06:36:45 +0000 | [diff] [blame] | 2806 | ArrayRef<Expr *> Args, |
Argyrios Kyrtzidis | 746f5bc | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 2807 | SourceLocation RBracLoc, |
| 2808 | bool isImplicit) |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2809 | : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(), |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2810 | Receiver->isTypeDependent(), |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 2811 | Receiver->isInstantiationDependent(), |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2812 | Receiver->containsUnexpandedParameterPack()), |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2813 | SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method |
| 2814 | : Sel.getAsOpaquePtr())), |
Argyrios Kyrtzidis | b994e6c | 2011-10-03 06:36:55 +0000 | [diff] [blame] | 2815 | Kind(Instance), |
Argyrios Kyrtzidis | 746f5bc | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 2816 | HasMethod(Method != 0), IsDelegateInitCall(false), IsImplicit(isImplicit), |
Argyrios Kyrtzidis | 2071808 | 2011-10-03 06:36:51 +0000 | [diff] [blame] | 2817 | LBracLoc(LBracLoc), RBracLoc(RBracLoc) |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2818 | { |
Argyrios Kyrtzidis | 2071808 | 2011-10-03 06:36:51 +0000 | [diff] [blame] | 2819 | initArgsAndSelLocs(Args, SelLocs, SelLocsK); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2820 | setReceiverPointer(Receiver); |
Argyrios Kyrtzidis | 2071808 | 2011-10-03 06:36:51 +0000 | [diff] [blame] | 2821 | } |
| 2822 | |
| 2823 | void ObjCMessageExpr::initArgsAndSelLocs(ArrayRef<Expr *> Args, |
| 2824 | ArrayRef<SourceLocation> SelLocs, |
| 2825 | SelectorLocationsKind SelLocsK) { |
| 2826 | setNumArgs(Args.size()); |
Douglas Gregor | aa165f8 | 2011-01-03 19:04:46 +0000 | [diff] [blame] | 2827 | Expr **MyArgs = getArgs(); |
Argyrios Kyrtzidis | 8d9ed79 | 2011-10-03 06:36:45 +0000 | [diff] [blame] | 2828 | for (unsigned I = 0; I != Args.size(); ++I) { |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2829 | if (Args[I]->isTypeDependent()) |
| 2830 | ExprBits.TypeDependent = true; |
| 2831 | if (Args[I]->isValueDependent()) |
| 2832 | ExprBits.ValueDependent = true; |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 2833 | if (Args[I]->isInstantiationDependent()) |
| 2834 | ExprBits.InstantiationDependent = true; |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2835 | if (Args[I]->containsUnexpandedParameterPack()) |
| 2836 | ExprBits.ContainsUnexpandedParameterPack = true; |
| 2837 | |
| 2838 | MyArgs[I] = Args[I]; |
| 2839 | } |
Argyrios Kyrtzidis | 2071808 | 2011-10-03 06:36:51 +0000 | [diff] [blame] | 2840 | |
Benjamin Kramer | 19562c9 | 2012-02-20 00:20:48 +0000 | [diff] [blame] | 2841 | SelLocsKind = SelLocsK; |
Argyrios Kyrtzidis | 0c6b8e3 | 2012-01-12 22:34:19 +0000 | [diff] [blame] | 2842 | if (!isImplicit()) { |
Argyrios Kyrtzidis | 0c6b8e3 | 2012-01-12 22:34:19 +0000 | [diff] [blame] | 2843 | if (SelLocsK == SelLoc_NonStandard) |
| 2844 | std::copy(SelLocs.begin(), SelLocs.end(), getStoredSelLocs()); |
| 2845 | } |
Chris Lattner | 0389e6b | 2009-04-26 00:44:05 +0000 | [diff] [blame] | 2846 | } |
| 2847 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2848 | ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2849 | ExprValueKind VK, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2850 | SourceLocation LBracLoc, |
| 2851 | SourceLocation SuperLoc, |
| 2852 | bool IsInstanceSuper, |
| 2853 | QualType SuperType, |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2854 | Selector Sel, |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 2855 | ArrayRef<SourceLocation> SelLocs, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2856 | ObjCMethodDecl *Method, |
Argyrios Kyrtzidis | 8d9ed79 | 2011-10-03 06:36:45 +0000 | [diff] [blame] | 2857 | ArrayRef<Expr *> Args, |
Argyrios Kyrtzidis | 746f5bc | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 2858 | SourceLocation RBracLoc, |
| 2859 | bool isImplicit) { |
| 2860 | assert((!SelLocs.empty() || isImplicit) && |
| 2861 | "No selector locs for non-implicit message"); |
| 2862 | ObjCMessageExpr *Mem; |
| 2863 | SelectorLocationsKind SelLocsK = SelectorLocationsKind(); |
| 2864 | if (isImplicit) |
| 2865 | Mem = alloc(Context, Args.size(), 0); |
| 2866 | else |
| 2867 | Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2868 | return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper, |
Argyrios Kyrtzidis | 2071808 | 2011-10-03 06:36:51 +0000 | [diff] [blame] | 2869 | SuperType, Sel, SelLocs, SelLocsK, |
Argyrios Kyrtzidis | 746f5bc | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 2870 | Method, Args, RBracLoc, isImplicit); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2871 | } |
| 2872 | |
| 2873 | ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2874 | ExprValueKind VK, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2875 | SourceLocation LBracLoc, |
| 2876 | TypeSourceInfo *Receiver, |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2877 | Selector Sel, |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 2878 | ArrayRef<SourceLocation> SelLocs, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2879 | ObjCMethodDecl *Method, |
Argyrios Kyrtzidis | 8d9ed79 | 2011-10-03 06:36:45 +0000 | [diff] [blame] | 2880 | ArrayRef<Expr *> Args, |
Argyrios Kyrtzidis | 746f5bc | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 2881 | SourceLocation RBracLoc, |
| 2882 | bool isImplicit) { |
| 2883 | assert((!SelLocs.empty() || isImplicit) && |
| 2884 | "No selector locs for non-implicit message"); |
| 2885 | ObjCMessageExpr *Mem; |
| 2886 | SelectorLocationsKind SelLocsK = SelectorLocationsKind(); |
| 2887 | if (isImplicit) |
| 2888 | Mem = alloc(Context, Args.size(), 0); |
| 2889 | else |
| 2890 | Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK); |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 2891 | return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, |
Argyrios Kyrtzidis | 746f5bc | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 2892 | SelLocs, SelLocsK, Method, Args, RBracLoc, |
| 2893 | isImplicit); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2894 | } |
| 2895 | |
| 2896 | ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2897 | ExprValueKind VK, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2898 | SourceLocation LBracLoc, |
| 2899 | Expr *Receiver, |
Argyrios Kyrtzidis | f40f0d5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 2900 | Selector Sel, |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 2901 | ArrayRef<SourceLocation> SelLocs, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2902 | ObjCMethodDecl *Method, |
Argyrios Kyrtzidis | 8d9ed79 | 2011-10-03 06:36:45 +0000 | [diff] [blame] | 2903 | ArrayRef<Expr *> Args, |
Argyrios Kyrtzidis | 746f5bc | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 2904 | SourceLocation RBracLoc, |
| 2905 | bool isImplicit) { |
| 2906 | assert((!SelLocs.empty() || isImplicit) && |
| 2907 | "No selector locs for non-implicit message"); |
| 2908 | ObjCMessageExpr *Mem; |
| 2909 | SelectorLocationsKind SelLocsK = SelectorLocationsKind(); |
| 2910 | if (isImplicit) |
| 2911 | Mem = alloc(Context, Args.size(), 0); |
| 2912 | else |
| 2913 | Mem = alloc(Context, Args, RBracLoc, SelLocs, Sel, SelLocsK); |
Argyrios Kyrtzidis | 9513762 | 2011-10-03 06:36:17 +0000 | [diff] [blame] | 2914 | return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, |
Argyrios Kyrtzidis | 746f5bc | 2012-01-12 02:34:39 +0000 | [diff] [blame] | 2915 | SelLocs, SelLocsK, Method, Args, RBracLoc, |
| 2916 | isImplicit); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2917 | } |
| 2918 | |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2919 | ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context, |
Argyrios Kyrtzidis | 2071808 | 2011-10-03 06:36:51 +0000 | [diff] [blame] | 2920 | unsigned NumArgs, |
| 2921 | unsigned NumStoredSelLocs) { |
| 2922 | ObjCMessageExpr *Mem = alloc(Context, NumArgs, NumStoredSelLocs); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2923 | return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs); |
| 2924 | } |
Argyrios Kyrtzidis | e005d19 | 2010-12-10 20:08:30 +0000 | [diff] [blame] | 2925 | |
Argyrios Kyrtzidis | 2071808 | 2011-10-03 06:36:51 +0000 | [diff] [blame] | 2926 | ObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C, |
| 2927 | ArrayRef<Expr *> Args, |
| 2928 | SourceLocation RBraceLoc, |
| 2929 | ArrayRef<SourceLocation> SelLocs, |
| 2930 | Selector Sel, |
| 2931 | SelectorLocationsKind &SelLocsK) { |
| 2932 | SelLocsK = hasStandardSelectorLocs(Sel, SelLocs, Args, RBraceLoc); |
| 2933 | unsigned NumStoredSelLocs = (SelLocsK == SelLoc_NonStandard) ? SelLocs.size() |
| 2934 | : 0; |
| 2935 | return alloc(C, Args.size(), NumStoredSelLocs); |
| 2936 | } |
| 2937 | |
| 2938 | ObjCMessageExpr *ObjCMessageExpr::alloc(ASTContext &C, |
| 2939 | unsigned NumArgs, |
| 2940 | unsigned NumStoredSelLocs) { |
| 2941 | unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) + |
| 2942 | NumArgs * sizeof(Expr *) + NumStoredSelLocs * sizeof(SourceLocation); |
| 2943 | return (ObjCMessageExpr *)C.Allocate(Size, |
| 2944 | llvm::AlignOf<ObjCMessageExpr>::Alignment); |
| 2945 | } |
| 2946 | |
| 2947 | void ObjCMessageExpr::getSelectorLocs( |
| 2948 | SmallVectorImpl<SourceLocation> &SelLocs) const { |
| 2949 | for (unsigned i = 0, e = getNumSelectorLocs(); i != e; ++i) |
| 2950 | SelLocs.push_back(getSelectorLoc(i)); |
| 2951 | } |
| 2952 | |
Argyrios Kyrtzidis | e005d19 | 2010-12-10 20:08:30 +0000 | [diff] [blame] | 2953 | SourceRange ObjCMessageExpr::getReceiverRange() const { |
| 2954 | switch (getReceiverKind()) { |
| 2955 | case Instance: |
| 2956 | return getInstanceReceiver()->getSourceRange(); |
| 2957 | |
| 2958 | case Class: |
| 2959 | return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange(); |
| 2960 | |
| 2961 | case SuperInstance: |
| 2962 | case SuperClass: |
| 2963 | return getSuperLoc(); |
| 2964 | } |
| 2965 | |
David Blaikie | 3026348 | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 2966 | llvm_unreachable("Invalid ReceiverKind!"); |
Argyrios Kyrtzidis | e005d19 | 2010-12-10 20:08:30 +0000 | [diff] [blame] | 2967 | } |
| 2968 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2969 | Selector ObjCMessageExpr::getSelector() const { |
| 2970 | if (HasMethod) |
| 2971 | return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod) |
| 2972 | ->getSelector(); |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2973 | return Selector(SelectorOrMethod); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2974 | } |
| 2975 | |
| 2976 | ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const { |
| 2977 | switch (getReceiverKind()) { |
| 2978 | case Instance: |
| 2979 | if (const ObjCObjectPointerType *Ptr |
| 2980 | = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>()) |
| 2981 | return Ptr->getInterfaceDecl(); |
| 2982 | break; |
| 2983 | |
| 2984 | case Class: |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2985 | if (const ObjCObjectType *Ty |
| 2986 | = getClassReceiver()->getAs<ObjCObjectType>()) |
| 2987 | return Ty->getInterface(); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2988 | break; |
| 2989 | |
| 2990 | case SuperInstance: |
| 2991 | if (const ObjCObjectPointerType *Ptr |
| 2992 | = getSuperType()->getAs<ObjCObjectPointerType>()) |
| 2993 | return Ptr->getInterfaceDecl(); |
| 2994 | break; |
| 2995 | |
| 2996 | case SuperClass: |
Argyrios Kyrtzidis | ee8a6ca | 2011-01-25 00:03:48 +0000 | [diff] [blame] | 2997 | if (const ObjCObjectType *Iface |
| 2998 | = getSuperType()->getAs<ObjCObjectType>()) |
| 2999 | return Iface->getInterface(); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 3000 | break; |
| 3001 | } |
| 3002 | |
| 3003 | return 0; |
Ted Kremenek | eb3b324 | 2010-02-11 22:41:21 +0000 | [diff] [blame] | 3004 | } |
Chris Lattner | 0389e6b | 2009-04-26 00:44:05 +0000 | [diff] [blame] | 3005 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3006 | StringRef ObjCBridgedCastExpr::getBridgeKindName() const { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3007 | switch (getBridgeKind()) { |
| 3008 | case OBC_Bridge: |
| 3009 | return "__bridge"; |
| 3010 | case OBC_BridgeTransfer: |
| 3011 | return "__bridge_transfer"; |
| 3012 | case OBC_BridgeRetained: |
| 3013 | return "__bridge_retained"; |
| 3014 | } |
David Blaikie | 3026348 | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 3015 | |
| 3016 | llvm_unreachable("Invalid BridgeKind!"); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3017 | } |
| 3018 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 3019 | bool ChooseExpr::isConditionTrue(const ASTContext &C) const { |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 3020 | return getCond()->EvaluateKnownConstInt(C) != 0; |
Chris Lattner | 27437ca | 2007-10-25 00:29:32 +0000 | [diff] [blame] | 3021 | } |
| 3022 | |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 3023 | ShuffleVectorExpr::ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr, |
| 3024 | QualType Type, SourceLocation BLoc, |
| 3025 | SourceLocation RP) |
| 3026 | : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary, |
| 3027 | Type->isDependentType(), Type->isDependentType(), |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 3028 | Type->isInstantiationDependentType(), |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 3029 | Type->containsUnexpandedParameterPack()), |
| 3030 | BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(nexpr) |
| 3031 | { |
| 3032 | SubExprs = new (C) Stmt*[nexpr]; |
| 3033 | for (unsigned i = 0; i < nexpr; i++) { |
| 3034 | if (args[i]->isTypeDependent()) |
| 3035 | ExprBits.TypeDependent = true; |
| 3036 | if (args[i]->isValueDependent()) |
| 3037 | ExprBits.ValueDependent = true; |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 3038 | if (args[i]->isInstantiationDependent()) |
| 3039 | ExprBits.InstantiationDependent = true; |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 3040 | if (args[i]->containsUnexpandedParameterPack()) |
| 3041 | ExprBits.ContainsUnexpandedParameterPack = true; |
| 3042 | |
| 3043 | SubExprs[i] = args[i]; |
| 3044 | } |
| 3045 | } |
| 3046 | |
Nate Begeman | 888376a | 2009-08-12 02:28:50 +0000 | [diff] [blame] | 3047 | void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs, |
| 3048 | unsigned NumExprs) { |
| 3049 | if (SubExprs) C.Deallocate(SubExprs); |
| 3050 | |
| 3051 | SubExprs = new (C) Stmt* [NumExprs]; |
Douglas Gregor | 94cd5d1 | 2009-04-16 00:01:45 +0000 | [diff] [blame] | 3052 | this->NumExprs = NumExprs; |
| 3053 | memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3054 | } |
Nate Begeman | 888376a | 2009-08-12 02:28:50 +0000 | [diff] [blame] | 3055 | |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 3056 | GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context, |
| 3057 | SourceLocation GenericLoc, Expr *ControllingExpr, |
| 3058 | TypeSourceInfo **AssocTypes, Expr **AssocExprs, |
| 3059 | unsigned NumAssocs, SourceLocation DefaultLoc, |
| 3060 | SourceLocation RParenLoc, |
| 3061 | bool ContainsUnexpandedParameterPack, |
| 3062 | unsigned ResultIndex) |
| 3063 | : Expr(GenericSelectionExprClass, |
| 3064 | AssocExprs[ResultIndex]->getType(), |
| 3065 | AssocExprs[ResultIndex]->getValueKind(), |
| 3066 | AssocExprs[ResultIndex]->getObjectKind(), |
| 3067 | AssocExprs[ResultIndex]->isTypeDependent(), |
| 3068 | AssocExprs[ResultIndex]->isValueDependent(), |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 3069 | AssocExprs[ResultIndex]->isInstantiationDependent(), |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 3070 | ContainsUnexpandedParameterPack), |
| 3071 | AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]), |
| 3072 | SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs), |
| 3073 | ResultIndex(ResultIndex), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc), |
| 3074 | RParenLoc(RParenLoc) { |
| 3075 | SubExprs[CONTROLLING] = ControllingExpr; |
| 3076 | std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes); |
| 3077 | std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR); |
| 3078 | } |
| 3079 | |
| 3080 | GenericSelectionExpr::GenericSelectionExpr(ASTContext &Context, |
| 3081 | SourceLocation GenericLoc, Expr *ControllingExpr, |
| 3082 | TypeSourceInfo **AssocTypes, Expr **AssocExprs, |
| 3083 | unsigned NumAssocs, SourceLocation DefaultLoc, |
| 3084 | SourceLocation RParenLoc, |
| 3085 | bool ContainsUnexpandedParameterPack) |
| 3086 | : Expr(GenericSelectionExprClass, |
| 3087 | Context.DependentTy, |
| 3088 | VK_RValue, |
| 3089 | OK_Ordinary, |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 3090 | /*isTypeDependent=*/true, |
| 3091 | /*isValueDependent=*/true, |
| 3092 | /*isInstantiationDependent=*/true, |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 3093 | ContainsUnexpandedParameterPack), |
| 3094 | AssocTypes(new (Context) TypeSourceInfo*[NumAssocs]), |
| 3095 | SubExprs(new (Context) Stmt*[END_EXPR+NumAssocs]), NumAssocs(NumAssocs), |
| 3096 | ResultIndex(-1U), GenericLoc(GenericLoc), DefaultLoc(DefaultLoc), |
| 3097 | RParenLoc(RParenLoc) { |
| 3098 | SubExprs[CONTROLLING] = ControllingExpr; |
| 3099 | std::copy(AssocTypes, AssocTypes+NumAssocs, this->AssocTypes); |
| 3100 | std::copy(AssocExprs, AssocExprs+NumAssocs, SubExprs+END_EXPR); |
| 3101 | } |
| 3102 | |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 3103 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3104 | // DesignatedInitExpr |
| 3105 | //===----------------------------------------------------------------------===// |
| 3106 | |
Chandler Carruth | b113824 | 2011-06-16 06:47:06 +0000 | [diff] [blame] | 3107 | IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const { |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3108 | assert(Kind == FieldDesignator && "Only valid on a field designator"); |
| 3109 | if (Field.NameOrField & 0x01) |
| 3110 | return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01); |
| 3111 | else |
| 3112 | return getField()->getIdentifier(); |
| 3113 | } |
| 3114 | |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 3115 | DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty, |
Douglas Gregor | 319d57f | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 3116 | unsigned NumDesignators, |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 3117 | const Designator *Designators, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3118 | SourceLocation EqualOrColonLoc, |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 3119 | bool GNUSyntax, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3120 | Expr **IndexExprs, |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 3121 | unsigned NumIndexExprs, |
| 3122 | Expr *Init) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3123 | : Expr(DesignatedInitExprClass, Ty, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3124 | Init->getValueKind(), Init->getObjectKind(), |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 3125 | Init->isTypeDependent(), Init->isValueDependent(), |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 3126 | Init->isInstantiationDependent(), |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 3127 | Init->containsUnexpandedParameterPack()), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3128 | EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax), |
| 3129 | NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) { |
Douglas Gregor | 319d57f | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 3130 | this->Designators = new (C) Designator[NumDesignators]; |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 3131 | |
| 3132 | // Record the initializer itself. |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 3133 | child_range Child = children(); |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 3134 | *Child++ = Init; |
| 3135 | |
| 3136 | // Copy the designators and their subexpressions, computing |
| 3137 | // value-dependence along the way. |
| 3138 | unsigned IndexIdx = 0; |
| 3139 | for (unsigned I = 0; I != NumDesignators; ++I) { |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 3140 | this->Designators[I] = Designators[I]; |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 3141 | |
| 3142 | if (this->Designators[I].isArrayDesignator()) { |
| 3143 | // Compute type- and value-dependence. |
| 3144 | Expr *Index = IndexExprs[IndexIdx]; |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 3145 | if (Index->isTypeDependent() || Index->isValueDependent()) |
| 3146 | ExprBits.ValueDependent = true; |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 3147 | if (Index->isInstantiationDependent()) |
| 3148 | ExprBits.InstantiationDependent = true; |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 3149 | // Propagate unexpanded parameter packs. |
| 3150 | if (Index->containsUnexpandedParameterPack()) |
| 3151 | ExprBits.ContainsUnexpandedParameterPack = true; |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 3152 | |
| 3153 | // Copy the index expressions into permanent storage. |
| 3154 | *Child++ = IndexExprs[IndexIdx++]; |
| 3155 | } else if (this->Designators[I].isArrayRangeDesignator()) { |
| 3156 | // Compute type- and value-dependence. |
| 3157 | Expr *Start = IndexExprs[IndexIdx]; |
| 3158 | Expr *End = IndexExprs[IndexIdx + 1]; |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 3159 | if (Start->isTypeDependent() || Start->isValueDependent() || |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 3160 | End->isTypeDependent() || End->isValueDependent()) { |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 3161 | ExprBits.ValueDependent = true; |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 3162 | ExprBits.InstantiationDependent = true; |
| 3163 | } else if (Start->isInstantiationDependent() || |
| 3164 | End->isInstantiationDependent()) { |
| 3165 | ExprBits.InstantiationDependent = true; |
| 3166 | } |
| 3167 | |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 3168 | // Propagate unexpanded parameter packs. |
| 3169 | if (Start->containsUnexpandedParameterPack() || |
| 3170 | End->containsUnexpandedParameterPack()) |
| 3171 | ExprBits.ContainsUnexpandedParameterPack = true; |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 3172 | |
| 3173 | // Copy the start/end expressions into permanent storage. |
| 3174 | *Child++ = IndexExprs[IndexIdx++]; |
| 3175 | *Child++ = IndexExprs[IndexIdx++]; |
| 3176 | } |
| 3177 | } |
| 3178 | |
| 3179 | assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions"); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 3180 | } |
| 3181 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3182 | DesignatedInitExpr * |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3183 | DesignatedInitExpr::Create(ASTContext &C, Designator *Designators, |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3184 | unsigned NumDesignators, |
| 3185 | Expr **IndexExprs, unsigned NumIndexExprs, |
| 3186 | SourceLocation ColonOrEqualLoc, |
| 3187 | bool UsesColonSyntax, Expr *Init) { |
Steve Naroff | c0ac492 | 2009-01-27 23:20:32 +0000 | [diff] [blame] | 3188 | void *Mem = C.Allocate(sizeof(DesignatedInitExpr) + |
Steve Naroff | c0ac492 | 2009-01-27 23:20:32 +0000 | [diff] [blame] | 3189 | sizeof(Stmt *) * (NumIndexExprs + 1), 8); |
Douglas Gregor | 319d57f | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 3190 | return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators, |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 3191 | ColonOrEqualLoc, UsesColonSyntax, |
| 3192 | IndexExprs, NumIndexExprs, Init); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3193 | } |
| 3194 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3195 | DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C, |
Douglas Gregor | d077d75 | 2009-04-16 00:55:48 +0000 | [diff] [blame] | 3196 | unsigned NumIndexExprs) { |
| 3197 | void *Mem = C.Allocate(sizeof(DesignatedInitExpr) + |
| 3198 | sizeof(Stmt *) * (NumIndexExprs + 1), 8); |
| 3199 | return new (Mem) DesignatedInitExpr(NumIndexExprs + 1); |
| 3200 | } |
| 3201 | |
Douglas Gregor | 319d57f | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 3202 | void DesignatedInitExpr::setDesignators(ASTContext &C, |
| 3203 | const Designator *Desigs, |
Douglas Gregor | d077d75 | 2009-04-16 00:55:48 +0000 | [diff] [blame] | 3204 | unsigned NumDesigs) { |
Douglas Gregor | 319d57f | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 3205 | Designators = new (C) Designator[NumDesigs]; |
Douglas Gregor | d077d75 | 2009-04-16 00:55:48 +0000 | [diff] [blame] | 3206 | NumDesignators = NumDesigs; |
| 3207 | for (unsigned I = 0; I != NumDesigs; ++I) |
| 3208 | Designators[I] = Desigs[I]; |
| 3209 | } |
| 3210 | |
Abramo Bagnara | 24f4674 | 2011-03-16 15:08:46 +0000 | [diff] [blame] | 3211 | SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const { |
| 3212 | DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this); |
| 3213 | if (size() == 1) |
| 3214 | return DIE->getDesignator(0)->getSourceRange(); |
| 3215 | return SourceRange(DIE->getDesignator(0)->getStartLocation(), |
| 3216 | DIE->getDesignator(size()-1)->getEndLocation()); |
| 3217 | } |
| 3218 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3219 | SourceRange DesignatedInitExpr::getSourceRange() const { |
| 3220 | SourceLocation StartLoc; |
Chris Lattner | d603eaa | 2009-02-16 22:33:34 +0000 | [diff] [blame] | 3221 | Designator &First = |
| 3222 | *const_cast<DesignatedInitExpr*>(this)->designators_begin(); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3223 | if (First.isFieldDesignator()) { |
Douglas Gregor | eeae8f0 | 2009-03-28 00:41:23 +0000 | [diff] [blame] | 3224 | if (GNUSyntax) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3225 | StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc); |
| 3226 | else |
| 3227 | StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc); |
| 3228 | } else |
Chris Lattner | d603eaa | 2009-02-16 22:33:34 +0000 | [diff] [blame] | 3229 | StartLoc = |
| 3230 | SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3231 | return SourceRange(StartLoc, getInit()->getSourceRange().getEnd()); |
| 3232 | } |
| 3233 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3234 | Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) { |
| 3235 | assert(D.Kind == Designator::ArrayDesignator && "Requires array designator"); |
| 3236 | char* Ptr = static_cast<char*>(static_cast<void *>(this)); |
| 3237 | Ptr += sizeof(DesignatedInitExpr); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3238 | Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr)); |
| 3239 | return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1)); |
| 3240 | } |
| 3241 | |
| 3242 | Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3243 | assert(D.Kind == Designator::ArrayRangeDesignator && |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3244 | "Requires array range designator"); |
| 3245 | char* Ptr = static_cast<char*>(static_cast<void *>(this)); |
| 3246 | Ptr += sizeof(DesignatedInitExpr); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3247 | Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr)); |
| 3248 | return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1)); |
| 3249 | } |
| 3250 | |
| 3251 | Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3252 | assert(D.Kind == Designator::ArrayRangeDesignator && |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3253 | "Requires array range designator"); |
| 3254 | char* Ptr = static_cast<char*>(static_cast<void *>(this)); |
| 3255 | Ptr += sizeof(DesignatedInitExpr); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3256 | Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr)); |
| 3257 | return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2)); |
| 3258 | } |
| 3259 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 3260 | /// \brief Replaces the designator at index @p Idx with the series |
| 3261 | /// of designators in [First, Last). |
Douglas Gregor | 319d57f | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 3262 | void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3263 | const Designator *First, |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 3264 | const Designator *Last) { |
| 3265 | unsigned NumNewDesignators = Last - First; |
| 3266 | if (NumNewDesignators == 0) { |
| 3267 | std::copy_backward(Designators + Idx + 1, |
| 3268 | Designators + NumDesignators, |
| 3269 | Designators + Idx); |
| 3270 | --NumNewDesignators; |
| 3271 | return; |
| 3272 | } else if (NumNewDesignators == 1) { |
| 3273 | Designators[Idx] = *First; |
| 3274 | return; |
| 3275 | } |
| 3276 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3277 | Designator *NewDesignators |
Douglas Gregor | 319d57f | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 3278 | = new (C) Designator[NumDesignators - 1 + NumNewDesignators]; |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 3279 | std::copy(Designators, Designators + Idx, NewDesignators); |
| 3280 | std::copy(First, Last, NewDesignators + Idx); |
| 3281 | std::copy(Designators + Idx + 1, Designators + NumDesignators, |
| 3282 | NewDesignators + Idx + NumNewDesignators); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 3283 | Designators = NewDesignators; |
| 3284 | NumDesignators = NumDesignators - 1 + NumNewDesignators; |
| 3285 | } |
| 3286 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3287 | ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc, |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3288 | Expr **exprs, unsigned nexprs, |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 3289 | SourceLocation rparenloc) |
| 3290 | : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary, |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 3291 | false, false, false, false), |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 3292 | NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3293 | Exprs = new (C) Stmt*[nexprs]; |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 3294 | for (unsigned i = 0; i != nexprs; ++i) { |
| 3295 | if (exprs[i]->isTypeDependent()) |
| 3296 | ExprBits.TypeDependent = true; |
| 3297 | if (exprs[i]->isValueDependent()) |
| 3298 | ExprBits.ValueDependent = true; |
Douglas Gregor | 561f812 | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 3299 | if (exprs[i]->isInstantiationDependent()) |
| 3300 | ExprBits.InstantiationDependent = true; |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 3301 | if (exprs[i]->containsUnexpandedParameterPack()) |
| 3302 | ExprBits.ContainsUnexpandedParameterPack = true; |
| 3303 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3304 | Exprs[i] = exprs[i]; |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 3305 | } |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3306 | } |
| 3307 | |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 3308 | const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) { |
| 3309 | if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e)) |
| 3310 | e = ewc->getSubExpr(); |
Douglas Gregor | 03e8003 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 3311 | if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e)) |
| 3312 | e = m->GetTemporaryExpr(); |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 3313 | e = cast<CXXConstructExpr>(e)->getArg(0); |
| 3314 | while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e)) |
| 3315 | e = ice->getSubExpr(); |
| 3316 | return cast<OpaqueValueExpr>(e); |
| 3317 | } |
| 3318 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 3319 | PseudoObjectExpr *PseudoObjectExpr::Create(ASTContext &Context, EmptyShell sh, |
| 3320 | unsigned numSemanticExprs) { |
| 3321 | void *buffer = Context.Allocate(sizeof(PseudoObjectExpr) + |
| 3322 | (1 + numSemanticExprs) * sizeof(Expr*), |
| 3323 | llvm::alignOf<PseudoObjectExpr>()); |
| 3324 | return new(buffer) PseudoObjectExpr(sh, numSemanticExprs); |
| 3325 | } |
| 3326 | |
| 3327 | PseudoObjectExpr::PseudoObjectExpr(EmptyShell shell, unsigned numSemanticExprs) |
| 3328 | : Expr(PseudoObjectExprClass, shell) { |
| 3329 | PseudoObjectExprBits.NumSubExprs = numSemanticExprs + 1; |
| 3330 | } |
| 3331 | |
| 3332 | PseudoObjectExpr *PseudoObjectExpr::Create(ASTContext &C, Expr *syntax, |
| 3333 | ArrayRef<Expr*> semantics, |
| 3334 | unsigned resultIndex) { |
| 3335 | assert(syntax && "no syntactic expression!"); |
| 3336 | assert(semantics.size() && "no semantic expressions!"); |
| 3337 | |
| 3338 | QualType type; |
| 3339 | ExprValueKind VK; |
| 3340 | if (resultIndex == NoResult) { |
| 3341 | type = C.VoidTy; |
| 3342 | VK = VK_RValue; |
| 3343 | } else { |
| 3344 | assert(resultIndex < semantics.size()); |
| 3345 | type = semantics[resultIndex]->getType(); |
| 3346 | VK = semantics[resultIndex]->getValueKind(); |
| 3347 | assert(semantics[resultIndex]->getObjectKind() == OK_Ordinary); |
| 3348 | } |
| 3349 | |
| 3350 | void *buffer = C.Allocate(sizeof(PseudoObjectExpr) + |
| 3351 | (1 + semantics.size()) * sizeof(Expr*), |
| 3352 | llvm::alignOf<PseudoObjectExpr>()); |
| 3353 | return new(buffer) PseudoObjectExpr(type, VK, syntax, semantics, |
| 3354 | resultIndex); |
| 3355 | } |
| 3356 | |
| 3357 | PseudoObjectExpr::PseudoObjectExpr(QualType type, ExprValueKind VK, |
| 3358 | Expr *syntax, ArrayRef<Expr*> semantics, |
| 3359 | unsigned resultIndex) |
| 3360 | : Expr(PseudoObjectExprClass, type, VK, OK_Ordinary, |
| 3361 | /*filled in at end of ctor*/ false, false, false, false) { |
| 3362 | PseudoObjectExprBits.NumSubExprs = semantics.size() + 1; |
| 3363 | PseudoObjectExprBits.ResultIndex = resultIndex + 1; |
| 3364 | |
| 3365 | for (unsigned i = 0, e = semantics.size() + 1; i != e; ++i) { |
| 3366 | Expr *E = (i == 0 ? syntax : semantics[i-1]); |
| 3367 | getSubExprsBuffer()[i] = E; |
| 3368 | |
| 3369 | if (E->isTypeDependent()) |
| 3370 | ExprBits.TypeDependent = true; |
| 3371 | if (E->isValueDependent()) |
| 3372 | ExprBits.ValueDependent = true; |
| 3373 | if (E->isInstantiationDependent()) |
| 3374 | ExprBits.InstantiationDependent = true; |
| 3375 | if (E->containsUnexpandedParameterPack()) |
| 3376 | ExprBits.ContainsUnexpandedParameterPack = true; |
| 3377 | |
| 3378 | if (isa<OpaqueValueExpr>(E)) |
| 3379 | assert(cast<OpaqueValueExpr>(E)->getSourceExpr() != 0 && |
| 3380 | "opaque-value semantic expressions for pseudo-object " |
| 3381 | "operations must have sources"); |
| 3382 | } |
| 3383 | } |
| 3384 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3385 | //===----------------------------------------------------------------------===// |
Ted Kremenek | ce2fc3a | 2008-10-27 18:40:21 +0000 | [diff] [blame] | 3386 | // ExprIterator. |
| 3387 | //===----------------------------------------------------------------------===// |
| 3388 | |
| 3389 | Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); } |
| 3390 | Expr* ExprIterator::operator*() const { return cast<Expr>(*I); } |
| 3391 | Expr* ExprIterator::operator->() const { return cast<Expr>(*I); } |
| 3392 | const Expr* ConstExprIterator::operator[](size_t idx) const { |
| 3393 | return cast<Expr>(I[idx]); |
| 3394 | } |
| 3395 | const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); } |
| 3396 | const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); } |
| 3397 | |
| 3398 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 3399 | // Child Iterators for iterating over subexpressions/substatements |
| 3400 | //===----------------------------------------------------------------------===// |
| 3401 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3402 | // UnaryExprOrTypeTraitExpr |
| 3403 | Stmt::child_range UnaryExprOrTypeTraitExpr::children() { |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 3404 | // If this is of a type and the type is a VLA type (and not a typedef), the |
| 3405 | // size expression of the VLA needs to be treated as an executable expression. |
| 3406 | // Why isn't this weirdness documented better in StmtIterator? |
| 3407 | if (isArgumentType()) { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3408 | if (const VariableArrayType* T = dyn_cast<VariableArrayType>( |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 3409 | getArgumentType().getTypePtr())) |
John McCall | 63c00d7 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 3410 | return child_range(child_iterator(T), child_iterator()); |
| 3411 | return child_range(); |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 3412 | } |
John McCall | 63c00d7 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 3413 | return child_range(&Argument.Ex, &Argument.Ex + 1); |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 3414 | } |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 3415 | |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 3416 | // ObjCMessageExpr |
John McCall | 63c00d7 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 3417 | Stmt::child_range ObjCMessageExpr::children() { |
| 3418 | Stmt **begin; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 3419 | if (getReceiverKind() == Instance) |
John McCall | 63c00d7 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 3420 | begin = reinterpret_cast<Stmt **>(this + 1); |
| 3421 | else |
| 3422 | begin = reinterpret_cast<Stmt **>(getArgs()); |
| 3423 | return child_range(begin, |
| 3424 | reinterpret_cast<Stmt **>(getArgs() + getNumArgs())); |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 3425 | } |
| 3426 | |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 3427 | ObjCArrayLiteral::ObjCArrayLiteral(llvm::ArrayRef<Expr *> Elements, |
| 3428 | QualType T, ObjCMethodDecl *Method, |
| 3429 | SourceRange SR) |
| 3430 | : Expr(ObjCArrayLiteralClass, T, VK_RValue, OK_Ordinary, |
| 3431 | false, false, false, false), |
| 3432 | NumElements(Elements.size()), Range(SR), ArrayWithObjectsMethod(Method) |
| 3433 | { |
| 3434 | Expr **SaveElements = getElements(); |
| 3435 | for (unsigned I = 0, N = Elements.size(); I != N; ++I) { |
| 3436 | if (Elements[I]->isTypeDependent() || Elements[I]->isValueDependent()) |
| 3437 | ExprBits.ValueDependent = true; |
| 3438 | if (Elements[I]->isInstantiationDependent()) |
| 3439 | ExprBits.InstantiationDependent = true; |
| 3440 | if (Elements[I]->containsUnexpandedParameterPack()) |
| 3441 | ExprBits.ContainsUnexpandedParameterPack = true; |
| 3442 | |
| 3443 | SaveElements[I] = Elements[I]; |
| 3444 | } |
| 3445 | } |
| 3446 | |
| 3447 | ObjCArrayLiteral *ObjCArrayLiteral::Create(ASTContext &C, |
| 3448 | llvm::ArrayRef<Expr *> Elements, |
| 3449 | QualType T, ObjCMethodDecl * Method, |
| 3450 | SourceRange SR) { |
| 3451 | void *Mem = C.Allocate(sizeof(ObjCArrayLiteral) |
| 3452 | + Elements.size() * sizeof(Expr *)); |
| 3453 | return new (Mem) ObjCArrayLiteral(Elements, T, Method, SR); |
| 3454 | } |
| 3455 | |
| 3456 | ObjCArrayLiteral *ObjCArrayLiteral::CreateEmpty(ASTContext &C, |
| 3457 | unsigned NumElements) { |
| 3458 | |
| 3459 | void *Mem = C.Allocate(sizeof(ObjCArrayLiteral) |
| 3460 | + NumElements * sizeof(Expr *)); |
| 3461 | return new (Mem) ObjCArrayLiteral(EmptyShell(), NumElements); |
| 3462 | } |
| 3463 | |
| 3464 | ObjCDictionaryLiteral::ObjCDictionaryLiteral( |
| 3465 | ArrayRef<ObjCDictionaryElement> VK, |
| 3466 | bool HasPackExpansions, |
| 3467 | QualType T, ObjCMethodDecl *method, |
| 3468 | SourceRange SR) |
| 3469 | : Expr(ObjCDictionaryLiteralClass, T, VK_RValue, OK_Ordinary, false, false, |
| 3470 | false, false), |
| 3471 | NumElements(VK.size()), HasPackExpansions(HasPackExpansions), Range(SR), |
| 3472 | DictWithObjectsMethod(method) |
| 3473 | { |
| 3474 | KeyValuePair *KeyValues = getKeyValues(); |
| 3475 | ExpansionData *Expansions = getExpansionData(); |
| 3476 | for (unsigned I = 0; I < NumElements; I++) { |
| 3477 | if (VK[I].Key->isTypeDependent() || VK[I].Key->isValueDependent() || |
| 3478 | VK[I].Value->isTypeDependent() || VK[I].Value->isValueDependent()) |
| 3479 | ExprBits.ValueDependent = true; |
| 3480 | if (VK[I].Key->isInstantiationDependent() || |
| 3481 | VK[I].Value->isInstantiationDependent()) |
| 3482 | ExprBits.InstantiationDependent = true; |
| 3483 | if (VK[I].EllipsisLoc.isInvalid() && |
| 3484 | (VK[I].Key->containsUnexpandedParameterPack() || |
| 3485 | VK[I].Value->containsUnexpandedParameterPack())) |
| 3486 | ExprBits.ContainsUnexpandedParameterPack = true; |
| 3487 | |
| 3488 | KeyValues[I].Key = VK[I].Key; |
| 3489 | KeyValues[I].Value = VK[I].Value; |
| 3490 | if (Expansions) { |
| 3491 | Expansions[I].EllipsisLoc = VK[I].EllipsisLoc; |
| 3492 | if (VK[I].NumExpansions) |
| 3493 | Expansions[I].NumExpansionsPlusOne = *VK[I].NumExpansions + 1; |
| 3494 | else |
| 3495 | Expansions[I].NumExpansionsPlusOne = 0; |
| 3496 | } |
| 3497 | } |
| 3498 | } |
| 3499 | |
| 3500 | ObjCDictionaryLiteral * |
| 3501 | ObjCDictionaryLiteral::Create(ASTContext &C, |
| 3502 | ArrayRef<ObjCDictionaryElement> VK, |
| 3503 | bool HasPackExpansions, |
| 3504 | QualType T, ObjCMethodDecl *method, |
| 3505 | SourceRange SR) { |
| 3506 | unsigned ExpansionsSize = 0; |
| 3507 | if (HasPackExpansions) |
| 3508 | ExpansionsSize = sizeof(ExpansionData) * VK.size(); |
| 3509 | |
| 3510 | void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) + |
| 3511 | sizeof(KeyValuePair) * VK.size() + ExpansionsSize); |
| 3512 | return new (Mem) ObjCDictionaryLiteral(VK, HasPackExpansions, T, method, SR); |
| 3513 | } |
| 3514 | |
| 3515 | ObjCDictionaryLiteral * |
| 3516 | ObjCDictionaryLiteral::CreateEmpty(ASTContext &C, unsigned NumElements, |
| 3517 | bool HasPackExpansions) { |
| 3518 | unsigned ExpansionsSize = 0; |
| 3519 | if (HasPackExpansions) |
| 3520 | ExpansionsSize = sizeof(ExpansionData) * NumElements; |
| 3521 | void *Mem = C.Allocate(sizeof(ObjCDictionaryLiteral) + |
| 3522 | sizeof(KeyValuePair) * NumElements + ExpansionsSize); |
| 3523 | return new (Mem) ObjCDictionaryLiteral(EmptyShell(), NumElements, |
| 3524 | HasPackExpansions); |
| 3525 | } |
| 3526 | |
| 3527 | ObjCSubscriptRefExpr *ObjCSubscriptRefExpr::Create(ASTContext &C, |
| 3528 | Expr *base, |
| 3529 | Expr *key, QualType T, |
| 3530 | ObjCMethodDecl *getMethod, |
| 3531 | ObjCMethodDecl *setMethod, |
| 3532 | SourceLocation RB) { |
| 3533 | void *Mem = C.Allocate(sizeof(ObjCSubscriptRefExpr)); |
| 3534 | return new (Mem) ObjCSubscriptRefExpr(base, key, T, VK_LValue, |
| 3535 | OK_ObjCSubscript, |
| 3536 | getMethod, setMethod, RB); |
| 3537 | } |
Eli Friedman | dfa64ba | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 3538 | |
| 3539 | AtomicExpr::AtomicExpr(SourceLocation BLoc, Expr **args, unsigned nexpr, |
| 3540 | QualType t, AtomicOp op, SourceLocation RP) |
| 3541 | : Expr(AtomicExprClass, t, VK_RValue, OK_Ordinary, |
| 3542 | false, false, false, false), |
| 3543 | NumSubExprs(nexpr), BuiltinLoc(BLoc), RParenLoc(RP), Op(op) |
| 3544 | { |
Richard Smith | e1b2abc | 2012-04-10 22:49:28 +0000 | [diff] [blame] | 3545 | assert(nexpr == getNumSubExprs(op) && "wrong number of subexpressions"); |
Eli Friedman | dfa64ba | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 3546 | for (unsigned i = 0; i < nexpr; i++) { |
| 3547 | if (args[i]->isTypeDependent()) |
| 3548 | ExprBits.TypeDependent = true; |
| 3549 | if (args[i]->isValueDependent()) |
| 3550 | ExprBits.ValueDependent = true; |
| 3551 | if (args[i]->isInstantiationDependent()) |
| 3552 | ExprBits.InstantiationDependent = true; |
| 3553 | if (args[i]->containsUnexpandedParameterPack()) |
| 3554 | ExprBits.ContainsUnexpandedParameterPack = true; |
| 3555 | |
| 3556 | SubExprs[i] = args[i]; |
| 3557 | } |
| 3558 | } |
Richard Smith | e1b2abc | 2012-04-10 22:49:28 +0000 | [diff] [blame] | 3559 | |
| 3560 | unsigned AtomicExpr::getNumSubExprs(AtomicOp Op) { |
| 3561 | switch (Op) { |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 3562 | case AO__c11_atomic_init: |
| 3563 | case AO__c11_atomic_load: |
| 3564 | case AO__atomic_load_n: |
Richard Smith | e1b2abc | 2012-04-10 22:49:28 +0000 | [diff] [blame] | 3565 | return 2; |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 3566 | |
| 3567 | case AO__c11_atomic_store: |
| 3568 | case AO__c11_atomic_exchange: |
| 3569 | case AO__atomic_load: |
| 3570 | case AO__atomic_store: |
| 3571 | case AO__atomic_store_n: |
| 3572 | case AO__atomic_exchange_n: |
| 3573 | case AO__c11_atomic_fetch_add: |
| 3574 | case AO__c11_atomic_fetch_sub: |
| 3575 | case AO__c11_atomic_fetch_and: |
| 3576 | case AO__c11_atomic_fetch_or: |
| 3577 | case AO__c11_atomic_fetch_xor: |
| 3578 | case AO__atomic_fetch_add: |
| 3579 | case AO__atomic_fetch_sub: |
| 3580 | case AO__atomic_fetch_and: |
| 3581 | case AO__atomic_fetch_or: |
| 3582 | case AO__atomic_fetch_xor: |
Richard Smith | 51b9240 | 2012-04-13 06:31:38 +0000 | [diff] [blame] | 3583 | case AO__atomic_fetch_nand: |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 3584 | case AO__atomic_add_fetch: |
| 3585 | case AO__atomic_sub_fetch: |
| 3586 | case AO__atomic_and_fetch: |
| 3587 | case AO__atomic_or_fetch: |
| 3588 | case AO__atomic_xor_fetch: |
Richard Smith | 51b9240 | 2012-04-13 06:31:38 +0000 | [diff] [blame] | 3589 | case AO__atomic_nand_fetch: |
Richard Smith | e1b2abc | 2012-04-10 22:49:28 +0000 | [diff] [blame] | 3590 | return 3; |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 3591 | |
| 3592 | case AO__atomic_exchange: |
| 3593 | return 4; |
| 3594 | |
| 3595 | case AO__c11_atomic_compare_exchange_strong: |
| 3596 | case AO__c11_atomic_compare_exchange_weak: |
Richard Smith | e1b2abc | 2012-04-10 22:49:28 +0000 | [diff] [blame] | 3597 | return 5; |
Richard Smith | ff34d40 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 3598 | |
| 3599 | case AO__atomic_compare_exchange: |
| 3600 | case AO__atomic_compare_exchange_n: |
| 3601 | return 6; |
Richard Smith | e1b2abc | 2012-04-10 22:49:28 +0000 | [diff] [blame] | 3602 | } |
| 3603 | llvm_unreachable("unknown atomic op"); |
| 3604 | } |