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