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