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