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