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