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