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