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