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