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