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