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