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" |
Anna Zaks | 3b40271 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 39 | #include "clang/Sema/SemaFixItUtils.h" |
John McCall | de6836a | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 40 | #include "clang/Sema/Template.h" |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 41 | using namespace clang; |
John McCall | aab3e41 | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 42 | using namespace sema; |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 43 | |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 44 | |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 45 | /// \brief Determine whether the use of this declaration is valid, and |
| 46 | /// emit any corresponding diagnostics. |
| 47 | /// |
| 48 | /// This routine diagnoses various problems with referencing |
| 49 | /// declarations that can occur when using a declaration. For example, |
| 50 | /// it might warn if a deprecated or unavailable declaration is being |
| 51 | /// used, or produce an error (and return true) if a C++0x deleted |
| 52 | /// function is being used. |
| 53 | /// |
Fariborz Jahanian | 7d6e11a | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 54 | /// If IgnoreDeprecated is set to true, this should not warn about deprecated |
Chris Lattner | b7df3c6 | 2009-10-25 22:31:57 +0000 | [diff] [blame] | 55 | /// decls. |
| 56 | /// |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 57 | /// \returns true if there was an error (this declaration cannot be |
| 58 | /// referenced), false otherwise. |
Chris Lattner | b7df3c6 | 2009-10-25 22:31:57 +0000 | [diff] [blame] | 59 | /// |
Fariborz Jahanian | 7d6e11a | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 60 | bool Sema::DiagnoseUseOfDecl(NamedDecl *D, SourceLocation Loc, |
Fariborz Jahanian | dbbdd2f | 2011-04-23 17:27:19 +0000 | [diff] [blame] | 61 | const ObjCInterfaceDecl *UnknownObjCClass) { |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 62 | if (getLangOptions().CPlusPlus && isa<FunctionDecl>(D)) { |
| 63 | // If there were any diagnostics suppressed by template argument deduction, |
| 64 | // emit them now. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 65 | llvm::DenseMap<Decl *, SmallVector<PartialDiagnosticAt, 1> >::iterator |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 66 | Pos = SuppressedDiagnostics.find(D->getCanonicalDecl()); |
| 67 | if (Pos != SuppressedDiagnostics.end()) { |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 68 | SmallVectorImpl<PartialDiagnosticAt> &Suppressed = Pos->second; |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 69 | for (unsigned I = 0, N = Suppressed.size(); I != N; ++I) |
| 70 | Diag(Suppressed[I].first, Suppressed[I].second); |
| 71 | |
| 72 | // 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] | 73 | // them again for this specialization. However, we don't obsolete this |
Douglas Gregor | 5bb5e4a | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 74 | // entry from the table, because we want to avoid ever emitting these |
| 75 | // diagnostics again. |
| 76 | Suppressed.clear(); |
| 77 | } |
| 78 | } |
| 79 | |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 80 | // 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] | 81 | if (ParsingInitForAutoVars.count(D)) { |
| 82 | Diag(Loc, diag::err_auto_variable_cannot_appear_in_own_initializer) |
| 83 | << D->getDeclName(); |
| 84 | return true; |
Richard Smith | 30482bc | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 87 | // See if this is a deleted function. |
Douglas Gregor | de681d4 | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 88 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 89 | if (FD->isDeleted()) { |
| 90 | Diag(Loc, diag::err_deleted_function_use); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 91 | Diag(D->getLocation(), diag::note_unavailable_here) << 1 << true; |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 92 | return true; |
| 93 | } |
Douglas Gregor | de681d4 | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 94 | } |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 95 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 96 | // See if this declaration is unavailable or deprecated. |
| 97 | std::string Message; |
| 98 | switch (D->getAvailability(&Message)) { |
| 99 | case AR_Available: |
| 100 | case AR_NotYetIntroduced: |
| 101 | break; |
| 102 | |
| 103 | case AR_Deprecated: |
| 104 | EmitDeprecationWarning(D, Message, Loc, UnknownObjCClass); |
| 105 | break; |
| 106 | |
| 107 | case AR_Unavailable: |
Argyrios Kyrtzidis | c30661f | 2011-06-17 17:28:30 +0000 | [diff] [blame] | 108 | if (cast<Decl>(CurContext)->getAvailability() != AR_Unavailable) { |
| 109 | if (Message.empty()) { |
| 110 | if (!UnknownObjCClass) |
| 111 | Diag(Loc, diag::err_unavailable) << D->getDeclName(); |
| 112 | else |
| 113 | Diag(Loc, diag::warn_unavailable_fwdclass_message) |
| 114 | << D->getDeclName(); |
| 115 | } |
| 116 | else |
| 117 | Diag(Loc, diag::err_unavailable_message) |
| 118 | << D->getDeclName() << Message; |
| 119 | Diag(D->getLocation(), diag::note_unavailable_here) |
| 120 | << isa<FunctionDecl>(D) << false; |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 121 | } |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 122 | break; |
| 123 | } |
| 124 | |
Anders Carlsson | 73067a0 | 2010-10-22 23:37:08 +0000 | [diff] [blame] | 125 | // Warn if this is used but marked unused. |
| 126 | if (D->hasAttr<UnusedAttr>()) |
| 127 | Diag(Loc, diag::warn_used_but_marked_unused) << D->getDeclName(); |
| 128 | |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 129 | return false; |
Chris Lattner | 4bf74fd | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Douglas Gregor | 20b2ebd | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 132 | /// \brief Retrieve the message suffix that should be added to a |
| 133 | /// diagnostic complaining about the given function being deleted or |
| 134 | /// unavailable. |
| 135 | std::string Sema::getDeletedOrUnavailableSuffix(const FunctionDecl *FD) { |
| 136 | // FIXME: C++0x implicitly-deleted special member functions could be |
| 137 | // detected here so that we could improve diagnostics to say, e.g., |
| 138 | // "base class 'A' had a deleted copy constructor". |
| 139 | if (FD->isDeleted()) |
| 140 | return std::string(); |
| 141 | |
| 142 | std::string Message; |
| 143 | if (FD->getAvailability(&Message)) |
| 144 | return ": " + Message; |
| 145 | |
| 146 | return std::string(); |
| 147 | } |
| 148 | |
Fariborz Jahanian | 027b886 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 149 | /// DiagnoseSentinelCalls - This routine checks on method dispatch calls |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 150 | /// (and other functions in future), which have been declared with sentinel |
Fariborz Jahanian | 027b886 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 151 | /// attribute. It warns if call does not have the sentinel argument. |
| 152 | /// |
| 153 | void Sema::DiagnoseSentinelCalls(NamedDecl *D, SourceLocation Loc, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 154 | Expr **Args, unsigned NumArgs) { |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 155 | const SentinelAttr *attr = D->getAttr<SentinelAttr>(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 156 | if (!attr) |
Fariborz Jahanian | 9e87721 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 157 | return; |
Douglas Gregor | c298ffc | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 158 | |
Fariborz Jahanian | 9e87721 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 159 | int sentinelPos = attr->getSentinel(); |
| 160 | int nullPos = attr->getNullPos(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 161 | |
Fariborz Jahanian | 9e87721 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 162 | unsigned int i = 0; |
Fariborz Jahanian | 4a52803 | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 163 | bool warnNotEnoughArgs = false; |
| 164 | int isMethod = 0; |
| 165 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
| 166 | // skip over named parameters. |
| 167 | ObjCMethodDecl::param_iterator P, E = MD->param_end(); |
| 168 | for (P = MD->param_begin(); (P != E && i < NumArgs); ++P) { |
| 169 | if (nullPos) |
| 170 | --nullPos; |
| 171 | else |
| 172 | ++i; |
| 173 | } |
| 174 | warnNotEnoughArgs = (P != E || i >= NumArgs); |
| 175 | isMethod = 1; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 176 | } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Fariborz Jahanian | 4a52803 | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 177 | // skip over named parameters. |
| 178 | ObjCMethodDecl::param_iterator P, E = FD->param_end(); |
| 179 | for (P = FD->param_begin(); (P != E && i < NumArgs); ++P) { |
| 180 | if (nullPos) |
| 181 | --nullPos; |
| 182 | else |
| 183 | ++i; |
| 184 | } |
| 185 | warnNotEnoughArgs = (P != E || i >= NumArgs); |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 186 | } else if (VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 187 | // block or function pointer call. |
| 188 | QualType Ty = V->getType(); |
| 189 | if (Ty->isBlockPointerType() || Ty->isFunctionPointerType()) { |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 190 | const FunctionType *FT = Ty->isFunctionPointerType() |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 191 | ? Ty->getAs<PointerType>()->getPointeeType()->getAs<FunctionType>() |
| 192 | : Ty->getAs<BlockPointerType>()->getPointeeType()->getAs<FunctionType>(); |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 193 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FT)) { |
| 194 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 195 | unsigned k; |
| 196 | for (k = 0; (k != NumArgsInProto && i < NumArgs); k++) { |
| 197 | if (nullPos) |
| 198 | --nullPos; |
| 199 | else |
| 200 | ++i; |
| 201 | } |
| 202 | warnNotEnoughArgs = (k != NumArgsInProto || i >= NumArgs); |
| 203 | } |
| 204 | if (Ty->isBlockPointerType()) |
| 205 | isMethod = 2; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 206 | } else |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 207 | return; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 208 | } else |
Fariborz Jahanian | 4a52803 | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 209 | return; |
| 210 | |
| 211 | if (warnNotEnoughArgs) { |
Fariborz Jahanian | 9e87721 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 212 | Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName(); |
Fariborz Jahanian | 4a52803 | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 213 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 9e87721 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 214 | return; |
| 215 | } |
| 216 | int sentinel = i; |
| 217 | while (sentinelPos > 0 && i < NumArgs-1) { |
| 218 | --sentinelPos; |
| 219 | ++i; |
| 220 | } |
| 221 | if (sentinelPos > 0) { |
| 222 | Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName(); |
Fariborz Jahanian | 4a52803 | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 223 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 9e87721 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 224 | return; |
| 225 | } |
| 226 | while (i < NumArgs-1) { |
| 227 | ++i; |
| 228 | ++sentinel; |
| 229 | } |
| 230 | Expr *sentinelExpr = Args[sentinel]; |
John McCall | 7ddbcf4 | 2010-05-06 23:53:00 +0000 | [diff] [blame] | 231 | if (!sentinelExpr) return; |
| 232 | if (sentinelExpr->isTypeDependent()) return; |
| 233 | if (sentinelExpr->isValueDependent()) return; |
Anders Carlsson | e981a8c | 2010-11-05 15:21:33 +0000 | [diff] [blame] | 234 | |
| 235 | // nullptr_t is always treated as null. |
| 236 | if (sentinelExpr->getType()->isNullPtrType()) return; |
| 237 | |
Fariborz Jahanian | c0b0ced | 2010-07-14 16:37:51 +0000 | [diff] [blame] | 238 | if (sentinelExpr->getType()->isAnyPointerType() && |
John McCall | 7ddbcf4 | 2010-05-06 23:53:00 +0000 | [diff] [blame] | 239 | sentinelExpr->IgnoreParenCasts()->isNullPointerConstant(Context, |
| 240 | Expr::NPC_ValueDependentIsNull)) |
| 241 | return; |
| 242 | |
| 243 | // Unfortunately, __null has type 'int'. |
| 244 | if (isa<GNUNullExpr>(sentinelExpr)) return; |
| 245 | |
Douglas Gregor | 5ff4e98 | 2011-07-30 08:57:03 +0000 | [diff] [blame] | 246 | SourceLocation MissingNilLoc |
| 247 | = PP.getLocForEndOfToken(sentinelExpr->getLocEnd()); |
| 248 | std::string NullValue; |
| 249 | if (isMethod && PP.getIdentifierInfo("nil")->hasMacroDefinition()) |
| 250 | NullValue = "nil"; |
| 251 | else if (PP.getIdentifierInfo("NULL")->hasMacroDefinition()) |
| 252 | NullValue = "NULL"; |
| 253 | else if (Context.getTypeSize(Context.IntTy) |
| 254 | == Context.getTypeSize(Context.getSizeType())) |
| 255 | NullValue = "0"; |
| 256 | else |
| 257 | NullValue = "0L"; |
| 258 | |
| 259 | Diag(MissingNilLoc, diag::warn_missing_sentinel) |
| 260 | << isMethod |
| 261 | << FixItHint::CreateInsertion(MissingNilLoc, ", " + NullValue); |
John McCall | 7ddbcf4 | 2010-05-06 23:53:00 +0000 | [diff] [blame] | 262 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 027b886 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Douglas Gregor | 87f95b0 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 265 | SourceRange Sema::getExprRange(ExprTy *E) const { |
| 266 | Expr *Ex = (Expr *)E; |
| 267 | return Ex? Ex->getSourceRange() : SourceRange(); |
| 268 | } |
| 269 | |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 270 | //===----------------------------------------------------------------------===// |
| 271 | // Standard Promotions and Conversions |
| 272 | //===----------------------------------------------------------------------===// |
| 273 | |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 274 | /// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4). |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 275 | ExprResult Sema::DefaultFunctionArrayConversion(Expr *E) { |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 276 | QualType Ty = E->getType(); |
| 277 | assert(!Ty.isNull() && "DefaultFunctionArrayConversion - missing type"); |
| 278 | |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 279 | if (Ty->isFunctionType()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 280 | E = ImpCastExprToType(E, Context.getPointerType(Ty), |
| 281 | CK_FunctionToPointerDecay).take(); |
Chris Lattner | 61f60a0 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 282 | else if (Ty->isArrayType()) { |
| 283 | // In C90 mode, arrays only promote to pointers if the array expression is |
| 284 | // an lvalue. The relevant legalese is C90 6.2.2.1p3: "an lvalue that has |
| 285 | // type 'array of type' is converted to an expression that has type 'pointer |
| 286 | // to type'...". In C99 this was changed to: C99 6.3.2.1p3: "an expression |
| 287 | // that has type 'array of type' ...". The relevant change is "an lvalue" |
| 288 | // (C90) to "an expression" (C99). |
Argyrios Kyrtzidis | 9321c74 | 2008-09-11 04:25:59 +0000 | [diff] [blame] | 289 | // |
| 290 | // C++ 4.2p1: |
| 291 | // An lvalue or rvalue of type "array of N T" or "array of unknown bound of |
| 292 | // T" can be converted to an rvalue of type "pointer to T". |
| 293 | // |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 294 | if (getLangOptions().C99 || getLangOptions().CPlusPlus || E->isLValue()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 295 | E = ImpCastExprToType(E, Context.getArrayDecayedType(Ty), |
| 296 | CK_ArrayToPointerDecay).take(); |
Chris Lattner | 61f60a0 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 297 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 298 | return Owned(E); |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Argyrios Kyrtzidis | a9b630e | 2011-04-26 17:41:22 +0000 | [diff] [blame] | 301 | static void CheckForNullPointerDereference(Sema &S, Expr *E) { |
| 302 | // Check to see if we are dereferencing a null pointer. If so, |
| 303 | // and if not volatile-qualified, this is undefined behavior that the |
| 304 | // optimizer will delete, so warn about it. People sometimes try to use this |
| 305 | // to get a deterministic trap and are surprised by clang's behavior. This |
| 306 | // only handles the pattern "*null", which is a very syntactic check. |
| 307 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E->IgnoreParenCasts())) |
| 308 | if (UO->getOpcode() == UO_Deref && |
| 309 | UO->getSubExpr()->IgnoreParenCasts()-> |
| 310 | isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull) && |
| 311 | !UO->getType().isVolatileQualified()) { |
| 312 | S.DiagRuntimeBehavior(UO->getOperatorLoc(), UO, |
| 313 | S.PDiag(diag::warn_indirection_through_null) |
| 314 | << UO->getSubExpr()->getSourceRange()); |
| 315 | S.DiagRuntimeBehavior(UO->getOperatorLoc(), UO, |
| 316 | S.PDiag(diag::note_indirection_through_null)); |
| 317 | } |
| 318 | } |
| 319 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 320 | ExprResult Sema::DefaultLvalueConversion(Expr *E) { |
John McCall | f3735e0 | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 321 | // C++ [conv.lval]p1: |
| 322 | // A glvalue of a non-function, non-array type T can be |
| 323 | // converted to a prvalue. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 324 | if (!E->isGLValue()) return Owned(E); |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 325 | |
John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 326 | QualType T = E->getType(); |
| 327 | assert(!T.isNull() && "r-value conversion on typeless expression?"); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 328 | |
John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 329 | // Create a load out of an ObjCProperty l-value, if necessary. |
| 330 | if (E->getObjectKind() == OK_ObjCProperty) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 331 | ExprResult Res = ConvertPropertyForRValue(E); |
| 332 | if (Res.isInvalid()) |
| 333 | return Owned(E); |
| 334 | E = Res.take(); |
John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 335 | if (!E->isGLValue()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 336 | return Owned(E); |
Douglas Gregor | b92a156 | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 337 | } |
John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 338 | |
| 339 | // We don't want to throw lvalue-to-rvalue casts on top of |
| 340 | // expressions of certain types in C++. |
| 341 | if (getLangOptions().CPlusPlus && |
| 342 | (E->getType() == Context.OverloadTy || |
| 343 | T->isDependentType() || |
| 344 | T->isRecordType())) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 345 | return Owned(E); |
John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 346 | |
| 347 | // The C standard is actually really unclear on this point, and |
| 348 | // DR106 tells us what the result should be but not why. It's |
| 349 | // generally best to say that void types just doesn't undergo |
| 350 | // lvalue-to-rvalue at all. Note that expressions of unqualified |
| 351 | // 'void' type are never l-values, but qualified void can be. |
| 352 | if (T->isVoidType()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 353 | return Owned(E); |
John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 354 | |
Argyrios Kyrtzidis | a9b630e | 2011-04-26 17:41:22 +0000 | [diff] [blame] | 355 | CheckForNullPointerDereference(*this, E); |
| 356 | |
John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 357 | // C++ [conv.lval]p1: |
| 358 | // [...] If T is a non-class type, the type of the prvalue is the |
| 359 | // cv-unqualified version of T. Otherwise, the type of the |
| 360 | // rvalue is T. |
| 361 | // |
| 362 | // C99 6.3.2.1p2: |
| 363 | // If the lvalue has qualified type, the value has the unqualified |
| 364 | // version of the type of the lvalue; otherwise, the value has the |
| 365 | // type of the lvalue. |
| 366 | if (T.hasQualifiers()) |
| 367 | T = T.getUnqualifiedType(); |
Ted Kremenek | 64699be | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 368 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 369 | return Owned(ImplicitCastExpr::Create(Context, T, CK_LValueToRValue, |
| 370 | E, 0, VK_RValue)); |
John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 371 | } |
| 372 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 373 | ExprResult Sema::DefaultFunctionArrayLvalueConversion(Expr *E) { |
| 374 | ExprResult Res = DefaultFunctionArrayConversion(E); |
| 375 | if (Res.isInvalid()) |
| 376 | return ExprError(); |
| 377 | Res = DefaultLvalueConversion(Res.take()); |
| 378 | if (Res.isInvalid()) |
| 379 | return ExprError(); |
| 380 | return move(Res); |
Douglas Gregor | b92a156 | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 384 | /// UsualUnaryConversions - Performs various conversions that are common to most |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 385 | /// operators (C99 6.3). The conversions of array and function types are |
Chris Lattner | 57540c5 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 386 | /// sometimes suppressed. For example, the array->pointer conversion doesn't |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 387 | /// apply if the array is an argument to the sizeof or address (&) operators. |
| 388 | /// In these instances, this routine should *not* be called. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 389 | ExprResult Sema::UsualUnaryConversions(Expr *E) { |
John McCall | f3735e0 | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 390 | // First, convert to an r-value. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 391 | ExprResult Res = DefaultFunctionArrayLvalueConversion(E); |
| 392 | if (Res.isInvalid()) |
| 393 | return Owned(E); |
| 394 | E = Res.take(); |
John McCall | f3735e0 | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 395 | |
| 396 | QualType Ty = E->getType(); |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 397 | assert(!Ty.isNull() && "UsualUnaryConversions - missing type"); |
John McCall | f3735e0 | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 398 | |
| 399 | // Try to perform integral promotions if the object has a theoretically |
| 400 | // promotable type. |
| 401 | if (Ty->isIntegralOrUnscopedEnumerationType()) { |
| 402 | // C99 6.3.1.1p2: |
| 403 | // |
| 404 | // The following may be used in an expression wherever an int or |
| 405 | // unsigned int may be used: |
| 406 | // - an object or expression with an integer type whose integer |
| 407 | // conversion rank is less than or equal to the rank of int |
| 408 | // and unsigned int. |
| 409 | // - A bit-field of type _Bool, int, signed int, or unsigned int. |
| 410 | // |
| 411 | // If an int can represent all values of the original type, the |
| 412 | // value is converted to an int; otherwise, it is converted to an |
| 413 | // unsigned int. These are called the integer promotions. All |
| 414 | // other types are unchanged by the integer promotions. |
| 415 | |
| 416 | QualType PTy = Context.isPromotableBitField(E); |
| 417 | if (!PTy.isNull()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 418 | E = ImpCastExprToType(E, PTy, CK_IntegralCast).take(); |
| 419 | return Owned(E); |
John McCall | f3735e0 | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 420 | } |
| 421 | if (Ty->isPromotableIntegerType()) { |
| 422 | QualType PT = Context.getPromotedIntegerType(Ty); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 423 | E = ImpCastExprToType(E, PT, CK_IntegralCast).take(); |
| 424 | return Owned(E); |
John McCall | f3735e0 | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 425 | } |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 426 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 427 | return Owned(E); |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Chris Lattner | 2ce500f | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 430 | /// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 431 | /// 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] | 432 | /// double. All other argument types are converted by UsualUnaryConversions(). |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 433 | ExprResult Sema::DefaultArgumentPromotion(Expr *E) { |
| 434 | QualType Ty = E->getType(); |
Chris Lattner | 2ce500f | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 435 | assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 436 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 437 | ExprResult Res = UsualUnaryConversions(E); |
| 438 | if (Res.isInvalid()) |
| 439 | return Owned(E); |
| 440 | E = Res.take(); |
John McCall | 9bc2677 | 2010-12-06 18:36:11 +0000 | [diff] [blame] | 441 | |
Chris Lattner | 2ce500f | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 442 | // If this is a 'float' (CVR qualified or typedef) promote to double. |
Chris Lattner | bb53efb | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 443 | if (Ty->isSpecificBuiltinType(BuiltinType::Float)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 444 | E = ImpCastExprToType(E, Context.DoubleTy, CK_FloatingCast).take(); |
| 445 | |
| 446 | return Owned(E); |
Chris Lattner | 2ce500f | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Chris Lattner | a8a7d0f | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 449 | /// DefaultVariadicArgumentPromotion - Like DefaultArgumentPromotion, but |
| 450 | /// 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] | 451 | /// interfaces passed by value. |
| 452 | ExprResult Sema::DefaultVariadicArgumentPromotion(Expr *E, VariadicCallType CT, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 453 | FunctionDecl *FDecl) { |
Douglas Gregor | cbd446d | 2011-06-17 00:15:10 +0000 | [diff] [blame] | 454 | ExprResult ExprRes = CheckPlaceholderExpr(E); |
| 455 | if (ExprRes.isInvalid()) |
| 456 | return ExprError(); |
| 457 | |
| 458 | ExprRes = DefaultArgumentPromotion(E); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 459 | if (ExprRes.isInvalid()) |
| 460 | return ExprError(); |
| 461 | E = ExprRes.take(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 462 | |
Chris Lattner | bb53efb | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 463 | // __builtin_va_start takes the second argument as a "varargs" argument, but |
| 464 | // it doesn't actually do anything with it. It doesn't need to be non-pod |
| 465 | // etc. |
| 466 | if (FDecl && FDecl->getBuiltinID() == Builtin::BI__builtin_va_start) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 467 | return Owned(E); |
Chris Lattner | bb53efb | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 468 | |
Douglas Gregor | 347e0f2 | 2011-05-21 19:26:31 +0000 | [diff] [blame] | 469 | // 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] | 470 | if (E->getType()->isObjCObjectType() && |
Douglas Gregor | 347e0f2 | 2011-05-21 19:26:31 +0000 | [diff] [blame] | 471 | DiagRuntimeBehavior(E->getLocStart(), 0, |
| 472 | PDiag(diag::err_cannot_pass_objc_interface_to_vararg) |
| 473 | << E->getType() << CT)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 474 | return ExprError(); |
Douglas Gregor | 347e0f2 | 2011-05-21 19:26:31 +0000 | [diff] [blame] | 475 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 476 | if (!E->getType().isPODType(Context)) { |
Douglas Gregor | 253cadf | 2011-05-21 16:27:21 +0000 | [diff] [blame] | 477 | // C++0x [expr.call]p7: |
| 478 | // Passing a potentially-evaluated argument of class type (Clause 9) |
| 479 | // having a non-trivial copy constructor, a non-trivial move constructor, |
| 480 | // or a non-trivial destructor, with no corresponding parameter, |
| 481 | // is conditionally-supported with implementation-defined semantics. |
| 482 | bool TrivialEnough = false; |
| 483 | if (getLangOptions().CPlusPlus0x && !E->getType()->isDependentType()) { |
| 484 | if (CXXRecordDecl *Record = E->getType()->getAsCXXRecordDecl()) { |
| 485 | if (Record->hasTrivialCopyConstructor() && |
| 486 | Record->hasTrivialMoveConstructor() && |
| 487 | Record->hasTrivialDestructor()) |
| 488 | TrivialEnough = true; |
| 489 | } |
| 490 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 491 | |
| 492 | if (!TrivialEnough && |
| 493 | getLangOptions().ObjCAutoRefCount && |
| 494 | E->getType()->isObjCLifetimeType()) |
| 495 | TrivialEnough = true; |
Douglas Gregor | 253cadf | 2011-05-21 16:27:21 +0000 | [diff] [blame] | 496 | |
| 497 | if (TrivialEnough) { |
| 498 | // Nothing to diagnose. This is okay. |
| 499 | } else if (DiagRuntimeBehavior(E->getLocStart(), 0, |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 500 | PDiag(diag::warn_cannot_pass_non_pod_arg_to_vararg) |
Douglas Gregor | 253cadf | 2011-05-21 16:27:21 +0000 | [diff] [blame] | 501 | << getLangOptions().CPlusPlus0x << E->getType() |
Douglas Gregor | 347e0f2 | 2011-05-21 19:26:31 +0000 | [diff] [blame] | 502 | << CT)) { |
| 503 | // Turn this into a trap. |
| 504 | CXXScopeSpec SS; |
| 505 | UnqualifiedId Name; |
| 506 | Name.setIdentifier(PP.getIdentifierInfo("__builtin_trap"), |
| 507 | E->getLocStart()); |
| 508 | ExprResult TrapFn = ActOnIdExpression(TUScope, SS, Name, true, false); |
| 509 | if (TrapFn.isInvalid()) |
| 510 | return ExprError(); |
| 511 | |
| 512 | ExprResult Call = ActOnCallExpr(TUScope, TrapFn.get(), E->getLocStart(), |
| 513 | MultiExprArg(), E->getLocEnd()); |
| 514 | if (Call.isInvalid()) |
| 515 | return ExprError(); |
| 516 | |
| 517 | ExprResult Comma = ActOnBinOp(TUScope, E->getLocStart(), tok::comma, |
| 518 | Call.get(), E); |
| 519 | if (Comma.isInvalid()) |
| 520 | return ExprError(); |
| 521 | |
| 522 | E = Comma.get(); |
| 523 | } |
Douglas Gregor | 253cadf | 2011-05-21 16:27:21 +0000 | [diff] [blame] | 524 | } |
| 525 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 526 | return Owned(E); |
Anders Carlsson | a7d069d | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 527 | } |
| 528 | |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 529 | /// UsualArithmeticConversions - Performs various conversions that are common to |
| 530 | /// 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] | 531 | /// routine returns the first non-arithmetic type found. The client is |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 532 | /// responsible for emitting appropriate error diagnostics. |
| 533 | /// FIXME: verify the conversion rules for "complex int" are consistent with |
| 534 | /// GCC. |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 535 | QualType Sema::UsualArithmeticConversions(ExprResult &lhsExpr, |
| 536 | ExprResult &rhsExpr, |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 537 | bool isCompAssign) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 538 | if (!isCompAssign) { |
| 539 | lhsExpr = UsualUnaryConversions(lhsExpr.take()); |
| 540 | if (lhsExpr.isInvalid()) |
| 541 | return QualType(); |
| 542 | } |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 543 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 544 | rhsExpr = UsualUnaryConversions(rhsExpr.take()); |
| 545 | if (rhsExpr.isInvalid()) |
| 546 | return QualType(); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 547 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 548 | // For conversion purposes, we ignore any qualifiers. |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 549 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 550 | QualType lhs = |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 551 | Context.getCanonicalType(lhsExpr.get()->getType()).getUnqualifiedType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 552 | QualType rhs = |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 553 | Context.getCanonicalType(rhsExpr.get()->getType()).getUnqualifiedType(); |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 554 | |
| 555 | // If both types are identical, no conversion is needed. |
| 556 | if (lhs == rhs) |
| 557 | return lhs; |
| 558 | |
| 559 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 560 | // The caller can deal with this (e.g. pointer + int). |
| 561 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
| 562 | return lhs; |
| 563 | |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 564 | // Apply unary and bitfield promotions to the LHS's type. |
| 565 | QualType lhs_unpromoted = lhs; |
| 566 | if (lhs->isPromotableIntegerType()) |
| 567 | lhs = Context.getPromotedIntegerType(lhs); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 568 | QualType LHSBitfieldPromoteTy = Context.isPromotableBitField(lhsExpr.get()); |
Douglas Gregor | d2c2d17 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 569 | if (!LHSBitfieldPromoteTy.isNull()) |
| 570 | lhs = LHSBitfieldPromoteTy; |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 571 | if (lhs != lhs_unpromoted && !isCompAssign) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 572 | lhsExpr = ImpCastExprToType(lhsExpr.take(), lhs, CK_IntegralCast); |
Douglas Gregor | d2c2d17 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 573 | |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 574 | // If both types are identical, no conversion is needed. |
| 575 | if (lhs == rhs) |
| 576 | return lhs; |
| 577 | |
| 578 | // At this point, we have two different arithmetic types. |
| 579 | |
| 580 | // Handle complex types first (C99 6.3.1.8p1). |
| 581 | bool LHSComplexFloat = lhs->isComplexType(); |
| 582 | bool RHSComplexFloat = rhs->isComplexType(); |
| 583 | if (LHSComplexFloat || RHSComplexFloat) { |
| 584 | // if we have an integer operand, the result is the complex type. |
| 585 | |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 586 | if (!RHSComplexFloat && !rhs->isRealFloatingType()) { |
| 587 | if (rhs->isIntegerType()) { |
| 588 | QualType fp = cast<ComplexType>(lhs)->getElementType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 589 | rhsExpr = ImpCastExprToType(rhsExpr.take(), fp, CK_IntegralToFloating); |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 590 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, |
| 591 | CK_FloatingRealToComplex); |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 592 | } else { |
| 593 | assert(rhs->isComplexIntegerType()); |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 594 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, |
| 595 | CK_IntegralComplexToFloatingComplex); |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 596 | } |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 597 | return lhs; |
| 598 | } |
| 599 | |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 600 | if (!LHSComplexFloat && !lhs->isRealFloatingType()) { |
| 601 | if (!isCompAssign) { |
| 602 | // int -> float -> _Complex float |
| 603 | if (lhs->isIntegerType()) { |
| 604 | QualType fp = cast<ComplexType>(rhs)->getElementType(); |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 605 | lhsExpr = ImpCastExprToType(lhsExpr.take(), fp, |
| 606 | CK_IntegralToFloating); |
| 607 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, |
| 608 | CK_FloatingRealToComplex); |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 609 | } else { |
| 610 | assert(lhs->isComplexIntegerType()); |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 611 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, |
| 612 | CK_IntegralComplexToFloatingComplex); |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 613 | } |
| 614 | } |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 615 | return rhs; |
| 616 | } |
| 617 | |
| 618 | // This handles complex/complex, complex/float, or float/complex. |
| 619 | // When both operands are complex, the shorter operand is converted to the |
| 620 | // type of the longer, and that is the type of the result. This corresponds |
| 621 | // to what is done when combining two real floating-point operands. |
| 622 | // The fun begins when size promotion occur across type domains. |
| 623 | // From H&S 6.3.4: When one operand is complex and the other is a real |
| 624 | // floating-point type, the less precise type is converted, within it's |
| 625 | // real or complex domain, to the precision of the other type. For example, |
| 626 | // when combining a "long double" with a "double _Complex", the |
| 627 | // "double _Complex" is promoted to "long double _Complex". |
| 628 | int order = Context.getFloatingTypeOrder(lhs, rhs); |
| 629 | |
| 630 | // If both are complex, just cast to the more precise type. |
| 631 | if (LHSComplexFloat && RHSComplexFloat) { |
| 632 | if (order > 0) { |
| 633 | // _Complex float -> _Complex double |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 634 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, |
| 635 | CK_FloatingComplexCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 636 | return lhs; |
| 637 | |
| 638 | } else if (order < 0) { |
| 639 | // _Complex float -> _Complex double |
| 640 | if (!isCompAssign) |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 641 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, |
| 642 | CK_FloatingComplexCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 643 | return rhs; |
| 644 | } |
| 645 | return lhs; |
| 646 | } |
| 647 | |
| 648 | // If just the LHS is complex, the RHS needs to be converted, |
| 649 | // and the LHS might need to be promoted. |
| 650 | if (LHSComplexFloat) { |
| 651 | if (order > 0) { // LHS is wider |
| 652 | // float -> _Complex double |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 653 | QualType fp = cast<ComplexType>(lhs)->getElementType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 654 | rhsExpr = ImpCastExprToType(rhsExpr.take(), fp, CK_FloatingCast); |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 655 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, |
| 656 | CK_FloatingRealToComplex); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 657 | return lhs; |
| 658 | } |
| 659 | |
| 660 | // RHS is at least as wide. Find its corresponding complex type. |
| 661 | QualType result = (order == 0 ? lhs : Context.getComplexType(rhs)); |
| 662 | |
| 663 | // double -> _Complex double |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 664 | rhsExpr = ImpCastExprToType(rhsExpr.take(), result, |
| 665 | CK_FloatingRealToComplex); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 666 | |
| 667 | // _Complex float -> _Complex double |
| 668 | if (!isCompAssign && order < 0) |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 669 | lhsExpr = ImpCastExprToType(lhsExpr.take(), result, |
| 670 | CK_FloatingComplexCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 671 | |
| 672 | return result; |
| 673 | } |
| 674 | |
| 675 | // Just the RHS is complex, so the LHS needs to be converted |
| 676 | // and the RHS might need to be promoted. |
| 677 | assert(RHSComplexFloat); |
| 678 | |
| 679 | if (order < 0) { // RHS is wider |
| 680 | // float -> _Complex double |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 681 | if (!isCompAssign) { |
Argyrios Kyrtzidis | e84389b | 2011-01-18 18:49:33 +0000 | [diff] [blame] | 682 | QualType fp = cast<ComplexType>(rhs)->getElementType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 683 | lhsExpr = ImpCastExprToType(lhsExpr.take(), fp, CK_FloatingCast); |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 684 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, |
| 685 | CK_FloatingRealToComplex); |
John McCall | c5e62b4 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 686 | } |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 687 | return rhs; |
| 688 | } |
| 689 | |
| 690 | // LHS is at least as wide. Find its corresponding complex type. |
| 691 | QualType result = (order == 0 ? rhs : Context.getComplexType(lhs)); |
| 692 | |
| 693 | // double -> _Complex double |
| 694 | if (!isCompAssign) |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 695 | lhsExpr = ImpCastExprToType(lhsExpr.take(), result, |
| 696 | CK_FloatingRealToComplex); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 697 | |
| 698 | // _Complex float -> _Complex double |
| 699 | if (order > 0) |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 700 | rhsExpr = ImpCastExprToType(rhsExpr.take(), result, |
| 701 | CK_FloatingComplexCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 702 | |
| 703 | return result; |
| 704 | } |
| 705 | |
| 706 | // Now handle "real" floating types (i.e. float, double, long double). |
| 707 | bool LHSFloat = lhs->isRealFloatingType(); |
| 708 | bool RHSFloat = rhs->isRealFloatingType(); |
| 709 | if (LHSFloat || RHSFloat) { |
| 710 | // If we have two real floating types, convert the smaller operand |
| 711 | // to the bigger result. |
| 712 | if (LHSFloat && RHSFloat) { |
| 713 | int order = Context.getFloatingTypeOrder(lhs, rhs); |
| 714 | if (order > 0) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 715 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_FloatingCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 716 | return lhs; |
| 717 | } |
| 718 | |
| 719 | assert(order < 0 && "illegal float comparison"); |
| 720 | if (!isCompAssign) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 721 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_FloatingCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 722 | return rhs; |
| 723 | } |
| 724 | |
| 725 | // If we have an integer operand, the result is the real floating type. |
| 726 | if (LHSFloat) { |
| 727 | if (rhs->isIntegerType()) { |
| 728 | // Convert rhs to the lhs floating point type. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 729 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralToFloating); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 730 | return lhs; |
| 731 | } |
| 732 | |
| 733 | // Convert both sides to the appropriate complex float. |
| 734 | assert(rhs->isComplexIntegerType()); |
| 735 | QualType result = Context.getComplexType(lhs); |
| 736 | |
| 737 | // _Complex int -> _Complex float |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 738 | rhsExpr = ImpCastExprToType(rhsExpr.take(), result, |
| 739 | CK_IntegralComplexToFloatingComplex); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 740 | |
| 741 | // float -> _Complex float |
| 742 | if (!isCompAssign) |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 743 | lhsExpr = ImpCastExprToType(lhsExpr.take(), result, |
| 744 | CK_FloatingRealToComplex); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 745 | |
| 746 | return result; |
| 747 | } |
| 748 | |
| 749 | assert(RHSFloat); |
| 750 | if (lhs->isIntegerType()) { |
| 751 | // Convert lhs to the rhs floating point type. |
| 752 | if (!isCompAssign) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 753 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralToFloating); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 754 | return rhs; |
| 755 | } |
| 756 | |
| 757 | // Convert both sides to the appropriate complex float. |
| 758 | assert(lhs->isComplexIntegerType()); |
| 759 | QualType result = Context.getComplexType(rhs); |
| 760 | |
| 761 | // _Complex int -> _Complex float |
| 762 | if (!isCompAssign) |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 763 | lhsExpr = ImpCastExprToType(lhsExpr.take(), result, |
| 764 | CK_IntegralComplexToFloatingComplex); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 765 | |
| 766 | // float -> _Complex float |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 767 | rhsExpr = ImpCastExprToType(rhsExpr.take(), result, |
| 768 | CK_FloatingRealToComplex); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 769 | |
| 770 | return result; |
| 771 | } |
| 772 | |
| 773 | // Handle GCC complex int extension. |
| 774 | // FIXME: if the operands are (int, _Complex long), we currently |
| 775 | // don't promote the complex. Also, signedness? |
| 776 | const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType(); |
| 777 | const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType(); |
| 778 | if (lhsComplexInt && rhsComplexInt) { |
| 779 | int order = Context.getIntegerTypeOrder(lhsComplexInt->getElementType(), |
| 780 | rhsComplexInt->getElementType()); |
| 781 | assert(order && "inequal types with equal element ordering"); |
| 782 | if (order > 0) { |
| 783 | // _Complex int -> _Complex long |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 784 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralComplexCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 785 | return lhs; |
| 786 | } |
| 787 | |
| 788 | if (!isCompAssign) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 789 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralComplexCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 790 | return rhs; |
| 791 | } else if (lhsComplexInt) { |
| 792 | // int -> _Complex int |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 793 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralRealToComplex); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 794 | return lhs; |
| 795 | } else if (rhsComplexInt) { |
| 796 | // int -> _Complex int |
| 797 | if (!isCompAssign) |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 798 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, |
| 799 | CK_IntegralRealToComplex); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 800 | return rhs; |
| 801 | } |
| 802 | |
| 803 | // Finally, we have two differing integer types. |
| 804 | // The rules for this case are in C99 6.3.1.8 |
| 805 | int compare = Context.getIntegerTypeOrder(lhs, rhs); |
| 806 | bool lhsSigned = lhs->hasSignedIntegerRepresentation(), |
| 807 | rhsSigned = rhs->hasSignedIntegerRepresentation(); |
| 808 | if (lhsSigned == rhsSigned) { |
| 809 | // Same signedness; use the higher-ranked type |
| 810 | if (compare >= 0) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 811 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 812 | return lhs; |
| 813 | } else if (!isCompAssign) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 814 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 815 | return rhs; |
| 816 | } else if (compare != (lhsSigned ? 1 : -1)) { |
| 817 | // The unsigned type has greater than or equal rank to the |
| 818 | // signed type, so use the unsigned type |
| 819 | if (rhsSigned) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 820 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 821 | return lhs; |
| 822 | } else if (!isCompAssign) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 823 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 824 | return rhs; |
| 825 | } else if (Context.getIntWidth(lhs) != Context.getIntWidth(rhs)) { |
| 826 | // The two types are different widths; if we are here, that |
| 827 | // means the signed type is larger than the unsigned type, so |
| 828 | // use the signed type. |
| 829 | if (lhsSigned) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 830 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 831 | return lhs; |
| 832 | } else if (!isCompAssign) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 833 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 834 | return rhs; |
| 835 | } else { |
| 836 | // The signed type is higher-ranked than the unsigned type, |
| 837 | // but isn't actually any bigger (like unsigned int and long |
| 838 | // on most 32-bit systems). Use the unsigned type corresponding |
| 839 | // to the signed type. |
| 840 | QualType result = |
| 841 | Context.getCorrespondingUnsignedType(lhsSigned ? lhs : rhs); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 842 | rhsExpr = ImpCastExprToType(rhsExpr.take(), result, CK_IntegralCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 843 | if (!isCompAssign) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 844 | lhsExpr = ImpCastExprToType(lhsExpr.take(), result, CK_IntegralCast); |
John McCall | d005ac9 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 845 | return result; |
| 846 | } |
Douglas Gregor | a11693b | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 847 | } |
| 848 | |
Chris Lattner | 513165e | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 849 | //===----------------------------------------------------------------------===// |
| 850 | // Semantic Analysis for various Expression Types |
| 851 | //===----------------------------------------------------------------------===// |
| 852 | |
| 853 | |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 854 | ExprResult |
| 855 | Sema::ActOnGenericSelectionExpr(SourceLocation KeyLoc, |
| 856 | SourceLocation DefaultLoc, |
| 857 | SourceLocation RParenLoc, |
| 858 | Expr *ControllingExpr, |
| 859 | MultiTypeArg types, |
| 860 | MultiExprArg exprs) { |
| 861 | unsigned NumAssocs = types.size(); |
| 862 | assert(NumAssocs == exprs.size()); |
| 863 | |
| 864 | ParsedType *ParsedTypes = types.release(); |
| 865 | Expr **Exprs = exprs.release(); |
| 866 | |
| 867 | TypeSourceInfo **Types = new TypeSourceInfo*[NumAssocs]; |
| 868 | for (unsigned i = 0; i < NumAssocs; ++i) { |
| 869 | if (ParsedTypes[i]) |
| 870 | (void) GetTypeFromParser(ParsedTypes[i], &Types[i]); |
| 871 | else |
| 872 | Types[i] = 0; |
| 873 | } |
| 874 | |
| 875 | ExprResult ER = CreateGenericSelectionExpr(KeyLoc, DefaultLoc, RParenLoc, |
| 876 | ControllingExpr, Types, Exprs, |
| 877 | NumAssocs); |
Benjamin Kramer | 3462376 | 2011-04-15 11:21:57 +0000 | [diff] [blame] | 878 | delete [] Types; |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 879 | return ER; |
| 880 | } |
| 881 | |
| 882 | ExprResult |
| 883 | Sema::CreateGenericSelectionExpr(SourceLocation KeyLoc, |
| 884 | SourceLocation DefaultLoc, |
| 885 | SourceLocation RParenLoc, |
| 886 | Expr *ControllingExpr, |
| 887 | TypeSourceInfo **Types, |
| 888 | Expr **Exprs, |
| 889 | unsigned NumAssocs) { |
| 890 | bool TypeErrorFound = false, |
| 891 | IsResultDependent = ControllingExpr->isTypeDependent(), |
| 892 | ContainsUnexpandedParameterPack |
| 893 | = ControllingExpr->containsUnexpandedParameterPack(); |
| 894 | |
| 895 | for (unsigned i = 0; i < NumAssocs; ++i) { |
| 896 | if (Exprs[i]->containsUnexpandedParameterPack()) |
| 897 | ContainsUnexpandedParameterPack = true; |
| 898 | |
| 899 | if (Types[i]) { |
| 900 | if (Types[i]->getType()->containsUnexpandedParameterPack()) |
| 901 | ContainsUnexpandedParameterPack = true; |
| 902 | |
| 903 | if (Types[i]->getType()->isDependentType()) { |
| 904 | IsResultDependent = true; |
| 905 | } else { |
| 906 | // C1X 6.5.1.1p2 "The type name in a generic association shall specify a |
| 907 | // complete object type other than a variably modified type." |
| 908 | unsigned D = 0; |
| 909 | if (Types[i]->getType()->isIncompleteType()) |
| 910 | D = diag::err_assoc_type_incomplete; |
| 911 | else if (!Types[i]->getType()->isObjectType()) |
| 912 | D = diag::err_assoc_type_nonobject; |
| 913 | else if (Types[i]->getType()->isVariablyModifiedType()) |
| 914 | D = diag::err_assoc_type_variably_modified; |
| 915 | |
| 916 | if (D != 0) { |
| 917 | Diag(Types[i]->getTypeLoc().getBeginLoc(), D) |
| 918 | << Types[i]->getTypeLoc().getSourceRange() |
| 919 | << Types[i]->getType(); |
| 920 | TypeErrorFound = true; |
| 921 | } |
| 922 | |
| 923 | // C1X 6.5.1.1p2 "No two generic associations in the same generic |
| 924 | // selection shall specify compatible types." |
| 925 | for (unsigned j = i+1; j < NumAssocs; ++j) |
| 926 | if (Types[j] && !Types[j]->getType()->isDependentType() && |
| 927 | Context.typesAreCompatible(Types[i]->getType(), |
| 928 | Types[j]->getType())) { |
| 929 | Diag(Types[j]->getTypeLoc().getBeginLoc(), |
| 930 | diag::err_assoc_compatible_types) |
| 931 | << Types[j]->getTypeLoc().getSourceRange() |
| 932 | << Types[j]->getType() |
| 933 | << Types[i]->getType(); |
| 934 | Diag(Types[i]->getTypeLoc().getBeginLoc(), |
| 935 | diag::note_compat_assoc) |
| 936 | << Types[i]->getTypeLoc().getSourceRange() |
| 937 | << Types[i]->getType(); |
| 938 | TypeErrorFound = true; |
| 939 | } |
| 940 | } |
| 941 | } |
| 942 | } |
| 943 | if (TypeErrorFound) |
| 944 | return ExprError(); |
| 945 | |
| 946 | // If we determined that the generic selection is result-dependent, don't |
| 947 | // try to compute the result expression. |
| 948 | if (IsResultDependent) |
| 949 | return Owned(new (Context) GenericSelectionExpr( |
| 950 | Context, KeyLoc, ControllingExpr, |
| 951 | Types, Exprs, NumAssocs, DefaultLoc, |
| 952 | RParenLoc, ContainsUnexpandedParameterPack)); |
| 953 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 954 | SmallVector<unsigned, 1> CompatIndices; |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 955 | unsigned DefaultIndex = -1U; |
| 956 | for (unsigned i = 0; i < NumAssocs; ++i) { |
| 957 | if (!Types[i]) |
| 958 | DefaultIndex = i; |
| 959 | else if (Context.typesAreCompatible(ControllingExpr->getType(), |
| 960 | Types[i]->getType())) |
| 961 | CompatIndices.push_back(i); |
| 962 | } |
| 963 | |
| 964 | // C1X 6.5.1.1p2 "The controlling expression of a generic selection shall have |
| 965 | // type compatible with at most one of the types named in its generic |
| 966 | // association list." |
| 967 | if (CompatIndices.size() > 1) { |
| 968 | // We strip parens here because the controlling expression is typically |
| 969 | // parenthesized in macro definitions. |
| 970 | ControllingExpr = ControllingExpr->IgnoreParens(); |
| 971 | Diag(ControllingExpr->getLocStart(), diag::err_generic_sel_multi_match) |
| 972 | << ControllingExpr->getSourceRange() << ControllingExpr->getType() |
| 973 | << (unsigned) CompatIndices.size(); |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 974 | for (SmallVector<unsigned, 1>::iterator I = CompatIndices.begin(), |
Peter Collingbourne | 9114759 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 975 | E = CompatIndices.end(); I != E; ++I) { |
| 976 | Diag(Types[*I]->getTypeLoc().getBeginLoc(), |
| 977 | diag::note_compat_assoc) |
| 978 | << Types[*I]->getTypeLoc().getSourceRange() |
| 979 | << Types[*I]->getType(); |
| 980 | } |
| 981 | return ExprError(); |
| 982 | } |
| 983 | |
| 984 | // C1X 6.5.1.1p2 "If a generic selection has no default generic association, |
| 985 | // its controlling expression shall have type compatible with exactly one of |
| 986 | // the types named in its generic association list." |
| 987 | if (DefaultIndex == -1U && CompatIndices.size() == 0) { |
| 988 | // We strip parens here because the controlling expression is typically |
| 989 | // parenthesized in macro definitions. |
| 990 | ControllingExpr = ControllingExpr->IgnoreParens(); |
| 991 | Diag(ControllingExpr->getLocStart(), diag::err_generic_sel_no_match) |
| 992 | << ControllingExpr->getSourceRange() << ControllingExpr->getType(); |
| 993 | return ExprError(); |
| 994 | } |
| 995 | |
| 996 | // C1X 6.5.1.1p3 "If a generic selection has a generic association with a |
| 997 | // type name that is compatible with the type of the controlling expression, |
| 998 | // then the result expression of the generic selection is the expression |
| 999 | // in that generic association. Otherwise, the result expression of the |
| 1000 | // generic selection is the expression in the default generic association." |
| 1001 | unsigned ResultIndex = |
| 1002 | CompatIndices.size() ? CompatIndices[0] : DefaultIndex; |
| 1003 | |
| 1004 | return Owned(new (Context) GenericSelectionExpr( |
| 1005 | Context, KeyLoc, ControllingExpr, |
| 1006 | Types, Exprs, NumAssocs, DefaultLoc, |
| 1007 | RParenLoc, ContainsUnexpandedParameterPack, |
| 1008 | ResultIndex)); |
| 1009 | } |
| 1010 | |
Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 1011 | /// ActOnStringLiteral - The specified tokens were lexed as pasted string |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 1012 | /// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string |
| 1013 | /// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from |
| 1014 | /// multiple tokens. However, the common case is that StringToks points to one |
| 1015 | /// string. |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1016 | /// |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1017 | ExprResult |
Alexis Hunt | 3b79186 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 1018 | Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) { |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 1019 | assert(NumStringToks && "Must have at least one string!"); |
| 1020 | |
Chris Lattner | 8a24e58 | 2009-01-16 18:51:42 +0000 | [diff] [blame] | 1021 | StringLiteralParser Literal(StringToks, NumStringToks, PP); |
Steve Naroff | 4f88b31 | 2007-03-13 22:37:02 +0000 | [diff] [blame] | 1022 | if (Literal.hadError) |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1023 | return ExprError(); |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 1024 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1025 | SmallVector<SourceLocation, 4> StringTokLocs; |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 1026 | for (unsigned i = 0; i != NumStringToks; ++i) |
| 1027 | StringTokLocs.push_back(StringToks[i].getLocation()); |
Chris Lattner | 36fc879 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 1028 | |
Chris Lattner | 36fc879 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 1029 | QualType StrTy = Context.CharTy; |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1030 | if (Literal.isWide()) |
Anders Carlsson | 6b06e18 | 2011-04-06 18:42:48 +0000 | [diff] [blame] | 1031 | StrTy = Context.getWCharType(); |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1032 | else if (Literal.isUTF16()) |
| 1033 | StrTy = Context.Char16Ty; |
| 1034 | else if (Literal.isUTF32()) |
| 1035 | StrTy = Context.Char32Ty; |
Anders Carlsson | 6b06e18 | 2011-04-06 18:42:48 +0000 | [diff] [blame] | 1036 | else if (Literal.Pascal) |
| 1037 | StrTy = Context.UnsignedCharTy; |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 1038 | |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1039 | StringLiteral::StringKind Kind = StringLiteral::Ascii; |
| 1040 | if (Literal.isWide()) |
| 1041 | Kind = StringLiteral::Wide; |
| 1042 | else if (Literal.isUTF8()) |
| 1043 | Kind = StringLiteral::UTF8; |
| 1044 | else if (Literal.isUTF16()) |
| 1045 | Kind = StringLiteral::UTF16; |
| 1046 | else if (Literal.isUTF32()) |
| 1047 | Kind = StringLiteral::UTF32; |
| 1048 | |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 1049 | // 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] | 1050 | if (getLangOptions().CPlusPlus || getLangOptions().ConstStrings) |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 1051 | StrTy.addConst(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1052 | |
Chris Lattner | 36fc879 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 1053 | // Get an array type for the string, according to C99 6.4.5. This includes |
| 1054 | // the nul terminator character as well as the string length for pascal |
| 1055 | // strings. |
| 1056 | StrTy = Context.getConstantArrayType(StrTy, |
Chris Lattner | d42c29f | 2009-02-26 23:01:51 +0000 | [diff] [blame] | 1057 | llvm::APInt(32, Literal.GetNumStringChars()+1), |
Chris Lattner | 36fc879 | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 1058 | ArrayType::Normal, 0); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1059 | |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 1060 | // Pass &StringTokLocs[0], StringTokLocs.size() to factory! |
Alexis Hunt | 3b79186 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 1061 | return Owned(StringLiteral::Create(Context, Literal.GetString(), |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1062 | Kind, Literal.Pascal, StrTy, |
Alexis Hunt | 3b79186 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 1063 | &StringTokLocs[0], |
| 1064 | StringTokLocs.size())); |
Chris Lattner | 5b183d8 | 2006-11-10 05:03:26 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1067 | enum CaptureResult { |
| 1068 | /// No capture is required. |
| 1069 | CR_NoCapture, |
| 1070 | |
| 1071 | /// A capture is required. |
| 1072 | CR_Capture, |
| 1073 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1074 | /// A by-ref capture is required. |
| 1075 | CR_CaptureByRef, |
| 1076 | |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1077 | /// An error occurred when trying to capture the given variable. |
| 1078 | CR_Error |
| 1079 | }; |
| 1080 | |
| 1081 | /// Diagnose an uncapturable value reference. |
Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1082 | /// |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1083 | /// \param var - the variable referenced |
| 1084 | /// \param DC - the context which we couldn't capture through |
| 1085 | static CaptureResult |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1086 | diagnoseUncapturableValueReference(Sema &S, SourceLocation loc, |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1087 | VarDecl *var, DeclContext *DC) { |
| 1088 | switch (S.ExprEvalContexts.back().Context) { |
| 1089 | case Sema::Unevaluated: |
| 1090 | // The argument will never be evaluated, so don't complain. |
| 1091 | return CR_NoCapture; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1092 | |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1093 | case Sema::PotentiallyEvaluated: |
| 1094 | case Sema::PotentiallyEvaluatedIfUsed: |
| 1095 | break; |
Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1096 | |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1097 | case Sema::PotentiallyPotentiallyEvaluated: |
| 1098 | // FIXME: delay these! |
| 1099 | break; |
Chris Lattner | 497d7b0 | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 1100 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1101 | |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1102 | // Don't diagnose about capture if we're not actually in code right |
| 1103 | // now; in general, there are more appropriate places that will |
| 1104 | // diagnose this. |
| 1105 | if (!S.CurContext->isFunctionOrMethod()) return CR_NoCapture; |
| 1106 | |
John McCall | 92d627e | 2011-03-22 23:15:50 +0000 | [diff] [blame] | 1107 | // Certain madnesses can happen with parameter declarations, which |
| 1108 | // we want to ignore. |
| 1109 | if (isa<ParmVarDecl>(var)) { |
| 1110 | // - If the parameter still belongs to the translation unit, then |
| 1111 | // we're actually just using one parameter in the declaration of |
| 1112 | // the next. This is useful in e.g. VLAs. |
| 1113 | if (isa<TranslationUnitDecl>(var->getDeclContext())) |
| 1114 | return CR_NoCapture; |
| 1115 | |
| 1116 | // - This particular madness can happen in ill-formed default |
| 1117 | // arguments; claim it's okay and let downstream code handle it. |
| 1118 | if (S.CurContext == var->getDeclContext()->getParent()) |
| 1119 | return CR_NoCapture; |
| 1120 | } |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1121 | |
| 1122 | DeclarationName functionName; |
| 1123 | if (FunctionDecl *fn = dyn_cast<FunctionDecl>(var->getDeclContext())) |
| 1124 | functionName = fn->getDeclName(); |
| 1125 | // FIXME: variable from enclosing block that we couldn't capture from! |
| 1126 | |
| 1127 | S.Diag(loc, diag::err_reference_to_local_var_in_enclosing_function) |
| 1128 | << var->getIdentifier() << functionName; |
| 1129 | S.Diag(var->getLocation(), diag::note_local_variable_declared_here) |
| 1130 | << var->getIdentifier(); |
| 1131 | |
| 1132 | return CR_Error; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1135 | /// There is a well-formed capture at a particular scope level; |
| 1136 | /// propagate it through all the nested blocks. |
| 1137 | static CaptureResult propagateCapture(Sema &S, unsigned validScopeIndex, |
| 1138 | const BlockDecl::Capture &capture) { |
| 1139 | VarDecl *var = capture.getVariable(); |
| 1140 | |
| 1141 | // Update all the inner blocks with the capture information. |
| 1142 | for (unsigned i = validScopeIndex + 1, e = S.FunctionScopes.size(); |
| 1143 | i != e; ++i) { |
| 1144 | BlockScopeInfo *innerBlock = cast<BlockScopeInfo>(S.FunctionScopes[i]); |
| 1145 | innerBlock->Captures.push_back( |
| 1146 | BlockDecl::Capture(capture.getVariable(), capture.isByRef(), |
| 1147 | /*nested*/ true, capture.getCopyExpr())); |
| 1148 | innerBlock->CaptureMap[var] = innerBlock->Captures.size(); // +1 |
| 1149 | } |
| 1150 | |
| 1151 | return capture.isByRef() ? CR_CaptureByRef : CR_Capture; |
| 1152 | } |
| 1153 | |
| 1154 | /// shouldCaptureValueReference - Determine if a reference to the |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1155 | /// given value in the current context requires a variable capture. |
| 1156 | /// |
| 1157 | /// This also keeps the captures set in the BlockScopeInfo records |
| 1158 | /// up-to-date. |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1159 | static CaptureResult shouldCaptureValueReference(Sema &S, SourceLocation loc, |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1160 | ValueDecl *value) { |
| 1161 | // Only variables ever require capture. |
| 1162 | VarDecl *var = dyn_cast<VarDecl>(value); |
John McCall | f4cd4f9 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1163 | if (!var) return CR_NoCapture; |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1164 | |
| 1165 | // Fast path: variables from the current context never require capture. |
| 1166 | DeclContext *DC = S.CurContext; |
| 1167 | if (var->getDeclContext() == DC) return CR_NoCapture; |
| 1168 | |
| 1169 | // Only variables with local storage require capture. |
| 1170 | // FIXME: What about 'const' variables in C++? |
| 1171 | if (!var->hasLocalStorage()) return CR_NoCapture; |
| 1172 | |
| 1173 | // Otherwise, we need to capture. |
| 1174 | |
| 1175 | unsigned functionScopesIndex = S.FunctionScopes.size() - 1; |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1176 | do { |
| 1177 | // Only blocks (and eventually C++0x closures) can capture; other |
| 1178 | // scopes don't work. |
| 1179 | if (!isa<BlockDecl>(DC)) |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1180 | return diagnoseUncapturableValueReference(S, loc, var, DC); |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1181 | |
| 1182 | BlockScopeInfo *blockScope = |
| 1183 | cast<BlockScopeInfo>(S.FunctionScopes[functionScopesIndex]); |
| 1184 | assert(blockScope->TheDecl == static_cast<BlockDecl*>(DC)); |
| 1185 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1186 | // Check whether we've already captured it in this block. If so, |
| 1187 | // we're done. |
| 1188 | if (unsigned indexPlus1 = blockScope->CaptureMap[var]) |
| 1189 | return propagateCapture(S, functionScopesIndex, |
| 1190 | blockScope->Captures[indexPlus1 - 1]); |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1191 | |
| 1192 | functionScopesIndex--; |
| 1193 | DC = cast<BlockDecl>(DC)->getDeclContext(); |
| 1194 | } while (var->getDeclContext() != DC); |
| 1195 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1196 | // Okay, we descended all the way to the block that defines the variable. |
| 1197 | // Actually try to capture it. |
| 1198 | QualType type = var->getType(); |
| 1199 | |
| 1200 | // Prohibit variably-modified types. |
| 1201 | if (type->isVariablyModifiedType()) { |
| 1202 | S.Diag(loc, diag::err_ref_vm_type); |
| 1203 | S.Diag(var->getLocation(), diag::note_declared_at); |
| 1204 | return CR_Error; |
| 1205 | } |
| 1206 | |
| 1207 | // Prohibit arrays, even in __block variables, but not references to |
| 1208 | // them. |
| 1209 | if (type->isArrayType()) { |
| 1210 | S.Diag(loc, diag::err_ref_array_type); |
| 1211 | S.Diag(var->getLocation(), diag::note_declared_at); |
| 1212 | return CR_Error; |
| 1213 | } |
| 1214 | |
| 1215 | S.MarkDeclarationReferenced(loc, var); |
| 1216 | |
| 1217 | // The BlocksAttr indicates the variable is bound by-reference. |
| 1218 | bool byRef = var->hasAttr<BlocksAttr>(); |
| 1219 | |
| 1220 | // Build a copy expression. |
| 1221 | Expr *copyExpr = 0; |
John McCall | a85af56 | 2011-04-28 02:15:35 +0000 | [diff] [blame] | 1222 | const RecordType *rtype; |
| 1223 | if (!byRef && S.getLangOptions().CPlusPlus && !type->isDependentType() && |
| 1224 | (rtype = type->getAs<RecordType>())) { |
| 1225 | |
| 1226 | // The capture logic needs the destructor, so make sure we mark it. |
| 1227 | // Usually this is unnecessary because most local variables have |
| 1228 | // their destructors marked at declaration time, but parameters are |
| 1229 | // an exception because it's technically only the call site that |
| 1230 | // actually requires the destructor. |
| 1231 | if (isa<ParmVarDecl>(var)) |
| 1232 | S.FinalizeVarWithDestructor(var, rtype); |
| 1233 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1234 | // According to the blocks spec, the capture of a variable from |
| 1235 | // the stack requires a const copy constructor. This is not true |
| 1236 | // of the copy/move done to move a __block variable to the heap. |
| 1237 | type.addConst(); |
| 1238 | |
| 1239 | Expr *declRef = new (S.Context) DeclRefExpr(var, type, VK_LValue, loc); |
| 1240 | ExprResult result = |
| 1241 | S.PerformCopyInitialization( |
| 1242 | InitializedEntity::InitializeBlock(var->getLocation(), |
| 1243 | type, false), |
| 1244 | loc, S.Owned(declRef)); |
| 1245 | |
| 1246 | // Build a full-expression copy expression if initialization |
| 1247 | // succeeded and used a non-trivial constructor. Recover from |
| 1248 | // errors by pretending that the copy isn't necessary. |
| 1249 | if (!result.isInvalid() && |
| 1250 | !cast<CXXConstructExpr>(result.get())->getConstructor()->isTrivial()) { |
| 1251 | result = S.MaybeCreateExprWithCleanups(result); |
| 1252 | copyExpr = result.take(); |
| 1253 | } |
| 1254 | } |
| 1255 | |
| 1256 | // We're currently at the declarer; go back to the closure. |
| 1257 | functionScopesIndex++; |
| 1258 | BlockScopeInfo *blockScope = |
| 1259 | cast<BlockScopeInfo>(S.FunctionScopes[functionScopesIndex]); |
| 1260 | |
| 1261 | // Build a valid capture in this scope. |
| 1262 | blockScope->Captures.push_back( |
| 1263 | BlockDecl::Capture(var, byRef, /*nested*/ false, copyExpr)); |
| 1264 | blockScope->CaptureMap[var] = blockScope->Captures.size(); // +1 |
| 1265 | |
| 1266 | // Propagate that to inner captures if necessary. |
| 1267 | return propagateCapture(S, functionScopesIndex, |
| 1268 | blockScope->Captures.back()); |
| 1269 | } |
| 1270 | |
| 1271 | static ExprResult BuildBlockDeclRefExpr(Sema &S, ValueDecl *vd, |
| 1272 | const DeclarationNameInfo &NameInfo, |
| 1273 | bool byRef) { |
| 1274 | assert(isa<VarDecl>(vd) && "capturing non-variable"); |
| 1275 | |
| 1276 | VarDecl *var = cast<VarDecl>(vd); |
| 1277 | assert(var->hasLocalStorage() && "capturing non-local"); |
| 1278 | assert(byRef == var->hasAttr<BlocksAttr>() && "byref set wrong"); |
| 1279 | |
| 1280 | QualType exprType = var->getType().getNonReferenceType(); |
| 1281 | |
| 1282 | BlockDeclRefExpr *BDRE; |
| 1283 | if (!byRef) { |
| 1284 | // The variable will be bound by copy; make it const within the |
| 1285 | // closure, but record that this was done in the expression. |
| 1286 | bool constAdded = !exprType.isConstQualified(); |
| 1287 | exprType.addConst(); |
| 1288 | |
| 1289 | BDRE = new (S.Context) BlockDeclRefExpr(var, exprType, VK_LValue, |
| 1290 | NameInfo.getLoc(), false, |
| 1291 | constAdded); |
| 1292 | } else { |
| 1293 | BDRE = new (S.Context) BlockDeclRefExpr(var, exprType, VK_LValue, |
| 1294 | NameInfo.getLoc(), true); |
| 1295 | } |
| 1296 | |
| 1297 | return S.Owned(BDRE); |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1298 | } |
Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1299 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1300 | ExprResult |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1301 | Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, |
John McCall | f4cd4f9 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1302 | SourceLocation Loc, |
| 1303 | const CXXScopeSpec *SS) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1304 | DeclarationNameInfo NameInfo(D->getDeclName(), Loc); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1305 | return BuildDeclRefExpr(D, Ty, VK, NameInfo, SS); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1306 | } |
| 1307 | |
John McCall | f4cd4f9 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1308 | /// BuildDeclRefExpr - Build an expression that references a |
| 1309 | /// declaration that does not require a closure capture. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1310 | ExprResult |
John McCall | f4cd4f9 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1311 | Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1312 | const DeclarationNameInfo &NameInfo, |
| 1313 | const CXXScopeSpec *SS) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1314 | MarkDeclarationReferenced(NameInfo.getLoc(), D); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1315 | |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1316 | Expr *E = DeclRefExpr::Create(Context, |
Douglas Gregor | ea972d3 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 1317 | SS? SS->getWithLocInContext(Context) |
| 1318 | : NestedNameSpecifierLoc(), |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1319 | D, NameInfo, Ty, VK); |
| 1320 | |
| 1321 | // Just in case we're building an illegal pointer-to-member. |
| 1322 | if (isa<FieldDecl>(D) && cast<FieldDecl>(D)->getBitWidth()) |
| 1323 | E->setObjectKind(OK_BitField); |
| 1324 | |
| 1325 | return Owned(E); |
Douglas Gregor | c7acfdf | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 1326 | } |
| 1327 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1328 | /// Decomposes the given name into a DeclarationNameInfo, its location, and |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1329 | /// possibly a list of template arguments. |
| 1330 | /// |
| 1331 | /// If this produces template arguments, it is permitted to call |
| 1332 | /// DecomposeTemplateName. |
| 1333 | /// |
| 1334 | /// This actually loses a lot of source location information for |
| 1335 | /// non-standard name kinds; we should consider preserving that in |
| 1336 | /// some way. |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 1337 | void |
| 1338 | Sema::DecomposeUnqualifiedId(const UnqualifiedId &Id, |
| 1339 | TemplateArgumentListInfo &Buffer, |
| 1340 | DeclarationNameInfo &NameInfo, |
| 1341 | const TemplateArgumentListInfo *&TemplateArgs) { |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1342 | if (Id.getKind() == UnqualifiedId::IK_TemplateId) { |
| 1343 | Buffer.setLAngleLoc(Id.TemplateId->LAngleLoc); |
| 1344 | Buffer.setRAngleLoc(Id.TemplateId->RAngleLoc); |
| 1345 | |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1346 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1347 | Id.TemplateId->getTemplateArgs(), |
| 1348 | Id.TemplateId->NumArgs); |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1349 | translateTemplateArguments(TemplateArgsPtr, Buffer); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1350 | TemplateArgsPtr.release(); |
| 1351 | |
John McCall | 3e56fd4 | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 1352 | TemplateName TName = Id.TemplateId->Template.get(); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1353 | SourceLocation TNameLoc = Id.TemplateId->TemplateNameLoc; |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1354 | NameInfo = Context.getNameForTemplate(TName, TNameLoc); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1355 | TemplateArgs = &Buffer; |
| 1356 | } else { |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1357 | NameInfo = GetNameFromUnqualifiedId(Id); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1358 | TemplateArgs = 0; |
| 1359 | } |
| 1360 | } |
| 1361 | |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1362 | /// Diagnose an empty lookup. |
| 1363 | /// |
| 1364 | /// \return false if new lookup candidates were found |
Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1365 | bool Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec &SS, LookupResult &R, |
Kaelyn Uhrain | 4283092 | 2011-08-05 00:09:52 +0000 | [diff] [blame] | 1366 | CorrectTypoContext CTC, |
| 1367 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
| 1368 | Expr **Args, unsigned NumArgs) { |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1369 | DeclarationName Name = R.getLookupName(); |
| 1370 | |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1371 | unsigned diagnostic = diag::err_undeclared_var_use; |
Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1372 | unsigned diagnostic_suggest = diag::err_undeclared_var_use_suggest; |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1373 | if (Name.getNameKind() == DeclarationName::CXXOperatorName || |
| 1374 | Name.getNameKind() == DeclarationName::CXXLiteralOperatorName || |
Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1375 | Name.getNameKind() == DeclarationName::CXXConversionFunctionName) { |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1376 | diagnostic = diag::err_undeclared_use; |
Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1377 | diagnostic_suggest = diag::err_undeclared_use_suggest; |
| 1378 | } |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1379 | |
Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1380 | // If the original lookup was an unqualified lookup, fake an |
| 1381 | // unqualified lookup. This is useful when (for example) the |
| 1382 | // original lookup would not have found something because it was a |
| 1383 | // dependent name. |
Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1384 | for (DeclContext *DC = SS.isEmpty() ? CurContext : 0; |
Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1385 | DC; DC = DC->getParent()) { |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1386 | if (isa<CXXRecordDecl>(DC)) { |
| 1387 | LookupQualifiedName(R, DC); |
| 1388 | |
| 1389 | if (!R.empty()) { |
| 1390 | // Don't give errors about ambiguities in this lookup. |
| 1391 | R.suppressDiagnostics(); |
| 1392 | |
| 1393 | CXXMethodDecl *CurMethod = dyn_cast<CXXMethodDecl>(CurContext); |
| 1394 | bool isInstance = CurMethod && |
| 1395 | CurMethod->isInstance() && |
| 1396 | DC == CurMethod->getParent(); |
| 1397 | |
| 1398 | // Give a code modification hint to insert 'this->'. |
| 1399 | // TODO: fixit for inserting 'Base<T>::' in the other cases. |
| 1400 | // Actually quite difficult! |
Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1401 | if (isInstance) { |
Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1402 | UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>( |
| 1403 | CallsUndergoingInstantiation.back()->getCallee()); |
Nick Lewycky | fe71238 | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1404 | CXXMethodDecl *DepMethod = cast_or_null<CXXMethodDecl>( |
Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1405 | CurMethod->getInstantiatedFromMemberFunction()); |
Eli Friedman | 0483192 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1406 | if (DepMethod) { |
Nick Lewycky | fe71238 | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1407 | Diag(R.getNameLoc(), diagnostic) << Name |
| 1408 | << FixItHint::CreateInsertion(R.getNameLoc(), "this->"); |
| 1409 | QualType DepThisType = DepMethod->getThisType(Context); |
| 1410 | CXXThisExpr *DepThis = new (Context) CXXThisExpr( |
| 1411 | R.getNameLoc(), DepThisType, false); |
| 1412 | TemplateArgumentListInfo TList; |
| 1413 | if (ULE->hasExplicitTemplateArgs()) |
| 1414 | ULE->copyTemplateArgumentsInto(TList); |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 1415 | |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 1416 | CXXScopeSpec SS; |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 1417 | SS.Adopt(ULE->getQualifierLoc()); |
Nick Lewycky | fe71238 | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1418 | CXXDependentScopeMemberExpr *DepExpr = |
| 1419 | CXXDependentScopeMemberExpr::Create( |
| 1420 | Context, DepThis, DepThisType, true, SourceLocation(), |
Douglas Gregor | e16af53 | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 1421 | SS.getWithLocInContext(Context), NULL, |
Nick Lewycky | fe71238 | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1422 | R.getLookupNameInfo(), &TList); |
| 1423 | CallsUndergoingInstantiation.back()->setCallee(DepExpr); |
Eli Friedman | 0483192 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1424 | } else { |
Nick Lewycky | fe71238 | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1425 | // FIXME: we should be able to handle this case too. It is correct |
| 1426 | // to add this-> here. This is a workaround for PR7947. |
| 1427 | Diag(R.getNameLoc(), diagnostic) << Name; |
Eli Friedman | 0483192 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1428 | } |
Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1429 | } else { |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1430 | Diag(R.getNameLoc(), diagnostic) << Name; |
Nick Lewycky | c96c37f | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1431 | } |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1432 | |
| 1433 | // Do we really want to note all of these? |
| 1434 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 1435 | Diag((*I)->getLocation(), diag::note_dependent_var_use); |
| 1436 | |
| 1437 | // Tell the callee to try to recover. |
| 1438 | return false; |
| 1439 | } |
Douglas Gregor | 86b8d9f | 2010-08-09 22:38:14 +0000 | [diff] [blame] | 1440 | |
| 1441 | R.clear(); |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1442 | } |
| 1443 | } |
| 1444 | |
Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1445 | // We didn't find anything, so try to correct for a typo. |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1446 | TypoCorrection Corrected; |
| 1447 | if (S && (Corrected = CorrectTypo(R.getLookupNameInfo(), R.getLookupKind(), |
| 1448 | S, &SS, NULL, false, CTC))) { |
| 1449 | std::string CorrectedStr(Corrected.getAsString(getLangOptions())); |
| 1450 | std::string CorrectedQuotedStr(Corrected.getQuoted(getLangOptions())); |
| 1451 | R.setLookupName(Corrected.getCorrection()); |
| 1452 | |
Hans Wennborg | 38198de | 2011-07-12 08:45:31 +0000 | [diff] [blame] | 1453 | if (NamedDecl *ND = Corrected.getCorrectionDecl()) { |
Kaelyn Uhrain | acbdc57 | 2011-08-03 20:36:05 +0000 | [diff] [blame] | 1454 | if (Corrected.isOverloaded()) { |
| 1455 | OverloadCandidateSet OCS(R.getNameLoc()); |
| 1456 | OverloadCandidateSet::iterator Best; |
| 1457 | for (TypoCorrection::decl_iterator CD = Corrected.begin(), |
| 1458 | CDEnd = Corrected.end(); |
| 1459 | CD != CDEnd; ++CD) { |
Kaelyn Uhrain | 6242220 | 2011-08-08 17:35:31 +0000 | [diff] [blame] | 1460 | if (FunctionTemplateDecl *FTD = |
Kaelyn Uhrain | 4283092 | 2011-08-05 00:09:52 +0000 | [diff] [blame] | 1461 | dyn_cast<FunctionTemplateDecl>(*CD)) |
| 1462 | AddTemplateOverloadCandidate( |
| 1463 | FTD, DeclAccessPair::make(FTD, AS_none), ExplicitTemplateArgs, |
| 1464 | Args, NumArgs, OCS); |
Kaelyn Uhrain | 6242220 | 2011-08-08 17:35:31 +0000 | [diff] [blame] | 1465 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*CD)) |
| 1466 | if (!ExplicitTemplateArgs || ExplicitTemplateArgs->size() == 0) |
| 1467 | AddOverloadCandidate(FD, DeclAccessPair::make(FD, AS_none), |
| 1468 | Args, NumArgs, OCS); |
Kaelyn Uhrain | acbdc57 | 2011-08-03 20:36:05 +0000 | [diff] [blame] | 1469 | } |
| 1470 | switch (OCS.BestViableFunction(*this, R.getNameLoc(), Best)) { |
| 1471 | case OR_Success: |
| 1472 | ND = Best->Function; |
| 1473 | break; |
| 1474 | default: |
Kaelyn Uhrain | ea35018 | 2011-08-04 23:30:54 +0000 | [diff] [blame] | 1475 | break; |
Kaelyn Uhrain | acbdc57 | 2011-08-03 20:36:05 +0000 | [diff] [blame] | 1476 | } |
| 1477 | } |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1478 | R.addDecl(ND); |
| 1479 | if (isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND)) { |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1480 | if (SS.isEmpty()) |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1481 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << CorrectedQuotedStr |
| 1482 | << FixItHint::CreateReplacement(R.getNameLoc(), CorrectedStr); |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1483 | else |
| 1484 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1485 | << Name << computeDeclContext(SS, false) << CorrectedQuotedStr |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1486 | << SS.getRange() |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1487 | << FixItHint::CreateReplacement(R.getNameLoc(), CorrectedStr); |
| 1488 | if (ND) |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1489 | Diag(ND->getLocation(), diag::note_previous_decl) |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1490 | << CorrectedQuotedStr; |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1491 | |
| 1492 | // Tell the callee to try to recover. |
| 1493 | return false; |
| 1494 | } |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1495 | |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1496 | if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND)) { |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1497 | // FIXME: If we ended up with a typo for a type name or |
| 1498 | // Objective-C class name, we're in trouble because the parser |
| 1499 | // is in the wrong place to recover. Suggest the typo |
| 1500 | // correction, but don't make it a fix-it since we're not going |
| 1501 | // to recover well anyway. |
| 1502 | if (SS.isEmpty()) |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 1503 | Diag(R.getNameLoc(), diagnostic_suggest) |
| 1504 | << Name << CorrectedQuotedStr; |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1505 | else |
| 1506 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1507 | << Name << computeDeclContext(SS, false) << CorrectedQuotedStr |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1508 | << SS.getRange(); |
| 1509 | |
| 1510 | // Don't try to recover; it won't work. |
| 1511 | return true; |
| 1512 | } |
| 1513 | } else { |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1514 | // 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] | 1515 | // because we aren't able to recover. |
Douglas Gregor | 2536398 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1516 | if (SS.isEmpty()) |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1517 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << CorrectedQuotedStr; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1518 | else |
Douglas Gregor | 2536398 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1519 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1520 | << Name << computeDeclContext(SS, false) << CorrectedQuotedStr |
Douglas Gregor | 280e1ee | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1521 | << SS.getRange(); |
Douglas Gregor | 2536398 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1522 | return true; |
| 1523 | } |
Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1524 | } |
Douglas Gregor | c2fa169 | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1525 | R.clear(); |
Douglas Gregor | 598b08f | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1526 | |
| 1527 | // Emit a special diagnostic for failed member lookups. |
| 1528 | // FIXME: computing the declaration context might fail here (?) |
| 1529 | if (!SS.isEmpty()) { |
| 1530 | Diag(R.getNameLoc(), diag::err_no_member) |
| 1531 | << Name << computeDeclContext(SS, false) |
| 1532 | << SS.getRange(); |
| 1533 | return true; |
| 1534 | } |
| 1535 | |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1536 | // Give up, we can't recover. |
| 1537 | Diag(R.getNameLoc(), diagnostic) << Name; |
| 1538 | return true; |
| 1539 | } |
| 1540 | |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1541 | ObjCPropertyDecl *Sema::canSynthesizeProvisionalIvar(IdentifierInfo *II) { |
| 1542 | ObjCMethodDecl *CurMeth = getCurMethodDecl(); |
Fariborz Jahanian | 8615134 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1543 | ObjCInterfaceDecl *IDecl = CurMeth->getClassInterface(); |
| 1544 | if (!IDecl) |
| 1545 | return 0; |
| 1546 | ObjCImplementationDecl *ClassImpDecl = IDecl->getImplementation(); |
| 1547 | if (!ClassImpDecl) |
| 1548 | return 0; |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1549 | ObjCPropertyDecl *property = LookupPropertyDecl(IDecl, II); |
Fariborz Jahanian | 8615134 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1550 | if (!property) |
| 1551 | return 0; |
| 1552 | if (ObjCPropertyImplDecl *PIDecl = ClassImpDecl->FindPropertyImplDecl(II)) |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1553 | if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic || |
| 1554 | PIDecl->getPropertyIvarDecl()) |
Fariborz Jahanian | 8615134 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1555 | return 0; |
| 1556 | return property; |
| 1557 | } |
| 1558 | |
Douglas Gregor | 05fcf84 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1559 | bool Sema::canSynthesizeProvisionalIvar(ObjCPropertyDecl *Property) { |
| 1560 | ObjCMethodDecl *CurMeth = getCurMethodDecl(); |
| 1561 | ObjCInterfaceDecl *IDecl = CurMeth->getClassInterface(); |
| 1562 | if (!IDecl) |
| 1563 | return false; |
| 1564 | ObjCImplementationDecl *ClassImpDecl = IDecl->getImplementation(); |
| 1565 | if (!ClassImpDecl) |
| 1566 | return false; |
| 1567 | if (ObjCPropertyImplDecl *PIDecl |
| 1568 | = ClassImpDecl->FindPropertyImplDecl(Property->getIdentifier())) |
| 1569 | if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic || |
| 1570 | PIDecl->getPropertyIvarDecl()) |
| 1571 | return false; |
| 1572 | |
| 1573 | return true; |
| 1574 | } |
| 1575 | |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1576 | ObjCIvarDecl *Sema::SynthesizeProvisionalIvar(LookupResult &Lookup, |
| 1577 | IdentifierInfo *II, |
| 1578 | SourceLocation NameLoc) { |
| 1579 | ObjCMethodDecl *CurMeth = getCurMethodDecl(); |
Fariborz Jahanian | 7b70eb4 | 2010-07-30 16:59:05 +0000 | [diff] [blame] | 1580 | bool LookForIvars; |
| 1581 | if (Lookup.empty()) |
| 1582 | LookForIvars = true; |
| 1583 | else if (CurMeth->isClassMethod()) |
| 1584 | LookForIvars = false; |
| 1585 | else |
| 1586 | LookForIvars = (Lookup.isSingleResult() && |
Fariborz Jahanian | 9312fcc | 2011-01-26 00:57:01 +0000 | [diff] [blame] | 1587 | Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod() && |
| 1588 | (Lookup.getAsSingle<VarDecl>() != 0)); |
Fariborz Jahanian | 7b70eb4 | 2010-07-30 16:59:05 +0000 | [diff] [blame] | 1589 | if (!LookForIvars) |
| 1590 | return 0; |
| 1591 | |
Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1592 | ObjCInterfaceDecl *IDecl = CurMeth->getClassInterface(); |
| 1593 | if (!IDecl) |
| 1594 | return 0; |
| 1595 | ObjCImplementationDecl *ClassImpDecl = IDecl->getImplementation(); |
Fariborz Jahanian | 2a36089 | 2010-07-19 16:14:33 +0000 | [diff] [blame] | 1596 | if (!ClassImpDecl) |
| 1597 | return 0; |
Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1598 | bool DynamicImplSeen = false; |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1599 | ObjCPropertyDecl *property = LookupPropertyDecl(IDecl, II); |
Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1600 | if (!property) |
| 1601 | return 0; |
Fariborz Jahanian | bfcbc85 | 2010-10-19 19:08:23 +0000 | [diff] [blame] | 1602 | if (ObjCPropertyImplDecl *PIDecl = ClassImpDecl->FindPropertyImplDecl(II)) { |
Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1603 | DynamicImplSeen = |
| 1604 | (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic); |
Fariborz Jahanian | bfcbc85 | 2010-10-19 19:08:23 +0000 | [diff] [blame] | 1605 | // property implementation has a designated ivar. No need to assume a new |
| 1606 | // one. |
| 1607 | if (!DynamicImplSeen && PIDecl->getPropertyIvarDecl()) |
| 1608 | return 0; |
| 1609 | } |
Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1610 | if (!DynamicImplSeen) { |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1611 | QualType PropType = Context.getCanonicalType(property->getType()); |
| 1612 | ObjCIvarDecl *Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl, |
Abramo Bagnara | dff1930 | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1613 | NameLoc, NameLoc, |
Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1614 | II, PropType, /*Dinfo=*/0, |
Fariborz Jahanian | 522eb7b | 2010-12-15 23:29:04 +0000 | [diff] [blame] | 1615 | ObjCIvarDecl::Private, |
Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1616 | (Expr *)0, true); |
| 1617 | ClassImpDecl->addDecl(Ivar); |
| 1618 | IDecl->makeDeclVisibleInContext(Ivar, false); |
| 1619 | property->setPropertyIvarDecl(Ivar); |
| 1620 | return Ivar; |
| 1621 | } |
| 1622 | return 0; |
| 1623 | } |
| 1624 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1625 | ExprResult Sema::ActOnIdExpression(Scope *S, |
John McCall | 24d1894 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 1626 | CXXScopeSpec &SS, |
| 1627 | UnqualifiedId &Id, |
| 1628 | bool HasTrailingLParen, |
| 1629 | bool isAddressOfOperand) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1630 | assert(!(isAddressOfOperand && HasTrailingLParen) && |
| 1631 | "cannot be direct & operand and have a trailing lparen"); |
| 1632 | |
| 1633 | if (SS.isInvalid()) |
Douglas Gregor | ed8f288 | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1634 | return ExprError(); |
Douglas Gregor | 90a1a65 | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1635 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1636 | TemplateArgumentListInfo TemplateArgsBuffer; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1637 | |
| 1638 | // Decompose the UnqualifiedId into the following data. |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1639 | DeclarationNameInfo NameInfo; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1640 | const TemplateArgumentListInfo *TemplateArgs; |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1641 | DecomposeUnqualifiedId(Id, TemplateArgsBuffer, NameInfo, TemplateArgs); |
Douglas Gregor | 90a1a65 | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1642 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1643 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | 4ea8043 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 1644 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1645 | SourceLocation NameLoc = NameInfo.getLoc(); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1646 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1647 | // C++ [temp.dep.expr]p3: |
| 1648 | // An id-expression is type-dependent if it contains: |
Douglas Gregor | ea0a0a9 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 1649 | // -- an identifier that was declared with a dependent type, |
| 1650 | // (note: handled after lookup) |
| 1651 | // -- a template-id that is dependent, |
| 1652 | // (note: handled in BuildTemplateIdExpr) |
| 1653 | // -- a conversion-function-id that specifies a dependent type, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1654 | // -- a nested-name-specifier that contains a class-name that |
| 1655 | // names a dependent type. |
| 1656 | // Determine whether this is a member of an unknown specialization; |
| 1657 | // we need to handle these differently. |
Eli Friedman | 964dbda | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1658 | bool DependentID = false; |
| 1659 | if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName && |
| 1660 | Name.getCXXNameType()->isDependentType()) { |
| 1661 | DependentID = true; |
| 1662 | } else if (SS.isSet()) { |
Chris Lattner | ebb5c6c | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1663 | if (DeclContext *DC = computeDeclContext(SS, false)) { |
Eli Friedman | 964dbda | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1664 | if (RequireCompleteDeclContext(SS, DC)) |
| 1665 | return ExprError(); |
Eli Friedman | 964dbda | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1666 | } else { |
| 1667 | DependentID = true; |
| 1668 | } |
| 1669 | } |
| 1670 | |
Chris Lattner | ebb5c6c | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1671 | if (DependentID) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1672 | return ActOnDependentIdExpression(SS, NameInfo, isAddressOfOperand, |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1673 | TemplateArgs); |
Chris Lattner | ebb5c6c | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1674 | |
Fariborz Jahanian | 8615134 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1675 | bool IvarLookupFollowUp = false; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1676 | // Perform the required lookup. |
Fariborz Jahanian | 7f4427f | 2011-07-12 17:16:56 +0000 | [diff] [blame] | 1677 | LookupResult R(*this, NameInfo, |
| 1678 | (Id.getKind() == UnqualifiedId::IK_ImplicitSelfParam) |
| 1679 | ? LookupObjCImplicitSelfParam : LookupOrdinaryName); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1680 | if (TemplateArgs) { |
Douglas Gregor | 3e51e17 | 2010-05-20 20:58:56 +0000 | [diff] [blame] | 1681 | // Lookup the template name again to correctly establish the context in |
| 1682 | // which it was found. This is really unfortunate as we already did the |
| 1683 | // lookup to determine that it was a template name in the first place. If |
| 1684 | // this becomes a performance hit, we can work harder to preserve those |
| 1685 | // results until we get here but it's likely not worth it. |
Douglas Gregor | 786123d | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1686 | bool MemberOfUnknownSpecialization; |
| 1687 | LookupTemplateName(R, S, SS, QualType(), /*EnteringContext=*/false, |
| 1688 | MemberOfUnknownSpecialization); |
Douglas Gregor | a522693 | 2011-02-04 13:35:07 +0000 | [diff] [blame] | 1689 | |
| 1690 | if (MemberOfUnknownSpecialization || |
| 1691 | (R.getResultKind() == LookupResult::NotFoundInCurrentInstantiation)) |
| 1692 | return ActOnDependentIdExpression(SS, NameInfo, isAddressOfOperand, |
| 1693 | TemplateArgs); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1694 | } else { |
Fariborz Jahanian | 8615134 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1695 | IvarLookupFollowUp = (!SS.isSet() && II && getCurMethodDecl()); |
Fariborz Jahanian | 6fada5b | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1696 | LookupParsedName(R, S, &SS, !IvarLookupFollowUp); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1697 | |
Douglas Gregor | a522693 | 2011-02-04 13:35:07 +0000 | [diff] [blame] | 1698 | // If the result might be in a dependent base class, this is a dependent |
| 1699 | // id-expression. |
| 1700 | if (R.getResultKind() == LookupResult::NotFoundInCurrentInstantiation) |
| 1701 | return ActOnDependentIdExpression(SS, NameInfo, isAddressOfOperand, |
| 1702 | TemplateArgs); |
| 1703 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1704 | // If this reference is in an Objective-C method, then we need to do |
| 1705 | // some special Objective-C lookup, too. |
Fariborz Jahanian | 6fada5b | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1706 | if (IvarLookupFollowUp) { |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1707 | ExprResult E(LookupInObjCMethod(R, S, II, true)); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1708 | if (E.isInvalid()) |
| 1709 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1710 | |
Chris Lattner | ebb5c6c | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1711 | if (Expr *Ex = E.takeAs<Expr>()) |
| 1712 | return Owned(Ex); |
| 1713 | |
| 1714 | // Synthesize ivars lazily. |
Fariborz Jahanian | c63f1c5 | 2011-01-03 18:08:02 +0000 | [diff] [blame] | 1715 | if (getLangOptions().ObjCDefaultSynthProperties && |
| 1716 | getLangOptions().ObjCNonFragileABI2) { |
Douglas Gregor | 0e7dde5 | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1717 | if (SynthesizeProvisionalIvar(R, II, NameLoc)) { |
Fariborz Jahanian | 8046af7 | 2010-11-17 19:41:23 +0000 | [diff] [blame] | 1718 | if (const ObjCPropertyDecl *Property = |
| 1719 | canSynthesizeProvisionalIvar(II)) { |
| 1720 | Diag(NameLoc, diag::warn_synthesized_ivar_access) << II; |
| 1721 | Diag(Property->getLocation(), diag::note_property_declare); |
| 1722 | } |
Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1723 | return ActOnIdExpression(S, SS, Id, HasTrailingLParen, |
| 1724 | isAddressOfOperand); |
Fariborz Jahanian | 8046af7 | 2010-11-17 19:41:23 +0000 | [diff] [blame] | 1725 | } |
Fariborz Jahanian | 1872298 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1726 | } |
Fariborz Jahanian | 18d90a9 | 2010-08-13 18:09:39 +0000 | [diff] [blame] | 1727 | // for further use, this must be set to false if in class method. |
| 1728 | IvarLookupFollowUp = getCurMethodDecl()->isInstanceMethod(); |
Steve Naroff | ebf4cb4 | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 1729 | } |
Chris Lattner | 59a2594 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 1730 | } |
Douglas Gregor | f15f5d3 | 2009-02-16 19:28:42 +0000 | [diff] [blame] | 1731 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1732 | if (R.isAmbiguous()) |
| 1733 | return ExprError(); |
| 1734 | |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1735 | // Determine whether this name might be a candidate for |
| 1736 | // argument-dependent lookup. |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1737 | bool ADL = UseArgumentDependentLookup(SS, R, HasTrailingLParen); |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1738 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1739 | if (R.empty() && !ADL) { |
Bill Wendling | 4073ed5 | 2007-02-13 01:51:42 +0000 | [diff] [blame] | 1740 | // Otherwise, this could be an implicitly declared function reference (legal |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1741 | // in C90, extension in C99, forbidden in C++). |
| 1742 | if (HasTrailingLParen && II && !getLangOptions().CPlusPlus) { |
| 1743 | NamedDecl *D = ImplicitlyDefineFunction(NameLoc, *II, S); |
| 1744 | if (D) R.addDecl(D); |
| 1745 | } |
| 1746 | |
| 1747 | // If this name wasn't predeclared and if this is not a function |
| 1748 | // call, diagnose the problem. |
| 1749 | if (R.empty()) { |
Douglas Gregor | 5fd04d4 | 2010-05-18 16:14:23 +0000 | [diff] [blame] | 1750 | if (DiagnoseEmptyLookup(S, SS, R, CTC_Unknown)) |
John McCall | d681c39 | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1751 | return ExprError(); |
| 1752 | |
| 1753 | assert(!R.empty() && |
| 1754 | "DiagnoseEmptyLookup returned false but added no results"); |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1755 | |
| 1756 | // If we found an Objective-C instance variable, let |
| 1757 | // LookupInObjCMethod build the appropriate expression to |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1758 | // reference the ivar. |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1759 | if (ObjCIvarDecl *Ivar = R.getAsSingle<ObjCIvarDecl>()) { |
| 1760 | R.clear(); |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1761 | ExprResult E(LookupInObjCMethod(R, S, Ivar->getIdentifier())); |
Douglas Gregor | 35b0bac | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1762 | assert(E.isInvalid() || E.get()); |
| 1763 | return move(E); |
| 1764 | } |
Steve Naroff | 92e30f8 | 2007-04-02 22:35:25 +0000 | [diff] [blame] | 1765 | } |
Chris Lattner | 17ed487 | 2006-11-20 04:58:19 +0000 | [diff] [blame] | 1766 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1767 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1768 | // This is guaranteed from this point on. |
| 1769 | assert(!R.empty() || ADL); |
| 1770 | |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1771 | // Check whether this might be a C++ implicit instance member access. |
John McCall | 24d1894 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 1772 | // C++ [class.mfct.non-static]p3: |
| 1773 | // When an id-expression that is not part of a class member access |
| 1774 | // syntax and not used to form a pointer to member is used in the |
| 1775 | // body of a non-static member function of class X, if name lookup |
| 1776 | // resolves the name in the id-expression to a non-static non-type |
| 1777 | // member of some class C, the id-expression is transformed into a |
| 1778 | // class member access expression using (*this) as the |
| 1779 | // postfix-expression to the left of the . operator. |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1780 | // |
| 1781 | // But we don't actually need to do this for '&' operands if R |
| 1782 | // resolved to a function or overloaded function set, because the |
| 1783 | // expression is ill-formed if it actually works out to be a |
| 1784 | // non-static member function: |
| 1785 | // |
| 1786 | // C++ [expr.ref]p4: |
| 1787 | // Otherwise, if E1.E2 refers to a non-static member function. . . |
| 1788 | // [t]he expression can be used only as the left-hand operand of a |
| 1789 | // member function call. |
| 1790 | // |
| 1791 | // There are other safeguards against such uses, but it's important |
| 1792 | // to get this right here so that we don't end up making a |
| 1793 | // spuriously dependent expression if we're inside a dependent |
| 1794 | // instance method. |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1795 | if (!R.empty() && (*R.begin())->isCXXClassMember()) { |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1796 | bool MightBeImplicitMember; |
| 1797 | if (!isAddressOfOperand) |
| 1798 | MightBeImplicitMember = true; |
| 1799 | else if (!SS.isEmpty()) |
| 1800 | MightBeImplicitMember = false; |
| 1801 | else if (R.isOverloadedResult()) |
| 1802 | MightBeImplicitMember = false; |
Douglas Gregor | 1262b06 | 2010-08-30 16:00:47 +0000 | [diff] [blame] | 1803 | else if (R.isUnresolvableResult()) |
| 1804 | MightBeImplicitMember = true; |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1805 | else |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 1806 | MightBeImplicitMember = isa<FieldDecl>(R.getFoundDecl()) || |
| 1807 | isa<IndirectFieldDecl>(R.getFoundDecl()); |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1808 | |
| 1809 | if (MightBeImplicitMember) |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1810 | return BuildPossibleImplicitMemberExpr(SS, R, TemplateArgs); |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1811 | } |
| 1812 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1813 | if (TemplateArgs) |
| 1814 | return BuildTemplateIdExpr(SS, R, ADL, *TemplateArgs); |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1815 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1816 | return BuildDeclarationNameExpr(SS, R, ADL); |
| 1817 | } |
| 1818 | |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1819 | /// BuildQualifiedDeclarationNameExpr - Build a C++ qualified |
| 1820 | /// declaration name, generally during template instantiation. |
| 1821 | /// There's a large number of things which don't need to be done along |
| 1822 | /// this path. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1823 | ExprResult |
Jeffrey Yasskin | c76498d | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1824 | Sema::BuildQualifiedDeclarationNameExpr(CXXScopeSpec &SS, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1825 | const DeclarationNameInfo &NameInfo) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1826 | DeclContext *DC; |
Douglas Gregor | a02bb34 | 2010-04-28 07:04:26 +0000 | [diff] [blame] | 1827 | if (!(DC = computeDeclContext(SS, false)) || DC->isDependentContext()) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1828 | return BuildDependentDeclRefExpr(SS, NameInfo, 0); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1829 | |
John McCall | 0b66eb3 | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 1830 | if (RequireCompleteDeclContext(SS, DC)) |
Douglas Gregor | a02bb34 | 2010-04-28 07:04:26 +0000 | [diff] [blame] | 1831 | return ExprError(); |
| 1832 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1833 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1834 | LookupQualifiedName(R, DC); |
| 1835 | |
| 1836 | if (R.isAmbiguous()) |
| 1837 | return ExprError(); |
| 1838 | |
| 1839 | if (R.empty()) { |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1840 | Diag(NameInfo.getLoc(), diag::err_no_member) |
| 1841 | << NameInfo.getName() << DC << SS.getRange(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1842 | return ExprError(); |
| 1843 | } |
| 1844 | |
| 1845 | return BuildDeclarationNameExpr(SS, R, /*ADL*/ false); |
| 1846 | } |
| 1847 | |
| 1848 | /// LookupInObjCMethod - The parser has read a name in, and Sema has |
| 1849 | /// detected that we're currently inside an ObjC method. Perform some |
| 1850 | /// additional lookup. |
| 1851 | /// |
| 1852 | /// Ideally, most of this would be done by lookup, but there's |
| 1853 | /// actually quite a lot of extra work involved. |
| 1854 | /// |
| 1855 | /// Returns a null sentinel to indicate trivial success. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1856 | ExprResult |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1857 | Sema::LookupInObjCMethod(LookupResult &Lookup, Scope *S, |
Chris Lattner | a36ec42 | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 1858 | IdentifierInfo *II, bool AllowBuiltinCreation) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1859 | SourceLocation Loc = Lookup.getNameLoc(); |
Chris Lattner | 8731366 | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1860 | ObjCMethodDecl *CurMethod = getCurMethodDecl(); |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1861 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1862 | // There are two cases to handle here. 1) scoped lookup could have failed, |
| 1863 | // in which case we should look for an ivar. 2) scoped lookup could have |
| 1864 | // found a decl, but that decl is outside the current instance method (i.e. |
| 1865 | // a global variable). In these two cases, we do a lookup for an ivar with |
| 1866 | // this name, if the lookup sucedes, we replace it our current decl. |
| 1867 | |
| 1868 | // If we're in a class method, we don't normally want to look for |
| 1869 | // ivars. But if we don't find anything else, and there's an |
| 1870 | // ivar, that's an error. |
Chris Lattner | 8731366 | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1871 | bool IsClassMethod = CurMethod->isClassMethod(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1872 | |
| 1873 | bool LookForIvars; |
| 1874 | if (Lookup.empty()) |
| 1875 | LookForIvars = true; |
| 1876 | else if (IsClassMethod) |
| 1877 | LookForIvars = false; |
| 1878 | else |
| 1879 | LookForIvars = (Lookup.isSingleResult() && |
| 1880 | Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod()); |
Fariborz Jahanian | 4587803 | 2010-02-09 19:31:38 +0000 | [diff] [blame] | 1881 | ObjCInterfaceDecl *IFace = 0; |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1882 | if (LookForIvars) { |
Chris Lattner | 8731366 | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1883 | IFace = CurMethod->getClassInterface(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1884 | ObjCInterfaceDecl *ClassDeclared; |
| 1885 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
| 1886 | // Diagnose using an ivar in a class method. |
| 1887 | if (IsClassMethod) |
| 1888 | return ExprError(Diag(Loc, diag::error_ivar_use_in_class_method) |
| 1889 | << IV->getDeclName()); |
| 1890 | |
| 1891 | // If we're referencing an invalid decl, just return this as a silent |
| 1892 | // error node. The error diagnostic was already emitted on the decl. |
| 1893 | if (IV->isInvalidDecl()) |
| 1894 | return ExprError(); |
| 1895 | |
| 1896 | // Check if referencing a field with __attribute__((deprecated)). |
| 1897 | if (DiagnoseUseOfDecl(IV, Loc)) |
| 1898 | return ExprError(); |
| 1899 | |
| 1900 | // Diagnose the use of an ivar outside of the declaring class. |
| 1901 | if (IV->getAccessControl() == ObjCIvarDecl::Private && |
| 1902 | ClassDeclared != IFace) |
| 1903 | Diag(Loc, diag::error_private_ivar_access) << IV->getDeclName(); |
| 1904 | |
| 1905 | // FIXME: This should use a new expr for a direct reference, don't |
| 1906 | // turn this into Self->ivar, just return a BareIVarExpr or something. |
| 1907 | IdentifierInfo &II = Context.Idents.get("self"); |
| 1908 | UnqualifiedId SelfName; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1909 | SelfName.setIdentifier(&II, SourceLocation()); |
Fariborz Jahanian | 7f4427f | 2011-07-12 17:16:56 +0000 | [diff] [blame] | 1910 | SelfName.setKind(UnqualifiedId::IK_ImplicitSelfParam); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1911 | CXXScopeSpec SelfScopeSpec; |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1912 | ExprResult SelfExpr = ActOnIdExpression(S, SelfScopeSpec, |
Douglas Gregor | a1ed39b | 2010-09-22 16:33:13 +0000 | [diff] [blame] | 1913 | SelfName, false, false); |
| 1914 | if (SelfExpr.isInvalid()) |
| 1915 | return ExprError(); |
| 1916 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1917 | SelfExpr = DefaultLvalueConversion(SelfExpr.take()); |
| 1918 | if (SelfExpr.isInvalid()) |
| 1919 | return ExprError(); |
John McCall | 2758424 | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 1920 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1921 | MarkDeclarationReferenced(Loc, IV); |
| 1922 | return Owned(new (Context) |
| 1923 | ObjCIvarRefExpr(IV, IV->getType(), Loc, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1924 | SelfExpr.take(), true, true)); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1925 | } |
Chris Lattner | 8731366 | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1926 | } else if (CurMethod->isInstanceMethod()) { |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1927 | // We should warn if a local variable hides an ivar. |
Chris Lattner | 8731366 | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1928 | ObjCInterfaceDecl *IFace = CurMethod->getClassInterface(); |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1929 | ObjCInterfaceDecl *ClassDeclared; |
| 1930 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
| 1931 | if (IV->getAccessControl() != ObjCIvarDecl::Private || |
| 1932 | IFace == ClassDeclared) |
| 1933 | Diag(Loc, diag::warn_ivar_use_hidden) << IV->getDeclName(); |
| 1934 | } |
| 1935 | } |
| 1936 | |
Fariborz Jahanian | 6fada5b | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1937 | if (Lookup.empty() && II && AllowBuiltinCreation) { |
| 1938 | // FIXME. Consolidate this with similar code in LookupName. |
| 1939 | if (unsigned BuiltinID = II->getBuiltinID()) { |
| 1940 | if (!(getLangOptions().CPlusPlus && |
| 1941 | Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))) { |
| 1942 | NamedDecl *D = LazilyCreateBuiltin((IdentifierInfo *)II, BuiltinID, |
| 1943 | S, Lookup.isForRedeclaration(), |
| 1944 | Lookup.getNameLoc()); |
| 1945 | if (D) Lookup.addDecl(D); |
| 1946 | } |
| 1947 | } |
| 1948 | } |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1949 | // Sentinel value saying that we didn't do anything special. |
| 1950 | return Owned((Expr*) 0); |
Douglas Gregor | 3256d04 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1951 | } |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1952 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1953 | /// \brief Cast a base object to a member's actual type. |
| 1954 | /// |
| 1955 | /// Logically this happens in three phases: |
| 1956 | /// |
| 1957 | /// * First we cast from the base type to the naming class. |
| 1958 | /// The naming class is the class into which we were looking |
| 1959 | /// when we found the member; it's the qualifier type if a |
| 1960 | /// qualifier was provided, and otherwise it's the base type. |
| 1961 | /// |
| 1962 | /// * Next we cast from the naming class to the declaring class. |
| 1963 | /// If the member we found was brought into a class's scope by |
| 1964 | /// a using declaration, this is that class; otherwise it's |
| 1965 | /// the class declaring the member. |
| 1966 | /// |
| 1967 | /// * Finally we cast from the declaring class to the "true" |
| 1968 | /// declaring class of the member. This conversion does not |
| 1969 | /// obey access control. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1970 | ExprResult |
| 1971 | Sema::PerformObjectMemberConversion(Expr *From, |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1972 | NestedNameSpecifier *Qualifier, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1973 | NamedDecl *FoundDecl, |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1974 | NamedDecl *Member) { |
| 1975 | CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Member->getDeclContext()); |
| 1976 | if (!RD) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1977 | return Owned(From); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1978 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1979 | QualType DestRecordType; |
| 1980 | QualType DestType; |
| 1981 | QualType FromRecordType; |
| 1982 | QualType FromType = From->getType(); |
| 1983 | bool PointerConversions = false; |
| 1984 | if (isa<FieldDecl>(Member)) { |
| 1985 | DestRecordType = Context.getCanonicalType(Context.getTypeDeclType(RD)); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1986 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1987 | if (FromType->getAs<PointerType>()) { |
| 1988 | DestType = Context.getPointerType(DestRecordType); |
| 1989 | FromRecordType = FromType->getPointeeType(); |
| 1990 | PointerConversions = true; |
| 1991 | } else { |
| 1992 | DestType = DestRecordType; |
| 1993 | FromRecordType = FromType; |
Fariborz Jahanian | bb67b82 | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 1994 | } |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1995 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member)) { |
| 1996 | if (Method->isStatic()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1997 | return Owned(From); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1998 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1999 | DestType = Method->getThisType(Context); |
| 2000 | DestRecordType = DestType->getPointeeType(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2001 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2002 | if (FromType->getAs<PointerType>()) { |
| 2003 | FromRecordType = FromType->getPointeeType(); |
| 2004 | PointerConversions = true; |
| 2005 | } else { |
| 2006 | FromRecordType = FromType; |
| 2007 | DestType = DestRecordType; |
| 2008 | } |
| 2009 | } else { |
| 2010 | // No conversion necessary. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2011 | return Owned(From); |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2012 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2013 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2014 | if (DestType->isDependentType() || FromType->isDependentType()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2015 | return Owned(From); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2016 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2017 | // If the unqualified types are the same, no conversion is necessary. |
| 2018 | if (Context.hasSameUnqualifiedType(FromRecordType, DestRecordType)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2019 | return Owned(From); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2020 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2021 | SourceRange FromRange = From->getSourceRange(); |
| 2022 | SourceLocation FromLoc = FromRange.getBegin(); |
| 2023 | |
John McCall | 2536c6d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2024 | ExprValueKind VK = CastCategory(From); |
Sebastian Redl | c57d34b | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2025 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2026 | // C++ [class.member.lookup]p8: |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2027 | // [...] Ambiguities can often be resolved by qualifying a name with its |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2028 | // class name. |
| 2029 | // |
| 2030 | // If the member was a qualified name and the qualified referred to a |
| 2031 | // specific base subobject type, we'll cast to that intermediate type |
| 2032 | // first and then to the object in which the member is declared. That allows |
| 2033 | // one to resolve ambiguities in, e.g., a diamond-shaped hierarchy such as: |
| 2034 | // |
| 2035 | // class Base { public: int x; }; |
| 2036 | // class Derived1 : public Base { }; |
| 2037 | // class Derived2 : public Base { }; |
| 2038 | // class VeryDerived : public Derived1, public Derived2 { void f(); }; |
| 2039 | // |
| 2040 | // void VeryDerived::f() { |
| 2041 | // x = 17; // error: ambiguous base subobjects |
| 2042 | // Derived1::x = 17; // okay, pick the Base subobject of Derived1 |
| 2043 | // } |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2044 | if (Qualifier) { |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2045 | QualType QType = QualType(Qualifier->getAsType(), 0); |
| 2046 | assert(!QType.isNull() && "lookup done with dependent qualifier?"); |
| 2047 | assert(QType->isRecordType() && "lookup done with non-record type"); |
| 2048 | |
| 2049 | QualType QRecordType = QualType(QType->getAs<RecordType>(), 0); |
| 2050 | |
| 2051 | // In C++98, the qualifier type doesn't actually have to be a base |
| 2052 | // type of the object type, in which case we just ignore it. |
| 2053 | // Otherwise build the appropriate casts. |
| 2054 | if (IsDerivedFrom(FromRecordType, QRecordType)) { |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2055 | CXXCastPath BasePath; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2056 | if (CheckDerivedToBaseConversion(FromRecordType, QRecordType, |
Anders Carlsson | b78feca | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2057 | FromLoc, FromRange, &BasePath)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2058 | return ExprError(); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2059 | |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2060 | if (PointerConversions) |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2061 | QType = Context.getPointerType(QType); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2062 | From = ImpCastExprToType(From, QType, CK_UncheckedDerivedToBase, |
| 2063 | VK, &BasePath).take(); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2064 | |
| 2065 | FromType = QType; |
| 2066 | FromRecordType = QRecordType; |
| 2067 | |
| 2068 | // If the qualifier type was the same as the destination type, |
| 2069 | // we're done. |
| 2070 | if (Context.hasSameUnqualifiedType(FromRecordType, DestRecordType)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2071 | return Owned(From); |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2072 | } |
| 2073 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2074 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2075 | bool IgnoreAccess = false; |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2076 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2077 | // If we actually found the member through a using declaration, cast |
| 2078 | // down to the using declaration's type. |
| 2079 | // |
| 2080 | // Pointer equality is fine here because only one declaration of a |
| 2081 | // class ever has member declarations. |
| 2082 | if (FoundDecl->getDeclContext() != Member->getDeclContext()) { |
| 2083 | assert(isa<UsingShadowDecl>(FoundDecl)); |
| 2084 | QualType URecordType = Context.getTypeDeclType( |
| 2085 | cast<CXXRecordDecl>(FoundDecl->getDeclContext())); |
| 2086 | |
| 2087 | // We only need to do this if the naming-class to declaring-class |
| 2088 | // conversion is non-trivial. |
| 2089 | if (!Context.hasSameUnqualifiedType(FromRecordType, URecordType)) { |
| 2090 | assert(IsDerivedFrom(FromRecordType, URecordType)); |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2091 | CXXCastPath BasePath; |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2092 | if (CheckDerivedToBaseConversion(FromRecordType, URecordType, |
Anders Carlsson | b78feca | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2093 | FromLoc, FromRange, &BasePath)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2094 | return ExprError(); |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 2095 | |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2096 | QualType UType = URecordType; |
| 2097 | if (PointerConversions) |
| 2098 | UType = Context.getPointerType(UType); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2099 | From = ImpCastExprToType(From, UType, CK_UncheckedDerivedToBase, |
| 2100 | VK, &BasePath).take(); |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2101 | FromType = UType; |
| 2102 | FromRecordType = URecordType; |
| 2103 | } |
| 2104 | |
| 2105 | // We don't do access control for the conversion from the |
| 2106 | // declaring class to the true declaring class. |
| 2107 | IgnoreAccess = true; |
Douglas Gregor | cc3f325 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2108 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2109 | |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2110 | CXXCastPath BasePath; |
Anders Carlsson | b78feca | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2111 | if (CheckDerivedToBaseConversion(FromRecordType, DestRecordType, |
| 2112 | FromLoc, FromRange, &BasePath, |
John McCall | 16df1e5 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2113 | IgnoreAccess)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2114 | return ExprError(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2115 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2116 | return ImpCastExprToType(From, DestType, CK_UncheckedDerivedToBase, |
| 2117 | VK, &BasePath); |
Fariborz Jahanian | bb67b82 | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 2118 | } |
Douglas Gregor | 3256d04 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 2119 | |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2120 | bool Sema::UseArgumentDependentLookup(const CXXScopeSpec &SS, |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2121 | const LookupResult &R, |
| 2122 | bool HasTrailingLParen) { |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2123 | // Only when used directly as the postfix-expression of a call. |
| 2124 | if (!HasTrailingLParen) |
| 2125 | return false; |
| 2126 | |
| 2127 | // Never if a scope specifier was provided. |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2128 | if (SS.isSet()) |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2129 | return false; |
| 2130 | |
| 2131 | // Only in C++ or ObjC++. |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2132 | if (!getLangOptions().CPlusPlus) |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2133 | return false; |
| 2134 | |
| 2135 | // Turn off ADL when we find certain kinds of declarations during |
| 2136 | // normal lookup: |
| 2137 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 2138 | NamedDecl *D = *I; |
| 2139 | |
| 2140 | // C++0x [basic.lookup.argdep]p3: |
| 2141 | // -- a declaration of a class member |
| 2142 | // Since using decls preserve this property, we check this on the |
| 2143 | // original decl. |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2144 | if (D->isCXXClassMember()) |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2145 | return false; |
| 2146 | |
| 2147 | // C++0x [basic.lookup.argdep]p3: |
| 2148 | // -- a block-scope function declaration that is not a |
| 2149 | // using-declaration |
| 2150 | // NOTE: we also trigger this for function templates (in fact, we |
| 2151 | // don't check the decl type at all, since all other decl types |
| 2152 | // turn off ADL anyway). |
| 2153 | if (isa<UsingShadowDecl>(D)) |
| 2154 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 2155 | else if (D->getDeclContext()->isFunctionOrMethod()) |
| 2156 | return false; |
| 2157 | |
| 2158 | // C++0x [basic.lookup.argdep]p3: |
| 2159 | // -- a declaration that is neither a function or a function |
| 2160 | // template |
| 2161 | // And also for builtin functions. |
| 2162 | if (isa<FunctionDecl>(D)) { |
| 2163 | FunctionDecl *FDecl = cast<FunctionDecl>(D); |
| 2164 | |
| 2165 | // But also builtin functions. |
| 2166 | if (FDecl->getBuiltinID() && FDecl->isImplicit()) |
| 2167 | return false; |
| 2168 | } else if (!isa<FunctionTemplateDecl>(D)) |
| 2169 | return false; |
| 2170 | } |
| 2171 | |
| 2172 | return true; |
| 2173 | } |
| 2174 | |
| 2175 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2176 | /// Diagnoses obvious problems with the use of the given declaration |
| 2177 | /// as an expression. This is only actually called for lookups that |
| 2178 | /// were not overloaded, and it doesn't promise that the declaration |
| 2179 | /// will in fact be used. |
| 2180 | static bool CheckDeclInExpr(Sema &S, SourceLocation Loc, NamedDecl *D) { |
Richard Smith | dda56e4 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2181 | if (isa<TypedefNameDecl>(D)) { |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2182 | S.Diag(Loc, diag::err_unexpected_typedef) << D->getDeclName(); |
| 2183 | return true; |
| 2184 | } |
| 2185 | |
| 2186 | if (isa<ObjCInterfaceDecl>(D)) { |
| 2187 | S.Diag(Loc, diag::err_unexpected_interface) << D->getDeclName(); |
| 2188 | return true; |
| 2189 | } |
| 2190 | |
| 2191 | if (isa<NamespaceDecl>(D)) { |
| 2192 | S.Diag(Loc, diag::err_unexpected_namespace) << D->getDeclName(); |
| 2193 | return true; |
| 2194 | } |
| 2195 | |
| 2196 | return false; |
| 2197 | } |
| 2198 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2199 | ExprResult |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2200 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2201 | LookupResult &R, |
| 2202 | bool NeedsADL) { |
John McCall | 3a60c87 | 2009-12-08 22:45:53 +0000 | [diff] [blame] | 2203 | // If this is a single, fully-resolved result and we don't need ADL, |
| 2204 | // just build an ordinary singleton decl ref. |
Douglas Gregor | 4b4844f | 2010-01-29 17:15:43 +0000 | [diff] [blame] | 2205 | if (!NeedsADL && R.isSingleResult() && !R.getAsSingle<FunctionTemplateDecl>()) |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2206 | return BuildDeclarationNameExpr(SS, R.getLookupNameInfo(), |
| 2207 | R.getFoundDecl()); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2208 | |
| 2209 | // We only need to check the declaration if there's exactly one |
| 2210 | // result, because in the overloaded case the results can only be |
| 2211 | // functions and function templates. |
John McCall | b53bbd4 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2212 | if (R.isSingleResult() && |
| 2213 | CheckDeclInExpr(*this, R.getNameLoc(), R.getFoundDecl())) |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2214 | return ExprError(); |
| 2215 | |
John McCall | 58cc69d | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2216 | // Otherwise, just build an unresolved lookup expression. Suppress |
| 2217 | // any lookup-related diagnostics; we'll hash these out later, when |
| 2218 | // we've picked a target. |
| 2219 | R.suppressDiagnostics(); |
| 2220 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2221 | UnresolvedLookupExpr *ULE |
Douglas Gregor | a6e053e | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2222 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 0da1d43 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2223 | SS.getWithLocInContext(Context), |
| 2224 | R.getLookupNameInfo(), |
Douglas Gregor | 30a4f4c | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 2225 | NeedsADL, R.isOverloadedResult(), |
| 2226 | R.begin(), R.end()); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2227 | |
| 2228 | return Owned(ULE); |
| 2229 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2230 | |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2231 | /// \brief Complete semantic analysis for a reference to the given declaration. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2232 | ExprResult |
John McCall | e66edc1 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2233 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2234 | const DeclarationNameInfo &NameInfo, |
| 2235 | NamedDecl *D) { |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2236 | assert(D && "Cannot refer to a NULL declaration"); |
John McCall | 283b901 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 2237 | assert(!isa<FunctionTemplateDecl>(D) && |
| 2238 | "Cannot refer unambiguously to a function template"); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2239 | |
Abramo Bagnara | d6d2f18 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2240 | SourceLocation Loc = NameInfo.getLoc(); |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2241 | if (CheckDeclInExpr(*this, Loc, D)) |
| 2242 | return ExprError(); |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 2243 | |
Douglas Gregor | e7488b9 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2244 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) { |
| 2245 | // Specifically diagnose references to class templates that are missing |
| 2246 | // a template argument list. |
| 2247 | Diag(Loc, diag::err_template_decl_ref) |
| 2248 | << Template << SS.getRange(); |
| 2249 | Diag(Template->getLocation(), diag::note_template_decl_here); |
| 2250 | return ExprError(); |
| 2251 | } |
| 2252 | |
| 2253 | // Make sure that we're referring to a value. |
| 2254 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 2255 | if (!VD) { |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2256 | Diag(Loc, diag::err_ref_non_value) |
Douglas Gregor | e7488b9 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2257 | << D << SS.getRange(); |
John McCall | b48971d | 2009-12-18 18:35:10 +0000 | [diff] [blame] | 2258 | Diag(D->getLocation(), diag::note_declared_at); |
Douglas Gregor | e7488b9 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2259 | return ExprError(); |
| 2260 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2261 | |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2262 | // Check whether this declaration can be used. Note that we suppress |
| 2263 | // this check when we're going to perform argument-dependent lookup |
| 2264 | // on this function name, because this might not be the function |
| 2265 | // that overload resolution actually selects. |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2266 | if (DiagnoseUseOfDecl(VD, Loc)) |
Douglas Gregor | 171c45a | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2267 | return ExprError(); |
| 2268 | |
Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2269 | // Only create DeclRefExpr's for valid Decl's. |
| 2270 | if (VD->isInvalidDecl()) |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2271 | return ExprError(); |
| 2272 | |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2273 | // Handle members of anonymous structs and unions. If we got here, |
| 2274 | // and the reference is to a class member indirect field, then this |
| 2275 | // must be the subject of a pointer-to-member expression. |
| 2276 | if (IndirectFieldDecl *indirectField = dyn_cast<IndirectFieldDecl>(VD)) |
| 2277 | if (!indirectField->isCXXClassMember()) |
| 2278 | return BuildAnonymousStructUnionMemberReference(SS, NameInfo.getLoc(), |
| 2279 | indirectField); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2280 | |
Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 2281 | // If the identifier reference is inside a block, and it refers to a value |
| 2282 | // that is outside the block, create a BlockDeclRefExpr instead of a |
| 2283 | // DeclRefExpr. This ensures the value is treated as a copy-in snapshot when |
| 2284 | // the block is formed. |
Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2285 | // |
Chris Lattner | 2a9d989 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 2286 | // We do not do this for things like enum constants, global variables, etc, |
| 2287 | // as they do not get snapshotted. |
| 2288 | // |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2289 | switch (shouldCaptureValueReference(*this, NameInfo.getLoc(), VD)) { |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2290 | case CR_Error: |
| 2291 | return ExprError(); |
Mike Stump | 7dafa0d | 2010-01-05 02:56:35 +0000 | [diff] [blame] | 2292 | |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2293 | case CR_Capture: |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2294 | assert(!SS.isSet() && "referenced local variable with scope specifier?"); |
| 2295 | return BuildBlockDeclRefExpr(*this, VD, NameInfo, /*byref*/ false); |
| 2296 | |
| 2297 | case CR_CaptureByRef: |
| 2298 | assert(!SS.isSet() && "referenced local variable with scope specifier?"); |
| 2299 | return BuildBlockDeclRefExpr(*this, VD, NameInfo, /*byref*/ true); |
John McCall | f4cd4f9 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2300 | |
| 2301 | case CR_NoCapture: { |
| 2302 | // If this reference is not in a block or if the referenced |
| 2303 | // variable is within the block, create a normal DeclRefExpr. |
| 2304 | |
| 2305 | QualType type = VD->getType(); |
Daniel Dunbar | 7c2dc36 | 2011-02-10 18:29:28 +0000 | [diff] [blame] | 2306 | ExprValueKind valueKind = VK_RValue; |
John McCall | f4cd4f9 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2307 | |
| 2308 | switch (D->getKind()) { |
| 2309 | // Ignore all the non-ValueDecl kinds. |
| 2310 | #define ABSTRACT_DECL(kind) |
| 2311 | #define VALUE(type, base) |
| 2312 | #define DECL(type, base) \ |
| 2313 | case Decl::type: |
| 2314 | #include "clang/AST/DeclNodes.inc" |
| 2315 | llvm_unreachable("invalid value decl kind"); |
| 2316 | return ExprError(); |
| 2317 | |
| 2318 | // These shouldn't make it here. |
| 2319 | case Decl::ObjCAtDefsField: |
| 2320 | case Decl::ObjCIvar: |
| 2321 | llvm_unreachable("forming non-member reference to ivar?"); |
| 2322 | return ExprError(); |
| 2323 | |
| 2324 | // Enum constants are always r-values and never references. |
| 2325 | // Unresolved using declarations are dependent. |
| 2326 | case Decl::EnumConstant: |
| 2327 | case Decl::UnresolvedUsingValue: |
| 2328 | valueKind = VK_RValue; |
| 2329 | break; |
| 2330 | |
| 2331 | // Fields and indirect fields that got here must be for |
| 2332 | // pointer-to-member expressions; we just call them l-values for |
| 2333 | // internal consistency, because this subexpression doesn't really |
| 2334 | // exist in the high-level semantics. |
| 2335 | case Decl::Field: |
| 2336 | case Decl::IndirectField: |
| 2337 | assert(getLangOptions().CPlusPlus && |
| 2338 | "building reference to field in C?"); |
| 2339 | |
| 2340 | // These can't have reference type in well-formed programs, but |
| 2341 | // for internal consistency we do this anyway. |
| 2342 | type = type.getNonReferenceType(); |
| 2343 | valueKind = VK_LValue; |
| 2344 | break; |
| 2345 | |
| 2346 | // Non-type template parameters are either l-values or r-values |
| 2347 | // depending on the type. |
| 2348 | case Decl::NonTypeTemplateParm: { |
| 2349 | if (const ReferenceType *reftype = type->getAs<ReferenceType>()) { |
| 2350 | type = reftype->getPointeeType(); |
| 2351 | valueKind = VK_LValue; // even if the parameter is an r-value reference |
| 2352 | break; |
| 2353 | } |
| 2354 | |
| 2355 | // For non-references, we need to strip qualifiers just in case |
| 2356 | // the template parameter was declared as 'const int' or whatever. |
| 2357 | valueKind = VK_RValue; |
| 2358 | type = type.getUnqualifiedType(); |
| 2359 | break; |
| 2360 | } |
| 2361 | |
| 2362 | case Decl::Var: |
| 2363 | // In C, "extern void blah;" is valid and is an r-value. |
| 2364 | if (!getLangOptions().CPlusPlus && |
| 2365 | !type.hasQualifiers() && |
| 2366 | type->isVoidType()) { |
| 2367 | valueKind = VK_RValue; |
| 2368 | break; |
| 2369 | } |
| 2370 | // fallthrough |
| 2371 | |
| 2372 | case Decl::ImplicitParam: |
| 2373 | case Decl::ParmVar: |
| 2374 | // These are always l-values. |
| 2375 | valueKind = VK_LValue; |
| 2376 | type = type.getNonReferenceType(); |
| 2377 | break; |
| 2378 | |
| 2379 | case Decl::Function: { |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2380 | const FunctionType *fty = type->castAs<FunctionType>(); |
| 2381 | |
| 2382 | // If we're referring to a function with an __unknown_anytype |
| 2383 | // result type, make the entire expression __unknown_anytype. |
| 2384 | if (fty->getResultType() == Context.UnknownAnyTy) { |
| 2385 | type = Context.UnknownAnyTy; |
| 2386 | valueKind = VK_RValue; |
| 2387 | break; |
| 2388 | } |
| 2389 | |
John McCall | f4cd4f9 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2390 | // Functions are l-values in C++. |
| 2391 | if (getLangOptions().CPlusPlus) { |
| 2392 | valueKind = VK_LValue; |
| 2393 | break; |
| 2394 | } |
| 2395 | |
| 2396 | // C99 DR 316 says that, if a function type comes from a |
| 2397 | // function definition (without a prototype), that type is only |
| 2398 | // used for checking compatibility. Therefore, when referencing |
| 2399 | // the function, we pretend that we don't have the full function |
| 2400 | // type. |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2401 | if (!cast<FunctionDecl>(VD)->hasPrototype() && |
| 2402 | isa<FunctionProtoType>(fty)) |
| 2403 | type = Context.getFunctionNoProtoType(fty->getResultType(), |
| 2404 | fty->getExtInfo()); |
John McCall | f4cd4f9 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2405 | |
| 2406 | // Functions are r-values in C. |
| 2407 | valueKind = VK_RValue; |
| 2408 | break; |
| 2409 | } |
| 2410 | |
| 2411 | case Decl::CXXMethod: |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2412 | // If we're referring to a method with an __unknown_anytype |
| 2413 | // result type, make the entire expression __unknown_anytype. |
| 2414 | // This should only be possible with a type written directly. |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 2415 | if (const FunctionProtoType *proto |
| 2416 | = dyn_cast<FunctionProtoType>(VD->getType())) |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2417 | if (proto->getResultType() == Context.UnknownAnyTy) { |
| 2418 | type = Context.UnknownAnyTy; |
| 2419 | valueKind = VK_RValue; |
| 2420 | break; |
| 2421 | } |
| 2422 | |
John McCall | f4cd4f9 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2423 | // C++ methods are l-values if static, r-values if non-static. |
| 2424 | if (cast<CXXMethodDecl>(VD)->isStatic()) { |
| 2425 | valueKind = VK_LValue; |
| 2426 | break; |
| 2427 | } |
| 2428 | // fallthrough |
| 2429 | |
| 2430 | case Decl::CXXConversion: |
| 2431 | case Decl::CXXDestructor: |
| 2432 | case Decl::CXXConstructor: |
| 2433 | valueKind = VK_RValue; |
| 2434 | break; |
| 2435 | } |
| 2436 | |
| 2437 | return BuildDeclRefExpr(VD, type, valueKind, NameInfo, &SS); |
| 2438 | } |
| 2439 | |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2440 | } |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2441 | |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2442 | llvm_unreachable("unknown capture result"); |
| 2443 | return ExprError(); |
Chris Lattner | 17ed487 | 2006-11-20 04:58:19 +0000 | [diff] [blame] | 2444 | } |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2445 | |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2446 | ExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc, tok::TokenKind Kind) { |
Chris Lattner | 6307f19 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 2447 | PredefinedExpr::IdentType IT; |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2448 | |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2449 | switch (Kind) { |
Chris Lattner | 317e6ba | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 2450 | default: assert(0 && "Unknown simple primary expr!"); |
Chris Lattner | 6307f19 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 2451 | case tok::kw___func__: IT = PredefinedExpr::Func; break; // [C99 6.4.2.2] |
| 2452 | case tok::kw___FUNCTION__: IT = PredefinedExpr::Function; break; |
| 2453 | case tok::kw___PRETTY_FUNCTION__: IT = PredefinedExpr::PrettyFunction; break; |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2454 | } |
Chris Lattner | 317e6ba | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 2455 | |
Chris Lattner | a81a027 | 2008-01-12 08:14:25 +0000 | [diff] [blame] | 2456 | // Pre-defined identifiers are of type char[x], where x is the length of the |
| 2457 | // string. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2458 | |
Anders Carlsson | 2fb0824 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2459 | Decl *currentDecl = getCurFunctionOrMethodDecl(); |
Fariborz Jahanian | 9462744 | 2010-07-23 21:53:24 +0000 | [diff] [blame] | 2460 | if (!currentDecl && getCurBlock()) |
| 2461 | currentDecl = getCurBlock()->TheDecl; |
Anders Carlsson | 2fb0824 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2462 | if (!currentDecl) { |
Chris Lattner | f45c5ec | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 2463 | Diag(Loc, diag::ext_predef_outside_function); |
Anders Carlsson | 2fb0824 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2464 | currentDecl = Context.getTranslationUnitDecl(); |
Chris Lattner | f45c5ec | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 2465 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2466 | |
Anders Carlsson | 0b209a8 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2467 | QualType ResTy; |
| 2468 | if (cast<DeclContext>(currentDecl)->isDependentContext()) { |
| 2469 | ResTy = Context.DependentTy; |
| 2470 | } else { |
Anders Carlsson | 5bd8d19 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 2471 | unsigned Length = PredefinedExpr::ComputeName(IT, currentDecl).length(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2472 | |
Anders Carlsson | 0b209a8 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2473 | llvm::APInt LengthI(32, Length + 1); |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2474 | ResTy = Context.CharTy.withConst(); |
Anders Carlsson | 0b209a8 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2475 | ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0); |
| 2476 | } |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2477 | return Owned(new (Context) PredefinedExpr(Loc, ResTy, IT)); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2478 | } |
| 2479 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2480 | ExprResult Sema::ActOnCharacterConstant(const Token &Tok) { |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 2481 | llvm::SmallString<16> CharBuffer; |
Douglas Gregor | dc970f0 | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2482 | bool Invalid = false; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2483 | StringRef ThisTok = PP.getSpelling(Tok, CharBuffer, &Invalid); |
Douglas Gregor | dc970f0 | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2484 | if (Invalid) |
| 2485 | return ExprError(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2486 | |
Benjamin Kramer | 0a1abd4 | 2010-02-27 13:44:12 +0000 | [diff] [blame] | 2487 | CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(), Tok.getLocation(), |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2488 | PP, Tok.getKind()); |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 2489 | if (Literal.hadError()) |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2490 | return ExprError(); |
Chris Lattner | ef24b38 | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 2491 | |
Chris Lattner | c3847ba | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 2492 | QualType Ty; |
| 2493 | if (!getLangOptions().CPlusPlus) |
| 2494 | Ty = Context.IntTy; // 'x' and L'x' -> int in C. |
| 2495 | else if (Literal.isWide()) |
| 2496 | Ty = Context.WCharTy; // L'x' -> wchar_t in C++. |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2497 | else if (Literal.isUTF16()) |
| 2498 | Ty = Context.Char16Ty; // u'x' -> char16_t in C++0x. |
| 2499 | else if (Literal.isUTF32()) |
| 2500 | Ty = Context.Char32Ty; // U'x' -> char32_t in C++0x. |
Eli Friedman | eb1df70 | 2010-02-03 18:21:45 +0000 | [diff] [blame] | 2501 | else if (Literal.isMultiChar()) |
| 2502 | Ty = Context.IntTy; // 'wxyz' -> int in C++. |
Chris Lattner | c3847ba | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 2503 | else |
| 2504 | Ty = Context.CharTy; // 'x' -> char in C++ |
Chris Lattner | ef24b38 | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 2505 | |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2506 | CharacterLiteral::CharacterKind Kind = CharacterLiteral::Ascii; |
| 2507 | if (Literal.isWide()) |
| 2508 | Kind = CharacterLiteral::Wide; |
| 2509 | else if (Literal.isUTF16()) |
| 2510 | Kind = CharacterLiteral::UTF16; |
| 2511 | else if (Literal.isUTF32()) |
| 2512 | Kind = CharacterLiteral::UTF32; |
| 2513 | |
| 2514 | return Owned(new (Context) CharacterLiteral(Literal.getValue(), Kind, Ty, |
| 2515 | Tok.getLocation())); |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 2516 | } |
| 2517 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2518 | ExprResult Sema::ActOnNumericConstant(const Token &Tok) { |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2519 | // 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] | 2520 | // cannot have a trigraph, escaped newline, radix prefix, or type suffix. |
| 2521 | if (Tok.getLength() == 1) { |
Chris Lattner | 9240b3e | 2009-01-26 22:36:52 +0000 | [diff] [blame] | 2522 | const char Val = PP.getSpellingOfSingleCharacterNumericConstant(Tok); |
Chris Lattner | c4c1819 | 2009-01-16 07:10:29 +0000 | [diff] [blame] | 2523 | unsigned IntSize = Context.Target.getIntWidth(); |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2524 | return Owned(IntegerLiteral::Create(Context, llvm::APInt(IntSize, Val-'0'), |
Steve Naroff | 5faaef7 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 2525 | Context.IntTy, Tok.getLocation())); |
Steve Naroff | f2fb89e | 2007-03-13 20:29:44 +0000 | [diff] [blame] | 2526 | } |
Ted Kremenek | e981418 | 2009-01-13 23:19:12 +0000 | [diff] [blame] | 2527 | |
Chris Lattner | 23b7eb6 | 2007-06-15 23:05:46 +0000 | [diff] [blame] | 2528 | llvm::SmallString<512> IntegerBuffer; |
Chris Lattner | a1cf5f9 | 2008-09-30 20:53:45 +0000 | [diff] [blame] | 2529 | // Add padding so that NumericLiteralParser can overread by one character. |
| 2530 | IntegerBuffer.resize(Tok.getLength()+1); |
Steve Naroff | 8160ea2 | 2007-03-06 01:09:46 +0000 | [diff] [blame] | 2531 | const char *ThisTokBegin = &IntegerBuffer[0]; |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2532 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2533 | // Get the spelling of the token, which eliminates trigraphs, etc. |
Douglas Gregor | dc970f0 | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2534 | bool Invalid = false; |
| 2535 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid); |
| 2536 | if (Invalid) |
| 2537 | return ExprError(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2538 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2539 | NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
Steve Naroff | 451d8f16 | 2007-03-12 23:22:38 +0000 | [diff] [blame] | 2540 | Tok.getLocation(), PP); |
Steve Naroff | f2fb89e | 2007-03-13 20:29:44 +0000 | [diff] [blame] | 2541 | if (Literal.hadError) |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2542 | return ExprError(); |
| 2543 | |
Chris Lattner | 1c20a17 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2544 | Expr *Res; |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2545 | |
Chris Lattner | 1c20a17 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2546 | if (Literal.isFloatingLiteral()) { |
Chris Lattner | ec0a6d9 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2547 | QualType Ty; |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2548 | if (Literal.isFloat) |
Chris Lattner | ec0a6d9 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2549 | Ty = Context.FloatTy; |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2550 | else if (!Literal.isLong) |
Chris Lattner | ec0a6d9 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2551 | Ty = Context.DoubleTy; |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2552 | else |
Chris Lattner | 7570e9c | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 2553 | Ty = Context.LongDoubleTy; |
Chris Lattner | 9a8d1d9 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2554 | |
| 2555 | const llvm::fltSemantics &Format = Context.getFloatTypeSemantics(Ty); |
| 2556 | |
John McCall | 53b93a0 | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2557 | using llvm::APFloat; |
| 2558 | APFloat Val(Format); |
| 2559 | |
| 2560 | APFloat::opStatus result = Literal.GetFloatValue(Val); |
John McCall | 122c831 | 2009-12-24 11:09:08 +0000 | [diff] [blame] | 2561 | |
| 2562 | // Overflow is always an error, but underflow is only an error if |
| 2563 | // we underflowed to zero (APFloat reports denormals as underflow). |
| 2564 | if ((result & APFloat::opOverflow) || |
| 2565 | ((result & APFloat::opUnderflow) && Val.isZero())) { |
John McCall | 53b93a0 | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2566 | unsigned diagnostic; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2567 | llvm::SmallString<20> buffer; |
John McCall | 53b93a0 | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2568 | if (result & APFloat::opOverflow) { |
John McCall | 62abc94 | 2010-02-26 23:35:57 +0000 | [diff] [blame] | 2569 | diagnostic = diag::warn_float_overflow; |
John McCall | 53b93a0 | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2570 | APFloat::getLargest(Format).toString(buffer); |
| 2571 | } else { |
John McCall | 62abc94 | 2010-02-26 23:35:57 +0000 | [diff] [blame] | 2572 | diagnostic = diag::warn_float_underflow; |
John McCall | 53b93a0 | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2573 | APFloat::getSmallest(Format).toString(buffer); |
| 2574 | } |
| 2575 | |
| 2576 | Diag(Tok.getLocation(), diagnostic) |
| 2577 | << Ty |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2578 | << StringRef(buffer.data(), buffer.size()); |
John McCall | 53b93a0 | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2579 | } |
| 2580 | |
| 2581 | bool isExact = (result == APFloat::opOK); |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2582 | Res = FloatingLiteral::Create(Context, Val, isExact, Ty, Tok.getLocation()); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2583 | |
Peter Collingbourne | c77f85b | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2584 | if (Ty == Context.DoubleTy) { |
| 2585 | if (getLangOptions().SinglePrecisionConstants) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2586 | Res = ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast).take(); |
Peter Collingbourne | c77f85b | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2587 | } else if (getLangOptions().OpenCL && !getOpenCLOptions().cl_khr_fp64) { |
| 2588 | Diag(Tok.getLocation(), diag::warn_double_const_requires_fp64); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2589 | Res = ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast).take(); |
Peter Collingbourne | c77f85b | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2590 | } |
| 2591 | } |
Chris Lattner | 1c20a17 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2592 | } else if (!Literal.isIntegerLiteral()) { |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2593 | return ExprError(); |
Chris Lattner | 1c20a17 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2594 | } else { |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2595 | QualType Ty; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2596 | |
Neil Booth | ac582c5 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 2597 | // long long is a C99 feature. |
| 2598 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && |
Neil Booth | 4a1ee05 | 2007-08-29 22:13:52 +0000 | [diff] [blame] | 2599 | Literal.isLongLong) |
Neil Booth | ac582c5 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 2600 | Diag(Tok.getLocation(), diag::ext_longlong); |
| 2601 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2602 | // Get the value in the widest-possible width. |
Chris Lattner | 37e0587 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2603 | llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(), 0); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2604 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2605 | if (Literal.GetIntegerValue(ResultVal)) { |
| 2606 | // If this value didn't fit into uintmax_t, warn and force to ull. |
| 2607 | Diag(Tok.getLocation(), diag::warn_integer_too_large); |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2608 | Ty = Context.UnsignedLongLongTy; |
| 2609 | assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() && |
Chris Lattner | 37e0587 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2610 | "long long is not intmax_t?"); |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 2611 | } else { |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2612 | // If this value fits into a ULL, try to figure out what else it fits into |
| 2613 | // according to the rules of C99 6.4.4.1p5. |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2614 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2615 | // Octal, Hexadecimal, and integers with a U suffix are allowed to |
| 2616 | // be an unsigned int. |
| 2617 | bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10; |
| 2618 | |
| 2619 | // Check from smallest to largest, picking the smallest type we can. |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2620 | unsigned Width = 0; |
Chris Lattner | 7b939cf | 2007-08-23 21:58:08 +0000 | [diff] [blame] | 2621 | if (!Literal.isLong && !Literal.isLongLong) { |
| 2622 | // Are int/unsigned possibilities? |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2623 | unsigned IntSize = Context.Target.getIntWidth(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2624 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2625 | // Does it fit in a unsigned int? |
| 2626 | if (ResultVal.isIntN(IntSize)) { |
| 2627 | // Does it fit in a signed int? |
| 2628 | if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0) |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2629 | Ty = Context.IntTy; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2630 | else if (AllowUnsigned) |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2631 | Ty = Context.UnsignedIntTy; |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2632 | Width = IntSize; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2633 | } |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2634 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2635 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2636 | // Are long/unsigned long possibilities? |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2637 | if (Ty.isNull() && !Literal.isLongLong) { |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2638 | unsigned LongSize = Context.Target.getLongWidth(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2639 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2640 | // Does it fit in a unsigned long? |
| 2641 | if (ResultVal.isIntN(LongSize)) { |
| 2642 | // Does it fit in a signed long? |
| 2643 | if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0) |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2644 | Ty = Context.LongTy; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2645 | else if (AllowUnsigned) |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2646 | Ty = Context.UnsignedLongTy; |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2647 | Width = LongSize; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2648 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2649 | } |
| 2650 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2651 | // Finally, check long long if needed. |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2652 | if (Ty.isNull()) { |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2653 | unsigned LongLongSize = Context.Target.getLongLongWidth(); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2654 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2655 | // Does it fit in a unsigned long long? |
| 2656 | if (ResultVal.isIntN(LongLongSize)) { |
| 2657 | // Does it fit in a signed long long? |
Francois Pichet | c3e73b3 | 2011-01-11 23:38:13 +0000 | [diff] [blame] | 2658 | // To be compatible with MSVC, hex integer literals ending with the |
| 2659 | // LL or i64 suffix are always signed in Microsoft mode. |
Francois Pichet | bf711d9 | 2011-01-11 12:23:00 +0000 | [diff] [blame] | 2660 | if (!Literal.isUnsigned && (ResultVal[LongLongSize-1] == 0 || |
| 2661 | (getLangOptions().Microsoft && Literal.isLongLong))) |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2662 | Ty = Context.LongLongTy; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2663 | else if (AllowUnsigned) |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2664 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2665 | Width = LongLongSize; |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2666 | } |
| 2667 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2668 | |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2669 | // If we still couldn't decide a type, we probably have something that |
| 2670 | // 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] | 2671 | if (Ty.isNull()) { |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2672 | Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed); |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2673 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2674 | Width = Context.Target.getLongLongWidth(); |
Chris Lattner | 67ca925 | 2007-05-21 01:08:44 +0000 | [diff] [blame] | 2675 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2676 | |
Chris Lattner | 55258cf | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2677 | if (ResultVal.getBitWidth() != Width) |
Jay Foad | 6d4db0c | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2678 | ResultVal = ResultVal.trunc(Width); |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 2679 | } |
Argyrios Kyrtzidis | 43b2057 | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2680 | Res = IntegerLiteral::Create(Context, ResultVal, Ty, Tok.getLocation()); |
Steve Naroff | 09ef474 | 2007-03-09 23:16:33 +0000 | [diff] [blame] | 2681 | } |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2682 | |
Chris Lattner | 1c20a17 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2683 | // If this is an imaginary literal, create the ImaginaryLiteral wrapper. |
| 2684 | if (Literal.isImaginary) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2685 | Res = new (Context) ImaginaryLiteral(Res, |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2686 | Context.getComplexType(Res->getType())); |
Sebastian Redl | ffbcf96 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2687 | |
| 2688 | return Owned(Res); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2689 | } |
| 2690 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2691 | ExprResult Sema::ActOnParenExpr(SourceLocation L, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2692 | SourceLocation R, Expr *E) { |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2693 | assert((E != 0) && "ActOnParenExpr() missing expr"); |
Steve Naroff | f6009ed | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2694 | return Owned(new (Context) ParenExpr(L, R, E)); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2695 | } |
| 2696 | |
Chandler Carruth | 62da79c | 2011-05-26 08:53:12 +0000 | [diff] [blame] | 2697 | static bool CheckVecStepTraitOperandType(Sema &S, QualType T, |
| 2698 | SourceLocation Loc, |
| 2699 | SourceRange ArgRange) { |
| 2700 | // [OpenCL 1.1 6.11.12] "The vec_step built-in function takes a built-in |
| 2701 | // scalar or vector data type argument..." |
| 2702 | // Every built-in scalar type (OpenCL 1.1 6.1.1) is either an arithmetic |
| 2703 | // type (C99 6.2.5p18) or void. |
| 2704 | if (!(T->isArithmeticType() || T->isVoidType() || T->isVectorType())) { |
| 2705 | S.Diag(Loc, diag::err_vecstep_non_scalar_vector_type) |
| 2706 | << T << ArgRange; |
| 2707 | return true; |
| 2708 | } |
| 2709 | |
| 2710 | assert((T->isVoidType() || !T->isIncompleteType()) && |
| 2711 | "Scalar types should always be complete"); |
| 2712 | return false; |
| 2713 | } |
| 2714 | |
Chandler Carruth | cea1aac | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 2715 | static bool CheckExtensionTraitOperandType(Sema &S, QualType T, |
| 2716 | SourceLocation Loc, |
| 2717 | SourceRange ArgRange, |
| 2718 | UnaryExprOrTypeTrait TraitKind) { |
| 2719 | // C99 6.5.3.4p1: |
| 2720 | if (T->isFunctionType()) { |
| 2721 | // alignof(function) is allowed as an extension. |
| 2722 | if (TraitKind == UETT_SizeOf) |
| 2723 | S.Diag(Loc, diag::ext_sizeof_function_type) << ArgRange; |
| 2724 | return false; |
| 2725 | } |
| 2726 | |
| 2727 | // Allow sizeof(void)/alignof(void) as an extension. |
| 2728 | if (T->isVoidType()) { |
| 2729 | S.Diag(Loc, diag::ext_sizeof_void_type) << TraitKind << ArgRange; |
| 2730 | return false; |
| 2731 | } |
| 2732 | |
| 2733 | return true; |
| 2734 | } |
| 2735 | |
| 2736 | static bool CheckObjCTraitOperandConstraints(Sema &S, QualType T, |
| 2737 | SourceLocation Loc, |
| 2738 | SourceRange ArgRange, |
| 2739 | UnaryExprOrTypeTrait TraitKind) { |
| 2740 | // Reject sizeof(interface) and sizeof(interface<proto>) in 64-bit mode. |
| 2741 | if (S.LangOpts.ObjCNonFragileABI && T->isObjCObjectType()) { |
| 2742 | S.Diag(Loc, diag::err_sizeof_nonfragile_interface) |
| 2743 | << T << (TraitKind == UETT_SizeOf) |
| 2744 | << ArgRange; |
| 2745 | return true; |
| 2746 | } |
| 2747 | |
| 2748 | return false; |
| 2749 | } |
| 2750 | |
Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2751 | /// \brief Check the constrains on expression operands to unary type expression |
| 2752 | /// and type traits. |
| 2753 | /// |
Chandler Carruth | 7c430c0 | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2754 | /// Completes any types necessary and validates the constraints on the operand |
| 2755 | /// expression. The logic mostly mirrors the type-based overload, but may modify |
| 2756 | /// the expression as it completes the type for that expression through template |
| 2757 | /// instantiation, etc. |
Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2758 | bool Sema::CheckUnaryExprOrTypeTraitOperand(Expr *Op, |
| 2759 | UnaryExprOrTypeTrait ExprKind) { |
Chandler Carruth | 7c430c0 | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2760 | QualType ExprTy = Op->getType(); |
| 2761 | |
| 2762 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 2763 | // the result is the size of the referenced type." |
| 2764 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 2765 | // result shall be the alignment of the referenced type." |
| 2766 | if (const ReferenceType *Ref = ExprTy->getAs<ReferenceType>()) |
| 2767 | ExprTy = Ref->getPointeeType(); |
| 2768 | |
| 2769 | if (ExprKind == UETT_VecStep) |
| 2770 | return CheckVecStepTraitOperandType(*this, ExprTy, Op->getExprLoc(), |
| 2771 | Op->getSourceRange()); |
| 2772 | |
| 2773 | // Whitelist some types as extensions |
| 2774 | if (!CheckExtensionTraitOperandType(*this, ExprTy, Op->getExprLoc(), |
| 2775 | Op->getSourceRange(), ExprKind)) |
| 2776 | return false; |
| 2777 | |
| 2778 | if (RequireCompleteExprType(Op, |
| 2779 | PDiag(diag::err_sizeof_alignof_incomplete_type) |
| 2780 | << ExprKind << Op->getSourceRange(), |
| 2781 | std::make_pair(SourceLocation(), PDiag(0)))) |
| 2782 | return true; |
| 2783 | |
| 2784 | // Completeing the expression's type may have changed it. |
| 2785 | ExprTy = Op->getType(); |
| 2786 | if (const ReferenceType *Ref = ExprTy->getAs<ReferenceType>()) |
| 2787 | ExprTy = Ref->getPointeeType(); |
| 2788 | |
| 2789 | if (CheckObjCTraitOperandConstraints(*this, ExprTy, Op->getExprLoc(), |
| 2790 | Op->getSourceRange(), ExprKind)) |
| 2791 | return true; |
| 2792 | |
Nico Weber | 0870deb | 2011-06-15 02:47:03 +0000 | [diff] [blame] | 2793 | if (ExprKind == UETT_SizeOf) { |
| 2794 | if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(Op->IgnoreParens())) { |
| 2795 | if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(DeclRef->getFoundDecl())) { |
| 2796 | QualType OType = PVD->getOriginalType(); |
| 2797 | QualType Type = PVD->getType(); |
| 2798 | if (Type->isPointerType() && OType->isArrayType()) { |
| 2799 | Diag(Op->getExprLoc(), diag::warn_sizeof_array_param) |
| 2800 | << Type << OType; |
| 2801 | Diag(PVD->getLocation(), diag::note_declared_at); |
| 2802 | } |
| 2803 | } |
| 2804 | } |
| 2805 | } |
| 2806 | |
Chandler Carruth | 7c430c0 | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2807 | return false; |
Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2808 | } |
| 2809 | |
| 2810 | /// \brief Check the constraints on operands to unary expression and type |
| 2811 | /// traits. |
| 2812 | /// |
| 2813 | /// This will complete any types necessary, and validate the various constraints |
| 2814 | /// on those operands. |
| 2815 | /// |
Steve Naroff | 71b59a9 | 2007-06-04 22:22:31 +0000 | [diff] [blame] | 2816 | /// The UsualUnaryConversions() function is *not* called by this routine. |
Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2817 | /// C99 6.3.2.1p[2-4] all state: |
| 2818 | /// Except when it is the operand of the sizeof operator ... |
| 2819 | /// |
| 2820 | /// C++ [expr.sizeof]p4 |
| 2821 | /// The lvalue-to-rvalue, array-to-pointer, and function-to-pointer |
| 2822 | /// standard conversions are not applied to the operand of sizeof. |
| 2823 | /// |
| 2824 | /// This policy is followed for all of the unary trait expressions. |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2825 | bool Sema::CheckUnaryExprOrTypeTraitOperand(QualType exprType, |
| 2826 | SourceLocation OpLoc, |
| 2827 | SourceRange ExprRange, |
| 2828 | UnaryExprOrTypeTrait ExprKind) { |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2829 | if (exprType->isDependentType()) |
| 2830 | return false; |
| 2831 | |
Sebastian Redl | 22e2e5c | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 2832 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 2833 | // the result is the size of the referenced type." |
| 2834 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 2835 | // result shall be the alignment of the referenced type." |
| 2836 | if (const ReferenceType *Ref = exprType->getAs<ReferenceType>()) |
| 2837 | exprType = Ref->getPointeeType(); |
| 2838 | |
Chandler Carruth | 62da79c | 2011-05-26 08:53:12 +0000 | [diff] [blame] | 2839 | if (ExprKind == UETT_VecStep) |
| 2840 | return CheckVecStepTraitOperandType(*this, exprType, OpLoc, ExprRange); |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2841 | |
Chandler Carruth | cea1aac | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 2842 | // Whitelist some types as extensions |
| 2843 | if (!CheckExtensionTraitOperandType(*this, exprType, OpLoc, ExprRange, |
| 2844 | ExprKind)) |
Chris Lattner | b1355b1 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 2845 | return false; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2846 | |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2847 | if (RequireCompleteType(OpLoc, exprType, |
Douglas Gregor | 906db8a | 2009-12-15 16:44:32 +0000 | [diff] [blame] | 2848 | PDiag(diag::err_sizeof_alignof_incomplete_type) |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2849 | << ExprKind << ExprRange)) |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2850 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2851 | |
Chandler Carruth | cea1aac | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 2852 | if (CheckObjCTraitOperandConstraints(*this, exprType, OpLoc, ExprRange, |
| 2853 | ExprKind)) |
Chris Lattner | cd2a8c5 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 2854 | return true; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2855 | |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2856 | return false; |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 2857 | } |
| 2858 | |
Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2859 | static bool CheckAlignOfExpr(Sema &S, Expr *E) { |
Chris Lattner | 8dff017 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 2860 | E = E->IgnoreParens(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2861 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2862 | // alignof decl is always ok. |
Chris Lattner | 8dff017 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 2863 | if (isa<DeclRefExpr>(E)) |
| 2864 | return false; |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2865 | |
| 2866 | // Cannot know anything else if the expression is dependent. |
| 2867 | if (E->isTypeDependent()) |
| 2868 | return false; |
| 2869 | |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2870 | if (E->getBitField()) { |
Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2871 | S.Diag(E->getExprLoc(), diag::err_sizeof_alignof_bitfield) |
| 2872 | << 1 << E->getSourceRange(); |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2873 | return true; |
Chris Lattner | 8dff017 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 2874 | } |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2875 | |
| 2876 | // Alignment of a field access is always okay, so long as it isn't a |
| 2877 | // bit-field. |
| 2878 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Mike Stump | 212005c | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 2879 | if (isa<FieldDecl>(ME->getMemberDecl())) |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2880 | return false; |
| 2881 | |
Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2882 | return S.CheckUnaryExprOrTypeTraitOperand(E, UETT_AlignOf); |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2883 | } |
| 2884 | |
Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2885 | bool Sema::CheckVecStepExpr(Expr *E) { |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2886 | E = E->IgnoreParens(); |
| 2887 | |
| 2888 | // Cannot know anything else if the expression is dependent. |
| 2889 | if (E->isTypeDependent()) |
| 2890 | return false; |
| 2891 | |
Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2892 | return CheckUnaryExprOrTypeTraitOperand(E, UETT_VecStep); |
Chris Lattner | 8dff017 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 2893 | } |
| 2894 | |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2895 | /// \brief Build a sizeof or alignof expression given a type operand. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2896 | ExprResult |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2897 | Sema::CreateUnaryExprOrTypeTraitExpr(TypeSourceInfo *TInfo, |
| 2898 | SourceLocation OpLoc, |
| 2899 | UnaryExprOrTypeTrait ExprKind, |
| 2900 | SourceRange R) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2901 | if (!TInfo) |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2902 | return ExprError(); |
| 2903 | |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2904 | QualType T = TInfo->getType(); |
John McCall | 4c98fd8 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 2905 | |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2906 | if (!T->isDependentType() && |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2907 | CheckUnaryExprOrTypeTraitOperand(T, OpLoc, R, ExprKind)) |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2908 | return ExprError(); |
| 2909 | |
| 2910 | // 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] | 2911 | return Owned(new (Context) UnaryExprOrTypeTraitExpr(ExprKind, TInfo, |
| 2912 | Context.getSizeType(), |
| 2913 | OpLoc, R.getEnd())); |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2914 | } |
| 2915 | |
| 2916 | /// \brief Build a sizeof or alignof expression given an expression |
| 2917 | /// operand. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2918 | ExprResult |
Chandler Carruth | a923fb2 | 2011-05-29 07:32:14 +0000 | [diff] [blame] | 2919 | Sema::CreateUnaryExprOrTypeTraitExpr(Expr *E, SourceLocation OpLoc, |
| 2920 | UnaryExprOrTypeTrait ExprKind) { |
Douglas Gregor | 835af98 | 2011-06-22 23:21:00 +0000 | [diff] [blame] | 2921 | ExprResult PE = CheckPlaceholderExpr(E); |
| 2922 | if (PE.isInvalid()) |
| 2923 | return ExprError(); |
| 2924 | |
| 2925 | E = PE.get(); |
| 2926 | |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2927 | // Verify that the operand is valid. |
| 2928 | bool isInvalid = false; |
| 2929 | if (E->isTypeDependent()) { |
| 2930 | // Delay type-checking for type-dependent expressions. |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2931 | } else if (ExprKind == UETT_AlignOf) { |
Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2932 | isInvalid = CheckAlignOfExpr(*this, E); |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2933 | } else if (ExprKind == UETT_VecStep) { |
Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2934 | isInvalid = CheckVecStepExpr(E); |
Douglas Gregor | 71235ec | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2935 | } else if (E->getBitField()) { // C99 6.5.3.4p1. |
Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2936 | Diag(E->getExprLoc(), diag::err_sizeof_alignof_bitfield) << 0; |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2937 | isInvalid = true; |
| 2938 | } else { |
Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2939 | isInvalid = CheckUnaryExprOrTypeTraitOperand(E, UETT_SizeOf); |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2940 | } |
| 2941 | |
| 2942 | if (isInvalid) |
| 2943 | return ExprError(); |
| 2944 | |
| 2945 | // 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] | 2946 | return Owned(new (Context) UnaryExprOrTypeTraitExpr( |
Chandler Carruth | a923fb2 | 2011-05-29 07:32:14 +0000 | [diff] [blame] | 2947 | ExprKind, E, Context.getSizeType(), OpLoc, |
Chandler Carruth | 14502c2 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2948 | E->getSourceRange().getEnd())); |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2949 | } |
| 2950 | |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2951 | /// ActOnUnaryExprOrTypeTraitExpr - Handle @c sizeof(type) and @c sizeof @c |
| 2952 | /// expr and the same for @c alignof and @c __alignof |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2953 | /// Note that the ArgRange is invalid if isType is false. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2954 | ExprResult |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2955 | Sema::ActOnUnaryExprOrTypeTraitExpr(SourceLocation OpLoc, |
| 2956 | UnaryExprOrTypeTrait ExprKind, bool isType, |
| 2957 | void *TyOrEx, const SourceRange &ArgRange) { |
Chris Lattner | 0d8b1a1 | 2006-11-20 04:34:45 +0000 | [diff] [blame] | 2958 | // If error parsing type, ignore. |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2959 | if (TyOrEx == 0) return ExprError(); |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 2960 | |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2961 | if (isType) { |
John McCall | bcd0350 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2962 | TypeSourceInfo *TInfo; |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2963 | (void) GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrEx), &TInfo); |
Peter Collingbourne | e190dee | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2964 | return CreateUnaryExprOrTypeTraitExpr(TInfo, OpLoc, ExprKind, ArgRange); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2965 | } |
Sebastian Redl | 6f28289 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2966 | |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2967 | Expr *ArgEx = (Expr *)TyOrEx; |
Chandler Carruth | a923fb2 | 2011-05-29 07:32:14 +0000 | [diff] [blame] | 2968 | ExprResult Result = CreateUnaryExprOrTypeTraitExpr(ArgEx, OpLoc, ExprKind); |
Douglas Gregor | 0950e41 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2969 | return move(Result); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 2970 | } |
| 2971 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2972 | static QualType CheckRealImagOperand(Sema &S, ExprResult &V, SourceLocation Loc, |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 2973 | bool isReal) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2974 | if (V.get()->isTypeDependent()) |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 2975 | return S.Context.DependentTy; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2976 | |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 2977 | // _Real and _Imag are only l-values for normal l-values. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2978 | if (V.get()->getObjectKind() != OK_Ordinary) { |
| 2979 | V = S.DefaultLvalueConversion(V.take()); |
| 2980 | if (V.isInvalid()) |
| 2981 | return QualType(); |
| 2982 | } |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 2983 | |
Chris Lattner | e267f5d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 2984 | // These operators return the element type of a complex type. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2985 | if (const ComplexType *CT = V.get()->getType()->getAs<ComplexType>()) |
Chris Lattner | 30b5dd0 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 2986 | return CT->getElementType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2987 | |
Chris Lattner | e267f5d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 2988 | // Otherwise they pass through real integer and floating point types here. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2989 | if (V.get()->getType()->isArithmeticType()) |
| 2990 | return V.get()->getType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2991 | |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 2992 | // Test for placeholders. |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 2993 | ExprResult PR = S.CheckPlaceholderExpr(V.get()); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 2994 | if (PR.isInvalid()) return QualType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2995 | if (PR.get() != V.get()) { |
| 2996 | V = move(PR); |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 2997 | return CheckRealImagOperand(S, V, Loc, isReal); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 2998 | } |
| 2999 | |
Chris Lattner | e267f5d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 3000 | // Reject anything else. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3001 | S.Diag(Loc, diag::err_realimag_invalid_type) << V.get()->getType() |
Chris Lattner | 709322b | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 3002 | << (isReal ? "__real" : "__imag"); |
Chris Lattner | e267f5d | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 3003 | return QualType(); |
Chris Lattner | 30b5dd0 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 3004 | } |
| 3005 | |
| 3006 | |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 3007 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3008 | ExprResult |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3009 | Sema::ActOnPostfixUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3010 | tok::TokenKind Kind, Expr *Input) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3011 | UnaryOperatorKind Opc; |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 3012 | switch (Kind) { |
| 3013 | default: assert(0 && "Unknown unary op!"); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3014 | case tok::plusplus: Opc = UO_PostInc; break; |
| 3015 | case tok::minusminus: Opc = UO_PostDec; break; |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 3016 | } |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3017 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3018 | return BuildUnaryOp(S, OpLoc, Opc, Input); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 3019 | } |
| 3020 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3021 | ExprResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3022 | Sema::ActOnArraySubscriptExpr(Scope *S, Expr *Base, SourceLocation LLoc, |
| 3023 | Expr *Idx, SourceLocation RLoc) { |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3024 | // Since this might be a postfix expression, get rid of ParenListExprs. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3025 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3026 | if (Result.isInvalid()) return ExprError(); |
| 3027 | Base = Result.take(); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3028 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3029 | Expr *LHSExp = Base, *RHSExp = Idx; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3030 | |
Douglas Gregor | 40412ac | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 3031 | if (getLangOptions().CPlusPlus && |
Douglas Gregor | 7a77a6b | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 3032 | (LHSExp->isTypeDependent() || RHSExp->isTypeDependent())) { |
Douglas Gregor | 7a77a6b | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 3033 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3034 | Context.DependentTy, |
| 3035 | VK_LValue, OK_Ordinary, |
| 3036 | RLoc)); |
Douglas Gregor | 7a77a6b | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 3037 | } |
| 3038 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3039 | if (getLangOptions().CPlusPlus && |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3040 | (LHSExp->getType()->isRecordType() || |
Eli Friedman | 254a1a2 | 2008-12-15 22:34:21 +0000 | [diff] [blame] | 3041 | LHSExp->getType()->isEnumeralType() || |
| 3042 | RHSExp->getType()->isRecordType() || |
| 3043 | RHSExp->getType()->isEnumeralType())) { |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3044 | return CreateOverloadedArraySubscriptExpr(LLoc, RLoc, Base, Idx); |
Douglas Gregor | 40412ac | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 3045 | } |
| 3046 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3047 | return CreateBuiltinArraySubscriptExpr(Base, LLoc, Idx, RLoc); |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 3048 | } |
| 3049 | |
| 3050 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3051 | ExprResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3052 | Sema::CreateBuiltinArraySubscriptExpr(Expr *Base, SourceLocation LLoc, |
| 3053 | Expr *Idx, SourceLocation RLoc) { |
| 3054 | Expr *LHSExp = Base; |
| 3055 | Expr *RHSExp = Idx; |
Sebastian Redl | adba46e | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 3056 | |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3057 | // Perform default conversions. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3058 | if (!LHSExp->getType()->getAs<VectorType>()) { |
| 3059 | ExprResult Result = DefaultFunctionArrayLvalueConversion(LHSExp); |
| 3060 | if (Result.isInvalid()) |
| 3061 | return ExprError(); |
| 3062 | LHSExp = Result.take(); |
| 3063 | } |
| 3064 | ExprResult Result = DefaultFunctionArrayLvalueConversion(RHSExp); |
| 3065 | if (Result.isInvalid()) |
| 3066 | return ExprError(); |
| 3067 | RHSExp = Result.take(); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3068 | |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3069 | QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType(); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3070 | ExprValueKind VK = VK_LValue; |
| 3071 | ExprObjectKind OK = OK_Ordinary; |
Steve Naroff | f1e5369 | 2007-03-23 22:27:02 +0000 | [diff] [blame] | 3072 | |
Steve Naroff | c1aadb1 | 2007-03-28 21:49:40 +0000 | [diff] [blame] | 3073 | // 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] | 3074 | // 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] | 3075 | // 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] | 3076 | // and index from the expression types. |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3077 | Expr *BaseExpr, *IndexExpr; |
| 3078 | QualType ResultType; |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3079 | if (LHSTy->isDependentType() || RHSTy->isDependentType()) { |
| 3080 | BaseExpr = LHSExp; |
| 3081 | IndexExpr = RHSExp; |
| 3082 | ResultType = Context.DependentTy; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3083 | } else if (const PointerType *PTy = LHSTy->getAs<PointerType>()) { |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3084 | BaseExpr = LHSExp; |
| 3085 | IndexExpr = RHSExp; |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3086 | ResultType = PTy->getPointeeType(); |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3087 | } else if (const PointerType *PTy = RHSTy->getAs<PointerType>()) { |
Chris Lattner | aee0cfd | 2007-07-16 00:23:25 +0000 | [diff] [blame] | 3088 | // Handle the uncommon case of "123[Ptr]". |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3089 | BaseExpr = RHSExp; |
| 3090 | IndexExpr = LHSExp; |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3091 | ResultType = PTy->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3092 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3093 | LHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3094 | BaseExpr = LHSExp; |
| 3095 | IndexExpr = RHSExp; |
| 3096 | ResultType = PTy->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3097 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3098 | RHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3099 | // Handle the uncommon case of "123[Ptr]". |
| 3100 | BaseExpr = RHSExp; |
| 3101 | IndexExpr = LHSExp; |
| 3102 | ResultType = PTy->getPointeeType(); |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3103 | } else if (const VectorType *VTy = LHSTy->getAs<VectorType>()) { |
Chris Lattner | 4197796 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 3104 | BaseExpr = LHSExp; // vectors: V[123] |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3105 | IndexExpr = RHSExp; |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3106 | VK = LHSExp->getValueKind(); |
| 3107 | if (VK != VK_RValue) |
| 3108 | OK = OK_VectorComponent; |
Nate Begeman | c1bf061 | 2009-01-18 00:45:31 +0000 | [diff] [blame] | 3109 | |
Chris Lattner | 36d572b | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3110 | // FIXME: need to deal with const... |
| 3111 | ResultType = VTy->getElementType(); |
Eli Friedman | ab2784f | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3112 | } else if (LHSTy->isArrayType()) { |
| 3113 | // If we see an array that wasn't promoted by |
Douglas Gregor | b92a156 | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 3114 | // DefaultFunctionArrayLvalueConversion, it must be an array that |
Eli Friedman | ab2784f | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3115 | // wasn't promoted because of the C90 rule that doesn't |
| 3116 | // allow promoting non-lvalue arrays. Warn, then |
| 3117 | // force the promotion here. |
| 3118 | Diag(LHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 3119 | LHSExp->getSourceRange(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3120 | LHSExp = ImpCastExprToType(LHSExp, Context.getArrayDecayedType(LHSTy), |
| 3121 | CK_ArrayToPointerDecay).take(); |
Eli Friedman | ab2784f | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3122 | LHSTy = LHSExp->getType(); |
| 3123 | |
| 3124 | BaseExpr = LHSExp; |
| 3125 | IndexExpr = RHSExp; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3126 | ResultType = LHSTy->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | ab2784f | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3127 | } else if (RHSTy->isArrayType()) { |
| 3128 | // Same as previous, except for 123[f().a] case |
| 3129 | Diag(RHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 3130 | RHSExp->getSourceRange(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3131 | RHSExp = ImpCastExprToType(RHSExp, Context.getArrayDecayedType(RHSTy), |
| 3132 | CK_ArrayToPointerDecay).take(); |
Eli Friedman | ab2784f | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3133 | RHSTy = RHSExp->getType(); |
| 3134 | |
| 3135 | BaseExpr = RHSExp; |
| 3136 | IndexExpr = LHSExp; |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3137 | ResultType = RHSTy->getAs<PointerType>()->getPointeeType(); |
Steve Naroff | b309644 | 2007-06-09 03:47:53 +0000 | [diff] [blame] | 3138 | } else { |
Chris Lattner | 003af24 | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 3139 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_value) |
| 3140 | << LHSExp->getSourceRange() << RHSExp->getSourceRange()); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3141 | } |
Steve Naroff | c1aadb1 | 2007-03-28 21:49:40 +0000 | [diff] [blame] | 3142 | // C99 6.5.2.1p1 |
Douglas Gregor | 5cc2c8b | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 3143 | if (!IndexExpr->getType()->isIntegerType() && !IndexExpr->isTypeDependent()) |
Chris Lattner | 003af24 | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 3144 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_not_integer) |
| 3145 | << IndexExpr->getSourceRange()); |
Steve Naroff | b29cdd5 | 2007-07-10 18:23:31 +0000 | [diff] [blame] | 3146 | |
Daniel Dunbar | 4782a6e | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 3147 | if ((IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_S) || |
Sam Weinig | b7608d7 | 2009-09-14 20:14:57 +0000 | [diff] [blame] | 3148 | IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U)) |
| 3149 | && !IndexExpr->isTypeDependent()) |
Sam Weinig | 914244e | 2009-09-14 01:58:58 +0000 | [diff] [blame] | 3150 | Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange(); |
| 3151 | |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3152 | // 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] | 3153 | // C++ [expr.sub]p1: The type "T" shall be a completely-defined object |
| 3154 | // type. Note that Functions are not objects, and that (in C99 parlance) |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3155 | // incomplete types are not object types. |
| 3156 | if (ResultType->isFunctionType()) { |
| 3157 | Diag(BaseExpr->getLocStart(), diag::err_subscript_function_type) |
| 3158 | << ResultType << BaseExpr->getSourceRange(); |
| 3159 | return ExprError(); |
| 3160 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3161 | |
Abramo Bagnara | 3aabb4b | 2010-09-13 06:50:07 +0000 | [diff] [blame] | 3162 | if (ResultType->isVoidType() && !getLangOptions().CPlusPlus) { |
| 3163 | // GNU extension: subscripting on pointer to void |
Chandler Carruth | 4cc3f29 | 2011-06-27 16:32:27 +0000 | [diff] [blame] | 3164 | Diag(LLoc, diag::ext_gnu_subscript_void_type) |
| 3165 | << BaseExpr->getSourceRange(); |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3166 | |
| 3167 | // C forbids expressions of unqualified void type from being l-values. |
| 3168 | // See IsCForbiddenLValueType. |
| 3169 | if (!ResultType.hasQualifiers()) VK = VK_RValue; |
Abramo Bagnara | 3aabb4b | 2010-09-13 06:50:07 +0000 | [diff] [blame] | 3170 | } else if (!ResultType->isDependentType() && |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3171 | RequireCompleteType(LLoc, ResultType, |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3172 | PDiag(diag::err_subscript_incomplete_type) |
| 3173 | << BaseExpr->getSourceRange())) |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3174 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3175 | |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3176 | // Diagnose bad cases where we step over interface counts. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3177 | if (ResultType->isObjCObjectType() && LangOpts.ObjCNonFragileABI) { |
Chris Lattner | 62975a7 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3178 | Diag(LLoc, diag::err_subscript_nonfragile_interface) |
| 3179 | << ResultType << BaseExpr->getSourceRange(); |
| 3180 | return ExprError(); |
| 3181 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3182 | |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3183 | assert(VK == VK_RValue || LangOpts.CPlusPlus || |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 3184 | !ResultType.isCForbiddenLValueType()); |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3185 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3186 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3187 | ResultType, VK, OK, RLoc)); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 3188 | } |
| 3189 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3190 | ExprResult Sema::BuildCXXDefaultArgExpr(SourceLocation CallLoc, |
Nico Weber | 44887f6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 3191 | FunctionDecl *FD, |
| 3192 | ParmVarDecl *Param) { |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3193 | if (Param->hasUnparsedDefaultArg()) { |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3194 | Diag(CallLoc, |
Nico Weber | ebd45a0 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 3195 | diag::err_use_of_default_argument_to_function_declared_later) << |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3196 | FD << cast<CXXRecordDecl>(FD->getDeclContext())->getDeclName(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3197 | Diag(UnparsedDefaultArgLocs[Param], |
Nico Weber | ebd45a0 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 3198 | diag::note_default_argument_declared_here); |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3199 | return ExprError(); |
| 3200 | } |
| 3201 | |
| 3202 | if (Param->hasUninstantiatedDefaultArg()) { |
| 3203 | Expr *UninstExpr = Param->getUninstantiatedDefaultArg(); |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3204 | |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3205 | // Instantiate the expression. |
| 3206 | MultiLevelTemplateArgumentList ArgList |
| 3207 | = getTemplateInstantiationArgs(FD, 0, /*RelativeToPrimary=*/true); |
Anders Carlsson | 657bad4 | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 3208 | |
Nico Weber | 44887f6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 3209 | std::pair<const TemplateArgument *, unsigned> Innermost |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3210 | = ArgList.getInnermost(); |
| 3211 | InstantiatingTemplate Inst(*this, CallLoc, Param, Innermost.first, |
| 3212 | Innermost.second); |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3213 | |
Nico Weber | 44887f6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 3214 | ExprResult Result; |
| 3215 | { |
| 3216 | // C++ [dcl.fct.default]p5: |
| 3217 | // The names in the [default argument] expression are bound, and |
| 3218 | // the semantic constraints are checked, at the point where the |
| 3219 | // default argument expression appears. |
Nico Weber | ebd45a0 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 3220 | ContextRAII SavedContext(*this, FD); |
Nico Weber | 44887f6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 3221 | Result = SubstExpr(UninstExpr, ArgList); |
| 3222 | } |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3223 | if (Result.isInvalid()) |
| 3224 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3225 | |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3226 | // Check the expression as an initializer for the parameter. |
| 3227 | InitializedEntity Entity |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 3228 | = InitializedEntity::InitializeParameter(Context, Param); |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3229 | InitializationKind Kind |
| 3230 | = InitializationKind::CreateCopy(Param->getLocation(), |
| 3231 | /*FIXME:EqualLoc*/UninstExpr->getSourceRange().getBegin()); |
| 3232 | Expr *ResultE = Result.takeAs<Expr>(); |
Douglas Gregor | 25ab25f | 2009-12-23 18:19:08 +0000 | [diff] [blame] | 3233 | |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3234 | InitializationSequence InitSeq(*this, Entity, Kind, &ResultE, 1); |
| 3235 | Result = InitSeq.Perform(*this, Entity, Kind, |
| 3236 | MultiExprArg(*this, &ResultE, 1)); |
| 3237 | if (Result.isInvalid()) |
| 3238 | return ExprError(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3239 | |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3240 | // Build the default argument expression. |
| 3241 | return Owned(CXXDefaultArgExpr::Create(Context, CallLoc, Param, |
| 3242 | Result.takeAs<Expr>())); |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3243 | } |
| 3244 | |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3245 | // If the default expression creates temporaries, we need to |
| 3246 | // push them to the current stack of expression temporaries so they'll |
| 3247 | // be properly destroyed. |
| 3248 | // FIXME: We should really be rebuilding the default argument with new |
| 3249 | // bound temporaries; see the comment in PR5810. |
Douglas Gregor | 6ed2fee | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 3250 | for (unsigned i = 0, e = Param->getNumDefaultArgTemporaries(); i != e; ++i) { |
| 3251 | CXXTemporary *Temporary = Param->getDefaultArgTemporary(i); |
| 3252 | MarkDeclarationReferenced(Param->getDefaultArg()->getLocStart(), |
| 3253 | const_cast<CXXDestructorDecl*>(Temporary->getDestructor())); |
| 3254 | ExprTemporaries.push_back(Temporary); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3255 | ExprNeedsCleanups = true; |
Douglas Gregor | 6ed2fee | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 3256 | } |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3257 | |
| 3258 | // We already type-checked the argument, so we know it works. |
Douglas Gregor | 32b3de5 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 3259 | // Just mark all of the declarations in this potentially-evaluated expression |
| 3260 | // as being "referenced". |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3261 | MarkDeclarationsReferencedInExpr(Param->getDefaultArg()); |
Douglas Gregor | 033f675 | 2009-12-23 23:03:06 +0000 | [diff] [blame] | 3262 | return Owned(CXXDefaultArgExpr::Create(Context, CallLoc, Param)); |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3263 | } |
| 3264 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3265 | /// ConvertArgumentsForCall - Converts the arguments specified in |
| 3266 | /// Args/NumArgs to the parameter types of the function FDecl with |
| 3267 | /// function prototype Proto. Call is the call expression itself, and |
| 3268 | /// Fn is the function expression. For a C++ member function, this |
| 3269 | /// routine does not attempt to convert the object argument. Returns |
| 3270 | /// true if the call is ill-formed. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3271 | bool |
| 3272 | Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3273 | FunctionDecl *FDecl, |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3274 | const FunctionProtoType *Proto, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3275 | Expr **Args, unsigned NumArgs, |
| 3276 | SourceLocation RParenLoc) { |
John McCall | bebede4 | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 3277 | // Bail out early if calling a builtin with custom typechecking. |
| 3278 | // We don't need to do this in the |
| 3279 | if (FDecl) |
| 3280 | if (unsigned ID = FDecl->getBuiltinID()) |
| 3281 | if (Context.BuiltinInfo.hasCustomTypechecking(ID)) |
| 3282 | return false; |
| 3283 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3284 | // 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] | 3285 | // assignment, to the types of the corresponding parameter, ... |
| 3286 | unsigned NumArgsInProto = Proto->getNumArgs(); |
Douglas Gregor | b6b9961 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 3287 | bool Invalid = false; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3288 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3289 | // If too few arguments are available (and we don't have default |
| 3290 | // arguments for the remaining parameters), don't make the call. |
| 3291 | if (NumArgs < NumArgsInProto) { |
Peter Collingbourne | 3bc84ca | 2011-07-29 00:24:42 +0000 | [diff] [blame] | 3292 | if (!FDecl || NumArgs < FDecl->getMinRequiredArguments()) { |
| 3293 | Diag(RParenLoc, diag::err_typecheck_call_too_few_args) |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 3294 | << Fn->getType()->isBlockPointerType() |
Eric Christopher | abf1e18 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 3295 | << NumArgsInProto << NumArgs << Fn->getSourceRange(); |
Peter Collingbourne | 3bc84ca | 2011-07-29 00:24:42 +0000 | [diff] [blame] | 3296 | |
| 3297 | // Emit the location of the prototype. |
| 3298 | if (FDecl && !FDecl->getBuiltinID()) |
| 3299 | Diag(FDecl->getLocStart(), diag::note_callee_decl) |
| 3300 | << FDecl; |
| 3301 | |
| 3302 | return true; |
| 3303 | } |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3304 | Call->setNumArgs(Context, NumArgsInProto); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3305 | } |
| 3306 | |
| 3307 | // If too many are passed and not variadic, error on the extras and drop |
| 3308 | // them. |
| 3309 | if (NumArgs > NumArgsInProto) { |
| 3310 | if (!Proto->isVariadic()) { |
| 3311 | Diag(Args[NumArgsInProto]->getLocStart(), |
| 3312 | diag::err_typecheck_call_too_many_args) |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 3313 | << Fn->getType()->isBlockPointerType() |
Eric Christopher | 2a5aaff | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 3314 | << NumArgsInProto << NumArgs << Fn->getSourceRange() |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3315 | << SourceRange(Args[NumArgsInProto]->getLocStart(), |
| 3316 | Args[NumArgs-1]->getLocEnd()); |
Ted Kremenek | 99a337e | 2011-04-04 17:22:27 +0000 | [diff] [blame] | 3317 | |
| 3318 | // Emit the location of the prototype. |
| 3319 | if (FDecl && !FDecl->getBuiltinID()) |
Peter Collingbourne | 3bc84ca | 2011-07-29 00:24:42 +0000 | [diff] [blame] | 3320 | Diag(FDecl->getLocStart(), diag::note_callee_decl) |
| 3321 | << FDecl; |
Ted Kremenek | 99a337e | 2011-04-04 17:22:27 +0000 | [diff] [blame] | 3322 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3323 | // This deletes the extra arguments. |
Ted Kremenek | 5a20195 | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3324 | Call->setNumArgs(Context, NumArgsInProto); |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3325 | return true; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3326 | } |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3327 | } |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3328 | SmallVector<Expr *, 8> AllArgs; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3329 | VariadicCallType CallType = |
Fariborz Jahanian | 6f2d25e | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 3330 | Proto->isVariadic() ? VariadicFunction : VariadicDoesNotApply; |
| 3331 | if (Fn->getType()->isBlockPointerType()) |
| 3332 | CallType = VariadicBlock; // Block |
| 3333 | else if (isa<MemberExpr>(Fn)) |
| 3334 | CallType = VariadicMethod; |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3335 | Invalid = GatherArgumentsForCall(Call->getSourceRange().getBegin(), FDecl, |
Fariborz Jahanian | 4fa66ce | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 3336 | Proto, 0, Args, NumArgs, AllArgs, CallType); |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3337 | if (Invalid) |
| 3338 | return true; |
| 3339 | unsigned TotalNumArgs = AllArgs.size(); |
| 3340 | for (unsigned i = 0; i < TotalNumArgs; ++i) |
| 3341 | Call->setArg(i, AllArgs[i]); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3342 | |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3343 | return false; |
| 3344 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3345 | |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3346 | bool Sema::GatherArgumentsForCall(SourceLocation CallLoc, |
| 3347 | FunctionDecl *FDecl, |
| 3348 | const FunctionProtoType *Proto, |
| 3349 | unsigned FirstProtoArg, |
| 3350 | Expr **Args, unsigned NumArgs, |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3351 | SmallVector<Expr *, 8> &AllArgs, |
Fariborz Jahanian | 6f2d25e | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 3352 | VariadicCallType CallType) { |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3353 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 3354 | unsigned NumArgsToCheck = NumArgs; |
| 3355 | bool Invalid = false; |
| 3356 | if (NumArgs != NumArgsInProto) |
| 3357 | // Use default arguments for missing arguments |
| 3358 | NumArgsToCheck = NumArgsInProto; |
| 3359 | unsigned ArgIx = 0; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3360 | // 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] | 3361 | for (unsigned i = FirstProtoArg; i != NumArgsToCheck; i++) { |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3362 | QualType ProtoArgType = Proto->getArgType(i); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3363 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3364 | Expr *Arg; |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3365 | if (ArgIx < NumArgs) { |
| 3366 | Arg = Args[ArgIx++]; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3367 | |
Eli Friedman | 3164fb1 | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3368 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 3369 | ProtoArgType, |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3370 | PDiag(diag::err_call_incomplete_argument) |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3371 | << Arg->getSourceRange())) |
Eli Friedman | 3164fb1 | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3372 | return true; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3373 | |
Douglas Gregor | bbeb5c3 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3374 | // Pass the argument |
| 3375 | ParmVarDecl *Param = 0; |
| 3376 | if (FDecl && i < FDecl->getNumParams()) |
| 3377 | Param = FDecl->getParamDecl(i); |
Douglas Gregor | 96596c9 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 3378 | |
Douglas Gregor | bbeb5c3 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3379 | InitializedEntity Entity = |
Fariborz Jahanian | 8fb87ae | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 3380 | Param? InitializedEntity::InitializeParameter(Context, Param) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3381 | : InitializedEntity::InitializeParameter(Context, ProtoArgType, |
| 3382 | Proto->isArgConsumed(i)); |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3383 | ExprResult ArgE = PerformCopyInitialization(Entity, |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3384 | SourceLocation(), |
| 3385 | Owned(Arg)); |
Douglas Gregor | bbeb5c3 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3386 | if (ArgE.isInvalid()) |
| 3387 | return true; |
| 3388 | |
| 3389 | Arg = ArgE.takeAs<Expr>(); |
Anders Carlsson | 84613c4 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 3390 | } else { |
Anders Carlsson | c80a127 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 3391 | ParmVarDecl *Param = FDecl->getParamDecl(i); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3392 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3393 | ExprResult ArgExpr = |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3394 | BuildCXXDefaultArgExpr(CallLoc, FDecl, Param); |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3395 | if (ArgExpr.isInvalid()) |
| 3396 | return true; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3397 | |
Anders Carlsson | 355933d | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3398 | Arg = ArgExpr.takeAs<Expr>(); |
Anders Carlsson | 84613c4 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 3399 | } |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 3400 | |
| 3401 | // Check for array bounds violations for each argument to the call. This |
| 3402 | // check only triggers warnings when the argument isn't a more complex Expr |
| 3403 | // with its own checking, such as a BinaryOperator. |
| 3404 | CheckArrayAccess(Arg); |
| 3405 | |
Fariborz Jahanian | 835026e | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3406 | AllArgs.push_back(Arg); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3407 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3408 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3409 | // If this is a variadic call, handle args passed through "...". |
Fariborz Jahanian | 6f2d25e | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 3410 | if (CallType != VariadicDoesNotApply) { |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3411 | |
| 3412 | // Assume that extern "C" functions with variadic arguments that |
| 3413 | // return __unknown_anytype aren't *really* variadic. |
| 3414 | if (Proto->getResultType() == Context.UnknownAnyTy && |
| 3415 | FDecl && FDecl->isExternC()) { |
| 3416 | for (unsigned i = ArgIx; i != NumArgs; ++i) { |
| 3417 | ExprResult arg; |
| 3418 | if (isa<ExplicitCastExpr>(Args[i]->IgnoreParens())) |
| 3419 | arg = DefaultFunctionArrayLvalueConversion(Args[i]); |
| 3420 | else |
| 3421 | arg = DefaultVariadicArgumentPromotion(Args[i], CallType, FDecl); |
| 3422 | Invalid |= arg.isInvalid(); |
| 3423 | AllArgs.push_back(arg.take()); |
| 3424 | } |
| 3425 | |
| 3426 | // Otherwise do argument promotion, (C99 6.5.2.2p7). |
| 3427 | } else { |
| 3428 | for (unsigned i = ArgIx; i != NumArgs; ++i) { |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3429 | ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], CallType, |
| 3430 | FDecl); |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3431 | Invalid |= Arg.isInvalid(); |
| 3432 | AllArgs.push_back(Arg.take()); |
| 3433 | } |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3434 | } |
| 3435 | } |
Douglas Gregor | b6b9961 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 3436 | return Invalid; |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3437 | } |
| 3438 | |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3439 | /// Given a function expression of unknown-any type, try to rebuild it |
| 3440 | /// to have a function type. |
| 3441 | static ExprResult rebuildUnknownAnyFunction(Sema &S, Expr *fn); |
| 3442 | |
Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3443 | /// ActOnCallExpr - Handle a call to Fn with the specified array of arguments. |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 3444 | /// This provides the location of the left/right parens and a list of comma |
| 3445 | /// locations. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3446 | ExprResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3447 | Sema::ActOnCallExpr(Scope *S, Expr *Fn, SourceLocation LParenLoc, |
Peter Collingbourne | 41f8546 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3448 | MultiExprArg args, SourceLocation RParenLoc, |
| 3449 | Expr *ExecConfig) { |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3450 | unsigned NumArgs = args.size(); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3451 | |
| 3452 | // Since this might be a postfix expression, get rid of ParenListExprs. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3453 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Fn); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3454 | if (Result.isInvalid()) return ExprError(); |
| 3455 | Fn = Result.take(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3456 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3457 | Expr **Args = args.release(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3458 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3459 | if (getLangOptions().CPlusPlus) { |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3460 | // If this is a pseudo-destructor expression, build the call immediately. |
| 3461 | if (isa<CXXPseudoDestructorExpr>(Fn)) { |
| 3462 | if (NumArgs > 0) { |
| 3463 | // Pseudo-destructor calls should not have any arguments. |
| 3464 | Diag(Fn->getLocStart(), diag::err_pseudo_dtor_call_with_args) |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3465 | << FixItHint::CreateRemoval( |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3466 | SourceRange(Args[0]->getLocStart(), |
| 3467 | Args[NumArgs-1]->getLocEnd())); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3468 | |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3469 | NumArgs = 0; |
| 3470 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3471 | |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3472 | return Owned(new (Context) CallExpr(Context, Fn, 0, 0, Context.VoidTy, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3473 | VK_RValue, RParenLoc)); |
Douglas Gregor | ad8a336 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3474 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3475 | |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3476 | // Determine whether this is a dependent call inside a C++ template, |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3477 | // in which case we won't do any semantic analysis now. |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3478 | // FIXME: Will need to cache the results of name lookup (including ADL) in |
| 3479 | // Fn. |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3480 | bool Dependent = false; |
| 3481 | if (Fn->isTypeDependent()) |
| 3482 | Dependent = true; |
| 3483 | else if (Expr::hasAnyTypeDependentArguments(Args, NumArgs)) |
| 3484 | Dependent = true; |
| 3485 | |
Peter Collingbourne | 41f8546 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3486 | if (Dependent) { |
| 3487 | if (ExecConfig) { |
| 3488 | return Owned(new (Context) CUDAKernelCallExpr( |
| 3489 | Context, Fn, cast<CallExpr>(ExecConfig), Args, NumArgs, |
| 3490 | Context.DependentTy, VK_RValue, RParenLoc)); |
| 3491 | } else { |
| 3492 | return Owned(new (Context) CallExpr(Context, Fn, Args, NumArgs, |
| 3493 | Context.DependentTy, VK_RValue, |
| 3494 | RParenLoc)); |
| 3495 | } |
| 3496 | } |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3497 | |
| 3498 | // Determine whether this is a call to an object (C++ [over.call.object]). |
| 3499 | if (Fn->getType()->isRecordType()) |
| 3500 | return Owned(BuildCallToObjectOfClassType(S, Fn, LParenLoc, Args, NumArgs, |
Douglas Gregor | ce5aa33 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 3501 | RParenLoc)); |
Douglas Gregor | b8a9a41 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3502 | |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3503 | if (Fn->getType() == Context.UnknownAnyTy) { |
| 3504 | ExprResult result = rebuildUnknownAnyFunction(*this, Fn); |
| 3505 | if (result.isInvalid()) return ExprError(); |
| 3506 | Fn = result.take(); |
| 3507 | } |
| 3508 | |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3509 | if (Fn->getType() == Context.BoundMemberTy) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3510 | return BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
Douglas Gregor | ce5aa33 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 3511 | RParenLoc); |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3512 | } |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3513 | } |
John McCall | 10eae18 | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3514 | |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3515 | // Check for overloaded calls. This can happen even in C due to extensions. |
| 3516 | if (Fn->getType() == Context.OverloadTy) { |
| 3517 | OverloadExpr::FindResult find = OverloadExpr::find(Fn); |
| 3518 | |
| 3519 | // We aren't supposed to apply this logic if there's an '&' involved. |
| 3520 | if (!find.IsAddressOfOperand) { |
| 3521 | OverloadExpr *ovl = find.Expression; |
| 3522 | if (isa<UnresolvedLookupExpr>(ovl)) { |
| 3523 | UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(ovl); |
| 3524 | return BuildOverloadedCallExpr(S, Fn, ULE, LParenLoc, Args, NumArgs, |
| 3525 | RParenLoc, ExecConfig); |
| 3526 | } else { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3527 | return BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
Douglas Gregor | ce5aa33 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 3528 | RParenLoc); |
Anders Carlsson | 61914b5 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 3529 | } |
| 3530 | } |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3531 | } |
| 3532 | |
Douglas Gregor | e254f90 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3533 | // If we're directly calling a function, get the appropriate declaration. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3534 | |
Eli Friedman | e14b199 | 2009-12-26 03:35:45 +0000 | [diff] [blame] | 3535 | Expr *NakedFn = Fn->IgnoreParens(); |
Douglas Gregor | 928479e | 2010-11-09 20:03:54 +0000 | [diff] [blame] | 3536 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3537 | NamedDecl *NDecl = 0; |
Douglas Gregor | 59f16ed | 2010-10-25 20:48:33 +0000 | [diff] [blame] | 3538 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(NakedFn)) |
| 3539 | if (UnOp->getOpcode() == UO_AddrOf) |
| 3540 | NakedFn = UnOp->getSubExpr()->IgnoreParens(); |
| 3541 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3542 | if (isa<DeclRefExpr>(NakedFn)) |
| 3543 | NDecl = cast<DeclRefExpr>(NakedFn)->getDecl(); |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3544 | else if (isa<MemberExpr>(NakedFn)) |
| 3545 | NDecl = cast<MemberExpr>(NakedFn)->getMemberDecl(); |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3546 | |
Peter Collingbourne | 41f8546 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3547 | return BuildResolvedCallExpr(Fn, NDecl, LParenLoc, Args, NumArgs, RParenLoc, |
| 3548 | ExecConfig); |
| 3549 | } |
| 3550 | |
| 3551 | ExprResult |
| 3552 | Sema::ActOnCUDAExecConfigExpr(Scope *S, SourceLocation LLLLoc, |
| 3553 | MultiExprArg execConfig, SourceLocation GGGLoc) { |
| 3554 | FunctionDecl *ConfigDecl = Context.getcudaConfigureCallDecl(); |
| 3555 | if (!ConfigDecl) |
| 3556 | return ExprError(Diag(LLLLoc, diag::err_undeclared_var_use) |
| 3557 | << "cudaConfigureCall"); |
| 3558 | QualType ConfigQTy = ConfigDecl->getType(); |
| 3559 | |
| 3560 | DeclRefExpr *ConfigDR = new (Context) DeclRefExpr( |
| 3561 | ConfigDecl, ConfigQTy, VK_LValue, LLLLoc); |
| 3562 | |
| 3563 | return ActOnCallExpr(S, ConfigDR, LLLLoc, execConfig, GGGLoc, 0); |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3564 | } |
| 3565 | |
Tanya Lattner | 55808c1 | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 3566 | /// ActOnAsTypeExpr - create a new asType (bitcast) from the arguments. |
| 3567 | /// |
| 3568 | /// __builtin_astype( value, dst type ) |
| 3569 | /// |
| 3570 | ExprResult Sema::ActOnAsTypeExpr(Expr *expr, ParsedType destty, |
| 3571 | SourceLocation BuiltinLoc, |
| 3572 | SourceLocation RParenLoc) { |
| 3573 | ExprValueKind VK = VK_RValue; |
| 3574 | ExprObjectKind OK = OK_Ordinary; |
| 3575 | QualType DstTy = GetTypeFromParser(destty); |
| 3576 | QualType SrcTy = expr->getType(); |
| 3577 | if (Context.getTypeSize(DstTy) != Context.getTypeSize(SrcTy)) |
| 3578 | return ExprError(Diag(BuiltinLoc, |
| 3579 | diag::err_invalid_astype_of_different_size) |
Peter Collingbourne | 23f1bee | 2011-06-08 15:15:17 +0000 | [diff] [blame] | 3580 | << DstTy |
| 3581 | << SrcTy |
Tanya Lattner | 55808c1 | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 3582 | << expr->getSourceRange()); |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3583 | return Owned(new (Context) AsTypeExpr(expr, DstTy, VK, OK, BuiltinLoc, |
| 3584 | RParenLoc)); |
Tanya Lattner | 55808c1 | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 3585 | } |
| 3586 | |
John McCall | 5750077 | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3587 | /// BuildResolvedCallExpr - Build a call to a resolved expression, |
| 3588 | /// i.e. an expression not of \p OverloadTy. The expression should |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3589 | /// unary-convert to an expression of function-pointer or |
| 3590 | /// block-pointer type. |
| 3591 | /// |
| 3592 | /// \param NDecl the declaration being called, if available |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3593 | ExprResult |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3594 | Sema::BuildResolvedCallExpr(Expr *Fn, NamedDecl *NDecl, |
| 3595 | SourceLocation LParenLoc, |
| 3596 | Expr **Args, unsigned NumArgs, |
Peter Collingbourne | 41f8546 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3597 | SourceLocation RParenLoc, |
| 3598 | Expr *Config) { |
John McCall | 2d74de9 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3599 | FunctionDecl *FDecl = dyn_cast_or_null<FunctionDecl>(NDecl); |
| 3600 | |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 3601 | // Promote the function operand. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3602 | ExprResult Result = UsualUnaryConversions(Fn); |
| 3603 | if (Result.isInvalid()) |
| 3604 | return ExprError(); |
| 3605 | Fn = Result.take(); |
Chris Lattner | aa9c7ae | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 3606 | |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3607 | // Make the call expr early, before semantic checks. This guarantees cleanup |
| 3608 | // of arguments and function on error. |
Peter Collingbourne | 41f8546 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3609 | CallExpr *TheCall; |
| 3610 | if (Config) { |
| 3611 | TheCall = new (Context) CUDAKernelCallExpr(Context, Fn, |
| 3612 | cast<CallExpr>(Config), |
| 3613 | Args, NumArgs, |
| 3614 | Context.BoolTy, |
| 3615 | VK_RValue, |
| 3616 | RParenLoc); |
| 3617 | } else { |
| 3618 | TheCall = new (Context) CallExpr(Context, Fn, |
| 3619 | Args, NumArgs, |
| 3620 | Context.BoolTy, |
| 3621 | VK_RValue, |
| 3622 | RParenLoc); |
| 3623 | } |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3624 | |
John McCall | bebede4 | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 3625 | unsigned BuiltinID = (FDecl ? FDecl->getBuiltinID() : 0); |
| 3626 | |
| 3627 | // Bail out early if calling a builtin with custom typechecking. |
| 3628 | if (BuiltinID && Context.BuiltinInfo.hasCustomTypechecking(BuiltinID)) |
| 3629 | return CheckBuiltinFunctionCall(BuiltinID, TheCall); |
| 3630 | |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 3631 | retry: |
Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 3632 | const FunctionType *FuncT; |
John McCall | bebede4 | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 3633 | if (const PointerType *PT = Fn->getType()->getAs<PointerType>()) { |
Steve Naroff | 8de9c3a | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 3634 | // C99 6.5.2.2p1 - "The expression that denotes the called function shall |
| 3635 | // have type pointer to function". |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3636 | FuncT = PT->getPointeeType()->getAs<FunctionType>(); |
John McCall | bebede4 | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 3637 | if (FuncT == 0) |
| 3638 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 3639 | << Fn->getType() << Fn->getSourceRange()); |
| 3640 | } else if (const BlockPointerType *BPT = |
| 3641 | Fn->getType()->getAs<BlockPointerType>()) { |
| 3642 | FuncT = BPT->getPointeeType()->castAs<FunctionType>(); |
| 3643 | } else { |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 3644 | // Handle calls to expressions of unknown-any type. |
| 3645 | if (Fn->getType() == Context.UnknownAnyTy) { |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3646 | ExprResult rewrite = rebuildUnknownAnyFunction(*this, Fn); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 3647 | if (rewrite.isInvalid()) return ExprError(); |
| 3648 | Fn = rewrite.take(); |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 3649 | TheCall->setCallee(Fn); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 3650 | goto retry; |
| 3651 | } |
| 3652 | |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3653 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 3654 | << Fn->getType() << Fn->getSourceRange()); |
John McCall | bebede4 | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 3655 | } |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3656 | |
Peter Collingbourne | 4b66c47 | 2011-02-23 01:53:29 +0000 | [diff] [blame] | 3657 | if (getLangOptions().CUDA) { |
| 3658 | if (Config) { |
| 3659 | // CUDA: Kernel calls must be to global functions |
| 3660 | if (FDecl && !FDecl->hasAttr<CUDAGlobalAttr>()) |
| 3661 | return ExprError(Diag(LParenLoc,diag::err_kern_call_not_global_function) |
| 3662 | << FDecl->getName() << Fn->getSourceRange()); |
| 3663 | |
| 3664 | // CUDA: Kernel function must have 'void' return type |
| 3665 | if (!FuncT->getResultType()->isVoidType()) |
| 3666 | return ExprError(Diag(LParenLoc, diag::err_kern_type_not_void_return) |
| 3667 | << Fn->getType() << Fn->getSourceRange()); |
| 3668 | } |
| 3669 | } |
| 3670 | |
Eli Friedman | 3164fb1 | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3671 | // Check for a valid return type |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3672 | if (CheckCallReturnType(FuncT->getResultType(), |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3673 | Fn->getSourceRange().getBegin(), TheCall, |
Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 3674 | FDecl)) |
Eli Friedman | 3164fb1 | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3675 | return ExprError(); |
| 3676 | |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3677 | // We know the result type of the call, set it. |
Douglas Gregor | 603d81b | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 3678 | TheCall->setType(FuncT->getCallResultType(Context)); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3679 | TheCall->setValueKind(Expr::getValueKindForType(FuncT->getResultType())); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3680 | |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3681 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FuncT)) { |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3682 | if (ConvertArgumentsForCall(TheCall, Fn, FDecl, Proto, Args, NumArgs, |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3683 | RParenLoc)) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3684 | return ExprError(); |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3685 | } else { |
Douglas Gregor | deaad8c | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3686 | assert(isa<FunctionNoProtoType>(FuncT) && "Unknown FunctionType!"); |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3687 | |
Douglas Gregor | d8e97de | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 3688 | if (FDecl) { |
| 3689 | // Check if we have too few/too many template arguments, based |
| 3690 | // on our knowledge of the function definition. |
| 3691 | const FunctionDecl *Def = 0; |
Argyrios Kyrtzidis | 36ea322 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 3692 | if (FDecl->hasBody(Def) && NumArgs != Def->param_size()) { |
Douglas Gregor | 8e09a72 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 3693 | const FunctionProtoType *Proto |
| 3694 | = Def->getType()->getAs<FunctionProtoType>(); |
| 3695 | if (!Proto || !(Proto->isVariadic() && NumArgs >= Def->param_size())) |
Eli Friedman | fcbf7d2 | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 3696 | Diag(RParenLoc, diag::warn_call_wrong_number_of_arguments) |
| 3697 | << (NumArgs > Def->param_size()) << FDecl << Fn->getSourceRange(); |
Eli Friedman | fcbf7d2 | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 3698 | } |
Douglas Gregor | 8e09a72 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 3699 | |
| 3700 | // If the function we're calling isn't a function prototype, but we have |
| 3701 | // a function prototype from a prior declaratiom, use that prototype. |
| 3702 | if (!FDecl->hasPrototype()) |
| 3703 | Proto = FDecl->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | d8e97de | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 3704 | } |
| 3705 | |
Steve Naroff | 0b66158 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 3706 | // Promote the arguments (C99 6.5.2.2p6). |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3707 | for (unsigned i = 0; i != NumArgs; i++) { |
| 3708 | Expr *Arg = Args[i]; |
Douglas Gregor | 8e09a72 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 3709 | |
| 3710 | if (Proto && i < Proto->getNumArgs()) { |
Douglas Gregor | 8e09a72 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 3711 | InitializedEntity Entity |
| 3712 | = InitializedEntity::InitializeParameter(Context, |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3713 | Proto->getArgType(i), |
| 3714 | Proto->isArgConsumed(i)); |
Douglas Gregor | 8e09a72 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 3715 | ExprResult ArgE = PerformCopyInitialization(Entity, |
| 3716 | SourceLocation(), |
| 3717 | Owned(Arg)); |
| 3718 | if (ArgE.isInvalid()) |
| 3719 | return true; |
| 3720 | |
| 3721 | Arg = ArgE.takeAs<Expr>(); |
| 3722 | |
| 3723 | } else { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3724 | ExprResult ArgE = DefaultArgumentPromotion(Arg); |
| 3725 | |
| 3726 | if (ArgE.isInvalid()) |
| 3727 | return true; |
| 3728 | |
| 3729 | Arg = ArgE.takeAs<Expr>(); |
Douglas Gregor | 8e09a72 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 3730 | } |
| 3731 | |
Douglas Gregor | 8302541 | 2010-10-26 05:45:40 +0000 | [diff] [blame] | 3732 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 3733 | Arg->getType(), |
| 3734 | PDiag(diag::err_call_incomplete_argument) |
| 3735 | << Arg->getSourceRange())) |
| 3736 | return ExprError(); |
| 3737 | |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3738 | TheCall->setArg(i, Arg); |
Steve Naroff | 0b66158 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 3739 | } |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 3740 | } |
Chris Lattner | 0846494 | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3741 | |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3742 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(FDecl)) |
| 3743 | if (!Method->isStatic()) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3744 | return ExprError(Diag(LParenLoc, diag::err_member_call_without_object) |
| 3745 | << Fn->getSourceRange()); |
Douglas Gregor | 97fd6e2 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3746 | |
Fariborz Jahanian | 0aa5c45 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 3747 | // Check for sentinels |
| 3748 | if (NDecl) |
| 3749 | DiagnoseSentinelCalls(NDecl, LParenLoc, Args, NumArgs); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3750 | |
Chris Lattner | b87b1b3 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 3751 | // Do special checking on direct calls to functions. |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 3752 | if (FDecl) { |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3753 | if (CheckFunctionCall(FDecl, TheCall)) |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 3754 | return ExprError(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3755 | |
John McCall | bebede4 | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 3756 | if (BuiltinID) |
Fariborz Jahanian | e8473c2 | 2010-11-30 17:35:24 +0000 | [diff] [blame] | 3757 | return CheckBuiltinFunctionCall(BuiltinID, TheCall); |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 3758 | } else if (NDecl) { |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3759 | if (CheckBlockCall(NDecl, TheCall)) |
Anders Carlsson | bc4c107 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 3760 | return ExprError(); |
| 3761 | } |
Chris Lattner | b87b1b3 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 3762 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3763 | return MaybeBindToTemporary(TheCall); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 3764 | } |
| 3765 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3766 | ExprResult |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3767 | Sema::ActOnCompoundLiteral(SourceLocation LParenLoc, ParsedType Ty, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3768 | SourceLocation RParenLoc, Expr *InitExpr) { |
Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3769 | assert((Ty != 0) && "ActOnCompoundLiteral(): missing type"); |
Steve Naroff | 57eb2c5 | 2007-07-19 21:32:11 +0000 | [diff] [blame] | 3770 | // FIXME: put back this assert when initializers are worked out. |
Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3771 | //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression"); |
John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 3772 | |
| 3773 | TypeSourceInfo *TInfo; |
| 3774 | QualType literalType = GetTypeFromParser(Ty, &TInfo); |
| 3775 | if (!TInfo) |
| 3776 | TInfo = Context.getTrivialTypeSourceInfo(literalType); |
| 3777 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3778 | return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, InitExpr); |
John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 3779 | } |
| 3780 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3781 | ExprResult |
John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 3782 | Sema::BuildCompoundLiteralExpr(SourceLocation LParenLoc, TypeSourceInfo *TInfo, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3783 | SourceLocation RParenLoc, Expr *literalExpr) { |
John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 3784 | QualType literalType = TInfo->getType(); |
Anders Carlsson | 2c1ec6d | 2007-12-05 07:24:19 +0000 | [diff] [blame] | 3785 | |
Eli Friedman | 37a186d | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 3786 | if (literalType->isArrayType()) { |
Argyrios Kyrtzidis | 8566356 | 2010-11-08 19:14:19 +0000 | [diff] [blame] | 3787 | if (RequireCompleteType(LParenLoc, Context.getBaseElementType(literalType), |
| 3788 | PDiag(diag::err_illegal_decl_array_incomplete_type) |
| 3789 | << SourceRange(LParenLoc, |
| 3790 | literalExpr->getSourceRange().getEnd()))) |
| 3791 | return ExprError(); |
Chris Lattner | 7adf076 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3792 | if (literalType->isVariableArrayType()) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3793 | return ExprError(Diag(LParenLoc, diag::err_variable_object_no_init) |
| 3794 | << SourceRange(LParenLoc, literalExpr->getSourceRange().getEnd())); |
Douglas Gregor | 1c37d9e | 2009-05-21 23:48:18 +0000 | [diff] [blame] | 3795 | } else if (!literalType->isDependentType() && |
| 3796 | RequireCompleteType(LParenLoc, literalType, |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3797 | PDiag(diag::err_typecheck_decl_incomplete_type) |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3798 | << SourceRange(LParenLoc, |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3799 | literalExpr->getSourceRange().getEnd()))) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3800 | return ExprError(); |
Eli Friedman | 37a186d | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 3801 | |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3802 | InitializedEntity Entity |
Douglas Gregor | 1b30393 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3803 | = InitializedEntity::InitializeTemporary(literalType); |
Douglas Gregor | 85dabae | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3804 | InitializationKind Kind |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3805 | = InitializationKind::CreateCStyleCast(LParenLoc, |
| 3806 | SourceRange(LParenLoc, RParenLoc)); |
Eli Friedman | a553d4a | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 3807 | InitializationSequence InitSeq(*this, Entity, Kind, &literalExpr, 1); |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3808 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, |
John McCall | 37ad551 | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 3809 | MultiExprArg(*this, &literalExpr, 1), |
Eli Friedman | a553d4a | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 3810 | &literalType); |
| 3811 | if (Result.isInvalid()) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3812 | return ExprError(); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3813 | literalExpr = Result.get(); |
Steve Naroff | d32419d | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 3814 | |
Chris Lattner | 7941395 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 3815 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
Steve Naroff | d32419d | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 3816 | if (isFileScope) { // 6.5.2.5p3 |
Steve Naroff | 98f7203 | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 3817 | if (CheckForConstantInitializer(literalExpr, literalType)) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3818 | return ExprError(); |
Steve Naroff | 98f7203 | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 3819 | } |
Eli Friedman | a553d4a | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 3820 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3821 | // In C, compound literals are l-values for some reason. |
| 3822 | ExprValueKind VK = getLangOptions().CPlusPlus ? VK_RValue : VK_LValue; |
| 3823 | |
Douglas Gregor | 9b71f0c | 2011-06-17 04:59:12 +0000 | [diff] [blame] | 3824 | return MaybeBindToTemporary( |
| 3825 | new (Context) CompoundLiteralExpr(LParenLoc, TInfo, literalType, |
| 3826 | VK, literalExpr, isFileScope)); |
Steve Naroff | fbd0983 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 3827 | } |
| 3828 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3829 | ExprResult |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3830 | Sema::ActOnInitList(SourceLocation LBraceLoc, MultiExprArg initlist, |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3831 | SourceLocation RBraceLoc) { |
| 3832 | unsigned NumInit = initlist.size(); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3833 | Expr **InitList = initlist.release(); |
Anders Carlsson | 4692db0 | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 3834 | |
Steve Naroff | 30d242c | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 3835 | // Semantic analysis for initializers is done by ActOnDeclarator() and |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3836 | // CheckInitializer() - it requires knowledge of the object being intialized. |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3837 | |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 3838 | InitListExpr *E = new (Context) InitListExpr(Context, LBraceLoc, InitList, |
| 3839 | NumInit, RBraceLoc); |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3840 | E->setType(Context.VoidTy); // FIXME: just a place holder for now. |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3841 | return Owned(E); |
Steve Naroff | fbd0983 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 3842 | } |
| 3843 | |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3844 | /// Prepares for a scalar cast, performing all the necessary stages |
| 3845 | /// except the final cast and returning the kind required. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3846 | static CastKind PrepareScalarCast(Sema &S, ExprResult &Src, QualType DestTy) { |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3847 | // Both Src and Dest are scalar types, i.e. arithmetic or pointer. |
| 3848 | // Also, callers should have filtered out the invalid cases with |
| 3849 | // pointers. Everything else should be possible. |
| 3850 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3851 | QualType SrcTy = Src.get()->getType(); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3852 | if (S.Context.hasSameUnqualifiedType(SrcTy, DestTy)) |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3853 | return CK_NoOp; |
Anders Carlsson | 094c459 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3854 | |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3855 | switch (SrcTy->getScalarTypeKind()) { |
| 3856 | case Type::STK_MemberPointer: |
| 3857 | llvm_unreachable("member pointer type in C"); |
Abramo Bagnara | ba85497 | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 3858 | |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3859 | case Type::STK_Pointer: |
| 3860 | switch (DestTy->getScalarTypeKind()) { |
| 3861 | case Type::STK_Pointer: |
| 3862 | return DestTy->isObjCObjectPointerType() ? |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3863 | CK_AnyPointerToObjCPointerCast : |
| 3864 | CK_BitCast; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3865 | case Type::STK_Bool: |
| 3866 | return CK_PointerToBoolean; |
| 3867 | case Type::STK_Integral: |
| 3868 | return CK_PointerToIntegral; |
| 3869 | case Type::STK_Floating: |
| 3870 | case Type::STK_FloatingComplex: |
| 3871 | case Type::STK_IntegralComplex: |
| 3872 | case Type::STK_MemberPointer: |
| 3873 | llvm_unreachable("illegal cast from pointer"); |
| 3874 | } |
| 3875 | break; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3876 | |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3877 | case Type::STK_Bool: // casting from bool is like casting from an integer |
| 3878 | case Type::STK_Integral: |
| 3879 | switch (DestTy->getScalarTypeKind()) { |
| 3880 | case Type::STK_Pointer: |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3881 | if (Src.get()->isNullPointerConstant(S.Context, |
| 3882 | Expr::NPC_ValueDependentIsNull)) |
John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 3883 | return CK_NullToPointer; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3884 | return CK_IntegralToPointer; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3885 | case Type::STK_Bool: |
| 3886 | return CK_IntegralToBoolean; |
| 3887 | case Type::STK_Integral: |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3888 | return CK_IntegralCast; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3889 | case Type::STK_Floating: |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3890 | return CK_IntegralToFloating; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3891 | case Type::STK_IntegralComplex: |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3892 | Src = S.ImpCastExprToType(Src.take(), |
| 3893 | DestTy->getAs<ComplexType>()->getElementType(), |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3894 | CK_IntegralCast); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3895 | return CK_IntegralRealToComplex; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3896 | case Type::STK_FloatingComplex: |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3897 | Src = S.ImpCastExprToType(Src.take(), |
| 3898 | DestTy->getAs<ComplexType>()->getElementType(), |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3899 | CK_IntegralToFloating); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3900 | return CK_FloatingRealToComplex; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3901 | case Type::STK_MemberPointer: |
| 3902 | llvm_unreachable("member pointer type in C"); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3903 | } |
| 3904 | break; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3905 | |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3906 | case Type::STK_Floating: |
| 3907 | switch (DestTy->getScalarTypeKind()) { |
| 3908 | case Type::STK_Floating: |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3909 | return CK_FloatingCast; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3910 | case Type::STK_Bool: |
| 3911 | return CK_FloatingToBoolean; |
| 3912 | case Type::STK_Integral: |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3913 | return CK_FloatingToIntegral; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3914 | case Type::STK_FloatingComplex: |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3915 | Src = S.ImpCastExprToType(Src.take(), |
| 3916 | DestTy->getAs<ComplexType>()->getElementType(), |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3917 | CK_FloatingCast); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3918 | return CK_FloatingRealToComplex; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3919 | case Type::STK_IntegralComplex: |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3920 | Src = S.ImpCastExprToType(Src.take(), |
| 3921 | DestTy->getAs<ComplexType>()->getElementType(), |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3922 | CK_FloatingToIntegral); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3923 | return CK_IntegralRealToComplex; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3924 | case Type::STK_Pointer: |
| 3925 | llvm_unreachable("valid float->pointer cast?"); |
| 3926 | case Type::STK_MemberPointer: |
| 3927 | llvm_unreachable("member pointer type in C"); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3928 | } |
| 3929 | break; |
| 3930 | |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3931 | case Type::STK_FloatingComplex: |
| 3932 | switch (DestTy->getScalarTypeKind()) { |
| 3933 | case Type::STK_FloatingComplex: |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3934 | return CK_FloatingComplexCast; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3935 | case Type::STK_IntegralComplex: |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3936 | return CK_FloatingComplexToIntegralComplex; |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3937 | case Type::STK_Floating: { |
Abramo Bagnara | ba85497 | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 3938 | QualType ET = SrcTy->getAs<ComplexType>()->getElementType(); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3939 | if (S.Context.hasSameType(ET, DestTy)) |
| 3940 | return CK_FloatingComplexToReal; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3941 | Src = S.ImpCastExprToType(Src.take(), ET, CK_FloatingComplexToReal); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3942 | return CK_FloatingCast; |
| 3943 | } |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3944 | case Type::STK_Bool: |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3945 | return CK_FloatingComplexToBoolean; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3946 | case Type::STK_Integral: |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3947 | Src = S.ImpCastExprToType(Src.take(), |
| 3948 | SrcTy->getAs<ComplexType>()->getElementType(), |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3949 | CK_FloatingComplexToReal); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3950 | return CK_FloatingToIntegral; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3951 | case Type::STK_Pointer: |
| 3952 | llvm_unreachable("valid complex float->pointer cast?"); |
| 3953 | case Type::STK_MemberPointer: |
| 3954 | llvm_unreachable("member pointer type in C"); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3955 | } |
| 3956 | break; |
| 3957 | |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3958 | case Type::STK_IntegralComplex: |
| 3959 | switch (DestTy->getScalarTypeKind()) { |
| 3960 | case Type::STK_FloatingComplex: |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3961 | return CK_IntegralComplexToFloatingComplex; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3962 | case Type::STK_IntegralComplex: |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3963 | return CK_IntegralComplexCast; |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3964 | case Type::STK_Integral: { |
Abramo Bagnara | ba85497 | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 3965 | QualType ET = SrcTy->getAs<ComplexType>()->getElementType(); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3966 | if (S.Context.hasSameType(ET, DestTy)) |
| 3967 | return CK_IntegralComplexToReal; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3968 | Src = S.ImpCastExprToType(Src.take(), ET, CK_IntegralComplexToReal); |
John McCall | fcef3cf | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3969 | return CK_IntegralCast; |
| 3970 | } |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3971 | case Type::STK_Bool: |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3972 | return CK_IntegralComplexToBoolean; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3973 | case Type::STK_Floating: |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3974 | Src = S.ImpCastExprToType(Src.take(), |
| 3975 | SrcTy->getAs<ComplexType>()->getElementType(), |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3976 | CK_IntegralComplexToReal); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3977 | return CK_IntegralToFloating; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3978 | case Type::STK_Pointer: |
| 3979 | llvm_unreachable("valid complex int->pointer cast?"); |
| 3980 | case Type::STK_MemberPointer: |
| 3981 | llvm_unreachable("member pointer type in C"); |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3982 | } |
| 3983 | break; |
Anders Carlsson | 094c459 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3984 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3985 | |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3986 | llvm_unreachable("Unhandled scalar cast"); |
| 3987 | return CK_BitCast; |
Anders Carlsson | 094c459 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3988 | } |
| 3989 | |
Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3990 | /// CheckCastTypes - Check type constraints for casting between types. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3991 | ExprResult Sema::CheckCastTypes(SourceLocation CastStartLoc, SourceRange TyR, |
| 3992 | QualType castType, Expr *castExpr, |
| 3993 | CastKind& Kind, ExprValueKind &VK, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3994 | CXXCastPath &BasePath, bool FunctionalStyle) { |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 3995 | if (castExpr->getType() == Context.UnknownAnyTy) |
| 3996 | return checkUnknownAnyCast(TyR, castType, castExpr, Kind, VK, BasePath); |
| 3997 | |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 3998 | if (getLangOptions().CPlusPlus) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3999 | return CXXCheckCStyleCast(SourceRange(CastStartLoc, |
Douglas Gregor | 15417cf | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 4000 | castExpr->getLocEnd()), |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4001 | castType, VK, castExpr, Kind, BasePath, |
Anders Carlsson | a70cff6 | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 4002 | FunctionalStyle); |
Sebastian Redl | 9f831db | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 4003 | |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 4004 | assert(!castExpr->getType()->isPlaceholderType()); |
| 4005 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4006 | // We only support r-value casts in C. |
| 4007 | VK = VK_RValue; |
| 4008 | |
Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 4009 | // C99 6.5.4p2: the cast type needs to be void or scalar and the expression |
| 4010 | // type needs to be scalar. |
| 4011 | if (castType->isVoidType()) { |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4012 | // We don't necessarily do lvalue-to-rvalue conversions on this. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4013 | ExprResult castExprRes = IgnoredValueConversions(castExpr); |
| 4014 | if (castExprRes.isInvalid()) |
| 4015 | return ExprError(); |
| 4016 | castExpr = castExprRes.take(); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4017 | |
Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 4018 | // Cast to void allows any expr type. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4019 | Kind = CK_ToVoid; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4020 | return Owned(castExpr); |
Anders Carlsson | ef918ac | 2009-10-16 02:35:04 +0000 | [diff] [blame] | 4021 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4022 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4023 | ExprResult castExprRes = DefaultFunctionArrayLvalueConversion(castExpr); |
| 4024 | if (castExprRes.isInvalid()) |
| 4025 | return ExprError(); |
| 4026 | castExpr = castExprRes.take(); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4027 | |
Eli Friedman | e98194d | 2010-07-17 20:43:49 +0000 | [diff] [blame] | 4028 | if (RequireCompleteType(TyR.getBegin(), castType, |
| 4029 | diag::err_typecheck_cast_to_incomplete)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4030 | return ExprError(); |
Eli Friedman | e98194d | 2010-07-17 20:43:49 +0000 | [diff] [blame] | 4031 | |
Anders Carlsson | ef918ac | 2009-10-16 02:35:04 +0000 | [diff] [blame] | 4032 | if (!castType->isScalarType() && !castType->isVectorType()) { |
Douglas Gregor | 1b8fe5b7 | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4033 | if (Context.hasSameUnqualifiedType(castType, castExpr->getType()) && |
Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 4034 | (castType->isStructureType() || castType->isUnionType())) { |
| 4035 | // GCC struct/union extension: allow cast to self. |
Eli Friedman | ba961a9 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 4036 | // FIXME: Check that the cast destination type is complete. |
Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 4037 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_nonscalar) |
| 4038 | << castType << castExpr->getSourceRange(); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4039 | Kind = CK_NoOp; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4040 | return Owned(castExpr); |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4041 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4042 | |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4043 | if (castType->isUnionType()) { |
Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 4044 | // GCC cast to union extension |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4045 | RecordDecl *RD = castType->getAs<RecordType>()->getDecl(); |
Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 4046 | RecordDecl::field_iterator Field, FieldEnd; |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4047 | for (Field = RD->field_begin(), FieldEnd = RD->field_end(); |
Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 4048 | Field != FieldEnd; ++Field) { |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4049 | if (Context.hasSameUnqualifiedType(Field->getType(), |
Abramo Bagnara | 5d3e724 | 2010-10-07 21:20:44 +0000 | [diff] [blame] | 4050 | castExpr->getType()) && |
| 4051 | !Field->isUnnamedBitfield()) { |
Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 4052 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_to_union) |
| 4053 | << castExpr->getSourceRange(); |
| 4054 | break; |
| 4055 | } |
| 4056 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4057 | if (Field == FieldEnd) { |
| 4058 | Diag(TyR.getBegin(), diag::err_typecheck_cast_to_union_no_type) |
Seo Sanghyeon | 39a3ebf | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 4059 | << castExpr->getType() << castExpr->getSourceRange(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4060 | return ExprError(); |
| 4061 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4062 | Kind = CK_ToUnion; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4063 | return Owned(castExpr); |
Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 4064 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4065 | |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4066 | // Reject any other conversions to non-scalar types. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4067 | Diag(TyR.getBegin(), diag::err_typecheck_cond_expect_scalar) |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4068 | << castType << castExpr->getSourceRange(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4069 | return ExprError(); |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4070 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4071 | |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4072 | // The type we're casting to is known to be a scalar or vector. |
| 4073 | |
| 4074 | // Require the operand to be a scalar or vector. |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4075 | if (!castExpr->getType()->isScalarType() && |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4076 | !castExpr->getType()->isVectorType()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4077 | Diag(castExpr->getLocStart(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4078 | diag::err_typecheck_expect_scalar_operand) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4079 | << castExpr->getType() << castExpr->getSourceRange(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4080 | return ExprError(); |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4081 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4082 | |
| 4083 | if (castType->isExtVectorType()) |
Anders Carlsson | 43d70f8 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 4084 | return CheckExtVectorCast(TyR, castType, castExpr, Kind); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4085 | |
Anton Yartsev | 28ccef7 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 4086 | if (castType->isVectorType()) { |
| 4087 | if (castType->getAs<VectorType>()->getVectorKind() == |
| 4088 | VectorType::AltiVecVector && |
| 4089 | (castExpr->getType()->isIntegerType() || |
| 4090 | castExpr->getType()->isFloatingType())) { |
| 4091 | Kind = CK_VectorSplat; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4092 | return Owned(castExpr); |
| 4093 | } else if (CheckVectorCast(TyR, castType, castExpr->getType(), Kind)) { |
| 4094 | return ExprError(); |
Anton Yartsev | 28ccef7 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 4095 | } else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4096 | return Owned(castExpr); |
Anton Yartsev | 28ccef7 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 4097 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4098 | if (castExpr->getType()->isVectorType()) { |
| 4099 | if (CheckVectorCast(TyR, castExpr->getType(), castType, Kind)) |
| 4100 | return ExprError(); |
| 4101 | else |
| 4102 | return Owned(castExpr); |
| 4103 | } |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4104 | |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4105 | // The source and target types are both scalars, i.e. |
| 4106 | // - arithmetic types (fundamental, enum, and complex) |
| 4107 | // - all kinds of pointers |
| 4108 | // Note that member pointers were filtered out with C++, above. |
| 4109 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4110 | if (isa<ObjCSelectorExpr>(castExpr)) { |
| 4111 | Diag(castExpr->getLocStart(), diag::err_cast_selector_expr); |
| 4112 | return ExprError(); |
| 4113 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4114 | |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4115 | // If either type is a pointer, the other type has to be either an |
| 4116 | // integer or a pointer. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4117 | QualType castExprType = castExpr->getType(); |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4118 | if (!castType->isArithmeticType()) { |
Douglas Gregor | 6972a62 | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 4119 | if (!castExprType->isIntegralType(Context) && |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4120 | castExprType->isArithmeticType()) { |
| 4121 | Diag(castExpr->getLocStart(), |
| 4122 | diag::err_cast_pointer_from_non_pointer_int) |
Eli Friedman | f4e3ad6 | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 4123 | << castExprType << castExpr->getSourceRange(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4124 | return ExprError(); |
| 4125 | } |
Eli Friedman | f4e3ad6 | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 4126 | } else if (!castExpr->getType()->isArithmeticType()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4127 | if (!castType->isIntegralType(Context) && castType->isArithmeticType()) { |
| 4128 | Diag(castExpr->getLocStart(), diag::err_cast_pointer_to_non_pointer_int) |
Eli Friedman | f4e3ad6 | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 4129 | << castType << castExpr->getSourceRange(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4130 | return ExprError(); |
| 4131 | } |
Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 4132 | } |
Anders Carlsson | 094c459 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 4133 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4134 | if (getLangOptions().ObjCAutoRefCount) { |
| 4135 | // Diagnose problems with Objective-C casts involving lifetime qualifiers. |
| 4136 | CheckObjCARCConversion(SourceRange(CastStartLoc, castExpr->getLocEnd()), |
| 4137 | castType, castExpr, CCK_CStyleCast); |
| 4138 | |
| 4139 | if (const PointerType *CastPtr = castType->getAs<PointerType>()) { |
| 4140 | if (const PointerType *ExprPtr = castExprType->getAs<PointerType>()) { |
| 4141 | Qualifiers CastQuals = CastPtr->getPointeeType().getQualifiers(); |
| 4142 | Qualifiers ExprQuals = ExprPtr->getPointeeType().getQualifiers(); |
| 4143 | if (CastPtr->getPointeeType()->isObjCLifetimeType() && |
| 4144 | ExprPtr->getPointeeType()->isObjCLifetimeType() && |
| 4145 | !CastQuals.compatiblyIncludesObjCLifetime(ExprQuals)) { |
| 4146 | Diag(castExpr->getLocStart(), |
Argyrios Kyrtzidis | cff00d9 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 4147 | diag::err_typecheck_incompatible_ownership) |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4148 | << castExprType << castType << AA_Casting |
| 4149 | << castExpr->getSourceRange(); |
| 4150 | |
| 4151 | return ExprError(); |
| 4152 | } |
| 4153 | } |
Fariborz Jahanian | 6f472e8 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 4154 | } |
Fariborz Jahanian | 7fcce68 | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 4155 | else if (!CheckObjCARCUnavailableWeakConversion(castType, castExprType)) { |
| 4156 | Diag(castExpr->getLocStart(), |
Fariborz Jahanian | f291340 | 2011-07-08 17:41:42 +0000 | [diff] [blame] | 4157 | diag::err_arc_convesion_of_weak_unavailable) << 1 |
Fariborz Jahanian | 6f472e8 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 4158 | << castExprType << castType |
| 4159 | << castExpr->getSourceRange(); |
Fariborz Jahanian | 7fcce68 | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 4160 | return ExprError(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4161 | } |
| 4162 | } |
| 4163 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4164 | castExprRes = Owned(castExpr); |
| 4165 | Kind = PrepareScalarCast(*this, castExprRes, castType); |
| 4166 | if (castExprRes.isInvalid()) |
| 4167 | return ExprError(); |
| 4168 | castExpr = castExprRes.take(); |
John McCall | 2b5c1b2 | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 4169 | |
John McCall | d764625 | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4170 | if (Kind == CK_BitCast) |
John McCall | 2b5c1b2 | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 4171 | CheckCastAlign(castExpr, castType, TyR); |
| 4172 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4173 | return Owned(castExpr); |
Argyrios Kyrtzidis | 2ade390 | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 4174 | } |
| 4175 | |
Anders Carlsson | 525b76b | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4176 | bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4177 | CastKind &Kind) { |
Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4178 | assert(VectorTy->isVectorType() && "Not a vector type!"); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4179 | |
Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4180 | if (Ty->isVectorType() || Ty->isIntegerType()) { |
Chris Lattner | 37e0587 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 4181 | if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty)) |
Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4182 | return Diag(R.getBegin(), |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4183 | Ty->isVectorType() ? |
Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4184 | diag::err_invalid_conversion_between_vectors : |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4185 | diag::err_invalid_conversion_between_vector_and_integer) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4186 | << VectorTy << Ty << R; |
Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4187 | } else |
| 4188 | return Diag(R.getBegin(), |
Chris Lattner | 3b05413 | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4189 | diag::err_invalid_conversion_between_vector_and_scalar) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4190 | << VectorTy << Ty << R; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4191 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4192 | Kind = CK_BitCast; |
Anders Carlsson | de71adf | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4193 | return false; |
| 4194 | } |
| 4195 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4196 | ExprResult Sema::CheckExtVectorCast(SourceRange R, QualType DestTy, |
| 4197 | Expr *CastExpr, CastKind &Kind) { |
Nate Begeman | c69b740 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4198 | assert(DestTy->isExtVectorType() && "Not an extended vector type!"); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4199 | |
Anders Carlsson | 43d70f8 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 4200 | QualType SrcTy = CastExpr->getType(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4201 | |
Nate Begeman | c8961a4 | 2009-06-27 22:05:55 +0000 | [diff] [blame] | 4202 | // If SrcTy is a VectorType, the total size must match to explicitly cast to |
| 4203 | // an ExtVectorType. |
Nate Begeman | c69b740 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4204 | if (SrcTy->isVectorType()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4205 | if (Context.getTypeSize(DestTy) != Context.getTypeSize(SrcTy)) { |
| 4206 | Diag(R.getBegin(),diag::err_invalid_conversion_between_ext_vectors) |
Nate Begeman | c69b740 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4207 | << DestTy << SrcTy << R; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4208 | return ExprError(); |
| 4209 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4210 | Kind = CK_BitCast; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4211 | return Owned(CastExpr); |
Nate Begeman | c69b740 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4212 | } |
| 4213 | |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4214 | // All non-pointer scalars can be cast to ExtVector type. The appropriate |
Nate Begeman | c69b740 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4215 | // conversion will take place first from scalar to elt type, and then |
| 4216 | // splat from elt type to vector. |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4217 | if (SrcTy->isPointerType()) |
| 4218 | return Diag(R.getBegin(), |
| 4219 | diag::err_invalid_conversion_between_vector_and_scalar) |
| 4220 | << DestTy << SrcTy << R; |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4221 | |
| 4222 | QualType DestElemTy = DestTy->getAs<ExtVectorType>()->getElementType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4223 | ExprResult CastExprRes = Owned(CastExpr); |
| 4224 | CastKind CK = PrepareScalarCast(*this, CastExprRes, DestElemTy); |
| 4225 | if (CastExprRes.isInvalid()) |
| 4226 | return ExprError(); |
| 4227 | CastExpr = ImpCastExprToType(CastExprRes.take(), DestElemTy, CK).take(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4228 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4229 | Kind = CK_VectorSplat; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4230 | return Owned(CastExpr); |
Nate Begeman | c69b740 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4231 | } |
| 4232 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4233 | ExprResult |
Argyrios Kyrtzidis | 7192a3b | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 4234 | Sema::ActOnCastExpr(Scope *S, SourceLocation LParenLoc, |
| 4235 | Declarator &D, ParsedType &Ty, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4236 | SourceLocation RParenLoc, Expr *castExpr) { |
Argyrios Kyrtzidis | 7192a3b | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 4237 | assert(!D.isInvalidType() && (castExpr != 0) && |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4238 | "ActOnCastExpr(): missing type or expr"); |
Steve Naroff | 1a2cf6b | 2007-07-16 23:25:18 +0000 | [diff] [blame] | 4239 | |
Argyrios Kyrtzidis | 7192a3b | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 4240 | TypeSourceInfo *castTInfo = GetTypeForDeclaratorCast(D, castExpr->getType()); |
| 4241 | if (D.isInvalidType()) |
| 4242 | return ExprError(); |
| 4243 | |
| 4244 | if (getLangOptions().CPlusPlus) { |
| 4245 | // Check that there are no default arguments (C++ only). |
| 4246 | CheckExtraCXXDefaultArguments(D); |
| 4247 | } |
| 4248 | |
| 4249 | QualType castType = castTInfo->getType(); |
| 4250 | Ty = CreateParsedType(castType, castTInfo); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4251 | |
Argyrios Kyrtzidis | d8701b6 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4252 | bool isVectorLiteral = false; |
| 4253 | |
| 4254 | // Check for an altivec or OpenCL literal, |
| 4255 | // i.e. all the elements are integer constants. |
| 4256 | ParenExpr *PE = dyn_cast<ParenExpr>(castExpr); |
| 4257 | ParenListExpr *PLE = dyn_cast<ParenListExpr>(castExpr); |
| 4258 | if (getLangOptions().AltiVec && castType->isVectorType() && (PE || PLE)) { |
| 4259 | if (PLE && PLE->getNumExprs() == 0) { |
| 4260 | Diag(PLE->getExprLoc(), diag::err_altivec_empty_initializer); |
| 4261 | return ExprError(); |
| 4262 | } |
| 4263 | if (PE || PLE->getNumExprs() == 1) { |
| 4264 | Expr *E = (PE ? PE->getSubExpr() : PLE->getExpr(0)); |
| 4265 | if (!E->getType()->isVectorType()) |
| 4266 | isVectorLiteral = true; |
| 4267 | } |
| 4268 | else |
| 4269 | isVectorLiteral = true; |
| 4270 | } |
| 4271 | |
| 4272 | // If this is a vector initializer, '(' type ')' '(' init, ..., init ')' |
| 4273 | // then handle it as such. |
| 4274 | if (isVectorLiteral) |
| 4275 | return BuildVectorLiteral(LParenLoc, RParenLoc, castExpr, castTInfo); |
| 4276 | |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4277 | // If the Expr being casted is a ParenListExpr, handle it specially. |
Argyrios Kyrtzidis | d8701b6 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4278 | // This is not an AltiVec-style cast, so turn the ParenListExpr into a |
| 4279 | // sequence of BinOp comma operators. |
| 4280 | if (isa<ParenListExpr>(castExpr)) { |
| 4281 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, castExpr); |
| 4282 | if (Result.isInvalid()) return ExprError(); |
| 4283 | castExpr = Result.take(); |
| 4284 | } |
John McCall | ebe5474 | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 4285 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4286 | return BuildCStyleCastExpr(LParenLoc, castTInfo, RParenLoc, castExpr); |
John McCall | ebe5474 | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 4287 | } |
| 4288 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4289 | ExprResult |
John McCall | ebe5474 | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 4290 | Sema::BuildCStyleCastExpr(SourceLocation LParenLoc, TypeSourceInfo *Ty, |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4291 | SourceLocation RParenLoc, Expr *castExpr) { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4292 | CastKind Kind = CK_Invalid; |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4293 | ExprValueKind VK = VK_RValue; |
John McCall | cf14216 | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4294 | CXXCastPath BasePath; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4295 | ExprResult CastResult = |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4296 | CheckCastTypes(LParenLoc, SourceRange(LParenLoc, RParenLoc), Ty->getType(), |
| 4297 | castExpr, Kind, VK, BasePath); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4298 | if (CastResult.isInvalid()) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4299 | return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4300 | castExpr = CastResult.take(); |
Anders Carlsson | e9766d5 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 4301 | |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4302 | return Owned(CStyleCastExpr::Create( |
| 4303 | Context, Ty->getType().getNonLValueExprType(Context), VK, Kind, castExpr, |
| 4304 | &BasePath, Ty, LParenLoc, RParenLoc)); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 4305 | } |
| 4306 | |
Argyrios Kyrtzidis | d8701b6 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4307 | ExprResult Sema::BuildVectorLiteral(SourceLocation LParenLoc, |
| 4308 | SourceLocation RParenLoc, Expr *E, |
| 4309 | TypeSourceInfo *TInfo) { |
| 4310 | assert((isa<ParenListExpr>(E) || isa<ParenExpr>(E)) && |
| 4311 | "Expected paren or paren list expression"); |
| 4312 | |
| 4313 | Expr **exprs; |
| 4314 | unsigned numExprs; |
| 4315 | Expr *subExpr; |
| 4316 | if (ParenListExpr *PE = dyn_cast<ParenListExpr>(E)) { |
| 4317 | exprs = PE->getExprs(); |
| 4318 | numExprs = PE->getNumExprs(); |
| 4319 | } else { |
| 4320 | subExpr = cast<ParenExpr>(E)->getSubExpr(); |
| 4321 | exprs = &subExpr; |
| 4322 | numExprs = 1; |
| 4323 | } |
| 4324 | |
| 4325 | QualType Ty = TInfo->getType(); |
| 4326 | assert(Ty->isVectorType() && "Expected vector type"); |
| 4327 | |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4328 | SmallVector<Expr *, 8> initExprs; |
Tanya Lattner | 8355938 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4329 | const VectorType *VTy = Ty->getAs<VectorType>(); |
| 4330 | unsigned numElems = Ty->getAs<VectorType>()->getNumElements(); |
| 4331 | |
Argyrios Kyrtzidis | d8701b6 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4332 | // '(...)' form of vector initialization in AltiVec: the number of |
| 4333 | // initializers must be one or must match the size of the vector. |
| 4334 | // If a single value is specified in the initializer then it will be |
| 4335 | // replicated to all the components of the vector |
Tanya Lattner | 8355938 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4336 | if (VTy->getVectorKind() == VectorType::AltiVecVector) { |
Argyrios Kyrtzidis | d8701b6 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4337 | // The number of initializers must be one or must match the size of the |
| 4338 | // vector. If a single value is specified in the initializer then it will |
| 4339 | // be replicated to all the components of the vector |
| 4340 | if (numExprs == 1) { |
| 4341 | QualType ElemTy = Ty->getAs<VectorType>()->getElementType(); |
| 4342 | ExprResult Literal = Owned(exprs[0]); |
| 4343 | Literal = ImpCastExprToType(Literal.take(), ElemTy, |
| 4344 | PrepareScalarCast(*this, Literal, ElemTy)); |
| 4345 | return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Literal.take()); |
| 4346 | } |
| 4347 | else if (numExprs < numElems) { |
| 4348 | Diag(E->getExprLoc(), |
| 4349 | diag::err_incorrect_number_of_vector_initializers); |
| 4350 | return ExprError(); |
| 4351 | } |
| 4352 | else |
| 4353 | for (unsigned i = 0, e = numExprs; i != e; ++i) |
| 4354 | initExprs.push_back(exprs[i]); |
| 4355 | } |
Tanya Lattner | 8355938 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4356 | else { |
| 4357 | // For OpenCL, when the number of initializers is a single value, |
| 4358 | // it will be replicated to all components of the vector. |
| 4359 | if (getLangOptions().OpenCL && |
| 4360 | VTy->getVectorKind() == VectorType::GenericVector && |
| 4361 | numExprs == 1) { |
| 4362 | QualType ElemTy = Ty->getAs<VectorType>()->getElementType(); |
| 4363 | ExprResult Literal = Owned(exprs[0]); |
| 4364 | Literal = ImpCastExprToType(Literal.take(), ElemTy, |
| 4365 | PrepareScalarCast(*this, Literal, ElemTy)); |
| 4366 | return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Literal.take()); |
| 4367 | } |
| 4368 | |
Argyrios Kyrtzidis | d8701b6 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4369 | for (unsigned i = 0, e = numExprs; i != e; ++i) |
| 4370 | initExprs.push_back(exprs[i]); |
Tanya Lattner | 8355938 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4371 | } |
Argyrios Kyrtzidis | d8701b6 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4372 | // FIXME: This means that pretty-printing the final AST will produce curly |
| 4373 | // braces instead of the original commas. |
| 4374 | InitListExpr *initE = new (Context) InitListExpr(Context, LParenLoc, |
| 4375 | &initExprs[0], |
| 4376 | initExprs.size(), RParenLoc); |
| 4377 | initE->setType(Ty); |
| 4378 | return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, initE); |
| 4379 | } |
| 4380 | |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4381 | /// This is not an AltiVec-style cast, so turn the ParenListExpr into a sequence |
| 4382 | /// of comma binary operators. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4383 | ExprResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4384 | Sema::MaybeConvertParenListExprToParenExpr(Scope *S, Expr *expr) { |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4385 | ParenListExpr *E = dyn_cast<ParenListExpr>(expr); |
| 4386 | if (!E) |
| 4387 | return Owned(expr); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4388 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4389 | ExprResult Result(E->getExpr(0)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4390 | |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4391 | for (unsigned i = 1, e = E->getNumExprs(); i != e && !Result.isInvalid(); ++i) |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4392 | Result = ActOnBinOp(S, E->getExprLoc(), tok::comma, Result.get(), |
| 4393 | E->getExpr(i)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4394 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4395 | if (Result.isInvalid()) return ExprError(); |
| 4396 | |
| 4397 | return ActOnParenExpr(E->getLParenLoc(), E->getRParenLoc(), Result.get()); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4398 | } |
| 4399 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4400 | ExprResult Sema::ActOnParenOrParenListExpr(SourceLocation L, |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4401 | SourceLocation R, |
Argyrios Kyrtzidis | d8701b6 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4402 | MultiExprArg Val) { |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4403 | unsigned nexprs = Val.size(); |
| 4404 | Expr **exprs = reinterpret_cast<Expr**>(Val.release()); |
Fariborz Jahanian | 906d871 | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 4405 | assert((exprs != 0) && "ActOnParenOrParenListExpr() missing expr list"); |
| 4406 | Expr *expr; |
Argyrios Kyrtzidis | d8701b6 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4407 | if (nexprs == 1) |
Fariborz Jahanian | 906d871 | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 4408 | expr = new (Context) ParenExpr(L, R, exprs[0]); |
| 4409 | else |
Manuel Klimek | f2b4b69 | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 4410 | expr = new (Context) ParenListExpr(Context, L, exprs, nexprs, R, |
| 4411 | exprs[nexprs-1]->getType()); |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4412 | return Owned(expr); |
| 4413 | } |
| 4414 | |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4415 | /// \brief Emit a specialized diagnostic when one expression is a null pointer |
| 4416 | /// constant and the other is not a pointer. |
| 4417 | bool Sema::DiagnoseConditionalForNull(Expr *LHS, Expr *RHS, |
| 4418 | SourceLocation QuestionLoc) { |
| 4419 | Expr *NullExpr = LHS; |
| 4420 | Expr *NonPointerExpr = RHS; |
| 4421 | Expr::NullPointerConstantKind NullKind = |
| 4422 | NullExpr->isNullPointerConstant(Context, |
| 4423 | Expr::NPC_ValueDependentIsNotNull); |
| 4424 | |
| 4425 | if (NullKind == Expr::NPCK_NotNull) { |
| 4426 | NullExpr = RHS; |
| 4427 | NonPointerExpr = LHS; |
| 4428 | NullKind = |
| 4429 | NullExpr->isNullPointerConstant(Context, |
| 4430 | Expr::NPC_ValueDependentIsNotNull); |
| 4431 | } |
| 4432 | |
| 4433 | if (NullKind == Expr::NPCK_NotNull) |
| 4434 | return false; |
| 4435 | |
| 4436 | if (NullKind == Expr::NPCK_ZeroInteger) { |
| 4437 | // In this case, check to make sure that we got here from a "NULL" |
| 4438 | // string in the source code. |
| 4439 | NullExpr = NullExpr->IgnoreParenImpCasts(); |
John McCall | 462c055 | 2011-03-08 07:59:04 +0000 | [diff] [blame] | 4440 | SourceLocation loc = NullExpr->getExprLoc(); |
| 4441 | if (!findMacroSpelling(loc, "NULL")) |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4442 | return false; |
| 4443 | } |
| 4444 | |
| 4445 | int DiagType = (NullKind == Expr::NPCK_CXX0X_nullptr); |
| 4446 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands_null) |
| 4447 | << NonPointerExpr->getType() << DiagType |
| 4448 | << NonPointerExpr->getSourceRange(); |
| 4449 | return true; |
| 4450 | } |
| 4451 | |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4452 | /// Note that lhs is not null here, even if this is the gnu "x ?: y" extension. |
| 4453 | /// In that case, lhs = cond. |
Chris Lattner | 2c48660 | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 4454 | /// C99 6.5.15 |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4455 | QualType Sema::CheckConditionalOperands(ExprResult &Cond, ExprResult &LHS, |
| 4456 | ExprResult &RHS, ExprValueKind &VK, |
| 4457 | ExprObjectKind &OK, |
Chris Lattner | 2c48660 | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 4458 | SourceLocation QuestionLoc) { |
Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 4459 | |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 4460 | ExprResult lhsResult = CheckPlaceholderExpr(LHS.get()); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 4461 | if (!lhsResult.isUsable()) return QualType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4462 | LHS = move(lhsResult); |
Douglas Gregor | 0124e9b | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 4463 | |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 4464 | ExprResult rhsResult = CheckPlaceholderExpr(RHS.get()); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 4465 | if (!rhsResult.isUsable()) return QualType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4466 | RHS = move(rhsResult); |
Douglas Gregor | 0124e9b | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 4467 | |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4468 | // C++ is sufficiently different to merit its own checker. |
| 4469 | if (getLangOptions().CPlusPlus) |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4470 | return CXXCheckConditionalOperands(Cond, LHS, RHS, VK, OK, QuestionLoc); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4471 | |
| 4472 | VK = VK_RValue; |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4473 | OK = OK_Ordinary; |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4474 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4475 | Cond = UsualUnaryConversions(Cond.take()); |
| 4476 | if (Cond.isInvalid()) |
| 4477 | return QualType(); |
| 4478 | LHS = UsualUnaryConversions(LHS.take()); |
| 4479 | if (LHS.isInvalid()) |
| 4480 | return QualType(); |
| 4481 | RHS = UsualUnaryConversions(RHS.take()); |
| 4482 | if (RHS.isInvalid()) |
| 4483 | return QualType(); |
| 4484 | |
| 4485 | QualType CondTy = Cond.get()->getType(); |
| 4486 | QualType LHSTy = LHS.get()->getType(); |
| 4487 | QualType RHSTy = RHS.get()->getType(); |
Steve Naroff | 3109001 | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 4488 | |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 4489 | // first, check the condition. |
Sebastian Redl | 1a99f44 | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4490 | if (!CondTy->isScalarType()) { // C99 6.5.15p2 |
Nate Begeman | abb5a73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 4491 | // OpenCL: Sec 6.3.i says the condition is allowed to be a vector or scalar. |
| 4492 | // Throw an error if its not either. |
| 4493 | if (getLangOptions().OpenCL) { |
| 4494 | if (!CondTy->isVectorType()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4495 | Diag(Cond.get()->getLocStart(), |
Nate Begeman | abb5a73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 4496 | diag::err_typecheck_cond_expect_scalar_or_vector) |
| 4497 | << CondTy; |
| 4498 | return QualType(); |
| 4499 | } |
| 4500 | } |
| 4501 | else { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4502 | Diag(Cond.get()->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
Nate Begeman | abb5a73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 4503 | << CondTy; |
| 4504 | return QualType(); |
| 4505 | } |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 4506 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4507 | |
Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4508 | // Now check the two expressions. |
Nate Begeman | 5ec4b31 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4509 | if (LHSTy->isVectorType() || RHSTy->isVectorType()) |
Eli Friedman | 1408bc9 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 4510 | return CheckVectorOperands(LHS, RHS, QuestionLoc, /*isCompAssign*/false); |
Douglas Gregor | 4619e43 | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 4511 | |
Nate Begeman | abb5a73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 4512 | // OpenCL: If the condition is a vector, and both operands are scalar, |
| 4513 | // attempt to implicity convert them to the vector type to act like the |
| 4514 | // built in select. |
| 4515 | if (getLangOptions().OpenCL && CondTy->isVectorType()) { |
| 4516 | // Both operands should be of scalar type. |
| 4517 | if (!LHSTy->isScalarType()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4518 | Diag(LHS.get()->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
Nate Begeman | abb5a73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 4519 | << CondTy; |
| 4520 | return QualType(); |
| 4521 | } |
| 4522 | if (!RHSTy->isScalarType()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4523 | Diag(RHS.get()->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
Nate Begeman | abb5a73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 4524 | << CondTy; |
| 4525 | return QualType(); |
| 4526 | } |
| 4527 | // Implicity convert these scalars to the type of the condition. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4528 | LHS = ImpCastExprToType(LHS.take(), CondTy, CK_IntegralCast); |
| 4529 | RHS = ImpCastExprToType(RHS.take(), CondTy, CK_IntegralCast); |
Nate Begeman | abb5a73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 4530 | } |
| 4531 | |
Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4532 | // If both operands have arithmetic type, do the usual arithmetic conversions |
| 4533 | // to find a common type: C99 6.5.15p3,5. |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4534 | if (LHSTy->isArithmeticType() && RHSTy->isArithmeticType()) { |
| 4535 | UsualArithmeticConversions(LHS, RHS); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4536 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 4537 | return QualType(); |
| 4538 | return LHS.get()->getType(); |
Steve Naroff | dbd9e89 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 4539 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4540 | |
Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4541 | // If both operands are the same structure or union type, the result is that |
| 4542 | // type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4543 | if (const RecordType *LHSRT = LHSTy->getAs<RecordType>()) { // C99 6.5.15p3 |
| 4544 | if (const RecordType *RHSRT = RHSTy->getAs<RecordType>()) |
Chris Lattner | 2ab40a6 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 4545 | if (LHSRT->getDecl() == RHSRT->getDecl()) |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4546 | // "If both the operands have structure or union type, the result has |
Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4547 | // that type." This implies that CV qualifiers are dropped. |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4548 | return LHSTy.getUnqualifiedType(); |
Eli Friedman | ba961a9 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 4549 | // FIXME: Type of conditional expression must be complete in C mode. |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 4550 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4551 | |
Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4552 | // 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] | 4553 | // The following || allows only one side to be void (a GCC-ism). |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4554 | if (LHSTy->isVoidType() || RHSTy->isVoidType()) { |
| 4555 | if (!LHSTy->isVoidType()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4556 | Diag(RHS.get()->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 4557 | << RHS.get()->getSourceRange(); |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4558 | if (!RHSTy->isVoidType()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4559 | Diag(LHS.get()->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 4560 | << LHS.get()->getSourceRange(); |
| 4561 | LHS = ImpCastExprToType(LHS.take(), Context.VoidTy, CK_ToVoid); |
| 4562 | RHS = ImpCastExprToType(RHS.take(), Context.VoidTy, CK_ToVoid); |
Eli Friedman | 3e1852f | 2008-06-04 19:47:51 +0000 | [diff] [blame] | 4563 | return Context.VoidTy; |
Steve Naroff | bf1516c | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 4564 | } |
Steve Naroff | 039ad3c | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 4565 | // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has |
| 4566 | // the type of the other operand." |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4567 | if ((LHSTy->isAnyPointerType() || LHSTy->isBlockPointerType()) && |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4568 | RHS.get()->isNullPointerConstant(Context, |
| 4569 | Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4570 | // promote the null to a pointer. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4571 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_NullToPointer); |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4572 | return LHSTy; |
Steve Naroff | 039ad3c | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 4573 | } |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4574 | if ((RHSTy->isAnyPointerType() || RHSTy->isBlockPointerType()) && |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4575 | LHS.get()->isNullPointerConstant(Context, |
| 4576 | Expr::NPC_ValueDependentIsNull)) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4577 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_NullToPointer); |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4578 | return RHSTy; |
Steve Naroff | 039ad3c | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 4579 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4580 | |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4581 | // All objective-c pointer type analysis is done here. |
| 4582 | QualType compositeType = FindCompositeObjCPointerType(LHS, RHS, |
| 4583 | QuestionLoc); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4584 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 4585 | return QualType(); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4586 | if (!compositeType.isNull()) |
| 4587 | return compositeType; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4588 | |
| 4589 | |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4590 | // Handle block pointer types. |
| 4591 | if (LHSTy->isBlockPointerType() || RHSTy->isBlockPointerType()) { |
| 4592 | if (!LHSTy->isBlockPointerType() || !RHSTy->isBlockPointerType()) { |
| 4593 | if (LHSTy->isVoidPointerType() || RHSTy->isVoidPointerType()) { |
| 4594 | QualType destType = Context.getPointerType(Context.VoidTy); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4595 | LHS = ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
| 4596 | RHS = ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4597 | return destType; |
| 4598 | } |
| 4599 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4600 | << LHSTy << RHSTy << LHS.get()->getSourceRange() |
| 4601 | << RHS.get()->getSourceRange(); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4602 | return QualType(); |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4603 | } |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4604 | // We have 2 block pointer types. |
| 4605 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 4606 | // Two identical block pointer types are always compatible. |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4607 | return LHSTy; |
| 4608 | } |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4609 | // The block pointer types aren't identical, continue checking. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4610 | QualType lhptee = LHSTy->getAs<BlockPointerType>()->getPointeeType(); |
| 4611 | QualType rhptee = RHSTy->getAs<BlockPointerType>()->getPointeeType(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4612 | |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4613 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 4614 | rhptee.getUnqualifiedType())) { |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4615 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4616 | << LHSTy << RHSTy << LHS.get()->getSourceRange() |
| 4617 | << RHS.get()->getSourceRange(); |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4618 | // In this situation, we assume void* type. No especially good |
| 4619 | // reason, but this is what gcc does, and we do have to pick |
| 4620 | // to get a consistent AST. |
| 4621 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4622 | LHS = ImpCastExprToType(LHS.take(), incompatTy, CK_BitCast); |
| 4623 | RHS = ImpCastExprToType(RHS.take(), incompatTy, CK_BitCast); |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4624 | return incompatTy; |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 4625 | } |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4626 | // The block pointer types are compatible. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4627 | LHS = ImpCastExprToType(LHS.take(), LHSTy, CK_BitCast); |
| 4628 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Steve Naroff | ea4c780 | 2009-04-08 17:05:15 +0000 | [diff] [blame] | 4629 | return LHSTy; |
| 4630 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4631 | |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4632 | // Check constraints for C object pointers types (C99 6.5.15p3,6). |
| 4633 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
| 4634 | // get the "pointed to" types |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4635 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 4636 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4637 | |
| 4638 | // ignore qualifiers on void (C99 6.5.15p3, clause 6) |
| 4639 | if (lhptee->isVoidType() && rhptee->isIncompleteOrObjectType()) { |
| 4640 | // Figure out necessary qualifiers (C99 6.5.15p6) |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4641 | QualType destPointee |
| 4642 | = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4643 | QualType destType = Context.getPointerType(destPointee); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4644 | // Add qualifiers if necessary. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4645 | LHS = ImpCastExprToType(LHS.take(), destType, CK_NoOp); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4646 | // Promote to void*. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4647 | RHS = ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4648 | return destType; |
| 4649 | } |
| 4650 | if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) { |
John McCall | 8ccfcb5 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4651 | QualType destPointee |
| 4652 | = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4653 | QualType destType = Context.getPointerType(destPointee); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4654 | // Add qualifiers if necessary. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4655 | RHS = ImpCastExprToType(RHS.take(), destType, CK_NoOp); |
Eli Friedman | 06ed2a5 | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4656 | // Promote to void*. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4657 | LHS = ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4658 | return destType; |
| 4659 | } |
| 4660 | |
| 4661 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 4662 | // Two identical pointer types are always compatible. |
| 4663 | return LHSTy; |
| 4664 | } |
| 4665 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 4666 | rhptee.getUnqualifiedType())) { |
| 4667 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4668 | << LHSTy << RHSTy << LHS.get()->getSourceRange() |
| 4669 | << RHS.get()->getSourceRange(); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4670 | // In this situation, we assume void* type. No especially good |
| 4671 | // reason, but this is what gcc does, and we do have to pick |
| 4672 | // to get a consistent AST. |
| 4673 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4674 | LHS = ImpCastExprToType(LHS.take(), incompatTy, CK_BitCast); |
| 4675 | RHS = ImpCastExprToType(RHS.take(), incompatTy, CK_BitCast); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4676 | return incompatTy; |
| 4677 | } |
| 4678 | // The pointer types are compatible. |
| 4679 | // C99 6.5.15p6: If both operands are pointers to compatible types *or* to |
| 4680 | // differently qualified versions of compatible types, the result type is |
| 4681 | // a pointer to an appropriately qualified version of the *composite* |
| 4682 | // type. |
| 4683 | // FIXME: Need to calculate the composite type. |
| 4684 | // FIXME: Need to add qualifiers |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4685 | LHS = ImpCastExprToType(LHS.take(), LHSTy, CK_BitCast); |
| 4686 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4687 | return LHSTy; |
| 4688 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4689 | |
John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 4690 | // GCC compatibility: soften pointer/integer mismatch. Note that |
| 4691 | // null pointers have been filtered out by this point. |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4692 | if (RHSTy->isPointerType() && LHSTy->isIntegerType()) { |
| 4693 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4694 | << LHSTy << RHSTy << LHS.get()->getSourceRange() |
| 4695 | << RHS.get()->getSourceRange(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4696 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_IntegralToPointer); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4697 | return RHSTy; |
| 4698 | } |
| 4699 | if (LHSTy->isPointerType() && RHSTy->isIntegerType()) { |
| 4700 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4701 | << LHSTy << RHSTy << LHS.get()->getSourceRange() |
| 4702 | << RHS.get()->getSourceRange(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4703 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_IntegralToPointer); |
Steve Naroff | 05efa97 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4704 | return LHSTy; |
| 4705 | } |
Daniel Dunbar | 484603b | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 4706 | |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4707 | // Emit a better diagnostic if one of the expressions is a null pointer |
| 4708 | // constant and the other is not a pointer type. In this case, the user most |
| 4709 | // likely forgot to take the address of the other expression. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4710 | if (DiagnoseConditionalForNull(LHS.get(), RHS.get(), QuestionLoc)) |
Chandler Carruth | a8bea4b | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4711 | return QualType(); |
| 4712 | |
Chris Lattner | e2949f4 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4713 | // Otherwise, the operands are not compatible. |
Chris Lattner | 432cff5 | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4714 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4715 | << LHSTy << RHSTy << LHS.get()->getSourceRange() |
| 4716 | << RHS.get()->getSourceRange(); |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 4717 | return QualType(); |
Steve Naroff | f8a28c5 | 2007-05-15 20:29:32 +0000 | [diff] [blame] | 4718 | } |
| 4719 | |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4720 | /// FindCompositeObjCPointerType - Helper method to find composite type of |
| 4721 | /// two objective-c pointer types of the two input expressions. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4722 | QualType Sema::FindCompositeObjCPointerType(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4723 | SourceLocation QuestionLoc) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4724 | QualType LHSTy = LHS.get()->getType(); |
| 4725 | QualType RHSTy = RHS.get()->getType(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4726 | |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4727 | // Handle things like Class and struct objc_class*. Here we case the result |
| 4728 | // to the pseudo-builtin, because that will be implicitly cast back to the |
| 4729 | // redefinition type if an attempt is made to access its fields. |
| 4730 | if (LHSTy->isObjCClassType() && |
John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 4731 | (Context.hasSameType(RHSTy, Context.ObjCClassRedefinitionType))) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4732 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4733 | return LHSTy; |
| 4734 | } |
| 4735 | if (RHSTy->isObjCClassType() && |
John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 4736 | (Context.hasSameType(LHSTy, Context.ObjCClassRedefinitionType))) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4737 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4738 | return RHSTy; |
| 4739 | } |
| 4740 | // And the same for struct objc_object* / id |
| 4741 | if (LHSTy->isObjCIdType() && |
John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 4742 | (Context.hasSameType(RHSTy, Context.ObjCIdRedefinitionType))) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4743 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4744 | return LHSTy; |
| 4745 | } |
| 4746 | if (RHSTy->isObjCIdType() && |
John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 4747 | (Context.hasSameType(LHSTy, Context.ObjCIdRedefinitionType))) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4748 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4749 | return RHSTy; |
| 4750 | } |
| 4751 | // And the same for struct objc_selector* / SEL |
| 4752 | if (Context.isObjCSelType(LHSTy) && |
John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 4753 | (Context.hasSameType(RHSTy, Context.ObjCSelRedefinitionType))) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4754 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4755 | return LHSTy; |
| 4756 | } |
| 4757 | if (Context.isObjCSelType(RHSTy) && |
John McCall | 717d9b0 | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 4758 | (Context.hasSameType(LHSTy, Context.ObjCSelRedefinitionType))) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4759 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4760 | return RHSTy; |
| 4761 | } |
| 4762 | // Check constraints for Objective-C object pointers types. |
| 4763 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isObjCObjectPointerType()) { |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4764 | |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4765 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 4766 | // Two identical object pointer types are always compatible. |
| 4767 | return LHSTy; |
| 4768 | } |
| 4769 | const ObjCObjectPointerType *LHSOPT = LHSTy->getAs<ObjCObjectPointerType>(); |
| 4770 | const ObjCObjectPointerType *RHSOPT = RHSTy->getAs<ObjCObjectPointerType>(); |
| 4771 | QualType compositeType = LHSTy; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4772 | |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4773 | // If both operands are interfaces and either operand can be |
| 4774 | // assigned to the other, use that type as the composite |
| 4775 | // type. This allows |
| 4776 | // xxx ? (A*) a : (B*) b |
| 4777 | // where B is a subclass of A. |
| 4778 | // |
| 4779 | // Additionally, as for assignment, if either type is 'id' |
| 4780 | // allow silent coercion. Finally, if the types are |
| 4781 | // incompatible then make sure to use 'id' as the composite |
| 4782 | // type so the result is acceptable for sending messages to. |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4783 | |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4784 | // FIXME: Consider unifying with 'areComparableObjCPointerTypes'. |
| 4785 | // It could return the composite type. |
| 4786 | if (Context.canAssignObjCInterfaces(LHSOPT, RHSOPT)) { |
| 4787 | compositeType = RHSOPT->isObjCBuiltinType() ? RHSTy : LHSTy; |
| 4788 | } else if (Context.canAssignObjCInterfaces(RHSOPT, LHSOPT)) { |
| 4789 | compositeType = LHSOPT->isObjCBuiltinType() ? LHSTy : RHSTy; |
| 4790 | } else if ((LHSTy->isObjCQualifiedIdType() || |
| 4791 | RHSTy->isObjCQualifiedIdType()) && |
| 4792 | Context.ObjCQualifiedIdTypesAreCompatible(LHSTy, RHSTy, true)) { |
| 4793 | // Need to handle "id<xx>" explicitly. |
| 4794 | // GCC allows qualified id and any Objective-C type to devolve to |
| 4795 | // id. Currently localizing to here until clear this should be |
| 4796 | // part of ObjCQualifiedIdTypesAreCompatible. |
| 4797 | compositeType = Context.getObjCIdType(); |
| 4798 | } else if (LHSTy->isObjCIdType() || RHSTy->isObjCIdType()) { |
| 4799 | compositeType = Context.getObjCIdType(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4800 | } else if (!(compositeType = |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4801 | Context.areCommonBaseCompatible(LHSOPT, RHSOPT)).isNull()) |
| 4802 | ; |
| 4803 | else { |
| 4804 | Diag(QuestionLoc, diag::ext_typecheck_cond_incompatible_operands) |
| 4805 | << LHSTy << RHSTy |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4806 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4807 | QualType incompatTy = Context.getObjCIdType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4808 | LHS = ImpCastExprToType(LHS.take(), incompatTy, CK_BitCast); |
| 4809 | RHS = ImpCastExprToType(RHS.take(), incompatTy, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4810 | return incompatTy; |
| 4811 | } |
| 4812 | // The object pointer types are compatible. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4813 | LHS = ImpCastExprToType(LHS.take(), compositeType, CK_BitCast); |
| 4814 | RHS = ImpCastExprToType(RHS.take(), compositeType, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4815 | return compositeType; |
| 4816 | } |
| 4817 | // Check Objective-C object pointer types and 'void *' |
| 4818 | if (LHSTy->isVoidPointerType() && RHSTy->isObjCObjectPointerType()) { |
| 4819 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 4820 | QualType rhptee = RHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 4821 | QualType destPointee |
| 4822 | = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
| 4823 | QualType destType = Context.getPointerType(destPointee); |
| 4824 | // Add qualifiers if necessary. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4825 | LHS = ImpCastExprToType(LHS.take(), destType, CK_NoOp); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4826 | // Promote to void*. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4827 | RHS = ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4828 | return destType; |
| 4829 | } |
| 4830 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isVoidPointerType()) { |
| 4831 | QualType lhptee = LHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 4832 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
| 4833 | QualType destPointee |
| 4834 | = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
| 4835 | QualType destType = Context.getPointerType(destPointee); |
| 4836 | // Add qualifiers if necessary. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4837 | RHS = ImpCastExprToType(RHS.take(), destType, CK_NoOp); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4838 | // Promote to void*. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4839 | LHS = ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
Fariborz Jahanian | a430f71 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4840 | return destType; |
| 4841 | } |
| 4842 | return QualType(); |
| 4843 | } |
| 4844 | |
Chandler Carruth | b00e8c0 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 4845 | /// SuggestParentheses - Emit a note with a fixit hint that wraps |
Hans Wennborg | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4846 | /// ParenRange in parentheses. |
| 4847 | static void SuggestParentheses(Sema &Self, SourceLocation Loc, |
Chandler Carruth | b00e8c0 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 4848 | const PartialDiagnostic &Note, |
| 4849 | SourceRange ParenRange) { |
| 4850 | SourceLocation EndLoc = Self.PP.getLocForEndOfToken(ParenRange.getEnd()); |
| 4851 | if (ParenRange.getBegin().isFileID() && ParenRange.getEnd().isFileID() && |
| 4852 | EndLoc.isValid()) { |
| 4853 | Self.Diag(Loc, Note) |
| 4854 | << FixItHint::CreateInsertion(ParenRange.getBegin(), "(") |
| 4855 | << FixItHint::CreateInsertion(EndLoc, ")"); |
| 4856 | } else { |
| 4857 | // We can't display the parentheses, so just show the bare note. |
| 4858 | Self.Diag(Loc, Note) << ParenRange; |
Hans Wennborg | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4859 | } |
Hans Wennborg | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4860 | } |
| 4861 | |
| 4862 | static bool IsArithmeticOp(BinaryOperatorKind Opc) { |
| 4863 | return Opc >= BO_Mul && Opc <= BO_Shr; |
| 4864 | } |
| 4865 | |
Hans Wennborg | de2e67e | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4866 | /// IsArithmeticBinaryExpr - Returns true if E is an arithmetic binary |
| 4867 | /// expression, either using a built-in or overloaded operator, |
| 4868 | /// and sets *OpCode to the opcode and *RHS to the right-hand side expression. |
| 4869 | static bool IsArithmeticBinaryExpr(Expr *E, BinaryOperatorKind *Opcode, |
| 4870 | Expr **RHS) { |
| 4871 | E = E->IgnoreParenImpCasts(); |
| 4872 | E = E->IgnoreConversionOperator(); |
| 4873 | E = E->IgnoreParenImpCasts(); |
| 4874 | |
| 4875 | // Built-in binary operator. |
| 4876 | if (BinaryOperator *OP = dyn_cast<BinaryOperator>(E)) { |
| 4877 | if (IsArithmeticOp(OP->getOpcode())) { |
| 4878 | *Opcode = OP->getOpcode(); |
| 4879 | *RHS = OP->getRHS(); |
| 4880 | return true; |
| 4881 | } |
| 4882 | } |
| 4883 | |
| 4884 | // Overloaded operator. |
| 4885 | if (CXXOperatorCallExpr *Call = dyn_cast<CXXOperatorCallExpr>(E)) { |
| 4886 | if (Call->getNumArgs() != 2) |
| 4887 | return false; |
| 4888 | |
| 4889 | // Make sure this is really a binary operator that is safe to pass into |
| 4890 | // BinaryOperator::getOverloadedOpcode(), e.g. it's not a subscript op. |
| 4891 | OverloadedOperatorKind OO = Call->getOperator(); |
| 4892 | if (OO < OO_Plus || OO > OO_Arrow) |
| 4893 | return false; |
| 4894 | |
| 4895 | BinaryOperatorKind OpKind = BinaryOperator::getOverloadedOpcode(OO); |
| 4896 | if (IsArithmeticOp(OpKind)) { |
| 4897 | *Opcode = OpKind; |
| 4898 | *RHS = Call->getArg(1); |
| 4899 | return true; |
| 4900 | } |
| 4901 | } |
| 4902 | |
| 4903 | return false; |
| 4904 | } |
| 4905 | |
Hans Wennborg | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4906 | static bool IsLogicOp(BinaryOperatorKind Opc) { |
| 4907 | return (Opc >= BO_LT && Opc <= BO_NE) || (Opc >= BO_LAnd && Opc <= BO_LOr); |
| 4908 | } |
| 4909 | |
Hans Wennborg | de2e67e | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4910 | /// ExprLooksBoolean - Returns true if E looks boolean, i.e. it has boolean type |
| 4911 | /// or is a logical expression such as (x==y) which has int type, but is |
| 4912 | /// commonly interpreted as boolean. |
| 4913 | static bool ExprLooksBoolean(Expr *E) { |
| 4914 | E = E->IgnoreParenImpCasts(); |
| 4915 | |
| 4916 | if (E->getType()->isBooleanType()) |
| 4917 | return true; |
| 4918 | if (BinaryOperator *OP = dyn_cast<BinaryOperator>(E)) |
| 4919 | return IsLogicOp(OP->getOpcode()); |
| 4920 | if (UnaryOperator *OP = dyn_cast<UnaryOperator>(E)) |
| 4921 | return OP->getOpcode() == UO_LNot; |
| 4922 | |
| 4923 | return false; |
| 4924 | } |
| 4925 | |
Hans Wennborg | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4926 | /// DiagnoseConditionalPrecedence - Emit a warning when a conditional operator |
| 4927 | /// and binary operator are mixed in a way that suggests the programmer assumed |
| 4928 | /// the conditional operator has higher precedence, for example: |
| 4929 | /// "int x = a + someBinaryCondition ? 1 : 2". |
| 4930 | static void DiagnoseConditionalPrecedence(Sema &Self, |
| 4931 | SourceLocation OpLoc, |
Chandler Carruth | 08dc2ba | 2011-06-16 01:05:08 +0000 | [diff] [blame] | 4932 | Expr *Condition, |
| 4933 | Expr *LHS, |
| 4934 | Expr *RHS) { |
Hans Wennborg | de2e67e | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4935 | BinaryOperatorKind CondOpcode; |
| 4936 | Expr *CondRHS; |
Hans Wennborg | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4937 | |
Chandler Carruth | 08dc2ba | 2011-06-16 01:05:08 +0000 | [diff] [blame] | 4938 | if (!IsArithmeticBinaryExpr(Condition, &CondOpcode, &CondRHS)) |
Hans Wennborg | de2e67e | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4939 | return; |
| 4940 | if (!ExprLooksBoolean(CondRHS)) |
| 4941 | return; |
Hans Wennborg | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4942 | |
Hans Wennborg | de2e67e | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4943 | // The condition is an arithmetic binary expression, with a right- |
| 4944 | // hand side that looks boolean, so warn. |
Hans Wennborg | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4945 | |
Chandler Carruth | b00e8c0 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 4946 | Self.Diag(OpLoc, diag::warn_precedence_conditional) |
Chandler Carruth | 08dc2ba | 2011-06-16 01:05:08 +0000 | [diff] [blame] | 4947 | << Condition->getSourceRange() |
Hans Wennborg | de2e67e | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4948 | << BinaryOperator::getOpcodeStr(CondOpcode); |
Hans Wennborg | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4949 | |
Chandler Carruth | b00e8c0 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 4950 | SuggestParentheses(Self, OpLoc, |
| 4951 | Self.PDiag(diag::note_precedence_conditional_silence) |
| 4952 | << BinaryOperator::getOpcodeStr(CondOpcode), |
| 4953 | SourceRange(Condition->getLocStart(), Condition->getLocEnd())); |
Chandler Carruth | f51c5a5 | 2011-06-21 23:04:18 +0000 | [diff] [blame] | 4954 | |
| 4955 | SuggestParentheses(Self, OpLoc, |
| 4956 | Self.PDiag(diag::note_precedence_conditional_first), |
| 4957 | SourceRange(CondRHS->getLocStart(), RHS->getLocEnd())); |
Hans Wennborg | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4958 | } |
| 4959 | |
Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 4960 | /// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 4961 | /// in the case of a the GNU conditional expr extension. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4962 | ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4963 | SourceLocation ColonLoc, |
| 4964 | Expr *CondExpr, Expr *LHSExpr, |
| 4965 | Expr *RHSExpr) { |
Chris Lattner | 2ab40a6 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 4966 | // If this is the gnu "x ?: y" extension, analyze the types as though the LHS |
| 4967 | // was the condition. |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4968 | OpaqueValueExpr *opaqueValue = 0; |
| 4969 | Expr *commonExpr = 0; |
| 4970 | if (LHSExpr == 0) { |
| 4971 | commonExpr = CondExpr; |
| 4972 | |
| 4973 | // We usually want to apply unary conversions *before* saving, except |
| 4974 | // in the special case of a C++ l-value conditional. |
| 4975 | if (!(getLangOptions().CPlusPlus |
| 4976 | && !commonExpr->isTypeDependent() |
| 4977 | && commonExpr->getValueKind() == RHSExpr->getValueKind() |
| 4978 | && commonExpr->isGLValue() |
| 4979 | && commonExpr->isOrdinaryOrBitFieldObject() |
| 4980 | && RHSExpr->isOrdinaryOrBitFieldObject() |
| 4981 | && Context.hasSameType(commonExpr->getType(), RHSExpr->getType()))) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4982 | ExprResult commonRes = UsualUnaryConversions(commonExpr); |
| 4983 | if (commonRes.isInvalid()) |
| 4984 | return ExprError(); |
| 4985 | commonExpr = commonRes.take(); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4986 | } |
| 4987 | |
| 4988 | opaqueValue = new (Context) OpaqueValueExpr(commonExpr->getExprLoc(), |
| 4989 | commonExpr->getType(), |
| 4990 | commonExpr->getValueKind(), |
| 4991 | commonExpr->getObjectKind()); |
| 4992 | LHSExpr = CondExpr = opaqueValue; |
Fariborz Jahanian | c6bf0bd | 2010-08-31 18:02:20 +0000 | [diff] [blame] | 4993 | } |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4994 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4995 | ExprValueKind VK = VK_RValue; |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4996 | ExprObjectKind OK = OK_Ordinary; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4997 | ExprResult Cond = Owned(CondExpr), LHS = Owned(LHSExpr), RHS = Owned(RHSExpr); |
| 4998 | QualType result = CheckConditionalOperands(Cond, LHS, RHS, |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4999 | VK, OK, QuestionLoc); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5000 | if (result.isNull() || Cond.isInvalid() || LHS.isInvalid() || |
| 5001 | RHS.isInvalid()) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5002 | return ExprError(); |
| 5003 | |
Hans Wennborg | cf9bac4 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5004 | DiagnoseConditionalPrecedence(*this, QuestionLoc, Cond.get(), LHS.get(), |
| 5005 | RHS.get()); |
| 5006 | |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5007 | if (!commonExpr) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5008 | return Owned(new (Context) ConditionalOperator(Cond.take(), QuestionLoc, |
| 5009 | LHS.take(), ColonLoc, |
| 5010 | RHS.take(), result, VK, OK)); |
John McCall | c07a0c7 | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5011 | |
| 5012 | return Owned(new (Context) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5013 | BinaryConditionalOperator(commonExpr, opaqueValue, Cond.take(), LHS.take(), |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5014 | RHS.take(), QuestionLoc, ColonLoc, result, VK, |
| 5015 | OK)); |
Chris Lattner | e168f76 | 2006-11-10 05:29:30 +0000 | [diff] [blame] | 5016 | } |
| 5017 | |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5018 | // checkPointerTypesForAssignment - This is a very tricky routine (despite |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5019 | // being closely modeled after the C99 spec:-). The odd characteristic of this |
Steve Naroff | 3f59729 | 2007-05-11 22:18:03 +0000 | [diff] [blame] | 5020 | // routine is it effectively iqnores the qualifiers on the top level pointee. |
| 5021 | // This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3]. |
| 5022 | // FIXME: add a couple examples in this comment. |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5023 | static Sema::AssignConvertType |
| 5024 | checkPointerTypesForAssignment(Sema &S, QualType lhsType, QualType rhsType) { |
| 5025 | assert(lhsType.isCanonical() && "LHS not canonicalized!"); |
| 5026 | assert(rhsType.isCanonical() && "RHS not canonicalized!"); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5027 | |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 5028 | // get the "pointed to" type (ignoring qualifiers at the top level) |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5029 | const Type *lhptee, *rhptee; |
| 5030 | Qualifiers lhq, rhq; |
| 5031 | llvm::tie(lhptee, lhq) = cast<PointerType>(lhsType)->getPointeeType().split(); |
| 5032 | llvm::tie(rhptee, rhq) = cast<PointerType>(rhsType)->getPointeeType().split(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5033 | |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5034 | Sema::AssignConvertType ConvTy = Sema::Compatible; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5035 | |
| 5036 | // C99 6.5.16.1p1: This following citation is common to constraints |
| 5037 | // 3 & 4 (below). ...and the type *pointed to* by the left has all the |
| 5038 | // qualifiers of the type *pointed to* by the right; |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5039 | Qualifiers lq; |
| 5040 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5041 | // As a special case, 'non-__weak A *' -> 'non-__weak const *' is okay. |
| 5042 | if (lhq.getObjCLifetime() != rhq.getObjCLifetime() && |
| 5043 | lhq.compatiblyIncludesObjCLifetime(rhq)) { |
| 5044 | // Ignore lifetime for further calculation. |
| 5045 | lhq.removeObjCLifetime(); |
| 5046 | rhq.removeObjCLifetime(); |
| 5047 | } |
| 5048 | |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5049 | if (!lhq.compatiblyIncludes(rhq)) { |
| 5050 | // Treat address-space mismatches as fatal. TODO: address subspaces |
| 5051 | if (lhq.getAddressSpace() != rhq.getAddressSpace()) |
| 5052 | ConvTy = Sema::IncompatiblePointerDiscardsQualifiers; |
| 5053 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5054 | // 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] | 5055 | // and from void*. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5056 | else if (lhq.withoutObjCGCAttr().withoutObjCGLifetime() |
| 5057 | .compatiblyIncludes( |
| 5058 | rhq.withoutObjCGCAttr().withoutObjCGLifetime()) |
John McCall | 7853595 | 2011-03-26 02:56:45 +0000 | [diff] [blame] | 5059 | && (lhptee->isVoidType() || rhptee->isVoidType())) |
| 5060 | ; // keep old |
| 5061 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5062 | // Treat lifetime mismatches as fatal. |
| 5063 | else if (lhq.getObjCLifetime() != rhq.getObjCLifetime()) |
| 5064 | ConvTy = Sema::IncompatiblePointerDiscardsQualifiers; |
| 5065 | |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5066 | // For GCC compatibility, other qualifier mismatches are treated |
| 5067 | // as still compatible in C. |
| 5068 | else ConvTy = Sema::CompatiblePointerDiscardsQualifiers; |
| 5069 | } |
Steve Naroff | 3f59729 | 2007-05-11 22:18:03 +0000 | [diff] [blame] | 5070 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5071 | // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or |
| 5072 | // 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] | 5073 | // version of void... |
Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5074 | if (lhptee->isVoidType()) { |
Chris Lattner | b3a176d | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5075 | if (rhptee->isIncompleteOrObjectType()) |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5076 | return ConvTy; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5077 | |
Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5078 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | b3a176d | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5079 | assert(rhptee->isFunctionType()); |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5080 | return Sema::FunctionVoidPointer; |
Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5081 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5082 | |
Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5083 | if (rhptee->isVoidType()) { |
Chris Lattner | b3a176d | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5084 | if (lhptee->isIncompleteOrObjectType()) |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5085 | return ConvTy; |
Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5086 | |
| 5087 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | b3a176d | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5088 | assert(lhptee->isFunctionType()); |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5089 | return Sema::FunctionVoidPointer; |
Chris Lattner | 0a78843 | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5090 | } |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5091 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5092 | // 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] | 5093 | // unqualified versions of compatible types, ... |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5094 | QualType ltrans = QualType(lhptee, 0), rtrans = QualType(rhptee, 0); |
| 5095 | if (!S.Context.typesAreCompatible(ltrans, rtrans)) { |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5096 | // Check if the pointee types are compatible ignoring the sign. |
| 5097 | // We explicitly check for char so that we catch "char" vs |
| 5098 | // "unsigned char" on systems where "char" is unsigned. |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5099 | if (lhptee->isCharType()) |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5100 | ltrans = S.Context.UnsignedCharTy; |
Douglas Gregor | 5cc2c8b | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 5101 | else if (lhptee->hasSignedIntegerRepresentation()) |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5102 | ltrans = S.Context.getCorrespondingUnsignedType(ltrans); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5103 | |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5104 | if (rhptee->isCharType()) |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5105 | rtrans = S.Context.UnsignedCharTy; |
Douglas Gregor | 5cc2c8b | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 5106 | else if (rhptee->hasSignedIntegerRepresentation()) |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5107 | rtrans = S.Context.getCorrespondingUnsignedType(rtrans); |
Chris Lattner | ec3a156 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5108 | |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5109 | if (ltrans == rtrans) { |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5110 | // Types are compatible ignoring the sign. Qualifier incompatibility |
| 5111 | // takes priority over sign incompatibility because the sign |
| 5112 | // warning can be disabled. |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5113 | if (ConvTy != Sema::Compatible) |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5114 | return ConvTy; |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5115 | |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5116 | return Sema::IncompatiblePointerSign; |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5117 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5118 | |
Fariborz Jahanian | d7aa9d8 | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 5119 | // If we are a multi-level pointer, it's possible that our issue is simply |
| 5120 | // one of qualification - e.g. char ** -> const char ** is not allowed. If |
| 5121 | // the eventual target type is the same and the pointers have the same |
| 5122 | // level of indirection, this must be the issue. |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5123 | if (isa<PointerType>(lhptee) && isa<PointerType>(rhptee)) { |
Fariborz Jahanian | d7aa9d8 | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 5124 | do { |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5125 | lhptee = cast<PointerType>(lhptee)->getPointeeType().getTypePtr(); |
| 5126 | rhptee = cast<PointerType>(rhptee)->getPointeeType().getTypePtr(); |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5127 | } while (isa<PointerType>(lhptee) && isa<PointerType>(rhptee)); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5128 | |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5129 | if (lhptee == rhptee) |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5130 | return Sema::IncompatibleNestedPointerQualifiers; |
Fariborz Jahanian | d7aa9d8 | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 5131 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5132 | |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5133 | // General pointer incompatibility takes priority over qualifiers. |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5134 | return Sema::IncompatiblePointer; |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5135 | } |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5136 | return ConvTy; |
Steve Naroff | 1f4d727 | 2007-05-11 04:00:31 +0000 | [diff] [blame] | 5137 | } |
| 5138 | |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5139 | /// checkBlockPointerTypesForAssignment - This routine determines whether two |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5140 | /// block pointer types are compatible or whether a block and normal pointer |
| 5141 | /// are compatible. It is more restrict than comparing two function pointer |
| 5142 | // types. |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5143 | static Sema::AssignConvertType |
| 5144 | checkBlockPointerTypesForAssignment(Sema &S, QualType lhsType, |
| 5145 | QualType rhsType) { |
| 5146 | assert(lhsType.isCanonical() && "LHS not canonicalized!"); |
| 5147 | assert(rhsType.isCanonical() && "RHS not canonicalized!"); |
| 5148 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5149 | QualType lhptee, rhptee; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5150 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5151 | // get the "pointed to" type (ignoring qualifiers at the top level) |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5152 | lhptee = cast<BlockPointerType>(lhsType)->getPointeeType(); |
| 5153 | rhptee = cast<BlockPointerType>(rhsType)->getPointeeType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5154 | |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5155 | // In C++, the types have to match exactly. |
| 5156 | if (S.getLangOptions().CPlusPlus) |
| 5157 | return Sema::IncompatibleBlockPointer; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5158 | |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5159 | Sema::AssignConvertType ConvTy = Sema::Compatible; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5160 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5161 | // For blocks we enforce that qualifiers are identical. |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5162 | if (lhptee.getLocalQualifiers() != rhptee.getLocalQualifiers()) |
| 5163 | ConvTy = Sema::CompatiblePointerDiscardsQualifiers; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5164 | |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5165 | if (!S.Context.typesAreBlockPointerCompatible(lhsType, rhsType)) |
| 5166 | return Sema::IncompatibleBlockPointer; |
| 5167 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5168 | return ConvTy; |
| 5169 | } |
| 5170 | |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5171 | /// checkObjCPointerTypesForAssignment - Compares two objective-c pointer types |
Fariborz Jahanian | 410f2eb | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5172 | /// for assignment compatibility. |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5173 | static Sema::AssignConvertType |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5174 | checkObjCPointerTypesForAssignment(Sema &S, QualType lhsType, |
| 5175 | QualType rhsType) { |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5176 | assert(lhsType.isCanonical() && "LHS was not canonicalized!"); |
| 5177 | assert(rhsType.isCanonical() && "RHS was not canonicalized!"); |
| 5178 | |
Fariborz Jahanian | d5bb8cb | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 5179 | if (lhsType->isObjCBuiltinType()) { |
| 5180 | // Class is not compatible with ObjC object pointers. |
Fariborz Jahanian | 9b37b1d | 2010-03-24 21:00:27 +0000 | [diff] [blame] | 5181 | if (lhsType->isObjCClassType() && !rhsType->isObjCBuiltinType() && |
| 5182 | !rhsType->isObjCQualifiedClassType()) |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5183 | return Sema::IncompatiblePointer; |
| 5184 | return Sema::Compatible; |
Fariborz Jahanian | d5bb8cb | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 5185 | } |
| 5186 | if (rhsType->isObjCBuiltinType()) { |
| 5187 | // Class is not compatible with ObjC object pointers. |
Fariborz Jahanian | 9b37b1d | 2010-03-24 21:00:27 +0000 | [diff] [blame] | 5188 | if (rhsType->isObjCClassType() && !lhsType->isObjCBuiltinType() && |
| 5189 | !lhsType->isObjCQualifiedClassType()) |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5190 | return Sema::IncompatiblePointer; |
| 5191 | return Sema::Compatible; |
Fariborz Jahanian | d5bb8cb | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 5192 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5193 | QualType lhptee = |
Fariborz Jahanian | 410f2eb | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5194 | lhsType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5195 | QualType rhptee = |
Fariborz Jahanian | 410f2eb | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5196 | rhsType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5197 | |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5198 | if (!lhptee.isAtLeastAsQualifiedAs(rhptee)) |
| 5199 | return Sema::CompatiblePointerDiscardsQualifiers; |
| 5200 | |
| 5201 | if (S.Context.typesAreCompatible(lhsType, rhsType)) |
| 5202 | return Sema::Compatible; |
Fariborz Jahanian | 410f2eb | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5203 | if (lhsType->isObjCQualifiedIdType() || rhsType->isObjCQualifiedIdType()) |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5204 | return Sema::IncompatibleObjCQualifiedId; |
| 5205 | return Sema::IncompatiblePointer; |
Fariborz Jahanian | 410f2eb | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5206 | } |
| 5207 | |
John McCall | 29600e1 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5208 | Sema::AssignConvertType |
Douglas Gregor | c03a108 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 5209 | Sema::CheckAssignmentConstraints(SourceLocation Loc, |
| 5210 | QualType lhsType, QualType rhsType) { |
John McCall | 29600e1 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5211 | // Fake up an opaque expression. We don't actually care about what |
| 5212 | // cast operations are required, so if CheckAssignmentConstraints |
| 5213 | // adds casts to this they'll be wasted, but fortunately that doesn't |
| 5214 | // usually happen on valid code. |
Douglas Gregor | c03a108 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 5215 | OpaqueValueExpr rhs(Loc, rhsType, VK_RValue); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5216 | ExprResult rhsPtr = &rhs; |
John McCall | 29600e1 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5217 | CastKind K = CK_Invalid; |
| 5218 | |
| 5219 | return CheckAssignmentConstraints(lhsType, rhsPtr, K); |
| 5220 | } |
| 5221 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5222 | /// CheckAssignmentConstraints (C99 6.5.16) - This routine currently |
| 5223 | /// has code to accommodate several GCC extensions when type checking |
Steve Naroff | 17f76e0 | 2007-05-03 21:03:48 +0000 | [diff] [blame] | 5224 | /// pointers. Here are some objectionable examples that GCC considers warnings: |
| 5225 | /// |
| 5226 | /// int a, *pint; |
| 5227 | /// short *pshort; |
| 5228 | /// struct foo *pfoo; |
| 5229 | /// |
| 5230 | /// pint = pshort; // warning: assignment from incompatible pointer type |
| 5231 | /// a = pint; // warning: assignment makes integer from pointer without a cast |
| 5232 | /// pint = a; // warning: assignment makes pointer from integer without a cast |
| 5233 | /// pint = pfoo; // warning: assignment from incompatible pointer type |
| 5234 | /// |
| 5235 | /// 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] | 5236 | /// C99 spec dictates. |
Steve Naroff | 17f76e0 | 2007-05-03 21:03:48 +0000 | [diff] [blame] | 5237 | /// |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5238 | /// Sets 'Kind' for any result kind except Incompatible. |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5239 | Sema::AssignConvertType |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5240 | Sema::CheckAssignmentConstraints(QualType lhsType, ExprResult &rhs, |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5241 | CastKind &Kind) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5242 | QualType rhsType = rhs.get()->getType(); |
Fariborz Jahanian | 6f472e8 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 5243 | QualType origLhsType = lhsType; |
John McCall | 29600e1 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5244 | |
Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5245 | // Get canonical types. We're not formatting these types, just comparing |
| 5246 | // them. |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 5247 | lhsType = Context.getCanonicalType(lhsType).getUnqualifiedType(); |
| 5248 | rhsType = Context.getCanonicalType(rhsType).getUnqualifiedType(); |
Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5249 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5250 | // Common case: no conversion required. |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5251 | if (lhsType == rhsType) { |
| 5252 | Kind = CK_NoOp; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5253 | return Compatible; |
David Chisnall | 9f57c29 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 5254 | } |
| 5255 | |
Douglas Gregor | 6b75484 | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 5256 | // If the left-hand side is a reference type, then we are in a |
| 5257 | // (rare!) case where we've allowed the use of references in C, |
| 5258 | // e.g., as a parameter type in a built-in function. In this case, |
| 5259 | // just make sure that the type referenced is compatible with the |
| 5260 | // right-hand side type. The caller is responsible for adjusting |
| 5261 | // lhsType so that the resulting expression does not have reference |
| 5262 | // type. |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5263 | if (const ReferenceType *lhsTypeRef = lhsType->getAs<ReferenceType>()) { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5264 | if (Context.typesAreCompatible(lhsTypeRef->getPointeeType(), rhsType)) { |
| 5265 | Kind = CK_LValueBitCast; |
Anders Carlsson | 24ebce6 | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 5266 | return Compatible; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5267 | } |
Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5268 | return Incompatible; |
Fariborz Jahanian | a1e3420 | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 5269 | } |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5270 | |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5271 | // Allow scalar to ExtVector assignments, and assignments of an ExtVector type |
| 5272 | // to the same ExtVector type. |
| 5273 | if (lhsType->isExtVectorType()) { |
| 5274 | if (rhsType->isExtVectorType()) |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5275 | return Incompatible; |
| 5276 | if (rhsType->isArithmeticType()) { |
John McCall | 29600e1 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5277 | // CK_VectorSplat does T -> vector T, so first cast to the |
| 5278 | // element type. |
| 5279 | QualType elType = cast<ExtVectorType>(lhsType)->getElementType(); |
| 5280 | if (elType != rhsType) { |
| 5281 | Kind = PrepareScalarCast(*this, rhs, elType); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5282 | rhs = ImpCastExprToType(rhs.take(), elType, Kind); |
John McCall | 29600e1 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5283 | } |
| 5284 | Kind = CK_VectorSplat; |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5285 | return Compatible; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5286 | } |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5287 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5288 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5289 | // Conversions to or from vector type. |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5290 | if (lhsType->isVectorType() || rhsType->isVectorType()) { |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5291 | if (lhsType->isVectorType() && rhsType->isVectorType()) { |
Bob Wilson | 01856f3 | 2010-12-02 00:25:15 +0000 | [diff] [blame] | 5292 | // Allow assignments of an AltiVec vector type to an equivalent GCC |
| 5293 | // vector type and vice versa |
| 5294 | if (Context.areCompatibleVectorTypes(lhsType, rhsType)) { |
| 5295 | Kind = CK_BitCast; |
| 5296 | return Compatible; |
| 5297 | } |
| 5298 | |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5299 | // If we are allowing lax vector conversions, and LHS and RHS are both |
| 5300 | // vectors, the total size only needs to be the same. This is a bitcast; |
| 5301 | // no bits are changed but the result type is different. |
| 5302 | if (getLangOptions().LaxVectorConversions && |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5303 | (Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType))) { |
John McCall | 3065d04 | 2010-11-15 10:08:00 +0000 | [diff] [blame] | 5304 | Kind = CK_BitCast; |
Anders Carlsson | db5a9b6 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 5305 | return IncompatibleVectors; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5306 | } |
Chris Lattner | 881a212 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 5307 | } |
| 5308 | return Incompatible; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5309 | } |
Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5310 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5311 | // Arithmetic conversions. |
Douglas Gregor | bea453a | 2010-05-23 21:53:47 +0000 | [diff] [blame] | 5312 | if (lhsType->isArithmeticType() && rhsType->isArithmeticType() && |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5313 | !(getLangOptions().CPlusPlus && lhsType->isEnumeralType())) { |
John McCall | 29600e1 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5314 | Kind = PrepareScalarCast(*this, rhs, lhsType); |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 5315 | return Compatible; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5316 | } |
Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5317 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5318 | // Conversions to normal pointers. |
| 5319 | if (const PointerType *lhsPointer = dyn_cast<PointerType>(lhsType)) { |
| 5320 | // U* -> T* |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5321 | if (isa<PointerType>(rhsType)) { |
| 5322 | Kind = CK_BitCast; |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5323 | return checkPointerTypesForAssignment(*this, lhsType, rhsType); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5324 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5325 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5326 | // int -> T* |
| 5327 | if (rhsType->isIntegerType()) { |
| 5328 | Kind = CK_IntegralToPointer; // FIXME: null? |
| 5329 | return IntToPointer; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5330 | } |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5331 | |
| 5332 | // C pointers are not compatible with ObjC object pointers, |
| 5333 | // with two exceptions: |
| 5334 | if (isa<ObjCObjectPointerType>(rhsType)) { |
| 5335 | // - conversions to void* |
| 5336 | if (lhsPointer->getPointeeType()->isVoidType()) { |
| 5337 | Kind = CK_AnyPointerToObjCPointerCast; |
| 5338 | return Compatible; |
| 5339 | } |
| 5340 | |
| 5341 | // - conversions from 'Class' to the redefinition type |
| 5342 | if (rhsType->isObjCClassType() && |
| 5343 | Context.hasSameType(lhsType, Context.ObjCClassRedefinitionType)) { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5344 | Kind = CK_BitCast; |
Douglas Gregor | e7dd145 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 5345 | return Compatible; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5346 | } |
Steve Naroff | 32d072c | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5347 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5348 | Kind = CK_BitCast; |
| 5349 | return IncompatiblePointer; |
| 5350 | } |
| 5351 | |
| 5352 | // U^ -> void* |
| 5353 | if (rhsType->getAs<BlockPointerType>()) { |
| 5354 | if (lhsPointer->getPointeeType()->isVoidType()) { |
| 5355 | Kind = CK_BitCast; |
Steve Naroff | 32d072c | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5356 | return Compatible; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5357 | } |
Steve Naroff | 32d072c | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5358 | } |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5359 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5360 | return Incompatible; |
| 5361 | } |
| 5362 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5363 | // Conversions to block pointers. |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5364 | if (isa<BlockPointerType>(lhsType)) { |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5365 | // U^ -> T^ |
| 5366 | if (rhsType->isBlockPointerType()) { |
| 5367 | Kind = CK_AnyPointerToBlockPointerCast; |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5368 | return checkBlockPointerTypesForAssignment(*this, lhsType, rhsType); |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5369 | } |
| 5370 | |
| 5371 | // int or null -> T^ |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5372 | if (rhsType->isIntegerType()) { |
| 5373 | Kind = CK_IntegralToPointer; // FIXME: null |
Eli Friedman | 8163b7a | 2009-02-25 04:20:42 +0000 | [diff] [blame] | 5374 | return IntToBlockPointer; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5375 | } |
| 5376 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5377 | // id -> T^ |
| 5378 | if (getLangOptions().ObjC1 && rhsType->isObjCIdType()) { |
| 5379 | Kind = CK_AnyPointerToBlockPointerCast; |
Steve Naroff | 32d072c | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5380 | return Compatible; |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5381 | } |
Steve Naroff | 32d072c | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5382 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5383 | // void* -> T^ |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5384 | if (const PointerType *RHSPT = rhsType->getAs<PointerType>()) |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5385 | if (RHSPT->getPointeeType()->isVoidType()) { |
| 5386 | Kind = CK_AnyPointerToBlockPointerCast; |
Douglas Gregor | e7dd145 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 5387 | return Compatible; |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5388 | } |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5389 | |
Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5390 | return Incompatible; |
| 5391 | } |
| 5392 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5393 | // Conversions to Objective-C pointers. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5394 | if (isa<ObjCObjectPointerType>(lhsType)) { |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5395 | // A* -> B* |
| 5396 | if (rhsType->isObjCObjectPointerType()) { |
| 5397 | Kind = CK_BitCast; |
Fariborz Jahanian | 6f472e8 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 5398 | Sema::AssignConvertType result = |
| 5399 | checkObjCPointerTypesForAssignment(*this, lhsType, rhsType); |
| 5400 | if (getLangOptions().ObjCAutoRefCount && |
| 5401 | result == Compatible && |
Fariborz Jahanian | 7fcce68 | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 5402 | !CheckObjCARCUnavailableWeakConversion(origLhsType, rhsType)) |
| 5403 | result = IncompatibleObjCWeakRef; |
Fariborz Jahanian | 6f472e8 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 5404 | return result; |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5405 | } |
| 5406 | |
| 5407 | // int or null -> A* |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5408 | if (rhsType->isIntegerType()) { |
| 5409 | Kind = CK_IntegralToPointer; // FIXME: null |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5410 | return IntToPointer; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5411 | } |
| 5412 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5413 | // In general, C pointers are not compatible with ObjC object pointers, |
| 5414 | // with two exceptions: |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5415 | if (isa<PointerType>(rhsType)) { |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5416 | // - conversions from 'void*' |
| 5417 | if (rhsType->isVoidPointerType()) { |
| 5418 | Kind = CK_AnyPointerToObjCPointerCast; |
Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 5419 | return Compatible; |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5420 | } |
| 5421 | |
| 5422 | // - conversions to 'Class' from its redefinition type |
| 5423 | if (lhsType->isObjCClassType() && |
| 5424 | Context.hasSameType(rhsType, Context.ObjCClassRedefinitionType)) { |
| 5425 | Kind = CK_BitCast; |
| 5426 | return Compatible; |
| 5427 | } |
| 5428 | |
| 5429 | Kind = CK_AnyPointerToObjCPointerCast; |
Steve Naroff | accc488 | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 5430 | return IncompatiblePointer; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5431 | } |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5432 | |
| 5433 | // T^ -> A* |
| 5434 | if (rhsType->isBlockPointerType()) { |
| 5435 | Kind = CK_AnyPointerToObjCPointerCast; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5436 | return Compatible; |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5437 | } |
| 5438 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5439 | return Incompatible; |
| 5440 | } |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5441 | |
| 5442 | // Conversions from pointers that are not covered by the above. |
Chris Lattner | ec64683 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 5443 | if (isa<PointerType>(rhsType)) { |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5444 | // T* -> _Bool |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5445 | if (lhsType == Context.BoolTy) { |
| 5446 | Kind = CK_PointerToBoolean; |
Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5447 | return Compatible; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5448 | } |
Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5449 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5450 | // T* -> int |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5451 | if (lhsType->isIntegerType()) { |
| 5452 | Kind = CK_PointerToIntegral; |
Chris Lattner | 940cfeb | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 5453 | return PointerToInt; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5454 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5455 | |
Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5456 | return Incompatible; |
Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5457 | } |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5458 | |
| 5459 | // Conversions from Objective-C pointers that are not covered by the above. |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5460 | if (isa<ObjCObjectPointerType>(rhsType)) { |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5461 | // T* -> _Bool |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5462 | if (lhsType == Context.BoolTy) { |
| 5463 | Kind = CK_PointerToBoolean; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5464 | return Compatible; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5465 | } |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5466 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5467 | // T* -> int |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5468 | if (lhsType->isIntegerType()) { |
| 5469 | Kind = CK_PointerToIntegral; |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5470 | return PointerToInt; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5471 | } |
| 5472 | |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5473 | return Incompatible; |
| 5474 | } |
Eli Friedman | 3360d89 | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5475 | |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5476 | // struct A -> struct B |
Chris Lattner | a52c2f2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5477 | if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5478 | if (Context.typesAreCompatible(lhsType, rhsType)) { |
| 5479 | Kind = CK_NoOp; |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 5480 | return Compatible; |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5481 | } |
Bill Wendling | 216423b | 2007-05-30 06:30:29 +0000 | [diff] [blame] | 5482 | } |
John McCall | e525593 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5483 | |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 5484 | return Incompatible; |
Steve Naroff | 9eb2465 | 2007-05-02 21:58:15 +0000 | [diff] [blame] | 5485 | } |
| 5486 | |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5487 | /// \brief Constructs a transparent union from an expression that is |
| 5488 | /// used to initialize the transparent union. |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5489 | static void ConstructTransparentUnion(Sema &S, ASTContext &C, |
| 5490 | ExprResult &EResult, QualType UnionType, |
| 5491 | FieldDecl *Field) { |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5492 | // Build an initializer list that designates the appropriate member |
| 5493 | // of the transparent union. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5494 | Expr *E = EResult.take(); |
Ted Kremenek | ac03461 | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 5495 | InitListExpr *Initializer = new (C) InitListExpr(C, SourceLocation(), |
Ted Kremenek | 013041e | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 5496 | &E, 1, |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5497 | SourceLocation()); |
| 5498 | Initializer->setType(UnionType); |
| 5499 | Initializer->setInitializedFieldInUnion(Field); |
| 5500 | |
| 5501 | // Build a compound literal constructing a value of the transparent |
| 5502 | // union type from this initializer list. |
John McCall | e15bbff | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5503 | TypeSourceInfo *unionTInfo = C.getTrivialTypeSourceInfo(UnionType); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5504 | EResult = S.Owned( |
| 5505 | new (C) CompoundLiteralExpr(SourceLocation(), unionTInfo, UnionType, |
| 5506 | VK_RValue, Initializer, false)); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5507 | } |
| 5508 | |
| 5509 | Sema::AssignConvertType |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5510 | Sema::CheckTransparentUnionArgumentConstraints(QualType ArgType, |
| 5511 | ExprResult &rExpr) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5512 | QualType FromType = rExpr.get()->getType(); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5513 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5514 | // 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] | 5515 | // transparent_union GCC extension. |
| 5516 | const RecordType *UT = ArgType->getAsUnionType(); |
Argyrios Kyrtzidis | b4b64ca | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 5517 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5518 | return Incompatible; |
| 5519 | |
| 5520 | // The field to initialize within the transparent union. |
| 5521 | RecordDecl *UD = UT->getDecl(); |
| 5522 | FieldDecl *InitField = 0; |
| 5523 | // It's compatible if the expression matches any of the fields. |
Argyrios Kyrtzidis | cfbfe78 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5524 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 5525 | itend = UD->field_end(); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5526 | it != itend; ++it) { |
| 5527 | if (it->getType()->isPointerType()) { |
| 5528 | // If the transparent union contains a pointer type, we allow: |
| 5529 | // 1) void pointer |
| 5530 | // 2) null pointer constant |
| 5531 | if (FromType->isPointerType()) |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5532 | if (FromType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5533 | rExpr = ImpCastExprToType(rExpr.take(), it->getType(), CK_BitCast); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5534 | InitField = *it; |
| 5535 | break; |
| 5536 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5537 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5538 | if (rExpr.get()->isNullPointerConstant(Context, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 5539 | Expr::NPC_ValueDependentIsNull)) { |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5540 | rExpr = ImpCastExprToType(rExpr.take(), it->getType(), |
| 5541 | CK_NullToPointer); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5542 | InitField = *it; |
| 5543 | break; |
| 5544 | } |
| 5545 | } |
| 5546 | |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5547 | CastKind Kind = CK_Invalid; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5548 | if (CheckAssignmentConstraints(it->getType(), rExpr, Kind) |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5549 | == Compatible) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5550 | rExpr = ImpCastExprToType(rExpr.take(), it->getType(), Kind); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5551 | InitField = *it; |
| 5552 | break; |
| 5553 | } |
| 5554 | } |
| 5555 | |
| 5556 | if (!InitField) |
| 5557 | return Incompatible; |
| 5558 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5559 | ConstructTransparentUnion(*this, Context, rExpr, ArgType, InitField); |
Douglas Gregor | 0cfbdab | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5560 | return Compatible; |
| 5561 | } |
| 5562 | |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5563 | Sema::AssignConvertType |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5564 | Sema::CheckSingleAssignmentConstraints(QualType lhsType, ExprResult &rExpr) { |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5565 | if (getLangOptions().CPlusPlus) { |
| 5566 | if (!lhsType->isRecordType()) { |
| 5567 | // C++ 5.17p3: If the left operand is not of class type, the |
| 5568 | // expression is implicitly converted (C++ 4) to the |
| 5569 | // cv-unqualified type of the left operand. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5570 | ExprResult Res = PerformImplicitConversion(rExpr.get(), |
| 5571 | lhsType.getUnqualifiedType(), |
| 5572 | AA_Assigning); |
| 5573 | if (Res.isInvalid()) |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5574 | return Incompatible; |
Fariborz Jahanian | 7fcce68 | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 5575 | Sema::AssignConvertType result = Compatible; |
| 5576 | if (getLangOptions().ObjCAutoRefCount && |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5577 | !CheckObjCARCUnavailableWeakConversion(lhsType, |
| 5578 | rExpr.get()->getType())) |
Fariborz Jahanian | 7fcce68 | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 5579 | result = IncompatibleObjCWeakRef; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5580 | rExpr = move(Res); |
Fariborz Jahanian | 7fcce68 | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 5581 | return result; |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5582 | } |
| 5583 | |
| 5584 | // FIXME: Currently, we fall through and treat C++ classes like C |
| 5585 | // structures. |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 5586 | } |
Douglas Gregor | 9a65793 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5587 | |
Steve Naroff | 0ee0b0a | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 5588 | // C99 6.5.16.1p1: the left operand is a pointer and the right is |
| 5589 | // a null pointer constant. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5590 | if ((lhsType->isPointerType() || |
| 5591 | lhsType->isObjCObjectPointerType() || |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5592 | lhsType->isBlockPointerType()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5593 | && rExpr.get()->isNullPointerConstant(Context, |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5594 | Expr::NPC_ValueDependentIsNull)) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5595 | rExpr = ImpCastExprToType(rExpr.take(), lhsType, CK_NullToPointer); |
Steve Naroff | 0ee0b0a | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 5596 | return Compatible; |
| 5597 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5598 | |
Chris Lattner | e6dcd50 | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 5599 | // This check seems unnatural, however it is necessary to ensure the proper |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 5600 | // conversion of functions/arrays. If the conversion were done for all |
Douglas Gregor | a121b75 | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 5601 | // DeclExpr's (created by ActOnIdExpression), it would mess up the unary |
Nick Lewycky | ef7c0ff | 2010-08-05 06:27:49 +0000 | [diff] [blame] | 5602 | // expressions that suppress this implicit conversion (&, sizeof). |
Chris Lattner | e6dcd50 | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 5603 | // |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5604 | // Suppress this for references: C++ 8.5.3p5. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5605 | if (!lhsType->isReferenceType()) { |
| 5606 | rExpr = DefaultFunctionArrayLvalueConversion(rExpr.take()); |
| 5607 | if (rExpr.isInvalid()) |
| 5608 | return Incompatible; |
| 5609 | } |
Steve Naroff | 0c1c7ed | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 5610 | |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5611 | CastKind Kind = CK_Invalid; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5612 | Sema::AssignConvertType result = |
John McCall | 29600e1 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5613 | CheckAssignmentConstraints(lhsType, rExpr, Kind); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5614 | |
Steve Naroff | 0c1c7ed | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 5615 | // C99 6.5.16.1p2: The value of the right operand is converted to the |
| 5616 | // type of the assignment expression. |
Douglas Gregor | 6b75484 | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 5617 | // CheckAssignmentConstraints allows the left-hand side to be a reference, |
| 5618 | // so that we can use references in built-in functions even in C. |
| 5619 | // The getNonReferenceType() call makes sure that the resulting expression |
| 5620 | // does not have reference type. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5621 | if (result != Incompatible && rExpr.get()->getType() != lhsType) |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5622 | rExpr = ImpCastExprToType(rExpr.take(), |
| 5623 | lhsType.getNonLValueExprType(Context), Kind); |
Steve Naroff | 0c1c7ed | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 5624 | return result; |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 5625 | } |
| 5626 | |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5627 | QualType Sema::InvalidOperands(SourceLocation Loc, ExprResult &lex, |
| 5628 | ExprResult &rex) { |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 5629 | Diag(Loc, diag::err_typecheck_invalid_operands) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5630 | << lex.get()->getType() << rex.get()->getType() |
| 5631 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Chris Lattner | 5c11c41 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 5632 | return QualType(); |
Steve Naroff | 6f49f5d | 2007-05-29 14:23:36 +0000 | [diff] [blame] | 5633 | } |
| 5634 | |
Eli Friedman | 1408bc9 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5635 | QualType Sema::CheckVectorOperands(ExprResult &lex, ExprResult &rex, |
| 5636 | SourceLocation Loc, bool isCompAssign) { |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5637 | // For conversion purposes, we ignore any qualifiers. |
Nate Begeman | 002e4bd | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 5638 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 5639 | QualType lhsType = |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5640 | Context.getCanonicalType(lex.get()->getType()).getUnqualifiedType(); |
Chris Lattner | 574dee6 | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 5641 | QualType rhsType = |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5642 | Context.getCanonicalType(rex.get()->getType()).getUnqualifiedType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5643 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5644 | // If the vector types are identical, return. |
Nate Begeman | 002e4bd | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 5645 | if (lhsType == rhsType) |
Steve Naroff | 84ff4b4 | 2007-07-09 21:31:10 +0000 | [diff] [blame] | 5646 | return lhsType; |
Nate Begeman | 330aaa7 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 5647 | |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5648 | // Handle the case of equivalent AltiVec and GCC vector types |
| 5649 | if (lhsType->isVectorType() && rhsType->isVectorType() && |
| 5650 | Context.areCompatibleVectorTypes(lhsType, rhsType)) { |
Eli Friedman | 1408bc9 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5651 | if (lhsType->isExtVectorType()) { |
| 5652 | rex = ImpCastExprToType(rex.take(), lhsType, CK_BitCast); |
| 5653 | return lhsType; |
| 5654 | } |
| 5655 | |
| 5656 | if (!isCompAssign) |
| 5657 | lex = ImpCastExprToType(lex.take(), rhsType, CK_BitCast); |
Douglas Gregor | 59e8b3b | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5658 | return rhsType; |
| 5659 | } |
| 5660 | |
Eli Friedman | 1408bc9 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5661 | if (getLangOptions().LaxVectorConversions && |
| 5662 | Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType)) { |
| 5663 | // If we are allowing lax vector conversions, and LHS and RHS are both |
| 5664 | // vectors, the total size only needs to be the same. This is a |
| 5665 | // bitcast; no bits are changed but the result type is different. |
| 5666 | // FIXME: Should we really be allowing this? |
| 5667 | rex = ImpCastExprToType(rex.take(), lhsType, CK_BitCast); |
| 5668 | return lhsType; |
| 5669 | } |
| 5670 | |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5671 | // Canonicalize the ExtVector to the LHS, remember if we swapped so we can |
| 5672 | // swap back (so that we don't reverse the inputs to a subtract, for instance. |
| 5673 | bool swapped = false; |
Eli Friedman | 1408bc9 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5674 | if (rhsType->isExtVectorType() && !isCompAssign) { |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5675 | swapped = true; |
| 5676 | std::swap(rex, lex); |
| 5677 | std::swap(rhsType, lhsType); |
| 5678 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5679 | |
Nate Begeman | 886448d | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 5680 | // Handle the case of an ext vector and scalar. |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5681 | if (const ExtVectorType *LV = lhsType->getAs<ExtVectorType>()) { |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5682 | QualType EltTy = LV->getElementType(); |
Douglas Gregor | 6972a62 | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 5683 | if (EltTy->isIntegralType(Context) && rhsType->isIntegralType(Context)) { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5684 | int order = Context.getIntegerTypeOrder(EltTy, rhsType); |
| 5685 | if (order > 0) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5686 | rex = ImpCastExprToType(rex.take(), EltTy, CK_IntegralCast); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5687 | if (order >= 0) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5688 | rex = ImpCastExprToType(rex.take(), lhsType, CK_VectorSplat); |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5689 | if (swapped) std::swap(rex, lex); |
| 5690 | return lhsType; |
| 5691 | } |
| 5692 | } |
| 5693 | if (EltTy->isRealFloatingType() && rhsType->isScalarType() && |
| 5694 | rhsType->isRealFloatingType()) { |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5695 | int order = Context.getFloatingTypeOrder(EltTy, rhsType); |
| 5696 | if (order > 0) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5697 | rex = ImpCastExprToType(rex.take(), EltTy, CK_FloatingCast); |
John McCall | 8cb679e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5698 | if (order >= 0) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5699 | rex = ImpCastExprToType(rex.take(), lhsType, CK_VectorSplat); |
Nate Begeman | bd956c4 | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5700 | if (swapped) std::swap(rex, lex); |
| 5701 | return lhsType; |
| 5702 | } |
Nate Begeman | 330aaa7 | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 5703 | } |
| 5704 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5705 | |
Nate Begeman | 886448d | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 5706 | // Vectors of different size or scalar and non-ext-vector are errors. |
Eli Friedman | 1408bc9 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5707 | if (swapped) std::swap(rex, lex); |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 5708 | Diag(Loc, diag::err_typecheck_vector_not_convertable) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5709 | << lex.get()->getType() << rex.get()->getType() |
| 5710 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Steve Naroff | 84ff4b4 | 2007-07-09 21:31:10 +0000 | [diff] [blame] | 5711 | return QualType(); |
Sebastian Redl | 112a9766 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 5712 | } |
| 5713 | |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5714 | QualType Sema::CheckMultiplyDivideOperands(ExprResult &lex, ExprResult &rex, |
| 5715 | SourceLocation Loc, |
| 5716 | bool isCompAssign, bool isDiv) { |
| 5717 | if (lex.get()->getType()->isVectorType() || |
| 5718 | rex.get()->getType()->isVectorType()) |
Eli Friedman | 1408bc9 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5719 | return CheckVectorOperands(lex, rex, Loc, isCompAssign); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5720 | |
Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 5721 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5722 | if (lex.isInvalid() || rex.isInvalid()) |
| 5723 | return QualType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5724 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5725 | if (!lex.get()->getType()->isArithmeticType() || |
| 5726 | !rex.get()->getType()->isArithmeticType()) |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5727 | return InvalidOperands(Loc, lex, rex); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5728 | |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5729 | // Check for division by zero. |
| 5730 | if (isDiv && |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5731 | rex.get()->isNullPointerConstant(Context, |
| 5732 | Expr::NPC_ValueDependentIsNotNull)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5733 | DiagRuntimeBehavior(Loc, rex.get(), PDiag(diag::warn_division_by_zero) |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5734 | << rex.get()->getSourceRange()); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5735 | |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5736 | return compType; |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 5737 | } |
| 5738 | |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5739 | QualType Sema::CheckRemainderOperands( |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5740 | ExprResult &lex, ExprResult &rex, SourceLocation Loc, bool isCompAssign) { |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5741 | if (lex.get()->getType()->isVectorType() || |
| 5742 | rex.get()->getType()->isVectorType()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5743 | if (lex.get()->getType()->hasIntegerRepresentation() && |
| 5744 | rex.get()->getType()->hasIntegerRepresentation()) |
Eli Friedman | 1408bc9 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5745 | return CheckVectorOperands(lex, rex, Loc, isCompAssign); |
Daniel Dunbar | 0d2bfec | 2009-01-05 22:55:36 +0000 | [diff] [blame] | 5746 | return InvalidOperands(Loc, lex, rex); |
| 5747 | } |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 5748 | |
Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 5749 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5750 | if (lex.isInvalid() || rex.isInvalid()) |
| 5751 | return QualType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5752 | |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5753 | if (!lex.get()->getType()->isIntegerType() || |
| 5754 | !rex.get()->getType()->isIntegerType()) |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5755 | return InvalidOperands(Loc, lex, rex); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5756 | |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5757 | // Check for remainder by zero. |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5758 | if (rex.get()->isNullPointerConstant(Context, |
| 5759 | Expr::NPC_ValueDependentIsNotNull)) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5760 | DiagRuntimeBehavior(Loc, rex.get(), PDiag(diag::warn_remainder_by_zero) |
| 5761 | << rex.get()->getSourceRange()); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5762 | |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5763 | return compType; |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 5764 | } |
| 5765 | |
Chandler Carruth | c933221 | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 5766 | /// \brief Diagnose invalid arithmetic on two void pointers. |
| 5767 | static void diagnoseArithmeticOnTwoVoidPointers(Sema &S, SourceLocation Loc, |
| 5768 | Expr *LHS, Expr *RHS) { |
| 5769 | S.Diag(Loc, S.getLangOptions().CPlusPlus |
| 5770 | ? diag::err_typecheck_pointer_arith_void_type |
| 5771 | : diag::ext_gnu_void_ptr) |
| 5772 | << 1 /* two pointers */ << LHS->getSourceRange() << RHS->getSourceRange(); |
| 5773 | } |
| 5774 | |
| 5775 | /// \brief Diagnose invalid arithmetic on a void pointer. |
| 5776 | static void diagnoseArithmeticOnVoidPointer(Sema &S, SourceLocation Loc, |
| 5777 | Expr *Pointer) { |
| 5778 | S.Diag(Loc, S.getLangOptions().CPlusPlus |
| 5779 | ? diag::err_typecheck_pointer_arith_void_type |
| 5780 | : diag::ext_gnu_void_ptr) |
| 5781 | << 0 /* one pointer */ << Pointer->getSourceRange(); |
| 5782 | } |
| 5783 | |
| 5784 | /// \brief Diagnose invalid arithmetic on two function pointers. |
| 5785 | static void diagnoseArithmeticOnTwoFunctionPointers(Sema &S, SourceLocation Loc, |
| 5786 | Expr *LHS, Expr *RHS) { |
| 5787 | assert(LHS->getType()->isAnyPointerType()); |
| 5788 | assert(RHS->getType()->isAnyPointerType()); |
| 5789 | S.Diag(Loc, S.getLangOptions().CPlusPlus |
| 5790 | ? diag::err_typecheck_pointer_arith_function_type |
| 5791 | : diag::ext_gnu_ptr_func_arith) |
| 5792 | << 1 /* two pointers */ << LHS->getType()->getPointeeType() |
| 5793 | // We only show the second type if it differs from the first. |
| 5794 | << (unsigned)!S.Context.hasSameUnqualifiedType(LHS->getType(), |
| 5795 | RHS->getType()) |
| 5796 | << RHS->getType()->getPointeeType() |
| 5797 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 5798 | } |
| 5799 | |
| 5800 | /// \brief Diagnose invalid arithmetic on a function pointer. |
| 5801 | static void diagnoseArithmeticOnFunctionPointer(Sema &S, SourceLocation Loc, |
| 5802 | Expr *Pointer) { |
| 5803 | assert(Pointer->getType()->isAnyPointerType()); |
| 5804 | S.Diag(Loc, S.getLangOptions().CPlusPlus |
| 5805 | ? diag::err_typecheck_pointer_arith_function_type |
| 5806 | : diag::ext_gnu_ptr_func_arith) |
| 5807 | << 0 /* one pointer */ << Pointer->getType()->getPointeeType() |
| 5808 | << 0 /* one pointer, so only one type */ |
| 5809 | << Pointer->getSourceRange(); |
| 5810 | } |
| 5811 | |
| 5812 | /// \brief Check the validity of an arithmetic pointer operand. |
| 5813 | /// |
| 5814 | /// If the operand has pointer type, this code will check for pointer types |
| 5815 | /// which are invalid in arithmetic operations. These will be diagnosed |
| 5816 | /// appropriately, including whether or not the use is supported as an |
| 5817 | /// extension. |
| 5818 | /// |
| 5819 | /// \returns True when the operand is valid to use (even if as an extension). |
| 5820 | static bool checkArithmeticOpPointerOperand(Sema &S, SourceLocation Loc, |
| 5821 | Expr *Operand) { |
| 5822 | if (!Operand->getType()->isAnyPointerType()) return true; |
| 5823 | |
| 5824 | QualType PointeeTy = Operand->getType()->getPointeeType(); |
| 5825 | if (PointeeTy->isVoidType()) { |
| 5826 | diagnoseArithmeticOnVoidPointer(S, Loc, Operand); |
| 5827 | return !S.getLangOptions().CPlusPlus; |
| 5828 | } |
| 5829 | if (PointeeTy->isFunctionType()) { |
| 5830 | diagnoseArithmeticOnFunctionPointer(S, Loc, Operand); |
| 5831 | return !S.getLangOptions().CPlusPlus; |
| 5832 | } |
| 5833 | |
| 5834 | if ((Operand->getType()->isPointerType() && |
| 5835 | !Operand->getType()->isDependentType()) || |
| 5836 | Operand->getType()->isObjCObjectPointerType()) { |
| 5837 | QualType PointeeTy = Operand->getType()->getPointeeType(); |
| 5838 | if (S.RequireCompleteType( |
| 5839 | Loc, PointeeTy, |
| 5840 | S.PDiag(diag::err_typecheck_arithmetic_incomplete_type) |
| 5841 | << PointeeTy << Operand->getSourceRange())) |
| 5842 | return false; |
| 5843 | } |
| 5844 | |
| 5845 | return true; |
| 5846 | } |
| 5847 | |
| 5848 | /// \brief Check the validity of a binary arithmetic operation w.r.t. pointer |
| 5849 | /// operands. |
| 5850 | /// |
| 5851 | /// This routine will diagnose any invalid arithmetic on pointer operands much |
| 5852 | /// like \see checkArithmeticOpPointerOperand. However, it has special logic |
| 5853 | /// for emitting a single diagnostic even for operations where both LHS and RHS |
| 5854 | /// are (potentially problematic) pointers. |
| 5855 | /// |
| 5856 | /// \returns True when the operand is valid to use (even if as an extension). |
| 5857 | static bool checkArithmeticBinOpPointerOperands(Sema &S, SourceLocation Loc, |
| 5858 | Expr *LHS, Expr *RHS) { |
| 5859 | bool isLHSPointer = LHS->getType()->isAnyPointerType(); |
| 5860 | bool isRHSPointer = RHS->getType()->isAnyPointerType(); |
| 5861 | if (!isLHSPointer && !isRHSPointer) return true; |
| 5862 | |
| 5863 | QualType LHSPointeeTy, RHSPointeeTy; |
| 5864 | if (isLHSPointer) LHSPointeeTy = LHS->getType()->getPointeeType(); |
| 5865 | if (isRHSPointer) RHSPointeeTy = RHS->getType()->getPointeeType(); |
| 5866 | |
| 5867 | // Check for arithmetic on pointers to incomplete types. |
| 5868 | bool isLHSVoidPtr = isLHSPointer && LHSPointeeTy->isVoidType(); |
| 5869 | bool isRHSVoidPtr = isRHSPointer && RHSPointeeTy->isVoidType(); |
| 5870 | if (isLHSVoidPtr || isRHSVoidPtr) { |
| 5871 | if (!isRHSVoidPtr) diagnoseArithmeticOnVoidPointer(S, Loc, LHS); |
| 5872 | else if (!isLHSVoidPtr) diagnoseArithmeticOnVoidPointer(S, Loc, RHS); |
| 5873 | else diagnoseArithmeticOnTwoVoidPointers(S, Loc, LHS, RHS); |
| 5874 | |
| 5875 | return !S.getLangOptions().CPlusPlus; |
| 5876 | } |
| 5877 | |
| 5878 | bool isLHSFuncPtr = isLHSPointer && LHSPointeeTy->isFunctionType(); |
| 5879 | bool isRHSFuncPtr = isRHSPointer && RHSPointeeTy->isFunctionType(); |
| 5880 | if (isLHSFuncPtr || isRHSFuncPtr) { |
| 5881 | if (!isRHSFuncPtr) diagnoseArithmeticOnFunctionPointer(S, Loc, LHS); |
| 5882 | else if (!isLHSFuncPtr) diagnoseArithmeticOnFunctionPointer(S, Loc, RHS); |
| 5883 | else diagnoseArithmeticOnTwoFunctionPointers(S, Loc, LHS, RHS); |
| 5884 | |
| 5885 | return !S.getLangOptions().CPlusPlus; |
| 5886 | } |
| 5887 | |
| 5888 | Expr *Operands[] = { LHS, RHS }; |
| 5889 | for (unsigned i = 0; i < 2; ++i) { |
| 5890 | Expr *Operand = Operands[i]; |
| 5891 | if ((Operand->getType()->isPointerType() && |
| 5892 | !Operand->getType()->isDependentType()) || |
| 5893 | Operand->getType()->isObjCObjectPointerType()) { |
| 5894 | QualType PointeeTy = Operand->getType()->getPointeeType(); |
| 5895 | if (S.RequireCompleteType( |
| 5896 | Loc, PointeeTy, |
| 5897 | S.PDiag(diag::err_typecheck_arithmetic_incomplete_type) |
| 5898 | << PointeeTy << Operand->getSourceRange())) |
| 5899 | return false; |
| 5900 | } |
| 5901 | } |
| 5902 | return true; |
| 5903 | } |
| 5904 | |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5905 | QualType Sema::CheckAdditionOperands( // C99 6.5.6 |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5906 | ExprResult &lex, ExprResult &rex, SourceLocation Loc, QualType* CompLHSTy) { |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5907 | if (lex.get()->getType()->isVectorType() || |
| 5908 | rex.get()->getType()->isVectorType()) { |
Eli Friedman | 1408bc9 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5909 | QualType compType = CheckVectorOperands(lex, rex, Loc, CompLHSTy); |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5910 | if (CompLHSTy) *CompLHSTy = compType; |
| 5911 | return compType; |
| 5912 | } |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 5913 | |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5914 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5915 | if (lex.isInvalid() || rex.isInvalid()) |
| 5916 | return QualType(); |
Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 5917 | |
Steve Naroff | e471889 | 2007-04-27 18:30:00 +0000 | [diff] [blame] | 5918 | // handle the common case first (both operands are arithmetic). |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5919 | if (lex.get()->getType()->isArithmeticType() && |
| 5920 | rex.get()->getType()->isArithmeticType()) { |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5921 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 5922 | return compType; |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5923 | } |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 5924 | |
Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 5925 | // Put any potential pointer into PExp |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5926 | Expr* PExp = lex.get(), *IExp = rex.get(); |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 5927 | if (IExp->getType()->isAnyPointerType()) |
Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 5928 | std::swap(PExp, IExp); |
| 5929 | |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 5930 | if (PExp->getType()->isAnyPointerType()) { |
Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 5931 | if (IExp->getType()->isIntegerType()) { |
Chandler Carruth | c933221 | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 5932 | if (!checkArithmeticOpPointerOperand(*this, Loc, PExp)) |
| 5933 | return QualType(); |
| 5934 | |
Steve Naroff | aacd4cc | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 5935 | QualType PointeeTy = PExp->getType()->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5936 | |
Chris Lattner | 12bdebb | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 5937 | // Diagnose bad cases where we step over interface counts. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5938 | if (PointeeTy->isObjCObjectType() && LangOpts.ObjCNonFragileABI) { |
Chris Lattner | 12bdebb | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 5939 | Diag(Loc, diag::err_arithmetic_nonfragile_interface) |
| 5940 | << PointeeTy << PExp->getSourceRange(); |
| 5941 | return QualType(); |
| 5942 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5943 | |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 5944 | // Check array bounds for pointer arithemtic |
| 5945 | CheckArrayAccess(PExp, IExp); |
| 5946 | |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5947 | if (CompLHSTy) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5948 | QualType LHSTy = Context.isPromotableBitField(lex.get()); |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 5949 | if (LHSTy.isNull()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5950 | LHSTy = lex.get()->getType(); |
Eli Friedman | 629ffb9 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 5951 | if (LHSTy->isPromotableIntegerType()) |
| 5952 | LHSTy = Context.getPromotedIntegerType(LHSTy); |
Douglas Gregor | d2c2d17 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 5953 | } |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5954 | *CompLHSTy = LHSTy; |
| 5955 | } |
Eli Friedman | 8e12298 | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 5956 | return PExp->getType(); |
| 5957 | } |
| 5958 | } |
| 5959 | |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5960 | return InvalidOperands(Loc, lex, rex); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 5961 | } |
| 5962 | |
Chris Lattner | 2a3569b | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 5963 | // C99 6.5.6 |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5964 | QualType Sema::CheckSubtractionOperands(ExprResult &lex, ExprResult &rex, |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5965 | SourceLocation Loc, |
| 5966 | QualType* CompLHSTy) { |
| 5967 | if (lex.get()->getType()->isVectorType() || |
| 5968 | rex.get()->getType()->isVectorType()) { |
Eli Friedman | 1408bc9 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5969 | QualType compType = CheckVectorOperands(lex, rex, Loc, CompLHSTy); |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5970 | if (CompLHSTy) *CompLHSTy = compType; |
| 5971 | return compType; |
| 5972 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5973 | |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5974 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5975 | if (lex.isInvalid() || rex.isInvalid()) |
| 5976 | return QualType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5977 | |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5978 | // Enforce type constraints: C99 6.5.6p3. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5979 | |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5980 | // Handle the common case first (both operands are arithmetic). |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5981 | if (lex.get()->getType()->isArithmeticType() && |
| 5982 | rex.get()->getType()->isArithmeticType()) { |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5983 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 5984 | return compType; |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5985 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5986 | |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5987 | // Either ptr - int or ptr - ptr. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5988 | if (lex.get()->getType()->isAnyPointerType()) { |
| 5989 | QualType lpointee = lex.get()->getType()->getPointeeType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5990 | |
Chris Lattner | 12bdebb | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 5991 | // Diagnose bad cases where we step over interface counts. |
John McCall | 8b07ec2 | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5992 | if (lpointee->isObjCObjectType() && LangOpts.ObjCNonFragileABI) { |
Chris Lattner | 12bdebb | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 5993 | Diag(Loc, diag::err_arithmetic_nonfragile_interface) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5994 | << lpointee << lex.get()->getSourceRange(); |
Chris Lattner | 12bdebb | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 5995 | return QualType(); |
| 5996 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5997 | |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5998 | // The result type of a pointer-int computation is the pointer type. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5999 | if (rex.get()->getType()->isIntegerType()) { |
Chandler Carruth | c933221 | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6000 | if (!checkArithmeticOpPointerOperand(*this, Loc, lex.get())) |
| 6001 | return QualType(); |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6002 | |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 6003 | Expr *IExpr = rex.get()->IgnoreParenCasts(); |
| 6004 | UnaryOperator negRex(IExpr, UO_Minus, IExpr->getType(), VK_RValue, |
| 6005 | OK_Ordinary, IExpr->getExprLoc()); |
| 6006 | // Check array bounds for pointer arithemtic |
| 6007 | CheckArrayAccess(lex.get()->IgnoreParenCasts(), &negRex); |
| 6008 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6009 | if (CompLHSTy) *CompLHSTy = lex.get()->getType(); |
| 6010 | return lex.get()->getType(); |
Douglas Gregor | ac1fb65 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6011 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6012 | |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6013 | // Handle pointer-pointer subtractions. |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6014 | if (const PointerType *RHSPTy |
| 6015 | = rex.get()->getType()->getAs<PointerType>()) { |
Eli Friedman | 1974e53 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 6016 | QualType rpointee = RHSPTy->getPointeeType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6017 | |
Eli Friedman | 168fe15 | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 6018 | if (getLangOptions().CPlusPlus) { |
| 6019 | // Pointee types must be the same: C++ [expr.add] |
| 6020 | if (!Context.hasSameUnqualifiedType(lpointee, rpointee)) { |
| 6021 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6022 | << lex.get()->getType() << rex.get()->getType() |
| 6023 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Eli Friedman | 168fe15 | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 6024 | return QualType(); |
| 6025 | } |
| 6026 | } else { |
| 6027 | // Pointee types must be compatible C99 6.5.6p3 |
| 6028 | if (!Context.typesAreCompatible( |
| 6029 | Context.getCanonicalType(lpointee).getUnqualifiedType(), |
| 6030 | Context.getCanonicalType(rpointee).getUnqualifiedType())) { |
| 6031 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6032 | << lex.get()->getType() << rex.get()->getType() |
| 6033 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Eli Friedman | 168fe15 | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 6034 | return QualType(); |
| 6035 | } |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6036 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6037 | |
Chandler Carruth | c933221 | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6038 | if (!checkArithmeticBinOpPointerOperands(*this, Loc, |
| 6039 | lex.get(), rex.get())) |
| 6040 | return QualType(); |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6041 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6042 | if (CompLHSTy) *CompLHSTy = lex.get()->getType(); |
Chris Lattner | 4d62f42 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6043 | return Context.getPointerDiffType(); |
| 6044 | } |
| 6045 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6046 | |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6047 | return InvalidOperands(Loc, lex, rex); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 6048 | } |
| 6049 | |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6050 | static bool isScopedEnumerationType(QualType T) { |
| 6051 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
| 6052 | return ET->getDecl()->isScoped(); |
| 6053 | return false; |
| 6054 | } |
| 6055 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6056 | static void DiagnoseBadShiftValues(Sema& S, ExprResult &lex, ExprResult &rex, |
Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6057 | SourceLocation Loc, unsigned Opc, |
| 6058 | QualType LHSTy) { |
| 6059 | llvm::APSInt Right; |
| 6060 | // Check right/shifter operand |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6061 | if (rex.get()->isValueDependent() || |
| 6062 | !rex.get()->isIntegerConstantExpr(Right, S.Context)) |
Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6063 | return; |
| 6064 | |
| 6065 | if (Right.isNegative()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6066 | S.DiagRuntimeBehavior(Loc, rex.get(), |
Ted Kremenek | 63657fe | 2011-03-01 18:09:31 +0000 | [diff] [blame] | 6067 | S.PDiag(diag::warn_shift_negative) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6068 | << rex.get()->getSourceRange()); |
Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6069 | return; |
| 6070 | } |
| 6071 | llvm::APInt LeftBits(Right.getBitWidth(), |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6072 | S.Context.getTypeSize(lex.get()->getType())); |
Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6073 | if (Right.uge(LeftBits)) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6074 | S.DiagRuntimeBehavior(Loc, rex.get(), |
Ted Kremenek | 26bbc3d | 2011-03-01 19:13:22 +0000 | [diff] [blame] | 6075 | S.PDiag(diag::warn_shift_gt_typewidth) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6076 | << rex.get()->getSourceRange()); |
Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6077 | return; |
| 6078 | } |
| 6079 | if (Opc != BO_Shl) |
| 6080 | return; |
| 6081 | |
| 6082 | // When left shifting an ICE which is signed, we can check for overflow which |
| 6083 | // according to C++ has undefined behavior ([expr.shift] 5.8/2). Unsigned |
| 6084 | // integers have defined behavior modulo one more than the maximum value |
| 6085 | // representable in the result type, so never warn for those. |
| 6086 | llvm::APSInt Left; |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6087 | if (lex.get()->isValueDependent() || |
| 6088 | !lex.get()->isIntegerConstantExpr(Left, S.Context) || |
Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6089 | LHSTy->hasUnsignedIntegerRepresentation()) |
| 6090 | return; |
| 6091 | llvm::APInt ResultBits = |
| 6092 | static_cast<llvm::APInt&>(Right) + Left.getMinSignedBits(); |
| 6093 | if (LeftBits.uge(ResultBits)) |
| 6094 | return; |
| 6095 | llvm::APSInt Result = Left.extend(ResultBits.getLimitedValue()); |
| 6096 | Result = Result.shl(Right); |
| 6097 | |
Ted Kremenek | 70f05fd | 2011-06-15 00:54:52 +0000 | [diff] [blame] | 6098 | // Print the bit representation of the signed integer as an unsigned |
| 6099 | // hexadecimal number. |
| 6100 | llvm::SmallString<40> HexResult; |
| 6101 | Result.toString(HexResult, 16, /*Signed =*/false, /*Literal =*/true); |
| 6102 | |
Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6103 | // If we are only missing a sign bit, this is less likely to result in actual |
| 6104 | // bugs -- if the result is cast back to an unsigned type, it will have the |
| 6105 | // expected value. Thus we place this behind a different warning that can be |
| 6106 | // turned off separately if needed. |
| 6107 | if (LeftBits == ResultBits - 1) { |
Ted Kremenek | 70f05fd | 2011-06-15 00:54:52 +0000 | [diff] [blame] | 6108 | S.Diag(Loc, diag::warn_shift_result_sets_sign_bit) |
| 6109 | << HexResult.str() << LHSTy |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6110 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6111 | return; |
| 6112 | } |
| 6113 | |
| 6114 | S.Diag(Loc, diag::warn_shift_result_gt_typewidth) |
Ted Kremenek | 70f05fd | 2011-06-15 00:54:52 +0000 | [diff] [blame] | 6115 | << HexResult.str() << Result.getMinSignedBits() << LHSTy |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6116 | << Left.getBitWidth() << lex.get()->getSourceRange() |
| 6117 | << rex.get()->getSourceRange(); |
Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6118 | } |
| 6119 | |
Chris Lattner | 2a3569b | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 6120 | // C99 6.5.7 |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6121 | QualType Sema::CheckShiftOperands(ExprResult &lex, ExprResult &rex, |
| 6122 | SourceLocation Loc, unsigned Opc, |
| 6123 | bool isCompAssign) { |
Chris Lattner | 5c11c41 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6124 | // C99 6.5.7p2: Each of the operands shall have integer type. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6125 | if (!lex.get()->getType()->hasIntegerRepresentation() || |
| 6126 | !rex.get()->getType()->hasIntegerRepresentation()) |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6127 | return InvalidOperands(Loc, lex, rex); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6128 | |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6129 | // C++0x: Don't allow scoped enums. FIXME: Use something better than |
| 6130 | // hasIntegerRepresentation() above instead of this. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6131 | if (isScopedEnumerationType(lex.get()->getType()) || |
| 6132 | isScopedEnumerationType(rex.get()->getType())) { |
Douglas Gregor | 0bf3140 | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6133 | return InvalidOperands(Loc, lex, rex); |
| 6134 | } |
| 6135 | |
Nate Begeman | e46ee9a | 2009-10-25 02:26:48 +0000 | [diff] [blame] | 6136 | // Vector shifts promote their scalar inputs to vector type. |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6137 | if (lex.get()->getType()->isVectorType() || |
| 6138 | rex.get()->getType()->isVectorType()) |
Eli Friedman | 1408bc9 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 6139 | return CheckVectorOperands(lex, rex, Loc, isCompAssign); |
Nate Begeman | e46ee9a | 2009-10-25 02:26:48 +0000 | [diff] [blame] | 6140 | |
Chris Lattner | 5c11c41 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6141 | // Shifts don't perform usual arithmetic conversions, they just do integer |
| 6142 | // promotions on each operand. C99 6.5.7p3 |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6143 | |
John McCall | 57cdd88 | 2010-12-16 19:28:59 +0000 | [diff] [blame] | 6144 | // For the LHS, do usual unary conversions, but then reset them away |
| 6145 | // if this is a compound assignment. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6146 | ExprResult old_lex = lex; |
| 6147 | lex = UsualUnaryConversions(lex.take()); |
| 6148 | if (lex.isInvalid()) |
| 6149 | return QualType(); |
| 6150 | QualType LHSTy = lex.get()->getType(); |
John McCall | 57cdd88 | 2010-12-16 19:28:59 +0000 | [diff] [blame] | 6151 | if (isCompAssign) lex = old_lex; |
| 6152 | |
| 6153 | // The RHS is simpler. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6154 | rex = UsualUnaryConversions(rex.take()); |
| 6155 | if (rex.isInvalid()) |
| 6156 | return QualType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6157 | |
Ryan Flynn | f53fab8 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 6158 | // Sanity-check shift operands |
Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6159 | DiagnoseBadShiftValues(*this, lex, rex, Loc, Opc, LHSTy); |
Ryan Flynn | f53fab8 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 6160 | |
Chris Lattner | 5c11c41 | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6161 | // "The type of the result is that of the promoted left operand." |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6162 | return LHSTy; |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 6163 | } |
| 6164 | |
Chandler Carruth | 17773fc | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6165 | static bool IsWithinTemplateSpecialization(Decl *D) { |
| 6166 | if (DeclContext *DC = D->getDeclContext()) { |
| 6167 | if (isa<ClassTemplateSpecializationDecl>(DC)) |
| 6168 | return true; |
| 6169 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) |
| 6170 | return FD->isFunctionTemplateSpecialization(); |
| 6171 | } |
| 6172 | return false; |
| 6173 | } |
| 6174 | |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6175 | // C99 6.5.8, C++ [expr.rel] |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6176 | QualType Sema::CheckCompareOperands(ExprResult &lex, ExprResult &rex, |
| 6177 | SourceLocation Loc, unsigned OpaqueOpc, |
| 6178 | bool isRelational) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6179 | BinaryOperatorKind Opc = (BinaryOperatorKind) OpaqueOpc; |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6180 | |
Chris Lattner | 9a152e2 | 2009-12-05 05:40:13 +0000 | [diff] [blame] | 6181 | // Handle vector comparisons separately. |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6182 | if (lex.get()->getType()->isVectorType() || |
| 6183 | rex.get()->getType()->isVectorType()) |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6184 | return CheckVectorCompareOperands(lex, rex, Loc, isRelational); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6185 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6186 | QualType lType = lex.get()->getType(); |
| 6187 | QualType rType = rex.get()->getType(); |
Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 6188 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6189 | Expr *LHSStripped = lex.get()->IgnoreParenImpCasts(); |
| 6190 | Expr *RHSStripped = rex.get()->IgnoreParenImpCasts(); |
Chandler Carruth | 712563b | 2011-02-17 08:37:06 +0000 | [diff] [blame] | 6191 | QualType LHSStrippedType = LHSStripped->getType(); |
| 6192 | QualType RHSStrippedType = RHSStripped->getType(); |
| 6193 | |
Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 6194 | |
| 6195 | |
Chandler Carruth | 712563b | 2011-02-17 08:37:06 +0000 | [diff] [blame] | 6196 | // Two different enums will raise a warning when compared. |
| 6197 | if (const EnumType *LHSEnumType = LHSStrippedType->getAs<EnumType>()) { |
| 6198 | if (const EnumType *RHSEnumType = RHSStrippedType->getAs<EnumType>()) { |
| 6199 | if (LHSEnumType->getDecl()->getIdentifier() && |
| 6200 | RHSEnumType->getDecl()->getIdentifier() && |
| 6201 | !Context.hasSameUnqualifiedType(LHSStrippedType, RHSStrippedType)) { |
| 6202 | Diag(Loc, diag::warn_comparison_of_mixed_enum_types) |
| 6203 | << LHSStrippedType << RHSStrippedType |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6204 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Chandler Carruth | 712563b | 2011-02-17 08:37:06 +0000 | [diff] [blame] | 6205 | } |
| 6206 | } |
| 6207 | } |
| 6208 | |
Douglas Gregor | 4ffbad1 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 6209 | if (!lType->hasFloatingRepresentation() && |
Ted Kremenek | 853734e | 2010-09-16 00:03:01 +0000 | [diff] [blame] | 6210 | !(lType->isBlockPointerType() && isRelational) && |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6211 | !lex.get()->getLocStart().isMacroID() && |
| 6212 | !rex.get()->getLocStart().isMacroID()) { |
Chris Lattner | 222b8bd | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6213 | // For non-floating point types, check for self-comparisons of the form |
| 6214 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 6215 | // often indicate logic errors in the program. |
Chandler Carruth | 65a3818 | 2010-07-12 06:23:38 +0000 | [diff] [blame] | 6216 | // |
| 6217 | // NOTE: Don't warn about comparison expressions resulting from macro |
| 6218 | // expansion. Also don't warn about comparisons which are only self |
| 6219 | // comparisons within a template specialization. The warnings should catch |
| 6220 | // obvious cases in the definition of the template anyways. The idea is to |
| 6221 | // warn when the typed comparison operator will always evaluate to the same |
| 6222 | // result. |
Chandler Carruth | 17773fc | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6223 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LHSStripped)) { |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6224 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RHSStripped)) { |
Ted Kremenek | 853734e | 2010-09-16 00:03:01 +0000 | [diff] [blame] | 6225 | if (DRL->getDecl() == DRR->getDecl() && |
Chandler Carruth | 17773fc | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6226 | !IsWithinTemplateSpecialization(DRL->getDecl())) { |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 6227 | DiagRuntimeBehavior(Loc, 0, PDiag(diag::warn_comparison_always) |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6228 | << 0 // self- |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6229 | << (Opc == BO_EQ |
| 6230 | || Opc == BO_LE |
| 6231 | || Opc == BO_GE)); |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6232 | } else if (lType->isArrayType() && rType->isArrayType() && |
| 6233 | !DRL->getDecl()->getType()->isReferenceType() && |
| 6234 | !DRR->getDecl()->getType()->isReferenceType()) { |
| 6235 | // what is it always going to eval to? |
| 6236 | char always_evals_to; |
| 6237 | switch(Opc) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6238 | case BO_EQ: // e.g. array1 == array2 |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6239 | always_evals_to = 0; // false |
| 6240 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6241 | case BO_NE: // e.g. array1 != array2 |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6242 | always_evals_to = 1; // true |
| 6243 | break; |
| 6244 | default: |
| 6245 | // best we can say is 'a constant' |
| 6246 | always_evals_to = 2; // e.g. array1 <= array2 |
| 6247 | break; |
| 6248 | } |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 6249 | DiagRuntimeBehavior(Loc, 0, PDiag(diag::warn_comparison_always) |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6250 | << 1 // array |
| 6251 | << always_evals_to); |
| 6252 | } |
| 6253 | } |
Chandler Carruth | 17773fc | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6254 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6255 | |
Chris Lattner | 222b8bd | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6256 | if (isa<CastExpr>(LHSStripped)) |
| 6257 | LHSStripped = LHSStripped->IgnoreParenCasts(); |
| 6258 | if (isa<CastExpr>(RHSStripped)) |
| 6259 | RHSStripped = RHSStripped->IgnoreParenCasts(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6260 | |
Chris Lattner | 222b8bd | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6261 | // Warn about comparisons against a string constant (unless the other |
| 6262 | // operand is null), the user probably wants strcmp. |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6263 | Expr *literalString = 0; |
| 6264 | Expr *literalStringStripped = 0; |
Chris Lattner | 222b8bd | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6265 | if ((isa<StringLiteral>(LHSStripped) || isa<ObjCEncodeExpr>(LHSStripped)) && |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6266 | !RHSStripped->isNullPointerConstant(Context, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6267 | Expr::NPC_ValueDependentIsNull)) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6268 | literalString = lex.get(); |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6269 | literalStringStripped = LHSStripped; |
Mike Stump | 12b8ce1 | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 6270 | } else if ((isa<StringLiteral>(RHSStripped) || |
| 6271 | isa<ObjCEncodeExpr>(RHSStripped)) && |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6272 | !LHSStripped->isNullPointerConstant(Context, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6273 | Expr::NPC_ValueDependentIsNull)) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6274 | literalString = rex.get(); |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6275 | literalStringStripped = RHSStripped; |
| 6276 | } |
| 6277 | |
| 6278 | if (literalString) { |
| 6279 | std::string resultComparison; |
| 6280 | switch (Opc) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6281 | case BO_LT: resultComparison = ") < 0"; break; |
| 6282 | case BO_GT: resultComparison = ") > 0"; break; |
| 6283 | case BO_LE: resultComparison = ") <= 0"; break; |
| 6284 | case BO_GE: resultComparison = ") >= 0"; break; |
| 6285 | case BO_EQ: resultComparison = ") == 0"; break; |
| 6286 | case BO_NE: resultComparison = ") != 0"; break; |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6287 | default: assert(false && "Invalid comparison operator"); |
| 6288 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6289 | |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 6290 | DiagRuntimeBehavior(Loc, 0, |
Douglas Gregor | 49862b8 | 2010-01-12 23:18:54 +0000 | [diff] [blame] | 6291 | PDiag(diag::warn_stringcompare) |
| 6292 | << isa<ObjCEncodeExpr>(literalStringStripped) |
Ted Kremenek | 800b66b | 2010-04-09 20:26:53 +0000 | [diff] [blame] | 6293 | << literalString->getSourceRange()); |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6294 | } |
Ted Kremenek | e451eae | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 6295 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6296 | |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6297 | // C99 6.5.8p3 / C99 6.5.9p4 |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6298 | if (lex.get()->getType()->isArithmeticType() && |
| 6299 | rex.get()->getType()->isArithmeticType()) { |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6300 | UsualArithmeticConversions(lex, rex); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6301 | if (lex.isInvalid() || rex.isInvalid()) |
| 6302 | return QualType(); |
| 6303 | } |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6304 | else { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6305 | lex = UsualUnaryConversions(lex.take()); |
| 6306 | if (lex.isInvalid()) |
| 6307 | return QualType(); |
| 6308 | |
| 6309 | rex = UsualUnaryConversions(rex.take()); |
| 6310 | if (rex.isInvalid()) |
| 6311 | return QualType(); |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6312 | } |
| 6313 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6314 | lType = lex.get()->getType(); |
| 6315 | rType = rex.get()->getType(); |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6316 | |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6317 | // The result of comparisons is 'bool' in C++, 'int' in C. |
Argyrios Kyrtzidis | 1bdd688 | 2011-02-18 20:55:15 +0000 | [diff] [blame] | 6318 | QualType ResultTy = Context.getLogicalOperationType(); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6319 | |
Chris Lattner | b620c34 | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 6320 | if (isRelational) { |
| 6321 | if (lType->isRealType() && rType->isRealType()) |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6322 | return ResultTy; |
Chris Lattner | b620c34 | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 6323 | } else { |
Ted Kremenek | e2763b0 | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 6324 | // Check for comparisons of floating point operands using != and ==. |
Douglas Gregor | 4ffbad1 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 6325 | if (lType->hasFloatingRepresentation()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6326 | CheckFloatComparison(Loc, lex.get(), rex.get()); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6327 | |
Chris Lattner | b620c34 | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 6328 | if (lType->isArithmeticType() && rType->isArithmeticType()) |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6329 | return ResultTy; |
Chris Lattner | b620c34 | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 6330 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6331 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6332 | bool LHSIsNull = lex.get()->isNullPointerConstant(Context, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6333 | Expr::NPC_ValueDependentIsNull); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6334 | bool RHSIsNull = rex.get()->isNullPointerConstant(Context, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6335 | Expr::NPC_ValueDependentIsNull); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6336 | |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6337 | // All of the following pointer-related warnings are GCC extensions, except |
| 6338 | // when handling null pointer constants. |
Steve Naroff | 808eb8f | 2007-08-27 04:08:11 +0000 | [diff] [blame] | 6339 | if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2 |
Chris Lattner | 3a0702e | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 6340 | QualType LCanPointeeTy = |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6341 | Context.getCanonicalType(lType->getAs<PointerType>()->getPointeeType()); |
Chris Lattner | 3a0702e | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 6342 | QualType RCanPointeeTy = |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6343 | Context.getCanonicalType(rType->getAs<PointerType>()->getPointeeType()); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6344 | |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6345 | if (getLangOptions().CPlusPlus) { |
Eli Friedman | 16c20961 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 6346 | if (LCanPointeeTy == RCanPointeeTy) |
| 6347 | return ResultTy; |
Fariborz Jahanian | ffc420c | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 6348 | if (!isRelational && |
| 6349 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 6350 | // Valid unless comparison between non-null pointer and function pointer |
| 6351 | // This is a gcc extension compatibility comparison. |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6352 | // In a SFINAE context, we treat this as a hard error to maintain |
| 6353 | // conformance with the C++ standard. |
Fariborz Jahanian | ffc420c | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 6354 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
| 6355 | && !LHSIsNull && !RHSIsNull) { |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6356 | Diag(Loc, |
| 6357 | isSFINAEContext()? |
| 6358 | diag::err_typecheck_comparison_of_fptr_to_void |
| 6359 | : diag::ext_typecheck_comparison_of_fptr_to_void) |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6360 | << lType << rType << lex.get()->getSourceRange() |
| 6361 | << rex.get()->getSourceRange(); |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6362 | |
| 6363 | if (isSFINAEContext()) |
| 6364 | return QualType(); |
| 6365 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6366 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
Fariborz Jahanian | ffc420c | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 6367 | return ResultTy; |
| 6368 | } |
| 6369 | } |
Anders Carlsson | a95069c | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 6370 | |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6371 | // C++ [expr.rel]p2: |
| 6372 | // [...] Pointer conversions (4.10) and qualification |
| 6373 | // conversions (4.4) are performed on pointer operands (or on |
| 6374 | // a pointer operand and a null pointer constant) to bring |
| 6375 | // them to their composite pointer type. [...] |
| 6376 | // |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6377 | // C++ [expr.eq]p1 uses the same notion for (in)equality |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6378 | // comparisons of pointers. |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6379 | bool NonStandardCompositeType = false; |
Douglas Gregor | 19175ff | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 6380 | QualType T = FindCompositePointerType(Loc, lex, rex, |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6381 | isSFINAEContext()? 0 : &NonStandardCompositeType); |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6382 | if (T.isNull()) { |
| 6383 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_pointers) |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6384 | << lType << rType << lex.get()->getSourceRange() |
| 6385 | << rex.get()->getSourceRange(); |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6386 | return QualType(); |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6387 | } else if (NonStandardCompositeType) { |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6388 | Diag(Loc, |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6389 | diag::ext_typecheck_comparison_of_distinct_pointers_nonstandard) |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6390 | << lType << rType << T |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6391 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6392 | } |
| 6393 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6394 | lex = ImpCastExprToType(lex.take(), T, CK_BitCast); |
| 6395 | rex = ImpCastExprToType(rex.take(), T, CK_BitCast); |
Douglas Gregor | 5b07c7e | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6396 | return ResultTy; |
| 6397 | } |
Eli Friedman | 16c20961 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 6398 | // C99 6.5.9p2 and C99 6.5.8p2 |
| 6399 | if (Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(), |
| 6400 | RCanPointeeTy.getUnqualifiedType())) { |
| 6401 | // Valid unless a relational comparison of function pointers |
| 6402 | if (isRelational && LCanPointeeTy->isFunctionType()) { |
| 6403 | Diag(Loc, diag::ext_typecheck_ordered_comparison_of_function_pointers) |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6404 | << lType << rType << lex.get()->getSourceRange() |
| 6405 | << rex.get()->getSourceRange(); |
Eli Friedman | 16c20961 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 6406 | } |
| 6407 | } else if (!isRelational && |
| 6408 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 6409 | // Valid unless comparison between non-null pointer and function pointer |
| 6410 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
| 6411 | && !LHSIsNull && !RHSIsNull) { |
| 6412 | Diag(Loc, diag::ext_typecheck_comparison_of_fptr_to_void) |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6413 | << lType << rType << lex.get()->getSourceRange() |
| 6414 | << rex.get()->getSourceRange(); |
Eli Friedman | 16c20961 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 6415 | } |
| 6416 | } else { |
| 6417 | // Invalid |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 6418 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6419 | << lType << rType << lex.get()->getSourceRange() |
| 6420 | << rex.get()->getSourceRange(); |
Steve Naroff | 75c1723 | 2007-06-13 21:41:08 +0000 | [diff] [blame] | 6421 | } |
John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6422 | if (LCanPointeeTy != RCanPointeeTy) { |
| 6423 | if (LHSIsNull && !RHSIsNull) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6424 | lex = ImpCastExprToType(lex.take(), rType, CK_BitCast); |
John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6425 | else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6426 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6427 | } |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6428 | return ResultTy; |
Steve Naroff | cdee44c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 6429 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6430 | |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 6431 | if (getLangOptions().CPlusPlus) { |
Anders Carlsson | a95069c | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 6432 | // Comparison of nullptr_t with itself. |
| 6433 | if (lType->isNullPtrType() && rType->isNullPtrType()) |
| 6434 | return ResultTy; |
| 6435 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6436 | // Comparison of pointers with null pointer constants and equality |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6437 | // comparisons of member pointers to null pointer constants. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6438 | if (RHSIsNull && |
Douglas Gregor | 39e9fa9 | 2011-06-01 15:12:24 +0000 | [diff] [blame] | 6439 | ((lType->isAnyPointerType() || lType->isNullPtrType()) || |
Douglas Gregor | 3e85c9c | 2011-06-16 18:52:05 +0000 | [diff] [blame] | 6440 | (!isRelational && |
| 6441 | (lType->isMemberPointerType() || lType->isBlockPointerType())))) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6442 | rex = ImpCastExprToType(rex.take(), lType, |
Douglas Gregor | f58ff32 | 2010-08-07 13:36:37 +0000 | [diff] [blame] | 6443 | lType->isMemberPointerType() |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6444 | ? CK_NullToMemberPointer |
John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 6445 | : CK_NullToPointer); |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 6446 | return ResultTy; |
| 6447 | } |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6448 | if (LHSIsNull && |
Douglas Gregor | 39e9fa9 | 2011-06-01 15:12:24 +0000 | [diff] [blame] | 6449 | ((rType->isAnyPointerType() || rType->isNullPtrType()) || |
Douglas Gregor | 3e85c9c | 2011-06-16 18:52:05 +0000 | [diff] [blame] | 6450 | (!isRelational && |
| 6451 | (rType->isMemberPointerType() || rType->isBlockPointerType())))) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6452 | lex = ImpCastExprToType(lex.take(), rType, |
Douglas Gregor | f58ff32 | 2010-08-07 13:36:37 +0000 | [diff] [blame] | 6453 | rType->isMemberPointerType() |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6454 | ? CK_NullToMemberPointer |
John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 6455 | : CK_NullToPointer); |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 6456 | return ResultTy; |
| 6457 | } |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6458 | |
| 6459 | // Comparison of member pointers. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6460 | if (!isRelational && |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6461 | lType->isMemberPointerType() && rType->isMemberPointerType()) { |
| 6462 | // C++ [expr.eq]p2: |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6463 | // In addition, pointers to members can be compared, or a pointer to |
| 6464 | // member and a null pointer constant. Pointer to member conversions |
| 6465 | // (4.11) and qualification conversions (4.4) are performed to bring |
| 6466 | // them to a common type. If one operand is a null pointer constant, |
| 6467 | // the common type is the type of the other operand. Otherwise, the |
| 6468 | // common type is a pointer to member type similar (4.4) to the type |
| 6469 | // of one of the operands, with a cv-qualification signature (4.4) |
| 6470 | // that is the union of the cv-qualification signatures of the operand |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6471 | // types. |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6472 | bool NonStandardCompositeType = false; |
Douglas Gregor | 19175ff | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 6473 | QualType T = FindCompositePointerType(Loc, lex, rex, |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6474 | isSFINAEContext()? 0 : &NonStandardCompositeType); |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6475 | if (T.isNull()) { |
| 6476 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_pointers) |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6477 | << lType << rType << lex.get()->getSourceRange() |
| 6478 | << rex.get()->getSourceRange(); |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6479 | return QualType(); |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6480 | } else if (NonStandardCompositeType) { |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6481 | Diag(Loc, |
Douglas Gregor | 6f5f642 | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6482 | diag::ext_typecheck_comparison_of_distinct_pointers_nonstandard) |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6483 | << lType << rType << T |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6484 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6485 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6486 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6487 | lex = ImpCastExprToType(lex.take(), T, CK_BitCast); |
| 6488 | rex = ImpCastExprToType(rex.take(), T, CK_BitCast); |
Douglas Gregor | b00b10e | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6489 | return ResultTy; |
| 6490 | } |
Douglas Gregor | 48e6bbf | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 6491 | |
| 6492 | // Handle scoped enumeration types specifically, since they don't promote |
| 6493 | // to integers. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6494 | if (lex.get()->getType()->isEnumeralType() && |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6495 | Context.hasSameUnqualifiedType(lex.get()->getType(), |
| 6496 | rex.get()->getType())) |
Douglas Gregor | 48e6bbf | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 6497 | return ResultTy; |
Sebastian Redl | 576fd42 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 6498 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6499 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6500 | // Handle block pointer types. |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6501 | if (!isRelational && lType->isBlockPointerType() && |
| 6502 | rType->isBlockPointerType()) { |
Ted Kremenek | c23c7e6 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6503 | QualType lpointee = lType->getAs<BlockPointerType>()->getPointeeType(); |
| 6504 | QualType rpointee = rType->getAs<BlockPointerType>()->getPointeeType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6505 | |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6506 | if (!LHSIsNull && !RHSIsNull && |
Eli Friedman | a6638ca | 2009-06-08 05:08:54 +0000 | [diff] [blame] | 6507 | !Context.typesAreCompatible(lpointee, rpointee)) { |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 6508 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6509 | << lType << rType << lex.get()->getSourceRange() |
| 6510 | << rex.get()->getSourceRange(); |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6511 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6512 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6513 | return ResultTy; |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6514 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6515 | |
Steve Naroff | e18f94c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 6516 | // Allow block pointers to be compared with null pointer constants. |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 6517 | if (!isRelational |
| 6518 | && ((lType->isBlockPointerType() && rType->isPointerType()) |
| 6519 | || (lType->isPointerType() && rType->isBlockPointerType()))) { |
Steve Naroff | e18f94c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 6520 | if (!LHSIsNull && !RHSIsNull) { |
John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6521 | if (!((rType->isPointerType() && rType->castAs<PointerType>() |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 6522 | ->getPointeeType()->isVoidType()) |
John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6523 | || (lType->isPointerType() && lType->castAs<PointerType>() |
Mike Stump | 1b821b4 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 6524 | ->getPointeeType()->isVoidType()))) |
| 6525 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6526 | << lType << rType << lex.get()->getSourceRange() |
| 6527 | << rex.get()->getSourceRange(); |
Steve Naroff | e18f94c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 6528 | } |
John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6529 | if (LHSIsNull && !RHSIsNull) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6530 | lex = ImpCastExprToType(lex.take(), rType, CK_BitCast); |
John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6531 | else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6532 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6533 | return ResultTy; |
Steve Naroff | e18f94c | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 6534 | } |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6535 | |
John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6536 | if (lType->isObjCObjectPointerType() || rType->isObjCObjectPointerType()) { |
| 6537 | const PointerType *LPT = lType->getAs<PointerType>(); |
| 6538 | const PointerType *RPT = rType->getAs<PointerType>(); |
| 6539 | if (LPT || RPT) { |
| 6540 | bool LPtrToVoid = LPT ? LPT->getPointeeType()->isVoidType() : false; |
| 6541 | bool RPtrToVoid = RPT ? RPT->getPointeeType()->isVoidType() : false; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6542 | |
Steve Naroff | 753567f | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 6543 | if (!LPtrToVoid && !RPtrToVoid && |
| 6544 | !Context.typesAreCompatible(lType, rType)) { |
Chris Lattner | 377d1f8 | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 6545 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6546 | << lType << rType << lex.get()->getSourceRange() |
| 6547 | << rex.get()->getSourceRange(); |
Steve Naroff | 1d4a9a3 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 6548 | } |
John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6549 | if (LHSIsNull && !RHSIsNull) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6550 | lex = ImpCastExprToType(lex.take(), rType, CK_BitCast); |
John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6551 | else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6552 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6553 | return ResultTy; |
Steve Naroff | ea54d9e | 2008-10-20 18:19:10 +0000 | [diff] [blame] | 6554 | } |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6555 | if (lType->isObjCObjectPointerType() && rType->isObjCObjectPointerType()) { |
Chris Lattner | f8344db | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 6556 | if (!Context.areComparableObjCPointerTypes(lType, rType)) |
Steve Naroff | 7cae42b | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6557 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6558 | << lType << rType << lex.get()->getSourceRange() |
| 6559 | << rex.get()->getSourceRange(); |
John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6560 | if (LHSIsNull && !RHSIsNull) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6561 | lex = ImpCastExprToType(lex.take(), rType, CK_BitCast); |
John McCall | 7684dde | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6562 | else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6563 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6564 | return ResultTy; |
Steve Naroff | b788d9b | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 6565 | } |
Fariborz Jahanian | 134cbef | 2007-12-20 01:06:58 +0000 | [diff] [blame] | 6566 | } |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6567 | if ((lType->isAnyPointerType() && rType->isIntegerType()) || |
| 6568 | (lType->isIntegerType() && rType->isAnyPointerType())) { |
Chris Lattner | d99bd52 | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6569 | unsigned DiagID = 0; |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6570 | bool isError = false; |
| 6571 | if ((LHSIsNull && lType->isIntegerType()) || |
| 6572 | (RHSIsNull && rType->isIntegerType())) { |
| 6573 | if (isRelational && !getLangOptions().CPlusPlus) |
Chris Lattner | d99bd52 | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6574 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_and_zero; |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6575 | } else if (isRelational && !getLangOptions().CPlusPlus) |
Chris Lattner | d99bd52 | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6576 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_integer; |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6577 | else if (getLangOptions().CPlusPlus) { |
| 6578 | DiagID = diag::err_typecheck_comparison_of_pointer_integer; |
| 6579 | isError = true; |
| 6580 | } else |
Chris Lattner | d99bd52 | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6581 | DiagID = diag::ext_typecheck_comparison_of_pointer_integer; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6582 | |
Chris Lattner | d99bd52 | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6583 | if (DiagID) { |
Chris Lattner | f8344db | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 6584 | Diag(Loc, DiagID) |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6585 | << lType << rType << lex.get()->getSourceRange() |
| 6586 | << rex.get()->getSourceRange(); |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6587 | if (isError) |
| 6588 | return QualType(); |
Chris Lattner | f8344db | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 6589 | } |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6590 | |
| 6591 | if (lType->isIntegerType()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6592 | lex = ImpCastExprToType(lex.take(), rType, |
John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 6593 | LHSIsNull ? CK_NullToPointer : CK_IntegralToPointer); |
Chris Lattner | d99bd52 | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6594 | else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6595 | rex = ImpCastExprToType(rex.take(), lType, |
John McCall | e84af4e | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 6596 | RHSIsNull ? CK_NullToPointer : CK_IntegralToPointer); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6597 | return ResultTy; |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 6598 | } |
Douglas Gregor | f267edd | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6599 | |
Steve Naroff | 4b19157 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 6600 | // Handle block pointers. |
Mike Stump | f70bcf7 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 6601 | if (!isRelational && RHSIsNull |
| 6602 | && lType->isBlockPointerType() && rType->isIntegerType()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6603 | rex = ImpCastExprToType(rex.take(), lType, CK_NullToPointer); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6604 | return ResultTy; |
Steve Naroff | 4b19157 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 6605 | } |
Mike Stump | f70bcf7 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 6606 | if (!isRelational && LHSIsNull |
| 6607 | && lType->isIntegerType() && rType->isBlockPointerType()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6608 | lex = ImpCastExprToType(lex.take(), rType, CK_NullToPointer); |
Douglas Gregor | ca63811b | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6609 | return ResultTy; |
Steve Naroff | 4b19157 | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 6610 | } |
Douglas Gregor | 48e6bbf | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 6611 | |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6612 | return InvalidOperands(Loc, lex, rex); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 6613 | } |
| 6614 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6615 | /// CheckVectorCompareOperands - vector comparisons are a clang extension that |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6616 | /// operates on extended vector types. Instead of producing an IntTy result, |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6617 | /// like a scalar comparison, a vector comparison produces a vector of integer |
| 6618 | /// types. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6619 | QualType Sema::CheckVectorCompareOperands(ExprResult &lex, ExprResult &rex, |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6620 | SourceLocation Loc, |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6621 | bool isRelational) { |
| 6622 | // Check to make sure we're operating on vectors of the same type and width, |
| 6623 | // Allowing one side to be a scalar of element type. |
Eli Friedman | 1408bc9 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 6624 | QualType vType = CheckVectorOperands(lex, rex, Loc, /*isCompAssign*/false); |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6625 | if (vType.isNull()) |
| 6626 | return vType; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6627 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6628 | QualType lType = lex.get()->getType(); |
| 6629 | QualType rType = rex.get()->getType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6630 | |
Anton Yartsev | 530deb9 | 2011-03-27 15:36:07 +0000 | [diff] [blame] | 6631 | // If AltiVec, the comparison results in a numeric type, i.e. |
| 6632 | // bool for C++, int for C |
Anton Yartsev | 93900c7 | 2011-03-28 21:00:05 +0000 | [diff] [blame] | 6633 | if (vType->getAs<VectorType>()->getVectorKind() == VectorType::AltiVecVector) |
Anton Yartsev | 530deb9 | 2011-03-27 15:36:07 +0000 | [diff] [blame] | 6634 | return Context.getLogicalOperationType(); |
| 6635 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6636 | // For non-floating point types, check for self-comparisons of the form |
| 6637 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 6638 | // often indicate logic errors in the program. |
Douglas Gregor | 4ffbad1 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 6639 | if (!lType->hasFloatingRepresentation()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6640 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex.get()->IgnoreParens())) |
| 6641 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex.get()->IgnoreParens())) |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6642 | if (DRL->getDecl() == DRR->getDecl()) |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 6643 | DiagRuntimeBehavior(Loc, 0, |
Douglas Gregor | ec170db | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6644 | PDiag(diag::warn_comparison_always) |
| 6645 | << 0 // self- |
| 6646 | << 2 // "a constant" |
| 6647 | ); |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6648 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6649 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6650 | // Check for comparisons of floating point operands using != and ==. |
Douglas Gregor | 4ffbad1 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 6651 | if (!isRelational && lType->hasFloatingRepresentation()) { |
| 6652 | assert (rType->hasFloatingRepresentation()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6653 | CheckFloatComparison(Loc, lex.get(), rex.get()); |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6654 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6655 | |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6656 | // Return the type for the comparison, which is the same as vector type for |
| 6657 | // integer vectors, or an integer type of identical size and number of |
| 6658 | // elements for floating point vectors. |
Douglas Gregor | 5cc2c8b | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 6659 | if (lType->hasIntegerRepresentation()) |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6660 | return lType; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6661 | |
John McCall | 9dd450b | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6662 | const VectorType *VTy = lType->getAs<VectorType>(); |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6663 | unsigned TypeSize = Context.getTypeSize(VTy->getElementType()); |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6664 | if (TypeSize == Context.getTypeSize(Context.IntTy)) |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6665 | return Context.getExtVectorType(Context.IntTy, VTy->getNumElements()); |
Chris Lattner | 5d68896 | 2009-03-31 07:46:52 +0000 | [diff] [blame] | 6666 | if (TypeSize == Context.getTypeSize(Context.LongTy)) |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6667 | return Context.getExtVectorType(Context.LongTy, VTy->getNumElements()); |
| 6668 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6669 | assert(TypeSize == Context.getTypeSize(Context.LongLongTy) && |
Nate Begeman | 2f2bdeb | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6670 | "Unhandled vector element size in vector compare"); |
Nate Begeman | 191a6b1 | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6671 | return Context.getExtVectorType(Context.LongLongTy, VTy->getNumElements()); |
| 6672 | } |
| 6673 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 6674 | inline QualType Sema::CheckBitwiseOperands( |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6675 | ExprResult &lex, ExprResult &rex, SourceLocation Loc, bool isCompAssign) { |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6676 | if (lex.get()->getType()->isVectorType() || |
| 6677 | rex.get()->getType()->isVectorType()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6678 | if (lex.get()->getType()->hasIntegerRepresentation() && |
| 6679 | rex.get()->getType()->hasIntegerRepresentation()) |
Eli Friedman | 1408bc9 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 6680 | return CheckVectorOperands(lex, rex, Loc, isCompAssign); |
Douglas Gregor | 5cc2c8b | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 6681 | |
| 6682 | return InvalidOperands(Loc, lex, rex); |
| 6683 | } |
Steve Naroff | b8ea4fb | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 6684 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6685 | ExprResult lexResult = Owned(lex), rexResult = Owned(rex); |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6686 | QualType compType = UsualArithmeticConversions(lexResult, rexResult, |
| 6687 | isCompAssign); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6688 | if (lexResult.isInvalid() || rexResult.isInvalid()) |
| 6689 | return QualType(); |
| 6690 | lex = lexResult.take(); |
| 6691 | rex = rexResult.take(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6692 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6693 | if (lex.get()->getType()->isIntegralOrUnscopedEnumerationType() && |
| 6694 | rex.get()->getType()->isIntegralOrUnscopedEnumerationType()) |
Steve Naroff | be4c4d1 | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 6695 | return compType; |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6696 | return InvalidOperands(Loc, lex, rex); |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 6697 | } |
| 6698 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 6699 | inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6700 | ExprResult &lex, ExprResult &rex, SourceLocation Loc, unsigned Opc) { |
Chris Lattner | 8406c51 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 6701 | |
| 6702 | // Diagnose cases where the user write a logical and/or but probably meant a |
| 6703 | // bitwise one. We do this when the LHS is a non-bool integer and the RHS |
| 6704 | // is a constant. |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6705 | if (lex.get()->getType()->isIntegerType() && |
| 6706 | !lex.get()->getType()->isBooleanType() && |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6707 | rex.get()->getType()->isIntegerType() && !rex.get()->isValueDependent() && |
Richard Trieu | cfe3926 | 2011-07-15 00:00:51 +0000 | [diff] [blame] | 6708 | // Don't warn in macros or template instantiations. |
| 6709 | !Loc.isMacroID() && ActiveTemplateInstantiations.empty()) { |
Chris Lattner | 938533d | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 6710 | // If the RHS can be constant folded, and if it constant folds to something |
| 6711 | // that isn't 0 or 1 (which indicate a potential logical operation that |
| 6712 | // happened to fold to true/false) then warn. |
Chandler Carruth | e54ff6c | 2011-05-31 05:41:42 +0000 | [diff] [blame] | 6713 | // Parens on the RHS are ignored. |
Chris Lattner | 938533d | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 6714 | Expr::EvalResult Result; |
Chandler Carruth | e54ff6c | 2011-05-31 05:41:42 +0000 | [diff] [blame] | 6715 | if (rex.get()->Evaluate(Result, Context) && !Result.HasSideEffects) |
| 6716 | if ((getLangOptions().Bool && !rex.get()->getType()->isBooleanType()) || |
| 6717 | (Result.Val.getInt() != 0 && Result.Val.getInt() != 1)) { |
| 6718 | Diag(Loc, diag::warn_logical_instead_of_bitwise) |
| 6719 | << rex.get()->getSourceRange() |
| 6720 | << (Opc == BO_LAnd ? "&&" : "||") |
| 6721 | << (Opc == BO_LAnd ? "&" : "|"); |
Chris Lattner | 938533d | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 6722 | } |
| 6723 | } |
Chris Lattner | 8406c51 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 6724 | |
Anders Carlsson | 2e7bc11 | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 6725 | if (!Context.getLangOptions().CPlusPlus) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6726 | lex = UsualUnaryConversions(lex.take()); |
| 6727 | if (lex.isInvalid()) |
| 6728 | return QualType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6729 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6730 | rex = UsualUnaryConversions(rex.take()); |
| 6731 | if (rex.isInvalid()) |
| 6732 | return QualType(); |
| 6733 | |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6734 | if (!lex.get()->getType()->isScalarType() || |
| 6735 | !rex.get()->getType()->isScalarType()) |
Anders Carlsson | 2e7bc11 | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 6736 | return InvalidOperands(Loc, lex, rex); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6737 | |
Anders Carlsson | 2e7bc11 | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 6738 | return Context.IntTy; |
Anders Carlsson | 35a99d9 | 2009-10-16 01:44:21 +0000 | [diff] [blame] | 6739 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6740 | |
John McCall | 4a2429a | 2010-06-04 00:29:51 +0000 | [diff] [blame] | 6741 | // The following is safe because we only use this method for |
| 6742 | // non-overloadable operands. |
| 6743 | |
Anders Carlsson | 2e7bc11 | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 6744 | // C++ [expr.log.and]p1 |
| 6745 | // C++ [expr.log.or]p1 |
John McCall | 4a2429a | 2010-06-04 00:29:51 +0000 | [diff] [blame] | 6746 | // The operands are both contextually converted to type bool. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6747 | ExprResult lexRes = PerformContextuallyConvertToBool(lex.get()); |
| 6748 | if (lexRes.isInvalid()) |
Anders Carlsson | 2e7bc11 | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 6749 | return InvalidOperands(Loc, lex, rex); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6750 | lex = move(lexRes); |
| 6751 | |
| 6752 | ExprResult rexRes = PerformContextuallyConvertToBool(rex.get()); |
| 6753 | if (rexRes.isInvalid()) |
| 6754 | return InvalidOperands(Loc, lex, rex); |
| 6755 | rex = move(rexRes); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6756 | |
Anders Carlsson | 2e7bc11 | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 6757 | // C++ [expr.log.and]p2 |
| 6758 | // C++ [expr.log.or]p2 |
| 6759 | // The result is a bool. |
| 6760 | return Context.BoolTy; |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 6761 | } |
| 6762 | |
Fariborz Jahanian | 8e1555c | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 6763 | /// IsReadonlyProperty - Verify that otherwise a valid l-value expression |
| 6764 | /// is a read-only property; return true if so. A readonly property expression |
| 6765 | /// depends on various declarations and thus must be treated specially. |
| 6766 | /// |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6767 | static bool IsReadonlyProperty(Expr *E, Sema &S) { |
Fariborz Jahanian | 8e1555c | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 6768 | if (E->getStmtClass() == Expr::ObjCPropertyRefExprClass) { |
| 6769 | const ObjCPropertyRefExpr* PropExpr = cast<ObjCPropertyRefExpr>(E); |
John McCall | b7bd14f | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 6770 | if (PropExpr->isImplicitProperty()) return false; |
| 6771 | |
| 6772 | ObjCPropertyDecl *PDecl = PropExpr->getExplicitProperty(); |
| 6773 | QualType BaseType = PropExpr->isSuperReceiver() ? |
| 6774 | PropExpr->getSuperReceiverType() : |
Fariborz Jahanian | 681c075 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 6775 | PropExpr->getBase()->getType(); |
| 6776 | |
John McCall | b7bd14f | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 6777 | if (const ObjCObjectPointerType *OPT = |
| 6778 | BaseType->getAsObjCInterfacePointerType()) |
| 6779 | if (ObjCInterfaceDecl *IFace = OPT->getInterfaceDecl()) |
| 6780 | if (S.isPropertyReadonly(PDecl, IFace)) |
| 6781 | return true; |
Fariborz Jahanian | 8e1555c | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 6782 | } |
| 6783 | return false; |
| 6784 | } |
| 6785 | |
Fariborz Jahanian | b24b568 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 6786 | static bool IsConstProperty(Expr *E, Sema &S) { |
| 6787 | if (E->getStmtClass() == Expr::ObjCPropertyRefExprClass) { |
| 6788 | const ObjCPropertyRefExpr* PropExpr = cast<ObjCPropertyRefExpr>(E); |
| 6789 | if (PropExpr->isImplicitProperty()) return false; |
| 6790 | |
| 6791 | ObjCPropertyDecl *PDecl = PropExpr->getExplicitProperty(); |
| 6792 | QualType T = PDecl->getType(); |
| 6793 | if (T->isReferenceType()) |
Fariborz Jahanian | 20688cc | 2011-03-30 16:59:30 +0000 | [diff] [blame] | 6794 | T = T->getAs<ReferenceType>()->getPointeeType(); |
Fariborz Jahanian | b24b568 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 6795 | CanQualType CT = S.Context.getCanonicalType(T); |
| 6796 | return CT.isConstQualified(); |
| 6797 | } |
| 6798 | return false; |
| 6799 | } |
| 6800 | |
Fariborz Jahanian | 071caef | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 6801 | static bool IsReadonlyMessage(Expr *E, Sema &S) { |
| 6802 | if (E->getStmtClass() != Expr::MemberExprClass) |
| 6803 | return false; |
| 6804 | const MemberExpr *ME = cast<MemberExpr>(E); |
| 6805 | NamedDecl *Member = ME->getMemberDecl(); |
| 6806 | if (isa<FieldDecl>(Member)) { |
| 6807 | Expr *Base = ME->getBase()->IgnoreParenImpCasts(); |
| 6808 | if (Base->getStmtClass() != Expr::ObjCMessageExprClass) |
| 6809 | return false; |
| 6810 | return cast<ObjCMessageExpr>(Base)->getMethodDecl() != 0; |
| 6811 | } |
| 6812 | return false; |
| 6813 | } |
| 6814 | |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6815 | /// CheckForModifiableLvalue - Verify that E is a modifiable lvalue. If not, |
| 6816 | /// emit an error and return true. If so, return false. |
| 6817 | static bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S) { |
Daniel Dunbar | c2223ab | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 6818 | SourceLocation OrigLoc = Loc; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6819 | Expr::isModifiableLvalueResult IsLV = E->isModifiableLvalue(S.Context, |
Daniel Dunbar | c2223ab | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 6820 | &Loc); |
Fariborz Jahanian | 8e1555c | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 6821 | if (IsLV == Expr::MLV_Valid && IsReadonlyProperty(E, S)) |
| 6822 | IsLV = Expr::MLV_ReadonlyProperty; |
Fariborz Jahanian | b24b568 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 6823 | else if (Expr::MLV_ConstQualified && IsConstProperty(E, S)) |
| 6824 | IsLV = Expr::MLV_Valid; |
Fariborz Jahanian | 071caef | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 6825 | else if (IsLV == Expr::MLV_ClassTemporary && IsReadonlyMessage(E, S)) |
| 6826 | IsLV = Expr::MLV_InvalidMessageExpression; |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6827 | if (IsLV == Expr::MLV_Valid) |
| 6828 | return false; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6829 | |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6830 | unsigned Diag = 0; |
| 6831 | bool NeedType = false; |
| 6832 | switch (IsLV) { // C99 6.5.16p2 |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6833 | case Expr::MLV_ConstQualified: |
| 6834 | Diag = diag::err_typecheck_assign_const; |
| 6835 | |
John McCall | d463132 | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 6836 | // In ARC, use some specialized diagnostics for occasions where we |
| 6837 | // infer 'const'. These are always pseudo-strong variables. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6838 | if (S.getLangOptions().ObjCAutoRefCount) { |
| 6839 | DeclRefExpr *declRef = dyn_cast<DeclRefExpr>(E->IgnoreParenCasts()); |
| 6840 | if (declRef && isa<VarDecl>(declRef->getDecl())) { |
| 6841 | VarDecl *var = cast<VarDecl>(declRef->getDecl()); |
| 6842 | |
John McCall | d463132 | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 6843 | // Use the normal diagnostic if it's pseudo-__strong but the |
| 6844 | // user actually wrote 'const'. |
| 6845 | if (var->isARCPseudoStrong() && |
| 6846 | (!var->getTypeSourceInfo() || |
| 6847 | !var->getTypeSourceInfo()->getType().isConstQualified())) { |
| 6848 | // There are two pseudo-strong cases: |
| 6849 | // - self |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6850 | ObjCMethodDecl *method = S.getCurMethodDecl(); |
| 6851 | if (method && var == method->getSelfDecl()) |
| 6852 | Diag = diag::err_typecheck_arr_assign_self; |
John McCall | d463132 | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 6853 | |
| 6854 | // - fast enumeration variables |
| 6855 | else |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6856 | Diag = diag::err_typecheck_arr_assign_enumeration; |
John McCall | d463132 | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 6857 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6858 | SourceRange Assign; |
| 6859 | if (Loc != OrigLoc) |
| 6860 | Assign = SourceRange(OrigLoc, OrigLoc); |
| 6861 | S.Diag(Loc, Diag) << E->getSourceRange() << Assign; |
| 6862 | // We need to preserve the AST regardless, so migration tool |
| 6863 | // can do its job. |
| 6864 | return false; |
| 6865 | } |
| 6866 | } |
| 6867 | } |
| 6868 | |
| 6869 | break; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6870 | case Expr::MLV_ArrayType: |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6871 | Diag = diag::err_typecheck_array_not_modifiable_lvalue; |
| 6872 | NeedType = true; |
| 6873 | break; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6874 | case Expr::MLV_NotObjectType: |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6875 | Diag = diag::err_typecheck_non_object_not_modifiable_lvalue; |
| 6876 | NeedType = true; |
| 6877 | break; |
Chris Lattner | 9b3bbe9 | 2008-11-17 19:51:54 +0000 | [diff] [blame] | 6878 | case Expr::MLV_LValueCast: |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6879 | Diag = diag::err_typecheck_lvalue_casts_not_supported; |
| 6880 | break; |
Douglas Gregor | b154fdc | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 6881 | case Expr::MLV_Valid: |
| 6882 | llvm_unreachable("did not take early return for MLV_Valid"); |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6883 | case Expr::MLV_InvalidExpression: |
Douglas Gregor | b154fdc | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 6884 | case Expr::MLV_MemberFunction: |
| 6885 | case Expr::MLV_ClassTemporary: |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6886 | Diag = diag::err_typecheck_expression_not_modifiable_lvalue; |
| 6887 | break; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6888 | case Expr::MLV_IncompleteType: |
| 6889 | case Expr::MLV_IncompleteVoidType: |
Douglas Gregor | ed0cfbd | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 6890 | return S.RequireCompleteType(Loc, E->getType(), |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 6891 | S.PDiag(diag::err_typecheck_incomplete_type_not_modifiable_lvalue) |
Anders Carlsson | d624e16 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 6892 | << E->getSourceRange()); |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6893 | case Expr::MLV_DuplicateVectorComponents: |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6894 | Diag = diag::err_typecheck_duplicate_vector_components_not_mlvalue; |
| 6895 | break; |
Steve Naroff | ba756cb | 2008-09-26 14:41:28 +0000 | [diff] [blame] | 6896 | case Expr::MLV_NotBlockQualified: |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6897 | Diag = diag::err_block_decl_ref_not_modifiable_lvalue; |
| 6898 | break; |
Fariborz Jahanian | 8a1810f | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 6899 | case Expr::MLV_ReadonlyProperty: |
| 6900 | Diag = diag::error_readonly_property_assignment; |
| 6901 | break; |
Fariborz Jahanian | 5118c41 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 6902 | case Expr::MLV_NoSetterProperty: |
| 6903 | Diag = diag::error_nosetter_property_assignment; |
| 6904 | break; |
Fariborz Jahanian | 071caef | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 6905 | case Expr::MLV_InvalidMessageExpression: |
| 6906 | Diag = diag::error_readonly_message_assignment; |
| 6907 | break; |
Fariborz Jahanian | e8d2890 | 2009-12-15 23:59:41 +0000 | [diff] [blame] | 6908 | case Expr::MLV_SubObjCPropertySetting: |
| 6909 | Diag = diag::error_no_subobject_property_setting; |
| 6910 | break; |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 6911 | } |
Steve Naroff | ad373bd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 6912 | |
Daniel Dunbar | c2223ab | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 6913 | SourceRange Assign; |
| 6914 | if (Loc != OrigLoc) |
| 6915 | Assign = SourceRange(OrigLoc, OrigLoc); |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6916 | if (NeedType) |
Daniel Dunbar | c2223ab | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 6917 | S.Diag(Loc, Diag) << E->getType() << E->getSourceRange() << Assign; |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6918 | else |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6919 | S.Diag(Loc, Diag) << E->getSourceRange() << Assign; |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6920 | return true; |
| 6921 | } |
| 6922 | |
| 6923 | |
| 6924 | |
| 6925 | // C99 6.5.16.1 |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6926 | QualType Sema::CheckAssignmentOperands(Expr *LHS, ExprResult &RHS, |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6927 | SourceLocation Loc, |
| 6928 | QualType CompoundType) { |
| 6929 | // Verify that LHS is a modifiable lvalue, and emit error if not. |
| 6930 | if (CheckForModifiableLvalue(LHS, Loc, *this)) |
Chris Lattner | 30bd327 | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6931 | return QualType(); |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6932 | |
| 6933 | QualType LHSType = LHS->getType(); |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6934 | QualType RHSType = CompoundType.isNull() ? RHS.get()->getType() : |
| 6935 | CompoundType; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6936 | AssignConvertType ConvTy; |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6937 | if (CompoundType.isNull()) { |
Fariborz Jahanian | be21aa3 | 2010-06-07 22:02:01 +0000 | [diff] [blame] | 6938 | QualType LHSTy(LHSType); |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 6939 | // Simple assignment "x = y". |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6940 | if (LHS->getObjectKind() == OK_ObjCProperty) { |
| 6941 | ExprResult LHSResult = Owned(LHS); |
| 6942 | ConvertPropertyForLValue(LHSResult, RHS, LHSTy); |
| 6943 | if (LHSResult.isInvalid()) |
| 6944 | return QualType(); |
| 6945 | LHS = LHSResult.take(); |
| 6946 | } |
Fariborz Jahanian | be21aa3 | 2010-06-07 22:02:01 +0000 | [diff] [blame] | 6947 | ConvTy = CheckSingleAssignmentConstraints(LHSTy, RHS); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6948 | if (RHS.isInvalid()) |
| 6949 | return QualType(); |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 6950 | // Special case of NSObject attributes on c-style pointer types. |
| 6951 | if (ConvTy == IncompatiblePointer && |
| 6952 | ((Context.isObjCNSObjectType(LHSType) && |
Steve Naroff | 79d1215 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 6953 | RHSType->isObjCObjectPointerType()) || |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 6954 | (Context.isObjCNSObjectType(RHSType) && |
Steve Naroff | 79d1215 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 6955 | LHSType->isObjCObjectPointerType()))) |
Fariborz Jahanian | 255c095 | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 6956 | ConvTy = Compatible; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6957 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6958 | if (ConvTy == Compatible && |
| 6959 | getLangOptions().ObjCNonFragileABI && |
| 6960 | LHSType->isObjCObjectType()) |
| 6961 | Diag(Loc, diag::err_assignment_requires_nonfragile_object) |
| 6962 | << LHSType; |
| 6963 | |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 6964 | // If the RHS is a unary plus or minus, check to see if they = and + are |
| 6965 | // right next to each other. If so, the user may have typo'd "x =+ 4" |
| 6966 | // instead of "x += 4". |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6967 | Expr *RHSCheck = RHS.get(); |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 6968 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RHSCheck)) |
| 6969 | RHSCheck = ICE->getSubExpr(); |
| 6970 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(RHSCheck)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6971 | if ((UO->getOpcode() == UO_Plus || |
| 6972 | UO->getOpcode() == UO_Minus) && |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6973 | Loc.isFileID() && UO->getOperatorLoc().isFileID() && |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 6974 | // Only if the two operators are exactly adjacent. |
Chris Lattner | 36c39c9 | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 6975 | Loc.getFileLocWithOffset(1) == UO->getOperatorLoc() && |
| 6976 | // And there is a space or other character before the subexpr of the |
| 6977 | // unary +/-. We don't want to warn on "x=-1". |
Chris Lattner | ed9f14c | 2009-03-09 07:11:10 +0000 | [diff] [blame] | 6978 | Loc.getFileLocWithOffset(2) != UO->getSubExpr()->getLocStart() && |
| 6979 | UO->getSubExpr()->getLocStart().isFileID()) { |
Chris Lattner | 29e812b | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 6980 | Diag(Loc, diag::warn_not_compound_assign) |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6981 | << (UO->getOpcode() == UO_Plus ? "+" : "-") |
Chris Lattner | 29e812b | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 6982 | << SourceRange(UO->getOperatorLoc(), UO->getOperatorLoc()); |
Chris Lattner | 36c39c9 | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 6983 | } |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 6984 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6985 | |
| 6986 | if (ConvTy == Compatible) { |
| 6987 | if (LHSType.getObjCLifetime() == Qualifiers::OCL_Strong) |
| 6988 | checkRetainCycles(LHS, RHS.get()); |
Fariborz Jahanian | 5f98da0 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 6989 | else if (getLangOptions().ObjCAutoRefCount) |
| 6990 | checkUnsafeExprAssigns(Loc, LHS, RHS.get()); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6991 | } |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 6992 | } else { |
| 6993 | // Compound assignment "x += y" |
Douglas Gregor | c03a108 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 6994 | ConvTy = CheckAssignmentConstraints(Loc, LHSType, RHSType); |
Chris Lattner | ea71438 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 6995 | } |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6996 | |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6997 | if (DiagnoseAssignmentResult(ConvTy, Loc, LHSType, RHSType, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6998 | RHS.get(), AA_Assigning)) |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6999 | return QualType(); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7000 | |
Argyrios Kyrtzidis | a9b630e | 2011-04-26 17:41:22 +0000 | [diff] [blame] | 7001 | CheckForNullPointerDereference(*this, LHS); |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 7002 | |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 7003 | // C99 6.5.16p3: The type of an assignment expression is the type of the |
| 7004 | // left operand unless the left operand has qualified type, in which case |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7005 | // it is the unqualified version of the type of the left operand. |
Steve Naroff | 98cf3e9 | 2007-06-06 18:38:38 +0000 | [diff] [blame] | 7006 | // C99 6.5.16.1p2: In simple assignment, the value of the right operand |
| 7007 | // is converted to the type of the assignment expression (above). |
Chris Lattner | f17bd42 | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 7008 | // 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] | 7009 | // operand. |
John McCall | 01cbf2d | 2010-10-12 02:19:57 +0000 | [diff] [blame] | 7010 | return (getLangOptions().CPlusPlus |
| 7011 | ? LHSType : LHSType.getUnqualifiedType()); |
Steve Naroff | ae4143e | 2007-04-26 20:39:23 +0000 | [diff] [blame] | 7012 | } |
| 7013 | |
Chris Lattner | 326f757 | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7014 | // C99 6.5.17 |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7015 | static QualType CheckCommaOperands(Sema &S, ExprResult &LHS, ExprResult &RHS, |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7016 | SourceLocation Loc) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7017 | S.DiagnoseUnusedExprResult(LHS.get()); |
Argyrios Kyrtzidis | 639ffb0 | 2010-06-30 10:53:14 +0000 | [diff] [blame] | 7018 | |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 7019 | LHS = S.CheckPlaceholderExpr(LHS.take()); |
| 7020 | RHS = S.CheckPlaceholderExpr(RHS.take()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7021 | if (LHS.isInvalid() || RHS.isInvalid()) |
Douglas Gregor | 0124e9b | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 7022 | return QualType(); |
| 7023 | |
John McCall | 73d3618 | 2010-10-12 07:14:40 +0000 | [diff] [blame] | 7024 | // C's comma performs lvalue conversion (C99 6.3.2.1) on both its |
| 7025 | // operands, but not unary promotions. |
| 7026 | // 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] | 7027 | |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7028 | // So we treat the LHS as a ignored value, and in C++ we allow the |
| 7029 | // containing site to determine what should be done with the RHS. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7030 | LHS = S.IgnoredValueConversions(LHS.take()); |
| 7031 | if (LHS.isInvalid()) |
| 7032 | return QualType(); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7033 | |
| 7034 | if (!S.getLangOptions().CPlusPlus) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7035 | RHS = S.DefaultFunctionArrayLvalueConversion(RHS.take()); |
| 7036 | if (RHS.isInvalid()) |
| 7037 | return QualType(); |
| 7038 | if (!RHS.get()->getType()->isVoidType()) |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 7039 | S.RequireCompleteType(Loc, RHS.get()->getType(), |
| 7040 | diag::err_incomplete_type); |
John McCall | 73d3618 | 2010-10-12 07:14:40 +0000 | [diff] [blame] | 7041 | } |
Eli Friedman | ba961a9 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 7042 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7043 | return RHS.get()->getType(); |
Steve Naroff | 95af013 | 2007-03-30 23:47:58 +0000 | [diff] [blame] | 7044 | } |
| 7045 | |
Steve Naroff | 7a5af78 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 7046 | /// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine |
| 7047 | /// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7048 | static QualType CheckIncrementDecrementOperand(Sema &S, Expr *Op, |
| 7049 | ExprValueKind &VK, |
| 7050 | SourceLocation OpLoc, |
| 7051 | bool isInc, bool isPrefix) { |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7052 | if (Op->isTypeDependent()) |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7053 | return S.Context.DependentTy; |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7054 | |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7055 | QualType ResType = Op->getType(); |
| 7056 | assert(!ResType.isNull() && "no type for increment/decrement expression"); |
Steve Naroff | d50c88e | 2007-04-05 21:15:20 +0000 | [diff] [blame] | 7057 | |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7058 | if (S.getLangOptions().CPlusPlus && ResType->isBooleanType()) { |
Sebastian Redl | e10c2c3 | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 7059 | // Decrement of bool is not allowed. |
| 7060 | if (!isInc) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7061 | S.Diag(OpLoc, diag::err_decrement_bool) << Op->getSourceRange(); |
Sebastian Redl | e10c2c3 | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 7062 | return QualType(); |
| 7063 | } |
| 7064 | // Increment of bool sets it to true, but is deprecated. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7065 | S.Diag(OpLoc, diag::warn_increment_bool) << Op->getSourceRange(); |
Sebastian Redl | e10c2c3 | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 7066 | } else if (ResType->isRealType()) { |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7067 | // OK! |
Steve Naroff | 6b712a7 | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 7068 | } else if (ResType->isAnyPointerType()) { |
| 7069 | QualType PointeeTy = ResType->getPointeeType(); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7070 | |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7071 | // C99 6.5.2.4p2, 6.5.6p2 |
Chandler Carruth | c933221 | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 7072 | if (!checkArithmeticOpPointerOperand(S, OpLoc, Op)) |
Douglas Gregor | dd430f7 | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 7073 | return QualType(); |
Chandler Carruth | c933221 | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 7074 | |
Fariborz Jahanian | ca75db7 | 2009-07-16 17:59:14 +0000 | [diff] [blame] | 7075 | // Diagnose bad cases where we step over interface counts. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7076 | else if (PointeeTy->isObjCObjectType() && S.LangOpts.ObjCNonFragileABI) { |
| 7077 | S.Diag(OpLoc, diag::err_arithmetic_nonfragile_interface) |
Fariborz Jahanian | ca75db7 | 2009-07-16 17:59:14 +0000 | [diff] [blame] | 7078 | << PointeeTy << Op->getSourceRange(); |
| 7079 | return QualType(); |
| 7080 | } |
Eli Friedman | 090addd | 2010-01-03 00:20:48 +0000 | [diff] [blame] | 7081 | } else if (ResType->isAnyComplexType()) { |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7082 | // C99 does not support ++/-- on complex types, we allow as an extension. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7083 | S.Diag(OpLoc, diag::ext_integer_increment_complex) |
Chris Lattner | 1e5665e | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 7084 | << ResType << Op->getSourceRange(); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7085 | } else if (ResType->isPlaceholderType()) { |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 7086 | ExprResult PR = S.CheckPlaceholderExpr(Op); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7087 | if (PR.isInvalid()) return QualType(); |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7088 | return CheckIncrementDecrementOperand(S, PR.take(), VK, OpLoc, |
| 7089 | isInc, isPrefix); |
Anton Yartsev | 85129b8 | 2011-02-07 02:17:30 +0000 | [diff] [blame] | 7090 | } else if (S.getLangOptions().AltiVec && ResType->isVectorType()) { |
| 7091 | // OK! ( C/C++ Language Extensions for CBEA(Version 2.6) 10.3 ) |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7092 | } else { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7093 | S.Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement) |
Douglas Gregor | 906db8a | 2009-12-15 16:44:32 +0000 | [diff] [blame] | 7094 | << ResType << int(isInc) << Op->getSourceRange(); |
Chris Lattner | 6b0cf14 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7095 | return QualType(); |
Steve Naroff | 46ba1eb | 2007-04-03 23:13:13 +0000 | [diff] [blame] | 7096 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7097 | // 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] | 7098 | // Now make sure the operand is a modifiable lvalue. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7099 | if (CheckForModifiableLvalue(Op, OpLoc, S)) |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 7100 | return QualType(); |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 7101 | // In C++, a prefix increment is the same type as the operand. Otherwise |
| 7102 | // (in C or with postfix), the increment is the unqualified type of the |
| 7103 | // operand. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7104 | if (isPrefix && S.getLangOptions().CPlusPlus) { |
| 7105 | VK = VK_LValue; |
| 7106 | return ResType; |
| 7107 | } else { |
| 7108 | VK = VK_RValue; |
| 7109 | return ResType.getUnqualifiedType(); |
| 7110 | } |
Steve Naroff | 26c8ea5 | 2007-03-21 21:08:52 +0000 | [diff] [blame] | 7111 | } |
| 7112 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7113 | ExprResult Sema::ConvertPropertyForRValue(Expr *E) { |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7114 | assert(E->getValueKind() == VK_LValue && |
| 7115 | E->getObjectKind() == OK_ObjCProperty); |
| 7116 | const ObjCPropertyRefExpr *PRE = E->getObjCProperty(); |
| 7117 | |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 7118 | QualType T = E->getType(); |
| 7119 | QualType ReceiverType; |
| 7120 | if (PRE->isObjectReceiver()) |
| 7121 | ReceiverType = PRE->getBase()->getType(); |
| 7122 | else if (PRE->isSuperReceiver()) |
| 7123 | ReceiverType = PRE->getSuperReceiverType(); |
| 7124 | else |
| 7125 | ReceiverType = Context.getObjCInterfaceType(PRE->getClassReceiver()); |
| 7126 | |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7127 | ExprValueKind VK = VK_RValue; |
| 7128 | if (PRE->isImplicitProperty()) { |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 7129 | if (ObjCMethodDecl *GetterMethod = |
Fariborz Jahanian | 0f0b302 | 2010-12-22 19:46:35 +0000 | [diff] [blame] | 7130 | PRE->getImplicitPropertyGetter()) { |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 7131 | T = getMessageSendResultType(ReceiverType, GetterMethod, |
| 7132 | PRE->isClassReceiver(), |
| 7133 | PRE->isSuperReceiver()); |
| 7134 | VK = Expr::getValueKindForType(GetterMethod->getResultType()); |
Fariborz Jahanian | 0f0b302 | 2010-12-22 19:46:35 +0000 | [diff] [blame] | 7135 | } |
| 7136 | else { |
| 7137 | Diag(PRE->getLocation(), diag::err_getter_not_found) |
| 7138 | << PRE->getBase()->getType(); |
| 7139 | } |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7140 | } |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 7141 | |
| 7142 | E = ImplicitCastExpr::Create(Context, T, CK_GetObjCProperty, |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7143 | E, 0, VK); |
John McCall | 4f26cd8 | 2010-12-10 01:49:45 +0000 | [diff] [blame] | 7144 | |
| 7145 | ExprResult Result = MaybeBindToTemporary(E); |
| 7146 | if (!Result.isInvalid()) |
| 7147 | E = Result.take(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7148 | |
| 7149 | return Owned(E); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7150 | } |
| 7151 | |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 7152 | void Sema::ConvertPropertyForLValue(ExprResult &LHS, ExprResult &RHS, |
| 7153 | QualType &LHSTy) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7154 | assert(LHS.get()->getValueKind() == VK_LValue && |
| 7155 | LHS.get()->getObjectKind() == OK_ObjCProperty); |
| 7156 | const ObjCPropertyRefExpr *PropRef = LHS.get()->getObjCProperty(); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7157 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7158 | bool Consumed = false; |
| 7159 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7160 | if (PropRef->isImplicitProperty()) { |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7161 | // If using property-dot syntax notation for assignment, and there is a |
| 7162 | // setter, RHS expression is being passed to the setter argument. So, |
| 7163 | // type conversion (and comparison) is RHS to setter's argument type. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7164 | if (const ObjCMethodDecl *SetterMD = PropRef->getImplicitPropertySetter()) { |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7165 | ObjCMethodDecl::param_iterator P = SetterMD->param_begin(); |
| 7166 | LHSTy = (*P)->getType(); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7167 | Consumed = (getLangOptions().ObjCAutoRefCount && |
| 7168 | (*P)->hasAttr<NSConsumedAttr>()); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7169 | |
| 7170 | // Otherwise, if the getter returns an l-value, just call that. |
| 7171 | } else { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7172 | QualType Result = PropRef->getImplicitPropertyGetter()->getResultType(); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7173 | ExprValueKind VK = Expr::getValueKindForType(Result); |
| 7174 | if (VK == VK_LValue) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7175 | LHS = ImplicitCastExpr::Create(Context, LHS.get()->getType(), |
| 7176 | CK_GetObjCProperty, LHS.take(), 0, VK); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7177 | return; |
John McCall | b7bd14f | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 7178 | } |
Fariborz Jahanian | 805b74e | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 7179 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7180 | } else if (getLangOptions().ObjCAutoRefCount) { |
| 7181 | const ObjCMethodDecl *setter |
| 7182 | = PropRef->getExplicitProperty()->getSetterMethodDecl(); |
| 7183 | if (setter) { |
| 7184 | ObjCMethodDecl::param_iterator P = setter->param_begin(); |
| 7185 | LHSTy = (*P)->getType(); |
| 7186 | Consumed = (*P)->hasAttr<NSConsumedAttr>(); |
| 7187 | } |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7188 | } |
| 7189 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7190 | if ((getLangOptions().CPlusPlus && LHSTy->isRecordType()) || |
| 7191 | getLangOptions().ObjCAutoRefCount) { |
Fariborz Jahanian | 805b74e | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 7192 | InitializedEntity Entity = |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7193 | InitializedEntity::InitializeParameter(Context, LHSTy, Consumed); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7194 | ExprResult ArgE = PerformCopyInitialization(Entity, SourceLocation(), RHS); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7195 | if (!ArgE.isInvalid()) { |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7196 | RHS = ArgE; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7197 | if (getLangOptions().ObjCAutoRefCount && !PropRef->isSuperReceiver()) |
| 7198 | checkRetainCycles(const_cast<Expr*>(PropRef->getBase()), RHS.get()); |
| 7199 | } |
Fariborz Jahanian | 805b74e | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 7200 | } |
| 7201 | } |
| 7202 | |
| 7203 | |
Anders Carlsson | 806700f | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 7204 | /// getPrimaryDecl - Helper function for CheckAddressOfOperand(). |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 7205 | /// This routine allows us to typecheck complex/recursive expressions |
Daniel Dunbar | b692ef4 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7206 | /// where the declaration is needed for type checking. We only need to |
| 7207 | /// handle cases when the expression references a function designator |
| 7208 | /// or is an lvalue. Here are some examples: |
| 7209 | /// - &(x) => x |
| 7210 | /// - &*****f => f for f a function designator. |
| 7211 | /// - &s.xx => s |
| 7212 | /// - &s.zz[1].yy -> s, if zz is an array |
| 7213 | /// - *(x + 1) -> x, if x is an array |
| 7214 | /// - &"123"[2] -> 0 |
| 7215 | /// - & __real__ x -> x |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7216 | static ValueDecl *getPrimaryDecl(Expr *E) { |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7217 | switch (E->getStmtClass()) { |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 7218 | case Stmt::DeclRefExprClass: |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7219 | return cast<DeclRefExpr>(E)->getDecl(); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 7220 | case Stmt::MemberExprClass: |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7221 | // If this is an arrow operator, the address is an offset from |
| 7222 | // the base's value, so the object the base refers to is |
| 7223 | // irrelevant. |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7224 | if (cast<MemberExpr>(E)->isArrow()) |
Chris Lattner | 48d5284 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 7225 | return 0; |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7226 | // Otherwise, the expression refers to a part of the base |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7227 | return getPrimaryDecl(cast<MemberExpr>(E)->getBase()); |
Anders Carlsson | 806700f | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 7228 | case Stmt::ArraySubscriptExprClass: { |
Mike Stump | 87c57ac | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 7229 | // FIXME: This code shouldn't be necessary! We should catch the implicit |
| 7230 | // promotion of register arrays earlier. |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7231 | Expr* Base = cast<ArraySubscriptExpr>(E)->getBase(); |
| 7232 | if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Base)) { |
| 7233 | if (ICE->getSubExpr()->getType()->isArrayType()) |
| 7234 | return getPrimaryDecl(ICE->getSubExpr()); |
| 7235 | } |
| 7236 | return 0; |
Anders Carlsson | 806700f | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 7237 | } |
Daniel Dunbar | b692ef4 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7238 | case Stmt::UnaryOperatorClass: { |
| 7239 | UnaryOperator *UO = cast<UnaryOperator>(E); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7240 | |
Daniel Dunbar | b692ef4 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7241 | switch(UO->getOpcode()) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7242 | case UO_Real: |
| 7243 | case UO_Imag: |
| 7244 | case UO_Extension: |
Daniel Dunbar | b692ef4 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7245 | return getPrimaryDecl(UO->getSubExpr()); |
| 7246 | default: |
| 7247 | return 0; |
| 7248 | } |
| 7249 | } |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 7250 | case Stmt::ParenExprClass: |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7251 | return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr()); |
Chris Lattner | 48d5284 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 7252 | case Stmt::ImplicitCastExprClass: |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7253 | // If the result of an implicit cast is an l-value, we care about |
| 7254 | // the sub-expression; otherwise, the result here doesn't matter. |
Chris Lattner | 24d5bfe | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7255 | return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr()); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 7256 | default: |
| 7257 | return 0; |
| 7258 | } |
| 7259 | } |
| 7260 | |
| 7261 | /// CheckAddressOfOperand - The operand of & must be either a function |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7262 | /// 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] | 7263 | /// 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] | 7264 | /// Note: The usual conversions are *not* applied to the operand of the & |
Steve Naroff | a78fe7e | 2007-05-16 19:47:19 +0000 | [diff] [blame] | 7265 | /// 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] | 7266 | /// 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] | 7267 | /// we allow the '&' but retain the overloaded-function type. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7268 | static QualType CheckAddressOfOperand(Sema &S, Expr *OrigOp, |
| 7269 | SourceLocation OpLoc) { |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7270 | if (OrigOp->isTypeDependent()) |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7271 | return S.Context.DependentTy; |
| 7272 | if (OrigOp->getType() == S.Context.OverloadTy) |
| 7273 | return S.Context.OverloadTy; |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 7274 | if (OrigOp->getType() == S.Context.UnknownAnyTy) |
| 7275 | return S.Context.UnknownAnyTy; |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 7276 | if (OrigOp->getType() == S.Context.BoundMemberTy) { |
| 7277 | S.Diag(OpLoc, diag::err_invalid_form_pointer_member_function) |
| 7278 | << OrigOp->getSourceRange(); |
| 7279 | return QualType(); |
| 7280 | } |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7281 | |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 7282 | assert(!OrigOp->getType()->isPlaceholderType()); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7283 | |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7284 | // Make sure to ignore parentheses in subsequent checks |
| 7285 | Expr *op = OrigOp->IgnoreParens(); |
Douglas Gregor | 19b8c4f | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 7286 | |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7287 | if (S.getLangOptions().C99) { |
Steve Naroff | 826e91a | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 7288 | // Implement C99-only parts of addressof rules. |
| 7289 | if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7290 | if (uOp->getOpcode() == UO_Deref) |
Steve Naroff | 826e91a | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 7291 | // Per C99 6.5.3.2, the address of a deref always returns a valid result |
| 7292 | // (assuming the deref expression is valid). |
| 7293 | return uOp->getSubExpr()->getType(); |
| 7294 | } |
| 7295 | // Technically, there should be a check for array subscript |
| 7296 | // expressions here, but the result of one is always an lvalue anyway. |
| 7297 | } |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7298 | ValueDecl *dcl = getPrimaryDecl(op); |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 7299 | Expr::LValueClassification lval = op->ClassifyLValue(S.Context); |
Nuno Lopes | 17f345f | 2008-12-16 22:59:47 +0000 | [diff] [blame] | 7300 | |
Fariborz Jahanian | 071caef | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 7301 | if (lval == Expr::LV_ClassTemporary) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7302 | bool sfinae = S.isSFINAEContext(); |
| 7303 | S.Diag(OpLoc, sfinae ? diag::err_typecheck_addrof_class_temporary |
| 7304 | : diag::ext_typecheck_addrof_class_temporary) |
Douglas Gregor | b154fdc | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 7305 | << op->getType() << op->getSourceRange(); |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7306 | if (sfinae) |
Douglas Gregor | b154fdc | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 7307 | return QualType(); |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7308 | } else if (isa<ObjCSelectorExpr>(op)) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7309 | return S.Context.getPointerType(op->getType()); |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7310 | } else if (lval == Expr::LV_MemberFunction) { |
| 7311 | // If it's an instance method, make a member pointer. |
| 7312 | // The expression must have exactly the form &A::foo. |
| 7313 | |
| 7314 | // If the underlying expression isn't a decl ref, give up. |
| 7315 | if (!isa<DeclRefExpr>(op)) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7316 | S.Diag(OpLoc, diag::err_invalid_form_pointer_member_function) |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7317 | << OrigOp->getSourceRange(); |
| 7318 | return QualType(); |
| 7319 | } |
| 7320 | DeclRefExpr *DRE = cast<DeclRefExpr>(op); |
| 7321 | CXXMethodDecl *MD = cast<CXXMethodDecl>(DRE->getDecl()); |
| 7322 | |
| 7323 | // The id-expression was parenthesized. |
| 7324 | if (OrigOp != DRE) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7325 | S.Diag(OpLoc, diag::err_parens_pointer_member_function) |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7326 | << OrigOp->getSourceRange(); |
| 7327 | |
| 7328 | // The method was named without a qualifier. |
| 7329 | } else if (!DRE->getQualifier()) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7330 | S.Diag(OpLoc, diag::err_unqualified_pointer_member_function) |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7331 | << op->getSourceRange(); |
| 7332 | } |
| 7333 | |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7334 | return S.Context.getMemberPointerType(op->getType(), |
| 7335 | S.Context.getTypeDeclType(MD->getParent()).getTypePtr()); |
John McCall | 8d08b9b | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7336 | } else if (lval != Expr::LV_Valid && lval != Expr::LV_IncompleteVoidType) { |
Eli Friedman | ce7f900 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 7337 | // C99 6.5.3.2p1 |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7338 | // The operand must be either an l-value or a function designator |
Eli Friedman | ce7f900 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 7339 | if (!op->getType()->isFunctionType()) { |
Chris Lattner | 48d5284 | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 7340 | // FIXME: emit more specific diag... |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7341 | S.Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof) |
Chris Lattner | f490e15 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 7342 | << op->getSourceRange(); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 7343 | return QualType(); |
| 7344 | } |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 7345 | } else if (op->getObjectKind() == OK_BitField) { // C99 6.5.3.2p1 |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7346 | // The operand cannot be a bit-field |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7347 | S.Diag(OpLoc, diag::err_typecheck_address_of) |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7348 | << "bit-field" << op->getSourceRange(); |
Douglas Gregor | 2eedc3a | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 7349 | return QualType(); |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 7350 | } else if (op->getObjectKind() == OK_VectorComponent) { |
Eli Friedman | 3a1e692 | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7351 | // The operand cannot be an element of a vector |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7352 | S.Diag(OpLoc, diag::err_typecheck_address_of) |
Nate Begeman | a6b47a4 | 2009-02-15 22:45:20 +0000 | [diff] [blame] | 7353 | << "vector element" << op->getSourceRange(); |
Steve Naroff | b96e4ab6 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 7354 | return QualType(); |
John McCall | 086a464 | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 7355 | } else if (op->getObjectKind() == OK_ObjCProperty) { |
Fariborz Jahanian | 385db80 | 2009-07-07 18:50:52 +0000 | [diff] [blame] | 7356 | // cannot take address of a property expression. |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7357 | S.Diag(OpLoc, diag::err_typecheck_address_of) |
Fariborz Jahanian | 385db80 | 2009-07-07 18:50:52 +0000 | [diff] [blame] | 7358 | << "property expression" << op->getSourceRange(); |
| 7359 | return QualType(); |
Steve Naroff | b96e4ab6 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 7360 | } else if (dcl) { // C99 6.5.3.2p1 |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7361 | // 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] | 7362 | // with the register storage-class specifier. |
| 7363 | if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { |
Fariborz Jahanian | e0fd5a9 | 2010-08-24 22:21:48 +0000 | [diff] [blame] | 7364 | // in C++ it is not error to take address of a register |
| 7365 | // variable (c++03 7.1.1P3) |
John McCall | 8e7d656 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 7366 | if (vd->getStorageClass() == SC_Register && |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7367 | !S.getLangOptions().CPlusPlus) { |
| 7368 | S.Diag(OpLoc, diag::err_typecheck_address_of) |
Chris Lattner | 29e812b | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 7369 | << "register variable" << op->getSourceRange(); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 7370 | return QualType(); |
| 7371 | } |
John McCall | d14a864 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7372 | } else if (isa<FunctionTemplateDecl>(dcl)) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7373 | return S.Context.OverloadTy; |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7374 | } else if (isa<FieldDecl>(dcl) || isa<IndirectFieldDecl>(dcl)) { |
Douglas Gregor | 9aa8b55 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 7375 | // Okay: we can take the address of a field. |
Sebastian Redl | 3d3f75a | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 7376 | // Could be a pointer to member, though, if there is an explicit |
| 7377 | // scope qualifier for the class. |
Douglas Gregor | 4bd90e5 | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 7378 | if (isa<DeclRefExpr>(op) && cast<DeclRefExpr>(op)->getQualifier()) { |
Sebastian Redl | 3d3f75a | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 7379 | DeclContext *Ctx = dcl->getDeclContext(); |
Anders Carlsson | 0b675f5 | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 7380 | if (Ctx && Ctx->isRecord()) { |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7381 | if (dcl->getType()->isReferenceType()) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7382 | S.Diag(OpLoc, |
| 7383 | diag::err_cannot_form_pointer_to_member_of_reference_type) |
John McCall | f3a8860 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7384 | << dcl->getDeclName() << dcl->getType(); |
Anders Carlsson | 0b675f5 | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 7385 | return QualType(); |
| 7386 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7387 | |
Argyrios Kyrtzidis | 8322b42 | 2011-01-31 07:04:29 +0000 | [diff] [blame] | 7388 | while (cast<RecordDecl>(Ctx)->isAnonymousStructOrUnion()) |
| 7389 | Ctx = Ctx->getParent(); |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7390 | return S.Context.getMemberPointerType(op->getType(), |
| 7391 | S.Context.getTypeDeclType(cast<RecordDecl>(Ctx)).getTypePtr()); |
Anders Carlsson | 0b675f5 | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 7392 | } |
Sebastian Redl | 3d3f75a | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 7393 | } |
Anders Carlsson | 5b53576 | 2009-05-16 21:43:42 +0000 | [diff] [blame] | 7394 | } else if (!isa<FunctionDecl>(dcl)) |
Steve Naroff | f633d09 | 2007-04-25 19:01:39 +0000 | [diff] [blame] | 7395 | assert(0 && "Unknown/unexpected decl type"); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 7396 | } |
Sebastian Redl | 18f8ff6 | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 7397 | |
Eli Friedman | ce7f900 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 7398 | if (lval == Expr::LV_IncompleteVoidType) { |
| 7399 | // Taking the address of a void variable is technically illegal, but we |
| 7400 | // allow it in cases which are otherwise valid. |
| 7401 | // Example: "extern void x; void* y = &x;". |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7402 | S.Diag(OpLoc, diag::ext_typecheck_addrof_void) << op->getSourceRange(); |
Eli Friedman | ce7f900 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 7403 | } |
| 7404 | |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 7405 | // 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] | 7406 | if (op->getType()->isObjCObjectType()) |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7407 | return S.Context.getObjCObjectPointerType(op->getType()); |
| 7408 | return S.Context.getPointerType(op->getType()); |
Steve Naroff | 4750051 | 2007-04-19 23:00:49 +0000 | [diff] [blame] | 7409 | } |
| 7410 | |
Chris Lattner | 9156f1b | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7411 | /// CheckIndirectionOperand - Type check unary indirection (prefix '*'). |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7412 | static QualType CheckIndirectionOperand(Sema &S, Expr *Op, ExprValueKind &VK, |
| 7413 | SourceLocation OpLoc) { |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7414 | if (Op->isTypeDependent()) |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7415 | return S.Context.DependentTy; |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7416 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7417 | ExprResult ConvResult = S.UsualUnaryConversions(Op); |
| 7418 | if (ConvResult.isInvalid()) |
| 7419 | return QualType(); |
| 7420 | Op = ConvResult.take(); |
Chris Lattner | 9156f1b | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7421 | QualType OpTy = Op->getType(); |
| 7422 | QualType Result; |
Argyrios Kyrtzidis | 69a2c92 | 2011-05-02 18:21:19 +0000 | [diff] [blame] | 7423 | |
| 7424 | if (isa<CXXReinterpretCastExpr>(Op)) { |
| 7425 | QualType OpOrigType = Op->IgnoreParenCasts()->getType(); |
| 7426 | S.CheckCompatibleReinterpretCast(OpOrigType, OpTy, /*IsDereference*/true, |
| 7427 | Op->getSourceRange()); |
| 7428 | } |
| 7429 | |
Chris Lattner | 9156f1b | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7430 | // Note that per both C89 and C99, indirection is always legal, even if OpTy |
| 7431 | // is an incomplete type or void. It would be possible to warn about |
| 7432 | // dereferencing a void pointer, but it's completely well-defined, and such a |
| 7433 | // warning is unlikely to catch any mistakes. |
| 7434 | if (const PointerType *PT = OpTy->getAs<PointerType>()) |
| 7435 | Result = PT->getPointeeType(); |
| 7436 | else if (const ObjCObjectPointerType *OPT = |
| 7437 | OpTy->getAs<ObjCObjectPointerType>()) |
| 7438 | Result = OPT->getPointeeType(); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7439 | else { |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 7440 | ExprResult PR = S.CheckPlaceholderExpr(Op); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7441 | if (PR.isInvalid()) return QualType(); |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7442 | if (PR.take() != Op) |
| 7443 | return CheckIndirectionOperand(S, PR.take(), VK, OpLoc); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7444 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7445 | |
Chris Lattner | 9156f1b | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7446 | if (Result.isNull()) { |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7447 | S.Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer) |
Chris Lattner | 9156f1b | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7448 | << OpTy << Op->getSourceRange(); |
| 7449 | return QualType(); |
| 7450 | } |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7451 | |
| 7452 | // Dereferences are usually l-values... |
| 7453 | VK = VK_LValue; |
| 7454 | |
| 7455 | // ...except that certain expressions are never l-values in C. |
Douglas Gregor | 5476205b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 7456 | if (!S.getLangOptions().CPlusPlus && Result.isCForbiddenLValueType()) |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7457 | VK = VK_RValue; |
Chris Lattner | 9156f1b | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7458 | |
| 7459 | return Result; |
Steve Naroff | 1926c83 | 2007-04-24 00:23:05 +0000 | [diff] [blame] | 7460 | } |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 7461 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7462 | static inline BinaryOperatorKind ConvertTokenKindToBinaryOpcode( |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 7463 | tok::TokenKind Kind) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7464 | BinaryOperatorKind Opc; |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 7465 | switch (Kind) { |
| 7466 | default: assert(0 && "Unknown binop!"); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7467 | case tok::periodstar: Opc = BO_PtrMemD; break; |
| 7468 | case tok::arrowstar: Opc = BO_PtrMemI; break; |
| 7469 | case tok::star: Opc = BO_Mul; break; |
| 7470 | case tok::slash: Opc = BO_Div; break; |
| 7471 | case tok::percent: Opc = BO_Rem; break; |
| 7472 | case tok::plus: Opc = BO_Add; break; |
| 7473 | case tok::minus: Opc = BO_Sub; break; |
| 7474 | case tok::lessless: Opc = BO_Shl; break; |
| 7475 | case tok::greatergreater: Opc = BO_Shr; break; |
| 7476 | case tok::lessequal: Opc = BO_LE; break; |
| 7477 | case tok::less: Opc = BO_LT; break; |
| 7478 | case tok::greaterequal: Opc = BO_GE; break; |
| 7479 | case tok::greater: Opc = BO_GT; break; |
| 7480 | case tok::exclaimequal: Opc = BO_NE; break; |
| 7481 | case tok::equalequal: Opc = BO_EQ; break; |
| 7482 | case tok::amp: Opc = BO_And; break; |
| 7483 | case tok::caret: Opc = BO_Xor; break; |
| 7484 | case tok::pipe: Opc = BO_Or; break; |
| 7485 | case tok::ampamp: Opc = BO_LAnd; break; |
| 7486 | case tok::pipepipe: Opc = BO_LOr; break; |
| 7487 | case tok::equal: Opc = BO_Assign; break; |
| 7488 | case tok::starequal: Opc = BO_MulAssign; break; |
| 7489 | case tok::slashequal: Opc = BO_DivAssign; break; |
| 7490 | case tok::percentequal: Opc = BO_RemAssign; break; |
| 7491 | case tok::plusequal: Opc = BO_AddAssign; break; |
| 7492 | case tok::minusequal: Opc = BO_SubAssign; break; |
| 7493 | case tok::lesslessequal: Opc = BO_ShlAssign; break; |
| 7494 | case tok::greatergreaterequal: Opc = BO_ShrAssign; break; |
| 7495 | case tok::ampequal: Opc = BO_AndAssign; break; |
| 7496 | case tok::caretequal: Opc = BO_XorAssign; break; |
| 7497 | case tok::pipeequal: Opc = BO_OrAssign; break; |
| 7498 | case tok::comma: Opc = BO_Comma; break; |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 7499 | } |
| 7500 | return Opc; |
| 7501 | } |
| 7502 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7503 | static inline UnaryOperatorKind ConvertTokenKindToUnaryOpcode( |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 7504 | tok::TokenKind Kind) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7505 | UnaryOperatorKind Opc; |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 7506 | switch (Kind) { |
| 7507 | default: assert(0 && "Unknown unary op!"); |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7508 | case tok::plusplus: Opc = UO_PreInc; break; |
| 7509 | case tok::minusminus: Opc = UO_PreDec; break; |
| 7510 | case tok::amp: Opc = UO_AddrOf; break; |
| 7511 | case tok::star: Opc = UO_Deref; break; |
| 7512 | case tok::plus: Opc = UO_Plus; break; |
| 7513 | case tok::minus: Opc = UO_Minus; break; |
| 7514 | case tok::tilde: Opc = UO_Not; break; |
| 7515 | case tok::exclaim: Opc = UO_LNot; break; |
| 7516 | case tok::kw___real: Opc = UO_Real; break; |
| 7517 | case tok::kw___imag: Opc = UO_Imag; break; |
| 7518 | case tok::kw___extension__: Opc = UO_Extension; break; |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 7519 | } |
| 7520 | return Opc; |
| 7521 | } |
| 7522 | |
Chandler Carruth | e0cee6a | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 7523 | /// DiagnoseSelfAssignment - Emits a warning if a value is assigned to itself. |
| 7524 | /// This warning is only emitted for builtin assignment operations. It is also |
| 7525 | /// suppressed in the event of macro expansions. |
| 7526 | static void DiagnoseSelfAssignment(Sema &S, Expr *lhs, Expr *rhs, |
| 7527 | SourceLocation OpLoc) { |
| 7528 | if (!S.ActiveTemplateInstantiations.empty()) |
| 7529 | return; |
| 7530 | if (OpLoc.isInvalid() || OpLoc.isMacroID()) |
| 7531 | return; |
| 7532 | lhs = lhs->IgnoreParenImpCasts(); |
| 7533 | rhs = rhs->IgnoreParenImpCasts(); |
| 7534 | const DeclRefExpr *LeftDeclRef = dyn_cast<DeclRefExpr>(lhs); |
| 7535 | const DeclRefExpr *RightDeclRef = dyn_cast<DeclRefExpr>(rhs); |
| 7536 | if (!LeftDeclRef || !RightDeclRef || |
| 7537 | LeftDeclRef->getLocation().isMacroID() || |
| 7538 | RightDeclRef->getLocation().isMacroID()) |
| 7539 | return; |
| 7540 | const ValueDecl *LeftDecl = |
| 7541 | cast<ValueDecl>(LeftDeclRef->getDecl()->getCanonicalDecl()); |
| 7542 | const ValueDecl *RightDecl = |
| 7543 | cast<ValueDecl>(RightDeclRef->getDecl()->getCanonicalDecl()); |
| 7544 | if (LeftDecl != RightDecl) |
| 7545 | return; |
| 7546 | if (LeftDecl->getType().isVolatileQualified()) |
| 7547 | return; |
| 7548 | if (const ReferenceType *RefTy = LeftDecl->getType()->getAs<ReferenceType>()) |
| 7549 | if (RefTy->getPointeeType().isVolatileQualified()) |
| 7550 | return; |
| 7551 | |
| 7552 | S.Diag(OpLoc, diag::warn_self_assignment) |
| 7553 | << LeftDeclRef->getType() |
| 7554 | << lhs->getSourceRange() << rhs->getSourceRange(); |
| 7555 | } |
| 7556 | |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7557 | /// CreateBuiltinBinOp - Creates a new built-in binary operation with |
| 7558 | /// operator @p Opc at location @c TokLoc. This routine only supports |
| 7559 | /// built-in operations; ActOnBinOp handles overloaded operators. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7560 | ExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, |
Argyrios Kyrtzidis | 7a808c0 | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 7561 | BinaryOperatorKind Opc, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7562 | Expr *lhsExpr, Expr *rhsExpr) { |
| 7563 | ExprResult lhs = Owned(lhsExpr), rhs = Owned(rhsExpr); |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7564 | QualType ResultTy; // Result type of the binary operator. |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7565 | // The following two variables are used for compound assignment operators |
| 7566 | QualType CompLHSTy; // Type of LHS after promotions for computation |
| 7567 | QualType CompResultTy; // Type of computation result |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7568 | ExprValueKind VK = VK_RValue; |
| 7569 | ExprObjectKind OK = OK_Ordinary; |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7570 | |
Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 7571 | // Check if a 'foo<int>' involved in a binary op, identifies a single |
| 7572 | // function unambiguously (i.e. an lvalue ala 13.4) |
| 7573 | // But since an assignment can trigger target based overload, exclude it in |
| 7574 | // our blind search. i.e: |
| 7575 | // template<class T> void f(); template<class T, class U> void f(U); |
| 7576 | // f<int> == 0; // resolve f<int> blindly |
| 7577 | // void (*p)(int); p = f<int>; // resolve f<int> using target |
| 7578 | if (Opc != BO_Assign) { |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 7579 | ExprResult resolvedLHS = CheckPlaceholderExpr(lhs.get()); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 7580 | if (!resolvedLHS.isUsable()) return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7581 | lhs = move(resolvedLHS); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 7582 | |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 7583 | ExprResult resolvedRHS = CheckPlaceholderExpr(rhs.get()); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 7584 | if (!resolvedRHS.isUsable()) return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7585 | rhs = move(resolvedRHS); |
Douglas Gregor | 1beec45 | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 7586 | } |
| 7587 | |
Eli Friedman | 8e6f5a6 | 2011-06-17 20:52:22 +0000 | [diff] [blame] | 7588 | // The canonical way to check for a GNU null is with isNullPointerConstant, |
| 7589 | // but we use a bit of a hack here for speed; this is a relatively |
| 7590 | // hot path, and isNullPointerConstant is slow. |
| 7591 | bool LeftNull = isa<GNUNullExpr>(lhs.get()->IgnoreParenImpCasts()); |
| 7592 | bool RightNull = isa<GNUNullExpr>(rhs.get()->IgnoreParenImpCasts()); |
Richard Trieu | 701fb36 | 2011-06-16 21:36:56 +0000 | [diff] [blame] | 7593 | |
| 7594 | // Detect when a NULL constant is used improperly in an expression. These |
| 7595 | // are mainly cases where the null pointer is used as an integer instead |
| 7596 | // of a pointer. |
| 7597 | if (LeftNull || RightNull) { |
Chandler Carruth | 4f04b43 | 2011-06-20 07:38:51 +0000 | [diff] [blame] | 7598 | // Avoid analyzing cases where the result will either be invalid (and |
| 7599 | // diagnosed as such) or entirely valid and not something to warn about. |
| 7600 | QualType LeftType = lhs.get()->getType(); |
| 7601 | QualType RightType = rhs.get()->getType(); |
| 7602 | if (!LeftType->isBlockPointerType() && !LeftType->isMemberPointerType() && |
| 7603 | !LeftType->isFunctionType() && |
| 7604 | !RightType->isBlockPointerType() && |
| 7605 | !RightType->isMemberPointerType() && |
| 7606 | !RightType->isFunctionType()) { |
| 7607 | if (Opc == BO_Mul || Opc == BO_Div || Opc == BO_Rem || Opc == BO_Add || |
| 7608 | Opc == BO_Sub || Opc == BO_Shl || Opc == BO_Shr || Opc == BO_And || |
| 7609 | Opc == BO_Xor || Opc == BO_Or || Opc == BO_MulAssign || |
| 7610 | Opc == BO_DivAssign || Opc == BO_AddAssign || Opc == BO_SubAssign || |
| 7611 | Opc == BO_RemAssign || Opc == BO_ShlAssign || Opc == BO_ShrAssign || |
| 7612 | Opc == BO_AndAssign || Opc == BO_OrAssign || Opc == BO_XorAssign) { |
| 7613 | // These are the operations that would not make sense with a null pointer |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 7614 | // pointer no matter what the other expression is. |
Chandler Carruth | e1db1cf | 2011-06-19 09:05:14 +0000 | [diff] [blame] | 7615 | Diag(OpLoc, diag::warn_null_in_arithmetic_operation) |
Chandler Carruth | 4f04b43 | 2011-06-20 07:38:51 +0000 | [diff] [blame] | 7616 | << (LeftNull ? lhs.get()->getSourceRange() : SourceRange()) |
| 7617 | << (RightNull ? rhs.get()->getSourceRange() : SourceRange()); |
| 7618 | } else if (Opc == BO_LE || Opc == BO_LT || Opc == BO_GE || Opc == BO_GT || |
| 7619 | Opc == BO_EQ || Opc == BO_NE) { |
| 7620 | // These are the operations that would not make sense with a null pointer |
| 7621 | // if the other expression the other expression is not a pointer. |
| 7622 | if (LeftNull != RightNull && |
| 7623 | !LeftType->isAnyPointerType() && |
| 7624 | !LeftType->canDecayToPointerType() && |
| 7625 | !RightType->isAnyPointerType() && |
| 7626 | !RightType->canDecayToPointerType()) { |
| 7627 | Diag(OpLoc, diag::warn_null_in_arithmetic_operation) |
| 7628 | << (LeftNull ? lhs.get()->getSourceRange() |
| 7629 | : rhs.get()->getSourceRange()); |
| 7630 | } |
Richard Trieu | 701fb36 | 2011-06-16 21:36:56 +0000 | [diff] [blame] | 7631 | } |
| 7632 | } |
| 7633 | } |
| 7634 | |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7635 | switch (Opc) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7636 | case BO_Assign: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7637 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, QualType()); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7638 | if (getLangOptions().CPlusPlus && |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7639 | lhs.get()->getObjectKind() != OK_ObjCProperty) { |
| 7640 | VK = lhs.get()->getValueKind(); |
| 7641 | OK = lhs.get()->getObjectKind(); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7642 | } |
Chandler Carruth | e0cee6a | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 7643 | if (!ResultTy.isNull()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7644 | DiagnoseSelfAssignment(*this, lhs.get(), rhs.get(), OpLoc); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7645 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7646 | case BO_PtrMemD: |
| 7647 | case BO_PtrMemI: |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7648 | ResultTy = CheckPointerToMemberOperands(lhs, rhs, VK, OpLoc, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7649 | Opc == BO_PtrMemI); |
Sebastian Redl | 112a9766 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 7650 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7651 | case BO_Mul: |
| 7652 | case BO_Div: |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 7653 | ResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc, false, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7654 | Opc == BO_Div); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7655 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7656 | case BO_Rem: |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7657 | ResultTy = CheckRemainderOperands(lhs, rhs, OpLoc); |
| 7658 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7659 | case BO_Add: |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7660 | ResultTy = CheckAdditionOperands(lhs, rhs, OpLoc); |
| 7661 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7662 | case BO_Sub: |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7663 | ResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc); |
| 7664 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7665 | case BO_Shl: |
| 7666 | case BO_Shr: |
Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7667 | ResultTy = CheckShiftOperands(lhs, rhs, OpLoc, Opc); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7668 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7669 | case BO_LE: |
| 7670 | case BO_LT: |
| 7671 | case BO_GE: |
| 7672 | case BO_GT: |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7673 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, true); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7674 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7675 | case BO_EQ: |
| 7676 | case BO_NE: |
Douglas Gregor | 7a5bc76 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7677 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, false); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7678 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7679 | case BO_And: |
| 7680 | case BO_Xor: |
| 7681 | case BO_Or: |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7682 | ResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc); |
| 7683 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7684 | case BO_LAnd: |
| 7685 | case BO_LOr: |
Chris Lattner | 8406c51 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 7686 | ResultTy = CheckLogicalOperands(lhs, rhs, OpLoc, Opc); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7687 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7688 | case BO_MulAssign: |
| 7689 | case BO_DivAssign: |
Chris Lattner | faa5417 | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 7690 | CompResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc, true, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7691 | Opc == BO_DivAssign); |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7692 | CompLHSTy = CompResultTy; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7693 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 7694 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7695 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7696 | case BO_RemAssign: |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7697 | CompResultTy = CheckRemainderOperands(lhs, rhs, OpLoc, true); |
| 7698 | CompLHSTy = CompResultTy; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7699 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 7700 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7701 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7702 | case BO_AddAssign: |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7703 | CompResultTy = CheckAdditionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7704 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 7705 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7706 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7707 | case BO_SubAssign: |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7708 | CompResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7709 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 7710 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7711 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7712 | case BO_ShlAssign: |
| 7713 | case BO_ShrAssign: |
Chandler Carruth | 4c6fdca | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7714 | CompResultTy = CheckShiftOperands(lhs, rhs, OpLoc, Opc, true); |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7715 | CompLHSTy = CompResultTy; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7716 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 7717 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7718 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7719 | case BO_AndAssign: |
| 7720 | case BO_XorAssign: |
| 7721 | case BO_OrAssign: |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7722 | CompResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc, true); |
| 7723 | CompLHSTy = CompResultTy; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7724 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 7725 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7726 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7727 | case BO_Comma: |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7728 | ResultTy = CheckCommaOperands(*this, lhs, rhs, OpLoc); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7729 | if (getLangOptions().CPlusPlus && !rhs.isInvalid()) { |
| 7730 | VK = rhs.get()->getValueKind(); |
| 7731 | OK = rhs.get()->getObjectKind(); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7732 | } |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7733 | break; |
| 7734 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7735 | if (ResultTy.isNull() || lhs.isInvalid() || rhs.isInvalid()) |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 7736 | return ExprError(); |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 7737 | |
| 7738 | // Check for array bounds violations for both sides of the BinaryOperator |
| 7739 | CheckArrayAccess(lhs.get()); |
| 7740 | CheckArrayAccess(rhs.get()); |
| 7741 | |
Eli Friedman | 8b7b1b1 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7742 | if (CompResultTy.isNull()) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7743 | return Owned(new (Context) BinaryOperator(lhs.take(), rhs.take(), Opc, |
| 7744 | ResultTy, VK, OK, OpLoc)); |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 7745 | if (getLangOptions().CPlusPlus && lhs.get()->getObjectKind() != |
| 7746 | OK_ObjCProperty) { |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7747 | VK = VK_LValue; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7748 | OK = lhs.get()->getObjectKind(); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7749 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7750 | return Owned(new (Context) CompoundAssignOperator(lhs.take(), rhs.take(), Opc, |
| 7751 | ResultTy, VK, OK, CompLHSTy, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7752 | CompResultTy, OpLoc)); |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7753 | } |
| 7754 | |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 7755 | /// DiagnoseBitwisePrecedence - Emit a warning when bitwise and comparison |
| 7756 | /// operators are mixed in a way that suggests that the programmer forgot that |
| 7757 | /// comparison operators have higher precedence. The most typical example of |
| 7758 | /// such code is "flags & 0x0020 != 0", which is equivalent to "flags & 1". |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7759 | static void DiagnoseBitwisePrecedence(Sema &Self, BinaryOperatorKind Opc, |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7760 | SourceLocation OpLoc,Expr *lhs,Expr *rhs){ |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 7761 | typedef BinaryOperator BinOp; |
| 7762 | BinOp::Opcode lhsopc = static_cast<BinOp::Opcode>(-1), |
| 7763 | rhsopc = static_cast<BinOp::Opcode>(-1); |
| 7764 | if (BinOp *BO = dyn_cast<BinOp>(lhs)) |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7765 | lhsopc = BO->getOpcode(); |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 7766 | if (BinOp *BO = dyn_cast<BinOp>(rhs)) |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7767 | rhsopc = BO->getOpcode(); |
| 7768 | |
| 7769 | // Subs are not binary operators. |
| 7770 | if (lhsopc == -1 && rhsopc == -1) |
| 7771 | return; |
| 7772 | |
| 7773 | // Bitwise operations are sometimes used as eager logical ops. |
| 7774 | // Don't diagnose this. |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 7775 | if ((BinOp::isComparisonOp(lhsopc) || BinOp::isBitwiseOp(lhsopc)) && |
| 7776 | (BinOp::isComparisonOp(rhsopc) || BinOp::isBitwiseOp(rhsopc))) |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7777 | return; |
| 7778 | |
Chandler Carruth | b00e8c0 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 7779 | if (BinOp::isComparisonOp(lhsopc)) { |
| 7780 | Self.Diag(OpLoc, diag::warn_precedence_bitwise_rel) |
| 7781 | << SourceRange(lhs->getLocStart(), OpLoc) |
| 7782 | << BinOp::getOpcodeStr(Opc) << BinOp::getOpcodeStr(lhsopc); |
Sebastian Redl | 4afb7c58 | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 7783 | SuggestParentheses(Self, OpLoc, |
Douglas Gregor | 2bf2d3d | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 7784 | Self.PDiag(diag::note_precedence_bitwise_silence) |
| 7785 | << BinOp::getOpcodeStr(lhsopc), |
Chandler Carruth | b00e8c0 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 7786 | lhs->getSourceRange()); |
| 7787 | SuggestParentheses(Self, OpLoc, |
Argyrios Kyrtzidis | e1b97c4 | 2011-04-25 23:01:29 +0000 | [diff] [blame] | 7788 | Self.PDiag(diag::note_precedence_bitwise_first) |
| 7789 | << BinOp::getOpcodeStr(Opc), |
| 7790 | SourceRange(cast<BinOp>(lhs)->getRHS()->getLocStart(), rhs->getLocEnd())); |
Chandler Carruth | b00e8c0 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 7791 | } else if (BinOp::isComparisonOp(rhsopc)) { |
| 7792 | Self.Diag(OpLoc, diag::warn_precedence_bitwise_rel) |
| 7793 | << SourceRange(OpLoc, rhs->getLocEnd()) |
| 7794 | << BinOp::getOpcodeStr(Opc) << BinOp::getOpcodeStr(rhsopc); |
Sebastian Redl | 4afb7c58 | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 7795 | SuggestParentheses(Self, OpLoc, |
Argyrios Kyrtzidis | e1b97c4 | 2011-04-25 23:01:29 +0000 | [diff] [blame] | 7796 | Self.PDiag(diag::note_precedence_bitwise_silence) |
| 7797 | << BinOp::getOpcodeStr(rhsopc), |
Chandler Carruth | b00e8c0 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 7798 | rhs->getSourceRange()); |
| 7799 | SuggestParentheses(Self, OpLoc, |
Douglas Gregor | 8933623 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 7800 | Self.PDiag(diag::note_precedence_bitwise_first) |
Douglas Gregor | fa1e36d | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 7801 | << BinOp::getOpcodeStr(Opc), |
Douglas Gregor | bb9a5e6 | 2011-06-22 18:41:08 +0000 | [diff] [blame] | 7802 | SourceRange(lhs->getLocStart(), |
| 7803 | cast<BinOp>(rhs)->getLHS()->getLocStart())); |
Chandler Carruth | b00e8c0 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 7804 | } |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7805 | } |
| 7806 | |
Argyrios Kyrtzidis | 01bf777 | 2011-06-20 18:41:26 +0000 | [diff] [blame] | 7807 | /// \brief It accepts a '&' expr that is inside a '|' one. |
| 7808 | /// Emit a diagnostic together with a fixit hint that wraps the '&' expression |
| 7809 | /// in parentheses. |
| 7810 | static void |
| 7811 | EmitDiagnosticForBitwiseAndInBitwiseOr(Sema &Self, SourceLocation OpLoc, |
| 7812 | BinaryOperator *Bop) { |
| 7813 | assert(Bop->getOpcode() == BO_And); |
| 7814 | Self.Diag(Bop->getOperatorLoc(), diag::warn_bitwise_and_in_bitwise_or) |
| 7815 | << Bop->getSourceRange() << OpLoc; |
| 7816 | SuggestParentheses(Self, Bop->getOperatorLoc(), |
| 7817 | Self.PDiag(diag::note_bitwise_and_in_bitwise_or_silence), |
| 7818 | Bop->getSourceRange()); |
| 7819 | } |
| 7820 | |
Argyrios Kyrtzidis | 14a9662 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7821 | /// \brief It accepts a '&&' expr that is inside a '||' one. |
| 7822 | /// Emit a diagnostic together with a fixit hint that wraps the '&&' expression |
| 7823 | /// in parentheses. |
| 7824 | static void |
| 7825 | EmitDiagnosticForLogicalAndInLogicalOr(Sema &Self, SourceLocation OpLoc, |
Argyrios Kyrtzidis | ad8b4d4 | 2011-04-22 19:16:27 +0000 | [diff] [blame] | 7826 | BinaryOperator *Bop) { |
| 7827 | assert(Bop->getOpcode() == BO_LAnd); |
Chandler Carruth | b00e8c0 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 7828 | Self.Diag(Bop->getOperatorLoc(), diag::warn_logical_and_in_logical_or) |
| 7829 | << Bop->getSourceRange() << OpLoc; |
Argyrios Kyrtzidis | ad8b4d4 | 2011-04-22 19:16:27 +0000 | [diff] [blame] | 7830 | SuggestParentheses(Self, Bop->getOperatorLoc(), |
Argyrios Kyrtzidis | 14a9662 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7831 | Self.PDiag(diag::note_logical_and_in_logical_or_silence), |
Chandler Carruth | b00e8c0 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 7832 | Bop->getSourceRange()); |
Argyrios Kyrtzidis | 14a9662 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7833 | } |
| 7834 | |
| 7835 | /// \brief Returns true if the given expression can be evaluated as a constant |
| 7836 | /// 'true'. |
| 7837 | static bool EvaluatesAsTrue(Sema &S, Expr *E) { |
| 7838 | bool Res; |
| 7839 | return E->EvaluateAsBooleanCondition(Res, S.getASTContext()) && Res; |
| 7840 | } |
| 7841 | |
Argyrios Kyrtzidis | 56e879d | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 7842 | /// \brief Returns true if the given expression can be evaluated as a constant |
| 7843 | /// 'false'. |
| 7844 | static bool EvaluatesAsFalse(Sema &S, Expr *E) { |
| 7845 | bool Res; |
| 7846 | return E->EvaluateAsBooleanCondition(Res, S.getASTContext()) && !Res; |
| 7847 | } |
| 7848 | |
Argyrios Kyrtzidis | 14a9662 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7849 | /// \brief Look for '&&' in the left hand of a '||' expr. |
| 7850 | static void DiagnoseLogicalAndInLogicalOrLHS(Sema &S, SourceLocation OpLoc, |
Argyrios Kyrtzidis | 56e879d | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 7851 | Expr *OrLHS, Expr *OrRHS) { |
| 7852 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(OrLHS)) { |
Argyrios Kyrtzidis | f89a56c | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 7853 | if (Bop->getOpcode() == BO_LAnd) { |
Argyrios Kyrtzidis | 56e879d | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 7854 | // If it's "a && b || 0" don't warn since the precedence doesn't matter. |
| 7855 | if (EvaluatesAsFalse(S, OrRHS)) |
| 7856 | return; |
Argyrios Kyrtzidis | 14a9662 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7857 | // If it's "1 && a || b" don't warn since the precedence doesn't matter. |
| 7858 | if (!EvaluatesAsTrue(S, Bop->getLHS())) |
| 7859 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, Bop); |
| 7860 | } else if (Bop->getOpcode() == BO_LOr) { |
| 7861 | if (BinaryOperator *RBop = dyn_cast<BinaryOperator>(Bop->getRHS())) { |
| 7862 | // If it's "a || b && 1 || c" we didn't warn earlier for |
| 7863 | // "a || b && 1", but warn now. |
| 7864 | if (RBop->getOpcode() == BO_LAnd && EvaluatesAsTrue(S, RBop->getRHS())) |
| 7865 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, RBop); |
| 7866 | } |
| 7867 | } |
| 7868 | } |
| 7869 | } |
| 7870 | |
| 7871 | /// \brief Look for '&&' in the right hand of a '||' expr. |
| 7872 | static void DiagnoseLogicalAndInLogicalOrRHS(Sema &S, SourceLocation OpLoc, |
Argyrios Kyrtzidis | 56e879d | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 7873 | Expr *OrLHS, Expr *OrRHS) { |
| 7874 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(OrRHS)) { |
Argyrios Kyrtzidis | 14a9662 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7875 | if (Bop->getOpcode() == BO_LAnd) { |
Argyrios Kyrtzidis | 56e879d | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 7876 | // If it's "0 || a && b" don't warn since the precedence doesn't matter. |
| 7877 | if (EvaluatesAsFalse(S, OrLHS)) |
| 7878 | return; |
Argyrios Kyrtzidis | 14a9662 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7879 | // If it's "a || b && 1" don't warn since the precedence doesn't matter. |
| 7880 | if (!EvaluatesAsTrue(S, Bop->getRHS())) |
| 7881 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, Bop); |
Argyrios Kyrtzidis | f89a56c | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 7882 | } |
| 7883 | } |
| 7884 | } |
| 7885 | |
Argyrios Kyrtzidis | 01bf777 | 2011-06-20 18:41:26 +0000 | [diff] [blame] | 7886 | /// \brief Look for '&' in the left or right hand of a '|' expr. |
| 7887 | static void DiagnoseBitwiseAndInBitwiseOr(Sema &S, SourceLocation OpLoc, |
| 7888 | Expr *OrArg) { |
| 7889 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(OrArg)) { |
| 7890 | if (Bop->getOpcode() == BO_And) |
| 7891 | return EmitDiagnosticForBitwiseAndInBitwiseOr(S, OpLoc, Bop); |
| 7892 | } |
| 7893 | } |
| 7894 | |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7895 | /// DiagnoseBinOpPrecedence - Emit warnings for expressions with tricky |
Argyrios Kyrtzidis | f89a56c | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 7896 | /// precedence. |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7897 | static void DiagnoseBinOpPrecedence(Sema &Self, BinaryOperatorKind Opc, |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7898 | SourceLocation OpLoc, Expr *lhs, Expr *rhs){ |
Argyrios Kyrtzidis | f89a56c | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 7899 | // Diagnose "arg1 'bitwise' arg2 'eq' arg3". |
Sebastian Redl | 4461507 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 7900 | if (BinaryOperator::isBitwiseOp(Opc)) |
Argyrios Kyrtzidis | 01bf777 | 2011-06-20 18:41:26 +0000 | [diff] [blame] | 7901 | DiagnoseBitwisePrecedence(Self, Opc, OpLoc, lhs, rhs); |
| 7902 | |
| 7903 | // Diagnose "arg1 & arg2 | arg3" |
| 7904 | if (Opc == BO_Or && !OpLoc.isMacroID()/* Don't warn in macros. */) { |
| 7905 | DiagnoseBitwiseAndInBitwiseOr(Self, OpLoc, lhs); |
| 7906 | DiagnoseBitwiseAndInBitwiseOr(Self, OpLoc, rhs); |
| 7907 | } |
Argyrios Kyrtzidis | f89a56c | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 7908 | |
Argyrios Kyrtzidis | 14a9662 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7909 | // Warn about arg1 || arg2 && arg3, as GCC 4.3+ does. |
| 7910 | // 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] | 7911 | if (Opc == BO_LOr && !OpLoc.isMacroID()/* Don't warn in macros. */) { |
Argyrios Kyrtzidis | 56e879d | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 7912 | DiagnoseLogicalAndInLogicalOrLHS(Self, OpLoc, lhs, rhs); |
| 7913 | DiagnoseLogicalAndInLogicalOrRHS(Self, OpLoc, lhs, rhs); |
Argyrios Kyrtzidis | f89a56c | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 7914 | } |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7915 | } |
| 7916 | |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 7917 | // Binary Operators. 'Tok' is the token for the operator. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7918 | ExprResult Sema::ActOnBinOp(Scope *S, SourceLocation TokLoc, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7919 | tok::TokenKind Kind, |
| 7920 | Expr *lhs, Expr *rhs) { |
| 7921 | BinaryOperatorKind Opc = ConvertTokenKindToBinaryOpcode(Kind); |
Steve Naroff | 83895f7 | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 7922 | assert((lhs != 0) && "ActOnBinOp(): missing left expression"); |
| 7923 | assert((rhs != 0) && "ActOnBinOp(): missing right expression"); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 7924 | |
Sebastian Redl | 4302824 | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7925 | // Emit warnings for tricky precedence issues, e.g. "bitfield & 0x4 == 0" |
| 7926 | DiagnoseBinOpPrecedence(*this, Opc, TokLoc, lhs, rhs); |
| 7927 | |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 7928 | return BuildBinOp(S, TokLoc, Opc, lhs, rhs); |
| 7929 | } |
| 7930 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7931 | ExprResult Sema::BuildBinOp(Scope *S, SourceLocation OpLoc, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7932 | BinaryOperatorKind Opc, |
| 7933 | Expr *lhs, Expr *rhs) { |
John McCall | 622114c | 2010-12-06 05:26:58 +0000 | [diff] [blame] | 7934 | if (getLangOptions().CPlusPlus) { |
| 7935 | bool UseBuiltinOperator; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7936 | |
John McCall | 622114c | 2010-12-06 05:26:58 +0000 | [diff] [blame] | 7937 | if (lhs->isTypeDependent() || rhs->isTypeDependent()) { |
| 7938 | UseBuiltinOperator = false; |
| 7939 | } else if (Opc == BO_Assign && lhs->getObjectKind() == OK_ObjCProperty) { |
| 7940 | UseBuiltinOperator = true; |
| 7941 | } else { |
| 7942 | UseBuiltinOperator = !lhs->getType()->isOverloadableType() && |
| 7943 | !rhs->getType()->isOverloadableType(); |
| 7944 | } |
| 7945 | |
| 7946 | if (!UseBuiltinOperator) { |
| 7947 | // Find all of the overloaded operators visible from this |
| 7948 | // point. We perform both an operator-name lookup from the local |
| 7949 | // scope and an argument-dependent lookup based on the types of |
| 7950 | // the arguments. |
| 7951 | UnresolvedSet<16> Functions; |
| 7952 | OverloadedOperatorKind OverOp |
| 7953 | = BinaryOperator::getOverloadedOperator(Opc); |
| 7954 | if (S && OverOp != OO_None) |
| 7955 | LookupOverloadedOperatorName(OverOp, S, lhs->getType(), rhs->getType(), |
| 7956 | Functions); |
| 7957 | |
| 7958 | // Build the (potentially-overloaded, potentially-dependent) |
| 7959 | // binary operation. |
| 7960 | return CreateOverloadedBinOp(OpLoc, Opc, Functions, lhs, rhs); |
| 7961 | } |
Sebastian Redl | b5d4935 | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 7962 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7963 | |
Douglas Gregor | 7d5fc7e | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7964 | // Build a built-in binary operation. |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 7965 | return CreateBuiltinBinOp(OpLoc, Opc, lhs, rhs); |
Steve Naroff | 218bc2b | 2007-05-04 21:54:46 +0000 | [diff] [blame] | 7966 | } |
| 7967 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7968 | ExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc, |
Argyrios Kyrtzidis | 7a808c0 | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 7969 | UnaryOperatorKind Opc, |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7970 | Expr *InputExpr) { |
| 7971 | ExprResult Input = Owned(InputExpr); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7972 | ExprValueKind VK = VK_RValue; |
| 7973 | ExprObjectKind OK = OK_Ordinary; |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 7974 | QualType resultType; |
| 7975 | switch (Opc) { |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7976 | case UO_PreInc: |
| 7977 | case UO_PreDec: |
| 7978 | case UO_PostInc: |
| 7979 | case UO_PostDec: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7980 | resultType = CheckIncrementDecrementOperand(*this, Input.get(), VK, OpLoc, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7981 | Opc == UO_PreInc || |
| 7982 | Opc == UO_PostInc, |
| 7983 | Opc == UO_PreInc || |
| 7984 | Opc == UO_PreDec); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 7985 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7986 | case UO_AddrOf: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7987 | resultType = CheckAddressOfOperand(*this, Input.get(), OpLoc); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 7988 | break; |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 7989 | case UO_Deref: { |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 7990 | ExprResult resolved = CheckPlaceholderExpr(Input.get()); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 7991 | if (!resolved.isUsable()) return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7992 | Input = move(resolved); |
| 7993 | Input = DefaultFunctionArrayLvalueConversion(Input.take()); |
| 7994 | resultType = CheckIndirectionOperand(*this, Input.get(), VK, OpLoc); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 7995 | break; |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 7996 | } |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7997 | case UO_Plus: |
| 7998 | case UO_Minus: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7999 | Input = UsualUnaryConversions(Input.take()); |
| 8000 | if (Input.isInvalid()) return ExprError(); |
| 8001 | resultType = Input.get()->getType(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8002 | if (resultType->isDependentType()) |
| 8003 | break; |
Douglas Gregor | a3208f9 | 2010-06-22 23:41:02 +0000 | [diff] [blame] | 8004 | if (resultType->isArithmeticType() || // C99 6.5.3.3p1 |
| 8005 | resultType->isVectorType()) |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8006 | break; |
| 8007 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6-7 |
| 8008 | resultType->isEnumeralType()) |
| 8009 | break; |
| 8010 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6 |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8011 | Opc == UO_Plus && |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8012 | resultType->isPointerType()) |
| 8013 | break; |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8014 | else if (resultType->isPlaceholderType()) { |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8015 | Input = CheckPlaceholderExpr(Input.take()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8016 | if (Input.isInvalid()) return ExprError(); |
| 8017 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input.take()); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8018 | } |
Douglas Gregor | d08452f | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8019 | |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8020 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8021 | << resultType << Input.get()->getSourceRange()); |
| 8022 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8023 | case UO_Not: // bitwise complement |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8024 | Input = UsualUnaryConversions(Input.take()); |
| 8025 | if (Input.isInvalid()) return ExprError(); |
| 8026 | resultType = Input.get()->getType(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8027 | if (resultType->isDependentType()) |
| 8028 | break; |
Chris Lattner | 0d70761 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 8029 | // C99 6.5.3.3p1. We allow complex int and float as a GCC extension. |
| 8030 | if (resultType->isComplexType() || resultType->isComplexIntegerType()) |
| 8031 | // C99 does not support '~' for complex conjugation. |
Chris Lattner | 29e812b | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 8032 | Diag(OpLoc, diag::ext_integer_complement_complex) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8033 | << resultType << Input.get()->getSourceRange(); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8034 | else if (resultType->hasIntegerRepresentation()) |
| 8035 | break; |
| 8036 | else if (resultType->isPlaceholderType()) { |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8037 | Input = CheckPlaceholderExpr(Input.take()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8038 | if (Input.isInvalid()) return ExprError(); |
| 8039 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input.take()); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8040 | } else { |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8041 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8042 | << resultType << Input.get()->getSourceRange()); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8043 | } |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 8044 | break; |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8045 | |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8046 | case UO_LNot: // logical negation |
Steve Naroff | 71b59a9 | 2007-06-04 22:22:31 +0000 | [diff] [blame] | 8047 | // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5). |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8048 | Input = DefaultFunctionArrayLvalueConversion(Input.take()); |
| 8049 | if (Input.isInvalid()) return ExprError(); |
| 8050 | resultType = Input.get()->getType(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8051 | if (resultType->isDependentType()) |
| 8052 | break; |
Abramo Bagnara | 7ccce98 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 8053 | if (resultType->isScalarType()) { |
| 8054 | // C99 6.5.3.3p1: ok, fallthrough; |
| 8055 | if (Context.getLangOptions().CPlusPlus) { |
| 8056 | // C++03 [expr.unary.op]p8, C++0x [expr.unary.op]p9: |
| 8057 | // operand contextually converted to bool. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8058 | Input = ImpCastExprToType(Input.take(), Context.BoolTy, |
| 8059 | ScalarTypeToBooleanCastKind(resultType)); |
Abramo Bagnara | 7ccce98 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 8060 | } |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8061 | } else if (resultType->isPlaceholderType()) { |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8062 | Input = CheckPlaceholderExpr(Input.take()); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8063 | if (Input.isInvalid()) return ExprError(); |
| 8064 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input.take()); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8065 | } else { |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8066 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8067 | << resultType << Input.get()->getSourceRange()); |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8068 | } |
Douglas Gregor | db8c6fd | 2010-09-20 17:13:33 +0000 | [diff] [blame] | 8069 | |
Chris Lattner | be31ed8 | 2007-06-02 19:11:33 +0000 | [diff] [blame] | 8070 | // LNot always has type int. C99 6.5.3.3p5. |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8071 | // In C++, it's bool. C++ 5.3.1p8 |
Argyrios Kyrtzidis | 1bdd688 | 2011-02-18 20:55:15 +0000 | [diff] [blame] | 8072 | resultType = Context.getLogicalOperationType(); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 8073 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8074 | case UO_Real: |
| 8075 | case UO_Imag: |
John McCall | 4bc41ae | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8076 | resultType = CheckRealImagOperand(*this, Input, OpLoc, Opc == UO_Real); |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8077 | // _Real and _Imag map ordinary l-values into ordinary l-values. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8078 | if (Input.isInvalid()) return ExprError(); |
| 8079 | if (Input.get()->getValueKind() != VK_RValue && |
| 8080 | Input.get()->getObjectKind() == OK_Ordinary) |
| 8081 | VK = Input.get()->getValueKind(); |
Chris Lattner | 30b5dd0 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 8082 | break; |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8083 | case UO_Extension: |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8084 | resultType = Input.get()->getType(); |
| 8085 | VK = Input.get()->getValueKind(); |
| 8086 | OK = Input.get()->getObjectKind(); |
Steve Naroff | 043d45d | 2007-05-15 02:32:35 +0000 | [diff] [blame] | 8087 | break; |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 8088 | } |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8089 | if (resultType.isNull() || Input.isInvalid()) |
Sebastian Redl | c215cfc | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8090 | return ExprError(); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8091 | |
Kaelyn Uhrain | 2e7aa5a | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8092 | // Check for array bounds violations in the operand of the UnaryOperator, |
| 8093 | // except for the '*' and '&' operators that have to be handled specially |
| 8094 | // by CheckArrayAccess (as there are special cases like &array[arraysize] |
| 8095 | // that are explicitly defined as valid by the standard). |
| 8096 | if (Opc != UO_AddrOf && Opc != UO_Deref) |
| 8097 | CheckArrayAccess(Input.get()); |
| 8098 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8099 | return Owned(new (Context) UnaryOperator(Input.take(), Opc, resultType, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8100 | VK, OK, OpLoc)); |
Steve Naroff | 35d8515 | 2007-05-07 00:24:15 +0000 | [diff] [blame] | 8101 | } |
Chris Lattner | eefa10e | 2007-05-28 06:56:27 +0000 | [diff] [blame] | 8102 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8103 | ExprResult Sema::BuildUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | e302792 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8104 | UnaryOperatorKind Opc, |
| 8105 | Expr *Input) { |
Anders Carlsson | 461a2c0 | 2009-11-14 21:26:41 +0000 | [diff] [blame] | 8106 | if (getLangOptions().CPlusPlus && Input->getType()->isOverloadableType() && |
Eli Friedman | 8ed2bac | 2010-09-05 23:15:52 +0000 | [diff] [blame] | 8107 | UnaryOperator::getOverloadedOperator(Opc) != OO_None) { |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8108 | // Find all of the overloaded operators visible from this |
| 8109 | // point. We perform both an operator-name lookup from the local |
| 8110 | // scope and an argument-dependent lookup based on the types of |
| 8111 | // the arguments. |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8112 | UnresolvedSet<16> Functions; |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8113 | OverloadedOperatorKind OverOp = UnaryOperator::getOverloadedOperator(Opc); |
John McCall | 4c4c1df | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8114 | if (S && OverOp != OO_None) |
| 8115 | LookupOverloadedOperatorName(OverOp, S, Input->getType(), QualType(), |
| 8116 | Functions); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8117 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8118 | return CreateOverloadedUnaryOp(OpLoc, Opc, Functions, Input); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8119 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8120 | |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8121 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
Douglas Gregor | 084d855 | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8122 | } |
| 8123 | |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8124 | // Unary Operators. 'Tok' is the token for the operator. |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8125 | ExprResult Sema::ActOnUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | 424cec9 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 8126 | tok::TokenKind Op, Expr *Input) { |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8127 | return BuildUnaryOp(S, OpLoc, ConvertTokenKindToUnaryOpcode(Op), Input); |
Douglas Gregor | 5287f09 | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8128 | } |
| 8129 | |
Steve Naroff | 66356bd | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 8130 | /// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo". |
Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 8131 | ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc, SourceLocation LabLoc, |
Chris Lattner | cab02a6 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 8132 | LabelDecl *TheDecl) { |
Chris Lattner | c8e630e | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 8133 | TheDecl->setUsed(); |
Chris Lattner | eefa10e | 2007-05-28 06:56:27 +0000 | [diff] [blame] | 8134 | // 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] | 8135 | return Owned(new (Context) AddrLabelExpr(OpLoc, LabLoc, TheDecl, |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8136 | Context.getPointerType(Context.VoidTy))); |
Chris Lattner | eefa10e | 2007-05-28 06:56:27 +0000 | [diff] [blame] | 8137 | } |
| 8138 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8139 | /// Given the last statement in a statement-expression, check whether |
| 8140 | /// the result is a producing expression (like a call to an |
| 8141 | /// ns_returns_retained function) and, if so, rebuild it to hoist the |
| 8142 | /// release out of the full-expression. Otherwise, return null. |
| 8143 | /// Cannot fail. |
| 8144 | static Expr *maybeRebuildARCConsumingStmt(Stmt *s) { |
| 8145 | // Should always be wrapped with one of these. |
| 8146 | ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(s); |
| 8147 | if (!cleanups) return 0; |
| 8148 | |
| 8149 | ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(cleanups->getSubExpr()); |
| 8150 | if (!cast || cast->getCastKind() != CK_ObjCConsumeObject) |
| 8151 | return 0; |
| 8152 | |
| 8153 | // Splice out the cast. This shouldn't modify any interesting |
| 8154 | // features of the statement. |
| 8155 | Expr *producer = cast->getSubExpr(); |
| 8156 | assert(producer->getType() == cast->getType()); |
| 8157 | assert(producer->getValueKind() == cast->getValueKind()); |
| 8158 | cleanups->setSubExpr(producer); |
| 8159 | return cleanups; |
| 8160 | } |
| 8161 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8162 | ExprResult |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8163 | Sema::ActOnStmtExpr(SourceLocation LPLoc, Stmt *SubStmt, |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8164 | SourceLocation RPLoc) { // "({..})" |
Chris Lattner | 366727f | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8165 | assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!"); |
| 8166 | CompoundStmt *Compound = cast<CompoundStmt>(SubStmt); |
| 8167 | |
Douglas Gregor | 6cf3f3c | 2010-03-10 04:54:39 +0000 | [diff] [blame] | 8168 | bool isFileScope |
| 8169 | = (getCurFunctionOrMethodDecl() == 0) && (getCurBlock() == 0); |
Chris Lattner | a69b076 | 2009-04-25 19:11:05 +0000 | [diff] [blame] | 8170 | if (isFileScope) |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8171 | return ExprError(Diag(LPLoc, diag::err_stmtexpr_file_scope)); |
Eli Friedman | 52cc016 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 8172 | |
Chris Lattner | 366727f | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8173 | // FIXME: there are a variety of strange constraints to enforce here, for |
| 8174 | // example, it is not possible to goto into a stmt expression apparently. |
| 8175 | // More semantic analysis is needed. |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8176 | |
Chris Lattner | 366727f | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8177 | // If there are sub stmts in the compound stmt, take the type of the last one |
| 8178 | // as the type of the stmtexpr. |
| 8179 | QualType Ty = Context.VoidTy; |
Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8180 | bool StmtExprMayBindToTemp = false; |
Chris Lattner | 944d306 | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8181 | if (!Compound->body_empty()) { |
| 8182 | Stmt *LastStmt = Compound->body_back(); |
Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8183 | LabelStmt *LastLabelStmt = 0; |
Chris Lattner | 944d306 | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8184 | // If LastStmt is a label, skip down through into the body. |
Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8185 | while (LabelStmt *Label = dyn_cast<LabelStmt>(LastStmt)) { |
| 8186 | LastLabelStmt = Label; |
Chris Lattner | 944d306 | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8187 | LastStmt = Label->getSubStmt(); |
Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8188 | } |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8189 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8190 | if (Expr *LastE = dyn_cast<Expr>(LastStmt)) { |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8191 | // Do function/array conversion on the last expression, but not |
| 8192 | // lvalue-to-rvalue. However, initialize an unqualified type. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8193 | ExprResult LastExpr = DefaultFunctionArrayConversion(LastE); |
| 8194 | if (LastExpr.isInvalid()) |
| 8195 | return ExprError(); |
| 8196 | Ty = LastExpr.get()->getType().getUnqualifiedType(); |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8197 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8198 | if (!Ty->isDependentType() && !LastExpr.get()->isTypeDependent()) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8199 | // In ARC, if the final expression ends in a consume, splice |
| 8200 | // the consume out and bind it later. In the alternate case |
| 8201 | // (when dealing with a retainable type), the result |
| 8202 | // initialization will create a produce. In both cases the |
| 8203 | // result will be +1, and we'll need to balance that out with |
| 8204 | // a bind. |
| 8205 | if (Expr *rebuiltLastStmt |
| 8206 | = maybeRebuildARCConsumingStmt(LastExpr.get())) { |
| 8207 | LastExpr = rebuiltLastStmt; |
| 8208 | } else { |
| 8209 | LastExpr = PerformCopyInitialization( |
Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8210 | InitializedEntity::InitializeResult(LPLoc, |
| 8211 | Ty, |
| 8212 | false), |
| 8213 | SourceLocation(), |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8214 | LastExpr); |
| 8215 | } |
| 8216 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8217 | if (LastExpr.isInvalid()) |
Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8218 | return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8219 | if (LastExpr.get() != 0) { |
Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8220 | if (!LastLabelStmt) |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8221 | Compound->setLastStmt(LastExpr.take()); |
Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8222 | else |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8223 | LastLabelStmt->setSubStmt(LastExpr.take()); |
Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8224 | StmtExprMayBindToTemp = true; |
| 8225 | } |
| 8226 | } |
| 8227 | } |
Chris Lattner | 944d306 | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8228 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8229 | |
Eli Friedman | ba961a9 | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 8230 | // FIXME: Check that expression type is complete/non-abstract; statement |
| 8231 | // expressions are not lvalues. |
Fariborz Jahanian | 56143ae | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8232 | Expr *ResStmtExpr = new (Context) StmtExpr(Compound, Ty, LPLoc, RPLoc); |
| 8233 | if (StmtExprMayBindToTemp) |
| 8234 | return MaybeBindToTemporary(ResStmtExpr); |
| 8235 | return Owned(ResStmtExpr); |
Chris Lattner | 366727f | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8236 | } |
Steve Naroff | 7886467 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 8237 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8238 | ExprResult Sema::BuildBuiltinOffsetOf(SourceLocation BuiltinLoc, |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8239 | TypeSourceInfo *TInfo, |
| 8240 | OffsetOfComponent *CompPtr, |
| 8241 | unsigned NumComponents, |
| 8242 | SourceLocation RParenLoc) { |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8243 | QualType ArgTy = TInfo->getType(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8244 | bool Dependent = ArgTy->isDependentType(); |
Abramo Bagnara | 1108e7b | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 8245 | SourceRange TypeRange = TInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8246 | |
Chris Lattner | f17bd42 | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 8247 | // We must have at least one component that refers to the type, and the first |
| 8248 | // one is known to be a field designator. Verify that the ArgTy represents |
| 8249 | // a struct/union/class. |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8250 | if (!Dependent && !ArgTy->isRecordType()) |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8251 | return ExprError(Diag(BuiltinLoc, diag::err_offsetof_record_type) |
| 8252 | << ArgTy << TypeRange); |
| 8253 | |
| 8254 | // Type must be complete per C99 7.17p3 because a declaring a variable |
| 8255 | // with an incomplete type would be ill-formed. |
| 8256 | if (!Dependent |
| 8257 | && RequireCompleteType(BuiltinLoc, ArgTy, |
| 8258 | PDiag(diag::err_offsetof_incomplete_type) |
| 8259 | << TypeRange)) |
| 8260 | return ExprError(); |
| 8261 | |
Chris Lattner | 78502cf | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 8262 | // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a |
| 8263 | // GCC extension, diagnose them. |
Eli Friedman | 988a16b | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 8264 | // FIXME: This diagnostic isn't actually visible because the location is in |
| 8265 | // a system header! |
Chris Lattner | 78502cf | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 8266 | if (NumComponents != 1) |
Chris Lattner | f490e15 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 8267 | Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator) |
| 8268 | << SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd); |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8269 | |
| 8270 | bool DidWarnAboutNonPOD = false; |
| 8271 | QualType CurrentType = ArgTy; |
| 8272 | typedef OffsetOfExpr::OffsetOfNode OffsetOfNode; |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8273 | SmallVector<OffsetOfNode, 4> Comps; |
| 8274 | SmallVector<Expr*, 4> Exprs; |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8275 | for (unsigned i = 0; i != NumComponents; ++i) { |
| 8276 | const OffsetOfComponent &OC = CompPtr[i]; |
| 8277 | if (OC.isBrackets) { |
| 8278 | // Offset of an array sub-field. TODO: Should we allow vector elements? |
| 8279 | if (!CurrentType->isDependentType()) { |
| 8280 | const ArrayType *AT = Context.getAsArrayType(CurrentType); |
| 8281 | if(!AT) |
| 8282 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_array_type) |
| 8283 | << CurrentType); |
| 8284 | CurrentType = AT->getElementType(); |
| 8285 | } else |
| 8286 | CurrentType = Context.DependentTy; |
| 8287 | |
| 8288 | // The expression must be an integral expression. |
| 8289 | // FIXME: An integral constant expression? |
| 8290 | Expr *Idx = static_cast<Expr*>(OC.U.E); |
| 8291 | if (!Idx->isTypeDependent() && !Idx->isValueDependent() && |
| 8292 | !Idx->getType()->isIntegerType()) |
| 8293 | return ExprError(Diag(Idx->getLocStart(), |
| 8294 | diag::err_typecheck_subscript_not_integer) |
| 8295 | << Idx->getSourceRange()); |
| 8296 | |
| 8297 | // Record this array index. |
| 8298 | Comps.push_back(OffsetOfNode(OC.LocStart, Exprs.size(), OC.LocEnd)); |
| 8299 | Exprs.push_back(Idx); |
| 8300 | continue; |
| 8301 | } |
| 8302 | |
| 8303 | // Offset of a field. |
| 8304 | if (CurrentType->isDependentType()) { |
| 8305 | // We have the offset of a field, but we can't look into the dependent |
| 8306 | // type. Just record the identifier of the field. |
| 8307 | Comps.push_back(OffsetOfNode(OC.LocStart, OC.U.IdentInfo, OC.LocEnd)); |
| 8308 | CurrentType = Context.DependentTy; |
| 8309 | continue; |
| 8310 | } |
| 8311 | |
| 8312 | // We need to have a complete type to look into. |
| 8313 | if (RequireCompleteType(OC.LocStart, CurrentType, |
| 8314 | diag::err_offsetof_incomplete_type)) |
| 8315 | return ExprError(); |
| 8316 | |
| 8317 | // Look for the designated field. |
| 8318 | const RecordType *RC = CurrentType->getAs<RecordType>(); |
| 8319 | if (!RC) |
| 8320 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_record_type) |
| 8321 | << CurrentType); |
| 8322 | RecordDecl *RD = RC->getDecl(); |
| 8323 | |
| 8324 | // C++ [lib.support.types]p5: |
| 8325 | // The macro offsetof accepts a restricted set of type arguments in this |
| 8326 | // International Standard. type shall be a POD structure or a POD union |
| 8327 | // (clause 9). |
| 8328 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 8329 | if (!CRD->isPOD() && !DidWarnAboutNonPOD && |
Ted Kremenek | 55ae319 | 2011-02-23 01:51:43 +0000 | [diff] [blame] | 8330 | DiagRuntimeBehavior(BuiltinLoc, 0, |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8331 | PDiag(diag::warn_offsetof_non_pod_type) |
| 8332 | << SourceRange(CompPtr[0].LocStart, OC.LocEnd) |
| 8333 | << CurrentType)) |
| 8334 | DidWarnAboutNonPOD = true; |
| 8335 | } |
| 8336 | |
| 8337 | // Look for the field. |
| 8338 | LookupResult R(*this, OC.U.IdentInfo, OC.LocStart, LookupMemberName); |
| 8339 | LookupQualifiedName(R, RD); |
| 8340 | FieldDecl *MemberDecl = R.getAsSingle<FieldDecl>(); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8341 | IndirectFieldDecl *IndirectMemberDecl = 0; |
| 8342 | if (!MemberDecl) { |
Benjamin Kramer | 3959370 | 2010-11-21 14:11:41 +0000 | [diff] [blame] | 8343 | if ((IndirectMemberDecl = R.getAsSingle<IndirectFieldDecl>())) |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8344 | MemberDecl = IndirectMemberDecl->getAnonField(); |
| 8345 | } |
| 8346 | |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8347 | if (!MemberDecl) |
| 8348 | return ExprError(Diag(BuiltinLoc, diag::err_no_member) |
| 8349 | << OC.U.IdentInfo << RD << SourceRange(OC.LocStart, |
| 8350 | OC.LocEnd)); |
| 8351 | |
Douglas Gregor | 10982ea | 2010-04-28 22:36:06 +0000 | [diff] [blame] | 8352 | // C99 7.17p3: |
| 8353 | // (If the specified member is a bit-field, the behavior is undefined.) |
| 8354 | // |
| 8355 | // We diagnose this as an error. |
| 8356 | if (MemberDecl->getBitWidth()) { |
| 8357 | Diag(OC.LocEnd, diag::err_offsetof_bitfield) |
| 8358 | << MemberDecl->getDeclName() |
| 8359 | << SourceRange(BuiltinLoc, RParenLoc); |
| 8360 | Diag(MemberDecl->getLocation(), diag::note_bitfield_decl); |
| 8361 | return ExprError(); |
| 8362 | } |
Eli Friedman | 74ef7cf | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 8363 | |
| 8364 | RecordDecl *Parent = MemberDecl->getParent(); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8365 | if (IndirectMemberDecl) |
| 8366 | Parent = cast<RecordDecl>(IndirectMemberDecl->getDeclContext()); |
Eli Friedman | 74ef7cf | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 8367 | |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 8368 | // If the member was found in a base class, introduce OffsetOfNodes for |
| 8369 | // the base class indirections. |
| 8370 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 8371 | /*DetectVirtual=*/false); |
Eli Friedman | 74ef7cf | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 8372 | if (IsDerivedFrom(CurrentType, Context.getTypeDeclType(Parent), Paths)) { |
Douglas Gregor | d170206 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 8373 | CXXBasePath &Path = Paths.front(); |
| 8374 | for (CXXBasePath::iterator B = Path.begin(), BEnd = Path.end(); |
| 8375 | B != BEnd; ++B) |
| 8376 | Comps.push_back(OffsetOfNode(B->Base)); |
| 8377 | } |
Eli Friedman | 74ef7cf | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 8378 | |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8379 | if (IndirectMemberDecl) { |
| 8380 | for (IndirectFieldDecl::chain_iterator FI = |
| 8381 | IndirectMemberDecl->chain_begin(), |
| 8382 | FEnd = IndirectMemberDecl->chain_end(); FI != FEnd; FI++) { |
| 8383 | assert(isa<FieldDecl>(*FI)); |
| 8384 | Comps.push_back(OffsetOfNode(OC.LocStart, |
| 8385 | cast<FieldDecl>(*FI), OC.LocEnd)); |
| 8386 | } |
| 8387 | } else |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8388 | Comps.push_back(OffsetOfNode(OC.LocStart, MemberDecl, OC.LocEnd)); |
Francois Pichet | 783dd6e | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8389 | |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8390 | CurrentType = MemberDecl->getType().getNonReferenceType(); |
| 8391 | } |
| 8392 | |
| 8393 | return Owned(OffsetOfExpr::Create(Context, Context.getSizeType(), BuiltinLoc, |
| 8394 | TInfo, Comps.data(), Comps.size(), |
| 8395 | Exprs.data(), Exprs.size(), RParenLoc)); |
| 8396 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8397 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8398 | ExprResult Sema::ActOnBuiltinOffsetOf(Scope *S, |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8399 | SourceLocation BuiltinLoc, |
| 8400 | SourceLocation TypeLoc, |
| 8401 | ParsedType argty, |
| 8402 | OffsetOfComponent *CompPtr, |
| 8403 | unsigned NumComponents, |
| 8404 | SourceLocation RPLoc) { |
| 8405 | |
Douglas Gregor | 882211c | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8406 | TypeSourceInfo *ArgTInfo; |
| 8407 | QualType ArgTy = GetTypeFromParser(argty, &ArgTInfo); |
| 8408 | if (ArgTy.isNull()) |
| 8409 | return ExprError(); |
| 8410 | |
Eli Friedman | 06dcfd9 | 2010-08-05 10:15:45 +0000 | [diff] [blame] | 8411 | if (!ArgTInfo) |
| 8412 | ArgTInfo = Context.getTrivialTypeSourceInfo(ArgTy, TypeLoc); |
| 8413 | |
| 8414 | return BuildBuiltinOffsetOf(BuiltinLoc, ArgTInfo, CompPtr, NumComponents, |
| 8415 | RPLoc); |
Chris Lattner | f17bd42 | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 8416 | } |
| 8417 | |
| 8418 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8419 | ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, |
John McCall | 3622662 | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8420 | Expr *CondExpr, |
| 8421 | Expr *LHSExpr, Expr *RHSExpr, |
| 8422 | SourceLocation RPLoc) { |
Steve Naroff | 9efdabc | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 8423 | assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)"); |
| 8424 | |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8425 | ExprValueKind VK = VK_RValue; |
| 8426 | ExprObjectKind OK = OK_Ordinary; |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8427 | QualType resType; |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 8428 | bool ValueDependent = false; |
Douglas Gregor | 0df9112 | 2009-05-19 22:43:30 +0000 | [diff] [blame] | 8429 | if (CondExpr->isTypeDependent() || CondExpr->isValueDependent()) { |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8430 | resType = Context.DependentTy; |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 8431 | ValueDependent = true; |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8432 | } else { |
| 8433 | // The conditional expression is required to be a constant expression. |
| 8434 | llvm::APSInt condEval(32); |
| 8435 | SourceLocation ExpLoc; |
| 8436 | if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc)) |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8437 | return ExprError(Diag(ExpLoc, |
| 8438 | diag::err_typecheck_choose_expr_requires_constant) |
| 8439 | << CondExpr->getSourceRange()); |
Steve Naroff | 9efdabc | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 8440 | |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8441 | // 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] | 8442 | Expr *ActiveExpr = condEval.getZExtValue() ? LHSExpr : RHSExpr; |
| 8443 | |
| 8444 | resType = ActiveExpr->getType(); |
| 8445 | ValueDependent = ActiveExpr->isValueDependent(); |
| 8446 | VK = ActiveExpr->getValueKind(); |
| 8447 | OK = ActiveExpr->getObjectKind(); |
Sebastian Redl | 8d2ccae | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8448 | } |
| 8449 | |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8450 | return Owned(new (Context) ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8451 | resType, VK, OK, RPLoc, |
Douglas Gregor | 56751b5 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 8452 | resType->isDependentType(), |
| 8453 | ValueDependent)); |
Steve Naroff | 9efdabc | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 8454 | } |
| 8455 | |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8456 | //===----------------------------------------------------------------------===// |
| 8457 | // Clang Extensions. |
| 8458 | //===----------------------------------------------------------------------===// |
| 8459 | |
| 8460 | /// ActOnBlockStart - This callback is invoked when a block literal is started. |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8461 | void Sema::ActOnBlockStart(SourceLocation CaretLoc, Scope *BlockScope) { |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8462 | BlockDecl *Block = BlockDecl::Create(Context, CurContext, CaretLoc); |
| 8463 | PushBlockScope(BlockScope, Block); |
| 8464 | CurContext->addDecl(Block); |
Fariborz Jahanian | 1babe77 | 2010-07-09 18:44:02 +0000 | [diff] [blame] | 8465 | if (BlockScope) |
| 8466 | PushDeclContext(BlockScope, Block); |
| 8467 | else |
| 8468 | CurContext = Block; |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8469 | } |
| 8470 | |
Mike Stump | 82f071f | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 8471 | void Sema::ActOnBlockArguments(Declarator &ParamInfo, Scope *CurScope) { |
Mike Stump | f70bcf7 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 8472 | assert(ParamInfo.getIdentifier()==0 && "block-id should have no identifier!"); |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8473 | assert(ParamInfo.getContext() == Declarator::BlockLiteralContext); |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8474 | BlockScopeInfo *CurBlock = getCurBlock(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8475 | |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 8476 | TypeSourceInfo *Sig = GetTypeForDeclarator(ParamInfo, CurScope); |
John McCall | 8cb7bdf | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 8477 | QualType T = Sig->getType(); |
Mike Stump | 82f071f | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 8478 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8479 | // GetTypeForDeclarator always produces a function type for a block |
| 8480 | // literal signature. Furthermore, it is always a FunctionProtoType |
| 8481 | // unless the function was written with a typedef. |
| 8482 | assert(T->isFunctionType() && |
| 8483 | "GetTypeForDeclarator made a non-function block signature"); |
| 8484 | |
| 8485 | // Look for an explicit signature in that function type. |
| 8486 | FunctionProtoTypeLoc ExplicitSignature; |
| 8487 | |
| 8488 | TypeLoc tmp = Sig->getTypeLoc().IgnoreParens(); |
| 8489 | if (isa<FunctionProtoTypeLoc>(tmp)) { |
| 8490 | ExplicitSignature = cast<FunctionProtoTypeLoc>(tmp); |
| 8491 | |
| 8492 | // Check whether that explicit signature was synthesized by |
| 8493 | // GetTypeForDeclarator. If so, don't save that as part of the |
| 8494 | // written signature. |
Abramo Bagnara | f2a79d9 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 8495 | if (ExplicitSignature.getLocalRangeBegin() == |
| 8496 | ExplicitSignature.getLocalRangeEnd()) { |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8497 | // This would be much cheaper if we stored TypeLocs instead of |
| 8498 | // TypeSourceInfos. |
| 8499 | TypeLoc Result = ExplicitSignature.getResultLoc(); |
| 8500 | unsigned Size = Result.getFullDataSize(); |
| 8501 | Sig = Context.CreateTypeSourceInfo(Result.getType(), Size); |
| 8502 | Sig->getTypeLoc().initializeFullCopy(Result, Size); |
| 8503 | |
| 8504 | ExplicitSignature = FunctionProtoTypeLoc(); |
| 8505 | } |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8506 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8507 | |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8508 | CurBlock->TheDecl->setSignatureAsWritten(Sig); |
| 8509 | CurBlock->FunctionType = T; |
| 8510 | |
| 8511 | const FunctionType *Fn = T->getAs<FunctionType>(); |
| 8512 | QualType RetTy = Fn->getResultType(); |
| 8513 | bool isVariadic = |
| 8514 | (isa<FunctionProtoType>(Fn) && cast<FunctionProtoType>(Fn)->isVariadic()); |
| 8515 | |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8516 | CurBlock->TheDecl->setIsVariadic(isVariadic); |
Douglas Gregor | b92a156 | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 8517 | |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8518 | // Don't allow returning a objc interface by value. |
| 8519 | if (RetTy->isObjCObjectType()) { |
| 8520 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 8521 | diag::err_object_cannot_be_passed_returned_by_value) << 0 << RetTy; |
| 8522 | return; |
| 8523 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8524 | |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8525 | // Context.DependentTy is used as a placeholder for a missing block |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8526 | // return type. TODO: what should we do with declarators like: |
| 8527 | // ^ * { ... } |
| 8528 | // If the answer is "apply template argument deduction".... |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8529 | if (RetTy != Context.DependentTy) |
| 8530 | CurBlock->ReturnType = RetTy; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8531 | |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8532 | // Push block parameters from the declarator if we had them. |
Chris Lattner | 0e62c1c | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8533 | SmallVector<ParmVarDecl*, 8> Params; |
John McCall | 3882ace | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8534 | if (ExplicitSignature) { |
| 8535 | for (unsigned I = 0, E = ExplicitSignature.getNumArgs(); I != E; ++I) { |
| 8536 | ParmVarDecl *Param = ExplicitSignature.getArg(I); |
Fariborz Jahanian | 5ec502e | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 8537 | if (Param->getIdentifier() == 0 && |
| 8538 | !Param->isImplicit() && |
| 8539 | !Param->isInvalidDecl() && |
| 8540 | !getLangOptions().CPlusPlus) |
| 8541 | Diag(Param->getLocation(), diag::err_parameter_name_omitted); |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8542 | Params.push_back(Param); |
Fariborz Jahanian | 5ec502e | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 8543 | } |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8544 | |
| 8545 | // Fake up parameter variables if we have a typedef, like |
| 8546 | // ^ fntype { ... } |
| 8547 | } else if (const FunctionProtoType *Fn = T->getAs<FunctionProtoType>()) { |
| 8548 | for (FunctionProtoType::arg_type_iterator |
| 8549 | I = Fn->arg_type_begin(), E = Fn->arg_type_end(); I != E; ++I) { |
| 8550 | ParmVarDecl *Param = |
| 8551 | BuildParmVarDeclForTypedef(CurBlock->TheDecl, |
| 8552 | ParamInfo.getSourceRange().getBegin(), |
| 8553 | *I); |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8554 | Params.push_back(Param); |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8555 | } |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8556 | } |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8557 | |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8558 | // Set the parameters on the block decl. |
Douglas Gregor | b524d90 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 8559 | if (!Params.empty()) { |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8560 | CurBlock->TheDecl->setParams(Params.data(), Params.size()); |
Douglas Gregor | b524d90 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 8561 | CheckParmsForFunctionDef(CurBlock->TheDecl->param_begin(), |
| 8562 | CurBlock->TheDecl->param_end(), |
| 8563 | /*CheckParameterNames=*/false); |
| 8564 | } |
| 8565 | |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8566 | // Finally we can process decl attributes. |
Douglas Gregor | 758a869 | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 8567 | ProcessDeclAttributes(CurScope, CurBlock->TheDecl, ParamInfo); |
John McCall | df8b37c | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 8568 | |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8569 | if (!isVariadic && CurBlock->TheDecl->getAttr<SentinelAttr>()) { |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8570 | Diag(ParamInfo.getAttributes()->getLoc(), |
| 8571 | diag::warn_attribute_sentinel_not_variadic) << 1; |
| 8572 | // FIXME: remove the attribute. |
| 8573 | } |
| 8574 | |
| 8575 | // Put the parameter variables in scope. We can bail out immediately |
| 8576 | // if we don't have any. |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8577 | if (Params.empty()) |
John McCall | a3ccba0 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8578 | return; |
| 8579 | |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8580 | for (BlockDecl::param_iterator AI = CurBlock->TheDecl->param_begin(), |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 8581 | E = CurBlock->TheDecl->param_end(); AI != E; ++AI) { |
| 8582 | (*AI)->setOwningFunction(CurBlock->TheDecl); |
| 8583 | |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8584 | // If this has an identifier, add it to the scope stack. |
John McCall | df8b37c | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 8585 | if ((*AI)->getIdentifier()) { |
Argyrios Kyrtzidis | 1cb0de1 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 8586 | CheckShadow(CurBlock->TheScope, *AI); |
John McCall | df8b37c | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 8587 | |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8588 | PushOnScopeChains(*AI, CurBlock->TheScope); |
John McCall | df8b37c | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 8589 | } |
John McCall | f7b2fb5 | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 8590 | } |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8591 | } |
| 8592 | |
| 8593 | /// ActOnBlockError - If there is an error parsing a block, this callback |
| 8594 | /// is invoked to pop the information about the block from the action impl. |
| 8595 | void Sema::ActOnBlockError(SourceLocation CaretLoc, Scope *CurScope) { |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8596 | // Pop off CurBlock, handle nested blocks. |
Chris Lattner | 41b8694 | 2009-04-21 22:38:46 +0000 | [diff] [blame] | 8597 | PopDeclContext(); |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8598 | PopFunctionOrBlockScope(); |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8599 | } |
| 8600 | |
| 8601 | /// ActOnBlockStmtExpr - This is called when the body of a block statement |
| 8602 | /// literal was successfully completed. ^(int x){...} |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8603 | ExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, |
Chris Lattner | 60f8449 | 2011-02-17 23:58:47 +0000 | [diff] [blame] | 8604 | Stmt *Body, Scope *CurScope) { |
Chris Lattner | 9eac931 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 8605 | // If blocks are disabled, emit an error. |
| 8606 | if (!LangOpts.Blocks) |
| 8607 | Diag(CaretLoc, diag::err_blocks_disable); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8608 | |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8609 | BlockScopeInfo *BSI = cast<BlockScopeInfo>(FunctionScopes.back()); |
Fariborz Jahanian | 1babe77 | 2010-07-09 18:44:02 +0000 | [diff] [blame] | 8610 | |
Steve Naroff | 1d95e5a | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8611 | PopDeclContext(); |
| 8612 | |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8613 | QualType RetTy = Context.VoidTy; |
Fariborz Jahanian | 3fd7310 | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 8614 | if (!BSI->ReturnType.isNull()) |
| 8615 | RetTy = BSI->ReturnType; |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8616 | |
Mike Stump | 3bf1ab4 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 8617 | bool NoReturn = BSI->TheDecl->getAttr<NoReturnAttr>(); |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8618 | QualType BlockTy; |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8619 | |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 8620 | // Set the captured variables on the block. |
John McCall | 351762c | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 8621 | BSI->TheDecl->setCaptures(Context, BSI->Captures.begin(), BSI->Captures.end(), |
| 8622 | BSI->CapturesCXXThis); |
John McCall | c63de66 | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 8623 | |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8624 | // If the user wrote a function type in some form, try to use that. |
| 8625 | if (!BSI->FunctionType.isNull()) { |
| 8626 | const FunctionType *FTy = BSI->FunctionType->getAs<FunctionType>(); |
| 8627 | |
| 8628 | FunctionType::ExtInfo Ext = FTy->getExtInfo(); |
| 8629 | if (NoReturn && !Ext.getNoReturn()) Ext = Ext.withNoReturn(true); |
| 8630 | |
| 8631 | // Turn protoless block types into nullary block types. |
| 8632 | if (isa<FunctionNoProtoType>(FTy)) { |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8633 | FunctionProtoType::ExtProtoInfo EPI; |
| 8634 | EPI.ExtInfo = Ext; |
| 8635 | BlockTy = Context.getFunctionType(RetTy, 0, 0, EPI); |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8636 | |
| 8637 | // Otherwise, if we don't need to change anything about the function type, |
| 8638 | // preserve its sugar structure. |
| 8639 | } else if (FTy->getResultType() == RetTy && |
| 8640 | (!NoReturn || FTy->getNoReturnAttr())) { |
| 8641 | BlockTy = BSI->FunctionType; |
| 8642 | |
| 8643 | // Otherwise, make the minimal modifications to the function type. |
| 8644 | } else { |
| 8645 | const FunctionProtoType *FPT = cast<FunctionProtoType>(FTy); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8646 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 8647 | EPI.TypeQuals = 0; // FIXME: silently? |
| 8648 | EPI.ExtInfo = Ext; |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8649 | BlockTy = Context.getFunctionType(RetTy, |
| 8650 | FPT->arg_type_begin(), |
| 8651 | FPT->getNumArgs(), |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8652 | EPI); |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8653 | } |
| 8654 | |
| 8655 | // If we don't have a function type, just build one from nothing. |
| 8656 | } else { |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8657 | FunctionProtoType::ExtProtoInfo EPI; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8658 | EPI.ExtInfo = FunctionType::ExtInfo().withNoReturn(NoReturn); |
John McCall | db40c7f | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8659 | BlockTy = Context.getFunctionType(RetTy, 0, 0, EPI); |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8660 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8661 | |
John McCall | 8e34670 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8662 | DiagnoseUnusedParameters(BSI->TheDecl->param_begin(), |
| 8663 | BSI->TheDecl->param_end()); |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8664 | BlockTy = Context.getBlockPointerType(BlockTy); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8665 | |
Chris Lattner | 45542ea | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 8666 | // If needed, diagnose invalid gotos and switches in the block. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8667 | if (getCurFunction()->NeedsScopeChecking() && |
| 8668 | !hasAnyUnrecoverableErrorsInThisFunction()) |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8669 | DiagnoseInvalidJumps(cast<CompoundStmt>(Body)); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8670 | |
Chris Lattner | 60f8449 | 2011-02-17 23:58:47 +0000 | [diff] [blame] | 8671 | BSI->TheDecl->setBody(cast<CompoundStmt>(Body)); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8672 | |
Fariborz Jahanian | 256d39d | 2011-07-11 18:04:54 +0000 | [diff] [blame] | 8673 | for (BlockDecl::capture_const_iterator ci = BSI->TheDecl->capture_begin(), |
| 8674 | ce = BSI->TheDecl->capture_end(); ci != ce; ++ci) { |
| 8675 | const VarDecl *variable = ci->getVariable(); |
| 8676 | QualType T = variable->getType(); |
| 8677 | QualType::DestructionKind destructKind = T.isDestructedType(); |
| 8678 | if (destructKind != QualType::DK_none) |
| 8679 | getCurFunction()->setHasBranchProtectedScope(); |
| 8680 | } |
| 8681 | |
Benjamin Kramer | a4fb836 | 2011-07-12 14:11:05 +0000 | [diff] [blame] | 8682 | BlockExpr *Result = new (Context) BlockExpr(BSI->TheDecl, BlockTy); |
| 8683 | const AnalysisBasedWarnings::Policy &WP = AnalysisWarnings.getDefaultPolicy(); |
| 8684 | PopFunctionOrBlockScope(&WP, Result->getBlockDecl(), Result); |
| 8685 | |
Douglas Gregor | 9a28e84 | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8686 | return Owned(Result); |
Steve Naroff | c540d66 | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8687 | } |
| 8688 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8689 | ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc, |
John McCall | ba7bf59 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 8690 | Expr *expr, ParsedType type, |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8691 | SourceLocation RPLoc) { |
Abramo Bagnara | 27db239 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 8692 | TypeSourceInfo *TInfo; |
Jeffrey Yasskin | 8dfa5f1 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 8693 | GetTypeFromParser(type, &TInfo); |
John McCall | b268a28 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8694 | return BuildVAArgExpr(BuiltinLoc, expr, TInfo, RPLoc); |
Abramo Bagnara | 27db239 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 8695 | } |
| 8696 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8697 | ExprResult Sema::BuildVAArgExpr(SourceLocation BuiltinLoc, |
John McCall | 7decc9e | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8698 | Expr *E, TypeSourceInfo *TInfo, |
| 8699 | SourceLocation RPLoc) { |
Chris Lattner | 56382aa | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 8700 | Expr *OrigExpr = E; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8701 | |
Eli Friedman | 121ba0c | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 8702 | // Get the va_list type |
| 8703 | QualType VaListType = Context.getBuiltinVaListType(); |
Eli Friedman | e2cad65 | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 8704 | if (VaListType->isArrayType()) { |
| 8705 | // Deal with implicit array decay; for example, on x86-64, |
| 8706 | // va_list is an array, but it's supposed to decay to |
| 8707 | // a pointer for va_arg. |
Eli Friedman | 121ba0c | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 8708 | VaListType = Context.getArrayDecayedType(VaListType); |
Eli Friedman | e2cad65 | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 8709 | // Make sure the input expression also decays appropriately. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8710 | ExprResult Result = UsualUnaryConversions(E); |
| 8711 | if (Result.isInvalid()) |
| 8712 | return ExprError(); |
| 8713 | E = Result.take(); |
Eli Friedman | e2cad65 | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 8714 | } else { |
| 8715 | // Otherwise, the va_list argument must be an l-value because |
| 8716 | // it is modified by va_arg. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8717 | if (!E->isTypeDependent() && |
Douglas Gregor | ad3150c | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 8718 | CheckForModifiableLvalue(E, BuiltinLoc, *this)) |
Eli Friedman | e2cad65 | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 8719 | return ExprError(); |
| 8720 | } |
Eli Friedman | 121ba0c | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 8721 | |
Douglas Gregor | ad3150c | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 8722 | if (!E->isTypeDependent() && |
| 8723 | !Context.hasSameType(VaListType, E->getType())) { |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8724 | return ExprError(Diag(E->getLocStart(), |
| 8725 | diag::err_first_argument_to_va_arg_not_of_type_va_list) |
Chris Lattner | 56382aa | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 8726 | << OrigExpr->getType() << E->getSourceRange()); |
Chris Lattner | 3f5cd77 | 2009-04-05 00:59:53 +0000 | [diff] [blame] | 8727 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8728 | |
David Majnemer | c75d1a1 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 8729 | if (!TInfo->getType()->isDependentType()) { |
| 8730 | if (RequireCompleteType(TInfo->getTypeLoc().getBeginLoc(), TInfo->getType(), |
| 8731 | PDiag(diag::err_second_parameter_to_va_arg_incomplete) |
| 8732 | << TInfo->getTypeLoc().getSourceRange())) |
| 8733 | return ExprError(); |
David Majnemer | 254a5c0 | 2011-06-13 06:37:03 +0000 | [diff] [blame] | 8734 | |
David Majnemer | c75d1a1 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 8735 | if (RequireNonAbstractType(TInfo->getTypeLoc().getBeginLoc(), |
| 8736 | TInfo->getType(), |
| 8737 | PDiag(diag::err_second_parameter_to_va_arg_abstract) |
| 8738 | << TInfo->getTypeLoc().getSourceRange())) |
| 8739 | return ExprError(); |
| 8740 | |
Douglas Gregor | 7e1eb93 | 2011-07-30 06:45:27 +0000 | [diff] [blame] | 8741 | if (!TInfo->getType().isPODType(Context)) { |
David Majnemer | c75d1a1 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 8742 | Diag(TInfo->getTypeLoc().getBeginLoc(), |
Douglas Gregor | 7e1eb93 | 2011-07-30 06:45:27 +0000 | [diff] [blame] | 8743 | TInfo->getType()->isObjCLifetimeType() |
| 8744 | ? diag::warn_second_parameter_to_va_arg_ownership_qualified |
| 8745 | : diag::warn_second_parameter_to_va_arg_not_pod) |
David Majnemer | c75d1a1 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 8746 | << TInfo->getType() |
| 8747 | << TInfo->getTypeLoc().getSourceRange(); |
Douglas Gregor | 7e1eb93 | 2011-07-30 06:45:27 +0000 | [diff] [blame] | 8748 | } |
Eli Friedman | 6290ae4 | 2011-07-11 21:45:59 +0000 | [diff] [blame] | 8749 | |
| 8750 | // Check for va_arg where arguments of the given type will be promoted |
| 8751 | // (i.e. this va_arg is guaranteed to have undefined behavior). |
| 8752 | QualType PromoteType; |
| 8753 | if (TInfo->getType()->isPromotableIntegerType()) { |
| 8754 | PromoteType = Context.getPromotedIntegerType(TInfo->getType()); |
| 8755 | if (Context.typesAreCompatible(PromoteType, TInfo->getType())) |
| 8756 | PromoteType = QualType(); |
| 8757 | } |
| 8758 | if (TInfo->getType()->isSpecificBuiltinType(BuiltinType::Float)) |
| 8759 | PromoteType = Context.DoubleTy; |
| 8760 | if (!PromoteType.isNull()) |
| 8761 | Diag(TInfo->getTypeLoc().getBeginLoc(), |
| 8762 | diag::warn_second_parameter_to_va_arg_never_compatible) |
| 8763 | << TInfo->getType() |
| 8764 | << PromoteType |
| 8765 | << TInfo->getTypeLoc().getSourceRange(); |
David Majnemer | c75d1a1 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 8766 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8767 | |
Abramo Bagnara | 27db239 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 8768 | QualType T = TInfo->getType().getNonLValueExprType(Context); |
| 8769 | return Owned(new (Context) VAArgExpr(BuiltinLoc, E, TInfo, RPLoc, T)); |
Anders Carlsson | 7e13ab8 | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 8770 | } |
| 8771 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8772 | ExprResult Sema::ActOnGNUNullExpr(SourceLocation TokenLoc) { |
Douglas Gregor | 3be4b12 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 8773 | // The type of __null will be int or long, depending on the size of |
| 8774 | // pointers on the target. |
| 8775 | QualType Ty; |
NAKAMURA Takumi | 0d13fd3 | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 8776 | unsigned pw = Context.Target.getPointerWidth(0); |
| 8777 | if (pw == Context.Target.getIntWidth()) |
Douglas Gregor | 3be4b12 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 8778 | Ty = Context.IntTy; |
NAKAMURA Takumi | 0d13fd3 | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 8779 | else if (pw == Context.Target.getLongWidth()) |
Douglas Gregor | 3be4b12 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 8780 | Ty = Context.LongTy; |
NAKAMURA Takumi | 0d13fd3 | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 8781 | else if (pw == Context.Target.getLongLongWidth()) |
| 8782 | Ty = Context.LongLongTy; |
| 8783 | else { |
| 8784 | assert(!"I don't know size of pointer!"); |
| 8785 | Ty = Context.IntTy; |
| 8786 | } |
Douglas Gregor | 3be4b12 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 8787 | |
Sebastian Redl | 6d4256c | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8788 | return Owned(new (Context) GNUNullExpr(Ty, TokenLoc)); |
Douglas Gregor | 3be4b12 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 8789 | } |
| 8790 | |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 8791 | static void MakeObjCStringLiteralFixItHint(Sema& SemaRef, QualType DstType, |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 8792 | Expr *SrcExpr, FixItHint &Hint) { |
Anders Carlsson | ace5d07 | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 8793 | if (!SemaRef.getLangOptions().ObjC1) |
| 8794 | return; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8795 | |
Anders Carlsson | ace5d07 | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 8796 | const ObjCObjectPointerType *PT = DstType->getAs<ObjCObjectPointerType>(); |
| 8797 | if (!PT) |
| 8798 | return; |
| 8799 | |
| 8800 | // Check if the destination is of type 'id'. |
| 8801 | if (!PT->isObjCIdType()) { |
| 8802 | // Check if the destination is the 'NSString' interface. |
| 8803 | const ObjCInterfaceDecl *ID = PT->getInterfaceDecl(); |
| 8804 | if (!ID || !ID->getIdentifier()->isStr("NSString")) |
| 8805 | return; |
| 8806 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8807 | |
Anders Carlsson | ace5d07 | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 8808 | // Strip off any parens and casts. |
| 8809 | StringLiteral *SL = dyn_cast<StringLiteral>(SrcExpr->IgnoreParenCasts()); |
Douglas Gregor | fb65e59 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 8810 | if (!SL || !SL->isAscii()) |
Anders Carlsson | ace5d07 | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 8811 | return; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8812 | |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 8813 | Hint = FixItHint::CreateInsertion(SL->getLocStart(), "@"); |
Anders Carlsson | ace5d07 | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 8814 | } |
| 8815 | |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8816 | bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, |
| 8817 | SourceLocation Loc, |
| 8818 | QualType DstType, QualType SrcType, |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 8819 | Expr *SrcExpr, AssignmentAction Action, |
| 8820 | bool *Complained) { |
| 8821 | if (Complained) |
| 8822 | *Complained = false; |
| 8823 | |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8824 | // Decode the result (notice that AST's are still created for extensions). |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 8825 | bool CheckInferredResultType = false; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8826 | bool isInvalid = false; |
| 8827 | unsigned DiagKind; |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 8828 | FixItHint Hint; |
Anna Zaks | 3b40271 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 8829 | ConversionFixItGenerator ConvHints; |
| 8830 | bool MayHaveConvFixit = false; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8831 | |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8832 | switch (ConvTy) { |
| 8833 | default: assert(0 && "Unknown conversion type"); |
| 8834 | case Compatible: return false; |
Chris Lattner | 940cfeb | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 8835 | case PointerToInt: |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8836 | DiagKind = diag::ext_typecheck_convert_pointer_int; |
Anna Zaks | 3b40271 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 8837 | ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); |
| 8838 | MayHaveConvFixit = true; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8839 | break; |
Chris Lattner | 940cfeb | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 8840 | case IntToPointer: |
| 8841 | DiagKind = diag::ext_typecheck_convert_int_pointer; |
Anna Zaks | 3b40271 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 8842 | ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); |
| 8843 | MayHaveConvFixit = true; |
Chris Lattner | 940cfeb | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 8844 | break; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8845 | case IncompatiblePointer: |
Douglas Gregor | a771f46 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 8846 | MakeObjCStringLiteralFixItHint(*this, DstType, SrcExpr, Hint); |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8847 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer; |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 8848 | CheckInferredResultType = DstType->isObjCObjectPointerType() && |
| 8849 | SrcType->isObjCObjectPointerType(); |
Anna Zaks | 3b40271 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 8850 | if (Hint.isNull() && !CheckInferredResultType) { |
| 8851 | ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); |
| 8852 | } |
| 8853 | MayHaveConvFixit = true; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8854 | break; |
Eli Friedman | 80160bd | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 8855 | case IncompatiblePointerSign: |
| 8856 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer_sign; |
| 8857 | break; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8858 | case FunctionVoidPointer: |
| 8859 | DiagKind = diag::ext_typecheck_convert_pointer_void_func; |
| 8860 | break; |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 8861 | case IncompatiblePointerDiscardsQualifiers: { |
John McCall | 71de91c | 2011-02-01 23:28:01 +0000 | [diff] [blame] | 8862 | // Perform array-to-pointer decay if necessary. |
| 8863 | if (SrcType->isArrayType()) SrcType = Context.getArrayDecayedType(SrcType); |
| 8864 | |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 8865 | Qualifiers lhq = SrcType->getPointeeType().getQualifiers(); |
| 8866 | Qualifiers rhq = DstType->getPointeeType().getQualifiers(); |
| 8867 | if (lhq.getAddressSpace() != rhq.getAddressSpace()) { |
| 8868 | DiagKind = diag::err_typecheck_incompatible_address_space; |
| 8869 | break; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8870 | |
| 8871 | |
| 8872 | } else if (lhq.getObjCLifetime() != rhq.getObjCLifetime()) { |
Argyrios Kyrtzidis | cff00d9 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 8873 | DiagKind = diag::err_typecheck_incompatible_ownership; |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8874 | break; |
John McCall | 4fff8f6 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 8875 | } |
| 8876 | |
| 8877 | llvm_unreachable("unknown error case for discarding qualifiers!"); |
| 8878 | // fallthrough |
| 8879 | } |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8880 | case CompatiblePointerDiscardsQualifiers: |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 8881 | // If the qualifiers lost were because we were applying the |
| 8882 | // (deprecated) C++ conversion from a string literal to a char* |
| 8883 | // (or wchar_t*), then there was no error (C++ 4.2p2). FIXME: |
| 8884 | // Ideally, this check would be performed in |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 8885 | // checkPointerTypesForAssignment. However, that would require a |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 8886 | // bit of refactoring (so that the second argument is an |
| 8887 | // expression, rather than a type), which should be done as part |
John McCall | aba9082 | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 8888 | // of a larger effort to fix checkPointerTypesForAssignment for |
Douglas Gregor | aa1e21d | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 8889 | // C++ semantics. |
| 8890 | if (getLangOptions().CPlusPlus && |
| 8891 | IsStringLiteralToNonConstPointerConversion(SrcExpr, DstType)) |
| 8892 | return false; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8893 | DiagKind = diag::ext_typecheck_convert_discards_qualifiers; |
| 8894 | break; |
Alexis Hunt | 6f3de50 | 2009-11-08 07:46:34 +0000 | [diff] [blame] | 8895 | case IncompatibleNestedPointerQualifiers: |
Fariborz Jahanian | b98dade | 2009-11-09 22:16:37 +0000 | [diff] [blame] | 8896 | DiagKind = diag::ext_nested_pointer_qualifier_mismatch; |
Fariborz Jahanian | d7aa9d8 | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 8897 | break; |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 8898 | case IntToBlockPointer: |
| 8899 | DiagKind = diag::err_int_to_block_pointer; |
| 8900 | break; |
| 8901 | case IncompatibleBlockPointer: |
Mike Stump | d79b5a8 | 2009-04-21 22:51:42 +0000 | [diff] [blame] | 8902 | DiagKind = diag::err_typecheck_convert_incompatible_block_pointer; |
Steve Naroff | 081c742 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 8903 | break; |
Steve Naroff | 8afa989 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 8904 | case IncompatibleObjCQualifiedId: |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8905 | // FIXME: Diagnose the problem in ObjCQualifiedIdTypesAreCompatible, since |
Steve Naroff | 8afa989 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 8906 | // it can give a more specific diagnostic. |
| 8907 | DiagKind = diag::warn_incompatible_qualified_id; |
| 8908 | break; |
Anders Carlsson | db5a9b6 | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 8909 | case IncompatibleVectors: |
| 8910 | DiagKind = diag::warn_incompatible_vectors; |
| 8911 | break; |
Fariborz Jahanian | 6f472e8 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 8912 | case IncompatibleObjCWeakRef: |
| 8913 | DiagKind = diag::err_arc_weak_unavailable_assign; |
| 8914 | break; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8915 | case Incompatible: |
| 8916 | DiagKind = diag::err_typecheck_convert_incompatible; |
Anna Zaks | 3b40271 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 8917 | ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); |
| 8918 | MayHaveConvFixit = true; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8919 | isInvalid = true; |
| 8920 | break; |
| 8921 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8922 | |
Douglas Gregor | c68e140 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 8923 | QualType FirstType, SecondType; |
| 8924 | switch (Action) { |
| 8925 | case AA_Assigning: |
| 8926 | case AA_Initializing: |
| 8927 | // The destination type comes first. |
| 8928 | FirstType = DstType; |
| 8929 | SecondType = SrcType; |
| 8930 | break; |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 8931 | |
Douglas Gregor | c68e140 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 8932 | case AA_Returning: |
| 8933 | case AA_Passing: |
| 8934 | case AA_Converting: |
| 8935 | case AA_Sending: |
| 8936 | case AA_Casting: |
| 8937 | // The source type comes first. |
| 8938 | FirstType = SrcType; |
| 8939 | SecondType = DstType; |
| 8940 | break; |
| 8941 | } |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 8942 | |
Anna Zaks | 3b40271 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 8943 | PartialDiagnostic FDiag = PDiag(DiagKind); |
| 8944 | FDiag << FirstType << SecondType << Action << SrcExpr->getSourceRange(); |
| 8945 | |
| 8946 | // If we can fix the conversion, suggest the FixIts. |
| 8947 | assert(ConvHints.isNull() || Hint.isNull()); |
| 8948 | if (!ConvHints.isNull()) { |
| 8949 | for (llvm::SmallVector<FixItHint, 1>::iterator |
| 8950 | HI = ConvHints.Hints.begin(), HE = ConvHints.Hints.end(); |
| 8951 | HI != HE; ++HI) |
| 8952 | FDiag << *HI; |
| 8953 | } else { |
| 8954 | FDiag << Hint; |
| 8955 | } |
| 8956 | if (MayHaveConvFixit) { FDiag << (unsigned) (ConvHints.Kind); } |
| 8957 | |
| 8958 | Diag(Loc, FDiag); |
| 8959 | |
Douglas Gregor | 3382372 | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 8960 | if (CheckInferredResultType) |
| 8961 | EmitRelatedResultTypeNote(SrcExpr); |
| 8962 | |
Douglas Gregor | 4f4946a | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 8963 | if (Complained) |
| 8964 | *Complained = true; |
Chris Lattner | 9bad62c | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8965 | return isInvalid; |
| 8966 | } |
Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 8967 | |
Chris Lattner | c71d08b | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 8968 | bool Sema::VerifyIntegerConstantExpression(const Expr *E, llvm::APSInt *Result){ |
Eli Friedman | bb967cc | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 8969 | llvm::APSInt ICEResult; |
| 8970 | if (E->isIntegerConstantExpr(ICEResult, Context)) { |
| 8971 | if (Result) |
| 8972 | *Result = ICEResult; |
| 8973 | return false; |
| 8974 | } |
| 8975 | |
Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 8976 | Expr::EvalResult EvalResult; |
| 8977 | |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8978 | if (!E->Evaluate(EvalResult, Context) || !EvalResult.Val.isInt() || |
Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 8979 | EvalResult.HasSideEffects) { |
| 8980 | Diag(E->getExprLoc(), diag::err_expr_not_ice) << E->getSourceRange(); |
| 8981 | |
| 8982 | if (EvalResult.Diag) { |
| 8983 | // We only show the note if it's not the usual "invalid subexpression" |
| 8984 | // or if it's actually in a subexpression. |
| 8985 | if (EvalResult.Diag != diag::note_invalid_subexpr_in_ice || |
| 8986 | E->IgnoreParens() != EvalResult.DiagExpr->IgnoreParens()) |
| 8987 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
| 8988 | } |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8989 | |
Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 8990 | return true; |
| 8991 | } |
| 8992 | |
Eli Friedman | bb967cc | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 8993 | Diag(E->getExprLoc(), diag::ext_expr_not_ice) << |
| 8994 | E->getSourceRange(); |
Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 8995 | |
Eli Friedman | bb967cc | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 8996 | if (EvalResult.Diag && |
Argyrios Kyrtzidis | 1cb0de1 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 8997 | Diags.getDiagnosticLevel(diag::ext_expr_not_ice, EvalResult.DiagLoc) |
| 8998 | != Diagnostic::Ignored) |
Eli Friedman | bb967cc | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 8999 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
Mike Stump | 4e1f26a | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9000 | |
Anders Carlsson | e54e8a1 | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9001 | if (Result) |
| 9002 | *Result = EvalResult.Val.getInt(); |
| 9003 | return false; |
| 9004 | } |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9005 | |
Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9006 | void |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9007 | Sema::PushExpressionEvaluationContext(ExpressionEvaluationContext NewContext) { |
Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9008 | ExprEvalContexts.push_back( |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9009 | ExpressionEvaluationContextRecord(NewContext, |
| 9010 | ExprTemporaries.size(), |
| 9011 | ExprNeedsCleanups)); |
| 9012 | ExprNeedsCleanups = false; |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9013 | } |
| 9014 | |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 9015 | void Sema::PopExpressionEvaluationContext() { |
Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9016 | // Pop the current expression evaluation context off the stack. |
| 9017 | ExpressionEvaluationContextRecord Rec = ExprEvalContexts.back(); |
| 9018 | ExprEvalContexts.pop_back(); |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9019 | |
Douglas Gregor | fab31f4 | 2009-12-12 07:57:52 +0000 | [diff] [blame] | 9020 | if (Rec.Context == PotentiallyPotentiallyEvaluated) { |
| 9021 | if (Rec.PotentiallyReferenced) { |
| 9022 | // Mark any remaining declarations in the current position of the stack |
| 9023 | // as "referenced". If they were not meant to be referenced, semantic |
| 9024 | // analysis would have eliminated them (e.g., in ActOnCXXTypeId). |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9025 | for (PotentiallyReferencedDecls::iterator |
Douglas Gregor | fab31f4 | 2009-12-12 07:57:52 +0000 | [diff] [blame] | 9026 | I = Rec.PotentiallyReferenced->begin(), |
| 9027 | IEnd = Rec.PotentiallyReferenced->end(); |
| 9028 | I != IEnd; ++I) |
| 9029 | MarkDeclarationReferenced(I->first, I->second); |
| 9030 | } |
| 9031 | |
| 9032 | if (Rec.PotentiallyDiagnosed) { |
| 9033 | // Emit any pending diagnostics. |
| 9034 | for (PotentiallyEmittedDiagnostics::iterator |
| 9035 | I = Rec.PotentiallyDiagnosed->begin(), |
| 9036 | IEnd = Rec.PotentiallyDiagnosed->end(); |
| 9037 | I != IEnd; ++I) |
| 9038 | Diag(I->first, I->second); |
| 9039 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9040 | } |
Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9041 | |
| 9042 | // When are coming out of an unevaluated context, clear out any |
| 9043 | // temporaries that we may have created as part of the evaluation of |
| 9044 | // the expression in that context: they aren't relevant because they |
| 9045 | // will never be constructed. |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9046 | if (Rec.Context == Unevaluated) { |
Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9047 | ExprTemporaries.erase(ExprTemporaries.begin() + Rec.NumTemporaries, |
| 9048 | ExprTemporaries.end()); |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9049 | ExprNeedsCleanups = Rec.ParentNeedsCleanups; |
| 9050 | |
| 9051 | // Otherwise, merge the contexts together. |
| 9052 | } else { |
| 9053 | ExprNeedsCleanups |= Rec.ParentNeedsCleanups; |
| 9054 | } |
Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9055 | |
| 9056 | // Destroy the popped expression evaluation record. |
| 9057 | Rec.Destroy(); |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9058 | } |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9059 | |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9060 | void Sema::DiscardCleanupsInEvaluationContext() { |
| 9061 | ExprTemporaries.erase( |
| 9062 | ExprTemporaries.begin() + ExprEvalContexts.back().NumTemporaries, |
| 9063 | ExprTemporaries.end()); |
| 9064 | ExprNeedsCleanups = false; |
| 9065 | } |
| 9066 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9067 | /// \brief Note that the given declaration was referenced in the source code. |
| 9068 | /// |
| 9069 | /// This routine should be invoke whenever a given declaration is referenced |
| 9070 | /// in the source code, and where that reference occurred. If this declaration |
| 9071 | /// reference means that the the declaration is used (C++ [basic.def.odr]p2, |
| 9072 | /// C99 6.9p3), then the declaration will be marked as used. |
| 9073 | /// |
| 9074 | /// \param Loc the location where the declaration was referenced. |
| 9075 | /// |
| 9076 | /// \param D the declaration that has been referenced by the source code. |
| 9077 | void Sema::MarkDeclarationReferenced(SourceLocation Loc, Decl *D) { |
| 9078 | assert(D && "No declaration?"); |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9079 | |
Argyrios Kyrtzidis | 1618023 | 2011-04-19 19:51:10 +0000 | [diff] [blame] | 9080 | D->setReferenced(); |
| 9081 | |
Douglas Gregor | ebada077 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 9082 | if (D->isUsed(false)) |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 9083 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9084 | |
Richard Trieu | cfc491d | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 9085 | // Mark a parameter or variable declaration "used", regardless of whether |
| 9086 | // we're in a template or not. The reason for this is that unevaluated |
| 9087 | // expressions (e.g. (void)sizeof()) constitute a use for warning purposes |
| 9088 | // (-Wunused-variables and -Wunused-parameters) |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9089 | if (isa<ParmVarDecl>(D) || |
Douglas Gregor | fd27fed | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 9090 | (isa<VarDecl>(D) && D->getDeclContext()->isFunctionOrMethod())) { |
Anders Carlsson | 73067a0 | 2010-10-22 23:37:08 +0000 | [diff] [blame] | 9091 | D->setUsed(); |
Douglas Gregor | fd27fed | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 9092 | return; |
| 9093 | } |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9094 | |
Douglas Gregor | fd27fed | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 9095 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) |
| 9096 | return; |
Alexis Hunt | c46382e | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9097 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9098 | // Do not mark anything as "used" within a dependent context; wait for |
| 9099 | // an instantiation. |
| 9100 | if (CurContext->isDependentContext()) |
| 9101 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9102 | |
Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9103 | switch (ExprEvalContexts.back().Context) { |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9104 | case Unevaluated: |
| 9105 | // We are in an expression that is not potentially evaluated; do nothing. |
| 9106 | return; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9107 | |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9108 | case PotentiallyEvaluated: |
| 9109 | // We are in a potentially-evaluated expression, so this declaration is |
| 9110 | // "used"; handle this below. |
| 9111 | break; |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9112 | |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9113 | case PotentiallyPotentiallyEvaluated: |
| 9114 | // We are in an expression that may be potentially evaluated; queue this |
| 9115 | // declaration reference until we know whether the expression is |
| 9116 | // potentially evaluated. |
Douglas Gregor | ff790f1 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9117 | ExprEvalContexts.back().addReferencedDecl(Loc, D); |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9118 | return; |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9119 | |
| 9120 | case PotentiallyEvaluatedIfUsed: |
| 9121 | // Referenced declarations will only be used if the construct in the |
| 9122 | // containing expression is used. |
| 9123 | return; |
Douglas Gregor | 0b6a624 | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9124 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9125 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9126 | // Note that this declaration has been used. |
Fariborz Jahanian | 3a36343 | 2009-06-22 17:30:33 +0000 | [diff] [blame] | 9127 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
Alexis Hunt | f92197c | 2011-05-12 03:51:51 +0000 | [diff] [blame] | 9128 | if (Constructor->isDefaulted() && Constructor->isDefaultConstructor()) { |
| 9129 | if (Constructor->isTrivial()) |
Chandler Carruth | c926240 | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 9130 | return; |
| 9131 | if (!Constructor->isUsed(false)) |
| 9132 | DefineImplicitDefaultConstructor(Loc, Constructor); |
Alexis Hunt | 22b5b13 | 2011-05-14 18:20:50 +0000 | [diff] [blame] | 9133 | } else if (Constructor->isDefaulted() && |
Alexis Hunt | 913820d | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 9134 | Constructor->isCopyConstructor()) { |
Douglas Gregor | ebada077 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 9135 | if (!Constructor->isUsed(false)) |
Alexis Hunt | 913820d | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 9136 | DefineImplicitCopyConstructor(Loc, Constructor); |
Fariborz Jahanian | 477d242 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 9137 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9138 | |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9139 | MarkVTableUsed(Loc, Constructor->getParent()); |
Fariborz Jahanian | 24a175b | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 9140 | } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) { |
Alexis Hunt | f9172946 | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 9141 | if (Destructor->isDefaulted() && !Destructor->isUsed(false)) |
Fariborz Jahanian | 24a175b | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 9142 | DefineImplicitDestructor(Loc, Destructor); |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9143 | if (Destructor->isVirtual()) |
| 9144 | MarkVTableUsed(Loc, Destructor->getParent()); |
Fariborz Jahanian | 41f7927 | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 9145 | } else if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) { |
Alexis Hunt | c9a5573 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 9146 | if (MethodDecl->isDefaulted() && MethodDecl->isOverloadedOperator() && |
Fariborz Jahanian | 41f7927 | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 9147 | MethodDecl->getOverloadedOperator() == OO_Equal) { |
Douglas Gregor | ebada077 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 9148 | if (!MethodDecl->isUsed(false)) |
Douglas Gregor | a57478e | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 9149 | DefineImplicitCopyAssignment(Loc, MethodDecl); |
Douglas Gregor | 88d292c | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9150 | } else if (MethodDecl->isVirtual()) |
| 9151 | MarkVTableUsed(Loc, MethodDecl->getParent()); |
Fariborz Jahanian | 41f7927 | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 9152 | } |
Fariborz Jahanian | 49796cc7 | 2009-06-24 22:09:44 +0000 | [diff] [blame] | 9153 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
John McCall | 8377967 | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9154 | // Recursive functions should be marked when used from another function. |
| 9155 | if (CurContext == Function) return; |
| 9156 | |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9157 | // Implicit instantiation of function templates and member functions of |
Douglas Gregor | 4adbc6d | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 9158 | // class templates. |
Douglas Gregor | 69f6a36 | 2010-05-17 17:34:56 +0000 | [diff] [blame] | 9159 | if (Function->isImplicitlyInstantiable()) { |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9160 | bool AlreadyInstantiated = false; |
| 9161 | if (FunctionTemplateSpecializationInfo *SpecInfo |
| 9162 | = Function->getTemplateSpecializationInfo()) { |
| 9163 | if (SpecInfo->getPointOfInstantiation().isInvalid()) |
| 9164 | SpecInfo->setPointOfInstantiation(Loc); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9165 | else if (SpecInfo->getTemplateSpecializationKind() |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 9166 | == TSK_ImplicitInstantiation) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9167 | AlreadyInstantiated = true; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9168 | } else if (MemberSpecializationInfo *MSInfo |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9169 | = Function->getMemberSpecializationInfo()) { |
| 9170 | if (MSInfo->getPointOfInstantiation().isInvalid()) |
| 9171 | MSInfo->setPointOfInstantiation(Loc); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9172 | else if (MSInfo->getTemplateSpecializationKind() |
Douglas Gregor | afca3b4 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 9173 | == TSK_ImplicitInstantiation) |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9174 | AlreadyInstantiated = true; |
| 9175 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9176 | |
Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 9177 | if (!AlreadyInstantiated) { |
| 9178 | if (isa<CXXRecordDecl>(Function->getDeclContext()) && |
| 9179 | cast<CXXRecordDecl>(Function->getDeclContext())->isLocalClass()) |
| 9180 | PendingLocalImplicitInstantiations.push_back(std::make_pair(Function, |
| 9181 | Loc)); |
| 9182 | else |
Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 9183 | PendingInstantiations.push_back(std::make_pair(Function, Loc)); |
Douglas Gregor | 7f792cf | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 9184 | } |
John McCall | 8377967 | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9185 | } else { |
| 9186 | // Walk redefinitions, as some of them may be instantiable. |
Gabor Greif | b6aba3e | 2010-08-28 00:16:06 +0000 | [diff] [blame] | 9187 | for (FunctionDecl::redecl_iterator i(Function->redecls_begin()), |
| 9188 | e(Function->redecls_end()); i != e; ++i) { |
Gabor Greif | 34ecff2 | 2010-08-28 01:58:12 +0000 | [diff] [blame] | 9189 | if (!i->isUsed(false) && i->isImplicitlyInstantiable()) |
Gabor Greif | b6aba3e | 2010-08-28 00:16:06 +0000 | [diff] [blame] | 9190 | MarkDeclarationReferenced(Loc, *i); |
| 9191 | } |
John McCall | 8377967 | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9192 | } |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9193 | |
John McCall | 8377967 | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9194 | // Keep track of used but undefined functions. |
| 9195 | if (!Function->isPure() && !Function->hasBody() && |
| 9196 | Function->getLinkage() != ExternalLinkage) { |
| 9197 | SourceLocation &old = UndefinedInternals[Function->getCanonicalDecl()]; |
| 9198 | if (old.isInvalid()) old = Loc; |
| 9199 | } |
Argyrios Kyrtzidis | dfffabd | 2010-08-25 10:34:54 +0000 | [diff] [blame] | 9200 | |
John McCall | 8377967 | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9201 | Function->setUsed(true); |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9202 | return; |
Douglas Gregor | 77b50e1 | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 9203 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9204 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9205 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) { |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 9206 | // Implicit instantiation of static data members of class templates. |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9207 | if (Var->isStaticDataMember() && |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9208 | Var->getInstantiatedFromStaticDataMember()) { |
| 9209 | MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo(); |
| 9210 | assert(MSInfo && "Missing member specialization information?"); |
| 9211 | if (MSInfo->getPointOfInstantiation().isInvalid() && |
| 9212 | MSInfo->getTemplateSpecializationKind()== TSK_ImplicitInstantiation) { |
| 9213 | MSInfo->setPointOfInstantiation(Loc); |
Sebastian Redl | 2ac2c72 | 2011-04-29 08:19:30 +0000 | [diff] [blame] | 9214 | // This is a modification of an existing AST node. Notify listeners. |
| 9215 | if (ASTMutationListener *L = getASTMutationListener()) |
| 9216 | L->StaticDataMemberInstantiated(Var); |
Chandler Carruth | 5408017 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 9217 | PendingInstantiations.push_back(std::make_pair(Var, Loc)); |
Douglas Gregor | 06db9f5 | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9218 | } |
| 9219 | } |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9220 | |
John McCall | 15dd404 | 2011-02-21 19:25:48 +0000 | [diff] [blame] | 9221 | // Keep track of used but undefined variables. We make a hole in |
| 9222 | // the warning for static const data members with in-line |
| 9223 | // initializers. |
John McCall | 8377967 | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9224 | if (Var->hasDefinition() == VarDecl::DeclarationOnly |
John McCall | 15dd404 | 2011-02-21 19:25:48 +0000 | [diff] [blame] | 9225 | && Var->getLinkage() != ExternalLinkage |
| 9226 | && !(Var->isStaticDataMember() && Var->hasInit())) { |
John McCall | 8377967 | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9227 | SourceLocation &old = UndefinedInternals[Var->getCanonicalDecl()]; |
| 9228 | if (old.isInvalid()) old = Loc; |
| 9229 | } |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 9230 | |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9231 | D->setUsed(true); |
Douglas Gregor | a6ef8f0 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 9232 | return; |
Sam Weinig | bae6914 | 2009-09-11 03:29:30 +0000 | [diff] [blame] | 9233 | } |
Douglas Gregor | c9c02ed | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9234 | } |
Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 9235 | |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9236 | namespace { |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9237 | // Mark all of the declarations referenced |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9238 | // FIXME: Not fully implemented yet! We need to have a better understanding |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9239 | // of when we're entering |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9240 | class MarkReferencedDecls : public RecursiveASTVisitor<MarkReferencedDecls> { |
| 9241 | Sema &S; |
| 9242 | SourceLocation Loc; |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9243 | |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9244 | public: |
| 9245 | typedef RecursiveASTVisitor<MarkReferencedDecls> Inherited; |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9246 | |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9247 | MarkReferencedDecls(Sema &S, SourceLocation Loc) : S(S), Loc(Loc) { } |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9248 | |
| 9249 | bool TraverseTemplateArgument(const TemplateArgument &Arg); |
| 9250 | bool TraverseRecordType(RecordType *T); |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9251 | }; |
| 9252 | } |
| 9253 | |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9254 | bool MarkReferencedDecls::TraverseTemplateArgument( |
| 9255 | const TemplateArgument &Arg) { |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9256 | if (Arg.getKind() == TemplateArgument::Declaration) { |
| 9257 | S.MarkDeclarationReferenced(Loc, Arg.getAsDecl()); |
| 9258 | } |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9259 | |
| 9260 | return Inherited::TraverseTemplateArgument(Arg); |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9261 | } |
| 9262 | |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9263 | bool MarkReferencedDecls::TraverseRecordType(RecordType *T) { |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9264 | if (ClassTemplateSpecializationDecl *Spec |
| 9265 | = dyn_cast<ClassTemplateSpecializationDecl>(T->getDecl())) { |
| 9266 | const TemplateArgumentList &Args = Spec->getTemplateArgs(); |
Douglas Gregor | 1ccc841 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 9267 | return TraverseTemplateArguments(Args.data(), Args.size()); |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9268 | } |
| 9269 | |
Chandler Carruth | c65667c | 2010-06-10 10:31:57 +0000 | [diff] [blame] | 9270 | return true; |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9271 | } |
| 9272 | |
| 9273 | void Sema::MarkDeclarationsReferencedInType(SourceLocation Loc, QualType T) { |
| 9274 | MarkReferencedDecls Marker(*this, Loc); |
Chandler Carruth | af80f66 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9275 | Marker.TraverseType(Context.getCanonicalType(T)); |
Douglas Gregor | 5597ab4 | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9276 | } |
| 9277 | |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9278 | namespace { |
| 9279 | /// \brief Helper class that marks all of the declarations referenced by |
| 9280 | /// potentially-evaluated subexpressions as "referenced". |
| 9281 | class EvaluatedExprMarker : public EvaluatedExprVisitor<EvaluatedExprMarker> { |
| 9282 | Sema &S; |
| 9283 | |
| 9284 | public: |
| 9285 | typedef EvaluatedExprVisitor<EvaluatedExprMarker> Inherited; |
| 9286 | |
| 9287 | explicit EvaluatedExprMarker(Sema &S) : Inherited(S.Context), S(S) { } |
| 9288 | |
| 9289 | void VisitDeclRefExpr(DeclRefExpr *E) { |
| 9290 | S.MarkDeclarationReferenced(E->getLocation(), E->getDecl()); |
| 9291 | } |
| 9292 | |
| 9293 | void VisitMemberExpr(MemberExpr *E) { |
| 9294 | S.MarkDeclarationReferenced(E->getMemberLoc(), E->getMemberDecl()); |
Douglas Gregor | 32b3de5 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 9295 | Inherited::VisitMemberExpr(E); |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9296 | } |
| 9297 | |
| 9298 | void VisitCXXNewExpr(CXXNewExpr *E) { |
| 9299 | if (E->getConstructor()) |
| 9300 | S.MarkDeclarationReferenced(E->getLocStart(), E->getConstructor()); |
| 9301 | if (E->getOperatorNew()) |
| 9302 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorNew()); |
| 9303 | if (E->getOperatorDelete()) |
| 9304 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorDelete()); |
Douglas Gregor | 32b3de5 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 9305 | Inherited::VisitCXXNewExpr(E); |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9306 | } |
| 9307 | |
| 9308 | void VisitCXXDeleteExpr(CXXDeleteExpr *E) { |
| 9309 | if (E->getOperatorDelete()) |
| 9310 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorDelete()); |
Douglas Gregor | 6ed2fee | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 9311 | QualType Destroyed = S.Context.getBaseElementType(E->getDestroyedType()); |
| 9312 | if (const RecordType *DestroyedRec = Destroyed->getAs<RecordType>()) { |
| 9313 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DestroyedRec->getDecl()); |
| 9314 | S.MarkDeclarationReferenced(E->getLocStart(), |
| 9315 | S.LookupDestructor(Record)); |
| 9316 | } |
| 9317 | |
Douglas Gregor | 32b3de5 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 9318 | Inherited::VisitCXXDeleteExpr(E); |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9319 | } |
| 9320 | |
| 9321 | void VisitCXXConstructExpr(CXXConstructExpr *E) { |
| 9322 | S.MarkDeclarationReferenced(E->getLocStart(), E->getConstructor()); |
Douglas Gregor | 32b3de5 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 9323 | Inherited::VisitCXXConstructExpr(E); |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9324 | } |
| 9325 | |
| 9326 | void VisitBlockDeclRefExpr(BlockDeclRefExpr *E) { |
| 9327 | S.MarkDeclarationReferenced(E->getLocation(), E->getDecl()); |
| 9328 | } |
Douglas Gregor | f0873f4 | 2010-10-19 17:17:35 +0000 | [diff] [blame] | 9329 | |
| 9330 | void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { |
| 9331 | Visit(E->getExpr()); |
| 9332 | } |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9333 | }; |
| 9334 | } |
| 9335 | |
| 9336 | /// \brief Mark any declarations that appear within this expression or any |
| 9337 | /// potentially-evaluated subexpressions as "referenced". |
| 9338 | void Sema::MarkDeclarationsReferencedInExpr(Expr *E) { |
| 9339 | EvaluatedExprMarker(*this).Visit(E); |
| 9340 | } |
| 9341 | |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9342 | /// \brief Emit a diagnostic that describes an effect on the run-time behavior |
| 9343 | /// of the program being compiled. |
| 9344 | /// |
| 9345 | /// This routine emits the given diagnostic when the code currently being |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9346 | /// type-checked is "potentially evaluated", meaning that there is a |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9347 | /// possibility that the code will actually be executable. Code in sizeof() |
| 9348 | /// expressions, code used only during overload resolution, etc., are not |
| 9349 | /// potentially evaluated. This routine will suppress such diagnostics or, |
| 9350 | /// in the absolutely nutty case of potentially potentially evaluated |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9351 | /// expressions (C++ typeid), queue the diagnostic to potentially emit it |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9352 | /// later. |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9353 | /// |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9354 | /// This routine should be used for all diagnostics that describe the run-time |
| 9355 | /// behavior of a program, such as passing a non-POD value through an ellipsis. |
| 9356 | /// Failure to do so will likely result in spurious diagnostics or failures |
| 9357 | /// during overload resolution or within sizeof/alignof/typeof/typeid. |
Ted Kremenek | 55ae319 | 2011-02-23 01:51:43 +0000 | [diff] [blame] | 9358 | bool Sema::DiagRuntimeBehavior(SourceLocation Loc, const Stmt *stmt, |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9359 | const PartialDiagnostic &PD) { |
John McCall | 31168b0 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9360 | switch (ExprEvalContexts.back().Context) { |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9361 | case Unevaluated: |
| 9362 | // The argument will never be evaluated, so don't complain. |
| 9363 | break; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9364 | |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9365 | case PotentiallyEvaluated: |
Douglas Gregor | 8a01b2a | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9366 | case PotentiallyEvaluatedIfUsed: |
Ted Kremenek | 3427fac | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 9367 | if (stmt && getCurFunctionOrMethodDecl()) { |
| 9368 | FunctionScopes.back()->PossiblyUnreachableDiags. |
| 9369 | push_back(sema::PossiblyUnreachableDiag(PD, Loc, stmt)); |
| 9370 | } |
| 9371 | else |
| 9372 | Diag(Loc, PD); |
| 9373 | |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9374 | return true; |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9375 | |
Douglas Gregor | da8cdbc | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9376 | case PotentiallyPotentiallyEvaluated: |
| 9377 | ExprEvalContexts.back().addDiagnostic(Loc, PD); |
| 9378 | break; |
| 9379 | } |
| 9380 | |
| 9381 | return false; |
| 9382 | } |
| 9383 | |
Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 9384 | bool Sema::CheckCallReturnType(QualType ReturnType, SourceLocation Loc, |
| 9385 | CallExpr *CE, FunctionDecl *FD) { |
| 9386 | if (ReturnType->isVoidType() || !ReturnType->isIncompleteType()) |
| 9387 | return false; |
| 9388 | |
| 9389 | PartialDiagnostic Note = |
| 9390 | FD ? PDiag(diag::note_function_with_incomplete_return_type_declared_here) |
| 9391 | << FD->getDeclName() : PDiag(); |
| 9392 | SourceLocation NoteLoc = FD ? FD->getLocation() : SourceLocation(); |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9393 | |
Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 9394 | if (RequireCompleteType(Loc, ReturnType, |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9395 | FD ? |
Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 9396 | PDiag(diag::err_call_function_incomplete_return) |
| 9397 | << CE->getSourceRange() << FD->getDeclName() : |
Kovarththanan Rajaratnam | ba2c652 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9398 | PDiag(diag::err_call_incomplete_return) |
Anders Carlsson | 7f84ed9 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 9399 | << CE->getSourceRange(), |
| 9400 | std::make_pair(NoteLoc, Note))) |
| 9401 | return true; |
| 9402 | |
| 9403 | return false; |
| 9404 | } |
| 9405 | |
Douglas Gregor | 2d4f64f | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9406 | // Diagnose the s/=/==/ and s/\|=/!=/ typos. Note that adding parentheses |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9407 | // will prevent this condition from triggering, which is what we want. |
| 9408 | void Sema::DiagnoseAssignmentAsCondition(Expr *E) { |
| 9409 | SourceLocation Loc; |
| 9410 | |
John McCall | 0506e4a | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 9411 | unsigned diagnostic = diag::warn_condition_is_assignment; |
Douglas Gregor | 2d4f64f | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9412 | bool IsOrAssign = false; |
John McCall | 0506e4a | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 9413 | |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9414 | if (isa<BinaryOperator>(E)) { |
| 9415 | BinaryOperator *Op = cast<BinaryOperator>(E); |
Douglas Gregor | 2d4f64f | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9416 | if (Op->getOpcode() != BO_Assign && Op->getOpcode() != BO_OrAssign) |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9417 | return; |
| 9418 | |
Douglas Gregor | 2d4f64f | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9419 | IsOrAssign = Op->getOpcode() == BO_OrAssign; |
| 9420 | |
John McCall | b0e419e | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 9421 | // Greylist some idioms by putting them into a warning subcategory. |
| 9422 | if (ObjCMessageExpr *ME |
| 9423 | = dyn_cast<ObjCMessageExpr>(Op->getRHS()->IgnoreParenCasts())) { |
| 9424 | Selector Sel = ME->getSelector(); |
| 9425 | |
John McCall | b0e419e | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 9426 | // self = [<foo> init...] |
Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 9427 | if (isSelfExpr(Op->getLHS()) && Sel.getNameForSlot(0).startswith("init")) |
John McCall | b0e419e | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 9428 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 9429 | |
| 9430 | // <foo> = [<bar> nextObject] |
Douglas Gregor | af2a6ae | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 9431 | else if (Sel.isUnarySelector() && Sel.getNameForSlot(0) == "nextObject") |
John McCall | b0e419e | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 9432 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 9433 | } |
John McCall | 0506e4a | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 9434 | |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9435 | Loc = Op->getOperatorLoc(); |
| 9436 | } else if (isa<CXXOperatorCallExpr>(E)) { |
| 9437 | CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(E); |
Douglas Gregor | 2d4f64f | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9438 | if (Op->getOperator() != OO_Equal && Op->getOperator() != OO_PipeEqual) |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9439 | return; |
| 9440 | |
Douglas Gregor | 2d4f64f | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9441 | IsOrAssign = Op->getOperator() == OO_PipeEqual; |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9442 | Loc = Op->getOperatorLoc(); |
| 9443 | } else { |
| 9444 | // Not an assignment. |
| 9445 | return; |
| 9446 | } |
| 9447 | |
Douglas Gregor | 2bf2d3d | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 9448 | Diag(Loc, diagnostic) << E->getSourceRange(); |
Douglas Gregor | 2d4f64f | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9449 | |
Argyrios Kyrtzidis | e1b97c4 | 2011-04-25 23:01:29 +0000 | [diff] [blame] | 9450 | SourceLocation Open = E->getSourceRange().getBegin(); |
| 9451 | SourceLocation Close = PP.getLocForEndOfToken(E->getSourceRange().getEnd()); |
| 9452 | Diag(Loc, diag::note_condition_assign_silence) |
| 9453 | << FixItHint::CreateInsertion(Open, "(") |
| 9454 | << FixItHint::CreateInsertion(Close, ")"); |
| 9455 | |
Douglas Gregor | 2d4f64f | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9456 | if (IsOrAssign) |
| 9457 | Diag(Loc, diag::note_condition_or_assign_to_comparison) |
| 9458 | << FixItHint::CreateReplacement(Loc, "!="); |
| 9459 | else |
| 9460 | Diag(Loc, diag::note_condition_assign_to_comparison) |
| 9461 | << FixItHint::CreateReplacement(Loc, "=="); |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9462 | } |
| 9463 | |
Argyrios Kyrtzidis | 8b6ec68 | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 9464 | /// \brief Redundant parentheses over an equality comparison can indicate |
| 9465 | /// that the user intended an assignment used as condition. |
| 9466 | void Sema::DiagnoseEqualityWithExtraParens(ParenExpr *parenE) { |
Argyrios Kyrtzidis | f4f8278 | 2011-02-01 22:23:56 +0000 | [diff] [blame] | 9467 | // Don't warn if the parens came from a macro. |
| 9468 | SourceLocation parenLoc = parenE->getLocStart(); |
| 9469 | if (parenLoc.isInvalid() || parenLoc.isMacroID()) |
| 9470 | return; |
Argyrios Kyrtzidis | ba699d6 | 2011-03-28 23:52:04 +0000 | [diff] [blame] | 9471 | // Don't warn for dependent expressions. |
| 9472 | if (parenE->isTypeDependent()) |
| 9473 | return; |
Argyrios Kyrtzidis | f4f8278 | 2011-02-01 22:23:56 +0000 | [diff] [blame] | 9474 | |
Argyrios Kyrtzidis | 8b6ec68 | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 9475 | Expr *E = parenE->IgnoreParens(); |
| 9476 | |
| 9477 | if (BinaryOperator *opE = dyn_cast<BinaryOperator>(E)) |
Argyrios Kyrtzidis | 582dd68 | 2011-02-01 19:32:59 +0000 | [diff] [blame] | 9478 | if (opE->getOpcode() == BO_EQ && |
| 9479 | opE->getLHS()->IgnoreParenImpCasts()->isModifiableLvalue(Context) |
| 9480 | == Expr::MLV_Valid) { |
Argyrios Kyrtzidis | 8b6ec68 | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 9481 | SourceLocation Loc = opE->getOperatorLoc(); |
Ted Kremenek | c358d9f | 2011-02-01 22:36:09 +0000 | [diff] [blame] | 9482 | |
Ted Kremenek | ae02209 | 2011-02-02 02:20:30 +0000 | [diff] [blame] | 9483 | Diag(Loc, diag::warn_equality_with_extra_parens) << E->getSourceRange(); |
Ted Kremenek | ae02209 | 2011-02-02 02:20:30 +0000 | [diff] [blame] | 9484 | Diag(Loc, diag::note_equality_comparison_silence) |
| 9485 | << FixItHint::CreateRemoval(parenE->getSourceRange().getBegin()) |
| 9486 | << FixItHint::CreateRemoval(parenE->getSourceRange().getEnd()); |
Argyrios Kyrtzidis | e1b97c4 | 2011-04-25 23:01:29 +0000 | [diff] [blame] | 9487 | Diag(Loc, diag::note_equality_comparison_to_assign) |
| 9488 | << FixItHint::CreateReplacement(Loc, "="); |
Argyrios Kyrtzidis | 8b6ec68 | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 9489 | } |
| 9490 | } |
| 9491 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9492 | ExprResult Sema::CheckBooleanCondition(Expr *E, SourceLocation Loc) { |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9493 | DiagnoseAssignmentAsCondition(E); |
Argyrios Kyrtzidis | 8b6ec68 | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 9494 | if (ParenExpr *parenE = dyn_cast<ParenExpr>(E)) |
| 9495 | DiagnoseEqualityWithExtraParens(parenE); |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9496 | |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9497 | ExprResult result = CheckPlaceholderExpr(E); |
| 9498 | if (result.isInvalid()) return ExprError(); |
| 9499 | E = result.take(); |
Argyrios Kyrtzidis | ca76629 | 2010-11-01 18:49:26 +0000 | [diff] [blame] | 9500 | |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9501 | if (!E->isTypeDependent()) { |
John McCall | 34376a6 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 9502 | if (getLangOptions().CPlusPlus) |
| 9503 | return CheckCXXBooleanCondition(E); // C++ 6.4p4 |
| 9504 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9505 | ExprResult ERes = DefaultFunctionArrayLvalueConversion(E); |
| 9506 | if (ERes.isInvalid()) |
| 9507 | return ExprError(); |
| 9508 | E = ERes.take(); |
John McCall | 29cb2fd | 2010-12-04 06:09:13 +0000 | [diff] [blame] | 9509 | |
| 9510 | QualType T = E->getType(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9511 | if (!T->isScalarType()) { // C99 6.8.4.1p1 |
| 9512 | Diag(Loc, diag::err_typecheck_statement_requires_scalar) |
| 9513 | << T << E->getSourceRange(); |
| 9514 | return ExprError(); |
| 9515 | } |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9516 | } |
| 9517 | |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9518 | return Owned(E); |
John McCall | d5707ab | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9519 | } |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 9520 | |
John McCall | dadc575 | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9521 | ExprResult Sema::ActOnBooleanCondition(Scope *S, SourceLocation Loc, |
| 9522 | Expr *Sub) { |
Douglas Gregor | 12cc7ee | 2010-05-06 21:39:56 +0000 | [diff] [blame] | 9523 | if (!Sub) |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 9524 | return ExprError(); |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9525 | |
| 9526 | return CheckBooleanCondition(Sub, Loc); |
Douglas Gregor | e60e41a | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 9527 | } |
John McCall | 36e7fe3 | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 9528 | |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9529 | namespace { |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9530 | /// A visitor for rebuilding a call to an __unknown_any expression |
| 9531 | /// to have an appropriate type. |
| 9532 | struct RebuildUnknownAnyFunction |
| 9533 | : StmtVisitor<RebuildUnknownAnyFunction, ExprResult> { |
| 9534 | |
| 9535 | Sema &S; |
| 9536 | |
| 9537 | RebuildUnknownAnyFunction(Sema &S) : S(S) {} |
| 9538 | |
| 9539 | ExprResult VisitStmt(Stmt *S) { |
| 9540 | llvm_unreachable("unexpected statement!"); |
| 9541 | return ExprError(); |
| 9542 | } |
| 9543 | |
| 9544 | ExprResult VisitExpr(Expr *expr) { |
| 9545 | S.Diag(expr->getExprLoc(), diag::err_unsupported_unknown_any_call) |
| 9546 | << expr->getSourceRange(); |
| 9547 | return ExprError(); |
| 9548 | } |
| 9549 | |
| 9550 | /// Rebuild an expression which simply semantically wraps another |
| 9551 | /// expression which it shares the type and value kind of. |
| 9552 | template <class T> ExprResult rebuildSugarExpr(T *expr) { |
| 9553 | ExprResult subResult = Visit(expr->getSubExpr()); |
| 9554 | if (subResult.isInvalid()) return ExprError(); |
| 9555 | |
| 9556 | Expr *subExpr = subResult.take(); |
| 9557 | expr->setSubExpr(subExpr); |
| 9558 | expr->setType(subExpr->getType()); |
| 9559 | expr->setValueKind(subExpr->getValueKind()); |
| 9560 | assert(expr->getObjectKind() == OK_Ordinary); |
| 9561 | return expr; |
| 9562 | } |
| 9563 | |
| 9564 | ExprResult VisitParenExpr(ParenExpr *paren) { |
| 9565 | return rebuildSugarExpr(paren); |
| 9566 | } |
| 9567 | |
| 9568 | ExprResult VisitUnaryExtension(UnaryOperator *op) { |
| 9569 | return rebuildSugarExpr(op); |
| 9570 | } |
| 9571 | |
| 9572 | ExprResult VisitUnaryAddrOf(UnaryOperator *op) { |
| 9573 | ExprResult subResult = Visit(op->getSubExpr()); |
| 9574 | if (subResult.isInvalid()) return ExprError(); |
| 9575 | |
| 9576 | Expr *subExpr = subResult.take(); |
| 9577 | op->setSubExpr(subExpr); |
| 9578 | op->setType(S.Context.getPointerType(subExpr->getType())); |
| 9579 | assert(op->getValueKind() == VK_RValue); |
| 9580 | assert(op->getObjectKind() == OK_Ordinary); |
| 9581 | return op; |
| 9582 | } |
| 9583 | |
| 9584 | ExprResult resolveDecl(Expr *expr, ValueDecl *decl) { |
| 9585 | if (!isa<FunctionDecl>(decl)) return VisitExpr(expr); |
| 9586 | |
| 9587 | expr->setType(decl->getType()); |
| 9588 | |
| 9589 | assert(expr->getValueKind() == VK_RValue); |
| 9590 | if (S.getLangOptions().CPlusPlus && |
| 9591 | !(isa<CXXMethodDecl>(decl) && |
| 9592 | cast<CXXMethodDecl>(decl)->isInstance())) |
| 9593 | expr->setValueKind(VK_LValue); |
| 9594 | |
| 9595 | return expr; |
| 9596 | } |
| 9597 | |
| 9598 | ExprResult VisitMemberExpr(MemberExpr *mem) { |
| 9599 | return resolveDecl(mem, mem->getMemberDecl()); |
| 9600 | } |
| 9601 | |
| 9602 | ExprResult VisitDeclRefExpr(DeclRefExpr *ref) { |
| 9603 | return resolveDecl(ref, ref->getDecl()); |
| 9604 | } |
| 9605 | }; |
| 9606 | } |
| 9607 | |
| 9608 | /// Given a function expression of unknown-any type, try to rebuild it |
| 9609 | /// to have a function type. |
| 9610 | static ExprResult rebuildUnknownAnyFunction(Sema &S, Expr *fn) { |
| 9611 | ExprResult result = RebuildUnknownAnyFunction(S).Visit(fn); |
| 9612 | if (result.isInvalid()) return ExprError(); |
| 9613 | return S.DefaultFunctionArrayConversion(result.take()); |
| 9614 | } |
| 9615 | |
| 9616 | namespace { |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9617 | /// A visitor for rebuilding an expression of type __unknown_anytype |
| 9618 | /// into one which resolves the type directly on the referring |
| 9619 | /// expression. Strict preservation of the original source |
| 9620 | /// structure is not a goal. |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9621 | struct RebuildUnknownAnyExpr |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9622 | : StmtVisitor<RebuildUnknownAnyExpr, ExprResult> { |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9623 | |
| 9624 | Sema &S; |
| 9625 | |
| 9626 | /// The current destination type. |
| 9627 | QualType DestType; |
| 9628 | |
| 9629 | RebuildUnknownAnyExpr(Sema &S, QualType castType) |
| 9630 | : S(S), DestType(castType) {} |
| 9631 | |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9632 | ExprResult VisitStmt(Stmt *S) { |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9633 | llvm_unreachable("unexpected statement!"); |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9634 | return ExprError(); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9635 | } |
| 9636 | |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9637 | ExprResult VisitExpr(Expr *expr) { |
| 9638 | S.Diag(expr->getExprLoc(), diag::err_unsupported_unknown_any_expr) |
| 9639 | << expr->getSourceRange(); |
| 9640 | return ExprError(); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9641 | } |
| 9642 | |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9643 | ExprResult VisitCallExpr(CallExpr *call); |
| 9644 | ExprResult VisitObjCMessageExpr(ObjCMessageExpr *message); |
| 9645 | |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9646 | /// Rebuild an expression which simply semantically wraps another |
| 9647 | /// expression which it shares the type and value kind of. |
| 9648 | template <class T> ExprResult rebuildSugarExpr(T *expr) { |
| 9649 | ExprResult subResult = Visit(expr->getSubExpr()); |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9650 | if (subResult.isInvalid()) return ExprError(); |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9651 | Expr *subExpr = subResult.take(); |
| 9652 | expr->setSubExpr(subExpr); |
| 9653 | expr->setType(subExpr->getType()); |
| 9654 | expr->setValueKind(subExpr->getValueKind()); |
| 9655 | assert(expr->getObjectKind() == OK_Ordinary); |
| 9656 | return expr; |
| 9657 | } |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9658 | |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9659 | ExprResult VisitParenExpr(ParenExpr *paren) { |
| 9660 | return rebuildSugarExpr(paren); |
| 9661 | } |
| 9662 | |
| 9663 | ExprResult VisitUnaryExtension(UnaryOperator *op) { |
| 9664 | return rebuildSugarExpr(op); |
| 9665 | } |
| 9666 | |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9667 | ExprResult VisitUnaryAddrOf(UnaryOperator *op) { |
| 9668 | const PointerType *ptr = DestType->getAs<PointerType>(); |
| 9669 | if (!ptr) { |
| 9670 | S.Diag(op->getOperatorLoc(), diag::err_unknown_any_addrof) |
| 9671 | << op->getSourceRange(); |
| 9672 | return ExprError(); |
| 9673 | } |
| 9674 | assert(op->getValueKind() == VK_RValue); |
| 9675 | assert(op->getObjectKind() == OK_Ordinary); |
| 9676 | op->setType(DestType); |
| 9677 | |
| 9678 | // Build the sub-expression as if it were an object of the pointee type. |
| 9679 | DestType = ptr->getPointeeType(); |
| 9680 | ExprResult subResult = Visit(op->getSubExpr()); |
| 9681 | if (subResult.isInvalid()) return ExprError(); |
| 9682 | op->setSubExpr(subResult.take()); |
| 9683 | return op; |
| 9684 | } |
| 9685 | |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9686 | ExprResult VisitImplicitCastExpr(ImplicitCastExpr *ice); |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9687 | |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9688 | ExprResult resolveDecl(Expr *expr, ValueDecl *decl); |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9689 | |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9690 | ExprResult VisitMemberExpr(MemberExpr *mem) { |
| 9691 | return resolveDecl(mem, mem->getMemberDecl()); |
| 9692 | } |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9693 | |
| 9694 | ExprResult VisitDeclRefExpr(DeclRefExpr *ref) { |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9695 | return resolveDecl(ref, ref->getDecl()); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9696 | } |
| 9697 | }; |
| 9698 | } |
| 9699 | |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9700 | /// Rebuilds a call expression which yielded __unknown_anytype. |
| 9701 | ExprResult RebuildUnknownAnyExpr::VisitCallExpr(CallExpr *call) { |
| 9702 | Expr *callee = call->getCallee(); |
| 9703 | |
| 9704 | enum FnKind { |
John McCall | 4adb38c | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 9705 | FK_MemberFunction, |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9706 | FK_FunctionPointer, |
| 9707 | FK_BlockPointer |
| 9708 | }; |
| 9709 | |
| 9710 | FnKind kind; |
| 9711 | QualType type = callee->getType(); |
John McCall | 4adb38c | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 9712 | if (type == S.Context.BoundMemberTy) { |
| 9713 | assert(isa<CXXMemberCallExpr>(call) || isa<CXXOperatorCallExpr>(call)); |
| 9714 | kind = FK_MemberFunction; |
| 9715 | type = Expr::findBoundMemberType(callee); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9716 | } else if (const PointerType *ptr = type->getAs<PointerType>()) { |
| 9717 | type = ptr->getPointeeType(); |
| 9718 | kind = FK_FunctionPointer; |
| 9719 | } else { |
| 9720 | type = type->castAs<BlockPointerType>()->getPointeeType(); |
| 9721 | kind = FK_BlockPointer; |
| 9722 | } |
| 9723 | const FunctionType *fnType = type->castAs<FunctionType>(); |
| 9724 | |
| 9725 | // Verify that this is a legal result type of a function. |
| 9726 | if (DestType->isArrayType() || DestType->isFunctionType()) { |
| 9727 | unsigned diagID = diag::err_func_returning_array_function; |
| 9728 | if (kind == FK_BlockPointer) |
| 9729 | diagID = diag::err_block_returning_array_function; |
| 9730 | |
| 9731 | S.Diag(call->getExprLoc(), diagID) |
| 9732 | << DestType->isFunctionType() << DestType; |
| 9733 | return ExprError(); |
| 9734 | } |
| 9735 | |
| 9736 | // Otherwise, go ahead and set DestType as the call's result. |
| 9737 | call->setType(DestType.getNonLValueExprType(S.Context)); |
| 9738 | call->setValueKind(Expr::getValueKindForType(DestType)); |
| 9739 | assert(call->getObjectKind() == OK_Ordinary); |
| 9740 | |
| 9741 | // Rebuild the function type, replacing the result type with DestType. |
| 9742 | if (const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(fnType)) |
| 9743 | DestType = S.Context.getFunctionType(DestType, |
| 9744 | proto->arg_type_begin(), |
| 9745 | proto->getNumArgs(), |
| 9746 | proto->getExtProtoInfo()); |
| 9747 | else |
| 9748 | DestType = S.Context.getFunctionNoProtoType(DestType, |
| 9749 | fnType->getExtInfo()); |
| 9750 | |
| 9751 | // Rebuild the appropriate pointer-to-function type. |
| 9752 | switch (kind) { |
John McCall | 4adb38c | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 9753 | case FK_MemberFunction: |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9754 | // Nothing to do. |
| 9755 | break; |
| 9756 | |
| 9757 | case FK_FunctionPointer: |
| 9758 | DestType = S.Context.getPointerType(DestType); |
| 9759 | break; |
| 9760 | |
| 9761 | case FK_BlockPointer: |
| 9762 | DestType = S.Context.getBlockPointerType(DestType); |
| 9763 | break; |
| 9764 | } |
| 9765 | |
| 9766 | // Finally, we can recurse. |
| 9767 | ExprResult calleeResult = Visit(callee); |
| 9768 | if (!calleeResult.isUsable()) return ExprError(); |
| 9769 | call->setCallee(calleeResult.take()); |
| 9770 | |
| 9771 | // Bind a temporary if necessary. |
| 9772 | return S.MaybeBindToTemporary(call); |
| 9773 | } |
| 9774 | |
| 9775 | ExprResult RebuildUnknownAnyExpr::VisitObjCMessageExpr(ObjCMessageExpr *msg) { |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9776 | // Verify that this is a legal result type of a call. |
| 9777 | if (DestType->isArrayType() || DestType->isFunctionType()) { |
| 9778 | S.Diag(msg->getExprLoc(), diag::err_func_returning_array_function) |
| 9779 | << DestType->isFunctionType() << DestType; |
| 9780 | return ExprError(); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9781 | } |
| 9782 | |
John McCall | 3f4138c | 2011-07-13 17:56:40 +0000 | [diff] [blame] | 9783 | // Rewrite the method result type if available. |
| 9784 | if (ObjCMethodDecl *method = msg->getMethodDecl()) { |
| 9785 | assert(method->getResultType() == S.Context.UnknownAnyTy); |
| 9786 | method->setResultType(DestType); |
| 9787 | } |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9788 | |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9789 | // Change the type of the message. |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9790 | msg->setType(DestType.getNonReferenceType()); |
| 9791 | msg->setValueKind(Expr::getValueKindForType(DestType)); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9792 | |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9793 | return S.MaybeBindToTemporary(msg); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9794 | } |
| 9795 | |
| 9796 | ExprResult RebuildUnknownAnyExpr::VisitImplicitCastExpr(ImplicitCastExpr *ice) { |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9797 | // 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] | 9798 | assert(ice->getCastKind() == CK_FunctionToPointerDecay); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9799 | assert(ice->getValueKind() == VK_RValue); |
| 9800 | assert(ice->getObjectKind() == OK_Ordinary); |
| 9801 | |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9802 | ice->setType(DestType); |
| 9803 | |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9804 | // Rebuild the sub-expression as the pointee (function) type. |
| 9805 | DestType = DestType->castAs<PointerType>()->getPointeeType(); |
| 9806 | |
| 9807 | ExprResult result = Visit(ice->getSubExpr()); |
| 9808 | if (!result.isUsable()) return ExprError(); |
| 9809 | |
| 9810 | ice->setSubExpr(result.take()); |
| 9811 | return S.Owned(ice); |
| 9812 | } |
| 9813 | |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9814 | ExprResult RebuildUnknownAnyExpr::resolveDecl(Expr *expr, ValueDecl *decl) { |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9815 | ExprValueKind valueKind = VK_LValue; |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9816 | QualType type = DestType; |
| 9817 | |
| 9818 | // We know how to make this work for certain kinds of decls: |
| 9819 | |
| 9820 | // - functions |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9821 | if (FunctionDecl *fn = dyn_cast<FunctionDecl>(decl)) { |
John McCall | 9a877fe | 2011-08-10 04:12:23 +0000 | [diff] [blame^] | 9822 | if (const PointerType *ptr = type->getAs<PointerType>()) { |
| 9823 | DestType = ptr->getPointeeType(); |
| 9824 | ExprResult result = resolveDecl(expr, decl); |
| 9825 | if (result.isInvalid()) return ExprError(); |
| 9826 | return S.ImpCastExprToType(result.take(), type, |
| 9827 | CK_FunctionToPointerDecay, VK_RValue); |
| 9828 | } |
| 9829 | |
| 9830 | if (!type->isFunctionType()) { |
| 9831 | S.Diag(expr->getExprLoc(), diag::err_unknown_any_function) |
| 9832 | << decl << expr->getSourceRange(); |
| 9833 | return ExprError(); |
| 9834 | } |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9835 | |
John McCall | 4adb38c | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 9836 | if (CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(fn)) |
| 9837 | if (method->isInstance()) { |
| 9838 | valueKind = VK_RValue; |
| 9839 | type = S.Context.BoundMemberTy; |
| 9840 | } |
| 9841 | |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9842 | // Function references aren't l-values in C. |
| 9843 | if (!S.getLangOptions().CPlusPlus) |
| 9844 | valueKind = VK_RValue; |
| 9845 | |
| 9846 | // - variables |
| 9847 | } else if (isa<VarDecl>(decl)) { |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9848 | if (const ReferenceType *refTy = type->getAs<ReferenceType>()) { |
| 9849 | type = refTy->getPointeeType(); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9850 | } else if (type->isFunctionType()) { |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9851 | S.Diag(expr->getExprLoc(), diag::err_unknown_any_var_function_type) |
| 9852 | << decl << expr->getSourceRange(); |
| 9853 | return ExprError(); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9854 | } |
| 9855 | |
| 9856 | // - nothing else |
| 9857 | } else { |
| 9858 | S.Diag(expr->getExprLoc(), diag::err_unsupported_unknown_any_decl) |
| 9859 | << decl << expr->getSourceRange(); |
| 9860 | return ExprError(); |
| 9861 | } |
| 9862 | |
John McCall | 2979fe0 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9863 | decl->setType(DestType); |
| 9864 | expr->setType(type); |
| 9865 | expr->setValueKind(valueKind); |
| 9866 | return S.Owned(expr); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9867 | } |
| 9868 | |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9869 | /// Check a cast of an unknown-any type. We intentionally only |
| 9870 | /// trigger this for C-style casts. |
John Wiegley | 0129629 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9871 | ExprResult Sema::checkUnknownAnyCast(SourceRange typeRange, QualType castType, |
| 9872 | Expr *castExpr, CastKind &castKind, |
| 9873 | ExprValueKind &VK, CXXCastPath &path) { |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9874 | // Rewrite the casted expression from scratch. |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9875 | ExprResult result = RebuildUnknownAnyExpr(*this, castType).Visit(castExpr); |
| 9876 | if (!result.isUsable()) return ExprError(); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9877 | |
John McCall | 3943973 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9878 | castExpr = result.take(); |
| 9879 | VK = castExpr->getValueKind(); |
| 9880 | castKind = CK_NoOp; |
| 9881 | |
| 9882 | return castExpr; |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9883 | } |
| 9884 | |
| 9885 | static ExprResult diagnoseUnknownAnyExpr(Sema &S, Expr *e) { |
| 9886 | Expr *orig = e; |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9887 | unsigned diagID = diag::err_uncasted_use_of_unknown_any; |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9888 | while (true) { |
| 9889 | e = e->IgnoreParenImpCasts(); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9890 | if (CallExpr *call = dyn_cast<CallExpr>(e)) { |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9891 | e = call->getCallee(); |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9892 | diagID = diag::err_uncasted_call_of_unknown_any; |
| 9893 | } else { |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9894 | break; |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9895 | } |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9896 | } |
| 9897 | |
John McCall | 2d2e870 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9898 | SourceLocation loc; |
| 9899 | NamedDecl *d; |
| 9900 | if (DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e)) { |
| 9901 | loc = ref->getLocation(); |
| 9902 | d = ref->getDecl(); |
| 9903 | } else if (MemberExpr *mem = dyn_cast<MemberExpr>(e)) { |
| 9904 | loc = mem->getMemberLoc(); |
| 9905 | d = mem->getMemberDecl(); |
| 9906 | } else if (ObjCMessageExpr *msg = dyn_cast<ObjCMessageExpr>(e)) { |
| 9907 | diagID = diag::err_uncasted_call_of_unknown_any; |
| 9908 | loc = msg->getSelectorLoc(); |
| 9909 | d = msg->getMethodDecl(); |
| 9910 | assert(d && "unknown method returning __unknown_any?"); |
| 9911 | } else { |
| 9912 | S.Diag(e->getExprLoc(), diag::err_unsupported_unknown_any_expr) |
| 9913 | << e->getSourceRange(); |
| 9914 | return ExprError(); |
| 9915 | } |
| 9916 | |
| 9917 | S.Diag(loc, diagID) << d << orig->getSourceRange(); |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9918 | |
| 9919 | // Never recoverable. |
| 9920 | return ExprError(); |
| 9921 | } |
| 9922 | |
John McCall | 36e7fe3 | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 9923 | /// Check for operands with placeholder types and complain if found. |
| 9924 | /// Returns true if there was an error and no recovery was possible. |
John McCall | 3aef3d8 | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 9925 | ExprResult Sema::CheckPlaceholderExpr(Expr *E) { |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9926 | // Placeholder types are always *exactly* the appropriate builtin type. |
| 9927 | QualType type = E->getType(); |
John McCall | 36e7fe3 | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 9928 | |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9929 | // Overloaded expressions. |
| 9930 | if (type == Context.OverloadTy) |
| 9931 | return ResolveAndFixSingleFunctionTemplateSpecialization(E, false, true, |
Douglas Gregor | 89f3cd5 | 2011-03-16 19:16:25 +0000 | [diff] [blame] | 9932 | E->getSourceRange(), |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9933 | QualType(), |
| 9934 | diag::err_ovl_unresolvable); |
| 9935 | |
John McCall | 0009fcc | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9936 | // Bound member functions. |
| 9937 | if (type == Context.BoundMemberTy) { |
| 9938 | Diag(E->getLocStart(), diag::err_invalid_use_of_bound_member_func) |
| 9939 | << E->getSourceRange(); |
| 9940 | return ExprError(); |
| 9941 | } |
| 9942 | |
John McCall | 3199634 | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9943 | // Expressions of unknown type. |
| 9944 | if (type == Context.UnknownAnyTy) |
| 9945 | return diagnoseUnknownAnyExpr(*this, E); |
| 9946 | |
| 9947 | assert(!type->isPlaceholderType()); |
| 9948 | return Owned(E); |
John McCall | 36e7fe3 | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 9949 | } |
Richard Trieu | 2c850c0 | 2011-04-21 21:44:26 +0000 | [diff] [blame] | 9950 | |
| 9951 | bool Sema::CheckCaseExpression(Expr *expr) { |
| 9952 | if (expr->isTypeDependent()) |
| 9953 | return true; |
| 9954 | if (expr->isValueDependent() || expr->isIntegerConstantExpr(Context)) |
| 9955 | return expr->getType()->isIntegralOrEnumerationType(); |
| 9956 | return false; |
| 9957 | } |