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