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: |
Davide Italiano | 04839a5 | 2016-01-30 08:03:54 +0000 | [diff] [blame] | 988 | llvm_unreachable("unsupported CharByteWidth"); |
Eli Friedman | fcec630 | 2011-11-01 02:23:42 +0000 | [diff] [blame] | 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) { |
Etienne Bergeron | 5356d96 | 2016-05-12 20:58:56 +0000 | [diff] [blame] | 1087 | #define UNARY_OPERATION(Name, Spelling) case UO_##Name: return Spelling; |
| 1088 | #include "clang/AST/OperationKinds.def" |
Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame] | 1089 | } |
David Blaikie | f47fa30 | 2012-01-17 02:30:50 +0000 | [diff] [blame] | 1090 | llvm_unreachable("Unknown unary operator"); |
Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame] | 1091 | } |
| 1092 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1093 | UnaryOperatorKind |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 1094 | UnaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO, bool Postfix) { |
| 1095 | switch (OO) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1096 | default: llvm_unreachable("No unary operator for overloaded function"); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1097 | case OO_PlusPlus: return Postfix ? UO_PostInc : UO_PreInc; |
| 1098 | case OO_MinusMinus: return Postfix ? UO_PostDec : UO_PreDec; |
| 1099 | case OO_Amp: return UO_AddrOf; |
| 1100 | case OO_Star: return UO_Deref; |
| 1101 | case OO_Plus: return UO_Plus; |
| 1102 | case OO_Minus: return UO_Minus; |
| 1103 | case OO_Tilde: return UO_Not; |
| 1104 | case OO_Exclaim: return UO_LNot; |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 1105 | case OO_Coawait: return UO_Coawait; |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 1106 | } |
| 1107 | } |
| 1108 | |
| 1109 | OverloadedOperatorKind UnaryOperator::getOverloadedOperator(Opcode Opc) { |
| 1110 | switch (Opc) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1111 | case UO_PostInc: case UO_PreInc: return OO_PlusPlus; |
| 1112 | case UO_PostDec: case UO_PreDec: return OO_MinusMinus; |
| 1113 | case UO_AddrOf: return OO_Amp; |
| 1114 | case UO_Deref: return OO_Star; |
| 1115 | case UO_Plus: return OO_Plus; |
| 1116 | case UO_Minus: return OO_Minus; |
| 1117 | case UO_Not: return OO_Tilde; |
| 1118 | case UO_LNot: return OO_Exclaim; |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 1119 | case UO_Coawait: return OO_Coawait; |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 1120 | default: return OO_None; |
| 1121 | } |
| 1122 | } |
| 1123 | |
| 1124 | |
Chris Lattner | 0eedafe | 2006-08-24 04:56:27 +0000 | [diff] [blame] | 1125 | //===----------------------------------------------------------------------===// |
| 1126 | // Postfix Operators. |
| 1127 | //===----------------------------------------------------------------------===// |
Chris Lattner | e165d94 | 2006-08-24 04:40:38 +0000 | [diff] [blame] | 1128 | |
Justin Lebar | f8bdacb | 2016-01-14 23:31:30 +0000 | [diff] [blame] | 1129 | CallExpr::CallExpr(const ASTContext &C, StmtClass SC, Expr *fn, |
| 1130 | ArrayRef<Expr *> preargs, ArrayRef<Expr *> args, QualType t, |
Craig Topper | 3793291 | 2013-08-18 10:09:15 +0000 | [diff] [blame] | 1131 | ExprValueKind VK, SourceLocation rparenloc) |
Justin Lebar | f8bdacb | 2016-01-14 23:31:30 +0000 | [diff] [blame] | 1132 | : Expr(SC, t, VK, OK_Ordinary, fn->isTypeDependent(), |
| 1133 | fn->isValueDependent(), fn->isInstantiationDependent(), |
| 1134 | fn->containsUnexpandedParameterPack()), |
| 1135 | NumArgs(args.size()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1136 | |
Justin Lebar | f8bdacb | 2016-01-14 23:31:30 +0000 | [diff] [blame] | 1137 | unsigned NumPreArgs = preargs.size(); |
| 1138 | SubExprs = new (C) Stmt *[args.size()+PREARGS_START+NumPreArgs]; |
Douglas Gregor | 993603d | 2008-11-14 16:09:21 +0000 | [diff] [blame] | 1139 | SubExprs[FN] = fn; |
Justin Lebar | f8bdacb | 2016-01-14 23:31:30 +0000 | [diff] [blame] | 1140 | for (unsigned i = 0; i != NumPreArgs; ++i) { |
| 1141 | updateDependenciesFromArg(preargs[i]); |
| 1142 | SubExprs[i+PREARGS_START] = preargs[i]; |
| 1143 | } |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 1144 | for (unsigned i = 0; i != args.size(); ++i) { |
Justin Lebar | f8bdacb | 2016-01-14 23:31:30 +0000 | [diff] [blame] | 1145 | updateDependenciesFromArg(args[i]); |
Peter Collingbourne | 3a34725 | 2011-02-08 21:18:02 +0000 | [diff] [blame] | 1146 | SubExprs[i+PREARGS_START+NumPreArgs] = args[i]; |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 1147 | } |
Ted Kremenek | d7b4f40 | 2009-02-09 20:51:47 +0000 | [diff] [blame] | 1148 | |
Peter Collingbourne | 3a34725 | 2011-02-08 21:18:02 +0000 | [diff] [blame] | 1149 | CallExprBits.NumPreArgs = NumPreArgs; |
Douglas Gregor | 993603d | 2008-11-14 16:09:21 +0000 | [diff] [blame] | 1150 | RParenLoc = rparenloc; |
| 1151 | } |
Nate Begeman | 1e36a85 | 2008-01-17 17:46:27 +0000 | [diff] [blame] | 1152 | |
Justin Lebar | f8bdacb | 2016-01-14 23:31:30 +0000 | [diff] [blame] | 1153 | CallExpr::CallExpr(const ASTContext &C, StmtClass SC, Expr *fn, |
| 1154 | ArrayRef<Expr *> args, QualType t, ExprValueKind VK, |
| 1155 | SourceLocation rparenloc) |
| 1156 | : CallExpr(C, SC, fn, ArrayRef<Expr *>(), args, t, VK, rparenloc) {} |
| 1157 | |
Benjamin Kramer | f04f98d | 2015-03-06 14:15:57 +0000 | [diff] [blame] | 1158 | CallExpr::CallExpr(const ASTContext &C, Expr *fn, ArrayRef<Expr *> args, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1159 | QualType t, ExprValueKind VK, SourceLocation rparenloc) |
Justin Lebar | f8bdacb | 2016-01-14 23:31:30 +0000 | [diff] [blame] | 1160 | : CallExpr(C, CallExprClass, fn, ArrayRef<Expr *>(), args, t, VK, rparenloc) { |
Chris Lattner | e165d94 | 2006-08-24 04:40:38 +0000 | [diff] [blame] | 1161 | } |
| 1162 | |
Craig Topper | 3793291 | 2013-08-18 10:09:15 +0000 | [diff] [blame] | 1163 | CallExpr::CallExpr(const ASTContext &C, StmtClass SC, EmptyShell Empty) |
Benjamin Kramer | f04f98d | 2015-03-06 14:15:57 +0000 | [diff] [blame] | 1164 | : CallExpr(C, SC, /*NumPreArgs=*/0, Empty) {} |
Peter Collingbourne | 3a34725 | 2011-02-08 21:18:02 +0000 | [diff] [blame] | 1165 | |
Craig Topper | 3793291 | 2013-08-18 10:09:15 +0000 | [diff] [blame] | 1166 | CallExpr::CallExpr(const ASTContext &C, StmtClass SC, unsigned NumPreArgs, |
Peter Collingbourne | 3a34725 | 2011-02-08 21:18:02 +0000 | [diff] [blame] | 1167 | EmptyShell Empty) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1168 | : Expr(SC, Empty), SubExprs(nullptr), NumArgs(0) { |
Peter Collingbourne | 3a34725 | 2011-02-08 21:18:02 +0000 | [diff] [blame] | 1169 | // FIXME: Why do we allocate this? |
Justin Lebar | f8bdacb | 2016-01-14 23:31:30 +0000 | [diff] [blame] | 1170 | SubExprs = new (C) Stmt*[PREARGS_START+NumPreArgs](); |
Peter Collingbourne | 3a34725 | 2011-02-08 21:18:02 +0000 | [diff] [blame] | 1171 | CallExprBits.NumPreArgs = NumPreArgs; |
Douglas Gregor | e20a2e5 | 2009-04-15 17:43:59 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
Justin Lebar | f8bdacb | 2016-01-14 23:31:30 +0000 | [diff] [blame] | 1174 | void CallExpr::updateDependenciesFromArg(Expr *Arg) { |
| 1175 | if (Arg->isTypeDependent()) |
| 1176 | ExprBits.TypeDependent = true; |
| 1177 | if (Arg->isValueDependent()) |
| 1178 | ExprBits.ValueDependent = true; |
| 1179 | if (Arg->isInstantiationDependent()) |
| 1180 | ExprBits.InstantiationDependent = true; |
| 1181 | if (Arg->containsUnexpandedParameterPack()) |
| 1182 | ExprBits.ContainsUnexpandedParameterPack = true; |
| 1183 | } |
| 1184 | |
Nuno Lopes | 518e370 | 2009-12-20 23:11:08 +0000 | [diff] [blame] | 1185 | Decl *CallExpr::getCalleeDecl() { |
John McCall | e3ca8eb | 2011-09-13 23:08:34 +0000 | [diff] [blame] | 1186 | Expr *CEE = getCallee()->IgnoreParenImpCasts(); |
Douglas Gregor | e0e9630 | 2011-09-06 21:41:04 +0000 | [diff] [blame] | 1187 | |
| 1188 | while (SubstNonTypeTemplateParmExpr *NTTP |
| 1189 | = dyn_cast<SubstNonTypeTemplateParmExpr>(CEE)) { |
| 1190 | CEE = NTTP->getReplacement()->IgnoreParenCasts(); |
| 1191 | } |
| 1192 | |
Sebastian Redl | 2b1832e | 2010-09-10 20:55:30 +0000 | [diff] [blame] | 1193 | // If we're calling a dereference, look at the pointer instead. |
| 1194 | if (BinaryOperator *BO = dyn_cast<BinaryOperator>(CEE)) { |
| 1195 | if (BO->isPtrMemOp()) |
| 1196 | CEE = BO->getRHS()->IgnoreParenCasts(); |
| 1197 | } else if (UnaryOperator *UO = dyn_cast<UnaryOperator>(CEE)) { |
| 1198 | if (UO->getOpcode() == UO_Deref) |
| 1199 | CEE = UO->getSubExpr()->IgnoreParenCasts(); |
| 1200 | } |
Chris Lattner | 5230191 | 2009-07-17 15:46:27 +0000 | [diff] [blame] | 1201 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(CEE)) |
Nuno Lopes | 518e370 | 2009-12-20 23:11:08 +0000 | [diff] [blame] | 1202 | return DRE->getDecl(); |
Nuno Lopes | c095b53 | 2009-12-24 00:28:18 +0000 | [diff] [blame] | 1203 | if (MemberExpr *ME = dyn_cast<MemberExpr>(CEE)) |
| 1204 | return ME->getMemberDecl(); |
Zhongxing Xu | 3c8fa97 | 2009-07-17 07:29:51 +0000 | [diff] [blame] | 1205 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1206 | return nullptr; |
Zhongxing Xu | 3c8fa97 | 2009-07-17 07:29:51 +0000 | [diff] [blame] | 1207 | } |
| 1208 | |
Nuno Lopes | 518e370 | 2009-12-20 23:11:08 +0000 | [diff] [blame] | 1209 | FunctionDecl *CallExpr::getDirectCallee() { |
Chris Lattner | 3a6af3d | 2009-12-21 01:10:56 +0000 | [diff] [blame] | 1210 | return dyn_cast_or_null<FunctionDecl>(getCalleeDecl()); |
Nuno Lopes | 518e370 | 2009-12-20 23:11:08 +0000 | [diff] [blame] | 1211 | } |
| 1212 | |
Chris Lattner | e4407ed | 2007-12-28 05:25:02 +0000 | [diff] [blame] | 1213 | /// setNumArgs - This changes the number of arguments present in this call. |
| 1214 | /// Any orphaned expressions are deleted by this, and any new operands are set |
| 1215 | /// to null. |
Craig Topper | 3793291 | 2013-08-18 10:09:15 +0000 | [diff] [blame] | 1216 | void CallExpr::setNumArgs(const ASTContext& C, unsigned NumArgs) { |
Chris Lattner | e4407ed | 2007-12-28 05:25:02 +0000 | [diff] [blame] | 1217 | // No change, just return. |
| 1218 | if (NumArgs == getNumArgs()) return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1219 | |
Chris Lattner | e4407ed | 2007-12-28 05:25:02 +0000 | [diff] [blame] | 1220 | // If shrinking # arguments, just delete the extras and forgot them. |
| 1221 | if (NumArgs < getNumArgs()) { |
Chris Lattner | e4407ed | 2007-12-28 05:25:02 +0000 | [diff] [blame] | 1222 | this->NumArgs = NumArgs; |
| 1223 | return; |
| 1224 | } |
| 1225 | |
| 1226 | // Otherwise, we are growing the # arguments. New an bigger argument array. |
Peter Collingbourne | 3a34725 | 2011-02-08 21:18:02 +0000 | [diff] [blame] | 1227 | unsigned NumPreArgs = getNumPreArgs(); |
| 1228 | Stmt **NewSubExprs = new (C) Stmt*[NumArgs+PREARGS_START+NumPreArgs]; |
Chris Lattner | e4407ed | 2007-12-28 05:25:02 +0000 | [diff] [blame] | 1229 | // Copy over args. |
Peter Collingbourne | 3a34725 | 2011-02-08 21:18:02 +0000 | [diff] [blame] | 1230 | for (unsigned i = 0; i != getNumArgs()+PREARGS_START+NumPreArgs; ++i) |
Chris Lattner | e4407ed | 2007-12-28 05:25:02 +0000 | [diff] [blame] | 1231 | NewSubExprs[i] = SubExprs[i]; |
| 1232 | // Null out new args. |
Peter Collingbourne | 3a34725 | 2011-02-08 21:18:02 +0000 | [diff] [blame] | 1233 | for (unsigned i = getNumArgs()+PREARGS_START+NumPreArgs; |
| 1234 | i != NumArgs+PREARGS_START+NumPreArgs; ++i) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1235 | NewSubExprs[i] = nullptr; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1236 | |
Douglas Gregor | ba6e557 | 2009-04-17 21:46:47 +0000 | [diff] [blame] | 1237 | if (SubExprs) C.Deallocate(SubExprs); |
Chris Lattner | e4407ed | 2007-12-28 05:25:02 +0000 | [diff] [blame] | 1238 | SubExprs = NewSubExprs; |
| 1239 | this->NumArgs = NumArgs; |
| 1240 | } |
| 1241 | |
Alp Toker | a724cff | 2013-12-28 21:59:02 +0000 | [diff] [blame] | 1242 | /// 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] | 1243 | /// not, return 0. |
Alp Toker | a724cff | 2013-12-28 21:59:02 +0000 | [diff] [blame] | 1244 | unsigned CallExpr::getBuiltinCallee() const { |
Steve Naroff | f6e3b329 | 2008-01-31 01:07:12 +0000 | [diff] [blame] | 1245 | // 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] | 1246 | // function. As a result, we try and obtain the DeclRefExpr from the |
Steve Naroff | f6e3b329 | 2008-01-31 01:07:12 +0000 | [diff] [blame] | 1247 | // ImplicitCastExpr. |
| 1248 | const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(getCallee()); |
| 1249 | if (!ICE) // FIXME: deal with more complex calls (e.g. (func)(), (*func)()). |
Chris Lattner | 01ff98a | 2008-10-06 05:00:53 +0000 | [diff] [blame] | 1250 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1251 | |
Steve Naroff | f6e3b329 | 2008-01-31 01:07:12 +0000 | [diff] [blame] | 1252 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(ICE->getSubExpr()); |
| 1253 | if (!DRE) |
Chris Lattner | 01ff98a | 2008-10-06 05:00:53 +0000 | [diff] [blame] | 1254 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1255 | |
Anders Carlsson | fbcf676 | 2008-01-31 02:13:57 +0000 | [diff] [blame] | 1256 | const FunctionDecl *FDecl = dyn_cast<FunctionDecl>(DRE->getDecl()); |
| 1257 | if (!FDecl) |
Chris Lattner | 01ff98a | 2008-10-06 05:00:53 +0000 | [diff] [blame] | 1258 | return 0; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1259 | |
Douglas Gregor | 9eb16ea | 2008-11-21 15:30:19 +0000 | [diff] [blame] | 1260 | if (!FDecl->getIdentifier()) |
| 1261 | return 0; |
| 1262 | |
Douglas Gregor | 15fc956 | 2009-09-12 00:22:50 +0000 | [diff] [blame] | 1263 | return FDecl->getBuiltinID(); |
Chris Lattner | 01ff98a | 2008-10-06 05:00:53 +0000 | [diff] [blame] | 1264 | } |
Anders Carlsson | fbcf676 | 2008-01-31 02:13:57 +0000 | [diff] [blame] | 1265 | |
Scott Douglass | 503fc39 | 2015-06-10 13:53:15 +0000 | [diff] [blame] | 1266 | bool CallExpr::isUnevaluatedBuiltinCall(const ASTContext &Ctx) const { |
Alp Toker | a724cff | 2013-12-28 21:59:02 +0000 | [diff] [blame] | 1267 | if (unsigned BI = getBuiltinCallee()) |
Richard Smith | 5011a00 | 2013-01-17 23:46:04 +0000 | [diff] [blame] | 1268 | return Ctx.BuiltinInfo.isUnevaluated(BI); |
| 1269 | return false; |
| 1270 | } |
| 1271 | |
David Majnemer | ced8bdf | 2015-02-25 17:36:15 +0000 | [diff] [blame] | 1272 | QualType CallExpr::getCallReturnType(const ASTContext &Ctx) const { |
| 1273 | const Expr *Callee = getCallee(); |
| 1274 | QualType CalleeType = Callee->getType(); |
| 1275 | if (const auto *FnTypePtr = CalleeType->getAs<PointerType>()) { |
Anders Carlsson | 00a2759 | 2009-05-26 04:57:27 +0000 | [diff] [blame] | 1276 | CalleeType = FnTypePtr->getPointeeType(); |
David Majnemer | ced8bdf | 2015-02-25 17:36:15 +0000 | [diff] [blame] | 1277 | } else if (const auto *BPT = CalleeType->getAs<BlockPointerType>()) { |
Anders Carlsson | 00a2759 | 2009-05-26 04:57:27 +0000 | [diff] [blame] | 1278 | CalleeType = BPT->getPointeeType(); |
David Majnemer | ced8bdf | 2015-02-25 17:36:15 +0000 | [diff] [blame] | 1279 | } else if (CalleeType->isSpecificPlaceholderType(BuiltinType::BoundMember)) { |
| 1280 | if (isa<CXXPseudoDestructorExpr>(Callee->IgnoreParens())) |
| 1281 | return Ctx.VoidTy; |
| 1282 | |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 1283 | // This should never be overloaded and so should never return null. |
David Majnemer | ced8bdf | 2015-02-25 17:36:15 +0000 | [diff] [blame] | 1284 | CalleeType = Expr::findBoundMemberType(Callee); |
| 1285 | } |
| 1286 | |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 1287 | const FunctionType *FnType = CalleeType->castAs<FunctionType>(); |
Alp Toker | 314cc81 | 2014-01-25 16:55:45 +0000 | [diff] [blame] | 1288 | return FnType->getReturnType(); |
Anders Carlsson | 00a2759 | 2009-05-26 04:57:27 +0000 | [diff] [blame] | 1289 | } |
Chris Lattner | 01ff98a | 2008-10-06 05:00:53 +0000 | [diff] [blame] | 1290 | |
Daniel Dunbar | cdf295c | 2012-03-09 15:39:24 +0000 | [diff] [blame] | 1291 | SourceLocation CallExpr::getLocStart() const { |
| 1292 | if (isa<CXXOperatorCallExpr>(this)) |
Erik Verbruggen | 11a2ecc | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 1293 | return cast<CXXOperatorCallExpr>(this)->getLocStart(); |
Daniel Dunbar | cdf295c | 2012-03-09 15:39:24 +0000 | [diff] [blame] | 1294 | |
| 1295 | SourceLocation begin = getCallee()->getLocStart(); |
Keno Fischer | 070db17 | 2014-08-15 01:39:12 +0000 | [diff] [blame] | 1296 | if (begin.isInvalid() && getNumArgs() > 0 && getArg(0)) |
Daniel Dunbar | cdf295c | 2012-03-09 15:39:24 +0000 | [diff] [blame] | 1297 | begin = getArg(0)->getLocStart(); |
| 1298 | return begin; |
| 1299 | } |
| 1300 | SourceLocation CallExpr::getLocEnd() const { |
| 1301 | if (isa<CXXOperatorCallExpr>(this)) |
Erik Verbruggen | 11a2ecc | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 1302 | return cast<CXXOperatorCallExpr>(this)->getLocEnd(); |
Daniel Dunbar | cdf295c | 2012-03-09 15:39:24 +0000 | [diff] [blame] | 1303 | |
| 1304 | SourceLocation end = getRParenLoc(); |
Keno Fischer | 070db17 | 2014-08-15 01:39:12 +0000 | [diff] [blame] | 1305 | if (end.isInvalid() && getNumArgs() > 0 && getArg(getNumArgs() - 1)) |
Daniel Dunbar | cdf295c | 2012-03-09 15:39:24 +0000 | [diff] [blame] | 1306 | end = getArg(getNumArgs() - 1)->getLocEnd(); |
| 1307 | return end; |
| 1308 | } |
John McCall | 701417a | 2011-02-21 06:23:05 +0000 | [diff] [blame] | 1309 | |
Craig Topper | 3793291 | 2013-08-18 10:09:15 +0000 | [diff] [blame] | 1310 | OffsetOfExpr *OffsetOfExpr::Create(const ASTContext &C, QualType type, |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 1311 | SourceLocation OperatorLoc, |
Alexis Hunt | a8136cc | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 1312 | TypeSourceInfo *tsi, |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 1313 | ArrayRef<OffsetOfNode> comps, |
| 1314 | ArrayRef<Expr*> exprs, |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 1315 | SourceLocation RParenLoc) { |
James Y Knight | 7281c35 | 2015-12-29 22:31:18 +0000 | [diff] [blame] | 1316 | void *Mem = C.Allocate( |
| 1317 | totalSizeToAlloc<OffsetOfNode, Expr *>(comps.size(), exprs.size())); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 1318 | |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 1319 | return new (Mem) OffsetOfExpr(C, type, OperatorLoc, tsi, comps, exprs, |
| 1320 | RParenLoc); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 1321 | } |
| 1322 | |
Craig Topper | 3793291 | 2013-08-18 10:09:15 +0000 | [diff] [blame] | 1323 | OffsetOfExpr *OffsetOfExpr::CreateEmpty(const ASTContext &C, |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 1324 | unsigned numComps, unsigned numExprs) { |
James Y Knight | 7281c35 | 2015-12-29 22:31:18 +0000 | [diff] [blame] | 1325 | void *Mem = |
| 1326 | C.Allocate(totalSizeToAlloc<OffsetOfNode, Expr *>(numComps, numExprs)); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 1327 | return new (Mem) OffsetOfExpr(numComps, numExprs); |
| 1328 | } |
| 1329 | |
Craig Topper | 3793291 | 2013-08-18 10:09:15 +0000 | [diff] [blame] | 1330 | OffsetOfExpr::OffsetOfExpr(const ASTContext &C, QualType type, |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 1331 | SourceLocation OperatorLoc, TypeSourceInfo *tsi, |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 1332 | ArrayRef<OffsetOfNode> comps, ArrayRef<Expr*> exprs, |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 1333 | SourceLocation RParenLoc) |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1334 | : Expr(OffsetOfExprClass, type, VK_RValue, OK_Ordinary, |
| 1335 | /*TypeDependent=*/false, |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 1336 | /*ValueDependent=*/tsi->getType()->isDependentType(), |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 1337 | tsi->getType()->isInstantiationDependentType(), |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 1338 | tsi->getType()->containsUnexpandedParameterPack()), |
Alexis Hunt | a8136cc | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 1339 | OperatorLoc(OperatorLoc), RParenLoc(RParenLoc), TSInfo(tsi), |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 1340 | NumComps(comps.size()), NumExprs(exprs.size()) |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 1341 | { |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 1342 | for (unsigned i = 0; i != comps.size(); ++i) { |
| 1343 | setComponent(i, comps[i]); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 1344 | } |
Alexis Hunt | a8136cc | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 1345 | |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 1346 | for (unsigned i = 0; i != exprs.size(); ++i) { |
| 1347 | if (exprs[i]->isTypeDependent() || exprs[i]->isValueDependent()) |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 1348 | ExprBits.ValueDependent = true; |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 1349 | if (exprs[i]->containsUnexpandedParameterPack()) |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 1350 | ExprBits.ContainsUnexpandedParameterPack = true; |
| 1351 | |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 1352 | setIndexExpr(i, exprs[i]); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 1353 | } |
| 1354 | } |
| 1355 | |
James Y Knight | 7281c35 | 2015-12-29 22:31:18 +0000 | [diff] [blame] | 1356 | IdentifierInfo *OffsetOfNode::getFieldName() const { |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 1357 | assert(getKind() == Field || getKind() == Identifier); |
| 1358 | if (getKind() == Field) |
| 1359 | return getField()->getIdentifier(); |
Alexis Hunt | a8136cc | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 1360 | |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 1361 | return reinterpret_cast<IdentifierInfo *> (Data & ~(uintptr_t)Mask); |
| 1362 | } |
| 1363 | |
David Majnemer | 10fd83d | 2015-01-15 10:04:14 +0000 | [diff] [blame] | 1364 | UnaryExprOrTypeTraitExpr::UnaryExprOrTypeTraitExpr( |
| 1365 | UnaryExprOrTypeTrait ExprKind, Expr *E, QualType resultType, |
| 1366 | SourceLocation op, SourceLocation rp) |
| 1367 | : Expr(UnaryExprOrTypeTraitExprClass, resultType, VK_RValue, OK_Ordinary, |
| 1368 | false, // Never type-dependent (C++ [temp.dep.expr]p3). |
| 1369 | // Value-dependent if the argument is type-dependent. |
| 1370 | E->isTypeDependent(), E->isInstantiationDependent(), |
| 1371 | E->containsUnexpandedParameterPack()), |
| 1372 | OpLoc(op), RParenLoc(rp) { |
| 1373 | UnaryExprOrTypeTraitExprBits.Kind = ExprKind; |
| 1374 | UnaryExprOrTypeTraitExprBits.IsType = false; |
| 1375 | Argument.Ex = E; |
| 1376 | |
| 1377 | // Check to see if we are in the situation where alignof(decl) should be |
| 1378 | // dependent because decl's alignment is dependent. |
| 1379 | if (ExprKind == UETT_AlignOf) { |
| 1380 | if (!isValueDependent() || !isInstantiationDependent()) { |
| 1381 | E = E->IgnoreParens(); |
| 1382 | |
| 1383 | const ValueDecl *D = nullptr; |
| 1384 | if (const auto *DRE = dyn_cast<DeclRefExpr>(E)) |
| 1385 | D = DRE->getDecl(); |
| 1386 | else if (const auto *ME = dyn_cast<MemberExpr>(E)) |
| 1387 | D = ME->getMemberDecl(); |
| 1388 | |
| 1389 | if (D) { |
| 1390 | for (const auto *I : D->specific_attrs<AlignedAttr>()) { |
| 1391 | if (I->isAlignmentDependent()) { |
| 1392 | setValueDependent(true); |
| 1393 | setInstantiationDependent(true); |
| 1394 | break; |
| 1395 | } |
| 1396 | } |
| 1397 | } |
| 1398 | } |
| 1399 | } |
| 1400 | } |
| 1401 | |
Aaron Ballman | f4cb2be | 2015-03-24 15:07:53 +0000 | [diff] [blame] | 1402 | MemberExpr *MemberExpr::Create( |
| 1403 | const ASTContext &C, Expr *base, bool isarrow, SourceLocation OperatorLoc, |
| 1404 | NestedNameSpecifierLoc QualifierLoc, SourceLocation TemplateKWLoc, |
| 1405 | ValueDecl *memberdecl, DeclAccessPair founddecl, |
| 1406 | DeclarationNameInfo nameinfo, const TemplateArgumentListInfo *targs, |
| 1407 | QualType ty, ExprValueKind vk, ExprObjectKind ok) { |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1408 | |
Douglas Gregor | ea972d3 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 1409 | bool hasQualOrFound = (QualifierLoc || |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1410 | founddecl.getDecl() != memberdecl || |
| 1411 | founddecl.getAccess() != memberdecl->getAccess()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1412 | |
James Y Knight | e7d8228 | 2015-12-29 18:15:14 +0000 | [diff] [blame] | 1413 | bool HasTemplateKWAndArgsInfo = targs || TemplateKWLoc.isValid(); |
| 1414 | std::size_t Size = |
| 1415 | totalSizeToAlloc<MemberExprNameQualifier, ASTTemplateKWAndArgsInfo, |
| 1416 | TemplateArgumentLoc>(hasQualOrFound ? 1 : 0, |
| 1417 | HasTemplateKWAndArgsInfo ? 1 : 0, |
| 1418 | targs ? targs->size() : 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1419 | |
Chris Lattner | 5c0b405 | 2010-10-30 05:14:06 +0000 | [diff] [blame] | 1420 | void *Mem = C.Allocate(Size, llvm::alignOf<MemberExpr>()); |
Aaron Ballman | f4cb2be | 2015-03-24 15:07:53 +0000 | [diff] [blame] | 1421 | MemberExpr *E = new (Mem) |
| 1422 | MemberExpr(base, isarrow, OperatorLoc, memberdecl, nameinfo, ty, vk, ok); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1423 | |
| 1424 | if (hasQualOrFound) { |
Douglas Gregor | ea972d3 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 1425 | // FIXME: Wrong. We should be looking at the member declaration we found. |
| 1426 | if (QualifierLoc && QualifierLoc.getNestedNameSpecifier()->isDependent()) { |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1427 | E->setValueDependent(true); |
| 1428 | E->setTypeDependent(true); |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 1429 | E->setInstantiationDependent(true); |
| 1430 | } |
| 1431 | else if (QualifierLoc && |
| 1432 | QualifierLoc.getNestedNameSpecifier()->isInstantiationDependent()) |
| 1433 | E->setInstantiationDependent(true); |
| 1434 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1435 | E->HasQualifierOrFoundDecl = true; |
| 1436 | |
James Y Knight | e7d8228 | 2015-12-29 18:15:14 +0000 | [diff] [blame] | 1437 | MemberExprNameQualifier *NQ = |
| 1438 | E->getTrailingObjects<MemberExprNameQualifier>(); |
Douglas Gregor | ea972d3 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 1439 | NQ->QualifierLoc = QualifierLoc; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1440 | NQ->FoundDecl = founddecl; |
| 1441 | } |
| 1442 | |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1443 | E->HasTemplateKWAndArgsInfo = (targs || TemplateKWLoc.isValid()); |
| 1444 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1445 | if (targs) { |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 1446 | bool Dependent = false; |
| 1447 | bool InstantiationDependent = false; |
| 1448 | bool ContainsUnexpandedParameterPack = false; |
James Y Knight | e7d8228 | 2015-12-29 18:15:14 +0000 | [diff] [blame] | 1449 | E->getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom( |
| 1450 | TemplateKWLoc, *targs, E->getTrailingObjects<TemplateArgumentLoc>(), |
| 1451 | Dependent, InstantiationDependent, ContainsUnexpandedParameterPack); |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 1452 | if (InstantiationDependent) |
| 1453 | E->setInstantiationDependent(true); |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1454 | } else if (TemplateKWLoc.isValid()) { |
James Y Knight | e7d8228 | 2015-12-29 18:15:14 +0000 | [diff] [blame] | 1455 | E->getTrailingObjects<ASTTemplateKWAndArgsInfo>()->initializeFrom( |
| 1456 | TemplateKWLoc); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1457 | } |
| 1458 | |
| 1459 | return E; |
Douglas Gregor | f405d7e | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 1460 | } |
| 1461 | |
Daniel Dunbar | b507f27 | 2012-03-09 15:39:15 +0000 | [diff] [blame] | 1462 | SourceLocation MemberExpr::getLocStart() const { |
Douglas Gregor | 25b7e05 | 2011-03-02 21:06:53 +0000 | [diff] [blame] | 1463 | if (isImplicitAccess()) { |
| 1464 | if (hasQualifier()) |
Daniel Dunbar | b507f27 | 2012-03-09 15:39:15 +0000 | [diff] [blame] | 1465 | return getQualifierLoc().getBeginLoc(); |
| 1466 | return MemberLoc; |
Douglas Gregor | 25b7e05 | 2011-03-02 21:06:53 +0000 | [diff] [blame] | 1467 | } |
Abramo Bagnara | 7945c98 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1468 | |
Daniel Dunbar | b507f27 | 2012-03-09 15:39:15 +0000 | [diff] [blame] | 1469 | // FIXME: We don't want this to happen. Rather, we should be able to |
| 1470 | // detect all kinds of implicit accesses more cleanly. |
| 1471 | SourceLocation BaseStartLoc = getBase()->getLocStart(); |
| 1472 | if (BaseStartLoc.isValid()) |
| 1473 | return BaseStartLoc; |
| 1474 | return MemberLoc; |
| 1475 | } |
| 1476 | SourceLocation MemberExpr::getLocEnd() const { |
Abramo Bagnara | 9b836fb | 2012-11-08 13:52:58 +0000 | [diff] [blame] | 1477 | SourceLocation EndLoc = getMemberNameInfo().getEndLoc(); |
Daniel Dunbar | b507f27 | 2012-03-09 15:39:15 +0000 | [diff] [blame] | 1478 | if (hasExplicitTemplateArgs()) |
Abramo Bagnara | 9b836fb | 2012-11-08 13:52:58 +0000 | [diff] [blame] | 1479 | EndLoc = getRAngleLoc(); |
| 1480 | else if (EndLoc.isInvalid()) |
| 1481 | EndLoc = getBase()->getLocEnd(); |
| 1482 | return EndLoc; |
Douglas Gregor | 25b7e05 | 2011-03-02 21:06:53 +0000 | [diff] [blame] | 1483 | } |
| 1484 | |
Alp Toker | c108676 | 2013-12-07 13:51:35 +0000 | [diff] [blame] | 1485 | bool CastExpr::CastConsistency() const { |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 1486 | switch (getCastKind()) { |
| 1487 | case CK_DerivedToBase: |
| 1488 | case CK_UncheckedDerivedToBase: |
| 1489 | case CK_DerivedToBaseMemberPointer: |
| 1490 | case CK_BaseToDerived: |
| 1491 | case CK_BaseToDerivedMemberPointer: |
| 1492 | assert(!path_empty() && "Cast kind should have a base path!"); |
| 1493 | break; |
| 1494 | |
| 1495 | case CK_CPointerToObjCPointerCast: |
| 1496 | assert(getType()->isObjCObjectPointerType()); |
| 1497 | assert(getSubExpr()->getType()->isPointerType()); |
| 1498 | goto CheckNoBasePath; |
| 1499 | |
| 1500 | case CK_BlockPointerToObjCPointerCast: |
| 1501 | assert(getType()->isObjCObjectPointerType()); |
| 1502 | assert(getSubExpr()->getType()->isBlockPointerType()); |
| 1503 | goto CheckNoBasePath; |
| 1504 | |
John McCall | c62bb39 | 2012-02-15 01:22:51 +0000 | [diff] [blame] | 1505 | case CK_ReinterpretMemberPointer: |
| 1506 | assert(getType()->isMemberPointerType()); |
| 1507 | assert(getSubExpr()->getType()->isMemberPointerType()); |
| 1508 | goto CheckNoBasePath; |
| 1509 | |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 1510 | case CK_BitCast: |
| 1511 | // Arbitrary casts to C pointer types count as bitcasts. |
| 1512 | // Otherwise, we should only have block and ObjC pointer casts |
| 1513 | // here if they stay within the type kind. |
| 1514 | if (!getType()->isPointerType()) { |
| 1515 | assert(getType()->isObjCObjectPointerType() == |
| 1516 | getSubExpr()->getType()->isObjCObjectPointerType()); |
| 1517 | assert(getType()->isBlockPointerType() == |
| 1518 | getSubExpr()->getType()->isBlockPointerType()); |
| 1519 | } |
| 1520 | goto CheckNoBasePath; |
| 1521 | |
| 1522 | case CK_AnyPointerToBlockPointerCast: |
| 1523 | assert(getType()->isBlockPointerType()); |
| 1524 | assert(getSubExpr()->getType()->isAnyPointerType() && |
| 1525 | !getSubExpr()->getType()->isBlockPointerType()); |
| 1526 | goto CheckNoBasePath; |
| 1527 | |
Douglas Gregor | ed90df3 | 2012-02-22 05:02:47 +0000 | [diff] [blame] | 1528 | case CK_CopyAndAutoreleaseBlockObject: |
| 1529 | assert(getType()->isBlockPointerType()); |
| 1530 | assert(getSubExpr()->getType()->isBlockPointerType()); |
| 1531 | goto CheckNoBasePath; |
Eli Friedman | 34866c7 | 2012-08-31 00:14:07 +0000 | [diff] [blame] | 1532 | |
| 1533 | case CK_FunctionToPointerDecay: |
| 1534 | assert(getType()->isPointerType()); |
| 1535 | assert(getSubExpr()->getType()->isFunctionType()); |
| 1536 | goto CheckNoBasePath; |
| 1537 | |
David Tweed | e146832 | 2013-12-11 13:39:46 +0000 | [diff] [blame] | 1538 | case CK_AddressSpaceConversion: |
| 1539 | assert(getType()->isPointerType()); |
| 1540 | assert(getSubExpr()->getType()->isPointerType()); |
| 1541 | assert(getType()->getPointeeType().getAddressSpace() != |
| 1542 | getSubExpr()->getType()->getPointeeType().getAddressSpace()); |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 1543 | // These should not have an inheritance path. |
| 1544 | case CK_Dynamic: |
| 1545 | case CK_ToUnion: |
| 1546 | case CK_ArrayToPointerDecay: |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 1547 | case CK_NullToMemberPointer: |
| 1548 | case CK_NullToPointer: |
| 1549 | case CK_ConstructorConversion: |
| 1550 | case CK_IntegralToPointer: |
| 1551 | case CK_PointerToIntegral: |
| 1552 | case CK_ToVoid: |
| 1553 | case CK_VectorSplat: |
| 1554 | case CK_IntegralCast: |
George Burgess IV | df1ed00 | 2016-01-13 01:52:39 +0000 | [diff] [blame] | 1555 | case CK_BooleanToSignedIntegral: |
John McCall | 9320b87 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 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()) { |
Etienne Bergeron | 5356d96 | 2016-05-12 20:58:56 +0000 | [diff] [blame] | 1599 | #define CAST_OPERATION(Name) case CK_##Name: return #Name; |
| 1600 | #include "clang/AST/OperationKinds.def" |
Anders Carlsson | 496335e | 2009-09-03 00:59:21 +0000 | [diff] [blame] | 1601 | } |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 1602 | llvm_unreachable("Unhandled cast kind!"); |
Anders Carlsson | 496335e | 2009-09-03 00:59:21 +0000 | [diff] [blame] | 1603 | } |
| 1604 | |
Douglas Gregor | d196a58 | 2009-12-14 19:27:10 +0000 | [diff] [blame] | 1605 | Expr *CastExpr::getSubExprAsWritten() { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1606 | Expr *SubExpr = nullptr; |
Douglas Gregor | d196a58 | 2009-12-14 19:27:10 +0000 | [diff] [blame] | 1607 | CastExpr *E = this; |
| 1608 | do { |
| 1609 | SubExpr = E->getSubExpr(); |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 1610 | |
| 1611 | // Skip through reference binding to temporary. |
| 1612 | if (MaterializeTemporaryExpr *Materialize |
| 1613 | = dyn_cast<MaterializeTemporaryExpr>(SubExpr)) |
| 1614 | SubExpr = Materialize->GetTemporaryExpr(); |
| 1615 | |
Douglas Gregor | d196a58 | 2009-12-14 19:27:10 +0000 | [diff] [blame] | 1616 | // Skip any temporary bindings; they're implicit. |
| 1617 | if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr)) |
| 1618 | SubExpr = Binder->getSubExpr(); |
Alexis Hunt | a8136cc | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 1619 | |
Douglas Gregor | d196a58 | 2009-12-14 19:27:10 +0000 | [diff] [blame] | 1620 | // Conversions by constructor and conversion functions have a |
| 1621 | // subexpression describing the call; strip it off. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1622 | if (E->getCastKind() == CK_ConstructorConversion) |
Douglas Gregor | d196a58 | 2009-12-14 19:27:10 +0000 | [diff] [blame] | 1623 | SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0); |
Manman Ren | 8abc2e5 | 2016-02-02 22:23:03 +0000 | [diff] [blame] | 1624 | else if (E->getCastKind() == CK_UserDefinedConversion) { |
| 1625 | assert((isa<CXXMemberCallExpr>(SubExpr) || |
| 1626 | isa<BlockExpr>(SubExpr)) && |
| 1627 | "Unexpected SubExpr for CK_UserDefinedConversion."); |
| 1628 | if (isa<CXXMemberCallExpr>(SubExpr)) |
| 1629 | SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument(); |
| 1630 | } |
Alexis Hunt | a8136cc | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 1631 | |
Douglas Gregor | d196a58 | 2009-12-14 19:27:10 +0000 | [diff] [blame] | 1632 | // If the subexpression we're left with is an implicit cast, look |
| 1633 | // through that, too. |
Alexis Hunt | a8136cc | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 1634 | } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr))); |
| 1635 | |
Douglas Gregor | d196a58 | 2009-12-14 19:27:10 +0000 | [diff] [blame] | 1636 | return SubExpr; |
| 1637 | } |
| 1638 | |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1639 | CXXBaseSpecifier **CastExpr::path_buffer() { |
| 1640 | switch (getStmtClass()) { |
| 1641 | #define ABSTRACT_STMT(x) |
James Y Knight | 1d75c5e | 2015-12-30 02:27:28 +0000 | [diff] [blame] | 1642 | #define CASTEXPR(Type, Base) \ |
| 1643 | case Stmt::Type##Class: \ |
| 1644 | return static_cast<Type *>(this)->getTrailingObjects<CXXBaseSpecifier *>(); |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1645 | #define STMT(Type, Base) |
| 1646 | #include "clang/AST/StmtNodes.inc" |
| 1647 | default: |
| 1648 | llvm_unreachable("non-cast expressions not possible here"); |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1649 | } |
| 1650 | } |
| 1651 | |
Craig Topper | 3793291 | 2013-08-18 10:09:15 +0000 | [diff] [blame] | 1652 | ImplicitCastExpr *ImplicitCastExpr::Create(const ASTContext &C, QualType T, |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1653 | CastKind Kind, Expr *Operand, |
| 1654 | const CXXCastPath *BasePath, |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 1655 | ExprValueKind VK) { |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1656 | unsigned PathSize = (BasePath ? BasePath->size() : 0); |
James Y Knight | 1d75c5e | 2015-12-30 02:27:28 +0000 | [diff] [blame] | 1657 | void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize)); |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1658 | ImplicitCastExpr *E = |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 1659 | new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK); |
James Y Knight | 1d75c5e | 2015-12-30 02:27:28 +0000 | [diff] [blame] | 1660 | if (PathSize) |
| 1661 | std::uninitialized_copy_n(BasePath->data(), BasePath->size(), |
| 1662 | E->getTrailingObjects<CXXBaseSpecifier *>()); |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1663 | return E; |
| 1664 | } |
| 1665 | |
Craig Topper | 3793291 | 2013-08-18 10:09:15 +0000 | [diff] [blame] | 1666 | ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(const ASTContext &C, |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1667 | unsigned PathSize) { |
James Y Knight | 1d75c5e | 2015-12-30 02:27:28 +0000 | [diff] [blame] | 1668 | void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize)); |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1669 | return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize); |
| 1670 | } |
| 1671 | |
| 1672 | |
Craig Topper | 3793291 | 2013-08-18 10:09:15 +0000 | [diff] [blame] | 1673 | CStyleCastExpr *CStyleCastExpr::Create(const ASTContext &C, QualType T, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1674 | ExprValueKind VK, CastKind K, Expr *Op, |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1675 | const CXXCastPath *BasePath, |
| 1676 | TypeSourceInfo *WrittenTy, |
| 1677 | SourceLocation L, SourceLocation R) { |
| 1678 | unsigned PathSize = (BasePath ? BasePath->size() : 0); |
James Y Knight | 1d75c5e | 2015-12-30 02:27:28 +0000 | [diff] [blame] | 1679 | void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize)); |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1680 | CStyleCastExpr *E = |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1681 | new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R); |
James Y Knight | 1d75c5e | 2015-12-30 02:27:28 +0000 | [diff] [blame] | 1682 | if (PathSize) |
| 1683 | std::uninitialized_copy_n(BasePath->data(), BasePath->size(), |
| 1684 | E->getTrailingObjects<CXXBaseSpecifier *>()); |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1685 | return E; |
| 1686 | } |
| 1687 | |
Craig Topper | 3793291 | 2013-08-18 10:09:15 +0000 | [diff] [blame] | 1688 | CStyleCastExpr *CStyleCastExpr::CreateEmpty(const ASTContext &C, |
| 1689 | unsigned PathSize) { |
James Y Knight | 1d75c5e | 2015-12-30 02:27:28 +0000 | [diff] [blame] | 1690 | void *Buffer = C.Allocate(totalSizeToAlloc<CXXBaseSpecifier *>(PathSize)); |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 1691 | return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize); |
| 1692 | } |
| 1693 | |
Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame] | 1694 | /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it |
| 1695 | /// corresponds to, e.g. "<<=". |
David Blaikie | 1d202a6 | 2012-10-08 01:11:04 +0000 | [diff] [blame] | 1696 | StringRef BinaryOperator::getOpcodeStr(Opcode Op) { |
Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame] | 1697 | switch (Op) { |
Etienne Bergeron | 5356d96 | 2016-05-12 20:58:56 +0000 | [diff] [blame] | 1698 | #define BINARY_OPERATION(Name, Spelling) case BO_##Name: return Spelling; |
| 1699 | #include "clang/AST/OperationKinds.def" |
Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame] | 1700 | } |
David Blaikie | e4d798f | 2012-01-20 21:50:17 +0000 | [diff] [blame] | 1701 | llvm_unreachable("Invalid OpCode!"); |
Chris Lattner | 1b92649 | 2006-08-23 06:42:10 +0000 | [diff] [blame] | 1702 | } |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 1703 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1704 | BinaryOperatorKind |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 1705 | BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) { |
| 1706 | switch (OO) { |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 1707 | default: llvm_unreachable("Not an overloadable binary operator"); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1708 | case OO_Plus: return BO_Add; |
| 1709 | case OO_Minus: return BO_Sub; |
| 1710 | case OO_Star: return BO_Mul; |
| 1711 | case OO_Slash: return BO_Div; |
| 1712 | case OO_Percent: return BO_Rem; |
| 1713 | case OO_Caret: return BO_Xor; |
| 1714 | case OO_Amp: return BO_And; |
| 1715 | case OO_Pipe: return BO_Or; |
| 1716 | case OO_Equal: return BO_Assign; |
| 1717 | case OO_Less: return BO_LT; |
| 1718 | case OO_Greater: return BO_GT; |
| 1719 | case OO_PlusEqual: return BO_AddAssign; |
| 1720 | case OO_MinusEqual: return BO_SubAssign; |
| 1721 | case OO_StarEqual: return BO_MulAssign; |
| 1722 | case OO_SlashEqual: return BO_DivAssign; |
| 1723 | case OO_PercentEqual: return BO_RemAssign; |
| 1724 | case OO_CaretEqual: return BO_XorAssign; |
| 1725 | case OO_AmpEqual: return BO_AndAssign; |
| 1726 | case OO_PipeEqual: return BO_OrAssign; |
| 1727 | case OO_LessLess: return BO_Shl; |
| 1728 | case OO_GreaterGreater: return BO_Shr; |
| 1729 | case OO_LessLessEqual: return BO_ShlAssign; |
| 1730 | case OO_GreaterGreaterEqual: return BO_ShrAssign; |
| 1731 | case OO_EqualEqual: return BO_EQ; |
| 1732 | case OO_ExclaimEqual: return BO_NE; |
| 1733 | case OO_LessEqual: return BO_LE; |
| 1734 | case OO_GreaterEqual: return BO_GE; |
| 1735 | case OO_AmpAmp: return BO_LAnd; |
| 1736 | case OO_PipePipe: return BO_LOr; |
| 1737 | case OO_Comma: return BO_Comma; |
| 1738 | case OO_ArrowStar: return BO_PtrMemI; |
Douglas Gregor | 1baf54e | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 1739 | } |
| 1740 | } |
| 1741 | |
| 1742 | OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) { |
| 1743 | static const OverloadedOperatorKind OverOps[] = { |
| 1744 | /* .* Cannot be overloaded */OO_None, OO_ArrowStar, |
| 1745 | OO_Star, OO_Slash, OO_Percent, |
| 1746 | OO_Plus, OO_Minus, |
| 1747 | OO_LessLess, OO_GreaterGreater, |
| 1748 | OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual, |
| 1749 | OO_EqualEqual, OO_ExclaimEqual, |
| 1750 | OO_Amp, |
| 1751 | OO_Caret, |
| 1752 | OO_Pipe, |
| 1753 | OO_AmpAmp, |
| 1754 | OO_PipePipe, |
| 1755 | OO_Equal, OO_StarEqual, |
| 1756 | OO_SlashEqual, OO_PercentEqual, |
| 1757 | OO_PlusEqual, OO_MinusEqual, |
| 1758 | OO_LessLessEqual, OO_GreaterGreaterEqual, |
| 1759 | OO_AmpEqual, OO_CaretEqual, |
| 1760 | OO_PipeEqual, |
| 1761 | OO_Comma |
| 1762 | }; |
| 1763 | return OverOps[Opc]; |
| 1764 | } |
| 1765 | |
Craig Topper | 3793291 | 2013-08-18 10:09:15 +0000 | [diff] [blame] | 1766 | InitListExpr::InitListExpr(const ASTContext &C, SourceLocation lbraceloc, |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 1767 | ArrayRef<Expr*> initExprs, SourceLocation rbraceloc) |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 1768 | : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false, |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 1769 | false, false), |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 1770 | InitExprs(C, initExprs.size()), |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1771 | LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), AltForm(nullptr, true) |
Sebastian Redl | c83ed82 | 2012-02-17 08:42:25 +0000 | [diff] [blame] | 1772 | { |
| 1773 | sawArrayRangeDesignator(false); |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 1774 | for (unsigned I = 0; I != initExprs.size(); ++I) { |
Ted Kremenek | 013041e | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 1775 | if (initExprs[I]->isTypeDependent()) |
John McCall | 925b1662 | 2010-10-26 08:39:16 +0000 | [diff] [blame] | 1776 | ExprBits.TypeDependent = true; |
Ted Kremenek | 013041e | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 1777 | if (initExprs[I]->isValueDependent()) |
John McCall | 925b1662 | 2010-10-26 08:39:16 +0000 | [diff] [blame] | 1778 | ExprBits.ValueDependent = true; |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 1779 | if (initExprs[I]->isInstantiationDependent()) |
| 1780 | ExprBits.InstantiationDependent = true; |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 1781 | if (initExprs[I]->containsUnexpandedParameterPack()) |
| 1782 | ExprBits.ContainsUnexpandedParameterPack = true; |
Douglas Gregor | deebf6e | 2009-11-19 23:25:22 +0000 | [diff] [blame] | 1783 | } |
Alexis Hunt | a8136cc | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 1784 | |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 1785 | InitExprs.insert(C, InitExprs.end(), initExprs.begin(), initExprs.end()); |
Anders Carlsson | 4692db0 | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 1786 | } |
Chris Lattner | 1ec5f56 | 2007-06-27 05:38:08 +0000 | [diff] [blame] | 1787 | |
Craig Topper | 3793291 | 2013-08-18 10:09:15 +0000 | [diff] [blame] | 1788 | void InitListExpr::reserveInits(const ASTContext &C, unsigned NumInits) { |
Ted Kremenek | 013041e | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 1789 | if (NumInits > InitExprs.size()) |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1790 | InitExprs.reserve(C, NumInits); |
Douglas Gregor | 6d00c99 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1791 | } |
| 1792 | |
Craig Topper | 3793291 | 2013-08-18 10:09:15 +0000 | [diff] [blame] | 1793 | void InitListExpr::resizeInits(const ASTContext &C, unsigned NumInits) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1794 | InitExprs.resize(C, NumInits, nullptr); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1795 | } |
| 1796 | |
Craig Topper | 3793291 | 2013-08-18 10:09:15 +0000 | [diff] [blame] | 1797 | Expr *InitListExpr::updateInit(const ASTContext &C, unsigned Init, Expr *expr) { |
Ted Kremenek | 013041e | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 1798 | if (Init >= InitExprs.size()) { |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1799 | InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, nullptr); |
Richard Smith | c275da6 | 2013-12-06 01:27:24 +0000 | [diff] [blame] | 1800 | setInit(Init, expr); |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1801 | return nullptr; |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1802 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1803 | |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1804 | Expr *Result = cast_or_null<Expr>(InitExprs[Init]); |
Richard Smith | c275da6 | 2013-12-06 01:27:24 +0000 | [diff] [blame] | 1805 | setInit(Init, expr); |
Douglas Gregor | 347f7ea | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1806 | return Result; |
| 1807 | } |
| 1808 | |
Argyrios Kyrtzidis | 446bcf2 | 2011-04-21 20:03:38 +0000 | [diff] [blame] | 1809 | void InitListExpr::setArrayFiller(Expr *filler) { |
Argyrios Kyrtzidis | d4590a5d | 2011-10-21 23:02:22 +0000 | [diff] [blame] | 1810 | assert(!hasArrayFiller() && "Filler already set!"); |
Argyrios Kyrtzidis | 446bcf2 | 2011-04-21 20:03:38 +0000 | [diff] [blame] | 1811 | ArrayFillerOrUnionFieldInit = filler; |
| 1812 | // Fill out any "holes" in the array due to designated initializers. |
| 1813 | Expr **inits = getInits(); |
| 1814 | for (unsigned i = 0, e = getNumInits(); i != e; ++i) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 1815 | if (inits[i] == nullptr) |
Argyrios Kyrtzidis | 446bcf2 | 2011-04-21 20:03:38 +0000 | [diff] [blame] | 1816 | inits[i] = filler; |
| 1817 | } |
| 1818 | |
Richard Smith | 9ec1e48 | 2012-04-15 02:50:59 +0000 | [diff] [blame] | 1819 | bool InitListExpr::isStringLiteralInit() const { |
| 1820 | if (getNumInits() != 1) |
| 1821 | return false; |
Eli Friedman | cf4ab08 | 2012-08-20 20:55:45 +0000 | [diff] [blame] | 1822 | const ArrayType *AT = getType()->getAsArrayTypeUnsafe(); |
| 1823 | if (!AT || !AT->getElementType()->isIntegerType()) |
Richard Smith | 9ec1e48 | 2012-04-15 02:50:59 +0000 | [diff] [blame] | 1824 | return false; |
Ted Kremenek | 256bd96 | 2014-01-19 06:31:34 +0000 | [diff] [blame] | 1825 | // It is possible for getInit() to return null. |
| 1826 | const Expr *Init = getInit(0); |
| 1827 | if (!Init) |
| 1828 | return false; |
| 1829 | Init = Init->IgnoreParens(); |
Richard Smith | 9ec1e48 | 2012-04-15 02:50:59 +0000 | [diff] [blame] | 1830 | return isa<StringLiteral>(Init) || isa<ObjCEncodeExpr>(Init); |
| 1831 | } |
| 1832 | |
Erik Verbruggen | 11a2ecc | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 1833 | SourceLocation InitListExpr::getLocStart() const { |
Abramo Bagnara | 8d16bd4 | 2012-11-08 18:41:43 +0000 | [diff] [blame] | 1834 | if (InitListExpr *SyntacticForm = getSyntacticForm()) |
Erik Verbruggen | 11a2ecc | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 1835 | return SyntacticForm->getLocStart(); |
| 1836 | SourceLocation Beg = LBraceLoc; |
Ted Kremenek | 16e6026f | 2010-11-09 02:11:40 +0000 | [diff] [blame] | 1837 | if (Beg.isInvalid()) { |
| 1838 | // Find the first non-null initializer. |
| 1839 | for (InitExprsTy::const_iterator I = InitExprs.begin(), |
| 1840 | E = InitExprs.end(); |
| 1841 | I != E; ++I) { |
| 1842 | if (Stmt *S = *I) { |
| 1843 | Beg = S->getLocStart(); |
| 1844 | break; |
| 1845 | } |
| 1846 | } |
| 1847 | } |
Erik Verbruggen | 11a2ecc | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 1848 | return Beg; |
| 1849 | } |
| 1850 | |
| 1851 | SourceLocation InitListExpr::getLocEnd() const { |
| 1852 | if (InitListExpr *SyntacticForm = getSyntacticForm()) |
| 1853 | return SyntacticForm->getLocEnd(); |
| 1854 | SourceLocation End = RBraceLoc; |
Ted Kremenek | 16e6026f | 2010-11-09 02:11:40 +0000 | [diff] [blame] | 1855 | if (End.isInvalid()) { |
| 1856 | // Find the first non-null initializer from the end. |
| 1857 | for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(), |
Erik Verbruggen | 11a2ecc | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 1858 | E = InitExprs.rend(); |
| 1859 | I != E; ++I) { |
Ted Kremenek | 16e6026f | 2010-11-09 02:11:40 +0000 | [diff] [blame] | 1860 | if (Stmt *S = *I) { |
Erik Verbruggen | 11a2ecc | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 1861 | End = S->getLocEnd(); |
Ted Kremenek | 16e6026f | 2010-11-09 02:11:40 +0000 | [diff] [blame] | 1862 | break; |
Erik Verbruggen | 11a2ecc | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 1863 | } |
Ted Kremenek | 16e6026f | 2010-11-09 02:11:40 +0000 | [diff] [blame] | 1864 | } |
| 1865 | } |
Erik Verbruggen | 11a2ecc | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 1866 | return End; |
Ted Kremenek | 16e6026f | 2010-11-09 02:11:40 +0000 | [diff] [blame] | 1867 | } |
| 1868 | |
Steve Naroff | 991e99d | 2008-09-04 15:31:07 +0000 | [diff] [blame] | 1869 | /// getFunctionType - Return the underlying function type for this block. |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 1870 | /// |
John McCall | c833dea | 2012-02-17 03:32:35 +0000 | [diff] [blame] | 1871 | const FunctionProtoType *BlockExpr::getFunctionType() const { |
| 1872 | // The block pointer is never sugared, but the function type might be. |
| 1873 | return cast<BlockPointerType>(getType()) |
| 1874 | ->getPointeeType()->castAs<FunctionProtoType>(); |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 1875 | } |
| 1876 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1877 | SourceLocation BlockExpr::getCaretLocation() const { |
| 1878 | return TheBlock->getCaretLocation(); |
Steve Naroff | 415d3d5 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 1879 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1880 | const Stmt *BlockExpr::getBody() const { |
Douglas Gregor | e3dcb2d | 2009-04-18 00:02:19 +0000 | [diff] [blame] | 1881 | return TheBlock->getBody(); |
| 1882 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1883 | Stmt *BlockExpr::getBody() { |
| 1884 | return TheBlock->getBody(); |
Douglas Gregor | e3dcb2d | 2009-04-18 00:02:19 +0000 | [diff] [blame] | 1885 | } |
Steve Naroff | 415d3d5 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 1886 | |
| 1887 | |
Chris Lattner | 1ec5f56 | 2007-06-27 05:38:08 +0000 | [diff] [blame] | 1888 | //===----------------------------------------------------------------------===// |
| 1889 | // Generic Expression Routines |
| 1890 | //===----------------------------------------------------------------------===// |
| 1891 | |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1892 | /// isUnusedResultAWarning - Return true if this immediate expression should |
| 1893 | /// be warned about if the result is unused. If so, fill in Loc and Ranges |
| 1894 | /// with location to warn on and the source range[s] to report with the |
| 1895 | /// warning. |
Eli Friedman | c11535c | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 1896 | bool Expr::isUnusedResultAWarning(const Expr *&WarnE, SourceLocation &Loc, |
| 1897 | SourceRange &R1, SourceRange &R2, |
| 1898 | ASTContext &Ctx) const { |
Anders Carlsson | 789e2cc | 2009-05-15 23:10:19 +0000 | [diff] [blame] | 1899 | // Don't warn if the expr is type dependent. The type could end up |
| 1900 | // instantiating to void. |
| 1901 | if (isTypeDependent()) |
| 1902 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1903 | |
Chris Lattner | 1ec5f56 | 2007-06-27 05:38:08 +0000 | [diff] [blame] | 1904 | switch (getStmtClass()) { |
| 1905 | default: |
John McCall | c493a73 | 2010-03-12 07:11:26 +0000 | [diff] [blame] | 1906 | if (getType()->isVoidType()) |
| 1907 | return false; |
Eli Friedman | c11535c | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 1908 | WarnE = this; |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1909 | Loc = getExprLoc(); |
| 1910 | R1 = getSourceRange(); |
| 1911 | return true; |
Chris Lattner | 1ec5f56 | 2007-06-27 05:38:08 +0000 | [diff] [blame] | 1912 | case ParenExprClass: |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1913 | return cast<ParenExpr>(this)->getSubExpr()-> |
Eli Friedman | c11535c | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 1914 | isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx); |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1915 | case GenericSelectionExprClass: |
| 1916 | return cast<GenericSelectionExpr>(this)->getResultExpr()-> |
Eli Friedman | c11535c | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 1917 | isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx); |
Eli Friedman | 75807f2 | 2013-07-20 00:40:58 +0000 | [diff] [blame] | 1918 | case ChooseExprClass: |
| 1919 | return cast<ChooseExpr>(this)->getChosenSubExpr()-> |
| 1920 | isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx); |
Chris Lattner | 1ec5f56 | 2007-06-27 05:38:08 +0000 | [diff] [blame] | 1921 | case UnaryOperatorClass: { |
| 1922 | const UnaryOperator *UO = cast<UnaryOperator>(this); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1923 | |
Chris Lattner | 1ec5f56 | 2007-06-27 05:38:08 +0000 | [diff] [blame] | 1924 | switch (UO->getOpcode()) { |
Eli Friedman | c11535c | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 1925 | case UO_Plus: |
| 1926 | case UO_Minus: |
| 1927 | case UO_AddrOf: |
| 1928 | case UO_Not: |
| 1929 | case UO_LNot: |
| 1930 | case UO_Deref: |
| 1931 | break; |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 1932 | case UO_Coawait: |
| 1933 | // This is just the 'operator co_await' call inside the guts of a |
| 1934 | // dependent co_await call. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1935 | case UO_PostInc: |
| 1936 | case UO_PostDec: |
| 1937 | case UO_PreInc: |
| 1938 | case UO_PreDec: // ++/-- |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1939 | return false; // Not a warning. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1940 | case UO_Real: |
| 1941 | case UO_Imag: |
Chris Lattner | a44d116 | 2007-06-27 05:58:59 +0000 | [diff] [blame] | 1942 | // accessing a piece of a volatile complex is a side-effect. |
Mike Stump | 53f9ded | 2009-11-03 23:25:48 +0000 | [diff] [blame] | 1943 | if (Ctx.getCanonicalType(UO->getSubExpr()->getType()) |
| 1944 | .isVolatileQualified()) |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1945 | return false; |
| 1946 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1947 | case UO_Extension: |
Eli Friedman | c11535c | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 1948 | return UO->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx); |
Chris Lattner | 1ec5f56 | 2007-06-27 05:38:08 +0000 | [diff] [blame] | 1949 | } |
Eli Friedman | c11535c | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 1950 | WarnE = this; |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1951 | Loc = UO->getOperatorLoc(); |
| 1952 | R1 = UO->getSubExpr()->getSourceRange(); |
| 1953 | return true; |
Chris Lattner | 1ec5f56 | 2007-06-27 05:38:08 +0000 | [diff] [blame] | 1954 | } |
Chris Lattner | ae7a834 | 2007-12-01 06:07:34 +0000 | [diff] [blame] | 1955 | case BinaryOperatorClass: { |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1956 | const BinaryOperator *BO = cast<BinaryOperator>(this); |
Ted Kremenek | 43a9c96 | 2010-04-07 18:49:21 +0000 | [diff] [blame] | 1957 | switch (BO->getOpcode()) { |
| 1958 | default: |
| 1959 | break; |
Argyrios Kyrtzidis | 639ffb0 | 2010-06-30 10:53:14 +0000 | [diff] [blame] | 1960 | // Consider the RHS of comma for side effects. LHS was checked by |
| 1961 | // Sema::CheckCommaOperands. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1962 | case BO_Comma: |
Ted Kremenek | 43a9c96 | 2010-04-07 18:49:21 +0000 | [diff] [blame] | 1963 | // ((foo = <blah>), 0) is an idiom for hiding the result (and |
| 1964 | // lvalue-ness) of an assignment written in a macro. |
| 1965 | if (IntegerLiteral *IE = |
| 1966 | dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens())) |
| 1967 | if (IE->getValue() == 0) |
| 1968 | return false; |
Eli Friedman | c11535c | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 1969 | return BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx); |
Argyrios Kyrtzidis | 639ffb0 | 2010-06-30 10:53:14 +0000 | [diff] [blame] | 1970 | // Consider '||', '&&' to have side effects if the LHS or RHS does. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1971 | case BO_LAnd: |
| 1972 | case BO_LOr: |
Eli Friedman | c11535c | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 1973 | if (!BO->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx) || |
| 1974 | !BO->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx)) |
Argyrios Kyrtzidis | 639ffb0 | 2010-06-30 10:53:14 +0000 | [diff] [blame] | 1975 | return false; |
| 1976 | break; |
John McCall | 1e3715a | 2010-02-16 04:10:53 +0000 | [diff] [blame] | 1977 | } |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1978 | if (BO->isAssignmentOp()) |
| 1979 | return false; |
Eli Friedman | c11535c | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 1980 | WarnE = this; |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1981 | Loc = BO->getOperatorLoc(); |
| 1982 | R1 = BO->getLHS()->getSourceRange(); |
| 1983 | R2 = BO->getRHS()->getSourceRange(); |
| 1984 | return true; |
Chris Lattner | ae7a834 | 2007-12-01 06:07:34 +0000 | [diff] [blame] | 1985 | } |
Chris Lattner | 8692811 | 2007-08-25 02:00:02 +0000 | [diff] [blame] | 1986 | case CompoundAssignOperatorClass: |
Douglas Gregor | 0bbe94d | 2010-05-08 22:41:50 +0000 | [diff] [blame] | 1987 | case VAArgExprClass: |
Eli Friedman | df14b3a | 2011-10-11 02:20:01 +0000 | [diff] [blame] | 1988 | case AtomicExprClass: |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1989 | return false; |
Chris Lattner | 1ec5f56 | 2007-06-27 05:38:08 +0000 | [diff] [blame] | 1990 | |
Fariborz Jahanian | 9fac54d | 2007-12-01 19:58:28 +0000 | [diff] [blame] | 1991 | case ConditionalOperatorClass: { |
Ted Kremenek | e96dad9 | 2011-03-01 20:34:48 +0000 | [diff] [blame] | 1992 | // If only one of the LHS or RHS is a warning, the operator might |
| 1993 | // be being used for control flow. Only warn if both the LHS and |
| 1994 | // RHS are warnings. |
Fariborz Jahanian | 9fac54d | 2007-12-01 19:58:28 +0000 | [diff] [blame] | 1995 | const ConditionalOperator *Exp = cast<ConditionalOperator>(this); |
Eli Friedman | c11535c | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 1996 | if (!Exp->getRHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx)) |
Ted Kremenek | e96dad9 | 2011-03-01 20:34:48 +0000 | [diff] [blame] | 1997 | return false; |
| 1998 | if (!Exp->getLHS()) |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1999 | return true; |
Eli Friedman | c11535c | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 2000 | return Exp->getLHS()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx); |
Fariborz Jahanian | 9fac54d | 2007-12-01 19:58:28 +0000 | [diff] [blame] | 2001 | } |
| 2002 | |
Chris Lattner | a44d116 | 2007-06-27 05:58:59 +0000 | [diff] [blame] | 2003 | case MemberExprClass: |
Eli Friedman | c11535c | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 2004 | WarnE = this; |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2005 | Loc = cast<MemberExpr>(this)->getMemberLoc(); |
| 2006 | R1 = SourceRange(Loc, Loc); |
| 2007 | R2 = cast<MemberExpr>(this)->getBase()->getSourceRange(); |
| 2008 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2009 | |
Chris Lattner | 1ec5f56 | 2007-06-27 05:38:08 +0000 | [diff] [blame] | 2010 | case ArraySubscriptExprClass: |
Eli Friedman | c11535c | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 2011 | WarnE = this; |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2012 | Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc(); |
| 2013 | R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange(); |
| 2014 | R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange(); |
| 2015 | return true; |
Eli Friedman | 824f8c1 | 2008-05-27 15:24:04 +0000 | [diff] [blame] | 2016 | |
Chandler Carruth | 4633947 | 2011-08-17 09:49:44 +0000 | [diff] [blame] | 2017 | case CXXOperatorCallExprClass: { |
Richard Trieu | 99e1c95 | 2014-03-11 03:11:08 +0000 | [diff] [blame] | 2018 | // Warn about operator ==,!=,<,>,<=, and >= even when user-defined operator |
Chandler Carruth | 4633947 | 2011-08-17 09:49:44 +0000 | [diff] [blame] | 2019 | // overloads as there is no reasonable way to define these such that they |
| 2020 | // have non-trivial, desirable side-effects. See the -Wunused-comparison |
Richard Trieu | 99e1c95 | 2014-03-11 03:11:08 +0000 | [diff] [blame] | 2021 | // warning: operators == and != are commonly typo'ed, and so warning on them |
Chandler Carruth | 4633947 | 2011-08-17 09:49:44 +0000 | [diff] [blame] | 2022 | // provides additional value as well. If this list is updated, |
| 2023 | // DiagnoseUnusedComparison should be as well. |
| 2024 | const CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(this); |
Richard Trieu | 99e1c95 | 2014-03-11 03:11:08 +0000 | [diff] [blame] | 2025 | switch (Op->getOperator()) { |
| 2026 | default: |
| 2027 | break; |
| 2028 | case OO_EqualEqual: |
| 2029 | case OO_ExclaimEqual: |
| 2030 | case OO_Less: |
| 2031 | case OO_Greater: |
| 2032 | case OO_GreaterEqual: |
| 2033 | case OO_LessEqual: |
David Majnemer | ced8bdf | 2015-02-25 17:36:15 +0000 | [diff] [blame] | 2034 | if (Op->getCallReturnType(Ctx)->isReferenceType() || |
| 2035 | Op->getCallReturnType(Ctx)->isVoidType()) |
Richard Trieu | 161132b | 2014-05-14 23:22:10 +0000 | [diff] [blame] | 2036 | break; |
Eli Friedman | c11535c | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 2037 | WarnE = this; |
Matt Beaumont-Gay | dcaacaa | 2011-09-19 18:51:20 +0000 | [diff] [blame] | 2038 | Loc = Op->getOperatorLoc(); |
| 2039 | R1 = Op->getSourceRange(); |
Chandler Carruth | 4633947 | 2011-08-17 09:49:44 +0000 | [diff] [blame] | 2040 | return true; |
Matt Beaumont-Gay | dcaacaa | 2011-09-19 18:51:20 +0000 | [diff] [blame] | 2041 | } |
Chandler Carruth | 4633947 | 2011-08-17 09:49:44 +0000 | [diff] [blame] | 2042 | |
| 2043 | // Fallthrough for generic call handling. |
| 2044 | } |
Chris Lattner | 1ec5f56 | 2007-06-27 05:38:08 +0000 | [diff] [blame] | 2045 | case CallExprClass: |
Richard Smith | c67fdd4 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 2046 | case CXXMemberCallExprClass: |
| 2047 | case UserDefinedLiteralClass: { |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2048 | // If this is a direct call, get the callee. |
| 2049 | const CallExpr *CE = cast<CallExpr>(this); |
Nuno Lopes | 518e370 | 2009-12-20 23:11:08 +0000 | [diff] [blame] | 2050 | if (const Decl *FD = CE->getCalleeDecl()) { |
Kaelyn Takata | 0a2e84c | 2015-04-09 19:43:04 +0000 | [diff] [blame] | 2051 | const FunctionDecl *Func = dyn_cast<FunctionDecl>(FD); |
| 2052 | bool HasWarnUnusedResultAttr = Func ? Func->hasUnusedResultAttr() |
| 2053 | : FD->hasAttr<WarnUnusedResultAttr>(); |
| 2054 | |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2055 | // If the callee has attribute pure, const, or warn_unused_result, warn |
| 2056 | // about it. void foo() { strlen("bar"); } should warn. |
Chris Lattner | 1a6babf | 2009-10-13 04:53:48 +0000 | [diff] [blame] | 2057 | // |
| 2058 | // Note: If new cases are added here, DiagnoseUnusedExprResult should be |
| 2059 | // updated to match for QoI. |
Kaelyn Takata | 0a2e84c | 2015-04-09 19:43:04 +0000 | [diff] [blame] | 2060 | if (HasWarnUnusedResultAttr || |
Aaron Ballman | 9ead124 | 2013-12-19 02:39:40 +0000 | [diff] [blame] | 2061 | FD->hasAttr<PureAttr>() || FD->hasAttr<ConstAttr>()) { |
Eli Friedman | c11535c | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 2062 | WarnE = this; |
Chris Lattner | 1a6babf | 2009-10-13 04:53:48 +0000 | [diff] [blame] | 2063 | Loc = CE->getCallee()->getLocStart(); |
| 2064 | R1 = CE->getCallee()->getSourceRange(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2065 | |
Chris Lattner | 1a6babf | 2009-10-13 04:53:48 +0000 | [diff] [blame] | 2066 | if (unsigned NumArgs = CE->getNumArgs()) |
| 2067 | R2 = SourceRange(CE->getArg(0)->getLocStart(), |
| 2068 | CE->getArg(NumArgs-1)->getLocEnd()); |
| 2069 | return true; |
| 2070 | } |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2071 | } |
| 2072 | return false; |
| 2073 | } |
Anders Carlsson | 6aa5039 | 2009-11-17 17:11:23 +0000 | [diff] [blame] | 2074 | |
Matt Beaumont-Gay | abf836c | 2012-10-23 06:15:26 +0000 | [diff] [blame] | 2075 | // If we don't know precisely what we're looking at, let's not warn. |
| 2076 | case UnresolvedLookupExprClass: |
| 2077 | case CXXUnresolvedConstructExprClass: |
| 2078 | return false; |
| 2079 | |
Anders Carlsson | 6aa5039 | 2009-11-17 17:11:23 +0000 | [diff] [blame] | 2080 | case CXXTemporaryObjectExprClass: |
Lubos Lunak | 1f490f3 | 2013-07-21 13:15:58 +0000 | [diff] [blame] | 2081 | case CXXConstructExprClass: { |
| 2082 | if (const CXXRecordDecl *Type = getType()->getAsCXXRecordDecl()) { |
| 2083 | if (Type->hasAttr<WarnUnusedAttr>()) { |
| 2084 | WarnE = this; |
| 2085 | Loc = getLocStart(); |
| 2086 | R1 = getSourceRange(); |
| 2087 | return true; |
| 2088 | } |
| 2089 | } |
Anders Carlsson | 6aa5039 | 2009-11-17 17:11:23 +0000 | [diff] [blame] | 2090 | return false; |
Lubos Lunak | 1f490f3 | 2013-07-21 13:15:58 +0000 | [diff] [blame] | 2091 | } |
Anders Carlsson | 6aa5039 | 2009-11-17 17:11:23 +0000 | [diff] [blame] | 2092 | |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2093 | case ObjCMessageExprClass: { |
| 2094 | const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this); |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2095 | if (Ctx.getLangOpts().ObjCAutoRefCount && |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2096 | ME->isInstanceMessage() && |
| 2097 | !ME->getType()->isVoidType() && |
Jean-Daniel Dupas | 06028a5 | 2013-07-19 20:25:56 +0000 | [diff] [blame] | 2098 | ME->getMethodFamily() == OMF_init) { |
Eli Friedman | c11535c | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 2099 | WarnE = this; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 2100 | Loc = getExprLoc(); |
| 2101 | R1 = ME->getSourceRange(); |
| 2102 | return true; |
| 2103 | } |
| 2104 | |
Fariborz Jahanian | ac6b4ef | 2014-07-18 22:59:10 +0000 | [diff] [blame] | 2105 | if (const ObjCMethodDecl *MD = ME->getMethodDecl()) |
Fariborz Jahanian | b0553e2 | 2015-02-16 23:49:44 +0000 | [diff] [blame] | 2106 | if (MD->hasAttr<WarnUnusedResultAttr>()) { |
Fariborz Jahanian | ac6b4ef | 2014-07-18 22:59:10 +0000 | [diff] [blame] | 2107 | WarnE = this; |
| 2108 | Loc = getExprLoc(); |
| 2109 | return true; |
| 2110 | } |
| 2111 | |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2112 | return false; |
Fariborz Jahanian | 5cab26d | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 2113 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2114 | |
John McCall | b7bd14f | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 2115 | case ObjCPropertyRefExprClass: |
Eli Friedman | c11535c | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 2116 | WarnE = this; |
Chris Lattner | d37f61c | 2009-08-16 16:51:50 +0000 | [diff] [blame] | 2117 | Loc = getExprLoc(); |
| 2118 | R1 = getSourceRange(); |
Chris Lattner | d8b800a | 2009-08-16 16:45:18 +0000 | [diff] [blame] | 2119 | return true; |
John McCall | b7bd14f | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 2120 | |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 2121 | case PseudoObjectExprClass: { |
| 2122 | const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this); |
| 2123 | |
| 2124 | // Only complain about things that have the form of a getter. |
| 2125 | if (isa<UnaryOperator>(PO->getSyntacticForm()) || |
| 2126 | isa<BinaryOperator>(PO->getSyntacticForm())) |
| 2127 | return false; |
| 2128 | |
Eli Friedman | c11535c | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 2129 | WarnE = this; |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 2130 | Loc = getExprLoc(); |
| 2131 | R1 = getSourceRange(); |
| 2132 | return true; |
| 2133 | } |
| 2134 | |
Chris Lattner | 944d306 | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 2135 | case StmtExprClass: { |
| 2136 | // Statement exprs don't logically have side effects themselves, but are |
| 2137 | // sometimes used in macros in ways that give them a type that is unused. |
| 2138 | // For example ({ blah; foo(); }) will end up with a type if foo has a type. |
| 2139 | // however, if the result of the stmt expr is dead, we don't want to emit a |
| 2140 | // warning. |
| 2141 | const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt(); |
Argyrios Kyrtzidis | 9096341 | 2010-09-19 21:21:10 +0000 | [diff] [blame] | 2142 | if (!CS->body_empty()) { |
Chris Lattner | 944d306 | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 2143 | if (const Expr *E = dyn_cast<Expr>(CS->body_back())) |
Eli Friedman | c11535c | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 2144 | return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx); |
Argyrios Kyrtzidis | 9096341 | 2010-09-19 21:21:10 +0000 | [diff] [blame] | 2145 | if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back())) |
| 2146 | if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt())) |
Eli Friedman | c11535c | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 2147 | return E->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx); |
Argyrios Kyrtzidis | 9096341 | 2010-09-19 21:21:10 +0000 | [diff] [blame] | 2148 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2149 | |
John McCall | c493a73 | 2010-03-12 07:11:26 +0000 | [diff] [blame] | 2150 | if (getType()->isVoidType()) |
| 2151 | return false; |
Eli Friedman | c11535c | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 2152 | WarnE = this; |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2153 | Loc = cast<StmtExpr>(this)->getLParenLoc(); |
| 2154 | R1 = getSourceRange(); |
| 2155 | return true; |
Chris Lattner | 944d306 | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 2156 | } |
Eli Friedman | bdd5753 | 2012-09-24 23:02:26 +0000 | [diff] [blame] | 2157 | case CXXFunctionalCastExprClass: |
Eli Friedman | c11535c | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 2158 | case CStyleCastExprClass: { |
Eli Friedman | f92f645 | 2012-05-24 21:05:41 +0000 | [diff] [blame] | 2159 | // 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] | 2160 | // volatile lvalue. |
Eli Friedman | f92f645 | 2012-05-24 21:05:41 +0000 | [diff] [blame] | 2161 | const CastExpr *CE = cast<CastExpr>(this); |
Eli Friedman | c11535c | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 2162 | if (CE->getCastKind() == CK_ToVoid) { |
| 2163 | if (CE->getSubExpr()->isGLValue() && |
Eli Friedman | f92f645 | 2012-05-24 21:05:41 +0000 | [diff] [blame] | 2164 | CE->getSubExpr()->getType().isVolatileQualified()) { |
| 2165 | const DeclRefExpr *DRE = |
| 2166 | dyn_cast<DeclRefExpr>(CE->getSubExpr()->IgnoreParens()); |
| 2167 | if (!(DRE && isa<VarDecl>(DRE->getDecl()) && |
| 2168 | cast<VarDecl>(DRE->getDecl())->hasLocalStorage())) { |
| 2169 | return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, |
| 2170 | R1, R2, Ctx); |
| 2171 | } |
| 2172 | } |
Chris Lattner | 2706a55 | 2009-07-28 18:25:28 +0000 | [diff] [blame] | 2173 | return false; |
Eli Friedman | c11535c | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 2174 | } |
Eli Friedman | f92f645 | 2012-05-24 21:05:41 +0000 | [diff] [blame] | 2175 | |
Eli Friedman | c11535c | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 2176 | // If this is a cast to a constructor conversion, check the operand. |
Anders Carlsson | 6aa5039 | 2009-11-17 17:11:23 +0000 | [diff] [blame] | 2177 | // Otherwise, the result of the cast is unused. |
Eli Friedman | c11535c | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 2178 | if (CE->getCastKind() == CK_ConstructorConversion) |
| 2179 | return CE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx); |
Eli Friedman | f92f645 | 2012-05-24 21:05:41 +0000 | [diff] [blame] | 2180 | |
Eli Friedman | c11535c | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 2181 | WarnE = this; |
Eli Friedman | f92f645 | 2012-05-24 21:05:41 +0000 | [diff] [blame] | 2182 | if (const CXXFunctionalCastExpr *CXXCE = |
| 2183 | dyn_cast<CXXFunctionalCastExpr>(this)) { |
Eli Friedman | 89fe0d5 | 2013-08-15 22:02:56 +0000 | [diff] [blame] | 2184 | Loc = CXXCE->getLocStart(); |
Eli Friedman | f92f645 | 2012-05-24 21:05:41 +0000 | [diff] [blame] | 2185 | R1 = CXXCE->getSubExpr()->getSourceRange(); |
| 2186 | } else { |
| 2187 | const CStyleCastExpr *CStyleCE = cast<CStyleCastExpr>(this); |
| 2188 | Loc = CStyleCE->getLParenLoc(); |
| 2189 | R1 = CStyleCE->getSubExpr()->getSourceRange(); |
| 2190 | } |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2191 | return true; |
Anders Carlsson | 6aa5039 | 2009-11-17 17:11:23 +0000 | [diff] [blame] | 2192 | } |
Eli Friedman | c11535c | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 2193 | case ImplicitCastExprClass: { |
| 2194 | const CastExpr *ICE = cast<ImplicitCastExpr>(this); |
Eli Friedman | ca8da1d | 2008-05-19 21:24:43 +0000 | [diff] [blame] | 2195 | |
Eli Friedman | c11535c | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 2196 | // lvalue-to-rvalue conversion on a volatile lvalue is a side-effect. |
| 2197 | if (ICE->getCastKind() == CK_LValueToRValue && |
| 2198 | ICE->getSubExpr()->getType().isVolatileQualified()) |
| 2199 | return false; |
| 2200 | |
| 2201 | return ICE->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx); |
| 2202 | } |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2203 | case CXXDefaultArgExprClass: |
Mike Stump | 53f9ded | 2009-11-03 23:25:48 +0000 | [diff] [blame] | 2204 | return (cast<CXXDefaultArgExpr>(this) |
Eli Friedman | c11535c | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 2205 | ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx)); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 2206 | case CXXDefaultInitExprClass: |
| 2207 | return (cast<CXXDefaultInitExpr>(this) |
| 2208 | ->getExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx)); |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2209 | |
| 2210 | case CXXNewExprClass: |
| 2211 | // FIXME: In theory, there might be new expressions that don't have side |
| 2212 | // effects (e.g. a placement new with an uninitialized POD). |
| 2213 | case CXXDeleteExprClass: |
Chris Lattner | 237f275 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 2214 | return false; |
Anders Carlsson | e80ccac | 2009-08-16 04:11:06 +0000 | [diff] [blame] | 2215 | case CXXBindTemporaryExprClass: |
Mike Stump | 53f9ded | 2009-11-03 23:25:48 +0000 | [diff] [blame] | 2216 | return (cast<CXXBindTemporaryExpr>(this) |
Eli Friedman | c11535c | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 2217 | ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx)); |
John McCall | 5d41378 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 2218 | case ExprWithCleanupsClass: |
| 2219 | return (cast<ExprWithCleanups>(this) |
Eli Friedman | c11535c | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 2220 | ->getSubExpr()->isUnusedResultAWarning(WarnE, Loc, R1, R2, Ctx)); |
Sebastian Redl | bd150f4 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 2221 | } |
Chris Lattner | 1ec5f56 | 2007-06-27 05:38:08 +0000 | [diff] [blame] | 2222 | } |
| 2223 | |
Fariborz Jahanian | 0773533 | 2009-02-22 18:40:18 +0000 | [diff] [blame] | 2224 | /// isOBJCGCCandidate - Check if an expression is objc gc'able. |
Fariborz Jahanian | 063c772 | 2009-09-08 23:38:54 +0000 | [diff] [blame] | 2225 | /// returns true, if it is; false otherwise. |
Fariborz Jahanian | c6d9800 | 2009-06-01 21:29:32 +0000 | [diff] [blame] | 2226 | bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const { |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 2227 | const Expr *E = IgnoreParens(); |
| 2228 | switch (E->getStmtClass()) { |
Fariborz Jahanian | 0773533 | 2009-02-22 18:40:18 +0000 | [diff] [blame] | 2229 | default: |
| 2230 | return false; |
| 2231 | case ObjCIvarRefExprClass: |
| 2232 | return true; |
Fariborz Jahanian | 392124c | 2009-02-23 18:59:50 +0000 | [diff] [blame] | 2233 | case Expr::UnaryOperatorClass: |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 2234 | return cast<UnaryOperator>(E)->getSubExpr()->isOBJCGCCandidate(Ctx); |
Fariborz Jahanian | 0773533 | 2009-02-22 18:40:18 +0000 | [diff] [blame] | 2235 | case ImplicitCastExprClass: |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 2236 | return cast<ImplicitCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx); |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 2237 | case MaterializeTemporaryExprClass: |
| 2238 | return cast<MaterializeTemporaryExpr>(E)->GetTemporaryExpr() |
| 2239 | ->isOBJCGCCandidate(Ctx); |
Fariborz Jahanian | a16904b | 2009-05-05 23:28:21 +0000 | [diff] [blame] | 2240 | case CStyleCastExprClass: |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 2241 | return cast<CStyleCastExpr>(E)->getSubExpr()->isOBJCGCCandidate(Ctx); |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 2242 | case DeclRefExprClass: { |
John McCall | 113bee0 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 2243 | const Decl *D = cast<DeclRefExpr>(E)->getDecl(); |
Fariborz Jahanian | c367b8f | 2011-09-23 18:57:30 +0000 | [diff] [blame] | 2244 | |
Fariborz Jahanian | c6d9800 | 2009-06-01 21:29:32 +0000 | [diff] [blame] | 2245 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 2246 | if (VD->hasGlobalStorage()) |
| 2247 | return true; |
| 2248 | QualType T = VD->getType(); |
Fariborz Jahanian | cceedbf | 2009-09-16 18:09:18 +0000 | [diff] [blame] | 2249 | // dereferencing to a pointer is always a gc'able candidate, |
| 2250 | // unless it is __weak. |
Daniel Dunbar | 4782a6e | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 2251 | return T->isPointerType() && |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2252 | (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak); |
Fariborz Jahanian | c6d9800 | 2009-06-01 21:29:32 +0000 | [diff] [blame] | 2253 | } |
Fariborz Jahanian | 0773533 | 2009-02-22 18:40:18 +0000 | [diff] [blame] | 2254 | return false; |
| 2255 | } |
Douglas Gregor | f405d7e | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 2256 | case MemberExprClass: { |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 2257 | const MemberExpr *M = cast<MemberExpr>(E); |
Fariborz Jahanian | c6d9800 | 2009-06-01 21:29:32 +0000 | [diff] [blame] | 2258 | return M->getBase()->isOBJCGCCandidate(Ctx); |
Fariborz Jahanian | 0773533 | 2009-02-22 18:40:18 +0000 | [diff] [blame] | 2259 | } |
| 2260 | case ArraySubscriptExprClass: |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 2261 | return cast<ArraySubscriptExpr>(E)->getBase()->isOBJCGCCandidate(Ctx); |
Fariborz Jahanian | 0773533 | 2009-02-22 18:40:18 +0000 | [diff] [blame] | 2262 | } |
| 2263 | } |
Sebastian Redl | ce354af | 2010-09-10 20:55:33 +0000 | [diff] [blame] | 2264 | |
Argyrios Kyrtzidis | ca76629 | 2010-11-01 18:49:26 +0000 | [diff] [blame] | 2265 | bool Expr::isBoundMemberFunction(ASTContext &Ctx) const { |
| 2266 | if (isTypeDependent()) |
| 2267 | return false; |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 2268 | return ClassifyLValue(Ctx) == Expr::LV_MemberFunction; |
Argyrios Kyrtzidis | ca76629 | 2010-11-01 18:49:26 +0000 | [diff] [blame] | 2269 | } |
| 2270 | |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 2271 | QualType Expr::findBoundMemberType(const Expr *expr) { |
John McCall | e314e27 | 2011-10-18 21:02:43 +0000 | [diff] [blame] | 2272 | assert(expr->hasPlaceholderType(BuiltinType::BoundMember)); |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 2273 | |
| 2274 | // Bound member expressions are always one of these possibilities: |
| 2275 | // x->m x.m x->*y x.*y |
| 2276 | // (possibly parenthesized) |
| 2277 | |
| 2278 | expr = expr->IgnoreParens(); |
| 2279 | if (const MemberExpr *mem = dyn_cast<MemberExpr>(expr)) { |
| 2280 | assert(isa<CXXMethodDecl>(mem->getMemberDecl())); |
| 2281 | return mem->getMemberDecl()->getType(); |
| 2282 | } |
| 2283 | |
| 2284 | if (const BinaryOperator *op = dyn_cast<BinaryOperator>(expr)) { |
| 2285 | QualType type = op->getRHS()->getType()->castAs<MemberPointerType>() |
| 2286 | ->getPointeeType(); |
| 2287 | assert(type->isFunctionType()); |
| 2288 | return type; |
| 2289 | } |
| 2290 | |
David Majnemer | ced8bdf | 2015-02-25 17:36:15 +0000 | [diff] [blame] | 2291 | assert(isa<UnresolvedMemberExpr>(expr) || isa<CXXPseudoDestructorExpr>(expr)); |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 2292 | return QualType(); |
| 2293 | } |
| 2294 | |
Ted Kremenek | fff7096 | 2008-01-17 16:57:34 +0000 | [diff] [blame] | 2295 | Expr* Expr::IgnoreParens() { |
| 2296 | Expr* E = this; |
Abramo Bagnara | 932e393 | 2010-10-15 07:51:18 +0000 | [diff] [blame] | 2297 | while (true) { |
| 2298 | if (ParenExpr* P = dyn_cast<ParenExpr>(E)) { |
| 2299 | E = P->getSubExpr(); |
| 2300 | continue; |
| 2301 | } |
| 2302 | if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) { |
| 2303 | if (P->getOpcode() == UO_Extension) { |
| 2304 | E = P->getSubExpr(); |
| 2305 | continue; |
| 2306 | } |
| 2307 | } |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 2308 | if (GenericSelectionExpr* P = dyn_cast<GenericSelectionExpr>(E)) { |
| 2309 | if (!P->isResultDependent()) { |
| 2310 | E = P->getResultExpr(); |
| 2311 | continue; |
| 2312 | } |
| 2313 | } |
Eli Friedman | 75807f2 | 2013-07-20 00:40:58 +0000 | [diff] [blame] | 2314 | if (ChooseExpr* P = dyn_cast<ChooseExpr>(E)) { |
| 2315 | if (!P->isConditionDependent()) { |
| 2316 | E = P->getChosenSubExpr(); |
| 2317 | continue; |
| 2318 | } |
| 2319 | } |
Abramo Bagnara | 932e393 | 2010-10-15 07:51:18 +0000 | [diff] [blame] | 2320 | return E; |
| 2321 | } |
Ted Kremenek | fff7096 | 2008-01-17 16:57:34 +0000 | [diff] [blame] | 2322 | } |
| 2323 | |
Chris Lattner | f266096 | 2008-02-13 01:02:39 +0000 | [diff] [blame] | 2324 | /// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr |
| 2325 | /// or CastExprs or ImplicitCastExprs, returning their operand. |
| 2326 | Expr *Expr::IgnoreParenCasts() { |
| 2327 | Expr *E = this; |
| 2328 | while (true) { |
Eli Friedman | 75807f2 | 2013-07-20 00:40:58 +0000 | [diff] [blame] | 2329 | E = E->IgnoreParens(); |
Abramo Bagnara | 932e393 | 2010-10-15 07:51:18 +0000 | [diff] [blame] | 2330 | if (CastExpr *P = dyn_cast<CastExpr>(E)) { |
Chris Lattner | f266096 | 2008-02-13 01:02:39 +0000 | [diff] [blame] | 2331 | E = P->getSubExpr(); |
Abramo Bagnara | 932e393 | 2010-10-15 07:51:18 +0000 | [diff] [blame] | 2332 | continue; |
| 2333 | } |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 2334 | if (MaterializeTemporaryExpr *Materialize |
| 2335 | = dyn_cast<MaterializeTemporaryExpr>(E)) { |
| 2336 | E = Materialize->GetTemporaryExpr(); |
| 2337 | continue; |
| 2338 | } |
Douglas Gregor | 6a40b08 | 2011-09-08 17:56:33 +0000 | [diff] [blame] | 2339 | if (SubstNonTypeTemplateParmExpr *NTTP |
| 2340 | = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) { |
| 2341 | E = NTTP->getReplacement(); |
| 2342 | continue; |
| 2343 | } |
Abramo Bagnara | 932e393 | 2010-10-15 07:51:18 +0000 | [diff] [blame] | 2344 | return E; |
Chris Lattner | f266096 | 2008-02-13 01:02:39 +0000 | [diff] [blame] | 2345 | } |
| 2346 | } |
| 2347 | |
Ted Kremenek | 6f375e5 | 2014-04-16 07:26:09 +0000 | [diff] [blame] | 2348 | Expr *Expr::IgnoreCasts() { |
| 2349 | Expr *E = this; |
| 2350 | while (true) { |
| 2351 | if (CastExpr *P = dyn_cast<CastExpr>(E)) { |
| 2352 | E = P->getSubExpr(); |
| 2353 | continue; |
| 2354 | } |
| 2355 | if (MaterializeTemporaryExpr *Materialize |
| 2356 | = dyn_cast<MaterializeTemporaryExpr>(E)) { |
| 2357 | E = Materialize->GetTemporaryExpr(); |
| 2358 | continue; |
| 2359 | } |
| 2360 | if (SubstNonTypeTemplateParmExpr *NTTP |
| 2361 | = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) { |
| 2362 | E = NTTP->getReplacement(); |
| 2363 | continue; |
| 2364 | } |
| 2365 | return E; |
| 2366 | } |
| 2367 | } |
| 2368 | |
John McCall | 5a4ce8b | 2010-12-04 08:24:19 +0000 | [diff] [blame] | 2369 | /// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue |
| 2370 | /// casts. This is intended purely as a temporary workaround for code |
| 2371 | /// that hasn't yet been rewritten to do the right thing about those |
| 2372 | /// casts, and may disappear along with the last internal use. |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 2373 | Expr *Expr::IgnoreParenLValueCasts() { |
| 2374 | Expr *E = this; |
John McCall | 5a4ce8b | 2010-12-04 08:24:19 +0000 | [diff] [blame] | 2375 | while (true) { |
Eli Friedman | 75807f2 | 2013-07-20 00:40:58 +0000 | [diff] [blame] | 2376 | E = E->IgnoreParens(); |
| 2377 | if (CastExpr *P = dyn_cast<CastExpr>(E)) { |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 2378 | if (P->getCastKind() == CK_LValueToRValue) { |
| 2379 | E = P->getSubExpr(); |
| 2380 | continue; |
| 2381 | } |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 2382 | } else if (MaterializeTemporaryExpr *Materialize |
| 2383 | = dyn_cast<MaterializeTemporaryExpr>(E)) { |
| 2384 | E = Materialize->GetTemporaryExpr(); |
| 2385 | continue; |
Douglas Gregor | 6a40b08 | 2011-09-08 17:56:33 +0000 | [diff] [blame] | 2386 | } else if (SubstNonTypeTemplateParmExpr *NTTP |
| 2387 | = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) { |
| 2388 | E = NTTP->getReplacement(); |
| 2389 | continue; |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 2390 | } |
| 2391 | break; |
| 2392 | } |
| 2393 | return E; |
| 2394 | } |
Rafael Espindola | ecbe2e9 | 2012-06-28 01:56:38 +0000 | [diff] [blame] | 2395 | |
| 2396 | Expr *Expr::ignoreParenBaseCasts() { |
| 2397 | Expr *E = this; |
| 2398 | while (true) { |
Eli Friedman | 75807f2 | 2013-07-20 00:40:58 +0000 | [diff] [blame] | 2399 | E = E->IgnoreParens(); |
Rafael Espindola | ecbe2e9 | 2012-06-28 01:56:38 +0000 | [diff] [blame] | 2400 | if (CastExpr *CE = dyn_cast<CastExpr>(E)) { |
| 2401 | if (CE->getCastKind() == CK_DerivedToBase || |
| 2402 | CE->getCastKind() == CK_UncheckedDerivedToBase || |
| 2403 | CE->getCastKind() == CK_NoOp) { |
| 2404 | E = CE->getSubExpr(); |
| 2405 | continue; |
| 2406 | } |
| 2407 | } |
| 2408 | |
| 2409 | return E; |
| 2410 | } |
| 2411 | } |
| 2412 | |
John McCall | eebc832 | 2010-05-05 22:59:52 +0000 | [diff] [blame] | 2413 | Expr *Expr::IgnoreParenImpCasts() { |
| 2414 | Expr *E = this; |
| 2415 | while (true) { |
Eli Friedman | 75807f2 | 2013-07-20 00:40:58 +0000 | [diff] [blame] | 2416 | E = E->IgnoreParens(); |
Abramo Bagnara | 932e393 | 2010-10-15 07:51:18 +0000 | [diff] [blame] | 2417 | if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | eebc832 | 2010-05-05 22:59:52 +0000 | [diff] [blame] | 2418 | E = P->getSubExpr(); |
Abramo Bagnara | 932e393 | 2010-10-15 07:51:18 +0000 | [diff] [blame] | 2419 | continue; |
| 2420 | } |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 2421 | if (MaterializeTemporaryExpr *Materialize |
| 2422 | = dyn_cast<MaterializeTemporaryExpr>(E)) { |
| 2423 | E = Materialize->GetTemporaryExpr(); |
| 2424 | continue; |
| 2425 | } |
Douglas Gregor | 6a40b08 | 2011-09-08 17:56:33 +0000 | [diff] [blame] | 2426 | if (SubstNonTypeTemplateParmExpr *NTTP |
| 2427 | = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) { |
| 2428 | E = NTTP->getReplacement(); |
| 2429 | continue; |
| 2430 | } |
Abramo Bagnara | 932e393 | 2010-10-15 07:51:18 +0000 | [diff] [blame] | 2431 | return E; |
John McCall | eebc832 | 2010-05-05 22:59:52 +0000 | [diff] [blame] | 2432 | } |
| 2433 | } |
| 2434 | |
Hans Wennborg | de2e67e | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 2435 | Expr *Expr::IgnoreConversionOperator() { |
| 2436 | if (CXXMemberCallExpr *MCE = dyn_cast<CXXMemberCallExpr>(this)) { |
Chandler Carruth | 4352b0b | 2011-06-21 17:22:09 +0000 | [diff] [blame] | 2437 | if (MCE->getMethodDecl() && isa<CXXConversionDecl>(MCE->getMethodDecl())) |
Hans Wennborg | de2e67e | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 2438 | return MCE->getImplicitObjectArgument(); |
| 2439 | } |
| 2440 | return this; |
| 2441 | } |
| 2442 | |
Chris Lattner | ef26c77 | 2009-03-13 17:28:01 +0000 | [diff] [blame] | 2443 | /// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the |
| 2444 | /// value (including ptr->int casts of the same size). Strip off any |
| 2445 | /// ParenExpr or CastExprs, returning their operand. |
| 2446 | Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) { |
| 2447 | Expr *E = this; |
| 2448 | while (true) { |
Eli Friedman | 75807f2 | 2013-07-20 00:40:58 +0000 | [diff] [blame] | 2449 | E = E->IgnoreParens(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2450 | |
Chris Lattner | ef26c77 | 2009-03-13 17:28:01 +0000 | [diff] [blame] | 2451 | if (CastExpr *P = dyn_cast<CastExpr>(E)) { |
| 2452 | // 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] | 2453 | // 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] | 2454 | Expr *SE = P->getSubExpr(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2455 | |
Chris Lattner | ef26c77 | 2009-03-13 17:28:01 +0000 | [diff] [blame] | 2456 | if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) { |
| 2457 | E = SE; |
| 2458 | continue; |
| 2459 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2460 | |
Abramo Bagnara | 932e393 | 2010-10-15 07:51:18 +0000 | [diff] [blame] | 2461 | if ((E->getType()->isPointerType() || |
Douglas Gregor | 6972a62 | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 2462 | E->getType()->isIntegralType(Ctx)) && |
Abramo Bagnara | 932e393 | 2010-10-15 07:51:18 +0000 | [diff] [blame] | 2463 | (SE->getType()->isPointerType() || |
Douglas Gregor | 6972a62 | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 2464 | SE->getType()->isIntegralType(Ctx)) && |
Chris Lattner | ef26c77 | 2009-03-13 17:28:01 +0000 | [diff] [blame] | 2465 | Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) { |
| 2466 | E = SE; |
| 2467 | continue; |
| 2468 | } |
| 2469 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2470 | |
Douglas Gregor | 6a40b08 | 2011-09-08 17:56:33 +0000 | [diff] [blame] | 2471 | if (SubstNonTypeTemplateParmExpr *NTTP |
| 2472 | = dyn_cast<SubstNonTypeTemplateParmExpr>(E)) { |
| 2473 | E = NTTP->getReplacement(); |
| 2474 | continue; |
| 2475 | } |
| 2476 | |
Chris Lattner | ef26c77 | 2009-03-13 17:28:01 +0000 | [diff] [blame] | 2477 | return E; |
| 2478 | } |
| 2479 | } |
| 2480 | |
Douglas Gregor | d196a58 | 2009-12-14 19:27:10 +0000 | [diff] [blame] | 2481 | bool Expr::isDefaultArgument() const { |
| 2482 | const Expr *E = this; |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 2483 | if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E)) |
| 2484 | E = M->GetTemporaryExpr(); |
| 2485 | |
Douglas Gregor | d196a58 | 2009-12-14 19:27:10 +0000 | [diff] [blame] | 2486 | while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) |
| 2487 | E = ICE->getSubExprAsWritten(); |
Alexis Hunt | a8136cc | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2488 | |
Douglas Gregor | d196a58 | 2009-12-14 19:27:10 +0000 | [diff] [blame] | 2489 | return isa<CXXDefaultArgExpr>(E); |
| 2490 | } |
Chris Lattner | ef26c77 | 2009-03-13 17:28:01 +0000 | [diff] [blame] | 2491 | |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 2492 | /// \brief Skip over any no-op casts and any temporary-binding |
| 2493 | /// expressions. |
Anders Carlsson | 66bbf50 | 2010-11-28 16:40:49 +0000 | [diff] [blame] | 2494 | static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) { |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 2495 | if (const MaterializeTemporaryExpr *M = dyn_cast<MaterializeTemporaryExpr>(E)) |
| 2496 | E = M->GetTemporaryExpr(); |
| 2497 | |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 2498 | while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2499 | if (ICE->getCastKind() == CK_NoOp) |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 2500 | E = ICE->getSubExpr(); |
| 2501 | else |
| 2502 | break; |
| 2503 | } |
| 2504 | |
| 2505 | while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E)) |
| 2506 | E = BE->getSubExpr(); |
| 2507 | |
| 2508 | while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2509 | if (ICE->getCastKind() == CK_NoOp) |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 2510 | E = ICE->getSubExpr(); |
| 2511 | else |
| 2512 | break; |
| 2513 | } |
Anders Carlsson | 66bbf50 | 2010-11-28 16:40:49 +0000 | [diff] [blame] | 2514 | |
| 2515 | return E->IgnoreParens(); |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 2516 | } |
| 2517 | |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 2518 | /// isTemporaryObject - Determines if this expression produces a |
| 2519 | /// temporary of the given class type. |
| 2520 | bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const { |
| 2521 | if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy))) |
| 2522 | return false; |
| 2523 | |
Anders Carlsson | 66bbf50 | 2010-11-28 16:40:49 +0000 | [diff] [blame] | 2524 | const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this); |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 2525 | |
John McCall | 02dc8c7 | 2010-09-15 20:59:13 +0000 | [diff] [blame] | 2526 | // Temporaries are by definition pr-values of class type. |
Fariborz Jahanian | 30e8d58 | 2010-09-27 17:30:38 +0000 | [diff] [blame] | 2527 | if (!E->Classify(C).isPRValue()) { |
| 2528 | // 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] | 2529 | if (!isa<ObjCPropertyRefExpr>(E)) |
Fariborz Jahanian | 30e8d58 | 2010-09-27 17:30:38 +0000 | [diff] [blame] | 2530 | return false; |
| 2531 | } |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 2532 | |
John McCall | f4ee1dd | 2010-09-16 06:57:56 +0000 | [diff] [blame] | 2533 | // Black-list a few cases which yield pr-values of class type that don't |
| 2534 | // refer to temporaries of that type: |
| 2535 | |
| 2536 | // - implicit derived-to-base conversions |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 2537 | if (isa<ImplicitCastExpr>(E)) { |
| 2538 | switch (cast<ImplicitCastExpr>(E)->getCastKind()) { |
| 2539 | case CK_DerivedToBase: |
| 2540 | case CK_UncheckedDerivedToBase: |
| 2541 | return false; |
| 2542 | default: |
| 2543 | break; |
| 2544 | } |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 2545 | } |
| 2546 | |
John McCall | f4ee1dd | 2010-09-16 06:57:56 +0000 | [diff] [blame] | 2547 | // - member expressions (all) |
| 2548 | if (isa<MemberExpr>(E)) |
| 2549 | return false; |
| 2550 | |
Eli Friedman | 13ffdd8 | 2012-06-15 23:51:06 +0000 | [diff] [blame] | 2551 | if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) |
| 2552 | if (BO->isPtrMemOp()) |
| 2553 | return false; |
| 2554 | |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 2555 | // - opaque values (all) |
| 2556 | if (isa<OpaqueValueExpr>(E)) |
| 2557 | return false; |
| 2558 | |
John McCall | 7a626f6 | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 2559 | return true; |
Douglas Gregor | 45cf7e3 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 2560 | } |
| 2561 | |
Douglas Gregor | 25b7e05 | 2011-03-02 21:06:53 +0000 | [diff] [blame] | 2562 | bool Expr::isImplicitCXXThis() const { |
| 2563 | const Expr *E = this; |
| 2564 | |
| 2565 | // Strip away parentheses and casts we don't care about. |
| 2566 | while (true) { |
| 2567 | if (const ParenExpr *Paren = dyn_cast<ParenExpr>(E)) { |
| 2568 | E = Paren->getSubExpr(); |
| 2569 | continue; |
| 2570 | } |
| 2571 | |
| 2572 | if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
| 2573 | if (ICE->getCastKind() == CK_NoOp || |
| 2574 | ICE->getCastKind() == CK_LValueToRValue || |
| 2575 | ICE->getCastKind() == CK_DerivedToBase || |
| 2576 | ICE->getCastKind() == CK_UncheckedDerivedToBase) { |
| 2577 | E = ICE->getSubExpr(); |
| 2578 | continue; |
| 2579 | } |
| 2580 | } |
| 2581 | |
| 2582 | if (const UnaryOperator* UnOp = dyn_cast<UnaryOperator>(E)) { |
| 2583 | if (UnOp->getOpcode() == UO_Extension) { |
| 2584 | E = UnOp->getSubExpr(); |
| 2585 | continue; |
| 2586 | } |
| 2587 | } |
| 2588 | |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 2589 | if (const MaterializeTemporaryExpr *M |
| 2590 | = dyn_cast<MaterializeTemporaryExpr>(E)) { |
| 2591 | E = M->GetTemporaryExpr(); |
| 2592 | continue; |
| 2593 | } |
| 2594 | |
Douglas Gregor | 25b7e05 | 2011-03-02 21:06:53 +0000 | [diff] [blame] | 2595 | break; |
| 2596 | } |
| 2597 | |
| 2598 | if (const CXXThisExpr *This = dyn_cast<CXXThisExpr>(E)) |
| 2599 | return This->isImplicit(); |
| 2600 | |
| 2601 | return false; |
| 2602 | } |
| 2603 | |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2604 | /// hasAnyTypeDependentArguments - Determines if any of the expressions |
| 2605 | /// in Exprs is type-dependent. |
Dmitri Gribenko | f857950 | 2013-01-12 19:30:44 +0000 | [diff] [blame] | 2606 | bool Expr::hasAnyTypeDependentArguments(ArrayRef<Expr *> Exprs) { |
Ahmed Charles | b24b9aa | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 2607 | for (unsigned I = 0; I < Exprs.size(); ++I) |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2608 | if (Exprs[I]->isTypeDependent()) |
| 2609 | return true; |
| 2610 | |
| 2611 | return false; |
| 2612 | } |
| 2613 | |
Abramo Bagnara | 847c660 | 2014-05-22 19:20:46 +0000 | [diff] [blame] | 2614 | bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef, |
| 2615 | const Expr **Culprit) const { |
Eli Friedman | 384da27 | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 2616 | // This function is attempting whether an expression is an initializer |
Eli Friedman | 4c27ac2 | 2013-07-16 22:40:53 +0000 | [diff] [blame] | 2617 | // which can be evaluated at compile-time. It very closely parallels |
| 2618 | // ConstExprEmitter in CGExprConstant.cpp; if they don't match, it |
| 2619 | // will lead to unexpected results. Like ConstExprEmitter, it falls back |
| 2620 | // to isEvaluatable most of the time. |
| 2621 | // |
John McCall | 8b0f4ff | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 2622 | // If we ever capture reference-binding directly in the AST, we can |
| 2623 | // kill the second parameter. |
| 2624 | |
| 2625 | if (IsForRef) { |
| 2626 | EvalResult Result; |
Abramo Bagnara | 847c660 | 2014-05-22 19:20:46 +0000 | [diff] [blame] | 2627 | if (EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects) |
| 2628 | return true; |
| 2629 | if (Culprit) |
| 2630 | *Culprit = this; |
| 2631 | return false; |
John McCall | 8b0f4ff | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 2632 | } |
Eli Friedman | cf7cbe7 | 2009-02-20 02:36:22 +0000 | [diff] [blame] | 2633 | |
Anders Carlsson | a7c5eb7 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 2634 | switch (getStmtClass()) { |
Eli Friedman | 384da27 | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 2635 | default: break; |
Anders Carlsson | a7c5eb7 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 2636 | case StringLiteralClass: |
Chris Lattner | d7e7b8e | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 2637 | case ObjCEncodeExprClass: |
Anders Carlsson | a7c5eb7 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 2638 | return true; |
John McCall | 81c9cea | 2010-08-01 21:51:45 +0000 | [diff] [blame] | 2639 | case CXXTemporaryObjectExprClass: |
| 2640 | case CXXConstructExprClass: { |
| 2641 | const CXXConstructExpr *CE = cast<CXXConstructExpr>(this); |
John McCall | 8b0f4ff | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 2642 | |
Eli Friedman | 4c27ac2 | 2013-07-16 22:40:53 +0000 | [diff] [blame] | 2643 | if (CE->getConstructor()->isTrivial() && |
| 2644 | CE->getConstructor()->getParent()->hasTrivialDestructor()) { |
| 2645 | // Trivial default constructor |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2646 | if (!CE->getNumArgs()) return true; |
John McCall | 8b0f4ff | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 2647 | |
Eli Friedman | 4c27ac2 | 2013-07-16 22:40:53 +0000 | [diff] [blame] | 2648 | // Trivial copy constructor |
| 2649 | assert(CE->getNumArgs() == 1 && "trivial ctor with > 1 argument"); |
Abramo Bagnara | 847c660 | 2014-05-22 19:20:46 +0000 | [diff] [blame] | 2650 | return CE->getArg(0)->isConstantInitializer(Ctx, false, Culprit); |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2651 | } |
| 2652 | |
Richard Smith | d62306a | 2011-11-10 06:34:14 +0000 | [diff] [blame] | 2653 | break; |
John McCall | 81c9cea | 2010-08-01 21:51:45 +0000 | [diff] [blame] | 2654 | } |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2655 | case CompoundLiteralExprClass: { |
Eli Friedman | cf7cbe7 | 2009-02-20 02:36:22 +0000 | [diff] [blame] | 2656 | // This handles gcc's extension that allows global initializers like |
| 2657 | // "struct x {int x;} x = (struct x) {};". |
| 2658 | // FIXME: This accepts other cases it shouldn't! |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2659 | const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer(); |
Abramo Bagnara | 847c660 | 2014-05-22 19:20:46 +0000 | [diff] [blame] | 2660 | return Exp->isConstantInitializer(Ctx, false, Culprit); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2661 | } |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2662 | case DesignatedInitUpdateExprClass: { |
| 2663 | const DesignatedInitUpdateExpr *DIUE = cast<DesignatedInitUpdateExpr>(this); |
| 2664 | return DIUE->getBase()->isConstantInitializer(Ctx, false, Culprit) && |
| 2665 | DIUE->getUpdater()->isConstantInitializer(Ctx, false, Culprit); |
| 2666 | } |
Anders Carlsson | a7c5eb7 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 2667 | case InitListExprClass: { |
Eli Friedman | 4c27ac2 | 2013-07-16 22:40:53 +0000 | [diff] [blame] | 2668 | const InitListExpr *ILE = cast<InitListExpr>(this); |
| 2669 | if (ILE->getType()->isArrayType()) { |
| 2670 | unsigned numInits = ILE->getNumInits(); |
| 2671 | for (unsigned i = 0; i < numInits; i++) { |
Abramo Bagnara | 847c660 | 2014-05-22 19:20:46 +0000 | [diff] [blame] | 2672 | if (!ILE->getInit(i)->isConstantInitializer(Ctx, false, Culprit)) |
Eli Friedman | 4c27ac2 | 2013-07-16 22:40:53 +0000 | [diff] [blame] | 2673 | return false; |
| 2674 | } |
| 2675 | return true; |
Anders Carlsson | a7c5eb7 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 2676 | } |
Eli Friedman | 4c27ac2 | 2013-07-16 22:40:53 +0000 | [diff] [blame] | 2677 | |
| 2678 | if (ILE->getType()->isRecordType()) { |
| 2679 | unsigned ElementNo = 0; |
| 2680 | RecordDecl *RD = ILE->getType()->getAs<RecordType>()->getDecl(); |
Hans Wennborg | a302cd9 | 2014-08-21 16:06:57 +0000 | [diff] [blame] | 2681 | for (const auto *Field : RD->fields()) { |
Eli Friedman | 4c27ac2 | 2013-07-16 22:40:53 +0000 | [diff] [blame] | 2682 | // 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] | 2683 | if (RD->isUnion() && ILE->getInitializedFieldInUnion() != Field) |
Eli Friedman | 4c27ac2 | 2013-07-16 22:40:53 +0000 | [diff] [blame] | 2684 | continue; |
| 2685 | |
| 2686 | // Don't emit anonymous bitfields, they just affect layout. |
| 2687 | if (Field->isUnnamedBitfield()) |
| 2688 | continue; |
| 2689 | |
| 2690 | if (ElementNo < ILE->getNumInits()) { |
| 2691 | const Expr *Elt = ILE->getInit(ElementNo++); |
| 2692 | if (Field->isBitField()) { |
| 2693 | // Bitfields have to evaluate to an integer. |
| 2694 | llvm::APSInt ResultTmp; |
Abramo Bagnara | 847c660 | 2014-05-22 19:20:46 +0000 | [diff] [blame] | 2695 | if (!Elt->EvaluateAsInt(ResultTmp, Ctx)) { |
| 2696 | if (Culprit) |
| 2697 | *Culprit = Elt; |
Eli Friedman | 4c27ac2 | 2013-07-16 22:40:53 +0000 | [diff] [blame] | 2698 | return false; |
Abramo Bagnara | 847c660 | 2014-05-22 19:20:46 +0000 | [diff] [blame] | 2699 | } |
Eli Friedman | 4c27ac2 | 2013-07-16 22:40:53 +0000 | [diff] [blame] | 2700 | } else { |
| 2701 | bool RefType = Field->getType()->isReferenceType(); |
Abramo Bagnara | 847c660 | 2014-05-22 19:20:46 +0000 | [diff] [blame] | 2702 | if (!Elt->isConstantInitializer(Ctx, RefType, Culprit)) |
Eli Friedman | 4c27ac2 | 2013-07-16 22:40:53 +0000 | [diff] [blame] | 2703 | return false; |
| 2704 | } |
| 2705 | } |
| 2706 | } |
| 2707 | return true; |
| 2708 | } |
| 2709 | |
| 2710 | break; |
Anders Carlsson | a7c5eb7 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 2711 | } |
Douglas Gregor | 0202cb4 | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 2712 | case ImplicitValueInitExprClass: |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2713 | case NoInitExprClass: |
Douglas Gregor | 0202cb4 | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 2714 | return true; |
Chris Lattner | 3eb172a | 2009-10-13 07:14:16 +0000 | [diff] [blame] | 2715 | case ParenExprClass: |
John McCall | 8b0f4ff | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 2716 | return cast<ParenExpr>(this)->getSubExpr() |
Abramo Bagnara | 847c660 | 2014-05-22 19:20:46 +0000 | [diff] [blame] | 2717 | ->isConstantInitializer(Ctx, IsForRef, Culprit); |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 2718 | case GenericSelectionExprClass: |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 2719 | return cast<GenericSelectionExpr>(this)->getResultExpr() |
Abramo Bagnara | 847c660 | 2014-05-22 19:20:46 +0000 | [diff] [blame] | 2720 | ->isConstantInitializer(Ctx, IsForRef, Culprit); |
Abramo Bagnara | b59a5b6 | 2010-09-27 07:13:32 +0000 | [diff] [blame] | 2721 | case ChooseExprClass: |
Abramo Bagnara | 847c660 | 2014-05-22 19:20:46 +0000 | [diff] [blame] | 2722 | if (cast<ChooseExpr>(this)->isConditionDependent()) { |
| 2723 | if (Culprit) |
| 2724 | *Culprit = this; |
Eli Friedman | 75807f2 | 2013-07-20 00:40:58 +0000 | [diff] [blame] | 2725 | return false; |
Abramo Bagnara | 847c660 | 2014-05-22 19:20:46 +0000 | [diff] [blame] | 2726 | } |
Eli Friedman | 75807f2 | 2013-07-20 00:40:58 +0000 | [diff] [blame] | 2727 | return cast<ChooseExpr>(this)->getChosenSubExpr() |
Abramo Bagnara | 847c660 | 2014-05-22 19:20:46 +0000 | [diff] [blame] | 2728 | ->isConstantInitializer(Ctx, IsForRef, Culprit); |
Eli Friedman | 384da27 | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 2729 | case UnaryOperatorClass: { |
| 2730 | const UnaryOperator* Exp = cast<UnaryOperator>(this); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2731 | if (Exp->getOpcode() == UO_Extension) |
Abramo Bagnara | 847c660 | 2014-05-22 19:20:46 +0000 | [diff] [blame] | 2732 | return Exp->getSubExpr()->isConstantInitializer(Ctx, false, Culprit); |
Eli Friedman | 384da27 | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 2733 | break; |
| 2734 | } |
John McCall | 8b0f4ff | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 2735 | case CXXFunctionalCastExprClass: |
John McCall | 81c9cea | 2010-08-01 21:51:45 +0000 | [diff] [blame] | 2736 | case CXXStaticCastExprClass: |
Chris Lattner | 1f02e05 | 2009-04-21 05:19:11 +0000 | [diff] [blame] | 2737 | case ImplicitCastExprClass: |
Eli Friedman | 4c27ac2 | 2013-07-16 22:40:53 +0000 | [diff] [blame] | 2738 | case CStyleCastExprClass: |
| 2739 | case ObjCBridgedCastExprClass: |
| 2740 | case CXXDynamicCastExprClass: |
| 2741 | case CXXReinterpretCastExprClass: |
| 2742 | case CXXConstCastExprClass: { |
Richard Smith | 161f09a | 2011-12-06 22:44:34 +0000 | [diff] [blame] | 2743 | const CastExpr *CE = cast<CastExpr>(this); |
| 2744 | |
Eli Friedman | 13ec75b | 2011-12-21 00:43:02 +0000 | [diff] [blame] | 2745 | // Handle misc casts we want to ignore. |
Eli Friedman | 13ec75b | 2011-12-21 00:43:02 +0000 | [diff] [blame] | 2746 | if (CE->getCastKind() == CK_NoOp || |
| 2747 | CE->getCastKind() == CK_LValueToRValue || |
| 2748 | CE->getCastKind() == CK_ToUnion || |
Eli Friedman | 4c27ac2 | 2013-07-16 22:40:53 +0000 | [diff] [blame] | 2749 | CE->getCastKind() == CK_ConstructorConversion || |
| 2750 | CE->getCastKind() == CK_NonAtomicToAtomic || |
| 2751 | CE->getCastKind() == CK_AtomicToNonAtomic) |
Abramo Bagnara | 847c660 | 2014-05-22 19:20:46 +0000 | [diff] [blame] | 2752 | return CE->getSubExpr()->isConstantInitializer(Ctx, false, Culprit); |
Richard Smith | 161f09a | 2011-12-06 22:44:34 +0000 | [diff] [blame] | 2753 | |
Eli Friedman | 384da27 | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 2754 | break; |
Richard Smith | 161f09a | 2011-12-06 22:44:34 +0000 | [diff] [blame] | 2755 | } |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 2756 | case MaterializeTemporaryExprClass: |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2757 | return cast<MaterializeTemporaryExpr>(this)->GetTemporaryExpr() |
Abramo Bagnara | 847c660 | 2014-05-22 19:20:46 +0000 | [diff] [blame] | 2758 | ->isConstantInitializer(Ctx, false, Culprit); |
Eli Friedman | 4c27ac2 | 2013-07-16 22:40:53 +0000 | [diff] [blame] | 2759 | |
| 2760 | case SubstNonTypeTemplateParmExprClass: |
| 2761 | return cast<SubstNonTypeTemplateParmExpr>(this)->getReplacement() |
Abramo Bagnara | 847c660 | 2014-05-22 19:20:46 +0000 | [diff] [blame] | 2762 | ->isConstantInitializer(Ctx, false, Culprit); |
Eli Friedman | 4c27ac2 | 2013-07-16 22:40:53 +0000 | [diff] [blame] | 2763 | case CXXDefaultArgExprClass: |
| 2764 | return cast<CXXDefaultArgExpr>(this)->getExpr() |
Abramo Bagnara | 847c660 | 2014-05-22 19:20:46 +0000 | [diff] [blame] | 2765 | ->isConstantInitializer(Ctx, false, Culprit); |
Eli Friedman | 4c27ac2 | 2013-07-16 22:40:53 +0000 | [diff] [blame] | 2766 | case CXXDefaultInitExprClass: |
| 2767 | return cast<CXXDefaultInitExpr>(this)->getExpr() |
Abramo Bagnara | 847c660 | 2014-05-22 19:20:46 +0000 | [diff] [blame] | 2768 | ->isConstantInitializer(Ctx, false, Culprit); |
Anders Carlsson | a7c5eb7 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 2769 | } |
Richard Smith | ce8eca5 | 2015-12-08 03:21:47 +0000 | [diff] [blame] | 2770 | // Allow certain forms of UB in constant initializers: signed integer |
| 2771 | // overflow and floating-point division by zero. We'll give a warning on |
| 2772 | // these, but they're common enough that we have to accept them. |
| 2773 | if (isEvaluatable(Ctx, SE_AllowUndefinedBehavior)) |
Abramo Bagnara | 847c660 | 2014-05-22 19:20:46 +0000 | [diff] [blame] | 2774 | return true; |
| 2775 | if (Culprit) |
| 2776 | *Culprit = this; |
| 2777 | return false; |
Steve Naroff | b03f594 | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 2778 | } |
| 2779 | |
Scott Douglass | cc01359 | 2015-06-10 15:18:23 +0000 | [diff] [blame] | 2780 | namespace { |
| 2781 | /// \brief Look for any side effects within a Stmt. |
| 2782 | class SideEffectFinder : public ConstEvaluatedExprVisitor<SideEffectFinder> { |
| 2783 | typedef ConstEvaluatedExprVisitor<SideEffectFinder> Inherited; |
| 2784 | const bool IncludePossibleEffects; |
| 2785 | bool HasSideEffects; |
| 2786 | |
| 2787 | public: |
| 2788 | explicit SideEffectFinder(const ASTContext &Context, bool IncludePossible) |
| 2789 | : Inherited(Context), |
| 2790 | IncludePossibleEffects(IncludePossible), HasSideEffects(false) { } |
| 2791 | |
| 2792 | bool hasSideEffects() const { return HasSideEffects; } |
| 2793 | |
| 2794 | void VisitExpr(const Expr *E) { |
| 2795 | if (!HasSideEffects && |
| 2796 | E->HasSideEffects(Context, IncludePossibleEffects)) |
| 2797 | HasSideEffects = true; |
| 2798 | } |
| 2799 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 2800 | } |
Scott Douglass | cc01359 | 2015-06-10 15:18:23 +0000 | [diff] [blame] | 2801 | |
Aaron Ballman | 6c93b3e | 2014-12-17 21:57:17 +0000 | [diff] [blame] | 2802 | bool Expr::HasSideEffects(const ASTContext &Ctx, |
| 2803 | bool IncludePossibleEffects) const { |
| 2804 | // In circumstances where we care about definite side effects instead of |
| 2805 | // potential side effects, we want to ignore expressions that are part of a |
| 2806 | // macro expansion as a potential side effect. |
| 2807 | if (!IncludePossibleEffects && getExprLoc().isMacroID()) |
| 2808 | return false; |
| 2809 | |
Richard Smith | 0421ce7 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 2810 | if (isInstantiationDependent()) |
Aaron Ballman | 6c93b3e | 2014-12-17 21:57:17 +0000 | [diff] [blame] | 2811 | return IncludePossibleEffects; |
Richard Smith | 0421ce7 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 2812 | |
| 2813 | switch (getStmtClass()) { |
| 2814 | case NoStmtClass: |
| 2815 | #define ABSTRACT_STMT(Type) |
| 2816 | #define STMT(Type, Base) case Type##Class: |
| 2817 | #define EXPR(Type, Base) |
| 2818 | #include "clang/AST/StmtNodes.inc" |
| 2819 | llvm_unreachable("unexpected Expr kind"); |
| 2820 | |
| 2821 | case DependentScopeDeclRefExprClass: |
| 2822 | case CXXUnresolvedConstructExprClass: |
| 2823 | case CXXDependentScopeMemberExprClass: |
| 2824 | case UnresolvedLookupExprClass: |
| 2825 | case UnresolvedMemberExprClass: |
| 2826 | case PackExpansionExprClass: |
| 2827 | case SubstNonTypeTemplateParmPackExprClass: |
Richard Smith | b15fe3a | 2012-09-12 00:56:43 +0000 | [diff] [blame] | 2828 | case FunctionParmPackExprClass: |
Kaelyn Takata | e1f49d5 | 2014-10-27 18:07:20 +0000 | [diff] [blame] | 2829 | case TypoExprClass: |
Richard Smith | 0f0af19 | 2014-11-08 05:07:16 +0000 | [diff] [blame] | 2830 | case CXXFoldExprClass: |
Richard Smith | 0421ce7 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 2831 | llvm_unreachable("shouldn't see dependent / unresolved nodes here"); |
| 2832 | |
Richard Smith | a33e4fe | 2012-08-07 05:18:29 +0000 | [diff] [blame] | 2833 | case DeclRefExprClass: |
| 2834 | case ObjCIvarRefExprClass: |
Richard Smith | 0421ce7 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 2835 | case PredefinedExprClass: |
| 2836 | case IntegerLiteralClass: |
| 2837 | case FloatingLiteralClass: |
| 2838 | case ImaginaryLiteralClass: |
| 2839 | case StringLiteralClass: |
| 2840 | case CharacterLiteralClass: |
| 2841 | case OffsetOfExprClass: |
| 2842 | case ImplicitValueInitExprClass: |
| 2843 | case UnaryExprOrTypeTraitExprClass: |
| 2844 | case AddrLabelExprClass: |
| 2845 | case GNUNullExprClass: |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2846 | case NoInitExprClass: |
Richard Smith | 0421ce7 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 2847 | case CXXBoolLiteralExprClass: |
| 2848 | case CXXNullPtrLiteralExprClass: |
| 2849 | case CXXThisExprClass: |
| 2850 | case CXXScalarValueInitExprClass: |
| 2851 | case TypeTraitExprClass: |
Richard Smith | 0421ce7 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 2852 | case ArrayTypeTraitExprClass: |
| 2853 | case ExpressionTraitExprClass: |
| 2854 | case CXXNoexceptExprClass: |
| 2855 | case SizeOfPackExprClass: |
| 2856 | case ObjCStringLiteralClass: |
| 2857 | case ObjCEncodeExprClass: |
| 2858 | case ObjCBoolLiteralExprClass: |
| 2859 | case CXXUuidofExprClass: |
| 2860 | case OpaqueValueExprClass: |
| 2861 | // These never have a side-effect. |
| 2862 | return false; |
| 2863 | |
| 2864 | case CallExprClass: |
Aaron Ballman | 6c93b3e | 2014-12-17 21:57:17 +0000 | [diff] [blame] | 2865 | case CXXOperatorCallExprClass: |
| 2866 | case CXXMemberCallExprClass: |
| 2867 | case CUDAKernelCallExprClass: |
Michael Kuperstein | aed5ccd | 2015-04-06 13:22:01 +0000 | [diff] [blame] | 2868 | case UserDefinedLiteralClass: { |
| 2869 | // We don't know a call definitely has side effects, except for calls |
| 2870 | // to pure/const functions that definitely don't. |
| 2871 | // If the call itself is considered side-effect free, check the operands. |
| 2872 | const Decl *FD = cast<CallExpr>(this)->getCalleeDecl(); |
| 2873 | bool IsPure = FD && (FD->hasAttr<ConstAttr>() || FD->hasAttr<PureAttr>()); |
| 2874 | if (IsPure || !IncludePossibleEffects) |
| 2875 | break; |
| 2876 | return true; |
| 2877 | } |
| 2878 | |
Aaron Ballman | 6c93b3e | 2014-12-17 21:57:17 +0000 | [diff] [blame] | 2879 | case BlockExprClass: |
| 2880 | case CXXBindTemporaryExprClass: |
Aaron Ballman | 6c93b3e | 2014-12-17 21:57:17 +0000 | [diff] [blame] | 2881 | if (!IncludePossibleEffects) |
| 2882 | break; |
| 2883 | return true; |
| 2884 | |
John McCall | 5e77d76 | 2013-04-16 07:28:30 +0000 | [diff] [blame] | 2885 | case MSPropertyRefExprClass: |
Alexey Bataev | f763027 | 2015-11-25 12:01:00 +0000 | [diff] [blame] | 2886 | case MSPropertySubscriptExprClass: |
Richard Smith | 0421ce7 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 2887 | case CompoundAssignOperatorClass: |
| 2888 | case VAArgExprClass: |
| 2889 | case AtomicExprClass: |
Richard Smith | 0421ce7 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 2890 | case CXXThrowExprClass: |
| 2891 | case CXXNewExprClass: |
| 2892 | case CXXDeleteExprClass: |
| 2893 | case ExprWithCleanupsClass: |
Richard Smith | 9f690bd | 2015-10-27 06:02:45 +0000 | [diff] [blame] | 2894 | case CoawaitExprClass: |
| 2895 | case CoyieldExprClass: |
Richard Smith | 0421ce7 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 2896 | // These always have a side-effect. |
| 2897 | return true; |
| 2898 | |
Scott Douglass | cc01359 | 2015-06-10 15:18:23 +0000 | [diff] [blame] | 2899 | case StmtExprClass: { |
| 2900 | // StmtExprs have a side-effect if any substatement does. |
| 2901 | SideEffectFinder Finder(Ctx, IncludePossibleEffects); |
| 2902 | Finder.Visit(cast<StmtExpr>(this)->getSubStmt()); |
| 2903 | return Finder.hasSideEffects(); |
| 2904 | } |
| 2905 | |
Richard Smith | 0421ce7 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 2906 | case ParenExprClass: |
| 2907 | case ArraySubscriptExprClass: |
Alexey Bataev | 1a3320e | 2015-08-25 14:24:04 +0000 | [diff] [blame] | 2908 | case OMPArraySectionExprClass: |
Richard Smith | 0421ce7 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 2909 | case MemberExprClass: |
| 2910 | case ConditionalOperatorClass: |
| 2911 | case BinaryConditionalOperatorClass: |
Richard Smith | 0421ce7 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 2912 | case CompoundLiteralExprClass: |
| 2913 | case ExtVectorElementExprClass: |
| 2914 | case DesignatedInitExprClass: |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 2915 | case DesignatedInitUpdateExprClass: |
Richard Smith | 0421ce7 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 2916 | case ParenListExprClass: |
Richard Smith | 0421ce7 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 2917 | case CXXPseudoDestructorExprClass: |
Richard Smith | cc1b96d | 2013-06-12 22:31:48 +0000 | [diff] [blame] | 2918 | case CXXStdInitializerListExprClass: |
Richard Smith | 0421ce7 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 2919 | case SubstNonTypeTemplateParmExprClass: |
| 2920 | case MaterializeTemporaryExprClass: |
| 2921 | case ShuffleVectorExprClass: |
Hal Finkel | c4d7c82 | 2013-09-18 03:29:45 +0000 | [diff] [blame] | 2922 | case ConvertVectorExprClass: |
Richard Smith | 0421ce7 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 2923 | case AsTypeExprClass: |
| 2924 | // These have a side-effect if any subexpression does. |
| 2925 | break; |
| 2926 | |
Richard Smith | a33e4fe | 2012-08-07 05:18:29 +0000 | [diff] [blame] | 2927 | case UnaryOperatorClass: |
| 2928 | if (cast<UnaryOperator>(this)->isIncrementDecrementOp()) |
Richard Smith | 0421ce7 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 2929 | return true; |
| 2930 | break; |
Richard Smith | 0421ce7 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 2931 | |
| 2932 | case BinaryOperatorClass: |
| 2933 | if (cast<BinaryOperator>(this)->isAssignmentOp()) |
| 2934 | return true; |
| 2935 | break; |
| 2936 | |
Richard Smith | 0421ce7 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 2937 | case InitListExprClass: |
| 2938 | // FIXME: The children for an InitListExpr doesn't include the array filler. |
| 2939 | if (const Expr *E = cast<InitListExpr>(this)->getArrayFiller()) |
Aaron Ballman | 6c93b3e | 2014-12-17 21:57:17 +0000 | [diff] [blame] | 2940 | if (E->HasSideEffects(Ctx, IncludePossibleEffects)) |
Richard Smith | 0421ce7 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 2941 | return true; |
| 2942 | break; |
| 2943 | |
| 2944 | case GenericSelectionExprClass: |
| 2945 | return cast<GenericSelectionExpr>(this)->getResultExpr()-> |
Aaron Ballman | 6c93b3e | 2014-12-17 21:57:17 +0000 | [diff] [blame] | 2946 | HasSideEffects(Ctx, IncludePossibleEffects); |
Richard Smith | 0421ce7 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 2947 | |
| 2948 | case ChooseExprClass: |
Aaron Ballman | 6c93b3e | 2014-12-17 21:57:17 +0000 | [diff] [blame] | 2949 | return cast<ChooseExpr>(this)->getChosenSubExpr()->HasSideEffects( |
| 2950 | Ctx, IncludePossibleEffects); |
Richard Smith | 0421ce7 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 2951 | |
| 2952 | case CXXDefaultArgExprClass: |
Aaron Ballman | 6c93b3e | 2014-12-17 21:57:17 +0000 | [diff] [blame] | 2953 | return cast<CXXDefaultArgExpr>(this)->getExpr()->HasSideEffects( |
| 2954 | Ctx, IncludePossibleEffects); |
Richard Smith | 0421ce7 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 2955 | |
Reid Kleckner | d60b82f | 2014-11-17 23:36:45 +0000 | [diff] [blame] | 2956 | case CXXDefaultInitExprClass: { |
| 2957 | const FieldDecl *FD = cast<CXXDefaultInitExpr>(this)->getField(); |
| 2958 | if (const Expr *E = FD->getInClassInitializer()) |
Aaron Ballman | 6c93b3e | 2014-12-17 21:57:17 +0000 | [diff] [blame] | 2959 | return E->HasSideEffects(Ctx, IncludePossibleEffects); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 2960 | // If we've not yet parsed the initializer, assume it has side-effects. |
| 2961 | return true; |
Reid Kleckner | d60b82f | 2014-11-17 23:36:45 +0000 | [diff] [blame] | 2962 | } |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 2963 | |
Richard Smith | 0421ce7 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 2964 | case CXXDynamicCastExprClass: { |
| 2965 | // A dynamic_cast expression has side-effects if it can throw. |
| 2966 | const CXXDynamicCastExpr *DCE = cast<CXXDynamicCastExpr>(this); |
| 2967 | if (DCE->getTypeAsWritten()->isReferenceType() && |
| 2968 | DCE->getCastKind() == CK_Dynamic) |
| 2969 | return true; |
Richard Smith | a33e4fe | 2012-08-07 05:18:29 +0000 | [diff] [blame] | 2970 | } // Fall through. |
| 2971 | case ImplicitCastExprClass: |
| 2972 | case CStyleCastExprClass: |
| 2973 | case CXXStaticCastExprClass: |
| 2974 | case CXXReinterpretCastExprClass: |
| 2975 | case CXXConstCastExprClass: |
| 2976 | case CXXFunctionalCastExprClass: { |
Aaron Ballman | 409af50 | 2015-01-03 17:00:12 +0000 | [diff] [blame] | 2977 | // While volatile reads are side-effecting in both C and C++, we treat them |
| 2978 | // as having possible (not definite) side-effects. This allows idiomatic |
| 2979 | // code to behave without warning, such as sizeof(*v) for a volatile- |
| 2980 | // qualified pointer. |
| 2981 | if (!IncludePossibleEffects) |
| 2982 | break; |
| 2983 | |
Richard Smith | a33e4fe | 2012-08-07 05:18:29 +0000 | [diff] [blame] | 2984 | const CastExpr *CE = cast<CastExpr>(this); |
| 2985 | if (CE->getCastKind() == CK_LValueToRValue && |
| 2986 | CE->getSubExpr()->getType().isVolatileQualified()) |
| 2987 | return true; |
Richard Smith | 0421ce7 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 2988 | break; |
| 2989 | } |
| 2990 | |
Richard Smith | ef8bf43 | 2012-08-13 20:08:14 +0000 | [diff] [blame] | 2991 | case CXXTypeidExprClass: |
| 2992 | // typeid might throw if its subexpression is potentially-evaluated, so has |
| 2993 | // side-effects in that case whether or not its subexpression does. |
| 2994 | return cast<CXXTypeidExpr>(this)->isPotentiallyEvaluated(); |
Richard Smith | 0421ce7 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 2995 | |
| 2996 | case CXXConstructExprClass: |
| 2997 | case CXXTemporaryObjectExprClass: { |
| 2998 | const CXXConstructExpr *CE = cast<CXXConstructExpr>(this); |
Aaron Ballman | 6c93b3e | 2014-12-17 21:57:17 +0000 | [diff] [blame] | 2999 | if (!CE->getConstructor()->isTrivial() && IncludePossibleEffects) |
Richard Smith | 0421ce7 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 3000 | return true; |
Richard Smith | a33e4fe | 2012-08-07 05:18:29 +0000 | [diff] [blame] | 3001 | // A trivial constructor does not add any side-effects of its own. Just look |
| 3002 | // at its arguments. |
Richard Smith | 0421ce7 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 3003 | break; |
| 3004 | } |
| 3005 | |
| 3006 | case LambdaExprClass: { |
| 3007 | const LambdaExpr *LE = cast<LambdaExpr>(this); |
| 3008 | for (LambdaExpr::capture_iterator I = LE->capture_begin(), |
| 3009 | E = LE->capture_end(); I != E; ++I) |
| 3010 | if (I->getCaptureKind() == LCK_ByCopy) |
| 3011 | // FIXME: Only has a side-effect if the variable is volatile or if |
| 3012 | // the copy would invoke a non-trivial copy constructor. |
| 3013 | return true; |
| 3014 | return false; |
| 3015 | } |
| 3016 | |
| 3017 | case PseudoObjectExprClass: { |
| 3018 | // Only look for side-effects in the semantic form, and look past |
| 3019 | // OpaqueValueExpr bindings in that form. |
| 3020 | const PseudoObjectExpr *PO = cast<PseudoObjectExpr>(this); |
| 3021 | for (PseudoObjectExpr::const_semantics_iterator I = PO->semantics_begin(), |
| 3022 | E = PO->semantics_end(); |
| 3023 | I != E; ++I) { |
| 3024 | const Expr *Subexpr = *I; |
| 3025 | if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(Subexpr)) |
| 3026 | Subexpr = OVE->getSourceExpr(); |
Aaron Ballman | 6c93b3e | 2014-12-17 21:57:17 +0000 | [diff] [blame] | 3027 | if (Subexpr->HasSideEffects(Ctx, IncludePossibleEffects)) |
Richard Smith | 0421ce7 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 3028 | return true; |
| 3029 | } |
| 3030 | return false; |
| 3031 | } |
| 3032 | |
| 3033 | case ObjCBoxedExprClass: |
| 3034 | case ObjCArrayLiteralClass: |
| 3035 | case ObjCDictionaryLiteralClass: |
Richard Smith | 0421ce7 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 3036 | case ObjCSelectorExprClass: |
| 3037 | case ObjCProtocolExprClass: |
Richard Smith | 0421ce7 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 3038 | case ObjCIsaExprClass: |
| 3039 | case ObjCIndirectCopyRestoreExprClass: |
| 3040 | case ObjCSubscriptRefExprClass: |
| 3041 | case ObjCBridgedCastExprClass: |
Aaron Ballman | 6c93b3e | 2014-12-17 21:57:17 +0000 | [diff] [blame] | 3042 | case ObjCMessageExprClass: |
| 3043 | case ObjCPropertyRefExprClass: |
| 3044 | // FIXME: Classify these cases better. |
| 3045 | if (IncludePossibleEffects) |
| 3046 | return true; |
| 3047 | break; |
Richard Smith | 0421ce7 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 3048 | } |
| 3049 | |
| 3050 | // Recurse to children. |
Benjamin Kramer | 642f173 | 2015-07-02 21:03:14 +0000 | [diff] [blame] | 3051 | for (const Stmt *SubStmt : children()) |
| 3052 | if (SubStmt && |
| 3053 | cast<Expr>(SubStmt)->HasSideEffects(Ctx, IncludePossibleEffects)) |
| 3054 | return true; |
Richard Smith | 0421ce7 | 2012-08-07 04:16:51 +0000 | [diff] [blame] | 3055 | |
| 3056 | return false; |
| 3057 | } |
| 3058 | |
Douglas Gregor | 1be329d | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 3059 | namespace { |
| 3060 | /// \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] | 3061 | class NonTrivialCallFinder : public ConstEvaluatedExprVisitor<NonTrivialCallFinder> |
Douglas Gregor | 1be329d | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 3062 | { |
Scott Douglass | 503fc39 | 2015-06-10 13:53:15 +0000 | [diff] [blame] | 3063 | typedef ConstEvaluatedExprVisitor<NonTrivialCallFinder> Inherited; |
| 3064 | |
Douglas Gregor | 1be329d | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 3065 | bool NonTrivial; |
| 3066 | |
| 3067 | public: |
Scott Douglass | 503fc39 | 2015-06-10 13:53:15 +0000 | [diff] [blame] | 3068 | explicit NonTrivialCallFinder(const ASTContext &Context) |
Douglas Gregor | 6427a5e | 2012-02-23 07:44:18 +0000 | [diff] [blame] | 3069 | : Inherited(Context), NonTrivial(false) { } |
Douglas Gregor | 1be329d | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 3070 | |
| 3071 | bool hasNonTrivialCall() const { return NonTrivial; } |
Scott Douglass | 503fc39 | 2015-06-10 13:53:15 +0000 | [diff] [blame] | 3072 | |
| 3073 | void VisitCallExpr(const CallExpr *E) { |
| 3074 | if (const CXXMethodDecl *Method |
| 3075 | = dyn_cast_or_null<const CXXMethodDecl>(E->getCalleeDecl())) { |
Douglas Gregor | 1be329d | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 3076 | if (Method->isTrivial()) { |
| 3077 | // Recurse to children of the call. |
| 3078 | Inherited::VisitStmt(E); |
| 3079 | return; |
| 3080 | } |
| 3081 | } |
| 3082 | |
| 3083 | NonTrivial = true; |
| 3084 | } |
Scott Douglass | 503fc39 | 2015-06-10 13:53:15 +0000 | [diff] [blame] | 3085 | |
| 3086 | void VisitCXXConstructExpr(const CXXConstructExpr *E) { |
Douglas Gregor | 1be329d | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 3087 | if (E->getConstructor()->isTrivial()) { |
| 3088 | // Recurse to children of the call. |
| 3089 | Inherited::VisitStmt(E); |
| 3090 | return; |
| 3091 | } |
| 3092 | |
| 3093 | NonTrivial = true; |
| 3094 | } |
Scott Douglass | 503fc39 | 2015-06-10 13:53:15 +0000 | [diff] [blame] | 3095 | |
| 3096 | void VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *E) { |
Douglas Gregor | 1be329d | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 3097 | if (E->getTemporary()->getDestructor()->isTrivial()) { |
| 3098 | Inherited::VisitStmt(E); |
| 3099 | return; |
| 3100 | } |
| 3101 | |
| 3102 | NonTrivial = true; |
| 3103 | } |
| 3104 | }; |
Alexander Kornienko | ab9db51 | 2015-06-22 23:07:51 +0000 | [diff] [blame] | 3105 | } |
Douglas Gregor | 1be329d | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 3106 | |
Scott Douglass | 503fc39 | 2015-06-10 13:53:15 +0000 | [diff] [blame] | 3107 | bool Expr::hasNonTrivialCall(const ASTContext &Ctx) const { |
Douglas Gregor | 1be329d | 2012-02-23 07:33:15 +0000 | [diff] [blame] | 3108 | NonTrivialCallFinder Finder(Ctx); |
| 3109 | Finder.Visit(this); |
| 3110 | return Finder.hasNonTrivialCall(); |
| 3111 | } |
| 3112 | |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 3113 | /// isNullPointerConstant - C99 6.3.2.3p3 - Return whether this is a null |
| 3114 | /// pointer constant or not, as well as the specific kind of constant detected. |
| 3115 | /// Null pointer constants can be integer constant expressions with the |
| 3116 | /// value zero, casts of zero to void*, nullptr (C++0X), or __null |
| 3117 | /// (a GNU extension). |
| 3118 | Expr::NullPointerConstantKind |
| 3119 | Expr::isNullPointerConstant(ASTContext &Ctx, |
| 3120 | NullPointerConstantValueDependence NPC) const { |
Reid Kleckner | a5eef14 | 2013-11-12 02:22:34 +0000 | [diff] [blame] | 3121 | if (isValueDependent() && |
Alp Toker | bfa3934 | 2014-01-14 12:51:41 +0000 | [diff] [blame] | 3122 | (!Ctx.getLangOpts().CPlusPlus11 || Ctx.getLangOpts().MSVCCompat)) { |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 3123 | switch (NPC) { |
| 3124 | case NPC_NeverValueDependent: |
David Blaikie | 83d382b | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3125 | llvm_unreachable("Unexpected value dependent expression!"); |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 3126 | case NPC_ValueDependentIsNull: |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 3127 | if (isTypeDependent() || getType()->isIntegralType(Ctx)) |
David Blaikie | 1c7c8f7 | 2012-08-08 17:33:31 +0000 | [diff] [blame] | 3128 | return NPCK_ZeroExpression; |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 3129 | else |
| 3130 | return NPCK_NotNull; |
Alexis Hunt | a8136cc | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 3131 | |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 3132 | case NPC_ValueDependentIsNotNull: |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 3133 | return NPCK_NotNull; |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 3134 | } |
| 3135 | } |
Daniel Dunbar | ebc5140 | 2009-09-18 08:46:16 +0000 | [diff] [blame] | 3136 | |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 3137 | // Strip off a cast to void*, if it exists. Except in C++. |
Argyrios Kyrtzidis | 3bab3d2 | 2008-08-18 23:01:59 +0000 | [diff] [blame] | 3138 | if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) { |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3139 | if (!Ctx.getLangOpts().CPlusPlus) { |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 3140 | // Check that it is a cast to void*. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3141 | if (const PointerType *PT = CE->getType()->getAs<PointerType>()) { |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 3142 | QualType Pointee = PT->getPointeeType(); |
Anastasia Stulova | 2446b8b | 2015-12-11 17:41:19 +0000 | [diff] [blame] | 3143 | Qualifiers Q = Pointee.getQualifiers(); |
| 3144 | // In OpenCL v2.0 generic address space acts as a placeholder |
| 3145 | // and should be ignored. |
| 3146 | bool IsASValid = true; |
| 3147 | if (Ctx.getLangOpts().OpenCLVersion >= 200) { |
| 3148 | if (Pointee.getAddressSpace() == LangAS::opencl_generic) |
| 3149 | Q.removeAddressSpace(); |
| 3150 | else |
| 3151 | IsASValid = false; |
| 3152 | } |
| 3153 | |
| 3154 | if (IsASValid && !Q.hasQualifiers() && |
| 3155 | Pointee->isVoidType() && // to void* |
| 3156 | CE->getSubExpr()->getType()->isIntegerType()) // from int. |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 3157 | return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC); |
Sebastian Redl | 72b8aef | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 3158 | } |
Steve Naroff | ada7d42 | 2007-05-20 17:54:12 +0000 | [diff] [blame] | 3159 | } |
Steve Naroff | 4871fe0 | 2008-01-14 16:10:57 +0000 | [diff] [blame] | 3160 | } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) { |
| 3161 | // Ignore the ImplicitCastExpr type entirely. |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 3162 | return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC); |
Steve Naroff | 4871fe0 | 2008-01-14 16:10:57 +0000 | [diff] [blame] | 3163 | } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) { |
| 3164 | // Accept ((void*)0) as a null pointer constant, as many other |
| 3165 | // implementations do. |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 3166 | return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC); |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 3167 | } else if (const GenericSelectionExpr *GE = |
| 3168 | dyn_cast<GenericSelectionExpr>(this)) { |
Eli Friedman | 75807f2 | 2013-07-20 00:40:58 +0000 | [diff] [blame] | 3169 | if (GE->isResultDependent()) |
| 3170 | return NPCK_NotNull; |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 3171 | return GE->getResultExpr()->isNullPointerConstant(Ctx, NPC); |
Eli Friedman | 75807f2 | 2013-07-20 00:40:58 +0000 | [diff] [blame] | 3172 | } else if (const ChooseExpr *CE = dyn_cast<ChooseExpr>(this)) { |
| 3173 | if (CE->isConditionDependent()) |
| 3174 | return NPCK_NotNull; |
| 3175 | return CE->getChosenSubExpr()->isNullPointerConstant(Ctx, NPC); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3176 | } else if (const CXXDefaultArgExpr *DefaultArg |
Chris Lattner | 5825824 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 3177 | = dyn_cast<CXXDefaultArgExpr>(this)) { |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 3178 | // See through default argument expressions. |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 3179 | return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC); |
Richard Smith | 852c9db | 2013-04-20 22:23:05 +0000 | [diff] [blame] | 3180 | } else if (const CXXDefaultInitExpr *DefaultInit |
| 3181 | = dyn_cast<CXXDefaultInitExpr>(this)) { |
| 3182 | // See through default initializer expressions. |
| 3183 | return DefaultInit->getExpr()->isNullPointerConstant(Ctx, NPC); |
Douglas Gregor | 3be4b12 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 3184 | } else if (isa<GNUNullExpr>(this)) { |
| 3185 | // The GNU __null extension is always a null pointer constant. |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 3186 | return NPCK_GNUNull; |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 3187 | } else if (const MaterializeTemporaryExpr *M |
| 3188 | = dyn_cast<MaterializeTemporaryExpr>(this)) { |
| 3189 | return M->GetTemporaryExpr()->isNullPointerConstant(Ctx, NPC); |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 3190 | } else if (const OpaqueValueExpr *OVE = dyn_cast<OpaqueValueExpr>(this)) { |
| 3191 | if (const Expr *Source = OVE->getSourceExpr()) |
| 3192 | return Source->isNullPointerConstant(Ctx, NPC); |
Steve Naroff | 0903531 | 2008-01-14 02:53:34 +0000 | [diff] [blame] | 3193 | } |
Douglas Gregor | 3be4b12 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 3194 | |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 3195 | // C++11 nullptr_t is always a null pointer constant. |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 3196 | if (getType()->isNullPtrType()) |
Richard Smith | 89645bc | 2013-01-02 12:01:23 +0000 | [diff] [blame] | 3197 | return NPCK_CXX11_nullptr; |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 3198 | |
Fariborz Jahanian | 3567c42 | 2010-09-27 22:42:37 +0000 | [diff] [blame] | 3199 | if (const RecordType *UT = getType()->getAsUnionType()) |
Richard Smith | 4055de4 | 2013-06-13 02:46:14 +0000 | [diff] [blame] | 3200 | if (!Ctx.getLangOpts().CPlusPlus11 && |
| 3201 | UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
Fariborz Jahanian | 3567c42 | 2010-09-27 22:42:37 +0000 | [diff] [blame] | 3202 | if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){ |
| 3203 | const Expr *InitExpr = CLE->getInitializer(); |
| 3204 | if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr)) |
| 3205 | return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC); |
| 3206 | } |
Steve Naroff | 4871fe0 | 2008-01-14 16:10:57 +0000 | [diff] [blame] | 3207 | // This expression must be an integer type. |
Alexis Hunt | a8136cc | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 3208 | if (!getType()->isIntegerType() || |
David Blaikie | bbafb8a | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3209 | (Ctx.getLangOpts().CPlusPlus && getType()->isEnumeralType())) |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 3210 | return NPCK_NotNull; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3211 | |
Richard Smith | 2bf7fdb | 2013-01-02 11:42:31 +0000 | [diff] [blame] | 3212 | if (Ctx.getLangOpts().CPlusPlus11) { |
Richard Smith | 4055de4 | 2013-06-13 02:46:14 +0000 | [diff] [blame] | 3213 | // C++11 [conv.ptr]p1: A null pointer constant is an integer literal with |
| 3214 | // value zero or a prvalue of type std::nullptr_t. |
Reid Kleckner | a5eef14 | 2013-11-12 02:22:34 +0000 | [diff] [blame] | 3215 | // Microsoft mode permits C++98 rules reflecting MSVC behavior. |
Richard Smith | 4055de4 | 2013-06-13 02:46:14 +0000 | [diff] [blame] | 3216 | const IntegerLiteral *Lit = dyn_cast<IntegerLiteral>(this); |
Reid Kleckner | a5eef14 | 2013-11-12 02:22:34 +0000 | [diff] [blame] | 3217 | if (Lit && !Lit->getValue()) |
| 3218 | return NPCK_ZeroLiteral; |
Alp Toker | bfa3934 | 2014-01-14 12:51:41 +0000 | [diff] [blame] | 3219 | else if (!Ctx.getLangOpts().MSVCCompat || !isCXX98IntegralConstantExpr(Ctx)) |
Reid Kleckner | a5eef14 | 2013-11-12 02:22:34 +0000 | [diff] [blame] | 3220 | return NPCK_NotNull; |
Richard Smith | 98a0a49 | 2012-02-14 21:38:30 +0000 | [diff] [blame] | 3221 | } else { |
Richard Smith | 4055de4 | 2013-06-13 02:46:14 +0000 | [diff] [blame] | 3222 | // If we have an integer constant expression, we need to *evaluate* it and |
| 3223 | // test for the value 0. |
Richard Smith | 98a0a49 | 2012-02-14 21:38:30 +0000 | [diff] [blame] | 3224 | if (!isIntegerConstantExpr(Ctx)) |
| 3225 | return NPCK_NotNull; |
| 3226 | } |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 3227 | |
David Blaikie | 1c7c8f7 | 2012-08-08 17:33:31 +0000 | [diff] [blame] | 3228 | if (EvaluateKnownConstInt(Ctx) != 0) |
| 3229 | return NPCK_NotNull; |
| 3230 | |
| 3231 | if (isa<IntegerLiteral>(this)) |
| 3232 | return NPCK_ZeroLiteral; |
| 3233 | return NPCK_ZeroExpression; |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 3234 | } |
Steve Naroff | f7a5da1 | 2007-07-28 23:10:27 +0000 | [diff] [blame] | 3235 | |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3236 | /// \brief If this expression is an l-value for an Objective C |
| 3237 | /// property, find the underlying property reference expression. |
| 3238 | const ObjCPropertyRefExpr *Expr::getObjCProperty() const { |
| 3239 | const Expr *E = this; |
| 3240 | while (true) { |
| 3241 | assert((E->getValueKind() == VK_LValue && |
| 3242 | E->getObjectKind() == OK_ObjCProperty) && |
| 3243 | "expression is not a property reference"); |
| 3244 | E = E->IgnoreParenCasts(); |
| 3245 | if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
| 3246 | if (BO->getOpcode() == BO_Comma) { |
| 3247 | E = BO->getRHS(); |
| 3248 | continue; |
| 3249 | } |
| 3250 | } |
| 3251 | |
| 3252 | break; |
| 3253 | } |
| 3254 | |
| 3255 | return cast<ObjCPropertyRefExpr>(E); |
| 3256 | } |
| 3257 | |
Anna Zaks | 97c7ce3 | 2012-10-01 20:34:04 +0000 | [diff] [blame] | 3258 | bool Expr::isObjCSelfExpr() const { |
| 3259 | const Expr *E = IgnoreParenImpCasts(); |
| 3260 | |
| 3261 | const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E); |
| 3262 | if (!DRE) |
| 3263 | return false; |
| 3264 | |
| 3265 | const ImplicitParamDecl *Param = dyn_cast<ImplicitParamDecl>(DRE->getDecl()); |
| 3266 | if (!Param) |
| 3267 | return false; |
| 3268 | |
| 3269 | const ObjCMethodDecl *M = dyn_cast<ObjCMethodDecl>(Param->getDeclContext()); |
| 3270 | if (!M) |
| 3271 | return false; |
| 3272 | |
| 3273 | return M->getSelfDecl() == Param; |
| 3274 | } |
| 3275 | |
John McCall | d25db7e | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 3276 | FieldDecl *Expr::getSourceBitField() { |
Douglas Gregor | 19623dc | 2009-07-06 15:38:40 +0000 | [diff] [blame] | 3277 | Expr *E = this->IgnoreParens(); |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3278 | |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 3279 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3280 | if (ICE->getCastKind() == CK_LValueToRValue || |
| 3281 | (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp)) |
Douglas Gregor | 65eb86e | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 3282 | E = ICE->getSubExpr()->IgnoreParens(); |
| 3283 | else |
| 3284 | break; |
| 3285 | } |
| 3286 | |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3287 | if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E)) |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 3288 | if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl())) |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3289 | if (Field->isBitField()) |
| 3290 | return Field; |
| 3291 | |
John McCall | d25db7e | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 3292 | if (ObjCIvarRefExpr *IvarRef = dyn_cast<ObjCIvarRefExpr>(E)) |
| 3293 | if (FieldDecl *Ivar = dyn_cast<FieldDecl>(IvarRef->getDecl())) |
| 3294 | if (Ivar->isBitField()) |
| 3295 | return Ivar; |
| 3296 | |
Argyrios Kyrtzidis | d3f0054 | 2010-10-30 19:52:22 +0000 | [diff] [blame] | 3297 | if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E)) |
| 3298 | if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl())) |
| 3299 | if (Field->isBitField()) |
| 3300 | return Field; |
| 3301 | |
Eli Friedman | 609ada2 | 2011-07-13 02:05:57 +0000 | [diff] [blame] | 3302 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) { |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3303 | if (BinOp->isAssignmentOp() && BinOp->getLHS()) |
John McCall | d25db7e | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 3304 | return BinOp->getLHS()->getSourceBitField(); |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3305 | |
Eli Friedman | 609ada2 | 2011-07-13 02:05:57 +0000 | [diff] [blame] | 3306 | if (BinOp->getOpcode() == BO_Comma && BinOp->getRHS()) |
John McCall | d25db7e | 2013-05-06 21:39:12 +0000 | [diff] [blame] | 3307 | return BinOp->getRHS()->getSourceBitField(); |
Eli Friedman | 609ada2 | 2011-07-13 02:05:57 +0000 | [diff] [blame] | 3308 | } |
| 3309 | |
Richard Smith | 5b57167 | 2014-09-24 23:55:00 +0000 | [diff] [blame] | 3310 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(E)) |
| 3311 | if (UnOp->isPrefix() && UnOp->isIncrementDecrementOp()) |
| 3312 | return UnOp->getSubExpr()->getSourceBitField(); |
| 3313 | |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3314 | return nullptr; |
Douglas Gregor | 8e1cf60 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 3315 | } |
| 3316 | |
Anders Carlsson | 8abde4b | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 3317 | bool Expr::refersToVectorElement() const { |
| 3318 | const Expr *E = this->IgnoreParens(); |
Alexis Hunt | a8136cc | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 3319 | |
Anders Carlsson | 8abde4b | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 3320 | while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 3321 | if (ICE->getValueKind() != VK_RValue && |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3322 | ICE->getCastKind() == CK_NoOp) |
Anders Carlsson | 8abde4b | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 3323 | E = ICE->getSubExpr()->IgnoreParens(); |
| 3324 | else |
| 3325 | break; |
| 3326 | } |
Alexis Hunt | a8136cc | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 3327 | |
Anders Carlsson | 8abde4b | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 3328 | if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) |
| 3329 | return ASE->getBase()->getType()->isVectorType(); |
| 3330 | |
| 3331 | if (isa<ExtVectorElementExpr>(E)) |
| 3332 | return true; |
| 3333 | |
| 3334 | return false; |
| 3335 | } |
| 3336 | |
Andrey Bokhanko | d9eab9c | 2015-08-03 10:38:10 +0000 | [diff] [blame] | 3337 | bool Expr::refersToGlobalRegisterVar() const { |
| 3338 | const Expr *E = this->IgnoreParenImpCasts(); |
| 3339 | |
| 3340 | if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) |
| 3341 | if (const auto *VD = dyn_cast<VarDecl>(DRE->getDecl())) |
| 3342 | if (VD->getStorageClass() == SC_Register && |
| 3343 | VD->hasAttr<AsmLabelAttr>() && !VD->isLocalVarDecl()) |
| 3344 | return true; |
| 3345 | |
| 3346 | return false; |
| 3347 | } |
| 3348 | |
Chris Lattner | b8211f6 | 2009-02-16 22:14:05 +0000 | [diff] [blame] | 3349 | /// isArrow - Return true if the base expression is a pointer to vector, |
| 3350 | /// return false if the base expression is a vector. |
| 3351 | bool ExtVectorElementExpr::isArrow() const { |
| 3352 | return getBase()->getType()->isPointerType(); |
| 3353 | } |
| 3354 | |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 3355 | unsigned ExtVectorElementExpr::getNumElements() const { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3356 | if (const VectorType *VT = getType()->getAs<VectorType>()) |
Nate Begeman | f322eab | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 3357 | return VT->getNumElements(); |
| 3358 | return 1; |
Chris Lattner | 177bd45 | 2007-08-03 16:00:20 +0000 | [diff] [blame] | 3359 | } |
| 3360 | |
Nate Begeman | f322eab | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 3361 | /// containsDuplicateElements - Return true if any element access is repeated. |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 3362 | bool ExtVectorElementExpr::containsDuplicateElements() const { |
Daniel Dunbar | cb2a056 | 2009-10-18 02:09:09 +0000 | [diff] [blame] | 3363 | // FIXME: Refactor this code to an accessor on the AST node which returns the |
| 3364 | // "type" of component access, and share with code below and in Sema. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3365 | StringRef Comp = Accessor->getName(); |
Nate Begeman | 7e5185b | 2009-01-18 02:01:21 +0000 | [diff] [blame] | 3366 | |
| 3367 | // Halving swizzles do not contain duplicate elements. |
Daniel Dunbar | 125c9c9 | 2009-10-17 23:53:04 +0000 | [diff] [blame] | 3368 | if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd") |
Nate Begeman | 7e5185b | 2009-01-18 02:01:21 +0000 | [diff] [blame] | 3369 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3370 | |
Nate Begeman | 7e5185b | 2009-01-18 02:01:21 +0000 | [diff] [blame] | 3371 | // Advance past s-char prefix on hex swizzles. |
Daniel Dunbar | 125c9c9 | 2009-10-17 23:53:04 +0000 | [diff] [blame] | 3372 | if (Comp[0] == 's' || Comp[0] == 'S') |
| 3373 | Comp = Comp.substr(1); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3374 | |
Daniel Dunbar | 125c9c9 | 2009-10-17 23:53:04 +0000 | [diff] [blame] | 3375 | for (unsigned i = 0, e = Comp.size(); i != e; ++i) |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3376 | if (Comp.substr(i + 1).find(Comp[i]) != StringRef::npos) |
Steve Naroff | 0d595ca | 2007-07-30 03:29:09 +0000 | [diff] [blame] | 3377 | return true; |
Daniel Dunbar | 125c9c9 | 2009-10-17 23:53:04 +0000 | [diff] [blame] | 3378 | |
Steve Naroff | 0d595ca | 2007-07-30 03:29:09 +0000 | [diff] [blame] | 3379 | return false; |
| 3380 | } |
Chris Lattner | 885b495 | 2007-08-02 23:36:59 +0000 | [diff] [blame] | 3381 | |
Nate Begeman | f322eab | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 3382 | /// getEncodedElementAccess - We encode the fields as a llvm ConstantArray. |
Nate Begeman | d386215 | 2008-05-13 21:03:02 +0000 | [diff] [blame] | 3383 | void ExtVectorElementExpr::getEncodedElementAccess( |
Benjamin Kramer | 9938310 | 2015-07-28 16:25:32 +0000 | [diff] [blame] | 3384 | SmallVectorImpl<uint32_t> &Elts) const { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3385 | StringRef Comp = Accessor->getName(); |
Daniel Dunbar | ce5a0b3 | 2009-10-18 02:09:31 +0000 | [diff] [blame] | 3386 | if (Comp[0] == 's' || Comp[0] == 'S') |
| 3387 | Comp = Comp.substr(1); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3388 | |
Daniel Dunbar | ce5a0b3 | 2009-10-18 02:09:31 +0000 | [diff] [blame] | 3389 | bool isHi = Comp == "hi"; |
| 3390 | bool isLo = Comp == "lo"; |
| 3391 | bool isEven = Comp == "even"; |
| 3392 | bool isOdd = Comp == "odd"; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3393 | |
Nate Begeman | f322eab | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 3394 | for (unsigned i = 0, e = getNumElements(); i != e; ++i) { |
| 3395 | uint64_t Index; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3396 | |
Nate Begeman | f322eab | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 3397 | if (isHi) |
| 3398 | Index = e + i; |
| 3399 | else if (isLo) |
| 3400 | Index = i; |
| 3401 | else if (isEven) |
| 3402 | Index = 2 * i; |
| 3403 | else if (isOdd) |
| 3404 | Index = 2 * i + 1; |
| 3405 | else |
Daniel Dunbar | ce5a0b3 | 2009-10-18 02:09:31 +0000 | [diff] [blame] | 3406 | Index = ExtVectorType::getAccessorIdx(Comp[i]); |
Chris Lattner | 885b495 | 2007-08-02 23:36:59 +0000 | [diff] [blame] | 3407 | |
Nate Begeman | d386215 | 2008-05-13 21:03:02 +0000 | [diff] [blame] | 3408 | Elts.push_back(Index); |
Chris Lattner | 885b495 | 2007-08-02 23:36:59 +0000 | [diff] [blame] | 3409 | } |
Nate Begeman | f322eab | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 3410 | } |
| 3411 | |
Craig Topper | 3793291 | 2013-08-18 10:09:15 +0000 | [diff] [blame] | 3412 | ShuffleVectorExpr::ShuffleVectorExpr(const ASTContext &C, ArrayRef<Expr*> args, |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 3413 | QualType Type, SourceLocation BLoc, |
| 3414 | SourceLocation RP) |
| 3415 | : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary, |
| 3416 | Type->isDependentType(), Type->isDependentType(), |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 3417 | Type->isInstantiationDependentType(), |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 3418 | Type->containsUnexpandedParameterPack()), |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3419 | BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(args.size()) |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 3420 | { |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3421 | SubExprs = new (C) Stmt*[args.size()]; |
| 3422 | for (unsigned i = 0; i != args.size(); i++) { |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 3423 | if (args[i]->isTypeDependent()) |
| 3424 | ExprBits.TypeDependent = true; |
| 3425 | if (args[i]->isValueDependent()) |
| 3426 | ExprBits.ValueDependent = true; |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 3427 | if (args[i]->isInstantiationDependent()) |
| 3428 | ExprBits.InstantiationDependent = true; |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 3429 | if (args[i]->containsUnexpandedParameterPack()) |
| 3430 | ExprBits.ContainsUnexpandedParameterPack = true; |
| 3431 | |
| 3432 | SubExprs[i] = args[i]; |
| 3433 | } |
| 3434 | } |
| 3435 | |
Craig Topper | 3793291 | 2013-08-18 10:09:15 +0000 | [diff] [blame] | 3436 | void ShuffleVectorExpr::setExprs(const ASTContext &C, ArrayRef<Expr *> Exprs) { |
Nate Begeman | 4874592 | 2009-08-12 02:28:50 +0000 | [diff] [blame] | 3437 | if (SubExprs) C.Deallocate(SubExprs); |
| 3438 | |
Dmitri Gribenko | 674eaa2 | 2013-05-10 00:43:44 +0000 | [diff] [blame] | 3439 | this->NumExprs = Exprs.size(); |
Dmitri Gribenko | 48d6daf | 2013-05-10 17:30:13 +0000 | [diff] [blame] | 3440 | SubExprs = new (C) Stmt*[NumExprs]; |
Dmitri Gribenko | 674eaa2 | 2013-05-10 00:43:44 +0000 | [diff] [blame] | 3441 | memcpy(SubExprs, Exprs.data(), sizeof(Expr *) * Exprs.size()); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3442 | } |
Nate Begeman | 4874592 | 2009-08-12 02:28:50 +0000 | [diff] [blame] | 3443 | |
Craig Topper | 3793291 | 2013-08-18 10:09:15 +0000 | [diff] [blame] | 3444 | GenericSelectionExpr::GenericSelectionExpr(const ASTContext &Context, |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 3445 | SourceLocation GenericLoc, Expr *ControllingExpr, |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3446 | ArrayRef<TypeSourceInfo*> AssocTypes, |
| 3447 | ArrayRef<Expr*> AssocExprs, |
| 3448 | SourceLocation DefaultLoc, |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 3449 | SourceLocation RParenLoc, |
| 3450 | bool ContainsUnexpandedParameterPack, |
| 3451 | unsigned ResultIndex) |
| 3452 | : Expr(GenericSelectionExprClass, |
| 3453 | AssocExprs[ResultIndex]->getType(), |
| 3454 | AssocExprs[ResultIndex]->getValueKind(), |
| 3455 | AssocExprs[ResultIndex]->getObjectKind(), |
| 3456 | AssocExprs[ResultIndex]->isTypeDependent(), |
| 3457 | AssocExprs[ResultIndex]->isValueDependent(), |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 3458 | AssocExprs[ResultIndex]->isInstantiationDependent(), |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 3459 | ContainsUnexpandedParameterPack), |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3460 | AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]), |
| 3461 | SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]), |
| 3462 | NumAssocs(AssocExprs.size()), ResultIndex(ResultIndex), |
| 3463 | GenericLoc(GenericLoc), DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) { |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 3464 | SubExprs[CONTROLLING] = ControllingExpr; |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3465 | assert(AssocTypes.size() == AssocExprs.size()); |
| 3466 | std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes); |
| 3467 | std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR); |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 3468 | } |
| 3469 | |
Craig Topper | 3793291 | 2013-08-18 10:09:15 +0000 | [diff] [blame] | 3470 | GenericSelectionExpr::GenericSelectionExpr(const ASTContext &Context, |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 3471 | SourceLocation GenericLoc, Expr *ControllingExpr, |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3472 | ArrayRef<TypeSourceInfo*> AssocTypes, |
| 3473 | ArrayRef<Expr*> AssocExprs, |
| 3474 | SourceLocation DefaultLoc, |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 3475 | SourceLocation RParenLoc, |
| 3476 | bool ContainsUnexpandedParameterPack) |
| 3477 | : Expr(GenericSelectionExprClass, |
| 3478 | Context.DependentTy, |
| 3479 | VK_RValue, |
| 3480 | OK_Ordinary, |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 3481 | /*isTypeDependent=*/true, |
| 3482 | /*isValueDependent=*/true, |
| 3483 | /*isInstantiationDependent=*/true, |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 3484 | ContainsUnexpandedParameterPack), |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3485 | AssocTypes(new (Context) TypeSourceInfo*[AssocTypes.size()]), |
| 3486 | SubExprs(new (Context) Stmt*[END_EXPR+AssocExprs.size()]), |
| 3487 | NumAssocs(AssocExprs.size()), ResultIndex(-1U), GenericLoc(GenericLoc), |
| 3488 | DefaultLoc(DefaultLoc), RParenLoc(RParenLoc) { |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 3489 | SubExprs[CONTROLLING] = ControllingExpr; |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3490 | assert(AssocTypes.size() == AssocExprs.size()); |
| 3491 | std::copy(AssocTypes.begin(), AssocTypes.end(), this->AssocTypes); |
| 3492 | std::copy(AssocExprs.begin(), AssocExprs.end(), SubExprs+END_EXPR); |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 3493 | } |
| 3494 | |
Ted Kremenek | 85e92ec | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 3495 | //===----------------------------------------------------------------------===// |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3496 | // DesignatedInitExpr |
| 3497 | //===----------------------------------------------------------------------===// |
| 3498 | |
Chandler Carruth | 631abd9 | 2011-06-16 06:47:06 +0000 | [diff] [blame] | 3499 | IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() const { |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3500 | assert(Kind == FieldDesignator && "Only valid on a field designator"); |
| 3501 | if (Field.NameOrField & 0x01) |
| 3502 | return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01); |
| 3503 | else |
| 3504 | return getField()->getIdentifier(); |
| 3505 | } |
| 3506 | |
Craig Topper | 3793291 | 2013-08-18 10:09:15 +0000 | [diff] [blame] | 3507 | DesignatedInitExpr::DesignatedInitExpr(const ASTContext &C, QualType Ty, |
Douglas Gregor | 03e8bdc | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 3508 | unsigned NumDesignators, |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 3509 | const Designator *Designators, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3510 | SourceLocation EqualOrColonLoc, |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 3511 | bool GNUSyntax, |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3512 | ArrayRef<Expr*> IndexExprs, |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 3513 | Expr *Init) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3514 | : Expr(DesignatedInitExprClass, Ty, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3515 | Init->getValueKind(), Init->getObjectKind(), |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 3516 | Init->isTypeDependent(), Init->isValueDependent(), |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 3517 | Init->isInstantiationDependent(), |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 3518 | Init->containsUnexpandedParameterPack()), |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3519 | EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax), |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3520 | NumDesignators(NumDesignators), NumSubExprs(IndexExprs.size() + 1) { |
Douglas Gregor | 03e8bdc | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 3521 | this->Designators = new (C) Designator[NumDesignators]; |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 3522 | |
| 3523 | // Record the initializer itself. |
Benjamin Kramer | 5733e35 | 2015-07-18 17:09:36 +0000 | [diff] [blame] | 3524 | child_iterator Child = child_begin(); |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 3525 | *Child++ = Init; |
| 3526 | |
| 3527 | // Copy the designators and their subexpressions, computing |
| 3528 | // value-dependence along the way. |
| 3529 | unsigned IndexIdx = 0; |
| 3530 | for (unsigned I = 0; I != NumDesignators; ++I) { |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 3531 | this->Designators[I] = Designators[I]; |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 3532 | |
| 3533 | if (this->Designators[I].isArrayDesignator()) { |
| 3534 | // Compute type- and value-dependence. |
| 3535 | Expr *Index = IndexExprs[IndexIdx]; |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 3536 | if (Index->isTypeDependent() || Index->isValueDependent()) |
David Majnemer | 4f21768 | 2015-01-09 01:39:09 +0000 | [diff] [blame] | 3537 | ExprBits.TypeDependent = ExprBits.ValueDependent = true; |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 3538 | if (Index->isInstantiationDependent()) |
| 3539 | ExprBits.InstantiationDependent = true; |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 3540 | // Propagate unexpanded parameter packs. |
| 3541 | if (Index->containsUnexpandedParameterPack()) |
| 3542 | ExprBits.ContainsUnexpandedParameterPack = true; |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 3543 | |
| 3544 | // Copy the index expressions into permanent storage. |
| 3545 | *Child++ = IndexExprs[IndexIdx++]; |
| 3546 | } else if (this->Designators[I].isArrayRangeDesignator()) { |
| 3547 | // Compute type- and value-dependence. |
| 3548 | Expr *Start = IndexExprs[IndexIdx]; |
| 3549 | Expr *End = IndexExprs[IndexIdx + 1]; |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 3550 | if (Start->isTypeDependent() || Start->isValueDependent() || |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 3551 | End->isTypeDependent() || End->isValueDependent()) { |
David Majnemer | 4f21768 | 2015-01-09 01:39:09 +0000 | [diff] [blame] | 3552 | ExprBits.TypeDependent = ExprBits.ValueDependent = true; |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 3553 | ExprBits.InstantiationDependent = true; |
| 3554 | } else if (Start->isInstantiationDependent() || |
| 3555 | End->isInstantiationDependent()) { |
| 3556 | ExprBits.InstantiationDependent = true; |
| 3557 | } |
| 3558 | |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 3559 | // Propagate unexpanded parameter packs. |
| 3560 | if (Start->containsUnexpandedParameterPack() || |
| 3561 | End->containsUnexpandedParameterPack()) |
| 3562 | ExprBits.ContainsUnexpandedParameterPack = true; |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 3563 | |
| 3564 | // Copy the start/end expressions into permanent storage. |
| 3565 | *Child++ = IndexExprs[IndexIdx++]; |
| 3566 | *Child++ = IndexExprs[IndexIdx++]; |
| 3567 | } |
| 3568 | } |
| 3569 | |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3570 | assert(IndexIdx == IndexExprs.size() && "Wrong number of index expressions"); |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 3571 | } |
| 3572 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3573 | DesignatedInitExpr * |
Craig Topper | 3793291 | 2013-08-18 10:09:15 +0000 | [diff] [blame] | 3574 | DesignatedInitExpr::Create(const ASTContext &C, Designator *Designators, |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3575 | unsigned NumDesignators, |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3576 | ArrayRef<Expr*> IndexExprs, |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3577 | SourceLocation ColonOrEqualLoc, |
| 3578 | bool UsesColonSyntax, Expr *Init) { |
James Y Knight | e00a67e | 2015-12-31 04:18:25 +0000 | [diff] [blame] | 3579 | void *Mem = C.Allocate(totalSizeToAlloc<Stmt *>(IndexExprs.size() + 1), |
James Y Knight | 53c7616 | 2015-07-17 18:21:37 +0000 | [diff] [blame] | 3580 | llvm::alignOf<DesignatedInitExpr>()); |
Douglas Gregor | 03e8bdc | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 3581 | return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators, |
Douglas Gregor | ca1aeec | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 3582 | ColonOrEqualLoc, UsesColonSyntax, |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3583 | IndexExprs, Init); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3584 | } |
| 3585 | |
Craig Topper | 3793291 | 2013-08-18 10:09:15 +0000 | [diff] [blame] | 3586 | DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(const ASTContext &C, |
Douglas Gregor | 38676d5 | 2009-04-16 00:55:48 +0000 | [diff] [blame] | 3587 | unsigned NumIndexExprs) { |
James Y Knight | e00a67e | 2015-12-31 04:18:25 +0000 | [diff] [blame] | 3588 | void *Mem = C.Allocate(totalSizeToAlloc<Stmt *>(NumIndexExprs + 1), |
| 3589 | llvm::alignOf<DesignatedInitExpr>()); |
Douglas Gregor | 38676d5 | 2009-04-16 00:55:48 +0000 | [diff] [blame] | 3590 | return new (Mem) DesignatedInitExpr(NumIndexExprs + 1); |
| 3591 | } |
| 3592 | |
Craig Topper | 3793291 | 2013-08-18 10:09:15 +0000 | [diff] [blame] | 3593 | void DesignatedInitExpr::setDesignators(const ASTContext &C, |
Douglas Gregor | 03e8bdc | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 3594 | const Designator *Desigs, |
Douglas Gregor | 38676d5 | 2009-04-16 00:55:48 +0000 | [diff] [blame] | 3595 | unsigned NumDesigs) { |
Douglas Gregor | 03e8bdc | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 3596 | Designators = new (C) Designator[NumDesigs]; |
Douglas Gregor | 38676d5 | 2009-04-16 00:55:48 +0000 | [diff] [blame] | 3597 | NumDesignators = NumDesigs; |
| 3598 | for (unsigned I = 0; I != NumDesigs; ++I) |
| 3599 | Designators[I] = Desigs[I]; |
| 3600 | } |
| 3601 | |
Abramo Bagnara | 22f8cd7 | 2011-03-16 15:08:46 +0000 | [diff] [blame] | 3602 | SourceRange DesignatedInitExpr::getDesignatorsSourceRange() const { |
| 3603 | DesignatedInitExpr *DIE = const_cast<DesignatedInitExpr*>(this); |
| 3604 | if (size() == 1) |
| 3605 | return DIE->getDesignator(0)->getSourceRange(); |
Erik Verbruggen | 11a2ecc | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 3606 | return SourceRange(DIE->getDesignator(0)->getLocStart(), |
| 3607 | DIE->getDesignator(size()-1)->getLocEnd()); |
Abramo Bagnara | 22f8cd7 | 2011-03-16 15:08:46 +0000 | [diff] [blame] | 3608 | } |
| 3609 | |
Erik Verbruggen | 11a2ecc | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 3610 | SourceLocation DesignatedInitExpr::getLocStart() const { |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3611 | SourceLocation StartLoc; |
Chris Lattner | 8ba2247 | 2009-02-16 22:33:34 +0000 | [diff] [blame] | 3612 | Designator &First = |
| 3613 | *const_cast<DesignatedInitExpr*>(this)->designators_begin(); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3614 | if (First.isFieldDesignator()) { |
Douglas Gregor | 5c7c9cb | 2009-03-28 00:41:23 +0000 | [diff] [blame] | 3615 | if (GNUSyntax) |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3616 | StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc); |
| 3617 | else |
| 3618 | StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc); |
| 3619 | } else |
Chris Lattner | 8ba2247 | 2009-02-16 22:33:34 +0000 | [diff] [blame] | 3620 | StartLoc = |
| 3621 | SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc); |
Erik Verbruggen | 11a2ecc | 2012-12-25 14:51:39 +0000 | [diff] [blame] | 3622 | return StartLoc; |
| 3623 | } |
| 3624 | |
| 3625 | SourceLocation DesignatedInitExpr::getLocEnd() const { |
| 3626 | return getInit()->getLocEnd(); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3627 | } |
| 3628 | |
Dmitri Gribenko | d06f7ff | 2013-01-26 15:15:52 +0000 | [diff] [blame] | 3629 | Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) const { |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3630 | assert(D.Kind == Designator::ArrayDesignator && "Requires array designator"); |
James Y Knight | e00a67e | 2015-12-31 04:18:25 +0000 | [diff] [blame] | 3631 | return getSubExpr(D.ArrayOrRange.Index + 1); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3632 | } |
| 3633 | |
Dmitri Gribenko | d06f7ff | 2013-01-26 15:15:52 +0000 | [diff] [blame] | 3634 | Expr *DesignatedInitExpr::getArrayRangeStart(const Designator &D) const { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3635 | assert(D.Kind == Designator::ArrayRangeDesignator && |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3636 | "Requires array range designator"); |
James Y Knight | e00a67e | 2015-12-31 04:18:25 +0000 | [diff] [blame] | 3637 | return getSubExpr(D.ArrayOrRange.Index + 1); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3638 | } |
| 3639 | |
Dmitri Gribenko | d06f7ff | 2013-01-26 15:15:52 +0000 | [diff] [blame] | 3640 | Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator &D) const { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3641 | assert(D.Kind == Designator::ArrayRangeDesignator && |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3642 | "Requires array range designator"); |
James Y Knight | e00a67e | 2015-12-31 04:18:25 +0000 | [diff] [blame] | 3643 | return getSubExpr(D.ArrayOrRange.Index + 2); |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3644 | } |
| 3645 | |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 3646 | /// \brief Replaces the designator at index @p Idx with the series |
| 3647 | /// of designators in [First, Last). |
Craig Topper | 3793291 | 2013-08-18 10:09:15 +0000 | [diff] [blame] | 3648 | void DesignatedInitExpr::ExpandDesignator(const ASTContext &C, unsigned Idx, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3649 | const Designator *First, |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 3650 | const Designator *Last) { |
| 3651 | unsigned NumNewDesignators = Last - First; |
| 3652 | if (NumNewDesignators == 0) { |
| 3653 | std::copy_backward(Designators + Idx + 1, |
| 3654 | Designators + NumDesignators, |
| 3655 | Designators + Idx); |
| 3656 | --NumNewDesignators; |
| 3657 | return; |
| 3658 | } else if (NumNewDesignators == 1) { |
| 3659 | Designators[Idx] = *First; |
| 3660 | return; |
| 3661 | } |
| 3662 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3663 | Designator *NewDesignators |
Douglas Gregor | 03e8bdc | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 3664 | = new (C) Designator[NumDesignators - 1 + NumNewDesignators]; |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 3665 | std::copy(Designators, Designators + Idx, NewDesignators); |
| 3666 | std::copy(First, Last, NewDesignators + Idx); |
| 3667 | std::copy(Designators + Idx + 1, Designators + NumDesignators, |
| 3668 | NewDesignators + Idx + NumNewDesignators); |
Douglas Gregor | d5846a1 | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 3669 | Designators = NewDesignators; |
| 3670 | NumDesignators = NumDesignators - 1 + NumNewDesignators; |
| 3671 | } |
| 3672 | |
Yunzhong Gao | cb77930 | 2015-06-10 00:27:52 +0000 | [diff] [blame] | 3673 | DesignatedInitUpdateExpr::DesignatedInitUpdateExpr(const ASTContext &C, |
| 3674 | SourceLocation lBraceLoc, Expr *baseExpr, SourceLocation rBraceLoc) |
| 3675 | : Expr(DesignatedInitUpdateExprClass, baseExpr->getType(), VK_RValue, |
| 3676 | OK_Ordinary, false, false, false, false) { |
| 3677 | BaseAndUpdaterExprs[0] = baseExpr; |
| 3678 | |
| 3679 | InitListExpr *ILE = new (C) InitListExpr(C, lBraceLoc, None, rBraceLoc); |
| 3680 | ILE->setType(baseExpr->getType()); |
| 3681 | BaseAndUpdaterExprs[1] = ILE; |
| 3682 | } |
| 3683 | |
| 3684 | SourceLocation DesignatedInitUpdateExpr::getLocStart() const { |
| 3685 | return getBase()->getLocStart(); |
| 3686 | } |
| 3687 | |
| 3688 | SourceLocation DesignatedInitUpdateExpr::getLocEnd() const { |
| 3689 | return getBase()->getLocEnd(); |
| 3690 | } |
| 3691 | |
Craig Topper | 3793291 | 2013-08-18 10:09:15 +0000 | [diff] [blame] | 3692 | ParenListExpr::ParenListExpr(const ASTContext& C, SourceLocation lparenloc, |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3693 | ArrayRef<Expr*> exprs, |
Sebastian Redl | a935179 | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 3694 | SourceLocation rparenloc) |
| 3695 | : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary, |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 3696 | false, false, false, false), |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3697 | NumExprs(exprs.size()), LParenLoc(lparenloc), RParenLoc(rparenloc) { |
| 3698 | Exprs = new (C) Stmt*[exprs.size()]; |
| 3699 | for (unsigned i = 0; i != exprs.size(); ++i) { |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 3700 | if (exprs[i]->isTypeDependent()) |
| 3701 | ExprBits.TypeDependent = true; |
| 3702 | if (exprs[i]->isValueDependent()) |
| 3703 | ExprBits.ValueDependent = true; |
Douglas Gregor | 678d76c | 2011-07-01 01:22:09 +0000 | [diff] [blame] | 3704 | if (exprs[i]->isInstantiationDependent()) |
| 3705 | ExprBits.InstantiationDependent = true; |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 3706 | if (exprs[i]->containsUnexpandedParameterPack()) |
| 3707 | ExprBits.ContainsUnexpandedParameterPack = true; |
| 3708 | |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3709 | Exprs[i] = exprs[i]; |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 3710 | } |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3711 | } |
| 3712 | |
John McCall | 1bf5846 | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 3713 | const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) { |
| 3714 | if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e)) |
| 3715 | e = ewc->getSubExpr(); |
Douglas Gregor | fe31481 | 2011-06-21 17:03:29 +0000 | [diff] [blame] | 3716 | if (const MaterializeTemporaryExpr *m = dyn_cast<MaterializeTemporaryExpr>(e)) |
| 3717 | e = m->GetTemporaryExpr(); |
John McCall | 1bf5846 | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 3718 | e = cast<CXXConstructExpr>(e)->getArg(0); |
| 3719 | while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e)) |
| 3720 | e = ice->getSubExpr(); |
| 3721 | return cast<OpaqueValueExpr>(e); |
| 3722 | } |
| 3723 | |
Craig Topper | 3793291 | 2013-08-18 10:09:15 +0000 | [diff] [blame] | 3724 | PseudoObjectExpr *PseudoObjectExpr::Create(const ASTContext &Context, |
| 3725 | EmptyShell sh, |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 3726 | unsigned numSemanticExprs) { |
James Y Knight | e00a67e | 2015-12-31 04:18:25 +0000 | [diff] [blame] | 3727 | void *buffer = |
| 3728 | Context.Allocate(totalSizeToAlloc<Expr *>(1 + numSemanticExprs), |
| 3729 | llvm::alignOf<PseudoObjectExpr>()); |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 3730 | return new(buffer) PseudoObjectExpr(sh, numSemanticExprs); |
| 3731 | } |
| 3732 | |
| 3733 | PseudoObjectExpr::PseudoObjectExpr(EmptyShell shell, unsigned numSemanticExprs) |
| 3734 | : Expr(PseudoObjectExprClass, shell) { |
| 3735 | PseudoObjectExprBits.NumSubExprs = numSemanticExprs + 1; |
| 3736 | } |
| 3737 | |
Craig Topper | 3793291 | 2013-08-18 10:09:15 +0000 | [diff] [blame] | 3738 | PseudoObjectExpr *PseudoObjectExpr::Create(const ASTContext &C, Expr *syntax, |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 3739 | ArrayRef<Expr*> semantics, |
| 3740 | unsigned resultIndex) { |
| 3741 | assert(syntax && "no syntactic expression!"); |
| 3742 | assert(semantics.size() && "no semantic expressions!"); |
| 3743 | |
| 3744 | QualType type; |
| 3745 | ExprValueKind VK; |
| 3746 | if (resultIndex == NoResult) { |
| 3747 | type = C.VoidTy; |
| 3748 | VK = VK_RValue; |
| 3749 | } else { |
| 3750 | assert(resultIndex < semantics.size()); |
| 3751 | type = semantics[resultIndex]->getType(); |
| 3752 | VK = semantics[resultIndex]->getValueKind(); |
| 3753 | assert(semantics[resultIndex]->getObjectKind() == OK_Ordinary); |
| 3754 | } |
| 3755 | |
James Y Knight | e00a67e | 2015-12-31 04:18:25 +0000 | [diff] [blame] | 3756 | void *buffer = C.Allocate(totalSizeToAlloc<Expr *>(semantics.size() + 1), |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 3757 | llvm::alignOf<PseudoObjectExpr>()); |
| 3758 | return new(buffer) PseudoObjectExpr(type, VK, syntax, semantics, |
| 3759 | resultIndex); |
| 3760 | } |
| 3761 | |
| 3762 | PseudoObjectExpr::PseudoObjectExpr(QualType type, ExprValueKind VK, |
| 3763 | Expr *syntax, ArrayRef<Expr*> semantics, |
| 3764 | unsigned resultIndex) |
| 3765 | : Expr(PseudoObjectExprClass, type, VK, OK_Ordinary, |
| 3766 | /*filled in at end of ctor*/ false, false, false, false) { |
| 3767 | PseudoObjectExprBits.NumSubExprs = semantics.size() + 1; |
| 3768 | PseudoObjectExprBits.ResultIndex = resultIndex + 1; |
| 3769 | |
| 3770 | for (unsigned i = 0, e = semantics.size() + 1; i != e; ++i) { |
| 3771 | Expr *E = (i == 0 ? syntax : semantics[i-1]); |
| 3772 | getSubExprsBuffer()[i] = E; |
| 3773 | |
| 3774 | if (E->isTypeDependent()) |
| 3775 | ExprBits.TypeDependent = true; |
| 3776 | if (E->isValueDependent()) |
| 3777 | ExprBits.ValueDependent = true; |
| 3778 | if (E->isInstantiationDependent()) |
| 3779 | ExprBits.InstantiationDependent = true; |
| 3780 | if (E->containsUnexpandedParameterPack()) |
| 3781 | ExprBits.ContainsUnexpandedParameterPack = true; |
| 3782 | |
| 3783 | if (isa<OpaqueValueExpr>(E)) |
Craig Topper | 36250ad | 2014-05-12 05:36:57 +0000 | [diff] [blame] | 3784 | assert(cast<OpaqueValueExpr>(E)->getSourceExpr() != nullptr && |
John McCall | fe96e0b | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 3785 | "opaque-value semantic expressions for pseudo-object " |
| 3786 | "operations must have sources"); |
| 3787 | } |
| 3788 | } |
| 3789 | |
Douglas Gregor | e4a0bb7 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 3790 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 85e92ec | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 3791 | // Child Iterators for iterating over subexpressions/substatements |
| 3792 | //===----------------------------------------------------------------------===// |
| 3793 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3794 | // UnaryExprOrTypeTraitExpr |
| 3795 | Stmt::child_range UnaryExprOrTypeTraitExpr::children() { |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 3796 | // If this is of a type and the type is a VLA type (and not a typedef), the |
| 3797 | // size expression of the VLA needs to be treated as an executable expression. |
| 3798 | // Why isn't this weirdness documented better in StmtIterator? |
| 3799 | if (isArgumentType()) { |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 3800 | if (const VariableArrayType* T = dyn_cast<VariableArrayType>( |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 3801 | getArgumentType().getTypePtr())) |
John McCall | bd06678 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 3802 | return child_range(child_iterator(T), child_iterator()); |
Benjamin Kramer | 5733e35 | 2015-07-18 17:09:36 +0000 | [diff] [blame] | 3803 | return child_range(child_iterator(), child_iterator()); |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 3804 | } |
John McCall | bd06678 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 3805 | return child_range(&Argument.Ex, &Argument.Ex + 1); |
Ted Kremenek | 04746ce | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 3806 | } |
Fariborz Jahanian | a32aaef | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 3807 | |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3808 | AtomicExpr::AtomicExpr(SourceLocation BLoc, ArrayRef<Expr*> args, |
Eli Friedman | 8d3e43f | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 3809 | QualType t, AtomicOp op, SourceLocation RP) |
| 3810 | : Expr(AtomicExprClass, t, VK_RValue, OK_Ordinary, |
| 3811 | false, false, false, false), |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3812 | NumSubExprs(args.size()), BuiltinLoc(BLoc), RParenLoc(RP), Op(op) |
Eli Friedman | 8d3e43f | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 3813 | { |
Benjamin Kramer | c215e76 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3814 | assert(args.size() == getNumSubExprs(op) && "wrong number of subexpressions"); |
| 3815 | for (unsigned i = 0; i != args.size(); i++) { |
Eli Friedman | 8d3e43f | 2011-10-14 22:48:56 +0000 | [diff] [blame] | 3816 | if (args[i]->isTypeDependent()) |
| 3817 | ExprBits.TypeDependent = true; |
| 3818 | if (args[i]->isValueDependent()) |
| 3819 | ExprBits.ValueDependent = true; |
| 3820 | if (args[i]->isInstantiationDependent()) |
| 3821 | ExprBits.InstantiationDependent = true; |
| 3822 | if (args[i]->containsUnexpandedParameterPack()) |
| 3823 | ExprBits.ContainsUnexpandedParameterPack = true; |
| 3824 | |
| 3825 | SubExprs[i] = args[i]; |
| 3826 | } |
| 3827 | } |
Richard Smith | aa22a8c | 2012-04-10 22:49:28 +0000 | [diff] [blame] | 3828 | |
| 3829 | unsigned AtomicExpr::getNumSubExprs(AtomicOp Op) { |
| 3830 | switch (Op) { |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 3831 | case AO__c11_atomic_init: |
| 3832 | case AO__c11_atomic_load: |
| 3833 | case AO__atomic_load_n: |
Richard Smith | aa22a8c | 2012-04-10 22:49:28 +0000 | [diff] [blame] | 3834 | return 2; |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 3835 | |
| 3836 | case AO__c11_atomic_store: |
| 3837 | case AO__c11_atomic_exchange: |
| 3838 | case AO__atomic_load: |
| 3839 | case AO__atomic_store: |
| 3840 | case AO__atomic_store_n: |
| 3841 | case AO__atomic_exchange_n: |
| 3842 | case AO__c11_atomic_fetch_add: |
| 3843 | case AO__c11_atomic_fetch_sub: |
| 3844 | case AO__c11_atomic_fetch_and: |
| 3845 | case AO__c11_atomic_fetch_or: |
| 3846 | case AO__c11_atomic_fetch_xor: |
| 3847 | case AO__atomic_fetch_add: |
| 3848 | case AO__atomic_fetch_sub: |
| 3849 | case AO__atomic_fetch_and: |
| 3850 | case AO__atomic_fetch_or: |
| 3851 | case AO__atomic_fetch_xor: |
Richard Smith | d65cee9 | 2012-04-13 06:31:38 +0000 | [diff] [blame] | 3852 | case AO__atomic_fetch_nand: |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 3853 | case AO__atomic_add_fetch: |
| 3854 | case AO__atomic_sub_fetch: |
| 3855 | case AO__atomic_and_fetch: |
| 3856 | case AO__atomic_or_fetch: |
| 3857 | case AO__atomic_xor_fetch: |
Richard Smith | d65cee9 | 2012-04-13 06:31:38 +0000 | [diff] [blame] | 3858 | case AO__atomic_nand_fetch: |
Richard Smith | aa22a8c | 2012-04-10 22:49:28 +0000 | [diff] [blame] | 3859 | return 3; |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 3860 | |
| 3861 | case AO__atomic_exchange: |
| 3862 | return 4; |
| 3863 | |
| 3864 | case AO__c11_atomic_compare_exchange_strong: |
| 3865 | case AO__c11_atomic_compare_exchange_weak: |
Richard Smith | aa22a8c | 2012-04-10 22:49:28 +0000 | [diff] [blame] | 3866 | return 5; |
Richard Smith | feea883 | 2012-04-12 05:08:17 +0000 | [diff] [blame] | 3867 | |
| 3868 | case AO__atomic_compare_exchange: |
| 3869 | case AO__atomic_compare_exchange_n: |
| 3870 | return 6; |
Richard Smith | aa22a8c | 2012-04-10 22:49:28 +0000 | [diff] [blame] | 3871 | } |
| 3872 | llvm_unreachable("unknown atomic op"); |
| 3873 | } |
Alexey Bataev | a176421 | 2015-09-30 09:22:36 +0000 | [diff] [blame] | 3874 | |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 3875 | QualType OMPArraySectionExpr::getBaseOriginalType(const Expr *Base) { |
Alexey Bataev | a176421 | 2015-09-30 09:22:36 +0000 | [diff] [blame] | 3876 | unsigned ArraySectionCount = 0; |
| 3877 | while (auto *OASE = dyn_cast<OMPArraySectionExpr>(Base->IgnoreParens())) { |
| 3878 | Base = OASE->getBase(); |
| 3879 | ++ArraySectionCount; |
| 3880 | } |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 3881 | while (auto *ASE = |
| 3882 | dyn_cast<ArraySubscriptExpr>(Base->IgnoreParenImpCasts())) { |
Alexey Bataev | f24e7b1 | 2015-10-08 09:10:53 +0000 | [diff] [blame] | 3883 | Base = ASE->getBase(); |
| 3884 | ++ArraySectionCount; |
| 3885 | } |
Alexey Bataev | 31300ed | 2016-02-04 11:27:03 +0000 | [diff] [blame] | 3886 | Base = Base->IgnoreParenImpCasts(); |
Alexey Bataev | a176421 | 2015-09-30 09:22:36 +0000 | [diff] [blame] | 3887 | auto OriginalTy = Base->getType(); |
| 3888 | if (auto *DRE = dyn_cast<DeclRefExpr>(Base)) |
| 3889 | if (auto *PVD = dyn_cast<ParmVarDecl>(DRE->getDecl())) |
| 3890 | OriginalTy = PVD->getOriginalType().getNonReferenceType(); |
| 3891 | |
| 3892 | for (unsigned Cnt = 0; Cnt < ArraySectionCount; ++Cnt) { |
| 3893 | if (OriginalTy->isAnyPointerType()) |
| 3894 | OriginalTy = OriginalTy->getPointeeType(); |
| 3895 | else { |
| 3896 | assert (OriginalTy->isArrayType()); |
| 3897 | OriginalTy = OriginalTy->castAsArrayTypeUnsafe()->getElementType(); |
| 3898 | } |
| 3899 | } |
| 3900 | return OriginalTy; |
| 3901 | } |