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); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 90 | Diag(D->getLocation(), diag::note_unavailable_here) << 1 << true; |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 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; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 117 | Diag(D->getLocation(), diag::note_unavailable_here) |
| 118 | << isa<FunctionDecl>(D) << false; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 119 | break; |
| 120 | } |
| 121 | |
Anders Carlsson | 73067a0 | 2010-10-22 23:37:08 +0000 | [diff] [blame] | 122 | // Warn if this is used but marked unused. |
| 123 | if (D->hasAttr<UnusedAttr>()) |
| 124 | Diag(Loc, diag::warn_used_but_marked_unused) << D->getDeclName(); |
| 125 | |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 126 | return false; |
Chris Lattner | 4bf74fd | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 127 | } |
| 128 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 129 | /// \brief Retrieve the message suffix that should be added to a |
| 130 | /// diagnostic complaining about the given function being deleted or |
| 131 | /// unavailable. |
| 132 | std::string Sema::getDeletedOrUnavailableSuffix(const FunctionDecl *FD) { |
| 133 | // FIXME: C++0x implicitly-deleted special member functions could be |
| 134 | // detected here so that we could improve diagnostics to say, e.g., |
| 135 | // "base class 'A' had a deleted copy constructor". |
| 136 | if (FD->isDeleted()) |
| 137 | return std::string(); |
| 138 | |
| 139 | std::string Message; |
| 140 | if (FD->getAvailability(&Message)) |
| 141 | return ": " + Message; |
| 142 | |
| 143 | return std::string(); |
| 144 | } |
| 145 | |
Fariborz Jahanian | 027b886 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 146 | /// DiagnoseSentinelCalls - This routine checks on method dispatch calls |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 147 | /// (and other functions in future), which have been declared with sentinel |
Fariborz Jahanian | 027b886 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 148 | /// attribute. It warns if call does not have the sentinel argument. |
| 149 | /// |
| 150 | void Sema::DiagnoseSentinelCalls(NamedDecl *D, SourceLocation Loc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 151 | Expr **Args, unsigned NumArgs) { |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 152 | const SentinelAttr *attr = D->getAttr<SentinelAttr>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 153 | if (!attr) |
Fariborz Jahanian | 9e87721 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 154 | return; |
Douglas Gregor | c298ffc | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 155 | |
| 156 | // FIXME: In C++0x, if any of the arguments are parameter pack |
| 157 | // expansions, we can't check for the sentinel now. |
Fariborz Jahanian | 9e87721 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 158 | int sentinelPos = attr->getSentinel(); |
| 159 | int nullPos = attr->getNullPos(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 160 | |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 161 | // FIXME. ObjCMethodDecl and FunctionDecl need be derived from the same common |
| 162 | // 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] | 163 | unsigned int i = 0; |
Fariborz Jahanian | 4a52803 | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 164 | bool warnNotEnoughArgs = false; |
| 165 | int isMethod = 0; |
| 166 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 167 | // skip over named parameters. |
| 168 | ObjCMethodDecl::param_iterator P, E = MD->param_end(); |
| 169 | for (P = MD->param_begin(); (P != E && i < NumArgs); ++P) { |
| 170 | if (nullPos) |
| 171 | --nullPos; |
| 172 | else |
| 173 | ++i; |
| 174 | } |
| 175 | warnNotEnoughArgs = (P != E || i >= NumArgs); |
| 176 | isMethod = 1; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 177 | } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Fariborz Jahanian | 4a52803 | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 178 | // skip over named parameters. |
| 179 | ObjCMethodDecl::param_iterator P, E = FD->param_end(); |
| 180 | for (P = FD->param_begin(); (P != E && i < NumArgs); ++P) { |
| 181 | if (nullPos) |
| 182 | --nullPos; |
| 183 | else |
| 184 | ++i; |
| 185 | } |
| 186 | warnNotEnoughArgs = (P != E || i >= NumArgs); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 187 | } else if (VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 188 | // block or function pointer call. |
| 189 | QualType Ty = V->getType(); |
| 190 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 191 | const FunctionType *FT = Ty->isFunctionPointerType() |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 192 | ? Ty->getAs<PointerType>()->getPointeeType()->getAs<FunctionType>() |
| 193 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 194 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT)) { |
| 195 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 196 | unsigned k; |
| 197 | for (k = 0; (k != NumArgsInProto && i < NumArgs); k++) { |
| 198 | if (nullPos) |
| 199 | --nullPos; |
| 200 | else |
| 201 | ++i; |
| 202 | } |
| 203 | warnNotEnoughArgs = (k != NumArgsInProto || i >= NumArgs); |
| 204 | } |
| 205 | if (Ty->isBlockPointerType()) |
| 206 | isMethod = 2; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 207 | } else |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 208 | return; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 209 | } else |
Fariborz Jahanian | 4a52803 | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 210 | return; |
| 211 | |
| 212 | if (warnNotEnoughArgs) { |
Fariborz Jahanian | 9e87721 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 213 | Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName(); |
Fariborz Jahanian | 4a52803 | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 214 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 9e87721 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 215 | return; |
| 216 | } |
| 217 | int sentinel = i; |
| 218 | while (sentinelPos > 0 && i < NumArgs-1) { |
| 219 | --sentinelPos; |
| 220 | ++i; |
| 221 | } |
| 222 | if (sentinelPos > 0) { |
| 223 | Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName(); |
Fariborz Jahanian | 4a52803 | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 224 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 9e87721 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 225 | return; |
| 226 | } |
| 227 | while (i < NumArgs-1) { |
| 228 | ++i; |
| 229 | ++sentinel; |
| 230 | } |
| 231 | Expr *sentinelExpr = Args[sentinel]; |
John McCall | 7ddbcf4 | 2010-05-06 23:53:00 +0000 | [diff] [blame] | 232 | if (!sentinelExpr) return; |
| 233 | if (sentinelExpr->isTypeDependent()) return; |
| 234 | if (sentinelExpr->isValueDependent()) return; |
Anders Carlsson | e981a8c | 2010-11-05 15:21:33 +0000 | [diff] [blame] | 235 | |
| 236 | // nullptr_t is always treated as null. |
| 237 | if (sentinelExpr->getType()->isNullPtrType()) return; |
| 238 | |
Fariborz Jahanian | c0b0ced | 2010-07-14 16:37:51 +0000 | [diff] [blame] | 239 | if (sentinelExpr->getType()->isAnyPointerType() && |
John McCall | 7ddbcf4 | 2010-05-06 23:53:00 +0000 | [diff] [blame] | 240 | sentinelExpr->IgnoreParenCasts()->isNullPointerConstant(Context, |
| 241 | Expr::NPC_ValueDependentIsNull)) |
| 242 | return; |
| 243 | |
| 244 | // Unfortunately, __null has type 'int'. |
| 245 | if (isa<GNUNullExpr>(sentinelExpr)) return; |
| 246 | |
| 247 | Diag(Loc, diag::warn_missing_sentinel) << isMethod; |
| 248 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 027b886 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Douglas Gregor | 87f95b0 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 251 | SourceRange Sema::getExprRange(ExprTy *E) const { |
| 252 | Expr *Ex = (Expr *)E; |
| 253 | return Ex? Ex->getSourceRange() : SourceRange(); |
| 254 | } |
| 255 | |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 256 | //===----------------------------------------------------------------------===// |
| 257 | // Standard Promotions and Conversions |
| 258 | //===----------------------------------------------------------------------===// |
| 259 | |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 260 | /// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4). |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 261 | ExprResult Sema::DefaultFunctionArrayConversion(Expr *E) { |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 262 | QualType Ty = E->getType(); |
| 263 | assert(!Ty.isNull() && "DefaultFunctionArrayConversion - missing type"); |
| 264 | |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 265 | if (Ty->isFunctionType()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 266 | E = ImpCastExprToType(E, Context.getPointerType(Ty), |
| 267 | CK_FunctionToPointerDecay).take(); |
Chris Lattner | 61f60a0 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 268 | else if (Ty->isArrayType()) { |
| 269 | // In C90 mode, arrays only promote to pointers if the array expression is |
| 270 | // an lvalue. The relevant legalese is C90 6.2.2.1p3: "an lvalue that has |
| 271 | // type 'array of type' is converted to an expression that has type 'pointer |
| 272 | // to type'...". In C99 this was changed to: C99 6.3.2.1p3: "an expression |
| 273 | // that has type 'array of type' ...". The relevant change is "an lvalue" |
| 274 | // (C90) to "an expression" (C99). |
Argyrios Kyrtzidis | 9321c74 | 2008-09-11 04:25:59 +0000 | [diff] [blame] | 275 | // |
| 276 | // C++ 4.2p1: |
| 277 | // An lvalue or rvalue of type "array of N T" or "array of unknown bound of |
| 278 | // T" can be converted to an rvalue of type "pointer to T". |
| 279 | // |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 280 | if (getLangOptions().C99 || getLangOptions().CPlusPlus || E->isLValue()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 281 | E = ImpCastExprToType(E, Context.getArrayDecayedType(Ty), |
| 282 | CK_ArrayToPointerDecay).take(); |
Chris Lattner | 61f60a0 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 283 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 284 | return Owned(E); |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 285 | } |
| 286 | |
Argyrios Kyrtzidis | a9b630e | 2011-04-26 17:41:22 +0000 | [diff] [blame] | 287 | static void CheckForNullPointerDereference(Sema &S, Expr *E) { |
| 288 | // Check to see if we are dereferencing a null pointer. If so, |
| 289 | // and if not volatile-qualified, this is undefined behavior that the |
| 290 | // optimizer will delete, so warn about it. People sometimes try to use this |
| 291 | // to get a deterministic trap and are surprised by clang's behavior. This |
| 292 | // only handles the pattern "*null", which is a very syntactic check. |
| 293 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E->IgnoreParenCasts())) |
| 294 | if (UO->getOpcode() == UO_Deref && |
| 295 | UO->getSubExpr()->IgnoreParenCasts()-> |
| 296 | isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull) && |
| 297 | !UO->getType().isVolatileQualified()) { |
| 298 | S.DiagRuntimeBehavior(UO->getOperatorLoc(), UO, |
| 299 | S.PDiag(diag::warn_indirection_through_null) |
| 300 | << UO->getSubExpr()->getSourceRange()); |
| 301 | S.DiagRuntimeBehavior(UO->getOperatorLoc(), UO, |
| 302 | S.PDiag(diag::note_indirection_through_null)); |
| 303 | } |
| 304 | } |
| 305 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 306 | ExprResult Sema::DefaultLvalueConversion(Expr *E) { |
John McCall | f3735e0 | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 307 | // C++ [conv.lval]p1: |
| 308 | // A glvalue of a non-function, non-array type T can be |
| 309 | // converted to a prvalue. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 310 | if (!E->isGLValue()) return Owned(E); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 311 | |
John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 312 | QualType T = E->getType(); |
| 313 | assert(!T.isNull() && "r-value conversion on typeless expression?"); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 314 | |
John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 315 | // Create a load out of an ObjCProperty l-value, if necessary. |
| 316 | if (E->getObjectKind() == OK_ObjCProperty) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 317 | ExprResult Res = ConvertPropertyForRValue(E); |
| 318 | if (Res.isInvalid()) |
| 319 | return Owned(E); |
| 320 | E = Res.take(); |
John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 321 | if (!E->isGLValue()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 322 | return Owned(E); |
Douglas Gregor | b92a156 | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 323 | } |
John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 324 | |
| 325 | // We don't want to throw lvalue-to-rvalue casts on top of |
| 326 | // expressions of certain types in C++. |
| 327 | if (getLangOptions().CPlusPlus && |
| 328 | (E->getType() == Context.OverloadTy || |
| 329 | T->isDependentType() || |
| 330 | T->isRecordType())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 331 | return Owned(E); |
John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 332 | |
| 333 | // The C standard is actually really unclear on this point, and |
| 334 | // DR106 tells us what the result should be but not why. It's |
| 335 | // generally best to say that void types just doesn't undergo |
| 336 | // lvalue-to-rvalue at all. Note that expressions of unqualified |
| 337 | // 'void' type are never l-values, but qualified void can be. |
| 338 | if (T->isVoidType()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 339 | return Owned(E); |
John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 340 | |
Argyrios Kyrtzidis | a9b630e | 2011-04-26 17:41:22 +0000 | [diff] [blame] | 341 | CheckForNullPointerDereference(*this, E); |
| 342 | |
John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 343 | // C++ [conv.lval]p1: |
| 344 | // [...] If T is a non-class type, the type of the prvalue is the |
| 345 | // cv-unqualified version of T. Otherwise, the type of the |
| 346 | // rvalue is T. |
| 347 | // |
| 348 | // C99 6.3.2.1p2: |
| 349 | // If the lvalue has qualified type, the value has the unqualified |
| 350 | // version of the type of the lvalue; otherwise, the value has the |
| 351 | // type of the lvalue. |
| 352 | if (T.hasQualifiers()) |
| 353 | T = T.getUnqualifiedType(); |
| 354 | |
Ted Kremenek | df26df7 | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 355 | CheckArrayAccess(E); |
Ted Kremenek | 64699be | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 356 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 357 | return Owned(ImplicitCastExpr::Create(Context, T, CK_LValueToRValue, |
| 358 | E, 0, VK_RValue)); |
John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 359 | } |
| 360 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 361 | ExprResult Sema::DefaultFunctionArrayLvalueConversion(Expr *E) { |
| 362 | ExprResult Res = DefaultFunctionArrayConversion(E); |
| 363 | if (Res.isInvalid()) |
| 364 | return ExprError(); |
| 365 | Res = DefaultLvalueConversion(Res.take()); |
| 366 | if (Res.isInvalid()) |
| 367 | return ExprError(); |
| 368 | return move(Res); |
Douglas Gregor | b92a156 | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 372 | /// UsualUnaryConversions - Performs various conversions that are common to most |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 373 | /// operators (C99 6.3). The conversions of array and function types are |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 374 | /// sometimes suppressed. For example, the array->pointer conversion doesn't |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 375 | /// apply if the array is an argument to the sizeof or address (&) operators. |
| 376 | /// In these instances, this routine should *not* be called. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 377 | ExprResult Sema::UsualUnaryConversions(Expr *E) { |
John McCall | f3735e0 | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 378 | // First, convert to an r-value. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 379 | ExprResult Res = DefaultFunctionArrayLvalueConversion(E); |
| 380 | if (Res.isInvalid()) |
| 381 | return Owned(E); |
| 382 | E = Res.take(); |
John McCall | f3735e0 | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 383 | |
| 384 | QualType Ty = E->getType(); |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 385 | assert(!Ty.isNull() && "UsualUnaryConversions - missing type"); |
John McCall | f3735e0 | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 386 | |
| 387 | // Try to perform integral promotions if the object has a theoretically |
| 388 | // promotable type. |
| 389 | if (Ty->isIntegralOrUnscopedEnumerationType()) { |
| 390 | // C99 6.3.1.1p2: |
| 391 | // |
| 392 | // The following may be used in an expression wherever an int or |
| 393 | // unsigned int may be used: |
| 394 | // - an object or expression with an integer type whose integer |
| 395 | // conversion rank is less than or equal to the rank of int |
| 396 | // and unsigned int. |
| 397 | // - A bit-field of type _Bool, int, signed int, or unsigned int. |
| 398 | // |
| 399 | // If an int can represent all values of the original type, the |
| 400 | // value is converted to an int; otherwise, it is converted to an |
| 401 | // unsigned int. These are called the integer promotions. All |
| 402 | // other types are unchanged by the integer promotions. |
| 403 | |
| 404 | QualType PTy = Context.isPromotableBitField(E); |
| 405 | if (!PTy.isNull()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 406 | E = ImpCastExprToType(E, PTy, CK_IntegralCast).take(); |
| 407 | return Owned(E); |
John McCall | f3735e0 | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 408 | } |
| 409 | if (Ty->isPromotableIntegerType()) { |
| 410 | QualType PT = Context.getPromotedIntegerType(Ty); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 411 | E = ImpCastExprToType(E, PT, CK_IntegralCast).take(); |
| 412 | return Owned(E); |
John McCall | f3735e0 | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 413 | } |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 414 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 415 | return Owned(E); |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 416 | } |
| 417 | |
Chris Lattner | 2ce500f | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 418 | /// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 419 | /// 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] | 420 | /// double. All other argument types are converted by UsualUnaryConversions(). |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 421 | ExprResult Sema::DefaultArgumentPromotion(Expr *E) { |
| 422 | QualType Ty = E->getType(); |
Chris Lattner | 2ce500f | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 423 | assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 424 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 425 | ExprResult Res = UsualUnaryConversions(E); |
| 426 | if (Res.isInvalid()) |
| 427 | return Owned(E); |
| 428 | E = Res.take(); |
John McCall | 9bc2677 | 2010-12-06 18:36:11 +0000 | [diff] [blame] | 429 | |
Chris Lattner | 2ce500f | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 430 | // If this is a 'float' (CVR qualified or typedef) promote to double. |
Chris Lattner | bb53efb | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 431 | if (Ty->isSpecificBuiltinType(BuiltinType::Float)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 432 | E = ImpCastExprToType(E, Context.DoubleTy, CK_FloatingCast).take(); |
| 433 | |
| 434 | return Owned(E); |
Chris Lattner | 2ce500f | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 437 | /// DefaultVariadicArgumentPromotion - Like DefaultArgumentPromotion, but |
| 438 | /// 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] | 439 | /// interfaces passed by value. |
| 440 | ExprResult Sema::DefaultVariadicArgumentPromotion(Expr *E, VariadicCallType CT, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 441 | FunctionDecl *FDecl) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 442 | ExprResult ExprRes = DefaultArgumentPromotion(E); |
| 443 | if (ExprRes.isInvalid()) |
| 444 | return ExprError(); |
| 445 | E = ExprRes.take(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 446 | |
Chris Lattner | bb53efb | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 447 | // __builtin_va_start takes the second argument as a "varargs" argument, but |
| 448 | // it doesn't actually do anything with it. It doesn't need to be non-pod |
| 449 | // etc. |
| 450 | if (FDecl && FDecl->getBuiltinID() == Builtin::BI__builtin_va_start) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 451 | return Owned(E); |
Chris Lattner | bb53efb | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 452 | |
Douglas Gregor | 347e0f2 | 2011-05-21 19:26:31 +0000 | [diff] [blame] | 453 | // 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] | 454 | if (E->getType()->isObjCObjectType() && |
Douglas Gregor | 347e0f2 | 2011-05-21 19:26:31 +0000 | [diff] [blame] | 455 | DiagRuntimeBehavior(E->getLocStart(), 0, |
| 456 | PDiag(diag::err_cannot_pass_objc_interface_to_vararg) |
| 457 | << E->getType() << CT)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 458 | return ExprError(); |
Douglas Gregor | 347e0f2 | 2011-05-21 19:26:31 +0000 | [diff] [blame] | 459 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 460 | if (!E->getType().isPODType(Context)) { |
Douglas Gregor | 253cadf | 2011-05-21 16:27:21 +0000 | [diff] [blame] | 461 | // C++0x [expr.call]p7: |
| 462 | // Passing a potentially-evaluated argument of class type (Clause 9) |
| 463 | // having a non-trivial copy constructor, a non-trivial move constructor, |
| 464 | // or a non-trivial destructor, with no corresponding parameter, |
| 465 | // is conditionally-supported with implementation-defined semantics. |
| 466 | bool TrivialEnough = false; |
| 467 | if (getLangOptions().CPlusPlus0x && !E->getType()->isDependentType()) { |
| 468 | if (CXXRecordDecl *Record = E->getType()->getAsCXXRecordDecl()) { |
| 469 | if (Record->hasTrivialCopyConstructor() && |
| 470 | Record->hasTrivialMoveConstructor() && |
| 471 | Record->hasTrivialDestructor()) |
| 472 | TrivialEnough = true; |
| 473 | } |
| 474 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 475 | |
| 476 | if (!TrivialEnough && |
| 477 | getLangOptions().ObjCAutoRefCount && |
| 478 | E->getType()->isObjCLifetimeType()) |
| 479 | TrivialEnough = true; |
Douglas Gregor | 253cadf | 2011-05-21 16:27:21 +0000 | [diff] [blame] | 480 | |
| 481 | if (TrivialEnough) { |
| 482 | // Nothing to diagnose. This is okay. |
| 483 | } else if (DiagRuntimeBehavior(E->getLocStart(), 0, |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 484 | PDiag(diag::warn_cannot_pass_non_pod_arg_to_vararg) |
Douglas Gregor | 253cadf | 2011-05-21 16:27:21 +0000 | [diff] [blame] | 485 | << getLangOptions().CPlusPlus0x << E->getType() |
Douglas Gregor | 347e0f2 | 2011-05-21 19:26:31 +0000 | [diff] [blame] | 486 | << CT)) { |
| 487 | // Turn this into a trap. |
| 488 | CXXScopeSpec SS; |
| 489 | UnqualifiedId Name; |
| 490 | Name.setIdentifier(PP.getIdentifierInfo("__builtin_trap"), |
| 491 | E->getLocStart()); |
| 492 | ExprResult TrapFn = ActOnIdExpression(TUScope, SS, Name, true, false); |
| 493 | if (TrapFn.isInvalid()) |
| 494 | return ExprError(); |
| 495 | |
| 496 | ExprResult Call = ActOnCallExpr(TUScope, TrapFn.get(), E->getLocStart(), |
| 497 | MultiExprArg(), E->getLocEnd()); |
| 498 | if (Call.isInvalid()) |
| 499 | return ExprError(); |
| 500 | |
| 501 | ExprResult Comma = ActOnBinOp(TUScope, E->getLocStart(), tok::comma, |
| 502 | Call.get(), E); |
| 503 | if (Comma.isInvalid()) |
| 504 | return ExprError(); |
| 505 | |
| 506 | E = Comma.get(); |
| 507 | } |
Douglas Gregor | 253cadf | 2011-05-21 16:27:21 +0000 | [diff] [blame] | 508 | } |
| 509 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 510 | return Owned(E); |
Anders Carlsson | a7d069d | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 511 | } |
| 512 | |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 513 | /// UsualArithmeticConversions - Performs various conversions that are common to |
| 514 | /// 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] | 515 | /// routine returns the first non-arithmetic type found. The client is |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 516 | /// responsible for emitting appropriate error diagnostics. |
| 517 | /// FIXME: verify the conversion rules for "complex int" are consistent with |
| 518 | /// GCC. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 519 | QualType Sema::UsualArithmeticConversions(ExprResult &lhsExpr, ExprResult &rhsExpr, |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 520 | bool isCompAssign) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 521 | if (!isCompAssign) { |
| 522 | lhsExpr = UsualUnaryConversions(lhsExpr.take()); |
| 523 | if (lhsExpr.isInvalid()) |
| 524 | return QualType(); |
| 525 | } |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 526 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 527 | rhsExpr = UsualUnaryConversions(rhsExpr.take()); |
| 528 | if (rhsExpr.isInvalid()) |
| 529 | return QualType(); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 530 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 531 | // For conversion purposes, we ignore any qualifiers. |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 532 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 533 | QualType lhs = |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 534 | Context.getCanonicalType(lhsExpr.get()->getType()).getUnqualifiedType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 535 | QualType rhs = |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 536 | Context.getCanonicalType(rhsExpr.get()->getType()).getUnqualifiedType(); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 537 | |
| 538 | // If both types are identical, no conversion is needed. |
| 539 | if (lhs == rhs) |
| 540 | return lhs; |
| 541 | |
| 542 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 543 | // The caller can deal with this (e.g. pointer + int). |
| 544 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
| 545 | return lhs; |
| 546 | |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 547 | // Apply unary and bitfield promotions to the LHS's type. |
| 548 | QualType lhs_unpromoted = lhs; |
| 549 | if (lhs->isPromotableIntegerType()) |
| 550 | lhs = Context.getPromotedIntegerType(lhs); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 551 | QualType LHSBitfieldPromoteTy = Context.isPromotableBitField(lhsExpr.get()); |
Douglas Gregor | d2c2d17 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 552 | if (!LHSBitfieldPromoteTy.isNull()) |
| 553 | lhs = LHSBitfieldPromoteTy; |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 554 | if (lhs != lhs_unpromoted && !isCompAssign) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 555 | lhsExpr = ImpCastExprToType(lhsExpr.take(), lhs, CK_IntegralCast); |
Douglas Gregor | d2c2d17 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 556 | |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 557 | // If both types are identical, no conversion is needed. |
| 558 | if (lhs == rhs) |
| 559 | return lhs; |
| 560 | |
| 561 | // At this point, we have two different arithmetic types. |
| 562 | |
| 563 | // Handle complex types first (C99 6.3.1.8p1). |
| 564 | bool LHSComplexFloat = lhs->isComplexType(); |
| 565 | bool RHSComplexFloat = rhs->isComplexType(); |
| 566 | if (LHSComplexFloat || RHSComplexFloat) { |
| 567 | // if we have an integer operand, the result is the complex type. |
| 568 | |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 569 | if (!RHSComplexFloat && !rhs->isRealFloatingType()) { |
| 570 | if (rhs->isIntegerType()) { |
| 571 | QualType fp = cast<ComplexType>(lhs)->getElementType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 572 | rhsExpr = ImpCastExprToType(rhsExpr.take(), fp, CK_IntegralToFloating); |
| 573 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_FloatingRealToComplex); |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 574 | } else { |
| 575 | assert(rhs->isComplexIntegerType()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 576 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralComplexToFloatingComplex); |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 577 | } |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 578 | return lhs; |
| 579 | } |
| 580 | |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 581 | if (!LHSComplexFloat && !lhs->isRealFloatingType()) { |
| 582 | if (!isCompAssign) { |
| 583 | // int -> float -> _Complex float |
| 584 | if (lhs->isIntegerType()) { |
| 585 | QualType fp = cast<ComplexType>(rhs)->getElementType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 586 | lhsExpr = ImpCastExprToType(lhsExpr.take(), fp, CK_IntegralToFloating); |
| 587 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_FloatingRealToComplex); |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 588 | } else { |
| 589 | assert(lhs->isComplexIntegerType()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 590 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralComplexToFloatingComplex); |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 591 | } |
| 592 | } |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 593 | return rhs; |
| 594 | } |
| 595 | |
| 596 | // This handles complex/complex, complex/float, or float/complex. |
| 597 | // When both operands are complex, the shorter operand is converted to the |
| 598 | // type of the longer, and that is the type of the result. This corresponds |
| 599 | // to what is done when combining two real floating-point operands. |
| 600 | // The fun begins when size promotion occur across type domains. |
| 601 | // From H&S 6.3.4: When one operand is complex and the other is a real |
| 602 | // floating-point type, the less precise type is converted, within it's |
| 603 | // real or complex domain, to the precision of the other type. For example, |
| 604 | // when combining a "long double" with a "double _Complex", the |
| 605 | // "double _Complex" is promoted to "long double _Complex". |
| 606 | int order = Context.getFloatingTypeOrder(lhs, rhs); |
| 607 | |
| 608 | // If both are complex, just cast to the more precise type. |
| 609 | if (LHSComplexFloat && RHSComplexFloat) { |
| 610 | if (order > 0) { |
| 611 | // _Complex float -> _Complex double |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 612 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_FloatingComplexCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 613 | return lhs; |
| 614 | |
| 615 | } else if (order < 0) { |
| 616 | // _Complex float -> _Complex double |
| 617 | if (!isCompAssign) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 618 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_FloatingComplexCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 619 | return rhs; |
| 620 | } |
| 621 | return lhs; |
| 622 | } |
| 623 | |
| 624 | // If just the LHS is complex, the RHS needs to be converted, |
| 625 | // and the LHS might need to be promoted. |
| 626 | if (LHSComplexFloat) { |
| 627 | if (order > 0) { // LHS is wider |
| 628 | // float -> _Complex double |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 629 | QualType fp = cast<ComplexType>(lhs)->getElementType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 630 | rhsExpr = ImpCastExprToType(rhsExpr.take(), fp, CK_FloatingCast); |
| 631 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_FloatingRealToComplex); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 632 | return lhs; |
| 633 | } |
| 634 | |
| 635 | // RHS is at least as wide. Find its corresponding complex type. |
| 636 | QualType result = (order == 0 ? lhs : Context.getComplexType(rhs)); |
| 637 | |
| 638 | // double -> _Complex double |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 639 | rhsExpr = ImpCastExprToType(rhsExpr.take(), result, CK_FloatingRealToComplex); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 640 | |
| 641 | // _Complex float -> _Complex double |
| 642 | if (!isCompAssign && order < 0) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 643 | lhsExpr = ImpCastExprToType(lhsExpr.take(), result, CK_FloatingComplexCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 644 | |
| 645 | return result; |
| 646 | } |
| 647 | |
| 648 | // Just the RHS is complex, so the LHS needs to be converted |
| 649 | // and the RHS might need to be promoted. |
| 650 | assert(RHSComplexFloat); |
| 651 | |
| 652 | if (order < 0) { // RHS is wider |
| 653 | // float -> _Complex double |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 654 | if (!isCompAssign) { |
Argyrios Kyrtzidis | e84389b | 2011-01-18 18:49:33 +0000 | [diff] [blame] | 655 | QualType fp = cast<ComplexType>(rhs)->getElementType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 656 | lhsExpr = ImpCastExprToType(lhsExpr.take(), fp, CK_FloatingCast); |
| 657 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_FloatingRealToComplex); |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 658 | } |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 659 | return rhs; |
| 660 | } |
| 661 | |
| 662 | // LHS is at least as wide. Find its corresponding complex type. |
| 663 | QualType result = (order == 0 ? rhs : Context.getComplexType(lhs)); |
| 664 | |
| 665 | // double -> _Complex double |
| 666 | if (!isCompAssign) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 667 | lhsExpr = ImpCastExprToType(lhsExpr.take(), result, CK_FloatingRealToComplex); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 668 | |
| 669 | // _Complex float -> _Complex double |
| 670 | if (order > 0) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 671 | rhsExpr = ImpCastExprToType(rhsExpr.take(), result, CK_FloatingComplexCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 672 | |
| 673 | return result; |
| 674 | } |
| 675 | |
| 676 | // Now handle "real" floating types (i.e. float, double, long double). |
| 677 | bool LHSFloat = lhs->isRealFloatingType(); |
| 678 | bool RHSFloat = rhs->isRealFloatingType(); |
| 679 | if (LHSFloat || RHSFloat) { |
| 680 | // If we have two real floating types, convert the smaller operand |
| 681 | // to the bigger result. |
| 682 | if (LHSFloat && RHSFloat) { |
| 683 | int order = Context.getFloatingTypeOrder(lhs, rhs); |
| 684 | if (order > 0) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 685 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_FloatingCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 686 | return lhs; |
| 687 | } |
| 688 | |
| 689 | assert(order < 0 && "illegal float comparison"); |
| 690 | if (!isCompAssign) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 691 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_FloatingCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 692 | return rhs; |
| 693 | } |
| 694 | |
| 695 | // If we have an integer operand, the result is the real floating type. |
| 696 | if (LHSFloat) { |
| 697 | if (rhs->isIntegerType()) { |
| 698 | // Convert rhs to the lhs floating point type. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 699 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralToFloating); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 700 | return lhs; |
| 701 | } |
| 702 | |
| 703 | // Convert both sides to the appropriate complex float. |
| 704 | assert(rhs->isComplexIntegerType()); |
| 705 | QualType result = Context.getComplexType(lhs); |
| 706 | |
| 707 | // _Complex int -> _Complex float |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 708 | rhsExpr = ImpCastExprToType(rhsExpr.take(), result, CK_IntegralComplexToFloatingComplex); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 709 | |
| 710 | // float -> _Complex float |
| 711 | if (!isCompAssign) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 712 | lhsExpr = ImpCastExprToType(lhsExpr.take(), result, CK_FloatingRealToComplex); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 713 | |
| 714 | return result; |
| 715 | } |
| 716 | |
| 717 | assert(RHSFloat); |
| 718 | if (lhs->isIntegerType()) { |
| 719 | // Convert lhs to the rhs floating point type. |
| 720 | if (!isCompAssign) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 721 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralToFloating); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 722 | return rhs; |
| 723 | } |
| 724 | |
| 725 | // Convert both sides to the appropriate complex float. |
| 726 | assert(lhs->isComplexIntegerType()); |
| 727 | QualType result = Context.getComplexType(rhs); |
| 728 | |
| 729 | // _Complex int -> _Complex float |
| 730 | if (!isCompAssign) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 731 | lhsExpr = ImpCastExprToType(lhsExpr.take(), result, CK_IntegralComplexToFloatingComplex); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 732 | |
| 733 | // float -> _Complex float |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 734 | rhsExpr = ImpCastExprToType(rhsExpr.take(), result, CK_FloatingRealToComplex); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 735 | |
| 736 | return result; |
| 737 | } |
| 738 | |
| 739 | // Handle GCC complex int extension. |
| 740 | // FIXME: if the operands are (int, _Complex long), we currently |
| 741 | // don't promote the complex. Also, signedness? |
| 742 | const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType(); |
| 743 | const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType(); |
| 744 | if (lhsComplexInt && rhsComplexInt) { |
| 745 | int order = Context.getIntegerTypeOrder(lhsComplexInt->getElementType(), |
| 746 | rhsComplexInt->getElementType()); |
| 747 | assert(order && "inequal types with equal element ordering"); |
| 748 | if (order > 0) { |
| 749 | // _Complex int -> _Complex long |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 750 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralComplexCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 751 | return lhs; |
| 752 | } |
| 753 | |
| 754 | if (!isCompAssign) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 755 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralComplexCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 756 | return rhs; |
| 757 | } else if (lhsComplexInt) { |
| 758 | // int -> _Complex int |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 759 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralRealToComplex); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 760 | return lhs; |
| 761 | } else if (rhsComplexInt) { |
| 762 | // int -> _Complex int |
| 763 | if (!isCompAssign) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 764 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralRealToComplex); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 765 | return rhs; |
| 766 | } |
| 767 | |
| 768 | // Finally, we have two differing integer types. |
| 769 | // The rules for this case are in C99 6.3.1.8 |
| 770 | int compare = Context.getIntegerTypeOrder(lhs, rhs); |
| 771 | bool lhsSigned = lhs->hasSignedIntegerRepresentation(), |
| 772 | rhsSigned = rhs->hasSignedIntegerRepresentation(); |
| 773 | if (lhsSigned == rhsSigned) { |
| 774 | // Same signedness; use the higher-ranked type |
| 775 | if (compare >= 0) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 776 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 777 | return lhs; |
| 778 | } else if (!isCompAssign) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 779 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 780 | return rhs; |
| 781 | } else if (compare != (lhsSigned ? 1 : -1)) { |
| 782 | // The unsigned type has greater than or equal rank to the |
| 783 | // signed type, so use the unsigned type |
| 784 | if (rhsSigned) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 785 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 786 | return lhs; |
| 787 | } else if (!isCompAssign) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 788 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 789 | return rhs; |
| 790 | } else if (Context.getIntWidth(lhs) != Context.getIntWidth(rhs)) { |
| 791 | // The two types are different widths; if we are here, that |
| 792 | // means the signed type is larger than the unsigned type, so |
| 793 | // use the signed type. |
| 794 | if (lhsSigned) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 795 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 796 | return lhs; |
| 797 | } else if (!isCompAssign) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 798 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 799 | return rhs; |
| 800 | } else { |
| 801 | // The signed type is higher-ranked than the unsigned type, |
| 802 | // but isn't actually any bigger (like unsigned int and long |
| 803 | // on most 32-bit systems). Use the unsigned type corresponding |
| 804 | // to the signed type. |
| 805 | QualType result = |
| 806 | Context.getCorrespondingUnsignedType(lhsSigned ? lhs : rhs); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 807 | rhsExpr = ImpCastExprToType(rhsExpr.take(), result, CK_IntegralCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 808 | if (!isCompAssign) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 809 | lhsExpr = ImpCastExprToType(lhsExpr.take(), result, CK_IntegralCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 810 | return result; |
| 811 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 812 | } |
| 813 | |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 814 | //===----------------------------------------------------------------------===// |
| 815 | // Semantic Analysis for various Expression Types |
| 816 | //===----------------------------------------------------------------------===// |
| 817 | |
| 818 | |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 819 | ExprResult |
| 820 | Sema::ActOnGenericSelectionExpr(SourceLocation KeyLoc, |
| 821 | SourceLocation DefaultLoc, |
| 822 | SourceLocation RParenLoc, |
| 823 | Expr *ControllingExpr, |
| 824 | MultiTypeArg types, |
| 825 | MultiExprArg exprs) { |
| 826 | unsigned NumAssocs = types.size(); |
| 827 | assert(NumAssocs == exprs.size()); |
| 828 | |
| 829 | ParsedType *ParsedTypes = types.release(); |
| 830 | Expr **Exprs = exprs.release(); |
| 831 | |
| 832 | TypeSourceInfo **Types = new TypeSourceInfo*[NumAssocs]; |
| 833 | for (unsigned i = 0; i < NumAssocs; ++i) { |
| 834 | if (ParsedTypes[i]) |
| 835 | (void) GetTypeFromParser(ParsedTypes[i], &Types[i]); |
| 836 | else |
| 837 | Types[i] = 0; |
| 838 | } |
| 839 | |
| 840 | ExprResult ER = CreateGenericSelectionExpr(KeyLoc, DefaultLoc, RParenLoc, |
| 841 | ControllingExpr, Types, Exprs, |
| 842 | NumAssocs); |
Benjamin Kramer | 3462376 | 2011-04-15 11:21:57 +0000 | [diff] [blame] | 843 | delete [] Types; |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 844 | return ER; |
| 845 | } |
| 846 | |
| 847 | ExprResult |
| 848 | Sema::CreateGenericSelectionExpr(SourceLocation KeyLoc, |
| 849 | SourceLocation DefaultLoc, |
| 850 | SourceLocation RParenLoc, |
| 851 | Expr *ControllingExpr, |
| 852 | TypeSourceInfo **Types, |
| 853 | Expr **Exprs, |
| 854 | unsigned NumAssocs) { |
| 855 | bool TypeErrorFound = false, |
| 856 | IsResultDependent = ControllingExpr->isTypeDependent(), |
| 857 | ContainsUnexpandedParameterPack |
| 858 | = ControllingExpr->containsUnexpandedParameterPack(); |
| 859 | |
| 860 | for (unsigned i = 0; i < NumAssocs; ++i) { |
| 861 | if (Exprs[i]->containsUnexpandedParameterPack()) |
| 862 | ContainsUnexpandedParameterPack = true; |
| 863 | |
| 864 | if (Types[i]) { |
| 865 | if (Types[i]->getType()->containsUnexpandedParameterPack()) |
| 866 | ContainsUnexpandedParameterPack = true; |
| 867 | |
| 868 | if (Types[i]->getType()->isDependentType()) { |
| 869 | IsResultDependent = true; |
| 870 | } else { |
| 871 | // C1X 6.5.1.1p2 "The type name in a generic association shall specify a |
| 872 | // complete object type other than a variably modified type." |
| 873 | unsigned D = 0; |
| 874 | if (Types[i]->getType()->isIncompleteType()) |
| 875 | D = diag::err_assoc_type_incomplete; |
| 876 | else if (!Types[i]->getType()->isObjectType()) |
| 877 | D = diag::err_assoc_type_nonobject; |
| 878 | else if (Types[i]->getType()->isVariablyModifiedType()) |
| 879 | D = diag::err_assoc_type_variably_modified; |
| 880 | |
| 881 | if (D != 0) { |
| 882 | Diag(Types[i]->getTypeLoc().getBeginLoc(), D) |
| 883 | << Types[i]->getTypeLoc().getSourceRange() |
| 884 | << Types[i]->getType(); |
| 885 | TypeErrorFound = true; |
| 886 | } |
| 887 | |
| 888 | // C1X 6.5.1.1p2 "No two generic associations in the same generic |
| 889 | // selection shall specify compatible types." |
| 890 | for (unsigned j = i+1; j < NumAssocs; ++j) |
| 891 | if (Types[j] && !Types[j]->getType()->isDependentType() && |
| 892 | Context.typesAreCompatible(Types[i]->getType(), |
| 893 | Types[j]->getType())) { |
| 894 | Diag(Types[j]->getTypeLoc().getBeginLoc(), |
| 895 | diag::err_assoc_compatible_types) |
| 896 | << Types[j]->getTypeLoc().getSourceRange() |
| 897 | << Types[j]->getType() |
| 898 | << Types[i]->getType(); |
| 899 | Diag(Types[i]->getTypeLoc().getBeginLoc(), |
| 900 | diag::note_compat_assoc) |
| 901 | << Types[i]->getTypeLoc().getSourceRange() |
| 902 | << Types[i]->getType(); |
| 903 | TypeErrorFound = true; |
| 904 | } |
| 905 | } |
| 906 | } |
| 907 | } |
| 908 | if (TypeErrorFound) |
| 909 | return ExprError(); |
| 910 | |
| 911 | // If we determined that the generic selection is result-dependent, don't |
| 912 | // try to compute the result expression. |
| 913 | if (IsResultDependent) |
| 914 | return Owned(new (Context) GenericSelectionExpr( |
| 915 | Context, KeyLoc, ControllingExpr, |
| 916 | Types, Exprs, NumAssocs, DefaultLoc, |
| 917 | RParenLoc, ContainsUnexpandedParameterPack)); |
| 918 | |
| 919 | llvm::SmallVector<unsigned, 1> CompatIndices; |
| 920 | unsigned DefaultIndex = -1U; |
| 921 | for (unsigned i = 0; i < NumAssocs; ++i) { |
| 922 | if (!Types[i]) |
| 923 | DefaultIndex = i; |
| 924 | else if (Context.typesAreCompatible(ControllingExpr->getType(), |
| 925 | Types[i]->getType())) |
| 926 | CompatIndices.push_back(i); |
| 927 | } |
| 928 | |
| 929 | // C1X 6.5.1.1p2 "The controlling expression of a generic selection shall have |
| 930 | // type compatible with at most one of the types named in its generic |
| 931 | // association list." |
| 932 | if (CompatIndices.size() > 1) { |
| 933 | // We strip parens here because the controlling expression is typically |
| 934 | // parenthesized in macro definitions. |
| 935 | ControllingExpr = ControllingExpr->IgnoreParens(); |
| 936 | Diag(ControllingExpr->getLocStart(), diag::err_generic_sel_multi_match) |
| 937 | << ControllingExpr->getSourceRange() << ControllingExpr->getType() |
| 938 | << (unsigned) CompatIndices.size(); |
| 939 | for (llvm::SmallVector<unsigned, 1>::iterator I = CompatIndices.begin(), |
| 940 | E = CompatIndices.end(); I != E; ++I) { |
| 941 | Diag(Types[*I]->getTypeLoc().getBeginLoc(), |
| 942 | diag::note_compat_assoc) |
| 943 | << Types[*I]->getTypeLoc().getSourceRange() |
| 944 | << Types[*I]->getType(); |
| 945 | } |
| 946 | return ExprError(); |
| 947 | } |
| 948 | |
| 949 | // C1X 6.5.1.1p2 "If a generic selection has no default generic association, |
| 950 | // its controlling expression shall have type compatible with exactly one of |
| 951 | // the types named in its generic association list." |
| 952 | if (DefaultIndex == -1U && CompatIndices.size() == 0) { |
| 953 | // We strip parens here because the controlling expression is typically |
| 954 | // parenthesized in macro definitions. |
| 955 | ControllingExpr = ControllingExpr->IgnoreParens(); |
| 956 | Diag(ControllingExpr->getLocStart(), diag::err_generic_sel_no_match) |
| 957 | << ControllingExpr->getSourceRange() << ControllingExpr->getType(); |
| 958 | return ExprError(); |
| 959 | } |
| 960 | |
| 961 | // C1X 6.5.1.1p3 "If a generic selection has a generic association with a |
| 962 | // type name that is compatible with the type of the controlling expression, |
| 963 | // then the result expression of the generic selection is the expression |
| 964 | // in that generic association. Otherwise, the result expression of the |
| 965 | // generic selection is the expression in the default generic association." |
| 966 | unsigned ResultIndex = |
| 967 | CompatIndices.size() ? CompatIndices[0] : DefaultIndex; |
| 968 | |
| 969 | return Owned(new (Context) GenericSelectionExpr( |
| 970 | Context, KeyLoc, ControllingExpr, |
| 971 | Types, Exprs, NumAssocs, DefaultLoc, |
| 972 | RParenLoc, ContainsUnexpandedParameterPack, |
| 973 | ResultIndex)); |
| 974 | } |
| 975 | |
Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 976 | /// ActOnStringLiteral - The specified tokens were lexed as pasted string |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 977 | /// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string |
| 978 | /// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from |
| 979 | /// multiple tokens. However, the common case is that StringToks points to one |
| 980 | /// string. |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 981 | /// |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 982 | ExprResult |
Alexis Hunt | 3b79186 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 983 | Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) { |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 984 | assert(NumStringToks && "Must have at least one string!"); |
| 985 | |
Chris Lattner | 8a24e58 | 2009-01-16 18:51:42 +0000 | [diff] [blame] | 986 | StringLiteralParser Literal(StringToks, NumStringToks, PP); |
Steve Naroff | 4f88b31 | 2007-03-13 22:37:02 +0000 | [diff] [blame] | 987 | if (Literal.hadError) |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 988 | return ExprError(); |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 989 | |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 990 | llvm::SmallVector<SourceLocation, 4> StringTokLocs; |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 991 | for (unsigned i = 0; i != NumStringToks; ++i) |
| 992 | StringTokLocs.push_back(StringToks[i].getLocation()); |
Chris Lattner | 36fc879 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 993 | |
Chris Lattner | 36fc879 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 994 | QualType StrTy = Context.CharTy; |
Anders Carlsson | 6b06e18 | 2011-04-06 18:42:48 +0000 | [diff] [blame] | 995 | if (Literal.AnyWide) |
| 996 | StrTy = Context.getWCharType(); |
| 997 | else if (Literal.Pascal) |
| 998 | StrTy = Context.UnsignedCharTy; |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 999 | |
| 1000 | // 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] | 1001 | if (getLangOptions().CPlusPlus || getLangOptions().ConstStrings) |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 1002 | StrTy.addConst(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1003 | |
Chris Lattner | 36fc879 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 1004 | // Get an array type for the string, according to C99 6.4.5. This includes |
| 1005 | // the nul terminator character as well as the string length for pascal |
| 1006 | // strings. |
| 1007 | StrTy = Context.getConstantArrayType(StrTy, |
Chris Lattner | d42c29f | 2009-02-26 23:01:51 +0000 | [diff] [blame] | 1008 | llvm::APInt(32, Literal.GetNumStringChars()+1), |
Chris Lattner | 36fc879 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 1009 | ArrayType::Normal, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1010 | |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 1011 | // Pass &StringTokLocs[0], StringTokLocs.size() to factory! |
Alexis Hunt | 3b79186 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 1012 | return Owned(StringLiteral::Create(Context, Literal.GetString(), |
| 1013 | Literal.GetStringLength(), |
Anders Carlsson | 7524540 | 2011-04-14 00:40:03 +0000 | [diff] [blame] | 1014 | Literal.AnyWide, Literal.Pascal, StrTy, |
Alexis Hunt | 3b79186 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 1015 | &StringTokLocs[0], |
| 1016 | StringTokLocs.size())); |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 1017 | } |
| 1018 | |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1019 | enum CaptureResult { |
| 1020 | /// No capture is required. |
| 1021 | CR_NoCapture, |
| 1022 | |
| 1023 | /// A capture is required. |
| 1024 | CR_Capture, |
| 1025 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1026 | /// A by-ref capture is required. |
| 1027 | CR_CaptureByRef, |
| 1028 | |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1029 | /// An error occurred when trying to capture the given variable. |
| 1030 | CR_Error |
| 1031 | }; |
| 1032 | |
| 1033 | /// Diagnose an uncapturable value reference. |
Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1034 | /// |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1035 | /// \param var - the variable referenced |
| 1036 | /// \param DC - the context which we couldn't capture through |
| 1037 | static CaptureResult |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1038 | diagnoseUncapturableValueReference(Sema &S, SourceLocation loc, |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1039 | VarDecl *var, DeclContext *DC) { |
| 1040 | switch (S.ExprEvalContexts.back().Context) { |
| 1041 | case Sema::Unevaluated: |
| 1042 | // The argument will never be evaluated, so don't complain. |
| 1043 | return CR_NoCapture; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1044 | |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1045 | case Sema::PotentiallyEvaluated: |
| 1046 | case Sema::PotentiallyEvaluatedIfUsed: |
| 1047 | break; |
Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1048 | |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1049 | case Sema::PotentiallyPotentiallyEvaluated: |
| 1050 | // FIXME: delay these! |
| 1051 | break; |
Chris Lattner | 497d7b0 | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 1052 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1053 | |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1054 | // Don't diagnose about capture if we're not actually in code right |
| 1055 | // now; in general, there are more appropriate places that will |
| 1056 | // diagnose this. |
| 1057 | if (!S.CurContext->isFunctionOrMethod()) return CR_NoCapture; |
| 1058 | |
John McCall | 92d627e | 2011-03-22 23:15:50 +0000 | [diff] [blame] | 1059 | // Certain madnesses can happen with parameter declarations, which |
| 1060 | // we want to ignore. |
| 1061 | if (isa<ParmVarDecl>(var)) { |
| 1062 | // - If the parameter still belongs to the translation unit, then |
| 1063 | // we're actually just using one parameter in the declaration of |
| 1064 | // the next. This is useful in e.g. VLAs. |
| 1065 | if (isa<TranslationUnitDecl>(var->getDeclContext())) |
| 1066 | return CR_NoCapture; |
| 1067 | |
| 1068 | // - This particular madness can happen in ill-formed default |
| 1069 | // arguments; claim it's okay and let downstream code handle it. |
| 1070 | if (S.CurContext == var->getDeclContext()->getParent()) |
| 1071 | return CR_NoCapture; |
| 1072 | } |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1073 | |
| 1074 | DeclarationName functionName; |
| 1075 | if (FunctionDecl *fn = dyn_cast<FunctionDecl>(var->getDeclContext())) |
| 1076 | functionName = fn->getDeclName(); |
| 1077 | // FIXME: variable from enclosing block that we couldn't capture from! |
| 1078 | |
| 1079 | S.Diag(loc, diag::err_reference_to_local_var_in_enclosing_function) |
| 1080 | << var->getIdentifier() << functionName; |
| 1081 | S.Diag(var->getLocation(), diag::note_local_variable_declared_here) |
| 1082 | << var->getIdentifier(); |
| 1083 | |
| 1084 | return CR_Error; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1085 | } |
| 1086 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1087 | /// There is a well-formed capture at a particular scope level; |
| 1088 | /// propagate it through all the nested blocks. |
| 1089 | static CaptureResult propagateCapture(Sema &S, unsigned validScopeIndex, |
| 1090 | const BlockDecl::Capture &capture) { |
| 1091 | VarDecl *var = capture.getVariable(); |
| 1092 | |
| 1093 | // Update all the inner blocks with the capture information. |
| 1094 | for (unsigned i = validScopeIndex + 1, e = S.FunctionScopes.size(); |
| 1095 | i != e; ++i) { |
| 1096 | BlockScopeInfo *innerBlock = cast<BlockScopeInfo>(S.FunctionScopes[i]); |
| 1097 | innerBlock->Captures.push_back( |
| 1098 | BlockDecl::Capture(capture.getVariable(), capture.isByRef(), |
| 1099 | /*nested*/ true, capture.getCopyExpr())); |
| 1100 | innerBlock->CaptureMap[var] = innerBlock->Captures.size(); // +1 |
| 1101 | } |
| 1102 | |
| 1103 | return capture.isByRef() ? CR_CaptureByRef : CR_Capture; |
| 1104 | } |
| 1105 | |
| 1106 | /// shouldCaptureValueReference - Determine if a reference to the |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1107 | /// given value in the current context requires a variable capture. |
| 1108 | /// |
| 1109 | /// This also keeps the captures set in the BlockScopeInfo records |
| 1110 | /// up-to-date. |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1111 | static CaptureResult shouldCaptureValueReference(Sema &S, SourceLocation loc, |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1112 | ValueDecl *value) { |
| 1113 | // Only variables ever require capture. |
| 1114 | VarDecl *var = dyn_cast<VarDecl>(value); |
John McCall | f4cd4f9 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1115 | if (!var) return CR_NoCapture; |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1116 | |
| 1117 | // Fast path: variables from the current context never require capture. |
| 1118 | DeclContext *DC = S.CurContext; |
| 1119 | if (var->getDeclContext() == DC) return CR_NoCapture; |
| 1120 | |
| 1121 | // Only variables with local storage require capture. |
| 1122 | // FIXME: What about 'const' variables in C++? |
| 1123 | if (!var->hasLocalStorage()) return CR_NoCapture; |
| 1124 | |
| 1125 | // Otherwise, we need to capture. |
| 1126 | |
| 1127 | unsigned functionScopesIndex = S.FunctionScopes.size() - 1; |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1128 | do { |
| 1129 | // Only blocks (and eventually C++0x closures) can capture; other |
| 1130 | // scopes don't work. |
| 1131 | if (!isa<BlockDecl>(DC)) |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1132 | return diagnoseUncapturableValueReference(S, loc, var, DC); |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1133 | |
| 1134 | BlockScopeInfo *blockScope = |
| 1135 | cast<BlockScopeInfo>(S.FunctionScopes[functionScopesIndex]); |
| 1136 | assert(blockScope->TheDecl == static_cast<BlockDecl*>(DC)); |
| 1137 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1138 | // Check whether we've already captured it in this block. If so, |
| 1139 | // we're done. |
| 1140 | if (unsigned indexPlus1 = blockScope->CaptureMap[var]) |
| 1141 | return propagateCapture(S, functionScopesIndex, |
| 1142 | blockScope->Captures[indexPlus1 - 1]); |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1143 | |
| 1144 | functionScopesIndex--; |
| 1145 | DC = cast<BlockDecl>(DC)->getDeclContext(); |
| 1146 | } while (var->getDeclContext() != DC); |
| 1147 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1148 | // Okay, we descended all the way to the block that defines the variable. |
| 1149 | // Actually try to capture it. |
| 1150 | QualType type = var->getType(); |
| 1151 | |
| 1152 | // Prohibit variably-modified types. |
| 1153 | if (type->isVariablyModifiedType()) { |
| 1154 | S.Diag(loc, diag::err_ref_vm_type); |
| 1155 | S.Diag(var->getLocation(), diag::note_declared_at); |
| 1156 | return CR_Error; |
| 1157 | } |
| 1158 | |
| 1159 | // Prohibit arrays, even in __block variables, but not references to |
| 1160 | // them. |
| 1161 | if (type->isArrayType()) { |
| 1162 | S.Diag(loc, diag::err_ref_array_type); |
| 1163 | S.Diag(var->getLocation(), diag::note_declared_at); |
| 1164 | return CR_Error; |
| 1165 | } |
| 1166 | |
| 1167 | S.MarkDeclarationReferenced(loc, var); |
| 1168 | |
| 1169 | // The BlocksAttr indicates the variable is bound by-reference. |
| 1170 | bool byRef = var->hasAttr<BlocksAttr>(); |
| 1171 | |
| 1172 | // Build a copy expression. |
| 1173 | Expr *copyExpr = 0; |
John McCall | a85af56 | 2011-04-28 02:15:35 +0000 | [diff] [blame] | 1174 | const RecordType *rtype; |
| 1175 | if (!byRef && S.getLangOptions().CPlusPlus && !type->isDependentType() && |
| 1176 | (rtype = type->getAs<RecordType>())) { |
| 1177 | |
| 1178 | // The capture logic needs the destructor, so make sure we mark it. |
| 1179 | // Usually this is unnecessary because most local variables have |
| 1180 | // their destructors marked at declaration time, but parameters are |
| 1181 | // an exception because it's technically only the call site that |
| 1182 | // actually requires the destructor. |
| 1183 | if (isa<ParmVarDecl>(var)) |
| 1184 | S.FinalizeVarWithDestructor(var, rtype); |
| 1185 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1186 | // According to the blocks spec, the capture of a variable from |
| 1187 | // the stack requires a const copy constructor. This is not true |
| 1188 | // of the copy/move done to move a __block variable to the heap. |
| 1189 | type.addConst(); |
| 1190 | |
| 1191 | Expr *declRef = new (S.Context) DeclRefExpr(var, type, VK_LValue, loc); |
| 1192 | ExprResult result = |
| 1193 | S.PerformCopyInitialization( |
| 1194 | InitializedEntity::InitializeBlock(var->getLocation(), |
| 1195 | type, false), |
| 1196 | loc, S.Owned(declRef)); |
| 1197 | |
| 1198 | // Build a full-expression copy expression if initialization |
| 1199 | // succeeded and used a non-trivial constructor. Recover from |
| 1200 | // errors by pretending that the copy isn't necessary. |
| 1201 | if (!result.isInvalid() && |
| 1202 | !cast<CXXConstructExpr>(result.get())->getConstructor()->isTrivial()) { |
| 1203 | result = S.MaybeCreateExprWithCleanups(result); |
| 1204 | copyExpr = result.take(); |
| 1205 | } |
| 1206 | } |
| 1207 | |
| 1208 | // We're currently at the declarer; go back to the closure. |
| 1209 | functionScopesIndex++; |
| 1210 | BlockScopeInfo *blockScope = |
| 1211 | cast<BlockScopeInfo>(S.FunctionScopes[functionScopesIndex]); |
| 1212 | |
| 1213 | // Build a valid capture in this scope. |
| 1214 | blockScope->Captures.push_back( |
| 1215 | BlockDecl::Capture(var, byRef, /*nested*/ false, copyExpr)); |
| 1216 | blockScope->CaptureMap[var] = blockScope->Captures.size(); // +1 |
| 1217 | |
| 1218 | // Propagate that to inner captures if necessary. |
| 1219 | return propagateCapture(S, functionScopesIndex, |
| 1220 | blockScope->Captures.back()); |
| 1221 | } |
| 1222 | |
| 1223 | static ExprResult BuildBlockDeclRefExpr(Sema &S, ValueDecl *vd, |
| 1224 | const DeclarationNameInfo &NameInfo, |
| 1225 | bool byRef) { |
| 1226 | assert(isa<VarDecl>(vd) && "capturing non-variable"); |
| 1227 | |
| 1228 | VarDecl *var = cast<VarDecl>(vd); |
| 1229 | assert(var->hasLocalStorage() && "capturing non-local"); |
| 1230 | assert(byRef == var->hasAttr<BlocksAttr>() && "byref set wrong"); |
| 1231 | |
| 1232 | QualType exprType = var->getType().getNonReferenceType(); |
| 1233 | |
| 1234 | BlockDeclRefExpr *BDRE; |
| 1235 | if (!byRef) { |
| 1236 | // The variable will be bound by copy; make it const within the |
| 1237 | // closure, but record that this was done in the expression. |
| 1238 | bool constAdded = !exprType.isConstQualified(); |
| 1239 | exprType.addConst(); |
| 1240 | |
| 1241 | BDRE = new (S.Context) BlockDeclRefExpr(var, exprType, VK_LValue, |
| 1242 | NameInfo.getLoc(), false, |
| 1243 | constAdded); |
| 1244 | } else { |
| 1245 | BDRE = new (S.Context) BlockDeclRefExpr(var, exprType, VK_LValue, |
| 1246 | NameInfo.getLoc(), true); |
| 1247 | } |
| 1248 | |
| 1249 | return S.Owned(BDRE); |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1250 | } |
Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1251 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1252 | ExprResult |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1253 | Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, |
John McCall | f4cd4f9 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1254 | SourceLocation Loc, |
| 1255 | const CXXScopeSpec *SS) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1256 | DeclarationNameInfo NameInfo(D->getDeclName(), Loc); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1257 | return BuildDeclRefExpr(D, Ty, VK, NameInfo, SS); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1258 | } |
| 1259 | |
John McCall | f4cd4f9 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1260 | /// BuildDeclRefExpr - Build an expression that references a |
| 1261 | /// declaration that does not require a closure capture. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1262 | ExprResult |
John McCall | f4cd4f9 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1263 | Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1264 | const DeclarationNameInfo &NameInfo, |
| 1265 | const CXXScopeSpec *SS) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1266 | MarkDeclarationReferenced(NameInfo.getLoc(), D); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1267 | |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1268 | Expr *E = DeclRefExpr::Create(Context, |
Douglas Gregor | ea972d3 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 1269 | SS? SS->getWithLocInContext(Context) |
| 1270 | : NestedNameSpecifierLoc(), |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1271 | D, NameInfo, Ty, VK); |
| 1272 | |
| 1273 | // Just in case we're building an illegal pointer-to-member. |
| 1274 | if (isa<FieldDecl>(D) && cast<FieldDecl>(D)->getBitWidth()) |
| 1275 | E->setObjectKind(OK_BitField); |
| 1276 | |
| 1277 | return Owned(E); |
Douglas Gregor | c7acfdf | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 1278 | } |
| 1279 | |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 1280 | static ExprResult |
| 1281 | BuildFieldReferenceExpr(Sema &S, Expr *BaseExpr, bool IsArrow, |
| 1282 | const CXXScopeSpec &SS, FieldDecl *Field, |
| 1283 | DeclAccessPair FoundDecl, |
| 1284 | const DeclarationNameInfo &MemberNameInfo); |
| 1285 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1286 | ExprResult |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1287 | Sema::BuildAnonymousStructUnionMemberReference(const CXXScopeSpec &SS, |
| 1288 | SourceLocation loc, |
| 1289 | IndirectFieldDecl *indirectField, |
| 1290 | Expr *baseObjectExpr, |
| 1291 | SourceLocation opLoc) { |
| 1292 | // First, build the expression that refers to the base object. |
| 1293 | |
| 1294 | bool baseObjectIsPointer = false; |
| 1295 | Qualifiers baseQuals; |
| 1296 | |
| 1297 | // Case 1: the base of the indirect field is not a field. |
| 1298 | VarDecl *baseVariable = indirectField->getVarDecl(); |
Douglas Gregor | e10f36d | 2011-02-18 02:44:58 +0000 | [diff] [blame] | 1299 | CXXScopeSpec EmptySS; |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1300 | if (baseVariable) { |
| 1301 | assert(baseVariable->getType()->isRecordType()); |
| 1302 | |
| 1303 | // In principle we could have a member access expression that |
| 1304 | // accesses an anonymous struct/union that's a static member of |
| 1305 | // the base object's class. However, under the current standard, |
| 1306 | // static data members cannot be anonymous structs or unions. |
| 1307 | // Supporting this is as easy as building a MemberExpr here. |
| 1308 | assert(!baseObjectExpr && "anonymous struct/union is static data member?"); |
| 1309 | |
| 1310 | DeclarationNameInfo baseNameInfo(DeclarationName(), loc); |
| 1311 | |
| 1312 | ExprResult result = |
Douglas Gregor | e10f36d | 2011-02-18 02:44:58 +0000 | [diff] [blame] | 1313 | BuildDeclarationNameExpr(EmptySS, baseNameInfo, baseVariable); |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1314 | if (result.isInvalid()) return ExprError(); |
| 1315 | |
| 1316 | baseObjectExpr = result.take(); |
| 1317 | baseObjectIsPointer = false; |
| 1318 | baseQuals = baseObjectExpr->getType().getQualifiers(); |
| 1319 | |
| 1320 | // Case 2: the base of the indirect field is a field and the user |
| 1321 | // wrote a member expression. |
| 1322 | } else if (baseObjectExpr) { |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1323 | // The caller provided the base object expression. Determine |
| 1324 | // whether its a pointer and whether it adds any qualifiers to the |
| 1325 | // anonymous struct/union fields we're looking into. |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1326 | QualType objectType = baseObjectExpr->getType(); |
| 1327 | |
| 1328 | if (const PointerType *ptr = objectType->getAs<PointerType>()) { |
| 1329 | baseObjectIsPointer = true; |
| 1330 | objectType = ptr->getPointeeType(); |
| 1331 | } else { |
| 1332 | baseObjectIsPointer = false; |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1333 | } |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1334 | baseQuals = objectType.getQualifiers(); |
| 1335 | |
| 1336 | // Case 3: the base of the indirect field is a field and we should |
| 1337 | // build an implicit member access. |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1338 | } else { |
| 1339 | // We've found a member of an anonymous struct/union that is |
| 1340 | // inside a non-anonymous struct/union, so in a well-formed |
| 1341 | // program our base object expression is "this". |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1342 | QualType ThisTy = getAndCaptureCurrentThisType(); |
| 1343 | if (ThisTy.isNull()) { |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1344 | Diag(loc, diag::err_invalid_member_use_in_static_method) |
| 1345 | << indirectField->getDeclName(); |
| 1346 | return ExprError(); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1347 | } |
| 1348 | |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1349 | // Our base object expression is "this". |
| 1350 | baseObjectExpr = |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1351 | new (Context) CXXThisExpr(loc, ThisTy, /*isImplicit=*/ true); |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1352 | baseObjectIsPointer = true; |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1353 | baseQuals = ThisTy->castAs<PointerType>()->getPointeeType().getQualifiers(); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1354 | } |
| 1355 | |
| 1356 | // Build the implicit member references to the field of the |
| 1357 | // anonymous struct/union. |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1358 | Expr *result = baseObjectExpr; |
| 1359 | IndirectFieldDecl::chain_iterator |
| 1360 | FI = indirectField->chain_begin(), FEnd = indirectField->chain_end(); |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 1361 | |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1362 | // Build the first member access in the chain with full information. |
| 1363 | if (!baseVariable) { |
| 1364 | FieldDecl *field = cast<FieldDecl>(*FI); |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 1365 | |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1366 | // FIXME: use the real found-decl info! |
| 1367 | DeclAccessPair foundDecl = DeclAccessPair::make(field, field->getAccess()); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1368 | |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1369 | // Make a nameInfo that properly uses the anonymous name. |
| 1370 | DeclarationNameInfo memberNameInfo(field->getDeclName(), loc); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1371 | |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1372 | result = BuildFieldReferenceExpr(*this, result, baseObjectIsPointer, |
Douglas Gregor | e10f36d | 2011-02-18 02:44:58 +0000 | [diff] [blame] | 1373 | EmptySS, field, foundDecl, |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1374 | memberNameInfo).take(); |
| 1375 | baseObjectIsPointer = false; |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 1376 | |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1377 | // FIXME: check qualified member access |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1378 | } |
| 1379 | |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1380 | // In all cases, we should now skip the first declaration in the chain. |
| 1381 | ++FI; |
| 1382 | |
Douglas Gregor | e10f36d | 2011-02-18 02:44:58 +0000 | [diff] [blame] | 1383 | while (FI != FEnd) { |
| 1384 | FieldDecl *field = cast<FieldDecl>(*FI++); |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1385 | |
| 1386 | // FIXME: these are somewhat meaningless |
| 1387 | DeclarationNameInfo memberNameInfo(field->getDeclName(), loc); |
| 1388 | DeclAccessPair foundDecl = DeclAccessPair::make(field, field->getAccess()); |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1389 | |
| 1390 | result = BuildFieldReferenceExpr(*this, result, /*isarrow*/ false, |
Douglas Gregor | e10f36d | 2011-02-18 02:44:58 +0000 | [diff] [blame] | 1391 | (FI == FEnd? SS : EmptySS), field, |
| 1392 | foundDecl, memberNameInfo) |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1393 | .take(); |
| 1394 | } |
| 1395 | |
| 1396 | return Owned(result); |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 1397 | } |
| 1398 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1399 | /// Decomposes the given name into a DeclarationNameInfo, its location, and |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1400 | /// possibly a list of template arguments. |
| 1401 | /// |
| 1402 | /// If this produces template arguments, it is permitted to call |
| 1403 | /// DecomposeTemplateName. |
| 1404 | /// |
| 1405 | /// This actually loses a lot of source location information for |
| 1406 | /// non-standard name kinds; we should consider preserving that in |
| 1407 | /// some way. |
| 1408 | static void DecomposeUnqualifiedId(Sema &SemaRef, |
| 1409 | const UnqualifiedId &Id, |
| 1410 | TemplateArgumentListInfo &Buffer, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1411 | DeclarationNameInfo &NameInfo, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1412 | const TemplateArgumentListInfo *&TemplateArgs) { |
| 1413 | if (Id.getKind() == UnqualifiedId::IK_TemplateId) { |
| 1414 | Buffer.setLAngleLoc(Id.TemplateId->LAngleLoc); |
| 1415 | Buffer.setRAngleLoc(Id.TemplateId->RAngleLoc); |
| 1416 | |
| 1417 | ASTTemplateArgsPtr TemplateArgsPtr(SemaRef, |
| 1418 | Id.TemplateId->getTemplateArgs(), |
| 1419 | Id.TemplateId->NumArgs); |
| 1420 | SemaRef.translateTemplateArguments(TemplateArgsPtr, Buffer); |
| 1421 | TemplateArgsPtr.release(); |
| 1422 | |
John McCall | 3e56fd4 | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 1423 | TemplateName TName = Id.TemplateId->Template.get(); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1424 | SourceLocation TNameLoc = Id.TemplateId->TemplateNameLoc; |
| 1425 | NameInfo = SemaRef.Context.getNameForTemplate(TName, TNameLoc); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1426 | TemplateArgs = &Buffer; |
| 1427 | } else { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1428 | NameInfo = SemaRef.GetNameFromUnqualifiedId(Id); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1429 | TemplateArgs = 0; |
| 1430 | } |
| 1431 | } |
| 1432 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1433 | /// Determines if the given class is provably not derived from all of |
| 1434 | /// the prospective base classes. |
| 1435 | static bool IsProvablyNotDerivedFrom(Sema &SemaRef, |
| 1436 | CXXRecordDecl *Record, |
| 1437 | const llvm::SmallPtrSet<CXXRecordDecl*, 4> &Bases) { |
John McCall | a6d407c | 2009-12-01 22:28:41 +0000 | [diff] [blame] | 1438 | if (Bases.count(Record->getCanonicalDecl())) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1439 | return false; |
| 1440 | |
Douglas Gregor | 0a5a221 | 2010-02-11 01:04:33 +0000 | [diff] [blame] | 1441 | RecordDecl *RD = Record->getDefinition(); |
John McCall | a6d407c | 2009-12-01 22:28:41 +0000 | [diff] [blame] | 1442 | if (!RD) return false; |
| 1443 | Record = cast<CXXRecordDecl>(RD); |
| 1444 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1445 | for (CXXRecordDecl::base_class_iterator I = Record->bases_begin(), |
| 1446 | E = Record->bases_end(); I != E; ++I) { |
| 1447 | CanQualType BaseT = SemaRef.Context.getCanonicalType((*I).getType()); |
| 1448 | CanQual<RecordType> BaseRT = BaseT->getAs<RecordType>(); |
| 1449 | if (!BaseRT) return false; |
| 1450 | |
| 1451 | CXXRecordDecl *BaseRecord = cast<CXXRecordDecl>(BaseRT->getDecl()); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1452 | if (!IsProvablyNotDerivedFrom(SemaRef, BaseRecord, Bases)) |
| 1453 | return false; |
| 1454 | } |
| 1455 | |
| 1456 | return true; |
| 1457 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1458 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1459 | enum IMAKind { |
| 1460 | /// The reference is definitely not an instance member access. |
| 1461 | IMA_Static, |
| 1462 | |
| 1463 | /// The reference may be an implicit instance member access. |
| 1464 | IMA_Mixed, |
| 1465 | |
| 1466 | /// The reference may be to an instance member, but it is invalid if |
| 1467 | /// so, because the context is not an instance method. |
| 1468 | IMA_Mixed_StaticContext, |
| 1469 | |
| 1470 | /// The reference may be to an instance member, but it is invalid if |
| 1471 | /// so, because the context is from an unrelated class. |
| 1472 | IMA_Mixed_Unrelated, |
| 1473 | |
| 1474 | /// The reference is definitely an implicit instance member access. |
| 1475 | IMA_Instance, |
| 1476 | |
| 1477 | /// The reference may be to an unresolved using declaration. |
| 1478 | IMA_Unresolved, |
| 1479 | |
| 1480 | /// The reference may be to an unresolved using declaration and the |
| 1481 | /// context is not an instance method. |
| 1482 | IMA_Unresolved_StaticContext, |
| 1483 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1484 | /// All possible referrents are instance members and the current |
| 1485 | /// context is not an instance method. |
| 1486 | IMA_Error_StaticContext, |
| 1487 | |
| 1488 | /// All possible referrents are instance members of an unrelated |
| 1489 | /// class. |
| 1490 | IMA_Error_Unrelated |
| 1491 | }; |
| 1492 | |
| 1493 | /// The given lookup names class member(s) and is not being used for |
| 1494 | /// an address-of-member expression. Classify the type of access |
| 1495 | /// according to whether it's possible that this reference names an |
| 1496 | /// instance member. This is best-effort; it is okay to |
| 1497 | /// conservatively answer "yes", in which case some errors will simply |
| 1498 | /// not be caught until template-instantiation. |
| 1499 | static IMAKind ClassifyImplicitMemberAccess(Sema &SemaRef, |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1500 | Scope *CurScope, |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1501 | const LookupResult &R) { |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1502 | assert(!R.empty() && (*R.begin())->isCXXClassMember()); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1503 | |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1504 | DeclContext *DC = SemaRef.getFunctionLevelDeclContext(); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1505 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1506 | bool isStaticContext = |
John McCall | 87fe5d5 | 2010-05-20 01:18:31 +0000 | [diff] [blame] | 1507 | (!isa<CXXMethodDecl>(DC) || |
| 1508 | cast<CXXMethodDecl>(DC)->isStatic()); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1509 | |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1510 | // C++0x [expr.prim]p4: |
| 1511 | // Otherwise, if a member-declarator declares a non-static data member |
| 1512 | // of a class X, the expression this is a prvalue of type "pointer to X" |
| 1513 | // within the optional brace-or-equal-initializer. |
| 1514 | if (CurScope->getFlags() & Scope::ThisScope) |
| 1515 | isStaticContext = false; |
| 1516 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1517 | if (R.isUnresolvableResult()) |
| 1518 | return isStaticContext ? IMA_Unresolved_StaticContext : IMA_Unresolved; |
| 1519 | |
| 1520 | // Collect all the declaring classes of instance members we find. |
| 1521 | bool hasNonInstance = false; |
Sebastian Redl | 3462031 | 2010-11-26 16:28:07 +0000 | [diff] [blame] | 1522 | bool hasField = false; |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1523 | llvm::SmallPtrSet<CXXRecordDecl*, 4> Classes; |
| 1524 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1525 | NamedDecl *D = *I; |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 1526 | |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 1527 | if (D->isCXXInstanceMember()) { |
Sebastian Redl | 3462031 | 2010-11-26 16:28:07 +0000 | [diff] [blame] | 1528 | if (dyn_cast<FieldDecl>(D)) |
| 1529 | hasField = true; |
| 1530 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1531 | CXXRecordDecl *R = cast<CXXRecordDecl>(D->getDeclContext()); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1532 | Classes.insert(R->getCanonicalDecl()); |
| 1533 | } |
| 1534 | else |
| 1535 | hasNonInstance = true; |
| 1536 | } |
| 1537 | |
| 1538 | // If we didn't find any instance members, it can't be an implicit |
| 1539 | // member reference. |
| 1540 | if (Classes.empty()) |
| 1541 | return IMA_Static; |
| 1542 | |
| 1543 | // If the current context is not an instance method, it can't be |
| 1544 | // an implicit member reference. |
Sebastian Redl | 3462031 | 2010-11-26 16:28:07 +0000 | [diff] [blame] | 1545 | if (isStaticContext) { |
| 1546 | if (hasNonInstance) |
| 1547 | return IMA_Mixed_StaticContext; |
| 1548 | |
| 1549 | if (SemaRef.getLangOptions().CPlusPlus0x && hasField) { |
| 1550 | // C++0x [expr.prim.general]p10: |
| 1551 | // An id-expression that denotes a non-static data member or non-static |
| 1552 | // member function of a class can only be used: |
| 1553 | // (...) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 1554 | // - if that id-expression denotes a non-static data member and it |
| 1555 | // appears in an unevaluated operand. |
| 1556 | const Sema::ExpressionEvaluationContextRecord& record |
| 1557 | = SemaRef.ExprEvalContexts.back(); |
| 1558 | bool isUnevaluatedExpression = (record.Context == Sema::Unevaluated); |
Sebastian Redl | 3462031 | 2010-11-26 16:28:07 +0000 | [diff] [blame] | 1559 | if (isUnevaluatedExpression) |
| 1560 | return IMA_Mixed_StaticContext; |
| 1561 | } |
| 1562 | |
| 1563 | return IMA_Error_StaticContext; |
| 1564 | } |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1565 | |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 1566 | CXXRecordDecl *contextClass; |
| 1567 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(DC)) |
| 1568 | contextClass = MD->getParent()->getCanonicalDecl(); |
| 1569 | else |
| 1570 | contextClass = cast<CXXRecordDecl>(DC); |
Argyrios Kyrtzidis | 36e4ae3 | 2011-04-14 00:46:47 +0000 | [diff] [blame] | 1571 | |
| 1572 | // [class.mfct.non-static]p3: |
| 1573 | // ...is used in the body of a non-static member function of class X, |
| 1574 | // if name lookup (3.4.1) resolves the name in the id-expression to a |
| 1575 | // non-static non-type member of some class C [...] |
| 1576 | // ...if C is not X or a base class of X, the class member access expression |
| 1577 | // is ill-formed. |
| 1578 | if (R.getNamingClass() && |
| 1579 | contextClass != R.getNamingClass()->getCanonicalDecl() && |
| 1580 | contextClass->isProvablyNotDerivedFrom(R.getNamingClass())) |
| 1581 | return (hasNonInstance ? IMA_Mixed_Unrelated : IMA_Error_Unrelated); |
| 1582 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1583 | // If we can prove that the current context is unrelated to all the |
| 1584 | // declaring classes, it can't be an implicit member reference (in |
| 1585 | // 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] | 1586 | if (IsProvablyNotDerivedFrom(SemaRef, contextClass, Classes)) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1587 | return (hasNonInstance ? IMA_Mixed_Unrelated : IMA_Error_Unrelated); |
| 1588 | |
| 1589 | return (hasNonInstance ? IMA_Mixed : IMA_Instance); |
| 1590 | } |
| 1591 | |
| 1592 | /// Diagnose a reference to a field with no object available. |
| 1593 | static void DiagnoseInstanceReference(Sema &SemaRef, |
| 1594 | const CXXScopeSpec &SS, |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1595 | NamedDecl *rep, |
| 1596 | const DeclarationNameInfo &nameInfo) { |
| 1597 | SourceLocation Loc = nameInfo.getLoc(); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1598 | SourceRange Range(Loc); |
| 1599 | if (SS.isSet()) Range.setBegin(SS.getRange().getBegin()); |
| 1600 | |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1601 | if (isa<FieldDecl>(rep) || isa<IndirectFieldDecl>(rep)) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1602 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(SemaRef.CurContext)) { |
| 1603 | if (MD->isStatic()) { |
| 1604 | // "invalid use of member 'x' in static member function" |
| 1605 | SemaRef.Diag(Loc, diag::err_invalid_member_use_in_static_method) |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1606 | << Range << nameInfo.getName(); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1607 | return; |
| 1608 | } |
| 1609 | } |
| 1610 | |
| 1611 | SemaRef.Diag(Loc, diag::err_invalid_non_static_member_use) |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 1612 | << nameInfo.getName() << Range; |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1613 | return; |
| 1614 | } |
| 1615 | |
| 1616 | SemaRef.Diag(Loc, diag::err_member_call_without_object) << Range; |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1617 | } |
| 1618 | |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1619 | /// Diagnose an empty lookup. |
| 1620 | /// |
| 1621 | /// \return false if new lookup candidates were found |
Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1622 | bool Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec &SS, LookupResult &R, |
| 1623 | CorrectTypoContext CTC) { |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1624 | DeclarationName Name = R.getLookupName(); |
| 1625 | |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1626 | unsigned diagnostic = diag::err_undeclared_var_use; |
Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1627 | unsigned diagnostic_suggest = diag::err_undeclared_var_use_suggest; |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1628 | if (Name.getNameKind() == DeclarationName::CXXOperatorName || |
| 1629 | Name.getNameKind() == DeclarationName::CXXLiteralOperatorName || |
Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1630 | Name.getNameKind() == DeclarationName::CXXConversionFunctionName) { |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1631 | diagnostic = diag::err_undeclared_use; |
Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1632 | diagnostic_suggest = diag::err_undeclared_use_suggest; |
| 1633 | } |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1634 | |
Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1635 | // If the original lookup was an unqualified lookup, fake an |
| 1636 | // unqualified lookup. This is useful when (for example) the |
| 1637 | // original lookup would not have found something because it was a |
| 1638 | // dependent name. |
Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1639 | for (DeclContext *DC = SS.isEmpty() ? CurContext : 0; |
Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1640 | DC; DC = DC->getParent()) { |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1641 | if (isa<CXXRecordDecl>(DC)) { |
| 1642 | LookupQualifiedName(R, DC); |
| 1643 | |
| 1644 | if (!R.empty()) { |
| 1645 | // Don't give errors about ambiguities in this lookup. |
| 1646 | R.suppressDiagnostics(); |
| 1647 | |
| 1648 | CXXMethodDecl *CurMethod = dyn_cast<CXXMethodDecl>(CurContext); |
| 1649 | bool isInstance = CurMethod && |
| 1650 | CurMethod->isInstance() && |
| 1651 | DC == CurMethod->getParent(); |
| 1652 | |
| 1653 | // Give a code modification hint to insert 'this->'. |
| 1654 | // TODO: fixit for inserting 'Base<T>::' in the other cases. |
| 1655 | // Actually quite difficult! |
Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1656 | if (isInstance) { |
Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1657 | UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>( |
| 1658 | CallsUndergoingInstantiation.back()->getCallee()); |
Nick Lewycky | fe71238 | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1659 | CXXMethodDecl *DepMethod = cast_or_null<CXXMethodDecl>( |
Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1660 | CurMethod->getInstantiatedFromMemberFunction()); |
Eli Friedman | 0483192 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1661 | if (DepMethod) { |
Nick Lewycky | fe71238 | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1662 | Diag(R.getNameLoc(), diagnostic) << Name |
| 1663 | << FixItHint::CreateInsertion(R.getNameLoc(), "this->"); |
| 1664 | QualType DepThisType = DepMethod->getThisType(Context); |
| 1665 | CXXThisExpr *DepThis = new (Context) CXXThisExpr( |
| 1666 | R.getNameLoc(), DepThisType, false); |
| 1667 | TemplateArgumentListInfo TList; |
| 1668 | if (ULE->hasExplicitTemplateArgs()) |
| 1669 | ULE->copyTemplateArgumentsInto(TList); |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 1670 | |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 1671 | CXXScopeSpec SS; |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 1672 | SS.Adopt(ULE->getQualifierLoc()); |
Nick Lewycky | fe71238 | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1673 | CXXDependentScopeMemberExpr *DepExpr = |
| 1674 | CXXDependentScopeMemberExpr::Create( |
| 1675 | Context, DepThis, DepThisType, true, SourceLocation(), |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 1676 | SS.getWithLocInContext(Context), NULL, |
Nick Lewycky | fe71238 | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1677 | R.getLookupNameInfo(), &TList); |
| 1678 | CallsUndergoingInstantiation.back()->setCallee(DepExpr); |
Eli Friedman | 0483192 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1679 | } else { |
Nick Lewycky | fe71238 | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1680 | // FIXME: we should be able to handle this case too. It is correct |
| 1681 | // to add this-> here. This is a workaround for PR7947. |
| 1682 | Diag(R.getNameLoc(), diagnostic) << Name; |
Eli Friedman | 0483192 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1683 | } |
Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1684 | } else { |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1685 | Diag(R.getNameLoc(), diagnostic) << Name; |
Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1686 | } |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1687 | |
| 1688 | // Do we really want to note all of these? |
| 1689 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 1690 | Diag((*I)->getLocation(), diag::note_dependent_var_use); |
| 1691 | |
| 1692 | // Tell the callee to try to recover. |
| 1693 | return false; |
| 1694 | } |
Douglas Gregor | 86b8d9f | 2010-08-09 22:38:14 +0000 | [diff] [blame] | 1695 | |
| 1696 | R.clear(); |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1697 | } |
| 1698 | } |
| 1699 | |
Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1700 | // We didn't find anything, so try to correct for a typo. |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1701 | DeclarationName Corrected; |
Daniel Dunbar | f7ced25 | 2010-06-02 15:46:52 +0000 | [diff] [blame] | 1702 | if (S && (Corrected = CorrectTypo(R, S, &SS, 0, false, CTC))) { |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1703 | if (!R.empty()) { |
| 1704 | if (isa<ValueDecl>(*R.begin()) || isa<FunctionTemplateDecl>(*R.begin())) { |
| 1705 | if (SS.isEmpty()) |
| 1706 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << R.getLookupName() |
| 1707 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 1708 | R.getLookupName().getAsString()); |
| 1709 | else |
| 1710 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
| 1711 | << Name << computeDeclContext(SS, false) << R.getLookupName() |
| 1712 | << SS.getRange() |
| 1713 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 1714 | R.getLookupName().getAsString()); |
| 1715 | if (NamedDecl *ND = R.getAsSingle<NamedDecl>()) |
| 1716 | Diag(ND->getLocation(), diag::note_previous_decl) |
| 1717 | << ND->getDeclName(); |
| 1718 | |
| 1719 | // Tell the callee to try to recover. |
| 1720 | return false; |
| 1721 | } |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1722 | |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1723 | if (isa<TypeDecl>(*R.begin()) || isa<ObjCInterfaceDecl>(*R.begin())) { |
| 1724 | // FIXME: If we ended up with a typo for a type name or |
| 1725 | // Objective-C class name, we're in trouble because the parser |
| 1726 | // is in the wrong place to recover. Suggest the typo |
| 1727 | // correction, but don't make it a fix-it since we're not going |
| 1728 | // to recover well anyway. |
| 1729 | if (SS.isEmpty()) |
| 1730 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << R.getLookupName(); |
| 1731 | else |
| 1732 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
| 1733 | << Name << computeDeclContext(SS, false) << R.getLookupName() |
| 1734 | << SS.getRange(); |
| 1735 | |
| 1736 | // Don't try to recover; it won't work. |
| 1737 | return true; |
| 1738 | } |
| 1739 | } else { |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1740 | // 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] | 1741 | // because we aren't able to recover. |
Douglas Gregor | 2536398 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1742 | if (SS.isEmpty()) |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1743 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << Corrected; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1744 | else |
Douglas Gregor | 2536398 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1745 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1746 | << Name << computeDeclContext(SS, false) << Corrected |
| 1747 | << SS.getRange(); |
Douglas Gregor | 2536398 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1748 | return true; |
| 1749 | } |
Douglas Gregor | 2536398 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1750 | R.clear(); |
Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1751 | } |
| 1752 | |
| 1753 | // Emit a special diagnostic for failed member lookups. |
| 1754 | // FIXME: computing the declaration context might fail here (?) |
| 1755 | if (!SS.isEmpty()) { |
| 1756 | Diag(R.getNameLoc(), diag::err_no_member) |
| 1757 | << Name << computeDeclContext(SS, false) |
| 1758 | << SS.getRange(); |
| 1759 | return true; |
| 1760 | } |
| 1761 | |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1762 | // Give up, we can't recover. |
| 1763 | Diag(R.getNameLoc(), diagnostic) << Name; |
| 1764 | return true; |
| 1765 | } |
| 1766 | |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1767 | ObjCPropertyDecl *Sema::canSynthesizeProvisionalIvar(IdentifierInfo *II) { |
| 1768 | ObjCMethodDecl *CurMeth = getCurMethodDecl(); |
Fariborz Jahanian | 8615134 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1769 | ObjCInterfaceDecl *IDecl = CurMeth->getClassInterface(); |
| 1770 | if (!IDecl) |
| 1771 | return 0; |
| 1772 | ObjCImplementationDecl *ClassImpDecl = IDecl->getImplementation(); |
| 1773 | if (!ClassImpDecl) |
| 1774 | return 0; |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1775 | ObjCPropertyDecl *property = LookupPropertyDecl(IDecl, II); |
Fariborz Jahanian | 8615134 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1776 | if (!property) |
| 1777 | return 0; |
| 1778 | if (ObjCPropertyImplDecl *PIDecl = ClassImpDecl->FindPropertyImplDecl(II)) |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1779 | if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic || |
| 1780 | PIDecl->getPropertyIvarDecl()) |
Fariborz Jahanian | 8615134 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1781 | return 0; |
| 1782 | return property; |
| 1783 | } |
| 1784 | |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1785 | bool Sema::canSynthesizeProvisionalIvar(ObjCPropertyDecl *Property) { |
| 1786 | ObjCMethodDecl *CurMeth = getCurMethodDecl(); |
| 1787 | ObjCInterfaceDecl *IDecl = CurMeth->getClassInterface(); |
| 1788 | if (!IDecl) |
| 1789 | return false; |
| 1790 | ObjCImplementationDecl *ClassImpDecl = IDecl->getImplementation(); |
| 1791 | if (!ClassImpDecl) |
| 1792 | return false; |
| 1793 | if (ObjCPropertyImplDecl *PIDecl |
| 1794 | = ClassImpDecl->FindPropertyImplDecl(Property->getIdentifier())) |
| 1795 | if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic || |
| 1796 | PIDecl->getPropertyIvarDecl()) |
| 1797 | return false; |
| 1798 | |
| 1799 | return true; |
| 1800 | } |
| 1801 | |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1802 | ObjCIvarDecl *Sema::SynthesizeProvisionalIvar(LookupResult &Lookup, |
| 1803 | IdentifierInfo *II, |
| 1804 | SourceLocation NameLoc) { |
| 1805 | ObjCMethodDecl *CurMeth = getCurMethodDecl(); |
Fariborz Jahanian | 7b70eb4 | 2010-07-30 16:59:05 +0000 | [diff] [blame] | 1806 | bool LookForIvars; |
| 1807 | if (Lookup.empty()) |
| 1808 | LookForIvars = true; |
| 1809 | else if (CurMeth->isClassMethod()) |
| 1810 | LookForIvars = false; |
| 1811 | else |
| 1812 | LookForIvars = (Lookup.isSingleResult() && |
Fariborz Jahanian | 9312fcc | 2011-01-26 00:57:01 +0000 | [diff] [blame] | 1813 | Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod() && |
| 1814 | (Lookup.getAsSingle<VarDecl>() != 0)); |
Fariborz Jahanian | 7b70eb4 | 2010-07-30 16:59:05 +0000 | [diff] [blame] | 1815 | if (!LookForIvars) |
| 1816 | return 0; |
| 1817 | |
Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1818 | ObjCInterfaceDecl *IDecl = CurMeth->getClassInterface(); |
| 1819 | if (!IDecl) |
| 1820 | return 0; |
| 1821 | ObjCImplementationDecl *ClassImpDecl = IDecl->getImplementation(); |
Fariborz Jahanian | 2a36089 | 2010-07-19 16:14:33 +0000 | [diff] [blame] | 1822 | if (!ClassImpDecl) |
| 1823 | return 0; |
Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1824 | bool DynamicImplSeen = false; |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1825 | ObjCPropertyDecl *property = LookupPropertyDecl(IDecl, II); |
Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1826 | if (!property) |
| 1827 | return 0; |
Fariborz Jahanian | bfcbc85 | 2010-10-19 19:08:23 +0000 | [diff] [blame] | 1828 | if (ObjCPropertyImplDecl *PIDecl = ClassImpDecl->FindPropertyImplDecl(II)) { |
Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1829 | DynamicImplSeen = |
| 1830 | (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic); |
Fariborz Jahanian | bfcbc85 | 2010-10-19 19:08:23 +0000 | [diff] [blame] | 1831 | // property implementation has a designated ivar. No need to assume a new |
| 1832 | // one. |
| 1833 | if (!DynamicImplSeen && PIDecl->getPropertyIvarDecl()) |
| 1834 | return 0; |
| 1835 | } |
Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1836 | if (!DynamicImplSeen) { |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1837 | QualType PropType = Context.getCanonicalType(property->getType()); |
| 1838 | ObjCIvarDecl *Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1839 | NameLoc, NameLoc, |
Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1840 | II, PropType, /*Dinfo=*/0, |
Fariborz Jahanian | 522eb7b | 2010-12-15 23:29:04 +0000 | [diff] [blame] | 1841 | ObjCIvarDecl::Private, |
Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1842 | (Expr *)0, true); |
| 1843 | ClassImpDecl->addDecl(Ivar); |
| 1844 | IDecl->makeDeclVisibleInContext(Ivar, false); |
| 1845 | property->setPropertyIvarDecl(Ivar); |
| 1846 | return Ivar; |
| 1847 | } |
| 1848 | return 0; |
| 1849 | } |
| 1850 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1851 | ExprResult Sema::ActOnIdExpression(Scope *S, |
John McCall | 24d1894 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 1852 | CXXScopeSpec &SS, |
| 1853 | UnqualifiedId &Id, |
| 1854 | bool HasTrailingLParen, |
| 1855 | bool isAddressOfOperand) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1856 | assert(!(isAddressOfOperand && HasTrailingLParen) && |
| 1857 | "cannot be direct & operand and have a trailing lparen"); |
| 1858 | |
| 1859 | if (SS.isInvalid()) |
Douglas Gregor | ed8f288 | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1860 | return ExprError(); |
Douglas Gregor | 90a1a65 | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1861 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1862 | TemplateArgumentListInfo TemplateArgsBuffer; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1863 | |
| 1864 | // Decompose the UnqualifiedId into the following data. |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1865 | DeclarationNameInfo NameInfo; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1866 | const TemplateArgumentListInfo *TemplateArgs; |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1867 | DecomposeUnqualifiedId(*this, Id, TemplateArgsBuffer, NameInfo, TemplateArgs); |
Douglas Gregor | 90a1a65 | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1868 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1869 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | 4ea8043 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 1870 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1871 | SourceLocation NameLoc = NameInfo.getLoc(); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1872 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1873 | // C++ [temp.dep.expr]p3: |
| 1874 | // An id-expression is type-dependent if it contains: |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 1875 | // -- an identifier that was declared with a dependent type, |
| 1876 | // (note: handled after lookup) |
| 1877 | // -- a template-id that is dependent, |
| 1878 | // (note: handled in BuildTemplateIdExpr) |
| 1879 | // -- a conversion-function-id that specifies a dependent type, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1880 | // -- a nested-name-specifier that contains a class-name that |
| 1881 | // names a dependent type. |
| 1882 | // Determine whether this is a member of an unknown specialization; |
| 1883 | // we need to handle these differently. |
Eli Friedman | 964dbda | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1884 | bool DependentID = false; |
| 1885 | if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName && |
| 1886 | Name.getCXXNameType()->isDependentType()) { |
| 1887 | DependentID = true; |
| 1888 | } else if (SS.isSet()) { |
Chris Lattner | ebb5c6c | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1889 | if (DeclContext *DC = computeDeclContext(SS, false)) { |
Eli Friedman | 964dbda | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1890 | if (RequireCompleteDeclContext(SS, DC)) |
| 1891 | return ExprError(); |
Eli Friedman | 964dbda | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1892 | } else { |
| 1893 | DependentID = true; |
| 1894 | } |
| 1895 | } |
| 1896 | |
Chris Lattner | ebb5c6c | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1897 | if (DependentID) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1898 | return ActOnDependentIdExpression(SS, NameInfo, isAddressOfOperand, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1899 | TemplateArgs); |
Chris Lattner | ebb5c6c | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1900 | |
Fariborz Jahanian | 8615134 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1901 | bool IvarLookupFollowUp = false; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1902 | // Perform the required lookup. |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1903 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1904 | if (TemplateArgs) { |
Douglas Gregor | 3e51e17 | 2010-05-20 20:58:56 +0000 | [diff] [blame] | 1905 | // Lookup the template name again to correctly establish the context in |
| 1906 | // which it was found. This is really unfortunate as we already did the |
| 1907 | // lookup to determine that it was a template name in the first place. If |
| 1908 | // this becomes a performance hit, we can work harder to preserve those |
| 1909 | // results until we get here but it's likely not worth it. |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1910 | bool MemberOfUnknownSpecialization; |
| 1911 | LookupTemplateName(R, S, SS, QualType(), /*EnteringContext=*/false, |
| 1912 | MemberOfUnknownSpecialization); |
Douglas Gregor | a522693 | 2011-02-04 13:35:07 +0000 | [diff] [blame] | 1913 | |
| 1914 | if (MemberOfUnknownSpecialization || |
| 1915 | (R.getResultKind() == LookupResult::NotFoundInCurrentInstantiation)) |
| 1916 | return ActOnDependentIdExpression(SS, NameInfo, isAddressOfOperand, |
| 1917 | TemplateArgs); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1918 | } else { |
Fariborz Jahanian | 8615134 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1919 | IvarLookupFollowUp = (!SS.isSet() && II && getCurMethodDecl()); |
Fariborz Jahanian | 6fada5b | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1920 | LookupParsedName(R, S, &SS, !IvarLookupFollowUp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1921 | |
Douglas Gregor | a522693 | 2011-02-04 13:35:07 +0000 | [diff] [blame] | 1922 | // If the result might be in a dependent base class, this is a dependent |
| 1923 | // id-expression. |
| 1924 | if (R.getResultKind() == LookupResult::NotFoundInCurrentInstantiation) |
| 1925 | return ActOnDependentIdExpression(SS, NameInfo, isAddressOfOperand, |
| 1926 | TemplateArgs); |
| 1927 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1928 | // If this reference is in an Objective-C method, then we need to do |
| 1929 | // some special Objective-C lookup, too. |
Fariborz Jahanian | 6fada5b | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1930 | if (IvarLookupFollowUp) { |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1931 | ExprResult E(LookupInObjCMethod(R, S, II, true)); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1932 | if (E.isInvalid()) |
| 1933 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1934 | |
Chris Lattner | ebb5c6c | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1935 | if (Expr *Ex = E.takeAs<Expr>()) |
| 1936 | return Owned(Ex); |
| 1937 | |
| 1938 | // Synthesize ivars lazily. |
Fariborz Jahanian | c63f1c5 | 2011-01-03 18:08:02 +0000 | [diff] [blame] | 1939 | if (getLangOptions().ObjCDefaultSynthProperties && |
| 1940 | getLangOptions().ObjCNonFragileABI2) { |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1941 | if (SynthesizeProvisionalIvar(R, II, NameLoc)) { |
Fariborz Jahanian | 8046af7 | 2010-11-17 19:41:23 +0000 | [diff] [blame] | 1942 | if (const ObjCPropertyDecl *Property = |
| 1943 | canSynthesizeProvisionalIvar(II)) { |
| 1944 | Diag(NameLoc, diag::warn_synthesized_ivar_access) << II; |
| 1945 | Diag(Property->getLocation(), diag::note_property_declare); |
| 1946 | } |
Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1947 | return ActOnIdExpression(S, SS, Id, HasTrailingLParen, |
| 1948 | isAddressOfOperand); |
Fariborz Jahanian | 8046af7 | 2010-11-17 19:41:23 +0000 | [diff] [blame] | 1949 | } |
Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1950 | } |
Fariborz Jahanian | 18d90a9 | 2010-08-13 18:09:39 +0000 | [diff] [blame] | 1951 | // for further use, this must be set to false if in class method. |
| 1952 | IvarLookupFollowUp = getCurMethodDecl()->isInstanceMethod(); |
Steve Naroff | ebf4cb4 | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 1953 | } |
Chris Lattner | 59a2594 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 1954 | } |
Douglas Gregor | f15f5d3 | 2009-02-16 19:28:42 +0000 | [diff] [blame] | 1955 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1956 | if (R.isAmbiguous()) |
| 1957 | return ExprError(); |
| 1958 | |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1959 | // Determine whether this name might be a candidate for |
| 1960 | // argument-dependent lookup. |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1961 | bool ADL = UseArgumentDependentLookup(SS, R, HasTrailingLParen); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1962 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1963 | if (R.empty() && !ADL) { |
Bill Wendling | 4073ed5 | 2007-02-13 01:51:42 +0000 | [diff] [blame] | 1964 | // Otherwise, this could be an implicitly declared function reference (legal |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1965 | // in C90, extension in C99, forbidden in C++). |
| 1966 | if (HasTrailingLParen && II && !getLangOptions().CPlusPlus) { |
| 1967 | NamedDecl *D = ImplicitlyDefineFunction(NameLoc, *II, S); |
| 1968 | if (D) R.addDecl(D); |
| 1969 | } |
| 1970 | |
| 1971 | // If this name wasn't predeclared and if this is not a function |
| 1972 | // call, diagnose the problem. |
| 1973 | if (R.empty()) { |
Douglas Gregor | 5fd04d4 | 2010-05-18 16:14:23 +0000 | [diff] [blame] | 1974 | if (DiagnoseEmptyLookup(S, SS, R, CTC_Unknown)) |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1975 | return ExprError(); |
| 1976 | |
| 1977 | assert(!R.empty() && |
| 1978 | "DiagnoseEmptyLookup returned false but added no results"); |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1979 | |
| 1980 | // If we found an Objective-C instance variable, let |
| 1981 | // LookupInObjCMethod build the appropriate expression to |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1982 | // reference the ivar. |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1983 | if (ObjCIvarDecl *Ivar = R.getAsSingle<ObjCIvarDecl>()) { |
| 1984 | R.clear(); |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1985 | ExprResult E(LookupInObjCMethod(R, S, Ivar->getIdentifier())); |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1986 | assert(E.isInvalid() || E.get()); |
| 1987 | return move(E); |
| 1988 | } |
Steve Naroff | 92e30f8 | 2007-04-02 22:35:25 +0000 | [diff] [blame] | 1989 | } |
Chris Lattner | 17ed487 | 2006-11-20 04:58:19 +0000 | [diff] [blame] | 1990 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1991 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1992 | // This is guaranteed from this point on. |
| 1993 | assert(!R.empty() || ADL); |
| 1994 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1995 | // Check whether this might be a C++ implicit instance member access. |
John McCall | 24d1894 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 1996 | // C++ [class.mfct.non-static]p3: |
| 1997 | // When an id-expression that is not part of a class member access |
| 1998 | // syntax and not used to form a pointer to member is used in the |
| 1999 | // body of a non-static member function of class X, if name lookup |
| 2000 | // resolves the name in the id-expression to a non-static non-type |
| 2001 | // member of some class C, the id-expression is transformed into a |
| 2002 | // class member access expression using (*this) as the |
| 2003 | // postfix-expression to the left of the . operator. |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 2004 | // |
| 2005 | // But we don't actually need to do this for '&' operands if R |
| 2006 | // resolved to a function or overloaded function set, because the |
| 2007 | // expression is ill-formed if it actually works out to be a |
| 2008 | // non-static member function: |
| 2009 | // |
| 2010 | // C++ [expr.ref]p4: |
| 2011 | // Otherwise, if E1.E2 refers to a non-static member function. . . |
| 2012 | // [t]he expression can be used only as the left-hand operand of a |
| 2013 | // member function call. |
| 2014 | // |
| 2015 | // There are other safeguards against such uses, but it's important |
| 2016 | // to get this right here so that we don't end up making a |
| 2017 | // spuriously dependent expression if we're inside a dependent |
| 2018 | // instance method. |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2019 | if (!R.empty() && (*R.begin())->isCXXClassMember()) { |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 2020 | bool MightBeImplicitMember; |
| 2021 | if (!isAddressOfOperand) |
| 2022 | MightBeImplicitMember = true; |
| 2023 | else if (!SS.isEmpty()) |
| 2024 | MightBeImplicitMember = false; |
| 2025 | else if (R.isOverloadedResult()) |
| 2026 | MightBeImplicitMember = false; |
Douglas Gregor | 1262b06 | 2010-08-30 16:00:47 +0000 | [diff] [blame] | 2027 | else if (R.isUnresolvableResult()) |
| 2028 | MightBeImplicitMember = true; |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 2029 | else |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2030 | MightBeImplicitMember = isa<FieldDecl>(R.getFoundDecl()) || |
| 2031 | isa<IndirectFieldDecl>(R.getFoundDecl()); |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 2032 | |
| 2033 | if (MightBeImplicitMember) |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2034 | return BuildPossibleImplicitMemberExpr(SS, R, TemplateArgs); |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2035 | } |
| 2036 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2037 | if (TemplateArgs) |
| 2038 | return BuildTemplateIdExpr(SS, R, ADL, *TemplateArgs); |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2039 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2040 | return BuildDeclarationNameExpr(SS, R, ADL); |
| 2041 | } |
| 2042 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2043 | /// Builds an expression which might be an implicit member expression. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2044 | ExprResult |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2045 | Sema::BuildPossibleImplicitMemberExpr(const CXXScopeSpec &SS, |
| 2046 | LookupResult &R, |
| 2047 | const TemplateArgumentListInfo *TemplateArgs) { |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2048 | switch (ClassifyImplicitMemberAccess(*this, CurScope, R)) { |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2049 | case IMA_Instance: |
| 2050 | return BuildImplicitMemberExpr(SS, R, TemplateArgs, true); |
| 2051 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2052 | case IMA_Mixed: |
| 2053 | case IMA_Mixed_Unrelated: |
| 2054 | case IMA_Unresolved: |
| 2055 | return BuildImplicitMemberExpr(SS, R, TemplateArgs, false); |
| 2056 | |
| 2057 | case IMA_Static: |
| 2058 | case IMA_Mixed_StaticContext: |
| 2059 | case IMA_Unresolved_StaticContext: |
| 2060 | if (TemplateArgs) |
| 2061 | return BuildTemplateIdExpr(SS, R, false, *TemplateArgs); |
| 2062 | return BuildDeclarationNameExpr(SS, R, false); |
| 2063 | |
| 2064 | case IMA_Error_StaticContext: |
| 2065 | case IMA_Error_Unrelated: |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2066 | DiagnoseInstanceReference(*this, SS, R.getRepresentativeDecl(), |
| 2067 | R.getLookupNameInfo()); |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2068 | return ExprError(); |
| 2069 | } |
| 2070 | |
| 2071 | llvm_unreachable("unexpected instance member access kind"); |
| 2072 | return ExprError(); |
| 2073 | } |
| 2074 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 2075 | /// BuildQualifiedDeclarationNameExpr - Build a C++ qualified |
| 2076 | /// declaration name, generally during template instantiation. |
| 2077 | /// There's a large number of things which don't need to be done along |
| 2078 | /// this path. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2079 | ExprResult |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 2080 | Sema::BuildQualifiedDeclarationNameExpr(CXXScopeSpec &SS, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2081 | const DeclarationNameInfo &NameInfo) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2082 | DeclContext *DC; |
Douglas Gregor | a02bb34 | 2010-04-28 07:04:26 +0000 | [diff] [blame] | 2083 | if (!(DC = computeDeclContext(SS, false)) || DC->isDependentContext()) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2084 | return BuildDependentDeclRefExpr(SS, NameInfo, 0); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2085 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 2086 | if (RequireCompleteDeclContext(SS, DC)) |
Douglas Gregor | a02bb34 | 2010-04-28 07:04:26 +0000 | [diff] [blame] | 2087 | return ExprError(); |
| 2088 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2089 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2090 | LookupQualifiedName(R, DC); |
| 2091 | |
| 2092 | if (R.isAmbiguous()) |
| 2093 | return ExprError(); |
| 2094 | |
| 2095 | if (R.empty()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2096 | Diag(NameInfo.getLoc(), diag::err_no_member) |
| 2097 | << NameInfo.getName() << DC << SS.getRange(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2098 | return ExprError(); |
| 2099 | } |
| 2100 | |
| 2101 | return BuildDeclarationNameExpr(SS, R, /*ADL*/ false); |
| 2102 | } |
| 2103 | |
| 2104 | /// LookupInObjCMethod - The parser has read a name in, and Sema has |
| 2105 | /// detected that we're currently inside an ObjC method. Perform some |
| 2106 | /// additional lookup. |
| 2107 | /// |
| 2108 | /// Ideally, most of this would be done by lookup, but there's |
| 2109 | /// actually quite a lot of extra work involved. |
| 2110 | /// |
| 2111 | /// Returns a null sentinel to indicate trivial success. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2112 | ExprResult |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2113 | Sema::LookupInObjCMethod(LookupResult &Lookup, Scope *S, |
Chris Lattner | a36ec42 | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 2114 | IdentifierInfo *II, bool AllowBuiltinCreation) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2115 | SourceLocation Loc = Lookup.getNameLoc(); |
Chris Lattner | 8731366 | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 2116 | ObjCMethodDecl *CurMethod = getCurMethodDecl(); |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 2117 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2118 | // There are two cases to handle here. 1) scoped lookup could have failed, |
| 2119 | // in which case we should look for an ivar. 2) scoped lookup could have |
| 2120 | // found a decl, but that decl is outside the current instance method (i.e. |
| 2121 | // a global variable). In these two cases, we do a lookup for an ivar with |
| 2122 | // this name, if the lookup sucedes, we replace it our current decl. |
| 2123 | |
| 2124 | // If we're in a class method, we don't normally want to look for |
| 2125 | // ivars. But if we don't find anything else, and there's an |
| 2126 | // ivar, that's an error. |
Chris Lattner | 8731366 | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 2127 | bool IsClassMethod = CurMethod->isClassMethod(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2128 | |
| 2129 | bool LookForIvars; |
| 2130 | if (Lookup.empty()) |
| 2131 | LookForIvars = true; |
| 2132 | else if (IsClassMethod) |
| 2133 | LookForIvars = false; |
| 2134 | else |
| 2135 | LookForIvars = (Lookup.isSingleResult() && |
| 2136 | Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod()); |
Fariborz Jahanian | 4587803 | 2010-02-09 19:31:38 +0000 | [diff] [blame] | 2137 | ObjCInterfaceDecl *IFace = 0; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2138 | if (LookForIvars) { |
Chris Lattner | 8731366 | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 2139 | IFace = CurMethod->getClassInterface(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2140 | ObjCInterfaceDecl *ClassDeclared; |
| 2141 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
| 2142 | // Diagnose using an ivar in a class method. |
| 2143 | if (IsClassMethod) |
| 2144 | return ExprError(Diag(Loc, diag::error_ivar_use_in_class_method) |
| 2145 | << IV->getDeclName()); |
| 2146 | |
| 2147 | // If we're referencing an invalid decl, just return this as a silent |
| 2148 | // error node. The error diagnostic was already emitted on the decl. |
| 2149 | if (IV->isInvalidDecl()) |
| 2150 | return ExprError(); |
| 2151 | |
| 2152 | // Check if referencing a field with __attribute__((deprecated)). |
| 2153 | if (DiagnoseUseOfDecl(IV, Loc)) |
| 2154 | return ExprError(); |
| 2155 | |
| 2156 | // Diagnose the use of an ivar outside of the declaring class. |
| 2157 | if (IV->getAccessControl() == ObjCIvarDecl::Private && |
| 2158 | ClassDeclared != IFace) |
| 2159 | Diag(Loc, diag::error_private_ivar_access) << IV->getDeclName(); |
| 2160 | |
| 2161 | // FIXME: This should use a new expr for a direct reference, don't |
| 2162 | // turn this into Self->ivar, just return a BareIVarExpr or something. |
| 2163 | IdentifierInfo &II = Context.Idents.get("self"); |
| 2164 | UnqualifiedId SelfName; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2165 | SelfName.setIdentifier(&II, SourceLocation()); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2166 | CXXScopeSpec SelfScopeSpec; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2167 | ExprResult SelfExpr = ActOnIdExpression(S, SelfScopeSpec, |
Douglas Gregor | a1ed39b | 2010-09-22 16:33:13 +0000 | [diff] [blame] | 2168 | SelfName, false, false); |
| 2169 | if (SelfExpr.isInvalid()) |
| 2170 | return ExprError(); |
| 2171 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2172 | SelfExpr = DefaultLvalueConversion(SelfExpr.take()); |
| 2173 | if (SelfExpr.isInvalid()) |
| 2174 | return ExprError(); |
John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 2175 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2176 | MarkDeclarationReferenced(Loc, IV); |
Fariborz Jahanian | 82bc436 | 2011-04-12 23:39:33 +0000 | [diff] [blame] | 2177 | Expr *base = SelfExpr.take(); |
| 2178 | base = base->IgnoreParenImpCasts(); |
| 2179 | if (const DeclRefExpr *DE = dyn_cast<DeclRefExpr>(base)) { |
| 2180 | const NamedDecl *ND = DE->getDecl(); |
| 2181 | if (!isa<ImplicitParamDecl>(ND)) { |
Fariborz Jahanian | 66a6c06 | 2011-04-15 17:04:42 +0000 | [diff] [blame] | 2182 | // relax the rule such that it is allowed to have a shadow 'self' |
| 2183 | // where stand-alone ivar can be found in this 'self' object. |
| 2184 | // This is to match gcc's behavior. |
| 2185 | ObjCInterfaceDecl *selfIFace = 0; |
| 2186 | if (const ObjCObjectPointerType *OPT = |
| 2187 | base->getType()->getAsObjCInterfacePointerType()) |
| 2188 | selfIFace = OPT->getInterfaceDecl(); |
| 2189 | if (!selfIFace || |
| 2190 | !selfIFace->lookupInstanceVariable(IV->getIdentifier())) { |
Fariborz Jahanian | 82bc436 | 2011-04-12 23:39:33 +0000 | [diff] [blame] | 2191 | Diag(Loc, diag::error_implicit_ivar_access) |
| 2192 | << IV->getDeclName(); |
| 2193 | Diag(ND->getLocation(), diag::note_declared_at); |
| 2194 | return ExprError(); |
| 2195 | } |
Fariborz Jahanian | 66a6c06 | 2011-04-15 17:04:42 +0000 | [diff] [blame] | 2196 | } |
Fariborz Jahanian | 82bc436 | 2011-04-12 23:39:33 +0000 | [diff] [blame] | 2197 | } |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2198 | return Owned(new (Context) |
| 2199 | ObjCIvarRefExpr(IV, IV->getType(), Loc, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2200 | SelfExpr.take(), true, true)); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2201 | } |
Chris Lattner | 8731366 | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 2202 | } else if (CurMethod->isInstanceMethod()) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2203 | // We should warn if a local variable hides an ivar. |
Chris Lattner | 8731366 | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 2204 | ObjCInterfaceDecl *IFace = CurMethod->getClassInterface(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2205 | ObjCInterfaceDecl *ClassDeclared; |
| 2206 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
| 2207 | if (IV->getAccessControl() != ObjCIvarDecl::Private || |
| 2208 | IFace == ClassDeclared) |
| 2209 | Diag(Loc, diag::warn_ivar_use_hidden) << IV->getDeclName(); |
| 2210 | } |
| 2211 | } |
| 2212 | |
Fariborz Jahanian | 6fada5b | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 2213 | if (Lookup.empty() && II && AllowBuiltinCreation) { |
| 2214 | // FIXME. Consolidate this with similar code in LookupName. |
| 2215 | if (unsigned BuiltinID = II->getBuiltinID()) { |
| 2216 | if (!(getLangOptions().CPlusPlus && |
| 2217 | Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))) { |
| 2218 | NamedDecl *D = LazilyCreateBuiltin((IdentifierInfo *)II, BuiltinID, |
| 2219 | S, Lookup.isForRedeclaration(), |
| 2220 | Lookup.getNameLoc()); |
| 2221 | if (D) Lookup.addDecl(D); |
| 2222 | } |
| 2223 | } |
| 2224 | } |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2225 | // Sentinel value saying that we didn't do anything special. |
| 2226 | return Owned((Expr*) 0); |
Douglas Gregor | 3256d04 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 2227 | } |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2228 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2229 | /// \brief Cast a base object to a member's actual type. |
| 2230 | /// |
| 2231 | /// Logically this happens in three phases: |
| 2232 | /// |
| 2233 | /// * First we cast from the base type to the naming class. |
| 2234 | /// The naming class is the class into which we were looking |
| 2235 | /// when we found the member; it's the qualifier type if a |
| 2236 | /// qualifier was provided, and otherwise it's the base type. |
| 2237 | /// |
| 2238 | /// * Next we cast from the naming class to the declaring class. |
| 2239 | /// If the member we found was brought into a class's scope by |
| 2240 | /// a using declaration, this is that class; otherwise it's |
| 2241 | /// the class declaring the member. |
| 2242 | /// |
| 2243 | /// * Finally we cast from the declaring class to the "true" |
| 2244 | /// declaring class of the member. This conversion does not |
| 2245 | /// obey access control. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2246 | ExprResult |
| 2247 | Sema::PerformObjectMemberConversion(Expr *From, |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2248 | NestedNameSpecifier *Qualifier, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2249 | NamedDecl *FoundDecl, |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2250 | NamedDecl *Member) { |
| 2251 | CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Member->getDeclContext()); |
| 2252 | if (!RD) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2253 | return Owned(From); |
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 | QualType DestRecordType; |
| 2256 | QualType DestType; |
| 2257 | QualType FromRecordType; |
| 2258 | QualType FromType = From->getType(); |
| 2259 | bool PointerConversions = false; |
| 2260 | if (isa<FieldDecl>(Member)) { |
| 2261 | DestRecordType = Context.getCanonicalType(Context.getTypeDeclType(RD)); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2262 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2263 | if (FromType->getAs<PointerType>()) { |
| 2264 | DestType = Context.getPointerType(DestRecordType); |
| 2265 | FromRecordType = FromType->getPointeeType(); |
| 2266 | PointerConversions = true; |
| 2267 | } else { |
| 2268 | DestType = DestRecordType; |
| 2269 | FromRecordType = FromType; |
Fariborz Jahanian | bb67b82 | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 2270 | } |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2271 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member)) { |
| 2272 | if (Method->isStatic()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2273 | return Owned(From); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2274 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2275 | DestType = Method->getThisType(Context); |
| 2276 | DestRecordType = DestType->getPointeeType(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2277 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2278 | if (FromType->getAs<PointerType>()) { |
| 2279 | FromRecordType = FromType->getPointeeType(); |
| 2280 | PointerConversions = true; |
| 2281 | } else { |
| 2282 | FromRecordType = FromType; |
| 2283 | DestType = DestRecordType; |
| 2284 | } |
| 2285 | } else { |
| 2286 | // No conversion necessary. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2287 | return Owned(From); |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2288 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2289 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2290 | if (DestType->isDependentType() || FromType->isDependentType()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2291 | return Owned(From); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2292 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2293 | // If the unqualified types are the same, no conversion is necessary. |
| 2294 | if (Context.hasSameUnqualifiedType(FromRecordType, DestRecordType)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2295 | return Owned(From); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2296 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2297 | SourceRange FromRange = From->getSourceRange(); |
| 2298 | SourceLocation FromLoc = FromRange.getBegin(); |
| 2299 | |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2300 | ExprValueKind VK = CastCategory(From); |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2301 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2302 | // C++ [class.member.lookup]p8: |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2303 | // [...] Ambiguities can often be resolved by qualifying a name with its |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2304 | // class name. |
| 2305 | // |
| 2306 | // If the member was a qualified name and the qualified referred to a |
| 2307 | // specific base subobject type, we'll cast to that intermediate type |
| 2308 | // first and then to the object in which the member is declared. That allows |
| 2309 | // one to resolve ambiguities in, e.g., a diamond-shaped hierarchy such as: |
| 2310 | // |
| 2311 | // class Base { public: int x; }; |
| 2312 | // class Derived1 : public Base { }; |
| 2313 | // class Derived2 : public Base { }; |
| 2314 | // class VeryDerived : public Derived1, public Derived2 { void f(); }; |
| 2315 | // |
| 2316 | // void VeryDerived::f() { |
| 2317 | // x = 17; // error: ambiguous base subobjects |
| 2318 | // Derived1::x = 17; // okay, pick the Base subobject of Derived1 |
| 2319 | // } |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2320 | if (Qualifier) { |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2321 | QualType QType = QualType(Qualifier->getAsType(), 0); |
| 2322 | assert(!QType.isNull() && "lookup done with dependent qualifier?"); |
| 2323 | assert(QType->isRecordType() && "lookup done with non-record type"); |
| 2324 | |
| 2325 | QualType QRecordType = QualType(QType->getAs<RecordType>(), 0); |
| 2326 | |
| 2327 | // In C++98, the qualifier type doesn't actually have to be a base |
| 2328 | // type of the object type, in which case we just ignore it. |
| 2329 | // Otherwise build the appropriate casts. |
| 2330 | if (IsDerivedFrom(FromRecordType, QRecordType)) { |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2331 | CXXCastPath BasePath; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2332 | if (CheckDerivedToBaseConversion(FromRecordType, QRecordType, |
Anders Carlsson | b78feca | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2333 | FromLoc, FromRange, &BasePath)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2334 | return ExprError(); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2335 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2336 | if (PointerConversions) |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2337 | QType = Context.getPointerType(QType); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2338 | From = ImpCastExprToType(From, QType, CK_UncheckedDerivedToBase, |
| 2339 | VK, &BasePath).take(); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2340 | |
| 2341 | FromType = QType; |
| 2342 | FromRecordType = QRecordType; |
| 2343 | |
| 2344 | // If the qualifier type was the same as the destination type, |
| 2345 | // we're done. |
| 2346 | if (Context.hasSameUnqualifiedType(FromRecordType, DestRecordType)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2347 | return Owned(From); |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2348 | } |
| 2349 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2350 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2351 | bool IgnoreAccess = false; |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2352 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2353 | // If we actually found the member through a using declaration, cast |
| 2354 | // down to the using declaration's type. |
| 2355 | // |
| 2356 | // Pointer equality is fine here because only one declaration of a |
| 2357 | // class ever has member declarations. |
| 2358 | if (FoundDecl->getDeclContext() != Member->getDeclContext()) { |
| 2359 | assert(isa<UsingShadowDecl>(FoundDecl)); |
| 2360 | QualType URecordType = Context.getTypeDeclType( |
| 2361 | cast<CXXRecordDecl>(FoundDecl->getDeclContext())); |
| 2362 | |
| 2363 | // We only need to do this if the naming-class to declaring-class |
| 2364 | // conversion is non-trivial. |
| 2365 | if (!Context.hasSameUnqualifiedType(FromRecordType, URecordType)) { |
| 2366 | assert(IsDerivedFrom(FromRecordType, URecordType)); |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2367 | CXXCastPath BasePath; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2368 | if (CheckDerivedToBaseConversion(FromRecordType, URecordType, |
Anders Carlsson | b78feca | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2369 | FromLoc, FromRange, &BasePath)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2370 | return ExprError(); |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 2371 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2372 | QualType UType = URecordType; |
| 2373 | if (PointerConversions) |
| 2374 | UType = Context.getPointerType(UType); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2375 | From = ImpCastExprToType(From, UType, CK_UncheckedDerivedToBase, |
| 2376 | VK, &BasePath).take(); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2377 | FromType = UType; |
| 2378 | FromRecordType = URecordType; |
| 2379 | } |
| 2380 | |
| 2381 | // We don't do access control for the conversion from the |
| 2382 | // declaring class to the true declaring class. |
| 2383 | IgnoreAccess = true; |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2384 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2385 | |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2386 | CXXCastPath BasePath; |
Anders Carlsson | b78feca | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2387 | if (CheckDerivedToBaseConversion(FromRecordType, DestRecordType, |
| 2388 | FromLoc, FromRange, &BasePath, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2389 | IgnoreAccess)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2390 | return ExprError(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2391 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2392 | return ImpCastExprToType(From, DestType, CK_UncheckedDerivedToBase, |
| 2393 | VK, &BasePath); |
Fariborz Jahanian | bb67b82 | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 2394 | } |
Douglas Gregor | 3256d04 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 2395 | |
Douglas Gregor | f405d7e | 2009-08-31 23:41:50 +0000 | [diff] [blame] | 2396 | /// \brief Build a MemberExpr AST node. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2397 | static MemberExpr *BuildMemberExpr(ASTContext &C, Expr *Base, bool isArrow, |
Eli Friedman | 2cfcef6 | 2009-12-04 06:40:45 +0000 | [diff] [blame] | 2398 | const CXXScopeSpec &SS, ValueDecl *Member, |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 2399 | DeclAccessPair FoundDecl, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2400 | const DeclarationNameInfo &MemberNameInfo, |
| 2401 | QualType Ty, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2402 | ExprValueKind VK, ExprObjectKind OK, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2403 | const TemplateArgumentListInfo *TemplateArgs = 0) { |
Douglas Gregor | ea972d3 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 2404 | return MemberExpr::Create(C, Base, isArrow, SS.getWithLocInContext(C), |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2405 | Member, FoundDecl, MemberNameInfo, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2406 | TemplateArgs, Ty, VK, OK); |
Douglas Gregor | c190523 | 2009-08-26 22:36:53 +0000 | [diff] [blame] | 2407 | } |
| 2408 | |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 2409 | static ExprResult |
| 2410 | BuildFieldReferenceExpr(Sema &S, Expr *BaseExpr, bool IsArrow, |
| 2411 | const CXXScopeSpec &SS, FieldDecl *Field, |
| 2412 | DeclAccessPair FoundDecl, |
| 2413 | const DeclarationNameInfo &MemberNameInfo) { |
| 2414 | // x.a is an l-value if 'a' has a reference type. Otherwise: |
| 2415 | // x.a is an l-value/x-value/pr-value if the base is (and note |
| 2416 | // that *x is always an l-value), except that if the base isn't |
| 2417 | // an ordinary object then we must have an rvalue. |
| 2418 | ExprValueKind VK = VK_LValue; |
| 2419 | ExprObjectKind OK = OK_Ordinary; |
| 2420 | if (!IsArrow) { |
| 2421 | if (BaseExpr->getObjectKind() == OK_Ordinary) |
| 2422 | VK = BaseExpr->getValueKind(); |
| 2423 | else |
| 2424 | VK = VK_RValue; |
| 2425 | } |
| 2426 | if (VK != VK_RValue && Field->isBitField()) |
| 2427 | OK = OK_BitField; |
| 2428 | |
| 2429 | // Figure out the type of the member; see C99 6.5.2.3p3, C++ [expr.ref] |
| 2430 | QualType MemberType = Field->getType(); |
| 2431 | if (const ReferenceType *Ref = MemberType->getAs<ReferenceType>()) { |
| 2432 | MemberType = Ref->getPointeeType(); |
| 2433 | VK = VK_LValue; |
| 2434 | } else { |
| 2435 | QualType BaseType = BaseExpr->getType(); |
| 2436 | if (IsArrow) BaseType = BaseType->getAs<PointerType>()->getPointeeType(); |
| 2437 | |
| 2438 | Qualifiers BaseQuals = BaseType.getQualifiers(); |
| 2439 | |
| 2440 | // GC attributes are never picked up by members. |
| 2441 | BaseQuals.removeObjCGCAttr(); |
| 2442 | |
| 2443 | // CVR attributes from the base are picked up by members, |
| 2444 | // except that 'mutable' members don't pick up 'const'. |
| 2445 | if (Field->isMutable()) BaseQuals.removeConst(); |
| 2446 | |
| 2447 | Qualifiers MemberQuals |
| 2448 | = S.Context.getCanonicalType(MemberType).getQualifiers(); |
| 2449 | |
| 2450 | // TR 18037 does not allow fields to be declared with address spaces. |
| 2451 | assert(!MemberQuals.hasAddressSpace()); |
| 2452 | |
| 2453 | Qualifiers Combined = BaseQuals + MemberQuals; |
| 2454 | if (Combined != MemberQuals) |
| 2455 | MemberType = S.Context.getQualifiedType(MemberType, Combined); |
| 2456 | } |
| 2457 | |
| 2458 | S.MarkDeclarationReferenced(MemberNameInfo.getLoc(), Field); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2459 | ExprResult Base = |
| 2460 | S.PerformObjectMemberConversion(BaseExpr, SS.getScopeRep(), |
| 2461 | FoundDecl, Field); |
| 2462 | if (Base.isInvalid()) |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 2463 | return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2464 | return S.Owned(BuildMemberExpr(S.Context, Base.take(), IsArrow, SS, |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 2465 | Field, FoundDecl, MemberNameInfo, |
| 2466 | MemberType, VK, OK)); |
| 2467 | } |
| 2468 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2469 | /// Builds an implicit member access expression. The current context |
| 2470 | /// is known to be an instance method, and the given unqualified lookup |
| 2471 | /// set is known to contain only instance members, at least one of which |
| 2472 | /// is from an appropriate type. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2473 | ExprResult |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2474 | Sema::BuildImplicitMemberExpr(const CXXScopeSpec &SS, |
| 2475 | LookupResult &R, |
| 2476 | const TemplateArgumentListInfo *TemplateArgs, |
| 2477 | bool IsKnownInstance) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2478 | assert(!R.empty() && !R.isAmbiguous()); |
| 2479 | |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2480 | SourceLocation loc = R.getNameLoc(); |
Sebastian Redl | 3d3f75a | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 2481 | |
Douglas Gregor | 9ac7a07 | 2009-01-07 00:43:41 +0000 | [diff] [blame] | 2482 | // We may have found a field within an anonymous union or struct |
| 2483 | // (C++ [class.union]). |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2484 | // FIXME: template-ids inside anonymous structs? |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2485 | if (IndirectFieldDecl *FD = R.getAsSingle<IndirectFieldDecl>()) |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2486 | return BuildAnonymousStructUnionMemberReference(SS, R.getNameLoc(), FD); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2487 | |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2488 | // If this is known to be an instance access, go ahead and build an |
| 2489 | // implicit 'this' expression now. |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2490 | // 'this' expression now. |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2491 | QualType ThisTy = getAndCaptureCurrentThisType(); |
| 2492 | assert(!ThisTy.isNull() && "didn't correctly pre-flight capture of 'this'"); |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2493 | |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2494 | Expr *baseExpr = 0; // null signifies implicit access |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2495 | if (IsKnownInstance) { |
Douglas Gregor | b15af89 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 2496 | SourceLocation Loc = R.getNameLoc(); |
| 2497 | if (SS.getRange().isValid()) |
| 2498 | Loc = SS.getRange().getBegin(); |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2499 | baseExpr = new (Context) CXXThisExpr(loc, ThisTy, /*isImplicit=*/true); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 2500 | } |
| 2501 | |
Richard Smith | 938f40b | 2011-06-11 17:19:42 +0000 | [diff] [blame] | 2502 | return BuildMemberReferenceExpr(baseExpr, ThisTy, |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 2503 | /*OpLoc*/ SourceLocation(), |
| 2504 | /*IsArrow*/ true, |
John McCall | 38836f0 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 2505 | SS, |
| 2506 | /*FirstQualifierInScope*/ 0, |
| 2507 | R, TemplateArgs); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2508 | } |
| 2509 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2510 | bool Sema::UseArgumentDependentLookup(const CXXScopeSpec &SS, |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2511 | const LookupResult &R, |
| 2512 | bool HasTrailingLParen) { |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2513 | // Only when used directly as the postfix-expression of a call. |
| 2514 | if (!HasTrailingLParen) |
| 2515 | return false; |
| 2516 | |
| 2517 | // Never if a scope specifier was provided. |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2518 | if (SS.isSet()) |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2519 | return false; |
| 2520 | |
| 2521 | // Only in C++ or ObjC++. |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2522 | if (!getLangOptions().CPlusPlus) |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2523 | return false; |
| 2524 | |
| 2525 | // Turn off ADL when we find certain kinds of declarations during |
| 2526 | // normal lookup: |
| 2527 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 2528 | NamedDecl *D = *I; |
| 2529 | |
| 2530 | // C++0x [basic.lookup.argdep]p3: |
| 2531 | // -- a declaration of a class member |
| 2532 | // Since using decls preserve this property, we check this on the |
| 2533 | // original decl. |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2534 | if (D->isCXXClassMember()) |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2535 | return false; |
| 2536 | |
| 2537 | // C++0x [basic.lookup.argdep]p3: |
| 2538 | // -- a block-scope function declaration that is not a |
| 2539 | // using-declaration |
| 2540 | // NOTE: we also trigger this for function templates (in fact, we |
| 2541 | // don't check the decl type at all, since all other decl types |
| 2542 | // turn off ADL anyway). |
| 2543 | if (isa<UsingShadowDecl>(D)) |
| 2544 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 2545 | else if (D->getDeclContext()->isFunctionOrMethod()) |
| 2546 | return false; |
| 2547 | |
| 2548 | // C++0x [basic.lookup.argdep]p3: |
| 2549 | // -- a declaration that is neither a function or a function |
| 2550 | // template |
| 2551 | // And also for builtin functions. |
| 2552 | if (isa<FunctionDecl>(D)) { |
| 2553 | FunctionDecl *FDecl = cast<FunctionDecl>(D); |
| 2554 | |
| 2555 | // But also builtin functions. |
| 2556 | if (FDecl->getBuiltinID() && FDecl->isImplicit()) |
| 2557 | return false; |
| 2558 | } else if (!isa<FunctionTemplateDecl>(D)) |
| 2559 | return false; |
| 2560 | } |
| 2561 | |
| 2562 | return true; |
| 2563 | } |
| 2564 | |
| 2565 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2566 | /// Diagnoses obvious problems with the use of the given declaration |
| 2567 | /// as an expression. This is only actually called for lookups that |
| 2568 | /// were not overloaded, and it doesn't promise that the declaration |
| 2569 | /// will in fact be used. |
| 2570 | static bool CheckDeclInExpr(Sema &S, SourceLocation Loc, NamedDecl *D) { |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2571 | if (isa<TypedefNameDecl>(D)) { |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2572 | S.Diag(Loc, diag::err_unexpected_typedef) << D->getDeclName(); |
| 2573 | return true; |
| 2574 | } |
| 2575 | |
| 2576 | if (isa<ObjCInterfaceDecl>(D)) { |
| 2577 | S.Diag(Loc, diag::err_unexpected_interface) << D->getDeclName(); |
| 2578 | return true; |
| 2579 | } |
| 2580 | |
| 2581 | if (isa<NamespaceDecl>(D)) { |
| 2582 | S.Diag(Loc, diag::err_unexpected_namespace) << D->getDeclName(); |
| 2583 | return true; |
| 2584 | } |
| 2585 | |
| 2586 | return false; |
| 2587 | } |
| 2588 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2589 | ExprResult |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2590 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2591 | LookupResult &R, |
| 2592 | bool NeedsADL) { |
John McCall | 3a60c87 | 2009-12-08 22:45:53 +0000 | [diff] [blame] | 2593 | // If this is a single, fully-resolved result and we don't need ADL, |
| 2594 | // just build an ordinary singleton decl ref. |
Douglas Gregor | 4b4844f | 2010-01-29 17:15:43 +0000 | [diff] [blame] | 2595 | if (!NeedsADL && R.isSingleResult() && !R.getAsSingle<FunctionTemplateDecl>()) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2596 | return BuildDeclarationNameExpr(SS, R.getLookupNameInfo(), |
| 2597 | R.getFoundDecl()); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2598 | |
| 2599 | // We only need to check the declaration if there's exactly one |
| 2600 | // result, because in the overloaded case the results can only be |
| 2601 | // functions and function templates. |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2602 | if (R.isSingleResult() && |
| 2603 | CheckDeclInExpr(*this, R.getNameLoc(), R.getFoundDecl())) |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2604 | return ExprError(); |
| 2605 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2606 | // Otherwise, just build an unresolved lookup expression. Suppress |
| 2607 | // any lookup-related diagnostics; we'll hash these out later, when |
| 2608 | // we've picked a target. |
| 2609 | R.suppressDiagnostics(); |
| 2610 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2611 | UnresolvedLookupExpr *ULE |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2612 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2613 | SS.getWithLocInContext(Context), |
| 2614 | R.getLookupNameInfo(), |
Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 2615 | NeedsADL, R.isOverloadedResult(), |
| 2616 | R.begin(), R.end()); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2617 | |
| 2618 | return Owned(ULE); |
| 2619 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2620 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2621 | /// \brief Complete semantic analysis for a reference to the given declaration. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2622 | ExprResult |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2623 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2624 | const DeclarationNameInfo &NameInfo, |
| 2625 | NamedDecl *D) { |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2626 | assert(D && "Cannot refer to a NULL declaration"); |
John McCall | 283b901 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 2627 | assert(!isa<FunctionTemplateDecl>(D) && |
| 2628 | "Cannot refer unambiguously to a function template"); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2629 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2630 | SourceLocation Loc = NameInfo.getLoc(); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2631 | if (CheckDeclInExpr(*this, Loc, D)) |
| 2632 | return ExprError(); |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 2633 | |
Douglas Gregor | e7488b9 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2634 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) { |
| 2635 | // Specifically diagnose references to class templates that are missing |
| 2636 | // a template argument list. |
| 2637 | Diag(Loc, diag::err_template_decl_ref) |
| 2638 | << Template << SS.getRange(); |
| 2639 | Diag(Template->getLocation(), diag::note_template_decl_here); |
| 2640 | return ExprError(); |
| 2641 | } |
| 2642 | |
| 2643 | // Make sure that we're referring to a value. |
| 2644 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 2645 | if (!VD) { |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2646 | Diag(Loc, diag::err_ref_non_value) |
Douglas Gregor | e7488b9 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2647 | << D << SS.getRange(); |
John McCall | b48971d | 2009-12-18 18:35:10 +0000 | [diff] [blame] | 2648 | Diag(D->getLocation(), diag::note_declared_at); |
Douglas Gregor | e7488b9 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2649 | return ExprError(); |
| 2650 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2651 | |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2652 | // Check whether this declaration can be used. Note that we suppress |
| 2653 | // this check when we're going to perform argument-dependent lookup |
| 2654 | // on this function name, because this might not be the function |
| 2655 | // that overload resolution actually selects. |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2656 | if (DiagnoseUseOfDecl(VD, Loc)) |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2657 | return ExprError(); |
| 2658 | |
Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2659 | // Only create DeclRefExpr's for valid Decl's. |
| 2660 | if (VD->isInvalidDecl()) |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2661 | return ExprError(); |
| 2662 | |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2663 | // Handle members of anonymous structs and unions. If we got here, |
| 2664 | // and the reference is to a class member indirect field, then this |
| 2665 | // must be the subject of a pointer-to-member expression. |
| 2666 | if (IndirectFieldDecl *indirectField = dyn_cast<IndirectFieldDecl>(VD)) |
| 2667 | if (!indirectField->isCXXClassMember()) |
| 2668 | return BuildAnonymousStructUnionMemberReference(SS, NameInfo.getLoc(), |
| 2669 | indirectField); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2670 | |
Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 2671 | // If the identifier reference is inside a block, and it refers to a value |
| 2672 | // that is outside the block, create a BlockDeclRefExpr instead of a |
| 2673 | // DeclRefExpr. This ensures the value is treated as a copy-in snapshot when |
| 2674 | // the block is formed. |
Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2675 | // |
Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 2676 | // We do not do this for things like enum constants, global variables, etc, |
| 2677 | // as they do not get snapshotted. |
| 2678 | // |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2679 | switch (shouldCaptureValueReference(*this, NameInfo.getLoc(), VD)) { |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2680 | case CR_Error: |
| 2681 | return ExprError(); |
Mike Stump | 7dafa0d | 2010-01-05 02:56:35 +0000 | [diff] [blame] | 2682 | |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2683 | case CR_Capture: |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2684 | assert(!SS.isSet() && "referenced local variable with scope specifier?"); |
| 2685 | return BuildBlockDeclRefExpr(*this, VD, NameInfo, /*byref*/ false); |
| 2686 | |
| 2687 | case CR_CaptureByRef: |
| 2688 | assert(!SS.isSet() && "referenced local variable with scope specifier?"); |
| 2689 | return BuildBlockDeclRefExpr(*this, VD, NameInfo, /*byref*/ true); |
John McCall | f4cd4f9 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2690 | |
| 2691 | case CR_NoCapture: { |
| 2692 | // If this reference is not in a block or if the referenced |
| 2693 | // variable is within the block, create a normal DeclRefExpr. |
| 2694 | |
| 2695 | QualType type = VD->getType(); |
Daniel Dunbar | 7c2dc36 | 2011-02-10 18:29:28 +0000 | [diff] [blame] | 2696 | ExprValueKind valueKind = VK_RValue; |
John McCall | f4cd4f9 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2697 | |
| 2698 | switch (D->getKind()) { |
| 2699 | // Ignore all the non-ValueDecl kinds. |
| 2700 | #define ABSTRACT_DECL(kind) |
| 2701 | #define VALUE(type, base) |
| 2702 | #define DECL(type, base) \ |
| 2703 | case Decl::type: |
| 2704 | #include "clang/AST/DeclNodes.inc" |
| 2705 | llvm_unreachable("invalid value decl kind"); |
| 2706 | return ExprError(); |
| 2707 | |
| 2708 | // These shouldn't make it here. |
| 2709 | case Decl::ObjCAtDefsField: |
| 2710 | case Decl::ObjCIvar: |
| 2711 | llvm_unreachable("forming non-member reference to ivar?"); |
| 2712 | return ExprError(); |
| 2713 | |
| 2714 | // Enum constants are always r-values and never references. |
| 2715 | // Unresolved using declarations are dependent. |
| 2716 | case Decl::EnumConstant: |
| 2717 | case Decl::UnresolvedUsingValue: |
| 2718 | valueKind = VK_RValue; |
| 2719 | break; |
| 2720 | |
| 2721 | // Fields and indirect fields that got here must be for |
| 2722 | // pointer-to-member expressions; we just call them l-values for |
| 2723 | // internal consistency, because this subexpression doesn't really |
| 2724 | // exist in the high-level semantics. |
| 2725 | case Decl::Field: |
| 2726 | case Decl::IndirectField: |
| 2727 | assert(getLangOptions().CPlusPlus && |
| 2728 | "building reference to field in C?"); |
| 2729 | |
| 2730 | // These can't have reference type in well-formed programs, but |
| 2731 | // for internal consistency we do this anyway. |
| 2732 | type = type.getNonReferenceType(); |
| 2733 | valueKind = VK_LValue; |
| 2734 | break; |
| 2735 | |
| 2736 | // Non-type template parameters are either l-values or r-values |
| 2737 | // depending on the type. |
| 2738 | case Decl::NonTypeTemplateParm: { |
| 2739 | if (const ReferenceType *reftype = type->getAs<ReferenceType>()) { |
| 2740 | type = reftype->getPointeeType(); |
| 2741 | valueKind = VK_LValue; // even if the parameter is an r-value reference |
| 2742 | break; |
| 2743 | } |
| 2744 | |
| 2745 | // For non-references, we need to strip qualifiers just in case |
| 2746 | // the template parameter was declared as 'const int' or whatever. |
| 2747 | valueKind = VK_RValue; |
| 2748 | type = type.getUnqualifiedType(); |
| 2749 | break; |
| 2750 | } |
| 2751 | |
| 2752 | case Decl::Var: |
| 2753 | // In C, "extern void blah;" is valid and is an r-value. |
| 2754 | if (!getLangOptions().CPlusPlus && |
| 2755 | !type.hasQualifiers() && |
| 2756 | type->isVoidType()) { |
| 2757 | valueKind = VK_RValue; |
| 2758 | break; |
| 2759 | } |
| 2760 | // fallthrough |
| 2761 | |
| 2762 | case Decl::ImplicitParam: |
| 2763 | case Decl::ParmVar: |
| 2764 | // These are always l-values. |
| 2765 | valueKind = VK_LValue; |
| 2766 | type = type.getNonReferenceType(); |
| 2767 | break; |
| 2768 | |
| 2769 | case Decl::Function: { |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2770 | const FunctionType *fty = type->castAs<FunctionType>(); |
| 2771 | |
| 2772 | // If we're referring to a function with an __unknown_anytype |
| 2773 | // result type, make the entire expression __unknown_anytype. |
| 2774 | if (fty->getResultType() == Context.UnknownAnyTy) { |
| 2775 | type = Context.UnknownAnyTy; |
| 2776 | valueKind = VK_RValue; |
| 2777 | break; |
| 2778 | } |
| 2779 | |
John McCall | f4cd4f9 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2780 | // Functions are l-values in C++. |
| 2781 | if (getLangOptions().CPlusPlus) { |
| 2782 | valueKind = VK_LValue; |
| 2783 | break; |
| 2784 | } |
| 2785 | |
| 2786 | // C99 DR 316 says that, if a function type comes from a |
| 2787 | // function definition (without a prototype), that type is only |
| 2788 | // used for checking compatibility. Therefore, when referencing |
| 2789 | // the function, we pretend that we don't have the full function |
| 2790 | // type. |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2791 | if (!cast<FunctionDecl>(VD)->hasPrototype() && |
| 2792 | isa<FunctionProtoType>(fty)) |
| 2793 | type = Context.getFunctionNoProtoType(fty->getResultType(), |
| 2794 | fty->getExtInfo()); |
John McCall | f4cd4f9 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2795 | |
| 2796 | // Functions are r-values in C. |
| 2797 | valueKind = VK_RValue; |
| 2798 | break; |
| 2799 | } |
| 2800 | |
| 2801 | case Decl::CXXMethod: |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2802 | // If we're referring to a method with an __unknown_anytype |
| 2803 | // result type, make the entire expression __unknown_anytype. |
| 2804 | // This should only be possible with a type written directly. |
| 2805 | if (const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(VD->getType())) |
| 2806 | if (proto->getResultType() == Context.UnknownAnyTy) { |
| 2807 | type = Context.UnknownAnyTy; |
| 2808 | valueKind = VK_RValue; |
| 2809 | break; |
| 2810 | } |
| 2811 | |
John McCall | f4cd4f9 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2812 | // C++ methods are l-values if static, r-values if non-static. |
| 2813 | if (cast<CXXMethodDecl>(VD)->isStatic()) { |
| 2814 | valueKind = VK_LValue; |
| 2815 | break; |
| 2816 | } |
| 2817 | // fallthrough |
| 2818 | |
| 2819 | case Decl::CXXConversion: |
| 2820 | case Decl::CXXDestructor: |
| 2821 | case Decl::CXXConstructor: |
| 2822 | valueKind = VK_RValue; |
| 2823 | break; |
| 2824 | } |
| 2825 | |
| 2826 | return BuildDeclRefExpr(VD, type, valueKind, NameInfo, &SS); |
| 2827 | } |
| 2828 | |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2829 | } |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2830 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2831 | llvm_unreachable("unknown capture result"); |
| 2832 | return ExprError(); |
Chris Lattner | 17ed487 | 2006-11-20 04:58:19 +0000 | [diff] [blame] | 2833 | } |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2834 | |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2835 | ExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc, tok::TokenKind Kind) { |
Chris Lattner | 6307f19 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 2836 | PredefinedExpr::IdentType IT; |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2837 | |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2838 | switch (Kind) { |
Chris Lattner | 317e6ba | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 2839 | default: assert(0 && "Unknown simple primary expr!"); |
Chris Lattner | 6307f19 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 2840 | case tok::kw___func__: IT = PredefinedExpr::Func; break; // [C99 6.4.2.2] |
| 2841 | case tok::kw___FUNCTION__: IT = PredefinedExpr::Function; break; |
| 2842 | case tok::kw___PRETTY_FUNCTION__: IT = PredefinedExpr::PrettyFunction; break; |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2843 | } |
Chris Lattner | 317e6ba | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 2844 | |
Chris Lattner | a81a027 | 2008-01-12 08:14:25 +0000 | [diff] [blame] | 2845 | // Pre-defined identifiers are of type char[x], where x is the length of the |
| 2846 | // string. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2847 | |
Anders Carlsson | 2fb0824 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2848 | Decl *currentDecl = getCurFunctionOrMethodDecl(); |
Fariborz Jahanian | 9462744 | 2010-07-23 21:53:24 +0000 | [diff] [blame] | 2849 | if (!currentDecl && getCurBlock()) |
| 2850 | currentDecl = getCurBlock()->TheDecl; |
Anders Carlsson | 2fb0824 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2851 | if (!currentDecl) { |
Chris Lattner | f45c5ec | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 2852 | Diag(Loc, diag::ext_predef_outside_function); |
Anders Carlsson | 2fb0824 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2853 | currentDecl = Context.getTranslationUnitDecl(); |
Chris Lattner | f45c5ec | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 2854 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2855 | |
Anders Carlsson | 0b209a8 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2856 | QualType ResTy; |
| 2857 | if (cast<DeclContext>(currentDecl)->isDependentContext()) { |
| 2858 | ResTy = Context.DependentTy; |
| 2859 | } else { |
Anders Carlsson | 5bd8d19 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 2860 | unsigned Length = PredefinedExpr::ComputeName(IT, currentDecl).length(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2861 | |
Anders Carlsson | 0b209a8 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2862 | llvm::APInt LengthI(32, Length + 1); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2863 | ResTy = Context.CharTy.withConst(); |
Anders Carlsson | 0b209a8 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2864 | ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0); |
| 2865 | } |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2866 | return Owned(new (Context) PredefinedExpr(Loc, ResTy, IT)); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2867 | } |
| 2868 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2869 | ExprResult Sema::ActOnCharacterConstant(const Token &Tok) { |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 2870 | llvm::SmallString<16> CharBuffer; |
Douglas Gregor | dc970f0 | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2871 | bool Invalid = false; |
| 2872 | llvm::StringRef ThisTok = PP.getSpelling(Tok, CharBuffer, &Invalid); |
| 2873 | if (Invalid) |
| 2874 | return ExprError(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2875 | |
Benjamin Kramer | 0a1abd4 | 2010-02-27 13:44:12 +0000 | [diff] [blame] | 2876 | CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(), Tok.getLocation(), |
| 2877 | PP); |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 2878 | if (Literal.hadError()) |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2879 | return ExprError(); |
Chris Lattner | ef24b38 | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 2880 | |
Chris Lattner | c3847ba | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 2881 | QualType Ty; |
| 2882 | if (!getLangOptions().CPlusPlus) |
| 2883 | Ty = Context.IntTy; // 'x' and L'x' -> int in C. |
| 2884 | else if (Literal.isWide()) |
| 2885 | Ty = Context.WCharTy; // L'x' -> wchar_t in C++. |
Eli Friedman | eb1df70 | 2010-02-03 18:21:45 +0000 | [diff] [blame] | 2886 | else if (Literal.isMultiChar()) |
| 2887 | Ty = Context.IntTy; // 'wxyz' -> int in C++. |
Chris Lattner | c3847ba | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 2888 | else |
| 2889 | Ty = Context.CharTy; // 'x' -> char in C++ |
Chris Lattner | ef24b38 | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 2890 | |
Sebastian Redl | 20614a7 | 2009-01-20 22:23:13 +0000 | [diff] [blame] | 2891 | return Owned(new (Context) CharacterLiteral(Literal.getValue(), |
| 2892 | Literal.isWide(), |
Chris Lattner | c3847ba | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 2893 | Ty, Tok.getLocation())); |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 2894 | } |
| 2895 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2896 | ExprResult Sema::ActOnNumericConstant(const Token &Tok) { |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2897 | // 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] | 2898 | // cannot have a trigraph, escaped newline, radix prefix, or type suffix. |
| 2899 | if (Tok.getLength() == 1) { |
Chris Lattner | 9240b3e | 2009-01-26 22:36:52 +0000 | [diff] [blame] | 2900 | const char Val = PP.getSpellingOfSingleCharacterNumericConstant(Tok); |
Chris Lattner | c4c1819 | 2009-01-16 07:10:29 +0000 | [diff] [blame] | 2901 | unsigned IntSize = Context.Target.getIntWidth(); |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2902 | return Owned(IntegerLiteral::Create(Context, llvm::APInt(IntSize, Val-'0'), |
Steve Naroff | 5faaef7 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 2903 | Context.IntTy, Tok.getLocation())); |
Steve Naroff | f2fb89e | 2007-03-13 20:29:44 +0000 | [diff] [blame] | 2904 | } |
Ted Kremenek | e981418 | 2009-01-13 23:19:12 +0000 | [diff] [blame] | 2905 | |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 2906 | llvm::SmallString<512> IntegerBuffer; |
Chris Lattner | a1cf5f9 | 2008-09-30 20:53:45 +0000 | [diff] [blame] | 2907 | // Add padding so that NumericLiteralParser can overread by one character. |
| 2908 | IntegerBuffer.resize(Tok.getLength()+1); |
Steve Naroff | 8160ea2 | 2007-03-06 01:09:46 +0000 | [diff] [blame] | 2909 | const char *ThisTokBegin = &IntegerBuffer[0]; |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2910 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2911 | // Get the spelling of the token, which eliminates trigraphs, etc. |
Douglas Gregor | dc970f0 | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2912 | bool Invalid = false; |
| 2913 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid); |
| 2914 | if (Invalid) |
| 2915 | return ExprError(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2916 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2917 | NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
Steve Naroff | 451d8f16 | 2007-03-12 23:22:38 +0000 | [diff] [blame] | 2918 | Tok.getLocation(), PP); |
Steve Naroff | f2fb89e | 2007-03-13 20:29:44 +0000 | [diff] [blame] | 2919 | if (Literal.hadError) |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2920 | return ExprError(); |
| 2921 | |
Chris Lattner | 1c20a17 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2922 | Expr *Res; |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2923 | |
Chris Lattner | 1c20a17 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2924 | if (Literal.isFloatingLiteral()) { |
Chris Lattner | ec0a6d9 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2925 | QualType Ty; |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2926 | if (Literal.isFloat) |
Chris Lattner | ec0a6d9 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2927 | Ty = Context.FloatTy; |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2928 | else if (!Literal.isLong) |
Chris Lattner | ec0a6d9 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2929 | Ty = Context.DoubleTy; |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2930 | else |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 2931 | Ty = Context.LongDoubleTy; |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2932 | |
| 2933 | const llvm::fltSemantics &Format = Context.getFloatTypeSemantics(Ty); |
| 2934 | |
John McCall | 53b93a0 | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2935 | using llvm::APFloat; |
| 2936 | APFloat Val(Format); |
| 2937 | |
| 2938 | APFloat::opStatus result = Literal.GetFloatValue(Val); |
John McCall | 122c831 | 2009-12-24 11:09:08 +0000 | [diff] [blame] | 2939 | |
| 2940 | // Overflow is always an error, but underflow is only an error if |
| 2941 | // we underflowed to zero (APFloat reports denormals as underflow). |
| 2942 | if ((result & APFloat::opOverflow) || |
| 2943 | ((result & APFloat::opUnderflow) && Val.isZero())) { |
John McCall | 53b93a0 | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2944 | unsigned diagnostic; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2945 | llvm::SmallString<20> buffer; |
John McCall | 53b93a0 | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2946 | if (result & APFloat::opOverflow) { |
John McCall | 62abc94 | 2010-02-26 23:35:57 +0000 | [diff] [blame] | 2947 | diagnostic = diag::warn_float_overflow; |
John McCall | 53b93a0 | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2948 | APFloat::getLargest(Format).toString(buffer); |
| 2949 | } else { |
John McCall | 62abc94 | 2010-02-26 23:35:57 +0000 | [diff] [blame] | 2950 | diagnostic = diag::warn_float_underflow; |
John McCall | 53b93a0 | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2951 | APFloat::getSmallest(Format).toString(buffer); |
| 2952 | } |
| 2953 | |
| 2954 | Diag(Tok.getLocation(), diagnostic) |
| 2955 | << Ty |
| 2956 | << llvm::StringRef(buffer.data(), buffer.size()); |
| 2957 | } |
| 2958 | |
| 2959 | bool isExact = (result == APFloat::opOK); |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2960 | Res = FloatingLiteral::Create(Context, Val, isExact, Ty, Tok.getLocation()); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2961 | |
Peter Collingbourne | c77f85b | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2962 | if (Ty == Context.DoubleTy) { |
| 2963 | if (getLangOptions().SinglePrecisionConstants) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2964 | Res = ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast).take(); |
Peter Collingbourne | c77f85b | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2965 | } else if (getLangOptions().OpenCL && !getOpenCLOptions().cl_khr_fp64) { |
| 2966 | Diag(Tok.getLocation(), diag::warn_double_const_requires_fp64); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2967 | Res = ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast).take(); |
Peter Collingbourne | c77f85b | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2968 | } |
| 2969 | } |
Chris Lattner | 1c20a17 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2970 | } else if (!Literal.isIntegerLiteral()) { |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2971 | return ExprError(); |
Chris Lattner | 1c20a17 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2972 | } else { |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2973 | QualType Ty; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2974 | |
Neil Booth | ac582c5 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 2975 | // long long is a C99 feature. |
| 2976 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && |
Neil Booth | 4a1ee05 | 2007-08-29 22:13:52 +0000 | [diff] [blame] | 2977 | Literal.isLongLong) |
Neil Booth | ac582c5 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 2978 | Diag(Tok.getLocation(), diag::ext_longlong); |
| 2979 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2980 | // Get the value in the widest-possible width. |
Chris Lattner | 37e0587 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2981 | llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(), 0); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2982 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2983 | if (Literal.GetIntegerValue(ResultVal)) { |
| 2984 | // If this value didn't fit into uintmax_t, warn and force to ull. |
| 2985 | Diag(Tok.getLocation(), diag::warn_integer_too_large); |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2986 | Ty = Context.UnsignedLongLongTy; |
| 2987 | assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() && |
Chris Lattner | 37e0587 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2988 | "long long is not intmax_t?"); |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 2989 | } else { |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2990 | // If this value fits into a ULL, try to figure out what else it fits into |
| 2991 | // according to the rules of C99 6.4.4.1p5. |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2992 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2993 | // Octal, Hexadecimal, and integers with a U suffix are allowed to |
| 2994 | // be an unsigned int. |
| 2995 | bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10; |
| 2996 | |
| 2997 | // Check from smallest to largest, picking the smallest type we can. |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2998 | unsigned Width = 0; |
Chris Lattner | 7b939cf | 2007-08-23 21:58:08 +0000 | [diff] [blame] | 2999 | if (!Literal.isLong && !Literal.isLongLong) { |
| 3000 | // Are int/unsigned possibilities? |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 3001 | unsigned IntSize = Context.Target.getIntWidth(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 3002 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 3003 | // Does it fit in a unsigned int? |
| 3004 | if (ResultVal.isIntN(IntSize)) { |
| 3005 | // Does it fit in a signed int? |
| 3006 | if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0) |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3007 | Ty = Context.IntTy; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 3008 | else if (AllowUnsigned) |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3009 | Ty = Context.UnsignedIntTy; |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 3010 | Width = IntSize; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 3011 | } |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 3012 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 3013 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 3014 | // Are long/unsigned long possibilities? |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3015 | if (Ty.isNull() && !Literal.isLongLong) { |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 3016 | unsigned LongSize = Context.Target.getLongWidth(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 3017 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 3018 | // Does it fit in a unsigned long? |
| 3019 | if (ResultVal.isIntN(LongSize)) { |
| 3020 | // Does it fit in a signed long? |
| 3021 | if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0) |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3022 | Ty = Context.LongTy; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 3023 | else if (AllowUnsigned) |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3024 | Ty = Context.UnsignedLongTy; |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 3025 | Width = LongSize; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 3026 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 3027 | } |
| 3028 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 3029 | // Finally, check long long if needed. |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3030 | if (Ty.isNull()) { |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 3031 | unsigned LongLongSize = Context.Target.getLongLongWidth(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 3032 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 3033 | // Does it fit in a unsigned long long? |
| 3034 | if (ResultVal.isIntN(LongLongSize)) { |
| 3035 | // Does it fit in a signed long long? |
Francois Pichet | c3e73b3 | 2011-01-11 23:38:13 +0000 | [diff] [blame] | 3036 | // To be compatible with MSVC, hex integer literals ending with the |
| 3037 | // LL or i64 suffix are always signed in Microsoft mode. |
Francois Pichet | bf711d9 | 2011-01-11 12:23:00 +0000 | [diff] [blame] | 3038 | if (!Literal.isUnsigned && (ResultVal[LongLongSize-1] == 0 || |
| 3039 | (getLangOptions().Microsoft && Literal.isLongLong))) |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3040 | Ty = Context.LongLongTy; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 3041 | else if (AllowUnsigned) |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3042 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 3043 | Width = LongLongSize; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 3044 | } |
| 3045 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 3046 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 3047 | // If we still couldn't decide a type, we probably have something that |
| 3048 | // 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] | 3049 | if (Ty.isNull()) { |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 3050 | Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed); |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3051 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 3052 | Width = Context.Target.getLongLongWidth(); |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 3053 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 3054 | |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 3055 | if (ResultVal.getBitWidth() != Width) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 3056 | ResultVal = ResultVal.trunc(Width); |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 3057 | } |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 3058 | Res = IntegerLiteral::Create(Context, ResultVal, Ty, Tok.getLocation()); |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 3059 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 3060 | |
Chris Lattner | 1c20a17 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 3061 | // If this is an imaginary literal, create the ImaginaryLiteral wrapper. |
| 3062 | if (Literal.isImaginary) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3063 | Res = new (Context) ImaginaryLiteral(Res, |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3064 | Context.getComplexType(Res->getType())); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 3065 | |
| 3066 | return Owned(Res); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 3067 | } |
| 3068 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3069 | ExprResult Sema::ActOnParenExpr(SourceLocation L, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3070 | SourceLocation R, Expr *E) { |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3071 | assert((E != 0) && "ActOnParenExpr() missing expr"); |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 3072 | return Owned(new (Context) ParenExpr(L, R, E)); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 3073 | } |
| 3074 | |
Chandler Carruth | 62da79c | 2011-05-26 08:53:12 +0000 | [diff] [blame] | 3075 | static bool CheckVecStepTraitOperandType(Sema &S, QualType T, |
| 3076 | SourceLocation Loc, |
| 3077 | SourceRange ArgRange) { |
| 3078 | // [OpenCL 1.1 6.11.12] "The vec_step built-in function takes a built-in |
| 3079 | // scalar or vector data type argument..." |
| 3080 | // Every built-in scalar type (OpenCL 1.1 6.1.1) is either an arithmetic |
| 3081 | // type (C99 6.2.5p18) or void. |
| 3082 | if (!(T->isArithmeticType() || T->isVoidType() || T->isVectorType())) { |
| 3083 | S.Diag(Loc, diag::err_vecstep_non_scalar_vector_type) |
| 3084 | << T << ArgRange; |
| 3085 | return true; |
| 3086 | } |
| 3087 | |
| 3088 | assert((T->isVoidType() || !T->isIncompleteType()) && |
| 3089 | "Scalar types should always be complete"); |
| 3090 | return false; |
| 3091 | } |
| 3092 | |
Chandler Carruth | cea1aac | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 3093 | static bool CheckExtensionTraitOperandType(Sema &S, QualType T, |
| 3094 | SourceLocation Loc, |
| 3095 | SourceRange ArgRange, |
| 3096 | UnaryExprOrTypeTrait TraitKind) { |
| 3097 | // C99 6.5.3.4p1: |
| 3098 | if (T->isFunctionType()) { |
| 3099 | // alignof(function) is allowed as an extension. |
| 3100 | if (TraitKind == UETT_SizeOf) |
| 3101 | S.Diag(Loc, diag::ext_sizeof_function_type) << ArgRange; |
| 3102 | return false; |
| 3103 | } |
| 3104 | |
| 3105 | // Allow sizeof(void)/alignof(void) as an extension. |
| 3106 | if (T->isVoidType()) { |
| 3107 | S.Diag(Loc, diag::ext_sizeof_void_type) << TraitKind << ArgRange; |
| 3108 | return false; |
| 3109 | } |
| 3110 | |
| 3111 | return true; |
| 3112 | } |
| 3113 | |
| 3114 | static bool CheckObjCTraitOperandConstraints(Sema &S, QualType T, |
| 3115 | SourceLocation Loc, |
| 3116 | SourceRange ArgRange, |
| 3117 | UnaryExprOrTypeTrait TraitKind) { |
| 3118 | // Reject sizeof(interface) and sizeof(interface<proto>) in 64-bit mode. |
| 3119 | if (S.LangOpts.ObjCNonFragileABI && T->isObjCObjectType()) { |
| 3120 | S.Diag(Loc, diag::err_sizeof_nonfragile_interface) |
| 3121 | << T << (TraitKind == UETT_SizeOf) |
| 3122 | << ArgRange; |
| 3123 | return true; |
| 3124 | } |
| 3125 | |
| 3126 | return false; |
| 3127 | } |
| 3128 | |
Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3129 | /// \brief Check the constrains on expression operands to unary type expression |
| 3130 | /// and type traits. |
| 3131 | /// |
Chandler Carruth | 7c430c0 | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 3132 | /// Completes any types necessary and validates the constraints on the operand |
| 3133 | /// expression. The logic mostly mirrors the type-based overload, but may modify |
| 3134 | /// the expression as it completes the type for that expression through template |
| 3135 | /// instantiation, etc. |
Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3136 | bool Sema::CheckUnaryExprOrTypeTraitOperand(Expr *Op, |
| 3137 | UnaryExprOrTypeTrait ExprKind) { |
Chandler Carruth | 7c430c0 | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 3138 | QualType ExprTy = Op->getType(); |
| 3139 | |
| 3140 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 3141 | // the result is the size of the referenced type." |
| 3142 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 3143 | // result shall be the alignment of the referenced type." |
| 3144 | if (const ReferenceType *Ref = ExprTy->getAs<ReferenceType>()) |
| 3145 | ExprTy = Ref->getPointeeType(); |
| 3146 | |
| 3147 | if (ExprKind == UETT_VecStep) |
| 3148 | return CheckVecStepTraitOperandType(*this, ExprTy, Op->getExprLoc(), |
| 3149 | Op->getSourceRange()); |
| 3150 | |
| 3151 | // Whitelist some types as extensions |
| 3152 | if (!CheckExtensionTraitOperandType(*this, ExprTy, Op->getExprLoc(), |
| 3153 | Op->getSourceRange(), ExprKind)) |
| 3154 | return false; |
| 3155 | |
| 3156 | if (RequireCompleteExprType(Op, |
| 3157 | PDiag(diag::err_sizeof_alignof_incomplete_type) |
| 3158 | << ExprKind << Op->getSourceRange(), |
| 3159 | std::make_pair(SourceLocation(), PDiag(0)))) |
| 3160 | return true; |
| 3161 | |
| 3162 | // Completeing the expression's type may have changed it. |
| 3163 | ExprTy = Op->getType(); |
| 3164 | if (const ReferenceType *Ref = ExprTy->getAs<ReferenceType>()) |
| 3165 | ExprTy = Ref->getPointeeType(); |
| 3166 | |
| 3167 | if (CheckObjCTraitOperandConstraints(*this, ExprTy, Op->getExprLoc(), |
| 3168 | Op->getSourceRange(), ExprKind)) |
| 3169 | return true; |
| 3170 | |
Nico Weber | 0870deb | 2011-06-15 02:47:03 +0000 | [diff] [blame] | 3171 | if (ExprKind == UETT_SizeOf) { |
| 3172 | if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(Op->IgnoreParens())) { |
| 3173 | if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(DeclRef->getFoundDecl())) { |
| 3174 | QualType OType = PVD->getOriginalType(); |
| 3175 | QualType Type = PVD->getType(); |
| 3176 | if (Type->isPointerType() && OType->isArrayType()) { |
| 3177 | Diag(Op->getExprLoc(), diag::warn_sizeof_array_param) |
| 3178 | << Type << OType; |
| 3179 | Diag(PVD->getLocation(), diag::note_declared_at); |
| 3180 | } |
| 3181 | } |
| 3182 | } |
| 3183 | } |
| 3184 | |
Chandler Carruth | 7c430c0 | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 3185 | return false; |
Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3186 | } |
| 3187 | |
| 3188 | /// \brief Check the constraints on operands to unary expression and type |
| 3189 | /// traits. |
| 3190 | /// |
| 3191 | /// This will complete any types necessary, and validate the various constraints |
| 3192 | /// on those operands. |
| 3193 | /// |
Steve Naroff | 71b59a9 | 2007-06-04 22:22:31 +0000 | [diff] [blame] | 3194 | /// The UsualUnaryConversions() function is *not* called by this routine. |
Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3195 | /// C99 6.3.2.1p[2-4] all state: |
| 3196 | /// Except when it is the operand of the sizeof operator ... |
| 3197 | /// |
| 3198 | /// C++ [expr.sizeof]p4 |
| 3199 | /// The lvalue-to-rvalue, array-to-pointer, and function-to-pointer |
| 3200 | /// standard conversions are not applied to the operand of sizeof. |
| 3201 | /// |
| 3202 | /// This policy is followed for all of the unary trait expressions. |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3203 | bool Sema::CheckUnaryExprOrTypeTraitOperand(QualType exprType, |
| 3204 | SourceLocation OpLoc, |
| 3205 | SourceRange ExprRange, |
| 3206 | UnaryExprOrTypeTrait ExprKind) { |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3207 | if (exprType->isDependentType()) |
| 3208 | return false; |
| 3209 | |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 3210 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 3211 | // the result is the size of the referenced type." |
| 3212 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 3213 | // result shall be the alignment of the referenced type." |
| 3214 | if (const ReferenceType *Ref = exprType->getAs<ReferenceType>()) |
| 3215 | exprType = Ref->getPointeeType(); |
| 3216 | |
Chandler Carruth | 62da79c | 2011-05-26 08:53:12 +0000 | [diff] [blame] | 3217 | if (ExprKind == UETT_VecStep) |
| 3218 | return CheckVecStepTraitOperandType(*this, exprType, OpLoc, ExprRange); |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3219 | |
Chandler Carruth | cea1aac | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 3220 | // Whitelist some types as extensions |
| 3221 | if (!CheckExtensionTraitOperandType(*this, exprType, OpLoc, ExprRange, |
| 3222 | ExprKind)) |
Chris Lattner | b1355b1 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 3223 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3224 | |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3225 | if (RequireCompleteType(OpLoc, exprType, |
Douglas Gregor | 906db8a | 2009-12-15 16:44:32 +0000 | [diff] [blame] | 3226 | PDiag(diag::err_sizeof_alignof_incomplete_type) |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3227 | << ExprKind << ExprRange)) |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3228 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3229 | |
Chandler Carruth | cea1aac | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 3230 | if (CheckObjCTraitOperandConstraints(*this, exprType, OpLoc, ExprRange, |
| 3231 | ExprKind)) |
Chris Lattner | cd2a8c5 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 3232 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3233 | |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3234 | return false; |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 3235 | } |
| 3236 | |
Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3237 | static bool CheckAlignOfExpr(Sema &S, Expr *E) { |
Chris Lattner | 8dff017 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 3238 | E = E->IgnoreParens(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3239 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3240 | // alignof decl is always ok. |
Chris Lattner | 8dff017 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 3241 | if (isa<DeclRefExpr>(E)) |
| 3242 | return false; |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3243 | |
| 3244 | // Cannot know anything else if the expression is dependent. |
| 3245 | if (E->isTypeDependent()) |
| 3246 | return false; |
| 3247 | |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3248 | if (E->getBitField()) { |
Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3249 | S.Diag(E->getExprLoc(), diag::err_sizeof_alignof_bitfield) |
| 3250 | << 1 << E->getSourceRange(); |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3251 | return true; |
Chris Lattner | 8dff017 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 3252 | } |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3253 | |
| 3254 | // Alignment of a field access is always okay, so long as it isn't a |
| 3255 | // bit-field. |
| 3256 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Mike Stump | 212005c | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 3257 | if (isa<FieldDecl>(ME->getMemberDecl())) |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3258 | return false; |
| 3259 | |
Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3260 | return S.CheckUnaryExprOrTypeTraitOperand(E, UETT_AlignOf); |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3261 | } |
| 3262 | |
Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3263 | bool Sema::CheckVecStepExpr(Expr *E) { |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3264 | E = E->IgnoreParens(); |
| 3265 | |
| 3266 | // Cannot know anything else if the expression is dependent. |
| 3267 | if (E->isTypeDependent()) |
| 3268 | return false; |
| 3269 | |
Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3270 | return CheckUnaryExprOrTypeTraitOperand(E, UETT_VecStep); |
Chris Lattner | 8dff017 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 3271 | } |
| 3272 | |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3273 | /// \brief Build a sizeof or alignof expression given a type operand. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3274 | ExprResult |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3275 | Sema::CreateUnaryExprOrTypeTraitExpr(TypeSourceInfo *TInfo, |
| 3276 | SourceLocation OpLoc, |
| 3277 | UnaryExprOrTypeTrait ExprKind, |
| 3278 | SourceRange R) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3279 | if (!TInfo) |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3280 | return ExprError(); |
| 3281 | |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3282 | QualType T = TInfo->getType(); |
John McCall | 4c98fd8 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 3283 | |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3284 | if (!T->isDependentType() && |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3285 | CheckUnaryExprOrTypeTraitOperand(T, OpLoc, R, ExprKind)) |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3286 | return ExprError(); |
| 3287 | |
| 3288 | // 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] | 3289 | return Owned(new (Context) UnaryExprOrTypeTraitExpr(ExprKind, TInfo, |
| 3290 | Context.getSizeType(), |
| 3291 | OpLoc, R.getEnd())); |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3292 | } |
| 3293 | |
| 3294 | /// \brief Build a sizeof or alignof expression given an expression |
| 3295 | /// operand. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3296 | ExprResult |
Chandler Carruth | a923fb2 | 2011-05-29 07:32:14 +0000 | [diff] [blame] | 3297 | Sema::CreateUnaryExprOrTypeTraitExpr(Expr *E, SourceLocation OpLoc, |
| 3298 | UnaryExprOrTypeTrait ExprKind) { |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3299 | // Verify that the operand is valid. |
| 3300 | bool isInvalid = false; |
| 3301 | if (E->isTypeDependent()) { |
| 3302 | // Delay type-checking for type-dependent expressions. |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3303 | } else if (ExprKind == UETT_AlignOf) { |
Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3304 | isInvalid = CheckAlignOfExpr(*this, E); |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3305 | } else if (ExprKind == UETT_VecStep) { |
Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3306 | isInvalid = CheckVecStepExpr(E); |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3307 | } else if (E->getBitField()) { // C99 6.5.3.4p1. |
Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3308 | Diag(E->getExprLoc(), diag::err_sizeof_alignof_bitfield) << 0; |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3309 | isInvalid = true; |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 3310 | } else if (E->getType()->isPlaceholderType()) { |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 3311 | ExprResult PE = CheckPlaceholderExpr(E); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 3312 | if (PE.isInvalid()) return ExprError(); |
Chandler Carruth | a923fb2 | 2011-05-29 07:32:14 +0000 | [diff] [blame] | 3313 | return CreateUnaryExprOrTypeTraitExpr(PE.take(), OpLoc, ExprKind); |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3314 | } else { |
Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3315 | isInvalid = CheckUnaryExprOrTypeTraitOperand(E, UETT_SizeOf); |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3316 | } |
| 3317 | |
| 3318 | if (isInvalid) |
| 3319 | return ExprError(); |
| 3320 | |
| 3321 | // 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] | 3322 | return Owned(new (Context) UnaryExprOrTypeTraitExpr( |
Chandler Carruth | a923fb2 | 2011-05-29 07:32:14 +0000 | [diff] [blame] | 3323 | ExprKind, E, Context.getSizeType(), OpLoc, |
Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3324 | E->getSourceRange().getEnd())); |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3325 | } |
| 3326 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3327 | /// ActOnUnaryExprOrTypeTraitExpr - Handle @c sizeof(type) and @c sizeof @c |
| 3328 | /// expr and the same for @c alignof and @c __alignof |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 3329 | /// Note that the ArgRange is invalid if isType is false. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3330 | ExprResult |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3331 | Sema::ActOnUnaryExprOrTypeTraitExpr(SourceLocation OpLoc, |
| 3332 | UnaryExprOrTypeTrait ExprKind, bool isType, |
| 3333 | void *TyOrEx, const SourceRange &ArgRange) { |
Chris Lattner | 0d8b1a1 | 2006-11-20 04:34:45 +0000 | [diff] [blame] | 3334 | // If error parsing type, ignore. |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3335 | if (TyOrEx == 0) return ExprError(); |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 3336 | |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 3337 | if (isType) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3338 | TypeSourceInfo *TInfo; |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3339 | (void) GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrEx), &TInfo); |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3340 | return CreateUnaryExprOrTypeTraitExpr(TInfo, OpLoc, ExprKind, ArgRange); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3341 | } |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 3342 | |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3343 | Expr *ArgEx = (Expr *)TyOrEx; |
Chandler Carruth | a923fb2 | 2011-05-29 07:32:14 +0000 | [diff] [blame] | 3344 | ExprResult Result = CreateUnaryExprOrTypeTraitExpr(ArgEx, OpLoc, ExprKind); |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3345 | return move(Result); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 3346 | } |
| 3347 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3348 | static QualType CheckRealImagOperand(Sema &S, ExprResult &V, SourceLocation Loc, |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3349 | bool isReal) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3350 | if (V.get()->isTypeDependent()) |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3351 | return S.Context.DependentTy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3352 | |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3353 | // _Real and _Imag are only l-values for normal l-values. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3354 | if (V.get()->getObjectKind() != OK_Ordinary) { |
| 3355 | V = S.DefaultLvalueConversion(V.take()); |
| 3356 | if (V.isInvalid()) |
| 3357 | return QualType(); |
| 3358 | } |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3359 | |
Chris Lattner | e267f5d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 3360 | // These operators return the element type of a complex type. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3361 | if (const ComplexType *CT = V.get()->getType()->getAs<ComplexType>()) |
Chris Lattner | 30b5dd0 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 3362 | return CT->getElementType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3363 | |
Chris Lattner | e267f5d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 3364 | // Otherwise they pass through real integer and floating point types here. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3365 | if (V.get()->getType()->isArithmeticType()) |
| 3366 | return V.get()->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3367 | |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 3368 | // Test for placeholders. |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 3369 | ExprResult PR = S.CheckPlaceholderExpr(V.get()); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 3370 | if (PR.isInvalid()) return QualType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3371 | if (PR.get() != V.get()) { |
| 3372 | V = move(PR); |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3373 | return CheckRealImagOperand(S, V, Loc, isReal); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 3374 | } |
| 3375 | |
Chris Lattner | e267f5d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 3376 | // Reject anything else. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3377 | S.Diag(Loc, diag::err_realimag_invalid_type) << V.get()->getType() |
Chris Lattner | 709322b | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 3378 | << (isReal ? "__real" : "__imag"); |
Chris Lattner | e267f5d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 3379 | return QualType(); |
Chris Lattner | 30b5dd0 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 3380 | } |
| 3381 | |
| 3382 | |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 3383 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3384 | ExprResult |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3385 | Sema::ActOnPostfixUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3386 | tok::TokenKind Kind, Expr *Input) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3387 | UnaryOperatorKind Opc; |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 3388 | switch (Kind) { |
| 3389 | default: assert(0 && "Unknown unary op!"); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3390 | case tok::plusplus: Opc = UO_PostInc; break; |
| 3391 | case tok::minusminus: Opc = UO_PostDec; break; |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 3392 | } |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3393 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3394 | return BuildUnaryOp(S, OpLoc, Opc, Input); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 3395 | } |
| 3396 | |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3397 | /// Expressions of certain arbitrary types are forbidden by C from |
| 3398 | /// having l-value type. These are: |
| 3399 | /// - 'void', but not qualified void |
| 3400 | /// - function types |
| 3401 | /// |
| 3402 | /// The exact rule here is C99 6.3.2.1: |
| 3403 | /// An lvalue is an expression with an object type or an incomplete |
| 3404 | /// type other than void. |
| 3405 | static bool IsCForbiddenLValueType(ASTContext &C, QualType T) { |
| 3406 | return ((T->isVoidType() && !T.hasQualifiers()) || |
| 3407 | T->isFunctionType()); |
| 3408 | } |
| 3409 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3410 | ExprResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3411 | Sema::ActOnArraySubscriptExpr(Scope *S, Expr *Base, SourceLocation LLoc, |
| 3412 | Expr *Idx, SourceLocation RLoc) { |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3413 | // Since this might be a postfix expression, get rid of ParenListExprs. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3414 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3415 | if (Result.isInvalid()) return ExprError(); |
| 3416 | Base = Result.take(); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3417 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3418 | Expr *LHSExp = Base, *RHSExp = Idx; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3419 | |
Douglas Gregor | 40412ac | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 3420 | if (getLangOptions().CPlusPlus && |
Douglas Gregor | 7a77a6b | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 3421 | (LHSExp->isTypeDependent() || RHSExp->isTypeDependent())) { |
Douglas Gregor | 7a77a6b | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 3422 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3423 | Context.DependentTy, |
| 3424 | VK_LValue, OK_Ordinary, |
| 3425 | RLoc)); |
Douglas Gregor | 7a77a6b | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 3426 | } |
| 3427 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3428 | if (getLangOptions().CPlusPlus && |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3429 | (LHSExp->getType()->isRecordType() || |
Eli Friedman | 254a1a2 | 2008-12-15 22:34:21 +0000 | [diff] [blame] | 3430 | LHSExp->getType()->isEnumeralType() || |
| 3431 | RHSExp->getType()->isRecordType() || |
| 3432 | RHSExp->getType()->isEnumeralType())) { |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3433 | return CreateOverloadedArraySubscriptExpr(LLoc, RLoc, Base, Idx); |
Douglas Gregor | 40412ac | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 3434 | } |
| 3435 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3436 | return CreateBuiltinArraySubscriptExpr(Base, LLoc, Idx, RLoc); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 3437 | } |
| 3438 | |
| 3439 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3440 | ExprResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3441 | Sema::CreateBuiltinArraySubscriptExpr(Expr *Base, SourceLocation LLoc, |
| 3442 | Expr *Idx, SourceLocation RLoc) { |
| 3443 | Expr *LHSExp = Base; |
| 3444 | Expr *RHSExp = Idx; |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 3445 | |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3446 | // Perform default conversions. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3447 | if (!LHSExp->getType()->getAs<VectorType>()) { |
| 3448 | ExprResult Result = DefaultFunctionArrayLvalueConversion(LHSExp); |
| 3449 | if (Result.isInvalid()) |
| 3450 | return ExprError(); |
| 3451 | LHSExp = Result.take(); |
| 3452 | } |
| 3453 | ExprResult Result = DefaultFunctionArrayLvalueConversion(RHSExp); |
| 3454 | if (Result.isInvalid()) |
| 3455 | return ExprError(); |
| 3456 | RHSExp = Result.take(); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3457 | |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3458 | QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType(); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3459 | ExprValueKind VK = VK_LValue; |
| 3460 | ExprObjectKind OK = OK_Ordinary; |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 3461 | |
Steve Naroff | c1aadb1 | 2007-03-28 21:49:40 +0000 | [diff] [blame] | 3462 | // 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] | 3463 | // 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] | 3464 | // 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] | 3465 | // and index from the expression types. |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3466 | Expr *BaseExpr, *IndexExpr; |
| 3467 | QualType ResultType; |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3468 | if (LHSTy->isDependentType() || RHSTy->isDependentType()) { |
| 3469 | BaseExpr = LHSExp; |
| 3470 | IndexExpr = RHSExp; |
| 3471 | ResultType = Context.DependentTy; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3472 | } else if (const PointerType *PTy = LHSTy->getAs<PointerType>()) { |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3473 | BaseExpr = LHSExp; |
| 3474 | IndexExpr = RHSExp; |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3475 | ResultType = PTy->getPointeeType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3476 | } else if (const PointerType *PTy = RHSTy->getAs<PointerType>()) { |
Chris Lattner | aee0cfd | 2007-07-16 00:23:25 +0000 | [diff] [blame] | 3477 | // Handle the uncommon case of "123[Ptr]". |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3478 | BaseExpr = RHSExp; |
| 3479 | IndexExpr = LHSExp; |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3480 | ResultType = PTy->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3481 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3482 | LHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3483 | BaseExpr = LHSExp; |
| 3484 | IndexExpr = RHSExp; |
| 3485 | ResultType = PTy->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3486 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3487 | RHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3488 | // Handle the uncommon case of "123[Ptr]". |
| 3489 | BaseExpr = RHSExp; |
| 3490 | IndexExpr = LHSExp; |
| 3491 | ResultType = PTy->getPointeeType(); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3492 | } else if (const VectorType *VTy = LHSTy->getAs<VectorType>()) { |
Chris Lattner | 4197796 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 3493 | BaseExpr = LHSExp; // vectors: V[123] |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3494 | IndexExpr = RHSExp; |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3495 | VK = LHSExp->getValueKind(); |
| 3496 | if (VK != VK_RValue) |
| 3497 | OK = OK_VectorComponent; |
Nate Begeman | c1bf061 | 2009-01-18 00:45:31 +0000 | [diff] [blame] | 3498 | |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3499 | // FIXME: need to deal with const... |
| 3500 | ResultType = VTy->getElementType(); |
Eli Friedman | ab2784f | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3501 | } else if (LHSTy->isArrayType()) { |
| 3502 | // If we see an array that wasn't promoted by |
Douglas Gregor | b92a156 | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 3503 | // DefaultFunctionArrayLvalueConversion, it must be an array that |
Eli Friedman | ab2784f | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3504 | // wasn't promoted because of the C90 rule that doesn't |
| 3505 | // allow promoting non-lvalue arrays. Warn, then |
| 3506 | // force the promotion here. |
| 3507 | Diag(LHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 3508 | LHSExp->getSourceRange(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3509 | LHSExp = ImpCastExprToType(LHSExp, Context.getArrayDecayedType(LHSTy), |
| 3510 | CK_ArrayToPointerDecay).take(); |
Eli Friedman | ab2784f | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3511 | LHSTy = LHSExp->getType(); |
| 3512 | |
| 3513 | BaseExpr = LHSExp; |
| 3514 | IndexExpr = RHSExp; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3515 | ResultType = LHSTy->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | ab2784f | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3516 | } else if (RHSTy->isArrayType()) { |
| 3517 | // Same as previous, except for 123[f().a] case |
| 3518 | Diag(RHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 3519 | RHSExp->getSourceRange(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3520 | RHSExp = ImpCastExprToType(RHSExp, Context.getArrayDecayedType(RHSTy), |
| 3521 | CK_ArrayToPointerDecay).take(); |
Eli Friedman | ab2784f | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3522 | RHSTy = RHSExp->getType(); |
| 3523 | |
| 3524 | BaseExpr = RHSExp; |
| 3525 | IndexExpr = LHSExp; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3526 | ResultType = RHSTy->getAs<PointerType>()->getPointeeType(); |
Steve Naroff | b309644 | 2007-06-09 03:47:53 +0000 | [diff] [blame] | 3527 | } else { |
Chris Lattner | 003af24 | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 3528 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_value) |
| 3529 | << LHSExp->getSourceRange() << RHSExp->getSourceRange()); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3530 | } |
Steve Naroff | c1aadb1 | 2007-03-28 21:49:40 +0000 | [diff] [blame] | 3531 | // C99 6.5.2.1p1 |
Douglas Gregor | 5cc2c8b | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 3532 | if (!IndexExpr->getType()->isIntegerType() && !IndexExpr->isTypeDependent()) |
Chris Lattner | 003af24 | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 3533 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_not_integer) |
| 3534 | << IndexExpr->getSourceRange()); |
Steve Naroff | b29cdd5 | 2007-07-10 18:23:31 +0000 | [diff] [blame] | 3535 | |
Daniel Dunbar | 4782a6e | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 3536 | if ((IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_S) || |
Sam Weinig | b7608d7 | 2009-09-14 20:14:57 +0000 | [diff] [blame] | 3537 | IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U)) |
| 3538 | && !IndexExpr->isTypeDependent()) |
Sam Weinig | 914244e | 2009-09-14 01:58:58 +0000 | [diff] [blame] | 3539 | Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange(); |
| 3540 | |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3541 | // 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] | 3542 | // C++ [expr.sub]p1: The type "T" shall be a completely-defined object |
| 3543 | // type. Note that Functions are not objects, and that (in C99 parlance) |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3544 | // incomplete types are not object types. |
| 3545 | if (ResultType->isFunctionType()) { |
| 3546 | Diag(BaseExpr->getLocStart(), diag::err_subscript_function_type) |
| 3547 | << ResultType << BaseExpr->getSourceRange(); |
| 3548 | return ExprError(); |
| 3549 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3550 | |
Abramo Bagnara | 3aabb4b | 2010-09-13 06:50:07 +0000 | [diff] [blame] | 3551 | if (ResultType->isVoidType() && !getLangOptions().CPlusPlus) { |
| 3552 | // GNU extension: subscripting on pointer to void |
| 3553 | Diag(LLoc, diag::ext_gnu_void_ptr) |
| 3554 | << BaseExpr->getSourceRange(); |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3555 | |
| 3556 | // C forbids expressions of unqualified void type from being l-values. |
| 3557 | // See IsCForbiddenLValueType. |
| 3558 | if (!ResultType.hasQualifiers()) VK = VK_RValue; |
Abramo Bagnara | 3aabb4b | 2010-09-13 06:50:07 +0000 | [diff] [blame] | 3559 | } else if (!ResultType->isDependentType() && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3560 | RequireCompleteType(LLoc, ResultType, |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3561 | PDiag(diag::err_subscript_incomplete_type) |
| 3562 | << BaseExpr->getSourceRange())) |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3563 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3564 | |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3565 | // Diagnose bad cases where we step over interface counts. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3566 | if (ResultType->isObjCObjectType() && LangOpts.ObjCNonFragileABI) { |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3567 | Diag(LLoc, diag::err_subscript_nonfragile_interface) |
| 3568 | << ResultType << BaseExpr->getSourceRange(); |
| 3569 | return ExprError(); |
| 3570 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3571 | |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3572 | assert(VK == VK_RValue || LangOpts.CPlusPlus || |
| 3573 | !IsCForbiddenLValueType(Context, ResultType)); |
| 3574 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3575 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3576 | ResultType, VK, OK, RLoc)); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 3577 | } |
| 3578 | |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3579 | /// Check an ext-vector component access expression. |
| 3580 | /// |
| 3581 | /// VK should be set in advance to the value kind of the base |
| 3582 | /// expression. |
| 3583 | static QualType |
| 3584 | CheckExtVectorComponent(Sema &S, QualType baseType, ExprValueKind &VK, |
| 3585 | SourceLocation OpLoc, const IdentifierInfo *CompName, |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3586 | SourceLocation CompLoc) { |
Daniel Dunbar | c042940 | 2009-10-18 02:09:38 +0000 | [diff] [blame] | 3587 | // FIXME: Share logic with ExtVectorElementExpr::containsDuplicateElements, |
| 3588 | // see FIXME there. |
| 3589 | // |
| 3590 | // FIXME: This logic can be greatly simplified by splitting it along |
| 3591 | // halving/not halving and reworking the component checking. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3592 | const ExtVectorType *vecType = baseType->getAs<ExtVectorType>(); |
Nate Begeman | f322eab | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 3593 | |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3594 | // The vector accessor can't exceed the number of elements. |
Daniel Dunbar | 2c422dc9 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 3595 | const char *compStr = CompName->getNameStart(); |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3596 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3597 | // 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] | 3598 | // special names that indicate a subset of exactly half the elements are |
| 3599 | // to be selected. |
| 3600 | bool HalvingSwizzle = false; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3601 | |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3602 | // This flag determines whether or not CompName has an 's' char prefix, |
| 3603 | // 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] | 3604 | bool HexSwizzle = *compStr == 's' || *compStr == 'S'; |
Nate Begeman | f322eab | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 3605 | |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3606 | bool HasRepeated = false; |
| 3607 | bool HasIndex[16] = {}; |
| 3608 | |
| 3609 | int Idx; |
| 3610 | |
Nate Begeman | f322eab | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 3611 | // Check that we've found one of the special components, or that the component |
| 3612 | // names must come from the same set. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3613 | if (!strcmp(compStr, "hi") || !strcmp(compStr, "lo") || |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3614 | !strcmp(compStr, "even") || !strcmp(compStr, "odd")) { |
| 3615 | HalvingSwizzle = true; |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3616 | } else if (!HexSwizzle && |
| 3617 | (Idx = vecType->getPointAccessorIdx(*compStr)) != -1) { |
| 3618 | do { |
| 3619 | if (HasIndex[Idx]) HasRepeated = true; |
| 3620 | HasIndex[Idx] = true; |
Chris Lattner | 7e152db | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 3621 | compStr++; |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3622 | } while (*compStr && (Idx = vecType->getPointAccessorIdx(*compStr)) != -1); |
| 3623 | } else { |
| 3624 | if (HexSwizzle) compStr++; |
| 3625 | while ((Idx = vecType->getNumericAccessorIdx(*compStr)) != -1) { |
| 3626 | if (HasIndex[Idx]) HasRepeated = true; |
| 3627 | HasIndex[Idx] = true; |
Chris Lattner | 7e152db | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 3628 | compStr++; |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3629 | } |
Chris Lattner | 7e152db | 2007-08-02 22:33:49 +0000 | [diff] [blame] | 3630 | } |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3631 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3632 | if (!HalvingSwizzle && *compStr) { |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3633 | // We didn't get to the end of the string. This means the component names |
| 3634 | // 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] | 3635 | S.Diag(OpLoc, diag::err_ext_vector_component_name_illegal) |
Benjamin Kramer | e8394df | 2010-08-11 14:47:12 +0000 | [diff] [blame] | 3636 | << llvm::StringRef(compStr, 1) << SourceRange(CompLoc); |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3637 | return QualType(); |
| 3638 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3639 | |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3640 | // Ensure no component accessor exceeds the width of the vector type it |
| 3641 | // operates on. |
| 3642 | if (!HalvingSwizzle) { |
Daniel Dunbar | 2c422dc9 | 2009-10-18 20:26:12 +0000 | [diff] [blame] | 3643 | compStr = CompName->getNameStart(); |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3644 | |
| 3645 | if (HexSwizzle) |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3646 | compStr++; |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3647 | |
| 3648 | while (*compStr) { |
| 3649 | if (!vecType->isAccessorWithinNumElements(*compStr++)) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3650 | S.Diag(OpLoc, diag::err_ext_vector_component_exceeds_length) |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3651 | << baseType << SourceRange(CompLoc); |
| 3652 | return QualType(); |
| 3653 | } |
| 3654 | } |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3655 | } |
Nate Begeman | f322eab | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 3656 | |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3657 | // 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] | 3658 | // The vector type is implied by the component accessor. For example, |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3659 | // 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] | 3660 | // vec4.s0 is a float, vec4.s23 is a vec3, etc. |
Nate Begeman | f322eab | 2008-05-09 06:41:27 +0000 | [diff] [blame] | 3661 | // vec4.hi, vec4.lo, vec4.e, and vec4.o all return vec2. |
Nate Begeman | ac8183a | 2009-12-15 18:13:04 +0000 | [diff] [blame] | 3662 | unsigned CompSize = HalvingSwizzle ? (vecType->getNumElements() + 1) / 2 |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3663 | : CompName->getLength(); |
Nate Begeman | bb70bf6 | 2009-01-18 01:47:54 +0000 | [diff] [blame] | 3664 | if (HexSwizzle) |
| 3665 | CompSize--; |
| 3666 | |
Steve Naroff | f8fd09e | 2007-07-27 22:15:19 +0000 | [diff] [blame] | 3667 | if (CompSize == 1) |
| 3668 | return vecType->getElementType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3669 | |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3670 | if (HasRepeated) VK = VK_RValue; |
| 3671 | |
| 3672 | QualType VT = S.Context.getExtVectorType(vecType->getElementType(), CompSize); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3673 | // Now look up the TypeDefDecl from the vector type. Without this, |
Nate Begeman | ce4d7fc | 2008-04-18 23:10:10 +0000 | [diff] [blame] | 3674 | // diagostics look bad. We want extended vector types to appear built-in. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3675 | for (unsigned i = 0, E = S.ExtVectorDecls.size(); i != E; ++i) { |
| 3676 | if (S.ExtVectorDecls[i]->getUnderlyingType() == VT) |
| 3677 | return S.Context.getTypedefType(S.ExtVectorDecls[i]); |
Steve Naroff | ddf5a1d | 2007-07-29 16:33:31 +0000 | [diff] [blame] | 3678 | } |
| 3679 | 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] | 3680 | } |
| 3681 | |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3682 | static Decl *FindGetterSetterNameDeclFromProtocolList(const ObjCProtocolDecl*PDecl, |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 3683 | IdentifierInfo *Member, |
Douglas Gregor | bcced4e | 2009-04-09 21:40:53 +0000 | [diff] [blame] | 3684 | const Selector &Sel, |
| 3685 | ASTContext &Context) { |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3686 | if (Member) |
| 3687 | if (ObjCPropertyDecl *PD = PDecl->FindPropertyDeclaration(Member)) |
| 3688 | return PD; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 3689 | if (ObjCMethodDecl *OMD = PDecl->getInstanceMethod(Sel)) |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3690 | return OMD; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3691 | |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3692 | for (ObjCProtocolDecl::protocol_iterator I = PDecl->protocol_begin(), |
| 3693 | E = PDecl->protocol_end(); I != E; ++I) { |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3694 | if (Decl *D = FindGetterSetterNameDeclFromProtocolList(*I, Member, Sel, |
| 3695 | Context)) |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3696 | return D; |
| 3697 | } |
| 3698 | return 0; |
| 3699 | } |
| 3700 | |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3701 | static Decl *FindGetterSetterNameDecl(const ObjCObjectPointerType *QIdTy, |
| 3702 | IdentifierInfo *Member, |
| 3703 | const Selector &Sel, |
| 3704 | ASTContext &Context) { |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3705 | // Check protocols on qualified interfaces. |
| 3706 | Decl *GDecl = 0; |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3707 | for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(), |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3708 | E = QIdTy->qual_end(); I != E; ++I) { |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3709 | if (Member) |
| 3710 | if (ObjCPropertyDecl *PD = (*I)->FindPropertyDeclaration(Member)) { |
| 3711 | GDecl = PD; |
| 3712 | break; |
| 3713 | } |
| 3714 | // 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] | 3715 | if (ObjCMethodDecl *OMD = (*I)->getInstanceMethod(Sel)) { |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3716 | GDecl = OMD; |
| 3717 | break; |
| 3718 | } |
| 3719 | } |
| 3720 | if (!GDecl) { |
Steve Naroff | fb4330f | 2009-06-17 22:40:22 +0000 | [diff] [blame] | 3721 | for (ObjCObjectPointerType::qual_iterator I = QIdTy->qual_begin(), |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3722 | E = QIdTy->qual_end(); I != E; ++I) { |
| 3723 | // Search in the protocol-qualifier list of current protocol. |
Fariborz Jahanian | f3f903a | 2010-10-11 21:29:12 +0000 | [diff] [blame] | 3724 | GDecl = FindGetterSetterNameDeclFromProtocolList(*I, Member, Sel, |
| 3725 | Context); |
Fariborz Jahanian | d302bbd0 | 2009-03-19 18:15:34 +0000 | [diff] [blame] | 3726 | if (GDecl) |
| 3727 | return GDecl; |
| 3728 | } |
| 3729 | } |
| 3730 | return GDecl; |
| 3731 | } |
Chris Lattner | 4bf74fd | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 3732 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3733 | ExprResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3734 | Sema::ActOnDependentMemberExpr(Expr *BaseExpr, QualType BaseType, |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3735 | bool IsArrow, SourceLocation OpLoc, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3736 | const CXXScopeSpec &SS, |
| 3737 | NamedDecl *FirstQualifierInScope, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3738 | const DeclarationNameInfo &NameInfo, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3739 | const TemplateArgumentListInfo *TemplateArgs) { |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3740 | // Even in dependent contexts, try to diagnose base expressions with |
| 3741 | // obviously wrong types, e.g.: |
| 3742 | // |
| 3743 | // T* t; |
| 3744 | // t.f; |
| 3745 | // |
| 3746 | // In Obj-C++, however, the above expression is valid, since it could be |
| 3747 | // accessing the 'f' property if T is an Obj-C interface. The extra check |
| 3748 | // allows this, while still reporting an error if T is a struct pointer. |
| 3749 | if (!IsArrow) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3750 | const PointerType *PT = BaseType->getAs<PointerType>(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3751 | if (PT && (!getLangOptions().ObjC1 || |
| 3752 | PT->getPointeeType()->isRecordType())) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3753 | assert(BaseExpr && "cannot happen with implicit member accesses"); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3754 | Diag(NameInfo.getLoc(), diag::err_typecheck_member_reference_struct_union) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3755 | << BaseType << BaseExpr->getSourceRange(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3756 | return ExprError(); |
| 3757 | } |
| 3758 | } |
| 3759 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3760 | assert(BaseType->isDependentType() || |
| 3761 | NameInfo.getName().isDependentName() || |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 3762 | isDependentScopeSpecifier(SS)); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3763 | |
| 3764 | // Get the type being accessed in BaseType. If this is an arrow, the BaseExpr |
| 3765 | // must have pointer type, and the accessed type is the pointee. |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3766 | return Owned(CXXDependentScopeMemberExpr::Create(Context, BaseExpr, BaseType, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3767 | IsArrow, OpLoc, |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 3768 | SS.getWithLocInContext(Context), |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3769 | FirstQualifierInScope, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3770 | NameInfo, TemplateArgs)); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3771 | } |
| 3772 | |
| 3773 | /// We know that the given qualified member reference points only to |
| 3774 | /// declarations which do not belong to the static type of the base |
| 3775 | /// expression. Diagnose the problem. |
| 3776 | static void DiagnoseQualifiedMemberReference(Sema &SemaRef, |
| 3777 | Expr *BaseExpr, |
| 3778 | QualType BaseType, |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 3779 | const CXXScopeSpec &SS, |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 3780 | NamedDecl *rep, |
| 3781 | const DeclarationNameInfo &nameInfo) { |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 3782 | // If this is an implicit member access, use a different set of |
| 3783 | // diagnostics. |
| 3784 | if (!BaseExpr) |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 3785 | return DiagnoseInstanceReference(SemaRef, SS, rep, nameInfo); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3786 | |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 3787 | SemaRef.Diag(nameInfo.getLoc(), diag::err_qualified_member_of_unrelated) |
| 3788 | << SS.getRange() << rep << BaseType; |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3789 | } |
| 3790 | |
| 3791 | // Check whether the declarations we found through a nested-name |
| 3792 | // specifier in a member expression are actually members of the base |
| 3793 | // type. The restriction here is: |
| 3794 | // |
| 3795 | // C++ [expr.ref]p2: |
| 3796 | // ... In these cases, the id-expression shall name a |
| 3797 | // member of the class or of one of its base classes. |
| 3798 | // |
| 3799 | // So it's perfectly legitimate for the nested-name specifier to name |
| 3800 | // an unrelated class, and for us to find an overload set including |
| 3801 | // decls from classes which are not superclasses, as long as the decl |
| 3802 | // we actually pick through overload resolution is from a superclass. |
| 3803 | bool Sema::CheckQualifiedMemberReference(Expr *BaseExpr, |
| 3804 | QualType BaseType, |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 3805 | const CXXScopeSpec &SS, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3806 | const LookupResult &R) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3807 | const RecordType *BaseRT = BaseType->getAs<RecordType>(); |
| 3808 | if (!BaseRT) { |
| 3809 | // We can't check this yet because the base type is still |
| 3810 | // dependent. |
| 3811 | assert(BaseType->isDependentType()); |
| 3812 | return false; |
| 3813 | } |
| 3814 | CXXRecordDecl *BaseRecord = cast<CXXRecordDecl>(BaseRT->getDecl()); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3815 | |
| 3816 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3817 | // If this is an implicit member reference and we find a |
| 3818 | // non-instance member, it's not an error. |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 3819 | if (!BaseExpr && !(*I)->isCXXInstanceMember()) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3820 | return false; |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3821 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3822 | // 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] | 3823 | DeclContext *DC = (*I)->getDeclContext(); |
| 3824 | while (DC->isTransparentContext()) |
| 3825 | DC = DC->getParent(); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3826 | |
Douglas Gregor | a9c3e82 | 2010-07-28 22:27:52 +0000 | [diff] [blame] | 3827 | if (!DC->isRecord()) |
| 3828 | continue; |
| 3829 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3830 | llvm::SmallPtrSet<CXXRecordDecl*,4> MemberRecord; |
Eli Friedman | 7530049 | 2010-07-27 20:51:02 +0000 | [diff] [blame] | 3831 | MemberRecord.insert(cast<CXXRecordDecl>(DC)->getCanonicalDecl()); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3832 | |
| 3833 | if (!IsProvablyNotDerivedFrom(*this, BaseRecord, MemberRecord)) |
| 3834 | return false; |
| 3835 | } |
| 3836 | |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 3837 | DiagnoseQualifiedMemberReference(*this, BaseExpr, BaseType, SS, |
| 3838 | R.getRepresentativeDecl(), |
| 3839 | R.getLookupNameInfo()); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3840 | return true; |
| 3841 | } |
| 3842 | |
| 3843 | static bool |
| 3844 | LookupMemberExprInRecord(Sema &SemaRef, LookupResult &R, |
| 3845 | SourceRange BaseRange, const RecordType *RTy, |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 3846 | SourceLocation OpLoc, CXXScopeSpec &SS, |
| 3847 | bool HasTemplateArgs) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3848 | RecordDecl *RDecl = RTy->getDecl(); |
| 3849 | if (SemaRef.RequireCompleteType(OpLoc, QualType(RTy, 0), |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 3850 | SemaRef.PDiag(diag::err_typecheck_incomplete_tag) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3851 | << BaseRange)) |
| 3852 | return true; |
| 3853 | |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 3854 | if (HasTemplateArgs) { |
| 3855 | // LookupTemplateName doesn't expect these both to exist simultaneously. |
| 3856 | QualType ObjectType = SS.isSet() ? QualType() : QualType(RTy, 0); |
| 3857 | |
| 3858 | bool MOUS; |
| 3859 | SemaRef.LookupTemplateName(R, 0, SS, ObjectType, false, MOUS); |
| 3860 | return false; |
| 3861 | } |
| 3862 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3863 | DeclContext *DC = RDecl; |
| 3864 | if (SS.isSet()) { |
| 3865 | // If the member name was a qualified-id, look into the |
| 3866 | // nested-name-specifier. |
| 3867 | DC = SemaRef.computeDeclContext(SS, false); |
| 3868 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 3869 | if (SemaRef.RequireCompleteDeclContext(SS, DC)) { |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 3870 | SemaRef.Diag(SS.getRange().getEnd(), diag::err_typecheck_incomplete_tag) |
| 3871 | << SS.getRange() << DC; |
| 3872 | return true; |
| 3873 | } |
| 3874 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3875 | assert(DC && "Cannot handle non-computable dependent contexts in lookup"); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3876 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3877 | if (!isa<TypeDecl>(DC)) { |
| 3878 | SemaRef.Diag(R.getNameLoc(), diag::err_qualified_member_nonclass) |
| 3879 | << DC << SS.getRange(); |
| 3880 | return true; |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3881 | } |
| 3882 | } |
| 3883 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3884 | // The record definition is complete, now look up the member. |
| 3885 | SemaRef.LookupQualifiedName(R, DC); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3886 | |
Douglas Gregor | af2bd47 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 3887 | if (!R.empty()) |
| 3888 | return false; |
| 3889 | |
| 3890 | // We didn't find anything with the given name, so try to correct |
| 3891 | // for typos. |
| 3892 | DeclarationName Name = R.getLookupName(); |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 3893 | if (SemaRef.CorrectTypo(R, 0, &SS, DC, false, Sema::CTC_MemberLookup) && |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 3894 | !R.empty() && |
Douglas Gregor | af2bd47 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 3895 | (isa<ValueDecl>(*R.begin()) || isa<FunctionTemplateDecl>(*R.begin()))) { |
| 3896 | SemaRef.Diag(R.getNameLoc(), diag::err_no_member_suggest) |
| 3897 | << Name << DC << R.getLookupName() << SS.getRange() |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3898 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 3899 | R.getLookupName().getAsString()); |
Douglas Gregor | 6da8362 | 2010-01-07 00:17:44 +0000 | [diff] [blame] | 3900 | if (NamedDecl *ND = R.getAsSingle<NamedDecl>()) |
| 3901 | SemaRef.Diag(ND->getLocation(), diag::note_previous_decl) |
| 3902 | << ND->getDeclName(); |
Douglas Gregor | af2bd47 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 3903 | return false; |
| 3904 | } else { |
| 3905 | R.clear(); |
Douglas Gregor | c048c52 | 2010-06-29 19:27:42 +0000 | [diff] [blame] | 3906 | R.setLookupName(Name); |
Douglas Gregor | af2bd47 | 2009-12-31 07:42:17 +0000 | [diff] [blame] | 3907 | } |
| 3908 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3909 | return false; |
| 3910 | } |
| 3911 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3912 | ExprResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3913 | Sema::BuildMemberReferenceExpr(Expr *Base, QualType BaseType, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3914 | SourceLocation OpLoc, bool IsArrow, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 3915 | CXXScopeSpec &SS, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3916 | NamedDecl *FirstQualifierInScope, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3917 | const DeclarationNameInfo &NameInfo, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3918 | const TemplateArgumentListInfo *TemplateArgs) { |
John McCall | cd4b477 | 2009-12-02 03:53:29 +0000 | [diff] [blame] | 3919 | if (BaseType->isDependentType() || |
| 3920 | (SS.isSet() && isDependentScopeSpecifier(SS))) |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3921 | return ActOnDependentMemberExpr(Base, BaseType, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3922 | IsArrow, OpLoc, |
| 3923 | SS, FirstQualifierInScope, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3924 | NameInfo, TemplateArgs); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3925 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3926 | LookupResult R(*this, NameInfo, LookupMemberName); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3927 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3928 | // Implicit member accesses. |
| 3929 | if (!Base) { |
| 3930 | QualType RecordTy = BaseType; |
| 3931 | if (IsArrow) RecordTy = RecordTy->getAs<PointerType>()->getPointeeType(); |
| 3932 | if (LookupMemberExprInRecord(*this, R, SourceRange(), |
| 3933 | RecordTy->getAs<RecordType>(), |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 3934 | OpLoc, SS, TemplateArgs != 0)) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3935 | return ExprError(); |
| 3936 | |
| 3937 | // Explicit member accesses. |
| 3938 | } else { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3939 | ExprResult BaseResult = Owned(Base); |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3940 | ExprResult Result = |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3941 | LookupMemberExpr(R, BaseResult, IsArrow, OpLoc, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 3942 | SS, /*ObjCImpDecl*/ 0, TemplateArgs != 0); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3943 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3944 | if (BaseResult.isInvalid()) |
| 3945 | return ExprError(); |
| 3946 | Base = BaseResult.take(); |
| 3947 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3948 | if (Result.isInvalid()) { |
| 3949 | Owned(Base); |
| 3950 | return ExprError(); |
| 3951 | } |
| 3952 | |
| 3953 | if (Result.get()) |
| 3954 | return move(Result); |
Sebastian Redl | fa1f70f | 2010-05-07 09:25:11 +0000 | [diff] [blame] | 3955 | |
| 3956 | // LookupMemberExpr can modify Base, and thus change BaseType |
| 3957 | BaseType = Base->getType(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3958 | } |
| 3959 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3960 | return BuildMemberReferenceExpr(Base, BaseType, |
John McCall | 38836f0 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 3961 | OpLoc, IsArrow, SS, FirstQualifierInScope, |
| 3962 | R, TemplateArgs); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3963 | } |
| 3964 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3965 | ExprResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3966 | Sema::BuildMemberReferenceExpr(Expr *BaseExpr, QualType BaseExprType, |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3967 | SourceLocation OpLoc, bool IsArrow, |
| 3968 | const CXXScopeSpec &SS, |
John McCall | 38836f0 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 3969 | NamedDecl *FirstQualifierInScope, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3970 | LookupResult &R, |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 3971 | const TemplateArgumentListInfo *TemplateArgs, |
| 3972 | bool SuppressQualifierCheck) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3973 | QualType BaseType = BaseExprType; |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3974 | if (IsArrow) { |
| 3975 | assert(BaseType->isPointerType()); |
| 3976 | BaseType = BaseType->getAs<PointerType>()->getPointeeType(); |
| 3977 | } |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 3978 | R.setBaseObjectType(BaseType); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3979 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 3980 | const DeclarationNameInfo &MemberNameInfo = R.getLookupNameInfo(); |
| 3981 | DeclarationName MemberName = MemberNameInfo.getName(); |
| 3982 | SourceLocation MemberLoc = MemberNameInfo.getLoc(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3983 | |
| 3984 | if (R.isAmbiguous()) |
Douglas Gregor | d806156 | 2009-08-06 03:17:00 +0000 | [diff] [blame] | 3985 | return ExprError(); |
| 3986 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3987 | if (R.empty()) { |
| 3988 | // Rederive where we looked up. |
| 3989 | DeclContext *DC = (SS.isSet() |
| 3990 | ? computeDeclContext(SS, false) |
| 3991 | : BaseType->getAs<RecordType>()->getDecl()); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3992 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3993 | Diag(R.getNameLoc(), diag::err_no_member) |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3994 | << MemberName << DC |
| 3995 | << (BaseExpr ? BaseExpr->getSourceRange() : SourceRange()); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3996 | return ExprError(); |
| 3997 | } |
| 3998 | |
John McCall | 38836f0 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 3999 | // Diagnose lookups that find only declarations from a non-base |
| 4000 | // type. This is possible for either qualified lookups (which may |
| 4001 | // have been qualified with an unrelated type) or implicit member |
| 4002 | // expressions (which were found with unqualified lookup and thus |
| 4003 | // may have come from an enclosing scope). Note that it's okay for |
| 4004 | // lookup to find declarations from a non-base type as long as those |
| 4005 | // aren't the ones picked by overload resolution. |
| 4006 | if ((SS.isSet() || !BaseExpr || |
| 4007 | (isa<CXXThisExpr>(BaseExpr) && |
| 4008 | cast<CXXThisExpr>(BaseExpr)->isImplicit())) && |
Douglas Gregor | b139cd5 | 2010-05-01 20:49:11 +0000 | [diff] [blame] | 4009 | !SuppressQualifierCheck && |
John McCall | 38836f0 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 4010 | CheckQualifiedMemberReference(BaseExpr, BaseType, SS, R)) |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4011 | return ExprError(); |
| 4012 | |
| 4013 | // Construct an unresolved result if we in fact got an unresolved |
| 4014 | // result. |
| 4015 | if (R.isOverloadedResult() || R.isUnresolvableResult()) { |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 4016 | // Suppress any lookup-related diagnostics; we'll do these when we |
| 4017 | // pick a member. |
| 4018 | R.suppressDiagnostics(); |
| 4019 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4020 | UnresolvedMemberExpr *MemExpr |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 4021 | = UnresolvedMemberExpr::Create(Context, R.isUnresolvableResult(), |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4022 | BaseExpr, BaseExprType, |
| 4023 | IsArrow, OpLoc, |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 4024 | SS.getWithLocInContext(Context), |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4025 | MemberNameInfo, |
Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 4026 | TemplateArgs, R.begin(), R.end()); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4027 | |
| 4028 | return Owned(MemExpr); |
| 4029 | } |
| 4030 | |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4031 | assert(R.isSingleResult()); |
John McCall | a8ae222 | 2010-04-06 21:38:20 +0000 | [diff] [blame] | 4032 | DeclAccessPair FoundDecl = R.begin().getPair(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4033 | NamedDecl *MemberDecl = R.getFoundDecl(); |
| 4034 | |
| 4035 | // FIXME: diagnose the presence of template arguments now. |
| 4036 | |
| 4037 | // If the decl being referenced had an error, return an error for this |
| 4038 | // sub-expr without emitting another error, in order to avoid cascading |
| 4039 | // error cases. |
| 4040 | if (MemberDecl->isInvalidDecl()) |
| 4041 | return ExprError(); |
| 4042 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4043 | // Handle the implicit-member-access case. |
| 4044 | if (!BaseExpr) { |
| 4045 | // 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] | 4046 | if (!MemberDecl->isCXXInstanceMember()) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4047 | return BuildDeclarationNameExpr(SS, R.getLookupNameInfo(), MemberDecl); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4048 | |
Douglas Gregor | b15af89 | 2010-01-07 23:12:05 +0000 | [diff] [blame] | 4049 | SourceLocation Loc = R.getNameLoc(); |
| 4050 | if (SS.getRange().isValid()) |
| 4051 | Loc = SS.getRange().getBegin(); |
| 4052 | BaseExpr = new (Context) CXXThisExpr(Loc, BaseExprType,/*isImplicit=*/true); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4053 | } |
| 4054 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4055 | bool ShouldCheckUse = true; |
| 4056 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(MemberDecl)) { |
| 4057 | // Don't diagnose the use of a virtual member function unless it's |
| 4058 | // explicitly qualified. |
| 4059 | if (MD->isVirtual() && !SS.isSet()) |
| 4060 | ShouldCheckUse = false; |
| 4061 | } |
| 4062 | |
| 4063 | // Check the use of this member. |
| 4064 | if (ShouldCheckUse && DiagnoseUseOfDecl(MemberDecl, MemberLoc)) { |
| 4065 | Owned(BaseExpr); |
| 4066 | return ExprError(); |
| 4067 | } |
| 4068 | |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4069 | // Perform a property load on the base regardless of whether we |
| 4070 | // actually need it for the declaration. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4071 | if (BaseExpr->getObjectKind() == OK_ObjCProperty) { |
| 4072 | ExprResult Result = ConvertPropertyForRValue(BaseExpr); |
| 4073 | if (Result.isInvalid()) |
| 4074 | return ExprError(); |
| 4075 | BaseExpr = Result.take(); |
| 4076 | } |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4077 | |
John McCall | feb624a | 2010-11-23 20:48:44 +0000 | [diff] [blame] | 4078 | if (FieldDecl *FD = dyn_cast<FieldDecl>(MemberDecl)) |
| 4079 | return BuildFieldReferenceExpr(*this, BaseExpr, IsArrow, |
| 4080 | SS, FD, FoundDecl, MemberNameInfo); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4081 | |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 4082 | if (IndirectFieldDecl *FD = dyn_cast<IndirectFieldDecl>(MemberDecl)) |
| 4083 | // We may have found a field within an anonymous union or struct |
| 4084 | // (C++ [class.union]). |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 4085 | return BuildAnonymousStructUnionMemberReference(SS, MemberLoc, FD, |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4086 | BaseExpr, OpLoc); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 4087 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4088 | if (VarDecl *Var = dyn_cast<VarDecl>(MemberDecl)) { |
| 4089 | MarkDeclarationReferenced(MemberLoc, Var); |
| 4090 | return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4091 | Var, FoundDecl, MemberNameInfo, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4092 | Var->getType().getNonReferenceType(), |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4093 | VK_LValue, OK_Ordinary)); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4094 | } |
| 4095 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4096 | if (CXXMethodDecl *MemberFn = dyn_cast<CXXMethodDecl>(MemberDecl)) { |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 4097 | ExprValueKind valueKind; |
| 4098 | QualType type; |
| 4099 | if (MemberFn->isInstance()) { |
| 4100 | valueKind = VK_RValue; |
| 4101 | type = Context.BoundMemberTy; |
| 4102 | } else { |
| 4103 | valueKind = VK_LValue; |
| 4104 | type = MemberFn->getType(); |
| 4105 | } |
| 4106 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 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 | MemberFn, FoundDecl, MemberNameInfo, |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 4110 | type, valueKind, OK_Ordinary)); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4111 | } |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4112 | assert(!isa<FunctionDecl>(MemberDecl) && "member function not C++ method?"); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4113 | |
| 4114 | if (EnumConstantDecl *Enum = dyn_cast<EnumConstantDecl>(MemberDecl)) { |
| 4115 | MarkDeclarationReferenced(MemberLoc, MemberDecl); |
| 4116 | return Owned(BuildMemberExpr(Context, BaseExpr, IsArrow, SS, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4117 | Enum, FoundDecl, MemberNameInfo, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4118 | Enum->getType(), VK_RValue, OK_Ordinary)); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4119 | } |
| 4120 | |
| 4121 | Owned(BaseExpr); |
| 4122 | |
Douglas Gregor | 861eb80 | 2010-04-25 20:55:08 +0000 | [diff] [blame] | 4123 | // We found something that we didn't expect. Complain. |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4124 | if (isa<TypeDecl>(MemberDecl)) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4125 | Diag(MemberLoc, diag::err_typecheck_member_reference_type) |
Douglas Gregor | 861eb80 | 2010-04-25 20:55:08 +0000 | [diff] [blame] | 4126 | << MemberName << BaseType << int(IsArrow); |
| 4127 | else |
| 4128 | Diag(MemberLoc, diag::err_typecheck_member_reference_unknown) |
| 4129 | << MemberName << BaseType << int(IsArrow); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4130 | |
Douglas Gregor | 861eb80 | 2010-04-25 20:55:08 +0000 | [diff] [blame] | 4131 | Diag(MemberDecl->getLocation(), diag::note_member_declared_here) |
| 4132 | << MemberName; |
Douglas Gregor | 516d672 | 2010-04-25 21:15:30 +0000 | [diff] [blame] | 4133 | R.suppressDiagnostics(); |
Douglas Gregor | 861eb80 | 2010-04-25 20:55:08 +0000 | [diff] [blame] | 4134 | return ExprError(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4135 | } |
| 4136 | |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4137 | /// Given that normal member access failed on the given expression, |
| 4138 | /// and given that the expression's type involves builtin-id or |
| 4139 | /// builtin-Class, decide whether substituting in the redefinition |
| 4140 | /// types would be profitable. The redefinition type is whatever |
| 4141 | /// this translation unit tried to typedef to id/Class; we store |
| 4142 | /// 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] | 4143 | static bool ShouldTryAgainWithRedefinitionType(Sema &S, ExprResult &base) { |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4144 | const ObjCObjectPointerType *opty |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4145 | = base.get()->getType()->getAs<ObjCObjectPointerType>(); |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4146 | if (!opty) return false; |
| 4147 | |
| 4148 | const ObjCObjectType *ty = opty->getObjectType(); |
| 4149 | |
| 4150 | QualType redef; |
| 4151 | if (ty->isObjCId()) { |
| 4152 | redef = S.Context.ObjCIdRedefinitionType; |
| 4153 | } else if (ty->isObjCClass()) { |
| 4154 | redef = S.Context.ObjCClassRedefinitionType; |
| 4155 | } else { |
| 4156 | return false; |
| 4157 | } |
| 4158 | |
| 4159 | // Do the substitution as long as the redefinition type isn't just a |
| 4160 | // possibly-qualified pointer to builtin-id or builtin-Class again. |
| 4161 | opty = redef->getAs<ObjCObjectPointerType>(); |
| 4162 | if (opty && !opty->getObjectType()->getInterface() != 0) |
| 4163 | return false; |
| 4164 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4165 | base = S.ImpCastExprToType(base.take(), redef, CK_BitCast); |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4166 | return true; |
| 4167 | } |
| 4168 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4169 | /// Look up the given member of the given non-type-dependent |
| 4170 | /// expression. This can return in one of two ways: |
| 4171 | /// * If it returns a sentinel null-but-valid result, the caller will |
| 4172 | /// assume that lookup was performed and the results written into |
| 4173 | /// the provided structure. It will take over from there. |
| 4174 | /// * Otherwise, the returned expression will be produced in place of |
| 4175 | /// an ordinary member expression. |
| 4176 | /// |
| 4177 | /// The ObjCImpDecl bit is a gross hack that will need to be properly |
| 4178 | /// fixed for ObjC++. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4179 | ExprResult |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4180 | Sema::LookupMemberExpr(LookupResult &R, ExprResult &BaseExpr, |
John McCall | a928c65 | 2009-12-07 22:46:59 +0000 | [diff] [blame] | 4181 | bool &IsArrow, SourceLocation OpLoc, |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 4182 | CXXScopeSpec &SS, |
John McCall | 4887165 | 2010-08-21 09:40:31 +0000 | [diff] [blame] | 4183 | Decl *ObjCImpDecl, bool HasTemplateArgs) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4184 | assert(BaseExpr.get() && "no base expression"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4185 | |
Steve Naroff | eaaae46 | 2007-12-16 21:42:28 +0000 | [diff] [blame] | 4186 | // Perform default conversions. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4187 | BaseExpr = DefaultFunctionArrayConversion(BaseExpr.take()); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4188 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4189 | if (IsArrow) { |
| 4190 | BaseExpr = DefaultLvalueConversion(BaseExpr.take()); |
| 4191 | if (BaseExpr.isInvalid()) |
| 4192 | return ExprError(); |
| 4193 | } |
| 4194 | |
| 4195 | QualType BaseType = BaseExpr.get()->getType(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4196 | assert(!BaseType->isDependentType()); |
| 4197 | |
| 4198 | DeclarationName MemberName = R.getLookupName(); |
| 4199 | SourceLocation MemberLoc = R.getNameLoc(); |
Douglas Gregor | d82ae38 | 2009-11-06 06:30:47 +0000 | [diff] [blame] | 4200 | |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4201 | // For later type-checking purposes, turn arrow accesses into dot |
| 4202 | // accesses. The only access type we support that doesn't follow |
| 4203 | // the C equivalence "a->b === (*a).b" is ObjC property accesses, |
| 4204 | // and those never use arrows, so this is unaffected. |
| 4205 | if (IsArrow) { |
| 4206 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) |
| 4207 | BaseType = Ptr->getPointeeType(); |
| 4208 | else if (const ObjCObjectPointerType *Ptr |
| 4209 | = BaseType->getAs<ObjCObjectPointerType>()) |
| 4210 | BaseType = Ptr->getPointeeType(); |
| 4211 | else if (BaseType->isRecordType()) { |
| 4212 | // Recover from arrow accesses to records, e.g.: |
| 4213 | // struct MyRecord foo; |
| 4214 | // foo->bar |
| 4215 | // This is actually well-formed in C++ if MyRecord has an |
| 4216 | // overloaded operator->, but that should have been dealt with |
| 4217 | // by now. |
| 4218 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4219 | << BaseType << int(IsArrow) << BaseExpr.get()->getSourceRange() |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4220 | << FixItHint::CreateReplacement(OpLoc, "."); |
| 4221 | IsArrow = false; |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 4222 | } else if (BaseType == Context.BoundMemberTy) { |
| 4223 | goto fail; |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4224 | } else { |
| 4225 | Diag(MemberLoc, diag::err_typecheck_member_reference_arrow) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4226 | << BaseType << BaseExpr.get()->getSourceRange(); |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4227 | return ExprError(); |
Douglas Gregor | d82ae38 | 2009-11-06 06:30:47 +0000 | [diff] [blame] | 4228 | } |
| 4229 | } |
| 4230 | |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4231 | // Handle field access to simple records. |
| 4232 | if (const RecordType *RTy = BaseType->getAs<RecordType>()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4233 | if (LookupMemberExprInRecord(*this, R, BaseExpr.get()->getSourceRange(), |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4234 | RTy, OpLoc, SS, HasTemplateArgs)) |
| 4235 | return ExprError(); |
| 4236 | |
| 4237 | // Returning valid-but-null is how we indicate to the caller that |
| 4238 | // the lookup result was filled in. |
| 4239 | return Owned((Expr*) 0); |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 4240 | } |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4241 | |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4242 | // Handle ivar access to Objective-C objects. |
| 4243 | if (const ObjCObjectType *OTy = BaseType->getAs<ObjCObjectType>()) { |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4244 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4245 | |
| 4246 | // There are three cases for the base type: |
| 4247 | // - builtin id (qualified or unqualified) |
| 4248 | // - builtin Class (qualified or unqualified) |
| 4249 | // - an interface |
| 4250 | ObjCInterfaceDecl *IDecl = OTy->getInterface(); |
| 4251 | if (!IDecl) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4252 | if (getLangOptions().ObjCAutoRefCount && |
| 4253 | (OTy->isObjCId() || OTy->isObjCClass())) |
| 4254 | goto fail; |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4255 | // There's an implicit 'isa' ivar on all objects. |
| 4256 | // But we only actually find it this way on objects of type 'id', |
| 4257 | // apparently. |
| 4258 | if (OTy->isObjCId() && Member->isStr("isa")) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4259 | return Owned(new (Context) ObjCIsaExpr(BaseExpr.take(), IsArrow, MemberLoc, |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4260 | Context.getObjCClassType())); |
| 4261 | |
| 4262 | if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr)) |
| 4263 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 4264 | ObjCImpDecl, HasTemplateArgs); |
| 4265 | goto fail; |
| 4266 | } |
| 4267 | |
| 4268 | ObjCInterfaceDecl *ClassDeclared; |
| 4269 | ObjCIvarDecl *IV = IDecl->lookupInstanceVariable(Member, ClassDeclared); |
| 4270 | |
| 4271 | if (!IV) { |
| 4272 | // Attempt to correct for typos in ivar names. |
| 4273 | LookupResult Res(*this, R.getLookupName(), R.getNameLoc(), |
| 4274 | LookupMemberName); |
| 4275 | if (CorrectTypo(Res, 0, 0, IDecl, false, |
| 4276 | IsArrow ? CTC_ObjCIvarLookup |
| 4277 | : CTC_ObjCPropertyLookup) && |
| 4278 | (IV = Res.getAsSingle<ObjCIvarDecl>())) { |
| 4279 | Diag(R.getNameLoc(), |
| 4280 | diag::err_typecheck_member_reference_ivar_suggest) |
| 4281 | << IDecl->getDeclName() << MemberName << IV->getDeclName() |
| 4282 | << FixItHint::CreateReplacement(R.getNameLoc(), |
| 4283 | IV->getNameAsString()); |
| 4284 | Diag(IV->getLocation(), diag::note_previous_decl) |
| 4285 | << IV->getDeclName(); |
| 4286 | } else { |
| 4287 | Res.clear(); |
| 4288 | Res.setLookupName(Member); |
| 4289 | |
| 4290 | Diag(MemberLoc, diag::err_typecheck_member_reference_ivar) |
| 4291 | << IDecl->getDeclName() << MemberName |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4292 | << BaseExpr.get()->getSourceRange(); |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4293 | return ExprError(); |
| 4294 | } |
| 4295 | } |
| 4296 | |
| 4297 | // If the decl being referenced had an error, return an error for this |
| 4298 | // sub-expr without emitting another error, in order to avoid cascading |
| 4299 | // error cases. |
| 4300 | if (IV->isInvalidDecl()) |
| 4301 | return ExprError(); |
| 4302 | |
| 4303 | // Check whether we can reference this field. |
| 4304 | if (DiagnoseUseOfDecl(IV, MemberLoc)) |
| 4305 | return ExprError(); |
| 4306 | if (IV->getAccessControl() != ObjCIvarDecl::Public && |
| 4307 | IV->getAccessControl() != ObjCIvarDecl::Package) { |
| 4308 | ObjCInterfaceDecl *ClassOfMethodDecl = 0; |
| 4309 | if (ObjCMethodDecl *MD = getCurMethodDecl()) |
| 4310 | ClassOfMethodDecl = MD->getClassInterface(); |
| 4311 | else if (ObjCImpDecl && getCurFunctionDecl()) { |
| 4312 | // Case of a c-function declared inside an objc implementation. |
| 4313 | // FIXME: For a c-style function nested inside an objc implementation |
| 4314 | // class, there is no implementation context available, so we pass |
| 4315 | // down the context as argument to this routine. Ideally, this context |
| 4316 | // need be passed down in the AST node and somehow calculated from the |
| 4317 | // AST for a function decl. |
| 4318 | if (ObjCImplementationDecl *IMPD = |
| 4319 | dyn_cast<ObjCImplementationDecl>(ObjCImpDecl)) |
| 4320 | ClassOfMethodDecl = IMPD->getClassInterface(); |
| 4321 | else if (ObjCCategoryImplDecl* CatImplClass = |
| 4322 | dyn_cast<ObjCCategoryImplDecl>(ObjCImpDecl)) |
| 4323 | ClassOfMethodDecl = CatImplClass->getClassInterface(); |
| 4324 | } |
| 4325 | |
| 4326 | if (IV->getAccessControl() == ObjCIvarDecl::Private) { |
| 4327 | if (ClassDeclared != IDecl || |
| 4328 | ClassOfMethodDecl != ClassDeclared) |
| 4329 | Diag(MemberLoc, diag::error_private_ivar_access) |
| 4330 | << IV->getDeclName(); |
| 4331 | } else if (!IDecl->isSuperClassOf(ClassOfMethodDecl)) |
| 4332 | // @protected |
| 4333 | Diag(MemberLoc, diag::error_protected_ivar_access) |
| 4334 | << IV->getDeclName(); |
| 4335 | } |
Fariborz Jahanian | 62c72d0 | 2011-06-16 17:29:56 +0000 | [diff] [blame] | 4336 | if (getLangOptions().ObjCAutoRefCount) { |
| 4337 | Expr *BaseExp = BaseExpr.get()->IgnoreParenImpCasts(); |
| 4338 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(BaseExp)) |
| 4339 | if (UO->getOpcode() == UO_Deref) |
| 4340 | BaseExp = UO->getSubExpr()->IgnoreParenCasts(); |
| 4341 | |
| 4342 | if (DeclRefExpr *DE = dyn_cast<DeclRefExpr>(BaseExp)) |
| 4343 | if (DE->getType().getObjCLifetime() == Qualifiers::OCL_Weak) |
| 4344 | Diag(DE->getLocation(), diag::error_arc_weak_ivar_access); |
| 4345 | } |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4346 | |
| 4347 | return Owned(new (Context) ObjCIvarRefExpr(IV, IV->getType(), |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4348 | MemberLoc, BaseExpr.take(), |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4349 | IsArrow)); |
| 4350 | } |
| 4351 | |
| 4352 | // Objective-C property access. |
| 4353 | const ObjCObjectPointerType *OPT; |
| 4354 | if (!IsArrow && (OPT = BaseType->getAs<ObjCObjectPointerType>())) { |
| 4355 | // This actually uses the base as an r-value. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4356 | BaseExpr = DefaultLvalueConversion(BaseExpr.take()); |
| 4357 | if (BaseExpr.isInvalid()) |
| 4358 | return ExprError(); |
| 4359 | |
| 4360 | assert(Context.hasSameUnqualifiedType(BaseType, BaseExpr.get()->getType())); |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4361 | |
| 4362 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
| 4363 | |
| 4364 | const ObjCObjectType *OT = OPT->getObjectType(); |
| 4365 | |
| 4366 | // id, with and without qualifiers. |
| 4367 | if (OT->isObjCId()) { |
| 4368 | // Check protocols on qualified interfaces. |
| 4369 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
| 4370 | if (Decl *PMDecl = FindGetterSetterNameDecl(OPT, Member, Sel, Context)) { |
| 4371 | if (ObjCPropertyDecl *PD = dyn_cast<ObjCPropertyDecl>(PMDecl)) { |
| 4372 | // Check the use of this declaration |
| 4373 | if (DiagnoseUseOfDecl(PD, MemberLoc)) |
| 4374 | return ExprError(); |
| 4375 | |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4376 | QualType T = PD->getType(); |
| 4377 | if (ObjCMethodDecl *Getter = PD->getGetterMethodDecl()) |
| 4378 | T = getMessageSendResultType(BaseType, Getter, false, false); |
| 4379 | |
| 4380 | return Owned(new (Context) ObjCPropertyRefExpr(PD, T, |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4381 | VK_LValue, |
| 4382 | OK_ObjCProperty, |
| 4383 | MemberLoc, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4384 | BaseExpr.take())); |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4385 | } |
| 4386 | |
| 4387 | if (ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(PMDecl)) { |
| 4388 | // Check the use of this method. |
| 4389 | if (DiagnoseUseOfDecl(OMD, MemberLoc)) |
| 4390 | return ExprError(); |
| 4391 | Selector SetterSel = |
| 4392 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
| 4393 | PP.getSelectorTable(), Member); |
| 4394 | ObjCMethodDecl *SMD = 0; |
| 4395 | if (Decl *SDecl = FindGetterSetterNameDecl(OPT, /*Property id*/0, |
| 4396 | SetterSel, Context)) |
| 4397 | SMD = dyn_cast<ObjCMethodDecl>(SDecl); |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4398 | QualType PType = getMessageSendResultType(BaseType, OMD, false, |
| 4399 | false); |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4400 | |
| 4401 | ExprValueKind VK = VK_LValue; |
| 4402 | if (!getLangOptions().CPlusPlus && |
| 4403 | IsCForbiddenLValueType(Context, PType)) |
| 4404 | VK = VK_RValue; |
| 4405 | ExprObjectKind OK = (VK == VK_RValue ? OK_Ordinary : OK_ObjCProperty); |
| 4406 | |
| 4407 | return Owned(new (Context) ObjCPropertyRefExpr(OMD, SMD, PType, |
| 4408 | VK, OK, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4409 | MemberLoc, BaseExpr.take())); |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4410 | } |
| 4411 | } |
Fariborz Jahanian | b03a4c2 | 2011-03-15 17:27:48 +0000 | [diff] [blame] | 4412 | // Use of id.member can only be for a property reference. Do not |
| 4413 | // use the 'id' redefinition in this case. |
| 4414 | if (IsArrow && ShouldTryAgainWithRedefinitionType(*this, BaseExpr)) |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4415 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 4416 | ObjCImpDecl, HasTemplateArgs); |
| 4417 | |
| 4418 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
| 4419 | << MemberName << BaseType); |
| 4420 | } |
| 4421 | |
| 4422 | // 'Class', unqualified only. |
| 4423 | if (OT->isObjCClass()) { |
| 4424 | // Only works in a method declaration (??!). |
| 4425 | ObjCMethodDecl *MD = getCurMethodDecl(); |
| 4426 | if (!MD) { |
| 4427 | if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr)) |
| 4428 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 4429 | ObjCImpDecl, HasTemplateArgs); |
| 4430 | |
| 4431 | goto fail; |
| 4432 | } |
| 4433 | |
| 4434 | // Also must look for a getter name which uses property syntax. |
| 4435 | Selector Sel = PP.getSelectorTable().getNullarySelector(Member); |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4436 | ObjCInterfaceDecl *IFace = MD->getClassInterface(); |
| 4437 | ObjCMethodDecl *Getter; |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4438 | if ((Getter = IFace->lookupClassMethod(Sel))) { |
| 4439 | // Check the use of this method. |
| 4440 | if (DiagnoseUseOfDecl(Getter, MemberLoc)) |
| 4441 | return ExprError(); |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4442 | } else |
Fariborz Jahanian | ecbbb6e | 2010-12-03 23:37:08 +0000 | [diff] [blame] | 4443 | Getter = IFace->lookupPrivateMethod(Sel, false); |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4444 | // If we found a getter then this may be a valid dot-reference, we |
| 4445 | // will look for the matching setter, in case it is needed. |
| 4446 | Selector SetterSel = |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4447 | SelectorTable::constructSetterName(PP.getIdentifierTable(), |
| 4448 | PP.getSelectorTable(), Member); |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4449 | ObjCMethodDecl *Setter = IFace->lookupClassMethod(SetterSel); |
| 4450 | if (!Setter) { |
| 4451 | // If this reference is in an @implementation, also check for 'private' |
| 4452 | // methods. |
Fariborz Jahanian | ecbbb6e | 2010-12-03 23:37:08 +0000 | [diff] [blame] | 4453 | Setter = IFace->lookupPrivateMethod(SetterSel, false); |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4454 | } |
| 4455 | // Look through local category implementations associated with the class. |
| 4456 | if (!Setter) |
| 4457 | Setter = IFace->getCategoryClassMethod(SetterSel); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4458 | |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4459 | if (Setter && DiagnoseUseOfDecl(Setter, MemberLoc)) |
| 4460 | return ExprError(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4461 | |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4462 | if (Getter || Setter) { |
| 4463 | QualType PType; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4464 | |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4465 | ExprValueKind VK = VK_LValue; |
| 4466 | if (Getter) { |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 4467 | PType = getMessageSendResultType(QualType(OT, 0), Getter, true, |
| 4468 | false); |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4469 | if (!getLangOptions().CPlusPlus && |
| 4470 | IsCForbiddenLValueType(Context, PType)) |
| 4471 | VK = VK_RValue; |
| 4472 | } else { |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4473 | // Get the expression type from Setter's incoming parameter. |
| 4474 | PType = (*(Setter->param_end() -1))->getType(); |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4475 | } |
| 4476 | ExprObjectKind OK = (VK == VK_RValue ? OK_Ordinary : OK_ObjCProperty); |
| 4477 | |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4478 | // FIXME: we must check that the setter has property type. |
John McCall | b7bd14f | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 4479 | return Owned(new (Context) ObjCPropertyRefExpr(Getter, Setter, |
| 4480 | PType, VK, OK, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4481 | MemberLoc, BaseExpr.take())); |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4482 | } |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4483 | |
| 4484 | if (ShouldTryAgainWithRedefinitionType(*this, BaseExpr)) |
| 4485 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 4486 | ObjCImpDecl, HasTemplateArgs); |
| 4487 | |
Fariborz Jahanian | e983d17 | 2009-09-22 16:48:37 +0000 | [diff] [blame] | 4488 | return ExprError(Diag(MemberLoc, diag::err_property_not_found) |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4489 | << MemberName << BaseType); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4490 | } |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4491 | |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4492 | // Normal property access. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4493 | return HandleExprPropertyRefExpr(OPT, BaseExpr.get(), MemberName, MemberLoc, |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4494 | SourceLocation(), QualType(), false); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 4495 | } |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 4496 | |
Chris Lattner | b63a745 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 4497 | // Handle 'field access' to vectors, such as 'V.xx'. |
Chris Lattner | 6c7ce10 | 2009-02-16 21:11:58 +0000 | [diff] [blame] | 4498 | if (BaseType->isExtVectorType()) { |
John McCall | 15317a2 | 2010-12-15 04:42:30 +0000 | [diff] [blame] | 4499 | // FIXME: this expr should store IsArrow. |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 4500 | IdentifierInfo *Member = MemberName.getAsIdentifierInfo(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4501 | ExprValueKind VK = (IsArrow ? VK_LValue : BaseExpr.get()->getValueKind()); |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4502 | QualType ret = CheckExtVectorComponent(*this, BaseType, VK, OpLoc, |
| 4503 | Member, MemberLoc); |
Chris Lattner | b63a745 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 4504 | if (ret.isNull()) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4505 | return ExprError(); |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4506 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4507 | return Owned(new (Context) ExtVectorElementExpr(ret, VK, BaseExpr.take(), |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4508 | *Member, MemberLoc)); |
Chris Lattner | b63a745 | 2008-07-21 04:28:12 +0000 | [diff] [blame] | 4509 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4510 | |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4511 | // Adjust builtin-sel to the appropriate redefinition type if that's |
| 4512 | // not just a pointer to builtin-sel again. |
| 4513 | if (IsArrow && |
| 4514 | BaseType->isSpecificBuiltinType(BuiltinType::ObjCSel) && |
| 4515 | !Context.ObjCSelRedefinitionType->isObjCSelType()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4516 | BaseExpr = ImpCastExprToType(BaseExpr.take(), Context.ObjCSelRedefinitionType, |
| 4517 | CK_BitCast); |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4518 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 4519 | ObjCImpDecl, HasTemplateArgs); |
| 4520 | } |
| 4521 | |
| 4522 | // Failure cases. |
| 4523 | fail: |
| 4524 | |
Matt Beaumont-Gay | 06de255 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4525 | // Recover from dot accesses to pointers, e.g.: |
| 4526 | // type *foo; |
| 4527 | // foo.bar |
| 4528 | // This is actually well-formed in two cases: |
| 4529 | // - 'type' is an Objective C type |
| 4530 | // - 'bar' is a pseudo-destructor name which happens to refer to |
| 4531 | // the appropriate pointer type |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4532 | if (const PointerType *Ptr = BaseType->getAs<PointerType>()) { |
Matt Beaumont-Gay | 06de255 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4533 | if (!IsArrow && Ptr->getPointeeType()->isRecordType() && |
| 4534 | MemberName.getNameKind() != DeclarationName::CXXDestructorName) { |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4535 | Diag(OpLoc, diag::err_typecheck_member_reference_suggestion) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4536 | << BaseType << int(IsArrow) << BaseExpr.get()->getSourceRange() |
Matt Beaumont-Gay | 06de255 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4537 | << FixItHint::CreateReplacement(OpLoc, "->"); |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4538 | |
| 4539 | // Recurse as an -> access. |
| 4540 | IsArrow = true; |
| 4541 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 4542 | ObjCImpDecl, HasTemplateArgs); |
| 4543 | } |
John McCall | 68fc88ec | 2010-12-15 16:46:44 +0000 | [diff] [blame] | 4544 | } |
| 4545 | |
Matt Beaumont-Gay | 06de255 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4546 | // If the user is trying to apply -> or . to a function name, it's probably |
| 4547 | // because they forgot parentheses to call that function. |
Matt Beaumont-Gay | 3c27391 | 2011-05-04 22:10:40 +0000 | [diff] [blame] | 4548 | QualType ZeroArgCallTy; |
| 4549 | UnresolvedSet<4> Overloads; |
| 4550 | if (isExprCallable(*BaseExpr.get(), ZeroArgCallTy, Overloads)) { |
| 4551 | if (ZeroArgCallTy.isNull()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4552 | Diag(BaseExpr.get()->getExprLoc(), diag::err_member_reference_needs_call) |
Matt Beaumont-Gay | 3c27391 | 2011-05-04 22:10:40 +0000 | [diff] [blame] | 4553 | << (Overloads.size() > 1) << 0 << BaseExpr.get()->getSourceRange(); |
| 4554 | UnresolvedSet<2> PlausibleOverloads; |
| 4555 | for (OverloadExpr::decls_iterator It = Overloads.begin(), |
| 4556 | DeclsEnd = Overloads.end(); It != DeclsEnd; ++It) { |
| 4557 | const FunctionDecl *OverloadDecl = cast<FunctionDecl>(*It); |
| 4558 | QualType OverloadResultTy = OverloadDecl->getResultType(); |
| 4559 | if ((!IsArrow && OverloadResultTy->isRecordType()) || |
| 4560 | (IsArrow && OverloadResultTy->isPointerType() && |
| 4561 | OverloadResultTy->getPointeeType()->isRecordType())) |
| 4562 | PlausibleOverloads.addDecl(It.getDecl()); |
Matt Beaumont-Gay | 06de255 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4563 | } |
Matt Beaumont-Gay | 3c27391 | 2011-05-04 22:10:40 +0000 | [diff] [blame] | 4564 | NoteOverloads(PlausibleOverloads, BaseExpr.get()->getExprLoc()); |
Matt Beaumont-Gay | 06de255 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4565 | return ExprError(); |
| 4566 | } |
Matt Beaumont-Gay | 3c27391 | 2011-05-04 22:10:40 +0000 | [diff] [blame] | 4567 | if ((!IsArrow && ZeroArgCallTy->isRecordType()) || |
| 4568 | (IsArrow && ZeroArgCallTy->isPointerType() && |
| 4569 | ZeroArgCallTy->getPointeeType()->isRecordType())) { |
| 4570 | // At this point, we know BaseExpr looks like it's potentially callable |
| 4571 | // with 0 arguments, and that it returns something of a reasonable type, |
| 4572 | // so we can emit a fixit and carry on pretending that BaseExpr was |
| 4573 | // actually a CallExpr. |
| 4574 | SourceLocation ParenInsertionLoc = |
| 4575 | PP.getLocForEndOfToken(BaseExpr.get()->getLocEnd()); |
| 4576 | Diag(BaseExpr.get()->getExprLoc(), diag::err_member_reference_needs_call) |
| 4577 | << (Overloads.size() > 1) << 1 << BaseExpr.get()->getSourceRange() |
| 4578 | << FixItHint::CreateInsertion(ParenInsertionLoc, "()"); |
| 4579 | // FIXME: Try this before emitting the fixit, and suppress diagnostics |
| 4580 | // while doing so. |
| 4581 | ExprResult NewBase = |
| 4582 | ActOnCallExpr(0, BaseExpr.take(), ParenInsertionLoc, |
| 4583 | MultiExprArg(*this, 0, 0), |
| 4584 | ParenInsertionLoc.getFileLocWithOffset(1)); |
| 4585 | if (NewBase.isInvalid()) |
| 4586 | return ExprError(); |
| 4587 | BaseExpr = NewBase; |
| 4588 | BaseExpr = DefaultFunctionArrayConversion(BaseExpr.take()); |
| 4589 | return LookupMemberExpr(R, BaseExpr, IsArrow, OpLoc, SS, |
| 4590 | ObjCImpDecl, HasTemplateArgs); |
Matt Beaumont-Gay | 06de255 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4591 | } |
Matt Beaumont-Gay | 06de255 | 2011-02-22 23:52:53 +0000 | [diff] [blame] | 4592 | } |
| 4593 | |
Douglas Gregor | 0b08ba4 | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 4594 | Diag(MemberLoc, diag::err_typecheck_member_reference_struct_union) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4595 | << BaseType << BaseExpr.get()->getSourceRange(); |
Douglas Gregor | 0b08ba4 | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 4596 | |
Douglas Gregor | 0b08ba4 | 2009-03-27 06:00:30 +0000 | [diff] [blame] | 4597 | return ExprError(); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 4598 | } |
| 4599 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4600 | /// The main callback when the parser finds something like |
| 4601 | /// expression . [nested-name-specifier] identifier |
| 4602 | /// expression -> [nested-name-specifier] identifier |
| 4603 | /// where 'identifier' encompasses a fairly broad spectrum of |
| 4604 | /// possibilities, including destructor and operator references. |
| 4605 | /// |
| 4606 | /// \param OpKind either tok::arrow or tok::period |
| 4607 | /// \param HasTrailingLParen whether the next token is '(', which |
| 4608 | /// is used to diagnose mis-uses of special members that can |
| 4609 | /// only be called |
| 4610 | /// \param ObjCImpDecl the current ObjC @implementation decl; |
| 4611 | /// this is an ugly hack around the fact that ObjC @implementations |
| 4612 | /// aren't properly put in the context chain |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4613 | ExprResult Sema::ActOnMemberAccessExpr(Scope *S, Expr *Base, |
John McCall | 15317a2 | 2010-12-15 04:42:30 +0000 | [diff] [blame] | 4614 | SourceLocation OpLoc, |
| 4615 | tok::TokenKind OpKind, |
| 4616 | CXXScopeSpec &SS, |
| 4617 | UnqualifiedId &Id, |
| 4618 | Decl *ObjCImpDecl, |
| 4619 | bool HasTrailingLParen) { |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4620 | if (SS.isSet() && SS.isInvalid()) |
| 4621 | return ExprError(); |
| 4622 | |
Francois Pichet | 6422579 | 2011-01-18 05:04:39 +0000 | [diff] [blame] | 4623 | // Warn about the explicit constructor calls Microsoft extension. |
| 4624 | if (getLangOptions().Microsoft && |
| 4625 | Id.getKind() == UnqualifiedId::IK_ConstructorName) |
| 4626 | Diag(Id.getSourceRange().getBegin(), |
| 4627 | diag::ext_ms_explicit_constructor_call); |
| 4628 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4629 | TemplateArgumentListInfo TemplateArgsBuffer; |
| 4630 | |
| 4631 | // Decompose the name into its component parts. |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4632 | DeclarationNameInfo NameInfo; |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4633 | const TemplateArgumentListInfo *TemplateArgs; |
| 4634 | DecomposeUnqualifiedId(*this, Id, TemplateArgsBuffer, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4635 | NameInfo, TemplateArgs); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4636 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4637 | DeclarationName Name = NameInfo.getName(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4638 | bool IsArrow = (OpKind == tok::arrow); |
| 4639 | |
| 4640 | NamedDecl *FirstQualifierInScope |
| 4641 | = (!SS.isSet() ? 0 : FindFirstQualifierInScope(S, |
| 4642 | static_cast<NestedNameSpecifier*>(SS.getScopeRep()))); |
| 4643 | |
| 4644 | // This is a postfix expression, so get rid of ParenListExprs. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4645 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4646 | if (Result.isInvalid()) return ExprError(); |
| 4647 | Base = Result.take(); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4648 | |
Douglas Gregor | 41f9030 | 2010-04-12 20:54:26 +0000 | [diff] [blame] | 4649 | if (Base->getType()->isDependentType() || Name.isDependentName() || |
| 4650 | isDependentScopeSpecifier(SS)) { |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4651 | Result = ActOnDependentMemberExpr(Base, Base->getType(), |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4652 | IsArrow, OpLoc, |
| 4653 | SS, FirstQualifierInScope, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4654 | NameInfo, TemplateArgs); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4655 | } else { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 4656 | LookupResult R(*this, NameInfo, LookupMemberName); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4657 | ExprResult BaseResult = Owned(Base); |
| 4658 | Result = LookupMemberExpr(R, BaseResult, IsArrow, OpLoc, |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 4659 | SS, ObjCImpDecl, TemplateArgs != 0); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4660 | if (BaseResult.isInvalid()) |
| 4661 | return ExprError(); |
| 4662 | Base = BaseResult.take(); |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 4663 | |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 4664 | if (Result.isInvalid()) { |
| 4665 | Owned(Base); |
| 4666 | return ExprError(); |
| 4667 | } |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4668 | |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 4669 | if (Result.get()) { |
| 4670 | // The only way a reference to a destructor can be used is to |
| 4671 | // immediately call it, which falls into this case. If the |
| 4672 | // next token is not a '(', produce a diagnostic and build the |
| 4673 | // call now. |
| 4674 | if (!HasTrailingLParen && |
| 4675 | Id.getKind() == UnqualifiedId::IK_DestructorName) |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4676 | return DiagnoseDtorReference(NameInfo.getLoc(), Result.get()); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4677 | |
John McCall | e9cccd8 | 2010-06-16 08:42:20 +0000 | [diff] [blame] | 4678 | return move(Result); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4679 | } |
| 4680 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4681 | Result = BuildMemberReferenceExpr(Base, Base->getType(), |
John McCall | 38836f0 | 2010-01-15 08:34:02 +0000 | [diff] [blame] | 4682 | OpLoc, IsArrow, SS, FirstQualifierInScope, |
| 4683 | R, TemplateArgs); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4684 | } |
| 4685 | |
| 4686 | return move(Result); |
Anders Carlsson | f571c11 | 2009-08-26 18:25:21 +0000 | [diff] [blame] | 4687 | } |
| 4688 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4689 | ExprResult Sema::BuildCXXDefaultArgExpr(SourceLocation CallLoc, |
Nico Weber | 44887f6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 4690 | FunctionDecl *FD, |
| 4691 | ParmVarDecl *Param) { |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4692 | if (Param->hasUnparsedDefaultArg()) { |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4693 | Diag(CallLoc, |
Nico Weber | ebd45a0 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 4694 | diag::err_use_of_default_argument_to_function_declared_later) << |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4695 | FD << cast<CXXRecordDecl>(FD->getDeclContext())->getDeclName(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4696 | Diag(UnparsedDefaultArgLocs[Param], |
Nico Weber | ebd45a0 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 4697 | diag::note_default_argument_declared_here); |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4698 | return ExprError(); |
| 4699 | } |
| 4700 | |
| 4701 | if (Param->hasUninstantiatedDefaultArg()) { |
| 4702 | Expr *UninstExpr = Param->getUninstantiatedDefaultArg(); |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4703 | |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4704 | // Instantiate the expression. |
| 4705 | MultiLevelTemplateArgumentList ArgList |
| 4706 | = getTemplateInstantiationArgs(FD, 0, /*RelativeToPrimary=*/true); |
Anders Carlsson | 657bad4 | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 4707 | |
Nico Weber | 44887f6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 4708 | std::pair<const TemplateArgument *, unsigned> Innermost |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4709 | = ArgList.getInnermost(); |
| 4710 | InstantiatingTemplate Inst(*this, CallLoc, Param, Innermost.first, |
| 4711 | Innermost.second); |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4712 | |
Nico Weber | 44887f6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 4713 | ExprResult Result; |
| 4714 | { |
| 4715 | // C++ [dcl.fct.default]p5: |
| 4716 | // The names in the [default argument] expression are bound, and |
| 4717 | // the semantic constraints are checked, at the point where the |
| 4718 | // default argument expression appears. |
Nico Weber | ebd45a0 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 4719 | ContextRAII SavedContext(*this, FD); |
Nico Weber | 44887f6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 4720 | Result = SubstExpr(UninstExpr, ArgList); |
| 4721 | } |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4722 | if (Result.isInvalid()) |
| 4723 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4724 | |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4725 | // Check the expression as an initializer for the parameter. |
| 4726 | InitializedEntity Entity |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 4727 | = InitializedEntity::InitializeParameter(Context, Param); |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4728 | InitializationKind Kind |
| 4729 | = InitializationKind::CreateCopy(Param->getLocation(), |
| 4730 | /*FIXME:EqualLoc*/UninstExpr->getSourceRange().getBegin()); |
| 4731 | Expr *ResultE = Result.takeAs<Expr>(); |
Douglas Gregor | 25ab25f | 2009-12-23 18:19:08 +0000 | [diff] [blame] | 4732 | |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4733 | InitializationSequence InitSeq(*this, Entity, Kind, &ResultE, 1); |
| 4734 | Result = InitSeq.Perform(*this, Entity, Kind, |
| 4735 | MultiExprArg(*this, &ResultE, 1)); |
| 4736 | if (Result.isInvalid()) |
| 4737 | return ExprError(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4738 | |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4739 | // Build the default argument expression. |
| 4740 | return Owned(CXXDefaultArgExpr::Create(Context, CallLoc, Param, |
| 4741 | Result.takeAs<Expr>())); |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4742 | } |
| 4743 | |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4744 | // If the default expression creates temporaries, we need to |
| 4745 | // push them to the current stack of expression temporaries so they'll |
| 4746 | // be properly destroyed. |
| 4747 | // FIXME: We should really be rebuilding the default argument with new |
| 4748 | // bound temporaries; see the comment in PR5810. |
Douglas Gregor | 6ed2fee | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 4749 | for (unsigned i = 0, e = Param->getNumDefaultArgTemporaries(); i != e; ++i) { |
| 4750 | CXXTemporary *Temporary = Param->getDefaultArgTemporary(i); |
| 4751 | MarkDeclarationReferenced(Param->getDefaultArg()->getLocStart(), |
| 4752 | const_cast<CXXDestructorDecl*>(Temporary->getDestructor())); |
| 4753 | ExprTemporaries.push_back(Temporary); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4754 | ExprNeedsCleanups = true; |
Douglas Gregor | 6ed2fee | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 4755 | } |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4756 | |
| 4757 | // We already type-checked the argument, so we know it works. |
Douglas Gregor | 32b3de5 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 4758 | // Just mark all of the declarations in this potentially-evaluated expression |
| 4759 | // as being "referenced". |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 4760 | MarkDeclarationsReferencedInExpr(Param->getDefaultArg()); |
Douglas Gregor | 033f675 | 2009-12-23 23:03:06 +0000 | [diff] [blame] | 4761 | return Owned(CXXDefaultArgExpr::Create(Context, CallLoc, Param)); |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4762 | } |
| 4763 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4764 | /// ConvertArgumentsForCall - Converts the arguments specified in |
| 4765 | /// Args/NumArgs to the parameter types of the function FDecl with |
| 4766 | /// function prototype Proto. Call is the call expression itself, and |
| 4767 | /// Fn is the function expression. For a C++ member function, this |
| 4768 | /// routine does not attempt to convert the object argument. Returns |
| 4769 | /// true if the call is ill-formed. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4770 | bool |
| 4771 | Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4772 | FunctionDecl *FDecl, |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4773 | const FunctionProtoType *Proto, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4774 | Expr **Args, unsigned NumArgs, |
| 4775 | SourceLocation RParenLoc) { |
John McCall | bebede4 | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 4776 | // Bail out early if calling a builtin with custom typechecking. |
| 4777 | // We don't need to do this in the |
| 4778 | if (FDecl) |
| 4779 | if (unsigned ID = FDecl->getBuiltinID()) |
| 4780 | if (Context.BuiltinInfo.hasCustomTypechecking(ID)) |
| 4781 | return false; |
| 4782 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4783 | // 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] | 4784 | // assignment, to the types of the corresponding parameter, ... |
| 4785 | unsigned NumArgsInProto = Proto->getNumArgs(); |
Douglas Gregor | b6b9961 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 4786 | bool Invalid = false; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4787 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4788 | // If too few arguments are available (and we don't have default |
| 4789 | // arguments for the remaining parameters), don't make the call. |
| 4790 | if (NumArgs < NumArgsInProto) { |
| 4791 | if (!FDecl || NumArgs < FDecl->getMinRequiredArguments()) |
| 4792 | return Diag(RParenLoc, diag::err_typecheck_call_too_few_args) |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 4793 | << Fn->getType()->isBlockPointerType() |
Eric Christopher | abf1e18 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 4794 | << NumArgsInProto << NumArgs << Fn->getSourceRange(); |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4795 | Call->setNumArgs(Context, NumArgsInProto); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4796 | } |
| 4797 | |
| 4798 | // If too many are passed and not variadic, error on the extras and drop |
| 4799 | // them. |
| 4800 | if (NumArgs > NumArgsInProto) { |
| 4801 | if (!Proto->isVariadic()) { |
| 4802 | Diag(Args[NumArgsInProto]->getLocStart(), |
| 4803 | diag::err_typecheck_call_too_many_args) |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 4804 | << Fn->getType()->isBlockPointerType() |
Eric Christopher | 2a5aaff | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 4805 | << NumArgsInProto << NumArgs << Fn->getSourceRange() |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4806 | << SourceRange(Args[NumArgsInProto]->getLocStart(), |
| 4807 | Args[NumArgs-1]->getLocEnd()); |
Ted Kremenek | 99a337e | 2011-04-04 17:22:27 +0000 | [diff] [blame] | 4808 | |
| 4809 | // Emit the location of the prototype. |
| 4810 | if (FDecl && !FDecl->getBuiltinID()) |
| 4811 | Diag(FDecl->getLocStart(), |
| 4812 | diag::note_typecheck_call_too_many_args) |
| 4813 | << FDecl; |
| 4814 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4815 | // This deletes the extra arguments. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 4816 | Call->setNumArgs(Context, NumArgsInProto); |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4817 | return true; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4818 | } |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4819 | } |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4820 | llvm::SmallVector<Expr *, 8> AllArgs; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4821 | VariadicCallType CallType = |
Fariborz Jahanian | 6f2d25e | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 4822 | Proto->isVariadic() ? VariadicFunction : VariadicDoesNotApply; |
| 4823 | if (Fn->getType()->isBlockPointerType()) |
| 4824 | CallType = VariadicBlock; // Block |
| 4825 | else if (isa<MemberExpr>(Fn)) |
| 4826 | CallType = VariadicMethod; |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4827 | Invalid = GatherArgumentsForCall(Call->getSourceRange().getBegin(), FDecl, |
Fariborz Jahanian | 4fa66ce | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 4828 | Proto, 0, Args, NumArgs, AllArgs, CallType); |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4829 | if (Invalid) |
| 4830 | return true; |
| 4831 | unsigned TotalNumArgs = AllArgs.size(); |
| 4832 | for (unsigned i = 0; i < TotalNumArgs; ++i) |
| 4833 | Call->setArg(i, AllArgs[i]); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4834 | |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4835 | return false; |
| 4836 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4837 | |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4838 | bool Sema::GatherArgumentsForCall(SourceLocation CallLoc, |
| 4839 | FunctionDecl *FDecl, |
| 4840 | const FunctionProtoType *Proto, |
| 4841 | unsigned FirstProtoArg, |
| 4842 | Expr **Args, unsigned NumArgs, |
| 4843 | llvm::SmallVector<Expr *, 8> &AllArgs, |
Fariborz Jahanian | 6f2d25e | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 4844 | VariadicCallType CallType) { |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4845 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 4846 | unsigned NumArgsToCheck = NumArgs; |
| 4847 | bool Invalid = false; |
| 4848 | if (NumArgs != NumArgsInProto) |
| 4849 | // Use default arguments for missing arguments |
| 4850 | NumArgsToCheck = NumArgsInProto; |
| 4851 | unsigned ArgIx = 0; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4852 | // 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] | 4853 | for (unsigned i = FirstProtoArg; i != NumArgsToCheck; i++) { |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4854 | QualType ProtoArgType = Proto->getArgType(i); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4855 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4856 | Expr *Arg; |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4857 | if (ArgIx < NumArgs) { |
| 4858 | Arg = Args[ArgIx++]; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4859 | |
Eli Friedman | 3164fb1 | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 4860 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 4861 | ProtoArgType, |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 4862 | PDiag(diag::err_call_incomplete_argument) |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4863 | << Arg->getSourceRange())) |
Eli Friedman | 3164fb1 | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 4864 | return true; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4865 | |
Douglas Gregor | bbeb5c3 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 4866 | // Pass the argument |
| 4867 | ParmVarDecl *Param = 0; |
| 4868 | if (FDecl && i < FDecl->getNumParams()) |
| 4869 | Param = FDecl->getParamDecl(i); |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 4870 | |
Douglas Gregor | bbeb5c3 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 4871 | InitializedEntity Entity = |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 4872 | Param? InitializedEntity::InitializeParameter(Context, Param) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4873 | : InitializedEntity::InitializeParameter(Context, ProtoArgType, |
| 4874 | Proto->isArgConsumed(i)); |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4875 | ExprResult ArgE = PerformCopyInitialization(Entity, |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4876 | SourceLocation(), |
| 4877 | Owned(Arg)); |
Douglas Gregor | bbeb5c3 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 4878 | if (ArgE.isInvalid()) |
| 4879 | return true; |
| 4880 | |
| 4881 | Arg = ArgE.takeAs<Expr>(); |
Anders Carlsson | 84613c4 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 4882 | } else { |
Anders Carlsson | c80a127 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 4883 | ParmVarDecl *Param = FDecl->getParamDecl(i); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4884 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4885 | ExprResult ArgExpr = |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4886 | BuildCXXDefaultArgExpr(CallLoc, FDecl, Param); |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4887 | if (ArgExpr.isInvalid()) |
| 4888 | return true; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4889 | |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 4890 | Arg = ArgExpr.takeAs<Expr>(); |
Anders Carlsson | 84613c4 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 4891 | } |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 4892 | AllArgs.push_back(Arg); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4893 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4894 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4895 | // If this is a variadic call, handle args passed through "...". |
Fariborz Jahanian | 6f2d25e | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 4896 | if (CallType != VariadicDoesNotApply) { |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 4897 | |
| 4898 | // Assume that extern "C" functions with variadic arguments that |
| 4899 | // return __unknown_anytype aren't *really* variadic. |
| 4900 | if (Proto->getResultType() == Context.UnknownAnyTy && |
| 4901 | FDecl && FDecl->isExternC()) { |
| 4902 | for (unsigned i = ArgIx; i != NumArgs; ++i) { |
| 4903 | ExprResult arg; |
| 4904 | if (isa<ExplicitCastExpr>(Args[i]->IgnoreParens())) |
| 4905 | arg = DefaultFunctionArrayLvalueConversion(Args[i]); |
| 4906 | else |
| 4907 | arg = DefaultVariadicArgumentPromotion(Args[i], CallType, FDecl); |
| 4908 | Invalid |= arg.isInvalid(); |
| 4909 | AllArgs.push_back(arg.take()); |
| 4910 | } |
| 4911 | |
| 4912 | // Otherwise do argument promotion, (C99 6.5.2.2p7). |
| 4913 | } else { |
| 4914 | for (unsigned i = ArgIx; i != NumArgs; ++i) { |
| 4915 | ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], CallType, FDecl); |
| 4916 | Invalid |= Arg.isInvalid(); |
| 4917 | AllArgs.push_back(Arg.take()); |
| 4918 | } |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4919 | } |
| 4920 | } |
Douglas Gregor | b6b9961 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 4921 | return Invalid; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4922 | } |
| 4923 | |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 4924 | /// Given a function expression of unknown-any type, try to rebuild it |
| 4925 | /// to have a function type. |
| 4926 | static ExprResult rebuildUnknownAnyFunction(Sema &S, Expr *fn); |
| 4927 | |
Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 4928 | /// ActOnCallExpr - Handle a call to Fn with the specified array of arguments. |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 4929 | /// This provides the location of the left/right parens and a list of comma |
| 4930 | /// locations. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4931 | ExprResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4932 | Sema::ActOnCallExpr(Scope *S, Expr *Fn, SourceLocation LParenLoc, |
Peter Collingbourne | 41f8546 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4933 | MultiExprArg args, SourceLocation RParenLoc, |
| 4934 | Expr *ExecConfig) { |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4935 | unsigned NumArgs = args.size(); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4936 | |
| 4937 | // Since this might be a postfix expression, get rid of ParenListExprs. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4938 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Fn); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4939 | if (Result.isInvalid()) return ExprError(); |
| 4940 | Fn = Result.take(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4941 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4942 | Expr **Args = args.release(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4943 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4944 | if (getLangOptions().CPlusPlus) { |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 4945 | // If this is a pseudo-destructor expression, build the call immediately. |
| 4946 | if (isa<CXXPseudoDestructorExpr>(Fn)) { |
| 4947 | if (NumArgs > 0) { |
| 4948 | // Pseudo-destructor calls should not have any arguments. |
| 4949 | Diag(Fn->getLocStart(), diag::err_pseudo_dtor_call_with_args) |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 4950 | << FixItHint::CreateRemoval( |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 4951 | SourceRange(Args[0]->getLocStart(), |
| 4952 | Args[NumArgs-1]->getLocEnd())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4953 | |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 4954 | NumArgs = 0; |
| 4955 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4956 | |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 4957 | return Owned(new (Context) CallExpr(Context, Fn, 0, 0, Context.VoidTy, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4958 | VK_RValue, RParenLoc)); |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 4959 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4960 | |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4961 | // Determine whether this is a dependent call inside a C++ template, |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4962 | // in which case we won't do any semantic analysis now. |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 4963 | // FIXME: Will need to cache the results of name lookup (including ADL) in |
| 4964 | // Fn. |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4965 | bool Dependent = false; |
| 4966 | if (Fn->isTypeDependent()) |
| 4967 | Dependent = true; |
| 4968 | else if (Expr::hasAnyTypeDependentArguments(Args, NumArgs)) |
| 4969 | Dependent = true; |
| 4970 | |
Peter Collingbourne | 41f8546 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 4971 | if (Dependent) { |
| 4972 | if (ExecConfig) { |
| 4973 | return Owned(new (Context) CUDAKernelCallExpr( |
| 4974 | Context, Fn, cast<CallExpr>(ExecConfig), Args, NumArgs, |
| 4975 | Context.DependentTy, VK_RValue, RParenLoc)); |
| 4976 | } else { |
| 4977 | return Owned(new (Context) CallExpr(Context, Fn, Args, NumArgs, |
| 4978 | Context.DependentTy, VK_RValue, |
| 4979 | RParenLoc)); |
| 4980 | } |
| 4981 | } |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4982 | |
| 4983 | // Determine whether this is a call to an object (C++ [over.call.object]). |
| 4984 | if (Fn->getType()->isRecordType()) |
| 4985 | return Owned(BuildCallToObjectOfClassType(S, Fn, LParenLoc, Args, NumArgs, |
Douglas Gregor | ce5aa33 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 4986 | RParenLoc)); |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 4987 | |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 4988 | if (Fn->getType() == Context.UnknownAnyTy) { |
| 4989 | ExprResult result = rebuildUnknownAnyFunction(*this, Fn); |
| 4990 | if (result.isInvalid()) return ExprError(); |
| 4991 | Fn = result.take(); |
| 4992 | } |
| 4993 | |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 4994 | if (Fn->getType() == Context.BoundMemberTy) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 4995 | return BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
Douglas Gregor | ce5aa33 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 4996 | RParenLoc); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4997 | } |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 4998 | } |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 4999 | |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 5000 | // Check for overloaded calls. This can happen even in C due to extensions. |
| 5001 | if (Fn->getType() == Context.OverloadTy) { |
| 5002 | OverloadExpr::FindResult find = OverloadExpr::find(Fn); |
| 5003 | |
| 5004 | // We aren't supposed to apply this logic if there's an '&' involved. |
| 5005 | if (!find.IsAddressOfOperand) { |
| 5006 | OverloadExpr *ovl = find.Expression; |
| 5007 | if (isa<UnresolvedLookupExpr>(ovl)) { |
| 5008 | UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(ovl); |
| 5009 | return BuildOverloadedCallExpr(S, Fn, ULE, LParenLoc, Args, NumArgs, |
| 5010 | RParenLoc, ExecConfig); |
| 5011 | } else { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5012 | return BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
Douglas Gregor | ce5aa33 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 5013 | RParenLoc); |
Anders Carlsson | 61914b5 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 5014 | } |
| 5015 | } |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5016 | } |
| 5017 | |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 5018 | // If we're directly calling a function, get the appropriate declaration. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5019 | |
Eli Friedman | e14b199 | 2009-12-26 03:35:45 +0000 | [diff] [blame] | 5020 | Expr *NakedFn = Fn->IgnoreParens(); |
Douglas Gregor | 928479e | 2010-11-09 20:03:54 +0000 | [diff] [blame] | 5021 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5022 | NamedDecl *NDecl = 0; |
Douglas Gregor | 59f16ed | 2010-10-25 20:48:33 +0000 | [diff] [blame] | 5023 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(NakedFn)) |
| 5024 | if (UnOp->getOpcode() == UO_AddrOf) |
| 5025 | NakedFn = UnOp->getSubExpr()->IgnoreParens(); |
| 5026 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5027 | if (isa<DeclRefExpr>(NakedFn)) |
| 5028 | NDecl = cast<DeclRefExpr>(NakedFn)->getDecl(); |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 5029 | else if (isa<MemberExpr>(NakedFn)) |
| 5030 | NDecl = cast<MemberExpr>(NakedFn)->getMemberDecl(); |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5031 | |
Peter Collingbourne | 41f8546 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 5032 | return BuildResolvedCallExpr(Fn, NDecl, LParenLoc, Args, NumArgs, RParenLoc, |
| 5033 | ExecConfig); |
| 5034 | } |
| 5035 | |
| 5036 | ExprResult |
| 5037 | Sema::ActOnCUDAExecConfigExpr(Scope *S, SourceLocation LLLLoc, |
| 5038 | MultiExprArg execConfig, SourceLocation GGGLoc) { |
| 5039 | FunctionDecl *ConfigDecl = Context.getcudaConfigureCallDecl(); |
| 5040 | if (!ConfigDecl) |
| 5041 | return ExprError(Diag(LLLLoc, diag::err_undeclared_var_use) |
| 5042 | << "cudaConfigureCall"); |
| 5043 | QualType ConfigQTy = ConfigDecl->getType(); |
| 5044 | |
| 5045 | DeclRefExpr *ConfigDR = new (Context) DeclRefExpr( |
| 5046 | ConfigDecl, ConfigQTy, VK_LValue, LLLLoc); |
| 5047 | |
| 5048 | return ActOnCallExpr(S, ConfigDR, LLLLoc, execConfig, GGGLoc, 0); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5049 | } |
| 5050 | |
Tanya Lattner | 55808c1 | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 5051 | /// ActOnAsTypeExpr - create a new asType (bitcast) from the arguments. |
| 5052 | /// |
| 5053 | /// __builtin_astype( value, dst type ) |
| 5054 | /// |
| 5055 | ExprResult Sema::ActOnAsTypeExpr(Expr *expr, ParsedType destty, |
| 5056 | SourceLocation BuiltinLoc, |
| 5057 | SourceLocation RParenLoc) { |
| 5058 | ExprValueKind VK = VK_RValue; |
| 5059 | ExprObjectKind OK = OK_Ordinary; |
| 5060 | QualType DstTy = GetTypeFromParser(destty); |
| 5061 | QualType SrcTy = expr->getType(); |
| 5062 | if (Context.getTypeSize(DstTy) != Context.getTypeSize(SrcTy)) |
| 5063 | return ExprError(Diag(BuiltinLoc, |
| 5064 | diag::err_invalid_astype_of_different_size) |
Peter Collingbourne | 23f1bee | 2011-06-08 15:15:17 +0000 | [diff] [blame] | 5065 | << DstTy |
| 5066 | << SrcTy |
Tanya Lattner | 55808c1 | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 5067 | << expr->getSourceRange()); |
| 5068 | return Owned(new (Context) AsTypeExpr(expr, DstTy, VK, OK, BuiltinLoc, RParenLoc)); |
| 5069 | } |
| 5070 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 5071 | /// BuildResolvedCallExpr - Build a call to a resolved expression, |
| 5072 | /// i.e. an expression not of \p OverloadTy. The expression should |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5073 | /// unary-convert to an expression of function-pointer or |
| 5074 | /// block-pointer type. |
| 5075 | /// |
| 5076 | /// \param NDecl the declaration being called, if available |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5077 | ExprResult |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5078 | Sema::BuildResolvedCallExpr(Expr *Fn, NamedDecl *NDecl, |
| 5079 | SourceLocation LParenLoc, |
| 5080 | Expr **Args, unsigned NumArgs, |
Peter Collingbourne | 41f8546 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 5081 | SourceLocation RParenLoc, |
| 5082 | Expr *Config) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 5083 | FunctionDecl *FDecl = dyn_cast_or_null<FunctionDecl>(NDecl); |
| 5084 | |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5085 | // Promote the function operand. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5086 | ExprResult Result = UsualUnaryConversions(Fn); |
| 5087 | if (Result.isInvalid()) |
| 5088 | return ExprError(); |
| 5089 | Fn = Result.take(); |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 5090 | |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 5091 | // Make the call expr early, before semantic checks. This guarantees cleanup |
| 5092 | // of arguments and function on error. |
Peter Collingbourne | 41f8546 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 5093 | CallExpr *TheCall; |
| 5094 | if (Config) { |
| 5095 | TheCall = new (Context) CUDAKernelCallExpr(Context, Fn, |
| 5096 | cast<CallExpr>(Config), |
| 5097 | Args, NumArgs, |
| 5098 | Context.BoolTy, |
| 5099 | VK_RValue, |
| 5100 | RParenLoc); |
| 5101 | } else { |
| 5102 | TheCall = new (Context) CallExpr(Context, Fn, |
| 5103 | Args, NumArgs, |
| 5104 | Context.BoolTy, |
| 5105 | VK_RValue, |
| 5106 | RParenLoc); |
| 5107 | } |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5108 | |
John McCall | bebede4 | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 5109 | unsigned BuiltinID = (FDecl ? FDecl->getBuiltinID() : 0); |
| 5110 | |
| 5111 | // Bail out early if calling a builtin with custom typechecking. |
| 5112 | if (BuiltinID && Context.BuiltinInfo.hasCustomTypechecking(BuiltinID)) |
| 5113 | return CheckBuiltinFunctionCall(BuiltinID, TheCall); |
| 5114 | |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 5115 | retry: |
Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 5116 | const FunctionType *FuncT; |
John McCall | bebede4 | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 5117 | if (const PointerType *PT = Fn->getType()->getAs<PointerType>()) { |
Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 5118 | // C99 6.5.2.2p1 - "The expression that denotes the called function shall |
| 5119 | // have type pointer to function". |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5120 | FuncT = PT->getPointeeType()->getAs<FunctionType>(); |
John McCall | bebede4 | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 5121 | if (FuncT == 0) |
| 5122 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 5123 | << Fn->getType() << Fn->getSourceRange()); |
| 5124 | } else if (const BlockPointerType *BPT = |
| 5125 | Fn->getType()->getAs<BlockPointerType>()) { |
| 5126 | FuncT = BPT->getPointeeType()->castAs<FunctionType>(); |
| 5127 | } else { |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 5128 | // Handle calls to expressions of unknown-any type. |
| 5129 | if (Fn->getType() == Context.UnknownAnyTy) { |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 5130 | ExprResult rewrite = rebuildUnknownAnyFunction(*this, Fn); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 5131 | if (rewrite.isInvalid()) return ExprError(); |
| 5132 | Fn = rewrite.take(); |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 5133 | TheCall->setCallee(Fn); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 5134 | goto retry; |
| 5135 | } |
| 5136 | |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5137 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 5138 | << Fn->getType() << Fn->getSourceRange()); |
John McCall | bebede4 | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 5139 | } |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5140 | |
Peter Collingbourne | 4b66c47 | 2011-02-23 01:53:29 +0000 | [diff] [blame] | 5141 | if (getLangOptions().CUDA) { |
| 5142 | if (Config) { |
| 5143 | // CUDA: Kernel calls must be to global functions |
| 5144 | if (FDecl && !FDecl->hasAttr<CUDAGlobalAttr>()) |
| 5145 | return ExprError(Diag(LParenLoc,diag::err_kern_call_not_global_function) |
| 5146 | << FDecl->getName() << Fn->getSourceRange()); |
| 5147 | |
| 5148 | // CUDA: Kernel function must have 'void' return type |
| 5149 | if (!FuncT->getResultType()->isVoidType()) |
| 5150 | return ExprError(Diag(LParenLoc, diag::err_kern_type_not_void_return) |
| 5151 | << Fn->getType() << Fn->getSourceRange()); |
| 5152 | } |
| 5153 | } |
| 5154 | |
Eli Friedman | 3164fb1 | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 5155 | // Check for a valid return type |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5156 | if (CheckCallReturnType(FuncT->getResultType(), |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5157 | Fn->getSourceRange().getBegin(), TheCall, |
Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 5158 | FDecl)) |
Eli Friedman | 3164fb1 | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 5159 | return ExprError(); |
| 5160 | |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 5161 | // We know the result type of the call, set it. |
Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 5162 | TheCall->setType(FuncT->getCallResultType(Context)); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5163 | TheCall->setValueKind(Expr::getValueKindForType(FuncT->getResultType())); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5164 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5165 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FuncT)) { |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5166 | if (ConvertArgumentsForCall(TheCall, Fn, FDecl, Proto, Args, NumArgs, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5167 | RParenLoc)) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5168 | return ExprError(); |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 5169 | } else { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 5170 | assert(isa<FunctionNoProtoType>(FuncT) && "Unknown FunctionType!"); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5171 | |
Douglas Gregor | d8e97de | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 5172 | if (FDecl) { |
| 5173 | // Check if we have too few/too many template arguments, based |
| 5174 | // on our knowledge of the function definition. |
| 5175 | const FunctionDecl *Def = 0; |
Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 5176 | if (FDecl->hasBody(Def) && NumArgs != Def->param_size()) { |
Douglas Gregor | 8e09a72 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 5177 | const FunctionProtoType *Proto |
| 5178 | = Def->getType()->getAs<FunctionProtoType>(); |
| 5179 | if (!Proto || !(Proto->isVariadic() && NumArgs >= Def->param_size())) |
Eli Friedman | fcbf7d2 | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 5180 | Diag(RParenLoc, diag::warn_call_wrong_number_of_arguments) |
| 5181 | << (NumArgs > Def->param_size()) << FDecl << Fn->getSourceRange(); |
Eli Friedman | fcbf7d2 | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 5182 | } |
Douglas Gregor | 8e09a72 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 5183 | |
| 5184 | // If the function we're calling isn't a function prototype, but we have |
| 5185 | // a function prototype from a prior declaratiom, use that prototype. |
| 5186 | if (!FDecl->hasPrototype()) |
| 5187 | Proto = FDecl->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | d8e97de | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 5188 | } |
| 5189 | |
Steve Naroff | 0b66158 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 5190 | // Promote the arguments (C99 6.5.2.2p6). |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 5191 | for (unsigned i = 0; i != NumArgs; i++) { |
| 5192 | Expr *Arg = Args[i]; |
Douglas Gregor | 8e09a72 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 5193 | |
| 5194 | if (Proto && i < Proto->getNumArgs()) { |
Douglas Gregor | 8e09a72 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 5195 | InitializedEntity Entity |
| 5196 | = InitializedEntity::InitializeParameter(Context, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5197 | Proto->getArgType(i), |
| 5198 | Proto->isArgConsumed(i)); |
Douglas Gregor | 8e09a72 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 5199 | ExprResult ArgE = PerformCopyInitialization(Entity, |
| 5200 | SourceLocation(), |
| 5201 | Owned(Arg)); |
| 5202 | if (ArgE.isInvalid()) |
| 5203 | return true; |
| 5204 | |
| 5205 | Arg = ArgE.takeAs<Expr>(); |
| 5206 | |
| 5207 | } else { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5208 | ExprResult ArgE = DefaultArgumentPromotion(Arg); |
| 5209 | |
| 5210 | if (ArgE.isInvalid()) |
| 5211 | return true; |
| 5212 | |
| 5213 | Arg = ArgE.takeAs<Expr>(); |
Douglas Gregor | 8e09a72 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 5214 | } |
| 5215 | |
Douglas Gregor | 8302541 | 2010-10-26 05:45:40 +0000 | [diff] [blame] | 5216 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 5217 | Arg->getType(), |
| 5218 | PDiag(diag::err_call_incomplete_argument) |
| 5219 | << Arg->getSourceRange())) |
| 5220 | return ExprError(); |
| 5221 | |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 5222 | TheCall->setArg(i, Arg); |
Steve Naroff | 0b66158 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 5223 | } |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 5224 | } |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 5225 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5226 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(FDecl)) |
| 5227 | if (!Method->isStatic()) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 5228 | return ExprError(Diag(LParenLoc, diag::err_member_call_without_object) |
| 5229 | << Fn->getSourceRange()); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 5230 | |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 5231 | // Check for sentinels |
| 5232 | if (NDecl) |
| 5233 | DiagnoseSentinelCalls(NDecl, LParenLoc, Args, NumArgs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5234 | |
Chris Lattner | b87b1b3 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 5235 | // Do special checking on direct calls to functions. |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 5236 | if (FDecl) { |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5237 | if (CheckFunctionCall(FDecl, TheCall)) |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 5238 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5239 | |
John McCall | bebede4 | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 5240 | if (BuiltinID) |
Fariborz Jahanian | e8473c2 | 2010-11-30 17:35:24 +0000 | [diff] [blame] | 5241 | return CheckBuiltinFunctionCall(BuiltinID, TheCall); |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 5242 | } else if (NDecl) { |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5243 | if (CheckBlockCall(NDecl, TheCall)) |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 5244 | return ExprError(); |
| 5245 | } |
Chris Lattner | b87b1b3 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 5246 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5247 | return MaybeBindToTemporary(TheCall); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 5248 | } |
| 5249 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5250 | ExprResult |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5251 | Sema::ActOnCompoundLiteral(SourceLocation LParenLoc, ParsedType Ty, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5252 | SourceLocation RParenLoc, Expr *InitExpr) { |
Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 5253 | assert((Ty != 0) && "ActOnCompoundLiteral(): missing type"); |
Steve Naroff | 57eb2c5 | 2007-07-19 21:32:11 +0000 | [diff] [blame] | 5254 | // FIXME: put back this assert when initializers are worked out. |
Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 5255 | //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression"); |
John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5256 | |
| 5257 | TypeSourceInfo *TInfo; |
| 5258 | QualType literalType = GetTypeFromParser(Ty, &TInfo); |
| 5259 | if (!TInfo) |
| 5260 | TInfo = Context.getTrivialTypeSourceInfo(literalType); |
| 5261 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5262 | return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, InitExpr); |
John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5263 | } |
| 5264 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5265 | ExprResult |
John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5266 | Sema::BuildCompoundLiteralExpr(SourceLocation LParenLoc, TypeSourceInfo *TInfo, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5267 | SourceLocation RParenLoc, Expr *literalExpr) { |
John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5268 | QualType literalType = TInfo->getType(); |
Anders Carlsson | 2c1ec6d | 2007-12-05 07:24:19 +0000 | [diff] [blame] | 5269 | |
Eli Friedman | 37a186d | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 5270 | if (literalType->isArrayType()) { |
Argyrios Kyrtzidis | 8566356 | 2010-11-08 19:14:19 +0000 | [diff] [blame] | 5271 | if (RequireCompleteType(LParenLoc, Context.getBaseElementType(literalType), |
| 5272 | PDiag(diag::err_illegal_decl_array_incomplete_type) |
| 5273 | << SourceRange(LParenLoc, |
| 5274 | literalExpr->getSourceRange().getEnd()))) |
| 5275 | return ExprError(); |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 5276 | if (literalType->isVariableArrayType()) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5277 | return ExprError(Diag(LParenLoc, diag::err_variable_object_no_init) |
| 5278 | << SourceRange(LParenLoc, literalExpr->getSourceRange().getEnd())); |
Douglas Gregor | 1c37d9e | 2009-05-21 23:48:18 +0000 | [diff] [blame] | 5279 | } else if (!literalType->isDependentType() && |
| 5280 | RequireCompleteType(LParenLoc, literalType, |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 5281 | PDiag(diag::err_typecheck_decl_incomplete_type) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5282 | << SourceRange(LParenLoc, |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 5283 | literalExpr->getSourceRange().getEnd()))) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5284 | return ExprError(); |
Eli Friedman | 37a186d | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 5285 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5286 | InitializedEntity Entity |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 5287 | = InitializedEntity::InitializeTemporary(literalType); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 5288 | InitializationKind Kind |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5289 | = InitializationKind::CreateCStyleCast(LParenLoc, |
| 5290 | SourceRange(LParenLoc, RParenLoc)); |
Eli Friedman | a553d4a | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 5291 | InitializationSequence InitSeq(*this, Entity, Kind, &literalExpr, 1); |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5292 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, |
John McCall | 37ad551 | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 5293 | MultiExprArg(*this, &literalExpr, 1), |
Eli Friedman | a553d4a | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 5294 | &literalType); |
| 5295 | if (Result.isInvalid()) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5296 | return ExprError(); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5297 | literalExpr = Result.get(); |
Steve Naroff | d32419d | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 5298 | |
Chris Lattner | 7941395 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 5299 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
Steve Naroff | d32419d | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 5300 | if (isFileScope) { // 6.5.2.5p3 |
Steve Naroff | 98f7203 | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 5301 | if (CheckForConstantInitializer(literalExpr, literalType)) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5302 | return ExprError(); |
Steve Naroff | 98f7203 | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 5303 | } |
Eli Friedman | a553d4a | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 5304 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5305 | // In C, compound literals are l-values for some reason. |
| 5306 | ExprValueKind VK = getLangOptions().CPlusPlus ? VK_RValue : VK_LValue; |
| 5307 | |
John McCall | 5d7aa7f | 2010-01-19 22:33:45 +0000 | [diff] [blame] | 5308 | return Owned(new (Context) CompoundLiteralExpr(LParenLoc, TInfo, literalType, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5309 | VK, literalExpr, isFileScope)); |
Steve Naroff | fbd0983 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 5310 | } |
| 5311 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5312 | ExprResult |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5313 | Sema::ActOnInitList(SourceLocation LBraceLoc, MultiExprArg initlist, |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5314 | SourceLocation RBraceLoc) { |
| 5315 | unsigned NumInit = initlist.size(); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5316 | Expr **InitList = initlist.release(); |
Anders Carlsson | 4692db0 | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 5317 | |
Steve Naroff | 30d242c | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 5318 | // Semantic analysis for initializers is done by ActOnDeclarator() and |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5319 | // CheckInitializer() - it requires knowledge of the object being intialized. |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5320 | |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 5321 | InitListExpr *E = new (Context) InitListExpr(Context, LBraceLoc, InitList, |
| 5322 | NumInit, RBraceLoc); |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 5323 | E->setType(Context.VoidTy); // FIXME: just a place holder for now. |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5324 | return Owned(E); |
Steve Naroff | fbd0983 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 5325 | } |
| 5326 | |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5327 | /// Prepares for a scalar cast, performing all the necessary stages |
| 5328 | /// except the final cast and returning the kind required. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5329 | static CastKind PrepareScalarCast(Sema &S, ExprResult &Src, QualType DestTy) { |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5330 | // Both Src and Dest are scalar types, i.e. arithmetic or pointer. |
| 5331 | // Also, callers should have filtered out the invalid cases with |
| 5332 | // pointers. Everything else should be possible. |
| 5333 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5334 | QualType SrcTy = Src.get()->getType(); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5335 | if (S.Context.hasSameUnqualifiedType(SrcTy, DestTy)) |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5336 | return CK_NoOp; |
Anders Carlsson | 094c459 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 5337 | |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5338 | switch (SrcTy->getScalarTypeKind()) { |
| 5339 | case Type::STK_MemberPointer: |
| 5340 | llvm_unreachable("member pointer type in C"); |
Abramo Bagnara | ba85497 | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 5341 | |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5342 | case Type::STK_Pointer: |
| 5343 | switch (DestTy->getScalarTypeKind()) { |
| 5344 | case Type::STK_Pointer: |
| 5345 | return DestTy->isObjCObjectPointerType() ? |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5346 | CK_AnyPointerToObjCPointerCast : |
| 5347 | CK_BitCast; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5348 | case Type::STK_Bool: |
| 5349 | return CK_PointerToBoolean; |
| 5350 | case Type::STK_Integral: |
| 5351 | return CK_PointerToIntegral; |
| 5352 | case Type::STK_Floating: |
| 5353 | case Type::STK_FloatingComplex: |
| 5354 | case Type::STK_IntegralComplex: |
| 5355 | case Type::STK_MemberPointer: |
| 5356 | llvm_unreachable("illegal cast from pointer"); |
| 5357 | } |
| 5358 | break; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5359 | |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5360 | case Type::STK_Bool: // casting from bool is like casting from an integer |
| 5361 | case Type::STK_Integral: |
| 5362 | switch (DestTy->getScalarTypeKind()) { |
| 5363 | case Type::STK_Pointer: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5364 | if (Src.get()->isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNull)) |
John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 5365 | return CK_NullToPointer; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5366 | return CK_IntegralToPointer; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5367 | case Type::STK_Bool: |
| 5368 | return CK_IntegralToBoolean; |
| 5369 | case Type::STK_Integral: |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5370 | return CK_IntegralCast; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5371 | case Type::STK_Floating: |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5372 | return CK_IntegralToFloating; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5373 | case Type::STK_IntegralComplex: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5374 | Src = S.ImpCastExprToType(Src.take(), DestTy->getAs<ComplexType>()->getElementType(), |
| 5375 | CK_IntegralCast); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5376 | return CK_IntegralRealToComplex; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5377 | case Type::STK_FloatingComplex: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5378 | Src = S.ImpCastExprToType(Src.take(), DestTy->getAs<ComplexType>()->getElementType(), |
| 5379 | CK_IntegralToFloating); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5380 | return CK_FloatingRealToComplex; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5381 | case Type::STK_MemberPointer: |
| 5382 | llvm_unreachable("member pointer type in C"); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5383 | } |
| 5384 | break; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5385 | |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5386 | case Type::STK_Floating: |
| 5387 | switch (DestTy->getScalarTypeKind()) { |
| 5388 | case Type::STK_Floating: |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5389 | return CK_FloatingCast; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5390 | case Type::STK_Bool: |
| 5391 | return CK_FloatingToBoolean; |
| 5392 | case Type::STK_Integral: |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5393 | return CK_FloatingToIntegral; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5394 | case Type::STK_FloatingComplex: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5395 | Src = S.ImpCastExprToType(Src.take(), DestTy->getAs<ComplexType>()->getElementType(), |
| 5396 | CK_FloatingCast); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5397 | return CK_FloatingRealToComplex; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5398 | case Type::STK_IntegralComplex: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5399 | Src = S.ImpCastExprToType(Src.take(), DestTy->getAs<ComplexType>()->getElementType(), |
| 5400 | CK_FloatingToIntegral); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5401 | return CK_IntegralRealToComplex; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5402 | case Type::STK_Pointer: |
| 5403 | llvm_unreachable("valid float->pointer cast?"); |
| 5404 | case Type::STK_MemberPointer: |
| 5405 | llvm_unreachable("member pointer type in C"); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5406 | } |
| 5407 | break; |
| 5408 | |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5409 | case Type::STK_FloatingComplex: |
| 5410 | switch (DestTy->getScalarTypeKind()) { |
| 5411 | case Type::STK_FloatingComplex: |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5412 | return CK_FloatingComplexCast; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5413 | case Type::STK_IntegralComplex: |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5414 | return CK_FloatingComplexToIntegralComplex; |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5415 | case Type::STK_Floating: { |
Abramo Bagnara | ba85497 | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 5416 | QualType ET = SrcTy->getAs<ComplexType>()->getElementType(); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5417 | if (S.Context.hasSameType(ET, DestTy)) |
| 5418 | return CK_FloatingComplexToReal; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5419 | Src = S.ImpCastExprToType(Src.take(), ET, CK_FloatingComplexToReal); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5420 | return CK_FloatingCast; |
| 5421 | } |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5422 | case Type::STK_Bool: |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5423 | return CK_FloatingComplexToBoolean; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5424 | case Type::STK_Integral: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5425 | Src = S.ImpCastExprToType(Src.take(), SrcTy->getAs<ComplexType>()->getElementType(), |
| 5426 | CK_FloatingComplexToReal); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5427 | return CK_FloatingToIntegral; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5428 | case Type::STK_Pointer: |
| 5429 | llvm_unreachable("valid complex float->pointer cast?"); |
| 5430 | case Type::STK_MemberPointer: |
| 5431 | llvm_unreachable("member pointer type in C"); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5432 | } |
| 5433 | break; |
| 5434 | |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5435 | case Type::STK_IntegralComplex: |
| 5436 | switch (DestTy->getScalarTypeKind()) { |
| 5437 | case Type::STK_FloatingComplex: |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5438 | return CK_IntegralComplexToFloatingComplex; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5439 | case Type::STK_IntegralComplex: |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5440 | return CK_IntegralComplexCast; |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5441 | case Type::STK_Integral: { |
Abramo Bagnara | ba85497 | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 5442 | QualType ET = SrcTy->getAs<ComplexType>()->getElementType(); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5443 | if (S.Context.hasSameType(ET, DestTy)) |
| 5444 | return CK_IntegralComplexToReal; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5445 | Src = S.ImpCastExprToType(Src.take(), ET, CK_IntegralComplexToReal); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 5446 | return CK_IntegralCast; |
| 5447 | } |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5448 | case Type::STK_Bool: |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5449 | return CK_IntegralComplexToBoolean; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5450 | case Type::STK_Floating: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5451 | Src = S.ImpCastExprToType(Src.take(), SrcTy->getAs<ComplexType>()->getElementType(), |
| 5452 | CK_IntegralComplexToReal); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5453 | return CK_IntegralToFloating; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5454 | case Type::STK_Pointer: |
| 5455 | llvm_unreachable("valid complex int->pointer cast?"); |
| 5456 | case Type::STK_MemberPointer: |
| 5457 | llvm_unreachable("member pointer type in C"); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5458 | } |
| 5459 | break; |
Anders Carlsson | 094c459 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 5460 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5461 | |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5462 | llvm_unreachable("Unhandled scalar cast"); |
| 5463 | return CK_BitCast; |
Anders Carlsson | 094c459 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 5464 | } |
| 5465 | |
Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 5466 | /// CheckCastTypes - Check type constraints for casting between types. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5467 | ExprResult Sema::CheckCastTypes(SourceLocation CastStartLoc, SourceRange TyR, |
| 5468 | QualType castType, Expr *castExpr, |
| 5469 | CastKind& Kind, ExprValueKind &VK, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5470 | CXXCastPath &BasePath, bool FunctionalStyle) { |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 5471 | if (castExpr->getType() == Context.UnknownAnyTy) |
| 5472 | return checkUnknownAnyCast(TyR, castType, castExpr, Kind, VK, BasePath); |
| 5473 | |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 5474 | if (getLangOptions().CPlusPlus) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5475 | return CXXCheckCStyleCast(SourceRange(CastStartLoc, |
Douglas Gregor | 15417cf | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 5476 | castExpr->getLocEnd()), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5477 | castType, VK, castExpr, Kind, BasePath, |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 5478 | FunctionalStyle); |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 5479 | |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 5480 | assert(!castExpr->getType()->isPlaceholderType()); |
| 5481 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5482 | // We only support r-value casts in C. |
| 5483 | VK = VK_RValue; |
| 5484 | |
Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 5485 | // C99 6.5.4p2: the cast type needs to be void or scalar and the expression |
| 5486 | // type needs to be scalar. |
| 5487 | if (castType->isVoidType()) { |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 5488 | // We don't necessarily do lvalue-to-rvalue conversions on this. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5489 | ExprResult castExprRes = IgnoredValueConversions(castExpr); |
| 5490 | if (castExprRes.isInvalid()) |
| 5491 | return ExprError(); |
| 5492 | castExpr = castExprRes.take(); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 5493 | |
Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 5494 | // Cast to void allows any expr type. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5495 | Kind = CK_ToVoid; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5496 | return Owned(castExpr); |
Anders Carlsson | ef918ac | 2009-10-16 02:35:04 +0000 | [diff] [blame] | 5497 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5498 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5499 | ExprResult castExprRes = DefaultFunctionArrayLvalueConversion(castExpr); |
| 5500 | if (castExprRes.isInvalid()) |
| 5501 | return ExprError(); |
| 5502 | castExpr = castExprRes.take(); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 5503 | |
Eli Friedman | e98194d | 2010-07-17 20:43:49 +0000 | [diff] [blame] | 5504 | if (RequireCompleteType(TyR.getBegin(), castType, |
| 5505 | diag::err_typecheck_cast_to_incomplete)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5506 | return ExprError(); |
Eli Friedman | e98194d | 2010-07-17 20:43:49 +0000 | [diff] [blame] | 5507 | |
Anders Carlsson | ef918ac | 2009-10-16 02:35:04 +0000 | [diff] [blame] | 5508 | if (!castType->isScalarType() && !castType->isVectorType()) { |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 5509 | if (Context.hasSameUnqualifiedType(castType, castExpr->getType()) && |
Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 5510 | (castType->isStructureType() || castType->isUnionType())) { |
| 5511 | // GCC struct/union extension: allow cast to self. |
Eli Friedman | ba961a9 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 5512 | // FIXME: Check that the cast destination type is complete. |
Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 5513 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_nonscalar) |
| 5514 | << castType << castExpr->getSourceRange(); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5515 | Kind = CK_NoOp; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5516 | return Owned(castExpr); |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5517 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5518 | |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5519 | if (castType->isUnionType()) { |
Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 5520 | // GCC cast to union extension |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5521 | RecordDecl *RD = castType->getAs<RecordType>()->getDecl(); |
Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 5522 | RecordDecl::field_iterator Field, FieldEnd; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5523 | for (Field = RD->field_begin(), FieldEnd = RD->field_end(); |
Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 5524 | Field != FieldEnd; ++Field) { |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5525 | if (Context.hasSameUnqualifiedType(Field->getType(), |
Abramo Bagnara | 5d3e724 | 2010-10-07 21:20:44 +0000 | [diff] [blame] | 5526 | castExpr->getType()) && |
| 5527 | !Field->isUnnamedBitfield()) { |
Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 5528 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_to_union) |
| 5529 | << castExpr->getSourceRange(); |
| 5530 | break; |
| 5531 | } |
| 5532 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5533 | if (Field == FieldEnd) { |
| 5534 | Diag(TyR.getBegin(), diag::err_typecheck_cast_to_union_no_type) |
Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 5535 | << castExpr->getType() << castExpr->getSourceRange(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5536 | return ExprError(); |
| 5537 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5538 | Kind = CK_ToUnion; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5539 | return Owned(castExpr); |
Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 5540 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5541 | |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5542 | // Reject any other conversions to non-scalar types. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5543 | Diag(TyR.getBegin(), diag::err_typecheck_cond_expect_scalar) |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5544 | << castType << castExpr->getSourceRange(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5545 | return ExprError(); |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5546 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5547 | |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5548 | // The type we're casting to is known to be a scalar or vector. |
| 5549 | |
| 5550 | // Require the operand to be a scalar or vector. |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5551 | if (!castExpr->getType()->isScalarType() && |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5552 | !castExpr->getType()->isVectorType()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5553 | Diag(castExpr->getLocStart(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5554 | diag::err_typecheck_expect_scalar_operand) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5555 | << castExpr->getType() << castExpr->getSourceRange(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5556 | return ExprError(); |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5557 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5558 | |
| 5559 | if (castType->isExtVectorType()) |
Anders Carlsson | 43d70f8 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 5560 | return CheckExtVectorCast(TyR, castType, castExpr, Kind); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5561 | |
Anton Yartsev | 28ccef7 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5562 | if (castType->isVectorType()) { |
| 5563 | if (castType->getAs<VectorType>()->getVectorKind() == |
| 5564 | VectorType::AltiVecVector && |
| 5565 | (castExpr->getType()->isIntegerType() || |
| 5566 | castExpr->getType()->isFloatingType())) { |
| 5567 | Kind = CK_VectorSplat; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5568 | return Owned(castExpr); |
| 5569 | } else if (CheckVectorCast(TyR, castType, castExpr->getType(), Kind)) { |
| 5570 | return ExprError(); |
Anton Yartsev | 28ccef7 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5571 | } else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5572 | return Owned(castExpr); |
Anton Yartsev | 28ccef7 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5573 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5574 | if (castExpr->getType()->isVectorType()) { |
| 5575 | if (CheckVectorCast(TyR, castExpr->getType(), castType, Kind)) |
| 5576 | return ExprError(); |
| 5577 | else |
| 5578 | return Owned(castExpr); |
| 5579 | } |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5580 | |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5581 | // The source and target types are both scalars, i.e. |
| 5582 | // - arithmetic types (fundamental, enum, and complex) |
| 5583 | // - all kinds of pointers |
| 5584 | // Note that member pointers were filtered out with C++, above. |
| 5585 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5586 | if (isa<ObjCSelectorExpr>(castExpr)) { |
| 5587 | Diag(castExpr->getLocStart(), diag::err_cast_selector_expr); |
| 5588 | return ExprError(); |
| 5589 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5590 | |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5591 | // If either type is a pointer, the other type has to be either an |
| 5592 | // integer or a pointer. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5593 | QualType castExprType = castExpr->getType(); |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5594 | if (!castType->isArithmeticType()) { |
Douglas Gregor | 6972a62 | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 5595 | if (!castExprType->isIntegralType(Context) && |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5596 | castExprType->isArithmeticType()) { |
| 5597 | Diag(castExpr->getLocStart(), |
| 5598 | diag::err_cast_pointer_from_non_pointer_int) |
Eli Friedman | f4e3ad6 | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 5599 | << castExprType << castExpr->getSourceRange(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5600 | return ExprError(); |
| 5601 | } |
Eli Friedman | f4e3ad6 | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 5602 | } else if (!castExpr->getType()->isArithmeticType()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5603 | if (!castType->isIntegralType(Context) && castType->isArithmeticType()) { |
| 5604 | Diag(castExpr->getLocStart(), diag::err_cast_pointer_to_non_pointer_int) |
Eli Friedman | f4e3ad6 | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 5605 | << castType << castExpr->getSourceRange(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5606 | return ExprError(); |
| 5607 | } |
Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 5608 | } |
Anders Carlsson | 094c459 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 5609 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5610 | if (getLangOptions().ObjCAutoRefCount) { |
| 5611 | // Diagnose problems with Objective-C casts involving lifetime qualifiers. |
| 5612 | CheckObjCARCConversion(SourceRange(CastStartLoc, castExpr->getLocEnd()), |
| 5613 | castType, castExpr, CCK_CStyleCast); |
| 5614 | |
| 5615 | if (const PointerType *CastPtr = castType->getAs<PointerType>()) { |
| 5616 | if (const PointerType *ExprPtr = castExprType->getAs<PointerType>()) { |
| 5617 | Qualifiers CastQuals = CastPtr->getPointeeType().getQualifiers(); |
| 5618 | Qualifiers ExprQuals = ExprPtr->getPointeeType().getQualifiers(); |
| 5619 | if (CastPtr->getPointeeType()->isObjCLifetimeType() && |
| 5620 | ExprPtr->getPointeeType()->isObjCLifetimeType() && |
| 5621 | !CastQuals.compatiblyIncludesObjCLifetime(ExprQuals)) { |
| 5622 | Diag(castExpr->getLocStart(), |
| 5623 | diag::err_typecheck_incompatible_lifetime) |
| 5624 | << castExprType << castType << AA_Casting |
| 5625 | << castExpr->getSourceRange(); |
| 5626 | |
| 5627 | return ExprError(); |
| 5628 | } |
| 5629 | } |
| 5630 | } |
| 5631 | } |
| 5632 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5633 | castExprRes = Owned(castExpr); |
| 5634 | Kind = PrepareScalarCast(*this, castExprRes, castType); |
| 5635 | if (castExprRes.isInvalid()) |
| 5636 | return ExprError(); |
| 5637 | castExpr = castExprRes.take(); |
John McCall | 2b5c1b2 | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 5638 | |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 5639 | if (Kind == CK_BitCast) |
John McCall | 2b5c1b2 | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 5640 | CheckCastAlign(castExpr, castType, TyR); |
| 5641 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5642 | return Owned(castExpr); |
Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 5643 | } |
| 5644 | |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 5645 | bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5646 | CastKind &Kind) { |
Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 5647 | assert(VectorTy->isVectorType() && "Not a vector type!"); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5648 | |
Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 5649 | if (Ty->isVectorType() || Ty->isIntegerType()) { |
Chris Lattner | 37e0587 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 5650 | if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty)) |
Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 5651 | return Diag(R.getBegin(), |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5652 | Ty->isVectorType() ? |
Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 5653 | diag::err_invalid_conversion_between_vectors : |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5654 | diag::err_invalid_conversion_between_vector_and_integer) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5655 | << VectorTy << Ty << R; |
Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 5656 | } else |
| 5657 | return Diag(R.getBegin(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 5658 | diag::err_invalid_conversion_between_vector_and_scalar) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 5659 | << VectorTy << Ty << R; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5660 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5661 | Kind = CK_BitCast; |
Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 5662 | return false; |
| 5663 | } |
| 5664 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5665 | ExprResult Sema::CheckExtVectorCast(SourceRange R, QualType DestTy, |
| 5666 | Expr *CastExpr, CastKind &Kind) { |
Nate Begeman | c69b740 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 5667 | assert(DestTy->isExtVectorType() && "Not an extended vector type!"); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5668 | |
Anders Carlsson | 43d70f8 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 5669 | QualType SrcTy = CastExpr->getType(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5670 | |
Nate Begeman | c8961a4 | 2009-06-27 22:05:55 +0000 | [diff] [blame] | 5671 | // If SrcTy is a VectorType, the total size must match to explicitly cast to |
| 5672 | // an ExtVectorType. |
Nate Begeman | c69b740 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 5673 | if (SrcTy->isVectorType()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5674 | if (Context.getTypeSize(DestTy) != Context.getTypeSize(SrcTy)) { |
| 5675 | Diag(R.getBegin(),diag::err_invalid_conversion_between_ext_vectors) |
Nate Begeman | c69b740 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 5676 | << DestTy << SrcTy << R; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5677 | return ExprError(); |
| 5678 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5679 | Kind = CK_BitCast; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5680 | return Owned(CastExpr); |
Nate Begeman | c69b740 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 5681 | } |
| 5682 | |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5683 | // All non-pointer scalars can be cast to ExtVector type. The appropriate |
Nate Begeman | c69b740 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 5684 | // conversion will take place first from scalar to elt type, and then |
| 5685 | // splat from elt type to vector. |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5686 | if (SrcTy->isPointerType()) |
| 5687 | return Diag(R.getBegin(), |
| 5688 | diag::err_invalid_conversion_between_vector_and_scalar) |
| 5689 | << DestTy << SrcTy << R; |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 5690 | |
| 5691 | QualType DestElemTy = DestTy->getAs<ExtVectorType>()->getElementType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5692 | ExprResult CastExprRes = Owned(CastExpr); |
| 5693 | CastKind CK = PrepareScalarCast(*this, CastExprRes, DestElemTy); |
| 5694 | if (CastExprRes.isInvalid()) |
| 5695 | return ExprError(); |
| 5696 | CastExpr = ImpCastExprToType(CastExprRes.take(), DestElemTy, CK).take(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5697 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 5698 | Kind = CK_VectorSplat; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5699 | return Owned(CastExpr); |
Nate Begeman | c69b740 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 5700 | } |
| 5701 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5702 | ExprResult |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5703 | Sema::ActOnCastExpr(Scope *S, SourceLocation LParenLoc, ParsedType Ty, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5704 | SourceLocation RParenLoc, Expr *castExpr) { |
| 5705 | assert((Ty != 0) && (castExpr != 0) && |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5706 | "ActOnCastExpr(): missing type or expr"); |
Steve Naroff | 1a2cf6b | 2007-07-16 23:25:18 +0000 | [diff] [blame] | 5707 | |
John McCall | 9751396 | 2010-01-15 18:39:57 +0000 | [diff] [blame] | 5708 | TypeSourceInfo *castTInfo; |
| 5709 | QualType castType = GetTypeFromParser(Ty, &castTInfo); |
| 5710 | if (!castTInfo) |
John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5711 | castTInfo = Context.getTrivialTypeSourceInfo(castType); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5712 | |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5713 | // If the Expr being casted is a ParenListExpr, handle it specially. |
| 5714 | if (isa<ParenListExpr>(castExpr)) |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5715 | return ActOnCastOfParenListExpr(S, LParenLoc, RParenLoc, castExpr, |
John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5716 | castTInfo); |
John McCall | ebe5474 | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 5717 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5718 | return BuildCStyleCastExpr(LParenLoc, castTInfo, RParenLoc, castExpr); |
John McCall | ebe5474 | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 5719 | } |
| 5720 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5721 | ExprResult |
John McCall | ebe5474 | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 5722 | Sema::BuildCStyleCastExpr(SourceLocation LParenLoc, TypeSourceInfo *Ty, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5723 | SourceLocation RParenLoc, Expr *castExpr) { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5724 | CastKind Kind = CK_Invalid; |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5725 | ExprValueKind VK = VK_RValue; |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5726 | CXXCastPath BasePath; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5727 | ExprResult CastResult = |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5728 | CheckCastTypes(LParenLoc, SourceRange(LParenLoc, RParenLoc), Ty->getType(), |
| 5729 | castExpr, Kind, VK, BasePath); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5730 | if (CastResult.isInvalid()) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5731 | return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5732 | castExpr = CastResult.take(); |
Anders Carlsson | e9766d5 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 5733 | |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5734 | return Owned(CStyleCastExpr::Create(Context, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5735 | Ty->getType().getNonLValueExprType(Context), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5736 | VK, Kind, castExpr, &BasePath, Ty, |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 5737 | LParenLoc, RParenLoc)); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 5738 | } |
| 5739 | |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5740 | /// This is not an AltiVec-style cast, so turn the ParenListExpr into a sequence |
| 5741 | /// of comma binary operators. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5742 | ExprResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5743 | Sema::MaybeConvertParenListExprToParenExpr(Scope *S, Expr *expr) { |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5744 | ParenListExpr *E = dyn_cast<ParenListExpr>(expr); |
| 5745 | if (!E) |
| 5746 | return Owned(expr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5747 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5748 | ExprResult Result(E->getExpr(0)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5749 | |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5750 | for (unsigned i = 1, e = E->getNumExprs(); i != e && !Result.isInvalid(); ++i) |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5751 | Result = ActOnBinOp(S, E->getExprLoc(), tok::comma, Result.get(), |
| 5752 | E->getExpr(i)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5753 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5754 | if (Result.isInvalid()) return ExprError(); |
| 5755 | |
| 5756 | return ActOnParenExpr(E->getLParenLoc(), E->getRParenLoc(), Result.get()); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5757 | } |
| 5758 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5759 | ExprResult |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5760 | Sema::ActOnCastOfParenListExpr(Scope *S, SourceLocation LParenLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5761 | SourceLocation RParenLoc, Expr *Op, |
John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5762 | TypeSourceInfo *TInfo) { |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5763 | ParenListExpr *PE = cast<ParenListExpr>(Op); |
John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5764 | QualType Ty = TInfo->getType(); |
Anton Yartsev | 28ccef7 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5765 | bool isVectorLiteral = false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5766 | |
Anton Yartsev | 28ccef7 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5767 | // Check for an altivec or OpenCL literal, |
John Thompson | 781ad17 | 2010-06-30 22:55:51 +0000 | [diff] [blame] | 5768 | // i.e. all the elements are integer constants. |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5769 | if (getLangOptions().AltiVec && Ty->isVectorType()) { |
| 5770 | if (PE->getNumExprs() == 0) { |
| 5771 | Diag(PE->getExprLoc(), diag::err_altivec_empty_initializer); |
| 5772 | return ExprError(); |
| 5773 | } |
John Thompson | 781ad17 | 2010-06-30 22:55:51 +0000 | [diff] [blame] | 5774 | if (PE->getNumExprs() == 1) { |
| 5775 | if (!PE->getExpr(0)->getType()->isVectorType()) |
Anton Yartsev | 28ccef7 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5776 | isVectorLiteral = true; |
John Thompson | 781ad17 | 2010-06-30 22:55:51 +0000 | [diff] [blame] | 5777 | } |
| 5778 | else |
Anton Yartsev | 28ccef7 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5779 | isVectorLiteral = true; |
John Thompson | 781ad17 | 2010-06-30 22:55:51 +0000 | [diff] [blame] | 5780 | } |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5781 | |
Anton Yartsev | 28ccef7 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5782 | // If this is a vector initializer, '(' type ')' '(' init, ..., init ')' |
John Thompson | 781ad17 | 2010-06-30 22:55:51 +0000 | [diff] [blame] | 5783 | // then handle it as such. |
Anton Yartsev | 28ccef7 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5784 | if (isVectorLiteral) { |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5785 | llvm::SmallVector<Expr *, 8> initExprs; |
Anton Yartsev | 28ccef7 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5786 | // '(...)' form of vector initialization in AltiVec: the number of |
| 5787 | // initializers must be one or must match the size of the vector. |
| 5788 | // If a single value is specified in the initializer then it will be |
| 5789 | // replicated to all the components of the vector |
| 5790 | if (Ty->getAs<VectorType>()->getVectorKind() == |
| 5791 | VectorType::AltiVecVector) { |
| 5792 | unsigned numElems = Ty->getAs<VectorType>()->getNumElements(); |
| 5793 | // The number of initializers must be one or must match the size of the |
| 5794 | // vector. If a single value is specified in the initializer then it will |
| 5795 | // be replicated to all the components of the vector |
| 5796 | if (PE->getNumExprs() == 1) { |
| 5797 | QualType ElemTy = Ty->getAs<VectorType>()->getElementType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5798 | ExprResult Literal = Owned(PE->getExpr(0)); |
| 5799 | Literal = ImpCastExprToType(Literal.take(), ElemTy, |
| 5800 | PrepareScalarCast(*this, Literal, ElemTy)); |
| 5801 | return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Literal.take()); |
Anton Yartsev | 28ccef7 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 5802 | } |
| 5803 | else if (PE->getNumExprs() < numElems) { |
| 5804 | Diag(PE->getExprLoc(), |
| 5805 | diag::err_incorrect_number_of_vector_initializers); |
| 5806 | return ExprError(); |
| 5807 | } |
| 5808 | else |
| 5809 | for (unsigned i = 0, e = PE->getNumExprs(); i != e; ++i) |
| 5810 | initExprs.push_back(PE->getExpr(i)); |
| 5811 | } |
| 5812 | else |
| 5813 | for (unsigned i = 0, e = PE->getNumExprs(); i != e; ++i) |
| 5814 | initExprs.push_back(PE->getExpr(i)); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5815 | |
| 5816 | // FIXME: This means that pretty-printing the final AST will produce curly |
| 5817 | // braces instead of the original commas. |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 5818 | InitListExpr *E = new (Context) InitListExpr(Context, LParenLoc, |
| 5819 | &initExprs[0], |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5820 | initExprs.size(), RParenLoc); |
| 5821 | E->setType(Ty); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5822 | return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, E); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5823 | } else { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5824 | // 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] | 5825 | // sequence of BinOp comma operators. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5826 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Op); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 5827 | if (Result.isInvalid()) return ExprError(); |
| 5828 | return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Result.take()); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5829 | } |
| 5830 | } |
| 5831 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5832 | ExprResult Sema::ActOnParenOrParenListExpr(SourceLocation L, |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5833 | SourceLocation R, |
Fariborz Jahanian | 906d871 | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 5834 | MultiExprArg Val, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 5835 | ParsedType TypeOfCast) { |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5836 | unsigned nexprs = Val.size(); |
| 5837 | Expr **exprs = reinterpret_cast<Expr**>(Val.release()); |
Fariborz Jahanian | 906d871 | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 5838 | assert((exprs != 0) && "ActOnParenOrParenListExpr() missing expr list"); |
| 5839 | Expr *expr; |
| 5840 | if (nexprs == 1 && TypeOfCast && !TypeIsVectorType(TypeOfCast)) |
| 5841 | expr = new (Context) ParenExpr(L, R, exprs[0]); |
| 5842 | else |
| 5843 | expr = new (Context) ParenListExpr(Context, L, exprs, nexprs, R); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5844 | return Owned(expr); |
| 5845 | } |
| 5846 | |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5847 | /// \brief Emit a specialized diagnostic when one expression is a null pointer |
| 5848 | /// constant and the other is not a pointer. |
| 5849 | bool Sema::DiagnoseConditionalForNull(Expr *LHS, Expr *RHS, |
| 5850 | SourceLocation QuestionLoc) { |
| 5851 | Expr *NullExpr = LHS; |
| 5852 | Expr *NonPointerExpr = RHS; |
| 5853 | Expr::NullPointerConstantKind NullKind = |
| 5854 | NullExpr->isNullPointerConstant(Context, |
| 5855 | Expr::NPC_ValueDependentIsNotNull); |
| 5856 | |
| 5857 | if (NullKind == Expr::NPCK_NotNull) { |
| 5858 | NullExpr = RHS; |
| 5859 | NonPointerExpr = LHS; |
| 5860 | NullKind = |
| 5861 | NullExpr->isNullPointerConstant(Context, |
| 5862 | Expr::NPC_ValueDependentIsNotNull); |
| 5863 | } |
| 5864 | |
| 5865 | if (NullKind == Expr::NPCK_NotNull) |
| 5866 | return false; |
| 5867 | |
| 5868 | if (NullKind == Expr::NPCK_ZeroInteger) { |
| 5869 | // In this case, check to make sure that we got here from a "NULL" |
| 5870 | // string in the source code. |
| 5871 | NullExpr = NullExpr->IgnoreParenImpCasts(); |
John McCall | 462c055 | 2011-03-08 07:59:04 +0000 | [diff] [blame] | 5872 | SourceLocation loc = NullExpr->getExprLoc(); |
| 5873 | if (!findMacroSpelling(loc, "NULL")) |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 5874 | return false; |
| 5875 | } |
| 5876 | |
| 5877 | int DiagType = (NullKind == Expr::NPCK_CXX0X_nullptr); |
| 5878 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands_null) |
| 5879 | << NonPointerExpr->getType() << DiagType |
| 5880 | << NonPointerExpr->getSourceRange(); |
| 5881 | return true; |
| 5882 | } |
| 5883 | |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 5884 | /// Note that lhs is not null here, even if this is the gnu "x ?: y" extension. |
| 5885 | /// In that case, lhs = cond. |
Chris Lattner | 2c48660 | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 5886 | /// C99 6.5.15 |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5887 | QualType Sema::CheckConditionalOperands(ExprResult &Cond, ExprResult &LHS, ExprResult &RHS, |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5888 | ExprValueKind &VK, ExprObjectKind &OK, |
Chris Lattner | 2c48660 | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 5889 | SourceLocation QuestionLoc) { |
Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 5890 | |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 5891 | ExprResult lhsResult = CheckPlaceholderExpr(LHS.get()); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 5892 | if (!lhsResult.isUsable()) return QualType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5893 | LHS = move(lhsResult); |
Douglas Gregor | 0124e9b | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 5894 | |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 5895 | ExprResult rhsResult = CheckPlaceholderExpr(RHS.get()); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 5896 | if (!rhsResult.isUsable()) return QualType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5897 | RHS = move(rhsResult); |
Douglas Gregor | 0124e9b | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 5898 | |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5899 | // C++ is sufficiently different to merit its own checker. |
| 5900 | if (getLangOptions().CPlusPlus) |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5901 | return CXXCheckConditionalOperands(Cond, LHS, RHS, VK, OK, QuestionLoc); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5902 | |
| 5903 | VK = VK_RValue; |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 5904 | OK = OK_Ordinary; |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5905 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5906 | Cond = UsualUnaryConversions(Cond.take()); |
| 5907 | if (Cond.isInvalid()) |
| 5908 | return QualType(); |
| 5909 | LHS = UsualUnaryConversions(LHS.take()); |
| 5910 | if (LHS.isInvalid()) |
| 5911 | return QualType(); |
| 5912 | RHS = UsualUnaryConversions(RHS.take()); |
| 5913 | if (RHS.isInvalid()) |
| 5914 | return QualType(); |
| 5915 | |
| 5916 | QualType CondTy = Cond.get()->getType(); |
| 5917 | QualType LHSTy = LHS.get()->getType(); |
| 5918 | QualType RHSTy = RHS.get()->getType(); |
Steve Naroff | 3109001 | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 5919 | |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 5920 | // first, check the condition. |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 5921 | if (!CondTy->isScalarType()) { // C99 6.5.15p2 |
Nate Begeman | abb5a73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 5922 | // OpenCL: Sec 6.3.i says the condition is allowed to be a vector or scalar. |
| 5923 | // Throw an error if its not either. |
| 5924 | if (getLangOptions().OpenCL) { |
| 5925 | if (!CondTy->isVectorType()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5926 | Diag(Cond.get()->getLocStart(), |
Nate Begeman | abb5a73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 5927 | diag::err_typecheck_cond_expect_scalar_or_vector) |
| 5928 | << CondTy; |
| 5929 | return QualType(); |
| 5930 | } |
| 5931 | } |
| 5932 | else { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5933 | Diag(Cond.get()->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
Nate Begeman | abb5a73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 5934 | << CondTy; |
| 5935 | return QualType(); |
| 5936 | } |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 5937 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5938 | |
Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5939 | // Now check the two expressions. |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 5940 | if (LHSTy->isVectorType() || RHSTy->isVectorType()) |
| 5941 | return CheckVectorOperands(QuestionLoc, LHS, RHS); |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 5942 | |
Nate Begeman | abb5a73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 5943 | // OpenCL: If the condition is a vector, and both operands are scalar, |
| 5944 | // attempt to implicity convert them to the vector type to act like the |
| 5945 | // built in select. |
| 5946 | if (getLangOptions().OpenCL && CondTy->isVectorType()) { |
| 5947 | // Both operands should be of scalar type. |
| 5948 | if (!LHSTy->isScalarType()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5949 | Diag(LHS.get()->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
Nate Begeman | abb5a73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 5950 | << CondTy; |
| 5951 | return QualType(); |
| 5952 | } |
| 5953 | if (!RHSTy->isScalarType()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5954 | Diag(RHS.get()->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
Nate Begeman | abb5a73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 5955 | << CondTy; |
| 5956 | return QualType(); |
| 5957 | } |
| 5958 | // Implicity convert these scalars to the type of the condition. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5959 | LHS = ImpCastExprToType(LHS.take(), CondTy, CK_IntegralCast); |
| 5960 | RHS = ImpCastExprToType(RHS.take(), CondTy, CK_IntegralCast); |
Nate Begeman | abb5a73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 5961 | } |
| 5962 | |
Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5963 | // If both operands have arithmetic type, do the usual arithmetic conversions |
| 5964 | // to find a common type: C99 6.5.15p3,5. |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5965 | if (LHSTy->isArithmeticType() && RHSTy->isArithmeticType()) { |
| 5966 | UsualArithmeticConversions(LHS, RHS); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5967 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 5968 | return QualType(); |
| 5969 | return LHS.get()->getType(); |
Steve Naroff | dbd9e89 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 5970 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5971 | |
Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5972 | // If both operands are the same structure or union type, the result is that |
| 5973 | // type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5974 | if (const RecordType *LHSRT = LHSTy->getAs<RecordType>()) { // C99 6.5.15p3 |
| 5975 | if (const RecordType *RHSRT = RHSTy->getAs<RecordType>()) |
Chris Lattner | 2ab40a6 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 5976 | if (LHSRT->getDecl() == RHSRT->getDecl()) |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5977 | // "If both the operands have structure or union type, the result has |
Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5978 | // that type." This implies that CV qualifiers are dropped. |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5979 | return LHSTy.getUnqualifiedType(); |
Eli Friedman | ba961a9 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 5980 | // FIXME: Type of conditional expression must be complete in C mode. |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 5981 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5982 | |
Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5983 | // 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] | 5984 | // The following || allows only one side to be void (a GCC-ism). |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5985 | if (LHSTy->isVoidType() || RHSTy->isVoidType()) { |
| 5986 | if (!LHSTy->isVoidType()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5987 | Diag(RHS.get()->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 5988 | << RHS.get()->getSourceRange(); |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5989 | if (!RHSTy->isVoidType()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5990 | Diag(LHS.get()->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 5991 | << LHS.get()->getSourceRange(); |
| 5992 | LHS = ImpCastExprToType(LHS.take(), Context.VoidTy, CK_ToVoid); |
| 5993 | RHS = ImpCastExprToType(RHS.take(), Context.VoidTy, CK_ToVoid); |
Eli Friedman | 3e1852f | 2008-06-04 19:47:51 +0000 | [diff] [blame] | 5994 | return Context.VoidTy; |
Steve Naroff | bf1516c | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 5995 | } |
Steve Naroff | 039ad3c | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 5996 | // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has |
| 5997 | // the type of the other operand." |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 5998 | if ((LHSTy->isAnyPointerType() || LHSTy->isBlockPointerType()) && |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5999 | RHS.get()->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 6000 | // promote the null to a pointer. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6001 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_NullToPointer); |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 6002 | return LHSTy; |
Steve Naroff | 039ad3c | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 6003 | } |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 6004 | if ((RHSTy->isAnyPointerType() || RHSTy->isBlockPointerType()) && |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6005 | LHS.get()->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNull)) { |
| 6006 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_NullToPointer); |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 6007 | return RHSTy; |
Steve Naroff | 039ad3c | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 6008 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6009 | |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6010 | // All objective-c pointer type analysis is done here. |
| 6011 | QualType compositeType = FindCompositeObjCPointerType(LHS, RHS, |
| 6012 | QuestionLoc); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6013 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 6014 | return QualType(); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6015 | if (!compositeType.isNull()) |
| 6016 | return compositeType; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6017 | |
| 6018 | |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6019 | // Handle block pointer types. |
| 6020 | if (LHSTy->isBlockPointerType() || RHSTy->isBlockPointerType()) { |
| 6021 | if (!LHSTy->isBlockPointerType() || !RHSTy->isBlockPointerType()) { |
| 6022 | if (LHSTy->isVoidPointerType() || RHSTy->isVoidPointerType()) { |
| 6023 | QualType destType = Context.getPointerType(Context.VoidTy); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6024 | LHS = ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
| 6025 | RHS = ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6026 | return destType; |
| 6027 | } |
| 6028 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6029 | << LHSTy << RHSTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6030 | return QualType(); |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 6031 | } |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6032 | // We have 2 block pointer types. |
| 6033 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 6034 | // Two identical block pointer types are always compatible. |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 6035 | return LHSTy; |
| 6036 | } |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6037 | // The block pointer types aren't identical, continue checking. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6038 | QualType lhptee = LHSTy->getAs<BlockPointerType>()->getPointeeType(); |
| 6039 | QualType rhptee = RHSTy->getAs<BlockPointerType>()->getPointeeType(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6040 | |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6041 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 6042 | rhptee.getUnqualifiedType())) { |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 6043 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6044 | << LHSTy << RHSTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 6045 | // In this situation, we assume void* type. No especially good |
| 6046 | // reason, but this is what gcc does, and we do have to pick |
| 6047 | // to get a consistent AST. |
| 6048 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6049 | LHS = ImpCastExprToType(LHS.take(), incompatTy, CK_BitCast); |
| 6050 | RHS = ImpCastExprToType(RHS.take(), incompatTy, CK_BitCast); |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 6051 | return incompatTy; |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 6052 | } |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6053 | // The block pointer types are compatible. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6054 | LHS = ImpCastExprToType(LHS.take(), LHSTy, CK_BitCast); |
| 6055 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Steve Naroff | ea4c780 | 2009-04-08 17:05:15 +0000 | [diff] [blame] | 6056 | return LHSTy; |
| 6057 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6058 | |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6059 | // Check constraints for C object pointers types (C99 6.5.15p3,6). |
| 6060 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
| 6061 | // get the "pointed to" types |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6062 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 6063 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6064 | |
| 6065 | // ignore qualifiers on void (C99 6.5.15p3, clause 6) |
| 6066 | if (lhptee->isVoidType() && rhptee->isIncompleteOrObjectType()) { |
| 6067 | // Figure out necessary qualifiers (C99 6.5.15p6) |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6068 | QualType destPointee |
| 6069 | = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6070 | QualType destType = Context.getPointerType(destPointee); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 6071 | // Add qualifiers if necessary. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6072 | LHS = ImpCastExprToType(LHS.take(), destType, CK_NoOp); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 6073 | // Promote to void*. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6074 | RHS = ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6075 | return destType; |
| 6076 | } |
| 6077 | if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) { |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 6078 | QualType destPointee |
| 6079 | = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6080 | QualType destType = Context.getPointerType(destPointee); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 6081 | // Add qualifiers if necessary. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6082 | RHS = ImpCastExprToType(RHS.take(), destType, CK_NoOp); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 6083 | // Promote to void*. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6084 | LHS = ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6085 | return destType; |
| 6086 | } |
| 6087 | |
| 6088 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 6089 | // Two identical pointer types are always compatible. |
| 6090 | return LHSTy; |
| 6091 | } |
| 6092 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 6093 | rhptee.getUnqualifiedType())) { |
| 6094 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6095 | << LHSTy << RHSTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6096 | // In this situation, we assume void* type. No especially good |
| 6097 | // reason, but this is what gcc does, and we do have to pick |
| 6098 | // to get a consistent AST. |
| 6099 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6100 | LHS = ImpCastExprToType(LHS.take(), incompatTy, CK_BitCast); |
| 6101 | RHS = ImpCastExprToType(RHS.take(), incompatTy, CK_BitCast); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6102 | return incompatTy; |
| 6103 | } |
| 6104 | // The pointer types are compatible. |
| 6105 | // C99 6.5.15p6: If both operands are pointers to compatible types *or* to |
| 6106 | // differently qualified versions of compatible types, the result type is |
| 6107 | // a pointer to an appropriately qualified version of the *composite* |
| 6108 | // type. |
| 6109 | // FIXME: Need to calculate the composite type. |
| 6110 | // FIXME: Need to add qualifiers |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6111 | LHS = ImpCastExprToType(LHS.take(), LHSTy, CK_BitCast); |
| 6112 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6113 | return LHSTy; |
| 6114 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6115 | |
John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 6116 | // GCC compatibility: soften pointer/integer mismatch. Note that |
| 6117 | // null pointers have been filtered out by this point. |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6118 | if (RHSTy->isPointerType() && LHSTy->isIntegerType()) { |
| 6119 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6120 | << LHSTy << RHSTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
| 6121 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_IntegralToPointer); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6122 | return RHSTy; |
| 6123 | } |
| 6124 | if (LHSTy->isPointerType() && RHSTy->isIntegerType()) { |
| 6125 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6126 | << LHSTy << RHSTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
| 6127 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_IntegralToPointer); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 6128 | return LHSTy; |
| 6129 | } |
Daniel Dunbar | 484603b | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 6130 | |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 6131 | // Emit a better diagnostic if one of the expressions is a null pointer |
| 6132 | // constant and the other is not a pointer type. In this case, the user most |
| 6133 | // likely forgot to take the address of the other expression. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6134 | if (DiagnoseConditionalForNull(LHS.get(), RHS.get(), QuestionLoc)) |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 6135 | return QualType(); |
| 6136 | |
Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 6137 | // Otherwise, the operands are not compatible. |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 6138 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6139 | << LHSTy << RHSTy << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 6140 | return QualType(); |
Steve Naroff | f8a28c5 | 2007-05-15 20:29:32 +0000 | [diff] [blame] | 6141 | } |
| 6142 | |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6143 | /// FindCompositeObjCPointerType - Helper method to find composite type of |
| 6144 | /// two objective-c pointer types of the two input expressions. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6145 | QualType Sema::FindCompositeObjCPointerType(ExprResult &LHS, ExprResult &RHS, |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6146 | SourceLocation QuestionLoc) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6147 | QualType LHSTy = LHS.get()->getType(); |
| 6148 | QualType RHSTy = RHS.get()->getType(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6149 | |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6150 | // Handle things like Class and struct objc_class*. Here we case the result |
| 6151 | // to the pseudo-builtin, because that will be implicitly cast back to the |
| 6152 | // redefinition type if an attempt is made to access its fields. |
| 6153 | if (LHSTy->isObjCClassType() && |
John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 6154 | (Context.hasSameType(RHSTy, Context.ObjCClassRedefinitionType))) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6155 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6156 | return LHSTy; |
| 6157 | } |
| 6158 | if (RHSTy->isObjCClassType() && |
John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 6159 | (Context.hasSameType(LHSTy, Context.ObjCClassRedefinitionType))) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6160 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6161 | return RHSTy; |
| 6162 | } |
| 6163 | // And the same for struct objc_object* / id |
| 6164 | if (LHSTy->isObjCIdType() && |
John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 6165 | (Context.hasSameType(RHSTy, Context.ObjCIdRedefinitionType))) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6166 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6167 | return LHSTy; |
| 6168 | } |
| 6169 | if (RHSTy->isObjCIdType() && |
John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 6170 | (Context.hasSameType(LHSTy, Context.ObjCIdRedefinitionType))) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6171 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6172 | return RHSTy; |
| 6173 | } |
| 6174 | // And the same for struct objc_selector* / SEL |
| 6175 | if (Context.isObjCSelType(LHSTy) && |
John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 6176 | (Context.hasSameType(RHSTy, Context.ObjCSelRedefinitionType))) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6177 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6178 | return LHSTy; |
| 6179 | } |
| 6180 | if (Context.isObjCSelType(RHSTy) && |
John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 6181 | (Context.hasSameType(LHSTy, Context.ObjCSelRedefinitionType))) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6182 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6183 | return RHSTy; |
| 6184 | } |
| 6185 | // Check constraints for Objective-C object pointers types. |
| 6186 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isObjCObjectPointerType()) { |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6187 | |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6188 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 6189 | // Two identical object pointer types are always compatible. |
| 6190 | return LHSTy; |
| 6191 | } |
| 6192 | const ObjCObjectPointerType *LHSOPT = LHSTy->getAs<ObjCObjectPointerType>(); |
| 6193 | const ObjCObjectPointerType *RHSOPT = RHSTy->getAs<ObjCObjectPointerType>(); |
| 6194 | QualType compositeType = LHSTy; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6195 | |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6196 | // If both operands are interfaces and either operand can be |
| 6197 | // assigned to the other, use that type as the composite |
| 6198 | // type. This allows |
| 6199 | // xxx ? (A*) a : (B*) b |
| 6200 | // where B is a subclass of A. |
| 6201 | // |
| 6202 | // Additionally, as for assignment, if either type is 'id' |
| 6203 | // allow silent coercion. Finally, if the types are |
| 6204 | // incompatible then make sure to use 'id' as the composite |
| 6205 | // type so the result is acceptable for sending messages to. |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6206 | |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6207 | // FIXME: Consider unifying with 'areComparableObjCPointerTypes'. |
| 6208 | // It could return the composite type. |
| 6209 | if (Context.canAssignObjCInterfaces(LHSOPT, RHSOPT)) { |
| 6210 | compositeType = RHSOPT->isObjCBuiltinType() ? RHSTy : LHSTy; |
| 6211 | } else if (Context.canAssignObjCInterfaces(RHSOPT, LHSOPT)) { |
| 6212 | compositeType = LHSOPT->isObjCBuiltinType() ? LHSTy : RHSTy; |
| 6213 | } else if ((LHSTy->isObjCQualifiedIdType() || |
| 6214 | RHSTy->isObjCQualifiedIdType()) && |
| 6215 | Context.ObjCQualifiedIdTypesAreCompatible(LHSTy, RHSTy, true)) { |
| 6216 | // Need to handle "id<xx>" explicitly. |
| 6217 | // GCC allows qualified id and any Objective-C type to devolve to |
| 6218 | // id. Currently localizing to here until clear this should be |
| 6219 | // part of ObjCQualifiedIdTypesAreCompatible. |
| 6220 | compositeType = Context.getObjCIdType(); |
| 6221 | } else if (LHSTy->isObjCIdType() || RHSTy->isObjCIdType()) { |
| 6222 | compositeType = Context.getObjCIdType(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6223 | } else if (!(compositeType = |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6224 | Context.areCommonBaseCompatible(LHSOPT, RHSOPT)).isNull()) |
| 6225 | ; |
| 6226 | else { |
| 6227 | Diag(QuestionLoc, diag::ext_typecheck_cond_incompatible_operands) |
| 6228 | << LHSTy << RHSTy |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6229 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6230 | QualType incompatTy = Context.getObjCIdType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6231 | LHS = ImpCastExprToType(LHS.take(), incompatTy, CK_BitCast); |
| 6232 | RHS = ImpCastExprToType(RHS.take(), incompatTy, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6233 | return incompatTy; |
| 6234 | } |
| 6235 | // The object pointer types are compatible. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6236 | LHS = ImpCastExprToType(LHS.take(), compositeType, CK_BitCast); |
| 6237 | RHS = ImpCastExprToType(RHS.take(), compositeType, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6238 | return compositeType; |
| 6239 | } |
| 6240 | // Check Objective-C object pointer types and 'void *' |
| 6241 | if (LHSTy->isVoidPointerType() && RHSTy->isObjCObjectPointerType()) { |
| 6242 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 6243 | QualType rhptee = RHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 6244 | QualType destPointee |
| 6245 | = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
| 6246 | QualType destType = Context.getPointerType(destPointee); |
| 6247 | // Add qualifiers if necessary. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6248 | LHS = ImpCastExprToType(LHS.take(), destType, CK_NoOp); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6249 | // Promote to void*. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6250 | RHS = ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6251 | return destType; |
| 6252 | } |
| 6253 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isVoidPointerType()) { |
| 6254 | QualType lhptee = LHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 6255 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
| 6256 | QualType destPointee |
| 6257 | = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
| 6258 | QualType destType = Context.getPointerType(destPointee); |
| 6259 | // Add qualifiers if necessary. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6260 | RHS = ImpCastExprToType(RHS.take(), destType, CK_NoOp); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6261 | // Promote to void*. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6262 | LHS = ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 6263 | return destType; |
| 6264 | } |
| 6265 | return QualType(); |
| 6266 | } |
| 6267 | |
Chandler Carruth | b00e8c0 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 6268 | /// SuggestParentheses - Emit a note with a fixit hint that wraps |
Hans Wennborg | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 6269 | /// ParenRange in parentheses. |
| 6270 | static void SuggestParentheses(Sema &Self, SourceLocation Loc, |
Chandler Carruth | b00e8c0 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 6271 | const PartialDiagnostic &Note, |
| 6272 | SourceRange ParenRange) { |
| 6273 | SourceLocation EndLoc = Self.PP.getLocForEndOfToken(ParenRange.getEnd()); |
| 6274 | if (ParenRange.getBegin().isFileID() && ParenRange.getEnd().isFileID() && |
| 6275 | EndLoc.isValid()) { |
| 6276 | Self.Diag(Loc, Note) |
| 6277 | << FixItHint::CreateInsertion(ParenRange.getBegin(), "(") |
| 6278 | << FixItHint::CreateInsertion(EndLoc, ")"); |
| 6279 | } else { |
| 6280 | // We can't display the parentheses, so just show the bare note. |
| 6281 | Self.Diag(Loc, Note) << ParenRange; |
Hans Wennborg | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 6282 | } |
Hans Wennborg | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 6283 | } |
| 6284 | |
| 6285 | static bool IsArithmeticOp(BinaryOperatorKind Opc) { |
| 6286 | return Opc >= BO_Mul && Opc <= BO_Shr; |
| 6287 | } |
| 6288 | |
Hans Wennborg | de2e67e | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 6289 | /// IsArithmeticBinaryExpr - Returns true if E is an arithmetic binary |
| 6290 | /// expression, either using a built-in or overloaded operator, |
| 6291 | /// and sets *OpCode to the opcode and *RHS to the right-hand side expression. |
| 6292 | static bool IsArithmeticBinaryExpr(Expr *E, BinaryOperatorKind *Opcode, |
| 6293 | Expr **RHS) { |
| 6294 | E = E->IgnoreParenImpCasts(); |
| 6295 | E = E->IgnoreConversionOperator(); |
| 6296 | E = E->IgnoreParenImpCasts(); |
| 6297 | |
| 6298 | // Built-in binary operator. |
| 6299 | if (BinaryOperator *OP = dyn_cast<BinaryOperator>(E)) { |
| 6300 | if (IsArithmeticOp(OP->getOpcode())) { |
| 6301 | *Opcode = OP->getOpcode(); |
| 6302 | *RHS = OP->getRHS(); |
| 6303 | return true; |
| 6304 | } |
| 6305 | } |
| 6306 | |
| 6307 | // Overloaded operator. |
| 6308 | if (CXXOperatorCallExpr *Call = dyn_cast<CXXOperatorCallExpr>(E)) { |
| 6309 | if (Call->getNumArgs() != 2) |
| 6310 | return false; |
| 6311 | |
| 6312 | // Make sure this is really a binary operator that is safe to pass into |
| 6313 | // BinaryOperator::getOverloadedOpcode(), e.g. it's not a subscript op. |
| 6314 | OverloadedOperatorKind OO = Call->getOperator(); |
| 6315 | if (OO < OO_Plus || OO > OO_Arrow) |
| 6316 | return false; |
| 6317 | |
| 6318 | BinaryOperatorKind OpKind = BinaryOperator::getOverloadedOpcode(OO); |
| 6319 | if (IsArithmeticOp(OpKind)) { |
| 6320 | *Opcode = OpKind; |
| 6321 | *RHS = Call->getArg(1); |
| 6322 | return true; |
| 6323 | } |
| 6324 | } |
| 6325 | |
| 6326 | return false; |
| 6327 | } |
| 6328 | |
Hans Wennborg | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 6329 | static bool IsLogicOp(BinaryOperatorKind Opc) { |
| 6330 | return (Opc >= BO_LT && Opc <= BO_NE) || (Opc >= BO_LAnd && Opc <= BO_LOr); |
| 6331 | } |
| 6332 | |
Hans Wennborg | de2e67e | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 6333 | /// ExprLooksBoolean - Returns true if E looks boolean, i.e. it has boolean type |
| 6334 | /// or is a logical expression such as (x==y) which has int type, but is |
| 6335 | /// commonly interpreted as boolean. |
| 6336 | static bool ExprLooksBoolean(Expr *E) { |
| 6337 | E = E->IgnoreParenImpCasts(); |
| 6338 | |
| 6339 | if (E->getType()->isBooleanType()) |
| 6340 | return true; |
| 6341 | if (BinaryOperator *OP = dyn_cast<BinaryOperator>(E)) |
| 6342 | return IsLogicOp(OP->getOpcode()); |
| 6343 | if (UnaryOperator *OP = dyn_cast<UnaryOperator>(E)) |
| 6344 | return OP->getOpcode() == UO_LNot; |
| 6345 | |
| 6346 | return false; |
| 6347 | } |
| 6348 | |
Hans Wennborg | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 6349 | /// DiagnoseConditionalPrecedence - Emit a warning when a conditional operator |
| 6350 | /// and binary operator are mixed in a way that suggests the programmer assumed |
| 6351 | /// the conditional operator has higher precedence, for example: |
| 6352 | /// "int x = a + someBinaryCondition ? 1 : 2". |
| 6353 | static void DiagnoseConditionalPrecedence(Sema &Self, |
| 6354 | SourceLocation OpLoc, |
Chandler Carruth | 08dc2ba | 2011-06-16 01:05:08 +0000 | [diff] [blame] | 6355 | Expr *Condition, |
| 6356 | Expr *LHS, |
| 6357 | Expr *RHS) { |
Hans Wennborg | de2e67e | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 6358 | BinaryOperatorKind CondOpcode; |
| 6359 | Expr *CondRHS; |
Hans Wennborg | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 6360 | |
Chandler Carruth | 08dc2ba | 2011-06-16 01:05:08 +0000 | [diff] [blame] | 6361 | if (!IsArithmeticBinaryExpr(Condition, &CondOpcode, &CondRHS)) |
Hans Wennborg | de2e67e | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 6362 | return; |
| 6363 | if (!ExprLooksBoolean(CondRHS)) |
| 6364 | return; |
Hans Wennborg | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 6365 | |
Hans Wennborg | de2e67e | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 6366 | // The condition is an arithmetic binary expression, with a right- |
| 6367 | // hand side that looks boolean, so warn. |
Hans Wennborg | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 6368 | |
Chandler Carruth | b00e8c0 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 6369 | Self.Diag(OpLoc, diag::warn_precedence_conditional) |
Chandler Carruth | 08dc2ba | 2011-06-16 01:05:08 +0000 | [diff] [blame] | 6370 | << Condition->getSourceRange() |
Hans Wennborg | de2e67e | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 6371 | << BinaryOperator::getOpcodeStr(CondOpcode); |
Hans Wennborg | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 6372 | |
Chandler Carruth | b00e8c0 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 6373 | SuggestParentheses(Self, OpLoc, |
| 6374 | Self.PDiag(diag::note_precedence_conditional_silence) |
| 6375 | << BinaryOperator::getOpcodeStr(CondOpcode), |
| 6376 | SourceRange(Condition->getLocStart(), Condition->getLocEnd())); |
Hans Wennborg | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 6377 | |
Chandler Carruth | b00e8c0 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 6378 | SuggestParentheses(Self, OpLoc, |
| 6379 | Self.PDiag(diag::note_precedence_conditional_first), |
| 6380 | SourceRange(CondRHS->getLocStart(), RHS->getLocEnd())); |
Hans Wennborg | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 6381 | } |
| 6382 | |
Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 6383 | /// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 6384 | /// in the case of a the GNU conditional expr extension. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 6385 | ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 6386 | SourceLocation ColonLoc, |
| 6387 | Expr *CondExpr, Expr *LHSExpr, |
| 6388 | Expr *RHSExpr) { |
Chris Lattner | 2ab40a6 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 6389 | // If this is the gnu "x ?: y" extension, analyze the types as though the LHS |
| 6390 | // was the condition. |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 6391 | OpaqueValueExpr *opaqueValue = 0; |
| 6392 | Expr *commonExpr = 0; |
| 6393 | if (LHSExpr == 0) { |
| 6394 | commonExpr = CondExpr; |
| 6395 | |
| 6396 | // We usually want to apply unary conversions *before* saving, except |
| 6397 | // in the special case of a C++ l-value conditional. |
| 6398 | if (!(getLangOptions().CPlusPlus |
| 6399 | && !commonExpr->isTypeDependent() |
| 6400 | && commonExpr->getValueKind() == RHSExpr->getValueKind() |
| 6401 | && commonExpr->isGLValue() |
| 6402 | && commonExpr->isOrdinaryOrBitFieldObject() |
| 6403 | && RHSExpr->isOrdinaryOrBitFieldObject() |
| 6404 | && Context.hasSameType(commonExpr->getType(), RHSExpr->getType()))) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6405 | ExprResult commonRes = UsualUnaryConversions(commonExpr); |
| 6406 | if (commonRes.isInvalid()) |
| 6407 | return ExprError(); |
| 6408 | commonExpr = commonRes.take(); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 6409 | } |
| 6410 | |
| 6411 | opaqueValue = new (Context) OpaqueValueExpr(commonExpr->getExprLoc(), |
| 6412 | commonExpr->getType(), |
| 6413 | commonExpr->getValueKind(), |
| 6414 | commonExpr->getObjectKind()); |
| 6415 | LHSExpr = CondExpr = opaqueValue; |
Fariborz Jahanian | c6bf0bd | 2010-08-31 18:02:20 +0000 | [diff] [blame] | 6416 | } |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 6417 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6418 | ExprValueKind VK = VK_RValue; |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 6419 | ExprObjectKind OK = OK_Ordinary; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6420 | ExprResult Cond = Owned(CondExpr), LHS = Owned(LHSExpr), RHS = Owned(RHSExpr); |
| 6421 | QualType result = CheckConditionalOperands(Cond, LHS, RHS, |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 6422 | VK, OK, QuestionLoc); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6423 | if (result.isNull() || Cond.isInvalid() || LHS.isInvalid() || |
| 6424 | RHS.isInvalid()) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 6425 | return ExprError(); |
| 6426 | |
Hans Wennborg | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 6427 | DiagnoseConditionalPrecedence(*this, QuestionLoc, Cond.get(), LHS.get(), |
| 6428 | RHS.get()); |
| 6429 | |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 6430 | if (!commonExpr) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6431 | return Owned(new (Context) ConditionalOperator(Cond.take(), QuestionLoc, |
| 6432 | LHS.take(), ColonLoc, |
| 6433 | RHS.take(), result, VK, OK)); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 6434 | |
| 6435 | return Owned(new (Context) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6436 | BinaryConditionalOperator(commonExpr, opaqueValue, Cond.take(), LHS.take(), |
| 6437 | RHS.take(), QuestionLoc, ColonLoc, result, VK, OK)); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 6438 | } |
| 6439 | |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6440 | // checkPointerTypesForAssignment - This is a very tricky routine (despite |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6441 | // being closely modeled after the C99 spec:-). The odd characteristic of this |
Steve Naroff | 3f59729 | 2007-05-11 22:18:03 +0000 | [diff] [blame] | 6442 | // routine is it effectively iqnores the qualifiers on the top level pointee. |
| 6443 | // This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3]. |
| 6444 | // FIXME: add a couple examples in this comment. |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6445 | static Sema::AssignConvertType |
| 6446 | checkPointerTypesForAssignment(Sema &S, QualType lhsType, QualType rhsType) { |
| 6447 | assert(lhsType.isCanonical() && "LHS not canonicalized!"); |
| 6448 | assert(rhsType.isCanonical() && "RHS not canonicalized!"); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6449 | |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 6450 | // get the "pointed to" type (ignoring qualifiers at the top level) |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6451 | const Type *lhptee, *rhptee; |
| 6452 | Qualifiers lhq, rhq; |
| 6453 | llvm::tie(lhptee, lhq) = cast<PointerType>(lhsType)->getPointeeType().split(); |
| 6454 | llvm::tie(rhptee, rhq) = cast<PointerType>(rhsType)->getPointeeType().split(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6455 | |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6456 | Sema::AssignConvertType ConvTy = Sema::Compatible; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6457 | |
| 6458 | // C99 6.5.16.1p1: This following citation is common to constraints |
| 6459 | // 3 & 4 (below). ...and the type *pointed to* by the left has all the |
| 6460 | // qualifiers of the type *pointed to* by the right; |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6461 | Qualifiers lq; |
| 6462 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6463 | // As a special case, 'non-__weak A *' -> 'non-__weak const *' is okay. |
| 6464 | if (lhq.getObjCLifetime() != rhq.getObjCLifetime() && |
| 6465 | lhq.compatiblyIncludesObjCLifetime(rhq)) { |
| 6466 | // Ignore lifetime for further calculation. |
| 6467 | lhq.removeObjCLifetime(); |
| 6468 | rhq.removeObjCLifetime(); |
| 6469 | } |
| 6470 | |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6471 | if (!lhq.compatiblyIncludes(rhq)) { |
| 6472 | // Treat address-space mismatches as fatal. TODO: address subspaces |
| 6473 | if (lhq.getAddressSpace() != rhq.getAddressSpace()) |
| 6474 | ConvTy = Sema::IncompatiblePointerDiscardsQualifiers; |
| 6475 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6476 | // It's okay to add or remove GC or lifetime qualifiers when converting to |
John McCall | 7853595 | 2011-03-26 02:56:45 +0000 | [diff] [blame] | 6477 | // and from void*. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6478 | else if (lhq.withoutObjCGCAttr().withoutObjCGLifetime() |
| 6479 | .compatiblyIncludes( |
| 6480 | rhq.withoutObjCGCAttr().withoutObjCGLifetime()) |
John McCall | 7853595 | 2011-03-26 02:56:45 +0000 | [diff] [blame] | 6481 | && (lhptee->isVoidType() || rhptee->isVoidType())) |
| 6482 | ; // keep old |
| 6483 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6484 | // Treat lifetime mismatches as fatal. |
| 6485 | else if (lhq.getObjCLifetime() != rhq.getObjCLifetime()) |
| 6486 | ConvTy = Sema::IncompatiblePointerDiscardsQualifiers; |
| 6487 | |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6488 | // For GCC compatibility, other qualifier mismatches are treated |
| 6489 | // as still compatible in C. |
| 6490 | else ConvTy = Sema::CompatiblePointerDiscardsQualifiers; |
| 6491 | } |
Steve Naroff | 3f59729 | 2007-05-11 22:18:03 +0000 | [diff] [blame] | 6492 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6493 | // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or |
| 6494 | // 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] | 6495 | // version of void... |
Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 6496 | if (lhptee->isVoidType()) { |
Chris Lattner | b3a176d | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 6497 | if (rhptee->isIncompleteOrObjectType()) |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6498 | return ConvTy; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6499 | |
Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 6500 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | b3a176d | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 6501 | assert(rhptee->isFunctionType()); |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6502 | return Sema::FunctionVoidPointer; |
Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 6503 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6504 | |
Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 6505 | if (rhptee->isVoidType()) { |
Chris Lattner | b3a176d | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 6506 | if (lhptee->isIncompleteOrObjectType()) |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6507 | return ConvTy; |
Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 6508 | |
| 6509 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | b3a176d | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 6510 | assert(lhptee->isFunctionType()); |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6511 | return Sema::FunctionVoidPointer; |
Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 6512 | } |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6513 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6514 | // 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] | 6515 | // unqualified versions of compatible types, ... |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6516 | QualType ltrans = QualType(lhptee, 0), rtrans = QualType(rhptee, 0); |
| 6517 | if (!S.Context.typesAreCompatible(ltrans, rtrans)) { |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 6518 | // Check if the pointee types are compatible ignoring the sign. |
| 6519 | // We explicitly check for char so that we catch "char" vs |
| 6520 | // "unsigned char" on systems where "char" is unsigned. |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 6521 | if (lhptee->isCharType()) |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6522 | ltrans = S.Context.UnsignedCharTy; |
Douglas Gregor | 5cc2c8b | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 6523 | else if (lhptee->hasSignedIntegerRepresentation()) |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6524 | ltrans = S.Context.getCorrespondingUnsignedType(ltrans); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6525 | |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 6526 | if (rhptee->isCharType()) |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6527 | rtrans = S.Context.UnsignedCharTy; |
Douglas Gregor | 5cc2c8b | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 6528 | else if (rhptee->hasSignedIntegerRepresentation()) |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6529 | rtrans = S.Context.getCorrespondingUnsignedType(rtrans); |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 6530 | |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6531 | if (ltrans == rtrans) { |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 6532 | // Types are compatible ignoring the sign. Qualifier incompatibility |
| 6533 | // takes priority over sign incompatibility because the sign |
| 6534 | // warning can be disabled. |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6535 | if (ConvTy != Sema::Compatible) |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 6536 | return ConvTy; |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6537 | |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6538 | return Sema::IncompatiblePointerSign; |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 6539 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6540 | |
Fariborz Jahanian | d7aa9d8 | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 6541 | // If we are a multi-level pointer, it's possible that our issue is simply |
| 6542 | // one of qualification - e.g. char ** -> const char ** is not allowed. If |
| 6543 | // the eventual target type is the same and the pointers have the same |
| 6544 | // level of indirection, this must be the issue. |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6545 | if (isa<PointerType>(lhptee) && isa<PointerType>(rhptee)) { |
Fariborz Jahanian | d7aa9d8 | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 6546 | do { |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6547 | lhptee = cast<PointerType>(lhptee)->getPointeeType().getTypePtr(); |
| 6548 | rhptee = cast<PointerType>(rhptee)->getPointeeType().getTypePtr(); |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6549 | } while (isa<PointerType>(lhptee) && isa<PointerType>(rhptee)); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6550 | |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 6551 | if (lhptee == rhptee) |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6552 | return Sema::IncompatibleNestedPointerQualifiers; |
Fariborz Jahanian | d7aa9d8 | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 6553 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6554 | |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 6555 | // General pointer incompatibility takes priority over qualifiers. |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6556 | return Sema::IncompatiblePointer; |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 6557 | } |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6558 | return ConvTy; |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 6559 | } |
| 6560 | |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6561 | /// checkBlockPointerTypesForAssignment - This routine determines whether two |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6562 | /// block pointer types are compatible or whether a block and normal pointer |
| 6563 | /// are compatible. It is more restrict than comparing two function pointer |
| 6564 | // types. |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6565 | static Sema::AssignConvertType |
| 6566 | checkBlockPointerTypesForAssignment(Sema &S, QualType lhsType, |
| 6567 | QualType rhsType) { |
| 6568 | assert(lhsType.isCanonical() && "LHS not canonicalized!"); |
| 6569 | assert(rhsType.isCanonical() && "RHS not canonicalized!"); |
| 6570 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6571 | QualType lhptee, rhptee; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6572 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6573 | // get the "pointed to" type (ignoring qualifiers at the top level) |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6574 | lhptee = cast<BlockPointerType>(lhsType)->getPointeeType(); |
| 6575 | rhptee = cast<BlockPointerType>(rhsType)->getPointeeType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6576 | |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6577 | // In C++, the types have to match exactly. |
| 6578 | if (S.getLangOptions().CPlusPlus) |
| 6579 | return Sema::IncompatibleBlockPointer; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6580 | |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6581 | Sema::AssignConvertType ConvTy = Sema::Compatible; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6582 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6583 | // For blocks we enforce that qualifiers are identical. |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6584 | if (lhptee.getLocalQualifiers() != rhptee.getLocalQualifiers()) |
| 6585 | ConvTy = Sema::CompatiblePointerDiscardsQualifiers; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6586 | |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6587 | if (!S.Context.typesAreBlockPointerCompatible(lhsType, rhsType)) |
| 6588 | return Sema::IncompatibleBlockPointer; |
| 6589 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6590 | return ConvTy; |
| 6591 | } |
| 6592 | |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6593 | /// checkObjCPointerTypesForAssignment - Compares two objective-c pointer types |
Fariborz Jahanian | 410f2eb | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 6594 | /// for assignment compatibility. |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6595 | static Sema::AssignConvertType |
| 6596 | checkObjCPointerTypesForAssignment(Sema &S, QualType lhsType, QualType rhsType) { |
| 6597 | assert(lhsType.isCanonical() && "LHS was not canonicalized!"); |
| 6598 | assert(rhsType.isCanonical() && "RHS was not canonicalized!"); |
| 6599 | |
Fariborz Jahanian | d5bb8cb | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 6600 | if (lhsType->isObjCBuiltinType()) { |
| 6601 | // Class is not compatible with ObjC object pointers. |
Fariborz Jahanian | 9b37b1d | 2010-03-24 21:00:27 +0000 | [diff] [blame] | 6602 | if (lhsType->isObjCClassType() && !rhsType->isObjCBuiltinType() && |
| 6603 | !rhsType->isObjCQualifiedClassType()) |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6604 | return Sema::IncompatiblePointer; |
| 6605 | return Sema::Compatible; |
Fariborz Jahanian | d5bb8cb | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 6606 | } |
| 6607 | if (rhsType->isObjCBuiltinType()) { |
| 6608 | // Class is not compatible with ObjC object pointers. |
Fariborz Jahanian | 9b37b1d | 2010-03-24 21:00:27 +0000 | [diff] [blame] | 6609 | if (rhsType->isObjCClassType() && !lhsType->isObjCBuiltinType() && |
| 6610 | !lhsType->isObjCQualifiedClassType()) |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6611 | return Sema::IncompatiblePointer; |
| 6612 | return Sema::Compatible; |
Fariborz Jahanian | d5bb8cb | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 6613 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6614 | QualType lhptee = |
Fariborz Jahanian | 410f2eb | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 6615 | lhsType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6616 | QualType rhptee = |
Fariborz Jahanian | 410f2eb | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 6617 | rhsType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6618 | |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6619 | if (!lhptee.isAtLeastAsQualifiedAs(rhptee)) |
| 6620 | return Sema::CompatiblePointerDiscardsQualifiers; |
| 6621 | |
| 6622 | if (S.Context.typesAreCompatible(lhsType, rhsType)) |
| 6623 | return Sema::Compatible; |
Fariborz Jahanian | 410f2eb | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 6624 | if (lhsType->isObjCQualifiedIdType() || rhsType->isObjCQualifiedIdType()) |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6625 | return Sema::IncompatibleObjCQualifiedId; |
| 6626 | return Sema::IncompatiblePointer; |
Fariborz Jahanian | 410f2eb | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 6627 | } |
| 6628 | |
John McCall | 29600e1 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6629 | Sema::AssignConvertType |
Douglas Gregor | c03a108 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 6630 | Sema::CheckAssignmentConstraints(SourceLocation Loc, |
| 6631 | QualType lhsType, QualType rhsType) { |
John McCall | 29600e1 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6632 | // Fake up an opaque expression. We don't actually care about what |
| 6633 | // cast operations are required, so if CheckAssignmentConstraints |
| 6634 | // adds casts to this they'll be wasted, but fortunately that doesn't |
| 6635 | // usually happen on valid code. |
Douglas Gregor | c03a108 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 6636 | OpaqueValueExpr rhs(Loc, rhsType, VK_RValue); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6637 | ExprResult rhsPtr = &rhs; |
John McCall | 29600e1 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6638 | CastKind K = CK_Invalid; |
| 6639 | |
| 6640 | return CheckAssignmentConstraints(lhsType, rhsPtr, K); |
| 6641 | } |
| 6642 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6643 | /// CheckAssignmentConstraints (C99 6.5.16) - This routine currently |
| 6644 | /// has code to accommodate several GCC extensions when type checking |
Steve Naroff | 17f76e0 | 2007-05-03 21:03:48 +0000 | [diff] [blame] | 6645 | /// pointers. Here are some objectionable examples that GCC considers warnings: |
| 6646 | /// |
| 6647 | /// int a, *pint; |
| 6648 | /// short *pshort; |
| 6649 | /// struct foo *pfoo; |
| 6650 | /// |
| 6651 | /// pint = pshort; // warning: assignment from incompatible pointer type |
| 6652 | /// a = pint; // warning: assignment makes integer from pointer without a cast |
| 6653 | /// pint = a; // warning: assignment makes pointer from integer without a cast |
| 6654 | /// pint = pfoo; // warning: assignment from incompatible pointer type |
| 6655 | /// |
| 6656 | /// 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] | 6657 | /// C99 spec dictates. |
Steve Naroff | 17f76e0 | 2007-05-03 21:03:48 +0000 | [diff] [blame] | 6658 | /// |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6659 | /// Sets 'Kind' for any result kind except Incompatible. |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6660 | Sema::AssignConvertType |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6661 | Sema::CheckAssignmentConstraints(QualType lhsType, ExprResult &rhs, |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6662 | CastKind &Kind) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6663 | QualType rhsType = rhs.get()->getType(); |
John McCall | 29600e1 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6664 | |
Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 6665 | // Get canonical types. We're not formatting these types, just comparing |
| 6666 | // them. |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 6667 | lhsType = Context.getCanonicalType(lhsType).getUnqualifiedType(); |
| 6668 | rhsType = Context.getCanonicalType(rhsType).getUnqualifiedType(); |
Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 6669 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6670 | // Common case: no conversion required. |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6671 | if (lhsType == rhsType) { |
| 6672 | Kind = CK_NoOp; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6673 | return Compatible; |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 6674 | } |
| 6675 | |
Douglas Gregor | 6b75484 | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 6676 | // If the left-hand side is a reference type, then we are in a |
| 6677 | // (rare!) case where we've allowed the use of references in C, |
| 6678 | // e.g., as a parameter type in a built-in function. In this case, |
| 6679 | // just make sure that the type referenced is compatible with the |
| 6680 | // right-hand side type. The caller is responsible for adjusting |
| 6681 | // lhsType so that the resulting expression does not have reference |
| 6682 | // type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6683 | if (const ReferenceType *lhsTypeRef = lhsType->getAs<ReferenceType>()) { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6684 | if (Context.typesAreCompatible(lhsTypeRef->getPointeeType(), rhsType)) { |
| 6685 | Kind = CK_LValueBitCast; |
Anders Carlsson | 24ebce6 | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 6686 | return Compatible; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6687 | } |
Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 6688 | return Incompatible; |
Fariborz Jahanian | a1e3420 | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 6689 | } |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6690 | |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6691 | // Allow scalar to ExtVector assignments, and assignments of an ExtVector type |
| 6692 | // to the same ExtVector type. |
| 6693 | if (lhsType->isExtVectorType()) { |
| 6694 | if (rhsType->isExtVectorType()) |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6695 | return Incompatible; |
| 6696 | if (rhsType->isArithmeticType()) { |
John McCall | 29600e1 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6697 | // CK_VectorSplat does T -> vector T, so first cast to the |
| 6698 | // element type. |
| 6699 | QualType elType = cast<ExtVectorType>(lhsType)->getElementType(); |
| 6700 | if (elType != rhsType) { |
| 6701 | Kind = PrepareScalarCast(*this, rhs, elType); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6702 | rhs = ImpCastExprToType(rhs.take(), elType, Kind); |
John McCall | 29600e1 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6703 | } |
| 6704 | Kind = CK_VectorSplat; |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6705 | return Compatible; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6706 | } |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6707 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6708 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6709 | // Conversions to or from vector type. |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6710 | if (lhsType->isVectorType() || rhsType->isVectorType()) { |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6711 | if (lhsType->isVectorType() && rhsType->isVectorType()) { |
Bob Wilson | 01856f3 | 2010-12-02 00:25:15 +0000 | [diff] [blame] | 6712 | // Allow assignments of an AltiVec vector type to an equivalent GCC |
| 6713 | // vector type and vice versa |
| 6714 | if (Context.areCompatibleVectorTypes(lhsType, rhsType)) { |
| 6715 | Kind = CK_BitCast; |
| 6716 | return Compatible; |
| 6717 | } |
| 6718 | |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6719 | // If we are allowing lax vector conversions, and LHS and RHS are both |
| 6720 | // vectors, the total size only needs to be the same. This is a bitcast; |
| 6721 | // no bits are changed but the result type is different. |
| 6722 | if (getLangOptions().LaxVectorConversions && |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6723 | (Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType))) { |
John McCall | 3065d04 | 2010-11-15 10:08:00 +0000 | [diff] [blame] | 6724 | Kind = CK_BitCast; |
Anders Carlsson | db5a9b6 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 6725 | return IncompatibleVectors; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6726 | } |
Chris Lattner | 881a212 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 6727 | } |
| 6728 | return Incompatible; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6729 | } |
Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 6730 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6731 | // Arithmetic conversions. |
Douglas Gregor | bea453a | 2010-05-23 21:53:47 +0000 | [diff] [blame] | 6732 | if (lhsType->isArithmeticType() && rhsType->isArithmeticType() && |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6733 | !(getLangOptions().CPlusPlus && lhsType->isEnumeralType())) { |
John McCall | 29600e1 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 6734 | Kind = PrepareScalarCast(*this, rhs, lhsType); |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 6735 | return Compatible; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6736 | } |
Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 6737 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6738 | // Conversions to normal pointers. |
| 6739 | if (const PointerType *lhsPointer = dyn_cast<PointerType>(lhsType)) { |
| 6740 | // U* -> T* |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6741 | if (isa<PointerType>(rhsType)) { |
| 6742 | Kind = CK_BitCast; |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6743 | return checkPointerTypesForAssignment(*this, lhsType, rhsType); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6744 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6745 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6746 | // int -> T* |
| 6747 | if (rhsType->isIntegerType()) { |
| 6748 | Kind = CK_IntegralToPointer; // FIXME: null? |
| 6749 | return IntToPointer; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6750 | } |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6751 | |
| 6752 | // C pointers are not compatible with ObjC object pointers, |
| 6753 | // with two exceptions: |
| 6754 | if (isa<ObjCObjectPointerType>(rhsType)) { |
| 6755 | // - conversions to void* |
| 6756 | if (lhsPointer->getPointeeType()->isVoidType()) { |
| 6757 | Kind = CK_AnyPointerToObjCPointerCast; |
| 6758 | return Compatible; |
| 6759 | } |
| 6760 | |
| 6761 | // - conversions from 'Class' to the redefinition type |
| 6762 | if (rhsType->isObjCClassType() && |
| 6763 | Context.hasSameType(lhsType, Context.ObjCClassRedefinitionType)) { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6764 | Kind = CK_BitCast; |
Douglas Gregor | e7dd145 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 6765 | return Compatible; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6766 | } |
Steve Naroff | 32d072c | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 6767 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6768 | Kind = CK_BitCast; |
| 6769 | return IncompatiblePointer; |
| 6770 | } |
| 6771 | |
| 6772 | // U^ -> void* |
| 6773 | if (rhsType->getAs<BlockPointerType>()) { |
| 6774 | if (lhsPointer->getPointeeType()->isVoidType()) { |
| 6775 | Kind = CK_BitCast; |
Steve Naroff | 32d072c | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 6776 | return Compatible; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6777 | } |
Steve Naroff | 32d072c | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 6778 | } |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6779 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6780 | return Incompatible; |
| 6781 | } |
| 6782 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6783 | // Conversions to block pointers. |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6784 | if (isa<BlockPointerType>(lhsType)) { |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6785 | // U^ -> T^ |
| 6786 | if (rhsType->isBlockPointerType()) { |
| 6787 | Kind = CK_AnyPointerToBlockPointerCast; |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6788 | return checkBlockPointerTypesForAssignment(*this, lhsType, rhsType); |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6789 | } |
| 6790 | |
| 6791 | // int or null -> T^ |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6792 | if (rhsType->isIntegerType()) { |
| 6793 | Kind = CK_IntegralToPointer; // FIXME: null |
Eli Friedman | 8163b7a | 2009-02-25 04:20:42 +0000 | [diff] [blame] | 6794 | return IntToBlockPointer; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6795 | } |
| 6796 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6797 | // id -> T^ |
| 6798 | if (getLangOptions().ObjC1 && rhsType->isObjCIdType()) { |
| 6799 | Kind = CK_AnyPointerToBlockPointerCast; |
Steve Naroff | 32d072c | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 6800 | return Compatible; |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6801 | } |
Steve Naroff | 32d072c | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 6802 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6803 | // void* -> T^ |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6804 | if (const PointerType *RHSPT = rhsType->getAs<PointerType>()) |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6805 | if (RHSPT->getPointeeType()->isVoidType()) { |
| 6806 | Kind = CK_AnyPointerToBlockPointerCast; |
Douglas Gregor | e7dd145 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 6807 | return Compatible; |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6808 | } |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6809 | |
Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 6810 | return Incompatible; |
| 6811 | } |
| 6812 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6813 | // Conversions to Objective-C pointers. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6814 | if (isa<ObjCObjectPointerType>(lhsType)) { |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6815 | // A* -> B* |
| 6816 | if (rhsType->isObjCObjectPointerType()) { |
| 6817 | Kind = CK_BitCast; |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 6818 | return checkObjCPointerTypesForAssignment(*this, lhsType, rhsType); |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6819 | } |
| 6820 | |
| 6821 | // int or null -> A* |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6822 | if (rhsType->isIntegerType()) { |
| 6823 | Kind = CK_IntegralToPointer; // FIXME: null |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6824 | return IntToPointer; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6825 | } |
| 6826 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6827 | // In general, C pointers are not compatible with ObjC object pointers, |
| 6828 | // with two exceptions: |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6829 | if (isa<PointerType>(rhsType)) { |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6830 | // - conversions from 'void*' |
| 6831 | if (rhsType->isVoidPointerType()) { |
| 6832 | Kind = CK_AnyPointerToObjCPointerCast; |
Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 6833 | return Compatible; |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6834 | } |
| 6835 | |
| 6836 | // - conversions to 'Class' from its redefinition type |
| 6837 | if (lhsType->isObjCClassType() && |
| 6838 | Context.hasSameType(rhsType, Context.ObjCClassRedefinitionType)) { |
| 6839 | Kind = CK_BitCast; |
| 6840 | return Compatible; |
| 6841 | } |
| 6842 | |
| 6843 | Kind = CK_AnyPointerToObjCPointerCast; |
Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 6844 | return IncompatiblePointer; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6845 | } |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6846 | |
| 6847 | // T^ -> A* |
| 6848 | if (rhsType->isBlockPointerType()) { |
| 6849 | Kind = CK_AnyPointerToObjCPointerCast; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6850 | return Compatible; |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6851 | } |
| 6852 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6853 | return Incompatible; |
| 6854 | } |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6855 | |
| 6856 | // Conversions from pointers that are not covered by the above. |
Chris Lattner | ec64683 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 6857 | if (isa<PointerType>(rhsType)) { |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6858 | // T* -> _Bool |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6859 | if (lhsType == Context.BoolTy) { |
| 6860 | Kind = CK_PointerToBoolean; |
Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 6861 | return Compatible; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6862 | } |
Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 6863 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6864 | // T* -> int |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6865 | if (lhsType->isIntegerType()) { |
| 6866 | Kind = CK_PointerToIntegral; |
Chris Lattner | 940cfeb | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 6867 | return PointerToInt; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6868 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6869 | |
Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 6870 | return Incompatible; |
Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 6871 | } |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6872 | |
| 6873 | // Conversions from Objective-C pointers that are not covered by the above. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6874 | if (isa<ObjCObjectPointerType>(rhsType)) { |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6875 | // T* -> _Bool |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6876 | if (lhsType == Context.BoolTy) { |
| 6877 | Kind = CK_PointerToBoolean; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6878 | return Compatible; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6879 | } |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6880 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6881 | // T* -> int |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6882 | if (lhsType->isIntegerType()) { |
| 6883 | Kind = CK_PointerToIntegral; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6884 | return PointerToInt; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6885 | } |
| 6886 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6887 | return Incompatible; |
| 6888 | } |
Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 6889 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6890 | // struct A -> struct B |
Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 6891 | if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6892 | if (Context.typesAreCompatible(lhsType, rhsType)) { |
| 6893 | Kind = CK_NoOp; |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 6894 | return Compatible; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6895 | } |
Bill Wendling | 216423b | 2007-05-30 06:30:29 +0000 | [diff] [blame] | 6896 | } |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 6897 | |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 6898 | return Incompatible; |
Steve Naroff | 9eb2465 | 2007-05-02 21:58:15 +0000 | [diff] [blame] | 6899 | } |
| 6900 | |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6901 | /// \brief Constructs a transparent union from an expression that is |
| 6902 | /// used to initialize the transparent union. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6903 | static void ConstructTransparentUnion(Sema &S, ASTContext &C, ExprResult &EResult, |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6904 | QualType UnionType, FieldDecl *Field) { |
| 6905 | // Build an initializer list that designates the appropriate member |
| 6906 | // of the transparent union. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6907 | Expr *E = EResult.take(); |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 6908 | InitListExpr *Initializer = new (C) InitListExpr(C, SourceLocation(), |
Ted Kremenek | 013041e | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 6909 | &E, 1, |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6910 | SourceLocation()); |
| 6911 | Initializer->setType(UnionType); |
| 6912 | Initializer->setInitializedFieldInUnion(Field); |
| 6913 | |
| 6914 | // Build a compound literal constructing a value of the transparent |
| 6915 | // union type from this initializer list. |
John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 6916 | TypeSourceInfo *unionTInfo = C.getTrivialTypeSourceInfo(UnionType); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6917 | EResult = S.Owned( |
| 6918 | new (C) CompoundLiteralExpr(SourceLocation(), unionTInfo, UnionType, |
| 6919 | VK_RValue, Initializer, false)); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6920 | } |
| 6921 | |
| 6922 | Sema::AssignConvertType |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6923 | Sema::CheckTransparentUnionArgumentConstraints(QualType ArgType, ExprResult &rExpr) { |
| 6924 | QualType FromType = rExpr.get()->getType(); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6925 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6926 | // 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] | 6927 | // transparent_union GCC extension. |
| 6928 | const RecordType *UT = ArgType->getAsUnionType(); |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 6929 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6930 | return Incompatible; |
| 6931 | |
| 6932 | // The field to initialize within the transparent union. |
| 6933 | RecordDecl *UD = UT->getDecl(); |
| 6934 | FieldDecl *InitField = 0; |
| 6935 | // It's compatible if the expression matches any of the fields. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 6936 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 6937 | itend = UD->field_end(); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6938 | it != itend; ++it) { |
| 6939 | if (it->getType()->isPointerType()) { |
| 6940 | // If the transparent union contains a pointer type, we allow: |
| 6941 | // 1) void pointer |
| 6942 | // 2) null pointer constant |
| 6943 | if (FromType->isPointerType()) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6944 | if (FromType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6945 | rExpr = ImpCastExprToType(rExpr.take(), it->getType(), CK_BitCast); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6946 | InitField = *it; |
| 6947 | break; |
| 6948 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6949 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6950 | if (rExpr.get()->isNullPointerConstant(Context, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6951 | Expr::NPC_ValueDependentIsNull)) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6952 | rExpr = ImpCastExprToType(rExpr.take(), it->getType(), CK_NullToPointer); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6953 | InitField = *it; |
| 6954 | break; |
| 6955 | } |
| 6956 | } |
| 6957 | |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6958 | CastKind Kind = CK_Invalid; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6959 | if (CheckAssignmentConstraints(it->getType(), rExpr, Kind) |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6960 | == Compatible) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6961 | rExpr = ImpCastExprToType(rExpr.take(), it->getType(), Kind); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6962 | InitField = *it; |
| 6963 | break; |
| 6964 | } |
| 6965 | } |
| 6966 | |
| 6967 | if (!InitField) |
| 6968 | return Incompatible; |
| 6969 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6970 | ConstructTransparentUnion(*this, Context, rExpr, ArgType, InitField); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 6971 | return Compatible; |
| 6972 | } |
| 6973 | |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6974 | Sema::AssignConvertType |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6975 | Sema::CheckSingleAssignmentConstraints(QualType lhsType, ExprResult &rExpr) { |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 6976 | if (getLangOptions().CPlusPlus) { |
| 6977 | if (!lhsType->isRecordType()) { |
| 6978 | // C++ 5.17p3: If the left operand is not of class type, the |
| 6979 | // expression is implicitly converted (C++ 4) to the |
| 6980 | // cv-unqualified type of the left operand. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6981 | ExprResult Res = PerformImplicitConversion(rExpr.get(), |
| 6982 | lhsType.getUnqualifiedType(), |
| 6983 | AA_Assigning); |
| 6984 | if (Res.isInvalid()) |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 6985 | return Incompatible; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6986 | rExpr = move(Res); |
Chris Lattner | 0d5640c | 2009-04-12 09:02:39 +0000 | [diff] [blame] | 6987 | return Compatible; |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 6988 | } |
| 6989 | |
| 6990 | // FIXME: Currently, we fall through and treat C++ classes like C |
| 6991 | // structures. |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 6992 | } |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 6993 | |
Steve Naroff | 0ee0b0a | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 6994 | // C99 6.5.16.1p1: the left operand is a pointer and the right is |
| 6995 | // a null pointer constant. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6996 | if ((lhsType->isPointerType() || |
| 6997 | lhsType->isObjCObjectPointerType() || |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6998 | lhsType->isBlockPointerType()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6999 | && rExpr.get()->isNullPointerConstant(Context, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 7000 | Expr::NPC_ValueDependentIsNull)) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7001 | rExpr = ImpCastExprToType(rExpr.take(), lhsType, CK_NullToPointer); |
Steve Naroff | 0ee0b0a | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 7002 | return Compatible; |
| 7003 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7004 | |
Chris Lattner | e6dcd50 | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 7005 | // This check seems unnatural, however it is necessary to ensure the proper |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 7006 | // conversion of functions/arrays. If the conversion were done for all |
Douglas Gregor | a121b75 | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 7007 | // DeclExpr's (created by ActOnIdExpression), it would mess up the unary |
Nick Lewycky | ef7c0ff | 2010-08-05 06:27:49 +0000 | [diff] [blame] | 7008 | // expressions that suppress this implicit conversion (&, sizeof). |
Chris Lattner | e6dcd50 | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 7009 | // |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7010 | // Suppress this for references: C++ 8.5.3p5. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7011 | if (!lhsType->isReferenceType()) { |
| 7012 | rExpr = DefaultFunctionArrayLvalueConversion(rExpr.take()); |
| 7013 | if (rExpr.isInvalid()) |
| 7014 | return Incompatible; |
| 7015 | } |
Steve Naroff | 0c1c7ed | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 7016 | |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 7017 | CastKind Kind = CK_Invalid; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7018 | Sema::AssignConvertType result = |
John McCall | 29600e1 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 7019 | CheckAssignmentConstraints(lhsType, rExpr, Kind); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7020 | |
Steve Naroff | 0c1c7ed | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 7021 | // C99 6.5.16.1p2: The value of the right operand is converted to the |
| 7022 | // type of the assignment expression. |
Douglas Gregor | 6b75484 | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 7023 | // CheckAssignmentConstraints allows the left-hand side to be a reference, |
| 7024 | // so that we can use references in built-in functions even in C. |
| 7025 | // The getNonReferenceType() call makes sure that the resulting expression |
| 7026 | // does not have reference type. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7027 | if (result != Incompatible && rExpr.get()->getType() != lhsType) |
| 7028 | rExpr = ImpCastExprToType(rExpr.take(), lhsType.getNonLValueExprType(Context), Kind); |
Steve Naroff | 0c1c7ed | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 7029 | return result; |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 7030 | } |
| 7031 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7032 | QualType Sema::InvalidOperands(SourceLocation Loc, ExprResult &lex, ExprResult &rex) { |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 7033 | Diag(Loc, diag::err_typecheck_invalid_operands) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7034 | << lex.get()->getType() << rex.get()->getType() |
| 7035 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Chris Lattner | 5c11c41 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 7036 | return QualType(); |
Steve Naroff | 6f49f5d | 2007-05-29 14:23:36 +0000 | [diff] [blame] | 7037 | } |
| 7038 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7039 | QualType Sema::CheckVectorOperands(SourceLocation Loc, ExprResult &lex, ExprResult &rex) { |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7040 | // For conversion purposes, we ignore any qualifiers. |
Nate Begeman | 002e4bd | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 7041 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 7042 | QualType lhsType = |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7043 | Context.getCanonicalType(lex.get()->getType()).getUnqualifiedType(); |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 7044 | QualType rhsType = |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7045 | Context.getCanonicalType(rex.get()->getType()).getUnqualifiedType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7046 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7047 | // If the vector types are identical, return. |
Nate Begeman | 002e4bd | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 7048 | if (lhsType == rhsType) |
Steve Naroff | 84ff4b4 | 2007-07-09 21:31:10 +0000 | [diff] [blame] | 7049 | return lhsType; |
Nate Begeman | 330aaa7 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 7050 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7051 | // Handle the case of a vector & extvector type of the same size and element |
| 7052 | // 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] | 7053 | if (getLangOptions().LaxVectorConversions) { |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 7054 | if (const VectorType *LV = lhsType->getAs<VectorType>()) { |
Chandler Carruth | 9ed87ba | 2010-08-30 07:36:24 +0000 | [diff] [blame] | 7055 | if (const VectorType *RV = rhsType->getAs<VectorType>()) { |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7056 | if (LV->getElementType() == RV->getElementType() && |
Anders Carlsson | db5a9b6 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 7057 | LV->getNumElements() == RV->getNumElements()) { |
Douglas Gregor | cbfbca1 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 7058 | if (lhsType->isExtVectorType()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7059 | rex = ImpCastExprToType(rex.take(), lhsType, CK_BitCast); |
Douglas Gregor | cbfbca1 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 7060 | return lhsType; |
| 7061 | } |
| 7062 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7063 | lex = ImpCastExprToType(lex.take(), rhsType, CK_BitCast); |
Douglas Gregor | cbfbca1 | 2010-05-19 03:21:00 +0000 | [diff] [blame] | 7064 | return rhsType; |
Eric Christopher | a613f56 | 2010-08-26 00:42:16 +0000 | [diff] [blame] | 7065 | } else if (Context.getTypeSize(lhsType) ==Context.getTypeSize(rhsType)){ |
| 7066 | // If we are allowing lax vector conversions, and LHS and RHS are both |
| 7067 | // vectors, the total size only needs to be the same. This is a |
| 7068 | // bitcast; no bits are changed but the result type is different. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7069 | rex = ImpCastExprToType(rex.take(), lhsType, CK_BitCast); |
Eric Christopher | a613f56 | 2010-08-26 00:42:16 +0000 | [diff] [blame] | 7070 | return lhsType; |
Anders Carlsson | db5a9b6 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 7071 | } |
Eric Christopher | a613f56 | 2010-08-26 00:42:16 +0000 | [diff] [blame] | 7072 | } |
Chandler Carruth | 9ed87ba | 2010-08-30 07:36:24 +0000 | [diff] [blame] | 7073 | } |
Anders Carlsson | db5a9b6 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 7074 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7075 | |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 7076 | // Handle the case of equivalent AltiVec and GCC vector types |
| 7077 | if (lhsType->isVectorType() && rhsType->isVectorType() && |
| 7078 | Context.areCompatibleVectorTypes(lhsType, rhsType)) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7079 | lex = ImpCastExprToType(lex.take(), rhsType, CK_BitCast); |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 7080 | return rhsType; |
| 7081 | } |
| 7082 | |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 7083 | // Canonicalize the ExtVector to the LHS, remember if we swapped so we can |
| 7084 | // swap back (so that we don't reverse the inputs to a subtract, for instance. |
| 7085 | bool swapped = false; |
| 7086 | if (rhsType->isExtVectorType()) { |
| 7087 | swapped = true; |
| 7088 | std::swap(rex, lex); |
| 7089 | std::swap(rhsType, lhsType); |
| 7090 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7091 | |
Nate Begeman | 886448d | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 7092 | // Handle the case of an ext vector and scalar. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 7093 | if (const ExtVectorType *LV = lhsType->getAs<ExtVectorType>()) { |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 7094 | QualType EltTy = LV->getElementType(); |
Douglas Gregor | 6972a62 | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 7095 | if (EltTy->isIntegralType(Context) && rhsType->isIntegralType(Context)) { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 7096 | int order = Context.getIntegerTypeOrder(EltTy, rhsType); |
| 7097 | if (order > 0) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7098 | rex = ImpCastExprToType(rex.take(), EltTy, CK_IntegralCast); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 7099 | if (order >= 0) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7100 | rex = ImpCastExprToType(rex.take(), lhsType, CK_VectorSplat); |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 7101 | if (swapped) std::swap(rex, lex); |
| 7102 | return lhsType; |
| 7103 | } |
| 7104 | } |
| 7105 | if (EltTy->isRealFloatingType() && rhsType->isScalarType() && |
| 7106 | rhsType->isRealFloatingType()) { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 7107 | int order = Context.getFloatingTypeOrder(EltTy, rhsType); |
| 7108 | if (order > 0) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7109 | rex = ImpCastExprToType(rex.take(), EltTy, CK_FloatingCast); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 7110 | if (order >= 0) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7111 | rex = ImpCastExprToType(rex.take(), lhsType, CK_VectorSplat); |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 7112 | if (swapped) std::swap(rex, lex); |
| 7113 | return lhsType; |
| 7114 | } |
Nate Begeman | 330aaa7 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 7115 | } |
| 7116 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7117 | |
Nate Begeman | 886448d | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 7118 | // Vectors of different size or scalar and non-ext-vector are errors. |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 7119 | Diag(Loc, diag::err_typecheck_vector_not_convertable) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7120 | << lex.get()->getType() << rex.get()->getType() |
| 7121 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Steve Naroff | 84ff4b4 | 2007-07-09 21:31:10 +0000 | [diff] [blame] | 7122 | return QualType(); |
Sebastian Redl | 112a9766 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 7123 | } |
| 7124 | |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 7125 | QualType Sema::CheckMultiplyDivideOperands( |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7126 | ExprResult &lex, ExprResult &rex, SourceLocation Loc, bool isCompAssign, bool isDiv) { |
| 7127 | if (lex.get()->getType()->isVectorType() || rex.get()->getType()->isVectorType()) |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7128 | return CheckVectorOperands(Loc, lex, rex); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7129 | |
Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 7130 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7131 | if (lex.isInvalid() || rex.isInvalid()) |
| 7132 | return QualType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7133 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7134 | if (!lex.get()->getType()->isArithmeticType() || |
| 7135 | !rex.get()->getType()->isArithmeticType()) |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 7136 | return InvalidOperands(Loc, lex, rex); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7137 | |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 7138 | // Check for division by zero. |
| 7139 | if (isDiv && |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7140 | rex.get()->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull)) |
| 7141 | DiagRuntimeBehavior(Loc, rex.get(), PDiag(diag::warn_division_by_zero) |
| 7142 | << rex.get()->getSourceRange()); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7143 | |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 7144 | return compType; |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 7145 | } |
| 7146 | |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 7147 | QualType Sema::CheckRemainderOperands( |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7148 | ExprResult &lex, ExprResult &rex, SourceLocation Loc, bool isCompAssign) { |
| 7149 | if (lex.get()->getType()->isVectorType() || rex.get()->getType()->isVectorType()) { |
| 7150 | if (lex.get()->getType()->hasIntegerRepresentation() && |
| 7151 | rex.get()->getType()->hasIntegerRepresentation()) |
Daniel Dunbar | 0d2bfec | 2009-01-05 22:55:36 +0000 | [diff] [blame] | 7152 | return CheckVectorOperands(Loc, lex, rex); |
| 7153 | return InvalidOperands(Loc, lex, rex); |
| 7154 | } |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 7155 | |
Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 7156 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7157 | if (lex.isInvalid() || rex.isInvalid()) |
| 7158 | return QualType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7159 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7160 | if (!lex.get()->getType()->isIntegerType() || !rex.get()->getType()->isIntegerType()) |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 7161 | return InvalidOperands(Loc, lex, rex); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7162 | |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 7163 | // Check for remainder by zero. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7164 | if (rex.get()->isNullPointerConstant(Context, Expr::NPC_ValueDependentIsNotNull)) |
| 7165 | DiagRuntimeBehavior(Loc, rex.get(), PDiag(diag::warn_remainder_by_zero) |
| 7166 | << rex.get()->getSourceRange()); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7167 | |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 7168 | return compType; |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 7169 | } |
| 7170 | |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 7171 | QualType Sema::CheckAdditionOperands( // C99 6.5.6 |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7172 | ExprResult &lex, ExprResult &rex, SourceLocation Loc, QualType* CompLHSTy) { |
| 7173 | if (lex.get()->getType()->isVectorType() || rex.get()->getType()->isVectorType()) { |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7174 | QualType compType = CheckVectorOperands(Loc, lex, rex); |
| 7175 | if (CompLHSTy) *CompLHSTy = compType; |
| 7176 | return compType; |
| 7177 | } |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 7178 | |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7179 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7180 | if (lex.isInvalid() || rex.isInvalid()) |
| 7181 | return QualType(); |
Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 7182 | |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 7183 | // handle the common case first (both operands are arithmetic). |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7184 | if (lex.get()->getType()->isArithmeticType() && |
| 7185 | rex.get()->getType()->isArithmeticType()) { |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7186 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 7187 | return compType; |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7188 | } |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 7189 | |
Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 7190 | // Put any potential pointer into PExp |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7191 | Expr* PExp = lex.get(), *IExp = rex.get(); |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 7192 | if (IExp->getType()->isAnyPointerType()) |
Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 7193 | std::swap(PExp, IExp); |
| 7194 | |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 7195 | if (PExp->getType()->isAnyPointerType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7196 | |
Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 7197 | if (IExp->getType()->isIntegerType()) { |
Steve Naroff | aacd4cc | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 7198 | QualType PointeeTy = PExp->getType()->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7199 | |
Chris Lattner | 12bdebb | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 7200 | // Check for arithmetic on pointers to incomplete types. |
| 7201 | if (PointeeTy->isVoidType()) { |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7202 | if (getLangOptions().CPlusPlus) { |
| 7203 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7204 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | dd430f7 | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 7205 | return QualType(); |
Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 7206 | } |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7207 | |
| 7208 | // GNU extension: arithmetic on pointer to void |
| 7209 | Diag(Loc, diag::ext_gnu_void_ptr) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7210 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Chris Lattner | 12bdebb | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 7211 | } else if (PointeeTy->isFunctionType()) { |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7212 | if (getLangOptions().CPlusPlus) { |
| 7213 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7214 | << lex.get()->getType() << lex.get()->getSourceRange(); |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7215 | return QualType(); |
| 7216 | } |
| 7217 | |
| 7218 | // GNU extension: arithmetic on pointer to function |
| 7219 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7220 | << lex.get()->getType() << lex.get()->getSourceRange(); |
Steve Naroff | a63372d | 2009-07-13 21:32:29 +0000 | [diff] [blame] | 7221 | } else { |
Steve Naroff | aacd4cc | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 7222 | // Check if we require a complete type. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7223 | if (((PExp->getType()->isPointerType() && |
Steve Naroff | a63372d | 2009-07-13 21:32:29 +0000 | [diff] [blame] | 7224 | !PExp->getType()->isDependentType()) || |
Steve Naroff | aacd4cc | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 7225 | PExp->getType()->isObjCObjectPointerType()) && |
| 7226 | RequireCompleteType(Loc, PointeeTy, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7227 | PDiag(diag::err_typecheck_arithmetic_incomplete_type) |
| 7228 | << PExp->getSourceRange() |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 7229 | << PExp->getType())) |
Steve Naroff | aacd4cc | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 7230 | return QualType(); |
| 7231 | } |
Chris Lattner | 12bdebb | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 7232 | // Diagnose bad cases where we step over interface counts. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 7233 | if (PointeeTy->isObjCObjectType() && LangOpts.ObjCNonFragileABI) { |
Chris Lattner | 12bdebb | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 7234 | Diag(Loc, diag::err_arithmetic_nonfragile_interface) |
| 7235 | << PointeeTy << PExp->getSourceRange(); |
| 7236 | return QualType(); |
| 7237 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7238 | |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7239 | if (CompLHSTy) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7240 | QualType LHSTy = Context.isPromotableBitField(lex.get()); |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 7241 | if (LHSTy.isNull()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7242 | LHSTy = lex.get()->getType(); |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 7243 | if (LHSTy->isPromotableIntegerType()) |
| 7244 | LHSTy = Context.getPromotedIntegerType(LHSTy); |
Douglas Gregor | d2c2d17 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 7245 | } |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7246 | *CompLHSTy = LHSTy; |
| 7247 | } |
Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 7248 | return PExp->getType(); |
| 7249 | } |
| 7250 | } |
| 7251 | |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7252 | return InvalidOperands(Loc, lex, rex); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 7253 | } |
| 7254 | |
Chris Lattner | 2a3569b | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 7255 | // C99 6.5.6 |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7256 | QualType Sema::CheckSubtractionOperands(ExprResult &lex, ExprResult &rex, |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7257 | SourceLocation Loc, QualType* CompLHSTy) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7258 | if (lex.get()->getType()->isVectorType() || rex.get()->getType()->isVectorType()) { |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7259 | QualType compType = CheckVectorOperands(Loc, lex, rex); |
| 7260 | if (CompLHSTy) *CompLHSTy = compType; |
| 7261 | return compType; |
| 7262 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7263 | |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7264 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7265 | if (lex.isInvalid() || rex.isInvalid()) |
| 7266 | return QualType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7267 | |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 7268 | // Enforce type constraints: C99 6.5.6p3. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7269 | |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 7270 | // Handle the common case first (both operands are arithmetic). |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7271 | if (lex.get()->getType()->isArithmeticType() && |
| 7272 | rex.get()->getType()->isArithmeticType()) { |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7273 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 7274 | return compType; |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7275 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7276 | |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 7277 | // Either ptr - int or ptr - ptr. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7278 | if (lex.get()->getType()->isAnyPointerType()) { |
| 7279 | QualType lpointee = lex.get()->getType()->getPointeeType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7280 | |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7281 | // The LHS must be an completely-defined object type. |
Douglas Gregor | f6cd928 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 7282 | |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7283 | bool ComplainAboutVoid = false; |
| 7284 | Expr *ComplainAboutFunc = 0; |
| 7285 | if (lpointee->isVoidType()) { |
| 7286 | if (getLangOptions().CPlusPlus) { |
| 7287 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
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 | return QualType(); |
| 7290 | } |
| 7291 | |
| 7292 | // GNU C extension: arithmetic on pointer to void |
| 7293 | ComplainAboutVoid = true; |
| 7294 | } else if (lpointee->isFunctionType()) { |
| 7295 | if (getLangOptions().CPlusPlus) { |
| 7296 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7297 | << lex.get()->getType() << lex.get()->getSourceRange(); |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 7298 | return QualType(); |
| 7299 | } |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7300 | |
| 7301 | // GNU C extension: arithmetic on pointer to function |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7302 | ComplainAboutFunc = lex.get(); |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7303 | } else if (!lpointee->isDependentType() && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7304 | RequireCompleteType(Loc, lpointee, |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 7305 | PDiag(diag::err_typecheck_sub_ptr_object) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7306 | << lex.get()->getSourceRange() |
| 7307 | << lex.get()->getType())) |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7308 | return QualType(); |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 7309 | |
Chris Lattner | 12bdebb | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 7310 | // Diagnose bad cases where we step over interface counts. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 7311 | if (lpointee->isObjCObjectType() && LangOpts.ObjCNonFragileABI) { |
Chris Lattner | 12bdebb | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 7312 | Diag(Loc, diag::err_arithmetic_nonfragile_interface) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7313 | << lpointee << lex.get()->getSourceRange(); |
Chris Lattner | 12bdebb | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 7314 | return QualType(); |
| 7315 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7316 | |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 7317 | // The result type of a pointer-int computation is the pointer type. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7318 | if (rex.get()->getType()->isIntegerType()) { |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7319 | if (ComplainAboutVoid) |
| 7320 | Diag(Loc, diag::ext_gnu_void_ptr) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7321 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7322 | if (ComplainAboutFunc) |
| 7323 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7324 | << ComplainAboutFunc->getType() |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7325 | << ComplainAboutFunc->getSourceRange(); |
| 7326 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7327 | if (CompLHSTy) *CompLHSTy = lex.get()->getType(); |
| 7328 | return lex.get()->getType(); |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7329 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7330 | |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 7331 | // Handle pointer-pointer subtractions. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7332 | if (const PointerType *RHSPTy = rex.get()->getType()->getAs<PointerType>()) { |
Eli Friedman | 1974e53 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 7333 | QualType rpointee = RHSPTy->getPointeeType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7334 | |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7335 | // RHS must be a completely-type object type. |
| 7336 | // Handle the GNU void* extension. |
| 7337 | if (rpointee->isVoidType()) { |
| 7338 | if (getLangOptions().CPlusPlus) { |
| 7339 | Diag(Loc, diag::err_typecheck_pointer_arith_void_type) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7340 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7341 | return QualType(); |
| 7342 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7343 | |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7344 | ComplainAboutVoid = true; |
| 7345 | } else if (rpointee->isFunctionType()) { |
| 7346 | if (getLangOptions().CPlusPlus) { |
| 7347 | Diag(Loc, diag::err_typecheck_pointer_arith_function_type) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7348 | << rex.get()->getType() << rex.get()->getSourceRange(); |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 7349 | return QualType(); |
| 7350 | } |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7351 | |
| 7352 | // GNU extension: arithmetic on pointer to function |
| 7353 | if (!ComplainAboutFunc) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7354 | ComplainAboutFunc = rex.get(); |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7355 | } else if (!rpointee->isDependentType() && |
| 7356 | RequireCompleteType(Loc, rpointee, |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 7357 | PDiag(diag::err_typecheck_sub_ptr_object) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7358 | << rex.get()->getSourceRange() |
| 7359 | << rex.get()->getType())) |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7360 | return QualType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7361 | |
Eli Friedman | 168fe15 | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 7362 | if (getLangOptions().CPlusPlus) { |
| 7363 | // Pointee types must be the same: C++ [expr.add] |
| 7364 | if (!Context.hasSameUnqualifiedType(lpointee, rpointee)) { |
| 7365 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7366 | << lex.get()->getType() << rex.get()->getType() |
| 7367 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Eli Friedman | 168fe15 | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 7368 | return QualType(); |
| 7369 | } |
| 7370 | } else { |
| 7371 | // Pointee types must be compatible C99 6.5.6p3 |
| 7372 | if (!Context.typesAreCompatible( |
| 7373 | Context.getCanonicalType(lpointee).getUnqualifiedType(), |
| 7374 | Context.getCanonicalType(rpointee).getUnqualifiedType())) { |
| 7375 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7376 | << lex.get()->getType() << rex.get()->getType() |
| 7377 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Eli Friedman | 168fe15 | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 7378 | return QualType(); |
| 7379 | } |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 7380 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7381 | |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7382 | if (ComplainAboutVoid) |
| 7383 | Diag(Loc, diag::ext_gnu_void_ptr) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7384 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7385 | if (ComplainAboutFunc) |
| 7386 | Diag(Loc, diag::ext_gnu_ptr_func_arith) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7387 | << ComplainAboutFunc->getType() |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 7388 | << ComplainAboutFunc->getSourceRange(); |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7389 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7390 | if (CompLHSTy) *CompLHSTy = lex.get()->getType(); |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 7391 | return Context.getPointerDiffType(); |
| 7392 | } |
| 7393 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7394 | |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7395 | return InvalidOperands(Loc, lex, rex); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 7396 | } |
| 7397 | |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 7398 | static bool isScopedEnumerationType(QualType T) { |
| 7399 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
| 7400 | return ET->getDecl()->isScoped(); |
| 7401 | return false; |
| 7402 | } |
| 7403 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7404 | static void DiagnoseBadShiftValues(Sema& S, ExprResult &lex, ExprResult &rex, |
Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7405 | SourceLocation Loc, unsigned Opc, |
| 7406 | QualType LHSTy) { |
| 7407 | llvm::APSInt Right; |
| 7408 | // Check right/shifter operand |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7409 | if (rex.get()->isValueDependent() || !rex.get()->isIntegerConstantExpr(Right, S.Context)) |
Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7410 | return; |
| 7411 | |
| 7412 | if (Right.isNegative()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7413 | S.DiagRuntimeBehavior(Loc, rex.get(), |
Ted Kremenek | 63657fe | 2011-03-01 18:09:31 +0000 | [diff] [blame] | 7414 | S.PDiag(diag::warn_shift_negative) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7415 | << rex.get()->getSourceRange()); |
Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7416 | return; |
| 7417 | } |
| 7418 | llvm::APInt LeftBits(Right.getBitWidth(), |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7419 | S.Context.getTypeSize(lex.get()->getType())); |
Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7420 | if (Right.uge(LeftBits)) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7421 | S.DiagRuntimeBehavior(Loc, rex.get(), |
Ted Kremenek | 26bbc3d | 2011-03-01 19:13:22 +0000 | [diff] [blame] | 7422 | S.PDiag(diag::warn_shift_gt_typewidth) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7423 | << rex.get()->getSourceRange()); |
Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7424 | return; |
| 7425 | } |
| 7426 | if (Opc != BO_Shl) |
| 7427 | return; |
| 7428 | |
| 7429 | // When left shifting an ICE which is signed, we can check for overflow which |
| 7430 | // according to C++ has undefined behavior ([expr.shift] 5.8/2). Unsigned |
| 7431 | // integers have defined behavior modulo one more than the maximum value |
| 7432 | // representable in the result type, so never warn for those. |
| 7433 | llvm::APSInt Left; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7434 | if (lex.get()->isValueDependent() || !lex.get()->isIntegerConstantExpr(Left, S.Context) || |
Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7435 | LHSTy->hasUnsignedIntegerRepresentation()) |
| 7436 | return; |
| 7437 | llvm::APInt ResultBits = |
| 7438 | static_cast<llvm::APInt&>(Right) + Left.getMinSignedBits(); |
| 7439 | if (LeftBits.uge(ResultBits)) |
| 7440 | return; |
| 7441 | llvm::APSInt Result = Left.extend(ResultBits.getLimitedValue()); |
| 7442 | Result = Result.shl(Right); |
| 7443 | |
Ted Kremenek | 70f05fd | 2011-06-15 00:54:52 +0000 | [diff] [blame] | 7444 | // Print the bit representation of the signed integer as an unsigned |
| 7445 | // hexadecimal number. |
| 7446 | llvm::SmallString<40> HexResult; |
| 7447 | Result.toString(HexResult, 16, /*Signed =*/false, /*Literal =*/true); |
| 7448 | |
Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7449 | // If we are only missing a sign bit, this is less likely to result in actual |
| 7450 | // bugs -- if the result is cast back to an unsigned type, it will have the |
| 7451 | // expected value. Thus we place this behind a different warning that can be |
| 7452 | // turned off separately if needed. |
| 7453 | if (LeftBits == ResultBits - 1) { |
Ted Kremenek | 70f05fd | 2011-06-15 00:54:52 +0000 | [diff] [blame] | 7454 | S.Diag(Loc, diag::warn_shift_result_sets_sign_bit) |
| 7455 | << HexResult.str() << LHSTy |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7456 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7457 | return; |
| 7458 | } |
| 7459 | |
| 7460 | S.Diag(Loc, diag::warn_shift_result_gt_typewidth) |
Ted Kremenek | 70f05fd | 2011-06-15 00:54:52 +0000 | [diff] [blame] | 7461 | << HexResult.str() << Result.getMinSignedBits() << LHSTy |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7462 | << Left.getBitWidth() << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7463 | } |
| 7464 | |
Chris Lattner | 2a3569b | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 7465 | // C99 6.5.7 |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7466 | QualType Sema::CheckShiftOperands(ExprResult &lex, ExprResult &rex, SourceLocation Loc, |
Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7467 | unsigned Opc, bool isCompAssign) { |
Chris Lattner | 5c11c41 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 7468 | // C99 6.5.7p2: Each of the operands shall have integer type. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7469 | if (!lex.get()->getType()->hasIntegerRepresentation() || |
| 7470 | !rex.get()->getType()->hasIntegerRepresentation()) |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7471 | return InvalidOperands(Loc, lex, rex); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7472 | |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 7473 | // C++0x: Don't allow scoped enums. FIXME: Use something better than |
| 7474 | // hasIntegerRepresentation() above instead of this. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7475 | if (isScopedEnumerationType(lex.get()->getType()) || |
| 7476 | isScopedEnumerationType(rex.get()->getType())) { |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 7477 | return InvalidOperands(Loc, lex, rex); |
| 7478 | } |
| 7479 | |
Nate Begeman | e46ee9a | 2009-10-25 02:26:48 +0000 | [diff] [blame] | 7480 | // Vector shifts promote their scalar inputs to vector type. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7481 | if (lex.get()->getType()->isVectorType() || rex.get()->getType()->isVectorType()) |
Nate Begeman | e46ee9a | 2009-10-25 02:26:48 +0000 | [diff] [blame] | 7482 | return CheckVectorOperands(Loc, lex, rex); |
| 7483 | |
Chris Lattner | 5c11c41 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 7484 | // Shifts don't perform usual arithmetic conversions, they just do integer |
| 7485 | // promotions on each operand. C99 6.5.7p3 |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7486 | |
John McCall | 57cdd88 | 2010-12-16 19:28:59 +0000 | [diff] [blame] | 7487 | // For the LHS, do usual unary conversions, but then reset them away |
| 7488 | // if this is a compound assignment. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7489 | ExprResult old_lex = lex; |
| 7490 | lex = UsualUnaryConversions(lex.take()); |
| 7491 | if (lex.isInvalid()) |
| 7492 | return QualType(); |
| 7493 | QualType LHSTy = lex.get()->getType(); |
John McCall | 57cdd88 | 2010-12-16 19:28:59 +0000 | [diff] [blame] | 7494 | if (isCompAssign) lex = old_lex; |
| 7495 | |
| 7496 | // The RHS is simpler. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7497 | rex = UsualUnaryConversions(rex.take()); |
| 7498 | if (rex.isInvalid()) |
| 7499 | return QualType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7500 | |
Ryan Flynn | f53fab8 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 7501 | // Sanity-check shift operands |
Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7502 | DiagnoseBadShiftValues(*this, lex, rex, Loc, Opc, LHSTy); |
Ryan Flynn | f53fab8 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 7503 | |
Chris Lattner | 5c11c41 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 7504 | // "The type of the result is that of the promoted left operand." |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7505 | return LHSTy; |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 7506 | } |
| 7507 | |
Chandler Carruth | 17773fc | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 7508 | static bool IsWithinTemplateSpecialization(Decl *D) { |
| 7509 | if (DeclContext *DC = D->getDeclContext()) { |
| 7510 | if (isa<ClassTemplateSpecializationDecl>(DC)) |
| 7511 | return true; |
| 7512 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) |
| 7513 | return FD->isFunctionTemplateSpecialization(); |
| 7514 | } |
| 7515 | return false; |
| 7516 | } |
| 7517 | |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7518 | // C99 6.5.8, C++ [expr.rel] |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7519 | QualType Sema::CheckCompareOperands(ExprResult &lex, ExprResult &rex, SourceLocation Loc, |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7520 | unsigned OpaqueOpc, bool isRelational) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7521 | BinaryOperatorKind Opc = (BinaryOperatorKind) OpaqueOpc; |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7522 | |
Chris Lattner | 9a152e2 | 2009-12-05 05:40:13 +0000 | [diff] [blame] | 7523 | // Handle vector comparisons separately. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7524 | if (lex.get()->getType()->isVectorType() || rex.get()->getType()->isVectorType()) |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7525 | return CheckVectorCompareOperands(lex, rex, Loc, isRelational); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7526 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7527 | QualType lType = lex.get()->getType(); |
| 7528 | QualType rType = rex.get()->getType(); |
Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 7529 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7530 | Expr *LHSStripped = lex.get()->IgnoreParenImpCasts(); |
| 7531 | Expr *RHSStripped = rex.get()->IgnoreParenImpCasts(); |
Chandler Carruth | 712563b | 2011-02-17 08:37:06 +0000 | [diff] [blame] | 7532 | QualType LHSStrippedType = LHSStripped->getType(); |
| 7533 | QualType RHSStrippedType = RHSStripped->getType(); |
| 7534 | |
Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 7535 | |
| 7536 | |
Chandler Carruth | 712563b | 2011-02-17 08:37:06 +0000 | [diff] [blame] | 7537 | // Two different enums will raise a warning when compared. |
| 7538 | if (const EnumType *LHSEnumType = LHSStrippedType->getAs<EnumType>()) { |
| 7539 | if (const EnumType *RHSEnumType = RHSStrippedType->getAs<EnumType>()) { |
| 7540 | if (LHSEnumType->getDecl()->getIdentifier() && |
| 7541 | RHSEnumType->getDecl()->getIdentifier() && |
| 7542 | !Context.hasSameUnqualifiedType(LHSStrippedType, RHSStrippedType)) { |
| 7543 | Diag(Loc, diag::warn_comparison_of_mixed_enum_types) |
| 7544 | << LHSStrippedType << RHSStrippedType |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7545 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Chandler Carruth | 712563b | 2011-02-17 08:37:06 +0000 | [diff] [blame] | 7546 | } |
| 7547 | } |
| 7548 | } |
| 7549 | |
Douglas Gregor | 4ffbad1 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 7550 | if (!lType->hasFloatingRepresentation() && |
Ted Kremenek | 853734e | 2010-09-16 00:03:01 +0000 | [diff] [blame] | 7551 | !(lType->isBlockPointerType() && isRelational) && |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7552 | !lex.get()->getLocStart().isMacroID() && |
| 7553 | !rex.get()->getLocStart().isMacroID()) { |
Chris Lattner | 222b8bd | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 7554 | // For non-floating point types, check for self-comparisons of the form |
| 7555 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 7556 | // often indicate logic errors in the program. |
Chandler Carruth | 65a3818 | 2010-07-12 06:23:38 +0000 | [diff] [blame] | 7557 | // |
| 7558 | // NOTE: Don't warn about comparison expressions resulting from macro |
| 7559 | // expansion. Also don't warn about comparisons which are only self |
| 7560 | // comparisons within a template specialization. The warnings should catch |
| 7561 | // obvious cases in the definition of the template anyways. The idea is to |
| 7562 | // warn when the typed comparison operator will always evaluate to the same |
| 7563 | // result. |
Chandler Carruth | 17773fc | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 7564 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LHSStripped)) { |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7565 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RHSStripped)) { |
Ted Kremenek | 853734e | 2010-09-16 00:03:01 +0000 | [diff] [blame] | 7566 | if (DRL->getDecl() == DRR->getDecl() && |
Chandler Carruth | 17773fc | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 7567 | !IsWithinTemplateSpecialization(DRL->getDecl())) { |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 7568 | DiagRuntimeBehavior(Loc, 0, PDiag(diag::warn_comparison_always) |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7569 | << 0 // self- |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7570 | << (Opc == BO_EQ |
| 7571 | || Opc == BO_LE |
| 7572 | || Opc == BO_GE)); |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7573 | } else if (lType->isArrayType() && rType->isArrayType() && |
| 7574 | !DRL->getDecl()->getType()->isReferenceType() && |
| 7575 | !DRR->getDecl()->getType()->isReferenceType()) { |
| 7576 | // what is it always going to eval to? |
| 7577 | char always_evals_to; |
| 7578 | switch(Opc) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7579 | case BO_EQ: // e.g. array1 == array2 |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7580 | always_evals_to = 0; // false |
| 7581 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7582 | case BO_NE: // e.g. array1 != array2 |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7583 | always_evals_to = 1; // true |
| 7584 | break; |
| 7585 | default: |
| 7586 | // best we can say is 'a constant' |
| 7587 | always_evals_to = 2; // e.g. array1 <= array2 |
| 7588 | break; |
| 7589 | } |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 7590 | DiagRuntimeBehavior(Loc, 0, PDiag(diag::warn_comparison_always) |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7591 | << 1 // array |
| 7592 | << always_evals_to); |
| 7593 | } |
| 7594 | } |
Chandler Carruth | 17773fc | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 7595 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7596 | |
Chris Lattner | 222b8bd | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 7597 | if (isa<CastExpr>(LHSStripped)) |
| 7598 | LHSStripped = LHSStripped->IgnoreParenCasts(); |
| 7599 | if (isa<CastExpr>(RHSStripped)) |
| 7600 | RHSStripped = RHSStripped->IgnoreParenCasts(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7601 | |
Chris Lattner | 222b8bd | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 7602 | // Warn about comparisons against a string constant (unless the other |
| 7603 | // operand is null), the user probably wants strcmp. |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7604 | Expr *literalString = 0; |
| 7605 | Expr *literalStringStripped = 0; |
Chris Lattner | 222b8bd | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 7606 | if ((isa<StringLiteral>(LHSStripped) || isa<ObjCEncodeExpr>(LHSStripped)) && |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7607 | !RHSStripped->isNullPointerConstant(Context, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 7608 | Expr::NPC_ValueDependentIsNull)) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7609 | literalString = lex.get(); |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7610 | literalStringStripped = LHSStripped; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 7611 | } else if ((isa<StringLiteral>(RHSStripped) || |
| 7612 | isa<ObjCEncodeExpr>(RHSStripped)) && |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7613 | !LHSStripped->isNullPointerConstant(Context, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 7614 | Expr::NPC_ValueDependentIsNull)) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7615 | literalString = rex.get(); |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7616 | literalStringStripped = RHSStripped; |
| 7617 | } |
| 7618 | |
| 7619 | if (literalString) { |
| 7620 | std::string resultComparison; |
| 7621 | switch (Opc) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7622 | case BO_LT: resultComparison = ") < 0"; break; |
| 7623 | case BO_GT: resultComparison = ") > 0"; break; |
| 7624 | case BO_LE: resultComparison = ") <= 0"; break; |
| 7625 | case BO_GE: resultComparison = ") >= 0"; break; |
| 7626 | case BO_EQ: resultComparison = ") == 0"; break; |
| 7627 | case BO_NE: resultComparison = ") != 0"; break; |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7628 | default: assert(false && "Invalid comparison operator"); |
| 7629 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7630 | |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 7631 | DiagRuntimeBehavior(Loc, 0, |
Douglas Gregor | 49862b8 | 2010-01-12 23:18:54 +0000 | [diff] [blame] | 7632 | PDiag(diag::warn_stringcompare) |
| 7633 | << isa<ObjCEncodeExpr>(literalStringStripped) |
Ted Kremenek | 800b66b | 2010-04-09 20:26:53 +0000 | [diff] [blame] | 7634 | << literalString->getSourceRange()); |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7635 | } |
Ted Kremenek | e451eae | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 7636 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7637 | |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7638 | // C99 6.5.8p3 / C99 6.5.9p4 |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7639 | if (lex.get()->getType()->isArithmeticType() && rex.get()->getType()->isArithmeticType()) { |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7640 | UsualArithmeticConversions(lex, rex); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7641 | if (lex.isInvalid() || rex.isInvalid()) |
| 7642 | return QualType(); |
| 7643 | } |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7644 | else { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7645 | lex = UsualUnaryConversions(lex.take()); |
| 7646 | if (lex.isInvalid()) |
| 7647 | return QualType(); |
| 7648 | |
| 7649 | rex = UsualUnaryConversions(rex.take()); |
| 7650 | if (rex.isInvalid()) |
| 7651 | return QualType(); |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7652 | } |
| 7653 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7654 | lType = lex.get()->getType(); |
| 7655 | rType = rex.get()->getType(); |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7656 | |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7657 | // The result of comparisons is 'bool' in C++, 'int' in C. |
Argyrios Kyrtzidis | 1bdd688 | 2011-02-18 20:55:15 +0000 | [diff] [blame] | 7658 | QualType ResultTy = Context.getLogicalOperationType(); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7659 | |
Chris Lattner | b620c34 | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 7660 | if (isRelational) { |
| 7661 | if (lType->isRealType() && rType->isRealType()) |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7662 | return ResultTy; |
Chris Lattner | b620c34 | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 7663 | } else { |
Ted Kremenek | e2763b0 | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 7664 | // Check for comparisons of floating point operands using != and ==. |
Douglas Gregor | 4ffbad1 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 7665 | if (lType->hasFloatingRepresentation()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7666 | CheckFloatComparison(Loc, lex.get(), rex.get()); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7667 | |
Chris Lattner | b620c34 | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 7668 | if (lType->isArithmeticType() && rType->isArithmeticType()) |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7669 | return ResultTy; |
Chris Lattner | b620c34 | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 7670 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7671 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7672 | bool LHSIsNull = lex.get()->isNullPointerConstant(Context, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 7673 | Expr::NPC_ValueDependentIsNull); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7674 | bool RHSIsNull = rex.get()->isNullPointerConstant(Context, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 7675 | Expr::NPC_ValueDependentIsNull); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7676 | |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7677 | // All of the following pointer-related warnings are GCC extensions, except |
| 7678 | // when handling null pointer constants. |
Steve Naroff | 808eb8f | 2007-08-27 04:08:11 +0000 | [diff] [blame] | 7679 | if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2 |
Chris Lattner | 3a0702e | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 7680 | QualType LCanPointeeTy = |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 7681 | Context.getCanonicalType(lType->getAs<PointerType>()->getPointeeType()); |
Chris Lattner | 3a0702e | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 7682 | QualType RCanPointeeTy = |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 7683 | Context.getCanonicalType(rType->getAs<PointerType>()->getPointeeType()); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7684 | |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7685 | if (getLangOptions().CPlusPlus) { |
Eli Friedman | 16c20961 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 7686 | if (LCanPointeeTy == RCanPointeeTy) |
| 7687 | return ResultTy; |
Fariborz Jahanian | ffc420c | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 7688 | if (!isRelational && |
| 7689 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 7690 | // Valid unless comparison between non-null pointer and function pointer |
| 7691 | // This is a gcc extension compatibility comparison. |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7692 | // In a SFINAE context, we treat this as a hard error to maintain |
| 7693 | // conformance with the C++ standard. |
Fariborz Jahanian | ffc420c | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 7694 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
| 7695 | && !LHSIsNull && !RHSIsNull) { |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7696 | Diag(Loc, |
| 7697 | isSFINAEContext()? |
| 7698 | diag::err_typecheck_comparison_of_fptr_to_void |
| 7699 | : diag::ext_typecheck_comparison_of_fptr_to_void) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7700 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7701 | |
| 7702 | if (isSFINAEContext()) |
| 7703 | return QualType(); |
| 7704 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7705 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
Fariborz Jahanian | ffc420c | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 7706 | return ResultTy; |
| 7707 | } |
| 7708 | } |
Anders Carlsson | a95069c | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 7709 | |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7710 | // C++ [expr.rel]p2: |
| 7711 | // [...] Pointer conversions (4.10) and qualification |
| 7712 | // conversions (4.4) are performed on pointer operands (or on |
| 7713 | // a pointer operand and a null pointer constant) to bring |
| 7714 | // them to their composite pointer type. [...] |
| 7715 | // |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7716 | // C++ [expr.eq]p1 uses the same notion for (in)equality |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7717 | // comparisons of pointers. |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7718 | bool NonStandardCompositeType = false; |
Douglas Gregor | 19175ff | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 7719 | QualType T = FindCompositePointerType(Loc, lex, rex, |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7720 | isSFINAEContext()? 0 : &NonStandardCompositeType); |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7721 | if (T.isNull()) { |
| 7722 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_pointers) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7723 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7724 | return QualType(); |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7725 | } else if (NonStandardCompositeType) { |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7726 | Diag(Loc, |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7727 | diag::ext_typecheck_comparison_of_distinct_pointers_nonstandard) |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7728 | << lType << rType << T |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7729 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7730 | } |
| 7731 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7732 | lex = ImpCastExprToType(lex.take(), T, CK_BitCast); |
| 7733 | rex = ImpCastExprToType(rex.take(), T, CK_BitCast); |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7734 | return ResultTy; |
| 7735 | } |
Eli Friedman | 16c20961 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 7736 | // C99 6.5.9p2 and C99 6.5.8p2 |
| 7737 | if (Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(), |
| 7738 | RCanPointeeTy.getUnqualifiedType())) { |
| 7739 | // Valid unless a relational comparison of function pointers |
| 7740 | if (isRelational && LCanPointeeTy->isFunctionType()) { |
| 7741 | Diag(Loc, diag::ext_typecheck_ordered_comparison_of_function_pointers) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7742 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Eli Friedman | 16c20961 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 7743 | } |
| 7744 | } else if (!isRelational && |
| 7745 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 7746 | // Valid unless comparison between non-null pointer and function pointer |
| 7747 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
| 7748 | && !LHSIsNull && !RHSIsNull) { |
| 7749 | Diag(Loc, diag::ext_typecheck_comparison_of_fptr_to_void) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7750 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Eli Friedman | 16c20961 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 7751 | } |
| 7752 | } else { |
| 7753 | // Invalid |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 7754 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7755 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Steve Naroff | 75c1723 | 2007-06-13 21:41:08 +0000 | [diff] [blame] | 7756 | } |
John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7757 | if (LCanPointeeTy != RCanPointeeTy) { |
| 7758 | if (LHSIsNull && !RHSIsNull) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7759 | lex = ImpCastExprToType(lex.take(), rType, CK_BitCast); |
John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7760 | else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7761 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7762 | } |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7763 | return ResultTy; |
Steve Naroff | cdee44c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 7764 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7765 | |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 7766 | if (getLangOptions().CPlusPlus) { |
Anders Carlsson | a95069c | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 7767 | // Comparison of nullptr_t with itself. |
| 7768 | if (lType->isNullPtrType() && rType->isNullPtrType()) |
| 7769 | return ResultTy; |
| 7770 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7771 | // Comparison of pointers with null pointer constants and equality |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7772 | // comparisons of member pointers to null pointer constants. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7773 | if (RHSIsNull && |
Douglas Gregor | 39e9fa9 | 2011-06-01 15:12:24 +0000 | [diff] [blame] | 7774 | ((lType->isAnyPointerType() || lType->isNullPtrType()) || |
Douglas Gregor | 3e85c9c | 2011-06-16 18:52:05 +0000 | [diff] [blame] | 7775 | (!isRelational && |
| 7776 | (lType->isMemberPointerType() || lType->isBlockPointerType())))) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7777 | rex = ImpCastExprToType(rex.take(), lType, |
Douglas Gregor | f58ff32 | 2010-08-07 13:36:37 +0000 | [diff] [blame] | 7778 | lType->isMemberPointerType() |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7779 | ? CK_NullToMemberPointer |
John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7780 | : CK_NullToPointer); |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 7781 | return ResultTy; |
| 7782 | } |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7783 | if (LHSIsNull && |
Douglas Gregor | 39e9fa9 | 2011-06-01 15:12:24 +0000 | [diff] [blame] | 7784 | ((rType->isAnyPointerType() || rType->isNullPtrType()) || |
Douglas Gregor | 3e85c9c | 2011-06-16 18:52:05 +0000 | [diff] [blame] | 7785 | (!isRelational && |
| 7786 | (rType->isMemberPointerType() || rType->isBlockPointerType())))) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7787 | lex = ImpCastExprToType(lex.take(), rType, |
Douglas Gregor | f58ff32 | 2010-08-07 13:36:37 +0000 | [diff] [blame] | 7788 | rType->isMemberPointerType() |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7789 | ? CK_NullToMemberPointer |
John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7790 | : CK_NullToPointer); |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 7791 | return ResultTy; |
| 7792 | } |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7793 | |
| 7794 | // Comparison of member pointers. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7795 | if (!isRelational && |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7796 | lType->isMemberPointerType() && rType->isMemberPointerType()) { |
| 7797 | // C++ [expr.eq]p2: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7798 | // In addition, pointers to members can be compared, or a pointer to |
| 7799 | // member and a null pointer constant. Pointer to member conversions |
| 7800 | // (4.11) and qualification conversions (4.4) are performed to bring |
| 7801 | // them to a common type. If one operand is a null pointer constant, |
| 7802 | // the common type is the type of the other operand. Otherwise, the |
| 7803 | // common type is a pointer to member type similar (4.4) to the type |
| 7804 | // of one of the operands, with a cv-qualification signature (4.4) |
| 7805 | // that is the union of the cv-qualification signatures of the operand |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7806 | // types. |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7807 | bool NonStandardCompositeType = false; |
Douglas Gregor | 19175ff | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 7808 | QualType T = FindCompositePointerType(Loc, lex, rex, |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7809 | isSFINAEContext()? 0 : &NonStandardCompositeType); |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7810 | if (T.isNull()) { |
| 7811 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_pointers) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7812 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7813 | return QualType(); |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7814 | } else if (NonStandardCompositeType) { |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7815 | Diag(Loc, |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 7816 | diag::ext_typecheck_comparison_of_distinct_pointers_nonstandard) |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7817 | << lType << rType << T |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7818 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7819 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7820 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7821 | lex = ImpCastExprToType(lex.take(), T, CK_BitCast); |
| 7822 | rex = ImpCastExprToType(rex.take(), T, CK_BitCast); |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7823 | return ResultTy; |
| 7824 | } |
Douglas Gregor | 48e6bbf | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 7825 | |
| 7826 | // Handle scoped enumeration types specifically, since they don't promote |
| 7827 | // to integers. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7828 | if (lex.get()->getType()->isEnumeralType() && |
| 7829 | Context.hasSameUnqualifiedType(lex.get()->getType(), rex.get()->getType())) |
Douglas Gregor | 48e6bbf | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 7830 | return ResultTy; |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 7831 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7832 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7833 | // Handle block pointer types. |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 7834 | if (!isRelational && lType->isBlockPointerType() && rType->isBlockPointerType()) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 7835 | QualType lpointee = lType->getAs<BlockPointerType>()->getPointeeType(); |
| 7836 | QualType rpointee = rType->getAs<BlockPointerType>()->getPointeeType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7837 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7838 | if (!LHSIsNull && !RHSIsNull && |
Eli Friedman | a6638ca | 2009-06-08 05:08:54 +0000 | [diff] [blame] | 7839 | !Context.typesAreCompatible(lpointee, rpointee)) { |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 7840 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7841 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7842 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7843 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7844 | return ResultTy; |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7845 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7846 | |
Steve Naroff | e18f94c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 7847 | // Allow block pointers to be compared with null pointer constants. |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 7848 | if (!isRelational |
| 7849 | && ((lType->isBlockPointerType() && rType->isPointerType()) |
| 7850 | || (lType->isPointerType() && rType->isBlockPointerType()))) { |
Steve Naroff | e18f94c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 7851 | if (!LHSIsNull && !RHSIsNull) { |
John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7852 | if (!((rType->isPointerType() && rType->castAs<PointerType>() |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 7853 | ->getPointeeType()->isVoidType()) |
John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7854 | || (lType->isPointerType() && lType->castAs<PointerType>() |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 7855 | ->getPointeeType()->isVoidType()))) |
| 7856 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7857 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Steve Naroff | e18f94c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 7858 | } |
John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7859 | if (LHSIsNull && !RHSIsNull) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7860 | lex = ImpCastExprToType(lex.take(), rType, CK_BitCast); |
John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7861 | else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7862 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7863 | return ResultTy; |
Steve Naroff | e18f94c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 7864 | } |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7865 | |
John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7866 | if (lType->isObjCObjectPointerType() || rType->isObjCObjectPointerType()) { |
| 7867 | const PointerType *LPT = lType->getAs<PointerType>(); |
| 7868 | const PointerType *RPT = rType->getAs<PointerType>(); |
| 7869 | if (LPT || RPT) { |
| 7870 | bool LPtrToVoid = LPT ? LPT->getPointeeType()->isVoidType() : false; |
| 7871 | bool RPtrToVoid = RPT ? RPT->getPointeeType()->isVoidType() : false; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7872 | |
Steve Naroff | 753567f | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 7873 | if (!LPtrToVoid && !RPtrToVoid && |
| 7874 | !Context.typesAreCompatible(lType, rType)) { |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 7875 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7876 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Steve Naroff | 1d4a9a3 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 7877 | } |
John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7878 | if (LHSIsNull && !RHSIsNull) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7879 | lex = ImpCastExprToType(lex.take(), rType, CK_BitCast); |
John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7880 | else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7881 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7882 | return ResultTy; |
Steve Naroff | ea54d9e | 2008-10-20 18:19:10 +0000 | [diff] [blame] | 7883 | } |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7884 | if (lType->isObjCObjectPointerType() && rType->isObjCObjectPointerType()) { |
Chris Lattner | f8344db | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 7885 | if (!Context.areComparableObjCPointerTypes(lType, rType)) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 7886 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7887 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7888 | if (LHSIsNull && !RHSIsNull) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7889 | lex = ImpCastExprToType(lex.take(), rType, CK_BitCast); |
John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7890 | else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7891 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7892 | return ResultTy; |
Steve Naroff | b788d9b | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 7893 | } |
Fariborz Jahanian | 134cbef | 2007-12-20 01:06:58 +0000 | [diff] [blame] | 7894 | } |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7895 | if ((lType->isAnyPointerType() && rType->isIntegerType()) || |
| 7896 | (lType->isIntegerType() && rType->isAnyPointerType())) { |
Chris Lattner | d99bd52 | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7897 | unsigned DiagID = 0; |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7898 | bool isError = false; |
| 7899 | if ((LHSIsNull && lType->isIntegerType()) || |
| 7900 | (RHSIsNull && rType->isIntegerType())) { |
| 7901 | if (isRelational && !getLangOptions().CPlusPlus) |
Chris Lattner | d99bd52 | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7902 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_and_zero; |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7903 | } else if (isRelational && !getLangOptions().CPlusPlus) |
Chris Lattner | d99bd52 | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7904 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_integer; |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7905 | else if (getLangOptions().CPlusPlus) { |
| 7906 | DiagID = diag::err_typecheck_comparison_of_pointer_integer; |
| 7907 | isError = true; |
| 7908 | } else |
Chris Lattner | d99bd52 | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7909 | DiagID = diag::ext_typecheck_comparison_of_pointer_integer; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7910 | |
Chris Lattner | d99bd52 | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7911 | if (DiagID) { |
Chris Lattner | f8344db | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 7912 | Diag(Loc, DiagID) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7913 | << lType << rType << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7914 | if (isError) |
| 7915 | return QualType(); |
Chris Lattner | f8344db | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 7916 | } |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7917 | |
| 7918 | if (lType->isIntegerType()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7919 | lex = ImpCastExprToType(lex.take(), rType, |
John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7920 | LHSIsNull ? CK_NullToPointer : CK_IntegralToPointer); |
Chris Lattner | d99bd52 | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7921 | else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7922 | rex = ImpCastExprToType(rex.take(), lType, |
John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7923 | RHSIsNull ? CK_NullToPointer : CK_IntegralToPointer); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7924 | return ResultTy; |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 7925 | } |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7926 | |
Steve Naroff | 4b19157 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 7927 | // Handle block pointers. |
Mike Stump | f70bcf7 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 7928 | if (!isRelational && RHSIsNull |
| 7929 | && lType->isBlockPointerType() && rType->isIntegerType()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7930 | rex = ImpCastExprToType(rex.take(), lType, CK_NullToPointer); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7931 | return ResultTy; |
Steve Naroff | 4b19157 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 7932 | } |
Mike Stump | f70bcf7 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 7933 | if (!isRelational && LHSIsNull |
| 7934 | && lType->isIntegerType() && rType->isBlockPointerType()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7935 | lex = ImpCastExprToType(lex.take(), rType, CK_NullToPointer); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7936 | return ResultTy; |
Steve Naroff | 4b19157 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 7937 | } |
Douglas Gregor | 48e6bbf | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 7938 | |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7939 | return InvalidOperands(Loc, lex, rex); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 7940 | } |
| 7941 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7942 | /// CheckVectorCompareOperands - vector comparisons are a clang extension that |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7943 | /// operates on extended vector types. Instead of producing an IntTy result, |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7944 | /// like a scalar comparison, a vector comparison produces a vector of integer |
| 7945 | /// types. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7946 | QualType Sema::CheckVectorCompareOperands(ExprResult &lex, ExprResult &rex, |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7947 | SourceLocation Loc, |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7948 | bool isRelational) { |
| 7949 | // Check to make sure we're operating on vectors of the same type and width, |
| 7950 | // Allowing one side to be a scalar of element type. |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7951 | QualType vType = CheckVectorOperands(Loc, lex, rex); |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7952 | if (vType.isNull()) |
| 7953 | return vType; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7954 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7955 | QualType lType = lex.get()->getType(); |
| 7956 | QualType rType = rex.get()->getType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7957 | |
Anton Yartsev | 530deb9 | 2011-03-27 15:36:07 +0000 | [diff] [blame] | 7958 | // If AltiVec, the comparison results in a numeric type, i.e. |
| 7959 | // bool for C++, int for C |
Anton Yartsev | 93900c7 | 2011-03-28 21:00:05 +0000 | [diff] [blame] | 7960 | if (vType->getAs<VectorType>()->getVectorKind() == VectorType::AltiVecVector) |
Anton Yartsev | 530deb9 | 2011-03-27 15:36:07 +0000 | [diff] [blame] | 7961 | return Context.getLogicalOperationType(); |
| 7962 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7963 | // For non-floating point types, check for self-comparisons of the form |
| 7964 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 7965 | // often indicate logic errors in the program. |
Douglas Gregor | 4ffbad1 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 7966 | if (!lType->hasFloatingRepresentation()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7967 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex.get()->IgnoreParens())) |
| 7968 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex.get()->IgnoreParens())) |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7969 | if (DRL->getDecl() == DRR->getDecl()) |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 7970 | DiagRuntimeBehavior(Loc, 0, |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7971 | PDiag(diag::warn_comparison_always) |
| 7972 | << 0 // self- |
| 7973 | << 2 // "a constant" |
| 7974 | ); |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7975 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7976 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7977 | // Check for comparisons of floating point operands using != and ==. |
Douglas Gregor | 4ffbad1 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 7978 | if (!isRelational && lType->hasFloatingRepresentation()) { |
| 7979 | assert (rType->hasFloatingRepresentation()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7980 | CheckFloatComparison(Loc, lex.get(), rex.get()); |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7981 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7982 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7983 | // Return the type for the comparison, which is the same as vector type for |
| 7984 | // integer vectors, or an integer type of identical size and number of |
| 7985 | // elements for floating point vectors. |
Douglas Gregor | 5cc2c8b | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 7986 | if (lType->hasIntegerRepresentation()) |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7987 | return lType; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7988 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 7989 | const VectorType *VTy = lType->getAs<VectorType>(); |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7990 | unsigned TypeSize = Context.getTypeSize(VTy->getElementType()); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 7991 | if (TypeSize == Context.getTypeSize(Context.IntTy)) |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7992 | return Context.getExtVectorType(Context.IntTy, VTy->getNumElements()); |
Chris Lattner | 5d68896 | 2009-03-31 07:46:52 +0000 | [diff] [blame] | 7993 | if (TypeSize == Context.getTypeSize(Context.LongTy)) |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 7994 | return Context.getExtVectorType(Context.LongTy, VTy->getNumElements()); |
| 7995 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7996 | assert(TypeSize == Context.getTypeSize(Context.LongLongTy) && |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 7997 | "Unhandled vector element size in vector compare"); |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7998 | return Context.getExtVectorType(Context.LongLongTy, VTy->getNumElements()); |
| 7999 | } |
| 8000 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 8001 | inline QualType Sema::CheckBitwiseOperands( |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8002 | ExprResult &lex, ExprResult &rex, SourceLocation Loc, bool isCompAssign) { |
| 8003 | if (lex.get()->getType()->isVectorType() || rex.get()->getType()->isVectorType()) { |
| 8004 | if (lex.get()->getType()->hasIntegerRepresentation() && |
| 8005 | rex.get()->getType()->hasIntegerRepresentation()) |
Douglas Gregor | 5cc2c8b | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 8006 | return CheckVectorOperands(Loc, lex, rex); |
| 8007 | |
| 8008 | return InvalidOperands(Loc, lex, rex); |
| 8009 | } |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 8010 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8011 | ExprResult lexResult = Owned(lex), rexResult = Owned(rex); |
| 8012 | QualType compType = UsualArithmeticConversions(lexResult, rexResult, isCompAssign); |
| 8013 | if (lexResult.isInvalid() || rexResult.isInvalid()) |
| 8014 | return QualType(); |
| 8015 | lex = lexResult.take(); |
| 8016 | rex = rexResult.take(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8017 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8018 | if (lex.get()->getType()->isIntegralOrUnscopedEnumerationType() && |
| 8019 | rex.get()->getType()->isIntegralOrUnscopedEnumerationType()) |
Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 8020 | return compType; |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 8021 | return InvalidOperands(Loc, lex, rex); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 8022 | } |
| 8023 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 8024 | inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8025 | ExprResult &lex, ExprResult &rex, SourceLocation Loc, unsigned Opc) { |
Chris Lattner | 8406c51 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 8026 | |
| 8027 | // Diagnose cases where the user write a logical and/or but probably meant a |
| 8028 | // bitwise one. We do this when the LHS is a non-bool integer and the RHS |
| 8029 | // is a constant. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8030 | if (lex.get()->getType()->isIntegerType() && !lex.get()->getType()->isBooleanType() && |
| 8031 | rex.get()->getType()->isIntegerType() && !rex.get()->isValueDependent() && |
Chris Lattner | deee7a3 | 2010-07-15 00:26:43 +0000 | [diff] [blame] | 8032 | // Don't warn in macros. |
Chris Lattner | 938533d | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 8033 | !Loc.isMacroID()) { |
| 8034 | // If the RHS can be constant folded, and if it constant folds to something |
| 8035 | // that isn't 0 or 1 (which indicate a potential logical operation that |
| 8036 | // happened to fold to true/false) then warn. |
Chandler Carruth | e54ff6c | 2011-05-31 05:41:42 +0000 | [diff] [blame] | 8037 | // Parens on the RHS are ignored. |
Chris Lattner | 938533d | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 8038 | Expr::EvalResult Result; |
Chandler Carruth | e54ff6c | 2011-05-31 05:41:42 +0000 | [diff] [blame] | 8039 | if (rex.get()->Evaluate(Result, Context) && !Result.HasSideEffects) |
| 8040 | if ((getLangOptions().Bool && !rex.get()->getType()->isBooleanType()) || |
| 8041 | (Result.Val.getInt() != 0 && Result.Val.getInt() != 1)) { |
| 8042 | Diag(Loc, diag::warn_logical_instead_of_bitwise) |
| 8043 | << rex.get()->getSourceRange() |
| 8044 | << (Opc == BO_LAnd ? "&&" : "||") |
| 8045 | << (Opc == BO_LAnd ? "&" : "|"); |
Chris Lattner | 938533d | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 8046 | } |
| 8047 | } |
Chris Lattner | 8406c51 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 8048 | |
Anders Carlsson | 2e7bc11 | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 8049 | if (!Context.getLangOptions().CPlusPlus) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8050 | lex = UsualUnaryConversions(lex.take()); |
| 8051 | if (lex.isInvalid()) |
| 8052 | return QualType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8053 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8054 | rex = UsualUnaryConversions(rex.take()); |
| 8055 | if (rex.isInvalid()) |
| 8056 | return QualType(); |
| 8057 | |
| 8058 | if (!lex.get()->getType()->isScalarType() || !rex.get()->getType()->isScalarType()) |
Anders Carlsson | 2e7bc11 | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 8059 | return InvalidOperands(Loc, lex, rex); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8060 | |
Anders Carlsson | 2e7bc11 | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 8061 | return Context.IntTy; |
Anders Carlsson | 35a99d9 | 2009-10-16 01:44:21 +0000 | [diff] [blame] | 8062 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8063 | |
John McCall | 4a2429a | 2010-06-04 00:29:51 +0000 | [diff] [blame] | 8064 | // The following is safe because we only use this method for |
| 8065 | // non-overloadable operands. |
| 8066 | |
Anders Carlsson | 2e7bc11 | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 8067 | // C++ [expr.log.and]p1 |
| 8068 | // C++ [expr.log.or]p1 |
John McCall | 4a2429a | 2010-06-04 00:29:51 +0000 | [diff] [blame] | 8069 | // The operands are both contextually converted to type bool. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8070 | ExprResult lexRes = PerformContextuallyConvertToBool(lex.get()); |
| 8071 | if (lexRes.isInvalid()) |
Anders Carlsson | 2e7bc11 | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 8072 | return InvalidOperands(Loc, lex, rex); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8073 | lex = move(lexRes); |
| 8074 | |
| 8075 | ExprResult rexRes = PerformContextuallyConvertToBool(rex.get()); |
| 8076 | if (rexRes.isInvalid()) |
| 8077 | return InvalidOperands(Loc, lex, rex); |
| 8078 | rex = move(rexRes); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8079 | |
Anders Carlsson | 2e7bc11 | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 8080 | // C++ [expr.log.and]p2 |
| 8081 | // C++ [expr.log.or]p2 |
| 8082 | // The result is a bool. |
| 8083 | return Context.BoolTy; |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 8084 | } |
| 8085 | |
Fariborz Jahanian | 8e1555c | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 8086 | /// IsReadonlyProperty - Verify that otherwise a valid l-value expression |
| 8087 | /// is a read-only property; return true if so. A readonly property expression |
| 8088 | /// depends on various declarations and thus must be treated specially. |
| 8089 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8090 | static bool IsReadonlyProperty(Expr *E, Sema &S) { |
Fariborz Jahanian | 8e1555c | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 8091 | if (E->getStmtClass() == Expr::ObjCPropertyRefExprClass) { |
| 8092 | const ObjCPropertyRefExpr* PropExpr = cast<ObjCPropertyRefExpr>(E); |
John McCall | b7bd14f | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 8093 | if (PropExpr->isImplicitProperty()) return false; |
| 8094 | |
| 8095 | ObjCPropertyDecl *PDecl = PropExpr->getExplicitProperty(); |
| 8096 | QualType BaseType = PropExpr->isSuperReceiver() ? |
| 8097 | PropExpr->getSuperReceiverType() : |
Fariborz Jahanian | 681c075 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 8098 | PropExpr->getBase()->getType(); |
| 8099 | |
John McCall | b7bd14f | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 8100 | if (const ObjCObjectPointerType *OPT = |
| 8101 | BaseType->getAsObjCInterfacePointerType()) |
| 8102 | if (ObjCInterfaceDecl *IFace = OPT->getInterfaceDecl()) |
| 8103 | if (S.isPropertyReadonly(PDecl, IFace)) |
| 8104 | return true; |
Fariborz Jahanian | 8e1555c | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 8105 | } |
| 8106 | return false; |
| 8107 | } |
| 8108 | |
Fariborz Jahanian | b24b568 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 8109 | static bool IsConstProperty(Expr *E, Sema &S) { |
| 8110 | if (E->getStmtClass() == Expr::ObjCPropertyRefExprClass) { |
| 8111 | const ObjCPropertyRefExpr* PropExpr = cast<ObjCPropertyRefExpr>(E); |
| 8112 | if (PropExpr->isImplicitProperty()) return false; |
| 8113 | |
| 8114 | ObjCPropertyDecl *PDecl = PropExpr->getExplicitProperty(); |
| 8115 | QualType T = PDecl->getType(); |
| 8116 | if (T->isReferenceType()) |
Fariborz Jahanian | 20688cc | 2011-03-30 16:59:30 +0000 | [diff] [blame] | 8117 | T = T->getAs<ReferenceType>()->getPointeeType(); |
Fariborz Jahanian | b24b568 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 8118 | CanQualType CT = S.Context.getCanonicalType(T); |
| 8119 | return CT.isConstQualified(); |
| 8120 | } |
| 8121 | return false; |
| 8122 | } |
| 8123 | |
Fariborz Jahanian | 071caef | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 8124 | static bool IsReadonlyMessage(Expr *E, Sema &S) { |
| 8125 | if (E->getStmtClass() != Expr::MemberExprClass) |
| 8126 | return false; |
| 8127 | const MemberExpr *ME = cast<MemberExpr>(E); |
| 8128 | NamedDecl *Member = ME->getMemberDecl(); |
| 8129 | if (isa<FieldDecl>(Member)) { |
| 8130 | Expr *Base = ME->getBase()->IgnoreParenImpCasts(); |
| 8131 | if (Base->getStmtClass() != Expr::ObjCMessageExprClass) |
| 8132 | return false; |
| 8133 | return cast<ObjCMessageExpr>(Base)->getMethodDecl() != 0; |
| 8134 | } |
| 8135 | return false; |
| 8136 | } |
| 8137 | |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8138 | /// CheckForModifiableLvalue - Verify that E is a modifiable lvalue. If not, |
| 8139 | /// emit an error and return true. If so, return false. |
| 8140 | static bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S) { |
Daniel Dunbar | c2223ab | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 8141 | SourceLocation OrigLoc = Loc; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8142 | Expr::isModifiableLvalueResult IsLV = E->isModifiableLvalue(S.Context, |
Daniel Dunbar | c2223ab | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 8143 | &Loc); |
Fariborz Jahanian | 8e1555c | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 8144 | if (IsLV == Expr::MLV_Valid && IsReadonlyProperty(E, S)) |
| 8145 | IsLV = Expr::MLV_ReadonlyProperty; |
Fariborz Jahanian | b24b568 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 8146 | else if (Expr::MLV_ConstQualified && IsConstProperty(E, S)) |
| 8147 | IsLV = Expr::MLV_Valid; |
Fariborz Jahanian | 071caef | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 8148 | else if (IsLV == Expr::MLV_ClassTemporary && IsReadonlyMessage(E, S)) |
| 8149 | IsLV = Expr::MLV_InvalidMessageExpression; |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8150 | if (IsLV == Expr::MLV_Valid) |
| 8151 | return false; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8152 | |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8153 | unsigned Diag = 0; |
| 8154 | bool NeedType = false; |
| 8155 | switch (IsLV) { // C99 6.5.16p2 |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8156 | case Expr::MLV_ConstQualified: |
| 8157 | Diag = diag::err_typecheck_assign_const; |
| 8158 | |
| 8159 | // In ARC, use some specialized diagnostics for the times when we |
| 8160 | // infer const. |
| 8161 | if (S.getLangOptions().ObjCAutoRefCount) { |
| 8162 | DeclRefExpr *declRef = dyn_cast<DeclRefExpr>(E->IgnoreParenCasts()); |
| 8163 | if (declRef && isa<VarDecl>(declRef->getDecl())) { |
| 8164 | VarDecl *var = cast<VarDecl>(declRef->getDecl()); |
| 8165 | |
| 8166 | // If the variable wasn't written with 'const', there are some |
| 8167 | // cases where we infer const anyway: |
| 8168 | // - self |
| 8169 | // - fast enumeration variables |
| 8170 | if (!var->getTypeSourceInfo() || |
| 8171 | !var->getTypeSourceInfo()->getType().isConstQualified()) { |
| 8172 | ObjCMethodDecl *method = S.getCurMethodDecl(); |
| 8173 | if (method && var == method->getSelfDecl()) |
| 8174 | Diag = diag::err_typecheck_arr_assign_self; |
| 8175 | else if (var->getType().getObjCLifetime() |
| 8176 | == Qualifiers::OCL_ExplicitNone) |
| 8177 | Diag = diag::err_typecheck_arr_assign_enumeration; |
| 8178 | SourceRange Assign; |
| 8179 | if (Loc != OrigLoc) |
| 8180 | Assign = SourceRange(OrigLoc, OrigLoc); |
| 8181 | S.Diag(Loc, Diag) << E->getSourceRange() << Assign; |
| 8182 | // We need to preserve the AST regardless, so migration tool |
| 8183 | // can do its job. |
| 8184 | return false; |
| 8185 | } |
| 8186 | } |
| 8187 | } |
| 8188 | |
| 8189 | break; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8190 | case Expr::MLV_ArrayType: |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8191 | Diag = diag::err_typecheck_array_not_modifiable_lvalue; |
| 8192 | NeedType = true; |
| 8193 | break; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8194 | case Expr::MLV_NotObjectType: |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8195 | Diag = diag::err_typecheck_non_object_not_modifiable_lvalue; |
| 8196 | NeedType = true; |
| 8197 | break; |
Chris Lattner | 9b3bbe9 | 2008-11-17 19:51:54 +0000 | [diff] [blame] | 8198 | case Expr::MLV_LValueCast: |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8199 | Diag = diag::err_typecheck_lvalue_casts_not_supported; |
| 8200 | break; |
Douglas Gregor | b154fdc | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 8201 | case Expr::MLV_Valid: |
| 8202 | llvm_unreachable("did not take early return for MLV_Valid"); |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8203 | case Expr::MLV_InvalidExpression: |
Douglas Gregor | b154fdc | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 8204 | case Expr::MLV_MemberFunction: |
| 8205 | case Expr::MLV_ClassTemporary: |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8206 | Diag = diag::err_typecheck_expression_not_modifiable_lvalue; |
| 8207 | break; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8208 | case Expr::MLV_IncompleteType: |
| 8209 | case Expr::MLV_IncompleteVoidType: |
Douglas Gregor | ed0cfbd | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 8210 | return S.RequireCompleteType(Loc, E->getType(), |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 8211 | S.PDiag(diag::err_typecheck_incomplete_type_not_modifiable_lvalue) |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 8212 | << E->getSourceRange()); |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8213 | case Expr::MLV_DuplicateVectorComponents: |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8214 | Diag = diag::err_typecheck_duplicate_vector_components_not_mlvalue; |
| 8215 | break; |
Steve Naroff | ba756cb | 2008-09-26 14:41:28 +0000 | [diff] [blame] | 8216 | case Expr::MLV_NotBlockQualified: |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8217 | Diag = diag::err_block_decl_ref_not_modifiable_lvalue; |
| 8218 | break; |
Fariborz Jahanian | 8a1810f | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 8219 | case Expr::MLV_ReadonlyProperty: |
| 8220 | Diag = diag::error_readonly_property_assignment; |
| 8221 | break; |
Fariborz Jahanian | 5118c41 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 8222 | case Expr::MLV_NoSetterProperty: |
| 8223 | Diag = diag::error_nosetter_property_assignment; |
| 8224 | break; |
Fariborz Jahanian | 071caef | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 8225 | case Expr::MLV_InvalidMessageExpression: |
| 8226 | Diag = diag::error_readonly_message_assignment; |
| 8227 | break; |
Fariborz Jahanian | e8d2890 | 2009-12-15 23:59:41 +0000 | [diff] [blame] | 8228 | case Expr::MLV_SubObjCPropertySetting: |
| 8229 | Diag = diag::error_no_subobject_property_setting; |
| 8230 | break; |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 8231 | } |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 8232 | |
Daniel Dunbar | c2223ab | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 8233 | SourceRange Assign; |
| 8234 | if (Loc != OrigLoc) |
| 8235 | Assign = SourceRange(OrigLoc, OrigLoc); |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8236 | if (NeedType) |
Daniel Dunbar | c2223ab | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 8237 | S.Diag(Loc, Diag) << E->getType() << E->getSourceRange() << Assign; |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8238 | else |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8239 | S.Diag(Loc, Diag) << E->getSourceRange() << Assign; |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8240 | return true; |
| 8241 | } |
| 8242 | |
| 8243 | |
| 8244 | |
| 8245 | // C99 6.5.16.1 |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8246 | QualType Sema::CheckAssignmentOperands(Expr *LHS, ExprResult &RHS, |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 8247 | SourceLocation Loc, |
| 8248 | QualType CompoundType) { |
| 8249 | // Verify that LHS is a modifiable lvalue, and emit error if not. |
| 8250 | if (CheckForModifiableLvalue(LHS, Loc, *this)) |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 8251 | return QualType(); |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 8252 | |
| 8253 | QualType LHSType = LHS->getType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8254 | QualType RHSType = CompoundType.isNull() ? RHS.get()->getType() : CompoundType; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8255 | AssignConvertType ConvTy; |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 8256 | if (CompoundType.isNull()) { |
Fariborz Jahanian | be21aa3 | 2010-06-07 22:02:01 +0000 | [diff] [blame] | 8257 | QualType LHSTy(LHSType); |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 8258 | // Simple assignment "x = y". |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8259 | if (LHS->getObjectKind() == OK_ObjCProperty) { |
| 8260 | ExprResult LHSResult = Owned(LHS); |
| 8261 | ConvertPropertyForLValue(LHSResult, RHS, LHSTy); |
| 8262 | if (LHSResult.isInvalid()) |
| 8263 | return QualType(); |
| 8264 | LHS = LHSResult.take(); |
| 8265 | } |
Fariborz Jahanian | be21aa3 | 2010-06-07 22:02:01 +0000 | [diff] [blame] | 8266 | ConvTy = CheckSingleAssignmentConstraints(LHSTy, RHS); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8267 | if (RHS.isInvalid()) |
| 8268 | return QualType(); |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 8269 | // Special case of NSObject attributes on c-style pointer types. |
| 8270 | if (ConvTy == IncompatiblePointer && |
| 8271 | ((Context.isObjCNSObjectType(LHSType) && |
Steve Naroff | 79d1215 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 8272 | RHSType->isObjCObjectPointerType()) || |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 8273 | (Context.isObjCNSObjectType(RHSType) && |
Steve Naroff | 79d1215 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 8274 | LHSType->isObjCObjectPointerType()))) |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 8275 | ConvTy = Compatible; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8276 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8277 | if (ConvTy == Compatible && |
| 8278 | getLangOptions().ObjCNonFragileABI && |
| 8279 | LHSType->isObjCObjectType()) |
| 8280 | Diag(Loc, diag::err_assignment_requires_nonfragile_object) |
| 8281 | << LHSType; |
| 8282 | |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 8283 | // If the RHS is a unary plus or minus, check to see if they = and + are |
| 8284 | // right next to each other. If so, the user may have typo'd "x =+ 4" |
| 8285 | // instead of "x += 4". |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8286 | Expr *RHSCheck = RHS.get(); |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 8287 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RHSCheck)) |
| 8288 | RHSCheck = ICE->getSubExpr(); |
| 8289 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(RHSCheck)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8290 | if ((UO->getOpcode() == UO_Plus || |
| 8291 | UO->getOpcode() == UO_Minus) && |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 8292 | Loc.isFileID() && UO->getOperatorLoc().isFileID() && |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 8293 | // Only if the two operators are exactly adjacent. |
Chris Lattner | 36c39c9 | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 8294 | Loc.getFileLocWithOffset(1) == UO->getOperatorLoc() && |
| 8295 | // And there is a space or other character before the subexpr of the |
| 8296 | // unary +/-. We don't want to warn on "x=-1". |
Chris Lattner | ed9f14c | 2009-03-09 07:11:10 +0000 | [diff] [blame] | 8297 | Loc.getFileLocWithOffset(2) != UO->getSubExpr()->getLocStart() && |
| 8298 | UO->getSubExpr()->getLocStart().isFileID()) { |
Chris Lattner | 29e812b | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 8299 | Diag(Loc, diag::warn_not_compound_assign) |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8300 | << (UO->getOpcode() == UO_Plus ? "+" : "-") |
Chris Lattner | 29e812b | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 8301 | << SourceRange(UO->getOperatorLoc(), UO->getOperatorLoc()); |
Chris Lattner | 36c39c9 | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 8302 | } |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 8303 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8304 | |
| 8305 | if (ConvTy == Compatible) { |
| 8306 | if (LHSType.getObjCLifetime() == Qualifiers::OCL_Strong) |
| 8307 | checkRetainCycles(LHS, RHS.get()); |
| 8308 | else |
| 8309 | checkUnsafeAssigns(Loc, LHSType, RHS.get()); |
| 8310 | } |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 8311 | } else { |
| 8312 | // Compound assignment "x += y" |
Douglas Gregor | c03a108 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 8313 | ConvTy = CheckAssignmentConstraints(Loc, LHSType, RHSType); |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 8314 | } |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8315 | |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 8316 | if (DiagnoseAssignmentResult(ConvTy, Loc, LHSType, RHSType, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8317 | RHS.get(), AA_Assigning)) |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8318 | return QualType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8319 | |
Argyrios Kyrtzidis | a9b630e | 2011-04-26 17:41:22 +0000 | [diff] [blame] | 8320 | CheckForNullPointerDereference(*this, LHS); |
Ted Kremenek | 64699be | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 8321 | // Check for trivial buffer overflows. |
Ted Kremenek | df26df7 | 2011-03-01 18:41:00 +0000 | [diff] [blame] | 8322 | CheckArrayAccess(LHS->IgnoreParenCasts()); |
Ted Kremenek | 64699be | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 8323 | |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 8324 | // C99 6.5.16p3: The type of an assignment expression is the type of the |
| 8325 | // left operand unless the left operand has qualified type, in which case |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8326 | // it is the unqualified version of the type of the left operand. |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 8327 | // C99 6.5.16.1p2: In simple assignment, the value of the right operand |
| 8328 | // is converted to the type of the assignment expression (above). |
Chris Lattner | f17bd42 | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 8329 | // 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] | 8330 | // operand. |
John McCall | 01cbf2d | 2010-10-12 02:19:57 +0000 | [diff] [blame] | 8331 | return (getLangOptions().CPlusPlus |
| 8332 | ? LHSType : LHSType.getUnqualifiedType()); |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 8333 | } |
| 8334 | |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 8335 | // C99 6.5.17 |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8336 | static QualType CheckCommaOperands(Sema &S, ExprResult &LHS, ExprResult &RHS, |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8337 | SourceLocation Loc) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8338 | S.DiagnoseUnusedExprResult(LHS.get()); |
Argyrios Kyrtzidis | 639ffb0 | 2010-06-30 10:53:14 +0000 | [diff] [blame] | 8339 | |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8340 | LHS = S.CheckPlaceholderExpr(LHS.take()); |
| 8341 | RHS = S.CheckPlaceholderExpr(RHS.take()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8342 | if (LHS.isInvalid() || RHS.isInvalid()) |
Douglas Gregor | 0124e9b | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 8343 | return QualType(); |
| 8344 | |
John McCall | 73d3618 | 2010-10-12 07:14:40 +0000 | [diff] [blame] | 8345 | // C's comma performs lvalue conversion (C99 6.3.2.1) on both its |
| 8346 | // operands, but not unary promotions. |
| 8347 | // 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] | 8348 | |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8349 | // So we treat the LHS as a ignored value, and in C++ we allow the |
| 8350 | // containing site to determine what should be done with the RHS. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8351 | LHS = S.IgnoredValueConversions(LHS.take()); |
| 8352 | if (LHS.isInvalid()) |
| 8353 | return QualType(); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8354 | |
| 8355 | if (!S.getLangOptions().CPlusPlus) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8356 | RHS = S.DefaultFunctionArrayLvalueConversion(RHS.take()); |
| 8357 | if (RHS.isInvalid()) |
| 8358 | return QualType(); |
| 8359 | if (!RHS.get()->getType()->isVoidType()) |
| 8360 | S.RequireCompleteType(Loc, RHS.get()->getType(), diag::err_incomplete_type); |
John McCall | 73d3618 | 2010-10-12 07:14:40 +0000 | [diff] [blame] | 8361 | } |
Eli Friedman | ba961a9 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 8362 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8363 | return RHS.get()->getType(); |
Steve Naroff | 95af013 | 2007-03-30 23:47:58 +0000 | [diff] [blame] | 8364 | } |
| 8365 | |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 8366 | /// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine |
| 8367 | /// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8368 | static QualType CheckIncrementDecrementOperand(Sema &S, Expr *Op, |
| 8369 | ExprValueKind &VK, |
| 8370 | SourceLocation OpLoc, |
| 8371 | bool isInc, bool isPrefix) { |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8372 | if (Op->isTypeDependent()) |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8373 | return S.Context.DependentTy; |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8374 | |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 8375 | QualType ResType = Op->getType(); |
| 8376 | assert(!ResType.isNull() && "no type for increment/decrement expression"); |
Steve Naroff | d50c88e | 2007-04-05 21:15:20 +0000 | [diff] [blame] | 8377 | |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8378 | if (S.getLangOptions().CPlusPlus && ResType->isBooleanType()) { |
Sebastian Redl | e10c2c3 | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 8379 | // Decrement of bool is not allowed. |
| 8380 | if (!isInc) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8381 | S.Diag(OpLoc, diag::err_decrement_bool) << Op->getSourceRange(); |
Sebastian Redl | e10c2c3 | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 8382 | return QualType(); |
| 8383 | } |
| 8384 | // Increment of bool sets it to true, but is deprecated. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8385 | S.Diag(OpLoc, diag::warn_increment_bool) << Op->getSourceRange(); |
Sebastian Redl | e10c2c3 | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 8386 | } else if (ResType->isRealType()) { |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 8387 | // OK! |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 8388 | } else if (ResType->isAnyPointerType()) { |
| 8389 | QualType PointeeTy = ResType->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8390 | |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 8391 | // C99 6.5.2.4p2, 6.5.6p2 |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 8392 | if (PointeeTy->isVoidType()) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8393 | if (S.getLangOptions().CPlusPlus) { |
| 8394 | S.Diag(OpLoc, diag::err_typecheck_pointer_arith_void_type) |
Douglas Gregor | f6cd928 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 8395 | << Op->getSourceRange(); |
| 8396 | return QualType(); |
| 8397 | } |
| 8398 | |
| 8399 | // Pointer to void is a GNU extension in C. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8400 | S.Diag(OpLoc, diag::ext_gnu_void_ptr) << Op->getSourceRange(); |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 8401 | } else if (PointeeTy->isFunctionType()) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8402 | if (S.getLangOptions().CPlusPlus) { |
| 8403 | S.Diag(OpLoc, diag::err_typecheck_pointer_arith_function_type) |
Douglas Gregor | f6cd928 | 2009-01-23 00:36:41 +0000 | [diff] [blame] | 8404 | << Op->getType() << Op->getSourceRange(); |
| 8405 | return QualType(); |
| 8406 | } |
| 8407 | |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8408 | S.Diag(OpLoc, diag::ext_gnu_ptr_func_arith) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 8409 | << ResType << Op->getSourceRange(); |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8410 | } else if (S.RequireCompleteType(OpLoc, PointeeTy, |
| 8411 | S.PDiag(diag::err_typecheck_arithmetic_incomplete_type) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8412 | << Op->getSourceRange() |
Anders Carlsson | 029fc69 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 8413 | << ResType)) |
Douglas Gregor | dd430f7 | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 8414 | return QualType(); |
Fariborz Jahanian | ca75db7 | 2009-07-16 17:59:14 +0000 | [diff] [blame] | 8415 | // Diagnose bad cases where we step over interface counts. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8416 | else if (PointeeTy->isObjCObjectType() && S.LangOpts.ObjCNonFragileABI) { |
| 8417 | S.Diag(OpLoc, diag::err_arithmetic_nonfragile_interface) |
Fariborz Jahanian | ca75db7 | 2009-07-16 17:59:14 +0000 | [diff] [blame] | 8418 | << PointeeTy << Op->getSourceRange(); |
| 8419 | return QualType(); |
| 8420 | } |
Eli Friedman | 090addd | 2010-01-03 00:20:48 +0000 | [diff] [blame] | 8421 | } else if (ResType->isAnyComplexType()) { |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 8422 | // C99 does not support ++/-- on complex types, we allow as an extension. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8423 | S.Diag(OpLoc, diag::ext_integer_increment_complex) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 8424 | << ResType << Op->getSourceRange(); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8425 | } else if (ResType->isPlaceholderType()) { |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8426 | ExprResult PR = S.CheckPlaceholderExpr(Op); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8427 | if (PR.isInvalid()) return QualType(); |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8428 | return CheckIncrementDecrementOperand(S, PR.take(), VK, OpLoc, |
| 8429 | isInc, isPrefix); |
Anton Yartsev | 85129b8 | 2011-02-07 02:17:30 +0000 | [diff] [blame] | 8430 | } else if (S.getLangOptions().AltiVec && ResType->isVectorType()) { |
| 8431 | // OK! ( C/C++ Language Extensions for CBEA(Version 2.6) 10.3 ) |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 8432 | } else { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8433 | S.Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement) |
Douglas Gregor | 906db8a | 2009-12-15 16:44:32 +0000 | [diff] [blame] | 8434 | << ResType << int(isInc) << Op->getSourceRange(); |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 8435 | return QualType(); |
Steve Naroff | 46ba1eb | 2007-04-03 23:13:13 +0000 | [diff] [blame] | 8436 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8437 | // 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] | 8438 | // Now make sure the operand is a modifiable lvalue. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8439 | if (CheckForModifiableLvalue(Op, OpLoc, S)) |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 8440 | return QualType(); |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 8441 | // In C++, a prefix increment is the same type as the operand. Otherwise |
| 8442 | // (in C or with postfix), the increment is the unqualified type of the |
| 8443 | // operand. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8444 | if (isPrefix && S.getLangOptions().CPlusPlus) { |
| 8445 | VK = VK_LValue; |
| 8446 | return ResType; |
| 8447 | } else { |
| 8448 | VK = VK_RValue; |
| 8449 | return ResType.getUnqualifiedType(); |
| 8450 | } |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 8451 | } |
| 8452 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8453 | ExprResult Sema::ConvertPropertyForRValue(Expr *E) { |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8454 | assert(E->getValueKind() == VK_LValue && |
| 8455 | E->getObjectKind() == OK_ObjCProperty); |
| 8456 | const ObjCPropertyRefExpr *PRE = E->getObjCProperty(); |
| 8457 | |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 8458 | QualType T = E->getType(); |
| 8459 | QualType ReceiverType; |
| 8460 | if (PRE->isObjectReceiver()) |
| 8461 | ReceiverType = PRE->getBase()->getType(); |
| 8462 | else if (PRE->isSuperReceiver()) |
| 8463 | ReceiverType = PRE->getSuperReceiverType(); |
| 8464 | else |
| 8465 | ReceiverType = Context.getObjCInterfaceType(PRE->getClassReceiver()); |
| 8466 | |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8467 | ExprValueKind VK = VK_RValue; |
| 8468 | if (PRE->isImplicitProperty()) { |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 8469 | if (ObjCMethodDecl *GetterMethod = |
Fariborz Jahanian | 0f0b302 | 2010-12-22 19:46:35 +0000 | [diff] [blame] | 8470 | PRE->getImplicitPropertyGetter()) { |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 8471 | T = getMessageSendResultType(ReceiverType, GetterMethod, |
| 8472 | PRE->isClassReceiver(), |
| 8473 | PRE->isSuperReceiver()); |
| 8474 | VK = Expr::getValueKindForType(GetterMethod->getResultType()); |
Fariborz Jahanian | 0f0b302 | 2010-12-22 19:46:35 +0000 | [diff] [blame] | 8475 | } |
| 8476 | else { |
| 8477 | Diag(PRE->getLocation(), diag::err_getter_not_found) |
| 8478 | << PRE->getBase()->getType(); |
| 8479 | } |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8480 | } |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 8481 | |
| 8482 | E = ImplicitCastExpr::Create(Context, T, CK_GetObjCProperty, |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8483 | E, 0, VK); |
John McCall | 4f26cd8 | 2010-12-10 01:49:45 +0000 | [diff] [blame] | 8484 | |
| 8485 | ExprResult Result = MaybeBindToTemporary(E); |
| 8486 | if (!Result.isInvalid()) |
| 8487 | E = Result.take(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8488 | |
| 8489 | return Owned(E); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8490 | } |
| 8491 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8492 | void Sema::ConvertPropertyForLValue(ExprResult &LHS, ExprResult &RHS, QualType &LHSTy) { |
| 8493 | assert(LHS.get()->getValueKind() == VK_LValue && |
| 8494 | LHS.get()->getObjectKind() == OK_ObjCProperty); |
| 8495 | const ObjCPropertyRefExpr *PropRef = LHS.get()->getObjCProperty(); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8496 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8497 | bool Consumed = false; |
| 8498 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8499 | if (PropRef->isImplicitProperty()) { |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8500 | // If using property-dot syntax notation for assignment, and there is a |
| 8501 | // setter, RHS expression is being passed to the setter argument. So, |
| 8502 | // type conversion (and comparison) is RHS to setter's argument type. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8503 | if (const ObjCMethodDecl *SetterMD = PropRef->getImplicitPropertySetter()) { |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8504 | ObjCMethodDecl::param_iterator P = SetterMD->param_begin(); |
| 8505 | LHSTy = (*P)->getType(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8506 | Consumed = (getLangOptions().ObjCAutoRefCount && |
| 8507 | (*P)->hasAttr<NSConsumedAttr>()); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8508 | |
| 8509 | // Otherwise, if the getter returns an l-value, just call that. |
| 8510 | } else { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8511 | QualType Result = PropRef->getImplicitPropertyGetter()->getResultType(); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8512 | ExprValueKind VK = Expr::getValueKindForType(Result); |
| 8513 | if (VK == VK_LValue) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8514 | LHS = ImplicitCastExpr::Create(Context, LHS.get()->getType(), |
| 8515 | CK_GetObjCProperty, LHS.take(), 0, VK); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8516 | return; |
John McCall | b7bd14f | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 8517 | } |
Fariborz Jahanian | 805b74e | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 8518 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8519 | } else if (getLangOptions().ObjCAutoRefCount) { |
| 8520 | const ObjCMethodDecl *setter |
| 8521 | = PropRef->getExplicitProperty()->getSetterMethodDecl(); |
| 8522 | if (setter) { |
| 8523 | ObjCMethodDecl::param_iterator P = setter->param_begin(); |
| 8524 | LHSTy = (*P)->getType(); |
| 8525 | Consumed = (*P)->hasAttr<NSConsumedAttr>(); |
| 8526 | } |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8527 | } |
| 8528 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8529 | if ((getLangOptions().CPlusPlus && LHSTy->isRecordType()) || |
| 8530 | getLangOptions().ObjCAutoRefCount) { |
Fariborz Jahanian | 805b74e | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 8531 | InitializedEntity Entity = |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8532 | InitializedEntity::InitializeParameter(Context, LHSTy, Consumed); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8533 | ExprResult ArgE = PerformCopyInitialization(Entity, SourceLocation(), RHS); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8534 | if (!ArgE.isInvalid()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8535 | RHS = ArgE; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8536 | if (getLangOptions().ObjCAutoRefCount && !PropRef->isSuperReceiver()) |
| 8537 | checkRetainCycles(const_cast<Expr*>(PropRef->getBase()), RHS.get()); |
| 8538 | } |
Fariborz Jahanian | 805b74e | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 8539 | } |
| 8540 | } |
| 8541 | |
| 8542 | |
Anders Carlsson | 806700f | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 8543 | /// getPrimaryDecl - Helper function for CheckAddressOfOperand(). |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 8544 | /// This routine allows us to typecheck complex/recursive expressions |
Daniel Dunbar | b692ef4 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 8545 | /// where the declaration is needed for type checking. We only need to |
| 8546 | /// handle cases when the expression references a function designator |
| 8547 | /// or is an lvalue. Here are some examples: |
| 8548 | /// - &(x) => x |
| 8549 | /// - &*****f => f for f a function designator. |
| 8550 | /// - &s.xx => s |
| 8551 | /// - &s.zz[1].yy -> s, if zz is an array |
| 8552 | /// - *(x + 1) -> x, if x is an array |
| 8553 | /// - &"123"[2] -> 0 |
| 8554 | /// - & __real__ x -> x |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 8555 | static ValueDecl *getPrimaryDecl(Expr *E) { |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 8556 | switch (E->getStmtClass()) { |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 8557 | case Stmt::DeclRefExprClass: |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 8558 | return cast<DeclRefExpr>(E)->getDecl(); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 8559 | case Stmt::MemberExprClass: |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8560 | // If this is an arrow operator, the address is an offset from |
| 8561 | // the base's value, so the object the base refers to is |
| 8562 | // irrelevant. |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 8563 | if (cast<MemberExpr>(E)->isArrow()) |
Chris Lattner | 48d5284 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 8564 | return 0; |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8565 | // Otherwise, the expression refers to a part of the base |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 8566 | return getPrimaryDecl(cast<MemberExpr>(E)->getBase()); |
Anders Carlsson | 806700f | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 8567 | case Stmt::ArraySubscriptExprClass: { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 8568 | // FIXME: This code shouldn't be necessary! We should catch the implicit |
| 8569 | // promotion of register arrays earlier. |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8570 | Expr* Base = cast<ArraySubscriptExpr>(E)->getBase(); |
| 8571 | if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Base)) { |
| 8572 | if (ICE->getSubExpr()->getType()->isArrayType()) |
| 8573 | return getPrimaryDecl(ICE->getSubExpr()); |
| 8574 | } |
| 8575 | return 0; |
Anders Carlsson | 806700f | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 8576 | } |
Daniel Dunbar | b692ef4 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 8577 | case Stmt::UnaryOperatorClass: { |
| 8578 | UnaryOperator *UO = cast<UnaryOperator>(E); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8579 | |
Daniel Dunbar | b692ef4 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 8580 | switch(UO->getOpcode()) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8581 | case UO_Real: |
| 8582 | case UO_Imag: |
| 8583 | case UO_Extension: |
Daniel Dunbar | b692ef4 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 8584 | return getPrimaryDecl(UO->getSubExpr()); |
| 8585 | default: |
| 8586 | return 0; |
| 8587 | } |
| 8588 | } |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 8589 | case Stmt::ParenExprClass: |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 8590 | return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr()); |
Chris Lattner | 48d5284 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 8591 | case Stmt::ImplicitCastExprClass: |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8592 | // If the result of an implicit cast is an l-value, we care about |
| 8593 | // the sub-expression; otherwise, the result here doesn't matter. |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 8594 | return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr()); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 8595 | default: |
| 8596 | return 0; |
| 8597 | } |
| 8598 | } |
| 8599 | |
| 8600 | /// CheckAddressOfOperand - The operand of & must be either a function |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8601 | /// 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] | 8602 | /// 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] | 8603 | /// Note: The usual conversions are *not* applied to the operand of the & |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 8604 | /// 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] | 8605 | /// 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] | 8606 | /// we allow the '&' but retain the overloaded-function type. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8607 | static QualType CheckAddressOfOperand(Sema &S, Expr *OrigOp, |
| 8608 | SourceLocation OpLoc) { |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8609 | if (OrigOp->isTypeDependent()) |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8610 | return S.Context.DependentTy; |
| 8611 | if (OrigOp->getType() == S.Context.OverloadTy) |
| 8612 | return S.Context.OverloadTy; |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 8613 | if (OrigOp->getType() == S.Context.UnknownAnyTy) |
| 8614 | return S.Context.UnknownAnyTy; |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 8615 | if (OrigOp->getType() == S.Context.BoundMemberTy) { |
| 8616 | S.Diag(OpLoc, diag::err_invalid_form_pointer_member_function) |
| 8617 | << OrigOp->getSourceRange(); |
| 8618 | return QualType(); |
| 8619 | } |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8620 | |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 8621 | assert(!OrigOp->getType()->isPlaceholderType()); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8622 | |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8623 | // Make sure to ignore parentheses in subsequent checks |
| 8624 | Expr *op = OrigOp->IgnoreParens(); |
Douglas Gregor | 19b8c4f | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 8625 | |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8626 | if (S.getLangOptions().C99) { |
Steve Naroff | 826e91a | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 8627 | // Implement C99-only parts of addressof rules. |
| 8628 | if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8629 | if (uOp->getOpcode() == UO_Deref) |
Steve Naroff | 826e91a | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 8630 | // Per C99 6.5.3.2, the address of a deref always returns a valid result |
| 8631 | // (assuming the deref expression is valid). |
| 8632 | return uOp->getSubExpr()->getType(); |
| 8633 | } |
| 8634 | // Technically, there should be a check for array subscript |
| 8635 | // expressions here, but the result of one is always an lvalue anyway. |
| 8636 | } |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 8637 | ValueDecl *dcl = getPrimaryDecl(op); |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 8638 | Expr::LValueClassification lval = op->ClassifyLValue(S.Context); |
Nuno Lopes | 17f345f | 2008-12-16 22:59:47 +0000 | [diff] [blame] | 8639 | |
Fariborz Jahanian | 071caef | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 8640 | if (lval == Expr::LV_ClassTemporary) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8641 | bool sfinae = S.isSFINAEContext(); |
| 8642 | S.Diag(OpLoc, sfinae ? diag::err_typecheck_addrof_class_temporary |
| 8643 | : diag::ext_typecheck_addrof_class_temporary) |
Douglas Gregor | b154fdc | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 8644 | << op->getType() << op->getSourceRange(); |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8645 | if (sfinae) |
Douglas Gregor | b154fdc | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 8646 | return QualType(); |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8647 | } else if (isa<ObjCSelectorExpr>(op)) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8648 | return S.Context.getPointerType(op->getType()); |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8649 | } else if (lval == Expr::LV_MemberFunction) { |
| 8650 | // If it's an instance method, make a member pointer. |
| 8651 | // The expression must have exactly the form &A::foo. |
| 8652 | |
| 8653 | // If the underlying expression isn't a decl ref, give up. |
| 8654 | if (!isa<DeclRefExpr>(op)) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8655 | S.Diag(OpLoc, diag::err_invalid_form_pointer_member_function) |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8656 | << OrigOp->getSourceRange(); |
| 8657 | return QualType(); |
| 8658 | } |
| 8659 | DeclRefExpr *DRE = cast<DeclRefExpr>(op); |
| 8660 | CXXMethodDecl *MD = cast<CXXMethodDecl>(DRE->getDecl()); |
| 8661 | |
| 8662 | // The id-expression was parenthesized. |
| 8663 | if (OrigOp != DRE) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8664 | S.Diag(OpLoc, diag::err_parens_pointer_member_function) |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8665 | << OrigOp->getSourceRange(); |
| 8666 | |
| 8667 | // The method was named without a qualifier. |
| 8668 | } else if (!DRE->getQualifier()) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8669 | S.Diag(OpLoc, diag::err_unqualified_pointer_member_function) |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8670 | << op->getSourceRange(); |
| 8671 | } |
| 8672 | |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8673 | return S.Context.getMemberPointerType(op->getType(), |
| 8674 | S.Context.getTypeDeclType(MD->getParent()).getTypePtr()); |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 8675 | } else if (lval != Expr::LV_Valid && lval != Expr::LV_IncompleteVoidType) { |
Eli Friedman | ce7f900 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 8676 | // C99 6.5.3.2p1 |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8677 | // The operand must be either an l-value or a function designator |
Eli Friedman | ce7f900 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 8678 | if (!op->getType()->isFunctionType()) { |
Chris Lattner | 48d5284 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 8679 | // FIXME: emit more specific diag... |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8680 | S.Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof) |
Chris Lattner | f490e15 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 8681 | << op->getSourceRange(); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 8682 | return QualType(); |
| 8683 | } |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 8684 | } else if (op->getObjectKind() == OK_BitField) { // C99 6.5.3.2p1 |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8685 | // The operand cannot be a bit-field |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8686 | S.Diag(OpLoc, diag::err_typecheck_address_of) |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8687 | << "bit-field" << op->getSourceRange(); |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 8688 | return QualType(); |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 8689 | } else if (op->getObjectKind() == OK_VectorComponent) { |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8690 | // The operand cannot be an element of a vector |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8691 | S.Diag(OpLoc, diag::err_typecheck_address_of) |
Nate Begeman | a6b47a4 | 2009-02-15 22:45:20 +0000 | [diff] [blame] | 8692 | << "vector element" << op->getSourceRange(); |
Steve Naroff | b96e4ab6 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 8693 | return QualType(); |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 8694 | } else if (op->getObjectKind() == OK_ObjCProperty) { |
Fariborz Jahanian | 385db80 | 2009-07-07 18:50:52 +0000 | [diff] [blame] | 8695 | // cannot take address of a property expression. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8696 | S.Diag(OpLoc, diag::err_typecheck_address_of) |
Fariborz Jahanian | 385db80 | 2009-07-07 18:50:52 +0000 | [diff] [blame] | 8697 | << "property expression" << op->getSourceRange(); |
| 8698 | return QualType(); |
Steve Naroff | b96e4ab6 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 8699 | } else if (dcl) { // C99 6.5.3.2p1 |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8700 | // 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] | 8701 | // with the register storage-class specifier. |
| 8702 | if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { |
Fariborz Jahanian | e0fd5a9 | 2010-08-24 22:21:48 +0000 | [diff] [blame] | 8703 | // in C++ it is not error to take address of a register |
| 8704 | // variable (c++03 7.1.1P3) |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 8705 | if (vd->getStorageClass() == SC_Register && |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8706 | !S.getLangOptions().CPlusPlus) { |
| 8707 | S.Diag(OpLoc, diag::err_typecheck_address_of) |
Chris Lattner | 29e812b | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 8708 | << "register variable" << op->getSourceRange(); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 8709 | return QualType(); |
| 8710 | } |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8711 | } else if (isa<FunctionTemplateDecl>(dcl)) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8712 | return S.Context.OverloadTy; |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 8713 | } else if (isa<FieldDecl>(dcl) || isa<IndirectFieldDecl>(dcl)) { |
Douglas Gregor | 9aa8b55 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 8714 | // Okay: we can take the address of a field. |
Sebastian Redl | 3d3f75a | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 8715 | // Could be a pointer to member, though, if there is an explicit |
| 8716 | // scope qualifier for the class. |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 8717 | if (isa<DeclRefExpr>(op) && cast<DeclRefExpr>(op)->getQualifier()) { |
Sebastian Redl | 3d3f75a | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 8718 | DeclContext *Ctx = dcl->getDeclContext(); |
Anders Carlsson | 0b675f5 | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 8719 | if (Ctx && Ctx->isRecord()) { |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 8720 | if (dcl->getType()->isReferenceType()) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8721 | S.Diag(OpLoc, |
| 8722 | diag::err_cannot_form_pointer_to_member_of_reference_type) |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 8723 | << dcl->getDeclName() << dcl->getType(); |
Anders Carlsson | 0b675f5 | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 8724 | return QualType(); |
| 8725 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8726 | |
Argyrios Kyrtzidis | 8322b42 | 2011-01-31 07:04:29 +0000 | [diff] [blame] | 8727 | while (cast<RecordDecl>(Ctx)->isAnonymousStructOrUnion()) |
| 8728 | Ctx = Ctx->getParent(); |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8729 | return S.Context.getMemberPointerType(op->getType(), |
| 8730 | S.Context.getTypeDeclType(cast<RecordDecl>(Ctx)).getTypePtr()); |
Anders Carlsson | 0b675f5 | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 8731 | } |
Sebastian Redl | 3d3f75a | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 8732 | } |
Anders Carlsson | 5b53576 | 2009-05-16 21:43:42 +0000 | [diff] [blame] | 8733 | } else if (!isa<FunctionDecl>(dcl)) |
Steve Naroff | f633d09 | 2007-04-25 19:01:39 +0000 | [diff] [blame] | 8734 | assert(0 && "Unknown/unexpected decl type"); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 8735 | } |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 8736 | |
Eli Friedman | ce7f900 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 8737 | if (lval == Expr::LV_IncompleteVoidType) { |
| 8738 | // Taking the address of a void variable is technically illegal, but we |
| 8739 | // allow it in cases which are otherwise valid. |
| 8740 | // Example: "extern void x; void* y = &x;". |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8741 | S.Diag(OpLoc, diag::ext_typecheck_addrof_void) << op->getSourceRange(); |
Eli Friedman | ce7f900 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 8742 | } |
| 8743 | |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 8744 | // 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] | 8745 | if (op->getType()->isObjCObjectType()) |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8746 | return S.Context.getObjCObjectPointerType(op->getType()); |
| 8747 | return S.Context.getPointerType(op->getType()); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 8748 | } |
| 8749 | |
Chris Lattner | 9156f1b | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8750 | /// CheckIndirectionOperand - Type check unary indirection (prefix '*'). |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8751 | static QualType CheckIndirectionOperand(Sema &S, Expr *Op, ExprValueKind &VK, |
| 8752 | SourceLocation OpLoc) { |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8753 | if (Op->isTypeDependent()) |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8754 | return S.Context.DependentTy; |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8755 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8756 | ExprResult ConvResult = S.UsualUnaryConversions(Op); |
| 8757 | if (ConvResult.isInvalid()) |
| 8758 | return QualType(); |
| 8759 | Op = ConvResult.take(); |
Chris Lattner | 9156f1b | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8760 | QualType OpTy = Op->getType(); |
| 8761 | QualType Result; |
Argyrios Kyrtzidis | 69a2c92 | 2011-05-02 18:21:19 +0000 | [diff] [blame] | 8762 | |
| 8763 | if (isa<CXXReinterpretCastExpr>(Op)) { |
| 8764 | QualType OpOrigType = Op->IgnoreParenCasts()->getType(); |
| 8765 | S.CheckCompatibleReinterpretCast(OpOrigType, OpTy, /*IsDereference*/true, |
| 8766 | Op->getSourceRange()); |
| 8767 | } |
| 8768 | |
Chris Lattner | 9156f1b | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8769 | // Note that per both C89 and C99, indirection is always legal, even if OpTy |
| 8770 | // is an incomplete type or void. It would be possible to warn about |
| 8771 | // dereferencing a void pointer, but it's completely well-defined, and such a |
| 8772 | // warning is unlikely to catch any mistakes. |
| 8773 | if (const PointerType *PT = OpTy->getAs<PointerType>()) |
| 8774 | Result = PT->getPointeeType(); |
| 8775 | else if (const ObjCObjectPointerType *OPT = |
| 8776 | OpTy->getAs<ObjCObjectPointerType>()) |
| 8777 | Result = OPT->getPointeeType(); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8778 | else { |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8779 | ExprResult PR = S.CheckPlaceholderExpr(Op); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8780 | if (PR.isInvalid()) return QualType(); |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8781 | if (PR.take() != Op) |
| 8782 | return CheckIndirectionOperand(S, PR.take(), VK, OpLoc); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8783 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8784 | |
Chris Lattner | 9156f1b | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8785 | if (Result.isNull()) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8786 | S.Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer) |
Chris Lattner | 9156f1b | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8787 | << OpTy << Op->getSourceRange(); |
| 8788 | return QualType(); |
| 8789 | } |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8790 | |
| 8791 | // Dereferences are usually l-values... |
| 8792 | VK = VK_LValue; |
| 8793 | |
| 8794 | // ...except that certain expressions are never l-values in C. |
| 8795 | if (!S.getLangOptions().CPlusPlus && |
| 8796 | IsCForbiddenLValueType(S.Context, Result)) |
| 8797 | VK = VK_RValue; |
Chris Lattner | 9156f1b | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8798 | |
| 8799 | return Result; |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 8800 | } |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 8801 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8802 | static inline BinaryOperatorKind ConvertTokenKindToBinaryOpcode( |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 8803 | tok::TokenKind Kind) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8804 | BinaryOperatorKind Opc; |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 8805 | switch (Kind) { |
| 8806 | default: assert(0 && "Unknown binop!"); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8807 | case tok::periodstar: Opc = BO_PtrMemD; break; |
| 8808 | case tok::arrowstar: Opc = BO_PtrMemI; break; |
| 8809 | case tok::star: Opc = BO_Mul; break; |
| 8810 | case tok::slash: Opc = BO_Div; break; |
| 8811 | case tok::percent: Opc = BO_Rem; break; |
| 8812 | case tok::plus: Opc = BO_Add; break; |
| 8813 | case tok::minus: Opc = BO_Sub; break; |
| 8814 | case tok::lessless: Opc = BO_Shl; break; |
| 8815 | case tok::greatergreater: Opc = BO_Shr; break; |
| 8816 | case tok::lessequal: Opc = BO_LE; break; |
| 8817 | case tok::less: Opc = BO_LT; break; |
| 8818 | case tok::greaterequal: Opc = BO_GE; break; |
| 8819 | case tok::greater: Opc = BO_GT; break; |
| 8820 | case tok::exclaimequal: Opc = BO_NE; break; |
| 8821 | case tok::equalequal: Opc = BO_EQ; break; |
| 8822 | case tok::amp: Opc = BO_And; break; |
| 8823 | case tok::caret: Opc = BO_Xor; break; |
| 8824 | case tok::pipe: Opc = BO_Or; break; |
| 8825 | case tok::ampamp: Opc = BO_LAnd; break; |
| 8826 | case tok::pipepipe: Opc = BO_LOr; break; |
| 8827 | case tok::equal: Opc = BO_Assign; break; |
| 8828 | case tok::starequal: Opc = BO_MulAssign; break; |
| 8829 | case tok::slashequal: Opc = BO_DivAssign; break; |
| 8830 | case tok::percentequal: Opc = BO_RemAssign; break; |
| 8831 | case tok::plusequal: Opc = BO_AddAssign; break; |
| 8832 | case tok::minusequal: Opc = BO_SubAssign; break; |
| 8833 | case tok::lesslessequal: Opc = BO_ShlAssign; break; |
| 8834 | case tok::greatergreaterequal: Opc = BO_ShrAssign; break; |
| 8835 | case tok::ampequal: Opc = BO_AndAssign; break; |
| 8836 | case tok::caretequal: Opc = BO_XorAssign; break; |
| 8837 | case tok::pipeequal: Opc = BO_OrAssign; break; |
| 8838 | case tok::comma: Opc = BO_Comma; break; |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 8839 | } |
| 8840 | return Opc; |
| 8841 | } |
| 8842 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8843 | static inline UnaryOperatorKind ConvertTokenKindToUnaryOpcode( |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 8844 | tok::TokenKind Kind) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8845 | UnaryOperatorKind Opc; |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 8846 | switch (Kind) { |
| 8847 | default: assert(0 && "Unknown unary op!"); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8848 | case tok::plusplus: Opc = UO_PreInc; break; |
| 8849 | case tok::minusminus: Opc = UO_PreDec; break; |
| 8850 | case tok::amp: Opc = UO_AddrOf; break; |
| 8851 | case tok::star: Opc = UO_Deref; break; |
| 8852 | case tok::plus: Opc = UO_Plus; break; |
| 8853 | case tok::minus: Opc = UO_Minus; break; |
| 8854 | case tok::tilde: Opc = UO_Not; break; |
| 8855 | case tok::exclaim: Opc = UO_LNot; break; |
| 8856 | case tok::kw___real: Opc = UO_Real; break; |
| 8857 | case tok::kw___imag: Opc = UO_Imag; break; |
| 8858 | case tok::kw___extension__: Opc = UO_Extension; break; |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 8859 | } |
| 8860 | return Opc; |
| 8861 | } |
| 8862 | |
Chandler Carruth | e0cee6a | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 8863 | /// DiagnoseSelfAssignment - Emits a warning if a value is assigned to itself. |
| 8864 | /// This warning is only emitted for builtin assignment operations. It is also |
| 8865 | /// suppressed in the event of macro expansions. |
| 8866 | static void DiagnoseSelfAssignment(Sema &S, Expr *lhs, Expr *rhs, |
| 8867 | SourceLocation OpLoc) { |
| 8868 | if (!S.ActiveTemplateInstantiations.empty()) |
| 8869 | return; |
| 8870 | if (OpLoc.isInvalid() || OpLoc.isMacroID()) |
| 8871 | return; |
| 8872 | lhs = lhs->IgnoreParenImpCasts(); |
| 8873 | rhs = rhs->IgnoreParenImpCasts(); |
| 8874 | const DeclRefExpr *LeftDeclRef = dyn_cast<DeclRefExpr>(lhs); |
| 8875 | const DeclRefExpr *RightDeclRef = dyn_cast<DeclRefExpr>(rhs); |
| 8876 | if (!LeftDeclRef || !RightDeclRef || |
| 8877 | LeftDeclRef->getLocation().isMacroID() || |
| 8878 | RightDeclRef->getLocation().isMacroID()) |
| 8879 | return; |
| 8880 | const ValueDecl *LeftDecl = |
| 8881 | cast<ValueDecl>(LeftDeclRef->getDecl()->getCanonicalDecl()); |
| 8882 | const ValueDecl *RightDecl = |
| 8883 | cast<ValueDecl>(RightDeclRef->getDecl()->getCanonicalDecl()); |
| 8884 | if (LeftDecl != RightDecl) |
| 8885 | return; |
| 8886 | if (LeftDecl->getType().isVolatileQualified()) |
| 8887 | return; |
| 8888 | if (const ReferenceType *RefTy = LeftDecl->getType()->getAs<ReferenceType>()) |
| 8889 | if (RefTy->getPointeeType().isVolatileQualified()) |
| 8890 | return; |
| 8891 | |
| 8892 | S.Diag(OpLoc, diag::warn_self_assignment) |
| 8893 | << LeftDeclRef->getType() |
| 8894 | << lhs->getSourceRange() << rhs->getSourceRange(); |
| 8895 | } |
| 8896 | |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8897 | /// CreateBuiltinBinOp - Creates a new built-in binary operation with |
| 8898 | /// operator @p Opc at location @c TokLoc. This routine only supports |
| 8899 | /// built-in operations; ActOnBinOp handles overloaded operators. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8900 | ExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, |
Argyrios Kyrtzidis | 7a808c0 | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 8901 | BinaryOperatorKind Opc, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8902 | Expr *lhsExpr, Expr *rhsExpr) { |
| 8903 | ExprResult lhs = Owned(lhsExpr), rhs = Owned(rhsExpr); |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8904 | QualType ResultTy; // Result type of the binary operator. |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8905 | // The following two variables are used for compound assignment operators |
| 8906 | QualType CompLHSTy; // Type of LHS after promotions for computation |
| 8907 | QualType CompResultTy; // Type of computation result |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8908 | ExprValueKind VK = VK_RValue; |
| 8909 | ExprObjectKind OK = OK_Ordinary; |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8910 | |
Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 8911 | // Check if a 'foo<int>' involved in a binary op, identifies a single |
| 8912 | // function unambiguously (i.e. an lvalue ala 13.4) |
| 8913 | // But since an assignment can trigger target based overload, exclude it in |
| 8914 | // our blind search. i.e: |
| 8915 | // template<class T> void f(); template<class T, class U> void f(U); |
| 8916 | // f<int> == 0; // resolve f<int> blindly |
| 8917 | // void (*p)(int); p = f<int>; // resolve f<int> using target |
| 8918 | if (Opc != BO_Assign) { |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8919 | ExprResult resolvedLHS = CheckPlaceholderExpr(lhs.get()); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 8920 | if (!resolvedLHS.isUsable()) return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8921 | lhs = move(resolvedLHS); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 8922 | |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8923 | ExprResult resolvedRHS = CheckPlaceholderExpr(rhs.get()); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 8924 | if (!resolvedRHS.isUsable()) return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8925 | rhs = move(resolvedRHS); |
Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 8926 | } |
| 8927 | |
Richard Trieu | 701fb36 | 2011-06-16 21:36:56 +0000 | [diff] [blame^] | 8928 | bool LeftNull = Expr::NPCK_GNUNull == |
| 8929 | lhs.get()->isNullPointerConstant(Context, |
| 8930 | Expr::NPC_ValueDependentIsNotNull); |
| 8931 | bool RightNull = Expr::NPCK_GNUNull == |
| 8932 | rhs.get()->isNullPointerConstant(Context, |
| 8933 | Expr::NPC_ValueDependentIsNotNull); |
| 8934 | |
| 8935 | // Detect when a NULL constant is used improperly in an expression. These |
| 8936 | // are mainly cases where the null pointer is used as an integer instead |
| 8937 | // of a pointer. |
| 8938 | if (LeftNull || RightNull) { |
| 8939 | if (Opc == BO_Mul || Opc == BO_Div || Opc == BO_Rem || Opc == BO_Add || |
| 8940 | Opc == BO_Sub || Opc == BO_Shl || Opc == BO_Shr || Opc == BO_And || |
| 8941 | Opc == BO_Xor || Opc == BO_Or || Opc == BO_MulAssign || |
| 8942 | Opc == BO_DivAssign || Opc == BO_AddAssign || Opc == BO_SubAssign || |
| 8943 | Opc == BO_RemAssign || Opc == BO_ShlAssign || Opc == BO_ShrAssign || |
| 8944 | Opc == BO_AndAssign || Opc == BO_OrAssign || Opc == BO_XorAssign) { |
| 8945 | // These are the operations that would not make sense with a null pointer |
| 8946 | // no matter what the other expression is. |
| 8947 | if (LeftNull && RightNull) { |
| 8948 | Diag(OpLoc, diag::warn_null_in_arithmetic_operation) |
| 8949 | << lhs.get()->getSourceRange() << rhs.get()->getSourceRange(); |
| 8950 | } else if (LeftNull) { |
| 8951 | Diag(OpLoc, diag::warn_null_in_arithmetic_operation) |
| 8952 | << lhs.get()->getSourceRange(); |
| 8953 | } else if (RightNull) { |
| 8954 | Diag(OpLoc, diag::warn_null_in_arithmetic_operation) |
| 8955 | << rhs.get()->getSourceRange(); |
| 8956 | } |
| 8957 | } else if (Opc == BO_LE || Opc == BO_LT || Opc == BO_GE || Opc == BO_GT || |
| 8958 | Opc == BO_EQ || Opc == BO_NE) { |
| 8959 | // These are the operations that would not make sense with a null pointer |
| 8960 | // if the other expression the other expression is not a pointer. |
| 8961 | QualType LeftType = lhs.get()->getType(); |
| 8962 | QualType RightType = rhs.get()->getType(); |
| 8963 | bool LeftPointer = LeftType->isPointerType() || |
| 8964 | LeftType->isBlockPointerType() || |
| 8965 | LeftType->isMemberPointerType() || |
| 8966 | LeftType->isObjCObjectPointerType(); |
| 8967 | bool RightPointer = RightType->isPointerType() || |
| 8968 | RightType->isBlockPointerType() || |
| 8969 | RightType->isMemberPointerType() || |
| 8970 | RightType->isObjCObjectPointerType(); |
| 8971 | if ((LeftNull != RightNull) && !LeftPointer && !RightPointer) { |
| 8972 | if (LeftNull) |
| 8973 | Diag(OpLoc, diag::warn_null_in_arithmetic_operation) |
| 8974 | << lhs.get()->getSourceRange(); |
| 8975 | if (RightNull) |
| 8976 | Diag(OpLoc, diag::warn_null_in_arithmetic_operation) |
| 8977 | << rhs.get()->getSourceRange(); |
| 8978 | } |
| 8979 | } |
| 8980 | } |
| 8981 | |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8982 | switch (Opc) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8983 | case BO_Assign: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8984 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, QualType()); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8985 | if (getLangOptions().CPlusPlus && |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8986 | lhs.get()->getObjectKind() != OK_ObjCProperty) { |
| 8987 | VK = lhs.get()->getValueKind(); |
| 8988 | OK = lhs.get()->getObjectKind(); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8989 | } |
Chandler Carruth | e0cee6a | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 8990 | if (!ResultTy.isNull()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8991 | DiagnoseSelfAssignment(*this, lhs.get(), rhs.get(), OpLoc); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8992 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8993 | case BO_PtrMemD: |
| 8994 | case BO_PtrMemI: |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8995 | ResultTy = CheckPointerToMemberOperands(lhs, rhs, VK, OpLoc, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8996 | Opc == BO_PtrMemI); |
Sebastian Redl | 112a9766 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 8997 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8998 | case BO_Mul: |
| 8999 | case BO_Div: |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 9000 | ResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc, false, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9001 | Opc == BO_Div); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 9002 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9003 | case BO_Rem: |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 9004 | ResultTy = CheckRemainderOperands(lhs, rhs, OpLoc); |
| 9005 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9006 | case BO_Add: |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 9007 | ResultTy = CheckAdditionOperands(lhs, rhs, OpLoc); |
| 9008 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9009 | case BO_Sub: |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 9010 | ResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc); |
| 9011 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9012 | case BO_Shl: |
| 9013 | case BO_Shr: |
Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 9014 | ResultTy = CheckShiftOperands(lhs, rhs, OpLoc, Opc); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 9015 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9016 | case BO_LE: |
| 9017 | case BO_LT: |
| 9018 | case BO_GE: |
| 9019 | case BO_GT: |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 9020 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, true); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 9021 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9022 | case BO_EQ: |
| 9023 | case BO_NE: |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 9024 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, false); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 9025 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9026 | case BO_And: |
| 9027 | case BO_Xor: |
| 9028 | case BO_Or: |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 9029 | ResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc); |
| 9030 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9031 | case BO_LAnd: |
| 9032 | case BO_LOr: |
Chris Lattner | 8406c51 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 9033 | ResultTy = CheckLogicalOperands(lhs, rhs, OpLoc, Opc); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 9034 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9035 | case BO_MulAssign: |
| 9036 | case BO_DivAssign: |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 9037 | CompResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc, true, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9038 | Opc == BO_DivAssign); |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 9039 | CompLHSTy = CompResultTy; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9040 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 9041 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 9042 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9043 | case BO_RemAssign: |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 9044 | CompResultTy = CheckRemainderOperands(lhs, rhs, OpLoc, true); |
| 9045 | CompLHSTy = CompResultTy; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9046 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 9047 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 9048 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9049 | case BO_AddAssign: |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 9050 | CompResultTy = CheckAdditionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9051 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 9052 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 9053 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9054 | case BO_SubAssign: |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 9055 | CompResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9056 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 9057 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 9058 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9059 | case BO_ShlAssign: |
| 9060 | case BO_ShrAssign: |
Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 9061 | CompResultTy = CheckShiftOperands(lhs, rhs, OpLoc, Opc, true); |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 9062 | CompLHSTy = CompResultTy; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9063 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 9064 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 9065 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9066 | case BO_AndAssign: |
| 9067 | case BO_XorAssign: |
| 9068 | case BO_OrAssign: |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 9069 | CompResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc, true); |
| 9070 | CompLHSTy = CompResultTy; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9071 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 9072 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 9073 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9074 | case BO_Comma: |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 9075 | ResultTy = CheckCommaOperands(*this, lhs, rhs, OpLoc); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9076 | if (getLangOptions().CPlusPlus && !rhs.isInvalid()) { |
| 9077 | VK = rhs.get()->getValueKind(); |
| 9078 | OK = rhs.get()->getObjectKind(); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9079 | } |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 9080 | break; |
| 9081 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9082 | if (ResultTy.isNull() || lhs.isInvalid() || rhs.isInvalid()) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 9083 | return ExprError(); |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 9084 | if (CompResultTy.isNull()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9085 | return Owned(new (Context) BinaryOperator(lhs.take(), rhs.take(), Opc, |
| 9086 | ResultTy, VK, OK, OpLoc)); |
| 9087 | if (getLangOptions().CPlusPlus && lhs.get()->getObjectKind() != OK_ObjCProperty) { |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9088 | VK = VK_LValue; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9089 | OK = lhs.get()->getObjectKind(); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9090 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9091 | return Owned(new (Context) CompoundAssignOperator(lhs.take(), rhs.take(), Opc, |
| 9092 | ResultTy, VK, OK, CompLHSTy, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9093 | CompResultTy, OpLoc)); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 9094 | } |
| 9095 | |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 9096 | /// DiagnoseBitwisePrecedence - Emit a warning when bitwise and comparison |
| 9097 | /// operators are mixed in a way that suggests that the programmer forgot that |
| 9098 | /// comparison operators have higher precedence. The most typical example of |
| 9099 | /// such code is "flags & 0x0020 != 0", which is equivalent to "flags & 1". |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9100 | static void DiagnoseBitwisePrecedence(Sema &Self, BinaryOperatorKind Opc, |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 9101 | SourceLocation OpLoc,Expr *lhs,Expr *rhs){ |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 9102 | typedef BinaryOperator BinOp; |
| 9103 | BinOp::Opcode lhsopc = static_cast<BinOp::Opcode>(-1), |
| 9104 | rhsopc = static_cast<BinOp::Opcode>(-1); |
| 9105 | if (BinOp *BO = dyn_cast<BinOp>(lhs)) |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 9106 | lhsopc = BO->getOpcode(); |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 9107 | if (BinOp *BO = dyn_cast<BinOp>(rhs)) |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 9108 | rhsopc = BO->getOpcode(); |
| 9109 | |
| 9110 | // Subs are not binary operators. |
| 9111 | if (lhsopc == -1 && rhsopc == -1) |
| 9112 | return; |
| 9113 | |
| 9114 | // Bitwise operations are sometimes used as eager logical ops. |
| 9115 | // Don't diagnose this. |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 9116 | if ((BinOp::isComparisonOp(lhsopc) || BinOp::isBitwiseOp(lhsopc)) && |
| 9117 | (BinOp::isComparisonOp(rhsopc) || BinOp::isBitwiseOp(rhsopc))) |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 9118 | return; |
| 9119 | |
Chandler Carruth | b00e8c0 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 9120 | if (BinOp::isComparisonOp(lhsopc)) { |
| 9121 | Self.Diag(OpLoc, diag::warn_precedence_bitwise_rel) |
| 9122 | << SourceRange(lhs->getLocStart(), OpLoc) |
| 9123 | << BinOp::getOpcodeStr(Opc) << BinOp::getOpcodeStr(lhsopc); |
Sebastian Redl | 4afb7c58 | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 9124 | SuggestParentheses(Self, OpLoc, |
Douglas Gregor | 2bf2d3d | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 9125 | Self.PDiag(diag::note_precedence_bitwise_silence) |
| 9126 | << BinOp::getOpcodeStr(lhsopc), |
Chandler Carruth | b00e8c0 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 9127 | lhs->getSourceRange()); |
| 9128 | SuggestParentheses(Self, OpLoc, |
Argyrios Kyrtzidis | e1b97c4 | 2011-04-25 23:01:29 +0000 | [diff] [blame] | 9129 | Self.PDiag(diag::note_precedence_bitwise_first) |
| 9130 | << BinOp::getOpcodeStr(Opc), |
| 9131 | SourceRange(cast<BinOp>(lhs)->getRHS()->getLocStart(), rhs->getLocEnd())); |
Chandler Carruth | b00e8c0 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 9132 | } else if (BinOp::isComparisonOp(rhsopc)) { |
| 9133 | Self.Diag(OpLoc, diag::warn_precedence_bitwise_rel) |
| 9134 | << SourceRange(OpLoc, rhs->getLocEnd()) |
| 9135 | << BinOp::getOpcodeStr(Opc) << BinOp::getOpcodeStr(rhsopc); |
Sebastian Redl | 4afb7c58 | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 9136 | SuggestParentheses(Self, OpLoc, |
Argyrios Kyrtzidis | e1b97c4 | 2011-04-25 23:01:29 +0000 | [diff] [blame] | 9137 | Self.PDiag(diag::note_precedence_bitwise_silence) |
| 9138 | << BinOp::getOpcodeStr(rhsopc), |
Chandler Carruth | b00e8c0 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 9139 | rhs->getSourceRange()); |
| 9140 | SuggestParentheses(Self, OpLoc, |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 9141 | Self.PDiag(diag::note_precedence_bitwise_first) |
Douglas Gregor | fa1e36d | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 9142 | << BinOp::getOpcodeStr(Opc), |
Argyrios Kyrtzidis | e1b97c4 | 2011-04-25 23:01:29 +0000 | [diff] [blame] | 9143 | SourceRange(lhs->getLocEnd(), cast<BinOp>(rhs)->getLHS()->getLocStart())); |
Chandler Carruth | b00e8c0 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 9144 | } |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 9145 | } |
| 9146 | |
Argyrios Kyrtzidis | 14a9662 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 9147 | /// \brief It accepts a '&&' expr that is inside a '||' one. |
| 9148 | /// Emit a diagnostic together with a fixit hint that wraps the '&&' expression |
| 9149 | /// in parentheses. |
| 9150 | static void |
| 9151 | EmitDiagnosticForLogicalAndInLogicalOr(Sema &Self, SourceLocation OpLoc, |
Argyrios Kyrtzidis | ad8b4d4 | 2011-04-22 19:16:27 +0000 | [diff] [blame] | 9152 | BinaryOperator *Bop) { |
| 9153 | assert(Bop->getOpcode() == BO_LAnd); |
Chandler Carruth | b00e8c0 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 9154 | Self.Diag(Bop->getOperatorLoc(), diag::warn_logical_and_in_logical_or) |
| 9155 | << Bop->getSourceRange() << OpLoc; |
Argyrios Kyrtzidis | ad8b4d4 | 2011-04-22 19:16:27 +0000 | [diff] [blame] | 9156 | SuggestParentheses(Self, Bop->getOperatorLoc(), |
Argyrios Kyrtzidis | 14a9662 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 9157 | Self.PDiag(diag::note_logical_and_in_logical_or_silence), |
Chandler Carruth | b00e8c0 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 9158 | Bop->getSourceRange()); |
Argyrios Kyrtzidis | 14a9662 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 9159 | } |
| 9160 | |
| 9161 | /// \brief Returns true if the given expression can be evaluated as a constant |
| 9162 | /// 'true'. |
| 9163 | static bool EvaluatesAsTrue(Sema &S, Expr *E) { |
| 9164 | bool Res; |
| 9165 | return E->EvaluateAsBooleanCondition(Res, S.getASTContext()) && Res; |
| 9166 | } |
| 9167 | |
Argyrios Kyrtzidis | 56e879d | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 9168 | /// \brief Returns true if the given expression can be evaluated as a constant |
| 9169 | /// 'false'. |
| 9170 | static bool EvaluatesAsFalse(Sema &S, Expr *E) { |
| 9171 | bool Res; |
| 9172 | return E->EvaluateAsBooleanCondition(Res, S.getASTContext()) && !Res; |
| 9173 | } |
| 9174 | |
Argyrios Kyrtzidis | 14a9662 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 9175 | /// \brief Look for '&&' in the left hand of a '||' expr. |
| 9176 | static void DiagnoseLogicalAndInLogicalOrLHS(Sema &S, SourceLocation OpLoc, |
Argyrios Kyrtzidis | 56e879d | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 9177 | Expr *OrLHS, Expr *OrRHS) { |
| 9178 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(OrLHS)) { |
Argyrios Kyrtzidis | f89a56c | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 9179 | if (Bop->getOpcode() == BO_LAnd) { |
Argyrios Kyrtzidis | 56e879d | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 9180 | // If it's "a && b || 0" don't warn since the precedence doesn't matter. |
| 9181 | if (EvaluatesAsFalse(S, OrRHS)) |
| 9182 | return; |
Argyrios Kyrtzidis | 14a9662 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 9183 | // If it's "1 && a || b" don't warn since the precedence doesn't matter. |
| 9184 | if (!EvaluatesAsTrue(S, Bop->getLHS())) |
| 9185 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, Bop); |
| 9186 | } else if (Bop->getOpcode() == BO_LOr) { |
| 9187 | if (BinaryOperator *RBop = dyn_cast<BinaryOperator>(Bop->getRHS())) { |
| 9188 | // If it's "a || b && 1 || c" we didn't warn earlier for |
| 9189 | // "a || b && 1", but warn now. |
| 9190 | if (RBop->getOpcode() == BO_LAnd && EvaluatesAsTrue(S, RBop->getRHS())) |
| 9191 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, RBop); |
| 9192 | } |
| 9193 | } |
| 9194 | } |
| 9195 | } |
| 9196 | |
| 9197 | /// \brief Look for '&&' in the right hand of a '||' expr. |
| 9198 | static void DiagnoseLogicalAndInLogicalOrRHS(Sema &S, SourceLocation OpLoc, |
Argyrios Kyrtzidis | 56e879d | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 9199 | Expr *OrLHS, Expr *OrRHS) { |
| 9200 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(OrRHS)) { |
Argyrios Kyrtzidis | 14a9662 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 9201 | if (Bop->getOpcode() == BO_LAnd) { |
Argyrios Kyrtzidis | 56e879d | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 9202 | // If it's "0 || a && b" don't warn since the precedence doesn't matter. |
| 9203 | if (EvaluatesAsFalse(S, OrLHS)) |
| 9204 | return; |
Argyrios Kyrtzidis | 14a9662 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 9205 | // If it's "a || b && 1" don't warn since the precedence doesn't matter. |
| 9206 | if (!EvaluatesAsTrue(S, Bop->getRHS())) |
| 9207 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, Bop); |
Argyrios Kyrtzidis | f89a56c | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 9208 | } |
| 9209 | } |
| 9210 | } |
| 9211 | |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 9212 | /// DiagnoseBinOpPrecedence - Emit warnings for expressions with tricky |
Argyrios Kyrtzidis | f89a56c | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 9213 | /// precedence. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9214 | static void DiagnoseBinOpPrecedence(Sema &Self, BinaryOperatorKind Opc, |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 9215 | SourceLocation OpLoc, Expr *lhs, Expr *rhs){ |
Argyrios Kyrtzidis | f89a56c | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 9216 | // Diagnose "arg1 'bitwise' arg2 'eq' arg3". |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 9217 | if (BinaryOperator::isBitwiseOp(Opc)) |
Argyrios Kyrtzidis | f89a56c | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 9218 | return DiagnoseBitwisePrecedence(Self, Opc, OpLoc, lhs, rhs); |
| 9219 | |
Argyrios Kyrtzidis | 14a9662 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 9220 | // Warn about arg1 || arg2 && arg3, as GCC 4.3+ does. |
| 9221 | // 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] | 9222 | if (Opc == BO_LOr && !OpLoc.isMacroID()/* Don't warn in macros. */) { |
Argyrios Kyrtzidis | 56e879d | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 9223 | DiagnoseLogicalAndInLogicalOrLHS(Self, OpLoc, lhs, rhs); |
| 9224 | DiagnoseLogicalAndInLogicalOrRHS(Self, OpLoc, lhs, rhs); |
Argyrios Kyrtzidis | f89a56c | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 9225 | } |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 9226 | } |
| 9227 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 9228 | // Binary Operators. 'Tok' is the token for the operator. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9229 | ExprResult Sema::ActOnBinOp(Scope *S, SourceLocation TokLoc, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9230 | tok::TokenKind Kind, |
| 9231 | Expr *lhs, Expr *rhs) { |
| 9232 | BinaryOperatorKind Opc = ConvertTokenKindToBinaryOpcode(Kind); |
Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 9233 | assert((lhs != 0) && "ActOnBinOp(): missing left expression"); |
| 9234 | assert((rhs != 0) && "ActOnBinOp(): missing right expression"); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 9235 | |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 9236 | // Emit warnings for tricky precedence issues, e.g. "bitfield & 0x4 == 0" |
| 9237 | DiagnoseBinOpPrecedence(*this, Opc, TokLoc, lhs, rhs); |
| 9238 | |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 9239 | return BuildBinOp(S, TokLoc, Opc, lhs, rhs); |
| 9240 | } |
| 9241 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9242 | ExprResult Sema::BuildBinOp(Scope *S, SourceLocation OpLoc, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9243 | BinaryOperatorKind Opc, |
| 9244 | Expr *lhs, Expr *rhs) { |
John McCall | 622114c | 2010-12-06 05:26:58 +0000 | [diff] [blame] | 9245 | if (getLangOptions().CPlusPlus) { |
| 9246 | bool UseBuiltinOperator; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9247 | |
John McCall | 622114c | 2010-12-06 05:26:58 +0000 | [diff] [blame] | 9248 | if (lhs->isTypeDependent() || rhs->isTypeDependent()) { |
| 9249 | UseBuiltinOperator = false; |
| 9250 | } else if (Opc == BO_Assign && lhs->getObjectKind() == OK_ObjCProperty) { |
| 9251 | UseBuiltinOperator = true; |
| 9252 | } else { |
| 9253 | UseBuiltinOperator = !lhs->getType()->isOverloadableType() && |
| 9254 | !rhs->getType()->isOverloadableType(); |
| 9255 | } |
| 9256 | |
| 9257 | if (!UseBuiltinOperator) { |
| 9258 | // Find all of the overloaded operators visible from this |
| 9259 | // point. We perform both an operator-name lookup from the local |
| 9260 | // scope and an argument-dependent lookup based on the types of |
| 9261 | // the arguments. |
| 9262 | UnresolvedSet<16> Functions; |
| 9263 | OverloadedOperatorKind OverOp |
| 9264 | = BinaryOperator::getOverloadedOperator(Opc); |
| 9265 | if (S && OverOp != OO_None) |
| 9266 | LookupOverloadedOperatorName(OverOp, S, lhs->getType(), rhs->getType(), |
| 9267 | Functions); |
| 9268 | |
| 9269 | // Build the (potentially-overloaded, potentially-dependent) |
| 9270 | // binary operation. |
| 9271 | return CreateOverloadedBinOp(OpLoc, Opc, Functions, lhs, rhs); |
| 9272 | } |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 9273 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9274 | |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 9275 | // Build a built-in binary operation. |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 9276 | return CreateBuiltinBinOp(OpLoc, Opc, lhs, rhs); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 9277 | } |
| 9278 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9279 | ExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc, |
Argyrios Kyrtzidis | 7a808c0 | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 9280 | UnaryOperatorKind Opc, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9281 | Expr *InputExpr) { |
| 9282 | ExprResult Input = Owned(InputExpr); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9283 | ExprValueKind VK = VK_RValue; |
| 9284 | ExprObjectKind OK = OK_Ordinary; |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 9285 | QualType resultType; |
| 9286 | switch (Opc) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9287 | case UO_PreInc: |
| 9288 | case UO_PreDec: |
| 9289 | case UO_PostInc: |
| 9290 | case UO_PostDec: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9291 | resultType = CheckIncrementDecrementOperand(*this, Input.get(), VK, OpLoc, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9292 | Opc == UO_PreInc || |
| 9293 | Opc == UO_PostInc, |
| 9294 | Opc == UO_PreInc || |
| 9295 | Opc == UO_PreDec); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 9296 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9297 | case UO_AddrOf: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9298 | resultType = CheckAddressOfOperand(*this, Input.get(), OpLoc); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 9299 | break; |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9300 | case UO_Deref: { |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 9301 | ExprResult resolved = CheckPlaceholderExpr(Input.get()); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9302 | if (!resolved.isUsable()) return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9303 | Input = move(resolved); |
| 9304 | Input = DefaultFunctionArrayLvalueConversion(Input.take()); |
| 9305 | resultType = CheckIndirectionOperand(*this, Input.get(), VK, OpLoc); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 9306 | break; |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9307 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9308 | case UO_Plus: |
| 9309 | case UO_Minus: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9310 | Input = UsualUnaryConversions(Input.take()); |
| 9311 | if (Input.isInvalid()) return ExprError(); |
| 9312 | resultType = Input.get()->getType(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9313 | if (resultType->isDependentType()) |
| 9314 | break; |
Douglas Gregor | a3208f9 | 2010-06-22 23:41:02 +0000 | [diff] [blame] | 9315 | if (resultType->isArithmeticType() || // C99 6.5.3.3p1 |
| 9316 | resultType->isVectorType()) |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 9317 | break; |
| 9318 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6-7 |
| 9319 | resultType->isEnumeralType()) |
| 9320 | break; |
| 9321 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6 |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9322 | Opc == UO_Plus && |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 9323 | resultType->isPointerType()) |
| 9324 | break; |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9325 | else if (resultType->isPlaceholderType()) { |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 9326 | Input = CheckPlaceholderExpr(Input.take()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9327 | if (Input.isInvalid()) return ExprError(); |
| 9328 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input.take()); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9329 | } |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 9330 | |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 9331 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9332 | << resultType << Input.get()->getSourceRange()); |
| 9333 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9334 | case UO_Not: // bitwise complement |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9335 | Input = UsualUnaryConversions(Input.take()); |
| 9336 | if (Input.isInvalid()) return ExprError(); |
| 9337 | resultType = Input.get()->getType(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9338 | if (resultType->isDependentType()) |
| 9339 | break; |
Chris Lattner | 0d70761 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 9340 | // C99 6.5.3.3p1. We allow complex int and float as a GCC extension. |
| 9341 | if (resultType->isComplexType() || resultType->isComplexIntegerType()) |
| 9342 | // C99 does not support '~' for complex conjugation. |
Chris Lattner | 29e812b | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 9343 | Diag(OpLoc, diag::ext_integer_complement_complex) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9344 | << resultType << Input.get()->getSourceRange(); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9345 | else if (resultType->hasIntegerRepresentation()) |
| 9346 | break; |
| 9347 | else if (resultType->isPlaceholderType()) { |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 9348 | Input = CheckPlaceholderExpr(Input.take()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9349 | if (Input.isInvalid()) return ExprError(); |
| 9350 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input.take()); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9351 | } else { |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 9352 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9353 | << resultType << Input.get()->getSourceRange()); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9354 | } |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 9355 | break; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9356 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9357 | case UO_LNot: // logical negation |
Steve Naroff | 71b59a9 | 2007-06-04 22:22:31 +0000 | [diff] [blame] | 9358 | // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5). |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9359 | Input = DefaultFunctionArrayLvalueConversion(Input.take()); |
| 9360 | if (Input.isInvalid()) return ExprError(); |
| 9361 | resultType = Input.get()->getType(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9362 | if (resultType->isDependentType()) |
| 9363 | break; |
Abramo Bagnara | 7ccce98 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 9364 | if (resultType->isScalarType()) { |
| 9365 | // C99 6.5.3.3p1: ok, fallthrough; |
| 9366 | if (Context.getLangOptions().CPlusPlus) { |
| 9367 | // C++03 [expr.unary.op]p8, C++0x [expr.unary.op]p9: |
| 9368 | // operand contextually converted to bool. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9369 | Input = ImpCastExprToType(Input.take(), Context.BoolTy, |
| 9370 | ScalarTypeToBooleanCastKind(resultType)); |
Abramo Bagnara | 7ccce98 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 9371 | } |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9372 | } else if (resultType->isPlaceholderType()) { |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 9373 | Input = CheckPlaceholderExpr(Input.take()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9374 | if (Input.isInvalid()) return ExprError(); |
| 9375 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input.take()); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9376 | } else { |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 9377 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9378 | << resultType << Input.get()->getSourceRange()); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9379 | } |
Douglas Gregor | db8c6fd | 2010-09-20 17:13:33 +0000 | [diff] [blame] | 9380 | |
Chris Lattner | be31ed8 | 2007-06-02 19:11:33 +0000 | [diff] [blame] | 9381 | // LNot always has type int. C99 6.5.3.3p5. |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 9382 | // In C++, it's bool. C++ 5.3.1p8 |
Argyrios Kyrtzidis | 1bdd688 | 2011-02-18 20:55:15 +0000 | [diff] [blame] | 9383 | resultType = Context.getLogicalOperationType(); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 9384 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9385 | case UO_Real: |
| 9386 | case UO_Imag: |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 9387 | resultType = CheckRealImagOperand(*this, Input, OpLoc, Opc == UO_Real); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9388 | // _Real and _Imag map ordinary l-values into ordinary l-values. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9389 | if (Input.isInvalid()) return ExprError(); |
| 9390 | if (Input.get()->getValueKind() != VK_RValue && |
| 9391 | Input.get()->getObjectKind() == OK_Ordinary) |
| 9392 | VK = Input.get()->getValueKind(); |
Chris Lattner | 30b5dd0 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 9393 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9394 | case UO_Extension: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9395 | resultType = Input.get()->getType(); |
| 9396 | VK = Input.get()->getValueKind(); |
| 9397 | OK = Input.get()->getObjectKind(); |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 9398 | break; |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 9399 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9400 | if (resultType.isNull() || Input.isInvalid()) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 9401 | return ExprError(); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9402 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9403 | return Owned(new (Context) UnaryOperator(Input.take(), Opc, resultType, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9404 | VK, OK, OpLoc)); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 9405 | } |
Chris Lattner | eefa10e | 2007-05-28 06:56:27 +0000 | [diff] [blame] | 9406 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9407 | ExprResult Sema::BuildUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 9408 | UnaryOperatorKind Opc, |
| 9409 | Expr *Input) { |
Anders Carlsson | 461a2c0 | 2009-11-14 21:26:41 +0000 | [diff] [blame] | 9410 | if (getLangOptions().CPlusPlus && Input->getType()->isOverloadableType() && |
Eli Friedman | 8ed2bac | 2010-09-05 23:15:52 +0000 | [diff] [blame] | 9411 | UnaryOperator::getOverloadedOperator(Opc) != OO_None) { |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9412 | // Find all of the overloaded operators visible from this |
| 9413 | // point. We perform both an operator-name lookup from the local |
| 9414 | // scope and an argument-dependent lookup based on the types of |
| 9415 | // the arguments. |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 9416 | UnresolvedSet<16> Functions; |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9417 | OverloadedOperatorKind OverOp = UnaryOperator::getOverloadedOperator(Opc); |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 9418 | if (S && OverOp != OO_None) |
| 9419 | LookupOverloadedOperatorName(OverOp, S, Input->getType(), QualType(), |
| 9420 | Functions); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9421 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9422 | return CreateOverloadedUnaryOp(OpLoc, Opc, Functions, Input); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9423 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9424 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9425 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 9426 | } |
| 9427 | |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 9428 | // Unary Operators. 'Tok' is the token for the operator. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9429 | ExprResult Sema::ActOnUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 9430 | tok::TokenKind Op, Expr *Input) { |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9431 | return BuildUnaryOp(S, OpLoc, ConvertTokenKindToUnaryOpcode(Op), Input); |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 9432 | } |
| 9433 | |
Steve Naroff | 66356bd | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 9434 | /// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo". |
Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 9435 | ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc, SourceLocation LabLoc, |
Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 9436 | LabelDecl *TheDecl) { |
Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 9437 | TheDecl->setUsed(); |
Chris Lattner | eefa10e | 2007-05-28 06:56:27 +0000 | [diff] [blame] | 9438 | // 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] | 9439 | return Owned(new (Context) AddrLabelExpr(OpLoc, LabLoc, TheDecl, |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9440 | Context.getPointerType(Context.VoidTy))); |
Chris Lattner | eefa10e | 2007-05-28 06:56:27 +0000 | [diff] [blame] | 9441 | } |
| 9442 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9443 | /// Given the last statement in a statement-expression, check whether |
| 9444 | /// the result is a producing expression (like a call to an |
| 9445 | /// ns_returns_retained function) and, if so, rebuild it to hoist the |
| 9446 | /// release out of the full-expression. Otherwise, return null. |
| 9447 | /// Cannot fail. |
| 9448 | static Expr *maybeRebuildARCConsumingStmt(Stmt *s) { |
| 9449 | // Should always be wrapped with one of these. |
| 9450 | ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(s); |
| 9451 | if (!cleanups) return 0; |
| 9452 | |
| 9453 | ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(cleanups->getSubExpr()); |
| 9454 | if (!cast || cast->getCastKind() != CK_ObjCConsumeObject) |
| 9455 | return 0; |
| 9456 | |
| 9457 | // Splice out the cast. This shouldn't modify any interesting |
| 9458 | // features of the statement. |
| 9459 | Expr *producer = cast->getSubExpr(); |
| 9460 | assert(producer->getType() == cast->getType()); |
| 9461 | assert(producer->getValueKind() == cast->getValueKind()); |
| 9462 | cleanups->setSubExpr(producer); |
| 9463 | return cleanups; |
| 9464 | } |
| 9465 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9466 | ExprResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9467 | Sema::ActOnStmtExpr(SourceLocation LPLoc, Stmt *SubStmt, |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9468 | SourceLocation RPLoc) { // "({..})" |
Chris Lattner | 366727f | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 9469 | assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!"); |
| 9470 | CompoundStmt *Compound = cast<CompoundStmt>(SubStmt); |
| 9471 | |
Douglas Gregor | 6cf3f3c | 2010-03-10 04:54:39 +0000 | [diff] [blame] | 9472 | bool isFileScope |
| 9473 | = (getCurFunctionOrMethodDecl() == 0) && (getCurBlock() == 0); |
Chris Lattner | a69b076 | 2009-04-25 19:11:05 +0000 | [diff] [blame] | 9474 | if (isFileScope) |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9475 | return ExprError(Diag(LPLoc, diag::err_stmtexpr_file_scope)); |
Eli Friedman | 52cc016 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 9476 | |
Chris Lattner | 366727f | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 9477 | // FIXME: there are a variety of strange constraints to enforce here, for |
| 9478 | // example, it is not possible to goto into a stmt expression apparently. |
| 9479 | // More semantic analysis is needed. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9480 | |
Chris Lattner | 366727f | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 9481 | // If there are sub stmts in the compound stmt, take the type of the last one |
| 9482 | // as the type of the stmtexpr. |
| 9483 | QualType Ty = Context.VoidTy; |
Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9484 | bool StmtExprMayBindToTemp = false; |
Chris Lattner | 944d306 | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 9485 | if (!Compound->body_empty()) { |
| 9486 | Stmt *LastStmt = Compound->body_back(); |
Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9487 | LabelStmt *LastLabelStmt = 0; |
Chris Lattner | 944d306 | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 9488 | // If LastStmt is a label, skip down through into the body. |
Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9489 | while (LabelStmt *Label = dyn_cast<LabelStmt>(LastStmt)) { |
| 9490 | LastLabelStmt = Label; |
Chris Lattner | 944d306 | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 9491 | LastStmt = Label->getSubStmt(); |
Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9492 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9493 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9494 | if (Expr *LastE = dyn_cast<Expr>(LastStmt)) { |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 9495 | // Do function/array conversion on the last expression, but not |
| 9496 | // lvalue-to-rvalue. However, initialize an unqualified type. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9497 | ExprResult LastExpr = DefaultFunctionArrayConversion(LastE); |
| 9498 | if (LastExpr.isInvalid()) |
| 9499 | return ExprError(); |
| 9500 | Ty = LastExpr.get()->getType().getUnqualifiedType(); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 9501 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9502 | if (!Ty->isDependentType() && !LastExpr.get()->isTypeDependent()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9503 | // In ARC, if the final expression ends in a consume, splice |
| 9504 | // the consume out and bind it later. In the alternate case |
| 9505 | // (when dealing with a retainable type), the result |
| 9506 | // initialization will create a produce. In both cases the |
| 9507 | // result will be +1, and we'll need to balance that out with |
| 9508 | // a bind. |
| 9509 | if (Expr *rebuiltLastStmt |
| 9510 | = maybeRebuildARCConsumingStmt(LastExpr.get())) { |
| 9511 | LastExpr = rebuiltLastStmt; |
| 9512 | } else { |
| 9513 | LastExpr = PerformCopyInitialization( |
Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9514 | InitializedEntity::InitializeResult(LPLoc, |
| 9515 | Ty, |
| 9516 | false), |
| 9517 | SourceLocation(), |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9518 | LastExpr); |
| 9519 | } |
| 9520 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9521 | if (LastExpr.isInvalid()) |
Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9522 | return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9523 | if (LastExpr.get() != 0) { |
Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9524 | if (!LastLabelStmt) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9525 | Compound->setLastStmt(LastExpr.take()); |
Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9526 | else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9527 | LastLabelStmt->setSubStmt(LastExpr.take()); |
Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9528 | StmtExprMayBindToTemp = true; |
| 9529 | } |
| 9530 | } |
| 9531 | } |
Chris Lattner | 944d306 | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 9532 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9533 | |
Eli Friedman | ba961a9 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 9534 | // FIXME: Check that expression type is complete/non-abstract; statement |
| 9535 | // expressions are not lvalues. |
Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 9536 | Expr *ResStmtExpr = new (Context) StmtExpr(Compound, Ty, LPLoc, RPLoc); |
| 9537 | if (StmtExprMayBindToTemp) |
| 9538 | return MaybeBindToTemporary(ResStmtExpr); |
| 9539 | return Owned(ResStmtExpr); |
Chris Lattner | 366727f | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 9540 | } |
Steve Naroff | 7886467 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 9541 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9542 | ExprResult Sema::BuildBuiltinOffsetOf(SourceLocation BuiltinLoc, |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9543 | TypeSourceInfo *TInfo, |
| 9544 | OffsetOfComponent *CompPtr, |
| 9545 | unsigned NumComponents, |
| 9546 | SourceLocation RParenLoc) { |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9547 | QualType ArgTy = TInfo->getType(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9548 | bool Dependent = ArgTy->isDependentType(); |
Abramo Bagnara | 1108e7b | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 9549 | SourceRange TypeRange = TInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9550 | |
Chris Lattner | f17bd42 | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 9551 | // We must have at least one component that refers to the type, and the first |
| 9552 | // one is known to be a field designator. Verify that the ArgTy represents |
| 9553 | // a struct/union/class. |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9554 | if (!Dependent && !ArgTy->isRecordType()) |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9555 | return ExprError(Diag(BuiltinLoc, diag::err_offsetof_record_type) |
| 9556 | << ArgTy << TypeRange); |
| 9557 | |
| 9558 | // Type must be complete per C99 7.17p3 because a declaring a variable |
| 9559 | // with an incomplete type would be ill-formed. |
| 9560 | if (!Dependent |
| 9561 | && RequireCompleteType(BuiltinLoc, ArgTy, |
| 9562 | PDiag(diag::err_offsetof_incomplete_type) |
| 9563 | << TypeRange)) |
| 9564 | return ExprError(); |
| 9565 | |
Chris Lattner | 78502cf | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 9566 | // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a |
| 9567 | // GCC extension, diagnose them. |
Eli Friedman | 988a16b | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 9568 | // FIXME: This diagnostic isn't actually visible because the location is in |
| 9569 | // a system header! |
Chris Lattner | 78502cf | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 9570 | if (NumComponents != 1) |
Chris Lattner | f490e15 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 9571 | Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator) |
| 9572 | << SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9573 | |
| 9574 | bool DidWarnAboutNonPOD = false; |
| 9575 | QualType CurrentType = ArgTy; |
| 9576 | typedef OffsetOfExpr::OffsetOfNode OffsetOfNode; |
| 9577 | llvm::SmallVector<OffsetOfNode, 4> Comps; |
| 9578 | llvm::SmallVector<Expr*, 4> Exprs; |
| 9579 | for (unsigned i = 0; i != NumComponents; ++i) { |
| 9580 | const OffsetOfComponent &OC = CompPtr[i]; |
| 9581 | if (OC.isBrackets) { |
| 9582 | // Offset of an array sub-field. TODO: Should we allow vector elements? |
| 9583 | if (!CurrentType->isDependentType()) { |
| 9584 | const ArrayType *AT = Context.getAsArrayType(CurrentType); |
| 9585 | if(!AT) |
| 9586 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_array_type) |
| 9587 | << CurrentType); |
| 9588 | CurrentType = AT->getElementType(); |
| 9589 | } else |
| 9590 | CurrentType = Context.DependentTy; |
| 9591 | |
| 9592 | // The expression must be an integral expression. |
| 9593 | // FIXME: An integral constant expression? |
| 9594 | Expr *Idx = static_cast<Expr*>(OC.U.E); |
| 9595 | if (!Idx->isTypeDependent() && !Idx->isValueDependent() && |
| 9596 | !Idx->getType()->isIntegerType()) |
| 9597 | return ExprError(Diag(Idx->getLocStart(), |
| 9598 | diag::err_typecheck_subscript_not_integer) |
| 9599 | << Idx->getSourceRange()); |
| 9600 | |
| 9601 | // Record this array index. |
| 9602 | Comps.push_back(OffsetOfNode(OC.LocStart, Exprs.size(), OC.LocEnd)); |
| 9603 | Exprs.push_back(Idx); |
| 9604 | continue; |
| 9605 | } |
| 9606 | |
| 9607 | // Offset of a field. |
| 9608 | if (CurrentType->isDependentType()) { |
| 9609 | // We have the offset of a field, but we can't look into the dependent |
| 9610 | // type. Just record the identifier of the field. |
| 9611 | Comps.push_back(OffsetOfNode(OC.LocStart, OC.U.IdentInfo, OC.LocEnd)); |
| 9612 | CurrentType = Context.DependentTy; |
| 9613 | continue; |
| 9614 | } |
| 9615 | |
| 9616 | // We need to have a complete type to look into. |
| 9617 | if (RequireCompleteType(OC.LocStart, CurrentType, |
| 9618 | diag::err_offsetof_incomplete_type)) |
| 9619 | return ExprError(); |
| 9620 | |
| 9621 | // Look for the designated field. |
| 9622 | const RecordType *RC = CurrentType->getAs<RecordType>(); |
| 9623 | if (!RC) |
| 9624 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_record_type) |
| 9625 | << CurrentType); |
| 9626 | RecordDecl *RD = RC->getDecl(); |
| 9627 | |
| 9628 | // C++ [lib.support.types]p5: |
| 9629 | // The macro offsetof accepts a restricted set of type arguments in this |
| 9630 | // International Standard. type shall be a POD structure or a POD union |
| 9631 | // (clause 9). |
| 9632 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 9633 | if (!CRD->isPOD() && !DidWarnAboutNonPOD && |
Ted Kremenek | 55ae319 | 2011-02-23 01:51:43 +0000 | [diff] [blame] | 9634 | DiagRuntimeBehavior(BuiltinLoc, 0, |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9635 | PDiag(diag::warn_offsetof_non_pod_type) |
| 9636 | << SourceRange(CompPtr[0].LocStart, OC.LocEnd) |
| 9637 | << CurrentType)) |
| 9638 | DidWarnAboutNonPOD = true; |
| 9639 | } |
| 9640 | |
| 9641 | // Look for the field. |
| 9642 | LookupResult R(*this, OC.U.IdentInfo, OC.LocStart, LookupMemberName); |
| 9643 | LookupQualifiedName(R, RD); |
| 9644 | FieldDecl *MemberDecl = R.getAsSingle<FieldDecl>(); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 9645 | IndirectFieldDecl *IndirectMemberDecl = 0; |
| 9646 | if (!MemberDecl) { |
Benjamin Kramer | 3959370 | 2010-11-21 14:11:41 +0000 | [diff] [blame] | 9647 | if ((IndirectMemberDecl = R.getAsSingle<IndirectFieldDecl>())) |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 9648 | MemberDecl = IndirectMemberDecl->getAnonField(); |
| 9649 | } |
| 9650 | |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9651 | if (!MemberDecl) |
| 9652 | return ExprError(Diag(BuiltinLoc, diag::err_no_member) |
| 9653 | << OC.U.IdentInfo << RD << SourceRange(OC.LocStart, |
| 9654 | OC.LocEnd)); |
| 9655 | |
Douglas Gregor | 10982ea | 2010-04-28 22:36:06 +0000 | [diff] [blame] | 9656 | // C99 7.17p3: |
| 9657 | // (If the specified member is a bit-field, the behavior is undefined.) |
| 9658 | // |
| 9659 | // We diagnose this as an error. |
| 9660 | if (MemberDecl->getBitWidth()) { |
| 9661 | Diag(OC.LocEnd, diag::err_offsetof_bitfield) |
| 9662 | << MemberDecl->getDeclName() |
| 9663 | << SourceRange(BuiltinLoc, RParenLoc); |
| 9664 | Diag(MemberDecl->getLocation(), diag::note_bitfield_decl); |
| 9665 | return ExprError(); |
| 9666 | } |
Eli Friedman | 74ef7cf | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 9667 | |
| 9668 | RecordDecl *Parent = MemberDecl->getParent(); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 9669 | if (IndirectMemberDecl) |
| 9670 | Parent = cast<RecordDecl>(IndirectMemberDecl->getDeclContext()); |
Eli Friedman | 74ef7cf | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 9671 | |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 9672 | // If the member was found in a base class, introduce OffsetOfNodes for |
| 9673 | // the base class indirections. |
| 9674 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 9675 | /*DetectVirtual=*/false); |
Eli Friedman | 74ef7cf | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 9676 | if (IsDerivedFrom(CurrentType, Context.getTypeDeclType(Parent), Paths)) { |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 9677 | CXXBasePath &Path = Paths.front(); |
| 9678 | for (CXXBasePath::iterator B = Path.begin(), BEnd = Path.end(); |
| 9679 | B != BEnd; ++B) |
| 9680 | Comps.push_back(OffsetOfNode(B->Base)); |
| 9681 | } |
Eli Friedman | 74ef7cf | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 9682 | |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 9683 | if (IndirectMemberDecl) { |
| 9684 | for (IndirectFieldDecl::chain_iterator FI = |
| 9685 | IndirectMemberDecl->chain_begin(), |
| 9686 | FEnd = IndirectMemberDecl->chain_end(); FI != FEnd; FI++) { |
| 9687 | assert(isa<FieldDecl>(*FI)); |
| 9688 | Comps.push_back(OffsetOfNode(OC.LocStart, |
| 9689 | cast<FieldDecl>(*FI), OC.LocEnd)); |
| 9690 | } |
| 9691 | } else |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9692 | Comps.push_back(OffsetOfNode(OC.LocStart, MemberDecl, OC.LocEnd)); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 9693 | |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9694 | CurrentType = MemberDecl->getType().getNonReferenceType(); |
| 9695 | } |
| 9696 | |
| 9697 | return Owned(OffsetOfExpr::Create(Context, Context.getSizeType(), BuiltinLoc, |
| 9698 | TInfo, Comps.data(), Comps.size(), |
| 9699 | Exprs.data(), Exprs.size(), RParenLoc)); |
| 9700 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9701 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9702 | ExprResult Sema::ActOnBuiltinOffsetOf(Scope *S, |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9703 | SourceLocation BuiltinLoc, |
| 9704 | SourceLocation TypeLoc, |
| 9705 | ParsedType argty, |
| 9706 | OffsetOfComponent *CompPtr, |
| 9707 | unsigned NumComponents, |
| 9708 | SourceLocation RPLoc) { |
| 9709 | |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9710 | TypeSourceInfo *ArgTInfo; |
| 9711 | QualType ArgTy = GetTypeFromParser(argty, &ArgTInfo); |
| 9712 | if (ArgTy.isNull()) |
| 9713 | return ExprError(); |
| 9714 | |
Eli Friedman | 06dcfd9 | 2010-08-05 10:15:45 +0000 | [diff] [blame] | 9715 | if (!ArgTInfo) |
| 9716 | ArgTInfo = Context.getTrivialTypeSourceInfo(ArgTy, TypeLoc); |
| 9717 | |
| 9718 | return BuildBuiltinOffsetOf(BuiltinLoc, ArgTInfo, CompPtr, NumComponents, |
| 9719 | RPLoc); |
Chris Lattner | f17bd42 | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 9720 | } |
| 9721 | |
| 9722 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9723 | ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9724 | Expr *CondExpr, |
| 9725 | Expr *LHSExpr, Expr *RHSExpr, |
| 9726 | SourceLocation RPLoc) { |
Steve Naroff | 9efdabc | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 9727 | assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)"); |
| 9728 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9729 | ExprValueKind VK = VK_RValue; |
| 9730 | ExprObjectKind OK = OK_Ordinary; |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9731 | QualType resType; |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 9732 | bool ValueDependent = false; |
Douglas Gregor | 0df9112 | 2009-05-19 22:43:30 +0000 | [diff] [blame] | 9733 | if (CondExpr->isTypeDependent() || CondExpr->isValueDependent()) { |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9734 | resType = Context.DependentTy; |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 9735 | ValueDependent = true; |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9736 | } else { |
| 9737 | // The conditional expression is required to be a constant expression. |
| 9738 | llvm::APSInt condEval(32); |
| 9739 | SourceLocation ExpLoc; |
| 9740 | if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc)) |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9741 | return ExprError(Diag(ExpLoc, |
| 9742 | diag::err_typecheck_choose_expr_requires_constant) |
| 9743 | << CondExpr->getSourceRange()); |
Steve Naroff | 9efdabc | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 9744 | |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9745 | // 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] | 9746 | Expr *ActiveExpr = condEval.getZExtValue() ? LHSExpr : RHSExpr; |
| 9747 | |
| 9748 | resType = ActiveExpr->getType(); |
| 9749 | ValueDependent = ActiveExpr->isValueDependent(); |
| 9750 | VK = ActiveExpr->getValueKind(); |
| 9751 | OK = ActiveExpr->getObjectKind(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9752 | } |
| 9753 | |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9754 | return Owned(new (Context) ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9755 | resType, VK, OK, RPLoc, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 9756 | resType->isDependentType(), |
| 9757 | ValueDependent)); |
Steve Naroff | 9efdabc | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 9758 | } |
| 9759 | |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9760 | //===----------------------------------------------------------------------===// |
| 9761 | // Clang Extensions. |
| 9762 | //===----------------------------------------------------------------------===// |
| 9763 | |
| 9764 | /// ActOnBlockStart - This callback is invoked when a block literal is started. |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9765 | void Sema::ActOnBlockStart(SourceLocation CaretLoc, Scope *BlockScope) { |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9766 | BlockDecl *Block = BlockDecl::Create(Context, CurContext, CaretLoc); |
| 9767 | PushBlockScope(BlockScope, Block); |
| 9768 | CurContext->addDecl(Block); |
Fariborz Jahanian | 1babe77 | 2010-07-09 18:44:02 +0000 | [diff] [blame] | 9769 | if (BlockScope) |
| 9770 | PushDeclContext(BlockScope, Block); |
| 9771 | else |
| 9772 | CurContext = Block; |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9773 | } |
| 9774 | |
Mike Stump | 82f071f | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 9775 | void Sema::ActOnBlockArguments(Declarator &ParamInfo, Scope *CurScope) { |
Mike Stump | f70bcf7 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 9776 | assert(ParamInfo.getIdentifier()==0 && "block-id should have no identifier!"); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9777 | assert(ParamInfo.getContext() == Declarator::BlockLiteralContext); |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9778 | BlockScopeInfo *CurBlock = getCurBlock(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9779 | |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 9780 | TypeSourceInfo *Sig = GetTypeForDeclarator(ParamInfo, CurScope); |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 9781 | QualType T = Sig->getType(); |
Mike Stump | 82f071f | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 9782 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9783 | // GetTypeForDeclarator always produces a function type for a block |
| 9784 | // literal signature. Furthermore, it is always a FunctionProtoType |
| 9785 | // unless the function was written with a typedef. |
| 9786 | assert(T->isFunctionType() && |
| 9787 | "GetTypeForDeclarator made a non-function block signature"); |
| 9788 | |
| 9789 | // Look for an explicit signature in that function type. |
| 9790 | FunctionProtoTypeLoc ExplicitSignature; |
| 9791 | |
| 9792 | TypeLoc tmp = Sig->getTypeLoc().IgnoreParens(); |
| 9793 | if (isa<FunctionProtoTypeLoc>(tmp)) { |
| 9794 | ExplicitSignature = cast<FunctionProtoTypeLoc>(tmp); |
| 9795 | |
| 9796 | // Check whether that explicit signature was synthesized by |
| 9797 | // GetTypeForDeclarator. If so, don't save that as part of the |
| 9798 | // written signature. |
Abramo Bagnara | f2a79d9 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 9799 | if (ExplicitSignature.getLocalRangeBegin() == |
| 9800 | ExplicitSignature.getLocalRangeEnd()) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9801 | // This would be much cheaper if we stored TypeLocs instead of |
| 9802 | // TypeSourceInfos. |
| 9803 | TypeLoc Result = ExplicitSignature.getResultLoc(); |
| 9804 | unsigned Size = Result.getFullDataSize(); |
| 9805 | Sig = Context.CreateTypeSourceInfo(Result.getType(), Size); |
| 9806 | Sig->getTypeLoc().initializeFullCopy(Result, Size); |
| 9807 | |
| 9808 | ExplicitSignature = FunctionProtoTypeLoc(); |
| 9809 | } |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9810 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9811 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9812 | CurBlock->TheDecl->setSignatureAsWritten(Sig); |
| 9813 | CurBlock->FunctionType = T; |
| 9814 | |
| 9815 | const FunctionType *Fn = T->getAs<FunctionType>(); |
| 9816 | QualType RetTy = Fn->getResultType(); |
| 9817 | bool isVariadic = |
| 9818 | (isa<FunctionProtoType>(Fn) && cast<FunctionProtoType>(Fn)->isVariadic()); |
| 9819 | |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9820 | CurBlock->TheDecl->setIsVariadic(isVariadic); |
Douglas Gregor | b92a156 | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 9821 | |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9822 | // Don't allow returning a objc interface by value. |
| 9823 | if (RetTy->isObjCObjectType()) { |
| 9824 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 9825 | diag::err_object_cannot_be_passed_returned_by_value) << 0 << RetTy; |
| 9826 | return; |
| 9827 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9828 | |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9829 | // Context.DependentTy is used as a placeholder for a missing block |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9830 | // return type. TODO: what should we do with declarators like: |
| 9831 | // ^ * { ... } |
| 9832 | // If the answer is "apply template argument deduction".... |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9833 | if (RetTy != Context.DependentTy) |
| 9834 | CurBlock->ReturnType = RetTy; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9835 | |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9836 | // Push block parameters from the declarator if we had them. |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9837 | llvm::SmallVector<ParmVarDecl*, 8> Params; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9838 | if (ExplicitSignature) { |
| 9839 | for (unsigned I = 0, E = ExplicitSignature.getNumArgs(); I != E; ++I) { |
| 9840 | ParmVarDecl *Param = ExplicitSignature.getArg(I); |
Fariborz Jahanian | 5ec502e | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 9841 | if (Param->getIdentifier() == 0 && |
| 9842 | !Param->isImplicit() && |
| 9843 | !Param->isInvalidDecl() && |
| 9844 | !getLangOptions().CPlusPlus) |
| 9845 | Diag(Param->getLocation(), diag::err_parameter_name_omitted); |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9846 | Params.push_back(Param); |
Fariborz Jahanian | 5ec502e | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 9847 | } |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9848 | |
| 9849 | // Fake up parameter variables if we have a typedef, like |
| 9850 | // ^ fntype { ... } |
| 9851 | } else if (const FunctionProtoType *Fn = T->getAs<FunctionProtoType>()) { |
| 9852 | for (FunctionProtoType::arg_type_iterator |
| 9853 | I = Fn->arg_type_begin(), E = Fn->arg_type_end(); I != E; ++I) { |
| 9854 | ParmVarDecl *Param = |
| 9855 | BuildParmVarDeclForTypedef(CurBlock->TheDecl, |
| 9856 | ParamInfo.getSourceRange().getBegin(), |
| 9857 | *I); |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9858 | Params.push_back(Param); |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9859 | } |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9860 | } |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9861 | |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9862 | // Set the parameters on the block decl. |
Douglas Gregor | b524d90 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 9863 | if (!Params.empty()) { |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9864 | CurBlock->TheDecl->setParams(Params.data(), Params.size()); |
Douglas Gregor | b524d90 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 9865 | CheckParmsForFunctionDef(CurBlock->TheDecl->param_begin(), |
| 9866 | CurBlock->TheDecl->param_end(), |
| 9867 | /*CheckParameterNames=*/false); |
| 9868 | } |
| 9869 | |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9870 | // Finally we can process decl attributes. |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 9871 | ProcessDeclAttributes(CurScope, CurBlock->TheDecl, ParamInfo); |
John McCall | df8b37c | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9872 | |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9873 | if (!isVariadic && CurBlock->TheDecl->getAttr<SentinelAttr>()) { |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9874 | Diag(ParamInfo.getAttributes()->getLoc(), |
| 9875 | diag::warn_attribute_sentinel_not_variadic) << 1; |
| 9876 | // FIXME: remove the attribute. |
| 9877 | } |
| 9878 | |
| 9879 | // Put the parameter variables in scope. We can bail out immediately |
| 9880 | // if we don't have any. |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9881 | if (Params.empty()) |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9882 | return; |
| 9883 | |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9884 | for (BlockDecl::param_iterator AI = CurBlock->TheDecl->param_begin(), |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 9885 | E = CurBlock->TheDecl->param_end(); AI != E; ++AI) { |
| 9886 | (*AI)->setOwningFunction(CurBlock->TheDecl); |
| 9887 | |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9888 | // If this has an identifier, add it to the scope stack. |
John McCall | df8b37c | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9889 | if ((*AI)->getIdentifier()) { |
Argyrios Kyrtzidis | 1cb0de1 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 9890 | CheckShadow(CurBlock->TheScope, *AI); |
John McCall | df8b37c | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9891 | |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9892 | PushOnScopeChains(*AI, CurBlock->TheScope); |
John McCall | df8b37c | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9893 | } |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 9894 | } |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9895 | } |
| 9896 | |
| 9897 | /// ActOnBlockError - If there is an error parsing a block, this callback |
| 9898 | /// is invoked to pop the information about the block from the action impl. |
| 9899 | void Sema::ActOnBlockError(SourceLocation CaretLoc, Scope *CurScope) { |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9900 | // Pop off CurBlock, handle nested blocks. |
Chris Lattner | 41b8694 | 2009-04-21 22:38:46 +0000 | [diff] [blame] | 9901 | PopDeclContext(); |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9902 | PopFunctionOrBlockScope(); |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9903 | } |
| 9904 | |
| 9905 | /// ActOnBlockStmtExpr - This is called when the body of a block statement |
| 9906 | /// literal was successfully completed. ^(int x){...} |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9907 | ExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, |
Chris Lattner | 60f8449 | 2011-02-17 23:58:47 +0000 | [diff] [blame] | 9908 | Stmt *Body, Scope *CurScope) { |
Chris Lattner | 9eac931 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 9909 | // If blocks are disabled, emit an error. |
| 9910 | if (!LangOpts.Blocks) |
| 9911 | Diag(CaretLoc, diag::err_blocks_disable); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9912 | |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9913 | BlockScopeInfo *BSI = cast<BlockScopeInfo>(FunctionScopes.back()); |
Fariborz Jahanian | 1babe77 | 2010-07-09 18:44:02 +0000 | [diff] [blame] | 9914 | |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9915 | PopDeclContext(); |
| 9916 | |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9917 | QualType RetTy = Context.VoidTy; |
Fariborz Jahanian | 3fd7310 | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 9918 | if (!BSI->ReturnType.isNull()) |
| 9919 | RetTy = BSI->ReturnType; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9920 | |
Mike Stump | 3bf1ab4 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 9921 | bool NoReturn = BSI->TheDecl->getAttr<NoReturnAttr>(); |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9922 | QualType BlockTy; |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9923 | |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 9924 | // Set the captured variables on the block. |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 9925 | BSI->TheDecl->setCaptures(Context, BSI->Captures.begin(), BSI->Captures.end(), |
| 9926 | BSI->CapturesCXXThis); |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 9927 | |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9928 | // If the user wrote a function type in some form, try to use that. |
| 9929 | if (!BSI->FunctionType.isNull()) { |
| 9930 | const FunctionType *FTy = BSI->FunctionType->getAs<FunctionType>(); |
| 9931 | |
| 9932 | FunctionType::ExtInfo Ext = FTy->getExtInfo(); |
| 9933 | if (NoReturn && !Ext.getNoReturn()) Ext = Ext.withNoReturn(true); |
| 9934 | |
| 9935 | // Turn protoless block types into nullary block types. |
| 9936 | if (isa<FunctionNoProtoType>(FTy)) { |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9937 | FunctionProtoType::ExtProtoInfo EPI; |
| 9938 | EPI.ExtInfo = Ext; |
| 9939 | BlockTy = Context.getFunctionType(RetTy, 0, 0, EPI); |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9940 | |
| 9941 | // Otherwise, if we don't need to change anything about the function type, |
| 9942 | // preserve its sugar structure. |
| 9943 | } else if (FTy->getResultType() == RetTy && |
| 9944 | (!NoReturn || FTy->getNoReturnAttr())) { |
| 9945 | BlockTy = BSI->FunctionType; |
| 9946 | |
| 9947 | // Otherwise, make the minimal modifications to the function type. |
| 9948 | } else { |
| 9949 | const FunctionProtoType *FPT = cast<FunctionProtoType>(FTy); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9950 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 9951 | EPI.TypeQuals = 0; // FIXME: silently? |
| 9952 | EPI.ExtInfo = Ext; |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9953 | BlockTy = Context.getFunctionType(RetTy, |
| 9954 | FPT->arg_type_begin(), |
| 9955 | FPT->getNumArgs(), |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9956 | EPI); |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9957 | } |
| 9958 | |
| 9959 | // If we don't have a function type, just build one from nothing. |
| 9960 | } else { |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9961 | FunctionProtoType::ExtProtoInfo EPI; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9962 | EPI.ExtInfo = FunctionType::ExtInfo().withNoReturn(NoReturn); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9963 | BlockTy = Context.getFunctionType(RetTy, 0, 0, EPI); |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9964 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9965 | |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9966 | DiagnoseUnusedParameters(BSI->TheDecl->param_begin(), |
| 9967 | BSI->TheDecl->param_end()); |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9968 | BlockTy = Context.getBlockPointerType(BlockTy); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9969 | |
Chris Lattner | 45542ea | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 9970 | // If needed, diagnose invalid gotos and switches in the block. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9971 | if (getCurFunction()->NeedsScopeChecking() && |
| 9972 | !hasAnyUnrecoverableErrorsInThisFunction()) |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9973 | DiagnoseInvalidJumps(cast<CompoundStmt>(Body)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9974 | |
Chris Lattner | 60f8449 | 2011-02-17 23:58:47 +0000 | [diff] [blame] | 9975 | BSI->TheDecl->setBody(cast<CompoundStmt>(Body)); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9976 | |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 9977 | BlockExpr *Result = new (Context) BlockExpr(BSI->TheDecl, BlockTy); |
John McCall | 1d570a7 | 2010-08-25 05:56:39 +0000 | [diff] [blame] | 9978 | |
Ted Kremenek | 1767a27 | 2011-02-23 01:51:48 +0000 | [diff] [blame] | 9979 | const AnalysisBasedWarnings::Policy &WP = AnalysisWarnings.getDefaultPolicy(); |
| 9980 | PopFunctionOrBlockScope(&WP, Result->getBlockDecl(), Result); |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9981 | return Owned(Result); |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9982 | } |
| 9983 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9984 | ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 9985 | Expr *expr, ParsedType type, |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9986 | SourceLocation RPLoc) { |
Abramo Bagnara | 27db239 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 9987 | TypeSourceInfo *TInfo; |
Jeffrey Yasskin | 8dfa5f1 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 9988 | GetTypeFromParser(type, &TInfo); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9989 | return BuildVAArgExpr(BuiltinLoc, expr, TInfo, RPLoc); |
Abramo Bagnara | 27db239 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 9990 | } |
| 9991 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9992 | ExprResult Sema::BuildVAArgExpr(SourceLocation BuiltinLoc, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9993 | Expr *E, TypeSourceInfo *TInfo, |
| 9994 | SourceLocation RPLoc) { |
Chris Lattner | 56382aa | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 9995 | Expr *OrigExpr = E; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9996 | |
Eli Friedman | 121ba0c | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 9997 | // Get the va_list type |
| 9998 | QualType VaListType = Context.getBuiltinVaListType(); |
Eli Friedman | e2cad65 | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 9999 | if (VaListType->isArrayType()) { |
| 10000 | // Deal with implicit array decay; for example, on x86-64, |
| 10001 | // va_list is an array, but it's supposed to decay to |
| 10002 | // a pointer for va_arg. |
Eli Friedman | 121ba0c | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 10003 | VaListType = Context.getArrayDecayedType(VaListType); |
Eli Friedman | e2cad65 | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 10004 | // Make sure the input expression also decays appropriately. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10005 | ExprResult Result = UsualUnaryConversions(E); |
| 10006 | if (Result.isInvalid()) |
| 10007 | return ExprError(); |
| 10008 | E = Result.take(); |
Eli Friedman | e2cad65 | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 10009 | } else { |
| 10010 | // Otherwise, the va_list argument must be an l-value because |
| 10011 | // it is modified by va_arg. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10012 | if (!E->isTypeDependent() && |
Douglas Gregor | ad3150c | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 10013 | CheckForModifiableLvalue(E, BuiltinLoc, *this)) |
Eli Friedman | e2cad65 | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 10014 | return ExprError(); |
| 10015 | } |
Eli Friedman | 121ba0c | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 10016 | |
Douglas Gregor | ad3150c | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 10017 | if (!E->isTypeDependent() && |
| 10018 | !Context.hasSameType(VaListType, E->getType())) { |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 10019 | return ExprError(Diag(E->getLocStart(), |
| 10020 | diag::err_first_argument_to_va_arg_not_of_type_va_list) |
Chris Lattner | 56382aa | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 10021 | << OrigExpr->getType() << E->getSourceRange()); |
Chris Lattner | 3f5cd77 | 2009-04-05 00:59:53 +0000 | [diff] [blame] | 10022 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 10023 | |
David Majnemer | c75d1a1 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 10024 | if (!TInfo->getType()->isDependentType()) { |
| 10025 | if (RequireCompleteType(TInfo->getTypeLoc().getBeginLoc(), TInfo->getType(), |
| 10026 | PDiag(diag::err_second_parameter_to_va_arg_incomplete) |
| 10027 | << TInfo->getTypeLoc().getSourceRange())) |
| 10028 | return ExprError(); |
David Majnemer | 254a5c0 | 2011-06-13 06:37:03 +0000 | [diff] [blame] | 10029 | |
David Majnemer | c75d1a1 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 10030 | if (RequireNonAbstractType(TInfo->getTypeLoc().getBeginLoc(), |
| 10031 | TInfo->getType(), |
| 10032 | PDiag(diag::err_second_parameter_to_va_arg_abstract) |
| 10033 | << TInfo->getTypeLoc().getSourceRange())) |
| 10034 | return ExprError(); |
| 10035 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10036 | if (!TInfo->getType().isPODType(Context)) |
David Majnemer | c75d1a1 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 10037 | Diag(TInfo->getTypeLoc().getBeginLoc(), |
| 10038 | diag::warn_second_parameter_to_va_arg_not_pod) |
| 10039 | << TInfo->getType() |
| 10040 | << TInfo->getTypeLoc().getSourceRange(); |
| 10041 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 10042 | |
Abramo Bagnara | 27db239 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 10043 | QualType T = TInfo->getType().getNonLValueExprType(Context); |
| 10044 | return Owned(new (Context) VAArgExpr(BuiltinLoc, E, TInfo, RPLoc, T)); |
Anders Carlsson | 7e13ab8 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 10045 | } |
| 10046 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10047 | ExprResult Sema::ActOnGNUNullExpr(SourceLocation TokenLoc) { |
Douglas Gregor | 3be4b12 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 10048 | // The type of __null will be int or long, depending on the size of |
| 10049 | // pointers on the target. |
| 10050 | QualType Ty; |
NAKAMURA Takumi | 0d13fd3 | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 10051 | unsigned pw = Context.Target.getPointerWidth(0); |
| 10052 | if (pw == Context.Target.getIntWidth()) |
Douglas Gregor | 3be4b12 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 10053 | Ty = Context.IntTy; |
NAKAMURA Takumi | 0d13fd3 | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 10054 | else if (pw == Context.Target.getLongWidth()) |
Douglas Gregor | 3be4b12 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 10055 | Ty = Context.LongTy; |
NAKAMURA Takumi | 0d13fd3 | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 10056 | else if (pw == Context.Target.getLongLongWidth()) |
| 10057 | Ty = Context.LongLongTy; |
| 10058 | else { |
| 10059 | assert(!"I don't know size of pointer!"); |
| 10060 | Ty = Context.IntTy; |
| 10061 | } |
Douglas Gregor | 3be4b12 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 10062 | |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 10063 | return Owned(new (Context) GNUNullExpr(Ty, TokenLoc)); |
Douglas Gregor | 3be4b12 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 10064 | } |
| 10065 | |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 10066 | static void MakeObjCStringLiteralFixItHint(Sema& SemaRef, QualType DstType, |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 10067 | Expr *SrcExpr, FixItHint &Hint) { |
Anders Carlsson | ace5d07 | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 10068 | if (!SemaRef.getLangOptions().ObjC1) |
| 10069 | return; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10070 | |
Anders Carlsson | ace5d07 | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 10071 | const ObjCObjectPointerType *PT = DstType->getAs<ObjCObjectPointerType>(); |
| 10072 | if (!PT) |
| 10073 | return; |
| 10074 | |
| 10075 | // Check if the destination is of type 'id'. |
| 10076 | if (!PT->isObjCIdType()) { |
| 10077 | // Check if the destination is the 'NSString' interface. |
| 10078 | const ObjCInterfaceDecl *ID = PT->getInterfaceDecl(); |
| 10079 | if (!ID || !ID->getIdentifier()->isStr("NSString")) |
| 10080 | return; |
| 10081 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10082 | |
Anders Carlsson | ace5d07 | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 10083 | // Strip off any parens and casts. |
| 10084 | StringLiteral *SL = dyn_cast<StringLiteral>(SrcExpr->IgnoreParenCasts()); |
| 10085 | if (!SL || SL->isWide()) |
| 10086 | return; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10087 | |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 10088 | Hint = FixItHint::CreateInsertion(SL->getLocStart(), "@"); |
Anders Carlsson | ace5d07 | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 10089 | } |
| 10090 | |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 10091 | bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, |
| 10092 | SourceLocation Loc, |
| 10093 | QualType DstType, QualType SrcType, |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 10094 | Expr *SrcExpr, AssignmentAction Action, |
| 10095 | bool *Complained) { |
| 10096 | if (Complained) |
| 10097 | *Complained = false; |
| 10098 | |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 10099 | // Decode the result (notice that AST's are still created for extensions). |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 10100 | bool CheckInferredResultType = false; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 10101 | bool isInvalid = false; |
| 10102 | unsigned DiagKind; |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 10103 | FixItHint Hint; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10104 | |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 10105 | switch (ConvTy) { |
| 10106 | default: assert(0 && "Unknown conversion type"); |
| 10107 | case Compatible: return false; |
Chris Lattner | 940cfeb | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 10108 | case PointerToInt: |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 10109 | DiagKind = diag::ext_typecheck_convert_pointer_int; |
| 10110 | break; |
Chris Lattner | 940cfeb | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 10111 | case IntToPointer: |
| 10112 | DiagKind = diag::ext_typecheck_convert_int_pointer; |
| 10113 | break; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 10114 | case IncompatiblePointer: |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 10115 | MakeObjCStringLiteralFixItHint(*this, DstType, SrcExpr, Hint); |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 10116 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer; |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 10117 | CheckInferredResultType = DstType->isObjCObjectPointerType() && |
| 10118 | SrcType->isObjCObjectPointerType(); |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 10119 | break; |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 10120 | case IncompatiblePointerSign: |
| 10121 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer_sign; |
| 10122 | break; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 10123 | case FunctionVoidPointer: |
| 10124 | DiagKind = diag::ext_typecheck_convert_pointer_void_func; |
| 10125 | break; |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 10126 | case IncompatiblePointerDiscardsQualifiers: { |
John McCall | 71de91c | 2011-02-01 23:28:01 +0000 | [diff] [blame] | 10127 | // Perform array-to-pointer decay if necessary. |
| 10128 | if (SrcType->isArrayType()) SrcType = Context.getArrayDecayedType(SrcType); |
| 10129 | |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 10130 | Qualifiers lhq = SrcType->getPointeeType().getQualifiers(); |
| 10131 | Qualifiers rhq = DstType->getPointeeType().getQualifiers(); |
| 10132 | if (lhq.getAddressSpace() != rhq.getAddressSpace()) { |
| 10133 | DiagKind = diag::err_typecheck_incompatible_address_space; |
| 10134 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10135 | |
| 10136 | |
| 10137 | } else if (lhq.getObjCLifetime() != rhq.getObjCLifetime()) { |
| 10138 | DiagKind = diag::err_typecheck_incompatible_lifetime; |
| 10139 | break; |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 10140 | } |
| 10141 | |
| 10142 | llvm_unreachable("unknown error case for discarding qualifiers!"); |
| 10143 | // fallthrough |
| 10144 | } |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 10145 | case CompatiblePointerDiscardsQualifiers: |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 10146 | // If the qualifiers lost were because we were applying the |
| 10147 | // (deprecated) C++ conversion from a string literal to a char* |
| 10148 | // (or wchar_t*), then there was no error (C++ 4.2p2). FIXME: |
| 10149 | // Ideally, this check would be performed in |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 10150 | // checkPointerTypesForAssignment. However, that would require a |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 10151 | // bit of refactoring (so that the second argument is an |
| 10152 | // expression, rather than a type), which should be done as part |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 10153 | // of a larger effort to fix checkPointerTypesForAssignment for |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 10154 | // C++ semantics. |
| 10155 | if (getLangOptions().CPlusPlus && |
| 10156 | IsStringLiteralToNonConstPointerConversion(SrcExpr, DstType)) |
| 10157 | return false; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 10158 | DiagKind = diag::ext_typecheck_convert_discards_qualifiers; |
| 10159 | break; |
Alexis Hunt | 6f3de50 | 2009-11-08 07:46:34 +0000 | [diff] [blame] | 10160 | case IncompatibleNestedPointerQualifiers: |
Fariborz Jahanian | b98dade | 2009-11-09 22:16:37 +0000 | [diff] [blame] | 10161 | DiagKind = diag::ext_nested_pointer_qualifier_mismatch; |
Fariborz Jahanian | d7aa9d8 | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 10162 | break; |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 10163 | case IntToBlockPointer: |
| 10164 | DiagKind = diag::err_int_to_block_pointer; |
| 10165 | break; |
| 10166 | case IncompatibleBlockPointer: |
Mike Stump | d79b5a8 | 2009-04-21 22:51:42 +0000 | [diff] [blame] | 10167 | DiagKind = diag::err_typecheck_convert_incompatible_block_pointer; |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 10168 | break; |
Steve Naroff | 8afa989 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 10169 | case IncompatibleObjCQualifiedId: |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 10170 | // FIXME: Diagnose the problem in ObjCQualifiedIdTypesAreCompatible, since |
Steve Naroff | 8afa989 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 10171 | // it can give a more specific diagnostic. |
| 10172 | DiagKind = diag::warn_incompatible_qualified_id; |
| 10173 | break; |
Anders Carlsson | db5a9b6 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 10174 | case IncompatibleVectors: |
| 10175 | DiagKind = diag::warn_incompatible_vectors; |
| 10176 | break; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 10177 | case Incompatible: |
| 10178 | DiagKind = diag::err_typecheck_convert_incompatible; |
| 10179 | isInvalid = true; |
| 10180 | break; |
| 10181 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 10182 | |
Douglas Gregor | c68e140 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 10183 | QualType FirstType, SecondType; |
| 10184 | switch (Action) { |
| 10185 | case AA_Assigning: |
| 10186 | case AA_Initializing: |
| 10187 | // The destination type comes first. |
| 10188 | FirstType = DstType; |
| 10189 | SecondType = SrcType; |
| 10190 | break; |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 10191 | |
Douglas Gregor | c68e140 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 10192 | case AA_Returning: |
| 10193 | case AA_Passing: |
| 10194 | case AA_Converting: |
| 10195 | case AA_Sending: |
| 10196 | case AA_Casting: |
| 10197 | // The source type comes first. |
| 10198 | FirstType = SrcType; |
| 10199 | SecondType = DstType; |
| 10200 | break; |
| 10201 | } |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 10202 | |
Douglas Gregor | c68e140 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 10203 | Diag(Loc, DiagKind) << FirstType << SecondType << Action |
Anders Carlsson | ace5d07 | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 10204 | << SrcExpr->getSourceRange() << Hint; |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 10205 | if (CheckInferredResultType) |
| 10206 | EmitRelatedResultTypeNote(SrcExpr); |
| 10207 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 10208 | if (Complained) |
| 10209 | *Complained = true; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 10210 | return isInvalid; |
| 10211 | } |
Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 10212 | |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 10213 | bool Sema::VerifyIntegerConstantExpression(const Expr *E, llvm::APSInt *Result){ |
Eli Friedman | bb967cc | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 10214 | llvm::APSInt ICEResult; |
| 10215 | if (E->isIntegerConstantExpr(ICEResult, Context)) { |
| 10216 | if (Result) |
| 10217 | *Result = ICEResult; |
| 10218 | return false; |
| 10219 | } |
| 10220 | |
Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 10221 | Expr::EvalResult EvalResult; |
| 10222 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 10223 | if (!E->Evaluate(EvalResult, Context) || !EvalResult.Val.isInt() || |
Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 10224 | EvalResult.HasSideEffects) { |
| 10225 | Diag(E->getExprLoc(), diag::err_expr_not_ice) << E->getSourceRange(); |
| 10226 | |
| 10227 | if (EvalResult.Diag) { |
| 10228 | // We only show the note if it's not the usual "invalid subexpression" |
| 10229 | // or if it's actually in a subexpression. |
| 10230 | if (EvalResult.Diag != diag::note_invalid_subexpr_in_ice || |
| 10231 | E->IgnoreParens() != EvalResult.DiagExpr->IgnoreParens()) |
| 10232 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
| 10233 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 10234 | |
Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 10235 | return true; |
| 10236 | } |
| 10237 | |
Eli Friedman | bb967cc | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 10238 | Diag(E->getExprLoc(), diag::ext_expr_not_ice) << |
| 10239 | E->getSourceRange(); |
Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 10240 | |
Eli Friedman | bb967cc | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 10241 | if (EvalResult.Diag && |
Argyrios Kyrtzidis | 1cb0de1 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 10242 | Diags.getDiagnosticLevel(diag::ext_expr_not_ice, EvalResult.DiagLoc) |
| 10243 | != Diagnostic::Ignored) |
Eli Friedman | bb967cc | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 10244 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 10245 | |
Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 10246 | if (Result) |
| 10247 | *Result = EvalResult.Val.getInt(); |
| 10248 | return false; |
| 10249 | } |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10250 | |
Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 10251 | void |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10252 | Sema::PushExpressionEvaluationContext(ExpressionEvaluationContext NewContext) { |
Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 10253 | ExprEvalContexts.push_back( |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10254 | ExpressionEvaluationContextRecord(NewContext, |
| 10255 | ExprTemporaries.size(), |
| 10256 | ExprNeedsCleanups)); |
| 10257 | ExprNeedsCleanups = false; |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 10258 | } |
| 10259 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10260 | void |
Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 10261 | Sema::PopExpressionEvaluationContext() { |
| 10262 | // Pop the current expression evaluation context off the stack. |
| 10263 | ExpressionEvaluationContextRecord Rec = ExprEvalContexts.back(); |
| 10264 | ExprEvalContexts.pop_back(); |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 10265 | |
Douglas Gregor | fab31f4 | 2009-12-12 07:57:52 +0000 | [diff] [blame] | 10266 | if (Rec.Context == PotentiallyPotentiallyEvaluated) { |
| 10267 | if (Rec.PotentiallyReferenced) { |
| 10268 | // Mark any remaining declarations in the current position of the stack |
| 10269 | // as "referenced". If they were not meant to be referenced, semantic |
| 10270 | // analysis would have eliminated them (e.g., in ActOnCXXTypeId). |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10271 | for (PotentiallyReferencedDecls::iterator |
Douglas Gregor | fab31f4 | 2009-12-12 07:57:52 +0000 | [diff] [blame] | 10272 | I = Rec.PotentiallyReferenced->begin(), |
| 10273 | IEnd = Rec.PotentiallyReferenced->end(); |
| 10274 | I != IEnd; ++I) |
| 10275 | MarkDeclarationReferenced(I->first, I->second); |
| 10276 | } |
| 10277 | |
| 10278 | if (Rec.PotentiallyDiagnosed) { |
| 10279 | // Emit any pending diagnostics. |
| 10280 | for (PotentiallyEmittedDiagnostics::iterator |
| 10281 | I = Rec.PotentiallyDiagnosed->begin(), |
| 10282 | IEnd = Rec.PotentiallyDiagnosed->end(); |
| 10283 | I != IEnd; ++I) |
| 10284 | Diag(I->first, I->second); |
| 10285 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10286 | } |
Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 10287 | |
| 10288 | // When are coming out of an unevaluated context, clear out any |
| 10289 | // temporaries that we may have created as part of the evaluation of |
| 10290 | // the expression in that context: they aren't relevant because they |
| 10291 | // will never be constructed. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10292 | if (Rec.Context == Unevaluated) { |
Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 10293 | ExprTemporaries.erase(ExprTemporaries.begin() + Rec.NumTemporaries, |
| 10294 | ExprTemporaries.end()); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10295 | ExprNeedsCleanups = Rec.ParentNeedsCleanups; |
| 10296 | |
| 10297 | // Otherwise, merge the contexts together. |
| 10298 | } else { |
| 10299 | ExprNeedsCleanups |= Rec.ParentNeedsCleanups; |
| 10300 | } |
Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 10301 | |
| 10302 | // Destroy the popped expression evaluation record. |
| 10303 | Rec.Destroy(); |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 10304 | } |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10305 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10306 | void Sema::DiscardCleanupsInEvaluationContext() { |
| 10307 | ExprTemporaries.erase( |
| 10308 | ExprTemporaries.begin() + ExprEvalContexts.back().NumTemporaries, |
| 10309 | ExprTemporaries.end()); |
| 10310 | ExprNeedsCleanups = false; |
| 10311 | } |
| 10312 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10313 | /// \brief Note that the given declaration was referenced in the source code. |
| 10314 | /// |
| 10315 | /// This routine should be invoke whenever a given declaration is referenced |
| 10316 | /// in the source code, and where that reference occurred. If this declaration |
| 10317 | /// reference means that the the declaration is used (C++ [basic.def.odr]p2, |
| 10318 | /// C99 6.9p3), then the declaration will be marked as used. |
| 10319 | /// |
| 10320 | /// \param Loc the location where the declaration was referenced. |
| 10321 | /// |
| 10322 | /// \param D the declaration that has been referenced by the source code. |
| 10323 | void Sema::MarkDeclarationReferenced(SourceLocation Loc, Decl *D) { |
| 10324 | assert(D && "No declaration?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10325 | |
Argyrios Kyrtzidis | 1618023 | 2011-04-19 19:51:10 +0000 | [diff] [blame] | 10326 | D->setReferenced(); |
| 10327 | |
Douglas Gregor | ebada077 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 10328 | if (D->isUsed(false)) |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 10329 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10330 | |
Douglas Gregor | 3beaf9b | 2009-10-08 21:35:42 +0000 | [diff] [blame] | 10331 | // Mark a parameter or variable declaration "used", regardless of whether we're in a |
| 10332 | // template or not. The reason for this is that unevaluated expressions |
| 10333 | // (e.g. (void)sizeof()) constitute a use for warning purposes (-Wunused-variables and |
| 10334 | // -Wunused-parameters) |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10335 | if (isa<ParmVarDecl>(D) || |
Douglas Gregor | fd27fed | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 10336 | (isa<VarDecl>(D) && D->getDeclContext()->isFunctionOrMethod())) { |
Anders Carlsson | 73067a0 | 2010-10-22 23:37:08 +0000 | [diff] [blame] | 10337 | D->setUsed(); |
Douglas Gregor | fd27fed | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 10338 | return; |
| 10339 | } |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 10340 | |
Douglas Gregor | fd27fed | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 10341 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) |
| 10342 | return; |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 10343 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10344 | // Do not mark anything as "used" within a dependent context; wait for |
| 10345 | // an instantiation. |
| 10346 | if (CurContext->isDependentContext()) |
| 10347 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10348 | |
Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 10349 | switch (ExprEvalContexts.back().Context) { |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 10350 | case Unevaluated: |
| 10351 | // We are in an expression that is not potentially evaluated; do nothing. |
| 10352 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10353 | |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 10354 | case PotentiallyEvaluated: |
| 10355 | // We are in a potentially-evaluated expression, so this declaration is |
| 10356 | // "used"; handle this below. |
| 10357 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10358 | |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 10359 | case PotentiallyPotentiallyEvaluated: |
| 10360 | // We are in an expression that may be potentially evaluated; queue this |
| 10361 | // declaration reference until we know whether the expression is |
| 10362 | // potentially evaluated. |
Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 10363 | ExprEvalContexts.back().addReferencedDecl(Loc, D); |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 10364 | return; |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10365 | |
| 10366 | case PotentiallyEvaluatedIfUsed: |
| 10367 | // Referenced declarations will only be used if the construct in the |
| 10368 | // containing expression is used. |
| 10369 | return; |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 10370 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10371 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10372 | // Note that this declaration has been used. |
Fariborz Jahanian | 3a36343 | 2009-06-22 17:30:33 +0000 | [diff] [blame] | 10373 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
Alexis Hunt | f92197c | 2011-05-12 03:51:51 +0000 | [diff] [blame] | 10374 | if (Constructor->isDefaulted() && Constructor->isDefaultConstructor()) { |
| 10375 | if (Constructor->isTrivial()) |
Chandler Carruth | c926240 | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 10376 | return; |
| 10377 | if (!Constructor->isUsed(false)) |
| 10378 | DefineImplicitDefaultConstructor(Loc, Constructor); |
Alexis Hunt | 22b5b13 | 2011-05-14 18:20:50 +0000 | [diff] [blame] | 10379 | } else if (Constructor->isDefaulted() && |
Alexis Hunt | 913820d | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 10380 | Constructor->isCopyConstructor()) { |
Douglas Gregor | ebada077 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 10381 | if (!Constructor->isUsed(false)) |
Alexis Hunt | 913820d | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 10382 | DefineImplicitCopyConstructor(Loc, Constructor); |
Fariborz Jahanian | 477d242 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 10383 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10384 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10385 | MarkVTableUsed(Loc, Constructor->getParent()); |
Fariborz Jahanian | 24a175b | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 10386 | } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) { |
Alexis Hunt | f9172946 | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 10387 | if (Destructor->isDefaulted() && !Destructor->isUsed(false)) |
Fariborz Jahanian | 24a175b | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 10388 | DefineImplicitDestructor(Loc, Destructor); |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10389 | if (Destructor->isVirtual()) |
| 10390 | MarkVTableUsed(Loc, Destructor->getParent()); |
Fariborz Jahanian | 41f7927 | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 10391 | } else if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) { |
Alexis Hunt | c9a5573 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 10392 | if (MethodDecl->isDefaulted() && MethodDecl->isOverloadedOperator() && |
Fariborz Jahanian | 41f7927 | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 10393 | MethodDecl->getOverloadedOperator() == OO_Equal) { |
Douglas Gregor | ebada077 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 10394 | if (!MethodDecl->isUsed(false)) |
Douglas Gregor | a57478e | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 10395 | DefineImplicitCopyAssignment(Loc, MethodDecl); |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10396 | } else if (MethodDecl->isVirtual()) |
| 10397 | MarkVTableUsed(Loc, MethodDecl->getParent()); |
Fariborz Jahanian | 41f7927 | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 10398 | } |
Fariborz Jahanian | 49796cc7 | 2009-06-24 22:09:44 +0000 | [diff] [blame] | 10399 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
John McCall | 8377967 | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 10400 | // Recursive functions should be marked when used from another function. |
| 10401 | if (CurContext == Function) return; |
| 10402 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10403 | // Implicit instantiation of function templates and member functions of |
Douglas Gregor | 4adbc6d | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 10404 | // class templates. |
Douglas Gregor | 69f6a36 | 2010-05-17 17:34:56 +0000 | [diff] [blame] | 10405 | if (Function->isImplicitlyInstantiable()) { |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 10406 | bool AlreadyInstantiated = false; |
| 10407 | if (FunctionTemplateSpecializationInfo *SpecInfo |
| 10408 | = Function->getTemplateSpecializationInfo()) { |
| 10409 | if (SpecInfo->getPointOfInstantiation().isInvalid()) |
| 10410 | SpecInfo->setPointOfInstantiation(Loc); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10411 | else if (SpecInfo->getTemplateSpecializationKind() |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 10412 | == TSK_ImplicitInstantiation) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 10413 | AlreadyInstantiated = true; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10414 | } else if (MemberSpecializationInfo *MSInfo |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 10415 | = Function->getMemberSpecializationInfo()) { |
| 10416 | if (MSInfo->getPointOfInstantiation().isInvalid()) |
| 10417 | MSInfo->setPointOfInstantiation(Loc); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10418 | else if (MSInfo->getTemplateSpecializationKind() |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 10419 | == TSK_ImplicitInstantiation) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 10420 | AlreadyInstantiated = true; |
| 10421 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10422 | |
Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 10423 | if (!AlreadyInstantiated) { |
| 10424 | if (isa<CXXRecordDecl>(Function->getDeclContext()) && |
| 10425 | cast<CXXRecordDecl>(Function->getDeclContext())->isLocalClass()) |
| 10426 | PendingLocalImplicitInstantiations.push_back(std::make_pair(Function, |
| 10427 | Loc)); |
| 10428 | else |
Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 10429 | PendingInstantiations.push_back(std::make_pair(Function, Loc)); |
Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 10430 | } |
John McCall | 8377967 | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 10431 | } else { |
| 10432 | // Walk redefinitions, as some of them may be instantiable. |
Gabor Greif | b6aba3e | 2010-08-28 00:16:06 +0000 | [diff] [blame] | 10433 | for (FunctionDecl::redecl_iterator i(Function->redecls_begin()), |
| 10434 | e(Function->redecls_end()); i != e; ++i) { |
Gabor Greif | 34ecff2 | 2010-08-28 01:58:12 +0000 | [diff] [blame] | 10435 | if (!i->isUsed(false) && i->isImplicitlyInstantiable()) |
Gabor Greif | b6aba3e | 2010-08-28 00:16:06 +0000 | [diff] [blame] | 10436 | MarkDeclarationReferenced(Loc, *i); |
| 10437 | } |
John McCall | 8377967 | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 10438 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10439 | |
John McCall | 8377967 | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 10440 | // Keep track of used but undefined functions. |
| 10441 | if (!Function->isPure() && !Function->hasBody() && |
| 10442 | Function->getLinkage() != ExternalLinkage) { |
| 10443 | SourceLocation &old = UndefinedInternals[Function->getCanonicalDecl()]; |
| 10444 | if (old.isInvalid()) old = Loc; |
| 10445 | } |
Argyrios Kyrtzidis | dfffabd | 2010-08-25 10:34:54 +0000 | [diff] [blame] | 10446 | |
John McCall | 8377967 | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 10447 | Function->setUsed(true); |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10448 | return; |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 10449 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10450 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10451 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) { |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 10452 | // Implicit instantiation of static data members of class templates. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10453 | if (Var->isStaticDataMember() && |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 10454 | Var->getInstantiatedFromStaticDataMember()) { |
| 10455 | MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo(); |
| 10456 | assert(MSInfo && "Missing member specialization information?"); |
| 10457 | if (MSInfo->getPointOfInstantiation().isInvalid() && |
| 10458 | MSInfo->getTemplateSpecializationKind()== TSK_ImplicitInstantiation) { |
| 10459 | MSInfo->setPointOfInstantiation(Loc); |
Sebastian Redl | 2ac2c72 | 2011-04-29 08:19:30 +0000 | [diff] [blame] | 10460 | // This is a modification of an existing AST node. Notify listeners. |
| 10461 | if (ASTMutationListener *L = getASTMutationListener()) |
| 10462 | L->StaticDataMemberInstantiated(Var); |
Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 10463 | PendingInstantiations.push_back(std::make_pair(Var, Loc)); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 10464 | } |
| 10465 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10466 | |
John McCall | 15dd404 | 2011-02-21 19:25:48 +0000 | [diff] [blame] | 10467 | // Keep track of used but undefined variables. We make a hole in |
| 10468 | // the warning for static const data members with in-line |
| 10469 | // initializers. |
John McCall | 8377967 | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 10470 | if (Var->hasDefinition() == VarDecl::DeclarationOnly |
John McCall | 15dd404 | 2011-02-21 19:25:48 +0000 | [diff] [blame] | 10471 | && Var->getLinkage() != ExternalLinkage |
| 10472 | && !(Var->isStaticDataMember() && Var->hasInit())) { |
John McCall | 8377967 | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 10473 | SourceLocation &old = UndefinedInternals[Var->getCanonicalDecl()]; |
| 10474 | if (old.isInvalid()) old = Loc; |
| 10475 | } |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 10476 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10477 | D->setUsed(true); |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 10478 | return; |
Sam Weinig | bae6914 | 2009-09-11 03:29:30 +0000 | [diff] [blame] | 10479 | } |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10480 | } |
Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 10481 | |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10482 | namespace { |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10483 | // Mark all of the declarations referenced |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10484 | // FIXME: Not fully implemented yet! We need to have a better understanding |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10485 | // of when we're entering |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10486 | class MarkReferencedDecls : public RecursiveASTVisitor<MarkReferencedDecls> { |
| 10487 | Sema &S; |
| 10488 | SourceLocation Loc; |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10489 | |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10490 | public: |
| 10491 | typedef RecursiveASTVisitor<MarkReferencedDecls> Inherited; |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10492 | |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10493 | MarkReferencedDecls(Sema &S, SourceLocation Loc) : S(S), Loc(Loc) { } |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10494 | |
| 10495 | bool TraverseTemplateArgument(const TemplateArgument &Arg); |
| 10496 | bool TraverseRecordType(RecordType *T); |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10497 | }; |
| 10498 | } |
| 10499 | |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10500 | bool MarkReferencedDecls::TraverseTemplateArgument( |
| 10501 | const TemplateArgument &Arg) { |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10502 | if (Arg.getKind() == TemplateArgument::Declaration) { |
| 10503 | S.MarkDeclarationReferenced(Loc, Arg.getAsDecl()); |
| 10504 | } |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10505 | |
| 10506 | return Inherited::TraverseTemplateArgument(Arg); |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10507 | } |
| 10508 | |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10509 | bool MarkReferencedDecls::TraverseRecordType(RecordType *T) { |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10510 | if (ClassTemplateSpecializationDecl *Spec |
| 10511 | = dyn_cast<ClassTemplateSpecializationDecl>(T->getDecl())) { |
| 10512 | const TemplateArgumentList &Args = Spec->getTemplateArgs(); |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 10513 | return TraverseTemplateArguments(Args.data(), Args.size()); |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10514 | } |
| 10515 | |
Chandler Carruth | c65667c | 2010-06-10 10:31:57 +0000 | [diff] [blame] | 10516 | return true; |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10517 | } |
| 10518 | |
| 10519 | void Sema::MarkDeclarationsReferencedInType(SourceLocation Loc, QualType T) { |
| 10520 | MarkReferencedDecls Marker(*this, Loc); |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10521 | Marker.TraverseType(Context.getCanonicalType(T)); |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10522 | } |
| 10523 | |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10524 | namespace { |
| 10525 | /// \brief Helper class that marks all of the declarations referenced by |
| 10526 | /// potentially-evaluated subexpressions as "referenced". |
| 10527 | class EvaluatedExprMarker : public EvaluatedExprVisitor<EvaluatedExprMarker> { |
| 10528 | Sema &S; |
| 10529 | |
| 10530 | public: |
| 10531 | typedef EvaluatedExprVisitor<EvaluatedExprMarker> Inherited; |
| 10532 | |
| 10533 | explicit EvaluatedExprMarker(Sema &S) : Inherited(S.Context), S(S) { } |
| 10534 | |
| 10535 | void VisitDeclRefExpr(DeclRefExpr *E) { |
| 10536 | S.MarkDeclarationReferenced(E->getLocation(), E->getDecl()); |
| 10537 | } |
| 10538 | |
| 10539 | void VisitMemberExpr(MemberExpr *E) { |
| 10540 | S.MarkDeclarationReferenced(E->getMemberLoc(), E->getMemberDecl()); |
Douglas Gregor | 32b3de5 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 10541 | Inherited::VisitMemberExpr(E); |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10542 | } |
| 10543 | |
| 10544 | void VisitCXXNewExpr(CXXNewExpr *E) { |
| 10545 | if (E->getConstructor()) |
| 10546 | S.MarkDeclarationReferenced(E->getLocStart(), E->getConstructor()); |
| 10547 | if (E->getOperatorNew()) |
| 10548 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorNew()); |
| 10549 | if (E->getOperatorDelete()) |
| 10550 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorDelete()); |
Douglas Gregor | 32b3de5 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 10551 | Inherited::VisitCXXNewExpr(E); |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10552 | } |
| 10553 | |
| 10554 | void VisitCXXDeleteExpr(CXXDeleteExpr *E) { |
| 10555 | if (E->getOperatorDelete()) |
| 10556 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorDelete()); |
Douglas Gregor | 6ed2fee | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 10557 | QualType Destroyed = S.Context.getBaseElementType(E->getDestroyedType()); |
| 10558 | if (const RecordType *DestroyedRec = Destroyed->getAs<RecordType>()) { |
| 10559 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DestroyedRec->getDecl()); |
| 10560 | S.MarkDeclarationReferenced(E->getLocStart(), |
| 10561 | S.LookupDestructor(Record)); |
| 10562 | } |
| 10563 | |
Douglas Gregor | 32b3de5 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 10564 | Inherited::VisitCXXDeleteExpr(E); |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10565 | } |
| 10566 | |
| 10567 | void VisitCXXConstructExpr(CXXConstructExpr *E) { |
| 10568 | S.MarkDeclarationReferenced(E->getLocStart(), E->getConstructor()); |
Douglas Gregor | 32b3de5 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 10569 | Inherited::VisitCXXConstructExpr(E); |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10570 | } |
| 10571 | |
| 10572 | void VisitBlockDeclRefExpr(BlockDeclRefExpr *E) { |
| 10573 | S.MarkDeclarationReferenced(E->getLocation(), E->getDecl()); |
| 10574 | } |
Douglas Gregor | f0873f4 | 2010-10-19 17:17:35 +0000 | [diff] [blame] | 10575 | |
| 10576 | void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { |
| 10577 | Visit(E->getExpr()); |
| 10578 | } |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10579 | }; |
| 10580 | } |
| 10581 | |
| 10582 | /// \brief Mark any declarations that appear within this expression or any |
| 10583 | /// potentially-evaluated subexpressions as "referenced". |
| 10584 | void Sema::MarkDeclarationsReferencedInExpr(Expr *E) { |
| 10585 | EvaluatedExprMarker(*this).Visit(E); |
| 10586 | } |
| 10587 | |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10588 | /// \brief Emit a diagnostic that describes an effect on the run-time behavior |
| 10589 | /// of the program being compiled. |
| 10590 | /// |
| 10591 | /// This routine emits the given diagnostic when the code currently being |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10592 | /// type-checked is "potentially evaluated", meaning that there is a |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10593 | /// possibility that the code will actually be executable. Code in sizeof() |
| 10594 | /// expressions, code used only during overload resolution, etc., are not |
| 10595 | /// potentially evaluated. This routine will suppress such diagnostics or, |
| 10596 | /// in the absolutely nutty case of potentially potentially evaluated |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10597 | /// expressions (C++ typeid), queue the diagnostic to potentially emit it |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10598 | /// later. |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10599 | /// |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10600 | /// This routine should be used for all diagnostics that describe the run-time |
| 10601 | /// behavior of a program, such as passing a non-POD value through an ellipsis. |
| 10602 | /// Failure to do so will likely result in spurious diagnostics or failures |
| 10603 | /// during overload resolution or within sizeof/alignof/typeof/typeid. |
Ted Kremenek | 55ae319 | 2011-02-23 01:51:43 +0000 | [diff] [blame] | 10604 | bool Sema::DiagRuntimeBehavior(SourceLocation Loc, const Stmt *stmt, |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10605 | const PartialDiagnostic &PD) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10606 | switch (ExprEvalContexts.back().Context) { |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10607 | case Unevaluated: |
| 10608 | // The argument will never be evaluated, so don't complain. |
| 10609 | break; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10610 | |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10611 | case PotentiallyEvaluated: |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10612 | case PotentiallyEvaluatedIfUsed: |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 10613 | if (stmt && getCurFunctionOrMethodDecl()) { |
| 10614 | FunctionScopes.back()->PossiblyUnreachableDiags. |
| 10615 | push_back(sema::PossiblyUnreachableDiag(PD, Loc, stmt)); |
| 10616 | } |
| 10617 | else |
| 10618 | Diag(Loc, PD); |
| 10619 | |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10620 | return true; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10621 | |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 10622 | case PotentiallyPotentiallyEvaluated: |
| 10623 | ExprEvalContexts.back().addDiagnostic(Loc, PD); |
| 10624 | break; |
| 10625 | } |
| 10626 | |
| 10627 | return false; |
| 10628 | } |
| 10629 | |
Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 10630 | bool Sema::CheckCallReturnType(QualType ReturnType, SourceLocation Loc, |
| 10631 | CallExpr *CE, FunctionDecl *FD) { |
| 10632 | if (ReturnType->isVoidType() || !ReturnType->isIncompleteType()) |
| 10633 | return false; |
| 10634 | |
| 10635 | PartialDiagnostic Note = |
| 10636 | FD ? PDiag(diag::note_function_with_incomplete_return_type_declared_here) |
| 10637 | << FD->getDeclName() : PDiag(); |
| 10638 | SourceLocation NoteLoc = FD ? FD->getLocation() : SourceLocation(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10639 | |
Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 10640 | if (RequireCompleteType(Loc, ReturnType, |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10641 | FD ? |
Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 10642 | PDiag(diag::err_call_function_incomplete_return) |
| 10643 | << CE->getSourceRange() << FD->getDeclName() : |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10644 | PDiag(diag::err_call_incomplete_return) |
Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 10645 | << CE->getSourceRange(), |
| 10646 | std::make_pair(NoteLoc, Note))) |
| 10647 | return true; |
| 10648 | |
| 10649 | return false; |
| 10650 | } |
| 10651 | |
Douglas Gregor | 2d4f64f | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10652 | // Diagnose the s/=/==/ and s/\|=/!=/ typos. Note that adding parentheses |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10653 | // will prevent this condition from triggering, which is what we want. |
| 10654 | void Sema::DiagnoseAssignmentAsCondition(Expr *E) { |
| 10655 | SourceLocation Loc; |
| 10656 | |
John McCall | 0506e4a | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 10657 | unsigned diagnostic = diag::warn_condition_is_assignment; |
Douglas Gregor | 2d4f64f | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10658 | bool IsOrAssign = false; |
John McCall | 0506e4a | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 10659 | |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10660 | if (isa<BinaryOperator>(E)) { |
| 10661 | BinaryOperator *Op = cast<BinaryOperator>(E); |
Douglas Gregor | 2d4f64f | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10662 | if (Op->getOpcode() != BO_Assign && Op->getOpcode() != BO_OrAssign) |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10663 | return; |
| 10664 | |
Douglas Gregor | 2d4f64f | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10665 | IsOrAssign = Op->getOpcode() == BO_OrAssign; |
| 10666 | |
John McCall | b0e419e | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 10667 | // Greylist some idioms by putting them into a warning subcategory. |
| 10668 | if (ObjCMessageExpr *ME |
| 10669 | = dyn_cast<ObjCMessageExpr>(Op->getRHS()->IgnoreParenCasts())) { |
| 10670 | Selector Sel = ME->getSelector(); |
| 10671 | |
John McCall | b0e419e | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 10672 | // self = [<foo> init...] |
Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 10673 | if (isSelfExpr(Op->getLHS()) && Sel.getNameForSlot(0).startswith("init")) |
John McCall | b0e419e | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 10674 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 10675 | |
| 10676 | // <foo> = [<bar> nextObject] |
Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 10677 | else if (Sel.isUnarySelector() && Sel.getNameForSlot(0) == "nextObject") |
John McCall | b0e419e | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 10678 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 10679 | } |
John McCall | 0506e4a | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 10680 | |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10681 | Loc = Op->getOperatorLoc(); |
| 10682 | } else if (isa<CXXOperatorCallExpr>(E)) { |
| 10683 | CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(E); |
Douglas Gregor | 2d4f64f | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10684 | if (Op->getOperator() != OO_Equal && Op->getOperator() != OO_PipeEqual) |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10685 | return; |
| 10686 | |
Douglas Gregor | 2d4f64f | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10687 | IsOrAssign = Op->getOperator() == OO_PipeEqual; |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10688 | Loc = Op->getOperatorLoc(); |
| 10689 | } else { |
| 10690 | // Not an assignment. |
| 10691 | return; |
| 10692 | } |
| 10693 | |
Douglas Gregor | 2bf2d3d | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 10694 | Diag(Loc, diagnostic) << E->getSourceRange(); |
Douglas Gregor | 2d4f64f | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10695 | |
Argyrios Kyrtzidis | e1b97c4 | 2011-04-25 23:01:29 +0000 | [diff] [blame] | 10696 | SourceLocation Open = E->getSourceRange().getBegin(); |
| 10697 | SourceLocation Close = PP.getLocForEndOfToken(E->getSourceRange().getEnd()); |
| 10698 | Diag(Loc, diag::note_condition_assign_silence) |
| 10699 | << FixItHint::CreateInsertion(Open, "(") |
| 10700 | << FixItHint::CreateInsertion(Close, ")"); |
| 10701 | |
Douglas Gregor | 2d4f64f | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 10702 | if (IsOrAssign) |
| 10703 | Diag(Loc, diag::note_condition_or_assign_to_comparison) |
| 10704 | << FixItHint::CreateReplacement(Loc, "!="); |
| 10705 | else |
| 10706 | Diag(Loc, diag::note_condition_assign_to_comparison) |
| 10707 | << FixItHint::CreateReplacement(Loc, "=="); |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10708 | } |
| 10709 | |
Argyrios Kyrtzidis | 8b6ec68 | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 10710 | /// \brief Redundant parentheses over an equality comparison can indicate |
| 10711 | /// that the user intended an assignment used as condition. |
| 10712 | void Sema::DiagnoseEqualityWithExtraParens(ParenExpr *parenE) { |
Argyrios Kyrtzidis | f4f8278 | 2011-02-01 22:23:56 +0000 | [diff] [blame] | 10713 | // Don't warn if the parens came from a macro. |
| 10714 | SourceLocation parenLoc = parenE->getLocStart(); |
| 10715 | if (parenLoc.isInvalid() || parenLoc.isMacroID()) |
| 10716 | return; |
Argyrios Kyrtzidis | ba699d6 | 2011-03-28 23:52:04 +0000 | [diff] [blame] | 10717 | // Don't warn for dependent expressions. |
| 10718 | if (parenE->isTypeDependent()) |
| 10719 | return; |
Argyrios Kyrtzidis | f4f8278 | 2011-02-01 22:23:56 +0000 | [diff] [blame] | 10720 | |
Argyrios Kyrtzidis | 8b6ec68 | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 10721 | Expr *E = parenE->IgnoreParens(); |
| 10722 | |
| 10723 | if (BinaryOperator *opE = dyn_cast<BinaryOperator>(E)) |
Argyrios Kyrtzidis | 582dd68 | 2011-02-01 19:32:59 +0000 | [diff] [blame] | 10724 | if (opE->getOpcode() == BO_EQ && |
| 10725 | opE->getLHS()->IgnoreParenImpCasts()->isModifiableLvalue(Context) |
| 10726 | == Expr::MLV_Valid) { |
Argyrios Kyrtzidis | 8b6ec68 | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 10727 | SourceLocation Loc = opE->getOperatorLoc(); |
Ted Kremenek | c358d9f | 2011-02-01 22:36:09 +0000 | [diff] [blame] | 10728 | |
Ted Kremenek | ae02209 | 2011-02-02 02:20:30 +0000 | [diff] [blame] | 10729 | Diag(Loc, diag::warn_equality_with_extra_parens) << E->getSourceRange(); |
Ted Kremenek | ae02209 | 2011-02-02 02:20:30 +0000 | [diff] [blame] | 10730 | Diag(Loc, diag::note_equality_comparison_silence) |
| 10731 | << FixItHint::CreateRemoval(parenE->getSourceRange().getBegin()) |
| 10732 | << FixItHint::CreateRemoval(parenE->getSourceRange().getEnd()); |
Argyrios Kyrtzidis | e1b97c4 | 2011-04-25 23:01:29 +0000 | [diff] [blame] | 10733 | Diag(Loc, diag::note_equality_comparison_to_assign) |
| 10734 | << FixItHint::CreateReplacement(Loc, "="); |
Argyrios Kyrtzidis | 8b6ec68 | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 10735 | } |
| 10736 | } |
| 10737 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10738 | ExprResult Sema::CheckBooleanCondition(Expr *E, SourceLocation Loc) { |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10739 | DiagnoseAssignmentAsCondition(E); |
Argyrios Kyrtzidis | 8b6ec68 | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 10740 | if (ParenExpr *parenE = dyn_cast<ParenExpr>(E)) |
| 10741 | DiagnoseEqualityWithExtraParens(parenE); |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10742 | |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10743 | ExprResult result = CheckPlaceholderExpr(E); |
| 10744 | if (result.isInvalid()) return ExprError(); |
| 10745 | E = result.take(); |
Argyrios Kyrtzidis | ca76629 | 2010-11-01 18:49:26 +0000 | [diff] [blame] | 10746 | |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10747 | if (!E->isTypeDependent()) { |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 10748 | if (getLangOptions().CPlusPlus) |
| 10749 | return CheckCXXBooleanCondition(E); // C++ 6.4p4 |
| 10750 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10751 | ExprResult ERes = DefaultFunctionArrayLvalueConversion(E); |
| 10752 | if (ERes.isInvalid()) |
| 10753 | return ExprError(); |
| 10754 | E = ERes.take(); |
John McCall | 29cb2fd | 2010-12-04 06:09:13 +0000 | [diff] [blame] | 10755 | |
| 10756 | QualType T = E->getType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10757 | if (!T->isScalarType()) { // C99 6.8.4.1p1 |
| 10758 | Diag(Loc, diag::err_typecheck_statement_requires_scalar) |
| 10759 | << T << E->getSourceRange(); |
| 10760 | return ExprError(); |
| 10761 | } |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10762 | } |
| 10763 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10764 | return Owned(E); |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 10765 | } |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 10766 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 10767 | ExprResult Sema::ActOnBooleanCondition(Scope *S, SourceLocation Loc, |
| 10768 | Expr *Sub) { |
Douglas Gregor | 12cc7ee | 2010-05-06 21:39:56 +0000 | [diff] [blame] | 10769 | if (!Sub) |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 10770 | return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 10771 | |
| 10772 | return CheckBooleanCondition(Sub, Loc); |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 10773 | } |
John McCall | 36e7fe3 | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 10774 | |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10775 | namespace { |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10776 | /// A visitor for rebuilding a call to an __unknown_any expression |
| 10777 | /// to have an appropriate type. |
| 10778 | struct RebuildUnknownAnyFunction |
| 10779 | : StmtVisitor<RebuildUnknownAnyFunction, ExprResult> { |
| 10780 | |
| 10781 | Sema &S; |
| 10782 | |
| 10783 | RebuildUnknownAnyFunction(Sema &S) : S(S) {} |
| 10784 | |
| 10785 | ExprResult VisitStmt(Stmt *S) { |
| 10786 | llvm_unreachable("unexpected statement!"); |
| 10787 | return ExprError(); |
| 10788 | } |
| 10789 | |
| 10790 | ExprResult VisitExpr(Expr *expr) { |
| 10791 | S.Diag(expr->getExprLoc(), diag::err_unsupported_unknown_any_call) |
| 10792 | << expr->getSourceRange(); |
| 10793 | return ExprError(); |
| 10794 | } |
| 10795 | |
| 10796 | /// Rebuild an expression which simply semantically wraps another |
| 10797 | /// expression which it shares the type and value kind of. |
| 10798 | template <class T> ExprResult rebuildSugarExpr(T *expr) { |
| 10799 | ExprResult subResult = Visit(expr->getSubExpr()); |
| 10800 | if (subResult.isInvalid()) return ExprError(); |
| 10801 | |
| 10802 | Expr *subExpr = subResult.take(); |
| 10803 | expr->setSubExpr(subExpr); |
| 10804 | expr->setType(subExpr->getType()); |
| 10805 | expr->setValueKind(subExpr->getValueKind()); |
| 10806 | assert(expr->getObjectKind() == OK_Ordinary); |
| 10807 | return expr; |
| 10808 | } |
| 10809 | |
| 10810 | ExprResult VisitParenExpr(ParenExpr *paren) { |
| 10811 | return rebuildSugarExpr(paren); |
| 10812 | } |
| 10813 | |
| 10814 | ExprResult VisitUnaryExtension(UnaryOperator *op) { |
| 10815 | return rebuildSugarExpr(op); |
| 10816 | } |
| 10817 | |
| 10818 | ExprResult VisitUnaryAddrOf(UnaryOperator *op) { |
| 10819 | ExprResult subResult = Visit(op->getSubExpr()); |
| 10820 | if (subResult.isInvalid()) return ExprError(); |
| 10821 | |
| 10822 | Expr *subExpr = subResult.take(); |
| 10823 | op->setSubExpr(subExpr); |
| 10824 | op->setType(S.Context.getPointerType(subExpr->getType())); |
| 10825 | assert(op->getValueKind() == VK_RValue); |
| 10826 | assert(op->getObjectKind() == OK_Ordinary); |
| 10827 | return op; |
| 10828 | } |
| 10829 | |
| 10830 | ExprResult resolveDecl(Expr *expr, ValueDecl *decl) { |
| 10831 | if (!isa<FunctionDecl>(decl)) return VisitExpr(expr); |
| 10832 | |
| 10833 | expr->setType(decl->getType()); |
| 10834 | |
| 10835 | assert(expr->getValueKind() == VK_RValue); |
| 10836 | if (S.getLangOptions().CPlusPlus && |
| 10837 | !(isa<CXXMethodDecl>(decl) && |
| 10838 | cast<CXXMethodDecl>(decl)->isInstance())) |
| 10839 | expr->setValueKind(VK_LValue); |
| 10840 | |
| 10841 | return expr; |
| 10842 | } |
| 10843 | |
| 10844 | ExprResult VisitMemberExpr(MemberExpr *mem) { |
| 10845 | return resolveDecl(mem, mem->getMemberDecl()); |
| 10846 | } |
| 10847 | |
| 10848 | ExprResult VisitDeclRefExpr(DeclRefExpr *ref) { |
| 10849 | return resolveDecl(ref, ref->getDecl()); |
| 10850 | } |
| 10851 | }; |
| 10852 | } |
| 10853 | |
| 10854 | /// Given a function expression of unknown-any type, try to rebuild it |
| 10855 | /// to have a function type. |
| 10856 | static ExprResult rebuildUnknownAnyFunction(Sema &S, Expr *fn) { |
| 10857 | ExprResult result = RebuildUnknownAnyFunction(S).Visit(fn); |
| 10858 | if (result.isInvalid()) return ExprError(); |
| 10859 | return S.DefaultFunctionArrayConversion(result.take()); |
| 10860 | } |
| 10861 | |
| 10862 | namespace { |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10863 | /// A visitor for rebuilding an expression of type __unknown_anytype |
| 10864 | /// into one which resolves the type directly on the referring |
| 10865 | /// expression. Strict preservation of the original source |
| 10866 | /// structure is not a goal. |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10867 | struct RebuildUnknownAnyExpr |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10868 | : StmtVisitor<RebuildUnknownAnyExpr, ExprResult> { |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10869 | |
| 10870 | Sema &S; |
| 10871 | |
| 10872 | /// The current destination type. |
| 10873 | QualType DestType; |
| 10874 | |
| 10875 | RebuildUnknownAnyExpr(Sema &S, QualType castType) |
| 10876 | : S(S), DestType(castType) {} |
| 10877 | |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10878 | ExprResult VisitStmt(Stmt *S) { |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10879 | llvm_unreachable("unexpected statement!"); |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10880 | return ExprError(); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10881 | } |
| 10882 | |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10883 | ExprResult VisitExpr(Expr *expr) { |
| 10884 | S.Diag(expr->getExprLoc(), diag::err_unsupported_unknown_any_expr) |
| 10885 | << expr->getSourceRange(); |
| 10886 | return ExprError(); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10887 | } |
| 10888 | |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10889 | ExprResult VisitCallExpr(CallExpr *call); |
| 10890 | ExprResult VisitObjCMessageExpr(ObjCMessageExpr *message); |
| 10891 | |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10892 | /// Rebuild an expression which simply semantically wraps another |
| 10893 | /// expression which it shares the type and value kind of. |
| 10894 | template <class T> ExprResult rebuildSugarExpr(T *expr) { |
| 10895 | ExprResult subResult = Visit(expr->getSubExpr()); |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10896 | if (subResult.isInvalid()) return ExprError(); |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10897 | Expr *subExpr = subResult.take(); |
| 10898 | expr->setSubExpr(subExpr); |
| 10899 | expr->setType(subExpr->getType()); |
| 10900 | expr->setValueKind(subExpr->getValueKind()); |
| 10901 | assert(expr->getObjectKind() == OK_Ordinary); |
| 10902 | return expr; |
| 10903 | } |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10904 | |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10905 | ExprResult VisitParenExpr(ParenExpr *paren) { |
| 10906 | return rebuildSugarExpr(paren); |
| 10907 | } |
| 10908 | |
| 10909 | ExprResult VisitUnaryExtension(UnaryOperator *op) { |
| 10910 | return rebuildSugarExpr(op); |
| 10911 | } |
| 10912 | |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10913 | ExprResult VisitUnaryAddrOf(UnaryOperator *op) { |
| 10914 | const PointerType *ptr = DestType->getAs<PointerType>(); |
| 10915 | if (!ptr) { |
| 10916 | S.Diag(op->getOperatorLoc(), diag::err_unknown_any_addrof) |
| 10917 | << op->getSourceRange(); |
| 10918 | return ExprError(); |
| 10919 | } |
| 10920 | assert(op->getValueKind() == VK_RValue); |
| 10921 | assert(op->getObjectKind() == OK_Ordinary); |
| 10922 | op->setType(DestType); |
| 10923 | |
| 10924 | // Build the sub-expression as if it were an object of the pointee type. |
| 10925 | DestType = ptr->getPointeeType(); |
| 10926 | ExprResult subResult = Visit(op->getSubExpr()); |
| 10927 | if (subResult.isInvalid()) return ExprError(); |
| 10928 | op->setSubExpr(subResult.take()); |
| 10929 | return op; |
| 10930 | } |
| 10931 | |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10932 | ExprResult VisitImplicitCastExpr(ImplicitCastExpr *ice); |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10933 | |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10934 | ExprResult resolveDecl(Expr *expr, ValueDecl *decl); |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10935 | |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 10936 | ExprResult VisitMemberExpr(MemberExpr *mem) { |
| 10937 | return resolveDecl(mem, mem->getMemberDecl()); |
| 10938 | } |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 10939 | |
| 10940 | ExprResult VisitDeclRefExpr(DeclRefExpr *ref) { |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10941 | return resolveDecl(ref, ref->getDecl()); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10942 | } |
| 10943 | }; |
| 10944 | } |
| 10945 | |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10946 | /// Rebuilds a call expression which yielded __unknown_anytype. |
| 10947 | ExprResult RebuildUnknownAnyExpr::VisitCallExpr(CallExpr *call) { |
| 10948 | Expr *callee = call->getCallee(); |
| 10949 | |
| 10950 | enum FnKind { |
John McCall | 4adb38c | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 10951 | FK_MemberFunction, |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10952 | FK_FunctionPointer, |
| 10953 | FK_BlockPointer |
| 10954 | }; |
| 10955 | |
| 10956 | FnKind kind; |
| 10957 | QualType type = callee->getType(); |
John McCall | 4adb38c | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 10958 | if (type == S.Context.BoundMemberTy) { |
| 10959 | assert(isa<CXXMemberCallExpr>(call) || isa<CXXOperatorCallExpr>(call)); |
| 10960 | kind = FK_MemberFunction; |
| 10961 | type = Expr::findBoundMemberType(callee); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10962 | } else if (const PointerType *ptr = type->getAs<PointerType>()) { |
| 10963 | type = ptr->getPointeeType(); |
| 10964 | kind = FK_FunctionPointer; |
| 10965 | } else { |
| 10966 | type = type->castAs<BlockPointerType>()->getPointeeType(); |
| 10967 | kind = FK_BlockPointer; |
| 10968 | } |
| 10969 | const FunctionType *fnType = type->castAs<FunctionType>(); |
| 10970 | |
| 10971 | // Verify that this is a legal result type of a function. |
| 10972 | if (DestType->isArrayType() || DestType->isFunctionType()) { |
| 10973 | unsigned diagID = diag::err_func_returning_array_function; |
| 10974 | if (kind == FK_BlockPointer) |
| 10975 | diagID = diag::err_block_returning_array_function; |
| 10976 | |
| 10977 | S.Diag(call->getExprLoc(), diagID) |
| 10978 | << DestType->isFunctionType() << DestType; |
| 10979 | return ExprError(); |
| 10980 | } |
| 10981 | |
| 10982 | // Otherwise, go ahead and set DestType as the call's result. |
| 10983 | call->setType(DestType.getNonLValueExprType(S.Context)); |
| 10984 | call->setValueKind(Expr::getValueKindForType(DestType)); |
| 10985 | assert(call->getObjectKind() == OK_Ordinary); |
| 10986 | |
| 10987 | // Rebuild the function type, replacing the result type with DestType. |
| 10988 | if (const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(fnType)) |
| 10989 | DestType = S.Context.getFunctionType(DestType, |
| 10990 | proto->arg_type_begin(), |
| 10991 | proto->getNumArgs(), |
| 10992 | proto->getExtProtoInfo()); |
| 10993 | else |
| 10994 | DestType = S.Context.getFunctionNoProtoType(DestType, |
| 10995 | fnType->getExtInfo()); |
| 10996 | |
| 10997 | // Rebuild the appropriate pointer-to-function type. |
| 10998 | switch (kind) { |
John McCall | 4adb38c | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 10999 | case FK_MemberFunction: |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11000 | // Nothing to do. |
| 11001 | break; |
| 11002 | |
| 11003 | case FK_FunctionPointer: |
| 11004 | DestType = S.Context.getPointerType(DestType); |
| 11005 | break; |
| 11006 | |
| 11007 | case FK_BlockPointer: |
| 11008 | DestType = S.Context.getBlockPointerType(DestType); |
| 11009 | break; |
| 11010 | } |
| 11011 | |
| 11012 | // Finally, we can recurse. |
| 11013 | ExprResult calleeResult = Visit(callee); |
| 11014 | if (!calleeResult.isUsable()) return ExprError(); |
| 11015 | call->setCallee(calleeResult.take()); |
| 11016 | |
| 11017 | // Bind a temporary if necessary. |
| 11018 | return S.MaybeBindToTemporary(call); |
| 11019 | } |
| 11020 | |
| 11021 | ExprResult RebuildUnknownAnyExpr::VisitObjCMessageExpr(ObjCMessageExpr *msg) { |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11022 | ObjCMethodDecl *method = msg->getMethodDecl(); |
| 11023 | assert(method && "__unknown_anytype message without result type?"); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11024 | |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11025 | // Verify that this is a legal result type of a call. |
| 11026 | if (DestType->isArrayType() || DestType->isFunctionType()) { |
| 11027 | S.Diag(msg->getExprLoc(), diag::err_func_returning_array_function) |
| 11028 | << DestType->isFunctionType() << DestType; |
| 11029 | return ExprError(); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11030 | } |
| 11031 | |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11032 | assert(method->getResultType() == S.Context.UnknownAnyTy); |
| 11033 | method->setResultType(DestType); |
| 11034 | |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11035 | // Change the type of the message. |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11036 | msg->setType(DestType.getNonReferenceType()); |
| 11037 | msg->setValueKind(Expr::getValueKindForType(DestType)); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11038 | |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11039 | return S.MaybeBindToTemporary(msg); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11040 | } |
| 11041 | |
| 11042 | ExprResult RebuildUnknownAnyExpr::VisitImplicitCastExpr(ImplicitCastExpr *ice) { |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11043 | // 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] | 11044 | assert(ice->getCastKind() == CK_FunctionToPointerDecay); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11045 | assert(ice->getValueKind() == VK_RValue); |
| 11046 | assert(ice->getObjectKind() == OK_Ordinary); |
| 11047 | |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11048 | ice->setType(DestType); |
| 11049 | |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11050 | // Rebuild the sub-expression as the pointee (function) type. |
| 11051 | DestType = DestType->castAs<PointerType>()->getPointeeType(); |
| 11052 | |
| 11053 | ExprResult result = Visit(ice->getSubExpr()); |
| 11054 | if (!result.isUsable()) return ExprError(); |
| 11055 | |
| 11056 | ice->setSubExpr(result.take()); |
| 11057 | return S.Owned(ice); |
| 11058 | } |
| 11059 | |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11060 | ExprResult RebuildUnknownAnyExpr::resolveDecl(Expr *expr, ValueDecl *decl) { |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11061 | ExprValueKind valueKind = VK_LValue; |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11062 | QualType type = DestType; |
| 11063 | |
| 11064 | // We know how to make this work for certain kinds of decls: |
| 11065 | |
| 11066 | // - functions |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11067 | if (FunctionDecl *fn = dyn_cast<FunctionDecl>(decl)) { |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11068 | // This is true because FunctionDecls must always have function |
| 11069 | // type, so we can't be resolving the entire thing at once. |
| 11070 | assert(type->isFunctionType()); |
| 11071 | |
John McCall | 4adb38c | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 11072 | if (CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(fn)) |
| 11073 | if (method->isInstance()) { |
| 11074 | valueKind = VK_RValue; |
| 11075 | type = S.Context.BoundMemberTy; |
| 11076 | } |
| 11077 | |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11078 | // Function references aren't l-values in C. |
| 11079 | if (!S.getLangOptions().CPlusPlus) |
| 11080 | valueKind = VK_RValue; |
| 11081 | |
| 11082 | // - variables |
| 11083 | } else if (isa<VarDecl>(decl)) { |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11084 | if (const ReferenceType *refTy = type->getAs<ReferenceType>()) { |
| 11085 | type = refTy->getPointeeType(); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11086 | } else if (type->isFunctionType()) { |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11087 | S.Diag(expr->getExprLoc(), diag::err_unknown_any_var_function_type) |
| 11088 | << decl << expr->getSourceRange(); |
| 11089 | return ExprError(); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11090 | } |
| 11091 | |
| 11092 | // - nothing else |
| 11093 | } else { |
| 11094 | S.Diag(expr->getExprLoc(), diag::err_unsupported_unknown_any_decl) |
| 11095 | << decl << expr->getSourceRange(); |
| 11096 | return ExprError(); |
| 11097 | } |
| 11098 | |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11099 | decl->setType(DestType); |
| 11100 | expr->setType(type); |
| 11101 | expr->setValueKind(valueKind); |
| 11102 | return S.Owned(expr); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11103 | } |
| 11104 | |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11105 | /// Check a cast of an unknown-any type. We intentionally only |
| 11106 | /// trigger this for C-style casts. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11107 | ExprResult Sema::checkUnknownAnyCast(SourceRange typeRange, QualType castType, |
| 11108 | Expr *castExpr, CastKind &castKind, |
| 11109 | ExprValueKind &VK, CXXCastPath &path) { |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11110 | // Rewrite the casted expression from scratch. |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 11111 | ExprResult result = RebuildUnknownAnyExpr(*this, castType).Visit(castExpr); |
| 11112 | if (!result.isUsable()) return ExprError(); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11113 | |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 11114 | castExpr = result.take(); |
| 11115 | VK = castExpr->getValueKind(); |
| 11116 | castKind = CK_NoOp; |
| 11117 | |
| 11118 | return castExpr; |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11119 | } |
| 11120 | |
| 11121 | static ExprResult diagnoseUnknownAnyExpr(Sema &S, Expr *e) { |
| 11122 | Expr *orig = e; |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11123 | unsigned diagID = diag::err_uncasted_use_of_unknown_any; |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11124 | while (true) { |
| 11125 | e = e->IgnoreParenImpCasts(); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11126 | if (CallExpr *call = dyn_cast<CallExpr>(e)) { |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11127 | e = call->getCallee(); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11128 | diagID = diag::err_uncasted_call_of_unknown_any; |
| 11129 | } else { |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11130 | break; |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11131 | } |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11132 | } |
| 11133 | |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11134 | SourceLocation loc; |
| 11135 | NamedDecl *d; |
| 11136 | if (DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e)) { |
| 11137 | loc = ref->getLocation(); |
| 11138 | d = ref->getDecl(); |
| 11139 | } else if (MemberExpr *mem = dyn_cast<MemberExpr>(e)) { |
| 11140 | loc = mem->getMemberLoc(); |
| 11141 | d = mem->getMemberDecl(); |
| 11142 | } else if (ObjCMessageExpr *msg = dyn_cast<ObjCMessageExpr>(e)) { |
| 11143 | diagID = diag::err_uncasted_call_of_unknown_any; |
| 11144 | loc = msg->getSelectorLoc(); |
| 11145 | d = msg->getMethodDecl(); |
| 11146 | assert(d && "unknown method returning __unknown_any?"); |
| 11147 | } else { |
| 11148 | S.Diag(e->getExprLoc(), diag::err_unsupported_unknown_any_expr) |
| 11149 | << e->getSourceRange(); |
| 11150 | return ExprError(); |
| 11151 | } |
| 11152 | |
| 11153 | S.Diag(loc, diagID) << d << orig->getSourceRange(); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11154 | |
| 11155 | // Never recoverable. |
| 11156 | return ExprError(); |
| 11157 | } |
| 11158 | |
John McCall | 36e7fe3 | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 11159 | /// Check for operands with placeholder types and complain if found. |
| 11160 | /// Returns true if there was an error and no recovery was possible. |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 11161 | ExprResult Sema::CheckPlaceholderExpr(Expr *E) { |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11162 | // Placeholder types are always *exactly* the appropriate builtin type. |
| 11163 | QualType type = E->getType(); |
John McCall | 36e7fe3 | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 11164 | |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11165 | // Overloaded expressions. |
| 11166 | if (type == Context.OverloadTy) |
| 11167 | return ResolveAndFixSingleFunctionTemplateSpecialization(E, false, true, |
Douglas Gregor | 89f3cd5 | 2011-03-16 19:16:25 +0000 | [diff] [blame] | 11168 | E->getSourceRange(), |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11169 | QualType(), |
| 11170 | diag::err_ovl_unresolvable); |
| 11171 | |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11172 | // Bound member functions. |
| 11173 | if (type == Context.BoundMemberTy) { |
| 11174 | Diag(E->getLocStart(), diag::err_invalid_use_of_bound_member_func) |
| 11175 | << E->getSourceRange(); |
| 11176 | return ExprError(); |
| 11177 | } |
| 11178 | |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11179 | // Expressions of unknown type. |
| 11180 | if (type == Context.UnknownAnyTy) |
| 11181 | return diagnoseUnknownAnyExpr(*this, E); |
| 11182 | |
| 11183 | assert(!type->isPlaceholderType()); |
| 11184 | return Owned(E); |
John McCall | 36e7fe3 | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 11185 | } |
Richard Trieu | 2c850c0 | 2011-04-21 21:44:26 +0000 | [diff] [blame] | 11186 | |
| 11187 | bool Sema::CheckCaseExpression(Expr *expr) { |
| 11188 | if (expr->isTypeDependent()) |
| 11189 | return true; |
| 11190 | if (expr->isValueDependent() || expr->isIntegerConstantExpr(Context)) |
| 11191 | return expr->getType()->isIntegralOrEnumerationType(); |
| 11192 | return false; |
| 11193 | } |