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