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