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