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