Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- SemaExpr.cpp - Semantic Analysis for Expressions -----------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements semantic analysis for expressions. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
John McCall | 2d88708 | 2010-08-25 22:03:47 +0000 | [diff] [blame] | 14 | #include "clang/Sema/SemaInternal.h" |
Douglas Gregor | e737f50 | 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" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 18 | #include "clang/AST/ASTContext.h" |
Sebastian Redl | f79a719 | 2011-04-29 08:19:30 +0000 | [diff] [blame] | 19 | #include "clang/AST/ASTMutationListener.h" |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 20 | #include "clang/AST/CXXInheritance.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 21 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 22 | #include "clang/AST/DeclTemplate.h" |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 23 | #include "clang/AST/EvaluatedExprVisitor.h" |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 24 | #include "clang/AST/Expr.h" |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 25 | #include "clang/AST/ExprCXX.h" |
Steve Naroff | f494b57 | 2008-05-29 21:12:08 +0000 | [diff] [blame] | 26 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 27 | #include "clang/AST/RecursiveASTVisitor.h" |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 28 | #include "clang/AST/TypeLoc.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 29 | #include "clang/Basic/PartialDiagnostic.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 30 | #include "clang/Basic/SourceManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 31 | #include "clang/Basic/TargetInfo.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 32 | #include "clang/Lex/LiteralSupport.h" |
| 33 | #include "clang/Lex/Preprocessor.h" |
John McCall | 1951085 | 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 | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 37 | #include "clang/Sema/ScopeInfo.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 38 | #include "clang/Sema/ParsedTemplate.h" |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 39 | #include "clang/Sema/SemaFixItUtils.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 40 | #include "clang/Sema/Template.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 41 | using namespace clang; |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 42 | using namespace sema; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 43 | |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 44 | |
Douglas Gregor | 48f3bb9 | 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 | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 54 | /// If IgnoreDeprecated is set to true, this should not warn about deprecated |
Chris Lattner | 5233826 | 2009-10-25 22:31:57 +0000 | [diff] [blame] | 55 | /// decls. |
| 56 | /// |
Douglas Gregor | 48f3bb9 | 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 | 5233826 | 2009-10-25 22:31:57 +0000 | [diff] [blame] | 59 | /// |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 60 | bool Sema::DiagnoseUseOfDecl(NamedDecl *D, SourceLocation Loc, |
Fariborz Jahanian | 89ebaed | 2011-04-23 17:27:19 +0000 | [diff] [blame] | 61 | const ObjCInterfaceDecl *UnknownObjCClass) { |
Douglas Gregor | 9b62363 | 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 | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 65 | llvm::DenseMap<Decl *, SmallVector<PartialDiagnosticAt, 1> >::iterator |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 66 | Pos = SuppressedDiagnostics.find(D->getCanonicalDecl()); |
| 67 | if (Pos != SuppressedDiagnostics.end()) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 68 | SmallVectorImpl<PartialDiagnosticAt> &Suppressed = Pos->second; |
Douglas Gregor | 9b62363 | 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 | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 73 | // them again for this specialization. However, we don't obsolete this |
Douglas Gregor | 9b62363 | 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 | 34b41d9 | 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 | 483b9f3 | 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 | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 85 | } |
| 86 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 87 | // See if this is a deleted function. |
Douglas Gregor | 25d944a | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 88 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 89 | if (FD->isDeleted()) { |
| 90 | Diag(Loc, diag::err_deleted_function_use); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 91 | Diag(D->getLocation(), diag::note_unavailable_here) << 1 << true; |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 92 | return true; |
| 93 | } |
Douglas Gregor | 25d944a | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 94 | } |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 95 | |
Douglas Gregor | 0a0d2b1 | 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 | 12189f5 | 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 | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 121 | } |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 122 | break; |
| 123 | } |
| 124 | |
Anders Carlsson | 2127ecc | 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 | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 129 | return false; |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 130 | } |
| 131 | |
Douglas Gregor | 0a0d2b1 | 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 | 5b53005 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 149 | /// DiagnoseSentinelCalls - This routine checks on method dispatch calls |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 150 | /// (and other functions in future), which have been declared with sentinel |
Fariborz Jahanian | 5b53005 | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 154 | Expr **Args, unsigned NumArgs) { |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 155 | const SentinelAttr *attr = D->getAttr<SentinelAttr>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 156 | if (!attr) |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 157 | return; |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 158 | |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 159 | int sentinelPos = attr->getSentinel(); |
| 160 | int nullPos = attr->getNullPos(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 161 | |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 162 | unsigned int i = 0; |
Fariborz Jahanian | 236673e | 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 | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 176 | } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Fariborz Jahanian | 236673e | 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 | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 186 | } else if (VarDecl *V = dyn_cast<VarDecl>(D)) { |
Fariborz Jahanian | daf0415 | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 190 | const FunctionType *FT = Ty->isFunctionPointerType() |
John McCall | 183700f | 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 | daf0415 | 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 | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 206 | } else |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 207 | return; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 208 | } else |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 209 | return; |
| 210 | |
| 211 | if (warnNotEnoughArgs) { |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 212 | Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName(); |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 213 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 88f1ba0 | 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 | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 223 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 88f1ba0 | 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 | 8eb662e | 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 | 343e6ff | 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 | 9ccd725 | 2010-07-14 16:37:51 +0000 | [diff] [blame] | 238 | if (sentinelExpr->getType()->isAnyPointerType() && |
John McCall | 8eb662e | 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 | f78c4e5 | 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 | 8eb662e | 2010-05-06 23:53:00 +0000 | [diff] [blame] | 262 | Diag(D->getLocation(), diag::note_sentinel_here) << isMethod; |
Fariborz Jahanian | 5b53005 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Douglas Gregor | 4b2d3f7 | 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 | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 270 | //===----------------------------------------------------------------------===// |
| 271 | // Standard Promotions and Conversions |
| 272 | //===----------------------------------------------------------------------===// |
| 273 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 274 | /// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 275 | ExprResult Sema::DefaultFunctionArrayConversion(Expr *E) { |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 276 | QualType Ty = E->getType(); |
| 277 | assert(!Ty.isNull() && "DefaultFunctionArrayConversion - missing type"); |
| 278 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 279 | if (Ty->isFunctionType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 280 | E = ImpCastExprToType(E, Context.getPointerType(Ty), |
| 281 | CK_FunctionToPointerDecay).take(); |
Chris Lattner | 67d33d8 | 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 | c39a3d7 | 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 | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 294 | if (getLangOptions().C99 || getLangOptions().CPlusPlus || E->isLValue()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 295 | E = ImpCastExprToType(E, Context.getArrayDecayedType(Ty), |
| 296 | CK_ArrayToPointerDecay).take(); |
Chris Lattner | 67d33d8 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 297 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 298 | return Owned(E); |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Argyrios Kyrtzidis | 8a285ae | 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 | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 320 | ExprResult Sema::DefaultLvalueConversion(Expr *E) { |
John McCall | 0ae287a | 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 | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 324 | if (!E->isGLValue()) return Owned(E); |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame^] | 325 | |
John McCall | 409fa9a | 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 | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 328 | |
John McCall | 409fa9a | 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 | 429bb27 | 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 | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 335 | if (!E->isGLValue()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 336 | return Owned(E); |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 337 | } |
John McCall | 409fa9a | 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 | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 345 | return Owned(E); |
John McCall | 409fa9a | 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 | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 353 | return Owned(E); |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 354 | |
Argyrios Kyrtzidis | 8a285ae | 2011-04-26 17:41:22 +0000 | [diff] [blame] | 355 | CheckForNullPointerDereference(*this, E); |
| 356 | |
John McCall | 409fa9a | 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 | a0125d8 | 2011-02-16 01:57:07 +0000 | [diff] [blame] | 368 | |
John Wiegley | 429bb27 | 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 | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 371 | } |
| 372 | |
John Wiegley | 429bb27 | 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 | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 381 | } |
| 382 | |
| 383 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 384 | /// UsualUnaryConversions - Performs various conversions that are common to most |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 385 | /// operators (C99 6.3). The conversions of array and function types are |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 386 | /// sometimes suppressed. For example, the array->pointer conversion doesn't |
Chris Lattner | e7a2e91 | 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 | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 389 | ExprResult Sema::UsualUnaryConversions(Expr *E) { |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 390 | // First, convert to an r-value. |
John Wiegley | 429bb27 | 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 | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 395 | |
| 396 | QualType Ty = E->getType(); |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 397 | assert(!Ty.isNull() && "UsualUnaryConversions - missing type"); |
John McCall | 0ae287a | 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 | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 418 | E = ImpCastExprToType(E, PTy, CK_IntegralCast).take(); |
| 419 | return Owned(E); |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 420 | } |
| 421 | if (Ty->isPromotableIntegerType()) { |
| 422 | QualType PT = Context.getPromotedIntegerType(Ty); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 423 | E = ImpCastExprToType(E, PT, CK_IntegralCast).take(); |
| 424 | return Owned(E); |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 425 | } |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 426 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 427 | return Owned(E); |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 428 | } |
| 429 | |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 430 | /// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that |
Mike Stump | 1eb4433 | 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 | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 432 | /// double. All other argument types are converted by UsualUnaryConversions(). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 433 | ExprResult Sema::DefaultArgumentPromotion(Expr *E) { |
| 434 | QualType Ty = E->getType(); |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 435 | assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 436 | |
John Wiegley | 429bb27 | 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 | 40c2913 | 2010-12-06 18:36:11 +0000 | [diff] [blame] | 441 | |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 442 | // If this is a 'float' (CVR qualified or typedef) promote to double. |
Chris Lattner | 4037833 | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 443 | if (Ty->isSpecificBuiltinType(BuiltinType::Float)) |
John Wiegley | 429bb27 | 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 | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 447 | } |
| 448 | |
Chris Lattner | 312531a | 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 | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 451 | /// interfaces passed by value. |
| 452 | ExprResult Sema::DefaultVariadicArgumentPromotion(Expr *E, VariadicCallType CT, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 453 | FunctionDecl *FDecl) { |
Douglas Gregor | 8d5e18c | 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 | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 459 | if (ExprRes.isInvalid()) |
| 460 | return ExprError(); |
| 461 | E = ExprRes.take(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 462 | |
Chris Lattner | 4037833 | 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 | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 467 | return Owned(E); |
Chris Lattner | 4037833 | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 468 | |
Douglas Gregor | 930a9ab | 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 | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 470 | if (E->getType()->isObjCObjectType() && |
Douglas Gregor | 930a9ab | 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 | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 474 | return ExprError(); |
Douglas Gregor | 930a9ab | 2011-05-21 19:26:31 +0000 | [diff] [blame] | 475 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 476 | if (!E->getType().isPODType(Context)) { |
Douglas Gregor | 0fd228d | 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 | f85e193 | 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 | 0fd228d | 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 | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 500 | PDiag(diag::warn_cannot_pass_non_pod_arg_to_vararg) |
Douglas Gregor | 0fd228d | 2011-05-21 16:27:21 +0000 | [diff] [blame] | 501 | << getLangOptions().CPlusPlus0x << E->getType() |
Douglas Gregor | 930a9ab | 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 | 0fd228d | 2011-05-21 16:27:21 +0000 | [diff] [blame] | 524 | } |
| 525 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 526 | return Owned(E); |
Anders Carlsson | dce5e2c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 527 | } |
| 528 | |
Chris Lattner | e7a2e91 | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 531 | /// routine returns the first non-arithmetic type found. The client is |
Chris Lattner | e7a2e91 | 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 | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 535 | QualType Sema::UsualArithmeticConversions(ExprResult &lhsExpr, |
| 536 | ExprResult &rhsExpr, |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 537 | bool isCompAssign) { |
John Wiegley | 429bb27 | 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 | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 543 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 544 | rhsExpr = UsualUnaryConversions(rhsExpr.take()); |
| 545 | if (rhsExpr.isInvalid()) |
| 546 | return QualType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 547 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 548 | // For conversion purposes, we ignore any qualifiers. |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 549 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 550 | QualType lhs = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 551 | Context.getCanonicalType(lhsExpr.get()->getType()).getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 552 | QualType rhs = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 553 | Context.getCanonicalType(rhsExpr.get()->getType()).getUnqualifiedType(); |
Douglas Gregor | eb8f306 | 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 | cf33b24 | 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 | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 568 | QualType LHSBitfieldPromoteTy = Context.isPromotableBitField(lhsExpr.get()); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 569 | if (!LHSBitfieldPromoteTy.isNull()) |
| 570 | lhs = LHSBitfieldPromoteTy; |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 571 | if (lhs != lhs_unpromoted && !isCompAssign) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 572 | lhsExpr = ImpCastExprToType(lhsExpr.take(), lhs, CK_IntegralCast); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 573 | |
John McCall | cf33b24 | 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 | 2bb5d00 | 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 | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 589 | rhsExpr = ImpCastExprToType(rhsExpr.take(), fp, CK_IntegralToFloating); |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 590 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, |
| 591 | CK_FloatingRealToComplex); |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 592 | } else { |
| 593 | assert(rhs->isComplexIntegerType()); |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 594 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, |
| 595 | CK_IntegralComplexToFloatingComplex); |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 596 | } |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 597 | return lhs; |
| 598 | } |
| 599 | |
John McCall | 2bb5d00 | 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 | 67e2933 | 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 | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 609 | } else { |
| 610 | assert(lhs->isComplexIntegerType()); |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 611 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, |
| 612 | CK_IntegralComplexToFloatingComplex); |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 613 | } |
| 614 | } |
John McCall | cf33b24 | 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 | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 634 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, |
| 635 | CK_FloatingComplexCast); |
John McCall | cf33b24 | 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 | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 641 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, |
| 642 | CK_FloatingComplexCast); |
John McCall | cf33b24 | 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 | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 653 | QualType fp = cast<ComplexType>(lhs)->getElementType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 654 | rhsExpr = ImpCastExprToType(rhsExpr.take(), fp, CK_FloatingCast); |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 655 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, |
| 656 | CK_FloatingRealToComplex); |
John McCall | cf33b24 | 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 | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 664 | rhsExpr = ImpCastExprToType(rhsExpr.take(), result, |
| 665 | CK_FloatingRealToComplex); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 666 | |
| 667 | // _Complex float -> _Complex double |
| 668 | if (!isCompAssign && order < 0) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 669 | lhsExpr = ImpCastExprToType(lhsExpr.take(), result, |
| 670 | CK_FloatingComplexCast); |
John McCall | cf33b24 | 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 | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 681 | if (!isCompAssign) { |
Argyrios Kyrtzidis | e188933 | 2011-01-18 18:49:33 +0000 | [diff] [blame] | 682 | QualType fp = cast<ComplexType>(rhs)->getElementType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 683 | lhsExpr = ImpCastExprToType(lhsExpr.take(), fp, CK_FloatingCast); |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 684 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, |
| 685 | CK_FloatingRealToComplex); |
John McCall | 2bb5d00 | 2010-11-13 09:02:35 +0000 | [diff] [blame] | 686 | } |
John McCall | cf33b24 | 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 | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 695 | lhsExpr = ImpCastExprToType(lhsExpr.take(), result, |
| 696 | CK_FloatingRealToComplex); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 697 | |
| 698 | // _Complex float -> _Complex double |
| 699 | if (order > 0) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 700 | rhsExpr = ImpCastExprToType(rhsExpr.take(), result, |
| 701 | CK_FloatingComplexCast); |
John McCall | cf33b24 | 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 | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 715 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_FloatingCast); |
John McCall | cf33b24 | 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 | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 721 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_FloatingCast); |
John McCall | cf33b24 | 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 | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 729 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralToFloating); |
John McCall | cf33b24 | 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 | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 738 | rhsExpr = ImpCastExprToType(rhsExpr.take(), result, |
| 739 | CK_IntegralComplexToFloatingComplex); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 740 | |
| 741 | // float -> _Complex float |
| 742 | if (!isCompAssign) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 743 | lhsExpr = ImpCastExprToType(lhsExpr.take(), result, |
| 744 | CK_FloatingRealToComplex); |
John McCall | cf33b24 | 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 | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 753 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralToFloating); |
John McCall | cf33b24 | 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 | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 763 | lhsExpr = ImpCastExprToType(lhsExpr.take(), result, |
| 764 | CK_IntegralComplexToFloatingComplex); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 765 | |
| 766 | // float -> _Complex float |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 767 | rhsExpr = ImpCastExprToType(rhsExpr.take(), result, |
| 768 | CK_FloatingRealToComplex); |
John McCall | cf33b24 | 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 | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 784 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralComplexCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 785 | return lhs; |
| 786 | } |
| 787 | |
| 788 | if (!isCompAssign) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 789 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralComplexCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 790 | return rhs; |
| 791 | } else if (lhsComplexInt) { |
| 792 | // int -> _Complex int |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 793 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralRealToComplex); |
John McCall | cf33b24 | 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 | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 798 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, |
| 799 | CK_IntegralRealToComplex); |
John McCall | cf33b24 | 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 | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 811 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 812 | return lhs; |
| 813 | } else if (!isCompAssign) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 814 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralCast); |
John McCall | cf33b24 | 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 | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 820 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 821 | return lhs; |
| 822 | } else if (!isCompAssign) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 823 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralCast); |
John McCall | cf33b24 | 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 | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 830 | rhsExpr = ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 831 | return lhs; |
| 832 | } else if (!isCompAssign) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 833 | lhsExpr = ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralCast); |
John McCall | cf33b24 | 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 | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 842 | rhsExpr = ImpCastExprToType(rhsExpr.take(), result, CK_IntegralCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 843 | if (!isCompAssign) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 844 | lhsExpr = ImpCastExprToType(lhsExpr.take(), result, CK_IntegralCast); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 845 | return result; |
| 846 | } |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 847 | } |
| 848 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 849 | //===----------------------------------------------------------------------===// |
| 850 | // Semantic Analysis for various Expression Types |
| 851 | //===----------------------------------------------------------------------===// |
| 852 | |
| 853 | |
Peter Collingbourne | f111d93 | 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 | 5bf47f7 | 2011-04-15 11:21:57 +0000 | [diff] [blame] | 878 | delete [] Types; |
Peter Collingbourne | f111d93 | 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 | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 954 | SmallVector<unsigned, 1> CompatIndices; |
Peter Collingbourne | f111d93 | 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 | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 974 | for (SmallVector<unsigned, 1>::iterator I = CompatIndices.begin(), |
Peter Collingbourne | f111d93 | 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 | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 1011 | /// ActOnStringLiteral - The specified tokens were lexed as pasted string |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +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 | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1016 | /// |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1017 | ExprResult |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 1018 | Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1019 | assert(NumStringToks && "Must have at least one string!"); |
| 1020 | |
Chris Lattner | bbee00b | 2009-01-16 18:51:42 +0000 | [diff] [blame] | 1021 | StringLiteralParser Literal(StringToks, NumStringToks, PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1022 | if (Literal.hadError) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1023 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1024 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1025 | SmallVector<SourceLocation, 4> StringTokLocs; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1026 | for (unsigned i = 0; i != NumStringToks; ++i) |
| 1027 | StringTokLocs.push_back(StringToks[i].getLocation()); |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 1028 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 1029 | QualType StrTy = Context.CharTy; |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1030 | if (Literal.isWide()) |
Anders Carlsson | 96b4adc | 2011-04-06 18:42:48 +0000 | [diff] [blame] | 1031 | StrTy = Context.getWCharType(); |
Douglas Gregor | 5cee119 | 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 | 96b4adc | 2011-04-06 18:42:48 +0000 | [diff] [blame] | 1036 | else if (Literal.Pascal) |
| 1037 | StrTy = Context.UnsignedCharTy; |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 1038 | |
Douglas Gregor | 5cee119 | 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 | 77a5223 | 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 | 7dc480f | 2010-06-15 18:05:34 +0000 | [diff] [blame] | 1050 | if (getLangOptions().CPlusPlus || getLangOptions().ConstStrings) |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 1051 | StrTy.addConst(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1052 | |
Chris Lattner | a7ad98f | 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 | dbb1ecc | 2009-02-26 23:01:51 +0000 | [diff] [blame] | 1057 | llvm::APInt(32, Literal.GetNumStringChars()+1), |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 1058 | ArrayType::Normal, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1059 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1060 | // Pass &StringTokLocs[0], StringTokLocs.size() to factory! |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 1061 | return Owned(StringLiteral::Create(Context, Literal.GetString(), |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1062 | Kind, Literal.Pascal, StrTy, |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 1063 | &StringTokLocs[0], |
| 1064 | StringTokLocs.size())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1065 | } |
| 1066 | |
John McCall | 469a1eb | 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 | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1074 | /// A by-ref capture is required. |
| 1075 | CR_CaptureByRef, |
| 1076 | |
John McCall | 469a1eb | 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 | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1082 | /// |
John McCall | 469a1eb | 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 | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1086 | diagnoseUncapturableValueReference(Sema &S, SourceLocation loc, |
John McCall | 469a1eb | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1092 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1093 | case Sema::PotentiallyEvaluated: |
| 1094 | case Sema::PotentiallyEvaluatedIfUsed: |
| 1095 | break; |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1096 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1097 | case Sema::PotentiallyPotentiallyEvaluated: |
| 1098 | // FIXME: delay these! |
| 1099 | break; |
Chris Lattner | 17f3a6d | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 1100 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1101 | |
John McCall | 469a1eb | 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 | 4f38f41 | 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 | 469a1eb | 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 | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1133 | } |
| 1134 | |
John McCall | 6b5a61b | 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 | 469a1eb | 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 | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1159 | static CaptureResult shouldCaptureValueReference(Sema &S, SourceLocation loc, |
John McCall | 469a1eb | 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 | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1163 | if (!var) return CR_NoCapture; |
John McCall | 469a1eb | 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 | 469a1eb | 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 | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1180 | return diagnoseUncapturableValueReference(S, loc, var, DC); |
John McCall | 469a1eb | 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 | 6b5a61b | 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 | 469a1eb | 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 | 6b5a61b | 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 | 642a75f | 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 | 6b5a61b | 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 | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1298 | } |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1299 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1300 | ExprResult |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1301 | Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1302 | SourceLocation Loc, |
| 1303 | const CXXScopeSpec *SS) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1304 | DeclarationNameInfo NameInfo(D->getDeclName(), Loc); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1305 | return BuildDeclRefExpr(D, Ty, VK, NameInfo, SS); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1306 | } |
| 1307 | |
John McCall | 76a4021 | 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 | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1310 | ExprResult |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1311 | Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1312 | const DeclarationNameInfo &NameInfo, |
| 1313 | const CXXScopeSpec *SS) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1314 | MarkDeclarationReferenced(NameInfo.getLoc(), D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1315 | |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1316 | Expr *E = DeclRefExpr::Create(Context, |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 1317 | SS? SS->getWithLocInContext(Context) |
| 1318 | : NestedNameSpecifierLoc(), |
John McCall | 7eb0a9e | 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 | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 1326 | } |
| 1327 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1328 | /// Decomposes the given name into a DeclarationNameInfo, its location, and |
John McCall | 129e2df | 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 | 67e2933 | 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 | 129e2df | 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 | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1346 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1347 | Id.TemplateId->getTemplateArgs(), |
| 1348 | Id.TemplateId->NumArgs); |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1349 | translateTemplateArguments(TemplateArgsPtr, Buffer); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1350 | TemplateArgsPtr.release(); |
| 1351 | |
John McCall | 2b5289b | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 1352 | TemplateName TName = Id.TemplateId->Template.get(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1353 | SourceLocation TNameLoc = Id.TemplateId->TemplateNameLoc; |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1354 | NameInfo = Context.getNameForTemplate(TName, TNameLoc); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1355 | TemplateArgs = &Buffer; |
| 1356 | } else { |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1357 | NameInfo = GetNameFromUnqualifiedId(Id); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1358 | TemplateArgs = 0; |
| 1359 | } |
| 1360 | } |
| 1361 | |
John McCall | 578b69b | 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 | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1365 | bool Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec &SS, LookupResult &R, |
Kaelyn Uhrain | ace5e76 | 2011-08-05 00:09:52 +0000 | [diff] [blame] | 1366 | CorrectTypoContext CTC, |
| 1367 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
| 1368 | Expr **Args, unsigned NumArgs) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1369 | DeclarationName Name = R.getLookupName(); |
| 1370 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1371 | unsigned diagnostic = diag::err_undeclared_var_use; |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1372 | unsigned diagnostic_suggest = diag::err_undeclared_var_use_suggest; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1373 | if (Name.getNameKind() == DeclarationName::CXXOperatorName || |
| 1374 | Name.getNameKind() == DeclarationName::CXXLiteralOperatorName || |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1375 | Name.getNameKind() == DeclarationName::CXXConversionFunctionName) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1376 | diagnostic = diag::err_undeclared_use; |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1377 | diagnostic_suggest = diag::err_undeclared_use_suggest; |
| 1378 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1379 | |
Douglas Gregor | bb092ba | 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 | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1384 | for (DeclContext *DC = SS.isEmpty() ? CurContext : 0; |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1385 | DC; DC = DC->getParent()) { |
John McCall | 578b69b | 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 | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1401 | if (isInstance) { |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1402 | UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>( |
| 1403 | CallsUndergoingInstantiation.back()->getCallee()); |
Nick Lewycky | d9ca4ab | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1404 | CXXMethodDecl *DepMethod = cast_or_null<CXXMethodDecl>( |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1405 | CurMethod->getInstantiatedFromMemberFunction()); |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1406 | if (DepMethod) { |
Nick Lewycky | d9ca4ab | 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 | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 1415 | |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 1416 | CXXScopeSpec SS; |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 1417 | SS.Adopt(ULE->getQualifierLoc()); |
Nick Lewycky | d9ca4ab | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1418 | CXXDependentScopeMemberExpr *DepExpr = |
| 1419 | CXXDependentScopeMemberExpr::Create( |
| 1420 | Context, DepThis, DepThisType, true, SourceLocation(), |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 1421 | SS.getWithLocInContext(Context), NULL, |
Nick Lewycky | d9ca4ab | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1422 | R.getLookupNameInfo(), &TList); |
| 1423 | CallsUndergoingInstantiation.back()->setCallee(DepExpr); |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1424 | } else { |
Nick Lewycky | d9ca4ab | 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 | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1428 | } |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1429 | } else { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1430 | Diag(R.getNameLoc(), diagnostic) << Name; |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1431 | } |
John McCall | 578b69b | 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 | e26f043 | 2010-08-09 22:38:14 +0000 | [diff] [blame] | 1440 | |
| 1441 | R.clear(); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1442 | } |
| 1443 | } |
| 1444 | |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1445 | // We didn't find anything, so try to correct for a typo. |
Douglas Gregor | d8bba9c | 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 | 701d1e7 | 2011-07-12 08:45:31 +0000 | [diff] [blame] | 1453 | if (NamedDecl *ND = Corrected.getCorrectionDecl()) { |
Kaelyn Uhrain | f0c1d8f | 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) { |
| 1460 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*CD)) |
Kaelyn Uhrain | ace5e76 | 2011-08-05 00:09:52 +0000 | [diff] [blame] | 1461 | AddOverloadCandidate(FD, DeclAccessPair::make(FD, AS_none), |
Kaelyn Uhrain | f0c1d8f | 2011-08-03 20:36:05 +0000 | [diff] [blame] | 1462 | Args, NumArgs, OCS); |
Kaelyn Uhrain | ace5e76 | 2011-08-05 00:09:52 +0000 | [diff] [blame] | 1463 | else if (FunctionTemplateDecl *FTD = |
| 1464 | dyn_cast<FunctionTemplateDecl>(*CD)) |
| 1465 | AddTemplateOverloadCandidate( |
| 1466 | FTD, DeclAccessPair::make(FTD, AS_none), ExplicitTemplateArgs, |
| 1467 | Args, NumArgs, OCS); |
Kaelyn Uhrain | f0c1d8f | 2011-08-03 20:36:05 +0000 | [diff] [blame] | 1468 | } |
| 1469 | switch (OCS.BestViableFunction(*this, R.getNameLoc(), Best)) { |
| 1470 | case OR_Success: |
| 1471 | ND = Best->Function; |
| 1472 | break; |
| 1473 | default: |
Kaelyn Uhrain | 844d572 | 2011-08-04 23:30:54 +0000 | [diff] [blame] | 1474 | break; |
Kaelyn Uhrain | f0c1d8f | 2011-08-03 20:36:05 +0000 | [diff] [blame] | 1475 | } |
| 1476 | } |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1477 | R.addDecl(ND); |
| 1478 | if (isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND)) { |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1479 | if (SS.isEmpty()) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1480 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << CorrectedQuotedStr |
| 1481 | << FixItHint::CreateReplacement(R.getNameLoc(), CorrectedStr); |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1482 | else |
| 1483 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1484 | << Name << computeDeclContext(SS, false) << CorrectedQuotedStr |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1485 | << SS.getRange() |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1486 | << FixItHint::CreateReplacement(R.getNameLoc(), CorrectedStr); |
| 1487 | if (ND) |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1488 | Diag(ND->getLocation(), diag::note_previous_decl) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1489 | << CorrectedQuotedStr; |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1490 | |
| 1491 | // Tell the callee to try to recover. |
| 1492 | return false; |
| 1493 | } |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1494 | |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1495 | if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND)) { |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1496 | // FIXME: If we ended up with a typo for a type name or |
| 1497 | // Objective-C class name, we're in trouble because the parser |
| 1498 | // is in the wrong place to recover. Suggest the typo |
| 1499 | // correction, but don't make it a fix-it since we're not going |
| 1500 | // to recover well anyway. |
| 1501 | if (SS.isEmpty()) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 1502 | Diag(R.getNameLoc(), diagnostic_suggest) |
| 1503 | << Name << CorrectedQuotedStr; |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1504 | else |
| 1505 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1506 | << Name << computeDeclContext(SS, false) << CorrectedQuotedStr |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1507 | << SS.getRange(); |
| 1508 | |
| 1509 | // Don't try to recover; it won't work. |
| 1510 | return true; |
| 1511 | } |
| 1512 | } else { |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1513 | // FIXME: We found a keyword. Suggest it, but don't provide a fix-it |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1514 | // because we aren't able to recover. |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1515 | if (SS.isEmpty()) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1516 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << CorrectedQuotedStr; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1517 | else |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1518 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1519 | << Name << computeDeclContext(SS, false) << CorrectedQuotedStr |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1520 | << SS.getRange(); |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1521 | return true; |
| 1522 | } |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1523 | } |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1524 | R.clear(); |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1525 | |
| 1526 | // Emit a special diagnostic for failed member lookups. |
| 1527 | // FIXME: computing the declaration context might fail here (?) |
| 1528 | if (!SS.isEmpty()) { |
| 1529 | Diag(R.getNameLoc(), diag::err_no_member) |
| 1530 | << Name << computeDeclContext(SS, false) |
| 1531 | << SS.getRange(); |
| 1532 | return true; |
| 1533 | } |
| 1534 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1535 | // Give up, we can't recover. |
| 1536 | Diag(R.getNameLoc(), diagnostic) << Name; |
| 1537 | return true; |
| 1538 | } |
| 1539 | |
Douglas Gregor | ca45da0 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1540 | ObjCPropertyDecl *Sema::canSynthesizeProvisionalIvar(IdentifierInfo *II) { |
| 1541 | ObjCMethodDecl *CurMeth = getCurMethodDecl(); |
Fariborz Jahanian | 69d5624 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1542 | ObjCInterfaceDecl *IDecl = CurMeth->getClassInterface(); |
| 1543 | if (!IDecl) |
| 1544 | return 0; |
| 1545 | ObjCImplementationDecl *ClassImpDecl = IDecl->getImplementation(); |
| 1546 | if (!ClassImpDecl) |
| 1547 | return 0; |
Douglas Gregor | ca45da0 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1548 | ObjCPropertyDecl *property = LookupPropertyDecl(IDecl, II); |
Fariborz Jahanian | 69d5624 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1549 | if (!property) |
| 1550 | return 0; |
| 1551 | if (ObjCPropertyImplDecl *PIDecl = ClassImpDecl->FindPropertyImplDecl(II)) |
Douglas Gregor | ca45da0 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1552 | if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic || |
| 1553 | PIDecl->getPropertyIvarDecl()) |
Fariborz Jahanian | 69d5624 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1554 | return 0; |
| 1555 | return property; |
| 1556 | } |
| 1557 | |
Douglas Gregor | ca45da0 | 2010-11-02 20:36:02 +0000 | [diff] [blame] | 1558 | bool Sema::canSynthesizeProvisionalIvar(ObjCPropertyDecl *Property) { |
| 1559 | ObjCMethodDecl *CurMeth = getCurMethodDecl(); |
| 1560 | ObjCInterfaceDecl *IDecl = CurMeth->getClassInterface(); |
| 1561 | if (!IDecl) |
| 1562 | return false; |
| 1563 | ObjCImplementationDecl *ClassImpDecl = IDecl->getImplementation(); |
| 1564 | if (!ClassImpDecl) |
| 1565 | return false; |
| 1566 | if (ObjCPropertyImplDecl *PIDecl |
| 1567 | = ClassImpDecl->FindPropertyImplDecl(Property->getIdentifier())) |
| 1568 | if (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic || |
| 1569 | PIDecl->getPropertyIvarDecl()) |
| 1570 | return false; |
| 1571 | |
| 1572 | return true; |
| 1573 | } |
| 1574 | |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1575 | ObjCIvarDecl *Sema::SynthesizeProvisionalIvar(LookupResult &Lookup, |
| 1576 | IdentifierInfo *II, |
| 1577 | SourceLocation NameLoc) { |
| 1578 | ObjCMethodDecl *CurMeth = getCurMethodDecl(); |
Fariborz Jahanian | 73f666f | 2010-07-30 16:59:05 +0000 | [diff] [blame] | 1579 | bool LookForIvars; |
| 1580 | if (Lookup.empty()) |
| 1581 | LookForIvars = true; |
| 1582 | else if (CurMeth->isClassMethod()) |
| 1583 | LookForIvars = false; |
| 1584 | else |
| 1585 | LookForIvars = (Lookup.isSingleResult() && |
Fariborz Jahanian | d0fbadd | 2011-01-26 00:57:01 +0000 | [diff] [blame] | 1586 | Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod() && |
| 1587 | (Lookup.getAsSingle<VarDecl>() != 0)); |
Fariborz Jahanian | 73f666f | 2010-07-30 16:59:05 +0000 | [diff] [blame] | 1588 | if (!LookForIvars) |
| 1589 | return 0; |
| 1590 | |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1591 | ObjCInterfaceDecl *IDecl = CurMeth->getClassInterface(); |
| 1592 | if (!IDecl) |
| 1593 | return 0; |
| 1594 | ObjCImplementationDecl *ClassImpDecl = IDecl->getImplementation(); |
Fariborz Jahanian | 84ef4b2 | 2010-07-19 16:14:33 +0000 | [diff] [blame] | 1595 | if (!ClassImpDecl) |
| 1596 | return 0; |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1597 | bool DynamicImplSeen = false; |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1598 | ObjCPropertyDecl *property = LookupPropertyDecl(IDecl, II); |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1599 | if (!property) |
| 1600 | return 0; |
Fariborz Jahanian | 43e1b46 | 2010-10-19 19:08:23 +0000 | [diff] [blame] | 1601 | if (ObjCPropertyImplDecl *PIDecl = ClassImpDecl->FindPropertyImplDecl(II)) { |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1602 | DynamicImplSeen = |
| 1603 | (PIDecl->getPropertyImplementation() == ObjCPropertyImplDecl::Dynamic); |
Fariborz Jahanian | 43e1b46 | 2010-10-19 19:08:23 +0000 | [diff] [blame] | 1604 | // property implementation has a designated ivar. No need to assume a new |
| 1605 | // one. |
| 1606 | if (!DynamicImplSeen && PIDecl->getPropertyIvarDecl()) |
| 1607 | return 0; |
| 1608 | } |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1609 | if (!DynamicImplSeen) { |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1610 | QualType PropType = Context.getCanonicalType(property->getType()); |
| 1611 | ObjCIvarDecl *Ivar = ObjCIvarDecl::Create(Context, ClassImpDecl, |
Abramo Bagnara | ff676cb | 2011-03-08 08:55:46 +0000 | [diff] [blame] | 1612 | NameLoc, NameLoc, |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1613 | II, PropType, /*Dinfo=*/0, |
Fariborz Jahanian | 7504966 | 2010-12-15 23:29:04 +0000 | [diff] [blame] | 1614 | ObjCIvarDecl::Private, |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1615 | (Expr *)0, true); |
| 1616 | ClassImpDecl->addDecl(Ivar); |
| 1617 | IDecl->makeDeclVisibleInContext(Ivar, false); |
| 1618 | property->setPropertyIvarDecl(Ivar); |
| 1619 | return Ivar; |
| 1620 | } |
| 1621 | return 0; |
| 1622 | } |
| 1623 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1624 | ExprResult Sema::ActOnIdExpression(Scope *S, |
John McCall | fb97e75 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 1625 | CXXScopeSpec &SS, |
| 1626 | UnqualifiedId &Id, |
| 1627 | bool HasTrailingLParen, |
| 1628 | bool isAddressOfOperand) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1629 | assert(!(isAddressOfOperand && HasTrailingLParen) && |
| 1630 | "cannot be direct & operand and have a trailing lparen"); |
| 1631 | |
| 1632 | if (SS.isInvalid()) |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1633 | return ExprError(); |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1634 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1635 | TemplateArgumentListInfo TemplateArgsBuffer; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1636 | |
| 1637 | // Decompose the UnqualifiedId into the following data. |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1638 | DeclarationNameInfo NameInfo; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1639 | const TemplateArgumentListInfo *TemplateArgs; |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1640 | DecomposeUnqualifiedId(Id, TemplateArgsBuffer, NameInfo, TemplateArgs); |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1641 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1642 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 1643 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1644 | SourceLocation NameLoc = NameInfo.getLoc(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1645 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1646 | // C++ [temp.dep.expr]p3: |
| 1647 | // An id-expression is type-dependent if it contains: |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 1648 | // -- an identifier that was declared with a dependent type, |
| 1649 | // (note: handled after lookup) |
| 1650 | // -- a template-id that is dependent, |
| 1651 | // (note: handled in BuildTemplateIdExpr) |
| 1652 | // -- a conversion-function-id that specifies a dependent type, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1653 | // -- a nested-name-specifier that contains a class-name that |
| 1654 | // names a dependent type. |
| 1655 | // Determine whether this is a member of an unknown specialization; |
| 1656 | // we need to handle these differently. |
Eli Friedman | 647c8b3 | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1657 | bool DependentID = false; |
| 1658 | if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName && |
| 1659 | Name.getCXXNameType()->isDependentType()) { |
| 1660 | DependentID = true; |
| 1661 | } else if (SS.isSet()) { |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1662 | if (DeclContext *DC = computeDeclContext(SS, false)) { |
Eli Friedman | 647c8b3 | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1663 | if (RequireCompleteDeclContext(SS, DC)) |
| 1664 | return ExprError(); |
Eli Friedman | 647c8b3 | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1665 | } else { |
| 1666 | DependentID = true; |
| 1667 | } |
| 1668 | } |
| 1669 | |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1670 | if (DependentID) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1671 | return ActOnDependentIdExpression(SS, NameInfo, isAddressOfOperand, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1672 | TemplateArgs); |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1673 | |
Fariborz Jahanian | 69d5624 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1674 | bool IvarLookupFollowUp = false; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1675 | // Perform the required lookup. |
Fariborz Jahanian | 98a5403 | 2011-07-12 17:16:56 +0000 | [diff] [blame] | 1676 | LookupResult R(*this, NameInfo, |
| 1677 | (Id.getKind() == UnqualifiedId::IK_ImplicitSelfParam) |
| 1678 | ? LookupObjCImplicitSelfParam : LookupOrdinaryName); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1679 | if (TemplateArgs) { |
Douglas Gregor | d2235f6 | 2010-05-20 20:58:56 +0000 | [diff] [blame] | 1680 | // Lookup the template name again to correctly establish the context in |
| 1681 | // which it was found. This is really unfortunate as we already did the |
| 1682 | // lookup to determine that it was a template name in the first place. If |
| 1683 | // this becomes a performance hit, we can work harder to preserve those |
| 1684 | // results until we get here but it's likely not worth it. |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1685 | bool MemberOfUnknownSpecialization; |
| 1686 | LookupTemplateName(R, S, SS, QualType(), /*EnteringContext=*/false, |
| 1687 | MemberOfUnknownSpecialization); |
Douglas Gregor | 2f9f89c | 2011-02-04 13:35:07 +0000 | [diff] [blame] | 1688 | |
| 1689 | if (MemberOfUnknownSpecialization || |
| 1690 | (R.getResultKind() == LookupResult::NotFoundInCurrentInstantiation)) |
| 1691 | return ActOnDependentIdExpression(SS, NameInfo, isAddressOfOperand, |
| 1692 | TemplateArgs); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1693 | } else { |
Fariborz Jahanian | 69d5624 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1694 | IvarLookupFollowUp = (!SS.isSet() && II && getCurMethodDecl()); |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1695 | LookupParsedName(R, S, &SS, !IvarLookupFollowUp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1696 | |
Douglas Gregor | 2f9f89c | 2011-02-04 13:35:07 +0000 | [diff] [blame] | 1697 | // If the result might be in a dependent base class, this is a dependent |
| 1698 | // id-expression. |
| 1699 | if (R.getResultKind() == LookupResult::NotFoundInCurrentInstantiation) |
| 1700 | return ActOnDependentIdExpression(SS, NameInfo, isAddressOfOperand, |
| 1701 | TemplateArgs); |
| 1702 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1703 | // If this reference is in an Objective-C method, then we need to do |
| 1704 | // some special Objective-C lookup, too. |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1705 | if (IvarLookupFollowUp) { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1706 | ExprResult E(LookupInObjCMethod(R, S, II, true)); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1707 | if (E.isInvalid()) |
| 1708 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1709 | |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1710 | if (Expr *Ex = E.takeAs<Expr>()) |
| 1711 | return Owned(Ex); |
| 1712 | |
| 1713 | // Synthesize ivars lazily. |
Fariborz Jahanian | e776f88 | 2011-01-03 18:08:02 +0000 | [diff] [blame] | 1714 | if (getLangOptions().ObjCDefaultSynthProperties && |
| 1715 | getLangOptions().ObjCNonFragileABI2) { |
Douglas Gregor | 312eadb | 2011-04-24 05:37:28 +0000 | [diff] [blame] | 1716 | if (SynthesizeProvisionalIvar(R, II, NameLoc)) { |
Fariborz Jahanian | de26760 | 2010-11-17 19:41:23 +0000 | [diff] [blame] | 1717 | if (const ObjCPropertyDecl *Property = |
| 1718 | canSynthesizeProvisionalIvar(II)) { |
| 1719 | Diag(NameLoc, diag::warn_synthesized_ivar_access) << II; |
| 1720 | Diag(Property->getLocation(), diag::note_property_declare); |
| 1721 | } |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1722 | return ActOnIdExpression(S, SS, Id, HasTrailingLParen, |
| 1723 | isAddressOfOperand); |
Fariborz Jahanian | de26760 | 2010-11-17 19:41:23 +0000 | [diff] [blame] | 1724 | } |
Fariborz Jahanian | ad51e74 | 2010-07-17 00:59:30 +0000 | [diff] [blame] | 1725 | } |
Fariborz Jahanian | f759b4d | 2010-08-13 18:09:39 +0000 | [diff] [blame] | 1726 | // for further use, this must be set to false if in class method. |
| 1727 | IvarLookupFollowUp = getCurMethodDecl()->isInstanceMethod(); |
Steve Naroff | e3e9add | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 1728 | } |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 1729 | } |
Douglas Gregor | c71e28c | 2009-02-16 19:28:42 +0000 | [diff] [blame] | 1730 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1731 | if (R.isAmbiguous()) |
| 1732 | return ExprError(); |
| 1733 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1734 | // Determine whether this name might be a candidate for |
| 1735 | // argument-dependent lookup. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1736 | bool ADL = UseArgumentDependentLookup(SS, R, HasTrailingLParen); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1737 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1738 | if (R.empty() && !ADL) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1739 | // Otherwise, this could be an implicitly declared function reference (legal |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1740 | // in C90, extension in C99, forbidden in C++). |
| 1741 | if (HasTrailingLParen && II && !getLangOptions().CPlusPlus) { |
| 1742 | NamedDecl *D = ImplicitlyDefineFunction(NameLoc, *II, S); |
| 1743 | if (D) R.addDecl(D); |
| 1744 | } |
| 1745 | |
| 1746 | // If this name wasn't predeclared and if this is not a function |
| 1747 | // call, diagnose the problem. |
| 1748 | if (R.empty()) { |
Douglas Gregor | 91f7ac7 | 2010-05-18 16:14:23 +0000 | [diff] [blame] | 1749 | if (DiagnoseEmptyLookup(S, SS, R, CTC_Unknown)) |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1750 | return ExprError(); |
| 1751 | |
| 1752 | assert(!R.empty() && |
| 1753 | "DiagnoseEmptyLookup returned false but added no results"); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1754 | |
| 1755 | // If we found an Objective-C instance variable, let |
| 1756 | // LookupInObjCMethod build the appropriate expression to |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1757 | // reference the ivar. |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1758 | if (ObjCIvarDecl *Ivar = R.getAsSingle<ObjCIvarDecl>()) { |
| 1759 | R.clear(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1760 | ExprResult E(LookupInObjCMethod(R, S, Ivar->getIdentifier())); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1761 | assert(E.isInvalid() || E.get()); |
| 1762 | return move(E); |
| 1763 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1764 | } |
| 1765 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1766 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1767 | // This is guaranteed from this point on. |
| 1768 | assert(!R.empty() || ADL); |
| 1769 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1770 | // Check whether this might be a C++ implicit instance member access. |
John McCall | fb97e75 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 1771 | // C++ [class.mfct.non-static]p3: |
| 1772 | // When an id-expression that is not part of a class member access |
| 1773 | // syntax and not used to form a pointer to member is used in the |
| 1774 | // body of a non-static member function of class X, if name lookup |
| 1775 | // resolves the name in the id-expression to a non-static non-type |
| 1776 | // member of some class C, the id-expression is transformed into a |
| 1777 | // class member access expression using (*this) as the |
| 1778 | // postfix-expression to the left of the . operator. |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1779 | // |
| 1780 | // But we don't actually need to do this for '&' operands if R |
| 1781 | // resolved to a function or overloaded function set, because the |
| 1782 | // expression is ill-formed if it actually works out to be a |
| 1783 | // non-static member function: |
| 1784 | // |
| 1785 | // C++ [expr.ref]p4: |
| 1786 | // Otherwise, if E1.E2 refers to a non-static member function. . . |
| 1787 | // [t]he expression can be used only as the left-hand operand of a |
| 1788 | // member function call. |
| 1789 | // |
| 1790 | // There are other safeguards against such uses, but it's important |
| 1791 | // to get this right here so that we don't end up making a |
| 1792 | // spuriously dependent expression if we're inside a dependent |
| 1793 | // instance method. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1794 | if (!R.empty() && (*R.begin())->isCXXClassMember()) { |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1795 | bool MightBeImplicitMember; |
| 1796 | if (!isAddressOfOperand) |
| 1797 | MightBeImplicitMember = true; |
| 1798 | else if (!SS.isEmpty()) |
| 1799 | MightBeImplicitMember = false; |
| 1800 | else if (R.isOverloadedResult()) |
| 1801 | MightBeImplicitMember = false; |
Douglas Gregor | e2248be | 2010-08-30 16:00:47 +0000 | [diff] [blame] | 1802 | else if (R.isUnresolvableResult()) |
| 1803 | MightBeImplicitMember = true; |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1804 | else |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 1805 | MightBeImplicitMember = isa<FieldDecl>(R.getFoundDecl()) || |
| 1806 | isa<IndirectFieldDecl>(R.getFoundDecl()); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1807 | |
| 1808 | if (MightBeImplicitMember) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1809 | return BuildPossibleImplicitMemberExpr(SS, R, TemplateArgs); |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1810 | } |
| 1811 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1812 | if (TemplateArgs) |
| 1813 | return BuildTemplateIdExpr(SS, R, ADL, *TemplateArgs); |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1814 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1815 | return BuildDeclarationNameExpr(SS, R, ADL); |
| 1816 | } |
| 1817 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1818 | /// BuildQualifiedDeclarationNameExpr - Build a C++ qualified |
| 1819 | /// declaration name, generally during template instantiation. |
| 1820 | /// There's a large number of things which don't need to be done along |
| 1821 | /// this path. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1822 | ExprResult |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1823 | Sema::BuildQualifiedDeclarationNameExpr(CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1824 | const DeclarationNameInfo &NameInfo) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1825 | DeclContext *DC; |
Douglas Gregor | e6ec5c4 | 2010-04-28 07:04:26 +0000 | [diff] [blame] | 1826 | if (!(DC = computeDeclContext(SS, false)) || DC->isDependentContext()) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1827 | return BuildDependentDeclRefExpr(SS, NameInfo, 0); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1828 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 1829 | if (RequireCompleteDeclContext(SS, DC)) |
Douglas Gregor | e6ec5c4 | 2010-04-28 07:04:26 +0000 | [diff] [blame] | 1830 | return ExprError(); |
| 1831 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1832 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1833 | LookupQualifiedName(R, DC); |
| 1834 | |
| 1835 | if (R.isAmbiguous()) |
| 1836 | return ExprError(); |
| 1837 | |
| 1838 | if (R.empty()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1839 | Diag(NameInfo.getLoc(), diag::err_no_member) |
| 1840 | << NameInfo.getName() << DC << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1841 | return ExprError(); |
| 1842 | } |
| 1843 | |
| 1844 | return BuildDeclarationNameExpr(SS, R, /*ADL*/ false); |
| 1845 | } |
| 1846 | |
| 1847 | /// LookupInObjCMethod - The parser has read a name in, and Sema has |
| 1848 | /// detected that we're currently inside an ObjC method. Perform some |
| 1849 | /// additional lookup. |
| 1850 | /// |
| 1851 | /// Ideally, most of this would be done by lookup, but there's |
| 1852 | /// actually quite a lot of extra work involved. |
| 1853 | /// |
| 1854 | /// Returns a null sentinel to indicate trivial success. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1855 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1856 | Sema::LookupInObjCMethod(LookupResult &Lookup, Scope *S, |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 1857 | IdentifierInfo *II, bool AllowBuiltinCreation) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1858 | SourceLocation Loc = Lookup.getNameLoc(); |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1859 | ObjCMethodDecl *CurMethod = getCurMethodDecl(); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1860 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1861 | // There are two cases to handle here. 1) scoped lookup could have failed, |
| 1862 | // in which case we should look for an ivar. 2) scoped lookup could have |
| 1863 | // found a decl, but that decl is outside the current instance method (i.e. |
| 1864 | // a global variable). In these two cases, we do a lookup for an ivar with |
| 1865 | // this name, if the lookup sucedes, we replace it our current decl. |
| 1866 | |
| 1867 | // If we're in a class method, we don't normally want to look for |
| 1868 | // ivars. But if we don't find anything else, and there's an |
| 1869 | // ivar, that's an error. |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1870 | bool IsClassMethod = CurMethod->isClassMethod(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1871 | |
| 1872 | bool LookForIvars; |
| 1873 | if (Lookup.empty()) |
| 1874 | LookForIvars = true; |
| 1875 | else if (IsClassMethod) |
| 1876 | LookForIvars = false; |
| 1877 | else |
| 1878 | LookForIvars = (Lookup.isSingleResult() && |
| 1879 | Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod()); |
Fariborz Jahanian | 412e798 | 2010-02-09 19:31:38 +0000 | [diff] [blame] | 1880 | ObjCInterfaceDecl *IFace = 0; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1881 | if (LookForIvars) { |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1882 | IFace = CurMethod->getClassInterface(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1883 | ObjCInterfaceDecl *ClassDeclared; |
| 1884 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
| 1885 | // Diagnose using an ivar in a class method. |
| 1886 | if (IsClassMethod) |
| 1887 | return ExprError(Diag(Loc, diag::error_ivar_use_in_class_method) |
| 1888 | << IV->getDeclName()); |
| 1889 | |
| 1890 | // If we're referencing an invalid decl, just return this as a silent |
| 1891 | // error node. The error diagnostic was already emitted on the decl. |
| 1892 | if (IV->isInvalidDecl()) |
| 1893 | return ExprError(); |
| 1894 | |
| 1895 | // Check if referencing a field with __attribute__((deprecated)). |
| 1896 | if (DiagnoseUseOfDecl(IV, Loc)) |
| 1897 | return ExprError(); |
| 1898 | |
| 1899 | // Diagnose the use of an ivar outside of the declaring class. |
| 1900 | if (IV->getAccessControl() == ObjCIvarDecl::Private && |
| 1901 | ClassDeclared != IFace) |
| 1902 | Diag(Loc, diag::error_private_ivar_access) << IV->getDeclName(); |
| 1903 | |
| 1904 | // FIXME: This should use a new expr for a direct reference, don't |
| 1905 | // turn this into Self->ivar, just return a BareIVarExpr or something. |
| 1906 | IdentifierInfo &II = Context.Idents.get("self"); |
| 1907 | UnqualifiedId SelfName; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1908 | SelfName.setIdentifier(&II, SourceLocation()); |
Fariborz Jahanian | 98a5403 | 2011-07-12 17:16:56 +0000 | [diff] [blame] | 1909 | SelfName.setKind(UnqualifiedId::IK_ImplicitSelfParam); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1910 | CXXScopeSpec SelfScopeSpec; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1911 | ExprResult SelfExpr = ActOnIdExpression(S, SelfScopeSpec, |
Douglas Gregor | e45bb6a | 2010-09-22 16:33:13 +0000 | [diff] [blame] | 1912 | SelfName, false, false); |
| 1913 | if (SelfExpr.isInvalid()) |
| 1914 | return ExprError(); |
| 1915 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1916 | SelfExpr = DefaultLvalueConversion(SelfExpr.take()); |
| 1917 | if (SelfExpr.isInvalid()) |
| 1918 | return ExprError(); |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 1919 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1920 | MarkDeclarationReferenced(Loc, IV); |
| 1921 | return Owned(new (Context) |
| 1922 | ObjCIvarRefExpr(IV, IV->getType(), Loc, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1923 | SelfExpr.take(), true, true)); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1924 | } |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1925 | } else if (CurMethod->isInstanceMethod()) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1926 | // We should warn if a local variable hides an ivar. |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1927 | ObjCInterfaceDecl *IFace = CurMethod->getClassInterface(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1928 | ObjCInterfaceDecl *ClassDeclared; |
| 1929 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
| 1930 | if (IV->getAccessControl() != ObjCIvarDecl::Private || |
| 1931 | IFace == ClassDeclared) |
| 1932 | Diag(Loc, diag::warn_ivar_use_hidden) << IV->getDeclName(); |
| 1933 | } |
| 1934 | } |
| 1935 | |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1936 | if (Lookup.empty() && II && AllowBuiltinCreation) { |
| 1937 | // FIXME. Consolidate this with similar code in LookupName. |
| 1938 | if (unsigned BuiltinID = II->getBuiltinID()) { |
| 1939 | if (!(getLangOptions().CPlusPlus && |
| 1940 | Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))) { |
| 1941 | NamedDecl *D = LazilyCreateBuiltin((IdentifierInfo *)II, BuiltinID, |
| 1942 | S, Lookup.isForRedeclaration(), |
| 1943 | Lookup.getNameLoc()); |
| 1944 | if (D) Lookup.addDecl(D); |
| 1945 | } |
| 1946 | } |
| 1947 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1948 | // Sentinel value saying that we didn't do anything special. |
| 1949 | return Owned((Expr*) 0); |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1950 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1951 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1952 | /// \brief Cast a base object to a member's actual type. |
| 1953 | /// |
| 1954 | /// Logically this happens in three phases: |
| 1955 | /// |
| 1956 | /// * First we cast from the base type to the naming class. |
| 1957 | /// The naming class is the class into which we were looking |
| 1958 | /// when we found the member; it's the qualifier type if a |
| 1959 | /// qualifier was provided, and otherwise it's the base type. |
| 1960 | /// |
| 1961 | /// * Next we cast from the naming class to the declaring class. |
| 1962 | /// If the member we found was brought into a class's scope by |
| 1963 | /// a using declaration, this is that class; otherwise it's |
| 1964 | /// the class declaring the member. |
| 1965 | /// |
| 1966 | /// * Finally we cast from the declaring class to the "true" |
| 1967 | /// declaring class of the member. This conversion does not |
| 1968 | /// obey access control. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1969 | ExprResult |
| 1970 | Sema::PerformObjectMemberConversion(Expr *From, |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1971 | NestedNameSpecifier *Qualifier, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1972 | NamedDecl *FoundDecl, |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1973 | NamedDecl *Member) { |
| 1974 | CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Member->getDeclContext()); |
| 1975 | if (!RD) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1976 | return Owned(From); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1977 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1978 | QualType DestRecordType; |
| 1979 | QualType DestType; |
| 1980 | QualType FromRecordType; |
| 1981 | QualType FromType = From->getType(); |
| 1982 | bool PointerConversions = false; |
| 1983 | if (isa<FieldDecl>(Member)) { |
| 1984 | DestRecordType = Context.getCanonicalType(Context.getTypeDeclType(RD)); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1985 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1986 | if (FromType->getAs<PointerType>()) { |
| 1987 | DestType = Context.getPointerType(DestRecordType); |
| 1988 | FromRecordType = FromType->getPointeeType(); |
| 1989 | PointerConversions = true; |
| 1990 | } else { |
| 1991 | DestType = DestRecordType; |
| 1992 | FromRecordType = FromType; |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 1993 | } |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1994 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member)) { |
| 1995 | if (Method->isStatic()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1996 | return Owned(From); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1997 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1998 | DestType = Method->getThisType(Context); |
| 1999 | DestRecordType = DestType->getPointeeType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2000 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2001 | if (FromType->getAs<PointerType>()) { |
| 2002 | FromRecordType = FromType->getPointeeType(); |
| 2003 | PointerConversions = true; |
| 2004 | } else { |
| 2005 | FromRecordType = FromType; |
| 2006 | DestType = DestRecordType; |
| 2007 | } |
| 2008 | } else { |
| 2009 | // No conversion necessary. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2010 | return Owned(From); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2011 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2012 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2013 | if (DestType->isDependentType() || FromType->isDependentType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2014 | return Owned(From); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2015 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2016 | // If the unqualified types are the same, no conversion is necessary. |
| 2017 | if (Context.hasSameUnqualifiedType(FromRecordType, DestRecordType)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2018 | return Owned(From); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2019 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2020 | SourceRange FromRange = From->getSourceRange(); |
| 2021 | SourceLocation FromLoc = FromRange.getBegin(); |
| 2022 | |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 2023 | ExprValueKind VK = CastCategory(From); |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2024 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2025 | // C++ [class.member.lookup]p8: |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2026 | // [...] Ambiguities can often be resolved by qualifying a name with its |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2027 | // class name. |
| 2028 | // |
| 2029 | // If the member was a qualified name and the qualified referred to a |
| 2030 | // specific base subobject type, we'll cast to that intermediate type |
| 2031 | // first and then to the object in which the member is declared. That allows |
| 2032 | // one to resolve ambiguities in, e.g., a diamond-shaped hierarchy such as: |
| 2033 | // |
| 2034 | // class Base { public: int x; }; |
| 2035 | // class Derived1 : public Base { }; |
| 2036 | // class Derived2 : public Base { }; |
| 2037 | // class VeryDerived : public Derived1, public Derived2 { void f(); }; |
| 2038 | // |
| 2039 | // void VeryDerived::f() { |
| 2040 | // x = 17; // error: ambiguous base subobjects |
| 2041 | // Derived1::x = 17; // okay, pick the Base subobject of Derived1 |
| 2042 | // } |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2043 | if (Qualifier) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2044 | QualType QType = QualType(Qualifier->getAsType(), 0); |
| 2045 | assert(!QType.isNull() && "lookup done with dependent qualifier?"); |
| 2046 | assert(QType->isRecordType() && "lookup done with non-record type"); |
| 2047 | |
| 2048 | QualType QRecordType = QualType(QType->getAs<RecordType>(), 0); |
| 2049 | |
| 2050 | // In C++98, the qualifier type doesn't actually have to be a base |
| 2051 | // type of the object type, in which case we just ignore it. |
| 2052 | // Otherwise build the appropriate casts. |
| 2053 | if (IsDerivedFrom(FromRecordType, QRecordType)) { |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2054 | CXXCastPath BasePath; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2055 | if (CheckDerivedToBaseConversion(FromRecordType, QRecordType, |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2056 | FromLoc, FromRange, &BasePath)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2057 | return ExprError(); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2058 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2059 | if (PointerConversions) |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2060 | QType = Context.getPointerType(QType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2061 | From = ImpCastExprToType(From, QType, CK_UncheckedDerivedToBase, |
| 2062 | VK, &BasePath).take(); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2063 | |
| 2064 | FromType = QType; |
| 2065 | FromRecordType = QRecordType; |
| 2066 | |
| 2067 | // If the qualifier type was the same as the destination type, |
| 2068 | // we're done. |
| 2069 | if (Context.hasSameUnqualifiedType(FromRecordType, DestRecordType)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2070 | return Owned(From); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2071 | } |
| 2072 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2073 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2074 | bool IgnoreAccess = false; |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2075 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2076 | // If we actually found the member through a using declaration, cast |
| 2077 | // down to the using declaration's type. |
| 2078 | // |
| 2079 | // Pointer equality is fine here because only one declaration of a |
| 2080 | // class ever has member declarations. |
| 2081 | if (FoundDecl->getDeclContext() != Member->getDeclContext()) { |
| 2082 | assert(isa<UsingShadowDecl>(FoundDecl)); |
| 2083 | QualType URecordType = Context.getTypeDeclType( |
| 2084 | cast<CXXRecordDecl>(FoundDecl->getDeclContext())); |
| 2085 | |
| 2086 | // We only need to do this if the naming-class to declaring-class |
| 2087 | // conversion is non-trivial. |
| 2088 | if (!Context.hasSameUnqualifiedType(FromRecordType, URecordType)) { |
| 2089 | assert(IsDerivedFrom(FromRecordType, URecordType)); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2090 | CXXCastPath BasePath; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2091 | if (CheckDerivedToBaseConversion(FromRecordType, URecordType, |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2092 | FromLoc, FromRange, &BasePath)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2093 | return ExprError(); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 2094 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2095 | QualType UType = URecordType; |
| 2096 | if (PointerConversions) |
| 2097 | UType = Context.getPointerType(UType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2098 | From = ImpCastExprToType(From, UType, CK_UncheckedDerivedToBase, |
| 2099 | VK, &BasePath).take(); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2100 | FromType = UType; |
| 2101 | FromRecordType = URecordType; |
| 2102 | } |
| 2103 | |
| 2104 | // We don't do access control for the conversion from the |
| 2105 | // declaring class to the true declaring class. |
| 2106 | IgnoreAccess = true; |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2107 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2108 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2109 | CXXCastPath BasePath; |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2110 | if (CheckDerivedToBaseConversion(FromRecordType, DestRecordType, |
| 2111 | FromLoc, FromRange, &BasePath, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2112 | IgnoreAccess)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2113 | return ExprError(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2114 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2115 | return ImpCastExprToType(From, DestType, CK_UncheckedDerivedToBase, |
| 2116 | VK, &BasePath); |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 2117 | } |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 2118 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2119 | bool Sema::UseArgumentDependentLookup(const CXXScopeSpec &SS, |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2120 | const LookupResult &R, |
| 2121 | bool HasTrailingLParen) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2122 | // Only when used directly as the postfix-expression of a call. |
| 2123 | if (!HasTrailingLParen) |
| 2124 | return false; |
| 2125 | |
| 2126 | // Never if a scope specifier was provided. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2127 | if (SS.isSet()) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2128 | return false; |
| 2129 | |
| 2130 | // Only in C++ or ObjC++. |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2131 | if (!getLangOptions().CPlusPlus) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2132 | return false; |
| 2133 | |
| 2134 | // Turn off ADL when we find certain kinds of declarations during |
| 2135 | // normal lookup: |
| 2136 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 2137 | NamedDecl *D = *I; |
| 2138 | |
| 2139 | // C++0x [basic.lookup.argdep]p3: |
| 2140 | // -- a declaration of a class member |
| 2141 | // Since using decls preserve this property, we check this on the |
| 2142 | // original decl. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2143 | if (D->isCXXClassMember()) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2144 | return false; |
| 2145 | |
| 2146 | // C++0x [basic.lookup.argdep]p3: |
| 2147 | // -- a block-scope function declaration that is not a |
| 2148 | // using-declaration |
| 2149 | // NOTE: we also trigger this for function templates (in fact, we |
| 2150 | // don't check the decl type at all, since all other decl types |
| 2151 | // turn off ADL anyway). |
| 2152 | if (isa<UsingShadowDecl>(D)) |
| 2153 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 2154 | else if (D->getDeclContext()->isFunctionOrMethod()) |
| 2155 | return false; |
| 2156 | |
| 2157 | // C++0x [basic.lookup.argdep]p3: |
| 2158 | // -- a declaration that is neither a function or a function |
| 2159 | // template |
| 2160 | // And also for builtin functions. |
| 2161 | if (isa<FunctionDecl>(D)) { |
| 2162 | FunctionDecl *FDecl = cast<FunctionDecl>(D); |
| 2163 | |
| 2164 | // But also builtin functions. |
| 2165 | if (FDecl->getBuiltinID() && FDecl->isImplicit()) |
| 2166 | return false; |
| 2167 | } else if (!isa<FunctionTemplateDecl>(D)) |
| 2168 | return false; |
| 2169 | } |
| 2170 | |
| 2171 | return true; |
| 2172 | } |
| 2173 | |
| 2174 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2175 | /// Diagnoses obvious problems with the use of the given declaration |
| 2176 | /// as an expression. This is only actually called for lookups that |
| 2177 | /// were not overloaded, and it doesn't promise that the declaration |
| 2178 | /// will in fact be used. |
| 2179 | static bool CheckDeclInExpr(Sema &S, SourceLocation Loc, NamedDecl *D) { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2180 | if (isa<TypedefNameDecl>(D)) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2181 | S.Diag(Loc, diag::err_unexpected_typedef) << D->getDeclName(); |
| 2182 | return true; |
| 2183 | } |
| 2184 | |
| 2185 | if (isa<ObjCInterfaceDecl>(D)) { |
| 2186 | S.Diag(Loc, diag::err_unexpected_interface) << D->getDeclName(); |
| 2187 | return true; |
| 2188 | } |
| 2189 | |
| 2190 | if (isa<NamespaceDecl>(D)) { |
| 2191 | S.Diag(Loc, diag::err_unexpected_namespace) << D->getDeclName(); |
| 2192 | return true; |
| 2193 | } |
| 2194 | |
| 2195 | return false; |
| 2196 | } |
| 2197 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2198 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2199 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2200 | LookupResult &R, |
| 2201 | bool NeedsADL) { |
John McCall | fead20c | 2009-12-08 22:45:53 +0000 | [diff] [blame] | 2202 | // If this is a single, fully-resolved result and we don't need ADL, |
| 2203 | // just build an ordinary singleton decl ref. |
Douglas Gregor | 86b8e09 | 2010-01-29 17:15:43 +0000 | [diff] [blame] | 2204 | if (!NeedsADL && R.isSingleResult() && !R.getAsSingle<FunctionTemplateDecl>()) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2205 | return BuildDeclarationNameExpr(SS, R.getLookupNameInfo(), |
| 2206 | R.getFoundDecl()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2207 | |
| 2208 | // We only need to check the declaration if there's exactly one |
| 2209 | // result, because in the overloaded case the results can only be |
| 2210 | // functions and function templates. |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2211 | if (R.isSingleResult() && |
| 2212 | CheckDeclInExpr(*this, R.getNameLoc(), R.getFoundDecl())) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2213 | return ExprError(); |
| 2214 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2215 | // Otherwise, just build an unresolved lookup expression. Suppress |
| 2216 | // any lookup-related diagnostics; we'll hash these out later, when |
| 2217 | // we've picked a target. |
| 2218 | R.suppressDiagnostics(); |
| 2219 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2220 | UnresolvedLookupExpr *ULE |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2221 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2222 | SS.getWithLocInContext(Context), |
| 2223 | R.getLookupNameInfo(), |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 2224 | NeedsADL, R.isOverloadedResult(), |
| 2225 | R.begin(), R.end()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2226 | |
| 2227 | return Owned(ULE); |
| 2228 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2229 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2230 | /// \brief Complete semantic analysis for a reference to the given declaration. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2231 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2232 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2233 | const DeclarationNameInfo &NameInfo, |
| 2234 | NamedDecl *D) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2235 | assert(D && "Cannot refer to a NULL declaration"); |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 2236 | assert(!isa<FunctionTemplateDecl>(D) && |
| 2237 | "Cannot refer unambiguously to a function template"); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2238 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2239 | SourceLocation Loc = NameInfo.getLoc(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2240 | if (CheckDeclInExpr(*this, Loc, D)) |
| 2241 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2242 | |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2243 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) { |
| 2244 | // Specifically diagnose references to class templates that are missing |
| 2245 | // a template argument list. |
| 2246 | Diag(Loc, diag::err_template_decl_ref) |
| 2247 | << Template << SS.getRange(); |
| 2248 | Diag(Template->getLocation(), diag::note_template_decl_here); |
| 2249 | return ExprError(); |
| 2250 | } |
| 2251 | |
| 2252 | // Make sure that we're referring to a value. |
| 2253 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 2254 | if (!VD) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2255 | Diag(Loc, diag::err_ref_non_value) |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2256 | << D << SS.getRange(); |
John McCall | 87cf670 | 2009-12-18 18:35:10 +0000 | [diff] [blame] | 2257 | Diag(D->getLocation(), diag::note_declared_at); |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2258 | return ExprError(); |
| 2259 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2260 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2261 | // Check whether this declaration can be used. Note that we suppress |
| 2262 | // this check when we're going to perform argument-dependent lookup |
| 2263 | // on this function name, because this might not be the function |
| 2264 | // that overload resolution actually selects. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2265 | if (DiagnoseUseOfDecl(VD, Loc)) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2266 | return ExprError(); |
| 2267 | |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2268 | // Only create DeclRefExpr's for valid Decl's. |
| 2269 | if (VD->isInvalidDecl()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2270 | return ExprError(); |
| 2271 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2272 | // Handle members of anonymous structs and unions. If we got here, |
| 2273 | // and the reference is to a class member indirect field, then this |
| 2274 | // must be the subject of a pointer-to-member expression. |
| 2275 | if (IndirectFieldDecl *indirectField = dyn_cast<IndirectFieldDecl>(VD)) |
| 2276 | if (!indirectField->isCXXClassMember()) |
| 2277 | return BuildAnonymousStructUnionMemberReference(SS, NameInfo.getLoc(), |
| 2278 | indirectField); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2279 | |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 2280 | // If the identifier reference is inside a block, and it refers to a value |
| 2281 | // that is outside the block, create a BlockDeclRefExpr instead of a |
| 2282 | // DeclRefExpr. This ensures the value is treated as a copy-in snapshot when |
| 2283 | // the block is formed. |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2284 | // |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 2285 | // We do not do this for things like enum constants, global variables, etc, |
| 2286 | // as they do not get snapshotted. |
| 2287 | // |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2288 | switch (shouldCaptureValueReference(*this, NameInfo.getLoc(), VD)) { |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2289 | case CR_Error: |
| 2290 | return ExprError(); |
Mike Stump | 0d6fd57 | 2010-01-05 02:56:35 +0000 | [diff] [blame] | 2291 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2292 | case CR_Capture: |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2293 | assert(!SS.isSet() && "referenced local variable with scope specifier?"); |
| 2294 | return BuildBlockDeclRefExpr(*this, VD, NameInfo, /*byref*/ false); |
| 2295 | |
| 2296 | case CR_CaptureByRef: |
| 2297 | assert(!SS.isSet() && "referenced local variable with scope specifier?"); |
| 2298 | return BuildBlockDeclRefExpr(*this, VD, NameInfo, /*byref*/ true); |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2299 | |
| 2300 | case CR_NoCapture: { |
| 2301 | // If this reference is not in a block or if the referenced |
| 2302 | // variable is within the block, create a normal DeclRefExpr. |
| 2303 | |
| 2304 | QualType type = VD->getType(); |
Daniel Dunbar | b20de81 | 2011-02-10 18:29:28 +0000 | [diff] [blame] | 2305 | ExprValueKind valueKind = VK_RValue; |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2306 | |
| 2307 | switch (D->getKind()) { |
| 2308 | // Ignore all the non-ValueDecl kinds. |
| 2309 | #define ABSTRACT_DECL(kind) |
| 2310 | #define VALUE(type, base) |
| 2311 | #define DECL(type, base) \ |
| 2312 | case Decl::type: |
| 2313 | #include "clang/AST/DeclNodes.inc" |
| 2314 | llvm_unreachable("invalid value decl kind"); |
| 2315 | return ExprError(); |
| 2316 | |
| 2317 | // These shouldn't make it here. |
| 2318 | case Decl::ObjCAtDefsField: |
| 2319 | case Decl::ObjCIvar: |
| 2320 | llvm_unreachable("forming non-member reference to ivar?"); |
| 2321 | return ExprError(); |
| 2322 | |
| 2323 | // Enum constants are always r-values and never references. |
| 2324 | // Unresolved using declarations are dependent. |
| 2325 | case Decl::EnumConstant: |
| 2326 | case Decl::UnresolvedUsingValue: |
| 2327 | valueKind = VK_RValue; |
| 2328 | break; |
| 2329 | |
| 2330 | // Fields and indirect fields that got here must be for |
| 2331 | // pointer-to-member expressions; we just call them l-values for |
| 2332 | // internal consistency, because this subexpression doesn't really |
| 2333 | // exist in the high-level semantics. |
| 2334 | case Decl::Field: |
| 2335 | case Decl::IndirectField: |
| 2336 | assert(getLangOptions().CPlusPlus && |
| 2337 | "building reference to field in C?"); |
| 2338 | |
| 2339 | // These can't have reference type in well-formed programs, but |
| 2340 | // for internal consistency we do this anyway. |
| 2341 | type = type.getNonReferenceType(); |
| 2342 | valueKind = VK_LValue; |
| 2343 | break; |
| 2344 | |
| 2345 | // Non-type template parameters are either l-values or r-values |
| 2346 | // depending on the type. |
| 2347 | case Decl::NonTypeTemplateParm: { |
| 2348 | if (const ReferenceType *reftype = type->getAs<ReferenceType>()) { |
| 2349 | type = reftype->getPointeeType(); |
| 2350 | valueKind = VK_LValue; // even if the parameter is an r-value reference |
| 2351 | break; |
| 2352 | } |
| 2353 | |
| 2354 | // For non-references, we need to strip qualifiers just in case |
| 2355 | // the template parameter was declared as 'const int' or whatever. |
| 2356 | valueKind = VK_RValue; |
| 2357 | type = type.getUnqualifiedType(); |
| 2358 | break; |
| 2359 | } |
| 2360 | |
| 2361 | case Decl::Var: |
| 2362 | // In C, "extern void blah;" is valid and is an r-value. |
| 2363 | if (!getLangOptions().CPlusPlus && |
| 2364 | !type.hasQualifiers() && |
| 2365 | type->isVoidType()) { |
| 2366 | valueKind = VK_RValue; |
| 2367 | break; |
| 2368 | } |
| 2369 | // fallthrough |
| 2370 | |
| 2371 | case Decl::ImplicitParam: |
| 2372 | case Decl::ParmVar: |
| 2373 | // These are always l-values. |
| 2374 | valueKind = VK_LValue; |
| 2375 | type = type.getNonReferenceType(); |
| 2376 | break; |
| 2377 | |
| 2378 | case Decl::Function: { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2379 | const FunctionType *fty = type->castAs<FunctionType>(); |
| 2380 | |
| 2381 | // If we're referring to a function with an __unknown_anytype |
| 2382 | // result type, make the entire expression __unknown_anytype. |
| 2383 | if (fty->getResultType() == Context.UnknownAnyTy) { |
| 2384 | type = Context.UnknownAnyTy; |
| 2385 | valueKind = VK_RValue; |
| 2386 | break; |
| 2387 | } |
| 2388 | |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2389 | // Functions are l-values in C++. |
| 2390 | if (getLangOptions().CPlusPlus) { |
| 2391 | valueKind = VK_LValue; |
| 2392 | break; |
| 2393 | } |
| 2394 | |
| 2395 | // C99 DR 316 says that, if a function type comes from a |
| 2396 | // function definition (without a prototype), that type is only |
| 2397 | // used for checking compatibility. Therefore, when referencing |
| 2398 | // the function, we pretend that we don't have the full function |
| 2399 | // type. |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2400 | if (!cast<FunctionDecl>(VD)->hasPrototype() && |
| 2401 | isa<FunctionProtoType>(fty)) |
| 2402 | type = Context.getFunctionNoProtoType(fty->getResultType(), |
| 2403 | fty->getExtInfo()); |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2404 | |
| 2405 | // Functions are r-values in C. |
| 2406 | valueKind = VK_RValue; |
| 2407 | break; |
| 2408 | } |
| 2409 | |
| 2410 | case Decl::CXXMethod: |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2411 | // If we're referring to a method with an __unknown_anytype |
| 2412 | // result type, make the entire expression __unknown_anytype. |
| 2413 | // This should only be possible with a type written directly. |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 2414 | if (const FunctionProtoType *proto |
| 2415 | = dyn_cast<FunctionProtoType>(VD->getType())) |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2416 | if (proto->getResultType() == Context.UnknownAnyTy) { |
| 2417 | type = Context.UnknownAnyTy; |
| 2418 | valueKind = VK_RValue; |
| 2419 | break; |
| 2420 | } |
| 2421 | |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2422 | // C++ methods are l-values if static, r-values if non-static. |
| 2423 | if (cast<CXXMethodDecl>(VD)->isStatic()) { |
| 2424 | valueKind = VK_LValue; |
| 2425 | break; |
| 2426 | } |
| 2427 | // fallthrough |
| 2428 | |
| 2429 | case Decl::CXXConversion: |
| 2430 | case Decl::CXXDestructor: |
| 2431 | case Decl::CXXConstructor: |
| 2432 | valueKind = VK_RValue; |
| 2433 | break; |
| 2434 | } |
| 2435 | |
| 2436 | return BuildDeclRefExpr(VD, type, valueKind, NameInfo, &SS); |
| 2437 | } |
| 2438 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2439 | } |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2440 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2441 | llvm_unreachable("unknown capture result"); |
| 2442 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2443 | } |
| 2444 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2445 | ExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc, tok::TokenKind Kind) { |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 2446 | PredefinedExpr::IdentType IT; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2447 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2448 | switch (Kind) { |
Chris Lattner | 1423ea4 | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 2449 | default: assert(0 && "Unknown simple primary expr!"); |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 2450 | case tok::kw___func__: IT = PredefinedExpr::Func; break; // [C99 6.4.2.2] |
| 2451 | case tok::kw___FUNCTION__: IT = PredefinedExpr::Function; break; |
| 2452 | case tok::kw___PRETTY_FUNCTION__: IT = PredefinedExpr::PrettyFunction; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2453 | } |
Chris Lattner | 1423ea4 | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 2454 | |
Chris Lattner | fa28b30 | 2008-01-12 08:14:25 +0000 | [diff] [blame] | 2455 | // Pre-defined identifiers are of type char[x], where x is the length of the |
| 2456 | // string. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2457 | |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2458 | Decl *currentDecl = getCurFunctionOrMethodDecl(); |
Fariborz Jahanian | eb024ac | 2010-07-23 21:53:24 +0000 | [diff] [blame] | 2459 | if (!currentDecl && getCurBlock()) |
| 2460 | currentDecl = getCurBlock()->TheDecl; |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2461 | if (!currentDecl) { |
Chris Lattner | b0da923 | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 2462 | Diag(Loc, diag::ext_predef_outside_function); |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2463 | currentDecl = Context.getTranslationUnitDecl(); |
Chris Lattner | b0da923 | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 2464 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2465 | |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2466 | QualType ResTy; |
| 2467 | if (cast<DeclContext>(currentDecl)->isDependentContext()) { |
| 2468 | ResTy = Context.DependentTy; |
| 2469 | } else { |
Anders Carlsson | 848fa64 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 2470 | unsigned Length = PredefinedExpr::ComputeName(IT, currentDecl).length(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2471 | |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2472 | llvm::APInt LengthI(32, Length + 1); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2473 | ResTy = Context.CharTy.withConst(); |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2474 | ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0); |
| 2475 | } |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2476 | return Owned(new (Context) PredefinedExpr(Loc, ResTy, IT)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2477 | } |
| 2478 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2479 | ExprResult Sema::ActOnCharacterConstant(const Token &Tok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2480 | llvm::SmallString<16> CharBuffer; |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2481 | bool Invalid = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2482 | StringRef ThisTok = PP.getSpelling(Tok, CharBuffer, &Invalid); |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2483 | if (Invalid) |
| 2484 | return ExprError(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2485 | |
Benjamin Kramer | ddeea56 | 2010-02-27 13:44:12 +0000 | [diff] [blame] | 2486 | CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(), Tok.getLocation(), |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2487 | PP, Tok.getKind()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2488 | if (Literal.hadError()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2489 | return ExprError(); |
Chris Lattner | fc62bfd | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 2490 | |
Chris Lattner | e8337df | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 2491 | QualType Ty; |
| 2492 | if (!getLangOptions().CPlusPlus) |
| 2493 | Ty = Context.IntTy; // 'x' and L'x' -> int in C. |
| 2494 | else if (Literal.isWide()) |
| 2495 | Ty = Context.WCharTy; // L'x' -> wchar_t in C++. |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2496 | else if (Literal.isUTF16()) |
| 2497 | Ty = Context.Char16Ty; // u'x' -> char16_t in C++0x. |
| 2498 | else if (Literal.isUTF32()) |
| 2499 | Ty = Context.Char32Ty; // U'x' -> char32_t in C++0x. |
Eli Friedman | 136b0cd | 2010-02-03 18:21:45 +0000 | [diff] [blame] | 2500 | else if (Literal.isMultiChar()) |
| 2501 | Ty = Context.IntTy; // 'wxyz' -> int in C++. |
Chris Lattner | e8337df | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 2502 | else |
| 2503 | Ty = Context.CharTy; // 'x' -> char in C++ |
Chris Lattner | fc62bfd | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 2504 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2505 | CharacterLiteral::CharacterKind Kind = CharacterLiteral::Ascii; |
| 2506 | if (Literal.isWide()) |
| 2507 | Kind = CharacterLiteral::Wide; |
| 2508 | else if (Literal.isUTF16()) |
| 2509 | Kind = CharacterLiteral::UTF16; |
| 2510 | else if (Literal.isUTF32()) |
| 2511 | Kind = CharacterLiteral::UTF32; |
| 2512 | |
| 2513 | return Owned(new (Context) CharacterLiteral(Literal.getValue(), Kind, Ty, |
| 2514 | Tok.getLocation())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2515 | } |
| 2516 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2517 | ExprResult Sema::ActOnNumericConstant(const Token &Tok) { |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2518 | // Fast path for a single digit (which is quite common). A single digit |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2519 | // cannot have a trigraph, escaped newline, radix prefix, or type suffix. |
| 2520 | if (Tok.getLength() == 1) { |
Chris Lattner | 7216dc9 | 2009-01-26 22:36:52 +0000 | [diff] [blame] | 2521 | const char Val = PP.getSpellingOfSingleCharacterNumericConstant(Tok); |
Chris Lattner | 0c21e84 | 2009-01-16 07:10:29 +0000 | [diff] [blame] | 2522 | unsigned IntSize = Context.Target.getIntWidth(); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2523 | return Owned(IntegerLiteral::Create(Context, llvm::APInt(IntSize, Val-'0'), |
Steve Naroff | 0a47393 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 2524 | Context.IntTy, Tok.getLocation())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2525 | } |
Ted Kremenek | 2839660 | 2009-01-13 23:19:12 +0000 | [diff] [blame] | 2526 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2527 | llvm::SmallString<512> IntegerBuffer; |
Chris Lattner | 2a29904 | 2008-09-30 20:53:45 +0000 | [diff] [blame] | 2528 | // Add padding so that NumericLiteralParser can overread by one character. |
| 2529 | IntegerBuffer.resize(Tok.getLength()+1); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2530 | const char *ThisTokBegin = &IntegerBuffer[0]; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2531 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2532 | // Get the spelling of the token, which eliminates trigraphs, etc. |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2533 | bool Invalid = false; |
| 2534 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid); |
| 2535 | if (Invalid) |
| 2536 | return ExprError(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2537 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2538 | NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2539 | Tok.getLocation(), PP); |
| 2540 | if (Literal.hadError) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2541 | return ExprError(); |
| 2542 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2543 | Expr *Res; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2544 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2545 | if (Literal.isFloatingLiteral()) { |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2546 | QualType Ty; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2547 | if (Literal.isFloat) |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2548 | Ty = Context.FloatTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2549 | else if (!Literal.isLong) |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2550 | Ty = Context.DoubleTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2551 | else |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 2552 | Ty = Context.LongDoubleTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2553 | |
| 2554 | const llvm::fltSemantics &Format = Context.getFloatTypeSemantics(Ty); |
| 2555 | |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2556 | using llvm::APFloat; |
| 2557 | APFloat Val(Format); |
| 2558 | |
| 2559 | APFloat::opStatus result = Literal.GetFloatValue(Val); |
John McCall | 9f2df88 | 2009-12-24 11:09:08 +0000 | [diff] [blame] | 2560 | |
| 2561 | // Overflow is always an error, but underflow is only an error if |
| 2562 | // we underflowed to zero (APFloat reports denormals as underflow). |
| 2563 | if ((result & APFloat::opOverflow) || |
| 2564 | ((result & APFloat::opUnderflow) && Val.isZero())) { |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2565 | unsigned diagnostic; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2566 | llvm::SmallString<20> buffer; |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2567 | if (result & APFloat::opOverflow) { |
John McCall | 2a0d757 | 2010-02-26 23:35:57 +0000 | [diff] [blame] | 2568 | diagnostic = diag::warn_float_overflow; |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2569 | APFloat::getLargest(Format).toString(buffer); |
| 2570 | } else { |
John McCall | 2a0d757 | 2010-02-26 23:35:57 +0000 | [diff] [blame] | 2571 | diagnostic = diag::warn_float_underflow; |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2572 | APFloat::getSmallest(Format).toString(buffer); |
| 2573 | } |
| 2574 | |
| 2575 | Diag(Tok.getLocation(), diagnostic) |
| 2576 | << Ty |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2577 | << StringRef(buffer.data(), buffer.size()); |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2578 | } |
| 2579 | |
| 2580 | bool isExact = (result == APFloat::opOK); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2581 | Res = FloatingLiteral::Create(Context, Val, isExact, Ty, Tok.getLocation()); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2582 | |
Peter Collingbourne | f4f7cb8 | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2583 | if (Ty == Context.DoubleTy) { |
| 2584 | if (getLangOptions().SinglePrecisionConstants) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2585 | Res = ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast).take(); |
Peter Collingbourne | f4f7cb8 | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2586 | } else if (getLangOptions().OpenCL && !getOpenCLOptions().cl_khr_fp64) { |
| 2587 | Diag(Tok.getLocation(), diag::warn_double_const_requires_fp64); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2588 | Res = ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast).take(); |
Peter Collingbourne | f4f7cb8 | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2589 | } |
| 2590 | } |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2591 | } else if (!Literal.isIntegerLiteral()) { |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2592 | return ExprError(); |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2593 | } else { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2594 | QualType Ty; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2595 | |
Neil Booth | b944951 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 2596 | // long long is a C99 feature. |
| 2597 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && |
Neil Booth | 79859c3 | 2007-08-29 22:13:52 +0000 | [diff] [blame] | 2598 | Literal.isLongLong) |
Neil Booth | b944951 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 2599 | Diag(Tok.getLocation(), diag::ext_longlong); |
| 2600 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2601 | // Get the value in the widest-possible width. |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2602 | llvm::APInt ResultVal(Context.Target.getIntMaxTWidth(), 0); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2603 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2604 | if (Literal.GetIntegerValue(ResultVal)) { |
| 2605 | // If this value didn't fit into uintmax_t, warn and force to ull. |
| 2606 | Diag(Tok.getLocation(), diag::warn_integer_too_large); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2607 | Ty = Context.UnsignedLongLongTy; |
| 2608 | assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() && |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2609 | "long long is not intmax_t?"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2610 | } else { |
| 2611 | // If this value fits into a ULL, try to figure out what else it fits into |
| 2612 | // according to the rules of C99 6.4.4.1p5. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2613 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2614 | // Octal, Hexadecimal, and integers with a U suffix are allowed to |
| 2615 | // be an unsigned int. |
| 2616 | bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10; |
| 2617 | |
| 2618 | // Check from smallest to largest, picking the smallest type we can. |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2619 | unsigned Width = 0; |
Chris Lattner | 97c5156 | 2007-08-23 21:58:08 +0000 | [diff] [blame] | 2620 | if (!Literal.isLong && !Literal.isLongLong) { |
| 2621 | // Are int/unsigned possibilities? |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2622 | unsigned IntSize = Context.Target.getIntWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2623 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2624 | // Does it fit in a unsigned int? |
| 2625 | if (ResultVal.isIntN(IntSize)) { |
| 2626 | // Does it fit in a signed int? |
| 2627 | if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2628 | Ty = Context.IntTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2629 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2630 | Ty = Context.UnsignedIntTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2631 | Width = IntSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2632 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2633 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2634 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2635 | // Are long/unsigned long possibilities? |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2636 | if (Ty.isNull() && !Literal.isLongLong) { |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2637 | unsigned LongSize = Context.Target.getLongWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2638 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2639 | // Does it fit in a unsigned long? |
| 2640 | if (ResultVal.isIntN(LongSize)) { |
| 2641 | // Does it fit in a signed long? |
| 2642 | if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2643 | Ty = Context.LongTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2644 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2645 | Ty = Context.UnsignedLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2646 | Width = LongSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2647 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2648 | } |
| 2649 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2650 | // Finally, check long long if needed. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2651 | if (Ty.isNull()) { |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2652 | unsigned LongLongSize = Context.Target.getLongLongWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2653 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2654 | // Does it fit in a unsigned long long? |
| 2655 | if (ResultVal.isIntN(LongLongSize)) { |
| 2656 | // Does it fit in a signed long long? |
Francois Pichet | 2432320 | 2011-01-11 23:38:13 +0000 | [diff] [blame] | 2657 | // To be compatible with MSVC, hex integer literals ending with the |
| 2658 | // LL or i64 suffix are always signed in Microsoft mode. |
Francois Pichet | a15a5ee | 2011-01-11 12:23:00 +0000 | [diff] [blame] | 2659 | if (!Literal.isUnsigned && (ResultVal[LongLongSize-1] == 0 || |
| 2660 | (getLangOptions().Microsoft && Literal.isLongLong))) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2661 | Ty = Context.LongLongTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2662 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2663 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2664 | Width = LongLongSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2665 | } |
| 2666 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2667 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2668 | // If we still couldn't decide a type, we probably have something that |
| 2669 | // does not fit in a signed long long, but has no U suffix. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2670 | if (Ty.isNull()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2671 | Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2672 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2673 | Width = Context.Target.getLongLongWidth(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2674 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2675 | |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2676 | if (ResultVal.getBitWidth() != Width) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2677 | ResultVal = ResultVal.trunc(Width); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2678 | } |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2679 | Res = IntegerLiteral::Create(Context, ResultVal, Ty, Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2680 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2681 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2682 | // If this is an imaginary literal, create the ImaginaryLiteral wrapper. |
| 2683 | if (Literal.isImaginary) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2684 | Res = new (Context) ImaginaryLiteral(Res, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2685 | Context.getComplexType(Res->getType())); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2686 | |
| 2687 | return Owned(Res); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2688 | } |
| 2689 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2690 | ExprResult Sema::ActOnParenExpr(SourceLocation L, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2691 | SourceLocation R, Expr *E) { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2692 | assert((E != 0) && "ActOnParenExpr() missing expr"); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2693 | return Owned(new (Context) ParenExpr(L, R, E)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2694 | } |
| 2695 | |
Chandler Carruth | df1f377 | 2011-05-26 08:53:12 +0000 | [diff] [blame] | 2696 | static bool CheckVecStepTraitOperandType(Sema &S, QualType T, |
| 2697 | SourceLocation Loc, |
| 2698 | SourceRange ArgRange) { |
| 2699 | // [OpenCL 1.1 6.11.12] "The vec_step built-in function takes a built-in |
| 2700 | // scalar or vector data type argument..." |
| 2701 | // Every built-in scalar type (OpenCL 1.1 6.1.1) is either an arithmetic |
| 2702 | // type (C99 6.2.5p18) or void. |
| 2703 | if (!(T->isArithmeticType() || T->isVoidType() || T->isVectorType())) { |
| 2704 | S.Diag(Loc, diag::err_vecstep_non_scalar_vector_type) |
| 2705 | << T << ArgRange; |
| 2706 | return true; |
| 2707 | } |
| 2708 | |
| 2709 | assert((T->isVoidType() || !T->isIncompleteType()) && |
| 2710 | "Scalar types should always be complete"); |
| 2711 | return false; |
| 2712 | } |
| 2713 | |
Chandler Carruth | 42ec65d | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 2714 | static bool CheckExtensionTraitOperandType(Sema &S, QualType T, |
| 2715 | SourceLocation Loc, |
| 2716 | SourceRange ArgRange, |
| 2717 | UnaryExprOrTypeTrait TraitKind) { |
| 2718 | // C99 6.5.3.4p1: |
| 2719 | if (T->isFunctionType()) { |
| 2720 | // alignof(function) is allowed as an extension. |
| 2721 | if (TraitKind == UETT_SizeOf) |
| 2722 | S.Diag(Loc, diag::ext_sizeof_function_type) << ArgRange; |
| 2723 | return false; |
| 2724 | } |
| 2725 | |
| 2726 | // Allow sizeof(void)/alignof(void) as an extension. |
| 2727 | if (T->isVoidType()) { |
| 2728 | S.Diag(Loc, diag::ext_sizeof_void_type) << TraitKind << ArgRange; |
| 2729 | return false; |
| 2730 | } |
| 2731 | |
| 2732 | return true; |
| 2733 | } |
| 2734 | |
| 2735 | static bool CheckObjCTraitOperandConstraints(Sema &S, QualType T, |
| 2736 | SourceLocation Loc, |
| 2737 | SourceRange ArgRange, |
| 2738 | UnaryExprOrTypeTrait TraitKind) { |
| 2739 | // Reject sizeof(interface) and sizeof(interface<proto>) in 64-bit mode. |
| 2740 | if (S.LangOpts.ObjCNonFragileABI && T->isObjCObjectType()) { |
| 2741 | S.Diag(Loc, diag::err_sizeof_nonfragile_interface) |
| 2742 | << T << (TraitKind == UETT_SizeOf) |
| 2743 | << ArgRange; |
| 2744 | return true; |
| 2745 | } |
| 2746 | |
| 2747 | return false; |
| 2748 | } |
| 2749 | |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2750 | /// \brief Check the constrains on expression operands to unary type expression |
| 2751 | /// and type traits. |
| 2752 | /// |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2753 | /// Completes any types necessary and validates the constraints on the operand |
| 2754 | /// expression. The logic mostly mirrors the type-based overload, but may modify |
| 2755 | /// the expression as it completes the type for that expression through template |
| 2756 | /// instantiation, etc. |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2757 | bool Sema::CheckUnaryExprOrTypeTraitOperand(Expr *Op, |
| 2758 | UnaryExprOrTypeTrait ExprKind) { |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2759 | QualType ExprTy = Op->getType(); |
| 2760 | |
| 2761 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 2762 | // the result is the size of the referenced type." |
| 2763 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 2764 | // result shall be the alignment of the referenced type." |
| 2765 | if (const ReferenceType *Ref = ExprTy->getAs<ReferenceType>()) |
| 2766 | ExprTy = Ref->getPointeeType(); |
| 2767 | |
| 2768 | if (ExprKind == UETT_VecStep) |
| 2769 | return CheckVecStepTraitOperandType(*this, ExprTy, Op->getExprLoc(), |
| 2770 | Op->getSourceRange()); |
| 2771 | |
| 2772 | // Whitelist some types as extensions |
| 2773 | if (!CheckExtensionTraitOperandType(*this, ExprTy, Op->getExprLoc(), |
| 2774 | Op->getSourceRange(), ExprKind)) |
| 2775 | return false; |
| 2776 | |
| 2777 | if (RequireCompleteExprType(Op, |
| 2778 | PDiag(diag::err_sizeof_alignof_incomplete_type) |
| 2779 | << ExprKind << Op->getSourceRange(), |
| 2780 | std::make_pair(SourceLocation(), PDiag(0)))) |
| 2781 | return true; |
| 2782 | |
| 2783 | // Completeing the expression's type may have changed it. |
| 2784 | ExprTy = Op->getType(); |
| 2785 | if (const ReferenceType *Ref = ExprTy->getAs<ReferenceType>()) |
| 2786 | ExprTy = Ref->getPointeeType(); |
| 2787 | |
| 2788 | if (CheckObjCTraitOperandConstraints(*this, ExprTy, Op->getExprLoc(), |
| 2789 | Op->getSourceRange(), ExprKind)) |
| 2790 | return true; |
| 2791 | |
Nico Weber | cf73992 | 2011-06-15 02:47:03 +0000 | [diff] [blame] | 2792 | if (ExprKind == UETT_SizeOf) { |
| 2793 | if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(Op->IgnoreParens())) { |
| 2794 | if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(DeclRef->getFoundDecl())) { |
| 2795 | QualType OType = PVD->getOriginalType(); |
| 2796 | QualType Type = PVD->getType(); |
| 2797 | if (Type->isPointerType() && OType->isArrayType()) { |
| 2798 | Diag(Op->getExprLoc(), diag::warn_sizeof_array_param) |
| 2799 | << Type << OType; |
| 2800 | Diag(PVD->getLocation(), diag::note_declared_at); |
| 2801 | } |
| 2802 | } |
| 2803 | } |
| 2804 | } |
| 2805 | |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2806 | return false; |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2807 | } |
| 2808 | |
| 2809 | /// \brief Check the constraints on operands to unary expression and type |
| 2810 | /// traits. |
| 2811 | /// |
| 2812 | /// This will complete any types necessary, and validate the various constraints |
| 2813 | /// on those operands. |
| 2814 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2815 | /// The UsualUnaryConversions() function is *not* called by this routine. |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2816 | /// C99 6.3.2.1p[2-4] all state: |
| 2817 | /// Except when it is the operand of the sizeof operator ... |
| 2818 | /// |
| 2819 | /// C++ [expr.sizeof]p4 |
| 2820 | /// The lvalue-to-rvalue, array-to-pointer, and function-to-pointer |
| 2821 | /// standard conversions are not applied to the operand of sizeof. |
| 2822 | /// |
| 2823 | /// This policy is followed for all of the unary trait expressions. |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2824 | bool Sema::CheckUnaryExprOrTypeTraitOperand(QualType exprType, |
| 2825 | SourceLocation OpLoc, |
| 2826 | SourceRange ExprRange, |
| 2827 | UnaryExprOrTypeTrait ExprKind) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2828 | if (exprType->isDependentType()) |
| 2829 | return false; |
| 2830 | |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 2831 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 2832 | // the result is the size of the referenced type." |
| 2833 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 2834 | // result shall be the alignment of the referenced type." |
| 2835 | if (const ReferenceType *Ref = exprType->getAs<ReferenceType>()) |
| 2836 | exprType = Ref->getPointeeType(); |
| 2837 | |
Chandler Carruth | df1f377 | 2011-05-26 08:53:12 +0000 | [diff] [blame] | 2838 | if (ExprKind == UETT_VecStep) |
| 2839 | return CheckVecStepTraitOperandType(*this, exprType, OpLoc, ExprRange); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2840 | |
Chandler Carruth | 42ec65d | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 2841 | // Whitelist some types as extensions |
| 2842 | if (!CheckExtensionTraitOperandType(*this, exprType, OpLoc, ExprRange, |
| 2843 | ExprKind)) |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 2844 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2845 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2846 | if (RequireCompleteType(OpLoc, exprType, |
Douglas Gregor | 5cc07df | 2009-12-15 16:44:32 +0000 | [diff] [blame] | 2847 | PDiag(diag::err_sizeof_alignof_incomplete_type) |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2848 | << ExprKind << ExprRange)) |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2849 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2850 | |
Chandler Carruth | 42ec65d | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 2851 | if (CheckObjCTraitOperandConstraints(*this, exprType, OpLoc, ExprRange, |
| 2852 | ExprKind)) |
Chris Lattner | 5cb10d3 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 2853 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2854 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2855 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2856 | } |
| 2857 | |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2858 | static bool CheckAlignOfExpr(Sema &S, Expr *E) { |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 2859 | E = E->IgnoreParens(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2860 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2861 | // alignof decl is always ok. |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 2862 | if (isa<DeclRefExpr>(E)) |
| 2863 | return false; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2864 | |
| 2865 | // Cannot know anything else if the expression is dependent. |
| 2866 | if (E->isTypeDependent()) |
| 2867 | return false; |
| 2868 | |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2869 | if (E->getBitField()) { |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2870 | S.Diag(E->getExprLoc(), diag::err_sizeof_alignof_bitfield) |
| 2871 | << 1 << E->getSourceRange(); |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2872 | return true; |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 2873 | } |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2874 | |
| 2875 | // Alignment of a field access is always okay, so long as it isn't a |
| 2876 | // bit-field. |
| 2877 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 2878 | if (isa<FieldDecl>(ME->getMemberDecl())) |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2879 | return false; |
| 2880 | |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2881 | return S.CheckUnaryExprOrTypeTraitOperand(E, UETT_AlignOf); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2882 | } |
| 2883 | |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2884 | bool Sema::CheckVecStepExpr(Expr *E) { |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2885 | E = E->IgnoreParens(); |
| 2886 | |
| 2887 | // Cannot know anything else if the expression is dependent. |
| 2888 | if (E->isTypeDependent()) |
| 2889 | return false; |
| 2890 | |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2891 | return CheckUnaryExprOrTypeTraitOperand(E, UETT_VecStep); |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 2892 | } |
| 2893 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2894 | /// \brief Build a sizeof or alignof expression given a type operand. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2895 | ExprResult |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2896 | Sema::CreateUnaryExprOrTypeTraitExpr(TypeSourceInfo *TInfo, |
| 2897 | SourceLocation OpLoc, |
| 2898 | UnaryExprOrTypeTrait ExprKind, |
| 2899 | SourceRange R) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2900 | if (!TInfo) |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2901 | return ExprError(); |
| 2902 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2903 | QualType T = TInfo->getType(); |
John McCall | 5ab7517 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 2904 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2905 | if (!T->isDependentType() && |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2906 | CheckUnaryExprOrTypeTraitOperand(T, OpLoc, R, ExprKind)) |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2907 | return ExprError(); |
| 2908 | |
| 2909 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2910 | return Owned(new (Context) UnaryExprOrTypeTraitExpr(ExprKind, TInfo, |
| 2911 | Context.getSizeType(), |
| 2912 | OpLoc, R.getEnd())); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2913 | } |
| 2914 | |
| 2915 | /// \brief Build a sizeof or alignof expression given an expression |
| 2916 | /// operand. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2917 | ExprResult |
Chandler Carruth | e72c55b | 2011-05-29 07:32:14 +0000 | [diff] [blame] | 2918 | Sema::CreateUnaryExprOrTypeTraitExpr(Expr *E, SourceLocation OpLoc, |
| 2919 | UnaryExprOrTypeTrait ExprKind) { |
Douglas Gregor | 4f0845e | 2011-06-22 23:21:00 +0000 | [diff] [blame] | 2920 | ExprResult PE = CheckPlaceholderExpr(E); |
| 2921 | if (PE.isInvalid()) |
| 2922 | return ExprError(); |
| 2923 | |
| 2924 | E = PE.get(); |
| 2925 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2926 | // Verify that the operand is valid. |
| 2927 | bool isInvalid = false; |
| 2928 | if (E->isTypeDependent()) { |
| 2929 | // Delay type-checking for type-dependent expressions. |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2930 | } else if (ExprKind == UETT_AlignOf) { |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2931 | isInvalid = CheckAlignOfExpr(*this, E); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2932 | } else if (ExprKind == UETT_VecStep) { |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2933 | isInvalid = CheckVecStepExpr(E); |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2934 | } else if (E->getBitField()) { // C99 6.5.3.4p1. |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2935 | Diag(E->getExprLoc(), diag::err_sizeof_alignof_bitfield) << 0; |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2936 | isInvalid = true; |
| 2937 | } else { |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2938 | isInvalid = CheckUnaryExprOrTypeTraitOperand(E, UETT_SizeOf); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2939 | } |
| 2940 | |
| 2941 | if (isInvalid) |
| 2942 | return ExprError(); |
| 2943 | |
| 2944 | // C99 6.5.3.4p4: the type (an unsigned integer type) is size_t. |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2945 | return Owned(new (Context) UnaryExprOrTypeTraitExpr( |
Chandler Carruth | e72c55b | 2011-05-29 07:32:14 +0000 | [diff] [blame] | 2946 | ExprKind, E, Context.getSizeType(), OpLoc, |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2947 | E->getSourceRange().getEnd())); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2948 | } |
| 2949 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2950 | /// ActOnUnaryExprOrTypeTraitExpr - Handle @c sizeof(type) and @c sizeof @c |
| 2951 | /// expr and the same for @c alignof and @c __alignof |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2952 | /// Note that the ArgRange is invalid if isType is false. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2953 | ExprResult |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2954 | Sema::ActOnUnaryExprOrTypeTraitExpr(SourceLocation OpLoc, |
| 2955 | UnaryExprOrTypeTrait ExprKind, bool isType, |
| 2956 | void *TyOrEx, const SourceRange &ArgRange) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2957 | // If error parsing type, ignore. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2958 | if (TyOrEx == 0) return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2959 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2960 | if (isType) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2961 | TypeSourceInfo *TInfo; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2962 | (void) GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrEx), &TInfo); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2963 | return CreateUnaryExprOrTypeTraitExpr(TInfo, OpLoc, ExprKind, ArgRange); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2964 | } |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2965 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2966 | Expr *ArgEx = (Expr *)TyOrEx; |
Chandler Carruth | e72c55b | 2011-05-29 07:32:14 +0000 | [diff] [blame] | 2967 | ExprResult Result = CreateUnaryExprOrTypeTraitExpr(ArgEx, OpLoc, ExprKind); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2968 | return move(Result); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2969 | } |
| 2970 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2971 | static QualType CheckRealImagOperand(Sema &S, ExprResult &V, SourceLocation Loc, |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 2972 | bool isReal) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2973 | if (V.get()->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 2974 | return S.Context.DependentTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2975 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 2976 | // _Real and _Imag are only l-values for normal l-values. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2977 | if (V.get()->getObjectKind() != OK_Ordinary) { |
| 2978 | V = S.DefaultLvalueConversion(V.take()); |
| 2979 | if (V.isInvalid()) |
| 2980 | return QualType(); |
| 2981 | } |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 2982 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 2983 | // These operators return the element type of a complex type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2984 | if (const ComplexType *CT = V.get()->getType()->getAs<ComplexType>()) |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 2985 | return CT->getElementType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2986 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 2987 | // Otherwise they pass through real integer and floating point types here. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2988 | if (V.get()->getType()->isArithmeticType()) |
| 2989 | return V.get()->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2990 | |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 2991 | // Test for placeholders. |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 2992 | ExprResult PR = S.CheckPlaceholderExpr(V.get()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 2993 | if (PR.isInvalid()) return QualType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2994 | if (PR.get() != V.get()) { |
| 2995 | V = move(PR); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 2996 | return CheckRealImagOperand(S, V, Loc, isReal); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 2997 | } |
| 2998 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 2999 | // Reject anything else. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3000 | S.Diag(Loc, diag::err_realimag_invalid_type) << V.get()->getType() |
Chris Lattner | ba27e2a | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 3001 | << (isReal ? "__real" : "__imag"); |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 3002 | return QualType(); |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 3003 | } |
| 3004 | |
| 3005 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3006 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3007 | ExprResult |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3008 | Sema::ActOnPostfixUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3009 | tok::TokenKind Kind, Expr *Input) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3010 | UnaryOperatorKind Opc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3011 | switch (Kind) { |
| 3012 | default: assert(0 && "Unknown unary op!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3013 | case tok::plusplus: Opc = UO_PostInc; break; |
| 3014 | case tok::minusminus: Opc = UO_PostDec; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3015 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3016 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3017 | return BuildUnaryOp(S, OpLoc, Opc, Input); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3018 | } |
| 3019 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3020 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3021 | Sema::ActOnArraySubscriptExpr(Scope *S, Expr *Base, SourceLocation LLoc, |
| 3022 | Expr *Idx, SourceLocation RLoc) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3023 | // Since this might be a postfix expression, get rid of ParenListExprs. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3024 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3025 | if (Result.isInvalid()) return ExprError(); |
| 3026 | Base = Result.take(); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3027 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3028 | Expr *LHSExp = Base, *RHSExp = Idx; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3029 | |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 3030 | if (getLangOptions().CPlusPlus && |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 3031 | (LHSExp->isTypeDependent() || RHSExp->isTypeDependent())) { |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 3032 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3033 | Context.DependentTy, |
| 3034 | VK_LValue, OK_Ordinary, |
| 3035 | RLoc)); |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 3036 | } |
| 3037 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3038 | if (getLangOptions().CPlusPlus && |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3039 | (LHSExp->getType()->isRecordType() || |
Eli Friedman | 03f332a | 2008-12-15 22:34:21 +0000 | [diff] [blame] | 3040 | LHSExp->getType()->isEnumeralType() || |
| 3041 | RHSExp->getType()->isRecordType() || |
| 3042 | RHSExp->getType()->isEnumeralType())) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3043 | return CreateOverloadedArraySubscriptExpr(LLoc, RLoc, Base, Idx); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 3044 | } |
| 3045 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3046 | return CreateBuiltinArraySubscriptExpr(Base, LLoc, Idx, RLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 3047 | } |
| 3048 | |
| 3049 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3050 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3051 | Sema::CreateBuiltinArraySubscriptExpr(Expr *Base, SourceLocation LLoc, |
| 3052 | Expr *Idx, SourceLocation RLoc) { |
| 3053 | Expr *LHSExp = Base; |
| 3054 | Expr *RHSExp = Idx; |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 3055 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3056 | // Perform default conversions. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3057 | if (!LHSExp->getType()->getAs<VectorType>()) { |
| 3058 | ExprResult Result = DefaultFunctionArrayLvalueConversion(LHSExp); |
| 3059 | if (Result.isInvalid()) |
| 3060 | return ExprError(); |
| 3061 | LHSExp = Result.take(); |
| 3062 | } |
| 3063 | ExprResult Result = DefaultFunctionArrayLvalueConversion(RHSExp); |
| 3064 | if (Result.isInvalid()) |
| 3065 | return ExprError(); |
| 3066 | RHSExp = Result.take(); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3067 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3068 | QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3069 | ExprValueKind VK = VK_LValue; |
| 3070 | ExprObjectKind OK = OK_Ordinary; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3071 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3072 | // C99 6.5.2.1p2: the expression e1[e2] is by definition precisely equivalent |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 3073 | // to the expression *((e1)+(e2)). This means the array "Base" may actually be |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3074 | // in the subscript position. As a result, we need to derive the array base |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3075 | // and index from the expression types. |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3076 | Expr *BaseExpr, *IndexExpr; |
| 3077 | QualType ResultType; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3078 | if (LHSTy->isDependentType() || RHSTy->isDependentType()) { |
| 3079 | BaseExpr = LHSExp; |
| 3080 | IndexExpr = RHSExp; |
| 3081 | ResultType = Context.DependentTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3082 | } else if (const PointerType *PTy = LHSTy->getAs<PointerType>()) { |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3083 | BaseExpr = LHSExp; |
| 3084 | IndexExpr = RHSExp; |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3085 | ResultType = PTy->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3086 | } else if (const PointerType *PTy = RHSTy->getAs<PointerType>()) { |
Chris Lattner | 7a2e047 | 2007-07-16 00:23:25 +0000 | [diff] [blame] | 3087 | // Handle the uncommon case of "123[Ptr]". |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3088 | BaseExpr = RHSExp; |
| 3089 | IndexExpr = LHSExp; |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3090 | ResultType = PTy->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3091 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3092 | LHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3093 | BaseExpr = LHSExp; |
| 3094 | IndexExpr = RHSExp; |
| 3095 | ResultType = PTy->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3096 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3097 | RHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3098 | // Handle the uncommon case of "123[Ptr]". |
| 3099 | BaseExpr = RHSExp; |
| 3100 | IndexExpr = LHSExp; |
| 3101 | ResultType = PTy->getPointeeType(); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3102 | } else if (const VectorType *VTy = LHSTy->getAs<VectorType>()) { |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 3103 | BaseExpr = LHSExp; // vectors: V[123] |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3104 | IndexExpr = RHSExp; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3105 | VK = LHSExp->getValueKind(); |
| 3106 | if (VK != VK_RValue) |
| 3107 | OK = OK_VectorComponent; |
Nate Begeman | 334a802 | 2009-01-18 00:45:31 +0000 | [diff] [blame] | 3108 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3109 | // FIXME: need to deal with const... |
| 3110 | ResultType = VTy->getElementType(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3111 | } else if (LHSTy->isArrayType()) { |
| 3112 | // If we see an array that wasn't promoted by |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 3113 | // DefaultFunctionArrayLvalueConversion, it must be an array that |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3114 | // wasn't promoted because of the C90 rule that doesn't |
| 3115 | // allow promoting non-lvalue arrays. Warn, then |
| 3116 | // force the promotion here. |
| 3117 | Diag(LHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 3118 | LHSExp->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3119 | LHSExp = ImpCastExprToType(LHSExp, Context.getArrayDecayedType(LHSTy), |
| 3120 | CK_ArrayToPointerDecay).take(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3121 | LHSTy = LHSExp->getType(); |
| 3122 | |
| 3123 | BaseExpr = LHSExp; |
| 3124 | IndexExpr = RHSExp; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3125 | ResultType = LHSTy->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3126 | } else if (RHSTy->isArrayType()) { |
| 3127 | // Same as previous, except for 123[f().a] case |
| 3128 | Diag(RHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 3129 | RHSExp->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3130 | RHSExp = ImpCastExprToType(RHSExp, Context.getArrayDecayedType(RHSTy), |
| 3131 | CK_ArrayToPointerDecay).take(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3132 | RHSTy = RHSExp->getType(); |
| 3133 | |
| 3134 | BaseExpr = RHSExp; |
| 3135 | IndexExpr = LHSExp; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3136 | ResultType = RHSTy->getAs<PointerType>()->getPointeeType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3137 | } else { |
Chris Lattner | 338395d | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 3138 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_value) |
| 3139 | << LHSExp->getSourceRange() << RHSExp->getSourceRange()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3140 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3141 | // C99 6.5.2.1p1 |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 3142 | if (!IndexExpr->getType()->isIntegerType() && !IndexExpr->isTypeDependent()) |
Chris Lattner | 338395d | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 3143 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_not_integer) |
| 3144 | << IndexExpr->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3145 | |
Daniel Dunbar | 7e88a60 | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 3146 | if ((IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_S) || |
Sam Weinig | 0f9a5b5 | 2009-09-14 20:14:57 +0000 | [diff] [blame] | 3147 | IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U)) |
| 3148 | && !IndexExpr->isTypeDependent()) |
Sam Weinig | 76e2b71 | 2009-09-14 01:58:58 +0000 | [diff] [blame] | 3149 | Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange(); |
| 3150 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3151 | // C99 6.5.2.1p1: "shall have type "pointer to *object* type". Similarly, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3152 | // C++ [expr.sub]p1: The type "T" shall be a completely-defined object |
| 3153 | // type. Note that Functions are not objects, and that (in C99 parlance) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3154 | // incomplete types are not object types. |
| 3155 | if (ResultType->isFunctionType()) { |
| 3156 | Diag(BaseExpr->getLocStart(), diag::err_subscript_function_type) |
| 3157 | << ResultType << BaseExpr->getSourceRange(); |
| 3158 | return ExprError(); |
| 3159 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3160 | |
Abramo Bagnara | 4635845 | 2010-09-13 06:50:07 +0000 | [diff] [blame] | 3161 | if (ResultType->isVoidType() && !getLangOptions().CPlusPlus) { |
| 3162 | // GNU extension: subscripting on pointer to void |
Chandler Carruth | 6628969 | 2011-06-27 16:32:27 +0000 | [diff] [blame] | 3163 | Diag(LLoc, diag::ext_gnu_subscript_void_type) |
| 3164 | << BaseExpr->getSourceRange(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3165 | |
| 3166 | // C forbids expressions of unqualified void type from being l-values. |
| 3167 | // See IsCForbiddenLValueType. |
| 3168 | if (!ResultType.hasQualifiers()) VK = VK_RValue; |
Abramo Bagnara | 4635845 | 2010-09-13 06:50:07 +0000 | [diff] [blame] | 3169 | } else if (!ResultType->isDependentType() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3170 | RequireCompleteType(LLoc, ResultType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3171 | PDiag(diag::err_subscript_incomplete_type) |
| 3172 | << BaseExpr->getSourceRange())) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3173 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3174 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3175 | // Diagnose bad cases where we step over interface counts. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3176 | if (ResultType->isObjCObjectType() && LangOpts.ObjCNonFragileABI) { |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3177 | Diag(LLoc, diag::err_subscript_nonfragile_interface) |
| 3178 | << ResultType << BaseExpr->getSourceRange(); |
| 3179 | return ExprError(); |
| 3180 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3181 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3182 | assert(VK == VK_RValue || LangOpts.CPlusPlus || |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 3183 | !ResultType.isCForbiddenLValueType()); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3184 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3185 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3186 | ResultType, VK, OK, RLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3187 | } |
| 3188 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3189 | ExprResult Sema::BuildCXXDefaultArgExpr(SourceLocation CallLoc, |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 3190 | FunctionDecl *FD, |
| 3191 | ParmVarDecl *Param) { |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3192 | if (Param->hasUnparsedDefaultArg()) { |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3193 | Diag(CallLoc, |
Nico Weber | 15d5c83 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 3194 | diag::err_use_of_default_argument_to_function_declared_later) << |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3195 | FD << cast<CXXRecordDecl>(FD->getDeclContext())->getDeclName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3196 | Diag(UnparsedDefaultArgLocs[Param], |
Nico Weber | 15d5c83 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 3197 | diag::note_default_argument_declared_here); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3198 | return ExprError(); |
| 3199 | } |
| 3200 | |
| 3201 | if (Param->hasUninstantiatedDefaultArg()) { |
| 3202 | Expr *UninstExpr = Param->getUninstantiatedDefaultArg(); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3203 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3204 | // Instantiate the expression. |
| 3205 | MultiLevelTemplateArgumentList ArgList |
| 3206 | = getTemplateInstantiationArgs(FD, 0, /*RelativeToPrimary=*/true); |
Anders Carlsson | 25cae7f | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 3207 | |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 3208 | std::pair<const TemplateArgument *, unsigned> Innermost |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3209 | = ArgList.getInnermost(); |
| 3210 | InstantiatingTemplate Inst(*this, CallLoc, Param, Innermost.first, |
| 3211 | Innermost.second); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3212 | |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 3213 | ExprResult Result; |
| 3214 | { |
| 3215 | // C++ [dcl.fct.default]p5: |
| 3216 | // The names in the [default argument] expression are bound, and |
| 3217 | // the semantic constraints are checked, at the point where the |
| 3218 | // default argument expression appears. |
Nico Weber | 15d5c83 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 3219 | ContextRAII SavedContext(*this, FD); |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 3220 | Result = SubstExpr(UninstExpr, ArgList); |
| 3221 | } |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3222 | if (Result.isInvalid()) |
| 3223 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3224 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3225 | // Check the expression as an initializer for the parameter. |
| 3226 | InitializedEntity Entity |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 3227 | = InitializedEntity::InitializeParameter(Context, Param); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3228 | InitializationKind Kind |
| 3229 | = InitializationKind::CreateCopy(Param->getLocation(), |
| 3230 | /*FIXME:EqualLoc*/UninstExpr->getSourceRange().getBegin()); |
| 3231 | Expr *ResultE = Result.takeAs<Expr>(); |
Douglas Gregor | 65222e8 | 2009-12-23 18:19:08 +0000 | [diff] [blame] | 3232 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3233 | InitializationSequence InitSeq(*this, Entity, Kind, &ResultE, 1); |
| 3234 | Result = InitSeq.Perform(*this, Entity, Kind, |
| 3235 | MultiExprArg(*this, &ResultE, 1)); |
| 3236 | if (Result.isInvalid()) |
| 3237 | return ExprError(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3238 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3239 | // Build the default argument expression. |
| 3240 | return Owned(CXXDefaultArgExpr::Create(Context, CallLoc, Param, |
| 3241 | Result.takeAs<Expr>())); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3242 | } |
| 3243 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3244 | // If the default expression creates temporaries, we need to |
| 3245 | // push them to the current stack of expression temporaries so they'll |
| 3246 | // be properly destroyed. |
| 3247 | // FIXME: We should really be rebuilding the default argument with new |
| 3248 | // bound temporaries; see the comment in PR5810. |
Douglas Gregor | 5833b0b | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 3249 | for (unsigned i = 0, e = Param->getNumDefaultArgTemporaries(); i != e; ++i) { |
| 3250 | CXXTemporary *Temporary = Param->getDefaultArgTemporary(i); |
| 3251 | MarkDeclarationReferenced(Param->getDefaultArg()->getLocStart(), |
| 3252 | const_cast<CXXDestructorDecl*>(Temporary->getDestructor())); |
| 3253 | ExprTemporaries.push_back(Temporary); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3254 | ExprNeedsCleanups = true; |
Douglas Gregor | 5833b0b | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 3255 | } |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3256 | |
| 3257 | // We already type-checked the argument, so we know it works. |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 3258 | // Just mark all of the declarations in this potentially-evaluated expression |
| 3259 | // as being "referenced". |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3260 | MarkDeclarationsReferencedInExpr(Param->getDefaultArg()); |
Douglas Gregor | 036aed1 | 2009-12-23 23:03:06 +0000 | [diff] [blame] | 3261 | return Owned(CXXDefaultArgExpr::Create(Context, CallLoc, Param)); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3262 | } |
| 3263 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3264 | /// ConvertArgumentsForCall - Converts the arguments specified in |
| 3265 | /// Args/NumArgs to the parameter types of the function FDecl with |
| 3266 | /// function prototype Proto. Call is the call expression itself, and |
| 3267 | /// Fn is the function expression. For a C++ member function, this |
| 3268 | /// routine does not attempt to convert the object argument. Returns |
| 3269 | /// true if the call is ill-formed. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3270 | bool |
| 3271 | Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3272 | FunctionDecl *FDecl, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3273 | const FunctionProtoType *Proto, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3274 | Expr **Args, unsigned NumArgs, |
| 3275 | SourceLocation RParenLoc) { |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 3276 | // Bail out early if calling a builtin with custom typechecking. |
| 3277 | // We don't need to do this in the |
| 3278 | if (FDecl) |
| 3279 | if (unsigned ID = FDecl->getBuiltinID()) |
| 3280 | if (Context.BuiltinInfo.hasCustomTypechecking(ID)) |
| 3281 | return false; |
| 3282 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3283 | // C99 6.5.2.2p7 - the arguments are implicitly converted, as if by |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3284 | // assignment, to the types of the corresponding parameter, ... |
| 3285 | unsigned NumArgsInProto = Proto->getNumArgs(); |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 3286 | bool Invalid = false; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3287 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3288 | // If too few arguments are available (and we don't have default |
| 3289 | // arguments for the remaining parameters), don't make the call. |
| 3290 | if (NumArgs < NumArgsInProto) { |
Peter Collingbourne | 9aab148 | 2011-07-29 00:24:42 +0000 | [diff] [blame] | 3291 | if (!FDecl || NumArgs < FDecl->getMinRequiredArguments()) { |
| 3292 | Diag(RParenLoc, diag::err_typecheck_call_too_few_args) |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 3293 | << Fn->getType()->isBlockPointerType() |
Eric Christopher | d77b9a2 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 3294 | << NumArgsInProto << NumArgs << Fn->getSourceRange(); |
Peter Collingbourne | 9aab148 | 2011-07-29 00:24:42 +0000 | [diff] [blame] | 3295 | |
| 3296 | // Emit the location of the prototype. |
| 3297 | if (FDecl && !FDecl->getBuiltinID()) |
| 3298 | Diag(FDecl->getLocStart(), diag::note_callee_decl) |
| 3299 | << FDecl; |
| 3300 | |
| 3301 | return true; |
| 3302 | } |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3303 | Call->setNumArgs(Context, NumArgsInProto); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3304 | } |
| 3305 | |
| 3306 | // If too many are passed and not variadic, error on the extras and drop |
| 3307 | // them. |
| 3308 | if (NumArgs > NumArgsInProto) { |
| 3309 | if (!Proto->isVariadic()) { |
| 3310 | Diag(Args[NumArgsInProto]->getLocStart(), |
| 3311 | diag::err_typecheck_call_too_many_args) |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 3312 | << Fn->getType()->isBlockPointerType() |
Eric Christopher | ccfa963 | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 3313 | << NumArgsInProto << NumArgs << Fn->getSourceRange() |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3314 | << SourceRange(Args[NumArgsInProto]->getLocStart(), |
| 3315 | Args[NumArgs-1]->getLocEnd()); |
Ted Kremenek | 5862f0e | 2011-04-04 17:22:27 +0000 | [diff] [blame] | 3316 | |
| 3317 | // Emit the location of the prototype. |
| 3318 | if (FDecl && !FDecl->getBuiltinID()) |
Peter Collingbourne | 9aab148 | 2011-07-29 00:24:42 +0000 | [diff] [blame] | 3319 | Diag(FDecl->getLocStart(), diag::note_callee_decl) |
| 3320 | << FDecl; |
Ted Kremenek | 5862f0e | 2011-04-04 17:22:27 +0000 | [diff] [blame] | 3321 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3322 | // This deletes the extra arguments. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3323 | Call->setNumArgs(Context, NumArgsInProto); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3324 | return true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3325 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3326 | } |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3327 | SmallVector<Expr *, 8> AllArgs; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3328 | VariadicCallType CallType = |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 3329 | Proto->isVariadic() ? VariadicFunction : VariadicDoesNotApply; |
| 3330 | if (Fn->getType()->isBlockPointerType()) |
| 3331 | CallType = VariadicBlock; // Block |
| 3332 | else if (isa<MemberExpr>(Fn)) |
| 3333 | CallType = VariadicMethod; |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3334 | Invalid = GatherArgumentsForCall(Call->getSourceRange().getBegin(), FDecl, |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 3335 | Proto, 0, Args, NumArgs, AllArgs, CallType); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3336 | if (Invalid) |
| 3337 | return true; |
| 3338 | unsigned TotalNumArgs = AllArgs.size(); |
| 3339 | for (unsigned i = 0; i < TotalNumArgs; ++i) |
| 3340 | Call->setArg(i, AllArgs[i]); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3341 | |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3342 | return false; |
| 3343 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3344 | |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3345 | bool Sema::GatherArgumentsForCall(SourceLocation CallLoc, |
| 3346 | FunctionDecl *FDecl, |
| 3347 | const FunctionProtoType *Proto, |
| 3348 | unsigned FirstProtoArg, |
| 3349 | Expr **Args, unsigned NumArgs, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3350 | SmallVector<Expr *, 8> &AllArgs, |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 3351 | VariadicCallType CallType) { |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3352 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 3353 | unsigned NumArgsToCheck = NumArgs; |
| 3354 | bool Invalid = false; |
| 3355 | if (NumArgs != NumArgsInProto) |
| 3356 | // Use default arguments for missing arguments |
| 3357 | NumArgsToCheck = NumArgsInProto; |
| 3358 | unsigned ArgIx = 0; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3359 | // Continue to check argument types (even if we have too few/many args). |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3360 | for (unsigned i = FirstProtoArg; i != NumArgsToCheck; i++) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3361 | QualType ProtoArgType = Proto->getArgType(i); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3362 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3363 | Expr *Arg; |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3364 | if (ArgIx < NumArgs) { |
| 3365 | Arg = Args[ArgIx++]; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3366 | |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3367 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 3368 | ProtoArgType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3369 | PDiag(diag::err_call_incomplete_argument) |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3370 | << Arg->getSourceRange())) |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3371 | return true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3372 | |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3373 | // Pass the argument |
| 3374 | ParmVarDecl *Param = 0; |
| 3375 | if (FDecl && i < FDecl->getNumParams()) |
| 3376 | Param = FDecl->getParamDecl(i); |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 3377 | |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3378 | InitializedEntity Entity = |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 3379 | Param? InitializedEntity::InitializeParameter(Context, Param) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3380 | : InitializedEntity::InitializeParameter(Context, ProtoArgType, |
| 3381 | Proto->isArgConsumed(i)); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3382 | ExprResult ArgE = PerformCopyInitialization(Entity, |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3383 | SourceLocation(), |
| 3384 | Owned(Arg)); |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3385 | if (ArgE.isInvalid()) |
| 3386 | return true; |
| 3387 | |
| 3388 | Arg = ArgE.takeAs<Expr>(); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 3389 | } else { |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 3390 | ParmVarDecl *Param = FDecl->getParamDecl(i); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3391 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3392 | ExprResult ArgExpr = |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3393 | BuildCXXDefaultArgExpr(CallLoc, FDecl, Param); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3394 | if (ArgExpr.isInvalid()) |
| 3395 | return true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3396 | |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3397 | Arg = ArgExpr.takeAs<Expr>(); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 3398 | } |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame^] | 3399 | |
| 3400 | // Check for array bounds violations for each argument to the call. This |
| 3401 | // check only triggers warnings when the argument isn't a more complex Expr |
| 3402 | // with its own checking, such as a BinaryOperator. |
| 3403 | CheckArrayAccess(Arg); |
| 3404 | |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3405 | AllArgs.push_back(Arg); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3406 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3407 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3408 | // If this is a variadic call, handle args passed through "...". |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 3409 | if (CallType != VariadicDoesNotApply) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3410 | |
| 3411 | // Assume that extern "C" functions with variadic arguments that |
| 3412 | // return __unknown_anytype aren't *really* variadic. |
| 3413 | if (Proto->getResultType() == Context.UnknownAnyTy && |
| 3414 | FDecl && FDecl->isExternC()) { |
| 3415 | for (unsigned i = ArgIx; i != NumArgs; ++i) { |
| 3416 | ExprResult arg; |
| 3417 | if (isa<ExplicitCastExpr>(Args[i]->IgnoreParens())) |
| 3418 | arg = DefaultFunctionArrayLvalueConversion(Args[i]); |
| 3419 | else |
| 3420 | arg = DefaultVariadicArgumentPromotion(Args[i], CallType, FDecl); |
| 3421 | Invalid |= arg.isInvalid(); |
| 3422 | AllArgs.push_back(arg.take()); |
| 3423 | } |
| 3424 | |
| 3425 | // Otherwise do argument promotion, (C99 6.5.2.2p7). |
| 3426 | } else { |
| 3427 | for (unsigned i = ArgIx; i != NumArgs; ++i) { |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3428 | ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], CallType, |
| 3429 | FDecl); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3430 | Invalid |= Arg.isInvalid(); |
| 3431 | AllArgs.push_back(Arg.take()); |
| 3432 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3433 | } |
| 3434 | } |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 3435 | return Invalid; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3436 | } |
| 3437 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3438 | /// Given a function expression of unknown-any type, try to rebuild it |
| 3439 | /// to have a function type. |
| 3440 | static ExprResult rebuildUnknownAnyFunction(Sema &S, Expr *fn); |
| 3441 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3442 | /// ActOnCallExpr - Handle a call to Fn with the specified array of arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3443 | /// This provides the location of the left/right parens and a list of comma |
| 3444 | /// locations. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3445 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3446 | Sema::ActOnCallExpr(Scope *S, Expr *Fn, SourceLocation LParenLoc, |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3447 | MultiExprArg args, SourceLocation RParenLoc, |
| 3448 | Expr *ExecConfig) { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3449 | unsigned NumArgs = args.size(); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3450 | |
| 3451 | // Since this might be a postfix expression, get rid of ParenListExprs. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3452 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Fn); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3453 | if (Result.isInvalid()) return ExprError(); |
| 3454 | Fn = Result.take(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3455 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3456 | Expr **Args = args.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3457 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3458 | if (getLangOptions().CPlusPlus) { |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3459 | // If this is a pseudo-destructor expression, build the call immediately. |
| 3460 | if (isa<CXXPseudoDestructorExpr>(Fn)) { |
| 3461 | if (NumArgs > 0) { |
| 3462 | // Pseudo-destructor calls should not have any arguments. |
| 3463 | Diag(Fn->getLocStart(), diag::err_pseudo_dtor_call_with_args) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3464 | << FixItHint::CreateRemoval( |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3465 | SourceRange(Args[0]->getLocStart(), |
| 3466 | Args[NumArgs-1]->getLocEnd())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3467 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3468 | NumArgs = 0; |
| 3469 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3470 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3471 | return Owned(new (Context) CallExpr(Context, Fn, 0, 0, Context.VoidTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3472 | VK_RValue, RParenLoc)); |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3473 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3474 | |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3475 | // Determine whether this is a dependent call inside a C++ template, |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3476 | // in which case we won't do any semantic analysis now. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3477 | // FIXME: Will need to cache the results of name lookup (including ADL) in |
| 3478 | // Fn. |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3479 | bool Dependent = false; |
| 3480 | if (Fn->isTypeDependent()) |
| 3481 | Dependent = true; |
| 3482 | else if (Expr::hasAnyTypeDependentArguments(Args, NumArgs)) |
| 3483 | Dependent = true; |
| 3484 | |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3485 | if (Dependent) { |
| 3486 | if (ExecConfig) { |
| 3487 | return Owned(new (Context) CUDAKernelCallExpr( |
| 3488 | Context, Fn, cast<CallExpr>(ExecConfig), Args, NumArgs, |
| 3489 | Context.DependentTy, VK_RValue, RParenLoc)); |
| 3490 | } else { |
| 3491 | return Owned(new (Context) CallExpr(Context, Fn, Args, NumArgs, |
| 3492 | Context.DependentTy, VK_RValue, |
| 3493 | RParenLoc)); |
| 3494 | } |
| 3495 | } |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3496 | |
| 3497 | // Determine whether this is a call to an object (C++ [over.call.object]). |
| 3498 | if (Fn->getType()->isRecordType()) |
| 3499 | return Owned(BuildCallToObjectOfClassType(S, Fn, LParenLoc, Args, NumArgs, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 3500 | RParenLoc)); |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3501 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3502 | if (Fn->getType() == Context.UnknownAnyTy) { |
| 3503 | ExprResult result = rebuildUnknownAnyFunction(*this, Fn); |
| 3504 | if (result.isInvalid()) return ExprError(); |
| 3505 | Fn = result.take(); |
| 3506 | } |
| 3507 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3508 | if (Fn->getType() == Context.BoundMemberTy) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3509 | return BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 3510 | RParenLoc); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3511 | } |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3512 | } |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3513 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3514 | // Check for overloaded calls. This can happen even in C due to extensions. |
| 3515 | if (Fn->getType() == Context.OverloadTy) { |
| 3516 | OverloadExpr::FindResult find = OverloadExpr::find(Fn); |
| 3517 | |
| 3518 | // We aren't supposed to apply this logic if there's an '&' involved. |
| 3519 | if (!find.IsAddressOfOperand) { |
| 3520 | OverloadExpr *ovl = find.Expression; |
| 3521 | if (isa<UnresolvedLookupExpr>(ovl)) { |
| 3522 | UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(ovl); |
| 3523 | return BuildOverloadedCallExpr(S, Fn, ULE, LParenLoc, Args, NumArgs, |
| 3524 | RParenLoc, ExecConfig); |
| 3525 | } else { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3526 | return BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 3527 | RParenLoc); |
Anders Carlsson | 83ccfc3 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 3528 | } |
| 3529 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3530 | } |
| 3531 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3532 | // If we're directly calling a function, get the appropriate declaration. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3533 | |
Eli Friedman | efa42f7 | 2009-12-26 03:35:45 +0000 | [diff] [blame] | 3534 | Expr *NakedFn = Fn->IgnoreParens(); |
Douglas Gregor | ef9b149 | 2010-11-09 20:03:54 +0000 | [diff] [blame] | 3535 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3536 | NamedDecl *NDecl = 0; |
Douglas Gregor | d8f0ade | 2010-10-25 20:48:33 +0000 | [diff] [blame] | 3537 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(NakedFn)) |
| 3538 | if (UnOp->getOpcode() == UO_AddrOf) |
| 3539 | NakedFn = UnOp->getSubExpr()->IgnoreParens(); |
| 3540 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3541 | if (isa<DeclRefExpr>(NakedFn)) |
| 3542 | NDecl = cast<DeclRefExpr>(NakedFn)->getDecl(); |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3543 | else if (isa<MemberExpr>(NakedFn)) |
| 3544 | NDecl = cast<MemberExpr>(NakedFn)->getMemberDecl(); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3545 | |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3546 | return BuildResolvedCallExpr(Fn, NDecl, LParenLoc, Args, NumArgs, RParenLoc, |
| 3547 | ExecConfig); |
| 3548 | } |
| 3549 | |
| 3550 | ExprResult |
| 3551 | Sema::ActOnCUDAExecConfigExpr(Scope *S, SourceLocation LLLLoc, |
| 3552 | MultiExprArg execConfig, SourceLocation GGGLoc) { |
| 3553 | FunctionDecl *ConfigDecl = Context.getcudaConfigureCallDecl(); |
| 3554 | if (!ConfigDecl) |
| 3555 | return ExprError(Diag(LLLLoc, diag::err_undeclared_var_use) |
| 3556 | << "cudaConfigureCall"); |
| 3557 | QualType ConfigQTy = ConfigDecl->getType(); |
| 3558 | |
| 3559 | DeclRefExpr *ConfigDR = new (Context) DeclRefExpr( |
| 3560 | ConfigDecl, ConfigQTy, VK_LValue, LLLLoc); |
| 3561 | |
| 3562 | return ActOnCallExpr(S, ConfigDR, LLLLoc, execConfig, GGGLoc, 0); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3563 | } |
| 3564 | |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 3565 | /// ActOnAsTypeExpr - create a new asType (bitcast) from the arguments. |
| 3566 | /// |
| 3567 | /// __builtin_astype( value, dst type ) |
| 3568 | /// |
| 3569 | ExprResult Sema::ActOnAsTypeExpr(Expr *expr, ParsedType destty, |
| 3570 | SourceLocation BuiltinLoc, |
| 3571 | SourceLocation RParenLoc) { |
| 3572 | ExprValueKind VK = VK_RValue; |
| 3573 | ExprObjectKind OK = OK_Ordinary; |
| 3574 | QualType DstTy = GetTypeFromParser(destty); |
| 3575 | QualType SrcTy = expr->getType(); |
| 3576 | if (Context.getTypeSize(DstTy) != Context.getTypeSize(SrcTy)) |
| 3577 | return ExprError(Diag(BuiltinLoc, |
| 3578 | diag::err_invalid_astype_of_different_size) |
Peter Collingbourne | af9cddf | 2011-06-08 15:15:17 +0000 | [diff] [blame] | 3579 | << DstTy |
| 3580 | << SrcTy |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 3581 | << expr->getSourceRange()); |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3582 | return Owned(new (Context) AsTypeExpr(expr, DstTy, VK, OK, BuiltinLoc, |
| 3583 | RParenLoc)); |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 3584 | } |
| 3585 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3586 | /// BuildResolvedCallExpr - Build a call to a resolved expression, |
| 3587 | /// i.e. an expression not of \p OverloadTy. The expression should |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3588 | /// unary-convert to an expression of function-pointer or |
| 3589 | /// block-pointer type. |
| 3590 | /// |
| 3591 | /// \param NDecl the declaration being called, if available |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3592 | ExprResult |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3593 | Sema::BuildResolvedCallExpr(Expr *Fn, NamedDecl *NDecl, |
| 3594 | SourceLocation LParenLoc, |
| 3595 | Expr **Args, unsigned NumArgs, |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3596 | SourceLocation RParenLoc, |
| 3597 | Expr *Config) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3598 | FunctionDecl *FDecl = dyn_cast_or_null<FunctionDecl>(NDecl); |
| 3599 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 3600 | // Promote the function operand. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3601 | ExprResult Result = UsualUnaryConversions(Fn); |
| 3602 | if (Result.isInvalid()) |
| 3603 | return ExprError(); |
| 3604 | Fn = Result.take(); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 3605 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3606 | // Make the call expr early, before semantic checks. This guarantees cleanup |
| 3607 | // of arguments and function on error. |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3608 | CallExpr *TheCall; |
| 3609 | if (Config) { |
| 3610 | TheCall = new (Context) CUDAKernelCallExpr(Context, Fn, |
| 3611 | cast<CallExpr>(Config), |
| 3612 | Args, NumArgs, |
| 3613 | Context.BoolTy, |
| 3614 | VK_RValue, |
| 3615 | RParenLoc); |
| 3616 | } else { |
| 3617 | TheCall = new (Context) CallExpr(Context, Fn, |
| 3618 | Args, NumArgs, |
| 3619 | Context.BoolTy, |
| 3620 | VK_RValue, |
| 3621 | RParenLoc); |
| 3622 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3623 | |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 3624 | unsigned BuiltinID = (FDecl ? FDecl->getBuiltinID() : 0); |
| 3625 | |
| 3626 | // Bail out early if calling a builtin with custom typechecking. |
| 3627 | if (BuiltinID && Context.BuiltinInfo.hasCustomTypechecking(BuiltinID)) |
| 3628 | return CheckBuiltinFunctionCall(BuiltinID, TheCall); |
| 3629 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 3630 | retry: |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 3631 | const FunctionType *FuncT; |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 3632 | if (const PointerType *PT = Fn->getType()->getAs<PointerType>()) { |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 3633 | // C99 6.5.2.2p1 - "The expression that denotes the called function shall |
| 3634 | // have type pointer to function". |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3635 | FuncT = PT->getPointeeType()->getAs<FunctionType>(); |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 3636 | if (FuncT == 0) |
| 3637 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 3638 | << Fn->getType() << Fn->getSourceRange()); |
| 3639 | } else if (const BlockPointerType *BPT = |
| 3640 | Fn->getType()->getAs<BlockPointerType>()) { |
| 3641 | FuncT = BPT->getPointeeType()->castAs<FunctionType>(); |
| 3642 | } else { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 3643 | // Handle calls to expressions of unknown-any type. |
| 3644 | if (Fn->getType() == Context.UnknownAnyTy) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3645 | ExprResult rewrite = rebuildUnknownAnyFunction(*this, Fn); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 3646 | if (rewrite.isInvalid()) return ExprError(); |
| 3647 | Fn = rewrite.take(); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 3648 | TheCall->setCallee(Fn); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 3649 | goto retry; |
| 3650 | } |
| 3651 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3652 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 3653 | << Fn->getType() << Fn->getSourceRange()); |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 3654 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3655 | |
Peter Collingbourne | 0423fc6 | 2011-02-23 01:53:29 +0000 | [diff] [blame] | 3656 | if (getLangOptions().CUDA) { |
| 3657 | if (Config) { |
| 3658 | // CUDA: Kernel calls must be to global functions |
| 3659 | if (FDecl && !FDecl->hasAttr<CUDAGlobalAttr>()) |
| 3660 | return ExprError(Diag(LParenLoc,diag::err_kern_call_not_global_function) |
| 3661 | << FDecl->getName() << Fn->getSourceRange()); |
| 3662 | |
| 3663 | // CUDA: Kernel function must have 'void' return type |
| 3664 | if (!FuncT->getResultType()->isVoidType()) |
| 3665 | return ExprError(Diag(LParenLoc, diag::err_kern_type_not_void_return) |
| 3666 | << Fn->getType() << Fn->getSourceRange()); |
| 3667 | } |
| 3668 | } |
| 3669 | |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3670 | // Check for a valid return type |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3671 | if (CheckCallReturnType(FuncT->getResultType(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3672 | Fn->getSourceRange().getBegin(), TheCall, |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 3673 | FDecl)) |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3674 | return ExprError(); |
| 3675 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3676 | // We know the result type of the call, set it. |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 3677 | TheCall->setType(FuncT->getCallResultType(Context)); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3678 | TheCall->setValueKind(Expr::getValueKindForType(FuncT->getResultType())); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3679 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3680 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FuncT)) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3681 | if (ConvertArgumentsForCall(TheCall, Fn, FDecl, Proto, Args, NumArgs, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3682 | RParenLoc)) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3683 | return ExprError(); |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3684 | } else { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3685 | assert(isa<FunctionNoProtoType>(FuncT) && "Unknown FunctionType!"); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3686 | |
Douglas Gregor | 74734d5 | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 3687 | if (FDecl) { |
| 3688 | // Check if we have too few/too many template arguments, based |
| 3689 | // on our knowledge of the function definition. |
| 3690 | const FunctionDecl *Def = 0; |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 3691 | if (FDecl->hasBody(Def) && NumArgs != Def->param_size()) { |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 3692 | const FunctionProtoType *Proto |
| 3693 | = Def->getType()->getAs<FunctionProtoType>(); |
| 3694 | if (!Proto || !(Proto->isVariadic() && NumArgs >= Def->param_size())) |
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 3695 | Diag(RParenLoc, diag::warn_call_wrong_number_of_arguments) |
| 3696 | << (NumArgs > Def->param_size()) << FDecl << Fn->getSourceRange(); |
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 3697 | } |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 3698 | |
| 3699 | // If the function we're calling isn't a function prototype, but we have |
| 3700 | // a function prototype from a prior declaratiom, use that prototype. |
| 3701 | if (!FDecl->hasPrototype()) |
| 3702 | Proto = FDecl->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | 74734d5 | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 3703 | } |
| 3704 | |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 3705 | // Promote the arguments (C99 6.5.2.2p6). |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3706 | for (unsigned i = 0; i != NumArgs; i++) { |
| 3707 | Expr *Arg = Args[i]; |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 3708 | |
| 3709 | if (Proto && i < Proto->getNumArgs()) { |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 3710 | InitializedEntity Entity |
| 3711 | = InitializedEntity::InitializeParameter(Context, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3712 | Proto->getArgType(i), |
| 3713 | Proto->isArgConsumed(i)); |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 3714 | ExprResult ArgE = PerformCopyInitialization(Entity, |
| 3715 | SourceLocation(), |
| 3716 | Owned(Arg)); |
| 3717 | if (ArgE.isInvalid()) |
| 3718 | return true; |
| 3719 | |
| 3720 | Arg = ArgE.takeAs<Expr>(); |
| 3721 | |
| 3722 | } else { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3723 | ExprResult ArgE = DefaultArgumentPromotion(Arg); |
| 3724 | |
| 3725 | if (ArgE.isInvalid()) |
| 3726 | return true; |
| 3727 | |
| 3728 | Arg = ArgE.takeAs<Expr>(); |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 3729 | } |
| 3730 | |
Douglas Gregor | 0700bbf | 2010-10-26 05:45:40 +0000 | [diff] [blame] | 3731 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 3732 | Arg->getType(), |
| 3733 | PDiag(diag::err_call_incomplete_argument) |
| 3734 | << Arg->getSourceRange())) |
| 3735 | return ExprError(); |
| 3736 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3737 | TheCall->setArg(i, Arg); |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 3738 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3739 | } |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3740 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3741 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(FDecl)) |
| 3742 | if (!Method->isStatic()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3743 | return ExprError(Diag(LParenLoc, diag::err_member_call_without_object) |
| 3744 | << Fn->getSourceRange()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3745 | |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 3746 | // Check for sentinels |
| 3747 | if (NDecl) |
| 3748 | DiagnoseSentinelCalls(NDecl, LParenLoc, Args, NumArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3749 | |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 3750 | // Do special checking on direct calls to functions. |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 3751 | if (FDecl) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3752 | if (CheckFunctionCall(FDecl, TheCall)) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 3753 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3754 | |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 3755 | if (BuiltinID) |
Fariborz Jahanian | 67aba81 | 2010-11-30 17:35:24 +0000 | [diff] [blame] | 3756 | return CheckBuiltinFunctionCall(BuiltinID, TheCall); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 3757 | } else if (NDecl) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3758 | if (CheckBlockCall(NDecl, TheCall)) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 3759 | return ExprError(); |
| 3760 | } |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 3761 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3762 | return MaybeBindToTemporary(TheCall); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3763 | } |
| 3764 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3765 | ExprResult |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3766 | Sema::ActOnCompoundLiteral(SourceLocation LParenLoc, ParsedType Ty, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3767 | SourceLocation RParenLoc, Expr *InitExpr) { |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3768 | assert((Ty != 0) && "ActOnCompoundLiteral(): missing type"); |
Steve Naroff | aff1edd | 2007-07-19 21:32:11 +0000 | [diff] [blame] | 3769 | // FIXME: put back this assert when initializers are worked out. |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3770 | //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression"); |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 3771 | |
| 3772 | TypeSourceInfo *TInfo; |
| 3773 | QualType literalType = GetTypeFromParser(Ty, &TInfo); |
| 3774 | if (!TInfo) |
| 3775 | TInfo = Context.getTrivialTypeSourceInfo(literalType); |
| 3776 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3777 | return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, InitExpr); |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 3778 | } |
| 3779 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3780 | ExprResult |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 3781 | Sema::BuildCompoundLiteralExpr(SourceLocation LParenLoc, TypeSourceInfo *TInfo, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3782 | SourceLocation RParenLoc, Expr *literalExpr) { |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 3783 | QualType literalType = TInfo->getType(); |
Anders Carlsson | d35c832 | 2007-12-05 07:24:19 +0000 | [diff] [blame] | 3784 | |
Eli Friedman | 6223c22 | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 3785 | if (literalType->isArrayType()) { |
Argyrios Kyrtzidis | e6fe9a2 | 2010-11-08 19:14:19 +0000 | [diff] [blame] | 3786 | if (RequireCompleteType(LParenLoc, Context.getBaseElementType(literalType), |
| 3787 | PDiag(diag::err_illegal_decl_array_incomplete_type) |
| 3788 | << SourceRange(LParenLoc, |
| 3789 | literalExpr->getSourceRange().getEnd()))) |
| 3790 | return ExprError(); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3791 | if (literalType->isVariableArrayType()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3792 | return ExprError(Diag(LParenLoc, diag::err_variable_object_no_init) |
| 3793 | << SourceRange(LParenLoc, literalExpr->getSourceRange().getEnd())); |
Douglas Gregor | 690dc7f | 2009-05-21 23:48:18 +0000 | [diff] [blame] | 3794 | } else if (!literalType->isDependentType() && |
| 3795 | RequireCompleteType(LParenLoc, literalType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3796 | PDiag(diag::err_typecheck_decl_incomplete_type) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3797 | << SourceRange(LParenLoc, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3798 | literalExpr->getSourceRange().getEnd()))) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3799 | return ExprError(); |
Eli Friedman | 6223c22 | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 3800 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3801 | InitializedEntity Entity |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3802 | = InitializedEntity::InitializeTemporary(literalType); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3803 | InitializationKind Kind |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3804 | = InitializationKind::CreateCStyleCast(LParenLoc, |
| 3805 | SourceRange(LParenLoc, RParenLoc)); |
Eli Friedman | 0854462 | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 3806 | InitializationSequence InitSeq(*this, Entity, Kind, &literalExpr, 1); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3807 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 3808 | MultiExprArg(*this, &literalExpr, 1), |
Eli Friedman | 0854462 | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 3809 | &literalType); |
| 3810 | if (Result.isInvalid()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3811 | return ExprError(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3812 | literalExpr = Result.get(); |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 3813 | |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 3814 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 3815 | if (isFileScope) { // 6.5.2.5p3 |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 3816 | if (CheckForConstantInitializer(literalExpr, literalType)) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3817 | return ExprError(); |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 3818 | } |
Eli Friedman | 0854462 | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 3819 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3820 | // In C, compound literals are l-values for some reason. |
| 3821 | ExprValueKind VK = getLangOptions().CPlusPlus ? VK_RValue : VK_LValue; |
| 3822 | |
Douglas Gregor | 751ec9b | 2011-06-17 04:59:12 +0000 | [diff] [blame] | 3823 | return MaybeBindToTemporary( |
| 3824 | new (Context) CompoundLiteralExpr(LParenLoc, TInfo, literalType, |
| 3825 | VK, literalExpr, isFileScope)); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 3826 | } |
| 3827 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3828 | ExprResult |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3829 | Sema::ActOnInitList(SourceLocation LBraceLoc, MultiExprArg initlist, |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3830 | SourceLocation RBraceLoc) { |
| 3831 | unsigned NumInit = initlist.size(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3832 | Expr **InitList = initlist.release(); |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 3833 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 3834 | // Semantic analysis for initializers is done by ActOnDeclarator() and |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3835 | // CheckInitializer() - it requires knowledge of the object being intialized. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3836 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 3837 | InitListExpr *E = new (Context) InitListExpr(Context, LBraceLoc, InitList, |
| 3838 | NumInit, RBraceLoc); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3839 | E->setType(Context.VoidTy); // FIXME: just a place holder for now. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3840 | return Owned(E); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 3841 | } |
| 3842 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3843 | /// Prepares for a scalar cast, performing all the necessary stages |
| 3844 | /// except the final cast and returning the kind required. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3845 | static CastKind PrepareScalarCast(Sema &S, ExprResult &Src, QualType DestTy) { |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3846 | // Both Src and Dest are scalar types, i.e. arithmetic or pointer. |
| 3847 | // Also, callers should have filtered out the invalid cases with |
| 3848 | // pointers. Everything else should be possible. |
| 3849 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3850 | QualType SrcTy = Src.get()->getType(); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3851 | if (S.Context.hasSameUnqualifiedType(SrcTy, DestTy)) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3852 | return CK_NoOp; |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3853 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3854 | switch (SrcTy->getScalarTypeKind()) { |
| 3855 | case Type::STK_MemberPointer: |
| 3856 | llvm_unreachable("member pointer type in C"); |
Abramo Bagnara | bb03f5d | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 3857 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3858 | case Type::STK_Pointer: |
| 3859 | switch (DestTy->getScalarTypeKind()) { |
| 3860 | case Type::STK_Pointer: |
| 3861 | return DestTy->isObjCObjectPointerType() ? |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3862 | CK_AnyPointerToObjCPointerCast : |
| 3863 | CK_BitCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3864 | case Type::STK_Bool: |
| 3865 | return CK_PointerToBoolean; |
| 3866 | case Type::STK_Integral: |
| 3867 | return CK_PointerToIntegral; |
| 3868 | case Type::STK_Floating: |
| 3869 | case Type::STK_FloatingComplex: |
| 3870 | case Type::STK_IntegralComplex: |
| 3871 | case Type::STK_MemberPointer: |
| 3872 | llvm_unreachable("illegal cast from pointer"); |
| 3873 | } |
| 3874 | break; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3875 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3876 | case Type::STK_Bool: // casting from bool is like casting from an integer |
| 3877 | case Type::STK_Integral: |
| 3878 | switch (DestTy->getScalarTypeKind()) { |
| 3879 | case Type::STK_Pointer: |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3880 | if (Src.get()->isNullPointerConstant(S.Context, |
| 3881 | Expr::NPC_ValueDependentIsNull)) |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 3882 | return CK_NullToPointer; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3883 | return CK_IntegralToPointer; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3884 | case Type::STK_Bool: |
| 3885 | return CK_IntegralToBoolean; |
| 3886 | case Type::STK_Integral: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3887 | return CK_IntegralCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3888 | case Type::STK_Floating: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3889 | return CK_IntegralToFloating; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3890 | case Type::STK_IntegralComplex: |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3891 | Src = S.ImpCastExprToType(Src.take(), |
| 3892 | DestTy->getAs<ComplexType>()->getElementType(), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3893 | CK_IntegralCast); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3894 | return CK_IntegralRealToComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3895 | case Type::STK_FloatingComplex: |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3896 | Src = S.ImpCastExprToType(Src.take(), |
| 3897 | DestTy->getAs<ComplexType>()->getElementType(), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3898 | CK_IntegralToFloating); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3899 | return CK_FloatingRealToComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3900 | case Type::STK_MemberPointer: |
| 3901 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3902 | } |
| 3903 | break; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3904 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3905 | case Type::STK_Floating: |
| 3906 | switch (DestTy->getScalarTypeKind()) { |
| 3907 | case Type::STK_Floating: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3908 | return CK_FloatingCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3909 | case Type::STK_Bool: |
| 3910 | return CK_FloatingToBoolean; |
| 3911 | case Type::STK_Integral: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3912 | return CK_FloatingToIntegral; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3913 | case Type::STK_FloatingComplex: |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3914 | Src = S.ImpCastExprToType(Src.take(), |
| 3915 | DestTy->getAs<ComplexType>()->getElementType(), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3916 | CK_FloatingCast); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3917 | return CK_FloatingRealToComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3918 | case Type::STK_IntegralComplex: |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3919 | Src = S.ImpCastExprToType(Src.take(), |
| 3920 | DestTy->getAs<ComplexType>()->getElementType(), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3921 | CK_FloatingToIntegral); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3922 | return CK_IntegralRealToComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3923 | case Type::STK_Pointer: |
| 3924 | llvm_unreachable("valid float->pointer cast?"); |
| 3925 | case Type::STK_MemberPointer: |
| 3926 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3927 | } |
| 3928 | break; |
| 3929 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3930 | case Type::STK_FloatingComplex: |
| 3931 | switch (DestTy->getScalarTypeKind()) { |
| 3932 | case Type::STK_FloatingComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3933 | return CK_FloatingComplexCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3934 | case Type::STK_IntegralComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3935 | return CK_FloatingComplexToIntegralComplex; |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3936 | case Type::STK_Floating: { |
Abramo Bagnara | bb03f5d | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 3937 | QualType ET = SrcTy->getAs<ComplexType>()->getElementType(); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3938 | if (S.Context.hasSameType(ET, DestTy)) |
| 3939 | return CK_FloatingComplexToReal; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3940 | Src = S.ImpCastExprToType(Src.take(), ET, CK_FloatingComplexToReal); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3941 | return CK_FloatingCast; |
| 3942 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3943 | case Type::STK_Bool: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3944 | return CK_FloatingComplexToBoolean; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3945 | case Type::STK_Integral: |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3946 | Src = S.ImpCastExprToType(Src.take(), |
| 3947 | SrcTy->getAs<ComplexType>()->getElementType(), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3948 | CK_FloatingComplexToReal); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3949 | return CK_FloatingToIntegral; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3950 | case Type::STK_Pointer: |
| 3951 | llvm_unreachable("valid complex float->pointer cast?"); |
| 3952 | case Type::STK_MemberPointer: |
| 3953 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3954 | } |
| 3955 | break; |
| 3956 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3957 | case Type::STK_IntegralComplex: |
| 3958 | switch (DestTy->getScalarTypeKind()) { |
| 3959 | case Type::STK_FloatingComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3960 | return CK_IntegralComplexToFloatingComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3961 | case Type::STK_IntegralComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3962 | return CK_IntegralComplexCast; |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3963 | case Type::STK_Integral: { |
Abramo Bagnara | bb03f5d | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 3964 | QualType ET = SrcTy->getAs<ComplexType>()->getElementType(); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3965 | if (S.Context.hasSameType(ET, DestTy)) |
| 3966 | return CK_IntegralComplexToReal; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3967 | Src = S.ImpCastExprToType(Src.take(), ET, CK_IntegralComplexToReal); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3968 | return CK_IntegralCast; |
| 3969 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3970 | case Type::STK_Bool: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3971 | return CK_IntegralComplexToBoolean; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3972 | case Type::STK_Floating: |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3973 | Src = S.ImpCastExprToType(Src.take(), |
| 3974 | SrcTy->getAs<ComplexType>()->getElementType(), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3975 | CK_IntegralComplexToReal); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3976 | return CK_IntegralToFloating; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3977 | case Type::STK_Pointer: |
| 3978 | llvm_unreachable("valid complex int->pointer cast?"); |
| 3979 | case Type::STK_MemberPointer: |
| 3980 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3981 | } |
| 3982 | break; |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3983 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3984 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3985 | llvm_unreachable("Unhandled scalar cast"); |
| 3986 | return CK_BitCast; |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3987 | } |
| 3988 | |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3989 | /// CheckCastTypes - Check type constraints for casting between types. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3990 | ExprResult Sema::CheckCastTypes(SourceLocation CastStartLoc, SourceRange TyR, |
| 3991 | QualType castType, Expr *castExpr, |
| 3992 | CastKind& Kind, ExprValueKind &VK, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3993 | CXXCastPath &BasePath, bool FunctionalStyle) { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 3994 | if (castExpr->getType() == Context.UnknownAnyTy) |
| 3995 | return checkUnknownAnyCast(TyR, castType, castExpr, Kind, VK, BasePath); |
| 3996 | |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 3997 | if (getLangOptions().CPlusPlus) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3998 | return CXXCheckCStyleCast(SourceRange(CastStartLoc, |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3999 | castExpr->getLocEnd()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4000 | castType, VK, castExpr, Kind, BasePath, |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 4001 | FunctionalStyle); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 4002 | |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 4003 | assert(!castExpr->getType()->isPlaceholderType()); |
| 4004 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4005 | // We only support r-value casts in C. |
| 4006 | VK = VK_RValue; |
| 4007 | |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 4008 | // C99 6.5.4p2: the cast type needs to be void or scalar and the expression |
| 4009 | // type needs to be scalar. |
| 4010 | if (castType->isVoidType()) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4011 | // We don't necessarily do lvalue-to-rvalue conversions on this. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4012 | ExprResult castExprRes = IgnoredValueConversions(castExpr); |
| 4013 | if (castExprRes.isInvalid()) |
| 4014 | return ExprError(); |
| 4015 | castExpr = castExprRes.take(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4016 | |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 4017 | // Cast to void allows any expr type. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4018 | Kind = CK_ToVoid; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4019 | return Owned(castExpr); |
Anders Carlsson | ebeaf20 | 2009-10-16 02:35:04 +0000 | [diff] [blame] | 4020 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4021 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4022 | ExprResult castExprRes = DefaultFunctionArrayLvalueConversion(castExpr); |
| 4023 | if (castExprRes.isInvalid()) |
| 4024 | return ExprError(); |
| 4025 | castExpr = castExprRes.take(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 4026 | |
Eli Friedman | 8d43808 | 2010-07-17 20:43:49 +0000 | [diff] [blame] | 4027 | if (RequireCompleteType(TyR.getBegin(), castType, |
| 4028 | diag::err_typecheck_cast_to_incomplete)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4029 | return ExprError(); |
Eli Friedman | 8d43808 | 2010-07-17 20:43:49 +0000 | [diff] [blame] | 4030 | |
Anders Carlsson | ebeaf20 | 2009-10-16 02:35:04 +0000 | [diff] [blame] | 4031 | if (!castType->isScalarType() && !castType->isVectorType()) { |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 4032 | if (Context.hasSameUnqualifiedType(castType, castExpr->getType()) && |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 4033 | (castType->isStructureType() || castType->isUnionType())) { |
| 4034 | // GCC struct/union extension: allow cast to self. |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 4035 | // FIXME: Check that the cast destination type is complete. |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 4036 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_nonscalar) |
| 4037 | << castType << castExpr->getSourceRange(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4038 | Kind = CK_NoOp; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4039 | return Owned(castExpr); |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4040 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4041 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4042 | if (castType->isUnionType()) { |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 4043 | // GCC cast to union extension |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4044 | RecordDecl *RD = castType->getAs<RecordType>()->getDecl(); |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 4045 | RecordDecl::field_iterator Field, FieldEnd; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4046 | for (Field = RD->field_begin(), FieldEnd = RD->field_end(); |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 4047 | Field != FieldEnd; ++Field) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4048 | if (Context.hasSameUnqualifiedType(Field->getType(), |
Abramo Bagnara | 8c4bfe5 | 2010-10-07 21:20:44 +0000 | [diff] [blame] | 4049 | castExpr->getType()) && |
| 4050 | !Field->isUnnamedBitfield()) { |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 4051 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_to_union) |
| 4052 | << castExpr->getSourceRange(); |
| 4053 | break; |
| 4054 | } |
| 4055 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4056 | if (Field == FieldEnd) { |
| 4057 | Diag(TyR.getBegin(), diag::err_typecheck_cast_to_union_no_type) |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 4058 | << castExpr->getType() << castExpr->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4059 | return ExprError(); |
| 4060 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4061 | Kind = CK_ToUnion; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4062 | return Owned(castExpr); |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 4063 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4064 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4065 | // Reject any other conversions to non-scalar types. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4066 | Diag(TyR.getBegin(), diag::err_typecheck_cond_expect_scalar) |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4067 | << castType << castExpr->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4068 | return ExprError(); |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4069 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4070 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4071 | // The type we're casting to is known to be a scalar or vector. |
| 4072 | |
| 4073 | // Require the operand to be a scalar or vector. |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4074 | if (!castExpr->getType()->isScalarType() && |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4075 | !castExpr->getType()->isVectorType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4076 | Diag(castExpr->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4077 | diag::err_typecheck_expect_scalar_operand) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4078 | << castExpr->getType() << castExpr->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4079 | return ExprError(); |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4080 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4081 | |
| 4082 | if (castType->isExtVectorType()) |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 4083 | return CheckExtVectorCast(TyR, castType, castExpr, Kind); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4084 | |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 4085 | if (castType->isVectorType()) { |
| 4086 | if (castType->getAs<VectorType>()->getVectorKind() == |
| 4087 | VectorType::AltiVecVector && |
| 4088 | (castExpr->getType()->isIntegerType() || |
| 4089 | castExpr->getType()->isFloatingType())) { |
| 4090 | Kind = CK_VectorSplat; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4091 | return Owned(castExpr); |
| 4092 | } else if (CheckVectorCast(TyR, castType, castExpr->getType(), Kind)) { |
| 4093 | return ExprError(); |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 4094 | } else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4095 | return Owned(castExpr); |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 4096 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4097 | if (castExpr->getType()->isVectorType()) { |
| 4098 | if (CheckVectorCast(TyR, castExpr->getType(), castType, Kind)) |
| 4099 | return ExprError(); |
| 4100 | else |
| 4101 | return Owned(castExpr); |
| 4102 | } |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4103 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4104 | // The source and target types are both scalars, i.e. |
| 4105 | // - arithmetic types (fundamental, enum, and complex) |
| 4106 | // - all kinds of pointers |
| 4107 | // Note that member pointers were filtered out with C++, above. |
| 4108 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4109 | if (isa<ObjCSelectorExpr>(castExpr)) { |
| 4110 | Diag(castExpr->getLocStart(), diag::err_cast_selector_expr); |
| 4111 | return ExprError(); |
| 4112 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4113 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4114 | // If either type is a pointer, the other type has to be either an |
| 4115 | // integer or a pointer. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4116 | QualType castExprType = castExpr->getType(); |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4117 | if (!castType->isArithmeticType()) { |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 4118 | if (!castExprType->isIntegralType(Context) && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4119 | castExprType->isArithmeticType()) { |
| 4120 | Diag(castExpr->getLocStart(), |
| 4121 | diag::err_cast_pointer_from_non_pointer_int) |
Eli Friedman | 41826bb | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 4122 | << castExprType << castExpr->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4123 | return ExprError(); |
| 4124 | } |
Eli Friedman | 41826bb | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 4125 | } else if (!castExpr->getType()->isArithmeticType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4126 | if (!castType->isIntegralType(Context) && castType->isArithmeticType()) { |
| 4127 | Diag(castExpr->getLocStart(), diag::err_cast_pointer_to_non_pointer_int) |
Eli Friedman | 41826bb | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 4128 | << castType << castExpr->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4129 | return ExprError(); |
| 4130 | } |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 4131 | } |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 4132 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4133 | if (getLangOptions().ObjCAutoRefCount) { |
| 4134 | // Diagnose problems with Objective-C casts involving lifetime qualifiers. |
| 4135 | CheckObjCARCConversion(SourceRange(CastStartLoc, castExpr->getLocEnd()), |
| 4136 | castType, castExpr, CCK_CStyleCast); |
| 4137 | |
| 4138 | if (const PointerType *CastPtr = castType->getAs<PointerType>()) { |
| 4139 | if (const PointerType *ExprPtr = castExprType->getAs<PointerType>()) { |
| 4140 | Qualifiers CastQuals = CastPtr->getPointeeType().getQualifiers(); |
| 4141 | Qualifiers ExprQuals = ExprPtr->getPointeeType().getQualifiers(); |
| 4142 | if (CastPtr->getPointeeType()->isObjCLifetimeType() && |
| 4143 | ExprPtr->getPointeeType()->isObjCLifetimeType() && |
| 4144 | !CastQuals.compatiblyIncludesObjCLifetime(ExprQuals)) { |
| 4145 | Diag(castExpr->getLocStart(), |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 4146 | diag::err_typecheck_incompatible_ownership) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4147 | << castExprType << castType << AA_Casting |
| 4148 | << castExpr->getSourceRange(); |
| 4149 | |
| 4150 | return ExprError(); |
| 4151 | } |
| 4152 | } |
Fariborz Jahanian | 04e5a25 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 4153 | } |
Fariborz Jahanian | 7a084ec | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 4154 | else if (!CheckObjCARCUnavailableWeakConversion(castType, castExprType)) { |
| 4155 | Diag(castExpr->getLocStart(), |
Fariborz Jahanian | 82007c3 | 2011-07-08 17:41:42 +0000 | [diff] [blame] | 4156 | diag::err_arc_convesion_of_weak_unavailable) << 1 |
Fariborz Jahanian | 04e5a25 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 4157 | << castExprType << castType |
| 4158 | << castExpr->getSourceRange(); |
Fariborz Jahanian | 7a084ec | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 4159 | return ExprError(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4160 | } |
| 4161 | } |
| 4162 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4163 | castExprRes = Owned(castExpr); |
| 4164 | Kind = PrepareScalarCast(*this, castExprRes, castType); |
| 4165 | if (castExprRes.isInvalid()) |
| 4166 | return ExprError(); |
| 4167 | castExpr = castExprRes.take(); |
John McCall | b7f4ffe | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 4168 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4169 | if (Kind == CK_BitCast) |
John McCall | b7f4ffe | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 4170 | CheckCastAlign(castExpr, castType, TyR); |
| 4171 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4172 | return Owned(castExpr); |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 4173 | } |
| 4174 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4175 | bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4176 | CastKind &Kind) { |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4177 | assert(VectorTy->isVectorType() && "Not a vector type!"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4178 | |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4179 | if (Ty->isVectorType() || Ty->isIntegerType()) { |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 4180 | if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty)) |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4181 | return Diag(R.getBegin(), |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4182 | Ty->isVectorType() ? |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4183 | diag::err_invalid_conversion_between_vectors : |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4184 | diag::err_invalid_conversion_between_vector_and_integer) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4185 | << VectorTy << Ty << R; |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4186 | } else |
| 4187 | return Diag(R.getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4188 | diag::err_invalid_conversion_between_vector_and_scalar) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4189 | << VectorTy << Ty << R; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4190 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4191 | Kind = CK_BitCast; |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4192 | return false; |
| 4193 | } |
| 4194 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4195 | ExprResult Sema::CheckExtVectorCast(SourceRange R, QualType DestTy, |
| 4196 | Expr *CastExpr, CastKind &Kind) { |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4197 | assert(DestTy->isExtVectorType() && "Not an extended vector type!"); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4198 | |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 4199 | QualType SrcTy = CastExpr->getType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4200 | |
Nate Begeman | 9b10da6 | 2009-06-27 22:05:55 +0000 | [diff] [blame] | 4201 | // If SrcTy is a VectorType, the total size must match to explicitly cast to |
| 4202 | // an ExtVectorType. |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4203 | if (SrcTy->isVectorType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4204 | if (Context.getTypeSize(DestTy) != Context.getTypeSize(SrcTy)) { |
| 4205 | Diag(R.getBegin(),diag::err_invalid_conversion_between_ext_vectors) |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4206 | << DestTy << SrcTy << R; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4207 | return ExprError(); |
| 4208 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4209 | Kind = CK_BitCast; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4210 | return Owned(CastExpr); |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4211 | } |
| 4212 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4213 | // All non-pointer scalars can be cast to ExtVector type. The appropriate |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4214 | // conversion will take place first from scalar to elt type, and then |
| 4215 | // splat from elt type to vector. |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4216 | if (SrcTy->isPointerType()) |
| 4217 | return Diag(R.getBegin(), |
| 4218 | diag::err_invalid_conversion_between_vector_and_scalar) |
| 4219 | << DestTy << SrcTy << R; |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4220 | |
| 4221 | QualType DestElemTy = DestTy->getAs<ExtVectorType>()->getElementType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4222 | ExprResult CastExprRes = Owned(CastExpr); |
| 4223 | CastKind CK = PrepareScalarCast(*this, CastExprRes, DestElemTy); |
| 4224 | if (CastExprRes.isInvalid()) |
| 4225 | return ExprError(); |
| 4226 | CastExpr = ImpCastExprToType(CastExprRes.take(), DestElemTy, CK).take(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4227 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4228 | Kind = CK_VectorSplat; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4229 | return Owned(CastExpr); |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4230 | } |
| 4231 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4232 | ExprResult |
Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 4233 | Sema::ActOnCastExpr(Scope *S, SourceLocation LParenLoc, |
| 4234 | Declarator &D, ParsedType &Ty, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4235 | SourceLocation RParenLoc, Expr *castExpr) { |
Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 4236 | assert(!D.isInvalidType() && (castExpr != 0) && |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4237 | "ActOnCastExpr(): missing type or expr"); |
Steve Naroff | 16beff8 | 2007-07-16 23:25:18 +0000 | [diff] [blame] | 4238 | |
Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 4239 | TypeSourceInfo *castTInfo = GetTypeForDeclaratorCast(D, castExpr->getType()); |
| 4240 | if (D.isInvalidType()) |
| 4241 | return ExprError(); |
| 4242 | |
| 4243 | if (getLangOptions().CPlusPlus) { |
| 4244 | // Check that there are no default arguments (C++ only). |
| 4245 | CheckExtraCXXDefaultArguments(D); |
| 4246 | } |
| 4247 | |
| 4248 | QualType castType = castTInfo->getType(); |
| 4249 | Ty = CreateParsedType(castType, castTInfo); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4250 | |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4251 | bool isVectorLiteral = false; |
| 4252 | |
| 4253 | // Check for an altivec or OpenCL literal, |
| 4254 | // i.e. all the elements are integer constants. |
| 4255 | ParenExpr *PE = dyn_cast<ParenExpr>(castExpr); |
| 4256 | ParenListExpr *PLE = dyn_cast<ParenListExpr>(castExpr); |
| 4257 | if (getLangOptions().AltiVec && castType->isVectorType() && (PE || PLE)) { |
| 4258 | if (PLE && PLE->getNumExprs() == 0) { |
| 4259 | Diag(PLE->getExprLoc(), diag::err_altivec_empty_initializer); |
| 4260 | return ExprError(); |
| 4261 | } |
| 4262 | if (PE || PLE->getNumExprs() == 1) { |
| 4263 | Expr *E = (PE ? PE->getSubExpr() : PLE->getExpr(0)); |
| 4264 | if (!E->getType()->isVectorType()) |
| 4265 | isVectorLiteral = true; |
| 4266 | } |
| 4267 | else |
| 4268 | isVectorLiteral = true; |
| 4269 | } |
| 4270 | |
| 4271 | // If this is a vector initializer, '(' type ')' '(' init, ..., init ')' |
| 4272 | // then handle it as such. |
| 4273 | if (isVectorLiteral) |
| 4274 | return BuildVectorLiteral(LParenLoc, RParenLoc, castExpr, castTInfo); |
| 4275 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4276 | // If the Expr being casted is a ParenListExpr, handle it specially. |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4277 | // This is not an AltiVec-style cast, so turn the ParenListExpr into a |
| 4278 | // sequence of BinOp comma operators. |
| 4279 | if (isa<ParenListExpr>(castExpr)) { |
| 4280 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, castExpr); |
| 4281 | if (Result.isInvalid()) return ExprError(); |
| 4282 | castExpr = Result.take(); |
| 4283 | } |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 4284 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4285 | return BuildCStyleCastExpr(LParenLoc, castTInfo, RParenLoc, castExpr); |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 4286 | } |
| 4287 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4288 | ExprResult |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 4289 | Sema::BuildCStyleCastExpr(SourceLocation LParenLoc, TypeSourceInfo *Ty, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4290 | SourceLocation RParenLoc, Expr *castExpr) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4291 | CastKind Kind = CK_Invalid; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4292 | ExprValueKind VK = VK_RValue; |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4293 | CXXCastPath BasePath; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4294 | ExprResult CastResult = |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4295 | CheckCastTypes(LParenLoc, SourceRange(LParenLoc, RParenLoc), Ty->getType(), |
| 4296 | castExpr, Kind, VK, BasePath); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4297 | if (CastResult.isInvalid()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4298 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4299 | castExpr = CastResult.take(); |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 4300 | |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4301 | return Owned(CStyleCastExpr::Create( |
| 4302 | Context, Ty->getType().getNonLValueExprType(Context), VK, Kind, castExpr, |
| 4303 | &BasePath, Ty, LParenLoc, RParenLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4304 | } |
| 4305 | |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4306 | ExprResult Sema::BuildVectorLiteral(SourceLocation LParenLoc, |
| 4307 | SourceLocation RParenLoc, Expr *E, |
| 4308 | TypeSourceInfo *TInfo) { |
| 4309 | assert((isa<ParenListExpr>(E) || isa<ParenExpr>(E)) && |
| 4310 | "Expected paren or paren list expression"); |
| 4311 | |
| 4312 | Expr **exprs; |
| 4313 | unsigned numExprs; |
| 4314 | Expr *subExpr; |
| 4315 | if (ParenListExpr *PE = dyn_cast<ParenListExpr>(E)) { |
| 4316 | exprs = PE->getExprs(); |
| 4317 | numExprs = PE->getNumExprs(); |
| 4318 | } else { |
| 4319 | subExpr = cast<ParenExpr>(E)->getSubExpr(); |
| 4320 | exprs = &subExpr; |
| 4321 | numExprs = 1; |
| 4322 | } |
| 4323 | |
| 4324 | QualType Ty = TInfo->getType(); |
| 4325 | assert(Ty->isVectorType() && "Expected vector type"); |
| 4326 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4327 | SmallVector<Expr *, 8> initExprs; |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4328 | const VectorType *VTy = Ty->getAs<VectorType>(); |
| 4329 | unsigned numElems = Ty->getAs<VectorType>()->getNumElements(); |
| 4330 | |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4331 | // '(...)' form of vector initialization in AltiVec: the number of |
| 4332 | // initializers must be one or must match the size of the vector. |
| 4333 | // If a single value is specified in the initializer then it will be |
| 4334 | // replicated to all the components of the vector |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4335 | if (VTy->getVectorKind() == VectorType::AltiVecVector) { |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4336 | // The number of initializers must be one or must match the size of the |
| 4337 | // vector. If a single value is specified in the initializer then it will |
| 4338 | // be replicated to all the components of the vector |
| 4339 | if (numExprs == 1) { |
| 4340 | QualType ElemTy = Ty->getAs<VectorType>()->getElementType(); |
| 4341 | ExprResult Literal = Owned(exprs[0]); |
| 4342 | Literal = ImpCastExprToType(Literal.take(), ElemTy, |
| 4343 | PrepareScalarCast(*this, Literal, ElemTy)); |
| 4344 | return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Literal.take()); |
| 4345 | } |
| 4346 | else if (numExprs < numElems) { |
| 4347 | Diag(E->getExprLoc(), |
| 4348 | diag::err_incorrect_number_of_vector_initializers); |
| 4349 | return ExprError(); |
| 4350 | } |
| 4351 | else |
| 4352 | for (unsigned i = 0, e = numExprs; i != e; ++i) |
| 4353 | initExprs.push_back(exprs[i]); |
| 4354 | } |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4355 | else { |
| 4356 | // For OpenCL, when the number of initializers is a single value, |
| 4357 | // it will be replicated to all components of the vector. |
| 4358 | if (getLangOptions().OpenCL && |
| 4359 | VTy->getVectorKind() == VectorType::GenericVector && |
| 4360 | numExprs == 1) { |
| 4361 | QualType ElemTy = Ty->getAs<VectorType>()->getElementType(); |
| 4362 | ExprResult Literal = Owned(exprs[0]); |
| 4363 | Literal = ImpCastExprToType(Literal.take(), ElemTy, |
| 4364 | PrepareScalarCast(*this, Literal, ElemTy)); |
| 4365 | return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Literal.take()); |
| 4366 | } |
| 4367 | |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4368 | for (unsigned i = 0, e = numExprs; i != e; ++i) |
| 4369 | initExprs.push_back(exprs[i]); |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4370 | } |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4371 | // FIXME: This means that pretty-printing the final AST will produce curly |
| 4372 | // braces instead of the original commas. |
| 4373 | InitListExpr *initE = new (Context) InitListExpr(Context, LParenLoc, |
| 4374 | &initExprs[0], |
| 4375 | initExprs.size(), RParenLoc); |
| 4376 | initE->setType(Ty); |
| 4377 | return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, initE); |
| 4378 | } |
| 4379 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4380 | /// This is not an AltiVec-style cast, so turn the ParenListExpr into a sequence |
| 4381 | /// of comma binary operators. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4382 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4383 | Sema::MaybeConvertParenListExprToParenExpr(Scope *S, Expr *expr) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4384 | ParenListExpr *E = dyn_cast<ParenListExpr>(expr); |
| 4385 | if (!E) |
| 4386 | return Owned(expr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4387 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4388 | ExprResult Result(E->getExpr(0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4389 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4390 | for (unsigned i = 1, e = E->getNumExprs(); i != e && !Result.isInvalid(); ++i) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4391 | Result = ActOnBinOp(S, E->getExprLoc(), tok::comma, Result.get(), |
| 4392 | E->getExpr(i)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4393 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4394 | if (Result.isInvalid()) return ExprError(); |
| 4395 | |
| 4396 | return ActOnParenExpr(E->getLParenLoc(), E->getRParenLoc(), Result.get()); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4397 | } |
| 4398 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4399 | ExprResult Sema::ActOnParenOrParenListExpr(SourceLocation L, |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4400 | SourceLocation R, |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4401 | MultiExprArg Val) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4402 | unsigned nexprs = Val.size(); |
| 4403 | Expr **exprs = reinterpret_cast<Expr**>(Val.release()); |
Fariborz Jahanian | f88f7ab | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 4404 | assert((exprs != 0) && "ActOnParenOrParenListExpr() missing expr list"); |
| 4405 | Expr *expr; |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4406 | if (nexprs == 1) |
Fariborz Jahanian | f88f7ab | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 4407 | expr = new (Context) ParenExpr(L, R, exprs[0]); |
| 4408 | else |
Manuel Klimek | 0d9106f | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 4409 | expr = new (Context) ParenListExpr(Context, L, exprs, nexprs, R, |
| 4410 | exprs[nexprs-1]->getType()); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4411 | return Owned(expr); |
| 4412 | } |
| 4413 | |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4414 | /// \brief Emit a specialized diagnostic when one expression is a null pointer |
| 4415 | /// constant and the other is not a pointer. |
| 4416 | bool Sema::DiagnoseConditionalForNull(Expr *LHS, Expr *RHS, |
| 4417 | SourceLocation QuestionLoc) { |
| 4418 | Expr *NullExpr = LHS; |
| 4419 | Expr *NonPointerExpr = RHS; |
| 4420 | Expr::NullPointerConstantKind NullKind = |
| 4421 | NullExpr->isNullPointerConstant(Context, |
| 4422 | Expr::NPC_ValueDependentIsNotNull); |
| 4423 | |
| 4424 | if (NullKind == Expr::NPCK_NotNull) { |
| 4425 | NullExpr = RHS; |
| 4426 | NonPointerExpr = LHS; |
| 4427 | NullKind = |
| 4428 | NullExpr->isNullPointerConstant(Context, |
| 4429 | Expr::NPC_ValueDependentIsNotNull); |
| 4430 | } |
| 4431 | |
| 4432 | if (NullKind == Expr::NPCK_NotNull) |
| 4433 | return false; |
| 4434 | |
| 4435 | if (NullKind == Expr::NPCK_ZeroInteger) { |
| 4436 | // In this case, check to make sure that we got here from a "NULL" |
| 4437 | // string in the source code. |
| 4438 | NullExpr = NullExpr->IgnoreParenImpCasts(); |
John McCall | 834e3f6 | 2011-03-08 07:59:04 +0000 | [diff] [blame] | 4439 | SourceLocation loc = NullExpr->getExprLoc(); |
| 4440 | if (!findMacroSpelling(loc, "NULL")) |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4441 | return false; |
| 4442 | } |
| 4443 | |
| 4444 | int DiagType = (NullKind == Expr::NPCK_CXX0X_nullptr); |
| 4445 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands_null) |
| 4446 | << NonPointerExpr->getType() << DiagType |
| 4447 | << NonPointerExpr->getSourceRange(); |
| 4448 | return true; |
| 4449 | } |
| 4450 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4451 | /// Note that lhs is not null here, even if this is the gnu "x ?: y" extension. |
| 4452 | /// In that case, lhs = cond. |
Chris Lattner | a119a3b | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 4453 | /// C99 6.5.15 |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4454 | QualType Sema::CheckConditionalOperands(ExprResult &Cond, ExprResult &LHS, |
| 4455 | ExprResult &RHS, ExprValueKind &VK, |
| 4456 | ExprObjectKind &OK, |
Chris Lattner | a119a3b | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 4457 | SourceLocation QuestionLoc) { |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 4458 | |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 4459 | ExprResult lhsResult = CheckPlaceholderExpr(LHS.get()); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 4460 | if (!lhsResult.isUsable()) return QualType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4461 | LHS = move(lhsResult); |
Douglas Gregor | 7ad5d42 | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 4462 | |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 4463 | ExprResult rhsResult = CheckPlaceholderExpr(RHS.get()); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 4464 | if (!rhsResult.isUsable()) return QualType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4465 | RHS = move(rhsResult); |
Douglas Gregor | 7ad5d42 | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 4466 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4467 | // C++ is sufficiently different to merit its own checker. |
| 4468 | if (getLangOptions().CPlusPlus) |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4469 | return CXXCheckConditionalOperands(Cond, LHS, RHS, VK, OK, QuestionLoc); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4470 | |
| 4471 | VK = VK_RValue; |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4472 | OK = OK_Ordinary; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4473 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4474 | Cond = UsualUnaryConversions(Cond.take()); |
| 4475 | if (Cond.isInvalid()) |
| 4476 | return QualType(); |
| 4477 | LHS = UsualUnaryConversions(LHS.take()); |
| 4478 | if (LHS.isInvalid()) |
| 4479 | return QualType(); |
| 4480 | RHS = UsualUnaryConversions(RHS.take()); |
| 4481 | if (RHS.isInvalid()) |
| 4482 | return QualType(); |
| 4483 | |
| 4484 | QualType CondTy = Cond.get()->getType(); |
| 4485 | QualType LHSTy = LHS.get()->getType(); |
| 4486 | QualType RHSTy = RHS.get()->getType(); |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 4487 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4488 | // first, check the condition. |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4489 | if (!CondTy->isScalarType()) { // C99 6.5.15p2 |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 4490 | // OpenCL: Sec 6.3.i says the condition is allowed to be a vector or scalar. |
| 4491 | // Throw an error if its not either. |
| 4492 | if (getLangOptions().OpenCL) { |
| 4493 | if (!CondTy->isVectorType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4494 | Diag(Cond.get()->getLocStart(), |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 4495 | diag::err_typecheck_cond_expect_scalar_or_vector) |
| 4496 | << CondTy; |
| 4497 | return QualType(); |
| 4498 | } |
| 4499 | } |
| 4500 | else { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4501 | Diag(Cond.get()->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 4502 | << CondTy; |
| 4503 | return QualType(); |
| 4504 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4505 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4506 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4507 | // Now check the two expressions. |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4508 | if (LHSTy->isVectorType() || RHSTy->isVectorType()) |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 4509 | return CheckVectorOperands(LHS, RHS, QuestionLoc, /*isCompAssign*/false); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 4510 | |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 4511 | // OpenCL: If the condition is a vector, and both operands are scalar, |
| 4512 | // attempt to implicity convert them to the vector type to act like the |
| 4513 | // built in select. |
| 4514 | if (getLangOptions().OpenCL && CondTy->isVectorType()) { |
| 4515 | // Both operands should be of scalar type. |
| 4516 | if (!LHSTy->isScalarType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4517 | Diag(LHS.get()->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 4518 | << CondTy; |
| 4519 | return QualType(); |
| 4520 | } |
| 4521 | if (!RHSTy->isScalarType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4522 | Diag(RHS.get()->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 4523 | << CondTy; |
| 4524 | return QualType(); |
| 4525 | } |
| 4526 | // Implicity convert these scalars to the type of the condition. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4527 | LHS = ImpCastExprToType(LHS.take(), CondTy, CK_IntegralCast); |
| 4528 | RHS = ImpCastExprToType(RHS.take(), CondTy, CK_IntegralCast); |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 4529 | } |
| 4530 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4531 | // If both operands have arithmetic type, do the usual arithmetic conversions |
| 4532 | // to find a common type: C99 6.5.15p3,5. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4533 | if (LHSTy->isArithmeticType() && RHSTy->isArithmeticType()) { |
| 4534 | UsualArithmeticConversions(LHS, RHS); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4535 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 4536 | return QualType(); |
| 4537 | return LHS.get()->getType(); |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 4538 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4539 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4540 | // If both operands are the same structure or union type, the result is that |
| 4541 | // type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4542 | if (const RecordType *LHSRT = LHSTy->getAs<RecordType>()) { // C99 6.5.15p3 |
| 4543 | if (const RecordType *RHSRT = RHSTy->getAs<RecordType>()) |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 4544 | if (LHSRT->getDecl() == RHSRT->getDecl()) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4545 | // "If both the operands have structure or union type, the result has |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4546 | // that type." This implies that CV qualifiers are dropped. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4547 | return LHSTy.getUnqualifiedType(); |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 4548 | // FIXME: Type of conditional expression must be complete in C mode. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4549 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4550 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4551 | // C99 6.5.15p5: "If both operands have void type, the result has void type." |
Steve Naroff | e701c0a | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 4552 | // The following || allows only one side to be void (a GCC-ism). |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4553 | if (LHSTy->isVoidType() || RHSTy->isVoidType()) { |
| 4554 | if (!LHSTy->isVoidType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4555 | Diag(RHS.get()->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 4556 | << RHS.get()->getSourceRange(); |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4557 | if (!RHSTy->isVoidType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4558 | Diag(LHS.get()->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 4559 | << LHS.get()->getSourceRange(); |
| 4560 | LHS = ImpCastExprToType(LHS.take(), Context.VoidTy, CK_ToVoid); |
| 4561 | RHS = ImpCastExprToType(RHS.take(), Context.VoidTy, CK_ToVoid); |
Eli Friedman | 0e72401 | 2008-06-04 19:47:51 +0000 | [diff] [blame] | 4562 | return Context.VoidTy; |
Steve Naroff | e701c0a | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 4563 | } |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 4564 | // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has |
| 4565 | // the type of the other operand." |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4566 | if ((LHSTy->isAnyPointerType() || LHSTy->isBlockPointerType()) && |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4567 | RHS.get()->isNullPointerConstant(Context, |
| 4568 | Expr::NPC_ValueDependentIsNull)) { |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4569 | // promote the null to a pointer. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4570 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_NullToPointer); |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4571 | return LHSTy; |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 4572 | } |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 4573 | if ((RHSTy->isAnyPointerType() || RHSTy->isBlockPointerType()) && |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4574 | LHS.get()->isNullPointerConstant(Context, |
| 4575 | Expr::NPC_ValueDependentIsNull)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4576 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_NullToPointer); |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4577 | return RHSTy; |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 4578 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4579 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4580 | // All objective-c pointer type analysis is done here. |
| 4581 | QualType compositeType = FindCompositeObjCPointerType(LHS, RHS, |
| 4582 | QuestionLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4583 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 4584 | return QualType(); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4585 | if (!compositeType.isNull()) |
| 4586 | return compositeType; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4587 | |
| 4588 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4589 | // Handle block pointer types. |
| 4590 | if (LHSTy->isBlockPointerType() || RHSTy->isBlockPointerType()) { |
| 4591 | if (!LHSTy->isBlockPointerType() || !RHSTy->isBlockPointerType()) { |
| 4592 | if (LHSTy->isVoidPointerType() || RHSTy->isVoidPointerType()) { |
| 4593 | QualType destType = Context.getPointerType(Context.VoidTy); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4594 | LHS = ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
| 4595 | RHS = ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4596 | return destType; |
| 4597 | } |
| 4598 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4599 | << LHSTy << RHSTy << LHS.get()->getSourceRange() |
| 4600 | << RHS.get()->getSourceRange(); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4601 | return QualType(); |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4602 | } |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4603 | // We have 2 block pointer types. |
| 4604 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 4605 | // Two identical block pointer types are always compatible. |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4606 | return LHSTy; |
| 4607 | } |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4608 | // The block pointer types aren't identical, continue checking. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4609 | QualType lhptee = LHSTy->getAs<BlockPointerType>()->getPointeeType(); |
| 4610 | QualType rhptee = RHSTy->getAs<BlockPointerType>()->getPointeeType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4611 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4612 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 4613 | rhptee.getUnqualifiedType())) { |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4614 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4615 | << LHSTy << RHSTy << LHS.get()->getSourceRange() |
| 4616 | << RHS.get()->getSourceRange(); |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4617 | // In this situation, we assume void* type. No especially good |
| 4618 | // reason, but this is what gcc does, and we do have to pick |
| 4619 | // to get a consistent AST. |
| 4620 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4621 | LHS = ImpCastExprToType(LHS.take(), incompatTy, CK_BitCast); |
| 4622 | RHS = ImpCastExprToType(RHS.take(), incompatTy, CK_BitCast); |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 4623 | return incompatTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4624 | } |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4625 | // The block pointer types are compatible. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4626 | LHS = ImpCastExprToType(LHS.take(), LHSTy, CK_BitCast); |
| 4627 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Steve Naroff | 9158804 | 2009-04-08 17:05:15 +0000 | [diff] [blame] | 4628 | return LHSTy; |
| 4629 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4630 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4631 | // Check constraints for C object pointers types (C99 6.5.15p3,6). |
| 4632 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) { |
| 4633 | // get the "pointed to" types |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4634 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 4635 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4636 | |
| 4637 | // ignore qualifiers on void (C99 6.5.15p3, clause 6) |
| 4638 | if (lhptee->isVoidType() && rhptee->isIncompleteOrObjectType()) { |
| 4639 | // Figure out necessary qualifiers (C99 6.5.15p6) |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4640 | QualType destPointee |
| 4641 | = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4642 | QualType destType = Context.getPointerType(destPointee); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4643 | // Add qualifiers if necessary. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4644 | LHS = ImpCastExprToType(LHS.take(), destType, CK_NoOp); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4645 | // Promote to void*. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4646 | RHS = ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4647 | return destType; |
| 4648 | } |
| 4649 | if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) { |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 4650 | QualType destPointee |
| 4651 | = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4652 | QualType destType = Context.getPointerType(destPointee); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4653 | // Add qualifiers if necessary. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4654 | RHS = ImpCastExprToType(RHS.take(), destType, CK_NoOp); |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4655 | // Promote to void*. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4656 | LHS = ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4657 | return destType; |
| 4658 | } |
| 4659 | |
| 4660 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 4661 | // Two identical pointer types are always compatible. |
| 4662 | return LHSTy; |
| 4663 | } |
| 4664 | if (!Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 4665 | rhptee.getUnqualifiedType())) { |
| 4666 | Diag(QuestionLoc, diag::warn_typecheck_cond_incompatible_pointers) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4667 | << LHSTy << RHSTy << LHS.get()->getSourceRange() |
| 4668 | << RHS.get()->getSourceRange(); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4669 | // In this situation, we assume void* type. No especially good |
| 4670 | // reason, but this is what gcc does, and we do have to pick |
| 4671 | // to get a consistent AST. |
| 4672 | QualType incompatTy = Context.getPointerType(Context.VoidTy); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4673 | LHS = ImpCastExprToType(LHS.take(), incompatTy, CK_BitCast); |
| 4674 | RHS = ImpCastExprToType(RHS.take(), incompatTy, CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4675 | return incompatTy; |
| 4676 | } |
| 4677 | // The pointer types are compatible. |
| 4678 | // C99 6.5.15p6: If both operands are pointers to compatible types *or* to |
| 4679 | // differently qualified versions of compatible types, the result type is |
| 4680 | // a pointer to an appropriately qualified version of the *composite* |
| 4681 | // type. |
| 4682 | // FIXME: Need to calculate the composite type. |
| 4683 | // FIXME: Need to add qualifiers |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4684 | LHS = ImpCastExprToType(LHS.take(), LHSTy, CK_BitCast); |
| 4685 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4686 | return LHSTy; |
| 4687 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4688 | |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 4689 | // GCC compatibility: soften pointer/integer mismatch. Note that |
| 4690 | // null pointers have been filtered out by this point. |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4691 | if (RHSTy->isPointerType() && LHSTy->isIntegerType()) { |
| 4692 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4693 | << LHSTy << RHSTy << LHS.get()->getSourceRange() |
| 4694 | << RHS.get()->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4695 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_IntegralToPointer); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4696 | return RHSTy; |
| 4697 | } |
| 4698 | if (LHSTy->isPointerType() && RHSTy->isIntegerType()) { |
| 4699 | Diag(QuestionLoc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4700 | << LHSTy << RHSTy << LHS.get()->getSourceRange() |
| 4701 | << RHS.get()->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4702 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_IntegralToPointer); |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4703 | return LHSTy; |
| 4704 | } |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 4705 | |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4706 | // Emit a better diagnostic if one of the expressions is a null pointer |
| 4707 | // constant and the other is not a pointer type. In this case, the user most |
| 4708 | // likely forgot to take the address of the other expression. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4709 | if (DiagnoseConditionalForNull(LHS.get(), RHS.get(), QuestionLoc)) |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4710 | return QualType(); |
| 4711 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4712 | // Otherwise, the operands are not compatible. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4713 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4714 | << LHSTy << RHSTy << LHS.get()->getSourceRange() |
| 4715 | << RHS.get()->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4716 | return QualType(); |
| 4717 | } |
| 4718 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4719 | /// FindCompositeObjCPointerType - Helper method to find composite type of |
| 4720 | /// two objective-c pointer types of the two input expressions. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4721 | QualType Sema::FindCompositeObjCPointerType(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4722 | SourceLocation QuestionLoc) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4723 | QualType LHSTy = LHS.get()->getType(); |
| 4724 | QualType RHSTy = RHS.get()->getType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4725 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4726 | // Handle things like Class and struct objc_class*. Here we case the result |
| 4727 | // to the pseudo-builtin, because that will be implicitly cast back to the |
| 4728 | // redefinition type if an attempt is made to access its fields. |
| 4729 | if (LHSTy->isObjCClassType() && |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 4730 | (Context.hasSameType(RHSTy, Context.ObjCClassRedefinitionType))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4731 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4732 | return LHSTy; |
| 4733 | } |
| 4734 | if (RHSTy->isObjCClassType() && |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 4735 | (Context.hasSameType(LHSTy, Context.ObjCClassRedefinitionType))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4736 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4737 | return RHSTy; |
| 4738 | } |
| 4739 | // And the same for struct objc_object* / id |
| 4740 | if (LHSTy->isObjCIdType() && |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 4741 | (Context.hasSameType(RHSTy, Context.ObjCIdRedefinitionType))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4742 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4743 | return LHSTy; |
| 4744 | } |
| 4745 | if (RHSTy->isObjCIdType() && |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 4746 | (Context.hasSameType(LHSTy, Context.ObjCIdRedefinitionType))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4747 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4748 | return RHSTy; |
| 4749 | } |
| 4750 | // And the same for struct objc_selector* / SEL |
| 4751 | if (Context.isObjCSelType(LHSTy) && |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 4752 | (Context.hasSameType(RHSTy, Context.ObjCSelRedefinitionType))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4753 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4754 | return LHSTy; |
| 4755 | } |
| 4756 | if (Context.isObjCSelType(RHSTy) && |
John McCall | 49f4e1c | 2010-12-10 11:01:00 +0000 | [diff] [blame] | 4757 | (Context.hasSameType(LHSTy, Context.ObjCSelRedefinitionType))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4758 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4759 | return RHSTy; |
| 4760 | } |
| 4761 | // Check constraints for Objective-C object pointers types. |
| 4762 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isObjCObjectPointerType()) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4763 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4764 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 4765 | // Two identical object pointer types are always compatible. |
| 4766 | return LHSTy; |
| 4767 | } |
| 4768 | const ObjCObjectPointerType *LHSOPT = LHSTy->getAs<ObjCObjectPointerType>(); |
| 4769 | const ObjCObjectPointerType *RHSOPT = RHSTy->getAs<ObjCObjectPointerType>(); |
| 4770 | QualType compositeType = LHSTy; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4771 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4772 | // If both operands are interfaces and either operand can be |
| 4773 | // assigned to the other, use that type as the composite |
| 4774 | // type. This allows |
| 4775 | // xxx ? (A*) a : (B*) b |
| 4776 | // where B is a subclass of A. |
| 4777 | // |
| 4778 | // Additionally, as for assignment, if either type is 'id' |
| 4779 | // allow silent coercion. Finally, if the types are |
| 4780 | // incompatible then make sure to use 'id' as the composite |
| 4781 | // type so the result is acceptable for sending messages to. |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4782 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4783 | // FIXME: Consider unifying with 'areComparableObjCPointerTypes'. |
| 4784 | // It could return the composite type. |
| 4785 | if (Context.canAssignObjCInterfaces(LHSOPT, RHSOPT)) { |
| 4786 | compositeType = RHSOPT->isObjCBuiltinType() ? RHSTy : LHSTy; |
| 4787 | } else if (Context.canAssignObjCInterfaces(RHSOPT, LHSOPT)) { |
| 4788 | compositeType = LHSOPT->isObjCBuiltinType() ? LHSTy : RHSTy; |
| 4789 | } else if ((LHSTy->isObjCQualifiedIdType() || |
| 4790 | RHSTy->isObjCQualifiedIdType()) && |
| 4791 | Context.ObjCQualifiedIdTypesAreCompatible(LHSTy, RHSTy, true)) { |
| 4792 | // Need to handle "id<xx>" explicitly. |
| 4793 | // GCC allows qualified id and any Objective-C type to devolve to |
| 4794 | // id. Currently localizing to here until clear this should be |
| 4795 | // part of ObjCQualifiedIdTypesAreCompatible. |
| 4796 | compositeType = Context.getObjCIdType(); |
| 4797 | } else if (LHSTy->isObjCIdType() || RHSTy->isObjCIdType()) { |
| 4798 | compositeType = Context.getObjCIdType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4799 | } else if (!(compositeType = |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4800 | Context.areCommonBaseCompatible(LHSOPT, RHSOPT)).isNull()) |
| 4801 | ; |
| 4802 | else { |
| 4803 | Diag(QuestionLoc, diag::ext_typecheck_cond_incompatible_operands) |
| 4804 | << LHSTy << RHSTy |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4805 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4806 | QualType incompatTy = Context.getObjCIdType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4807 | LHS = ImpCastExprToType(LHS.take(), incompatTy, CK_BitCast); |
| 4808 | RHS = ImpCastExprToType(RHS.take(), incompatTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4809 | return incompatTy; |
| 4810 | } |
| 4811 | // The object pointer types are compatible. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4812 | LHS = ImpCastExprToType(LHS.take(), compositeType, CK_BitCast); |
| 4813 | RHS = ImpCastExprToType(RHS.take(), compositeType, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4814 | return compositeType; |
| 4815 | } |
| 4816 | // Check Objective-C object pointer types and 'void *' |
| 4817 | if (LHSTy->isVoidPointerType() && RHSTy->isObjCObjectPointerType()) { |
| 4818 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 4819 | QualType rhptee = RHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 4820 | QualType destPointee |
| 4821 | = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
| 4822 | QualType destType = Context.getPointerType(destPointee); |
| 4823 | // Add qualifiers if necessary. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4824 | LHS = ImpCastExprToType(LHS.take(), destType, CK_NoOp); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4825 | // Promote to void*. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4826 | RHS = ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4827 | return destType; |
| 4828 | } |
| 4829 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isVoidPointerType()) { |
| 4830 | QualType lhptee = LHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 4831 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
| 4832 | QualType destPointee |
| 4833 | = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
| 4834 | QualType destType = Context.getPointerType(destPointee); |
| 4835 | // Add qualifiers if necessary. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4836 | RHS = ImpCastExprToType(RHS.take(), destType, CK_NoOp); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4837 | // Promote to void*. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4838 | LHS = ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4839 | return destType; |
| 4840 | } |
| 4841 | return QualType(); |
| 4842 | } |
| 4843 | |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 4844 | /// SuggestParentheses - Emit a note with a fixit hint that wraps |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4845 | /// ParenRange in parentheses. |
| 4846 | static void SuggestParentheses(Sema &Self, SourceLocation Loc, |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 4847 | const PartialDiagnostic &Note, |
| 4848 | SourceRange ParenRange) { |
| 4849 | SourceLocation EndLoc = Self.PP.getLocForEndOfToken(ParenRange.getEnd()); |
| 4850 | if (ParenRange.getBegin().isFileID() && ParenRange.getEnd().isFileID() && |
| 4851 | EndLoc.isValid()) { |
| 4852 | Self.Diag(Loc, Note) |
| 4853 | << FixItHint::CreateInsertion(ParenRange.getBegin(), "(") |
| 4854 | << FixItHint::CreateInsertion(EndLoc, ")"); |
| 4855 | } else { |
| 4856 | // We can't display the parentheses, so just show the bare note. |
| 4857 | Self.Diag(Loc, Note) << ParenRange; |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4858 | } |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4859 | } |
| 4860 | |
| 4861 | static bool IsArithmeticOp(BinaryOperatorKind Opc) { |
| 4862 | return Opc >= BO_Mul && Opc <= BO_Shr; |
| 4863 | } |
| 4864 | |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4865 | /// IsArithmeticBinaryExpr - Returns true if E is an arithmetic binary |
| 4866 | /// expression, either using a built-in or overloaded operator, |
| 4867 | /// and sets *OpCode to the opcode and *RHS to the right-hand side expression. |
| 4868 | static bool IsArithmeticBinaryExpr(Expr *E, BinaryOperatorKind *Opcode, |
| 4869 | Expr **RHS) { |
| 4870 | E = E->IgnoreParenImpCasts(); |
| 4871 | E = E->IgnoreConversionOperator(); |
| 4872 | E = E->IgnoreParenImpCasts(); |
| 4873 | |
| 4874 | // Built-in binary operator. |
| 4875 | if (BinaryOperator *OP = dyn_cast<BinaryOperator>(E)) { |
| 4876 | if (IsArithmeticOp(OP->getOpcode())) { |
| 4877 | *Opcode = OP->getOpcode(); |
| 4878 | *RHS = OP->getRHS(); |
| 4879 | return true; |
| 4880 | } |
| 4881 | } |
| 4882 | |
| 4883 | // Overloaded operator. |
| 4884 | if (CXXOperatorCallExpr *Call = dyn_cast<CXXOperatorCallExpr>(E)) { |
| 4885 | if (Call->getNumArgs() != 2) |
| 4886 | return false; |
| 4887 | |
| 4888 | // Make sure this is really a binary operator that is safe to pass into |
| 4889 | // BinaryOperator::getOverloadedOpcode(), e.g. it's not a subscript op. |
| 4890 | OverloadedOperatorKind OO = Call->getOperator(); |
| 4891 | if (OO < OO_Plus || OO > OO_Arrow) |
| 4892 | return false; |
| 4893 | |
| 4894 | BinaryOperatorKind OpKind = BinaryOperator::getOverloadedOpcode(OO); |
| 4895 | if (IsArithmeticOp(OpKind)) { |
| 4896 | *Opcode = OpKind; |
| 4897 | *RHS = Call->getArg(1); |
| 4898 | return true; |
| 4899 | } |
| 4900 | } |
| 4901 | |
| 4902 | return false; |
| 4903 | } |
| 4904 | |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4905 | static bool IsLogicOp(BinaryOperatorKind Opc) { |
| 4906 | return (Opc >= BO_LT && Opc <= BO_NE) || (Opc >= BO_LAnd && Opc <= BO_LOr); |
| 4907 | } |
| 4908 | |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4909 | /// ExprLooksBoolean - Returns true if E looks boolean, i.e. it has boolean type |
| 4910 | /// or is a logical expression such as (x==y) which has int type, but is |
| 4911 | /// commonly interpreted as boolean. |
| 4912 | static bool ExprLooksBoolean(Expr *E) { |
| 4913 | E = E->IgnoreParenImpCasts(); |
| 4914 | |
| 4915 | if (E->getType()->isBooleanType()) |
| 4916 | return true; |
| 4917 | if (BinaryOperator *OP = dyn_cast<BinaryOperator>(E)) |
| 4918 | return IsLogicOp(OP->getOpcode()); |
| 4919 | if (UnaryOperator *OP = dyn_cast<UnaryOperator>(E)) |
| 4920 | return OP->getOpcode() == UO_LNot; |
| 4921 | |
| 4922 | return false; |
| 4923 | } |
| 4924 | |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4925 | /// DiagnoseConditionalPrecedence - Emit a warning when a conditional operator |
| 4926 | /// and binary operator are mixed in a way that suggests the programmer assumed |
| 4927 | /// the conditional operator has higher precedence, for example: |
| 4928 | /// "int x = a + someBinaryCondition ? 1 : 2". |
| 4929 | static void DiagnoseConditionalPrecedence(Sema &Self, |
| 4930 | SourceLocation OpLoc, |
Chandler Carruth | 43bc78d | 2011-06-16 01:05:08 +0000 | [diff] [blame] | 4931 | Expr *Condition, |
| 4932 | Expr *LHS, |
| 4933 | Expr *RHS) { |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4934 | BinaryOperatorKind CondOpcode; |
| 4935 | Expr *CondRHS; |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4936 | |
Chandler Carruth | 43bc78d | 2011-06-16 01:05:08 +0000 | [diff] [blame] | 4937 | if (!IsArithmeticBinaryExpr(Condition, &CondOpcode, &CondRHS)) |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4938 | return; |
| 4939 | if (!ExprLooksBoolean(CondRHS)) |
| 4940 | return; |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4941 | |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4942 | // The condition is an arithmetic binary expression, with a right- |
| 4943 | // hand side that looks boolean, so warn. |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4944 | |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 4945 | Self.Diag(OpLoc, diag::warn_precedence_conditional) |
Chandler Carruth | 43bc78d | 2011-06-16 01:05:08 +0000 | [diff] [blame] | 4946 | << Condition->getSourceRange() |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4947 | << BinaryOperator::getOpcodeStr(CondOpcode); |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4948 | |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 4949 | SuggestParentheses(Self, OpLoc, |
| 4950 | Self.PDiag(diag::note_precedence_conditional_silence) |
| 4951 | << BinaryOperator::getOpcodeStr(CondOpcode), |
| 4952 | SourceRange(Condition->getLocStart(), Condition->getLocEnd())); |
Chandler Carruth | 9d5353c | 2011-06-21 23:04:18 +0000 | [diff] [blame] | 4953 | |
| 4954 | SuggestParentheses(Self, OpLoc, |
| 4955 | Self.PDiag(diag::note_precedence_conditional_first), |
| 4956 | SourceRange(CondRHS->getLocStart(), RHS->getLocEnd())); |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4957 | } |
| 4958 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 4959 | /// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4960 | /// in the case of a the GNU conditional expr extension. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4961 | ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4962 | SourceLocation ColonLoc, |
| 4963 | Expr *CondExpr, Expr *LHSExpr, |
| 4964 | Expr *RHSExpr) { |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 4965 | // If this is the gnu "x ?: y" extension, analyze the types as though the LHS |
| 4966 | // was the condition. |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4967 | OpaqueValueExpr *opaqueValue = 0; |
| 4968 | Expr *commonExpr = 0; |
| 4969 | if (LHSExpr == 0) { |
| 4970 | commonExpr = CondExpr; |
| 4971 | |
| 4972 | // We usually want to apply unary conversions *before* saving, except |
| 4973 | // in the special case of a C++ l-value conditional. |
| 4974 | if (!(getLangOptions().CPlusPlus |
| 4975 | && !commonExpr->isTypeDependent() |
| 4976 | && commonExpr->getValueKind() == RHSExpr->getValueKind() |
| 4977 | && commonExpr->isGLValue() |
| 4978 | && commonExpr->isOrdinaryOrBitFieldObject() |
| 4979 | && RHSExpr->isOrdinaryOrBitFieldObject() |
| 4980 | && Context.hasSameType(commonExpr->getType(), RHSExpr->getType()))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4981 | ExprResult commonRes = UsualUnaryConversions(commonExpr); |
| 4982 | if (commonRes.isInvalid()) |
| 4983 | return ExprError(); |
| 4984 | commonExpr = commonRes.take(); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4985 | } |
| 4986 | |
| 4987 | opaqueValue = new (Context) OpaqueValueExpr(commonExpr->getExprLoc(), |
| 4988 | commonExpr->getType(), |
| 4989 | commonExpr->getValueKind(), |
| 4990 | commonExpr->getObjectKind()); |
| 4991 | LHSExpr = CondExpr = opaqueValue; |
Fariborz Jahanian | f9b949f | 2010-08-31 18:02:20 +0000 | [diff] [blame] | 4992 | } |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4993 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4994 | ExprValueKind VK = VK_RValue; |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4995 | ExprObjectKind OK = OK_Ordinary; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4996 | ExprResult Cond = Owned(CondExpr), LHS = Owned(LHSExpr), RHS = Owned(RHSExpr); |
| 4997 | QualType result = CheckConditionalOperands(Cond, LHS, RHS, |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4998 | VK, OK, QuestionLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4999 | if (result.isNull() || Cond.isInvalid() || LHS.isInvalid() || |
| 5000 | RHS.isInvalid()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5001 | return ExprError(); |
| 5002 | |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5003 | DiagnoseConditionalPrecedence(*this, QuestionLoc, Cond.get(), LHS.get(), |
| 5004 | RHS.get()); |
| 5005 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5006 | if (!commonExpr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5007 | return Owned(new (Context) ConditionalOperator(Cond.take(), QuestionLoc, |
| 5008 | LHS.take(), ColonLoc, |
| 5009 | RHS.take(), result, VK, OK)); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5010 | |
| 5011 | return Owned(new (Context) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5012 | BinaryConditionalOperator(commonExpr, opaqueValue, Cond.take(), LHS.take(), |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5013 | RHS.take(), QuestionLoc, ColonLoc, result, VK, |
| 5014 | OK)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5015 | } |
| 5016 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5017 | // checkPointerTypesForAssignment - This is a very tricky routine (despite |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5018 | // being closely modeled after the C99 spec:-). The odd characteristic of this |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5019 | // routine is it effectively iqnores the qualifiers on the top level pointee. |
| 5020 | // This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3]. |
| 5021 | // FIXME: add a couple examples in this comment. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5022 | static Sema::AssignConvertType |
| 5023 | checkPointerTypesForAssignment(Sema &S, QualType lhsType, QualType rhsType) { |
| 5024 | assert(lhsType.isCanonical() && "LHS not canonicalized!"); |
| 5025 | assert(rhsType.isCanonical() && "RHS not canonicalized!"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5026 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5027 | // get the "pointed to" type (ignoring qualifiers at the top level) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5028 | const Type *lhptee, *rhptee; |
| 5029 | Qualifiers lhq, rhq; |
| 5030 | llvm::tie(lhptee, lhq) = cast<PointerType>(lhsType)->getPointeeType().split(); |
| 5031 | llvm::tie(rhptee, rhq) = cast<PointerType>(rhsType)->getPointeeType().split(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5032 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5033 | Sema::AssignConvertType ConvTy = Sema::Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5034 | |
| 5035 | // C99 6.5.16.1p1: This following citation is common to constraints |
| 5036 | // 3 & 4 (below). ...and the type *pointed to* by the left has all the |
| 5037 | // qualifiers of the type *pointed to* by the right; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5038 | Qualifiers lq; |
| 5039 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5040 | // As a special case, 'non-__weak A *' -> 'non-__weak const *' is okay. |
| 5041 | if (lhq.getObjCLifetime() != rhq.getObjCLifetime() && |
| 5042 | lhq.compatiblyIncludesObjCLifetime(rhq)) { |
| 5043 | // Ignore lifetime for further calculation. |
| 5044 | lhq.removeObjCLifetime(); |
| 5045 | rhq.removeObjCLifetime(); |
| 5046 | } |
| 5047 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5048 | if (!lhq.compatiblyIncludes(rhq)) { |
| 5049 | // Treat address-space mismatches as fatal. TODO: address subspaces |
| 5050 | if (lhq.getAddressSpace() != rhq.getAddressSpace()) |
| 5051 | ConvTy = Sema::IncompatiblePointerDiscardsQualifiers; |
| 5052 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5053 | // It's okay to add or remove GC or lifetime qualifiers when converting to |
John McCall | 2234873 | 2011-03-26 02:56:45 +0000 | [diff] [blame] | 5054 | // and from void*. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5055 | else if (lhq.withoutObjCGCAttr().withoutObjCGLifetime() |
| 5056 | .compatiblyIncludes( |
| 5057 | rhq.withoutObjCGCAttr().withoutObjCGLifetime()) |
John McCall | 2234873 | 2011-03-26 02:56:45 +0000 | [diff] [blame] | 5058 | && (lhptee->isVoidType() || rhptee->isVoidType())) |
| 5059 | ; // keep old |
| 5060 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5061 | // Treat lifetime mismatches as fatal. |
| 5062 | else if (lhq.getObjCLifetime() != rhq.getObjCLifetime()) |
| 5063 | ConvTy = Sema::IncompatiblePointerDiscardsQualifiers; |
| 5064 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5065 | // For GCC compatibility, other qualifier mismatches are treated |
| 5066 | // as still compatible in C. |
| 5067 | else ConvTy = Sema::CompatiblePointerDiscardsQualifiers; |
| 5068 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5069 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5070 | // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or |
| 5071 | // incomplete type and the other is a pointer to a qualified or unqualified |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5072 | // version of void... |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5073 | if (lhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5074 | if (rhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5075 | return ConvTy; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5076 | |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5077 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5078 | assert(rhptee->isFunctionType()); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5079 | return Sema::FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5080 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5081 | |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5082 | if (rhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5083 | if (lhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5084 | return ConvTy; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5085 | |
| 5086 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5087 | assert(lhptee->isFunctionType()); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5088 | return Sema::FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5089 | } |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5090 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5091 | // C99 6.5.16.1p1 (constraint 3): both operands are pointers to qualified or |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5092 | // unqualified versions of compatible types, ... |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5093 | QualType ltrans = QualType(lhptee, 0), rtrans = QualType(rhptee, 0); |
| 5094 | if (!S.Context.typesAreCompatible(ltrans, rtrans)) { |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5095 | // Check if the pointee types are compatible ignoring the sign. |
| 5096 | // We explicitly check for char so that we catch "char" vs |
| 5097 | // "unsigned char" on systems where "char" is unsigned. |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5098 | if (lhptee->isCharType()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5099 | ltrans = S.Context.UnsignedCharTy; |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 5100 | else if (lhptee->hasSignedIntegerRepresentation()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5101 | ltrans = S.Context.getCorrespondingUnsignedType(ltrans); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5102 | |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5103 | if (rhptee->isCharType()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5104 | rtrans = S.Context.UnsignedCharTy; |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 5105 | else if (rhptee->hasSignedIntegerRepresentation()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5106 | rtrans = S.Context.getCorrespondingUnsignedType(rtrans); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5107 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5108 | if (ltrans == rtrans) { |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5109 | // Types are compatible ignoring the sign. Qualifier incompatibility |
| 5110 | // takes priority over sign incompatibility because the sign |
| 5111 | // warning can be disabled. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5112 | if (ConvTy != Sema::Compatible) |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5113 | return ConvTy; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5114 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5115 | return Sema::IncompatiblePointerSign; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5116 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5117 | |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 5118 | // If we are a multi-level pointer, it's possible that our issue is simply |
| 5119 | // one of qualification - e.g. char ** -> const char ** is not allowed. If |
| 5120 | // the eventual target type is the same and the pointers have the same |
| 5121 | // level of indirection, this must be the issue. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5122 | if (isa<PointerType>(lhptee) && isa<PointerType>(rhptee)) { |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 5123 | do { |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5124 | lhptee = cast<PointerType>(lhptee)->getPointeeType().getTypePtr(); |
| 5125 | rhptee = cast<PointerType>(rhptee)->getPointeeType().getTypePtr(); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5126 | } while (isa<PointerType>(lhptee) && isa<PointerType>(rhptee)); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5127 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5128 | if (lhptee == rhptee) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5129 | return Sema::IncompatibleNestedPointerQualifiers; |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 5130 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5131 | |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5132 | // General pointer incompatibility takes priority over qualifiers. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5133 | return Sema::IncompatiblePointer; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5134 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5135 | return ConvTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5136 | } |
| 5137 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5138 | /// checkBlockPointerTypesForAssignment - This routine determines whether two |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5139 | /// block pointer types are compatible or whether a block and normal pointer |
| 5140 | /// are compatible. It is more restrict than comparing two function pointer |
| 5141 | // types. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5142 | static Sema::AssignConvertType |
| 5143 | checkBlockPointerTypesForAssignment(Sema &S, QualType lhsType, |
| 5144 | QualType rhsType) { |
| 5145 | assert(lhsType.isCanonical() && "LHS not canonicalized!"); |
| 5146 | assert(rhsType.isCanonical() && "RHS not canonicalized!"); |
| 5147 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5148 | QualType lhptee, rhptee; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5149 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5150 | // get the "pointed to" type (ignoring qualifiers at the top level) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5151 | lhptee = cast<BlockPointerType>(lhsType)->getPointeeType(); |
| 5152 | rhptee = cast<BlockPointerType>(rhsType)->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5153 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5154 | // In C++, the types have to match exactly. |
| 5155 | if (S.getLangOptions().CPlusPlus) |
| 5156 | return Sema::IncompatibleBlockPointer; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5157 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5158 | Sema::AssignConvertType ConvTy = Sema::Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5159 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5160 | // For blocks we enforce that qualifiers are identical. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5161 | if (lhptee.getLocalQualifiers() != rhptee.getLocalQualifiers()) |
| 5162 | ConvTy = Sema::CompatiblePointerDiscardsQualifiers; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5163 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5164 | if (!S.Context.typesAreBlockPointerCompatible(lhsType, rhsType)) |
| 5165 | return Sema::IncompatibleBlockPointer; |
| 5166 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5167 | return ConvTy; |
| 5168 | } |
| 5169 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5170 | /// checkObjCPointerTypesForAssignment - Compares two objective-c pointer types |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5171 | /// for assignment compatibility. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5172 | static Sema::AssignConvertType |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5173 | checkObjCPointerTypesForAssignment(Sema &S, QualType lhsType, |
| 5174 | QualType rhsType) { |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5175 | assert(lhsType.isCanonical() && "LHS was not canonicalized!"); |
| 5176 | assert(rhsType.isCanonical() && "RHS was not canonicalized!"); |
| 5177 | |
Fariborz Jahanian | d4c6090 | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 5178 | if (lhsType->isObjCBuiltinType()) { |
| 5179 | // Class is not compatible with ObjC object pointers. |
Fariborz Jahanian | 528adb1 | 2010-03-24 21:00:27 +0000 | [diff] [blame] | 5180 | if (lhsType->isObjCClassType() && !rhsType->isObjCBuiltinType() && |
| 5181 | !rhsType->isObjCQualifiedClassType()) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5182 | return Sema::IncompatiblePointer; |
| 5183 | return Sema::Compatible; |
Fariborz Jahanian | d4c6090 | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 5184 | } |
| 5185 | if (rhsType->isObjCBuiltinType()) { |
| 5186 | // Class is not compatible with ObjC object pointers. |
Fariborz Jahanian | 528adb1 | 2010-03-24 21:00:27 +0000 | [diff] [blame] | 5187 | if (rhsType->isObjCClassType() && !lhsType->isObjCBuiltinType() && |
| 5188 | !lhsType->isObjCQualifiedClassType()) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5189 | return Sema::IncompatiblePointer; |
| 5190 | return Sema::Compatible; |
Fariborz Jahanian | d4c6090 | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 5191 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5192 | QualType lhptee = |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5193 | lhsType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5194 | QualType rhptee = |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5195 | rhsType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5196 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5197 | if (!lhptee.isAtLeastAsQualifiedAs(rhptee)) |
| 5198 | return Sema::CompatiblePointerDiscardsQualifiers; |
| 5199 | |
| 5200 | if (S.Context.typesAreCompatible(lhsType, rhsType)) |
| 5201 | return Sema::Compatible; |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5202 | if (lhsType->isObjCQualifiedIdType() || rhsType->isObjCQualifiedIdType()) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5203 | return Sema::IncompatibleObjCQualifiedId; |
| 5204 | return Sema::IncompatiblePointer; |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5205 | } |
| 5206 | |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5207 | Sema::AssignConvertType |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 5208 | Sema::CheckAssignmentConstraints(SourceLocation Loc, |
| 5209 | QualType lhsType, QualType rhsType) { |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5210 | // Fake up an opaque expression. We don't actually care about what |
| 5211 | // cast operations are required, so if CheckAssignmentConstraints |
| 5212 | // adds casts to this they'll be wasted, but fortunately that doesn't |
| 5213 | // usually happen on valid code. |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 5214 | OpaqueValueExpr rhs(Loc, rhsType, VK_RValue); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5215 | ExprResult rhsPtr = &rhs; |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5216 | CastKind K = CK_Invalid; |
| 5217 | |
| 5218 | return CheckAssignmentConstraints(lhsType, rhsPtr, K); |
| 5219 | } |
| 5220 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5221 | /// CheckAssignmentConstraints (C99 6.5.16) - This routine currently |
| 5222 | /// has code to accommodate several GCC extensions when type checking |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5223 | /// pointers. Here are some objectionable examples that GCC considers warnings: |
| 5224 | /// |
| 5225 | /// int a, *pint; |
| 5226 | /// short *pshort; |
| 5227 | /// struct foo *pfoo; |
| 5228 | /// |
| 5229 | /// pint = pshort; // warning: assignment from incompatible pointer type |
| 5230 | /// a = pint; // warning: assignment makes integer from pointer without a cast |
| 5231 | /// pint = a; // warning: assignment makes pointer from integer without a cast |
| 5232 | /// pint = pfoo; // warning: assignment from incompatible pointer type |
| 5233 | /// |
| 5234 | /// As a result, the code for dealing with pointers is more complex than the |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5235 | /// C99 spec dictates. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5236 | /// |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5237 | /// Sets 'Kind' for any result kind except Incompatible. |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5238 | Sema::AssignConvertType |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5239 | Sema::CheckAssignmentConstraints(QualType lhsType, ExprResult &rhs, |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5240 | CastKind &Kind) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5241 | QualType rhsType = rhs.get()->getType(); |
Fariborz Jahanian | 04e5a25 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 5242 | QualType origLhsType = lhsType; |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5243 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5244 | // Get canonical types. We're not formatting these types, just comparing |
| 5245 | // them. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 5246 | lhsType = Context.getCanonicalType(lhsType).getUnqualifiedType(); |
| 5247 | rhsType = Context.getCanonicalType(rhsType).getUnqualifiedType(); |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5248 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5249 | // Common case: no conversion required. |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5250 | if (lhsType == rhsType) { |
| 5251 | Kind = CK_NoOp; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5252 | return Compatible; |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 5253 | } |
| 5254 | |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 5255 | // If the left-hand side is a reference type, then we are in a |
| 5256 | // (rare!) case where we've allowed the use of references in C, |
| 5257 | // e.g., as a parameter type in a built-in function. In this case, |
| 5258 | // just make sure that the type referenced is compatible with the |
| 5259 | // right-hand side type. The caller is responsible for adjusting |
| 5260 | // lhsType so that the resulting expression does not have reference |
| 5261 | // type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5262 | if (const ReferenceType *lhsTypeRef = lhsType->getAs<ReferenceType>()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5263 | if (Context.typesAreCompatible(lhsTypeRef->getPointeeType(), rhsType)) { |
| 5264 | Kind = CK_LValueBitCast; |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 5265 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5266 | } |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5267 | return Incompatible; |
Fariborz Jahanian | 411f373 | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 5268 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5269 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5270 | // Allow scalar to ExtVector assignments, and assignments of an ExtVector type |
| 5271 | // to the same ExtVector type. |
| 5272 | if (lhsType->isExtVectorType()) { |
| 5273 | if (rhsType->isExtVectorType()) |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5274 | return Incompatible; |
| 5275 | if (rhsType->isArithmeticType()) { |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5276 | // CK_VectorSplat does T -> vector T, so first cast to the |
| 5277 | // element type. |
| 5278 | QualType elType = cast<ExtVectorType>(lhsType)->getElementType(); |
| 5279 | if (elType != rhsType) { |
| 5280 | Kind = PrepareScalarCast(*this, rhs, elType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5281 | rhs = ImpCastExprToType(rhs.take(), elType, Kind); |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5282 | } |
| 5283 | Kind = CK_VectorSplat; |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5284 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5285 | } |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5286 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5287 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5288 | // Conversions to or from vector type. |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5289 | if (lhsType->isVectorType() || rhsType->isVectorType()) { |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5290 | if (lhsType->isVectorType() && rhsType->isVectorType()) { |
Bob Wilson | de3deea | 2010-12-02 00:25:15 +0000 | [diff] [blame] | 5291 | // Allow assignments of an AltiVec vector type to an equivalent GCC |
| 5292 | // vector type and vice versa |
| 5293 | if (Context.areCompatibleVectorTypes(lhsType, rhsType)) { |
| 5294 | Kind = CK_BitCast; |
| 5295 | return Compatible; |
| 5296 | } |
| 5297 | |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5298 | // If we are allowing lax vector conversions, and LHS and RHS are both |
| 5299 | // vectors, the total size only needs to be the same. This is a bitcast; |
| 5300 | // no bits are changed but the result type is different. |
| 5301 | if (getLangOptions().LaxVectorConversions && |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5302 | (Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType))) { |
John McCall | 0c6d28d | 2010-11-15 10:08:00 +0000 | [diff] [blame] | 5303 | Kind = CK_BitCast; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 5304 | return IncompatibleVectors; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5305 | } |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 5306 | } |
| 5307 | return Incompatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5308 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5309 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5310 | // Arithmetic conversions. |
Douglas Gregor | 88623ad | 2010-05-23 21:53:47 +0000 | [diff] [blame] | 5311 | if (lhsType->isArithmeticType() && rhsType->isArithmeticType() && |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5312 | !(getLangOptions().CPlusPlus && lhsType->isEnumeralType())) { |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5313 | Kind = PrepareScalarCast(*this, rhs, lhsType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5314 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5315 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5316 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5317 | // Conversions to normal pointers. |
| 5318 | if (const PointerType *lhsPointer = dyn_cast<PointerType>(lhsType)) { |
| 5319 | // U* -> T* |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5320 | if (isa<PointerType>(rhsType)) { |
| 5321 | Kind = CK_BitCast; |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5322 | return checkPointerTypesForAssignment(*this, lhsType, rhsType); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5323 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5324 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5325 | // int -> T* |
| 5326 | if (rhsType->isIntegerType()) { |
| 5327 | Kind = CK_IntegralToPointer; // FIXME: null? |
| 5328 | return IntToPointer; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5329 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5330 | |
| 5331 | // C pointers are not compatible with ObjC object pointers, |
| 5332 | // with two exceptions: |
| 5333 | if (isa<ObjCObjectPointerType>(rhsType)) { |
| 5334 | // - conversions to void* |
| 5335 | if (lhsPointer->getPointeeType()->isVoidType()) { |
| 5336 | Kind = CK_AnyPointerToObjCPointerCast; |
| 5337 | return Compatible; |
| 5338 | } |
| 5339 | |
| 5340 | // - conversions from 'Class' to the redefinition type |
| 5341 | if (rhsType->isObjCClassType() && |
| 5342 | Context.hasSameType(lhsType, Context.ObjCClassRedefinitionType)) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5343 | Kind = CK_BitCast; |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 5344 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5345 | } |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5346 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5347 | Kind = CK_BitCast; |
| 5348 | return IncompatiblePointer; |
| 5349 | } |
| 5350 | |
| 5351 | // U^ -> void* |
| 5352 | if (rhsType->getAs<BlockPointerType>()) { |
| 5353 | if (lhsPointer->getPointeeType()->isVoidType()) { |
| 5354 | Kind = CK_BitCast; |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5355 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5356 | } |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5357 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5358 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5359 | return Incompatible; |
| 5360 | } |
| 5361 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5362 | // Conversions to block pointers. |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5363 | if (isa<BlockPointerType>(lhsType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5364 | // U^ -> T^ |
| 5365 | if (rhsType->isBlockPointerType()) { |
| 5366 | Kind = CK_AnyPointerToBlockPointerCast; |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5367 | return checkBlockPointerTypesForAssignment(*this, lhsType, rhsType); |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5368 | } |
| 5369 | |
| 5370 | // int or null -> T^ |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5371 | if (rhsType->isIntegerType()) { |
| 5372 | Kind = CK_IntegralToPointer; // FIXME: null |
Eli Friedman | d8f4f43 | 2009-02-25 04:20:42 +0000 | [diff] [blame] | 5373 | return IntToBlockPointer; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5374 | } |
| 5375 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5376 | // id -> T^ |
| 5377 | if (getLangOptions().ObjC1 && rhsType->isObjCIdType()) { |
| 5378 | Kind = CK_AnyPointerToBlockPointerCast; |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5379 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5380 | } |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5381 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5382 | // void* -> T^ |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5383 | if (const PointerType *RHSPT = rhsType->getAs<PointerType>()) |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5384 | if (RHSPT->getPointeeType()->isVoidType()) { |
| 5385 | Kind = CK_AnyPointerToBlockPointerCast; |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 5386 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5387 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5388 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5389 | return Incompatible; |
| 5390 | } |
| 5391 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5392 | // Conversions to Objective-C pointers. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5393 | if (isa<ObjCObjectPointerType>(lhsType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5394 | // A* -> B* |
| 5395 | if (rhsType->isObjCObjectPointerType()) { |
| 5396 | Kind = CK_BitCast; |
Fariborz Jahanian | 04e5a25 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 5397 | Sema::AssignConvertType result = |
| 5398 | checkObjCPointerTypesForAssignment(*this, lhsType, rhsType); |
| 5399 | if (getLangOptions().ObjCAutoRefCount && |
| 5400 | result == Compatible && |
Fariborz Jahanian | 7a084ec | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 5401 | !CheckObjCARCUnavailableWeakConversion(origLhsType, rhsType)) |
| 5402 | result = IncompatibleObjCWeakRef; |
Fariborz Jahanian | 04e5a25 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 5403 | return result; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5404 | } |
| 5405 | |
| 5406 | // int or null -> A* |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5407 | if (rhsType->isIntegerType()) { |
| 5408 | Kind = CK_IntegralToPointer; // FIXME: null |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5409 | return IntToPointer; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5410 | } |
| 5411 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5412 | // In general, C pointers are not compatible with ObjC object pointers, |
| 5413 | // with two exceptions: |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5414 | if (isa<PointerType>(rhsType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5415 | // - conversions from 'void*' |
| 5416 | if (rhsType->isVoidPointerType()) { |
| 5417 | Kind = CK_AnyPointerToObjCPointerCast; |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 5418 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5419 | } |
| 5420 | |
| 5421 | // - conversions to 'Class' from its redefinition type |
| 5422 | if (lhsType->isObjCClassType() && |
| 5423 | Context.hasSameType(rhsType, Context.ObjCClassRedefinitionType)) { |
| 5424 | Kind = CK_BitCast; |
| 5425 | return Compatible; |
| 5426 | } |
| 5427 | |
| 5428 | Kind = CK_AnyPointerToObjCPointerCast; |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 5429 | return IncompatiblePointer; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5430 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5431 | |
| 5432 | // T^ -> A* |
| 5433 | if (rhsType->isBlockPointerType()) { |
| 5434 | Kind = CK_AnyPointerToObjCPointerCast; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5435 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5436 | } |
| 5437 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5438 | return Incompatible; |
| 5439 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5440 | |
| 5441 | // Conversions from pointers that are not covered by the above. |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 5442 | if (isa<PointerType>(rhsType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5443 | // T* -> _Bool |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5444 | if (lhsType == Context.BoolTy) { |
| 5445 | Kind = CK_PointerToBoolean; |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5446 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5447 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5448 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5449 | // T* -> int |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5450 | if (lhsType->isIntegerType()) { |
| 5451 | Kind = CK_PointerToIntegral; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 5452 | return PointerToInt; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5453 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5454 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5455 | return Incompatible; |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5456 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5457 | |
| 5458 | // Conversions from Objective-C pointers that are not covered by the above. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5459 | if (isa<ObjCObjectPointerType>(rhsType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5460 | // T* -> _Bool |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5461 | if (lhsType == Context.BoolTy) { |
| 5462 | Kind = CK_PointerToBoolean; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5463 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5464 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5465 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5466 | // T* -> int |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5467 | if (lhsType->isIntegerType()) { |
| 5468 | Kind = CK_PointerToIntegral; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5469 | return PointerToInt; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5470 | } |
| 5471 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5472 | return Incompatible; |
| 5473 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5474 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5475 | // struct A -> struct B |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5476 | if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5477 | if (Context.typesAreCompatible(lhsType, rhsType)) { |
| 5478 | Kind = CK_NoOp; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5479 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5480 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5481 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5482 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5483 | return Incompatible; |
| 5484 | } |
| 5485 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5486 | /// \brief Constructs a transparent union from an expression that is |
| 5487 | /// used to initialize the transparent union. |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5488 | static void ConstructTransparentUnion(Sema &S, ASTContext &C, |
| 5489 | ExprResult &EResult, QualType UnionType, |
| 5490 | FieldDecl *Field) { |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5491 | // Build an initializer list that designates the appropriate member |
| 5492 | // of the transparent union. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5493 | Expr *E = EResult.take(); |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 5494 | InitListExpr *Initializer = new (C) InitListExpr(C, SourceLocation(), |
Ted Kremenek | ba7bc55 | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 5495 | &E, 1, |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5496 | SourceLocation()); |
| 5497 | Initializer->setType(UnionType); |
| 5498 | Initializer->setInitializedFieldInUnion(Field); |
| 5499 | |
| 5500 | // Build a compound literal constructing a value of the transparent |
| 5501 | // union type from this initializer list. |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5502 | TypeSourceInfo *unionTInfo = C.getTrivialTypeSourceInfo(UnionType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5503 | EResult = S.Owned( |
| 5504 | new (C) CompoundLiteralExpr(SourceLocation(), unionTInfo, UnionType, |
| 5505 | VK_RValue, Initializer, false)); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5506 | } |
| 5507 | |
| 5508 | Sema::AssignConvertType |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5509 | Sema::CheckTransparentUnionArgumentConstraints(QualType ArgType, |
| 5510 | ExprResult &rExpr) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5511 | QualType FromType = rExpr.get()->getType(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5512 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5513 | // If the ArgType is a Union type, we want to handle a potential |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5514 | // transparent_union GCC extension. |
| 5515 | const RecordType *UT = ArgType->getAsUnionType(); |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 5516 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5517 | return Incompatible; |
| 5518 | |
| 5519 | // The field to initialize within the transparent union. |
| 5520 | RecordDecl *UD = UT->getDecl(); |
| 5521 | FieldDecl *InitField = 0; |
| 5522 | // It's compatible if the expression matches any of the fields. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5523 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 5524 | itend = UD->field_end(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5525 | it != itend; ++it) { |
| 5526 | if (it->getType()->isPointerType()) { |
| 5527 | // If the transparent union contains a pointer type, we allow: |
| 5528 | // 1) void pointer |
| 5529 | // 2) null pointer constant |
| 5530 | if (FromType->isPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5531 | if (FromType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5532 | rExpr = ImpCastExprToType(rExpr.take(), it->getType(), CK_BitCast); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5533 | InitField = *it; |
| 5534 | break; |
| 5535 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5536 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5537 | if (rExpr.get()->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 5538 | Expr::NPC_ValueDependentIsNull)) { |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5539 | rExpr = ImpCastExprToType(rExpr.take(), it->getType(), |
| 5540 | CK_NullToPointer); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5541 | InitField = *it; |
| 5542 | break; |
| 5543 | } |
| 5544 | } |
| 5545 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5546 | CastKind Kind = CK_Invalid; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5547 | if (CheckAssignmentConstraints(it->getType(), rExpr, Kind) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5548 | == Compatible) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5549 | rExpr = ImpCastExprToType(rExpr.take(), it->getType(), Kind); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5550 | InitField = *it; |
| 5551 | break; |
| 5552 | } |
| 5553 | } |
| 5554 | |
| 5555 | if (!InitField) |
| 5556 | return Incompatible; |
| 5557 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5558 | ConstructTransparentUnion(*this, Context, rExpr, ArgType, InitField); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5559 | return Compatible; |
| 5560 | } |
| 5561 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5562 | Sema::AssignConvertType |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5563 | Sema::CheckSingleAssignmentConstraints(QualType lhsType, ExprResult &rExpr) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5564 | if (getLangOptions().CPlusPlus) { |
| 5565 | if (!lhsType->isRecordType()) { |
| 5566 | // C++ 5.17p3: If the left operand is not of class type, the |
| 5567 | // expression is implicitly converted (C++ 4) to the |
| 5568 | // cv-unqualified type of the left operand. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5569 | ExprResult Res = PerformImplicitConversion(rExpr.get(), |
| 5570 | lhsType.getUnqualifiedType(), |
| 5571 | AA_Assigning); |
| 5572 | if (Res.isInvalid()) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5573 | return Incompatible; |
Fariborz Jahanian | 7a084ec | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 5574 | Sema::AssignConvertType result = Compatible; |
| 5575 | if (getLangOptions().ObjCAutoRefCount && |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5576 | !CheckObjCARCUnavailableWeakConversion(lhsType, |
| 5577 | rExpr.get()->getType())) |
Fariborz Jahanian | 7a084ec | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 5578 | result = IncompatibleObjCWeakRef; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5579 | rExpr = move(Res); |
Fariborz Jahanian | 7a084ec | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 5580 | return result; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5581 | } |
| 5582 | |
| 5583 | // FIXME: Currently, we fall through and treat C++ classes like C |
| 5584 | // structures. |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 5585 | } |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5586 | |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 5587 | // C99 6.5.16.1p1: the left operand is a pointer and the right is |
| 5588 | // a null pointer constant. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5589 | if ((lhsType->isPointerType() || |
| 5590 | lhsType->isObjCObjectPointerType() || |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5591 | lhsType->isBlockPointerType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5592 | && rExpr.get()->isNullPointerConstant(Context, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5593 | Expr::NPC_ValueDependentIsNull)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5594 | rExpr = ImpCastExprToType(rExpr.take(), lhsType, CK_NullToPointer); |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 5595 | return Compatible; |
| 5596 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5597 | |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 5598 | // This check seems unnatural, however it is necessary to ensure the proper |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 5599 | // conversion of functions/arrays. If the conversion were done for all |
Douglas Gregor | 02a24ee | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 5600 | // DeclExpr's (created by ActOnIdExpression), it would mess up the unary |
Nick Lewycky | c133e9e | 2010-08-05 06:27:49 +0000 | [diff] [blame] | 5601 | // expressions that suppress this implicit conversion (&, sizeof). |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 5602 | // |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5603 | // Suppress this for references: C++ 8.5.3p5. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5604 | if (!lhsType->isReferenceType()) { |
| 5605 | rExpr = DefaultFunctionArrayLvalueConversion(rExpr.take()); |
| 5606 | if (rExpr.isInvalid()) |
| 5607 | return Incompatible; |
| 5608 | } |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 5609 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5610 | CastKind Kind = CK_Invalid; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5611 | Sema::AssignConvertType result = |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5612 | CheckAssignmentConstraints(lhsType, rExpr, Kind); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5613 | |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 5614 | // C99 6.5.16.1p2: The value of the right operand is converted to the |
| 5615 | // type of the assignment expression. |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 5616 | // CheckAssignmentConstraints allows the left-hand side to be a reference, |
| 5617 | // so that we can use references in built-in functions even in C. |
| 5618 | // The getNonReferenceType() call makes sure that the resulting expression |
| 5619 | // does not have reference type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5620 | if (result != Incompatible && rExpr.get()->getType() != lhsType) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5621 | rExpr = ImpCastExprToType(rExpr.take(), |
| 5622 | lhsType.getNonLValueExprType(Context), Kind); |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 5623 | return result; |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 5624 | } |
| 5625 | |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5626 | QualType Sema::InvalidOperands(SourceLocation Loc, ExprResult &lex, |
| 5627 | ExprResult &rex) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 5628 | Diag(Loc, diag::err_typecheck_invalid_operands) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5629 | << lex.get()->getType() << rex.get()->getType() |
| 5630 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 5631 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5632 | } |
| 5633 | |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5634 | QualType Sema::CheckVectorOperands(ExprResult &lex, ExprResult &rex, |
| 5635 | SourceLocation Loc, bool isCompAssign) { |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5636 | // For conversion purposes, we ignore any qualifiers. |
Nate Begeman | 1330b0e | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 5637 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 5638 | QualType lhsType = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5639 | Context.getCanonicalType(lex.get()->getType()).getUnqualifiedType(); |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 5640 | QualType rhsType = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5641 | Context.getCanonicalType(rex.get()->getType()).getUnqualifiedType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5642 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5643 | // If the vector types are identical, return. |
Nate Begeman | 1330b0e | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 5644 | if (lhsType == rhsType) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5645 | return lhsType; |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 5646 | |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5647 | // Handle the case of equivalent AltiVec and GCC vector types |
| 5648 | if (lhsType->isVectorType() && rhsType->isVectorType() && |
| 5649 | Context.areCompatibleVectorTypes(lhsType, rhsType)) { |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5650 | if (lhsType->isExtVectorType()) { |
| 5651 | rex = ImpCastExprToType(rex.take(), lhsType, CK_BitCast); |
| 5652 | return lhsType; |
| 5653 | } |
| 5654 | |
| 5655 | if (!isCompAssign) |
| 5656 | lex = ImpCastExprToType(lex.take(), rhsType, CK_BitCast); |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5657 | return rhsType; |
| 5658 | } |
| 5659 | |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5660 | if (getLangOptions().LaxVectorConversions && |
| 5661 | Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType)) { |
| 5662 | // If we are allowing lax vector conversions, and LHS and RHS are both |
| 5663 | // vectors, the total size only needs to be the same. This is a |
| 5664 | // bitcast; no bits are changed but the result type is different. |
| 5665 | // FIXME: Should we really be allowing this? |
| 5666 | rex = ImpCastExprToType(rex.take(), lhsType, CK_BitCast); |
| 5667 | return lhsType; |
| 5668 | } |
| 5669 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5670 | // Canonicalize the ExtVector to the LHS, remember if we swapped so we can |
| 5671 | // swap back (so that we don't reverse the inputs to a subtract, for instance. |
| 5672 | bool swapped = false; |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5673 | if (rhsType->isExtVectorType() && !isCompAssign) { |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5674 | swapped = true; |
| 5675 | std::swap(rex, lex); |
| 5676 | std::swap(rhsType, lhsType); |
| 5677 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5678 | |
Nate Begeman | dde2598 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 5679 | // Handle the case of an ext vector and scalar. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5680 | if (const ExtVectorType *LV = lhsType->getAs<ExtVectorType>()) { |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5681 | QualType EltTy = LV->getElementType(); |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 5682 | if (EltTy->isIntegralType(Context) && rhsType->isIntegralType(Context)) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5683 | int order = Context.getIntegerTypeOrder(EltTy, rhsType); |
| 5684 | if (order > 0) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5685 | rex = ImpCastExprToType(rex.take(), EltTy, CK_IntegralCast); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5686 | if (order >= 0) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5687 | rex = ImpCastExprToType(rex.take(), lhsType, CK_VectorSplat); |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5688 | if (swapped) std::swap(rex, lex); |
| 5689 | return lhsType; |
| 5690 | } |
| 5691 | } |
| 5692 | if (EltTy->isRealFloatingType() && rhsType->isScalarType() && |
| 5693 | rhsType->isRealFloatingType()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5694 | int order = Context.getFloatingTypeOrder(EltTy, rhsType); |
| 5695 | if (order > 0) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5696 | rex = ImpCastExprToType(rex.take(), EltTy, CK_FloatingCast); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5697 | if (order >= 0) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5698 | rex = ImpCastExprToType(rex.take(), lhsType, CK_VectorSplat); |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5699 | if (swapped) std::swap(rex, lex); |
| 5700 | return lhsType; |
| 5701 | } |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 5702 | } |
| 5703 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5704 | |
Nate Begeman | dde2598 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 5705 | // Vectors of different size or scalar and non-ext-vector are errors. |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5706 | if (swapped) std::swap(rex, lex); |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 5707 | Diag(Loc, diag::err_typecheck_vector_not_convertable) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5708 | << lex.get()->getType() << rex.get()->getType() |
| 5709 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5710 | return QualType(); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 5711 | } |
| 5712 | |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5713 | QualType Sema::CheckMultiplyDivideOperands(ExprResult &lex, ExprResult &rex, |
| 5714 | SourceLocation Loc, |
| 5715 | bool isCompAssign, bool isDiv) { |
| 5716 | if (lex.get()->getType()->isVectorType() || |
| 5717 | rex.get()->getType()->isVectorType()) |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5718 | return CheckVectorOperands(lex, rex, Loc, isCompAssign); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5719 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 5720 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5721 | if (lex.isInvalid() || rex.isInvalid()) |
| 5722 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5723 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5724 | if (!lex.get()->getType()->isArithmeticType() || |
| 5725 | !rex.get()->getType()->isArithmeticType()) |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5726 | return InvalidOperands(Loc, lex, rex); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5727 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5728 | // Check for division by zero. |
| 5729 | if (isDiv && |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5730 | rex.get()->isNullPointerConstant(Context, |
| 5731 | Expr::NPC_ValueDependentIsNotNull)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5732 | DiagRuntimeBehavior(Loc, rex.get(), PDiag(diag::warn_division_by_zero) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5733 | << rex.get()->getSourceRange()); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5734 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5735 | return compType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5736 | } |
| 5737 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5738 | QualType Sema::CheckRemainderOperands( |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5739 | ExprResult &lex, ExprResult &rex, SourceLocation Loc, bool isCompAssign) { |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5740 | if (lex.get()->getType()->isVectorType() || |
| 5741 | rex.get()->getType()->isVectorType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5742 | if (lex.get()->getType()->hasIntegerRepresentation() && |
| 5743 | rex.get()->getType()->hasIntegerRepresentation()) |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5744 | return CheckVectorOperands(lex, rex, Loc, isCompAssign); |
Daniel Dunbar | 523aa60 | 2009-01-05 22:55:36 +0000 | [diff] [blame] | 5745 | return InvalidOperands(Loc, lex, rex); |
| 5746 | } |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 5747 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 5748 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5749 | if (lex.isInvalid() || rex.isInvalid()) |
| 5750 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5751 | |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5752 | if (!lex.get()->getType()->isIntegerType() || |
| 5753 | !rex.get()->getType()->isIntegerType()) |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5754 | return InvalidOperands(Loc, lex, rex); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5755 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5756 | // Check for remainder by zero. |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5757 | if (rex.get()->isNullPointerConstant(Context, |
| 5758 | Expr::NPC_ValueDependentIsNotNull)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5759 | DiagRuntimeBehavior(Loc, rex.get(), PDiag(diag::warn_remainder_by_zero) |
| 5760 | << rex.get()->getSourceRange()); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5761 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5762 | return compType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5763 | } |
| 5764 | |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 5765 | /// \brief Diagnose invalid arithmetic on two void pointers. |
| 5766 | static void diagnoseArithmeticOnTwoVoidPointers(Sema &S, SourceLocation Loc, |
| 5767 | Expr *LHS, Expr *RHS) { |
| 5768 | S.Diag(Loc, S.getLangOptions().CPlusPlus |
| 5769 | ? diag::err_typecheck_pointer_arith_void_type |
| 5770 | : diag::ext_gnu_void_ptr) |
| 5771 | << 1 /* two pointers */ << LHS->getSourceRange() << RHS->getSourceRange(); |
| 5772 | } |
| 5773 | |
| 5774 | /// \brief Diagnose invalid arithmetic on a void pointer. |
| 5775 | static void diagnoseArithmeticOnVoidPointer(Sema &S, SourceLocation Loc, |
| 5776 | Expr *Pointer) { |
| 5777 | S.Diag(Loc, S.getLangOptions().CPlusPlus |
| 5778 | ? diag::err_typecheck_pointer_arith_void_type |
| 5779 | : diag::ext_gnu_void_ptr) |
| 5780 | << 0 /* one pointer */ << Pointer->getSourceRange(); |
| 5781 | } |
| 5782 | |
| 5783 | /// \brief Diagnose invalid arithmetic on two function pointers. |
| 5784 | static void diagnoseArithmeticOnTwoFunctionPointers(Sema &S, SourceLocation Loc, |
| 5785 | Expr *LHS, Expr *RHS) { |
| 5786 | assert(LHS->getType()->isAnyPointerType()); |
| 5787 | assert(RHS->getType()->isAnyPointerType()); |
| 5788 | S.Diag(Loc, S.getLangOptions().CPlusPlus |
| 5789 | ? diag::err_typecheck_pointer_arith_function_type |
| 5790 | : diag::ext_gnu_ptr_func_arith) |
| 5791 | << 1 /* two pointers */ << LHS->getType()->getPointeeType() |
| 5792 | // We only show the second type if it differs from the first. |
| 5793 | << (unsigned)!S.Context.hasSameUnqualifiedType(LHS->getType(), |
| 5794 | RHS->getType()) |
| 5795 | << RHS->getType()->getPointeeType() |
| 5796 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 5797 | } |
| 5798 | |
| 5799 | /// \brief Diagnose invalid arithmetic on a function pointer. |
| 5800 | static void diagnoseArithmeticOnFunctionPointer(Sema &S, SourceLocation Loc, |
| 5801 | Expr *Pointer) { |
| 5802 | assert(Pointer->getType()->isAnyPointerType()); |
| 5803 | S.Diag(Loc, S.getLangOptions().CPlusPlus |
| 5804 | ? diag::err_typecheck_pointer_arith_function_type |
| 5805 | : diag::ext_gnu_ptr_func_arith) |
| 5806 | << 0 /* one pointer */ << Pointer->getType()->getPointeeType() |
| 5807 | << 0 /* one pointer, so only one type */ |
| 5808 | << Pointer->getSourceRange(); |
| 5809 | } |
| 5810 | |
| 5811 | /// \brief Check the validity of an arithmetic pointer operand. |
| 5812 | /// |
| 5813 | /// If the operand has pointer type, this code will check for pointer types |
| 5814 | /// which are invalid in arithmetic operations. These will be diagnosed |
| 5815 | /// appropriately, including whether or not the use is supported as an |
| 5816 | /// extension. |
| 5817 | /// |
| 5818 | /// \returns True when the operand is valid to use (even if as an extension). |
| 5819 | static bool checkArithmeticOpPointerOperand(Sema &S, SourceLocation Loc, |
| 5820 | Expr *Operand) { |
| 5821 | if (!Operand->getType()->isAnyPointerType()) return true; |
| 5822 | |
| 5823 | QualType PointeeTy = Operand->getType()->getPointeeType(); |
| 5824 | if (PointeeTy->isVoidType()) { |
| 5825 | diagnoseArithmeticOnVoidPointer(S, Loc, Operand); |
| 5826 | return !S.getLangOptions().CPlusPlus; |
| 5827 | } |
| 5828 | if (PointeeTy->isFunctionType()) { |
| 5829 | diagnoseArithmeticOnFunctionPointer(S, Loc, Operand); |
| 5830 | return !S.getLangOptions().CPlusPlus; |
| 5831 | } |
| 5832 | |
| 5833 | if ((Operand->getType()->isPointerType() && |
| 5834 | !Operand->getType()->isDependentType()) || |
| 5835 | Operand->getType()->isObjCObjectPointerType()) { |
| 5836 | QualType PointeeTy = Operand->getType()->getPointeeType(); |
| 5837 | if (S.RequireCompleteType( |
| 5838 | Loc, PointeeTy, |
| 5839 | S.PDiag(diag::err_typecheck_arithmetic_incomplete_type) |
| 5840 | << PointeeTy << Operand->getSourceRange())) |
| 5841 | return false; |
| 5842 | } |
| 5843 | |
| 5844 | return true; |
| 5845 | } |
| 5846 | |
| 5847 | /// \brief Check the validity of a binary arithmetic operation w.r.t. pointer |
| 5848 | /// operands. |
| 5849 | /// |
| 5850 | /// This routine will diagnose any invalid arithmetic on pointer operands much |
| 5851 | /// like \see checkArithmeticOpPointerOperand. However, it has special logic |
| 5852 | /// for emitting a single diagnostic even for operations where both LHS and RHS |
| 5853 | /// are (potentially problematic) pointers. |
| 5854 | /// |
| 5855 | /// \returns True when the operand is valid to use (even if as an extension). |
| 5856 | static bool checkArithmeticBinOpPointerOperands(Sema &S, SourceLocation Loc, |
| 5857 | Expr *LHS, Expr *RHS) { |
| 5858 | bool isLHSPointer = LHS->getType()->isAnyPointerType(); |
| 5859 | bool isRHSPointer = RHS->getType()->isAnyPointerType(); |
| 5860 | if (!isLHSPointer && !isRHSPointer) return true; |
| 5861 | |
| 5862 | QualType LHSPointeeTy, RHSPointeeTy; |
| 5863 | if (isLHSPointer) LHSPointeeTy = LHS->getType()->getPointeeType(); |
| 5864 | if (isRHSPointer) RHSPointeeTy = RHS->getType()->getPointeeType(); |
| 5865 | |
| 5866 | // Check for arithmetic on pointers to incomplete types. |
| 5867 | bool isLHSVoidPtr = isLHSPointer && LHSPointeeTy->isVoidType(); |
| 5868 | bool isRHSVoidPtr = isRHSPointer && RHSPointeeTy->isVoidType(); |
| 5869 | if (isLHSVoidPtr || isRHSVoidPtr) { |
| 5870 | if (!isRHSVoidPtr) diagnoseArithmeticOnVoidPointer(S, Loc, LHS); |
| 5871 | else if (!isLHSVoidPtr) diagnoseArithmeticOnVoidPointer(S, Loc, RHS); |
| 5872 | else diagnoseArithmeticOnTwoVoidPointers(S, Loc, LHS, RHS); |
| 5873 | |
| 5874 | return !S.getLangOptions().CPlusPlus; |
| 5875 | } |
| 5876 | |
| 5877 | bool isLHSFuncPtr = isLHSPointer && LHSPointeeTy->isFunctionType(); |
| 5878 | bool isRHSFuncPtr = isRHSPointer && RHSPointeeTy->isFunctionType(); |
| 5879 | if (isLHSFuncPtr || isRHSFuncPtr) { |
| 5880 | if (!isRHSFuncPtr) diagnoseArithmeticOnFunctionPointer(S, Loc, LHS); |
| 5881 | else if (!isLHSFuncPtr) diagnoseArithmeticOnFunctionPointer(S, Loc, RHS); |
| 5882 | else diagnoseArithmeticOnTwoFunctionPointers(S, Loc, LHS, RHS); |
| 5883 | |
| 5884 | return !S.getLangOptions().CPlusPlus; |
| 5885 | } |
| 5886 | |
| 5887 | Expr *Operands[] = { LHS, RHS }; |
| 5888 | for (unsigned i = 0; i < 2; ++i) { |
| 5889 | Expr *Operand = Operands[i]; |
| 5890 | if ((Operand->getType()->isPointerType() && |
| 5891 | !Operand->getType()->isDependentType()) || |
| 5892 | Operand->getType()->isObjCObjectPointerType()) { |
| 5893 | QualType PointeeTy = Operand->getType()->getPointeeType(); |
| 5894 | if (S.RequireCompleteType( |
| 5895 | Loc, PointeeTy, |
| 5896 | S.PDiag(diag::err_typecheck_arithmetic_incomplete_type) |
| 5897 | << PointeeTy << Operand->getSourceRange())) |
| 5898 | return false; |
| 5899 | } |
| 5900 | } |
| 5901 | return true; |
| 5902 | } |
| 5903 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5904 | QualType Sema::CheckAdditionOperands( // C99 6.5.6 |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5905 | ExprResult &lex, ExprResult &rex, SourceLocation Loc, QualType* CompLHSTy) { |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5906 | if (lex.get()->getType()->isVectorType() || |
| 5907 | rex.get()->getType()->isVectorType()) { |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5908 | QualType compType = CheckVectorOperands(lex, rex, Loc, CompLHSTy); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5909 | if (CompLHSTy) *CompLHSTy = compType; |
| 5910 | return compType; |
| 5911 | } |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 5912 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5913 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5914 | if (lex.isInvalid() || rex.isInvalid()) |
| 5915 | return QualType(); |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 5916 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5917 | // handle the common case first (both operands are arithmetic). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5918 | if (lex.get()->getType()->isArithmeticType() && |
| 5919 | rex.get()->getType()->isArithmeticType()) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5920 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 5921 | return compType; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5922 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5923 | |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 5924 | // Put any potential pointer into PExp |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5925 | Expr* PExp = lex.get(), *IExp = rex.get(); |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 5926 | if (IExp->getType()->isAnyPointerType()) |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 5927 | std::swap(PExp, IExp); |
| 5928 | |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 5929 | if (PExp->getType()->isAnyPointerType()) { |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 5930 | if (IExp->getType()->isIntegerType()) { |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 5931 | if (!checkArithmeticOpPointerOperand(*this, Loc, PExp)) |
| 5932 | return QualType(); |
| 5933 | |
Steve Naroff | 760e3c4 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 5934 | QualType PointeeTy = PExp->getType()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5935 | |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 5936 | // Diagnose bad cases where we step over interface counts. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5937 | if (PointeeTy->isObjCObjectType() && LangOpts.ObjCNonFragileABI) { |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 5938 | Diag(Loc, diag::err_arithmetic_nonfragile_interface) |
| 5939 | << PointeeTy << PExp->getSourceRange(); |
| 5940 | return QualType(); |
| 5941 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5942 | |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame^] | 5943 | // Check array bounds for pointer arithemtic |
| 5944 | CheckArrayAccess(PExp, IExp); |
| 5945 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5946 | if (CompLHSTy) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5947 | QualType LHSTy = Context.isPromotableBitField(lex.get()); |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 5948 | if (LHSTy.isNull()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5949 | LHSTy = lex.get()->getType(); |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 5950 | if (LHSTy->isPromotableIntegerType()) |
| 5951 | LHSTy = Context.getPromotedIntegerType(LHSTy); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 5952 | } |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5953 | *CompLHSTy = LHSTy; |
| 5954 | } |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 5955 | return PExp->getType(); |
| 5956 | } |
| 5957 | } |
| 5958 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 5959 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5960 | } |
| 5961 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 5962 | // C99 6.5.6 |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5963 | QualType Sema::CheckSubtractionOperands(ExprResult &lex, ExprResult &rex, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5964 | SourceLocation Loc, |
| 5965 | QualType* CompLHSTy) { |
| 5966 | if (lex.get()->getType()->isVectorType() || |
| 5967 | rex.get()->getType()->isVectorType()) { |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5968 | QualType compType = CheckVectorOperands(lex, rex, Loc, CompLHSTy); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5969 | if (CompLHSTy) *CompLHSTy = compType; |
| 5970 | return compType; |
| 5971 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5972 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5973 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5974 | if (lex.isInvalid() || rex.isInvalid()) |
| 5975 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5976 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5977 | // Enforce type constraints: C99 6.5.6p3. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5978 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5979 | // Handle the common case first (both operands are arithmetic). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5980 | if (lex.get()->getType()->isArithmeticType() && |
| 5981 | rex.get()->getType()->isArithmeticType()) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5982 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 5983 | return compType; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5984 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5985 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5986 | // Either ptr - int or ptr - ptr. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5987 | if (lex.get()->getType()->isAnyPointerType()) { |
| 5988 | QualType lpointee = lex.get()->getType()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5989 | |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 5990 | // Diagnose bad cases where we step over interface counts. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 5991 | if (lpointee->isObjCObjectType() && LangOpts.ObjCNonFragileABI) { |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 5992 | Diag(Loc, diag::err_arithmetic_nonfragile_interface) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5993 | << lpointee << lex.get()->getSourceRange(); |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 5994 | return QualType(); |
| 5995 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5996 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 5997 | // The result type of a pointer-int computation is the pointer type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5998 | if (rex.get()->getType()->isIntegerType()) { |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 5999 | if (!checkArithmeticOpPointerOperand(*this, Loc, lex.get())) |
| 6000 | return QualType(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6001 | |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame^] | 6002 | Expr *IExpr = rex.get()->IgnoreParenCasts(); |
| 6003 | UnaryOperator negRex(IExpr, UO_Minus, IExpr->getType(), VK_RValue, |
| 6004 | OK_Ordinary, IExpr->getExprLoc()); |
| 6005 | // Check array bounds for pointer arithemtic |
| 6006 | CheckArrayAccess(lex.get()->IgnoreParenCasts(), &negRex); |
| 6007 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6008 | if (CompLHSTy) *CompLHSTy = lex.get()->getType(); |
| 6009 | return lex.get()->getType(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6010 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6011 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6012 | // Handle pointer-pointer subtractions. |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6013 | if (const PointerType *RHSPTy |
| 6014 | = rex.get()->getType()->getAs<PointerType>()) { |
Eli Friedman | 8e54ad0 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 6015 | QualType rpointee = RHSPTy->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6016 | |
Eli Friedman | 88d936b | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 6017 | if (getLangOptions().CPlusPlus) { |
| 6018 | // Pointee types must be the same: C++ [expr.add] |
| 6019 | if (!Context.hasSameUnqualifiedType(lpointee, rpointee)) { |
| 6020 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6021 | << lex.get()->getType() << rex.get()->getType() |
| 6022 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Eli Friedman | 88d936b | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 6023 | return QualType(); |
| 6024 | } |
| 6025 | } else { |
| 6026 | // Pointee types must be compatible C99 6.5.6p3 |
| 6027 | if (!Context.typesAreCompatible( |
| 6028 | Context.getCanonicalType(lpointee).getUnqualifiedType(), |
| 6029 | Context.getCanonicalType(rpointee).getUnqualifiedType())) { |
| 6030 | Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6031 | << lex.get()->getType() << rex.get()->getType() |
| 6032 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Eli Friedman | 88d936b | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 6033 | return QualType(); |
| 6034 | } |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6035 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6036 | |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6037 | if (!checkArithmeticBinOpPointerOperands(*this, Loc, |
| 6038 | lex.get(), rex.get())) |
| 6039 | return QualType(); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6040 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6041 | if (CompLHSTy) *CompLHSTy = lex.get()->getType(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6042 | return Context.getPointerDiffType(); |
| 6043 | } |
| 6044 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6045 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6046 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6047 | } |
| 6048 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6049 | static bool isScopedEnumerationType(QualType T) { |
| 6050 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
| 6051 | return ET->getDecl()->isScoped(); |
| 6052 | return false; |
| 6053 | } |
| 6054 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6055 | static void DiagnoseBadShiftValues(Sema& S, ExprResult &lex, ExprResult &rex, |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6056 | SourceLocation Loc, unsigned Opc, |
| 6057 | QualType LHSTy) { |
| 6058 | llvm::APSInt Right; |
| 6059 | // Check right/shifter operand |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6060 | if (rex.get()->isValueDependent() || |
| 6061 | !rex.get()->isIntegerConstantExpr(Right, S.Context)) |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6062 | return; |
| 6063 | |
| 6064 | if (Right.isNegative()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6065 | S.DiagRuntimeBehavior(Loc, rex.get(), |
Ted Kremenek | 082bf7a | 2011-03-01 18:09:31 +0000 | [diff] [blame] | 6066 | S.PDiag(diag::warn_shift_negative) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6067 | << rex.get()->getSourceRange()); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6068 | return; |
| 6069 | } |
| 6070 | llvm::APInt LeftBits(Right.getBitWidth(), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6071 | S.Context.getTypeSize(lex.get()->getType())); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6072 | if (Right.uge(LeftBits)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6073 | S.DiagRuntimeBehavior(Loc, rex.get(), |
Ted Kremenek | 425a31e | 2011-03-01 19:13:22 +0000 | [diff] [blame] | 6074 | S.PDiag(diag::warn_shift_gt_typewidth) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6075 | << rex.get()->getSourceRange()); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6076 | return; |
| 6077 | } |
| 6078 | if (Opc != BO_Shl) |
| 6079 | return; |
| 6080 | |
| 6081 | // When left shifting an ICE which is signed, we can check for overflow which |
| 6082 | // according to C++ has undefined behavior ([expr.shift] 5.8/2). Unsigned |
| 6083 | // integers have defined behavior modulo one more than the maximum value |
| 6084 | // representable in the result type, so never warn for those. |
| 6085 | llvm::APSInt Left; |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6086 | if (lex.get()->isValueDependent() || |
| 6087 | !lex.get()->isIntegerConstantExpr(Left, S.Context) || |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6088 | LHSTy->hasUnsignedIntegerRepresentation()) |
| 6089 | return; |
| 6090 | llvm::APInt ResultBits = |
| 6091 | static_cast<llvm::APInt&>(Right) + Left.getMinSignedBits(); |
| 6092 | if (LeftBits.uge(ResultBits)) |
| 6093 | return; |
| 6094 | llvm::APSInt Result = Left.extend(ResultBits.getLimitedValue()); |
| 6095 | Result = Result.shl(Right); |
| 6096 | |
Ted Kremenek | fa82138 | 2011-06-15 00:54:52 +0000 | [diff] [blame] | 6097 | // Print the bit representation of the signed integer as an unsigned |
| 6098 | // hexadecimal number. |
| 6099 | llvm::SmallString<40> HexResult; |
| 6100 | Result.toString(HexResult, 16, /*Signed =*/false, /*Literal =*/true); |
| 6101 | |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6102 | // If we are only missing a sign bit, this is less likely to result in actual |
| 6103 | // bugs -- if the result is cast back to an unsigned type, it will have the |
| 6104 | // expected value. Thus we place this behind a different warning that can be |
| 6105 | // turned off separately if needed. |
| 6106 | if (LeftBits == ResultBits - 1) { |
Ted Kremenek | fa82138 | 2011-06-15 00:54:52 +0000 | [diff] [blame] | 6107 | S.Diag(Loc, diag::warn_shift_result_sets_sign_bit) |
| 6108 | << HexResult.str() << LHSTy |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6109 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6110 | return; |
| 6111 | } |
| 6112 | |
| 6113 | S.Diag(Loc, diag::warn_shift_result_gt_typewidth) |
Ted Kremenek | fa82138 | 2011-06-15 00:54:52 +0000 | [diff] [blame] | 6114 | << HexResult.str() << Result.getMinSignedBits() << LHSTy |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6115 | << Left.getBitWidth() << lex.get()->getSourceRange() |
| 6116 | << rex.get()->getSourceRange(); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6117 | } |
| 6118 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 6119 | // C99 6.5.7 |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6120 | QualType Sema::CheckShiftOperands(ExprResult &lex, ExprResult &rex, |
| 6121 | SourceLocation Loc, unsigned Opc, |
| 6122 | bool isCompAssign) { |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6123 | // C99 6.5.7p2: Each of the operands shall have integer type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6124 | if (!lex.get()->getType()->hasIntegerRepresentation() || |
| 6125 | !rex.get()->getType()->hasIntegerRepresentation()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6126 | return InvalidOperands(Loc, lex, rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6127 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6128 | // C++0x: Don't allow scoped enums. FIXME: Use something better than |
| 6129 | // hasIntegerRepresentation() above instead of this. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6130 | if (isScopedEnumerationType(lex.get()->getType()) || |
| 6131 | isScopedEnumerationType(rex.get()->getType())) { |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6132 | return InvalidOperands(Loc, lex, rex); |
| 6133 | } |
| 6134 | |
Nate Begeman | 2207d79 | 2009-10-25 02:26:48 +0000 | [diff] [blame] | 6135 | // Vector shifts promote their scalar inputs to vector type. |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6136 | if (lex.get()->getType()->isVectorType() || |
| 6137 | rex.get()->getType()->isVectorType()) |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 6138 | return CheckVectorOperands(lex, rex, Loc, isCompAssign); |
Nate Begeman | 2207d79 | 2009-10-25 02:26:48 +0000 | [diff] [blame] | 6139 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6140 | // Shifts don't perform usual arithmetic conversions, they just do integer |
| 6141 | // promotions on each operand. C99 6.5.7p3 |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6142 | |
John McCall | 1bc80af | 2010-12-16 19:28:59 +0000 | [diff] [blame] | 6143 | // For the LHS, do usual unary conversions, but then reset them away |
| 6144 | // if this is a compound assignment. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6145 | ExprResult old_lex = lex; |
| 6146 | lex = UsualUnaryConversions(lex.take()); |
| 6147 | if (lex.isInvalid()) |
| 6148 | return QualType(); |
| 6149 | QualType LHSTy = lex.get()->getType(); |
John McCall | 1bc80af | 2010-12-16 19:28:59 +0000 | [diff] [blame] | 6150 | if (isCompAssign) lex = old_lex; |
| 6151 | |
| 6152 | // The RHS is simpler. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6153 | rex = UsualUnaryConversions(rex.take()); |
| 6154 | if (rex.isInvalid()) |
| 6155 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6156 | |
Ryan Flynn | d043968 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 6157 | // Sanity-check shift operands |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6158 | DiagnoseBadShiftValues(*this, lex, rex, Loc, Opc, LHSTy); |
Ryan Flynn | d043968 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 6159 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6160 | // "The type of the result is that of the promoted left operand." |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6161 | return LHSTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6162 | } |
| 6163 | |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6164 | static bool IsWithinTemplateSpecialization(Decl *D) { |
| 6165 | if (DeclContext *DC = D->getDeclContext()) { |
| 6166 | if (isa<ClassTemplateSpecializationDecl>(DC)) |
| 6167 | return true; |
| 6168 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) |
| 6169 | return FD->isFunctionTemplateSpecialization(); |
| 6170 | } |
| 6171 | return false; |
| 6172 | } |
| 6173 | |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6174 | // C99 6.5.8, C++ [expr.rel] |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6175 | QualType Sema::CheckCompareOperands(ExprResult &lex, ExprResult &rex, |
| 6176 | SourceLocation Loc, unsigned OpaqueOpc, |
| 6177 | bool isRelational) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6178 | BinaryOperatorKind Opc = (BinaryOperatorKind) OpaqueOpc; |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6179 | |
Chris Lattner | 02dd4b1 | 2009-12-05 05:40:13 +0000 | [diff] [blame] | 6180 | // Handle vector comparisons separately. |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6181 | if (lex.get()->getType()->isVectorType() || |
| 6182 | rex.get()->getType()->isVectorType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6183 | return CheckVectorCompareOperands(lex, rex, Loc, isRelational); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6184 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6185 | QualType lType = lex.get()->getType(); |
| 6186 | QualType rType = rex.get()->getType(); |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 6187 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6188 | Expr *LHSStripped = lex.get()->IgnoreParenImpCasts(); |
| 6189 | Expr *RHSStripped = rex.get()->IgnoreParenImpCasts(); |
Chandler Carruth | 543cb65 | 2011-02-17 08:37:06 +0000 | [diff] [blame] | 6190 | QualType LHSStrippedType = LHSStripped->getType(); |
| 6191 | QualType RHSStrippedType = RHSStripped->getType(); |
| 6192 | |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 6193 | |
| 6194 | |
Chandler Carruth | 543cb65 | 2011-02-17 08:37:06 +0000 | [diff] [blame] | 6195 | // Two different enums will raise a warning when compared. |
| 6196 | if (const EnumType *LHSEnumType = LHSStrippedType->getAs<EnumType>()) { |
| 6197 | if (const EnumType *RHSEnumType = RHSStrippedType->getAs<EnumType>()) { |
| 6198 | if (LHSEnumType->getDecl()->getIdentifier() && |
| 6199 | RHSEnumType->getDecl()->getIdentifier() && |
| 6200 | !Context.hasSameUnqualifiedType(LHSStrippedType, RHSStrippedType)) { |
| 6201 | Diag(Loc, diag::warn_comparison_of_mixed_enum_types) |
| 6202 | << LHSStrippedType << RHSStrippedType |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6203 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Chandler Carruth | 543cb65 | 2011-02-17 08:37:06 +0000 | [diff] [blame] | 6204 | } |
| 6205 | } |
| 6206 | } |
| 6207 | |
Douglas Gregor | 8eee119 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 6208 | if (!lType->hasFloatingRepresentation() && |
Ted Kremenek | fbcb0eb | 2010-09-16 00:03:01 +0000 | [diff] [blame] | 6209 | !(lType->isBlockPointerType() && isRelational) && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6210 | !lex.get()->getLocStart().isMacroID() && |
| 6211 | !rex.get()->getLocStart().isMacroID()) { |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6212 | // For non-floating point types, check for self-comparisons of the form |
| 6213 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 6214 | // often indicate logic errors in the program. |
Chandler Carruth | 64d092c | 2010-07-12 06:23:38 +0000 | [diff] [blame] | 6215 | // |
| 6216 | // NOTE: Don't warn about comparison expressions resulting from macro |
| 6217 | // expansion. Also don't warn about comparisons which are only self |
| 6218 | // comparisons within a template specialization. The warnings should catch |
| 6219 | // obvious cases in the definition of the template anyways. The idea is to |
| 6220 | // warn when the typed comparison operator will always evaluate to the same |
| 6221 | // result. |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6222 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LHSStripped)) { |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6223 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RHSStripped)) { |
Ted Kremenek | fbcb0eb | 2010-09-16 00:03:01 +0000 | [diff] [blame] | 6224 | if (DRL->getDecl() == DRR->getDecl() && |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6225 | !IsWithinTemplateSpecialization(DRL->getDecl())) { |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 6226 | DiagRuntimeBehavior(Loc, 0, PDiag(diag::warn_comparison_always) |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6227 | << 0 // self- |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6228 | << (Opc == BO_EQ |
| 6229 | || Opc == BO_LE |
| 6230 | || Opc == BO_GE)); |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6231 | } else if (lType->isArrayType() && rType->isArrayType() && |
| 6232 | !DRL->getDecl()->getType()->isReferenceType() && |
| 6233 | !DRR->getDecl()->getType()->isReferenceType()) { |
| 6234 | // what is it always going to eval to? |
| 6235 | char always_evals_to; |
| 6236 | switch(Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6237 | case BO_EQ: // e.g. array1 == array2 |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6238 | always_evals_to = 0; // false |
| 6239 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6240 | case BO_NE: // e.g. array1 != array2 |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6241 | always_evals_to = 1; // true |
| 6242 | break; |
| 6243 | default: |
| 6244 | // best we can say is 'a constant' |
| 6245 | always_evals_to = 2; // e.g. array1 <= array2 |
| 6246 | break; |
| 6247 | } |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 6248 | DiagRuntimeBehavior(Loc, 0, PDiag(diag::warn_comparison_always) |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6249 | << 1 // array |
| 6250 | << always_evals_to); |
| 6251 | } |
| 6252 | } |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6253 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6254 | |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6255 | if (isa<CastExpr>(LHSStripped)) |
| 6256 | LHSStripped = LHSStripped->IgnoreParenCasts(); |
| 6257 | if (isa<CastExpr>(RHSStripped)) |
| 6258 | RHSStripped = RHSStripped->IgnoreParenCasts(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6259 | |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6260 | // Warn about comparisons against a string constant (unless the other |
| 6261 | // operand is null), the user probably wants strcmp. |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6262 | Expr *literalString = 0; |
| 6263 | Expr *literalStringStripped = 0; |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6264 | if ((isa<StringLiteral>(LHSStripped) || isa<ObjCEncodeExpr>(LHSStripped)) && |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6265 | !RHSStripped->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6266 | Expr::NPC_ValueDependentIsNull)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6267 | literalString = lex.get(); |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6268 | literalStringStripped = LHSStripped; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 6269 | } else if ((isa<StringLiteral>(RHSStripped) || |
| 6270 | isa<ObjCEncodeExpr>(RHSStripped)) && |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6271 | !LHSStripped->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6272 | Expr::NPC_ValueDependentIsNull)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6273 | literalString = rex.get(); |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6274 | literalStringStripped = RHSStripped; |
| 6275 | } |
| 6276 | |
| 6277 | if (literalString) { |
| 6278 | std::string resultComparison; |
| 6279 | switch (Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6280 | case BO_LT: resultComparison = ") < 0"; break; |
| 6281 | case BO_GT: resultComparison = ") > 0"; break; |
| 6282 | case BO_LE: resultComparison = ") <= 0"; break; |
| 6283 | case BO_GE: resultComparison = ") >= 0"; break; |
| 6284 | case BO_EQ: resultComparison = ") == 0"; break; |
| 6285 | case BO_NE: resultComparison = ") != 0"; break; |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6286 | default: assert(false && "Invalid comparison operator"); |
| 6287 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6288 | |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 6289 | DiagRuntimeBehavior(Loc, 0, |
Douglas Gregor | d1e4d9b | 2010-01-12 23:18:54 +0000 | [diff] [blame] | 6290 | PDiag(diag::warn_stringcompare) |
| 6291 | << isa<ObjCEncodeExpr>(literalStringStripped) |
Ted Kremenek | 03a4bee | 2010-04-09 20:26:53 +0000 | [diff] [blame] | 6292 | << literalString->getSourceRange()); |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6293 | } |
Ted Kremenek | 3ca0bf2 | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 6294 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6295 | |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6296 | // C99 6.5.8p3 / C99 6.5.9p4 |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6297 | if (lex.get()->getType()->isArithmeticType() && |
| 6298 | rex.get()->getType()->isArithmeticType()) { |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6299 | UsualArithmeticConversions(lex, rex); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6300 | if (lex.isInvalid() || rex.isInvalid()) |
| 6301 | return QualType(); |
| 6302 | } |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6303 | else { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6304 | lex = UsualUnaryConversions(lex.take()); |
| 6305 | if (lex.isInvalid()) |
| 6306 | return QualType(); |
| 6307 | |
| 6308 | rex = UsualUnaryConversions(rex.take()); |
| 6309 | if (rex.isInvalid()) |
| 6310 | return QualType(); |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6311 | } |
| 6312 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6313 | lType = lex.get()->getType(); |
| 6314 | rType = rex.get()->getType(); |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6315 | |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6316 | // The result of comparisons is 'bool' in C++, 'int' in C. |
Argyrios Kyrtzidis | 16f744b | 2011-02-18 20:55:15 +0000 | [diff] [blame] | 6317 | QualType ResultTy = Context.getLogicalOperationType(); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6318 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 6319 | if (isRelational) { |
| 6320 | if (lType->isRealType() && rType->isRealType()) |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6321 | return ResultTy; |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 6322 | } else { |
Ted Kremenek | 72cb1ae | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 6323 | // Check for comparisons of floating point operands using != and ==. |
Douglas Gregor | 8eee119 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 6324 | if (lType->hasFloatingRepresentation()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6325 | CheckFloatComparison(Loc, lex.get(), rex.get()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6326 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 6327 | if (lType->isArithmeticType() && rType->isArithmeticType()) |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6328 | return ResultTy; |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 6329 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6330 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6331 | bool LHSIsNull = lex.get()->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6332 | Expr::NPC_ValueDependentIsNull); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6333 | bool RHSIsNull = rex.get()->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6334 | Expr::NPC_ValueDependentIsNull); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6335 | |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6336 | // All of the following pointer-related warnings are GCC extensions, except |
| 6337 | // when handling null pointer constants. |
Steve Naroff | 77878cc | 2007-08-27 04:08:11 +0000 | [diff] [blame] | 6338 | if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2 |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 6339 | QualType LCanPointeeTy = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6340 | Context.getCanonicalType(lType->getAs<PointerType>()->getPointeeType()); |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 6341 | QualType RCanPointeeTy = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6342 | Context.getCanonicalType(rType->getAs<PointerType>()->getPointeeType()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6343 | |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6344 | if (getLangOptions().CPlusPlus) { |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 6345 | if (LCanPointeeTy == RCanPointeeTy) |
| 6346 | return ResultTy; |
Fariborz Jahanian | 51874dd | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 6347 | if (!isRelational && |
| 6348 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 6349 | // Valid unless comparison between non-null pointer and function pointer |
| 6350 | // This is a gcc extension compatibility comparison. |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6351 | // In a SFINAE context, we treat this as a hard error to maintain |
| 6352 | // conformance with the C++ standard. |
Fariborz Jahanian | 51874dd | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 6353 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
| 6354 | && !LHSIsNull && !RHSIsNull) { |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6355 | Diag(Loc, |
| 6356 | isSFINAEContext()? |
| 6357 | diag::err_typecheck_comparison_of_fptr_to_void |
| 6358 | : diag::ext_typecheck_comparison_of_fptr_to_void) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6359 | << lType << rType << lex.get()->getSourceRange() |
| 6360 | << rex.get()->getSourceRange(); |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6361 | |
| 6362 | if (isSFINAEContext()) |
| 6363 | return QualType(); |
| 6364 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6365 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
Fariborz Jahanian | 51874dd | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 6366 | return ResultTy; |
| 6367 | } |
| 6368 | } |
Anders Carlsson | 0c8209e | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 6369 | |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6370 | // C++ [expr.rel]p2: |
| 6371 | // [...] Pointer conversions (4.10) and qualification |
| 6372 | // conversions (4.4) are performed on pointer operands (or on |
| 6373 | // a pointer operand and a null pointer constant) to bring |
| 6374 | // them to their composite pointer type. [...] |
| 6375 | // |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6376 | // C++ [expr.eq]p1 uses the same notion for (in)equality |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6377 | // comparisons of pointers. |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6378 | bool NonStandardCompositeType = false; |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 6379 | QualType T = FindCompositePointerType(Loc, lex, rex, |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6380 | isSFINAEContext()? 0 : &NonStandardCompositeType); |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6381 | if (T.isNull()) { |
| 6382 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_pointers) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6383 | << lType << rType << lex.get()->getSourceRange() |
| 6384 | << rex.get()->getSourceRange(); |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6385 | return QualType(); |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6386 | } else if (NonStandardCompositeType) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6387 | Diag(Loc, |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6388 | diag::ext_typecheck_comparison_of_distinct_pointers_nonstandard) |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6389 | << lType << rType << T |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6390 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6391 | } |
| 6392 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6393 | lex = ImpCastExprToType(lex.take(), T, CK_BitCast); |
| 6394 | rex = ImpCastExprToType(rex.take(), T, CK_BitCast); |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6395 | return ResultTy; |
| 6396 | } |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 6397 | // C99 6.5.9p2 and C99 6.5.8p2 |
| 6398 | if (Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(), |
| 6399 | RCanPointeeTy.getUnqualifiedType())) { |
| 6400 | // Valid unless a relational comparison of function pointers |
| 6401 | if (isRelational && LCanPointeeTy->isFunctionType()) { |
| 6402 | Diag(Loc, diag::ext_typecheck_ordered_comparison_of_function_pointers) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6403 | << lType << rType << lex.get()->getSourceRange() |
| 6404 | << rex.get()->getSourceRange(); |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 6405 | } |
| 6406 | } else if (!isRelational && |
| 6407 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 6408 | // Valid unless comparison between non-null pointer and function pointer |
| 6409 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
| 6410 | && !LHSIsNull && !RHSIsNull) { |
| 6411 | Diag(Loc, diag::ext_typecheck_comparison_of_fptr_to_void) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6412 | << lType << rType << lex.get()->getSourceRange() |
| 6413 | << rex.get()->getSourceRange(); |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 6414 | } |
| 6415 | } else { |
| 6416 | // Invalid |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 6417 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6418 | << lType << rType << lex.get()->getSourceRange() |
| 6419 | << rex.get()->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6420 | } |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6421 | if (LCanPointeeTy != RCanPointeeTy) { |
| 6422 | if (LHSIsNull && !RHSIsNull) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6423 | lex = ImpCastExprToType(lex.take(), rType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6424 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6425 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6426 | } |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6427 | return ResultTy; |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 6428 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6429 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 6430 | if (getLangOptions().CPlusPlus) { |
Anders Carlsson | 0c8209e | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 6431 | // Comparison of nullptr_t with itself. |
| 6432 | if (lType->isNullPtrType() && rType->isNullPtrType()) |
| 6433 | return ResultTy; |
| 6434 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6435 | // Comparison of pointers with null pointer constants and equality |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6436 | // comparisons of member pointers to null pointer constants. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6437 | if (RHSIsNull && |
Douglas Gregor | 17e37c7 | 2011-06-01 15:12:24 +0000 | [diff] [blame] | 6438 | ((lType->isAnyPointerType() || lType->isNullPtrType()) || |
Douglas Gregor | 16cd4b7 | 2011-06-16 18:52:05 +0000 | [diff] [blame] | 6439 | (!isRelational && |
| 6440 | (lType->isMemberPointerType() || lType->isBlockPointerType())))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6441 | rex = ImpCastExprToType(rex.take(), lType, |
Douglas Gregor | 443c212 | 2010-08-07 13:36:37 +0000 | [diff] [blame] | 6442 | lType->isMemberPointerType() |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6443 | ? CK_NullToMemberPointer |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 6444 | : CK_NullToPointer); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 6445 | return ResultTy; |
| 6446 | } |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6447 | if (LHSIsNull && |
Douglas Gregor | 17e37c7 | 2011-06-01 15:12:24 +0000 | [diff] [blame] | 6448 | ((rType->isAnyPointerType() || rType->isNullPtrType()) || |
Douglas Gregor | 16cd4b7 | 2011-06-16 18:52:05 +0000 | [diff] [blame] | 6449 | (!isRelational && |
| 6450 | (rType->isMemberPointerType() || rType->isBlockPointerType())))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6451 | lex = ImpCastExprToType(lex.take(), rType, |
Douglas Gregor | 443c212 | 2010-08-07 13:36:37 +0000 | [diff] [blame] | 6452 | rType->isMemberPointerType() |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6453 | ? CK_NullToMemberPointer |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 6454 | : CK_NullToPointer); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 6455 | return ResultTy; |
| 6456 | } |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6457 | |
| 6458 | // Comparison of member pointers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6459 | if (!isRelational && |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6460 | lType->isMemberPointerType() && rType->isMemberPointerType()) { |
| 6461 | // C++ [expr.eq]p2: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6462 | // In addition, pointers to members can be compared, or a pointer to |
| 6463 | // member and a null pointer constant. Pointer to member conversions |
| 6464 | // (4.11) and qualification conversions (4.4) are performed to bring |
| 6465 | // them to a common type. If one operand is a null pointer constant, |
| 6466 | // the common type is the type of the other operand. Otherwise, the |
| 6467 | // common type is a pointer to member type similar (4.4) to the type |
| 6468 | // of one of the operands, with a cv-qualification signature (4.4) |
| 6469 | // that is the union of the cv-qualification signatures of the operand |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6470 | // types. |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6471 | bool NonStandardCompositeType = false; |
Douglas Gregor | 8f00dcf | 2010-04-16 23:20:25 +0000 | [diff] [blame] | 6472 | QualType T = FindCompositePointerType(Loc, lex, rex, |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6473 | isSFINAEContext()? 0 : &NonStandardCompositeType); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6474 | if (T.isNull()) { |
| 6475 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_pointers) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6476 | << lType << rType << lex.get()->getSourceRange() |
| 6477 | << rex.get()->getSourceRange(); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6478 | return QualType(); |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6479 | } else if (NonStandardCompositeType) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6480 | Diag(Loc, |
Douglas Gregor | b2cb1cb | 2010-02-25 22:29:57 +0000 | [diff] [blame] | 6481 | diag::ext_typecheck_comparison_of_distinct_pointers_nonstandard) |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6482 | << lType << rType << T |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6483 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6484 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6485 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6486 | lex = ImpCastExprToType(lex.take(), T, CK_BitCast); |
| 6487 | rex = ImpCastExprToType(rex.take(), T, CK_BitCast); |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6488 | return ResultTy; |
| 6489 | } |
Douglas Gregor | 90566c0 | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 6490 | |
| 6491 | // Handle scoped enumeration types specifically, since they don't promote |
| 6492 | // to integers. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6493 | if (lex.get()->getType()->isEnumeralType() && |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6494 | Context.hasSameUnqualifiedType(lex.get()->getType(), |
| 6495 | rex.get()->getType())) |
Douglas Gregor | 90566c0 | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 6496 | return ResultTy; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 6497 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6498 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6499 | // Handle block pointer types. |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6500 | if (!isRelational && lType->isBlockPointerType() && |
| 6501 | rType->isBlockPointerType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6502 | QualType lpointee = lType->getAs<BlockPointerType>()->getPointeeType(); |
| 6503 | QualType rpointee = rType->getAs<BlockPointerType>()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6504 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6505 | if (!LHSIsNull && !RHSIsNull && |
Eli Friedman | 26784c1 | 2009-06-08 05:08:54 +0000 | [diff] [blame] | 6506 | !Context.typesAreCompatible(lpointee, rpointee)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 6507 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6508 | << lType << rType << lex.get()->getSourceRange() |
| 6509 | << rex.get()->getSourceRange(); |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6510 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6511 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6512 | return ResultTy; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6513 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6514 | |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 6515 | // Allow block pointers to be compared with null pointer constants. |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 6516 | if (!isRelational |
| 6517 | && ((lType->isBlockPointerType() && rType->isPointerType()) |
| 6518 | || (lType->isPointerType() && rType->isBlockPointerType()))) { |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 6519 | if (!LHSIsNull && !RHSIsNull) { |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6520 | if (!((rType->isPointerType() && rType->castAs<PointerType>() |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 6521 | ->getPointeeType()->isVoidType()) |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6522 | || (lType->isPointerType() && lType->castAs<PointerType>() |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 6523 | ->getPointeeType()->isVoidType()))) |
| 6524 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6525 | << lType << rType << lex.get()->getSourceRange() |
| 6526 | << rex.get()->getSourceRange(); |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 6527 | } |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6528 | if (LHSIsNull && !RHSIsNull) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6529 | lex = ImpCastExprToType(lex.take(), rType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6530 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6531 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6532 | return ResultTy; |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 6533 | } |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6534 | |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6535 | if (lType->isObjCObjectPointerType() || rType->isObjCObjectPointerType()) { |
| 6536 | const PointerType *LPT = lType->getAs<PointerType>(); |
| 6537 | const PointerType *RPT = rType->getAs<PointerType>(); |
| 6538 | if (LPT || RPT) { |
| 6539 | bool LPtrToVoid = LPT ? LPT->getPointeeType()->isVoidType() : false; |
| 6540 | bool RPtrToVoid = RPT ? RPT->getPointeeType()->isVoidType() : false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6541 | |
Steve Naroff | a8069f1 | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 6542 | if (!LPtrToVoid && !RPtrToVoid && |
| 6543 | !Context.typesAreCompatible(lType, rType)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 6544 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6545 | << lType << rType << lex.get()->getSourceRange() |
| 6546 | << rex.get()->getSourceRange(); |
Steve Naroff | a5ad863 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 6547 | } |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6548 | if (LHSIsNull && !RHSIsNull) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6549 | lex = ImpCastExprToType(lex.take(), rType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6550 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6551 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6552 | return ResultTy; |
Steve Naroff | 87f3b93 | 2008-10-20 18:19:10 +0000 | [diff] [blame] | 6553 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6554 | if (lType->isObjCObjectPointerType() && rType->isObjCObjectPointerType()) { |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 6555 | if (!Context.areComparableObjCPointerTypes(lType, rType)) |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6556 | Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6557 | << lType << rType << lex.get()->getSourceRange() |
| 6558 | << rex.get()->getSourceRange(); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6559 | if (LHSIsNull && !RHSIsNull) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6560 | lex = ImpCastExprToType(lex.take(), rType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6561 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6562 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6563 | return ResultTy; |
Steve Naroff | 2037322 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 6564 | } |
Fariborz Jahanian | 7359f04 | 2007-12-20 01:06:58 +0000 | [diff] [blame] | 6565 | } |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6566 | if ((lType->isAnyPointerType() && rType->isIntegerType()) || |
| 6567 | (lType->isIntegerType() && rType->isAnyPointerType())) { |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6568 | unsigned DiagID = 0; |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6569 | bool isError = false; |
| 6570 | if ((LHSIsNull && lType->isIntegerType()) || |
| 6571 | (RHSIsNull && rType->isIntegerType())) { |
| 6572 | if (isRelational && !getLangOptions().CPlusPlus) |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6573 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_and_zero; |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6574 | } else if (isRelational && !getLangOptions().CPlusPlus) |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6575 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_integer; |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6576 | else if (getLangOptions().CPlusPlus) { |
| 6577 | DiagID = diag::err_typecheck_comparison_of_pointer_integer; |
| 6578 | isError = true; |
| 6579 | } else |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6580 | DiagID = diag::ext_typecheck_comparison_of_pointer_integer; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6581 | |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6582 | if (DiagID) { |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 6583 | Diag(Loc, DiagID) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6584 | << lType << rType << lex.get()->getSourceRange() |
| 6585 | << rex.get()->getSourceRange(); |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6586 | if (isError) |
| 6587 | return QualType(); |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 6588 | } |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6589 | |
| 6590 | if (lType->isIntegerType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6591 | lex = ImpCastExprToType(lex.take(), rType, |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 6592 | LHSIsNull ? CK_NullToPointer : CK_IntegralToPointer); |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6593 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6594 | rex = ImpCastExprToType(rex.take(), lType, |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 6595 | RHSIsNull ? CK_NullToPointer : CK_IntegralToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6596 | return ResultTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6597 | } |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6598 | |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 6599 | // Handle block pointers. |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 6600 | if (!isRelational && RHSIsNull |
| 6601 | && lType->isBlockPointerType() && rType->isIntegerType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6602 | rex = ImpCastExprToType(rex.take(), lType, CK_NullToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6603 | return ResultTy; |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 6604 | } |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 6605 | if (!isRelational && LHSIsNull |
| 6606 | && lType->isIntegerType() && rType->isBlockPointerType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6607 | lex = ImpCastExprToType(lex.take(), rType, CK_NullToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6608 | return ResultTy; |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 6609 | } |
Douglas Gregor | 90566c0 | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 6610 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6611 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6612 | } |
| 6613 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6614 | /// CheckVectorCompareOperands - vector comparisons are a clang extension that |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6615 | /// operates on extended vector types. Instead of producing an IntTy result, |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6616 | /// like a scalar comparison, a vector comparison produces a vector of integer |
| 6617 | /// types. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6618 | QualType Sema::CheckVectorCompareOperands(ExprResult &lex, ExprResult &rex, |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6619 | SourceLocation Loc, |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6620 | bool isRelational) { |
| 6621 | // Check to make sure we're operating on vectors of the same type and width, |
| 6622 | // Allowing one side to be a scalar of element type. |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 6623 | QualType vType = CheckVectorOperands(lex, rex, Loc, /*isCompAssign*/false); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6624 | if (vType.isNull()) |
| 6625 | return vType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6626 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6627 | QualType lType = lex.get()->getType(); |
| 6628 | QualType rType = rex.get()->getType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6629 | |
Anton Yartsev | 7870b13 | 2011-03-27 15:36:07 +0000 | [diff] [blame] | 6630 | // If AltiVec, the comparison results in a numeric type, i.e. |
| 6631 | // bool for C++, int for C |
Anton Yartsev | 6305f72 | 2011-03-28 21:00:05 +0000 | [diff] [blame] | 6632 | if (vType->getAs<VectorType>()->getVectorKind() == VectorType::AltiVecVector) |
Anton Yartsev | 7870b13 | 2011-03-27 15:36:07 +0000 | [diff] [blame] | 6633 | return Context.getLogicalOperationType(); |
| 6634 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6635 | // For non-floating point types, check for self-comparisons of the form |
| 6636 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 6637 | // often indicate logic errors in the program. |
Douglas Gregor | 8eee119 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 6638 | if (!lType->hasFloatingRepresentation()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6639 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex.get()->IgnoreParens())) |
| 6640 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex.get()->IgnoreParens())) |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6641 | if (DRL->getDecl() == DRR->getDecl()) |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 6642 | DiagRuntimeBehavior(Loc, 0, |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6643 | PDiag(diag::warn_comparison_always) |
| 6644 | << 0 // self- |
| 6645 | << 2 // "a constant" |
| 6646 | ); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6647 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6648 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6649 | // Check for comparisons of floating point operands using != and ==. |
Douglas Gregor | 8eee119 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 6650 | if (!isRelational && lType->hasFloatingRepresentation()) { |
| 6651 | assert (rType->hasFloatingRepresentation()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6652 | CheckFloatComparison(Loc, lex.get(), rex.get()); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6653 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6654 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6655 | // Return the type for the comparison, which is the same as vector type for |
| 6656 | // integer vectors, or an integer type of identical size and number of |
| 6657 | // elements for floating point vectors. |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 6658 | if (lType->hasIntegerRepresentation()) |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6659 | return lType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6660 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6661 | const VectorType *VTy = lType->getAs<VectorType>(); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6662 | unsigned TypeSize = Context.getTypeSize(VTy->getElementType()); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6663 | if (TypeSize == Context.getTypeSize(Context.IntTy)) |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6664 | return Context.getExtVectorType(Context.IntTy, VTy->getNumElements()); |
Chris Lattner | d013aa1 | 2009-03-31 07:46:52 +0000 | [diff] [blame] | 6665 | if (TypeSize == Context.getTypeSize(Context.LongTy)) |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6666 | return Context.getExtVectorType(Context.LongTy, VTy->getNumElements()); |
| 6667 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6668 | assert(TypeSize == Context.getTypeSize(Context.LongLongTy) && |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6669 | "Unhandled vector element size in vector compare"); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6670 | return Context.getExtVectorType(Context.LongLongTy, VTy->getNumElements()); |
| 6671 | } |
| 6672 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6673 | inline QualType Sema::CheckBitwiseOperands( |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6674 | ExprResult &lex, ExprResult &rex, SourceLocation Loc, bool isCompAssign) { |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6675 | if (lex.get()->getType()->isVectorType() || |
| 6676 | rex.get()->getType()->isVectorType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6677 | if (lex.get()->getType()->hasIntegerRepresentation() && |
| 6678 | rex.get()->getType()->hasIntegerRepresentation()) |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 6679 | return CheckVectorOperands(lex, rex, Loc, isCompAssign); |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 6680 | |
| 6681 | return InvalidOperands(Loc, lex, rex); |
| 6682 | } |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 6683 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6684 | ExprResult lexResult = Owned(lex), rexResult = Owned(rex); |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6685 | QualType compType = UsualArithmeticConversions(lexResult, rexResult, |
| 6686 | isCompAssign); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6687 | if (lexResult.isInvalid() || rexResult.isInvalid()) |
| 6688 | return QualType(); |
| 6689 | lex = lexResult.take(); |
| 6690 | rex = rexResult.take(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6691 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6692 | if (lex.get()->getType()->isIntegralOrUnscopedEnumerationType() && |
| 6693 | rex.get()->getType()->isIntegralOrUnscopedEnumerationType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 6694 | return compType; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6695 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6696 | } |
| 6697 | |
| 6698 | inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6699 | ExprResult &lex, ExprResult &rex, SourceLocation Loc, unsigned Opc) { |
Chris Lattner | 90a8f27 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 6700 | |
| 6701 | // Diagnose cases where the user write a logical and/or but probably meant a |
| 6702 | // bitwise one. We do this when the LHS is a non-bool integer and the RHS |
| 6703 | // is a constant. |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6704 | if (lex.get()->getType()->isIntegerType() && |
| 6705 | !lex.get()->getType()->isBooleanType() && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6706 | rex.get()->getType()->isIntegerType() && !rex.get()->isValueDependent() && |
Richard Trieu | e5adf59 | 2011-07-15 00:00:51 +0000 | [diff] [blame] | 6707 | // Don't warn in macros or template instantiations. |
| 6708 | !Loc.isMacroID() && ActiveTemplateInstantiations.empty()) { |
Chris Lattner | b7690b4 | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 6709 | // If the RHS can be constant folded, and if it constant folds to something |
| 6710 | // that isn't 0 or 1 (which indicate a potential logical operation that |
| 6711 | // happened to fold to true/false) then warn. |
Chandler Carruth | 0683a14 | 2011-05-31 05:41:42 +0000 | [diff] [blame] | 6712 | // Parens on the RHS are ignored. |
Chris Lattner | b7690b4 | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 6713 | Expr::EvalResult Result; |
Chandler Carruth | 0683a14 | 2011-05-31 05:41:42 +0000 | [diff] [blame] | 6714 | if (rex.get()->Evaluate(Result, Context) && !Result.HasSideEffects) |
| 6715 | if ((getLangOptions().Bool && !rex.get()->getType()->isBooleanType()) || |
| 6716 | (Result.Val.getInt() != 0 && Result.Val.getInt() != 1)) { |
| 6717 | Diag(Loc, diag::warn_logical_instead_of_bitwise) |
| 6718 | << rex.get()->getSourceRange() |
| 6719 | << (Opc == BO_LAnd ? "&&" : "||") |
| 6720 | << (Opc == BO_LAnd ? "&" : "|"); |
Chris Lattner | b7690b4 | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 6721 | } |
| 6722 | } |
Chris Lattner | 90a8f27 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 6723 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 6724 | if (!Context.getLangOptions().CPlusPlus) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6725 | lex = UsualUnaryConversions(lex.take()); |
| 6726 | if (lex.isInvalid()) |
| 6727 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6728 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6729 | rex = UsualUnaryConversions(rex.take()); |
| 6730 | if (rex.isInvalid()) |
| 6731 | return QualType(); |
| 6732 | |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6733 | if (!lex.get()->getType()->isScalarType() || |
| 6734 | !rex.get()->getType()->isScalarType()) |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 6735 | return InvalidOperands(Loc, lex, rex); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6736 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 6737 | return Context.IntTy; |
Anders Carlsson | 0490501 | 2009-10-16 01:44:21 +0000 | [diff] [blame] | 6738 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6739 | |
John McCall | 75f7c0f | 2010-06-04 00:29:51 +0000 | [diff] [blame] | 6740 | // The following is safe because we only use this method for |
| 6741 | // non-overloadable operands. |
| 6742 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 6743 | // C++ [expr.log.and]p1 |
| 6744 | // C++ [expr.log.or]p1 |
John McCall | 75f7c0f | 2010-06-04 00:29:51 +0000 | [diff] [blame] | 6745 | // The operands are both contextually converted to type bool. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6746 | ExprResult lexRes = PerformContextuallyConvertToBool(lex.get()); |
| 6747 | if (lexRes.isInvalid()) |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 6748 | return InvalidOperands(Loc, lex, rex); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6749 | lex = move(lexRes); |
| 6750 | |
| 6751 | ExprResult rexRes = PerformContextuallyConvertToBool(rex.get()); |
| 6752 | if (rexRes.isInvalid()) |
| 6753 | return InvalidOperands(Loc, lex, rex); |
| 6754 | rex = move(rexRes); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6755 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 6756 | // C++ [expr.log.and]p2 |
| 6757 | // C++ [expr.log.or]p2 |
| 6758 | // The result is a bool. |
| 6759 | return Context.BoolTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6760 | } |
| 6761 | |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 6762 | /// IsReadonlyProperty - Verify that otherwise a valid l-value expression |
| 6763 | /// is a read-only property; return true if so. A readonly property expression |
| 6764 | /// depends on various declarations and thus must be treated specially. |
| 6765 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6766 | static bool IsReadonlyProperty(Expr *E, Sema &S) { |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 6767 | if (E->getStmtClass() == Expr::ObjCPropertyRefExprClass) { |
| 6768 | const ObjCPropertyRefExpr* PropExpr = cast<ObjCPropertyRefExpr>(E); |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 6769 | if (PropExpr->isImplicitProperty()) return false; |
| 6770 | |
| 6771 | ObjCPropertyDecl *PDecl = PropExpr->getExplicitProperty(); |
| 6772 | QualType BaseType = PropExpr->isSuperReceiver() ? |
| 6773 | PropExpr->getSuperReceiverType() : |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 6774 | PropExpr->getBase()->getType(); |
| 6775 | |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 6776 | if (const ObjCObjectPointerType *OPT = |
| 6777 | BaseType->getAsObjCInterfacePointerType()) |
| 6778 | if (ObjCInterfaceDecl *IFace = OPT->getInterfaceDecl()) |
| 6779 | if (S.isPropertyReadonly(PDecl, IFace)) |
| 6780 | return true; |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 6781 | } |
| 6782 | return false; |
| 6783 | } |
| 6784 | |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 6785 | static bool IsConstProperty(Expr *E, Sema &S) { |
| 6786 | if (E->getStmtClass() == Expr::ObjCPropertyRefExprClass) { |
| 6787 | const ObjCPropertyRefExpr* PropExpr = cast<ObjCPropertyRefExpr>(E); |
| 6788 | if (PropExpr->isImplicitProperty()) return false; |
| 6789 | |
| 6790 | ObjCPropertyDecl *PDecl = PropExpr->getExplicitProperty(); |
| 6791 | QualType T = PDecl->getType(); |
| 6792 | if (T->isReferenceType()) |
Fariborz Jahanian | 61750f2 | 2011-03-30 16:59:30 +0000 | [diff] [blame] | 6793 | T = T->getAs<ReferenceType>()->getPointeeType(); |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 6794 | CanQualType CT = S.Context.getCanonicalType(T); |
| 6795 | return CT.isConstQualified(); |
| 6796 | } |
| 6797 | return false; |
| 6798 | } |
| 6799 | |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 6800 | static bool IsReadonlyMessage(Expr *E, Sema &S) { |
| 6801 | if (E->getStmtClass() != Expr::MemberExprClass) |
| 6802 | return false; |
| 6803 | const MemberExpr *ME = cast<MemberExpr>(E); |
| 6804 | NamedDecl *Member = ME->getMemberDecl(); |
| 6805 | if (isa<FieldDecl>(Member)) { |
| 6806 | Expr *Base = ME->getBase()->IgnoreParenImpCasts(); |
| 6807 | if (Base->getStmtClass() != Expr::ObjCMessageExprClass) |
| 6808 | return false; |
| 6809 | return cast<ObjCMessageExpr>(Base)->getMethodDecl() != 0; |
| 6810 | } |
| 6811 | return false; |
| 6812 | } |
| 6813 | |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6814 | /// CheckForModifiableLvalue - Verify that E is a modifiable lvalue. If not, |
| 6815 | /// emit an error and return true. If so, return false. |
| 6816 | static bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S) { |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 6817 | SourceLocation OrigLoc = Loc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6818 | Expr::isModifiableLvalueResult IsLV = E->isModifiableLvalue(S.Context, |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 6819 | &Loc); |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 6820 | if (IsLV == Expr::MLV_Valid && IsReadonlyProperty(E, S)) |
| 6821 | IsLV = Expr::MLV_ReadonlyProperty; |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 6822 | else if (Expr::MLV_ConstQualified && IsConstProperty(E, S)) |
| 6823 | IsLV = Expr::MLV_Valid; |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 6824 | else if (IsLV == Expr::MLV_ClassTemporary && IsReadonlyMessage(E, S)) |
| 6825 | IsLV = Expr::MLV_InvalidMessageExpression; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6826 | if (IsLV == Expr::MLV_Valid) |
| 6827 | return false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6828 | |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6829 | unsigned Diag = 0; |
| 6830 | bool NeedType = false; |
| 6831 | switch (IsLV) { // C99 6.5.16p2 |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6832 | case Expr::MLV_ConstQualified: |
| 6833 | Diag = diag::err_typecheck_assign_const; |
| 6834 | |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 6835 | // In ARC, use some specialized diagnostics for occasions where we |
| 6836 | // infer 'const'. These are always pseudo-strong variables. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6837 | if (S.getLangOptions().ObjCAutoRefCount) { |
| 6838 | DeclRefExpr *declRef = dyn_cast<DeclRefExpr>(E->IgnoreParenCasts()); |
| 6839 | if (declRef && isa<VarDecl>(declRef->getDecl())) { |
| 6840 | VarDecl *var = cast<VarDecl>(declRef->getDecl()); |
| 6841 | |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 6842 | // Use the normal diagnostic if it's pseudo-__strong but the |
| 6843 | // user actually wrote 'const'. |
| 6844 | if (var->isARCPseudoStrong() && |
| 6845 | (!var->getTypeSourceInfo() || |
| 6846 | !var->getTypeSourceInfo()->getType().isConstQualified())) { |
| 6847 | // There are two pseudo-strong cases: |
| 6848 | // - self |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6849 | ObjCMethodDecl *method = S.getCurMethodDecl(); |
| 6850 | if (method && var == method->getSelfDecl()) |
| 6851 | Diag = diag::err_typecheck_arr_assign_self; |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 6852 | |
| 6853 | // - fast enumeration variables |
| 6854 | else |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6855 | Diag = diag::err_typecheck_arr_assign_enumeration; |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 6856 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6857 | SourceRange Assign; |
| 6858 | if (Loc != OrigLoc) |
| 6859 | Assign = SourceRange(OrigLoc, OrigLoc); |
| 6860 | S.Diag(Loc, Diag) << E->getSourceRange() << Assign; |
| 6861 | // We need to preserve the AST regardless, so migration tool |
| 6862 | // can do its job. |
| 6863 | return false; |
| 6864 | } |
| 6865 | } |
| 6866 | } |
| 6867 | |
| 6868 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6869 | case Expr::MLV_ArrayType: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6870 | Diag = diag::err_typecheck_array_not_modifiable_lvalue; |
| 6871 | NeedType = true; |
| 6872 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6873 | case Expr::MLV_NotObjectType: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6874 | Diag = diag::err_typecheck_non_object_not_modifiable_lvalue; |
| 6875 | NeedType = true; |
| 6876 | break; |
Chris Lattner | ca354fa | 2008-11-17 19:51:54 +0000 | [diff] [blame] | 6877 | case Expr::MLV_LValueCast: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6878 | Diag = diag::err_typecheck_lvalue_casts_not_supported; |
| 6879 | break; |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 6880 | case Expr::MLV_Valid: |
| 6881 | llvm_unreachable("did not take early return for MLV_Valid"); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6882 | case Expr::MLV_InvalidExpression: |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 6883 | case Expr::MLV_MemberFunction: |
| 6884 | case Expr::MLV_ClassTemporary: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6885 | Diag = diag::err_typecheck_expression_not_modifiable_lvalue; |
| 6886 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6887 | case Expr::MLV_IncompleteType: |
| 6888 | case Expr::MLV_IncompleteVoidType: |
Douglas Gregor | 86447ec | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 6889 | return S.RequireCompleteType(Loc, E->getType(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 6890 | S.PDiag(diag::err_typecheck_incomplete_type_not_modifiable_lvalue) |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 6891 | << E->getSourceRange()); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6892 | case Expr::MLV_DuplicateVectorComponents: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6893 | Diag = diag::err_typecheck_duplicate_vector_components_not_mlvalue; |
| 6894 | break; |
Steve Naroff | 4f6a7d7 | 2008-09-26 14:41:28 +0000 | [diff] [blame] | 6895 | case Expr::MLV_NotBlockQualified: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6896 | Diag = diag::err_block_decl_ref_not_modifiable_lvalue; |
| 6897 | break; |
Fariborz Jahanian | 5daf570 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 6898 | case Expr::MLV_ReadonlyProperty: |
| 6899 | Diag = diag::error_readonly_property_assignment; |
| 6900 | break; |
Fariborz Jahanian | ba8d2d6 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 6901 | case Expr::MLV_NoSetterProperty: |
| 6902 | Diag = diag::error_nosetter_property_assignment; |
| 6903 | break; |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 6904 | case Expr::MLV_InvalidMessageExpression: |
| 6905 | Diag = diag::error_readonly_message_assignment; |
| 6906 | break; |
Fariborz Jahanian | 2514a30 | 2009-12-15 23:59:41 +0000 | [diff] [blame] | 6907 | case Expr::MLV_SubObjCPropertySetting: |
| 6908 | Diag = diag::error_no_subobject_property_setting; |
| 6909 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6910 | } |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 6911 | |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 6912 | SourceRange Assign; |
| 6913 | if (Loc != OrigLoc) |
| 6914 | Assign = SourceRange(OrigLoc, OrigLoc); |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6915 | if (NeedType) |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 6916 | S.Diag(Loc, Diag) << E->getType() << E->getSourceRange() << Assign; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6917 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6918 | S.Diag(Loc, Diag) << E->getSourceRange() << Assign; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6919 | return true; |
| 6920 | } |
| 6921 | |
| 6922 | |
| 6923 | |
| 6924 | // C99 6.5.16.1 |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6925 | QualType Sema::CheckAssignmentOperands(Expr *LHS, ExprResult &RHS, |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6926 | SourceLocation Loc, |
| 6927 | QualType CompoundType) { |
| 6928 | // Verify that LHS is a modifiable lvalue, and emit error if not. |
| 6929 | if (CheckForModifiableLvalue(LHS, Loc, *this)) |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6930 | return QualType(); |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6931 | |
| 6932 | QualType LHSType = LHS->getType(); |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6933 | QualType RHSType = CompoundType.isNull() ? RHS.get()->getType() : |
| 6934 | CompoundType; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6935 | AssignConvertType ConvTy; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6936 | if (CompoundType.isNull()) { |
Fariborz Jahanian | e2a901a | 2010-06-07 22:02:01 +0000 | [diff] [blame] | 6937 | QualType LHSTy(LHSType); |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 6938 | // Simple assignment "x = y". |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6939 | if (LHS->getObjectKind() == OK_ObjCProperty) { |
| 6940 | ExprResult LHSResult = Owned(LHS); |
| 6941 | ConvertPropertyForLValue(LHSResult, RHS, LHSTy); |
| 6942 | if (LHSResult.isInvalid()) |
| 6943 | return QualType(); |
| 6944 | LHS = LHSResult.take(); |
| 6945 | } |
Fariborz Jahanian | e2a901a | 2010-06-07 22:02:01 +0000 | [diff] [blame] | 6946 | ConvTy = CheckSingleAssignmentConstraints(LHSTy, RHS); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6947 | if (RHS.isInvalid()) |
| 6948 | return QualType(); |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 6949 | // Special case of NSObject attributes on c-style pointer types. |
| 6950 | if (ConvTy == IncompatiblePointer && |
| 6951 | ((Context.isObjCNSObjectType(LHSType) && |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 6952 | RHSType->isObjCObjectPointerType()) || |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 6953 | (Context.isObjCNSObjectType(RHSType) && |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 6954 | LHSType->isObjCObjectPointerType()))) |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 6955 | ConvTy = Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6956 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 6957 | if (ConvTy == Compatible && |
| 6958 | getLangOptions().ObjCNonFragileABI && |
| 6959 | LHSType->isObjCObjectType()) |
| 6960 | Diag(Loc, diag::err_assignment_requires_nonfragile_object) |
| 6961 | << LHSType; |
| 6962 | |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 6963 | // If the RHS is a unary plus or minus, check to see if they = and + are |
| 6964 | // right next to each other. If so, the user may have typo'd "x =+ 4" |
| 6965 | // instead of "x += 4". |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6966 | Expr *RHSCheck = RHS.get(); |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 6967 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RHSCheck)) |
| 6968 | RHSCheck = ICE->getSubExpr(); |
| 6969 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(RHSCheck)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6970 | if ((UO->getOpcode() == UO_Plus || |
| 6971 | UO->getOpcode() == UO_Minus) && |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6972 | Loc.isFileID() && UO->getOperatorLoc().isFileID() && |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 6973 | // Only if the two operators are exactly adjacent. |
Chris Lattner | 399bd1b | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 6974 | Loc.getFileLocWithOffset(1) == UO->getOperatorLoc() && |
| 6975 | // And there is a space or other character before the subexpr of the |
| 6976 | // unary +/-. We don't want to warn on "x=-1". |
Chris Lattner | 3e87209 | 2009-03-09 07:11:10 +0000 | [diff] [blame] | 6977 | Loc.getFileLocWithOffset(2) != UO->getSubExpr()->getLocStart() && |
| 6978 | UO->getSubExpr()->getLocStart().isFileID()) { |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 6979 | Diag(Loc, diag::warn_not_compound_assign) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6980 | << (UO->getOpcode() == UO_Plus ? "+" : "-") |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 6981 | << SourceRange(UO->getOperatorLoc(), UO->getOperatorLoc()); |
Chris Lattner | 399bd1b | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 6982 | } |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 6983 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6984 | |
| 6985 | if (ConvTy == Compatible) { |
| 6986 | if (LHSType.getObjCLifetime() == Qualifiers::OCL_Strong) |
| 6987 | checkRetainCycles(LHS, RHS.get()); |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 6988 | else if (getLangOptions().ObjCAutoRefCount) |
| 6989 | checkUnsafeExprAssigns(Loc, LHS, RHS.get()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6990 | } |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 6991 | } else { |
| 6992 | // Compound assignment "x += y" |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 6993 | ConvTy = CheckAssignmentConstraints(Loc, LHSType, RHSType); |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 6994 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6995 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6996 | if (DiagnoseAssignmentResult(ConvTy, Loc, LHSType, RHSType, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6997 | RHS.get(), AA_Assigning)) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6998 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6999 | |
Argyrios Kyrtzidis | 8a285ae | 2011-04-26 17:41:22 +0000 | [diff] [blame] | 7000 | CheckForNullPointerDereference(*this, LHS); |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame^] | 7001 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7002 | // C99 6.5.16p3: The type of an assignment expression is the type of the |
| 7003 | // left operand unless the left operand has qualified type, in which case |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7004 | // it is the unqualified version of the type of the left operand. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7005 | // C99 6.5.16.1p2: In simple assignment, the value of the right operand |
| 7006 | // is converted to the type of the assignment expression (above). |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 7007 | // C++ 5.17p1: the type of the assignment expression is that of its left |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 7008 | // operand. |
John McCall | 2bf6f49 | 2010-10-12 02:19:57 +0000 | [diff] [blame] | 7009 | return (getLangOptions().CPlusPlus |
| 7010 | ? LHSType : LHSType.getUnqualifiedType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7011 | } |
| 7012 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7013 | // C99 6.5.17 |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7014 | static QualType CheckCommaOperands(Sema &S, ExprResult &LHS, ExprResult &RHS, |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7015 | SourceLocation Loc) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7016 | S.DiagnoseUnusedExprResult(LHS.get()); |
Argyrios Kyrtzidis | 2597345 | 2010-06-30 10:53:14 +0000 | [diff] [blame] | 7017 | |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 7018 | LHS = S.CheckPlaceholderExpr(LHS.take()); |
| 7019 | RHS = S.CheckPlaceholderExpr(RHS.take()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7020 | if (LHS.isInvalid() || RHS.isInvalid()) |
Douglas Gregor | 7ad5d42 | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 7021 | return QualType(); |
| 7022 | |
John McCall | cf2e506 | 2010-10-12 07:14:40 +0000 | [diff] [blame] | 7023 | // C's comma performs lvalue conversion (C99 6.3.2.1) on both its |
| 7024 | // operands, but not unary promotions. |
| 7025 | // C++'s comma does not do any conversions at all (C++ [expr.comma]p1). |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 7026 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7027 | // So we treat the LHS as a ignored value, and in C++ we allow the |
| 7028 | // containing site to determine what should be done with the RHS. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7029 | LHS = S.IgnoredValueConversions(LHS.take()); |
| 7030 | if (LHS.isInvalid()) |
| 7031 | return QualType(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7032 | |
| 7033 | if (!S.getLangOptions().CPlusPlus) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7034 | RHS = S.DefaultFunctionArrayLvalueConversion(RHS.take()); |
| 7035 | if (RHS.isInvalid()) |
| 7036 | return QualType(); |
| 7037 | if (!RHS.get()->getType()->isVoidType()) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 7038 | S.RequireCompleteType(Loc, RHS.get()->getType(), |
| 7039 | diag::err_incomplete_type); |
John McCall | cf2e506 | 2010-10-12 07:14:40 +0000 | [diff] [blame] | 7040 | } |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 7041 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7042 | return RHS.get()->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7043 | } |
| 7044 | |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 7045 | /// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine |
| 7046 | /// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7047 | static QualType CheckIncrementDecrementOperand(Sema &S, Expr *Op, |
| 7048 | ExprValueKind &VK, |
| 7049 | SourceLocation OpLoc, |
| 7050 | bool isInc, bool isPrefix) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7051 | if (Op->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7052 | return S.Context.DependentTy; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7053 | |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7054 | QualType ResType = Op->getType(); |
| 7055 | assert(!ResType.isNull() && "no type for increment/decrement expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7056 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7057 | if (S.getLangOptions().CPlusPlus && ResType->isBooleanType()) { |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 7058 | // Decrement of bool is not allowed. |
| 7059 | if (!isInc) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7060 | S.Diag(OpLoc, diag::err_decrement_bool) << Op->getSourceRange(); |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 7061 | return QualType(); |
| 7062 | } |
| 7063 | // Increment of bool sets it to true, but is deprecated. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7064 | S.Diag(OpLoc, diag::warn_increment_bool) << Op->getSourceRange(); |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 7065 | } else if (ResType->isRealType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7066 | // OK! |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 7067 | } else if (ResType->isAnyPointerType()) { |
| 7068 | QualType PointeeTy = ResType->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7069 | |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7070 | // C99 6.5.2.4p2, 6.5.6p2 |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 7071 | if (!checkArithmeticOpPointerOperand(S, OpLoc, Op)) |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 7072 | return QualType(); |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 7073 | |
Fariborz Jahanian | 9f8a04f | 2009-07-16 17:59:14 +0000 | [diff] [blame] | 7074 | // Diagnose bad cases where we step over interface counts. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7075 | else if (PointeeTy->isObjCObjectType() && S.LangOpts.ObjCNonFragileABI) { |
| 7076 | S.Diag(OpLoc, diag::err_arithmetic_nonfragile_interface) |
Fariborz Jahanian | 9f8a04f | 2009-07-16 17:59:14 +0000 | [diff] [blame] | 7077 | << PointeeTy << Op->getSourceRange(); |
| 7078 | return QualType(); |
| 7079 | } |
Eli Friedman | 5b088a1 | 2010-01-03 00:20:48 +0000 | [diff] [blame] | 7080 | } else if (ResType->isAnyComplexType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7081 | // C99 does not support ++/-- on complex types, we allow as an extension. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7082 | S.Diag(OpLoc, diag::ext_integer_increment_complex) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 7083 | << ResType << Op->getSourceRange(); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7084 | } else if (ResType->isPlaceholderType()) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 7085 | ExprResult PR = S.CheckPlaceholderExpr(Op); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7086 | if (PR.isInvalid()) return QualType(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7087 | return CheckIncrementDecrementOperand(S, PR.take(), VK, OpLoc, |
| 7088 | isInc, isPrefix); |
Anton Yartsev | 683564a | 2011-02-07 02:17:30 +0000 | [diff] [blame] | 7089 | } else if (S.getLangOptions().AltiVec && ResType->isVectorType()) { |
| 7090 | // OK! ( C/C++ Language Extensions for CBEA(Version 2.6) 10.3 ) |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7091 | } else { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7092 | S.Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement) |
Douglas Gregor | 5cc07df | 2009-12-15 16:44:32 +0000 | [diff] [blame] | 7093 | << ResType << int(isInc) << Op->getSourceRange(); |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7094 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7095 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7096 | // At this point, we know we have a real, complex or pointer type. |
Steve Naroff | dd10e02 | 2007-08-23 21:37:33 +0000 | [diff] [blame] | 7097 | // Now make sure the operand is a modifiable lvalue. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7098 | if (CheckForModifiableLvalue(Op, OpLoc, S)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7099 | return QualType(); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 7100 | // In C++, a prefix increment is the same type as the operand. Otherwise |
| 7101 | // (in C or with postfix), the increment is the unqualified type of the |
| 7102 | // operand. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7103 | if (isPrefix && S.getLangOptions().CPlusPlus) { |
| 7104 | VK = VK_LValue; |
| 7105 | return ResType; |
| 7106 | } else { |
| 7107 | VK = VK_RValue; |
| 7108 | return ResType.getUnqualifiedType(); |
| 7109 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7110 | } |
| 7111 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7112 | ExprResult Sema::ConvertPropertyForRValue(Expr *E) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7113 | assert(E->getValueKind() == VK_LValue && |
| 7114 | E->getObjectKind() == OK_ObjCProperty); |
| 7115 | const ObjCPropertyRefExpr *PRE = E->getObjCProperty(); |
| 7116 | |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 7117 | QualType T = E->getType(); |
| 7118 | QualType ReceiverType; |
| 7119 | if (PRE->isObjectReceiver()) |
| 7120 | ReceiverType = PRE->getBase()->getType(); |
| 7121 | else if (PRE->isSuperReceiver()) |
| 7122 | ReceiverType = PRE->getSuperReceiverType(); |
| 7123 | else |
| 7124 | ReceiverType = Context.getObjCInterfaceType(PRE->getClassReceiver()); |
| 7125 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7126 | ExprValueKind VK = VK_RValue; |
| 7127 | if (PRE->isImplicitProperty()) { |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 7128 | if (ObjCMethodDecl *GetterMethod = |
Fariborz Jahanian | 99130e5 | 2010-12-22 19:46:35 +0000 | [diff] [blame] | 7129 | PRE->getImplicitPropertyGetter()) { |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 7130 | T = getMessageSendResultType(ReceiverType, GetterMethod, |
| 7131 | PRE->isClassReceiver(), |
| 7132 | PRE->isSuperReceiver()); |
| 7133 | VK = Expr::getValueKindForType(GetterMethod->getResultType()); |
Fariborz Jahanian | 99130e5 | 2010-12-22 19:46:35 +0000 | [diff] [blame] | 7134 | } |
| 7135 | else { |
| 7136 | Diag(PRE->getLocation(), diag::err_getter_not_found) |
| 7137 | << PRE->getBase()->getType(); |
| 7138 | } |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7139 | } |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 7140 | |
| 7141 | E = ImplicitCastExpr::Create(Context, T, CK_GetObjCProperty, |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7142 | E, 0, VK); |
John McCall | db67e2f | 2010-12-10 01:49:45 +0000 | [diff] [blame] | 7143 | |
| 7144 | ExprResult Result = MaybeBindToTemporary(E); |
| 7145 | if (!Result.isInvalid()) |
| 7146 | E = Result.take(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7147 | |
| 7148 | return Owned(E); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7149 | } |
| 7150 | |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 7151 | void Sema::ConvertPropertyForLValue(ExprResult &LHS, ExprResult &RHS, |
| 7152 | QualType &LHSTy) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7153 | assert(LHS.get()->getValueKind() == VK_LValue && |
| 7154 | LHS.get()->getObjectKind() == OK_ObjCProperty); |
| 7155 | const ObjCPropertyRefExpr *PropRef = LHS.get()->getObjCProperty(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7156 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7157 | bool Consumed = false; |
| 7158 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7159 | if (PropRef->isImplicitProperty()) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7160 | // If using property-dot syntax notation for assignment, and there is a |
| 7161 | // setter, RHS expression is being passed to the setter argument. So, |
| 7162 | // type conversion (and comparison) is RHS to setter's argument type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7163 | if (const ObjCMethodDecl *SetterMD = PropRef->getImplicitPropertySetter()) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7164 | ObjCMethodDecl::param_iterator P = SetterMD->param_begin(); |
| 7165 | LHSTy = (*P)->getType(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7166 | Consumed = (getLangOptions().ObjCAutoRefCount && |
| 7167 | (*P)->hasAttr<NSConsumedAttr>()); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7168 | |
| 7169 | // Otherwise, if the getter returns an l-value, just call that. |
| 7170 | } else { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7171 | QualType Result = PropRef->getImplicitPropertyGetter()->getResultType(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7172 | ExprValueKind VK = Expr::getValueKindForType(Result); |
| 7173 | if (VK == VK_LValue) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7174 | LHS = ImplicitCastExpr::Create(Context, LHS.get()->getType(), |
| 7175 | CK_GetObjCProperty, LHS.take(), 0, VK); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7176 | return; |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 7177 | } |
Fariborz Jahanian | c4e1a68 | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 7178 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7179 | } else if (getLangOptions().ObjCAutoRefCount) { |
| 7180 | const ObjCMethodDecl *setter |
| 7181 | = PropRef->getExplicitProperty()->getSetterMethodDecl(); |
| 7182 | if (setter) { |
| 7183 | ObjCMethodDecl::param_iterator P = setter->param_begin(); |
| 7184 | LHSTy = (*P)->getType(); |
| 7185 | Consumed = (*P)->hasAttr<NSConsumedAttr>(); |
| 7186 | } |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7187 | } |
| 7188 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7189 | if ((getLangOptions().CPlusPlus && LHSTy->isRecordType()) || |
| 7190 | getLangOptions().ObjCAutoRefCount) { |
Fariborz Jahanian | c4e1a68 | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 7191 | InitializedEntity Entity = |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7192 | InitializedEntity::InitializeParameter(Context, LHSTy, Consumed); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7193 | ExprResult ArgE = PerformCopyInitialization(Entity, SourceLocation(), RHS); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7194 | if (!ArgE.isInvalid()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7195 | RHS = ArgE; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7196 | if (getLangOptions().ObjCAutoRefCount && !PropRef->isSuperReceiver()) |
| 7197 | checkRetainCycles(const_cast<Expr*>(PropRef->getBase()), RHS.get()); |
| 7198 | } |
Fariborz Jahanian | c4e1a68 | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 7199 | } |
| 7200 | } |
| 7201 | |
| 7202 | |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 7203 | /// getPrimaryDecl - Helper function for CheckAddressOfOperand(). |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7204 | /// This routine allows us to typecheck complex/recursive expressions |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7205 | /// where the declaration is needed for type checking. We only need to |
| 7206 | /// handle cases when the expression references a function designator |
| 7207 | /// or is an lvalue. Here are some examples: |
| 7208 | /// - &(x) => x |
| 7209 | /// - &*****f => f for f a function designator. |
| 7210 | /// - &s.xx => s |
| 7211 | /// - &s.zz[1].yy -> s, if zz is an array |
| 7212 | /// - *(x + 1) -> x, if x is an array |
| 7213 | /// - &"123"[2] -> 0 |
| 7214 | /// - & __real__ x -> x |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7215 | static ValueDecl *getPrimaryDecl(Expr *E) { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7216 | switch (E->getStmtClass()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7217 | case Stmt::DeclRefExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7218 | return cast<DeclRefExpr>(E)->getDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7219 | case Stmt::MemberExprClass: |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7220 | // If this is an arrow operator, the address is an offset from |
| 7221 | // the base's value, so the object the base refers to is |
| 7222 | // irrelevant. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7223 | if (cast<MemberExpr>(E)->isArrow()) |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 7224 | return 0; |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7225 | // Otherwise, the expression refers to a part of the base |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7226 | return getPrimaryDecl(cast<MemberExpr>(E)->getBase()); |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 7227 | case Stmt::ArraySubscriptExprClass: { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 7228 | // FIXME: This code shouldn't be necessary! We should catch the implicit |
| 7229 | // promotion of register arrays earlier. |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7230 | Expr* Base = cast<ArraySubscriptExpr>(E)->getBase(); |
| 7231 | if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Base)) { |
| 7232 | if (ICE->getSubExpr()->getType()->isArrayType()) |
| 7233 | return getPrimaryDecl(ICE->getSubExpr()); |
| 7234 | } |
| 7235 | return 0; |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 7236 | } |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7237 | case Stmt::UnaryOperatorClass: { |
| 7238 | UnaryOperator *UO = cast<UnaryOperator>(E); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7239 | |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7240 | switch(UO->getOpcode()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7241 | case UO_Real: |
| 7242 | case UO_Imag: |
| 7243 | case UO_Extension: |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7244 | return getPrimaryDecl(UO->getSubExpr()); |
| 7245 | default: |
| 7246 | return 0; |
| 7247 | } |
| 7248 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7249 | case Stmt::ParenExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7250 | return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr()); |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 7251 | case Stmt::ImplicitCastExprClass: |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7252 | // If the result of an implicit cast is an l-value, we care about |
| 7253 | // the sub-expression; otherwise, the result here doesn't matter. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7254 | return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7255 | default: |
| 7256 | return 0; |
| 7257 | } |
| 7258 | } |
| 7259 | |
| 7260 | /// CheckAddressOfOperand - The operand of & must be either a function |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7261 | /// designator or an lvalue designating an object. If it is an lvalue, the |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7262 | /// object cannot be declared with storage class register or be a bit field. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7263 | /// Note: The usual conversions are *not* applied to the operand of the & |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7264 | /// operator (C99 6.3.2.1p[2-4]), and its result is never an lvalue. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7265 | /// In C++, the operand might be an overloaded function name, in which case |
Douglas Gregor | 904eed3 | 2008-11-10 20:40:00 +0000 | [diff] [blame] | 7266 | /// we allow the '&' but retain the overloaded-function type. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7267 | static QualType CheckAddressOfOperand(Sema &S, Expr *OrigOp, |
| 7268 | SourceLocation OpLoc) { |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7269 | if (OrigOp->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7270 | return S.Context.DependentTy; |
| 7271 | if (OrigOp->getType() == S.Context.OverloadTy) |
| 7272 | return S.Context.OverloadTy; |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 7273 | if (OrigOp->getType() == S.Context.UnknownAnyTy) |
| 7274 | return S.Context.UnknownAnyTy; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 7275 | if (OrigOp->getType() == S.Context.BoundMemberTy) { |
| 7276 | S.Diag(OpLoc, diag::err_invalid_form_pointer_member_function) |
| 7277 | << OrigOp->getSourceRange(); |
| 7278 | return QualType(); |
| 7279 | } |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7280 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 7281 | assert(!OrigOp->getType()->isPlaceholderType()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7282 | |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7283 | // Make sure to ignore parentheses in subsequent checks |
| 7284 | Expr *op = OrigOp->IgnoreParens(); |
Douglas Gregor | 9103bb2 | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 7285 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7286 | if (S.getLangOptions().C99) { |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 7287 | // Implement C99-only parts of addressof rules. |
| 7288 | if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7289 | if (uOp->getOpcode() == UO_Deref) |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 7290 | // Per C99 6.5.3.2, the address of a deref always returns a valid result |
| 7291 | // (assuming the deref expression is valid). |
| 7292 | return uOp->getSubExpr()->getType(); |
| 7293 | } |
| 7294 | // Technically, there should be a check for array subscript |
| 7295 | // expressions here, but the result of one is always an lvalue anyway. |
| 7296 | } |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7297 | ValueDecl *dcl = getPrimaryDecl(op); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 7298 | Expr::LValueClassification lval = op->ClassifyLValue(S.Context); |
Nuno Lopes | 6b6609f | 2008-12-16 22:59:47 +0000 | [diff] [blame] | 7299 | |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 7300 | if (lval == Expr::LV_ClassTemporary) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7301 | bool sfinae = S.isSFINAEContext(); |
| 7302 | S.Diag(OpLoc, sfinae ? diag::err_typecheck_addrof_class_temporary |
| 7303 | : diag::ext_typecheck_addrof_class_temporary) |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 7304 | << op->getType() << op->getSourceRange(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7305 | if (sfinae) |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 7306 | return QualType(); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7307 | } else if (isa<ObjCSelectorExpr>(op)) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7308 | return S.Context.getPointerType(op->getType()); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7309 | } else if (lval == Expr::LV_MemberFunction) { |
| 7310 | // If it's an instance method, make a member pointer. |
| 7311 | // The expression must have exactly the form &A::foo. |
| 7312 | |
| 7313 | // If the underlying expression isn't a decl ref, give up. |
| 7314 | if (!isa<DeclRefExpr>(op)) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7315 | S.Diag(OpLoc, diag::err_invalid_form_pointer_member_function) |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7316 | << OrigOp->getSourceRange(); |
| 7317 | return QualType(); |
| 7318 | } |
| 7319 | DeclRefExpr *DRE = cast<DeclRefExpr>(op); |
| 7320 | CXXMethodDecl *MD = cast<CXXMethodDecl>(DRE->getDecl()); |
| 7321 | |
| 7322 | // The id-expression was parenthesized. |
| 7323 | if (OrigOp != DRE) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7324 | S.Diag(OpLoc, diag::err_parens_pointer_member_function) |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7325 | << OrigOp->getSourceRange(); |
| 7326 | |
| 7327 | // The method was named without a qualifier. |
| 7328 | } else if (!DRE->getQualifier()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7329 | S.Diag(OpLoc, diag::err_unqualified_pointer_member_function) |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7330 | << op->getSourceRange(); |
| 7331 | } |
| 7332 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7333 | return S.Context.getMemberPointerType(op->getType(), |
| 7334 | S.Context.getTypeDeclType(MD->getParent()).getTypePtr()); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7335 | } else if (lval != Expr::LV_Valid && lval != Expr::LV_IncompleteVoidType) { |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 7336 | // C99 6.5.3.2p1 |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7337 | // The operand must be either an l-value or a function designator |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 7338 | if (!op->getType()->isFunctionType()) { |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 7339 | // FIXME: emit more specific diag... |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7340 | S.Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 7341 | << op->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7342 | return QualType(); |
| 7343 | } |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 7344 | } else if (op->getObjectKind() == OK_BitField) { // C99 6.5.3.2p1 |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7345 | // The operand cannot be a bit-field |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7346 | S.Diag(OpLoc, diag::err_typecheck_address_of) |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7347 | << "bit-field" << op->getSourceRange(); |
Douglas Gregor | 86f1940 | 2008-12-20 23:49:58 +0000 | [diff] [blame] | 7348 | return QualType(); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 7349 | } else if (op->getObjectKind() == OK_VectorComponent) { |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7350 | // The operand cannot be an element of a vector |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7351 | S.Diag(OpLoc, diag::err_typecheck_address_of) |
Nate Begeman | b104b1f | 2009-02-15 22:45:20 +0000 | [diff] [blame] | 7352 | << "vector element" << op->getSourceRange(); |
Steve Naroff | bcb2b61 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 7353 | return QualType(); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 7354 | } else if (op->getObjectKind() == OK_ObjCProperty) { |
Fariborz Jahanian | 0337f21 | 2009-07-07 18:50:52 +0000 | [diff] [blame] | 7355 | // cannot take address of a property expression. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7356 | S.Diag(OpLoc, diag::err_typecheck_address_of) |
Fariborz Jahanian | 0337f21 | 2009-07-07 18:50:52 +0000 | [diff] [blame] | 7357 | << "property expression" << op->getSourceRange(); |
| 7358 | return QualType(); |
Steve Naroff | bcb2b61 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 7359 | } else if (dcl) { // C99 6.5.3.2p1 |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7360 | // We have an lvalue with a decl. Make sure the decl is not declared |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7361 | // with the register storage-class specifier. |
| 7362 | if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { |
Fariborz Jahanian | 4020f87 | 2010-08-24 22:21:48 +0000 | [diff] [blame] | 7363 | // in C++ it is not error to take address of a register |
| 7364 | // variable (c++03 7.1.1P3) |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 7365 | if (vd->getStorageClass() == SC_Register && |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7366 | !S.getLangOptions().CPlusPlus) { |
| 7367 | S.Diag(OpLoc, diag::err_typecheck_address_of) |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 7368 | << "register variable" << op->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7369 | return QualType(); |
| 7370 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7371 | } else if (isa<FunctionTemplateDecl>(dcl)) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7372 | return S.Context.OverloadTy; |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7373 | } else if (isa<FieldDecl>(dcl) || isa<IndirectFieldDecl>(dcl)) { |
Douglas Gregor | 2988205 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 7374 | // Okay: we can take the address of a field. |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 7375 | // Could be a pointer to member, though, if there is an explicit |
| 7376 | // scope qualifier for the class. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 7377 | if (isa<DeclRefExpr>(op) && cast<DeclRefExpr>(op)->getQualifier()) { |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 7378 | DeclContext *Ctx = dcl->getDeclContext(); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 7379 | if (Ctx && Ctx->isRecord()) { |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7380 | if (dcl->getType()->isReferenceType()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7381 | S.Diag(OpLoc, |
| 7382 | diag::err_cannot_form_pointer_to_member_of_reference_type) |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7383 | << dcl->getDeclName() << dcl->getType(); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 7384 | return QualType(); |
| 7385 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7386 | |
Argyrios Kyrtzidis | 0413db4 | 2011-01-31 07:04:29 +0000 | [diff] [blame] | 7387 | while (cast<RecordDecl>(Ctx)->isAnonymousStructOrUnion()) |
| 7388 | Ctx = Ctx->getParent(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7389 | return S.Context.getMemberPointerType(op->getType(), |
| 7390 | S.Context.getTypeDeclType(cast<RecordDecl>(Ctx)).getTypePtr()); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 7391 | } |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 7392 | } |
Anders Carlsson | 196f7d0 | 2009-05-16 21:43:42 +0000 | [diff] [blame] | 7393 | } else if (!isa<FunctionDecl>(dcl)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7394 | assert(0 && "Unknown/unexpected decl type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7395 | } |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 7396 | |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 7397 | if (lval == Expr::LV_IncompleteVoidType) { |
| 7398 | // Taking the address of a void variable is technically illegal, but we |
| 7399 | // allow it in cases which are otherwise valid. |
| 7400 | // Example: "extern void x; void* y = &x;". |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7401 | S.Diag(OpLoc, diag::ext_typecheck_addrof_void) << op->getSourceRange(); |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 7402 | } |
| 7403 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7404 | // If the operand has type "type", the result has type "pointer to type". |
Douglas Gregor | 8f70ddb | 2010-07-29 16:05:45 +0000 | [diff] [blame] | 7405 | if (op->getType()->isObjCObjectType()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7406 | return S.Context.getObjCObjectPointerType(op->getType()); |
| 7407 | return S.Context.getPointerType(op->getType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7408 | } |
| 7409 | |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7410 | /// CheckIndirectionOperand - Type check unary indirection (prefix '*'). |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7411 | static QualType CheckIndirectionOperand(Sema &S, Expr *Op, ExprValueKind &VK, |
| 7412 | SourceLocation OpLoc) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7413 | if (Op->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7414 | return S.Context.DependentTy; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7415 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7416 | ExprResult ConvResult = S.UsualUnaryConversions(Op); |
| 7417 | if (ConvResult.isInvalid()) |
| 7418 | return QualType(); |
| 7419 | Op = ConvResult.take(); |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7420 | QualType OpTy = Op->getType(); |
| 7421 | QualType Result; |
Argyrios Kyrtzidis | f4bbbf0 | 2011-05-02 18:21:19 +0000 | [diff] [blame] | 7422 | |
| 7423 | if (isa<CXXReinterpretCastExpr>(Op)) { |
| 7424 | QualType OpOrigType = Op->IgnoreParenCasts()->getType(); |
| 7425 | S.CheckCompatibleReinterpretCast(OpOrigType, OpTy, /*IsDereference*/true, |
| 7426 | Op->getSourceRange()); |
| 7427 | } |
| 7428 | |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7429 | // Note that per both C89 and C99, indirection is always legal, even if OpTy |
| 7430 | // is an incomplete type or void. It would be possible to warn about |
| 7431 | // dereferencing a void pointer, but it's completely well-defined, and such a |
| 7432 | // warning is unlikely to catch any mistakes. |
| 7433 | if (const PointerType *PT = OpTy->getAs<PointerType>()) |
| 7434 | Result = PT->getPointeeType(); |
| 7435 | else if (const ObjCObjectPointerType *OPT = |
| 7436 | OpTy->getAs<ObjCObjectPointerType>()) |
| 7437 | Result = OPT->getPointeeType(); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7438 | else { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 7439 | ExprResult PR = S.CheckPlaceholderExpr(Op); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7440 | if (PR.isInvalid()) return QualType(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7441 | if (PR.take() != Op) |
| 7442 | return CheckIndirectionOperand(S, PR.take(), VK, OpLoc); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7443 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7444 | |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7445 | if (Result.isNull()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7446 | S.Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer) |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7447 | << OpTy << Op->getSourceRange(); |
| 7448 | return QualType(); |
| 7449 | } |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7450 | |
| 7451 | // Dereferences are usually l-values... |
| 7452 | VK = VK_LValue; |
| 7453 | |
| 7454 | // ...except that certain expressions are never l-values in C. |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 7455 | if (!S.getLangOptions().CPlusPlus && Result.isCForbiddenLValueType()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7456 | VK = VK_RValue; |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7457 | |
| 7458 | return Result; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7459 | } |
| 7460 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7461 | static inline BinaryOperatorKind ConvertTokenKindToBinaryOpcode( |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7462 | tok::TokenKind Kind) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7463 | BinaryOperatorKind Opc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7464 | switch (Kind) { |
| 7465 | default: assert(0 && "Unknown binop!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7466 | case tok::periodstar: Opc = BO_PtrMemD; break; |
| 7467 | case tok::arrowstar: Opc = BO_PtrMemI; break; |
| 7468 | case tok::star: Opc = BO_Mul; break; |
| 7469 | case tok::slash: Opc = BO_Div; break; |
| 7470 | case tok::percent: Opc = BO_Rem; break; |
| 7471 | case tok::plus: Opc = BO_Add; break; |
| 7472 | case tok::minus: Opc = BO_Sub; break; |
| 7473 | case tok::lessless: Opc = BO_Shl; break; |
| 7474 | case tok::greatergreater: Opc = BO_Shr; break; |
| 7475 | case tok::lessequal: Opc = BO_LE; break; |
| 7476 | case tok::less: Opc = BO_LT; break; |
| 7477 | case tok::greaterequal: Opc = BO_GE; break; |
| 7478 | case tok::greater: Opc = BO_GT; break; |
| 7479 | case tok::exclaimequal: Opc = BO_NE; break; |
| 7480 | case tok::equalequal: Opc = BO_EQ; break; |
| 7481 | case tok::amp: Opc = BO_And; break; |
| 7482 | case tok::caret: Opc = BO_Xor; break; |
| 7483 | case tok::pipe: Opc = BO_Or; break; |
| 7484 | case tok::ampamp: Opc = BO_LAnd; break; |
| 7485 | case tok::pipepipe: Opc = BO_LOr; break; |
| 7486 | case tok::equal: Opc = BO_Assign; break; |
| 7487 | case tok::starequal: Opc = BO_MulAssign; break; |
| 7488 | case tok::slashequal: Opc = BO_DivAssign; break; |
| 7489 | case tok::percentequal: Opc = BO_RemAssign; break; |
| 7490 | case tok::plusequal: Opc = BO_AddAssign; break; |
| 7491 | case tok::minusequal: Opc = BO_SubAssign; break; |
| 7492 | case tok::lesslessequal: Opc = BO_ShlAssign; break; |
| 7493 | case tok::greatergreaterequal: Opc = BO_ShrAssign; break; |
| 7494 | case tok::ampequal: Opc = BO_AndAssign; break; |
| 7495 | case tok::caretequal: Opc = BO_XorAssign; break; |
| 7496 | case tok::pipeequal: Opc = BO_OrAssign; break; |
| 7497 | case tok::comma: Opc = BO_Comma; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7498 | } |
| 7499 | return Opc; |
| 7500 | } |
| 7501 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7502 | static inline UnaryOperatorKind ConvertTokenKindToUnaryOpcode( |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7503 | tok::TokenKind Kind) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7504 | UnaryOperatorKind Opc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7505 | switch (Kind) { |
| 7506 | default: assert(0 && "Unknown unary op!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7507 | case tok::plusplus: Opc = UO_PreInc; break; |
| 7508 | case tok::minusminus: Opc = UO_PreDec; break; |
| 7509 | case tok::amp: Opc = UO_AddrOf; break; |
| 7510 | case tok::star: Opc = UO_Deref; break; |
| 7511 | case tok::plus: Opc = UO_Plus; break; |
| 7512 | case tok::minus: Opc = UO_Minus; break; |
| 7513 | case tok::tilde: Opc = UO_Not; break; |
| 7514 | case tok::exclaim: Opc = UO_LNot; break; |
| 7515 | case tok::kw___real: Opc = UO_Real; break; |
| 7516 | case tok::kw___imag: Opc = UO_Imag; break; |
| 7517 | case tok::kw___extension__: Opc = UO_Extension; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7518 | } |
| 7519 | return Opc; |
| 7520 | } |
| 7521 | |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 7522 | /// DiagnoseSelfAssignment - Emits a warning if a value is assigned to itself. |
| 7523 | /// This warning is only emitted for builtin assignment operations. It is also |
| 7524 | /// suppressed in the event of macro expansions. |
| 7525 | static void DiagnoseSelfAssignment(Sema &S, Expr *lhs, Expr *rhs, |
| 7526 | SourceLocation OpLoc) { |
| 7527 | if (!S.ActiveTemplateInstantiations.empty()) |
| 7528 | return; |
| 7529 | if (OpLoc.isInvalid() || OpLoc.isMacroID()) |
| 7530 | return; |
| 7531 | lhs = lhs->IgnoreParenImpCasts(); |
| 7532 | rhs = rhs->IgnoreParenImpCasts(); |
| 7533 | const DeclRefExpr *LeftDeclRef = dyn_cast<DeclRefExpr>(lhs); |
| 7534 | const DeclRefExpr *RightDeclRef = dyn_cast<DeclRefExpr>(rhs); |
| 7535 | if (!LeftDeclRef || !RightDeclRef || |
| 7536 | LeftDeclRef->getLocation().isMacroID() || |
| 7537 | RightDeclRef->getLocation().isMacroID()) |
| 7538 | return; |
| 7539 | const ValueDecl *LeftDecl = |
| 7540 | cast<ValueDecl>(LeftDeclRef->getDecl()->getCanonicalDecl()); |
| 7541 | const ValueDecl *RightDecl = |
| 7542 | cast<ValueDecl>(RightDeclRef->getDecl()->getCanonicalDecl()); |
| 7543 | if (LeftDecl != RightDecl) |
| 7544 | return; |
| 7545 | if (LeftDecl->getType().isVolatileQualified()) |
| 7546 | return; |
| 7547 | if (const ReferenceType *RefTy = LeftDecl->getType()->getAs<ReferenceType>()) |
| 7548 | if (RefTy->getPointeeType().isVolatileQualified()) |
| 7549 | return; |
| 7550 | |
| 7551 | S.Diag(OpLoc, diag::warn_self_assignment) |
| 7552 | << LeftDeclRef->getType() |
| 7553 | << lhs->getSourceRange() << rhs->getSourceRange(); |
| 7554 | } |
| 7555 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7556 | /// CreateBuiltinBinOp - Creates a new built-in binary operation with |
| 7557 | /// operator @p Opc at location @c TokLoc. This routine only supports |
| 7558 | /// built-in operations; ActOnBinOp handles overloaded operators. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7559 | ExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, |
Argyrios Kyrtzidis | b1fa3dc | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 7560 | BinaryOperatorKind Opc, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7561 | Expr *lhsExpr, Expr *rhsExpr) { |
| 7562 | ExprResult lhs = Owned(lhsExpr), rhs = Owned(rhsExpr); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7563 | QualType ResultTy; // Result type of the binary operator. |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7564 | // The following two variables are used for compound assignment operators |
| 7565 | QualType CompLHSTy; // Type of LHS after promotions for computation |
| 7566 | QualType CompResultTy; // Type of computation result |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7567 | ExprValueKind VK = VK_RValue; |
| 7568 | ExprObjectKind OK = OK_Ordinary; |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7569 | |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 7570 | // Check if a 'foo<int>' involved in a binary op, identifies a single |
| 7571 | // function unambiguously (i.e. an lvalue ala 13.4) |
| 7572 | // But since an assignment can trigger target based overload, exclude it in |
| 7573 | // our blind search. i.e: |
| 7574 | // template<class T> void f(); template<class T, class U> void f(U); |
| 7575 | // f<int> == 0; // resolve f<int> blindly |
| 7576 | // void (*p)(int); p = f<int>; // resolve f<int> using target |
| 7577 | if (Opc != BO_Assign) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 7578 | ExprResult resolvedLHS = CheckPlaceholderExpr(lhs.get()); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 7579 | if (!resolvedLHS.isUsable()) return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7580 | lhs = move(resolvedLHS); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 7581 | |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 7582 | ExprResult resolvedRHS = CheckPlaceholderExpr(rhs.get()); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 7583 | if (!resolvedRHS.isUsable()) return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7584 | rhs = move(resolvedRHS); |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 7585 | } |
| 7586 | |
Eli Friedman | ed3b256 | 2011-06-17 20:52:22 +0000 | [diff] [blame] | 7587 | // The canonical way to check for a GNU null is with isNullPointerConstant, |
| 7588 | // but we use a bit of a hack here for speed; this is a relatively |
| 7589 | // hot path, and isNullPointerConstant is slow. |
| 7590 | bool LeftNull = isa<GNUNullExpr>(lhs.get()->IgnoreParenImpCasts()); |
| 7591 | bool RightNull = isa<GNUNullExpr>(rhs.get()->IgnoreParenImpCasts()); |
Richard Trieu | 3e95ba9 | 2011-06-16 21:36:56 +0000 | [diff] [blame] | 7592 | |
| 7593 | // Detect when a NULL constant is used improperly in an expression. These |
| 7594 | // are mainly cases where the null pointer is used as an integer instead |
| 7595 | // of a pointer. |
| 7596 | if (LeftNull || RightNull) { |
Chandler Carruth | 1567a8b | 2011-06-20 07:38:51 +0000 | [diff] [blame] | 7597 | // Avoid analyzing cases where the result will either be invalid (and |
| 7598 | // diagnosed as such) or entirely valid and not something to warn about. |
| 7599 | QualType LeftType = lhs.get()->getType(); |
| 7600 | QualType RightType = rhs.get()->getType(); |
| 7601 | if (!LeftType->isBlockPointerType() && !LeftType->isMemberPointerType() && |
| 7602 | !LeftType->isFunctionType() && |
| 7603 | !RightType->isBlockPointerType() && |
| 7604 | !RightType->isMemberPointerType() && |
| 7605 | !RightType->isFunctionType()) { |
| 7606 | if (Opc == BO_Mul || Opc == BO_Div || Opc == BO_Rem || Opc == BO_Add || |
| 7607 | Opc == BO_Sub || Opc == BO_Shl || Opc == BO_Shr || Opc == BO_And || |
| 7608 | Opc == BO_Xor || Opc == BO_Or || Opc == BO_MulAssign || |
| 7609 | Opc == BO_DivAssign || Opc == BO_AddAssign || Opc == BO_SubAssign || |
| 7610 | Opc == BO_RemAssign || Opc == BO_ShlAssign || Opc == BO_ShrAssign || |
| 7611 | Opc == BO_AndAssign || Opc == BO_OrAssign || Opc == BO_XorAssign) { |
| 7612 | // These are the operations that would not make sense with a null pointer |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 7613 | // pointer no matter what the other expression is. |
Chandler Carruth | 2af68e4 | 2011-06-19 09:05:14 +0000 | [diff] [blame] | 7614 | Diag(OpLoc, diag::warn_null_in_arithmetic_operation) |
Chandler Carruth | 1567a8b | 2011-06-20 07:38:51 +0000 | [diff] [blame] | 7615 | << (LeftNull ? lhs.get()->getSourceRange() : SourceRange()) |
| 7616 | << (RightNull ? rhs.get()->getSourceRange() : SourceRange()); |
| 7617 | } else if (Opc == BO_LE || Opc == BO_LT || Opc == BO_GE || Opc == BO_GT || |
| 7618 | Opc == BO_EQ || Opc == BO_NE) { |
| 7619 | // These are the operations that would not make sense with a null pointer |
| 7620 | // if the other expression the other expression is not a pointer. |
| 7621 | if (LeftNull != RightNull && |
| 7622 | !LeftType->isAnyPointerType() && |
| 7623 | !LeftType->canDecayToPointerType() && |
| 7624 | !RightType->isAnyPointerType() && |
| 7625 | !RightType->canDecayToPointerType()) { |
| 7626 | Diag(OpLoc, diag::warn_null_in_arithmetic_operation) |
| 7627 | << (LeftNull ? lhs.get()->getSourceRange() |
| 7628 | : rhs.get()->getSourceRange()); |
| 7629 | } |
Richard Trieu | 3e95ba9 | 2011-06-16 21:36:56 +0000 | [diff] [blame] | 7630 | } |
| 7631 | } |
| 7632 | } |
| 7633 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7634 | switch (Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7635 | case BO_Assign: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7636 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, QualType()); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7637 | if (getLangOptions().CPlusPlus && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7638 | lhs.get()->getObjectKind() != OK_ObjCProperty) { |
| 7639 | VK = lhs.get()->getValueKind(); |
| 7640 | OK = lhs.get()->getObjectKind(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7641 | } |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 7642 | if (!ResultTy.isNull()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7643 | DiagnoseSelfAssignment(*this, lhs.get(), rhs.get(), OpLoc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7644 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7645 | case BO_PtrMemD: |
| 7646 | case BO_PtrMemI: |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7647 | ResultTy = CheckPointerToMemberOperands(lhs, rhs, VK, OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7648 | Opc == BO_PtrMemI); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 7649 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7650 | case BO_Mul: |
| 7651 | case BO_Div: |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 7652 | ResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc, false, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7653 | Opc == BO_Div); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7654 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7655 | case BO_Rem: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7656 | ResultTy = CheckRemainderOperands(lhs, rhs, OpLoc); |
| 7657 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7658 | case BO_Add: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7659 | ResultTy = CheckAdditionOperands(lhs, rhs, OpLoc); |
| 7660 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7661 | case BO_Sub: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7662 | ResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc); |
| 7663 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7664 | case BO_Shl: |
| 7665 | case BO_Shr: |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7666 | ResultTy = CheckShiftOperands(lhs, rhs, OpLoc, Opc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7667 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7668 | case BO_LE: |
| 7669 | case BO_LT: |
| 7670 | case BO_GE: |
| 7671 | case BO_GT: |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7672 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, true); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7673 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7674 | case BO_EQ: |
| 7675 | case BO_NE: |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7676 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, false); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7677 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7678 | case BO_And: |
| 7679 | case BO_Xor: |
| 7680 | case BO_Or: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7681 | ResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc); |
| 7682 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7683 | case BO_LAnd: |
| 7684 | case BO_LOr: |
Chris Lattner | 90a8f27 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 7685 | ResultTy = CheckLogicalOperands(lhs, rhs, OpLoc, Opc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7686 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7687 | case BO_MulAssign: |
| 7688 | case BO_DivAssign: |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 7689 | CompResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc, true, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7690 | Opc == BO_DivAssign); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7691 | CompLHSTy = CompResultTy; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7692 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 7693 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7694 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7695 | case BO_RemAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7696 | CompResultTy = CheckRemainderOperands(lhs, rhs, OpLoc, true); |
| 7697 | CompLHSTy = CompResultTy; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7698 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 7699 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7700 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7701 | case BO_AddAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7702 | CompResultTy = CheckAdditionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7703 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 7704 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7705 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7706 | case BO_SubAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7707 | CompResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7708 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 7709 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7710 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7711 | case BO_ShlAssign: |
| 7712 | case BO_ShrAssign: |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7713 | CompResultTy = CheckShiftOperands(lhs, rhs, OpLoc, Opc, true); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7714 | CompLHSTy = CompResultTy; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7715 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 7716 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7717 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7718 | case BO_AndAssign: |
| 7719 | case BO_XorAssign: |
| 7720 | case BO_OrAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7721 | CompResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc, true); |
| 7722 | CompLHSTy = CompResultTy; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7723 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 7724 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7725 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7726 | case BO_Comma: |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7727 | ResultTy = CheckCommaOperands(*this, lhs, rhs, OpLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7728 | if (getLangOptions().CPlusPlus && !rhs.isInvalid()) { |
| 7729 | VK = rhs.get()->getValueKind(); |
| 7730 | OK = rhs.get()->getObjectKind(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7731 | } |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7732 | break; |
| 7733 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7734 | if (ResultTy.isNull() || lhs.isInvalid() || rhs.isInvalid()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 7735 | return ExprError(); |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame^] | 7736 | |
| 7737 | // Check for array bounds violations for both sides of the BinaryOperator |
| 7738 | CheckArrayAccess(lhs.get()); |
| 7739 | CheckArrayAccess(rhs.get()); |
| 7740 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7741 | if (CompResultTy.isNull()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7742 | return Owned(new (Context) BinaryOperator(lhs.take(), rhs.take(), Opc, |
| 7743 | ResultTy, VK, OK, OpLoc)); |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 7744 | if (getLangOptions().CPlusPlus && lhs.get()->getObjectKind() != |
| 7745 | OK_ObjCProperty) { |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7746 | VK = VK_LValue; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7747 | OK = lhs.get()->getObjectKind(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7748 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7749 | return Owned(new (Context) CompoundAssignOperator(lhs.take(), rhs.take(), Opc, |
| 7750 | ResultTy, VK, OK, CompLHSTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7751 | CompResultTy, OpLoc)); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7752 | } |
| 7753 | |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 7754 | /// DiagnoseBitwisePrecedence - Emit a warning when bitwise and comparison |
| 7755 | /// operators are mixed in a way that suggests that the programmer forgot that |
| 7756 | /// comparison operators have higher precedence. The most typical example of |
| 7757 | /// such code is "flags & 0x0020 != 0", which is equivalent to "flags & 1". |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7758 | static void DiagnoseBitwisePrecedence(Sema &Self, BinaryOperatorKind Opc, |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7759 | SourceLocation OpLoc,Expr *lhs,Expr *rhs){ |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 7760 | typedef BinaryOperator BinOp; |
| 7761 | BinOp::Opcode lhsopc = static_cast<BinOp::Opcode>(-1), |
| 7762 | rhsopc = static_cast<BinOp::Opcode>(-1); |
| 7763 | if (BinOp *BO = dyn_cast<BinOp>(lhs)) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7764 | lhsopc = BO->getOpcode(); |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 7765 | if (BinOp *BO = dyn_cast<BinOp>(rhs)) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7766 | rhsopc = BO->getOpcode(); |
| 7767 | |
| 7768 | // Subs are not binary operators. |
| 7769 | if (lhsopc == -1 && rhsopc == -1) |
| 7770 | return; |
| 7771 | |
| 7772 | // Bitwise operations are sometimes used as eager logical ops. |
| 7773 | // Don't diagnose this. |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 7774 | if ((BinOp::isComparisonOp(lhsopc) || BinOp::isBitwiseOp(lhsopc)) && |
| 7775 | (BinOp::isComparisonOp(rhsopc) || BinOp::isBitwiseOp(rhsopc))) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7776 | return; |
| 7777 | |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 7778 | if (BinOp::isComparisonOp(lhsopc)) { |
| 7779 | Self.Diag(OpLoc, diag::warn_precedence_bitwise_rel) |
| 7780 | << SourceRange(lhs->getLocStart(), OpLoc) |
| 7781 | << BinOp::getOpcodeStr(Opc) << BinOp::getOpcodeStr(lhsopc); |
Sebastian Redl | 6b169ac | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 7782 | SuggestParentheses(Self, OpLoc, |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 7783 | Self.PDiag(diag::note_precedence_bitwise_silence) |
| 7784 | << BinOp::getOpcodeStr(lhsopc), |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 7785 | lhs->getSourceRange()); |
| 7786 | SuggestParentheses(Self, OpLoc, |
Argyrios Kyrtzidis | abdd3b3 | 2011-04-25 23:01:29 +0000 | [diff] [blame] | 7787 | Self.PDiag(diag::note_precedence_bitwise_first) |
| 7788 | << BinOp::getOpcodeStr(Opc), |
| 7789 | SourceRange(cast<BinOp>(lhs)->getRHS()->getLocStart(), rhs->getLocEnd())); |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 7790 | } else if (BinOp::isComparisonOp(rhsopc)) { |
| 7791 | Self.Diag(OpLoc, diag::warn_precedence_bitwise_rel) |
| 7792 | << SourceRange(OpLoc, rhs->getLocEnd()) |
| 7793 | << BinOp::getOpcodeStr(Opc) << BinOp::getOpcodeStr(rhsopc); |
Sebastian Redl | 6b169ac | 2009-10-26 17:01:32 +0000 | [diff] [blame] | 7794 | SuggestParentheses(Self, OpLoc, |
Argyrios Kyrtzidis | abdd3b3 | 2011-04-25 23:01:29 +0000 | [diff] [blame] | 7795 | Self.PDiag(diag::note_precedence_bitwise_silence) |
| 7796 | << BinOp::getOpcodeStr(rhsopc), |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 7797 | rhs->getSourceRange()); |
| 7798 | SuggestParentheses(Self, OpLoc, |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 7799 | Self.PDiag(diag::note_precedence_bitwise_first) |
Douglas Gregor | 827feec | 2010-01-08 00:20:23 +0000 | [diff] [blame] | 7800 | << BinOp::getOpcodeStr(Opc), |
Douglas Gregor | b27c7a1 | 2011-06-22 18:41:08 +0000 | [diff] [blame] | 7801 | SourceRange(lhs->getLocStart(), |
| 7802 | cast<BinOp>(rhs)->getLHS()->getLocStart())); |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 7803 | } |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7804 | } |
| 7805 | |
Argyrios Kyrtzidis | 33f46e2 | 2011-06-20 18:41:26 +0000 | [diff] [blame] | 7806 | /// \brief It accepts a '&' expr that is inside a '|' one. |
| 7807 | /// Emit a diagnostic together with a fixit hint that wraps the '&' expression |
| 7808 | /// in parentheses. |
| 7809 | static void |
| 7810 | EmitDiagnosticForBitwiseAndInBitwiseOr(Sema &Self, SourceLocation OpLoc, |
| 7811 | BinaryOperator *Bop) { |
| 7812 | assert(Bop->getOpcode() == BO_And); |
| 7813 | Self.Diag(Bop->getOperatorLoc(), diag::warn_bitwise_and_in_bitwise_or) |
| 7814 | << Bop->getSourceRange() << OpLoc; |
| 7815 | SuggestParentheses(Self, Bop->getOperatorLoc(), |
| 7816 | Self.PDiag(diag::note_bitwise_and_in_bitwise_or_silence), |
| 7817 | Bop->getSourceRange()); |
| 7818 | } |
| 7819 | |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7820 | /// \brief It accepts a '&&' expr that is inside a '||' one. |
| 7821 | /// Emit a diagnostic together with a fixit hint that wraps the '&&' expression |
| 7822 | /// in parentheses. |
| 7823 | static void |
| 7824 | EmitDiagnosticForLogicalAndInLogicalOr(Sema &Self, SourceLocation OpLoc, |
Argyrios Kyrtzidis | a61aedc | 2011-04-22 19:16:27 +0000 | [diff] [blame] | 7825 | BinaryOperator *Bop) { |
| 7826 | assert(Bop->getOpcode() == BO_LAnd); |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 7827 | Self.Diag(Bop->getOperatorLoc(), diag::warn_logical_and_in_logical_or) |
| 7828 | << Bop->getSourceRange() << OpLoc; |
Argyrios Kyrtzidis | a61aedc | 2011-04-22 19:16:27 +0000 | [diff] [blame] | 7829 | SuggestParentheses(Self, Bop->getOperatorLoc(), |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7830 | Self.PDiag(diag::note_logical_and_in_logical_or_silence), |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 7831 | Bop->getSourceRange()); |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7832 | } |
| 7833 | |
| 7834 | /// \brief Returns true if the given expression can be evaluated as a constant |
| 7835 | /// 'true'. |
| 7836 | static bool EvaluatesAsTrue(Sema &S, Expr *E) { |
| 7837 | bool Res; |
| 7838 | return E->EvaluateAsBooleanCondition(Res, S.getASTContext()) && Res; |
| 7839 | } |
| 7840 | |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 7841 | /// \brief Returns true if the given expression can be evaluated as a constant |
| 7842 | /// 'false'. |
| 7843 | static bool EvaluatesAsFalse(Sema &S, Expr *E) { |
| 7844 | bool Res; |
| 7845 | return E->EvaluateAsBooleanCondition(Res, S.getASTContext()) && !Res; |
| 7846 | } |
| 7847 | |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7848 | /// \brief Look for '&&' in the left hand of a '||' expr. |
| 7849 | static void DiagnoseLogicalAndInLogicalOrLHS(Sema &S, SourceLocation OpLoc, |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 7850 | Expr *OrLHS, Expr *OrRHS) { |
| 7851 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(OrLHS)) { |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 7852 | if (Bop->getOpcode() == BO_LAnd) { |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 7853 | // If it's "a && b || 0" don't warn since the precedence doesn't matter. |
| 7854 | if (EvaluatesAsFalse(S, OrRHS)) |
| 7855 | return; |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7856 | // If it's "1 && a || b" don't warn since the precedence doesn't matter. |
| 7857 | if (!EvaluatesAsTrue(S, Bop->getLHS())) |
| 7858 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, Bop); |
| 7859 | } else if (Bop->getOpcode() == BO_LOr) { |
| 7860 | if (BinaryOperator *RBop = dyn_cast<BinaryOperator>(Bop->getRHS())) { |
| 7861 | // If it's "a || b && 1 || c" we didn't warn earlier for |
| 7862 | // "a || b && 1", but warn now. |
| 7863 | if (RBop->getOpcode() == BO_LAnd && EvaluatesAsTrue(S, RBop->getRHS())) |
| 7864 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, RBop); |
| 7865 | } |
| 7866 | } |
| 7867 | } |
| 7868 | } |
| 7869 | |
| 7870 | /// \brief Look for '&&' in the right hand of a '||' expr. |
| 7871 | static void DiagnoseLogicalAndInLogicalOrRHS(Sema &S, SourceLocation OpLoc, |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 7872 | Expr *OrLHS, Expr *OrRHS) { |
| 7873 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(OrRHS)) { |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7874 | if (Bop->getOpcode() == BO_LAnd) { |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 7875 | // If it's "0 || a && b" don't warn since the precedence doesn't matter. |
| 7876 | if (EvaluatesAsFalse(S, OrLHS)) |
| 7877 | return; |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7878 | // If it's "a || b && 1" don't warn since the precedence doesn't matter. |
| 7879 | if (!EvaluatesAsTrue(S, Bop->getRHS())) |
| 7880 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, Bop); |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 7881 | } |
| 7882 | } |
| 7883 | } |
| 7884 | |
Argyrios Kyrtzidis | 33f46e2 | 2011-06-20 18:41:26 +0000 | [diff] [blame] | 7885 | /// \brief Look for '&' in the left or right hand of a '|' expr. |
| 7886 | static void DiagnoseBitwiseAndInBitwiseOr(Sema &S, SourceLocation OpLoc, |
| 7887 | Expr *OrArg) { |
| 7888 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(OrArg)) { |
| 7889 | if (Bop->getOpcode() == BO_And) |
| 7890 | return EmitDiagnosticForBitwiseAndInBitwiseOr(S, OpLoc, Bop); |
| 7891 | } |
| 7892 | } |
| 7893 | |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7894 | /// DiagnoseBinOpPrecedence - Emit warnings for expressions with tricky |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 7895 | /// precedence. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7896 | static void DiagnoseBinOpPrecedence(Sema &Self, BinaryOperatorKind Opc, |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7897 | SourceLocation OpLoc, Expr *lhs, Expr *rhs){ |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 7898 | // Diagnose "arg1 'bitwise' arg2 'eq' arg3". |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 7899 | if (BinaryOperator::isBitwiseOp(Opc)) |
Argyrios Kyrtzidis | 33f46e2 | 2011-06-20 18:41:26 +0000 | [diff] [blame] | 7900 | DiagnoseBitwisePrecedence(Self, Opc, OpLoc, lhs, rhs); |
| 7901 | |
| 7902 | // Diagnose "arg1 & arg2 | arg3" |
| 7903 | if (Opc == BO_Or && !OpLoc.isMacroID()/* Don't warn in macros. */) { |
| 7904 | DiagnoseBitwiseAndInBitwiseOr(Self, OpLoc, lhs); |
| 7905 | DiagnoseBitwiseAndInBitwiseOr(Self, OpLoc, rhs); |
| 7906 | } |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 7907 | |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7908 | // Warn about arg1 || arg2 && arg3, as GCC 4.3+ does. |
| 7909 | // We don't warn for 'assert(a || b && "bad")' since this is safe. |
Argyrios Kyrtzidis | d92ccaa | 2010-11-17 18:54:22 +0000 | [diff] [blame] | 7910 | if (Opc == BO_LOr && !OpLoc.isMacroID()/* Don't warn in macros. */) { |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 7911 | DiagnoseLogicalAndInLogicalOrLHS(Self, OpLoc, lhs, rhs); |
| 7912 | DiagnoseLogicalAndInLogicalOrRHS(Self, OpLoc, lhs, rhs); |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 7913 | } |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7914 | } |
| 7915 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7916 | // Binary Operators. 'Tok' is the token for the operator. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7917 | ExprResult Sema::ActOnBinOp(Scope *S, SourceLocation TokLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7918 | tok::TokenKind Kind, |
| 7919 | Expr *lhs, Expr *rhs) { |
| 7920 | BinaryOperatorKind Opc = ConvertTokenKindToBinaryOpcode(Kind); |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 7921 | assert((lhs != 0) && "ActOnBinOp(): missing left expression"); |
| 7922 | assert((rhs != 0) && "ActOnBinOp(): missing right expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7923 | |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7924 | // Emit warnings for tricky precedence issues, e.g. "bitfield & 0x4 == 0" |
| 7925 | DiagnoseBinOpPrecedence(*this, Opc, TokLoc, lhs, rhs); |
| 7926 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 7927 | return BuildBinOp(S, TokLoc, Opc, lhs, rhs); |
| 7928 | } |
| 7929 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7930 | ExprResult Sema::BuildBinOp(Scope *S, SourceLocation OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7931 | BinaryOperatorKind Opc, |
| 7932 | Expr *lhs, Expr *rhs) { |
John McCall | 01b2e4e | 2010-12-06 05:26:58 +0000 | [diff] [blame] | 7933 | if (getLangOptions().CPlusPlus) { |
| 7934 | bool UseBuiltinOperator; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7935 | |
John McCall | 01b2e4e | 2010-12-06 05:26:58 +0000 | [diff] [blame] | 7936 | if (lhs->isTypeDependent() || rhs->isTypeDependent()) { |
| 7937 | UseBuiltinOperator = false; |
| 7938 | } else if (Opc == BO_Assign && lhs->getObjectKind() == OK_ObjCProperty) { |
| 7939 | UseBuiltinOperator = true; |
| 7940 | } else { |
| 7941 | UseBuiltinOperator = !lhs->getType()->isOverloadableType() && |
| 7942 | !rhs->getType()->isOverloadableType(); |
| 7943 | } |
| 7944 | |
| 7945 | if (!UseBuiltinOperator) { |
| 7946 | // Find all of the overloaded operators visible from this |
| 7947 | // point. We perform both an operator-name lookup from the local |
| 7948 | // scope and an argument-dependent lookup based on the types of |
| 7949 | // the arguments. |
| 7950 | UnresolvedSet<16> Functions; |
| 7951 | OverloadedOperatorKind OverOp |
| 7952 | = BinaryOperator::getOverloadedOperator(Opc); |
| 7953 | if (S && OverOp != OO_None) |
| 7954 | LookupOverloadedOperatorName(OverOp, S, lhs->getType(), rhs->getType(), |
| 7955 | Functions); |
| 7956 | |
| 7957 | // Build the (potentially-overloaded, potentially-dependent) |
| 7958 | // binary operation. |
| 7959 | return CreateOverloadedBinOp(OpLoc, Opc, Functions, lhs, rhs); |
| 7960 | } |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 7961 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7962 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7963 | // Build a built-in binary operation. |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 7964 | return CreateBuiltinBinOp(OpLoc, Opc, lhs, rhs); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7965 | } |
| 7966 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7967 | ExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc, |
Argyrios Kyrtzidis | b1fa3dc | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 7968 | UnaryOperatorKind Opc, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7969 | Expr *InputExpr) { |
| 7970 | ExprResult Input = Owned(InputExpr); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7971 | ExprValueKind VK = VK_RValue; |
| 7972 | ExprObjectKind OK = OK_Ordinary; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7973 | QualType resultType; |
| 7974 | switch (Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7975 | case UO_PreInc: |
| 7976 | case UO_PreDec: |
| 7977 | case UO_PostInc: |
| 7978 | case UO_PostDec: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7979 | resultType = CheckIncrementDecrementOperand(*this, Input.get(), VK, OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7980 | Opc == UO_PreInc || |
| 7981 | Opc == UO_PostInc, |
| 7982 | Opc == UO_PreInc || |
| 7983 | Opc == UO_PreDec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7984 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7985 | case UO_AddrOf: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7986 | resultType = CheckAddressOfOperand(*this, Input.get(), OpLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7987 | break; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 7988 | case UO_Deref: { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 7989 | ExprResult resolved = CheckPlaceholderExpr(Input.get()); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 7990 | if (!resolved.isUsable()) return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7991 | Input = move(resolved); |
| 7992 | Input = DefaultFunctionArrayLvalueConversion(Input.take()); |
| 7993 | resultType = CheckIndirectionOperand(*this, Input.get(), VK, OpLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7994 | break; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 7995 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7996 | case UO_Plus: |
| 7997 | case UO_Minus: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7998 | Input = UsualUnaryConversions(Input.take()); |
| 7999 | if (Input.isInvalid()) return ExprError(); |
| 8000 | resultType = Input.get()->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8001 | if (resultType->isDependentType()) |
| 8002 | break; |
Douglas Gregor | 0061962 | 2010-06-22 23:41:02 +0000 | [diff] [blame] | 8003 | if (resultType->isArithmeticType() || // C99 6.5.3.3p1 |
| 8004 | resultType->isVectorType()) |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8005 | break; |
| 8006 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6-7 |
| 8007 | resultType->isEnumeralType()) |
| 8008 | break; |
| 8009 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6 |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8010 | Opc == UO_Plus && |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8011 | resultType->isPointerType()) |
| 8012 | break; |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8013 | else if (resultType->isPlaceholderType()) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8014 | Input = CheckPlaceholderExpr(Input.take()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8015 | if (Input.isInvalid()) return ExprError(); |
| 8016 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input.take()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8017 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8018 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8019 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8020 | << resultType << Input.get()->getSourceRange()); |
| 8021 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8022 | case UO_Not: // bitwise complement |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8023 | Input = UsualUnaryConversions(Input.take()); |
| 8024 | if (Input.isInvalid()) return ExprError(); |
| 8025 | resultType = Input.get()->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8026 | if (resultType->isDependentType()) |
| 8027 | break; |
Chris Lattner | 02a6514 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 8028 | // C99 6.5.3.3p1. We allow complex int and float as a GCC extension. |
| 8029 | if (resultType->isComplexType() || resultType->isComplexIntegerType()) |
| 8030 | // C99 does not support '~' for complex conjugation. |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 8031 | Diag(OpLoc, diag::ext_integer_complement_complex) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8032 | << resultType << Input.get()->getSourceRange(); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8033 | else if (resultType->hasIntegerRepresentation()) |
| 8034 | break; |
| 8035 | else if (resultType->isPlaceholderType()) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8036 | Input = CheckPlaceholderExpr(Input.take()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8037 | if (Input.isInvalid()) return ExprError(); |
| 8038 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input.take()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8039 | } else { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8040 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8041 | << resultType << Input.get()->getSourceRange()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8042 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8043 | break; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8044 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8045 | case UO_LNot: // logical negation |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8046 | // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8047 | Input = DefaultFunctionArrayLvalueConversion(Input.take()); |
| 8048 | if (Input.isInvalid()) return ExprError(); |
| 8049 | resultType = Input.get()->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8050 | if (resultType->isDependentType()) |
| 8051 | break; |
Abramo Bagnara | 737d544 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 8052 | if (resultType->isScalarType()) { |
| 8053 | // C99 6.5.3.3p1: ok, fallthrough; |
| 8054 | if (Context.getLangOptions().CPlusPlus) { |
| 8055 | // C++03 [expr.unary.op]p8, C++0x [expr.unary.op]p9: |
| 8056 | // operand contextually converted to bool. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8057 | Input = ImpCastExprToType(Input.take(), Context.BoolTy, |
| 8058 | ScalarTypeToBooleanCastKind(resultType)); |
Abramo Bagnara | 737d544 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 8059 | } |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8060 | } else if (resultType->isPlaceholderType()) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8061 | Input = CheckPlaceholderExpr(Input.take()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8062 | if (Input.isInvalid()) return ExprError(); |
| 8063 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input.take()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8064 | } else { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8065 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8066 | << resultType << Input.get()->getSourceRange()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8067 | } |
Douglas Gregor | ea844f3 | 2010-09-20 17:13:33 +0000 | [diff] [blame] | 8068 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8069 | // LNot always has type int. C99 6.5.3.3p5. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8070 | // In C++, it's bool. C++ 5.3.1p8 |
Argyrios Kyrtzidis | 16f744b | 2011-02-18 20:55:15 +0000 | [diff] [blame] | 8071 | resultType = Context.getLogicalOperationType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8072 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8073 | case UO_Real: |
| 8074 | case UO_Imag: |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8075 | resultType = CheckRealImagOperand(*this, Input, OpLoc, Opc == UO_Real); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8076 | // _Real and _Imag map ordinary l-values into ordinary l-values. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8077 | if (Input.isInvalid()) return ExprError(); |
| 8078 | if (Input.get()->getValueKind() != VK_RValue && |
| 8079 | Input.get()->getObjectKind() == OK_Ordinary) |
| 8080 | VK = Input.get()->getValueKind(); |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 8081 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8082 | case UO_Extension: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8083 | resultType = Input.get()->getType(); |
| 8084 | VK = Input.get()->getValueKind(); |
| 8085 | OK = Input.get()->getObjectKind(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8086 | break; |
| 8087 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8088 | if (resultType.isNull() || Input.isInvalid()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8089 | return ExprError(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8090 | |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame^] | 8091 | // Check for array bounds violations in the operand of the UnaryOperator, |
| 8092 | // except for the '*' and '&' operators that have to be handled specially |
| 8093 | // by CheckArrayAccess (as there are special cases like &array[arraysize] |
| 8094 | // that are explicitly defined as valid by the standard). |
| 8095 | if (Opc != UO_AddrOf && Opc != UO_Deref) |
| 8096 | CheckArrayAccess(Input.get()); |
| 8097 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8098 | return Owned(new (Context) UnaryOperator(Input.take(), Opc, resultType, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8099 | VK, OK, OpLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8100 | } |
| 8101 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8102 | ExprResult Sema::BuildUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8103 | UnaryOperatorKind Opc, |
| 8104 | Expr *Input) { |
Anders Carlsson | a8a1e3d | 2009-11-14 21:26:41 +0000 | [diff] [blame] | 8105 | if (getLangOptions().CPlusPlus && Input->getType()->isOverloadableType() && |
Eli Friedman | 957c094 | 2010-09-05 23:15:52 +0000 | [diff] [blame] | 8106 | UnaryOperator::getOverloadedOperator(Opc) != OO_None) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8107 | // Find all of the overloaded operators visible from this |
| 8108 | // point. We perform both an operator-name lookup from the local |
| 8109 | // scope and an argument-dependent lookup based on the types of |
| 8110 | // the arguments. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8111 | UnresolvedSet<16> Functions; |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8112 | OverloadedOperatorKind OverOp = UnaryOperator::getOverloadedOperator(Opc); |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8113 | if (S && OverOp != OO_None) |
| 8114 | LookupOverloadedOperatorName(OverOp, S, Input->getType(), QualType(), |
| 8115 | Functions); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8116 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8117 | return CreateOverloadedUnaryOp(OpLoc, Opc, Functions, Input); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8118 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8119 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8120 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8121 | } |
| 8122 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8123 | // Unary Operators. 'Tok' is the token for the operator. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8124 | ExprResult Sema::ActOnUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 8125 | tok::TokenKind Op, Expr *Input) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8126 | return BuildUnaryOp(S, OpLoc, ConvertTokenKindToUnaryOpcode(Op), Input); |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8127 | } |
| 8128 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 8129 | /// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo". |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 8130 | ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc, SourceLocation LabLoc, |
Chris Lattner | 57ad378 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 8131 | LabelDecl *TheDecl) { |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 8132 | TheDecl->setUsed(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8133 | // Create the AST node. The address of a label always has type 'void*'. |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 8134 | return Owned(new (Context) AddrLabelExpr(OpLoc, LabLoc, TheDecl, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8135 | Context.getPointerType(Context.VoidTy))); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8136 | } |
| 8137 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8138 | /// Given the last statement in a statement-expression, check whether |
| 8139 | /// the result is a producing expression (like a call to an |
| 8140 | /// ns_returns_retained function) and, if so, rebuild it to hoist the |
| 8141 | /// release out of the full-expression. Otherwise, return null. |
| 8142 | /// Cannot fail. |
| 8143 | static Expr *maybeRebuildARCConsumingStmt(Stmt *s) { |
| 8144 | // Should always be wrapped with one of these. |
| 8145 | ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(s); |
| 8146 | if (!cleanups) return 0; |
| 8147 | |
| 8148 | ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(cleanups->getSubExpr()); |
| 8149 | if (!cast || cast->getCastKind() != CK_ObjCConsumeObject) |
| 8150 | return 0; |
| 8151 | |
| 8152 | // Splice out the cast. This shouldn't modify any interesting |
| 8153 | // features of the statement. |
| 8154 | Expr *producer = cast->getSubExpr(); |
| 8155 | assert(producer->getType() == cast->getType()); |
| 8156 | assert(producer->getValueKind() == cast->getValueKind()); |
| 8157 | cleanups->setSubExpr(producer); |
| 8158 | return cleanups; |
| 8159 | } |
| 8160 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8161 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8162 | Sema::ActOnStmtExpr(SourceLocation LPLoc, Stmt *SubStmt, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8163 | SourceLocation RPLoc) { // "({..})" |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8164 | assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!"); |
| 8165 | CompoundStmt *Compound = cast<CompoundStmt>(SubStmt); |
| 8166 | |
Douglas Gregor | dd8f569 | 2010-03-10 04:54:39 +0000 | [diff] [blame] | 8167 | bool isFileScope |
| 8168 | = (getCurFunctionOrMethodDecl() == 0) && (getCurBlock() == 0); |
Chris Lattner | 4a049f0 | 2009-04-25 19:11:05 +0000 | [diff] [blame] | 8169 | if (isFileScope) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8170 | return ExprError(Diag(LPLoc, diag::err_stmtexpr_file_scope)); |
Eli Friedman | dca2b73 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 8171 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8172 | // FIXME: there are a variety of strange constraints to enforce here, for |
| 8173 | // example, it is not possible to goto into a stmt expression apparently. |
| 8174 | // More semantic analysis is needed. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8175 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8176 | // If there are sub stmts in the compound stmt, take the type of the last one |
| 8177 | // as the type of the stmtexpr. |
| 8178 | QualType Ty = Context.VoidTy; |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8179 | bool StmtExprMayBindToTemp = false; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8180 | if (!Compound->body_empty()) { |
| 8181 | Stmt *LastStmt = Compound->body_back(); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8182 | LabelStmt *LastLabelStmt = 0; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8183 | // If LastStmt is a label, skip down through into the body. |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8184 | while (LabelStmt *Label = dyn_cast<LabelStmt>(LastStmt)) { |
| 8185 | LastLabelStmt = Label; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8186 | LastStmt = Label->getSubStmt(); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8187 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8188 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8189 | if (Expr *LastE = dyn_cast<Expr>(LastStmt)) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8190 | // Do function/array conversion on the last expression, but not |
| 8191 | // lvalue-to-rvalue. However, initialize an unqualified type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8192 | ExprResult LastExpr = DefaultFunctionArrayConversion(LastE); |
| 8193 | if (LastExpr.isInvalid()) |
| 8194 | return ExprError(); |
| 8195 | Ty = LastExpr.get()->getType().getUnqualifiedType(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8196 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8197 | if (!Ty->isDependentType() && !LastExpr.get()->isTypeDependent()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8198 | // In ARC, if the final expression ends in a consume, splice |
| 8199 | // the consume out and bind it later. In the alternate case |
| 8200 | // (when dealing with a retainable type), the result |
| 8201 | // initialization will create a produce. In both cases the |
| 8202 | // result will be +1, and we'll need to balance that out with |
| 8203 | // a bind. |
| 8204 | if (Expr *rebuiltLastStmt |
| 8205 | = maybeRebuildARCConsumingStmt(LastExpr.get())) { |
| 8206 | LastExpr = rebuiltLastStmt; |
| 8207 | } else { |
| 8208 | LastExpr = PerformCopyInitialization( |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8209 | InitializedEntity::InitializeResult(LPLoc, |
| 8210 | Ty, |
| 8211 | false), |
| 8212 | SourceLocation(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8213 | LastExpr); |
| 8214 | } |
| 8215 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8216 | if (LastExpr.isInvalid()) |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8217 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8218 | if (LastExpr.get() != 0) { |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8219 | if (!LastLabelStmt) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8220 | Compound->setLastStmt(LastExpr.take()); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8221 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8222 | LastLabelStmt->setSubStmt(LastExpr.take()); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8223 | StmtExprMayBindToTemp = true; |
| 8224 | } |
| 8225 | } |
| 8226 | } |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8227 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8228 | |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 8229 | // FIXME: Check that expression type is complete/non-abstract; statement |
| 8230 | // expressions are not lvalues. |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8231 | Expr *ResStmtExpr = new (Context) StmtExpr(Compound, Ty, LPLoc, RPLoc); |
| 8232 | if (StmtExprMayBindToTemp) |
| 8233 | return MaybeBindToTemporary(ResStmtExpr); |
| 8234 | return Owned(ResStmtExpr); |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8235 | } |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 8236 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8237 | ExprResult Sema::BuildBuiltinOffsetOf(SourceLocation BuiltinLoc, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8238 | TypeSourceInfo *TInfo, |
| 8239 | OffsetOfComponent *CompPtr, |
| 8240 | unsigned NumComponents, |
| 8241 | SourceLocation RParenLoc) { |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8242 | QualType ArgTy = TInfo->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8243 | bool Dependent = ArgTy->isDependentType(); |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 8244 | SourceRange TypeRange = TInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8245 | |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 8246 | // We must have at least one component that refers to the type, and the first |
| 8247 | // one is known to be a field designator. Verify that the ArgTy represents |
| 8248 | // a struct/union/class. |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8249 | if (!Dependent && !ArgTy->isRecordType()) |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8250 | return ExprError(Diag(BuiltinLoc, diag::err_offsetof_record_type) |
| 8251 | << ArgTy << TypeRange); |
| 8252 | |
| 8253 | // Type must be complete per C99 7.17p3 because a declaring a variable |
| 8254 | // with an incomplete type would be ill-formed. |
| 8255 | if (!Dependent |
| 8256 | && RequireCompleteType(BuiltinLoc, ArgTy, |
| 8257 | PDiag(diag::err_offsetof_incomplete_type) |
| 8258 | << TypeRange)) |
| 8259 | return ExprError(); |
| 8260 | |
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 8261 | // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a |
| 8262 | // GCC extension, diagnose them. |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 8263 | // FIXME: This diagnostic isn't actually visible because the location is in |
| 8264 | // a system header! |
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 8265 | if (NumComponents != 1) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 8266 | Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator) |
| 8267 | << SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8268 | |
| 8269 | bool DidWarnAboutNonPOD = false; |
| 8270 | QualType CurrentType = ArgTy; |
| 8271 | typedef OffsetOfExpr::OffsetOfNode OffsetOfNode; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8272 | SmallVector<OffsetOfNode, 4> Comps; |
| 8273 | SmallVector<Expr*, 4> Exprs; |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8274 | for (unsigned i = 0; i != NumComponents; ++i) { |
| 8275 | const OffsetOfComponent &OC = CompPtr[i]; |
| 8276 | if (OC.isBrackets) { |
| 8277 | // Offset of an array sub-field. TODO: Should we allow vector elements? |
| 8278 | if (!CurrentType->isDependentType()) { |
| 8279 | const ArrayType *AT = Context.getAsArrayType(CurrentType); |
| 8280 | if(!AT) |
| 8281 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_array_type) |
| 8282 | << CurrentType); |
| 8283 | CurrentType = AT->getElementType(); |
| 8284 | } else |
| 8285 | CurrentType = Context.DependentTy; |
| 8286 | |
| 8287 | // The expression must be an integral expression. |
| 8288 | // FIXME: An integral constant expression? |
| 8289 | Expr *Idx = static_cast<Expr*>(OC.U.E); |
| 8290 | if (!Idx->isTypeDependent() && !Idx->isValueDependent() && |
| 8291 | !Idx->getType()->isIntegerType()) |
| 8292 | return ExprError(Diag(Idx->getLocStart(), |
| 8293 | diag::err_typecheck_subscript_not_integer) |
| 8294 | << Idx->getSourceRange()); |
| 8295 | |
| 8296 | // Record this array index. |
| 8297 | Comps.push_back(OffsetOfNode(OC.LocStart, Exprs.size(), OC.LocEnd)); |
| 8298 | Exprs.push_back(Idx); |
| 8299 | continue; |
| 8300 | } |
| 8301 | |
| 8302 | // Offset of a field. |
| 8303 | if (CurrentType->isDependentType()) { |
| 8304 | // We have the offset of a field, but we can't look into the dependent |
| 8305 | // type. Just record the identifier of the field. |
| 8306 | Comps.push_back(OffsetOfNode(OC.LocStart, OC.U.IdentInfo, OC.LocEnd)); |
| 8307 | CurrentType = Context.DependentTy; |
| 8308 | continue; |
| 8309 | } |
| 8310 | |
| 8311 | // We need to have a complete type to look into. |
| 8312 | if (RequireCompleteType(OC.LocStart, CurrentType, |
| 8313 | diag::err_offsetof_incomplete_type)) |
| 8314 | return ExprError(); |
| 8315 | |
| 8316 | // Look for the designated field. |
| 8317 | const RecordType *RC = CurrentType->getAs<RecordType>(); |
| 8318 | if (!RC) |
| 8319 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_record_type) |
| 8320 | << CurrentType); |
| 8321 | RecordDecl *RD = RC->getDecl(); |
| 8322 | |
| 8323 | // C++ [lib.support.types]p5: |
| 8324 | // The macro offsetof accepts a restricted set of type arguments in this |
| 8325 | // International Standard. type shall be a POD structure or a POD union |
| 8326 | // (clause 9). |
| 8327 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 8328 | if (!CRD->isPOD() && !DidWarnAboutNonPOD && |
Ted Kremenek | 762696f | 2011-02-23 01:51:43 +0000 | [diff] [blame] | 8329 | DiagRuntimeBehavior(BuiltinLoc, 0, |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8330 | PDiag(diag::warn_offsetof_non_pod_type) |
| 8331 | << SourceRange(CompPtr[0].LocStart, OC.LocEnd) |
| 8332 | << CurrentType)) |
| 8333 | DidWarnAboutNonPOD = true; |
| 8334 | } |
| 8335 | |
| 8336 | // Look for the field. |
| 8337 | LookupResult R(*this, OC.U.IdentInfo, OC.LocStart, LookupMemberName); |
| 8338 | LookupQualifiedName(R, RD); |
| 8339 | FieldDecl *MemberDecl = R.getAsSingle<FieldDecl>(); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8340 | IndirectFieldDecl *IndirectMemberDecl = 0; |
| 8341 | if (!MemberDecl) { |
Benjamin Kramer | d981146 | 2010-11-21 14:11:41 +0000 | [diff] [blame] | 8342 | if ((IndirectMemberDecl = R.getAsSingle<IndirectFieldDecl>())) |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8343 | MemberDecl = IndirectMemberDecl->getAnonField(); |
| 8344 | } |
| 8345 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8346 | if (!MemberDecl) |
| 8347 | return ExprError(Diag(BuiltinLoc, diag::err_no_member) |
| 8348 | << OC.U.IdentInfo << RD << SourceRange(OC.LocStart, |
| 8349 | OC.LocEnd)); |
| 8350 | |
Douglas Gregor | 9d5d60f | 2010-04-28 22:36:06 +0000 | [diff] [blame] | 8351 | // C99 7.17p3: |
| 8352 | // (If the specified member is a bit-field, the behavior is undefined.) |
| 8353 | // |
| 8354 | // We diagnose this as an error. |
| 8355 | if (MemberDecl->getBitWidth()) { |
| 8356 | Diag(OC.LocEnd, diag::err_offsetof_bitfield) |
| 8357 | << MemberDecl->getDeclName() |
| 8358 | << SourceRange(BuiltinLoc, RParenLoc); |
| 8359 | Diag(MemberDecl->getLocation(), diag::note_bitfield_decl); |
| 8360 | return ExprError(); |
| 8361 | } |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 8362 | |
| 8363 | RecordDecl *Parent = MemberDecl->getParent(); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8364 | if (IndirectMemberDecl) |
| 8365 | Parent = cast<RecordDecl>(IndirectMemberDecl->getDeclContext()); |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 8366 | |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 8367 | // If the member was found in a base class, introduce OffsetOfNodes for |
| 8368 | // the base class indirections. |
| 8369 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 8370 | /*DetectVirtual=*/false); |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 8371 | if (IsDerivedFrom(CurrentType, Context.getTypeDeclType(Parent), Paths)) { |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 8372 | CXXBasePath &Path = Paths.front(); |
| 8373 | for (CXXBasePath::iterator B = Path.begin(), BEnd = Path.end(); |
| 8374 | B != BEnd; ++B) |
| 8375 | Comps.push_back(OffsetOfNode(B->Base)); |
| 8376 | } |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 8377 | |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8378 | if (IndirectMemberDecl) { |
| 8379 | for (IndirectFieldDecl::chain_iterator FI = |
| 8380 | IndirectMemberDecl->chain_begin(), |
| 8381 | FEnd = IndirectMemberDecl->chain_end(); FI != FEnd; FI++) { |
| 8382 | assert(isa<FieldDecl>(*FI)); |
| 8383 | Comps.push_back(OffsetOfNode(OC.LocStart, |
| 8384 | cast<FieldDecl>(*FI), OC.LocEnd)); |
| 8385 | } |
| 8386 | } else |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8387 | Comps.push_back(OffsetOfNode(OC.LocStart, MemberDecl, OC.LocEnd)); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8388 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8389 | CurrentType = MemberDecl->getType().getNonReferenceType(); |
| 8390 | } |
| 8391 | |
| 8392 | return Owned(OffsetOfExpr::Create(Context, Context.getSizeType(), BuiltinLoc, |
| 8393 | TInfo, Comps.data(), Comps.size(), |
| 8394 | Exprs.data(), Exprs.size(), RParenLoc)); |
| 8395 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8396 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8397 | ExprResult Sema::ActOnBuiltinOffsetOf(Scope *S, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8398 | SourceLocation BuiltinLoc, |
| 8399 | SourceLocation TypeLoc, |
| 8400 | ParsedType argty, |
| 8401 | OffsetOfComponent *CompPtr, |
| 8402 | unsigned NumComponents, |
| 8403 | SourceLocation RPLoc) { |
| 8404 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8405 | TypeSourceInfo *ArgTInfo; |
| 8406 | QualType ArgTy = GetTypeFromParser(argty, &ArgTInfo); |
| 8407 | if (ArgTy.isNull()) |
| 8408 | return ExprError(); |
| 8409 | |
Eli Friedman | 5a15dc1 | 2010-08-05 10:15:45 +0000 | [diff] [blame] | 8410 | if (!ArgTInfo) |
| 8411 | ArgTInfo = Context.getTrivialTypeSourceInfo(ArgTy, TypeLoc); |
| 8412 | |
| 8413 | return BuildBuiltinOffsetOf(BuiltinLoc, ArgTInfo, CompPtr, NumComponents, |
| 8414 | RPLoc); |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 8415 | } |
| 8416 | |
| 8417 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8418 | ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8419 | Expr *CondExpr, |
| 8420 | Expr *LHSExpr, Expr *RHSExpr, |
| 8421 | SourceLocation RPLoc) { |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 8422 | assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)"); |
| 8423 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8424 | ExprValueKind VK = VK_RValue; |
| 8425 | ExprObjectKind OK = OK_Ordinary; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8426 | QualType resType; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 8427 | bool ValueDependent = false; |
Douglas Gregor | c9ecc57 | 2009-05-19 22:43:30 +0000 | [diff] [blame] | 8428 | if (CondExpr->isTypeDependent() || CondExpr->isValueDependent()) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8429 | resType = Context.DependentTy; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 8430 | ValueDependent = true; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8431 | } else { |
| 8432 | // The conditional expression is required to be a constant expression. |
| 8433 | llvm::APSInt condEval(32); |
| 8434 | SourceLocation ExpLoc; |
| 8435 | if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc)) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8436 | return ExprError(Diag(ExpLoc, |
| 8437 | diag::err_typecheck_choose_expr_requires_constant) |
| 8438 | << CondExpr->getSourceRange()); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 8439 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8440 | // If the condition is > zero, then the AST type is the same as the LSHExpr. |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8441 | Expr *ActiveExpr = condEval.getZExtValue() ? LHSExpr : RHSExpr; |
| 8442 | |
| 8443 | resType = ActiveExpr->getType(); |
| 8444 | ValueDependent = ActiveExpr->isValueDependent(); |
| 8445 | VK = ActiveExpr->getValueKind(); |
| 8446 | OK = ActiveExpr->getObjectKind(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8447 | } |
| 8448 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8449 | return Owned(new (Context) ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8450 | resType, VK, OK, RPLoc, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 8451 | resType->isDependentType(), |
| 8452 | ValueDependent)); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 8453 | } |
| 8454 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8455 | //===----------------------------------------------------------------------===// |
| 8456 | // Clang Extensions. |
| 8457 | //===----------------------------------------------------------------------===// |
| 8458 | |
| 8459 | /// ActOnBlockStart - This callback is invoked when a block literal is started. |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8460 | void Sema::ActOnBlockStart(SourceLocation CaretLoc, Scope *BlockScope) { |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8461 | BlockDecl *Block = BlockDecl::Create(Context, CurContext, CaretLoc); |
| 8462 | PushBlockScope(BlockScope, Block); |
| 8463 | CurContext->addDecl(Block); |
Fariborz Jahanian | a729da2 | 2010-07-09 18:44:02 +0000 | [diff] [blame] | 8464 | if (BlockScope) |
| 8465 | PushDeclContext(BlockScope, Block); |
| 8466 | else |
| 8467 | CurContext = Block; |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8468 | } |
| 8469 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 8470 | void Sema::ActOnBlockArguments(Declarator &ParamInfo, Scope *CurScope) { |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 8471 | assert(ParamInfo.getIdentifier()==0 && "block-id should have no identifier!"); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8472 | assert(ParamInfo.getContext() == Declarator::BlockLiteralContext); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8473 | BlockScopeInfo *CurBlock = getCurBlock(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8474 | |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 8475 | TypeSourceInfo *Sig = GetTypeForDeclarator(ParamInfo, CurScope); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 8476 | QualType T = Sig->getType(); |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 8477 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8478 | // GetTypeForDeclarator always produces a function type for a block |
| 8479 | // literal signature. Furthermore, it is always a FunctionProtoType |
| 8480 | // unless the function was written with a typedef. |
| 8481 | assert(T->isFunctionType() && |
| 8482 | "GetTypeForDeclarator made a non-function block signature"); |
| 8483 | |
| 8484 | // Look for an explicit signature in that function type. |
| 8485 | FunctionProtoTypeLoc ExplicitSignature; |
| 8486 | |
| 8487 | TypeLoc tmp = Sig->getTypeLoc().IgnoreParens(); |
| 8488 | if (isa<FunctionProtoTypeLoc>(tmp)) { |
| 8489 | ExplicitSignature = cast<FunctionProtoTypeLoc>(tmp); |
| 8490 | |
| 8491 | // Check whether that explicit signature was synthesized by |
| 8492 | // GetTypeForDeclarator. If so, don't save that as part of the |
| 8493 | // written signature. |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 8494 | if (ExplicitSignature.getLocalRangeBegin() == |
| 8495 | ExplicitSignature.getLocalRangeEnd()) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8496 | // This would be much cheaper if we stored TypeLocs instead of |
| 8497 | // TypeSourceInfos. |
| 8498 | TypeLoc Result = ExplicitSignature.getResultLoc(); |
| 8499 | unsigned Size = Result.getFullDataSize(); |
| 8500 | Sig = Context.CreateTypeSourceInfo(Result.getType(), Size); |
| 8501 | Sig->getTypeLoc().initializeFullCopy(Result, Size); |
| 8502 | |
| 8503 | ExplicitSignature = FunctionProtoTypeLoc(); |
| 8504 | } |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8505 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8506 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8507 | CurBlock->TheDecl->setSignatureAsWritten(Sig); |
| 8508 | CurBlock->FunctionType = T; |
| 8509 | |
| 8510 | const FunctionType *Fn = T->getAs<FunctionType>(); |
| 8511 | QualType RetTy = Fn->getResultType(); |
| 8512 | bool isVariadic = |
| 8513 | (isa<FunctionProtoType>(Fn) && cast<FunctionProtoType>(Fn)->isVariadic()); |
| 8514 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8515 | CurBlock->TheDecl->setIsVariadic(isVariadic); |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 8516 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8517 | // Don't allow returning a objc interface by value. |
| 8518 | if (RetTy->isObjCObjectType()) { |
| 8519 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 8520 | diag::err_object_cannot_be_passed_returned_by_value) << 0 << RetTy; |
| 8521 | return; |
| 8522 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8523 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8524 | // Context.DependentTy is used as a placeholder for a missing block |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8525 | // return type. TODO: what should we do with declarators like: |
| 8526 | // ^ * { ... } |
| 8527 | // If the answer is "apply template argument deduction".... |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8528 | if (RetTy != Context.DependentTy) |
| 8529 | CurBlock->ReturnType = RetTy; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8530 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8531 | // Push block parameters from the declarator if we had them. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8532 | SmallVector<ParmVarDecl*, 8> Params; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8533 | if (ExplicitSignature) { |
| 8534 | for (unsigned I = 0, E = ExplicitSignature.getNumArgs(); I != E; ++I) { |
| 8535 | ParmVarDecl *Param = ExplicitSignature.getArg(I); |
Fariborz Jahanian | 9a66c30 | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 8536 | if (Param->getIdentifier() == 0 && |
| 8537 | !Param->isImplicit() && |
| 8538 | !Param->isInvalidDecl() && |
| 8539 | !getLangOptions().CPlusPlus) |
| 8540 | Diag(Param->getLocation(), diag::err_parameter_name_omitted); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8541 | Params.push_back(Param); |
Fariborz Jahanian | 9a66c30 | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 8542 | } |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8543 | |
| 8544 | // Fake up parameter variables if we have a typedef, like |
| 8545 | // ^ fntype { ... } |
| 8546 | } else if (const FunctionProtoType *Fn = T->getAs<FunctionProtoType>()) { |
| 8547 | for (FunctionProtoType::arg_type_iterator |
| 8548 | I = Fn->arg_type_begin(), E = Fn->arg_type_end(); I != E; ++I) { |
| 8549 | ParmVarDecl *Param = |
| 8550 | BuildParmVarDeclForTypedef(CurBlock->TheDecl, |
| 8551 | ParamInfo.getSourceRange().getBegin(), |
| 8552 | *I); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8553 | Params.push_back(Param); |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8554 | } |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8555 | } |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8556 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8557 | // Set the parameters on the block decl. |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 8558 | if (!Params.empty()) { |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8559 | CurBlock->TheDecl->setParams(Params.data(), Params.size()); |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 8560 | CheckParmsForFunctionDef(CurBlock->TheDecl->param_begin(), |
| 8561 | CurBlock->TheDecl->param_end(), |
| 8562 | /*CheckParameterNames=*/false); |
| 8563 | } |
| 8564 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8565 | // Finally we can process decl attributes. |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 8566 | ProcessDeclAttributes(CurScope, CurBlock->TheDecl, ParamInfo); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 8567 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8568 | if (!isVariadic && CurBlock->TheDecl->getAttr<SentinelAttr>()) { |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8569 | Diag(ParamInfo.getAttributes()->getLoc(), |
| 8570 | diag::warn_attribute_sentinel_not_variadic) << 1; |
| 8571 | // FIXME: remove the attribute. |
| 8572 | } |
| 8573 | |
| 8574 | // Put the parameter variables in scope. We can bail out immediately |
| 8575 | // if we don't have any. |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8576 | if (Params.empty()) |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8577 | return; |
| 8578 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8579 | for (BlockDecl::param_iterator AI = CurBlock->TheDecl->param_begin(), |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 8580 | E = CurBlock->TheDecl->param_end(); AI != E; ++AI) { |
| 8581 | (*AI)->setOwningFunction(CurBlock->TheDecl); |
| 8582 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8583 | // If this has an identifier, add it to the scope stack. |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 8584 | if ((*AI)->getIdentifier()) { |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 8585 | CheckShadow(CurBlock->TheScope, *AI); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 8586 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8587 | PushOnScopeChains(*AI, CurBlock->TheScope); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 8588 | } |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 8589 | } |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8590 | } |
| 8591 | |
| 8592 | /// ActOnBlockError - If there is an error parsing a block, this callback |
| 8593 | /// is invoked to pop the information about the block from the action impl. |
| 8594 | void Sema::ActOnBlockError(SourceLocation CaretLoc, Scope *CurScope) { |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8595 | // Pop off CurBlock, handle nested blocks. |
Chris Lattner | 5c59e2b | 2009-04-21 22:38:46 +0000 | [diff] [blame] | 8596 | PopDeclContext(); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8597 | PopFunctionOrBlockScope(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8598 | } |
| 8599 | |
| 8600 | /// ActOnBlockStmtExpr - This is called when the body of a block statement |
| 8601 | /// literal was successfully completed. ^(int x){...} |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8602 | ExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, |
Chris Lattner | e476bdc | 2011-02-17 23:58:47 +0000 | [diff] [blame] | 8603 | Stmt *Body, Scope *CurScope) { |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 8604 | // If blocks are disabled, emit an error. |
| 8605 | if (!LangOpts.Blocks) |
| 8606 | Diag(CaretLoc, diag::err_blocks_disable); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8607 | |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8608 | BlockScopeInfo *BSI = cast<BlockScopeInfo>(FunctionScopes.back()); |
Fariborz Jahanian | a729da2 | 2010-07-09 18:44:02 +0000 | [diff] [blame] | 8609 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8610 | PopDeclContext(); |
| 8611 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8612 | QualType RetTy = Context.VoidTy; |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 8613 | if (!BSI->ReturnType.isNull()) |
| 8614 | RetTy = BSI->ReturnType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8615 | |
Mike Stump | 5692586 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 8616 | bool NoReturn = BSI->TheDecl->getAttr<NoReturnAttr>(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8617 | QualType BlockTy; |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8618 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 8619 | // Set the captured variables on the block. |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 8620 | BSI->TheDecl->setCaptures(Context, BSI->Captures.begin(), BSI->Captures.end(), |
| 8621 | BSI->CapturesCXXThis); |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 8622 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8623 | // If the user wrote a function type in some form, try to use that. |
| 8624 | if (!BSI->FunctionType.isNull()) { |
| 8625 | const FunctionType *FTy = BSI->FunctionType->getAs<FunctionType>(); |
| 8626 | |
| 8627 | FunctionType::ExtInfo Ext = FTy->getExtInfo(); |
| 8628 | if (NoReturn && !Ext.getNoReturn()) Ext = Ext.withNoReturn(true); |
| 8629 | |
| 8630 | // Turn protoless block types into nullary block types. |
| 8631 | if (isa<FunctionNoProtoType>(FTy)) { |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8632 | FunctionProtoType::ExtProtoInfo EPI; |
| 8633 | EPI.ExtInfo = Ext; |
| 8634 | BlockTy = Context.getFunctionType(RetTy, 0, 0, EPI); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8635 | |
| 8636 | // Otherwise, if we don't need to change anything about the function type, |
| 8637 | // preserve its sugar structure. |
| 8638 | } else if (FTy->getResultType() == RetTy && |
| 8639 | (!NoReturn || FTy->getNoReturnAttr())) { |
| 8640 | BlockTy = BSI->FunctionType; |
| 8641 | |
| 8642 | // Otherwise, make the minimal modifications to the function type. |
| 8643 | } else { |
| 8644 | const FunctionProtoType *FPT = cast<FunctionProtoType>(FTy); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8645 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 8646 | EPI.TypeQuals = 0; // FIXME: silently? |
| 8647 | EPI.ExtInfo = Ext; |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8648 | BlockTy = Context.getFunctionType(RetTy, |
| 8649 | FPT->arg_type_begin(), |
| 8650 | FPT->getNumArgs(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8651 | EPI); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8652 | } |
| 8653 | |
| 8654 | // If we don't have a function type, just build one from nothing. |
| 8655 | } else { |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8656 | FunctionProtoType::ExtProtoInfo EPI; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8657 | EPI.ExtInfo = FunctionType::ExtInfo().withNoReturn(NoReturn); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8658 | BlockTy = Context.getFunctionType(RetTy, 0, 0, EPI); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8659 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8660 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8661 | DiagnoseUnusedParameters(BSI->TheDecl->param_begin(), |
| 8662 | BSI->TheDecl->param_end()); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8663 | BlockTy = Context.getBlockPointerType(BlockTy); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8664 | |
Chris Lattner | 17a7830 | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 8665 | // If needed, diagnose invalid gotos and switches in the block. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8666 | if (getCurFunction()->NeedsScopeChecking() && |
| 8667 | !hasAnyUnrecoverableErrorsInThisFunction()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8668 | DiagnoseInvalidJumps(cast<CompoundStmt>(Body)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8669 | |
Chris Lattner | e476bdc | 2011-02-17 23:58:47 +0000 | [diff] [blame] | 8670 | BSI->TheDecl->setBody(cast<CompoundStmt>(Body)); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8671 | |
Fariborz Jahanian | 4e7c7f2 | 2011-07-11 18:04:54 +0000 | [diff] [blame] | 8672 | for (BlockDecl::capture_const_iterator ci = BSI->TheDecl->capture_begin(), |
| 8673 | ce = BSI->TheDecl->capture_end(); ci != ce; ++ci) { |
| 8674 | const VarDecl *variable = ci->getVariable(); |
| 8675 | QualType T = variable->getType(); |
| 8676 | QualType::DestructionKind destructKind = T.isDestructedType(); |
| 8677 | if (destructKind != QualType::DK_none) |
| 8678 | getCurFunction()->setHasBranchProtectedScope(); |
| 8679 | } |
| 8680 | |
Benjamin Kramer | d248619 | 2011-07-12 14:11:05 +0000 | [diff] [blame] | 8681 | BlockExpr *Result = new (Context) BlockExpr(BSI->TheDecl, BlockTy); |
| 8682 | const AnalysisBasedWarnings::Policy &WP = AnalysisWarnings.getDefaultPolicy(); |
| 8683 | PopFunctionOrBlockScope(&WP, Result->getBlockDecl(), Result); |
| 8684 | |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8685 | return Owned(Result); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8686 | } |
| 8687 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8688 | ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 8689 | Expr *expr, ParsedType type, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8690 | SourceLocation RPLoc) { |
Abramo Bagnara | 2cad900 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 8691 | TypeSourceInfo *TInfo; |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 8692 | GetTypeFromParser(type, &TInfo); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8693 | return BuildVAArgExpr(BuiltinLoc, expr, TInfo, RPLoc); |
Abramo Bagnara | 2cad900 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 8694 | } |
| 8695 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8696 | ExprResult Sema::BuildVAArgExpr(SourceLocation BuiltinLoc, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8697 | Expr *E, TypeSourceInfo *TInfo, |
| 8698 | SourceLocation RPLoc) { |
Chris Lattner | 0d20b8a | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 8699 | Expr *OrigExpr = E; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8700 | |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 8701 | // Get the va_list type |
| 8702 | QualType VaListType = Context.getBuiltinVaListType(); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 8703 | if (VaListType->isArrayType()) { |
| 8704 | // Deal with implicit array decay; for example, on x86-64, |
| 8705 | // va_list is an array, but it's supposed to decay to |
| 8706 | // a pointer for va_arg. |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 8707 | VaListType = Context.getArrayDecayedType(VaListType); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 8708 | // Make sure the input expression also decays appropriately. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8709 | ExprResult Result = UsualUnaryConversions(E); |
| 8710 | if (Result.isInvalid()) |
| 8711 | return ExprError(); |
| 8712 | E = Result.take(); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 8713 | } else { |
| 8714 | // Otherwise, the va_list argument must be an l-value because |
| 8715 | // it is modified by va_arg. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8716 | if (!E->isTypeDependent() && |
Douglas Gregor | dd02730 | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 8717 | CheckForModifiableLvalue(E, BuiltinLoc, *this)) |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 8718 | return ExprError(); |
| 8719 | } |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 8720 | |
Douglas Gregor | dd02730 | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 8721 | if (!E->isTypeDependent() && |
| 8722 | !Context.hasSameType(VaListType, E->getType())) { |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8723 | return ExprError(Diag(E->getLocStart(), |
| 8724 | diag::err_first_argument_to_va_arg_not_of_type_va_list) |
Chris Lattner | 0d20b8a | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 8725 | << OrigExpr->getType() << E->getSourceRange()); |
Chris Lattner | 9dc8f19 | 2009-04-05 00:59:53 +0000 | [diff] [blame] | 8726 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8727 | |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 8728 | if (!TInfo->getType()->isDependentType()) { |
| 8729 | if (RequireCompleteType(TInfo->getTypeLoc().getBeginLoc(), TInfo->getType(), |
| 8730 | PDiag(diag::err_second_parameter_to_va_arg_incomplete) |
| 8731 | << TInfo->getTypeLoc().getSourceRange())) |
| 8732 | return ExprError(); |
David Majnemer | db11b01 | 2011-06-13 06:37:03 +0000 | [diff] [blame] | 8733 | |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 8734 | if (RequireNonAbstractType(TInfo->getTypeLoc().getBeginLoc(), |
| 8735 | TInfo->getType(), |
| 8736 | PDiag(diag::err_second_parameter_to_va_arg_abstract) |
| 8737 | << TInfo->getTypeLoc().getSourceRange())) |
| 8738 | return ExprError(); |
| 8739 | |
Douglas Gregor | 4eb7522 | 2011-07-30 06:45:27 +0000 | [diff] [blame] | 8740 | if (!TInfo->getType().isPODType(Context)) { |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 8741 | Diag(TInfo->getTypeLoc().getBeginLoc(), |
Douglas Gregor | 4eb7522 | 2011-07-30 06:45:27 +0000 | [diff] [blame] | 8742 | TInfo->getType()->isObjCLifetimeType() |
| 8743 | ? diag::warn_second_parameter_to_va_arg_ownership_qualified |
| 8744 | : diag::warn_second_parameter_to_va_arg_not_pod) |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 8745 | << TInfo->getType() |
| 8746 | << TInfo->getTypeLoc().getSourceRange(); |
Douglas Gregor | 4eb7522 | 2011-07-30 06:45:27 +0000 | [diff] [blame] | 8747 | } |
Eli Friedman | 46d37c1 | 2011-07-11 21:45:59 +0000 | [diff] [blame] | 8748 | |
| 8749 | // Check for va_arg where arguments of the given type will be promoted |
| 8750 | // (i.e. this va_arg is guaranteed to have undefined behavior). |
| 8751 | QualType PromoteType; |
| 8752 | if (TInfo->getType()->isPromotableIntegerType()) { |
| 8753 | PromoteType = Context.getPromotedIntegerType(TInfo->getType()); |
| 8754 | if (Context.typesAreCompatible(PromoteType, TInfo->getType())) |
| 8755 | PromoteType = QualType(); |
| 8756 | } |
| 8757 | if (TInfo->getType()->isSpecificBuiltinType(BuiltinType::Float)) |
| 8758 | PromoteType = Context.DoubleTy; |
| 8759 | if (!PromoteType.isNull()) |
| 8760 | Diag(TInfo->getTypeLoc().getBeginLoc(), |
| 8761 | diag::warn_second_parameter_to_va_arg_never_compatible) |
| 8762 | << TInfo->getType() |
| 8763 | << PromoteType |
| 8764 | << TInfo->getTypeLoc().getSourceRange(); |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 8765 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8766 | |
Abramo Bagnara | 2cad900 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 8767 | QualType T = TInfo->getType().getNonLValueExprType(Context); |
| 8768 | return Owned(new (Context) VAArgExpr(BuiltinLoc, E, TInfo, RPLoc, T)); |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 8769 | } |
| 8770 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8771 | ExprResult Sema::ActOnGNUNullExpr(SourceLocation TokenLoc) { |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 8772 | // The type of __null will be int or long, depending on the size of |
| 8773 | // pointers on the target. |
| 8774 | QualType Ty; |
NAKAMURA Takumi | 6e5658d | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 8775 | unsigned pw = Context.Target.getPointerWidth(0); |
| 8776 | if (pw == Context.Target.getIntWidth()) |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 8777 | Ty = Context.IntTy; |
NAKAMURA Takumi | 6e5658d | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 8778 | else if (pw == Context.Target.getLongWidth()) |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 8779 | Ty = Context.LongTy; |
NAKAMURA Takumi | 6e5658d | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 8780 | else if (pw == Context.Target.getLongLongWidth()) |
| 8781 | Ty = Context.LongLongTy; |
| 8782 | else { |
| 8783 | assert(!"I don't know size of pointer!"); |
| 8784 | Ty = Context.IntTy; |
| 8785 | } |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 8786 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8787 | return Owned(new (Context) GNUNullExpr(Ty, TokenLoc)); |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 8788 | } |
| 8789 | |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 8790 | static void MakeObjCStringLiteralFixItHint(Sema& SemaRef, QualType DstType, |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 8791 | Expr *SrcExpr, FixItHint &Hint) { |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 8792 | if (!SemaRef.getLangOptions().ObjC1) |
| 8793 | return; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8794 | |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 8795 | const ObjCObjectPointerType *PT = DstType->getAs<ObjCObjectPointerType>(); |
| 8796 | if (!PT) |
| 8797 | return; |
| 8798 | |
| 8799 | // Check if the destination is of type 'id'. |
| 8800 | if (!PT->isObjCIdType()) { |
| 8801 | // Check if the destination is the 'NSString' interface. |
| 8802 | const ObjCInterfaceDecl *ID = PT->getInterfaceDecl(); |
| 8803 | if (!ID || !ID->getIdentifier()->isStr("NSString")) |
| 8804 | return; |
| 8805 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8806 | |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 8807 | // Strip off any parens and casts. |
| 8808 | StringLiteral *SL = dyn_cast<StringLiteral>(SrcExpr->IgnoreParenCasts()); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 8809 | if (!SL || !SL->isAscii()) |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 8810 | return; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8811 | |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 8812 | Hint = FixItHint::CreateInsertion(SL->getLocStart(), "@"); |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 8813 | } |
| 8814 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8815 | bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, |
| 8816 | SourceLocation Loc, |
| 8817 | QualType DstType, QualType SrcType, |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 8818 | Expr *SrcExpr, AssignmentAction Action, |
| 8819 | bool *Complained) { |
| 8820 | if (Complained) |
| 8821 | *Complained = false; |
| 8822 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8823 | // Decode the result (notice that AST's are still created for extensions). |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 8824 | bool CheckInferredResultType = false; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8825 | bool isInvalid = false; |
| 8826 | unsigned DiagKind; |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 8827 | FixItHint Hint; |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 8828 | ConversionFixItGenerator ConvHints; |
| 8829 | bool MayHaveConvFixit = false; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8830 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8831 | switch (ConvTy) { |
| 8832 | default: assert(0 && "Unknown conversion type"); |
| 8833 | case Compatible: return false; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 8834 | case PointerToInt: |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8835 | DiagKind = diag::ext_typecheck_convert_pointer_int; |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 8836 | ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); |
| 8837 | MayHaveConvFixit = true; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8838 | break; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 8839 | case IntToPointer: |
| 8840 | DiagKind = diag::ext_typecheck_convert_int_pointer; |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 8841 | ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); |
| 8842 | MayHaveConvFixit = true; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 8843 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8844 | case IncompatiblePointer: |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 8845 | MakeObjCStringLiteralFixItHint(*this, DstType, SrcExpr, Hint); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8846 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer; |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 8847 | CheckInferredResultType = DstType->isObjCObjectPointerType() && |
| 8848 | SrcType->isObjCObjectPointerType(); |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 8849 | if (Hint.isNull() && !CheckInferredResultType) { |
| 8850 | ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); |
| 8851 | } |
| 8852 | MayHaveConvFixit = true; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8853 | break; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 8854 | case IncompatiblePointerSign: |
| 8855 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer_sign; |
| 8856 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8857 | case FunctionVoidPointer: |
| 8858 | DiagKind = diag::ext_typecheck_convert_pointer_void_func; |
| 8859 | break; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 8860 | case IncompatiblePointerDiscardsQualifiers: { |
John McCall | 40249e7 | 2011-02-01 23:28:01 +0000 | [diff] [blame] | 8861 | // Perform array-to-pointer decay if necessary. |
| 8862 | if (SrcType->isArrayType()) SrcType = Context.getArrayDecayedType(SrcType); |
| 8863 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 8864 | Qualifiers lhq = SrcType->getPointeeType().getQualifiers(); |
| 8865 | Qualifiers rhq = DstType->getPointeeType().getQualifiers(); |
| 8866 | if (lhq.getAddressSpace() != rhq.getAddressSpace()) { |
| 8867 | DiagKind = diag::err_typecheck_incompatible_address_space; |
| 8868 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8869 | |
| 8870 | |
| 8871 | } else if (lhq.getObjCLifetime() != rhq.getObjCLifetime()) { |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 8872 | DiagKind = diag::err_typecheck_incompatible_ownership; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8873 | break; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 8874 | } |
| 8875 | |
| 8876 | llvm_unreachable("unknown error case for discarding qualifiers!"); |
| 8877 | // fallthrough |
| 8878 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8879 | case CompatiblePointerDiscardsQualifiers: |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 8880 | // If the qualifiers lost were because we were applying the |
| 8881 | // (deprecated) C++ conversion from a string literal to a char* |
| 8882 | // (or wchar_t*), then there was no error (C++ 4.2p2). FIXME: |
| 8883 | // Ideally, this check would be performed in |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 8884 | // checkPointerTypesForAssignment. However, that would require a |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 8885 | // bit of refactoring (so that the second argument is an |
| 8886 | // expression, rather than a type), which should be done as part |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 8887 | // of a larger effort to fix checkPointerTypesForAssignment for |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 8888 | // C++ semantics. |
| 8889 | if (getLangOptions().CPlusPlus && |
| 8890 | IsStringLiteralToNonConstPointerConversion(SrcExpr, DstType)) |
| 8891 | return false; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8892 | DiagKind = diag::ext_typecheck_convert_discards_qualifiers; |
| 8893 | break; |
Sean Hunt | c9132b6 | 2009-11-08 07:46:34 +0000 | [diff] [blame] | 8894 | case IncompatibleNestedPointerQualifiers: |
Fariborz Jahanian | 3451e92 | 2009-11-09 22:16:37 +0000 | [diff] [blame] | 8895 | DiagKind = diag::ext_nested_pointer_qualifier_mismatch; |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 8896 | break; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 8897 | case IntToBlockPointer: |
| 8898 | DiagKind = diag::err_int_to_block_pointer; |
| 8899 | break; |
| 8900 | case IncompatibleBlockPointer: |
Mike Stump | 25efa10 | 2009-04-21 22:51:42 +0000 | [diff] [blame] | 8901 | DiagKind = diag::err_typecheck_convert_incompatible_block_pointer; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 8902 | break; |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 8903 | case IncompatibleObjCQualifiedId: |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8904 | // FIXME: Diagnose the problem in ObjCQualifiedIdTypesAreCompatible, since |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 8905 | // it can give a more specific diagnostic. |
| 8906 | DiagKind = diag::warn_incompatible_qualified_id; |
| 8907 | break; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 8908 | case IncompatibleVectors: |
| 8909 | DiagKind = diag::warn_incompatible_vectors; |
| 8910 | break; |
Fariborz Jahanian | 04e5a25 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 8911 | case IncompatibleObjCWeakRef: |
| 8912 | DiagKind = diag::err_arc_weak_unavailable_assign; |
| 8913 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8914 | case Incompatible: |
| 8915 | DiagKind = diag::err_typecheck_convert_incompatible; |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 8916 | ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); |
| 8917 | MayHaveConvFixit = true; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8918 | isInvalid = true; |
| 8919 | break; |
| 8920 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8921 | |
Douglas Gregor | d4eea83 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 8922 | QualType FirstType, SecondType; |
| 8923 | switch (Action) { |
| 8924 | case AA_Assigning: |
| 8925 | case AA_Initializing: |
| 8926 | // The destination type comes first. |
| 8927 | FirstType = DstType; |
| 8928 | SecondType = SrcType; |
| 8929 | break; |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 8930 | |
Douglas Gregor | d4eea83 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 8931 | case AA_Returning: |
| 8932 | case AA_Passing: |
| 8933 | case AA_Converting: |
| 8934 | case AA_Sending: |
| 8935 | case AA_Casting: |
| 8936 | // The source type comes first. |
| 8937 | FirstType = SrcType; |
| 8938 | SecondType = DstType; |
| 8939 | break; |
| 8940 | } |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 8941 | |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 8942 | PartialDiagnostic FDiag = PDiag(DiagKind); |
| 8943 | FDiag << FirstType << SecondType << Action << SrcExpr->getSourceRange(); |
| 8944 | |
| 8945 | // If we can fix the conversion, suggest the FixIts. |
| 8946 | assert(ConvHints.isNull() || Hint.isNull()); |
| 8947 | if (!ConvHints.isNull()) { |
| 8948 | for (llvm::SmallVector<FixItHint, 1>::iterator |
| 8949 | HI = ConvHints.Hints.begin(), HE = ConvHints.Hints.end(); |
| 8950 | HI != HE; ++HI) |
| 8951 | FDiag << *HI; |
| 8952 | } else { |
| 8953 | FDiag << Hint; |
| 8954 | } |
| 8955 | if (MayHaveConvFixit) { FDiag << (unsigned) (ConvHints.Kind); } |
| 8956 | |
| 8957 | Diag(Loc, FDiag); |
| 8958 | |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 8959 | if (CheckInferredResultType) |
| 8960 | EmitRelatedResultTypeNote(SrcExpr); |
| 8961 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 8962 | if (Complained) |
| 8963 | *Complained = true; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8964 | return isInvalid; |
| 8965 | } |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 8966 | |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 8967 | bool Sema::VerifyIntegerConstantExpression(const Expr *E, llvm::APSInt *Result){ |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 8968 | llvm::APSInt ICEResult; |
| 8969 | if (E->isIntegerConstantExpr(ICEResult, Context)) { |
| 8970 | if (Result) |
| 8971 | *Result = ICEResult; |
| 8972 | return false; |
| 8973 | } |
| 8974 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 8975 | Expr::EvalResult EvalResult; |
| 8976 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8977 | if (!E->Evaluate(EvalResult, Context) || !EvalResult.Val.isInt() || |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 8978 | EvalResult.HasSideEffects) { |
| 8979 | Diag(E->getExprLoc(), diag::err_expr_not_ice) << E->getSourceRange(); |
| 8980 | |
| 8981 | if (EvalResult.Diag) { |
| 8982 | // We only show the note if it's not the usual "invalid subexpression" |
| 8983 | // or if it's actually in a subexpression. |
| 8984 | if (EvalResult.Diag != diag::note_invalid_subexpr_in_ice || |
| 8985 | E->IgnoreParens() != EvalResult.DiagExpr->IgnoreParens()) |
| 8986 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
| 8987 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8988 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 8989 | return true; |
| 8990 | } |
| 8991 | |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 8992 | Diag(E->getExprLoc(), diag::ext_expr_not_ice) << |
| 8993 | E->getSourceRange(); |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 8994 | |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 8995 | if (EvalResult.Diag && |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 8996 | Diags.getDiagnosticLevel(diag::ext_expr_not_ice, EvalResult.DiagLoc) |
| 8997 | != Diagnostic::Ignored) |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 8998 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8999 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9000 | if (Result) |
| 9001 | *Result = EvalResult.Val.getInt(); |
| 9002 | return false; |
| 9003 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9004 | |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9005 | void |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9006 | Sema::PushExpressionEvaluationContext(ExpressionEvaluationContext NewContext) { |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9007 | ExprEvalContexts.push_back( |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9008 | ExpressionEvaluationContextRecord(NewContext, |
| 9009 | ExprTemporaries.size(), |
| 9010 | ExprNeedsCleanups)); |
| 9011 | ExprNeedsCleanups = false; |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9012 | } |
| 9013 | |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 9014 | void Sema::PopExpressionEvaluationContext() { |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9015 | // Pop the current expression evaluation context off the stack. |
| 9016 | ExpressionEvaluationContextRecord Rec = ExprEvalContexts.back(); |
| 9017 | ExprEvalContexts.pop_back(); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9018 | |
Douglas Gregor | 06d3369 | 2009-12-12 07:57:52 +0000 | [diff] [blame] | 9019 | if (Rec.Context == PotentiallyPotentiallyEvaluated) { |
| 9020 | if (Rec.PotentiallyReferenced) { |
| 9021 | // Mark any remaining declarations in the current position of the stack |
| 9022 | // as "referenced". If they were not meant to be referenced, semantic |
| 9023 | // analysis would have eliminated them (e.g., in ActOnCXXTypeId). |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9024 | for (PotentiallyReferencedDecls::iterator |
Douglas Gregor | 06d3369 | 2009-12-12 07:57:52 +0000 | [diff] [blame] | 9025 | I = Rec.PotentiallyReferenced->begin(), |
| 9026 | IEnd = Rec.PotentiallyReferenced->end(); |
| 9027 | I != IEnd; ++I) |
| 9028 | MarkDeclarationReferenced(I->first, I->second); |
| 9029 | } |
| 9030 | |
| 9031 | if (Rec.PotentiallyDiagnosed) { |
| 9032 | // Emit any pending diagnostics. |
| 9033 | for (PotentiallyEmittedDiagnostics::iterator |
| 9034 | I = Rec.PotentiallyDiagnosed->begin(), |
| 9035 | IEnd = Rec.PotentiallyDiagnosed->end(); |
| 9036 | I != IEnd; ++I) |
| 9037 | Diag(I->first, I->second); |
| 9038 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9039 | } |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9040 | |
| 9041 | // When are coming out of an unevaluated context, clear out any |
| 9042 | // temporaries that we may have created as part of the evaluation of |
| 9043 | // the expression in that context: they aren't relevant because they |
| 9044 | // will never be constructed. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9045 | if (Rec.Context == Unevaluated) { |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9046 | ExprTemporaries.erase(ExprTemporaries.begin() + Rec.NumTemporaries, |
| 9047 | ExprTemporaries.end()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9048 | ExprNeedsCleanups = Rec.ParentNeedsCleanups; |
| 9049 | |
| 9050 | // Otherwise, merge the contexts together. |
| 9051 | } else { |
| 9052 | ExprNeedsCleanups |= Rec.ParentNeedsCleanups; |
| 9053 | } |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9054 | |
| 9055 | // Destroy the popped expression evaluation record. |
| 9056 | Rec.Destroy(); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9057 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9058 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9059 | void Sema::DiscardCleanupsInEvaluationContext() { |
| 9060 | ExprTemporaries.erase( |
| 9061 | ExprTemporaries.begin() + ExprEvalContexts.back().NumTemporaries, |
| 9062 | ExprTemporaries.end()); |
| 9063 | ExprNeedsCleanups = false; |
| 9064 | } |
| 9065 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9066 | /// \brief Note that the given declaration was referenced in the source code. |
| 9067 | /// |
| 9068 | /// This routine should be invoke whenever a given declaration is referenced |
| 9069 | /// in the source code, and where that reference occurred. If this declaration |
| 9070 | /// reference means that the the declaration is used (C++ [basic.def.odr]p2, |
| 9071 | /// C99 6.9p3), then the declaration will be marked as used. |
| 9072 | /// |
| 9073 | /// \param Loc the location where the declaration was referenced. |
| 9074 | /// |
| 9075 | /// \param D the declaration that has been referenced by the source code. |
| 9076 | void Sema::MarkDeclarationReferenced(SourceLocation Loc, Decl *D) { |
| 9077 | assert(D && "No declaration?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9078 | |
Argyrios Kyrtzidis | 6b6b42a | 2011-04-19 19:51:10 +0000 | [diff] [blame] | 9079 | D->setReferenced(); |
| 9080 | |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 9081 | if (D->isUsed(false)) |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 9082 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9083 | |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 9084 | // Mark a parameter or variable declaration "used", regardless of whether |
| 9085 | // we're in a template or not. The reason for this is that unevaluated |
| 9086 | // expressions (e.g. (void)sizeof()) constitute a use for warning purposes |
| 9087 | // (-Wunused-variables and -Wunused-parameters) |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9088 | if (isa<ParmVarDecl>(D) || |
Douglas Gregor | fc2ca56 | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 9089 | (isa<VarDecl>(D) && D->getDeclContext()->isFunctionOrMethod())) { |
Anders Carlsson | 2127ecc | 2010-10-22 23:37:08 +0000 | [diff] [blame] | 9090 | D->setUsed(); |
Douglas Gregor | fc2ca56 | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 9091 | return; |
| 9092 | } |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9093 | |
Douglas Gregor | fc2ca56 | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 9094 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) |
| 9095 | return; |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9096 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9097 | // Do not mark anything as "used" within a dependent context; wait for |
| 9098 | // an instantiation. |
| 9099 | if (CurContext->isDependentContext()) |
| 9100 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9101 | |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9102 | switch (ExprEvalContexts.back().Context) { |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9103 | case Unevaluated: |
| 9104 | // We are in an expression that is not potentially evaluated; do nothing. |
| 9105 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9106 | |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9107 | case PotentiallyEvaluated: |
| 9108 | // We are in a potentially-evaluated expression, so this declaration is |
| 9109 | // "used"; handle this below. |
| 9110 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9111 | |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9112 | case PotentiallyPotentiallyEvaluated: |
| 9113 | // We are in an expression that may be potentially evaluated; queue this |
| 9114 | // declaration reference until we know whether the expression is |
| 9115 | // potentially evaluated. |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9116 | ExprEvalContexts.back().addReferencedDecl(Loc, D); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9117 | return; |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9118 | |
| 9119 | case PotentiallyEvaluatedIfUsed: |
| 9120 | // Referenced declarations will only be used if the construct in the |
| 9121 | // containing expression is used. |
| 9122 | return; |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9123 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9124 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9125 | // Note that this declaration has been used. |
Fariborz Jahanian | b7f4cc0 | 2009-06-22 17:30:33 +0000 | [diff] [blame] | 9126 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
Sean Hunt | 1e23865 | 2011-05-12 03:51:51 +0000 | [diff] [blame] | 9127 | if (Constructor->isDefaulted() && Constructor->isDefaultConstructor()) { |
| 9128 | if (Constructor->isTrivial()) |
Chandler Carruth | 4e6fbce | 2010-08-23 07:55:51 +0000 | [diff] [blame] | 9129 | return; |
| 9130 | if (!Constructor->isUsed(false)) |
| 9131 | DefineImplicitDefaultConstructor(Loc, Constructor); |
Sean Hunt | 509f048 | 2011-05-14 18:20:50 +0000 | [diff] [blame] | 9132 | } else if (Constructor->isDefaulted() && |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 9133 | Constructor->isCopyConstructor()) { |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 9134 | if (!Constructor->isUsed(false)) |
Sean Hunt | 49634cf | 2011-05-13 06:10:58 +0000 | [diff] [blame] | 9135 | DefineImplicitCopyConstructor(Loc, Constructor); |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 9136 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9137 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9138 | MarkVTableUsed(Loc, Constructor->getParent()); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 9139 | } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) { |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 9140 | if (Destructor->isDefaulted() && !Destructor->isUsed(false)) |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 9141 | DefineImplicitDestructor(Loc, Destructor); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9142 | if (Destructor->isVirtual()) |
| 9143 | MarkVTableUsed(Loc, Destructor->getParent()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 9144 | } else if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) { |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 9145 | if (MethodDecl->isDefaulted() && MethodDecl->isOverloadedOperator() && |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 9146 | MethodDecl->getOverloadedOperator() == OO_Equal) { |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 9147 | if (!MethodDecl->isUsed(false)) |
Douglas Gregor | 39957dc | 2010-05-01 15:04:51 +0000 | [diff] [blame] | 9148 | DefineImplicitCopyAssignment(Loc, MethodDecl); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9149 | } else if (MethodDecl->isVirtual()) |
| 9150 | MarkVTableUsed(Loc, MethodDecl->getParent()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 9151 | } |
Fariborz Jahanian | f5ed9e0 | 2009-06-24 22:09:44 +0000 | [diff] [blame] | 9152 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9153 | // Recursive functions should be marked when used from another function. |
| 9154 | if (CurContext == Function) return; |
| 9155 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9156 | // Implicit instantiation of function templates and member functions of |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 9157 | // class templates. |
Douglas Gregor | 6cfacfe | 2010-05-17 17:34:56 +0000 | [diff] [blame] | 9158 | if (Function->isImplicitlyInstantiable()) { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9159 | bool AlreadyInstantiated = false; |
| 9160 | if (FunctionTemplateSpecializationInfo *SpecInfo |
| 9161 | = Function->getTemplateSpecializationInfo()) { |
| 9162 | if (SpecInfo->getPointOfInstantiation().isInvalid()) |
| 9163 | SpecInfo->setPointOfInstantiation(Loc); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9164 | else if (SpecInfo->getTemplateSpecializationKind() |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 9165 | == TSK_ImplicitInstantiation) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9166 | AlreadyInstantiated = true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9167 | } else if (MemberSpecializationInfo *MSInfo |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9168 | = Function->getMemberSpecializationInfo()) { |
| 9169 | if (MSInfo->getPointOfInstantiation().isInvalid()) |
| 9170 | MSInfo->setPointOfInstantiation(Loc); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9171 | else if (MSInfo->getTemplateSpecializationKind() |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 9172 | == TSK_ImplicitInstantiation) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9173 | AlreadyInstantiated = true; |
| 9174 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9175 | |
Douglas Gregor | 60406be | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 9176 | if (!AlreadyInstantiated) { |
| 9177 | if (isa<CXXRecordDecl>(Function->getDeclContext()) && |
| 9178 | cast<CXXRecordDecl>(Function->getDeclContext())->isLocalClass()) |
| 9179 | PendingLocalImplicitInstantiations.push_back(std::make_pair(Function, |
| 9180 | Loc)); |
| 9181 | else |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 9182 | PendingInstantiations.push_back(std::make_pair(Function, Loc)); |
Douglas Gregor | 60406be | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 9183 | } |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9184 | } else { |
| 9185 | // Walk redefinitions, as some of them may be instantiable. |
Gabor Greif | 40181c4 | 2010-08-28 00:16:06 +0000 | [diff] [blame] | 9186 | for (FunctionDecl::redecl_iterator i(Function->redecls_begin()), |
| 9187 | e(Function->redecls_end()); i != e; ++i) { |
Gabor Greif | be9ebe3 | 2010-08-28 01:58:12 +0000 | [diff] [blame] | 9188 | if (!i->isUsed(false) && i->isImplicitlyInstantiable()) |
Gabor Greif | 40181c4 | 2010-08-28 00:16:06 +0000 | [diff] [blame] | 9189 | MarkDeclarationReferenced(Loc, *i); |
| 9190 | } |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9191 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9192 | |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9193 | // Keep track of used but undefined functions. |
| 9194 | if (!Function->isPure() && !Function->hasBody() && |
| 9195 | Function->getLinkage() != ExternalLinkage) { |
| 9196 | SourceLocation &old = UndefinedInternals[Function->getCanonicalDecl()]; |
| 9197 | if (old.isInvalid()) old = Loc; |
| 9198 | } |
Argyrios Kyrtzidis | 58b5259 | 2010-08-25 10:34:54 +0000 | [diff] [blame] | 9199 | |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9200 | Function->setUsed(true); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9201 | return; |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 9202 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9203 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9204 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 9205 | // Implicit instantiation of static data members of class templates. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9206 | if (Var->isStaticDataMember() && |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9207 | Var->getInstantiatedFromStaticDataMember()) { |
| 9208 | MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo(); |
| 9209 | assert(MSInfo && "Missing member specialization information?"); |
| 9210 | if (MSInfo->getPointOfInstantiation().isInvalid() && |
| 9211 | MSInfo->getTemplateSpecializationKind()== TSK_ImplicitInstantiation) { |
| 9212 | MSInfo->setPointOfInstantiation(Loc); |
Sebastian Redl | f79a719 | 2011-04-29 08:19:30 +0000 | [diff] [blame] | 9213 | // This is a modification of an existing AST node. Notify listeners. |
| 9214 | if (ASTMutationListener *L = getASTMutationListener()) |
| 9215 | L->StaticDataMemberInstantiated(Var); |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 9216 | PendingInstantiations.push_back(std::make_pair(Var, Loc)); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9217 | } |
| 9218 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9219 | |
John McCall | 77efc68 | 2011-02-21 19:25:48 +0000 | [diff] [blame] | 9220 | // Keep track of used but undefined variables. We make a hole in |
| 9221 | // the warning for static const data members with in-line |
| 9222 | // initializers. |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9223 | if (Var->hasDefinition() == VarDecl::DeclarationOnly |
John McCall | 77efc68 | 2011-02-21 19:25:48 +0000 | [diff] [blame] | 9224 | && Var->getLinkage() != ExternalLinkage |
| 9225 | && !(Var->isStaticDataMember() && Var->hasInit())) { |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9226 | SourceLocation &old = UndefinedInternals[Var->getCanonicalDecl()]; |
| 9227 | if (old.isInvalid()) old = Loc; |
| 9228 | } |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 9229 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9230 | D->setUsed(true); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 9231 | return; |
Sam Weinig | cce6ebc | 2009-09-11 03:29:30 +0000 | [diff] [blame] | 9232 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9233 | } |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 9234 | |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9235 | namespace { |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9236 | // Mark all of the declarations referenced |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9237 | // FIXME: Not fully implemented yet! We need to have a better understanding |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9238 | // of when we're entering |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9239 | class MarkReferencedDecls : public RecursiveASTVisitor<MarkReferencedDecls> { |
| 9240 | Sema &S; |
| 9241 | SourceLocation Loc; |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9242 | |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9243 | public: |
| 9244 | typedef RecursiveASTVisitor<MarkReferencedDecls> Inherited; |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9245 | |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9246 | MarkReferencedDecls(Sema &S, SourceLocation Loc) : S(S), Loc(Loc) { } |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9247 | |
| 9248 | bool TraverseTemplateArgument(const TemplateArgument &Arg); |
| 9249 | bool TraverseRecordType(RecordType *T); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9250 | }; |
| 9251 | } |
| 9252 | |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9253 | bool MarkReferencedDecls::TraverseTemplateArgument( |
| 9254 | const TemplateArgument &Arg) { |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9255 | if (Arg.getKind() == TemplateArgument::Declaration) { |
| 9256 | S.MarkDeclarationReferenced(Loc, Arg.getAsDecl()); |
| 9257 | } |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9258 | |
| 9259 | return Inherited::TraverseTemplateArgument(Arg); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9260 | } |
| 9261 | |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9262 | bool MarkReferencedDecls::TraverseRecordType(RecordType *T) { |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9263 | if (ClassTemplateSpecializationDecl *Spec |
| 9264 | = dyn_cast<ClassTemplateSpecializationDecl>(T->getDecl())) { |
| 9265 | const TemplateArgumentList &Args = Spec->getTemplateArgs(); |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 9266 | return TraverseTemplateArguments(Args.data(), Args.size()); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9267 | } |
| 9268 | |
Chandler Carruth | e3e210c | 2010-06-10 10:31:57 +0000 | [diff] [blame] | 9269 | return true; |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9270 | } |
| 9271 | |
| 9272 | void Sema::MarkDeclarationsReferencedInType(SourceLocation Loc, QualType T) { |
| 9273 | MarkReferencedDecls Marker(*this, Loc); |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9274 | Marker.TraverseType(Context.getCanonicalType(T)); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9275 | } |
| 9276 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9277 | namespace { |
| 9278 | /// \brief Helper class that marks all of the declarations referenced by |
| 9279 | /// potentially-evaluated subexpressions as "referenced". |
| 9280 | class EvaluatedExprMarker : public EvaluatedExprVisitor<EvaluatedExprMarker> { |
| 9281 | Sema &S; |
| 9282 | |
| 9283 | public: |
| 9284 | typedef EvaluatedExprVisitor<EvaluatedExprMarker> Inherited; |
| 9285 | |
| 9286 | explicit EvaluatedExprMarker(Sema &S) : Inherited(S.Context), S(S) { } |
| 9287 | |
| 9288 | void VisitDeclRefExpr(DeclRefExpr *E) { |
| 9289 | S.MarkDeclarationReferenced(E->getLocation(), E->getDecl()); |
| 9290 | } |
| 9291 | |
| 9292 | void VisitMemberExpr(MemberExpr *E) { |
| 9293 | S.MarkDeclarationReferenced(E->getMemberLoc(), E->getMemberDecl()); |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 9294 | Inherited::VisitMemberExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9295 | } |
| 9296 | |
| 9297 | void VisitCXXNewExpr(CXXNewExpr *E) { |
| 9298 | if (E->getConstructor()) |
| 9299 | S.MarkDeclarationReferenced(E->getLocStart(), E->getConstructor()); |
| 9300 | if (E->getOperatorNew()) |
| 9301 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorNew()); |
| 9302 | if (E->getOperatorDelete()) |
| 9303 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorDelete()); |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 9304 | Inherited::VisitCXXNewExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9305 | } |
| 9306 | |
| 9307 | void VisitCXXDeleteExpr(CXXDeleteExpr *E) { |
| 9308 | if (E->getOperatorDelete()) |
| 9309 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorDelete()); |
Douglas Gregor | 5833b0b | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 9310 | QualType Destroyed = S.Context.getBaseElementType(E->getDestroyedType()); |
| 9311 | if (const RecordType *DestroyedRec = Destroyed->getAs<RecordType>()) { |
| 9312 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DestroyedRec->getDecl()); |
| 9313 | S.MarkDeclarationReferenced(E->getLocStart(), |
| 9314 | S.LookupDestructor(Record)); |
| 9315 | } |
| 9316 | |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 9317 | Inherited::VisitCXXDeleteExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9318 | } |
| 9319 | |
| 9320 | void VisitCXXConstructExpr(CXXConstructExpr *E) { |
| 9321 | S.MarkDeclarationReferenced(E->getLocStart(), E->getConstructor()); |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 9322 | Inherited::VisitCXXConstructExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9323 | } |
| 9324 | |
| 9325 | void VisitBlockDeclRefExpr(BlockDeclRefExpr *E) { |
| 9326 | S.MarkDeclarationReferenced(E->getLocation(), E->getDecl()); |
| 9327 | } |
Douglas Gregor | 102ff97 | 2010-10-19 17:17:35 +0000 | [diff] [blame] | 9328 | |
| 9329 | void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { |
| 9330 | Visit(E->getExpr()); |
| 9331 | } |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9332 | }; |
| 9333 | } |
| 9334 | |
| 9335 | /// \brief Mark any declarations that appear within this expression or any |
| 9336 | /// potentially-evaluated subexpressions as "referenced". |
| 9337 | void Sema::MarkDeclarationsReferencedInExpr(Expr *E) { |
| 9338 | EvaluatedExprMarker(*this).Visit(E); |
| 9339 | } |
| 9340 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9341 | /// \brief Emit a diagnostic that describes an effect on the run-time behavior |
| 9342 | /// of the program being compiled. |
| 9343 | /// |
| 9344 | /// This routine emits the given diagnostic when the code currently being |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9345 | /// type-checked is "potentially evaluated", meaning that there is a |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9346 | /// possibility that the code will actually be executable. Code in sizeof() |
| 9347 | /// expressions, code used only during overload resolution, etc., are not |
| 9348 | /// potentially evaluated. This routine will suppress such diagnostics or, |
| 9349 | /// in the absolutely nutty case of potentially potentially evaluated |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9350 | /// expressions (C++ typeid), queue the diagnostic to potentially emit it |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9351 | /// later. |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9352 | /// |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9353 | /// This routine should be used for all diagnostics that describe the run-time |
| 9354 | /// behavior of a program, such as passing a non-POD value through an ellipsis. |
| 9355 | /// Failure to do so will likely result in spurious diagnostics or failures |
| 9356 | /// during overload resolution or within sizeof/alignof/typeof/typeid. |
Ted Kremenek | 762696f | 2011-02-23 01:51:43 +0000 | [diff] [blame] | 9357 | bool Sema::DiagRuntimeBehavior(SourceLocation Loc, const Stmt *stmt, |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9358 | const PartialDiagnostic &PD) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9359 | switch (ExprEvalContexts.back().Context) { |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9360 | case Unevaluated: |
| 9361 | // The argument will never be evaluated, so don't complain. |
| 9362 | break; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9363 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9364 | case PotentiallyEvaluated: |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9365 | case PotentiallyEvaluatedIfUsed: |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 9366 | if (stmt && getCurFunctionOrMethodDecl()) { |
| 9367 | FunctionScopes.back()->PossiblyUnreachableDiags. |
| 9368 | push_back(sema::PossiblyUnreachableDiag(PD, Loc, stmt)); |
| 9369 | } |
| 9370 | else |
| 9371 | Diag(Loc, PD); |
| 9372 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9373 | return true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9374 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9375 | case PotentiallyPotentiallyEvaluated: |
| 9376 | ExprEvalContexts.back().addDiagnostic(Loc, PD); |
| 9377 | break; |
| 9378 | } |
| 9379 | |
| 9380 | return false; |
| 9381 | } |
| 9382 | |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 9383 | bool Sema::CheckCallReturnType(QualType ReturnType, SourceLocation Loc, |
| 9384 | CallExpr *CE, FunctionDecl *FD) { |
| 9385 | if (ReturnType->isVoidType() || !ReturnType->isIncompleteType()) |
| 9386 | return false; |
| 9387 | |
| 9388 | PartialDiagnostic Note = |
| 9389 | FD ? PDiag(diag::note_function_with_incomplete_return_type_declared_here) |
| 9390 | << FD->getDeclName() : PDiag(); |
| 9391 | SourceLocation NoteLoc = FD ? FD->getLocation() : SourceLocation(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9392 | |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 9393 | if (RequireCompleteType(Loc, ReturnType, |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9394 | FD ? |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 9395 | PDiag(diag::err_call_function_incomplete_return) |
| 9396 | << CE->getSourceRange() << FD->getDeclName() : |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9397 | PDiag(diag::err_call_incomplete_return) |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 9398 | << CE->getSourceRange(), |
| 9399 | std::make_pair(NoteLoc, Note))) |
| 9400 | return true; |
| 9401 | |
| 9402 | return false; |
| 9403 | } |
| 9404 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9405 | // Diagnose the s/=/==/ and s/\|=/!=/ typos. Note that adding parentheses |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9406 | // will prevent this condition from triggering, which is what we want. |
| 9407 | void Sema::DiagnoseAssignmentAsCondition(Expr *E) { |
| 9408 | SourceLocation Loc; |
| 9409 | |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 9410 | unsigned diagnostic = diag::warn_condition_is_assignment; |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9411 | bool IsOrAssign = false; |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 9412 | |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9413 | if (isa<BinaryOperator>(E)) { |
| 9414 | BinaryOperator *Op = cast<BinaryOperator>(E); |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9415 | if (Op->getOpcode() != BO_Assign && Op->getOpcode() != BO_OrAssign) |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9416 | return; |
| 9417 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9418 | IsOrAssign = Op->getOpcode() == BO_OrAssign; |
| 9419 | |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 9420 | // Greylist some idioms by putting them into a warning subcategory. |
| 9421 | if (ObjCMessageExpr *ME |
| 9422 | = dyn_cast<ObjCMessageExpr>(Op->getRHS()->IgnoreParenCasts())) { |
| 9423 | Selector Sel = ME->getSelector(); |
| 9424 | |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 9425 | // self = [<foo> init...] |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 9426 | if (isSelfExpr(Op->getLHS()) && Sel.getNameForSlot(0).startswith("init")) |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 9427 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 9428 | |
| 9429 | // <foo> = [<bar> nextObject] |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 9430 | else if (Sel.isUnarySelector() && Sel.getNameForSlot(0) == "nextObject") |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 9431 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 9432 | } |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 9433 | |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9434 | Loc = Op->getOperatorLoc(); |
| 9435 | } else if (isa<CXXOperatorCallExpr>(E)) { |
| 9436 | CXXOperatorCallExpr *Op = cast<CXXOperatorCallExpr>(E); |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9437 | if (Op->getOperator() != OO_Equal && Op->getOperator() != OO_PipeEqual) |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9438 | return; |
| 9439 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9440 | IsOrAssign = Op->getOperator() == OO_PipeEqual; |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9441 | Loc = Op->getOperatorLoc(); |
| 9442 | } else { |
| 9443 | // Not an assignment. |
| 9444 | return; |
| 9445 | } |
| 9446 | |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 9447 | Diag(Loc, diagnostic) << E->getSourceRange(); |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9448 | |
Argyrios Kyrtzidis | abdd3b3 | 2011-04-25 23:01:29 +0000 | [diff] [blame] | 9449 | SourceLocation Open = E->getSourceRange().getBegin(); |
| 9450 | SourceLocation Close = PP.getLocForEndOfToken(E->getSourceRange().getEnd()); |
| 9451 | Diag(Loc, diag::note_condition_assign_silence) |
| 9452 | << FixItHint::CreateInsertion(Open, "(") |
| 9453 | << FixItHint::CreateInsertion(Close, ")"); |
| 9454 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9455 | if (IsOrAssign) |
| 9456 | Diag(Loc, diag::note_condition_or_assign_to_comparison) |
| 9457 | << FixItHint::CreateReplacement(Loc, "!="); |
| 9458 | else |
| 9459 | Diag(Loc, diag::note_condition_assign_to_comparison) |
| 9460 | << FixItHint::CreateReplacement(Loc, "=="); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9461 | } |
| 9462 | |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 9463 | /// \brief Redundant parentheses over an equality comparison can indicate |
| 9464 | /// that the user intended an assignment used as condition. |
| 9465 | void Sema::DiagnoseEqualityWithExtraParens(ParenExpr *parenE) { |
Argyrios Kyrtzidis | cf1620a | 2011-02-01 22:23:56 +0000 | [diff] [blame] | 9466 | // Don't warn if the parens came from a macro. |
| 9467 | SourceLocation parenLoc = parenE->getLocStart(); |
| 9468 | if (parenLoc.isInvalid() || parenLoc.isMacroID()) |
| 9469 | return; |
Argyrios Kyrtzidis | 170a6a2 | 2011-03-28 23:52:04 +0000 | [diff] [blame] | 9470 | // Don't warn for dependent expressions. |
| 9471 | if (parenE->isTypeDependent()) |
| 9472 | return; |
Argyrios Kyrtzidis | cf1620a | 2011-02-01 22:23:56 +0000 | [diff] [blame] | 9473 | |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 9474 | Expr *E = parenE->IgnoreParens(); |
| 9475 | |
| 9476 | if (BinaryOperator *opE = dyn_cast<BinaryOperator>(E)) |
Argyrios Kyrtzidis | 70f2330 | 2011-02-01 19:32:59 +0000 | [diff] [blame] | 9477 | if (opE->getOpcode() == BO_EQ && |
| 9478 | opE->getLHS()->IgnoreParenImpCasts()->isModifiableLvalue(Context) |
| 9479 | == Expr::MLV_Valid) { |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 9480 | SourceLocation Loc = opE->getOperatorLoc(); |
Ted Kremenek | 006ae38 | 2011-02-01 22:36:09 +0000 | [diff] [blame] | 9481 | |
Ted Kremenek | f7275cd | 2011-02-02 02:20:30 +0000 | [diff] [blame] | 9482 | Diag(Loc, diag::warn_equality_with_extra_parens) << E->getSourceRange(); |
Ted Kremenek | f7275cd | 2011-02-02 02:20:30 +0000 | [diff] [blame] | 9483 | Diag(Loc, diag::note_equality_comparison_silence) |
| 9484 | << FixItHint::CreateRemoval(parenE->getSourceRange().getBegin()) |
| 9485 | << FixItHint::CreateRemoval(parenE->getSourceRange().getEnd()); |
Argyrios Kyrtzidis | abdd3b3 | 2011-04-25 23:01:29 +0000 | [diff] [blame] | 9486 | Diag(Loc, diag::note_equality_comparison_to_assign) |
| 9487 | << FixItHint::CreateReplacement(Loc, "="); |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 9488 | } |
| 9489 | } |
| 9490 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9491 | ExprResult Sema::CheckBooleanCondition(Expr *E, SourceLocation Loc) { |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9492 | DiagnoseAssignmentAsCondition(E); |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 9493 | if (ParenExpr *parenE = dyn_cast<ParenExpr>(E)) |
| 9494 | DiagnoseEqualityWithExtraParens(parenE); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9495 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9496 | ExprResult result = CheckPlaceholderExpr(E); |
| 9497 | if (result.isInvalid()) return ExprError(); |
| 9498 | E = result.take(); |
Argyrios Kyrtzidis | 11ab790 | 2010-11-01 18:49:26 +0000 | [diff] [blame] | 9499 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9500 | if (!E->isTypeDependent()) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 9501 | if (getLangOptions().CPlusPlus) |
| 9502 | return CheckCXXBooleanCondition(E); // C++ 6.4p4 |
| 9503 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9504 | ExprResult ERes = DefaultFunctionArrayLvalueConversion(E); |
| 9505 | if (ERes.isInvalid()) |
| 9506 | return ExprError(); |
| 9507 | E = ERes.take(); |
John McCall | abc56c7 | 2010-12-04 06:09:13 +0000 | [diff] [blame] | 9508 | |
| 9509 | QualType T = E->getType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9510 | if (!T->isScalarType()) { // C99 6.8.4.1p1 |
| 9511 | Diag(Loc, diag::err_typecheck_statement_requires_scalar) |
| 9512 | << T << E->getSourceRange(); |
| 9513 | return ExprError(); |
| 9514 | } |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9515 | } |
| 9516 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9517 | return Owned(E); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9518 | } |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 9519 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9520 | ExprResult Sema::ActOnBooleanCondition(Scope *S, SourceLocation Loc, |
| 9521 | Expr *Sub) { |
Douglas Gregor | eecf38f | 2010-05-06 21:39:56 +0000 | [diff] [blame] | 9522 | if (!Sub) |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 9523 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9524 | |
| 9525 | return CheckBooleanCondition(Sub, Loc); |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 9526 | } |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 9527 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9528 | namespace { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9529 | /// A visitor for rebuilding a call to an __unknown_any expression |
| 9530 | /// to have an appropriate type. |
| 9531 | struct RebuildUnknownAnyFunction |
| 9532 | : StmtVisitor<RebuildUnknownAnyFunction, ExprResult> { |
| 9533 | |
| 9534 | Sema &S; |
| 9535 | |
| 9536 | RebuildUnknownAnyFunction(Sema &S) : S(S) {} |
| 9537 | |
| 9538 | ExprResult VisitStmt(Stmt *S) { |
| 9539 | llvm_unreachable("unexpected statement!"); |
| 9540 | return ExprError(); |
| 9541 | } |
| 9542 | |
| 9543 | ExprResult VisitExpr(Expr *expr) { |
| 9544 | S.Diag(expr->getExprLoc(), diag::err_unsupported_unknown_any_call) |
| 9545 | << expr->getSourceRange(); |
| 9546 | return ExprError(); |
| 9547 | } |
| 9548 | |
| 9549 | /// Rebuild an expression which simply semantically wraps another |
| 9550 | /// expression which it shares the type and value kind of. |
| 9551 | template <class T> ExprResult rebuildSugarExpr(T *expr) { |
| 9552 | ExprResult subResult = Visit(expr->getSubExpr()); |
| 9553 | if (subResult.isInvalid()) return ExprError(); |
| 9554 | |
| 9555 | Expr *subExpr = subResult.take(); |
| 9556 | expr->setSubExpr(subExpr); |
| 9557 | expr->setType(subExpr->getType()); |
| 9558 | expr->setValueKind(subExpr->getValueKind()); |
| 9559 | assert(expr->getObjectKind() == OK_Ordinary); |
| 9560 | return expr; |
| 9561 | } |
| 9562 | |
| 9563 | ExprResult VisitParenExpr(ParenExpr *paren) { |
| 9564 | return rebuildSugarExpr(paren); |
| 9565 | } |
| 9566 | |
| 9567 | ExprResult VisitUnaryExtension(UnaryOperator *op) { |
| 9568 | return rebuildSugarExpr(op); |
| 9569 | } |
| 9570 | |
| 9571 | ExprResult VisitUnaryAddrOf(UnaryOperator *op) { |
| 9572 | ExprResult subResult = Visit(op->getSubExpr()); |
| 9573 | if (subResult.isInvalid()) return ExprError(); |
| 9574 | |
| 9575 | Expr *subExpr = subResult.take(); |
| 9576 | op->setSubExpr(subExpr); |
| 9577 | op->setType(S.Context.getPointerType(subExpr->getType())); |
| 9578 | assert(op->getValueKind() == VK_RValue); |
| 9579 | assert(op->getObjectKind() == OK_Ordinary); |
| 9580 | return op; |
| 9581 | } |
| 9582 | |
| 9583 | ExprResult resolveDecl(Expr *expr, ValueDecl *decl) { |
| 9584 | if (!isa<FunctionDecl>(decl)) return VisitExpr(expr); |
| 9585 | |
| 9586 | expr->setType(decl->getType()); |
| 9587 | |
| 9588 | assert(expr->getValueKind() == VK_RValue); |
| 9589 | if (S.getLangOptions().CPlusPlus && |
| 9590 | !(isa<CXXMethodDecl>(decl) && |
| 9591 | cast<CXXMethodDecl>(decl)->isInstance())) |
| 9592 | expr->setValueKind(VK_LValue); |
| 9593 | |
| 9594 | return expr; |
| 9595 | } |
| 9596 | |
| 9597 | ExprResult VisitMemberExpr(MemberExpr *mem) { |
| 9598 | return resolveDecl(mem, mem->getMemberDecl()); |
| 9599 | } |
| 9600 | |
| 9601 | ExprResult VisitDeclRefExpr(DeclRefExpr *ref) { |
| 9602 | return resolveDecl(ref, ref->getDecl()); |
| 9603 | } |
| 9604 | }; |
| 9605 | } |
| 9606 | |
| 9607 | /// Given a function expression of unknown-any type, try to rebuild it |
| 9608 | /// to have a function type. |
| 9609 | static ExprResult rebuildUnknownAnyFunction(Sema &S, Expr *fn) { |
| 9610 | ExprResult result = RebuildUnknownAnyFunction(S).Visit(fn); |
| 9611 | if (result.isInvalid()) return ExprError(); |
| 9612 | return S.DefaultFunctionArrayConversion(result.take()); |
| 9613 | } |
| 9614 | |
| 9615 | namespace { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9616 | /// A visitor for rebuilding an expression of type __unknown_anytype |
| 9617 | /// into one which resolves the type directly on the referring |
| 9618 | /// expression. Strict preservation of the original source |
| 9619 | /// structure is not a goal. |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9620 | struct RebuildUnknownAnyExpr |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9621 | : StmtVisitor<RebuildUnknownAnyExpr, ExprResult> { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9622 | |
| 9623 | Sema &S; |
| 9624 | |
| 9625 | /// The current destination type. |
| 9626 | QualType DestType; |
| 9627 | |
| 9628 | RebuildUnknownAnyExpr(Sema &S, QualType castType) |
| 9629 | : S(S), DestType(castType) {} |
| 9630 | |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9631 | ExprResult VisitStmt(Stmt *S) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9632 | llvm_unreachable("unexpected statement!"); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9633 | return ExprError(); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9634 | } |
| 9635 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9636 | ExprResult VisitExpr(Expr *expr) { |
| 9637 | S.Diag(expr->getExprLoc(), diag::err_unsupported_unknown_any_expr) |
| 9638 | << expr->getSourceRange(); |
| 9639 | return ExprError(); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9640 | } |
| 9641 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9642 | ExprResult VisitCallExpr(CallExpr *call); |
| 9643 | ExprResult VisitObjCMessageExpr(ObjCMessageExpr *message); |
| 9644 | |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9645 | /// Rebuild an expression which simply semantically wraps another |
| 9646 | /// expression which it shares the type and value kind of. |
| 9647 | template <class T> ExprResult rebuildSugarExpr(T *expr) { |
| 9648 | ExprResult subResult = Visit(expr->getSubExpr()); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9649 | if (subResult.isInvalid()) return ExprError(); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9650 | Expr *subExpr = subResult.take(); |
| 9651 | expr->setSubExpr(subExpr); |
| 9652 | expr->setType(subExpr->getType()); |
| 9653 | expr->setValueKind(subExpr->getValueKind()); |
| 9654 | assert(expr->getObjectKind() == OK_Ordinary); |
| 9655 | return expr; |
| 9656 | } |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9657 | |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9658 | ExprResult VisitParenExpr(ParenExpr *paren) { |
| 9659 | return rebuildSugarExpr(paren); |
| 9660 | } |
| 9661 | |
| 9662 | ExprResult VisitUnaryExtension(UnaryOperator *op) { |
| 9663 | return rebuildSugarExpr(op); |
| 9664 | } |
| 9665 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9666 | ExprResult VisitUnaryAddrOf(UnaryOperator *op) { |
| 9667 | const PointerType *ptr = DestType->getAs<PointerType>(); |
| 9668 | if (!ptr) { |
| 9669 | S.Diag(op->getOperatorLoc(), diag::err_unknown_any_addrof) |
| 9670 | << op->getSourceRange(); |
| 9671 | return ExprError(); |
| 9672 | } |
| 9673 | assert(op->getValueKind() == VK_RValue); |
| 9674 | assert(op->getObjectKind() == OK_Ordinary); |
| 9675 | op->setType(DestType); |
| 9676 | |
| 9677 | // Build the sub-expression as if it were an object of the pointee type. |
| 9678 | DestType = ptr->getPointeeType(); |
| 9679 | ExprResult subResult = Visit(op->getSubExpr()); |
| 9680 | if (subResult.isInvalid()) return ExprError(); |
| 9681 | op->setSubExpr(subResult.take()); |
| 9682 | return op; |
| 9683 | } |
| 9684 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9685 | ExprResult VisitImplicitCastExpr(ImplicitCastExpr *ice); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9686 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9687 | ExprResult resolveDecl(Expr *expr, ValueDecl *decl); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9688 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9689 | ExprResult VisitMemberExpr(MemberExpr *mem) { |
| 9690 | return resolveDecl(mem, mem->getMemberDecl()); |
| 9691 | } |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9692 | |
| 9693 | ExprResult VisitDeclRefExpr(DeclRefExpr *ref) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9694 | return resolveDecl(ref, ref->getDecl()); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9695 | } |
| 9696 | }; |
| 9697 | } |
| 9698 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9699 | /// Rebuilds a call expression which yielded __unknown_anytype. |
| 9700 | ExprResult RebuildUnknownAnyExpr::VisitCallExpr(CallExpr *call) { |
| 9701 | Expr *callee = call->getCallee(); |
| 9702 | |
| 9703 | enum FnKind { |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 9704 | FK_MemberFunction, |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9705 | FK_FunctionPointer, |
| 9706 | FK_BlockPointer |
| 9707 | }; |
| 9708 | |
| 9709 | FnKind kind; |
| 9710 | QualType type = callee->getType(); |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 9711 | if (type == S.Context.BoundMemberTy) { |
| 9712 | assert(isa<CXXMemberCallExpr>(call) || isa<CXXOperatorCallExpr>(call)); |
| 9713 | kind = FK_MemberFunction; |
| 9714 | type = Expr::findBoundMemberType(callee); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9715 | } else if (const PointerType *ptr = type->getAs<PointerType>()) { |
| 9716 | type = ptr->getPointeeType(); |
| 9717 | kind = FK_FunctionPointer; |
| 9718 | } else { |
| 9719 | type = type->castAs<BlockPointerType>()->getPointeeType(); |
| 9720 | kind = FK_BlockPointer; |
| 9721 | } |
| 9722 | const FunctionType *fnType = type->castAs<FunctionType>(); |
| 9723 | |
| 9724 | // Verify that this is a legal result type of a function. |
| 9725 | if (DestType->isArrayType() || DestType->isFunctionType()) { |
| 9726 | unsigned diagID = diag::err_func_returning_array_function; |
| 9727 | if (kind == FK_BlockPointer) |
| 9728 | diagID = diag::err_block_returning_array_function; |
| 9729 | |
| 9730 | S.Diag(call->getExprLoc(), diagID) |
| 9731 | << DestType->isFunctionType() << DestType; |
| 9732 | return ExprError(); |
| 9733 | } |
| 9734 | |
| 9735 | // Otherwise, go ahead and set DestType as the call's result. |
| 9736 | call->setType(DestType.getNonLValueExprType(S.Context)); |
| 9737 | call->setValueKind(Expr::getValueKindForType(DestType)); |
| 9738 | assert(call->getObjectKind() == OK_Ordinary); |
| 9739 | |
| 9740 | // Rebuild the function type, replacing the result type with DestType. |
| 9741 | if (const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(fnType)) |
| 9742 | DestType = S.Context.getFunctionType(DestType, |
| 9743 | proto->arg_type_begin(), |
| 9744 | proto->getNumArgs(), |
| 9745 | proto->getExtProtoInfo()); |
| 9746 | else |
| 9747 | DestType = S.Context.getFunctionNoProtoType(DestType, |
| 9748 | fnType->getExtInfo()); |
| 9749 | |
| 9750 | // Rebuild the appropriate pointer-to-function type. |
| 9751 | switch (kind) { |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 9752 | case FK_MemberFunction: |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9753 | // Nothing to do. |
| 9754 | break; |
| 9755 | |
| 9756 | case FK_FunctionPointer: |
| 9757 | DestType = S.Context.getPointerType(DestType); |
| 9758 | break; |
| 9759 | |
| 9760 | case FK_BlockPointer: |
| 9761 | DestType = S.Context.getBlockPointerType(DestType); |
| 9762 | break; |
| 9763 | } |
| 9764 | |
| 9765 | // Finally, we can recurse. |
| 9766 | ExprResult calleeResult = Visit(callee); |
| 9767 | if (!calleeResult.isUsable()) return ExprError(); |
| 9768 | call->setCallee(calleeResult.take()); |
| 9769 | |
| 9770 | // Bind a temporary if necessary. |
| 9771 | return S.MaybeBindToTemporary(call); |
| 9772 | } |
| 9773 | |
| 9774 | ExprResult RebuildUnknownAnyExpr::VisitObjCMessageExpr(ObjCMessageExpr *msg) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9775 | // Verify that this is a legal result type of a call. |
| 9776 | if (DestType->isArrayType() || DestType->isFunctionType()) { |
| 9777 | S.Diag(msg->getExprLoc(), diag::err_func_returning_array_function) |
| 9778 | << DestType->isFunctionType() << DestType; |
| 9779 | return ExprError(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9780 | } |
| 9781 | |
John McCall | 48218c6 | 2011-07-13 17:56:40 +0000 | [diff] [blame] | 9782 | // Rewrite the method result type if available. |
| 9783 | if (ObjCMethodDecl *method = msg->getMethodDecl()) { |
| 9784 | assert(method->getResultType() == S.Context.UnknownAnyTy); |
| 9785 | method->setResultType(DestType); |
| 9786 | } |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9787 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9788 | // Change the type of the message. |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9789 | msg->setType(DestType.getNonReferenceType()); |
| 9790 | msg->setValueKind(Expr::getValueKindForType(DestType)); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9791 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9792 | return S.MaybeBindToTemporary(msg); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9793 | } |
| 9794 | |
| 9795 | ExprResult RebuildUnknownAnyExpr::VisitImplicitCastExpr(ImplicitCastExpr *ice) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9796 | // The only case we should ever see here is a function-to-pointer decay. |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9797 | assert(ice->getCastKind() == CK_FunctionToPointerDecay); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9798 | assert(ice->getValueKind() == VK_RValue); |
| 9799 | assert(ice->getObjectKind() == OK_Ordinary); |
| 9800 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9801 | ice->setType(DestType); |
| 9802 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9803 | // Rebuild the sub-expression as the pointee (function) type. |
| 9804 | DestType = DestType->castAs<PointerType>()->getPointeeType(); |
| 9805 | |
| 9806 | ExprResult result = Visit(ice->getSubExpr()); |
| 9807 | if (!result.isUsable()) return ExprError(); |
| 9808 | |
| 9809 | ice->setSubExpr(result.take()); |
| 9810 | return S.Owned(ice); |
| 9811 | } |
| 9812 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9813 | ExprResult RebuildUnknownAnyExpr::resolveDecl(Expr *expr, ValueDecl *decl) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9814 | ExprValueKind valueKind = VK_LValue; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9815 | QualType type = DestType; |
| 9816 | |
| 9817 | // We know how to make this work for certain kinds of decls: |
| 9818 | |
| 9819 | // - functions |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9820 | if (FunctionDecl *fn = dyn_cast<FunctionDecl>(decl)) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9821 | // This is true because FunctionDecls must always have function |
| 9822 | // type, so we can't be resolving the entire thing at once. |
| 9823 | assert(type->isFunctionType()); |
| 9824 | |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 9825 | if (CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(fn)) |
| 9826 | if (method->isInstance()) { |
| 9827 | valueKind = VK_RValue; |
| 9828 | type = S.Context.BoundMemberTy; |
| 9829 | } |
| 9830 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9831 | // Function references aren't l-values in C. |
| 9832 | if (!S.getLangOptions().CPlusPlus) |
| 9833 | valueKind = VK_RValue; |
| 9834 | |
| 9835 | // - variables |
| 9836 | } else if (isa<VarDecl>(decl)) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9837 | if (const ReferenceType *refTy = type->getAs<ReferenceType>()) { |
| 9838 | type = refTy->getPointeeType(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9839 | } else if (type->isFunctionType()) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9840 | S.Diag(expr->getExprLoc(), diag::err_unknown_any_var_function_type) |
| 9841 | << decl << expr->getSourceRange(); |
| 9842 | return ExprError(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9843 | } |
| 9844 | |
| 9845 | // - nothing else |
| 9846 | } else { |
| 9847 | S.Diag(expr->getExprLoc(), diag::err_unsupported_unknown_any_decl) |
| 9848 | << decl << expr->getSourceRange(); |
| 9849 | return ExprError(); |
| 9850 | } |
| 9851 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9852 | decl->setType(DestType); |
| 9853 | expr->setType(type); |
| 9854 | expr->setValueKind(valueKind); |
| 9855 | return S.Owned(expr); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9856 | } |
| 9857 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9858 | /// Check a cast of an unknown-any type. We intentionally only |
| 9859 | /// trigger this for C-style casts. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9860 | ExprResult Sema::checkUnknownAnyCast(SourceRange typeRange, QualType castType, |
| 9861 | Expr *castExpr, CastKind &castKind, |
| 9862 | ExprValueKind &VK, CXXCastPath &path) { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9863 | // Rewrite the casted expression from scratch. |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9864 | ExprResult result = RebuildUnknownAnyExpr(*this, castType).Visit(castExpr); |
| 9865 | if (!result.isUsable()) return ExprError(); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9866 | |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9867 | castExpr = result.take(); |
| 9868 | VK = castExpr->getValueKind(); |
| 9869 | castKind = CK_NoOp; |
| 9870 | |
| 9871 | return castExpr; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9872 | } |
| 9873 | |
| 9874 | static ExprResult diagnoseUnknownAnyExpr(Sema &S, Expr *e) { |
| 9875 | Expr *orig = e; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9876 | unsigned diagID = diag::err_uncasted_use_of_unknown_any; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9877 | while (true) { |
| 9878 | e = e->IgnoreParenImpCasts(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9879 | if (CallExpr *call = dyn_cast<CallExpr>(e)) { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9880 | e = call->getCallee(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9881 | diagID = diag::err_uncasted_call_of_unknown_any; |
| 9882 | } else { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9883 | break; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9884 | } |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9885 | } |
| 9886 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9887 | SourceLocation loc; |
| 9888 | NamedDecl *d; |
| 9889 | if (DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e)) { |
| 9890 | loc = ref->getLocation(); |
| 9891 | d = ref->getDecl(); |
| 9892 | } else if (MemberExpr *mem = dyn_cast<MemberExpr>(e)) { |
| 9893 | loc = mem->getMemberLoc(); |
| 9894 | d = mem->getMemberDecl(); |
| 9895 | } else if (ObjCMessageExpr *msg = dyn_cast<ObjCMessageExpr>(e)) { |
| 9896 | diagID = diag::err_uncasted_call_of_unknown_any; |
| 9897 | loc = msg->getSelectorLoc(); |
| 9898 | d = msg->getMethodDecl(); |
| 9899 | assert(d && "unknown method returning __unknown_any?"); |
| 9900 | } else { |
| 9901 | S.Diag(e->getExprLoc(), diag::err_unsupported_unknown_any_expr) |
| 9902 | << e->getSourceRange(); |
| 9903 | return ExprError(); |
| 9904 | } |
| 9905 | |
| 9906 | S.Diag(loc, diagID) << d << orig->getSourceRange(); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9907 | |
| 9908 | // Never recoverable. |
| 9909 | return ExprError(); |
| 9910 | } |
| 9911 | |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 9912 | /// Check for operands with placeholder types and complain if found. |
| 9913 | /// Returns true if there was an error and no recovery was possible. |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 9914 | ExprResult Sema::CheckPlaceholderExpr(Expr *E) { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9915 | // Placeholder types are always *exactly* the appropriate builtin type. |
| 9916 | QualType type = E->getType(); |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 9917 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9918 | // Overloaded expressions. |
| 9919 | if (type == Context.OverloadTy) |
| 9920 | return ResolveAndFixSingleFunctionTemplateSpecialization(E, false, true, |
Douglas Gregor | db2eae6 | 2011-03-16 19:16:25 +0000 | [diff] [blame] | 9921 | E->getSourceRange(), |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9922 | QualType(), |
| 9923 | diag::err_ovl_unresolvable); |
| 9924 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9925 | // Bound member functions. |
| 9926 | if (type == Context.BoundMemberTy) { |
| 9927 | Diag(E->getLocStart(), diag::err_invalid_use_of_bound_member_func) |
| 9928 | << E->getSourceRange(); |
| 9929 | return ExprError(); |
| 9930 | } |
| 9931 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9932 | // Expressions of unknown type. |
| 9933 | if (type == Context.UnknownAnyTy) |
| 9934 | return diagnoseUnknownAnyExpr(*this, E); |
| 9935 | |
| 9936 | assert(!type->isPlaceholderType()); |
| 9937 | return Owned(E); |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 9938 | } |
Richard Trieu | bb9b80c | 2011-04-21 21:44:26 +0000 | [diff] [blame] | 9939 | |
| 9940 | bool Sema::CheckCaseExpression(Expr *expr) { |
| 9941 | if (expr->isTypeDependent()) |
| 9942 | return true; |
| 9943 | if (expr->isValueDependent() || expr->isIntegerConstantExpr(Context)) |
| 9944 | return expr->getType()->isIntegralOrEnumerationType(); |
| 9945 | return false; |
| 9946 | } |