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