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