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