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 | |
John McCall | 96a914a | 2011-08-27 22:06:17 +0000 | [diff] [blame] | 446 | // C++ performs lvalue-to-rvalue conversion as a default argument |
John McCall | 709bca8 | 2011-08-29 23:55:37 +0000 | [diff] [blame] | 447 | // promotion, even on class types, but note: |
| 448 | // C++11 [conv.lval]p2: |
| 449 | // When an lvalue-to-rvalue conversion occurs in an unevaluated |
| 450 | // operand or a subexpression thereof the value contained in the |
| 451 | // referenced object is not accessed. Otherwise, if the glvalue |
| 452 | // has a class type, the conversion copy-initializes a temporary |
| 453 | // of type T from the glvalue and the result of the conversion |
| 454 | // is a prvalue for the temporary. |
| 455 | // FIXME: add some way to gate this entire thing for correctness in |
| 456 | // potentially potentially evaluated contexts. |
John McCall | 96a914a | 2011-08-27 22:06:17 +0000 | [diff] [blame] | 457 | if (getLangOptions().CPlusPlus && E->isGLValue() && |
| 458 | ExprEvalContexts.back().Context != Unevaluated) { |
John McCall | 5f8d604 | 2011-08-27 01:09:30 +0000 | [diff] [blame] | 459 | ExprResult Temp = PerformCopyInitialization( |
| 460 | InitializedEntity::InitializeTemporary(E->getType()), |
| 461 | E->getExprLoc(), |
| 462 | Owned(E)); |
| 463 | if (Temp.isInvalid()) |
| 464 | return ExprError(); |
| 465 | E = Temp.get(); |
| 466 | } |
| 467 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 468 | return Owned(E); |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 469 | } |
| 470 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 471 | /// DefaultVariadicArgumentPromotion - Like DefaultArgumentPromotion, but |
| 472 | /// 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] | 473 | /// interfaces passed by value. |
| 474 | ExprResult Sema::DefaultVariadicArgumentPromotion(Expr *E, VariadicCallType CT, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 475 | FunctionDecl *FDecl) { |
Douglas Gregor | 8d5e18c | 2011-06-17 00:15:10 +0000 | [diff] [blame] | 476 | ExprResult ExprRes = CheckPlaceholderExpr(E); |
| 477 | if (ExprRes.isInvalid()) |
| 478 | return ExprError(); |
| 479 | |
| 480 | ExprRes = DefaultArgumentPromotion(E); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 481 | if (ExprRes.isInvalid()) |
| 482 | return ExprError(); |
| 483 | E = ExprRes.take(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 484 | |
Douglas Gregor | 930a9ab | 2011-05-21 19:26:31 +0000 | [diff] [blame] | 485 | // 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] | 486 | if (E->getType()->isObjCObjectType() && |
Douglas Gregor | 930a9ab | 2011-05-21 19:26:31 +0000 | [diff] [blame] | 487 | DiagRuntimeBehavior(E->getLocStart(), 0, |
| 488 | PDiag(diag::err_cannot_pass_objc_interface_to_vararg) |
| 489 | << E->getType() << CT)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 490 | return ExprError(); |
John McCall | 5f8d604 | 2011-08-27 01:09:30 +0000 | [diff] [blame] | 491 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 492 | if (!E->getType().isPODType(Context)) { |
Douglas Gregor | 0fd228d | 2011-05-21 16:27:21 +0000 | [diff] [blame] | 493 | // C++0x [expr.call]p7: |
| 494 | // Passing a potentially-evaluated argument of class type (Clause 9) |
| 495 | // having a non-trivial copy constructor, a non-trivial move constructor, |
| 496 | // or a non-trivial destructor, with no corresponding parameter, |
| 497 | // is conditionally-supported with implementation-defined semantics. |
| 498 | bool TrivialEnough = false; |
| 499 | if (getLangOptions().CPlusPlus0x && !E->getType()->isDependentType()) { |
| 500 | if (CXXRecordDecl *Record = E->getType()->getAsCXXRecordDecl()) { |
| 501 | if (Record->hasTrivialCopyConstructor() && |
| 502 | Record->hasTrivialMoveConstructor() && |
| 503 | Record->hasTrivialDestructor()) |
| 504 | TrivialEnough = true; |
| 505 | } |
| 506 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 507 | |
| 508 | if (!TrivialEnough && |
| 509 | getLangOptions().ObjCAutoRefCount && |
| 510 | E->getType()->isObjCLifetimeType()) |
| 511 | TrivialEnough = true; |
Douglas Gregor | 0fd228d | 2011-05-21 16:27:21 +0000 | [diff] [blame] | 512 | |
| 513 | if (TrivialEnough) { |
| 514 | // Nothing to diagnose. This is okay. |
| 515 | } else if (DiagRuntimeBehavior(E->getLocStart(), 0, |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 516 | PDiag(diag::warn_cannot_pass_non_pod_arg_to_vararg) |
Douglas Gregor | 0fd228d | 2011-05-21 16:27:21 +0000 | [diff] [blame] | 517 | << getLangOptions().CPlusPlus0x << E->getType() |
Douglas Gregor | 930a9ab | 2011-05-21 19:26:31 +0000 | [diff] [blame] | 518 | << CT)) { |
| 519 | // Turn this into a trap. |
| 520 | CXXScopeSpec SS; |
| 521 | UnqualifiedId Name; |
| 522 | Name.setIdentifier(PP.getIdentifierInfo("__builtin_trap"), |
| 523 | E->getLocStart()); |
| 524 | ExprResult TrapFn = ActOnIdExpression(TUScope, SS, Name, true, false); |
| 525 | if (TrapFn.isInvalid()) |
| 526 | return ExprError(); |
| 527 | |
| 528 | ExprResult Call = ActOnCallExpr(TUScope, TrapFn.get(), E->getLocStart(), |
| 529 | MultiExprArg(), E->getLocEnd()); |
| 530 | if (Call.isInvalid()) |
| 531 | return ExprError(); |
| 532 | |
| 533 | ExprResult Comma = ActOnBinOp(TUScope, E->getLocStart(), tok::comma, |
| 534 | Call.get(), E); |
| 535 | if (Comma.isInvalid()) |
John McCall | 66c2030 | 2011-08-26 18:41:18 +0000 | [diff] [blame] | 536 | return ExprError(); |
Douglas Gregor | 930a9ab | 2011-05-21 19:26:31 +0000 | [diff] [blame] | 537 | E = Comma.get(); |
| 538 | } |
Douglas Gregor | 0fd228d | 2011-05-21 16:27:21 +0000 | [diff] [blame] | 539 | } |
| 540 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 541 | return Owned(E); |
Anders Carlsson | dce5e2c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 542 | } |
| 543 | |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 544 | /// \brief Converts an integer to complex float type. Helper function of |
| 545 | /// UsualArithmeticConversions() |
| 546 | /// |
| 547 | /// \return false if the integer expression is an integer type and is |
| 548 | /// successfully converted to the complex type. |
| 549 | static bool handleIntegerToComplexFloatConversion(Sema &S, ExprResult &intExpr, |
| 550 | ExprResult &complexExpr, |
| 551 | QualType intTy, |
| 552 | QualType complexTy, |
| 553 | bool skipCast) { |
| 554 | if (intTy->isComplexType() || intTy->isRealFloatingType()) return true; |
| 555 | if (skipCast) return false; |
| 556 | if (intTy->isIntegerType()) { |
| 557 | QualType fpTy = cast<ComplexType>(complexTy)->getElementType(); |
| 558 | intExpr = S.ImpCastExprToType(intExpr.take(), fpTy, CK_IntegralToFloating); |
| 559 | intExpr = S.ImpCastExprToType(intExpr.take(), complexTy, |
| 560 | CK_FloatingRealToComplex); |
| 561 | } else { |
| 562 | assert(intTy->isComplexIntegerType()); |
| 563 | intExpr = S.ImpCastExprToType(intExpr.take(), complexTy, |
| 564 | CK_IntegralComplexToFloatingComplex); |
| 565 | } |
| 566 | return false; |
| 567 | } |
| 568 | |
| 569 | /// \brief Takes two complex float types and converts them to the same type. |
| 570 | /// Helper function of UsualArithmeticConversions() |
| 571 | static QualType |
| 572 | handleComplexFloatToComplexFloatConverstion(Sema &S, ExprResult &lhsExpr, |
| 573 | ExprResult &rhsExpr, QualType lhs, |
| 574 | QualType rhs, bool isCompAssign) { |
| 575 | int order = S.Context.getFloatingTypeOrder(lhs, rhs); |
| 576 | |
| 577 | if (order < 0) { |
| 578 | // _Complex float -> _Complex double |
| 579 | if (!isCompAssign) |
| 580 | lhsExpr = S.ImpCastExprToType(lhsExpr.take(), rhs, |
| 581 | CK_FloatingComplexCast); |
| 582 | return rhs; |
| 583 | } |
| 584 | if (order > 0) |
| 585 | // _Complex float -> _Complex double |
| 586 | rhsExpr = S.ImpCastExprToType(rhsExpr.take(), lhs, |
| 587 | CK_FloatingComplexCast); |
| 588 | return lhs; |
| 589 | } |
| 590 | |
| 591 | /// \brief Converts otherExpr to complex float and promotes complexExpr if |
| 592 | /// necessary. Helper function of UsualArithmeticConversions() |
| 593 | static QualType handleOtherComplexFloatConversion(Sema &S, |
| 594 | ExprResult &complexExpr, |
| 595 | ExprResult &otherExpr, |
| 596 | QualType complexTy, |
| 597 | QualType otherTy, |
| 598 | bool convertComplexExpr, |
| 599 | bool convertOtherExpr) { |
| 600 | int order = S.Context.getFloatingTypeOrder(complexTy, otherTy); |
| 601 | |
| 602 | // If just the complexExpr is complex, the otherExpr needs to be converted, |
| 603 | // and the complexExpr might need to be promoted. |
| 604 | if (order > 0) { // complexExpr is wider |
| 605 | // float -> _Complex double |
| 606 | if (convertOtherExpr) { |
| 607 | QualType fp = cast<ComplexType>(complexTy)->getElementType(); |
| 608 | otherExpr = S.ImpCastExprToType(otherExpr.take(), fp, CK_FloatingCast); |
| 609 | otherExpr = S.ImpCastExprToType(otherExpr.take(), complexTy, |
| 610 | CK_FloatingRealToComplex); |
| 611 | } |
| 612 | return complexTy; |
| 613 | } |
| 614 | |
| 615 | // otherTy is at least as wide. Find its corresponding complex type. |
| 616 | QualType result = (order == 0 ? complexTy : |
| 617 | S.Context.getComplexType(otherTy)); |
| 618 | |
| 619 | // double -> _Complex double |
| 620 | if (convertOtherExpr) |
| 621 | otherExpr = S.ImpCastExprToType(otherExpr.take(), result, |
| 622 | CK_FloatingRealToComplex); |
| 623 | |
| 624 | // _Complex float -> _Complex double |
| 625 | if (convertComplexExpr && order < 0) |
| 626 | complexExpr = S.ImpCastExprToType(complexExpr.take(), result, |
| 627 | CK_FloatingComplexCast); |
| 628 | |
| 629 | return result; |
| 630 | } |
| 631 | |
| 632 | /// \brief Handle arithmetic conversion with complex types. Helper function of |
| 633 | /// UsualArithmeticConversions() |
| 634 | static QualType handleComplexFloatConversion(Sema &S, ExprResult &lhsExpr, |
| 635 | ExprResult &rhsExpr, QualType lhs, |
| 636 | QualType rhs, bool isCompAssign) { |
| 637 | // if we have an integer operand, the result is the complex type. |
| 638 | if (!handleIntegerToComplexFloatConversion(S, rhsExpr, lhsExpr, rhs, lhs, |
| 639 | /*skipCast*/false)) |
| 640 | return lhs; |
| 641 | if (!handleIntegerToComplexFloatConversion(S, lhsExpr, rhsExpr, lhs, rhs, |
| 642 | /*skipCast*/isCompAssign)) |
| 643 | return rhs; |
| 644 | |
| 645 | // This handles complex/complex, complex/float, or float/complex. |
| 646 | // When both operands are complex, the shorter operand is converted to the |
| 647 | // type of the longer, and that is the type of the result. This corresponds |
| 648 | // to what is done when combining two real floating-point operands. |
| 649 | // The fun begins when size promotion occur across type domains. |
| 650 | // From H&S 6.3.4: When one operand is complex and the other is a real |
| 651 | // floating-point type, the less precise type is converted, within it's |
| 652 | // real or complex domain, to the precision of the other type. For example, |
| 653 | // when combining a "long double" with a "double _Complex", the |
| 654 | // "double _Complex" is promoted to "long double _Complex". |
| 655 | |
| 656 | bool LHSComplexFloat = lhs->isComplexType(); |
| 657 | bool RHSComplexFloat = rhs->isComplexType(); |
| 658 | |
| 659 | // If both are complex, just cast to the more precise type. |
| 660 | if (LHSComplexFloat && RHSComplexFloat) |
| 661 | return handleComplexFloatToComplexFloatConverstion(S, lhsExpr, rhsExpr, |
| 662 | lhs, rhs, isCompAssign); |
| 663 | |
| 664 | // If only one operand is complex, promote it if necessary and convert the |
| 665 | // other operand to complex. |
| 666 | if (LHSComplexFloat) |
| 667 | return handleOtherComplexFloatConversion( |
| 668 | S, lhsExpr, rhsExpr, lhs, rhs, /*convertComplexExpr*/!isCompAssign, |
| 669 | /*convertOtherExpr*/ true); |
| 670 | |
| 671 | assert(RHSComplexFloat); |
| 672 | return handleOtherComplexFloatConversion( |
| 673 | S, rhsExpr, lhsExpr, rhs, lhs, /*convertComplexExpr*/true, |
| 674 | /*convertOtherExpr*/ !isCompAssign); |
| 675 | } |
| 676 | |
| 677 | /// \brief Hande arithmetic conversion from integer to float. Helper function |
| 678 | /// of UsualArithmeticConversions() |
| 679 | static QualType handleIntToFloatConversion(Sema &S, ExprResult &floatExpr, |
| 680 | ExprResult &intExpr, |
| 681 | QualType floatTy, QualType intTy, |
| 682 | bool convertFloat, bool convertInt) { |
| 683 | if (intTy->isIntegerType()) { |
| 684 | if (convertInt) |
| 685 | // Convert intExpr to the lhs floating point type. |
| 686 | intExpr = S.ImpCastExprToType(intExpr.take(), floatTy, |
| 687 | CK_IntegralToFloating); |
| 688 | return floatTy; |
| 689 | } |
| 690 | |
| 691 | // Convert both sides to the appropriate complex float. |
| 692 | assert(intTy->isComplexIntegerType()); |
| 693 | QualType result = S.Context.getComplexType(floatTy); |
| 694 | |
| 695 | // _Complex int -> _Complex float |
| 696 | if (convertInt) |
| 697 | intExpr = S.ImpCastExprToType(intExpr.take(), result, |
| 698 | CK_IntegralComplexToFloatingComplex); |
| 699 | |
| 700 | // float -> _Complex float |
| 701 | if (convertFloat) |
| 702 | floatExpr = S.ImpCastExprToType(floatExpr.take(), result, |
| 703 | CK_FloatingRealToComplex); |
| 704 | |
| 705 | return result; |
| 706 | } |
| 707 | |
| 708 | /// \brief Handle arithmethic conversion with floating point types. Helper |
| 709 | /// function of UsualArithmeticConversions() |
| 710 | static QualType handleFloatConversion(Sema &S, ExprResult &lhsExpr, |
| 711 | ExprResult &rhsExpr, QualType lhs, |
| 712 | QualType rhs, bool isCompAssign) { |
| 713 | bool LHSFloat = lhs->isRealFloatingType(); |
| 714 | bool RHSFloat = rhs->isRealFloatingType(); |
| 715 | |
| 716 | // If we have two real floating types, convert the smaller operand |
| 717 | // to the bigger result. |
| 718 | if (LHSFloat && RHSFloat) { |
| 719 | int order = S.Context.getFloatingTypeOrder(lhs, rhs); |
| 720 | if (order > 0) { |
| 721 | rhsExpr = S.ImpCastExprToType(rhsExpr.take(), lhs, CK_FloatingCast); |
| 722 | return lhs; |
| 723 | } |
| 724 | |
| 725 | assert(order < 0 && "illegal float comparison"); |
| 726 | if (!isCompAssign) |
| 727 | lhsExpr = S.ImpCastExprToType(lhsExpr.take(), rhs, CK_FloatingCast); |
| 728 | return rhs; |
| 729 | } |
| 730 | |
| 731 | if (LHSFloat) |
| 732 | return handleIntToFloatConversion(S, lhsExpr, rhsExpr, lhs, rhs, |
| 733 | /*convertFloat=*/!isCompAssign, |
| 734 | /*convertInt=*/ true); |
| 735 | assert(RHSFloat); |
| 736 | return handleIntToFloatConversion(S, rhsExpr, lhsExpr, rhs, lhs, |
| 737 | /*convertInt=*/ true, |
| 738 | /*convertFloat=*/!isCompAssign); |
| 739 | } |
| 740 | |
| 741 | /// \brief Handle conversions with GCC complex int extension. Helper function |
| 742 | /// of UsualArithmeticConverions() |
| 743 | // FIXME: if the operands are (int, _Complex long), we currently |
| 744 | // don't promote the complex. Also, signedness? |
| 745 | static QualType handleComplexIntConvsersion(Sema &S, ExprResult &lhsExpr, |
| 746 | ExprResult &rhsExpr, QualType lhs, |
| 747 | QualType rhs, bool isCompAssign) { |
| 748 | const ComplexType *lhsComplexInt = lhs->getAsComplexIntegerType(); |
| 749 | const ComplexType *rhsComplexInt = rhs->getAsComplexIntegerType(); |
| 750 | |
| 751 | if (lhsComplexInt && rhsComplexInt) { |
| 752 | int order = S.Context.getIntegerTypeOrder(lhsComplexInt->getElementType(), |
| 753 | rhsComplexInt->getElementType()); |
| 754 | assert(order && "inequal types with equal element ordering"); |
| 755 | if (order > 0) { |
| 756 | // _Complex int -> _Complex long |
| 757 | rhsExpr = S.ImpCastExprToType(rhsExpr.take(), lhs, |
| 758 | CK_IntegralComplexCast); |
| 759 | return lhs; |
| 760 | } |
| 761 | |
| 762 | if (!isCompAssign) |
| 763 | lhsExpr = S.ImpCastExprToType(lhsExpr.take(), rhs, |
| 764 | CK_IntegralComplexCast); |
| 765 | return rhs; |
| 766 | } |
| 767 | |
| 768 | if (lhsComplexInt) { |
| 769 | // int -> _Complex int |
| 770 | rhsExpr = S.ImpCastExprToType(rhsExpr.take(), lhs, |
| 771 | CK_IntegralRealToComplex); |
| 772 | return lhs; |
| 773 | } |
| 774 | |
| 775 | assert(rhsComplexInt); |
| 776 | // int -> _Complex int |
| 777 | if (!isCompAssign) |
| 778 | lhsExpr = S.ImpCastExprToType(lhsExpr.take(), rhs, |
| 779 | CK_IntegralRealToComplex); |
| 780 | return rhs; |
| 781 | } |
| 782 | |
| 783 | /// \brief Handle integer arithmetic conversions. Helper function of |
| 784 | /// UsualArithmeticConversions() |
| 785 | static QualType handleIntegerConversion(Sema &S, ExprResult &lhsExpr, |
| 786 | ExprResult &rhsExpr, QualType lhs, |
| 787 | QualType rhs, bool isCompAssign) { |
| 788 | // The rules for this case are in C99 6.3.1.8 |
| 789 | int order = S.Context.getIntegerTypeOrder(lhs, rhs); |
| 790 | bool lhsSigned = lhs->hasSignedIntegerRepresentation(); |
| 791 | bool rhsSigned = rhs->hasSignedIntegerRepresentation(); |
| 792 | if (lhsSigned == rhsSigned) { |
| 793 | // Same signedness; use the higher-ranked type |
| 794 | if (order >= 0) { |
| 795 | rhsExpr = S.ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralCast); |
| 796 | return lhs; |
| 797 | } else if (!isCompAssign) |
| 798 | lhsExpr = S.ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralCast); |
| 799 | return rhs; |
| 800 | } else if (order != (lhsSigned ? 1 : -1)) { |
| 801 | // The unsigned type has greater than or equal rank to the |
| 802 | // signed type, so use the unsigned type |
| 803 | if (rhsSigned) { |
| 804 | rhsExpr = S.ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralCast); |
| 805 | return lhs; |
| 806 | } else if (!isCompAssign) |
| 807 | lhsExpr = S.ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralCast); |
| 808 | return rhs; |
| 809 | } else if (S.Context.getIntWidth(lhs) != S.Context.getIntWidth(rhs)) { |
| 810 | // The two types are different widths; if we are here, that |
| 811 | // means the signed type is larger than the unsigned type, so |
| 812 | // use the signed type. |
| 813 | if (lhsSigned) { |
| 814 | rhsExpr = S.ImpCastExprToType(rhsExpr.take(), lhs, CK_IntegralCast); |
| 815 | return lhs; |
| 816 | } else if (!isCompAssign) |
| 817 | lhsExpr = S.ImpCastExprToType(lhsExpr.take(), rhs, CK_IntegralCast); |
| 818 | return rhs; |
| 819 | } else { |
| 820 | // The signed type is higher-ranked than the unsigned type, |
| 821 | // but isn't actually any bigger (like unsigned int and long |
| 822 | // on most 32-bit systems). Use the unsigned type corresponding |
| 823 | // to the signed type. |
| 824 | QualType result = |
| 825 | S.Context.getCorrespondingUnsignedType(lhsSigned ? lhs : rhs); |
| 826 | rhsExpr = S.ImpCastExprToType(rhsExpr.take(), result, CK_IntegralCast); |
| 827 | if (!isCompAssign) |
| 828 | lhsExpr = S.ImpCastExprToType(lhsExpr.take(), result, CK_IntegralCast); |
| 829 | return result; |
| 830 | } |
| 831 | } |
| 832 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 833 | /// UsualArithmeticConversions - Performs various conversions that are common to |
| 834 | /// 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] | 835 | /// routine returns the first non-arithmetic type found. The client is |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 836 | /// responsible for emitting appropriate error diagnostics. |
| 837 | /// FIXME: verify the conversion rules for "complex int" are consistent with |
| 838 | /// GCC. |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 839 | QualType Sema::UsualArithmeticConversions(ExprResult &lhsExpr, |
| 840 | ExprResult &rhsExpr, |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 841 | bool isCompAssign) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 842 | if (!isCompAssign) { |
| 843 | lhsExpr = UsualUnaryConversions(lhsExpr.take()); |
| 844 | if (lhsExpr.isInvalid()) |
| 845 | return QualType(); |
| 846 | } |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 847 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 848 | rhsExpr = UsualUnaryConversions(rhsExpr.take()); |
| 849 | if (rhsExpr.isInvalid()) |
| 850 | return QualType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 851 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 852 | // For conversion purposes, we ignore any qualifiers. |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 853 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 854 | QualType lhs = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 855 | Context.getCanonicalType(lhsExpr.get()->getType()).getUnqualifiedType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 856 | QualType rhs = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 857 | Context.getCanonicalType(rhsExpr.get()->getType()).getUnqualifiedType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 858 | |
| 859 | // If both types are identical, no conversion is needed. |
| 860 | if (lhs == rhs) |
| 861 | return lhs; |
| 862 | |
| 863 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 864 | // The caller can deal with this (e.g. pointer + int). |
| 865 | if (!lhs->isArithmeticType() || !rhs->isArithmeticType()) |
| 866 | return lhs; |
| 867 | |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 868 | // Apply unary and bitfield promotions to the LHS's type. |
| 869 | QualType lhs_unpromoted = lhs; |
| 870 | if (lhs->isPromotableIntegerType()) |
| 871 | lhs = Context.getPromotedIntegerType(lhs); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 872 | QualType LHSBitfieldPromoteTy = Context.isPromotableBitField(lhsExpr.get()); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 873 | if (!LHSBitfieldPromoteTy.isNull()) |
| 874 | lhs = LHSBitfieldPromoteTy; |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 875 | if (lhs != lhs_unpromoted && !isCompAssign) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 876 | lhsExpr = ImpCastExprToType(lhsExpr.take(), lhs, CK_IntegralCast); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 877 | |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 878 | // If both types are identical, no conversion is needed. |
| 879 | if (lhs == rhs) |
| 880 | return lhs; |
| 881 | |
| 882 | // At this point, we have two different arithmetic types. |
| 883 | |
| 884 | // Handle complex types first (C99 6.3.1.8p1). |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 885 | if (lhs->isComplexType() || rhs->isComplexType()) |
| 886 | return handleComplexFloatConversion(*this, lhsExpr, rhsExpr, lhs, rhs, |
| 887 | isCompAssign); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 888 | |
| 889 | // Now handle "real" floating types (i.e. float, double, long double). |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 890 | if (lhs->isRealFloatingType() || rhs->isRealFloatingType()) |
| 891 | return handleFloatConversion(*this, lhsExpr, rhsExpr, lhs, rhs, |
| 892 | isCompAssign); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 893 | |
| 894 | // Handle GCC complex int extension. |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 895 | if (lhs->isComplexIntegerType() || rhs->isComplexIntegerType()) |
| 896 | return handleComplexIntConvsersion(*this, lhsExpr, rhsExpr, lhs, rhs, |
| 897 | isCompAssign); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 898 | |
| 899 | // Finally, we have two differing integer types. |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 900 | return handleIntegerConversion(*this, lhsExpr, rhsExpr, lhs, rhs, |
| 901 | isCompAssign); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 902 | } |
| 903 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 904 | //===----------------------------------------------------------------------===// |
| 905 | // Semantic Analysis for various Expression Types |
| 906 | //===----------------------------------------------------------------------===// |
| 907 | |
| 908 | |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 909 | ExprResult |
| 910 | Sema::ActOnGenericSelectionExpr(SourceLocation KeyLoc, |
| 911 | SourceLocation DefaultLoc, |
| 912 | SourceLocation RParenLoc, |
| 913 | Expr *ControllingExpr, |
| 914 | MultiTypeArg types, |
| 915 | MultiExprArg exprs) { |
| 916 | unsigned NumAssocs = types.size(); |
| 917 | assert(NumAssocs == exprs.size()); |
| 918 | |
| 919 | ParsedType *ParsedTypes = types.release(); |
| 920 | Expr **Exprs = exprs.release(); |
| 921 | |
| 922 | TypeSourceInfo **Types = new TypeSourceInfo*[NumAssocs]; |
| 923 | for (unsigned i = 0; i < NumAssocs; ++i) { |
| 924 | if (ParsedTypes[i]) |
| 925 | (void) GetTypeFromParser(ParsedTypes[i], &Types[i]); |
| 926 | else |
| 927 | Types[i] = 0; |
| 928 | } |
| 929 | |
| 930 | ExprResult ER = CreateGenericSelectionExpr(KeyLoc, DefaultLoc, RParenLoc, |
| 931 | ControllingExpr, Types, Exprs, |
| 932 | NumAssocs); |
Benjamin Kramer | 5bf47f7 | 2011-04-15 11:21:57 +0000 | [diff] [blame] | 933 | delete [] Types; |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 934 | return ER; |
| 935 | } |
| 936 | |
| 937 | ExprResult |
| 938 | Sema::CreateGenericSelectionExpr(SourceLocation KeyLoc, |
| 939 | SourceLocation DefaultLoc, |
| 940 | SourceLocation RParenLoc, |
| 941 | Expr *ControllingExpr, |
| 942 | TypeSourceInfo **Types, |
| 943 | Expr **Exprs, |
| 944 | unsigned NumAssocs) { |
| 945 | bool TypeErrorFound = false, |
| 946 | IsResultDependent = ControllingExpr->isTypeDependent(), |
| 947 | ContainsUnexpandedParameterPack |
| 948 | = ControllingExpr->containsUnexpandedParameterPack(); |
| 949 | |
| 950 | for (unsigned i = 0; i < NumAssocs; ++i) { |
| 951 | if (Exprs[i]->containsUnexpandedParameterPack()) |
| 952 | ContainsUnexpandedParameterPack = true; |
| 953 | |
| 954 | if (Types[i]) { |
| 955 | if (Types[i]->getType()->containsUnexpandedParameterPack()) |
| 956 | ContainsUnexpandedParameterPack = true; |
| 957 | |
| 958 | if (Types[i]->getType()->isDependentType()) { |
| 959 | IsResultDependent = true; |
| 960 | } else { |
| 961 | // C1X 6.5.1.1p2 "The type name in a generic association shall specify a |
| 962 | // complete object type other than a variably modified type." |
| 963 | unsigned D = 0; |
| 964 | if (Types[i]->getType()->isIncompleteType()) |
| 965 | D = diag::err_assoc_type_incomplete; |
| 966 | else if (!Types[i]->getType()->isObjectType()) |
| 967 | D = diag::err_assoc_type_nonobject; |
| 968 | else if (Types[i]->getType()->isVariablyModifiedType()) |
| 969 | D = diag::err_assoc_type_variably_modified; |
| 970 | |
| 971 | if (D != 0) { |
| 972 | Diag(Types[i]->getTypeLoc().getBeginLoc(), D) |
| 973 | << Types[i]->getTypeLoc().getSourceRange() |
| 974 | << Types[i]->getType(); |
| 975 | TypeErrorFound = true; |
| 976 | } |
| 977 | |
| 978 | // C1X 6.5.1.1p2 "No two generic associations in the same generic |
| 979 | // selection shall specify compatible types." |
| 980 | for (unsigned j = i+1; j < NumAssocs; ++j) |
| 981 | if (Types[j] && !Types[j]->getType()->isDependentType() && |
| 982 | Context.typesAreCompatible(Types[i]->getType(), |
| 983 | Types[j]->getType())) { |
| 984 | Diag(Types[j]->getTypeLoc().getBeginLoc(), |
| 985 | diag::err_assoc_compatible_types) |
| 986 | << Types[j]->getTypeLoc().getSourceRange() |
| 987 | << Types[j]->getType() |
| 988 | << Types[i]->getType(); |
| 989 | Diag(Types[i]->getTypeLoc().getBeginLoc(), |
| 990 | diag::note_compat_assoc) |
| 991 | << Types[i]->getTypeLoc().getSourceRange() |
| 992 | << Types[i]->getType(); |
| 993 | TypeErrorFound = true; |
| 994 | } |
| 995 | } |
| 996 | } |
| 997 | } |
| 998 | if (TypeErrorFound) |
| 999 | return ExprError(); |
| 1000 | |
| 1001 | // If we determined that the generic selection is result-dependent, don't |
| 1002 | // try to compute the result expression. |
| 1003 | if (IsResultDependent) |
| 1004 | return Owned(new (Context) GenericSelectionExpr( |
| 1005 | Context, KeyLoc, ControllingExpr, |
| 1006 | Types, Exprs, NumAssocs, DefaultLoc, |
| 1007 | RParenLoc, ContainsUnexpandedParameterPack)); |
| 1008 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1009 | SmallVector<unsigned, 1> CompatIndices; |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1010 | unsigned DefaultIndex = -1U; |
| 1011 | for (unsigned i = 0; i < NumAssocs; ++i) { |
| 1012 | if (!Types[i]) |
| 1013 | DefaultIndex = i; |
| 1014 | else if (Context.typesAreCompatible(ControllingExpr->getType(), |
| 1015 | Types[i]->getType())) |
| 1016 | CompatIndices.push_back(i); |
| 1017 | } |
| 1018 | |
| 1019 | // C1X 6.5.1.1p2 "The controlling expression of a generic selection shall have |
| 1020 | // type compatible with at most one of the types named in its generic |
| 1021 | // association list." |
| 1022 | if (CompatIndices.size() > 1) { |
| 1023 | // We strip parens here because the controlling expression is typically |
| 1024 | // parenthesized in macro definitions. |
| 1025 | ControllingExpr = ControllingExpr->IgnoreParens(); |
| 1026 | Diag(ControllingExpr->getLocStart(), diag::err_generic_sel_multi_match) |
| 1027 | << ControllingExpr->getSourceRange() << ControllingExpr->getType() |
| 1028 | << (unsigned) CompatIndices.size(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1029 | for (SmallVector<unsigned, 1>::iterator I = CompatIndices.begin(), |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1030 | E = CompatIndices.end(); I != E; ++I) { |
| 1031 | Diag(Types[*I]->getTypeLoc().getBeginLoc(), |
| 1032 | diag::note_compat_assoc) |
| 1033 | << Types[*I]->getTypeLoc().getSourceRange() |
| 1034 | << Types[*I]->getType(); |
| 1035 | } |
| 1036 | return ExprError(); |
| 1037 | } |
| 1038 | |
| 1039 | // C1X 6.5.1.1p2 "If a generic selection has no default generic association, |
| 1040 | // its controlling expression shall have type compatible with exactly one of |
| 1041 | // the types named in its generic association list." |
| 1042 | if (DefaultIndex == -1U && CompatIndices.size() == 0) { |
| 1043 | // We strip parens here because the controlling expression is typically |
| 1044 | // parenthesized in macro definitions. |
| 1045 | ControllingExpr = ControllingExpr->IgnoreParens(); |
| 1046 | Diag(ControllingExpr->getLocStart(), diag::err_generic_sel_no_match) |
| 1047 | << ControllingExpr->getSourceRange() << ControllingExpr->getType(); |
| 1048 | return ExprError(); |
| 1049 | } |
| 1050 | |
| 1051 | // C1X 6.5.1.1p3 "If a generic selection has a generic association with a |
| 1052 | // type name that is compatible with the type of the controlling expression, |
| 1053 | // then the result expression of the generic selection is the expression |
| 1054 | // in that generic association. Otherwise, the result expression of the |
| 1055 | // generic selection is the expression in the default generic association." |
| 1056 | unsigned ResultIndex = |
| 1057 | CompatIndices.size() ? CompatIndices[0] : DefaultIndex; |
| 1058 | |
| 1059 | return Owned(new (Context) GenericSelectionExpr( |
| 1060 | Context, KeyLoc, ControllingExpr, |
| 1061 | Types, Exprs, NumAssocs, DefaultLoc, |
| 1062 | RParenLoc, ContainsUnexpandedParameterPack, |
| 1063 | ResultIndex)); |
| 1064 | } |
| 1065 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 1066 | /// ActOnStringLiteral - The specified tokens were lexed as pasted string |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1067 | /// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string |
| 1068 | /// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from |
| 1069 | /// multiple tokens. However, the common case is that StringToks points to one |
| 1070 | /// string. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1071 | /// |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1072 | ExprResult |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 1073 | Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1074 | assert(NumStringToks && "Must have at least one string!"); |
| 1075 | |
Chris Lattner | bbee00b | 2009-01-16 18:51:42 +0000 | [diff] [blame] | 1076 | StringLiteralParser Literal(StringToks, NumStringToks, PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1077 | if (Literal.hadError) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1078 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1079 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1080 | SmallVector<SourceLocation, 4> StringTokLocs; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1081 | for (unsigned i = 0; i != NumStringToks; ++i) |
| 1082 | StringTokLocs.push_back(StringToks[i].getLocation()); |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 1083 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 1084 | QualType StrTy = Context.CharTy; |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1085 | if (Literal.isWide()) |
Anders Carlsson | 96b4adc | 2011-04-06 18:42:48 +0000 | [diff] [blame] | 1086 | StrTy = Context.getWCharType(); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1087 | else if (Literal.isUTF16()) |
| 1088 | StrTy = Context.Char16Ty; |
| 1089 | else if (Literal.isUTF32()) |
| 1090 | StrTy = Context.Char32Ty; |
Anders Carlsson | 96b4adc | 2011-04-06 18:42:48 +0000 | [diff] [blame] | 1091 | else if (Literal.Pascal) |
| 1092 | StrTy = Context.UnsignedCharTy; |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 1093 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1094 | StringLiteral::StringKind Kind = StringLiteral::Ascii; |
| 1095 | if (Literal.isWide()) |
| 1096 | Kind = StringLiteral::Wide; |
| 1097 | else if (Literal.isUTF8()) |
| 1098 | Kind = StringLiteral::UTF8; |
| 1099 | else if (Literal.isUTF16()) |
| 1100 | Kind = StringLiteral::UTF16; |
| 1101 | else if (Literal.isUTF32()) |
| 1102 | Kind = StringLiteral::UTF32; |
| 1103 | |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 1104 | // 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] | 1105 | if (getLangOptions().CPlusPlus || getLangOptions().ConstStrings) |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 1106 | StrTy.addConst(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1107 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 1108 | // Get an array type for the string, according to C99 6.4.5. This includes |
| 1109 | // the nul terminator character as well as the string length for pascal |
| 1110 | // strings. |
| 1111 | StrTy = Context.getConstantArrayType(StrTy, |
Chris Lattner | dbb1ecc | 2009-02-26 23:01:51 +0000 | [diff] [blame] | 1112 | llvm::APInt(32, Literal.GetNumStringChars()+1), |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 1113 | ArrayType::Normal, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1114 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1115 | // Pass &StringTokLocs[0], StringTokLocs.size() to factory! |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 1116 | return Owned(StringLiteral::Create(Context, Literal.GetString(), |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1117 | Kind, Literal.Pascal, StrTy, |
Sean Hunt | 6cf7502 | 2010-08-30 17:47:05 +0000 | [diff] [blame] | 1118 | &StringTokLocs[0], |
| 1119 | StringTokLocs.size())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1120 | } |
| 1121 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1122 | enum CaptureResult { |
| 1123 | /// No capture is required. |
| 1124 | CR_NoCapture, |
| 1125 | |
| 1126 | /// A capture is required. |
| 1127 | CR_Capture, |
| 1128 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1129 | /// A by-ref capture is required. |
| 1130 | CR_CaptureByRef, |
| 1131 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1132 | /// An error occurred when trying to capture the given variable. |
| 1133 | CR_Error |
| 1134 | }; |
| 1135 | |
| 1136 | /// Diagnose an uncapturable value reference. |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1137 | /// |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1138 | /// \param var - the variable referenced |
| 1139 | /// \param DC - the context which we couldn't capture through |
| 1140 | static CaptureResult |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1141 | diagnoseUncapturableValueReference(Sema &S, SourceLocation loc, |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1142 | VarDecl *var, DeclContext *DC) { |
| 1143 | switch (S.ExprEvalContexts.back().Context) { |
| 1144 | case Sema::Unevaluated: |
| 1145 | // The argument will never be evaluated, so don't complain. |
| 1146 | return CR_NoCapture; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1147 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1148 | case Sema::PotentiallyEvaluated: |
| 1149 | case Sema::PotentiallyEvaluatedIfUsed: |
| 1150 | break; |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1151 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1152 | case Sema::PotentiallyPotentiallyEvaluated: |
| 1153 | // FIXME: delay these! |
| 1154 | break; |
Chris Lattner | 17f3a6d | 2009-04-21 22:26:47 +0000 | [diff] [blame] | 1155 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1156 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1157 | // Don't diagnose about capture if we're not actually in code right |
| 1158 | // now; in general, there are more appropriate places that will |
| 1159 | // diagnose this. |
| 1160 | if (!S.CurContext->isFunctionOrMethod()) return CR_NoCapture; |
| 1161 | |
John McCall | 4f38f41 | 2011-03-22 23:15:50 +0000 | [diff] [blame] | 1162 | // Certain madnesses can happen with parameter declarations, which |
| 1163 | // we want to ignore. |
| 1164 | if (isa<ParmVarDecl>(var)) { |
| 1165 | // - If the parameter still belongs to the translation unit, then |
| 1166 | // we're actually just using one parameter in the declaration of |
| 1167 | // the next. This is useful in e.g. VLAs. |
| 1168 | if (isa<TranslationUnitDecl>(var->getDeclContext())) |
| 1169 | return CR_NoCapture; |
| 1170 | |
| 1171 | // - This particular madness can happen in ill-formed default |
| 1172 | // arguments; claim it's okay and let downstream code handle it. |
| 1173 | if (S.CurContext == var->getDeclContext()->getParent()) |
| 1174 | return CR_NoCapture; |
| 1175 | } |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1176 | |
| 1177 | DeclarationName functionName; |
| 1178 | if (FunctionDecl *fn = dyn_cast<FunctionDecl>(var->getDeclContext())) |
| 1179 | functionName = fn->getDeclName(); |
| 1180 | // FIXME: variable from enclosing block that we couldn't capture from! |
| 1181 | |
| 1182 | S.Diag(loc, diag::err_reference_to_local_var_in_enclosing_function) |
| 1183 | << var->getIdentifier() << functionName; |
| 1184 | S.Diag(var->getLocation(), diag::note_local_variable_declared_here) |
| 1185 | << var->getIdentifier(); |
| 1186 | |
| 1187 | return CR_Error; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1188 | } |
| 1189 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1190 | /// There is a well-formed capture at a particular scope level; |
| 1191 | /// propagate it through all the nested blocks. |
| 1192 | static CaptureResult propagateCapture(Sema &S, unsigned validScopeIndex, |
| 1193 | const BlockDecl::Capture &capture) { |
| 1194 | VarDecl *var = capture.getVariable(); |
| 1195 | |
| 1196 | // Update all the inner blocks with the capture information. |
| 1197 | for (unsigned i = validScopeIndex + 1, e = S.FunctionScopes.size(); |
| 1198 | i != e; ++i) { |
| 1199 | BlockScopeInfo *innerBlock = cast<BlockScopeInfo>(S.FunctionScopes[i]); |
| 1200 | innerBlock->Captures.push_back( |
| 1201 | BlockDecl::Capture(capture.getVariable(), capture.isByRef(), |
| 1202 | /*nested*/ true, capture.getCopyExpr())); |
| 1203 | innerBlock->CaptureMap[var] = innerBlock->Captures.size(); // +1 |
| 1204 | } |
| 1205 | |
| 1206 | return capture.isByRef() ? CR_CaptureByRef : CR_Capture; |
| 1207 | } |
| 1208 | |
| 1209 | /// shouldCaptureValueReference - Determine if a reference to the |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1210 | /// given value in the current context requires a variable capture. |
| 1211 | /// |
| 1212 | /// This also keeps the captures set in the BlockScopeInfo records |
| 1213 | /// up-to-date. |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1214 | static CaptureResult shouldCaptureValueReference(Sema &S, SourceLocation loc, |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1215 | ValueDecl *value) { |
| 1216 | // Only variables ever require capture. |
| 1217 | VarDecl *var = dyn_cast<VarDecl>(value); |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1218 | if (!var) return CR_NoCapture; |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1219 | |
| 1220 | // Fast path: variables from the current context never require capture. |
| 1221 | DeclContext *DC = S.CurContext; |
| 1222 | if (var->getDeclContext() == DC) return CR_NoCapture; |
| 1223 | |
| 1224 | // Only variables with local storage require capture. |
| 1225 | // FIXME: What about 'const' variables in C++? |
| 1226 | if (!var->hasLocalStorage()) return CR_NoCapture; |
| 1227 | |
| 1228 | // Otherwise, we need to capture. |
| 1229 | |
| 1230 | unsigned functionScopesIndex = S.FunctionScopes.size() - 1; |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1231 | do { |
| 1232 | // Only blocks (and eventually C++0x closures) can capture; other |
| 1233 | // scopes don't work. |
| 1234 | if (!isa<BlockDecl>(DC)) |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1235 | return diagnoseUncapturableValueReference(S, loc, var, DC); |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1236 | |
| 1237 | BlockScopeInfo *blockScope = |
| 1238 | cast<BlockScopeInfo>(S.FunctionScopes[functionScopesIndex]); |
| 1239 | assert(blockScope->TheDecl == static_cast<BlockDecl*>(DC)); |
| 1240 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1241 | // Check whether we've already captured it in this block. If so, |
| 1242 | // we're done. |
| 1243 | if (unsigned indexPlus1 = blockScope->CaptureMap[var]) |
| 1244 | return propagateCapture(S, functionScopesIndex, |
| 1245 | blockScope->Captures[indexPlus1 - 1]); |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1246 | |
| 1247 | functionScopesIndex--; |
| 1248 | DC = cast<BlockDecl>(DC)->getDeclContext(); |
| 1249 | } while (var->getDeclContext() != DC); |
| 1250 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1251 | // Okay, we descended all the way to the block that defines the variable. |
| 1252 | // Actually try to capture it. |
| 1253 | QualType type = var->getType(); |
| 1254 | |
| 1255 | // Prohibit variably-modified types. |
| 1256 | if (type->isVariablyModifiedType()) { |
| 1257 | S.Diag(loc, diag::err_ref_vm_type); |
| 1258 | S.Diag(var->getLocation(), diag::note_declared_at); |
| 1259 | return CR_Error; |
| 1260 | } |
| 1261 | |
| 1262 | // Prohibit arrays, even in __block variables, but not references to |
| 1263 | // them. |
| 1264 | if (type->isArrayType()) { |
| 1265 | S.Diag(loc, diag::err_ref_array_type); |
| 1266 | S.Diag(var->getLocation(), diag::note_declared_at); |
| 1267 | return CR_Error; |
| 1268 | } |
| 1269 | |
| 1270 | S.MarkDeclarationReferenced(loc, var); |
| 1271 | |
| 1272 | // The BlocksAttr indicates the variable is bound by-reference. |
| 1273 | bool byRef = var->hasAttr<BlocksAttr>(); |
| 1274 | |
| 1275 | // Build a copy expression. |
| 1276 | Expr *copyExpr = 0; |
John McCall | 642a75f | 2011-04-28 02:15:35 +0000 | [diff] [blame] | 1277 | const RecordType *rtype; |
| 1278 | if (!byRef && S.getLangOptions().CPlusPlus && !type->isDependentType() && |
| 1279 | (rtype = type->getAs<RecordType>())) { |
| 1280 | |
| 1281 | // The capture logic needs the destructor, so make sure we mark it. |
| 1282 | // Usually this is unnecessary because most local variables have |
| 1283 | // their destructors marked at declaration time, but parameters are |
| 1284 | // an exception because it's technically only the call site that |
| 1285 | // actually requires the destructor. |
| 1286 | if (isa<ParmVarDecl>(var)) |
| 1287 | S.FinalizeVarWithDestructor(var, rtype); |
| 1288 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 1289 | // According to the blocks spec, the capture of a variable from |
| 1290 | // the stack requires a const copy constructor. This is not true |
| 1291 | // of the copy/move done to move a __block variable to the heap. |
| 1292 | type.addConst(); |
| 1293 | |
| 1294 | Expr *declRef = new (S.Context) DeclRefExpr(var, type, VK_LValue, loc); |
| 1295 | ExprResult result = |
| 1296 | S.PerformCopyInitialization( |
| 1297 | InitializedEntity::InitializeBlock(var->getLocation(), |
| 1298 | type, false), |
| 1299 | loc, S.Owned(declRef)); |
| 1300 | |
| 1301 | // Build a full-expression copy expression if initialization |
| 1302 | // succeeded and used a non-trivial constructor. Recover from |
| 1303 | // errors by pretending that the copy isn't necessary. |
| 1304 | if (!result.isInvalid() && |
| 1305 | !cast<CXXConstructExpr>(result.get())->getConstructor()->isTrivial()) { |
| 1306 | result = S.MaybeCreateExprWithCleanups(result); |
| 1307 | copyExpr = result.take(); |
| 1308 | } |
| 1309 | } |
| 1310 | |
| 1311 | // We're currently at the declarer; go back to the closure. |
| 1312 | functionScopesIndex++; |
| 1313 | BlockScopeInfo *blockScope = |
| 1314 | cast<BlockScopeInfo>(S.FunctionScopes[functionScopesIndex]); |
| 1315 | |
| 1316 | // Build a valid capture in this scope. |
| 1317 | blockScope->Captures.push_back( |
| 1318 | BlockDecl::Capture(var, byRef, /*nested*/ false, copyExpr)); |
| 1319 | blockScope->CaptureMap[var] = blockScope->Captures.size(); // +1 |
| 1320 | |
| 1321 | // Propagate that to inner captures if necessary. |
| 1322 | return propagateCapture(S, functionScopesIndex, |
| 1323 | blockScope->Captures.back()); |
| 1324 | } |
| 1325 | |
| 1326 | static ExprResult BuildBlockDeclRefExpr(Sema &S, ValueDecl *vd, |
| 1327 | const DeclarationNameInfo &NameInfo, |
| 1328 | bool byRef) { |
| 1329 | assert(isa<VarDecl>(vd) && "capturing non-variable"); |
| 1330 | |
| 1331 | VarDecl *var = cast<VarDecl>(vd); |
| 1332 | assert(var->hasLocalStorage() && "capturing non-local"); |
| 1333 | assert(byRef == var->hasAttr<BlocksAttr>() && "byref set wrong"); |
| 1334 | |
| 1335 | QualType exprType = var->getType().getNonReferenceType(); |
| 1336 | |
| 1337 | BlockDeclRefExpr *BDRE; |
| 1338 | if (!byRef) { |
| 1339 | // The variable will be bound by copy; make it const within the |
| 1340 | // closure, but record that this was done in the expression. |
| 1341 | bool constAdded = !exprType.isConstQualified(); |
| 1342 | exprType.addConst(); |
| 1343 | |
| 1344 | BDRE = new (S.Context) BlockDeclRefExpr(var, exprType, VK_LValue, |
| 1345 | NameInfo.getLoc(), false, |
| 1346 | constAdded); |
| 1347 | } else { |
| 1348 | BDRE = new (S.Context) BlockDeclRefExpr(var, exprType, VK_LValue, |
| 1349 | NameInfo.getLoc(), true); |
| 1350 | } |
| 1351 | |
| 1352 | return S.Owned(BDRE); |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 1353 | } |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 1354 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1355 | ExprResult |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1356 | Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1357 | SourceLocation Loc, |
| 1358 | const CXXScopeSpec *SS) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1359 | DeclarationNameInfo NameInfo(D->getDeclName(), Loc); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1360 | return BuildDeclRefExpr(D, Ty, VK, NameInfo, SS); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1363 | /// BuildDeclRefExpr - Build an expression that references a |
| 1364 | /// declaration that does not require a closure capture. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1365 | ExprResult |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1366 | Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1367 | const DeclarationNameInfo &NameInfo, |
| 1368 | const CXXScopeSpec *SS) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1369 | MarkDeclarationReferenced(NameInfo.getLoc(), D); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1370 | |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1371 | Expr *E = DeclRefExpr::Create(Context, |
Douglas Gregor | 40d96a6 | 2011-02-28 21:54:11 +0000 | [diff] [blame] | 1372 | SS? SS->getWithLocInContext(Context) |
| 1373 | : NestedNameSpecifierLoc(), |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1374 | D, NameInfo, Ty, VK); |
| 1375 | |
| 1376 | // Just in case we're building an illegal pointer-to-member. |
| 1377 | if (isa<FieldDecl>(D) && cast<FieldDecl>(D)->getBitWidth()) |
| 1378 | E->setObjectKind(OK_BitField); |
| 1379 | |
| 1380 | return Owned(E); |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1383 | /// Decomposes the given name into a DeclarationNameInfo, its location, and |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1384 | /// possibly a list of template arguments. |
| 1385 | /// |
| 1386 | /// If this produces template arguments, it is permitted to call |
| 1387 | /// DecomposeTemplateName. |
| 1388 | /// |
| 1389 | /// This actually loses a lot of source location information for |
| 1390 | /// non-standard name kinds; we should consider preserving that in |
| 1391 | /// some way. |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 1392 | void |
| 1393 | Sema::DecomposeUnqualifiedId(const UnqualifiedId &Id, |
| 1394 | TemplateArgumentListInfo &Buffer, |
| 1395 | DeclarationNameInfo &NameInfo, |
| 1396 | const TemplateArgumentListInfo *&TemplateArgs) { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1397 | if (Id.getKind() == UnqualifiedId::IK_TemplateId) { |
| 1398 | Buffer.setLAngleLoc(Id.TemplateId->LAngleLoc); |
| 1399 | Buffer.setRAngleLoc(Id.TemplateId->RAngleLoc); |
| 1400 | |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1401 | ASTTemplateArgsPtr TemplateArgsPtr(*this, |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1402 | Id.TemplateId->getTemplateArgs(), |
| 1403 | Id.TemplateId->NumArgs); |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1404 | translateTemplateArguments(TemplateArgsPtr, Buffer); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1405 | TemplateArgsPtr.release(); |
| 1406 | |
John McCall | 2b5289b | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 1407 | TemplateName TName = Id.TemplateId->Template.get(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1408 | SourceLocation TNameLoc = Id.TemplateId->TemplateNameLoc; |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1409 | NameInfo = Context.getNameForTemplate(TName, TNameLoc); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1410 | TemplateArgs = &Buffer; |
| 1411 | } else { |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1412 | NameInfo = GetNameFromUnqualifiedId(Id); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1413 | TemplateArgs = 0; |
| 1414 | } |
| 1415 | } |
| 1416 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1417 | /// Diagnose an empty lookup. |
| 1418 | /// |
| 1419 | /// \return false if new lookup candidates were found |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1420 | bool Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec &SS, LookupResult &R, |
Kaelyn Uhrain | ace5e76 | 2011-08-05 00:09:52 +0000 | [diff] [blame] | 1421 | CorrectTypoContext CTC, |
| 1422 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
| 1423 | Expr **Args, unsigned NumArgs) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1424 | DeclarationName Name = R.getLookupName(); |
| 1425 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1426 | unsigned diagnostic = diag::err_undeclared_var_use; |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1427 | unsigned diagnostic_suggest = diag::err_undeclared_var_use_suggest; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1428 | if (Name.getNameKind() == DeclarationName::CXXOperatorName || |
| 1429 | Name.getNameKind() == DeclarationName::CXXLiteralOperatorName || |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1430 | Name.getNameKind() == DeclarationName::CXXConversionFunctionName) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1431 | diagnostic = diag::err_undeclared_use; |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1432 | diagnostic_suggest = diag::err_undeclared_use_suggest; |
| 1433 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1434 | |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1435 | // If the original lookup was an unqualified lookup, fake an |
| 1436 | // unqualified lookup. This is useful when (for example) the |
| 1437 | // original lookup would not have found something because it was a |
| 1438 | // dependent name. |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1439 | for (DeclContext *DC = SS.isEmpty() ? CurContext : 0; |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1440 | DC; DC = DC->getParent()) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1441 | if (isa<CXXRecordDecl>(DC)) { |
| 1442 | LookupQualifiedName(R, DC); |
| 1443 | |
| 1444 | if (!R.empty()) { |
| 1445 | // Don't give errors about ambiguities in this lookup. |
| 1446 | R.suppressDiagnostics(); |
| 1447 | |
| 1448 | CXXMethodDecl *CurMethod = dyn_cast<CXXMethodDecl>(CurContext); |
| 1449 | bool isInstance = CurMethod && |
| 1450 | CurMethod->isInstance() && |
| 1451 | DC == CurMethod->getParent(); |
| 1452 | |
| 1453 | // Give a code modification hint to insert 'this->'. |
| 1454 | // TODO: fixit for inserting 'Base<T>::' in the other cases. |
| 1455 | // Actually quite difficult! |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1456 | if (isInstance) { |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1457 | UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>( |
| 1458 | CallsUndergoingInstantiation.back()->getCallee()); |
Nick Lewycky | d9ca4ab | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1459 | CXXMethodDecl *DepMethod = cast_or_null<CXXMethodDecl>( |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1460 | CurMethod->getInstantiatedFromMemberFunction()); |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1461 | if (DepMethod) { |
Nick Lewycky | d9ca4ab | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1462 | Diag(R.getNameLoc(), diagnostic) << Name |
| 1463 | << FixItHint::CreateInsertion(R.getNameLoc(), "this->"); |
| 1464 | QualType DepThisType = DepMethod->getThisType(Context); |
| 1465 | CXXThisExpr *DepThis = new (Context) CXXThisExpr( |
| 1466 | R.getNameLoc(), DepThisType, false); |
| 1467 | TemplateArgumentListInfo TList; |
| 1468 | if (ULE->hasExplicitTemplateArgs()) |
| 1469 | ULE->copyTemplateArgumentsInto(TList); |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 1470 | |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 1471 | CXXScopeSpec SS; |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 1472 | SS.Adopt(ULE->getQualifierLoc()); |
Nick Lewycky | d9ca4ab | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1473 | CXXDependentScopeMemberExpr *DepExpr = |
| 1474 | CXXDependentScopeMemberExpr::Create( |
| 1475 | Context, DepThis, DepThisType, true, SourceLocation(), |
Douglas Gregor | 7c3179c | 2011-02-28 18:50:33 +0000 | [diff] [blame] | 1476 | SS.getWithLocInContext(Context), NULL, |
Nick Lewycky | d9ca4ab | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1477 | R.getLookupNameInfo(), &TList); |
| 1478 | CallsUndergoingInstantiation.back()->setCallee(DepExpr); |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1479 | } else { |
Nick Lewycky | d9ca4ab | 2010-08-20 20:54:15 +0000 | [diff] [blame] | 1480 | // FIXME: we should be able to handle this case too. It is correct |
| 1481 | // to add this-> here. This is a workaround for PR7947. |
| 1482 | Diag(R.getNameLoc(), diagnostic) << Name; |
Eli Friedman | a7e6845 | 2010-08-22 01:00:03 +0000 | [diff] [blame] | 1483 | } |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1484 | } else { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1485 | Diag(R.getNameLoc(), diagnostic) << Name; |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1486 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1487 | |
| 1488 | // Do we really want to note all of these? |
| 1489 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 1490 | Diag((*I)->getLocation(), diag::note_dependent_var_use); |
| 1491 | |
| 1492 | // Tell the callee to try to recover. |
| 1493 | return false; |
| 1494 | } |
Douglas Gregor | e26f043 | 2010-08-09 22:38:14 +0000 | [diff] [blame] | 1495 | |
| 1496 | R.clear(); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1497 | } |
| 1498 | } |
| 1499 | |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1500 | // We didn't find anything, so try to correct for a typo. |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1501 | TypoCorrection Corrected; |
| 1502 | if (S && (Corrected = CorrectTypo(R.getLookupNameInfo(), R.getLookupKind(), |
| 1503 | S, &SS, NULL, false, CTC))) { |
| 1504 | std::string CorrectedStr(Corrected.getAsString(getLangOptions())); |
| 1505 | std::string CorrectedQuotedStr(Corrected.getQuoted(getLangOptions())); |
| 1506 | R.setLookupName(Corrected.getCorrection()); |
| 1507 | |
Hans Wennborg | 701d1e7 | 2011-07-12 08:45:31 +0000 | [diff] [blame] | 1508 | if (NamedDecl *ND = Corrected.getCorrectionDecl()) { |
Kaelyn Uhrain | f0c1d8f | 2011-08-03 20:36:05 +0000 | [diff] [blame] | 1509 | if (Corrected.isOverloaded()) { |
| 1510 | OverloadCandidateSet OCS(R.getNameLoc()); |
| 1511 | OverloadCandidateSet::iterator Best; |
| 1512 | for (TypoCorrection::decl_iterator CD = Corrected.begin(), |
| 1513 | CDEnd = Corrected.end(); |
| 1514 | CD != CDEnd; ++CD) { |
Kaelyn Uhrain | adc7a73 | 2011-08-08 17:35:31 +0000 | [diff] [blame] | 1515 | if (FunctionTemplateDecl *FTD = |
Kaelyn Uhrain | ace5e76 | 2011-08-05 00:09:52 +0000 | [diff] [blame] | 1516 | dyn_cast<FunctionTemplateDecl>(*CD)) |
| 1517 | AddTemplateOverloadCandidate( |
| 1518 | FTD, DeclAccessPair::make(FTD, AS_none), ExplicitTemplateArgs, |
| 1519 | Args, NumArgs, OCS); |
Kaelyn Uhrain | adc7a73 | 2011-08-08 17:35:31 +0000 | [diff] [blame] | 1520 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*CD)) |
| 1521 | if (!ExplicitTemplateArgs || ExplicitTemplateArgs->size() == 0) |
| 1522 | AddOverloadCandidate(FD, DeclAccessPair::make(FD, AS_none), |
| 1523 | Args, NumArgs, OCS); |
Kaelyn Uhrain | f0c1d8f | 2011-08-03 20:36:05 +0000 | [diff] [blame] | 1524 | } |
| 1525 | switch (OCS.BestViableFunction(*this, R.getNameLoc(), Best)) { |
| 1526 | case OR_Success: |
| 1527 | ND = Best->Function; |
| 1528 | break; |
| 1529 | default: |
Kaelyn Uhrain | 844d572 | 2011-08-04 23:30:54 +0000 | [diff] [blame] | 1530 | break; |
Kaelyn Uhrain | f0c1d8f | 2011-08-03 20:36:05 +0000 | [diff] [blame] | 1531 | } |
| 1532 | } |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1533 | R.addDecl(ND); |
| 1534 | if (isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND)) { |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1535 | if (SS.isEmpty()) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1536 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << CorrectedQuotedStr |
| 1537 | << FixItHint::CreateReplacement(R.getNameLoc(), CorrectedStr); |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1538 | else |
| 1539 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1540 | << Name << computeDeclContext(SS, false) << CorrectedQuotedStr |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1541 | << SS.getRange() |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1542 | << FixItHint::CreateReplacement(R.getNameLoc(), CorrectedStr); |
| 1543 | if (ND) |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1544 | Diag(ND->getLocation(), diag::note_previous_decl) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1545 | << CorrectedQuotedStr; |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1546 | |
| 1547 | // Tell the callee to try to recover. |
| 1548 | return false; |
| 1549 | } |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1550 | |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1551 | if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND)) { |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1552 | // FIXME: If we ended up with a typo for a type name or |
| 1553 | // Objective-C class name, we're in trouble because the parser |
| 1554 | // is in the wrong place to recover. Suggest the typo |
| 1555 | // correction, but don't make it a fix-it since we're not going |
| 1556 | // to recover well anyway. |
| 1557 | if (SS.isEmpty()) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 1558 | Diag(R.getNameLoc(), diagnostic_suggest) |
| 1559 | << Name << CorrectedQuotedStr; |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1560 | else |
| 1561 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1562 | << Name << computeDeclContext(SS, false) << CorrectedQuotedStr |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1563 | << SS.getRange(); |
| 1564 | |
| 1565 | // Don't try to recover; it won't work. |
| 1566 | return true; |
| 1567 | } |
| 1568 | } else { |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1569 | // 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] | 1570 | // because we aren't able to recover. |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1571 | if (SS.isEmpty()) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1572 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << CorrectedQuotedStr; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1573 | else |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1574 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1575 | << Name << computeDeclContext(SS, false) << CorrectedQuotedStr |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1576 | << SS.getRange(); |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1577 | return true; |
| 1578 | } |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1579 | } |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1580 | R.clear(); |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1581 | |
| 1582 | // Emit a special diagnostic for failed member lookups. |
| 1583 | // FIXME: computing the declaration context might fail here (?) |
| 1584 | if (!SS.isEmpty()) { |
| 1585 | Diag(R.getNameLoc(), diag::err_no_member) |
| 1586 | << Name << computeDeclContext(SS, false) |
| 1587 | << SS.getRange(); |
| 1588 | return true; |
| 1589 | } |
| 1590 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1591 | // Give up, we can't recover. |
| 1592 | Diag(R.getNameLoc(), diagnostic) << Name; |
| 1593 | return true; |
| 1594 | } |
| 1595 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1596 | ExprResult Sema::ActOnIdExpression(Scope *S, |
John McCall | fb97e75 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 1597 | CXXScopeSpec &SS, |
| 1598 | UnqualifiedId &Id, |
| 1599 | bool HasTrailingLParen, |
| 1600 | bool isAddressOfOperand) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1601 | assert(!(isAddressOfOperand && HasTrailingLParen) && |
| 1602 | "cannot be direct & operand and have a trailing lparen"); |
| 1603 | |
| 1604 | if (SS.isInvalid()) |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1605 | return ExprError(); |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1606 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1607 | TemplateArgumentListInfo TemplateArgsBuffer; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1608 | |
| 1609 | // Decompose the UnqualifiedId into the following data. |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1610 | DeclarationNameInfo NameInfo; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1611 | const TemplateArgumentListInfo *TemplateArgs; |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1612 | DecomposeUnqualifiedId(Id, TemplateArgsBuffer, NameInfo, TemplateArgs); |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1613 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1614 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 1615 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1616 | SourceLocation NameLoc = NameInfo.getLoc(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1617 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1618 | // C++ [temp.dep.expr]p3: |
| 1619 | // An id-expression is type-dependent if it contains: |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 1620 | // -- an identifier that was declared with a dependent type, |
| 1621 | // (note: handled after lookup) |
| 1622 | // -- a template-id that is dependent, |
| 1623 | // (note: handled in BuildTemplateIdExpr) |
| 1624 | // -- a conversion-function-id that specifies a dependent type, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1625 | // -- a nested-name-specifier that contains a class-name that |
| 1626 | // names a dependent type. |
| 1627 | // Determine whether this is a member of an unknown specialization; |
| 1628 | // we need to handle these differently. |
Eli Friedman | 647c8b3 | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1629 | bool DependentID = false; |
| 1630 | if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName && |
| 1631 | Name.getCXXNameType()->isDependentType()) { |
| 1632 | DependentID = true; |
| 1633 | } else if (SS.isSet()) { |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1634 | if (DeclContext *DC = computeDeclContext(SS, false)) { |
Eli Friedman | 647c8b3 | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1635 | if (RequireCompleteDeclContext(SS, DC)) |
| 1636 | return ExprError(); |
Eli Friedman | 647c8b3 | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1637 | } else { |
| 1638 | DependentID = true; |
| 1639 | } |
| 1640 | } |
| 1641 | |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1642 | if (DependentID) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1643 | return ActOnDependentIdExpression(SS, NameInfo, isAddressOfOperand, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1644 | TemplateArgs); |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1645 | |
Fariborz Jahanian | 69d5624 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1646 | bool IvarLookupFollowUp = false; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1647 | // Perform the required lookup. |
Fariborz Jahanian | 98a5403 | 2011-07-12 17:16:56 +0000 | [diff] [blame] | 1648 | LookupResult R(*this, NameInfo, |
| 1649 | (Id.getKind() == UnqualifiedId::IK_ImplicitSelfParam) |
| 1650 | ? LookupObjCImplicitSelfParam : LookupOrdinaryName); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1651 | if (TemplateArgs) { |
Douglas Gregor | d2235f6 | 2010-05-20 20:58:56 +0000 | [diff] [blame] | 1652 | // Lookup the template name again to correctly establish the context in |
| 1653 | // which it was found. This is really unfortunate as we already did the |
| 1654 | // lookup to determine that it was a template name in the first place. If |
| 1655 | // this becomes a performance hit, we can work harder to preserve those |
| 1656 | // results until we get here but it's likely not worth it. |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1657 | bool MemberOfUnknownSpecialization; |
| 1658 | LookupTemplateName(R, S, SS, QualType(), /*EnteringContext=*/false, |
| 1659 | MemberOfUnknownSpecialization); |
Douglas Gregor | 2f9f89c | 2011-02-04 13:35:07 +0000 | [diff] [blame] | 1660 | |
| 1661 | if (MemberOfUnknownSpecialization || |
| 1662 | (R.getResultKind() == LookupResult::NotFoundInCurrentInstantiation)) |
| 1663 | return ActOnDependentIdExpression(SS, NameInfo, isAddressOfOperand, |
| 1664 | TemplateArgs); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1665 | } else { |
Fariborz Jahanian | 69d5624 | 2010-07-22 23:33:21 +0000 | [diff] [blame] | 1666 | IvarLookupFollowUp = (!SS.isSet() && II && getCurMethodDecl()); |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1667 | LookupParsedName(R, S, &SS, !IvarLookupFollowUp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1668 | |
Douglas Gregor | 2f9f89c | 2011-02-04 13:35:07 +0000 | [diff] [blame] | 1669 | // If the result might be in a dependent base class, this is a dependent |
| 1670 | // id-expression. |
| 1671 | if (R.getResultKind() == LookupResult::NotFoundInCurrentInstantiation) |
| 1672 | return ActOnDependentIdExpression(SS, NameInfo, isAddressOfOperand, |
| 1673 | TemplateArgs); |
| 1674 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1675 | // If this reference is in an Objective-C method, then we need to do |
| 1676 | // some special Objective-C lookup, too. |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1677 | if (IvarLookupFollowUp) { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1678 | ExprResult E(LookupInObjCMethod(R, S, II, true)); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1679 | if (E.isInvalid()) |
| 1680 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1681 | |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1682 | if (Expr *Ex = E.takeAs<Expr>()) |
| 1683 | return Owned(Ex); |
| 1684 | |
Fariborz Jahanian | f759b4d | 2010-08-13 18:09:39 +0000 | [diff] [blame] | 1685 | // for further use, this must be set to false if in class method. |
| 1686 | IvarLookupFollowUp = getCurMethodDecl()->isInstanceMethod(); |
Steve Naroff | e3e9add | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 1687 | } |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 1688 | } |
Douglas Gregor | c71e28c | 2009-02-16 19:28:42 +0000 | [diff] [blame] | 1689 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1690 | if (R.isAmbiguous()) |
| 1691 | return ExprError(); |
| 1692 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1693 | // Determine whether this name might be a candidate for |
| 1694 | // argument-dependent lookup. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1695 | bool ADL = UseArgumentDependentLookup(SS, R, HasTrailingLParen); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1696 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1697 | if (R.empty() && !ADL) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1698 | // Otherwise, this could be an implicitly declared function reference (legal |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1699 | // in C90, extension in C99, forbidden in C++). |
| 1700 | if (HasTrailingLParen && II && !getLangOptions().CPlusPlus) { |
| 1701 | NamedDecl *D = ImplicitlyDefineFunction(NameLoc, *II, S); |
| 1702 | if (D) R.addDecl(D); |
| 1703 | } |
| 1704 | |
| 1705 | // If this name wasn't predeclared and if this is not a function |
| 1706 | // call, diagnose the problem. |
| 1707 | if (R.empty()) { |
Douglas Gregor | 91f7ac7 | 2010-05-18 16:14:23 +0000 | [diff] [blame] | 1708 | if (DiagnoseEmptyLookup(S, SS, R, CTC_Unknown)) |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1709 | return ExprError(); |
| 1710 | |
| 1711 | assert(!R.empty() && |
| 1712 | "DiagnoseEmptyLookup returned false but added no results"); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1713 | |
| 1714 | // If we found an Objective-C instance variable, let |
| 1715 | // LookupInObjCMethod build the appropriate expression to |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1716 | // reference the ivar. |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1717 | if (ObjCIvarDecl *Ivar = R.getAsSingle<ObjCIvarDecl>()) { |
| 1718 | R.clear(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1719 | ExprResult E(LookupInObjCMethod(R, S, Ivar->getIdentifier())); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1720 | assert(E.isInvalid() || E.get()); |
| 1721 | return move(E); |
| 1722 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1723 | } |
| 1724 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1725 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1726 | // This is guaranteed from this point on. |
| 1727 | assert(!R.empty() || ADL); |
| 1728 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1729 | // Check whether this might be a C++ implicit instance member access. |
John McCall | fb97e75 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 1730 | // C++ [class.mfct.non-static]p3: |
| 1731 | // When an id-expression that is not part of a class member access |
| 1732 | // syntax and not used to form a pointer to member is used in the |
| 1733 | // body of a non-static member function of class X, if name lookup |
| 1734 | // resolves the name in the id-expression to a non-static non-type |
| 1735 | // member of some class C, the id-expression is transformed into a |
| 1736 | // class member access expression using (*this) as the |
| 1737 | // postfix-expression to the left of the . operator. |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1738 | // |
| 1739 | // But we don't actually need to do this for '&' operands if R |
| 1740 | // resolved to a function or overloaded function set, because the |
| 1741 | // expression is ill-formed if it actually works out to be a |
| 1742 | // non-static member function: |
| 1743 | // |
| 1744 | // C++ [expr.ref]p4: |
| 1745 | // Otherwise, if E1.E2 refers to a non-static member function. . . |
| 1746 | // [t]he expression can be used only as the left-hand operand of a |
| 1747 | // member function call. |
| 1748 | // |
| 1749 | // There are other safeguards against such uses, but it's important |
| 1750 | // to get this right here so that we don't end up making a |
| 1751 | // spuriously dependent expression if we're inside a dependent |
| 1752 | // instance method. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1753 | if (!R.empty() && (*R.begin())->isCXXClassMember()) { |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1754 | bool MightBeImplicitMember; |
| 1755 | if (!isAddressOfOperand) |
| 1756 | MightBeImplicitMember = true; |
| 1757 | else if (!SS.isEmpty()) |
| 1758 | MightBeImplicitMember = false; |
| 1759 | else if (R.isOverloadedResult()) |
| 1760 | MightBeImplicitMember = false; |
Douglas Gregor | e2248be | 2010-08-30 16:00:47 +0000 | [diff] [blame] | 1761 | else if (R.isUnresolvableResult()) |
| 1762 | MightBeImplicitMember = true; |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1763 | else |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 1764 | MightBeImplicitMember = isa<FieldDecl>(R.getFoundDecl()) || |
| 1765 | isa<IndirectFieldDecl>(R.getFoundDecl()); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1766 | |
| 1767 | if (MightBeImplicitMember) |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1768 | return BuildPossibleImplicitMemberExpr(SS, R, TemplateArgs); |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1769 | } |
| 1770 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1771 | if (TemplateArgs) |
| 1772 | return BuildTemplateIdExpr(SS, R, ADL, *TemplateArgs); |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1773 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1774 | return BuildDeclarationNameExpr(SS, R, ADL); |
| 1775 | } |
| 1776 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1777 | /// BuildQualifiedDeclarationNameExpr - Build a C++ qualified |
| 1778 | /// declaration name, generally during template instantiation. |
| 1779 | /// There's a large number of things which don't need to be done along |
| 1780 | /// this path. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1781 | ExprResult |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1782 | Sema::BuildQualifiedDeclarationNameExpr(CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1783 | const DeclarationNameInfo &NameInfo) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1784 | DeclContext *DC; |
Douglas Gregor | e6ec5c4 | 2010-04-28 07:04:26 +0000 | [diff] [blame] | 1785 | if (!(DC = computeDeclContext(SS, false)) || DC->isDependentContext()) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1786 | return BuildDependentDeclRefExpr(SS, NameInfo, 0); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1787 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 1788 | if (RequireCompleteDeclContext(SS, DC)) |
Douglas Gregor | e6ec5c4 | 2010-04-28 07:04:26 +0000 | [diff] [blame] | 1789 | return ExprError(); |
| 1790 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1791 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1792 | LookupQualifiedName(R, DC); |
| 1793 | |
| 1794 | if (R.isAmbiguous()) |
| 1795 | return ExprError(); |
| 1796 | |
| 1797 | if (R.empty()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1798 | Diag(NameInfo.getLoc(), diag::err_no_member) |
| 1799 | << NameInfo.getName() << DC << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1800 | return ExprError(); |
| 1801 | } |
| 1802 | |
| 1803 | return BuildDeclarationNameExpr(SS, R, /*ADL*/ false); |
| 1804 | } |
| 1805 | |
| 1806 | /// LookupInObjCMethod - The parser has read a name in, and Sema has |
| 1807 | /// detected that we're currently inside an ObjC method. Perform some |
| 1808 | /// additional lookup. |
| 1809 | /// |
| 1810 | /// Ideally, most of this would be done by lookup, but there's |
| 1811 | /// actually quite a lot of extra work involved. |
| 1812 | /// |
| 1813 | /// Returns a null sentinel to indicate trivial success. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1814 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1815 | Sema::LookupInObjCMethod(LookupResult &Lookup, Scope *S, |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 1816 | IdentifierInfo *II, bool AllowBuiltinCreation) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1817 | SourceLocation Loc = Lookup.getNameLoc(); |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1818 | ObjCMethodDecl *CurMethod = getCurMethodDecl(); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1819 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1820 | // There are two cases to handle here. 1) scoped lookup could have failed, |
| 1821 | // in which case we should look for an ivar. 2) scoped lookup could have |
| 1822 | // found a decl, but that decl is outside the current instance method (i.e. |
| 1823 | // a global variable). In these two cases, we do a lookup for an ivar with |
| 1824 | // this name, if the lookup sucedes, we replace it our current decl. |
| 1825 | |
| 1826 | // If we're in a class method, we don't normally want to look for |
| 1827 | // ivars. But if we don't find anything else, and there's an |
| 1828 | // ivar, that's an error. |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1829 | bool IsClassMethod = CurMethod->isClassMethod(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1830 | |
| 1831 | bool LookForIvars; |
| 1832 | if (Lookup.empty()) |
| 1833 | LookForIvars = true; |
| 1834 | else if (IsClassMethod) |
| 1835 | LookForIvars = false; |
| 1836 | else |
| 1837 | LookForIvars = (Lookup.isSingleResult() && |
| 1838 | Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod()); |
Fariborz Jahanian | 412e798 | 2010-02-09 19:31:38 +0000 | [diff] [blame] | 1839 | ObjCInterfaceDecl *IFace = 0; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1840 | if (LookForIvars) { |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1841 | IFace = CurMethod->getClassInterface(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1842 | ObjCInterfaceDecl *ClassDeclared; |
| 1843 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
| 1844 | // Diagnose using an ivar in a class method. |
| 1845 | if (IsClassMethod) |
| 1846 | return ExprError(Diag(Loc, diag::error_ivar_use_in_class_method) |
| 1847 | << IV->getDeclName()); |
| 1848 | |
| 1849 | // If we're referencing an invalid decl, just return this as a silent |
| 1850 | // error node. The error diagnostic was already emitted on the decl. |
| 1851 | if (IV->isInvalidDecl()) |
| 1852 | return ExprError(); |
| 1853 | |
| 1854 | // Check if referencing a field with __attribute__((deprecated)). |
| 1855 | if (DiagnoseUseOfDecl(IV, Loc)) |
| 1856 | return ExprError(); |
| 1857 | |
| 1858 | // Diagnose the use of an ivar outside of the declaring class. |
| 1859 | if (IV->getAccessControl() == ObjCIvarDecl::Private && |
| 1860 | ClassDeclared != IFace) |
| 1861 | Diag(Loc, diag::error_private_ivar_access) << IV->getDeclName(); |
| 1862 | |
| 1863 | // FIXME: This should use a new expr for a direct reference, don't |
| 1864 | // turn this into Self->ivar, just return a BareIVarExpr or something. |
| 1865 | IdentifierInfo &II = Context.Idents.get("self"); |
| 1866 | UnqualifiedId SelfName; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1867 | SelfName.setIdentifier(&II, SourceLocation()); |
Fariborz Jahanian | 98a5403 | 2011-07-12 17:16:56 +0000 | [diff] [blame] | 1868 | SelfName.setKind(UnqualifiedId::IK_ImplicitSelfParam); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1869 | CXXScopeSpec SelfScopeSpec; |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1870 | ExprResult SelfExpr = ActOnIdExpression(S, SelfScopeSpec, |
Douglas Gregor | e45bb6a | 2010-09-22 16:33:13 +0000 | [diff] [blame] | 1871 | SelfName, false, false); |
| 1872 | if (SelfExpr.isInvalid()) |
| 1873 | return ExprError(); |
| 1874 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1875 | SelfExpr = DefaultLvalueConversion(SelfExpr.take()); |
| 1876 | if (SelfExpr.isInvalid()) |
| 1877 | return ExprError(); |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 1878 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1879 | MarkDeclarationReferenced(Loc, IV); |
| 1880 | return Owned(new (Context) |
| 1881 | ObjCIvarRefExpr(IV, IV->getType(), Loc, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1882 | SelfExpr.take(), true, true)); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1883 | } |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1884 | } else if (CurMethod->isInstanceMethod()) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1885 | // We should warn if a local variable hides an ivar. |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1886 | ObjCInterfaceDecl *IFace = CurMethod->getClassInterface(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1887 | ObjCInterfaceDecl *ClassDeclared; |
| 1888 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
| 1889 | if (IV->getAccessControl() != ObjCIvarDecl::Private || |
| 1890 | IFace == ClassDeclared) |
| 1891 | Diag(Loc, diag::warn_ivar_use_hidden) << IV->getDeclName(); |
| 1892 | } |
| 1893 | } |
| 1894 | |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1895 | if (Lookup.empty() && II && AllowBuiltinCreation) { |
| 1896 | // FIXME. Consolidate this with similar code in LookupName. |
| 1897 | if (unsigned BuiltinID = II->getBuiltinID()) { |
| 1898 | if (!(getLangOptions().CPlusPlus && |
| 1899 | Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))) { |
| 1900 | NamedDecl *D = LazilyCreateBuiltin((IdentifierInfo *)II, BuiltinID, |
| 1901 | S, Lookup.isForRedeclaration(), |
| 1902 | Lookup.getNameLoc()); |
| 1903 | if (D) Lookup.addDecl(D); |
| 1904 | } |
| 1905 | } |
| 1906 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1907 | // Sentinel value saying that we didn't do anything special. |
| 1908 | return Owned((Expr*) 0); |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 1909 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1910 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1911 | /// \brief Cast a base object to a member's actual type. |
| 1912 | /// |
| 1913 | /// Logically this happens in three phases: |
| 1914 | /// |
| 1915 | /// * First we cast from the base type to the naming class. |
| 1916 | /// The naming class is the class into which we were looking |
| 1917 | /// when we found the member; it's the qualifier type if a |
| 1918 | /// qualifier was provided, and otherwise it's the base type. |
| 1919 | /// |
| 1920 | /// * Next we cast from the naming class to the declaring class. |
| 1921 | /// If the member we found was brought into a class's scope by |
| 1922 | /// a using declaration, this is that class; otherwise it's |
| 1923 | /// the class declaring the member. |
| 1924 | /// |
| 1925 | /// * Finally we cast from the declaring class to the "true" |
| 1926 | /// declaring class of the member. This conversion does not |
| 1927 | /// obey access control. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1928 | ExprResult |
| 1929 | Sema::PerformObjectMemberConversion(Expr *From, |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1930 | NestedNameSpecifier *Qualifier, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1931 | NamedDecl *FoundDecl, |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1932 | NamedDecl *Member) { |
| 1933 | CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Member->getDeclContext()); |
| 1934 | if (!RD) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1935 | return Owned(From); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1936 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1937 | QualType DestRecordType; |
| 1938 | QualType DestType; |
| 1939 | QualType FromRecordType; |
| 1940 | QualType FromType = From->getType(); |
| 1941 | bool PointerConversions = false; |
| 1942 | if (isa<FieldDecl>(Member)) { |
| 1943 | DestRecordType = Context.getCanonicalType(Context.getTypeDeclType(RD)); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1944 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1945 | if (FromType->getAs<PointerType>()) { |
| 1946 | DestType = Context.getPointerType(DestRecordType); |
| 1947 | FromRecordType = FromType->getPointeeType(); |
| 1948 | PointerConversions = true; |
| 1949 | } else { |
| 1950 | DestType = DestRecordType; |
| 1951 | FromRecordType = FromType; |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 1952 | } |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1953 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member)) { |
| 1954 | if (Method->isStatic()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1955 | return Owned(From); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1956 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1957 | DestType = Method->getThisType(Context); |
| 1958 | DestRecordType = DestType->getPointeeType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1959 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1960 | if (FromType->getAs<PointerType>()) { |
| 1961 | FromRecordType = FromType->getPointeeType(); |
| 1962 | PointerConversions = true; |
| 1963 | } else { |
| 1964 | FromRecordType = FromType; |
| 1965 | DestType = DestRecordType; |
| 1966 | } |
| 1967 | } else { |
| 1968 | // No conversion necessary. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1969 | return Owned(From); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1970 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1971 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1972 | if (DestType->isDependentType() || FromType->isDependentType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1973 | return Owned(From); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1974 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1975 | // If the unqualified types are the same, no conversion is necessary. |
| 1976 | if (Context.hasSameUnqualifiedType(FromRecordType, DestRecordType)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1977 | return Owned(From); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1978 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 1979 | SourceRange FromRange = From->getSourceRange(); |
| 1980 | SourceLocation FromLoc = FromRange.getBegin(); |
| 1981 | |
John McCall | 5baba9d | 2010-08-25 10:28:54 +0000 | [diff] [blame] | 1982 | ExprValueKind VK = CastCategory(From); |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 1983 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1984 | // C++ [class.member.lookup]p8: |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1985 | // [...] Ambiguities can often be resolved by qualifying a name with its |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 1986 | // class name. |
| 1987 | // |
| 1988 | // If the member was a qualified name and the qualified referred to a |
| 1989 | // specific base subobject type, we'll cast to that intermediate type |
| 1990 | // first and then to the object in which the member is declared. That allows |
| 1991 | // one to resolve ambiguities in, e.g., a diamond-shaped hierarchy such as: |
| 1992 | // |
| 1993 | // class Base { public: int x; }; |
| 1994 | // class Derived1 : public Base { }; |
| 1995 | // class Derived2 : public Base { }; |
| 1996 | // class VeryDerived : public Derived1, public Derived2 { void f(); }; |
| 1997 | // |
| 1998 | // void VeryDerived::f() { |
| 1999 | // x = 17; // error: ambiguous base subobjects |
| 2000 | // Derived1::x = 17; // okay, pick the Base subobject of Derived1 |
| 2001 | // } |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2002 | if (Qualifier) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2003 | QualType QType = QualType(Qualifier->getAsType(), 0); |
| 2004 | assert(!QType.isNull() && "lookup done with dependent qualifier?"); |
| 2005 | assert(QType->isRecordType() && "lookup done with non-record type"); |
| 2006 | |
| 2007 | QualType QRecordType = QualType(QType->getAs<RecordType>(), 0); |
| 2008 | |
| 2009 | // In C++98, the qualifier type doesn't actually have to be a base |
| 2010 | // type of the object type, in which case we just ignore it. |
| 2011 | // Otherwise build the appropriate casts. |
| 2012 | if (IsDerivedFrom(FromRecordType, QRecordType)) { |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2013 | CXXCastPath BasePath; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2014 | if (CheckDerivedToBaseConversion(FromRecordType, QRecordType, |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2015 | FromLoc, FromRange, &BasePath)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2016 | return ExprError(); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2017 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2018 | if (PointerConversions) |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2019 | QType = Context.getPointerType(QType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2020 | From = ImpCastExprToType(From, QType, CK_UncheckedDerivedToBase, |
| 2021 | VK, &BasePath).take(); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2022 | |
| 2023 | FromType = QType; |
| 2024 | FromRecordType = QRecordType; |
| 2025 | |
| 2026 | // If the qualifier type was the same as the destination type, |
| 2027 | // we're done. |
| 2028 | if (Context.hasSameUnqualifiedType(FromRecordType, DestRecordType)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2029 | return Owned(From); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2030 | } |
| 2031 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2032 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2033 | bool IgnoreAccess = false; |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2034 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2035 | // If we actually found the member through a using declaration, cast |
| 2036 | // down to the using declaration's type. |
| 2037 | // |
| 2038 | // Pointer equality is fine here because only one declaration of a |
| 2039 | // class ever has member declarations. |
| 2040 | if (FoundDecl->getDeclContext() != Member->getDeclContext()) { |
| 2041 | assert(isa<UsingShadowDecl>(FoundDecl)); |
| 2042 | QualType URecordType = Context.getTypeDeclType( |
| 2043 | cast<CXXRecordDecl>(FoundDecl->getDeclContext())); |
| 2044 | |
| 2045 | // We only need to do this if the naming-class to declaring-class |
| 2046 | // conversion is non-trivial. |
| 2047 | if (!Context.hasSameUnqualifiedType(FromRecordType, URecordType)) { |
| 2048 | assert(IsDerivedFrom(FromRecordType, URecordType)); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2049 | CXXCastPath BasePath; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2050 | if (CheckDerivedToBaseConversion(FromRecordType, URecordType, |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2051 | FromLoc, FromRange, &BasePath)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2052 | return ExprError(); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 2053 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2054 | QualType UType = URecordType; |
| 2055 | if (PointerConversions) |
| 2056 | UType = Context.getPointerType(UType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2057 | From = ImpCastExprToType(From, UType, CK_UncheckedDerivedToBase, |
| 2058 | VK, &BasePath).take(); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2059 | FromType = UType; |
| 2060 | FromRecordType = URecordType; |
| 2061 | } |
| 2062 | |
| 2063 | // We don't do access control for the conversion from the |
| 2064 | // declaring class to the true declaring class. |
| 2065 | IgnoreAccess = true; |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2066 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2067 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2068 | CXXCastPath BasePath; |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2069 | if (CheckDerivedToBaseConversion(FromRecordType, DestRecordType, |
| 2070 | FromLoc, FromRange, &BasePath, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2071 | IgnoreAccess)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2072 | return ExprError(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2073 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2074 | return ImpCastExprToType(From, DestType, CK_UncheckedDerivedToBase, |
| 2075 | VK, &BasePath); |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 2076 | } |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 2077 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2078 | bool Sema::UseArgumentDependentLookup(const CXXScopeSpec &SS, |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2079 | const LookupResult &R, |
| 2080 | bool HasTrailingLParen) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2081 | // Only when used directly as the postfix-expression of a call. |
| 2082 | if (!HasTrailingLParen) |
| 2083 | return false; |
| 2084 | |
| 2085 | // Never if a scope specifier was provided. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2086 | if (SS.isSet()) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2087 | return false; |
| 2088 | |
| 2089 | // Only in C++ or ObjC++. |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2090 | if (!getLangOptions().CPlusPlus) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2091 | return false; |
| 2092 | |
| 2093 | // Turn off ADL when we find certain kinds of declarations during |
| 2094 | // normal lookup: |
| 2095 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 2096 | NamedDecl *D = *I; |
| 2097 | |
| 2098 | // C++0x [basic.lookup.argdep]p3: |
| 2099 | // -- a declaration of a class member |
| 2100 | // Since using decls preserve this property, we check this on the |
| 2101 | // original decl. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2102 | if (D->isCXXClassMember()) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2103 | return false; |
| 2104 | |
| 2105 | // C++0x [basic.lookup.argdep]p3: |
| 2106 | // -- a block-scope function declaration that is not a |
| 2107 | // using-declaration |
| 2108 | // NOTE: we also trigger this for function templates (in fact, we |
| 2109 | // don't check the decl type at all, since all other decl types |
| 2110 | // turn off ADL anyway). |
| 2111 | if (isa<UsingShadowDecl>(D)) |
| 2112 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 2113 | else if (D->getDeclContext()->isFunctionOrMethod()) |
| 2114 | return false; |
| 2115 | |
| 2116 | // C++0x [basic.lookup.argdep]p3: |
| 2117 | // -- a declaration that is neither a function or a function |
| 2118 | // template |
| 2119 | // And also for builtin functions. |
| 2120 | if (isa<FunctionDecl>(D)) { |
| 2121 | FunctionDecl *FDecl = cast<FunctionDecl>(D); |
| 2122 | |
| 2123 | // But also builtin functions. |
| 2124 | if (FDecl->getBuiltinID() && FDecl->isImplicit()) |
| 2125 | return false; |
| 2126 | } else if (!isa<FunctionTemplateDecl>(D)) |
| 2127 | return false; |
| 2128 | } |
| 2129 | |
| 2130 | return true; |
| 2131 | } |
| 2132 | |
| 2133 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2134 | /// Diagnoses obvious problems with the use of the given declaration |
| 2135 | /// as an expression. This is only actually called for lookups that |
| 2136 | /// were not overloaded, and it doesn't promise that the declaration |
| 2137 | /// will in fact be used. |
| 2138 | static bool CheckDeclInExpr(Sema &S, SourceLocation Loc, NamedDecl *D) { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2139 | if (isa<TypedefNameDecl>(D)) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2140 | S.Diag(Loc, diag::err_unexpected_typedef) << D->getDeclName(); |
| 2141 | return true; |
| 2142 | } |
| 2143 | |
| 2144 | if (isa<ObjCInterfaceDecl>(D)) { |
| 2145 | S.Diag(Loc, diag::err_unexpected_interface) << D->getDeclName(); |
| 2146 | return true; |
| 2147 | } |
| 2148 | |
| 2149 | if (isa<NamespaceDecl>(D)) { |
| 2150 | S.Diag(Loc, diag::err_unexpected_namespace) << D->getDeclName(); |
| 2151 | return true; |
| 2152 | } |
| 2153 | |
| 2154 | return false; |
| 2155 | } |
| 2156 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2157 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2158 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2159 | LookupResult &R, |
| 2160 | bool NeedsADL) { |
John McCall | fead20c | 2009-12-08 22:45:53 +0000 | [diff] [blame] | 2161 | // If this is a single, fully-resolved result and we don't need ADL, |
| 2162 | // just build an ordinary singleton decl ref. |
Douglas Gregor | 86b8e09 | 2010-01-29 17:15:43 +0000 | [diff] [blame] | 2163 | if (!NeedsADL && R.isSingleResult() && !R.getAsSingle<FunctionTemplateDecl>()) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2164 | return BuildDeclarationNameExpr(SS, R.getLookupNameInfo(), |
| 2165 | R.getFoundDecl()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2166 | |
| 2167 | // We only need to check the declaration if there's exactly one |
| 2168 | // result, because in the overloaded case the results can only be |
| 2169 | // functions and function templates. |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2170 | if (R.isSingleResult() && |
| 2171 | CheckDeclInExpr(*this, R.getNameLoc(), R.getFoundDecl())) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2172 | return ExprError(); |
| 2173 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2174 | // Otherwise, just build an unresolved lookup expression. Suppress |
| 2175 | // any lookup-related diagnostics; we'll hash these out later, when |
| 2176 | // we've picked a target. |
| 2177 | R.suppressDiagnostics(); |
| 2178 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2179 | UnresolvedLookupExpr *ULE |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2180 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2181 | SS.getWithLocInContext(Context), |
| 2182 | R.getLookupNameInfo(), |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 2183 | NeedsADL, R.isOverloadedResult(), |
| 2184 | R.begin(), R.end()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2185 | |
| 2186 | return Owned(ULE); |
| 2187 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2188 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2189 | /// \brief Complete semantic analysis for a reference to the given declaration. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2190 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2191 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2192 | const DeclarationNameInfo &NameInfo, |
| 2193 | NamedDecl *D) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2194 | assert(D && "Cannot refer to a NULL declaration"); |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 2195 | assert(!isa<FunctionTemplateDecl>(D) && |
| 2196 | "Cannot refer unambiguously to a function template"); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2197 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2198 | SourceLocation Loc = NameInfo.getLoc(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2199 | if (CheckDeclInExpr(*this, Loc, D)) |
| 2200 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2201 | |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2202 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) { |
| 2203 | // Specifically diagnose references to class templates that are missing |
| 2204 | // a template argument list. |
| 2205 | Diag(Loc, diag::err_template_decl_ref) |
| 2206 | << Template << SS.getRange(); |
| 2207 | Diag(Template->getLocation(), diag::note_template_decl_here); |
| 2208 | return ExprError(); |
| 2209 | } |
| 2210 | |
| 2211 | // Make sure that we're referring to a value. |
| 2212 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 2213 | if (!VD) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2214 | Diag(Loc, diag::err_ref_non_value) |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2215 | << D << SS.getRange(); |
John McCall | 87cf670 | 2009-12-18 18:35:10 +0000 | [diff] [blame] | 2216 | Diag(D->getLocation(), diag::note_declared_at); |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2217 | return ExprError(); |
| 2218 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2219 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2220 | // Check whether this declaration can be used. Note that we suppress |
| 2221 | // this check when we're going to perform argument-dependent lookup |
| 2222 | // on this function name, because this might not be the function |
| 2223 | // that overload resolution actually selects. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2224 | if (DiagnoseUseOfDecl(VD, Loc)) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2225 | return ExprError(); |
| 2226 | |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2227 | // Only create DeclRefExpr's for valid Decl's. |
| 2228 | if (VD->isInvalidDecl()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2229 | return ExprError(); |
| 2230 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2231 | // Handle members of anonymous structs and unions. If we got here, |
| 2232 | // and the reference is to a class member indirect field, then this |
| 2233 | // must be the subject of a pointer-to-member expression. |
| 2234 | if (IndirectFieldDecl *indirectField = dyn_cast<IndirectFieldDecl>(VD)) |
| 2235 | if (!indirectField->isCXXClassMember()) |
| 2236 | return BuildAnonymousStructUnionMemberReference(SS, NameInfo.getLoc(), |
| 2237 | indirectField); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2238 | |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 2239 | // If the identifier reference is inside a block, and it refers to a value |
| 2240 | // that is outside the block, create a BlockDeclRefExpr instead of a |
| 2241 | // DeclRefExpr. This ensures the value is treated as a copy-in snapshot when |
| 2242 | // the block is formed. |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2243 | // |
Chris Lattner | 639e2d3 | 2008-10-20 05:16:36 +0000 | [diff] [blame] | 2244 | // We do not do this for things like enum constants, global variables, etc, |
| 2245 | // as they do not get snapshotted. |
| 2246 | // |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2247 | switch (shouldCaptureValueReference(*this, NameInfo.getLoc(), VD)) { |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2248 | case CR_Error: |
| 2249 | return ExprError(); |
Mike Stump | 0d6fd57 | 2010-01-05 02:56:35 +0000 | [diff] [blame] | 2250 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2251 | case CR_Capture: |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2252 | assert(!SS.isSet() && "referenced local variable with scope specifier?"); |
| 2253 | return BuildBlockDeclRefExpr(*this, VD, NameInfo, /*byref*/ false); |
| 2254 | |
| 2255 | case CR_CaptureByRef: |
| 2256 | assert(!SS.isSet() && "referenced local variable with scope specifier?"); |
| 2257 | return BuildBlockDeclRefExpr(*this, VD, NameInfo, /*byref*/ true); |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2258 | |
| 2259 | case CR_NoCapture: { |
| 2260 | // If this reference is not in a block or if the referenced |
| 2261 | // variable is within the block, create a normal DeclRefExpr. |
| 2262 | |
| 2263 | QualType type = VD->getType(); |
Daniel Dunbar | b20de81 | 2011-02-10 18:29:28 +0000 | [diff] [blame] | 2264 | ExprValueKind valueKind = VK_RValue; |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2265 | |
| 2266 | switch (D->getKind()) { |
| 2267 | // Ignore all the non-ValueDecl kinds. |
| 2268 | #define ABSTRACT_DECL(kind) |
| 2269 | #define VALUE(type, base) |
| 2270 | #define DECL(type, base) \ |
| 2271 | case Decl::type: |
| 2272 | #include "clang/AST/DeclNodes.inc" |
| 2273 | llvm_unreachable("invalid value decl kind"); |
| 2274 | return ExprError(); |
| 2275 | |
| 2276 | // These shouldn't make it here. |
| 2277 | case Decl::ObjCAtDefsField: |
| 2278 | case Decl::ObjCIvar: |
| 2279 | llvm_unreachable("forming non-member reference to ivar?"); |
| 2280 | return ExprError(); |
| 2281 | |
| 2282 | // Enum constants are always r-values and never references. |
| 2283 | // Unresolved using declarations are dependent. |
| 2284 | case Decl::EnumConstant: |
| 2285 | case Decl::UnresolvedUsingValue: |
| 2286 | valueKind = VK_RValue; |
| 2287 | break; |
| 2288 | |
| 2289 | // Fields and indirect fields that got here must be for |
| 2290 | // pointer-to-member expressions; we just call them l-values for |
| 2291 | // internal consistency, because this subexpression doesn't really |
| 2292 | // exist in the high-level semantics. |
| 2293 | case Decl::Field: |
| 2294 | case Decl::IndirectField: |
| 2295 | assert(getLangOptions().CPlusPlus && |
| 2296 | "building reference to field in C?"); |
| 2297 | |
| 2298 | // These can't have reference type in well-formed programs, but |
| 2299 | // for internal consistency we do this anyway. |
| 2300 | type = type.getNonReferenceType(); |
| 2301 | valueKind = VK_LValue; |
| 2302 | break; |
| 2303 | |
| 2304 | // Non-type template parameters are either l-values or r-values |
| 2305 | // depending on the type. |
| 2306 | case Decl::NonTypeTemplateParm: { |
| 2307 | if (const ReferenceType *reftype = type->getAs<ReferenceType>()) { |
| 2308 | type = reftype->getPointeeType(); |
| 2309 | valueKind = VK_LValue; // even if the parameter is an r-value reference |
| 2310 | break; |
| 2311 | } |
| 2312 | |
| 2313 | // For non-references, we need to strip qualifiers just in case |
| 2314 | // the template parameter was declared as 'const int' or whatever. |
| 2315 | valueKind = VK_RValue; |
| 2316 | type = type.getUnqualifiedType(); |
| 2317 | break; |
| 2318 | } |
| 2319 | |
| 2320 | case Decl::Var: |
| 2321 | // In C, "extern void blah;" is valid and is an r-value. |
| 2322 | if (!getLangOptions().CPlusPlus && |
| 2323 | !type.hasQualifiers() && |
| 2324 | type->isVoidType()) { |
| 2325 | valueKind = VK_RValue; |
| 2326 | break; |
| 2327 | } |
| 2328 | // fallthrough |
| 2329 | |
| 2330 | case Decl::ImplicitParam: |
| 2331 | case Decl::ParmVar: |
| 2332 | // These are always l-values. |
| 2333 | valueKind = VK_LValue; |
| 2334 | type = type.getNonReferenceType(); |
| 2335 | break; |
| 2336 | |
| 2337 | case Decl::Function: { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2338 | const FunctionType *fty = type->castAs<FunctionType>(); |
| 2339 | |
| 2340 | // If we're referring to a function with an __unknown_anytype |
| 2341 | // result type, make the entire expression __unknown_anytype. |
| 2342 | if (fty->getResultType() == Context.UnknownAnyTy) { |
| 2343 | type = Context.UnknownAnyTy; |
| 2344 | valueKind = VK_RValue; |
| 2345 | break; |
| 2346 | } |
| 2347 | |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2348 | // Functions are l-values in C++. |
| 2349 | if (getLangOptions().CPlusPlus) { |
| 2350 | valueKind = VK_LValue; |
| 2351 | break; |
| 2352 | } |
| 2353 | |
| 2354 | // C99 DR 316 says that, if a function type comes from a |
| 2355 | // function definition (without a prototype), that type is only |
| 2356 | // used for checking compatibility. Therefore, when referencing |
| 2357 | // the function, we pretend that we don't have the full function |
| 2358 | // type. |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2359 | if (!cast<FunctionDecl>(VD)->hasPrototype() && |
| 2360 | isa<FunctionProtoType>(fty)) |
| 2361 | type = Context.getFunctionNoProtoType(fty->getResultType(), |
| 2362 | fty->getExtInfo()); |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2363 | |
| 2364 | // Functions are r-values in C. |
| 2365 | valueKind = VK_RValue; |
| 2366 | break; |
| 2367 | } |
| 2368 | |
| 2369 | case Decl::CXXMethod: |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2370 | // If we're referring to a method with an __unknown_anytype |
| 2371 | // result type, make the entire expression __unknown_anytype. |
| 2372 | // This should only be possible with a type written directly. |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 2373 | if (const FunctionProtoType *proto |
| 2374 | = dyn_cast<FunctionProtoType>(VD->getType())) |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2375 | if (proto->getResultType() == Context.UnknownAnyTy) { |
| 2376 | type = Context.UnknownAnyTy; |
| 2377 | valueKind = VK_RValue; |
| 2378 | break; |
| 2379 | } |
| 2380 | |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2381 | // C++ methods are l-values if static, r-values if non-static. |
| 2382 | if (cast<CXXMethodDecl>(VD)->isStatic()) { |
| 2383 | valueKind = VK_LValue; |
| 2384 | break; |
| 2385 | } |
| 2386 | // fallthrough |
| 2387 | |
| 2388 | case Decl::CXXConversion: |
| 2389 | case Decl::CXXDestructor: |
| 2390 | case Decl::CXXConstructor: |
| 2391 | valueKind = VK_RValue; |
| 2392 | break; |
| 2393 | } |
| 2394 | |
| 2395 | return BuildDeclRefExpr(VD, type, valueKind, NameInfo, &SS); |
| 2396 | } |
| 2397 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 2398 | } |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2399 | |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 2400 | llvm_unreachable("unknown capture result"); |
| 2401 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2402 | } |
| 2403 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2404 | ExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc, tok::TokenKind Kind) { |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 2405 | PredefinedExpr::IdentType IT; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2406 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2407 | switch (Kind) { |
Chris Lattner | 1423ea4 | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 2408 | default: assert(0 && "Unknown simple primary expr!"); |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 2409 | case tok::kw___func__: IT = PredefinedExpr::Func; break; // [C99 6.4.2.2] |
| 2410 | case tok::kw___FUNCTION__: IT = PredefinedExpr::Function; break; |
| 2411 | case tok::kw___PRETTY_FUNCTION__: IT = PredefinedExpr::PrettyFunction; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2412 | } |
Chris Lattner | 1423ea4 | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 2413 | |
Chris Lattner | fa28b30 | 2008-01-12 08:14:25 +0000 | [diff] [blame] | 2414 | // Pre-defined identifiers are of type char[x], where x is the length of the |
| 2415 | // string. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2416 | |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2417 | Decl *currentDecl = getCurFunctionOrMethodDecl(); |
Fariborz Jahanian | eb024ac | 2010-07-23 21:53:24 +0000 | [diff] [blame] | 2418 | if (!currentDecl && getCurBlock()) |
| 2419 | currentDecl = getCurBlock()->TheDecl; |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2420 | if (!currentDecl) { |
Chris Lattner | b0da923 | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 2421 | Diag(Loc, diag::ext_predef_outside_function); |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2422 | currentDecl = Context.getTranslationUnitDecl(); |
Chris Lattner | b0da923 | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 2423 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2424 | |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2425 | QualType ResTy; |
| 2426 | if (cast<DeclContext>(currentDecl)->isDependentContext()) { |
| 2427 | ResTy = Context.DependentTy; |
| 2428 | } else { |
Anders Carlsson | 848fa64 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 2429 | unsigned Length = PredefinedExpr::ComputeName(IT, currentDecl).length(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2430 | |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2431 | llvm::APInt LengthI(32, Length + 1); |
John McCall | 0953e76 | 2009-09-24 19:53:00 +0000 | [diff] [blame] | 2432 | ResTy = Context.CharTy.withConst(); |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2433 | ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0); |
| 2434 | } |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2435 | return Owned(new (Context) PredefinedExpr(Loc, ResTy, IT)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2436 | } |
| 2437 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2438 | ExprResult Sema::ActOnCharacterConstant(const Token &Tok) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2439 | llvm::SmallString<16> CharBuffer; |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2440 | bool Invalid = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2441 | StringRef ThisTok = PP.getSpelling(Tok, CharBuffer, &Invalid); |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2442 | if (Invalid) |
| 2443 | return ExprError(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2444 | |
Benjamin Kramer | ddeea56 | 2010-02-27 13:44:12 +0000 | [diff] [blame] | 2445 | CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(), Tok.getLocation(), |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2446 | PP, Tok.getKind()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2447 | if (Literal.hadError()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2448 | return ExprError(); |
Chris Lattner | fc62bfd | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 2449 | |
Chris Lattner | e8337df | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 2450 | QualType Ty; |
| 2451 | if (!getLangOptions().CPlusPlus) |
| 2452 | Ty = Context.IntTy; // 'x' and L'x' -> int in C. |
| 2453 | else if (Literal.isWide()) |
| 2454 | Ty = Context.WCharTy; // L'x' -> wchar_t in C++. |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2455 | else if (Literal.isUTF16()) |
| 2456 | Ty = Context.Char16Ty; // u'x' -> char16_t in C++0x. |
| 2457 | else if (Literal.isUTF32()) |
| 2458 | Ty = Context.Char32Ty; // U'x' -> char32_t in C++0x. |
Eli Friedman | 136b0cd | 2010-02-03 18:21:45 +0000 | [diff] [blame] | 2459 | else if (Literal.isMultiChar()) |
| 2460 | Ty = Context.IntTy; // 'wxyz' -> int in C++. |
Chris Lattner | e8337df | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 2461 | else |
| 2462 | Ty = Context.CharTy; // 'x' -> char in C++ |
Chris Lattner | fc62bfd | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 2463 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2464 | CharacterLiteral::CharacterKind Kind = CharacterLiteral::Ascii; |
| 2465 | if (Literal.isWide()) |
| 2466 | Kind = CharacterLiteral::Wide; |
| 2467 | else if (Literal.isUTF16()) |
| 2468 | Kind = CharacterLiteral::UTF16; |
| 2469 | else if (Literal.isUTF32()) |
| 2470 | Kind = CharacterLiteral::UTF32; |
| 2471 | |
| 2472 | return Owned(new (Context) CharacterLiteral(Literal.getValue(), Kind, Ty, |
| 2473 | Tok.getLocation())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2474 | } |
| 2475 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2476 | ExprResult Sema::ActOnNumericConstant(const Token &Tok) { |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2477 | // 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] | 2478 | // cannot have a trigraph, escaped newline, radix prefix, or type suffix. |
| 2479 | if (Tok.getLength() == 1) { |
Chris Lattner | 7216dc9 | 2009-01-26 22:36:52 +0000 | [diff] [blame] | 2480 | const char Val = PP.getSpellingOfSingleCharacterNumericConstant(Tok); |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2481 | unsigned IntSize = Context.getTargetInfo().getIntWidth(); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2482 | return Owned(IntegerLiteral::Create(Context, llvm::APInt(IntSize, Val-'0'), |
Steve Naroff | 0a47393 | 2009-01-20 19:53:53 +0000 | [diff] [blame] | 2483 | Context.IntTy, Tok.getLocation())); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2484 | } |
Ted Kremenek | 2839660 | 2009-01-13 23:19:12 +0000 | [diff] [blame] | 2485 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2486 | llvm::SmallString<512> IntegerBuffer; |
Chris Lattner | 2a29904 | 2008-09-30 20:53:45 +0000 | [diff] [blame] | 2487 | // Add padding so that NumericLiteralParser can overread by one character. |
| 2488 | IntegerBuffer.resize(Tok.getLength()+1); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2489 | const char *ThisTokBegin = &IntegerBuffer[0]; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2490 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2491 | // Get the spelling of the token, which eliminates trigraphs, etc. |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2492 | bool Invalid = false; |
| 2493 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid); |
| 2494 | if (Invalid) |
| 2495 | return ExprError(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2496 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2497 | NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2498 | Tok.getLocation(), PP); |
| 2499 | if (Literal.hadError) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2500 | return ExprError(); |
| 2501 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2502 | Expr *Res; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2503 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2504 | if (Literal.isFloatingLiteral()) { |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2505 | QualType Ty; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2506 | if (Literal.isFloat) |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2507 | Ty = Context.FloatTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2508 | else if (!Literal.isLong) |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2509 | Ty = Context.DoubleTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2510 | else |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 2511 | Ty = Context.LongDoubleTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2512 | |
| 2513 | const llvm::fltSemantics &Format = Context.getFloatTypeSemantics(Ty); |
| 2514 | |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2515 | using llvm::APFloat; |
| 2516 | APFloat Val(Format); |
| 2517 | |
| 2518 | APFloat::opStatus result = Literal.GetFloatValue(Val); |
John McCall | 9f2df88 | 2009-12-24 11:09:08 +0000 | [diff] [blame] | 2519 | |
| 2520 | // Overflow is always an error, but underflow is only an error if |
| 2521 | // we underflowed to zero (APFloat reports denormals as underflow). |
| 2522 | if ((result & APFloat::opOverflow) || |
| 2523 | ((result & APFloat::opUnderflow) && Val.isZero())) { |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2524 | unsigned diagnostic; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2525 | llvm::SmallString<20> buffer; |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2526 | if (result & APFloat::opOverflow) { |
John McCall | 2a0d757 | 2010-02-26 23:35:57 +0000 | [diff] [blame] | 2527 | diagnostic = diag::warn_float_overflow; |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2528 | APFloat::getLargest(Format).toString(buffer); |
| 2529 | } else { |
John McCall | 2a0d757 | 2010-02-26 23:35:57 +0000 | [diff] [blame] | 2530 | diagnostic = diag::warn_float_underflow; |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2531 | APFloat::getSmallest(Format).toString(buffer); |
| 2532 | } |
| 2533 | |
| 2534 | Diag(Tok.getLocation(), diagnostic) |
| 2535 | << Ty |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2536 | << StringRef(buffer.data(), buffer.size()); |
John McCall | 94c939d | 2009-12-24 09:08:04 +0000 | [diff] [blame] | 2537 | } |
| 2538 | |
| 2539 | bool isExact = (result == APFloat::opOK); |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2540 | Res = FloatingLiteral::Create(Context, Val, isExact, Ty, Tok.getLocation()); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2541 | |
Peter Collingbourne | f4f7cb8 | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2542 | if (Ty == Context.DoubleTy) { |
| 2543 | if (getLangOptions().SinglePrecisionConstants) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2544 | Res = ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast).take(); |
Peter Collingbourne | f4f7cb8 | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2545 | } else if (getLangOptions().OpenCL && !getOpenCLOptions().cl_khr_fp64) { |
| 2546 | Diag(Tok.getLocation(), diag::warn_double_const_requires_fp64); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2547 | Res = ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast).take(); |
Peter Collingbourne | f4f7cb8 | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2548 | } |
| 2549 | } |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2550 | } else if (!Literal.isIntegerLiteral()) { |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2551 | return ExprError(); |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2552 | } else { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2553 | QualType Ty; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2554 | |
Neil Booth | b944951 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 2555 | // long long is a C99 feature. |
| 2556 | if (!getLangOptions().C99 && !getLangOptions().CPlusPlus0x && |
Neil Booth | 79859c3 | 2007-08-29 22:13:52 +0000 | [diff] [blame] | 2557 | Literal.isLongLong) |
Neil Booth | b944951 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 2558 | Diag(Tok.getLocation(), diag::ext_longlong); |
| 2559 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2560 | // Get the value in the widest-possible width. |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2561 | llvm::APInt ResultVal(Context.getTargetInfo().getIntMaxTWidth(), 0); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2562 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2563 | if (Literal.GetIntegerValue(ResultVal)) { |
| 2564 | // If this value didn't fit into uintmax_t, warn and force to ull. |
| 2565 | Diag(Tok.getLocation(), diag::warn_integer_too_large); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2566 | Ty = Context.UnsignedLongLongTy; |
| 2567 | assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() && |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2568 | "long long is not intmax_t?"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2569 | } else { |
| 2570 | // If this value fits into a ULL, try to figure out what else it fits into |
| 2571 | // according to the rules of C99 6.4.4.1p5. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2572 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2573 | // Octal, Hexadecimal, and integers with a U suffix are allowed to |
| 2574 | // be an unsigned int. |
| 2575 | bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10; |
| 2576 | |
| 2577 | // Check from smallest to largest, picking the smallest type we can. |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2578 | unsigned Width = 0; |
Chris Lattner | 97c5156 | 2007-08-23 21:58:08 +0000 | [diff] [blame] | 2579 | if (!Literal.isLong && !Literal.isLongLong) { |
| 2580 | // Are int/unsigned possibilities? |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2581 | unsigned IntSize = Context.getTargetInfo().getIntWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2582 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2583 | // Does it fit in a unsigned int? |
| 2584 | if (ResultVal.isIntN(IntSize)) { |
| 2585 | // Does it fit in a signed int? |
| 2586 | if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2587 | Ty = Context.IntTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2588 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2589 | Ty = Context.UnsignedIntTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2590 | Width = IntSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2591 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2592 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2593 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2594 | // Are long/unsigned long possibilities? |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2595 | if (Ty.isNull() && !Literal.isLongLong) { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2596 | unsigned LongSize = Context.getTargetInfo().getLongWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2597 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2598 | // Does it fit in a unsigned long? |
| 2599 | if (ResultVal.isIntN(LongSize)) { |
| 2600 | // Does it fit in a signed long? |
| 2601 | if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2602 | Ty = Context.LongTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2603 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2604 | Ty = Context.UnsignedLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2605 | Width = LongSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2606 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2607 | } |
| 2608 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2609 | // Finally, check long long if needed. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2610 | if (Ty.isNull()) { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2611 | unsigned LongLongSize = Context.getTargetInfo().getLongLongWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2612 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2613 | // Does it fit in a unsigned long long? |
| 2614 | if (ResultVal.isIntN(LongLongSize)) { |
| 2615 | // Does it fit in a signed long long? |
Francois Pichet | 2432320 | 2011-01-11 23:38:13 +0000 | [diff] [blame] | 2616 | // To be compatible with MSVC, hex integer literals ending with the |
| 2617 | // LL or i64 suffix are always signed in Microsoft mode. |
Francois Pichet | a15a5ee | 2011-01-11 12:23:00 +0000 | [diff] [blame] | 2618 | if (!Literal.isUnsigned && (ResultVal[LongLongSize-1] == 0 || |
| 2619 | (getLangOptions().Microsoft && Literal.isLongLong))) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2620 | Ty = Context.LongLongTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2621 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2622 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2623 | Width = LongLongSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2624 | } |
| 2625 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2626 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2627 | // If we still couldn't decide a type, we probably have something that |
| 2628 | // 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] | 2629 | if (Ty.isNull()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2630 | Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2631 | Ty = Context.UnsignedLongLongTy; |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2632 | Width = Context.getTargetInfo().getLongLongWidth(); |
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 | |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2635 | if (ResultVal.getBitWidth() != Width) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2636 | ResultVal = ResultVal.trunc(Width); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2637 | } |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2638 | Res = IntegerLiteral::Create(Context, ResultVal, Ty, Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2639 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2640 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2641 | // If this is an imaginary literal, create the ImaginaryLiteral wrapper. |
| 2642 | if (Literal.isImaginary) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2643 | Res = new (Context) ImaginaryLiteral(Res, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2644 | Context.getComplexType(Res->getType())); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2645 | |
| 2646 | return Owned(Res); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2647 | } |
| 2648 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2649 | ExprResult Sema::ActOnParenExpr(SourceLocation L, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2650 | SourceLocation R, Expr *E) { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2651 | assert((E != 0) && "ActOnParenExpr() missing expr"); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2652 | return Owned(new (Context) ParenExpr(L, R, E)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2653 | } |
| 2654 | |
Chandler Carruth | df1f377 | 2011-05-26 08:53:12 +0000 | [diff] [blame] | 2655 | static bool CheckVecStepTraitOperandType(Sema &S, QualType T, |
| 2656 | SourceLocation Loc, |
| 2657 | SourceRange ArgRange) { |
| 2658 | // [OpenCL 1.1 6.11.12] "The vec_step built-in function takes a built-in |
| 2659 | // scalar or vector data type argument..." |
| 2660 | // Every built-in scalar type (OpenCL 1.1 6.1.1) is either an arithmetic |
| 2661 | // type (C99 6.2.5p18) or void. |
| 2662 | if (!(T->isArithmeticType() || T->isVoidType() || T->isVectorType())) { |
| 2663 | S.Diag(Loc, diag::err_vecstep_non_scalar_vector_type) |
| 2664 | << T << ArgRange; |
| 2665 | return true; |
| 2666 | } |
| 2667 | |
| 2668 | assert((T->isVoidType() || !T->isIncompleteType()) && |
| 2669 | "Scalar types should always be complete"); |
| 2670 | return false; |
| 2671 | } |
| 2672 | |
Chandler Carruth | 42ec65d | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 2673 | static bool CheckExtensionTraitOperandType(Sema &S, QualType T, |
| 2674 | SourceLocation Loc, |
| 2675 | SourceRange ArgRange, |
| 2676 | UnaryExprOrTypeTrait TraitKind) { |
| 2677 | // C99 6.5.3.4p1: |
| 2678 | if (T->isFunctionType()) { |
| 2679 | // alignof(function) is allowed as an extension. |
| 2680 | if (TraitKind == UETT_SizeOf) |
| 2681 | S.Diag(Loc, diag::ext_sizeof_function_type) << ArgRange; |
| 2682 | return false; |
| 2683 | } |
| 2684 | |
| 2685 | // Allow sizeof(void)/alignof(void) as an extension. |
| 2686 | if (T->isVoidType()) { |
| 2687 | S.Diag(Loc, diag::ext_sizeof_void_type) << TraitKind << ArgRange; |
| 2688 | return false; |
| 2689 | } |
| 2690 | |
| 2691 | return true; |
| 2692 | } |
| 2693 | |
| 2694 | static bool CheckObjCTraitOperandConstraints(Sema &S, QualType T, |
| 2695 | SourceLocation Loc, |
| 2696 | SourceRange ArgRange, |
| 2697 | UnaryExprOrTypeTrait TraitKind) { |
| 2698 | // Reject sizeof(interface) and sizeof(interface<proto>) in 64-bit mode. |
| 2699 | if (S.LangOpts.ObjCNonFragileABI && T->isObjCObjectType()) { |
| 2700 | S.Diag(Loc, diag::err_sizeof_nonfragile_interface) |
| 2701 | << T << (TraitKind == UETT_SizeOf) |
| 2702 | << ArgRange; |
| 2703 | return true; |
| 2704 | } |
| 2705 | |
| 2706 | return false; |
| 2707 | } |
| 2708 | |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2709 | /// \brief Check the constrains on expression operands to unary type expression |
| 2710 | /// and type traits. |
| 2711 | /// |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2712 | /// Completes any types necessary and validates the constraints on the operand |
| 2713 | /// expression. The logic mostly mirrors the type-based overload, but may modify |
| 2714 | /// the expression as it completes the type for that expression through template |
| 2715 | /// instantiation, etc. |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2716 | bool Sema::CheckUnaryExprOrTypeTraitOperand(Expr *Op, |
| 2717 | UnaryExprOrTypeTrait ExprKind) { |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2718 | QualType ExprTy = Op->getType(); |
| 2719 | |
| 2720 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 2721 | // the result is the size of the referenced type." |
| 2722 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 2723 | // result shall be the alignment of the referenced type." |
| 2724 | if (const ReferenceType *Ref = ExprTy->getAs<ReferenceType>()) |
| 2725 | ExprTy = Ref->getPointeeType(); |
| 2726 | |
| 2727 | if (ExprKind == UETT_VecStep) |
| 2728 | return CheckVecStepTraitOperandType(*this, ExprTy, Op->getExprLoc(), |
| 2729 | Op->getSourceRange()); |
| 2730 | |
| 2731 | // Whitelist some types as extensions |
| 2732 | if (!CheckExtensionTraitOperandType(*this, ExprTy, Op->getExprLoc(), |
| 2733 | Op->getSourceRange(), ExprKind)) |
| 2734 | return false; |
| 2735 | |
| 2736 | if (RequireCompleteExprType(Op, |
| 2737 | PDiag(diag::err_sizeof_alignof_incomplete_type) |
| 2738 | << ExprKind << Op->getSourceRange(), |
| 2739 | std::make_pair(SourceLocation(), PDiag(0)))) |
| 2740 | return true; |
| 2741 | |
| 2742 | // Completeing the expression's type may have changed it. |
| 2743 | ExprTy = Op->getType(); |
| 2744 | if (const ReferenceType *Ref = ExprTy->getAs<ReferenceType>()) |
| 2745 | ExprTy = Ref->getPointeeType(); |
| 2746 | |
| 2747 | if (CheckObjCTraitOperandConstraints(*this, ExprTy, Op->getExprLoc(), |
| 2748 | Op->getSourceRange(), ExprKind)) |
| 2749 | return true; |
| 2750 | |
Nico Weber | cf73992 | 2011-06-15 02:47:03 +0000 | [diff] [blame] | 2751 | if (ExprKind == UETT_SizeOf) { |
| 2752 | if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(Op->IgnoreParens())) { |
| 2753 | if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(DeclRef->getFoundDecl())) { |
| 2754 | QualType OType = PVD->getOriginalType(); |
| 2755 | QualType Type = PVD->getType(); |
| 2756 | if (Type->isPointerType() && OType->isArrayType()) { |
| 2757 | Diag(Op->getExprLoc(), diag::warn_sizeof_array_param) |
| 2758 | << Type << OType; |
| 2759 | Diag(PVD->getLocation(), diag::note_declared_at); |
| 2760 | } |
| 2761 | } |
| 2762 | } |
| 2763 | } |
| 2764 | |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2765 | return false; |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2766 | } |
| 2767 | |
| 2768 | /// \brief Check the constraints on operands to unary expression and type |
| 2769 | /// traits. |
| 2770 | /// |
| 2771 | /// This will complete any types necessary, and validate the various constraints |
| 2772 | /// on those operands. |
| 2773 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2774 | /// The UsualUnaryConversions() function is *not* called by this routine. |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2775 | /// C99 6.3.2.1p[2-4] all state: |
| 2776 | /// Except when it is the operand of the sizeof operator ... |
| 2777 | /// |
| 2778 | /// C++ [expr.sizeof]p4 |
| 2779 | /// The lvalue-to-rvalue, array-to-pointer, and function-to-pointer |
| 2780 | /// standard conversions are not applied to the operand of sizeof. |
| 2781 | /// |
| 2782 | /// This policy is followed for all of the unary trait expressions. |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2783 | bool Sema::CheckUnaryExprOrTypeTraitOperand(QualType exprType, |
| 2784 | SourceLocation OpLoc, |
| 2785 | SourceRange ExprRange, |
| 2786 | UnaryExprOrTypeTrait ExprKind) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2787 | if (exprType->isDependentType()) |
| 2788 | return false; |
| 2789 | |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 2790 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 2791 | // the result is the size of the referenced type." |
| 2792 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 2793 | // result shall be the alignment of the referenced type." |
| 2794 | if (const ReferenceType *Ref = exprType->getAs<ReferenceType>()) |
| 2795 | exprType = Ref->getPointeeType(); |
| 2796 | |
Chandler Carruth | df1f377 | 2011-05-26 08:53:12 +0000 | [diff] [blame] | 2797 | if (ExprKind == UETT_VecStep) |
| 2798 | return CheckVecStepTraitOperandType(*this, exprType, OpLoc, ExprRange); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2799 | |
Chandler Carruth | 42ec65d | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 2800 | // Whitelist some types as extensions |
| 2801 | if (!CheckExtensionTraitOperandType(*this, exprType, OpLoc, ExprRange, |
| 2802 | ExprKind)) |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 2803 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2804 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2805 | if (RequireCompleteType(OpLoc, exprType, |
Douglas Gregor | 5cc07df | 2009-12-15 16:44:32 +0000 | [diff] [blame] | 2806 | PDiag(diag::err_sizeof_alignof_incomplete_type) |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2807 | << ExprKind << ExprRange)) |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2808 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2809 | |
Chandler Carruth | 42ec65d | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 2810 | if (CheckObjCTraitOperandConstraints(*this, exprType, OpLoc, ExprRange, |
| 2811 | ExprKind)) |
Chris Lattner | 5cb10d3 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 2812 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2813 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 2814 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2815 | } |
| 2816 | |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2817 | static bool CheckAlignOfExpr(Sema &S, Expr *E) { |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 2818 | E = E->IgnoreParens(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2819 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2820 | // alignof decl is always ok. |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 2821 | if (isa<DeclRefExpr>(E)) |
| 2822 | return false; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 2823 | |
| 2824 | // Cannot know anything else if the expression is dependent. |
| 2825 | if (E->isTypeDependent()) |
| 2826 | return false; |
| 2827 | |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2828 | if (E->getBitField()) { |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2829 | S.Diag(E->getExprLoc(), diag::err_sizeof_alignof_bitfield) |
| 2830 | << 1 << E->getSourceRange(); |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2831 | return true; |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 2832 | } |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2833 | |
| 2834 | // Alignment of a field access is always okay, so long as it isn't a |
| 2835 | // bit-field. |
| 2836 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 2837 | if (isa<FieldDecl>(ME->getMemberDecl())) |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2838 | return false; |
| 2839 | |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2840 | return S.CheckUnaryExprOrTypeTraitOperand(E, UETT_AlignOf); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2841 | } |
| 2842 | |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2843 | bool Sema::CheckVecStepExpr(Expr *E) { |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2844 | E = E->IgnoreParens(); |
| 2845 | |
| 2846 | // Cannot know anything else if the expression is dependent. |
| 2847 | if (E->isTypeDependent()) |
| 2848 | return false; |
| 2849 | |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2850 | return CheckUnaryExprOrTypeTraitOperand(E, UETT_VecStep); |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 2851 | } |
| 2852 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2853 | /// \brief Build a sizeof or alignof expression given a type operand. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2854 | ExprResult |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2855 | Sema::CreateUnaryExprOrTypeTraitExpr(TypeSourceInfo *TInfo, |
| 2856 | SourceLocation OpLoc, |
| 2857 | UnaryExprOrTypeTrait ExprKind, |
| 2858 | SourceRange R) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2859 | if (!TInfo) |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2860 | return ExprError(); |
| 2861 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2862 | QualType T = TInfo->getType(); |
John McCall | 5ab7517 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 2863 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2864 | if (!T->isDependentType() && |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2865 | CheckUnaryExprOrTypeTraitOperand(T, OpLoc, R, ExprKind)) |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2866 | return ExprError(); |
| 2867 | |
| 2868 | // 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] | 2869 | return Owned(new (Context) UnaryExprOrTypeTraitExpr(ExprKind, TInfo, |
| 2870 | Context.getSizeType(), |
| 2871 | OpLoc, R.getEnd())); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2872 | } |
| 2873 | |
| 2874 | /// \brief Build a sizeof or alignof expression given an expression |
| 2875 | /// operand. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2876 | ExprResult |
Chandler Carruth | e72c55b | 2011-05-29 07:32:14 +0000 | [diff] [blame] | 2877 | Sema::CreateUnaryExprOrTypeTraitExpr(Expr *E, SourceLocation OpLoc, |
| 2878 | UnaryExprOrTypeTrait ExprKind) { |
Douglas Gregor | 4f0845e | 2011-06-22 23:21:00 +0000 | [diff] [blame] | 2879 | ExprResult PE = CheckPlaceholderExpr(E); |
| 2880 | if (PE.isInvalid()) |
| 2881 | return ExprError(); |
| 2882 | |
| 2883 | E = PE.get(); |
| 2884 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2885 | // Verify that the operand is valid. |
| 2886 | bool isInvalid = false; |
| 2887 | if (E->isTypeDependent()) { |
| 2888 | // Delay type-checking for type-dependent expressions. |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2889 | } else if (ExprKind == UETT_AlignOf) { |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2890 | isInvalid = CheckAlignOfExpr(*this, E); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2891 | } else if (ExprKind == UETT_VecStep) { |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2892 | isInvalid = CheckVecStepExpr(E); |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 2893 | } else if (E->getBitField()) { // C99 6.5.3.4p1. |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2894 | Diag(E->getExprLoc(), diag::err_sizeof_alignof_bitfield) << 0; |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2895 | isInvalid = true; |
| 2896 | } else { |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2897 | isInvalid = CheckUnaryExprOrTypeTraitOperand(E, UETT_SizeOf); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2898 | } |
| 2899 | |
| 2900 | if (isInvalid) |
| 2901 | return ExprError(); |
| 2902 | |
| 2903 | // 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] | 2904 | return Owned(new (Context) UnaryExprOrTypeTraitExpr( |
Chandler Carruth | e72c55b | 2011-05-29 07:32:14 +0000 | [diff] [blame] | 2905 | ExprKind, E, Context.getSizeType(), OpLoc, |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2906 | E->getSourceRange().getEnd())); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2907 | } |
| 2908 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2909 | /// ActOnUnaryExprOrTypeTraitExpr - Handle @c sizeof(type) and @c sizeof @c |
| 2910 | /// expr and the same for @c alignof and @c __alignof |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2911 | /// Note that the ArgRange is invalid if isType is false. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2912 | ExprResult |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2913 | Sema::ActOnUnaryExprOrTypeTraitExpr(SourceLocation OpLoc, |
| 2914 | UnaryExprOrTypeTrait ExprKind, bool isType, |
| 2915 | void *TyOrEx, const SourceRange &ArgRange) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2916 | // If error parsing type, ignore. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2917 | if (TyOrEx == 0) return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2918 | |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2919 | if (isType) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 2920 | TypeSourceInfo *TInfo; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 2921 | (void) GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrEx), &TInfo); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 2922 | return CreateUnaryExprOrTypeTraitExpr(TInfo, OpLoc, ExprKind, ArgRange); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2923 | } |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 2924 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2925 | Expr *ArgEx = (Expr *)TyOrEx; |
Chandler Carruth | e72c55b | 2011-05-29 07:32:14 +0000 | [diff] [blame] | 2926 | ExprResult Result = CreateUnaryExprOrTypeTraitExpr(ArgEx, OpLoc, ExprKind); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 2927 | return move(Result); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2928 | } |
| 2929 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2930 | static QualType CheckRealImagOperand(Sema &S, ExprResult &V, SourceLocation Loc, |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 2931 | bool isReal) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2932 | if (V.get()->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 2933 | return S.Context.DependentTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2934 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 2935 | // _Real and _Imag are only l-values for normal l-values. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2936 | if (V.get()->getObjectKind() != OK_Ordinary) { |
| 2937 | V = S.DefaultLvalueConversion(V.take()); |
| 2938 | if (V.isInvalid()) |
| 2939 | return QualType(); |
| 2940 | } |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 2941 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 2942 | // These operators return the element type of a complex type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2943 | if (const ComplexType *CT = V.get()->getType()->getAs<ComplexType>()) |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 2944 | return CT->getElementType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2945 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 2946 | // Otherwise they pass through real integer and floating point types here. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2947 | if (V.get()->getType()->isArithmeticType()) |
| 2948 | return V.get()->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2949 | |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 2950 | // Test for placeholders. |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 2951 | ExprResult PR = S.CheckPlaceholderExpr(V.get()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 2952 | if (PR.isInvalid()) return QualType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2953 | if (PR.get() != V.get()) { |
| 2954 | V = move(PR); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 2955 | return CheckRealImagOperand(S, V, Loc, isReal); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 2956 | } |
| 2957 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 2958 | // Reject anything else. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2959 | S.Diag(Loc, diag::err_realimag_invalid_type) << V.get()->getType() |
Chris Lattner | ba27e2a | 2009-02-17 08:12:06 +0000 | [diff] [blame] | 2960 | << (isReal ? "__real" : "__imag"); |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 2961 | return QualType(); |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 2962 | } |
| 2963 | |
| 2964 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2965 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2966 | ExprResult |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2967 | Sema::ActOnPostfixUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2968 | tok::TokenKind Kind, Expr *Input) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2969 | UnaryOperatorKind Opc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2970 | switch (Kind) { |
| 2971 | default: assert(0 && "Unknown unary op!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 2972 | case tok::plusplus: Opc = UO_PostInc; break; |
| 2973 | case tok::minusminus: Opc = UO_PostDec; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2974 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2975 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2976 | return BuildUnaryOp(S, OpLoc, Opc, Input); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2977 | } |
| 2978 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2979 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2980 | Sema::ActOnArraySubscriptExpr(Scope *S, Expr *Base, SourceLocation LLoc, |
| 2981 | Expr *Idx, SourceLocation RLoc) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 2982 | // Since this might be a postfix expression, get rid of ParenListExprs. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2983 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2984 | if (Result.isInvalid()) return ExprError(); |
| 2985 | Base = Result.take(); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 2986 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 2987 | Expr *LHSExp = Base, *RHSExp = Idx; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2988 | |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 2989 | if (getLangOptions().CPlusPlus && |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 2990 | (LHSExp->isTypeDependent() || RHSExp->isTypeDependent())) { |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 2991 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 2992 | Context.DependentTy, |
| 2993 | VK_LValue, OK_Ordinary, |
| 2994 | RLoc)); |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 2995 | } |
| 2996 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2997 | if (getLangOptions().CPlusPlus && |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 2998 | (LHSExp->getType()->isRecordType() || |
Eli Friedman | 03f332a | 2008-12-15 22:34:21 +0000 | [diff] [blame] | 2999 | LHSExp->getType()->isEnumeralType() || |
| 3000 | RHSExp->getType()->isRecordType() || |
| 3001 | RHSExp->getType()->isEnumeralType())) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3002 | return CreateOverloadedArraySubscriptExpr(LLoc, RLoc, Base, Idx); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 3003 | } |
| 3004 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3005 | return CreateBuiltinArraySubscriptExpr(Base, LLoc, Idx, RLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 3006 | } |
| 3007 | |
| 3008 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3009 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3010 | Sema::CreateBuiltinArraySubscriptExpr(Expr *Base, SourceLocation LLoc, |
| 3011 | Expr *Idx, SourceLocation RLoc) { |
| 3012 | Expr *LHSExp = Base; |
| 3013 | Expr *RHSExp = Idx; |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 3014 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3015 | // Perform default conversions. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3016 | if (!LHSExp->getType()->getAs<VectorType>()) { |
| 3017 | ExprResult Result = DefaultFunctionArrayLvalueConversion(LHSExp); |
| 3018 | if (Result.isInvalid()) |
| 3019 | return ExprError(); |
| 3020 | LHSExp = Result.take(); |
| 3021 | } |
| 3022 | ExprResult Result = DefaultFunctionArrayLvalueConversion(RHSExp); |
| 3023 | if (Result.isInvalid()) |
| 3024 | return ExprError(); |
| 3025 | RHSExp = Result.take(); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3026 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3027 | QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3028 | ExprValueKind VK = VK_LValue; |
| 3029 | ExprObjectKind OK = OK_Ordinary; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3030 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3031 | // 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] | 3032 | // 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] | 3033 | // 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] | 3034 | // and index from the expression types. |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3035 | Expr *BaseExpr, *IndexExpr; |
| 3036 | QualType ResultType; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3037 | if (LHSTy->isDependentType() || RHSTy->isDependentType()) { |
| 3038 | BaseExpr = LHSExp; |
| 3039 | IndexExpr = RHSExp; |
| 3040 | ResultType = Context.DependentTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3041 | } else if (const PointerType *PTy = LHSTy->getAs<PointerType>()) { |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3042 | BaseExpr = LHSExp; |
| 3043 | IndexExpr = RHSExp; |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3044 | ResultType = PTy->getPointeeType(); |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3045 | } else if (const PointerType *PTy = RHSTy->getAs<PointerType>()) { |
Chris Lattner | 7a2e047 | 2007-07-16 00:23:25 +0000 | [diff] [blame] | 3046 | // Handle the uncommon case of "123[Ptr]". |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3047 | BaseExpr = RHSExp; |
| 3048 | IndexExpr = LHSExp; |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3049 | ResultType = PTy->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3050 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3051 | LHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3052 | BaseExpr = LHSExp; |
| 3053 | IndexExpr = RHSExp; |
| 3054 | ResultType = PTy->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3055 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3056 | RHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3057 | // Handle the uncommon case of "123[Ptr]". |
| 3058 | BaseExpr = RHSExp; |
| 3059 | IndexExpr = LHSExp; |
| 3060 | ResultType = PTy->getPointeeType(); |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3061 | } else if (const VectorType *VTy = LHSTy->getAs<VectorType>()) { |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 3062 | BaseExpr = LHSExp; // vectors: V[123] |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3063 | IndexExpr = RHSExp; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3064 | VK = LHSExp->getValueKind(); |
| 3065 | if (VK != VK_RValue) |
| 3066 | OK = OK_VectorComponent; |
Nate Begeman | 334a802 | 2009-01-18 00:45:31 +0000 | [diff] [blame] | 3067 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3068 | // FIXME: need to deal with const... |
| 3069 | ResultType = VTy->getElementType(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3070 | } else if (LHSTy->isArrayType()) { |
| 3071 | // If we see an array that wasn't promoted by |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 3072 | // DefaultFunctionArrayLvalueConversion, it must be an array that |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3073 | // wasn't promoted because of the C90 rule that doesn't |
| 3074 | // allow promoting non-lvalue arrays. Warn, then |
| 3075 | // force the promotion here. |
| 3076 | Diag(LHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 3077 | LHSExp->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3078 | LHSExp = ImpCastExprToType(LHSExp, Context.getArrayDecayedType(LHSTy), |
| 3079 | CK_ArrayToPointerDecay).take(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3080 | LHSTy = LHSExp->getType(); |
| 3081 | |
| 3082 | BaseExpr = LHSExp; |
| 3083 | IndexExpr = RHSExp; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3084 | ResultType = LHSTy->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3085 | } else if (RHSTy->isArrayType()) { |
| 3086 | // Same as previous, except for 123[f().a] case |
| 3087 | Diag(RHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 3088 | RHSExp->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3089 | RHSExp = ImpCastExprToType(RHSExp, Context.getArrayDecayedType(RHSTy), |
| 3090 | CK_ArrayToPointerDecay).take(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3091 | RHSTy = RHSExp->getType(); |
| 3092 | |
| 3093 | BaseExpr = RHSExp; |
| 3094 | IndexExpr = LHSExp; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3095 | ResultType = RHSTy->getAs<PointerType>()->getPointeeType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3096 | } else { |
Chris Lattner | 338395d | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 3097 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_value) |
| 3098 | << LHSExp->getSourceRange() << RHSExp->getSourceRange()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3099 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3100 | // C99 6.5.2.1p1 |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 3101 | if (!IndexExpr->getType()->isIntegerType() && !IndexExpr->isTypeDependent()) |
Chris Lattner | 338395d | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 3102 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_not_integer) |
| 3103 | << IndexExpr->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3104 | |
Daniel Dunbar | 7e88a60 | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 3105 | if ((IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_S) || |
Sam Weinig | 0f9a5b5 | 2009-09-14 20:14:57 +0000 | [diff] [blame] | 3106 | IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U)) |
| 3107 | && !IndexExpr->isTypeDependent()) |
Sam Weinig | 76e2b71 | 2009-09-14 01:58:58 +0000 | [diff] [blame] | 3108 | Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange(); |
| 3109 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3110 | // 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] | 3111 | // C++ [expr.sub]p1: The type "T" shall be a completely-defined object |
| 3112 | // type. Note that Functions are not objects, and that (in C99 parlance) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3113 | // incomplete types are not object types. |
| 3114 | if (ResultType->isFunctionType()) { |
| 3115 | Diag(BaseExpr->getLocStart(), diag::err_subscript_function_type) |
| 3116 | << ResultType << BaseExpr->getSourceRange(); |
| 3117 | return ExprError(); |
| 3118 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3119 | |
Abramo Bagnara | 4635845 | 2010-09-13 06:50:07 +0000 | [diff] [blame] | 3120 | if (ResultType->isVoidType() && !getLangOptions().CPlusPlus) { |
| 3121 | // GNU extension: subscripting on pointer to void |
Chandler Carruth | 6628969 | 2011-06-27 16:32:27 +0000 | [diff] [blame] | 3122 | Diag(LLoc, diag::ext_gnu_subscript_void_type) |
| 3123 | << BaseExpr->getSourceRange(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3124 | |
| 3125 | // C forbids expressions of unqualified void type from being l-values. |
| 3126 | // See IsCForbiddenLValueType. |
| 3127 | if (!ResultType.hasQualifiers()) VK = VK_RValue; |
Abramo Bagnara | 4635845 | 2010-09-13 06:50:07 +0000 | [diff] [blame] | 3128 | } else if (!ResultType->isDependentType() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3129 | RequireCompleteType(LLoc, ResultType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3130 | PDiag(diag::err_subscript_incomplete_type) |
| 3131 | << BaseExpr->getSourceRange())) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3132 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3133 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3134 | // Diagnose bad cases where we step over interface counts. |
John McCall | c12c5bb | 2010-05-15 11:32:37 +0000 | [diff] [blame] | 3135 | if (ResultType->isObjCObjectType() && LangOpts.ObjCNonFragileABI) { |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3136 | Diag(LLoc, diag::err_subscript_nonfragile_interface) |
| 3137 | << ResultType << BaseExpr->getSourceRange(); |
| 3138 | return ExprError(); |
| 3139 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3140 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3141 | assert(VK == VK_RValue || LangOpts.CPlusPlus || |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 3142 | !ResultType.isCForbiddenLValueType()); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3143 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3144 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3145 | ResultType, VK, OK, RLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3146 | } |
| 3147 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3148 | ExprResult Sema::BuildCXXDefaultArgExpr(SourceLocation CallLoc, |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 3149 | FunctionDecl *FD, |
| 3150 | ParmVarDecl *Param) { |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3151 | if (Param->hasUnparsedDefaultArg()) { |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3152 | Diag(CallLoc, |
Nico Weber | 15d5c83 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 3153 | diag::err_use_of_default_argument_to_function_declared_later) << |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3154 | FD << cast<CXXRecordDecl>(FD->getDeclContext())->getDeclName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3155 | Diag(UnparsedDefaultArgLocs[Param], |
Nico Weber | 15d5c83 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 3156 | diag::note_default_argument_declared_here); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3157 | return ExprError(); |
| 3158 | } |
| 3159 | |
| 3160 | if (Param->hasUninstantiatedDefaultArg()) { |
| 3161 | Expr *UninstExpr = Param->getUninstantiatedDefaultArg(); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3162 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3163 | // Instantiate the expression. |
| 3164 | MultiLevelTemplateArgumentList ArgList |
| 3165 | = getTemplateInstantiationArgs(FD, 0, /*RelativeToPrimary=*/true); |
Anders Carlsson | 25cae7f | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 3166 | |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 3167 | std::pair<const TemplateArgument *, unsigned> Innermost |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3168 | = ArgList.getInnermost(); |
| 3169 | InstantiatingTemplate Inst(*this, CallLoc, Param, Innermost.first, |
| 3170 | Innermost.second); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3171 | |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 3172 | ExprResult Result; |
| 3173 | { |
| 3174 | // C++ [dcl.fct.default]p5: |
| 3175 | // The names in the [default argument] expression are bound, and |
| 3176 | // the semantic constraints are checked, at the point where the |
| 3177 | // default argument expression appears. |
Nico Weber | 15d5c83 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 3178 | ContextRAII SavedContext(*this, FD); |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 3179 | Result = SubstExpr(UninstExpr, ArgList); |
| 3180 | } |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3181 | if (Result.isInvalid()) |
| 3182 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3183 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3184 | // Check the expression as an initializer for the parameter. |
| 3185 | InitializedEntity Entity |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 3186 | = InitializedEntity::InitializeParameter(Context, Param); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3187 | InitializationKind Kind |
| 3188 | = InitializationKind::CreateCopy(Param->getLocation(), |
| 3189 | /*FIXME:EqualLoc*/UninstExpr->getSourceRange().getBegin()); |
| 3190 | Expr *ResultE = Result.takeAs<Expr>(); |
Douglas Gregor | 65222e8 | 2009-12-23 18:19:08 +0000 | [diff] [blame] | 3191 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3192 | InitializationSequence InitSeq(*this, Entity, Kind, &ResultE, 1); |
| 3193 | Result = InitSeq.Perform(*this, Entity, Kind, |
| 3194 | MultiExprArg(*this, &ResultE, 1)); |
| 3195 | if (Result.isInvalid()) |
| 3196 | return ExprError(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3197 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3198 | // Build the default argument expression. |
| 3199 | return Owned(CXXDefaultArgExpr::Create(Context, CallLoc, Param, |
| 3200 | Result.takeAs<Expr>())); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3201 | } |
| 3202 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3203 | // If the default expression creates temporaries, we need to |
| 3204 | // push them to the current stack of expression temporaries so they'll |
| 3205 | // be properly destroyed. |
| 3206 | // FIXME: We should really be rebuilding the default argument with new |
| 3207 | // bound temporaries; see the comment in PR5810. |
Douglas Gregor | 5833b0b | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 3208 | for (unsigned i = 0, e = Param->getNumDefaultArgTemporaries(); i != e; ++i) { |
| 3209 | CXXTemporary *Temporary = Param->getDefaultArgTemporary(i); |
| 3210 | MarkDeclarationReferenced(Param->getDefaultArg()->getLocStart(), |
| 3211 | const_cast<CXXDestructorDecl*>(Temporary->getDestructor())); |
| 3212 | ExprTemporaries.push_back(Temporary); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3213 | ExprNeedsCleanups = true; |
Douglas Gregor | 5833b0b | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 3214 | } |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3215 | |
| 3216 | // We already type-checked the argument, so we know it works. |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 3217 | // Just mark all of the declarations in this potentially-evaluated expression |
| 3218 | // as being "referenced". |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3219 | MarkDeclarationsReferencedInExpr(Param->getDefaultArg()); |
Douglas Gregor | 036aed1 | 2009-12-23 23:03:06 +0000 | [diff] [blame] | 3220 | return Owned(CXXDefaultArgExpr::Create(Context, CallLoc, Param)); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3221 | } |
| 3222 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3223 | /// ConvertArgumentsForCall - Converts the arguments specified in |
| 3224 | /// Args/NumArgs to the parameter types of the function FDecl with |
| 3225 | /// function prototype Proto. Call is the call expression itself, and |
| 3226 | /// Fn is the function expression. For a C++ member function, this |
| 3227 | /// routine does not attempt to convert the object argument. Returns |
| 3228 | /// true if the call is ill-formed. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3229 | bool |
| 3230 | Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3231 | FunctionDecl *FDecl, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3232 | const FunctionProtoType *Proto, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3233 | Expr **Args, unsigned NumArgs, |
| 3234 | SourceLocation RParenLoc) { |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 3235 | // Bail out early if calling a builtin with custom typechecking. |
| 3236 | // We don't need to do this in the |
| 3237 | if (FDecl) |
| 3238 | if (unsigned ID = FDecl->getBuiltinID()) |
| 3239 | if (Context.BuiltinInfo.hasCustomTypechecking(ID)) |
| 3240 | return false; |
| 3241 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3242 | // 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] | 3243 | // assignment, to the types of the corresponding parameter, ... |
| 3244 | unsigned NumArgsInProto = Proto->getNumArgs(); |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 3245 | bool Invalid = false; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3246 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3247 | // If too few arguments are available (and we don't have default |
| 3248 | // arguments for the remaining parameters), don't make the call. |
| 3249 | if (NumArgs < NumArgsInProto) { |
Peter Collingbourne | 9aab148 | 2011-07-29 00:24:42 +0000 | [diff] [blame] | 3250 | if (!FDecl || NumArgs < FDecl->getMinRequiredArguments()) { |
| 3251 | Diag(RParenLoc, diag::err_typecheck_call_too_few_args) |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 3252 | << Fn->getType()->isBlockPointerType() |
Eric Christopher | d77b9a2 | 2010-04-16 04:48:22 +0000 | [diff] [blame] | 3253 | << NumArgsInProto << NumArgs << Fn->getSourceRange(); |
Peter Collingbourne | 9aab148 | 2011-07-29 00:24:42 +0000 | [diff] [blame] | 3254 | |
| 3255 | // Emit the location of the prototype. |
| 3256 | if (FDecl && !FDecl->getBuiltinID()) |
| 3257 | Diag(FDecl->getLocStart(), diag::note_callee_decl) |
| 3258 | << FDecl; |
| 3259 | |
| 3260 | return true; |
| 3261 | } |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3262 | Call->setNumArgs(Context, NumArgsInProto); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3263 | } |
| 3264 | |
| 3265 | // If too many are passed and not variadic, error on the extras and drop |
| 3266 | // them. |
| 3267 | if (NumArgs > NumArgsInProto) { |
| 3268 | if (!Proto->isVariadic()) { |
| 3269 | Diag(Args[NumArgsInProto]->getLocStart(), |
| 3270 | diag::err_typecheck_call_too_many_args) |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 3271 | << Fn->getType()->isBlockPointerType() |
Eric Christopher | ccfa963 | 2010-04-16 04:56:46 +0000 | [diff] [blame] | 3272 | << NumArgsInProto << NumArgs << Fn->getSourceRange() |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3273 | << SourceRange(Args[NumArgsInProto]->getLocStart(), |
| 3274 | Args[NumArgs-1]->getLocEnd()); |
Ted Kremenek | 5862f0e | 2011-04-04 17:22:27 +0000 | [diff] [blame] | 3275 | |
| 3276 | // Emit the location of the prototype. |
| 3277 | if (FDecl && !FDecl->getBuiltinID()) |
Peter Collingbourne | 9aab148 | 2011-07-29 00:24:42 +0000 | [diff] [blame] | 3278 | Diag(FDecl->getLocStart(), diag::note_callee_decl) |
| 3279 | << FDecl; |
Ted Kremenek | 5862f0e | 2011-04-04 17:22:27 +0000 | [diff] [blame] | 3280 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3281 | // This deletes the extra arguments. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3282 | Call->setNumArgs(Context, NumArgsInProto); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3283 | return true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3284 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3285 | } |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3286 | SmallVector<Expr *, 8> AllArgs; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3287 | VariadicCallType CallType = |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 3288 | Proto->isVariadic() ? VariadicFunction : VariadicDoesNotApply; |
| 3289 | if (Fn->getType()->isBlockPointerType()) |
| 3290 | CallType = VariadicBlock; // Block |
| 3291 | else if (isa<MemberExpr>(Fn)) |
| 3292 | CallType = VariadicMethod; |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3293 | Invalid = GatherArgumentsForCall(Call->getSourceRange().getBegin(), FDecl, |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 3294 | Proto, 0, Args, NumArgs, AllArgs, CallType); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3295 | if (Invalid) |
| 3296 | return true; |
| 3297 | unsigned TotalNumArgs = AllArgs.size(); |
| 3298 | for (unsigned i = 0; i < TotalNumArgs; ++i) |
| 3299 | Call->setArg(i, AllArgs[i]); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3300 | |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3301 | return false; |
| 3302 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3303 | |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3304 | bool Sema::GatherArgumentsForCall(SourceLocation CallLoc, |
| 3305 | FunctionDecl *FDecl, |
| 3306 | const FunctionProtoType *Proto, |
| 3307 | unsigned FirstProtoArg, |
| 3308 | Expr **Args, unsigned NumArgs, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3309 | SmallVector<Expr *, 8> &AllArgs, |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 3310 | VariadicCallType CallType) { |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3311 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 3312 | unsigned NumArgsToCheck = NumArgs; |
| 3313 | bool Invalid = false; |
| 3314 | if (NumArgs != NumArgsInProto) |
| 3315 | // Use default arguments for missing arguments |
| 3316 | NumArgsToCheck = NumArgsInProto; |
| 3317 | unsigned ArgIx = 0; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3318 | // 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] | 3319 | for (unsigned i = FirstProtoArg; i != NumArgsToCheck; i++) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3320 | QualType ProtoArgType = Proto->getArgType(i); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3321 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3322 | Expr *Arg; |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3323 | if (ArgIx < NumArgs) { |
| 3324 | Arg = Args[ArgIx++]; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3325 | |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3326 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 3327 | ProtoArgType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3328 | PDiag(diag::err_call_incomplete_argument) |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3329 | << Arg->getSourceRange())) |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3330 | return true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3331 | |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3332 | // Pass the argument |
| 3333 | ParmVarDecl *Param = 0; |
| 3334 | if (FDecl && i < FDecl->getNumParams()) |
| 3335 | Param = FDecl->getParamDecl(i); |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 3336 | |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3337 | InitializedEntity Entity = |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 3338 | Param? InitializedEntity::InitializeParameter(Context, Param) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3339 | : InitializedEntity::InitializeParameter(Context, ProtoArgType, |
| 3340 | Proto->isArgConsumed(i)); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3341 | ExprResult ArgE = PerformCopyInitialization(Entity, |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3342 | SourceLocation(), |
| 3343 | Owned(Arg)); |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3344 | if (ArgE.isInvalid()) |
| 3345 | return true; |
| 3346 | |
| 3347 | Arg = ArgE.takeAs<Expr>(); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 3348 | } else { |
Anders Carlsson | ed961f9 | 2009-08-25 02:29:20 +0000 | [diff] [blame] | 3349 | ParmVarDecl *Param = FDecl->getParamDecl(i); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3350 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3351 | ExprResult ArgExpr = |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3352 | BuildCXXDefaultArgExpr(CallLoc, FDecl, Param); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3353 | if (ArgExpr.isInvalid()) |
| 3354 | return true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3355 | |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3356 | Arg = ArgExpr.takeAs<Expr>(); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 3357 | } |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 3358 | |
| 3359 | // Check for array bounds violations for each argument to the call. This |
| 3360 | // check only triggers warnings when the argument isn't a more complex Expr |
| 3361 | // with its own checking, such as a BinaryOperator. |
| 3362 | CheckArrayAccess(Arg); |
| 3363 | |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3364 | AllArgs.push_back(Arg); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3365 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3366 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3367 | // If this is a variadic call, handle args passed through "...". |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 3368 | if (CallType != VariadicDoesNotApply) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3369 | |
| 3370 | // Assume that extern "C" functions with variadic arguments that |
| 3371 | // return __unknown_anytype aren't *really* variadic. |
| 3372 | if (Proto->getResultType() == Context.UnknownAnyTy && |
| 3373 | FDecl && FDecl->isExternC()) { |
| 3374 | for (unsigned i = ArgIx; i != NumArgs; ++i) { |
| 3375 | ExprResult arg; |
| 3376 | if (isa<ExplicitCastExpr>(Args[i]->IgnoreParens())) |
| 3377 | arg = DefaultFunctionArrayLvalueConversion(Args[i]); |
| 3378 | else |
| 3379 | arg = DefaultVariadicArgumentPromotion(Args[i], CallType, FDecl); |
| 3380 | Invalid |= arg.isInvalid(); |
| 3381 | AllArgs.push_back(arg.take()); |
| 3382 | } |
| 3383 | |
| 3384 | // Otherwise do argument promotion, (C99 6.5.2.2p7). |
| 3385 | } else { |
| 3386 | for (unsigned i = ArgIx; i != NumArgs; ++i) { |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3387 | ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], CallType, |
| 3388 | FDecl); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3389 | Invalid |= Arg.isInvalid(); |
| 3390 | AllArgs.push_back(Arg.take()); |
| 3391 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3392 | } |
| 3393 | } |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 3394 | return Invalid; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3395 | } |
| 3396 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3397 | /// Given a function expression of unknown-any type, try to rebuild it |
| 3398 | /// to have a function type. |
| 3399 | static ExprResult rebuildUnknownAnyFunction(Sema &S, Expr *fn); |
| 3400 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3401 | /// ActOnCallExpr - Handle a call to Fn with the specified array of arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3402 | /// This provides the location of the left/right parens and a list of comma |
| 3403 | /// locations. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3404 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3405 | Sema::ActOnCallExpr(Scope *S, Expr *Fn, SourceLocation LParenLoc, |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3406 | MultiExprArg args, SourceLocation RParenLoc, |
| 3407 | Expr *ExecConfig) { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3408 | unsigned NumArgs = args.size(); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3409 | |
| 3410 | // Since this might be a postfix expression, get rid of ParenListExprs. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3411 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Fn); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3412 | if (Result.isInvalid()) return ExprError(); |
| 3413 | Fn = Result.take(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3414 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3415 | Expr **Args = args.release(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3416 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3417 | if (getLangOptions().CPlusPlus) { |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3418 | // If this is a pseudo-destructor expression, build the call immediately. |
| 3419 | if (isa<CXXPseudoDestructorExpr>(Fn)) { |
| 3420 | if (NumArgs > 0) { |
| 3421 | // Pseudo-destructor calls should not have any arguments. |
| 3422 | Diag(Fn->getLocStart(), diag::err_pseudo_dtor_call_with_args) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3423 | << FixItHint::CreateRemoval( |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3424 | SourceRange(Args[0]->getLocStart(), |
| 3425 | Args[NumArgs-1]->getLocEnd())); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3426 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3427 | NumArgs = 0; |
| 3428 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3429 | |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3430 | return Owned(new (Context) CallExpr(Context, Fn, 0, 0, Context.VoidTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3431 | VK_RValue, RParenLoc)); |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3432 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3433 | |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3434 | // Determine whether this is a dependent call inside a C++ template, |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3435 | // in which case we won't do any semantic analysis now. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3436 | // FIXME: Will need to cache the results of name lookup (including ADL) in |
| 3437 | // Fn. |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3438 | bool Dependent = false; |
| 3439 | if (Fn->isTypeDependent()) |
| 3440 | Dependent = true; |
| 3441 | else if (Expr::hasAnyTypeDependentArguments(Args, NumArgs)) |
| 3442 | Dependent = true; |
| 3443 | |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3444 | if (Dependent) { |
| 3445 | if (ExecConfig) { |
| 3446 | return Owned(new (Context) CUDAKernelCallExpr( |
| 3447 | Context, Fn, cast<CallExpr>(ExecConfig), Args, NumArgs, |
| 3448 | Context.DependentTy, VK_RValue, RParenLoc)); |
| 3449 | } else { |
| 3450 | return Owned(new (Context) CallExpr(Context, Fn, Args, NumArgs, |
| 3451 | Context.DependentTy, VK_RValue, |
| 3452 | RParenLoc)); |
| 3453 | } |
| 3454 | } |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3455 | |
| 3456 | // Determine whether this is a call to an object (C++ [over.call.object]). |
| 3457 | if (Fn->getType()->isRecordType()) |
| 3458 | return Owned(BuildCallToObjectOfClassType(S, Fn, LParenLoc, Args, NumArgs, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 3459 | RParenLoc)); |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3460 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3461 | if (Fn->getType() == Context.UnknownAnyTy) { |
| 3462 | ExprResult result = rebuildUnknownAnyFunction(*this, Fn); |
| 3463 | if (result.isInvalid()) return ExprError(); |
| 3464 | Fn = result.take(); |
| 3465 | } |
| 3466 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3467 | if (Fn->getType() == Context.BoundMemberTy) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3468 | return BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 3469 | RParenLoc); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3470 | } |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3471 | } |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3472 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3473 | // Check for overloaded calls. This can happen even in C due to extensions. |
| 3474 | if (Fn->getType() == Context.OverloadTy) { |
| 3475 | OverloadExpr::FindResult find = OverloadExpr::find(Fn); |
| 3476 | |
| 3477 | // We aren't supposed to apply this logic if there's an '&' involved. |
| 3478 | if (!find.IsAddressOfOperand) { |
| 3479 | OverloadExpr *ovl = find.Expression; |
| 3480 | if (isa<UnresolvedLookupExpr>(ovl)) { |
| 3481 | UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(ovl); |
| 3482 | return BuildOverloadedCallExpr(S, Fn, ULE, LParenLoc, Args, NumArgs, |
| 3483 | RParenLoc, ExecConfig); |
| 3484 | } else { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3485 | return BuildCallToMemberFunction(S, Fn, LParenLoc, Args, NumArgs, |
Douglas Gregor | a1a0478 | 2010-09-09 16:33:13 +0000 | [diff] [blame] | 3486 | RParenLoc); |
Anders Carlsson | 83ccfc3 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 3487 | } |
| 3488 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3489 | } |
| 3490 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3491 | // If we're directly calling a function, get the appropriate declaration. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3492 | |
Eli Friedman | efa42f7 | 2009-12-26 03:35:45 +0000 | [diff] [blame] | 3493 | Expr *NakedFn = Fn->IgnoreParens(); |
Douglas Gregor | ef9b149 | 2010-11-09 20:03:54 +0000 | [diff] [blame] | 3494 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3495 | NamedDecl *NDecl = 0; |
Douglas Gregor | d8f0ade | 2010-10-25 20:48:33 +0000 | [diff] [blame] | 3496 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(NakedFn)) |
| 3497 | if (UnOp->getOpcode() == UO_AddrOf) |
| 3498 | NakedFn = UnOp->getSubExpr()->IgnoreParens(); |
| 3499 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3500 | if (isa<DeclRefExpr>(NakedFn)) |
| 3501 | NDecl = cast<DeclRefExpr>(NakedFn)->getDecl(); |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3502 | else if (isa<MemberExpr>(NakedFn)) |
| 3503 | NDecl = cast<MemberExpr>(NakedFn)->getMemberDecl(); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3504 | |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3505 | return BuildResolvedCallExpr(Fn, NDecl, LParenLoc, Args, NumArgs, RParenLoc, |
| 3506 | ExecConfig); |
| 3507 | } |
| 3508 | |
| 3509 | ExprResult |
| 3510 | Sema::ActOnCUDAExecConfigExpr(Scope *S, SourceLocation LLLLoc, |
| 3511 | MultiExprArg execConfig, SourceLocation GGGLoc) { |
| 3512 | FunctionDecl *ConfigDecl = Context.getcudaConfigureCallDecl(); |
| 3513 | if (!ConfigDecl) |
| 3514 | return ExprError(Diag(LLLLoc, diag::err_undeclared_var_use) |
| 3515 | << "cudaConfigureCall"); |
| 3516 | QualType ConfigQTy = ConfigDecl->getType(); |
| 3517 | |
| 3518 | DeclRefExpr *ConfigDR = new (Context) DeclRefExpr( |
| 3519 | ConfigDecl, ConfigQTy, VK_LValue, LLLLoc); |
| 3520 | |
| 3521 | return ActOnCallExpr(S, ConfigDR, LLLLoc, execConfig, GGGLoc, 0); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3522 | } |
| 3523 | |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 3524 | /// ActOnAsTypeExpr - create a new asType (bitcast) from the arguments. |
| 3525 | /// |
| 3526 | /// __builtin_astype( value, dst type ) |
| 3527 | /// |
| 3528 | ExprResult Sema::ActOnAsTypeExpr(Expr *expr, ParsedType destty, |
| 3529 | SourceLocation BuiltinLoc, |
| 3530 | SourceLocation RParenLoc) { |
| 3531 | ExprValueKind VK = VK_RValue; |
| 3532 | ExprObjectKind OK = OK_Ordinary; |
| 3533 | QualType DstTy = GetTypeFromParser(destty); |
| 3534 | QualType SrcTy = expr->getType(); |
| 3535 | if (Context.getTypeSize(DstTy) != Context.getTypeSize(SrcTy)) |
| 3536 | return ExprError(Diag(BuiltinLoc, |
| 3537 | diag::err_invalid_astype_of_different_size) |
Peter Collingbourne | af9cddf | 2011-06-08 15:15:17 +0000 | [diff] [blame] | 3538 | << DstTy |
| 3539 | << SrcTy |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 3540 | << expr->getSourceRange()); |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3541 | return Owned(new (Context) AsTypeExpr(expr, DstTy, VK, OK, BuiltinLoc, |
| 3542 | RParenLoc)); |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 3543 | } |
| 3544 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3545 | /// BuildResolvedCallExpr - Build a call to a resolved expression, |
| 3546 | /// i.e. an expression not of \p OverloadTy. The expression should |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3547 | /// unary-convert to an expression of function-pointer or |
| 3548 | /// block-pointer type. |
| 3549 | /// |
| 3550 | /// \param NDecl the declaration being called, if available |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3551 | ExprResult |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3552 | Sema::BuildResolvedCallExpr(Expr *Fn, NamedDecl *NDecl, |
| 3553 | SourceLocation LParenLoc, |
| 3554 | Expr **Args, unsigned NumArgs, |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3555 | SourceLocation RParenLoc, |
| 3556 | Expr *Config) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3557 | FunctionDecl *FDecl = dyn_cast_or_null<FunctionDecl>(NDecl); |
| 3558 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 3559 | // Promote the function operand. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3560 | ExprResult Result = UsualUnaryConversions(Fn); |
| 3561 | if (Result.isInvalid()) |
| 3562 | return ExprError(); |
| 3563 | Fn = Result.take(); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 3564 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3565 | // Make the call expr early, before semantic checks. This guarantees cleanup |
| 3566 | // of arguments and function on error. |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3567 | CallExpr *TheCall; |
| 3568 | if (Config) { |
| 3569 | TheCall = new (Context) CUDAKernelCallExpr(Context, Fn, |
| 3570 | cast<CallExpr>(Config), |
| 3571 | Args, NumArgs, |
| 3572 | Context.BoolTy, |
| 3573 | VK_RValue, |
| 3574 | RParenLoc); |
| 3575 | } else { |
| 3576 | TheCall = new (Context) CallExpr(Context, Fn, |
| 3577 | Args, NumArgs, |
| 3578 | Context.BoolTy, |
| 3579 | VK_RValue, |
| 3580 | RParenLoc); |
| 3581 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3582 | |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 3583 | unsigned BuiltinID = (FDecl ? FDecl->getBuiltinID() : 0); |
| 3584 | |
| 3585 | // Bail out early if calling a builtin with custom typechecking. |
| 3586 | if (BuiltinID && Context.BuiltinInfo.hasCustomTypechecking(BuiltinID)) |
| 3587 | return CheckBuiltinFunctionCall(BuiltinID, TheCall); |
| 3588 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 3589 | retry: |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 3590 | const FunctionType *FuncT; |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 3591 | if (const PointerType *PT = Fn->getType()->getAs<PointerType>()) { |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 3592 | // C99 6.5.2.2p1 - "The expression that denotes the called function shall |
| 3593 | // have type pointer to function". |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3594 | FuncT = PT->getPointeeType()->getAs<FunctionType>(); |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 3595 | if (FuncT == 0) |
| 3596 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 3597 | << Fn->getType() << Fn->getSourceRange()); |
| 3598 | } else if (const BlockPointerType *BPT = |
| 3599 | Fn->getType()->getAs<BlockPointerType>()) { |
| 3600 | FuncT = BPT->getPointeeType()->castAs<FunctionType>(); |
| 3601 | } else { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 3602 | // Handle calls to expressions of unknown-any type. |
| 3603 | if (Fn->getType() == Context.UnknownAnyTy) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3604 | ExprResult rewrite = rebuildUnknownAnyFunction(*this, Fn); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 3605 | if (rewrite.isInvalid()) return ExprError(); |
| 3606 | Fn = rewrite.take(); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 3607 | TheCall->setCallee(Fn); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 3608 | goto retry; |
| 3609 | } |
| 3610 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3611 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 3612 | << Fn->getType() << Fn->getSourceRange()); |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 3613 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3614 | |
Peter Collingbourne | 0423fc6 | 2011-02-23 01:53:29 +0000 | [diff] [blame] | 3615 | if (getLangOptions().CUDA) { |
| 3616 | if (Config) { |
| 3617 | // CUDA: Kernel calls must be to global functions |
| 3618 | if (FDecl && !FDecl->hasAttr<CUDAGlobalAttr>()) |
| 3619 | return ExprError(Diag(LParenLoc,diag::err_kern_call_not_global_function) |
| 3620 | << FDecl->getName() << Fn->getSourceRange()); |
| 3621 | |
| 3622 | // CUDA: Kernel function must have 'void' return type |
| 3623 | if (!FuncT->getResultType()->isVoidType()) |
| 3624 | return ExprError(Diag(LParenLoc, diag::err_kern_type_not_void_return) |
| 3625 | << Fn->getType() << Fn->getSourceRange()); |
| 3626 | } |
| 3627 | } |
| 3628 | |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3629 | // Check for a valid return type |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3630 | if (CheckCallReturnType(FuncT->getResultType(), |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3631 | Fn->getSourceRange().getBegin(), TheCall, |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 3632 | FDecl)) |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3633 | return ExprError(); |
| 3634 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3635 | // We know the result type of the call, set it. |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 3636 | TheCall->setType(FuncT->getCallResultType(Context)); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3637 | TheCall->setValueKind(Expr::getValueKindForType(FuncT->getResultType())); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3638 | |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3639 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FuncT)) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3640 | if (ConvertArgumentsForCall(TheCall, Fn, FDecl, Proto, Args, NumArgs, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3641 | RParenLoc)) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3642 | return ExprError(); |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3643 | } else { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3644 | assert(isa<FunctionNoProtoType>(FuncT) && "Unknown FunctionType!"); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3645 | |
Douglas Gregor | 74734d5 | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 3646 | if (FDecl) { |
| 3647 | // Check if we have too few/too many template arguments, based |
| 3648 | // on our knowledge of the function definition. |
| 3649 | const FunctionDecl *Def = 0; |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 3650 | if (FDecl->hasBody(Def) && NumArgs != Def->param_size()) { |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 3651 | const FunctionProtoType *Proto |
| 3652 | = Def->getType()->getAs<FunctionProtoType>(); |
| 3653 | if (!Proto || !(Proto->isVariadic() && NumArgs >= Def->param_size())) |
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 3654 | Diag(RParenLoc, diag::warn_call_wrong_number_of_arguments) |
| 3655 | << (NumArgs > Def->param_size()) << FDecl << Fn->getSourceRange(); |
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 3656 | } |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 3657 | |
| 3658 | // If the function we're calling isn't a function prototype, but we have |
| 3659 | // a function prototype from a prior declaratiom, use that prototype. |
| 3660 | if (!FDecl->hasPrototype()) |
| 3661 | Proto = FDecl->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | 74734d5 | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 3662 | } |
| 3663 | |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 3664 | // Promote the arguments (C99 6.5.2.2p6). |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3665 | for (unsigned i = 0; i != NumArgs; i++) { |
| 3666 | Expr *Arg = Args[i]; |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 3667 | |
| 3668 | if (Proto && i < Proto->getNumArgs()) { |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 3669 | InitializedEntity Entity |
| 3670 | = InitializedEntity::InitializeParameter(Context, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3671 | Proto->getArgType(i), |
| 3672 | Proto->isArgConsumed(i)); |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 3673 | ExprResult ArgE = PerformCopyInitialization(Entity, |
| 3674 | SourceLocation(), |
| 3675 | Owned(Arg)); |
| 3676 | if (ArgE.isInvalid()) |
| 3677 | return true; |
| 3678 | |
| 3679 | Arg = ArgE.takeAs<Expr>(); |
| 3680 | |
| 3681 | } else { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3682 | ExprResult ArgE = DefaultArgumentPromotion(Arg); |
| 3683 | |
| 3684 | if (ArgE.isInvalid()) |
| 3685 | return true; |
| 3686 | |
| 3687 | Arg = ArgE.takeAs<Expr>(); |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 3688 | } |
| 3689 | |
Douglas Gregor | 0700bbf | 2010-10-26 05:45:40 +0000 | [diff] [blame] | 3690 | if (RequireCompleteType(Arg->getSourceRange().getBegin(), |
| 3691 | Arg->getType(), |
| 3692 | PDiag(diag::err_call_incomplete_argument) |
| 3693 | << Arg->getSourceRange())) |
| 3694 | return ExprError(); |
| 3695 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3696 | TheCall->setArg(i, Arg); |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 3697 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3698 | } |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3699 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3700 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(FDecl)) |
| 3701 | if (!Method->isStatic()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3702 | return ExprError(Diag(LParenLoc, diag::err_member_call_without_object) |
| 3703 | << Fn->getSourceRange()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3704 | |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 3705 | // Check for sentinels |
| 3706 | if (NDecl) |
| 3707 | DiagnoseSentinelCalls(NDecl, LParenLoc, Args, NumArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3708 | |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 3709 | // Do special checking on direct calls to functions. |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 3710 | if (FDecl) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3711 | if (CheckFunctionCall(FDecl, TheCall)) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 3712 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3713 | |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 3714 | if (BuiltinID) |
Fariborz Jahanian | 67aba81 | 2010-11-30 17:35:24 +0000 | [diff] [blame] | 3715 | return CheckBuiltinFunctionCall(BuiltinID, TheCall); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 3716 | } else if (NDecl) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3717 | if (CheckBlockCall(NDecl, TheCall)) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 3718 | return ExprError(); |
| 3719 | } |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 3720 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3721 | return MaybeBindToTemporary(TheCall); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3722 | } |
| 3723 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3724 | ExprResult |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3725 | Sema::ActOnCompoundLiteral(SourceLocation LParenLoc, ParsedType Ty, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3726 | SourceLocation RParenLoc, Expr *InitExpr) { |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3727 | assert((Ty != 0) && "ActOnCompoundLiteral(): missing type"); |
Steve Naroff | aff1edd | 2007-07-19 21:32:11 +0000 | [diff] [blame] | 3728 | // FIXME: put back this assert when initializers are worked out. |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3729 | //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression"); |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 3730 | |
| 3731 | TypeSourceInfo *TInfo; |
| 3732 | QualType literalType = GetTypeFromParser(Ty, &TInfo); |
| 3733 | if (!TInfo) |
| 3734 | TInfo = Context.getTrivialTypeSourceInfo(literalType); |
| 3735 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3736 | return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, InitExpr); |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 3737 | } |
| 3738 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3739 | ExprResult |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 3740 | Sema::BuildCompoundLiteralExpr(SourceLocation LParenLoc, TypeSourceInfo *TInfo, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3741 | SourceLocation RParenLoc, Expr *literalExpr) { |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 3742 | QualType literalType = TInfo->getType(); |
Anders Carlsson | d35c832 | 2007-12-05 07:24:19 +0000 | [diff] [blame] | 3743 | |
Eli Friedman | 6223c22 | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 3744 | if (literalType->isArrayType()) { |
Argyrios Kyrtzidis | e6fe9a2 | 2010-11-08 19:14:19 +0000 | [diff] [blame] | 3745 | if (RequireCompleteType(LParenLoc, Context.getBaseElementType(literalType), |
| 3746 | PDiag(diag::err_illegal_decl_array_incomplete_type) |
| 3747 | << SourceRange(LParenLoc, |
| 3748 | literalExpr->getSourceRange().getEnd()))) |
| 3749 | return ExprError(); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 3750 | if (literalType->isVariableArrayType()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3751 | return ExprError(Diag(LParenLoc, diag::err_variable_object_no_init) |
| 3752 | << SourceRange(LParenLoc, literalExpr->getSourceRange().getEnd())); |
Douglas Gregor | 690dc7f | 2009-05-21 23:48:18 +0000 | [diff] [blame] | 3753 | } else if (!literalType->isDependentType() && |
| 3754 | RequireCompleteType(LParenLoc, literalType, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3755 | PDiag(diag::err_typecheck_decl_incomplete_type) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3756 | << SourceRange(LParenLoc, |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 3757 | literalExpr->getSourceRange().getEnd()))) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3758 | return ExprError(); |
Eli Friedman | 6223c22 | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 3759 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3760 | InitializedEntity Entity |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 3761 | = InitializedEntity::InitializeTemporary(literalType); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 3762 | InitializationKind Kind |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3763 | = InitializationKind::CreateCStyleCast(LParenLoc, |
| 3764 | SourceRange(LParenLoc, RParenLoc)); |
Eli Friedman | 0854462 | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 3765 | InitializationSequence InitSeq(*this, Entity, Kind, &literalExpr, 1); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3766 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, |
John McCall | ca0408f | 2010-08-23 06:44:23 +0000 | [diff] [blame] | 3767 | MultiExprArg(*this, &literalExpr, 1), |
Eli Friedman | 0854462 | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 3768 | &literalType); |
| 3769 | if (Result.isInvalid()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3770 | return ExprError(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3771 | literalExpr = Result.get(); |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 3772 | |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 3773 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 3774 | if (isFileScope) { // 6.5.2.5p3 |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 3775 | if (CheckForConstantInitializer(literalExpr, literalType)) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3776 | return ExprError(); |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 3777 | } |
Eli Friedman | 0854462 | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 3778 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3779 | // In C, compound literals are l-values for some reason. |
| 3780 | ExprValueKind VK = getLangOptions().CPlusPlus ? VK_RValue : VK_LValue; |
| 3781 | |
Douglas Gregor | 751ec9b | 2011-06-17 04:59:12 +0000 | [diff] [blame] | 3782 | return MaybeBindToTemporary( |
| 3783 | new (Context) CompoundLiteralExpr(LParenLoc, TInfo, literalType, |
| 3784 | VK, literalExpr, isFileScope)); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 3785 | } |
| 3786 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3787 | ExprResult |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3788 | Sema::ActOnInitList(SourceLocation LBraceLoc, MultiExprArg initlist, |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3789 | SourceLocation RBraceLoc) { |
| 3790 | unsigned NumInit = initlist.size(); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3791 | Expr **InitList = initlist.release(); |
Anders Carlsson | 66b5a8a | 2007-08-31 04:56:16 +0000 | [diff] [blame] | 3792 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 3793 | // Semantic analysis for initializers is done by ActOnDeclarator() and |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3794 | // CheckInitializer() - it requires knowledge of the object being intialized. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3795 | |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 3796 | InitListExpr *E = new (Context) InitListExpr(Context, LBraceLoc, InitList, |
| 3797 | NumInit, RBraceLoc); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 3798 | E->setType(Context.VoidTy); // FIXME: just a place holder for now. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 3799 | return Owned(E); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 3800 | } |
| 3801 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3802 | /// Prepares for a scalar cast, performing all the necessary stages |
| 3803 | /// except the final cast and returning the kind required. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3804 | static CastKind PrepareScalarCast(Sema &S, ExprResult &Src, QualType DestTy) { |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3805 | // Both Src and Dest are scalar types, i.e. arithmetic or pointer. |
| 3806 | // Also, callers should have filtered out the invalid cases with |
| 3807 | // pointers. Everything else should be possible. |
| 3808 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3809 | QualType SrcTy = Src.get()->getType(); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3810 | if (S.Context.hasSameUnqualifiedType(SrcTy, DestTy)) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3811 | return CK_NoOp; |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3812 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3813 | switch (SrcTy->getScalarTypeKind()) { |
| 3814 | case Type::STK_MemberPointer: |
| 3815 | llvm_unreachable("member pointer type in C"); |
Abramo Bagnara | bb03f5d | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 3816 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3817 | case Type::STK_Pointer: |
| 3818 | switch (DestTy->getScalarTypeKind()) { |
| 3819 | case Type::STK_Pointer: |
| 3820 | return DestTy->isObjCObjectPointerType() ? |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3821 | CK_AnyPointerToObjCPointerCast : |
| 3822 | CK_BitCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3823 | case Type::STK_Bool: |
| 3824 | return CK_PointerToBoolean; |
| 3825 | case Type::STK_Integral: |
| 3826 | return CK_PointerToIntegral; |
| 3827 | case Type::STK_Floating: |
| 3828 | case Type::STK_FloatingComplex: |
| 3829 | case Type::STK_IntegralComplex: |
| 3830 | case Type::STK_MemberPointer: |
| 3831 | llvm_unreachable("illegal cast from pointer"); |
| 3832 | } |
| 3833 | break; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3834 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3835 | case Type::STK_Bool: // casting from bool is like casting from an integer |
| 3836 | case Type::STK_Integral: |
| 3837 | switch (DestTy->getScalarTypeKind()) { |
| 3838 | case Type::STK_Pointer: |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3839 | if (Src.get()->isNullPointerConstant(S.Context, |
| 3840 | Expr::NPC_ValueDependentIsNull)) |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 3841 | return CK_NullToPointer; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3842 | return CK_IntegralToPointer; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3843 | case Type::STK_Bool: |
| 3844 | return CK_IntegralToBoolean; |
| 3845 | case Type::STK_Integral: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3846 | return CK_IntegralCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3847 | case Type::STK_Floating: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3848 | return CK_IntegralToFloating; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3849 | case Type::STK_IntegralComplex: |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3850 | Src = S.ImpCastExprToType(Src.take(), |
| 3851 | DestTy->getAs<ComplexType>()->getElementType(), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3852 | CK_IntegralCast); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3853 | return CK_IntegralRealToComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3854 | case Type::STK_FloatingComplex: |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3855 | Src = S.ImpCastExprToType(Src.take(), |
| 3856 | DestTy->getAs<ComplexType>()->getElementType(), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3857 | CK_IntegralToFloating); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3858 | return CK_FloatingRealToComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3859 | case Type::STK_MemberPointer: |
| 3860 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3861 | } |
| 3862 | break; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3863 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3864 | case Type::STK_Floating: |
| 3865 | switch (DestTy->getScalarTypeKind()) { |
| 3866 | case Type::STK_Floating: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3867 | return CK_FloatingCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3868 | case Type::STK_Bool: |
| 3869 | return CK_FloatingToBoolean; |
| 3870 | case Type::STK_Integral: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3871 | return CK_FloatingToIntegral; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3872 | case Type::STK_FloatingComplex: |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3873 | Src = S.ImpCastExprToType(Src.take(), |
| 3874 | DestTy->getAs<ComplexType>()->getElementType(), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3875 | CK_FloatingCast); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3876 | return CK_FloatingRealToComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3877 | case Type::STK_IntegralComplex: |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3878 | Src = S.ImpCastExprToType(Src.take(), |
| 3879 | DestTy->getAs<ComplexType>()->getElementType(), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3880 | CK_FloatingToIntegral); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3881 | return CK_IntegralRealToComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3882 | case Type::STK_Pointer: |
| 3883 | llvm_unreachable("valid float->pointer cast?"); |
| 3884 | case Type::STK_MemberPointer: |
| 3885 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3886 | } |
| 3887 | break; |
| 3888 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3889 | case Type::STK_FloatingComplex: |
| 3890 | switch (DestTy->getScalarTypeKind()) { |
| 3891 | case Type::STK_FloatingComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3892 | return CK_FloatingComplexCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3893 | case Type::STK_IntegralComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3894 | return CK_FloatingComplexToIntegralComplex; |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3895 | case Type::STK_Floating: { |
Abramo Bagnara | bb03f5d | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 3896 | QualType ET = SrcTy->getAs<ComplexType>()->getElementType(); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3897 | if (S.Context.hasSameType(ET, DestTy)) |
| 3898 | return CK_FloatingComplexToReal; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3899 | Src = S.ImpCastExprToType(Src.take(), ET, CK_FloatingComplexToReal); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3900 | return CK_FloatingCast; |
| 3901 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3902 | case Type::STK_Bool: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3903 | return CK_FloatingComplexToBoolean; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3904 | case Type::STK_Integral: |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3905 | Src = S.ImpCastExprToType(Src.take(), |
| 3906 | SrcTy->getAs<ComplexType>()->getElementType(), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3907 | CK_FloatingComplexToReal); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3908 | return CK_FloatingToIntegral; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3909 | case Type::STK_Pointer: |
| 3910 | llvm_unreachable("valid complex float->pointer cast?"); |
| 3911 | case Type::STK_MemberPointer: |
| 3912 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3913 | } |
| 3914 | break; |
| 3915 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3916 | case Type::STK_IntegralComplex: |
| 3917 | switch (DestTy->getScalarTypeKind()) { |
| 3918 | case Type::STK_FloatingComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3919 | return CK_IntegralComplexToFloatingComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3920 | case Type::STK_IntegralComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3921 | return CK_IntegralComplexCast; |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3922 | case Type::STK_Integral: { |
Abramo Bagnara | bb03f5d | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 3923 | QualType ET = SrcTy->getAs<ComplexType>()->getElementType(); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3924 | if (S.Context.hasSameType(ET, DestTy)) |
| 3925 | return CK_IntegralComplexToReal; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3926 | Src = S.ImpCastExprToType(Src.take(), ET, CK_IntegralComplexToReal); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 3927 | return CK_IntegralCast; |
| 3928 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3929 | case Type::STK_Bool: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3930 | return CK_IntegralComplexToBoolean; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3931 | case Type::STK_Floating: |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3932 | Src = S.ImpCastExprToType(Src.take(), |
| 3933 | SrcTy->getAs<ComplexType>()->getElementType(), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3934 | CK_IntegralComplexToReal); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3935 | return CK_IntegralToFloating; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 3936 | case Type::STK_Pointer: |
| 3937 | llvm_unreachable("valid complex int->pointer cast?"); |
| 3938 | case Type::STK_MemberPointer: |
| 3939 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3940 | } |
| 3941 | break; |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3942 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3943 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 3944 | llvm_unreachable("Unhandled scalar cast"); |
| 3945 | return CK_BitCast; |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 3946 | } |
| 3947 | |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3948 | /// CheckCastTypes - Check type constraints for casting between types. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3949 | ExprResult Sema::CheckCastTypes(SourceLocation CastStartLoc, SourceRange TyR, |
| 3950 | QualType castType, Expr *castExpr, |
| 3951 | CastKind& Kind, ExprValueKind &VK, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3952 | CXXCastPath &BasePath, bool FunctionalStyle) { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 3953 | if (castExpr->getType() == Context.UnknownAnyTy) |
| 3954 | return checkUnknownAnyCast(TyR, castType, castExpr, Kind, VK, BasePath); |
| 3955 | |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 3956 | if (getLangOptions().CPlusPlus) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3957 | return CXXCheckCStyleCast(SourceRange(CastStartLoc, |
Douglas Gregor | 40749ee | 2010-11-03 00:35:38 +0000 | [diff] [blame] | 3958 | castExpr->getLocEnd()), |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3959 | castType, VK, castExpr, Kind, BasePath, |
Anders Carlsson | 5cf86ba | 2010-04-24 19:06:50 +0000 | [diff] [blame] | 3960 | FunctionalStyle); |
Sebastian Redl | 9cc11e7 | 2009-07-25 15:41:38 +0000 | [diff] [blame] | 3961 | |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 3962 | assert(!castExpr->getType()->isPlaceholderType()); |
| 3963 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3964 | // We only support r-value casts in C. |
| 3965 | VK = VK_RValue; |
| 3966 | |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3967 | // C99 6.5.4p2: the cast type needs to be void or scalar and the expression |
| 3968 | // type needs to be scalar. |
| 3969 | if (castType->isVoidType()) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3970 | // We don't necessarily do lvalue-to-rvalue conversions on this. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3971 | ExprResult castExprRes = IgnoredValueConversions(castExpr); |
| 3972 | if (castExprRes.isInvalid()) |
| 3973 | return ExprError(); |
| 3974 | castExpr = castExprRes.take(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3975 | |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 3976 | // Cast to void allows any expr type. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3977 | Kind = CK_ToVoid; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3978 | return Owned(castExpr); |
Anders Carlsson | ebeaf20 | 2009-10-16 02:35:04 +0000 | [diff] [blame] | 3979 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3980 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3981 | ExprResult castExprRes = DefaultFunctionArrayLvalueConversion(castExpr); |
| 3982 | if (castExprRes.isInvalid()) |
| 3983 | return ExprError(); |
| 3984 | castExpr = castExprRes.take(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3985 | |
Eli Friedman | 8d43808 | 2010-07-17 20:43:49 +0000 | [diff] [blame] | 3986 | if (RequireCompleteType(TyR.getBegin(), castType, |
| 3987 | diag::err_typecheck_cast_to_incomplete)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3988 | return ExprError(); |
Eli Friedman | 8d43808 | 2010-07-17 20:43:49 +0000 | [diff] [blame] | 3989 | |
Anders Carlsson | ebeaf20 | 2009-10-16 02:35:04 +0000 | [diff] [blame] | 3990 | if (!castType->isScalarType() && !castType->isVectorType()) { |
Douglas Gregor | a4923eb | 2009-11-16 21:35:15 +0000 | [diff] [blame] | 3991 | if (Context.hasSameUnqualifiedType(castType, castExpr->getType()) && |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3992 | (castType->isStructureType() || castType->isUnionType())) { |
| 3993 | // GCC struct/union extension: allow cast to self. |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 3994 | // FIXME: Check that the cast destination type is complete. |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 3995 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_nonscalar) |
| 3996 | << castType << castExpr->getSourceRange(); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3997 | Kind = CK_NoOp; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3998 | return Owned(castExpr); |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 3999 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4000 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4001 | if (castType->isUnionType()) { |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 4002 | // GCC cast to union extension |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4003 | RecordDecl *RD = castType->getAs<RecordType>()->getDecl(); |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 4004 | RecordDecl::field_iterator Field, FieldEnd; |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 4005 | for (Field = RD->field_begin(), FieldEnd = RD->field_end(); |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 4006 | Field != FieldEnd; ++Field) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4007 | if (Context.hasSameUnqualifiedType(Field->getType(), |
Abramo Bagnara | 8c4bfe5 | 2010-10-07 21:20:44 +0000 | [diff] [blame] | 4008 | castExpr->getType()) && |
| 4009 | !Field->isUnnamedBitfield()) { |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 4010 | Diag(TyR.getBegin(), diag::ext_typecheck_cast_to_union) |
| 4011 | << castExpr->getSourceRange(); |
| 4012 | break; |
| 4013 | } |
| 4014 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4015 | if (Field == FieldEnd) { |
| 4016 | Diag(TyR.getBegin(), diag::err_typecheck_cast_to_union_no_type) |
Seo Sanghyeon | eff2cd5 | 2009-01-15 04:51:39 +0000 | [diff] [blame] | 4017 | << castExpr->getType() << castExpr->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4018 | return ExprError(); |
| 4019 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4020 | Kind = CK_ToUnion; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4021 | return Owned(castExpr); |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 4022 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4023 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4024 | // Reject any other conversions to non-scalar types. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4025 | Diag(TyR.getBegin(), diag::err_typecheck_cond_expect_scalar) |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4026 | << castType << castExpr->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4027 | return ExprError(); |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4028 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4029 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4030 | // The type we're casting to is known to be a scalar or vector. |
| 4031 | |
| 4032 | // Require the operand to be a scalar or vector. |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4033 | if (!castExpr->getType()->isScalarType() && |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4034 | !castExpr->getType()->isVectorType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4035 | Diag(castExpr->getLocStart(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4036 | diag::err_typecheck_expect_scalar_operand) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4037 | << castExpr->getType() << castExpr->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4038 | return ExprError(); |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4039 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4040 | |
| 4041 | if (castType->isExtVectorType()) |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 4042 | return CheckExtVectorCast(TyR, castType, castExpr, Kind); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4043 | |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 4044 | if (castType->isVectorType()) { |
| 4045 | if (castType->getAs<VectorType>()->getVectorKind() == |
| 4046 | VectorType::AltiVecVector && |
| 4047 | (castExpr->getType()->isIntegerType() || |
| 4048 | castExpr->getType()->isFloatingType())) { |
| 4049 | Kind = CK_VectorSplat; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4050 | return Owned(castExpr); |
| 4051 | } else if (CheckVectorCast(TyR, castType, castExpr->getType(), Kind)) { |
| 4052 | return ExprError(); |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 4053 | } else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4054 | return Owned(castExpr); |
Anton Yartsev | d06fea8 | 2011-03-27 09:32:40 +0000 | [diff] [blame] | 4055 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4056 | if (castExpr->getType()->isVectorType()) { |
| 4057 | if (CheckVectorCast(TyR, castExpr->getType(), castType, Kind)) |
| 4058 | return ExprError(); |
| 4059 | else |
| 4060 | return Owned(castExpr); |
| 4061 | } |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4062 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4063 | // The source and target types are both scalars, i.e. |
| 4064 | // - arithmetic types (fundamental, enum, and complex) |
| 4065 | // - all kinds of pointers |
| 4066 | // Note that member pointers were filtered out with C++, above. |
| 4067 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4068 | if (isa<ObjCSelectorExpr>(castExpr)) { |
| 4069 | Diag(castExpr->getLocStart(), diag::err_cast_selector_expr); |
| 4070 | return ExprError(); |
| 4071 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4072 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4073 | // If either type is a pointer, the other type has to be either an |
| 4074 | // integer or a pointer. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4075 | QualType castExprType = castExpr->getType(); |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4076 | if (!castType->isArithmeticType()) { |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 4077 | if (!castExprType->isIntegralType(Context) && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4078 | castExprType->isArithmeticType()) { |
| 4079 | Diag(castExpr->getLocStart(), |
| 4080 | diag::err_cast_pointer_from_non_pointer_int) |
Eli Friedman | 41826bb | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 4081 | << castExprType << castExpr->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4082 | return ExprError(); |
| 4083 | } |
Eli Friedman | 41826bb | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 4084 | } else if (!castExpr->getType()->isArithmeticType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4085 | if (!castType->isIntegralType(Context) && castType->isArithmeticType()) { |
| 4086 | Diag(castExpr->getLocStart(), diag::err_cast_pointer_to_non_pointer_int) |
Eli Friedman | 41826bb | 2009-05-01 02:23:58 +0000 | [diff] [blame] | 4087 | << castType << castExpr->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4088 | return ExprError(); |
| 4089 | } |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 4090 | } |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 4091 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4092 | if (getLangOptions().ObjCAutoRefCount) { |
| 4093 | // Diagnose problems with Objective-C casts involving lifetime qualifiers. |
| 4094 | CheckObjCARCConversion(SourceRange(CastStartLoc, castExpr->getLocEnd()), |
| 4095 | castType, castExpr, CCK_CStyleCast); |
| 4096 | |
| 4097 | if (const PointerType *CastPtr = castType->getAs<PointerType>()) { |
| 4098 | if (const PointerType *ExprPtr = castExprType->getAs<PointerType>()) { |
| 4099 | Qualifiers CastQuals = CastPtr->getPointeeType().getQualifiers(); |
| 4100 | Qualifiers ExprQuals = ExprPtr->getPointeeType().getQualifiers(); |
| 4101 | if (CastPtr->getPointeeType()->isObjCLifetimeType() && |
| 4102 | ExprPtr->getPointeeType()->isObjCLifetimeType() && |
| 4103 | !CastQuals.compatiblyIncludesObjCLifetime(ExprQuals)) { |
| 4104 | Diag(castExpr->getLocStart(), |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 4105 | diag::err_typecheck_incompatible_ownership) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4106 | << castExprType << castType << AA_Casting |
| 4107 | << castExpr->getSourceRange(); |
| 4108 | |
| 4109 | return ExprError(); |
| 4110 | } |
| 4111 | } |
Fariborz Jahanian | 04e5a25 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 4112 | } |
Fariborz Jahanian | 7a084ec | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 4113 | else if (!CheckObjCARCUnavailableWeakConversion(castType, castExprType)) { |
| 4114 | Diag(castExpr->getLocStart(), |
Fariborz Jahanian | 82007c3 | 2011-07-08 17:41:42 +0000 | [diff] [blame] | 4115 | diag::err_arc_convesion_of_weak_unavailable) << 1 |
Fariborz Jahanian | 04e5a25 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 4116 | << castExprType << castType |
| 4117 | << castExpr->getSourceRange(); |
Fariborz Jahanian | 7a084ec | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 4118 | return ExprError(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4119 | } |
| 4120 | } |
| 4121 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4122 | castExprRes = Owned(castExpr); |
| 4123 | Kind = PrepareScalarCast(*this, castExprRes, castType); |
| 4124 | if (castExprRes.isInvalid()) |
| 4125 | return ExprError(); |
| 4126 | castExpr = castExprRes.take(); |
John McCall | b7f4ffe | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 4127 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4128 | if (Kind == CK_BitCast) |
John McCall | b7f4ffe | 2010-08-12 21:44:57 +0000 | [diff] [blame] | 4129 | CheckCastAlign(castExpr, castType, TyR); |
| 4130 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4131 | return Owned(castExpr); |
Argyrios Kyrtzidis | 6c2dc4d | 2008-08-16 20:27:34 +0000 | [diff] [blame] | 4132 | } |
| 4133 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4134 | bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4135 | CastKind &Kind) { |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4136 | assert(VectorTy->isVectorType() && "Not a vector type!"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4137 | |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4138 | if (Ty->isVectorType() || Ty->isIntegerType()) { |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 4139 | if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty)) |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4140 | return Diag(R.getBegin(), |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4141 | Ty->isVectorType() ? |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4142 | diag::err_invalid_conversion_between_vectors : |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4143 | diag::err_invalid_conversion_between_vector_and_integer) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4144 | << VectorTy << Ty << R; |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4145 | } else |
| 4146 | return Diag(R.getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4147 | diag::err_invalid_conversion_between_vector_and_scalar) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4148 | << VectorTy << Ty << R; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4149 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4150 | Kind = CK_BitCast; |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4151 | return false; |
| 4152 | } |
| 4153 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4154 | ExprResult Sema::CheckExtVectorCast(SourceRange R, QualType DestTy, |
| 4155 | Expr *CastExpr, CastKind &Kind) { |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4156 | assert(DestTy->isExtVectorType() && "Not an extended vector type!"); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4157 | |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 4158 | QualType SrcTy = CastExpr->getType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4159 | |
Nate Begeman | 9b10da6 | 2009-06-27 22:05:55 +0000 | [diff] [blame] | 4160 | // If SrcTy is a VectorType, the total size must match to explicitly cast to |
| 4161 | // an ExtVectorType. |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4162 | if (SrcTy->isVectorType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4163 | if (Context.getTypeSize(DestTy) != Context.getTypeSize(SrcTy)) { |
| 4164 | Diag(R.getBegin(),diag::err_invalid_conversion_between_ext_vectors) |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4165 | << DestTy << SrcTy << R; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4166 | return ExprError(); |
| 4167 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4168 | Kind = CK_BitCast; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4169 | return Owned(CastExpr); |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4170 | } |
| 4171 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4172 | // All non-pointer scalars can be cast to ExtVector type. The appropriate |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4173 | // conversion will take place first from scalar to elt type, and then |
| 4174 | // splat from elt type to vector. |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4175 | if (SrcTy->isPointerType()) |
| 4176 | return Diag(R.getBegin(), |
| 4177 | diag::err_invalid_conversion_between_vector_and_scalar) |
| 4178 | << DestTy << SrcTy << R; |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4179 | |
| 4180 | QualType DestElemTy = DestTy->getAs<ExtVectorType>()->getElementType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4181 | ExprResult CastExprRes = Owned(CastExpr); |
| 4182 | CastKind CK = PrepareScalarCast(*this, CastExprRes, DestElemTy); |
| 4183 | if (CastExprRes.isInvalid()) |
| 4184 | return ExprError(); |
| 4185 | CastExpr = ImpCastExprToType(CastExprRes.take(), DestElemTy, CK).take(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4186 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4187 | Kind = CK_VectorSplat; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4188 | return Owned(CastExpr); |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4189 | } |
| 4190 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4191 | ExprResult |
Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 4192 | Sema::ActOnCastExpr(Scope *S, SourceLocation LParenLoc, |
| 4193 | Declarator &D, ParsedType &Ty, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4194 | SourceLocation RParenLoc, Expr *castExpr) { |
Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 4195 | assert(!D.isInvalidType() && (castExpr != 0) && |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4196 | "ActOnCastExpr(): missing type or expr"); |
Steve Naroff | 16beff8 | 2007-07-16 23:25:18 +0000 | [diff] [blame] | 4197 | |
Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 4198 | TypeSourceInfo *castTInfo = GetTypeForDeclaratorCast(D, castExpr->getType()); |
| 4199 | if (D.isInvalidType()) |
| 4200 | return ExprError(); |
| 4201 | |
| 4202 | if (getLangOptions().CPlusPlus) { |
| 4203 | // Check that there are no default arguments (C++ only). |
| 4204 | CheckExtraCXXDefaultArguments(D); |
| 4205 | } |
| 4206 | |
| 4207 | QualType castType = castTInfo->getType(); |
| 4208 | Ty = CreateParsedType(castType, castTInfo); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4209 | |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4210 | bool isVectorLiteral = false; |
| 4211 | |
| 4212 | // Check for an altivec or OpenCL literal, |
| 4213 | // i.e. all the elements are integer constants. |
| 4214 | ParenExpr *PE = dyn_cast<ParenExpr>(castExpr); |
| 4215 | ParenListExpr *PLE = dyn_cast<ParenListExpr>(castExpr); |
| 4216 | if (getLangOptions().AltiVec && castType->isVectorType() && (PE || PLE)) { |
| 4217 | if (PLE && PLE->getNumExprs() == 0) { |
| 4218 | Diag(PLE->getExprLoc(), diag::err_altivec_empty_initializer); |
| 4219 | return ExprError(); |
| 4220 | } |
| 4221 | if (PE || PLE->getNumExprs() == 1) { |
| 4222 | Expr *E = (PE ? PE->getSubExpr() : PLE->getExpr(0)); |
| 4223 | if (!E->getType()->isVectorType()) |
| 4224 | isVectorLiteral = true; |
| 4225 | } |
| 4226 | else |
| 4227 | isVectorLiteral = true; |
| 4228 | } |
| 4229 | |
| 4230 | // If this is a vector initializer, '(' type ')' '(' init, ..., init ')' |
| 4231 | // then handle it as such. |
| 4232 | if (isVectorLiteral) |
| 4233 | return BuildVectorLiteral(LParenLoc, RParenLoc, castExpr, castTInfo); |
| 4234 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4235 | // If the Expr being casted is a ParenListExpr, handle it specially. |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4236 | // This is not an AltiVec-style cast, so turn the ParenListExpr into a |
| 4237 | // sequence of BinOp comma operators. |
| 4238 | if (isa<ParenListExpr>(castExpr)) { |
| 4239 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, castExpr); |
| 4240 | if (Result.isInvalid()) return ExprError(); |
| 4241 | castExpr = Result.take(); |
| 4242 | } |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 4243 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4244 | return BuildCStyleCastExpr(LParenLoc, castTInfo, RParenLoc, castExpr); |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 4245 | } |
| 4246 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4247 | ExprResult |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 4248 | Sema::BuildCStyleCastExpr(SourceLocation LParenLoc, TypeSourceInfo *Ty, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4249 | SourceLocation RParenLoc, Expr *castExpr) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4250 | CastKind Kind = CK_Invalid; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4251 | ExprValueKind VK = VK_RValue; |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 4252 | CXXCastPath BasePath; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4253 | ExprResult CastResult = |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4254 | CheckCastTypes(LParenLoc, SourceRange(LParenLoc, RParenLoc), Ty->getType(), |
| 4255 | castExpr, Kind, VK, BasePath); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4256 | if (CastResult.isInvalid()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4257 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4258 | castExpr = CastResult.take(); |
Anders Carlsson | 0aebc81 | 2009-09-09 21:33:21 +0000 | [diff] [blame] | 4259 | |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4260 | return Owned(CStyleCastExpr::Create( |
| 4261 | Context, Ty->getType().getNonLValueExprType(Context), VK, Kind, castExpr, |
| 4262 | &BasePath, Ty, LParenLoc, RParenLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4263 | } |
| 4264 | |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4265 | ExprResult Sema::BuildVectorLiteral(SourceLocation LParenLoc, |
| 4266 | SourceLocation RParenLoc, Expr *E, |
| 4267 | TypeSourceInfo *TInfo) { |
| 4268 | assert((isa<ParenListExpr>(E) || isa<ParenExpr>(E)) && |
| 4269 | "Expected paren or paren list expression"); |
| 4270 | |
| 4271 | Expr **exprs; |
| 4272 | unsigned numExprs; |
| 4273 | Expr *subExpr; |
| 4274 | if (ParenListExpr *PE = dyn_cast<ParenListExpr>(E)) { |
| 4275 | exprs = PE->getExprs(); |
| 4276 | numExprs = PE->getNumExprs(); |
| 4277 | } else { |
| 4278 | subExpr = cast<ParenExpr>(E)->getSubExpr(); |
| 4279 | exprs = &subExpr; |
| 4280 | numExprs = 1; |
| 4281 | } |
| 4282 | |
| 4283 | QualType Ty = TInfo->getType(); |
| 4284 | assert(Ty->isVectorType() && "Expected vector type"); |
| 4285 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4286 | SmallVector<Expr *, 8> initExprs; |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4287 | const VectorType *VTy = Ty->getAs<VectorType>(); |
| 4288 | unsigned numElems = Ty->getAs<VectorType>()->getNumElements(); |
| 4289 | |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4290 | // '(...)' form of vector initialization in AltiVec: the number of |
| 4291 | // initializers must be one or must match the size of the vector. |
| 4292 | // If a single value is specified in the initializer then it will be |
| 4293 | // replicated to all the components of the vector |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4294 | if (VTy->getVectorKind() == VectorType::AltiVecVector) { |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4295 | // The number of initializers must be one or must match the size of the |
| 4296 | // vector. If a single value is specified in the initializer then it will |
| 4297 | // be replicated to all the components of the vector |
| 4298 | if (numExprs == 1) { |
| 4299 | QualType ElemTy = Ty->getAs<VectorType>()->getElementType(); |
| 4300 | ExprResult Literal = Owned(exprs[0]); |
| 4301 | Literal = ImpCastExprToType(Literal.take(), ElemTy, |
| 4302 | PrepareScalarCast(*this, Literal, ElemTy)); |
| 4303 | return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Literal.take()); |
| 4304 | } |
| 4305 | else if (numExprs < numElems) { |
| 4306 | Diag(E->getExprLoc(), |
| 4307 | diag::err_incorrect_number_of_vector_initializers); |
| 4308 | return ExprError(); |
| 4309 | } |
| 4310 | else |
| 4311 | for (unsigned i = 0, e = numExprs; i != e; ++i) |
| 4312 | initExprs.push_back(exprs[i]); |
| 4313 | } |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4314 | else { |
| 4315 | // For OpenCL, when the number of initializers is a single value, |
| 4316 | // it will be replicated to all components of the vector. |
| 4317 | if (getLangOptions().OpenCL && |
| 4318 | VTy->getVectorKind() == VectorType::GenericVector && |
| 4319 | numExprs == 1) { |
| 4320 | QualType ElemTy = Ty->getAs<VectorType>()->getElementType(); |
| 4321 | ExprResult Literal = Owned(exprs[0]); |
| 4322 | Literal = ImpCastExprToType(Literal.take(), ElemTy, |
| 4323 | PrepareScalarCast(*this, Literal, ElemTy)); |
| 4324 | return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Literal.take()); |
| 4325 | } |
| 4326 | |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4327 | for (unsigned i = 0, e = numExprs; i != e; ++i) |
| 4328 | initExprs.push_back(exprs[i]); |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4329 | } |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4330 | // FIXME: This means that pretty-printing the final AST will produce curly |
| 4331 | // braces instead of the original commas. |
| 4332 | InitListExpr *initE = new (Context) InitListExpr(Context, LParenLoc, |
| 4333 | &initExprs[0], |
| 4334 | initExprs.size(), RParenLoc); |
| 4335 | initE->setType(Ty); |
| 4336 | return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, initE); |
| 4337 | } |
| 4338 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4339 | /// This is not an AltiVec-style cast, so turn the ParenListExpr into a sequence |
| 4340 | /// of comma binary operators. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4341 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4342 | Sema::MaybeConvertParenListExprToParenExpr(Scope *S, Expr *expr) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4343 | ParenListExpr *E = dyn_cast<ParenListExpr>(expr); |
| 4344 | if (!E) |
| 4345 | return Owned(expr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4346 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4347 | ExprResult Result(E->getExpr(0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4348 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4349 | for (unsigned i = 1, e = E->getNumExprs(); i != e && !Result.isInvalid(); ++i) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4350 | Result = ActOnBinOp(S, E->getExprLoc(), tok::comma, Result.get(), |
| 4351 | E->getExpr(i)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4352 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4353 | if (Result.isInvalid()) return ExprError(); |
| 4354 | |
| 4355 | return ActOnParenExpr(E->getLParenLoc(), E->getRParenLoc(), Result.get()); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4356 | } |
| 4357 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4358 | ExprResult Sema::ActOnParenOrParenListExpr(SourceLocation L, |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4359 | SourceLocation R, |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4360 | MultiExprArg Val) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4361 | unsigned nexprs = Val.size(); |
| 4362 | Expr **exprs = reinterpret_cast<Expr**>(Val.release()); |
Fariborz Jahanian | f88f7ab | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 4363 | assert((exprs != 0) && "ActOnParenOrParenListExpr() missing expr list"); |
| 4364 | Expr *expr; |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4365 | if (nexprs == 1) |
Fariborz Jahanian | f88f7ab | 2009-11-25 01:26:41 +0000 | [diff] [blame] | 4366 | expr = new (Context) ParenExpr(L, R, exprs[0]); |
| 4367 | else |
Manuel Klimek | 0d9106f | 2011-06-22 20:02:16 +0000 | [diff] [blame] | 4368 | expr = new (Context) ParenListExpr(Context, L, exprs, nexprs, R, |
| 4369 | exprs[nexprs-1]->getType()); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4370 | return Owned(expr); |
| 4371 | } |
| 4372 | |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4373 | /// \brief Emit a specialized diagnostic when one expression is a null pointer |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4374 | /// constant and the other is not a pointer. Returns true if a diagnostic is |
| 4375 | /// emitted. |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4376 | bool Sema::DiagnoseConditionalForNull(Expr *LHS, Expr *RHS, |
| 4377 | SourceLocation QuestionLoc) { |
| 4378 | Expr *NullExpr = LHS; |
| 4379 | Expr *NonPointerExpr = RHS; |
| 4380 | Expr::NullPointerConstantKind NullKind = |
| 4381 | NullExpr->isNullPointerConstant(Context, |
| 4382 | Expr::NPC_ValueDependentIsNotNull); |
| 4383 | |
| 4384 | if (NullKind == Expr::NPCK_NotNull) { |
| 4385 | NullExpr = RHS; |
| 4386 | NonPointerExpr = LHS; |
| 4387 | NullKind = |
| 4388 | NullExpr->isNullPointerConstant(Context, |
| 4389 | Expr::NPC_ValueDependentIsNotNull); |
| 4390 | } |
| 4391 | |
| 4392 | if (NullKind == Expr::NPCK_NotNull) |
| 4393 | return false; |
| 4394 | |
| 4395 | if (NullKind == Expr::NPCK_ZeroInteger) { |
| 4396 | // In this case, check to make sure that we got here from a "NULL" |
| 4397 | // string in the source code. |
| 4398 | NullExpr = NullExpr->IgnoreParenImpCasts(); |
John McCall | 834e3f6 | 2011-03-08 07:59:04 +0000 | [diff] [blame] | 4399 | SourceLocation loc = NullExpr->getExprLoc(); |
| 4400 | if (!findMacroSpelling(loc, "NULL")) |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4401 | return false; |
| 4402 | } |
| 4403 | |
| 4404 | int DiagType = (NullKind == Expr::NPCK_CXX0X_nullptr); |
| 4405 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands_null) |
| 4406 | << NonPointerExpr->getType() << DiagType |
| 4407 | << NonPointerExpr->getSourceRange(); |
| 4408 | return true; |
| 4409 | } |
| 4410 | |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4411 | /// \brief Return false if the condition expression is valid, true otherwise. |
| 4412 | static bool checkCondition(Sema &S, Expr *Cond) { |
| 4413 | QualType CondTy = Cond->getType(); |
| 4414 | |
| 4415 | // C99 6.5.15p2 |
| 4416 | if (CondTy->isScalarType()) return false; |
| 4417 | |
| 4418 | // OpenCL: Sec 6.3.i says the condition is allowed to be a vector or scalar. |
| 4419 | if (S.getLangOptions().OpenCL && CondTy->isVectorType()) |
| 4420 | return false; |
| 4421 | |
| 4422 | // Emit the proper error message. |
| 4423 | S.Diag(Cond->getLocStart(), S.getLangOptions().OpenCL ? |
| 4424 | diag::err_typecheck_cond_expect_scalar : |
| 4425 | diag::err_typecheck_cond_expect_scalar_or_vector) |
| 4426 | << CondTy; |
| 4427 | return true; |
| 4428 | } |
| 4429 | |
| 4430 | /// \brief Return false if the two expressions can be converted to a vector, |
| 4431 | /// true otherwise |
| 4432 | static bool checkConditionalConvertScalarsToVectors(Sema &S, ExprResult &LHS, |
| 4433 | ExprResult &RHS, |
| 4434 | QualType CondTy) { |
| 4435 | // Both operands should be of scalar type. |
| 4436 | if (!LHS.get()->getType()->isScalarType()) { |
| 4437 | S.Diag(LHS.get()->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
| 4438 | << CondTy; |
| 4439 | return true; |
| 4440 | } |
| 4441 | if (!RHS.get()->getType()->isScalarType()) { |
| 4442 | S.Diag(RHS.get()->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
| 4443 | << CondTy; |
| 4444 | return true; |
| 4445 | } |
| 4446 | |
| 4447 | // Implicity convert these scalars to the type of the condition. |
| 4448 | LHS = S.ImpCastExprToType(LHS.take(), CondTy, CK_IntegralCast); |
| 4449 | RHS = S.ImpCastExprToType(RHS.take(), CondTy, CK_IntegralCast); |
| 4450 | return false; |
| 4451 | } |
| 4452 | |
| 4453 | /// \brief Handle when one or both operands are void type. |
| 4454 | static QualType checkConditionalVoidType(Sema &S, ExprResult &LHS, |
| 4455 | ExprResult &RHS) { |
| 4456 | Expr *LHSExpr = LHS.get(); |
| 4457 | Expr *RHSExpr = RHS.get(); |
| 4458 | |
| 4459 | if (!LHSExpr->getType()->isVoidType()) |
| 4460 | S.Diag(RHSExpr->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 4461 | << RHSExpr->getSourceRange(); |
| 4462 | if (!RHSExpr->getType()->isVoidType()) |
| 4463 | S.Diag(LHSExpr->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 4464 | << LHSExpr->getSourceRange(); |
| 4465 | LHS = S.ImpCastExprToType(LHS.take(), S.Context.VoidTy, CK_ToVoid); |
| 4466 | RHS = S.ImpCastExprToType(RHS.take(), S.Context.VoidTy, CK_ToVoid); |
| 4467 | return S.Context.VoidTy; |
| 4468 | } |
| 4469 | |
| 4470 | /// \brief Return false if the NullExpr can be promoted to PointerTy, |
| 4471 | /// true otherwise. |
| 4472 | static bool checkConditionalNullPointer(Sema &S, ExprResult &NullExpr, |
| 4473 | QualType PointerTy) { |
| 4474 | if ((!PointerTy->isAnyPointerType() && !PointerTy->isBlockPointerType()) || |
| 4475 | !NullExpr.get()->isNullPointerConstant(S.Context, |
| 4476 | Expr::NPC_ValueDependentIsNull)) |
| 4477 | return true; |
| 4478 | |
| 4479 | NullExpr = S.ImpCastExprToType(NullExpr.take(), PointerTy, CK_NullToPointer); |
| 4480 | return false; |
| 4481 | } |
| 4482 | |
| 4483 | /// \brief Checks compatibility between two pointers and return the resulting |
| 4484 | /// type. |
| 4485 | static QualType checkConditionalPointerCompatibility(Sema &S, ExprResult &LHS, |
| 4486 | ExprResult &RHS, |
| 4487 | SourceLocation Loc) { |
| 4488 | QualType LHSTy = LHS.get()->getType(); |
| 4489 | QualType RHSTy = RHS.get()->getType(); |
| 4490 | |
| 4491 | if (S.Context.hasSameType(LHSTy, RHSTy)) { |
| 4492 | // Two identical pointers types are always compatible. |
| 4493 | return LHSTy; |
| 4494 | } |
| 4495 | |
| 4496 | QualType lhptee, rhptee; |
| 4497 | |
| 4498 | // Get the pointee types. |
| 4499 | if (LHSTy->isBlockPointerType()) { |
| 4500 | lhptee = LHSTy->getAs<BlockPointerType>()->getPointeeType(); |
| 4501 | rhptee = RHSTy->getAs<BlockPointerType>()->getPointeeType(); |
| 4502 | } else { |
| 4503 | lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 4504 | rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
| 4505 | } |
| 4506 | |
| 4507 | if (!S.Context.typesAreCompatible(lhptee.getUnqualifiedType(), |
| 4508 | rhptee.getUnqualifiedType())) { |
| 4509 | S.Diag(Loc, diag::warn_typecheck_cond_incompatible_pointers) |
| 4510 | << LHSTy << RHSTy << LHS.get()->getSourceRange() |
| 4511 | << RHS.get()->getSourceRange(); |
| 4512 | // In this situation, we assume void* type. No especially good |
| 4513 | // reason, but this is what gcc does, and we do have to pick |
| 4514 | // to get a consistent AST. |
| 4515 | QualType incompatTy = S.Context.getPointerType(S.Context.VoidTy); |
| 4516 | LHS = S.ImpCastExprToType(LHS.take(), incompatTy, CK_BitCast); |
| 4517 | RHS = S.ImpCastExprToType(RHS.take(), incompatTy, CK_BitCast); |
| 4518 | return incompatTy; |
| 4519 | } |
| 4520 | |
| 4521 | // The pointer types are compatible. |
| 4522 | // C99 6.5.15p6: If both operands are pointers to compatible types *or* to |
| 4523 | // differently qualified versions of compatible types, the result type is |
| 4524 | // a pointer to an appropriately qualified version of the *composite* |
| 4525 | // type. |
| 4526 | // FIXME: Need to calculate the composite type. |
| 4527 | // FIXME: Need to add qualifiers |
| 4528 | |
| 4529 | LHS = S.ImpCastExprToType(LHS.take(), LHSTy, CK_BitCast); |
| 4530 | RHS = S.ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
| 4531 | return LHSTy; |
| 4532 | } |
| 4533 | |
| 4534 | /// \brief Return the resulting type when the operands are both block pointers. |
| 4535 | static QualType checkConditionalBlockPointerCompatibility(Sema &S, |
| 4536 | ExprResult &LHS, |
| 4537 | ExprResult &RHS, |
| 4538 | SourceLocation Loc) { |
| 4539 | QualType LHSTy = LHS.get()->getType(); |
| 4540 | QualType RHSTy = RHS.get()->getType(); |
| 4541 | |
| 4542 | if (!LHSTy->isBlockPointerType() || !RHSTy->isBlockPointerType()) { |
| 4543 | if (LHSTy->isVoidPointerType() || RHSTy->isVoidPointerType()) { |
| 4544 | QualType destType = S.Context.getPointerType(S.Context.VoidTy); |
| 4545 | LHS = S.ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
| 4546 | RHS = S.ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
| 4547 | return destType; |
| 4548 | } |
| 4549 | S.Diag(Loc, diag::err_typecheck_cond_incompatible_operands) |
| 4550 | << LHSTy << RHSTy << LHS.get()->getSourceRange() |
| 4551 | << RHS.get()->getSourceRange(); |
| 4552 | return QualType(); |
| 4553 | } |
| 4554 | |
| 4555 | // We have 2 block pointer types. |
| 4556 | return checkConditionalPointerCompatibility(S, LHS, RHS, Loc); |
| 4557 | } |
| 4558 | |
| 4559 | /// \brief Return the resulting type when the operands are both pointers. |
| 4560 | static QualType |
| 4561 | checkConditionalObjectPointersCompatibility(Sema &S, ExprResult &LHS, |
| 4562 | ExprResult &RHS, |
| 4563 | SourceLocation Loc) { |
| 4564 | // get the pointer types |
| 4565 | QualType LHSTy = LHS.get()->getType(); |
| 4566 | QualType RHSTy = RHS.get()->getType(); |
| 4567 | |
| 4568 | // get the "pointed to" types |
| 4569 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 4570 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
| 4571 | |
| 4572 | // ignore qualifiers on void (C99 6.5.15p3, clause 6) |
| 4573 | if (lhptee->isVoidType() && rhptee->isIncompleteOrObjectType()) { |
| 4574 | // Figure out necessary qualifiers (C99 6.5.15p6) |
| 4575 | QualType destPointee |
| 4576 | = S.Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
| 4577 | QualType destType = S.Context.getPointerType(destPointee); |
| 4578 | // Add qualifiers if necessary. |
| 4579 | LHS = S.ImpCastExprToType(LHS.take(), destType, CK_NoOp); |
| 4580 | // Promote to void*. |
| 4581 | RHS = S.ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
| 4582 | return destType; |
| 4583 | } |
| 4584 | if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) { |
| 4585 | QualType destPointee |
| 4586 | = S.Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
| 4587 | QualType destType = S.Context.getPointerType(destPointee); |
| 4588 | // Add qualifiers if necessary. |
| 4589 | RHS = S.ImpCastExprToType(RHS.take(), destType, CK_NoOp); |
| 4590 | // Promote to void*. |
| 4591 | LHS = S.ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
| 4592 | return destType; |
| 4593 | } |
| 4594 | |
| 4595 | return checkConditionalPointerCompatibility(S, LHS, RHS, Loc); |
| 4596 | } |
| 4597 | |
| 4598 | /// \brief Return false if the first expression is not an integer and the second |
| 4599 | /// expression is not a pointer, true otherwise. |
| 4600 | static bool checkPointerIntegerMismatch(Sema &S, ExprResult &Int, |
| 4601 | Expr* PointerExpr, SourceLocation Loc, |
| 4602 | bool isIntFirstExpr) { |
| 4603 | if (!PointerExpr->getType()->isPointerType() || |
| 4604 | !Int.get()->getType()->isIntegerType()) |
| 4605 | return false; |
| 4606 | |
| 4607 | Expr *Expr1 = isIntFirstExpr ? Int.get() : PointerExpr; |
| 4608 | Expr *Expr2 = isIntFirstExpr ? PointerExpr : Int.get(); |
| 4609 | |
| 4610 | S.Diag(Loc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
| 4611 | << Expr1->getType() << Expr2->getType() |
| 4612 | << Expr1->getSourceRange() << Expr2->getSourceRange(); |
| 4613 | Int = S.ImpCastExprToType(Int.take(), PointerExpr->getType(), |
| 4614 | CK_IntegralToPointer); |
| 4615 | return true; |
| 4616 | } |
| 4617 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 4618 | /// Note that lhs is not null here, even if this is the gnu "x ?: y" extension. |
| 4619 | /// In that case, lhs = cond. |
Chris Lattner | a119a3b | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 4620 | /// C99 6.5.15 |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4621 | QualType Sema::CheckConditionalOperands(ExprResult &Cond, ExprResult &LHS, |
| 4622 | ExprResult &RHS, ExprValueKind &VK, |
| 4623 | ExprObjectKind &OK, |
Chris Lattner | a119a3b | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 4624 | SourceLocation QuestionLoc) { |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 4625 | |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 4626 | ExprResult lhsResult = CheckPlaceholderExpr(LHS.get()); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 4627 | if (!lhsResult.isUsable()) return QualType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4628 | LHS = move(lhsResult); |
Douglas Gregor | 7ad5d42 | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 4629 | |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 4630 | ExprResult rhsResult = CheckPlaceholderExpr(RHS.get()); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 4631 | if (!rhsResult.isUsable()) return QualType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4632 | RHS = move(rhsResult); |
Douglas Gregor | 7ad5d42 | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 4633 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4634 | // C++ is sufficiently different to merit its own checker. |
| 4635 | if (getLangOptions().CPlusPlus) |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4636 | return CXXCheckConditionalOperands(Cond, LHS, RHS, VK, OK, QuestionLoc); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4637 | |
| 4638 | VK = VK_RValue; |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4639 | OK = OK_Ordinary; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4640 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4641 | Cond = UsualUnaryConversions(Cond.take()); |
| 4642 | if (Cond.isInvalid()) |
| 4643 | return QualType(); |
| 4644 | LHS = UsualUnaryConversions(LHS.take()); |
| 4645 | if (LHS.isInvalid()) |
| 4646 | return QualType(); |
| 4647 | RHS = UsualUnaryConversions(RHS.take()); |
| 4648 | if (RHS.isInvalid()) |
| 4649 | return QualType(); |
| 4650 | |
| 4651 | QualType CondTy = Cond.get()->getType(); |
| 4652 | QualType LHSTy = LHS.get()->getType(); |
| 4653 | QualType RHSTy = RHS.get()->getType(); |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 4654 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4655 | // first, check the condition. |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4656 | if (checkCondition(*this, Cond.get())) |
| 4657 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4658 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4659 | // Now check the two expressions. |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4660 | if (LHSTy->isVectorType() || RHSTy->isVectorType()) |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 4661 | return CheckVectorOperands(LHS, RHS, QuestionLoc, /*isCompAssign*/false); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 4662 | |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 4663 | // OpenCL: If the condition is a vector, and both operands are scalar, |
| 4664 | // attempt to implicity convert them to the vector type to act like the |
| 4665 | // built in select. |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4666 | if (getLangOptions().OpenCL && CondTy->isVectorType()) |
| 4667 | if (checkConditionalConvertScalarsToVectors(*this, LHS, RHS, CondTy)) |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 4668 | return QualType(); |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 4669 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4670 | // If both operands have arithmetic type, do the usual arithmetic conversions |
| 4671 | // to find a common type: C99 6.5.15p3,5. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4672 | if (LHSTy->isArithmeticType() && RHSTy->isArithmeticType()) { |
| 4673 | UsualArithmeticConversions(LHS, RHS); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4674 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 4675 | return QualType(); |
| 4676 | return LHS.get()->getType(); |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 4677 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4678 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4679 | // If both operands are the same structure or union type, the result is that |
| 4680 | // type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4681 | if (const RecordType *LHSRT = LHSTy->getAs<RecordType>()) { // C99 6.5.15p3 |
| 4682 | if (const RecordType *RHSRT = RHSTy->getAs<RecordType>()) |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 4683 | if (LHSRT->getDecl() == RHSRT->getDecl()) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4684 | // "If both the operands have structure or union type, the result has |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4685 | // that type." This implies that CV qualifiers are dropped. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4686 | return LHSTy.getUnqualifiedType(); |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 4687 | // FIXME: Type of conditional expression must be complete in C mode. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4688 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4689 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4690 | // 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] | 4691 | // The following || allows only one side to be void (a GCC-ism). |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4692 | if (LHSTy->isVoidType() || RHSTy->isVoidType()) { |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4693 | return checkConditionalVoidType(*this, LHS, RHS); |
Steve Naroff | e701c0a | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 4694 | } |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4695 | |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 4696 | // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has |
| 4697 | // the type of the other operand." |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4698 | if (!checkConditionalNullPointer(*this, RHS, LHSTy)) return LHSTy; |
| 4699 | if (!checkConditionalNullPointer(*this, LHS, RHSTy)) return RHSTy; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4700 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4701 | // All objective-c pointer type analysis is done here. |
| 4702 | QualType compositeType = FindCompositeObjCPointerType(LHS, RHS, |
| 4703 | QuestionLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4704 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 4705 | return QualType(); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4706 | if (!compositeType.isNull()) |
| 4707 | return compositeType; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4708 | |
| 4709 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4710 | // Handle block pointer types. |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4711 | if (LHSTy->isBlockPointerType() || RHSTy->isBlockPointerType()) |
| 4712 | return checkConditionalBlockPointerCompatibility(*this, LHS, RHS, |
| 4713 | QuestionLoc); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4714 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4715 | // Check constraints for C object pointers types (C99 6.5.15p3,6). |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4716 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) |
| 4717 | return checkConditionalObjectPointersCompatibility(*this, LHS, RHS, |
| 4718 | QuestionLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4719 | |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 4720 | // GCC compatibility: soften pointer/integer mismatch. Note that |
| 4721 | // null pointers have been filtered out by this point. |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4722 | if (checkPointerIntegerMismatch(*this, LHS, RHS.get(), QuestionLoc, |
| 4723 | /*isIntFirstExpr=*/true)) |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4724 | return RHSTy; |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4725 | if (checkPointerIntegerMismatch(*this, RHS, LHS.get(), QuestionLoc, |
| 4726 | /*isIntFirstExpr=*/false)) |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4727 | return LHSTy; |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 4728 | |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4729 | // Emit a better diagnostic if one of the expressions is a null pointer |
| 4730 | // constant and the other is not a pointer type. In this case, the user most |
| 4731 | // likely forgot to take the address of the other expression. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4732 | if (DiagnoseConditionalForNull(LHS.get(), RHS.get(), QuestionLoc)) |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4733 | return QualType(); |
| 4734 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4735 | // Otherwise, the operands are not compatible. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4736 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4737 | << LHSTy << RHSTy << LHS.get()->getSourceRange() |
| 4738 | << RHS.get()->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4739 | return QualType(); |
| 4740 | } |
| 4741 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4742 | /// FindCompositeObjCPointerType - Helper method to find composite type of |
| 4743 | /// two objective-c pointer types of the two input expressions. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4744 | QualType Sema::FindCompositeObjCPointerType(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4745 | SourceLocation QuestionLoc) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4746 | QualType LHSTy = LHS.get()->getType(); |
| 4747 | QualType RHSTy = RHS.get()->getType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4748 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4749 | // Handle things like Class and struct objc_class*. Here we case the result |
| 4750 | // to the pseudo-builtin, because that will be implicitly cast back to the |
| 4751 | // redefinition type if an attempt is made to access its fields. |
| 4752 | if (LHSTy->isObjCClassType() && |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 4753 | (Context.hasSameType(RHSTy, Context.getObjCClassRedefinitionType()))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4754 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4755 | return LHSTy; |
| 4756 | } |
| 4757 | if (RHSTy->isObjCClassType() && |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 4758 | (Context.hasSameType(LHSTy, Context.getObjCClassRedefinitionType()))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4759 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4760 | return RHSTy; |
| 4761 | } |
| 4762 | // And the same for struct objc_object* / id |
| 4763 | if (LHSTy->isObjCIdType() && |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 4764 | (Context.hasSameType(RHSTy, Context.getObjCIdRedefinitionType()))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4765 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4766 | return LHSTy; |
| 4767 | } |
| 4768 | if (RHSTy->isObjCIdType() && |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 4769 | (Context.hasSameType(LHSTy, Context.getObjCIdRedefinitionType()))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4770 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4771 | return RHSTy; |
| 4772 | } |
| 4773 | // And the same for struct objc_selector* / SEL |
| 4774 | if (Context.isObjCSelType(LHSTy) && |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 4775 | (Context.hasSameType(RHSTy, Context.getObjCSelRedefinitionType()))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4776 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4777 | return LHSTy; |
| 4778 | } |
| 4779 | if (Context.isObjCSelType(RHSTy) && |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 4780 | (Context.hasSameType(LHSTy, Context.getObjCSelRedefinitionType()))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4781 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4782 | return RHSTy; |
| 4783 | } |
| 4784 | // Check constraints for Objective-C object pointers types. |
| 4785 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isObjCObjectPointerType()) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4786 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4787 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 4788 | // Two identical object pointer types are always compatible. |
| 4789 | return LHSTy; |
| 4790 | } |
| 4791 | const ObjCObjectPointerType *LHSOPT = LHSTy->getAs<ObjCObjectPointerType>(); |
| 4792 | const ObjCObjectPointerType *RHSOPT = RHSTy->getAs<ObjCObjectPointerType>(); |
| 4793 | QualType compositeType = LHSTy; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4794 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4795 | // If both operands are interfaces and either operand can be |
| 4796 | // assigned to the other, use that type as the composite |
| 4797 | // type. This allows |
| 4798 | // xxx ? (A*) a : (B*) b |
| 4799 | // where B is a subclass of A. |
| 4800 | // |
| 4801 | // Additionally, as for assignment, if either type is 'id' |
| 4802 | // allow silent coercion. Finally, if the types are |
| 4803 | // incompatible then make sure to use 'id' as the composite |
| 4804 | // type so the result is acceptable for sending messages to. |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4805 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4806 | // FIXME: Consider unifying with 'areComparableObjCPointerTypes'. |
| 4807 | // It could return the composite type. |
| 4808 | if (Context.canAssignObjCInterfaces(LHSOPT, RHSOPT)) { |
| 4809 | compositeType = RHSOPT->isObjCBuiltinType() ? RHSTy : LHSTy; |
| 4810 | } else if (Context.canAssignObjCInterfaces(RHSOPT, LHSOPT)) { |
| 4811 | compositeType = LHSOPT->isObjCBuiltinType() ? LHSTy : RHSTy; |
| 4812 | } else if ((LHSTy->isObjCQualifiedIdType() || |
| 4813 | RHSTy->isObjCQualifiedIdType()) && |
| 4814 | Context.ObjCQualifiedIdTypesAreCompatible(LHSTy, RHSTy, true)) { |
| 4815 | // Need to handle "id<xx>" explicitly. |
| 4816 | // GCC allows qualified id and any Objective-C type to devolve to |
| 4817 | // id. Currently localizing to here until clear this should be |
| 4818 | // part of ObjCQualifiedIdTypesAreCompatible. |
| 4819 | compositeType = Context.getObjCIdType(); |
| 4820 | } else if (LHSTy->isObjCIdType() || RHSTy->isObjCIdType()) { |
| 4821 | compositeType = Context.getObjCIdType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4822 | } else if (!(compositeType = |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4823 | Context.areCommonBaseCompatible(LHSOPT, RHSOPT)).isNull()) |
| 4824 | ; |
| 4825 | else { |
| 4826 | Diag(QuestionLoc, diag::ext_typecheck_cond_incompatible_operands) |
| 4827 | << LHSTy << RHSTy |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4828 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4829 | QualType incompatTy = Context.getObjCIdType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4830 | LHS = ImpCastExprToType(LHS.take(), incompatTy, CK_BitCast); |
| 4831 | RHS = ImpCastExprToType(RHS.take(), incompatTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4832 | return incompatTy; |
| 4833 | } |
| 4834 | // The object pointer types are compatible. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4835 | LHS = ImpCastExprToType(LHS.take(), compositeType, CK_BitCast); |
| 4836 | RHS = ImpCastExprToType(RHS.take(), compositeType, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4837 | return compositeType; |
| 4838 | } |
| 4839 | // Check Objective-C object pointer types and 'void *' |
| 4840 | if (LHSTy->isVoidPointerType() && RHSTy->isObjCObjectPointerType()) { |
| 4841 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 4842 | QualType rhptee = RHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 4843 | QualType destPointee |
| 4844 | = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
| 4845 | QualType destType = Context.getPointerType(destPointee); |
| 4846 | // Add qualifiers if necessary. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4847 | LHS = ImpCastExprToType(LHS.take(), destType, CK_NoOp); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4848 | // Promote to void*. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4849 | RHS = ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4850 | return destType; |
| 4851 | } |
| 4852 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isVoidPointerType()) { |
| 4853 | QualType lhptee = LHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 4854 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
| 4855 | QualType destPointee |
| 4856 | = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
| 4857 | QualType destType = Context.getPointerType(destPointee); |
| 4858 | // Add qualifiers if necessary. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4859 | RHS = ImpCastExprToType(RHS.take(), destType, CK_NoOp); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4860 | // Promote to void*. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4861 | LHS = ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4862 | return destType; |
| 4863 | } |
| 4864 | return QualType(); |
| 4865 | } |
| 4866 | |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 4867 | /// SuggestParentheses - Emit a note with a fixit hint that wraps |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4868 | /// ParenRange in parentheses. |
| 4869 | static void SuggestParentheses(Sema &Self, SourceLocation Loc, |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 4870 | const PartialDiagnostic &Note, |
| 4871 | SourceRange ParenRange) { |
| 4872 | SourceLocation EndLoc = Self.PP.getLocForEndOfToken(ParenRange.getEnd()); |
| 4873 | if (ParenRange.getBegin().isFileID() && ParenRange.getEnd().isFileID() && |
| 4874 | EndLoc.isValid()) { |
| 4875 | Self.Diag(Loc, Note) |
| 4876 | << FixItHint::CreateInsertion(ParenRange.getBegin(), "(") |
| 4877 | << FixItHint::CreateInsertion(EndLoc, ")"); |
| 4878 | } else { |
| 4879 | // We can't display the parentheses, so just show the bare note. |
| 4880 | Self.Diag(Loc, Note) << ParenRange; |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4881 | } |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4882 | } |
| 4883 | |
| 4884 | static bool IsArithmeticOp(BinaryOperatorKind Opc) { |
| 4885 | return Opc >= BO_Mul && Opc <= BO_Shr; |
| 4886 | } |
| 4887 | |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4888 | /// IsArithmeticBinaryExpr - Returns true if E is an arithmetic binary |
| 4889 | /// expression, either using a built-in or overloaded operator, |
| 4890 | /// and sets *OpCode to the opcode and *RHS to the right-hand side expression. |
| 4891 | static bool IsArithmeticBinaryExpr(Expr *E, BinaryOperatorKind *Opcode, |
| 4892 | Expr **RHS) { |
| 4893 | E = E->IgnoreParenImpCasts(); |
| 4894 | E = E->IgnoreConversionOperator(); |
| 4895 | E = E->IgnoreParenImpCasts(); |
| 4896 | |
| 4897 | // Built-in binary operator. |
| 4898 | if (BinaryOperator *OP = dyn_cast<BinaryOperator>(E)) { |
| 4899 | if (IsArithmeticOp(OP->getOpcode())) { |
| 4900 | *Opcode = OP->getOpcode(); |
| 4901 | *RHS = OP->getRHS(); |
| 4902 | return true; |
| 4903 | } |
| 4904 | } |
| 4905 | |
| 4906 | // Overloaded operator. |
| 4907 | if (CXXOperatorCallExpr *Call = dyn_cast<CXXOperatorCallExpr>(E)) { |
| 4908 | if (Call->getNumArgs() != 2) |
| 4909 | return false; |
| 4910 | |
| 4911 | // Make sure this is really a binary operator that is safe to pass into |
| 4912 | // BinaryOperator::getOverloadedOpcode(), e.g. it's not a subscript op. |
| 4913 | OverloadedOperatorKind OO = Call->getOperator(); |
| 4914 | if (OO < OO_Plus || OO > OO_Arrow) |
| 4915 | return false; |
| 4916 | |
| 4917 | BinaryOperatorKind OpKind = BinaryOperator::getOverloadedOpcode(OO); |
| 4918 | if (IsArithmeticOp(OpKind)) { |
| 4919 | *Opcode = OpKind; |
| 4920 | *RHS = Call->getArg(1); |
| 4921 | return true; |
| 4922 | } |
| 4923 | } |
| 4924 | |
| 4925 | return false; |
| 4926 | } |
| 4927 | |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4928 | static bool IsLogicOp(BinaryOperatorKind Opc) { |
| 4929 | return (Opc >= BO_LT && Opc <= BO_NE) || (Opc >= BO_LAnd && Opc <= BO_LOr); |
| 4930 | } |
| 4931 | |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4932 | /// ExprLooksBoolean - Returns true if E looks boolean, i.e. it has boolean type |
| 4933 | /// or is a logical expression such as (x==y) which has int type, but is |
| 4934 | /// commonly interpreted as boolean. |
| 4935 | static bool ExprLooksBoolean(Expr *E) { |
| 4936 | E = E->IgnoreParenImpCasts(); |
| 4937 | |
| 4938 | if (E->getType()->isBooleanType()) |
| 4939 | return true; |
| 4940 | if (BinaryOperator *OP = dyn_cast<BinaryOperator>(E)) |
| 4941 | return IsLogicOp(OP->getOpcode()); |
| 4942 | if (UnaryOperator *OP = dyn_cast<UnaryOperator>(E)) |
| 4943 | return OP->getOpcode() == UO_LNot; |
| 4944 | |
| 4945 | return false; |
| 4946 | } |
| 4947 | |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4948 | /// DiagnoseConditionalPrecedence - Emit a warning when a conditional operator |
| 4949 | /// and binary operator are mixed in a way that suggests the programmer assumed |
| 4950 | /// the conditional operator has higher precedence, for example: |
| 4951 | /// "int x = a + someBinaryCondition ? 1 : 2". |
| 4952 | static void DiagnoseConditionalPrecedence(Sema &Self, |
| 4953 | SourceLocation OpLoc, |
Chandler Carruth | 43bc78d | 2011-06-16 01:05:08 +0000 | [diff] [blame] | 4954 | Expr *Condition, |
| 4955 | Expr *LHS, |
| 4956 | Expr *RHS) { |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4957 | BinaryOperatorKind CondOpcode; |
| 4958 | Expr *CondRHS; |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4959 | |
Chandler Carruth | 43bc78d | 2011-06-16 01:05:08 +0000 | [diff] [blame] | 4960 | if (!IsArithmeticBinaryExpr(Condition, &CondOpcode, &CondRHS)) |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4961 | return; |
| 4962 | if (!ExprLooksBoolean(CondRHS)) |
| 4963 | return; |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4964 | |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4965 | // The condition is an arithmetic binary expression, with a right- |
| 4966 | // hand side that looks boolean, so warn. |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4967 | |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 4968 | Self.Diag(OpLoc, diag::warn_precedence_conditional) |
Chandler Carruth | 43bc78d | 2011-06-16 01:05:08 +0000 | [diff] [blame] | 4969 | << Condition->getSourceRange() |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 4970 | << BinaryOperator::getOpcodeStr(CondOpcode); |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4971 | |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 4972 | SuggestParentheses(Self, OpLoc, |
| 4973 | Self.PDiag(diag::note_precedence_conditional_silence) |
| 4974 | << BinaryOperator::getOpcodeStr(CondOpcode), |
| 4975 | SourceRange(Condition->getLocStart(), Condition->getLocEnd())); |
Chandler Carruth | 9d5353c | 2011-06-21 23:04:18 +0000 | [diff] [blame] | 4976 | |
| 4977 | SuggestParentheses(Self, OpLoc, |
| 4978 | Self.PDiag(diag::note_precedence_conditional_first), |
| 4979 | SourceRange(CondRHS->getLocStart(), RHS->getLocEnd())); |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 4980 | } |
| 4981 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 4982 | /// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4983 | /// in the case of a the GNU conditional expr extension. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4984 | ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4985 | SourceLocation ColonLoc, |
| 4986 | Expr *CondExpr, Expr *LHSExpr, |
| 4987 | Expr *RHSExpr) { |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 4988 | // If this is the gnu "x ?: y" extension, analyze the types as though the LHS |
| 4989 | // was the condition. |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4990 | OpaqueValueExpr *opaqueValue = 0; |
| 4991 | Expr *commonExpr = 0; |
| 4992 | if (LHSExpr == 0) { |
| 4993 | commonExpr = CondExpr; |
| 4994 | |
| 4995 | // We usually want to apply unary conversions *before* saving, except |
| 4996 | // in the special case of a C++ l-value conditional. |
| 4997 | if (!(getLangOptions().CPlusPlus |
| 4998 | && !commonExpr->isTypeDependent() |
| 4999 | && commonExpr->getValueKind() == RHSExpr->getValueKind() |
| 5000 | && commonExpr->isGLValue() |
| 5001 | && commonExpr->isOrdinaryOrBitFieldObject() |
| 5002 | && RHSExpr->isOrdinaryOrBitFieldObject() |
| 5003 | && Context.hasSameType(commonExpr->getType(), RHSExpr->getType()))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5004 | ExprResult commonRes = UsualUnaryConversions(commonExpr); |
| 5005 | if (commonRes.isInvalid()) |
| 5006 | return ExprError(); |
| 5007 | commonExpr = commonRes.take(); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5008 | } |
| 5009 | |
| 5010 | opaqueValue = new (Context) OpaqueValueExpr(commonExpr->getExprLoc(), |
| 5011 | commonExpr->getType(), |
| 5012 | commonExpr->getValueKind(), |
| 5013 | commonExpr->getObjectKind()); |
| 5014 | LHSExpr = CondExpr = opaqueValue; |
Fariborz Jahanian | f9b949f | 2010-08-31 18:02:20 +0000 | [diff] [blame] | 5015 | } |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5016 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5017 | ExprValueKind VK = VK_RValue; |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 5018 | ExprObjectKind OK = OK_Ordinary; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5019 | ExprResult Cond = Owned(CondExpr), LHS = Owned(LHSExpr), RHS = Owned(RHSExpr); |
| 5020 | QualType result = CheckConditionalOperands(Cond, LHS, RHS, |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5021 | VK, OK, QuestionLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5022 | if (result.isNull() || Cond.isInvalid() || LHS.isInvalid() || |
| 5023 | RHS.isInvalid()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5024 | return ExprError(); |
| 5025 | |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5026 | DiagnoseConditionalPrecedence(*this, QuestionLoc, Cond.get(), LHS.get(), |
| 5027 | RHS.get()); |
| 5028 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5029 | if (!commonExpr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5030 | return Owned(new (Context) ConditionalOperator(Cond.take(), QuestionLoc, |
| 5031 | LHS.take(), ColonLoc, |
| 5032 | RHS.take(), result, VK, OK)); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5033 | |
| 5034 | return Owned(new (Context) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5035 | BinaryConditionalOperator(commonExpr, opaqueValue, Cond.take(), LHS.take(), |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5036 | RHS.take(), QuestionLoc, ColonLoc, result, VK, |
| 5037 | OK)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5038 | } |
| 5039 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5040 | // checkPointerTypesForAssignment - This is a very tricky routine (despite |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5041 | // being closely modeled after the C99 spec:-). The odd characteristic of this |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5042 | // routine is it effectively iqnores the qualifiers on the top level pointee. |
| 5043 | // This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3]. |
| 5044 | // FIXME: add a couple examples in this comment. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5045 | static Sema::AssignConvertType |
| 5046 | checkPointerTypesForAssignment(Sema &S, QualType lhsType, QualType rhsType) { |
| 5047 | assert(lhsType.isCanonical() && "LHS not canonicalized!"); |
| 5048 | assert(rhsType.isCanonical() && "RHS not canonicalized!"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5049 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5050 | // get the "pointed to" type (ignoring qualifiers at the top level) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5051 | const Type *lhptee, *rhptee; |
| 5052 | Qualifiers lhq, rhq; |
| 5053 | llvm::tie(lhptee, lhq) = cast<PointerType>(lhsType)->getPointeeType().split(); |
| 5054 | llvm::tie(rhptee, rhq) = cast<PointerType>(rhsType)->getPointeeType().split(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5055 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5056 | Sema::AssignConvertType ConvTy = Sema::Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5057 | |
| 5058 | // C99 6.5.16.1p1: This following citation is common to constraints |
| 5059 | // 3 & 4 (below). ...and the type *pointed to* by the left has all the |
| 5060 | // qualifiers of the type *pointed to* by the right; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5061 | Qualifiers lq; |
| 5062 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5063 | // As a special case, 'non-__weak A *' -> 'non-__weak const *' is okay. |
| 5064 | if (lhq.getObjCLifetime() != rhq.getObjCLifetime() && |
| 5065 | lhq.compatiblyIncludesObjCLifetime(rhq)) { |
| 5066 | // Ignore lifetime for further calculation. |
| 5067 | lhq.removeObjCLifetime(); |
| 5068 | rhq.removeObjCLifetime(); |
| 5069 | } |
| 5070 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5071 | if (!lhq.compatiblyIncludes(rhq)) { |
| 5072 | // Treat address-space mismatches as fatal. TODO: address subspaces |
| 5073 | if (lhq.getAddressSpace() != rhq.getAddressSpace()) |
| 5074 | ConvTy = Sema::IncompatiblePointerDiscardsQualifiers; |
| 5075 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5076 | // 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] | 5077 | // and from void*. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5078 | else if (lhq.withoutObjCGCAttr().withoutObjCGLifetime() |
| 5079 | .compatiblyIncludes( |
| 5080 | rhq.withoutObjCGCAttr().withoutObjCGLifetime()) |
John McCall | 2234873 | 2011-03-26 02:56:45 +0000 | [diff] [blame] | 5081 | && (lhptee->isVoidType() || rhptee->isVoidType())) |
| 5082 | ; // keep old |
| 5083 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5084 | // Treat lifetime mismatches as fatal. |
| 5085 | else if (lhq.getObjCLifetime() != rhq.getObjCLifetime()) |
| 5086 | ConvTy = Sema::IncompatiblePointerDiscardsQualifiers; |
| 5087 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5088 | // For GCC compatibility, other qualifier mismatches are treated |
| 5089 | // as still compatible in C. |
| 5090 | else ConvTy = Sema::CompatiblePointerDiscardsQualifiers; |
| 5091 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5092 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5093 | // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or |
| 5094 | // 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] | 5095 | // version of void... |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5096 | if (lhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5097 | if (rhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5098 | return ConvTy; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5099 | |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5100 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5101 | assert(rhptee->isFunctionType()); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5102 | return Sema::FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5103 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5104 | |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5105 | if (rhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5106 | if (lhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5107 | return ConvTy; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5108 | |
| 5109 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5110 | assert(lhptee->isFunctionType()); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5111 | return Sema::FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5112 | } |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5113 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5114 | // 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] | 5115 | // unqualified versions of compatible types, ... |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5116 | QualType ltrans = QualType(lhptee, 0), rtrans = QualType(rhptee, 0); |
| 5117 | if (!S.Context.typesAreCompatible(ltrans, rtrans)) { |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5118 | // Check if the pointee types are compatible ignoring the sign. |
| 5119 | // We explicitly check for char so that we catch "char" vs |
| 5120 | // "unsigned char" on systems where "char" is unsigned. |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5121 | if (lhptee->isCharType()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5122 | ltrans = S.Context.UnsignedCharTy; |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 5123 | else if (lhptee->hasSignedIntegerRepresentation()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5124 | ltrans = S.Context.getCorrespondingUnsignedType(ltrans); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5125 | |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5126 | if (rhptee->isCharType()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5127 | rtrans = S.Context.UnsignedCharTy; |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 5128 | else if (rhptee->hasSignedIntegerRepresentation()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5129 | rtrans = S.Context.getCorrespondingUnsignedType(rtrans); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5130 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5131 | if (ltrans == rtrans) { |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5132 | // Types are compatible ignoring the sign. Qualifier incompatibility |
| 5133 | // takes priority over sign incompatibility because the sign |
| 5134 | // warning can be disabled. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5135 | if (ConvTy != Sema::Compatible) |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5136 | return ConvTy; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5137 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5138 | return Sema::IncompatiblePointerSign; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5139 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5140 | |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 5141 | // If we are a multi-level pointer, it's possible that our issue is simply |
| 5142 | // one of qualification - e.g. char ** -> const char ** is not allowed. If |
| 5143 | // the eventual target type is the same and the pointers have the same |
| 5144 | // level of indirection, this must be the issue. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5145 | if (isa<PointerType>(lhptee) && isa<PointerType>(rhptee)) { |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 5146 | do { |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5147 | lhptee = cast<PointerType>(lhptee)->getPointeeType().getTypePtr(); |
| 5148 | rhptee = cast<PointerType>(rhptee)->getPointeeType().getTypePtr(); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5149 | } while (isa<PointerType>(lhptee) && isa<PointerType>(rhptee)); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5150 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5151 | if (lhptee == rhptee) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5152 | return Sema::IncompatibleNestedPointerQualifiers; |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 5153 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5154 | |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5155 | // General pointer incompatibility takes priority over qualifiers. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5156 | return Sema::IncompatiblePointer; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5157 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5158 | return ConvTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5159 | } |
| 5160 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5161 | /// checkBlockPointerTypesForAssignment - This routine determines whether two |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5162 | /// block pointer types are compatible or whether a block and normal pointer |
| 5163 | /// are compatible. It is more restrict than comparing two function pointer |
| 5164 | // types. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5165 | static Sema::AssignConvertType |
| 5166 | checkBlockPointerTypesForAssignment(Sema &S, QualType lhsType, |
| 5167 | QualType rhsType) { |
| 5168 | assert(lhsType.isCanonical() && "LHS not canonicalized!"); |
| 5169 | assert(rhsType.isCanonical() && "RHS not canonicalized!"); |
| 5170 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5171 | QualType lhptee, rhptee; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5172 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5173 | // get the "pointed to" type (ignoring qualifiers at the top level) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5174 | lhptee = cast<BlockPointerType>(lhsType)->getPointeeType(); |
| 5175 | rhptee = cast<BlockPointerType>(rhsType)->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5176 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5177 | // In C++, the types have to match exactly. |
| 5178 | if (S.getLangOptions().CPlusPlus) |
| 5179 | return Sema::IncompatibleBlockPointer; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5180 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5181 | Sema::AssignConvertType ConvTy = Sema::Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5182 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5183 | // For blocks we enforce that qualifiers are identical. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5184 | if (lhptee.getLocalQualifiers() != rhptee.getLocalQualifiers()) |
| 5185 | ConvTy = Sema::CompatiblePointerDiscardsQualifiers; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5186 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5187 | if (!S.Context.typesAreBlockPointerCompatible(lhsType, rhsType)) |
| 5188 | return Sema::IncompatibleBlockPointer; |
| 5189 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5190 | return ConvTy; |
| 5191 | } |
| 5192 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5193 | /// checkObjCPointerTypesForAssignment - Compares two objective-c pointer types |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5194 | /// for assignment compatibility. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5195 | static Sema::AssignConvertType |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5196 | checkObjCPointerTypesForAssignment(Sema &S, QualType lhsType, |
| 5197 | QualType rhsType) { |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5198 | assert(lhsType.isCanonical() && "LHS was not canonicalized!"); |
| 5199 | assert(rhsType.isCanonical() && "RHS was not canonicalized!"); |
| 5200 | |
Fariborz Jahanian | d4c6090 | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 5201 | if (lhsType->isObjCBuiltinType()) { |
| 5202 | // Class is not compatible with ObjC object pointers. |
Fariborz Jahanian | 528adb1 | 2010-03-24 21:00:27 +0000 | [diff] [blame] | 5203 | if (lhsType->isObjCClassType() && !rhsType->isObjCBuiltinType() && |
| 5204 | !rhsType->isObjCQualifiedClassType()) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5205 | return Sema::IncompatiblePointer; |
| 5206 | return Sema::Compatible; |
Fariborz Jahanian | d4c6090 | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 5207 | } |
| 5208 | if (rhsType->isObjCBuiltinType()) { |
| 5209 | // Class is not compatible with ObjC object pointers. |
Fariborz Jahanian | 528adb1 | 2010-03-24 21:00:27 +0000 | [diff] [blame] | 5210 | if (rhsType->isObjCClassType() && !lhsType->isObjCBuiltinType() && |
| 5211 | !lhsType->isObjCQualifiedClassType()) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5212 | return Sema::IncompatiblePointer; |
| 5213 | return Sema::Compatible; |
Fariborz Jahanian | d4c6090 | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 5214 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5215 | QualType lhptee = |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5216 | lhsType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5217 | QualType rhptee = |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5218 | rhsType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5219 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5220 | if (!lhptee.isAtLeastAsQualifiedAs(rhptee)) |
| 5221 | return Sema::CompatiblePointerDiscardsQualifiers; |
| 5222 | |
| 5223 | if (S.Context.typesAreCompatible(lhsType, rhsType)) |
| 5224 | return Sema::Compatible; |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5225 | if (lhsType->isObjCQualifiedIdType() || rhsType->isObjCQualifiedIdType()) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5226 | return Sema::IncompatibleObjCQualifiedId; |
| 5227 | return Sema::IncompatiblePointer; |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5228 | } |
| 5229 | |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5230 | Sema::AssignConvertType |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 5231 | Sema::CheckAssignmentConstraints(SourceLocation Loc, |
| 5232 | QualType lhsType, QualType rhsType) { |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5233 | // Fake up an opaque expression. We don't actually care about what |
| 5234 | // cast operations are required, so if CheckAssignmentConstraints |
| 5235 | // adds casts to this they'll be wasted, but fortunately that doesn't |
| 5236 | // usually happen on valid code. |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 5237 | OpaqueValueExpr rhs(Loc, rhsType, VK_RValue); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5238 | ExprResult rhsPtr = &rhs; |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5239 | CastKind K = CK_Invalid; |
| 5240 | |
| 5241 | return CheckAssignmentConstraints(lhsType, rhsPtr, K); |
| 5242 | } |
| 5243 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5244 | /// CheckAssignmentConstraints (C99 6.5.16) - This routine currently |
| 5245 | /// has code to accommodate several GCC extensions when type checking |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5246 | /// pointers. Here are some objectionable examples that GCC considers warnings: |
| 5247 | /// |
| 5248 | /// int a, *pint; |
| 5249 | /// short *pshort; |
| 5250 | /// struct foo *pfoo; |
| 5251 | /// |
| 5252 | /// pint = pshort; // warning: assignment from incompatible pointer type |
| 5253 | /// a = pint; // warning: assignment makes integer from pointer without a cast |
| 5254 | /// pint = a; // warning: assignment makes pointer from integer without a cast |
| 5255 | /// pint = pfoo; // warning: assignment from incompatible pointer type |
| 5256 | /// |
| 5257 | /// 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] | 5258 | /// C99 spec dictates. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5259 | /// |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5260 | /// Sets 'Kind' for any result kind except Incompatible. |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5261 | Sema::AssignConvertType |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5262 | Sema::CheckAssignmentConstraints(QualType lhsType, ExprResult &rhs, |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5263 | CastKind &Kind) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5264 | QualType rhsType = rhs.get()->getType(); |
Fariborz Jahanian | 04e5a25 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 5265 | QualType origLhsType = lhsType; |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5266 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5267 | // Get canonical types. We're not formatting these types, just comparing |
| 5268 | // them. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 5269 | lhsType = Context.getCanonicalType(lhsType).getUnqualifiedType(); |
| 5270 | rhsType = Context.getCanonicalType(rhsType).getUnqualifiedType(); |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5271 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5272 | // Common case: no conversion required. |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5273 | if (lhsType == rhsType) { |
| 5274 | Kind = CK_NoOp; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5275 | return Compatible; |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 5276 | } |
| 5277 | |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 5278 | // If the left-hand side is a reference type, then we are in a |
| 5279 | // (rare!) case where we've allowed the use of references in C, |
| 5280 | // e.g., as a parameter type in a built-in function. In this case, |
| 5281 | // just make sure that the type referenced is compatible with the |
| 5282 | // right-hand side type. The caller is responsible for adjusting |
| 5283 | // lhsType so that the resulting expression does not have reference |
| 5284 | // type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5285 | if (const ReferenceType *lhsTypeRef = lhsType->getAs<ReferenceType>()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5286 | if (Context.typesAreCompatible(lhsTypeRef->getPointeeType(), rhsType)) { |
| 5287 | Kind = CK_LValueBitCast; |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 5288 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5289 | } |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5290 | return Incompatible; |
Fariborz Jahanian | 411f373 | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 5291 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5292 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5293 | // Allow scalar to ExtVector assignments, and assignments of an ExtVector type |
| 5294 | // to the same ExtVector type. |
| 5295 | if (lhsType->isExtVectorType()) { |
| 5296 | if (rhsType->isExtVectorType()) |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5297 | return Incompatible; |
| 5298 | if (rhsType->isArithmeticType()) { |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5299 | // CK_VectorSplat does T -> vector T, so first cast to the |
| 5300 | // element type. |
| 5301 | QualType elType = cast<ExtVectorType>(lhsType)->getElementType(); |
| 5302 | if (elType != rhsType) { |
| 5303 | Kind = PrepareScalarCast(*this, rhs, elType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5304 | rhs = ImpCastExprToType(rhs.take(), elType, Kind); |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5305 | } |
| 5306 | Kind = CK_VectorSplat; |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5307 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5308 | } |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5309 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5310 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5311 | // Conversions to or from vector type. |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5312 | if (lhsType->isVectorType() || rhsType->isVectorType()) { |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5313 | if (lhsType->isVectorType() && rhsType->isVectorType()) { |
Bob Wilson | de3deea | 2010-12-02 00:25:15 +0000 | [diff] [blame] | 5314 | // Allow assignments of an AltiVec vector type to an equivalent GCC |
| 5315 | // vector type and vice versa |
| 5316 | if (Context.areCompatibleVectorTypes(lhsType, rhsType)) { |
| 5317 | Kind = CK_BitCast; |
| 5318 | return Compatible; |
| 5319 | } |
| 5320 | |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5321 | // If we are allowing lax vector conversions, and LHS and RHS are both |
| 5322 | // vectors, the total size only needs to be the same. This is a bitcast; |
| 5323 | // no bits are changed but the result type is different. |
| 5324 | if (getLangOptions().LaxVectorConversions && |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5325 | (Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType))) { |
John McCall | 0c6d28d | 2010-11-15 10:08:00 +0000 | [diff] [blame] | 5326 | Kind = CK_BitCast; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 5327 | return IncompatibleVectors; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5328 | } |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 5329 | } |
| 5330 | return Incompatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5331 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5332 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5333 | // Arithmetic conversions. |
Douglas Gregor | 88623ad | 2010-05-23 21:53:47 +0000 | [diff] [blame] | 5334 | if (lhsType->isArithmeticType() && rhsType->isArithmeticType() && |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5335 | !(getLangOptions().CPlusPlus && lhsType->isEnumeralType())) { |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5336 | Kind = PrepareScalarCast(*this, rhs, lhsType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5337 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5338 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5339 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5340 | // Conversions to normal pointers. |
| 5341 | if (const PointerType *lhsPointer = dyn_cast<PointerType>(lhsType)) { |
| 5342 | // U* -> T* |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5343 | if (isa<PointerType>(rhsType)) { |
| 5344 | Kind = CK_BitCast; |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5345 | return checkPointerTypesForAssignment(*this, lhsType, rhsType); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5346 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5347 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5348 | // int -> T* |
| 5349 | if (rhsType->isIntegerType()) { |
| 5350 | Kind = CK_IntegralToPointer; // FIXME: null? |
| 5351 | return IntToPointer; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5352 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5353 | |
| 5354 | // C pointers are not compatible with ObjC object pointers, |
| 5355 | // with two exceptions: |
| 5356 | if (isa<ObjCObjectPointerType>(rhsType)) { |
| 5357 | // - conversions to void* |
| 5358 | if (lhsPointer->getPointeeType()->isVoidType()) { |
| 5359 | Kind = CK_AnyPointerToObjCPointerCast; |
| 5360 | return Compatible; |
| 5361 | } |
| 5362 | |
| 5363 | // - conversions from 'Class' to the redefinition type |
| 5364 | if (rhsType->isObjCClassType() && |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 5365 | Context.hasSameType(lhsType, |
| 5366 | Context.getObjCClassRedefinitionType())) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5367 | Kind = CK_BitCast; |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 5368 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5369 | } |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5370 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5371 | Kind = CK_BitCast; |
| 5372 | return IncompatiblePointer; |
| 5373 | } |
| 5374 | |
| 5375 | // U^ -> void* |
| 5376 | if (rhsType->getAs<BlockPointerType>()) { |
| 5377 | if (lhsPointer->getPointeeType()->isVoidType()) { |
| 5378 | Kind = CK_BitCast; |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5379 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +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 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5383 | return Incompatible; |
| 5384 | } |
| 5385 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5386 | // Conversions to block pointers. |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5387 | if (isa<BlockPointerType>(lhsType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5388 | // U^ -> T^ |
| 5389 | if (rhsType->isBlockPointerType()) { |
| 5390 | Kind = CK_AnyPointerToBlockPointerCast; |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5391 | return checkBlockPointerTypesForAssignment(*this, lhsType, rhsType); |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5392 | } |
| 5393 | |
| 5394 | // int or null -> T^ |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5395 | if (rhsType->isIntegerType()) { |
| 5396 | Kind = CK_IntegralToPointer; // FIXME: null |
Eli Friedman | d8f4f43 | 2009-02-25 04:20:42 +0000 | [diff] [blame] | 5397 | return IntToBlockPointer; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5398 | } |
| 5399 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5400 | // id -> T^ |
| 5401 | if (getLangOptions().ObjC1 && rhsType->isObjCIdType()) { |
| 5402 | Kind = CK_AnyPointerToBlockPointerCast; |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5403 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5404 | } |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5405 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5406 | // void* -> T^ |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5407 | if (const PointerType *RHSPT = rhsType->getAs<PointerType>()) |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5408 | if (RHSPT->getPointeeType()->isVoidType()) { |
| 5409 | Kind = CK_AnyPointerToBlockPointerCast; |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 5410 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5411 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5412 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5413 | return Incompatible; |
| 5414 | } |
| 5415 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5416 | // Conversions to Objective-C pointers. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5417 | if (isa<ObjCObjectPointerType>(lhsType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5418 | // A* -> B* |
| 5419 | if (rhsType->isObjCObjectPointerType()) { |
| 5420 | Kind = CK_BitCast; |
Fariborz Jahanian | 04e5a25 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 5421 | Sema::AssignConvertType result = |
| 5422 | checkObjCPointerTypesForAssignment(*this, lhsType, rhsType); |
| 5423 | if (getLangOptions().ObjCAutoRefCount && |
| 5424 | result == Compatible && |
Fariborz Jahanian | 7a084ec | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 5425 | !CheckObjCARCUnavailableWeakConversion(origLhsType, rhsType)) |
| 5426 | result = IncompatibleObjCWeakRef; |
Fariborz Jahanian | 04e5a25 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 5427 | return result; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5428 | } |
| 5429 | |
| 5430 | // int or null -> A* |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5431 | if (rhsType->isIntegerType()) { |
| 5432 | Kind = CK_IntegralToPointer; // FIXME: null |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5433 | return IntToPointer; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5434 | } |
| 5435 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5436 | // In general, C pointers are not compatible with ObjC object pointers, |
| 5437 | // with two exceptions: |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5438 | if (isa<PointerType>(rhsType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5439 | // - conversions from 'void*' |
| 5440 | if (rhsType->isVoidPointerType()) { |
| 5441 | Kind = CK_AnyPointerToObjCPointerCast; |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 5442 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5443 | } |
| 5444 | |
| 5445 | // - conversions to 'Class' from its redefinition type |
| 5446 | if (lhsType->isObjCClassType() && |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 5447 | Context.hasSameType(rhsType, |
| 5448 | Context.getObjCClassRedefinitionType())) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5449 | Kind = CK_BitCast; |
| 5450 | return Compatible; |
| 5451 | } |
| 5452 | |
| 5453 | Kind = CK_AnyPointerToObjCPointerCast; |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 5454 | return IncompatiblePointer; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5455 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5456 | |
| 5457 | // T^ -> A* |
| 5458 | if (rhsType->isBlockPointerType()) { |
| 5459 | Kind = CK_AnyPointerToObjCPointerCast; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5460 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5461 | } |
| 5462 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5463 | return Incompatible; |
| 5464 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5465 | |
| 5466 | // Conversions from pointers that are not covered by the above. |
Chris Lattner | 78eca28 | 2008-04-07 06:49:41 +0000 | [diff] [blame] | 5467 | if (isa<PointerType>(rhsType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5468 | // T* -> _Bool |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5469 | if (lhsType == Context.BoolTy) { |
| 5470 | Kind = CK_PointerToBoolean; |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5471 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5472 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5473 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5474 | // T* -> int |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5475 | if (lhsType->isIntegerType()) { |
| 5476 | Kind = CK_PointerToIntegral; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 5477 | return PointerToInt; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5478 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5479 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5480 | return Incompatible; |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5481 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5482 | |
| 5483 | // Conversions from Objective-C pointers that are not covered by the above. |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5484 | if (isa<ObjCObjectPointerType>(rhsType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5485 | // T* -> _Bool |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5486 | if (lhsType == Context.BoolTy) { |
| 5487 | Kind = CK_PointerToBoolean; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5488 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5489 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5490 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5491 | // T* -> int |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5492 | if (lhsType->isIntegerType()) { |
| 5493 | Kind = CK_PointerToIntegral; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5494 | return PointerToInt; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5495 | } |
| 5496 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5497 | return Incompatible; |
| 5498 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5499 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5500 | // struct A -> struct B |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5501 | if (isa<TagType>(lhsType) && isa<TagType>(rhsType)) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5502 | if (Context.typesAreCompatible(lhsType, rhsType)) { |
| 5503 | Kind = CK_NoOp; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5504 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5505 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5506 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5507 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5508 | return Incompatible; |
| 5509 | } |
| 5510 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5511 | /// \brief Constructs a transparent union from an expression that is |
| 5512 | /// used to initialize the transparent union. |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5513 | static void ConstructTransparentUnion(Sema &S, ASTContext &C, |
| 5514 | ExprResult &EResult, QualType UnionType, |
| 5515 | FieldDecl *Field) { |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5516 | // Build an initializer list that designates the appropriate member |
| 5517 | // of the transparent union. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5518 | Expr *E = EResult.take(); |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 5519 | InitListExpr *Initializer = new (C) InitListExpr(C, SourceLocation(), |
Ted Kremenek | ba7bc55 | 2010-02-19 01:50:18 +0000 | [diff] [blame] | 5520 | &E, 1, |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5521 | SourceLocation()); |
| 5522 | Initializer->setType(UnionType); |
| 5523 | Initializer->setInitializedFieldInUnion(Field); |
| 5524 | |
| 5525 | // Build a compound literal constructing a value of the transparent |
| 5526 | // union type from this initializer list. |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5527 | TypeSourceInfo *unionTInfo = C.getTrivialTypeSourceInfo(UnionType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5528 | EResult = S.Owned( |
| 5529 | new (C) CompoundLiteralExpr(SourceLocation(), unionTInfo, UnionType, |
| 5530 | VK_RValue, Initializer, false)); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5531 | } |
| 5532 | |
| 5533 | Sema::AssignConvertType |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5534 | Sema::CheckTransparentUnionArgumentConstraints(QualType ArgType, |
| 5535 | ExprResult &rExpr) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5536 | QualType FromType = rExpr.get()->getType(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5537 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5538 | // 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] | 5539 | // transparent_union GCC extension. |
| 5540 | const RecordType *UT = ArgType->getAsUnionType(); |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 5541 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5542 | return Incompatible; |
| 5543 | |
| 5544 | // The field to initialize within the transparent union. |
| 5545 | RecordDecl *UD = UT->getDecl(); |
| 5546 | FieldDecl *InitField = 0; |
| 5547 | // It's compatible if the expression matches any of the fields. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5548 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 5549 | itend = UD->field_end(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5550 | it != itend; ++it) { |
| 5551 | if (it->getType()->isPointerType()) { |
| 5552 | // If the transparent union contains a pointer type, we allow: |
| 5553 | // 1) void pointer |
| 5554 | // 2) null pointer constant |
| 5555 | if (FromType->isPointerType()) |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 5556 | if (FromType->getAs<PointerType>()->getPointeeType()->isVoidType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5557 | rExpr = ImpCastExprToType(rExpr.take(), it->getType(), CK_BitCast); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5558 | InitField = *it; |
| 5559 | break; |
| 5560 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5561 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5562 | if (rExpr.get()->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 5563 | Expr::NPC_ValueDependentIsNull)) { |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5564 | rExpr = ImpCastExprToType(rExpr.take(), it->getType(), |
| 5565 | CK_NullToPointer); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5566 | InitField = *it; |
| 5567 | break; |
| 5568 | } |
| 5569 | } |
| 5570 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5571 | CastKind Kind = CK_Invalid; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5572 | if (CheckAssignmentConstraints(it->getType(), rExpr, Kind) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5573 | == Compatible) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5574 | rExpr = ImpCastExprToType(rExpr.take(), it->getType(), Kind); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5575 | InitField = *it; |
| 5576 | break; |
| 5577 | } |
| 5578 | } |
| 5579 | |
| 5580 | if (!InitField) |
| 5581 | return Incompatible; |
| 5582 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5583 | ConstructTransparentUnion(*this, Context, rExpr, ArgType, InitField); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5584 | return Compatible; |
| 5585 | } |
| 5586 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5587 | Sema::AssignConvertType |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5588 | Sema::CheckSingleAssignmentConstraints(QualType lhsType, ExprResult &rExpr) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5589 | if (getLangOptions().CPlusPlus) { |
| 5590 | if (!lhsType->isRecordType()) { |
| 5591 | // C++ 5.17p3: If the left operand is not of class type, the |
| 5592 | // expression is implicitly converted (C++ 4) to the |
| 5593 | // cv-unqualified type of the left operand. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5594 | ExprResult Res = PerformImplicitConversion(rExpr.get(), |
| 5595 | lhsType.getUnqualifiedType(), |
| 5596 | AA_Assigning); |
| 5597 | if (Res.isInvalid()) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5598 | return Incompatible; |
Fariborz Jahanian | 7a084ec | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 5599 | Sema::AssignConvertType result = Compatible; |
| 5600 | if (getLangOptions().ObjCAutoRefCount && |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5601 | !CheckObjCARCUnavailableWeakConversion(lhsType, |
| 5602 | rExpr.get()->getType())) |
Fariborz Jahanian | 7a084ec | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 5603 | result = IncompatibleObjCWeakRef; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5604 | rExpr = move(Res); |
Fariborz Jahanian | 7a084ec | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 5605 | return result; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5606 | } |
| 5607 | |
| 5608 | // FIXME: Currently, we fall through and treat C++ classes like C |
| 5609 | // structures. |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 5610 | } |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5611 | |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 5612 | // C99 6.5.16.1p1: the left operand is a pointer and the right is |
| 5613 | // a null pointer constant. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5614 | if ((lhsType->isPointerType() || |
| 5615 | lhsType->isObjCObjectPointerType() || |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5616 | lhsType->isBlockPointerType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5617 | && rExpr.get()->isNullPointerConstant(Context, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5618 | Expr::NPC_ValueDependentIsNull)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5619 | rExpr = ImpCastExprToType(rExpr.take(), lhsType, CK_NullToPointer); |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 5620 | return Compatible; |
| 5621 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5622 | |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 5623 | // This check seems unnatural, however it is necessary to ensure the proper |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 5624 | // conversion of functions/arrays. If the conversion were done for all |
Douglas Gregor | 02a24ee | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 5625 | // DeclExpr's (created by ActOnIdExpression), it would mess up the unary |
Nick Lewycky | c133e9e | 2010-08-05 06:27:49 +0000 | [diff] [blame] | 5626 | // expressions that suppress this implicit conversion (&, sizeof). |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 5627 | // |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5628 | // Suppress this for references: C++ 8.5.3p5. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5629 | if (!lhsType->isReferenceType()) { |
| 5630 | rExpr = DefaultFunctionArrayLvalueConversion(rExpr.take()); |
| 5631 | if (rExpr.isInvalid()) |
| 5632 | return Incompatible; |
| 5633 | } |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 5634 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5635 | CastKind Kind = CK_Invalid; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5636 | Sema::AssignConvertType result = |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5637 | CheckAssignmentConstraints(lhsType, rExpr, Kind); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5638 | |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 5639 | // C99 6.5.16.1p2: The value of the right operand is converted to the |
| 5640 | // type of the assignment expression. |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 5641 | // CheckAssignmentConstraints allows the left-hand side to be a reference, |
| 5642 | // so that we can use references in built-in functions even in C. |
| 5643 | // The getNonReferenceType() call makes sure that the resulting expression |
| 5644 | // does not have reference type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5645 | if (result != Incompatible && rExpr.get()->getType() != lhsType) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5646 | rExpr = ImpCastExprToType(rExpr.take(), |
| 5647 | lhsType.getNonLValueExprType(Context), Kind); |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 5648 | return result; |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 5649 | } |
| 5650 | |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5651 | QualType Sema::InvalidOperands(SourceLocation Loc, ExprResult &lex, |
| 5652 | ExprResult &rex) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 5653 | Diag(Loc, diag::err_typecheck_invalid_operands) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5654 | << lex.get()->getType() << rex.get()->getType() |
| 5655 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 5656 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5657 | } |
| 5658 | |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5659 | QualType Sema::CheckVectorOperands(ExprResult &lex, ExprResult &rex, |
| 5660 | SourceLocation Loc, bool isCompAssign) { |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5661 | // For conversion purposes, we ignore any qualifiers. |
Nate Begeman | 1330b0e | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 5662 | // For example, "const float" and "float" are equivalent. |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 5663 | QualType lhsType = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5664 | Context.getCanonicalType(lex.get()->getType()).getUnqualifiedType(); |
Chris Lattner | b77792e | 2008-07-26 22:17:49 +0000 | [diff] [blame] | 5665 | QualType rhsType = |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5666 | Context.getCanonicalType(rex.get()->getType()).getUnqualifiedType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5667 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5668 | // If the vector types are identical, return. |
Nate Begeman | 1330b0e | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 5669 | if (lhsType == rhsType) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5670 | return lhsType; |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 5671 | |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5672 | // Handle the case of equivalent AltiVec and GCC vector types |
| 5673 | if (lhsType->isVectorType() && rhsType->isVectorType() && |
| 5674 | Context.areCompatibleVectorTypes(lhsType, rhsType)) { |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5675 | if (lhsType->isExtVectorType()) { |
| 5676 | rex = ImpCastExprToType(rex.take(), lhsType, CK_BitCast); |
| 5677 | return lhsType; |
| 5678 | } |
| 5679 | |
| 5680 | if (!isCompAssign) |
| 5681 | lex = ImpCastExprToType(lex.take(), rhsType, CK_BitCast); |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5682 | return rhsType; |
| 5683 | } |
| 5684 | |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5685 | if (getLangOptions().LaxVectorConversions && |
| 5686 | Context.getTypeSize(lhsType) == Context.getTypeSize(rhsType)) { |
| 5687 | // If we are allowing lax vector conversions, and LHS and RHS are both |
| 5688 | // vectors, the total size only needs to be the same. This is a |
| 5689 | // bitcast; no bits are changed but the result type is different. |
| 5690 | // FIXME: Should we really be allowing this? |
| 5691 | rex = ImpCastExprToType(rex.take(), lhsType, CK_BitCast); |
| 5692 | return lhsType; |
| 5693 | } |
| 5694 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5695 | // Canonicalize the ExtVector to the LHS, remember if we swapped so we can |
| 5696 | // swap back (so that we don't reverse the inputs to a subtract, for instance. |
| 5697 | bool swapped = false; |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5698 | if (rhsType->isExtVectorType() && !isCompAssign) { |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5699 | swapped = true; |
| 5700 | std::swap(rex, lex); |
| 5701 | std::swap(rhsType, lhsType); |
| 5702 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5703 | |
Nate Begeman | dde2598 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 5704 | // Handle the case of an ext vector and scalar. |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 5705 | if (const ExtVectorType *LV = lhsType->getAs<ExtVectorType>()) { |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5706 | QualType EltTy = LV->getElementType(); |
Douglas Gregor | 9d3347a | 2010-06-16 00:35:25 +0000 | [diff] [blame] | 5707 | if (EltTy->isIntegralType(Context) && rhsType->isIntegralType(Context)) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5708 | int order = Context.getIntegerTypeOrder(EltTy, rhsType); |
| 5709 | if (order > 0) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5710 | rex = ImpCastExprToType(rex.take(), EltTy, CK_IntegralCast); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5711 | if (order >= 0) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5712 | rex = ImpCastExprToType(rex.take(), lhsType, CK_VectorSplat); |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5713 | if (swapped) std::swap(rex, lex); |
| 5714 | return lhsType; |
| 5715 | } |
| 5716 | } |
| 5717 | if (EltTy->isRealFloatingType() && rhsType->isScalarType() && |
| 5718 | rhsType->isRealFloatingType()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5719 | int order = Context.getFloatingTypeOrder(EltTy, rhsType); |
| 5720 | if (order > 0) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5721 | rex = ImpCastExprToType(rex.take(), EltTy, CK_FloatingCast); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5722 | if (order >= 0) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5723 | rex = ImpCastExprToType(rex.take(), lhsType, CK_VectorSplat); |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5724 | if (swapped) std::swap(rex, lex); |
| 5725 | return lhsType; |
| 5726 | } |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 5727 | } |
| 5728 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5729 | |
Nate Begeman | dde2598 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 5730 | // Vectors of different size or scalar and non-ext-vector are errors. |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5731 | if (swapped) std::swap(rex, lex); |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 5732 | Diag(Loc, diag::err_typecheck_vector_not_convertable) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5733 | << lex.get()->getType() << rex.get()->getType() |
| 5734 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5735 | return QualType(); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 5736 | } |
| 5737 | |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5738 | QualType Sema::CheckMultiplyDivideOperands(ExprResult &lex, ExprResult &rex, |
| 5739 | SourceLocation Loc, |
| 5740 | bool isCompAssign, bool isDiv) { |
| 5741 | if (lex.get()->getType()->isVectorType() || |
| 5742 | rex.get()->getType()->isVectorType()) |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5743 | return CheckVectorOperands(lex, rex, Loc, isCompAssign); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5744 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 5745 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5746 | if (lex.isInvalid() || rex.isInvalid()) |
| 5747 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5748 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5749 | if (!lex.get()->getType()->isArithmeticType() || |
| 5750 | !rex.get()->getType()->isArithmeticType()) |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5751 | return InvalidOperands(Loc, lex, rex); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5752 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5753 | // Check for division by zero. |
| 5754 | if (isDiv && |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5755 | rex.get()->isNullPointerConstant(Context, |
| 5756 | Expr::NPC_ValueDependentIsNotNull)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5757 | DiagRuntimeBehavior(Loc, rex.get(), PDiag(diag::warn_division_by_zero) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5758 | << rex.get()->getSourceRange()); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5759 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5760 | return compType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5761 | } |
| 5762 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5763 | QualType Sema::CheckRemainderOperands( |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5764 | ExprResult &lex, ExprResult &rex, SourceLocation Loc, bool isCompAssign) { |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5765 | if (lex.get()->getType()->isVectorType() || |
| 5766 | rex.get()->getType()->isVectorType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5767 | if (lex.get()->getType()->hasIntegerRepresentation() && |
| 5768 | rex.get()->getType()->hasIntegerRepresentation()) |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5769 | return CheckVectorOperands(lex, rex, Loc, isCompAssign); |
Daniel Dunbar | 523aa60 | 2009-01-05 22:55:36 +0000 | [diff] [blame] | 5770 | return InvalidOperands(Loc, lex, rex); |
| 5771 | } |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 5772 | |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 5773 | QualType compType = UsualArithmeticConversions(lex, rex, isCompAssign); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5774 | if (lex.isInvalid() || rex.isInvalid()) |
| 5775 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5776 | |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5777 | if (!lex.get()->getType()->isIntegerType() || |
| 5778 | !rex.get()->getType()->isIntegerType()) |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5779 | return InvalidOperands(Loc, lex, rex); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5780 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5781 | // Check for remainder by zero. |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5782 | if (rex.get()->isNullPointerConstant(Context, |
| 5783 | Expr::NPC_ValueDependentIsNotNull)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5784 | DiagRuntimeBehavior(Loc, rex.get(), PDiag(diag::warn_remainder_by_zero) |
| 5785 | << rex.get()->getSourceRange()); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5786 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5787 | return compType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5788 | } |
| 5789 | |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 5790 | /// \brief Diagnose invalid arithmetic on two void pointers. |
| 5791 | static void diagnoseArithmeticOnTwoVoidPointers(Sema &S, SourceLocation Loc, |
| 5792 | Expr *LHS, Expr *RHS) { |
| 5793 | S.Diag(Loc, S.getLangOptions().CPlusPlus |
| 5794 | ? diag::err_typecheck_pointer_arith_void_type |
| 5795 | : diag::ext_gnu_void_ptr) |
| 5796 | << 1 /* two pointers */ << LHS->getSourceRange() << RHS->getSourceRange(); |
| 5797 | } |
| 5798 | |
| 5799 | /// \brief Diagnose invalid arithmetic on a void pointer. |
| 5800 | static void diagnoseArithmeticOnVoidPointer(Sema &S, SourceLocation Loc, |
| 5801 | Expr *Pointer) { |
| 5802 | S.Diag(Loc, S.getLangOptions().CPlusPlus |
| 5803 | ? diag::err_typecheck_pointer_arith_void_type |
| 5804 | : diag::ext_gnu_void_ptr) |
| 5805 | << 0 /* one pointer */ << Pointer->getSourceRange(); |
| 5806 | } |
| 5807 | |
| 5808 | /// \brief Diagnose invalid arithmetic on two function pointers. |
| 5809 | static void diagnoseArithmeticOnTwoFunctionPointers(Sema &S, SourceLocation Loc, |
| 5810 | Expr *LHS, Expr *RHS) { |
| 5811 | assert(LHS->getType()->isAnyPointerType()); |
| 5812 | assert(RHS->getType()->isAnyPointerType()); |
| 5813 | S.Diag(Loc, S.getLangOptions().CPlusPlus |
| 5814 | ? diag::err_typecheck_pointer_arith_function_type |
| 5815 | : diag::ext_gnu_ptr_func_arith) |
| 5816 | << 1 /* two pointers */ << LHS->getType()->getPointeeType() |
| 5817 | // We only show the second type if it differs from the first. |
| 5818 | << (unsigned)!S.Context.hasSameUnqualifiedType(LHS->getType(), |
| 5819 | RHS->getType()) |
| 5820 | << RHS->getType()->getPointeeType() |
| 5821 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 5822 | } |
| 5823 | |
| 5824 | /// \brief Diagnose invalid arithmetic on a function pointer. |
| 5825 | static void diagnoseArithmeticOnFunctionPointer(Sema &S, SourceLocation Loc, |
| 5826 | Expr *Pointer) { |
| 5827 | assert(Pointer->getType()->isAnyPointerType()); |
| 5828 | S.Diag(Loc, S.getLangOptions().CPlusPlus |
| 5829 | ? diag::err_typecheck_pointer_arith_function_type |
| 5830 | : diag::ext_gnu_ptr_func_arith) |
| 5831 | << 0 /* one pointer */ << Pointer->getType()->getPointeeType() |
| 5832 | << 0 /* one pointer, so only one type */ |
| 5833 | << Pointer->getSourceRange(); |
| 5834 | } |
| 5835 | |
Richard Trieu | 097ecd2 | 2011-09-02 02:15:37 +0000 | [diff] [blame] | 5836 | /// \brief Warn if Operand is incomplete pointer type |
| 5837 | /// |
| 5838 | /// \returns True if pointer has incomplete type |
| 5839 | static bool checkArithmeticIncompletePointerType(Sema &S, SourceLocation Loc, |
| 5840 | Expr *Operand) { |
| 5841 | if ((Operand->getType()->isPointerType() && |
| 5842 | !Operand->getType()->isDependentType()) || |
| 5843 | Operand->getType()->isObjCObjectPointerType()) { |
| 5844 | QualType PointeeTy = Operand->getType()->getPointeeType(); |
| 5845 | if (S.RequireCompleteType( |
| 5846 | Loc, PointeeTy, |
| 5847 | S.PDiag(diag::err_typecheck_arithmetic_incomplete_type) |
| 5848 | << PointeeTy << Operand->getSourceRange())) |
| 5849 | return true; |
| 5850 | } |
| 5851 | return false; |
| 5852 | } |
| 5853 | |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 5854 | /// \brief Check the validity of an arithmetic pointer operand. |
| 5855 | /// |
| 5856 | /// If the operand has pointer type, this code will check for pointer types |
| 5857 | /// which are invalid in arithmetic operations. These will be diagnosed |
| 5858 | /// appropriately, including whether or not the use is supported as an |
| 5859 | /// extension. |
| 5860 | /// |
| 5861 | /// \returns True when the operand is valid to use (even if as an extension). |
| 5862 | static bool checkArithmeticOpPointerOperand(Sema &S, SourceLocation Loc, |
| 5863 | Expr *Operand) { |
| 5864 | if (!Operand->getType()->isAnyPointerType()) return true; |
| 5865 | |
| 5866 | QualType PointeeTy = Operand->getType()->getPointeeType(); |
| 5867 | if (PointeeTy->isVoidType()) { |
| 5868 | diagnoseArithmeticOnVoidPointer(S, Loc, Operand); |
| 5869 | return !S.getLangOptions().CPlusPlus; |
| 5870 | } |
| 5871 | if (PointeeTy->isFunctionType()) { |
| 5872 | diagnoseArithmeticOnFunctionPointer(S, Loc, Operand); |
| 5873 | return !S.getLangOptions().CPlusPlus; |
| 5874 | } |
| 5875 | |
Richard Trieu | 097ecd2 | 2011-09-02 02:15:37 +0000 | [diff] [blame] | 5876 | if (checkArithmeticIncompletePointerType(S, Loc, Operand)) return false; |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 5877 | |
| 5878 | return true; |
| 5879 | } |
| 5880 | |
| 5881 | /// \brief Check the validity of a binary arithmetic operation w.r.t. pointer |
| 5882 | /// operands. |
| 5883 | /// |
| 5884 | /// This routine will diagnose any invalid arithmetic on pointer operands much |
| 5885 | /// like \see checkArithmeticOpPointerOperand. However, it has special logic |
| 5886 | /// for emitting a single diagnostic even for operations where both LHS and RHS |
| 5887 | /// are (potentially problematic) pointers. |
| 5888 | /// |
| 5889 | /// \returns True when the operand is valid to use (even if as an extension). |
| 5890 | static bool checkArithmeticBinOpPointerOperands(Sema &S, SourceLocation Loc, |
| 5891 | Expr *LHS, Expr *RHS) { |
| 5892 | bool isLHSPointer = LHS->getType()->isAnyPointerType(); |
| 5893 | bool isRHSPointer = RHS->getType()->isAnyPointerType(); |
| 5894 | if (!isLHSPointer && !isRHSPointer) return true; |
| 5895 | |
| 5896 | QualType LHSPointeeTy, RHSPointeeTy; |
| 5897 | if (isLHSPointer) LHSPointeeTy = LHS->getType()->getPointeeType(); |
| 5898 | if (isRHSPointer) RHSPointeeTy = RHS->getType()->getPointeeType(); |
| 5899 | |
| 5900 | // Check for arithmetic on pointers to incomplete types. |
| 5901 | bool isLHSVoidPtr = isLHSPointer && LHSPointeeTy->isVoidType(); |
| 5902 | bool isRHSVoidPtr = isRHSPointer && RHSPointeeTy->isVoidType(); |
| 5903 | if (isLHSVoidPtr || isRHSVoidPtr) { |
| 5904 | if (!isRHSVoidPtr) diagnoseArithmeticOnVoidPointer(S, Loc, LHS); |
| 5905 | else if (!isLHSVoidPtr) diagnoseArithmeticOnVoidPointer(S, Loc, RHS); |
| 5906 | else diagnoseArithmeticOnTwoVoidPointers(S, Loc, LHS, RHS); |
| 5907 | |
| 5908 | return !S.getLangOptions().CPlusPlus; |
| 5909 | } |
| 5910 | |
| 5911 | bool isLHSFuncPtr = isLHSPointer && LHSPointeeTy->isFunctionType(); |
| 5912 | bool isRHSFuncPtr = isRHSPointer && RHSPointeeTy->isFunctionType(); |
| 5913 | if (isLHSFuncPtr || isRHSFuncPtr) { |
| 5914 | if (!isRHSFuncPtr) diagnoseArithmeticOnFunctionPointer(S, Loc, LHS); |
| 5915 | else if (!isLHSFuncPtr) diagnoseArithmeticOnFunctionPointer(S, Loc, RHS); |
| 5916 | else diagnoseArithmeticOnTwoFunctionPointers(S, Loc, LHS, RHS); |
| 5917 | |
| 5918 | return !S.getLangOptions().CPlusPlus; |
| 5919 | } |
| 5920 | |
Richard Trieu | 097ecd2 | 2011-09-02 02:15:37 +0000 | [diff] [blame] | 5921 | if (checkArithmeticIncompletePointerType(S, Loc, LHS)) return false; |
| 5922 | if (checkArithmeticIncompletePointerType(S, Loc, RHS)) return false; |
| 5923 | |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 5924 | return true; |
| 5925 | } |
| 5926 | |
Richard Trieu | db44a6b | 2011-09-01 22:53:23 +0000 | [diff] [blame] | 5927 | /// \brief Check bad cases where we step over interface counts. |
| 5928 | static bool checkArithmethicPointerOnNonFragileABI(Sema &S, |
| 5929 | SourceLocation OpLoc, |
| 5930 | Expr *Op) { |
| 5931 | assert(Op->getType()->isAnyPointerType()); |
| 5932 | QualType PointeeTy = Op->getType()->getPointeeType(); |
| 5933 | if (!PointeeTy->isObjCObjectType() || !S.LangOpts.ObjCNonFragileABI) |
| 5934 | return true; |
| 5935 | |
| 5936 | S.Diag(OpLoc, diag::err_arithmetic_nonfragile_interface) |
| 5937 | << PointeeTy << Op->getSourceRange(); |
| 5938 | return false; |
| 5939 | } |
| 5940 | |
| 5941 | /// \brief Warn when two pointers are incompatible. |
| 5942 | static void diagnosePointerIncompatibility(Sema &S, SourceLocation Loc, |
| 5943 | Expr *LHS, Expr *RHS) { |
| 5944 | assert(LHS->getType()->isAnyPointerType()); |
| 5945 | assert(RHS->getType()->isAnyPointerType()); |
| 5946 | S.Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
| 5947 | << LHS->getType() << RHS->getType() << LHS->getSourceRange() |
| 5948 | << RHS->getSourceRange(); |
| 5949 | } |
| 5950 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 5951 | QualType Sema::CheckAdditionOperands( // C99 6.5.6 |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5952 | ExprResult &lex, ExprResult &rex, SourceLocation Loc, QualType* CompLHSTy) { |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5953 | if (lex.get()->getType()->isVectorType() || |
| 5954 | rex.get()->getType()->isVectorType()) { |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 5955 | QualType compType = CheckVectorOperands(lex, rex, Loc, CompLHSTy); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5956 | if (CompLHSTy) *CompLHSTy = compType; |
| 5957 | return compType; |
| 5958 | } |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 5959 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5960 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5961 | if (lex.isInvalid() || rex.isInvalid()) |
| 5962 | return QualType(); |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 5963 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5964 | // handle the common case first (both operands are arithmetic). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5965 | if (lex.get()->getType()->isArithmeticType() && |
| 5966 | rex.get()->getType()->isArithmeticType()) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5967 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 5968 | return compType; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5969 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5970 | |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 5971 | // Put any potential pointer into PExp |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5972 | Expr* PExp = lex.get(), *IExp = rex.get(); |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 5973 | if (IExp->getType()->isAnyPointerType()) |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 5974 | std::swap(PExp, IExp); |
| 5975 | |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 5976 | if (PExp->getType()->isAnyPointerType()) { |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 5977 | if (IExp->getType()->isIntegerType()) { |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 5978 | if (!checkArithmeticOpPointerOperand(*this, Loc, PExp)) |
| 5979 | return QualType(); |
| 5980 | |
Steve Naroff | 760e3c4 | 2009-07-13 21:20:41 +0000 | [diff] [blame] | 5981 | QualType PointeeTy = PExp->getType()->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5982 | |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 5983 | // Diagnose bad cases where we step over interface counts. |
Richard Trieu | db44a6b | 2011-09-01 22:53:23 +0000 | [diff] [blame] | 5984 | if (!checkArithmethicPointerOnNonFragileABI(*this, Loc, PExp)) |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 5985 | return QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5986 | |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 5987 | // Check array bounds for pointer arithemtic |
| 5988 | CheckArrayAccess(PExp, IExp); |
| 5989 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5990 | if (CompLHSTy) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5991 | QualType LHSTy = Context.isPromotableBitField(lex.get()); |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 5992 | if (LHSTy.isNull()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5993 | LHSTy = lex.get()->getType(); |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 5994 | if (LHSTy->isPromotableIntegerType()) |
| 5995 | LHSTy = Context.getPromotedIntegerType(LHSTy); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 5996 | } |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 5997 | *CompLHSTy = LHSTy; |
| 5998 | } |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 5999 | return PExp->getType(); |
| 6000 | } |
| 6001 | } |
| 6002 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6003 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6004 | } |
| 6005 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 6006 | // C99 6.5.6 |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6007 | QualType Sema::CheckSubtractionOperands(ExprResult &lex, ExprResult &rex, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6008 | SourceLocation Loc, |
| 6009 | QualType* CompLHSTy) { |
| 6010 | if (lex.get()->getType()->isVectorType() || |
| 6011 | rex.get()->getType()->isVectorType()) { |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 6012 | QualType compType = CheckVectorOperands(lex, rex, Loc, CompLHSTy); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6013 | if (CompLHSTy) *CompLHSTy = compType; |
| 6014 | return compType; |
| 6015 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6016 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6017 | QualType compType = UsualArithmeticConversions(lex, rex, CompLHSTy); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6018 | if (lex.isInvalid() || rex.isInvalid()) |
| 6019 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6020 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6021 | // Enforce type constraints: C99 6.5.6p3. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6022 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6023 | // Handle the common case first (both operands are arithmetic). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6024 | if (lex.get()->getType()->isArithmeticType() && |
| 6025 | rex.get()->getType()->isArithmeticType()) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6026 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 6027 | return compType; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6028 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6029 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6030 | // Either ptr - int or ptr - ptr. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6031 | if (lex.get()->getType()->isAnyPointerType()) { |
| 6032 | QualType lpointee = lex.get()->getType()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6033 | |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 6034 | // Diagnose bad cases where we step over interface counts. |
Richard Trieu | db44a6b | 2011-09-01 22:53:23 +0000 | [diff] [blame] | 6035 | if (!checkArithmethicPointerOnNonFragileABI(*this, Loc, lex.get())) |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 6036 | return QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6037 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6038 | // The result type of a pointer-int computation is the pointer type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6039 | if (rex.get()->getType()->isIntegerType()) { |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6040 | if (!checkArithmeticOpPointerOperand(*this, Loc, lex.get())) |
| 6041 | return QualType(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6042 | |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 6043 | Expr *IExpr = rex.get()->IgnoreParenCasts(); |
| 6044 | UnaryOperator negRex(IExpr, UO_Minus, IExpr->getType(), VK_RValue, |
| 6045 | OK_Ordinary, IExpr->getExprLoc()); |
| 6046 | // Check array bounds for pointer arithemtic |
| 6047 | CheckArrayAccess(lex.get()->IgnoreParenCasts(), &negRex); |
| 6048 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6049 | if (CompLHSTy) *CompLHSTy = lex.get()->getType(); |
| 6050 | return lex.get()->getType(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6051 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6052 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6053 | // Handle pointer-pointer subtractions. |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6054 | if (const PointerType *RHSPTy |
| 6055 | = rex.get()->getType()->getAs<PointerType>()) { |
Eli Friedman | 8e54ad0 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 6056 | QualType rpointee = RHSPTy->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6057 | |
Eli Friedman | 88d936b | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 6058 | if (getLangOptions().CPlusPlus) { |
| 6059 | // Pointee types must be the same: C++ [expr.add] |
| 6060 | if (!Context.hasSameUnqualifiedType(lpointee, rpointee)) { |
Richard Trieu | db44a6b | 2011-09-01 22:53:23 +0000 | [diff] [blame] | 6061 | diagnosePointerIncompatibility(*this, Loc, lex.get(), rex.get()); |
Eli Friedman | 88d936b | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 6062 | } |
| 6063 | } else { |
| 6064 | // Pointee types must be compatible C99 6.5.6p3 |
| 6065 | if (!Context.typesAreCompatible( |
| 6066 | Context.getCanonicalType(lpointee).getUnqualifiedType(), |
| 6067 | Context.getCanonicalType(rpointee).getUnqualifiedType())) { |
Richard Trieu | db44a6b | 2011-09-01 22:53:23 +0000 | [diff] [blame] | 6068 | diagnosePointerIncompatibility(*this, Loc, lex.get(), rex.get()); |
Eli Friedman | 88d936b | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 6069 | return QualType(); |
| 6070 | } |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6071 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6072 | |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6073 | if (!checkArithmeticBinOpPointerOperands(*this, Loc, |
| 6074 | lex.get(), rex.get())) |
| 6075 | return QualType(); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6076 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6077 | if (CompLHSTy) *CompLHSTy = lex.get()->getType(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6078 | return Context.getPointerDiffType(); |
| 6079 | } |
| 6080 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6081 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6082 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6083 | } |
| 6084 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6085 | static bool isScopedEnumerationType(QualType T) { |
| 6086 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
| 6087 | return ET->getDecl()->isScoped(); |
| 6088 | return false; |
| 6089 | } |
| 6090 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6091 | static void DiagnoseBadShiftValues(Sema& S, ExprResult &lex, ExprResult &rex, |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6092 | SourceLocation Loc, unsigned Opc, |
| 6093 | QualType LHSTy) { |
| 6094 | llvm::APSInt Right; |
| 6095 | // Check right/shifter operand |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6096 | if (rex.get()->isValueDependent() || |
| 6097 | !rex.get()->isIntegerConstantExpr(Right, S.Context)) |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6098 | return; |
| 6099 | |
| 6100 | if (Right.isNegative()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6101 | S.DiagRuntimeBehavior(Loc, rex.get(), |
Ted Kremenek | 082bf7a | 2011-03-01 18:09:31 +0000 | [diff] [blame] | 6102 | S.PDiag(diag::warn_shift_negative) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6103 | << rex.get()->getSourceRange()); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6104 | return; |
| 6105 | } |
| 6106 | llvm::APInt LeftBits(Right.getBitWidth(), |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6107 | S.Context.getTypeSize(lex.get()->getType())); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6108 | if (Right.uge(LeftBits)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6109 | S.DiagRuntimeBehavior(Loc, rex.get(), |
Ted Kremenek | 425a31e | 2011-03-01 19:13:22 +0000 | [diff] [blame] | 6110 | S.PDiag(diag::warn_shift_gt_typewidth) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6111 | << rex.get()->getSourceRange()); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6112 | return; |
| 6113 | } |
| 6114 | if (Opc != BO_Shl) |
| 6115 | return; |
| 6116 | |
| 6117 | // When left shifting an ICE which is signed, we can check for overflow which |
| 6118 | // according to C++ has undefined behavior ([expr.shift] 5.8/2). Unsigned |
| 6119 | // integers have defined behavior modulo one more than the maximum value |
| 6120 | // representable in the result type, so never warn for those. |
| 6121 | llvm::APSInt Left; |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6122 | if (lex.get()->isValueDependent() || |
| 6123 | !lex.get()->isIntegerConstantExpr(Left, S.Context) || |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6124 | LHSTy->hasUnsignedIntegerRepresentation()) |
| 6125 | return; |
| 6126 | llvm::APInt ResultBits = |
| 6127 | static_cast<llvm::APInt&>(Right) + Left.getMinSignedBits(); |
| 6128 | if (LeftBits.uge(ResultBits)) |
| 6129 | return; |
| 6130 | llvm::APSInt Result = Left.extend(ResultBits.getLimitedValue()); |
| 6131 | Result = Result.shl(Right); |
| 6132 | |
Ted Kremenek | fa82138 | 2011-06-15 00:54:52 +0000 | [diff] [blame] | 6133 | // Print the bit representation of the signed integer as an unsigned |
| 6134 | // hexadecimal number. |
| 6135 | llvm::SmallString<40> HexResult; |
| 6136 | Result.toString(HexResult, 16, /*Signed =*/false, /*Literal =*/true); |
| 6137 | |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6138 | // If we are only missing a sign bit, this is less likely to result in actual |
| 6139 | // bugs -- if the result is cast back to an unsigned type, it will have the |
| 6140 | // expected value. Thus we place this behind a different warning that can be |
| 6141 | // turned off separately if needed. |
| 6142 | if (LeftBits == ResultBits - 1) { |
Ted Kremenek | fa82138 | 2011-06-15 00:54:52 +0000 | [diff] [blame] | 6143 | S.Diag(Loc, diag::warn_shift_result_sets_sign_bit) |
| 6144 | << HexResult.str() << LHSTy |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6145 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6146 | return; |
| 6147 | } |
| 6148 | |
| 6149 | S.Diag(Loc, diag::warn_shift_result_gt_typewidth) |
Ted Kremenek | fa82138 | 2011-06-15 00:54:52 +0000 | [diff] [blame] | 6150 | << HexResult.str() << Result.getMinSignedBits() << LHSTy |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6151 | << Left.getBitWidth() << lex.get()->getSourceRange() |
| 6152 | << rex.get()->getSourceRange(); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6153 | } |
| 6154 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 6155 | // C99 6.5.7 |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6156 | QualType Sema::CheckShiftOperands(ExprResult &lex, ExprResult &rex, |
| 6157 | SourceLocation Loc, unsigned Opc, |
| 6158 | bool isCompAssign) { |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6159 | // C99 6.5.7p2: Each of the operands shall have integer type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6160 | if (!lex.get()->getType()->hasIntegerRepresentation() || |
| 6161 | !rex.get()->getType()->hasIntegerRepresentation()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6162 | return InvalidOperands(Loc, lex, rex); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6163 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6164 | // C++0x: Don't allow scoped enums. FIXME: Use something better than |
| 6165 | // hasIntegerRepresentation() above instead of this. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6166 | if (isScopedEnumerationType(lex.get()->getType()) || |
| 6167 | isScopedEnumerationType(rex.get()->getType())) { |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6168 | return InvalidOperands(Loc, lex, rex); |
| 6169 | } |
| 6170 | |
Nate Begeman | 2207d79 | 2009-10-25 02:26:48 +0000 | [diff] [blame] | 6171 | // Vector shifts promote their scalar inputs to vector type. |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6172 | if (lex.get()->getType()->isVectorType() || |
| 6173 | rex.get()->getType()->isVectorType()) |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 6174 | return CheckVectorOperands(lex, rex, Loc, isCompAssign); |
Nate Begeman | 2207d79 | 2009-10-25 02:26:48 +0000 | [diff] [blame] | 6175 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6176 | // Shifts don't perform usual arithmetic conversions, they just do integer |
| 6177 | // promotions on each operand. C99 6.5.7p3 |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6178 | |
John McCall | 1bc80af | 2010-12-16 19:28:59 +0000 | [diff] [blame] | 6179 | // For the LHS, do usual unary conversions, but then reset them away |
| 6180 | // if this is a compound assignment. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6181 | ExprResult old_lex = lex; |
| 6182 | lex = UsualUnaryConversions(lex.take()); |
| 6183 | if (lex.isInvalid()) |
| 6184 | return QualType(); |
| 6185 | QualType LHSTy = lex.get()->getType(); |
John McCall | 1bc80af | 2010-12-16 19:28:59 +0000 | [diff] [blame] | 6186 | if (isCompAssign) lex = old_lex; |
| 6187 | |
| 6188 | // The RHS is simpler. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6189 | rex = UsualUnaryConversions(rex.take()); |
| 6190 | if (rex.isInvalid()) |
| 6191 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6192 | |
Ryan Flynn | d043968 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 6193 | // Sanity-check shift operands |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6194 | DiagnoseBadShiftValues(*this, lex, rex, Loc, Opc, LHSTy); |
Ryan Flynn | d043968 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 6195 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6196 | // "The type of the result is that of the promoted left operand." |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6197 | return LHSTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6198 | } |
| 6199 | |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6200 | static bool IsWithinTemplateSpecialization(Decl *D) { |
| 6201 | if (DeclContext *DC = D->getDeclContext()) { |
| 6202 | if (isa<ClassTemplateSpecializationDecl>(DC)) |
| 6203 | return true; |
| 6204 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) |
| 6205 | return FD->isFunctionTemplateSpecialization(); |
| 6206 | } |
| 6207 | return false; |
| 6208 | } |
| 6209 | |
Richard Trieu | e648ac3 | 2011-09-02 03:48:46 +0000 | [diff] [blame] | 6210 | /// If two different enums are compared, raise a warning. |
| 6211 | static void checkEnumComparison(Sema &S, SourceLocation Loc, ExprResult &lex, |
| 6212 | ExprResult &rex) { |
| 6213 | QualType lType = lex.get()->getType(); |
| 6214 | QualType rType = rex.get()->getType(); |
| 6215 | QualType LHSStrippedType = lex.get()->IgnoreParenImpCasts()->getType(); |
| 6216 | QualType RHSStrippedType = rex.get()->IgnoreParenImpCasts()->getType(); |
| 6217 | |
| 6218 | const EnumType *LHSEnumType = LHSStrippedType->getAs<EnumType>(); |
| 6219 | if (!LHSEnumType) |
| 6220 | return; |
| 6221 | const EnumType *RHSEnumType = RHSStrippedType->getAs<EnumType>(); |
| 6222 | if (!RHSEnumType) |
| 6223 | return; |
| 6224 | |
| 6225 | // Ignore anonymous enums. |
| 6226 | if (!LHSEnumType->getDecl()->getIdentifier()) |
| 6227 | return; |
| 6228 | if (!RHSEnumType->getDecl()->getIdentifier()) |
| 6229 | return; |
| 6230 | |
| 6231 | if (S.Context.hasSameUnqualifiedType(LHSStrippedType, RHSStrippedType)) |
| 6232 | return; |
| 6233 | |
| 6234 | S.Diag(Loc, diag::warn_comparison_of_mixed_enum_types) |
| 6235 | << LHSStrippedType << RHSStrippedType |
| 6236 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
| 6237 | } |
| 6238 | |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6239 | /// \brief Diagnose bad pointer comparisons. |
| 6240 | static void diagnoseDistinctPointerComparison(Sema &S, SourceLocation Loc, |
| 6241 | ExprResult &lex, ExprResult &rex, |
| 6242 | bool isError) { |
| 6243 | S.Diag(Loc, isError ? diag::err_typecheck_comparison_of_distinct_pointers |
| 6244 | : diag::ext_typecheck_comparison_of_distinct_pointers) |
| 6245 | << lex.get()->getType() << rex.get()->getType() |
| 6246 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
| 6247 | } |
| 6248 | |
| 6249 | /// \brief Returns false if the pointers are converted to a composite type, |
| 6250 | /// true otherwise. |
| 6251 | static bool convertPointersToCompositeType(Sema &S, SourceLocation Loc, |
Richard Trieu | 43dff1b | 2011-09-02 21:44:27 +0000 | [diff] [blame] | 6252 | ExprResult &lex, ExprResult &rex) { |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6253 | // C++ [expr.rel]p2: |
| 6254 | // [...] Pointer conversions (4.10) and qualification |
| 6255 | // conversions (4.4) are performed on pointer operands (or on |
| 6256 | // a pointer operand and a null pointer constant) to bring |
| 6257 | // them to their composite pointer type. [...] |
| 6258 | // |
| 6259 | // C++ [expr.eq]p1 uses the same notion for (in)equality |
| 6260 | // comparisons of pointers. |
| 6261 | |
| 6262 | // C++ [expr.eq]p2: |
| 6263 | // In addition, pointers to members can be compared, or a pointer to |
| 6264 | // member and a null pointer constant. Pointer to member conversions |
| 6265 | // (4.11) and qualification conversions (4.4) are performed to bring |
| 6266 | // them to a common type. If one operand is a null pointer constant, |
| 6267 | // the common type is the type of the other operand. Otherwise, the |
| 6268 | // common type is a pointer to member type similar (4.4) to the type |
| 6269 | // of one of the operands, with a cv-qualification signature (4.4) |
| 6270 | // that is the union of the cv-qualification signatures of the operand |
| 6271 | // types. |
| 6272 | |
| 6273 | QualType lType = lex.get()->getType(); |
| 6274 | QualType rType = rex.get()->getType(); |
| 6275 | assert((lType->isPointerType() && rType->isPointerType()) || |
| 6276 | (lType->isMemberPointerType() && rType->isMemberPointerType())); |
| 6277 | |
| 6278 | bool NonStandardCompositeType = false; |
Richard Trieu | 43dff1b | 2011-09-02 21:44:27 +0000 | [diff] [blame] | 6279 | bool *BoolPtr = S.isSFINAEContext() ? 0 : &NonStandardCompositeType; |
| 6280 | QualType T = S.FindCompositePointerType(Loc, lex, rex, BoolPtr); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6281 | if (T.isNull()) { |
| 6282 | diagnoseDistinctPointerComparison(S, Loc, lex, rex, /*isError*/true); |
| 6283 | return true; |
| 6284 | } |
| 6285 | |
| 6286 | if (NonStandardCompositeType) |
| 6287 | S.Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers_nonstandard) |
| 6288 | << lType << rType << T << lex.get()->getSourceRange() |
| 6289 | << rex.get()->getSourceRange(); |
| 6290 | |
| 6291 | lex = S.ImpCastExprToType(lex.take(), T, CK_BitCast); |
| 6292 | rex = S.ImpCastExprToType(rex.take(), T, CK_BitCast); |
| 6293 | return false; |
| 6294 | } |
| 6295 | |
| 6296 | static void diagnoseFunctionPointerToVoidComparison(Sema &S, SourceLocation Loc, |
| 6297 | ExprResult &lex, |
| 6298 | ExprResult &rex, |
| 6299 | bool isError) { |
| 6300 | S.Diag(Loc,isError ? diag::err_typecheck_comparison_of_fptr_to_void |
| 6301 | : diag::ext_typecheck_comparison_of_fptr_to_void) |
| 6302 | << lex.get()->getType() << rex.get()->getType() |
| 6303 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
| 6304 | } |
| 6305 | |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6306 | // C99 6.5.8, C++ [expr.rel] |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6307 | QualType Sema::CheckCompareOperands(ExprResult &lex, ExprResult &rex, |
| 6308 | SourceLocation Loc, unsigned OpaqueOpc, |
| 6309 | bool isRelational) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6310 | BinaryOperatorKind Opc = (BinaryOperatorKind) OpaqueOpc; |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6311 | |
Chris Lattner | 02dd4b1 | 2009-12-05 05:40:13 +0000 | [diff] [blame] | 6312 | // Handle vector comparisons separately. |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6313 | if (lex.get()->getType()->isVectorType() || |
| 6314 | rex.get()->getType()->isVectorType()) |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6315 | return CheckVectorCompareOperands(lex, rex, Loc, isRelational); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6316 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6317 | QualType lType = lex.get()->getType(); |
| 6318 | QualType rType = rex.get()->getType(); |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 6319 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6320 | Expr *LHSStripped = lex.get()->IgnoreParenImpCasts(); |
| 6321 | Expr *RHSStripped = rex.get()->IgnoreParenImpCasts(); |
Chandler Carruth | 543cb65 | 2011-02-17 08:37:06 +0000 | [diff] [blame] | 6322 | QualType LHSStrippedType = LHSStripped->getType(); |
| 6323 | QualType RHSStrippedType = RHSStripped->getType(); |
| 6324 | |
Richard Trieu | e648ac3 | 2011-09-02 03:48:46 +0000 | [diff] [blame] | 6325 | checkEnumComparison(*this, Loc, lex, rex); |
Chandler Carruth | 543cb65 | 2011-02-17 08:37:06 +0000 | [diff] [blame] | 6326 | |
Douglas Gregor | 8eee119 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 6327 | if (!lType->hasFloatingRepresentation() && |
Ted Kremenek | fbcb0eb | 2010-09-16 00:03:01 +0000 | [diff] [blame] | 6328 | !(lType->isBlockPointerType() && isRelational) && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6329 | !lex.get()->getLocStart().isMacroID() && |
| 6330 | !rex.get()->getLocStart().isMacroID()) { |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6331 | // For non-floating point types, check for self-comparisons of the form |
| 6332 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 6333 | // often indicate logic errors in the program. |
Chandler Carruth | 64d092c | 2010-07-12 06:23:38 +0000 | [diff] [blame] | 6334 | // |
| 6335 | // NOTE: Don't warn about comparison expressions resulting from macro |
| 6336 | // expansion. Also don't warn about comparisons which are only self |
| 6337 | // comparisons within a template specialization. The warnings should catch |
| 6338 | // obvious cases in the definition of the template anyways. The idea is to |
| 6339 | // warn when the typed comparison operator will always evaluate to the same |
| 6340 | // result. |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6341 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LHSStripped)) { |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6342 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RHSStripped)) { |
Ted Kremenek | fbcb0eb | 2010-09-16 00:03:01 +0000 | [diff] [blame] | 6343 | if (DRL->getDecl() == DRR->getDecl() && |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6344 | !IsWithinTemplateSpecialization(DRL->getDecl())) { |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 6345 | DiagRuntimeBehavior(Loc, 0, PDiag(diag::warn_comparison_always) |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6346 | << 0 // self- |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6347 | << (Opc == BO_EQ |
| 6348 | || Opc == BO_LE |
| 6349 | || Opc == BO_GE)); |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6350 | } else if (lType->isArrayType() && rType->isArrayType() && |
| 6351 | !DRL->getDecl()->getType()->isReferenceType() && |
| 6352 | !DRR->getDecl()->getType()->isReferenceType()) { |
| 6353 | // what is it always going to eval to? |
| 6354 | char always_evals_to; |
| 6355 | switch(Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6356 | case BO_EQ: // e.g. array1 == array2 |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6357 | always_evals_to = 0; // false |
| 6358 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6359 | case BO_NE: // e.g. array1 != array2 |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6360 | always_evals_to = 1; // true |
| 6361 | break; |
| 6362 | default: |
| 6363 | // best we can say is 'a constant' |
| 6364 | always_evals_to = 2; // e.g. array1 <= array2 |
| 6365 | break; |
| 6366 | } |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 6367 | DiagRuntimeBehavior(Loc, 0, PDiag(diag::warn_comparison_always) |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6368 | << 1 // array |
| 6369 | << always_evals_to); |
| 6370 | } |
| 6371 | } |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6372 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6373 | |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6374 | if (isa<CastExpr>(LHSStripped)) |
| 6375 | LHSStripped = LHSStripped->IgnoreParenCasts(); |
| 6376 | if (isa<CastExpr>(RHSStripped)) |
| 6377 | RHSStripped = RHSStripped->IgnoreParenCasts(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6378 | |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6379 | // Warn about comparisons against a string constant (unless the other |
| 6380 | // operand is null), the user probably wants strcmp. |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6381 | Expr *literalString = 0; |
| 6382 | Expr *literalStringStripped = 0; |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6383 | if ((isa<StringLiteral>(LHSStripped) || isa<ObjCEncodeExpr>(LHSStripped)) && |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6384 | !RHSStripped->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6385 | Expr::NPC_ValueDependentIsNull)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6386 | literalString = lex.get(); |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6387 | literalStringStripped = LHSStripped; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 6388 | } else if ((isa<StringLiteral>(RHSStripped) || |
| 6389 | isa<ObjCEncodeExpr>(RHSStripped)) && |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6390 | !LHSStripped->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6391 | Expr::NPC_ValueDependentIsNull)) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6392 | literalString = rex.get(); |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6393 | literalStringStripped = RHSStripped; |
| 6394 | } |
| 6395 | |
| 6396 | if (literalString) { |
| 6397 | std::string resultComparison; |
| 6398 | switch (Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6399 | case BO_LT: resultComparison = ") < 0"; break; |
| 6400 | case BO_GT: resultComparison = ") > 0"; break; |
| 6401 | case BO_LE: resultComparison = ") <= 0"; break; |
| 6402 | case BO_GE: resultComparison = ") >= 0"; break; |
| 6403 | case BO_EQ: resultComparison = ") == 0"; break; |
| 6404 | case BO_NE: resultComparison = ") != 0"; break; |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6405 | default: assert(false && "Invalid comparison operator"); |
| 6406 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6407 | |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 6408 | DiagRuntimeBehavior(Loc, 0, |
Douglas Gregor | d1e4d9b | 2010-01-12 23:18:54 +0000 | [diff] [blame] | 6409 | PDiag(diag::warn_stringcompare) |
| 6410 | << isa<ObjCEncodeExpr>(literalStringStripped) |
Ted Kremenek | 03a4bee | 2010-04-09 20:26:53 +0000 | [diff] [blame] | 6411 | << literalString->getSourceRange()); |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6412 | } |
Ted Kremenek | 3ca0bf2 | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 6413 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6414 | |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6415 | // C99 6.5.8p3 / C99 6.5.9p4 |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6416 | if (lex.get()->getType()->isArithmeticType() && |
| 6417 | rex.get()->getType()->isArithmeticType()) { |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6418 | UsualArithmeticConversions(lex, rex); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6419 | if (lex.isInvalid() || rex.isInvalid()) |
| 6420 | return QualType(); |
| 6421 | } |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6422 | else { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6423 | lex = UsualUnaryConversions(lex.take()); |
| 6424 | if (lex.isInvalid()) |
| 6425 | return QualType(); |
| 6426 | |
| 6427 | rex = UsualUnaryConversions(rex.take()); |
| 6428 | if (rex.isInvalid()) |
| 6429 | return QualType(); |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6430 | } |
| 6431 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6432 | lType = lex.get()->getType(); |
| 6433 | rType = rex.get()->getType(); |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6434 | |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6435 | // The result of comparisons is 'bool' in C++, 'int' in C. |
Argyrios Kyrtzidis | 16f744b | 2011-02-18 20:55:15 +0000 | [diff] [blame] | 6436 | QualType ResultTy = Context.getLogicalOperationType(); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6437 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 6438 | if (isRelational) { |
| 6439 | if (lType->isRealType() && rType->isRealType()) |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6440 | return ResultTy; |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 6441 | } else { |
Ted Kremenek | 72cb1ae | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 6442 | // Check for comparisons of floating point operands using != and ==. |
Douglas Gregor | 8eee119 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 6443 | if (lType->hasFloatingRepresentation()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6444 | CheckFloatComparison(Loc, lex.get(), rex.get()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6445 | |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 6446 | if (lType->isArithmeticType() && rType->isArithmeticType()) |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6447 | return ResultTy; |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 6448 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6449 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6450 | bool LHSIsNull = lex.get()->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6451 | Expr::NPC_ValueDependentIsNull); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6452 | bool RHSIsNull = rex.get()->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6453 | Expr::NPC_ValueDependentIsNull); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6454 | |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6455 | // All of the following pointer-related warnings are GCC extensions, except |
| 6456 | // when handling null pointer constants. |
Steve Naroff | 77878cc | 2007-08-27 04:08:11 +0000 | [diff] [blame] | 6457 | if (lType->isPointerType() && rType->isPointerType()) { // C99 6.5.8p2 |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 6458 | QualType LCanPointeeTy = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6459 | Context.getCanonicalType(lType->getAs<PointerType>()->getPointeeType()); |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 6460 | QualType RCanPointeeTy = |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6461 | Context.getCanonicalType(rType->getAs<PointerType>()->getPointeeType()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6462 | |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6463 | if (getLangOptions().CPlusPlus) { |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 6464 | if (LCanPointeeTy == RCanPointeeTy) |
| 6465 | return ResultTy; |
Fariborz Jahanian | 51874dd | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 6466 | if (!isRelational && |
| 6467 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 6468 | // Valid unless comparison between non-null pointer and function pointer |
| 6469 | // This is a gcc extension compatibility comparison. |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6470 | // In a SFINAE context, we treat this as a hard error to maintain |
| 6471 | // conformance with the C++ standard. |
Fariborz Jahanian | 51874dd | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 6472 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
| 6473 | && !LHSIsNull && !RHSIsNull) { |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6474 | diagnoseFunctionPointerToVoidComparison( |
| 6475 | *this, Loc, lex, rex, /*isError*/ isSFINAEContext()); |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6476 | |
| 6477 | if (isSFINAEContext()) |
| 6478 | return QualType(); |
| 6479 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6480 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
Fariborz Jahanian | 51874dd | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 6481 | return ResultTy; |
| 6482 | } |
| 6483 | } |
Anders Carlsson | 0c8209e | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 6484 | |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6485 | if (convertPointersToCompositeType(*this, Loc, lex, rex)) |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6486 | return QualType(); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6487 | else |
| 6488 | return ResultTy; |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6489 | } |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 6490 | // C99 6.5.9p2 and C99 6.5.8p2 |
| 6491 | if (Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(), |
| 6492 | RCanPointeeTy.getUnqualifiedType())) { |
| 6493 | // Valid unless a relational comparison of function pointers |
| 6494 | if (isRelational && LCanPointeeTy->isFunctionType()) { |
| 6495 | Diag(Loc, diag::ext_typecheck_ordered_comparison_of_function_pointers) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6496 | << lType << rType << lex.get()->getSourceRange() |
| 6497 | << rex.get()->getSourceRange(); |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 6498 | } |
| 6499 | } else if (!isRelational && |
| 6500 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 6501 | // Valid unless comparison between non-null pointer and function pointer |
| 6502 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6503 | && !LHSIsNull && !RHSIsNull) |
| 6504 | diagnoseFunctionPointerToVoidComparison(*this, Loc, lex, rex, |
| 6505 | /*isError*/false); |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 6506 | } else { |
| 6507 | // Invalid |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6508 | diagnoseDistinctPointerComparison(*this, Loc, lex, rex, /*isError*/false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6509 | } |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6510 | if (LCanPointeeTy != RCanPointeeTy) { |
| 6511 | if (LHSIsNull && !RHSIsNull) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6512 | lex = ImpCastExprToType(lex.take(), rType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6513 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6514 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6515 | } |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6516 | return ResultTy; |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 6517 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6518 | |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 6519 | if (getLangOptions().CPlusPlus) { |
Anders Carlsson | 0c8209e | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 6520 | // Comparison of nullptr_t with itself. |
| 6521 | if (lType->isNullPtrType() && rType->isNullPtrType()) |
| 6522 | return ResultTy; |
| 6523 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6524 | // Comparison of pointers with null pointer constants and equality |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6525 | // comparisons of member pointers to null pointer constants. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6526 | if (RHSIsNull && |
Douglas Gregor | 17e37c7 | 2011-06-01 15:12:24 +0000 | [diff] [blame] | 6527 | ((lType->isAnyPointerType() || lType->isNullPtrType()) || |
Douglas Gregor | 16cd4b7 | 2011-06-16 18:52:05 +0000 | [diff] [blame] | 6528 | (!isRelational && |
| 6529 | (lType->isMemberPointerType() || lType->isBlockPointerType())))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6530 | rex = ImpCastExprToType(rex.take(), lType, |
Douglas Gregor | 443c212 | 2010-08-07 13:36:37 +0000 | [diff] [blame] | 6531 | lType->isMemberPointerType() |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6532 | ? CK_NullToMemberPointer |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 6533 | : CK_NullToPointer); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 6534 | return ResultTy; |
| 6535 | } |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6536 | if (LHSIsNull && |
Douglas Gregor | 17e37c7 | 2011-06-01 15:12:24 +0000 | [diff] [blame] | 6537 | ((rType->isAnyPointerType() || rType->isNullPtrType()) || |
Douglas Gregor | 16cd4b7 | 2011-06-16 18:52:05 +0000 | [diff] [blame] | 6538 | (!isRelational && |
| 6539 | (rType->isMemberPointerType() || rType->isBlockPointerType())))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6540 | lex = ImpCastExprToType(lex.take(), rType, |
Douglas Gregor | 443c212 | 2010-08-07 13:36:37 +0000 | [diff] [blame] | 6541 | rType->isMemberPointerType() |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6542 | ? CK_NullToMemberPointer |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 6543 | : CK_NullToPointer); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 6544 | return ResultTy; |
| 6545 | } |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6546 | |
| 6547 | // Comparison of member pointers. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6548 | if (!isRelational && |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6549 | lType->isMemberPointerType() && rType->isMemberPointerType()) { |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6550 | if (convertPointersToCompositeType(*this, Loc, lex, rex)) |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6551 | return QualType(); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6552 | else |
| 6553 | return ResultTy; |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 6554 | } |
Douglas Gregor | 90566c0 | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 6555 | |
| 6556 | // Handle scoped enumeration types specifically, since they don't promote |
| 6557 | // to integers. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6558 | if (lex.get()->getType()->isEnumeralType() && |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6559 | Context.hasSameUnqualifiedType(lex.get()->getType(), |
| 6560 | rex.get()->getType())) |
Douglas Gregor | 90566c0 | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 6561 | return ResultTy; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 6562 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6563 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6564 | // Handle block pointer types. |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6565 | if (!isRelational && lType->isBlockPointerType() && |
| 6566 | rType->isBlockPointerType()) { |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 6567 | QualType lpointee = lType->getAs<BlockPointerType>()->getPointeeType(); |
| 6568 | QualType rpointee = rType->getAs<BlockPointerType>()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6569 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6570 | if (!LHSIsNull && !RHSIsNull && |
Eli Friedman | 26784c1 | 2009-06-08 05:08:54 +0000 | [diff] [blame] | 6571 | !Context.typesAreCompatible(lpointee, rpointee)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 6572 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6573 | << lType << rType << lex.get()->getSourceRange() |
| 6574 | << rex.get()->getSourceRange(); |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6575 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6576 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6577 | return ResultTy; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6578 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6579 | |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 6580 | // Allow block pointers to be compared with null pointer constants. |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 6581 | if (!isRelational |
| 6582 | && ((lType->isBlockPointerType() && rType->isPointerType()) |
| 6583 | || (lType->isPointerType() && rType->isBlockPointerType()))) { |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 6584 | if (!LHSIsNull && !RHSIsNull) { |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6585 | if (!((rType->isPointerType() && rType->castAs<PointerType>() |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 6586 | ->getPointeeType()->isVoidType()) |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6587 | || (lType->isPointerType() && lType->castAs<PointerType>() |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 6588 | ->getPointeeType()->isVoidType()))) |
| 6589 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6590 | << lType << rType << lex.get()->getSourceRange() |
| 6591 | << rex.get()->getSourceRange(); |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 6592 | } |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6593 | if (LHSIsNull && !RHSIsNull) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6594 | lex = ImpCastExprToType(lex.take(), rType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6595 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6596 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6597 | return ResultTy; |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 6598 | } |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 6599 | |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6600 | if (lType->isObjCObjectPointerType() || rType->isObjCObjectPointerType()) { |
| 6601 | const PointerType *LPT = lType->getAs<PointerType>(); |
| 6602 | const PointerType *RPT = rType->getAs<PointerType>(); |
| 6603 | if (LPT || RPT) { |
| 6604 | bool LPtrToVoid = LPT ? LPT->getPointeeType()->isVoidType() : false; |
| 6605 | bool RPtrToVoid = RPT ? RPT->getPointeeType()->isVoidType() : false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6606 | |
Steve Naroff | a8069f1 | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 6607 | if (!LPtrToVoid && !RPtrToVoid && |
| 6608 | !Context.typesAreCompatible(lType, rType)) { |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6609 | diagnoseDistinctPointerComparison(*this, Loc, lex, rex, |
| 6610 | /*isError*/false); |
Steve Naroff | a5ad863 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 6611 | } |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6612 | if (LHSIsNull && !RHSIsNull) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6613 | lex = ImpCastExprToType(lex.take(), rType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6614 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6615 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6616 | return ResultTy; |
Steve Naroff | 87f3b93 | 2008-10-20 18:19:10 +0000 | [diff] [blame] | 6617 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 6618 | if (lType->isObjCObjectPointerType() && rType->isObjCObjectPointerType()) { |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 6619 | if (!Context.areComparableObjCPointerTypes(lType, rType)) |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6620 | diagnoseDistinctPointerComparison(*this, Loc, lex, rex, |
| 6621 | /*isError*/false); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6622 | if (LHSIsNull && !RHSIsNull) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6623 | lex = ImpCastExprToType(lex.take(), rType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 6624 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6625 | rex = ImpCastExprToType(rex.take(), lType, CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6626 | return ResultTy; |
Steve Naroff | 2037322 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 6627 | } |
Fariborz Jahanian | 7359f04 | 2007-12-20 01:06:58 +0000 | [diff] [blame] | 6628 | } |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6629 | if ((lType->isAnyPointerType() && rType->isIntegerType()) || |
| 6630 | (lType->isIntegerType() && rType->isAnyPointerType())) { |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6631 | unsigned DiagID = 0; |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6632 | bool isError = false; |
| 6633 | if ((LHSIsNull && lType->isIntegerType()) || |
| 6634 | (RHSIsNull && rType->isIntegerType())) { |
| 6635 | if (isRelational && !getLangOptions().CPlusPlus) |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6636 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_and_zero; |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6637 | } else if (isRelational && !getLangOptions().CPlusPlus) |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6638 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_integer; |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6639 | else if (getLangOptions().CPlusPlus) { |
| 6640 | DiagID = diag::err_typecheck_comparison_of_pointer_integer; |
| 6641 | isError = true; |
| 6642 | } else |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6643 | DiagID = diag::ext_typecheck_comparison_of_pointer_integer; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6644 | |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6645 | if (DiagID) { |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 6646 | Diag(Loc, DiagID) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6647 | << lType << rType << lex.get()->getSourceRange() |
| 6648 | << rex.get()->getSourceRange(); |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6649 | if (isError) |
| 6650 | return QualType(); |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 6651 | } |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6652 | |
| 6653 | if (lType->isIntegerType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6654 | lex = ImpCastExprToType(lex.take(), rType, |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 6655 | LHSIsNull ? CK_NullToPointer : CK_IntegralToPointer); |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 6656 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6657 | rex = ImpCastExprToType(rex.take(), lType, |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 6658 | RHSIsNull ? CK_NullToPointer : CK_IntegralToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6659 | return ResultTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6660 | } |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 6661 | |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 6662 | // Handle block pointers. |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 6663 | if (!isRelational && RHSIsNull |
| 6664 | && lType->isBlockPointerType() && rType->isIntegerType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6665 | rex = ImpCastExprToType(rex.take(), lType, CK_NullToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6666 | return ResultTy; |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 6667 | } |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 6668 | if (!isRelational && LHSIsNull |
| 6669 | && lType->isIntegerType() && rType->isBlockPointerType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6670 | lex = ImpCastExprToType(lex.take(), rType, CK_NullToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 6671 | return ResultTy; |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 6672 | } |
Douglas Gregor | 90566c0 | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 6673 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6674 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6675 | } |
| 6676 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6677 | /// CheckVectorCompareOperands - vector comparisons are a clang extension that |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6678 | /// operates on extended vector types. Instead of producing an IntTy result, |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6679 | /// like a scalar comparison, a vector comparison produces a vector of integer |
| 6680 | /// types. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6681 | QualType Sema::CheckVectorCompareOperands(ExprResult &lex, ExprResult &rex, |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6682 | SourceLocation Loc, |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6683 | bool isRelational) { |
| 6684 | // Check to make sure we're operating on vectors of the same type and width, |
| 6685 | // Allowing one side to be a scalar of element type. |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 6686 | QualType vType = CheckVectorOperands(lex, rex, Loc, /*isCompAssign*/false); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6687 | if (vType.isNull()) |
| 6688 | return vType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6689 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6690 | QualType lType = lex.get()->getType(); |
| 6691 | QualType rType = rex.get()->getType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6692 | |
Anton Yartsev | 7870b13 | 2011-03-27 15:36:07 +0000 | [diff] [blame] | 6693 | // If AltiVec, the comparison results in a numeric type, i.e. |
| 6694 | // bool for C++, int for C |
Anton Yartsev | 6305f72 | 2011-03-28 21:00:05 +0000 | [diff] [blame] | 6695 | if (vType->getAs<VectorType>()->getVectorKind() == VectorType::AltiVecVector) |
Anton Yartsev | 7870b13 | 2011-03-27 15:36:07 +0000 | [diff] [blame] | 6696 | return Context.getLogicalOperationType(); |
| 6697 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6698 | // For non-floating point types, check for self-comparisons of the form |
| 6699 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 6700 | // often indicate logic errors in the program. |
Douglas Gregor | 8eee119 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 6701 | if (!lType->hasFloatingRepresentation()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6702 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(lex.get()->IgnoreParens())) |
| 6703 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(rex.get()->IgnoreParens())) |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6704 | if (DRL->getDecl() == DRR->getDecl()) |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 6705 | DiagRuntimeBehavior(Loc, 0, |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6706 | PDiag(diag::warn_comparison_always) |
| 6707 | << 0 // self- |
| 6708 | << 2 // "a constant" |
| 6709 | ); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6710 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6711 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6712 | // Check for comparisons of floating point operands using != and ==. |
Douglas Gregor | 8eee119 | 2010-06-22 22:12:46 +0000 | [diff] [blame] | 6713 | if (!isRelational && lType->hasFloatingRepresentation()) { |
| 6714 | assert (rType->hasFloatingRepresentation()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6715 | CheckFloatComparison(Loc, lex.get(), rex.get()); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6716 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6717 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6718 | // Return the type for the comparison, which is the same as vector type for |
| 6719 | // integer vectors, or an integer type of identical size and number of |
| 6720 | // elements for floating point vectors. |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 6721 | if (lType->hasIntegerRepresentation()) |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6722 | return lType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6723 | |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 6724 | const VectorType *VTy = lType->getAs<VectorType>(); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6725 | unsigned TypeSize = Context.getTypeSize(VTy->getElementType()); |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6726 | if (TypeSize == Context.getTypeSize(Context.IntTy)) |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6727 | return Context.getExtVectorType(Context.IntTy, VTy->getNumElements()); |
Chris Lattner | d013aa1 | 2009-03-31 07:46:52 +0000 | [diff] [blame] | 6728 | if (TypeSize == Context.getTypeSize(Context.LongTy)) |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6729 | return Context.getExtVectorType(Context.LongTy, VTy->getNumElements()); |
| 6730 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6731 | assert(TypeSize == Context.getTypeSize(Context.LongLongTy) && |
Nate Begeman | 59b5da6 | 2009-01-18 03:20:47 +0000 | [diff] [blame] | 6732 | "Unhandled vector element size in vector compare"); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 6733 | return Context.getExtVectorType(Context.LongLongTy, VTy->getNumElements()); |
| 6734 | } |
| 6735 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6736 | inline QualType Sema::CheckBitwiseOperands( |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6737 | ExprResult &lex, ExprResult &rex, SourceLocation Loc, bool isCompAssign) { |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6738 | if (lex.get()->getType()->isVectorType() || |
| 6739 | rex.get()->getType()->isVectorType()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6740 | if (lex.get()->getType()->hasIntegerRepresentation() && |
| 6741 | rex.get()->getType()->hasIntegerRepresentation()) |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 6742 | return CheckVectorOperands(lex, rex, Loc, isCompAssign); |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 6743 | |
| 6744 | return InvalidOperands(Loc, lex, rex); |
| 6745 | } |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 6746 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6747 | ExprResult lexResult = Owned(lex), rexResult = Owned(rex); |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6748 | QualType compType = UsualArithmeticConversions(lexResult, rexResult, |
| 6749 | isCompAssign); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6750 | if (lexResult.isInvalid() || rexResult.isInvalid()) |
| 6751 | return QualType(); |
| 6752 | lex = lexResult.take(); |
| 6753 | rex = rexResult.take(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6754 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6755 | if (lex.get()->getType()->isIntegralOrUnscopedEnumerationType() && |
| 6756 | rex.get()->getType()->isIntegralOrUnscopedEnumerationType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 6757 | return compType; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 6758 | return InvalidOperands(Loc, lex, rex); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6759 | } |
| 6760 | |
| 6761 | inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6762 | ExprResult &lex, ExprResult &rex, SourceLocation Loc, unsigned Opc) { |
Chris Lattner | 90a8f27 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 6763 | |
| 6764 | // Diagnose cases where the user write a logical and/or but probably meant a |
| 6765 | // bitwise one. We do this when the LHS is a non-bool integer and the RHS |
| 6766 | // is a constant. |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6767 | if (lex.get()->getType()->isIntegerType() && |
| 6768 | !lex.get()->getType()->isBooleanType() && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6769 | rex.get()->getType()->isIntegerType() && !rex.get()->isValueDependent() && |
Richard Trieu | e5adf59 | 2011-07-15 00:00:51 +0000 | [diff] [blame] | 6770 | // Don't warn in macros or template instantiations. |
| 6771 | !Loc.isMacroID() && ActiveTemplateInstantiations.empty()) { |
Chris Lattner | b7690b4 | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 6772 | // If the RHS can be constant folded, and if it constant folds to something |
| 6773 | // that isn't 0 or 1 (which indicate a potential logical operation that |
| 6774 | // happened to fold to true/false) then warn. |
Chandler Carruth | 0683a14 | 2011-05-31 05:41:42 +0000 | [diff] [blame] | 6775 | // Parens on the RHS are ignored. |
Chris Lattner | b7690b4 | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 6776 | Expr::EvalResult Result; |
Chandler Carruth | 0683a14 | 2011-05-31 05:41:42 +0000 | [diff] [blame] | 6777 | if (rex.get()->Evaluate(Result, Context) && !Result.HasSideEffects) |
| 6778 | if ((getLangOptions().Bool && !rex.get()->getType()->isBooleanType()) || |
| 6779 | (Result.Val.getInt() != 0 && Result.Val.getInt() != 1)) { |
| 6780 | Diag(Loc, diag::warn_logical_instead_of_bitwise) |
| 6781 | << rex.get()->getSourceRange() |
Matt Beaumont-Gay | 9b127f3 | 2011-08-15 17:50:06 +0000 | [diff] [blame] | 6782 | << (Opc == BO_LAnd ? "&&" : "||"); |
| 6783 | // Suggest replacing the logical operator with the bitwise version |
| 6784 | Diag(Loc, diag::note_logical_instead_of_bitwise_change_operator) |
| 6785 | << (Opc == BO_LAnd ? "&" : "|") |
| 6786 | << FixItHint::CreateReplacement(SourceRange( |
| 6787 | Loc, Lexer::getLocForEndOfToken(Loc, 0, getSourceManager(), |
| 6788 | getLangOptions())), |
| 6789 | Opc == BO_LAnd ? "&" : "|"); |
| 6790 | if (Opc == BO_LAnd) |
| 6791 | // Suggest replacing "Foo() && kNonZero" with "Foo()" |
| 6792 | Diag(Loc, diag::note_logical_instead_of_bitwise_remove_constant) |
| 6793 | << FixItHint::CreateRemoval( |
| 6794 | SourceRange( |
| 6795 | Lexer::getLocForEndOfToken(lex.get()->getLocEnd(), |
| 6796 | 0, getSourceManager(), |
| 6797 | getLangOptions()), |
| 6798 | rex.get()->getLocEnd())); |
| 6799 | } |
Chris Lattner | b7690b4 | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 6800 | } |
Chris Lattner | 90a8f27 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 6801 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 6802 | if (!Context.getLangOptions().CPlusPlus) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6803 | lex = UsualUnaryConversions(lex.take()); |
| 6804 | if (lex.isInvalid()) |
| 6805 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6806 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6807 | rex = UsualUnaryConversions(rex.take()); |
| 6808 | if (rex.isInvalid()) |
| 6809 | return QualType(); |
| 6810 | |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6811 | if (!lex.get()->getType()->isScalarType() || |
| 6812 | !rex.get()->getType()->isScalarType()) |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 6813 | return InvalidOperands(Loc, lex, rex); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6814 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 6815 | return Context.IntTy; |
Anders Carlsson | 0490501 | 2009-10-16 01:44:21 +0000 | [diff] [blame] | 6816 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6817 | |
John McCall | 75f7c0f | 2010-06-04 00:29:51 +0000 | [diff] [blame] | 6818 | // The following is safe because we only use this method for |
| 6819 | // non-overloadable operands. |
| 6820 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 6821 | // C++ [expr.log.and]p1 |
| 6822 | // C++ [expr.log.or]p1 |
John McCall | 75f7c0f | 2010-06-04 00:29:51 +0000 | [diff] [blame] | 6823 | // The operands are both contextually converted to type bool. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6824 | ExprResult lexRes = PerformContextuallyConvertToBool(lex.get()); |
| 6825 | if (lexRes.isInvalid()) |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 6826 | return InvalidOperands(Loc, lex, rex); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6827 | lex = move(lexRes); |
| 6828 | |
| 6829 | ExprResult rexRes = PerformContextuallyConvertToBool(rex.get()); |
| 6830 | if (rexRes.isInvalid()) |
| 6831 | return InvalidOperands(Loc, lex, rex); |
| 6832 | rex = move(rexRes); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6833 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 6834 | // C++ [expr.log.and]p2 |
| 6835 | // C++ [expr.log.or]p2 |
| 6836 | // The result is a bool. |
| 6837 | return Context.BoolTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6838 | } |
| 6839 | |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 6840 | /// IsReadonlyProperty - Verify that otherwise a valid l-value expression |
| 6841 | /// is a read-only property; return true if so. A readonly property expression |
| 6842 | /// depends on various declarations and thus must be treated specially. |
| 6843 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6844 | static bool IsReadonlyProperty(Expr *E, Sema &S) { |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 6845 | if (E->getStmtClass() == Expr::ObjCPropertyRefExprClass) { |
| 6846 | const ObjCPropertyRefExpr* PropExpr = cast<ObjCPropertyRefExpr>(E); |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 6847 | if (PropExpr->isImplicitProperty()) return false; |
| 6848 | |
| 6849 | ObjCPropertyDecl *PDecl = PropExpr->getExplicitProperty(); |
| 6850 | QualType BaseType = PropExpr->isSuperReceiver() ? |
| 6851 | PropExpr->getSuperReceiverType() : |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 6852 | PropExpr->getBase()->getType(); |
| 6853 | |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 6854 | if (const ObjCObjectPointerType *OPT = |
| 6855 | BaseType->getAsObjCInterfacePointerType()) |
| 6856 | if (ObjCInterfaceDecl *IFace = OPT->getInterfaceDecl()) |
| 6857 | if (S.isPropertyReadonly(PDecl, IFace)) |
| 6858 | return true; |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 6859 | } |
| 6860 | return false; |
| 6861 | } |
| 6862 | |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 6863 | static bool IsConstProperty(Expr *E, Sema &S) { |
| 6864 | if (E->getStmtClass() == Expr::ObjCPropertyRefExprClass) { |
| 6865 | const ObjCPropertyRefExpr* PropExpr = cast<ObjCPropertyRefExpr>(E); |
| 6866 | if (PropExpr->isImplicitProperty()) return false; |
| 6867 | |
| 6868 | ObjCPropertyDecl *PDecl = PropExpr->getExplicitProperty(); |
| 6869 | QualType T = PDecl->getType(); |
| 6870 | if (T->isReferenceType()) |
Fariborz Jahanian | 61750f2 | 2011-03-30 16:59:30 +0000 | [diff] [blame] | 6871 | T = T->getAs<ReferenceType>()->getPointeeType(); |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 6872 | CanQualType CT = S.Context.getCanonicalType(T); |
| 6873 | return CT.isConstQualified(); |
| 6874 | } |
| 6875 | return false; |
| 6876 | } |
| 6877 | |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 6878 | static bool IsReadonlyMessage(Expr *E, Sema &S) { |
| 6879 | if (E->getStmtClass() != Expr::MemberExprClass) |
| 6880 | return false; |
| 6881 | const MemberExpr *ME = cast<MemberExpr>(E); |
| 6882 | NamedDecl *Member = ME->getMemberDecl(); |
| 6883 | if (isa<FieldDecl>(Member)) { |
| 6884 | Expr *Base = ME->getBase()->IgnoreParenImpCasts(); |
| 6885 | if (Base->getStmtClass() != Expr::ObjCMessageExprClass) |
| 6886 | return false; |
| 6887 | return cast<ObjCMessageExpr>(Base)->getMethodDecl() != 0; |
| 6888 | } |
| 6889 | return false; |
| 6890 | } |
| 6891 | |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6892 | /// CheckForModifiableLvalue - Verify that E is a modifiable lvalue. If not, |
| 6893 | /// emit an error and return true. If so, return false. |
| 6894 | static bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S) { |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 6895 | SourceLocation OrigLoc = Loc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6896 | Expr::isModifiableLvalueResult IsLV = E->isModifiableLvalue(S.Context, |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 6897 | &Loc); |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 6898 | if (IsLV == Expr::MLV_Valid && IsReadonlyProperty(E, S)) |
| 6899 | IsLV = Expr::MLV_ReadonlyProperty; |
Fariborz Jahanian | 1408676 | 2011-03-28 23:47:18 +0000 | [diff] [blame] | 6900 | else if (Expr::MLV_ConstQualified && IsConstProperty(E, S)) |
| 6901 | IsLV = Expr::MLV_Valid; |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 6902 | else if (IsLV == Expr::MLV_ClassTemporary && IsReadonlyMessage(E, S)) |
| 6903 | IsLV = Expr::MLV_InvalidMessageExpression; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6904 | if (IsLV == Expr::MLV_Valid) |
| 6905 | return false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6906 | |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6907 | unsigned Diag = 0; |
| 6908 | bool NeedType = false; |
| 6909 | switch (IsLV) { // C99 6.5.16p2 |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6910 | case Expr::MLV_ConstQualified: |
| 6911 | Diag = diag::err_typecheck_assign_const; |
| 6912 | |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 6913 | // In ARC, use some specialized diagnostics for occasions where we |
| 6914 | // infer 'const'. These are always pseudo-strong variables. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6915 | if (S.getLangOptions().ObjCAutoRefCount) { |
| 6916 | DeclRefExpr *declRef = dyn_cast<DeclRefExpr>(E->IgnoreParenCasts()); |
| 6917 | if (declRef && isa<VarDecl>(declRef->getDecl())) { |
| 6918 | VarDecl *var = cast<VarDecl>(declRef->getDecl()); |
| 6919 | |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 6920 | // Use the normal diagnostic if it's pseudo-__strong but the |
| 6921 | // user actually wrote 'const'. |
| 6922 | if (var->isARCPseudoStrong() && |
| 6923 | (!var->getTypeSourceInfo() || |
| 6924 | !var->getTypeSourceInfo()->getType().isConstQualified())) { |
| 6925 | // There are two pseudo-strong cases: |
| 6926 | // - self |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6927 | ObjCMethodDecl *method = S.getCurMethodDecl(); |
| 6928 | if (method && var == method->getSelfDecl()) |
| 6929 | Diag = diag::err_typecheck_arr_assign_self; |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 6930 | |
| 6931 | // - fast enumeration variables |
| 6932 | else |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6933 | Diag = diag::err_typecheck_arr_assign_enumeration; |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 6934 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 6935 | SourceRange Assign; |
| 6936 | if (Loc != OrigLoc) |
| 6937 | Assign = SourceRange(OrigLoc, OrigLoc); |
| 6938 | S.Diag(Loc, Diag) << E->getSourceRange() << Assign; |
| 6939 | // We need to preserve the AST regardless, so migration tool |
| 6940 | // can do its job. |
| 6941 | return false; |
| 6942 | } |
| 6943 | } |
| 6944 | } |
| 6945 | |
| 6946 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6947 | case Expr::MLV_ArrayType: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6948 | Diag = diag::err_typecheck_array_not_modifiable_lvalue; |
| 6949 | NeedType = true; |
| 6950 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6951 | case Expr::MLV_NotObjectType: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6952 | Diag = diag::err_typecheck_non_object_not_modifiable_lvalue; |
| 6953 | NeedType = true; |
| 6954 | break; |
Chris Lattner | ca354fa | 2008-11-17 19:51:54 +0000 | [diff] [blame] | 6955 | case Expr::MLV_LValueCast: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6956 | Diag = diag::err_typecheck_lvalue_casts_not_supported; |
| 6957 | break; |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 6958 | case Expr::MLV_Valid: |
| 6959 | llvm_unreachable("did not take early return for MLV_Valid"); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6960 | case Expr::MLV_InvalidExpression: |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 6961 | case Expr::MLV_MemberFunction: |
| 6962 | case Expr::MLV_ClassTemporary: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6963 | Diag = diag::err_typecheck_expression_not_modifiable_lvalue; |
| 6964 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6965 | case Expr::MLV_IncompleteType: |
| 6966 | case Expr::MLV_IncompleteVoidType: |
Douglas Gregor | 86447ec | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 6967 | return S.RequireCompleteType(Loc, E->getType(), |
Douglas Gregor | fe6b2d4 | 2010-03-29 23:34:08 +0000 | [diff] [blame] | 6968 | S.PDiag(diag::err_typecheck_incomplete_type_not_modifiable_lvalue) |
Anders Carlsson | b790661 | 2009-08-26 23:45:07 +0000 | [diff] [blame] | 6969 | << E->getSourceRange()); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 6970 | case Expr::MLV_DuplicateVectorComponents: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6971 | Diag = diag::err_typecheck_duplicate_vector_components_not_mlvalue; |
| 6972 | break; |
Steve Naroff | 4f6a7d7 | 2008-09-26 14:41:28 +0000 | [diff] [blame] | 6973 | case Expr::MLV_NotBlockQualified: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6974 | Diag = diag::err_block_decl_ref_not_modifiable_lvalue; |
| 6975 | break; |
Fariborz Jahanian | 5daf570 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 6976 | case Expr::MLV_ReadonlyProperty: |
| 6977 | Diag = diag::error_readonly_property_assignment; |
| 6978 | break; |
Fariborz Jahanian | ba8d2d6 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 6979 | case Expr::MLV_NoSetterProperty: |
| 6980 | Diag = diag::error_nosetter_property_assignment; |
| 6981 | break; |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 6982 | case Expr::MLV_InvalidMessageExpression: |
| 6983 | Diag = diag::error_readonly_message_assignment; |
| 6984 | break; |
Fariborz Jahanian | 2514a30 | 2009-12-15 23:59:41 +0000 | [diff] [blame] | 6985 | case Expr::MLV_SubObjCPropertySetting: |
| 6986 | Diag = diag::error_no_subobject_property_setting; |
| 6987 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6988 | } |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 6989 | |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 6990 | SourceRange Assign; |
| 6991 | if (Loc != OrigLoc) |
| 6992 | Assign = SourceRange(OrigLoc, OrigLoc); |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6993 | if (NeedType) |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 6994 | S.Diag(Loc, Diag) << E->getType() << E->getSourceRange() << Assign; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6995 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6996 | S.Diag(Loc, Diag) << E->getSourceRange() << Assign; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 6997 | return true; |
| 6998 | } |
| 6999 | |
| 7000 | |
| 7001 | |
| 7002 | // C99 6.5.16.1 |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7003 | QualType Sema::CheckAssignmentOperands(Expr *LHS, ExprResult &RHS, |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7004 | SourceLocation Loc, |
| 7005 | QualType CompoundType) { |
| 7006 | // Verify that LHS is a modifiable lvalue, and emit error if not. |
| 7007 | if (CheckForModifiableLvalue(LHS, Loc, *this)) |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7008 | return QualType(); |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7009 | |
| 7010 | QualType LHSType = LHS->getType(); |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 7011 | QualType RHSType = CompoundType.isNull() ? RHS.get()->getType() : |
| 7012 | CompoundType; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7013 | AssignConvertType ConvTy; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7014 | if (CompoundType.isNull()) { |
Fariborz Jahanian | e2a901a | 2010-06-07 22:02:01 +0000 | [diff] [blame] | 7015 | QualType LHSTy(LHSType); |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7016 | // Simple assignment "x = y". |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7017 | if (LHS->getObjectKind() == OK_ObjCProperty) { |
| 7018 | ExprResult LHSResult = Owned(LHS); |
| 7019 | ConvertPropertyForLValue(LHSResult, RHS, LHSTy); |
| 7020 | if (LHSResult.isInvalid()) |
| 7021 | return QualType(); |
| 7022 | LHS = LHSResult.take(); |
| 7023 | } |
Fariborz Jahanian | e2a901a | 2010-06-07 22:02:01 +0000 | [diff] [blame] | 7024 | ConvTy = CheckSingleAssignmentConstraints(LHSTy, RHS); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7025 | if (RHS.isInvalid()) |
| 7026 | return QualType(); |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 7027 | // Special case of NSObject attributes on c-style pointer types. |
| 7028 | if (ConvTy == IncompatiblePointer && |
| 7029 | ((Context.isObjCNSObjectType(LHSType) && |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 7030 | RHSType->isObjCObjectPointerType()) || |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 7031 | (Context.isObjCNSObjectType(RHSType) && |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 7032 | LHSType->isObjCObjectPointerType()))) |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 7033 | ConvTy = Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7034 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7035 | if (ConvTy == Compatible && |
| 7036 | getLangOptions().ObjCNonFragileABI && |
| 7037 | LHSType->isObjCObjectType()) |
| 7038 | Diag(Loc, diag::err_assignment_requires_nonfragile_object) |
| 7039 | << LHSType; |
| 7040 | |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7041 | // If the RHS is a unary plus or minus, check to see if they = and + are |
| 7042 | // right next to each other. If so, the user may have typo'd "x =+ 4" |
| 7043 | // instead of "x += 4". |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7044 | Expr *RHSCheck = RHS.get(); |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7045 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RHSCheck)) |
| 7046 | RHSCheck = ICE->getSubExpr(); |
| 7047 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(RHSCheck)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7048 | if ((UO->getOpcode() == UO_Plus || |
| 7049 | UO->getOpcode() == UO_Minus) && |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7050 | Loc.isFileID() && UO->getOperatorLoc().isFileID() && |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7051 | // Only if the two operators are exactly adjacent. |
Chris Lattner | 399bd1b | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 7052 | Loc.getFileLocWithOffset(1) == UO->getOperatorLoc() && |
| 7053 | // And there is a space or other character before the subexpr of the |
| 7054 | // unary +/-. We don't want to warn on "x=-1". |
Chris Lattner | 3e87209 | 2009-03-09 07:11:10 +0000 | [diff] [blame] | 7055 | Loc.getFileLocWithOffset(2) != UO->getSubExpr()->getLocStart() && |
| 7056 | UO->getSubExpr()->getLocStart().isFileID()) { |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 7057 | Diag(Loc, diag::warn_not_compound_assign) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7058 | << (UO->getOpcode() == UO_Plus ? "+" : "-") |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 7059 | << SourceRange(UO->getOperatorLoc(), UO->getOperatorLoc()); |
Chris Lattner | 399bd1b | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 7060 | } |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7061 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7062 | |
| 7063 | if (ConvTy == Compatible) { |
| 7064 | if (LHSType.getObjCLifetime() == Qualifiers::OCL_Strong) |
| 7065 | checkRetainCycles(LHS, RHS.get()); |
Fariborz Jahanian | 921c143 | 2011-06-24 18:25:34 +0000 | [diff] [blame] | 7066 | else if (getLangOptions().ObjCAutoRefCount) |
| 7067 | checkUnsafeExprAssigns(Loc, LHS, RHS.get()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7068 | } |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7069 | } else { |
| 7070 | // Compound assignment "x += y" |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 7071 | ConvTy = CheckAssignmentConstraints(Loc, LHSType, RHSType); |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7072 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7073 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7074 | if (DiagnoseAssignmentResult(ConvTy, Loc, LHSType, RHSType, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7075 | RHS.get(), AA_Assigning)) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7076 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7077 | |
Argyrios Kyrtzidis | 8a285ae | 2011-04-26 17:41:22 +0000 | [diff] [blame] | 7078 | CheckForNullPointerDereference(*this, LHS); |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 7079 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7080 | // C99 6.5.16p3: The type of an assignment expression is the type of the |
| 7081 | // left operand unless the left operand has qualified type, in which case |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7082 | // it is the unqualified version of the type of the left operand. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7083 | // C99 6.5.16.1p2: In simple assignment, the value of the right operand |
| 7084 | // is converted to the type of the assignment expression (above). |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 7085 | // 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] | 7086 | // operand. |
John McCall | 2bf6f49 | 2010-10-12 02:19:57 +0000 | [diff] [blame] | 7087 | return (getLangOptions().CPlusPlus |
| 7088 | ? LHSType : LHSType.getUnqualifiedType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7089 | } |
| 7090 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7091 | // C99 6.5.17 |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7092 | static QualType CheckCommaOperands(Sema &S, ExprResult &LHS, ExprResult &RHS, |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7093 | SourceLocation Loc) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7094 | S.DiagnoseUnusedExprResult(LHS.get()); |
Argyrios Kyrtzidis | 2597345 | 2010-06-30 10:53:14 +0000 | [diff] [blame] | 7095 | |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 7096 | LHS = S.CheckPlaceholderExpr(LHS.take()); |
| 7097 | RHS = S.CheckPlaceholderExpr(RHS.take()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7098 | if (LHS.isInvalid() || RHS.isInvalid()) |
Douglas Gregor | 7ad5d42 | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 7099 | return QualType(); |
| 7100 | |
John McCall | cf2e506 | 2010-10-12 07:14:40 +0000 | [diff] [blame] | 7101 | // C's comma performs lvalue conversion (C99 6.3.2.1) on both its |
| 7102 | // operands, but not unary promotions. |
| 7103 | // 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] | 7104 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7105 | // So we treat the LHS as a ignored value, and in C++ we allow the |
| 7106 | // containing site to determine what should be done with the RHS. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7107 | LHS = S.IgnoredValueConversions(LHS.take()); |
| 7108 | if (LHS.isInvalid()) |
| 7109 | return QualType(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7110 | |
| 7111 | if (!S.getLangOptions().CPlusPlus) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7112 | RHS = S.DefaultFunctionArrayLvalueConversion(RHS.take()); |
| 7113 | if (RHS.isInvalid()) |
| 7114 | return QualType(); |
| 7115 | if (!RHS.get()->getType()->isVoidType()) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 7116 | S.RequireCompleteType(Loc, RHS.get()->getType(), |
| 7117 | diag::err_incomplete_type); |
John McCall | cf2e506 | 2010-10-12 07:14:40 +0000 | [diff] [blame] | 7118 | } |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 7119 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7120 | return RHS.get()->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7121 | } |
| 7122 | |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 7123 | /// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine |
| 7124 | /// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7125 | static QualType CheckIncrementDecrementOperand(Sema &S, Expr *Op, |
| 7126 | ExprValueKind &VK, |
| 7127 | SourceLocation OpLoc, |
| 7128 | bool isInc, bool isPrefix) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7129 | if (Op->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7130 | return S.Context.DependentTy; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7131 | |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7132 | QualType ResType = Op->getType(); |
| 7133 | assert(!ResType.isNull() && "no type for increment/decrement expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7134 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7135 | if (S.getLangOptions().CPlusPlus && ResType->isBooleanType()) { |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 7136 | // Decrement of bool is not allowed. |
| 7137 | if (!isInc) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7138 | S.Diag(OpLoc, diag::err_decrement_bool) << Op->getSourceRange(); |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 7139 | return QualType(); |
| 7140 | } |
| 7141 | // Increment of bool sets it to true, but is deprecated. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7142 | S.Diag(OpLoc, diag::warn_increment_bool) << Op->getSourceRange(); |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 7143 | } else if (ResType->isRealType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7144 | // OK! |
Steve Naroff | 58f9f2c | 2009-07-14 18:25:06 +0000 | [diff] [blame] | 7145 | } else if (ResType->isAnyPointerType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7146 | // C99 6.5.2.4p2, 6.5.6p2 |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 7147 | if (!checkArithmeticOpPointerOperand(S, OpLoc, Op)) |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 7148 | return QualType(); |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 7149 | |
Fariborz Jahanian | 9f8a04f | 2009-07-16 17:59:14 +0000 | [diff] [blame] | 7150 | // Diagnose bad cases where we step over interface counts. |
Richard Trieu | db44a6b | 2011-09-01 22:53:23 +0000 | [diff] [blame] | 7151 | else if (!checkArithmethicPointerOnNonFragileABI(S, OpLoc, Op)) |
Fariborz Jahanian | 9f8a04f | 2009-07-16 17:59:14 +0000 | [diff] [blame] | 7152 | return QualType(); |
Eli Friedman | 5b088a1 | 2010-01-03 00:20:48 +0000 | [diff] [blame] | 7153 | } else if (ResType->isAnyComplexType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7154 | // C99 does not support ++/-- on complex types, we allow as an extension. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7155 | S.Diag(OpLoc, diag::ext_integer_increment_complex) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 7156 | << ResType << Op->getSourceRange(); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7157 | } else if (ResType->isPlaceholderType()) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 7158 | ExprResult PR = S.CheckPlaceholderExpr(Op); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7159 | if (PR.isInvalid()) return QualType(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7160 | return CheckIncrementDecrementOperand(S, PR.take(), VK, OpLoc, |
| 7161 | isInc, isPrefix); |
Anton Yartsev | 683564a | 2011-02-07 02:17:30 +0000 | [diff] [blame] | 7162 | } else if (S.getLangOptions().AltiVec && ResType->isVectorType()) { |
| 7163 | // OK! ( C/C++ Language Extensions for CBEA(Version 2.6) 10.3 ) |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7164 | } else { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7165 | S.Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement) |
Douglas Gregor | 5cc07df | 2009-12-15 16:44:32 +0000 | [diff] [blame] | 7166 | << ResType << int(isInc) << Op->getSourceRange(); |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7167 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7168 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7169 | // 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] | 7170 | // Now make sure the operand is a modifiable lvalue. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7171 | if (CheckForModifiableLvalue(Op, OpLoc, S)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7172 | return QualType(); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 7173 | // In C++, a prefix increment is the same type as the operand. Otherwise |
| 7174 | // (in C or with postfix), the increment is the unqualified type of the |
| 7175 | // operand. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7176 | if (isPrefix && S.getLangOptions().CPlusPlus) { |
| 7177 | VK = VK_LValue; |
| 7178 | return ResType; |
| 7179 | } else { |
| 7180 | VK = VK_RValue; |
| 7181 | return ResType.getUnqualifiedType(); |
| 7182 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7183 | } |
| 7184 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7185 | ExprResult Sema::ConvertPropertyForRValue(Expr *E) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7186 | assert(E->getValueKind() == VK_LValue && |
| 7187 | E->getObjectKind() == OK_ObjCProperty); |
| 7188 | const ObjCPropertyRefExpr *PRE = E->getObjCProperty(); |
| 7189 | |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 7190 | QualType T = E->getType(); |
| 7191 | QualType ReceiverType; |
| 7192 | if (PRE->isObjectReceiver()) |
| 7193 | ReceiverType = PRE->getBase()->getType(); |
| 7194 | else if (PRE->isSuperReceiver()) |
| 7195 | ReceiverType = PRE->getSuperReceiverType(); |
| 7196 | else |
| 7197 | ReceiverType = Context.getObjCInterfaceType(PRE->getClassReceiver()); |
| 7198 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7199 | ExprValueKind VK = VK_RValue; |
| 7200 | if (PRE->isImplicitProperty()) { |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 7201 | if (ObjCMethodDecl *GetterMethod = |
Fariborz Jahanian | 99130e5 | 2010-12-22 19:46:35 +0000 | [diff] [blame] | 7202 | PRE->getImplicitPropertyGetter()) { |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 7203 | T = getMessageSendResultType(ReceiverType, GetterMethod, |
| 7204 | PRE->isClassReceiver(), |
| 7205 | PRE->isSuperReceiver()); |
| 7206 | VK = Expr::getValueKindForType(GetterMethod->getResultType()); |
Fariborz Jahanian | 99130e5 | 2010-12-22 19:46:35 +0000 | [diff] [blame] | 7207 | } |
| 7208 | else { |
| 7209 | Diag(PRE->getLocation(), diag::err_getter_not_found) |
| 7210 | << PRE->getBase()->getType(); |
| 7211 | } |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7212 | } |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 7213 | |
| 7214 | E = ImplicitCastExpr::Create(Context, T, CK_GetObjCProperty, |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7215 | E, 0, VK); |
John McCall | db67e2f | 2010-12-10 01:49:45 +0000 | [diff] [blame] | 7216 | |
| 7217 | ExprResult Result = MaybeBindToTemporary(E); |
| 7218 | if (!Result.isInvalid()) |
| 7219 | E = Result.take(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7220 | |
| 7221 | return Owned(E); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7222 | } |
| 7223 | |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 7224 | void Sema::ConvertPropertyForLValue(ExprResult &LHS, ExprResult &RHS, |
| 7225 | QualType &LHSTy) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7226 | assert(LHS.get()->getValueKind() == VK_LValue && |
| 7227 | LHS.get()->getObjectKind() == OK_ObjCProperty); |
| 7228 | const ObjCPropertyRefExpr *PropRef = LHS.get()->getObjCProperty(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7229 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7230 | bool Consumed = false; |
| 7231 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7232 | if (PropRef->isImplicitProperty()) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7233 | // If using property-dot syntax notation for assignment, and there is a |
| 7234 | // setter, RHS expression is being passed to the setter argument. So, |
| 7235 | // type conversion (and comparison) is RHS to setter's argument type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7236 | if (const ObjCMethodDecl *SetterMD = PropRef->getImplicitPropertySetter()) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7237 | ObjCMethodDecl::param_iterator P = SetterMD->param_begin(); |
| 7238 | LHSTy = (*P)->getType(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7239 | Consumed = (getLangOptions().ObjCAutoRefCount && |
| 7240 | (*P)->hasAttr<NSConsumedAttr>()); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7241 | |
| 7242 | // Otherwise, if the getter returns an l-value, just call that. |
| 7243 | } else { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7244 | QualType Result = PropRef->getImplicitPropertyGetter()->getResultType(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7245 | ExprValueKind VK = Expr::getValueKindForType(Result); |
| 7246 | if (VK == VK_LValue) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7247 | LHS = ImplicitCastExpr::Create(Context, LHS.get()->getType(), |
| 7248 | CK_GetObjCProperty, LHS.take(), 0, VK); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7249 | return; |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 7250 | } |
Fariborz Jahanian | c4e1a68 | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 7251 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7252 | } else if (getLangOptions().ObjCAutoRefCount) { |
| 7253 | const ObjCMethodDecl *setter |
| 7254 | = PropRef->getExplicitProperty()->getSetterMethodDecl(); |
| 7255 | if (setter) { |
| 7256 | ObjCMethodDecl::param_iterator P = setter->param_begin(); |
| 7257 | LHSTy = (*P)->getType(); |
| 7258 | Consumed = (*P)->hasAttr<NSConsumedAttr>(); |
| 7259 | } |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7260 | } |
| 7261 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7262 | if ((getLangOptions().CPlusPlus && LHSTy->isRecordType()) || |
| 7263 | getLangOptions().ObjCAutoRefCount) { |
Fariborz Jahanian | c4e1a68 | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 7264 | InitializedEntity Entity = |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7265 | InitializedEntity::InitializeParameter(Context, LHSTy, Consumed); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7266 | ExprResult ArgE = PerformCopyInitialization(Entity, SourceLocation(), RHS); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7267 | if (!ArgE.isInvalid()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7268 | RHS = ArgE; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7269 | if (getLangOptions().ObjCAutoRefCount && !PropRef->isSuperReceiver()) |
| 7270 | checkRetainCycles(const_cast<Expr*>(PropRef->getBase()), RHS.get()); |
| 7271 | } |
Fariborz Jahanian | c4e1a68 | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 7272 | } |
| 7273 | } |
| 7274 | |
| 7275 | |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 7276 | /// getPrimaryDecl - Helper function for CheckAddressOfOperand(). |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7277 | /// This routine allows us to typecheck complex/recursive expressions |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7278 | /// where the declaration is needed for type checking. We only need to |
| 7279 | /// handle cases when the expression references a function designator |
| 7280 | /// or is an lvalue. Here are some examples: |
| 7281 | /// - &(x) => x |
| 7282 | /// - &*****f => f for f a function designator. |
| 7283 | /// - &s.xx => s |
| 7284 | /// - &s.zz[1].yy -> s, if zz is an array |
| 7285 | /// - *(x + 1) -> x, if x is an array |
| 7286 | /// - &"123"[2] -> 0 |
| 7287 | /// - & __real__ x -> x |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7288 | static ValueDecl *getPrimaryDecl(Expr *E) { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7289 | switch (E->getStmtClass()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7290 | case Stmt::DeclRefExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7291 | return cast<DeclRefExpr>(E)->getDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7292 | case Stmt::MemberExprClass: |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7293 | // If this is an arrow operator, the address is an offset from |
| 7294 | // the base's value, so the object the base refers to is |
| 7295 | // irrelevant. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7296 | if (cast<MemberExpr>(E)->isArrow()) |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 7297 | return 0; |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7298 | // Otherwise, the expression refers to a part of the base |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7299 | return getPrimaryDecl(cast<MemberExpr>(E)->getBase()); |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 7300 | case Stmt::ArraySubscriptExprClass: { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 7301 | // FIXME: This code shouldn't be necessary! We should catch the implicit |
| 7302 | // promotion of register arrays earlier. |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7303 | Expr* Base = cast<ArraySubscriptExpr>(E)->getBase(); |
| 7304 | if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Base)) { |
| 7305 | if (ICE->getSubExpr()->getType()->isArrayType()) |
| 7306 | return getPrimaryDecl(ICE->getSubExpr()); |
| 7307 | } |
| 7308 | return 0; |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 7309 | } |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7310 | case Stmt::UnaryOperatorClass: { |
| 7311 | UnaryOperator *UO = cast<UnaryOperator>(E); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7312 | |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7313 | switch(UO->getOpcode()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7314 | case UO_Real: |
| 7315 | case UO_Imag: |
| 7316 | case UO_Extension: |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7317 | return getPrimaryDecl(UO->getSubExpr()); |
| 7318 | default: |
| 7319 | return 0; |
| 7320 | } |
| 7321 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7322 | case Stmt::ParenExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7323 | return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr()); |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 7324 | case Stmt::ImplicitCastExprClass: |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7325 | // If the result of an implicit cast is an l-value, we care about |
| 7326 | // the sub-expression; otherwise, the result here doesn't matter. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7327 | return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7328 | default: |
| 7329 | return 0; |
| 7330 | } |
| 7331 | } |
| 7332 | |
Richard Trieu | 09a26ad | 2011-09-02 00:47:55 +0000 | [diff] [blame] | 7333 | /// \brief Diagnose invalid operand for address of operations. |
| 7334 | /// |
| 7335 | /// \param Type The type of operand which cannot have its address taken. |
| 7336 | /// 0:bit-field 1:vector element 2:property expression 3:register variable |
| 7337 | static void diagnoseAddressOfInvalidType(Sema &S, SourceLocation Loc, |
| 7338 | Expr *E, unsigned Type) { |
| 7339 | S.Diag(Loc, diag::err_typecheck_address_of) << Type << E->getSourceRange(); |
| 7340 | } |
| 7341 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7342 | /// CheckAddressOfOperand - The operand of & must be either a function |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7343 | /// 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] | 7344 | /// 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] | 7345 | /// Note: The usual conversions are *not* applied to the operand of the & |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7346 | /// 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] | 7347 | /// 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] | 7348 | /// we allow the '&' but retain the overloaded-function type. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7349 | static QualType CheckAddressOfOperand(Sema &S, Expr *OrigOp, |
| 7350 | SourceLocation OpLoc) { |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7351 | if (OrigOp->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7352 | return S.Context.DependentTy; |
| 7353 | if (OrigOp->getType() == S.Context.OverloadTy) |
| 7354 | return S.Context.OverloadTy; |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 7355 | if (OrigOp->getType() == S.Context.UnknownAnyTy) |
| 7356 | return S.Context.UnknownAnyTy; |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 7357 | if (OrigOp->getType() == S.Context.BoundMemberTy) { |
| 7358 | S.Diag(OpLoc, diag::err_invalid_form_pointer_member_function) |
| 7359 | << OrigOp->getSourceRange(); |
| 7360 | return QualType(); |
| 7361 | } |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7362 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 7363 | assert(!OrigOp->getType()->isPlaceholderType()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7364 | |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7365 | // Make sure to ignore parentheses in subsequent checks |
| 7366 | Expr *op = OrigOp->IgnoreParens(); |
Douglas Gregor | 9103bb2 | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 7367 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7368 | if (S.getLangOptions().C99) { |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 7369 | // Implement C99-only parts of addressof rules. |
| 7370 | if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7371 | if (uOp->getOpcode() == UO_Deref) |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 7372 | // Per C99 6.5.3.2, the address of a deref always returns a valid result |
| 7373 | // (assuming the deref expression is valid). |
| 7374 | return uOp->getSubExpr()->getType(); |
| 7375 | } |
| 7376 | // Technically, there should be a check for array subscript |
| 7377 | // expressions here, but the result of one is always an lvalue anyway. |
| 7378 | } |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7379 | ValueDecl *dcl = getPrimaryDecl(op); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 7380 | Expr::LValueClassification lval = op->ClassifyLValue(S.Context); |
Nuno Lopes | 6b6609f | 2008-12-16 22:59:47 +0000 | [diff] [blame] | 7381 | |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 7382 | if (lval == Expr::LV_ClassTemporary) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7383 | bool sfinae = S.isSFINAEContext(); |
| 7384 | S.Diag(OpLoc, sfinae ? diag::err_typecheck_addrof_class_temporary |
| 7385 | : diag::ext_typecheck_addrof_class_temporary) |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 7386 | << op->getType() << op->getSourceRange(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7387 | if (sfinae) |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 7388 | return QualType(); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7389 | } else if (isa<ObjCSelectorExpr>(op)) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7390 | return S.Context.getPointerType(op->getType()); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7391 | } else if (lval == Expr::LV_MemberFunction) { |
| 7392 | // If it's an instance method, make a member pointer. |
| 7393 | // The expression must have exactly the form &A::foo. |
| 7394 | |
| 7395 | // If the underlying expression isn't a decl ref, give up. |
| 7396 | if (!isa<DeclRefExpr>(op)) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7397 | S.Diag(OpLoc, diag::err_invalid_form_pointer_member_function) |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7398 | << OrigOp->getSourceRange(); |
| 7399 | return QualType(); |
| 7400 | } |
| 7401 | DeclRefExpr *DRE = cast<DeclRefExpr>(op); |
| 7402 | CXXMethodDecl *MD = cast<CXXMethodDecl>(DRE->getDecl()); |
| 7403 | |
| 7404 | // The id-expression was parenthesized. |
| 7405 | if (OrigOp != DRE) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7406 | S.Diag(OpLoc, diag::err_parens_pointer_member_function) |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7407 | << OrigOp->getSourceRange(); |
| 7408 | |
| 7409 | // The method was named without a qualifier. |
| 7410 | } else if (!DRE->getQualifier()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7411 | S.Diag(OpLoc, diag::err_unqualified_pointer_member_function) |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7412 | << op->getSourceRange(); |
| 7413 | } |
| 7414 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7415 | return S.Context.getMemberPointerType(op->getType(), |
| 7416 | S.Context.getTypeDeclType(MD->getParent()).getTypePtr()); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7417 | } else if (lval != Expr::LV_Valid && lval != Expr::LV_IncompleteVoidType) { |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 7418 | // C99 6.5.3.2p1 |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7419 | // The operand must be either an l-value or a function designator |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 7420 | if (!op->getType()->isFunctionType()) { |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 7421 | // FIXME: emit more specific diag... |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7422 | S.Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 7423 | << op->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7424 | return QualType(); |
| 7425 | } |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 7426 | } else if (op->getObjectKind() == OK_BitField) { // C99 6.5.3.2p1 |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7427 | // The operand cannot be a bit-field |
Richard Trieu | 09a26ad | 2011-09-02 00:47:55 +0000 | [diff] [blame] | 7428 | diagnoseAddressOfInvalidType(S, OpLoc, op, /*bit-field*/ 0); |
| 7429 | return QualType(); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 7430 | } else if (op->getObjectKind() == OK_VectorComponent) { |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7431 | // The operand cannot be an element of a vector |
Richard Trieu | 09a26ad | 2011-09-02 00:47:55 +0000 | [diff] [blame] | 7432 | diagnoseAddressOfInvalidType(S, OpLoc, op, /*vector element*/ 1); |
Steve Naroff | bcb2b61 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 7433 | return QualType(); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 7434 | } else if (op->getObjectKind() == OK_ObjCProperty) { |
Fariborz Jahanian | 0337f21 | 2009-07-07 18:50:52 +0000 | [diff] [blame] | 7435 | // cannot take address of a property expression. |
Richard Trieu | 09a26ad | 2011-09-02 00:47:55 +0000 | [diff] [blame] | 7436 | diagnoseAddressOfInvalidType(S, OpLoc, op, /*property expression*/ 2); |
Fariborz Jahanian | 0337f21 | 2009-07-07 18:50:52 +0000 | [diff] [blame] | 7437 | return QualType(); |
Steve Naroff | bcb2b61 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 7438 | } else if (dcl) { // C99 6.5.3.2p1 |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7439 | // 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] | 7440 | // with the register storage-class specifier. |
| 7441 | if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { |
Fariborz Jahanian | 4020f87 | 2010-08-24 22:21:48 +0000 | [diff] [blame] | 7442 | // in C++ it is not error to take address of a register |
| 7443 | // variable (c++03 7.1.1P3) |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 7444 | if (vd->getStorageClass() == SC_Register && |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7445 | !S.getLangOptions().CPlusPlus) { |
Richard Trieu | 09a26ad | 2011-09-02 00:47:55 +0000 | [diff] [blame] | 7446 | diagnoseAddressOfInvalidType(S, OpLoc, op, /*register variable*/ 3); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7447 | return QualType(); |
| 7448 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 7449 | } else if (isa<FunctionTemplateDecl>(dcl)) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7450 | return S.Context.OverloadTy; |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7451 | } else if (isa<FieldDecl>(dcl) || isa<IndirectFieldDecl>(dcl)) { |
Douglas Gregor | 2988205 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 7452 | // Okay: we can take the address of a field. |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 7453 | // Could be a pointer to member, though, if there is an explicit |
| 7454 | // scope qualifier for the class. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 7455 | if (isa<DeclRefExpr>(op) && cast<DeclRefExpr>(op)->getQualifier()) { |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 7456 | DeclContext *Ctx = dcl->getDeclContext(); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 7457 | if (Ctx && Ctx->isRecord()) { |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7458 | if (dcl->getType()->isReferenceType()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7459 | S.Diag(OpLoc, |
| 7460 | diag::err_cannot_form_pointer_to_member_of_reference_type) |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7461 | << dcl->getDeclName() << dcl->getType(); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 7462 | return QualType(); |
| 7463 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7464 | |
Argyrios Kyrtzidis | 0413db4 | 2011-01-31 07:04:29 +0000 | [diff] [blame] | 7465 | while (cast<RecordDecl>(Ctx)->isAnonymousStructOrUnion()) |
| 7466 | Ctx = Ctx->getParent(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7467 | return S.Context.getMemberPointerType(op->getType(), |
| 7468 | S.Context.getTypeDeclType(cast<RecordDecl>(Ctx)).getTypePtr()); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 7469 | } |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 7470 | } |
Eli Friedman | 7b2f51c | 2011-08-26 20:28:17 +0000 | [diff] [blame] | 7471 | } else if (!isa<FunctionDecl>(dcl) && !isa<NonTypeTemplateParmDecl>(dcl)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7472 | assert(0 && "Unknown/unexpected decl type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7473 | } |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 7474 | |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 7475 | if (lval == Expr::LV_IncompleteVoidType) { |
| 7476 | // Taking the address of a void variable is technically illegal, but we |
| 7477 | // allow it in cases which are otherwise valid. |
| 7478 | // Example: "extern void x; void* y = &x;". |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7479 | S.Diag(OpLoc, diag::ext_typecheck_addrof_void) << op->getSourceRange(); |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 7480 | } |
| 7481 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7482 | // 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] | 7483 | if (op->getType()->isObjCObjectType()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7484 | return S.Context.getObjCObjectPointerType(op->getType()); |
| 7485 | return S.Context.getPointerType(op->getType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7486 | } |
| 7487 | |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7488 | /// CheckIndirectionOperand - Type check unary indirection (prefix '*'). |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7489 | static QualType CheckIndirectionOperand(Sema &S, Expr *Op, ExprValueKind &VK, |
| 7490 | SourceLocation OpLoc) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7491 | if (Op->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7492 | return S.Context.DependentTy; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7493 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7494 | ExprResult ConvResult = S.UsualUnaryConversions(Op); |
| 7495 | if (ConvResult.isInvalid()) |
| 7496 | return QualType(); |
| 7497 | Op = ConvResult.take(); |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7498 | QualType OpTy = Op->getType(); |
| 7499 | QualType Result; |
Argyrios Kyrtzidis | f4bbbf0 | 2011-05-02 18:21:19 +0000 | [diff] [blame] | 7500 | |
| 7501 | if (isa<CXXReinterpretCastExpr>(Op)) { |
| 7502 | QualType OpOrigType = Op->IgnoreParenCasts()->getType(); |
| 7503 | S.CheckCompatibleReinterpretCast(OpOrigType, OpTy, /*IsDereference*/true, |
| 7504 | Op->getSourceRange()); |
| 7505 | } |
| 7506 | |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7507 | // Note that per both C89 and C99, indirection is always legal, even if OpTy |
| 7508 | // is an incomplete type or void. It would be possible to warn about |
| 7509 | // dereferencing a void pointer, but it's completely well-defined, and such a |
| 7510 | // warning is unlikely to catch any mistakes. |
| 7511 | if (const PointerType *PT = OpTy->getAs<PointerType>()) |
| 7512 | Result = PT->getPointeeType(); |
| 7513 | else if (const ObjCObjectPointerType *OPT = |
| 7514 | OpTy->getAs<ObjCObjectPointerType>()) |
| 7515 | Result = OPT->getPointeeType(); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7516 | else { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 7517 | ExprResult PR = S.CheckPlaceholderExpr(Op); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7518 | if (PR.isInvalid()) return QualType(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7519 | if (PR.take() != Op) |
| 7520 | return CheckIndirectionOperand(S, PR.take(), VK, OpLoc); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7521 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7522 | |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7523 | if (Result.isNull()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7524 | S.Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer) |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7525 | << OpTy << Op->getSourceRange(); |
| 7526 | return QualType(); |
| 7527 | } |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7528 | |
| 7529 | // Dereferences are usually l-values... |
| 7530 | VK = VK_LValue; |
| 7531 | |
| 7532 | // ...except that certain expressions are never l-values in C. |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 7533 | if (!S.getLangOptions().CPlusPlus && Result.isCForbiddenLValueType()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7534 | VK = VK_RValue; |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 7535 | |
| 7536 | return Result; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7537 | } |
| 7538 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7539 | static inline BinaryOperatorKind ConvertTokenKindToBinaryOpcode( |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7540 | tok::TokenKind Kind) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7541 | BinaryOperatorKind Opc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7542 | switch (Kind) { |
| 7543 | default: assert(0 && "Unknown binop!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7544 | case tok::periodstar: Opc = BO_PtrMemD; break; |
| 7545 | case tok::arrowstar: Opc = BO_PtrMemI; break; |
| 7546 | case tok::star: Opc = BO_Mul; break; |
| 7547 | case tok::slash: Opc = BO_Div; break; |
| 7548 | case tok::percent: Opc = BO_Rem; break; |
| 7549 | case tok::plus: Opc = BO_Add; break; |
| 7550 | case tok::minus: Opc = BO_Sub; break; |
| 7551 | case tok::lessless: Opc = BO_Shl; break; |
| 7552 | case tok::greatergreater: Opc = BO_Shr; break; |
| 7553 | case tok::lessequal: Opc = BO_LE; break; |
| 7554 | case tok::less: Opc = BO_LT; break; |
| 7555 | case tok::greaterequal: Opc = BO_GE; break; |
| 7556 | case tok::greater: Opc = BO_GT; break; |
| 7557 | case tok::exclaimequal: Opc = BO_NE; break; |
| 7558 | case tok::equalequal: Opc = BO_EQ; break; |
| 7559 | case tok::amp: Opc = BO_And; break; |
| 7560 | case tok::caret: Opc = BO_Xor; break; |
| 7561 | case tok::pipe: Opc = BO_Or; break; |
| 7562 | case tok::ampamp: Opc = BO_LAnd; break; |
| 7563 | case tok::pipepipe: Opc = BO_LOr; break; |
| 7564 | case tok::equal: Opc = BO_Assign; break; |
| 7565 | case tok::starequal: Opc = BO_MulAssign; break; |
| 7566 | case tok::slashequal: Opc = BO_DivAssign; break; |
| 7567 | case tok::percentequal: Opc = BO_RemAssign; break; |
| 7568 | case tok::plusequal: Opc = BO_AddAssign; break; |
| 7569 | case tok::minusequal: Opc = BO_SubAssign; break; |
| 7570 | case tok::lesslessequal: Opc = BO_ShlAssign; break; |
| 7571 | case tok::greatergreaterequal: Opc = BO_ShrAssign; break; |
| 7572 | case tok::ampequal: Opc = BO_AndAssign; break; |
| 7573 | case tok::caretequal: Opc = BO_XorAssign; break; |
| 7574 | case tok::pipeequal: Opc = BO_OrAssign; break; |
| 7575 | case tok::comma: Opc = BO_Comma; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7576 | } |
| 7577 | return Opc; |
| 7578 | } |
| 7579 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7580 | static inline UnaryOperatorKind ConvertTokenKindToUnaryOpcode( |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7581 | tok::TokenKind Kind) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7582 | UnaryOperatorKind Opc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7583 | switch (Kind) { |
| 7584 | default: assert(0 && "Unknown unary op!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7585 | case tok::plusplus: Opc = UO_PreInc; break; |
| 7586 | case tok::minusminus: Opc = UO_PreDec; break; |
| 7587 | case tok::amp: Opc = UO_AddrOf; break; |
| 7588 | case tok::star: Opc = UO_Deref; break; |
| 7589 | case tok::plus: Opc = UO_Plus; break; |
| 7590 | case tok::minus: Opc = UO_Minus; break; |
| 7591 | case tok::tilde: Opc = UO_Not; break; |
| 7592 | case tok::exclaim: Opc = UO_LNot; break; |
| 7593 | case tok::kw___real: Opc = UO_Real; break; |
| 7594 | case tok::kw___imag: Opc = UO_Imag; break; |
| 7595 | case tok::kw___extension__: Opc = UO_Extension; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7596 | } |
| 7597 | return Opc; |
| 7598 | } |
| 7599 | |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 7600 | /// DiagnoseSelfAssignment - Emits a warning if a value is assigned to itself. |
| 7601 | /// This warning is only emitted for builtin assignment operations. It is also |
| 7602 | /// suppressed in the event of macro expansions. |
| 7603 | static void DiagnoseSelfAssignment(Sema &S, Expr *lhs, Expr *rhs, |
| 7604 | SourceLocation OpLoc) { |
| 7605 | if (!S.ActiveTemplateInstantiations.empty()) |
| 7606 | return; |
| 7607 | if (OpLoc.isInvalid() || OpLoc.isMacroID()) |
| 7608 | return; |
| 7609 | lhs = lhs->IgnoreParenImpCasts(); |
| 7610 | rhs = rhs->IgnoreParenImpCasts(); |
| 7611 | const DeclRefExpr *LeftDeclRef = dyn_cast<DeclRefExpr>(lhs); |
| 7612 | const DeclRefExpr *RightDeclRef = dyn_cast<DeclRefExpr>(rhs); |
| 7613 | if (!LeftDeclRef || !RightDeclRef || |
| 7614 | LeftDeclRef->getLocation().isMacroID() || |
| 7615 | RightDeclRef->getLocation().isMacroID()) |
| 7616 | return; |
| 7617 | const ValueDecl *LeftDecl = |
| 7618 | cast<ValueDecl>(LeftDeclRef->getDecl()->getCanonicalDecl()); |
| 7619 | const ValueDecl *RightDecl = |
| 7620 | cast<ValueDecl>(RightDeclRef->getDecl()->getCanonicalDecl()); |
| 7621 | if (LeftDecl != RightDecl) |
| 7622 | return; |
| 7623 | if (LeftDecl->getType().isVolatileQualified()) |
| 7624 | return; |
| 7625 | if (const ReferenceType *RefTy = LeftDecl->getType()->getAs<ReferenceType>()) |
| 7626 | if (RefTy->getPointeeType().isVolatileQualified()) |
| 7627 | return; |
| 7628 | |
| 7629 | S.Diag(OpLoc, diag::warn_self_assignment) |
| 7630 | << LeftDeclRef->getType() |
| 7631 | << lhs->getSourceRange() << rhs->getSourceRange(); |
| 7632 | } |
| 7633 | |
Richard Trieu | e648ac3 | 2011-09-02 03:48:46 +0000 | [diff] [blame] | 7634 | // checkArithmeticNull - Detect when a NULL constant is used improperly in an |
| 7635 | // expression. These are mainly cases where the null pointer is used as an |
| 7636 | // integer instead of a pointer. |
| 7637 | static void checkArithmeticNull(Sema &S, ExprResult &lex, ExprResult &rex, |
| 7638 | SourceLocation Loc, bool isCompare) { |
| 7639 | // The canonical way to check for a GNU null is with isNullPointerConstant, |
| 7640 | // but we use a bit of a hack here for speed; this is a relatively |
| 7641 | // hot path, and isNullPointerConstant is slow. |
| 7642 | bool LeftNull = isa<GNUNullExpr>(lex.get()->IgnoreParenImpCasts()); |
| 7643 | bool RightNull = isa<GNUNullExpr>(rex.get()->IgnoreParenImpCasts()); |
| 7644 | |
| 7645 | // Detect when a NULL constant is used improperly in an expression. These |
| 7646 | // are mainly cases where the null pointer is used as an integer instead |
| 7647 | // of a pointer. |
| 7648 | if (!LeftNull && !RightNull) |
| 7649 | return; |
| 7650 | |
| 7651 | QualType LeftType = lex.get()->getType(); |
| 7652 | QualType RightType = rex.get()->getType(); |
| 7653 | |
| 7654 | // Avoid analyzing cases where the result will either be invalid (and |
| 7655 | // diagnosed as such) or entirely valid and not something to warn about. |
| 7656 | if (LeftType->isBlockPointerType() || LeftType->isMemberPointerType() || |
| 7657 | LeftType->isFunctionType() || RightType->isBlockPointerType() || |
| 7658 | RightType->isMemberPointerType() || RightType->isFunctionType()) |
| 7659 | return; |
| 7660 | |
| 7661 | // Comparison operations would not make sense with a null pointer no matter |
| 7662 | // what the other expression is. |
| 7663 | if (!isCompare) { |
| 7664 | S.Diag(Loc, diag::warn_null_in_arithmetic_operation) |
| 7665 | << (LeftNull ? lex.get()->getSourceRange() : SourceRange()) |
| 7666 | << (RightNull ? rex.get()->getSourceRange() : SourceRange()); |
| 7667 | return; |
| 7668 | } |
| 7669 | |
| 7670 | // The rest of the operations only make sense with a null pointer |
| 7671 | // if the other expression is a pointer. |
| 7672 | if (LeftNull == RightNull || LeftType->isAnyPointerType() || |
| 7673 | LeftType->canDecayToPointerType() || RightType->isAnyPointerType() || |
| 7674 | RightType->canDecayToPointerType()) |
| 7675 | return; |
| 7676 | |
| 7677 | S.Diag(Loc, diag::warn_null_in_comparison_operation) |
| 7678 | << LeftNull /* LHS is NULL */ |
| 7679 | << (LeftNull ? rex.get()->getType() : lex.get()->getType()) |
| 7680 | << lex.get()->getSourceRange() << rex.get()->getSourceRange(); |
| 7681 | } |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7682 | /// CreateBuiltinBinOp - Creates a new built-in binary operation with |
| 7683 | /// operator @p Opc at location @c TokLoc. This routine only supports |
| 7684 | /// built-in operations; ActOnBinOp handles overloaded operators. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 7685 | ExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, |
Argyrios Kyrtzidis | b1fa3dc | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 7686 | BinaryOperatorKind Opc, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7687 | Expr *lhsExpr, Expr *rhsExpr) { |
| 7688 | ExprResult lhs = Owned(lhsExpr), rhs = Owned(rhsExpr); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7689 | QualType ResultTy; // Result type of the binary operator. |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7690 | // The following two variables are used for compound assignment operators |
| 7691 | QualType CompLHSTy; // Type of LHS after promotions for computation |
| 7692 | QualType CompResultTy; // Type of computation result |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7693 | ExprValueKind VK = VK_RValue; |
| 7694 | ExprObjectKind OK = OK_Ordinary; |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7695 | |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 7696 | // Check if a 'foo<int>' involved in a binary op, identifies a single |
| 7697 | // function unambiguously (i.e. an lvalue ala 13.4) |
| 7698 | // But since an assignment can trigger target based overload, exclude it in |
| 7699 | // our blind search. i.e: |
| 7700 | // template<class T> void f(); template<class T, class U> void f(U); |
| 7701 | // f<int> == 0; // resolve f<int> blindly |
| 7702 | // void (*p)(int); p = f<int>; // resolve f<int> using target |
| 7703 | if (Opc != BO_Assign) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 7704 | ExprResult resolvedLHS = CheckPlaceholderExpr(lhs.get()); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 7705 | if (!resolvedLHS.isUsable()) return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7706 | lhs = move(resolvedLHS); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 7707 | |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 7708 | ExprResult resolvedRHS = CheckPlaceholderExpr(rhs.get()); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 7709 | if (!resolvedRHS.isUsable()) return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7710 | rhs = move(resolvedRHS); |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 7711 | } |
| 7712 | |
Richard Trieu | e648ac3 | 2011-09-02 03:48:46 +0000 | [diff] [blame] | 7713 | if (Opc == BO_Mul || Opc == BO_Div || Opc == BO_Rem || Opc == BO_Add || |
| 7714 | Opc == BO_Sub || Opc == BO_Shl || Opc == BO_Shr || Opc == BO_And || |
| 7715 | Opc == BO_Xor || Opc == BO_Or || Opc == BO_MulAssign || |
| 7716 | Opc == BO_DivAssign || Opc == BO_AddAssign || Opc == BO_SubAssign || |
| 7717 | Opc == BO_RemAssign || Opc == BO_ShlAssign || Opc == BO_ShrAssign || |
| 7718 | Opc == BO_AndAssign || Opc == BO_OrAssign || Opc == BO_XorAssign) |
| 7719 | checkArithmeticNull(*this, lhs, rhs, OpLoc, /*isCompare=*/false); |
| 7720 | else if (Opc == BO_LE || Opc == BO_LT || Opc == BO_GE || Opc == BO_GT || |
| 7721 | Opc == BO_EQ || Opc == BO_NE) |
| 7722 | checkArithmeticNull(*this, lhs, rhs, OpLoc, /*isCompare=*/true); |
Richard Trieu | 3e95ba9 | 2011-06-16 21:36:56 +0000 | [diff] [blame] | 7723 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7724 | switch (Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7725 | case BO_Assign: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7726 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, QualType()); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7727 | if (getLangOptions().CPlusPlus && |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7728 | lhs.get()->getObjectKind() != OK_ObjCProperty) { |
| 7729 | VK = lhs.get()->getValueKind(); |
| 7730 | OK = lhs.get()->getObjectKind(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7731 | } |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 7732 | if (!ResultTy.isNull()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7733 | DiagnoseSelfAssignment(*this, lhs.get(), rhs.get(), OpLoc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7734 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7735 | case BO_PtrMemD: |
| 7736 | case BO_PtrMemI: |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7737 | ResultTy = CheckPointerToMemberOperands(lhs, rhs, VK, OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7738 | Opc == BO_PtrMemI); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 7739 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7740 | case BO_Mul: |
| 7741 | case BO_Div: |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 7742 | ResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc, false, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7743 | Opc == BO_Div); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7744 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7745 | case BO_Rem: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7746 | ResultTy = CheckRemainderOperands(lhs, rhs, OpLoc); |
| 7747 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7748 | case BO_Add: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7749 | ResultTy = CheckAdditionOperands(lhs, rhs, OpLoc); |
| 7750 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7751 | case BO_Sub: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7752 | ResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc); |
| 7753 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7754 | case BO_Shl: |
| 7755 | case BO_Shr: |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7756 | ResultTy = CheckShiftOperands(lhs, rhs, OpLoc, Opc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7757 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7758 | case BO_LE: |
| 7759 | case BO_LT: |
| 7760 | case BO_GE: |
| 7761 | case BO_GT: |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7762 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, true); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7763 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7764 | case BO_EQ: |
| 7765 | case BO_NE: |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 7766 | ResultTy = CheckCompareOperands(lhs, rhs, OpLoc, Opc, false); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7767 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7768 | case BO_And: |
| 7769 | case BO_Xor: |
| 7770 | case BO_Or: |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7771 | ResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc); |
| 7772 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7773 | case BO_LAnd: |
| 7774 | case BO_LOr: |
Chris Lattner | 90a8f27 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 7775 | ResultTy = CheckLogicalOperands(lhs, rhs, OpLoc, Opc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7776 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7777 | case BO_MulAssign: |
| 7778 | case BO_DivAssign: |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 7779 | CompResultTy = CheckMultiplyDivideOperands(lhs, rhs, OpLoc, true, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7780 | Opc == BO_DivAssign); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7781 | CompLHSTy = CompResultTy; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7782 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 7783 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7784 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7785 | case BO_RemAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7786 | CompResultTy = CheckRemainderOperands(lhs, rhs, OpLoc, true); |
| 7787 | CompLHSTy = CompResultTy; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7788 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 7789 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7790 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7791 | case BO_AddAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7792 | CompResultTy = CheckAdditionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7793 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 7794 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7795 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7796 | case BO_SubAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7797 | CompResultTy = CheckSubtractionOperands(lhs, rhs, OpLoc, &CompLHSTy); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7798 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 7799 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7800 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7801 | case BO_ShlAssign: |
| 7802 | case BO_ShrAssign: |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 7803 | CompResultTy = CheckShiftOperands(lhs, rhs, OpLoc, Opc, true); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7804 | CompLHSTy = CompResultTy; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7805 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 7806 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7807 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7808 | case BO_AndAssign: |
| 7809 | case BO_XorAssign: |
| 7810 | case BO_OrAssign: |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7811 | CompResultTy = CheckBitwiseOperands(lhs, rhs, OpLoc, true); |
| 7812 | CompLHSTy = CompResultTy; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7813 | if (!CompResultTy.isNull() && !lhs.isInvalid() && !rhs.isInvalid()) |
| 7814 | ResultTy = CheckAssignmentOperands(lhs.get(), rhs, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7815 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7816 | case BO_Comma: |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7817 | ResultTy = CheckCommaOperands(*this, lhs, rhs, OpLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7818 | if (getLangOptions().CPlusPlus && !rhs.isInvalid()) { |
| 7819 | VK = rhs.get()->getValueKind(); |
| 7820 | OK = rhs.get()->getObjectKind(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7821 | } |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7822 | break; |
| 7823 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7824 | if (ResultTy.isNull() || lhs.isInvalid() || rhs.isInvalid()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 7825 | return ExprError(); |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 7826 | |
| 7827 | // Check for array bounds violations for both sides of the BinaryOperator |
| 7828 | CheckArrayAccess(lhs.get()); |
| 7829 | CheckArrayAccess(rhs.get()); |
| 7830 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 7831 | if (CompResultTy.isNull()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7832 | return Owned(new (Context) BinaryOperator(lhs.take(), rhs.take(), Opc, |
| 7833 | ResultTy, VK, OK, OpLoc)); |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 7834 | if (getLangOptions().CPlusPlus && lhs.get()->getObjectKind() != |
| 7835 | OK_ObjCProperty) { |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7836 | VK = VK_LValue; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7837 | OK = lhs.get()->getObjectKind(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7838 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7839 | return Owned(new (Context) CompoundAssignOperator(lhs.take(), rhs.take(), Opc, |
| 7840 | ResultTy, VK, OK, CompLHSTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7841 | CompResultTy, OpLoc)); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 7842 | } |
| 7843 | |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 7844 | /// DiagnoseBitwisePrecedence - Emit a warning when bitwise and comparison |
| 7845 | /// operators are mixed in a way that suggests that the programmer forgot that |
| 7846 | /// comparison operators have higher precedence. The most typical example of |
| 7847 | /// such code is "flags & 0x0020 != 0", which is equivalent to "flags & 1". |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7848 | static void DiagnoseBitwisePrecedence(Sema &Self, BinaryOperatorKind Opc, |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7849 | SourceLocation OpLoc,Expr *lhs,Expr *rhs){ |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 7850 | typedef BinaryOperator BinOp; |
| 7851 | BinOp::Opcode lhsopc = static_cast<BinOp::Opcode>(-1), |
| 7852 | rhsopc = static_cast<BinOp::Opcode>(-1); |
| 7853 | if (BinOp *BO = dyn_cast<BinOp>(lhs)) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7854 | lhsopc = BO->getOpcode(); |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 7855 | if (BinOp *BO = dyn_cast<BinOp>(rhs)) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7856 | rhsopc = BO->getOpcode(); |
| 7857 | |
| 7858 | // Subs are not binary operators. |
| 7859 | if (lhsopc == -1 && rhsopc == -1) |
| 7860 | return; |
| 7861 | |
| 7862 | // Bitwise operations are sometimes used as eager logical ops. |
| 7863 | // Don't diagnose this. |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 7864 | if ((BinOp::isComparisonOp(lhsopc) || BinOp::isBitwiseOp(lhsopc)) && |
| 7865 | (BinOp::isComparisonOp(rhsopc) || BinOp::isBitwiseOp(rhsopc))) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7866 | return; |
| 7867 | |
Richard Trieu | 70979d4 | 2011-08-10 22:41:34 +0000 | [diff] [blame] | 7868 | bool isLeftComp = BinOp::isComparisonOp(lhsopc); |
| 7869 | bool isRightComp = BinOp::isComparisonOp(rhsopc); |
| 7870 | if (!isLeftComp && !isRightComp) return; |
| 7871 | |
| 7872 | SourceRange DiagRange = isLeftComp ? SourceRange(lhs->getLocStart(), OpLoc) |
| 7873 | : SourceRange(OpLoc, rhs->getLocEnd()); |
| 7874 | std::string OpStr = isLeftComp ? BinOp::getOpcodeStr(lhsopc) |
| 7875 | : BinOp::getOpcodeStr(rhsopc); |
| 7876 | SourceRange ParensRange = isLeftComp ? |
| 7877 | SourceRange(cast<BinOp>(lhs)->getRHS()->getLocStart(), |
| 7878 | rhs->getLocEnd()) |
| 7879 | : SourceRange(lhs->getLocStart(), |
| 7880 | cast<BinOp>(rhs)->getLHS()->getLocStart()); |
| 7881 | |
| 7882 | Self.Diag(OpLoc, diag::warn_precedence_bitwise_rel) |
| 7883 | << DiagRange << BinOp::getOpcodeStr(Opc) << OpStr; |
| 7884 | SuggestParentheses(Self, OpLoc, |
| 7885 | Self.PDiag(diag::note_precedence_bitwise_silence) << OpStr, |
| 7886 | rhs->getSourceRange()); |
| 7887 | SuggestParentheses(Self, OpLoc, |
| 7888 | Self.PDiag(diag::note_precedence_bitwise_first) << BinOp::getOpcodeStr(Opc), |
| 7889 | ParensRange); |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7890 | } |
| 7891 | |
Argyrios Kyrtzidis | 33f46e2 | 2011-06-20 18:41:26 +0000 | [diff] [blame] | 7892 | /// \brief It accepts a '&' expr that is inside a '|' one. |
| 7893 | /// Emit a diagnostic together with a fixit hint that wraps the '&' expression |
| 7894 | /// in parentheses. |
| 7895 | static void |
| 7896 | EmitDiagnosticForBitwiseAndInBitwiseOr(Sema &Self, SourceLocation OpLoc, |
| 7897 | BinaryOperator *Bop) { |
| 7898 | assert(Bop->getOpcode() == BO_And); |
| 7899 | Self.Diag(Bop->getOperatorLoc(), diag::warn_bitwise_and_in_bitwise_or) |
| 7900 | << Bop->getSourceRange() << OpLoc; |
| 7901 | SuggestParentheses(Self, Bop->getOperatorLoc(), |
| 7902 | Self.PDiag(diag::note_bitwise_and_in_bitwise_or_silence), |
| 7903 | Bop->getSourceRange()); |
| 7904 | } |
| 7905 | |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7906 | /// \brief It accepts a '&&' expr that is inside a '||' one. |
| 7907 | /// Emit a diagnostic together with a fixit hint that wraps the '&&' expression |
| 7908 | /// in parentheses. |
| 7909 | static void |
| 7910 | EmitDiagnosticForLogicalAndInLogicalOr(Sema &Self, SourceLocation OpLoc, |
Argyrios Kyrtzidis | a61aedc | 2011-04-22 19:16:27 +0000 | [diff] [blame] | 7911 | BinaryOperator *Bop) { |
| 7912 | assert(Bop->getOpcode() == BO_LAnd); |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 7913 | Self.Diag(Bop->getOperatorLoc(), diag::warn_logical_and_in_logical_or) |
| 7914 | << Bop->getSourceRange() << OpLoc; |
Argyrios Kyrtzidis | a61aedc | 2011-04-22 19:16:27 +0000 | [diff] [blame] | 7915 | SuggestParentheses(Self, Bop->getOperatorLoc(), |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7916 | Self.PDiag(diag::note_logical_and_in_logical_or_silence), |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 7917 | Bop->getSourceRange()); |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7918 | } |
| 7919 | |
| 7920 | /// \brief Returns true if the given expression can be evaluated as a constant |
| 7921 | /// 'true'. |
| 7922 | static bool EvaluatesAsTrue(Sema &S, Expr *E) { |
| 7923 | bool Res; |
| 7924 | return E->EvaluateAsBooleanCondition(Res, S.getASTContext()) && Res; |
| 7925 | } |
| 7926 | |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 7927 | /// \brief Returns true if the given expression can be evaluated as a constant |
| 7928 | /// 'false'. |
| 7929 | static bool EvaluatesAsFalse(Sema &S, Expr *E) { |
| 7930 | bool Res; |
| 7931 | return E->EvaluateAsBooleanCondition(Res, S.getASTContext()) && !Res; |
| 7932 | } |
| 7933 | |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7934 | /// \brief Look for '&&' in the left hand of a '||' expr. |
| 7935 | static void DiagnoseLogicalAndInLogicalOrLHS(Sema &S, SourceLocation OpLoc, |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 7936 | Expr *OrLHS, Expr *OrRHS) { |
| 7937 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(OrLHS)) { |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 7938 | if (Bop->getOpcode() == BO_LAnd) { |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 7939 | // If it's "a && b || 0" don't warn since the precedence doesn't matter. |
| 7940 | if (EvaluatesAsFalse(S, OrRHS)) |
| 7941 | return; |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7942 | // If it's "1 && a || b" don't warn since the precedence doesn't matter. |
| 7943 | if (!EvaluatesAsTrue(S, Bop->getLHS())) |
| 7944 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, Bop); |
| 7945 | } else if (Bop->getOpcode() == BO_LOr) { |
| 7946 | if (BinaryOperator *RBop = dyn_cast<BinaryOperator>(Bop->getRHS())) { |
| 7947 | // If it's "a || b && 1 || c" we didn't warn earlier for |
| 7948 | // "a || b && 1", but warn now. |
| 7949 | if (RBop->getOpcode() == BO_LAnd && EvaluatesAsTrue(S, RBop->getRHS())) |
| 7950 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, RBop); |
| 7951 | } |
| 7952 | } |
| 7953 | } |
| 7954 | } |
| 7955 | |
| 7956 | /// \brief Look for '&&' in the right hand of a '||' expr. |
| 7957 | static void DiagnoseLogicalAndInLogicalOrRHS(Sema &S, SourceLocation OpLoc, |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 7958 | Expr *OrLHS, Expr *OrRHS) { |
| 7959 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(OrRHS)) { |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7960 | if (Bop->getOpcode() == BO_LAnd) { |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 7961 | // If it's "0 || a && b" don't warn since the precedence doesn't matter. |
| 7962 | if (EvaluatesAsFalse(S, OrLHS)) |
| 7963 | return; |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7964 | // If it's "a || b && 1" don't warn since the precedence doesn't matter. |
| 7965 | if (!EvaluatesAsTrue(S, Bop->getRHS())) |
| 7966 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, Bop); |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 7967 | } |
| 7968 | } |
| 7969 | } |
| 7970 | |
Argyrios Kyrtzidis | 33f46e2 | 2011-06-20 18:41:26 +0000 | [diff] [blame] | 7971 | /// \brief Look for '&' in the left or right hand of a '|' expr. |
| 7972 | static void DiagnoseBitwiseAndInBitwiseOr(Sema &S, SourceLocation OpLoc, |
| 7973 | Expr *OrArg) { |
| 7974 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(OrArg)) { |
| 7975 | if (Bop->getOpcode() == BO_And) |
| 7976 | return EmitDiagnosticForBitwiseAndInBitwiseOr(S, OpLoc, Bop); |
| 7977 | } |
| 7978 | } |
| 7979 | |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7980 | /// DiagnoseBinOpPrecedence - Emit warnings for expressions with tricky |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 7981 | /// precedence. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7982 | static void DiagnoseBinOpPrecedence(Sema &Self, BinaryOperatorKind Opc, |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 7983 | SourceLocation OpLoc, Expr *lhs, Expr *rhs){ |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 7984 | // Diagnose "arg1 'bitwise' arg2 'eq' arg3". |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 7985 | if (BinaryOperator::isBitwiseOp(Opc)) |
Argyrios Kyrtzidis | 33f46e2 | 2011-06-20 18:41:26 +0000 | [diff] [blame] | 7986 | DiagnoseBitwisePrecedence(Self, Opc, OpLoc, lhs, rhs); |
| 7987 | |
| 7988 | // Diagnose "arg1 & arg2 | arg3" |
| 7989 | if (Opc == BO_Or && !OpLoc.isMacroID()/* Don't warn in macros. */) { |
| 7990 | DiagnoseBitwiseAndInBitwiseOr(Self, OpLoc, lhs); |
| 7991 | DiagnoseBitwiseAndInBitwiseOr(Self, OpLoc, rhs); |
| 7992 | } |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 7993 | |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 7994 | // Warn about arg1 || arg2 && arg3, as GCC 4.3+ does. |
| 7995 | // 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] | 7996 | if (Opc == BO_LOr && !OpLoc.isMacroID()/* Don't warn in macros. */) { |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 7997 | DiagnoseLogicalAndInLogicalOrLHS(Self, OpLoc, lhs, rhs); |
| 7998 | DiagnoseLogicalAndInLogicalOrRHS(Self, OpLoc, lhs, rhs); |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 7999 | } |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8000 | } |
| 8001 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8002 | // Binary Operators. 'Tok' is the token for the operator. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8003 | ExprResult Sema::ActOnBinOp(Scope *S, SourceLocation TokLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8004 | tok::TokenKind Kind, |
| 8005 | Expr *lhs, Expr *rhs) { |
| 8006 | BinaryOperatorKind Opc = ConvertTokenKindToBinaryOpcode(Kind); |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 8007 | assert((lhs != 0) && "ActOnBinOp(): missing left expression"); |
| 8008 | assert((rhs != 0) && "ActOnBinOp(): missing right expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8009 | |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8010 | // Emit warnings for tricky precedence issues, e.g. "bitfield & 0x4 == 0" |
| 8011 | DiagnoseBinOpPrecedence(*this, Opc, TokLoc, lhs, rhs); |
| 8012 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8013 | return BuildBinOp(S, TokLoc, Opc, lhs, rhs); |
| 8014 | } |
| 8015 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8016 | ExprResult Sema::BuildBinOp(Scope *S, SourceLocation OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8017 | BinaryOperatorKind Opc, |
| 8018 | Expr *lhs, Expr *rhs) { |
John McCall | 01b2e4e | 2010-12-06 05:26:58 +0000 | [diff] [blame] | 8019 | if (getLangOptions().CPlusPlus) { |
| 8020 | bool UseBuiltinOperator; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8021 | |
John McCall | 01b2e4e | 2010-12-06 05:26:58 +0000 | [diff] [blame] | 8022 | if (lhs->isTypeDependent() || rhs->isTypeDependent()) { |
| 8023 | UseBuiltinOperator = false; |
| 8024 | } else if (Opc == BO_Assign && lhs->getObjectKind() == OK_ObjCProperty) { |
| 8025 | UseBuiltinOperator = true; |
| 8026 | } else { |
| 8027 | UseBuiltinOperator = !lhs->getType()->isOverloadableType() && |
| 8028 | !rhs->getType()->isOverloadableType(); |
| 8029 | } |
| 8030 | |
| 8031 | if (!UseBuiltinOperator) { |
| 8032 | // Find all of the overloaded operators visible from this |
| 8033 | // point. We perform both an operator-name lookup from the local |
| 8034 | // scope and an argument-dependent lookup based on the types of |
| 8035 | // the arguments. |
| 8036 | UnresolvedSet<16> Functions; |
| 8037 | OverloadedOperatorKind OverOp |
| 8038 | = BinaryOperator::getOverloadedOperator(Opc); |
| 8039 | if (S && OverOp != OO_None) |
| 8040 | LookupOverloadedOperatorName(OverOp, S, lhs->getType(), rhs->getType(), |
| 8041 | Functions); |
| 8042 | |
| 8043 | // Build the (potentially-overloaded, potentially-dependent) |
| 8044 | // binary operation. |
| 8045 | return CreateOverloadedBinOp(OpLoc, Opc, Functions, lhs, rhs); |
| 8046 | } |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 8047 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8048 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8049 | // Build a built-in binary operation. |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8050 | return CreateBuiltinBinOp(OpLoc, Opc, lhs, rhs); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8051 | } |
| 8052 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8053 | ExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc, |
Argyrios Kyrtzidis | b1fa3dc | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 8054 | UnaryOperatorKind Opc, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8055 | Expr *InputExpr) { |
| 8056 | ExprResult Input = Owned(InputExpr); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8057 | ExprValueKind VK = VK_RValue; |
| 8058 | ExprObjectKind OK = OK_Ordinary; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8059 | QualType resultType; |
| 8060 | switch (Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8061 | case UO_PreInc: |
| 8062 | case UO_PreDec: |
| 8063 | case UO_PostInc: |
| 8064 | case UO_PostDec: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8065 | resultType = CheckIncrementDecrementOperand(*this, Input.get(), VK, OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8066 | Opc == UO_PreInc || |
| 8067 | Opc == UO_PostInc, |
| 8068 | Opc == UO_PreInc || |
| 8069 | Opc == UO_PreDec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8070 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8071 | case UO_AddrOf: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8072 | resultType = CheckAddressOfOperand(*this, Input.get(), OpLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8073 | break; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 8074 | case UO_Deref: { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8075 | ExprResult resolved = CheckPlaceholderExpr(Input.get()); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 8076 | if (!resolved.isUsable()) return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8077 | Input = move(resolved); |
| 8078 | Input = DefaultFunctionArrayLvalueConversion(Input.take()); |
| 8079 | resultType = CheckIndirectionOperand(*this, Input.get(), VK, OpLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8080 | break; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 8081 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8082 | case UO_Plus: |
| 8083 | case UO_Minus: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8084 | Input = UsualUnaryConversions(Input.take()); |
| 8085 | if (Input.isInvalid()) return ExprError(); |
| 8086 | resultType = Input.get()->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8087 | if (resultType->isDependentType()) |
| 8088 | break; |
Douglas Gregor | 0061962 | 2010-06-22 23:41:02 +0000 | [diff] [blame] | 8089 | if (resultType->isArithmeticType() || // C99 6.5.3.3p1 |
| 8090 | resultType->isVectorType()) |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8091 | break; |
| 8092 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6-7 |
| 8093 | resultType->isEnumeralType()) |
| 8094 | break; |
| 8095 | else if (getLangOptions().CPlusPlus && // C++ [expr.unary.op]p6 |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8096 | Opc == UO_Plus && |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8097 | resultType->isPointerType()) |
| 8098 | break; |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8099 | else if (resultType->isPlaceholderType()) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8100 | Input = CheckPlaceholderExpr(Input.take()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8101 | if (Input.isInvalid()) return ExprError(); |
| 8102 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input.take()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8103 | } |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8104 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8105 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8106 | << resultType << Input.get()->getSourceRange()); |
| 8107 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8108 | case UO_Not: // bitwise complement |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8109 | Input = UsualUnaryConversions(Input.take()); |
| 8110 | if (Input.isInvalid()) return ExprError(); |
| 8111 | resultType = Input.get()->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8112 | if (resultType->isDependentType()) |
| 8113 | break; |
Chris Lattner | 02a6514 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 8114 | // C99 6.5.3.3p1. We allow complex int and float as a GCC extension. |
| 8115 | if (resultType->isComplexType() || resultType->isComplexIntegerType()) |
| 8116 | // C99 does not support '~' for complex conjugation. |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 8117 | Diag(OpLoc, diag::ext_integer_complement_complex) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8118 | << resultType << Input.get()->getSourceRange(); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8119 | else if (resultType->hasIntegerRepresentation()) |
| 8120 | break; |
| 8121 | else if (resultType->isPlaceholderType()) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8122 | Input = CheckPlaceholderExpr(Input.take()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8123 | if (Input.isInvalid()) return ExprError(); |
| 8124 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input.take()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8125 | } else { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8126 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8127 | << resultType << Input.get()->getSourceRange()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8128 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8129 | break; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8130 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8131 | case UO_LNot: // logical negation |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8132 | // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8133 | Input = DefaultFunctionArrayLvalueConversion(Input.take()); |
| 8134 | if (Input.isInvalid()) return ExprError(); |
| 8135 | resultType = Input.get()->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8136 | if (resultType->isDependentType()) |
| 8137 | break; |
Abramo Bagnara | 737d544 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 8138 | if (resultType->isScalarType()) { |
| 8139 | // C99 6.5.3.3p1: ok, fallthrough; |
| 8140 | if (Context.getLangOptions().CPlusPlus) { |
| 8141 | // C++03 [expr.unary.op]p8, C++0x [expr.unary.op]p9: |
| 8142 | // operand contextually converted to bool. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8143 | Input = ImpCastExprToType(Input.take(), Context.BoolTy, |
| 8144 | ScalarTypeToBooleanCastKind(resultType)); |
Abramo Bagnara | 737d544 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 8145 | } |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8146 | } else if (resultType->isPlaceholderType()) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8147 | Input = CheckPlaceholderExpr(Input.take()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8148 | if (Input.isInvalid()) return ExprError(); |
| 8149 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input.take()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8150 | } else { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8151 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8152 | << resultType << Input.get()->getSourceRange()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8153 | } |
Douglas Gregor | ea844f3 | 2010-09-20 17:13:33 +0000 | [diff] [blame] | 8154 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8155 | // LNot always has type int. C99 6.5.3.3p5. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8156 | // In C++, it's bool. C++ 5.3.1p8 |
Argyrios Kyrtzidis | 16f744b | 2011-02-18 20:55:15 +0000 | [diff] [blame] | 8157 | resultType = Context.getLogicalOperationType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8158 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8159 | case UO_Real: |
| 8160 | case UO_Imag: |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8161 | resultType = CheckRealImagOperand(*this, Input, OpLoc, Opc == UO_Real); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8162 | // _Real and _Imag map ordinary l-values into ordinary l-values. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8163 | if (Input.isInvalid()) return ExprError(); |
| 8164 | if (Input.get()->getValueKind() != VK_RValue && |
| 8165 | Input.get()->getObjectKind() == OK_Ordinary) |
| 8166 | VK = Input.get()->getValueKind(); |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 8167 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8168 | case UO_Extension: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8169 | resultType = Input.get()->getType(); |
| 8170 | VK = Input.get()->getValueKind(); |
| 8171 | OK = Input.get()->getObjectKind(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8172 | break; |
| 8173 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8174 | if (resultType.isNull() || Input.isInvalid()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8175 | return ExprError(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8176 | |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8177 | // Check for array bounds violations in the operand of the UnaryOperator, |
| 8178 | // except for the '*' and '&' operators that have to be handled specially |
| 8179 | // by CheckArrayAccess (as there are special cases like &array[arraysize] |
| 8180 | // that are explicitly defined as valid by the standard). |
| 8181 | if (Opc != UO_AddrOf && Opc != UO_Deref) |
| 8182 | CheckArrayAccess(Input.get()); |
| 8183 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8184 | return Owned(new (Context) UnaryOperator(Input.take(), Opc, resultType, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8185 | VK, OK, OpLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8186 | } |
| 8187 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8188 | ExprResult Sema::BuildUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8189 | UnaryOperatorKind Opc, |
| 8190 | Expr *Input) { |
Anders Carlsson | a8a1e3d | 2009-11-14 21:26:41 +0000 | [diff] [blame] | 8191 | if (getLangOptions().CPlusPlus && Input->getType()->isOverloadableType() && |
Eli Friedman | 957c094 | 2010-09-05 23:15:52 +0000 | [diff] [blame] | 8192 | UnaryOperator::getOverloadedOperator(Opc) != OO_None) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8193 | // Find all of the overloaded operators visible from this |
| 8194 | // point. We perform both an operator-name lookup from the local |
| 8195 | // scope and an argument-dependent lookup based on the types of |
| 8196 | // the arguments. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8197 | UnresolvedSet<16> Functions; |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8198 | OverloadedOperatorKind OverOp = UnaryOperator::getOverloadedOperator(Opc); |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8199 | if (S && OverOp != OO_None) |
| 8200 | LookupOverloadedOperatorName(OverOp, S, Input->getType(), QualType(), |
| 8201 | Functions); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8202 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8203 | return CreateOverloadedUnaryOp(OpLoc, Opc, Functions, Input); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8204 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8205 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8206 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8207 | } |
| 8208 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8209 | // Unary Operators. 'Tok' is the token for the operator. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8210 | ExprResult Sema::ActOnUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 8211 | tok::TokenKind Op, Expr *Input) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8212 | return BuildUnaryOp(S, OpLoc, ConvertTokenKindToUnaryOpcode(Op), Input); |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8213 | } |
| 8214 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 8215 | /// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo". |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 8216 | ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc, SourceLocation LabLoc, |
Chris Lattner | 57ad378 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 8217 | LabelDecl *TheDecl) { |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 8218 | TheDecl->setUsed(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8219 | // 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] | 8220 | return Owned(new (Context) AddrLabelExpr(OpLoc, LabLoc, TheDecl, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8221 | Context.getPointerType(Context.VoidTy))); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8222 | } |
| 8223 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8224 | /// Given the last statement in a statement-expression, check whether |
| 8225 | /// the result is a producing expression (like a call to an |
| 8226 | /// ns_returns_retained function) and, if so, rebuild it to hoist the |
| 8227 | /// release out of the full-expression. Otherwise, return null. |
| 8228 | /// Cannot fail. |
| 8229 | static Expr *maybeRebuildARCConsumingStmt(Stmt *s) { |
| 8230 | // Should always be wrapped with one of these. |
| 8231 | ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(s); |
| 8232 | if (!cleanups) return 0; |
| 8233 | |
| 8234 | ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(cleanups->getSubExpr()); |
| 8235 | if (!cast || cast->getCastKind() != CK_ObjCConsumeObject) |
| 8236 | return 0; |
| 8237 | |
| 8238 | // Splice out the cast. This shouldn't modify any interesting |
| 8239 | // features of the statement. |
| 8240 | Expr *producer = cast->getSubExpr(); |
| 8241 | assert(producer->getType() == cast->getType()); |
| 8242 | assert(producer->getValueKind() == cast->getValueKind()); |
| 8243 | cleanups->setSubExpr(producer); |
| 8244 | return cleanups; |
| 8245 | } |
| 8246 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8247 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8248 | Sema::ActOnStmtExpr(SourceLocation LPLoc, Stmt *SubStmt, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8249 | SourceLocation RPLoc) { // "({..})" |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8250 | assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!"); |
| 8251 | CompoundStmt *Compound = cast<CompoundStmt>(SubStmt); |
| 8252 | |
Douglas Gregor | dd8f569 | 2010-03-10 04:54:39 +0000 | [diff] [blame] | 8253 | bool isFileScope |
| 8254 | = (getCurFunctionOrMethodDecl() == 0) && (getCurBlock() == 0); |
Chris Lattner | 4a049f0 | 2009-04-25 19:11:05 +0000 | [diff] [blame] | 8255 | if (isFileScope) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8256 | return ExprError(Diag(LPLoc, diag::err_stmtexpr_file_scope)); |
Eli Friedman | dca2b73 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 8257 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8258 | // FIXME: there are a variety of strange constraints to enforce here, for |
| 8259 | // example, it is not possible to goto into a stmt expression apparently. |
| 8260 | // More semantic analysis is needed. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8261 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8262 | // If there are sub stmts in the compound stmt, take the type of the last one |
| 8263 | // as the type of the stmtexpr. |
| 8264 | QualType Ty = Context.VoidTy; |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8265 | bool StmtExprMayBindToTemp = false; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8266 | if (!Compound->body_empty()) { |
| 8267 | Stmt *LastStmt = Compound->body_back(); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8268 | LabelStmt *LastLabelStmt = 0; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8269 | // If LastStmt is a label, skip down through into the body. |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8270 | while (LabelStmt *Label = dyn_cast<LabelStmt>(LastStmt)) { |
| 8271 | LastLabelStmt = Label; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8272 | LastStmt = Label->getSubStmt(); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8273 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8274 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8275 | if (Expr *LastE = dyn_cast<Expr>(LastStmt)) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8276 | // Do function/array conversion on the last expression, but not |
| 8277 | // lvalue-to-rvalue. However, initialize an unqualified type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8278 | ExprResult LastExpr = DefaultFunctionArrayConversion(LastE); |
| 8279 | if (LastExpr.isInvalid()) |
| 8280 | return ExprError(); |
| 8281 | Ty = LastExpr.get()->getType().getUnqualifiedType(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8282 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8283 | if (!Ty->isDependentType() && !LastExpr.get()->isTypeDependent()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8284 | // In ARC, if the final expression ends in a consume, splice |
| 8285 | // the consume out and bind it later. In the alternate case |
| 8286 | // (when dealing with a retainable type), the result |
| 8287 | // initialization will create a produce. In both cases the |
| 8288 | // result will be +1, and we'll need to balance that out with |
| 8289 | // a bind. |
| 8290 | if (Expr *rebuiltLastStmt |
| 8291 | = maybeRebuildARCConsumingStmt(LastExpr.get())) { |
| 8292 | LastExpr = rebuiltLastStmt; |
| 8293 | } else { |
| 8294 | LastExpr = PerformCopyInitialization( |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8295 | InitializedEntity::InitializeResult(LPLoc, |
| 8296 | Ty, |
| 8297 | false), |
| 8298 | SourceLocation(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8299 | LastExpr); |
| 8300 | } |
| 8301 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8302 | if (LastExpr.isInvalid()) |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8303 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8304 | if (LastExpr.get() != 0) { |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8305 | if (!LastLabelStmt) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8306 | Compound->setLastStmt(LastExpr.take()); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8307 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8308 | LastLabelStmt->setSubStmt(LastExpr.take()); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8309 | StmtExprMayBindToTemp = true; |
| 8310 | } |
| 8311 | } |
| 8312 | } |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8313 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8314 | |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 8315 | // FIXME: Check that expression type is complete/non-abstract; statement |
| 8316 | // expressions are not lvalues. |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8317 | Expr *ResStmtExpr = new (Context) StmtExpr(Compound, Ty, LPLoc, RPLoc); |
| 8318 | if (StmtExprMayBindToTemp) |
| 8319 | return MaybeBindToTemporary(ResStmtExpr); |
| 8320 | return Owned(ResStmtExpr); |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8321 | } |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 8322 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8323 | ExprResult Sema::BuildBuiltinOffsetOf(SourceLocation BuiltinLoc, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8324 | TypeSourceInfo *TInfo, |
| 8325 | OffsetOfComponent *CompPtr, |
| 8326 | unsigned NumComponents, |
| 8327 | SourceLocation RParenLoc) { |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8328 | QualType ArgTy = TInfo->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8329 | bool Dependent = ArgTy->isDependentType(); |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 8330 | SourceRange TypeRange = TInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8331 | |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 8332 | // We must have at least one component that refers to the type, and the first |
| 8333 | // one is known to be a field designator. Verify that the ArgTy represents |
| 8334 | // a struct/union/class. |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8335 | if (!Dependent && !ArgTy->isRecordType()) |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8336 | return ExprError(Diag(BuiltinLoc, diag::err_offsetof_record_type) |
| 8337 | << ArgTy << TypeRange); |
| 8338 | |
| 8339 | // Type must be complete per C99 7.17p3 because a declaring a variable |
| 8340 | // with an incomplete type would be ill-formed. |
| 8341 | if (!Dependent |
| 8342 | && RequireCompleteType(BuiltinLoc, ArgTy, |
| 8343 | PDiag(diag::err_offsetof_incomplete_type) |
| 8344 | << TypeRange)) |
| 8345 | return ExprError(); |
| 8346 | |
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 8347 | // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a |
| 8348 | // GCC extension, diagnose them. |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 8349 | // FIXME: This diagnostic isn't actually visible because the location is in |
| 8350 | // a system header! |
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 8351 | if (NumComponents != 1) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 8352 | Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator) |
| 8353 | << SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8354 | |
| 8355 | bool DidWarnAboutNonPOD = false; |
| 8356 | QualType CurrentType = ArgTy; |
| 8357 | typedef OffsetOfExpr::OffsetOfNode OffsetOfNode; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8358 | SmallVector<OffsetOfNode, 4> Comps; |
| 8359 | SmallVector<Expr*, 4> Exprs; |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8360 | for (unsigned i = 0; i != NumComponents; ++i) { |
| 8361 | const OffsetOfComponent &OC = CompPtr[i]; |
| 8362 | if (OC.isBrackets) { |
| 8363 | // Offset of an array sub-field. TODO: Should we allow vector elements? |
| 8364 | if (!CurrentType->isDependentType()) { |
| 8365 | const ArrayType *AT = Context.getAsArrayType(CurrentType); |
| 8366 | if(!AT) |
| 8367 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_array_type) |
| 8368 | << CurrentType); |
| 8369 | CurrentType = AT->getElementType(); |
| 8370 | } else |
| 8371 | CurrentType = Context.DependentTy; |
| 8372 | |
| 8373 | // The expression must be an integral expression. |
| 8374 | // FIXME: An integral constant expression? |
| 8375 | Expr *Idx = static_cast<Expr*>(OC.U.E); |
| 8376 | if (!Idx->isTypeDependent() && !Idx->isValueDependent() && |
| 8377 | !Idx->getType()->isIntegerType()) |
| 8378 | return ExprError(Diag(Idx->getLocStart(), |
| 8379 | diag::err_typecheck_subscript_not_integer) |
| 8380 | << Idx->getSourceRange()); |
| 8381 | |
| 8382 | // Record this array index. |
| 8383 | Comps.push_back(OffsetOfNode(OC.LocStart, Exprs.size(), OC.LocEnd)); |
| 8384 | Exprs.push_back(Idx); |
| 8385 | continue; |
| 8386 | } |
| 8387 | |
| 8388 | // Offset of a field. |
| 8389 | if (CurrentType->isDependentType()) { |
| 8390 | // We have the offset of a field, but we can't look into the dependent |
| 8391 | // type. Just record the identifier of the field. |
| 8392 | Comps.push_back(OffsetOfNode(OC.LocStart, OC.U.IdentInfo, OC.LocEnd)); |
| 8393 | CurrentType = Context.DependentTy; |
| 8394 | continue; |
| 8395 | } |
| 8396 | |
| 8397 | // We need to have a complete type to look into. |
| 8398 | if (RequireCompleteType(OC.LocStart, CurrentType, |
| 8399 | diag::err_offsetof_incomplete_type)) |
| 8400 | return ExprError(); |
| 8401 | |
| 8402 | // Look for the designated field. |
| 8403 | const RecordType *RC = CurrentType->getAs<RecordType>(); |
| 8404 | if (!RC) |
| 8405 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_record_type) |
| 8406 | << CurrentType); |
| 8407 | RecordDecl *RD = RC->getDecl(); |
| 8408 | |
| 8409 | // C++ [lib.support.types]p5: |
| 8410 | // The macro offsetof accepts a restricted set of type arguments in this |
| 8411 | // International Standard. type shall be a POD structure or a POD union |
| 8412 | // (clause 9). |
| 8413 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
| 8414 | if (!CRD->isPOD() && !DidWarnAboutNonPOD && |
Ted Kremenek | 762696f | 2011-02-23 01:51:43 +0000 | [diff] [blame] | 8415 | DiagRuntimeBehavior(BuiltinLoc, 0, |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8416 | PDiag(diag::warn_offsetof_non_pod_type) |
| 8417 | << SourceRange(CompPtr[0].LocStart, OC.LocEnd) |
| 8418 | << CurrentType)) |
| 8419 | DidWarnAboutNonPOD = true; |
| 8420 | } |
| 8421 | |
| 8422 | // Look for the field. |
| 8423 | LookupResult R(*this, OC.U.IdentInfo, OC.LocStart, LookupMemberName); |
| 8424 | LookupQualifiedName(R, RD); |
| 8425 | FieldDecl *MemberDecl = R.getAsSingle<FieldDecl>(); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8426 | IndirectFieldDecl *IndirectMemberDecl = 0; |
| 8427 | if (!MemberDecl) { |
Benjamin Kramer | d981146 | 2010-11-21 14:11:41 +0000 | [diff] [blame] | 8428 | if ((IndirectMemberDecl = R.getAsSingle<IndirectFieldDecl>())) |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8429 | MemberDecl = IndirectMemberDecl->getAnonField(); |
| 8430 | } |
| 8431 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8432 | if (!MemberDecl) |
| 8433 | return ExprError(Diag(BuiltinLoc, diag::err_no_member) |
| 8434 | << OC.U.IdentInfo << RD << SourceRange(OC.LocStart, |
| 8435 | OC.LocEnd)); |
| 8436 | |
Douglas Gregor | 9d5d60f | 2010-04-28 22:36:06 +0000 | [diff] [blame] | 8437 | // C99 7.17p3: |
| 8438 | // (If the specified member is a bit-field, the behavior is undefined.) |
| 8439 | // |
| 8440 | // We diagnose this as an error. |
| 8441 | if (MemberDecl->getBitWidth()) { |
| 8442 | Diag(OC.LocEnd, diag::err_offsetof_bitfield) |
| 8443 | << MemberDecl->getDeclName() |
| 8444 | << SourceRange(BuiltinLoc, RParenLoc); |
| 8445 | Diag(MemberDecl->getLocation(), diag::note_bitfield_decl); |
| 8446 | return ExprError(); |
| 8447 | } |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 8448 | |
| 8449 | RecordDecl *Parent = MemberDecl->getParent(); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8450 | if (IndirectMemberDecl) |
| 8451 | Parent = cast<RecordDecl>(IndirectMemberDecl->getDeclContext()); |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 8452 | |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 8453 | // If the member was found in a base class, introduce OffsetOfNodes for |
| 8454 | // the base class indirections. |
| 8455 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 8456 | /*DetectVirtual=*/false); |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 8457 | if (IsDerivedFrom(CurrentType, Context.getTypeDeclType(Parent), Paths)) { |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 8458 | CXXBasePath &Path = Paths.front(); |
| 8459 | for (CXXBasePath::iterator B = Path.begin(), BEnd = Path.end(); |
| 8460 | B != BEnd; ++B) |
| 8461 | Comps.push_back(OffsetOfNode(B->Base)); |
| 8462 | } |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 8463 | |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8464 | if (IndirectMemberDecl) { |
| 8465 | for (IndirectFieldDecl::chain_iterator FI = |
| 8466 | IndirectMemberDecl->chain_begin(), |
| 8467 | FEnd = IndirectMemberDecl->chain_end(); FI != FEnd; FI++) { |
| 8468 | assert(isa<FieldDecl>(*FI)); |
| 8469 | Comps.push_back(OffsetOfNode(OC.LocStart, |
| 8470 | cast<FieldDecl>(*FI), OC.LocEnd)); |
| 8471 | } |
| 8472 | } else |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8473 | Comps.push_back(OffsetOfNode(OC.LocStart, MemberDecl, OC.LocEnd)); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 8474 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8475 | CurrentType = MemberDecl->getType().getNonReferenceType(); |
| 8476 | } |
| 8477 | |
| 8478 | return Owned(OffsetOfExpr::Create(Context, Context.getSizeType(), BuiltinLoc, |
| 8479 | TInfo, Comps.data(), Comps.size(), |
| 8480 | Exprs.data(), Exprs.size(), RParenLoc)); |
| 8481 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8482 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8483 | ExprResult Sema::ActOnBuiltinOffsetOf(Scope *S, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8484 | SourceLocation BuiltinLoc, |
| 8485 | SourceLocation TypeLoc, |
| 8486 | ParsedType argty, |
| 8487 | OffsetOfComponent *CompPtr, |
| 8488 | unsigned NumComponents, |
| 8489 | SourceLocation RPLoc) { |
| 8490 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8491 | TypeSourceInfo *ArgTInfo; |
| 8492 | QualType ArgTy = GetTypeFromParser(argty, &ArgTInfo); |
| 8493 | if (ArgTy.isNull()) |
| 8494 | return ExprError(); |
| 8495 | |
Eli Friedman | 5a15dc1 | 2010-08-05 10:15:45 +0000 | [diff] [blame] | 8496 | if (!ArgTInfo) |
| 8497 | ArgTInfo = Context.getTrivialTypeSourceInfo(ArgTy, TypeLoc); |
| 8498 | |
| 8499 | return BuildBuiltinOffsetOf(BuiltinLoc, ArgTInfo, CompPtr, NumComponents, |
| 8500 | RPLoc); |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 8501 | } |
| 8502 | |
| 8503 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8504 | ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8505 | Expr *CondExpr, |
| 8506 | Expr *LHSExpr, Expr *RHSExpr, |
| 8507 | SourceLocation RPLoc) { |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 8508 | assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)"); |
| 8509 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8510 | ExprValueKind VK = VK_RValue; |
| 8511 | ExprObjectKind OK = OK_Ordinary; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8512 | QualType resType; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 8513 | bool ValueDependent = false; |
Douglas Gregor | c9ecc57 | 2009-05-19 22:43:30 +0000 | [diff] [blame] | 8514 | if (CondExpr->isTypeDependent() || CondExpr->isValueDependent()) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8515 | resType = Context.DependentTy; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 8516 | ValueDependent = true; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8517 | } else { |
| 8518 | // The conditional expression is required to be a constant expression. |
| 8519 | llvm::APSInt condEval(32); |
| 8520 | SourceLocation ExpLoc; |
| 8521 | if (!CondExpr->isIntegerConstantExpr(condEval, Context, &ExpLoc)) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8522 | return ExprError(Diag(ExpLoc, |
| 8523 | diag::err_typecheck_choose_expr_requires_constant) |
| 8524 | << CondExpr->getSourceRange()); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 8525 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8526 | // 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] | 8527 | Expr *ActiveExpr = condEval.getZExtValue() ? LHSExpr : RHSExpr; |
| 8528 | |
| 8529 | resType = ActiveExpr->getType(); |
| 8530 | ValueDependent = ActiveExpr->isValueDependent(); |
| 8531 | VK = ActiveExpr->getValueKind(); |
| 8532 | OK = ActiveExpr->getObjectKind(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8533 | } |
| 8534 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8535 | return Owned(new (Context) ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8536 | resType, VK, OK, RPLoc, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 8537 | resType->isDependentType(), |
| 8538 | ValueDependent)); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 8539 | } |
| 8540 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8541 | //===----------------------------------------------------------------------===// |
| 8542 | // Clang Extensions. |
| 8543 | //===----------------------------------------------------------------------===// |
| 8544 | |
| 8545 | /// ActOnBlockStart - This callback is invoked when a block literal is started. |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8546 | void Sema::ActOnBlockStart(SourceLocation CaretLoc, Scope *BlockScope) { |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8547 | BlockDecl *Block = BlockDecl::Create(Context, CurContext, CaretLoc); |
| 8548 | PushBlockScope(BlockScope, Block); |
| 8549 | CurContext->addDecl(Block); |
Fariborz Jahanian | a729da2 | 2010-07-09 18:44:02 +0000 | [diff] [blame] | 8550 | if (BlockScope) |
| 8551 | PushDeclContext(BlockScope, Block); |
| 8552 | else |
| 8553 | CurContext = Block; |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8554 | } |
| 8555 | |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 8556 | void Sema::ActOnBlockArguments(Declarator &ParamInfo, Scope *CurScope) { |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 8557 | assert(ParamInfo.getIdentifier()==0 && "block-id should have no identifier!"); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8558 | assert(ParamInfo.getContext() == Declarator::BlockLiteralContext); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8559 | BlockScopeInfo *CurBlock = getCurBlock(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8560 | |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 8561 | TypeSourceInfo *Sig = GetTypeForDeclarator(ParamInfo, CurScope); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 8562 | QualType T = Sig->getType(); |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 8563 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8564 | // GetTypeForDeclarator always produces a function type for a block |
| 8565 | // literal signature. Furthermore, it is always a FunctionProtoType |
| 8566 | // unless the function was written with a typedef. |
| 8567 | assert(T->isFunctionType() && |
| 8568 | "GetTypeForDeclarator made a non-function block signature"); |
| 8569 | |
| 8570 | // Look for an explicit signature in that function type. |
| 8571 | FunctionProtoTypeLoc ExplicitSignature; |
| 8572 | |
| 8573 | TypeLoc tmp = Sig->getTypeLoc().IgnoreParens(); |
| 8574 | if (isa<FunctionProtoTypeLoc>(tmp)) { |
| 8575 | ExplicitSignature = cast<FunctionProtoTypeLoc>(tmp); |
| 8576 | |
| 8577 | // Check whether that explicit signature was synthesized by |
| 8578 | // GetTypeForDeclarator. If so, don't save that as part of the |
| 8579 | // written signature. |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 8580 | if (ExplicitSignature.getLocalRangeBegin() == |
| 8581 | ExplicitSignature.getLocalRangeEnd()) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8582 | // This would be much cheaper if we stored TypeLocs instead of |
| 8583 | // TypeSourceInfos. |
| 8584 | TypeLoc Result = ExplicitSignature.getResultLoc(); |
| 8585 | unsigned Size = Result.getFullDataSize(); |
| 8586 | Sig = Context.CreateTypeSourceInfo(Result.getType(), Size); |
| 8587 | Sig->getTypeLoc().initializeFullCopy(Result, Size); |
| 8588 | |
| 8589 | ExplicitSignature = FunctionProtoTypeLoc(); |
| 8590 | } |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8591 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8592 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8593 | CurBlock->TheDecl->setSignatureAsWritten(Sig); |
| 8594 | CurBlock->FunctionType = T; |
| 8595 | |
| 8596 | const FunctionType *Fn = T->getAs<FunctionType>(); |
| 8597 | QualType RetTy = Fn->getResultType(); |
| 8598 | bool isVariadic = |
| 8599 | (isa<FunctionProtoType>(Fn) && cast<FunctionProtoType>(Fn)->isVariadic()); |
| 8600 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8601 | CurBlock->TheDecl->setIsVariadic(isVariadic); |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 8602 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8603 | // Don't allow returning a objc interface by value. |
| 8604 | if (RetTy->isObjCObjectType()) { |
| 8605 | Diag(ParamInfo.getSourceRange().getBegin(), |
| 8606 | diag::err_object_cannot_be_passed_returned_by_value) << 0 << RetTy; |
| 8607 | return; |
| 8608 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8609 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8610 | // Context.DependentTy is used as a placeholder for a missing block |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8611 | // return type. TODO: what should we do with declarators like: |
| 8612 | // ^ * { ... } |
| 8613 | // If the answer is "apply template argument deduction".... |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8614 | if (RetTy != Context.DependentTy) |
| 8615 | CurBlock->ReturnType = RetTy; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8616 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8617 | // Push block parameters from the declarator if we had them. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 8618 | SmallVector<ParmVarDecl*, 8> Params; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 8619 | if (ExplicitSignature) { |
| 8620 | for (unsigned I = 0, E = ExplicitSignature.getNumArgs(); I != E; ++I) { |
| 8621 | ParmVarDecl *Param = ExplicitSignature.getArg(I); |
Fariborz Jahanian | 9a66c30 | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 8622 | if (Param->getIdentifier() == 0 && |
| 8623 | !Param->isImplicit() && |
| 8624 | !Param->isInvalidDecl() && |
| 8625 | !getLangOptions().CPlusPlus) |
| 8626 | Diag(Param->getLocation(), diag::err_parameter_name_omitted); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8627 | Params.push_back(Param); |
Fariborz Jahanian | 9a66c30 | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 8628 | } |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8629 | |
| 8630 | // Fake up parameter variables if we have a typedef, like |
| 8631 | // ^ fntype { ... } |
| 8632 | } else if (const FunctionProtoType *Fn = T->getAs<FunctionProtoType>()) { |
| 8633 | for (FunctionProtoType::arg_type_iterator |
| 8634 | I = Fn->arg_type_begin(), E = Fn->arg_type_end(); I != E; ++I) { |
| 8635 | ParmVarDecl *Param = |
| 8636 | BuildParmVarDeclForTypedef(CurBlock->TheDecl, |
| 8637 | ParamInfo.getSourceRange().getBegin(), |
| 8638 | *I); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8639 | Params.push_back(Param); |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8640 | } |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8641 | } |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8642 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8643 | // Set the parameters on the block decl. |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 8644 | if (!Params.empty()) { |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8645 | CurBlock->TheDecl->setParams(Params.data(), Params.size()); |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 8646 | CheckParmsForFunctionDef(CurBlock->TheDecl->param_begin(), |
| 8647 | CurBlock->TheDecl->param_end(), |
| 8648 | /*CheckParameterNames=*/false); |
| 8649 | } |
| 8650 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8651 | // Finally we can process decl attributes. |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 8652 | ProcessDeclAttributes(CurScope, CurBlock->TheDecl, ParamInfo); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 8653 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8654 | if (!isVariadic && CurBlock->TheDecl->getAttr<SentinelAttr>()) { |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8655 | Diag(ParamInfo.getAttributes()->getLoc(), |
| 8656 | diag::warn_attribute_sentinel_not_variadic) << 1; |
| 8657 | // FIXME: remove the attribute. |
| 8658 | } |
| 8659 | |
| 8660 | // Put the parameter variables in scope. We can bail out immediately |
| 8661 | // if we don't have any. |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8662 | if (Params.empty()) |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 8663 | return; |
| 8664 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8665 | for (BlockDecl::param_iterator AI = CurBlock->TheDecl->param_begin(), |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 8666 | E = CurBlock->TheDecl->param_end(); AI != E; ++AI) { |
| 8667 | (*AI)->setOwningFunction(CurBlock->TheDecl); |
| 8668 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8669 | // If this has an identifier, add it to the scope stack. |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 8670 | if ((*AI)->getIdentifier()) { |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 8671 | CheckShadow(CurBlock->TheScope, *AI); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 8672 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8673 | PushOnScopeChains(*AI, CurBlock->TheScope); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 8674 | } |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 8675 | } |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8676 | } |
| 8677 | |
| 8678 | /// ActOnBlockError - If there is an error parsing a block, this callback |
| 8679 | /// is invoked to pop the information about the block from the action impl. |
| 8680 | void Sema::ActOnBlockError(SourceLocation CaretLoc, Scope *CurScope) { |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8681 | // Pop off CurBlock, handle nested blocks. |
Chris Lattner | 5c59e2b | 2009-04-21 22:38:46 +0000 | [diff] [blame] | 8682 | PopDeclContext(); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8683 | PopFunctionOrBlockScope(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8684 | } |
| 8685 | |
| 8686 | /// ActOnBlockStmtExpr - This is called when the body of a block statement |
| 8687 | /// literal was successfully completed. ^(int x){...} |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8688 | ExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, |
Chris Lattner | e476bdc | 2011-02-17 23:58:47 +0000 | [diff] [blame] | 8689 | Stmt *Body, Scope *CurScope) { |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 8690 | // If blocks are disabled, emit an error. |
| 8691 | if (!LangOpts.Blocks) |
| 8692 | Diag(CaretLoc, diag::err_blocks_disable); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8693 | |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8694 | BlockScopeInfo *BSI = cast<BlockScopeInfo>(FunctionScopes.back()); |
Fariborz Jahanian | a729da2 | 2010-07-09 18:44:02 +0000 | [diff] [blame] | 8695 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 8696 | PopDeclContext(); |
| 8697 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8698 | QualType RetTy = Context.VoidTy; |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 8699 | if (!BSI->ReturnType.isNull()) |
| 8700 | RetTy = BSI->ReturnType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8701 | |
Mike Stump | 5692586 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 8702 | bool NoReturn = BSI->TheDecl->getAttr<NoReturnAttr>(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8703 | QualType BlockTy; |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8704 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 8705 | // Set the captured variables on the block. |
John McCall | 6b5a61b | 2011-02-07 10:33:21 +0000 | [diff] [blame] | 8706 | BSI->TheDecl->setCaptures(Context, BSI->Captures.begin(), BSI->Captures.end(), |
| 8707 | BSI->CapturesCXXThis); |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 8708 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8709 | // If the user wrote a function type in some form, try to use that. |
| 8710 | if (!BSI->FunctionType.isNull()) { |
| 8711 | const FunctionType *FTy = BSI->FunctionType->getAs<FunctionType>(); |
| 8712 | |
| 8713 | FunctionType::ExtInfo Ext = FTy->getExtInfo(); |
| 8714 | if (NoReturn && !Ext.getNoReturn()) Ext = Ext.withNoReturn(true); |
| 8715 | |
| 8716 | // Turn protoless block types into nullary block types. |
| 8717 | if (isa<FunctionNoProtoType>(FTy)) { |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8718 | FunctionProtoType::ExtProtoInfo EPI; |
| 8719 | EPI.ExtInfo = Ext; |
| 8720 | BlockTy = Context.getFunctionType(RetTy, 0, 0, EPI); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8721 | |
| 8722 | // Otherwise, if we don't need to change anything about the function type, |
| 8723 | // preserve its sugar structure. |
| 8724 | } else if (FTy->getResultType() == RetTy && |
| 8725 | (!NoReturn || FTy->getNoReturnAttr())) { |
| 8726 | BlockTy = BSI->FunctionType; |
| 8727 | |
| 8728 | // Otherwise, make the minimal modifications to the function type. |
| 8729 | } else { |
| 8730 | const FunctionProtoType *FPT = cast<FunctionProtoType>(FTy); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8731 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 8732 | EPI.TypeQuals = 0; // FIXME: silently? |
| 8733 | EPI.ExtInfo = Ext; |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8734 | BlockTy = Context.getFunctionType(RetTy, |
| 8735 | FPT->arg_type_begin(), |
| 8736 | FPT->getNumArgs(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8737 | EPI); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8738 | } |
| 8739 | |
| 8740 | // If we don't have a function type, just build one from nothing. |
| 8741 | } else { |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8742 | FunctionProtoType::ExtProtoInfo EPI; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8743 | EPI.ExtInfo = FunctionType::ExtInfo().withNoReturn(NoReturn); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 8744 | BlockTy = Context.getFunctionType(RetTy, 0, 0, EPI); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8745 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8746 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 8747 | DiagnoseUnusedParameters(BSI->TheDecl->param_begin(), |
| 8748 | BSI->TheDecl->param_end()); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8749 | BlockTy = Context.getBlockPointerType(BlockTy); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8750 | |
Chris Lattner | 17a7830 | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 8751 | // If needed, diagnose invalid gotos and switches in the block. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8752 | if (getCurFunction()->NeedsScopeChecking() && |
| 8753 | !hasAnyUnrecoverableErrorsInThisFunction()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8754 | DiagnoseInvalidJumps(cast<CompoundStmt>(Body)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8755 | |
Chris Lattner | e476bdc | 2011-02-17 23:58:47 +0000 | [diff] [blame] | 8756 | BSI->TheDecl->setBody(cast<CompoundStmt>(Body)); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8757 | |
Fariborz Jahanian | 4e7c7f2 | 2011-07-11 18:04:54 +0000 | [diff] [blame] | 8758 | for (BlockDecl::capture_const_iterator ci = BSI->TheDecl->capture_begin(), |
| 8759 | ce = BSI->TheDecl->capture_end(); ci != ce; ++ci) { |
| 8760 | const VarDecl *variable = ci->getVariable(); |
| 8761 | QualType T = variable->getType(); |
| 8762 | QualType::DestructionKind destructKind = T.isDestructedType(); |
| 8763 | if (destructKind != QualType::DK_none) |
| 8764 | getCurFunction()->setHasBranchProtectedScope(); |
| 8765 | } |
| 8766 | |
Benjamin Kramer | d248619 | 2011-07-12 14:11:05 +0000 | [diff] [blame] | 8767 | BlockExpr *Result = new (Context) BlockExpr(BSI->TheDecl, BlockTy); |
| 8768 | const AnalysisBasedWarnings::Policy &WP = AnalysisWarnings.getDefaultPolicy(); |
| 8769 | PopFunctionOrBlockScope(&WP, Result->getBlockDecl(), Result); |
| 8770 | |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 8771 | return Owned(Result); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 8772 | } |
| 8773 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8774 | ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc, |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 8775 | Expr *expr, ParsedType type, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8776 | SourceLocation RPLoc) { |
Abramo Bagnara | 2cad900 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 8777 | TypeSourceInfo *TInfo; |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 8778 | GetTypeFromParser(type, &TInfo); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8779 | return BuildVAArgExpr(BuiltinLoc, expr, TInfo, RPLoc); |
Abramo Bagnara | 2cad900 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 8780 | } |
| 8781 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8782 | ExprResult Sema::BuildVAArgExpr(SourceLocation BuiltinLoc, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8783 | Expr *E, TypeSourceInfo *TInfo, |
| 8784 | SourceLocation RPLoc) { |
Chris Lattner | 0d20b8a | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 8785 | Expr *OrigExpr = E; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8786 | |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 8787 | // Get the va_list type |
| 8788 | QualType VaListType = Context.getBuiltinVaListType(); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 8789 | if (VaListType->isArrayType()) { |
| 8790 | // Deal with implicit array decay; for example, on x86-64, |
| 8791 | // va_list is an array, but it's supposed to decay to |
| 8792 | // a pointer for va_arg. |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 8793 | VaListType = Context.getArrayDecayedType(VaListType); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 8794 | // Make sure the input expression also decays appropriately. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8795 | ExprResult Result = UsualUnaryConversions(E); |
| 8796 | if (Result.isInvalid()) |
| 8797 | return ExprError(); |
| 8798 | E = Result.take(); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 8799 | } else { |
| 8800 | // Otherwise, the va_list argument must be an l-value because |
| 8801 | // it is modified by va_arg. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8802 | if (!E->isTypeDependent() && |
Douglas Gregor | dd02730 | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 8803 | CheckForModifiableLvalue(E, BuiltinLoc, *this)) |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 8804 | return ExprError(); |
| 8805 | } |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 8806 | |
Douglas Gregor | dd02730 | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 8807 | if (!E->isTypeDependent() && |
| 8808 | !Context.hasSameType(VaListType, E->getType())) { |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8809 | return ExprError(Diag(E->getLocStart(), |
| 8810 | diag::err_first_argument_to_va_arg_not_of_type_va_list) |
Chris Lattner | 0d20b8a | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 8811 | << OrigExpr->getType() << E->getSourceRange()); |
Chris Lattner | 9dc8f19 | 2009-04-05 00:59:53 +0000 | [diff] [blame] | 8812 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8813 | |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 8814 | if (!TInfo->getType()->isDependentType()) { |
| 8815 | if (RequireCompleteType(TInfo->getTypeLoc().getBeginLoc(), TInfo->getType(), |
| 8816 | PDiag(diag::err_second_parameter_to_va_arg_incomplete) |
| 8817 | << TInfo->getTypeLoc().getSourceRange())) |
| 8818 | return ExprError(); |
David Majnemer | db11b01 | 2011-06-13 06:37:03 +0000 | [diff] [blame] | 8819 | |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 8820 | if (RequireNonAbstractType(TInfo->getTypeLoc().getBeginLoc(), |
| 8821 | TInfo->getType(), |
| 8822 | PDiag(diag::err_second_parameter_to_va_arg_abstract) |
| 8823 | << TInfo->getTypeLoc().getSourceRange())) |
| 8824 | return ExprError(); |
| 8825 | |
Douglas Gregor | 4eb7522 | 2011-07-30 06:45:27 +0000 | [diff] [blame] | 8826 | if (!TInfo->getType().isPODType(Context)) { |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 8827 | Diag(TInfo->getTypeLoc().getBeginLoc(), |
Douglas Gregor | 4eb7522 | 2011-07-30 06:45:27 +0000 | [diff] [blame] | 8828 | TInfo->getType()->isObjCLifetimeType() |
| 8829 | ? diag::warn_second_parameter_to_va_arg_ownership_qualified |
| 8830 | : diag::warn_second_parameter_to_va_arg_not_pod) |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 8831 | << TInfo->getType() |
| 8832 | << TInfo->getTypeLoc().getSourceRange(); |
Douglas Gregor | 4eb7522 | 2011-07-30 06:45:27 +0000 | [diff] [blame] | 8833 | } |
Eli Friedman | 46d37c1 | 2011-07-11 21:45:59 +0000 | [diff] [blame] | 8834 | |
| 8835 | // Check for va_arg where arguments of the given type will be promoted |
| 8836 | // (i.e. this va_arg is guaranteed to have undefined behavior). |
| 8837 | QualType PromoteType; |
| 8838 | if (TInfo->getType()->isPromotableIntegerType()) { |
| 8839 | PromoteType = Context.getPromotedIntegerType(TInfo->getType()); |
| 8840 | if (Context.typesAreCompatible(PromoteType, TInfo->getType())) |
| 8841 | PromoteType = QualType(); |
| 8842 | } |
| 8843 | if (TInfo->getType()->isSpecificBuiltinType(BuiltinType::Float)) |
| 8844 | PromoteType = Context.DoubleTy; |
| 8845 | if (!PromoteType.isNull()) |
| 8846 | Diag(TInfo->getTypeLoc().getBeginLoc(), |
| 8847 | diag::warn_second_parameter_to_va_arg_never_compatible) |
| 8848 | << TInfo->getType() |
| 8849 | << PromoteType |
| 8850 | << TInfo->getTypeLoc().getSourceRange(); |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 8851 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8852 | |
Abramo Bagnara | 2cad900 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 8853 | QualType T = TInfo->getType().getNonLValueExprType(Context); |
| 8854 | return Owned(new (Context) VAArgExpr(BuiltinLoc, E, TInfo, RPLoc, T)); |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 8855 | } |
| 8856 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8857 | ExprResult Sema::ActOnGNUNullExpr(SourceLocation TokenLoc) { |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 8858 | // The type of __null will be int or long, depending on the size of |
| 8859 | // pointers on the target. |
| 8860 | QualType Ty; |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 8861 | unsigned pw = Context.getTargetInfo().getPointerWidth(0); |
| 8862 | if (pw == Context.getTargetInfo().getIntWidth()) |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 8863 | Ty = Context.IntTy; |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 8864 | else if (pw == Context.getTargetInfo().getLongWidth()) |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 8865 | Ty = Context.LongTy; |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 8866 | else if (pw == Context.getTargetInfo().getLongLongWidth()) |
NAKAMURA Takumi | 6e5658d | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 8867 | Ty = Context.LongLongTy; |
| 8868 | else { |
| 8869 | assert(!"I don't know size of pointer!"); |
| 8870 | Ty = Context.IntTy; |
| 8871 | } |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 8872 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8873 | return Owned(new (Context) GNUNullExpr(Ty, TokenLoc)); |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 8874 | } |
| 8875 | |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 8876 | static void MakeObjCStringLiteralFixItHint(Sema& SemaRef, QualType DstType, |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 8877 | Expr *SrcExpr, FixItHint &Hint) { |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 8878 | if (!SemaRef.getLangOptions().ObjC1) |
| 8879 | return; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8880 | |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 8881 | const ObjCObjectPointerType *PT = DstType->getAs<ObjCObjectPointerType>(); |
| 8882 | if (!PT) |
| 8883 | return; |
| 8884 | |
| 8885 | // Check if the destination is of type 'id'. |
| 8886 | if (!PT->isObjCIdType()) { |
| 8887 | // Check if the destination is the 'NSString' interface. |
| 8888 | const ObjCInterfaceDecl *ID = PT->getInterfaceDecl(); |
| 8889 | if (!ID || !ID->getIdentifier()->isStr("NSString")) |
| 8890 | return; |
| 8891 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8892 | |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 8893 | // Strip off any parens and casts. |
| 8894 | StringLiteral *SL = dyn_cast<StringLiteral>(SrcExpr->IgnoreParenCasts()); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 8895 | if (!SL || !SL->isAscii()) |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 8896 | return; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8897 | |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 8898 | Hint = FixItHint::CreateInsertion(SL->getLocStart(), "@"); |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 8899 | } |
| 8900 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8901 | bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, |
| 8902 | SourceLocation Loc, |
| 8903 | QualType DstType, QualType SrcType, |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 8904 | Expr *SrcExpr, AssignmentAction Action, |
| 8905 | bool *Complained) { |
| 8906 | if (Complained) |
| 8907 | *Complained = false; |
| 8908 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8909 | // Decode the result (notice that AST's are still created for extensions). |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 8910 | bool CheckInferredResultType = false; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8911 | bool isInvalid = false; |
| 8912 | unsigned DiagKind; |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 8913 | FixItHint Hint; |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 8914 | ConversionFixItGenerator ConvHints; |
| 8915 | bool MayHaveConvFixit = false; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8916 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8917 | switch (ConvTy) { |
| 8918 | default: assert(0 && "Unknown conversion type"); |
| 8919 | case Compatible: return false; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 8920 | case PointerToInt: |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8921 | DiagKind = diag::ext_typecheck_convert_pointer_int; |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 8922 | ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); |
| 8923 | MayHaveConvFixit = true; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8924 | break; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 8925 | case IntToPointer: |
| 8926 | DiagKind = diag::ext_typecheck_convert_int_pointer; |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 8927 | ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); |
| 8928 | MayHaveConvFixit = true; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 8929 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8930 | case IncompatiblePointer: |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 8931 | MakeObjCStringLiteralFixItHint(*this, DstType, SrcExpr, Hint); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8932 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer; |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 8933 | CheckInferredResultType = DstType->isObjCObjectPointerType() && |
| 8934 | SrcType->isObjCObjectPointerType(); |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 8935 | if (Hint.isNull() && !CheckInferredResultType) { |
| 8936 | ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); |
| 8937 | } |
| 8938 | MayHaveConvFixit = true; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8939 | break; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 8940 | case IncompatiblePointerSign: |
| 8941 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer_sign; |
| 8942 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8943 | case FunctionVoidPointer: |
| 8944 | DiagKind = diag::ext_typecheck_convert_pointer_void_func; |
| 8945 | break; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 8946 | case IncompatiblePointerDiscardsQualifiers: { |
John McCall | 40249e7 | 2011-02-01 23:28:01 +0000 | [diff] [blame] | 8947 | // Perform array-to-pointer decay if necessary. |
| 8948 | if (SrcType->isArrayType()) SrcType = Context.getArrayDecayedType(SrcType); |
| 8949 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 8950 | Qualifiers lhq = SrcType->getPointeeType().getQualifiers(); |
| 8951 | Qualifiers rhq = DstType->getPointeeType().getQualifiers(); |
| 8952 | if (lhq.getAddressSpace() != rhq.getAddressSpace()) { |
| 8953 | DiagKind = diag::err_typecheck_incompatible_address_space; |
| 8954 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8955 | |
| 8956 | |
| 8957 | } else if (lhq.getObjCLifetime() != rhq.getObjCLifetime()) { |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 8958 | DiagKind = diag::err_typecheck_incompatible_ownership; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8959 | break; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 8960 | } |
| 8961 | |
| 8962 | llvm_unreachable("unknown error case for discarding qualifiers!"); |
| 8963 | // fallthrough |
| 8964 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8965 | case CompatiblePointerDiscardsQualifiers: |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 8966 | // If the qualifiers lost were because we were applying the |
| 8967 | // (deprecated) C++ conversion from a string literal to a char* |
| 8968 | // (or wchar_t*), then there was no error (C++ 4.2p2). FIXME: |
| 8969 | // Ideally, this check would be performed in |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 8970 | // checkPointerTypesForAssignment. However, that would require a |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 8971 | // bit of refactoring (so that the second argument is an |
| 8972 | // expression, rather than a type), which should be done as part |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 8973 | // of a larger effort to fix checkPointerTypesForAssignment for |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 8974 | // C++ semantics. |
| 8975 | if (getLangOptions().CPlusPlus && |
| 8976 | IsStringLiteralToNonConstPointerConversion(SrcExpr, DstType)) |
| 8977 | return false; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 8978 | DiagKind = diag::ext_typecheck_convert_discards_qualifiers; |
| 8979 | break; |
Sean Hunt | c9132b6 | 2009-11-08 07:46:34 +0000 | [diff] [blame] | 8980 | case IncompatibleNestedPointerQualifiers: |
Fariborz Jahanian | 3451e92 | 2009-11-09 22:16:37 +0000 | [diff] [blame] | 8981 | DiagKind = diag::ext_nested_pointer_qualifier_mismatch; |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 8982 | break; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 8983 | case IntToBlockPointer: |
| 8984 | DiagKind = diag::err_int_to_block_pointer; |
| 8985 | break; |
| 8986 | case IncompatibleBlockPointer: |
Mike Stump | 25efa10 | 2009-04-21 22:51:42 +0000 | [diff] [blame] | 8987 | DiagKind = diag::err_typecheck_convert_incompatible_block_pointer; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 8988 | break; |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 8989 | case IncompatibleObjCQualifiedId: |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8990 | // FIXME: Diagnose the problem in ObjCQualifiedIdTypesAreCompatible, since |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 8991 | // it can give a more specific diagnostic. |
| 8992 | DiagKind = diag::warn_incompatible_qualified_id; |
| 8993 | break; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 8994 | case IncompatibleVectors: |
| 8995 | DiagKind = diag::warn_incompatible_vectors; |
| 8996 | break; |
Fariborz Jahanian | 04e5a25 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 8997 | case IncompatibleObjCWeakRef: |
| 8998 | DiagKind = diag::err_arc_weak_unavailable_assign; |
| 8999 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9000 | case Incompatible: |
| 9001 | DiagKind = diag::err_typecheck_convert_incompatible; |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 9002 | ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); |
| 9003 | MayHaveConvFixit = true; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9004 | isInvalid = true; |
| 9005 | break; |
| 9006 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9007 | |
Douglas Gregor | d4eea83 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 9008 | QualType FirstType, SecondType; |
| 9009 | switch (Action) { |
| 9010 | case AA_Assigning: |
| 9011 | case AA_Initializing: |
| 9012 | // The destination type comes first. |
| 9013 | FirstType = DstType; |
| 9014 | SecondType = SrcType; |
| 9015 | break; |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9016 | |
Douglas Gregor | d4eea83 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 9017 | case AA_Returning: |
| 9018 | case AA_Passing: |
| 9019 | case AA_Converting: |
| 9020 | case AA_Sending: |
| 9021 | case AA_Casting: |
| 9022 | // The source type comes first. |
| 9023 | FirstType = SrcType; |
| 9024 | SecondType = DstType; |
| 9025 | break; |
| 9026 | } |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9027 | |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 9028 | PartialDiagnostic FDiag = PDiag(DiagKind); |
| 9029 | FDiag << FirstType << SecondType << Action << SrcExpr->getSourceRange(); |
| 9030 | |
| 9031 | // If we can fix the conversion, suggest the FixIts. |
| 9032 | assert(ConvHints.isNull() || Hint.isNull()); |
| 9033 | if (!ConvHints.isNull()) { |
| 9034 | for (llvm::SmallVector<FixItHint, 1>::iterator |
| 9035 | HI = ConvHints.Hints.begin(), HE = ConvHints.Hints.end(); |
| 9036 | HI != HE; ++HI) |
| 9037 | FDiag << *HI; |
| 9038 | } else { |
| 9039 | FDiag << Hint; |
| 9040 | } |
| 9041 | if (MayHaveConvFixit) { FDiag << (unsigned) (ConvHints.Kind); } |
| 9042 | |
| 9043 | Diag(Loc, FDiag); |
| 9044 | |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 9045 | if (CheckInferredResultType) |
| 9046 | EmitRelatedResultTypeNote(SrcExpr); |
| 9047 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 9048 | if (Complained) |
| 9049 | *Complained = true; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9050 | return isInvalid; |
| 9051 | } |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9052 | |
Chris Lattner | 3bf6893 | 2009-04-25 21:59:05 +0000 | [diff] [blame] | 9053 | bool Sema::VerifyIntegerConstantExpression(const Expr *E, llvm::APSInt *Result){ |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 9054 | llvm::APSInt ICEResult; |
| 9055 | if (E->isIntegerConstantExpr(ICEResult, Context)) { |
| 9056 | if (Result) |
| 9057 | *Result = ICEResult; |
| 9058 | return false; |
| 9059 | } |
| 9060 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9061 | Expr::EvalResult EvalResult; |
| 9062 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9063 | if (!E->Evaluate(EvalResult, Context) || !EvalResult.Val.isInt() || |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9064 | EvalResult.HasSideEffects) { |
| 9065 | Diag(E->getExprLoc(), diag::err_expr_not_ice) << E->getSourceRange(); |
| 9066 | |
| 9067 | if (EvalResult.Diag) { |
| 9068 | // We only show the note if it's not the usual "invalid subexpression" |
| 9069 | // or if it's actually in a subexpression. |
| 9070 | if (EvalResult.Diag != diag::note_invalid_subexpr_in_ice || |
| 9071 | E->IgnoreParens() != EvalResult.DiagExpr->IgnoreParens()) |
| 9072 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
| 9073 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9074 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9075 | return true; |
| 9076 | } |
| 9077 | |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 9078 | Diag(E->getExprLoc(), diag::ext_expr_not_ice) << |
| 9079 | E->getSourceRange(); |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9080 | |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 9081 | if (EvalResult.Diag && |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 9082 | Diags.getDiagnosticLevel(diag::ext_expr_not_ice, EvalResult.DiagLoc) |
| 9083 | != Diagnostic::Ignored) |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 9084 | Diag(EvalResult.DiagLoc, EvalResult.Diag); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9085 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9086 | if (Result) |
| 9087 | *Result = EvalResult.Val.getInt(); |
| 9088 | return false; |
| 9089 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9090 | |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9091 | void |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9092 | Sema::PushExpressionEvaluationContext(ExpressionEvaluationContext NewContext) { |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9093 | ExprEvalContexts.push_back( |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9094 | ExpressionEvaluationContextRecord(NewContext, |
| 9095 | ExprTemporaries.size(), |
| 9096 | ExprNeedsCleanups)); |
| 9097 | ExprNeedsCleanups = false; |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9098 | } |
| 9099 | |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 9100 | void Sema::PopExpressionEvaluationContext() { |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9101 | // Pop the current expression evaluation context off the stack. |
| 9102 | ExpressionEvaluationContextRecord Rec = ExprEvalContexts.back(); |
| 9103 | ExprEvalContexts.pop_back(); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9104 | |
Douglas Gregor | 06d3369 | 2009-12-12 07:57:52 +0000 | [diff] [blame] | 9105 | if (Rec.Context == PotentiallyPotentiallyEvaluated) { |
| 9106 | if (Rec.PotentiallyReferenced) { |
| 9107 | // Mark any remaining declarations in the current position of the stack |
| 9108 | // as "referenced". If they were not meant to be referenced, semantic |
| 9109 | // analysis would have eliminated them (e.g., in ActOnCXXTypeId). |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9110 | for (PotentiallyReferencedDecls::iterator |
Douglas Gregor | 06d3369 | 2009-12-12 07:57:52 +0000 | [diff] [blame] | 9111 | I = Rec.PotentiallyReferenced->begin(), |
| 9112 | IEnd = Rec.PotentiallyReferenced->end(); |
| 9113 | I != IEnd; ++I) |
| 9114 | MarkDeclarationReferenced(I->first, I->second); |
| 9115 | } |
| 9116 | |
| 9117 | if (Rec.PotentiallyDiagnosed) { |
| 9118 | // Emit any pending diagnostics. |
| 9119 | for (PotentiallyEmittedDiagnostics::iterator |
| 9120 | I = Rec.PotentiallyDiagnosed->begin(), |
| 9121 | IEnd = Rec.PotentiallyDiagnosed->end(); |
| 9122 | I != IEnd; ++I) |
| 9123 | Diag(I->first, I->second); |
| 9124 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9125 | } |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9126 | |
| 9127 | // When are coming out of an unevaluated context, clear out any |
| 9128 | // temporaries that we may have created as part of the evaluation of |
| 9129 | // the expression in that context: they aren't relevant because they |
| 9130 | // will never be constructed. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9131 | if (Rec.Context == Unevaluated) { |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9132 | ExprTemporaries.erase(ExprTemporaries.begin() + Rec.NumTemporaries, |
| 9133 | ExprTemporaries.end()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9134 | ExprNeedsCleanups = Rec.ParentNeedsCleanups; |
| 9135 | |
| 9136 | // Otherwise, merge the contexts together. |
| 9137 | } else { |
| 9138 | ExprNeedsCleanups |= Rec.ParentNeedsCleanups; |
| 9139 | } |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9140 | |
| 9141 | // Destroy the popped expression evaluation record. |
| 9142 | Rec.Destroy(); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9143 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9144 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9145 | void Sema::DiscardCleanupsInEvaluationContext() { |
| 9146 | ExprTemporaries.erase( |
| 9147 | ExprTemporaries.begin() + ExprEvalContexts.back().NumTemporaries, |
| 9148 | ExprTemporaries.end()); |
| 9149 | ExprNeedsCleanups = false; |
| 9150 | } |
| 9151 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9152 | /// \brief Note that the given declaration was referenced in the source code. |
| 9153 | /// |
| 9154 | /// This routine should be invoke whenever a given declaration is referenced |
| 9155 | /// in the source code, and where that reference occurred. If this declaration |
| 9156 | /// reference means that the the declaration is used (C++ [basic.def.odr]p2, |
| 9157 | /// C99 6.9p3), then the declaration will be marked as used. |
| 9158 | /// |
| 9159 | /// \param Loc the location where the declaration was referenced. |
| 9160 | /// |
| 9161 | /// \param D the declaration that has been referenced by the source code. |
| 9162 | void Sema::MarkDeclarationReferenced(SourceLocation Loc, Decl *D) { |
| 9163 | assert(D && "No declaration?"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9164 | |
Argyrios Kyrtzidis | 6b6b42a | 2011-04-19 19:51:10 +0000 | [diff] [blame] | 9165 | D->setReferenced(); |
| 9166 | |
Douglas Gregor | c070cc6 | 2010-06-17 23:14:26 +0000 | [diff] [blame] | 9167 | if (D->isUsed(false)) |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 9168 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9169 | |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 9170 | // Mark a parameter or variable declaration "used", regardless of whether |
| 9171 | // we're in a template or not. The reason for this is that unevaluated |
| 9172 | // expressions (e.g. (void)sizeof()) constitute a use for warning purposes |
| 9173 | // (-Wunused-variables and -Wunused-parameters) |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9174 | if (isa<ParmVarDecl>(D) || |
Douglas Gregor | fc2ca56 | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 9175 | (isa<VarDecl>(D) && D->getDeclContext()->isFunctionOrMethod())) { |
Anders Carlsson | 2127ecc | 2010-10-22 23:37:08 +0000 | [diff] [blame] | 9176 | D->setUsed(); |
Douglas Gregor | fc2ca56 | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 9177 | return; |
| 9178 | } |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9179 | |
Douglas Gregor | fc2ca56 | 2010-04-07 20:29:57 +0000 | [diff] [blame] | 9180 | if (!isa<VarDecl>(D) && !isa<FunctionDecl>(D)) |
| 9181 | return; |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9182 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9183 | // Do not mark anything as "used" within a dependent context; wait for |
| 9184 | // an instantiation. |
| 9185 | if (CurContext->isDependentContext()) |
| 9186 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9187 | |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9188 | switch (ExprEvalContexts.back().Context) { |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9189 | case Unevaluated: |
| 9190 | // We are in an expression that is not potentially evaluated; do nothing. |
| 9191 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9192 | |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9193 | case PotentiallyEvaluated: |
| 9194 | // We are in a potentially-evaluated expression, so this declaration is |
| 9195 | // "used"; handle this below. |
| 9196 | break; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9197 | |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9198 | case PotentiallyPotentiallyEvaluated: |
| 9199 | // We are in an expression that may be potentially evaluated; queue this |
| 9200 | // declaration reference until we know whether the expression is |
| 9201 | // potentially evaluated. |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 9202 | ExprEvalContexts.back().addReferencedDecl(Loc, D); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9203 | return; |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9204 | |
| 9205 | case PotentiallyEvaluatedIfUsed: |
| 9206 | // Referenced declarations will only be used if the construct in the |
| 9207 | // containing expression is used. |
| 9208 | return; |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 9209 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9210 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9211 | // Note that this declaration has been used. |
Fariborz Jahanian | b7f4cc0 | 2009-06-22 17:30:33 +0000 | [diff] [blame] | 9212 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(D)) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9213 | if (Constructor->isDefaulted()) { |
| 9214 | if (Constructor->isDefaultConstructor()) { |
| 9215 | if (Constructor->isTrivial()) |
| 9216 | return; |
| 9217 | if (!Constructor->isUsed(false)) |
| 9218 | DefineImplicitDefaultConstructor(Loc, Constructor); |
| 9219 | } else if (Constructor->isCopyConstructor()) { |
| 9220 | if (!Constructor->isUsed(false)) |
| 9221 | DefineImplicitCopyConstructor(Loc, Constructor); |
| 9222 | } else if (Constructor->isMoveConstructor()) { |
| 9223 | if (!Constructor->isUsed(false)) |
| 9224 | DefineImplicitMoveConstructor(Loc, Constructor); |
| 9225 | } |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 9226 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9227 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9228 | MarkVTableUsed(Loc, Constructor->getParent()); |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 9229 | } else if (CXXDestructorDecl *Destructor = dyn_cast<CXXDestructorDecl>(D)) { |
Sean Hunt | cb45a0f | 2011-05-12 22:46:25 +0000 | [diff] [blame] | 9230 | if (Destructor->isDefaulted() && !Destructor->isUsed(false)) |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 9231 | DefineImplicitDestructor(Loc, Destructor); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9232 | if (Destructor->isVirtual()) |
| 9233 | MarkVTableUsed(Loc, Destructor->getParent()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 9234 | } else if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(D)) { |
Sean Hunt | 2b18808 | 2011-05-14 05:23:28 +0000 | [diff] [blame] | 9235 | if (MethodDecl->isDefaulted() && MethodDecl->isOverloadedOperator() && |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 9236 | MethodDecl->getOverloadedOperator() == OO_Equal) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 9237 | if (!MethodDecl->isUsed(false)) { |
| 9238 | if (MethodDecl->isCopyAssignmentOperator()) |
| 9239 | DefineImplicitCopyAssignment(Loc, MethodDecl); |
| 9240 | else |
| 9241 | DefineImplicitMoveAssignment(Loc, MethodDecl); |
| 9242 | } |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 9243 | } else if (MethodDecl->isVirtual()) |
| 9244 | MarkVTableUsed(Loc, MethodDecl->getParent()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 9245 | } |
Fariborz Jahanian | f5ed9e0 | 2009-06-24 22:09:44 +0000 | [diff] [blame] | 9246 | if (FunctionDecl *Function = dyn_cast<FunctionDecl>(D)) { |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9247 | // Recursive functions should be marked when used from another function. |
| 9248 | if (CurContext == Function) return; |
| 9249 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9250 | // Implicit instantiation of function templates and member functions of |
Douglas Gregor | 1637be7 | 2009-06-26 00:10:03 +0000 | [diff] [blame] | 9251 | // class templates. |
Douglas Gregor | 6cfacfe | 2010-05-17 17:34:56 +0000 | [diff] [blame] | 9252 | if (Function->isImplicitlyInstantiable()) { |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9253 | bool AlreadyInstantiated = false; |
| 9254 | if (FunctionTemplateSpecializationInfo *SpecInfo |
| 9255 | = Function->getTemplateSpecializationInfo()) { |
| 9256 | if (SpecInfo->getPointOfInstantiation().isInvalid()) |
| 9257 | SpecInfo->setPointOfInstantiation(Loc); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9258 | else if (SpecInfo->getTemplateSpecializationKind() |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 9259 | == TSK_ImplicitInstantiation) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9260 | AlreadyInstantiated = true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9261 | } else if (MemberSpecializationInfo *MSInfo |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9262 | = Function->getMemberSpecializationInfo()) { |
| 9263 | if (MSInfo->getPointOfInstantiation().isInvalid()) |
| 9264 | MSInfo->setPointOfInstantiation(Loc); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9265 | else if (MSInfo->getTemplateSpecializationKind() |
Douglas Gregor | 3b846b6 | 2009-10-27 20:53:28 +0000 | [diff] [blame] | 9266 | == TSK_ImplicitInstantiation) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9267 | AlreadyInstantiated = true; |
| 9268 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9269 | |
Douglas Gregor | 60406be | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 9270 | if (!AlreadyInstantiated) { |
| 9271 | if (isa<CXXRecordDecl>(Function->getDeclContext()) && |
| 9272 | cast<CXXRecordDecl>(Function->getDeclContext())->isLocalClass()) |
| 9273 | PendingLocalImplicitInstantiations.push_back(std::make_pair(Function, |
| 9274 | Loc)); |
| 9275 | else |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 9276 | PendingInstantiations.push_back(std::make_pair(Function, Loc)); |
Douglas Gregor | 60406be | 2010-01-16 22:29:39 +0000 | [diff] [blame] | 9277 | } |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9278 | } else { |
| 9279 | // Walk redefinitions, as some of them may be instantiable. |
Gabor Greif | 40181c4 | 2010-08-28 00:16:06 +0000 | [diff] [blame] | 9280 | for (FunctionDecl::redecl_iterator i(Function->redecls_begin()), |
| 9281 | e(Function->redecls_end()); i != e; ++i) { |
Gabor Greif | be9ebe3 | 2010-08-28 01:58:12 +0000 | [diff] [blame] | 9282 | if (!i->isUsed(false) && i->isImplicitlyInstantiable()) |
Gabor Greif | 40181c4 | 2010-08-28 00:16:06 +0000 | [diff] [blame] | 9283 | MarkDeclarationReferenced(Loc, *i); |
| 9284 | } |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9285 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9286 | |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9287 | // Keep track of used but undefined functions. |
| 9288 | if (!Function->isPure() && !Function->hasBody() && |
| 9289 | Function->getLinkage() != ExternalLinkage) { |
| 9290 | SourceLocation &old = UndefinedInternals[Function->getCanonicalDecl()]; |
| 9291 | if (old.isInvalid()) old = Loc; |
| 9292 | } |
Argyrios Kyrtzidis | 58b5259 | 2010-08-25 10:34:54 +0000 | [diff] [blame] | 9293 | |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9294 | Function->setUsed(true); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9295 | return; |
Douglas Gregor | d7f37bf | 2009-06-22 23:06:13 +0000 | [diff] [blame] | 9296 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9297 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9298 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) { |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 9299 | // Implicit instantiation of static data members of class templates. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9300 | if (Var->isStaticDataMember() && |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9301 | Var->getInstantiatedFromStaticDataMember()) { |
| 9302 | MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo(); |
| 9303 | assert(MSInfo && "Missing member specialization information?"); |
| 9304 | if (MSInfo->getPointOfInstantiation().isInvalid() && |
| 9305 | MSInfo->getTemplateSpecializationKind()== TSK_ImplicitInstantiation) { |
| 9306 | MSInfo->setPointOfInstantiation(Loc); |
Sebastian Redl | f79a719 | 2011-04-29 08:19:30 +0000 | [diff] [blame] | 9307 | // This is a modification of an existing AST node. Notify listeners. |
| 9308 | if (ASTMutationListener *L = getASTMutationListener()) |
| 9309 | L->StaticDataMemberInstantiated(Var); |
Chandler Carruth | 62c78d5 | 2010-08-25 08:44:16 +0000 | [diff] [blame] | 9310 | PendingInstantiations.push_back(std::make_pair(Var, Loc)); |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 9311 | } |
| 9312 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9313 | |
John McCall | 77efc68 | 2011-02-21 19:25:48 +0000 | [diff] [blame] | 9314 | // Keep track of used but undefined variables. We make a hole in |
| 9315 | // the warning for static const data members with in-line |
| 9316 | // initializers. |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9317 | if (Var->hasDefinition() == VarDecl::DeclarationOnly |
John McCall | 77efc68 | 2011-02-21 19:25:48 +0000 | [diff] [blame] | 9318 | && Var->getLinkage() != ExternalLinkage |
| 9319 | && !(Var->isStaticDataMember() && Var->hasInit())) { |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 9320 | SourceLocation &old = UndefinedInternals[Var->getCanonicalDecl()]; |
| 9321 | if (old.isInvalid()) old = Loc; |
| 9322 | } |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 9323 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9324 | D->setUsed(true); |
Douglas Gregor | 7caa682 | 2009-07-24 20:34:43 +0000 | [diff] [blame] | 9325 | return; |
Sam Weinig | cce6ebc | 2009-09-11 03:29:30 +0000 | [diff] [blame] | 9326 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 9327 | } |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 9328 | |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9329 | namespace { |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9330 | // Mark all of the declarations referenced |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9331 | // FIXME: Not fully implemented yet! We need to have a better understanding |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9332 | // of when we're entering |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9333 | class MarkReferencedDecls : public RecursiveASTVisitor<MarkReferencedDecls> { |
| 9334 | Sema &S; |
| 9335 | SourceLocation Loc; |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9336 | |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9337 | public: |
| 9338 | typedef RecursiveASTVisitor<MarkReferencedDecls> Inherited; |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9339 | |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9340 | MarkReferencedDecls(Sema &S, SourceLocation Loc) : S(S), Loc(Loc) { } |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9341 | |
| 9342 | bool TraverseTemplateArgument(const TemplateArgument &Arg); |
| 9343 | bool TraverseRecordType(RecordType *T); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9344 | }; |
| 9345 | } |
| 9346 | |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9347 | bool MarkReferencedDecls::TraverseTemplateArgument( |
| 9348 | const TemplateArgument &Arg) { |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9349 | if (Arg.getKind() == TemplateArgument::Declaration) { |
| 9350 | S.MarkDeclarationReferenced(Loc, Arg.getAsDecl()); |
| 9351 | } |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9352 | |
| 9353 | return Inherited::TraverseTemplateArgument(Arg); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9354 | } |
| 9355 | |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9356 | bool MarkReferencedDecls::TraverseRecordType(RecordType *T) { |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9357 | if (ClassTemplateSpecializationDecl *Spec |
| 9358 | = dyn_cast<ClassTemplateSpecializationDecl>(T->getDecl())) { |
| 9359 | const TemplateArgumentList &Args = Spec->getTemplateArgs(); |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 9360 | return TraverseTemplateArguments(Args.data(), Args.size()); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9361 | } |
| 9362 | |
Chandler Carruth | e3e210c | 2010-06-10 10:31:57 +0000 | [diff] [blame] | 9363 | return true; |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9364 | } |
| 9365 | |
| 9366 | void Sema::MarkDeclarationsReferencedInType(SourceLocation Loc, QualType T) { |
| 9367 | MarkReferencedDecls Marker(*this, Loc); |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 9368 | Marker.TraverseType(Context.getCanonicalType(T)); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 9369 | } |
| 9370 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9371 | namespace { |
| 9372 | /// \brief Helper class that marks all of the declarations referenced by |
| 9373 | /// potentially-evaluated subexpressions as "referenced". |
| 9374 | class EvaluatedExprMarker : public EvaluatedExprVisitor<EvaluatedExprMarker> { |
| 9375 | Sema &S; |
| 9376 | |
| 9377 | public: |
| 9378 | typedef EvaluatedExprVisitor<EvaluatedExprMarker> Inherited; |
| 9379 | |
| 9380 | explicit EvaluatedExprMarker(Sema &S) : Inherited(S.Context), S(S) { } |
| 9381 | |
| 9382 | void VisitDeclRefExpr(DeclRefExpr *E) { |
| 9383 | S.MarkDeclarationReferenced(E->getLocation(), E->getDecl()); |
| 9384 | } |
| 9385 | |
| 9386 | void VisitMemberExpr(MemberExpr *E) { |
| 9387 | S.MarkDeclarationReferenced(E->getMemberLoc(), E->getMemberDecl()); |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 9388 | Inherited::VisitMemberExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9389 | } |
| 9390 | |
| 9391 | void VisitCXXNewExpr(CXXNewExpr *E) { |
| 9392 | if (E->getConstructor()) |
| 9393 | S.MarkDeclarationReferenced(E->getLocStart(), E->getConstructor()); |
| 9394 | if (E->getOperatorNew()) |
| 9395 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorNew()); |
| 9396 | if (E->getOperatorDelete()) |
| 9397 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorDelete()); |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 9398 | Inherited::VisitCXXNewExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9399 | } |
| 9400 | |
| 9401 | void VisitCXXDeleteExpr(CXXDeleteExpr *E) { |
| 9402 | if (E->getOperatorDelete()) |
| 9403 | S.MarkDeclarationReferenced(E->getLocStart(), E->getOperatorDelete()); |
Douglas Gregor | 5833b0b | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 9404 | QualType Destroyed = S.Context.getBaseElementType(E->getDestroyedType()); |
| 9405 | if (const RecordType *DestroyedRec = Destroyed->getAs<RecordType>()) { |
| 9406 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DestroyedRec->getDecl()); |
| 9407 | S.MarkDeclarationReferenced(E->getLocStart(), |
| 9408 | S.LookupDestructor(Record)); |
| 9409 | } |
| 9410 | |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 9411 | Inherited::VisitCXXDeleteExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9412 | } |
| 9413 | |
| 9414 | void VisitCXXConstructExpr(CXXConstructExpr *E) { |
| 9415 | S.MarkDeclarationReferenced(E->getLocStart(), E->getConstructor()); |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 9416 | Inherited::VisitCXXConstructExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9417 | } |
| 9418 | |
| 9419 | void VisitBlockDeclRefExpr(BlockDeclRefExpr *E) { |
| 9420 | S.MarkDeclarationReferenced(E->getLocation(), E->getDecl()); |
| 9421 | } |
Douglas Gregor | 102ff97 | 2010-10-19 17:17:35 +0000 | [diff] [blame] | 9422 | |
| 9423 | void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { |
| 9424 | Visit(E->getExpr()); |
| 9425 | } |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9426 | }; |
| 9427 | } |
| 9428 | |
| 9429 | /// \brief Mark any declarations that appear within this expression or any |
| 9430 | /// potentially-evaluated subexpressions as "referenced". |
| 9431 | void Sema::MarkDeclarationsReferencedInExpr(Expr *E) { |
| 9432 | EvaluatedExprMarker(*this).Visit(E); |
| 9433 | } |
| 9434 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9435 | /// \brief Emit a diagnostic that describes an effect on the run-time behavior |
| 9436 | /// of the program being compiled. |
| 9437 | /// |
| 9438 | /// This routine emits the given diagnostic when the code currently being |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9439 | /// type-checked is "potentially evaluated", meaning that there is a |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9440 | /// possibility that the code will actually be executable. Code in sizeof() |
| 9441 | /// expressions, code used only during overload resolution, etc., are not |
| 9442 | /// potentially evaluated. This routine will suppress such diagnostics or, |
| 9443 | /// in the absolutely nutty case of potentially potentially evaluated |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9444 | /// expressions (C++ typeid), queue the diagnostic to potentially emit it |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9445 | /// later. |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9446 | /// |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9447 | /// This routine should be used for all diagnostics that describe the run-time |
| 9448 | /// behavior of a program, such as passing a non-POD value through an ellipsis. |
| 9449 | /// Failure to do so will likely result in spurious diagnostics or failures |
| 9450 | /// during overload resolution or within sizeof/alignof/typeof/typeid. |
Ted Kremenek | 762696f | 2011-02-23 01:51:43 +0000 | [diff] [blame] | 9451 | bool Sema::DiagRuntimeBehavior(SourceLocation Loc, const Stmt *stmt, |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9452 | const PartialDiagnostic &PD) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9453 | switch (ExprEvalContexts.back().Context) { |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9454 | case Unevaluated: |
| 9455 | // The argument will never be evaluated, so don't complain. |
| 9456 | break; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9457 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9458 | case PotentiallyEvaluated: |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 9459 | case PotentiallyEvaluatedIfUsed: |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 9460 | if (stmt && getCurFunctionOrMethodDecl()) { |
| 9461 | FunctionScopes.back()->PossiblyUnreachableDiags. |
| 9462 | push_back(sema::PossiblyUnreachableDiag(PD, Loc, stmt)); |
| 9463 | } |
| 9464 | else |
| 9465 | Diag(Loc, PD); |
| 9466 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9467 | return true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9468 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 9469 | case PotentiallyPotentiallyEvaluated: |
| 9470 | ExprEvalContexts.back().addDiagnostic(Loc, PD); |
| 9471 | break; |
| 9472 | } |
| 9473 | |
| 9474 | return false; |
| 9475 | } |
| 9476 | |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 9477 | bool Sema::CheckCallReturnType(QualType ReturnType, SourceLocation Loc, |
| 9478 | CallExpr *CE, FunctionDecl *FD) { |
| 9479 | if (ReturnType->isVoidType() || !ReturnType->isIncompleteType()) |
| 9480 | return false; |
| 9481 | |
| 9482 | PartialDiagnostic Note = |
| 9483 | FD ? PDiag(diag::note_function_with_incomplete_return_type_declared_here) |
| 9484 | << FD->getDeclName() : PDiag(); |
| 9485 | SourceLocation NoteLoc = FD ? FD->getLocation() : SourceLocation(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9486 | |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 9487 | if (RequireCompleteType(Loc, ReturnType, |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9488 | FD ? |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 9489 | PDiag(diag::err_call_function_incomplete_return) |
| 9490 | << CE->getSourceRange() << FD->getDeclName() : |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9491 | PDiag(diag::err_call_incomplete_return) |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 9492 | << CE->getSourceRange(), |
| 9493 | std::make_pair(NoteLoc, Note))) |
| 9494 | return true; |
| 9495 | |
| 9496 | return false; |
| 9497 | } |
| 9498 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9499 | // Diagnose the s/=/==/ and s/\|=/!=/ typos. Note that adding parentheses |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9500 | // will prevent this condition from triggering, which is what we want. |
| 9501 | void Sema::DiagnoseAssignmentAsCondition(Expr *E) { |
| 9502 | SourceLocation Loc; |
| 9503 | |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 9504 | unsigned diagnostic = diag::warn_condition_is_assignment; |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9505 | bool IsOrAssign = false; |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 9506 | |
Chandler Carruth | b33c19f | 2011-08-16 22:30:10 +0000 | [diff] [blame] | 9507 | if (BinaryOperator *Op = dyn_cast<BinaryOperator>(E)) { |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9508 | if (Op->getOpcode() != BO_Assign && Op->getOpcode() != BO_OrAssign) |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9509 | return; |
| 9510 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9511 | IsOrAssign = Op->getOpcode() == BO_OrAssign; |
| 9512 | |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 9513 | // Greylist some idioms by putting them into a warning subcategory. |
| 9514 | if (ObjCMessageExpr *ME |
| 9515 | = dyn_cast<ObjCMessageExpr>(Op->getRHS()->IgnoreParenCasts())) { |
| 9516 | Selector Sel = ME->getSelector(); |
| 9517 | |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 9518 | // self = [<foo> init...] |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 9519 | if (isSelfExpr(Op->getLHS()) && Sel.getNameForSlot(0).startswith("init")) |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 9520 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 9521 | |
| 9522 | // <foo> = [<bar> nextObject] |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 9523 | else if (Sel.isUnarySelector() && Sel.getNameForSlot(0) == "nextObject") |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 9524 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 9525 | } |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 9526 | |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9527 | Loc = Op->getOperatorLoc(); |
Chandler Carruth | b33c19f | 2011-08-16 22:30:10 +0000 | [diff] [blame] | 9528 | } else if (CXXOperatorCallExpr *Op = dyn_cast<CXXOperatorCallExpr>(E)) { |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9529 | if (Op->getOperator() != OO_Equal && Op->getOperator() != OO_PipeEqual) |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9530 | return; |
| 9531 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9532 | IsOrAssign = Op->getOperator() == OO_PipeEqual; |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9533 | Loc = Op->getOperatorLoc(); |
| 9534 | } else { |
| 9535 | // Not an assignment. |
| 9536 | return; |
| 9537 | } |
| 9538 | |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 9539 | Diag(Loc, diagnostic) << E->getSourceRange(); |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9540 | |
Argyrios Kyrtzidis | abdd3b3 | 2011-04-25 23:01:29 +0000 | [diff] [blame] | 9541 | SourceLocation Open = E->getSourceRange().getBegin(); |
| 9542 | SourceLocation Close = PP.getLocForEndOfToken(E->getSourceRange().getEnd()); |
| 9543 | Diag(Loc, diag::note_condition_assign_silence) |
| 9544 | << FixItHint::CreateInsertion(Open, "(") |
| 9545 | << FixItHint::CreateInsertion(Close, ")"); |
| 9546 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 9547 | if (IsOrAssign) |
| 9548 | Diag(Loc, diag::note_condition_or_assign_to_comparison) |
| 9549 | << FixItHint::CreateReplacement(Loc, "!="); |
| 9550 | else |
| 9551 | Diag(Loc, diag::note_condition_assign_to_comparison) |
| 9552 | << FixItHint::CreateReplacement(Loc, "=="); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9553 | } |
| 9554 | |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 9555 | /// \brief Redundant parentheses over an equality comparison can indicate |
| 9556 | /// that the user intended an assignment used as condition. |
| 9557 | void Sema::DiagnoseEqualityWithExtraParens(ParenExpr *parenE) { |
Argyrios Kyrtzidis | cf1620a | 2011-02-01 22:23:56 +0000 | [diff] [blame] | 9558 | // Don't warn if the parens came from a macro. |
| 9559 | SourceLocation parenLoc = parenE->getLocStart(); |
| 9560 | if (parenLoc.isInvalid() || parenLoc.isMacroID()) |
| 9561 | return; |
Argyrios Kyrtzidis | 170a6a2 | 2011-03-28 23:52:04 +0000 | [diff] [blame] | 9562 | // Don't warn for dependent expressions. |
| 9563 | if (parenE->isTypeDependent()) |
| 9564 | return; |
Argyrios Kyrtzidis | cf1620a | 2011-02-01 22:23:56 +0000 | [diff] [blame] | 9565 | |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 9566 | Expr *E = parenE->IgnoreParens(); |
| 9567 | |
| 9568 | if (BinaryOperator *opE = dyn_cast<BinaryOperator>(E)) |
Argyrios Kyrtzidis | 70f2330 | 2011-02-01 19:32:59 +0000 | [diff] [blame] | 9569 | if (opE->getOpcode() == BO_EQ && |
| 9570 | opE->getLHS()->IgnoreParenImpCasts()->isModifiableLvalue(Context) |
| 9571 | == Expr::MLV_Valid) { |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 9572 | SourceLocation Loc = opE->getOperatorLoc(); |
Ted Kremenek | 006ae38 | 2011-02-01 22:36:09 +0000 | [diff] [blame] | 9573 | |
Ted Kremenek | f7275cd | 2011-02-02 02:20:30 +0000 | [diff] [blame] | 9574 | Diag(Loc, diag::warn_equality_with_extra_parens) << E->getSourceRange(); |
Ted Kremenek | f7275cd | 2011-02-02 02:20:30 +0000 | [diff] [blame] | 9575 | Diag(Loc, diag::note_equality_comparison_silence) |
| 9576 | << FixItHint::CreateRemoval(parenE->getSourceRange().getBegin()) |
| 9577 | << FixItHint::CreateRemoval(parenE->getSourceRange().getEnd()); |
Argyrios Kyrtzidis | abdd3b3 | 2011-04-25 23:01:29 +0000 | [diff] [blame] | 9578 | Diag(Loc, diag::note_equality_comparison_to_assign) |
| 9579 | << FixItHint::CreateReplacement(Loc, "="); |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 9580 | } |
| 9581 | } |
| 9582 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9583 | ExprResult Sema::CheckBooleanCondition(Expr *E, SourceLocation Loc) { |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9584 | DiagnoseAssignmentAsCondition(E); |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 9585 | if (ParenExpr *parenE = dyn_cast<ParenExpr>(E)) |
| 9586 | DiagnoseEqualityWithExtraParens(parenE); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9587 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9588 | ExprResult result = CheckPlaceholderExpr(E); |
| 9589 | if (result.isInvalid()) return ExprError(); |
| 9590 | E = result.take(); |
Argyrios Kyrtzidis | 11ab790 | 2010-11-01 18:49:26 +0000 | [diff] [blame] | 9591 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 9592 | if (!E->isTypeDependent()) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 9593 | if (getLangOptions().CPlusPlus) |
| 9594 | return CheckCXXBooleanCondition(E); // C++ 6.4p4 |
| 9595 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9596 | ExprResult ERes = DefaultFunctionArrayLvalueConversion(E); |
| 9597 | if (ERes.isInvalid()) |
| 9598 | return ExprError(); |
| 9599 | E = ERes.take(); |
John McCall | abc56c7 | 2010-12-04 06:09:13 +0000 | [diff] [blame] | 9600 | |
| 9601 | QualType T = E->getType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9602 | if (!T->isScalarType()) { // C99 6.8.4.1p1 |
| 9603 | Diag(Loc, diag::err_typecheck_statement_requires_scalar) |
| 9604 | << T << E->getSourceRange(); |
| 9605 | return ExprError(); |
| 9606 | } |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9607 | } |
| 9608 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9609 | return Owned(E); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 9610 | } |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 9611 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9612 | ExprResult Sema::ActOnBooleanCondition(Scope *S, SourceLocation Loc, |
| 9613 | Expr *Sub) { |
Douglas Gregor | eecf38f | 2010-05-06 21:39:56 +0000 | [diff] [blame] | 9614 | if (!Sub) |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 9615 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9616 | |
| 9617 | return CheckBooleanCondition(Sub, Loc); |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 9618 | } |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 9619 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9620 | namespace { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9621 | /// A visitor for rebuilding a call to an __unknown_any expression |
| 9622 | /// to have an appropriate type. |
| 9623 | struct RebuildUnknownAnyFunction |
| 9624 | : StmtVisitor<RebuildUnknownAnyFunction, ExprResult> { |
| 9625 | |
| 9626 | Sema &S; |
| 9627 | |
| 9628 | RebuildUnknownAnyFunction(Sema &S) : S(S) {} |
| 9629 | |
| 9630 | ExprResult VisitStmt(Stmt *S) { |
| 9631 | llvm_unreachable("unexpected statement!"); |
| 9632 | return ExprError(); |
| 9633 | } |
| 9634 | |
| 9635 | ExprResult VisitExpr(Expr *expr) { |
| 9636 | S.Diag(expr->getExprLoc(), diag::err_unsupported_unknown_any_call) |
| 9637 | << expr->getSourceRange(); |
| 9638 | return ExprError(); |
| 9639 | } |
| 9640 | |
| 9641 | /// Rebuild an expression which simply semantically wraps another |
| 9642 | /// expression which it shares the type and value kind of. |
| 9643 | template <class T> ExprResult rebuildSugarExpr(T *expr) { |
| 9644 | ExprResult subResult = Visit(expr->getSubExpr()); |
| 9645 | if (subResult.isInvalid()) return ExprError(); |
| 9646 | |
| 9647 | Expr *subExpr = subResult.take(); |
| 9648 | expr->setSubExpr(subExpr); |
| 9649 | expr->setType(subExpr->getType()); |
| 9650 | expr->setValueKind(subExpr->getValueKind()); |
| 9651 | assert(expr->getObjectKind() == OK_Ordinary); |
| 9652 | return expr; |
| 9653 | } |
| 9654 | |
| 9655 | ExprResult VisitParenExpr(ParenExpr *paren) { |
| 9656 | return rebuildSugarExpr(paren); |
| 9657 | } |
| 9658 | |
| 9659 | ExprResult VisitUnaryExtension(UnaryOperator *op) { |
| 9660 | return rebuildSugarExpr(op); |
| 9661 | } |
| 9662 | |
| 9663 | ExprResult VisitUnaryAddrOf(UnaryOperator *op) { |
| 9664 | ExprResult subResult = Visit(op->getSubExpr()); |
| 9665 | if (subResult.isInvalid()) return ExprError(); |
| 9666 | |
| 9667 | Expr *subExpr = subResult.take(); |
| 9668 | op->setSubExpr(subExpr); |
| 9669 | op->setType(S.Context.getPointerType(subExpr->getType())); |
| 9670 | assert(op->getValueKind() == VK_RValue); |
| 9671 | assert(op->getObjectKind() == OK_Ordinary); |
| 9672 | return op; |
| 9673 | } |
| 9674 | |
| 9675 | ExprResult resolveDecl(Expr *expr, ValueDecl *decl) { |
| 9676 | if (!isa<FunctionDecl>(decl)) return VisitExpr(expr); |
| 9677 | |
| 9678 | expr->setType(decl->getType()); |
| 9679 | |
| 9680 | assert(expr->getValueKind() == VK_RValue); |
| 9681 | if (S.getLangOptions().CPlusPlus && |
| 9682 | !(isa<CXXMethodDecl>(decl) && |
| 9683 | cast<CXXMethodDecl>(decl)->isInstance())) |
| 9684 | expr->setValueKind(VK_LValue); |
| 9685 | |
| 9686 | return expr; |
| 9687 | } |
| 9688 | |
| 9689 | ExprResult VisitMemberExpr(MemberExpr *mem) { |
| 9690 | return resolveDecl(mem, mem->getMemberDecl()); |
| 9691 | } |
| 9692 | |
| 9693 | ExprResult VisitDeclRefExpr(DeclRefExpr *ref) { |
| 9694 | return resolveDecl(ref, ref->getDecl()); |
| 9695 | } |
| 9696 | }; |
| 9697 | } |
| 9698 | |
| 9699 | /// Given a function expression of unknown-any type, try to rebuild it |
| 9700 | /// to have a function type. |
| 9701 | static ExprResult rebuildUnknownAnyFunction(Sema &S, Expr *fn) { |
| 9702 | ExprResult result = RebuildUnknownAnyFunction(S).Visit(fn); |
| 9703 | if (result.isInvalid()) return ExprError(); |
| 9704 | return S.DefaultFunctionArrayConversion(result.take()); |
| 9705 | } |
| 9706 | |
| 9707 | namespace { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9708 | /// A visitor for rebuilding an expression of type __unknown_anytype |
| 9709 | /// into one which resolves the type directly on the referring |
| 9710 | /// expression. Strict preservation of the original source |
| 9711 | /// structure is not a goal. |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9712 | struct RebuildUnknownAnyExpr |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9713 | : StmtVisitor<RebuildUnknownAnyExpr, ExprResult> { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9714 | |
| 9715 | Sema &S; |
| 9716 | |
| 9717 | /// The current destination type. |
| 9718 | QualType DestType; |
| 9719 | |
| 9720 | RebuildUnknownAnyExpr(Sema &S, QualType castType) |
| 9721 | : S(S), DestType(castType) {} |
| 9722 | |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9723 | ExprResult VisitStmt(Stmt *S) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9724 | llvm_unreachable("unexpected statement!"); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9725 | return ExprError(); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9726 | } |
| 9727 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9728 | ExprResult VisitExpr(Expr *expr) { |
| 9729 | S.Diag(expr->getExprLoc(), diag::err_unsupported_unknown_any_expr) |
| 9730 | << expr->getSourceRange(); |
| 9731 | return ExprError(); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9732 | } |
| 9733 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9734 | ExprResult VisitCallExpr(CallExpr *call); |
| 9735 | ExprResult VisitObjCMessageExpr(ObjCMessageExpr *message); |
| 9736 | |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9737 | /// Rebuild an expression which simply semantically wraps another |
| 9738 | /// expression which it shares the type and value kind of. |
| 9739 | template <class T> ExprResult rebuildSugarExpr(T *expr) { |
| 9740 | ExprResult subResult = Visit(expr->getSubExpr()); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9741 | if (subResult.isInvalid()) return ExprError(); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9742 | Expr *subExpr = subResult.take(); |
| 9743 | expr->setSubExpr(subExpr); |
| 9744 | expr->setType(subExpr->getType()); |
| 9745 | expr->setValueKind(subExpr->getValueKind()); |
| 9746 | assert(expr->getObjectKind() == OK_Ordinary); |
| 9747 | return expr; |
| 9748 | } |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9749 | |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9750 | ExprResult VisitParenExpr(ParenExpr *paren) { |
| 9751 | return rebuildSugarExpr(paren); |
| 9752 | } |
| 9753 | |
| 9754 | ExprResult VisitUnaryExtension(UnaryOperator *op) { |
| 9755 | return rebuildSugarExpr(op); |
| 9756 | } |
| 9757 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9758 | ExprResult VisitUnaryAddrOf(UnaryOperator *op) { |
| 9759 | const PointerType *ptr = DestType->getAs<PointerType>(); |
| 9760 | if (!ptr) { |
| 9761 | S.Diag(op->getOperatorLoc(), diag::err_unknown_any_addrof) |
| 9762 | << op->getSourceRange(); |
| 9763 | return ExprError(); |
| 9764 | } |
| 9765 | assert(op->getValueKind() == VK_RValue); |
| 9766 | assert(op->getObjectKind() == OK_Ordinary); |
| 9767 | op->setType(DestType); |
| 9768 | |
| 9769 | // Build the sub-expression as if it were an object of the pointee type. |
| 9770 | DestType = ptr->getPointeeType(); |
| 9771 | ExprResult subResult = Visit(op->getSubExpr()); |
| 9772 | if (subResult.isInvalid()) return ExprError(); |
| 9773 | op->setSubExpr(subResult.take()); |
| 9774 | return op; |
| 9775 | } |
| 9776 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9777 | ExprResult VisitImplicitCastExpr(ImplicitCastExpr *ice); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9778 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9779 | ExprResult resolveDecl(Expr *expr, ValueDecl *decl); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9780 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9781 | ExprResult VisitMemberExpr(MemberExpr *mem) { |
| 9782 | return resolveDecl(mem, mem->getMemberDecl()); |
| 9783 | } |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9784 | |
| 9785 | ExprResult VisitDeclRefExpr(DeclRefExpr *ref) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9786 | return resolveDecl(ref, ref->getDecl()); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9787 | } |
| 9788 | }; |
| 9789 | } |
| 9790 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9791 | /// Rebuilds a call expression which yielded __unknown_anytype. |
| 9792 | ExprResult RebuildUnknownAnyExpr::VisitCallExpr(CallExpr *call) { |
| 9793 | Expr *callee = call->getCallee(); |
| 9794 | |
| 9795 | enum FnKind { |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 9796 | FK_MemberFunction, |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9797 | FK_FunctionPointer, |
| 9798 | FK_BlockPointer |
| 9799 | }; |
| 9800 | |
| 9801 | FnKind kind; |
| 9802 | QualType type = callee->getType(); |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 9803 | if (type == S.Context.BoundMemberTy) { |
| 9804 | assert(isa<CXXMemberCallExpr>(call) || isa<CXXOperatorCallExpr>(call)); |
| 9805 | kind = FK_MemberFunction; |
| 9806 | type = Expr::findBoundMemberType(callee); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9807 | } else if (const PointerType *ptr = type->getAs<PointerType>()) { |
| 9808 | type = ptr->getPointeeType(); |
| 9809 | kind = FK_FunctionPointer; |
| 9810 | } else { |
| 9811 | type = type->castAs<BlockPointerType>()->getPointeeType(); |
| 9812 | kind = FK_BlockPointer; |
| 9813 | } |
| 9814 | const FunctionType *fnType = type->castAs<FunctionType>(); |
| 9815 | |
| 9816 | // Verify that this is a legal result type of a function. |
| 9817 | if (DestType->isArrayType() || DestType->isFunctionType()) { |
| 9818 | unsigned diagID = diag::err_func_returning_array_function; |
| 9819 | if (kind == FK_BlockPointer) |
| 9820 | diagID = diag::err_block_returning_array_function; |
| 9821 | |
| 9822 | S.Diag(call->getExprLoc(), diagID) |
| 9823 | << DestType->isFunctionType() << DestType; |
| 9824 | return ExprError(); |
| 9825 | } |
| 9826 | |
| 9827 | // Otherwise, go ahead and set DestType as the call's result. |
| 9828 | call->setType(DestType.getNonLValueExprType(S.Context)); |
| 9829 | call->setValueKind(Expr::getValueKindForType(DestType)); |
| 9830 | assert(call->getObjectKind() == OK_Ordinary); |
| 9831 | |
| 9832 | // Rebuild the function type, replacing the result type with DestType. |
| 9833 | if (const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(fnType)) |
| 9834 | DestType = S.Context.getFunctionType(DestType, |
| 9835 | proto->arg_type_begin(), |
| 9836 | proto->getNumArgs(), |
| 9837 | proto->getExtProtoInfo()); |
| 9838 | else |
| 9839 | DestType = S.Context.getFunctionNoProtoType(DestType, |
| 9840 | fnType->getExtInfo()); |
| 9841 | |
| 9842 | // Rebuild the appropriate pointer-to-function type. |
| 9843 | switch (kind) { |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 9844 | case FK_MemberFunction: |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9845 | // Nothing to do. |
| 9846 | break; |
| 9847 | |
| 9848 | case FK_FunctionPointer: |
| 9849 | DestType = S.Context.getPointerType(DestType); |
| 9850 | break; |
| 9851 | |
| 9852 | case FK_BlockPointer: |
| 9853 | DestType = S.Context.getBlockPointerType(DestType); |
| 9854 | break; |
| 9855 | } |
| 9856 | |
| 9857 | // Finally, we can recurse. |
| 9858 | ExprResult calleeResult = Visit(callee); |
| 9859 | if (!calleeResult.isUsable()) return ExprError(); |
| 9860 | call->setCallee(calleeResult.take()); |
| 9861 | |
| 9862 | // Bind a temporary if necessary. |
| 9863 | return S.MaybeBindToTemporary(call); |
| 9864 | } |
| 9865 | |
| 9866 | ExprResult RebuildUnknownAnyExpr::VisitObjCMessageExpr(ObjCMessageExpr *msg) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9867 | // Verify that this is a legal result type of a call. |
| 9868 | if (DestType->isArrayType() || DestType->isFunctionType()) { |
| 9869 | S.Diag(msg->getExprLoc(), diag::err_func_returning_array_function) |
| 9870 | << DestType->isFunctionType() << DestType; |
| 9871 | return ExprError(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9872 | } |
| 9873 | |
John McCall | 48218c6 | 2011-07-13 17:56:40 +0000 | [diff] [blame] | 9874 | // Rewrite the method result type if available. |
| 9875 | if (ObjCMethodDecl *method = msg->getMethodDecl()) { |
| 9876 | assert(method->getResultType() == S.Context.UnknownAnyTy); |
| 9877 | method->setResultType(DestType); |
| 9878 | } |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9879 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9880 | // Change the type of the message. |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9881 | msg->setType(DestType.getNonReferenceType()); |
| 9882 | msg->setValueKind(Expr::getValueKindForType(DestType)); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9883 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9884 | return S.MaybeBindToTemporary(msg); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9885 | } |
| 9886 | |
| 9887 | ExprResult RebuildUnknownAnyExpr::VisitImplicitCastExpr(ImplicitCastExpr *ice) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9888 | // 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] | 9889 | assert(ice->getCastKind() == CK_FunctionToPointerDecay); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9890 | assert(ice->getValueKind() == VK_RValue); |
| 9891 | assert(ice->getObjectKind() == OK_Ordinary); |
| 9892 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9893 | ice->setType(DestType); |
| 9894 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9895 | // Rebuild the sub-expression as the pointee (function) type. |
| 9896 | DestType = DestType->castAs<PointerType>()->getPointeeType(); |
| 9897 | |
| 9898 | ExprResult result = Visit(ice->getSubExpr()); |
| 9899 | if (!result.isUsable()) return ExprError(); |
| 9900 | |
| 9901 | ice->setSubExpr(result.take()); |
| 9902 | return S.Owned(ice); |
| 9903 | } |
| 9904 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9905 | ExprResult RebuildUnknownAnyExpr::resolveDecl(Expr *expr, ValueDecl *decl) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9906 | ExprValueKind valueKind = VK_LValue; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9907 | QualType type = DestType; |
| 9908 | |
| 9909 | // We know how to make this work for certain kinds of decls: |
| 9910 | |
| 9911 | // - functions |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9912 | if (FunctionDecl *fn = dyn_cast<FunctionDecl>(decl)) { |
John McCall | a19950e | 2011-08-10 04:12:23 +0000 | [diff] [blame] | 9913 | if (const PointerType *ptr = type->getAs<PointerType>()) { |
| 9914 | DestType = ptr->getPointeeType(); |
| 9915 | ExprResult result = resolveDecl(expr, decl); |
| 9916 | if (result.isInvalid()) return ExprError(); |
| 9917 | return S.ImpCastExprToType(result.take(), type, |
| 9918 | CK_FunctionToPointerDecay, VK_RValue); |
| 9919 | } |
| 9920 | |
| 9921 | if (!type->isFunctionType()) { |
| 9922 | S.Diag(expr->getExprLoc(), diag::err_unknown_any_function) |
| 9923 | << decl << expr->getSourceRange(); |
| 9924 | return ExprError(); |
| 9925 | } |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9926 | |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 9927 | if (CXXMethodDecl *method = dyn_cast<CXXMethodDecl>(fn)) |
| 9928 | if (method->isInstance()) { |
| 9929 | valueKind = VK_RValue; |
| 9930 | type = S.Context.BoundMemberTy; |
| 9931 | } |
| 9932 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9933 | // Function references aren't l-values in C. |
| 9934 | if (!S.getLangOptions().CPlusPlus) |
| 9935 | valueKind = VK_RValue; |
| 9936 | |
| 9937 | // - variables |
| 9938 | } else if (isa<VarDecl>(decl)) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9939 | if (const ReferenceType *refTy = type->getAs<ReferenceType>()) { |
| 9940 | type = refTy->getPointeeType(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9941 | } else if (type->isFunctionType()) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9942 | S.Diag(expr->getExprLoc(), diag::err_unknown_any_var_function_type) |
| 9943 | << decl << expr->getSourceRange(); |
| 9944 | return ExprError(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9945 | } |
| 9946 | |
| 9947 | // - nothing else |
| 9948 | } else { |
| 9949 | S.Diag(expr->getExprLoc(), diag::err_unsupported_unknown_any_decl) |
| 9950 | << decl << expr->getSourceRange(); |
| 9951 | return ExprError(); |
| 9952 | } |
| 9953 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 9954 | decl->setType(DestType); |
| 9955 | expr->setType(type); |
| 9956 | expr->setValueKind(valueKind); |
| 9957 | return S.Owned(expr); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9958 | } |
| 9959 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9960 | /// Check a cast of an unknown-any type. We intentionally only |
| 9961 | /// trigger this for C-style casts. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9962 | ExprResult Sema::checkUnknownAnyCast(SourceRange typeRange, QualType castType, |
| 9963 | Expr *castExpr, CastKind &castKind, |
| 9964 | ExprValueKind &VK, CXXCastPath &path) { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9965 | // Rewrite the casted expression from scratch. |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9966 | ExprResult result = RebuildUnknownAnyExpr(*this, castType).Visit(castExpr); |
| 9967 | if (!result.isUsable()) return ExprError(); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9968 | |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 9969 | castExpr = result.take(); |
| 9970 | VK = castExpr->getValueKind(); |
| 9971 | castKind = CK_NoOp; |
| 9972 | |
| 9973 | return castExpr; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9974 | } |
| 9975 | |
| 9976 | static ExprResult diagnoseUnknownAnyExpr(Sema &S, Expr *e) { |
| 9977 | Expr *orig = e; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9978 | unsigned diagID = diag::err_uncasted_use_of_unknown_any; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9979 | while (true) { |
| 9980 | e = e->IgnoreParenImpCasts(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9981 | if (CallExpr *call = dyn_cast<CallExpr>(e)) { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9982 | e = call->getCallee(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9983 | diagID = diag::err_uncasted_call_of_unknown_any; |
| 9984 | } else { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9985 | break; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9986 | } |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 9987 | } |
| 9988 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 9989 | SourceLocation loc; |
| 9990 | NamedDecl *d; |
| 9991 | if (DeclRefExpr *ref = dyn_cast<DeclRefExpr>(e)) { |
| 9992 | loc = ref->getLocation(); |
| 9993 | d = ref->getDecl(); |
| 9994 | } else if (MemberExpr *mem = dyn_cast<MemberExpr>(e)) { |
| 9995 | loc = mem->getMemberLoc(); |
| 9996 | d = mem->getMemberDecl(); |
| 9997 | } else if (ObjCMessageExpr *msg = dyn_cast<ObjCMessageExpr>(e)) { |
| 9998 | diagID = diag::err_uncasted_call_of_unknown_any; |
| 9999 | loc = msg->getSelectorLoc(); |
| 10000 | d = msg->getMethodDecl(); |
John McCall | 819e745 | 2011-08-31 20:57:36 +0000 | [diff] [blame] | 10001 | if (!d) { |
| 10002 | S.Diag(loc, diag::err_uncasted_send_to_unknown_any_method) |
| 10003 | << static_cast<unsigned>(msg->isClassMessage()) << msg->getSelector() |
| 10004 | << orig->getSourceRange(); |
| 10005 | return ExprError(); |
| 10006 | } |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 10007 | } else { |
| 10008 | S.Diag(e->getExprLoc(), diag::err_unsupported_unknown_any_expr) |
| 10009 | << e->getSourceRange(); |
| 10010 | return ExprError(); |
| 10011 | } |
| 10012 | |
| 10013 | S.Diag(loc, diagID) << d << orig->getSourceRange(); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10014 | |
| 10015 | // Never recoverable. |
| 10016 | return ExprError(); |
| 10017 | } |
| 10018 | |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 10019 | /// Check for operands with placeholder types and complain if found. |
| 10020 | /// Returns true if there was an error and no recovery was possible. |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 10021 | ExprResult Sema::CheckPlaceholderExpr(Expr *E) { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10022 | // Placeholder types are always *exactly* the appropriate builtin type. |
| 10023 | QualType type = E->getType(); |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 10024 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10025 | // Overloaded expressions. |
| 10026 | if (type == Context.OverloadTy) |
| 10027 | return ResolveAndFixSingleFunctionTemplateSpecialization(E, false, true, |
Douglas Gregor | db2eae6 | 2011-03-16 19:16:25 +0000 | [diff] [blame] | 10028 | E->getSourceRange(), |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10029 | QualType(), |
| 10030 | diag::err_ovl_unresolvable); |
| 10031 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 10032 | // Bound member functions. |
| 10033 | if (type == Context.BoundMemberTy) { |
| 10034 | Diag(E->getLocStart(), diag::err_invalid_use_of_bound_member_func) |
| 10035 | << E->getSourceRange(); |
| 10036 | return ExprError(); |
| 10037 | } |
| 10038 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 10039 | // Expressions of unknown type. |
| 10040 | if (type == Context.UnknownAnyTy) |
| 10041 | return diagnoseUnknownAnyExpr(*this, E); |
| 10042 | |
| 10043 | assert(!type->isPlaceholderType()); |
| 10044 | return Owned(E); |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 10045 | } |
Richard Trieu | bb9b80c | 2011-04-21 21:44:26 +0000 | [diff] [blame] | 10046 | |
| 10047 | bool Sema::CheckCaseExpression(Expr *expr) { |
| 10048 | if (expr->isTypeDependent()) |
| 10049 | return true; |
| 10050 | if (expr->isValueDependent() || expr->isIntegerConstantExpr(Context)) |
| 10051 | return expr->getType()->isIntegralOrEnumerationType(); |
| 10052 | return false; |
| 10053 | } |