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