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