| Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 1 | //===--- SemaExpr.cpp - Semantic Analysis for Expressions -----------------===// |
| 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 | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for expressions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| John McCall | 8302463 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
| Douglas Gregor | c3a6ade | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 15 | #include "clang/Sema/Initialization.h" |
| 16 | #include "clang/Sema/Lookup.h" |
| 17 | #include "clang/Sema/AnalysisBasedWarnings.h" |
| Chris Lattner | cb6a382 | 2006-11-10 06:20:45 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
| Sebastian Redl | 2ac2c72 | 2011-04-29 08:19:30 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTMutationListener.h" |
| Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 20 | #include "clang/AST/CXXInheritance.h" |
| Daniel Dunbar | 6e8aa53 | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclObjC.h" |
| Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclTemplate.h" |
| Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 23 | #include "clang/AST/EvaluatedExprVisitor.h" |
| Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 24 | #include "clang/AST/Expr.h" |
| Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 25 | #include "clang/AST/ExprCXX.h" |
| Steve Naroff | 021ca18 | 2008-05-29 21:12:08 +0000 | [diff] [blame] | 26 | #include "clang/AST/ExprObjC.h" |
| Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 27 | #include "clang/AST/RecursiveASTVisitor.h" |
| Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 28 | #include "clang/AST/TypeLoc.h" |
| Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 29 | #include "clang/Basic/PartialDiagnostic.h" |
| Steve Naroff | f2fb89e | 2007-03-13 20:29:44 +0000 | [diff] [blame] | 30 | #include "clang/Basic/SourceManager.h" |
| Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 31 | #include "clang/Basic/TargetInfo.h" |
| Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 32 | #include "clang/Lex/LiteralSupport.h" |
| 33 | #include "clang/Lex/Preprocessor.h" |
| John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 34 | #include "clang/Sema/DeclSpec.h" |
| 35 | #include "clang/Sema/Designator.h" |
| 36 | #include "clang/Sema/Scope.h" |
| John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 37 | #include "clang/Sema/ScopeInfo.h" |
| John McCall | 8b0666c | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 38 | #include "clang/Sema/ParsedTemplate.h" |
| John McCall | de6836a | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 39 | #include "clang/Sema/Template.h" |
| Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 40 | using namespace clang; |
| John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 41 | using namespace sema; |
| Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 42 | |
| David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 43 | |
| Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 44 | /// \brief Determine whether the use of this declaration is valid, and |
| 45 | /// emit any corresponding diagnostics. |
| 46 | /// |
| 47 | /// This routine diagnoses various problems with referencing |
| 48 | /// declarations that can occur when using a declaration. For example, |
| 49 | /// it might warn if a deprecated or unavailable declaration is being |
| 50 | /// used, or produce an error (and return true) if a C++0x deleted |
| 51 | /// function is being used. |
| 52 | /// |
| Fariborz Jahanian | 7d6e11a | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 53 | /// If IgnoreDeprecated is set to true, this should not warn about deprecated |
| Chris Lattner | b7df3c6 | 2009-10-25 22:31:57 +0000 | [diff] [blame] | 54 | /// decls. |
| 55 | /// |
| Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 56 | /// \returns true if there was an error (this declaration cannot be |
| 57 | /// referenced), false otherwise. |
| Chris Lattner | b7df3c6 | 2009-10-25 22:31:57 +0000 | [diff] [blame] | 58 | /// |
| Fariborz Jahanian | 7d6e11a | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 59 | bool Sema::DiagnoseUseOfDecl(NamedDecl *D, SourceLocation Loc, |
| Fariborz Jahanian | dbbdd2f | 2011-04-23 17:27:19 +0000 | [diff] [blame] | 60 | const ObjCInterfaceDecl *UnknownObjCClass) { |
| Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 61 | if (getLangOptions().CPlusPlus && isa<FunctionDecl>(D)) { |
| 62 | // If there were any diagnostics suppressed by template argument deduction, |
| 63 | // emit them now. |
| 64 | llvm::DenseMap<Decl *, llvm::SmallVector<PartialDiagnosticAt, 1> >::iterator |
| 65 | Pos = SuppressedDiagnostics.find(D->getCanonicalDecl()); |
| 66 | if (Pos != SuppressedDiagnostics.end()) { |
| 67 | llvm::SmallVectorImpl<PartialDiagnosticAt> &Suppressed = Pos->second; |
| 68 | for (unsigned I = 0, N = Suppressed.size(); I != N; ++I) |
| 69 | Diag(Suppressed[I].first, Suppressed[I].second); |
| 70 | |
| 71 | // Clear out the list of suppressed diagnostics, so that we don't emit |
| Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 72 | // them again for this specialization. However, we don't obsolete this |
| Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 73 | // entry from the table, because we want to avoid ever emitting these |
| 74 | // diagnostics again. |
| 75 | Suppressed.clear(); |
| 76 | } |
| 77 | } |
| 78 | |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 79 | // See if this is an auto-typed variable whose initializer we are parsing. |
| Richard Smith | b2bc2e6 | 2011-02-21 20:05:19 +0000 | [diff] [blame] | 80 | if (ParsingInitForAutoVars.count(D)) { |
| 81 | Diag(Loc, diag::err_auto_variable_cannot_appear_in_own_initializer) |
| 82 | << D->getDeclName(); |
| 83 | return true; |
| Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 86 | // See if this is a deleted function. |
| Douglas Gregor | de681d4 | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 87 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 88 | if (FD->isDeleted()) { |
| 89 | Diag(Loc, diag::err_deleted_function_use); |
| 90 | Diag(D->getLocation(), diag::note_unavailable_here) << true; |
| 91 | return true; |
| 92 | } |
| Douglas Gregor | de681d4 | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 93 | } |
| Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 94 | |
| Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 95 | // See if this declaration is unavailable or deprecated. |
| 96 | std::string Message; |
| 97 | switch (D->getAvailability(&Message)) { |
| 98 | case AR_Available: |
| 99 | case AR_NotYetIntroduced: |
| 100 | break; |
| 101 | |
| 102 | case AR_Deprecated: |
| 103 | EmitDeprecationWarning(D, Message, Loc, UnknownObjCClass); |
| 104 | break; |
| 105 | |
| 106 | case AR_Unavailable: |
| 107 | if (Message.empty()) { |
| 108 | if (!UnknownObjCClass) |
| 109 | Diag(Loc, diag::err_unavailable) << D->getDeclName(); |
| 110 | else |
| 111 | Diag(Loc, diag::warn_unavailable_fwdclass_message) |
| 112 | << D->getDeclName(); |
| 113 | } |
| 114 | else |
| 115 | Diag(Loc, diag::err_unavailable_message) |
| 116 | << D->getDeclName() << Message; |
| 117 | Diag(D->getLocation(), diag::note_unavailable_here) << 0; |
| 118 | break; |
| 119 | } |
| 120 | |
| Anders Carlsson | 73067a0 | 2010-10-22 23:37:08 +0000 | [diff] [blame] | 121 | // Warn if this is used but marked unused. |
| 122 | if (D->hasAttr<UnusedAttr>()) |
| 123 | Diag(Loc, diag::warn_used_but_marked_unused) << D->getDeclName(); |
| 124 | |
| Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 125 | return false; |
| Chris Lattner | 4bf74fd | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 126 | } |
| 127 | |
| Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 128 | /// \brief Retrieve the message suffix that should be added to a |
| 129 | /// diagnostic complaining about the given function being deleted or |
| 130 | /// unavailable. |
| 131 | std::string Sema::getDeletedOrUnavailableSuffix(const FunctionDecl *FD) { |
| 132 | // FIXME: C++0x implicitly-deleted special member functions could be |
| 133 | // detected here so that we could improve diagnostics to say, e.g., |
| 134 | // "base class 'A' had a deleted copy constructor". |
| 135 | if (FD->isDeleted()) |
| 136 | return std::string(); |
| 137 | |
| 138 | std::string Message; |
| 139 | if (FD->getAvailability(&Message)) |
| 140 | return ": " + Message; |
| 141 | |
| 142 | return std::string(); |
| 143 | } |
| 144 | |
| Fariborz Jahanian | 027b886 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 145 | /// DiagnoseSentinelCalls - This routine checks on method dispatch calls |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 146 | /// (and other functions in future), which have been declared with sentinel |
| Fariborz Jahanian | 027b886 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 147 | /// attribute. It warns if call does not have the sentinel argument. |
| 148 | /// |
| 149 | void Sema::DiagnoseSentinelCalls(NamedDecl *D, SourceLocation Loc, |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 150 | Expr **Args, unsigned NumArgs) { |
| Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 151 | const SentinelAttr *attr = D->getAttr<SentinelAttr>(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 152 | if (!attr) |
| Fariborz Jahanian | 9e87721 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 153 | return; |
| Douglas Gregor | c298ffc | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 154 | |
| 155 | // FIXME: In C++0x, if any of the arguments are parameter pack |
| 156 | // expansions, we can't check for the sentinel now. |
| Fariborz Jahanian | 9e87721 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 157 | int sentinelPos = attr->getSentinel(); |
| 158 | int nullPos = attr->getNullPos(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 159 | |
| Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 160 | // FIXME. ObjCMethodDecl and FunctionDecl need be derived from the same common |
| 161 | // base class. Then we won't be needing two versions of the same code. |
| Fariborz Jahanian | 9e87721 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 162 | unsigned int i = 0; |
| Fariborz Jahanian | 4a52803 | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 163 | bool warnNotEnoughArgs = false; |
| 164 | int isMethod = 0; |
| 165 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 166 | // skip over named parameters. |
| 167 | ObjCMethodDecl::param_iterator P, E = MD->param_end(); |
| 168 | for (P = MD->param_begin(); (P != E && i < NumArgs); ++P) { |
| 169 | if (nullPos) |
| 170 | --nullPos; |
| 171 | else |
| 172 | ++i; |
| 173 | } |
| 174 | warnNotEnoughArgs = (P != E || i >= NumArgs); |
| 175 | isMethod = 1; |
| Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 176 | } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| Fariborz Jahanian | 4a52803 | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 177 | // skip over named parameters. |
| 178 | ObjCMethodDecl::param_iterator P, E = FD->param_end(); |
| 179 | for (P = FD->param_begin(); (P != E && i < NumArgs); ++P) { |
| 180 | if (nullPos) |
| 181 | --nullPos; |
| 182 | else |
| 183 | ++i; |
| 184 | } |
| 185 | warnNotEnoughArgs = (P != E || i >= NumArgs); |
| Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 186 | } else if (VarDecl *V = dyn_cast<VarDecl>(D)) { |
| Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 187 | // block or function pointer call. |
| 188 | QualType Ty = V->getType(); |
| 189 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 190 | const FunctionType *FT = Ty->isFunctionPointerType() |
| John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 191 | ? Ty->getAs<PointerType>()->getPointeeType()->getAs<FunctionType>() |
| 192 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
| Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 193 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT)) { |
| 194 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 195 | unsigned k; |
| 196 | for (k = 0; (k != NumArgsInProto && i < NumArgs); k++) { |
| 197 | if (nullPos) |
| 198 | --nullPos; |
| 199 | else |
| 200 | ++i; |
| 201 | } |
| 202 | warnNotEnoughArgs = (k != NumArgsInProto || i >= NumArgs); |
| 203 | } |
| 204 | if (Ty->isBlockPointerType()) |
| 205 | isMethod = 2; |
| Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 206 | } else |
| Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 207 | return; |
| Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 208 | } else |
| Fariborz Jahanian | 4a52803 | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 209 | return; |
| 210 | |
| 211 | if (warnNotEnoughArgs) { |
| Fariborz Jahanian | 9e87721 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 212 | Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName(); |
| Fariborz Jahanian | 4a52803 | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 213 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
| Fariborz Jahanian | 9e87721 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 214 | return; |
| 215 | } |
| 216 | int sentinel = i; |
| 217 | while (sentinelPos > 0 && i < NumArgs-1) { |
| 218 | --sentinelPos; |
| 219 | ++i; |
| 220 | } |
| 221 | if (sentinelPos > 0) { |
| 222 | Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName(); |
| Fariborz Jahanian | 4a52803 | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 223 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
| Fariborz Jahanian | 9e87721 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 224 | return; |
| 225 | } |
| 226 | while (i < NumArgs-1) { |
| 227 | ++i; |
| 228 | ++sentinel; |
| 229 | } |
| 230 | Expr *sentinelExpr = Args[sentinel]; |
| John McCall | 7ddbcf4 | 2010-05-06 23:53:00 +0000 | [diff] [blame] | 231 | if (!sentinelExpr) return; |
| 232 | if (sentinelExpr->isTypeDependent()) return; |
| 233 | if (sentinelExpr->isValueDependent()) return; |
| Anders Carlsson | e981a8c | 2010-11-05 15:21:33 +0000 | [diff] [blame] | 234 | |
| 235 | // nullptr_t is always treated as null. |
| 236 | if (sentinelExpr->getType()->isNullPtrType()) return; |
| 237 | |
| Fariborz Jahanian | c0b0ced | 2010-07-14 16:37:51 +0000 | [diff] [blame] | 238 | if (sentinelExpr->getType()->isAnyPointerType() && |
| John McCall | 7ddbcf4 | 2010-05-06 23:53:00 +0000 | [diff] [blame] | 239 | sentinelExpr->IgnoreParenCasts()->isNullPointerConstant(Context, |
| 240 | Expr::NPC_ValueDependentIsNull)) |
| 241 | return; |
| 242 | |
| 243 | // Unfortunately, __null has type 'int'. |
| 244 | if (isa<GNUNullExpr>(sentinelExpr)) return; |
| 245 | |
| 246 | Diag(Loc, diag::warn_missing_sentinel) << isMethod; |
| 247 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
| Fariborz Jahanian | 027b886 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 248 | } |
| 249 | |
| Douglas Gregor | 87f95b0 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 250 | SourceRange Sema::getExprRange(ExprTy *E) const { |
| 251 | Expr *Ex = (Expr *)E; |
| 252 | return Ex? Ex->getSourceRange() : SourceRange(); |
| 253 | } |
| 254 | |
| Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 255 | //===----------------------------------------------------------------------===// |
| 256 | // Standard Promotions and Conversions |
| 257 | //===----------------------------------------------------------------------===// |
| 258 | |
| Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 259 | /// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4). |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 260 | ExprResult Sema::DefaultFunctionArrayConversion(Expr *E) { |
| Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 261 | QualType Ty = E->getType(); |
| 262 | assert(!Ty.isNull() && "DefaultFunctionArrayConversion - missing type"); |
| 263 | |
| Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 264 | if (Ty->isFunctionType()) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 265 | E = ImpCastExprToType(E, Context.getPointerType(Ty), |
| 266 | CK_FunctionToPointerDecay).take(); |
| Chris Lattner | 61f60a0 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 267 | else if (Ty->isArrayType()) { |
| 268 | // In C90 mode, arrays only promote to pointers if the array expression is |
| 269 | // an lvalue. The relevant legalese is C90 6.2.2.1p3: "an lvalue that has |
| 270 | // type 'array of type' is converted to an expression that has type 'pointer |
| 271 | // to type'...". In C99 this was changed to: C99 6.3.2.1p3: "an expression |
| 272 | // that has type 'array of type' ...". The relevant change is "an lvalue" |
| 273 | // (C90) to "an expression" (C99). |
| Argyrios Kyrtzidis | 9321c74 | 2008-09-11 04:25:59 +0000 | [diff] [blame] | 274 | // |
| 275 | // C++ 4.2p1: |
| 276 | // An lvalue or rvalue of type "array of N T" or "array of unknown bound of |
| 277 | // T" can be converted to an rvalue of type "pointer to T". |
| 278 | // |
| John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 279 | if (getLangOptions().C99 || getLangOptions().CPlusPlus || E->isLValue()) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 280 | E = ImpCastExprToType(E, Context.getArrayDecayedType(Ty), |
| 281 | CK_ArrayToPointerDecay).take(); |
| Chris Lattner | 61f60a0 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 282 | } |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 283 | return Owned(E); |
| Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 284 | } |
| 285 | |
| Argyrios Kyrtzidis | a9b630e | 2011-04-26 17:41:22 +0000 | [diff] [blame] | 286 | static void CheckForNullPointerDereference(Sema &S, Expr *E) { |
| 287 | // Check to see if we are dereferencing a null pointer. If so, |
| 288 | // and if not volatile-qualified, this is undefined behavior that the |
| 289 | // optimizer will delete, so warn about it. People sometimes try to use this |
| 290 | // to get a deterministic trap and are surprised by clang's behavior. This |
| 291 | // only handles the pattern "*null", which is a very syntactic check. |
| 292 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E->IgnoreParenCasts())) |
| 293 | if (UO->getOpcode() == UO_Deref && |
| 294 | UO->getSubExpr()->IgnoreParenCasts()-> |
| 295 | isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull) && |
| 296 | !UO->getType().isVolatileQualified()) { |
| 297 | S.DiagRuntimeBehavior(UO->getOperatorLoc(), UO, |
| 298 | S.PDiag(diag::warn_indirection_through_null) |
| 299 | << UO->getSubExpr()->getSourceRange()); |
| 300 | S.DiagRuntimeBehavior(UO->getOperatorLoc(), UO, |
| 301 | S.PDiag(diag::note_indirection_through_null)); |
| 302 | } |
| 303 | } |
| 304 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 305 | ExprResult Sema::DefaultLvalueConversion(Expr *E) { |
| John McCall | f3735e0 | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 306 | // C++ [conv.lval]p1: |
| 307 | // A glvalue of a non-function, non-array type T can be |
| 308 | // converted to a prvalue. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 309 | if (!E->isGLValue()) return Owned(E); |
| John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 310 | |
| John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 311 | QualType T = E->getType(); |
| 312 | assert(!T.isNull() && "r-value conversion on typeless expression?"); |
| John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 313 | |
| John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 314 | // Create a load out of an ObjCProperty l-value, if necessary. |
| 315 | if (E->getObjectKind() == OK_ObjCProperty) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 316 | ExprResult Res = ConvertPropertyForRValue(E); |
| 317 | if (Res.isInvalid()) |
| 318 | return Owned(E); |
| 319 | E = Res.take(); |
| John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 320 | if (!E->isGLValue()) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 321 | return Owned(E); |
| Douglas Gregor | b92a156 | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 322 | } |
| John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 323 | |
| 324 | // We don't want to throw lvalue-to-rvalue casts on top of |
| 325 | // expressions of certain types in C++. |
| 326 | if (getLangOptions().CPlusPlus && |
| 327 | (E->getType() == Context.OverloadTy || |
| 328 | T->isDependentType() || |
| 329 | T->isRecordType())) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 330 | return Owned(E); |
| John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 331 | |
| 332 | // The C standard is actually really unclear on this point, and |
| 333 | // DR106 tells us what the result should be but not why. It's |
| 334 | // generally best to say that void types just doesn't undergo |
| 335 | // lvalue-to-rvalue at all. Note that expressions of unqualified |
| 336 | // 'void' type are never l-values, but qualified void can be. |
| 337 | if (T->isVoidType()) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 338 | return Owned(E); |
| John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 339 | |
| Argyrios Kyrtzidis | a9b630e | 2011-04-26 17:41:22 +0000 | [diff] [blame] | 340 | CheckForNullPointerDereference(*this, E); |
| 341 | |
| John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 342 | // C++ [conv.lval]p1: |
| 343 | // [...] If T is a non-class type, the type of the prvalue is the |
| 344 | // cv-unqualified version of T. Otherwise, the type of the |
| 345 | // rvalue is T. |
| 346 | // |
| 347 | // C99 6.3.2.1p2: |
| 348 | // If the lvalue has qualified type, the value has the unqualified |
| 349 | // version of the type of the lvalue; otherwise, the value has the |
| 350 | // type of the lvalue. |
| 351 | if (T.hasQualifiers()) |
| 352 | T = T.getUnqualifiedType(); |
| 353 | |
| Ted Kremenek | df26df7 | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 354 | CheckArrayAccess(E); |
| Ted Kremenek | 64699be | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 355 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 356 | return Owned(ImplicitCastExpr::Create(Context, T, CK_LValueToRValue, |
| 357 | E, 0, VK_RValue)); |
| John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 358 | } |
| 359 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 360 | ExprResult Sema::DefaultFunctionArrayLvalueConversion(Expr *E) { |
| 361 | ExprResult Res = DefaultFunctionArrayConversion(E); |
| 362 | if (Res.isInvalid()) |
| 363 | return ExprError(); |
| 364 | Res = DefaultLvalueConversion(Res.take()); |
| 365 | if (Res.isInvalid()) |
| 366 | return ExprError(); |
| 367 | return move(Res); |
| Douglas Gregor | b92a156 | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | |
| Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 371 | /// UsualUnaryConversions - Performs various conversions that are common to most |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 372 | /// operators (C99 6.3). The conversions of array and function types are |
| Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 373 | /// sometimes suppressed. For example, the array->pointer conversion doesn't |
| Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 374 | /// apply if the array is an argument to the sizeof or address (&) operators. |
| 375 | /// In these instances, this routine should *not* be called. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 376 | ExprResult Sema::UsualUnaryConversions(Expr *E) { |
| John McCall | f3735e0 | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 377 | // First, convert to an r-value. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 378 | ExprResult Res = DefaultFunctionArrayLvalueConversion(E); |
| 379 | if (Res.isInvalid()) |
| 380 | return Owned(E); |
| 381 | E = Res.take(); |
| John McCall | f3735e0 | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 382 | |
| 383 | QualType Ty = E->getType(); |
| Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 384 | assert(!Ty.isNull() && "UsualUnaryConversions - missing type"); |
| John McCall | f3735e0 | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 385 | |
| 386 | // Try to perform integral promotions if the object has a theoretically |
| 387 | // promotable type. |
| 388 | if (Ty->isIntegralOrUnscopedEnumerationType()) { |
| 389 | // C99 6.3.1.1p2: |
| 390 | // |
| 391 | // The following may be used in an expression wherever an int or |
| 392 | // unsigned int may be used: |
| 393 | // - an object or expression with an integer type whose integer |
| 394 | // conversion rank is less than or equal to the rank of int |
| 395 | // and unsigned int. |
| 396 | // - A bit-field of type _Bool, int, signed int, or unsigned int. |
| 397 | // |
| 398 | // If an int can represent all values of the original type, the |
| 399 | // value is converted to an int; otherwise, it is converted to an |
| 400 | // unsigned int. These are called the integer promotions. All |
| 401 | // other types are unchanged by the integer promotions. |
| 402 | |
| 403 | QualType PTy = Context.isPromotableBitField(E); |
| 404 | if (!PTy.isNull()) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 405 | E = ImpCastExprToType(E, PTy, CK_IntegralCast).take(); |
| 406 | return Owned(E); |
| John McCall | f3735e0 | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 407 | } |
| 408 | if (Ty->isPromotableIntegerType()) { |
| 409 | QualType PT = Context.getPromotedIntegerType(Ty); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 410 | E = ImpCastExprToType(E, PT, CK_IntegralCast).take(); |
| 411 | return Owned(E); |
| John McCall | f3735e0 | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 412 | } |
| Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 413 | } |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 414 | return Owned(E); |
| Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 415 | } |
| 416 | |
| Chris Lattner | 2ce500f | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 417 | /// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 418 | /// do not have a prototype. Arguments that have type float are promoted to |
| Chris Lattner | 2ce500f | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 419 | /// double. All other argument types are converted by UsualUnaryConversions(). |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 420 | ExprResult Sema::DefaultArgumentPromotion(Expr *E) { |
| 421 | QualType Ty = E->getType(); |
| Chris Lattner | 2ce500f | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 422 | assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type"); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 423 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 424 | ExprResult Res = UsualUnaryConversions(E); |
| 425 | if (Res.isInvalid()) |
| 426 | return Owned(E); |
| 427 | E = Res.take(); |
| John McCall | 9bc2677 | 2010-12-06 18:36:11 +0000 | [diff] [blame] | 428 | |
| Chris Lattner | 2ce500f | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 429 | // If this is a 'float' (CVR qualified or typedef) promote to double. |
| Chris Lattner | bb53efb | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 430 | if (Ty->isSpecificBuiltinType(BuiltinType::Float)) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 431 | E = ImpCastExprToType(E, Context.DoubleTy, CK_FloatingCast).take(); |
| 432 | |
| 433 | return Owned(E); |
| Chris Lattner | 2ce500f | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 434 | } |
| 435 | |
| Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 436 | /// DefaultVariadicArgumentPromotion - Like DefaultArgumentPromotion, but |
| 437 | /// will warn if the resulting type is not a POD type, and rejects ObjC |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 438 | /// interfaces passed by value. |
| 439 | ExprResult Sema::DefaultVariadicArgumentPromotion(Expr *E, VariadicCallType CT, |
| Chris Lattner | bb53efb | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 440 | FunctionDecl *FDecl) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 441 | ExprResult ExprRes = DefaultArgumentPromotion(E); |
| 442 | if (ExprRes.isInvalid()) |
| 443 | return ExprError(); |
| 444 | E = ExprRes.take(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 445 | |
| Chris Lattner | bb53efb | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 446 | // __builtin_va_start takes the second argument as a "varargs" argument, but |
| 447 | // it doesn't actually do anything with it. It doesn't need to be non-pod |
| 448 | // etc. |
| 449 | if (FDecl && FDecl->getBuiltinID() == Builtin::BI__builtin_va_start) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 450 | return Owned(E); |
| Chris Lattner | bb53efb | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 451 | |
| Douglas Gregor | 347e0f2 | 2011-05-21 19:26:31 +0000 | [diff] [blame] | 452 | // Don't allow one to pass an Objective-C interface to a vararg. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 453 | if (E->getType()->isObjCObjectType() && |
| Douglas Gregor | 347e0f2 | 2011-05-21 19:26:31 +0000 | [diff] [blame] | 454 | DiagRuntimeBehavior(E->getLocStart(), 0, |
| 455 | PDiag(diag::err_cannot_pass_objc_interface_to_vararg) |
| 456 | << E->getType() << CT)) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 457 | return ExprError(); |
| Douglas Gregor | 347e0f2 | 2011-05-21 19:26:31 +0000 | [diff] [blame] | 458 | |
| Douglas Gregor | 253cadf | 2011-05-21 16:27:21 +0000 | [diff] [blame] | 459 | if (!E->getType()->isPODType()) { |
| 460 | // C++0x [expr.call]p7: |
| 461 | // Passing a potentially-evaluated argument of class type (Clause 9) |
| 462 | // having a non-trivial copy constructor, a non-trivial move constructor, |
| 463 | // or a non-trivial destructor, with no corresponding parameter, |
| 464 | // is conditionally-supported with implementation-defined semantics. |
| 465 | bool TrivialEnough = false; |
| 466 | if (getLangOptions().CPlusPlus0x && !E->getType()->isDependentType()) { |
| 467 | if (CXXRecordDecl *Record = E->getType()->getAsCXXRecordDecl()) { |
| 468 | if (Record->hasTrivialCopyConstructor() && |
| 469 | Record->hasTrivialMoveConstructor() && |
| 470 | Record->hasTrivialDestructor()) |
| 471 | TrivialEnough = true; |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | if (TrivialEnough) { |
| 476 | // Nothing to diagnose. This is okay. |
| 477 | } else if (DiagRuntimeBehavior(E->getLocStart(), 0, |
| Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 478 | PDiag(diag::warn_cannot_pass_non_pod_arg_to_vararg) |
| Douglas Gregor | 253cadf | 2011-05-21 16:27:21 +0000 | [diff] [blame] | 479 | << getLangOptions().CPlusPlus0x << E->getType() |
| Douglas Gregor | 347e0f2 | 2011-05-21 19:26:31 +0000 | [diff] [blame] | 480 | << CT)) { |
| 481 | // Turn this into a trap. |
| 482 | CXXScopeSpec SS; |
| 483 | UnqualifiedId Name; |
| 484 | Name.setIdentifier(PP.getIdentifierInfo("__builtin_trap"), |
| 485 | E->getLocStart()); |
| 486 | ExprResult TrapFn = ActOnIdExpression(TUScope, SS, Name, true, false); |
| 487 | if (TrapFn.isInvalid()) |
| 488 | return ExprError(); |
| 489 | |
| 490 | ExprResult Call = ActOnCallExpr(TUScope, TrapFn.get(), E->getLocStart(), |
| 491 | MultiExprArg(), E->getLocEnd()); |
| 492 | if (Call.isInvalid()) |
| 493 | return ExprError(); |
| 494 | |
| 495 | ExprResult Comma = ActOnBinOp(TUScope, E->getLocStart(), tok::comma, |
| 496 | Call.get(), E); |
| 497 | if (Comma.isInvalid()) |
| 498 | return ExprError(); |
| 499 | |
| 500 | E = Comma.get(); |
| 501 | } |
| Douglas Gregor | 253cadf | 2011-05-21 16:27:21 +0000 | [diff] [blame] | 502 | } |
| 503 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 504 | return Owned(E); |
| Anders Carlsson | a7d069d | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 505 | } |
| 506 | |
| Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 507 | /// UsualArithmeticConversions - Performs various conversions that are common to |
| 508 | /// binary operators (C99 6.3.1.8). If both operands aren't arithmetic, this |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 509 | /// routine returns the first non-arithmetic type found. The client is |
| Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 510 | /// responsible for emitting appropriate error diagnostics. |
| 511 | /// FIXME: verify the conversion rules for "complex int" are consistent with |
| 512 | /// GCC. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 513 | QualType Sema::UsualArithmeticConversions(ExprResult &lhsExpr, ExprResult &rhsExpr, |
| Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 514 | bool isCompAssign) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 515 | if (!isCompAssign) { |
| 516 | lhsExpr = UsualUnaryConversions(lhsExpr.take()); |
| 517 | if (lhsExpr.isInvalid()) |
| 518 | return QualType(); |
| 519 | } |
| Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 520 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 521 | rhsExpr = UsualUnaryConversions(rhsExpr.take()); |
| 522 | if (rhsExpr.isInvalid()) |
| 523 | return QualType(); |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 524 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 525 | // For conversion purposes, we ignore any qualifiers. |
| Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 526 | // For example, "const float" and "float" are equivalent. |
| Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 527 | QualType lhs = |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 528 | Context.getCanonicalType(lhsExpr.get()->getType()).getUnqualifiedType(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 529 | QualType rhs = |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 530 | Context.getCanonicalType(rhsExpr.get()->getType()).getUnqualifiedType(); |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 531 | |
| 532 | // If both types are identical, no conversion is needed. |
| 533 | if (lhs == rhs) |
| 534 | return lhs; |
| 535 | |
| 536 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 537 | // The caller can deal with this (e.g. pointer + int). |
| 538 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
| 539 | return lhs; |
| 540 | |
| John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 541 | // Apply unary and bitfield promotions to the LHS's type. |
| 542 | QualType lhs_unpromoted = lhs; |
| 543 | if (lhs->isPromotableIntegerType()) |
| 544 | lhs = Context.getPromotedIntegerType(lhs); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 545 | QualType LHSBitfieldPromoteTy = Context.isPromotableBitField(lhsExpr.get()); |
| Douglas Gregor | d2c2d17 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 546 | if (!LHSBitfieldPromoteTy.isNull()) |
| 547 | lhs = LHSBitfieldPromoteTy; |
| John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 548 | if (lhs != lhs_unpromoted && !isCompAssign) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 549 | lhsExpr = ImpCastExprToType(lhsExpr.take(), lhs, CK_IntegralCast); |
| Douglas Gregor | d2c2d17 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 550 | |
| John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 551 | // If both types are identical, no conversion is needed. |
| 552 | if (lhs == rhs) |
| 553 | return lhs; |
| 554 | |
| 555 | // At this point, we have two different arithmetic types. |
| 556 | |
| 557 | // Handle complex types first (C99 6.3.1.8p1). |
| 558 | bool LHSComplexFloat = lhs->isComplexType(); |
| 559 | bool RHSComplexFloat = rhs->isComplexType(); |
| 560 | if (LHSComplexFloat || RHSComplexFloat) { |
| 561 | // if we have an integer operand, the result is the complex type. |
| 562 | |
| John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 563 | if (!RHSComplexFloat && !rhs->isRealFloatingType()) { |
| 564 | if (rhs->isIntegerType()) { |
| 565 | QualType fp = cast<ComplexType>(lhs)->getElementType(); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 566 | rhsExpr = ImpCastExprToType(rhsExpr.take(), fp, CK_IntegralToFloating); |
| 567 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_FloatingRealToComplex); |
| John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 568 | } else { |
| 569 | assert(rhs->isComplexIntegerType()); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 570 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralComplexToFloatingComplex); |
| John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 571 | } |
| John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 572 | return lhs; |
| 573 | } |
| 574 | |
| John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 575 | if (!LHSComplexFloat && !lhs->isRealFloatingType()) { |
| 576 | if (!isCompAssign) { |
| 577 | // int -> float -> _Complex float |
| 578 | if (lhs->isIntegerType()) { |
| 579 | QualType fp = cast<ComplexType>(rhs)->getElementType(); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 580 | lhsExpr = ImpCastExprToType(lhsExpr.take(), fp, CK_IntegralToFloating); |
| 581 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_FloatingRealToComplex); |
| John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 582 | } else { |
| 583 | assert(lhs->isComplexIntegerType()); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 584 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralComplexToFloatingComplex); |
| John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 585 | } |
| 586 | } |
| John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 587 | return rhs; |
| 588 | } |
| 589 | |
| 590 | // This handles complex/complex, complex/float, or float/complex. |
| 591 | // When both operands are complex, the shorter operand is converted to the |
| 592 | // type of the longer, and that is the type of the result. This corresponds |
| 593 | // to what is done when combining two real floating-point operands. |
| 594 | // The fun begins when size promotion occur across type domains. |
| 595 | // From H&S 6.3.4: When one operand is complex and the other is a real |
| 596 | // floating-point type, the less precise type is converted, within it's |
| 597 | // real or complex domain, to the precision of the other type. For example, |
| 598 | // when combining a "long double" with a "double _Complex", the |
| 599 | // "double _Complex" is promoted to "long double _Complex". |
| 600 | int order = Context.getFloatingTypeOrder(lhs, rhs); |
| 601 | |
| 602 | // If both are complex, just cast to the more precise type. |
| 603 | if (LHSComplexFloat && RHSComplexFloat) { |
| 604 | if (order > 0) { |
| 605 | // _Complex float -> _Complex double |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 606 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_FloatingComplexCast); |
| John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 607 | return lhs; |
| 608 | |
| 609 | } else if (order < 0) { |
| 610 | // _Complex float -> _Complex double |
| 611 | if (!isCompAssign) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 612 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_FloatingComplexCast); |
| John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 613 | return rhs; |
| 614 | } |
| 615 | return lhs; |
| 616 | } |
| 617 | |
| 618 | // If just the LHS is complex, the RHS needs to be converted, |
| 619 | // and the LHS might need to be promoted. |
| 620 | if (LHSComplexFloat) { |
| 621 | if (order > 0) { // LHS is wider |
| 622 | // float -> _Complex double |
| John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 623 | QualType fp = cast<ComplexType>(lhs)->getElementType(); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 624 | rhsExpr = ImpCastExprToType(rhsExpr.take(), fp, CK_FloatingCast); |
| 625 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_FloatingRealToComplex); |
| John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 626 | return lhs; |
| 627 | } |
| 628 | |
| 629 | // RHS is at least as wide. Find its corresponding complex type. |
| 630 | QualType result = (order == 0 ? lhs : Context.getComplexType(rhs)); |
| 631 | |
| 632 | // double -> _Complex double |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 633 | rhsExpr = ImpCastExprToType(rhsExpr.take(), result, CK_FloatingRealToComplex); |
| John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 634 | |
| 635 | // _Complex float -> _Complex double |
| 636 | if (!isCompAssign && order < 0) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 637 | lhsExpr = ImpCastExprToType(lhsExpr.take(), result, CK_FloatingComplexCast); |
| John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 638 | |
| 639 | return result; |
| 640 | } |
| 641 | |
| 642 | // Just the RHS is complex, so the LHS needs to be converted |
| 643 | // and the RHS might need to be promoted. |
| 644 | assert(RHSComplexFloat); |
| 645 | |
| 646 | if (order < 0) { // RHS is wider |
| 647 | // float -> _Complex double |
| John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 648 | if (!isCompAssign) { |
| Argyrios Kyrtzidis | e84389b | 2011-01-18 18:49:33 +0000 | [diff] [blame] | 649 | QualType fp = cast<ComplexType>(rhs)->getElementType(); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 650 | lhsExpr = ImpCastExprToType(lhsExpr.take(), fp, CK_FloatingCast); |
| 651 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_FloatingRealToComplex); |
| John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 652 | } |
| John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 653 | return rhs; |
| 654 | } |
| 655 | |
| 656 | // LHS is at least as wide. Find its corresponding complex type. |
| 657 | QualType result = (order == 0 ? rhs : Context.getComplexType(lhs)); |
| 658 | |
| 659 | // double -> _Complex double |
| 660 | if (!isCompAssign) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 661 | lhsExpr = ImpCastExprToType(lhsExpr.take(), result, CK_FloatingRealToComplex); |
| John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 662 | |
| 663 | // _Complex float -> _Complex double |
| 664 | if (order > 0) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 665 | rhsExpr = ImpCastExprToType(rhsExpr.take(), result, CK_FloatingComplexCast); |
| John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 666 | |
| 667 | return result; |
| 668 | } |
| 669 | |
| 670 | // Now handle "real" floating types (i.e. float, double, long double). |
| 671 | bool LHSFloat = lhs->isRealFloatingType(); |
| 672 | bool RHSFloat = rhs->isRealFloatingType(); |
| 673 | if (LHSFloat || RHSFloat) { |
| 674 | // If we have two real floating types, convert the smaller operand |
| 675 | // to the bigger result. |
| 676 | if (LHSFloat && RHSFloat) { |
| 677 | int order = Context.getFloatingTypeOrder(lhs, rhs); |
| 678 | if (order > 0) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 679 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_FloatingCast); |
| John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 680 | return lhs; |
| 681 | } |
| 682 | |
| 683 | assert(order < 0 && "illegal float comparison"); |
| 684 | if (!isCompAssign) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 685 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_FloatingCast); |
| John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 686 | return rhs; |
| 687 | } |
| 688 | |
| 689 | // If we have an integer operand, the result is the real floating type. |
| 690 | if (LHSFloat) { |
| 691 | if (rhs->isIntegerType()) { |
| 692 | // Convert rhs to the lhs floating point type. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 693 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralToFloating); |
| John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 694 | return lhs; |
| 695 | } |
| 696 | |
| 697 | // Convert both sides to the appropriate complex float. |
| 698 | assert(rhs->isComplexIntegerType()); |
| 699 | QualType result = Context.getComplexType(lhs); |
| 700 | |
| 701 | // _Complex int -> _Complex float |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 702 | rhsExpr = ImpCastExprToType(rhsExpr.take(), result, CK_IntegralComplexToFloatingComplex); |
| John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 703 | |
| 704 | // float -> _Complex float |
| 705 | if (!isCompAssign) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 706 | lhsExpr = ImpCastExprToType(lhsExpr.take(), result, CK_FloatingRealToComplex); |
| John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 707 | |
| 708 | return result; |
| 709 | } |
| 710 | |
| 711 | assert(RHSFloat); |
| 712 | if (lhs->isIntegerType()) { |
| 713 | // Convert lhs to the rhs floating point type. |
| 714 | if (!isCompAssign) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 715 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralToFloating); |
| John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 716 | return rhs; |
| 717 | } |
| 718 | |
| 719 | // Convert both sides to the appropriate complex float. |
| 720 | assert(lhs->isComplexIntegerType()); |
| 721 | QualType result = Context.getComplexType(rhs); |
| 722 | |
| 723 | // _Complex int -> _Complex float |
| 724 | if (!isCompAssign) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 725 | lhsExpr = ImpCastExprToType(lhsExpr.take(), result, CK_IntegralComplexToFloatingComplex); |
| John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 726 | |
| 727 | // float -> _Complex float |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 728 | rhsExpr = ImpCastExprToType(rhsExpr.take(), result, CK_FloatingRealToComplex); |
| John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 729 | |
| 730 | return result; |
| 731 | } |
| 732 | |
| 733 | // Handle GCC complex int extension. |
| 734 | // FIXME: if the operands are (int, _Complex long), we currently |
| 735 | // don't promote the complex. Also, signedness? |
| 736 | const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType(); |
| 737 | const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType(); |
| 738 | if (lhsComplexInt && rhsComplexInt) { |
| 739 | int order = Context.getIntegerTypeOrder(lhsComplexInt->getElementType(), |
| 740 | rhsComplexInt->getElementType()); |
| 741 | assert(order && "inequal types with equal element ordering"); |
| 742 | if (order > 0) { |
| 743 | // _Complex int -> _Complex long |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 744 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralComplexCast); |
| John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 745 | return lhs; |
| 746 | } |
| 747 | |
| 748 | if (!isCompAssign) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 749 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralComplexCast); |
| John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 750 | return rhs; |
| 751 | } else if (lhsComplexInt) { |
| 752 | // int -> _Complex int |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 753 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralRealToComplex); |
| John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 754 | return lhs; |
| 755 | } else if (rhsComplexInt) { |
| 756 | // int -> _Complex int |
| 757 | if (!isCompAssign) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 758 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralRealToComplex); |
| John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 759 | return rhs; |
| 760 | } |
| 761 | |
| 762 | // Finally, we have two differing integer types. |
| 763 | // The rules for this case are in C99 6.3.1.8 |
| 764 | int compare = Context.getIntegerTypeOrder(lhs, rhs); |
| 765 | bool lhsSigned = lhs->hasSignedIntegerRepresentation(), |
| 766 | rhsSigned = rhs->hasSignedIntegerRepresentation(); |
| 767 | if (lhsSigned == rhsSigned) { |
| 768 | // Same signedness; use the higher-ranked type |
| 769 | if (compare >= 0) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 770 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralCast); |
| John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 771 | return lhs; |
| 772 | } else if (!isCompAssign) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 773 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralCast); |
| John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 774 | return rhs; |
| 775 | } else if (compare != (lhsSigned ? 1 : -1)) { |
| 776 | // The unsigned type has greater than or equal rank to the |
| 777 | // signed type, so use the unsigned type |
| 778 | if (rhsSigned) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 779 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralCast); |
| John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 780 | return lhs; |
| 781 | } else if (!isCompAssign) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 782 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralCast); |
| John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 783 | return rhs; |
| 784 | } else if (Context.getIntWidth(lhs) != Context.getIntWidth(rhs)) { |
| 785 | // The two types are different widths; if we are here, that |
| 786 | // means the signed type is larger than the unsigned type, so |
| 787 | // use the signed type. |
| 788 | if (lhsSigned) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 789 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralCast); |
| John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 790 | return lhs; |
| 791 | } else if (!isCompAssign) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 792 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralCast); |
| John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 793 | return rhs; |
| 794 | } else { |
| 795 | // The signed type is higher-ranked than the unsigned type, |
| 796 | // but isn't actually any bigger (like unsigned int and long |
| 797 | // on most 32-bit systems). Use the unsigned type corresponding |
| 798 | // to the signed type. |
| 799 | QualType result = |
| 800 | Context.getCorrespondingUnsignedType(lhsSigned ? lhs : rhs); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 801 | rhsExpr = ImpCastExprToType(rhsExpr.take(), result, CK_IntegralCast); |
| John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 802 | if (!isCompAssign) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 803 | lhsExpr = ImpCastExprToType(lhsExpr.take(), result, CK_IntegralCast); |
| John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 804 | return result; |
| 805 | } |
| Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 806 | } |
| 807 | |
| Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 808 | //===----------------------------------------------------------------------===// |
| 809 | // Semantic Analysis for various Expression Types |
| 810 | //===----------------------------------------------------------------------===// |
| 811 | |
| 812 | |
| Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 813 | ExprResult |
| 814 | Sema::ActOnGenericSelectionExpr(SourceLocation KeyLoc, |
| 815 | SourceLocation DefaultLoc, |
| 816 | SourceLocation RParenLoc, |
| 817 | Expr *ControllingExpr, |
| 818 | MultiTypeArg types, |
| 819 | MultiExprArg exprs) { |
| 820 | unsigned NumAssocs = types.size(); |
| 821 | assert(NumAssocs == exprs.size()); |
| 822 | |
| 823 | ParsedType *ParsedTypes = types.release(); |
| 824 | Expr **Exprs = exprs.release(); |
| 825 | |
| 826 | TypeSourceInfo **Types = new TypeSourceInfo*[NumAssocs]; |
| 827 | for (unsigned i = 0; i < NumAssocs; ++i) { |
| 828 | if (ParsedTypes[i]) |
| 829 | (void) GetTypeFromParser(ParsedTypes[i], &Types[i]); |
| 830 | else |
| 831 | Types[i] = 0; |
| 832 | } |
| 833 | |
| 834 | ExprResult ER = CreateGenericSelectionExpr(KeyLoc, DefaultLoc, RParenLoc, |
| 835 | ControllingExpr, Types, Exprs, |
| 836 | NumAssocs); |
| Benjamin Kramer | 3462376 | 2011-04-15 11:21:57 +0000 | [diff] [blame] | 837 | delete [] Types; |
| Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 838 | return ER; |
| 839 | } |
| 840 | |
| 841 | ExprResult |
| 842 | Sema::CreateGenericSelectionExpr(SourceLocation KeyLoc, |
| 843 | SourceLocation DefaultLoc, |
| 844 | SourceLocation RParenLoc, |
| 845 | Expr *ControllingExpr, |
| 846 | TypeSourceInfo **Types, |
| 847 | Expr **Exprs, |
| 848 | unsigned NumAssocs) { |
| 849 | bool TypeErrorFound = false, |
| 850 | IsResultDependent = ControllingExpr->isTypeDependent(), |
| 851 | ContainsUnexpandedParameterPack |
| 852 | = ControllingExpr->containsUnexpandedParameterPack(); |
| 853 | |
| 854 | for (unsigned i = 0; i < NumAssocs; ++i) { |
| 855 | if (Exprs[i]->containsUnexpandedParameterPack()) |
| 856 | ContainsUnexpandedParameterPack = true; |
| 857 | |
| 858 | if (Types[i]) { |
| 859 | if (Types[i]->getType()->containsUnexpandedParameterPack()) |
| 860 | ContainsUnexpandedParameterPack = true; |
| 861 | |
| 862 | if (Types[i]->getType()->isDependentType()) { |
| 863 | IsResultDependent = true; |
| 864 | } else { |
| 865 | // C1X 6.5.1.1p2 "The type name in a generic association shall specify a |
| 866 | // complete object type other than a variably modified type." |
| 867 | unsigned D = 0; |
| 868 | if (Types[i]->getType()->isIncompleteType()) |
| 869 | D = diag::err_assoc_type_incomplete; |
| 870 | else if (!Types[i]->getType()->isObjectType()) |
| 871 | D = diag::err_assoc_type_nonobject; |
| 872 | else if (Types[i]->getType()->isVariablyModifiedType()) |
| 873 | D = diag::err_assoc_type_variably_modified; |
| 874 | |
| 875 | if (D != 0) { |
| 876 | Diag(Types[i]->getTypeLoc().getBeginLoc(), D) |
| 877 | << Types[i]->getTypeLoc().getSourceRange() |
| 878 | << Types[i]->getType(); |
| 879 | TypeErrorFound = true; |
| 880 | } |
| 881 | |
| 882 | // C1X 6.5.1.1p2 "No two generic associations in the same generic |
| 883 | // selection shall specify compatible types." |
| 884 | for (unsigned j = i+1; j < NumAssocs; ++j) |
| 885 | if (Types[j] && !Types[j]->getType()->isDependentType() && |
| 886 | Context.typesAreCompatible(Types[i]->getType(), |
| 887 | Types[j]->getType())) { |
| 888 | Diag(Types[j]->getTypeLoc().getBeginLoc(), |
| 889 | diag::err_assoc_compatible_types) |
| 890 | << Types[j]->getTypeLoc().getSourceRange() |
| 891 | << Types[j]->getType() |
| 892 | << Types[i]->getType(); |
| 893 | Diag(Types[i]->getTypeLoc().getBeginLoc(), |
| 894 | diag::note_compat_assoc) |
| 895 | << Types[i]->getTypeLoc().getSourceRange() |
| 896 | << Types[i]->getType(); |
| 897 | TypeErrorFound = true; |
| 898 | } |
| 899 | } |
| 900 | } |
| 901 | } |
| 902 | if (TypeErrorFound) |
| 903 | return ExprError(); |
| 904 | |
| 905 | // If we determined that the generic selection is result-dependent, don't |
| 906 | // try to compute the result expression. |
| 907 | if (IsResultDependent) |
| 908 | return Owned(new (Context) GenericSelectionExpr( |
| 909 | Context, KeyLoc, ControllingExpr, |
| 910 | Types, Exprs, NumAssocs, DefaultLoc, |
| 911 | RParenLoc, ContainsUnexpandedParameterPack)); |
| 912 | |
| 913 | llvm::SmallVector<unsigned, 1> CompatIndices; |
| 914 | unsigned DefaultIndex = -1U; |
| 915 | for (unsigned i = 0; i < NumAssocs; ++i) { |
| 916 | if (!Types[i]) |
| 917 | DefaultIndex = i; |
| 918 | else if (Context.typesAreCompatible(ControllingExpr->getType(), |
| 919 | Types[i]->getType())) |
| 920 | CompatIndices.push_back(i); |
| 921 | } |
| 922 | |
| 923 | // C1X 6.5.1.1p2 "The controlling expression of a generic selection shall have |
| 924 | // type compatible with at most one of the types named in its generic |
| 925 | // association list." |
| 926 | if (CompatIndices.size() > 1) { |
| 927 | // We strip parens here because the controlling expression is typically |
| 928 | // parenthesized in macro definitions. |
| 929 | ControllingExpr = ControllingExpr->IgnoreParens(); |
| 930 | Diag(ControllingExpr->getLocStart(), diag::err_generic_sel_multi_match) |
| 931 | << ControllingExpr->getSourceRange() << ControllingExpr->getType() |
| 932 | << (unsigned) CompatIndices.size(); |
| 933 | for (llvm::SmallVector<unsigned, 1>::iterator I = CompatIndices.begin(), |
| 934 | E = CompatIndices.end(); I != E; ++I) { |
| 935 | Diag(Types[*I]->getTypeLoc().getBeginLoc(), |
| 936 | diag::note_compat_assoc) |
| 937 | << Types[*I]->getTypeLoc().getSourceRange() |
| 938 | << Types[*I]->getType(); |
| 939 | } |
| 940 | return ExprError(); |
| 941 | } |
| 942 | |
| 943 | // C1X 6.5.1.1p2 "If a generic selection has no default generic association, |
| 944 | // its controlling expression shall have type compatible with exactly one of |
| 945 | // the types named in its generic association list." |
| 946 | if (DefaultIndex == -1U && CompatIndices.size() == 0) { |
| 947 | // We strip parens here because the controlling expression is typically |
| 948 | // parenthesized in macro definitions. |
| 949 | ControllingExpr = ControllingExpr->IgnoreParens(); |
| 950 | Diag(ControllingExpr->getLocStart(), diag::err_generic_sel_no_match) |
| 951 | << ControllingExpr->getSourceRange() << ControllingExpr->getType(); |
| 952 | return ExprError(); |
| 953 | } |
| 954 | |
| 955 | // C1X 6.5.1.1p3 "If a generic selection has a generic association with a |
| 956 | // type name that is compatible with the type of the controlling expression, |
| 957 | // then the result expression of the generic selection is the expression |
| 958 | // in that generic association. Otherwise, the result expression of the |
| 959 | // generic selection is the expression in the default generic association." |
| 960 | unsigned ResultIndex = |
| 961 | CompatIndices.size() ? CompatIndices[0] : DefaultIndex; |
| 962 | |
| 963 | return Owned(new (Context) GenericSelectionExpr( |
| 964 | Context, KeyLoc, ControllingExpr, |
| 965 | Types, Exprs, NumAssocs, DefaultLoc, |
| 966 | RParenLoc, ContainsUnexpandedParameterPack, |
| 967 | ResultIndex)); |
| 968 | } |
| 969 | |
| Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 970 | /// ActOnStringLiteral - The specified tokens were lexed as pasted string |
| Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 971 | /// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string |
| 972 | /// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from |
| 973 | /// multiple tokens. However, the common case is that StringToks points to one |
| 974 | /// string. |
| Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 975 | /// |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 976 | ExprResult |
| Alexis Hunt | 3b79186 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 977 | Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) { |
| Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 978 | assert(NumStringToks && "Must have at least one string!"); |
| 979 | |
| Chris Lattner | 8a24e58 | 2009-01-16 18:51:42 +0000 | [diff] [blame] | 980 | StringLiteralParser Literal(StringToks, NumStringToks, PP); |
| Steve Naroff | 4f88b31 | 2007-03-13 22:37:02 +0000 | [diff] [blame] | 981 | if (Literal.hadError) |
| Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 982 | return ExprError(); |
| Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 983 | |
| Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 984 | llvm::SmallVector<SourceLocation, 4> StringTokLocs; |
| Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 985 | for (unsigned i = 0; i != NumStringToks; ++i) |
| 986 | StringTokLocs.push_back(StringToks[i].getLocation()); |
| Chris Lattner | 36fc879 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 987 | |
| Chris Lattner | 36fc879 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 988 | QualType StrTy = Context.CharTy; |
| Anders Carlsson | 6b06e18 | 2011-04-06 18:42:48 +0000 | [diff] [blame] | 989 | if (Literal.AnyWide) |
| 990 | StrTy = Context.getWCharType(); |
| 991 | else if (Literal.Pascal) |
| 992 | StrTy = Context.UnsignedCharTy; |
| Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 993 | |
| 994 | // A C++ string literal has a const-qualified element type (C++ 2.13.4p1). |
| Chris Lattner | a8687ae | 2010-06-15 18:05:34 +0000 | [diff] [blame] | 995 | if (getLangOptions().CPlusPlus || getLangOptions().ConstStrings) |
| Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 996 | StrTy.addConst(); |
| Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 997 | |
| Chris Lattner | 36fc879 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 998 | // Get an array type for the string, according to C99 6.4.5. This includes |
| 999 | // the nul terminator character as well as the string length for pascal |
| 1000 | // strings. |
| 1001 | StrTy = Context.getConstantArrayType(StrTy, |
| Chris Lattner | d42c29f | 2009-02-26 23:01:51 +0000 | [diff] [blame] | 1002 | llvm::APInt(32, Literal.GetNumStringChars()+1), |
| Chris Lattner | 36fc879 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 1003 | ArrayType::Normal, 0); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1004 | |
| Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 1005 | // Pass &StringTokLocs[0], StringTokLocs.size() to factory! |
| Alexis Hunt | 3b79186 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 1006 | return Owned(StringLiteral::Create(Context, Literal.GetString(), |
| 1007 | Literal.GetStringLength(), |
| Anders Carlsson | 7524540 | 2011-04-14 00:40:03 +0000 | [diff] [blame] | 1008 | Literal.AnyWide, Literal.Pascal, StrTy, |
| Alexis Hunt | 3b79186 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 1009 | &StringTokLocs[0], |
| 1010 | StringTokLocs.size())); |
| Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
| John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1013 | enum CaptureResult { |
| 1014 | /// No capture is required. |
| 1015 | CR_NoCapture, |
| 1016 | |
| 1017 | /// A capture is required. |
| 1018 | CR_Capture, |
| 1019 | |
| John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1020 | /// A by-ref capture is required. |
| 1021 | CR_CaptureByRef, |
| 1022 | |
| John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1023 | /// An error occurred when trying to capture the given variable. |
| 1024 | CR_Error |
| 1025 | }; |
| 1026 | |
| 1027 | /// Diagnose an uncapturable value reference. |
| Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1028 | /// |
| John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1029 | /// \param var - the variable referenced |
| 1030 | /// \param DC - the context which we couldn't capture through |
| 1031 | static CaptureResult |
| John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1032 | diagnoseUncapturableValueReference(Sema &S, SourceLocation loc, |
| John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1033 | VarDecl *var, DeclContext *DC) { |
| 1034 | switch (S.ExprEvalContexts.back().Context) { |
| 1035 | case Sema::Unevaluated: |
| 1036 | // The argument will never be evaluated, so don't complain. |
| 1037 | return CR_NoCapture; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1038 | |
| John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1039 | case Sema::PotentiallyEvaluated: |
| 1040 | case Sema::PotentiallyEvaluatedIfUsed: |
| 1041 | break; |
| Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1042 | |
| John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1043 | case Sema::PotentiallyPotentiallyEvaluated: |
| 1044 | // FIXME: delay these! |
| 1045 | break; |
| Chris Lattner | 497d7b0 | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 1046 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1047 | |
| John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1048 | // Don't diagnose about capture if we're not actually in code right |
| 1049 | // now; in general, there are more appropriate places that will |
| 1050 | // diagnose this. |
| 1051 | if (!S.CurContext->isFunctionOrMethod()) return CR_NoCapture; |
| 1052 | |
| John McCall | 92d627e | 2011-03-22 23:15:50 +0000 | [diff] [blame] | 1053 | // Certain madnesses can happen with parameter declarations, which |
| 1054 | // we want to ignore. |
| 1055 | if (isa<ParmVarDecl>(var)) { |
| 1056 | // - If the parameter still belongs to the translation unit, then |
| 1057 | // we're actually just using one parameter in the declaration of |
| 1058 | // the next. This is useful in e.g. VLAs. |
| 1059 | if (isa<TranslationUnitDecl>(var->getDeclContext())) |
| 1060 | return CR_NoCapture; |
| 1061 | |
| 1062 | // - This particular madness can happen in ill-formed default |
| 1063 | // arguments; claim it's okay and let downstream code handle it. |
| 1064 | if (S.CurContext == var->getDeclContext()->getParent()) |
| 1065 | return CR_NoCapture; |
| 1066 | } |
| John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1067 | |
| 1068 | DeclarationName functionName; |
| 1069 | if (FunctionDecl *fn = dyn_cast<FunctionDecl>(var->getDeclContext())) |
| 1070 | functionName = fn->getDeclName(); |
| 1071 | // FIXME: variable from enclosing block that we couldn't capture from! |
| 1072 | |
| 1073 | S.Diag(loc, diag::err_reference_to_local_var_in_enclosing_function) |
| 1074 | << var->getIdentifier() << functionName; |
| 1075 | S.Diag(var->getLocation(), diag::note_local_variable_declared_here) |
| 1076 | << var->getIdentifier(); |
| 1077 | |
| 1078 | return CR_Error; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
| John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1081 | /// There is a well-formed capture at a particular scope level; |
| 1082 | /// propagate it through all the nested blocks. |
| 1083 | static CaptureResult propagateCapture(Sema &S, unsigned validScopeIndex, |
| 1084 | const BlockDecl::Capture &capture) { |
| 1085 | VarDecl *var = capture.getVariable(); |
| 1086 | |
| 1087 | // Update all the inner blocks with the capture information. |
| 1088 | for (unsigned i = validScopeIndex + 1, e = S.FunctionScopes.size(); |
| 1089 | i != e; ++i) { |
| 1090 | BlockScopeInfo *innerBlock = cast<BlockScopeInfo>(S.FunctionScopes[i]); |
| 1091 | innerBlock->Captures.push_back( |
| 1092 | BlockDecl::Capture(capture.getVariable(), capture.isByRef(), |
| 1093 | /*nested*/ true, capture.getCopyExpr())); |
| 1094 | innerBlock->CaptureMap[var] = innerBlock->Captures.size(); // +1 |
| 1095 | } |
| 1096 | |
| 1097 | return capture.isByRef() ? CR_CaptureByRef : CR_Capture; |
| 1098 | } |
| 1099 | |
| 1100 | /// shouldCaptureValueReference - Determine if a reference to the |
| John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1101 | /// given value in the current context requires a variable capture. |
| 1102 | /// |
| 1103 | /// This also keeps the captures set in the BlockScopeInfo records |
| 1104 | /// up-to-date. |
| John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1105 | static CaptureResult shouldCaptureValueReference(Sema &S, SourceLocation loc, |
| John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1106 | ValueDecl *value) { |
| 1107 | // Only variables ever require capture. |
| 1108 | VarDecl *var = dyn_cast<VarDecl>(value); |
| John McCall | f4cd4f9 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1109 | if (!var) return CR_NoCapture; |
| John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1110 | |
| 1111 | // Fast path: variables from the current context never require capture. |
| 1112 | DeclContext *DC = S.CurContext; |
| 1113 | if (var->getDeclContext() == DC) return CR_NoCapture; |
| 1114 | |
| 1115 | // Only variables with local storage require capture. |
| 1116 | // FIXME: What about 'const' variables in C++? |
| 1117 | if (!var->hasLocalStorage()) return CR_NoCapture; |
| 1118 | |
| 1119 | // Otherwise, we need to capture. |
| 1120 | |
| 1121 | unsigned functionScopesIndex = S.FunctionScopes.size() - 1; |
| John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1122 | do { |
| 1123 | // Only blocks (and eventually C++0x closures) can capture; other |
| 1124 | // scopes don't work. |
| 1125 | if (!isa<BlockDecl>(DC)) |
| John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1126 | return diagnoseUncapturableValueReference(S, loc, var, DC); |
| John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1127 | |
| 1128 | BlockScopeInfo *blockScope = |
| 1129 | cast<BlockScopeInfo>(S.FunctionScopes[functionScopesIndex]); |
| 1130 | assert(blockScope->TheDecl == static_cast<BlockDecl*>(DC)); |
| 1131 | |
| John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1132 | // Check whether we've already captured it in this block. If so, |
| 1133 | // we're done. |
| 1134 | if (unsigned indexPlus1 = blockScope->CaptureMap[var]) |
| 1135 | return propagateCapture(S, functionScopesIndex, |
| 1136 | blockScope->Captures[indexPlus1 - 1]); |
| John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1137 | |
| 1138 | functionScopesIndex--; |
| 1139 | DC = cast<BlockDecl>(DC)->getDeclContext(); |
| 1140 | } while (var->getDeclContext() != DC); |
| 1141 | |
| John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1142 | // Okay, we descended all the way to the block that defines the variable. |
| 1143 | // Actually try to capture it. |
| 1144 | QualType type = var->getType(); |
| 1145 | |
| 1146 | // Prohibit variably-modified types. |
| 1147 | if (type->isVariablyModifiedType()) { |
| 1148 | S.Diag(loc, diag::err_ref_vm_type); |
| 1149 | S.Diag(var->getLocation(), diag::note_declared_at); |
| 1150 | return CR_Error; |
| 1151 | } |
| 1152 | |
| 1153 | // Prohibit arrays, even in __block variables, but not references to |
| 1154 | // them. |
| 1155 | if (type->isArrayType()) { |
| 1156 | S.Diag(loc, diag::err_ref_array_type); |
| 1157 | S.Diag(var->getLocation(), diag::note_declared_at); |
| 1158 | return CR_Error; |
| 1159 | } |
| 1160 | |
| 1161 | S.MarkDeclarationReferenced(loc, var); |
| 1162 | |
| 1163 | // The BlocksAttr indicates the variable is bound by-reference. |
| 1164 | bool byRef = var->hasAttr<BlocksAttr>(); |
| 1165 | |
| 1166 | // Build a copy expression. |
| 1167 | Expr *copyExpr = 0; |
| John McCall | a85af56 | 2011-04-28 02:15:35 +0000 | [diff] [blame] | 1168 | const RecordType *rtype; |
| 1169 | if (!byRef && S.getLangOptions().CPlusPlus && !type->isDependentType() && |
| 1170 | (rtype = type->getAs<RecordType>())) { |
| 1171 | |
| 1172 | // The capture logic needs the destructor, so make sure we mark it. |
| 1173 | // Usually this is unnecessary because most local variables have |
| 1174 | // their destructors marked at declaration time, but parameters are |
| 1175 | // an exception because it's technically only the call site that |
| 1176 | // actually requires the destructor. |
| 1177 | if (isa<ParmVarDecl>(var)) |
| 1178 | S.FinalizeVarWithDestructor(var, rtype); |
| 1179 | |
| John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1180 | // According to the blocks spec, the capture of a variable from |
| 1181 | // the stack requires a const copy constructor. This is not true |
| 1182 | // of the copy/move done to move a __block variable to the heap. |
| 1183 | type.addConst(); |
| 1184 | |
| 1185 | Expr *declRef = new (S.Context) DeclRefExpr(var, type, VK_LValue, loc); |
| 1186 | ExprResult result = |
| 1187 | S.PerformCopyInitialization( |
| 1188 | InitializedEntity::InitializeBlock(var->getLocation(), |
| 1189 | type, false), |
| 1190 | loc, S.Owned(declRef)); |
| 1191 | |
| 1192 | // Build a full-expression copy expression if initialization |
| 1193 | // succeeded and used a non-trivial constructor. Recover from |
| 1194 | // errors by pretending that the copy isn't necessary. |
| 1195 | if (!result.isInvalid() && |
| 1196 | !cast<CXXConstructExpr>(result.get())->getConstructor()->isTrivial()) { |
| 1197 | result = S.MaybeCreateExprWithCleanups(result); |
| 1198 | copyExpr = result.take(); |
| 1199 | } |
| 1200 | } |
| 1201 | |
| 1202 | // We're currently at the declarer; go back to the closure. |
| 1203 | functionScopesIndex++; |
| 1204 | BlockScopeInfo *blockScope = |
| 1205 | cast<BlockScopeInfo>(S.FunctionScopes[functionScopesIndex]); |
| 1206 | |
| 1207 | // Build a valid capture in this scope. |
| 1208 | blockScope->Captures.push_back( |
| 1209 | BlockDecl::Capture(var, byRef, /*nested*/ false, copyExpr)); |
| 1210 | blockScope->CaptureMap[var] = blockScope->Captures.size(); // +1 |
| 1211 | |
| 1212 | // Propagate that to inner captures if necessary. |
| 1213 | return propagateCapture(S, functionScopesIndex, |
| 1214 | blockScope->Captures.back()); |
| 1215 | } |
| 1216 | |
| 1217 | static ExprResult BuildBlockDeclRefExpr(Sema &S, ValueDecl *vd, |
| 1218 | const DeclarationNameInfo &NameInfo, |
| 1219 | bool byRef) { |
| 1220 | assert(isa<VarDecl>(vd) && "capturing non-variable"); |
| 1221 | |
| 1222 | VarDecl *var = cast<VarDecl>(vd); |
| 1223 | assert(var->hasLocalStorage() && "capturing non-local"); |
| 1224 | assert(byRef == var->hasAttr<BlocksAttr>() && "byref set wrong"); |
| 1225 | |
| 1226 | QualType exprType = var->getType().getNonReferenceType(); |
| 1227 | |
| 1228 | BlockDeclRefExpr *BDRE; |
| 1229 | if (!byRef) { |
| 1230 | // The variable will be bound by copy; make it const within the |
| 1231 | // closure, but record that this was done in the expression. |
| 1232 | bool constAdded = !exprType.isConstQualified(); |
| 1233 | exprType.addConst(); |
| 1234 | |
| 1235 | BDRE = new (S.Context) BlockDeclRefExpr(var, exprType, VK_LValue, |
| 1236 | NameInfo.getLoc(), false, |
| 1237 | constAdded); |
| 1238 | } else { |
| 1239 | BDRE = new (S.Context) BlockDeclRefExpr(var, exprType, VK_LValue, |
| 1240 | NameInfo.getLoc(), true); |
| 1241 | } |
| 1242 | |
| 1243 | return S.Owned(BDRE); |
| John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1244 | } |
| Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1245 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1246 | ExprResult |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1247 | Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, |
| John McCall | f4cd4f9 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1248 | SourceLocation Loc, |
| 1249 | const CXXScopeSpec *SS) { |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1250 | DeclarationNameInfo NameInfo(D->getDeclName(), Loc); |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1251 | return BuildDeclRefExpr(D, Ty, VK, NameInfo, SS); |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1252 | } |
| 1253 | |
| John McCall | f4cd4f9 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1254 | /// BuildDeclRefExpr - Build an expression that references a |
| 1255 | /// declaration that does not require a closure capture. |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1256 | ExprResult |
| John McCall | f4cd4f9 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1257 | Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1258 | const DeclarationNameInfo &NameInfo, |
| 1259 | const CXXScopeSpec *SS) { |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1260 | MarkDeclarationReferenced(NameInfo.getLoc(), D); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1261 | |
| John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1262 | Expr *E = DeclRefExpr::Create(Context, |
| Douglas Gregor | ea972d3 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 1263 | SS? SS->getWithLocInContext(Context) |
| 1264 | : NestedNameSpecifierLoc(), |
| John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1265 | D, NameInfo, Ty, VK); |
| 1266 | |
| 1267 | // Just in case we're building an illegal pointer-to-member. |
| 1268 | if (isa<FieldDecl>(D) && cast<FieldDecl>(D)->getBitWidth()) |
| 1269 | E->setObjectKind(OK_BitField); |
| 1270 | |
| 1271 | return Owned(E); |
| Douglas Gregor | c7acfdf | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 1272 | } |
| 1273 | |
| John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 1274 | static ExprResult |
| 1275 | BuildFieldReferenceExpr(Sema &S, Expr *BaseExpr, bool IsArrow, |
| 1276 | const CXXScopeSpec &SS, FieldDecl *Field, |
| 1277 | DeclAccessPair FoundDecl, |
| 1278 | const DeclarationNameInfo &MemberNameInfo); |
| 1279 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1280 | ExprResult |
| John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1281 | Sema::BuildAnonymousStructUnionMemberReference(const CXXScopeSpec &SS, |
| 1282 | SourceLocation loc, |
| 1283 | IndirectFieldDecl *indirectField, |
| 1284 | Expr *baseObjectExpr, |
| 1285 | SourceLocation opLoc) { |
| 1286 | // First, build the expression that refers to the base object. |
| 1287 | |
| 1288 | bool baseObjectIsPointer = false; |
| 1289 | Qualifiers baseQuals; |
| 1290 | |
| 1291 | // Case 1: the base of the indirect field is not a field. |
| 1292 | VarDecl *baseVariable = indirectField->getVarDecl(); |
| Douglas Gregor | e10f36d | 2011-02-18 02:44:58 +0000 | [diff] [blame] | 1293 | CXXScopeSpec EmptySS; |
| John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1294 | if (baseVariable) { |
| 1295 | assert(baseVariable->getType()->isRecordType()); |
| 1296 | |
| 1297 | // In principle we could have a member access expression that |
| 1298 | // accesses an anonymous struct/union that's a static member of |
| 1299 | // the base object's class. However, under the current standard, |
| 1300 | // static data members cannot be anonymous structs or unions. |
| 1301 | // Supporting this is as easy as building a MemberExpr here. |
| 1302 | assert(!baseObjectExpr && "anonymous struct/union is static data member?"); |
| 1303 | |
| 1304 | DeclarationNameInfo baseNameInfo(DeclarationName(), loc); |
| 1305 | |
| 1306 | ExprResult result = |
| Douglas Gregor | e10f36d | 2011-02-18 02:44:58 +0000 | [diff] [blame] | 1307 | BuildDeclarationNameExpr(EmptySS, baseNameInfo, baseVariable); |
| John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1308 | if (result.isInvalid()) return ExprError(); |
| 1309 | |
| 1310 | baseObjectExpr = result.take(); |
| 1311 | baseObjectIsPointer = false; |
| 1312 | baseQuals = baseObjectExpr->getType().getQualifiers(); |
| 1313 | |
| 1314 | // Case 2: the base of the indirect field is a field and the user |
| 1315 | // wrote a member expression. |
| 1316 | } else if (baseObjectExpr) { |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1317 | // The caller provided the base object expression. Determine |
| 1318 | // whether its a pointer and whether it adds any qualifiers to the |
| 1319 | // anonymous struct/union fields we're looking into. |
| John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1320 | QualType objectType = baseObjectExpr->getType(); |
| 1321 | |
| 1322 | if (const PointerType *ptr = objectType->getAs<PointerType>()) { |
| 1323 | baseObjectIsPointer = true; |
| 1324 | objectType = ptr->getPointeeType(); |
| 1325 | } else { |
| 1326 | baseObjectIsPointer = false; |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1327 | } |
| John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1328 | baseQuals = objectType.getQualifiers(); |
| 1329 | |
| 1330 | // Case 3: the base of the indirect field is a field and we should |
| 1331 | // build an implicit member access. |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1332 | } else { |
| 1333 | // We've found a member of an anonymous struct/union that is |
| 1334 | // inside a non-anonymous struct/union, so in a well-formed |
| 1335 | // program our base object expression is "this". |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1336 | QualType ThisTy = getAndCaptureCurrentThisType(); |
| 1337 | if (ThisTy.isNull()) { |
| John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1338 | Diag(loc, diag::err_invalid_member_use_in_static_method) |
| 1339 | << indirectField->getDeclName(); |
| 1340 | return ExprError(); |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1341 | } |
| 1342 | |
| John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1343 | // Our base object expression is "this". |
| 1344 | baseObjectExpr = |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1345 | new (Context) CXXThisExpr(loc, ThisTy, /*isImplicit=*/ true); |
| John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1346 | baseObjectIsPointer = true; |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1347 | baseQuals = ThisTy->castAs<PointerType>()->getPointeeType().getQualifiers(); |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1348 | } |
| 1349 | |
| 1350 | // Build the implicit member references to the field of the |
| 1351 | // anonymous struct/union. |
| John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1352 | Expr *result = baseObjectExpr; |
| 1353 | IndirectFieldDecl::chain_iterator |
| 1354 | FI = indirectField->chain_begin(), FEnd = indirectField->chain_end(); |
| John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 1355 | |
| John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1356 | // Build the first member access in the chain with full information. |
| 1357 | if (!baseVariable) { |
| 1358 | FieldDecl *field = cast<FieldDecl>(*FI); |
| John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 1359 | |
| John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1360 | // FIXME: use the real found-decl info! |
| 1361 | DeclAccessPair foundDecl = DeclAccessPair::make(field, field->getAccess()); |
| John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1362 | |
| John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1363 | // Make a nameInfo that properly uses the anonymous name. |
| 1364 | DeclarationNameInfo memberNameInfo(field->getDeclName(), loc); |
| John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1365 | |
| John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1366 | result = BuildFieldReferenceExpr(*this, result, baseObjectIsPointer, |
| Douglas Gregor | e10f36d | 2011-02-18 02:44:58 +0000 | [diff] [blame] | 1367 | EmptySS, field, foundDecl, |
| John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1368 | memberNameInfo).take(); |
| 1369 | baseObjectIsPointer = false; |
| John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1370 | |
| John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1371 | // FIXME: check qualified member access |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1372 | } |
| 1373 | |
| John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1374 | // In all cases, we should now skip the first declaration in the chain. |
| 1375 | ++FI; |
| 1376 | |
| Douglas Gregor | e10f36d | 2011-02-18 02:44:58 +0000 | [diff] [blame] | 1377 | while (FI != FEnd) { |
| 1378 | FieldDecl *field = cast<FieldDecl>(*FI++); |
| John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1379 | |
| 1380 | // FIXME: these are somewhat meaningless |
| 1381 | DeclarationNameInfo memberNameInfo(field->getDeclName(), loc); |
| 1382 | DeclAccessPair foundDecl = DeclAccessPair::make(field, field->getAccess()); |
| John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1383 | |
| 1384 | result = BuildFieldReferenceExpr(*this, result, /*isarrow*/ false, |
| Douglas Gregor | e10f36d | 2011-02-18 02:44:58 +0000 | [diff] [blame] | 1385 | (FI == FEnd? SS : EmptySS), field, |
| 1386 | foundDecl, memberNameInfo) |
| John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1387 | .take(); |
| 1388 | } |
| 1389 | |
| 1390 | return Owned(result); |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1391 | } |
| 1392 | |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1393 | /// Decomposes the given name into a DeclarationNameInfo, its location, and |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1394 | /// possibly a list of template arguments. |
| 1395 | /// |
| 1396 | /// If this produces template arguments, it is permitted to call |
| 1397 | /// DecomposeTemplateName. |
| 1398 | /// |
| 1399 | /// This actually loses a lot of source location information for |
| 1400 | /// non-standard name kinds; we should consider preserving that in |
| 1401 | /// some way. |
| 1402 | static void DecomposeUnqualifiedId(Sema &SemaRef, |
| 1403 | const UnqualifiedId &Id, |
| 1404 | TemplateArgumentListInfo &Buffer, |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1405 | DeclarationNameInfo &NameInfo, |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1406 | const TemplateArgumentListInfo *&TemplateArgs) { |
| 1407 | if (Id.getKind() == UnqualifiedId::IK_TemplateId) { |
| 1408 | Buffer.setLAngleLoc(Id.TemplateId->LAngleLoc); |
| 1409 | Buffer.setRAngleLoc(Id.TemplateId->RAngleLoc); |
| 1410 | |
| 1411 | ASTTemplateArgsPtr TemplateArgsPtr(SemaRef, |
| 1412 | Id.TemplateId->getTemplateArgs(), |
| 1413 | Id.TemplateId->NumArgs); |
| 1414 | SemaRef.translateTemplateArguments(TemplateArgsPtr, Buffer); |
| 1415 | TemplateArgsPtr.release(); |
| 1416 | |
| John McCall | 3e56fd4 | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 1417 | TemplateName TName = Id.TemplateId->Template.get(); |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1418 | SourceLocation TNameLoc = Id.TemplateId->TemplateNameLoc; |
| 1419 | NameInfo = SemaRef.Context.getNameForTemplate(TName, TNameLoc); |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1420 | TemplateArgs = &Buffer; |
| 1421 | } else { |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1422 | NameInfo = SemaRef.GetNameFromUnqualifiedId(Id); |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1423 | TemplateArgs = 0; |
| 1424 | } |
| 1425 | } |
| 1426 | |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1427 | /// Determines if the given class is provably not derived from all of |
| 1428 | /// the prospective base classes. |
| 1429 | static bool IsProvablyNotDerivedFrom(Sema &SemaRef, |
| 1430 | CXXRecordDecl *Record, |
| 1431 | const llvm::SmallPtrSet<CXXRecordDecl*, 4> &Bases) { |
| John McCall | a6d407c | 2009-12-01 22:28:41 +0000 | [diff] [blame] | 1432 | if (Bases.count(Record->getCanonicalDecl())) |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1433 | return false; |
| 1434 | |
| Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 1435 | RecordDecl *RD = Record->getDefinition(); |
| John McCall | a6d407c | 2009-12-01 22:28:41 +0000 | [diff] [blame] | 1436 | if (!RD) return false; |
| 1437 | Record = cast<CXXRecordDecl>(RD); |
| 1438 | |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1439 | for (CXXRecordDecl::base_class_iterator I = Record->bases_begin(), |
| 1440 | E = Record->bases_end(); I != E; ++I) { |
| 1441 | CanQualType BaseT = SemaRef.Context.getCanonicalType((*I).getType()); |
| 1442 | CanQual<RecordType> BaseRT = BaseT->getAs<RecordType>(); |
| 1443 | if (!BaseRT) return false; |
| 1444 | |
| 1445 | CXXRecordDecl *BaseRecord = cast<CXXRecordDecl>(BaseRT->getDecl()); |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1446 | if (!IsProvablyNotDerivedFrom(SemaRef, BaseRecord, Bases)) |
| 1447 | return false; |
| 1448 | } |
| 1449 | |
| 1450 | return true; |
| 1451 | } |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1452 | |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1453 | enum IMAKind { |
| 1454 | /// The reference is definitely not an instance member access. |
| 1455 | IMA_Static, |
| 1456 | |
| 1457 | /// The reference may be an implicit instance member access. |
| 1458 | IMA_Mixed, |
| 1459 | |
| 1460 | /// The reference may be to an instance member, but it is invalid if |
| 1461 | /// so, because the context is not an instance method. |
| 1462 | IMA_Mixed_StaticContext, |
| 1463 | |
| 1464 | /// The reference may be to an instance member, but it is invalid if |
| 1465 | /// so, because the context is from an unrelated class. |
| 1466 | IMA_Mixed_Unrelated, |
| 1467 | |
| 1468 | /// The reference is definitely an implicit instance member access. |
| 1469 | IMA_Instance, |
| 1470 | |
| 1471 | /// The reference may be to an unresolved using declaration. |
| 1472 | IMA_Unresolved, |
| 1473 | |
| 1474 | /// The reference may be to an unresolved using declaration and the |
| 1475 | /// context is not an instance method. |
| 1476 | IMA_Unresolved_StaticContext, |
| 1477 | |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1478 | /// All possible referrents are instance members and the current |
| 1479 | /// context is not an instance method. |
| 1480 | IMA_Error_StaticContext, |
| 1481 | |
| 1482 | /// All possible referrents are instance members of an unrelated |
| 1483 | /// class. |
| 1484 | IMA_Error_Unrelated |
| 1485 | }; |
| 1486 | |
| 1487 | /// The given lookup names class member(s) and is not being used for |
| 1488 | /// an address-of-member expression. Classify the type of access |
| 1489 | /// according to whether it's possible that this reference names an |
| 1490 | /// instance member. This is best-effort; it is okay to |
| 1491 | /// conservatively answer "yes", in which case some errors will simply |
| 1492 | /// not be caught until template-instantiation. |
| 1493 | static IMAKind ClassifyImplicitMemberAccess(Sema &SemaRef, |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1494 | Scope *CurScope, |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1495 | const LookupResult &R) { |
| John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1496 | assert(!R.empty() && (*R.begin())->isCXXClassMember()); |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1497 | |
| John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1498 | DeclContext *DC = SemaRef.getFunctionLevelDeclContext(); |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1499 | |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1500 | bool isStaticContext = |
| John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1501 | (!isa<CXXMethodDecl>(DC) || |
| 1502 | cast<CXXMethodDecl>(DC)->isStatic()); |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1503 | |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1504 | // C++0x [expr.prim]p4: |
| 1505 | // Otherwise, if a member-declarator declares a non-static data member |
| 1506 | // of a class X, the expression this is a prvalue of type "pointer to X" |
| 1507 | // within the optional brace-or-equal-initializer. |
| 1508 | if (CurScope->getFlags() & Scope::ThisScope) |
| 1509 | isStaticContext = false; |
| 1510 | |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1511 | if (R.isUnresolvableResult()) |
| 1512 | return isStaticContext ? IMA_Unresolved_StaticContext : IMA_Unresolved; |
| 1513 | |
| 1514 | // Collect all the declaring classes of instance members we find. |
| 1515 | bool hasNonInstance = false; |
| Sebastian Redl | 3462031 | 2010-11-26 16:28:07 +0000 | [diff] [blame] | 1516 | bool hasField = false; |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1517 | llvm::SmallPtrSet<CXXRecordDecl*, 4> Classes; |
| 1518 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1519 | NamedDecl *D = *I; |
| Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 1520 | |
| John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1521 | if (D->isCXXInstanceMember()) { |
| Sebastian Redl | 3462031 | 2010-11-26 16:28:07 +0000 | [diff] [blame] | 1522 | if (dyn_cast<FieldDecl>(D)) |
| 1523 | hasField = true; |
| 1524 | |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1525 | CXXRecordDecl *R = cast<CXXRecordDecl>(D->getDeclContext()); |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1526 | Classes.insert(R->getCanonicalDecl()); |
| 1527 | } |
| 1528 | else |
| 1529 | hasNonInstance = true; |
| 1530 | } |
| 1531 | |
| 1532 | // If we didn't find any instance members, it can't be an implicit |
| 1533 | // member reference. |
| 1534 | if (Classes.empty()) |
| 1535 | return IMA_Static; |
| 1536 | |
| 1537 | // If the current context is not an instance method, it can't be |
| 1538 | // an implicit member reference. |
| Sebastian Redl | 3462031 | 2010-11-26 16:28:07 +0000 | [diff] [blame] | 1539 | if (isStaticContext) { |
| 1540 | if (hasNonInstance) |
| 1541 | return IMA_Mixed_StaticContext; |
| 1542 | |
| 1543 | if (SemaRef.getLangOptions().CPlusPlus0x && hasField) { |
| 1544 | // C++0x [expr.prim.general]p10: |
| 1545 | // An id-expression that denotes a non-static data member or non-static |
| 1546 | // member function of a class can only be used: |
| 1547 | // (...) |
| 1548 | // - if that id-expression denotes a non-static data member and it appears in an unevaluated operand. |
| 1549 | const Sema::ExpressionEvaluationContextRecord& record = SemaRef.ExprEvalContexts.back(); |
| 1550 | bool isUnevaluatedExpression = record.Context == Sema::Unevaluated; |
| 1551 | if (isUnevaluatedExpression) |
| 1552 | return IMA_Mixed_StaticContext; |
| 1553 | } |
| 1554 | |
| 1555 | return IMA_Error_StaticContext; |
| 1556 | } |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1557 | |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1558 | CXXRecordDecl *contextClass; |
| 1559 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(DC)) |
| 1560 | contextClass = MD->getParent()->getCanonicalDecl(); |
| 1561 | else |
| 1562 | contextClass = cast<CXXRecordDecl>(DC); |
| Argyrios Kyrtzidis | 36e4ae3 | 2011-04-14 00:46:47 +0000 | [diff] [blame] | 1563 | |
| 1564 | // [class.mfct.non-static]p3: |
| 1565 | // ...is used in the body of a non-static member function of class X, |
| 1566 | // if name lookup (3.4.1) resolves the name in the id-expression to a |
| 1567 | // non-static non-type member of some class C [...] |
| 1568 | // ...if C is not X or a base class of X, the class member access expression |
| 1569 | // is ill-formed. |
| 1570 | if (R.getNamingClass() && |
| 1571 | contextClass != R.getNamingClass()->getCanonicalDecl() && |
| 1572 | contextClass->isProvablyNotDerivedFrom(R.getNamingClass())) |
| 1573 | return (hasNonInstance ? IMA_Mixed_Unrelated : IMA_Error_Unrelated); |
| 1574 | |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1575 | // If we can prove that the current context is unrelated to all the |
| 1576 | // declaring classes, it can't be an implicit member reference (in |
| 1577 | // which case it's an error if any of those members are selected). |
| Argyrios Kyrtzidis | 36e4ae3 | 2011-04-14 00:46:47 +0000 | [diff] [blame] | 1578 | if (IsProvablyNotDerivedFrom(SemaRef, contextClass, Classes)) |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1579 | return (hasNonInstance ? IMA_Mixed_Unrelated : IMA_Error_Unrelated); |
| 1580 | |
| 1581 | return (hasNonInstance ? IMA_Mixed : IMA_Instance); |
| 1582 | } |
| 1583 | |
| 1584 | /// Diagnose a reference to a field with no object available. |
| 1585 | static void DiagnoseInstanceReference(Sema &SemaRef, |
| 1586 | const CXXScopeSpec &SS, |
| John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1587 | NamedDecl *rep, |
| 1588 | const DeclarationNameInfo &nameInfo) { |
| 1589 | SourceLocation Loc = nameInfo.getLoc(); |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1590 | SourceRange Range(Loc); |
| 1591 | if (SS.isSet()) Range.setBegin(SS.getRange().getBegin()); |
| 1592 | |
| John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1593 | if (isa<FieldDecl>(rep) || isa<IndirectFieldDecl>(rep)) { |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1594 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(SemaRef.CurContext)) { |
| 1595 | if (MD->isStatic()) { |
| 1596 | // "invalid use of member 'x' in static member function" |
| 1597 | SemaRef.Diag(Loc, diag::err_invalid_member_use_in_static_method) |
| John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1598 | << Range << nameInfo.getName(); |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1599 | return; |
| 1600 | } |
| 1601 | } |
| 1602 | |
| 1603 | SemaRef.Diag(Loc, diag::err_invalid_non_static_member_use) |
| John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1604 | << nameInfo.getName() << Range; |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1605 | return; |
| 1606 | } |
| 1607 | |
| 1608 | SemaRef.Diag(Loc, diag::err_member_call_without_object) << Range; |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1609 | } |
| 1610 | |
| John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1611 | /// Diagnose an empty lookup. |
| 1612 | /// |
| 1613 | /// \return false if new lookup candidates were found |
| Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1614 | bool Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec &SS, LookupResult &R, |
| 1615 | CorrectTypoContext CTC) { |
| John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1616 | DeclarationName Name = R.getLookupName(); |
| 1617 | |
| John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1618 | unsigned diagnostic = diag::err_undeclared_var_use; |
| Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1619 | unsigned diagnostic_suggest = diag::err_undeclared_var_use_suggest; |
| John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1620 | if (Name.getNameKind() == DeclarationName::CXXOperatorName || |
| 1621 | Name.getNameKind() == DeclarationName::CXXLiteralOperatorName || |
| Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1622 | Name.getNameKind() == DeclarationName::CXXConversionFunctionName) { |
| John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1623 | diagnostic = diag::err_undeclared_use; |
| Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1624 | diagnostic_suggest = diag::err_undeclared_use_suggest; |
| 1625 | } |
| John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1626 | |
| Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1627 | // If the original lookup was an unqualified lookup, fake an |
| 1628 | // unqualified lookup. This is useful when (for example) the |
| 1629 | // original lookup would not have found something because it was a |
| 1630 | // dependent name. |
| Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1631 | for (DeclContext *DC = SS.isEmpty() ? CurContext : 0; |
| Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1632 | DC; DC = DC->getParent()) { |
| John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1633 | if (isa<CXXRecordDecl>(DC)) { |
| 1634 | LookupQualifiedName(R, DC); |
| 1635 | |
| 1636 | if (!R.empty()) { |
| 1637 | // Don't give errors about ambiguities in this lookup. |
| 1638 | R.suppressDiagnostics(); |
| 1639 | |
| 1640 | CXXMethodDecl *CurMethod = dyn_cast<CXXMethodDecl>(CurContext); |
| 1641 | bool isInstance = CurMethod && |
| 1642 | CurMethod->isInstance() && |
| 1643 | DC == CurMethod->getParent(); |
| 1644 | |
| 1645 | // Give a code modification hint to insert 'this->'. |
| 1646 | // TODO: fixit for inserting 'Base<T>::' in the other cases. |
| 1647 | // Actually quite difficult! |
| Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1648 | if (isInstance) { |
| Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1649 | UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>( |
| 1650 | CallsUndergoingInstantiation.back()->getCallee()); |
| Nick Lewycky | fe71238 | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1651 | CXXMethodDecl *DepMethod = cast_or_null<CXXMethodDecl>( |
| Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1652 | CurMethod->getInstantiatedFromMemberFunction()); |
| Eli Friedman | 0483192 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1653 | if (DepMethod) { |
| Nick Lewycky | fe71238 | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1654 | Diag(R.getNameLoc(), diagnostic) << Name |
| 1655 | << FixItHint::CreateInsertion(R.getNameLoc(), "this->"); |
| 1656 | QualType DepThisType = DepMethod->getThisType(Context); |
| 1657 | CXXThisExpr *DepThis = new (Context) CXXThisExpr( |
| 1658 | R.getNameLoc(), DepThisType, false); |
| 1659 | TemplateArgumentListInfo TList; |
| 1660 | if (ULE->hasExplicitTemplateArgs()) |
| 1661 | ULE->copyTemplateArgumentsInto(TList); |
| Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 1662 | |
| Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 1663 | CXXScopeSpec SS; |
| Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 1664 | SS.Adopt(ULE->getQualifierLoc()); |
| Nick Lewycky | fe71238 | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1665 | CXXDependentScopeMemberExpr *DepExpr = |
| 1666 | CXXDependentScopeMemberExpr::Create( |
| 1667 | Context, DepThis, DepThisType, true, SourceLocation(), |
| Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 1668 | SS.getWithLocInContext(Context), NULL, |
| Nick Lewycky | fe71238 | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1669 | R.getLookupNameInfo(), &TList); |
| 1670 | CallsUndergoingInstantiation.back()->setCallee(DepExpr); |
| Eli Friedman | 0483192 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1671 | } else { |
| Nick Lewycky | fe71238 | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1672 | // FIXME: we should be able to handle this case too. It is correct |
| 1673 | // to add this-> here. This is a workaround for PR7947. |
| 1674 | Diag(R.getNameLoc(), diagnostic) << Name; |
| Eli Friedman | 0483192 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1675 | } |
| Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1676 | } else { |
| John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1677 | Diag(R.getNameLoc(), diagnostic) << Name; |
| Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1678 | } |
| John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1679 | |
| 1680 | // Do we really want to note all of these? |
| 1681 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 1682 | Diag((*I)->getLocation(), diag::note_dependent_var_use); |
| 1683 | |
| 1684 | // Tell the callee to try to recover. |
| 1685 | return false; |
| 1686 | } |
| Douglas Gregor | 86b8d9f | 2010-08-09 22:38:14 +0000 | [diff] [blame] | 1687 | |
| 1688 | R.clear(); |
| John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1689 | } |
| 1690 | } |
| 1691 | |
| Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1692 | // We didn't find anything, so try to correct for a typo. |
| Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1693 | DeclarationName Corrected; |
| Daniel Dunbar | f7ced25 | 2010-06-02 15:46:52 +0000 | [diff] [blame] | 1694 | if (S && (Corrected = CorrectTypo(R, S, &SS, 0, false, CTC))) { |
| Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1695 | if (!R.empty()) { |
| 1696 | if (isa<ValueDecl>(*R.begin()) || isa<FunctionTemplateDecl>(*R.begin())) { |
| 1697 | if (SS.isEmpty()) |
| 1698 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << R.getLookupName() |
| 1699 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 1700 | R.getLookupName().getAsString()); |
| 1701 | else |
| 1702 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
| 1703 | << Name << computeDeclContext(SS, false) << R.getLookupName() |
| 1704 | << SS.getRange() |
| 1705 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 1706 | R.getLookupName().getAsString()); |
| 1707 | if (NamedDecl *ND = R.getAsSingle<NamedDecl>()) |
| 1708 | Diag(ND->getLocation(), diag::note_previous_decl) |
| 1709 | << ND->getDeclName(); |
| 1710 | |
| 1711 | // Tell the callee to try to recover. |
| 1712 | return false; |
| 1713 | } |
| Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1714 | |
| Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1715 | if (isa<TypeDecl>(*R.begin()) || isa<ObjCInterfaceDecl>(*R.begin())) { |
| 1716 | // FIXME: If we ended up with a typo for a type name or |
| 1717 | // Objective-C class name, we're in trouble because the parser |
| 1718 | // is in the wrong place to recover. Suggest the typo |
| 1719 | // correction, but don't make it a fix-it since we're not going |
| 1720 | // to recover well anyway. |
| 1721 | if (SS.isEmpty()) |
| 1722 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << R.getLookupName(); |
| 1723 | else |
| 1724 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
| 1725 | << Name << computeDeclContext(SS, false) << R.getLookupName() |
| 1726 | << SS.getRange(); |
| 1727 | |
| 1728 | // Don't try to recover; it won't work. |
| 1729 | return true; |
| 1730 | } |
| 1731 | } else { |
| Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1732 | // FIXME: We found a keyword. Suggest it, but don't provide a fix-it |
| Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1733 | // because we aren't able to recover. |
| Douglas Gregor | 2536398 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1734 | if (SS.isEmpty()) |
| Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1735 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << Corrected; |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1736 | else |
| Douglas Gregor | 2536398 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1737 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
| Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1738 | << Name << computeDeclContext(SS, false) << Corrected |
| 1739 | << SS.getRange(); |
| Douglas Gregor | 2536398 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1740 | return true; |
| 1741 | } |
| Douglas Gregor | 2536398 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1742 | R.clear(); |
| Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1743 | } |
| 1744 | |
| 1745 | // Emit a special diagnostic for failed member lookups. |
| 1746 | // FIXME: computing the declaration context might fail here (?) |
| 1747 | if (!SS.isEmpty()) { |
| 1748 | Diag(R.getNameLoc(), diag::err_no_member) |
| 1749 | << Name << computeDeclContext(SS, false) |
| 1750 | << SS.getRange(); |
| 1751 | return true; |
| 1752 | } |
| 1753 | |
| John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1754 | // Give up, we can't recover. |
| 1755 | Diag(R.getNameLoc(), diagnostic) << Name; |
| 1756 | return true; |
| 1757 | } |
| 1758 | |
| Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1759 | ObjCPropertyDecl *Sema::canSynthesizeProvisionalIvar(IdentifierInfo *II) { |
| 1760 | ObjCMethodDecl *CurMeth = getCurMethodDecl(); |
| Fariborz Jahanian | 8615134 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1761 | ObjCInterfaceDecl *IDecl = CurMeth->getClassInterface(); |
| 1762 | if (!IDecl) |
| 1763 | return 0; |
| 1764 | ObjCImplementationDecl *ClassImpDecl = IDecl->getImplementation(); |
| 1765 | if (!ClassImpDecl) |
| 1766 | return 0; |
| Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1767 | ObjCPropertyDecl *property = LookupPropertyDecl(IDecl, II); |
| Fariborz Jahanian | 8615134 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1768 | if (!property) |
| 1769 | return 0; |
| 1770 | if (ObjCPropertyImplDecl *PIDecl = ClassImpDecl->FindPropertyImplDecl(II)) |
| Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1771 | if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic || |
| 1772 | PIDecl->getPropertyIvarDecl()) |
| Fariborz Jahanian | 8615134 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1773 | return 0; |
| 1774 | return property; |
| 1775 | } |
| 1776 | |
| Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1777 | bool Sema::canSynthesizeProvisionalIvar(ObjCPropertyDecl *Property) { |
| 1778 | ObjCMethodDecl *CurMeth = getCurMethodDecl(); |
| 1779 | ObjCInterfaceDecl *IDecl = CurMeth->getClassInterface(); |
| 1780 | if (!IDecl) |
| 1781 | return false; |
| 1782 | ObjCImplementationDecl *ClassImpDecl = IDecl->getImplementation(); |
| 1783 | if (!ClassImpDecl) |
| 1784 | return false; |
| 1785 | if (ObjCPropertyImplDecl *PIDecl |
| 1786 | = ClassImpDecl->FindPropertyImplDecl(Property->getIdentifier())) |
| 1787 | if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic || |
| 1788 | PIDecl->getPropertyIvarDecl()) |
| 1789 | return false; |
| 1790 | |
| 1791 | return true; |
| 1792 | } |
| 1793 | |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1794 | ObjCIvarDecl *Sema::SynthesizeProvisionalIvar(LookupResult &Lookup, |
| 1795 | IdentifierInfo *II, |
| 1796 | SourceLocation NameLoc) { |
| 1797 | ObjCMethodDecl *CurMeth = getCurMethodDecl(); |
| Fariborz Jahanian | 7b70eb4 | 2010-07-30 16:59:05 +0000 | [diff] [blame] | 1798 | bool LookForIvars; |
| 1799 | if (Lookup.empty()) |
| 1800 | LookForIvars = true; |
| 1801 | else if (CurMeth->isClassMethod()) |
| 1802 | LookForIvars = false; |
| 1803 | else |
| 1804 | LookForIvars = (Lookup.isSingleResult() && |
| Fariborz Jahanian | 9312fcc | 2011-01-26 00:57:01 +0000 | [diff] [blame] | 1805 | Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod() && |
| 1806 | (Lookup.getAsSingle<VarDecl>() != 0)); |
| Fariborz Jahanian | 7b70eb4 | 2010-07-30 16:59:05 +0000 | [diff] [blame] | 1807 | if (!LookForIvars) |
| 1808 | return 0; |
| 1809 | |
| Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1810 | ObjCInterfaceDecl *IDecl = CurMeth->getClassInterface(); |
| 1811 | if (!IDecl) |
| 1812 | return 0; |
| 1813 | ObjCImplementationDecl *ClassImpDecl = IDecl->getImplementation(); |
| Fariborz Jahanian | 2a36089 | 2010-07-19 16:14:33 +0000 | [diff] [blame] | 1814 | if (!ClassImpDecl) |
| 1815 | return 0; |
| Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1816 | bool DynamicImplSeen = false; |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1817 | ObjCPropertyDecl *property = LookupPropertyDecl(IDecl, II); |
| Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1818 | if (!property) |
| 1819 | return 0; |
| Fariborz Jahanian | bfcbc85 | 2010-10-19 19:08:23 +0000 | [diff] [blame] | 1820 | if (ObjCPropertyImplDecl *PIDecl = ClassImpDecl->FindPropertyImplDecl(II)) { |
| Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1821 | DynamicImplSeen = |
| 1822 | (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic); |
| Fariborz Jahanian | bfcbc85 | 2010-10-19 19:08:23 +0000 | [diff] [blame] | 1823 | // property implementation has a designated ivar. No need to assume a new |
| 1824 | // one. |
| 1825 | if (!DynamicImplSeen && PIDecl->getPropertyIvarDecl()) |
| 1826 | return 0; |
| 1827 | } |
| Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1828 | if (!DynamicImplSeen) { |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1829 | QualType PropType = Context.getCanonicalType(property->getType()); |
| 1830 | ObjCIvarDecl *Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl, |
| Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1831 | NameLoc, NameLoc, |
| Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1832 | II, PropType, /*Dinfo=*/0, |
| Fariborz Jahanian | 522eb7b | 2010-12-15 23:29:04 +0000 | [diff] [blame] | 1833 | ObjCIvarDecl::Private, |
| Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1834 | (Expr *)0, true); |
| 1835 | ClassImpDecl->addDecl(Ivar); |
| 1836 | IDecl->makeDeclVisibleInContext(Ivar, false); |
| 1837 | property->setPropertyIvarDecl(Ivar); |
| 1838 | return Ivar; |
| 1839 | } |
| 1840 | return 0; |
| 1841 | } |
| 1842 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1843 | ExprResult Sema::ActOnIdExpression(Scope *S, |
| John McCall | 24d1894 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 1844 | CXXScopeSpec &SS, |
| 1845 | UnqualifiedId &Id, |
| 1846 | bool HasTrailingLParen, |
| 1847 | bool isAddressOfOperand) { |
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1848 | assert(!(isAddressOfOperand && HasTrailingLParen) && |
| 1849 | "cannot be direct & operand and have a trailing lparen"); |
| 1850 | |
| 1851 | if (SS.isInvalid()) |
| Douglas Gregor | ed8f288 | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1852 | return ExprError(); |
| Douglas Gregor | 90a1a65 | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1853 | |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1854 | TemplateArgumentListInfo TemplateArgsBuffer; |
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1855 | |
| 1856 | // Decompose the UnqualifiedId into the following data. |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1857 | DeclarationNameInfo NameInfo; |
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1858 | const TemplateArgumentListInfo *TemplateArgs; |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1859 | DecomposeUnqualifiedId(*this, Id, TemplateArgsBuffer, NameInfo, TemplateArgs); |
| Douglas Gregor | 90a1a65 | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1860 | |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1861 | DeclarationName Name = NameInfo.getName(); |
| Douglas Gregor | 4ea8043 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 1862 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1863 | SourceLocation NameLoc = NameInfo.getLoc(); |
| John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1864 | |
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1865 | // C++ [temp.dep.expr]p3: |
| 1866 | // An id-expression is type-dependent if it contains: |
| Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 1867 | // -- an identifier that was declared with a dependent type, |
| 1868 | // (note: handled after lookup) |
| 1869 | // -- a template-id that is dependent, |
| 1870 | // (note: handled in BuildTemplateIdExpr) |
| 1871 | // -- a conversion-function-id that specifies a dependent type, |
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1872 | // -- a nested-name-specifier that contains a class-name that |
| 1873 | // names a dependent type. |
| 1874 | // Determine whether this is a member of an unknown specialization; |
| 1875 | // we need to handle these differently. |
| Eli Friedman | 964dbda | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1876 | bool DependentID = false; |
| 1877 | if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName && |
| 1878 | Name.getCXXNameType()->isDependentType()) { |
| 1879 | DependentID = true; |
| 1880 | } else if (SS.isSet()) { |
| Chris Lattner | ebb5c6c | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1881 | if (DeclContext *DC = computeDeclContext(SS, false)) { |
| Eli Friedman | 964dbda | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1882 | if (RequireCompleteDeclContext(SS, DC)) |
| 1883 | return ExprError(); |
| Eli Friedman | 964dbda | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1884 | } else { |
| 1885 | DependentID = true; |
| 1886 | } |
| 1887 | } |
| 1888 | |
| Chris Lattner | ebb5c6c | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1889 | if (DependentID) |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1890 | return ActOnDependentIdExpression(SS, NameInfo, isAddressOfOperand, |
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1891 | TemplateArgs); |
| Chris Lattner | ebb5c6c | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1892 | |
| Fariborz Jahanian | 8615134 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1893 | bool IvarLookupFollowUp = false; |
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1894 | // Perform the required lookup. |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1895 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1896 | if (TemplateArgs) { |
| Douglas Gregor | 3e51e17 | 2010-05-20 20:58:56 +0000 | [diff] [blame] | 1897 | // Lookup the template name again to correctly establish the context in |
| 1898 | // which it was found. This is really unfortunate as we already did the |
| 1899 | // lookup to determine that it was a template name in the first place. If |
| 1900 | // this becomes a performance hit, we can work harder to preserve those |
| 1901 | // results until we get here but it's likely not worth it. |
| Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1902 | bool MemberOfUnknownSpecialization; |
| 1903 | LookupTemplateName(R, S, SS, QualType(), /*EnteringContext=*/false, |
| 1904 | MemberOfUnknownSpecialization); |
| Douglas Gregor | a522693 | 2011-02-04 13:35:07 +0000 | [diff] [blame] | 1905 | |
| 1906 | if (MemberOfUnknownSpecialization || |
| 1907 | (R.getResultKind() == LookupResult::NotFoundInCurrentInstantiation)) |
| 1908 | return ActOnDependentIdExpression(SS, NameInfo, isAddressOfOperand, |
| 1909 | TemplateArgs); |
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1910 | } else { |
| Fariborz Jahanian | 8615134 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1911 | IvarLookupFollowUp = (!SS.isSet() && II && getCurMethodDecl()); |
| Fariborz Jahanian | 6fada5b | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1912 | LookupParsedName(R, S, &SS, !IvarLookupFollowUp); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1913 | |
| Douglas Gregor | a522693 | 2011-02-04 13:35:07 +0000 | [diff] [blame] | 1914 | // If the result might be in a dependent base class, this is a dependent |
| 1915 | // id-expression. |
| 1916 | if (R.getResultKind() == LookupResult::NotFoundInCurrentInstantiation) |
| 1917 | return ActOnDependentIdExpression(SS, NameInfo, isAddressOfOperand, |
| 1918 | TemplateArgs); |
| 1919 | |
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1920 | // If this reference is in an Objective-C method, then we need to do |
| 1921 | // some special Objective-C lookup, too. |
| Fariborz Jahanian | 6fada5b | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1922 | if (IvarLookupFollowUp) { |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1923 | ExprResult E(LookupInObjCMethod(R, S, II, true)); |
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1924 | if (E.isInvalid()) |
| 1925 | return ExprError(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1926 | |
| Chris Lattner | ebb5c6c | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1927 | if (Expr *Ex = E.takeAs<Expr>()) |
| 1928 | return Owned(Ex); |
| 1929 | |
| 1930 | // Synthesize ivars lazily. |
| Fariborz Jahanian | c63f1c5 | 2011-01-03 18:08:02 +0000 | [diff] [blame] | 1931 | if (getLangOptions().ObjCDefaultSynthProperties && |
| 1932 | getLangOptions().ObjCNonFragileABI2) { |
| Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1933 | if (SynthesizeProvisionalIvar(R, II, NameLoc)) { |
| Fariborz Jahanian | 8046af7 | 2010-11-17 19:41:23 +0000 | [diff] [blame] | 1934 | if (const ObjCPropertyDecl *Property = |
| 1935 | canSynthesizeProvisionalIvar(II)) { |
| 1936 | Diag(NameLoc, diag::warn_synthesized_ivar_access) << II; |
| 1937 | Diag(Property->getLocation(), diag::note_property_declare); |
| 1938 | } |
| Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1939 | return ActOnIdExpression(S, SS, Id, HasTrailingLParen, |
| 1940 | isAddressOfOperand); |
| Fariborz Jahanian | 8046af7 | 2010-11-17 19:41:23 +0000 | [diff] [blame] | 1941 | } |
| Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1942 | } |
| Fariborz Jahanian | 18d90a9 | 2010-08-13 18:09:39 +0000 | [diff] [blame] | 1943 | // for further use, this must be set to false if in class method. |
| 1944 | IvarLookupFollowUp = getCurMethodDecl()->isInstanceMethod(); |
| Steve Naroff | ebf4cb4 | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 1945 | } |
| Chris Lattner | 59a2594 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 1946 | } |
| Douglas Gregor | f15f5d3 | 2009-02-16 19:28:42 +0000 | [diff] [blame] | 1947 | |
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1948 | if (R.isAmbiguous()) |
| 1949 | return ExprError(); |
| 1950 | |
| Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1951 | // Determine whether this name might be a candidate for |
| 1952 | // argument-dependent lookup. |
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1953 | bool ADL = UseArgumentDependentLookup(SS, R, HasTrailingLParen); |
| Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1954 | |
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1955 | if (R.empty() && !ADL) { |
| Bill Wendling | 4073ed5 | 2007-02-13 01:51:42 +0000 | [diff] [blame] | 1956 | // Otherwise, this could be an implicitly declared function reference (legal |
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1957 | // in C90, extension in C99, forbidden in C++). |
| 1958 | if (HasTrailingLParen && II && !getLangOptions().CPlusPlus) { |
| 1959 | NamedDecl *D = ImplicitlyDefineFunction(NameLoc, *II, S); |
| 1960 | if (D) R.addDecl(D); |
| 1961 | } |
| 1962 | |
| 1963 | // If this name wasn't predeclared and if this is not a function |
| 1964 | // call, diagnose the problem. |
| 1965 | if (R.empty()) { |
| Douglas Gregor | 5fd04d4 | 2010-05-18 16:14:23 +0000 | [diff] [blame] | 1966 | if (DiagnoseEmptyLookup(S, SS, R, CTC_Unknown)) |
| John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1967 | return ExprError(); |
| 1968 | |
| 1969 | assert(!R.empty() && |
| 1970 | "DiagnoseEmptyLookup returned false but added no results"); |
| Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1971 | |
| 1972 | // If we found an Objective-C instance variable, let |
| 1973 | // LookupInObjCMethod build the appropriate expression to |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1974 | // reference the ivar. |
| Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1975 | if (ObjCIvarDecl *Ivar = R.getAsSingle<ObjCIvarDecl>()) { |
| 1976 | R.clear(); |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1977 | ExprResult E(LookupInObjCMethod(R, S, Ivar->getIdentifier())); |
| Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1978 | assert(E.isInvalid() || E.get()); |
| 1979 | return move(E); |
| 1980 | } |
| Steve Naroff | 92e30f8 | 2007-04-02 22:35:25 +0000 | [diff] [blame] | 1981 | } |
| Chris Lattner | 17ed487 | 2006-11-20 04:58:19 +0000 | [diff] [blame] | 1982 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1983 | |
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1984 | // This is guaranteed from this point on. |
| 1985 | assert(!R.empty() || ADL); |
| 1986 | |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1987 | // Check whether this might be a C++ implicit instance member access. |
| John McCall | 24d1894 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 1988 | // C++ [class.mfct.non-static]p3: |
| 1989 | // When an id-expression that is not part of a class member access |
| 1990 | // syntax and not used to form a pointer to member is used in the |
| 1991 | // body of a non-static member function of class X, if name lookup |
| 1992 | // resolves the name in the id-expression to a non-static non-type |
| 1993 | // member of some class C, the id-expression is transformed into a |
| 1994 | // class member access expression using (*this) as the |
| 1995 | // postfix-expression to the left of the . operator. |
| John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1996 | // |
| 1997 | // But we don't actually need to do this for '&' operands if R |
| 1998 | // resolved to a function or overloaded function set, because the |
| 1999 | // expression is ill-formed if it actually works out to be a |
| 2000 | // non-static member function: |
| 2001 | // |
| 2002 | // C++ [expr.ref]p4: |
| 2003 | // Otherwise, if E1.E2 refers to a non-static member function. . . |
| 2004 | // [t]he expression can be used only as the left-hand operand of a |
| 2005 | // member function call. |
| 2006 | // |
| 2007 | // There are other safeguards against such uses, but it's important |
| 2008 | // to get this right here so that we don't end up making a |
| 2009 | // spuriously dependent expression if we're inside a dependent |
| 2010 | // instance method. |
| John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2011 | if (!R.empty() && (*R.begin())->isCXXClassMember()) { |
| John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 2012 | bool MightBeImplicitMember; |
| 2013 | if (!isAddressOfOperand) |
| 2014 | MightBeImplicitMember = true; |
| 2015 | else if (!SS.isEmpty()) |
| 2016 | MightBeImplicitMember = false; |
| 2017 | else if (R.isOverloadedResult()) |
| 2018 | MightBeImplicitMember = false; |
| Douglas Gregor | 1262b06 | 2010-08-30 16:00:47 +0000 | [diff] [blame] | 2019 | else if (R.isUnresolvableResult()) |
| 2020 | MightBeImplicitMember = true; |
| John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 2021 | else |
| Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2022 | MightBeImplicitMember = isa<FieldDecl>(R.getFoundDecl()) || |
| 2023 | isa<IndirectFieldDecl>(R.getFoundDecl()); |
| John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 2024 | |
| 2025 | if (MightBeImplicitMember) |
| John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2026 | return BuildPossibleImplicitMemberExpr(SS, R, TemplateArgs); |
| John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2027 | } |
| 2028 | |
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2029 | if (TemplateArgs) |
| 2030 | return BuildTemplateIdExpr(SS, R, ADL, *TemplateArgs); |
| John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2031 | |
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2032 | return BuildDeclarationNameExpr(SS, R, ADL); |
| 2033 | } |
| 2034 | |
| John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2035 | /// Builds an expression which might be an implicit member expression. |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2036 | ExprResult |
| John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2037 | Sema::BuildPossibleImplicitMemberExpr(const CXXScopeSpec &SS, |
| 2038 | LookupResult &R, |
| 2039 | const TemplateArgumentListInfo *TemplateArgs) { |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2040 | switch (ClassifyImplicitMemberAccess(*this, CurScope, R)) { |
| John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2041 | case IMA_Instance: |
| 2042 | return BuildImplicitMemberExpr(SS, R, TemplateArgs, true); |
| 2043 | |
| John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2044 | case IMA_Mixed: |
| 2045 | case IMA_Mixed_Unrelated: |
| 2046 | case IMA_Unresolved: |
| 2047 | return BuildImplicitMemberExpr(SS, R, TemplateArgs, false); |
| 2048 | |
| 2049 | case IMA_Static: |
| 2050 | case IMA_Mixed_StaticContext: |
| 2051 | case IMA_Unresolved_StaticContext: |
| 2052 | if (TemplateArgs) |
| 2053 | return BuildTemplateIdExpr(SS, R, false, *TemplateArgs); |
| 2054 | return BuildDeclarationNameExpr(SS, R, false); |
| 2055 | |
| 2056 | case IMA_Error_StaticContext: |
| 2057 | case IMA_Error_Unrelated: |
| John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2058 | DiagnoseInstanceReference(*this, SS, R.getRepresentativeDecl(), |
| 2059 | R.getLookupNameInfo()); |
| John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2060 | return ExprError(); |
| 2061 | } |
| 2062 | |
| 2063 | llvm_unreachable("unexpected instance member access kind"); |
| 2064 | return ExprError(); |
| 2065 | } |
| 2066 | |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2067 | /// BuildQualifiedDeclarationNameExpr - Build a C++ qualified |
| 2068 | /// declaration name, generally during template instantiation. |
| 2069 | /// There's a large number of things which don't need to be done along |
| 2070 | /// this path. |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2071 | ExprResult |
| Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 2072 | Sema::BuildQualifiedDeclarationNameExpr(CXXScopeSpec &SS, |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2073 | const DeclarationNameInfo &NameInfo) { |
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2074 | DeclContext *DC; |
| Douglas Gregor | a02bb34 | 2010-04-28 07:04:26 +0000 | [diff] [blame] | 2075 | if (!(DC = computeDeclContext(SS, false)) || DC->isDependentContext()) |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2076 | return BuildDependentDeclRefExpr(SS, NameInfo, 0); |
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2077 | |
| John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 2078 | if (RequireCompleteDeclContext(SS, DC)) |
| Douglas Gregor | a02bb34 | 2010-04-28 07:04:26 +0000 | [diff] [blame] | 2079 | return ExprError(); |
| 2080 | |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2081 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2082 | LookupQualifiedName(R, DC); |
| 2083 | |
| 2084 | if (R.isAmbiguous()) |
| 2085 | return ExprError(); |
| 2086 | |
| 2087 | if (R.empty()) { |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2088 | Diag(NameInfo.getLoc(), diag::err_no_member) |
| 2089 | << NameInfo.getName() << DC << SS.getRange(); |
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2090 | return ExprError(); |
| 2091 | } |
| 2092 | |
| 2093 | return BuildDeclarationNameExpr(SS, R, /*ADL*/ false); |
| 2094 | } |
| 2095 | |
| 2096 | /// LookupInObjCMethod - The parser has read a name in, and Sema has |
| 2097 | /// detected that we're currently inside an ObjC method. Perform some |
| 2098 | /// additional lookup. |
| 2099 | /// |
| 2100 | /// Ideally, most of this would be done by lookup, but there's |
| 2101 | /// actually quite a lot of extra work involved. |
| 2102 | /// |
| 2103 | /// Returns a null sentinel to indicate trivial success. |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2104 | ExprResult |
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2105 | Sema::LookupInObjCMethod(LookupResult &Lookup, Scope *S, |
| Chris Lattner | a36ec42 | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 2106 | IdentifierInfo *II, bool AllowBuiltinCreation) { |
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2107 | SourceLocation Loc = Lookup.getNameLoc(); |
| Chris Lattner | 8731366 | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 2108 | ObjCMethodDecl *CurMethod = getCurMethodDecl(); |
| Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 2109 | |
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2110 | // There are two cases to handle here. 1) scoped lookup could have failed, |
| 2111 | // in which case we should look for an ivar. 2) scoped lookup could have |
| 2112 | // found a decl, but that decl is outside the current instance method (i.e. |
| 2113 | // a global variable). In these two cases, we do a lookup for an ivar with |
| 2114 | // this name, if the lookup sucedes, we replace it our current decl. |
| 2115 | |
| 2116 | // If we're in a class method, we don't normally want to look for |
| 2117 | // ivars. But if we don't find anything else, and there's an |
| 2118 | // ivar, that's an error. |
| Chris Lattner | 8731366 | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 2119 | bool IsClassMethod = CurMethod->isClassMethod(); |
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2120 | |
| 2121 | bool LookForIvars; |
| 2122 | if (Lookup.empty()) |
| 2123 | LookForIvars = true; |
| 2124 | else if (IsClassMethod) |
| 2125 | LookForIvars = false; |
| 2126 | else |
| 2127 | LookForIvars = (Lookup.isSingleResult() && |
| 2128 | Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod()); |
| Fariborz Jahanian | 4587803 | 2010-02-09 19:31:38 +0000 | [diff] [blame] | 2129 | ObjCInterfaceDecl *IFace = 0; |
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2130 | if (LookForIvars) { |
| Chris Lattner | 8731366 | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 2131 | IFace = CurMethod->getClassInterface(); |
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2132 | ObjCInterfaceDecl *ClassDeclared; |
| 2133 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
| 2134 | // Diagnose using an ivar in a class method. |
| 2135 | if (IsClassMethod) |
| 2136 | return ExprError(Diag(Loc, diag::error_ivar_use_in_class_method) |
| 2137 | << IV->getDeclName()); |
| 2138 | |
| 2139 | // If we're referencing an invalid decl, just return this as a silent |
| 2140 | // error node. The error diagnostic was already emitted on the decl. |
| 2141 | if (IV->isInvalidDecl()) |
| 2142 | return ExprError(); |
| 2143 | |
| 2144 | // Check if referencing a field with __attribute__((deprecated)). |
| 2145 | if (DiagnoseUseOfDecl(IV, Loc)) |
| 2146 | return ExprError(); |
| 2147 | |
| 2148 | // Diagnose the use of an ivar outside of the declaring class. |
| 2149 | if (IV->getAccessControl() == ObjCIvarDecl::Private && |
| 2150 | ClassDeclared != IFace) |
| 2151 | Diag(Loc, diag::error_private_ivar_access) << IV->getDeclName(); |
| 2152 | |
| 2153 | // FIXME: This should use a new expr for a direct reference, don't |
| 2154 | // turn this into Self->ivar, just return a BareIVarExpr or something. |
| 2155 | IdentifierInfo &II = Context.Idents.get("self"); |
| 2156 | UnqualifiedId SelfName; |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2157 | SelfName.setIdentifier(&II, SourceLocation()); |
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2158 | CXXScopeSpec SelfScopeSpec; |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2159 | ExprResult SelfExpr = ActOnIdExpression(S, SelfScopeSpec, |
| Douglas Gregor | a1ed39b | 2010-09-22 16:33:13 +0000 | [diff] [blame] | 2160 | SelfName, false, false); |
| 2161 | if (SelfExpr.isInvalid()) |
| 2162 | return ExprError(); |
| 2163 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2164 | SelfExpr = DefaultLvalueConversion(SelfExpr.take()); |
| 2165 | if (SelfExpr.isInvalid()) |
| 2166 | return ExprError(); |
| John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 2167 | |
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2168 | MarkDeclarationReferenced(Loc, IV); |
| Fariborz Jahanian | 82bc436 | 2011-04-12 23:39:33 +0000 | [diff] [blame] | 2169 | Expr *base = SelfExpr.take(); |
| 2170 | base = base->IgnoreParenImpCasts(); |
| 2171 | if (const DeclRefExpr *DE = dyn_cast<DeclRefExpr>(base)) { |
| 2172 | const NamedDecl *ND = DE->getDecl(); |
| 2173 | if (!isa<ImplicitParamDecl>(ND)) { |
| Fariborz Jahanian | 66a6c06 | 2011-04-15 17:04:42 +0000 | [diff] [blame] | 2174 | // relax the rule such that it is allowed to have a shadow 'self' |
| 2175 | // where stand-alone ivar can be found in this 'self' object. |
| 2176 | // This is to match gcc's behavior. |
| 2177 | ObjCInterfaceDecl *selfIFace = 0; |
| 2178 | if (const ObjCObjectPointerType *OPT = |
| 2179 | base->getType()->getAsObjCInterfacePointerType()) |
| 2180 | selfIFace = OPT->getInterfaceDecl(); |
| 2181 | if (!selfIFace || |
| 2182 | !selfIFace->lookupInstanceVariable(IV->getIdentifier())) { |
| Fariborz Jahanian | 82bc436 | 2011-04-12 23:39:33 +0000 | [diff] [blame] | 2183 | Diag(Loc, diag::error_implicit_ivar_access) |
| 2184 | << IV->getDeclName(); |
| 2185 | Diag(ND->getLocation(), diag::note_declared_at); |
| 2186 | return ExprError(); |
| 2187 | } |
| Fariborz Jahanian | 66a6c06 | 2011-04-15 17:04:42 +0000 | [diff] [blame] | 2188 | } |
| Fariborz Jahanian | 82bc436 | 2011-04-12 23:39:33 +0000 | [diff] [blame] | 2189 | } |
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2190 | return Owned(new (Context) |
| 2191 | ObjCIvarRefExpr(IV, IV->getType(), Loc, |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2192 | SelfExpr.take(), true, true)); |
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2193 | } |
| Chris Lattner | 8731366 | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 2194 | } else if (CurMethod->isInstanceMethod()) { |
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2195 | // We should warn if a local variable hides an ivar. |
| Chris Lattner | 8731366 | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 2196 | ObjCInterfaceDecl *IFace = CurMethod->getClassInterface(); |
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2197 | ObjCInterfaceDecl *ClassDeclared; |
| 2198 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
| 2199 | if (IV->getAccessControl() != ObjCIvarDecl::Private || |
| 2200 | IFace == ClassDeclared) |
| 2201 | Diag(Loc, diag::warn_ivar_use_hidden) << IV->getDeclName(); |
| 2202 | } |
| 2203 | } |
| 2204 | |
| Fariborz Jahanian | 6fada5b | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 2205 | if (Lookup.empty() && II && AllowBuiltinCreation) { |
| 2206 | // FIXME. Consolidate this with similar code in LookupName. |
| 2207 | if (unsigned BuiltinID = II->getBuiltinID()) { |
| 2208 | if (!(getLangOptions().CPlusPlus && |
| 2209 | Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))) { |
| 2210 | NamedDecl *D = LazilyCreateBuiltin((IdentifierInfo *)II, BuiltinID, |
| 2211 | S, Lookup.isForRedeclaration(), |
| 2212 | Lookup.getNameLoc()); |
| 2213 | if (D) Lookup.addDecl(D); |
| 2214 | } |
| 2215 | } |
| 2216 | } |
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2217 | // Sentinel value saying that we didn't do anything special. |
| 2218 | return Owned((Expr*) 0); |
| Douglas Gregor | 3256d04 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 2219 | } |
| John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2220 | |
| John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2221 | /// \brief Cast a base object to a member's actual type. |
| 2222 | /// |
| 2223 | /// Logically this happens in three phases: |
| 2224 | /// |
| 2225 | /// * First we cast from the base type to the naming class. |
| 2226 | /// The naming class is the class into which we were looking |
| 2227 | /// when we found the member; it's the qualifier type if a |
| 2228 | /// qualifier was provided, and otherwise it's the base type. |
| 2229 | /// |
| 2230 | /// * Next we cast from the naming class to the declaring class. |
| 2231 | /// If the member we found was brought into a class's scope by |
| 2232 | /// a using declaration, this is that class; otherwise it's |
| 2233 | /// the class declaring the member. |
| 2234 | /// |
| 2235 | /// * Finally we cast from the declaring class to the "true" |
| 2236 | /// declaring class of the member. This conversion does not |
| 2237 | /// obey access control. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2238 | ExprResult |
| 2239 | Sema::PerformObjectMemberConversion(Expr *From, |
| Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2240 | NestedNameSpecifier *Qualifier, |
| John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2241 | NamedDecl *FoundDecl, |
| Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2242 | NamedDecl *Member) { |
| 2243 | CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Member->getDeclContext()); |
| 2244 | if (!RD) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2245 | return Owned(From); |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2246 | |
| Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2247 | QualType DestRecordType; |
| 2248 | QualType DestType; |
| 2249 | QualType FromRecordType; |
| 2250 | QualType FromType = From->getType(); |
| 2251 | bool PointerConversions = false; |
| 2252 | if (isa<FieldDecl>(Member)) { |
| 2253 | DestRecordType = Context.getCanonicalType(Context.getTypeDeclType(RD)); |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2254 | |
| Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2255 | if (FromType->getAs<PointerType>()) { |
| 2256 | DestType = Context.getPointerType(DestRecordType); |
| 2257 | FromRecordType = FromType->getPointeeType(); |
| 2258 | PointerConversions = true; |
| 2259 | } else { |
| 2260 | DestType = DestRecordType; |
| 2261 | FromRecordType = FromType; |
| Fariborz Jahanian | bb67b82 | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 2262 | } |
| Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2263 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member)) { |
| 2264 | if (Method->isStatic()) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2265 | return Owned(From); |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2266 | |
| Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2267 | DestType = Method->getThisType(Context); |
| 2268 | DestRecordType = DestType->getPointeeType(); |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2269 | |
| Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2270 | if (FromType->getAs<PointerType>()) { |
| 2271 | FromRecordType = FromType->getPointeeType(); |
| 2272 | PointerConversions = true; |
| 2273 | } else { |
| 2274 | FromRecordType = FromType; |
| 2275 | DestType = DestRecordType; |
| 2276 | } |
| 2277 | } else { |
| 2278 | // No conversion necessary. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2279 | return Owned(From); |
| Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2280 | } |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2281 | |
| Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2282 | if (DestType->isDependentType() || FromType->isDependentType()) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2283 | return Owned(From); |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2284 | |
| Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2285 | // If the unqualified types are the same, no conversion is necessary. |
| 2286 | if (Context.hasSameUnqualifiedType(FromRecordType, DestRecordType)) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2287 | return Owned(From); |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2288 | |
| John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2289 | SourceRange FromRange = From->getSourceRange(); |
| 2290 | SourceLocation FromLoc = FromRange.getBegin(); |
| 2291 | |
| John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2292 | ExprValueKind VK = CastCategory(From); |
| Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2293 | |
| Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2294 | // C++ [class.member.lookup]p8: |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2295 | // [...] Ambiguities can often be resolved by qualifying a name with its |
| Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2296 | // class name. |
| 2297 | // |
| 2298 | // If the member was a qualified name and the qualified referred to a |
| 2299 | // specific base subobject type, we'll cast to that intermediate type |
| 2300 | // first and then to the object in which the member is declared. That allows |
| 2301 | // one to resolve ambiguities in, e.g., a diamond-shaped hierarchy such as: |
| 2302 | // |
| 2303 | // class Base { public: int x; }; |
| 2304 | // class Derived1 : public Base { }; |
| 2305 | // class Derived2 : public Base { }; |
| 2306 | // class VeryDerived : public Derived1, public Derived2 { void f(); }; |
| 2307 | // |
| 2308 | // void VeryDerived::f() { |
| 2309 | // x = 17; // error: ambiguous base subobjects |
| 2310 | // Derived1::x = 17; // okay, pick the Base subobject of Derived1 |
| 2311 | // } |
| Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2312 | if (Qualifier) { |
| John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2313 | QualType QType = QualType(Qualifier->getAsType(), 0); |
| 2314 | assert(!QType.isNull() && "lookup done with dependent qualifier?"); |
| 2315 | assert(QType->isRecordType() && "lookup done with non-record type"); |
| 2316 | |
| 2317 | QualType QRecordType = QualType(QType->getAs<RecordType>(), 0); |
| 2318 | |
| 2319 | // In C++98, the qualifier type doesn't actually have to be a base |
| 2320 | // type of the object type, in which case we just ignore it. |
| 2321 | // Otherwise build the appropriate casts. |
| 2322 | if (IsDerivedFrom(FromRecordType, QRecordType)) { |
| John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2323 | CXXCastPath BasePath; |
| John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2324 | if (CheckDerivedToBaseConversion(FromRecordType, QRecordType, |
| Anders Carlsson | b78feca | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2325 | FromLoc, FromRange, &BasePath)) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2326 | return ExprError(); |
| John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2327 | |
| Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2328 | if (PointerConversions) |
| John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2329 | QType = Context.getPointerType(QType); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2330 | From = ImpCastExprToType(From, QType, CK_UncheckedDerivedToBase, |
| 2331 | VK, &BasePath).take(); |
| John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2332 | |
| 2333 | FromType = QType; |
| 2334 | FromRecordType = QRecordType; |
| 2335 | |
| 2336 | // If the qualifier type was the same as the destination type, |
| 2337 | // we're done. |
| 2338 | if (Context.hasSameUnqualifiedType(FromRecordType, DestRecordType)) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2339 | return Owned(From); |
| Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2340 | } |
| 2341 | } |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2342 | |
| John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2343 | bool IgnoreAccess = false; |
| Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2344 | |
| John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2345 | // If we actually found the member through a using declaration, cast |
| 2346 | // down to the using declaration's type. |
| 2347 | // |
| 2348 | // Pointer equality is fine here because only one declaration of a |
| 2349 | // class ever has member declarations. |
| 2350 | if (FoundDecl->getDeclContext() != Member->getDeclContext()) { |
| 2351 | assert(isa<UsingShadowDecl>(FoundDecl)); |
| 2352 | QualType URecordType = Context.getTypeDeclType( |
| 2353 | cast<CXXRecordDecl>(FoundDecl->getDeclContext())); |
| 2354 | |
| 2355 | // We only need to do this if the naming-class to declaring-class |
| 2356 | // conversion is non-trivial. |
| 2357 | if (!Context.hasSameUnqualifiedType(FromRecordType, URecordType)) { |
| 2358 | assert(IsDerivedFrom(FromRecordType, URecordType)); |
| John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2359 | CXXCastPath BasePath; |
| John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2360 | if (CheckDerivedToBaseConversion(FromRecordType, URecordType, |
| Anders Carlsson | b78feca | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2361 | FromLoc, FromRange, &BasePath)) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2362 | return ExprError(); |
| Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 2363 | |
| John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2364 | QualType UType = URecordType; |
| 2365 | if (PointerConversions) |
| 2366 | UType = Context.getPointerType(UType); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2367 | From = ImpCastExprToType(From, UType, CK_UncheckedDerivedToBase, |
| 2368 | VK, &BasePath).take(); |
| John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2369 | FromType = UType; |
| 2370 | FromRecordType = URecordType; |
| 2371 | } |
| 2372 | |
| 2373 | // We don't do access control for the conversion from the |
| 2374 | // declaring class to the true declaring class. |
| 2375 | IgnoreAccess = true; |
| Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2376 | } |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2377 | |
| John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2378 | CXXCastPath BasePath; |
| Anders Carlsson | b78feca | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2379 | if (CheckDerivedToBaseConversion(FromRecordType, DestRecordType, |
| 2380 | FromLoc, FromRange, &BasePath, |
| John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2381 | IgnoreAccess)) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2382 | return ExprError(); |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2383 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2384 | return ImpCastExprToType(From, DestType, CK_UncheckedDerivedToBase, |
| 2385 | VK, &BasePath); |
| Fariborz Jahanian | bb67b82 | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 2386 | } |
| Douglas Gregor | 3256d04 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 2387 | |
| Douglas Gregor | f405d7e | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 2388 | /// \brief Build a MemberExpr AST node. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2389 | static MemberExpr *BuildMemberExpr(ASTContext &C, Expr *Base, bool isArrow, |
| Eli Friedman | 2cfcef6 | 2009-12-04 06:40:45 +0000 | [diff] [blame] | 2390 | const CXXScopeSpec &SS, ValueDecl *Member, |
| John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 2391 | DeclAccessPair FoundDecl, |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2392 | const DeclarationNameInfo &MemberNameInfo, |
| 2393 | QualType Ty, |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2394 | ExprValueKind VK, ExprObjectKind OK, |
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2395 | const TemplateArgumentListInfo *TemplateArgs = 0) { |
| Douglas Gregor | ea972d3 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 2396 | return MemberExpr::Create(C, Base, isArrow, SS.getWithLocInContext(C), |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2397 | Member, FoundDecl, MemberNameInfo, |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2398 | TemplateArgs, Ty, VK, OK); |
| Douglas Gregor | c190523 | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 2399 | } |
| 2400 | |
| John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 2401 | static ExprResult |
| 2402 | BuildFieldReferenceExpr(Sema &S, Expr *BaseExpr, bool IsArrow, |
| 2403 | const CXXScopeSpec &SS, FieldDecl *Field, |
| 2404 | DeclAccessPair FoundDecl, |
| 2405 | const DeclarationNameInfo &MemberNameInfo) { |
| 2406 | // x.a is an l-value if 'a' has a reference type. Otherwise: |
| 2407 | // x.a is an l-value/x-value/pr-value if the base is (and note |
| 2408 | // that *x is always an l-value), except that if the base isn't |
| 2409 | // an ordinary object then we must have an rvalue. |
| 2410 | ExprValueKind VK = VK_LValue; |
| 2411 | ExprObjectKind OK = OK_Ordinary; |
| 2412 | if (!IsArrow) { |
| 2413 | if (BaseExpr->getObjectKind() == OK_Ordinary) |
| 2414 | VK = BaseExpr->getValueKind(); |
| 2415 | else |
| 2416 | VK = VK_RValue; |
| 2417 | } |
| 2418 | if (VK != VK_RValue && Field->isBitField()) |
| 2419 | OK = OK_BitField; |
| 2420 | |
| 2421 | // Figure out the type of the member; see C99 6.5.2.3p3, C++ [expr.ref] |
| 2422 | QualType MemberType = Field->getType(); |
| 2423 | if (const ReferenceType *Ref = MemberType->getAs<ReferenceType>()) { |
| 2424 | MemberType = Ref->getPointeeType(); |
| 2425 | VK = VK_LValue; |
| 2426 | } else { |
| 2427 | QualType BaseType = BaseExpr->getType(); |
| 2428 | if (IsArrow) BaseType = BaseType->getAs<PointerType>()->getPointeeType(); |
| 2429 | |
| 2430 | Qualifiers BaseQuals = BaseType.getQualifiers(); |
| 2431 | |
| 2432 | // GC attributes are never picked up by members. |
| 2433 | BaseQuals.removeObjCGCAttr(); |
| 2434 | |
| 2435 | // CVR attributes from the base are picked up by members, |
| 2436 | // except that 'mutable' members don't pick up 'const'. |
| 2437 | if (Field->isMutable()) BaseQuals.removeConst(); |
| 2438 | |
| 2439 | Qualifiers MemberQuals |
| 2440 | = S.Context.getCanonicalType(MemberType).getQualifiers(); |
| 2441 | |
| 2442 | // TR 18037 does not allow fields to be declared with address spaces. |
| 2443 | assert(!MemberQuals.hasAddressSpace()); |
| 2444 | |
| 2445 | Qualifiers Combined = BaseQuals + MemberQuals; |
| 2446 | if (Combined != MemberQuals) |
| 2447 | MemberType = S.Context.getQualifiedType(MemberType, Combined); |
| 2448 | } |
| 2449 | |
| 2450 | S.MarkDeclarationReferenced(MemberNameInfo.getLoc(), Field); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2451 | ExprResult Base = |
| 2452 | S.PerformObjectMemberConversion(BaseExpr, SS.getScopeRep(), |
| 2453 | FoundDecl, Field); |
| 2454 | if (Base.isInvalid()) |
| John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 2455 | return ExprError(); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2456 | return S.Owned(BuildMemberExpr(S.Context, Base.take(), IsArrow, SS, |
| John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 2457 | Field, FoundDecl, MemberNameInfo, |
| 2458 | MemberType, VK, OK)); |
| 2459 | } |
| 2460 | |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2461 | /// Builds an implicit member access expression. The current context |
| 2462 | /// is known to be an instance method, and the given unqualified lookup |
| 2463 | /// set is known to contain only instance members, at least one of which |
| 2464 | /// is from an appropriate type. |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2465 | ExprResult |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2466 | Sema::BuildImplicitMemberExpr(const CXXScopeSpec &SS, |
| 2467 | LookupResult &R, |
| 2468 | const TemplateArgumentListInfo *TemplateArgs, |
| 2469 | bool IsKnownInstance) { |
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2470 | assert(!R.empty() && !R.isAmbiguous()); |
| 2471 | |
| John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2472 | SourceLocation loc = R.getNameLoc(); |
| Sebastian Redl | 3d3f75a | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 2473 | |
| Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 2474 | // We may have found a field within an anonymous union or struct |
| 2475 | // (C++ [class.union]). |
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2476 | // FIXME: template-ids inside anonymous structs? |
| Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2477 | if (IndirectFieldDecl *FD = R.getAsSingle<IndirectFieldDecl>()) |
| John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2478 | return BuildAnonymousStructUnionMemberReference(SS, R.getNameLoc(), FD); |
| Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2479 | |
| John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2480 | // If this is known to be an instance access, go ahead and build an |
| 2481 | // implicit 'this' expression now. |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2482 | // 'this' expression now. |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2483 | QualType ThisTy = getAndCaptureCurrentThisType(); |
| 2484 | assert(!ThisTy.isNull() && "didn't correctly pre-flight capture of 'this'"); |
| John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2485 | |
| John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2486 | Expr *baseExpr = 0; // null signifies implicit access |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2487 | if (IsKnownInstance) { |
| Douglas Gregor | b15af89 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 2488 | SourceLocation Loc = R.getNameLoc(); |
| 2489 | if (SS.getRange().isValid()) |
| 2490 | Loc = SS.getRange().getBegin(); |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2491 | baseExpr = new (Context) CXXThisExpr(loc, ThisTy, /*isImplicit=*/true); |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2492 | } |
| 2493 | |
| Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2494 | return BuildMemberReferenceExpr(baseExpr, ThisTy, |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2495 | /*OpLoc*/ SourceLocation(), |
| 2496 | /*IsArrow*/ true, |
| John McCall | 38836f0 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 2497 | SS, |
| 2498 | /*FirstQualifierInScope*/ 0, |
| 2499 | R, TemplateArgs); |
| John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2500 | } |
| 2501 | |
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2502 | bool Sema::UseArgumentDependentLookup(const CXXScopeSpec &SS, |
| John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2503 | const LookupResult &R, |
| 2504 | bool HasTrailingLParen) { |
| John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2505 | // Only when used directly as the postfix-expression of a call. |
| 2506 | if (!HasTrailingLParen) |
| 2507 | return false; |
| 2508 | |
| 2509 | // Never if a scope specifier was provided. |
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2510 | if (SS.isSet()) |
| John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2511 | return false; |
| 2512 | |
| 2513 | // Only in C++ or ObjC++. |
| John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2514 | if (!getLangOptions().CPlusPlus) |
| John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2515 | return false; |
| 2516 | |
| 2517 | // Turn off ADL when we find certain kinds of declarations during |
| 2518 | // normal lookup: |
| 2519 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 2520 | NamedDecl *D = *I; |
| 2521 | |
| 2522 | // C++0x [basic.lookup.argdep]p3: |
| 2523 | // -- a declaration of a class member |
| 2524 | // Since using decls preserve this property, we check this on the |
| 2525 | // original decl. |
| John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2526 | if (D->isCXXClassMember()) |
| John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2527 | return false; |
| 2528 | |
| 2529 | // C++0x [basic.lookup.argdep]p3: |
| 2530 | // -- a block-scope function declaration that is not a |
| 2531 | // using-declaration |
| 2532 | // NOTE: we also trigger this for function templates (in fact, we |
| 2533 | // don't check the decl type at all, since all other decl types |
| 2534 | // turn off ADL anyway). |
| 2535 | if (isa<UsingShadowDecl>(D)) |
| 2536 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 2537 | else if (D->getDeclContext()->isFunctionOrMethod()) |
| 2538 | return false; |
| 2539 | |
| 2540 | // C++0x [basic.lookup.argdep]p3: |
| 2541 | // -- a declaration that is neither a function or a function |
| 2542 | // template |
| 2543 | // And also for builtin functions. |
| 2544 | if (isa<FunctionDecl>(D)) { |
| 2545 | FunctionDecl *FDecl = cast<FunctionDecl>(D); |
| 2546 | |
| 2547 | // But also builtin functions. |
| 2548 | if (FDecl->getBuiltinID() && FDecl->isImplicit()) |
| 2549 | return false; |
| 2550 | } else if (!isa<FunctionTemplateDecl>(D)) |
| 2551 | return false; |
| 2552 | } |
| 2553 | |
| 2554 | return true; |
| 2555 | } |
| 2556 | |
| 2557 | |
| John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2558 | /// Diagnoses obvious problems with the use of the given declaration |
| 2559 | /// as an expression. This is only actually called for lookups that |
| 2560 | /// were not overloaded, and it doesn't promise that the declaration |
| 2561 | /// will in fact be used. |
| 2562 | static bool CheckDeclInExpr(Sema &S, SourceLocation Loc, NamedDecl *D) { |
| Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2563 | if (isa<TypedefNameDecl>(D)) { |
| John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2564 | S.Diag(Loc, diag::err_unexpected_typedef) << D->getDeclName(); |
| 2565 | return true; |
| 2566 | } |
| 2567 | |
| 2568 | if (isa<ObjCInterfaceDecl>(D)) { |
| 2569 | S.Diag(Loc, diag::err_unexpected_interface) << D->getDeclName(); |
| 2570 | return true; |
| 2571 | } |
| 2572 | |
| 2573 | if (isa<NamespaceDecl>(D)) { |
| 2574 | S.Diag(Loc, diag::err_unexpected_namespace) << D->getDeclName(); |
| 2575 | return true; |
| 2576 | } |
| 2577 | |
| 2578 | return false; |
| 2579 | } |
| 2580 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2581 | ExprResult |
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2582 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
| John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2583 | LookupResult &R, |
| 2584 | bool NeedsADL) { |
| John McCall | 3a60c87 | 2009-12-08 22:45:53 +0000 | [diff] [blame] | 2585 | // If this is a single, fully-resolved result and we don't need ADL, |
| 2586 | // just build an ordinary singleton decl ref. |
| Douglas Gregor | 4b4844f | 2010-01-29 17:15:43 +0000 | [diff] [blame] | 2587 | if (!NeedsADL && R.isSingleResult() && !R.getAsSingle<FunctionTemplateDecl>()) |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2588 | return BuildDeclarationNameExpr(SS, R.getLookupNameInfo(), |
| 2589 | R.getFoundDecl()); |
| John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2590 | |
| 2591 | // We only need to check the declaration if there's exactly one |
| 2592 | // result, because in the overloaded case the results can only be |
| 2593 | // functions and function templates. |
| John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2594 | if (R.isSingleResult() && |
| 2595 | CheckDeclInExpr(*this, R.getNameLoc(), R.getFoundDecl())) |
| John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2596 | return ExprError(); |
| 2597 | |
| John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2598 | // Otherwise, just build an unresolved lookup expression. Suppress |
| 2599 | // any lookup-related diagnostics; we'll hash these out later, when |
| 2600 | // we've picked a target. |
| 2601 | R.suppressDiagnostics(); |
| 2602 | |
| John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2603 | UnresolvedLookupExpr *ULE |
| Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2604 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
| Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2605 | SS.getWithLocInContext(Context), |
| 2606 | R.getLookupNameInfo(), |
| Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 2607 | NeedsADL, R.isOverloadedResult(), |
| 2608 | R.begin(), R.end()); |
| John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2609 | |
| 2610 | return Owned(ULE); |
| 2611 | } |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2612 | |
| John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2613 | /// \brief Complete semantic analysis for a reference to the given declaration. |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2614 | ExprResult |
| John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2615 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2616 | const DeclarationNameInfo &NameInfo, |
| 2617 | NamedDecl *D) { |
| John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2618 | assert(D && "Cannot refer to a NULL declaration"); |
| John McCall | 283b901 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 2619 | assert(!isa<FunctionTemplateDecl>(D) && |
| 2620 | "Cannot refer unambiguously to a function template"); |
| John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2621 | |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2622 | SourceLocation Loc = NameInfo.getLoc(); |
| John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2623 | if (CheckDeclInExpr(*this, Loc, D)) |
| 2624 | return ExprError(); |
| Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 2625 | |
| Douglas Gregor | e7488b9 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2626 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) { |
| 2627 | // Specifically diagnose references to class templates that are missing |
| 2628 | // a template argument list. |
| 2629 | Diag(Loc, diag::err_template_decl_ref) |
| 2630 | << Template << SS.getRange(); |
| 2631 | Diag(Template->getLocation(), diag::note_template_decl_here); |
| 2632 | return ExprError(); |
| 2633 | } |
| 2634 | |
| 2635 | // Make sure that we're referring to a value. |
| 2636 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 2637 | if (!VD) { |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2638 | Diag(Loc, diag::err_ref_non_value) |
| Douglas Gregor | e7488b9 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2639 | << D << SS.getRange(); |
| John McCall | b48971d | 2009-12-18 18:35:10 +0000 | [diff] [blame] | 2640 | Diag(D->getLocation(), diag::note_declared_at); |
| Douglas Gregor | e7488b9 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2641 | return ExprError(); |
| 2642 | } |
| Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2643 | |
| Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2644 | // Check whether this declaration can be used. Note that we suppress |
| 2645 | // this check when we're going to perform argument-dependent lookup |
| 2646 | // on this function name, because this might not be the function |
| 2647 | // that overload resolution actually selects. |
| John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2648 | if (DiagnoseUseOfDecl(VD, Loc)) |
| Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2649 | return ExprError(); |
| 2650 | |
| Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2651 | // Only create DeclRefExpr's for valid Decl's. |
| 2652 | if (VD->isInvalidDecl()) |
| Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2653 | return ExprError(); |
| 2654 | |
| John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2655 | // Handle members of anonymous structs and unions. If we got here, |
| 2656 | // and the reference is to a class member indirect field, then this |
| 2657 | // must be the subject of a pointer-to-member expression. |
| 2658 | if (IndirectFieldDecl *indirectField = dyn_cast<IndirectFieldDecl>(VD)) |
| 2659 | if (!indirectField->isCXXClassMember()) |
| 2660 | return BuildAnonymousStructUnionMemberReference(SS, NameInfo.getLoc(), |
| 2661 | indirectField); |
| Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2662 | |
| Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 2663 | // If the identifier reference is inside a block, and it refers to a value |
| 2664 | // that is outside the block, create a BlockDeclRefExpr instead of a |
| 2665 | // DeclRefExpr. This ensures the value is treated as a copy-in snapshot when |
| 2666 | // the block is formed. |
| Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2667 | // |
| Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 2668 | // We do not do this for things like enum constants, global variables, etc, |
| 2669 | // as they do not get snapshotted. |
| 2670 | // |
| John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2671 | switch (shouldCaptureValueReference(*this, NameInfo.getLoc(), VD)) { |
| John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2672 | case CR_Error: |
| 2673 | return ExprError(); |
| Mike Stump | 7dafa0d | 2010-01-05 02:56:35 +0000 | [diff] [blame] | 2674 | |
| John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2675 | case CR_Capture: |
| John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2676 | assert(!SS.isSet() && "referenced local variable with scope specifier?"); |
| 2677 | return BuildBlockDeclRefExpr(*this, VD, NameInfo, /*byref*/ false); |
| 2678 | |
| 2679 | case CR_CaptureByRef: |
| 2680 | assert(!SS.isSet() && "referenced local variable with scope specifier?"); |
| 2681 | return BuildBlockDeclRefExpr(*this, VD, NameInfo, /*byref*/ true); |
| John McCall | f4cd4f9 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2682 | |
| 2683 | case CR_NoCapture: { |
| 2684 | // If this reference is not in a block or if the referenced |
| 2685 | // variable is within the block, create a normal DeclRefExpr. |
| 2686 | |
| 2687 | QualType type = VD->getType(); |
| Daniel Dunbar | 7c2dc36 | 2011-02-10 18:29:28 +0000 | [diff] [blame] | 2688 | ExprValueKind valueKind = VK_RValue; |
| John McCall | f4cd4f9 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2689 | |
| 2690 | switch (D->getKind()) { |
| 2691 | // Ignore all the non-ValueDecl kinds. |
| 2692 | #define ABSTRACT_DECL(kind) |
| 2693 | #define VALUE(type, base) |
| 2694 | #define DECL(type, base) \ |
| 2695 | case Decl::type: |
| 2696 | #include "clang/AST/DeclNodes.inc" |
| 2697 | llvm_unreachable("invalid value decl kind"); |
| 2698 | return ExprError(); |
| 2699 | |
| 2700 | // These shouldn't make it here. |
| 2701 | case Decl::ObjCAtDefsField: |
| 2702 | case Decl::ObjCIvar: |
| 2703 | llvm_unreachable("forming non-member reference to ivar?"); |
| 2704 | return ExprError(); |
| 2705 | |
| 2706 | // Enum constants are always r-values and never references. |
| 2707 | // Unresolved using declarations are dependent. |
| 2708 | case Decl::EnumConstant: |
| 2709 | case Decl::UnresolvedUsingValue: |
| 2710 | valueKind = VK_RValue; |
| 2711 | break; |
| 2712 | |
| 2713 | // Fields and indirect fields that got here must be for |
| 2714 | // pointer-to-member expressions; we just call them l-values for |
| 2715 | // internal consistency, because this subexpression doesn't really |
| 2716 | // exist in the high-level semantics. |
| 2717 | case Decl::Field: |
| 2718 | case Decl::IndirectField: |
| 2719 | assert(getLangOptions().CPlusPlus && |
| 2720 | "building reference to field in C?"); |
| 2721 | |
| 2722 | // These can't have reference type in well-formed programs, but |
| 2723 | // for internal consistency we do this anyway. |
| 2724 | type = type.getNonReferenceType(); |
| 2725 | valueKind = VK_LValue; |
| 2726 | break; |
| 2727 | |
| 2728 | // Non-type template parameters are either l-values or r-values |
| 2729 | // depending on the type. |
| 2730 | case Decl::NonTypeTemplateParm: { |
| 2731 | if (const ReferenceType *reftype = type->getAs<ReferenceType>()) { |
| 2732 | type = reftype->getPointeeType(); |
| 2733 | valueKind = VK_LValue; // even if the parameter is an r-value reference |
| 2734 | break; |
| 2735 | } |
| 2736 | |
| 2737 | // For non-references, we need to strip qualifiers just in case |
| 2738 | // the template parameter was declared as 'const int' or whatever. |
| 2739 | valueKind = VK_RValue; |
| 2740 | type = type.getUnqualifiedType(); |
| 2741 | break; |
| 2742 | } |
| 2743 | |
| 2744 | case Decl::Var: |
| 2745 | // In C, "extern void blah;" is valid and is an r-value. |
| 2746 | if (!getLangOptions().CPlusPlus && |
| 2747 | !type.hasQualifiers() && |
| 2748 | type->isVoidType()) { |
| 2749 | valueKind = VK_RValue; |
| 2750 | break; |
| 2751 | } |
| 2752 | // fallthrough |
| 2753 | |
| 2754 | case Decl::ImplicitParam: |
| 2755 | case Decl::ParmVar: |
| 2756 | // These are always l-values. |
| 2757 | valueKind = VK_LValue; |
| 2758 | type = type.getNonReferenceType(); |
| 2759 | break; |
| 2760 | |
| 2761 | case Decl::Function: { |
| John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2762 | const FunctionType *fty = type->castAs<FunctionType>(); |
| 2763 | |
| 2764 | // If we're referring to a function with an __unknown_anytype |
| 2765 | // result type, make the entire expression __unknown_anytype. |
| 2766 | if (fty->getResultType() == Context.UnknownAnyTy) { |
| 2767 | type = Context.UnknownAnyTy; |
| 2768 | valueKind = VK_RValue; |
| 2769 | break; |
| 2770 | } |
| 2771 | |
| John McCall | f4cd4f9 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2772 | // Functions are l-values in C++. |
| 2773 | if (getLangOptions().CPlusPlus) { |
| 2774 | valueKind = VK_LValue; |
| 2775 | break; |
| 2776 | } |
| 2777 | |
| 2778 | // C99 DR 316 says that, if a function type comes from a |
| 2779 | // function definition (without a prototype), that type is only |
| 2780 | // used for checking compatibility. Therefore, when referencing |
| 2781 | // the function, we pretend that we don't have the full function |
| 2782 | // type. |
| John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2783 | if (!cast<FunctionDecl>(VD)->hasPrototype() && |
| 2784 | isa<FunctionProtoType>(fty)) |
| 2785 | type = Context.getFunctionNoProtoType(fty->getResultType(), |
| 2786 | fty->getExtInfo()); |
| John McCall | f4cd4f9 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2787 | |
| 2788 | // Functions are r-values in C. |
| 2789 | valueKind = VK_RValue; |
| 2790 | break; |
| 2791 | } |
| 2792 | |
| 2793 | case Decl::CXXMethod: |
| John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2794 | // If we're referring to a method with an __unknown_anytype |
| 2795 | // result type, make the entire expression __unknown_anytype. |
| 2796 | // This should only be possible with a type written directly. |
| 2797 | if (const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(VD->getType())) |
| 2798 | if (proto->getResultType() == Context.UnknownAnyTy) { |
| 2799 | type = Context.UnknownAnyTy; |
| 2800 | valueKind = VK_RValue; |
| 2801 | break; |
| 2802 | } |
| 2803 | |
| John McCall | f4cd4f9 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2804 | // C++ methods are l-values if static, r-values if non-static. |
| 2805 | if (cast<CXXMethodDecl>(VD)->isStatic()) { |
| 2806 | valueKind = VK_LValue; |
| 2807 | break; |
| 2808 | } |
| 2809 | // fallthrough |
| 2810 | |
| 2811 | case Decl::CXXConversion: |
| 2812 | case Decl::CXXDestructor: |
| 2813 | case Decl::CXXConstructor: |
| 2814 | valueKind = VK_RValue; |
| 2815 | break; |
| 2816 | } |
| 2817 | |
| 2818 | return BuildDeclRefExpr(VD, type, valueKind, NameInfo, &SS); |
| 2819 | } |
| 2820 | |
| John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2821 | } |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2822 | |
| John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2823 | llvm_unreachable("unknown capture result"); |
| 2824 | return ExprError(); |
| Chris Lattner | 17ed487 | 2006-11-20 04:58:19 +0000 | [diff] [blame] | 2825 | } |
| Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2826 | |
| John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2827 | ExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc, tok::TokenKind Kind) { |
| Chris Lattner | 6307f19 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 2828 | PredefinedExpr::IdentType IT; |
| Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2829 | |
| Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2830 | switch (Kind) { |
| Chris Lattner | 317e6ba | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 2831 | default: assert(0 && "Unknown simple primary expr!"); |
| Chris Lattner | 6307f19 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 2832 | case tok::kw___func__: IT = PredefinedExpr::Func; break; // [C99 6.4.2.2] |
| 2833 | case tok::kw___FUNCTION__: IT = PredefinedExpr::Function; break; |
| 2834 | case tok::kw___PRETTY_FUNCTION__: IT = PredefinedExpr::PrettyFunction; break; |
| Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2835 | } |
| Chris Lattner | 317e6ba | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 2836 | |
| Chris Lattner | a81a027 | 2008-01-12 08:14:25 +0000 | [diff] [blame] | 2837 | // Pre-defined identifiers are of type char[x], where x is the length of the |
| 2838 | // string. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2839 | |
| Anders Carlsson | 2fb0824 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2840 | Decl *currentDecl = getCurFunctionOrMethodDecl(); |
| Fariborz Jahanian | 9462744 | 2010-07-23 21:53:24 +0000 | [diff] [blame] | 2841 | if (!currentDecl && getCurBlock()) |
| 2842 | currentDecl = getCurBlock()->TheDecl; |
| Anders Carlsson | 2fb0824 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2843 | if (!currentDecl) { |
| Chris Lattner | f45c5ec | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 2844 | Diag(Loc, diag::ext_predef_outside_function); |
| Anders Carlsson | 2fb0824 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2845 | currentDecl = Context.getTranslationUnitDecl(); |
| Chris Lattner | f45c5ec | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 2846 | } |
| Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2847 | |
| Anders Carlsson | 0b209a8 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2848 | QualType ResTy; |
| 2849 | if (cast<DeclContext>(currentDecl)->isDependentContext()) { |
| 2850 | ResTy = Context.DependentTy; |
| 2851 | } else { |
| Anders Carlsson | 5bd8d19 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 2852 | unsigned Length = PredefinedExpr::ComputeName(IT, currentDecl).length(); |
| Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2853 | |
| Anders Carlsson | 0b209a8 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2854 | llvm::APInt LengthI(32, Length + 1); |
| John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2855 | ResTy = Context.CharTy.withConst(); |
| Anders Carlsson | 0b209a8 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2856 | ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0); |
| 2857 | } |
| Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2858 | return Owned(new (Context) PredefinedExpr(Loc, ResTy, IT)); |
| Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2859 | } |
| 2860 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2861 | ExprResult Sema::ActOnCharacterConstant(const Token &Tok) { |
| Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 2862 | llvm::SmallString<16> CharBuffer; |
| Douglas Gregor | dc970f0 | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2863 | bool Invalid = false; |
| 2864 | llvm::StringRef ThisTok = PP.getSpelling(Tok, CharBuffer, &Invalid); |
| 2865 | if (Invalid) |
| 2866 | return ExprError(); |
| Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2867 | |
| Benjamin Kramer | 0a1abd4 | 2010-02-27 13:44:12 +0000 | [diff] [blame] | 2868 | CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(), Tok.getLocation(), |
| 2869 | PP); |
| Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 2870 | if (Literal.hadError()) |
| Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2871 | return ExprError(); |
| Chris Lattner | ef24b38 | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 2872 | |
| Chris Lattner | c3847ba | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 2873 | QualType Ty; |
| 2874 | if (!getLangOptions().CPlusPlus) |
| 2875 | Ty = Context.IntTy; // 'x' and L'x' -> int in C. |
| 2876 | else if (Literal.isWide()) |
| 2877 | Ty = Context.WCharTy; // L'x' -> wchar_t in C++. |
| Eli Friedman | eb1df70 | 2010-02-03 18:21:45 +0000 | [diff] [blame] | 2878 | else if (Literal.isMultiChar()) |
| 2879 | Ty = Context.IntTy; // 'wxyz' -> int in C++. |
| Chris Lattner | c3847ba | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 2880 | else |
| 2881 | Ty = Context.CharTy; // 'x' -> char in C++ |
| Chris Lattner | ef24b38 | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 2882 | |
| Sebastian Redl | 20614a7 | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 2883 | return Owned(new (Context) CharacterLiteral(Literal.getValue(), |
| 2884 | Literal.isWide(), |
| Chris Lattner | c3847ba | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 2885 | Ty, Tok.getLocation())); |
| Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 2886 | } |
| 2887 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2888 | ExprResult Sema::ActOnNumericConstant(const Token &Tok) { |
| Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2889 | // Fast path for a single digit (which is quite common). A single digit |
| Steve Naroff | f2fb89e | 2007-03-13 20:29:44 +0000 | [diff] [blame] | 2890 | // cannot have a trigraph, escaped newline, radix prefix, or type suffix. |
| 2891 | if (Tok.getLength() == 1) { |
| Chris Lattner | 9240b3e | 2009-01-26 22:36:52 +0000 | [diff] [blame] | 2892 | const char Val = PP.getSpellingOfSingleCharacterNumericConstant(Tok); |
| Chris Lattner | c4c1819 | 2009-01-16 07:10:29 +0000 | [diff] [blame] | 2893 | unsigned IntSize = Context.Target.getIntWidth(); |
| Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2894 | return Owned(IntegerLiteral::Create(Context, llvm::APInt(IntSize, Val-'0'), |
| Steve Naroff | 5faaef7 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 2895 | Context.IntTy, Tok.getLocation())); |
| Steve Naroff | f2fb89e | 2007-03-13 20:29:44 +0000 | [diff] [blame] | 2896 | } |
| Ted Kremenek | e981418 | 2009-01-13 23:19:12 +0000 | [diff] [blame] | 2897 | |
| Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 2898 | llvm::SmallString<512> IntegerBuffer; |
| Chris Lattner | a1cf5f9 | 2008-09-30 20:53:45 +0000 | [diff] [blame] | 2899 | // Add padding so that NumericLiteralParser can overread by one character. |
| 2900 | IntegerBuffer.resize(Tok.getLength()+1); |
| Steve Naroff | 8160ea2 | 2007-03-06 01:09:46 +0000 | [diff] [blame] | 2901 | const char *ThisTokBegin = &IntegerBuffer[0]; |
| Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2902 | |
| Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2903 | // Get the spelling of the token, which eliminates trigraphs, etc. |
| Douglas Gregor | dc970f0 | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2904 | bool Invalid = false; |
| 2905 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid); |
| 2906 | if (Invalid) |
| 2907 | return ExprError(); |
| Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2908 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2909 | NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
| Steve Naroff | 451d8f16 | 2007-03-12 23:22:38 +0000 | [diff] [blame] | 2910 | Tok.getLocation(), PP); |
| Steve Naroff | f2fb89e | 2007-03-13 20:29:44 +0000 | [diff] [blame] | 2911 | if (Literal.hadError) |
| Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2912 | return ExprError(); |
| 2913 | |
| Chris Lattner | 1c20a17 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2914 | Expr *Res; |
| Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2915 | |
| Chris Lattner | 1c20a17 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2916 | if (Literal.isFloatingLiteral()) { |
| Chris Lattner | ec0a6d9 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2917 | QualType Ty; |
| Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2918 | if (Literal.isFloat) |
| Chris Lattner | ec0a6d9 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2919 | Ty = Context.FloatTy; |
| Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2920 | else if (!Literal.isLong) |
| Chris Lattner | ec0a6d9 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2921 | Ty = Context.DoubleTy; |
| Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2922 | else |
| Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 2923 | Ty = Context.LongDoubleTy; |
| Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2924 | |
| 2925 | const llvm::fltSemantics &Format = Context.getFloatTypeSemantics(Ty); |
| 2926 | |
| John McCall | 53b93a0 | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2927 | using llvm::APFloat; |
| 2928 | APFloat Val(Format); |
| 2929 | |
| 2930 | APFloat::opStatus result = Literal.GetFloatValue(Val); |
| John McCall | 122c831 | 2009-12-24 11:09:08 +0000 | [diff] [blame] | 2931 | |
| 2932 | // Overflow is always an error, but underflow is only an error if |
| 2933 | // we underflowed to zero (APFloat reports denormals as underflow). |
| 2934 | if ((result & APFloat::opOverflow) || |
| 2935 | ((result & APFloat::opUnderflow) && Val.isZero())) { |
| John McCall | 53b93a0 | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2936 | unsigned diagnostic; |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2937 | llvm::SmallString<20> buffer; |
| John McCall | 53b93a0 | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2938 | if (result & APFloat::opOverflow) { |
| John McCall | 62abc94 | 2010-02-26 23:35:57 +0000 | [diff] [blame] | 2939 | diagnostic = diag::warn_float_overflow; |
| John McCall | 53b93a0 | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2940 | APFloat::getLargest(Format).toString(buffer); |
| 2941 | } else { |
| John McCall | 62abc94 | 2010-02-26 23:35:57 +0000 | [diff] [blame] | 2942 | diagnostic = diag::warn_float_underflow; |
| John McCall | 53b93a0 | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2943 | APFloat::getSmallest(Format).toString(buffer); |
| 2944 | } |
| 2945 | |
| 2946 | Diag(Tok.getLocation(), diagnostic) |
| 2947 | << Ty |
| 2948 | << llvm::StringRef(buffer.data(), buffer.size()); |
| 2949 | } |
| 2950 | |
| 2951 | bool isExact = (result == APFloat::opOK); |
| Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2952 | Res = FloatingLiteral::Create(Context, Val, isExact, Ty, Tok.getLocation()); |
| Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2953 | |
| Peter Collingbourne | c77f85b | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2954 | if (Ty == Context.DoubleTy) { |
| 2955 | if (getLangOptions().SinglePrecisionConstants) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2956 | Res = ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast).take(); |
| Peter Collingbourne | c77f85b | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2957 | } else if (getLangOptions().OpenCL && !getOpenCLOptions().cl_khr_fp64) { |
| 2958 | Diag(Tok.getLocation(), diag::warn_double_const_requires_fp64); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2959 | Res = ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast).take(); |
| Peter Collingbourne | c77f85b | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2960 | } |
| 2961 | } |
| Chris Lattner | 1c20a17 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2962 | } else if (!Literal.isIntegerLiteral()) { |
| Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2963 | return ExprError(); |
| Chris Lattner | 1c20a17 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2964 | } else { |
| Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2965 | QualType Ty; |
| Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2966 | |
| Neil Booth | ac582c5 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 2967 | // long long is a C99 feature. |
| 2968 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && |
| Neil Booth | 4a1ee05 | 2007-08-29 22:13:52 +0000 | [diff] [blame] | 2969 | Literal.isLongLong) |
| Neil Booth | ac582c5 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 2970 | Diag(Tok.getLocation(), diag::ext_longlong); |
| 2971 | |
| Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2972 | // Get the value in the widest-possible width. |
| Chris Lattner | 37e0587 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2973 | llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(), 0); |
| Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2974 | |
| Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2975 | if (Literal.GetIntegerValue(ResultVal)) { |
| 2976 | // If this value didn't fit into uintmax_t, warn and force to ull. |
| 2977 | Diag(Tok.getLocation(), diag::warn_integer_too_large); |
| Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2978 | Ty = Context.UnsignedLongLongTy; |
| 2979 | assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() && |
| Chris Lattner | 37e0587 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2980 | "long long is not intmax_t?"); |
| Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 2981 | } else { |
| Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2982 | // If this value fits into a ULL, try to figure out what else it fits into |
| 2983 | // according to the rules of C99 6.4.4.1p5. |
| Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2984 | |
| Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2985 | // Octal, Hexadecimal, and integers with a U suffix are allowed to |
| 2986 | // be an unsigned int. |
| 2987 | bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10; |
| 2988 | |
| 2989 | // Check from smallest to largest, picking the smallest type we can. |
| Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2990 | unsigned Width = 0; |
| Chris Lattner | 7b939cf | 2007-08-23 21:58:08 +0000 | [diff] [blame] | 2991 | if (!Literal.isLong && !Literal.isLongLong) { |
| 2992 | // Are int/unsigned possibilities? |
| Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2993 | unsigned IntSize = Context.Target.getIntWidth(); |
| Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2994 | |
| Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2995 | // Does it fit in a unsigned int? |
| 2996 | if (ResultVal.isIntN(IntSize)) { |
| 2997 | // Does it fit in a signed int? |
| 2998 | if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0) |
| Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2999 | Ty = Context.IntTy; |
| Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 3000 | else if (AllowUnsigned) |
| Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3001 | Ty = Context.UnsignedIntTy; |
| Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 3002 | Width = IntSize; |
| Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 3003 | } |
| Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 3004 | } |
| Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 3005 | |
| Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 3006 | // Are long/unsigned long possibilities? |
| Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3007 | if (Ty.isNull() && !Literal.isLongLong) { |
| Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 3008 | unsigned LongSize = Context.Target.getLongWidth(); |
| Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 3009 | |
| Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 3010 | // Does it fit in a unsigned long? |
| 3011 | if (ResultVal.isIntN(LongSize)) { |
| 3012 | // Does it fit in a signed long? |
| 3013 | if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0) |
| Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3014 | Ty = Context.LongTy; |
| Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 3015 | else if (AllowUnsigned) |
| Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3016 | Ty = Context.UnsignedLongTy; |
| Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 3017 | Width = LongSize; |
| Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 3018 | } |
| Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 3019 | } |
| 3020 | |
| Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 3021 | // Finally, check long long if needed. |
| Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3022 | if (Ty.isNull()) { |
| Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 3023 | unsigned LongLongSize = Context.Target.getLongLongWidth(); |
| Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 3024 | |
| Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 3025 | // Does it fit in a unsigned long long? |
| 3026 | if (ResultVal.isIntN(LongLongSize)) { |
| 3027 | // Does it fit in a signed long long? |
| Francois Pichet | c3e73b3 | 2011-01-11 23:38:13 +0000 | [diff] [blame] | 3028 | // To be compatible with MSVC, hex integer literals ending with the |
| 3029 | // LL or i64 suffix are always signed in Microsoft mode. |
| Francois Pichet | bf711d9 | 2011-01-11 12:23:00 +0000 | [diff] [blame] | 3030 | if (!Literal.isUnsigned && (ResultVal[LongLongSize-1] == 0 || |
| 3031 | (getLangOptions().Microsoft && Literal.isLongLong))) |
| Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3032 | Ty = Context.LongLongTy; |
| Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 3033 | else if (AllowUnsigned) |
| Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3034 | Ty = Context.UnsignedLongLongTy; |
| Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 3035 | Width = LongLongSize; |
| Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 3036 | } |
| 3037 | } |
| Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 3038 | |
| Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 3039 | // If we still couldn't decide a type, we probably have something that |
| 3040 | // does not fit in a signed long long, but has no U suffix. |
| Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3041 | if (Ty.isNull()) { |
| Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 3042 | Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed); |
| Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3043 | Ty = Context.UnsignedLongLongTy; |
| Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 3044 | Width = Context.Target.getLongLongWidth(); |
| Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 3045 | } |
| Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 3046 | |
| Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 3047 | if (ResultVal.getBitWidth() != Width) |
| Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 3048 | ResultVal = ResultVal.trunc(Width); |
| Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 3049 | } |
| Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 3050 | Res = IntegerLiteral::Create(Context, ResultVal, Ty, Tok.getLocation()); |
| Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 3051 | } |
| Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 3052 | |
| Chris Lattner | 1c20a17 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 3053 | // If this is an imaginary literal, create the ImaginaryLiteral wrapper. |
| 3054 | if (Literal.isImaginary) |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3055 | Res = new (Context) ImaginaryLiteral(Res, |
| Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3056 | Context.getComplexType(Res->getType())); |
| Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 3057 | |
| 3058 | return Owned(Res); |
| Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 3059 | } |
| 3060 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3061 | ExprResult Sema::ActOnParenExpr(SourceLocation L, |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3062 | SourceLocation R, Expr *E) { |
| Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3063 | assert((E != 0) && "ActOnParenExpr() missing expr"); |
| Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3064 | return Owned(new (Context) ParenExpr(L, R, E)); |
| Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 3065 | } |
| 3066 | |
| Chandler Carruth | 62da79c | 2011-05-26 08:53:12 +0000 | [diff] [blame] | 3067 | static bool CheckVecStepTraitOperandType(Sema &S, QualType T, |
| 3068 | SourceLocation Loc, |
| 3069 | SourceRange ArgRange) { |
| 3070 | // [OpenCL 1.1 6.11.12] "The vec_step built-in function takes a built-in |
| 3071 | // scalar or vector data type argument..." |
| 3072 | // Every built-in scalar type (OpenCL 1.1 6.1.1) is either an arithmetic |
| 3073 | // type (C99 6.2.5p18) or void. |
| 3074 | if (!(T->isArithmeticType() || T->isVoidType() || T->isVectorType())) { |
| 3075 | S.Diag(Loc, diag::err_vecstep_non_scalar_vector_type) |
| 3076 | << T << ArgRange; |
| 3077 | return true; |
| 3078 | } |
| 3079 | |
| 3080 | assert((T->isVoidType() || !T->isIncompleteType()) && |
| 3081 | "Scalar types should always be complete"); |
| 3082 | return false; |
| 3083 | } |
| 3084 | |
| Chandler Carruth | cea1aac | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 3085 | static bool CheckExtensionTraitOperandType(Sema &S, QualType T, |
| 3086 | SourceLocation Loc, |
| 3087 | SourceRange ArgRange, |
| 3088 | UnaryExprOrTypeTrait TraitKind) { |
| 3089 | // C99 6.5.3.4p1: |
| 3090 | if (T->isFunctionType()) { |
| 3091 | // alignof(function) is allowed as an extension. |
| 3092 | if (TraitKind == UETT_SizeOf) |
| 3093 | S.Diag(Loc, diag::ext_sizeof_function_type) << ArgRange; |
| 3094 | return false; |
| 3095 | } |
| 3096 | |
| 3097 | // Allow sizeof(void)/alignof(void) as an extension. |
| 3098 | if (T->isVoidType()) { |
| 3099 | S.Diag(Loc, diag::ext_sizeof_void_type) << TraitKind << ArgRange; |
| 3100 | return false; |
| 3101 | } |
| 3102 | |
| 3103 | return true; |
| 3104 | } |
| 3105 | |
| 3106 | static bool CheckObjCTraitOperandConstraints(Sema &S, QualType T, |
| 3107 | SourceLocation Loc, |
| 3108 | SourceRange ArgRange, |
| 3109 | UnaryExprOrTypeTrait TraitKind) { |
| 3110 | // Reject sizeof(interface) and sizeof(interface<proto>) in 64-bit mode. |
| 3111 | if (S.LangOpts.ObjCNonFragileABI && T->isObjCObjectType()) { |
| 3112 | S.Diag(Loc, diag::err_sizeof_nonfragile_interface) |
| 3113 | << T << (TraitKind == UETT_SizeOf) |
| 3114 | << ArgRange; |
| 3115 | return true; |
| 3116 | } |
| 3117 | |
| 3118 | return false; |
| 3119 | } |
| 3120 | |
| Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3121 | /// \brief Check the constrains on expression operands to unary type expression |
| 3122 | /// and type traits. |
| 3123 | /// |
| Chandler Carruth | 7c430c0 | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 3124 | /// Completes any types necessary and validates the constraints on the operand |
| 3125 | /// expression. The logic mostly mirrors the type-based overload, but may modify |
| 3126 | /// the expression as it completes the type for that expression through template |
| 3127 | /// instantiation, etc. |
| Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3128 | bool Sema::CheckUnaryExprOrTypeTraitOperand(Expr *Op, |
| 3129 | UnaryExprOrTypeTrait ExprKind) { |
| Chandler Carruth | 7c430c0 | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 3130 | QualType ExprTy = Op->getType(); |
| 3131 | |
| 3132 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 3133 | // the result is the size of the referenced type." |
| 3134 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 3135 | // result shall be the alignment of the referenced type." |
| 3136 | if (const ReferenceType *Ref = ExprTy->getAs<ReferenceType>()) |
| 3137 | ExprTy = Ref->getPointeeType(); |
| 3138 | |
| 3139 | if (ExprKind == UETT_VecStep) |
| 3140 | return CheckVecStepTraitOperandType(*this, ExprTy, Op->getExprLoc(), |
| 3141 | Op->getSourceRange()); |
| 3142 | |
| 3143 | // Whitelist some types as extensions |
| 3144 | if (!CheckExtensionTraitOperandType(*this, ExprTy, Op->getExprLoc(), |
| 3145 | Op->getSourceRange(), ExprKind)) |
| 3146 | return false; |
| 3147 | |
| 3148 | if (RequireCompleteExprType(Op, |
| 3149 | PDiag(diag::err_sizeof_alignof_incomplete_type) |
| 3150 | << ExprKind << Op->getSourceRange(), |
| 3151 | std::make_pair(SourceLocation(), PDiag(0)))) |
| 3152 | return true; |
| 3153 | |
| 3154 | // Completeing the expression's type may have changed it. |
| 3155 | ExprTy = Op->getType(); |
| 3156 | if (const ReferenceType *Ref = ExprTy->getAs<ReferenceType>()) |
| 3157 | ExprTy = Ref->getPointeeType(); |
| 3158 | |
| 3159 | if (CheckObjCTraitOperandConstraints(*this, ExprTy, Op->getExprLoc(), |
| 3160 | Op->getSourceRange(), ExprKind)) |
| 3161 | return true; |
| 3162 | |
| Nico Weber | 0870deb | 2011-06-15 02:47:03 +0000 | [diff] [blame^] | 3163 | if (ExprKind == UETT_SizeOf) { |
| 3164 | if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(Op->IgnoreParens())) { |
| 3165 | if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(DeclRef->getFoundDecl())) { |
| 3166 | QualType OType = PVD->getOriginalType(); |
| 3167 | QualType Type = PVD->getType(); |
| 3168 | if (Type->isPointerType() && OType->isArrayType()) { |
| 3169 | Diag(Op->getExprLoc(), diag::warn_sizeof_array_param) |
| 3170 | << Type << OType; |
| 3171 | Diag(PVD->getLocation(), diag::note_declared_at); |
| 3172 | } |
| 3173 | } |
| 3174 | } |
| 3175 | } |
| 3176 | |
| Chandler Carruth | 7c430c0 | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 3177 | return false; |
| Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3178 | } |
| 3179 | |
| 3180 | /// \brief Check the constraints on operands to unary expression and type |
| 3181 | /// traits. |
| 3182 | /// |
| 3183 | /// This will complete any types necessary, and validate the various constraints |
| 3184 | /// on those operands. |
| 3185 | /// |
| Steve Naroff | 71b59a9 | 2007-06-04 22:22:31 +0000 | [diff] [blame] | 3186 | /// The UsualUnaryConversions() function is *not* called by this routine. |
| Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3187 | /// C99 6.3.2.1p[2-4] all state: |
| 3188 | /// Except when it is the operand of the sizeof operator ... |
| 3189 | /// |
| 3190 | /// C++ [expr.sizeof]p4 |
| 3191 | /// The lvalue-to-rvalue, array-to-pointer, and function-to-pointer |
| 3192 | /// standard conversions are not applied to the operand of sizeof. |
| 3193 | /// |
| 3194 | /// This policy is followed for all of the unary trait expressions. |
| Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3195 | bool Sema::CheckUnaryExprOrTypeTraitOperand(QualType exprType, |
| 3196 | SourceLocation OpLoc, |
| 3197 | SourceRange ExprRange, |
| 3198 | UnaryExprOrTypeTrait ExprKind) { |
| Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3199 | if (exprType->isDependentType()) |
| 3200 | return false; |
| 3201 | |
| Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 3202 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 3203 | // the result is the size of the referenced type." |
| 3204 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 3205 | // result shall be the alignment of the referenced type." |
| 3206 | if (const ReferenceType *Ref = exprType->getAs<ReferenceType>()) |
| 3207 | exprType = Ref->getPointeeType(); |
| 3208 | |
| Chandler Carruth | 62da79c | 2011-05-26 08:53:12 +0000 | [diff] [blame] | 3209 | if (ExprKind == UETT_VecStep) |
| 3210 | return CheckVecStepTraitOperandType(*this, exprType, OpLoc, ExprRange); |
| Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3211 | |
| Chandler Carruth | cea1aac | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 3212 | // Whitelist some types as extensions |
| 3213 | if (!CheckExtensionTraitOperandType(*this, exprType, OpLoc, ExprRange, |
| 3214 | ExprKind)) |
| Chris Lattner | b1355b1 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 3215 | return false; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3216 | |
| Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3217 | if (RequireCompleteType(OpLoc, exprType, |
| Douglas Gregor | 906db8a | 2009-12-15 16:44:32 +0000 | [diff] [blame] | 3218 | PDiag(diag::err_sizeof_alignof_incomplete_type) |
| Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3219 | << ExprKind << ExprRange)) |
| Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3220 | return true; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3221 | |
| Chandler Carruth | cea1aac | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 3222 | if (CheckObjCTraitOperandConstraints(*this, exprType, OpLoc, ExprRange, |
| 3223 | ExprKind)) |
| Chris Lattner | cd2a8c5 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 3224 | return true; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3225 | |
| Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3226 | return false; |
| Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 3227 | } |
| 3228 | |
| Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3229 | static bool CheckAlignOfExpr(Sema &S, Expr *E) { |
| Chris Lattner | 8dff017 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 3230 | E = E->IgnoreParens(); |
| Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3231 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3232 | // alignof decl is always ok. |
| Chris Lattner | 8dff017 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 3233 | if (isa<DeclRefExpr>(E)) |
| 3234 | return false; |
| Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3235 | |
| 3236 | // Cannot know anything else if the expression is dependent. |
| 3237 | if (E->isTypeDependent()) |
| 3238 | return false; |
| 3239 | |
| Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3240 | if (E->getBitField()) { |
| Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3241 | S.Diag(E->getExprLoc(), diag::err_sizeof_alignof_bitfield) |
| 3242 | << 1 << E->getSourceRange(); |
| Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3243 | return true; |
| Chris Lattner | 8dff017 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 3244 | } |
| Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3245 | |
| 3246 | // Alignment of a field access is always okay, so long as it isn't a |
| 3247 | // bit-field. |
| 3248 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
| Mike Stump | 212005c | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 3249 | if (isa<FieldDecl>(ME->getMemberDecl())) |
| Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3250 | return false; |
| 3251 | |
| Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3252 | return S.CheckUnaryExprOrTypeTraitOperand(E, UETT_AlignOf); |
| Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3253 | } |
| 3254 | |
| Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3255 | bool Sema::CheckVecStepExpr(Expr *E) { |
| Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3256 | E = E->IgnoreParens(); |
| 3257 | |
| 3258 | // Cannot know anything else if the expression is dependent. |
| 3259 | if (E->isTypeDependent()) |
| 3260 | return false; |
| 3261 | |
| Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3262 | return CheckUnaryExprOrTypeTraitOperand(E, UETT_VecStep); |
| Chris Lattner | 8dff017 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 3263 | } |
| 3264 | |
| Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3265 | /// \brief Build a sizeof or alignof expression given a type operand. |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3266 | ExprResult |
| Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3267 | Sema::CreateUnaryExprOrTypeTraitExpr(TypeSourceInfo *TInfo, |
| 3268 | SourceLocation OpLoc, |
| 3269 | UnaryExprOrTypeTrait ExprKind, |
| 3270 | SourceRange R) { |
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3271 | if (!TInfo) |
| Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3272 | return ExprError(); |
| 3273 | |
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3274 | QualType T = TInfo->getType(); |
| John McCall | 4c98fd8 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 3275 | |
| Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3276 | if (!T->isDependentType() && |
| Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3277 | CheckUnaryExprOrTypeTraitOperand(T, OpLoc, R, ExprKind)) |
| Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3278 | return ExprError(); |
| 3279 | |
| 3280 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
| Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3281 | return Owned(new (Context) UnaryExprOrTypeTraitExpr(ExprKind, TInfo, |
| 3282 | Context.getSizeType(), |
| 3283 | OpLoc, R.getEnd())); |
| Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3284 | } |
| 3285 | |
| 3286 | /// \brief Build a sizeof or alignof expression given an expression |
| 3287 | /// operand. |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3288 | ExprResult |
| Chandler Carruth | a923fb2 | 2011-05-29 07:32:14 +0000 | [diff] [blame] | 3289 | Sema::CreateUnaryExprOrTypeTraitExpr(Expr *E, SourceLocation OpLoc, |
| 3290 | UnaryExprOrTypeTrait ExprKind) { |
| Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3291 | // Verify that the operand is valid. |
| 3292 | bool isInvalid = false; |
| 3293 | if (E->isTypeDependent()) { |
| 3294 | // Delay type-checking for type-dependent expressions. |
| Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3295 | } else if (ExprKind == UETT_AlignOf) { |
| Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3296 | isInvalid = CheckAlignOfExpr(*this, E); |
| Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3297 | } else if (ExprKind == UETT_VecStep) { |
| Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3298 | isInvalid = CheckVecStepExpr(E); |
| Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3299 | } else if (E->getBitField()) { // C99 6.5.3.4p1. |
| Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3300 | Diag(E->getExprLoc(), diag::err_sizeof_alignof_bitfield) << 0; |
| Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3301 | isInvalid = true; |
| John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 3302 | } else if (E->getType()->isPlaceholderType()) { |
| John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 3303 | ExprResult PE = CheckPlaceholderExpr(E); |
| John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 3304 | if (PE.isInvalid()) return ExprError(); |
| Chandler Carruth | a923fb2 | 2011-05-29 07:32:14 +0000 | [diff] [blame] | 3305 | return CreateUnaryExprOrTypeTraitExpr(PE.take(), OpLoc, ExprKind); |
| Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3306 | } else { |
| Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3307 | isInvalid = CheckUnaryExprOrTypeTraitOperand(E, UETT_SizeOf); |
| Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3308 | } |
| 3309 | |
| 3310 | if (isInvalid) |
| 3311 | return ExprError(); |
| 3312 | |
| 3313 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
| Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3314 | return Owned(new (Context) UnaryExprOrTypeTraitExpr( |
| Chandler Carruth | a923fb2 | 2011-05-29 07:32:14 +0000 | [diff] [blame] | 3315 | ExprKind, E, Context.getSizeType(), OpLoc, |
| Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3316 | E->getSourceRange().getEnd())); |
| Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3317 | } |
| 3318 | |
| Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3319 | /// ActOnUnaryExprOrTypeTraitExpr - Handle @c sizeof(type) and @c sizeof @c |
| 3320 | /// expr and the same for @c alignof and @c __alignof |
| Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 3321 | /// Note that the ArgRange is invalid if isType is false. |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3322 | ExprResult |
| Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3323 | Sema::ActOnUnaryExprOrTypeTraitExpr(SourceLocation OpLoc, |
| 3324 | UnaryExprOrTypeTrait ExprKind, bool isType, |
| 3325 | void *TyOrEx, const SourceRange &ArgRange) { |
| Chris Lattner | 0d8b1a1 | 2006-11-20 04:34:45 +0000 | [diff] [blame] | 3326 | // If error parsing type, ignore. |
| Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3327 | if (TyOrEx == 0) return ExprError(); |
| Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 3328 | |
| Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 3329 | if (isType) { |
| John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3330 | TypeSourceInfo *TInfo; |
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3331 | (void) GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrEx), &TInfo); |
| Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3332 | return CreateUnaryExprOrTypeTraitExpr(TInfo, OpLoc, ExprKind, ArgRange); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3333 | } |
| Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 3334 | |
| Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3335 | Expr *ArgEx = (Expr *)TyOrEx; |
| Chandler Carruth | a923fb2 | 2011-05-29 07:32:14 +0000 | [diff] [blame] | 3336 | ExprResult Result = CreateUnaryExprOrTypeTraitExpr(ArgEx, OpLoc, ExprKind); |
| Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3337 | return move(Result); |
| Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 3338 | } |
| 3339 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3340 | static QualType CheckRealImagOperand(Sema &S, ExprResult &V, SourceLocation Loc, |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3341 | bool isReal) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3342 | if (V.get()->isTypeDependent()) |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3343 | return S.Context.DependentTy; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3344 | |
| John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3345 | // _Real and _Imag are only l-values for normal l-values. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3346 | if (V.get()->getObjectKind() != OK_Ordinary) { |
| 3347 | V = S.DefaultLvalueConversion(V.take()); |
| 3348 | if (V.isInvalid()) |
| 3349 | return QualType(); |
| 3350 | } |
| John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3351 | |
| Chris Lattner | e267f5d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 3352 | // These operators return the element type of a complex type. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3353 | if (const ComplexType *CT = V.get()->getType()->getAs<ComplexType>()) |
| Chris Lattner | 30b5dd0 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 3354 | return CT->getElementType(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3355 | |
| Chris Lattner | e267f5d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 3356 | // Otherwise they pass through real integer and floating point types here. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3357 | if (V.get()->getType()->isArithmeticType()) |
| 3358 | return V.get()->getType(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3359 | |
| John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 3360 | // Test for placeholders. |
| John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 3361 | ExprResult PR = S.CheckPlaceholderExpr(V.get()); |
| John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 3362 | if (PR.isInvalid()) return QualType(); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3363 | if (PR.get() != V.get()) { |
| 3364 | V = move(PR); |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3365 | return CheckRealImagOperand(S, V, Loc, isReal); |
| John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 3366 | } |
| 3367 | |
| Chris Lattner | e267f5d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 3368 | // Reject anything else. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3369 | S.Diag(Loc, diag::err_realimag_invalid_type) << V.get()->getType() |
| Chris Lattner | 709322b | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 3370 | << (isReal ? "__real" : "__imag"); |
| Chris Lattner | e267f5d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 3371 | return QualType(); |
| Chris Lattner | 30b5dd0 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 3372 | } |
| 3373 | |
| 3374 | |
| Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 3375 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3376 | ExprResult |
| Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3377 | Sema::ActOnPostfixUnaryOp(Scope *S, SourceLocation OpLoc, |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3378 | tok::TokenKind Kind, Expr *Input) { |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3379 | UnaryOperatorKind Opc; |
| Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 3380 | switch (Kind) { |
| 3381 | default: assert(0 && "Unknown unary op!"); |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3382 | case tok::plusplus: Opc = UO_PostInc; break; |
| 3383 | case tok::minusminus: Opc = UO_PostDec; break; |
| Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 3384 | } |
| Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3385 | |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3386 | return BuildUnaryOp(S, OpLoc, Opc, Input); |
| Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 3387 | } |
| 3388 | |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3389 | /// Expressions of certain arbitrary types are forbidden by C from |
| 3390 | /// having l-value type. These are: |
| 3391 | /// - 'void', but not qualified void |
| 3392 | /// - function types |
| 3393 | /// |
| 3394 | /// The exact rule here is C99 6.3.2.1: |
| 3395 | /// An lvalue is an expression with an object type or an incomplete |
| 3396 | /// type other than void. |
| 3397 | static bool IsCForbiddenLValueType(ASTContext &C, QualType T) { |
| 3398 | return ((T->isVoidType() && !T.hasQualifiers()) || |
| 3399 | T->isFunctionType()); |
| 3400 | } |
| 3401 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3402 | ExprResult |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3403 | Sema::ActOnArraySubscriptExpr(Scope *S, Expr *Base, SourceLocation LLoc, |
| 3404 | Expr *Idx, SourceLocation RLoc) { |
| Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3405 | // Since this might be a postfix expression, get rid of ParenListExprs. |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3406 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base); |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3407 | if (Result.isInvalid()) return ExprError(); |
| 3408 | Base = Result.take(); |
| Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3409 | |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3410 | Expr *LHSExp = Base, *RHSExp = Idx; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3411 | |
| Douglas Gregor | 40412ac | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 3412 | if (getLangOptions().CPlusPlus && |
| Douglas Gregor | 7a77a6b | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 3413 | (LHSExp->isTypeDependent() || RHSExp->isTypeDependent())) { |
| Douglas Gregor | 7a77a6b | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 3414 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3415 | Context.DependentTy, |
| 3416 | VK_LValue, OK_Ordinary, |
| 3417 | RLoc)); |
| Douglas Gregor | 7a77a6b | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 3418 | } |
| 3419 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3420 | if (getLangOptions().CPlusPlus && |
| Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3421 | (LHSExp->getType()->isRecordType() || |
| Eli Friedman | 254a1a2 | 2008-12-15 22:34:21 +0000 | [diff] [blame] | 3422 | LHSExp->getType()->isEnumeralType() || |
| 3423 | RHSExp->getType()->isRecordType() || |
| 3424 | RHSExp->getType()->isEnumeralType())) { |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3425 | return CreateOverloadedArraySubscriptExpr(LLoc, RLoc, Base, Idx); |
| Douglas Gregor | 40412ac | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 3426 | } |
| 3427 | |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3428 | return CreateBuiltinArraySubscriptExpr(Base, LLoc, Idx, RLoc); |
| Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 3429 | } |
| 3430 | |
| 3431 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3432 | ExprResult |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3433 | Sema::CreateBuiltinArraySubscriptExpr(Expr *Base, SourceLocation LLoc, |
| 3434 | Expr *Idx, SourceLocation RLoc) { |
| 3435 | Expr *LHSExp = Base; |
| 3436 | Expr *RHSExp = Idx; |
| Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 3437 | |
| Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3438 | // Perform default conversions. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3439 | if (!LHSExp->getType()->getAs<VectorType>()) { |
| 3440 | ExprResult Result = DefaultFunctionArrayLvalueConversion(LHSExp); |
| 3441 | if (Result.isInvalid()) |
| 3442 | return ExprError(); |
| 3443 | LHSExp = Result.take(); |
| 3444 | } |
| 3445 | ExprResult Result = DefaultFunctionArrayLvalueConversion(RHSExp); |
| 3446 | if (Result.isInvalid()) |
| 3447 | return ExprError(); |
| 3448 | RHSExp = Result.take(); |
| Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3449 | |
| Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3450 | QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType(); |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3451 | ExprValueKind VK = VK_LValue; |
| 3452 | ExprObjectKind OK = OK_Ordinary; |
| Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 3453 | |
| Steve Naroff | c1aadb1 | 2007-03-28 21:49:40 +0000 | [diff] [blame] | 3454 | // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent |
| Chris Lattner | f17bd42 | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 3455 | // to the expression *((e1)+(e2)). This means the array "Base" may actually be |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3456 | // in the subscript position. As a result, we need to derive the array base |
| Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 3457 | // and index from the expression types. |
| Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3458 | Expr *BaseExpr, *IndexExpr; |
| 3459 | QualType ResultType; |
| Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3460 | if (LHSTy->isDependentType() || RHSTy->isDependentType()) { |
| 3461 | BaseExpr = LHSExp; |
| 3462 | IndexExpr = RHSExp; |
| 3463 | ResultType = Context.DependentTy; |
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3464 | } else if (const PointerType *PTy = LHSTy->getAs<PointerType>()) { |
| Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3465 | BaseExpr = LHSExp; |
| 3466 | IndexExpr = RHSExp; |
| Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3467 | ResultType = PTy->getPointeeType(); |
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3468 | } else if (const PointerType *PTy = RHSTy->getAs<PointerType>()) { |
| Chris Lattner | aee0cfd | 2007-07-16 00:23:25 +0000 | [diff] [blame] | 3469 | // Handle the uncommon case of "123[Ptr]". |
| Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3470 | BaseExpr = RHSExp; |
| 3471 | IndexExpr = LHSExp; |
| Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3472 | ResultType = PTy->getPointeeType(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3473 | } else if (const ObjCObjectPointerType *PTy = |
| John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3474 | LHSTy->getAs<ObjCObjectPointerType>()) { |
| Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3475 | BaseExpr = LHSExp; |
| 3476 | IndexExpr = RHSExp; |
| 3477 | ResultType = PTy->getPointeeType(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3478 | } else if (const ObjCObjectPointerType *PTy = |
| John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3479 | RHSTy->getAs<ObjCObjectPointerType>()) { |
| Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3480 | // Handle the uncommon case of "123[Ptr]". |
| 3481 | BaseExpr = RHSExp; |
| 3482 | IndexExpr = LHSExp; |
| 3483 | ResultType = PTy->getPointeeType(); |
| John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3484 | } else if (const VectorType *VTy = LHSTy->getAs<VectorType>()) { |
| Chris Lattner | 4197796 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 3485 | BaseExpr = LHSExp; // vectors: V[123] |
| Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3486 | IndexExpr = RHSExp; |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3487 | VK = LHSExp->getValueKind(); |
| 3488 | if (VK != VK_RValue) |
| 3489 | OK = OK_VectorComponent; |
| Nate Begeman | c1bf061 | 2009-01-18 00:45:31 +0000 | [diff] [blame] | 3490 | |
| Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3491 | // FIXME: need to deal with const... |
| 3492 | ResultType = VTy->getElementType(); |
| Eli Friedman | ab2784f | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3493 | } else if (LHSTy->isArrayType()) { |
| 3494 | // If we see an array that wasn't promoted by |
| Douglas Gregor | b92a156 | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 3495 | // DefaultFunctionArrayLvalueConversion, it must be an array that |
| Eli Friedman | ab2784f | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3496 | // wasn't promoted because of the C90 rule that doesn't |
| 3497 | // allow promoting non-lvalue arrays. Warn, then |
| 3498 | // force the promotion here. |
| 3499 | Diag(LHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 3500 | LHSExp->getSourceRange(); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3501 | LHSExp = ImpCastExprToType(LHSExp, Context.getArrayDecayedType(LHSTy), |
| 3502 | CK_ArrayToPointerDecay).take(); |
| Eli Friedman | ab2784f | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3503 | LHSTy = LHSExp->getType(); |
| 3504 | |
| 3505 | BaseExpr = LHSExp; |
| 3506 | IndexExpr = RHSExp; |
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3507 | ResultType = LHSTy->getAs<PointerType>()->getPointeeType(); |
| Eli Friedman | ab2784f | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3508 | } else if (RHSTy->isArrayType()) { |
| 3509 | // Same as previous, except for 123[f().a] case |
| 3510 | Diag(RHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 3511 | RHSExp->getSourceRange(); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3512 | RHSExp = ImpCastExprToType(RHSExp, Context.getArrayDecayedType(RHSTy), |
| 3513 | CK_ArrayToPointerDecay).take(); |
| Eli Friedman | ab2784f | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3514 | RHSTy = RHSExp->getType(); |
| 3515 | |
| 3516 | BaseExpr = RHSExp; |
| 3517 | IndexExpr = LHSExp; |
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3518 | ResultType = RHSTy->getAs<PointerType>()->getPointeeType(); |
| Steve Naroff | b309644 | 2007-06-09 03:47:53 +0000 | [diff] [blame] | 3519 | } else { |
| Chris Lattner | 003af24 | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 3520 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_value) |
| 3521 | << LHSExp->getSourceRange() << RHSExp->getSourceRange()); |
| Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3522 | } |
| Steve Naroff | c1aadb1 | 2007-03-28 21:49:40 +0000 | [diff] [blame] | 3523 | // C99 6.5.2.1p1 |
| Douglas Gregor | 5cc2c8b | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 3524 | if (!IndexExpr->getType()->isIntegerType() && !IndexExpr->isTypeDependent()) |
| Chris Lattner | 003af24 | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 3525 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_not_integer) |
| 3526 | << IndexExpr->getSourceRange()); |
| Steve Naroff | b29cdd5 | 2007-07-10 18:23:31 +0000 | [diff] [blame] | 3527 | |
| Daniel Dunbar | 4782a6e | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 3528 | if ((IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_S) || |
| Sam Weinig | b7608d7 | 2009-09-14 20:14:57 +0000 | [diff] [blame] | 3529 | IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U)) |
| 3530 | && !IndexExpr->isTypeDependent()) |
| Sam Weinig | 914244e | 2009-09-14 01:58:58 +0000 | [diff] [blame] | 3531 | Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange(); |
| 3532 | |
| Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3533 | // C99 6.5.2.1p1: "shall have type "pointer to *object* type". Similarly, |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3534 | // C++ [expr.sub]p1: The type "T" shall be a completely-defined object |
| 3535 | // type. Note that Functions are not objects, and that (in C99 parlance) |
| Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3536 | // incomplete types are not object types. |
| 3537 | if (ResultType->isFunctionType()) { |
| 3538 | Diag(BaseExpr->getLocStart(), diag::err_subscript_function_type) |
| 3539 | << ResultType << BaseExpr->getSourceRange(); |
| 3540 | return ExprError(); |
| 3541 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3542 | |
| Abramo Bagnara | 3aabb4b | 2010-09-13 06:50:07 +0000 | [diff] [blame] | 3543 | if (ResultType->isVoidType() && !getLangOptions().CPlusPlus) { |
| 3544 | // GNU extension: subscripting on pointer to void |
| 3545 | Diag(LLoc, diag::ext_gnu_void_ptr) |
| 3546 | << BaseExpr->getSourceRange(); |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3547 | |
| 3548 | // C forbids expressions of unqualified void type from being l-values. |
| 3549 | // See IsCForbiddenLValueType. |
| 3550 | if (!ResultType.hasQualifiers()) VK = VK_RValue; |
| Abramo Bagnara | 3aabb4b | 2010-09-13 06:50:07 +0000 | [diff] [blame] | 3551 | } else if (!ResultType->isDependentType() && |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3552 | RequireCompleteType(LLoc, ResultType, |
| Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3553 | PDiag(diag::err_subscript_incomplete_type) |
| 3554 | << BaseExpr->getSourceRange())) |
| Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3555 | return ExprError(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3556 | |
| Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3557 | // Diagnose bad cases where we step over interface counts. |
| John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3558 | if (ResultType->isObjCObjectType() && LangOpts.ObjCNonFragileABI) { |
| Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3559 | Diag(LLoc, diag::err_subscript_nonfragile_interface) |
| 3560 | << ResultType << BaseExpr->getSourceRange(); |
| 3561 | return ExprError(); |
| 3562 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3563 | |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3564 | assert(VK == VK_RValue || LangOpts.CPlusPlus || |
| 3565 | !IsCForbiddenLValueType(Context, ResultType)); |
| 3566 | |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3567 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3568 | ResultType, VK, OK, RLoc)); |
| Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 3569 | } |
| 3570 | |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3571 | /// Check an ext-vector component access expression. |
| 3572 | /// |
| 3573 | /// VK should be set in advance to the value kind of the base |
| 3574 | /// expression. |
| 3575 | static QualType |
| 3576 | CheckExtVectorComponent(Sema &S, QualType baseType, ExprValueKind &VK, |
| 3577 | SourceLocation OpLoc, const IdentifierInfo *CompName, |
| Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3578 | SourceLocation CompLoc) { |
| Daniel Dunbar | c042940 | 2009-10-18 02:09:38 +0000 | [diff] [blame] | 3579 | // FIXME: Share logic with ExtVectorElementExpr::containsDuplicateElements, |
| 3580 | // see FIXME there. |
| 3581 | // |
| 3582 | // FIXME: This logic can be greatly simplified by splitting it along |
| 3583 | // halving/not halving and reworking the component checking. |
| John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3584 | const ExtVectorType *vecType = baseType->getAs<ExtVectorType>(); |
| Nate Begeman | f322eab | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 3585 | |
| Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3586 | // The vector accessor can't exceed the number of elements. |
| Daniel Dunbar | 2c422dc9 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 3587 | const char *compStr = CompName->getNameStart(); |
| Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3588 | |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3589 | // This flag determines whether or not the component is one of the four |
| Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3590 | // special names that indicate a subset of exactly half the elements are |
| 3591 | // to be selected. |
| 3592 | bool HalvingSwizzle = false; |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3593 | |
| Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3594 | // This flag determines whether or not CompName has an 's' char prefix, |
| 3595 | // indicating that it is a string of hex values to be used as vector indices. |
| Nate Begeman | 0359e12 | 2009-06-25 21:06:09 +0000 | [diff] [blame] | 3596 | bool HexSwizzle = *compStr == 's' || *compStr == 'S'; |
| Nate Begeman | f322eab | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 3597 | |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3598 | bool HasRepeated = false; |
| 3599 | bool HasIndex[16] = {}; |
| 3600 | |
| 3601 | int Idx; |
| 3602 | |
| Nate Begeman | f322eab | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 3603 | // Check that we've found one of the special components, or that the component |
| 3604 | // names must come from the same set. |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3605 | if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") || |
| Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3606 | !strcmp(compStr, "even") || !strcmp(compStr, "odd")) { |
| 3607 | HalvingSwizzle = true; |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3608 | } else if (!HexSwizzle && |
| 3609 | (Idx = vecType->getPointAccessorIdx(*compStr)) != -1) { |
| 3610 | do { |
| 3611 | if (HasIndex[Idx]) HasRepeated = true; |
| 3612 | HasIndex[Idx] = true; |
| Chris Lattner | 7e152db | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 3613 | compStr++; |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3614 | } while (*compStr && (Idx = vecType->getPointAccessorIdx(*compStr)) != -1); |
| 3615 | } else { |
| 3616 | if (HexSwizzle) compStr++; |
| 3617 | while ((Idx = vecType->getNumericAccessorIdx(*compStr)) != -1) { |
| 3618 | if (HasIndex[Idx]) HasRepeated = true; |
| 3619 | HasIndex[Idx] = true; |
| Chris Lattner | 7e152db | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 3620 | compStr++; |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3621 | } |
| Chris Lattner | 7e152db | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 3622 | } |
| Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3623 | |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3624 | if (!HalvingSwizzle && *compStr) { |
| Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3625 | // We didn't get to the end of the string. This means the component names |
| 3626 | // didn't come from the same set *or* we encountered an illegal name. |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3627 | S.Diag(OpLoc, diag::err_ext_vector_component_name_illegal) |
| Benjamin Kramer | e8394df | 2010-08-11 14:47:12 +0000 | [diff] [blame] | 3628 | << llvm::StringRef(compStr, 1) << SourceRange(CompLoc); |
| Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3629 | return QualType(); |
| 3630 | } |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3631 | |
| Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3632 | // Ensure no component accessor exceeds the width of the vector type it |
| 3633 | // operates on. |
| 3634 | if (!HalvingSwizzle) { |
| Daniel Dunbar | 2c422dc9 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 3635 | compStr = CompName->getNameStart(); |
| Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3636 | |
| 3637 | if (HexSwizzle) |
| Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3638 | compStr++; |
| Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3639 | |
| 3640 | while (*compStr) { |
| 3641 | if (!vecType->isAccessorWithinNumElements(*compStr++)) { |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3642 | S.Diag(OpLoc, diag::err_ext_vector_component_exceeds_length) |
| Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3643 | << baseType << SourceRange(CompLoc); |
| 3644 | return QualType(); |
| 3645 | } |
| 3646 | } |
| Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3647 | } |
| Nate Begeman | f322eab | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 3648 | |
| Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3649 | // The component accessor looks fine - now we need to compute the actual type. |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3650 | // The vector type is implied by the component accessor. For example, |
| Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3651 | // vec4.b is a float, vec4.xy is a vec2, vec4.rgb is a vec3, etc. |
| Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3652 | // vec4.s0 is a float, vec4.s23 is a vec3, etc. |
| Nate Begeman | f322eab | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 3653 | // vec4.hi, vec4.lo, vec4.e, and vec4.o all return vec2. |
| Nate Begeman | ac8183a | 2009-12-15 18:13:04 +0000 | [diff] [blame] | 3654 | unsigned CompSize = HalvingSwizzle ? (vecType->getNumElements() + 1) / 2 |
| Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3655 | : CompName->getLength(); |
| Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3656 | if (HexSwizzle) |
| 3657 | CompSize--; |
| 3658 | |
| Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3659 | if (CompSize == 1) |
| 3660 | return vecType->getElementType(); |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3661 | |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3662 | if (HasRepeated) VK = VK_RValue; |
| 3663 | |
| 3664 | QualType VT = S.Context.getExtVectorType(vecType->getElementType(), CompSize); |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3665 | // Now look up the TypeDefDecl from the vector type. Without this, |
| Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 3666 | // diagostics look bad. We want extended vector types to appear built-in. |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3667 | for (unsigned i = 0, E = S.ExtVectorDecls.size(); i != E; ++i) { |
| 3668 | if (S.ExtVectorDecls[i]->getUnderlyingType() == VT) |
| 3669 | return S.Context.getTypedefType(S.ExtVectorDecls[i]); |
| Steve Naroff | ddf5a1d | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 3670 | } |
| 3671 | return VT; // should never get here (a typedef type should always be found). |
| Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3672 | } |
| 3673 | |
| Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3674 | static Decl *FindGetterSetterNameDeclFromProtocolList(const ObjCProtocolDecl*PDecl, |
| Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3675 | IdentifierInfo *Member, |
| Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3676 | const Selector &Sel, |
| 3677 | ASTContext &Context) { |
| Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3678 | if (Member) |
| 3679 | if (ObjCPropertyDecl *PD = PDecl->FindPropertyDeclaration(Member)) |
| 3680 | return PD; |
| Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3681 | if (ObjCMethodDecl *OMD = PDecl->getInstanceMethod(Sel)) |
| Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3682 | return OMD; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3683 | |
| Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3684 | for (ObjCProtocolDecl::protocol_iterator I = PDecl->protocol_begin(), |
| 3685 | E = PDecl->protocol_end(); I != E; ++I) { |
| Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3686 | if (Decl *D = FindGetterSetterNameDeclFromProtocolList(*I, Member, Sel, |
| 3687 | Context)) |
| Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3688 | return D; |
| 3689 | } |
| 3690 | return 0; |
| 3691 | } |
| 3692 | |
| Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3693 | static Decl *FindGetterSetterNameDecl(const ObjCObjectPointerType *QIdTy, |
| 3694 | IdentifierInfo *Member, |
| 3695 | const Selector &Sel, |
| 3696 | ASTContext &Context) { |
| Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3697 | // Check protocols on qualified interfaces. |
| 3698 | Decl *GDecl = 0; |
| Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3699 | for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(), |
| Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3700 | E = QIdTy->qual_end(); I != E; ++I) { |
| Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3701 | if (Member) |
| 3702 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) { |
| 3703 | GDecl = PD; |
| 3704 | break; |
| 3705 | } |
| 3706 | // Also must look for a getter or setter name which uses property syntax. |
| Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3707 | if (ObjCMethodDecl *OMD = (*I)->getInstanceMethod(Sel)) { |
| Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3708 | GDecl = OMD; |
| 3709 | break; |
| 3710 | } |
| 3711 | } |
| 3712 | if (!GDecl) { |
| Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3713 | for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(), |
| Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3714 | E = QIdTy->qual_end(); I != E; ++I) { |
| 3715 | // Search in the protocol-qualifier list of current protocol. |
| Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3716 | GDecl = FindGetterSetterNameDeclFromProtocolList(*I, Member, Sel, |
| 3717 | Context); |
| Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3718 | if (GDecl) |
| 3719 | return GDecl; |
| 3720 | } |
| 3721 | } |
| 3722 | return GDecl; |
| 3723 | } |
| Chris Lattner | 4bf74fd | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 3724 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3725 | ExprResult |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3726 | Sema::ActOnDependentMemberExpr(Expr *BaseExpr, QualType BaseType, |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3727 | bool IsArrow, SourceLocation OpLoc, |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3728 | const CXXScopeSpec &SS, |
| 3729 | NamedDecl *FirstQualifierInScope, |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3730 | const DeclarationNameInfo &NameInfo, |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3731 | const TemplateArgumentListInfo *TemplateArgs) { |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3732 | // Even in dependent contexts, try to diagnose base expressions with |
| 3733 | // obviously wrong types, e.g.: |
| 3734 | // |
| 3735 | // T* t; |
| 3736 | // t.f; |
| 3737 | // |
| 3738 | // In Obj-C++, however, the above expression is valid, since it could be |
| 3739 | // accessing the 'f' property if T is an Obj-C interface. The extra check |
| 3740 | // allows this, while still reporting an error if T is a struct pointer. |
| 3741 | if (!IsArrow) { |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3742 | const PointerType *PT = BaseType->getAs<PointerType>(); |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3743 | if (PT && (!getLangOptions().ObjC1 || |
| 3744 | PT->getPointeeType()->isRecordType())) { |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3745 | assert(BaseExpr && "cannot happen with implicit member accesses"); |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3746 | Diag(NameInfo.getLoc(), diag::err_typecheck_member_reference_struct_union) |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3747 | << BaseType << BaseExpr->getSourceRange(); |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3748 | return ExprError(); |
| 3749 | } |
| 3750 | } |
| 3751 | |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3752 | assert(BaseType->isDependentType() || |
| 3753 | NameInfo.getName().isDependentName() || |
| Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 3754 | isDependentScopeSpecifier(SS)); |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3755 | |
| 3756 | // Get the type being accessed in BaseType. If this is an arrow, the BaseExpr |
| 3757 | // must have pointer type, and the accessed type is the pointee. |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3758 | return Owned(CXXDependentScopeMemberExpr::Create(Context, BaseExpr, BaseType, |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3759 | IsArrow, OpLoc, |
| Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3760 | SS.getWithLocInContext(Context), |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3761 | FirstQualifierInScope, |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3762 | NameInfo, TemplateArgs)); |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3763 | } |
| 3764 | |
| 3765 | /// We know that the given qualified member reference points only to |
| 3766 | /// declarations which do not belong to the static type of the base |
| 3767 | /// expression. Diagnose the problem. |
| 3768 | static void DiagnoseQualifiedMemberReference(Sema &SemaRef, |
| 3769 | Expr *BaseExpr, |
| 3770 | QualType BaseType, |
| John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 3771 | const CXXScopeSpec &SS, |
| John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 3772 | NamedDecl *rep, |
| 3773 | const DeclarationNameInfo &nameInfo) { |
| John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 3774 | // If this is an implicit member access, use a different set of |
| 3775 | // diagnostics. |
| 3776 | if (!BaseExpr) |
| John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 3777 | return DiagnoseInstanceReference(SemaRef, SS, rep, nameInfo); |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3778 | |
| John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 3779 | SemaRef.Diag(nameInfo.getLoc(), diag::err_qualified_member_of_unrelated) |
| 3780 | << SS.getRange() << rep << BaseType; |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3781 | } |
| 3782 | |
| 3783 | // Check whether the declarations we found through a nested-name |
| 3784 | // specifier in a member expression are actually members of the base |
| 3785 | // type. The restriction here is: |
| 3786 | // |
| 3787 | // C++ [expr.ref]p2: |
| 3788 | // ... In these cases, the id-expression shall name a |
| 3789 | // member of the class or of one of its base classes. |
| 3790 | // |
| 3791 | // So it's perfectly legitimate for the nested-name specifier to name |
| 3792 | // an unrelated class, and for us to find an overload set including |
| 3793 | // decls from classes which are not superclasses, as long as the decl |
| 3794 | // we actually pick through overload resolution is from a superclass. |
| 3795 | bool Sema::CheckQualifiedMemberReference(Expr *BaseExpr, |
| 3796 | QualType BaseType, |
| John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 3797 | const CXXScopeSpec &SS, |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3798 | const LookupResult &R) { |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3799 | const RecordType *BaseRT = BaseType->getAs<RecordType>(); |
| 3800 | if (!BaseRT) { |
| 3801 | // We can't check this yet because the base type is still |
| 3802 | // dependent. |
| 3803 | assert(BaseType->isDependentType()); |
| 3804 | return false; |
| 3805 | } |
| 3806 | CXXRecordDecl *BaseRecord = cast<CXXRecordDecl>(BaseRT->getDecl()); |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3807 | |
| 3808 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3809 | // If this is an implicit member reference and we find a |
| 3810 | // non-instance member, it's not an error. |
| John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 3811 | if (!BaseExpr && !(*I)->isCXXInstanceMember()) |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3812 | return false; |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3813 | |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3814 | // Note that we use the DC of the decl, not the underlying decl. |
| Eli Friedman | 7530049 | 2010-07-27 20:51:02 +0000 | [diff] [blame] | 3815 | DeclContext *DC = (*I)->getDeclContext(); |
| 3816 | while (DC->isTransparentContext()) |
| 3817 | DC = DC->getParent(); |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3818 | |
| Douglas Gregor | a9c3e82 | 2010-07-28 22:27:52 +0000 | [diff] [blame] | 3819 | if (!DC->isRecord()) |
| 3820 | continue; |
| 3821 | |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3822 | llvm::SmallPtrSet<CXXRecordDecl*,4> MemberRecord; |
| Eli Friedman | 7530049 | 2010-07-27 20:51:02 +0000 | [diff] [blame] | 3823 | MemberRecord.insert(cast<CXXRecordDecl>(DC)->getCanonicalDecl()); |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3824 | |
| 3825 | if (!IsProvablyNotDerivedFrom(*this, BaseRecord, MemberRecord)) |
| 3826 | return false; |
| 3827 | } |
| 3828 | |
| John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 3829 | DiagnoseQualifiedMemberReference(*this, BaseExpr, BaseType, SS, |
| 3830 | R.getRepresentativeDecl(), |
| 3831 | R.getLookupNameInfo()); |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3832 | return true; |
| 3833 | } |
| 3834 | |
| 3835 | static bool |
| 3836 | LookupMemberExprInRecord(Sema &SemaRef, LookupResult &R, |
| 3837 | SourceRange BaseRange, const RecordType *RTy, |
| John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 3838 | SourceLocation OpLoc, CXXScopeSpec &SS, |
| 3839 | bool HasTemplateArgs) { |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3840 | RecordDecl *RDecl = RTy->getDecl(); |
| 3841 | if (SemaRef.RequireCompleteType(OpLoc, QualType(RTy, 0), |
| Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 3842 | SemaRef.PDiag(diag::err_typecheck_incomplete_tag) |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3843 | << BaseRange)) |
| 3844 | return true; |
| 3845 | |
| John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 3846 | if (HasTemplateArgs) { |
| 3847 | // LookupTemplateName doesn't expect these both to exist simultaneously. |
| 3848 | QualType ObjectType = SS.isSet() ? QualType() : QualType(RTy, 0); |
| 3849 | |
| 3850 | bool MOUS; |
| 3851 | SemaRef.LookupTemplateName(R, 0, SS, ObjectType, false, MOUS); |
| 3852 | return false; |
| 3853 | } |
| 3854 | |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3855 | DeclContext *DC = RDecl; |
| 3856 | if (SS.isSet()) { |
| 3857 | // If the member name was a qualified-id, look into the |
| 3858 | // nested-name-specifier. |
| 3859 | DC = SemaRef.computeDeclContext(SS, false); |
| 3860 | |
| John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 3861 | if (SemaRef.RequireCompleteDeclContext(SS, DC)) { |
| John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 3862 | SemaRef.Diag(SS.getRange().getEnd(), diag::err_typecheck_incomplete_tag) |
| 3863 | << SS.getRange() << DC; |
| 3864 | return true; |
| 3865 | } |
| 3866 | |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3867 | assert(DC && "Cannot handle non-computable dependent contexts in lookup"); |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3868 | |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3869 | if (!isa<TypeDecl>(DC)) { |
| 3870 | SemaRef.Diag(R.getNameLoc(), diag::err_qualified_member_nonclass) |
| 3871 | << DC << SS.getRange(); |
| 3872 | return true; |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3873 | } |
| 3874 | } |
| 3875 | |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3876 | // The record definition is complete, now look up the member. |
| 3877 | SemaRef.LookupQualifiedName(R, DC); |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3878 | |
| Douglas Gregor | af2bd47 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 3879 | if (!R.empty()) |
| 3880 | return false; |
| 3881 | |
| 3882 | // We didn't find anything with the given name, so try to correct |
| 3883 | // for typos. |
| 3884 | DeclarationName Name = R.getLookupName(); |
| Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 3885 | if (SemaRef.CorrectTypo(R, 0, &SS, DC, false, Sema::CTC_MemberLookup) && |
| Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 3886 | !R.empty() && |
| Douglas Gregor | af2bd47 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 3887 | (isa<ValueDecl>(*R.begin()) || isa<FunctionTemplateDecl>(*R.begin()))) { |
| 3888 | SemaRef.Diag(R.getNameLoc(), diag::err_no_member_suggest) |
| 3889 | << Name << DC << R.getLookupName() << SS.getRange() |
| Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3890 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 3891 | R.getLookupName().getAsString()); |
| Douglas Gregor | 6da8362 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 3892 | if (NamedDecl *ND = R.getAsSingle<NamedDecl>()) |
| 3893 | SemaRef.Diag(ND->getLocation(), diag::note_previous_decl) |
| 3894 | << ND->getDeclName(); |
| Douglas Gregor | af2bd47 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 3895 | return false; |
| 3896 | } else { |
| 3897 | R.clear(); |
| Douglas Gregor | c048c52 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 3898 | R.setLookupName(Name); |
| Douglas Gregor | af2bd47 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 3899 | } |
| 3900 | |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3901 | return false; |
| 3902 | } |
| 3903 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3904 | ExprResult |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3905 | Sema::BuildMemberReferenceExpr(Expr *Base, QualType BaseType, |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3906 | SourceLocation OpLoc, bool IsArrow, |
| Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3907 | CXXScopeSpec &SS, |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3908 | NamedDecl *FirstQualifierInScope, |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3909 | const DeclarationNameInfo &NameInfo, |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3910 | const TemplateArgumentListInfo *TemplateArgs) { |
| John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 3911 | if (BaseType->isDependentType() || |
| 3912 | (SS.isSet() && isDependentScopeSpecifier(SS))) |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3913 | return ActOnDependentMemberExpr(Base, BaseType, |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3914 | IsArrow, OpLoc, |
| 3915 | SS, FirstQualifierInScope, |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3916 | NameInfo, TemplateArgs); |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3917 | |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3918 | LookupResult R(*this, NameInfo, LookupMemberName); |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3919 | |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3920 | // Implicit member accesses. |
| 3921 | if (!Base) { |
| 3922 | QualType RecordTy = BaseType; |
| 3923 | if (IsArrow) RecordTy = RecordTy->getAs<PointerType>()->getPointeeType(); |
| 3924 | if (LookupMemberExprInRecord(*this, R, SourceRange(), |
| 3925 | RecordTy->getAs<RecordType>(), |
| John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 3926 | OpLoc, SS, TemplateArgs != 0)) |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3927 | return ExprError(); |
| 3928 | |
| 3929 | // Explicit member accesses. |
| 3930 | } else { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3931 | ExprResult BaseResult = Owned(Base); |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3932 | ExprResult Result = |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3933 | LookupMemberExpr(R, BaseResult, IsArrow, OpLoc, |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3934 | SS, /*ObjCImpDecl*/ 0, TemplateArgs != 0); |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3935 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3936 | if (BaseResult.isInvalid()) |
| 3937 | return ExprError(); |
| 3938 | Base = BaseResult.take(); |
| 3939 | |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3940 | if (Result.isInvalid()) { |
| 3941 | Owned(Base); |
| 3942 | return ExprError(); |
| 3943 | } |
| 3944 | |
| 3945 | if (Result.get()) |
| 3946 | return move(Result); |
| Sebastian Redl | fa1f70f | 2010-05-07 09:25:11 +0000 | [diff] [blame] | 3947 | |
| 3948 | // LookupMemberExpr can modify Base, and thus change BaseType |
| 3949 | BaseType = Base->getType(); |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3950 | } |
| 3951 | |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3952 | return BuildMemberReferenceExpr(Base, BaseType, |
| John McCall | 38836f0 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 3953 | OpLoc, IsArrow, SS, FirstQualifierInScope, |
| 3954 | R, TemplateArgs); |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3955 | } |
| 3956 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3957 | ExprResult |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3958 | Sema::BuildMemberReferenceExpr(Expr *BaseExpr, QualType BaseExprType, |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3959 | SourceLocation OpLoc, bool IsArrow, |
| 3960 | const CXXScopeSpec &SS, |
| John McCall | 38836f0 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 3961 | NamedDecl *FirstQualifierInScope, |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3962 | LookupResult &R, |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 3963 | const TemplateArgumentListInfo *TemplateArgs, |
| 3964 | bool SuppressQualifierCheck) { |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3965 | QualType BaseType = BaseExprType; |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3966 | if (IsArrow) { |
| 3967 | assert(BaseType->isPointerType()); |
| 3968 | BaseType = BaseType->getAs<PointerType>()->getPointeeType(); |
| 3969 | } |
| John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 3970 | R.setBaseObjectType(BaseType); |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3971 | |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3972 | const DeclarationNameInfo &MemberNameInfo = R.getLookupNameInfo(); |
| 3973 | DeclarationName MemberName = MemberNameInfo.getName(); |
| 3974 | SourceLocation MemberLoc = MemberNameInfo.getLoc(); |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3975 | |
| 3976 | if (R.isAmbiguous()) |
| Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 3977 | return ExprError(); |
| 3978 | |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3979 | if (R.empty()) { |
| 3980 | // Rederive where we looked up. |
| 3981 | DeclContext *DC = (SS.isSet() |
| 3982 | ? computeDeclContext(SS, false) |
| 3983 | : BaseType->getAs<RecordType>()->getDecl()); |
| Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3984 | |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3985 | Diag(R.getNameLoc(), diag::err_no_member) |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3986 | << MemberName << DC |
| 3987 | << (BaseExpr ? BaseExpr->getSourceRange() : SourceRange()); |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3988 | return ExprError(); |
| 3989 | } |
| 3990 | |
| John McCall | 38836f0 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 3991 | // Diagnose lookups that find only declarations from a non-base |
| 3992 | // type. This is possible for either qualified lookups (which may |
| 3993 | // have been qualified with an unrelated type) or implicit member |
| 3994 | // expressions (which were found with unqualified lookup and thus |
| 3995 | // may have come from an enclosing scope). Note that it's okay for |
| 3996 | // lookup to find declarations from a non-base type as long as those |
| 3997 | // aren't the ones picked by overload resolution. |
| 3998 | if ((SS.isSet() || !BaseExpr || |
| 3999 | (isa<CXXThisExpr>(BaseExpr) && |
| 4000 | cast<CXXThisExpr>(BaseExpr)->isImplicit())) && |
| Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4001 | !SuppressQualifierCheck && |
| John McCall | 38836f0 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 4002 | CheckQualifiedMemberReference(BaseExpr, BaseType, SS, R)) |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4003 | return ExprError(); |
| 4004 | |
| 4005 | // Construct an unresolved result if we in fact got an unresolved |
| 4006 | // result. |
| 4007 | if (R.isOverloadedResult() || R.isUnresolvableResult()) { |
| John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4008 | // Suppress any lookup-related diagnostics; we'll do these when we |
| 4009 | // pick a member. |
| 4010 | R.suppressDiagnostics(); |
| 4011 | |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4012 | UnresolvedMemberExpr *MemExpr |
| Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 4013 | = UnresolvedMemberExpr::Create(Context, R.isUnresolvableResult(), |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4014 | BaseExpr, BaseExprType, |
| 4015 | IsArrow, OpLoc, |
| Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 4016 | SS.getWithLocInContext(Context), |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4017 | MemberNameInfo, |
| Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 4018 | TemplateArgs, R.begin(), R.end()); |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4019 | |
| 4020 | return Owned(MemExpr); |
| 4021 | } |
| 4022 | |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4023 | assert(R.isSingleResult()); |
| John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 4024 | DeclAccessPair FoundDecl = R.begin().getPair(); |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4025 | NamedDecl *MemberDecl = R.getFoundDecl(); |
| 4026 | |
| 4027 | // FIXME: diagnose the presence of template arguments now. |
| 4028 | |
| 4029 | // If the decl being referenced had an error, return an error for this |
| 4030 | // sub-expr without emitting another error, in order to avoid cascading |
| 4031 | // error cases. |
| 4032 | if (MemberDecl->isInvalidDecl()) |
| 4033 | return ExprError(); |
| 4034 | |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4035 | // Handle the implicit-member-access case. |
| 4036 | if (!BaseExpr) { |
| 4037 | // If this is not an instance member, convert to a non-member access. |
| John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 4038 | if (!MemberDecl->isCXXInstanceMember()) |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4039 | return BuildDeclarationNameExpr(SS, R.getLookupNameInfo(), MemberDecl); |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4040 | |
| Douglas Gregor | b15af89 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 4041 | SourceLocation Loc = R.getNameLoc(); |
| 4042 | if (SS.getRange().isValid()) |
| 4043 | Loc = SS.getRange().getBegin(); |
| 4044 | BaseExpr = new (Context) CXXThisExpr(Loc, BaseExprType,/*isImplicit=*/true); |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4045 | } |
| 4046 | |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4047 | bool ShouldCheckUse = true; |
| 4048 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MemberDecl)) { |
| 4049 | // Don't diagnose the use of a virtual member function unless it's |
| 4050 | // explicitly qualified. |
| 4051 | if (MD->isVirtual() && !SS.isSet()) |
| 4052 | ShouldCheckUse = false; |
| 4053 | } |
| 4054 | |
| 4055 | // Check the use of this member. |
| 4056 | if (ShouldCheckUse && DiagnoseUseOfDecl(MemberDecl, MemberLoc)) { |
| 4057 | Owned(BaseExpr); |
| 4058 | return ExprError(); |
| 4059 | } |
| 4060 | |
| John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4061 | // Perform a property load on the base regardless of whether we |
| 4062 | // actually need it for the declaration. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4063 | if (BaseExpr->getObjectKind() == OK_ObjCProperty) { |
| 4064 | ExprResult Result = ConvertPropertyForRValue(BaseExpr); |
| 4065 | if (Result.isInvalid()) |
| 4066 | return ExprError(); |
| 4067 | BaseExpr = Result.take(); |
| 4068 | } |
| John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4069 | |
| John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 4070 | if (FieldDecl *FD = dyn_cast<FieldDecl>(MemberDecl)) |
| 4071 | return BuildFieldReferenceExpr(*this, BaseExpr, IsArrow, |
| 4072 | SS, FD, FoundDecl, MemberNameInfo); |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4073 | |
| Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 4074 | if (IndirectFieldDecl *FD = dyn_cast<IndirectFieldDecl>(MemberDecl)) |
| 4075 | // We may have found a field within an anonymous union or struct |
| 4076 | // (C++ [class.union]). |
| John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 4077 | return BuildAnonymousStructUnionMemberReference(SS, MemberLoc, FD, |
| John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4078 | BaseExpr, OpLoc); |
| Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 4079 | |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4080 | if (VarDecl *Var = dyn_cast<VarDecl>(MemberDecl)) { |
| 4081 | MarkDeclarationReferenced(MemberLoc, Var); |
| 4082 | return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4083 | Var, FoundDecl, MemberNameInfo, |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4084 | Var->getType().getNonReferenceType(), |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4085 | VK_LValue, OK_Ordinary)); |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4086 | } |
| 4087 | |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4088 | if (CXXMethodDecl *MemberFn = dyn_cast<CXXMethodDecl>(MemberDecl)) { |
| John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 4089 | ExprValueKind valueKind; |
| 4090 | QualType type; |
| 4091 | if (MemberFn->isInstance()) { |
| 4092 | valueKind = VK_RValue; |
| 4093 | type = Context.BoundMemberTy; |
| 4094 | } else { |
| 4095 | valueKind = VK_LValue; |
| 4096 | type = MemberFn->getType(); |
| 4097 | } |
| 4098 | |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4099 | MarkDeclarationReferenced(MemberLoc, MemberDecl); |
| 4100 | return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4101 | MemberFn, FoundDecl, MemberNameInfo, |
| John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 4102 | type, valueKind, OK_Ordinary)); |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4103 | } |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4104 | assert(!isa<FunctionDecl>(MemberDecl) && "member function not C++ method?"); |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4105 | |
| 4106 | if (EnumConstantDecl *Enum = dyn_cast<EnumConstantDecl>(MemberDecl)) { |
| 4107 | MarkDeclarationReferenced(MemberLoc, MemberDecl); |
| 4108 | return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4109 | Enum, FoundDecl, MemberNameInfo, |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4110 | Enum->getType(), VK_RValue, OK_Ordinary)); |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4111 | } |
| 4112 | |
| 4113 | Owned(BaseExpr); |
| 4114 | |
| Douglas Gregor | 861eb80 | 2010-04-25 20:55:08 +0000 | [diff] [blame] | 4115 | // We found something that we didn't expect. Complain. |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4116 | if (isa<TypeDecl>(MemberDecl)) |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4117 | Diag(MemberLoc, diag::err_typecheck_member_reference_type) |
| Douglas Gregor | 861eb80 | 2010-04-25 20:55:08 +0000 | [diff] [blame] | 4118 | << MemberName << BaseType << int(IsArrow); |
| 4119 | else |
| 4120 | Diag(MemberLoc, diag::err_typecheck_member_reference_unknown) |
| 4121 | << MemberName << BaseType << int(IsArrow); |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4122 | |
| Douglas Gregor | 861eb80 | 2010-04-25 20:55:08 +0000 | [diff] [blame] | 4123 | Diag(MemberDecl->getLocation(), diag::note_member_declared_here) |
| 4124 | << MemberName; |
| Douglas Gregor | 516d672 | 2010-04-25 21:15:30 +0000 | [diff] [blame] | 4125 | R.suppressDiagnostics(); |
| Douglas Gregor | 861eb80 | 2010-04-25 20:55:08 +0000 | [diff] [blame] | 4126 | return ExprError(); |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4127 | } |
| 4128 | |
| John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4129 | /// Given that normal member access failed on the given expression, |
| 4130 | /// and given that the expression's type involves builtin-id or |
| 4131 | /// builtin-Class, decide whether substituting in the redefinition |
| 4132 | /// types would be profitable. The redefinition type is whatever |
| 4133 | /// this translation unit tried to typedef to id/Class; we store |
| 4134 | /// it to the side and then re-use it in places like this. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4135 | static bool ShouldTryAgainWithRedefinitionType(Sema &S, ExprResult &base) { |
| John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4136 | const ObjCObjectPointerType *opty |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4137 | = base.get()->getType()->getAs<ObjCObjectPointerType>(); |
| John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4138 | if (!opty) return false; |
| 4139 | |
| 4140 | const ObjCObjectType *ty = opty->getObjectType(); |
| 4141 | |
| 4142 | QualType redef; |
| 4143 | if (ty->isObjCId()) { |
| 4144 | redef = S.Context.ObjCIdRedefinitionType; |
| 4145 | } else if (ty->isObjCClass()) { |
| 4146 | redef = S.Context.ObjCClassRedefinitionType; |
| 4147 | } else { |
| 4148 | return false; |
| 4149 | } |
| 4150 | |
| 4151 | // Do the substitution as long as the redefinition type isn't just a |
| 4152 | // possibly-qualified pointer to builtin-id or builtin-Class again. |
| 4153 | opty = redef->getAs<ObjCObjectPointerType>(); |
| 4154 | if (opty && !opty->getObjectType()->getInterface() != 0) |
| 4155 | return false; |
| 4156 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4157 | base = S.ImpCastExprToType(base.take(), redef, CK_BitCast); |
| John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4158 | return true; |
| 4159 | } |
| 4160 | |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4161 | /// Look up the given member of the given non-type-dependent |
| 4162 | /// expression. This can return in one of two ways: |
| 4163 | /// * If it returns a sentinel null-but-valid result, the caller will |
| 4164 | /// assume that lookup was performed and the results written into |
| 4165 | /// the provided structure. It will take over from there. |
| 4166 | /// * Otherwise, the returned expression will be produced in place of |
| 4167 | /// an ordinary member expression. |
| 4168 | /// |
| 4169 | /// The ObjCImpDecl bit is a gross hack that will need to be properly |
| 4170 | /// fixed for ObjC++. |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4171 | ExprResult |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4172 | Sema::LookupMemberExpr(LookupResult &R, ExprResult &BaseExpr, |
| John McCall | a928c65 | 2009-12-07 22:46:59 +0000 | [diff] [blame] | 4173 | bool &IsArrow, SourceLocation OpLoc, |
| Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4174 | CXXScopeSpec &SS, |
| John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4175 | Decl *ObjCImpDecl, bool HasTemplateArgs) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4176 | assert(BaseExpr.get() && "no base expression"); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4177 | |
| Steve Naroff | eaaae46 | 2007-12-16 21:42:28 +0000 | [diff] [blame] | 4178 | // Perform default conversions. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4179 | BaseExpr = DefaultFunctionArrayConversion(BaseExpr.take()); |
| Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4180 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4181 | if (IsArrow) { |
| 4182 | BaseExpr = DefaultLvalueConversion(BaseExpr.take()); |
| 4183 | if (BaseExpr.isInvalid()) |
| 4184 | return ExprError(); |
| 4185 | } |
| 4186 | |
| 4187 | QualType BaseType = BaseExpr.get()->getType(); |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4188 | assert(!BaseType->isDependentType()); |
| 4189 | |
| 4190 | DeclarationName MemberName = R.getLookupName(); |
| 4191 | SourceLocation MemberLoc = R.getNameLoc(); |
| Douglas Gregor | d82ae38 | 2009-11-06 06:30:47 +0000 | [diff] [blame] | 4192 | |
| John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4193 | // For later type-checking purposes, turn arrow accesses into dot |
| 4194 | // accesses. The only access type we support that doesn't follow |
| 4195 | // the C equivalence "a->b === (*a).b" is ObjC property accesses, |
| 4196 | // and those never use arrows, so this is unaffected. |
| 4197 | if (IsArrow) { |
| 4198 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) |
| 4199 | BaseType = Ptr->getPointeeType(); |
| 4200 | else if (const ObjCObjectPointerType *Ptr |
| 4201 | = BaseType->getAs<ObjCObjectPointerType>()) |
| 4202 | BaseType = Ptr->getPointeeType(); |
| 4203 | else if (BaseType->isRecordType()) { |
| 4204 | // Recover from arrow accesses to records, e.g.: |
| 4205 | // struct MyRecord foo; |
| 4206 | // foo->bar |
| 4207 | // This is actually well-formed in C++ if MyRecord has an |
| 4208 | // overloaded operator->, but that should have been dealt with |
| 4209 | // by now. |
| 4210 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4211 | << BaseType << int(IsArrow) << BaseExpr.get()->getSourceRange() |
| John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4212 | << FixItHint::CreateReplacement(OpLoc, "."); |
| 4213 | IsArrow = false; |
| John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 4214 | } else if (BaseType == Context.BoundMemberTy) { |
| 4215 | goto fail; |
| John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4216 | } else { |
| 4217 | Diag(MemberLoc, diag::err_typecheck_member_reference_arrow) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4218 | << BaseType << BaseExpr.get()->getSourceRange(); |
| John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4219 | return ExprError(); |
| Douglas Gregor | d82ae38 | 2009-11-06 06:30:47 +0000 | [diff] [blame] | 4220 | } |
| 4221 | } |
| 4222 | |
| John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4223 | // Handle field access to simple records. |
| 4224 | if (const RecordType *RTy = BaseType->getAs<RecordType>()) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4225 | if (LookupMemberExprInRecord(*this, R, BaseExpr.get()->getSourceRange(), |
| John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4226 | RTy, OpLoc, SS, HasTemplateArgs)) |
| 4227 | return ExprError(); |
| 4228 | |
| 4229 | // Returning valid-but-null is how we indicate to the caller that |
| 4230 | // the lookup result was filled in. |
| 4231 | return Owned((Expr*) 0); |
| David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 4232 | } |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4233 | |
| John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4234 | // Handle ivar access to Objective-C objects. |
| 4235 | if (const ObjCObjectType *OTy = BaseType->getAs<ObjCObjectType>()) { |
| Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4236 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
| John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4237 | |
| 4238 | // There are three cases for the base type: |
| 4239 | // - builtin id (qualified or unqualified) |
| 4240 | // - builtin Class (qualified or unqualified) |
| 4241 | // - an interface |
| 4242 | ObjCInterfaceDecl *IDecl = OTy->getInterface(); |
| 4243 | if (!IDecl) { |
| 4244 | // There's an implicit 'isa' ivar on all objects. |
| 4245 | // But we only actually find it this way on objects of type 'id', |
| 4246 | // apparently. |
| 4247 | if (OTy->isObjCId() && Member->isStr("isa")) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4248 | return Owned(new (Context) ObjCIsaExpr(BaseExpr.take(), IsArrow, MemberLoc, |
| John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4249 | Context.getObjCClassType())); |
| 4250 | |
| 4251 | if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr)) |
| 4252 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 4253 | ObjCImpDecl, HasTemplateArgs); |
| 4254 | goto fail; |
| 4255 | } |
| 4256 | |
| 4257 | ObjCInterfaceDecl *ClassDeclared; |
| 4258 | ObjCIvarDecl *IV = IDecl->lookupInstanceVariable(Member, ClassDeclared); |
| 4259 | |
| 4260 | if (!IV) { |
| 4261 | // Attempt to correct for typos in ivar names. |
| 4262 | LookupResult Res(*this, R.getLookupName(), R.getNameLoc(), |
| 4263 | LookupMemberName); |
| 4264 | if (CorrectTypo(Res, 0, 0, IDecl, false, |
| 4265 | IsArrow ? CTC_ObjCIvarLookup |
| 4266 | : CTC_ObjCPropertyLookup) && |
| 4267 | (IV = Res.getAsSingle<ObjCIvarDecl>())) { |
| 4268 | Diag(R.getNameLoc(), |
| 4269 | diag::err_typecheck_member_reference_ivar_suggest) |
| 4270 | << IDecl->getDeclName() << MemberName << IV->getDeclName() |
| 4271 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 4272 | IV->getNameAsString()); |
| 4273 | Diag(IV->getLocation(), diag::note_previous_decl) |
| 4274 | << IV->getDeclName(); |
| 4275 | } else { |
| 4276 | Res.clear(); |
| 4277 | Res.setLookupName(Member); |
| 4278 | |
| 4279 | Diag(MemberLoc, diag::err_typecheck_member_reference_ivar) |
| 4280 | << IDecl->getDeclName() << MemberName |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4281 | << BaseExpr.get()->getSourceRange(); |
| John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4282 | return ExprError(); |
| 4283 | } |
| 4284 | } |
| 4285 | |
| 4286 | // If the decl being referenced had an error, return an error for this |
| 4287 | // sub-expr without emitting another error, in order to avoid cascading |
| 4288 | // error cases. |
| 4289 | if (IV->isInvalidDecl()) |
| 4290 | return ExprError(); |
| 4291 | |
| 4292 | // Check whether we can reference this field. |
| 4293 | if (DiagnoseUseOfDecl(IV, MemberLoc)) |
| 4294 | return ExprError(); |
| 4295 | if (IV->getAccessControl() != ObjCIvarDecl::Public && |
| 4296 | IV->getAccessControl() != ObjCIvarDecl::Package) { |
| 4297 | ObjCInterfaceDecl *ClassOfMethodDecl = 0; |
| 4298 | if (ObjCMethodDecl *MD = getCurMethodDecl()) |
| 4299 | ClassOfMethodDecl = MD->getClassInterface(); |
| 4300 | else if (ObjCImpDecl && getCurFunctionDecl()) { |
| 4301 | // Case of a c-function declared inside an objc implementation. |
| 4302 | // FIXME: For a c-style function nested inside an objc implementation |
| 4303 | // class, there is no implementation context available, so we pass |
| 4304 | // down the context as argument to this routine. Ideally, this context |
| 4305 | // need be passed down in the AST node and somehow calculated from the |
| 4306 | // AST for a function decl. |
| 4307 | if (ObjCImplementationDecl *IMPD = |
| 4308 | dyn_cast<ObjCImplementationDecl>(ObjCImpDecl)) |
| 4309 | ClassOfMethodDecl = IMPD->getClassInterface(); |
| 4310 | else if (ObjCCategoryImplDecl* CatImplClass = |
| 4311 | dyn_cast<ObjCCategoryImplDecl>(ObjCImpDecl)) |
| 4312 | ClassOfMethodDecl = CatImplClass->getClassInterface(); |
| 4313 | } |
| 4314 | |
| 4315 | if (IV->getAccessControl() == ObjCIvarDecl::Private) { |
| 4316 | if (ClassDeclared != IDecl || |
| 4317 | ClassOfMethodDecl != ClassDeclared) |
| 4318 | Diag(MemberLoc, diag::error_private_ivar_access) |
| 4319 | << IV->getDeclName(); |
| 4320 | } else if (!IDecl->isSuperClassOf(ClassOfMethodDecl)) |
| 4321 | // @protected |
| 4322 | Diag(MemberLoc, diag::error_protected_ivar_access) |
| 4323 | << IV->getDeclName(); |
| 4324 | } |
| 4325 | |
| 4326 | return Owned(new (Context) ObjCIvarRefExpr(IV, IV->getType(), |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4327 | MemberLoc, BaseExpr.take(), |
| John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4328 | IsArrow)); |
| 4329 | } |
| 4330 | |
| 4331 | // Objective-C property access. |
| 4332 | const ObjCObjectPointerType *OPT; |
| 4333 | if (!IsArrow && (OPT = BaseType->getAs<ObjCObjectPointerType>())) { |
| 4334 | // This actually uses the base as an r-value. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4335 | BaseExpr = DefaultLvalueConversion(BaseExpr.take()); |
| 4336 | if (BaseExpr.isInvalid()) |
| 4337 | return ExprError(); |
| 4338 | |
| 4339 | assert(Context.hasSameUnqualifiedType(BaseType, BaseExpr.get()->getType())); |
| John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4340 | |
| 4341 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
| 4342 | |
| 4343 | const ObjCObjectType *OT = OPT->getObjectType(); |
| 4344 | |
| 4345 | // id, with and without qualifiers. |
| 4346 | if (OT->isObjCId()) { |
| 4347 | // Check protocols on qualified interfaces. |
| 4348 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
| 4349 | if (Decl *PMDecl = FindGetterSetterNameDecl(OPT, Member, Sel, Context)) { |
| 4350 | if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(PMDecl)) { |
| 4351 | // Check the use of this declaration |
| 4352 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 4353 | return ExprError(); |
| 4354 | |
| Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4355 | QualType T = PD->getType(); |
| 4356 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 4357 | T = getMessageSendResultType(BaseType, Getter, false, false); |
| 4358 | |
| 4359 | return Owned(new (Context) ObjCPropertyRefExpr(PD, T, |
| John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4360 | VK_LValue, |
| 4361 | OK_ObjCProperty, |
| 4362 | MemberLoc, |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4363 | BaseExpr.take())); |
| John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4364 | } |
| 4365 | |
| 4366 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(PMDecl)) { |
| 4367 | // Check the use of this method. |
| 4368 | if (DiagnoseUseOfDecl(OMD, MemberLoc)) |
| 4369 | return ExprError(); |
| 4370 | Selector SetterSel = |
| 4371 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
| 4372 | PP.getSelectorTable(), Member); |
| 4373 | ObjCMethodDecl *SMD = 0; |
| 4374 | if (Decl *SDecl = FindGetterSetterNameDecl(OPT, /*Property id*/0, |
| 4375 | SetterSel, Context)) |
| 4376 | SMD = dyn_cast<ObjCMethodDecl>(SDecl); |
| Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4377 | QualType PType = getMessageSendResultType(BaseType, OMD, false, |
| 4378 | false); |
| John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4379 | |
| 4380 | ExprValueKind VK = VK_LValue; |
| 4381 | if (!getLangOptions().CPlusPlus && |
| 4382 | IsCForbiddenLValueType(Context, PType)) |
| 4383 | VK = VK_RValue; |
| 4384 | ExprObjectKind OK = (VK == VK_RValue ? OK_Ordinary : OK_ObjCProperty); |
| 4385 | |
| 4386 | return Owned(new (Context) ObjCPropertyRefExpr(OMD, SMD, PType, |
| 4387 | VK, OK, |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4388 | MemberLoc, BaseExpr.take())); |
| John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4389 | } |
| 4390 | } |
| Fariborz Jahanian | b03a4c2 | 2011-03-15 17:27:48 +0000 | [diff] [blame] | 4391 | // Use of id.member can only be for a property reference. Do not |
| 4392 | // use the 'id' redefinition in this case. |
| 4393 | if (IsArrow && ShouldTryAgainWithRedefinitionType(*this, BaseExpr)) |
| John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4394 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 4395 | ObjCImpDecl, HasTemplateArgs); |
| 4396 | |
| 4397 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
| 4398 | << MemberName << BaseType); |
| 4399 | } |
| 4400 | |
| 4401 | // 'Class', unqualified only. |
| 4402 | if (OT->isObjCClass()) { |
| 4403 | // Only works in a method declaration (??!). |
| 4404 | ObjCMethodDecl *MD = getCurMethodDecl(); |
| 4405 | if (!MD) { |
| 4406 | if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr)) |
| 4407 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 4408 | ObjCImpDecl, HasTemplateArgs); |
| 4409 | |
| 4410 | goto fail; |
| 4411 | } |
| 4412 | |
| 4413 | // Also must look for a getter name which uses property syntax. |
| 4414 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
| Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4415 | ObjCInterfaceDecl *IFace = MD->getClassInterface(); |
| 4416 | ObjCMethodDecl *Getter; |
| Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4417 | if ((Getter = IFace->lookupClassMethod(Sel))) { |
| 4418 | // Check the use of this method. |
| 4419 | if (DiagnoseUseOfDecl(Getter, MemberLoc)) |
| 4420 | return ExprError(); |
| John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4421 | } else |
| Fariborz Jahanian | ecbbb6e | 2010-12-03 23:37:08 +0000 | [diff] [blame] | 4422 | Getter = IFace->lookupPrivateMethod(Sel, false); |
| Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4423 | // If we found a getter then this may be a valid dot-reference, we |
| 4424 | // will look for the matching setter, in case it is needed. |
| 4425 | Selector SetterSel = |
| John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4426 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
| 4427 | PP.getSelectorTable(), Member); |
| Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4428 | ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel); |
| 4429 | if (!Setter) { |
| 4430 | // If this reference is in an @implementation, also check for 'private' |
| 4431 | // methods. |
| Fariborz Jahanian | ecbbb6e | 2010-12-03 23:37:08 +0000 | [diff] [blame] | 4432 | Setter = IFace->lookupPrivateMethod(SetterSel, false); |
| Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4433 | } |
| 4434 | // Look through local category implementations associated with the class. |
| 4435 | if (!Setter) |
| 4436 | Setter = IFace->getCategoryClassMethod(SetterSel); |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4437 | |
| Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4438 | if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc)) |
| 4439 | return ExprError(); |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4440 | |
| Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4441 | if (Getter || Setter) { |
| 4442 | QualType PType; |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4443 | |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4444 | ExprValueKind VK = VK_LValue; |
| 4445 | if (Getter) { |
| Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4446 | PType = getMessageSendResultType(QualType(OT, 0), Getter, true, |
| 4447 | false); |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4448 | if (!getLangOptions().CPlusPlus && |
| 4449 | IsCForbiddenLValueType(Context, PType)) |
| 4450 | VK = VK_RValue; |
| 4451 | } else { |
| Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4452 | // Get the expression type from Setter's incoming parameter. |
| 4453 | PType = (*(Setter->param_end() -1))->getType(); |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4454 | } |
| 4455 | ExprObjectKind OK = (VK == VK_RValue ? OK_Ordinary : OK_ObjCProperty); |
| 4456 | |
| Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4457 | // FIXME: we must check that the setter has property type. |
| John McCall | b7bd14f | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 4458 | return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter, |
| 4459 | PType, VK, OK, |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4460 | MemberLoc, BaseExpr.take())); |
| Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4461 | } |
| John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4462 | |
| 4463 | if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr)) |
| 4464 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 4465 | ObjCImpDecl, HasTemplateArgs); |
| 4466 | |
| Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4467 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
| John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4468 | << MemberName << BaseType); |
| Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4469 | } |
| Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4470 | |
| John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4471 | // Normal property access. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4472 | return HandleExprPropertyRefExpr(OPT, BaseExpr.get(), MemberName, MemberLoc, |
| John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4473 | SourceLocation(), QualType(), false); |
| Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4474 | } |
| Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 4475 | |
| Chris Lattner | b63a745 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 4476 | // Handle 'field access' to vectors, such as 'V.xx'. |
| Chris Lattner | 6c7ce10 | 2009-02-16 21:11:58 +0000 | [diff] [blame] | 4477 | if (BaseType->isExtVectorType()) { |
| John McCall | 15317a2 | 2010-12-15 04:42:30 +0000 | [diff] [blame] | 4478 | // FIXME: this expr should store IsArrow. |
| Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 4479 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4480 | ExprValueKind VK = (IsArrow ? VK_LValue : BaseExpr.get()->getValueKind()); |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4481 | QualType ret = CheckExtVectorComponent(*this, BaseType, VK, OpLoc, |
| 4482 | Member, MemberLoc); |
| Chris Lattner | b63a745 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 4483 | if (ret.isNull()) |
| Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4484 | return ExprError(); |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4485 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4486 | return Owned(new (Context) ExtVectorElementExpr(ret, VK, BaseExpr.take(), |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4487 | *Member, MemberLoc)); |
| Chris Lattner | b63a745 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 4488 | } |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4489 | |
| John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4490 | // Adjust builtin-sel to the appropriate redefinition type if that's |
| 4491 | // not just a pointer to builtin-sel again. |
| 4492 | if (IsArrow && |
| 4493 | BaseType->isSpecificBuiltinType(BuiltinType::ObjCSel) && |
| 4494 | !Context.ObjCSelRedefinitionType->isObjCSelType()) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4495 | BaseExpr = ImpCastExprToType(BaseExpr.take(), Context.ObjCSelRedefinitionType, |
| 4496 | CK_BitCast); |
| John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4497 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 4498 | ObjCImpDecl, HasTemplateArgs); |
| 4499 | } |
| 4500 | |
| 4501 | // Failure cases. |
| 4502 | fail: |
| 4503 | |
| Matt Beaumont-Gay | 06de255 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4504 | // Recover from dot accesses to pointers, e.g.: |
| 4505 | // type *foo; |
| 4506 | // foo.bar |
| 4507 | // This is actually well-formed in two cases: |
| 4508 | // - 'type' is an Objective C type |
| 4509 | // - 'bar' is a pseudo-destructor name which happens to refer to |
| 4510 | // the appropriate pointer type |
| John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4511 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) { |
| Matt Beaumont-Gay | 06de255 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4512 | if (!IsArrow && Ptr->getPointeeType()->isRecordType() && |
| 4513 | MemberName.getNameKind() != DeclarationName::CXXDestructorName) { |
| John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4514 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4515 | << BaseType << int(IsArrow) << BaseExpr.get()->getSourceRange() |
| Matt Beaumont-Gay | 06de255 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4516 | << FixItHint::CreateReplacement(OpLoc, "->"); |
| John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4517 | |
| 4518 | // Recurse as an -> access. |
| 4519 | IsArrow = true; |
| 4520 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 4521 | ObjCImpDecl, HasTemplateArgs); |
| 4522 | } |
| John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4523 | } |
| 4524 | |
| Matt Beaumont-Gay | 06de255 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4525 | // If the user is trying to apply -> or . to a function name, it's probably |
| 4526 | // because they forgot parentheses to call that function. |
| Matt Beaumont-Gay | 3c27391 | 2011-05-04 22:10:40 +0000 | [diff] [blame] | 4527 | QualType ZeroArgCallTy; |
| 4528 | UnresolvedSet<4> Overloads; |
| 4529 | if (isExprCallable(*BaseExpr.get(), ZeroArgCallTy, Overloads)) { |
| 4530 | if (ZeroArgCallTy.isNull()) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4531 | Diag(BaseExpr.get()->getExprLoc(), diag::err_member_reference_needs_call) |
| Matt Beaumont-Gay | 3c27391 | 2011-05-04 22:10:40 +0000 | [diff] [blame] | 4532 | << (Overloads.size() > 1) << 0 << BaseExpr.get()->getSourceRange(); |
| 4533 | UnresolvedSet<2> PlausibleOverloads; |
| 4534 | for (OverloadExpr::decls_iterator It = Overloads.begin(), |
| 4535 | DeclsEnd = Overloads.end(); It != DeclsEnd; ++It) { |
| 4536 | const FunctionDecl *OverloadDecl = cast<FunctionDecl>(*It); |
| 4537 | QualType OverloadResultTy = OverloadDecl->getResultType(); |
| 4538 | if ((!IsArrow && OverloadResultTy->isRecordType()) || |
| 4539 | (IsArrow && OverloadResultTy->isPointerType() && |
| 4540 | OverloadResultTy->getPointeeType()->isRecordType())) |
| 4541 | PlausibleOverloads.addDecl(It.getDecl()); |
| Matt Beaumont-Gay | 06de255 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4542 | } |
| Matt Beaumont-Gay | 3c27391 | 2011-05-04 22:10:40 +0000 | [diff] [blame] | 4543 | NoteOverloads(PlausibleOverloads, BaseExpr.get()->getExprLoc()); |
| Matt Beaumont-Gay | 06de255 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4544 | return ExprError(); |
| 4545 | } |
| Matt Beaumont-Gay | 3c27391 | 2011-05-04 22:10:40 +0000 | [diff] [blame] | 4546 | if ((!IsArrow && ZeroArgCallTy->isRecordType()) || |
| 4547 | (IsArrow && ZeroArgCallTy->isPointerType() && |
| 4548 | ZeroArgCallTy->getPointeeType()->isRecordType())) { |
| 4549 | // At this point, we know BaseExpr looks like it's potentially callable |
| 4550 | // with 0 arguments, and that it returns something of a reasonable type, |
| 4551 | // so we can emit a fixit and carry on pretending that BaseExpr was |
| 4552 | // actually a CallExpr. |
| 4553 | SourceLocation ParenInsertionLoc = |
| 4554 | PP.getLocForEndOfToken(BaseExpr.get()->getLocEnd()); |
| 4555 | Diag(BaseExpr.get()->getExprLoc(), diag::err_member_reference_needs_call) |
| 4556 | << (Overloads.size() > 1) << 1 << BaseExpr.get()->getSourceRange() |
| 4557 | << FixItHint::CreateInsertion(ParenInsertionLoc, "()"); |
| 4558 | // FIXME: Try this before emitting the fixit, and suppress diagnostics |
| 4559 | // while doing so. |
| 4560 | ExprResult NewBase = |
| 4561 | ActOnCallExpr(0, BaseExpr.take(), ParenInsertionLoc, |
| 4562 | MultiExprArg(*this, 0, 0), |
| 4563 | ParenInsertionLoc.getFileLocWithOffset(1)); |
| 4564 | if (NewBase.isInvalid()) |
| 4565 | return ExprError(); |
| 4566 | BaseExpr = NewBase; |
| 4567 | BaseExpr = DefaultFunctionArrayConversion(BaseExpr.take()); |
| 4568 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 4569 | ObjCImpDecl, HasTemplateArgs); |
| Matt Beaumont-Gay | 06de255 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4570 | } |
| Matt Beaumont-Gay | 06de255 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4571 | } |
| 4572 | |
| Douglas Gregor | 0b08ba4 | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 4573 | Diag(MemberLoc, diag::err_typecheck_member_reference_struct_union) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4574 | << BaseType << BaseExpr.get()->getSourceRange(); |
| Douglas Gregor | 0b08ba4 | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 4575 | |
| Douglas Gregor | 0b08ba4 | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 4576 | return ExprError(); |
| Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 4577 | } |
| 4578 | |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4579 | /// The main callback when the parser finds something like |
| 4580 | /// expression . [nested-name-specifier] identifier |
| 4581 | /// expression -> [nested-name-specifier] identifier |
| 4582 | /// where 'identifier' encompasses a fairly broad spectrum of |
| 4583 | /// possibilities, including destructor and operator references. |
| 4584 | /// |
| 4585 | /// \param OpKind either tok::arrow or tok::period |
| 4586 | /// \param HasTrailingLParen whether the next token is '(', which |
| 4587 | /// is used to diagnose mis-uses of special members that can |
| 4588 | /// only be called |
| 4589 | /// \param ObjCImpDecl the current ObjC @implementation decl; |
| 4590 | /// this is an ugly hack around the fact that ObjC @implementations |
| 4591 | /// aren't properly put in the context chain |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4592 | ExprResult Sema::ActOnMemberAccessExpr(Scope *S, Expr *Base, |
| John McCall | 15317a2 | 2010-12-15 04:42:30 +0000 | [diff] [blame] | 4593 | SourceLocation OpLoc, |
| 4594 | tok::TokenKind OpKind, |
| 4595 | CXXScopeSpec &SS, |
| 4596 | UnqualifiedId &Id, |
| 4597 | Decl *ObjCImpDecl, |
| 4598 | bool HasTrailingLParen) { |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4599 | if (SS.isSet() && SS.isInvalid()) |
| 4600 | return ExprError(); |
| 4601 | |
| Francois Pichet | 6422579 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 4602 | // Warn about the explicit constructor calls Microsoft extension. |
| 4603 | if (getLangOptions().Microsoft && |
| 4604 | Id.getKind() == UnqualifiedId::IK_ConstructorName) |
| 4605 | Diag(Id.getSourceRange().getBegin(), |
| 4606 | diag::ext_ms_explicit_constructor_call); |
| 4607 | |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4608 | TemplateArgumentListInfo TemplateArgsBuffer; |
| 4609 | |
| 4610 | // Decompose the name into its component parts. |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4611 | DeclarationNameInfo NameInfo; |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4612 | const TemplateArgumentListInfo *TemplateArgs; |
| 4613 | DecomposeUnqualifiedId(*this, Id, TemplateArgsBuffer, |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4614 | NameInfo, TemplateArgs); |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4615 | |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4616 | DeclarationName Name = NameInfo.getName(); |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4617 | bool IsArrow = (OpKind == tok::arrow); |
| 4618 | |
| 4619 | NamedDecl *FirstQualifierInScope |
| 4620 | = (!SS.isSet() ? 0 : FindFirstQualifierInScope(S, |
| 4621 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()))); |
| 4622 | |
| 4623 | // This is a postfix expression, so get rid of ParenListExprs. |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4624 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base); |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4625 | if (Result.isInvalid()) return ExprError(); |
| 4626 | Base = Result.take(); |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4627 | |
| Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 4628 | if (Base->getType()->isDependentType() || Name.isDependentName() || |
| 4629 | isDependentScopeSpecifier(SS)) { |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4630 | Result = ActOnDependentMemberExpr(Base, Base->getType(), |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4631 | IsArrow, OpLoc, |
| 4632 | SS, FirstQualifierInScope, |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4633 | NameInfo, TemplateArgs); |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4634 | } else { |
| Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4635 | LookupResult R(*this, NameInfo, LookupMemberName); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4636 | ExprResult BaseResult = Owned(Base); |
| 4637 | Result = LookupMemberExpr(R, BaseResult, IsArrow, OpLoc, |
| John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 4638 | SS, ObjCImpDecl, TemplateArgs != 0); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4639 | if (BaseResult.isInvalid()) |
| 4640 | return ExprError(); |
| 4641 | Base = BaseResult.take(); |
| Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 4642 | |
| John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 4643 | if (Result.isInvalid()) { |
| 4644 | Owned(Base); |
| 4645 | return ExprError(); |
| 4646 | } |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4647 | |
| John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 4648 | if (Result.get()) { |
| 4649 | // The only way a reference to a destructor can be used is to |
| 4650 | // immediately call it, which falls into this case. If the |
| 4651 | // next token is not a '(', produce a diagnostic and build the |
| 4652 | // call now. |
| 4653 | if (!HasTrailingLParen && |
| 4654 | Id.getKind() == UnqualifiedId::IK_DestructorName) |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4655 | return DiagnoseDtorReference(NameInfo.getLoc(), Result.get()); |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4656 | |
| John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 4657 | return move(Result); |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4658 | } |
| 4659 | |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4660 | Result = BuildMemberReferenceExpr(Base, Base->getType(), |
| John McCall | 38836f0 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 4661 | OpLoc, IsArrow, SS, FirstQualifierInScope, |
| 4662 | R, TemplateArgs); |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4663 | } |
| 4664 | |
| 4665 | return move(Result); |
| Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 4666 | } |
| 4667 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4668 | ExprResult Sema::BuildCXXDefaultArgExpr(SourceLocation CallLoc, |
| Nico Weber | 44887f6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 4669 | FunctionDecl *FD, |
| 4670 | ParmVarDecl *Param) { |
| Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4671 | if (Param->hasUnparsedDefaultArg()) { |
| Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4672 | Diag(CallLoc, |
| Nico Weber | ebd45a0 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 4673 | diag::err_use_of_default_argument_to_function_declared_later) << |
| Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4674 | FD << cast<CXXRecordDecl>(FD->getDeclContext())->getDeclName(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4675 | Diag(UnparsedDefaultArgLocs[Param], |
| Nico Weber | ebd45a0 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 4676 | diag::note_default_argument_declared_here); |
| Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4677 | return ExprError(); |
| 4678 | } |
| 4679 | |
| 4680 | if (Param->hasUninstantiatedDefaultArg()) { |
| 4681 | Expr *UninstExpr = Param->getUninstantiatedDefaultArg(); |
| Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4682 | |
| Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4683 | // Instantiate the expression. |
| 4684 | MultiLevelTemplateArgumentList ArgList |
| 4685 | = getTemplateInstantiationArgs(FD, 0, /*RelativeToPrimary=*/true); |
| Anders Carlsson | 657bad4 | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 4686 | |
| Nico Weber | 44887f6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 4687 | std::pair<const TemplateArgument *, unsigned> Innermost |
| Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4688 | = ArgList.getInnermost(); |
| 4689 | InstantiatingTemplate Inst(*this, CallLoc, Param, Innermost.first, |
| 4690 | Innermost.second); |
| Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4691 | |
| Nico Weber | 44887f6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 4692 | ExprResult Result; |
| 4693 | { |
| 4694 | // C++ [dcl.fct.default]p5: |
| 4695 | // The names in the [default argument] expression are bound, and |
| 4696 | // the semantic constraints are checked, at the point where the |
| 4697 | // default argument expression appears. |
| Nico Weber | ebd45a0 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 4698 | ContextRAII SavedContext(*this, FD); |
| Nico Weber | 44887f6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 4699 | Result = SubstExpr(UninstExpr, ArgList); |
| 4700 | } |
| Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4701 | if (Result.isInvalid()) |
| 4702 | return ExprError(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4703 | |
| Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4704 | // Check the expression as an initializer for the parameter. |
| 4705 | InitializedEntity Entity |
| Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 4706 | = InitializedEntity::InitializeParameter(Context, Param); |
| Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4707 | InitializationKind Kind |
| 4708 | = InitializationKind::CreateCopy(Param->getLocation(), |
| 4709 | /*FIXME:EqualLoc*/UninstExpr->getSourceRange().getBegin()); |
| 4710 | Expr *ResultE = Result.takeAs<Expr>(); |
| Douglas Gregor | 25ab25f | 2009-12-23 18:19:08 +0000 | [diff] [blame] | 4711 | |
| Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4712 | InitializationSequence InitSeq(*this, Entity, Kind, &ResultE, 1); |
| 4713 | Result = InitSeq.Perform(*this, Entity, Kind, |
| 4714 | MultiExprArg(*this, &ResultE, 1)); |
| 4715 | if (Result.isInvalid()) |
| 4716 | return ExprError(); |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4717 | |
| Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4718 | // Build the default argument expression. |
| 4719 | return Owned(CXXDefaultArgExpr::Create(Context, CallLoc, Param, |
| 4720 | Result.takeAs<Expr>())); |
| Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4721 | } |
| 4722 | |
| Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4723 | // If the default expression creates temporaries, we need to |
| 4724 | // push them to the current stack of expression temporaries so they'll |
| 4725 | // be properly destroyed. |
| 4726 | // FIXME: We should really be rebuilding the default argument with new |
| 4727 | // bound temporaries; see the comment in PR5810. |
| Douglas Gregor | 6ed2fee | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 4728 | for (unsigned i = 0, e = Param->getNumDefaultArgTemporaries(); i != e; ++i) { |
| 4729 | CXXTemporary *Temporary = Param->getDefaultArgTemporary(i); |
| 4730 | MarkDeclarationReferenced(Param->getDefaultArg()->getLocStart(), |
| 4731 | const_cast<CXXDestructorDecl*>(Temporary->getDestructor())); |
| 4732 | ExprTemporaries.push_back(Temporary); |
| 4733 | } |
| Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4734 | |
| 4735 | // We already type-checked the argument, so we know it works. |
| Douglas Gregor | 32b3de5 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 4736 | // Just mark all of the declarations in this potentially-evaluated expression |
| 4737 | // as being "referenced". |
| Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4738 | MarkDeclarationsReferencedInExpr(Param->getDefaultArg()); |
| Douglas Gregor | 033f675 | 2009-12-23 23:03:06 +0000 | [diff] [blame] | 4739 | return Owned(CXXDefaultArgExpr::Create(Context, CallLoc, Param)); |
| Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4740 | } |
| 4741 | |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4742 | /// ConvertArgumentsForCall - Converts the arguments specified in |
| 4743 | /// Args/NumArgs to the parameter types of the function FDecl with |
| 4744 | /// function prototype Proto. Call is the call expression itself, and |
| 4745 | /// Fn is the function expression. For a C++ member function, this |
| 4746 | /// routine does not attempt to convert the object argument. Returns |
| 4747 | /// true if the call is ill-formed. |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4748 | bool |
| 4749 | Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4750 | FunctionDecl *FDecl, |
| Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4751 | const FunctionProtoType *Proto, |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4752 | Expr **Args, unsigned NumArgs, |
| 4753 | SourceLocation RParenLoc) { |
| John McCall | bebede4 | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 4754 | // Bail out early if calling a builtin with custom typechecking. |
| 4755 | // We don't need to do this in the |
| 4756 | if (FDecl) |
| 4757 | if (unsigned ID = FDecl->getBuiltinID()) |
| 4758 | if (Context.BuiltinInfo.hasCustomTypechecking(ID)) |
| 4759 | return false; |
| 4760 | |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4761 | // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4762 | // assignment, to the types of the corresponding parameter, ... |
| 4763 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| Douglas Gregor | b6b9961 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 4764 | bool Invalid = false; |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4765 | |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4766 | // If too few arguments are available (and we don't have default |
| 4767 | // arguments for the remaining parameters), don't make the call. |
| 4768 | if (NumArgs < NumArgsInProto) { |
| 4769 | if (!FDecl || NumArgs < FDecl->getMinRequiredArguments()) |
| 4770 | return Diag(RParenLoc, diag::err_typecheck_call_too_few_args) |
| Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 4771 | << Fn->getType()->isBlockPointerType() |
| Eric Christopher | abf1e18 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 4772 | << NumArgsInProto << NumArgs << Fn->getSourceRange(); |
| Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4773 | Call->setNumArgs(Context, NumArgsInProto); |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4774 | } |
| 4775 | |
| 4776 | // If too many are passed and not variadic, error on the extras and drop |
| 4777 | // them. |
| 4778 | if (NumArgs > NumArgsInProto) { |
| 4779 | if (!Proto->isVariadic()) { |
| 4780 | Diag(Args[NumArgsInProto]->getLocStart(), |
| 4781 | diag::err_typecheck_call_too_many_args) |
| Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 4782 | << Fn->getType()->isBlockPointerType() |
| Eric Christopher | 2a5aaff | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 4783 | << NumArgsInProto << NumArgs << Fn->getSourceRange() |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4784 | << SourceRange(Args[NumArgsInProto]->getLocStart(), |
| 4785 | Args[NumArgs-1]->getLocEnd()); |
| Ted Kremenek | 99a337e | 2011-04-04 17:22:27 +0000 | [diff] [blame] | 4786 | |
| 4787 | // Emit the location of the prototype. |
| 4788 | if (FDecl && !FDecl->getBuiltinID()) |
| 4789 | Diag(FDecl->getLocStart(), |
| 4790 | diag::note_typecheck_call_too_many_args) |
| 4791 | << FDecl; |
| 4792 | |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4793 | // This deletes the extra arguments. |
| Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4794 | Call->setNumArgs(Context, NumArgsInProto); |
| Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4795 | return true; |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4796 | } |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4797 | } |
| Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4798 | llvm::SmallVector<Expr *, 8> AllArgs; |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4799 | VariadicCallType CallType = |
| Fariborz Jahanian | 6f2d25e | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 4800 | Proto->isVariadic() ? VariadicFunction : VariadicDoesNotApply; |
| 4801 | if (Fn->getType()->isBlockPointerType()) |
| 4802 | CallType = VariadicBlock; // Block |
| 4803 | else if (isa<MemberExpr>(Fn)) |
| 4804 | CallType = VariadicMethod; |
| Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4805 | Invalid = GatherArgumentsForCall(Call->getSourceRange().getBegin(), FDecl, |
| Fariborz Jahanian | 4fa66ce | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 4806 | Proto, 0, Args, NumArgs, AllArgs, CallType); |
| Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4807 | if (Invalid) |
| 4808 | return true; |
| 4809 | unsigned TotalNumArgs = AllArgs.size(); |
| 4810 | for (unsigned i = 0; i < TotalNumArgs; ++i) |
| 4811 | Call->setArg(i, AllArgs[i]); |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4812 | |
| Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4813 | return false; |
| 4814 | } |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4815 | |
| Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4816 | bool Sema::GatherArgumentsForCall(SourceLocation CallLoc, |
| 4817 | FunctionDecl *FDecl, |
| 4818 | const FunctionProtoType *Proto, |
| 4819 | unsigned FirstProtoArg, |
| 4820 | Expr **Args, unsigned NumArgs, |
| 4821 | llvm::SmallVector<Expr *, 8> &AllArgs, |
| Fariborz Jahanian | 6f2d25e | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 4822 | VariadicCallType CallType) { |
| Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4823 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 4824 | unsigned NumArgsToCheck = NumArgs; |
| 4825 | bool Invalid = false; |
| 4826 | if (NumArgs != NumArgsInProto) |
| 4827 | // Use default arguments for missing arguments |
| 4828 | NumArgsToCheck = NumArgsInProto; |
| 4829 | unsigned ArgIx = 0; |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4830 | // Continue to check argument types (even if we have too few/many args). |
| Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4831 | for (unsigned i = FirstProtoArg; i != NumArgsToCheck; i++) { |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4832 | QualType ProtoArgType = Proto->getArgType(i); |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4833 | |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4834 | Expr *Arg; |
| Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4835 | if (ArgIx < NumArgs) { |
| 4836 | Arg = Args[ArgIx++]; |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4837 | |
| Eli Friedman | 3164fb1 | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 4838 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 4839 | ProtoArgType, |
| Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 4840 | PDiag(diag::err_call_incomplete_argument) |
| Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4841 | << Arg->getSourceRange())) |
| Eli Friedman | 3164fb1 | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 4842 | return true; |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4843 | |
| Douglas Gregor | bbeb5c3 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 4844 | // Pass the argument |
| 4845 | ParmVarDecl *Param = 0; |
| 4846 | if (FDecl && i < FDecl->getNumParams()) |
| 4847 | Param = FDecl->getParamDecl(i); |
| Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4848 | |
| Douglas Gregor | bbeb5c3 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 4849 | InitializedEntity Entity = |
| Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 4850 | Param? InitializedEntity::InitializeParameter(Context, Param) |
| 4851 | : InitializedEntity::InitializeParameter(Context, ProtoArgType); |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4852 | ExprResult ArgE = PerformCopyInitialization(Entity, |
| John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4853 | SourceLocation(), |
| 4854 | Owned(Arg)); |
| Douglas Gregor | bbeb5c3 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 4855 | if (ArgE.isInvalid()) |
| 4856 | return true; |
| 4857 | |
| 4858 | Arg = ArgE.takeAs<Expr>(); |
| Anders Carlsson | 84613c4 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 4859 | } else { |
| Anders Carlsson | c80a127 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 4860 | ParmVarDecl *Param = FDecl->getParamDecl(i); |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4861 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4862 | ExprResult ArgExpr = |
| Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4863 | BuildCXXDefaultArgExpr(CallLoc, FDecl, Param); |
| Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4864 | if (ArgExpr.isInvalid()) |
| 4865 | return true; |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4866 | |
| Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4867 | Arg = ArgExpr.takeAs<Expr>(); |
| Anders Carlsson | 84613c4 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 4868 | } |
| Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4869 | AllArgs.push_back(Arg); |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4870 | } |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4871 | |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4872 | // If this is a variadic call, handle args passed through "...". |
| Fariborz Jahanian | 6f2d25e | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 4873 | if (CallType != VariadicDoesNotApply) { |
| John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 4874 | |
| 4875 | // Assume that extern "C" functions with variadic arguments that |
| 4876 | // return __unknown_anytype aren't *really* variadic. |
| 4877 | if (Proto->getResultType() == Context.UnknownAnyTy && |
| 4878 | FDecl && FDecl->isExternC()) { |
| 4879 | for (unsigned i = ArgIx; i != NumArgs; ++i) { |
| 4880 | ExprResult arg; |
| 4881 | if (isa<ExplicitCastExpr>(Args[i]->IgnoreParens())) |
| 4882 | arg = DefaultFunctionArrayLvalueConversion(Args[i]); |
| 4883 | else |
| 4884 | arg = DefaultVariadicArgumentPromotion(Args[i], CallType, FDecl); |
| 4885 | Invalid |= arg.isInvalid(); |
| 4886 | AllArgs.push_back(arg.take()); |
| 4887 | } |
| 4888 | |
| 4889 | // Otherwise do argument promotion, (C99 6.5.2.2p7). |
| 4890 | } else { |
| 4891 | for (unsigned i = ArgIx; i != NumArgs; ++i) { |
| 4892 | ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], CallType, FDecl); |
| 4893 | Invalid |= Arg.isInvalid(); |
| 4894 | AllArgs.push_back(Arg.take()); |
| 4895 | } |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4896 | } |
| 4897 | } |
| Douglas Gregor | b6b9961 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 4898 | return Invalid; |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4899 | } |
| 4900 | |
| John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 4901 | /// Given a function expression of unknown-any type, try to rebuild it |
| 4902 | /// to have a function type. |
| 4903 | static ExprResult rebuildUnknownAnyFunction(Sema &S, Expr *fn); |
| 4904 | |
| Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 4905 | /// ActOnCallExpr - Handle a call to Fn with the specified array of arguments. |
| Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 4906 | /// This provides the location of the left/right parens and a list of comma |
| 4907 | /// locations. |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4908 | ExprResult |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4909 | Sema::ActOnCallExpr(Scope *S, Expr *Fn, SourceLocation LParenLoc, |
| Peter Collingbourne | 41f8546 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4910 | MultiExprArg args, SourceLocation RParenLoc, |
| 4911 | Expr *ExecConfig) { |
| Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4912 | unsigned NumArgs = args.size(); |
| Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4913 | |
| 4914 | // Since this might be a postfix expression, get rid of ParenListExprs. |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4915 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Fn); |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4916 | if (Result.isInvalid()) return ExprError(); |
| 4917 | Fn = Result.take(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4918 | |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4919 | Expr **Args = args.release(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4920 | |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4921 | if (getLangOptions().CPlusPlus) { |
| Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 4922 | // If this is a pseudo-destructor expression, build the call immediately. |
| 4923 | if (isa<CXXPseudoDestructorExpr>(Fn)) { |
| 4924 | if (NumArgs > 0) { |
| 4925 | // Pseudo-destructor calls should not have any arguments. |
| 4926 | Diag(Fn->getLocStart(), diag::err_pseudo_dtor_call_with_args) |
| Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4927 | << FixItHint::CreateRemoval( |
| Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 4928 | SourceRange(Args[0]->getLocStart(), |
| 4929 | Args[NumArgs-1]->getLocEnd())); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4930 | |
| Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 4931 | NumArgs = 0; |
| 4932 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4933 | |
| Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 4934 | return Owned(new (Context) CallExpr(Context, Fn, 0, 0, Context.VoidTy, |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4935 | VK_RValue, RParenLoc)); |
| Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 4936 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4937 | |
| Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4938 | // Determine whether this is a dependent call inside a C++ template, |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4939 | // in which case we won't do any semantic analysis now. |
| Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 4940 | // FIXME: Will need to cache the results of name lookup (including ADL) in |
| 4941 | // Fn. |
| Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4942 | bool Dependent = false; |
| 4943 | if (Fn->isTypeDependent()) |
| 4944 | Dependent = true; |
| 4945 | else if (Expr::hasAnyTypeDependentArguments(Args, NumArgs)) |
| 4946 | Dependent = true; |
| 4947 | |
| Peter Collingbourne | 41f8546 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4948 | if (Dependent) { |
| 4949 | if (ExecConfig) { |
| 4950 | return Owned(new (Context) CUDAKernelCallExpr( |
| 4951 | Context, Fn, cast<CallExpr>(ExecConfig), Args, NumArgs, |
| 4952 | Context.DependentTy, VK_RValue, RParenLoc)); |
| 4953 | } else { |
| 4954 | return Owned(new (Context) CallExpr(Context, Fn, Args, NumArgs, |
| 4955 | Context.DependentTy, VK_RValue, |
| 4956 | RParenLoc)); |
| 4957 | } |
| 4958 | } |
| Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4959 | |
| 4960 | // Determine whether this is a call to an object (C++ [over.call.object]). |
| 4961 | if (Fn->getType()->isRecordType()) |
| 4962 | return Owned(BuildCallToObjectOfClassType(S, Fn, LParenLoc, Args, NumArgs, |
| Douglas Gregor | ce5aa33 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 4963 | RParenLoc)); |
| Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4964 | |
| John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 4965 | if (Fn->getType() == Context.UnknownAnyTy) { |
| 4966 | ExprResult result = rebuildUnknownAnyFunction(*this, Fn); |
| 4967 | if (result.isInvalid()) return ExprError(); |
| 4968 | Fn = result.take(); |
| 4969 | } |
| 4970 | |
| John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 4971 | if (Fn->getType() == Context.BoundMemberTy) { |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4972 | return BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
| Douglas Gregor | ce5aa33 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 4973 | RParenLoc); |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4974 | } |
| John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 4975 | } |
| John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4976 | |
| John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 4977 | // Check for overloaded calls. This can happen even in C due to extensions. |
| 4978 | if (Fn->getType() == Context.OverloadTy) { |
| 4979 | OverloadExpr::FindResult find = OverloadExpr::find(Fn); |
| 4980 | |
| 4981 | // We aren't supposed to apply this logic if there's an '&' involved. |
| 4982 | if (!find.IsAddressOfOperand) { |
| 4983 | OverloadExpr *ovl = find.Expression; |
| 4984 | if (isa<UnresolvedLookupExpr>(ovl)) { |
| 4985 | UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(ovl); |
| 4986 | return BuildOverloadedCallExpr(S, Fn, ULE, LParenLoc, Args, NumArgs, |
| 4987 | RParenLoc, ExecConfig); |
| 4988 | } else { |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4989 | return BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
| Douglas Gregor | ce5aa33 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 4990 | RParenLoc); |
| Anders Carlsson | 61914b5 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 4991 | } |
| 4992 | } |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4993 | } |
| 4994 | |
| Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 4995 | // If we're directly calling a function, get the appropriate declaration. |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4996 | |
| Eli Friedman | e14b199 | 2009-12-26 03:35:45 +0000 | [diff] [blame] | 4997 | Expr *NakedFn = Fn->IgnoreParens(); |
| Douglas Gregor | 928479e | 2010-11-09 20:03:54 +0000 | [diff] [blame] | 4998 | |
| John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 4999 | NamedDecl *NDecl = 0; |
| Douglas Gregor | 59f16ed | 2010-10-25 20:48:33 +0000 | [diff] [blame] | 5000 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(NakedFn)) |
| 5001 | if (UnOp->getOpcode() == UO_AddrOf) |
| 5002 | NakedFn = UnOp->getSubExpr()->IgnoreParens(); |
| 5003 | |
| John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5004 | if (isa<DeclRefExpr>(NakedFn)) |
| 5005 | NDecl = cast<DeclRefExpr>(NakedFn)->getDecl(); |
| John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 5006 | else if (isa<MemberExpr>(NakedFn)) |
| 5007 | NDecl = cast<MemberExpr>(NakedFn)->getMemberDecl(); |
| John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5008 | |
| Peter Collingbourne | 41f8546 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 5009 | return BuildResolvedCallExpr(Fn, NDecl, LParenLoc, Args, NumArgs, RParenLoc, |
| 5010 | ExecConfig); |
| 5011 | } |
| 5012 | |
| 5013 | ExprResult |
| 5014 | Sema::ActOnCUDAExecConfigExpr(Scope *S, SourceLocation LLLLoc, |
| 5015 | MultiExprArg execConfig, SourceLocation GGGLoc) { |
| 5016 | FunctionDecl *ConfigDecl = Context.getcudaConfigureCallDecl(); |
| 5017 | if (!ConfigDecl) |
| 5018 | return ExprError(Diag(LLLLoc, diag::err_undeclared_var_use) |
| 5019 | << "cudaConfigureCall"); |
| 5020 | QualType ConfigQTy = ConfigDecl->getType(); |
| 5021 | |
| 5022 | DeclRefExpr *ConfigDR = new (Context) DeclRefExpr( |
| 5023 | ConfigDecl, ConfigQTy, VK_LValue, LLLLoc); |
| 5024 | |
| 5025 | return ActOnCallExpr(S, ConfigDR, LLLLoc, execConfig, GGGLoc, 0); |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5026 | } |
| 5027 | |
| Tanya Lattner | 55808c1 | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 5028 | /// ActOnAsTypeExpr - create a new asType (bitcast) from the arguments. |
| 5029 | /// |
| 5030 | /// __builtin_astype( value, dst type ) |
| 5031 | /// |
| 5032 | ExprResult Sema::ActOnAsTypeExpr(Expr *expr, ParsedType destty, |
| 5033 | SourceLocation BuiltinLoc, |
| 5034 | SourceLocation RParenLoc) { |
| 5035 | ExprValueKind VK = VK_RValue; |
| 5036 | ExprObjectKind OK = OK_Ordinary; |
| 5037 | QualType DstTy = GetTypeFromParser(destty); |
| 5038 | QualType SrcTy = expr->getType(); |
| 5039 | if (Context.getTypeSize(DstTy) != Context.getTypeSize(SrcTy)) |
| 5040 | return ExprError(Diag(BuiltinLoc, |
| 5041 | diag::err_invalid_astype_of_different_size) |
| Peter Collingbourne | 23f1bee | 2011-06-08 15:15:17 +0000 | [diff] [blame] | 5042 | << DstTy |
| 5043 | << SrcTy |
| Tanya Lattner | 55808c1 | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 5044 | << expr->getSourceRange()); |
| 5045 | return Owned(new (Context) AsTypeExpr(expr, DstTy, VK, OK, BuiltinLoc, RParenLoc)); |
| 5046 | } |
| 5047 | |
| John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5048 | /// BuildResolvedCallExpr - Build a call to a resolved expression, |
| 5049 | /// i.e. an expression not of \p OverloadTy. The expression should |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5050 | /// unary-convert to an expression of function-pointer or |
| 5051 | /// block-pointer type. |
| 5052 | /// |
| 5053 | /// \param NDecl the declaration being called, if available |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5054 | ExprResult |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5055 | Sema::BuildResolvedCallExpr(Expr *Fn, NamedDecl *NDecl, |
| 5056 | SourceLocation LParenLoc, |
| 5057 | Expr **Args, unsigned NumArgs, |
| Peter Collingbourne | 41f8546 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 5058 | SourceLocation RParenLoc, |
| 5059 | Expr *Config) { |
| John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5060 | FunctionDecl *FDecl = dyn_cast_or_null<FunctionDecl>(NDecl); |
| 5061 | |
| Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5062 | // Promote the function operand. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5063 | ExprResult Result = UsualUnaryConversions(Fn); |
| 5064 | if (Result.isInvalid()) |
| 5065 | return ExprError(); |
| 5066 | Fn = Result.take(); |
| Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5067 | |
| Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 5068 | // Make the call expr early, before semantic checks. This guarantees cleanup |
| 5069 | // of arguments and function on error. |
| Peter Collingbourne | 41f8546 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 5070 | CallExpr *TheCall; |
| 5071 | if (Config) { |
| 5072 | TheCall = new (Context) CUDAKernelCallExpr(Context, Fn, |
| 5073 | cast<CallExpr>(Config), |
| 5074 | Args, NumArgs, |
| 5075 | Context.BoolTy, |
| 5076 | VK_RValue, |
| 5077 | RParenLoc); |
| 5078 | } else { |
| 5079 | TheCall = new (Context) CallExpr(Context, Fn, |
| 5080 | Args, NumArgs, |
| 5081 | Context.BoolTy, |
| 5082 | VK_RValue, |
| 5083 | RParenLoc); |
| 5084 | } |
| Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5085 | |
| John McCall | bebede4 | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 5086 | unsigned BuiltinID = (FDecl ? FDecl->getBuiltinID() : 0); |
| 5087 | |
| 5088 | // Bail out early if calling a builtin with custom typechecking. |
| 5089 | if (BuiltinID && Context.BuiltinInfo.hasCustomTypechecking(BuiltinID)) |
| 5090 | return CheckBuiltinFunctionCall(BuiltinID, TheCall); |
| 5091 | |
| John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 5092 | retry: |
| Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 5093 | const FunctionType *FuncT; |
| John McCall | bebede4 | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 5094 | if (const PointerType *PT = Fn->getType()->getAs<PointerType>()) { |
| Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 5095 | // C99 6.5.2.2p1 - "The expression that denotes the called function shall |
| 5096 | // have type pointer to function". |
| John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5097 | FuncT = PT->getPointeeType()->getAs<FunctionType>(); |
| John McCall | bebede4 | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 5098 | if (FuncT == 0) |
| 5099 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 5100 | << Fn->getType() << Fn->getSourceRange()); |
| 5101 | } else if (const BlockPointerType *BPT = |
| 5102 | Fn->getType()->getAs<BlockPointerType>()) { |
| 5103 | FuncT = BPT->getPointeeType()->castAs<FunctionType>(); |
| 5104 | } else { |
| John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 5105 | // Handle calls to expressions of unknown-any type. |
| 5106 | if (Fn->getType() == Context.UnknownAnyTy) { |
| John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 5107 | ExprResult rewrite = rebuildUnknownAnyFunction(*this, Fn); |
| John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 5108 | if (rewrite.isInvalid()) return ExprError(); |
| 5109 | Fn = rewrite.take(); |
| John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 5110 | TheCall->setCallee(Fn); |
| John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 5111 | goto retry; |
| 5112 | } |
| 5113 | |
| Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5114 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 5115 | << Fn->getType() << Fn->getSourceRange()); |
| John McCall | bebede4 | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 5116 | } |
| Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5117 | |
| Peter Collingbourne | 4b66c47 | 2011-02-23 01:53:29 +0000 | [diff] [blame] | 5118 | if (getLangOptions().CUDA) { |
| 5119 | if (Config) { |
| 5120 | // CUDA: Kernel calls must be to global functions |
| 5121 | if (FDecl && !FDecl->hasAttr<CUDAGlobalAttr>()) |
| 5122 | return ExprError(Diag(LParenLoc,diag::err_kern_call_not_global_function) |
| 5123 | << FDecl->getName() << Fn->getSourceRange()); |
| 5124 | |
| 5125 | // CUDA: Kernel function must have 'void' return type |
| 5126 | if (!FuncT->getResultType()->isVoidType()) |
| 5127 | return ExprError(Diag(LParenLoc, diag::err_kern_type_not_void_return) |
| 5128 | << Fn->getType() << Fn->getSourceRange()); |
| 5129 | } |
| 5130 | } |
| 5131 | |
| Eli Friedman | 3164fb1 | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 5132 | // Check for a valid return type |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5133 | if (CheckCallReturnType(FuncT->getResultType(), |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5134 | Fn->getSourceRange().getBegin(), TheCall, |
| Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 5135 | FDecl)) |
| Eli Friedman | 3164fb1 | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 5136 | return ExprError(); |
| 5137 | |
| Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 5138 | // We know the result type of the call, set it. |
| Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 5139 | TheCall->setType(FuncT->getCallResultType(Context)); |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5140 | TheCall->setValueKind(Expr::getValueKindForType(FuncT->getResultType())); |
| Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5141 | |
| Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5142 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FuncT)) { |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5143 | if (ConvertArgumentsForCall(TheCall, Fn, FDecl, Proto, Args, NumArgs, |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5144 | RParenLoc)) |
| Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5145 | return ExprError(); |
| Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 5146 | } else { |
| Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5147 | assert(isa<FunctionNoProtoType>(FuncT) && "Unknown FunctionType!"); |
| Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5148 | |
| Douglas Gregor | d8e97de | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 5149 | if (FDecl) { |
| 5150 | // Check if we have too few/too many template arguments, based |
| 5151 | // on our knowledge of the function definition. |
| 5152 | const FunctionDecl *Def = 0; |
| Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 5153 | if (FDecl->hasBody(Def) && NumArgs != Def->param_size()) { |
| Douglas Gregor | 8e09a72 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 5154 | const FunctionProtoType *Proto |
| 5155 | = Def->getType()->getAs<FunctionProtoType>(); |
| 5156 | if (!Proto || !(Proto->isVariadic() && NumArgs >= Def->param_size())) |
| Eli Friedman | fcbf7d2 | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 5157 | Diag(RParenLoc, diag::warn_call_wrong_number_of_arguments) |
| 5158 | << (NumArgs > Def->param_size()) << FDecl << Fn->getSourceRange(); |
| Eli Friedman | fcbf7d2 | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 5159 | } |
| Douglas Gregor | 8e09a72 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 5160 | |
| 5161 | // If the function we're calling isn't a function prototype, but we have |
| 5162 | // a function prototype from a prior declaratiom, use that prototype. |
| 5163 | if (!FDecl->hasPrototype()) |
| 5164 | Proto = FDecl->getType()->getAs<FunctionProtoType>(); |
| Douglas Gregor | d8e97de | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 5165 | } |
| 5166 | |
| Steve Naroff | 0b66158 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 5167 | // Promote the arguments (C99 6.5.2.2p6). |
| Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 5168 | for (unsigned i = 0; i != NumArgs; i++) { |
| 5169 | Expr *Arg = Args[i]; |
| Douglas Gregor | 8e09a72 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 5170 | |
| 5171 | if (Proto && i < Proto->getNumArgs()) { |
| Douglas Gregor | 8e09a72 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 5172 | InitializedEntity Entity |
| 5173 | = InitializedEntity::InitializeParameter(Context, |
| 5174 | Proto->getArgType(i)); |
| 5175 | ExprResult ArgE = PerformCopyInitialization(Entity, |
| 5176 | SourceLocation(), |
| 5177 | Owned(Arg)); |
| 5178 | if (ArgE.isInvalid()) |
| 5179 | return true; |
| 5180 | |
| 5181 | Arg = ArgE.takeAs<Expr>(); |
| 5182 | |
| 5183 | } else { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5184 | ExprResult ArgE = DefaultArgumentPromotion(Arg); |
| 5185 | |
| 5186 | if (ArgE.isInvalid()) |
| 5187 | return true; |
| 5188 | |
| 5189 | Arg = ArgE.takeAs<Expr>(); |
| Douglas Gregor | 8e09a72 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 5190 | } |
| 5191 | |
| Douglas Gregor | 8302541 | 2010-10-26 05:45:40 +0000 | [diff] [blame] | 5192 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 5193 | Arg->getType(), |
| 5194 | PDiag(diag::err_call_incomplete_argument) |
| 5195 | << Arg->getSourceRange())) |
| 5196 | return ExprError(); |
| 5197 | |
| Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 5198 | TheCall->setArg(i, Arg); |
| Steve Naroff | 0b66158 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 5199 | } |
| Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 5200 | } |
| Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 5201 | |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5202 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(FDecl)) |
| 5203 | if (!Method->isStatic()) |
| Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5204 | return ExprError(Diag(LParenLoc, diag::err_member_call_without_object) |
| 5205 | << Fn->getSourceRange()); |
| Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5206 | |
| Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 5207 | // Check for sentinels |
| 5208 | if (NDecl) |
| 5209 | DiagnoseSentinelCalls(NDecl, LParenLoc, Args, NumArgs); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5210 | |
| Chris Lattner | b87b1b3 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 5211 | // Do special checking on direct calls to functions. |
| Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 5212 | if (FDecl) { |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5213 | if (CheckFunctionCall(FDecl, TheCall)) |
| Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 5214 | return ExprError(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5215 | |
| John McCall | bebede4 | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 5216 | if (BuiltinID) |
| Fariborz Jahanian | e8473c2 | 2010-11-30 17:35:24 +0000 | [diff] [blame] | 5217 | return CheckBuiltinFunctionCall(BuiltinID, TheCall); |
| Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 5218 | } else if (NDecl) { |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5219 | if (CheckBlockCall(NDecl, TheCall)) |
| Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 5220 | return ExprError(); |
| 5221 | } |
| Chris Lattner | b87b1b3 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 5222 | |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5223 | return MaybeBindToTemporary(TheCall); |
| Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 5224 | } |
| 5225 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5226 | ExprResult |
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5227 | Sema::ActOnCompoundLiteral(SourceLocation LParenLoc, ParsedType Ty, |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5228 | SourceLocation RParenLoc, Expr *InitExpr) { |
| Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 5229 | assert((Ty != 0) && "ActOnCompoundLiteral(): missing type"); |
| Steve Naroff | 57eb2c5 | 2007-07-19 21:32:11 +0000 | [diff] [blame] | 5230 | // FIXME: put back this assert when initializers are worked out. |
| Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 5231 | //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression"); |
| John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5232 | |
| 5233 | TypeSourceInfo *TInfo; |
| 5234 | QualType literalType = GetTypeFromParser(Ty, &TInfo); |
| 5235 | if (!TInfo) |
| 5236 | TInfo = Context.getTrivialTypeSourceInfo(literalType); |
| 5237 | |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5238 | return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, InitExpr); |
| John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5239 | } |
| 5240 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5241 | ExprResult |
| John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5242 | Sema::BuildCompoundLiteralExpr(SourceLocation LParenLoc, TypeSourceInfo *TInfo, |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5243 | SourceLocation RParenLoc, Expr *literalExpr) { |
| John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5244 | QualType literalType = TInfo->getType(); |
| Anders Carlsson | 2c1ec6d | 2007-12-05 07:24:19 +0000 | [diff] [blame] | 5245 | |
| Eli Friedman | 37a186d | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 5246 | if (literalType->isArrayType()) { |
| Argyrios Kyrtzidis | 8566356 | 2010-11-08 19:14:19 +0000 | [diff] [blame] | 5247 | if (RequireCompleteType(LParenLoc, Context.getBaseElementType(literalType), |
| 5248 | PDiag(diag::err_illegal_decl_array_incomplete_type) |
| 5249 | << SourceRange(LParenLoc, |
| 5250 | literalExpr->getSourceRange().getEnd()))) |
| 5251 | return ExprError(); |
| Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 5252 | if (literalType->isVariableArrayType()) |
| Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5253 | return ExprError(Diag(LParenLoc, diag::err_variable_object_no_init) |
| 5254 | << SourceRange(LParenLoc, literalExpr->getSourceRange().getEnd())); |
| Douglas Gregor | 1c37d9e | 2009-05-21 23:48:18 +0000 | [diff] [blame] | 5255 | } else if (!literalType->isDependentType() && |
| 5256 | RequireCompleteType(LParenLoc, literalType, |
| Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 5257 | PDiag(diag::err_typecheck_decl_incomplete_type) |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5258 | << SourceRange(LParenLoc, |
| Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 5259 | literalExpr->getSourceRange().getEnd()))) |
| Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5260 | return ExprError(); |
| Eli Friedman | 37a186d | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 5261 | |
| Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5262 | InitializedEntity Entity |
| Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5263 | = InitializedEntity::InitializeTemporary(literalType); |
| Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5264 | InitializationKind Kind |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5265 | = InitializationKind::CreateCast(SourceRange(LParenLoc, RParenLoc), |
| Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5266 | /*IsCStyleCast=*/true); |
| Eli Friedman | a553d4a | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 5267 | InitializationSequence InitSeq(*this, Entity, Kind, &literalExpr, 1); |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5268 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, |
| John McCall | 37ad551 | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 5269 | MultiExprArg(*this, &literalExpr, 1), |
| Eli Friedman | a553d4a | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 5270 | &literalType); |
| 5271 | if (Result.isInvalid()) |
| Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5272 | return ExprError(); |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5273 | literalExpr = Result.get(); |
| Steve Naroff | d32419d | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 5274 | |
| Chris Lattner | 7941395 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 5275 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
| Steve Naroff | d32419d | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 5276 | if (isFileScope) { // 6.5.2.5p3 |
| Steve Naroff | 98f7203 | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 5277 | if (CheckForConstantInitializer(literalExpr, literalType)) |
| Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5278 | return ExprError(); |
| Steve Naroff | 98f7203 | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 5279 | } |
| Eli Friedman | a553d4a | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 5280 | |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5281 | // In C, compound literals are l-values for some reason. |
| 5282 | ExprValueKind VK = getLangOptions().CPlusPlus ? VK_RValue : VK_LValue; |
| 5283 | |
| John McCall | 5d7aa7f | 2010-01-19 22:33:45 +0000 | [diff] [blame] | 5284 | return Owned(new (Context) CompoundLiteralExpr(LParenLoc, TInfo, literalType, |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5285 | VK, literalExpr, isFileScope)); |
| Steve Naroff | fbd0983 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 5286 | } |
| 5287 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5288 | ExprResult |
| Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5289 | Sema::ActOnInitList(SourceLocation LBraceLoc, MultiExprArg initlist, |
| Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5290 | SourceLocation RBraceLoc) { |
| 5291 | unsigned NumInit = initlist.size(); |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5292 | Expr **InitList = initlist.release(); |
| Anders Carlsson | 4692db0 | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 5293 | |
| Steve Naroff | 30d242c | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 5294 | // Semantic analysis for initializers is done by ActOnDeclarator() and |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5295 | // CheckInitializer() - it requires knowledge of the object being intialized. |
| Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5296 | |
| Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 5297 | InitListExpr *E = new (Context) InitListExpr(Context, LBraceLoc, InitList, |
| 5298 | NumInit, RBraceLoc); |
| Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 5299 | E->setType(Context.VoidTy); // FIXME: just a place holder for now. |
| Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5300 | return Owned(E); |
| Steve Naroff | fbd0983 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 5301 | } |
| 5302 | |
| John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5303 | /// Prepares for a scalar cast, performing all the necessary stages |
| 5304 | /// except the final cast and returning the kind required. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5305 | static CastKind PrepareScalarCast(Sema &S, ExprResult &Src, QualType DestTy) { |
| John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5306 | // Both Src and Dest are scalar types, i.e. arithmetic or pointer. |
| 5307 | // Also, callers should have filtered out the invalid cases with |
| 5308 | // pointers. Everything else should be possible. |
| 5309 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5310 | QualType SrcTy = Src.get()->getType(); |
| John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5311 | if (S.Context.hasSameUnqualifiedType(SrcTy, DestTy)) |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5312 | return CK_NoOp; |
| Anders Carlsson | 094c459 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 5313 | |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5314 | switch (SrcTy->getScalarTypeKind()) { |
| 5315 | case Type::STK_MemberPointer: |
| 5316 | llvm_unreachable("member pointer type in C"); |
| Abramo Bagnara | ba85497 | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 5317 | |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5318 | case Type::STK_Pointer: |
| 5319 | switch (DestTy->getScalarTypeKind()) { |
| 5320 | case Type::STK_Pointer: |
| 5321 | return DestTy->isObjCObjectPointerType() ? |
| John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5322 | CK_AnyPointerToObjCPointerCast : |
| 5323 | CK_BitCast; |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5324 | case Type::STK_Bool: |
| 5325 | return CK_PointerToBoolean; |
| 5326 | case Type::STK_Integral: |
| 5327 | return CK_PointerToIntegral; |
| 5328 | case Type::STK_Floating: |
| 5329 | case Type::STK_FloatingComplex: |
| 5330 | case Type::STK_IntegralComplex: |
| 5331 | case Type::STK_MemberPointer: |
| 5332 | llvm_unreachable("illegal cast from pointer"); |
| 5333 | } |
| 5334 | break; |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5335 | |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5336 | case Type::STK_Bool: // casting from bool is like casting from an integer |
| 5337 | case Type::STK_Integral: |
| 5338 | switch (DestTy->getScalarTypeKind()) { |
| 5339 | case Type::STK_Pointer: |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5340 | if (Src.get()->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNull)) |
| John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 5341 | return CK_NullToPointer; |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5342 | return CK_IntegralToPointer; |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5343 | case Type::STK_Bool: |
| 5344 | return CK_IntegralToBoolean; |
| 5345 | case Type::STK_Integral: |
| John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5346 | return CK_IntegralCast; |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5347 | case Type::STK_Floating: |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5348 | return CK_IntegralToFloating; |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5349 | case Type::STK_IntegralComplex: |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5350 | Src = S.ImpCastExprToType(Src.take(), DestTy->getAs<ComplexType>()->getElementType(), |
| 5351 | CK_IntegralCast); |
| John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5352 | return CK_IntegralRealToComplex; |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5353 | case Type::STK_FloatingComplex: |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5354 | Src = S.ImpCastExprToType(Src.take(), DestTy->getAs<ComplexType>()->getElementType(), |
| 5355 | CK_IntegralToFloating); |
| John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5356 | return CK_FloatingRealToComplex; |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5357 | case Type::STK_MemberPointer: |
| 5358 | llvm_unreachable("member pointer type in C"); |
| John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5359 | } |
| 5360 | break; |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5361 | |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5362 | case Type::STK_Floating: |
| 5363 | switch (DestTy->getScalarTypeKind()) { |
| 5364 | case Type::STK_Floating: |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5365 | return CK_FloatingCast; |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5366 | case Type::STK_Bool: |
| 5367 | return CK_FloatingToBoolean; |
| 5368 | case Type::STK_Integral: |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5369 | return CK_FloatingToIntegral; |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5370 | case Type::STK_FloatingComplex: |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5371 | Src = S.ImpCastExprToType(Src.take(), DestTy->getAs<ComplexType>()->getElementType(), |
| 5372 | CK_FloatingCast); |
| John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5373 | return CK_FloatingRealToComplex; |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5374 | case Type::STK_IntegralComplex: |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5375 | Src = S.ImpCastExprToType(Src.take(), DestTy->getAs<ComplexType>()->getElementType(), |
| 5376 | CK_FloatingToIntegral); |
| John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5377 | return CK_IntegralRealToComplex; |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5378 | case Type::STK_Pointer: |
| 5379 | llvm_unreachable("valid float->pointer cast?"); |
| 5380 | case Type::STK_MemberPointer: |
| 5381 | llvm_unreachable("member pointer type in C"); |
| John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5382 | } |
| 5383 | break; |
| 5384 | |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5385 | case Type::STK_FloatingComplex: |
| 5386 | switch (DestTy->getScalarTypeKind()) { |
| 5387 | case Type::STK_FloatingComplex: |
| John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5388 | return CK_FloatingComplexCast; |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5389 | case Type::STK_IntegralComplex: |
| John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5390 | return CK_FloatingComplexToIntegralComplex; |
| John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5391 | case Type::STK_Floating: { |
| Abramo Bagnara | ba85497 | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 5392 | QualType ET = SrcTy->getAs<ComplexType>()->getElementType(); |
| John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5393 | if (S.Context.hasSameType(ET, DestTy)) |
| 5394 | return CK_FloatingComplexToReal; |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5395 | Src = S.ImpCastExprToType(Src.take(), ET, CK_FloatingComplexToReal); |
| John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5396 | return CK_FloatingCast; |
| 5397 | } |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5398 | case Type::STK_Bool: |
| John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5399 | return CK_FloatingComplexToBoolean; |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5400 | case Type::STK_Integral: |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5401 | Src = S.ImpCastExprToType(Src.take(), SrcTy->getAs<ComplexType>()->getElementType(), |
| 5402 | CK_FloatingComplexToReal); |
| John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5403 | return CK_FloatingToIntegral; |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5404 | case Type::STK_Pointer: |
| 5405 | llvm_unreachable("valid complex float->pointer cast?"); |
| 5406 | case Type::STK_MemberPointer: |
| 5407 | llvm_unreachable("member pointer type in C"); |
| John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5408 | } |
| 5409 | break; |
| 5410 | |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5411 | case Type::STK_IntegralComplex: |
| 5412 | switch (DestTy->getScalarTypeKind()) { |
| 5413 | case Type::STK_FloatingComplex: |
| John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5414 | return CK_IntegralComplexToFloatingComplex; |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5415 | case Type::STK_IntegralComplex: |
| John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5416 | return CK_IntegralComplexCast; |
| John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5417 | case Type::STK_Integral: { |
| Abramo Bagnara | ba85497 | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 5418 | QualType ET = SrcTy->getAs<ComplexType>()->getElementType(); |
| John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5419 | if (S.Context.hasSameType(ET, DestTy)) |
| 5420 | return CK_IntegralComplexToReal; |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5421 | Src = S.ImpCastExprToType(Src.take(), ET, CK_IntegralComplexToReal); |
| John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5422 | return CK_IntegralCast; |
| 5423 | } |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5424 | case Type::STK_Bool: |
| John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5425 | return CK_IntegralComplexToBoolean; |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5426 | case Type::STK_Floating: |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5427 | Src = S.ImpCastExprToType(Src.take(), SrcTy->getAs<ComplexType>()->getElementType(), |
| 5428 | CK_IntegralComplexToReal); |
| John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5429 | return CK_IntegralToFloating; |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5430 | case Type::STK_Pointer: |
| 5431 | llvm_unreachable("valid complex int->pointer cast?"); |
| 5432 | case Type::STK_MemberPointer: |
| 5433 | llvm_unreachable("member pointer type in C"); |
| John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5434 | } |
| 5435 | break; |
| Anders Carlsson | 094c459 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 5436 | } |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5437 | |
| John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5438 | llvm_unreachable("Unhandled scalar cast"); |
| 5439 | return CK_BitCast; |
| Anders Carlsson | 094c459 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 5440 | } |
| 5441 | |
| Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 5442 | /// CheckCastTypes - Check type constraints for casting between types. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5443 | ExprResult Sema::CheckCastTypes(SourceRange TyR, QualType castType, |
| 5444 | Expr *castExpr, CastKind& Kind, ExprValueKind &VK, |
| 5445 | CXXCastPath &BasePath, bool FunctionalStyle) { |
| John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 5446 | if (castExpr->getType() == Context.UnknownAnyTy) |
| 5447 | return checkUnknownAnyCast(TyR, castType, castExpr, Kind, VK, BasePath); |
| 5448 | |
| Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 5449 | if (getLangOptions().CPlusPlus) |
| Douglas Gregor | 15417cf | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 5450 | return CXXCheckCStyleCast(SourceRange(TyR.getBegin(), |
| 5451 | castExpr->getLocEnd()), |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5452 | castType, VK, castExpr, Kind, BasePath, |
| Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 5453 | FunctionalStyle); |
| Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 5454 | |
| John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 5455 | assert(!castExpr->getType()->isPlaceholderType()); |
| 5456 | |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5457 | // We only support r-value casts in C. |
| 5458 | VK = VK_RValue; |
| 5459 | |
| Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 5460 | // C99 6.5.4p2: the cast type needs to be void or scalar and the expression |
| 5461 | // type needs to be scalar. |
| 5462 | if (castType->isVoidType()) { |
| John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 5463 | // We don't necessarily do lvalue-to-rvalue conversions on this. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5464 | ExprResult castExprRes = IgnoredValueConversions(castExpr); |
| 5465 | if (castExprRes.isInvalid()) |
| 5466 | return ExprError(); |
| 5467 | castExpr = castExprRes.take(); |
| John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 5468 | |
| Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 5469 | // Cast to void allows any expr type. |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5470 | Kind = CK_ToVoid; |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5471 | return Owned(castExpr); |
| Anders Carlsson | ef918ac | 2009-10-16 02:35:04 +0000 | [diff] [blame] | 5472 | } |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5473 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5474 | ExprResult castExprRes = DefaultFunctionArrayLvalueConversion(castExpr); |
| 5475 | if (castExprRes.isInvalid()) |
| 5476 | return ExprError(); |
| 5477 | castExpr = castExprRes.take(); |
| John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 5478 | |
| Eli Friedman | e98194d | 2010-07-17 20:43:49 +0000 | [diff] [blame] | 5479 | if (RequireCompleteType(TyR.getBegin(), castType, |
| 5480 | diag::err_typecheck_cast_to_incomplete)) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5481 | return ExprError(); |
| Eli Friedman | e98194d | 2010-07-17 20:43:49 +0000 | [diff] [blame] | 5482 | |
| Anders Carlsson | ef918ac | 2009-10-16 02:35:04 +0000 | [diff] [blame] | 5483 | if (!castType->isScalarType() && !castType->isVectorType()) { |
| Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 5484 | if (Context.hasSameUnqualifiedType(castType, castExpr->getType()) && |
| Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 5485 | (castType->isStructureType() || castType->isUnionType())) { |
| 5486 | // GCC struct/union extension: allow cast to self. |
| Eli Friedman | ba961a9 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 5487 | // FIXME: Check that the cast destination type is complete. |
| Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 5488 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_nonscalar) |
| 5489 | << castType << castExpr->getSourceRange(); |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5490 | Kind = CK_NoOp; |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5491 | return Owned(castExpr); |
| Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5492 | } |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5493 | |
| Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5494 | if (castType->isUnionType()) { |
| Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 5495 | // GCC cast to union extension |
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5496 | RecordDecl *RD = castType->getAs<RecordType>()->getDecl(); |
| Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 5497 | RecordDecl::field_iterator Field, FieldEnd; |
| Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5498 | for (Field = RD->field_begin(), FieldEnd = RD->field_end(); |
| Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 5499 | Field != FieldEnd; ++Field) { |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5500 | if (Context.hasSameUnqualifiedType(Field->getType(), |
| Abramo Bagnara | 5d3e724 | 2010-10-07 21:20:44 +0000 | [diff] [blame] | 5501 | castExpr->getType()) && |
| 5502 | !Field->isUnnamedBitfield()) { |
| Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 5503 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_to_union) |
| 5504 | << castExpr->getSourceRange(); |
| 5505 | break; |
| 5506 | } |
| 5507 | } |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5508 | if (Field == FieldEnd) { |
| 5509 | Diag(TyR.getBegin(), diag::err_typecheck_cast_to_union_no_type) |
| Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 5510 | << castExpr->getType() << castExpr->getSourceRange(); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5511 | return ExprError(); |
| 5512 | } |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5513 | Kind = CK_ToUnion; |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5514 | return Owned(castExpr); |
| Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 5515 | } |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5516 | |
| Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5517 | // Reject any other conversions to non-scalar types. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5518 | Diag(TyR.getBegin(), diag::err_typecheck_cond_expect_scalar) |
| Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5519 | << castType << castExpr->getSourceRange(); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5520 | return ExprError(); |
| Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5521 | } |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5522 | |
| John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5523 | // The type we're casting to is known to be a scalar or vector. |
| 5524 | |
| 5525 | // Require the operand to be a scalar or vector. |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5526 | if (!castExpr->getType()->isScalarType() && |
| Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5527 | !castExpr->getType()->isVectorType()) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5528 | Diag(castExpr->getLocStart(), |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5529 | diag::err_typecheck_expect_scalar_operand) |
| Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5530 | << castExpr->getType() << castExpr->getSourceRange(); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5531 | return ExprError(); |
| Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5532 | } |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5533 | |
| 5534 | if (castType->isExtVectorType()) |
| Anders Carlsson | 43d70f8 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 5535 | return CheckExtVectorCast(TyR, castType, castExpr, Kind); |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5536 | |
| Anton Yartsev | 28ccef7 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5537 | if (castType->isVectorType()) { |
| 5538 | if (castType->getAs<VectorType>()->getVectorKind() == |
| 5539 | VectorType::AltiVecVector && |
| 5540 | (castExpr->getType()->isIntegerType() || |
| 5541 | castExpr->getType()->isFloatingType())) { |
| 5542 | Kind = CK_VectorSplat; |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5543 | return Owned(castExpr); |
| 5544 | } else if (CheckVectorCast(TyR, castType, castExpr->getType(), Kind)) { |
| 5545 | return ExprError(); |
| Anton Yartsev | 28ccef7 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5546 | } else |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5547 | return Owned(castExpr); |
| Anton Yartsev | 28ccef7 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5548 | } |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5549 | if (castExpr->getType()->isVectorType()) { |
| 5550 | if (CheckVectorCast(TyR, castExpr->getType(), castType, Kind)) |
| 5551 | return ExprError(); |
| 5552 | else |
| 5553 | return Owned(castExpr); |
| 5554 | } |
| Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5555 | |
| John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5556 | // The source and target types are both scalars, i.e. |
| 5557 | // - arithmetic types (fundamental, enum, and complex) |
| 5558 | // - all kinds of pointers |
| 5559 | // Note that member pointers were filtered out with C++, above. |
| 5560 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5561 | if (isa<ObjCSelectorExpr>(castExpr)) { |
| 5562 | Diag(castExpr->getLocStart(), diag::err_cast_selector_expr); |
| 5563 | return ExprError(); |
| 5564 | } |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5565 | |
| John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5566 | // If either type is a pointer, the other type has to be either an |
| 5567 | // integer or a pointer. |
| Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5568 | if (!castType->isArithmeticType()) { |
| Eli Friedman | f4e3ad6 | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 5569 | QualType castExprType = castExpr->getType(); |
| Douglas Gregor | 6972a62 | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 5570 | if (!castExprType->isIntegralType(Context) && |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5571 | castExprType->isArithmeticType()) { |
| 5572 | Diag(castExpr->getLocStart(), |
| 5573 | diag::err_cast_pointer_from_non_pointer_int) |
| Eli Friedman | f4e3ad6 | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 5574 | << castExprType << castExpr->getSourceRange(); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5575 | return ExprError(); |
| 5576 | } |
| Eli Friedman | f4e3ad6 | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 5577 | } else if (!castExpr->getType()->isArithmeticType()) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5578 | if (!castType->isIntegralType(Context) && castType->isArithmeticType()) { |
| 5579 | Diag(castExpr->getLocStart(), diag::err_cast_pointer_to_non_pointer_int) |
| Eli Friedman | f4e3ad6 | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 5580 | << castType << castExpr->getSourceRange(); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5581 | return ExprError(); |
| 5582 | } |
| Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 5583 | } |
| Anders Carlsson | 094c459 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 5584 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5585 | castExprRes = Owned(castExpr); |
| 5586 | Kind = PrepareScalarCast(*this, castExprRes, castType); |
| 5587 | if (castExprRes.isInvalid()) |
| 5588 | return ExprError(); |
| 5589 | castExpr = castExprRes.take(); |
| John McCall | 2b5c1b2 | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 5590 | |
| John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5591 | if (Kind == CK_BitCast) |
| John McCall | 2b5c1b2 | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 5592 | CheckCastAlign(castExpr, castType, TyR); |
| 5593 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5594 | return Owned(castExpr); |
| Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 5595 | } |
| 5596 | |
| Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5597 | bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty, |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5598 | CastKind &Kind) { |
| Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 5599 | assert(VectorTy->isVectorType() && "Not a vector type!"); |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5600 | |
| Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 5601 | if (Ty->isVectorType() || Ty->isIntegerType()) { |
| Chris Lattner | 37e0587 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 5602 | if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty)) |
| Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 5603 | return Diag(R.getBegin(), |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5604 | Ty->isVectorType() ? |
| Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 5605 | diag::err_invalid_conversion_between_vectors : |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5606 | diag::err_invalid_conversion_between_vector_and_integer) |
| Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5607 | << VectorTy << Ty << R; |
| Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 5608 | } else |
| 5609 | return Diag(R.getBegin(), |
| Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5610 | diag::err_invalid_conversion_between_vector_and_scalar) |
| Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5611 | << VectorTy << Ty << R; |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5612 | |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5613 | Kind = CK_BitCast; |
| Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 5614 | return false; |
| 5615 | } |
| 5616 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5617 | ExprResult Sema::CheckExtVectorCast(SourceRange R, QualType DestTy, |
| 5618 | Expr *CastExpr, CastKind &Kind) { |
| Nate Begeman | c69b740 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 5619 | assert(DestTy->isExtVectorType() && "Not an extended vector type!"); |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5620 | |
| Anders Carlsson | 43d70f8 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 5621 | QualType SrcTy = CastExpr->getType(); |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5622 | |
| Nate Begeman | c8961a4 | 2009-06-27 22:05:55 +0000 | [diff] [blame] | 5623 | // If SrcTy is a VectorType, the total size must match to explicitly cast to |
| 5624 | // an ExtVectorType. |
| Nate Begeman | c69b740 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 5625 | if (SrcTy->isVectorType()) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5626 | if (Context.getTypeSize(DestTy) != Context.getTypeSize(SrcTy)) { |
| 5627 | Diag(R.getBegin(),diag::err_invalid_conversion_between_ext_vectors) |
| Nate Begeman | c69b740 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 5628 | << DestTy << SrcTy << R; |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5629 | return ExprError(); |
| 5630 | } |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5631 | Kind = CK_BitCast; |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5632 | return Owned(CastExpr); |
| Nate Begeman | c69b740 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 5633 | } |
| 5634 | |
| Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5635 | // All non-pointer scalars can be cast to ExtVector type. The appropriate |
| Nate Begeman | c69b740 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 5636 | // conversion will take place first from scalar to elt type, and then |
| 5637 | // splat from elt type to vector. |
| Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5638 | if (SrcTy->isPointerType()) |
| 5639 | return Diag(R.getBegin(), |
| 5640 | diag::err_invalid_conversion_between_vector_and_scalar) |
| 5641 | << DestTy << SrcTy << R; |
| Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5642 | |
| 5643 | QualType DestElemTy = DestTy->getAs<ExtVectorType>()->getElementType(); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5644 | ExprResult CastExprRes = Owned(CastExpr); |
| 5645 | CastKind CK = PrepareScalarCast(*this, CastExprRes, DestElemTy); |
| 5646 | if (CastExprRes.isInvalid()) |
| 5647 | return ExprError(); |
| 5648 | CastExpr = ImpCastExprToType(CastExprRes.take(), DestElemTy, CK).take(); |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5649 | |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5650 | Kind = CK_VectorSplat; |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5651 | return Owned(CastExpr); |
| Nate Begeman | c69b740 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 5652 | } |
| 5653 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5654 | ExprResult |
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5655 | Sema::ActOnCastExpr(Scope *S, SourceLocation LParenLoc, ParsedType Ty, |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5656 | SourceLocation RParenLoc, Expr *castExpr) { |
| 5657 | assert((Ty != 0) && (castExpr != 0) && |
| Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5658 | "ActOnCastExpr(): missing type or expr"); |
| Steve Naroff | 1a2cf6b | 2007-07-16 23:25:18 +0000 | [diff] [blame] | 5659 | |
| John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 5660 | TypeSourceInfo *castTInfo; |
| 5661 | QualType castType = GetTypeFromParser(Ty, &castTInfo); |
| 5662 | if (!castTInfo) |
| John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5663 | castTInfo = Context.getTrivialTypeSourceInfo(castType); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5664 | |
| Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5665 | // If the Expr being casted is a ParenListExpr, handle it specially. |
| 5666 | if (isa<ParenListExpr>(castExpr)) |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5667 | return ActOnCastOfParenListExpr(S, LParenLoc, RParenLoc, castExpr, |
| John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5668 | castTInfo); |
| John McCall | ebe5474 | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 5669 | |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5670 | return BuildCStyleCastExpr(LParenLoc, castTInfo, RParenLoc, castExpr); |
| John McCall | ebe5474 | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 5671 | } |
| 5672 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5673 | ExprResult |
| John McCall | ebe5474 | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 5674 | Sema::BuildCStyleCastExpr(SourceLocation LParenLoc, TypeSourceInfo *Ty, |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5675 | SourceLocation RParenLoc, Expr *castExpr) { |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5676 | CastKind Kind = CK_Invalid; |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5677 | ExprValueKind VK = VK_RValue; |
| John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5678 | CXXCastPath BasePath; |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5679 | ExprResult CastResult = |
| 5680 | CheckCastTypes(SourceRange(LParenLoc, RParenLoc), Ty->getType(), castExpr, |
| 5681 | Kind, VK, BasePath); |
| 5682 | if (CastResult.isInvalid()) |
| Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5683 | return ExprError(); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5684 | castExpr = CastResult.take(); |
| Anders Carlsson | e9766d5 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 5685 | |
| John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5686 | return Owned(CStyleCastExpr::Create(Context, |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5687 | Ty->getType().getNonLValueExprType(Context), |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5688 | VK, Kind, castExpr, &BasePath, Ty, |
| John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5689 | LParenLoc, RParenLoc)); |
| Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 5690 | } |
| 5691 | |
| Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5692 | /// This is not an AltiVec-style cast, so turn the ParenListExpr into a sequence |
| 5693 | /// of comma binary operators. |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5694 | ExprResult |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5695 | Sema::MaybeConvertParenListExprToParenExpr(Scope *S, Expr *expr) { |
| Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5696 | ParenListExpr *E = dyn_cast<ParenListExpr>(expr); |
| 5697 | if (!E) |
| 5698 | return Owned(expr); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5699 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5700 | ExprResult Result(E->getExpr(0)); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5701 | |
| Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5702 | for (unsigned i = 1, e = E->getNumExprs(); i != e && !Result.isInvalid(); ++i) |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5703 | Result = ActOnBinOp(S, E->getExprLoc(), tok::comma, Result.get(), |
| 5704 | E->getExpr(i)); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5705 | |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5706 | if (Result.isInvalid()) return ExprError(); |
| 5707 | |
| 5708 | return ActOnParenExpr(E->getLParenLoc(), E->getRParenLoc(), Result.get()); |
| Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5709 | } |
| 5710 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5711 | ExprResult |
| Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5712 | Sema::ActOnCastOfParenListExpr(Scope *S, SourceLocation LParenLoc, |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5713 | SourceLocation RParenLoc, Expr *Op, |
| John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5714 | TypeSourceInfo *TInfo) { |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5715 | ParenListExpr *PE = cast<ParenListExpr>(Op); |
| John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5716 | QualType Ty = TInfo->getType(); |
| Anton Yartsev | 28ccef7 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5717 | bool isVectorLiteral = false; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5718 | |
| Anton Yartsev | 28ccef7 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5719 | // Check for an altivec or OpenCL literal, |
| John Thompson | 781ad17 | 2010-06-30 22:55:51 +0000 | [diff] [blame] | 5720 | // i.e. all the elements are integer constants. |
| Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5721 | if (getLangOptions().AltiVec && Ty->isVectorType()) { |
| 5722 | if (PE->getNumExprs() == 0) { |
| 5723 | Diag(PE->getExprLoc(), diag::err_altivec_empty_initializer); |
| 5724 | return ExprError(); |
| 5725 | } |
| John Thompson | 781ad17 | 2010-06-30 22:55:51 +0000 | [diff] [blame] | 5726 | if (PE->getNumExprs() == 1) { |
| 5727 | if (!PE->getExpr(0)->getType()->isVectorType()) |
| Anton Yartsev | 28ccef7 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5728 | isVectorLiteral = true; |
| John Thompson | 781ad17 | 2010-06-30 22:55:51 +0000 | [diff] [blame] | 5729 | } |
| 5730 | else |
| Anton Yartsev | 28ccef7 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5731 | isVectorLiteral = true; |
| John Thompson | 781ad17 | 2010-06-30 22:55:51 +0000 | [diff] [blame] | 5732 | } |
| Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5733 | |
| Anton Yartsev | 28ccef7 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5734 | // If this is a vector initializer, '(' type ')' '(' init, ..., init ')' |
| John Thompson | 781ad17 | 2010-06-30 22:55:51 +0000 | [diff] [blame] | 5735 | // then handle it as such. |
| Anton Yartsev | 28ccef7 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5736 | if (isVectorLiteral) { |
| Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5737 | llvm::SmallVector<Expr *, 8> initExprs; |
| Anton Yartsev | 28ccef7 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5738 | // '(...)' form of vector initialization in AltiVec: the number of |
| 5739 | // initializers must be one or must match the size of the vector. |
| 5740 | // If a single value is specified in the initializer then it will be |
| 5741 | // replicated to all the components of the vector |
| 5742 | if (Ty->getAs<VectorType>()->getVectorKind() == |
| 5743 | VectorType::AltiVecVector) { |
| 5744 | unsigned numElems = Ty->getAs<VectorType>()->getNumElements(); |
| 5745 | // The number of initializers must be one or must match the size of the |
| 5746 | // vector. If a single value is specified in the initializer then it will |
| 5747 | // be replicated to all the components of the vector |
| 5748 | if (PE->getNumExprs() == 1) { |
| 5749 | QualType ElemTy = Ty->getAs<VectorType>()->getElementType(); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5750 | ExprResult Literal = Owned(PE->getExpr(0)); |
| 5751 | Literal = ImpCastExprToType(Literal.take(), ElemTy, |
| 5752 | PrepareScalarCast(*this, Literal, ElemTy)); |
| 5753 | return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Literal.take()); |
| Anton Yartsev | 28ccef7 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5754 | } |
| 5755 | else if (PE->getNumExprs() < numElems) { |
| 5756 | Diag(PE->getExprLoc(), |
| 5757 | diag::err_incorrect_number_of_vector_initializers); |
| 5758 | return ExprError(); |
| 5759 | } |
| 5760 | else |
| 5761 | for (unsigned i = 0, e = PE->getNumExprs(); i != e; ++i) |
| 5762 | initExprs.push_back(PE->getExpr(i)); |
| 5763 | } |
| 5764 | else |
| 5765 | for (unsigned i = 0, e = PE->getNumExprs(); i != e; ++i) |
| 5766 | initExprs.push_back(PE->getExpr(i)); |
| Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5767 | |
| 5768 | // FIXME: This means that pretty-printing the final AST will produce curly |
| 5769 | // braces instead of the original commas. |
| Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 5770 | InitListExpr *E = new (Context) InitListExpr(Context, LParenLoc, |
| 5771 | &initExprs[0], |
| Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5772 | initExprs.size(), RParenLoc); |
| 5773 | E->setType(Ty); |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5774 | return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, E); |
| Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5775 | } else { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5776 | // This is not an AltiVec-style cast, so turn the ParenListExpr into a |
| Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5777 | // sequence of BinOp comma operators. |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5778 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Op); |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5779 | if (Result.isInvalid()) return ExprError(); |
| 5780 | return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Result.take()); |
| Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5781 | } |
| 5782 | } |
| 5783 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5784 | ExprResult Sema::ActOnParenOrParenListExpr(SourceLocation L, |
| Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5785 | SourceLocation R, |
| Fariborz Jahanian | 906d871 | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 5786 | MultiExprArg Val, |
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5787 | ParsedType TypeOfCast) { |
| Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5788 | unsigned nexprs = Val.size(); |
| 5789 | Expr **exprs = reinterpret_cast<Expr**>(Val.release()); |
| Fariborz Jahanian | 906d871 | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 5790 | assert((exprs != 0) && "ActOnParenOrParenListExpr() missing expr list"); |
| 5791 | Expr *expr; |
| 5792 | if (nexprs == 1 && TypeOfCast && !TypeIsVectorType(TypeOfCast)) |
| 5793 | expr = new (Context) ParenExpr(L, R, exprs[0]); |
| 5794 | else |
| 5795 | expr = new (Context) ParenListExpr(Context, L, exprs, nexprs, R); |
| Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5796 | return Owned(expr); |
| 5797 | } |
| 5798 | |
| Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5799 | /// \brief Emit a specialized diagnostic when one expression is a null pointer |
| 5800 | /// constant and the other is not a pointer. |
| 5801 | bool Sema::DiagnoseConditionalForNull(Expr *LHS, Expr *RHS, |
| 5802 | SourceLocation QuestionLoc) { |
| 5803 | Expr *NullExpr = LHS; |
| 5804 | Expr *NonPointerExpr = RHS; |
| 5805 | Expr::NullPointerConstantKind NullKind = |
| 5806 | NullExpr->isNullPointerConstant(Context, |
| 5807 | Expr::NPC_ValueDependentIsNotNull); |
| 5808 | |
| 5809 | if (NullKind == Expr::NPCK_NotNull) { |
| 5810 | NullExpr = RHS; |
| 5811 | NonPointerExpr = LHS; |
| 5812 | NullKind = |
| 5813 | NullExpr->isNullPointerConstant(Context, |
| 5814 | Expr::NPC_ValueDependentIsNotNull); |
| 5815 | } |
| 5816 | |
| 5817 | if (NullKind == Expr::NPCK_NotNull) |
| 5818 | return false; |
| 5819 | |
| 5820 | if (NullKind == Expr::NPCK_ZeroInteger) { |
| 5821 | // In this case, check to make sure that we got here from a "NULL" |
| 5822 | // string in the source code. |
| 5823 | NullExpr = NullExpr->IgnoreParenImpCasts(); |
| John McCall | 462c055 | 2011-03-08 07:59:04 +0000 | [diff] [blame] | 5824 | SourceLocation loc = NullExpr->getExprLoc(); |
| 5825 | if (!findMacroSpelling(loc, "NULL")) |
| Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5826 | return false; |
| 5827 | } |
| 5828 | |
| 5829 | int DiagType = (NullKind == Expr::NPCK_CXX0X_nullptr); |
| 5830 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands_null) |
| 5831 | << NonPointerExpr->getType() << DiagType |
| 5832 | << NonPointerExpr->getSourceRange(); |
| 5833 | return true; |
| 5834 | } |
| 5835 | |
| Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5836 | /// Note that lhs is not null here, even if this is the gnu "x ?: y" extension. |
| 5837 | /// In that case, lhs = cond. |
| Chris Lattner | 2c48660 | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 5838 | /// C99 6.5.15 |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5839 | QualType Sema::CheckConditionalOperands(ExprResult &Cond, ExprResult &LHS, ExprResult &RHS, |
| John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5840 | ExprValueKind &VK, ExprObjectKind &OK, |
| Chris Lattner | 2c48660 | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 5841 | SourceLocation QuestionLoc) { |
| Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 5842 | |
| John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 5843 | ExprResult lhsResult = CheckPlaceholderExpr(LHS.get()); |
| John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 5844 | if (!lhsResult.isUsable()) return QualType(); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5845 | LHS = move(lhsResult); |
| Douglas Gregor | 0124e9b | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 5846 | |
| John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 5847 | ExprResult rhsResult = CheckPlaceholderExpr(RHS.get()); |
| John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 5848 | if (!rhsResult.isUsable()) return QualType(); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5849 | RHS = move(rhsResult); |
| Douglas Gregor | 0124e9b | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 5850 | |
| Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5851 | // C++ is sufficiently different to merit its own checker. |
| 5852 | if (getLangOptions().CPlusPlus) |
| John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5853 | return CXXCheckConditionalOperands(Cond, LHS, RHS, VK, OK, QuestionLoc); |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5854 | |
| 5855 | VK = VK_RValue; |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 5856 | OK = OK_Ordinary; |
| Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5857 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5858 | Cond = UsualUnaryConversions(Cond.take()); |
| 5859 | if (Cond.isInvalid()) |
| 5860 | return QualType(); |
| 5861 | LHS = UsualUnaryConversions(LHS.take()); |
| 5862 | if (LHS.isInvalid()) |
| 5863 | return QualType(); |
| 5864 | RHS = UsualUnaryConversions(RHS.take()); |
| 5865 | if (RHS.isInvalid()) |
| 5866 | return QualType(); |
| 5867 | |
| 5868 | QualType CondTy = Cond.get()->getType(); |
| 5869 | QualType LHSTy = LHS.get()->getType(); |
| 5870 | QualType RHSTy = RHS.get()->getType(); |
| Steve Naroff | 3109001 | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 5871 | |
| Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 5872 | // first, check the condition. |
| Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5873 | if (!CondTy->isScalarType()) { // C99 6.5.15p2 |
| Nate Begeman | abb5a73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 5874 | // OpenCL: Sec 6.3.i says the condition is allowed to be a vector or scalar. |
| 5875 | // Throw an error if its not either. |
| 5876 | if (getLangOptions().OpenCL) { |
| 5877 | if (!CondTy->isVectorType()) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5878 | Diag(Cond.get()->getLocStart(), |
| Nate Begeman | abb5a73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 5879 | diag::err_typecheck_cond_expect_scalar_or_vector) |
| 5880 | << CondTy; |
| 5881 | return QualType(); |
| 5882 | } |
| 5883 | } |
| 5884 | else { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5885 | Diag(Cond.get()->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
| Nate Begeman | abb5a73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 5886 | << CondTy; |
| 5887 | return QualType(); |
| 5888 | } |
| Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 5889 | } |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5890 | |
| Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5891 | // Now check the two expressions. |
| Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5892 | if (LHSTy->isVectorType() || RHSTy->isVectorType()) |
| 5893 | return CheckVectorOperands(QuestionLoc, LHS, RHS); |
| Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 5894 | |
| Nate Begeman | abb5a73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 5895 | // OpenCL: If the condition is a vector, and both operands are scalar, |
| 5896 | // attempt to implicity convert them to the vector type to act like the |
| 5897 | // built in select. |
| 5898 | if (getLangOptions().OpenCL && CondTy->isVectorType()) { |
| 5899 | // Both operands should be of scalar type. |
| 5900 | if (!LHSTy->isScalarType()) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5901 | Diag(LHS.get()->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
| Nate Begeman | abb5a73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 5902 | << CondTy; |
| 5903 | return QualType(); |
| 5904 | } |
| 5905 | if (!RHSTy->isScalarType()) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5906 | Diag(RHS.get()->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
| Nate Begeman | abb5a73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 5907 | << CondTy; |
| 5908 | return QualType(); |
| 5909 | } |
| 5910 | // Implicity convert these scalars to the type of the condition. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5911 | LHS = ImpCastExprToType(LHS.take(), CondTy, CK_IntegralCast); |
| 5912 | RHS = ImpCastExprToType(RHS.take(), CondTy, CK_IntegralCast); |
| Nate Begeman | abb5a73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 5913 | } |
| 5914 | |
| Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5915 | // If both operands have arithmetic type, do the usual arithmetic conversions |
| 5916 | // to find a common type: C99 6.5.15p3,5. |
| Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5917 | if (LHSTy->isArithmeticType() && RHSTy->isArithmeticType()) { |
| 5918 | UsualArithmeticConversions(LHS, RHS); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5919 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 5920 | return QualType(); |
| 5921 | return LHS.get()->getType(); |
| Steve Naroff | dbd9e89 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 5922 | } |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5923 | |
| Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5924 | // If both operands are the same structure or union type, the result is that |
| 5925 | // type. |
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5926 | if (const RecordType *LHSRT = LHSTy->getAs<RecordType>()) { // C99 6.5.15p3 |
| 5927 | if (const RecordType *RHSRT = RHSTy->getAs<RecordType>()) |
| Chris Lattner | 2ab40a6 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 5928 | if (LHSRT->getDecl() == RHSRT->getDecl()) |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5929 | // "If both the operands have structure or union type, the result has |
| Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5930 | // that type." This implies that CV qualifiers are dropped. |
| Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5931 | return LHSTy.getUnqualifiedType(); |
| Eli Friedman | ba961a9 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 5932 | // FIXME: Type of conditional expression must be complete in C mode. |
| Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 5933 | } |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5934 | |
| Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5935 | // C99 6.5.15p5: "If both operands have void type, the result has void type." |
| Steve Naroff | bf1516c | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 5936 | // The following || allows only one side to be void (a GCC-ism). |
| Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5937 | if (LHSTy->isVoidType() || RHSTy->isVoidType()) { |
| 5938 | if (!LHSTy->isVoidType()) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5939 | Diag(RHS.get()->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 5940 | << RHS.get()->getSourceRange(); |
| Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5941 | if (!RHSTy->isVoidType()) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5942 | Diag(LHS.get()->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 5943 | << LHS.get()->getSourceRange(); |
| 5944 | LHS = ImpCastExprToType(LHS.take(), Context.VoidTy, CK_ToVoid); |
| 5945 | RHS = ImpCastExprToType(RHS.take(), Context.VoidTy, CK_ToVoid); |
| Eli Friedman | 3e1852f | 2008-06-04 19:47:51 +0000 | [diff] [blame] | 5946 | return Context.VoidTy; |
| Steve Naroff | bf1516c | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 5947 | } |
| Steve Naroff | 039ad3c | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 5948 | // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has |
| 5949 | // the type of the other operand." |
| Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 5950 | if ((LHSTy->isAnyPointerType() || LHSTy->isBlockPointerType()) && |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5951 | RHS.get()->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
| Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5952 | // promote the null to a pointer. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5953 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_NullToPointer); |
| Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5954 | return LHSTy; |
| Steve Naroff | 039ad3c | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 5955 | } |
| Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 5956 | if ((RHSTy->isAnyPointerType() || RHSTy->isBlockPointerType()) && |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5957 | LHS.get()->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
| 5958 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_NullToPointer); |
| Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5959 | return RHSTy; |
| Steve Naroff | 039ad3c | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 5960 | } |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5961 | |
| Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5962 | // All objective-c pointer type analysis is done here. |
| 5963 | QualType compositeType = FindCompositeObjCPointerType(LHS, RHS, |
| 5964 | QuestionLoc); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5965 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 5966 | return QualType(); |
| Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5967 | if (!compositeType.isNull()) |
| 5968 | return compositeType; |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5969 | |
| 5970 | |
| Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5971 | // Handle block pointer types. |
| 5972 | if (LHSTy->isBlockPointerType() || RHSTy->isBlockPointerType()) { |
| 5973 | if (!LHSTy->isBlockPointerType() || !RHSTy->isBlockPointerType()) { |
| 5974 | if (LHSTy->isVoidPointerType() || RHSTy->isVoidPointerType()) { |
| 5975 | QualType destType = Context.getPointerType(Context.VoidTy); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5976 | LHS = ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
| 5977 | RHS = ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
| Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5978 | return destType; |
| 5979 | } |
| 5980 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5981 | << LHSTy << RHSTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
| Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5982 | return QualType(); |
| Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5983 | } |
| Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5984 | // We have 2 block pointer types. |
| 5985 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 5986 | // Two identical block pointer types are always compatible. |
| Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5987 | return LHSTy; |
| 5988 | } |
| Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5989 | // The block pointer types aren't identical, continue checking. |
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5990 | QualType lhptee = LHSTy->getAs<BlockPointerType>()->getPointeeType(); |
| 5991 | QualType rhptee = RHSTy->getAs<BlockPointerType>()->getPointeeType(); |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5992 | |
| Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 5993 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 5994 | rhptee.getUnqualifiedType())) { |
| Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5995 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5996 | << LHSTy << RHSTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
| Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 5997 | // In this situation, we assume void* type. No especially good |
| 5998 | // reason, but this is what gcc does, and we do have to pick |
| 5999 | // to get a consistent AST. |
| 6000 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6001 | LHS = ImpCastExprToType(LHS.take(), incompatTy, CK_BitCast); |
| 6002 | RHS = ImpCastExprToType(RHS.take(), incompatTy, CK_BitCast); |
| Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 6003 | return incompatTy; |
| Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 6004 | } |
| Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6005 | // The block pointer types are compatible. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6006 | LHS = ImpCastExprToType(LHS.take(), LHSTy, CK_BitCast); |
| 6007 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
| Steve Naroff | ea4c780 | 2009-04-08 17:05:15 +0000 | [diff] [blame] | 6008 | return LHSTy; |
| 6009 | } |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6010 | |
| Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6011 | // Check constraints for C object pointers types (C99 6.5.15p3,6). |
| 6012 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
| 6013 | // get the "pointed to" types |
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6014 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 6015 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
| Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6016 | |
| 6017 | // ignore qualifiers on void (C99 6.5.15p3, clause 6) |
| 6018 | if (lhptee->isVoidType() && rhptee->isIncompleteOrObjectType()) { |
| 6019 | // Figure out necessary qualifiers (C99 6.5.15p6) |
| John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6020 | QualType destPointee |
| 6021 | = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
| Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6022 | QualType destType = Context.getPointerType(destPointee); |
| Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 6023 | // Add qualifiers if necessary. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6024 | LHS = ImpCastExprToType(LHS.take(), destType, CK_NoOp); |
| Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 6025 | // Promote to void*. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6026 | RHS = ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
| Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6027 | return destType; |
| 6028 | } |
| 6029 | if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) { |
| John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6030 | QualType destPointee |
| 6031 | = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
| Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6032 | QualType destType = Context.getPointerType(destPointee); |
| Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 6033 | // Add qualifiers if necessary. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6034 | RHS = ImpCastExprToType(RHS.take(), destType, CK_NoOp); |
| Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 6035 | // Promote to void*. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6036 | LHS = ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
| Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6037 | return destType; |
| 6038 | } |
| 6039 | |
| 6040 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 6041 | // Two identical pointer types are always compatible. |
| 6042 | return LHSTy; |
| 6043 | } |
| 6044 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 6045 | rhptee.getUnqualifiedType())) { |
| 6046 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6047 | << LHSTy << RHSTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
| Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6048 | // In this situation, we assume void* type. No especially good |
| 6049 | // reason, but this is what gcc does, and we do have to pick |
| 6050 | // to get a consistent AST. |
| 6051 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6052 | LHS = ImpCastExprToType(LHS.take(), incompatTy, CK_BitCast); |
| 6053 | RHS = ImpCastExprToType(RHS.take(), incompatTy, CK_BitCast); |
| Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6054 | return incompatTy; |
| 6055 | } |
| 6056 | // The pointer types are compatible. |
| 6057 | // C99 6.5.15p6: If both operands are pointers to compatible types *or* to |
| 6058 | // differently qualified versions of compatible types, the result type is |
| 6059 | // a pointer to an appropriately qualified version of the *composite* |
| 6060 | // type. |
| 6061 | // FIXME: Need to calculate the composite type. |
| 6062 | // FIXME: Need to add qualifiers |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6063 | LHS = ImpCastExprToType(LHS.take(), LHSTy, CK_BitCast); |
| 6064 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
| Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6065 | return LHSTy; |
| 6066 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6067 | |
| John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 6068 | // GCC compatibility: soften pointer/integer mismatch. Note that |
| 6069 | // null pointers have been filtered out by this point. |
| Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6070 | if (RHSTy->isPointerType() && LHSTy->isIntegerType()) { |
| 6071 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6072 | << LHSTy << RHSTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
| 6073 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_IntegralToPointer); |
| Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6074 | return RHSTy; |
| 6075 | } |
| 6076 | if (LHSTy->isPointerType() && RHSTy->isIntegerType()) { |
| 6077 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6078 | << LHSTy << RHSTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
| 6079 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_IntegralToPointer); |
| Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6080 | return LHSTy; |
| 6081 | } |
| Daniel Dunbar | 484603b | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 6082 | |
| Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 6083 | // Emit a better diagnostic if one of the expressions is a null pointer |
| 6084 | // constant and the other is not a pointer type. In this case, the user most |
| 6085 | // likely forgot to take the address of the other expression. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6086 | if (DiagnoseConditionalForNull(LHS.get(), RHS.get(), QuestionLoc)) |
| Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 6087 | return QualType(); |
| 6088 | |
| Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 6089 | // Otherwise, the operands are not compatible. |
| Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 6090 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6091 | << LHSTy << RHSTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
| Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 6092 | return QualType(); |
| Steve Naroff | f8a28c5 | 2007-05-15 20:29:32 +0000 | [diff] [blame] | 6093 | } |
| 6094 | |
| Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6095 | /// FindCompositeObjCPointerType - Helper method to find composite type of |
| 6096 | /// two objective-c pointer types of the two input expressions. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6097 | QualType Sema::FindCompositeObjCPointerType(ExprResult &LHS, ExprResult &RHS, |
| Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6098 | SourceLocation QuestionLoc) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6099 | QualType LHSTy = LHS.get()->getType(); |
| 6100 | QualType RHSTy = RHS.get()->getType(); |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6101 | |
| Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6102 | // Handle things like Class and struct objc_class*. Here we case the result |
| 6103 | // to the pseudo-builtin, because that will be implicitly cast back to the |
| 6104 | // redefinition type if an attempt is made to access its fields. |
| 6105 | if (LHSTy->isObjCClassType() && |
| John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 6106 | (Context.hasSameType(RHSTy, Context.ObjCClassRedefinitionType))) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6107 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
| Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6108 | return LHSTy; |
| 6109 | } |
| 6110 | if (RHSTy->isObjCClassType() && |
| John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 6111 | (Context.hasSameType(LHSTy, Context.ObjCClassRedefinitionType))) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6112 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_BitCast); |
| Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6113 | return RHSTy; |
| 6114 | } |
| 6115 | // And the same for struct objc_object* / id |
| 6116 | if (LHSTy->isObjCIdType() && |
| John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 6117 | (Context.hasSameType(RHSTy, Context.ObjCIdRedefinitionType))) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6118 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
| Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6119 | return LHSTy; |
| 6120 | } |
| 6121 | if (RHSTy->isObjCIdType() && |
| John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 6122 | (Context.hasSameType(LHSTy, Context.ObjCIdRedefinitionType))) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6123 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_BitCast); |
| Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6124 | return RHSTy; |
| 6125 | } |
| 6126 | // And the same for struct objc_selector* / SEL |
| 6127 | if (Context.isObjCSelType(LHSTy) && |
| John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 6128 | (Context.hasSameType(RHSTy, Context.ObjCSelRedefinitionType))) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6129 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
| Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6130 | return LHSTy; |
| 6131 | } |
| 6132 | if (Context.isObjCSelType(RHSTy) && |
| John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 6133 | (Context.hasSameType(LHSTy, Context.ObjCSelRedefinitionType))) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6134 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_BitCast); |
| Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6135 | return RHSTy; |
| 6136 | } |
| 6137 | // Check constraints for Objective-C object pointers types. |
| 6138 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isObjCObjectPointerType()) { |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6139 | |
| Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6140 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 6141 | // Two identical object pointer types are always compatible. |
| 6142 | return LHSTy; |
| 6143 | } |
| 6144 | const ObjCObjectPointerType *LHSOPT = LHSTy->getAs<ObjCObjectPointerType>(); |
| 6145 | const ObjCObjectPointerType *RHSOPT = RHSTy->getAs<ObjCObjectPointerType>(); |
| 6146 | QualType compositeType = LHSTy; |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6147 | |
| Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6148 | // If both operands are interfaces and either operand can be |
| 6149 | // assigned to the other, use that type as the composite |
| 6150 | // type. This allows |
| 6151 | // xxx ? (A*) a : (B*) b |
| 6152 | // where B is a subclass of A. |
| 6153 | // |
| 6154 | // Additionally, as for assignment, if either type is 'id' |
| 6155 | // allow silent coercion. Finally, if the types are |
| 6156 | // incompatible then make sure to use 'id' as the composite |
| 6157 | // type so the result is acceptable for sending messages to. |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6158 | |
| Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6159 | // FIXME: Consider unifying with 'areComparableObjCPointerTypes'. |
| 6160 | // It could return the composite type. |
| 6161 | if (Context.canAssignObjCInterfaces(LHSOPT, RHSOPT)) { |
| 6162 | compositeType = RHSOPT->isObjCBuiltinType() ? RHSTy : LHSTy; |
| 6163 | } else if (Context.canAssignObjCInterfaces(RHSOPT, LHSOPT)) { |
| 6164 | compositeType = LHSOPT->isObjCBuiltinType() ? LHSTy : RHSTy; |
| 6165 | } else if ((LHSTy->isObjCQualifiedIdType() || |
| 6166 | RHSTy->isObjCQualifiedIdType()) && |
| 6167 | Context.ObjCQualifiedIdTypesAreCompatible(LHSTy, RHSTy, true)) { |
| 6168 | // Need to handle "id<xx>" explicitly. |
| 6169 | // GCC allows qualified id and any Objective-C type to devolve to |
| 6170 | // id. Currently localizing to here until clear this should be |
| 6171 | // part of ObjCQualifiedIdTypesAreCompatible. |
| 6172 | compositeType = Context.getObjCIdType(); |
| 6173 | } else if (LHSTy->isObjCIdType() || RHSTy->isObjCIdType()) { |
| 6174 | compositeType = Context.getObjCIdType(); |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6175 | } else if (!(compositeType = |
| Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6176 | Context.areCommonBaseCompatible(LHSOPT, RHSOPT)).isNull()) |
| 6177 | ; |
| 6178 | else { |
| 6179 | Diag(QuestionLoc, diag::ext_typecheck_cond_incompatible_operands) |
| 6180 | << LHSTy << RHSTy |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6181 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
| Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6182 | QualType incompatTy = Context.getObjCIdType(); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6183 | LHS = ImpCastExprToType(LHS.take(), incompatTy, CK_BitCast); |
| 6184 | RHS = ImpCastExprToType(RHS.take(), incompatTy, CK_BitCast); |
| Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6185 | return incompatTy; |
| 6186 | } |
| 6187 | // The object pointer types are compatible. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6188 | LHS = ImpCastExprToType(LHS.take(), compositeType, CK_BitCast); |
| 6189 | RHS = ImpCastExprToType(RHS.take(), compositeType, CK_BitCast); |
| Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6190 | return compositeType; |
| 6191 | } |
| 6192 | // Check Objective-C object pointer types and 'void *' |
| 6193 | if (LHSTy->isVoidPointerType() && RHSTy->isObjCObjectPointerType()) { |
| 6194 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 6195 | QualType rhptee = RHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 6196 | QualType destPointee |
| 6197 | = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
| 6198 | QualType destType = Context.getPointerType(destPointee); |
| 6199 | // Add qualifiers if necessary. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6200 | LHS = ImpCastExprToType(LHS.take(), destType, CK_NoOp); |
| Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6201 | // Promote to void*. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6202 | RHS = ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
| Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6203 | return destType; |
| 6204 | } |
| 6205 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isVoidPointerType()) { |
| 6206 | QualType lhptee = LHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 6207 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
| 6208 | QualType destPointee |
| 6209 | = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
| 6210 | QualType destType = Context.getPointerType(destPointee); |
| 6211 | // Add qualifiers if necessary. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6212 | RHS = ImpCastExprToType(RHS.take(), destType, CK_NoOp); |
| Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6213 | // Promote to void*. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6214 | LHS = ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
| Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6215 | return destType; |
| 6216 | } |
| 6217 | return QualType(); |
| 6218 | } |
| 6219 | |
| Hans Wennborg | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 6220 | /// SuggestParentheses - Emit a diagnostic together with a fixit hint that wraps |
| 6221 | /// ParenRange in parentheses. |
| 6222 | static void SuggestParentheses(Sema &Self, SourceLocation Loc, |
| 6223 | const PartialDiagnostic &PD, |
| 6224 | const PartialDiagnostic &FirstNote, |
| 6225 | SourceRange FirstParenRange, |
| 6226 | const PartialDiagnostic &SecondNote, |
| 6227 | SourceRange SecondParenRange) { |
| 6228 | Self.Diag(Loc, PD); |
| 6229 | |
| 6230 | if (!FirstNote.getDiagID()) |
| 6231 | return; |
| 6232 | |
| 6233 | SourceLocation EndLoc = Self.PP.getLocForEndOfToken(FirstParenRange.getEnd()); |
| 6234 | if (!FirstParenRange.getEnd().isFileID() || EndLoc.isInvalid()) { |
| 6235 | // We can't display the parentheses, so just return. |
| 6236 | return; |
| 6237 | } |
| 6238 | |
| 6239 | Self.Diag(Loc, FirstNote) |
| 6240 | << FixItHint::CreateInsertion(FirstParenRange.getBegin(), "(") |
| 6241 | << FixItHint::CreateInsertion(EndLoc, ")"); |
| 6242 | |
| 6243 | if (!SecondNote.getDiagID()) |
| 6244 | return; |
| 6245 | |
| 6246 | EndLoc = Self.PP.getLocForEndOfToken(SecondParenRange.getEnd()); |
| 6247 | if (!SecondParenRange.getEnd().isFileID() || EndLoc.isInvalid()) { |
| 6248 | // We can't display the parentheses, so just dig the |
| 6249 | // warning/error and return. |
| 6250 | Self.Diag(Loc, SecondNote); |
| 6251 | return; |
| 6252 | } |
| 6253 | |
| 6254 | Self.Diag(Loc, SecondNote) |
| 6255 | << FixItHint::CreateInsertion(SecondParenRange.getBegin(), "(") |
| 6256 | << FixItHint::CreateInsertion(EndLoc, ")"); |
| 6257 | } |
| 6258 | |
| 6259 | static bool IsArithmeticOp(BinaryOperatorKind Opc) { |
| 6260 | return Opc >= BO_Mul && Opc <= BO_Shr; |
| 6261 | } |
| 6262 | |
| Hans Wennborg | de2e67e | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 6263 | /// IsArithmeticBinaryExpr - Returns true if E is an arithmetic binary |
| 6264 | /// expression, either using a built-in or overloaded operator, |
| 6265 | /// and sets *OpCode to the opcode and *RHS to the right-hand side expression. |
| 6266 | static bool IsArithmeticBinaryExpr(Expr *E, BinaryOperatorKind *Opcode, |
| 6267 | Expr **RHS) { |
| 6268 | E = E->IgnoreParenImpCasts(); |
| 6269 | E = E->IgnoreConversionOperator(); |
| 6270 | E = E->IgnoreParenImpCasts(); |
| 6271 | |
| 6272 | // Built-in binary operator. |
| 6273 | if (BinaryOperator *OP = dyn_cast<BinaryOperator>(E)) { |
| 6274 | if (IsArithmeticOp(OP->getOpcode())) { |
| 6275 | *Opcode = OP->getOpcode(); |
| 6276 | *RHS = OP->getRHS(); |
| 6277 | return true; |
| 6278 | } |
| 6279 | } |
| 6280 | |
| 6281 | // Overloaded operator. |
| 6282 | if (CXXOperatorCallExpr *Call = dyn_cast<CXXOperatorCallExpr>(E)) { |
| 6283 | if (Call->getNumArgs() != 2) |
| 6284 | return false; |
| 6285 | |
| 6286 | // Make sure this is really a binary operator that is safe to pass into |
| 6287 | // BinaryOperator::getOverloadedOpcode(), e.g. it's not a subscript op. |
| 6288 | OverloadedOperatorKind OO = Call->getOperator(); |
| 6289 | if (OO < OO_Plus || OO > OO_Arrow) |
| 6290 | return false; |
| 6291 | |
| 6292 | BinaryOperatorKind OpKind = BinaryOperator::getOverloadedOpcode(OO); |
| 6293 | if (IsArithmeticOp(OpKind)) { |
| 6294 | *Opcode = OpKind; |
| 6295 | *RHS = Call->getArg(1); |
| 6296 | return true; |
| 6297 | } |
| 6298 | } |
| 6299 | |
| 6300 | return false; |
| 6301 | } |
| 6302 | |
| Hans Wennborg | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 6303 | static bool IsLogicOp(BinaryOperatorKind Opc) { |
| 6304 | return (Opc >= BO_LT && Opc <= BO_NE) || (Opc >= BO_LAnd && Opc <= BO_LOr); |
| 6305 | } |
| 6306 | |
| Hans Wennborg | de2e67e | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 6307 | /// ExprLooksBoolean - Returns true if E looks boolean, i.e. it has boolean type |
| 6308 | /// or is a logical expression such as (x==y) which has int type, but is |
| 6309 | /// commonly interpreted as boolean. |
| 6310 | static bool ExprLooksBoolean(Expr *E) { |
| 6311 | E = E->IgnoreParenImpCasts(); |
| 6312 | |
| 6313 | if (E->getType()->isBooleanType()) |
| 6314 | return true; |
| 6315 | if (BinaryOperator *OP = dyn_cast<BinaryOperator>(E)) |
| 6316 | return IsLogicOp(OP->getOpcode()); |
| 6317 | if (UnaryOperator *OP = dyn_cast<UnaryOperator>(E)) |
| 6318 | return OP->getOpcode() == UO_LNot; |
| 6319 | |
| 6320 | return false; |
| 6321 | } |
| 6322 | |
| Hans Wennborg | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 6323 | /// DiagnoseConditionalPrecedence - Emit a warning when a conditional operator |
| 6324 | /// and binary operator are mixed in a way that suggests the programmer assumed |
| 6325 | /// the conditional operator has higher precedence, for example: |
| 6326 | /// "int x = a + someBinaryCondition ? 1 : 2". |
| 6327 | static void DiagnoseConditionalPrecedence(Sema &Self, |
| 6328 | SourceLocation OpLoc, |
| 6329 | Expr *cond, |
| 6330 | Expr *lhs, |
| 6331 | Expr *rhs) { |
| Hans Wennborg | de2e67e | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 6332 | BinaryOperatorKind CondOpcode; |
| 6333 | Expr *CondRHS; |
| Hans Wennborg | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 6334 | |
| Hans Wennborg | de2e67e | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 6335 | if (!IsArithmeticBinaryExpr(cond, &CondOpcode, &CondRHS)) |
| 6336 | return; |
| 6337 | if (!ExprLooksBoolean(CondRHS)) |
| 6338 | return; |
| Hans Wennborg | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 6339 | |
| Hans Wennborg | de2e67e | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 6340 | // The condition is an arithmetic binary expression, with a right- |
| 6341 | // hand side that looks boolean, so warn. |
| Hans Wennborg | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 6342 | |
| Hans Wennborg | de2e67e | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 6343 | PartialDiagnostic Warn = Self.PDiag(diag::warn_precedence_conditional) |
| 6344 | << cond->getSourceRange() |
| 6345 | << BinaryOperator::getOpcodeStr(CondOpcode); |
| Hans Wennborg | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 6346 | |
| Hans Wennborg | de2e67e | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 6347 | PartialDiagnostic FirstNote = |
| 6348 | Self.PDiag(diag::note_precedence_conditional_silence) |
| 6349 | << BinaryOperator::getOpcodeStr(CondOpcode); |
| Hans Wennborg | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 6350 | |
| Hans Wennborg | de2e67e | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 6351 | SourceRange FirstParenRange(cond->getLocStart(), |
| 6352 | cond->getLocEnd()); |
| Hans Wennborg | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 6353 | |
| Hans Wennborg | de2e67e | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 6354 | PartialDiagnostic SecondNote = |
| 6355 | Self.PDiag(diag::note_precedence_conditional_first); |
| Hans Wennborg | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 6356 | |
| Hans Wennborg | de2e67e | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 6357 | SourceRange SecondParenRange(CondRHS->getLocStart(), |
| 6358 | rhs->getLocEnd()); |
| Hans Wennborg | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 6359 | |
| Hans Wennborg | de2e67e | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 6360 | SuggestParentheses(Self, OpLoc, Warn, FirstNote, FirstParenRange, |
| 6361 | SecondNote, SecondParenRange); |
| Hans Wennborg | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 6362 | } |
| 6363 | |
| Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 6364 | /// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
| Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 6365 | /// in the case of a the GNU conditional expr extension. |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6366 | ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, |
| John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 6367 | SourceLocation ColonLoc, |
| 6368 | Expr *CondExpr, Expr *LHSExpr, |
| 6369 | Expr *RHSExpr) { |
| Chris Lattner | 2ab40a6 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 6370 | // If this is the gnu "x ?: y" extension, analyze the types as though the LHS |
| 6371 | // was the condition. |
| John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 6372 | OpaqueValueExpr *opaqueValue = 0; |
| 6373 | Expr *commonExpr = 0; |
| 6374 | if (LHSExpr == 0) { |
| 6375 | commonExpr = CondExpr; |
| 6376 | |
| 6377 | // We usually want to apply unary conversions *before* saving, except |
| 6378 | // in the special case of a C++ l-value conditional. |
| 6379 | if (!(getLangOptions().CPlusPlus |
| 6380 | && !commonExpr->isTypeDependent() |
| 6381 | && commonExpr->getValueKind() == RHSExpr->getValueKind() |
| 6382 | && commonExpr->isGLValue() |
| 6383 | && commonExpr->isOrdinaryOrBitFieldObject() |
| 6384 | && RHSExpr->isOrdinaryOrBitFieldObject() |
| 6385 | && Context.hasSameType(commonExpr->getType(), RHSExpr->getType()))) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6386 | ExprResult commonRes = UsualUnaryConversions(commonExpr); |
| 6387 | if (commonRes.isInvalid()) |
| 6388 | return ExprError(); |
| 6389 | commonExpr = commonRes.take(); |
| John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 6390 | } |
| 6391 | |
| 6392 | opaqueValue = new (Context) OpaqueValueExpr(commonExpr->getExprLoc(), |
| 6393 | commonExpr->getType(), |
| 6394 | commonExpr->getValueKind(), |
| 6395 | commonExpr->getObjectKind()); |
| 6396 | LHSExpr = CondExpr = opaqueValue; |
| Fariborz Jahanian | c6bf0bd | 2010-08-31 18:02:20 +0000 | [diff] [blame] | 6397 | } |
| Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 6398 | |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6399 | ExprValueKind VK = VK_RValue; |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 6400 | ExprObjectKind OK = OK_Ordinary; |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6401 | ExprResult Cond = Owned(CondExpr), LHS = Owned(LHSExpr), RHS = Owned(RHSExpr); |
| 6402 | QualType result = CheckConditionalOperands(Cond, LHS, RHS, |
| John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 6403 | VK, OK, QuestionLoc); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6404 | if (result.isNull() || Cond.isInvalid() || LHS.isInvalid() || |
| 6405 | RHS.isInvalid()) |
| Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 6406 | return ExprError(); |
| 6407 | |
| Hans Wennborg | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 6408 | DiagnoseConditionalPrecedence(*this, QuestionLoc, Cond.get(), LHS.get(), |
| 6409 | RHS.get()); |
| 6410 | |
| John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 6411 | if (!commonExpr) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6412 | return Owned(new (Context) ConditionalOperator(Cond.take(), QuestionLoc, |
| 6413 | LHS.take(), ColonLoc, |
| 6414 | RHS.take(), result, VK, OK)); |
| John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 6415 | |
| 6416 | return Owned(new (Context) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6417 | BinaryConditionalOperator(commonExpr, opaqueValue, Cond.take(), LHS.take(), |
| 6418 | RHS.take(), QuestionLoc, ColonLoc, result, VK, OK)); |
| Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 6419 | } |
| 6420 | |
| John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6421 | // checkPointerTypesForAssignment - This is a very tricky routine (despite |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6422 | // being closely modeled after the C99 spec:-). The odd characteristic of this |
| Steve Naroff | 3f59729 | 2007-05-11 22:18:03 +0000 | [diff] [blame] | 6423 | // routine is it effectively iqnores the qualifiers on the top level pointee. |
| 6424 | // This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3]. |
| 6425 | // FIXME: add a couple examples in this comment. |
| John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6426 | static Sema::AssignConvertType |
| 6427 | checkPointerTypesForAssignment(Sema &S, QualType lhsType, QualType rhsType) { |
| 6428 | assert(lhsType.isCanonical() && "LHS not canonicalized!"); |
| 6429 | assert(rhsType.isCanonical() && "RHS not canonicalized!"); |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6430 | |
| Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 6431 | // get the "pointed to" type (ignoring qualifiers at the top level) |
| John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6432 | const Type *lhptee, *rhptee; |
| 6433 | Qualifiers lhq, rhq; |
| 6434 | llvm::tie(lhptee, lhq) = cast<PointerType>(lhsType)->getPointeeType().split(); |
| 6435 | llvm::tie(rhptee, rhq) = cast<PointerType>(rhsType)->getPointeeType().split(); |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6436 | |
| John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6437 | Sema::AssignConvertType ConvTy = Sema::Compatible; |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6438 | |
| 6439 | // C99 6.5.16.1p1: This following citation is common to constraints |
| 6440 | // 3 & 4 (below). ...and the type *pointed to* by the left has all the |
| 6441 | // qualifiers of the type *pointed to* by the right; |
| John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6442 | Qualifiers lq; |
| 6443 | |
| 6444 | if (!lhq.compatiblyIncludes(rhq)) { |
| 6445 | // Treat address-space mismatches as fatal. TODO: address subspaces |
| 6446 | if (lhq.getAddressSpace() != rhq.getAddressSpace()) |
| 6447 | ConvTy = Sema::IncompatiblePointerDiscardsQualifiers; |
| 6448 | |
| John McCall | 7853595 | 2011-03-26 02:56:45 +0000 | [diff] [blame] | 6449 | // It's okay to add or remove GC qualifiers when converting to |
| 6450 | // and from void*. |
| 6451 | else if (lhq.withoutObjCGCAttr().compatiblyIncludes(rhq.withoutObjCGCAttr()) |
| 6452 | && (lhptee->isVoidType() || rhptee->isVoidType())) |
| 6453 | ; // keep old |
| 6454 | |
| John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6455 | // For GCC compatibility, other qualifier mismatches are treated |
| 6456 | // as still compatible in C. |
| 6457 | else ConvTy = Sema::CompatiblePointerDiscardsQualifiers; |
| 6458 | } |
| Steve Naroff | 3f59729 | 2007-05-11 22:18:03 +0000 | [diff] [blame] | 6459 | |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6460 | // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or |
| 6461 | // incomplete type and the other is a pointer to a qualified or unqualified |
| Steve Naroff | 3f59729 | 2007-05-11 22:18:03 +0000 | [diff] [blame] | 6462 | // version of void... |
| Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 6463 | if (lhptee->isVoidType()) { |
| Chris Lattner | b3a176d | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 6464 | if (rhptee->isIncompleteOrObjectType()) |
| Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6465 | return ConvTy; |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6466 | |
| Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 6467 | // As an extension, we allow cast to/from void* to function pointer. |
| Chris Lattner | b3a176d | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 6468 | assert(rhptee->isFunctionType()); |
| John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6469 | return Sema::FunctionVoidPointer; |
| Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 6470 | } |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6471 | |
| Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 6472 | if (rhptee->isVoidType()) { |
| Chris Lattner | b3a176d | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 6473 | if (lhptee->isIncompleteOrObjectType()) |
| Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6474 | return ConvTy; |
| Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 6475 | |
| 6476 | // As an extension, we allow cast to/from void* to function pointer. |
| Chris Lattner | b3a176d | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 6477 | assert(lhptee->isFunctionType()); |
| John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6478 | return Sema::FunctionVoidPointer; |
| Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 6479 | } |
| John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6480 | |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6481 | // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or |
| Steve Naroff | 3f59729 | 2007-05-11 22:18:03 +0000 | [diff] [blame] | 6482 | // unqualified versions of compatible types, ... |
| John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6483 | QualType ltrans = QualType(lhptee, 0), rtrans = QualType(rhptee, 0); |
| 6484 | if (!S.Context.typesAreCompatible(ltrans, rtrans)) { |
| Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 6485 | // Check if the pointee types are compatible ignoring the sign. |
| 6486 | // We explicitly check for char so that we catch "char" vs |
| 6487 | // "unsigned char" on systems where "char" is unsigned. |
| Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 6488 | if (lhptee->isCharType()) |
| John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6489 | ltrans = S.Context.UnsignedCharTy; |
| Douglas Gregor | 5cc2c8b | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 6490 | else if (lhptee->hasSignedIntegerRepresentation()) |
| John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6491 | ltrans = S.Context.getCorrespondingUnsignedType(ltrans); |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6492 | |
| Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 6493 | if (rhptee->isCharType()) |
| John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6494 | rtrans = S.Context.UnsignedCharTy; |
| Douglas Gregor | 5cc2c8b | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 6495 | else if (rhptee->hasSignedIntegerRepresentation()) |
| John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6496 | rtrans = S.Context.getCorrespondingUnsignedType(rtrans); |
| Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 6497 | |
| John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6498 | if (ltrans == rtrans) { |
| Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 6499 | // Types are compatible ignoring the sign. Qualifier incompatibility |
| 6500 | // takes priority over sign incompatibility because the sign |
| 6501 | // warning can be disabled. |
| John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6502 | if (ConvTy != Sema::Compatible) |
| Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 6503 | return ConvTy; |
| John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6504 | |
| John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6505 | return Sema::IncompatiblePointerSign; |
| Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 6506 | } |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6507 | |
| Fariborz Jahanian | d7aa9d8 | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 6508 | // If we are a multi-level pointer, it's possible that our issue is simply |
| 6509 | // one of qualification - e.g. char ** -> const char ** is not allowed. If |
| 6510 | // the eventual target type is the same and the pointers have the same |
| 6511 | // level of indirection, this must be the issue. |
| John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6512 | if (isa<PointerType>(lhptee) && isa<PointerType>(rhptee)) { |
| Fariborz Jahanian | d7aa9d8 | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 6513 | do { |
| John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6514 | lhptee = cast<PointerType>(lhptee)->getPointeeType().getTypePtr(); |
| 6515 | rhptee = cast<PointerType>(rhptee)->getPointeeType().getTypePtr(); |
| John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6516 | } while (isa<PointerType>(lhptee) && isa<PointerType>(rhptee)); |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6517 | |
| John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6518 | if (lhptee == rhptee) |
| John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6519 | return Sema::IncompatibleNestedPointerQualifiers; |
| Fariborz Jahanian | d7aa9d8 | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 6520 | } |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6521 | |
| Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 6522 | // General pointer incompatibility takes priority over qualifiers. |
| John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6523 | return Sema::IncompatiblePointer; |
| Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 6524 | } |
| Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6525 | return ConvTy; |
| Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 6526 | } |
| 6527 | |
| John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6528 | /// checkBlockPointerTypesForAssignment - This routine determines whether two |
| Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6529 | /// block pointer types are compatible or whether a block and normal pointer |
| 6530 | /// are compatible. It is more restrict than comparing two function pointer |
| 6531 | // types. |
| John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6532 | static Sema::AssignConvertType |
| 6533 | checkBlockPointerTypesForAssignment(Sema &S, QualType lhsType, |
| 6534 | QualType rhsType) { |
| 6535 | assert(lhsType.isCanonical() && "LHS not canonicalized!"); |
| 6536 | assert(rhsType.isCanonical() && "RHS not canonicalized!"); |
| 6537 | |
| Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6538 | QualType lhptee, rhptee; |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6539 | |
| Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6540 | // get the "pointed to" type (ignoring qualifiers at the top level) |
| John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6541 | lhptee = cast<BlockPointerType>(lhsType)->getPointeeType(); |
| 6542 | rhptee = cast<BlockPointerType>(rhsType)->getPointeeType(); |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6543 | |
| John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6544 | // In C++, the types have to match exactly. |
| 6545 | if (S.getLangOptions().CPlusPlus) |
| 6546 | return Sema::IncompatibleBlockPointer; |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6547 | |
| John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6548 | Sema::AssignConvertType ConvTy = Sema::Compatible; |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6549 | |
| Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6550 | // For blocks we enforce that qualifiers are identical. |
| John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6551 | if (lhptee.getLocalQualifiers() != rhptee.getLocalQualifiers()) |
| 6552 | ConvTy = Sema::CompatiblePointerDiscardsQualifiers; |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6553 | |
| John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6554 | if (!S.Context.typesAreBlockPointerCompatible(lhsType, rhsType)) |
| 6555 | return Sema::IncompatibleBlockPointer; |
| 6556 | |
| Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6557 | return ConvTy; |
| 6558 | } |
| 6559 | |
| John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6560 | /// checkObjCPointerTypesForAssignment - Compares two objective-c pointer types |
| Fariborz Jahanian | 410f2eb | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 6561 | /// for assignment compatibility. |
| John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6562 | static Sema::AssignConvertType |
| 6563 | checkObjCPointerTypesForAssignment(Sema &S, QualType lhsType, QualType rhsType) { |
| 6564 | assert(lhsType.isCanonical() && "LHS was not canonicalized!"); |
| 6565 | assert(rhsType.isCanonical() && "RHS was not canonicalized!"); |
| 6566 | |
| Fariborz Jahanian | d5bb8cb | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 6567 | if (lhsType->isObjCBuiltinType()) { |
| 6568 | // Class is not compatible with ObjC object pointers. |
| Fariborz Jahanian | 9b37b1d | 2010-03-24 21:00:27 +0000 | [diff] [blame] | 6569 | if (lhsType->isObjCClassType() && !rhsType->isObjCBuiltinType() && |
| 6570 | !rhsType->isObjCQualifiedClassType()) |
| John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6571 | return Sema::IncompatiblePointer; |
| 6572 | return Sema::Compatible; |
| Fariborz Jahanian | d5bb8cb | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 6573 | } |
| 6574 | if (rhsType->isObjCBuiltinType()) { |
| 6575 | // Class is not compatible with ObjC object pointers. |
| Fariborz Jahanian | 9b37b1d | 2010-03-24 21:00:27 +0000 | [diff] [blame] | 6576 | if (rhsType->isObjCClassType() && !lhsType->isObjCBuiltinType() && |
| 6577 | !lhsType->isObjCQualifiedClassType()) |
| John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6578 | return Sema::IncompatiblePointer; |
| 6579 | return Sema::Compatible; |
| Fariborz Jahanian | d5bb8cb | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 6580 | } |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6581 | QualType lhptee = |
| Fariborz Jahanian | 410f2eb | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 6582 | lhsType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6583 | QualType rhptee = |
| Fariborz Jahanian | 410f2eb | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 6584 | rhsType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6585 | |
| John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6586 | if (!lhptee.isAtLeastAsQualifiedAs(rhptee)) |
| 6587 | return Sema::CompatiblePointerDiscardsQualifiers; |
| 6588 | |
| 6589 | if (S.Context.typesAreCompatible(lhsType, rhsType)) |
| 6590 | return Sema::Compatible; |
| Fariborz Jahanian | 410f2eb | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 6591 | if (lhsType->isObjCQualifiedIdType() || rhsType->isObjCQualifiedIdType()) |
| John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6592 | return Sema::IncompatibleObjCQualifiedId; |
| 6593 | return Sema::IncompatiblePointer; |
| Fariborz Jahanian | 410f2eb | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 6594 | } |
| 6595 | |
| John McCall | 29600e1 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6596 | Sema::AssignConvertType |
| Douglas Gregor | c03a108 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 6597 | Sema::CheckAssignmentConstraints(SourceLocation Loc, |
| 6598 | QualType lhsType, QualType rhsType) { |
| John McCall | 29600e1 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6599 | // Fake up an opaque expression. We don't actually care about what |
| 6600 | // cast operations are required, so if CheckAssignmentConstraints |
| 6601 | // adds casts to this they'll be wasted, but fortunately that doesn't |
| 6602 | // usually happen on valid code. |
| Douglas Gregor | c03a108 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 6603 | OpaqueValueExpr rhs(Loc, rhsType, VK_RValue); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6604 | ExprResult rhsPtr = &rhs; |
| John McCall | 29600e1 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6605 | CastKind K = CK_Invalid; |
| 6606 | |
| 6607 | return CheckAssignmentConstraints(lhsType, rhsPtr, K); |
| 6608 | } |
| 6609 | |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6610 | /// CheckAssignmentConstraints (C99 6.5.16) - This routine currently |
| 6611 | /// has code to accommodate several GCC extensions when type checking |
| Steve Naroff | 17f76e0 | 2007-05-03 21:03:48 +0000 | [diff] [blame] | 6612 | /// pointers. Here are some objectionable examples that GCC considers warnings: |
| 6613 | /// |
| 6614 | /// int a, *pint; |
| 6615 | /// short *pshort; |
| 6616 | /// struct foo *pfoo; |
| 6617 | /// |
| 6618 | /// pint = pshort; // warning: assignment from incompatible pointer type |
| 6619 | /// a = pint; // warning: assignment makes integer from pointer without a cast |
| 6620 | /// pint = a; // warning: assignment makes pointer from integer without a cast |
| 6621 | /// pint = pfoo; // warning: assignment from incompatible pointer type |
| 6622 | /// |
| 6623 | /// As a result, the code for dealing with pointers is more complex than the |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6624 | /// C99 spec dictates. |
| Steve Naroff | 17f76e0 | 2007-05-03 21:03:48 +0000 | [diff] [blame] | 6625 | /// |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6626 | /// Sets 'Kind' for any result kind except Incompatible. |
| Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6627 | Sema::AssignConvertType |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6628 | Sema::CheckAssignmentConstraints(QualType lhsType, ExprResult &rhs, |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6629 | CastKind &Kind) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6630 | QualType rhsType = rhs.get()->getType(); |
| John McCall | 29600e1 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6631 | |
| Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 6632 | // Get canonical types. We're not formatting these types, just comparing |
| 6633 | // them. |
| Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 6634 | lhsType = Context.getCanonicalType(lhsType).getUnqualifiedType(); |
| 6635 | rhsType = Context.getCanonicalType(rhsType).getUnqualifiedType(); |
| Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 6636 | |
| John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6637 | // Common case: no conversion required. |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6638 | if (lhsType == rhsType) { |
| 6639 | Kind = CK_NoOp; |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6640 | return Compatible; |
| David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 6641 | } |
| 6642 | |
| Douglas Gregor | 6b75484 | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 6643 | // If the left-hand side is a reference type, then we are in a |
| 6644 | // (rare!) case where we've allowed the use of references in C, |
| 6645 | // e.g., as a parameter type in a built-in function. In this case, |
| 6646 | // just make sure that the type referenced is compatible with the |
| 6647 | // right-hand side type. The caller is responsible for adjusting |
| 6648 | // lhsType so that the resulting expression does not have reference |
| 6649 | // type. |
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6650 | if (const ReferenceType *lhsTypeRef = lhsType->getAs<ReferenceType>()) { |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6651 | if (Context.typesAreCompatible(lhsTypeRef->getPointeeType(), rhsType)) { |
| 6652 | Kind = CK_LValueBitCast; |
| Anders Carlsson | 24ebce6 | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 6653 | return Compatible; |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6654 | } |
| Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 6655 | return Incompatible; |
| Fariborz Jahanian | a1e3420 | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 6656 | } |
| John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6657 | |
| Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6658 | // Allow scalar to ExtVector assignments, and assignments of an ExtVector type |
| 6659 | // to the same ExtVector type. |
| 6660 | if (lhsType->isExtVectorType()) { |
| 6661 | if (rhsType->isExtVectorType()) |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6662 | return Incompatible; |
| 6663 | if (rhsType->isArithmeticType()) { |
| John McCall | 29600e1 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6664 | // CK_VectorSplat does T -> vector T, so first cast to the |
| 6665 | // element type. |
| 6666 | QualType elType = cast<ExtVectorType>(lhsType)->getElementType(); |
| 6667 | if (elType != rhsType) { |
| 6668 | Kind = PrepareScalarCast(*this, rhs, elType); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6669 | rhs = ImpCastExprToType(rhs.take(), elType, Kind); |
| John McCall | 29600e1 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6670 | } |
| 6671 | Kind = CK_VectorSplat; |
| Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6672 | return Compatible; |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6673 | } |
| Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6674 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6675 | |
| John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6676 | // Conversions to or from vector type. |
| Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6677 | if (lhsType->isVectorType() || rhsType->isVectorType()) { |
| Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6678 | if (lhsType->isVectorType() && rhsType->isVectorType()) { |
| Bob Wilson | 01856f3 | 2010-12-02 00:25:15 +0000 | [diff] [blame] | 6679 | // Allow assignments of an AltiVec vector type to an equivalent GCC |
| 6680 | // vector type and vice versa |
| 6681 | if (Context.areCompatibleVectorTypes(lhsType, rhsType)) { |
| 6682 | Kind = CK_BitCast; |
| 6683 | return Compatible; |
| 6684 | } |
| 6685 | |
| Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6686 | // If we are allowing lax vector conversions, and LHS and RHS are both |
| 6687 | // vectors, the total size only needs to be the same. This is a bitcast; |
| 6688 | // no bits are changed but the result type is different. |
| 6689 | if (getLangOptions().LaxVectorConversions && |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6690 | (Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType))) { |
| John McCall | 3065d04 | 2010-11-15 10:08:00 +0000 | [diff] [blame] | 6691 | Kind = CK_BitCast; |
| Anders Carlsson | db5a9b6 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 6692 | return IncompatibleVectors; |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6693 | } |
| Chris Lattner | 881a212 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 6694 | } |
| 6695 | return Incompatible; |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6696 | } |
| Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 6697 | |
| John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6698 | // Arithmetic conversions. |
| Douglas Gregor | bea453a | 2010-05-23 21:53:47 +0000 | [diff] [blame] | 6699 | if (lhsType->isArithmeticType() && rhsType->isArithmeticType() && |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6700 | !(getLangOptions().CPlusPlus && lhsType->isEnumeralType())) { |
| John McCall | 29600e1 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6701 | Kind = PrepareScalarCast(*this, rhs, lhsType); |
| Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 6702 | return Compatible; |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6703 | } |
| Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 6704 | |
| John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6705 | // Conversions to normal pointers. |
| 6706 | if (const PointerType *lhsPointer = dyn_cast<PointerType>(lhsType)) { |
| 6707 | // U* -> T* |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6708 | if (isa<PointerType>(rhsType)) { |
| 6709 | Kind = CK_BitCast; |
| John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6710 | return checkPointerTypesForAssignment(*this, lhsType, rhsType); |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6711 | } |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6712 | |
| John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6713 | // int -> T* |
| 6714 | if (rhsType->isIntegerType()) { |
| 6715 | Kind = CK_IntegralToPointer; // FIXME: null? |
| 6716 | return IntToPointer; |
| Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6717 | } |
| John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6718 | |
| 6719 | // C pointers are not compatible with ObjC object pointers, |
| 6720 | // with two exceptions: |
| 6721 | if (isa<ObjCObjectPointerType>(rhsType)) { |
| 6722 | // - conversions to void* |
| 6723 | if (lhsPointer->getPointeeType()->isVoidType()) { |
| 6724 | Kind = CK_AnyPointerToObjCPointerCast; |
| 6725 | return Compatible; |
| 6726 | } |
| 6727 | |
| 6728 | // - conversions from 'Class' to the redefinition type |
| 6729 | if (rhsType->isObjCClassType() && |
| 6730 | Context.hasSameType(lhsType, Context.ObjCClassRedefinitionType)) { |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6731 | Kind = CK_BitCast; |
| Douglas Gregor | e7dd145 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 6732 | return Compatible; |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6733 | } |
| Steve Naroff | 32d072c | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 6734 | |
| John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6735 | Kind = CK_BitCast; |
| 6736 | return IncompatiblePointer; |
| 6737 | } |
| 6738 | |
| 6739 | // U^ -> void* |
| 6740 | if (rhsType->getAs<BlockPointerType>()) { |
| 6741 | if (lhsPointer->getPointeeType()->isVoidType()) { |
| 6742 | Kind = CK_BitCast; |
| Steve Naroff | 32d072c | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 6743 | return Compatible; |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6744 | } |
| Steve Naroff | 32d072c | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 6745 | } |
| John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6746 | |
| Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6747 | return Incompatible; |
| 6748 | } |
| 6749 | |
| John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6750 | // Conversions to block pointers. |
| Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6751 | if (isa<BlockPointerType>(lhsType)) { |
| John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6752 | // U^ -> T^ |
| 6753 | if (rhsType->isBlockPointerType()) { |
| 6754 | Kind = CK_AnyPointerToBlockPointerCast; |
| John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6755 | return checkBlockPointerTypesForAssignment(*this, lhsType, rhsType); |
| John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6756 | } |
| 6757 | |
| 6758 | // int or null -> T^ |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6759 | if (rhsType->isIntegerType()) { |
| 6760 | Kind = CK_IntegralToPointer; // FIXME: null |
| Eli Friedman | 8163b7a | 2009-02-25 04:20:42 +0000 | [diff] [blame] | 6761 | return IntToBlockPointer; |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6762 | } |
| 6763 | |
| John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6764 | // id -> T^ |
| 6765 | if (getLangOptions().ObjC1 && rhsType->isObjCIdType()) { |
| 6766 | Kind = CK_AnyPointerToBlockPointerCast; |
| Steve Naroff | 32d072c | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 6767 | return Compatible; |
| John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6768 | } |
| Steve Naroff | 32d072c | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 6769 | |
| John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6770 | // void* -> T^ |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6771 | if (const PointerType *RHSPT = rhsType->getAs<PointerType>()) |
| John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6772 | if (RHSPT->getPointeeType()->isVoidType()) { |
| 6773 | Kind = CK_AnyPointerToBlockPointerCast; |
| Douglas Gregor | e7dd145 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 6774 | return Compatible; |
| John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6775 | } |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6776 | |
| Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 6777 | return Incompatible; |
| 6778 | } |
| 6779 | |
| John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6780 | // Conversions to Objective-C pointers. |
| Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6781 | if (isa<ObjCObjectPointerType>(lhsType)) { |
| John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6782 | // A* -> B* |
| 6783 | if (rhsType->isObjCObjectPointerType()) { |
| 6784 | Kind = CK_BitCast; |
| John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6785 | return checkObjCPointerTypesForAssignment(*this, lhsType, rhsType); |
| John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6786 | } |
| 6787 | |
| 6788 | // int or null -> A* |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6789 | if (rhsType->isIntegerType()) { |
| 6790 | Kind = CK_IntegralToPointer; // FIXME: null |
| Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6791 | return IntToPointer; |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6792 | } |
| 6793 | |
| John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6794 | // In general, C pointers are not compatible with ObjC object pointers, |
| 6795 | // with two exceptions: |
| Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6796 | if (isa<PointerType>(rhsType)) { |
| John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6797 | // - conversions from 'void*' |
| 6798 | if (rhsType->isVoidPointerType()) { |
| 6799 | Kind = CK_AnyPointerToObjCPointerCast; |
| Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 6800 | return Compatible; |
| John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6801 | } |
| 6802 | |
| 6803 | // - conversions to 'Class' from its redefinition type |
| 6804 | if (lhsType->isObjCClassType() && |
| 6805 | Context.hasSameType(rhsType, Context.ObjCClassRedefinitionType)) { |
| 6806 | Kind = CK_BitCast; |
| 6807 | return Compatible; |
| 6808 | } |
| 6809 | |
| 6810 | Kind = CK_AnyPointerToObjCPointerCast; |
| Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 6811 | return IncompatiblePointer; |
| Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6812 | } |
| John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6813 | |
| 6814 | // T^ -> A* |
| 6815 | if (rhsType->isBlockPointerType()) { |
| 6816 | Kind = CK_AnyPointerToObjCPointerCast; |
| Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6817 | return Compatible; |
| John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6818 | } |
| 6819 | |
| Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6820 | return Incompatible; |
| 6821 | } |
| John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6822 | |
| 6823 | // Conversions from pointers that are not covered by the above. |
| Chris Lattner | ec64683 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 6824 | if (isa<PointerType>(rhsType)) { |
| John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6825 | // T* -> _Bool |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6826 | if (lhsType == Context.BoolTy) { |
| 6827 | Kind = CK_PointerToBoolean; |
| Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 6828 | return Compatible; |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6829 | } |
| Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 6830 | |
| John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6831 | // T* -> int |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6832 | if (lhsType->isIntegerType()) { |
| 6833 | Kind = CK_PointerToIntegral; |
| Chris Lattner | 940cfeb | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 6834 | return PointerToInt; |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6835 | } |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6836 | |
| Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 6837 | return Incompatible; |
| Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 6838 | } |
| John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6839 | |
| 6840 | // Conversions from Objective-C pointers that are not covered by the above. |
| Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6841 | if (isa<ObjCObjectPointerType>(rhsType)) { |
| John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6842 | // T* -> _Bool |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6843 | if (lhsType == Context.BoolTy) { |
| 6844 | Kind = CK_PointerToBoolean; |
| Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6845 | return Compatible; |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6846 | } |
| Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6847 | |
| John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6848 | // T* -> int |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6849 | if (lhsType->isIntegerType()) { |
| 6850 | Kind = CK_PointerToIntegral; |
| Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6851 | return PointerToInt; |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6852 | } |
| 6853 | |
| Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6854 | return Incompatible; |
| 6855 | } |
| Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 6856 | |
| John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6857 | // struct A -> struct B |
| Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 6858 | if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) { |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6859 | if (Context.typesAreCompatible(lhsType, rhsType)) { |
| 6860 | Kind = CK_NoOp; |
| Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 6861 | return Compatible; |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6862 | } |
| Bill Wendling | 216423b | 2007-05-30 06:30:29 +0000 | [diff] [blame] | 6863 | } |
| John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6864 | |
| Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 6865 | return Incompatible; |
| Steve Naroff | 9eb2465 | 2007-05-02 21:58:15 +0000 | [diff] [blame] | 6866 | } |
| 6867 | |
| Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6868 | /// \brief Constructs a transparent union from an expression that is |
| 6869 | /// used to initialize the transparent union. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6870 | static void ConstructTransparentUnion(Sema &S, ASTContext &C, ExprResult &EResult, |
| Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6871 | QualType UnionType, FieldDecl *Field) { |
| 6872 | // Build an initializer list that designates the appropriate member |
| 6873 | // of the transparent union. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6874 | Expr *E = EResult.take(); |
| Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 6875 | InitListExpr *Initializer = new (C) InitListExpr(C, SourceLocation(), |
| Ted Kremenek | 013041e | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 6876 | &E, 1, |
| Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6877 | SourceLocation()); |
| 6878 | Initializer->setType(UnionType); |
| 6879 | Initializer->setInitializedFieldInUnion(Field); |
| 6880 | |
| 6881 | // Build a compound literal constructing a value of the transparent |
| 6882 | // union type from this initializer list. |
| John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 6883 | TypeSourceInfo *unionTInfo = C.getTrivialTypeSourceInfo(UnionType); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6884 | EResult = S.Owned( |
| 6885 | new (C) CompoundLiteralExpr(SourceLocation(), unionTInfo, UnionType, |
| 6886 | VK_RValue, Initializer, false)); |
| Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6887 | } |
| 6888 | |
| 6889 | Sema::AssignConvertType |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6890 | Sema::CheckTransparentUnionArgumentConstraints(QualType ArgType, ExprResult &rExpr) { |
| 6891 | QualType FromType = rExpr.get()->getType(); |
| Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6892 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6893 | // If the ArgType is a Union type, we want to handle a potential |
| Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6894 | // transparent_union GCC extension. |
| 6895 | const RecordType *UT = ArgType->getAsUnionType(); |
| Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 6896 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
| Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6897 | return Incompatible; |
| 6898 | |
| 6899 | // The field to initialize within the transparent union. |
| 6900 | RecordDecl *UD = UT->getDecl(); |
| 6901 | FieldDecl *InitField = 0; |
| 6902 | // It's compatible if the expression matches any of the fields. |
| Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 6903 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 6904 | itend = UD->field_end(); |
| Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6905 | it != itend; ++it) { |
| 6906 | if (it->getType()->isPointerType()) { |
| 6907 | // If the transparent union contains a pointer type, we allow: |
| 6908 | // 1) void pointer |
| 6909 | // 2) null pointer constant |
| 6910 | if (FromType->isPointerType()) |
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6911 | if (FromType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6912 | rExpr = ImpCastExprToType(rExpr.take(), it->getType(), CK_BitCast); |
| Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6913 | InitField = *it; |
| 6914 | break; |
| 6915 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6916 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6917 | if (rExpr.get()->isNullPointerConstant(Context, |
| Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6918 | Expr::NPC_ValueDependentIsNull)) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6919 | rExpr = ImpCastExprToType(rExpr.take(), it->getType(), CK_NullToPointer); |
| Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6920 | InitField = *it; |
| 6921 | break; |
| 6922 | } |
| 6923 | } |
| 6924 | |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6925 | CastKind Kind = CK_Invalid; |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6926 | if (CheckAssignmentConstraints(it->getType(), rExpr, Kind) |
| Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6927 | == Compatible) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6928 | rExpr = ImpCastExprToType(rExpr.take(), it->getType(), Kind); |
| Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6929 | InitField = *it; |
| 6930 | break; |
| 6931 | } |
| 6932 | } |
| 6933 | |
| 6934 | if (!InitField) |
| 6935 | return Incompatible; |
| 6936 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6937 | ConstructTransparentUnion(*this, Context, rExpr, ArgType, InitField); |
| Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6938 | return Compatible; |
| 6939 | } |
| 6940 | |
| Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6941 | Sema::AssignConvertType |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6942 | Sema::CheckSingleAssignmentConstraints(QualType lhsType, ExprResult &rExpr) { |
| Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 6943 | if (getLangOptions().CPlusPlus) { |
| 6944 | if (!lhsType->isRecordType()) { |
| 6945 | // C++ 5.17p3: If the left operand is not of class type, the |
| 6946 | // expression is implicitly converted (C++ 4) to the |
| 6947 | // cv-unqualified type of the left operand. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6948 | ExprResult Res = PerformImplicitConversion(rExpr.get(), |
| 6949 | lhsType.getUnqualifiedType(), |
| 6950 | AA_Assigning); |
| 6951 | if (Res.isInvalid()) |
| Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 6952 | return Incompatible; |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6953 | rExpr = move(Res); |
| Chris Lattner | 0d5640c | 2009-04-12 09:02:39 +0000 | [diff] [blame] | 6954 | return Compatible; |
| Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 6955 | } |
| 6956 | |
| 6957 | // FIXME: Currently, we fall through and treat C++ classes like C |
| 6958 | // structures. |
| John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 6959 | } |
| Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 6960 | |
| Steve Naroff | 0ee0b0a | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 6961 | // C99 6.5.16.1p1: the left operand is a pointer and the right is |
| 6962 | // a null pointer constant. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6963 | if ((lhsType->isPointerType() || |
| 6964 | lhsType->isObjCObjectPointerType() || |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6965 | lhsType->isBlockPointerType()) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6966 | && rExpr.get()->isNullPointerConstant(Context, |
| Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6967 | Expr::NPC_ValueDependentIsNull)) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6968 | rExpr = ImpCastExprToType(rExpr.take(), lhsType, CK_NullToPointer); |
| Steve Naroff | 0ee0b0a | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 6969 | return Compatible; |
| 6970 | } |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6971 | |
| Chris Lattner | e6dcd50 | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 6972 | // This check seems unnatural, however it is necessary to ensure the proper |
| Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 6973 | // conversion of functions/arrays. If the conversion were done for all |
| Douglas Gregor | a121b75 | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 6974 | // DeclExpr's (created by ActOnIdExpression), it would mess up the unary |
| Nick Lewycky | ef7c0ff | 2010-08-05 06:27:49 +0000 | [diff] [blame] | 6975 | // expressions that suppress this implicit conversion (&, sizeof). |
| Chris Lattner | e6dcd50 | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 6976 | // |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6977 | // Suppress this for references: C++ 8.5.3p5. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6978 | if (!lhsType->isReferenceType()) { |
| 6979 | rExpr = DefaultFunctionArrayLvalueConversion(rExpr.take()); |
| 6980 | if (rExpr.isInvalid()) |
| 6981 | return Incompatible; |
| 6982 | } |
| Steve Naroff | 0c1c7ed | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 6983 | |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6984 | CastKind Kind = CK_Invalid; |
| Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6985 | Sema::AssignConvertType result = |
| John McCall | 29600e1 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6986 | CheckAssignmentConstraints(lhsType, rExpr, Kind); |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6987 | |
| Steve Naroff | 0c1c7ed | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 6988 | // C99 6.5.16.1p2: The value of the right operand is converted to the |
| 6989 | // type of the assignment expression. |
| Douglas Gregor | 6b75484 | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 6990 | // CheckAssignmentConstraints allows the left-hand side to be a reference, |
| 6991 | // so that we can use references in built-in functions even in C. |
| 6992 | // The getNonReferenceType() call makes sure that the resulting expression |
| 6993 | // does not have reference type. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6994 | if (result != Incompatible && rExpr.get()->getType() != lhsType) |
| 6995 | rExpr = ImpCastExprToType(rExpr.take(), lhsType.getNonLValueExprType(Context), Kind); |
| Steve Naroff | 0c1c7ed | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 6996 | return result; |
| Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 6997 | } |
| 6998 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6999 | QualType Sema::InvalidOperands(SourceLocation Loc, ExprResult &lex, ExprResult &rex) { |
| Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 7000 | Diag(Loc, diag::err_typecheck_invalid_operands) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7001 | << lex.get()->getType() << rex.get()->getType() |
| 7002 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
| Chris Lattner | 5c11c41 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 7003 | return QualType(); |
| Steve Naroff | 6f49f5d | 2007-05-29 14:23:36 +0000 | [diff] [blame] | 7004 | } |
| 7005 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7006 | QualType Sema::CheckVectorOperands(SourceLocation Loc, ExprResult &lex, ExprResult &rex) { |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7007 | // For conversion purposes, we ignore any qualifiers. |
| Nate Begeman | 002e4bd | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 7008 | // For example, "const float" and "float" are equivalent. |
| Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 7009 | QualType lhsType = |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7010 | Context.getCanonicalType(lex.get()->getType()).getUnqualifiedType(); |
| Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 7011 | QualType rhsType = |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7012 | Context.getCanonicalType(rex.get()->getType()).getUnqualifiedType(); |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7013 | |
| Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7014 | // If the vector types are identical, return. |
| Nate Begeman | 002e4bd | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 7015 | if (lhsType == rhsType) |
| Steve Naroff | 84ff4b4 | 2007-07-09 21:31:10 +0000 | [diff] [blame] | 7016 | return lhsType; |
| Nate Begeman | 330aaa7 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 7017 | |
| Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7018 | // Handle the case of a vector & extvector type of the same size and element |
| 7019 | // type. It would be nice if we only had one vector type someday. |
| Anders Carlsson | db5a9b6 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 7020 | if (getLangOptions().LaxVectorConversions) { |
| John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 7021 | if (const VectorType *LV = lhsType->getAs<VectorType>()) { |
| Chandler Carruth | 9ed87ba | 2010-08-30 07:36:24 +0000 | [diff] [blame] | 7022 | if (const VectorType *RV = rhsType->getAs<VectorType>()) { |
| Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7023 | if (LV->getElementType() == RV->getElementType() && |
| Anders Carlsson | db5a9b6 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 7024 | LV->getNumElements() == RV->getNumElements()) { |
| Douglas Gregor | cbfbca1 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 7025 | if (lhsType->isExtVectorType()) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7026 | rex = ImpCastExprToType(rex.take(), lhsType, CK_BitCast); |
| Douglas Gregor | cbfbca1 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 7027 | return lhsType; |
| 7028 | } |
| 7029 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7030 | lex = ImpCastExprToType(lex.take(), rhsType, CK_BitCast); |
| Douglas Gregor | cbfbca1 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 7031 | return rhsType; |
| Eric Christopher | a613f56 | 2010-08-26 00:42:16 +0000 | [diff] [blame] | 7032 | } else if (Context.getTypeSize(lhsType) ==Context.getTypeSize(rhsType)){ |
| 7033 | // If we are allowing lax vector conversions, and LHS and RHS are both |
| 7034 | // vectors, the total size only needs to be the same. This is a |
| 7035 | // bitcast; no bits are changed but the result type is different. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7036 | rex = ImpCastExprToType(rex.take(), lhsType, CK_BitCast); |
| Eric Christopher | a613f56 | 2010-08-26 00:42:16 +0000 | [diff] [blame] | 7037 | return lhsType; |
| Anders Carlsson | db5a9b6 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 7038 | } |
| Eric Christopher | a613f56 | 2010-08-26 00:42:16 +0000 | [diff] [blame] | 7039 | } |
| Chandler Carruth | 9ed87ba | 2010-08-30 07:36:24 +0000 | [diff] [blame] | 7040 | } |
| Anders Carlsson | db5a9b6 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 7041 | } |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7042 | |
| Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 7043 | // Handle the case of equivalent AltiVec and GCC vector types |
| 7044 | if (lhsType->isVectorType() && rhsType->isVectorType() && |
| 7045 | Context.areCompatibleVectorTypes(lhsType, rhsType)) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7046 | lex = ImpCastExprToType(lex.take(), rhsType, CK_BitCast); |
| Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 7047 | return rhsType; |
| 7048 | } |
| 7049 | |
| Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 7050 | // Canonicalize the ExtVector to the LHS, remember if we swapped so we can |
| 7051 | // swap back (so that we don't reverse the inputs to a subtract, for instance. |
| 7052 | bool swapped = false; |
| 7053 | if (rhsType->isExtVectorType()) { |
| 7054 | swapped = true; |
| 7055 | std::swap(rex, lex); |
| 7056 | std::swap(rhsType, lhsType); |
| 7057 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7058 | |
| Nate Begeman | 886448d | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 7059 | // Handle the case of an ext vector and scalar. |
| John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 7060 | if (const ExtVectorType *LV = lhsType->getAs<ExtVectorType>()) { |
| Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 7061 | QualType EltTy = LV->getElementType(); |
| Douglas Gregor | 6972a62 | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 7062 | if (EltTy->isIntegralType(Context) && rhsType->isIntegralType(Context)) { |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 7063 | int order = Context.getIntegerTypeOrder(EltTy, rhsType); |
| 7064 | if (order > 0) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7065 | rex = ImpCastExprToType(rex.take(), EltTy, CK_IntegralCast); |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 7066 | if (order >= 0) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7067 | rex = ImpCastExprToType(rex.take(), lhsType, CK_VectorSplat); |
| Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 7068 | if (swapped) std::swap(rex, lex); |
| 7069 | return lhsType; |
| 7070 | } |
| 7071 | } |
| 7072 | if (EltTy->isRealFloatingType() && rhsType->isScalarType() && |
| 7073 | rhsType->isRealFloatingType()) { |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 7074 | int order = Context.getFloatingTypeOrder(EltTy, rhsType); |
| 7075 | if (order > 0) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7076 | rex = ImpCastExprToType(rex.take(), EltTy, CK_FloatingCast); |
| John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 7077 | if (order >= 0) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7078 | rex = ImpCastExprToType(rex.take(), lhsType, CK_VectorSplat); |
| Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 7079 | if (swapped) std::swap(rex, lex); |
| 7080 | return lhsType; |
| 7081 | } |
| Nate Begeman | 330aaa7 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 7082 | } |
| 7083 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7084 | |
| Nate Begeman | 886448d | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 7085 | // Vectors of different size or scalar and non-ext-vector are errors. |
| Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 7086 | Diag(Loc, diag::err_typecheck_vector_not_convertable) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7087 | << lex.get()->getType() << rex.get()->getType() |
| 7088 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
| Steve Naroff | 84ff4b4 | 2007-07-09 21:31:10 +0000 | [diff] [blame] | 7089 | return QualType(); |
| Sebastian Redl | 112a9766 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 7090 | } |
| 7091 | |
| Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 7092 | QualType Sema::CheckMultiplyDivideOperands( |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7093 | ExprResult &lex, ExprResult &rex, SourceLocation Loc, bool isCompAssign, bool isDiv) { |
| 7094 | if (lex.get()->getType()->isVectorType() || rex.get()->getType()->isVectorType()) |
| Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7095 | return CheckVectorOperands(Loc, lex, rex); |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7096 | |
| Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 7097 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7098 | if (lex.isInvalid() || rex.isInvalid()) |
| 7099 | return QualType(); |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7100 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7101 | if (!lex.get()->getType()->isArithmeticType() || |
| 7102 | !rex.get()->getType()->isArithmeticType()) |
| Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 7103 | return InvalidOperands(Loc, lex, rex); |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7104 | |
| Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 7105 | // Check for division by zero. |
| 7106 | if (isDiv && |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7107 | rex.get()->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull)) |
| 7108 | DiagRuntimeBehavior(Loc, rex.get(), PDiag(diag::warn_division_by_zero) |
| 7109 | << rex.get()->getSourceRange()); |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7110 | |
| Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 7111 | return compType; |
| Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 7112 | } |
| 7113 | |
| Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 7114 | QualType Sema::CheckRemainderOperands( |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7115 | ExprResult &lex, ExprResult &rex, SourceLocation Loc, bool isCompAssign) { |
| 7116 | if (lex.get()->getType()->isVectorType() || rex.get()->getType()->isVectorType()) { |
| 7117 | if (lex.get()->getType()->hasIntegerRepresentation() && |
| 7118 | rex.get()->getType()->hasIntegerRepresentation()) |
| Daniel Dunbar | 0d2bfec | 2009-01-05 22:55:36 +0000 | [diff] [blame] | 7119 | return CheckVectorOperands(Loc, lex, rex); |
| 7120 | return InvalidOperands(Loc, lex, rex); |
| 7121 | } |
| Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 7122 | |
| Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 7123 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7124 | if (lex.isInvalid() || rex.isInvalid()) |
| 7125 | return QualType(); |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7126 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7127 | if (!lex.get()->getType()->isIntegerType() || !rex.get()->getType()->isIntegerType()) |
| Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 7128 | return InvalidOperands(Loc, lex, rex); |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7129 | |
| Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 7130 | // Check for remainder by zero. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7131 | if (rex.get()->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull)) |
| 7132 | DiagRuntimeBehavior(Loc, rex.get(), PDiag(diag::warn_remainder_by_zero) |
| 7133 | << rex.get()->getSourceRange()); |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7134 | |
| Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 7135 | return compType; |
| Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 7136 | } |
| 7137 | |
| Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 7138 | QualType Sema::CheckAdditionOperands( // C99 6.5.6 |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7139 | ExprResult &lex, ExprResult &rex, SourceLocation Loc, QualType* CompLHSTy) { |
| 7140 | if (lex.get()->getType()->isVectorType() || rex.get()->getType()->isVectorType()) { |
| Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7141 | QualType compType = CheckVectorOperands(Loc, lex, rex); |
| 7142 | if (CompLHSTy) *CompLHSTy = compType; |
| 7143 | return compType; |
| 7144 | } |
| Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 7145 | |
| Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7146 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7147 | if (lex.isInvalid() || rex.isInvalid()) |
| 7148 | return QualType(); |
| Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 7149 | |
| Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 7150 | // handle the common case first (both operands are arithmetic). |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7151 | if (lex.get()->getType()->isArithmeticType() && |
| 7152 | rex.get()->getType()->isArithmeticType()) { |
| Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7153 | if (CompLHSTy) *CompLHSTy = compType; |
| Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 7154 | return compType; |
| Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7155 | } |
| Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 7156 | |
| Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 7157 | // Put any potential pointer into PExp |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7158 | Expr* PExp = lex.get(), *IExp = rex.get(); |
| Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 7159 | if (IExp->getType()->isAnyPointerType()) |
| Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 7160 | std::swap(PExp, IExp); |
| 7161 | |
| Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 7162 | if (PExp->getType()->isAnyPointerType()) { |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7163 | |
| Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 7164 | if (IExp->getType()->isIntegerType()) { |
| Steve Naroff | aacd4cc | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 7165 | QualType PointeeTy = PExp->getType()->getPointeeType(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7166 | |
| Chris Lattner | 12bdebb | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 7167 | // Check for arithmetic on pointers to incomplete types. |
| 7168 | if (PointeeTy->isVoidType()) { |
| Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7169 | if (getLangOptions().CPlusPlus) { |
| 7170 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7171 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
| Douglas Gregor | dd430f7 | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 7172 | return QualType(); |
| Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 7173 | } |
| Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7174 | |
| 7175 | // GNU extension: arithmetic on pointer to void |
| 7176 | Diag(Loc, diag::ext_gnu_void_ptr) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7177 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
| Chris Lattner | 12bdebb | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 7178 | } else if (PointeeTy->isFunctionType()) { |
| Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7179 | if (getLangOptions().CPlusPlus) { |
| 7180 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7181 | << lex.get()->getType() << lex.get()->getSourceRange(); |
| Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7182 | return QualType(); |
| 7183 | } |
| 7184 | |
| 7185 | // GNU extension: arithmetic on pointer to function |
| 7186 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7187 | << lex.get()->getType() << lex.get()->getSourceRange(); |
| Steve Naroff | a63372d | 2009-07-13 21:32:29 +0000 | [diff] [blame] | 7188 | } else { |
| Steve Naroff | aacd4cc | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 7189 | // Check if we require a complete type. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7190 | if (((PExp->getType()->isPointerType() && |
| Steve Naroff | a63372d | 2009-07-13 21:32:29 +0000 | [diff] [blame] | 7191 | !PExp->getType()->isDependentType()) || |
| Steve Naroff | aacd4cc | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 7192 | PExp->getType()->isObjCObjectPointerType()) && |
| 7193 | RequireCompleteType(Loc, PointeeTy, |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7194 | PDiag(diag::err_typecheck_arithmetic_incomplete_type) |
| 7195 | << PExp->getSourceRange() |
| Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 7196 | << PExp->getType())) |
| Steve Naroff | aacd4cc | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 7197 | return QualType(); |
| 7198 | } |
| Chris Lattner | 12bdebb | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 7199 | // Diagnose bad cases where we step over interface counts. |
| John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 7200 | if (PointeeTy->isObjCObjectType() && LangOpts.ObjCNonFragileABI) { |
| Chris Lattner | 12bdebb | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 7201 | Diag(Loc, diag::err_arithmetic_nonfragile_interface) |
| 7202 | << PointeeTy << PExp->getSourceRange(); |
| 7203 | return QualType(); |
| 7204 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7205 | |
| Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7206 | if (CompLHSTy) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7207 | QualType LHSTy = Context.isPromotableBitField(lex.get()); |
| Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 7208 | if (LHSTy.isNull()) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7209 | LHSTy = lex.get()->getType(); |
| Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 7210 | if (LHSTy->isPromotableIntegerType()) |
| 7211 | LHSTy = Context.getPromotedIntegerType(LHSTy); |
| Douglas Gregor | d2c2d17 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 7212 | } |
| Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7213 | *CompLHSTy = LHSTy; |
| 7214 | } |
| Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 7215 | return PExp->getType(); |
| 7216 | } |
| 7217 | } |
| 7218 | |
| Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7219 | return InvalidOperands(Loc, lex, rex); |
| Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 7220 | } |
| 7221 | |
| Chris Lattner | 2a3569b | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 7222 | // C99 6.5.6 |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7223 | QualType Sema::CheckSubtractionOperands(ExprResult &lex, ExprResult &rex, |
| Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7224 | SourceLocation Loc, QualType* CompLHSTy) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7225 | if (lex.get()->getType()->isVectorType() || rex.get()->getType()->isVectorType()) { |
| Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7226 | QualType compType = CheckVectorOperands(Loc, lex, rex); |
| 7227 | if (CompLHSTy) *CompLHSTy = compType; |
| 7228 | return compType; |
| 7229 | } |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7230 | |
| Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7231 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7232 | if (lex.isInvalid() || rex.isInvalid()) |
| 7233 | return QualType(); |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7234 | |
| Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 7235 | // Enforce type constraints: C99 6.5.6p3. |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7236 | |
| Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 7237 | // Handle the common case first (both operands are arithmetic). |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7238 | if (lex.get()->getType()->isArithmeticType() && |
| 7239 | rex.get()->getType()->isArithmeticType()) { |
| Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7240 | if (CompLHSTy) *CompLHSTy = compType; |
| Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 7241 | return compType; |
| Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7242 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7243 | |
| Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 7244 | // Either ptr - int or ptr - ptr. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7245 | if (lex.get()->getType()->isAnyPointerType()) { |
| 7246 | QualType lpointee = lex.get()->getType()->getPointeeType(); |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7247 | |
| Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7248 | // The LHS must be an completely-defined object type. |
| Douglas Gregor | f6cd928 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 7249 | |
| Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7250 | bool ComplainAboutVoid = false; |
| 7251 | Expr *ComplainAboutFunc = 0; |
| 7252 | if (lpointee->isVoidType()) { |
| 7253 | if (getLangOptions().CPlusPlus) { |
| 7254 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7255 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
| Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7256 | return QualType(); |
| 7257 | } |
| 7258 | |
| 7259 | // GNU C extension: arithmetic on pointer to void |
| 7260 | ComplainAboutVoid = true; |
| 7261 | } else if (lpointee->isFunctionType()) { |
| 7262 | if (getLangOptions().CPlusPlus) { |
| 7263 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7264 | << lex.get()->getType() << lex.get()->getSourceRange(); |
| Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 7265 | return QualType(); |
| 7266 | } |
| Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7267 | |
| 7268 | // GNU C extension: arithmetic on pointer to function |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7269 | ComplainAboutFunc = lex.get(); |
| Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7270 | } else if (!lpointee->isDependentType() && |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7271 | RequireCompleteType(Loc, lpointee, |
| Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 7272 | PDiag(diag::err_typecheck_sub_ptr_object) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7273 | << lex.get()->getSourceRange() |
| 7274 | << lex.get()->getType())) |
| Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7275 | return QualType(); |
| Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 7276 | |
| Chris Lattner | 12bdebb | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 7277 | // Diagnose bad cases where we step over interface counts. |
| John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 7278 | if (lpointee->isObjCObjectType() && LangOpts.ObjCNonFragileABI) { |
| Chris Lattner | 12bdebb | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 7279 | Diag(Loc, diag::err_arithmetic_nonfragile_interface) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7280 | << lpointee << lex.get()->getSourceRange(); |
| Chris Lattner | 12bdebb | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 7281 | return QualType(); |
| 7282 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7283 | |
| Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 7284 | // The result type of a pointer-int computation is the pointer type. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7285 | if (rex.get()->getType()->isIntegerType()) { |
| Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7286 | if (ComplainAboutVoid) |
| 7287 | Diag(Loc, diag::ext_gnu_void_ptr) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7288 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
| Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7289 | if (ComplainAboutFunc) |
| 7290 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7291 | << ComplainAboutFunc->getType() |
| Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7292 | << ComplainAboutFunc->getSourceRange(); |
| 7293 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7294 | if (CompLHSTy) *CompLHSTy = lex.get()->getType(); |
| 7295 | return lex.get()->getType(); |
| Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7296 | } |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7297 | |
| Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 7298 | // Handle pointer-pointer subtractions. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7299 | if (const PointerType *RHSPTy = rex.get()->getType()->getAs<PointerType>()) { |
| Eli Friedman | 1974e53 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 7300 | QualType rpointee = RHSPTy->getPointeeType(); |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7301 | |
| Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7302 | // RHS must be a completely-type object type. |
| 7303 | // Handle the GNU void* extension. |
| 7304 | if (rpointee->isVoidType()) { |
| 7305 | if (getLangOptions().CPlusPlus) { |
| 7306 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7307 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
| Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7308 | return QualType(); |
| 7309 | } |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7310 | |
| Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7311 | ComplainAboutVoid = true; |
| 7312 | } else if (rpointee->isFunctionType()) { |
| 7313 | if (getLangOptions().CPlusPlus) { |
| 7314 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7315 | << rex.get()->getType() << rex.get()->getSourceRange(); |
| Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 7316 | return QualType(); |
| 7317 | } |
| Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7318 | |
| 7319 | // GNU extension: arithmetic on pointer to function |
| 7320 | if (!ComplainAboutFunc) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7321 | ComplainAboutFunc = rex.get(); |
| Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7322 | } else if (!rpointee->isDependentType() && |
| 7323 | RequireCompleteType(Loc, rpointee, |
| Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 7324 | PDiag(diag::err_typecheck_sub_ptr_object) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7325 | << rex.get()->getSourceRange() |
| 7326 | << rex.get()->getType())) |
| Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7327 | return QualType(); |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7328 | |
| Eli Friedman | 168fe15 | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 7329 | if (getLangOptions().CPlusPlus) { |
| 7330 | // Pointee types must be the same: C++ [expr.add] |
| 7331 | if (!Context.hasSameUnqualifiedType(lpointee, rpointee)) { |
| 7332 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7333 | << lex.get()->getType() << rex.get()->getType() |
| 7334 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
| Eli Friedman | 168fe15 | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 7335 | return QualType(); |
| 7336 | } |
| 7337 | } else { |
| 7338 | // Pointee types must be compatible C99 6.5.6p3 |
| 7339 | if (!Context.typesAreCompatible( |
| 7340 | Context.getCanonicalType(lpointee).getUnqualifiedType(), |
| 7341 | Context.getCanonicalType(rpointee).getUnqualifiedType())) { |
| 7342 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7343 | << lex.get()->getType() << rex.get()->getType() |
| 7344 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
| Eli Friedman | 168fe15 | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 7345 | return QualType(); |
| 7346 | } |
| Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 7347 | } |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7348 | |
| Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7349 | if (ComplainAboutVoid) |
| 7350 | Diag(Loc, diag::ext_gnu_void_ptr) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7351 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
| Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7352 | if (ComplainAboutFunc) |
| 7353 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7354 | << ComplainAboutFunc->getType() |
| Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7355 | << ComplainAboutFunc->getSourceRange(); |
| Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7356 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7357 | if (CompLHSTy) *CompLHSTy = lex.get()->getType(); |
| Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 7358 | return Context.getPointerDiffType(); |
| 7359 | } |
| 7360 | } |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7361 | |
| Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7362 | return InvalidOperands(Loc, lex, rex); |
| Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 7363 | } |
| 7364 | |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 7365 | static bool isScopedEnumerationType(QualType T) { |
| 7366 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
| 7367 | return ET->getDecl()->isScoped(); |
| 7368 | return false; |
| 7369 | } |
| 7370 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7371 | static void DiagnoseBadShiftValues(Sema& S, ExprResult &lex, ExprResult &rex, |
| Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7372 | SourceLocation Loc, unsigned Opc, |
| 7373 | QualType LHSTy) { |
| 7374 | llvm::APSInt Right; |
| 7375 | // Check right/shifter operand |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7376 | if (rex.get()->isValueDependent() || !rex.get()->isIntegerConstantExpr(Right, S.Context)) |
| Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7377 | return; |
| 7378 | |
| 7379 | if (Right.isNegative()) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7380 | S.DiagRuntimeBehavior(Loc, rex.get(), |
| Ted Kremenek | 63657fe | 2011-03-01 18:09:31 +0000 | [diff] [blame] | 7381 | S.PDiag(diag::warn_shift_negative) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7382 | << rex.get()->getSourceRange()); |
| Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7383 | return; |
| 7384 | } |
| 7385 | llvm::APInt LeftBits(Right.getBitWidth(), |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7386 | S.Context.getTypeSize(lex.get()->getType())); |
| Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7387 | if (Right.uge(LeftBits)) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7388 | S.DiagRuntimeBehavior(Loc, rex.get(), |
| Ted Kremenek | 26bbc3d | 2011-03-01 19:13:22 +0000 | [diff] [blame] | 7389 | S.PDiag(diag::warn_shift_gt_typewidth) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7390 | << rex.get()->getSourceRange()); |
| Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7391 | return; |
| 7392 | } |
| 7393 | if (Opc != BO_Shl) |
| 7394 | return; |
| 7395 | |
| 7396 | // When left shifting an ICE which is signed, we can check for overflow which |
| 7397 | // according to C++ has undefined behavior ([expr.shift] 5.8/2). Unsigned |
| 7398 | // integers have defined behavior modulo one more than the maximum value |
| 7399 | // representable in the result type, so never warn for those. |
| 7400 | llvm::APSInt Left; |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7401 | if (lex.get()->isValueDependent() || !lex.get()->isIntegerConstantExpr(Left, S.Context) || |
| Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7402 | LHSTy->hasUnsignedIntegerRepresentation()) |
| 7403 | return; |
| 7404 | llvm::APInt ResultBits = |
| 7405 | static_cast<llvm::APInt&>(Right) + Left.getMinSignedBits(); |
| 7406 | if (LeftBits.uge(ResultBits)) |
| 7407 | return; |
| 7408 | llvm::APSInt Result = Left.extend(ResultBits.getLimitedValue()); |
| 7409 | Result = Result.shl(Right); |
| 7410 | |
| Ted Kremenek | 70f05fd | 2011-06-15 00:54:52 +0000 | [diff] [blame] | 7411 | // Print the bit representation of the signed integer as an unsigned |
| 7412 | // hexadecimal number. |
| 7413 | llvm::SmallString<40> HexResult; |
| 7414 | Result.toString(HexResult, 16, /*Signed =*/false, /*Literal =*/true); |
| 7415 | |
| Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7416 | // If we are only missing a sign bit, this is less likely to result in actual |
| 7417 | // bugs -- if the result is cast back to an unsigned type, it will have the |
| 7418 | // expected value. Thus we place this behind a different warning that can be |
| 7419 | // turned off separately if needed. |
| 7420 | if (LeftBits == ResultBits - 1) { |
| Ted Kremenek | 70f05fd | 2011-06-15 00:54:52 +0000 | [diff] [blame] | 7421 | S.Diag(Loc, diag::warn_shift_result_sets_sign_bit) |
| 7422 | << HexResult.str() << LHSTy |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7423 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
| Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7424 | return; |
| 7425 | } |
| 7426 | |
| 7427 | S.Diag(Loc, diag::warn_shift_result_gt_typewidth) |
| Ted Kremenek | 70f05fd | 2011-06-15 00:54:52 +0000 | [diff] [blame] | 7428 | << HexResult.str() << Result.getMinSignedBits() << LHSTy |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7429 | << Left.getBitWidth() << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
| Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7430 | } |
| 7431 | |
| Chris Lattner | 2a3569b | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 7432 | // C99 6.5.7 |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7433 | QualType Sema::CheckShiftOperands(ExprResult &lex, ExprResult &rex, SourceLocation Loc, |
| Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7434 | unsigned Opc, bool isCompAssign) { |
| Chris Lattner | 5c11c41 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 7435 | // C99 6.5.7p2: Each of the operands shall have integer type. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7436 | if (!lex.get()->getType()->hasIntegerRepresentation() || |
| 7437 | !rex.get()->getType()->hasIntegerRepresentation()) |
| Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7438 | return InvalidOperands(Loc, lex, rex); |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7439 | |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 7440 | // C++0x: Don't allow scoped enums. FIXME: Use something better than |
| 7441 | // hasIntegerRepresentation() above instead of this. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7442 | if (isScopedEnumerationType(lex.get()->getType()) || |
| 7443 | isScopedEnumerationType(rex.get()->getType())) { |
| Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 7444 | return InvalidOperands(Loc, lex, rex); |
| 7445 | } |
| 7446 | |
| Nate Begeman | e46ee9a | 2009-10-25 02:26:48 +0000 | [diff] [blame] | 7447 | // Vector shifts promote their scalar inputs to vector type. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7448 | if (lex.get()->getType()->isVectorType() || rex.get()->getType()->isVectorType()) |
| Nate Begeman | e46ee9a | 2009-10-25 02:26:48 +0000 | [diff] [blame] | 7449 | return CheckVectorOperands(Loc, lex, rex); |
| 7450 | |
| Chris Lattner | 5c11c41 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 7451 | // Shifts don't perform usual arithmetic conversions, they just do integer |
| 7452 | // promotions on each operand. C99 6.5.7p3 |
| Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7453 | |
| John McCall | 57cdd88 | 2010-12-16 19:28:59 +0000 | [diff] [blame] | 7454 | // For the LHS, do usual unary conversions, but then reset them away |
| 7455 | // if this is a compound assignment. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7456 | ExprResult old_lex = lex; |
| 7457 | lex = UsualUnaryConversions(lex.take()); |
| 7458 | if (lex.isInvalid()) |
| 7459 | return QualType(); |
| 7460 | QualType LHSTy = lex.get()->getType(); |
| John McCall | 57cdd88 | 2010-12-16 19:28:59 +0000 | [diff] [blame] | 7461 | if (isCompAssign) lex = old_lex; |
| 7462 | |
| 7463 | // The RHS is simpler. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7464 | rex = UsualUnaryConversions(rex.take()); |
| 7465 | if (rex.isInvalid()) |
| 7466 | return QualType(); |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7467 | |
| Ryan Flynn | f53fab8 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 7468 | // Sanity-check shift operands |
| Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7469 | DiagnoseBadShiftValues(*this, lex, rex, Loc, Opc, LHSTy); |
| Ryan Flynn | f53fab8 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 7470 | |
| Chris Lattner | 5c11c41 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 7471 | // "The type of the result is that of the promoted left operand." |
| Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7472 | return LHSTy; |
| Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 7473 | } |
| 7474 | |
| Chandler Carruth | 17773fc | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 7475 | static bool IsWithinTemplateSpecialization(Decl *D) { |
| 7476 | if (DeclContext *DC = D->getDeclContext()) { |
| 7477 | if (isa<ClassTemplateSpecializationDecl>(DC)) |
| 7478 | return true; |
| 7479 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) |
| 7480 | return FD->isFunctionTemplateSpecialization(); |
| 7481 | } |
| 7482 | return false; |
| 7483 | } |
| 7484 | |
| Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7485 | // C99 6.5.8, C++ [expr.rel] |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7486 | QualType Sema::CheckCompareOperands(ExprResult &lex, ExprResult &rex, SourceLocation Loc, |
| Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7487 | unsigned OpaqueOpc, bool isRelational) { |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7488 | BinaryOperatorKind Opc = (BinaryOperatorKind) OpaqueOpc; |
| Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7489 | |
| Chris Lattner | 9a152e2 | 2009-12-05 05:40:13 +0000 | [diff] [blame] | 7490 | // Handle vector comparisons separately. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7491 | if (lex.get()->getType()->isVectorType() || rex.get()->getType()->isVectorType()) |
| Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7492 | return CheckVectorCompareOperands(lex, rex, Loc, isRelational); |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7493 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7494 | QualType lType = lex.get()->getType(); |
| 7495 | QualType rType = rex.get()->getType(); |
| Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 7496 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7497 | Expr *LHSStripped = lex.get()->IgnoreParenImpCasts(); |
| 7498 | Expr *RHSStripped = rex.get()->IgnoreParenImpCasts(); |
| Chandler Carruth | 712563b | 2011-02-17 08:37:06 +0000 | [diff] [blame] | 7499 | QualType LHSStrippedType = LHSStripped->getType(); |
| 7500 | QualType RHSStrippedType = RHSStripped->getType(); |
| 7501 | |
| Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 7502 | |
| 7503 | |
| Chandler Carruth | 712563b | 2011-02-17 08:37:06 +0000 | [diff] [blame] | 7504 | // Two different enums will raise a warning when compared. |
| 7505 | if (const EnumType *LHSEnumType = LHSStrippedType->getAs<EnumType>()) { |
| 7506 | if (const EnumType *RHSEnumType = RHSStrippedType->getAs<EnumType>()) { |
| 7507 | if (LHSEnumType->getDecl()->getIdentifier() && |
| 7508 | RHSEnumType->getDecl()->getIdentifier() && |
| 7509 | !Context.hasSameUnqualifiedType(LHSStrippedType, RHSStrippedType)) { |
| 7510 | Diag(Loc, diag::warn_comparison_of_mixed_enum_types) |
| 7511 | << LHSStrippedType << RHSStrippedType |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7512 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
| Chandler Carruth | 712563b | 2011-02-17 08:37:06 +0000 | [diff] [blame] | 7513 | } |
| 7514 | } |
| 7515 | } |
| 7516 | |
| Douglas Gregor | 4ffbad1 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 7517 | if (!lType->hasFloatingRepresentation() && |
| Ted Kremenek | 853734e | 2010-09-16 00:03:01 +0000 | [diff] [blame] | 7518 | !(lType->isBlockPointerType() && isRelational) && |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7519 | !lex.get()->getLocStart().isMacroID() && |
| 7520 | !rex.get()->getLocStart().isMacroID()) { |
| Chris Lattner | 222b8bd | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 7521 | // For non-floating point types, check for self-comparisons of the form |
| 7522 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 7523 | // often indicate logic errors in the program. |
| Chandler Carruth | 65a3818 | 2010-07-12 06:23:38 +0000 | [diff] [blame] | 7524 | // |
| 7525 | // NOTE: Don't warn about comparison expressions resulting from macro |
| 7526 | // expansion. Also don't warn about comparisons which are only self |
| 7527 | // comparisons within a template specialization. The warnings should catch |
| 7528 | // obvious cases in the definition of the template anyways. The idea is to |
| 7529 | // warn when the typed comparison operator will always evaluate to the same |
| 7530 | // result. |
| Chandler Carruth | 17773fc | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 7531 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LHSStripped)) { |
| Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7532 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RHSStripped)) { |
| Ted Kremenek | 853734e | 2010-09-16 00:03:01 +0000 | [diff] [blame] | 7533 | if (DRL->getDecl() == DRR->getDecl() && |
| Chandler Carruth | 17773fc | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 7534 | !IsWithinTemplateSpecialization(DRL->getDecl())) { |
| Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 7535 | DiagRuntimeBehavior(Loc, 0, PDiag(diag::warn_comparison_always) |
| Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7536 | << 0 // self- |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7537 | << (Opc == BO_EQ |
| 7538 | || Opc == BO_LE |
| 7539 | || Opc == BO_GE)); |
| Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7540 | } else if (lType->isArrayType() && rType->isArrayType() && |
| 7541 | !DRL->getDecl()->getType()->isReferenceType() && |
| 7542 | !DRR->getDecl()->getType()->isReferenceType()) { |
| 7543 | // what is it always going to eval to? |
| 7544 | char always_evals_to; |
| 7545 | switch(Opc) { |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7546 | case BO_EQ: // e.g. array1 == array2 |
| Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7547 | always_evals_to = 0; // false |
| 7548 | break; |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7549 | case BO_NE: // e.g. array1 != array2 |
| Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7550 | always_evals_to = 1; // true |
| 7551 | break; |
| 7552 | default: |
| 7553 | // best we can say is 'a constant' |
| 7554 | always_evals_to = 2; // e.g. array1 <= array2 |
| 7555 | break; |
| 7556 | } |
| Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 7557 | DiagRuntimeBehavior(Loc, 0, PDiag(diag::warn_comparison_always) |
| Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7558 | << 1 // array |
| 7559 | << always_evals_to); |
| 7560 | } |
| 7561 | } |
| Chandler Carruth | 17773fc | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 7562 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7563 | |
| Chris Lattner | 222b8bd | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 7564 | if (isa<CastExpr>(LHSStripped)) |
| 7565 | LHSStripped = LHSStripped->IgnoreParenCasts(); |
| 7566 | if (isa<CastExpr>(RHSStripped)) |
| 7567 | RHSStripped = RHSStripped->IgnoreParenCasts(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7568 | |
| Chris Lattner | 222b8bd | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 7569 | // Warn about comparisons against a string constant (unless the other |
| 7570 | // operand is null), the user probably wants strcmp. |
| Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7571 | Expr *literalString = 0; |
| 7572 | Expr *literalStringStripped = 0; |
| Chris Lattner | 222b8bd | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 7573 | if ((isa<StringLiteral>(LHSStripped) || isa<ObjCEncodeExpr>(LHSStripped)) && |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7574 | !RHSStripped->isNullPointerConstant(Context, |
| Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 7575 | Expr::NPC_ValueDependentIsNull)) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7576 | literalString = lex.get(); |
| Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7577 | literalStringStripped = LHSStripped; |
| Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 7578 | } else if ((isa<StringLiteral>(RHSStripped) || |
| 7579 | isa<ObjCEncodeExpr>(RHSStripped)) && |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7580 | !LHSStripped->isNullPointerConstant(Context, |
| Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 7581 | Expr::NPC_ValueDependentIsNull)) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7582 | literalString = rex.get(); |
| Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7583 | literalStringStripped = RHSStripped; |
| 7584 | } |
| 7585 | |
| 7586 | if (literalString) { |
| 7587 | std::string resultComparison; |
| 7588 | switch (Opc) { |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7589 | case BO_LT: resultComparison = ") < 0"; break; |
| 7590 | case BO_GT: resultComparison = ") > 0"; break; |
| 7591 | case BO_LE: resultComparison = ") <= 0"; break; |
| 7592 | case BO_GE: resultComparison = ") >= 0"; break; |
| 7593 | case BO_EQ: resultComparison = ") == 0"; break; |
| 7594 | case BO_NE: resultComparison = ") != 0"; break; |
| Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7595 | default: assert(false && "Invalid comparison operator"); |
| 7596 | } |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7597 | |
| Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 7598 | DiagRuntimeBehavior(Loc, 0, |
| Douglas Gregor | 49862b8 | 2010-01-12 23:18:54 +0000 | [diff] [blame] | 7599 | PDiag(diag::warn_stringcompare) |
| 7600 | << isa<ObjCEncodeExpr>(literalStringStripped) |
| Ted Kremenek | 800b66b | 2010-04-09 20:26:53 +0000 | [diff] [blame] | 7601 | << literalString->getSourceRange()); |
| Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7602 | } |
| Ted Kremenek | e451eae | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 7603 | } |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7604 | |
| Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7605 | // C99 6.5.8p3 / C99 6.5.9p4 |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7606 | if (lex.get()->getType()->isArithmeticType() && rex.get()->getType()->isArithmeticType()) { |
| Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7607 | UsualArithmeticConversions(lex, rex); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7608 | if (lex.isInvalid() || rex.isInvalid()) |
| 7609 | return QualType(); |
| 7610 | } |
| Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7611 | else { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7612 | lex = UsualUnaryConversions(lex.take()); |
| 7613 | if (lex.isInvalid()) |
| 7614 | return QualType(); |
| 7615 | |
| 7616 | rex = UsualUnaryConversions(rex.take()); |
| 7617 | if (rex.isInvalid()) |
| 7618 | return QualType(); |
| Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7619 | } |
| 7620 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7621 | lType = lex.get()->getType(); |
| 7622 | rType = rex.get()->getType(); |
| Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7623 | |
| Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7624 | // The result of comparisons is 'bool' in C++, 'int' in C. |
| Argyrios Kyrtzidis | 1bdd688 | 2011-02-18 20:55:15 +0000 | [diff] [blame] | 7625 | QualType ResultTy = Context.getLogicalOperationType(); |
| Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7626 | |
| Chris Lattner | b620c34 | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 7627 | if (isRelational) { |
| 7628 | if (lType->isRealType() && rType->isRealType()) |
| Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7629 | return ResultTy; |
| Chris Lattner | b620c34 | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 7630 | } else { |
| Ted Kremenek | e2763b0 | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 7631 | // Check for comparisons of floating point operands using != and ==. |
| Douglas Gregor | 4ffbad1 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 7632 | if (lType->hasFloatingRepresentation()) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7633 | CheckFloatComparison(Loc, lex.get(), rex.get()); |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7634 | |
| Chris Lattner | b620c34 | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 7635 | if (lType->isArithmeticType() && rType->isArithmeticType()) |
| Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7636 | return ResultTy; |
| Chris Lattner | b620c34 | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 7637 | } |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7638 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7639 | bool LHSIsNull = lex.get()->isNullPointerConstant(Context, |
| Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 7640 | Expr::NPC_ValueDependentIsNull); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7641 | bool RHSIsNull = rex.get()->isNullPointerConstant(Context, |
| Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 7642 | Expr::NPC_ValueDependentIsNull); |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7643 | |
| Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7644 | // All of the following pointer-related warnings are GCC extensions, except |
| 7645 | // when handling null pointer constants. |
| Steve Naroff | 808eb8f | 2007-08-27 04:08:11 +0000 | [diff] [blame] | 7646 | if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2 |
| Chris Lattner | 3a0702e | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 7647 | QualType LCanPointeeTy = |
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 7648 | Context.getCanonicalType(lType->getAs<PointerType>()->getPointeeType()); |
| Chris Lattner | 3a0702e | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 7649 | QualType RCanPointeeTy = |
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 7650 | Context.getCanonicalType(rType->getAs<PointerType>()->getPointeeType()); |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7651 | |
| Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7652 | if (getLangOptions().CPlusPlus) { |
| Eli Friedman | 16c20961 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 7653 | if (LCanPointeeTy == RCanPointeeTy) |
| 7654 | return ResultTy; |
| Fariborz Jahanian | ffc420c | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 7655 | if (!isRelational && |
| 7656 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 7657 | // Valid unless comparison between non-null pointer and function pointer |
| 7658 | // This is a gcc extension compatibility comparison. |
| Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7659 | // In a SFINAE context, we treat this as a hard error to maintain |
| 7660 | // conformance with the C++ standard. |
| Fariborz Jahanian | ffc420c | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 7661 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
| 7662 | && !LHSIsNull && !RHSIsNull) { |
| Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7663 | Diag(Loc, |
| 7664 | isSFINAEContext()? |
| 7665 | diag::err_typecheck_comparison_of_fptr_to_void |
| 7666 | : diag::ext_typecheck_comparison_of_fptr_to_void) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7667 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
| Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7668 | |
| 7669 | if (isSFINAEContext()) |
| 7670 | return QualType(); |
| 7671 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7672 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
| Fariborz Jahanian | ffc420c | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 7673 | return ResultTy; |
| 7674 | } |
| 7675 | } |
| Anders Carlsson | a95069c | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 7676 | |
| Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7677 | // C++ [expr.rel]p2: |
| 7678 | // [...] Pointer conversions (4.10) and qualification |
| 7679 | // conversions (4.4) are performed on pointer operands (or on |
| 7680 | // a pointer operand and a null pointer constant) to bring |
| 7681 | // them to their composite pointer type. [...] |
| 7682 | // |
| Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7683 | // C++ [expr.eq]p1 uses the same notion for (in)equality |
| Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7684 | // comparisons of pointers. |
| Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7685 | bool NonStandardCompositeType = false; |
| Douglas Gregor | 19175ff | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 7686 | QualType T = FindCompositePointerType(Loc, lex, rex, |
| Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7687 | isSFINAEContext()? 0 : &NonStandardCompositeType); |
| Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7688 | if (T.isNull()) { |
| 7689 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_pointers) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7690 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
| Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7691 | return QualType(); |
| Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7692 | } else if (NonStandardCompositeType) { |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7693 | Diag(Loc, |
| Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7694 | diag::ext_typecheck_comparison_of_distinct_pointers_nonstandard) |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7695 | << lType << rType << T |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7696 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
| Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7697 | } |
| 7698 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7699 | lex = ImpCastExprToType(lex.take(), T, CK_BitCast); |
| 7700 | rex = ImpCastExprToType(rex.take(), T, CK_BitCast); |
| Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7701 | return ResultTy; |
| 7702 | } |
| Eli Friedman | 16c20961 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 7703 | // C99 6.5.9p2 and C99 6.5.8p2 |
| 7704 | if (Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(), |
| 7705 | RCanPointeeTy.getUnqualifiedType())) { |
| 7706 | // Valid unless a relational comparison of function pointers |
| 7707 | if (isRelational && LCanPointeeTy->isFunctionType()) { |
| 7708 | Diag(Loc, diag::ext_typecheck_ordered_comparison_of_function_pointers) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7709 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
| Eli Friedman | 16c20961 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 7710 | } |
| 7711 | } else if (!isRelational && |
| 7712 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 7713 | // Valid unless comparison between non-null pointer and function pointer |
| 7714 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
| 7715 | && !LHSIsNull && !RHSIsNull) { |
| 7716 | Diag(Loc, diag::ext_typecheck_comparison_of_fptr_to_void) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7717 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
| Eli Friedman | 16c20961 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 7718 | } |
| 7719 | } else { |
| 7720 | // Invalid |
| Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 7721 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7722 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
| Steve Naroff | 75c1723 | 2007-06-13 21:41:08 +0000 | [diff] [blame] | 7723 | } |
| John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7724 | if (LCanPointeeTy != RCanPointeeTy) { |
| 7725 | if (LHSIsNull && !RHSIsNull) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7726 | lex = ImpCastExprToType(lex.take(), rType, CK_BitCast); |
| John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7727 | else |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7728 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
| John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7729 | } |
| Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7730 | return ResultTy; |
| Steve Naroff | cdee44c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 7731 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7732 | |
| Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 7733 | if (getLangOptions().CPlusPlus) { |
| Anders Carlsson | a95069c | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 7734 | // Comparison of nullptr_t with itself. |
| 7735 | if (lType->isNullPtrType() && rType->isNullPtrType()) |
| 7736 | return ResultTy; |
| 7737 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7738 | // Comparison of pointers with null pointer constants and equality |
| Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7739 | // comparisons of member pointers to null pointer constants. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7740 | if (RHSIsNull && |
| Douglas Gregor | 39e9fa9 | 2011-06-01 15:12:24 +0000 | [diff] [blame] | 7741 | ((lType->isAnyPointerType() || lType->isNullPtrType()) || |
| Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7742 | (!isRelational && lType->isMemberPointerType()))) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7743 | rex = ImpCastExprToType(rex.take(), lType, |
| Douglas Gregor | f58ff32 | 2010-08-07 13:36:37 +0000 | [diff] [blame] | 7744 | lType->isMemberPointerType() |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7745 | ? CK_NullToMemberPointer |
| John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7746 | : CK_NullToPointer); |
| Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 7747 | return ResultTy; |
| 7748 | } |
| Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7749 | if (LHSIsNull && |
| Douglas Gregor | 39e9fa9 | 2011-06-01 15:12:24 +0000 | [diff] [blame] | 7750 | ((rType->isAnyPointerType() || rType->isNullPtrType()) || |
| Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7751 | (!isRelational && rType->isMemberPointerType()))) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7752 | lex = ImpCastExprToType(lex.take(), rType, |
| Douglas Gregor | f58ff32 | 2010-08-07 13:36:37 +0000 | [diff] [blame] | 7753 | rType->isMemberPointerType() |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7754 | ? CK_NullToMemberPointer |
| John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7755 | : CK_NullToPointer); |
| Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 7756 | return ResultTy; |
| 7757 | } |
| Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7758 | |
| 7759 | // Comparison of member pointers. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7760 | if (!isRelational && |
| Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7761 | lType->isMemberPointerType() && rType->isMemberPointerType()) { |
| 7762 | // C++ [expr.eq]p2: |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7763 | // In addition, pointers to members can be compared, or a pointer to |
| 7764 | // member and a null pointer constant. Pointer to member conversions |
| 7765 | // (4.11) and qualification conversions (4.4) are performed to bring |
| 7766 | // them to a common type. If one operand is a null pointer constant, |
| 7767 | // the common type is the type of the other operand. Otherwise, the |
| 7768 | // common type is a pointer to member type similar (4.4) to the type |
| 7769 | // of one of the operands, with a cv-qualification signature (4.4) |
| 7770 | // that is the union of the cv-qualification signatures of the operand |
| Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7771 | // types. |
| Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7772 | bool NonStandardCompositeType = false; |
| Douglas Gregor | 19175ff | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 7773 | QualType T = FindCompositePointerType(Loc, lex, rex, |
| Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7774 | isSFINAEContext()? 0 : &NonStandardCompositeType); |
| Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7775 | if (T.isNull()) { |
| 7776 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_pointers) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7777 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
| Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7778 | return QualType(); |
| Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7779 | } else if (NonStandardCompositeType) { |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7780 | Diag(Loc, |
| Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7781 | diag::ext_typecheck_comparison_of_distinct_pointers_nonstandard) |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7782 | << lType << rType << T |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7783 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
| Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7784 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7785 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7786 | lex = ImpCastExprToType(lex.take(), T, CK_BitCast); |
| 7787 | rex = ImpCastExprToType(rex.take(), T, CK_BitCast); |
| Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7788 | return ResultTy; |
| 7789 | } |
| Douglas Gregor | 48e6bbf | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 7790 | |
| 7791 | // Handle scoped enumeration types specifically, since they don't promote |
| 7792 | // to integers. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7793 | if (lex.get()->getType()->isEnumeralType() && |
| 7794 | Context.hasSameUnqualifiedType(lex.get()->getType(), rex.get()->getType())) |
| Douglas Gregor | 48e6bbf | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 7795 | return ResultTy; |
| Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 7796 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7797 | |
| Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7798 | // Handle block pointer types. |
| Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 7799 | if (!isRelational && lType->isBlockPointerType() && rType->isBlockPointerType()) { |
| Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 7800 | QualType lpointee = lType->getAs<BlockPointerType>()->getPointeeType(); |
| 7801 | QualType rpointee = rType->getAs<BlockPointerType>()->getPointeeType(); |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7802 | |
| Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7803 | if (!LHSIsNull && !RHSIsNull && |
| Eli Friedman | a6638ca | 2009-06-08 05:08:54 +0000 | [diff] [blame] | 7804 | !Context.typesAreCompatible(lpointee, rpointee)) { |
| Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 7805 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7806 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
| Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7807 | } |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7808 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
| Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7809 | return ResultTy; |
| Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7810 | } |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7811 | |
| Steve Naroff | e18f94c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 7812 | // Allow block pointers to be compared with null pointer constants. |
| Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 7813 | if (!isRelational |
| 7814 | && ((lType->isBlockPointerType() && rType->isPointerType()) |
| 7815 | || (lType->isPointerType() && rType->isBlockPointerType()))) { |
| Steve Naroff | e18f94c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 7816 | if (!LHSIsNull && !RHSIsNull) { |
| John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7817 | if (!((rType->isPointerType() && rType->castAs<PointerType>() |
| Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 7818 | ->getPointeeType()->isVoidType()) |
| John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7819 | || (lType->isPointerType() && lType->castAs<PointerType>() |
| Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 7820 | ->getPointeeType()->isVoidType()))) |
| 7821 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7822 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
| Steve Naroff | e18f94c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 7823 | } |
| John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7824 | if (LHSIsNull && !RHSIsNull) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7825 | lex = ImpCastExprToType(lex.take(), rType, CK_BitCast); |
| John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7826 | else |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7827 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
| Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7828 | return ResultTy; |
| Steve Naroff | e18f94c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 7829 | } |
| Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7830 | |
| John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7831 | if (lType->isObjCObjectPointerType() || rType->isObjCObjectPointerType()) { |
| 7832 | const PointerType *LPT = lType->getAs<PointerType>(); |
| 7833 | const PointerType *RPT = rType->getAs<PointerType>(); |
| 7834 | if (LPT || RPT) { |
| 7835 | bool LPtrToVoid = LPT ? LPT->getPointeeType()->isVoidType() : false; |
| 7836 | bool RPtrToVoid = RPT ? RPT->getPointeeType()->isVoidType() : false; |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7837 | |
| Steve Naroff | 753567f | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 7838 | if (!LPtrToVoid && !RPtrToVoid && |
| 7839 | !Context.typesAreCompatible(lType, rType)) { |
| Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 7840 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7841 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
| Steve Naroff | 1d4a9a3 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 7842 | } |
| John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7843 | if (LHSIsNull && !RHSIsNull) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7844 | lex = ImpCastExprToType(lex.take(), rType, CK_BitCast); |
| John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7845 | else |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7846 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
| Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7847 | return ResultTy; |
| Steve Naroff | ea54d9e | 2008-10-20 18:19:10 +0000 | [diff] [blame] | 7848 | } |
| Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7849 | if (lType->isObjCObjectPointerType() && rType->isObjCObjectPointerType()) { |
| Chris Lattner | f8344db | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 7850 | if (!Context.areComparableObjCPointerTypes(lType, rType)) |
| Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7851 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7852 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
| John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7853 | if (LHSIsNull && !RHSIsNull) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7854 | lex = ImpCastExprToType(lex.take(), rType, CK_BitCast); |
| John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7855 | else |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7856 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
| Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7857 | return ResultTy; |
| Steve Naroff | b788d9b | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 7858 | } |
| Fariborz Jahanian | 134cbef | 2007-12-20 01:06:58 +0000 | [diff] [blame] | 7859 | } |
| Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7860 | if ((lType->isAnyPointerType() && rType->isIntegerType()) || |
| 7861 | (lType->isIntegerType() && rType->isAnyPointerType())) { |
| Chris Lattner | d99bd52 | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7862 | unsigned DiagID = 0; |
| Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7863 | bool isError = false; |
| 7864 | if ((LHSIsNull && lType->isIntegerType()) || |
| 7865 | (RHSIsNull && rType->isIntegerType())) { |
| 7866 | if (isRelational && !getLangOptions().CPlusPlus) |
| Chris Lattner | d99bd52 | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7867 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_and_zero; |
| Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7868 | } else if (isRelational && !getLangOptions().CPlusPlus) |
| Chris Lattner | d99bd52 | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7869 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_integer; |
| Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7870 | else if (getLangOptions().CPlusPlus) { |
| 7871 | DiagID = diag::err_typecheck_comparison_of_pointer_integer; |
| 7872 | isError = true; |
| 7873 | } else |
| Chris Lattner | d99bd52 | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7874 | DiagID = diag::ext_typecheck_comparison_of_pointer_integer; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7875 | |
| Chris Lattner | d99bd52 | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7876 | if (DiagID) { |
| Chris Lattner | f8344db | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 7877 | Diag(Loc, DiagID) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7878 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
| Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7879 | if (isError) |
| 7880 | return QualType(); |
| Chris Lattner | f8344db | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 7881 | } |
| Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7882 | |
| 7883 | if (lType->isIntegerType()) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7884 | lex = ImpCastExprToType(lex.take(), rType, |
| John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7885 | LHSIsNull ? CK_NullToPointer : CK_IntegralToPointer); |
| Chris Lattner | d99bd52 | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7886 | else |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7887 | rex = ImpCastExprToType(rex.take(), lType, |
| John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7888 | RHSIsNull ? CK_NullToPointer : CK_IntegralToPointer); |
| Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7889 | return ResultTy; |
| Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 7890 | } |
| Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7891 | |
| Steve Naroff | 4b19157 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 7892 | // Handle block pointers. |
| Mike Stump | f70bcf7 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 7893 | if (!isRelational && RHSIsNull |
| 7894 | && lType->isBlockPointerType() && rType->isIntegerType()) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7895 | rex = ImpCastExprToType(rex.take(), lType, CK_NullToPointer); |
| Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7896 | return ResultTy; |
| Steve Naroff | 4b19157 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 7897 | } |
| Mike Stump | f70bcf7 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 7898 | if (!isRelational && LHSIsNull |
| 7899 | && lType->isIntegerType() && rType->isBlockPointerType()) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7900 | lex = ImpCastExprToType(lex.take(), rType, CK_NullToPointer); |
| Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7901 | return ResultTy; |
| Steve Naroff | 4b19157 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 7902 | } |
| Douglas Gregor | 48e6bbf | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 7903 | |
| Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7904 | return InvalidOperands(Loc, lex, rex); |
| Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 7905 | } |
| 7906 | |
| Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7907 | /// CheckVectorCompareOperands - vector comparisons are a clang extension that |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7908 | /// operates on extended vector types. Instead of producing an IntTy result, |
| Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7909 | /// like a scalar comparison, a vector comparison produces a vector of integer |
| 7910 | /// types. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7911 | QualType Sema::CheckVectorCompareOperands(ExprResult &lex, ExprResult &rex, |
| Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7912 | SourceLocation Loc, |
| Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7913 | bool isRelational) { |
| 7914 | // Check to make sure we're operating on vectors of the same type and width, |
| 7915 | // Allowing one side to be a scalar of element type. |
| Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7916 | QualType vType = CheckVectorOperands(Loc, lex, rex); |
| Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7917 | if (vType.isNull()) |
| 7918 | return vType; |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7919 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7920 | QualType lType = lex.get()->getType(); |
| 7921 | QualType rType = rex.get()->getType(); |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7922 | |
| Anton Yartsev | 530deb9 | 2011-03-27 15:36:07 +0000 | [diff] [blame] | 7923 | // If AltiVec, the comparison results in a numeric type, i.e. |
| 7924 | // bool for C++, int for C |
| Anton Yartsev | 93900c7 | 2011-03-28 21:00:05 +0000 | [diff] [blame] | 7925 | if (vType->getAs<VectorType>()->getVectorKind() == VectorType::AltiVecVector) |
| Anton Yartsev | 530deb9 | 2011-03-27 15:36:07 +0000 | [diff] [blame] | 7926 | return Context.getLogicalOperationType(); |
| 7927 | |
| Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7928 | // For non-floating point types, check for self-comparisons of the form |
| 7929 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 7930 | // often indicate logic errors in the program. |
| Douglas Gregor | 4ffbad1 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 7931 | if (!lType->hasFloatingRepresentation()) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7932 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex.get()->IgnoreParens())) |
| 7933 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex.get()->IgnoreParens())) |
| Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7934 | if (DRL->getDecl() == DRR->getDecl()) |
| Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 7935 | DiagRuntimeBehavior(Loc, 0, |
| Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7936 | PDiag(diag::warn_comparison_always) |
| 7937 | << 0 // self- |
| 7938 | << 2 // "a constant" |
| 7939 | ); |
| Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7940 | } |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7941 | |
| Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7942 | // Check for comparisons of floating point operands using != and ==. |
| Douglas Gregor | 4ffbad1 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 7943 | if (!isRelational && lType->hasFloatingRepresentation()) { |
| 7944 | assert (rType->hasFloatingRepresentation()); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7945 | CheckFloatComparison(Loc, lex.get(), rex.get()); |
| Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7946 | } |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7947 | |
| Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7948 | // Return the type for the comparison, which is the same as vector type for |
| 7949 | // integer vectors, or an integer type of identical size and number of |
| 7950 | // elements for floating point vectors. |
| Douglas Gregor | 5cc2c8b | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 7951 | if (lType->hasIntegerRepresentation()) |
| Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7952 | return lType; |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7953 | |
| John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 7954 | const VectorType *VTy = lType->getAs<VectorType>(); |
| Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7955 | unsigned TypeSize = Context.getTypeSize(VTy->getElementType()); |
| Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 7956 | if (TypeSize == Context.getTypeSize(Context.IntTy)) |
| Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7957 | return Context.getExtVectorType(Context.IntTy, VTy->getNumElements()); |
| Chris Lattner | 5d68896 | 2009-03-31 07:46:52 +0000 | [diff] [blame] | 7958 | if (TypeSize == Context.getTypeSize(Context.LongTy)) |
| Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 7959 | return Context.getExtVectorType(Context.LongTy, VTy->getNumElements()); |
| 7960 | |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7961 | assert(TypeSize == Context.getTypeSize(Context.LongLongTy) && |
| Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 7962 | "Unhandled vector element size in vector compare"); |
| Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7963 | return Context.getExtVectorType(Context.LongLongTy, VTy->getNumElements()); |
| 7964 | } |
| 7965 | |
| Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 7966 | inline QualType Sema::CheckBitwiseOperands( |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7967 | ExprResult &lex, ExprResult &rex, SourceLocation Loc, bool isCompAssign) { |
| 7968 | if (lex.get()->getType()->isVectorType() || rex.get()->getType()->isVectorType()) { |
| 7969 | if (lex.get()->getType()->hasIntegerRepresentation() && |
| 7970 | rex.get()->getType()->hasIntegerRepresentation()) |
| Douglas Gregor | 5cc2c8b | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 7971 | return CheckVectorOperands(Loc, lex, rex); |
| 7972 | |
| 7973 | return InvalidOperands(Loc, lex, rex); |
| 7974 | } |
| Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 7975 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7976 | ExprResult lexResult = Owned(lex), rexResult = Owned(rex); |
| 7977 | QualType compType = UsualArithmeticConversions(lexResult, rexResult, isCompAssign); |
| 7978 | if (lexResult.isInvalid() || rexResult.isInvalid()) |
| 7979 | return QualType(); |
| 7980 | lex = lexResult.take(); |
| 7981 | rex = rexResult.take(); |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7982 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7983 | if (lex.get()->getType()->isIntegralOrUnscopedEnumerationType() && |
| 7984 | rex.get()->getType()->isIntegralOrUnscopedEnumerationType()) |
| Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 7985 | return compType; |
| Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7986 | return InvalidOperands(Loc, lex, rex); |
| Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 7987 | } |
| 7988 | |
| Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 7989 | inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7990 | ExprResult &lex, ExprResult &rex, SourceLocation Loc, unsigned Opc) { |
| Chris Lattner | 8406c51 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 7991 | |
| 7992 | // Diagnose cases where the user write a logical and/or but probably meant a |
| 7993 | // bitwise one. We do this when the LHS is a non-bool integer and the RHS |
| 7994 | // is a constant. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7995 | if (lex.get()->getType()->isIntegerType() && !lex.get()->getType()->isBooleanType() && |
| 7996 | rex.get()->getType()->isIntegerType() && !rex.get()->isValueDependent() && |
| Chris Lattner | deee7a3 | 2010-07-15 00:26:43 +0000 | [diff] [blame] | 7997 | // Don't warn in macros. |
| Chris Lattner | 938533d | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 7998 | !Loc.isMacroID()) { |
| 7999 | // If the RHS can be constant folded, and if it constant folds to something |
| 8000 | // that isn't 0 or 1 (which indicate a potential logical operation that |
| 8001 | // happened to fold to true/false) then warn. |
| Chandler Carruth | e54ff6c | 2011-05-31 05:41:42 +0000 | [diff] [blame] | 8002 | // Parens on the RHS are ignored. |
| Chris Lattner | 938533d | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 8003 | Expr::EvalResult Result; |
| Chandler Carruth | e54ff6c | 2011-05-31 05:41:42 +0000 | [diff] [blame] | 8004 | if (rex.get()->Evaluate(Result, Context) && !Result.HasSideEffects) |
| 8005 | if ((getLangOptions().Bool && !rex.get()->getType()->isBooleanType()) || |
| 8006 | (Result.Val.getInt() != 0 && Result.Val.getInt() != 1)) { |
| 8007 | Diag(Loc, diag::warn_logical_instead_of_bitwise) |
| 8008 | << rex.get()->getSourceRange() |
| 8009 | << (Opc == BO_LAnd ? "&&" : "||") |
| 8010 | << (Opc == BO_LAnd ? "&" : "|"); |
| Chris Lattner | 938533d | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 8011 | } |
| 8012 | } |
| Chris Lattner | 8406c51 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 8013 | |
| Anders Carlsson | 2e7bc11 | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 8014 | if (!Context.getLangOptions().CPlusPlus) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8015 | lex = UsualUnaryConversions(lex.take()); |
| 8016 | if (lex.isInvalid()) |
| 8017 | return QualType(); |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8018 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8019 | rex = UsualUnaryConversions(rex.take()); |
| 8020 | if (rex.isInvalid()) |
| 8021 | return QualType(); |
| 8022 | |
| 8023 | if (!lex.get()->getType()->isScalarType() || !rex.get()->getType()->isScalarType()) |
| Anders Carlsson | 2e7bc11 | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 8024 | return InvalidOperands(Loc, lex, rex); |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8025 | |
| Anders Carlsson | 2e7bc11 | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 8026 | return Context.IntTy; |
| Anders Carlsson | 35a99d9 | 2009-10-16 01:44:21 +0000 | [diff] [blame] | 8027 | } |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8028 | |
| John McCall | 4a2429a | 2010-06-04 00:29:51 +0000 | [diff] [blame] | 8029 | // The following is safe because we only use this method for |
| 8030 | // non-overloadable operands. |
| 8031 | |
| Anders Carlsson | 2e7bc11 | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 8032 | // C++ [expr.log.and]p1 |
| 8033 | // C++ [expr.log.or]p1 |
| John McCall | 4a2429a | 2010-06-04 00:29:51 +0000 | [diff] [blame] | 8034 | // The operands are both contextually converted to type bool. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8035 | ExprResult lexRes = PerformContextuallyConvertToBool(lex.get()); |
| 8036 | if (lexRes.isInvalid()) |
| Anders Carlsson | 2e7bc11 | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 8037 | return InvalidOperands(Loc, lex, rex); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8038 | lex = move(lexRes); |
| 8039 | |
| 8040 | ExprResult rexRes = PerformContextuallyConvertToBool(rex.get()); |
| 8041 | if (rexRes.isInvalid()) |
| 8042 | return InvalidOperands(Loc, lex, rex); |
| 8043 | rex = move(rexRes); |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8044 | |
| Anders Carlsson | 2e7bc11 | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 8045 | // C++ [expr.log.and]p2 |
| 8046 | // C++ [expr.log.or]p2 |
| 8047 | // The result is a bool. |
| 8048 | return Context.BoolTy; |
| Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 8049 | } |
| 8050 | |
| Fariborz Jahanian | 8e1555c | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 8051 | /// IsReadonlyProperty - Verify that otherwise a valid l-value expression |
| 8052 | /// is a read-only property; return true if so. A readonly property expression |
| 8053 | /// depends on various declarations and thus must be treated specially. |
| 8054 | /// |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8055 | static bool IsReadonlyProperty(Expr *E, Sema &S) { |
| Fariborz Jahanian | 8e1555c | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 8056 | if (E->getStmtClass() == Expr::ObjCPropertyRefExprClass) { |
| 8057 | const ObjCPropertyRefExpr* PropExpr = cast<ObjCPropertyRefExpr>(E); |
| John McCall | b7bd14f | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 8058 | if (PropExpr->isImplicitProperty()) return false; |
| 8059 | |
| 8060 | ObjCPropertyDecl *PDecl = PropExpr->getExplicitProperty(); |
| 8061 | QualType BaseType = PropExpr->isSuperReceiver() ? |
| 8062 | PropExpr->getSuperReceiverType() : |
| Fariborz Jahanian | 681c075 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 8063 | PropExpr->getBase()->getType(); |
| 8064 | |
| John McCall | b7bd14f | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 8065 | if (const ObjCObjectPointerType *OPT = |
| 8066 | BaseType->getAsObjCInterfacePointerType()) |
| 8067 | if (ObjCInterfaceDecl *IFace = OPT->getInterfaceDecl()) |
| 8068 | if (S.isPropertyReadonly(PDecl, IFace)) |
| 8069 | return true; |
| Fariborz Jahanian | 8e1555c | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 8070 | } |
| 8071 | return false; |
| 8072 | } |
| 8073 | |
| Fariborz Jahanian | b24b568 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 8074 | static bool IsConstProperty(Expr *E, Sema &S) { |
| 8075 | if (E->getStmtClass() == Expr::ObjCPropertyRefExprClass) { |
| 8076 | const ObjCPropertyRefExpr* PropExpr = cast<ObjCPropertyRefExpr>(E); |
| 8077 | if (PropExpr->isImplicitProperty()) return false; |
| 8078 | |
| 8079 | ObjCPropertyDecl *PDecl = PropExpr->getExplicitProperty(); |
| 8080 | QualType T = PDecl->getType(); |
| 8081 | if (T->isReferenceType()) |
| Fariborz Jahanian | 20688cc | 2011-03-30 16:59:30 +0000 | [diff] [blame] | 8082 | T = T->getAs<ReferenceType>()->getPointeeType(); |
| Fariborz Jahanian | b24b568 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 8083 | CanQualType CT = S.Context.getCanonicalType(T); |
| 8084 | return CT.isConstQualified(); |
| 8085 | } |
| 8086 | return false; |
| 8087 | } |
| 8088 | |
| Fariborz Jahanian | 071caef | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 8089 | static bool IsReadonlyMessage(Expr *E, Sema &S) { |
| 8090 | if (E->getStmtClass() != Expr::MemberExprClass) |
| 8091 | return false; |
| 8092 | const MemberExpr *ME = cast<MemberExpr>(E); |
| 8093 | NamedDecl *Member = ME->getMemberDecl(); |
| 8094 | if (isa<FieldDecl>(Member)) { |
| 8095 | Expr *Base = ME->getBase()->IgnoreParenImpCasts(); |
| 8096 | if (Base->getStmtClass() != Expr::ObjCMessageExprClass) |
| 8097 | return false; |
| 8098 | return cast<ObjCMessageExpr>(Base)->getMethodDecl() != 0; |
| 8099 | } |
| 8100 | return false; |
| 8101 | } |
| 8102 | |
| Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8103 | /// CheckForModifiableLvalue - Verify that E is a modifiable lvalue. If not, |
| 8104 | /// emit an error and return true. If so, return false. |
| 8105 | static bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S) { |
| Daniel Dunbar | c2223ab | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 8106 | SourceLocation OrigLoc = Loc; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8107 | Expr::isModifiableLvalueResult IsLV = E->isModifiableLvalue(S.Context, |
| Daniel Dunbar | c2223ab | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 8108 | &Loc); |
| Fariborz Jahanian | 8e1555c | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 8109 | if (IsLV == Expr::MLV_Valid && IsReadonlyProperty(E, S)) |
| 8110 | IsLV = Expr::MLV_ReadonlyProperty; |
| Fariborz Jahanian | b24b568 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 8111 | else if (Expr::MLV_ConstQualified && IsConstProperty(E, S)) |
| 8112 | IsLV = Expr::MLV_Valid; |
| Fariborz Jahanian | 071caef | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 8113 | else if (IsLV == Expr::MLV_ClassTemporary && IsReadonlyMessage(E, S)) |
| 8114 | IsLV = Expr::MLV_InvalidMessageExpression; |
| Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8115 | if (IsLV == Expr::MLV_Valid) |
| 8116 | return false; |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8117 | |
| Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8118 | unsigned Diag = 0; |
| 8119 | bool NeedType = false; |
| 8120 | switch (IsLV) { // C99 6.5.16p2 |
| Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8121 | case Expr::MLV_ConstQualified: Diag = diag::err_typecheck_assign_const; break; |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8122 | case Expr::MLV_ArrayType: |
| Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8123 | Diag = diag::err_typecheck_array_not_modifiable_lvalue; |
| 8124 | NeedType = true; |
| 8125 | break; |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8126 | case Expr::MLV_NotObjectType: |
| Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8127 | Diag = diag::err_typecheck_non_object_not_modifiable_lvalue; |
| 8128 | NeedType = true; |
| 8129 | break; |
| Chris Lattner | 9b3bbe9 | 2008-11-17 19:51:54 +0000 | [diff] [blame] | 8130 | case Expr::MLV_LValueCast: |
| Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8131 | Diag = diag::err_typecheck_lvalue_casts_not_supported; |
| 8132 | break; |
| Douglas Gregor | b154fdc | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 8133 | case Expr::MLV_Valid: |
| 8134 | llvm_unreachable("did not take early return for MLV_Valid"); |
| Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8135 | case Expr::MLV_InvalidExpression: |
| Douglas Gregor | b154fdc | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 8136 | case Expr::MLV_MemberFunction: |
| 8137 | case Expr::MLV_ClassTemporary: |
| Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8138 | Diag = diag::err_typecheck_expression_not_modifiable_lvalue; |
| 8139 | break; |
| Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8140 | case Expr::MLV_IncompleteType: |
| 8141 | case Expr::MLV_IncompleteVoidType: |
| Douglas Gregor | ed0cfbd | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 8142 | return S.RequireCompleteType(Loc, E->getType(), |
| Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 8143 | S.PDiag(diag::err_typecheck_incomplete_type_not_modifiable_lvalue) |
| Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 8144 | << E->getSourceRange()); |
| Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8145 | case Expr::MLV_DuplicateVectorComponents: |
| Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8146 | Diag = diag::err_typecheck_duplicate_vector_components_not_mlvalue; |
| 8147 | break; |
| Steve Naroff | ba756cb | 2008-09-26 14:41:28 +0000 | [diff] [blame] | 8148 | case Expr::MLV_NotBlockQualified: |
| Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8149 | Diag = diag::err_block_decl_ref_not_modifiable_lvalue; |
| 8150 | break; |
| Fariborz Jahanian | 8a1810f | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 8151 | case Expr::MLV_ReadonlyProperty: |
| 8152 | Diag = diag::error_readonly_property_assignment; |
| 8153 | break; |
| Fariborz Jahanian | 5118c41 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 8154 | case Expr::MLV_NoSetterProperty: |
| 8155 | Diag = diag::error_nosetter_property_assignment; |
| 8156 | break; |
| Fariborz Jahanian | 071caef | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 8157 | case Expr::MLV_InvalidMessageExpression: |
| 8158 | Diag = diag::error_readonly_message_assignment; |
| 8159 | break; |
| Fariborz Jahanian | e8d2890 | 2009-12-15 23:59:41 +0000 | [diff] [blame] | 8160 | case Expr::MLV_SubObjCPropertySetting: |
| 8161 | Diag = diag::error_no_subobject_property_setting; |
| 8162 | break; |
| Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 8163 | } |
| Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 8164 | |
| Daniel Dunbar | c2223ab | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 8165 | SourceRange Assign; |
| 8166 | if (Loc != OrigLoc) |
| 8167 | Assign = SourceRange(OrigLoc, OrigLoc); |
| Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8168 | if (NeedType) |
| Daniel Dunbar | c2223ab | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 8169 | S.Diag(Loc, Diag) << E->getType() << E->getSourceRange() << Assign; |
| Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8170 | else |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8171 | S.Diag(Loc, Diag) << E->getSourceRange() << Assign; |
| Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8172 | return true; |
| 8173 | } |
| 8174 | |
| 8175 | |
| 8176 | |
| 8177 | // C99 6.5.16.1 |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8178 | QualType Sema::CheckAssignmentOperands(Expr *LHS, ExprResult &RHS, |
| Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 8179 | SourceLocation Loc, |
| 8180 | QualType CompoundType) { |
| 8181 | // Verify that LHS is a modifiable lvalue, and emit error if not. |
| 8182 | if (CheckForModifiableLvalue(LHS, Loc, *this)) |
| Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8183 | return QualType(); |
| Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 8184 | |
| 8185 | QualType LHSType = LHS->getType(); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8186 | QualType RHSType = CompoundType.isNull() ? RHS.get()->getType() : CompoundType; |
| Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8187 | AssignConvertType ConvTy; |
| Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 8188 | if (CompoundType.isNull()) { |
| Fariborz Jahanian | be21aa3 | 2010-06-07 22:02:01 +0000 | [diff] [blame] | 8189 | QualType LHSTy(LHSType); |
| Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 8190 | // Simple assignment "x = y". |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8191 | if (LHS->getObjectKind() == OK_ObjCProperty) { |
| 8192 | ExprResult LHSResult = Owned(LHS); |
| 8193 | ConvertPropertyForLValue(LHSResult, RHS, LHSTy); |
| 8194 | if (LHSResult.isInvalid()) |
| 8195 | return QualType(); |
| 8196 | LHS = LHSResult.take(); |
| 8197 | } |
| Fariborz Jahanian | be21aa3 | 2010-06-07 22:02:01 +0000 | [diff] [blame] | 8198 | ConvTy = CheckSingleAssignmentConstraints(LHSTy, RHS); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8199 | if (RHS.isInvalid()) |
| 8200 | return QualType(); |
| Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 8201 | // Special case of NSObject attributes on c-style pointer types. |
| 8202 | if (ConvTy == IncompatiblePointer && |
| 8203 | ((Context.isObjCNSObjectType(LHSType) && |
| Steve Naroff | 79d1215 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 8204 | RHSType->isObjCObjectPointerType()) || |
| Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 8205 | (Context.isObjCNSObjectType(RHSType) && |
| Steve Naroff | 79d1215 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 8206 | LHSType->isObjCObjectPointerType()))) |
| Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 8207 | ConvTy = Compatible; |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8208 | |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8209 | if (ConvTy == Compatible && |
| 8210 | getLangOptions().ObjCNonFragileABI && |
| 8211 | LHSType->isObjCObjectType()) |
| 8212 | Diag(Loc, diag::err_assignment_requires_nonfragile_object) |
| 8213 | << LHSType; |
| 8214 | |
| Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 8215 | // If the RHS is a unary plus or minus, check to see if they = and + are |
| 8216 | // right next to each other. If so, the user may have typo'd "x =+ 4" |
| 8217 | // instead of "x += 4". |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8218 | Expr *RHSCheck = RHS.get(); |
| Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 8219 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RHSCheck)) |
| 8220 | RHSCheck = ICE->getSubExpr(); |
| 8221 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(RHSCheck)) { |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8222 | if ((UO->getOpcode() == UO_Plus || |
| 8223 | UO->getOpcode() == UO_Minus) && |
| Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 8224 | Loc.isFileID() && UO->getOperatorLoc().isFileID() && |
| Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 8225 | // Only if the two operators are exactly adjacent. |
| Chris Lattner | 36c39c9 | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 8226 | Loc.getFileLocWithOffset(1) == UO->getOperatorLoc() && |
| 8227 | // And there is a space or other character before the subexpr of the |
| 8228 | // unary +/-. We don't want to warn on "x=-1". |
| Chris Lattner | ed9f14c | 2009-03-09 07:11:10 +0000 | [diff] [blame] | 8229 | Loc.getFileLocWithOffset(2) != UO->getSubExpr()->getLocStart() && |
| 8230 | UO->getSubExpr()->getLocStart().isFileID()) { |
| Chris Lattner | 29e812b | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 8231 | Diag(Loc, diag::warn_not_compound_assign) |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8232 | << (UO->getOpcode() == UO_Plus ? "+" : "-") |
| Chris Lattner | 29e812b | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 8233 | << SourceRange(UO->getOperatorLoc(), UO->getOperatorLoc()); |
| Chris Lattner | 36c39c9 | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 8234 | } |
| Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 8235 | } |
| 8236 | } else { |
| 8237 | // Compound assignment "x += y" |
| Douglas Gregor | c03a108 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 8238 | ConvTy = CheckAssignmentConstraints(Loc, LHSType, RHSType); |
| Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 8239 | } |
| Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8240 | |
| Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 8241 | if (DiagnoseAssignmentResult(ConvTy, Loc, LHSType, RHSType, |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8242 | RHS.get(), AA_Assigning)) |
| Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8243 | return QualType(); |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8244 | |
| Argyrios Kyrtzidis | a9b630e | 2011-04-26 17:41:22 +0000 | [diff] [blame] | 8245 | CheckForNullPointerDereference(*this, LHS); |
| Ted Kremenek | 64699be | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 8246 | // Check for trivial buffer overflows. |
| Ted Kremenek | df26df7 | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 8247 | CheckArrayAccess(LHS->IgnoreParenCasts()); |
| Ted Kremenek | 64699be | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 8248 | |
| Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 8249 | // C99 6.5.16p3: The type of an assignment expression is the type of the |
| 8250 | // left operand unless the left operand has qualified type, in which case |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8251 | // it is the unqualified version of the type of the left operand. |
| Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 8252 | // C99 6.5.16.1p2: In simple assignment, the value of the right operand |
| 8253 | // is converted to the type of the assignment expression (above). |
| Chris Lattner | f17bd42 | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 8254 | // C++ 5.17p1: the type of the assignment expression is that of its left |
| Douglas Gregor | d2c2d17 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 8255 | // operand. |
| John McCall | 01cbf2d | 2010-10-12 02:19:57 +0000 | [diff] [blame] | 8256 | return (getLangOptions().CPlusPlus |
| 8257 | ? LHSType : LHSType.getUnqualifiedType()); |
| Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 8258 | } |
| 8259 | |
| Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 8260 | // C99 6.5.17 |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8261 | static QualType CheckCommaOperands(Sema &S, ExprResult &LHS, ExprResult &RHS, |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8262 | SourceLocation Loc) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8263 | S.DiagnoseUnusedExprResult(LHS.get()); |
| Argyrios Kyrtzidis | 639ffb0 | 2010-06-30 10:53:14 +0000 | [diff] [blame] | 8264 | |
| John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8265 | LHS = S.CheckPlaceholderExpr(LHS.take()); |
| 8266 | RHS = S.CheckPlaceholderExpr(RHS.take()); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8267 | if (LHS.isInvalid() || RHS.isInvalid()) |
| Douglas Gregor | 0124e9b | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 8268 | return QualType(); |
| 8269 | |
| John McCall | 73d3618 | 2010-10-12 07:14:40 +0000 | [diff] [blame] | 8270 | // C's comma performs lvalue conversion (C99 6.3.2.1) on both its |
| 8271 | // operands, but not unary promotions. |
| 8272 | // C++'s comma does not do any conversions at all (C++ [expr.comma]p1). |
| Eli Friedman | ba961a9 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 8273 | |
| John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8274 | // So we treat the LHS as a ignored value, and in C++ we allow the |
| 8275 | // containing site to determine what should be done with the RHS. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8276 | LHS = S.IgnoredValueConversions(LHS.take()); |
| 8277 | if (LHS.isInvalid()) |
| 8278 | return QualType(); |
| John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8279 | |
| 8280 | if (!S.getLangOptions().CPlusPlus) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8281 | RHS = S.DefaultFunctionArrayLvalueConversion(RHS.take()); |
| 8282 | if (RHS.isInvalid()) |
| 8283 | return QualType(); |
| 8284 | if (!RHS.get()->getType()->isVoidType()) |
| 8285 | S.RequireCompleteType(Loc, RHS.get()->getType(), diag::err_incomplete_type); |
| John McCall | 73d3618 | 2010-10-12 07:14:40 +0000 | [diff] [blame] | 8286 | } |
| Eli Friedman | ba961a9 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 8287 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8288 | return RHS.get()->getType(); |
| Steve Naroff | 95af013 | 2007-03-30 23:47:58 +0000 | [diff] [blame] | 8289 | } |
| 8290 | |
| Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 8291 | /// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine |
| 8292 | /// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions. |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8293 | static QualType CheckIncrementDecrementOperand(Sema &S, Expr *Op, |
| 8294 | ExprValueKind &VK, |
| 8295 | SourceLocation OpLoc, |
| 8296 | bool isInc, bool isPrefix) { |
| Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8297 | if (Op->isTypeDependent()) |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8298 | return S.Context.DependentTy; |
| Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8299 | |
| Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 8300 | QualType ResType = Op->getType(); |
| 8301 | assert(!ResType.isNull() && "no type for increment/decrement expression"); |
| Steve Naroff | d50c88e | 2007-04-05 21:15:20 +0000 | [diff] [blame] | 8302 | |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8303 | if (S.getLangOptions().CPlusPlus && ResType->isBooleanType()) { |
| Sebastian Redl | e10c2c3 | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 8304 | // Decrement of bool is not allowed. |
| 8305 | if (!isInc) { |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8306 | S.Diag(OpLoc, diag::err_decrement_bool) << Op->getSourceRange(); |
| Sebastian Redl | e10c2c3 | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 8307 | return QualType(); |
| 8308 | } |
| 8309 | // Increment of bool sets it to true, but is deprecated. |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8310 | S.Diag(OpLoc, diag::warn_increment_bool) << Op->getSourceRange(); |
| Sebastian Redl | e10c2c3 | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 8311 | } else if (ResType->isRealType()) { |
| Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 8312 | // OK! |
| Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 8313 | } else if (ResType->isAnyPointerType()) { |
| 8314 | QualType PointeeTy = ResType->getPointeeType(); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8315 | |
| Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 8316 | // C99 6.5.2.4p2, 6.5.6p2 |
| Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 8317 | if (PointeeTy->isVoidType()) { |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8318 | if (S.getLangOptions().CPlusPlus) { |
| 8319 | S.Diag(OpLoc, diag::err_typecheck_pointer_arith_void_type) |
| Douglas Gregor | f6cd928 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 8320 | << Op->getSourceRange(); |
| 8321 | return QualType(); |
| 8322 | } |
| 8323 | |
| 8324 | // Pointer to void is a GNU extension in C. |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8325 | S.Diag(OpLoc, diag::ext_gnu_void_ptr) << Op->getSourceRange(); |
| Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 8326 | } else if (PointeeTy->isFunctionType()) { |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8327 | if (S.getLangOptions().CPlusPlus) { |
| 8328 | S.Diag(OpLoc, diag::err_typecheck_pointer_arith_function_type) |
| Douglas Gregor | f6cd928 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 8329 | << Op->getType() << Op->getSourceRange(); |
| 8330 | return QualType(); |
| 8331 | } |
| 8332 | |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8333 | S.Diag(OpLoc, diag::ext_gnu_ptr_func_arith) |
| Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 8334 | << ResType << Op->getSourceRange(); |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8335 | } else if (S.RequireCompleteType(OpLoc, PointeeTy, |
| 8336 | S.PDiag(diag::err_typecheck_arithmetic_incomplete_type) |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8337 | << Op->getSourceRange() |
| Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 8338 | << ResType)) |
| Douglas Gregor | dd430f7 | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 8339 | return QualType(); |
| Fariborz Jahanian | ca75db7 | 2009-07-16 17:59:14 +0000 | [diff] [blame] | 8340 | // Diagnose bad cases where we step over interface counts. |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8341 | else if (PointeeTy->isObjCObjectType() && S.LangOpts.ObjCNonFragileABI) { |
| 8342 | S.Diag(OpLoc, diag::err_arithmetic_nonfragile_interface) |
| Fariborz Jahanian | ca75db7 | 2009-07-16 17:59:14 +0000 | [diff] [blame] | 8343 | << PointeeTy << Op->getSourceRange(); |
| 8344 | return QualType(); |
| 8345 | } |
| Eli Friedman | 090addd | 2010-01-03 00:20:48 +0000 | [diff] [blame] | 8346 | } else if (ResType->isAnyComplexType()) { |
| Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 8347 | // C99 does not support ++/-- on complex types, we allow as an extension. |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8348 | S.Diag(OpLoc, diag::ext_integer_increment_complex) |
| Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 8349 | << ResType << Op->getSourceRange(); |
| John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8350 | } else if (ResType->isPlaceholderType()) { |
| John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8351 | ExprResult PR = S.CheckPlaceholderExpr(Op); |
| John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8352 | if (PR.isInvalid()) return QualType(); |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8353 | return CheckIncrementDecrementOperand(S, PR.take(), VK, OpLoc, |
| 8354 | isInc, isPrefix); |
| Anton Yartsev | 85129b8 | 2011-02-07 02:17:30 +0000 | [diff] [blame] | 8355 | } else if (S.getLangOptions().AltiVec && ResType->isVectorType()) { |
| 8356 | // OK! ( C/C++ Language Extensions for CBEA(Version 2.6) 10.3 ) |
| Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 8357 | } else { |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8358 | S.Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement) |
| Douglas Gregor | 906db8a | 2009-12-15 16:44:32 +0000 | [diff] [blame] | 8359 | << ResType << int(isInc) << Op->getSourceRange(); |
| Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 8360 | return QualType(); |
| Steve Naroff | 46ba1eb | 2007-04-03 23:13:13 +0000 | [diff] [blame] | 8361 | } |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8362 | // At this point, we know we have a real, complex or pointer type. |
| Steve Naroff | 9e1e551 | 2007-08-23 21:37:33 +0000 | [diff] [blame] | 8363 | // Now make sure the operand is a modifiable lvalue. |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8364 | if (CheckForModifiableLvalue(Op, OpLoc, S)) |
| Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 8365 | return QualType(); |
| Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 8366 | // In C++, a prefix increment is the same type as the operand. Otherwise |
| 8367 | // (in C or with postfix), the increment is the unqualified type of the |
| 8368 | // operand. |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8369 | if (isPrefix && S.getLangOptions().CPlusPlus) { |
| 8370 | VK = VK_LValue; |
| 8371 | return ResType; |
| 8372 | } else { |
| 8373 | VK = VK_RValue; |
| 8374 | return ResType.getUnqualifiedType(); |
| 8375 | } |
| Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 8376 | } |
| 8377 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8378 | ExprResult Sema::ConvertPropertyForRValue(Expr *E) { |
| John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8379 | assert(E->getValueKind() == VK_LValue && |
| 8380 | E->getObjectKind() == OK_ObjCProperty); |
| 8381 | const ObjCPropertyRefExpr *PRE = E->getObjCProperty(); |
| 8382 | |
| Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 8383 | QualType T = E->getType(); |
| 8384 | QualType ReceiverType; |
| 8385 | if (PRE->isObjectReceiver()) |
| 8386 | ReceiverType = PRE->getBase()->getType(); |
| 8387 | else if (PRE->isSuperReceiver()) |
| 8388 | ReceiverType = PRE->getSuperReceiverType(); |
| 8389 | else |
| 8390 | ReceiverType = Context.getObjCInterfaceType(PRE->getClassReceiver()); |
| 8391 | |
| John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8392 | ExprValueKind VK = VK_RValue; |
| 8393 | if (PRE->isImplicitProperty()) { |
| Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 8394 | if (ObjCMethodDecl *GetterMethod = |
| Fariborz Jahanian | 0f0b302 | 2010-12-22 19:46:35 +0000 | [diff] [blame] | 8395 | PRE->getImplicitPropertyGetter()) { |
| Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 8396 | T = getMessageSendResultType(ReceiverType, GetterMethod, |
| 8397 | PRE->isClassReceiver(), |
| 8398 | PRE->isSuperReceiver()); |
| 8399 | VK = Expr::getValueKindForType(GetterMethod->getResultType()); |
| Fariborz Jahanian | 0f0b302 | 2010-12-22 19:46:35 +0000 | [diff] [blame] | 8400 | } |
| 8401 | else { |
| 8402 | Diag(PRE->getLocation(), diag::err_getter_not_found) |
| 8403 | << PRE->getBase()->getType(); |
| 8404 | } |
| John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8405 | } |
| Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 8406 | |
| 8407 | E = ImplicitCastExpr::Create(Context, T, CK_GetObjCProperty, |
| John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8408 | E, 0, VK); |
| John McCall | 4f26cd8 | 2010-12-10 01:49:45 +0000 | [diff] [blame] | 8409 | |
| 8410 | ExprResult Result = MaybeBindToTemporary(E); |
| 8411 | if (!Result.isInvalid()) |
| 8412 | E = Result.take(); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8413 | |
| 8414 | return Owned(E); |
| John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8415 | } |
| 8416 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8417 | void Sema::ConvertPropertyForLValue(ExprResult &LHS, ExprResult &RHS, QualType &LHSTy) { |
| 8418 | assert(LHS.get()->getValueKind() == VK_LValue && |
| 8419 | LHS.get()->getObjectKind() == OK_ObjCProperty); |
| 8420 | const ObjCPropertyRefExpr *PropRef = LHS.get()->getObjCProperty(); |
| John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8421 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8422 | if (PropRef->isImplicitProperty()) { |
| John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8423 | // If using property-dot syntax notation for assignment, and there is a |
| 8424 | // setter, RHS expression is being passed to the setter argument. So, |
| 8425 | // type conversion (and comparison) is RHS to setter's argument type. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8426 | if (const ObjCMethodDecl *SetterMD = PropRef->getImplicitPropertySetter()) { |
| John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8427 | ObjCMethodDecl::param_iterator P = SetterMD->param_begin(); |
| 8428 | LHSTy = (*P)->getType(); |
| 8429 | |
| 8430 | // Otherwise, if the getter returns an l-value, just call that. |
| 8431 | } else { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8432 | QualType Result = PropRef->getImplicitPropertyGetter()->getResultType(); |
| John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8433 | ExprValueKind VK = Expr::getValueKindForType(Result); |
| 8434 | if (VK == VK_LValue) { |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8435 | LHS = ImplicitCastExpr::Create(Context, LHS.get()->getType(), |
| 8436 | CK_GetObjCProperty, LHS.take(), 0, VK); |
| John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8437 | return; |
| John McCall | b7bd14f | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 8438 | } |
| Fariborz Jahanian | 805b74e | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 8439 | } |
| John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8440 | } |
| 8441 | |
| 8442 | if (getLangOptions().CPlusPlus && LHSTy->isRecordType()) { |
| Fariborz Jahanian | 805b74e | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 8443 | InitializedEntity Entity = |
| Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 8444 | InitializedEntity::InitializeParameter(Context, LHSTy); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8445 | ExprResult ArgE = PerformCopyInitialization(Entity, SourceLocation(), RHS); |
| Fariborz Jahanian | 805b74e | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 8446 | if (!ArgE.isInvalid()) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8447 | RHS = ArgE; |
| Fariborz Jahanian | 805b74e | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 8448 | } |
| 8449 | } |
| 8450 | |
| 8451 | |
| Anders Carlsson | 806700f | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 8452 | /// getPrimaryDecl - Helper function for CheckAddressOfOperand(). |
| Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 8453 | /// This routine allows us to typecheck complex/recursive expressions |
| Daniel Dunbar | b692ef4 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 8454 | /// where the declaration is needed for type checking. We only need to |
| 8455 | /// handle cases when the expression references a function designator |
| 8456 | /// or is an lvalue. Here are some examples: |
| 8457 | /// - &(x) => x |
| 8458 | /// - &*****f => f for f a function designator. |
| 8459 | /// - &s.xx => s |
| 8460 | /// - &s.zz[1].yy -> s, if zz is an array |
| 8461 | /// - *(x + 1) -> x, if x is an array |
| 8462 | /// - &"123"[2] -> 0 |
| 8463 | /// - & __real__ x -> x |
| John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 8464 | static ValueDecl *getPrimaryDecl(Expr *E) { |
| Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 8465 | switch (E->getStmtClass()) { |
| Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 8466 | case Stmt::DeclRefExprClass: |
| Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 8467 | return cast<DeclRefExpr>(E)->getDecl(); |
| Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 8468 | case Stmt::MemberExprClass: |
| Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8469 | // If this is an arrow operator, the address is an offset from |
| 8470 | // the base's value, so the object the base refers to is |
| 8471 | // irrelevant. |
| Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 8472 | if (cast<MemberExpr>(E)->isArrow()) |
| Chris Lattner | 48d5284 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 8473 | return 0; |
| Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8474 | // Otherwise, the expression refers to a part of the base |
| Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 8475 | return getPrimaryDecl(cast<MemberExpr>(E)->getBase()); |
| Anders Carlsson | 806700f | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 8476 | case Stmt::ArraySubscriptExprClass: { |
| Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 8477 | // FIXME: This code shouldn't be necessary! We should catch the implicit |
| 8478 | // promotion of register arrays earlier. |
| Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8479 | Expr* Base = cast<ArraySubscriptExpr>(E)->getBase(); |
| 8480 | if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Base)) { |
| 8481 | if (ICE->getSubExpr()->getType()->isArrayType()) |
| 8482 | return getPrimaryDecl(ICE->getSubExpr()); |
| 8483 | } |
| 8484 | return 0; |
| Anders Carlsson | 806700f | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 8485 | } |
| Daniel Dunbar | b692ef4 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 8486 | case Stmt::UnaryOperatorClass: { |
| 8487 | UnaryOperator *UO = cast<UnaryOperator>(E); |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8488 | |
| Daniel Dunbar | b692ef4 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 8489 | switch(UO->getOpcode()) { |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8490 | case UO_Real: |
| 8491 | case UO_Imag: |
| 8492 | case UO_Extension: |
| Daniel Dunbar | b692ef4 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 8493 | return getPrimaryDecl(UO->getSubExpr()); |
| 8494 | default: |
| 8495 | return 0; |
| 8496 | } |
| 8497 | } |
| Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 8498 | case Stmt::ParenExprClass: |
| Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 8499 | return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr()); |
| Chris Lattner | 48d5284 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 8500 | case Stmt::ImplicitCastExprClass: |
| Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8501 | // If the result of an implicit cast is an l-value, we care about |
| 8502 | // the sub-expression; otherwise, the result here doesn't matter. |
| Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 8503 | return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr()); |
| Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 8504 | default: |
| 8505 | return 0; |
| 8506 | } |
| 8507 | } |
| 8508 | |
| 8509 | /// CheckAddressOfOperand - The operand of & must be either a function |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8510 | /// designator or an lvalue designating an object. If it is an lvalue, the |
| Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 8511 | /// object cannot be declared with storage class register or be a bit field. |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8512 | /// Note: The usual conversions are *not* applied to the operand of the & |
| Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 8513 | /// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue. |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8514 | /// In C++, the operand might be an overloaded function name, in which case |
| Douglas Gregor | cd695e5 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 8515 | /// we allow the '&' but retain the overloaded-function type. |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8516 | static QualType CheckAddressOfOperand(Sema &S, Expr *OrigOp, |
| 8517 | SourceLocation OpLoc) { |
| John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8518 | if (OrigOp->isTypeDependent()) |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8519 | return S.Context.DependentTy; |
| 8520 | if (OrigOp->getType() == S.Context.OverloadTy) |
| 8521 | return S.Context.OverloadTy; |
| John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 8522 | if (OrigOp->getType() == S.Context.UnknownAnyTy) |
| 8523 | return S.Context.UnknownAnyTy; |
| John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8524 | if (OrigOp->getType() == S.Context.BoundMemberTy) { |
| 8525 | S.Diag(OpLoc, diag::err_invalid_form_pointer_member_function) |
| 8526 | << OrigOp->getSourceRange(); |
| 8527 | return QualType(); |
| 8528 | } |
| John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8529 | |
| John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 8530 | assert(!OrigOp->getType()->isPlaceholderType()); |
| John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8531 | |
| John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8532 | // Make sure to ignore parentheses in subsequent checks |
| 8533 | Expr *op = OrigOp->IgnoreParens(); |
| Douglas Gregor | 19b8c4f | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 8534 | |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8535 | if (S.getLangOptions().C99) { |
| Steve Naroff | 826e91a | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 8536 | // Implement C99-only parts of addressof rules. |
| 8537 | if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) { |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8538 | if (uOp->getOpcode() == UO_Deref) |
| Steve Naroff | 826e91a | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 8539 | // Per C99 6.5.3.2, the address of a deref always returns a valid result |
| 8540 | // (assuming the deref expression is valid). |
| 8541 | return uOp->getSubExpr()->getType(); |
| 8542 | } |
| 8543 | // Technically, there should be a check for array subscript |
| 8544 | // expressions here, but the result of one is always an lvalue anyway. |
| 8545 | } |
| John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 8546 | ValueDecl *dcl = getPrimaryDecl(op); |
| John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 8547 | Expr::LValueClassification lval = op->ClassifyLValue(S.Context); |
| Nuno Lopes | 17f345f | 2008-12-16 22:59:47 +0000 | [diff] [blame] | 8548 | |
| Fariborz Jahanian | 071caef | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 8549 | if (lval == Expr::LV_ClassTemporary) { |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8550 | bool sfinae = S.isSFINAEContext(); |
| 8551 | S.Diag(OpLoc, sfinae ? diag::err_typecheck_addrof_class_temporary |
| 8552 | : diag::ext_typecheck_addrof_class_temporary) |
| Douglas Gregor | b154fdc | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 8553 | << op->getType() << op->getSourceRange(); |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8554 | if (sfinae) |
| Douglas Gregor | b154fdc | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 8555 | return QualType(); |
| John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8556 | } else if (isa<ObjCSelectorExpr>(op)) { |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8557 | return S.Context.getPointerType(op->getType()); |
| John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8558 | } else if (lval == Expr::LV_MemberFunction) { |
| 8559 | // If it's an instance method, make a member pointer. |
| 8560 | // The expression must have exactly the form &A::foo. |
| 8561 | |
| 8562 | // If the underlying expression isn't a decl ref, give up. |
| 8563 | if (!isa<DeclRefExpr>(op)) { |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8564 | S.Diag(OpLoc, diag::err_invalid_form_pointer_member_function) |
| John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8565 | << OrigOp->getSourceRange(); |
| 8566 | return QualType(); |
| 8567 | } |
| 8568 | DeclRefExpr *DRE = cast<DeclRefExpr>(op); |
| 8569 | CXXMethodDecl *MD = cast<CXXMethodDecl>(DRE->getDecl()); |
| 8570 | |
| 8571 | // The id-expression was parenthesized. |
| 8572 | if (OrigOp != DRE) { |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8573 | S.Diag(OpLoc, diag::err_parens_pointer_member_function) |
| John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8574 | << OrigOp->getSourceRange(); |
| 8575 | |
| 8576 | // The method was named without a qualifier. |
| 8577 | } else if (!DRE->getQualifier()) { |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8578 | S.Diag(OpLoc, diag::err_unqualified_pointer_member_function) |
| John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8579 | << op->getSourceRange(); |
| 8580 | } |
| 8581 | |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8582 | return S.Context.getMemberPointerType(op->getType(), |
| 8583 | S.Context.getTypeDeclType(MD->getParent()).getTypePtr()); |
| John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8584 | } else if (lval != Expr::LV_Valid && lval != Expr::LV_IncompleteVoidType) { |
| Eli Friedman | ce7f900 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 8585 | // C99 6.5.3.2p1 |
| Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8586 | // The operand must be either an l-value or a function designator |
| Eli Friedman | ce7f900 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 8587 | if (!op->getType()->isFunctionType()) { |
| Chris Lattner | 48d5284 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 8588 | // FIXME: emit more specific diag... |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8589 | S.Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof) |
| Chris Lattner | f490e15 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 8590 | << op->getSourceRange(); |
| Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 8591 | return QualType(); |
| 8592 | } |
| John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 8593 | } else if (op->getObjectKind() == OK_BitField) { // C99 6.5.3.2p1 |
| Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8594 | // The operand cannot be a bit-field |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8595 | S.Diag(OpLoc, diag::err_typecheck_address_of) |
| Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8596 | << "bit-field" << op->getSourceRange(); |
| Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 8597 | return QualType(); |
| John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 8598 | } else if (op->getObjectKind() == OK_VectorComponent) { |
| Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8599 | // The operand cannot be an element of a vector |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8600 | S.Diag(OpLoc, diag::err_typecheck_address_of) |
| Nate Begeman | a6b47a4 | 2009-02-15 22:45:20 +0000 | [diff] [blame] | 8601 | << "vector element" << op->getSourceRange(); |
| Steve Naroff | b96e4ab6 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 8602 | return QualType(); |
| John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 8603 | } else if (op->getObjectKind() == OK_ObjCProperty) { |
| Fariborz Jahanian | 385db80 | 2009-07-07 18:50:52 +0000 | [diff] [blame] | 8604 | // cannot take address of a property expression. |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8605 | S.Diag(OpLoc, diag::err_typecheck_address_of) |
| Fariborz Jahanian | 385db80 | 2009-07-07 18:50:52 +0000 | [diff] [blame] | 8606 | << "property expression" << op->getSourceRange(); |
| 8607 | return QualType(); |
| Steve Naroff | b96e4ab6 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 8608 | } else if (dcl) { // C99 6.5.3.2p1 |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8609 | // We have an lvalue with a decl. Make sure the decl is not declared |
| Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 8610 | // with the register storage-class specifier. |
| 8611 | if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { |
| Fariborz Jahanian | e0fd5a9 | 2010-08-24 22:21:48 +0000 | [diff] [blame] | 8612 | // in C++ it is not error to take address of a register |
| 8613 | // variable (c++03 7.1.1P3) |
| John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 8614 | if (vd->getStorageClass() == SC_Register && |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8615 | !S.getLangOptions().CPlusPlus) { |
| 8616 | S.Diag(OpLoc, diag::err_typecheck_address_of) |
| Chris Lattner | 29e812b | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 8617 | << "register variable" << op->getSourceRange(); |
| Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 8618 | return QualType(); |
| 8619 | } |
| John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8620 | } else if (isa<FunctionTemplateDecl>(dcl)) { |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8621 | return S.Context.OverloadTy; |
| John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 8622 | } else if (isa<FieldDecl>(dcl) || isa<IndirectFieldDecl>(dcl)) { |
| Douglas Gregor | 9aa8b55 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 8623 | // Okay: we can take the address of a field. |
| Sebastian Redl | 3d3f75a | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 8624 | // Could be a pointer to member, though, if there is an explicit |
| 8625 | // scope qualifier for the class. |
| Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 8626 | if (isa<DeclRefExpr>(op) && cast<DeclRefExpr>(op)->getQualifier()) { |
| Sebastian Redl | 3d3f75a | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 8627 | DeclContext *Ctx = dcl->getDeclContext(); |
| Anders Carlsson | 0b675f5 | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 8628 | if (Ctx && Ctx->isRecord()) { |
| John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 8629 | if (dcl->getType()->isReferenceType()) { |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8630 | S.Diag(OpLoc, |
| 8631 | diag::err_cannot_form_pointer_to_member_of_reference_type) |
| John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 8632 | << dcl->getDeclName() << dcl->getType(); |
| Anders Carlsson | 0b675f5 | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 8633 | return QualType(); |
| 8634 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8635 | |
| Argyrios Kyrtzidis | 8322b42 | 2011-01-31 07:04:29 +0000 | [diff] [blame] | 8636 | while (cast<RecordDecl>(Ctx)->isAnonymousStructOrUnion()) |
| 8637 | Ctx = Ctx->getParent(); |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8638 | return S.Context.getMemberPointerType(op->getType(), |
| 8639 | S.Context.getTypeDeclType(cast<RecordDecl>(Ctx)).getTypePtr()); |
| Anders Carlsson | 0b675f5 | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 8640 | } |
| Sebastian Redl | 3d3f75a | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 8641 | } |
| Anders Carlsson | 5b53576 | 2009-05-16 21:43:42 +0000 | [diff] [blame] | 8642 | } else if (!isa<FunctionDecl>(dcl)) |
| Steve Naroff | f633d09 | 2007-04-25 19:01:39 +0000 | [diff] [blame] | 8643 | assert(0 && "Unknown/unexpected decl type"); |
| Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 8644 | } |
| Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 8645 | |
| Eli Friedman | ce7f900 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 8646 | if (lval == Expr::LV_IncompleteVoidType) { |
| 8647 | // Taking the address of a void variable is technically illegal, but we |
| 8648 | // allow it in cases which are otherwise valid. |
| 8649 | // Example: "extern void x; void* y = &x;". |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8650 | S.Diag(OpLoc, diag::ext_typecheck_addrof_void) << op->getSourceRange(); |
| Eli Friedman | ce7f900 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 8651 | } |
| 8652 | |
| Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 8653 | // If the operand has type "type", the result has type "pointer to type". |
| Douglas Gregor | 0bdcb8a | 2010-07-29 16:05:45 +0000 | [diff] [blame] | 8654 | if (op->getType()->isObjCObjectType()) |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8655 | return S.Context.getObjCObjectPointerType(op->getType()); |
| 8656 | return S.Context.getPointerType(op->getType()); |
| Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 8657 | } |
| 8658 | |
| Chris Lattner | 9156f1b | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8659 | /// CheckIndirectionOperand - Type check unary indirection (prefix '*'). |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8660 | static QualType CheckIndirectionOperand(Sema &S, Expr *Op, ExprValueKind &VK, |
| 8661 | SourceLocation OpLoc) { |
| Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8662 | if (Op->isTypeDependent()) |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8663 | return S.Context.DependentTy; |
| Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8664 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8665 | ExprResult ConvResult = S.UsualUnaryConversions(Op); |
| 8666 | if (ConvResult.isInvalid()) |
| 8667 | return QualType(); |
| 8668 | Op = ConvResult.take(); |
| Chris Lattner | 9156f1b | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8669 | QualType OpTy = Op->getType(); |
| 8670 | QualType Result; |
| Argyrios Kyrtzidis | 69a2c92 | 2011-05-02 18:21:19 +0000 | [diff] [blame] | 8671 | |
| 8672 | if (isa<CXXReinterpretCastExpr>(Op)) { |
| 8673 | QualType OpOrigType = Op->IgnoreParenCasts()->getType(); |
| 8674 | S.CheckCompatibleReinterpretCast(OpOrigType, OpTy, /*IsDereference*/true, |
| 8675 | Op->getSourceRange()); |
| 8676 | } |
| 8677 | |
| Chris Lattner | 9156f1b | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8678 | // Note that per both C89 and C99, indirection is always legal, even if OpTy |
| 8679 | // is an incomplete type or void. It would be possible to warn about |
| 8680 | // dereferencing a void pointer, but it's completely well-defined, and such a |
| 8681 | // warning is unlikely to catch any mistakes. |
| 8682 | if (const PointerType *PT = OpTy->getAs<PointerType>()) |
| 8683 | Result = PT->getPointeeType(); |
| 8684 | else if (const ObjCObjectPointerType *OPT = |
| 8685 | OpTy->getAs<ObjCObjectPointerType>()) |
| 8686 | Result = OPT->getPointeeType(); |
| John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8687 | else { |
| John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8688 | ExprResult PR = S.CheckPlaceholderExpr(Op); |
| John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8689 | if (PR.isInvalid()) return QualType(); |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8690 | if (PR.take() != Op) |
| 8691 | return CheckIndirectionOperand(S, PR.take(), VK, OpLoc); |
| John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8692 | } |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8693 | |
| Chris Lattner | 9156f1b | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8694 | if (Result.isNull()) { |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8695 | S.Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer) |
| Chris Lattner | 9156f1b | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8696 | << OpTy << Op->getSourceRange(); |
| 8697 | return QualType(); |
| 8698 | } |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8699 | |
| 8700 | // Dereferences are usually l-values... |
| 8701 | VK = VK_LValue; |
| 8702 | |
| 8703 | // ...except that certain expressions are never l-values in C. |
| 8704 | if (!S.getLangOptions().CPlusPlus && |
| 8705 | IsCForbiddenLValueType(S.Context, Result)) |
| 8706 | VK = VK_RValue; |
| Chris Lattner | 9156f1b | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8707 | |
| 8708 | return Result; |
| Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 8709 | } |
| Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 8710 | |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8711 | static inline BinaryOperatorKind ConvertTokenKindToBinaryOpcode( |
| Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 8712 | tok::TokenKind Kind) { |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8713 | BinaryOperatorKind Opc; |
| Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 8714 | switch (Kind) { |
| 8715 | default: assert(0 && "Unknown binop!"); |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8716 | case tok::periodstar: Opc = BO_PtrMemD; break; |
| 8717 | case tok::arrowstar: Opc = BO_PtrMemI; break; |
| 8718 | case tok::star: Opc = BO_Mul; break; |
| 8719 | case tok::slash: Opc = BO_Div; break; |
| 8720 | case tok::percent: Opc = BO_Rem; break; |
| 8721 | case tok::plus: Opc = BO_Add; break; |
| 8722 | case tok::minus: Opc = BO_Sub; break; |
| 8723 | case tok::lessless: Opc = BO_Shl; break; |
| 8724 | case tok::greatergreater: Opc = BO_Shr; break; |
| 8725 | case tok::lessequal: Opc = BO_LE; break; |
| 8726 | case tok::less: Opc = BO_LT; break; |
| 8727 | case tok::greaterequal: Opc = BO_GE; break; |
| 8728 | case tok::greater: Opc = BO_GT; break; |
| 8729 | case tok::exclaimequal: Opc = BO_NE; break; |
| 8730 | case tok::equalequal: Opc = BO_EQ; break; |
| 8731 | case tok::amp: Opc = BO_And; break; |
| 8732 | case tok::caret: Opc = BO_Xor; break; |
| 8733 | case tok::pipe: Opc = BO_Or; break; |
| 8734 | case tok::ampamp: Opc = BO_LAnd; break; |
| 8735 | case tok::pipepipe: Opc = BO_LOr; break; |
| 8736 | case tok::equal: Opc = BO_Assign; break; |
| 8737 | case tok::starequal: Opc = BO_MulAssign; break; |
| 8738 | case tok::slashequal: Opc = BO_DivAssign; break; |
| 8739 | case tok::percentequal: Opc = BO_RemAssign; break; |
| 8740 | case tok::plusequal: Opc = BO_AddAssign; break; |
| 8741 | case tok::minusequal: Opc = BO_SubAssign; break; |
| 8742 | case tok::lesslessequal: Opc = BO_ShlAssign; break; |
| 8743 | case tok::greatergreaterequal: Opc = BO_ShrAssign; break; |
| 8744 | case tok::ampequal: Opc = BO_AndAssign; break; |
| 8745 | case tok::caretequal: Opc = BO_XorAssign; break; |
| 8746 | case tok::pipeequal: Opc = BO_OrAssign; break; |
| 8747 | case tok::comma: Opc = BO_Comma; break; |
| Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 8748 | } |
| 8749 | return Opc; |
| 8750 | } |
| 8751 | |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8752 | static inline UnaryOperatorKind ConvertTokenKindToUnaryOpcode( |
| Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 8753 | tok::TokenKind Kind) { |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8754 | UnaryOperatorKind Opc; |
| Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 8755 | switch (Kind) { |
| 8756 | default: assert(0 && "Unknown unary op!"); |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8757 | case tok::plusplus: Opc = UO_PreInc; break; |
| 8758 | case tok::minusminus: Opc = UO_PreDec; break; |
| 8759 | case tok::amp: Opc = UO_AddrOf; break; |
| 8760 | case tok::star: Opc = UO_Deref; break; |
| 8761 | case tok::plus: Opc = UO_Plus; break; |
| 8762 | case tok::minus: Opc = UO_Minus; break; |
| 8763 | case tok::tilde: Opc = UO_Not; break; |
| 8764 | case tok::exclaim: Opc = UO_LNot; break; |
| 8765 | case tok::kw___real: Opc = UO_Real; break; |
| 8766 | case tok::kw___imag: Opc = UO_Imag; break; |
| 8767 | case tok::kw___extension__: Opc = UO_Extension; break; |
| Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 8768 | } |
| 8769 | return Opc; |
| 8770 | } |
| 8771 | |
| Chandler Carruth | e0cee6a | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 8772 | /// DiagnoseSelfAssignment - Emits a warning if a value is assigned to itself. |
| 8773 | /// This warning is only emitted for builtin assignment operations. It is also |
| 8774 | /// suppressed in the event of macro expansions. |
| 8775 | static void DiagnoseSelfAssignment(Sema &S, Expr *lhs, Expr *rhs, |
| 8776 | SourceLocation OpLoc) { |
| 8777 | if (!S.ActiveTemplateInstantiations.empty()) |
| 8778 | return; |
| 8779 | if (OpLoc.isInvalid() || OpLoc.isMacroID()) |
| 8780 | return; |
| 8781 | lhs = lhs->IgnoreParenImpCasts(); |
| 8782 | rhs = rhs->IgnoreParenImpCasts(); |
| 8783 | const DeclRefExpr *LeftDeclRef = dyn_cast<DeclRefExpr>(lhs); |
| 8784 | const DeclRefExpr *RightDeclRef = dyn_cast<DeclRefExpr>(rhs); |
| 8785 | if (!LeftDeclRef || !RightDeclRef || |
| 8786 | LeftDeclRef->getLocation().isMacroID() || |
| 8787 | RightDeclRef->getLocation().isMacroID()) |
| 8788 | return; |
| 8789 | const ValueDecl *LeftDecl = |
| 8790 | cast<ValueDecl>(LeftDeclRef->getDecl()->getCanonicalDecl()); |
| 8791 | const ValueDecl *RightDecl = |
| 8792 | cast<ValueDecl>(RightDeclRef->getDecl()->getCanonicalDecl()); |
| 8793 | if (LeftDecl != RightDecl) |
| 8794 | return; |
| 8795 | if (LeftDecl->getType().isVolatileQualified()) |
| 8796 | return; |
| 8797 | if (const ReferenceType *RefTy = LeftDecl->getType()->getAs<ReferenceType>()) |
| 8798 | if (RefTy->getPointeeType().isVolatileQualified()) |
| 8799 | return; |
| 8800 | |
| 8801 | S.Diag(OpLoc, diag::warn_self_assignment) |
| 8802 | << LeftDeclRef->getType() |
| 8803 | << lhs->getSourceRange() << rhs->getSourceRange(); |
| 8804 | } |
| 8805 | |
| Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8806 | /// CreateBuiltinBinOp - Creates a new built-in binary operation with |
| 8807 | /// operator @p Opc at location @c TokLoc. This routine only supports |
| 8808 | /// built-in operations; ActOnBinOp handles overloaded operators. |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8809 | ExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, |
| Argyrios Kyrtzidis | 7a808c0 | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 8810 | BinaryOperatorKind Opc, |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8811 | Expr *lhsExpr, Expr *rhsExpr) { |
| 8812 | ExprResult lhs = Owned(lhsExpr), rhs = Owned(rhsExpr); |
| Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8813 | QualType ResultTy; // Result type of the binary operator. |
| Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8814 | // The following two variables are used for compound assignment operators |
| 8815 | QualType CompLHSTy; // Type of LHS after promotions for computation |
| 8816 | QualType CompResultTy; // Type of computation result |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8817 | ExprValueKind VK = VK_RValue; |
| 8818 | ExprObjectKind OK = OK_Ordinary; |
| Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8819 | |
| Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 8820 | // Check if a 'foo<int>' involved in a binary op, identifies a single |
| 8821 | // function unambiguously (i.e. an lvalue ala 13.4) |
| 8822 | // But since an assignment can trigger target based overload, exclude it in |
| 8823 | // our blind search. i.e: |
| 8824 | // template<class T> void f(); template<class T, class U> void f(U); |
| 8825 | // f<int> == 0; // resolve f<int> blindly |
| 8826 | // void (*p)(int); p = f<int>; // resolve f<int> using target |
| 8827 | if (Opc != BO_Assign) { |
| John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8828 | ExprResult resolvedLHS = CheckPlaceholderExpr(lhs.get()); |
| John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 8829 | if (!resolvedLHS.isUsable()) return ExprError(); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8830 | lhs = move(resolvedLHS); |
| John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 8831 | |
| John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8832 | ExprResult resolvedRHS = CheckPlaceholderExpr(rhs.get()); |
| John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 8833 | if (!resolvedRHS.isUsable()) return ExprError(); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8834 | rhs = move(resolvedRHS); |
| Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 8835 | } |
| 8836 | |
| Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8837 | switch (Opc) { |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8838 | case BO_Assign: |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8839 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, QualType()); |
| John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8840 | if (getLangOptions().CPlusPlus && |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8841 | lhs.get()->getObjectKind() != OK_ObjCProperty) { |
| 8842 | VK = lhs.get()->getValueKind(); |
| 8843 | OK = lhs.get()->getObjectKind(); |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8844 | } |
| Chandler Carruth | e0cee6a | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 8845 | if (!ResultTy.isNull()) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8846 | DiagnoseSelfAssignment(*this, lhs.get(), rhs.get(), OpLoc); |
| Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8847 | break; |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8848 | case BO_PtrMemD: |
| 8849 | case BO_PtrMemI: |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8850 | ResultTy = CheckPointerToMemberOperands(lhs, rhs, VK, OpLoc, |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8851 | Opc == BO_PtrMemI); |
| Sebastian Redl | 112a9766 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 8852 | break; |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8853 | case BO_Mul: |
| 8854 | case BO_Div: |
| Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 8855 | ResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc, false, |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8856 | Opc == BO_Div); |
| Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8857 | break; |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8858 | case BO_Rem: |
| Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8859 | ResultTy = CheckRemainderOperands(lhs, rhs, OpLoc); |
| 8860 | break; |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8861 | case BO_Add: |
| Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8862 | ResultTy = CheckAdditionOperands(lhs, rhs, OpLoc); |
| 8863 | break; |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8864 | case BO_Sub: |
| Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8865 | ResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc); |
| 8866 | break; |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8867 | case BO_Shl: |
| 8868 | case BO_Shr: |
| Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 8869 | ResultTy = CheckShiftOperands(lhs, rhs, OpLoc, Opc); |
| Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8870 | break; |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8871 | case BO_LE: |
| 8872 | case BO_LT: |
| 8873 | case BO_GE: |
| 8874 | case BO_GT: |
| Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 8875 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, true); |
| Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8876 | break; |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8877 | case BO_EQ: |
| 8878 | case BO_NE: |
| Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 8879 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, false); |
| Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8880 | break; |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8881 | case BO_And: |
| 8882 | case BO_Xor: |
| 8883 | case BO_Or: |
| Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8884 | ResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc); |
| 8885 | break; |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8886 | case BO_LAnd: |
| 8887 | case BO_LOr: |
| Chris Lattner | 8406c51 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 8888 | ResultTy = CheckLogicalOperands(lhs, rhs, OpLoc, Opc); |
| Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8889 | break; |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8890 | case BO_MulAssign: |
| 8891 | case BO_DivAssign: |
| Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 8892 | CompResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc, true, |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8893 | Opc == BO_DivAssign); |
| Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8894 | CompLHSTy = CompResultTy; |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8895 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 8896 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
| Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8897 | break; |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8898 | case BO_RemAssign: |
| Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8899 | CompResultTy = CheckRemainderOperands(lhs, rhs, OpLoc, true); |
| 8900 | CompLHSTy = CompResultTy; |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8901 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 8902 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
| Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8903 | break; |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8904 | case BO_AddAssign: |
| Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8905 | CompResultTy = CheckAdditionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8906 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 8907 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
| Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8908 | break; |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8909 | case BO_SubAssign: |
| Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8910 | CompResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8911 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 8912 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
| Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8913 | break; |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8914 | case BO_ShlAssign: |
| 8915 | case BO_ShrAssign: |
| Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 8916 | CompResultTy = CheckShiftOperands(lhs, rhs, OpLoc, Opc, true); |
| Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8917 | CompLHSTy = CompResultTy; |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8918 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 8919 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
| Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8920 | break; |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8921 | case BO_AndAssign: |
| 8922 | case BO_XorAssign: |
| 8923 | case BO_OrAssign: |
| Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8924 | CompResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc, true); |
| 8925 | CompLHSTy = CompResultTy; |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8926 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 8927 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
| Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8928 | break; |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8929 | case BO_Comma: |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8930 | ResultTy = CheckCommaOperands(*this, lhs, rhs, OpLoc); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8931 | if (getLangOptions().CPlusPlus && !rhs.isInvalid()) { |
| 8932 | VK = rhs.get()->getValueKind(); |
| 8933 | OK = rhs.get()->getObjectKind(); |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8934 | } |
| Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8935 | break; |
| 8936 | } |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8937 | if (ResultTy.isNull() || lhs.isInvalid() || rhs.isInvalid()) |
| Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 8938 | return ExprError(); |
| Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8939 | if (CompResultTy.isNull()) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8940 | return Owned(new (Context) BinaryOperator(lhs.take(), rhs.take(), Opc, |
| 8941 | ResultTy, VK, OK, OpLoc)); |
| 8942 | if (getLangOptions().CPlusPlus && lhs.get()->getObjectKind() != OK_ObjCProperty) { |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8943 | VK = VK_LValue; |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8944 | OK = lhs.get()->getObjectKind(); |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8945 | } |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8946 | return Owned(new (Context) CompoundAssignOperator(lhs.take(), rhs.take(), Opc, |
| 8947 | ResultTy, VK, OK, CompLHSTy, |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8948 | CompResultTy, OpLoc)); |
| Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8949 | } |
| 8950 | |
| Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8951 | /// DiagnoseBitwisePrecedence - Emit a warning when bitwise and comparison |
| 8952 | /// operators are mixed in a way that suggests that the programmer forgot that |
| 8953 | /// comparison operators have higher precedence. The most typical example of |
| 8954 | /// such code is "flags & 0x0020 != 0", which is equivalent to "flags & 1". |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8955 | static void DiagnoseBitwisePrecedence(Sema &Self, BinaryOperatorKind Opc, |
| Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8956 | SourceLocation OpLoc,Expr *lhs,Expr *rhs){ |
| Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8957 | typedef BinaryOperator BinOp; |
| 8958 | BinOp::Opcode lhsopc = static_cast<BinOp::Opcode>(-1), |
| 8959 | rhsopc = static_cast<BinOp::Opcode>(-1); |
| 8960 | if (BinOp *BO = dyn_cast<BinOp>(lhs)) |
| Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8961 | lhsopc = BO->getOpcode(); |
| Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8962 | if (BinOp *BO = dyn_cast<BinOp>(rhs)) |
| Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8963 | rhsopc = BO->getOpcode(); |
| 8964 | |
| 8965 | // Subs are not binary operators. |
| 8966 | if (lhsopc == -1 && rhsopc == -1) |
| 8967 | return; |
| 8968 | |
| 8969 | // Bitwise operations are sometimes used as eager logical ops. |
| 8970 | // Don't diagnose this. |
| Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8971 | if ((BinOp::isComparisonOp(lhsopc) || BinOp::isBitwiseOp(lhsopc)) && |
| 8972 | (BinOp::isComparisonOp(rhsopc) || BinOp::isBitwiseOp(rhsopc))) |
| Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8973 | return; |
| 8974 | |
| Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8975 | if (BinOp::isComparisonOp(lhsopc)) |
| Sebastian Redl | 4afb7c58 | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 8976 | SuggestParentheses(Self, OpLoc, |
| Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 8977 | Self.PDiag(diag::warn_precedence_bitwise_rel) |
| Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8978 | << SourceRange(lhs->getLocStart(), OpLoc) |
| 8979 | << BinOp::getOpcodeStr(Opc) << BinOp::getOpcodeStr(lhsopc), |
| Douglas Gregor | 2bf2d3d | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 8980 | Self.PDiag(diag::note_precedence_bitwise_silence) |
| 8981 | << BinOp::getOpcodeStr(lhsopc), |
| Argyrios Kyrtzidis | e1b97c4 | 2011-04-25 23:01:29 +0000 | [diff] [blame] | 8982 | lhs->getSourceRange(), |
| 8983 | Self.PDiag(diag::note_precedence_bitwise_first) |
| 8984 | << BinOp::getOpcodeStr(Opc), |
| 8985 | SourceRange(cast<BinOp>(lhs)->getRHS()->getLocStart(), rhs->getLocEnd())); |
| Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8986 | else if (BinOp::isComparisonOp(rhsopc)) |
| Sebastian Redl | 4afb7c58 | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 8987 | SuggestParentheses(Self, OpLoc, |
| Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 8988 | Self.PDiag(diag::warn_precedence_bitwise_rel) |
| Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8989 | << SourceRange(OpLoc, rhs->getLocEnd()) |
| 8990 | << BinOp::getOpcodeStr(Opc) << BinOp::getOpcodeStr(rhsopc), |
| Argyrios Kyrtzidis | e1b97c4 | 2011-04-25 23:01:29 +0000 | [diff] [blame] | 8991 | Self.PDiag(diag::note_precedence_bitwise_silence) |
| 8992 | << BinOp::getOpcodeStr(rhsopc), |
| 8993 | rhs->getSourceRange(), |
| Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 8994 | Self.PDiag(diag::note_precedence_bitwise_first) |
| Douglas Gregor | fa1e36d | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 8995 | << BinOp::getOpcodeStr(Opc), |
| Argyrios Kyrtzidis | e1b97c4 | 2011-04-25 23:01:29 +0000 | [diff] [blame] | 8996 | SourceRange(lhs->getLocEnd(), cast<BinOp>(rhs)->getLHS()->getLocStart())); |
| Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8997 | } |
| 8998 | |
| Argyrios Kyrtzidis | 14a9662 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8999 | /// \brief It accepts a '&&' expr that is inside a '||' one. |
| 9000 | /// Emit a diagnostic together with a fixit hint that wraps the '&&' expression |
| 9001 | /// in parentheses. |
| 9002 | static void |
| 9003 | EmitDiagnosticForLogicalAndInLogicalOr(Sema &Self, SourceLocation OpLoc, |
| Argyrios Kyrtzidis | ad8b4d4 | 2011-04-22 19:16:27 +0000 | [diff] [blame] | 9004 | BinaryOperator *Bop) { |
| 9005 | assert(Bop->getOpcode() == BO_LAnd); |
| 9006 | SuggestParentheses(Self, Bop->getOperatorLoc(), |
| Argyrios Kyrtzidis | 14a9662 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 9007 | Self.PDiag(diag::warn_logical_and_in_logical_or) |
| Argyrios Kyrtzidis | ad8b4d4 | 2011-04-22 19:16:27 +0000 | [diff] [blame] | 9008 | << Bop->getSourceRange() << OpLoc, |
| Argyrios Kyrtzidis | 14a9662 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 9009 | Self.PDiag(diag::note_logical_and_in_logical_or_silence), |
| Argyrios Kyrtzidis | ad8b4d4 | 2011-04-22 19:16:27 +0000 | [diff] [blame] | 9010 | Bop->getSourceRange(), |
| Argyrios Kyrtzidis | 14a9662 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 9011 | Self.PDiag(0), SourceRange()); |
| 9012 | } |
| 9013 | |
| 9014 | /// \brief Returns true if the given expression can be evaluated as a constant |
| 9015 | /// 'true'. |
| 9016 | static bool EvaluatesAsTrue(Sema &S, Expr *E) { |
| 9017 | bool Res; |
| 9018 | return E->EvaluateAsBooleanCondition(Res, S.getASTContext()) && Res; |
| 9019 | } |
| 9020 | |
| Argyrios Kyrtzidis | 56e879d | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 9021 | /// \brief Returns true if the given expression can be evaluated as a constant |
| 9022 | /// 'false'. |
| 9023 | static bool EvaluatesAsFalse(Sema &S, Expr *E) { |
| 9024 | bool Res; |
| 9025 | return E->EvaluateAsBooleanCondition(Res, S.getASTContext()) && !Res; |
| 9026 | } |
| 9027 | |
| Argyrios Kyrtzidis | 14a9662 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 9028 | /// \brief Look for '&&' in the left hand of a '||' expr. |
| 9029 | static void DiagnoseLogicalAndInLogicalOrLHS(Sema &S, SourceLocation OpLoc, |
| Argyrios Kyrtzidis | 56e879d | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 9030 | Expr *OrLHS, Expr *OrRHS) { |
| 9031 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(OrLHS)) { |
| Argyrios Kyrtzidis | f89a56c | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 9032 | if (Bop->getOpcode() == BO_LAnd) { |
| Argyrios Kyrtzidis | 56e879d | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 9033 | // If it's "a && b || 0" don't warn since the precedence doesn't matter. |
| 9034 | if (EvaluatesAsFalse(S, OrRHS)) |
| 9035 | return; |
| Argyrios Kyrtzidis | 14a9662 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 9036 | // If it's "1 && a || b" don't warn since the precedence doesn't matter. |
| 9037 | if (!EvaluatesAsTrue(S, Bop->getLHS())) |
| 9038 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, Bop); |
| 9039 | } else if (Bop->getOpcode() == BO_LOr) { |
| 9040 | if (BinaryOperator *RBop = dyn_cast<BinaryOperator>(Bop->getRHS())) { |
| 9041 | // If it's "a || b && 1 || c" we didn't warn earlier for |
| 9042 | // "a || b && 1", but warn now. |
| 9043 | if (RBop->getOpcode() == BO_LAnd && EvaluatesAsTrue(S, RBop->getRHS())) |
| 9044 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, RBop); |
| 9045 | } |
| 9046 | } |
| 9047 | } |
| 9048 | } |
| 9049 | |
| 9050 | /// \brief Look for '&&' in the right hand of a '||' expr. |
| 9051 | static void DiagnoseLogicalAndInLogicalOrRHS(Sema &S, SourceLocation OpLoc, |
| Argyrios Kyrtzidis | 56e879d | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 9052 | Expr *OrLHS, Expr *OrRHS) { |
| 9053 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(OrRHS)) { |
| Argyrios Kyrtzidis | 14a9662 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 9054 | if (Bop->getOpcode() == BO_LAnd) { |
| Argyrios Kyrtzidis | 56e879d | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 9055 | // If it's "0 || a && b" don't warn since the precedence doesn't matter. |
| 9056 | if (EvaluatesAsFalse(S, OrLHS)) |
| 9057 | return; |
| Argyrios Kyrtzidis | 14a9662 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 9058 | // If it's "a || b && 1" don't warn since the precedence doesn't matter. |
| 9059 | if (!EvaluatesAsTrue(S, Bop->getRHS())) |
| 9060 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, Bop); |
| Argyrios Kyrtzidis | f89a56c | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 9061 | } |
| 9062 | } |
| 9063 | } |
| 9064 | |
| Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 9065 | /// DiagnoseBinOpPrecedence - Emit warnings for expressions with tricky |
| Argyrios Kyrtzidis | f89a56c | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 9066 | /// precedence. |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9067 | static void DiagnoseBinOpPrecedence(Sema &Self, BinaryOperatorKind Opc, |
| Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 9068 | SourceLocation OpLoc, Expr *lhs, Expr *rhs){ |
| Argyrios Kyrtzidis | f89a56c | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 9069 | // Diagnose "arg1 'bitwise' arg2 'eq' arg3". |
| Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 9070 | if (BinaryOperator::isBitwiseOp(Opc)) |
| Argyrios Kyrtzidis | f89a56c | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 9071 | return DiagnoseBitwisePrecedence(Self, Opc, OpLoc, lhs, rhs); |
| 9072 | |
| Argyrios Kyrtzidis | 14a9662 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 9073 | // Warn about arg1 || arg2 && arg3, as GCC 4.3+ does. |
| 9074 | // We don't warn for 'assert(a || b && "bad")' since this is safe. |
| Argyrios Kyrtzidis | b94e5a3 | 2010-11-17 18:54:22 +0000 | [diff] [blame] | 9075 | if (Opc == BO_LOr && !OpLoc.isMacroID()/* Don't warn in macros. */) { |
| Argyrios Kyrtzidis | 56e879d | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 9076 | DiagnoseLogicalAndInLogicalOrLHS(Self, OpLoc, lhs, rhs); |
| 9077 | DiagnoseLogicalAndInLogicalOrRHS(Self, OpLoc, lhs, rhs); |
| Argyrios Kyrtzidis | f89a56c | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 9078 | } |
| Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 9079 | } |
| 9080 | |
| Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 9081 | // Binary Operators. 'Tok' is the token for the operator. |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9082 | ExprResult Sema::ActOnBinOp(Scope *S, SourceLocation TokLoc, |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9083 | tok::TokenKind Kind, |
| 9084 | Expr *lhs, Expr *rhs) { |
| 9085 | BinaryOperatorKind Opc = ConvertTokenKindToBinaryOpcode(Kind); |
| Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 9086 | assert((lhs != 0) && "ActOnBinOp(): missing left expression"); |
| 9087 | assert((rhs != 0) && "ActOnBinOp(): missing right expression"); |
| Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 9088 | |
| Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 9089 | // Emit warnings for tricky precedence issues, e.g. "bitfield & 0x4 == 0" |
| 9090 | DiagnoseBinOpPrecedence(*this, Opc, TokLoc, lhs, rhs); |
| 9091 | |
| Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 9092 | return BuildBinOp(S, TokLoc, Opc, lhs, rhs); |
| 9093 | } |
| 9094 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9095 | ExprResult Sema::BuildBinOp(Scope *S, SourceLocation OpLoc, |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9096 | BinaryOperatorKind Opc, |
| 9097 | Expr *lhs, Expr *rhs) { |
| John McCall | 622114c | 2010-12-06 05:26:58 +0000 | [diff] [blame] | 9098 | if (getLangOptions().CPlusPlus) { |
| 9099 | bool UseBuiltinOperator; |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9100 | |
| John McCall | 622114c | 2010-12-06 05:26:58 +0000 | [diff] [blame] | 9101 | if (lhs->isTypeDependent() || rhs->isTypeDependent()) { |
| 9102 | UseBuiltinOperator = false; |
| 9103 | } else if (Opc == BO_Assign && lhs->getObjectKind() == OK_ObjCProperty) { |
| 9104 | UseBuiltinOperator = true; |
| 9105 | } else { |
| 9106 | UseBuiltinOperator = !lhs->getType()->isOverloadableType() && |
| 9107 | !rhs->getType()->isOverloadableType(); |
| 9108 | } |
| 9109 | |
| 9110 | if (!UseBuiltinOperator) { |
| 9111 | // Find all of the overloaded operators visible from this |
| 9112 | // point. We perform both an operator-name lookup from the local |
| 9113 | // scope and an argument-dependent lookup based on the types of |
| 9114 | // the arguments. |
| 9115 | UnresolvedSet<16> Functions; |
| 9116 | OverloadedOperatorKind OverOp |
| 9117 | = BinaryOperator::getOverloadedOperator(Opc); |
| 9118 | if (S && OverOp != OO_None) |
| 9119 | LookupOverloadedOperatorName(OverOp, S, lhs->getType(), rhs->getType(), |
| 9120 | Functions); |
| 9121 | |
| 9122 | // Build the (potentially-overloaded, potentially-dependent) |
| 9123 | // binary operation. |
| 9124 | return CreateOverloadedBinOp(OpLoc, Opc, Functions, lhs, rhs); |
| 9125 | } |
| Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 9126 | } |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9127 | |
| Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 9128 | // Build a built-in binary operation. |
| Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 9129 | return CreateBuiltinBinOp(OpLoc, Opc, lhs, rhs); |
| Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 9130 | } |
| 9131 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9132 | ExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc, |
| Argyrios Kyrtzidis | 7a808c0 | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 9133 | UnaryOperatorKind Opc, |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9134 | Expr *InputExpr) { |
| 9135 | ExprResult Input = Owned(InputExpr); |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9136 | ExprValueKind VK = VK_RValue; |
| 9137 | ExprObjectKind OK = OK_Ordinary; |
| Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 9138 | QualType resultType; |
| 9139 | switch (Opc) { |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9140 | case UO_PreInc: |
| 9141 | case UO_PreDec: |
| 9142 | case UO_PostInc: |
| 9143 | case UO_PostDec: |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9144 | resultType = CheckIncrementDecrementOperand(*this, Input.get(), VK, OpLoc, |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9145 | Opc == UO_PreInc || |
| 9146 | Opc == UO_PostInc, |
| 9147 | Opc == UO_PreInc || |
| 9148 | Opc == UO_PreDec); |
| Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 9149 | break; |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9150 | case UO_AddrOf: |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9151 | resultType = CheckAddressOfOperand(*this, Input.get(), OpLoc); |
| Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 9152 | break; |
| John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9153 | case UO_Deref: { |
| John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 9154 | ExprResult resolved = CheckPlaceholderExpr(Input.get()); |
| John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9155 | if (!resolved.isUsable()) return ExprError(); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9156 | Input = move(resolved); |
| 9157 | Input = DefaultFunctionArrayLvalueConversion(Input.take()); |
| 9158 | resultType = CheckIndirectionOperand(*this, Input.get(), VK, OpLoc); |
| Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 9159 | break; |
| John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9160 | } |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9161 | case UO_Plus: |
| 9162 | case UO_Minus: |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9163 | Input = UsualUnaryConversions(Input.take()); |
| 9164 | if (Input.isInvalid()) return ExprError(); |
| 9165 | resultType = Input.get()->getType(); |
| Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9166 | if (resultType->isDependentType()) |
| 9167 | break; |
| Douglas Gregor | a3208f9 | 2010-06-22 23:41:02 +0000 | [diff] [blame] | 9168 | if (resultType->isArithmeticType() || // C99 6.5.3.3p1 |
| 9169 | resultType->isVectorType()) |
| Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 9170 | break; |
| 9171 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6-7 |
| 9172 | resultType->isEnumeralType()) |
| 9173 | break; |
| 9174 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6 |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9175 | Opc == UO_Plus && |
| Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 9176 | resultType->isPointerType()) |
| 9177 | break; |
| John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9178 | else if (resultType->isPlaceholderType()) { |
| John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 9179 | Input = CheckPlaceholderExpr(Input.take()); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9180 | if (Input.isInvalid()) return ExprError(); |
| 9181 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input.take()); |
| John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9182 | } |
| Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 9183 | |
| Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 9184 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9185 | << resultType << Input.get()->getSourceRange()); |
| 9186 | |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9187 | case UO_Not: // bitwise complement |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9188 | Input = UsualUnaryConversions(Input.take()); |
| 9189 | if (Input.isInvalid()) return ExprError(); |
| 9190 | resultType = Input.get()->getType(); |
| Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9191 | if (resultType->isDependentType()) |
| 9192 | break; |
| Chris Lattner | 0d70761 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 9193 | // C99 6.5.3.3p1. We allow complex int and float as a GCC extension. |
| 9194 | if (resultType->isComplexType() || resultType->isComplexIntegerType()) |
| 9195 | // C99 does not support '~' for complex conjugation. |
| Chris Lattner | 29e812b | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 9196 | Diag(OpLoc, diag::ext_integer_complement_complex) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9197 | << resultType << Input.get()->getSourceRange(); |
| John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9198 | else if (resultType->hasIntegerRepresentation()) |
| 9199 | break; |
| 9200 | else if (resultType->isPlaceholderType()) { |
| John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 9201 | Input = CheckPlaceholderExpr(Input.take()); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9202 | if (Input.isInvalid()) return ExprError(); |
| 9203 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input.take()); |
| John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9204 | } else { |
| Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 9205 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9206 | << resultType << Input.get()->getSourceRange()); |
| John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9207 | } |
| Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 9208 | break; |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9209 | |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9210 | case UO_LNot: // logical negation |
| Steve Naroff | 71b59a9 | 2007-06-04 22:22:31 +0000 | [diff] [blame] | 9211 | // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5). |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9212 | Input = DefaultFunctionArrayLvalueConversion(Input.take()); |
| 9213 | if (Input.isInvalid()) return ExprError(); |
| 9214 | resultType = Input.get()->getType(); |
| Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9215 | if (resultType->isDependentType()) |
| 9216 | break; |
| Abramo Bagnara | 7ccce98 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 9217 | if (resultType->isScalarType()) { |
| 9218 | // C99 6.5.3.3p1: ok, fallthrough; |
| 9219 | if (Context.getLangOptions().CPlusPlus) { |
| 9220 | // C++03 [expr.unary.op]p8, C++0x [expr.unary.op]p9: |
| 9221 | // operand contextually converted to bool. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9222 | Input = ImpCastExprToType(Input.take(), Context.BoolTy, |
| 9223 | ScalarTypeToBooleanCastKind(resultType)); |
| Abramo Bagnara | 7ccce98 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 9224 | } |
| John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9225 | } else if (resultType->isPlaceholderType()) { |
| John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 9226 | Input = CheckPlaceholderExpr(Input.take()); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9227 | if (Input.isInvalid()) return ExprError(); |
| 9228 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input.take()); |
| John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9229 | } else { |
| Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 9230 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9231 | << resultType << Input.get()->getSourceRange()); |
| John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9232 | } |
| Douglas Gregor | db8c6fd | 2010-09-20 17:13:33 +0000 | [diff] [blame] | 9233 | |
| Chris Lattner | be31ed8 | 2007-06-02 19:11:33 +0000 | [diff] [blame] | 9234 | // LNot always has type int. C99 6.5.3.3p5. |
| Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 9235 | // In C++, it's bool. C++ 5.3.1p8 |
| Argyrios Kyrtzidis | 1bdd688 | 2011-02-18 20:55:15 +0000 | [diff] [blame] | 9236 | resultType = Context.getLogicalOperationType(); |
| Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 9237 | break; |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9238 | case UO_Real: |
| 9239 | case UO_Imag: |
| John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 9240 | resultType = CheckRealImagOperand(*this, Input, OpLoc, Opc == UO_Real); |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9241 | // _Real and _Imag map ordinary l-values into ordinary l-values. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9242 | if (Input.isInvalid()) return ExprError(); |
| 9243 | if (Input.get()->getValueKind() != VK_RValue && |
| 9244 | Input.get()->getObjectKind() == OK_Ordinary) |
| 9245 | VK = Input.get()->getValueKind(); |
| Chris Lattner | 30b5dd0 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 9246 | break; |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9247 | case UO_Extension: |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9248 | resultType = Input.get()->getType(); |
| 9249 | VK = Input.get()->getValueKind(); |
| 9250 | OK = Input.get()->getObjectKind(); |
| Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 9251 | break; |
| Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 9252 | } |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9253 | if (resultType.isNull() || Input.isInvalid()) |
| Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 9254 | return ExprError(); |
| Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9255 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9256 | return Owned(new (Context) UnaryOperator(Input.take(), Opc, resultType, |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9257 | VK, OK, OpLoc)); |
| Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 9258 | } |
| Chris Lattner | eefa10e | 2007-05-28 06:56:27 +0000 | [diff] [blame] | 9259 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9260 | ExprResult Sema::BuildUnaryOp(Scope *S, SourceLocation OpLoc, |
| John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9261 | UnaryOperatorKind Opc, |
| 9262 | Expr *Input) { |
| Anders Carlsson | 461a2c0 | 2009-11-14 21:26:41 +0000 | [diff] [blame] | 9263 | if (getLangOptions().CPlusPlus && Input->getType()->isOverloadableType() && |
| Eli Friedman | 8ed2bac | 2010-09-05 23:15:52 +0000 | [diff] [blame] | 9264 | UnaryOperator::getOverloadedOperator(Opc) != OO_None) { |
| Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9265 | // Find all of the overloaded operators visible from this |
| 9266 | // point. We perform both an operator-name lookup from the local |
| 9267 | // scope and an argument-dependent lookup based on the types of |
| 9268 | // the arguments. |
| John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 9269 | UnresolvedSet<16> Functions; |
| Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9270 | OverloadedOperatorKind OverOp = UnaryOperator::getOverloadedOperator(Opc); |
| John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 9271 | if (S && OverOp != OO_None) |
| 9272 | LookupOverloadedOperatorName(OverOp, S, Input->getType(), QualType(), |
| 9273 | Functions); |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9274 | |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9275 | return CreateOverloadedUnaryOp(OpLoc, Opc, Functions, Input); |
| Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9276 | } |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9277 | |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9278 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
| Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9279 | } |
| 9280 | |
| Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 9281 | // Unary Operators. 'Tok' is the token for the operator. |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9282 | ExprResult Sema::ActOnUnaryOp(Scope *S, SourceLocation OpLoc, |
| John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 9283 | tok::TokenKind Op, Expr *Input) { |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9284 | return BuildUnaryOp(S, OpLoc, ConvertTokenKindToUnaryOpcode(Op), Input); |
| Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 9285 | } |
| 9286 | |
| Steve Naroff | 66356bd | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 9287 | /// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo". |
| Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 9288 | ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc, SourceLocation LabLoc, |
| Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 9289 | LabelDecl *TheDecl) { |
| Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 9290 | TheDecl->setUsed(); |
| Chris Lattner | eefa10e | 2007-05-28 06:56:27 +0000 | [diff] [blame] | 9291 | // Create the AST node. The address of a label always has type 'void*'. |
| Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 9292 | return Owned(new (Context) AddrLabelExpr(OpLoc, LabLoc, TheDecl, |
| Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9293 | Context.getPointerType(Context.VoidTy))); |
| Chris Lattner | eefa10e | 2007-05-28 06:56:27 +0000 | [diff] [blame] | 9294 | } |
| 9295 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9296 | ExprResult |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9297 | Sema::ActOnStmtExpr(SourceLocation LPLoc, Stmt *SubStmt, |
| Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9298 | SourceLocation RPLoc) { // "({..})" |
| Chris Lattner | 366727f | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 9299 | assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!"); |
| 9300 | CompoundStmt *Compound = cast<CompoundStmt>(SubStmt); |
| 9301 | |
| Douglas Gregor | 6cf3f3c | 2010-03-10 04:54:39 +0000 | [diff] [blame] | 9302 | bool isFileScope |
| 9303 | = (getCurFunctionOrMethodDecl() == 0) && (getCurBlock() == 0); |
| Chris Lattner | a69b076 | 2009-04-25 19:11:05 +0000 | [diff] [blame] | 9304 | if (isFileScope) |
| Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9305 | return ExprError(Diag(LPLoc, diag::err_stmtexpr_file_scope)); |
| Eli Friedman | 52cc016 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 9306 | |
| Chris Lattner | 366727f | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 9307 | // FIXME: there are a variety of strange constraints to enforce here, for |
| 9308 | // example, it is not possible to goto into a stmt expression apparently. |
| 9309 | // More semantic analysis is needed. |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9310 | |
| Chris Lattner | 366727f | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 9311 | // If there are sub stmts in the compound stmt, take the type of the last one |
| 9312 | // as the type of the stmtexpr. |
| 9313 | QualType Ty = Context.VoidTy; |
| Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9314 | bool StmtExprMayBindToTemp = false; |
| Chris Lattner | 944d306 | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 9315 | if (!Compound->body_empty()) { |
| 9316 | Stmt *LastStmt = Compound->body_back(); |
| Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9317 | LabelStmt *LastLabelStmt = 0; |
| Chris Lattner | 944d306 | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 9318 | // If LastStmt is a label, skip down through into the body. |
| Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9319 | while (LabelStmt *Label = dyn_cast<LabelStmt>(LastStmt)) { |
| 9320 | LastLabelStmt = Label; |
| Chris Lattner | 944d306 | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 9321 | LastStmt = Label->getSubStmt(); |
| Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9322 | } |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9323 | if (Expr *LastE = dyn_cast<Expr>(LastStmt)) { |
| John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 9324 | // Do function/array conversion on the last expression, but not |
| 9325 | // lvalue-to-rvalue. However, initialize an unqualified type. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9326 | ExprResult LastExpr = DefaultFunctionArrayConversion(LastE); |
| 9327 | if (LastExpr.isInvalid()) |
| 9328 | return ExprError(); |
| 9329 | Ty = LastExpr.get()->getType().getUnqualifiedType(); |
| John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 9330 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9331 | if (!Ty->isDependentType() && !LastExpr.get()->isTypeDependent()) { |
| 9332 | LastExpr = PerformCopyInitialization( |
| Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9333 | InitializedEntity::InitializeResult(LPLoc, |
| 9334 | Ty, |
| 9335 | false), |
| 9336 | SourceLocation(), |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9337 | LastExpr); |
| 9338 | if (LastExpr.isInvalid()) |
| Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9339 | return ExprError(); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9340 | if (LastExpr.get() != 0) { |
| Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9341 | if (!LastLabelStmt) |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9342 | Compound->setLastStmt(LastExpr.take()); |
| Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9343 | else |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9344 | LastLabelStmt->setSubStmt(LastExpr.take()); |
| Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9345 | StmtExprMayBindToTemp = true; |
| 9346 | } |
| 9347 | } |
| 9348 | } |
| Chris Lattner | 944d306 | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 9349 | } |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9350 | |
| Eli Friedman | ba961a9 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 9351 | // FIXME: Check that expression type is complete/non-abstract; statement |
| 9352 | // expressions are not lvalues. |
| Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9353 | Expr *ResStmtExpr = new (Context) StmtExpr(Compound, Ty, LPLoc, RPLoc); |
| 9354 | if (StmtExprMayBindToTemp) |
| 9355 | return MaybeBindToTemporary(ResStmtExpr); |
| 9356 | return Owned(ResStmtExpr); |
| Chris Lattner | 366727f | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 9357 | } |
| Steve Naroff | 7886467 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 9358 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9359 | ExprResult Sema::BuildBuiltinOffsetOf(SourceLocation BuiltinLoc, |
| John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9360 | TypeSourceInfo *TInfo, |
| 9361 | OffsetOfComponent *CompPtr, |
| 9362 | unsigned NumComponents, |
| 9363 | SourceLocation RParenLoc) { |
| Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9364 | QualType ArgTy = TInfo->getType(); |
| Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9365 | bool Dependent = ArgTy->isDependentType(); |
| Abramo Bagnara | 1108e7b | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 9366 | SourceRange TypeRange = TInfo->getTypeLoc().getLocalSourceRange(); |
| Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9367 | |
| Chris Lattner | f17bd42 | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 9368 | // We must have at least one component that refers to the type, and the first |
| 9369 | // one is known to be a field designator. Verify that the ArgTy represents |
| 9370 | // a struct/union/class. |
| Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9371 | if (!Dependent && !ArgTy->isRecordType()) |
| Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9372 | return ExprError(Diag(BuiltinLoc, diag::err_offsetof_record_type) |
| 9373 | << ArgTy << TypeRange); |
| 9374 | |
| 9375 | // Type must be complete per C99 7.17p3 because a declaring a variable |
| 9376 | // with an incomplete type would be ill-formed. |
| 9377 | if (!Dependent |
| 9378 | && RequireCompleteType(BuiltinLoc, ArgTy, |
| 9379 | PDiag(diag::err_offsetof_incomplete_type) |
| 9380 | << TypeRange)) |
| 9381 | return ExprError(); |
| 9382 | |
| Chris Lattner | 78502cf | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 9383 | // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a |
| 9384 | // GCC extension, diagnose them. |
| Eli Friedman | 988a16b | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 9385 | // FIXME: This diagnostic isn't actually visible because the location is in |
| 9386 | // a system header! |
| Chris Lattner | 78502cf | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 9387 | if (NumComponents != 1) |
| Chris Lattner | f490e15 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 9388 | Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator) |
| 9389 | << SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd); |
| Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9390 | |
| 9391 | bool DidWarnAboutNonPOD = false; |
| 9392 | QualType CurrentType = ArgTy; |
| 9393 | typedef OffsetOfExpr::OffsetOfNode OffsetOfNode; |
| 9394 | llvm::SmallVector<OffsetOfNode, 4> Comps; |
| 9395 | llvm::SmallVector<Expr*, 4> Exprs; |
| 9396 | for (unsigned i = 0; i != NumComponents; ++i) { |
| 9397 | const OffsetOfComponent &OC = CompPtr[i]; |
| 9398 | if (OC.isBrackets) { |
| 9399 | // Offset of an array sub-field. TODO: Should we allow vector elements? |
| 9400 | if (!CurrentType->isDependentType()) { |
| 9401 | const ArrayType *AT = Context.getAsArrayType(CurrentType); |
| 9402 | if(!AT) |
| 9403 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_array_type) |
| 9404 | << CurrentType); |
| 9405 | CurrentType = AT->getElementType(); |
| 9406 | } else |
| 9407 | CurrentType = Context.DependentTy; |
| 9408 | |
| 9409 | // The expression must be an integral expression. |
| 9410 | // FIXME: An integral constant expression? |
| 9411 | Expr *Idx = static_cast<Expr*>(OC.U.E); |
| 9412 | if (!Idx->isTypeDependent() && !Idx->isValueDependent() && |
| 9413 | !Idx->getType()->isIntegerType()) |
| 9414 | return ExprError(Diag(Idx->getLocStart(), |
| 9415 | diag::err_typecheck_subscript_not_integer) |
| 9416 | << Idx->getSourceRange()); |
| 9417 | |
| 9418 | // Record this array index. |
| 9419 | Comps.push_back(OffsetOfNode(OC.LocStart, Exprs.size(), OC.LocEnd)); |
| 9420 | Exprs.push_back(Idx); |
| 9421 | continue; |
| 9422 | } |
| 9423 | |
| 9424 | // Offset of a field. |
| 9425 | if (CurrentType->isDependentType()) { |
| 9426 | // We have the offset of a field, but we can't look into the dependent |
| 9427 | // type. Just record the identifier of the field. |
| 9428 | Comps.push_back(OffsetOfNode(OC.LocStart, OC.U.IdentInfo, OC.LocEnd)); |
| 9429 | CurrentType = Context.DependentTy; |
| 9430 | continue; |
| 9431 | } |
| 9432 | |
| 9433 | // We need to have a complete type to look into. |
| 9434 | if (RequireCompleteType(OC.LocStart, CurrentType, |
| 9435 | diag::err_offsetof_incomplete_type)) |
| 9436 | return ExprError(); |
| 9437 | |
| 9438 | // Look for the designated field. |
| 9439 | const RecordType *RC = CurrentType->getAs<RecordType>(); |
| 9440 | if (!RC) |
| 9441 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_record_type) |
| 9442 | << CurrentType); |
| 9443 | RecordDecl *RD = RC->getDecl(); |
| 9444 | |
| 9445 | // C++ [lib.support.types]p5: |
| 9446 | // The macro offsetof accepts a restricted set of type arguments in this |
| 9447 | // International Standard. type shall be a POD structure or a POD union |
| 9448 | // (clause 9). |
| 9449 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 9450 | if (!CRD->isPOD() && !DidWarnAboutNonPOD && |
| Ted Kremenek | 55ae319 | 2011-02-23 01:51:43 +0000 | [diff] [blame] | 9451 | DiagRuntimeBehavior(BuiltinLoc, 0, |
| Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9452 | PDiag(diag::warn_offsetof_non_pod_type) |
| 9453 | << SourceRange(CompPtr[0].LocStart, OC.LocEnd) |
| 9454 | << CurrentType)) |
| 9455 | DidWarnAboutNonPOD = true; |
| 9456 | } |
| 9457 | |
| 9458 | // Look for the field. |
| 9459 | LookupResult R(*this, OC.U.IdentInfo, OC.LocStart, LookupMemberName); |
| 9460 | LookupQualifiedName(R, RD); |
| 9461 | FieldDecl *MemberDecl = R.getAsSingle<FieldDecl>(); |
| Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 9462 | IndirectFieldDecl *IndirectMemberDecl = 0; |
| 9463 | if (!MemberDecl) { |
| Benjamin Kramer | 3959370 | 2010-11-21 14:11:41 +0000 | [diff] [blame] | 9464 | if ((IndirectMemberDecl = R.getAsSingle<IndirectFieldDecl>())) |
| Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 9465 | MemberDecl = IndirectMemberDecl->getAnonField(); |
| 9466 | } |
| 9467 | |
| Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9468 | if (!MemberDecl) |
| 9469 | return ExprError(Diag(BuiltinLoc, diag::err_no_member) |
| 9470 | << OC.U.IdentInfo << RD << SourceRange(OC.LocStart, |
| 9471 | OC.LocEnd)); |
| 9472 | |
| Douglas Gregor | 10982ea | 2010-04-28 22:36:06 +0000 | [diff] [blame] | 9473 | // C99 7.17p3: |
| 9474 | // (If the specified member is a bit-field, the behavior is undefined.) |
| 9475 | // |
| 9476 | // We diagnose this as an error. |
| 9477 | if (MemberDecl->getBitWidth()) { |
| 9478 | Diag(OC.LocEnd, diag::err_offsetof_bitfield) |
| 9479 | << MemberDecl->getDeclName() |
| 9480 | << SourceRange(BuiltinLoc, RParenLoc); |
| 9481 | Diag(MemberDecl->getLocation(), diag::note_bitfield_decl); |
| 9482 | return ExprError(); |
| 9483 | } |
| Eli Friedman | 74ef7cf | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 9484 | |
| 9485 | RecordDecl *Parent = MemberDecl->getParent(); |
| Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 9486 | if (IndirectMemberDecl) |
| 9487 | Parent = cast<RecordDecl>(IndirectMemberDecl->getDeclContext()); |
| Eli Friedman | 74ef7cf | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 9488 | |
| Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 9489 | // If the member was found in a base class, introduce OffsetOfNodes for |
| 9490 | // the base class indirections. |
| 9491 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 9492 | /*DetectVirtual=*/false); |
| Eli Friedman | 74ef7cf | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 9493 | if (IsDerivedFrom(CurrentType, Context.getTypeDeclType(Parent), Paths)) { |
| Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 9494 | CXXBasePath &Path = Paths.front(); |
| 9495 | for (CXXBasePath::iterator B = Path.begin(), BEnd = Path.end(); |
| 9496 | B != BEnd; ++B) |
| 9497 | Comps.push_back(OffsetOfNode(B->Base)); |
| 9498 | } |
| Eli Friedman | 74ef7cf | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 9499 | |
| Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 9500 | if (IndirectMemberDecl) { |
| 9501 | for (IndirectFieldDecl::chain_iterator FI = |
| 9502 | IndirectMemberDecl->chain_begin(), |
| 9503 | FEnd = IndirectMemberDecl->chain_end(); FI != FEnd; FI++) { |
| 9504 | assert(isa<FieldDecl>(*FI)); |
| 9505 | Comps.push_back(OffsetOfNode(OC.LocStart, |
| 9506 | cast<FieldDecl>(*FI), OC.LocEnd)); |
| 9507 | } |
| 9508 | } else |
| Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9509 | Comps.push_back(OffsetOfNode(OC.LocStart, MemberDecl, OC.LocEnd)); |
| Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 9510 | |
| Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9511 | CurrentType = MemberDecl->getType().getNonReferenceType(); |
| 9512 | } |
| 9513 | |
| 9514 | return Owned(OffsetOfExpr::Create(Context, Context.getSizeType(), BuiltinLoc, |
| 9515 | TInfo, Comps.data(), Comps.size(), |
| 9516 | Exprs.data(), Exprs.size(), RParenLoc)); |
| 9517 | } |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9518 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9519 | ExprResult Sema::ActOnBuiltinOffsetOf(Scope *S, |
| John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9520 | SourceLocation BuiltinLoc, |
| 9521 | SourceLocation TypeLoc, |
| 9522 | ParsedType argty, |
| 9523 | OffsetOfComponent *CompPtr, |
| 9524 | unsigned NumComponents, |
| 9525 | SourceLocation RPLoc) { |
| 9526 | |
| Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9527 | TypeSourceInfo *ArgTInfo; |
| 9528 | QualType ArgTy = GetTypeFromParser(argty, &ArgTInfo); |
| 9529 | if (ArgTy.isNull()) |
| 9530 | return ExprError(); |
| 9531 | |
| Eli Friedman | 06dcfd9 | 2010-08-05 10:15:45 +0000 | [diff] [blame] | 9532 | if (!ArgTInfo) |
| 9533 | ArgTInfo = Context.getTrivialTypeSourceInfo(ArgTy, TypeLoc); |
| 9534 | |
| 9535 | return BuildBuiltinOffsetOf(BuiltinLoc, ArgTInfo, CompPtr, NumComponents, |
| 9536 | RPLoc); |
| Chris Lattner | f17bd42 | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 9537 | } |
| 9538 | |
| 9539 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9540 | ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, |
| John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9541 | Expr *CondExpr, |
| 9542 | Expr *LHSExpr, Expr *RHSExpr, |
| 9543 | SourceLocation RPLoc) { |
| Steve Naroff | 9efdabc | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 9544 | assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)"); |
| 9545 | |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9546 | ExprValueKind VK = VK_RValue; |
| 9547 | ExprObjectKind OK = OK_Ordinary; |
| Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9548 | QualType resType; |
| Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 9549 | bool ValueDependent = false; |
| Douglas Gregor | 0df9112 | 2009-05-19 22:43:30 +0000 | [diff] [blame] | 9550 | if (CondExpr->isTypeDependent() || CondExpr->isValueDependent()) { |
| Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9551 | resType = Context.DependentTy; |
| Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 9552 | ValueDependent = true; |
| Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9553 | } else { |
| 9554 | // The conditional expression is required to be a constant expression. |
| 9555 | llvm::APSInt condEval(32); |
| 9556 | SourceLocation ExpLoc; |
| 9557 | if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc)) |
| Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9558 | return ExprError(Diag(ExpLoc, |
| 9559 | diag::err_typecheck_choose_expr_requires_constant) |
| 9560 | << CondExpr->getSourceRange()); |
| Steve Naroff | 9efdabc | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 9561 | |
| Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9562 | // If the condition is > zero, then the AST type is the same as the LSHExpr. |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9563 | Expr *ActiveExpr = condEval.getZExtValue() ? LHSExpr : RHSExpr; |
| 9564 | |
| 9565 | resType = ActiveExpr->getType(); |
| 9566 | ValueDependent = ActiveExpr->isValueDependent(); |
| 9567 | VK = ActiveExpr->getValueKind(); |
| 9568 | OK = ActiveExpr->getObjectKind(); |
| Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9569 | } |
| 9570 | |
| Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9571 | return Owned(new (Context) ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9572 | resType, VK, OK, RPLoc, |
| Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 9573 | resType->isDependentType(), |
| 9574 | ValueDependent)); |
| Steve Naroff | 9efdabc | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 9575 | } |
| 9576 | |
| Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9577 | //===----------------------------------------------------------------------===// |
| 9578 | // Clang Extensions. |
| 9579 | //===----------------------------------------------------------------------===// |
| 9580 | |
| 9581 | /// ActOnBlockStart - This callback is invoked when a block literal is started. |
| Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9582 | void Sema::ActOnBlockStart(SourceLocation CaretLoc, Scope *BlockScope) { |
| Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9583 | BlockDecl *Block = BlockDecl::Create(Context, CurContext, CaretLoc); |
| 9584 | PushBlockScope(BlockScope, Block); |
| 9585 | CurContext->addDecl(Block); |
| Fariborz Jahanian | 1babe77 | 2010-07-09 18:44:02 +0000 | [diff] [blame] | 9586 | if (BlockScope) |
| 9587 | PushDeclContext(BlockScope, Block); |
| 9588 | else |
| 9589 | CurContext = Block; |
| Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9590 | } |
| 9591 | |
| Mike Stump | 82f071f | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 9592 | void Sema::ActOnBlockArguments(Declarator &ParamInfo, Scope *CurScope) { |
| Mike Stump | f70bcf7 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 9593 | assert(ParamInfo.getIdentifier()==0 && "block-id should have no identifier!"); |
| John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9594 | assert(ParamInfo.getContext() == Declarator::BlockLiteralContext); |
| Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9595 | BlockScopeInfo *CurBlock = getCurBlock(); |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9596 | |
| John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 9597 | TypeSourceInfo *Sig = GetTypeForDeclarator(ParamInfo, CurScope); |
| John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 9598 | QualType T = Sig->getType(); |
| Mike Stump | 82f071f | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 9599 | |
| John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9600 | // GetTypeForDeclarator always produces a function type for a block |
| 9601 | // literal signature. Furthermore, it is always a FunctionProtoType |
| 9602 | // unless the function was written with a typedef. |
| 9603 | assert(T->isFunctionType() && |
| 9604 | "GetTypeForDeclarator made a non-function block signature"); |
| 9605 | |
| 9606 | // Look for an explicit signature in that function type. |
| 9607 | FunctionProtoTypeLoc ExplicitSignature; |
| 9608 | |
| 9609 | TypeLoc tmp = Sig->getTypeLoc().IgnoreParens(); |
| 9610 | if (isa<FunctionProtoTypeLoc>(tmp)) { |
| 9611 | ExplicitSignature = cast<FunctionProtoTypeLoc>(tmp); |
| 9612 | |
| 9613 | // Check whether that explicit signature was synthesized by |
| 9614 | // GetTypeForDeclarator. If so, don't save that as part of the |
| 9615 | // written signature. |
| Abramo Bagnara | f2a79d9 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 9616 | if (ExplicitSignature.getLocalRangeBegin() == |
| 9617 | ExplicitSignature.getLocalRangeEnd()) { |
| John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9618 | // This would be much cheaper if we stored TypeLocs instead of |
| 9619 | // TypeSourceInfos. |
| 9620 | TypeLoc Result = ExplicitSignature.getResultLoc(); |
| 9621 | unsigned Size = Result.getFullDataSize(); |
| 9622 | Sig = Context.CreateTypeSourceInfo(Result.getType(), Size); |
| 9623 | Sig->getTypeLoc().initializeFullCopy(Result, Size); |
| 9624 | |
| 9625 | ExplicitSignature = FunctionProtoTypeLoc(); |
| 9626 | } |
| John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9627 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9628 | |
| John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9629 | CurBlock->TheDecl->setSignatureAsWritten(Sig); |
| 9630 | CurBlock->FunctionType = T; |
| 9631 | |
| 9632 | const FunctionType *Fn = T->getAs<FunctionType>(); |
| 9633 | QualType RetTy = Fn->getResultType(); |
| 9634 | bool isVariadic = |
| 9635 | (isa<FunctionProtoType>(Fn) && cast<FunctionProtoType>(Fn)->isVariadic()); |
| 9636 | |
| John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9637 | CurBlock->TheDecl->setIsVariadic(isVariadic); |
| Douglas Gregor | b92a156 | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 9638 | |
| John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9639 | // Don't allow returning a objc interface by value. |
| 9640 | if (RetTy->isObjCObjectType()) { |
| 9641 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 9642 | diag::err_object_cannot_be_passed_returned_by_value) << 0 << RetTy; |
| 9643 | return; |
| 9644 | } |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9645 | |
| John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9646 | // Context.DependentTy is used as a placeholder for a missing block |
| John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9647 | // return type. TODO: what should we do with declarators like: |
| 9648 | // ^ * { ... } |
| 9649 | // If the answer is "apply template argument deduction".... |
| John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9650 | if (RetTy != Context.DependentTy) |
| 9651 | CurBlock->ReturnType = RetTy; |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9652 | |
| John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9653 | // Push block parameters from the declarator if we had them. |
| John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9654 | llvm::SmallVector<ParmVarDecl*, 8> Params; |
| John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9655 | if (ExplicitSignature) { |
| 9656 | for (unsigned I = 0, E = ExplicitSignature.getNumArgs(); I != E; ++I) { |
| 9657 | ParmVarDecl *Param = ExplicitSignature.getArg(I); |
| Fariborz Jahanian | 5ec502e | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 9658 | if (Param->getIdentifier() == 0 && |
| 9659 | !Param->isImplicit() && |
| 9660 | !Param->isInvalidDecl() && |
| 9661 | !getLangOptions().CPlusPlus) |
| 9662 | Diag(Param->getLocation(), diag::err_parameter_name_omitted); |
| John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9663 | Params.push_back(Param); |
| Fariborz Jahanian | 5ec502e | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 9664 | } |
| John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9665 | |
| 9666 | // Fake up parameter variables if we have a typedef, like |
| 9667 | // ^ fntype { ... } |
| 9668 | } else if (const FunctionProtoType *Fn = T->getAs<FunctionProtoType>()) { |
| 9669 | for (FunctionProtoType::arg_type_iterator |
| 9670 | I = Fn->arg_type_begin(), E = Fn->arg_type_end(); I != E; ++I) { |
| 9671 | ParmVarDecl *Param = |
| 9672 | BuildParmVarDeclForTypedef(CurBlock->TheDecl, |
| 9673 | ParamInfo.getSourceRange().getBegin(), |
| 9674 | *I); |
| John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9675 | Params.push_back(Param); |
| John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9676 | } |
| Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9677 | } |
| John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9678 | |
| John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9679 | // Set the parameters on the block decl. |
| Douglas Gregor | b524d90 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 9680 | if (!Params.empty()) { |
| John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9681 | CurBlock->TheDecl->setParams(Params.data(), Params.size()); |
| Douglas Gregor | b524d90 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 9682 | CheckParmsForFunctionDef(CurBlock->TheDecl->param_begin(), |
| 9683 | CurBlock->TheDecl->param_end(), |
| 9684 | /*CheckParameterNames=*/false); |
| 9685 | } |
| 9686 | |
| John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9687 | // Finally we can process decl attributes. |
| Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 9688 | ProcessDeclAttributes(CurScope, CurBlock->TheDecl, ParamInfo); |
| John McCall | df8b37c | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9689 | |
| John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9690 | if (!isVariadic && CurBlock->TheDecl->getAttr<SentinelAttr>()) { |
| John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9691 | Diag(ParamInfo.getAttributes()->getLoc(), |
| 9692 | diag::warn_attribute_sentinel_not_variadic) << 1; |
| 9693 | // FIXME: remove the attribute. |
| 9694 | } |
| 9695 | |
| 9696 | // Put the parameter variables in scope. We can bail out immediately |
| 9697 | // if we don't have any. |
| John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9698 | if (Params.empty()) |
| John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9699 | return; |
| 9700 | |
| Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9701 | for (BlockDecl::param_iterator AI = CurBlock->TheDecl->param_begin(), |
| John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 9702 | E = CurBlock->TheDecl->param_end(); AI != E; ++AI) { |
| 9703 | (*AI)->setOwningFunction(CurBlock->TheDecl); |
| 9704 | |
| Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9705 | // If this has an identifier, add it to the scope stack. |
| John McCall | df8b37c | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9706 | if ((*AI)->getIdentifier()) { |
| Argyrios Kyrtzidis | 1cb0de1 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 9707 | CheckShadow(CurBlock->TheScope, *AI); |
| John McCall | df8b37c | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9708 | |
| Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9709 | PushOnScopeChains(*AI, CurBlock->TheScope); |
| John McCall | df8b37c | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9710 | } |
| John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 9711 | } |
| Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9712 | } |
| 9713 | |
| 9714 | /// ActOnBlockError - If there is an error parsing a block, this callback |
| 9715 | /// is invoked to pop the information about the block from the action impl. |
| 9716 | void Sema::ActOnBlockError(SourceLocation CaretLoc, Scope *CurScope) { |
| Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9717 | // Pop off CurBlock, handle nested blocks. |
| Chris Lattner | 41b8694 | 2009-04-21 22:38:46 +0000 | [diff] [blame] | 9718 | PopDeclContext(); |
| Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9719 | PopFunctionOrBlockScope(); |
| Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9720 | } |
| 9721 | |
| 9722 | /// ActOnBlockStmtExpr - This is called when the body of a block statement |
| 9723 | /// literal was successfully completed. ^(int x){...} |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9724 | ExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, |
| Chris Lattner | 60f8449 | 2011-02-17 23:58:47 +0000 | [diff] [blame] | 9725 | Stmt *Body, Scope *CurScope) { |
| Chris Lattner | 9eac931 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 9726 | // If blocks are disabled, emit an error. |
| 9727 | if (!LangOpts.Blocks) |
| 9728 | Diag(CaretLoc, diag::err_blocks_disable); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9729 | |
| Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9730 | BlockScopeInfo *BSI = cast<BlockScopeInfo>(FunctionScopes.back()); |
| Fariborz Jahanian | 1babe77 | 2010-07-09 18:44:02 +0000 | [diff] [blame] | 9731 | |
| Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9732 | PopDeclContext(); |
| 9733 | |
| Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9734 | QualType RetTy = Context.VoidTy; |
| Fariborz Jahanian | 3fd7310 | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 9735 | if (!BSI->ReturnType.isNull()) |
| 9736 | RetTy = BSI->ReturnType; |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9737 | |
| Mike Stump | 3bf1ab4 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 9738 | bool NoReturn = BSI->TheDecl->getAttr<NoReturnAttr>(); |
| Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9739 | QualType BlockTy; |
| John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9740 | |
| John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 9741 | // Set the captured variables on the block. |
| John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 9742 | BSI->TheDecl->setCaptures(Context, BSI->Captures.begin(), BSI->Captures.end(), |
| 9743 | BSI->CapturesCXXThis); |
| John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 9744 | |
| John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9745 | // If the user wrote a function type in some form, try to use that. |
| 9746 | if (!BSI->FunctionType.isNull()) { |
| 9747 | const FunctionType *FTy = BSI->FunctionType->getAs<FunctionType>(); |
| 9748 | |
| 9749 | FunctionType::ExtInfo Ext = FTy->getExtInfo(); |
| 9750 | if (NoReturn && !Ext.getNoReturn()) Ext = Ext.withNoReturn(true); |
| 9751 | |
| 9752 | // Turn protoless block types into nullary block types. |
| 9753 | if (isa<FunctionNoProtoType>(FTy)) { |
| John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9754 | FunctionProtoType::ExtProtoInfo EPI; |
| 9755 | EPI.ExtInfo = Ext; |
| 9756 | BlockTy = Context.getFunctionType(RetTy, 0, 0, EPI); |
| John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9757 | |
| 9758 | // Otherwise, if we don't need to change anything about the function type, |
| 9759 | // preserve its sugar structure. |
| 9760 | } else if (FTy->getResultType() == RetTy && |
| 9761 | (!NoReturn || FTy->getNoReturnAttr())) { |
| 9762 | BlockTy = BSI->FunctionType; |
| 9763 | |
| 9764 | // Otherwise, make the minimal modifications to the function type. |
| 9765 | } else { |
| 9766 | const FunctionProtoType *FPT = cast<FunctionProtoType>(FTy); |
| John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9767 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 9768 | EPI.TypeQuals = 0; // FIXME: silently? |
| 9769 | EPI.ExtInfo = Ext; |
| John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9770 | BlockTy = Context.getFunctionType(RetTy, |
| 9771 | FPT->arg_type_begin(), |
| 9772 | FPT->getNumArgs(), |
| John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9773 | EPI); |
| John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9774 | } |
| 9775 | |
| 9776 | // If we don't have a function type, just build one from nothing. |
| 9777 | } else { |
| John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9778 | FunctionProtoType::ExtProtoInfo EPI; |
| Eli Friedman | c5b20b5 | 2011-04-09 08:18:08 +0000 | [diff] [blame] | 9779 | EPI.ExtInfo = FunctionType::ExtInfo(NoReturn, false, 0, CC_Default); |
| John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9780 | BlockTy = Context.getFunctionType(RetTy, 0, 0, EPI); |
| John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9781 | } |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9782 | |
| John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9783 | DiagnoseUnusedParameters(BSI->TheDecl->param_begin(), |
| 9784 | BSI->TheDecl->param_end()); |
| Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9785 | BlockTy = Context.getBlockPointerType(BlockTy); |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9786 | |
| Chris Lattner | 45542ea | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 9787 | // If needed, diagnose invalid gotos and switches in the block. |
| John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 9788 | if (getCurFunction()->NeedsScopeChecking() && !hasAnyErrorsInThisFunction()) |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9789 | DiagnoseInvalidJumps(cast<CompoundStmt>(Body)); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9790 | |
| Chris Lattner | 60f8449 | 2011-02-17 23:58:47 +0000 | [diff] [blame] | 9791 | BSI->TheDecl->setBody(cast<CompoundStmt>(Body)); |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9792 | |
| John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 9793 | BlockExpr *Result = new (Context) BlockExpr(BSI->TheDecl, BlockTy); |
| John McCall | 1d570a7 | 2010-08-25 05:56:39 +0000 | [diff] [blame] | 9794 | |
| Ted Kremenek | 1767a27 | 2011-02-23 01:51:48 +0000 | [diff] [blame] | 9795 | const AnalysisBasedWarnings::Policy &WP = AnalysisWarnings.getDefaultPolicy(); |
| 9796 | PopFunctionOrBlockScope(&WP, Result->getBlockDecl(), Result); |
| Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9797 | return Owned(Result); |
| Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9798 | } |
| 9799 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9800 | ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc, |
| John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 9801 | Expr *expr, ParsedType type, |
| Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9802 | SourceLocation RPLoc) { |
| Abramo Bagnara | 27db239 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 9803 | TypeSourceInfo *TInfo; |
| Jeffrey Yasskin | 8dfa5f1 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 9804 | GetTypeFromParser(type, &TInfo); |
| John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9805 | return BuildVAArgExpr(BuiltinLoc, expr, TInfo, RPLoc); |
| Abramo Bagnara | 27db239 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 9806 | } |
| 9807 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9808 | ExprResult Sema::BuildVAArgExpr(SourceLocation BuiltinLoc, |
| John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9809 | Expr *E, TypeSourceInfo *TInfo, |
| 9810 | SourceLocation RPLoc) { |
| Chris Lattner | 56382aa | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 9811 | Expr *OrigExpr = E; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9812 | |
| Eli Friedman | 121ba0c | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 9813 | // Get the va_list type |
| 9814 | QualType VaListType = Context.getBuiltinVaListType(); |
| Eli Friedman | e2cad65 | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 9815 | if (VaListType->isArrayType()) { |
| 9816 | // Deal with implicit array decay; for example, on x86-64, |
| 9817 | // va_list is an array, but it's supposed to decay to |
| 9818 | // a pointer for va_arg. |
| Eli Friedman | 121ba0c | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 9819 | VaListType = Context.getArrayDecayedType(VaListType); |
| Eli Friedman | e2cad65 | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 9820 | // Make sure the input expression also decays appropriately. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9821 | ExprResult Result = UsualUnaryConversions(E); |
| 9822 | if (Result.isInvalid()) |
| 9823 | return ExprError(); |
| 9824 | E = Result.take(); |
| Eli Friedman | e2cad65 | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 9825 | } else { |
| 9826 | // Otherwise, the va_list argument must be an l-value because |
| 9827 | // it is modified by va_arg. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9828 | if (!E->isTypeDependent() && |
| Douglas Gregor | ad3150c | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 9829 | CheckForModifiableLvalue(E, BuiltinLoc, *this)) |
| Eli Friedman | e2cad65 | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 9830 | return ExprError(); |
| 9831 | } |
| Eli Friedman | 121ba0c | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 9832 | |
| Douglas Gregor | ad3150c | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 9833 | if (!E->isTypeDependent() && |
| 9834 | !Context.hasSameType(VaListType, E->getType())) { |
| Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9835 | return ExprError(Diag(E->getLocStart(), |
| 9836 | diag::err_first_argument_to_va_arg_not_of_type_va_list) |
| Chris Lattner | 56382aa | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 9837 | << OrigExpr->getType() << E->getSourceRange()); |
| Chris Lattner | 3f5cd77 | 2009-04-05 00:59:53 +0000 | [diff] [blame] | 9838 | } |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9839 | |
| David Majnemer | c75d1a1 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 9840 | if (!TInfo->getType()->isDependentType()) { |
| 9841 | if (RequireCompleteType(TInfo->getTypeLoc().getBeginLoc(), TInfo->getType(), |
| 9842 | PDiag(diag::err_second_parameter_to_va_arg_incomplete) |
| 9843 | << TInfo->getTypeLoc().getSourceRange())) |
| 9844 | return ExprError(); |
| David Majnemer | 254a5c0 | 2011-06-13 06:37:03 +0000 | [diff] [blame] | 9845 | |
| David Majnemer | c75d1a1 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 9846 | if (RequireNonAbstractType(TInfo->getTypeLoc().getBeginLoc(), |
| 9847 | TInfo->getType(), |
| 9848 | PDiag(diag::err_second_parameter_to_va_arg_abstract) |
| 9849 | << TInfo->getTypeLoc().getSourceRange())) |
| 9850 | return ExprError(); |
| 9851 | |
| 9852 | if (!TInfo->getType()->isPODType()) |
| 9853 | Diag(TInfo->getTypeLoc().getBeginLoc(), |
| 9854 | diag::warn_second_parameter_to_va_arg_not_pod) |
| 9855 | << TInfo->getType() |
| 9856 | << TInfo->getTypeLoc().getSourceRange(); |
| 9857 | } |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9858 | |
| Abramo Bagnara | 27db239 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 9859 | QualType T = TInfo->getType().getNonLValueExprType(Context); |
| 9860 | return Owned(new (Context) VAArgExpr(BuiltinLoc, E, TInfo, RPLoc, T)); |
| Anders Carlsson | 7e13ab8 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 9861 | } |
| 9862 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9863 | ExprResult Sema::ActOnGNUNullExpr(SourceLocation TokenLoc) { |
| Douglas Gregor | 3be4b12 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 9864 | // The type of __null will be int or long, depending on the size of |
| 9865 | // pointers on the target. |
| 9866 | QualType Ty; |
| NAKAMURA Takumi | 0d13fd3 | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 9867 | unsigned pw = Context.Target.getPointerWidth(0); |
| 9868 | if (pw == Context.Target.getIntWidth()) |
| Douglas Gregor | 3be4b12 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 9869 | Ty = Context.IntTy; |
| NAKAMURA Takumi | 0d13fd3 | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 9870 | else if (pw == Context.Target.getLongWidth()) |
| Douglas Gregor | 3be4b12 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 9871 | Ty = Context.LongTy; |
| NAKAMURA Takumi | 0d13fd3 | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 9872 | else if (pw == Context.Target.getLongLongWidth()) |
| 9873 | Ty = Context.LongLongTy; |
| 9874 | else { |
| 9875 | assert(!"I don't know size of pointer!"); |
| 9876 | Ty = Context.IntTy; |
| 9877 | } |
| Douglas Gregor | 3be4b12 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 9878 | |
| Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9879 | return Owned(new (Context) GNUNullExpr(Ty, TokenLoc)); |
| Douglas Gregor | 3be4b12 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 9880 | } |
| 9881 | |
| Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9882 | static void MakeObjCStringLiteralFixItHint(Sema& SemaRef, QualType DstType, |
| Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 9883 | Expr *SrcExpr, FixItHint &Hint) { |
| Anders Carlsson | ace5d07 | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 9884 | if (!SemaRef.getLangOptions().ObjC1) |
| 9885 | return; |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9886 | |
| Anders Carlsson | ace5d07 | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 9887 | const ObjCObjectPointerType *PT = DstType->getAs<ObjCObjectPointerType>(); |
| 9888 | if (!PT) |
| 9889 | return; |
| 9890 | |
| 9891 | // Check if the destination is of type 'id'. |
| 9892 | if (!PT->isObjCIdType()) { |
| 9893 | // Check if the destination is the 'NSString' interface. |
| 9894 | const ObjCInterfaceDecl *ID = PT->getInterfaceDecl(); |
| 9895 | if (!ID || !ID->getIdentifier()->isStr("NSString")) |
| 9896 | return; |
| 9897 | } |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9898 | |
| Anders Carlsson | ace5d07 | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 9899 | // Strip off any parens and casts. |
| 9900 | StringLiteral *SL = dyn_cast<StringLiteral>(SrcExpr->IgnoreParenCasts()); |
| 9901 | if (!SL || SL->isWide()) |
| 9902 | return; |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9903 | |
| Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 9904 | Hint = FixItHint::CreateInsertion(SL->getLocStart(), "@"); |
| Anders Carlsson | ace5d07 | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 9905 | } |
| 9906 | |
| Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9907 | bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, |
| 9908 | SourceLocation Loc, |
| 9909 | QualType DstType, QualType SrcType, |
| Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 9910 | Expr *SrcExpr, AssignmentAction Action, |
| 9911 | bool *Complained) { |
| 9912 | if (Complained) |
| 9913 | *Complained = false; |
| 9914 | |
| Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9915 | // Decode the result (notice that AST's are still created for extensions). |
| Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 9916 | bool CheckInferredResultType = false; |
| Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9917 | bool isInvalid = false; |
| 9918 | unsigned DiagKind; |
| Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 9919 | FixItHint Hint; |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9920 | |
| Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9921 | switch (ConvTy) { |
| 9922 | default: assert(0 && "Unknown conversion type"); |
| 9923 | case Compatible: return false; |
| Chris Lattner | 940cfeb | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 9924 | case PointerToInt: |
| Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9925 | DiagKind = diag::ext_typecheck_convert_pointer_int; |
| 9926 | break; |
| Chris Lattner | 940cfeb | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 9927 | case IntToPointer: |
| 9928 | DiagKind = diag::ext_typecheck_convert_int_pointer; |
| 9929 | break; |
| Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9930 | case IncompatiblePointer: |
| Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 9931 | MakeObjCStringLiteralFixItHint(*this, DstType, SrcExpr, Hint); |
| Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9932 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer; |
| Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 9933 | CheckInferredResultType = DstType->isObjCObjectPointerType() && |
| 9934 | SrcType->isObjCObjectPointerType(); |
| Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9935 | break; |
| Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 9936 | case IncompatiblePointerSign: |
| 9937 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer_sign; |
| 9938 | break; |
| Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9939 | case FunctionVoidPointer: |
| 9940 | DiagKind = diag::ext_typecheck_convert_pointer_void_func; |
| 9941 | break; |
| John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 9942 | case IncompatiblePointerDiscardsQualifiers: { |
| John McCall | 71de91c | 2011-02-01 23:28:01 +0000 | [diff] [blame] | 9943 | // Perform array-to-pointer decay if necessary. |
| 9944 | if (SrcType->isArrayType()) SrcType = Context.getArrayDecayedType(SrcType); |
| 9945 | |
| John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 9946 | Qualifiers lhq = SrcType->getPointeeType().getQualifiers(); |
| 9947 | Qualifiers rhq = DstType->getPointeeType().getQualifiers(); |
| 9948 | if (lhq.getAddressSpace() != rhq.getAddressSpace()) { |
| 9949 | DiagKind = diag::err_typecheck_incompatible_address_space; |
| 9950 | break; |
| 9951 | } |
| 9952 | |
| 9953 | llvm_unreachable("unknown error case for discarding qualifiers!"); |
| 9954 | // fallthrough |
| 9955 | } |
| Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9956 | case CompatiblePointerDiscardsQualifiers: |
| Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 9957 | // If the qualifiers lost were because we were applying the |
| 9958 | // (deprecated) C++ conversion from a string literal to a char* |
| 9959 | // (or wchar_t*), then there was no error (C++ 4.2p2). FIXME: |
| 9960 | // Ideally, this check would be performed in |
| John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 9961 | // checkPointerTypesForAssignment. However, that would require a |
| Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 9962 | // bit of refactoring (so that the second argument is an |
| 9963 | // expression, rather than a type), which should be done as part |
| John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 9964 | // of a larger effort to fix checkPointerTypesForAssignment for |
| Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 9965 | // C++ semantics. |
| 9966 | if (getLangOptions().CPlusPlus && |
| 9967 | IsStringLiteralToNonConstPointerConversion(SrcExpr, DstType)) |
| 9968 | return false; |
| Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9969 | DiagKind = diag::ext_typecheck_convert_discards_qualifiers; |
| 9970 | break; |
| Alexis Hunt | 6f3de50 | 2009-11-08 07:46:34 +0000 | [diff] [blame] | 9971 | case IncompatibleNestedPointerQualifiers: |
| Fariborz Jahanian | b98dade | 2009-11-09 22:16:37 +0000 | [diff] [blame] | 9972 | DiagKind = diag::ext_nested_pointer_qualifier_mismatch; |
| Fariborz Jahanian | d7aa9d8 | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 9973 | break; |
| Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 9974 | case IntToBlockPointer: |
| 9975 | DiagKind = diag::err_int_to_block_pointer; |
| 9976 | break; |
| 9977 | case IncompatibleBlockPointer: |
| Mike Stump | d79b5a8 | 2009-04-21 22:51:42 +0000 | [diff] [blame] | 9978 | DiagKind = diag::err_typecheck_convert_incompatible_block_pointer; |
| Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 9979 | break; |
| Steve Naroff | 8afa989 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 9980 | case IncompatibleObjCQualifiedId: |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9981 | // FIXME: Diagnose the problem in ObjCQualifiedIdTypesAreCompatible, since |
| Steve Naroff | 8afa989 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 9982 | // it can give a more specific diagnostic. |
| 9983 | DiagKind = diag::warn_incompatible_qualified_id; |
| 9984 | break; |
| Anders Carlsson | db5a9b6 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 9985 | case IncompatibleVectors: |
| 9986 | DiagKind = diag::warn_incompatible_vectors; |
| 9987 | break; |
| Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9988 | case Incompatible: |
| 9989 | DiagKind = diag::err_typecheck_convert_incompatible; |
| 9990 | isInvalid = true; |
| 9991 | break; |
| 9992 | } |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9993 | |
| Douglas Gregor | c68e140 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 9994 | QualType FirstType, SecondType; |
| 9995 | switch (Action) { |
| 9996 | case AA_Assigning: |
| 9997 | case AA_Initializing: |
| 9998 | // The destination type comes first. |
| 9999 | FirstType = DstType; |
| 10000 | SecondType = SrcType; |
| 10001 | break; |
| Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 10002 | |
| Douglas Gregor | c68e140 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 10003 | case AA_Returning: |
| 10004 | case AA_Passing: |
| 10005 | case AA_Converting: |
| 10006 | case AA_Sending: |
| 10007 | case AA_Casting: |
| 10008 | // The source type comes first. |
| 10009 | FirstType = SrcType; |
| 10010 | SecondType = DstType; |
| 10011 | break; |
| 10012 | } |
| Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 10013 | |
| Douglas Gregor | c68e140 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 10014 | Diag(Loc, DiagKind) << FirstType << SecondType << Action |
| Anders Carlsson | ace5d07 | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 10015 | << SrcExpr->getSourceRange() << Hint; |
| Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 10016 | if (CheckInferredResultType) |
| 10017 | EmitRelatedResultTypeNote(SrcExpr); |
| 10018 | |
| Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 10019 | if (Complained) |
| 10020 | *Complained = true; |
| Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 10021 | return isInvalid; |
| 10022 | } |
| Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 10023 | |
| Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 10024 | bool Sema::VerifyIntegerConstantExpression(const Expr *E, llvm::APSInt *Result){ |
| Eli Friedman | bb967cc | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 10025 | llvm::APSInt ICEResult; |
| 10026 | if (E->isIntegerConstantExpr(ICEResult, Context)) { |
| 10027 | if (Result) |
| 10028 | *Result = ICEResult; |
| 10029 | return false; |
| 10030 | } |
| 10031 | |
| Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 10032 | Expr::EvalResult EvalResult; |
| 10033 | |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 10034 | if (!E->Evaluate(EvalResult, Context) || !EvalResult.Val.isInt() || |
| Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 10035 | EvalResult.HasSideEffects) { |
| 10036 | Diag(E->getExprLoc(), diag::err_expr_not_ice) << E->getSourceRange(); |
| 10037 | |
| 10038 | if (EvalResult.Diag) { |
| 10039 | // We only show the note if it's not the usual "invalid subexpression" |
| 10040 | // or if it's actually in a subexpression. |
| 10041 | if (EvalResult.Diag != diag::note_invalid_subexpr_in_ice || |
| 10042 | E->IgnoreParens() != EvalResult.DiagExpr->IgnoreParens()) |
| 10043 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
| 10044 | } |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 10045 | |
| Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 10046 | return true; |
| 10047 | } |
| 10048 | |
| Eli Friedman | bb967cc | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 10049 | Diag(E->getExprLoc(), diag::ext_expr_not_ice) << |
| 10050 | E->getSourceRange(); |
| Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 10051 | |
| Eli Friedman | bb967cc | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 10052 | if (EvalResult.Diag && |
| Argyrios Kyrtzidis | 1cb0de1 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 10053 | Diags.getDiagnosticLevel(diag::ext_expr_not_ice, EvalResult.DiagLoc) |
| 10054 | != Diagnostic::Ignored) |
| Eli Friedman | bb967cc | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 10055 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
| Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 10056 | |
| Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 10057 | if (Result) |
| 10058 | *Result = EvalResult.Val.getInt(); |
| 10059 | return false; |
| 10060 | } |
| Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10061 | |
| Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 10062 | void |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10063 | Sema::PushExpressionEvaluationContext(ExpressionEvaluationContext NewContext) { |
| Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 10064 | ExprEvalContexts.push_back( |
| 10065 | ExpressionEvaluationContextRecord(NewContext, ExprTemporaries.size())); |
| Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 10066 | } |
| 10067 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10068 | void |
| Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 10069 | Sema::PopExpressionEvaluationContext() { |
| 10070 | // Pop the current expression evaluation context off the stack. |
| 10071 | ExpressionEvaluationContextRecord Rec = ExprEvalContexts.back(); |
| 10072 | ExprEvalContexts.pop_back(); |
| Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 10073 | |
| Douglas Gregor | fab31f4 | 2009-12-12 07:57:52 +0000 | [diff] [blame] | 10074 | if (Rec.Context == PotentiallyPotentiallyEvaluated) { |
| 10075 | if (Rec.PotentiallyReferenced) { |
| 10076 | // Mark any remaining declarations in the current position of the stack |
| 10077 | // as "referenced". If they were not meant to be referenced, semantic |
| 10078 | // analysis would have eliminated them (e.g., in ActOnCXXTypeId). |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10079 | for (PotentiallyReferencedDecls::iterator |
| Douglas Gregor | fab31f4 | 2009-12-12 07:57:52 +0000 | [diff] [blame] | 10080 | I = Rec.PotentiallyReferenced->begin(), |
| 10081 | IEnd = Rec.PotentiallyReferenced->end(); |
| 10082 | I != IEnd; ++I) |
| 10083 | MarkDeclarationReferenced(I->first, I->second); |
| 10084 | } |
| 10085 | |
| 10086 | if (Rec.PotentiallyDiagnosed) { |
| 10087 | // Emit any pending diagnostics. |
| 10088 | for (PotentiallyEmittedDiagnostics::iterator |
| 10089 | I = Rec.PotentiallyDiagnosed->begin(), |
| 10090 | IEnd = Rec.PotentiallyDiagnosed->end(); |
| 10091 | I != IEnd; ++I) |
| 10092 | Diag(I->first, I->second); |
| 10093 | } |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10094 | } |
| Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 10095 | |
| 10096 | // When are coming out of an unevaluated context, clear out any |
| 10097 | // temporaries that we may have created as part of the evaluation of |
| 10098 | // the expression in that context: they aren't relevant because they |
| 10099 | // will never be constructed. |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10100 | if (Rec.Context == Unevaluated && |
| Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 10101 | ExprTemporaries.size() > Rec.NumTemporaries) |
| 10102 | ExprTemporaries.erase(ExprTemporaries.begin() + Rec.NumTemporaries, |
| 10103 | ExprTemporaries.end()); |
| 10104 | |
| 10105 | // Destroy the popped expression evaluation record. |
| 10106 | Rec.Destroy(); |
| Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 10107 | } |
| Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10108 | |
| 10109 | /// \brief Note that the given declaration was referenced in the source code. |
| 10110 | /// |
| 10111 | /// This routine should be invoke whenever a given declaration is referenced |
| 10112 | /// in the source code, and where that reference occurred. If this declaration |
| 10113 | /// reference means that the the declaration is used (C++ [basic.def.odr]p2, |
| 10114 | /// C99 6.9p3), then the declaration will be marked as used. |
| 10115 | /// |
| 10116 | /// \param Loc the location where the declaration was referenced. |
| 10117 | /// |
| 10118 | /// \param D the declaration that has been referenced by the source code. |
| 10119 | void Sema::MarkDeclarationReferenced(SourceLocation Loc, Decl *D) { |
| 10120 | assert(D && "No declaration?"); |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10121 | |
| Argyrios Kyrtzidis | 1618023 | 2011-04-19 19:51:10 +0000 | [diff] [blame] | 10122 | D->setReferenced(); |
| 10123 | |
| Douglas Gregor | ebada077 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 10124 | if (D->isUsed(false)) |
| Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 10125 | return; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10126 | |
| Douglas Gregor | 3beaf9b | 2009-10-08 21:35:42 +0000 | [diff] [blame] | 10127 | // Mark a parameter or variable declaration "used", regardless of whether we're in a |
| 10128 | // template or not. The reason for this is that unevaluated expressions |
| 10129 | // (e.g. (void)sizeof()) constitute a use for warning purposes (-Wunused-variables and |
| 10130 | // -Wunused-parameters) |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10131 | if (isa<ParmVarDecl>(D) || |
| Douglas Gregor | fd27fed | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 10132 | (isa<VarDecl>(D) && D->getDeclContext()->isFunctionOrMethod())) { |
| Anders Carlsson | 73067a0 | 2010-10-22 23:37:08 +0000 | [diff] [blame] | 10133 | D->setUsed(); |
| Douglas Gregor | fd27fed | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 10134 | return; |
| 10135 | } |
| Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 10136 | |
| Douglas Gregor | fd27fed | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 10137 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) |
| 10138 | return; |
| Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 10139 | |
| Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10140 | // Do not mark anything as "used" within a dependent context; wait for |
| 10141 | // an instantiation. |
| 10142 | if (CurContext->isDependentContext()) |
| 10143 | return; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10144 | |
| Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 10145 | switch (ExprEvalContexts.back().Context) { |
| Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 10146 | case Unevaluated: |
| 10147 | // We are in an expression that is not potentially evaluated; do nothing. |
| 10148 | return; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10149 | |
| Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 10150 | case PotentiallyEvaluated: |
| 10151 | // We are in a potentially-evaluated expression, so this declaration is |
| 10152 | // "used"; handle this below. |
| 10153 | break; |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10154 | |
| Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 10155 | case PotentiallyPotentiallyEvaluated: |
| 10156 | // We are in an expression that may be potentially evaluated; queue this |
| 10157 | // declaration reference until we know whether the expression is |
| 10158 | // potentially evaluated. |
| Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 10159 | ExprEvalContexts.back().addReferencedDecl(Loc, D); |
| Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 10160 | return; |
| Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10161 | |
| 10162 | case PotentiallyEvaluatedIfUsed: |
| 10163 | // Referenced declarations will only be used if the construct in the |
| 10164 | // containing expression is used. |
| 10165 | return; |
| Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 10166 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10167 | |
| Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10168 | // Note that this declaration has been used. |
| Fariborz Jahanian | 3a36343 | 2009-06-22 17:30:33 +0000 | [diff] [blame] | 10169 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
| Alexis Hunt | f92197c | 2011-05-12 03:51:51 +0000 | [diff] [blame] | 10170 | if (Constructor->isDefaulted() && Constructor->isDefaultConstructor()) { |
| 10171 | if (Constructor->isTrivial()) |
| Chandler Carruth | c926240 | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 10172 | return; |
| 10173 | if (!Constructor->isUsed(false)) |
| 10174 | DefineImplicitDefaultConstructor(Loc, Constructor); |
| Alexis Hunt | 22b5b13 | 2011-05-14 18:20:50 +0000 | [diff] [blame] | 10175 | } else if (Constructor->isDefaulted() && |
| Alexis Hunt | 913820d | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 10176 | Constructor->isCopyConstructor()) { |
| Douglas Gregor | ebada077 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 10177 | if (!Constructor->isUsed(false)) |
| Alexis Hunt | 913820d | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 10178 | DefineImplicitCopyConstructor(Loc, Constructor); |
| Fariborz Jahanian | 477d242 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 10179 | } |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10180 | |
| Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10181 | MarkVTableUsed(Loc, Constructor->getParent()); |
| Fariborz Jahanian | 24a175b | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 10182 | } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) { |
| Alexis Hunt | f9172946 | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 10183 | if (Destructor->isDefaulted() && !Destructor->isUsed(false)) |
| Fariborz Jahanian | 24a175b | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 10184 | DefineImplicitDestructor(Loc, Destructor); |
| Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10185 | if (Destructor->isVirtual()) |
| 10186 | MarkVTableUsed(Loc, Destructor->getParent()); |
| Fariborz Jahanian | 41f7927 | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 10187 | } else if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) { |
| Alexis Hunt | c9a5573 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 10188 | if (MethodDecl->isDefaulted() && MethodDecl->isOverloadedOperator() && |
| Fariborz Jahanian | 41f7927 | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 10189 | MethodDecl->getOverloadedOperator() == OO_Equal) { |
| Douglas Gregor | ebada077 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 10190 | if (!MethodDecl->isUsed(false)) |
| Douglas Gregor | a57478e | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 10191 | DefineImplicitCopyAssignment(Loc, MethodDecl); |
| Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10192 | } else if (MethodDecl->isVirtual()) |
| 10193 | MarkVTableUsed(Loc, MethodDecl->getParent()); |
| Fariborz Jahanian | 41f7927 | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 10194 | } |
| Fariborz Jahanian | 49796cc7 | 2009-06-24 22:09:44 +0000 | [diff] [blame] | 10195 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
| John McCall | 8377967 | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 10196 | // Recursive functions should be marked when used from another function. |
| 10197 | if (CurContext == Function) return; |
| 10198 | |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10199 | // Implicit instantiation of function templates and member functions of |
| Douglas Gregor | 4adbc6d | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 10200 | // class templates. |
| Douglas Gregor | 69f6a36 | 2010-05-17 17:34:56 +0000 | [diff] [blame] | 10201 | if (Function->isImplicitlyInstantiable()) { |
| Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 10202 | bool AlreadyInstantiated = false; |
| 10203 | if (FunctionTemplateSpecializationInfo *SpecInfo |
| 10204 | = Function->getTemplateSpecializationInfo()) { |
| 10205 | if (SpecInfo->getPointOfInstantiation().isInvalid()) |
| 10206 | SpecInfo->setPointOfInstantiation(Loc); |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10207 | else if (SpecInfo->getTemplateSpecializationKind() |
| Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 10208 | == TSK_ImplicitInstantiation) |
| Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 10209 | AlreadyInstantiated = true; |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10210 | } else if (MemberSpecializationInfo *MSInfo |
| Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 10211 | = Function->getMemberSpecializationInfo()) { |
| 10212 | if (MSInfo->getPointOfInstantiation().isInvalid()) |
| 10213 | MSInfo->setPointOfInstantiation(Loc); |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10214 | else if (MSInfo->getTemplateSpecializationKind() |
| Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 10215 | == TSK_ImplicitInstantiation) |
| Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 10216 | AlreadyInstantiated = true; |
| 10217 | } |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10218 | |
| Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 10219 | if (!AlreadyInstantiated) { |
| 10220 | if (isa<CXXRecordDecl>(Function->getDeclContext()) && |
| 10221 | cast<CXXRecordDecl>(Function->getDeclContext())->isLocalClass()) |
| 10222 | PendingLocalImplicitInstantiations.push_back(std::make_pair(Function, |
| 10223 | Loc)); |
| 10224 | else |
| Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 10225 | PendingInstantiations.push_back(std::make_pair(Function, Loc)); |
| Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 10226 | } |
| John McCall | 8377967 | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 10227 | } else { |
| 10228 | // Walk redefinitions, as some of them may be instantiable. |
| Gabor Greif | b6aba3e | 2010-08-28 00:16:06 +0000 | [diff] [blame] | 10229 | for (FunctionDecl::redecl_iterator i(Function->redecls_begin()), |
| 10230 | e(Function->redecls_end()); i != e; ++i) { |
| Gabor Greif | 34ecff2 | 2010-08-28 01:58:12 +0000 | [diff] [blame] | 10231 | if (!i->isUsed(false) && i->isImplicitlyInstantiable()) |
| Gabor Greif | b6aba3e | 2010-08-28 00:16:06 +0000 | [diff] [blame] | 10232 | MarkDeclarationReferenced(Loc, *i); |
| 10233 | } |
| John McCall | 8377967 | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 10234 | } |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10235 | |
| John McCall | 8377967 | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 10236 | // Keep track of used but undefined functions. |
| 10237 | if (!Function->isPure() && !Function->hasBody() && |
| 10238 | Function->getLinkage() != ExternalLinkage) { |
| 10239 | SourceLocation &old = UndefinedInternals[Function->getCanonicalDecl()]; |
| 10240 | if (old.isInvalid()) old = Loc; |
| 10241 | } |
| Argyrios Kyrtzidis | dfffabd | 2010-08-25 10:34:54 +0000 | [diff] [blame] | 10242 | |
| John McCall | 8377967 | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 10243 | Function->setUsed(true); |
| Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10244 | return; |
| Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 10245 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10246 | |
| Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10247 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) { |
| Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 10248 | // Implicit instantiation of static data members of class templates. |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10249 | if (Var->isStaticDataMember() && |
| Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 10250 | Var->getInstantiatedFromStaticDataMember()) { |
| 10251 | MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo(); |
| 10252 | assert(MSInfo && "Missing member specialization information?"); |
| 10253 | if (MSInfo->getPointOfInstantiation().isInvalid() && |
| 10254 | MSInfo->getTemplateSpecializationKind()== TSK_ImplicitInstantiation) { |
| 10255 | MSInfo->setPointOfInstantiation(Loc); |
| Sebastian Redl | 2ac2c72 | 2011-04-29 08:19:30 +0000 | [diff] [blame] | 10256 | // This is a modification of an existing AST node. Notify listeners. |
| 10257 | if (ASTMutationListener *L = getASTMutationListener()) |
| 10258 | L->StaticDataMemberInstantiated(Var); |
| Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 10259 | PendingInstantiations.push_back(std::make_pair(Var, Loc)); |
| Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 10260 | } |
| 10261 | } |
| Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10262 | |
| John McCall | 15dd404 | 2011-02-21 19:25:48 +0000 | [diff] [blame] | 10263 | // Keep track of used but undefined variables. We make a hole in |
| 10264 | // the warning for static const data members with in-line |
| 10265 | // initializers. |
| John McCall | 8377967 | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 10266 | if (Var->hasDefinition() == VarDecl::DeclarationOnly |
| John McCall | 15dd404 | 2011-02-21 19:25:48 +0000 | [diff] [blame] | 10267 | && Var->getLinkage() != ExternalLinkage |
| 10268 | && !(Var->isStaticDataMember() && Var->hasInit())) { |
| John McCall | 8377967 | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 10269 | SourceLocation &old = UndefinedInternals[Var->getCanonicalDecl()]; |
| 10270 | if (old.isInvalid()) old = Loc; |
| 10271 | } |
| Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 10272 | |
| Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10273 | D->setUsed(true); |
| Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 10274 | return; |
| Sam Weinig | bae6914 | 2009-09-11 03:29:30 +0000 | [diff] [blame] | 10275 | } |
| Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10276 | } |
| Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 10277 | |
| Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10278 | namespace { |
| Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10279 | // Mark all of the declarations referenced |
| Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10280 | // FIXME: Not fully implemented yet! We need to have a better understanding |
| Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10281 | // of when we're entering |
| Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10282 | class MarkReferencedDecls : public RecursiveASTVisitor<MarkReferencedDecls> { |
| 10283 | Sema &S; |
| 10284 | SourceLocation Loc; |
| Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10285 | |
| Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10286 | public: |
| 10287 | typedef RecursiveASTVisitor<MarkReferencedDecls> Inherited; |
| Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10288 | |
| Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10289 | MarkReferencedDecls(Sema &S, SourceLocation Loc) : S(S), Loc(Loc) { } |
| Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10290 | |
| 10291 | bool TraverseTemplateArgument(const TemplateArgument &Arg); |
| 10292 | bool TraverseRecordType(RecordType *T); |
| Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10293 | }; |
| 10294 | } |
| 10295 | |
| Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10296 | bool MarkReferencedDecls::TraverseTemplateArgument( |
| 10297 | const TemplateArgument &Arg) { |
| Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10298 | if (Arg.getKind() == TemplateArgument::Declaration) { |
| 10299 | S.MarkDeclarationReferenced(Loc, Arg.getAsDecl()); |
| 10300 | } |
| Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10301 | |
| 10302 | return Inherited::TraverseTemplateArgument(Arg); |
| Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10303 | } |
| 10304 | |
| Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10305 | bool MarkReferencedDecls::TraverseRecordType(RecordType *T) { |
| Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10306 | if (ClassTemplateSpecializationDecl *Spec |
| 10307 | = dyn_cast<ClassTemplateSpecializationDecl>(T->getDecl())) { |
| 10308 | const TemplateArgumentList &Args = Spec->getTemplateArgs(); |
| Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 10309 | return TraverseTemplateArguments(Args.data(), Args.size()); |
| Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10310 | } |
| 10311 | |
| Chandler Carruth | c65667c | 2010-06-10 10:31:57 +0000 | [diff] [blame] | 10312 | return true; |
| Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10313 | } |
| 10314 | |
| 10315 | void Sema::MarkDeclarationsReferencedInType(SourceLocation Loc, QualType T) { |
| 10316 | MarkReferencedDecls Marker(*this, Loc); |
| Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10317 | Marker.TraverseType(Context.getCanonicalType(T)); |
| Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10318 | } |
| 10319 | |
| Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10320 | namespace { |
| 10321 | /// \brief Helper class that marks all of the declarations referenced by |
| 10322 | /// potentially-evaluated subexpressions as "referenced". |
| 10323 | class EvaluatedExprMarker : public EvaluatedExprVisitor<EvaluatedExprMarker> { |
| 10324 | Sema &S; |
| 10325 | |
| 10326 | public: |
| 10327 | typedef EvaluatedExprVisitor<EvaluatedExprMarker> Inherited; |
| 10328 | |
| 10329 | explicit EvaluatedExprMarker(Sema &S) : Inherited(S.Context), S(S) { } |
| 10330 | |
| 10331 | void VisitDeclRefExpr(DeclRefExpr *E) { |
| 10332 | S.MarkDeclarationReferenced(E->getLocation(), E->getDecl()); |
| 10333 | } |
| 10334 | |
| 10335 | void VisitMemberExpr(MemberExpr *E) { |
| 10336 | S.MarkDeclarationReferenced(E->getMemberLoc(), E->getMemberDecl()); |
| Douglas Gregor | 32b3de5 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 10337 | Inherited::VisitMemberExpr(E); |
| Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10338 | } |
| 10339 | |
| 10340 | void VisitCXXNewExpr(CXXNewExpr *E) { |
| 10341 | if (E->getConstructor()) |
| 10342 | S.MarkDeclarationReferenced(E->getLocStart(), E->getConstructor()); |
| 10343 | if (E->getOperatorNew()) |
| 10344 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorNew()); |
| 10345 | if (E->getOperatorDelete()) |
| 10346 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorDelete()); |
| Douglas Gregor | 32b3de5 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 10347 | Inherited::VisitCXXNewExpr(E); |
| Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10348 | } |
| 10349 | |
| 10350 | void VisitCXXDeleteExpr(CXXDeleteExpr *E) { |
| 10351 | if (E->getOperatorDelete()) |
| 10352 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorDelete()); |
| Douglas Gregor | 6ed2fee | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 10353 | QualType Destroyed = S.Context.getBaseElementType(E->getDestroyedType()); |
| 10354 | if (const RecordType *DestroyedRec = Destroyed->getAs<RecordType>()) { |
| 10355 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DestroyedRec->getDecl()); |
| 10356 | S.MarkDeclarationReferenced(E->getLocStart(), |
| 10357 | S.LookupDestructor(Record)); |
| 10358 | } |
| 10359 | |
| Douglas Gregor | 32b3de5 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 10360 | Inherited::VisitCXXDeleteExpr(E); |
| Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10361 | } |
| 10362 | |
| 10363 | void VisitCXXConstructExpr(CXXConstructExpr *E) { |
| 10364 | S.MarkDeclarationReferenced(E->getLocStart(), E->getConstructor()); |
| Douglas Gregor | 32b3de5 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 10365 | Inherited::VisitCXXConstructExpr(E); |
| Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10366 | } |
| 10367 | |
| 10368 | void VisitBlockDeclRefExpr(BlockDeclRefExpr *E) { |
| 10369 | S.MarkDeclarationReferenced(E->getLocation(), E->getDecl()); |
| 10370 | } |
| Douglas Gregor | f0873f4 | 2010-10-19 17:17:35 +0000 | [diff] [blame] | 10371 | |
| 10372 | void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { |
| 10373 | Visit(E->getExpr()); |
| 10374 | } |
| Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10375 | }; |
| 10376 | } |
| 10377 | |
| 10378 | /// \brief Mark any declarations that appear within this expression or any |
| 10379 | /// potentially-evaluated subexpressions as "referenced". |
| 10380 | void Sema::MarkDeclarationsReferencedInExpr(Expr *E) { |
| 10381 | EvaluatedExprMarker(*this).Visit(E); |
| 10382 | } |
| 10383 | |
| Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10384 | /// \brief Emit a diagnostic that describes an effect on the run-time behavior |
| 10385 | /// of the program being compiled. |
| 10386 | /// |
| 10387 | /// This routine emits the given diagnostic when the code currently being |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10388 | /// type-checked is "potentially evaluated", meaning that there is a |
| Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10389 | /// possibility that the code will actually be executable. Code in sizeof() |
| 10390 | /// expressions, code used only during overload resolution, etc., are not |
| 10391 | /// potentially evaluated. This routine will suppress such diagnostics or, |
| 10392 | /// in the absolutely nutty case of potentially potentially evaluated |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10393 | /// expressions (C++ typeid), queue the diagnostic to potentially emit it |
| Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10394 | /// later. |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10395 | /// |
| Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10396 | /// This routine should be used for all diagnostics that describe the run-time |
| 10397 | /// behavior of a program, such as passing a non-POD value through an ellipsis. |
| 10398 | /// Failure to do so will likely result in spurious diagnostics or failures |
| 10399 | /// during overload resolution or within sizeof/alignof/typeof/typeid. |
| Ted Kremenek | 55ae319 | 2011-02-23 01:51:43 +0000 | [diff] [blame] | 10400 | bool Sema::DiagRuntimeBehavior(SourceLocation Loc, const Stmt *stmt, |
| Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10401 | const PartialDiagnostic &PD) { |
| 10402 | switch (ExprEvalContexts.back().Context ) { |
| 10403 | case Unevaluated: |
| 10404 | // The argument will never be evaluated, so don't complain. |
| 10405 | break; |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10406 | |
| Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10407 | case PotentiallyEvaluated: |
| Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10408 | case PotentiallyEvaluatedIfUsed: |
| Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 10409 | if (stmt && getCurFunctionOrMethodDecl()) { |
| 10410 | FunctionScopes.back()->PossiblyUnreachableDiags. |
| 10411 | push_back(sema::PossiblyUnreachableDiag(PD, Loc, stmt)); |
| 10412 | } |
| 10413 | else |
| 10414 | Diag(Loc, PD); |
| 10415 | |
| Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10416 | return true; |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10417 | |
| Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10418 | case PotentiallyPotentiallyEvaluated: |
| 10419 | ExprEvalContexts.back().addDiagnostic(Loc, PD); |
| 10420 | break; |
| 10421 | } |
| 10422 | |
| 10423 | return false; |
| 10424 | } |
| 10425 | |
| Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 10426 | bool Sema::CheckCallReturnType(QualType ReturnType, SourceLocation Loc, |
| 10427 | CallExpr *CE, FunctionDecl *FD) { |
| 10428 | if (ReturnType->isVoidType() || !ReturnType->isIncompleteType()) |
| 10429 | return false; |
| 10430 | |
| 10431 | PartialDiagnostic Note = |
| 10432 | FD ? PDiag(diag::note_function_with_incomplete_return_type_declared_here) |
| 10433 | << FD->getDeclName() : PDiag(); |
| 10434 | SourceLocation NoteLoc = FD ? FD->getLocation() : SourceLocation(); |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10435 | |
| Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 10436 | if (RequireCompleteType(Loc, ReturnType, |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10437 | FD ? |
| Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 10438 | PDiag(diag::err_call_function_incomplete_return) |
| 10439 | << CE->getSourceRange() << FD->getDeclName() : |
| Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10440 | PDiag(diag::err_call_incomplete_return) |
| Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 10441 | << CE->getSourceRange(), |
| 10442 | std::make_pair(NoteLoc, Note))) |
| 10443 | return true; |
| 10444 | |
| 10445 | return false; |
| 10446 | } |
| 10447 | |
| Douglas Gregor | 2d4f64f | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10448 | // Diagnose the s/=/==/ and s/\|=/!=/ typos. Note that adding parentheses |
| John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10449 | // will prevent this condition from triggering, which is what we want. |
| 10450 | void Sema::DiagnoseAssignmentAsCondition(Expr *E) { |
| 10451 | SourceLocation Loc; |
| 10452 | |
| John McCall | 0506e4a | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 10453 | unsigned diagnostic = diag::warn_condition_is_assignment; |
| Douglas Gregor | 2d4f64f | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10454 | bool IsOrAssign = false; |
| John McCall | 0506e4a | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 10455 | |
| John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10456 | if (isa<BinaryOperator>(E)) { |
| 10457 | BinaryOperator *Op = cast<BinaryOperator>(E); |
| Douglas Gregor | 2d4f64f | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10458 | if (Op->getOpcode() != BO_Assign && Op->getOpcode() != BO_OrAssign) |
| John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10459 | return; |
| 10460 | |
| Douglas Gregor | 2d4f64f | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10461 | IsOrAssign = Op->getOpcode() == BO_OrAssign; |
| 10462 | |
| John McCall | b0e419e | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 10463 | // Greylist some idioms by putting them into a warning subcategory. |
| 10464 | if (ObjCMessageExpr *ME |
| 10465 | = dyn_cast<ObjCMessageExpr>(Op->getRHS()->IgnoreParenCasts())) { |
| 10466 | Selector Sel = ME->getSelector(); |
| 10467 | |
| John McCall | b0e419e | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 10468 | // self = [<foo> init...] |
| Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 10469 | if (isSelfExpr(Op->getLHS()) && Sel.getNameForSlot(0).startswith("init")) |
| John McCall | b0e419e | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 10470 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 10471 | |
| 10472 | // <foo> = [<bar> nextObject] |
| Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 10473 | else if (Sel.isUnarySelector() && Sel.getNameForSlot(0) == "nextObject") |
| John McCall | b0e419e | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 10474 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 10475 | } |
| John McCall | 0506e4a | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 10476 | |
| John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10477 | Loc = Op->getOperatorLoc(); |
| 10478 | } else if (isa<CXXOperatorCallExpr>(E)) { |
| 10479 | CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(E); |
| Douglas Gregor | 2d4f64f | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10480 | if (Op->getOperator() != OO_Equal && Op->getOperator() != OO_PipeEqual) |
| John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10481 | return; |
| 10482 | |
| Douglas Gregor | 2d4f64f | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10483 | IsOrAssign = Op->getOperator() == OO_PipeEqual; |
| John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10484 | Loc = Op->getOperatorLoc(); |
| 10485 | } else { |
| 10486 | // Not an assignment. |
| 10487 | return; |
| 10488 | } |
| 10489 | |
| Douglas Gregor | 2bf2d3d | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 10490 | Diag(Loc, diagnostic) << E->getSourceRange(); |
| Douglas Gregor | 2d4f64f | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10491 | |
| Argyrios Kyrtzidis | e1b97c4 | 2011-04-25 23:01:29 +0000 | [diff] [blame] | 10492 | SourceLocation Open = E->getSourceRange().getBegin(); |
| 10493 | SourceLocation Close = PP.getLocForEndOfToken(E->getSourceRange().getEnd()); |
| 10494 | Diag(Loc, diag::note_condition_assign_silence) |
| 10495 | << FixItHint::CreateInsertion(Open, "(") |
| 10496 | << FixItHint::CreateInsertion(Close, ")"); |
| 10497 | |
| Douglas Gregor | 2d4f64f | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10498 | if (IsOrAssign) |
| 10499 | Diag(Loc, diag::note_condition_or_assign_to_comparison) |
| 10500 | << FixItHint::CreateReplacement(Loc, "!="); |
| 10501 | else |
| 10502 | Diag(Loc, diag::note_condition_assign_to_comparison) |
| 10503 | << FixItHint::CreateReplacement(Loc, "=="); |
| John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10504 | } |
| 10505 | |
| Argyrios Kyrtzidis | 8b6ec68 | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 10506 | /// \brief Redundant parentheses over an equality comparison can indicate |
| 10507 | /// that the user intended an assignment used as condition. |
| 10508 | void Sema::DiagnoseEqualityWithExtraParens(ParenExpr *parenE) { |
| Argyrios Kyrtzidis | f4f8278 | 2011-02-01 22:23:56 +0000 | [diff] [blame] | 10509 | // Don't warn if the parens came from a macro. |
| 10510 | SourceLocation parenLoc = parenE->getLocStart(); |
| 10511 | if (parenLoc.isInvalid() || parenLoc.isMacroID()) |
| 10512 | return; |
| Argyrios Kyrtzidis | ba699d6 | 2011-03-28 23:52:04 +0000 | [diff] [blame] | 10513 | // Don't warn for dependent expressions. |
| 10514 | if (parenE->isTypeDependent()) |
| 10515 | return; |
| Argyrios Kyrtzidis | f4f8278 | 2011-02-01 22:23:56 +0000 | [diff] [blame] | 10516 | |
| Argyrios Kyrtzidis | 8b6ec68 | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 10517 | Expr *E = parenE->IgnoreParens(); |
| 10518 | |
| 10519 | if (BinaryOperator *opE = dyn_cast<BinaryOperator>(E)) |
| Argyrios Kyrtzidis | 582dd68 | 2011-02-01 19:32:59 +0000 | [diff] [blame] | 10520 | if (opE->getOpcode() == BO_EQ && |
| 10521 | opE->getLHS()->IgnoreParenImpCasts()->isModifiableLvalue(Context) |
| 10522 | == Expr::MLV_Valid) { |
| Argyrios Kyrtzidis | 8b6ec68 | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 10523 | SourceLocation Loc = opE->getOperatorLoc(); |
| Ted Kremenek | c358d9f | 2011-02-01 22:36:09 +0000 | [diff] [blame] | 10524 | |
| Ted Kremenek | ae02209 | 2011-02-02 02:20:30 +0000 | [diff] [blame] | 10525 | Diag(Loc, diag::warn_equality_with_extra_parens) << E->getSourceRange(); |
| Ted Kremenek | ae02209 | 2011-02-02 02:20:30 +0000 | [diff] [blame] | 10526 | Diag(Loc, diag::note_equality_comparison_silence) |
| 10527 | << FixItHint::CreateRemoval(parenE->getSourceRange().getBegin()) |
| 10528 | << FixItHint::CreateRemoval(parenE->getSourceRange().getEnd()); |
| Argyrios Kyrtzidis | e1b97c4 | 2011-04-25 23:01:29 +0000 | [diff] [blame] | 10529 | Diag(Loc, diag::note_equality_comparison_to_assign) |
| 10530 | << FixItHint::CreateReplacement(Loc, "="); |
| Argyrios Kyrtzidis | 8b6ec68 | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 10531 | } |
| 10532 | } |
| 10533 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10534 | ExprResult Sema::CheckBooleanCondition(Expr *E, SourceLocation Loc) { |
| John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10535 | DiagnoseAssignmentAsCondition(E); |
| Argyrios Kyrtzidis | 8b6ec68 | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 10536 | if (ParenExpr *parenE = dyn_cast<ParenExpr>(E)) |
| 10537 | DiagnoseEqualityWithExtraParens(parenE); |
| John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10538 | |
| John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10539 | ExprResult result = CheckPlaceholderExpr(E); |
| 10540 | if (result.isInvalid()) return ExprError(); |
| 10541 | E = result.take(); |
| Argyrios Kyrtzidis | ca76629 | 2010-11-01 18:49:26 +0000 | [diff] [blame] | 10542 | |
| John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10543 | if (!E->isTypeDependent()) { |
| John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 10544 | if (getLangOptions().CPlusPlus) |
| 10545 | return CheckCXXBooleanCondition(E); // C++ 6.4p4 |
| 10546 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10547 | ExprResult ERes = DefaultFunctionArrayLvalueConversion(E); |
| 10548 | if (ERes.isInvalid()) |
| 10549 | return ExprError(); |
| 10550 | E = ERes.take(); |
| John McCall | 29cb2fd | 2010-12-04 06:09:13 +0000 | [diff] [blame] | 10551 | |
| 10552 | QualType T = E->getType(); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10553 | if (!T->isScalarType()) { // C99 6.8.4.1p1 |
| 10554 | Diag(Loc, diag::err_typecheck_statement_requires_scalar) |
| 10555 | << T << E->getSourceRange(); |
| 10556 | return ExprError(); |
| 10557 | } |
| John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10558 | } |
| 10559 | |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10560 | return Owned(E); |
| John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10561 | } |
| Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 10562 | |
| John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10563 | ExprResult Sema::ActOnBooleanCondition(Scope *S, SourceLocation Loc, |
| 10564 | Expr *Sub) { |
| Douglas Gregor | 12cc7ee | 2010-05-06 21:39:56 +0000 | [diff] [blame] | 10565 | if (!Sub) |
| Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 10566 | return ExprError(); |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10567 | |
| 10568 | return CheckBooleanCondition(Sub, Loc); |
| Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 10569 | } |
| John McCall | 36e7fe3 | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 10570 | |
| John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10571 | namespace { |
| John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10572 | /// A visitor for rebuilding a call to an __unknown_any expression |
| 10573 | /// to have an appropriate type. |
| 10574 | struct RebuildUnknownAnyFunction |
| 10575 | : StmtVisitor<RebuildUnknownAnyFunction, ExprResult> { |
| 10576 | |
| 10577 | Sema &S; |
| 10578 | |
| 10579 | RebuildUnknownAnyFunction(Sema &S) : S(S) {} |
| 10580 | |
| 10581 | ExprResult VisitStmt(Stmt *S) { |
| 10582 | llvm_unreachable("unexpected statement!"); |
| 10583 | return ExprError(); |
| 10584 | } |
| 10585 | |
| 10586 | ExprResult VisitExpr(Expr *expr) { |
| 10587 | S.Diag(expr->getExprLoc(), diag::err_unsupported_unknown_any_call) |
| 10588 | << expr->getSourceRange(); |
| 10589 | return ExprError(); |
| 10590 | } |
| 10591 | |
| 10592 | /// Rebuild an expression which simply semantically wraps another |
| 10593 | /// expression which it shares the type and value kind of. |
| 10594 | template <class T> ExprResult rebuildSugarExpr(T *expr) { |
| 10595 | ExprResult subResult = Visit(expr->getSubExpr()); |
| 10596 | if (subResult.isInvalid()) return ExprError(); |
| 10597 | |
| 10598 | Expr *subExpr = subResult.take(); |
| 10599 | expr->setSubExpr(subExpr); |
| 10600 | expr->setType(subExpr->getType()); |
| 10601 | expr->setValueKind(subExpr->getValueKind()); |
| 10602 | assert(expr->getObjectKind() == OK_Ordinary); |
| 10603 | return expr; |
| 10604 | } |
| 10605 | |
| 10606 | ExprResult VisitParenExpr(ParenExpr *paren) { |
| 10607 | return rebuildSugarExpr(paren); |
| 10608 | } |
| 10609 | |
| 10610 | ExprResult VisitUnaryExtension(UnaryOperator *op) { |
| 10611 | return rebuildSugarExpr(op); |
| 10612 | } |
| 10613 | |
| 10614 | ExprResult VisitUnaryAddrOf(UnaryOperator *op) { |
| 10615 | ExprResult subResult = Visit(op->getSubExpr()); |
| 10616 | if (subResult.isInvalid()) return ExprError(); |
| 10617 | |
| 10618 | Expr *subExpr = subResult.take(); |
| 10619 | op->setSubExpr(subExpr); |
| 10620 | op->setType(S.Context.getPointerType(subExpr->getType())); |
| 10621 | assert(op->getValueKind() == VK_RValue); |
| 10622 | assert(op->getObjectKind() == OK_Ordinary); |
| 10623 | return op; |
| 10624 | } |
| 10625 | |
| 10626 | ExprResult resolveDecl(Expr *expr, ValueDecl *decl) { |
| 10627 | if (!isa<FunctionDecl>(decl)) return VisitExpr(expr); |
| 10628 | |
| 10629 | expr->setType(decl->getType()); |
| 10630 | |
| 10631 | assert(expr->getValueKind() == VK_RValue); |
| 10632 | if (S.getLangOptions().CPlusPlus && |
| 10633 | !(isa<CXXMethodDecl>(decl) && |
| 10634 | cast<CXXMethodDecl>(decl)->isInstance())) |
| 10635 | expr->setValueKind(VK_LValue); |
| 10636 | |
| 10637 | return expr; |
| 10638 | } |
| 10639 | |
| 10640 | ExprResult VisitMemberExpr(MemberExpr *mem) { |
| 10641 | return resolveDecl(mem, mem->getMemberDecl()); |
| 10642 | } |
| 10643 | |
| 10644 | ExprResult VisitDeclRefExpr(DeclRefExpr *ref) { |
| 10645 | return resolveDecl(ref, ref->getDecl()); |
| 10646 | } |
| 10647 | }; |
| 10648 | } |
| 10649 | |
| 10650 | /// Given a function expression of unknown-any type, try to rebuild it |
| 10651 | /// to have a function type. |
| 10652 | static ExprResult rebuildUnknownAnyFunction(Sema &S, Expr *fn) { |
| 10653 | ExprResult result = RebuildUnknownAnyFunction(S).Visit(fn); |
| 10654 | if (result.isInvalid()) return ExprError(); |
| 10655 | return S.DefaultFunctionArrayConversion(result.take()); |
| 10656 | } |
| 10657 | |
| 10658 | namespace { |
| John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10659 | /// A visitor for rebuilding an expression of type __unknown_anytype |
| 10660 | /// into one which resolves the type directly on the referring |
| 10661 | /// expression. Strict preservation of the original source |
| 10662 | /// structure is not a goal. |
| John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10663 | struct RebuildUnknownAnyExpr |
| John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10664 | : StmtVisitor<RebuildUnknownAnyExpr, ExprResult> { |
| John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10665 | |
| 10666 | Sema &S; |
| 10667 | |
| 10668 | /// The current destination type. |
| 10669 | QualType DestType; |
| 10670 | |
| 10671 | RebuildUnknownAnyExpr(Sema &S, QualType castType) |
| 10672 | : S(S), DestType(castType) {} |
| 10673 | |
| John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10674 | ExprResult VisitStmt(Stmt *S) { |
| John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10675 | llvm_unreachable("unexpected statement!"); |
| John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10676 | return ExprError(); |
| John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10677 | } |
| 10678 | |
| John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10679 | ExprResult VisitExpr(Expr *expr) { |
| 10680 | S.Diag(expr->getExprLoc(), diag::err_unsupported_unknown_any_expr) |
| 10681 | << expr->getSourceRange(); |
| 10682 | return ExprError(); |
| John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10683 | } |
| 10684 | |
| John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10685 | ExprResult VisitCallExpr(CallExpr *call); |
| 10686 | ExprResult VisitObjCMessageExpr(ObjCMessageExpr *message); |
| 10687 | |
| John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10688 | /// Rebuild an expression which simply semantically wraps another |
| 10689 | /// expression which it shares the type and value kind of. |
| 10690 | template <class T> ExprResult rebuildSugarExpr(T *expr) { |
| 10691 | ExprResult subResult = Visit(expr->getSubExpr()); |
| John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10692 | if (subResult.isInvalid()) return ExprError(); |
| John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10693 | Expr *subExpr = subResult.take(); |
| 10694 | expr->setSubExpr(subExpr); |
| 10695 | expr->setType(subExpr->getType()); |
| 10696 | expr->setValueKind(subExpr->getValueKind()); |
| 10697 | assert(expr->getObjectKind() == OK_Ordinary); |
| 10698 | return expr; |
| 10699 | } |
| John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10700 | |
| John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10701 | ExprResult VisitParenExpr(ParenExpr *paren) { |
| 10702 | return rebuildSugarExpr(paren); |
| 10703 | } |
| 10704 | |
| 10705 | ExprResult VisitUnaryExtension(UnaryOperator *op) { |
| 10706 | return rebuildSugarExpr(op); |
| 10707 | } |
| 10708 | |
| John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10709 | ExprResult VisitUnaryAddrOf(UnaryOperator *op) { |
| 10710 | const PointerType *ptr = DestType->getAs<PointerType>(); |
| 10711 | if (!ptr) { |
| 10712 | S.Diag(op->getOperatorLoc(), diag::err_unknown_any_addrof) |
| 10713 | << op->getSourceRange(); |
| 10714 | return ExprError(); |
| 10715 | } |
| 10716 | assert(op->getValueKind() == VK_RValue); |
| 10717 | assert(op->getObjectKind() == OK_Ordinary); |
| 10718 | op->setType(DestType); |
| 10719 | |
| 10720 | // Build the sub-expression as if it were an object of the pointee type. |
| 10721 | DestType = ptr->getPointeeType(); |
| 10722 | ExprResult subResult = Visit(op->getSubExpr()); |
| 10723 | if (subResult.isInvalid()) return ExprError(); |
| 10724 | op->setSubExpr(subResult.take()); |
| 10725 | return op; |
| 10726 | } |
| 10727 | |
| John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10728 | ExprResult VisitImplicitCastExpr(ImplicitCastExpr *ice); |
| John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10729 | |
| John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10730 | ExprResult resolveDecl(Expr *expr, ValueDecl *decl); |
| John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10731 | |
| John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10732 | ExprResult VisitMemberExpr(MemberExpr *mem) { |
| 10733 | return resolveDecl(mem, mem->getMemberDecl()); |
| 10734 | } |
| John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10735 | |
| 10736 | ExprResult VisitDeclRefExpr(DeclRefExpr *ref) { |
| John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10737 | return resolveDecl(ref, ref->getDecl()); |
| John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10738 | } |
| 10739 | }; |
| 10740 | } |
| 10741 | |
| John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10742 | /// Rebuilds a call expression which yielded __unknown_anytype. |
| 10743 | ExprResult RebuildUnknownAnyExpr::VisitCallExpr(CallExpr *call) { |
| 10744 | Expr *callee = call->getCallee(); |
| 10745 | |
| 10746 | enum FnKind { |
| John McCall | 4adb38c | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 10747 | FK_MemberFunction, |
| John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10748 | FK_FunctionPointer, |
| 10749 | FK_BlockPointer |
| 10750 | }; |
| 10751 | |
| 10752 | FnKind kind; |
| 10753 | QualType type = callee->getType(); |
| John McCall | 4adb38c | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 10754 | if (type == S.Context.BoundMemberTy) { |
| 10755 | assert(isa<CXXMemberCallExpr>(call) || isa<CXXOperatorCallExpr>(call)); |
| 10756 | kind = FK_MemberFunction; |
| 10757 | type = Expr::findBoundMemberType(callee); |
| John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10758 | } else if (const PointerType *ptr = type->getAs<PointerType>()) { |
| 10759 | type = ptr->getPointeeType(); |
| 10760 | kind = FK_FunctionPointer; |
| 10761 | } else { |
| 10762 | type = type->castAs<BlockPointerType>()->getPointeeType(); |
| 10763 | kind = FK_BlockPointer; |
| 10764 | } |
| 10765 | const FunctionType *fnType = type->castAs<FunctionType>(); |
| 10766 | |
| 10767 | // Verify that this is a legal result type of a function. |
| 10768 | if (DestType->isArrayType() || DestType->isFunctionType()) { |
| 10769 | unsigned diagID = diag::err_func_returning_array_function; |
| 10770 | if (kind == FK_BlockPointer) |
| 10771 | diagID = diag::err_block_returning_array_function; |
| 10772 | |
| 10773 | S.Diag(call->getExprLoc(), diagID) |
| 10774 | << DestType->isFunctionType() << DestType; |
| 10775 | return ExprError(); |
| 10776 | } |
| 10777 | |
| 10778 | // Otherwise, go ahead and set DestType as the call's result. |
| 10779 | call->setType(DestType.getNonLValueExprType(S.Context)); |
| 10780 | call->setValueKind(Expr::getValueKindForType(DestType)); |
| 10781 | assert(call->getObjectKind() == OK_Ordinary); |
| 10782 | |
| 10783 | // Rebuild the function type, replacing the result type with DestType. |
| 10784 | if (const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(fnType)) |
| 10785 | DestType = S.Context.getFunctionType(DestType, |
| 10786 | proto->arg_type_begin(), |
| 10787 | proto->getNumArgs(), |
| 10788 | proto->getExtProtoInfo()); |
| 10789 | else |
| 10790 | DestType = S.Context.getFunctionNoProtoType(DestType, |
| 10791 | fnType->getExtInfo()); |
| 10792 | |
| 10793 | // Rebuild the appropriate pointer-to-function type. |
| 10794 | switch (kind) { |
| John McCall | 4adb38c | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 10795 | case FK_MemberFunction: |
| John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10796 | // Nothing to do. |
| 10797 | break; |
| 10798 | |
| 10799 | case FK_FunctionPointer: |
| 10800 | DestType = S.Context.getPointerType(DestType); |
| 10801 | break; |
| 10802 | |
| 10803 | case FK_BlockPointer: |
| 10804 | DestType = S.Context.getBlockPointerType(DestType); |
| 10805 | break; |
| 10806 | } |
| 10807 | |
| 10808 | // Finally, we can recurse. |
| 10809 | ExprResult calleeResult = Visit(callee); |
| 10810 | if (!calleeResult.isUsable()) return ExprError(); |
| 10811 | call->setCallee(calleeResult.take()); |
| 10812 | |
| 10813 | // Bind a temporary if necessary. |
| 10814 | return S.MaybeBindToTemporary(call); |
| 10815 | } |
| 10816 | |
| 10817 | ExprResult RebuildUnknownAnyExpr::VisitObjCMessageExpr(ObjCMessageExpr *msg) { |
| John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10818 | ObjCMethodDecl *method = msg->getMethodDecl(); |
| 10819 | assert(method && "__unknown_anytype message without result type?"); |
| John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10820 | |
| John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10821 | // Verify that this is a legal result type of a call. |
| 10822 | if (DestType->isArrayType() || DestType->isFunctionType()) { |
| 10823 | S.Diag(msg->getExprLoc(), diag::err_func_returning_array_function) |
| 10824 | << DestType->isFunctionType() << DestType; |
| 10825 | return ExprError(); |
| John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10826 | } |
| 10827 | |
| John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10828 | assert(method->getResultType() == S.Context.UnknownAnyTy); |
| 10829 | method->setResultType(DestType); |
| 10830 | |
| John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10831 | // Change the type of the message. |
| John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10832 | msg->setType(DestType.getNonReferenceType()); |
| 10833 | msg->setValueKind(Expr::getValueKindForType(DestType)); |
| John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10834 | |
| John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10835 | return S.MaybeBindToTemporary(msg); |
| John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10836 | } |
| 10837 | |
| 10838 | ExprResult RebuildUnknownAnyExpr::VisitImplicitCastExpr(ImplicitCastExpr *ice) { |
| John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10839 | // The only case we should ever see here is a function-to-pointer decay. |
| John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10840 | assert(ice->getCastKind() == CK_FunctionToPointerDecay); |
| John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10841 | assert(ice->getValueKind() == VK_RValue); |
| 10842 | assert(ice->getObjectKind() == OK_Ordinary); |
| 10843 | |
| John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10844 | ice->setType(DestType); |
| 10845 | |
| John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10846 | // Rebuild the sub-expression as the pointee (function) type. |
| 10847 | DestType = DestType->castAs<PointerType>()->getPointeeType(); |
| 10848 | |
| 10849 | ExprResult result = Visit(ice->getSubExpr()); |
| 10850 | if (!result.isUsable()) return ExprError(); |
| 10851 | |
| 10852 | ice->setSubExpr(result.take()); |
| 10853 | return S.Owned(ice); |
| 10854 | } |
| 10855 | |
| John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10856 | ExprResult RebuildUnknownAnyExpr::resolveDecl(Expr *expr, ValueDecl *decl) { |
| John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10857 | ExprValueKind valueKind = VK_LValue; |
| John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10858 | QualType type = DestType; |
| 10859 | |
| 10860 | // We know how to make this work for certain kinds of decls: |
| 10861 | |
| 10862 | // - functions |
| John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10863 | if (FunctionDecl *fn = dyn_cast<FunctionDecl>(decl)) { |
| John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10864 | // This is true because FunctionDecls must always have function |
| 10865 | // type, so we can't be resolving the entire thing at once. |
| 10866 | assert(type->isFunctionType()); |
| 10867 | |
| John McCall | 4adb38c | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 10868 | if (CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(fn)) |
| 10869 | if (method->isInstance()) { |
| 10870 | valueKind = VK_RValue; |
| 10871 | type = S.Context.BoundMemberTy; |
| 10872 | } |
| 10873 | |
| John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10874 | // Function references aren't l-values in C. |
| 10875 | if (!S.getLangOptions().CPlusPlus) |
| 10876 | valueKind = VK_RValue; |
| 10877 | |
| 10878 | // - variables |
| 10879 | } else if (isa<VarDecl>(decl)) { |
| John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10880 | if (const ReferenceType *refTy = type->getAs<ReferenceType>()) { |
| 10881 | type = refTy->getPointeeType(); |
| John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10882 | } else if (type->isFunctionType()) { |
| John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10883 | S.Diag(expr->getExprLoc(), diag::err_unknown_any_var_function_type) |
| 10884 | << decl << expr->getSourceRange(); |
| 10885 | return ExprError(); |
| John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10886 | } |
| 10887 | |
| 10888 | // - nothing else |
| 10889 | } else { |
| 10890 | S.Diag(expr->getExprLoc(), diag::err_unsupported_unknown_any_decl) |
| 10891 | << decl << expr->getSourceRange(); |
| 10892 | return ExprError(); |
| 10893 | } |
| 10894 | |
| John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10895 | decl->setType(DestType); |
| 10896 | expr->setType(type); |
| 10897 | expr->setValueKind(valueKind); |
| 10898 | return S.Owned(expr); |
| John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10899 | } |
| 10900 | |
| John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10901 | /// Check a cast of an unknown-any type. We intentionally only |
| 10902 | /// trigger this for C-style casts. |
| John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10903 | ExprResult Sema::checkUnknownAnyCast(SourceRange typeRange, QualType castType, |
| 10904 | Expr *castExpr, CastKind &castKind, |
| 10905 | ExprValueKind &VK, CXXCastPath &path) { |
| John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10906 | // Rewrite the casted expression from scratch. |
| John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10907 | ExprResult result = RebuildUnknownAnyExpr(*this, castType).Visit(castExpr); |
| 10908 | if (!result.isUsable()) return ExprError(); |
| John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10909 | |
| John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10910 | castExpr = result.take(); |
| 10911 | VK = castExpr->getValueKind(); |
| 10912 | castKind = CK_NoOp; |
| 10913 | |
| 10914 | return castExpr; |
| John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10915 | } |
| 10916 | |
| 10917 | static ExprResult diagnoseUnknownAnyExpr(Sema &S, Expr *e) { |
| 10918 | Expr *orig = e; |
| John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10919 | unsigned diagID = diag::err_uncasted_use_of_unknown_any; |
| John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10920 | while (true) { |
| 10921 | e = e->IgnoreParenImpCasts(); |
| John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10922 | if (CallExpr *call = dyn_cast<CallExpr>(e)) { |
| John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10923 | e = call->getCallee(); |
| John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10924 | diagID = diag::err_uncasted_call_of_unknown_any; |
| 10925 | } else { |
| John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10926 | break; |
| John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10927 | } |
| John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10928 | } |
| 10929 | |
| John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10930 | SourceLocation loc; |
| 10931 | NamedDecl *d; |
| 10932 | if (DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e)) { |
| 10933 | loc = ref->getLocation(); |
| 10934 | d = ref->getDecl(); |
| 10935 | } else if (MemberExpr *mem = dyn_cast<MemberExpr>(e)) { |
| 10936 | loc = mem->getMemberLoc(); |
| 10937 | d = mem->getMemberDecl(); |
| 10938 | } else if (ObjCMessageExpr *msg = dyn_cast<ObjCMessageExpr>(e)) { |
| 10939 | diagID = diag::err_uncasted_call_of_unknown_any; |
| 10940 | loc = msg->getSelectorLoc(); |
| 10941 | d = msg->getMethodDecl(); |
| 10942 | assert(d && "unknown method returning __unknown_any?"); |
| 10943 | } else { |
| 10944 | S.Diag(e->getExprLoc(), diag::err_unsupported_unknown_any_expr) |
| 10945 | << e->getSourceRange(); |
| 10946 | return ExprError(); |
| 10947 | } |
| 10948 | |
| 10949 | S.Diag(loc, diagID) << d << orig->getSourceRange(); |
| John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10950 | |
| 10951 | // Never recoverable. |
| 10952 | return ExprError(); |
| 10953 | } |
| 10954 | |
| John McCall | 36e7fe3 | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 10955 | /// Check for operands with placeholder types and complain if found. |
| 10956 | /// Returns true if there was an error and no recovery was possible. |
| John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 10957 | ExprResult Sema::CheckPlaceholderExpr(Expr *E) { |
| John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10958 | // Placeholder types are always *exactly* the appropriate builtin type. |
| 10959 | QualType type = E->getType(); |
| John McCall | 36e7fe3 | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 10960 | |
| John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10961 | // Overloaded expressions. |
| 10962 | if (type == Context.OverloadTy) |
| 10963 | return ResolveAndFixSingleFunctionTemplateSpecialization(E, false, true, |
| Douglas Gregor | 89f3cd5 | 2011-03-16 19:16:25 +0000 | [diff] [blame] | 10964 | E->getSourceRange(), |
| John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10965 | QualType(), |
| 10966 | diag::err_ovl_unresolvable); |
| 10967 | |
| John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10968 | // Bound member functions. |
| 10969 | if (type == Context.BoundMemberTy) { |
| 10970 | Diag(E->getLocStart(), diag::err_invalid_use_of_bound_member_func) |
| 10971 | << E->getSourceRange(); |
| 10972 | return ExprError(); |
| 10973 | } |
| 10974 | |
| John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10975 | // Expressions of unknown type. |
| 10976 | if (type == Context.UnknownAnyTy) |
| 10977 | return diagnoseUnknownAnyExpr(*this, E); |
| 10978 | |
| 10979 | assert(!type->isPlaceholderType()); |
| 10980 | return Owned(E); |
| John McCall | 36e7fe3 | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 10981 | } |
| Richard Trieu | 2c850c0 | 2011-04-21 21:44:26 +0000 | [diff] [blame] | 10982 | |
| 10983 | bool Sema::CheckCaseExpression(Expr *expr) { |
| 10984 | if (expr->isTypeDependent()) |
| 10985 | return true; |
| 10986 | if (expr->isValueDependent() || expr->isIntegerConstantExpr(Context)) |
| 10987 | return expr->getType()->isIntegralOrEnumerationType(); |
| 10988 | return false; |
| 10989 | } |