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