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