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