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