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