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