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