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