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