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