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 | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 827 | case CK_GetObjCProperty: |
| 828 | return "GetObjCProperty"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 829 | case CK_NoOp: |
Anders Carlsson | f8ec55a | 2009-09-03 00:59:21 +0000 | [diff] [blame] | 830 | return "NoOp"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 831 | case CK_BaseToDerived: |
Anders Carlsson | 11de6de | 2009-11-12 16:43:42 +0000 | [diff] [blame] | 832 | return "BaseToDerived"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 833 | case CK_DerivedToBase: |
Anders Carlsson | f8ec55a | 2009-09-03 00:59:21 +0000 | [diff] [blame] | 834 | return "DerivedToBase"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 835 | case CK_UncheckedDerivedToBase: |
John McCall | 23cba80 | 2010-03-30 23:58:03 +0000 | [diff] [blame] | 836 | return "UncheckedDerivedToBase"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 837 | case CK_Dynamic: |
Anders Carlsson | f8ec55a | 2009-09-03 00:59:21 +0000 | [diff] [blame] | 838 | return "Dynamic"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 839 | case CK_ToUnion: |
Anders Carlsson | f8ec55a | 2009-09-03 00:59:21 +0000 | [diff] [blame] | 840 | return "ToUnion"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 841 | case CK_ArrayToPointerDecay: |
Anders Carlsson | f8ec55a | 2009-09-03 00:59:21 +0000 | [diff] [blame] | 842 | return "ArrayToPointerDecay"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 843 | case CK_FunctionToPointerDecay: |
Anders Carlsson | f8ec55a | 2009-09-03 00:59:21 +0000 | [diff] [blame] | 844 | return "FunctionToPointerDecay"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 845 | case CK_NullToMemberPointer: |
Anders Carlsson | f8ec55a | 2009-09-03 00:59:21 +0000 | [diff] [blame] | 846 | return "NullToMemberPointer"; |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 847 | case CK_NullToPointer: |
| 848 | return "NullToPointer"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 849 | case CK_BaseToDerivedMemberPointer: |
Anders Carlsson | f8ec55a | 2009-09-03 00:59:21 +0000 | [diff] [blame] | 850 | return "BaseToDerivedMemberPointer"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 851 | case CK_DerivedToBaseMemberPointer: |
Anders Carlsson | 1a31a18 | 2009-10-30 00:46:35 +0000 | [diff] [blame] | 852 | return "DerivedToBaseMemberPointer"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 853 | case CK_UserDefinedConversion: |
Anders Carlsson | f8ec55a | 2009-09-03 00:59:21 +0000 | [diff] [blame] | 854 | return "UserDefinedConversion"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 855 | case CK_ConstructorConversion: |
Anders Carlsson | f8ec55a | 2009-09-03 00:59:21 +0000 | [diff] [blame] | 856 | return "ConstructorConversion"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 857 | case CK_IntegralToPointer: |
Anders Carlsson | 7f9e646 | 2009-09-15 04:48:33 +0000 | [diff] [blame] | 858 | return "IntegralToPointer"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 859 | case CK_PointerToIntegral: |
Anders Carlsson | 7f9e646 | 2009-09-15 04:48:33 +0000 | [diff] [blame] | 860 | return "PointerToIntegral"; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 861 | case CK_PointerToBoolean: |
| 862 | return "PointerToBoolean"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 863 | case CK_ToVoid: |
Anders Carlsson | ebeaf20 | 2009-10-16 02:35:04 +0000 | [diff] [blame] | 864 | return "ToVoid"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 865 | case CK_VectorSplat: |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 866 | return "VectorSplat"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 867 | case CK_IntegralCast: |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 868 | return "IntegralCast"; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 869 | case CK_IntegralToBoolean: |
| 870 | return "IntegralToBoolean"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 871 | case CK_IntegralToFloating: |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 872 | return "IntegralToFloating"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 873 | case CK_FloatingToIntegral: |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 874 | return "FloatingToIntegral"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 875 | case CK_FloatingCast: |
Benjamin Kramer | c6b2916 | 2009-10-18 19:02:15 +0000 | [diff] [blame] | 876 | return "FloatingCast"; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 877 | case CK_FloatingToBoolean: |
| 878 | return "FloatingToBoolean"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 879 | case CK_MemberPointerToBoolean: |
Anders Carlsson | bc0e078 | 2009-11-23 20:04:44 +0000 | [diff] [blame] | 880 | return "MemberPointerToBoolean"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 881 | case CK_AnyPointerToObjCPointerCast: |
Fariborz Jahanian | 4cbf9d4 | 2009-12-08 23:46:15 +0000 | [diff] [blame] | 882 | return "AnyPointerToObjCPointerCast"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 883 | case CK_AnyPointerToBlockPointerCast: |
Fariborz Jahanian | 3b27f1a | 2009-12-11 22:40:48 +0000 | [diff] [blame] | 884 | return "AnyPointerToBlockPointerCast"; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 885 | case CK_ObjCObjectLValueCast: |
Douglas Gregor | 569c316 | 2010-08-07 11:51:51 +0000 | [diff] [blame] | 886 | return "ObjCObjectLValueCast"; |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 887 | case CK_FloatingRealToComplex: |
| 888 | return "FloatingRealToComplex"; |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 889 | case CK_FloatingComplexToReal: |
| 890 | return "FloatingComplexToReal"; |
| 891 | case CK_FloatingComplexToBoolean: |
| 892 | return "FloatingComplexToBoolean"; |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 893 | case CK_FloatingComplexCast: |
| 894 | return "FloatingComplexCast"; |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 895 | case CK_FloatingComplexToIntegralComplex: |
| 896 | return "FloatingComplexToIntegralComplex"; |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 897 | case CK_IntegralRealToComplex: |
| 898 | return "IntegralRealToComplex"; |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 899 | case CK_IntegralComplexToReal: |
| 900 | return "IntegralComplexToReal"; |
| 901 | case CK_IntegralComplexToBoolean: |
| 902 | return "IntegralComplexToBoolean"; |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 903 | case CK_IntegralComplexCast: |
| 904 | return "IntegralComplexCast"; |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 905 | case CK_IntegralComplexToFloatingComplex: |
| 906 | return "IntegralComplexToFloatingComplex"; |
Anders Carlsson | f8ec55a | 2009-09-03 00:59:21 +0000 | [diff] [blame] | 907 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 908 | |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 909 | llvm_unreachable("Unhandled cast kind!"); |
Anders Carlsson | f8ec55a | 2009-09-03 00:59:21 +0000 | [diff] [blame] | 910 | return 0; |
| 911 | } |
| 912 | |
Douglas Gregor | 6eef519 | 2009-12-14 19:27:10 +0000 | [diff] [blame] | 913 | Expr *CastExpr::getSubExprAsWritten() { |
| 914 | Expr *SubExpr = 0; |
| 915 | CastExpr *E = this; |
| 916 | do { |
| 917 | SubExpr = E->getSubExpr(); |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 918 | |
Douglas Gregor | 6eef519 | 2009-12-14 19:27:10 +0000 | [diff] [blame] | 919 | // Skip any temporary bindings; they're implicit. |
| 920 | if (CXXBindTemporaryExpr *Binder = dyn_cast<CXXBindTemporaryExpr>(SubExpr)) |
| 921 | SubExpr = Binder->getSubExpr(); |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 922 | |
Douglas Gregor | 6eef519 | 2009-12-14 19:27:10 +0000 | [diff] [blame] | 923 | // Conversions by constructor and conversion functions have a |
| 924 | // subexpression describing the call; strip it off. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 925 | if (E->getCastKind() == CK_ConstructorConversion) |
Douglas Gregor | 6eef519 | 2009-12-14 19:27:10 +0000 | [diff] [blame] | 926 | SubExpr = cast<CXXConstructExpr>(SubExpr)->getArg(0); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 927 | else if (E->getCastKind() == CK_UserDefinedConversion) |
Douglas Gregor | 6eef519 | 2009-12-14 19:27:10 +0000 | [diff] [blame] | 928 | SubExpr = cast<CXXMemberCallExpr>(SubExpr)->getImplicitObjectArgument(); |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 929 | |
Douglas Gregor | 6eef519 | 2009-12-14 19:27:10 +0000 | [diff] [blame] | 930 | // If the subexpression we're left with is an implicit cast, look |
| 931 | // through that, too. |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 932 | } while ((E = dyn_cast<ImplicitCastExpr>(SubExpr))); |
| 933 | |
Douglas Gregor | 6eef519 | 2009-12-14 19:27:10 +0000 | [diff] [blame] | 934 | return SubExpr; |
| 935 | } |
| 936 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 937 | CXXBaseSpecifier **CastExpr::path_buffer() { |
| 938 | switch (getStmtClass()) { |
| 939 | #define ABSTRACT_STMT(x) |
| 940 | #define CASTEXPR(Type, Base) \ |
| 941 | case Stmt::Type##Class: \ |
| 942 | return reinterpret_cast<CXXBaseSpecifier**>(static_cast<Type*>(this)+1); |
| 943 | #define STMT(Type, Base) |
| 944 | #include "clang/AST/StmtNodes.inc" |
| 945 | default: |
| 946 | llvm_unreachable("non-cast expressions not possible here"); |
| 947 | return 0; |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | void CastExpr::setCastPath(const CXXCastPath &Path) { |
| 952 | assert(Path.size() == path_size()); |
| 953 | memcpy(path_buffer(), Path.data(), Path.size() * sizeof(CXXBaseSpecifier*)); |
| 954 | } |
| 955 | |
| 956 | ImplicitCastExpr *ImplicitCastExpr::Create(ASTContext &C, QualType T, |
| 957 | CastKind Kind, Expr *Operand, |
| 958 | const CXXCastPath *BasePath, |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 959 | ExprValueKind VK) { |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 960 | unsigned PathSize = (BasePath ? BasePath->size() : 0); |
| 961 | void *Buffer = |
| 962 | C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*)); |
| 963 | ImplicitCastExpr *E = |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 964 | new (Buffer) ImplicitCastExpr(T, Kind, Operand, PathSize, VK); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 965 | if (PathSize) E->setCastPath(*BasePath); |
| 966 | return E; |
| 967 | } |
| 968 | |
| 969 | ImplicitCastExpr *ImplicitCastExpr::CreateEmpty(ASTContext &C, |
| 970 | unsigned PathSize) { |
| 971 | void *Buffer = |
| 972 | C.Allocate(sizeof(ImplicitCastExpr) + PathSize * sizeof(CXXBaseSpecifier*)); |
| 973 | return new (Buffer) ImplicitCastExpr(EmptyShell(), PathSize); |
| 974 | } |
| 975 | |
| 976 | |
| 977 | CStyleCastExpr *CStyleCastExpr::Create(ASTContext &C, QualType T, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 978 | ExprValueKind VK, CastKind K, Expr *Op, |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 979 | const CXXCastPath *BasePath, |
| 980 | TypeSourceInfo *WrittenTy, |
| 981 | SourceLocation L, SourceLocation R) { |
| 982 | unsigned PathSize = (BasePath ? BasePath->size() : 0); |
| 983 | void *Buffer = |
| 984 | C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*)); |
| 985 | CStyleCastExpr *E = |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 986 | new (Buffer) CStyleCastExpr(T, VK, K, Op, PathSize, WrittenTy, L, R); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 987 | if (PathSize) E->setCastPath(*BasePath); |
| 988 | return E; |
| 989 | } |
| 990 | |
| 991 | CStyleCastExpr *CStyleCastExpr::CreateEmpty(ASTContext &C, unsigned PathSize) { |
| 992 | void *Buffer = |
| 993 | C.Allocate(sizeof(CStyleCastExpr) + PathSize * sizeof(CXXBaseSpecifier*)); |
| 994 | return new (Buffer) CStyleCastExpr(EmptyShell(), PathSize); |
| 995 | } |
| 996 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 997 | /// getOpcodeStr - Turn an Opcode enum value into the punctuation char it |
| 998 | /// corresponds to, e.g. "<<=". |
| 999 | const char *BinaryOperator::getOpcodeStr(Opcode Op) { |
| 1000 | switch (Op) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1001 | case BO_PtrMemD: return ".*"; |
| 1002 | case BO_PtrMemI: return "->*"; |
| 1003 | case BO_Mul: return "*"; |
| 1004 | case BO_Div: return "/"; |
| 1005 | case BO_Rem: return "%"; |
| 1006 | case BO_Add: return "+"; |
| 1007 | case BO_Sub: return "-"; |
| 1008 | case BO_Shl: return "<<"; |
| 1009 | case BO_Shr: return ">>"; |
| 1010 | case BO_LT: return "<"; |
| 1011 | case BO_GT: return ">"; |
| 1012 | case BO_LE: return "<="; |
| 1013 | case BO_GE: return ">="; |
| 1014 | case BO_EQ: return "=="; |
| 1015 | case BO_NE: return "!="; |
| 1016 | case BO_And: return "&"; |
| 1017 | case BO_Xor: return "^"; |
| 1018 | case BO_Or: return "|"; |
| 1019 | case BO_LAnd: return "&&"; |
| 1020 | case BO_LOr: return "||"; |
| 1021 | case BO_Assign: return "="; |
| 1022 | case BO_MulAssign: return "*="; |
| 1023 | case BO_DivAssign: return "/="; |
| 1024 | case BO_RemAssign: return "%="; |
| 1025 | case BO_AddAssign: return "+="; |
| 1026 | case BO_SubAssign: return "-="; |
| 1027 | case BO_ShlAssign: return "<<="; |
| 1028 | case BO_ShrAssign: return ">>="; |
| 1029 | case BO_AndAssign: return "&="; |
| 1030 | case BO_XorAssign: return "^="; |
| 1031 | case BO_OrAssign: return "|="; |
| 1032 | case BO_Comma: return ","; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1033 | } |
Douglas Gregor | baf5348 | 2009-03-12 22:51:37 +0000 | [diff] [blame] | 1034 | |
| 1035 | return ""; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1036 | } |
| 1037 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1038 | BinaryOperatorKind |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 1039 | BinaryOperator::getOverloadedOpcode(OverloadedOperatorKind OO) { |
| 1040 | switch (OO) { |
Chris Lattner | b7beee9 | 2009-03-22 00:10:22 +0000 | [diff] [blame] | 1041 | default: assert(false && "Not an overloadable binary operator"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1042 | case OO_Plus: return BO_Add; |
| 1043 | case OO_Minus: return BO_Sub; |
| 1044 | case OO_Star: return BO_Mul; |
| 1045 | case OO_Slash: return BO_Div; |
| 1046 | case OO_Percent: return BO_Rem; |
| 1047 | case OO_Caret: return BO_Xor; |
| 1048 | case OO_Amp: return BO_And; |
| 1049 | case OO_Pipe: return BO_Or; |
| 1050 | case OO_Equal: return BO_Assign; |
| 1051 | case OO_Less: return BO_LT; |
| 1052 | case OO_Greater: return BO_GT; |
| 1053 | case OO_PlusEqual: return BO_AddAssign; |
| 1054 | case OO_MinusEqual: return BO_SubAssign; |
| 1055 | case OO_StarEqual: return BO_MulAssign; |
| 1056 | case OO_SlashEqual: return BO_DivAssign; |
| 1057 | case OO_PercentEqual: return BO_RemAssign; |
| 1058 | case OO_CaretEqual: return BO_XorAssign; |
| 1059 | case OO_AmpEqual: return BO_AndAssign; |
| 1060 | case OO_PipeEqual: return BO_OrAssign; |
| 1061 | case OO_LessLess: return BO_Shl; |
| 1062 | case OO_GreaterGreater: return BO_Shr; |
| 1063 | case OO_LessLessEqual: return BO_ShlAssign; |
| 1064 | case OO_GreaterGreaterEqual: return BO_ShrAssign; |
| 1065 | case OO_EqualEqual: return BO_EQ; |
| 1066 | case OO_ExclaimEqual: return BO_NE; |
| 1067 | case OO_LessEqual: return BO_LE; |
| 1068 | case OO_GreaterEqual: return BO_GE; |
| 1069 | case OO_AmpAmp: return BO_LAnd; |
| 1070 | case OO_PipePipe: return BO_LOr; |
| 1071 | case OO_Comma: return BO_Comma; |
| 1072 | case OO_ArrowStar: return BO_PtrMemI; |
Douglas Gregor | 063daf6 | 2009-03-13 18:40:31 +0000 | [diff] [blame] | 1073 | } |
| 1074 | } |
| 1075 | |
| 1076 | OverloadedOperatorKind BinaryOperator::getOverloadedOperator(Opcode Opc) { |
| 1077 | static const OverloadedOperatorKind OverOps[] = { |
| 1078 | /* .* Cannot be overloaded */OO_None, OO_ArrowStar, |
| 1079 | OO_Star, OO_Slash, OO_Percent, |
| 1080 | OO_Plus, OO_Minus, |
| 1081 | OO_LessLess, OO_GreaterGreater, |
| 1082 | OO_Less, OO_Greater, OO_LessEqual, OO_GreaterEqual, |
| 1083 | OO_EqualEqual, OO_ExclaimEqual, |
| 1084 | OO_Amp, |
| 1085 | OO_Caret, |
| 1086 | OO_Pipe, |
| 1087 | OO_AmpAmp, |
| 1088 | OO_PipePipe, |
| 1089 | OO_Equal, OO_StarEqual, |
| 1090 | OO_SlashEqual, OO_PercentEqual, |
| 1091 | OO_PlusEqual, OO_MinusEqual, |
| 1092 | OO_LessLessEqual, OO_GreaterGreaterEqual, |
| 1093 | OO_AmpEqual, OO_CaretEqual, |
| 1094 | OO_PipeEqual, |
| 1095 | OO_Comma |
| 1096 | }; |
| 1097 | return OverOps[Opc]; |
| 1098 | } |
| 1099 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1100 | InitListExpr::InitListExpr(ASTContext &C, SourceLocation lbraceloc, |
Chris Lattner | 418f6c7 | 2008-10-26 23:43:26 +0000 | [diff] [blame] | 1101 | Expr **initExprs, unsigned numInits, |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1102 | SourceLocation rbraceloc) |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1103 | : Expr(InitListExprClass, QualType(), VK_RValue, OK_Ordinary, false, false), |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1104 | InitExprs(C, numInits), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1105 | LBraceLoc(lbraceloc), RBraceLoc(rbraceloc), SyntacticForm(0), |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 1106 | UnionFieldInit(0), HadArrayRangeDesignator(false) |
| 1107 | { |
Ted Kremenek | ba7bc55 | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 1108 | for (unsigned I = 0; I != numInits; ++I) { |
| 1109 | if (initExprs[I]->isTypeDependent()) |
John McCall | 8e6285a | 2010-10-26 08:39:16 +0000 | [diff] [blame] | 1110 | ExprBits.TypeDependent = true; |
Ted Kremenek | ba7bc55 | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 1111 | if (initExprs[I]->isValueDependent()) |
John McCall | 8e6285a | 2010-10-26 08:39:16 +0000 | [diff] [blame] | 1112 | ExprBits.ValueDependent = true; |
Douglas Gregor | 73460a3 | 2009-11-19 23:25:22 +0000 | [diff] [blame] | 1113 | } |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 1114 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1115 | InitExprs.insert(C, InitExprs.end(), initExprs, initExprs+numInits); |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 1116 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1117 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1118 | void InitListExpr::reserveInits(ASTContext &C, unsigned NumInits) { |
Ted Kremenek | ba7bc55 | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 1119 | if (NumInits > InitExprs.size()) |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1120 | InitExprs.reserve(C, NumInits); |
Douglas Gregor | fa21920 | 2009-03-20 23:58:33 +0000 | [diff] [blame] | 1121 | } |
| 1122 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1123 | void InitListExpr::resizeInits(ASTContext &C, unsigned NumInits) { |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1124 | InitExprs.resize(C, NumInits, 0); |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1127 | Expr *InitListExpr::updateInit(ASTContext &C, unsigned Init, Expr *expr) { |
Ted Kremenek | ba7bc55 | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 1128 | if (Init >= InitExprs.size()) { |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 1129 | InitExprs.insert(C, InitExprs.end(), Init - InitExprs.size() + 1, 0); |
Ted Kremenek | ba7bc55 | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 1130 | InitExprs.back() = expr; |
| 1131 | return 0; |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1132 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1133 | |
Douglas Gregor | 4c67834 | 2009-01-28 21:54:33 +0000 | [diff] [blame] | 1134 | Expr *Result = cast_or_null<Expr>(InitExprs[Init]); |
| 1135 | InitExprs[Init] = expr; |
| 1136 | return Result; |
| 1137 | } |
| 1138 | |
Ted Kremenek | c4ba51f | 2010-11-09 02:11:40 +0000 | [diff] [blame] | 1139 | SourceRange InitListExpr::getSourceRange() const { |
| 1140 | if (SyntacticForm) |
| 1141 | return SyntacticForm->getSourceRange(); |
| 1142 | SourceLocation Beg = LBraceLoc, End = RBraceLoc; |
| 1143 | if (Beg.isInvalid()) { |
| 1144 | // Find the first non-null initializer. |
| 1145 | for (InitExprsTy::const_iterator I = InitExprs.begin(), |
| 1146 | E = InitExprs.end(); |
| 1147 | I != E; ++I) { |
| 1148 | if (Stmt *S = *I) { |
| 1149 | Beg = S->getLocStart(); |
| 1150 | break; |
| 1151 | } |
| 1152 | } |
| 1153 | } |
| 1154 | if (End.isInvalid()) { |
| 1155 | // Find the first non-null initializer from the end. |
| 1156 | for (InitExprsTy::const_reverse_iterator I = InitExprs.rbegin(), |
| 1157 | E = InitExprs.rend(); |
| 1158 | I != E; ++I) { |
| 1159 | if (Stmt *S = *I) { |
| 1160 | End = S->getSourceRange().getEnd(); |
| 1161 | break; |
| 1162 | } |
| 1163 | } |
| 1164 | } |
| 1165 | return SourceRange(Beg, End); |
| 1166 | } |
| 1167 | |
Steve Naroff | bfdcae6 | 2008-09-04 15:31:07 +0000 | [diff] [blame] | 1168 | /// getFunctionType - Return the underlying function type for this block. |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 1169 | /// |
| 1170 | const FunctionType *BlockExpr::getFunctionType() const { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 1171 | return getType()->getAs<BlockPointerType>()-> |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 1172 | getPointeeType()->getAs<FunctionType>(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 1173 | } |
| 1174 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1175 | SourceLocation BlockExpr::getCaretLocation() const { |
| 1176 | return TheBlock->getCaretLocation(); |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 1177 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1178 | const Stmt *BlockExpr::getBody() const { |
Douglas Gregor | 7297134 | 2009-04-18 00:02:19 +0000 | [diff] [blame] | 1179 | return TheBlock->getBody(); |
| 1180 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1181 | Stmt *BlockExpr::getBody() { |
| 1182 | return TheBlock->getBody(); |
Douglas Gregor | 7297134 | 2009-04-18 00:02:19 +0000 | [diff] [blame] | 1183 | } |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 1184 | |
| 1185 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1186 | //===----------------------------------------------------------------------===// |
| 1187 | // Generic Expression Routines |
| 1188 | //===----------------------------------------------------------------------===// |
| 1189 | |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1190 | /// isUnusedResultAWarning - Return true if this immediate expression should |
| 1191 | /// be warned about if the result is unused. If so, fill in Loc and Ranges |
| 1192 | /// with location to warn on and the source range[s] to report with the |
| 1193 | /// warning. |
| 1194 | bool Expr::isUnusedResultAWarning(SourceLocation &Loc, SourceRange &R1, |
Mike Stump | df317bf | 2009-11-03 23:25:48 +0000 | [diff] [blame] | 1195 | SourceRange &R2, ASTContext &Ctx) const { |
Anders Carlsson | ffce2df | 2009-05-15 23:10:19 +0000 | [diff] [blame] | 1196 | // Don't warn if the expr is type dependent. The type could end up |
| 1197 | // instantiating to void. |
| 1198 | if (isTypeDependent()) |
| 1199 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1200 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1201 | switch (getStmtClass()) { |
| 1202 | default: |
John McCall | 0faede6 | 2010-03-12 07:11:26 +0000 | [diff] [blame] | 1203 | if (getType()->isVoidType()) |
| 1204 | return false; |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1205 | Loc = getExprLoc(); |
| 1206 | R1 = getSourceRange(); |
| 1207 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1208 | case ParenExprClass: |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1209 | return cast<ParenExpr>(this)->getSubExpr()-> |
Mike Stump | df317bf | 2009-11-03 23:25:48 +0000 | [diff] [blame] | 1210 | isUnusedResultAWarning(Loc, R1, R2, Ctx); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1211 | case UnaryOperatorClass: { |
| 1212 | const UnaryOperator *UO = cast<UnaryOperator>(this); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1213 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1214 | switch (UO->getOpcode()) { |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1215 | default: break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1216 | case UO_PostInc: |
| 1217 | case UO_PostDec: |
| 1218 | case UO_PreInc: |
| 1219 | case UO_PreDec: // ++/-- |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1220 | return false; // Not a warning. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1221 | case UO_Deref: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1222 | // Dereferencing a volatile pointer is a side-effect. |
Mike Stump | df317bf | 2009-11-03 23:25:48 +0000 | [diff] [blame] | 1223 | if (Ctx.getCanonicalType(getType()).isVolatileQualified()) |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1224 | return false; |
| 1225 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1226 | case UO_Real: |
| 1227 | case UO_Imag: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1228 | // accessing a piece of a volatile complex is a side-effect. |
Mike Stump | df317bf | 2009-11-03 23:25:48 +0000 | [diff] [blame] | 1229 | if (Ctx.getCanonicalType(UO->getSubExpr()->getType()) |
| 1230 | .isVolatileQualified()) |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1231 | return false; |
| 1232 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1233 | case UO_Extension: |
Mike Stump | df317bf | 2009-11-03 23:25:48 +0000 | [diff] [blame] | 1234 | return UO->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1235 | } |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1236 | Loc = UO->getOperatorLoc(); |
| 1237 | R1 = UO->getSubExpr()->getSourceRange(); |
| 1238 | return true; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1239 | } |
Chris Lattner | e7716e6 | 2007-12-01 06:07:34 +0000 | [diff] [blame] | 1240 | case BinaryOperatorClass: { |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1241 | const BinaryOperator *BO = cast<BinaryOperator>(this); |
Ted Kremenek | c46a246 | 2010-04-07 18:49:21 +0000 | [diff] [blame] | 1242 | switch (BO->getOpcode()) { |
| 1243 | default: |
| 1244 | break; |
Argyrios Kyrtzidis | 2597345 | 2010-06-30 10:53:14 +0000 | [diff] [blame] | 1245 | // Consider the RHS of comma for side effects. LHS was checked by |
| 1246 | // Sema::CheckCommaOperands. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1247 | case BO_Comma: |
Ted Kremenek | c46a246 | 2010-04-07 18:49:21 +0000 | [diff] [blame] | 1248 | // ((foo = <blah>), 0) is an idiom for hiding the result (and |
| 1249 | // lvalue-ness) of an assignment written in a macro. |
| 1250 | if (IntegerLiteral *IE = |
| 1251 | dyn_cast<IntegerLiteral>(BO->getRHS()->IgnoreParens())) |
| 1252 | if (IE->getValue() == 0) |
| 1253 | return false; |
Argyrios Kyrtzidis | 2597345 | 2010-06-30 10:53:14 +0000 | [diff] [blame] | 1254 | return BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx); |
| 1255 | // Consider '||', '&&' to have side effects if the LHS or RHS does. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1256 | case BO_LAnd: |
| 1257 | case BO_LOr: |
Argyrios Kyrtzidis | 2597345 | 2010-06-30 10:53:14 +0000 | [diff] [blame] | 1258 | if (!BO->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx) || |
| 1259 | !BO->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx)) |
| 1260 | return false; |
| 1261 | break; |
John McCall | bf0ee35 | 2010-02-16 04:10:53 +0000 | [diff] [blame] | 1262 | } |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1263 | if (BO->isAssignmentOp()) |
| 1264 | return false; |
| 1265 | Loc = BO->getOperatorLoc(); |
| 1266 | R1 = BO->getLHS()->getSourceRange(); |
| 1267 | R2 = BO->getRHS()->getSourceRange(); |
| 1268 | return true; |
Chris Lattner | e7716e6 | 2007-12-01 06:07:34 +0000 | [diff] [blame] | 1269 | } |
Chris Lattner | eb14fe8 | 2007-08-25 02:00:02 +0000 | [diff] [blame] | 1270 | case CompoundAssignOperatorClass: |
Douglas Gregor | c6dfe19 | 2010-05-08 22:41:50 +0000 | [diff] [blame] | 1271 | case VAArgExprClass: |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1272 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1273 | |
Fariborz Jahanian | ab38e4b | 2007-12-01 19:58:28 +0000 | [diff] [blame] | 1274 | case ConditionalOperatorClass: { |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1275 | // The condition must be evaluated, but if either the LHS or RHS is a |
| 1276 | // warning, warn about them. |
Fariborz Jahanian | ab38e4b | 2007-12-01 19:58:28 +0000 | [diff] [blame] | 1277 | const ConditionalOperator *Exp = cast<ConditionalOperator>(this); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1278 | if (Exp->getLHS() && |
Mike Stump | df317bf | 2009-11-03 23:25:48 +0000 | [diff] [blame] | 1279 | Exp->getLHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx)) |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1280 | return true; |
Mike Stump | df317bf | 2009-11-03 23:25:48 +0000 | [diff] [blame] | 1281 | return Exp->getRHS()->isUnusedResultAWarning(Loc, R1, R2, Ctx); |
Fariborz Jahanian | ab38e4b | 2007-12-01 19:58:28 +0000 | [diff] [blame] | 1282 | } |
| 1283 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1284 | case MemberExprClass: |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1285 | // If the base pointer or element is to a volatile pointer/field, accessing |
| 1286 | // it is a side effect. |
Mike Stump | df317bf | 2009-11-03 23:25:48 +0000 | [diff] [blame] | 1287 | if (Ctx.getCanonicalType(getType()).isVolatileQualified()) |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1288 | return false; |
| 1289 | Loc = cast<MemberExpr>(this)->getMemberLoc(); |
| 1290 | R1 = SourceRange(Loc, Loc); |
| 1291 | R2 = cast<MemberExpr>(this)->getBase()->getSourceRange(); |
| 1292 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1293 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1294 | case ArraySubscriptExprClass: |
| 1295 | // 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] | 1296 | // it is a side effect. |
Mike Stump | df317bf | 2009-11-03 23:25:48 +0000 | [diff] [blame] | 1297 | if (Ctx.getCanonicalType(getType()).isVolatileQualified()) |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1298 | return false; |
| 1299 | Loc = cast<ArraySubscriptExpr>(this)->getRBracketLoc(); |
| 1300 | R1 = cast<ArraySubscriptExpr>(this)->getLHS()->getSourceRange(); |
| 1301 | R2 = cast<ArraySubscriptExpr>(this)->getRHS()->getSourceRange(); |
| 1302 | return true; |
Eli Friedman | 211f6ad | 2008-05-27 15:24:04 +0000 | [diff] [blame] | 1303 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1304 | case CallExprClass: |
Eli Friedman | 852871a | 2009-04-29 16:35:53 +0000 | [diff] [blame] | 1305 | case CXXOperatorCallExprClass: |
| 1306 | case CXXMemberCallExprClass: { |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1307 | // If this is a direct call, get the callee. |
| 1308 | const CallExpr *CE = cast<CallExpr>(this); |
Nuno Lopes | d20254f | 2009-12-20 23:11:08 +0000 | [diff] [blame] | 1309 | if (const Decl *FD = CE->getCalleeDecl()) { |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1310 | // If the callee has attribute pure, const, or warn_unused_result, warn |
| 1311 | // about it. void foo() { strlen("bar"); } should warn. |
Chris Lattner | bc8d42c | 2009-10-13 04:53:48 +0000 | [diff] [blame] | 1312 | // |
| 1313 | // Note: If new cases are added here, DiagnoseUnusedExprResult should be |
| 1314 | // updated to match for QoI. |
| 1315 | if (FD->getAttr<WarnUnusedResultAttr>() || |
| 1316 | FD->getAttr<PureAttr>() || FD->getAttr<ConstAttr>()) { |
| 1317 | Loc = CE->getCallee()->getLocStart(); |
| 1318 | R1 = CE->getCallee()->getSourceRange(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1319 | |
Chris Lattner | bc8d42c | 2009-10-13 04:53:48 +0000 | [diff] [blame] | 1320 | if (unsigned NumArgs = CE->getNumArgs()) |
| 1321 | R2 = SourceRange(CE->getArg(0)->getLocStart(), |
| 1322 | CE->getArg(NumArgs-1)->getLocEnd()); |
| 1323 | return true; |
| 1324 | } |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1325 | } |
| 1326 | return false; |
| 1327 | } |
Anders Carlsson | 58beed9 | 2009-11-17 17:11:23 +0000 | [diff] [blame] | 1328 | |
| 1329 | case CXXTemporaryObjectExprClass: |
| 1330 | case CXXConstructExprClass: |
| 1331 | return false; |
| 1332 | |
Fariborz Jahanian | f031774 | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 1333 | case ObjCMessageExprClass: { |
| 1334 | const ObjCMessageExpr *ME = cast<ObjCMessageExpr>(this); |
| 1335 | const ObjCMethodDecl *MD = ME->getMethodDecl(); |
| 1336 | if (MD && MD->getAttr<WarnUnusedResultAttr>()) { |
| 1337 | Loc = getExprLoc(); |
| 1338 | return true; |
| 1339 | } |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1340 | return false; |
Fariborz Jahanian | f031774 | 2010-03-30 18:22:15 +0000 | [diff] [blame] | 1341 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1342 | |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 1343 | case ObjCPropertyRefExprClass: |
Chris Lattner | 5e94a0d | 2009-08-16 16:51:50 +0000 | [diff] [blame] | 1344 | Loc = getExprLoc(); |
| 1345 | R1 = getSourceRange(); |
Chris Lattner | a50089e | 2009-08-16 16:45:18 +0000 | [diff] [blame] | 1346 | return true; |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 1347 | |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 1348 | case StmtExprClass: { |
| 1349 | // Statement exprs don't logically have side effects themselves, but are |
| 1350 | // sometimes used in macros in ways that give them a type that is unused. |
| 1351 | // For example ({ blah; foo(); }) will end up with a type if foo has a type. |
| 1352 | // however, if the result of the stmt expr is dead, we don't want to emit a |
| 1353 | // warning. |
| 1354 | const CompoundStmt *CS = cast<StmtExpr>(this)->getSubStmt(); |
Argyrios Kyrtzidis | d2827af | 2010-09-19 21:21:10 +0000 | [diff] [blame] | 1355 | if (!CS->body_empty()) { |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 1356 | if (const Expr *E = dyn_cast<Expr>(CS->body_back())) |
Mike Stump | df317bf | 2009-11-03 23:25:48 +0000 | [diff] [blame] | 1357 | return E->isUnusedResultAWarning(Loc, R1, R2, Ctx); |
Argyrios Kyrtzidis | d2827af | 2010-09-19 21:21:10 +0000 | [diff] [blame] | 1358 | if (const LabelStmt *Label = dyn_cast<LabelStmt>(CS->body_back())) |
| 1359 | if (const Expr *E = dyn_cast<Expr>(Label->getSubStmt())) |
| 1360 | return E->isUnusedResultAWarning(Loc, R1, R2, Ctx); |
| 1361 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1362 | |
John McCall | 0faede6 | 2010-03-12 07:11:26 +0000 | [diff] [blame] | 1363 | if (getType()->isVoidType()) |
| 1364 | return false; |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1365 | Loc = cast<StmtExpr>(this)->getLParenLoc(); |
| 1366 | R1 = getSourceRange(); |
| 1367 | return true; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 1368 | } |
Douglas Gregor | 6eec8e8 | 2008-10-28 15:36:24 +0000 | [diff] [blame] | 1369 | case CStyleCastExprClass: |
Chris Lattner | fb84664 | 2009-07-28 18:25:28 +0000 | [diff] [blame] | 1370 | // If this is an explicit cast to void, allow it. People do this when they |
| 1371 | // think they know what they're doing :). |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1372 | if (getType()->isVoidType()) |
Chris Lattner | fb84664 | 2009-07-28 18:25:28 +0000 | [diff] [blame] | 1373 | return false; |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1374 | Loc = cast<CStyleCastExpr>(this)->getLParenLoc(); |
| 1375 | R1 = cast<CStyleCastExpr>(this)->getSubExpr()->getSourceRange(); |
| 1376 | return true; |
Anders Carlsson | 58beed9 | 2009-11-17 17:11:23 +0000 | [diff] [blame] | 1377 | case CXXFunctionalCastExprClass: { |
John McCall | 0faede6 | 2010-03-12 07:11:26 +0000 | [diff] [blame] | 1378 | if (getType()->isVoidType()) |
| 1379 | return false; |
Anders Carlsson | 58beed9 | 2009-11-17 17:11:23 +0000 | [diff] [blame] | 1380 | const CastExpr *CE = cast<CastExpr>(this); |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 1381 | |
Anders Carlsson | 58beed9 | 2009-11-17 17:11:23 +0000 | [diff] [blame] | 1382 | // If this is a cast to void or a constructor conversion, check the operand. |
| 1383 | // Otherwise, the result of the cast is unused. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1384 | if (CE->getCastKind() == CK_ToVoid || |
| 1385 | CE->getCastKind() == CK_ConstructorConversion) |
Mike Stump | df317bf | 2009-11-03 23:25:48 +0000 | [diff] [blame] | 1386 | return (cast<CastExpr>(this)->getSubExpr() |
| 1387 | ->isUnusedResultAWarning(Loc, R1, R2, Ctx)); |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1388 | Loc = cast<CXXFunctionalCastExpr>(this)->getTypeBeginLoc(); |
| 1389 | R1 = cast<CXXFunctionalCastExpr>(this)->getSubExpr()->getSourceRange(); |
| 1390 | return true; |
Anders Carlsson | 58beed9 | 2009-11-17 17:11:23 +0000 | [diff] [blame] | 1391 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1392 | |
Eli Friedman | 4be1f47 | 2008-05-19 21:24:43 +0000 | [diff] [blame] | 1393 | case ImplicitCastExprClass: |
| 1394 | // Check the operand, since implicit casts are inserted by Sema |
Mike Stump | df317bf | 2009-11-03 23:25:48 +0000 | [diff] [blame] | 1395 | return (cast<ImplicitCastExpr>(this) |
| 1396 | ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx)); |
Eli Friedman | 4be1f47 | 2008-05-19 21:24:43 +0000 | [diff] [blame] | 1397 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 1398 | case CXXDefaultArgExprClass: |
Mike Stump | df317bf | 2009-11-03 23:25:48 +0000 | [diff] [blame] | 1399 | return (cast<CXXDefaultArgExpr>(this) |
| 1400 | ->getExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx)); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1401 | |
| 1402 | case CXXNewExprClass: |
| 1403 | // FIXME: In theory, there might be new expressions that don't have side |
| 1404 | // effects (e.g. a placement new with an uninitialized POD). |
| 1405 | case CXXDeleteExprClass: |
Chris Lattner | 026dc96 | 2009-02-14 07:37:35 +0000 | [diff] [blame] | 1406 | return false; |
Anders Carlsson | 2d46eb2 | 2009-08-16 04:11:06 +0000 | [diff] [blame] | 1407 | case CXXBindTemporaryExprClass: |
Mike Stump | df317bf | 2009-11-03 23:25:48 +0000 | [diff] [blame] | 1408 | return (cast<CXXBindTemporaryExpr>(this) |
| 1409 | ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx)); |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame^] | 1410 | case ExprWithCleanupsClass: |
| 1411 | return (cast<ExprWithCleanups>(this) |
Mike Stump | df317bf | 2009-11-03 23:25:48 +0000 | [diff] [blame] | 1412 | ->getSubExpr()->isUnusedResultAWarning(Loc, R1, R2, Ctx)); |
Sebastian Redl | 4c5d320 | 2008-11-21 19:14:01 +0000 | [diff] [blame] | 1413 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1414 | } |
| 1415 | |
Fariborz Jahanian | 44baa8a | 2009-02-22 18:40:18 +0000 | [diff] [blame] | 1416 | /// isOBJCGCCandidate - Check if an expression is objc gc'able. |
Fariborz Jahanian | 7f4f86a | 2009-09-08 23:38:54 +0000 | [diff] [blame] | 1417 | /// returns true, if it is; false otherwise. |
Fariborz Jahanian | 102e390 | 2009-06-01 21:29:32 +0000 | [diff] [blame] | 1418 | bool Expr::isOBJCGCCandidate(ASTContext &Ctx) const { |
Fariborz Jahanian | 44baa8a | 2009-02-22 18:40:18 +0000 | [diff] [blame] | 1419 | switch (getStmtClass()) { |
| 1420 | default: |
| 1421 | return false; |
| 1422 | case ObjCIvarRefExprClass: |
| 1423 | return true; |
Fariborz Jahanian | 207c521 | 2009-02-23 18:59:50 +0000 | [diff] [blame] | 1424 | case Expr::UnaryOperatorClass: |
Fariborz Jahanian | 102e390 | 2009-06-01 21:29:32 +0000 | [diff] [blame] | 1425 | return cast<UnaryOperator>(this)->getSubExpr()->isOBJCGCCandidate(Ctx); |
Fariborz Jahanian | 44baa8a | 2009-02-22 18:40:18 +0000 | [diff] [blame] | 1426 | case ParenExprClass: |
Fariborz Jahanian | 102e390 | 2009-06-01 21:29:32 +0000 | [diff] [blame] | 1427 | return cast<ParenExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx); |
Fariborz Jahanian | 44baa8a | 2009-02-22 18:40:18 +0000 | [diff] [blame] | 1428 | case ImplicitCastExprClass: |
Fariborz Jahanian | 102e390 | 2009-06-01 21:29:32 +0000 | [diff] [blame] | 1429 | return cast<ImplicitCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx); |
Fariborz Jahanian | 06b8912 | 2009-05-05 23:28:21 +0000 | [diff] [blame] | 1430 | case CStyleCastExprClass: |
Fariborz Jahanian | 102e390 | 2009-06-01 21:29:32 +0000 | [diff] [blame] | 1431 | return cast<CStyleCastExpr>(this)->getSubExpr()->isOBJCGCCandidate(Ctx); |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 1432 | case DeclRefExprClass: { |
Fariborz Jahanian | 44baa8a | 2009-02-22 18:40:18 +0000 | [diff] [blame] | 1433 | const Decl *D = cast<DeclRefExpr>(this)->getDecl(); |
Fariborz Jahanian | 102e390 | 2009-06-01 21:29:32 +0000 | [diff] [blame] | 1434 | if (const VarDecl *VD = dyn_cast<VarDecl>(D)) { |
| 1435 | if (VD->hasGlobalStorage()) |
| 1436 | return true; |
| 1437 | QualType T = VD->getType(); |
Fariborz Jahanian | 59a53fa | 2009-09-16 18:09:18 +0000 | [diff] [blame] | 1438 | // dereferencing to a pointer is always a gc'able candidate, |
| 1439 | // unless it is __weak. |
Daniel Dunbar | 7e88a60 | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 1440 | return T->isPointerType() && |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1441 | (Ctx.getObjCGCAttrKind(T) != Qualifiers::Weak); |
Fariborz Jahanian | 102e390 | 2009-06-01 21:29:32 +0000 | [diff] [blame] | 1442 | } |
Fariborz Jahanian | 44baa8a | 2009-02-22 18:40:18 +0000 | [diff] [blame] | 1443 | return false; |
| 1444 | } |
Douglas Gregor | 83f6faf | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 1445 | case MemberExprClass: { |
Fariborz Jahanian | 44baa8a | 2009-02-22 18:40:18 +0000 | [diff] [blame] | 1446 | const MemberExpr *M = cast<MemberExpr>(this); |
Fariborz Jahanian | 102e390 | 2009-06-01 21:29:32 +0000 | [diff] [blame] | 1447 | return M->getBase()->isOBJCGCCandidate(Ctx); |
Fariborz Jahanian | 44baa8a | 2009-02-22 18:40:18 +0000 | [diff] [blame] | 1448 | } |
| 1449 | case ArraySubscriptExprClass: |
Fariborz Jahanian | 102e390 | 2009-06-01 21:29:32 +0000 | [diff] [blame] | 1450 | return cast<ArraySubscriptExpr>(this)->getBase()->isOBJCGCCandidate(Ctx); |
Fariborz Jahanian | 44baa8a | 2009-02-22 18:40:18 +0000 | [diff] [blame] | 1451 | } |
| 1452 | } |
Sebastian Redl | 369e51f | 2010-09-10 20:55:33 +0000 | [diff] [blame] | 1453 | |
Argyrios Kyrtzidis | 11ab790 | 2010-11-01 18:49:26 +0000 | [diff] [blame] | 1454 | bool Expr::isBoundMemberFunction(ASTContext &Ctx) const { |
| 1455 | if (isTypeDependent()) |
| 1456 | return false; |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1457 | return ClassifyLValue(Ctx) == Expr::LV_MemberFunction; |
Argyrios Kyrtzidis | 11ab790 | 2010-11-01 18:49:26 +0000 | [diff] [blame] | 1458 | } |
| 1459 | |
Sebastian Redl | 369e51f | 2010-09-10 20:55:33 +0000 | [diff] [blame] | 1460 | static Expr::CanThrowResult MergeCanThrow(Expr::CanThrowResult CT1, |
| 1461 | Expr::CanThrowResult CT2) { |
| 1462 | // CanThrowResult constants are ordered so that the maximum is the correct |
| 1463 | // merge result. |
| 1464 | return CT1 > CT2 ? CT1 : CT2; |
| 1465 | } |
| 1466 | |
| 1467 | static Expr::CanThrowResult CanSubExprsThrow(ASTContext &C, const Expr *CE) { |
| 1468 | Expr *E = const_cast<Expr*>(CE); |
| 1469 | Expr::CanThrowResult R = Expr::CT_Cannot; |
| 1470 | for (Expr::child_iterator I = E->child_begin(), IE = E->child_end(); |
| 1471 | I != IE && R != Expr::CT_Can; ++I) { |
| 1472 | R = MergeCanThrow(R, cast<Expr>(*I)->CanThrow(C)); |
| 1473 | } |
| 1474 | return R; |
| 1475 | } |
| 1476 | |
| 1477 | static Expr::CanThrowResult CanCalleeThrow(const Decl *D, |
| 1478 | bool NullThrows = true) { |
| 1479 | if (!D) |
| 1480 | return NullThrows ? Expr::CT_Can : Expr::CT_Cannot; |
| 1481 | |
| 1482 | // See if we can get a function type from the decl somehow. |
| 1483 | const ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 1484 | if (!VD) // If we have no clue what we're calling, assume the worst. |
| 1485 | return Expr::CT_Can; |
| 1486 | |
Sebastian Redl | 5221d8f | 2010-09-10 22:34:40 +0000 | [diff] [blame] | 1487 | // As an extension, we assume that __attribute__((nothrow)) functions don't |
| 1488 | // throw. |
| 1489 | if (isa<FunctionDecl>(D) && D->hasAttr<NoThrowAttr>()) |
| 1490 | return Expr::CT_Cannot; |
| 1491 | |
Sebastian Redl | 369e51f | 2010-09-10 20:55:33 +0000 | [diff] [blame] | 1492 | QualType T = VD->getType(); |
| 1493 | const FunctionProtoType *FT; |
| 1494 | if ((FT = T->getAs<FunctionProtoType>())) { |
| 1495 | } else if (const PointerType *PT = T->getAs<PointerType>()) |
| 1496 | FT = PT->getPointeeType()->getAs<FunctionProtoType>(); |
| 1497 | else if (const ReferenceType *RT = T->getAs<ReferenceType>()) |
| 1498 | FT = RT->getPointeeType()->getAs<FunctionProtoType>(); |
| 1499 | else if (const MemberPointerType *MT = T->getAs<MemberPointerType>()) |
| 1500 | FT = MT->getPointeeType()->getAs<FunctionProtoType>(); |
| 1501 | else if (const BlockPointerType *BT = T->getAs<BlockPointerType>()) |
| 1502 | FT = BT->getPointeeType()->getAs<FunctionProtoType>(); |
| 1503 | |
| 1504 | if (!FT) |
| 1505 | return Expr::CT_Can; |
| 1506 | |
| 1507 | return FT->hasEmptyExceptionSpec() ? Expr::CT_Cannot : Expr::CT_Can; |
| 1508 | } |
| 1509 | |
| 1510 | static Expr::CanThrowResult CanDynamicCastThrow(const CXXDynamicCastExpr *DC) { |
| 1511 | if (DC->isTypeDependent()) |
| 1512 | return Expr::CT_Dependent; |
| 1513 | |
Sebastian Redl | 295995c | 2010-09-10 20:55:47 +0000 | [diff] [blame] | 1514 | if (!DC->getTypeAsWritten()->isReferenceType()) |
| 1515 | return Expr::CT_Cannot; |
| 1516 | |
Sebastian Redl | 369e51f | 2010-09-10 20:55:33 +0000 | [diff] [blame] | 1517 | return DC->getCastKind() == clang::CK_Dynamic? Expr::CT_Can : Expr::CT_Cannot; |
| 1518 | } |
| 1519 | |
| 1520 | static Expr::CanThrowResult CanTypeidThrow(ASTContext &C, |
| 1521 | const CXXTypeidExpr *DC) { |
| 1522 | if (DC->isTypeOperand()) |
| 1523 | return Expr::CT_Cannot; |
| 1524 | |
| 1525 | Expr *Op = DC->getExprOperand(); |
| 1526 | if (Op->isTypeDependent()) |
| 1527 | return Expr::CT_Dependent; |
| 1528 | |
| 1529 | const RecordType *RT = Op->getType()->getAs<RecordType>(); |
| 1530 | if (!RT) |
| 1531 | return Expr::CT_Cannot; |
| 1532 | |
| 1533 | if (!cast<CXXRecordDecl>(RT->getDecl())->isPolymorphic()) |
| 1534 | return Expr::CT_Cannot; |
| 1535 | |
| 1536 | if (Op->Classify(C).isPRValue()) |
| 1537 | return Expr::CT_Cannot; |
| 1538 | |
| 1539 | return Expr::CT_Can; |
| 1540 | } |
| 1541 | |
| 1542 | Expr::CanThrowResult Expr::CanThrow(ASTContext &C) const { |
| 1543 | // C++ [expr.unary.noexcept]p3: |
| 1544 | // [Can throw] if in a potentially-evaluated context the expression would |
| 1545 | // contain: |
| 1546 | switch (getStmtClass()) { |
| 1547 | case CXXThrowExprClass: |
| 1548 | // - a potentially evaluated throw-expression |
| 1549 | return CT_Can; |
| 1550 | |
| 1551 | case CXXDynamicCastExprClass: { |
| 1552 | // - a potentially evaluated dynamic_cast expression dynamic_cast<T>(v), |
| 1553 | // where T is a reference type, that requires a run-time check |
| 1554 | CanThrowResult CT = CanDynamicCastThrow(cast<CXXDynamicCastExpr>(this)); |
| 1555 | if (CT == CT_Can) |
| 1556 | return CT; |
| 1557 | return MergeCanThrow(CT, CanSubExprsThrow(C, this)); |
| 1558 | } |
| 1559 | |
| 1560 | case CXXTypeidExprClass: |
| 1561 | // - a potentially evaluated typeid expression applied to a glvalue |
| 1562 | // expression whose type is a polymorphic class type |
| 1563 | return CanTypeidThrow(C, cast<CXXTypeidExpr>(this)); |
| 1564 | |
| 1565 | // - a potentially evaluated call to a function, member function, function |
| 1566 | // pointer, or member function pointer that does not have a non-throwing |
| 1567 | // exception-specification |
| 1568 | case CallExprClass: |
| 1569 | case CXXOperatorCallExprClass: |
| 1570 | case CXXMemberCallExprClass: { |
| 1571 | CanThrowResult CT = CanCalleeThrow(cast<CallExpr>(this)->getCalleeDecl()); |
| 1572 | if (CT == CT_Can) |
| 1573 | return CT; |
| 1574 | return MergeCanThrow(CT, CanSubExprsThrow(C, this)); |
| 1575 | } |
| 1576 | |
Sebastian Redl | 295995c | 2010-09-10 20:55:47 +0000 | [diff] [blame] | 1577 | case CXXConstructExprClass: |
| 1578 | case CXXTemporaryObjectExprClass: { |
Sebastian Redl | 369e51f | 2010-09-10 20:55:33 +0000 | [diff] [blame] | 1579 | CanThrowResult CT = CanCalleeThrow( |
| 1580 | cast<CXXConstructExpr>(this)->getConstructor()); |
| 1581 | if (CT == CT_Can) |
| 1582 | return CT; |
| 1583 | return MergeCanThrow(CT, CanSubExprsThrow(C, this)); |
| 1584 | } |
| 1585 | |
| 1586 | case CXXNewExprClass: { |
| 1587 | CanThrowResult CT = MergeCanThrow( |
| 1588 | CanCalleeThrow(cast<CXXNewExpr>(this)->getOperatorNew()), |
| 1589 | CanCalleeThrow(cast<CXXNewExpr>(this)->getConstructor(), |
| 1590 | /*NullThrows*/false)); |
| 1591 | if (CT == CT_Can) |
| 1592 | return CT; |
| 1593 | return MergeCanThrow(CT, CanSubExprsThrow(C, this)); |
| 1594 | } |
| 1595 | |
| 1596 | case CXXDeleteExprClass: { |
Sebastian Redl | 369e51f | 2010-09-10 20:55:33 +0000 | [diff] [blame] | 1597 | CanThrowResult CT = CanCalleeThrow( |
| 1598 | cast<CXXDeleteExpr>(this)->getOperatorDelete()); |
| 1599 | if (CT == CT_Can) |
| 1600 | return CT; |
Sebastian Redl | 0b34cf7 | 2010-09-10 23:27:10 +0000 | [diff] [blame] | 1601 | const Expr *Arg = cast<CXXDeleteExpr>(this)->getArgument(); |
| 1602 | // Unwrap exactly one implicit cast, which converts all pointers to void*. |
| 1603 | if (const ImplicitCastExpr *Cast = dyn_cast<ImplicitCastExpr>(Arg)) |
| 1604 | Arg = Cast->getSubExpr(); |
| 1605 | if (const PointerType *PT = Arg->getType()->getAs<PointerType>()) { |
| 1606 | if (const RecordType *RT = PT->getPointeeType()->getAs<RecordType>()) { |
| 1607 | CanThrowResult CT2 = CanCalleeThrow( |
| 1608 | cast<CXXRecordDecl>(RT->getDecl())->getDestructor()); |
| 1609 | if (CT2 == CT_Can) |
| 1610 | return CT2; |
| 1611 | CT = MergeCanThrow(CT, CT2); |
| 1612 | } |
| 1613 | } |
| 1614 | return MergeCanThrow(CT, CanSubExprsThrow(C, this)); |
| 1615 | } |
| 1616 | |
| 1617 | case CXXBindTemporaryExprClass: { |
| 1618 | // The bound temporary has to be destroyed again, which might throw. |
| 1619 | CanThrowResult CT = CanCalleeThrow( |
| 1620 | cast<CXXBindTemporaryExpr>(this)->getTemporary()->getDestructor()); |
| 1621 | if (CT == CT_Can) |
| 1622 | return CT; |
Sebastian Redl | 369e51f | 2010-09-10 20:55:33 +0000 | [diff] [blame] | 1623 | return MergeCanThrow(CT, CanSubExprsThrow(C, this)); |
| 1624 | } |
| 1625 | |
| 1626 | // ObjC message sends are like function calls, but never have exception |
| 1627 | // specs. |
| 1628 | case ObjCMessageExprClass: |
| 1629 | case ObjCPropertyRefExprClass: |
Sebastian Redl | 369e51f | 2010-09-10 20:55:33 +0000 | [diff] [blame] | 1630 | return CT_Can; |
| 1631 | |
| 1632 | // Many other things have subexpressions, so we have to test those. |
| 1633 | // Some are simple: |
| 1634 | case ParenExprClass: |
| 1635 | case MemberExprClass: |
| 1636 | case CXXReinterpretCastExprClass: |
| 1637 | case CXXConstCastExprClass: |
| 1638 | case ConditionalOperatorClass: |
| 1639 | case CompoundLiteralExprClass: |
| 1640 | case ExtVectorElementExprClass: |
| 1641 | case InitListExprClass: |
| 1642 | case DesignatedInitExprClass: |
| 1643 | case ParenListExprClass: |
| 1644 | case VAArgExprClass: |
| 1645 | case CXXDefaultArgExprClass: |
John McCall | 4765fa0 | 2010-12-06 08:20:24 +0000 | [diff] [blame^] | 1646 | case ExprWithCleanupsClass: |
Sebastian Redl | 369e51f | 2010-09-10 20:55:33 +0000 | [diff] [blame] | 1647 | case ObjCIvarRefExprClass: |
| 1648 | case ObjCIsaExprClass: |
| 1649 | case ShuffleVectorExprClass: |
| 1650 | return CanSubExprsThrow(C, this); |
| 1651 | |
| 1652 | // Some might be dependent for other reasons. |
| 1653 | case UnaryOperatorClass: |
| 1654 | case ArraySubscriptExprClass: |
| 1655 | case ImplicitCastExprClass: |
| 1656 | case CStyleCastExprClass: |
| 1657 | case CXXStaticCastExprClass: |
| 1658 | case CXXFunctionalCastExprClass: |
| 1659 | case BinaryOperatorClass: |
| 1660 | case CompoundAssignOperatorClass: { |
| 1661 | CanThrowResult CT = isTypeDependent() ? CT_Dependent : CT_Cannot; |
| 1662 | return MergeCanThrow(CT, CanSubExprsThrow(C, this)); |
| 1663 | } |
| 1664 | |
| 1665 | // FIXME: We should handle StmtExpr, but that opens a MASSIVE can of worms. |
| 1666 | case StmtExprClass: |
| 1667 | return CT_Can; |
| 1668 | |
| 1669 | case ChooseExprClass: |
| 1670 | if (isTypeDependent() || isValueDependent()) |
| 1671 | return CT_Dependent; |
| 1672 | return cast<ChooseExpr>(this)->getChosenSubExpr(C)->CanThrow(C); |
| 1673 | |
| 1674 | // Some expressions are always dependent. |
| 1675 | case DependentScopeDeclRefExprClass: |
| 1676 | case CXXUnresolvedConstructExprClass: |
| 1677 | case CXXDependentScopeMemberExprClass: |
| 1678 | return CT_Dependent; |
| 1679 | |
| 1680 | default: |
| 1681 | // All other expressions don't have subexpressions, or else they are |
| 1682 | // unevaluated. |
| 1683 | return CT_Cannot; |
| 1684 | } |
| 1685 | } |
| 1686 | |
Ted Kremenek | 4e99a5f | 2008-01-17 16:57:34 +0000 | [diff] [blame] | 1687 | Expr* Expr::IgnoreParens() { |
| 1688 | Expr* E = this; |
Abramo Bagnara | b9eb35c | 2010-10-15 07:51:18 +0000 | [diff] [blame] | 1689 | while (true) { |
| 1690 | if (ParenExpr* P = dyn_cast<ParenExpr>(E)) { |
| 1691 | E = P->getSubExpr(); |
| 1692 | continue; |
| 1693 | } |
| 1694 | if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) { |
| 1695 | if (P->getOpcode() == UO_Extension) { |
| 1696 | E = P->getSubExpr(); |
| 1697 | continue; |
| 1698 | } |
| 1699 | } |
| 1700 | return E; |
| 1701 | } |
Ted Kremenek | 4e99a5f | 2008-01-17 16:57:34 +0000 | [diff] [blame] | 1702 | } |
| 1703 | |
Chris Lattner | 56f3494 | 2008-02-13 01:02:39 +0000 | [diff] [blame] | 1704 | /// IgnoreParenCasts - Ignore parentheses and casts. Strip off any ParenExpr |
| 1705 | /// or CastExprs or ImplicitCastExprs, returning their operand. |
| 1706 | Expr *Expr::IgnoreParenCasts() { |
| 1707 | Expr *E = this; |
| 1708 | while (true) { |
Abramo Bagnara | b9eb35c | 2010-10-15 07:51:18 +0000 | [diff] [blame] | 1709 | if (ParenExpr* P = dyn_cast<ParenExpr>(E)) { |
Chris Lattner | 56f3494 | 2008-02-13 01:02:39 +0000 | [diff] [blame] | 1710 | E = P->getSubExpr(); |
Abramo Bagnara | b9eb35c | 2010-10-15 07:51:18 +0000 | [diff] [blame] | 1711 | continue; |
| 1712 | } |
| 1713 | if (CastExpr *P = dyn_cast<CastExpr>(E)) { |
Chris Lattner | 56f3494 | 2008-02-13 01:02:39 +0000 | [diff] [blame] | 1714 | E = P->getSubExpr(); |
Abramo Bagnara | b9eb35c | 2010-10-15 07:51:18 +0000 | [diff] [blame] | 1715 | continue; |
| 1716 | } |
| 1717 | if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) { |
| 1718 | if (P->getOpcode() == UO_Extension) { |
| 1719 | E = P->getSubExpr(); |
| 1720 | continue; |
| 1721 | } |
| 1722 | } |
| 1723 | return E; |
Chris Lattner | 56f3494 | 2008-02-13 01:02:39 +0000 | [diff] [blame] | 1724 | } |
| 1725 | } |
| 1726 | |
John McCall | 9c5d70c | 2010-12-04 08:24:19 +0000 | [diff] [blame] | 1727 | /// IgnoreParenLValueCasts - Ignore parentheses and lvalue-to-rvalue |
| 1728 | /// casts. This is intended purely as a temporary workaround for code |
| 1729 | /// that hasn't yet been rewritten to do the right thing about those |
| 1730 | /// casts, and may disappear along with the last internal use. |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 1731 | Expr *Expr::IgnoreParenLValueCasts() { |
| 1732 | Expr *E = this; |
John McCall | 9c5d70c | 2010-12-04 08:24:19 +0000 | [diff] [blame] | 1733 | while (true) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 1734 | if (ParenExpr *P = dyn_cast<ParenExpr>(E)) { |
| 1735 | E = P->getSubExpr(); |
| 1736 | continue; |
John McCall | 9c5d70c | 2010-12-04 08:24:19 +0000 | [diff] [blame] | 1737 | } else if (CastExpr *P = dyn_cast<CastExpr>(E)) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 1738 | if (P->getCastKind() == CK_LValueToRValue) { |
| 1739 | E = P->getSubExpr(); |
| 1740 | continue; |
| 1741 | } |
John McCall | 9c5d70c | 2010-12-04 08:24:19 +0000 | [diff] [blame] | 1742 | } else if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) { |
| 1743 | if (P->getOpcode() == UO_Extension) { |
| 1744 | E = P->getSubExpr(); |
| 1745 | continue; |
| 1746 | } |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 1747 | } |
| 1748 | break; |
| 1749 | } |
| 1750 | return E; |
| 1751 | } |
| 1752 | |
John McCall | 2fc46bf | 2010-05-05 22:59:52 +0000 | [diff] [blame] | 1753 | Expr *Expr::IgnoreParenImpCasts() { |
| 1754 | Expr *E = this; |
| 1755 | while (true) { |
Abramo Bagnara | b9eb35c | 2010-10-15 07:51:18 +0000 | [diff] [blame] | 1756 | if (ParenExpr *P = dyn_cast<ParenExpr>(E)) { |
John McCall | 2fc46bf | 2010-05-05 22:59:52 +0000 | [diff] [blame] | 1757 | E = P->getSubExpr(); |
Abramo Bagnara | b9eb35c | 2010-10-15 07:51:18 +0000 | [diff] [blame] | 1758 | continue; |
| 1759 | } |
| 1760 | if (ImplicitCastExpr *P = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | 2fc46bf | 2010-05-05 22:59:52 +0000 | [diff] [blame] | 1761 | E = P->getSubExpr(); |
Abramo Bagnara | b9eb35c | 2010-10-15 07:51:18 +0000 | [diff] [blame] | 1762 | continue; |
| 1763 | } |
| 1764 | if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) { |
| 1765 | if (P->getOpcode() == UO_Extension) { |
| 1766 | E = P->getSubExpr(); |
| 1767 | continue; |
| 1768 | } |
| 1769 | } |
| 1770 | return E; |
John McCall | 2fc46bf | 2010-05-05 22:59:52 +0000 | [diff] [blame] | 1771 | } |
| 1772 | } |
| 1773 | |
Chris Lattner | ecdd841 | 2009-03-13 17:28:01 +0000 | [diff] [blame] | 1774 | /// IgnoreParenNoopCasts - Ignore parentheses and casts that do not change the |
| 1775 | /// value (including ptr->int casts of the same size). Strip off any |
| 1776 | /// ParenExpr or CastExprs, returning their operand. |
| 1777 | Expr *Expr::IgnoreParenNoopCasts(ASTContext &Ctx) { |
| 1778 | Expr *E = this; |
| 1779 | while (true) { |
| 1780 | if (ParenExpr *P = dyn_cast<ParenExpr>(E)) { |
| 1781 | E = P->getSubExpr(); |
| 1782 | continue; |
| 1783 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1784 | |
Chris Lattner | ecdd841 | 2009-03-13 17:28:01 +0000 | [diff] [blame] | 1785 | if (CastExpr *P = dyn_cast<CastExpr>(E)) { |
| 1786 | // 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] | 1787 | // 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] | 1788 | Expr *SE = P->getSubExpr(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1789 | |
Chris Lattner | ecdd841 | 2009-03-13 17:28:01 +0000 | [diff] [blame] | 1790 | if (Ctx.hasSameUnqualifiedType(E->getType(), SE->getType())) { |
| 1791 | E = SE; |
| 1792 | continue; |
| 1793 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1794 | |
Abramo Bagnara | b9eb35c | 2010-10-15 07:51:18 +0000 | [diff] [blame] | 1795 | if ((E->getType()->isPointerType() || |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 1796 | E->getType()->isIntegralType(Ctx)) && |
Abramo Bagnara | b9eb35c | 2010-10-15 07:51:18 +0000 | [diff] [blame] | 1797 | (SE->getType()->isPointerType() || |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 1798 | SE->getType()->isIntegralType(Ctx)) && |
Chris Lattner | ecdd841 | 2009-03-13 17:28:01 +0000 | [diff] [blame] | 1799 | Ctx.getTypeSize(E->getType()) == Ctx.getTypeSize(SE->getType())) { |
| 1800 | E = SE; |
| 1801 | continue; |
| 1802 | } |
| 1803 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1804 | |
Abramo Bagnara | b9eb35c | 2010-10-15 07:51:18 +0000 | [diff] [blame] | 1805 | if (UnaryOperator* P = dyn_cast<UnaryOperator>(E)) { |
| 1806 | if (P->getOpcode() == UO_Extension) { |
| 1807 | E = P->getSubExpr(); |
| 1808 | continue; |
| 1809 | } |
| 1810 | } |
| 1811 | |
Chris Lattner | ecdd841 | 2009-03-13 17:28:01 +0000 | [diff] [blame] | 1812 | return E; |
| 1813 | } |
| 1814 | } |
| 1815 | |
Douglas Gregor | 6eef519 | 2009-12-14 19:27:10 +0000 | [diff] [blame] | 1816 | bool Expr::isDefaultArgument() const { |
| 1817 | const Expr *E = this; |
| 1818 | while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) |
| 1819 | E = ICE->getSubExprAsWritten(); |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 1820 | |
Douglas Gregor | 6eef519 | 2009-12-14 19:27:10 +0000 | [diff] [blame] | 1821 | return isa<CXXDefaultArgExpr>(E); |
| 1822 | } |
Chris Lattner | ecdd841 | 2009-03-13 17:28:01 +0000 | [diff] [blame] | 1823 | |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 1824 | /// \brief Skip over any no-op casts and any temporary-binding |
| 1825 | /// expressions. |
Anders Carlsson | f8b3015 | 2010-11-28 16:40:49 +0000 | [diff] [blame] | 1826 | static const Expr *skipTemporaryBindingsNoOpCastsAndParens(const Expr *E) { |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 1827 | while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1828 | if (ICE->getCastKind() == CK_NoOp) |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 1829 | E = ICE->getSubExpr(); |
| 1830 | else |
| 1831 | break; |
| 1832 | } |
| 1833 | |
| 1834 | while (const CXXBindTemporaryExpr *BE = dyn_cast<CXXBindTemporaryExpr>(E)) |
| 1835 | E = BE->getSubExpr(); |
| 1836 | |
| 1837 | while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1838 | if (ICE->getCastKind() == CK_NoOp) |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 1839 | E = ICE->getSubExpr(); |
| 1840 | else |
| 1841 | break; |
| 1842 | } |
Anders Carlsson | f8b3015 | 2010-11-28 16:40:49 +0000 | [diff] [blame] | 1843 | |
| 1844 | return E->IgnoreParens(); |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 1845 | } |
| 1846 | |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 1847 | /// isTemporaryObject - Determines if this expression produces a |
| 1848 | /// temporary of the given class type. |
| 1849 | bool Expr::isTemporaryObject(ASTContext &C, const CXXRecordDecl *TempTy) const { |
| 1850 | if (!C.hasSameUnqualifiedType(getType(), C.getTypeDeclType(TempTy))) |
| 1851 | return false; |
| 1852 | |
Anders Carlsson | f8b3015 | 2010-11-28 16:40:49 +0000 | [diff] [blame] | 1853 | const Expr *E = skipTemporaryBindingsNoOpCastsAndParens(this); |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 1854 | |
John McCall | 58277b5 | 2010-09-15 20:59:13 +0000 | [diff] [blame] | 1855 | // Temporaries are by definition pr-values of class type. |
Fariborz Jahanian | db148be | 2010-09-27 17:30:38 +0000 | [diff] [blame] | 1856 | if (!E->Classify(C).isPRValue()) { |
| 1857 | // In this context, property reference is a message call and is pr-value. |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 1858 | if (!isa<ObjCPropertyRefExpr>(E)) |
Fariborz Jahanian | db148be | 2010-09-27 17:30:38 +0000 | [diff] [blame] | 1859 | return false; |
| 1860 | } |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 1861 | |
John McCall | 19e60ad | 2010-09-16 06:57:56 +0000 | [diff] [blame] | 1862 | // Black-list a few cases which yield pr-values of class type that don't |
| 1863 | // refer to temporaries of that type: |
| 1864 | |
| 1865 | // - implicit derived-to-base conversions |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 1866 | if (isa<ImplicitCastExpr>(E)) { |
| 1867 | switch (cast<ImplicitCastExpr>(E)->getCastKind()) { |
| 1868 | case CK_DerivedToBase: |
| 1869 | case CK_UncheckedDerivedToBase: |
| 1870 | return false; |
| 1871 | default: |
| 1872 | break; |
| 1873 | } |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 1874 | } |
| 1875 | |
John McCall | 19e60ad | 2010-09-16 06:57:56 +0000 | [diff] [blame] | 1876 | // - member expressions (all) |
| 1877 | if (isa<MemberExpr>(E)) |
| 1878 | return false; |
| 1879 | |
John McCall | 558d2ab | 2010-09-15 10:14:12 +0000 | [diff] [blame] | 1880 | return true; |
Douglas Gregor | 2f59979 | 2010-04-02 18:24:57 +0000 | [diff] [blame] | 1881 | } |
| 1882 | |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 1883 | /// hasAnyTypeDependentArguments - Determines if any of the expressions |
| 1884 | /// in Exprs is type-dependent. |
| 1885 | bool Expr::hasAnyTypeDependentArguments(Expr** Exprs, unsigned NumExprs) { |
| 1886 | for (unsigned I = 0; I < NumExprs; ++I) |
| 1887 | if (Exprs[I]->isTypeDependent()) |
| 1888 | return true; |
| 1889 | |
| 1890 | return false; |
| 1891 | } |
| 1892 | |
| 1893 | /// hasAnyValueDependentArguments - Determines if any of the expressions |
| 1894 | /// in Exprs is value-dependent. |
| 1895 | bool Expr::hasAnyValueDependentArguments(Expr** Exprs, unsigned NumExprs) { |
| 1896 | for (unsigned I = 0; I < NumExprs; ++I) |
| 1897 | if (Exprs[I]->isValueDependent()) |
| 1898 | return true; |
| 1899 | |
| 1900 | return false; |
| 1901 | } |
| 1902 | |
John McCall | 4204f07 | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 1903 | bool Expr::isConstantInitializer(ASTContext &Ctx, bool IsForRef) const { |
Eli Friedman | c39dc9a | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 1904 | // This function is attempting whether an expression is an initializer |
| 1905 | // which can be evaluated at compile-time. isEvaluatable handles most |
| 1906 | // of the cases, but it can't deal with some initializer-specific |
| 1907 | // expressions, and it can't deal with aggregates; we deal with those here, |
| 1908 | // and fall back to isEvaluatable for the other cases. |
| 1909 | |
John McCall | 4204f07 | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 1910 | // If we ever capture reference-binding directly in the AST, we can |
| 1911 | // kill the second parameter. |
| 1912 | |
| 1913 | if (IsForRef) { |
| 1914 | EvalResult Result; |
| 1915 | return EvaluateAsLValue(Result, Ctx) && !Result.HasSideEffects; |
| 1916 | } |
Eli Friedman | 1f4a6db | 2009-02-20 02:36:22 +0000 | [diff] [blame] | 1917 | |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 1918 | switch (getStmtClass()) { |
Eli Friedman | c39dc9a | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 1919 | default: break; |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 1920 | case StringLiteralClass: |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 1921 | case ObjCStringLiteralClass: |
Chris Lattner | eaf2bb8 | 2009-02-24 22:18:39 +0000 | [diff] [blame] | 1922 | case ObjCEncodeExprClass: |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 1923 | return true; |
John McCall | b4b9b15 | 2010-08-01 21:51:45 +0000 | [diff] [blame] | 1924 | case CXXTemporaryObjectExprClass: |
| 1925 | case CXXConstructExprClass: { |
| 1926 | const CXXConstructExpr *CE = cast<CXXConstructExpr>(this); |
John McCall | 4204f07 | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 1927 | |
| 1928 | // Only if it's |
| 1929 | // 1) an application of the trivial default constructor or |
John McCall | b4b9b15 | 2010-08-01 21:51:45 +0000 | [diff] [blame] | 1930 | if (!CE->getConstructor()->isTrivial()) return false; |
John McCall | 4204f07 | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 1931 | if (!CE->getNumArgs()) return true; |
| 1932 | |
| 1933 | // 2) an elidable trivial copy construction of an operand which is |
| 1934 | // itself a constant initializer. Note that we consider the |
| 1935 | // operand on its own, *not* as a reference binding. |
| 1936 | return CE->isElidable() && |
| 1937 | CE->getArg(0)->isConstantInitializer(Ctx, false); |
John McCall | b4b9b15 | 2010-08-01 21:51:45 +0000 | [diff] [blame] | 1938 | } |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 1939 | case CompoundLiteralExprClass: { |
Eli Friedman | 1f4a6db | 2009-02-20 02:36:22 +0000 | [diff] [blame] | 1940 | // This handles gcc's extension that allows global initializers like |
| 1941 | // "struct x {int x;} x = (struct x) {};". |
| 1942 | // FIXME: This accepts other cases it shouldn't! |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 1943 | const Expr *Exp = cast<CompoundLiteralExpr>(this)->getInitializer(); |
John McCall | 4204f07 | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 1944 | return Exp->isConstantInitializer(Ctx, false); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 1945 | } |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 1946 | case InitListExprClass: { |
Eli Friedman | 1f4a6db | 2009-02-20 02:36:22 +0000 | [diff] [blame] | 1947 | // FIXME: This doesn't deal with fields with reference types correctly. |
| 1948 | // FIXME: This incorrectly allows pointers cast to integers to be assigned |
| 1949 | // to bitfields. |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 1950 | const InitListExpr *Exp = cast<InitListExpr>(this); |
| 1951 | unsigned numInits = Exp->getNumInits(); |
| 1952 | for (unsigned i = 0; i < numInits; i++) { |
John McCall | 4204f07 | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 1953 | if (!Exp->getInit(i)->isConstantInitializer(Ctx, false)) |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 1954 | return false; |
| 1955 | } |
Eli Friedman | c39dc9a | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 1956 | return true; |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 1957 | } |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 1958 | case ImplicitValueInitExprClass: |
| 1959 | return true; |
Chris Lattner | 3ae9f48 | 2009-10-13 07:14:16 +0000 | [diff] [blame] | 1960 | case ParenExprClass: |
John McCall | 4204f07 | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 1961 | return cast<ParenExpr>(this)->getSubExpr() |
| 1962 | ->isConstantInitializer(Ctx, IsForRef); |
Abramo Bagnara | 5cadfab | 2010-09-27 07:13:32 +0000 | [diff] [blame] | 1963 | case ChooseExprClass: |
| 1964 | return cast<ChooseExpr>(this)->getChosenSubExpr(Ctx) |
| 1965 | ->isConstantInitializer(Ctx, IsForRef); |
Eli Friedman | c39dc9a | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 1966 | case UnaryOperatorClass: { |
| 1967 | const UnaryOperator* Exp = cast<UnaryOperator>(this); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1968 | if (Exp->getOpcode() == UO_Extension) |
John McCall | 4204f07 | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 1969 | return Exp->getSubExpr()->isConstantInitializer(Ctx, false); |
Eli Friedman | c39dc9a | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 1970 | break; |
| 1971 | } |
Chris Lattner | 3ae9f48 | 2009-10-13 07:14:16 +0000 | [diff] [blame] | 1972 | case BinaryOperatorClass: { |
| 1973 | // Special case &&foo - &&bar. It would be nice to generalize this somehow |
| 1974 | // but this handles the common case. |
| 1975 | const BinaryOperator *Exp = cast<BinaryOperator>(this); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 1976 | if (Exp->getOpcode() == BO_Sub && |
Chris Lattner | 3ae9f48 | 2009-10-13 07:14:16 +0000 | [diff] [blame] | 1977 | isa<AddrLabelExpr>(Exp->getLHS()->IgnoreParenNoopCasts(Ctx)) && |
| 1978 | isa<AddrLabelExpr>(Exp->getRHS()->IgnoreParenNoopCasts(Ctx))) |
| 1979 | return true; |
| 1980 | break; |
| 1981 | } |
John McCall | 4204f07 | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 1982 | case CXXFunctionalCastExprClass: |
John McCall | b4b9b15 | 2010-08-01 21:51:45 +0000 | [diff] [blame] | 1983 | case CXXStaticCastExprClass: |
Chris Lattner | 81045d8 | 2009-04-21 05:19:11 +0000 | [diff] [blame] | 1984 | case ImplicitCastExprClass: |
Eli Friedman | c39dc9a | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 1985 | case CStyleCastExprClass: |
| 1986 | // Handle casts with a destination that's a struct or union; this |
| 1987 | // deals with both the gcc no-op struct cast extension and the |
| 1988 | // cast-to-union extension. |
| 1989 | if (getType()->isRecordType()) |
John McCall | 4204f07 | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 1990 | return cast<CastExpr>(this)->getSubExpr() |
| 1991 | ->isConstantInitializer(Ctx, false); |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 1992 | |
Chris Lattner | 430656e | 2009-10-13 22:12:09 +0000 | [diff] [blame] | 1993 | // Integer->integer casts can be handled here, which is important for |
| 1994 | // things like (int)(&&x-&&y). Scary but true. |
| 1995 | if (getType()->isIntegerType() && |
| 1996 | cast<CastExpr>(this)->getSubExpr()->getType()->isIntegerType()) |
John McCall | 4204f07 | 2010-08-02 21:13:48 +0000 | [diff] [blame] | 1997 | return cast<CastExpr>(this)->getSubExpr() |
| 1998 | ->isConstantInitializer(Ctx, false); |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 1999 | |
Eli Friedman | c39dc9a | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 2000 | break; |
Anders Carlsson | e8a32b8 | 2008-11-24 05:23:59 +0000 | [diff] [blame] | 2001 | } |
Eli Friedman | c39dc9a | 2009-01-25 03:12:18 +0000 | [diff] [blame] | 2002 | return isEvaluatable(Ctx); |
Steve Naroff | 38374b0 | 2007-09-02 20:30:18 +0000 | [diff] [blame] | 2003 | } |
| 2004 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2005 | /// isNullPointerConstant - C99 6.3.2.3p3 - Return true if this is either an |
| 2006 | /// integer constant expression with the value zero, or if this is one that is |
| 2007 | /// cast to void*. |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2008 | bool Expr::isNullPointerConstant(ASTContext &Ctx, |
| 2009 | NullPointerConstantValueDependence NPC) const { |
| 2010 | if (isValueDependent()) { |
| 2011 | switch (NPC) { |
| 2012 | case NPC_NeverValueDependent: |
| 2013 | assert(false && "Unexpected value dependent expression!"); |
| 2014 | // If the unthinkable happens, fall through to the safest alternative. |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2015 | |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2016 | case NPC_ValueDependentIsNull: |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 2017 | return isTypeDependent() || getType()->isIntegralType(Ctx); |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2018 | |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2019 | case NPC_ValueDependentIsNotNull: |
| 2020 | return false; |
| 2021 | } |
| 2022 | } |
Daniel Dunbar | f515b22 | 2009-09-18 08:46:16 +0000 | [diff] [blame] | 2023 | |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 2024 | // Strip off a cast to void*, if it exists. Except in C++. |
Argyrios Kyrtzidis | 0835a3c | 2008-08-18 23:01:59 +0000 | [diff] [blame] | 2025 | if (const ExplicitCastExpr *CE = dyn_cast<ExplicitCastExpr>(this)) { |
Sebastian Redl | 6215dee | 2008-11-04 11:45:54 +0000 | [diff] [blame] | 2026 | if (!Ctx.getLangOptions().CPlusPlus) { |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 2027 | // Check that it is a cast to void*. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 2028 | if (const PointerType *PT = CE->getType()->getAs<PointerType>()) { |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 2029 | QualType Pointee = PT->getPointeeType(); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2030 | if (!Pointee.hasQualifiers() && |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 2031 | Pointee->isVoidType() && // to void* |
| 2032 | CE->getSubExpr()->getType()->isIntegerType()) // from int. |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2033 | return CE->getSubExpr()->isNullPointerConstant(Ctx, NPC); |
Sebastian Redl | 0777972 | 2008-10-31 14:43:28 +0000 | [diff] [blame] | 2034 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2035 | } |
Steve Naroff | aa58f00 | 2008-01-14 16:10:57 +0000 | [diff] [blame] | 2036 | } else if (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(this)) { |
| 2037 | // Ignore the ImplicitCastExpr type entirely. |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2038 | return ICE->getSubExpr()->isNullPointerConstant(Ctx, NPC); |
Steve Naroff | aa58f00 | 2008-01-14 16:10:57 +0000 | [diff] [blame] | 2039 | } else if (const ParenExpr *PE = dyn_cast<ParenExpr>(this)) { |
| 2040 | // Accept ((void*)0) as a null pointer constant, as many other |
| 2041 | // implementations do. |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2042 | return PE->getSubExpr()->isNullPointerConstant(Ctx, NPC); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2043 | } else if (const CXXDefaultArgExpr *DefaultArg |
Chris Lattner | 8123a95 | 2008-04-10 02:22:51 +0000 | [diff] [blame] | 2044 | = dyn_cast<CXXDefaultArgExpr>(this)) { |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 2045 | // See through default argument expressions |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 2046 | return DefaultArg->getExpr()->isNullPointerConstant(Ctx, NPC); |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 2047 | } else if (isa<GNUNullExpr>(this)) { |
| 2048 | // The GNU __null extension is always a null pointer constant. |
| 2049 | return true; |
Steve Naroff | aaffbf7 | 2008-01-14 02:53:34 +0000 | [diff] [blame] | 2050 | } |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 2051 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 2052 | // C++0x nullptr_t is always a null pointer constant. |
| 2053 | if (getType()->isNullPtrType()) |
| 2054 | return true; |
| 2055 | |
Fariborz Jahanian | ff3a078 | 2010-09-27 22:42:37 +0000 | [diff] [blame] | 2056 | if (const RecordType *UT = getType()->getAsUnionType()) |
| 2057 | if (UT && UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
| 2058 | if (const CompoundLiteralExpr *CLE = dyn_cast<CompoundLiteralExpr>(this)){ |
| 2059 | const Expr *InitExpr = CLE->getInitializer(); |
| 2060 | if (const InitListExpr *ILE = dyn_cast<InitListExpr>(InitExpr)) |
| 2061 | return ILE->getInit(0)->isNullPointerConstant(Ctx, NPC); |
| 2062 | } |
Steve Naroff | aa58f00 | 2008-01-14 16:10:57 +0000 | [diff] [blame] | 2063 | // This expression must be an integer type. |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2064 | if (!getType()->isIntegerType() || |
Fariborz Jahanian | 56fc0d1 | 2009-10-06 00:09:31 +0000 | [diff] [blame] | 2065 | (Ctx.getLangOptions().CPlusPlus && getType()->isEnumeralType())) |
Steve Naroff | aa58f00 | 2008-01-14 16:10:57 +0000 | [diff] [blame] | 2066 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2067 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2068 | // If we have an integer constant expression, we need to *evaluate* it and |
| 2069 | // test for the value 0. |
Eli Friedman | 09de176 | 2009-04-25 22:37:12 +0000 | [diff] [blame] | 2070 | llvm::APSInt Result; |
| 2071 | return isIntegerConstantExpr(Result, Ctx) && Result == 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2072 | } |
Steve Naroff | 31a4584 | 2007-07-28 23:10:27 +0000 | [diff] [blame] | 2073 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 2074 | /// \brief If this expression is an l-value for an Objective C |
| 2075 | /// property, find the underlying property reference expression. |
| 2076 | const ObjCPropertyRefExpr *Expr::getObjCProperty() const { |
| 2077 | const Expr *E = this; |
| 2078 | while (true) { |
| 2079 | assert((E->getValueKind() == VK_LValue && |
| 2080 | E->getObjectKind() == OK_ObjCProperty) && |
| 2081 | "expression is not a property reference"); |
| 2082 | E = E->IgnoreParenCasts(); |
| 2083 | if (const BinaryOperator *BO = dyn_cast<BinaryOperator>(E)) { |
| 2084 | if (BO->getOpcode() == BO_Comma) { |
| 2085 | E = BO->getRHS(); |
| 2086 | continue; |
| 2087 | } |
| 2088 | } |
| 2089 | |
| 2090 | break; |
| 2091 | } |
| 2092 | |
| 2093 | return cast<ObjCPropertyRefExpr>(E); |
| 2094 | } |
| 2095 | |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2096 | FieldDecl *Expr::getBitField() { |
Douglas Gregor | 6f4a69a | 2009-07-06 15:38:40 +0000 | [diff] [blame] | 2097 | Expr *E = this->IgnoreParens(); |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2098 | |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 2099 | while (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 2100 | if (ICE->getCastKind() == CK_LValueToRValue || |
| 2101 | (ICE->getValueKind() != VK_RValue && ICE->getCastKind() == CK_NoOp)) |
Douglas Gregor | de4b1d8 | 2010-01-29 19:14:02 +0000 | [diff] [blame] | 2102 | E = ICE->getSubExpr()->IgnoreParens(); |
| 2103 | else |
| 2104 | break; |
| 2105 | } |
| 2106 | |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2107 | if (MemberExpr *MemRef = dyn_cast<MemberExpr>(E)) |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 2108 | if (FieldDecl *Field = dyn_cast<FieldDecl>(MemRef->getMemberDecl())) |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2109 | if (Field->isBitField()) |
| 2110 | return Field; |
| 2111 | |
Argyrios Kyrtzidis | 0f279e7 | 2010-10-30 19:52:22 +0000 | [diff] [blame] | 2112 | if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E)) |
| 2113 | if (FieldDecl *Field = dyn_cast<FieldDecl>(DeclRef->getDecl())) |
| 2114 | if (Field->isBitField()) |
| 2115 | return Field; |
| 2116 | |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2117 | if (BinaryOperator *BinOp = dyn_cast<BinaryOperator>(E)) |
| 2118 | if (BinOp->isAssignmentOp() && BinOp->getLHS()) |
| 2119 | return BinOp->getLHS()->getBitField(); |
| 2120 | |
| 2121 | return 0; |
Douglas Gregor | 27c8dc0 | 2008-10-29 00:13:59 +0000 | [diff] [blame] | 2122 | } |
| 2123 | |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 2124 | bool Expr::refersToVectorElement() const { |
| 2125 | const Expr *E = this->IgnoreParens(); |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2126 | |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 2127 | while (const ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(E)) { |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2128 | if (ICE->getValueKind() != VK_RValue && |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2129 | ICE->getCastKind() == CK_NoOp) |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 2130 | E = ICE->getSubExpr()->IgnoreParens(); |
| 2131 | else |
| 2132 | break; |
| 2133 | } |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2134 | |
Anders Carlsson | 0938026 | 2010-01-31 17:18:49 +0000 | [diff] [blame] | 2135 | if (const ArraySubscriptExpr *ASE = dyn_cast<ArraySubscriptExpr>(E)) |
| 2136 | return ASE->getBase()->getType()->isVectorType(); |
| 2137 | |
| 2138 | if (isa<ExtVectorElementExpr>(E)) |
| 2139 | return true; |
| 2140 | |
| 2141 | return false; |
| 2142 | } |
| 2143 | |
Chris Lattner | 2140e90 | 2009-02-16 22:14:05 +0000 | [diff] [blame] | 2144 | /// isArrow - Return true if the base expression is a pointer to vector, |
| 2145 | /// return false if the base expression is a vector. |
| 2146 | bool ExtVectorElementExpr::isArrow() const { |
| 2147 | return getBase()->getType()->isPointerType(); |
| 2148 | } |
| 2149 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2150 | unsigned ExtVectorElementExpr::getNumElements() const { |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 2151 | if (const VectorType *VT = getType()->getAs<VectorType>()) |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2152 | return VT->getNumElements(); |
| 2153 | return 1; |
Chris Lattner | 4d0ac88 | 2007-08-03 16:00:20 +0000 | [diff] [blame] | 2154 | } |
| 2155 | |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2156 | /// containsDuplicateElements - Return true if any element access is repeated. |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2157 | bool ExtVectorElementExpr::containsDuplicateElements() const { |
Daniel Dunbar | a2b34eb | 2009-10-18 02:09:09 +0000 | [diff] [blame] | 2158 | // FIXME: Refactor this code to an accessor on the AST node which returns the |
| 2159 | // "type" of component access, and share with code below and in Sema. |
Daniel Dunbar | 01eb9b9 | 2009-10-18 21:17:35 +0000 | [diff] [blame] | 2160 | llvm::StringRef Comp = Accessor->getName(); |
Nate Begeman | 190d6a2 | 2009-01-18 02:01:21 +0000 | [diff] [blame] | 2161 | |
| 2162 | // Halving swizzles do not contain duplicate elements. |
Daniel Dunbar | 1502742 | 2009-10-17 23:53:04 +0000 | [diff] [blame] | 2163 | if (Comp == "hi" || Comp == "lo" || Comp == "even" || Comp == "odd") |
Nate Begeman | 190d6a2 | 2009-01-18 02:01:21 +0000 | [diff] [blame] | 2164 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2165 | |
Nate Begeman | 190d6a2 | 2009-01-18 02:01:21 +0000 | [diff] [blame] | 2166 | // Advance past s-char prefix on hex swizzles. |
Daniel Dunbar | 1502742 | 2009-10-17 23:53:04 +0000 | [diff] [blame] | 2167 | if (Comp[0] == 's' || Comp[0] == 'S') |
| 2168 | Comp = Comp.substr(1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2169 | |
Daniel Dunbar | 1502742 | 2009-10-17 23:53:04 +0000 | [diff] [blame] | 2170 | for (unsigned i = 0, e = Comp.size(); i != e; ++i) |
| 2171 | if (Comp.substr(i + 1).find(Comp[i]) != llvm::StringRef::npos) |
Steve Naroff | fec0b49 | 2007-07-30 03:29:09 +0000 | [diff] [blame] | 2172 | return true; |
Daniel Dunbar | 1502742 | 2009-10-17 23:53:04 +0000 | [diff] [blame] | 2173 | |
Steve Naroff | fec0b49 | 2007-07-30 03:29:09 +0000 | [diff] [blame] | 2174 | return false; |
| 2175 | } |
Chris Lattner | b8f849d | 2007-08-02 23:36:59 +0000 | [diff] [blame] | 2176 | |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2177 | /// getEncodedElementAccess - We encode the fields as a llvm ConstantArray. |
Nate Begeman | 3b8d116 | 2008-05-13 21:03:02 +0000 | [diff] [blame] | 2178 | void ExtVectorElementExpr::getEncodedElementAccess( |
| 2179 | llvm::SmallVectorImpl<unsigned> &Elts) const { |
Daniel Dunbar | 4b55b24 | 2009-10-18 02:09:31 +0000 | [diff] [blame] | 2180 | llvm::StringRef Comp = Accessor->getName(); |
| 2181 | if (Comp[0] == 's' || Comp[0] == 'S') |
| 2182 | Comp = Comp.substr(1); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2183 | |
Daniel Dunbar | 4b55b24 | 2009-10-18 02:09:31 +0000 | [diff] [blame] | 2184 | bool isHi = Comp == "hi"; |
| 2185 | bool isLo = Comp == "lo"; |
| 2186 | bool isEven = Comp == "even"; |
| 2187 | bool isOdd = Comp == "odd"; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2188 | |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2189 | for (unsigned i = 0, e = getNumElements(); i != e; ++i) { |
| 2190 | uint64_t Index; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2191 | |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2192 | if (isHi) |
| 2193 | Index = e + i; |
| 2194 | else if (isLo) |
| 2195 | Index = i; |
| 2196 | else if (isEven) |
| 2197 | Index = 2 * i; |
| 2198 | else if (isOdd) |
| 2199 | Index = 2 * i + 1; |
| 2200 | else |
Daniel Dunbar | 4b55b24 | 2009-10-18 02:09:31 +0000 | [diff] [blame] | 2201 | Index = ExtVectorType::getAccessorIdx(Comp[i]); |
Chris Lattner | b8f849d | 2007-08-02 23:36:59 +0000 | [diff] [blame] | 2202 | |
Nate Begeman | 3b8d116 | 2008-05-13 21:03:02 +0000 | [diff] [blame] | 2203 | Elts.push_back(Index); |
Chris Lattner | b8f849d | 2007-08-02 23:36:59 +0000 | [diff] [blame] | 2204 | } |
Nate Begeman | 8a99764 | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 2205 | } |
| 2206 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2207 | ObjCMessageExpr::ObjCMessageExpr(QualType T, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2208 | ExprValueKind VK, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2209 | SourceLocation LBracLoc, |
| 2210 | SourceLocation SuperLoc, |
| 2211 | bool IsInstanceSuper, |
| 2212 | QualType SuperType, |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2213 | Selector Sel, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2214 | ObjCMethodDecl *Method, |
| 2215 | Expr **Args, unsigned NumArgs, |
| 2216 | SourceLocation RBracLoc) |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2217 | : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, |
| 2218 | /*TypeDependent=*/false, /*ValueDependent=*/false), |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2219 | NumArgs(NumArgs), Kind(IsInstanceSuper? SuperInstance : SuperClass), |
| 2220 | HasMethod(Method != 0), SuperLoc(SuperLoc), |
| 2221 | SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method |
| 2222 | : Sel.getAsOpaquePtr())), |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2223 | LBracLoc(LBracLoc), RBracLoc(RBracLoc) |
Douglas Gregor | c2350e5 | 2010-03-08 16:40:19 +0000 | [diff] [blame] | 2224 | { |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2225 | setReceiverPointer(SuperType.getAsOpaquePtr()); |
| 2226 | if (NumArgs) |
| 2227 | memcpy(getArgs(), Args, NumArgs * sizeof(Expr *)); |
Ted Kremenek | 4df728e | 2008-06-24 15:50:53 +0000 | [diff] [blame] | 2228 | } |
| 2229 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2230 | ObjCMessageExpr::ObjCMessageExpr(QualType T, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2231 | ExprValueKind VK, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2232 | SourceLocation LBracLoc, |
| 2233 | TypeSourceInfo *Receiver, |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2234 | Selector Sel, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2235 | ObjCMethodDecl *Method, |
| 2236 | Expr **Args, unsigned NumArgs, |
| 2237 | SourceLocation RBracLoc) |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2238 | : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, T->isDependentType(), |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2239 | (T->isDependentType() || |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2240 | hasAnyValueDependentArguments(Args, NumArgs))), |
| 2241 | NumArgs(NumArgs), Kind(Class), HasMethod(Method != 0), |
| 2242 | SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method |
| 2243 | : Sel.getAsOpaquePtr())), |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2244 | LBracLoc(LBracLoc), RBracLoc(RBracLoc) |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2245 | { |
| 2246 | setReceiverPointer(Receiver); |
| 2247 | if (NumArgs) |
| 2248 | memcpy(getArgs(), Args, NumArgs * sizeof(Expr *)); |
Ted Kremenek | 4df728e | 2008-06-24 15:50:53 +0000 | [diff] [blame] | 2249 | } |
| 2250 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2251 | ObjCMessageExpr::ObjCMessageExpr(QualType T, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2252 | ExprValueKind VK, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2253 | SourceLocation LBracLoc, |
| 2254 | Expr *Receiver, |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2255 | Selector Sel, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2256 | ObjCMethodDecl *Method, |
| 2257 | Expr **Args, unsigned NumArgs, |
| 2258 | SourceLocation RBracLoc) |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2259 | : Expr(ObjCMessageExprClass, T, VK, OK_Ordinary, Receiver->isTypeDependent(), |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2260 | (Receiver->isTypeDependent() || |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2261 | hasAnyValueDependentArguments(Args, NumArgs))), |
| 2262 | NumArgs(NumArgs), Kind(Instance), HasMethod(Method != 0), |
| 2263 | SelectorOrMethod(reinterpret_cast<uintptr_t>(Method? Method |
| 2264 | : Sel.getAsOpaquePtr())), |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2265 | LBracLoc(LBracLoc), RBracLoc(RBracLoc) |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2266 | { |
| 2267 | setReceiverPointer(Receiver); |
| 2268 | if (NumArgs) |
| 2269 | memcpy(getArgs(), Args, NumArgs * sizeof(Expr *)); |
Chris Lattner | 0389e6b | 2009-04-26 00:44:05 +0000 | [diff] [blame] | 2270 | } |
| 2271 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2272 | ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2273 | ExprValueKind VK, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2274 | SourceLocation LBracLoc, |
| 2275 | SourceLocation SuperLoc, |
| 2276 | bool IsInstanceSuper, |
| 2277 | QualType SuperType, |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2278 | Selector Sel, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2279 | ObjCMethodDecl *Method, |
| 2280 | Expr **Args, unsigned NumArgs, |
| 2281 | SourceLocation RBracLoc) { |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2282 | unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) + |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2283 | NumArgs * sizeof(Expr *); |
| 2284 | void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2285 | return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, SuperLoc, IsInstanceSuper, |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2286 | SuperType, Sel, Method, Args, NumArgs, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2287 | RBracLoc); |
| 2288 | } |
| 2289 | |
| 2290 | ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2291 | ExprValueKind VK, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2292 | SourceLocation LBracLoc, |
| 2293 | TypeSourceInfo *Receiver, |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2294 | Selector Sel, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2295 | ObjCMethodDecl *Method, |
| 2296 | Expr **Args, unsigned NumArgs, |
| 2297 | SourceLocation RBracLoc) { |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2298 | unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) + |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2299 | NumArgs * sizeof(Expr *); |
| 2300 | void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2301 | return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, Method, Args, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2302 | NumArgs, RBracLoc); |
| 2303 | } |
| 2304 | |
| 2305 | ObjCMessageExpr *ObjCMessageExpr::Create(ASTContext &Context, QualType T, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2306 | ExprValueKind VK, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2307 | SourceLocation LBracLoc, |
| 2308 | Expr *Receiver, |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2309 | Selector Sel, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2310 | ObjCMethodDecl *Method, |
| 2311 | Expr **Args, unsigned NumArgs, |
| 2312 | SourceLocation RBracLoc) { |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2313 | unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) + |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2314 | NumArgs * sizeof(Expr *); |
| 2315 | void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2316 | return new (Mem) ObjCMessageExpr(T, VK, LBracLoc, Receiver, Sel, Method, Args, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2317 | NumArgs, RBracLoc); |
| 2318 | } |
| 2319 | |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2320 | ObjCMessageExpr *ObjCMessageExpr::CreateEmpty(ASTContext &Context, |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2321 | unsigned NumArgs) { |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2322 | unsigned Size = sizeof(ObjCMessageExpr) + sizeof(void *) + |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2323 | NumArgs * sizeof(Expr *); |
| 2324 | void *Mem = Context.Allocate(Size, llvm::AlignOf<ObjCMessageExpr>::Alignment); |
| 2325 | return new (Mem) ObjCMessageExpr(EmptyShell(), NumArgs); |
| 2326 | } |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2327 | |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2328 | Selector ObjCMessageExpr::getSelector() const { |
| 2329 | if (HasMethod) |
| 2330 | return reinterpret_cast<const ObjCMethodDecl *>(SelectorOrMethod) |
| 2331 | ->getSelector(); |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2332 | return Selector(SelectorOrMethod); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2333 | } |
| 2334 | |
| 2335 | ObjCInterfaceDecl *ObjCMessageExpr::getReceiverInterface() const { |
| 2336 | switch (getReceiverKind()) { |
| 2337 | case Instance: |
| 2338 | if (const ObjCObjectPointerType *Ptr |
| 2339 | = getInstanceReceiver()->getType()->getAs<ObjCObjectPointerType>()) |
| 2340 | return Ptr->getInterfaceDecl(); |
| 2341 | break; |
| 2342 | |
| 2343 | case Class: |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 2344 | if (const ObjCObjectType *Ty |
| 2345 | = getClassReceiver()->getAs<ObjCObjectType>()) |
| 2346 | return Ty->getInterface(); |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2347 | break; |
| 2348 | |
| 2349 | case SuperInstance: |
| 2350 | if (const ObjCObjectPointerType *Ptr |
| 2351 | = getSuperType()->getAs<ObjCObjectPointerType>()) |
| 2352 | return Ptr->getInterfaceDecl(); |
| 2353 | break; |
| 2354 | |
| 2355 | case SuperClass: |
| 2356 | if (const ObjCObjectPointerType *Iface |
| 2357 | = getSuperType()->getAs<ObjCObjectPointerType>()) |
| 2358 | return Iface->getInterfaceDecl(); |
| 2359 | break; |
| 2360 | } |
| 2361 | |
| 2362 | return 0; |
Ted Kremenek | eb3b324 | 2010-02-11 22:41:21 +0000 | [diff] [blame] | 2363 | } |
Chris Lattner | 0389e6b | 2009-04-26 00:44:05 +0000 | [diff] [blame] | 2364 | |
Chris Lattner | 27437ca | 2007-10-25 00:29:32 +0000 | [diff] [blame] | 2365 | bool ChooseExpr::isConditionTrue(ASTContext &C) const { |
Eli Friedman | 9a901bb | 2009-04-26 19:19:15 +0000 | [diff] [blame] | 2366 | return getCond()->EvaluateAsInt(C) != 0; |
Chris Lattner | 27437ca | 2007-10-25 00:29:32 +0000 | [diff] [blame] | 2367 | } |
| 2368 | |
Nate Begeman | 888376a | 2009-08-12 02:28:50 +0000 | [diff] [blame] | 2369 | void ShuffleVectorExpr::setExprs(ASTContext &C, Expr ** Exprs, |
| 2370 | unsigned NumExprs) { |
| 2371 | if (SubExprs) C.Deallocate(SubExprs); |
| 2372 | |
| 2373 | SubExprs = new (C) Stmt* [NumExprs]; |
Douglas Gregor | 94cd5d1 | 2009-04-16 00:01:45 +0000 | [diff] [blame] | 2374 | this->NumExprs = NumExprs; |
| 2375 | memcpy(SubExprs, Exprs, sizeof(Expr *) * NumExprs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2376 | } |
Nate Begeman | 888376a | 2009-08-12 02:28:50 +0000 | [diff] [blame] | 2377 | |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 2378 | //===----------------------------------------------------------------------===// |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2379 | // DesignatedInitExpr |
| 2380 | //===----------------------------------------------------------------------===// |
| 2381 | |
| 2382 | IdentifierInfo *DesignatedInitExpr::Designator::getFieldName() { |
| 2383 | assert(Kind == FieldDesignator && "Only valid on a field designator"); |
| 2384 | if (Field.NameOrField & 0x01) |
| 2385 | return reinterpret_cast<IdentifierInfo *>(Field.NameOrField&~0x01); |
| 2386 | else |
| 2387 | return getField()->getIdentifier(); |
| 2388 | } |
| 2389 | |
Sean Hunt | c302113 | 2010-05-05 15:23:54 +0000 | [diff] [blame] | 2390 | DesignatedInitExpr::DesignatedInitExpr(ASTContext &C, QualType Ty, |
Douglas Gregor | 319d57f | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 2391 | unsigned NumDesignators, |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2392 | const Designator *Designators, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2393 | SourceLocation EqualOrColonLoc, |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2394 | bool GNUSyntax, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2395 | Expr **IndexExprs, |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2396 | unsigned NumIndexExprs, |
| 2397 | Expr *Init) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2398 | : Expr(DesignatedInitExprClass, Ty, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2399 | Init->getValueKind(), Init->getObjectKind(), |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2400 | Init->isTypeDependent(), Init->isValueDependent()), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2401 | EqualOrColonLoc(EqualOrColonLoc), GNUSyntax(GNUSyntax), |
| 2402 | NumDesignators(NumDesignators), NumSubExprs(NumIndexExprs + 1) { |
Douglas Gregor | 319d57f | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 2403 | this->Designators = new (C) Designator[NumDesignators]; |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2404 | |
| 2405 | // Record the initializer itself. |
| 2406 | child_iterator Child = child_begin(); |
| 2407 | *Child++ = Init; |
| 2408 | |
| 2409 | // Copy the designators and their subexpressions, computing |
| 2410 | // value-dependence along the way. |
| 2411 | unsigned IndexIdx = 0; |
| 2412 | for (unsigned I = 0; I != NumDesignators; ++I) { |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2413 | this->Designators[I] = Designators[I]; |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2414 | |
| 2415 | if (this->Designators[I].isArrayDesignator()) { |
| 2416 | // Compute type- and value-dependence. |
| 2417 | Expr *Index = IndexExprs[IndexIdx]; |
John McCall | 8e6285a | 2010-10-26 08:39:16 +0000 | [diff] [blame] | 2418 | ExprBits.ValueDependent = ExprBits.ValueDependent || |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2419 | Index->isTypeDependent() || Index->isValueDependent(); |
| 2420 | |
| 2421 | // Copy the index expressions into permanent storage. |
| 2422 | *Child++ = IndexExprs[IndexIdx++]; |
| 2423 | } else if (this->Designators[I].isArrayRangeDesignator()) { |
| 2424 | // Compute type- and value-dependence. |
| 2425 | Expr *Start = IndexExprs[IndexIdx]; |
| 2426 | Expr *End = IndexExprs[IndexIdx + 1]; |
John McCall | 8e6285a | 2010-10-26 08:39:16 +0000 | [diff] [blame] | 2427 | ExprBits.ValueDependent = ExprBits.ValueDependent || |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2428 | Start->isTypeDependent() || Start->isValueDependent() || |
| 2429 | End->isTypeDependent() || End->isValueDependent(); |
| 2430 | |
| 2431 | // Copy the start/end expressions into permanent storage. |
| 2432 | *Child++ = IndexExprs[IndexIdx++]; |
| 2433 | *Child++ = IndexExprs[IndexIdx++]; |
| 2434 | } |
| 2435 | } |
| 2436 | |
| 2437 | assert(IndexIdx == NumIndexExprs && "Wrong number of index expressions"); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2438 | } |
| 2439 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2440 | DesignatedInitExpr * |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2441 | DesignatedInitExpr::Create(ASTContext &C, Designator *Designators, |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2442 | unsigned NumDesignators, |
| 2443 | Expr **IndexExprs, unsigned NumIndexExprs, |
| 2444 | SourceLocation ColonOrEqualLoc, |
| 2445 | bool UsesColonSyntax, Expr *Init) { |
Steve Naroff | c0ac492 | 2009-01-27 23:20:32 +0000 | [diff] [blame] | 2446 | void *Mem = C.Allocate(sizeof(DesignatedInitExpr) + |
Steve Naroff | c0ac492 | 2009-01-27 23:20:32 +0000 | [diff] [blame] | 2447 | sizeof(Stmt *) * (NumIndexExprs + 1), 8); |
Douglas Gregor | 319d57f | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 2448 | return new (Mem) DesignatedInitExpr(C, C.VoidTy, NumDesignators, Designators, |
Douglas Gregor | 9ea6276 | 2009-05-21 23:17:49 +0000 | [diff] [blame] | 2449 | ColonOrEqualLoc, UsesColonSyntax, |
| 2450 | IndexExprs, NumIndexExprs, Init); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2451 | } |
| 2452 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2453 | DesignatedInitExpr *DesignatedInitExpr::CreateEmpty(ASTContext &C, |
Douglas Gregor | d077d75 | 2009-04-16 00:55:48 +0000 | [diff] [blame] | 2454 | unsigned NumIndexExprs) { |
| 2455 | void *Mem = C.Allocate(sizeof(DesignatedInitExpr) + |
| 2456 | sizeof(Stmt *) * (NumIndexExprs + 1), 8); |
| 2457 | return new (Mem) DesignatedInitExpr(NumIndexExprs + 1); |
| 2458 | } |
| 2459 | |
Douglas Gregor | 319d57f | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 2460 | void DesignatedInitExpr::setDesignators(ASTContext &C, |
| 2461 | const Designator *Desigs, |
Douglas Gregor | d077d75 | 2009-04-16 00:55:48 +0000 | [diff] [blame] | 2462 | unsigned NumDesigs) { |
Douglas Gregor | 319d57f | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 2463 | Designators = new (C) Designator[NumDesigs]; |
Douglas Gregor | d077d75 | 2009-04-16 00:55:48 +0000 | [diff] [blame] | 2464 | NumDesignators = NumDesigs; |
| 2465 | for (unsigned I = 0; I != NumDesigs; ++I) |
| 2466 | Designators[I] = Desigs[I]; |
| 2467 | } |
| 2468 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2469 | SourceRange DesignatedInitExpr::getSourceRange() const { |
| 2470 | SourceLocation StartLoc; |
Chris Lattner | d603eaa | 2009-02-16 22:33:34 +0000 | [diff] [blame] | 2471 | Designator &First = |
| 2472 | *const_cast<DesignatedInitExpr*>(this)->designators_begin(); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2473 | if (First.isFieldDesignator()) { |
Douglas Gregor | eeae8f0 | 2009-03-28 00:41:23 +0000 | [diff] [blame] | 2474 | if (GNUSyntax) |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2475 | StartLoc = SourceLocation::getFromRawEncoding(First.Field.FieldLoc); |
| 2476 | else |
| 2477 | StartLoc = SourceLocation::getFromRawEncoding(First.Field.DotLoc); |
| 2478 | } else |
Chris Lattner | d603eaa | 2009-02-16 22:33:34 +0000 | [diff] [blame] | 2479 | StartLoc = |
| 2480 | SourceLocation::getFromRawEncoding(First.ArrayOrRange.LBracketLoc); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2481 | return SourceRange(StartLoc, getInit()->getSourceRange().getEnd()); |
| 2482 | } |
| 2483 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2484 | Expr *DesignatedInitExpr::getArrayIndex(const Designator& D) { |
| 2485 | assert(D.Kind == Designator::ArrayDesignator && "Requires array designator"); |
| 2486 | char* Ptr = static_cast<char*>(static_cast<void *>(this)); |
| 2487 | Ptr += sizeof(DesignatedInitExpr); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2488 | Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr)); |
| 2489 | return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1)); |
| 2490 | } |
| 2491 | |
| 2492 | Expr *DesignatedInitExpr::getArrayRangeStart(const Designator& D) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2493 | assert(D.Kind == Designator::ArrayRangeDesignator && |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2494 | "Requires array range designator"); |
| 2495 | char* Ptr = static_cast<char*>(static_cast<void *>(this)); |
| 2496 | Ptr += sizeof(DesignatedInitExpr); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2497 | Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr)); |
| 2498 | return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 1)); |
| 2499 | } |
| 2500 | |
| 2501 | Expr *DesignatedInitExpr::getArrayRangeEnd(const Designator& D) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2502 | assert(D.Kind == Designator::ArrayRangeDesignator && |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2503 | "Requires array range designator"); |
| 2504 | char* Ptr = static_cast<char*>(static_cast<void *>(this)); |
| 2505 | Ptr += sizeof(DesignatedInitExpr); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2506 | Stmt **SubExprs = reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr)); |
| 2507 | return cast<Expr>(*(SubExprs + D.ArrayOrRange.Index + 2)); |
| 2508 | } |
| 2509 | |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2510 | /// \brief Replaces the designator at index @p Idx with the series |
| 2511 | /// of designators in [First, Last). |
Douglas Gregor | 319d57f | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 2512 | void DesignatedInitExpr::ExpandDesignator(ASTContext &C, unsigned Idx, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2513 | const Designator *First, |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2514 | const Designator *Last) { |
| 2515 | unsigned NumNewDesignators = Last - First; |
| 2516 | if (NumNewDesignators == 0) { |
| 2517 | std::copy_backward(Designators + Idx + 1, |
| 2518 | Designators + NumDesignators, |
| 2519 | Designators + Idx); |
| 2520 | --NumNewDesignators; |
| 2521 | return; |
| 2522 | } else if (NumNewDesignators == 1) { |
| 2523 | Designators[Idx] = *First; |
| 2524 | return; |
| 2525 | } |
| 2526 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2527 | Designator *NewDesignators |
Douglas Gregor | 319d57f | 2010-01-06 23:17:19 +0000 | [diff] [blame] | 2528 | = new (C) Designator[NumDesignators - 1 + NumNewDesignators]; |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2529 | std::copy(Designators, Designators + Idx, NewDesignators); |
| 2530 | std::copy(First, Last, NewDesignators + Idx); |
| 2531 | std::copy(Designators + Idx + 1, Designators + NumDesignators, |
| 2532 | NewDesignators + Idx + NumNewDesignators); |
Douglas Gregor | ffb4b6e | 2009-04-15 06:41:24 +0000 | [diff] [blame] | 2533 | Designators = NewDesignators; |
| 2534 | NumDesignators = NumDesignators - 1 + NumNewDesignators; |
| 2535 | } |
| 2536 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2537 | ParenListExpr::ParenListExpr(ASTContext& C, SourceLocation lparenloc, |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 2538 | Expr **exprs, unsigned nexprs, |
| 2539 | SourceLocation rparenloc) |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2540 | : Expr(ParenListExprClass, QualType(), VK_RValue, OK_Ordinary, |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 2541 | hasAnyTypeDependentArguments(exprs, nexprs), |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2542 | hasAnyValueDependentArguments(exprs, nexprs)), |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 2543 | NumExprs(nexprs), LParenLoc(lparenloc), RParenLoc(rparenloc) { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2544 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 2545 | Exprs = new (C) Stmt*[nexprs]; |
| 2546 | for (unsigned i = 0; i != nexprs; ++i) |
| 2547 | Exprs[i] = exprs[i]; |
| 2548 | } |
| 2549 | |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2550 | //===----------------------------------------------------------------------===// |
Ted Kremenek | ce2fc3a | 2008-10-27 18:40:21 +0000 | [diff] [blame] | 2551 | // ExprIterator. |
| 2552 | //===----------------------------------------------------------------------===// |
| 2553 | |
| 2554 | Expr* ExprIterator::operator[](size_t idx) { return cast<Expr>(I[idx]); } |
| 2555 | Expr* ExprIterator::operator*() const { return cast<Expr>(*I); } |
| 2556 | Expr* ExprIterator::operator->() const { return cast<Expr>(*I); } |
| 2557 | const Expr* ConstExprIterator::operator[](size_t idx) const { |
| 2558 | return cast<Expr>(I[idx]); |
| 2559 | } |
| 2560 | const Expr* ConstExprIterator::operator*() const { return cast<Expr>(*I); } |
| 2561 | const Expr* ConstExprIterator::operator->() const { return cast<Expr>(*I); } |
| 2562 | |
| 2563 | //===----------------------------------------------------------------------===// |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 2564 | // Child Iterators for iterating over subexpressions/substatements |
| 2565 | //===----------------------------------------------------------------------===// |
| 2566 | |
| 2567 | // DeclRefExpr |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 2568 | Stmt::child_iterator DeclRefExpr::child_begin() { return child_iterator(); } |
| 2569 | Stmt::child_iterator DeclRefExpr::child_end() { return child_iterator(); } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 2570 | |
Steve Naroff | 7779db4 | 2007-11-12 14:29:37 +0000 | [diff] [blame] | 2571 | // ObjCIvarRefExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2572 | Stmt::child_iterator ObjCIvarRefExpr::child_begin() { return &Base; } |
| 2573 | Stmt::child_iterator ObjCIvarRefExpr::child_end() { return &Base+1; } |
Steve Naroff | 7779db4 | 2007-11-12 14:29:37 +0000 | [diff] [blame] | 2574 | |
Steve Naroff | e3e9add | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 2575 | // ObjCPropertyRefExpr |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 2576 | Stmt::child_iterator ObjCPropertyRefExpr::child_begin() |
| 2577 | { |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 2578 | if (Receiver.is<Stmt*>()) { |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 2579 | // Hack alert! |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 2580 | return reinterpret_cast<Stmt**> (&Receiver); |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 2581 | } |
| 2582 | return child_iterator(); |
| 2583 | } |
| 2584 | |
| 2585 | Stmt::child_iterator ObjCPropertyRefExpr::child_end() |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 2586 | { return Receiver.is<Stmt*>() ? |
| 2587 | reinterpret_cast<Stmt**> (&Receiver)+1 : |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 2588 | child_iterator(); |
| 2589 | } |
Steve Naroff | ae78407 | 2008-05-30 00:40:33 +0000 | [diff] [blame] | 2590 | |
Steve Naroff | f242b1b | 2009-07-24 17:54:45 +0000 | [diff] [blame] | 2591 | // ObjCIsaExpr |
| 2592 | Stmt::child_iterator ObjCIsaExpr::child_begin() { return &Base; } |
| 2593 | Stmt::child_iterator ObjCIsaExpr::child_end() { return &Base+1; } |
| 2594 | |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 2595 | // PredefinedExpr |
| 2596 | Stmt::child_iterator PredefinedExpr::child_begin() { return child_iterator(); } |
| 2597 | Stmt::child_iterator PredefinedExpr::child_end() { return child_iterator(); } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 2598 | |
| 2599 | // IntegerLiteral |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 2600 | Stmt::child_iterator IntegerLiteral::child_begin() { return child_iterator(); } |
| 2601 | Stmt::child_iterator IntegerLiteral::child_end() { return child_iterator(); } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 2602 | |
| 2603 | // CharacterLiteral |
Chris Lattner | d603eaa | 2009-02-16 22:33:34 +0000 | [diff] [blame] | 2604 | Stmt::child_iterator CharacterLiteral::child_begin() { return child_iterator();} |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 2605 | Stmt::child_iterator CharacterLiteral::child_end() { return child_iterator(); } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 2606 | |
| 2607 | // FloatingLiteral |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 2608 | Stmt::child_iterator FloatingLiteral::child_begin() { return child_iterator(); } |
| 2609 | Stmt::child_iterator FloatingLiteral::child_end() { return child_iterator(); } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 2610 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2611 | // ImaginaryLiteral |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2612 | Stmt::child_iterator ImaginaryLiteral::child_begin() { return &Val; } |
| 2613 | Stmt::child_iterator ImaginaryLiteral::child_end() { return &Val+1; } |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2614 | |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 2615 | // StringLiteral |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 2616 | Stmt::child_iterator StringLiteral::child_begin() { return child_iterator(); } |
| 2617 | Stmt::child_iterator StringLiteral::child_end() { return child_iterator(); } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 2618 | |
| 2619 | // ParenExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2620 | Stmt::child_iterator ParenExpr::child_begin() { return &Val; } |
| 2621 | Stmt::child_iterator ParenExpr::child_end() { return &Val+1; } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 2622 | |
| 2623 | // UnaryOperator |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2624 | Stmt::child_iterator UnaryOperator::child_begin() { return &Val; } |
| 2625 | Stmt::child_iterator UnaryOperator::child_end() { return &Val+1; } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 2626 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 2627 | // OffsetOfExpr |
| 2628 | Stmt::child_iterator OffsetOfExpr::child_begin() { |
| 2629 | return reinterpret_cast<Stmt **> (reinterpret_cast<OffsetOfNode *> (this + 1) |
| 2630 | + NumComps); |
| 2631 | } |
| 2632 | Stmt::child_iterator OffsetOfExpr::child_end() { |
| 2633 | return child_iterator(&*child_begin() + NumExprs); |
| 2634 | } |
| 2635 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2636 | // SizeOfAlignOfExpr |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2637 | Stmt::child_iterator SizeOfAlignOfExpr::child_begin() { |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2638 | // If this is of a type and the type is a VLA type (and not a typedef), the |
| 2639 | // size expression of the VLA needs to be treated as an executable expression. |
| 2640 | // Why isn't this weirdness documented better in StmtIterator? |
| 2641 | if (isArgumentType()) { |
| 2642 | if (VariableArrayType* T = dyn_cast<VariableArrayType>( |
| 2643 | getArgumentType().getTypePtr())) |
| 2644 | return child_iterator(T); |
| 2645 | return child_iterator(); |
| 2646 | } |
Sebastian Redl | d457589 | 2008-12-03 23:17:54 +0000 | [diff] [blame] | 2647 | return child_iterator(&Argument.Ex); |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 2648 | } |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2649 | Stmt::child_iterator SizeOfAlignOfExpr::child_end() { |
| 2650 | if (isArgumentType()) |
| 2651 | return child_iterator(); |
Sebastian Redl | d457589 | 2008-12-03 23:17:54 +0000 | [diff] [blame] | 2652 | return child_iterator(&Argument.Ex + 1); |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 2653 | } |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 2654 | |
| 2655 | // ArraySubscriptExpr |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 2656 | Stmt::child_iterator ArraySubscriptExpr::child_begin() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2657 | return &SubExprs[0]; |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 2658 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 2659 | Stmt::child_iterator ArraySubscriptExpr::child_end() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2660 | return &SubExprs[0]+END_EXPR; |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 2661 | } |
| 2662 | |
| 2663 | // CallExpr |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 2664 | Stmt::child_iterator CallExpr::child_begin() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2665 | return &SubExprs[0]; |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 2666 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 2667 | Stmt::child_iterator CallExpr::child_end() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2668 | return &SubExprs[0]+NumArgs+ARGS_START; |
Ted Kremenek | 77ed8e4 | 2007-08-24 18:13:47 +0000 | [diff] [blame] | 2669 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 2670 | |
| 2671 | // MemberExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2672 | Stmt::child_iterator MemberExpr::child_begin() { return &Base; } |
| 2673 | Stmt::child_iterator MemberExpr::child_end() { return &Base+1; } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 2674 | |
Nate Begeman | 213541a | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 2675 | // ExtVectorElementExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2676 | Stmt::child_iterator ExtVectorElementExpr::child_begin() { return &Base; } |
| 2677 | Stmt::child_iterator ExtVectorElementExpr::child_end() { return &Base+1; } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 2678 | |
| 2679 | // CompoundLiteralExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2680 | Stmt::child_iterator CompoundLiteralExpr::child_begin() { return &Init; } |
| 2681 | Stmt::child_iterator CompoundLiteralExpr::child_end() { return &Init+1; } |
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 | // CastExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2684 | Stmt::child_iterator CastExpr::child_begin() { return &Op; } |
| 2685 | Stmt::child_iterator CastExpr::child_end() { return &Op+1; } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 2686 | |
| 2687 | // BinaryOperator |
| 2688 | Stmt::child_iterator BinaryOperator::child_begin() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2689 | return &SubExprs[0]; |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 2690 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 2691 | Stmt::child_iterator BinaryOperator::child_end() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2692 | return &SubExprs[0]+END_EXPR; |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 2693 | } |
| 2694 | |
| 2695 | // ConditionalOperator |
| 2696 | Stmt::child_iterator ConditionalOperator::child_begin() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2697 | return &SubExprs[0]; |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 2698 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 2699 | Stmt::child_iterator ConditionalOperator::child_end() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2700 | return &SubExprs[0]+END_EXPR; |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 2701 | } |
| 2702 | |
| 2703 | // AddrLabelExpr |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 2704 | Stmt::child_iterator AddrLabelExpr::child_begin() { return child_iterator(); } |
| 2705 | Stmt::child_iterator AddrLabelExpr::child_end() { return child_iterator(); } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 2706 | |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 2707 | // StmtExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2708 | Stmt::child_iterator StmtExpr::child_begin() { return &SubStmt; } |
| 2709 | Stmt::child_iterator StmtExpr::child_end() { return &SubStmt+1; } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 2710 | |
| 2711 | // TypesCompatibleExpr |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 2712 | Stmt::child_iterator TypesCompatibleExpr::child_begin() { |
| 2713 | return child_iterator(); |
| 2714 | } |
| 2715 | |
| 2716 | Stmt::child_iterator TypesCompatibleExpr::child_end() { |
| 2717 | return child_iterator(); |
| 2718 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 2719 | |
| 2720 | // ChooseExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2721 | Stmt::child_iterator ChooseExpr::child_begin() { return &SubExprs[0]; } |
| 2722 | Stmt::child_iterator ChooseExpr::child_end() { return &SubExprs[0]+END_EXPR; } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 2723 | |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 2724 | // GNUNullExpr |
| 2725 | Stmt::child_iterator GNUNullExpr::child_begin() { return child_iterator(); } |
| 2726 | Stmt::child_iterator GNUNullExpr::child_end() { return child_iterator(); } |
| 2727 | |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 2728 | // ShuffleVectorExpr |
| 2729 | Stmt::child_iterator ShuffleVectorExpr::child_begin() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2730 | return &SubExprs[0]; |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 2731 | } |
| 2732 | Stmt::child_iterator ShuffleVectorExpr::child_end() { |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2733 | return &SubExprs[0]+NumExprs; |
Eli Friedman | d38617c | 2008-05-14 19:38:39 +0000 | [diff] [blame] | 2734 | } |
| 2735 | |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 2736 | // VAArgExpr |
Ted Kremenek | 5549976 | 2008-06-17 02:43:46 +0000 | [diff] [blame] | 2737 | Stmt::child_iterator VAArgExpr::child_begin() { return &Val; } |
| 2738 | Stmt::child_iterator VAArgExpr::child_end() { return &Val+1; } |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 2739 | |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 2740 | // InitListExpr |
Ted Kremenek | ba7bc55 | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 2741 | Stmt::child_iterator InitListExpr::child_begin() { |
| 2742 | return InitExprs.size() ? &InitExprs[0] : 0; |
| 2743 | } |
| 2744 | Stmt::child_iterator InitListExpr::child_end() { |
| 2745 | return InitExprs.size() ? &InitExprs[0] + InitExprs.size() : 0; |
| 2746 | } |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 2747 | |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 2748 | // DesignatedInitExpr |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2749 | Stmt::child_iterator DesignatedInitExpr::child_begin() { |
| 2750 | char* Ptr = static_cast<char*>(static_cast<void *>(this)); |
| 2751 | Ptr += sizeof(DesignatedInitExpr); |
Douglas Gregor | 05c13a3 | 2009-01-22 00:58:24 +0000 | [diff] [blame] | 2752 | return reinterpret_cast<Stmt**>(reinterpret_cast<void**>(Ptr)); |
| 2753 | } |
| 2754 | Stmt::child_iterator DesignatedInitExpr::child_end() { |
| 2755 | return child_iterator(&*child_begin() + NumSubExprs); |
| 2756 | } |
| 2757 | |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 2758 | // ImplicitValueInitExpr |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2759 | Stmt::child_iterator ImplicitValueInitExpr::child_begin() { |
| 2760 | return child_iterator(); |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 2761 | } |
| 2762 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2763 | Stmt::child_iterator ImplicitValueInitExpr::child_end() { |
| 2764 | return child_iterator(); |
Douglas Gregor | 3498bdb | 2009-01-29 17:44:32 +0000 | [diff] [blame] | 2765 | } |
| 2766 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 2767 | // ParenListExpr |
| 2768 | Stmt::child_iterator ParenListExpr::child_begin() { |
| 2769 | return &Exprs[0]; |
| 2770 | } |
| 2771 | Stmt::child_iterator ParenListExpr::child_end() { |
| 2772 | return &Exprs[0]+NumExprs; |
| 2773 | } |
| 2774 | |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 2775 | // ObjCStringLiteral |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2776 | Stmt::child_iterator ObjCStringLiteral::child_begin() { |
Chris Lattner | c6c16af | 2009-02-18 06:53:08 +0000 | [diff] [blame] | 2777 | return &String; |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 2778 | } |
| 2779 | Stmt::child_iterator ObjCStringLiteral::child_end() { |
Chris Lattner | c6c16af | 2009-02-18 06:53:08 +0000 | [diff] [blame] | 2780 | return &String+1; |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 2781 | } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 2782 | |
| 2783 | // ObjCEncodeExpr |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 2784 | Stmt::child_iterator ObjCEncodeExpr::child_begin() { return child_iterator(); } |
| 2785 | Stmt::child_iterator ObjCEncodeExpr::child_end() { return child_iterator(); } |
Ted Kremenek | 1237c67 | 2007-08-24 20:06:47 +0000 | [diff] [blame] | 2786 | |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 2787 | // ObjCSelectorExpr |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2788 | Stmt::child_iterator ObjCSelectorExpr::child_begin() { |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 2789 | return child_iterator(); |
| 2790 | } |
| 2791 | Stmt::child_iterator ObjCSelectorExpr::child_end() { |
| 2792 | return child_iterator(); |
| 2793 | } |
Fariborz Jahanian | b62f681 | 2007-10-16 20:40:23 +0000 | [diff] [blame] | 2794 | |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 2795 | // ObjCProtocolExpr |
Ted Kremenek | 9ac5928 | 2007-10-18 23:28:49 +0000 | [diff] [blame] | 2796 | Stmt::child_iterator ObjCProtocolExpr::child_begin() { |
| 2797 | return child_iterator(); |
| 2798 | } |
| 2799 | Stmt::child_iterator ObjCProtocolExpr::child_end() { |
| 2800 | return child_iterator(); |
| 2801 | } |
Fariborz Jahanian | 390d50a | 2007-10-17 16:58:11 +0000 | [diff] [blame] | 2802 | |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 2803 | // ObjCMessageExpr |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2804 | Stmt::child_iterator ObjCMessageExpr::child_begin() { |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2805 | if (getReceiverKind() == Instance) |
| 2806 | return reinterpret_cast<Stmt **>(this + 1); |
| 2807 | return getArgs(); |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 2808 | } |
| 2809 | Stmt::child_iterator ObjCMessageExpr::child_end() { |
Douglas Gregor | 04badcf | 2010-04-21 00:45:42 +0000 | [diff] [blame] | 2810 | return getArgs() + getNumArgs(); |
Steve Naroff | 563477d | 2007-09-18 23:55:05 +0000 | [diff] [blame] | 2811 | } |
| 2812 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 2813 | // Blocks |
Steve Naroff | 56ee689 | 2008-10-08 17:01:13 +0000 | [diff] [blame] | 2814 | Stmt::child_iterator BlockExpr::child_begin() { return child_iterator(); } |
| 2815 | Stmt::child_iterator BlockExpr::child_end() { return child_iterator(); } |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 2816 | |
Ted Kremenek | 9da13f9 | 2008-09-26 23:24:14 +0000 | [diff] [blame] | 2817 | Stmt::child_iterator BlockDeclRefExpr::child_begin() { return child_iterator();} |
| 2818 | Stmt::child_iterator BlockDeclRefExpr::child_end() { return child_iterator(); } |
John McCall | 7cd7d1a | 2010-11-15 23:31:06 +0000 | [diff] [blame] | 2819 | |
| 2820 | // OpaqueValueExpr |
| 2821 | SourceRange OpaqueValueExpr::getSourceRange() const { return SourceRange(); } |
| 2822 | Stmt::child_iterator OpaqueValueExpr::child_begin() { return child_iterator(); } |
| 2823 | Stmt::child_iterator OpaqueValueExpr::child_end() { return child_iterator(); } |
| 2824 | |