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