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 | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 1597 | for (Expr::child_range I = E->children(); I && R != Expr::CT_Can; ++I) { |
Sebastian Redl | 369e51f | 2010-09-10 20:55:33 +0000 | [diff] [blame] | 1598 | R = MergeCanThrow(R, cast<Expr>(*I)->CanThrow(C)); |
| 1599 | } |
| 1600 | return R; |
| 1601 | } |
| 1602 | |
| 1603 | static Expr::CanThrowResult CanCalleeThrow(const Decl *D, |
| 1604 | bool NullThrows = true) { |
| 1605 | if (!D) |
| 1606 | return NullThrows ? Expr::CT_Can : Expr::CT_Cannot; |
| 1607 | |
| 1608 | // See if we can get a function type from the decl somehow. |
| 1609 | const ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 1610 | if (!VD) // If we have no clue what we're calling, assume the worst. |
| 1611 | return Expr::CT_Can; |
| 1612 | |
Sebastian Redl | 5221d8f | 2010-09-10 22:34:40 +0000 | [diff] [blame] | 1613 | // As an extension, we assume that __attribute__((nothrow)) functions don't |
| 1614 | // throw. |
| 1615 | if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>()) |
| 1616 | return Expr::CT_Cannot; |
| 1617 | |
Sebastian Redl | 369e51f | 2010-09-10 20:55:33 +0000 | [diff] [blame] | 1618 | QualType T = VD->getType(); |
| 1619 | const FunctionProtoType *FT; |
| 1620 | if ((FT = T->getAs<FunctionProtoType>())) { |
| 1621 | } else if (const PointerType *PT = T->getAs<PointerType>()) |
| 1622 | FT = PT->getPointeeType()->getAs<FunctionProtoType>(); |
| 1623 | else if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
| 1624 | FT = RT->getPointeeType()->getAs<FunctionProtoType>(); |
| 1625 | else if (const MemberPointerType *MT = T->getAs<MemberPointerType>()) |
| 1626 | FT = MT->getPointeeType()->getAs<FunctionProtoType>(); |
| 1627 | else if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) |
| 1628 | FT = BT->getPointeeType()->getAs<FunctionProtoType>(); |
| 1629 | |
| 1630 | if (!FT) |
| 1631 | return Expr::CT_Can; |
| 1632 | |
| 1633 | return FT->hasEmptyExceptionSpec() ? Expr::CT_Cannot : Expr::CT_Can; |
| 1634 | } |
| 1635 | |
| 1636 | static Expr::CanThrowResult CanDynamicCastThrow(const CXXDynamicCastExpr *DC) { |
| 1637 | if (DC->isTypeDependent()) |
| 1638 | return Expr::CT_Dependent; |
| 1639 | |
Sebastian Redl | 295995c | 2010-09-10 20:55:47 +0000 | [diff] [blame] | 1640 | if (!DC->getTypeAsWritten()->isReferenceType()) |
| 1641 | return Expr::CT_Cannot; |
| 1642 | |
Sebastian Redl | 369e51f | 2010-09-10 20:55:33 +0000 | [diff] [blame] | 1643 | return DC->getCastKind() == clang::CK_Dynamic? Expr::CT_Can : Expr::CT_Cannot; |
| 1644 | } |
| 1645 | |
| 1646 | static Expr::CanThrowResult CanTypeidThrow(ASTContext &C, |
| 1647 | const CXXTypeidExpr *DC) { |
| 1648 | if (DC->isTypeOperand()) |
| 1649 | return Expr::CT_Cannot; |
| 1650 | |
| 1651 | Expr *Op = DC->getExprOperand(); |
| 1652 | if (Op->isTypeDependent()) |
| 1653 | return Expr::CT_Dependent; |
| 1654 | |
| 1655 | const RecordType *RT = Op->getType()->getAs<RecordType>(); |
| 1656 | if (!RT) |
| 1657 | return Expr::CT_Cannot; |
| 1658 | |
| 1659 | if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic()) |
| 1660 | return Expr::CT_Cannot; |
| 1661 | |
| 1662 | if (Op->Classify(C).isPRValue()) |
| 1663 | return Expr::CT_Cannot; |
| 1664 | |
| 1665 | return Expr::CT_Can; |
| 1666 | } |
| 1667 | |
| 1668 | Expr::CanThrowResult Expr::CanThrow(ASTContext &C) const { |
| 1669 | // C++ [expr.unary.noexcept]p3: |
| 1670 | // [Can throw] if in a potentially-evaluated context the expression would |
| 1671 | // contain: |
| 1672 | switch (getStmtClass()) { |
| 1673 | case CXXThrowExprClass: |
| 1674 | // - a potentially evaluated throw-expression |
| 1675 | return CT_Can; |
| 1676 | |
| 1677 | case CXXDynamicCastExprClass: { |
| 1678 | // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v), |
| 1679 | // where T is a reference type, that requires a run-time check |
| 1680 | CanThrowResult CT = CanDynamicCastThrow(cast<CXXDynamicCastExpr>(this)); |
| 1681 | if (CT == CT_Can) |
| 1682 | return CT; |
| 1683 | return MergeCanThrow(CT, CanSubExprsThrow(C, this)); |
| 1684 | } |
| 1685 | |
| 1686 | case CXXTypeidExprClass: |
| 1687 | // - a potentially evaluated typeid expression applied to a glvalue |
| 1688 | // expression whose type is a polymorphic class type |
| 1689 | return CanTypeidThrow(C, cast<CXXTypeidExpr>(this)); |
| 1690 | |
| 1691 | // - a potentially evaluated call to a function, member function, function |
| 1692 | // pointer, or member function pointer that does not have a non-throwing |
| 1693 | // exception-specification |
| 1694 | case CallExprClass: |
| 1695 | case CXXOperatorCallExprClass: |
| 1696 | case CXXMemberCallExprClass: { |
| 1697 | CanThrowResult CT = CanCalleeThrow(cast<CallExpr>(this)->getCalleeDecl()); |
| 1698 | if (CT == CT_Can) |
| 1699 | return CT; |
| 1700 | return MergeCanThrow(CT, CanSubExprsThrow(C, this)); |
| 1701 | } |
| 1702 | |
Sebastian Redl | 295995c | 2010-09-10 20:55:47 +0000 | [diff] [blame] | 1703 | case CXXConstructExprClass: |
| 1704 | case CXXTemporaryObjectExprClass: { |
Sebastian Redl | 369e51f | 2010-09-10 20:55:33 +0000 | [diff] [blame] | 1705 | CanThrowResult CT = CanCalleeThrow( |
| 1706 | cast<CXXConstructExpr>(this)->getConstructor()); |
| 1707 | if (CT == CT_Can) |
| 1708 | return CT; |
| 1709 | return MergeCanThrow(CT, CanSubExprsThrow(C, this)); |
| 1710 | } |
| 1711 | |
| 1712 | case CXXNewExprClass: { |
| 1713 | CanThrowResult CT = MergeCanThrow( |
| 1714 | CanCalleeThrow(cast<CXXNewExpr>(this)->getOperatorNew()), |
| 1715 | CanCalleeThrow(cast<CXXNewExpr>(this)->getConstructor(), |
| 1716 | /*NullThrows*/false)); |
| 1717 | if (CT == CT_Can) |
| 1718 | return CT; |
| 1719 | return MergeCanThrow(CT, CanSubExprsThrow(C, this)); |
| 1720 | } |
| 1721 | |
| 1722 | case CXXDeleteExprClass: { |
Sebastian Redl | 369e51f | 2010-09-10 20:55:33 +0000 | [diff] [blame] | 1723 | CanThrowResult CT = CanCalleeThrow( |
| 1724 | cast<CXXDeleteExpr>(this)->getOperatorDelete()); |
| 1725 | if (CT == CT_Can) |
| 1726 | return CT; |
Sebastian Redl | 0b34cf7 | 2010-09-10 23:27:10 +0000 | [diff] [blame] | 1727 | const Expr *Arg = cast<CXXDeleteExpr>(this)->getArgument(); |
| 1728 | // Unwrap exactly one implicit cast, which converts all pointers to void*. |
| 1729 | if (const ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
| 1730 | Arg = Cast->getSubExpr(); |
| 1731 | if (const PointerType *PT = Arg->getType()->getAs<PointerType>()) { |
| 1732 | if (const RecordType *RT = PT->getPointeeType()->getAs<RecordType>()) { |
| 1733 | CanThrowResult CT2 = CanCalleeThrow( |
| 1734 | cast<CXXRecordDecl>(RT->getDecl())->getDestructor()); |
| 1735 | if (CT2 == CT_Can) |
| 1736 | return CT2; |
| 1737 | CT = MergeCanThrow(CT, CT2); |
| 1738 | } |
| 1739 | } |
| 1740 | return MergeCanThrow(CT, CanSubExprsThrow(C, this)); |
| 1741 | } |
| 1742 | |
| 1743 | case CXXBindTemporaryExprClass: { |
| 1744 | // The bound temporary has to be destroyed again, which might throw. |
| 1745 | CanThrowResult CT = CanCalleeThrow( |
| 1746 | cast<CXXBindTemporaryExpr>(this)->getTemporary()->getDestructor()); |
| 1747 | if (CT == CT_Can) |
| 1748 | return CT; |
Sebastian Redl | 369e51f | 2010-09-10 20:55:33 +0000 | [diff] [blame] | 1749 | return MergeCanThrow(CT, CanSubExprsThrow(C, this)); |
| 1750 | } |
| 1751 | |
| 1752 | // ObjC message sends are like function calls, but never have exception |
| 1753 | // specs. |
| 1754 | case ObjCMessageExprClass: |
| 1755 | case ObjCPropertyRefExprClass: |
Sebastian Redl | 369e51f | 2010-09-10 20:55:33 +0000 | [diff] [blame] | 1756 | return CT_Can; |
| 1757 | |
| 1758 | // Many other things have subexpressions, so we have to test those. |
| 1759 | // Some are simple: |
| 1760 | case ParenExprClass: |
| 1761 | case MemberExprClass: |
| 1762 | case CXXReinterpretCastExprClass: |
| 1763 | case CXXConstCastExprClass: |
| 1764 | case ConditionalOperatorClass: |
| 1765 | case CompoundLiteralExprClass: |
| 1766 | case ExtVectorElementExprClass: |
| 1767 | case InitListExprClass: |
| 1768 | case DesignatedInitExprClass: |
| 1769 | case ParenListExprClass: |
| 1770 | case VAArgExprClass: |
| 1771 | case CXXDefaultArgExprClass: |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame] | 1772 | case ExprWithCleanupsClass: |
Sebastian Redl | 369e51f | 2010-09-10 20:55:33 +0000 | [diff] [blame] | 1773 | case ObjCIvarRefExprClass: |
| 1774 | case ObjCIsaExprClass: |
| 1775 | case ShuffleVectorExprClass: |
| 1776 | return CanSubExprsThrow(C, this); |
| 1777 | |
| 1778 | // Some might be dependent for other reasons. |
| 1779 | case UnaryOperatorClass: |
| 1780 | case ArraySubscriptExprClass: |
| 1781 | case ImplicitCastExprClass: |
| 1782 | case CStyleCastExprClass: |
| 1783 | case CXXStaticCastExprClass: |
| 1784 | case CXXFunctionalCastExprClass: |
| 1785 | case BinaryOperatorClass: |
| 1786 | case CompoundAssignOperatorClass: { |
| 1787 | CanThrowResult CT = isTypeDependent() ? CT_Dependent : CT_Cannot; |
| 1788 | return MergeCanThrow(CT, CanSubExprsThrow(C, this)); |
| 1789 | } |
| 1790 | |
| 1791 | // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms. |
| 1792 | case StmtExprClass: |
| 1793 | return CT_Can; |
| 1794 | |
| 1795 | case ChooseExprClass: |
| 1796 | if (isTypeDependent() || isValueDependent()) |
| 1797 | return CT_Dependent; |
| 1798 | return cast<ChooseExpr>(this)->getChosenSubExpr(C)->CanThrow(C); |
| 1799 | |
| 1800 | // Some expressions are always dependent. |
| 1801 | case DependentScopeDeclRefExprClass: |
| 1802 | case CXXUnresolvedConstructExprClass: |
| 1803 | case CXXDependentScopeMemberExprClass: |
| 1804 | return CT_Dependent; |
| 1805 | |
| 1806 | default: |
| 1807 | // All other expressions don't have subexpressions, or else they are |
| 1808 | // unevaluated. |
| 1809 | return CT_Cannot; |
| 1810 | } |
| 1811 | } |
| 1812 | |
Ted Kremenek | 4e99a5f | 2008-01-17 16:57:34 +0000 | [diff] [blame] | 1813 | Expr* Expr::IgnoreParens() { |
| 1814 | Expr* E = this; |
Abramo Bagnara | b9eb35c | 2010-10-15 07:51:18 +0000 | [diff] [blame] | 1815 | while (true) { |
| 1816 | if (ParenExpr* P = dyn_cast<ParenExpr>(E)) { |
| 1817 | E = P->getSubExpr(); |
| 1818 | continue; |
| 1819 | } |
| 1820 | if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) { |
| 1821 | if (P->getOpcode() == UO_Extension) { |
| 1822 | E = P->getSubExpr(); |
| 1823 | continue; |
| 1824 | } |
| 1825 | } |
| 1826 | return E; |
| 1827 | } |
Ted Kremenek | 4e99a5f | 2008-01-17 16:57:34 +0000 | [diff] [blame] | 1828 | } |
| 1829 | |
Chris Lattner | 56f3494 | 2008-02-13 01:02:39 +0000 | [diff] [blame] | 1830 | /// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr |
| 1831 | /// or CastExprs or ImplicitCastExprs, returning their operand. |
| 1832 | Expr *Expr::IgnoreParenCasts() { |
| 1833 | Expr *E = this; |
| 1834 | while (true) { |
Abramo Bagnara | b9eb35c | 2010-10-15 07:51:18 +0000 | [diff] [blame] | 1835 | if (ParenExpr* P = dyn_cast<ParenExpr>(E)) { |
Chris Lattner | 56f3494 | 2008-02-13 01:02:39 +0000 | [diff] [blame] | 1836 | E = P->getSubExpr(); |
Abramo Bagnara | b9eb35c | 2010-10-15 07:51:18 +0000 | [diff] [blame] | 1837 | continue; |
| 1838 | } |
| 1839 | if (CastExpr *P = dyn_cast<CastExpr>(E)) { |
Chris Lattner | 56f3494 | 2008-02-13 01:02:39 +0000 | [diff] [blame] | 1840 | E = P->getSubExpr(); |
Abramo Bagnara | b9eb35c | 2010-10-15 07:51:18 +0000 | [diff] [blame] | 1841 | continue; |
| 1842 | } |
| 1843 | if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) { |
| 1844 | if (P->getOpcode() == UO_Extension) { |
| 1845 | E = P->getSubExpr(); |
| 1846 | continue; |
| 1847 | } |
| 1848 | } |
| 1849 | return E; |
Chris Lattner | 56f3494 | 2008-02-13 01:02:39 +0000 | [diff] [blame] | 1850 | } |
| 1851 | } |
| 1852 | |
John McCall | 9c5d70c | 2010-12-04 08:24:19 +0000 | [diff] [blame] | 1853 | /// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue |
| 1854 | /// casts. This is intended purely as a temporary workaround for code |
| 1855 | /// that hasn't yet been rewritten to do the right thing about those |
| 1856 | /// casts, and may disappear along with the last internal use. |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 1857 | Expr *Expr::IgnoreParenLValueCasts() { |
| 1858 | Expr *E = this; |
John McCall | 9c5d70c | 2010-12-04 08:24:19 +0000 | [diff] [blame] | 1859 | while (true) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 1860 | if (ParenExpr *P = dyn_cast<ParenExpr>(E)) { |
| 1861 | E = P->getSubExpr(); |
| 1862 | continue; |
John McCall | 9c5d70c | 2010-12-04 08:24:19 +0000 | [diff] [blame] | 1863 | } else if (CastExpr *P = dyn_cast<CastExpr>(E)) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 1864 | if (P->getCastKind() == CK_LValueToRValue) { |
| 1865 | E = P->getSubExpr(); |
| 1866 | continue; |
| 1867 | } |
John McCall | 9c5d70c | 2010-12-04 08:24:19 +0000 | [diff] [blame] | 1868 | } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) { |
| 1869 | if (P->getOpcode() == UO_Extension) { |
| 1870 | E = P->getSubExpr(); |
| 1871 | continue; |
| 1872 | } |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 1873 | } |
| 1874 | break; |
| 1875 | } |
| 1876 | return E; |
| 1877 | } |
| 1878 | |
John McCall | 2fc46bf | 2010-05-05 22:59:52 +0000 | [diff] [blame] | 1879 | Expr *Expr::IgnoreParenImpCasts() { |
| 1880 | Expr *E = this; |
| 1881 | while (true) { |
Abramo Bagnara | b9eb35c | 2010-10-15 07:51:18 +0000 | [diff] [blame] | 1882 | if (ParenExpr *P = dyn_cast<ParenExpr>(E)) { |
John McCall | 2fc46bf | 2010-05-05 22:59:52 +0000 | [diff] [blame] | 1883 | E = P->getSubExpr(); |
Abramo Bagnara | b9eb35c | 2010-10-15 07:51:18 +0000 | [diff] [blame] | 1884 | continue; |
| 1885 | } |
| 1886 | if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | 2fc46bf | 2010-05-05 22:59:52 +0000 | [diff] [blame] | 1887 | E = P->getSubExpr(); |
Abramo Bagnara | b9eb35c | 2010-10-15 07:51:18 +0000 | [diff] [blame] | 1888 | continue; |
| 1889 | } |
| 1890 | if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) { |
| 1891 | if (P->getOpcode() == UO_Extension) { |
| 1892 | E = P->getSubExpr(); |
| 1893 | continue; |
| 1894 | } |
| 1895 | } |
| 1896 | return E; |
John McCall | 2fc46bf | 2010-05-05 22:59:52 +0000 | [diff] [blame] | 1897 | } |
| 1898 | } |
| 1899 | |
Chris Lattner | ecdd841 | 2009-03-13 17:28:01 +0000 | [diff] [blame] | 1900 | /// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the |
| 1901 | /// value (including ptr->int casts of the same size). Strip off any |
| 1902 | /// ParenExpr or CastExprs, returning their operand. |
| 1903 | Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) { |
| 1904 | Expr *E = this; |
| 1905 | while (true) { |
| 1906 | if (ParenExpr *P = dyn_cast<ParenExpr>(E)) { |
| 1907 | E = P->getSubExpr(); |
| 1908 | continue; |
| 1909 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1910 | |
Chris Lattner | ecdd841 | 2009-03-13 17:28:01 +0000 | [diff] [blame] | 1911 | if (CastExpr *P = dyn_cast<CastExpr>(E)) { |
| 1912 | // 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] | 1913 | // 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] | 1914 | Expr *SE = P->getSubExpr(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1915 | |
Chris Lattner | ecdd841 | 2009-03-13 17:28:01 +0000 | [diff] [blame] | 1916 | if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) { |
| 1917 | E = SE; |
| 1918 | continue; |
| 1919 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1920 | |
Abramo Bagnara | b9eb35c | 2010-10-15 07:51:18 +0000 | [diff] [blame] | 1921 | if ((E->getType()->isPointerType() || |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 1922 | E->getType()->isIntegralType(Ctx)) && |
Abramo Bagnara | b9eb35c | 2010-10-15 07:51:18 +0000 | [diff] [blame] | 1923 | (SE->getType()->isPointerType() || |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 1924 | SE->getType()->isIntegralType(Ctx)) && |
Chris Lattner | ecdd841 | 2009-03-13 17:28:01 +0000 | [diff] [blame] | 1925 | Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) { |
| 1926 | E = SE; |
| 1927 | continue; |
| 1928 | } |
| 1929 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1930 | |
Abramo Bagnara | b9eb35c | 2010-10-15 07:51:18 +0000 | [diff] [blame] | 1931 | if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) { |
| 1932 | if (P->getOpcode() == UO_Extension) { |
| 1933 | E = P->getSubExpr(); |
| 1934 | continue; |
| 1935 | } |
| 1936 | } |
| 1937 | |
Chris Lattner | ecdd841 | 2009-03-13 17:28:01 +0000 | [diff] [blame] | 1938 | return E; |
| 1939 | } |
| 1940 | } |
| 1941 | |
Douglas Gregor | 6eef519 | 2009-12-14 19:27:10 +0000 | [diff] [blame] | 1942 | bool Expr::isDefaultArgument() const { |
| 1943 | const Expr *E = this; |
| 1944 | while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) |
| 1945 | E = ICE->getSubExprAsWritten(); |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 1946 | |
Douglas Gregor | 6eef519 | 2009-12-14 19:27:10 +0000 | [diff] [blame] | 1947 | return isa<CXXDefaultArgExpr>(E); |
| 1948 | } |
Chris Lattner | ecdd841 | 2009-03-13 17:28:01 +0000 | [diff] [blame] | 1949 | |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 1950 | /// \brief Skip over any no-op casts and any temporary-binding |
| 1951 | /// expressions. |
Anders Carlsson | f8b3015 | 2010-11-28 16:40:49 +0000 | [diff] [blame] | 1952 | static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) { |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 1953 | while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1954 | if (ICE->getCastKind() == CK_NoOp) |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 1955 | E = ICE->getSubExpr(); |
| 1956 | else |
| 1957 | break; |
| 1958 | } |
| 1959 | |
| 1960 | while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E)) |
| 1961 | E = BE->getSubExpr(); |
| 1962 | |
| 1963 | while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1964 | if (ICE->getCastKind() == CK_NoOp) |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 1965 | E = ICE->getSubExpr(); |
| 1966 | else |
| 1967 | break; |
| 1968 | } |
Anders Carlsson | f8b3015 | 2010-11-28 16:40:49 +0000 | [diff] [blame] | 1969 | |
| 1970 | return E->IgnoreParens(); |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 1971 | } |
| 1972 | |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 1973 | /// isTemporaryObject - Determines if this expression produces a |
| 1974 | /// temporary of the given class type. |
| 1975 | bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const { |
| 1976 | if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy))) |
| 1977 | return false; |
| 1978 | |
Anders Carlsson | f8b3015 | 2010-11-28 16:40:49 +0000 | [diff] [blame] | 1979 | const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this); |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 1980 | |
John McCall | 58277b5 | 2010-09-15 20:59:13 +0000 | [diff] [blame] | 1981 | // Temporaries are by definition pr-values of class type. |
Fariborz Jahanian | db148be | 2010-09-27 17:30:38 +0000 | [diff] [blame] | 1982 | if (!E->Classify(C).isPRValue()) { |
| 1983 | // 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] | 1984 | if (!isa<ObjCPropertyRefExpr>(E)) |
Fariborz Jahanian | db148be | 2010-09-27 17:30:38 +0000 | [diff] [blame] | 1985 | return false; |
| 1986 | } |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 1987 | |
John McCall | 19e60ad | 2010-09-16 06:57:56 +0000 | [diff] [blame] | 1988 | // Black-list a few cases which yield pr-values of class type that don't |
| 1989 | // refer to temporaries of that type: |
| 1990 | |
| 1991 | // - implicit derived-to-base conversions |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 1992 | if (isa<ImplicitCastExpr>(E)) { |
| 1993 | switch (cast<ImplicitCastExpr>(E)->getCastKind()) { |
| 1994 | case CK_DerivedToBase: |
| 1995 | case CK_UncheckedDerivedToBase: |
| 1996 | return false; |
| 1997 | default: |
| 1998 | break; |
| 1999 | } |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 2000 | } |
| 2001 | |
John McCall | 19e60ad | 2010-09-16 06:57:56 +0000 | [diff] [blame] | 2002 | // - member expressions (all) |
| 2003 | if (isa<MemberExpr>(E)) |
| 2004 | return false; |
| 2005 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame^] | 2006 | // - opaque values (all) |
| 2007 | if (isa<OpaqueValueExpr>(E)) |
| 2008 | return false; |
| 2009 | |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 2010 | return true; |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 2011 | } |
| 2012 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 2013 | /// hasAnyTypeDependentArguments - Determines if any of the expressions |
| 2014 | /// in Exprs is type-dependent. |
| 2015 | bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) { |
| 2016 | for (unsigned I = 0; I < NumExprs; ++I) |
| 2017 | if (Exprs[I]->isTypeDependent()) |
| 2018 | return true; |
| 2019 | |
| 2020 | return false; |
| 2021 | } |
| 2022 | |
| 2023 | /// hasAnyValueDependentArguments - Determines if any of the expressions |
| 2024 | /// in Exprs is value-dependent. |
| 2025 | bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) { |
| 2026 | for (unsigned I = 0; I < NumExprs; ++I) |
| 2027 | if (Exprs[I]->isValueDependent()) |
| 2028 | return true; |
| 2029 | |
| 2030 | return false; |
| 2031 | } |
| 2032 | |
John McCall | 4204f07 | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 2033 | bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const { |
Eli Friedman | c39dc9a | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 2034 | // This function is attempting whether an expression is an initializer |
| 2035 | // which can be evaluated at compile-time. isEvaluatable handles most |
| 2036 | // of the cases, but it can't deal with some initializer-specific |
| 2037 | // expressions, and it can't deal with aggregates; we deal with those here, |
| 2038 | // and fall back to isEvaluatable for the other cases. |
| 2039 | |
John McCall | 4204f07 | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 2040 | // If we ever capture reference-binding directly in the AST, we can |
| 2041 | // kill the second parameter. |
| 2042 | |
| 2043 | if (IsForRef) { |
| 2044 | EvalResult Result; |
| 2045 | return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects; |
| 2046 | } |
Eli Friedman | 1f4a6db | 2009-02-20 02:36:22 +0000 | [diff] [blame] | 2047 | |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 2048 | switch (getStmtClass()) { |
Eli Friedman | c39dc9a | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 2049 | default: break; |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 2050 | case StringLiteralClass: |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 2051 | case ObjCStringLiteralClass: |
Chris Lattner | eaf2bb8 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 2052 | case ObjCEncodeExprClass: |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 2053 | return true; |
John McCall | b4b9b15 | 2010-08-01 21:51:45 +0000 | [diff] [blame] | 2054 | case CXXTemporaryObjectExprClass: |
| 2055 | case CXXConstructExprClass: { |
| 2056 | const CXXConstructExpr *CE = cast<CXXConstructExpr>(this); |
John McCall | 4204f07 | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 2057 | |
| 2058 | // Only if it's |
| 2059 | // 1) an application of the trivial default constructor or |
John McCall | b4b9b15 | 2010-08-01 21:51:45 +0000 | [diff] [blame] | 2060 | if (!CE->getConstructor()->isTrivial()) return false; |
John McCall | 4204f07 | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 2061 | if (!CE->getNumArgs()) return true; |
| 2062 | |
| 2063 | // 2) an elidable trivial copy construction of an operand which is |
| 2064 | // itself a constant initializer. Note that we consider the |
| 2065 | // operand on its own, *not* as a reference binding. |
| 2066 | return CE->isElidable() && |
| 2067 | CE->getArg(0)->isConstantInitializer(Ctx, false); |
John McCall | b4b9b15 | 2010-08-01 21:51:45 +0000 | [diff] [blame] | 2068 | } |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2069 | case CompoundLiteralExprClass: { |
Eli Friedman | 1f4a6db | 2009-02-20 02:36:22 +0000 | [diff] [blame] | 2070 | // This handles gcc's extension that allows global initializers like |
| 2071 | // "struct x {int x;} x = (struct x) {};". |
| 2072 | // FIXME: This accepts other cases it shouldn't! |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2073 | const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer(); |
John McCall | 4204f07 | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 2074 | return Exp->isConstantInitializer(Ctx, false); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 2075 | } |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 2076 | case InitListExprClass: { |
Eli Friedman | 1f4a6db | 2009-02-20 02:36:22 +0000 | [diff] [blame] | 2077 | // FIXME: This doesn't deal with fields with reference types correctly. |
| 2078 | // FIXME: This incorrectly allows pointers cast to integers to be assigned |
| 2079 | // to bitfields. |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 2080 | const InitListExpr *Exp = cast<InitListExpr>(this); |
| 2081 | unsigned numInits = Exp->getNumInits(); |
| 2082 | for (unsigned i = 0; i < numInits; i++) { |
John McCall | 4204f07 | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 2083 | if (!Exp->getInit(i)->isConstantInitializer(Ctx, false)) |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 2084 | return false; |
| 2085 | } |
Eli Friedman | c39dc9a | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 2086 | return true; |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 2087 | } |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 2088 | case ImplicitValueInitExprClass: |
| 2089 | return true; |
Chris Lattner | 3ae9f48 | 2009-10-13 07:14:16 +0000 | [diff] [blame] | 2090 | case ParenExprClass: |
John McCall | 4204f07 | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 2091 | return cast<ParenExpr>(this)->getSubExpr() |
| 2092 | ->isConstantInitializer(Ctx, IsForRef); |
Abramo Bagnara | 5cadfab | 2010-09-27 07:13:32 +0000 | [diff] [blame] | 2093 | case ChooseExprClass: |
| 2094 | return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx) |
| 2095 | ->isConstantInitializer(Ctx, IsForRef); |
Eli Friedman | c39dc9a | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 2096 | case UnaryOperatorClass: { |
| 2097 | const UnaryOperator* Exp = cast<UnaryOperator>(this); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2098 | if (Exp->getOpcode() == UO_Extension) |
John McCall | 4204f07 | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 2099 | return Exp->getSubExpr()->isConstantInitializer(Ctx, false); |
Eli Friedman | c39dc9a | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 2100 | break; |
| 2101 | } |
Chris Lattner | 3ae9f48 | 2009-10-13 07:14:16 +0000 | [diff] [blame] | 2102 | case BinaryOperatorClass: { |
| 2103 | // Special case &&foo - &&bar. It would be nice to generalize this somehow |
| 2104 | // but this handles the common case. |
| 2105 | const BinaryOperator *Exp = cast<BinaryOperator>(this); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2106 | if (Exp->getOpcode() == BO_Sub && |
Chris Lattner | 3ae9f48 | 2009-10-13 07:14:16 +0000 | [diff] [blame] | 2107 | isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) && |
| 2108 | isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx))) |
| 2109 | return true; |
| 2110 | break; |
| 2111 | } |
John McCall | 4204f07 | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 2112 | case CXXFunctionalCastExprClass: |
John McCall | b4b9b15 | 2010-08-01 21:51:45 +0000 | [diff] [blame] | 2113 | case CXXStaticCastExprClass: |
Chris Lattner | 81045d8 | 2009-04-21 05:19:11 +0000 | [diff] [blame] | 2114 | case ImplicitCastExprClass: |
Eli Friedman | c39dc9a | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 2115 | case CStyleCastExprClass: |
| 2116 | // Handle casts with a destination that's a struct or union; this |
| 2117 | // deals with both the gcc no-op struct cast extension and the |
| 2118 | // cast-to-union extension. |
| 2119 | if (getType()->isRecordType()) |
John McCall | 4204f07 | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 2120 | return cast<CastExpr>(this)->getSubExpr() |
| 2121 | ->isConstantInitializer(Ctx, false); |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2122 | |
Chris Lattner | 430656e | 2009-10-13 22:12:09 +0000 | [diff] [blame] | 2123 | // Integer->integer casts can be handled here, which is important for |
| 2124 | // things like (int)(&&x-&&y). Scary but true. |
| 2125 | if (getType()->isIntegerType() && |
| 2126 | cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType()) |
John McCall | 4204f07 | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 2127 | return cast<CastExpr>(this)->getSubExpr() |
| 2128 | ->isConstantInitializer(Ctx, false); |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2129 | |
Eli Friedman | c39dc9a | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 2130 | break; |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 2131 | } |
Eli Friedman | c39dc9a | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 2132 | return isEvaluatable(Ctx); |
Steve Naroff | 38374b0 | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 2133 | } |
| 2134 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2135 | /// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an |
| 2136 | /// integer constant expression with the value zero, or if this is one that is |
| 2137 | /// cast to void*. |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2138 | bool Expr::isNullPointerConstant(ASTContext &Ctx, |
| 2139 | NullPointerConstantValueDependence NPC) const { |
| 2140 | if (isValueDependent()) { |
| 2141 | switch (NPC) { |
| 2142 | case NPC_NeverValueDependent: |
| 2143 | assert(false && "Unexpected value dependent expression!"); |
| 2144 | // If the unthinkable happens, fall through to the safest alternative. |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2145 | |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2146 | case NPC_ValueDependentIsNull: |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 2147 | return isTypeDependent() || getType()->isIntegralType(Ctx); |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2148 | |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2149 | case NPC_ValueDependentIsNotNull: |
| 2150 | return false; |
| 2151 | } |
| 2152 | } |
Daniel Dunbar | f515b22 | 2009-09-18 08:46:16 +0000 | [diff] [blame] | 2153 | |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 2154 | // Strip off a cast to void*, if it exists. Except in C++. |
Argyrios Kyrtzidis | 0835a3c | 2008-08-18 23:01:59 +0000 | [diff] [blame] | 2155 | if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) { |
Sebastian Redl | 6215dee | 2008-11-04 11:45:54 +0000 | [diff] [blame] | 2156 | if (!Ctx.getLangOptions().CPlusPlus) { |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 2157 | // Check that it is a cast to void*. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2158 | if (const PointerType *PT = CE->getType()->getAs<PointerType>()) { |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 2159 | QualType Pointee = PT->getPointeeType(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2160 | if (!Pointee.hasQualifiers() && |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 2161 | Pointee->isVoidType() && // to void* |
| 2162 | CE->getSubExpr()->getType()->isIntegerType()) // from int. |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2163 | return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC); |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 2164 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2165 | } |
Steve Naroff | aa58f00 | 2008-01-14 16:10:57 +0000 | [diff] [blame] | 2166 | } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) { |
| 2167 | // Ignore the ImplicitCastExpr type entirely. |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2168 | return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC); |
Steve Naroff | aa58f00 | 2008-01-14 16:10:57 +0000 | [diff] [blame] | 2169 | } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) { |
| 2170 | // Accept ((void*)0) as a null pointer constant, as many other |
| 2171 | // implementations do. |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2172 | return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2173 | } else if (const CXXDefaultArgExpr *DefaultArg |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 2174 | = dyn_cast<CXXDefaultArgExpr>(this)) { |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2175 | // See through default argument expressions |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2176 | return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC); |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 2177 | } else if (isa<GNUNullExpr>(this)) { |
| 2178 | // The GNU __null extension is always a null pointer constant. |
| 2179 | return true; |
Steve Naroff | aaffbf7 | 2008-01-14 02:53:34 +0000 | [diff] [blame] | 2180 | } |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 2181 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2182 | // C++0x nullptr_t is always a null pointer constant. |
| 2183 | if (getType()->isNullPtrType()) |
| 2184 | return true; |
| 2185 | |
Fariborz Jahanian | ff3a078 | 2010-09-27 22:42:37 +0000 | [diff] [blame] | 2186 | if (const RecordType *UT = getType()->getAsUnionType()) |
| 2187 | if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
| 2188 | if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){ |
| 2189 | const Expr *InitExpr = CLE->getInitializer(); |
| 2190 | if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr)) |
| 2191 | return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC); |
| 2192 | } |
Steve Naroff | aa58f00 | 2008-01-14 16:10:57 +0000 | [diff] [blame] | 2193 | // This expression must be an integer type. |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2194 | if (!getType()->isIntegerType() || |
Fariborz Jahanian | 56fc0d1 | 2009-10-06 00:09:31 +0000 | [diff] [blame] | 2195 | (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType())) |
Steve Naroff | aa58f00 | 2008-01-14 16:10:57 +0000 | [diff] [blame] | 2196 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2197 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2198 | // If we have an integer constant expression, we need to *evaluate* it and |
| 2199 | // test for the value 0. |
Eli Friedman | 09de176 | 2009-04-25 22:37:12 +0000 | [diff] [blame] | 2200 | llvm::APSInt Result; |
| 2201 | return isIntegerConstantExpr(Result, Ctx) && Result == 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2202 | } |
Steve Naroff | 31a4584 | 2007-07-28 23:10:27 +0000 | [diff] [blame] | 2203 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 2204 | /// \brief If this expression is an l-value for an Objective C |
| 2205 | /// property, find the underlying property reference expression. |
| 2206 | const ObjCPropertyRefExpr *Expr::getObjCProperty() const { |
| 2207 | const Expr *E = this; |
| 2208 | while (true) { |
| 2209 | assert((E->getValueKind() == VK_LValue && |
| 2210 | E->getObjectKind() == OK_ObjCProperty) && |
| 2211 | "expression is not a property reference"); |
| 2212 | E = E->IgnoreParenCasts(); |
| 2213 | if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
| 2214 | if (BO->getOpcode() == BO_Comma) { |
| 2215 | E = BO->getRHS(); |
| 2216 | continue; |
| 2217 | } |
| 2218 | } |
| 2219 | |
| 2220 | break; |
| 2221 | } |
| 2222 | |
| 2223 | return cast<ObjCPropertyRefExpr>(E); |
| 2224 | } |
| 2225 | |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2226 | FieldDecl *Expr::getBitField() { |
Douglas Gregor | 6f4a69a | 2009-07-06 15:38:40 +0000 | [diff] [blame] | 2227 | Expr *E = this->IgnoreParens(); |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2228 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 2229 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 2230 | if (ICE->getCastKind() == CK_LValueToRValue || |
| 2231 | (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp)) |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 2232 | E = ICE->getSubExpr()->IgnoreParens(); |
| 2233 | else |
| 2234 | break; |
| 2235 | } |
| 2236 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2237 | if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E)) |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 2238 | if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl())) |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2239 | if (Field->isBitField()) |
| 2240 | return Field; |
| 2241 | |
Argyrios Kyrtzidis | 0f279e7 | 2010-10-30 19:52:22 +0000 | [diff] [blame] | 2242 | if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E)) |
| 2243 | if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl())) |
| 2244 | if (Field->isBitField()) |
| 2245 | return Field; |
| 2246 | |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2247 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) |
| 2248 | if (BinOp->isAssignmentOp() && BinOp->getLHS()) |
| 2249 | return BinOp->getLHS()->getBitField(); |
| 2250 | |
| 2251 | return 0; |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2252 | } |
| 2253 | |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 2254 | bool Expr::refersToVectorElement() const { |
| 2255 | const Expr *E = this->IgnoreParens(); |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2256 | |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 2257 | while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2258 | if (ICE->getValueKind() != VK_RValue && |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2259 | ICE->getCastKind() == CK_NoOp) |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 2260 | E = ICE->getSubExpr()->IgnoreParens(); |
| 2261 | else |
| 2262 | break; |
| 2263 | } |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2264 | |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 2265 | if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) |
| 2266 | return ASE->getBase()->getType()->isVectorType(); |
| 2267 | |
| 2268 | if (isa<ExtVectorElementExpr>(E)) |
| 2269 | return true; |
| 2270 | |
| 2271 | return false; |
| 2272 | } |
| 2273 | |
Chris Lattner | 2140e90 | 2009-02-16 22:14:05 +0000 | [diff] [blame] | 2274 | /// isArrow - Return true if the base expression is a pointer to vector, |
| 2275 | /// return false if the base expression is a vector. |
| 2276 | bool ExtVectorElementExpr::isArrow() const { |
| 2277 | return getBase()->getType()->isPointerType(); |
| 2278 | } |
| 2279 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2280 | unsigned ExtVectorElementExpr::getNumElements() const { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2281 | if (const VectorType *VT = getType()->getAs<VectorType>()) |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2282 | return VT->getNumElements(); |
| 2283 | return 1; |
Chris Lattner | 4d0ac88 | 2007-08-03 16:00:20 +0000 | [diff] [blame] | 2284 | } |
| 2285 | |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2286 | /// containsDuplicateElements - Return true if any element access is repeated. |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2287 | bool ExtVectorElementExpr::containsDuplicateElements() const { |
Daniel Dunbar | a2b34eb | 2009-10-18 02:09:09 +0000 | [diff] [blame] | 2288 | // FIXME: Refactor this code to an accessor on the AST node which returns the |
| 2289 | // "type" of component access, and share with code below and in Sema. |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 2290 | llvm::StringRef Comp = Accessor->getName(); |
Nate Begeman | 190d6a2 | 2009-01-18 02:01:21 +0000 | [diff] [blame] | 2291 | |
| 2292 | // Halving swizzles do not contain duplicate elements. |
Daniel Dunbar | 1502742 | 2009-10-17 23:53:04 +0000 | [diff] [blame] | 2293 | if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd") |
Nate Begeman | 190d6a2 | 2009-01-18 02:01:21 +0000 | [diff] [blame] | 2294 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2295 | |
Nate Begeman | 190d6a2 | 2009-01-18 02:01:21 +0000 | [diff] [blame] | 2296 | // Advance past s-char prefix on hex swizzles. |
Daniel Dunbar | 1502742 | 2009-10-17 23:53:04 +0000 | [diff] [blame] | 2297 | if (Comp[0] == 's' || Comp[0] == 'S') |
| 2298 | Comp = Comp.substr(1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2299 | |
Daniel Dunbar | 1502742 | 2009-10-17 23:53:04 +0000 | [diff] [blame] | 2300 | for (unsigned i = 0, e = Comp.size(); i != e; ++i) |
| 2301 | if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos) |
Steve Naroff | fec0b49 | 2007-07-30 03:29:09 +0000 | [diff] [blame] | 2302 | return true; |
Daniel Dunbar | 1502742 | 2009-10-17 23:53:04 +0000 | [diff] [blame] | 2303 | |
Steve Naroff | fec0b49 | 2007-07-30 03:29:09 +0000 | [diff] [blame] | 2304 | return false; |
| 2305 | } |
Chris Lattner | b8f849d | 2007-08-02 23:36:59 +0000 | [diff] [blame] | 2306 | |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2307 | /// getEncodedElementAccess - We encode the fields as a llvm ConstantArray. |
Nate Begeman | 3b8d116 | 2008-05-13 21:03:02 +0000 | [diff] [blame] | 2308 | void ExtVectorElementExpr::getEncodedElementAccess( |
| 2309 | llvm::SmallVectorImpl<unsigned> &Elts) const { |
Daniel Dunbar | 4b55b24 | 2009-10-18 02:09:31 +0000 | [diff] [blame] | 2310 | llvm::StringRef Comp = Accessor->getName(); |
| 2311 | if (Comp[0] == 's' || Comp[0] == 'S') |
| 2312 | Comp = Comp.substr(1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2313 | |
Daniel Dunbar | 4b55b24 | 2009-10-18 02:09:31 +0000 | [diff] [blame] | 2314 | bool isHi = Comp == "hi"; |
| 2315 | bool isLo = Comp == "lo"; |
| 2316 | bool isEven = Comp == "even"; |
| 2317 | bool isOdd = Comp == "odd"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2318 | |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2319 | for (unsigned i = 0, e = getNumElements(); i != e; ++i) { |
| 2320 | uint64_t Index; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2321 | |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2322 | if (isHi) |
| 2323 | Index = e + i; |
| 2324 | else if (isLo) |
| 2325 | Index = i; |
| 2326 | else if (isEven) |
| 2327 | Index = 2 * i; |
| 2328 | else if (isOdd) |
| 2329 | Index = 2 * i + 1; |
| 2330 | else |
Daniel Dunbar | 4b55b24 | 2009-10-18 02:09:31 +0000 | [diff] [blame] | 2331 | Index = ExtVectorType::getAccessorIdx(Comp[i]); |
Chris Lattner | b8f849d | 2007-08-02 23:36:59 +0000 | [diff] [blame] | 2332 | |
Nate Begeman | 3b8d116 | 2008-05-13 21:03:02 +0000 | [diff] [blame] | 2333 | Elts.push_back(Index); |
Chris Lattner | b8f849d | 2007-08-02 23:36:59 +0000 | [diff] [blame] | 2334 | } |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2335 | } |
| 2336 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2337 | ObjCMessageExpr::ObjCMessageExpr(QualType T, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2338 | ExprValueKind VK, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2339 | SourceLocation LBracLoc, |
| 2340 | SourceLocation SuperLoc, |
| 2341 | bool IsInstanceSuper, |
| 2342 | QualType SuperType, |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2343 | Selector Sel, |
Argyrios Kyrtzidis | f40f0d5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 2344 | SourceLocation SelLoc, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2345 | ObjCMethodDecl *Method, |
| 2346 | Expr **Args, unsigned NumArgs, |
| 2347 | SourceLocation RBracLoc) |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2348 | : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2349 | /*TypeDependent=*/false, /*ValueDependent=*/false, |
| 2350 | /*ContainsUnexpandedParameterPack=*/false), |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2351 | NumArgs(NumArgs), Kind(IsInstanceSuper? SuperInstance : SuperClass), |
| 2352 | HasMethod(Method != 0), SuperLoc(SuperLoc), |
| 2353 | SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method |
| 2354 | : Sel.getAsOpaquePtr())), |
Argyrios Kyrtzidis | f40f0d5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 2355 | SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc) |
Douglas Gregor | c2350e5 | 2010-03-08 16:40:19 +0000 | [diff] [blame] | 2356 | { |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2357 | setReceiverPointer(SuperType.getAsOpaquePtr()); |
| 2358 | if (NumArgs) |
| 2359 | memcpy(getArgs(), Args, NumArgs * sizeof(Expr *)); |
Ted Kremenek | 4df728e | 2008-06-24 15:50:53 +0000 | [diff] [blame] | 2360 | } |
| 2361 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2362 | ObjCMessageExpr::ObjCMessageExpr(QualType T, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2363 | ExprValueKind VK, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2364 | SourceLocation LBracLoc, |
| 2365 | TypeSourceInfo *Receiver, |
Argyrios Kyrtzidis | f40f0d5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 2366 | Selector Sel, |
| 2367 | SourceLocation SelLoc, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2368 | ObjCMethodDecl *Method, |
| 2369 | Expr **Args, unsigned NumArgs, |
| 2370 | SourceLocation RBracLoc) |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2371 | : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(), |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2372 | T->isDependentType(), T->containsUnexpandedParameterPack()), |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2373 | NumArgs(NumArgs), Kind(Class), HasMethod(Method != 0), |
| 2374 | SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method |
| 2375 | : Sel.getAsOpaquePtr())), |
Argyrios Kyrtzidis | f40f0d5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 2376 | SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc) |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2377 | { |
| 2378 | setReceiverPointer(Receiver); |
Douglas Gregor | aa165f8 | 2011-01-03 19:04:46 +0000 | [diff] [blame] | 2379 | Expr **MyArgs = getArgs(); |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2380 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 2381 | if (Args[I]->isTypeDependent()) |
| 2382 | ExprBits.TypeDependent = true; |
| 2383 | if (Args[I]->isValueDependent()) |
| 2384 | ExprBits.ValueDependent = true; |
| 2385 | if (Args[I]->containsUnexpandedParameterPack()) |
| 2386 | ExprBits.ContainsUnexpandedParameterPack = true; |
| 2387 | |
| 2388 | MyArgs[I] = Args[I]; |
| 2389 | } |
Ted Kremenek | 4df728e | 2008-06-24 15:50:53 +0000 | [diff] [blame] | 2390 | } |
| 2391 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2392 | ObjCMessageExpr::ObjCMessageExpr(QualType T, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2393 | ExprValueKind VK, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2394 | SourceLocation LBracLoc, |
| 2395 | Expr *Receiver, |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2396 | Selector Sel, |
Argyrios Kyrtzidis | f40f0d5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 2397 | SourceLocation SelLoc, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2398 | ObjCMethodDecl *Method, |
| 2399 | Expr **Args, unsigned NumArgs, |
| 2400 | SourceLocation RBracLoc) |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2401 | : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(), |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2402 | Receiver->isTypeDependent(), |
| 2403 | Receiver->containsUnexpandedParameterPack()), |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2404 | NumArgs(NumArgs), Kind(Instance), HasMethod(Method != 0), |
| 2405 | SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method |
| 2406 | : Sel.getAsOpaquePtr())), |
Argyrios Kyrtzidis | f40f0d5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 2407 | SelectorLoc(SelLoc), LBracLoc(LBracLoc), RBracLoc(RBracLoc) |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2408 | { |
| 2409 | setReceiverPointer(Receiver); |
Douglas Gregor | aa165f8 | 2011-01-03 19:04:46 +0000 | [diff] [blame] | 2410 | Expr **MyArgs = getArgs(); |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2411 | for (unsigned I = 0; I != NumArgs; ++I) { |
| 2412 | if (Args[I]->isTypeDependent()) |
| 2413 | ExprBits.TypeDependent = true; |
| 2414 | if (Args[I]->isValueDependent()) |
| 2415 | ExprBits.ValueDependent = true; |
| 2416 | if (Args[I]->containsUnexpandedParameterPack()) |
| 2417 | ExprBits.ContainsUnexpandedParameterPack = true; |
| 2418 | |
| 2419 | MyArgs[I] = Args[I]; |
| 2420 | } |
Chris Lattner | 0389e6b | 2009-04-26 00:44:05 +0000 | [diff] [blame] | 2421 | } |
| 2422 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2423 | ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2424 | ExprValueKind VK, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2425 | SourceLocation LBracLoc, |
| 2426 | SourceLocation SuperLoc, |
| 2427 | bool IsInstanceSuper, |
| 2428 | QualType SuperType, |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2429 | Selector Sel, |
Argyrios Kyrtzidis | f40f0d5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 2430 | SourceLocation SelLoc, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2431 | ObjCMethodDecl *Method, |
| 2432 | Expr **Args, unsigned NumArgs, |
| 2433 | SourceLocation RBracLoc) { |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2434 | unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) + |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2435 | NumArgs * sizeof(Expr *); |
| 2436 | void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2437 | return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper, |
Argyrios Kyrtzidis | f40f0d5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 2438 | SuperType, Sel, SelLoc, Method, Args,NumArgs, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2439 | RBracLoc); |
| 2440 | } |
| 2441 | |
| 2442 | ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2443 | ExprValueKind VK, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2444 | SourceLocation LBracLoc, |
| 2445 | TypeSourceInfo *Receiver, |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2446 | Selector Sel, |
Argyrios Kyrtzidis | f40f0d5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 2447 | SourceLocation SelLoc, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2448 | ObjCMethodDecl *Method, |
| 2449 | Expr **Args, unsigned NumArgs, |
| 2450 | SourceLocation RBracLoc) { |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2451 | unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) + |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2452 | NumArgs * sizeof(Expr *); |
| 2453 | void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment); |
Argyrios Kyrtzidis | f40f0d5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 2454 | return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLoc, |
| 2455 | Method, Args, NumArgs, RBracLoc); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2456 | } |
| 2457 | |
| 2458 | ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2459 | ExprValueKind VK, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2460 | SourceLocation LBracLoc, |
| 2461 | Expr *Receiver, |
Argyrios Kyrtzidis | f40f0d5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 2462 | Selector Sel, |
| 2463 | SourceLocation SelLoc, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2464 | ObjCMethodDecl *Method, |
| 2465 | Expr **Args, unsigned NumArgs, |
| 2466 | SourceLocation RBracLoc) { |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2467 | unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) + |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2468 | NumArgs * sizeof(Expr *); |
| 2469 | void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment); |
Argyrios Kyrtzidis | f40f0d5 | 2010-12-10 20:08:27 +0000 | [diff] [blame] | 2470 | return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, SelLoc, |
| 2471 | Method, Args, NumArgs, RBracLoc); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2472 | } |
| 2473 | |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2474 | ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2475 | unsigned NumArgs) { |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2476 | unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) + |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2477 | NumArgs * sizeof(Expr *); |
| 2478 | void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment); |
| 2479 | return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs); |
| 2480 | } |
Argyrios Kyrtzidis | e005d19 | 2010-12-10 20:08:30 +0000 | [diff] [blame] | 2481 | |
| 2482 | SourceRange ObjCMessageExpr::getReceiverRange() const { |
| 2483 | switch (getReceiverKind()) { |
| 2484 | case Instance: |
| 2485 | return getInstanceReceiver()->getSourceRange(); |
| 2486 | |
| 2487 | case Class: |
| 2488 | return getClassReceiverTypeInfo()->getTypeLoc().getSourceRange(); |
| 2489 | |
| 2490 | case SuperInstance: |
| 2491 | case SuperClass: |
| 2492 | return getSuperLoc(); |
| 2493 | } |
| 2494 | |
| 2495 | return SourceLocation(); |
| 2496 | } |
| 2497 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2498 | Selector ObjCMessageExpr::getSelector() const { |
| 2499 | if (HasMethod) |
| 2500 | return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod) |
| 2501 | ->getSelector(); |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2502 | return Selector(SelectorOrMethod); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2503 | } |
| 2504 | |
| 2505 | ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const { |
| 2506 | switch (getReceiverKind()) { |
| 2507 | case Instance: |
| 2508 | if (const ObjCObjectPointerType *Ptr |
| 2509 | = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>()) |
| 2510 | return Ptr->getInterfaceDecl(); |
| 2511 | break; |
| 2512 | |
| 2513 | case Class: |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2514 | if (const ObjCObjectType *Ty |
| 2515 | = getClassReceiver()->getAs<ObjCObjectType>()) |
| 2516 | return Ty->getInterface(); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2517 | break; |
| 2518 | |
| 2519 | case SuperInstance: |
| 2520 | if (const ObjCObjectPointerType *Ptr |
| 2521 | = getSuperType()->getAs<ObjCObjectPointerType>()) |
| 2522 | return Ptr->getInterfaceDecl(); |
| 2523 | break; |
| 2524 | |
| 2525 | case SuperClass: |
Argyrios Kyrtzidis | ee8a6ca | 2011-01-25 00:03:48 +0000 | [diff] [blame] | 2526 | if (const ObjCObjectType *Iface |
| 2527 | = getSuperType()->getAs<ObjCObjectType>()) |
| 2528 | return Iface->getInterface(); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2529 | break; |
| 2530 | } |
| 2531 | |
| 2532 | return 0; |
Ted Kremenek | eb3b324 | 2010-02-11 22:41:21 +0000 | [diff] [blame] | 2533 | } |
Chris Lattner | 0389e6b | 2009-04-26 00:44:05 +0000 | [diff] [blame] | 2534 | |
Jay Foad | 4ba2a17 | 2011-01-12 09:06:06 +0000 | [diff] [blame] | 2535 | bool ChooseExpr::isConditionTrue(const ASTContext &C) const { |
Eli Friedman | 9a901bb | 2009-04-26 19:19:15 +0000 | [diff] [blame] | 2536 | return getCond()->EvaluateAsInt(C) != 0; |
Chris Lattner | 27437ca | 2007-10-25 00:29:32 +0000 | [diff] [blame] | 2537 | } |
| 2538 | |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2539 | ShuffleVectorExpr::ShuffleVectorExpr(ASTContext &C, Expr **args, unsigned nexpr, |
| 2540 | QualType Type, SourceLocation BLoc, |
| 2541 | SourceLocation RP) |
| 2542 | : Expr(ShuffleVectorExprClass, Type, VK_RValue, OK_Ordinary, |
| 2543 | Type->isDependentType(), Type->isDependentType(), |
| 2544 | Type->containsUnexpandedParameterPack()), |
| 2545 | BuiltinLoc(BLoc), RParenLoc(RP), NumExprs(nexpr) |
| 2546 | { |
| 2547 | SubExprs = new (C) Stmt*[nexpr]; |
| 2548 | for (unsigned i = 0; i < nexpr; i++) { |
| 2549 | if (args[i]->isTypeDependent()) |
| 2550 | ExprBits.TypeDependent = true; |
| 2551 | if (args[i]->isValueDependent()) |
| 2552 | ExprBits.ValueDependent = true; |
| 2553 | if (args[i]->containsUnexpandedParameterPack()) |
| 2554 | ExprBits.ContainsUnexpandedParameterPack = true; |
| 2555 | |
| 2556 | SubExprs[i] = args[i]; |
| 2557 | } |
| 2558 | } |
| 2559 | |
Nate Begeman | 888376a | 2009-08-12 02:28:50 +0000 | [diff] [blame] | 2560 | void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs, |
| 2561 | unsigned NumExprs) { |
| 2562 | if (SubExprs) C.Deallocate(SubExprs); |
| 2563 | |
| 2564 | SubExprs = new (C) Stmt* [NumExprs]; |
Douglas Gregor | 94cd5d1 | 2009-04-16 00:01:45 +0000 | [diff] [blame] | 2565 | this->NumExprs = NumExprs; |
| 2566 | memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2567 | } |
Nate Begeman | 888376a | 2009-08-12 02:28:50 +0000 | [diff] [blame] | 2568 | |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 2569 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2570 | // DesignatedInitExpr |
| 2571 | //===----------------------------------------------------------------------===// |
| 2572 | |
| 2573 | IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() { |
| 2574 | assert(Kind == FieldDesignator && "Only valid on a field designator"); |
| 2575 | if (Field.NameOrField & 0x01) |
| 2576 | return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01); |
| 2577 | else |
| 2578 | return getField()->getIdentifier(); |
| 2579 | } |
| 2580 | |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2581 | DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty, |
Douglas Gregor | 319d57f | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 2582 | unsigned NumDesignators, |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2583 | const Designator *Designators, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2584 | SourceLocation EqualOrColonLoc, |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2585 | bool GNUSyntax, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2586 | Expr **IndexExprs, |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2587 | unsigned NumIndexExprs, |
| 2588 | Expr *Init) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2589 | : Expr(DesignatedInitExprClass, Ty, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2590 | Init->getValueKind(), Init->getObjectKind(), |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2591 | Init->isTypeDependent(), Init->isValueDependent(), |
| 2592 | Init->containsUnexpandedParameterPack()), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2593 | EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax), |
| 2594 | NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) { |
Douglas Gregor | 319d57f | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 2595 | this->Designators = new (C) Designator[NumDesignators]; |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2596 | |
| 2597 | // Record the initializer itself. |
John McCall | 7502c1d | 2011-02-13 04:07:26 +0000 | [diff] [blame] | 2598 | child_range Child = children(); |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2599 | *Child++ = Init; |
| 2600 | |
| 2601 | // Copy the designators and their subexpressions, computing |
| 2602 | // value-dependence along the way. |
| 2603 | unsigned IndexIdx = 0; |
| 2604 | for (unsigned I = 0; I != NumDesignators; ++I) { |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2605 | this->Designators[I] = Designators[I]; |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2606 | |
| 2607 | if (this->Designators[I].isArrayDesignator()) { |
| 2608 | // Compute type- and value-dependence. |
| 2609 | Expr *Index = IndexExprs[IndexIdx]; |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2610 | if (Index->isTypeDependent() || Index->isValueDependent()) |
| 2611 | ExprBits.ValueDependent = true; |
| 2612 | |
| 2613 | // Propagate unexpanded parameter packs. |
| 2614 | if (Index->containsUnexpandedParameterPack()) |
| 2615 | ExprBits.ContainsUnexpandedParameterPack = true; |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2616 | |
| 2617 | // Copy the index expressions into permanent storage. |
| 2618 | *Child++ = IndexExprs[IndexIdx++]; |
| 2619 | } else if (this->Designators[I].isArrayRangeDesignator()) { |
| 2620 | // Compute type- and value-dependence. |
| 2621 | Expr *Start = IndexExprs[IndexIdx]; |
| 2622 | Expr *End = IndexExprs[IndexIdx + 1]; |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2623 | if (Start->isTypeDependent() || Start->isValueDependent() || |
| 2624 | End->isTypeDependent() || End->isValueDependent()) |
| 2625 | ExprBits.ValueDependent = true; |
| 2626 | |
| 2627 | // Propagate unexpanded parameter packs. |
| 2628 | if (Start->containsUnexpandedParameterPack() || |
| 2629 | End->containsUnexpandedParameterPack()) |
| 2630 | ExprBits.ContainsUnexpandedParameterPack = true; |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2631 | |
| 2632 | // Copy the start/end expressions into permanent storage. |
| 2633 | *Child++ = IndexExprs[IndexIdx++]; |
| 2634 | *Child++ = IndexExprs[IndexIdx++]; |
| 2635 | } |
| 2636 | } |
| 2637 | |
| 2638 | assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions"); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2639 | } |
| 2640 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2641 | DesignatedInitExpr * |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2642 | DesignatedInitExpr::Create(ASTContext &C, Designator *Designators, |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2643 | unsigned NumDesignators, |
| 2644 | Expr **IndexExprs, unsigned NumIndexExprs, |
| 2645 | SourceLocation ColonOrEqualLoc, |
| 2646 | bool UsesColonSyntax, Expr *Init) { |
Steve Naroff | c0ac492 | 2009-01-27 23:20:32 +0000 | [diff] [blame] | 2647 | void *Mem = C.Allocate(sizeof(DesignatedInitExpr) + |
Steve Naroff | c0ac492 | 2009-01-27 23:20:32 +0000 | [diff] [blame] | 2648 | sizeof(Stmt *) * (NumIndexExprs + 1), 8); |
Douglas Gregor | 319d57f | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 2649 | return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators, |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2650 | ColonOrEqualLoc, UsesColonSyntax, |
| 2651 | IndexExprs, NumIndexExprs, Init); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2652 | } |
| 2653 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2654 | DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C, |
Douglas Gregor | d077d75 | 2009-04-16 00:55:48 +0000 | [diff] [blame] | 2655 | unsigned NumIndexExprs) { |
| 2656 | void *Mem = C.Allocate(sizeof(DesignatedInitExpr) + |
| 2657 | sizeof(Stmt *) * (NumIndexExprs + 1), 8); |
| 2658 | return new (Mem) DesignatedInitExpr(NumIndexExprs + 1); |
| 2659 | } |
| 2660 | |
Douglas Gregor | 319d57f | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 2661 | void DesignatedInitExpr::setDesignators(ASTContext &C, |
| 2662 | const Designator *Desigs, |
Douglas Gregor | d077d75 | 2009-04-16 00:55:48 +0000 | [diff] [blame] | 2663 | unsigned NumDesigs) { |
Douglas Gregor | 319d57f | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 2664 | Designators = new (C) Designator[NumDesigs]; |
Douglas Gregor | d077d75 | 2009-04-16 00:55:48 +0000 | [diff] [blame] | 2665 | NumDesignators = NumDesigs; |
| 2666 | for (unsigned I = 0; I != NumDesigs; ++I) |
| 2667 | Designators[I] = Desigs[I]; |
| 2668 | } |
| 2669 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2670 | SourceRange DesignatedInitExpr::getSourceRange() const { |
| 2671 | SourceLocation StartLoc; |
Chris Lattner | d603eaa | 2009-02-16 22:33:34 +0000 | [diff] [blame] | 2672 | Designator &First = |
| 2673 | *const_cast<DesignatedInitExpr*>(this)->designators_begin(); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2674 | if (First.isFieldDesignator()) { |
Douglas Gregor | eeae8f0 | 2009-03-28 00:41:23 +0000 | [diff] [blame] | 2675 | if (GNUSyntax) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2676 | StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc); |
| 2677 | else |
| 2678 | StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc); |
| 2679 | } else |
Chris Lattner | d603eaa | 2009-02-16 22:33:34 +0000 | [diff] [blame] | 2680 | StartLoc = |
| 2681 | SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2682 | return SourceRange(StartLoc, getInit()->getSourceRange().getEnd()); |
| 2683 | } |
| 2684 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2685 | Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) { |
| 2686 | assert(D.Kind == Designator::ArrayDesignator && "Requires array designator"); |
| 2687 | char* Ptr = static_cast<char*>(static_cast<void *>(this)); |
| 2688 | Ptr += sizeof(DesignatedInitExpr); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2689 | Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr)); |
| 2690 | return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1)); |
| 2691 | } |
| 2692 | |
| 2693 | Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2694 | assert(D.Kind == Designator::ArrayRangeDesignator && |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2695 | "Requires array range designator"); |
| 2696 | char* Ptr = static_cast<char*>(static_cast<void *>(this)); |
| 2697 | Ptr += sizeof(DesignatedInitExpr); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2698 | Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr)); |
| 2699 | return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1)); |
| 2700 | } |
| 2701 | |
| 2702 | Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2703 | assert(D.Kind == Designator::ArrayRangeDesignator && |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2704 | "Requires array range designator"); |
| 2705 | char* Ptr = static_cast<char*>(static_cast<void *>(this)); |
| 2706 | Ptr += sizeof(DesignatedInitExpr); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2707 | Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr)); |
| 2708 | return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2)); |
| 2709 | } |
| 2710 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2711 | /// \brief Replaces the designator at index @p Idx with the series |
| 2712 | /// of designators in [First, Last). |
Douglas Gregor | 319d57f | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 2713 | void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2714 | const Designator *First, |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2715 | const Designator *Last) { |
| 2716 | unsigned NumNewDesignators = Last - First; |
| 2717 | if (NumNewDesignators == 0) { |
| 2718 | std::copy_backward(Designators + Idx + 1, |
| 2719 | Designators + NumDesignators, |
| 2720 | Designators + Idx); |
| 2721 | --NumNewDesignators; |
| 2722 | return; |
| 2723 | } else if (NumNewDesignators == 1) { |
| 2724 | Designators[Idx] = *First; |
| 2725 | return; |
| 2726 | } |
| 2727 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2728 | Designator *NewDesignators |
Douglas Gregor | 319d57f | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 2729 | = new (C) Designator[NumDesignators - 1 + NumNewDesignators]; |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2730 | std::copy(Designators, Designators + Idx, NewDesignators); |
| 2731 | std::copy(First, Last, NewDesignators + Idx); |
| 2732 | std::copy(Designators + Idx + 1, Designators + NumDesignators, |
| 2733 | NewDesignators + Idx + NumNewDesignators); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2734 | Designators = NewDesignators; |
| 2735 | NumDesignators = NumDesignators - 1 + NumNewDesignators; |
| 2736 | } |
| 2737 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2738 | ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc, |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 2739 | Expr **exprs, unsigned nexprs, |
| 2740 | SourceLocation rparenloc) |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2741 | : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary, |
| 2742 | false, false, false), |
| 2743 | NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2744 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 2745 | Exprs = new (C) Stmt*[nexprs]; |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2746 | for (unsigned i = 0; i != nexprs; ++i) { |
| 2747 | if (exprs[i]->isTypeDependent()) |
| 2748 | ExprBits.TypeDependent = true; |
| 2749 | if (exprs[i]->isValueDependent()) |
| 2750 | ExprBits.ValueDependent = true; |
| 2751 | if (exprs[i]->containsUnexpandedParameterPack()) |
| 2752 | ExprBits.ContainsUnexpandedParameterPack = true; |
| 2753 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 2754 | Exprs[i] = exprs[i]; |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2755 | } |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 2756 | } |
| 2757 | |
John McCall | e996ffd | 2011-02-16 08:02:54 +0000 | [diff] [blame] | 2758 | const OpaqueValueExpr *OpaqueValueExpr::findInCopyConstruct(const Expr *e) { |
| 2759 | if (const ExprWithCleanups *ewc = dyn_cast<ExprWithCleanups>(e)) |
| 2760 | e = ewc->getSubExpr(); |
| 2761 | e = cast<CXXConstructExpr>(e)->getArg(0); |
| 2762 | while (const ImplicitCastExpr *ice = dyn_cast<ImplicitCastExpr>(e)) |
| 2763 | e = ice->getSubExpr(); |
| 2764 | return cast<OpaqueValueExpr>(e); |
| 2765 | } |
| 2766 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2767 | //===----------------------------------------------------------------------===// |
Ted Kremenek | ce2fc3a | 2008-10-27 18:40:21 +0000 | [diff] [blame] | 2768 | // ExprIterator. |
| 2769 | //===----------------------------------------------------------------------===// |
| 2770 | |
| 2771 | Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); } |
| 2772 | Expr* ExprIterator::operator*() const { return cast<Expr>(*I); } |
| 2773 | Expr* ExprIterator::operator->() const { return cast<Expr>(*I); } |
| 2774 | const Expr* ConstExprIterator::operator[](size_t idx) const { |
| 2775 | return cast<Expr>(I[idx]); |
| 2776 | } |
| 2777 | const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); } |
| 2778 | const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); } |
| 2779 | |
| 2780 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 2781 | // Child Iterators for iterating over subexpressions/substatements |
| 2782 | //===----------------------------------------------------------------------===// |
| 2783 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2784 | // SizeOfAlignOfExpr |
John McCall | 63c00d7 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 2785 | Stmt::child_range SizeOfAlignOfExpr::children() { |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2786 | // If this is of a type and the type is a VLA type (and not a typedef), the |
| 2787 | // size expression of the VLA needs to be treated as an executable expression. |
| 2788 | // Why isn't this weirdness documented better in StmtIterator? |
| 2789 | if (isArgumentType()) { |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 2790 | if (const VariableArrayType* T = dyn_cast<VariableArrayType>( |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2791 | getArgumentType().getTypePtr())) |
John McCall | 63c00d7 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 2792 | return child_range(child_iterator(T), child_iterator()); |
| 2793 | return child_range(); |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2794 | } |
John McCall | 63c00d7 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 2795 | return child_range(&Argument.Ex, &Argument.Ex + 1); |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 2796 | } |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 2797 | |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 2798 | // ObjCMessageExpr |
John McCall | 63c00d7 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 2799 | Stmt::child_range ObjCMessageExpr::children() { |
| 2800 | Stmt **begin; |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2801 | if (getReceiverKind() == Instance) |
John McCall | 63c00d7 | 2011-02-09 08:16:59 +0000 | [diff] [blame] | 2802 | begin = reinterpret_cast<Stmt **>(this + 1); |
| 2803 | else |
| 2804 | begin = reinterpret_cast<Stmt **>(getArgs()); |
| 2805 | return child_range(begin, |
| 2806 | reinterpret_cast<Stmt **>(getArgs() + getNumArgs())); |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 2807 | } |
| 2808 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 2809 | // Blocks |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2810 | BlockDeclRefExpr::BlockDeclRefExpr(VarDecl *d, QualType t, ExprValueKind VK, |
Douglas Gregor | a779d9c | 2011-01-19 21:32:01 +0000 | [diff] [blame] | 2811 | SourceLocation l, bool ByRef, |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2812 | bool constAdded) |
Douglas Gregor | d967e31 | 2011-01-19 21:52:31 +0000 | [diff] [blame] | 2813 | : Expr(BlockDeclRefExprClass, t, VK, OK_Ordinary, false, false, |
Douglas Gregor | a779d9c | 2011-01-19 21:32:01 +0000 | [diff] [blame] | 2814 | d->isParameterPack()), |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2815 | D(d), Loc(l), IsByRef(ByRef), ConstQualAdded(constAdded) |
Douglas Gregor | a779d9c | 2011-01-19 21:32:01 +0000 | [diff] [blame] | 2816 | { |
Douglas Gregor | d967e31 | 2011-01-19 21:52:31 +0000 | [diff] [blame] | 2817 | bool TypeDependent = false; |
| 2818 | bool ValueDependent = false; |
| 2819 | computeDeclRefDependence(D, getType(), TypeDependent, ValueDependent); |
| 2820 | ExprBits.TypeDependent = TypeDependent; |
| 2821 | ExprBits.ValueDependent = ValueDependent; |
Douglas Gregor | a779d9c | 2011-01-19 21:32:01 +0000 | [diff] [blame] | 2822 | } |
| 2823 | |