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