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