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