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" |
Eli Friedman | 93c878e | 2012-01-18 01:05:54 +0000 | [diff] [blame] | 15 | #include "clang/Sema/DelayedDiagnostic.h" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 16 | #include "clang/Sema/Initialization.h" |
| 17 | #include "clang/Sema/Lookup.h" |
Eli Friedman | 93c878e | 2012-01-18 01:05:54 +0000 | [diff] [blame] | 18 | #include "clang/Sema/ScopeInfo.h" |
Douglas Gregor | e737f50 | 2010-08-12 20:07:10 +0000 | [diff] [blame] | 19 | #include "clang/Sema/AnalysisBasedWarnings.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | #include "clang/AST/ASTContext.h" |
Argyrios Kyrtzidis | 6d96836 | 2012-02-10 20:10:44 +0000 | [diff] [blame] | 21 | #include "clang/AST/ASTConsumer.h" |
Sebastian Redl | f79a719 | 2011-04-29 08:19:30 +0000 | [diff] [blame] | 22 | #include "clang/AST/ASTMutationListener.h" |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 23 | #include "clang/AST/CXXInheritance.h" |
Daniel Dunbar | c4a1dea | 2008-08-11 05:35:13 +0000 | [diff] [blame] | 24 | #include "clang/AST/DeclObjC.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 25 | #include "clang/AST/DeclTemplate.h" |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 26 | #include "clang/AST/EvaluatedExprVisitor.h" |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 27 | #include "clang/AST/Expr.h" |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 28 | #include "clang/AST/ExprCXX.h" |
Steve Naroff | f494b57 | 2008-05-29 21:12:08 +0000 | [diff] [blame] | 29 | #include "clang/AST/ExprObjC.h" |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 30 | #include "clang/AST/RecursiveASTVisitor.h" |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 31 | #include "clang/AST/TypeLoc.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 32 | #include "clang/Basic/PartialDiagnostic.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 33 | #include "clang/Basic/SourceManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 34 | #include "clang/Basic/TargetInfo.h" |
Anders Carlsson | d497ba7 | 2009-08-26 22:59:12 +0000 | [diff] [blame] | 35 | #include "clang/Lex/LiteralSupport.h" |
| 36 | #include "clang/Lex/Preprocessor.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 37 | #include "clang/Sema/DeclSpec.h" |
| 38 | #include "clang/Sema/Designator.h" |
| 39 | #include "clang/Sema/Scope.h" |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 40 | #include "clang/Sema/ScopeInfo.h" |
John McCall | 1951085 | 2010-08-20 18:27:03 +0000 | [diff] [blame] | 41 | #include "clang/Sema/ParsedTemplate.h" |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 42 | #include "clang/Sema/SemaFixItUtils.h" |
John McCall | 7cd088e | 2010-08-24 07:21:54 +0000 | [diff] [blame] | 43 | #include "clang/Sema/Template.h" |
Eli Friedman | ef331b7 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 44 | #include "TreeTransform.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 45 | using namespace clang; |
John McCall | 781472f | 2010-08-25 08:40:02 +0000 | [diff] [blame] | 46 | using namespace sema; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 47 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 48 | /// \brief Determine whether the use of this declaration is valid, without |
| 49 | /// emitting diagnostics. |
| 50 | bool Sema::CanUseDecl(NamedDecl *D) { |
| 51 | // See if this is an auto-typed variable whose initializer we are parsing. |
| 52 | if (ParsingInitForAutoVars.count(D)) |
| 53 | return false; |
| 54 | |
| 55 | // See if this is a deleted function. |
| 56 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
| 57 | if (FD->isDeleted()) |
| 58 | return false; |
| 59 | } |
Sebastian Redl | 28bdb14 | 2011-10-16 18:19:16 +0000 | [diff] [blame] | 60 | |
| 61 | // See if this function is unavailable. |
| 62 | if (D->getAvailability() == AR_Unavailable && |
| 63 | cast<Decl>(CurContext)->getAvailability() != AR_Unavailable) |
| 64 | return false; |
| 65 | |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 66 | return true; |
| 67 | } |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 68 | |
Ted Kremenek | d6cf912 | 2012-02-10 02:45:47 +0000 | [diff] [blame] | 69 | static AvailabilityResult DiagnoseAvailabilityOfDecl(Sema &S, |
Fariborz Jahanian | 0f32caf | 2011-09-29 22:45:21 +0000 | [diff] [blame] | 70 | NamedDecl *D, SourceLocation Loc, |
| 71 | const ObjCInterfaceDecl *UnknownObjCClass) { |
| 72 | // See if this declaration is unavailable or deprecated. |
| 73 | std::string Message; |
| 74 | AvailabilityResult Result = D->getAvailability(&Message); |
Fariborz Jahanian | 39b4fc8 | 2011-11-28 19:45:58 +0000 | [diff] [blame] | 75 | if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(D)) |
| 76 | if (Result == AR_Available) { |
| 77 | const DeclContext *DC = ECD->getDeclContext(); |
| 78 | if (const EnumDecl *TheEnumDecl = dyn_cast<EnumDecl>(DC)) |
| 79 | Result = TheEnumDecl->getAvailability(&Message); |
| 80 | } |
| 81 | |
Fariborz Jahanian | 0f32caf | 2011-09-29 22:45:21 +0000 | [diff] [blame] | 82 | switch (Result) { |
| 83 | case AR_Available: |
| 84 | case AR_NotYetIntroduced: |
| 85 | break; |
| 86 | |
| 87 | case AR_Deprecated: |
Ted Kremenek | d6cf912 | 2012-02-10 02:45:47 +0000 | [diff] [blame] | 88 | S.EmitDeprecationWarning(D, Message, Loc, UnknownObjCClass); |
Fariborz Jahanian | 0f32caf | 2011-09-29 22:45:21 +0000 | [diff] [blame] | 89 | break; |
| 90 | |
| 91 | case AR_Unavailable: |
Ted Kremenek | d6cf912 | 2012-02-10 02:45:47 +0000 | [diff] [blame] | 92 | if (S.getCurContextAvailability() != AR_Unavailable) { |
Fariborz Jahanian | 0f32caf | 2011-09-29 22:45:21 +0000 | [diff] [blame] | 93 | if (Message.empty()) { |
| 94 | if (!UnknownObjCClass) |
Ted Kremenek | d6cf912 | 2012-02-10 02:45:47 +0000 | [diff] [blame] | 95 | S.Diag(Loc, diag::err_unavailable) << D->getDeclName(); |
Fariborz Jahanian | 0f32caf | 2011-09-29 22:45:21 +0000 | [diff] [blame] | 96 | else |
Ted Kremenek | d6cf912 | 2012-02-10 02:45:47 +0000 | [diff] [blame] | 97 | S.Diag(Loc, diag::warn_unavailable_fwdclass_message) |
Fariborz Jahanian | 0f32caf | 2011-09-29 22:45:21 +0000 | [diff] [blame] | 98 | << D->getDeclName(); |
| 99 | } |
| 100 | else |
Ted Kremenek | d6cf912 | 2012-02-10 02:45:47 +0000 | [diff] [blame] | 101 | S.Diag(Loc, diag::err_unavailable_message) |
Fariborz Jahanian | 0f32caf | 2011-09-29 22:45:21 +0000 | [diff] [blame] | 102 | << D->getDeclName() << Message; |
Ted Kremenek | d6cf912 | 2012-02-10 02:45:47 +0000 | [diff] [blame] | 103 | S.Diag(D->getLocation(), diag::note_unavailable_here) |
Fariborz Jahanian | 0f32caf | 2011-09-29 22:45:21 +0000 | [diff] [blame] | 104 | << isa<FunctionDecl>(D) << false; |
| 105 | } |
| 106 | break; |
| 107 | } |
| 108 | return Result; |
| 109 | } |
| 110 | |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 111 | /// \brief Emit a note explaining that this function is deleted or unavailable. |
| 112 | void Sema::NoteDeletedFunction(FunctionDecl *Decl) { |
| 113 | CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Decl); |
| 114 | |
Richard Smith | 5bdaac5 | 2012-04-02 20:59:25 +0000 | [diff] [blame] | 115 | if (Method && Method->isDeleted() && !Method->isDeletedAsWritten()) { |
| 116 | // If the method was explicitly defaulted, point at that declaration. |
| 117 | if (!Method->isImplicit()) |
| 118 | Diag(Decl->getLocation(), diag::note_implicitly_deleted); |
| 119 | |
| 120 | // Try to diagnose why this special member function was implicitly |
| 121 | // deleted. This might fail, if that reason no longer applies. |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 122 | CXXSpecialMember CSM = getSpecialMember(Method); |
Richard Smith | 5bdaac5 | 2012-04-02 20:59:25 +0000 | [diff] [blame] | 123 | if (CSM != CXXInvalid) |
| 124 | ShouldDeleteSpecialMember(Method, CSM, /*Diagnose=*/true); |
| 125 | |
| 126 | return; |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | Diag(Decl->getLocation(), diag::note_unavailable_here) |
| 130 | << 1 << Decl->isDeleted(); |
| 131 | } |
| 132 | |
Jordan Rose | 0eb3f45 | 2012-06-18 22:09:19 +0000 | [diff] [blame] | 133 | /// \brief Determine whether a FunctionDecl was ever declared with an |
| 134 | /// explicit storage class. |
| 135 | static bool hasAnyExplicitStorageClass(const FunctionDecl *D) { |
| 136 | for (FunctionDecl::redecl_iterator I = D->redecls_begin(), |
| 137 | E = D->redecls_end(); |
| 138 | I != E; ++I) { |
| 139 | if (I->getStorageClassAsWritten() != SC_None) |
| 140 | return true; |
| 141 | } |
| 142 | return false; |
| 143 | } |
| 144 | |
| 145 | /// \brief Check whether we're in an extern inline function and referring to a |
Jordan Rose | 33c0f37 | 2012-06-20 18:50:06 +0000 | [diff] [blame] | 146 | /// variable or function with internal linkage (C11 6.7.4p3). |
Jordan Rose | 0eb3f45 | 2012-06-18 22:09:19 +0000 | [diff] [blame] | 147 | /// |
Jordan Rose | 0eb3f45 | 2012-06-18 22:09:19 +0000 | [diff] [blame] | 148 | /// This is only a warning because we used to silently accept this code, but |
Jordan Rose | 33c0f37 | 2012-06-20 18:50:06 +0000 | [diff] [blame] | 149 | /// in many cases it will not behave correctly. This is not enabled in C++ mode |
| 150 | /// because the restriction language is a bit weaker (C++11 [basic.def.odr]p6) |
| 151 | /// and so while there may still be user mistakes, most of the time we can't |
| 152 | /// prove that there are errors. |
Jordan Rose | 0eb3f45 | 2012-06-18 22:09:19 +0000 | [diff] [blame] | 153 | static void diagnoseUseOfInternalDeclInInlineFunction(Sema &S, |
| 154 | const NamedDecl *D, |
| 155 | SourceLocation Loc) { |
Jordan Rose | 33c0f37 | 2012-06-20 18:50:06 +0000 | [diff] [blame] | 156 | // This is disabled under C++; there are too many ways for this to fire in |
| 157 | // contexts where the warning is a false positive, or where it is technically |
| 158 | // correct but benign. |
| 159 | if (S.getLangOpts().CPlusPlus) |
| 160 | return; |
Jordan Rose | 0eb3f45 | 2012-06-18 22:09:19 +0000 | [diff] [blame] | 161 | |
| 162 | // Check if this is an inlined function or method. |
| 163 | FunctionDecl *Current = S.getCurFunctionDecl(); |
| 164 | if (!Current) |
| 165 | return; |
| 166 | if (!Current->isInlined()) |
| 167 | return; |
| 168 | if (Current->getLinkage() != ExternalLinkage) |
| 169 | return; |
| 170 | |
| 171 | // Check if the decl has internal linkage. |
Jordan Rose | 33c0f37 | 2012-06-20 18:50:06 +0000 | [diff] [blame] | 172 | if (D->getLinkage() != InternalLinkage) |
Jordan Rose | 0eb3f45 | 2012-06-18 22:09:19 +0000 | [diff] [blame] | 173 | return; |
Jordan Rose | 0eb3f45 | 2012-06-18 22:09:19 +0000 | [diff] [blame] | 174 | |
Jordan Rose | 0523327 | 2012-06-21 05:54:50 +0000 | [diff] [blame] | 175 | // Downgrade from ExtWarn to Extension if |
| 176 | // (1) the supposedly external inline function is in the main file, |
| 177 | // and probably won't be included anywhere else. |
| 178 | // (2) the thing we're referencing is a pure function. |
| 179 | // (3) the thing we're referencing is another inline function. |
| 180 | // This last can give us false negatives, but it's better than warning on |
| 181 | // wrappers for simple C library functions. |
| 182 | const FunctionDecl *UsedFn = dyn_cast<FunctionDecl>(D); |
| 183 | bool DowngradeWarning = S.getSourceManager().isFromMainFile(Loc); |
| 184 | if (!DowngradeWarning && UsedFn) |
| 185 | DowngradeWarning = UsedFn->isInlined() || UsedFn->hasAttr<ConstAttr>(); |
| 186 | |
| 187 | S.Diag(Loc, DowngradeWarning ? diag::ext_internal_in_extern_inline |
| 188 | : diag::warn_internal_in_extern_inline) |
| 189 | << /*IsVar=*/!UsedFn << D; |
Jordan Rose | 0eb3f45 | 2012-06-18 22:09:19 +0000 | [diff] [blame] | 190 | |
| 191 | // Suggest "static" on the inline function, if possible. |
Jordan Rose | 33c0f37 | 2012-06-20 18:50:06 +0000 | [diff] [blame] | 192 | if (!hasAnyExplicitStorageClass(Current)) { |
Jordan Rose | 0eb3f45 | 2012-06-18 22:09:19 +0000 | [diff] [blame] | 193 | const FunctionDecl *FirstDecl = Current->getCanonicalDecl(); |
| 194 | SourceLocation DeclBegin = FirstDecl->getSourceRange().getBegin(); |
| 195 | S.Diag(DeclBegin, diag::note_convert_inline_to_static) |
| 196 | << Current << FixItHint::CreateInsertion(DeclBegin, "static "); |
| 197 | } |
| 198 | |
| 199 | S.Diag(D->getCanonicalDecl()->getLocation(), |
| 200 | diag::note_internal_decl_declared_here) |
| 201 | << D; |
| 202 | } |
| 203 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 204 | /// \brief Determine whether the use of this declaration is valid, and |
| 205 | /// emit any corresponding diagnostics. |
| 206 | /// |
| 207 | /// This routine diagnoses various problems with referencing |
| 208 | /// declarations that can occur when using a declaration. For example, |
| 209 | /// it might warn if a deprecated or unavailable declaration is being |
| 210 | /// used, or produce an error (and return true) if a C++0x deleted |
| 211 | /// function is being used. |
| 212 | /// |
| 213 | /// \returns true if there was an error (this declaration cannot be |
| 214 | /// referenced), false otherwise. |
Chris Lattner | 5233826 | 2009-10-25 22:31:57 +0000 | [diff] [blame] | 215 | /// |
Fariborz Jahanian | 8e5fc9b | 2010-12-21 00:44:01 +0000 | [diff] [blame] | 216 | bool Sema::DiagnoseUseOfDecl(NamedDecl *D, SourceLocation Loc, |
Fariborz Jahanian | 0f32caf | 2011-09-29 22:45:21 +0000 | [diff] [blame] | 217 | const ObjCInterfaceDecl *UnknownObjCClass) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 218 | if (getLangOpts().CPlusPlus && isa<FunctionDecl>(D)) { |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 219 | // If there were any diagnostics suppressed by template argument deduction, |
| 220 | // emit them now. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 221 | llvm::DenseMap<Decl *, SmallVector<PartialDiagnosticAt, 1> >::iterator |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 222 | Pos = SuppressedDiagnostics.find(D->getCanonicalDecl()); |
| 223 | if (Pos != SuppressedDiagnostics.end()) { |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 224 | SmallVectorImpl<PartialDiagnosticAt> &Suppressed = Pos->second; |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 225 | for (unsigned I = 0, N = Suppressed.size(); I != N; ++I) |
| 226 | Diag(Suppressed[I].first, Suppressed[I].second); |
| 227 | |
| 228 | // 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] | 229 | // them again for this specialization. However, we don't obsolete this |
Douglas Gregor | 9b62363 | 2010-10-12 23:32:35 +0000 | [diff] [blame] | 230 | // entry from the table, because we want to avoid ever emitting these |
| 231 | // diagnostics again. |
| 232 | Suppressed.clear(); |
| 233 | } |
| 234 | } |
| 235 | |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 236 | // 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] | 237 | if (ParsingInitForAutoVars.count(D)) { |
| 238 | Diag(Loc, diag::err_auto_variable_cannot_appear_in_own_initializer) |
| 239 | << D->getDeclName(); |
| 240 | return true; |
Richard Smith | 34b41d9 | 2011-02-20 03:19:35 +0000 | [diff] [blame] | 241 | } |
| 242 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 243 | // See if this is a deleted function. |
Douglas Gregor | 25d944a | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 244 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 245 | if (FD->isDeleted()) { |
| 246 | Diag(Loc, diag::err_deleted_function_use); |
Richard Smith | 6c4c36c | 2012-03-30 20:53:28 +0000 | [diff] [blame] | 247 | NoteDeletedFunction(FD); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 248 | return true; |
| 249 | } |
Douglas Gregor | 25d944a | 2009-02-24 04:26:15 +0000 | [diff] [blame] | 250 | } |
Ted Kremenek | d6cf912 | 2012-02-10 02:45:47 +0000 | [diff] [blame] | 251 | DiagnoseAvailabilityOfDecl(*this, D, Loc, UnknownObjCClass); |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 252 | |
Anders Carlsson | 2127ecc | 2010-10-22 23:37:08 +0000 | [diff] [blame] | 253 | // Warn if this is used but marked unused. |
Fariborz Jahanian | 0f32caf | 2011-09-29 22:45:21 +0000 | [diff] [blame] | 254 | if (D->hasAttr<UnusedAttr>()) |
Anders Carlsson | 2127ecc | 2010-10-22 23:37:08 +0000 | [diff] [blame] | 255 | Diag(Loc, diag::warn_used_but_marked_unused) << D->getDeclName(); |
Jordan Rose | 106af9e | 2012-06-15 18:19:48 +0000 | [diff] [blame] | 256 | |
Jordan Rose | 0eb3f45 | 2012-06-18 22:09:19 +0000 | [diff] [blame] | 257 | diagnoseUseOfInternalDeclInInlineFunction(*this, D, Loc); |
Jordan Rose | 106af9e | 2012-06-15 18:19:48 +0000 | [diff] [blame] | 258 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 259 | return false; |
Chris Lattner | 76a642f | 2009-02-15 22:43:40 +0000 | [diff] [blame] | 260 | } |
| 261 | |
Douglas Gregor | 0a0d2b1 | 2011-03-23 00:50:03 +0000 | [diff] [blame] | 262 | /// \brief Retrieve the message suffix that should be added to a |
| 263 | /// diagnostic complaining about the given function being deleted or |
| 264 | /// unavailable. |
| 265 | std::string Sema::getDeletedOrUnavailableSuffix(const FunctionDecl *FD) { |
| 266 | // FIXME: C++0x implicitly-deleted special member functions could be |
| 267 | // detected here so that we could improve diagnostics to say, e.g., |
| 268 | // "base class 'A' had a deleted copy constructor". |
| 269 | if (FD->isDeleted()) |
| 270 | return std::string(); |
| 271 | |
| 272 | std::string Message; |
| 273 | if (FD->getAvailability(&Message)) |
| 274 | return ": " + Message; |
| 275 | |
| 276 | return std::string(); |
| 277 | } |
| 278 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 279 | /// DiagnoseSentinelCalls - This routine checks whether a call or |
| 280 | /// message-send is to a declaration with the sentinel attribute, and |
| 281 | /// if so, it checks that the requirements of the sentinel are |
| 282 | /// satisfied. |
Fariborz Jahanian | 5b53005 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 283 | void Sema::DiagnoseSentinelCalls(NamedDecl *D, SourceLocation Loc, |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 284 | Expr **args, unsigned numArgs) { |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 285 | const SentinelAttr *attr = D->getAttr<SentinelAttr>(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 286 | if (!attr) |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 287 | return; |
Douglas Gregor | 92e986e | 2010-04-22 16:44:27 +0000 | [diff] [blame] | 288 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 289 | // The number of formal parameters of the declaration. |
| 290 | unsigned numFormalParams; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 291 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 292 | // The kind of declaration. This is also an index into a %select in |
| 293 | // the diagnostic. |
| 294 | enum CalleeType { CT_Function, CT_Method, CT_Block } calleeType; |
| 295 | |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 296 | if (ObjCMethodDecl *MD = dyn_cast<ObjCMethodDecl>(D)) { |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 297 | numFormalParams = MD->param_size(); |
| 298 | calleeType = CT_Method; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 299 | } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) { |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 300 | numFormalParams = FD->param_size(); |
| 301 | calleeType = CT_Function; |
| 302 | } else if (isa<VarDecl>(D)) { |
| 303 | QualType type = cast<ValueDecl>(D)->getType(); |
| 304 | const FunctionType *fn = 0; |
| 305 | if (const PointerType *ptr = type->getAs<PointerType>()) { |
| 306 | fn = ptr->getPointeeType()->getAs<FunctionType>(); |
| 307 | if (!fn) return; |
| 308 | calleeType = CT_Function; |
| 309 | } else if (const BlockPointerType *ptr = type->getAs<BlockPointerType>()) { |
| 310 | fn = ptr->getPointeeType()->castAs<FunctionType>(); |
| 311 | calleeType = CT_Block; |
| 312 | } else { |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 313 | return; |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 314 | } |
Fariborz Jahanian | 236673e | 2009-05-14 18:00:00 +0000 | [diff] [blame] | 315 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 316 | if (const FunctionProtoType *proto = dyn_cast<FunctionProtoType>(fn)) { |
| 317 | numFormalParams = proto->getNumArgs(); |
| 318 | } else { |
| 319 | numFormalParams = 0; |
| 320 | } |
| 321 | } else { |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 322 | return; |
| 323 | } |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 324 | |
| 325 | // "nullPos" is the number of formal parameters at the end which |
| 326 | // effectively count as part of the variadic arguments. This is |
| 327 | // useful if you would prefer to not have *any* formal parameters, |
| 328 | // but the language forces you to have at least one. |
| 329 | unsigned nullPos = attr->getNullPos(); |
| 330 | assert((nullPos == 0 || nullPos == 1) && "invalid null position on sentinel"); |
| 331 | numFormalParams = (nullPos > numFormalParams ? 0 : numFormalParams - nullPos); |
| 332 | |
| 333 | // The number of arguments which should follow the sentinel. |
| 334 | unsigned numArgsAfterSentinel = attr->getSentinel(); |
| 335 | |
| 336 | // If there aren't enough arguments for all the formal parameters, |
| 337 | // the sentinel, and the args after the sentinel, complain. |
| 338 | if (numArgs < numFormalParams + numArgsAfterSentinel + 1) { |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 339 | Diag(Loc, diag::warn_not_enough_argument) << D->getDeclName(); |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 340 | Diag(D->getLocation(), diag::note_sentinel_here) << calleeType; |
Fariborz Jahanian | 88f1ba0 | 2009-05-13 23:20:50 +0000 | [diff] [blame] | 341 | return; |
| 342 | } |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 343 | |
| 344 | // Otherwise, find the sentinel expression. |
| 345 | Expr *sentinelExpr = args[numArgs - numArgsAfterSentinel - 1]; |
John McCall | 8eb662e | 2010-05-06 23:53:00 +0000 | [diff] [blame] | 346 | if (!sentinelExpr) return; |
John McCall | 8eb662e | 2010-05-06 23:53:00 +0000 | [diff] [blame] | 347 | if (sentinelExpr->isValueDependent()) return; |
Argyrios Kyrtzidis | 8deabc1 | 2012-02-03 05:58:16 +0000 | [diff] [blame] | 348 | if (Context.isSentinelNullExpr(sentinelExpr)) return; |
John McCall | 8eb662e | 2010-05-06 23:53:00 +0000 | [diff] [blame] | 349 | |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 350 | // Pick a reasonable string to insert. Optimistically use 'nil' or |
| 351 | // 'NULL' if those are actually defined in the context. Only use |
| 352 | // 'nil' for ObjC methods, where it's much more likely that the |
| 353 | // variadic arguments form a list of object pointers. |
| 354 | SourceLocation MissingNilLoc |
Douglas Gregor | f78c4e5 | 2011-07-30 08:57:03 +0000 | [diff] [blame] | 355 | = PP.getLocForEndOfToken(sentinelExpr->getLocEnd()); |
| 356 | std::string NullValue; |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 357 | if (calleeType == CT_Method && |
| 358 | PP.getIdentifierInfo("nil")->hasMacroDefinition()) |
Douglas Gregor | f78c4e5 | 2011-07-30 08:57:03 +0000 | [diff] [blame] | 359 | NullValue = "nil"; |
| 360 | else if (PP.getIdentifierInfo("NULL")->hasMacroDefinition()) |
| 361 | NullValue = "NULL"; |
Douglas Gregor | f78c4e5 | 2011-07-30 08:57:03 +0000 | [diff] [blame] | 362 | else |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 363 | NullValue = "(void*) 0"; |
Eli Friedman | 39834ba | 2011-09-27 23:46:37 +0000 | [diff] [blame] | 364 | |
| 365 | if (MissingNilLoc.isInvalid()) |
| 366 | Diag(Loc, diag::warn_missing_sentinel) << calleeType; |
| 367 | else |
| 368 | Diag(MissingNilLoc, diag::warn_missing_sentinel) |
| 369 | << calleeType |
| 370 | << FixItHint::CreateInsertion(MissingNilLoc, ", " + NullValue); |
John McCall | 3323fad | 2011-09-09 07:56:05 +0000 | [diff] [blame] | 371 | Diag(D->getLocation(), diag::note_sentinel_here) << calleeType; |
Fariborz Jahanian | 5b53005 | 2009-05-13 18:09:35 +0000 | [diff] [blame] | 372 | } |
| 373 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 374 | SourceRange Sema::getExprRange(Expr *E) const { |
| 375 | return E ? E->getSourceRange() : SourceRange(); |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 376 | } |
| 377 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 378 | //===----------------------------------------------------------------------===// |
| 379 | // Standard Promotions and Conversions |
| 380 | //===----------------------------------------------------------------------===// |
| 381 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 382 | /// DefaultFunctionArrayConversion (C99 6.3.2.1p3, C99 6.3.2.1p4). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 383 | ExprResult Sema::DefaultFunctionArrayConversion(Expr *E) { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 384 | // Handle any placeholder expressions which made it here. |
| 385 | if (E->getType()->isPlaceholderType()) { |
| 386 | ExprResult result = CheckPlaceholderExpr(E); |
| 387 | if (result.isInvalid()) return ExprError(); |
| 388 | E = result.take(); |
| 389 | } |
| 390 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 391 | QualType Ty = E->getType(); |
| 392 | assert(!Ty.isNull() && "DefaultFunctionArrayConversion - missing type"); |
| 393 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 394 | if (Ty->isFunctionType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 395 | E = ImpCastExprToType(E, Context.getPointerType(Ty), |
| 396 | CK_FunctionToPointerDecay).take(); |
Chris Lattner | 67d33d8 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 397 | else if (Ty->isArrayType()) { |
| 398 | // In C90 mode, arrays only promote to pointers if the array expression is |
| 399 | // an lvalue. The relevant legalese is C90 6.2.2.1p3: "an lvalue that has |
| 400 | // type 'array of type' is converted to an expression that has type 'pointer |
| 401 | // to type'...". In C99 this was changed to: C99 6.3.2.1p3: "an expression |
| 402 | // that has type 'array of type' ...". The relevant change is "an lvalue" |
| 403 | // (C90) to "an expression" (C99). |
Argyrios Kyrtzidis | c39a3d7 | 2008-09-11 04:25:59 +0000 | [diff] [blame] | 404 | // |
| 405 | // C++ 4.2p1: |
| 406 | // An lvalue or rvalue of type "array of N T" or "array of unknown bound of |
| 407 | // T" can be converted to an rvalue of type "pointer to T". |
| 408 | // |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 409 | if (getLangOpts().C99 || getLangOpts().CPlusPlus || E->isLValue()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 410 | E = ImpCastExprToType(E, Context.getArrayDecayedType(Ty), |
| 411 | CK_ArrayToPointerDecay).take(); |
Chris Lattner | 67d33d8 | 2008-07-25 21:33:13 +0000 | [diff] [blame] | 412 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 413 | return Owned(E); |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Argyrios Kyrtzidis | 8a285ae | 2011-04-26 17:41:22 +0000 | [diff] [blame] | 416 | static void CheckForNullPointerDereference(Sema &S, Expr *E) { |
| 417 | // Check to see if we are dereferencing a null pointer. If so, |
| 418 | // and if not volatile-qualified, this is undefined behavior that the |
| 419 | // optimizer will delete, so warn about it. People sometimes try to use this |
| 420 | // to get a deterministic trap and are surprised by clang's behavior. This |
| 421 | // only handles the pattern "*null", which is a very syntactic check. |
| 422 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(E->IgnoreParenCasts())) |
| 423 | if (UO->getOpcode() == UO_Deref && |
| 424 | UO->getSubExpr()->IgnoreParenCasts()-> |
| 425 | isNullPointerConstant(S.Context, Expr::NPC_ValueDependentIsNotNull) && |
| 426 | !UO->getType().isVolatileQualified()) { |
| 427 | S.DiagRuntimeBehavior(UO->getOperatorLoc(), UO, |
| 428 | S.PDiag(diag::warn_indirection_through_null) |
| 429 | << UO->getSubExpr()->getSourceRange()); |
| 430 | S.DiagRuntimeBehavior(UO->getOperatorLoc(), UO, |
| 431 | S.PDiag(diag::note_indirection_through_null)); |
| 432 | } |
| 433 | } |
| 434 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 435 | ExprResult Sema::DefaultLvalueConversion(Expr *E) { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 436 | // Handle any placeholder expressions which made it here. |
| 437 | if (E->getType()->isPlaceholderType()) { |
| 438 | ExprResult result = CheckPlaceholderExpr(E); |
| 439 | if (result.isInvalid()) return ExprError(); |
| 440 | E = result.take(); |
| 441 | } |
| 442 | |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 443 | // C++ [conv.lval]p1: |
| 444 | // A glvalue of a non-function, non-array type T can be |
| 445 | // converted to a prvalue. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 446 | if (!E->isGLValue()) return Owned(E); |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 447 | |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 448 | QualType T = E->getType(); |
| 449 | assert(!T.isNull() && "r-value conversion on typeless expression?"); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 450 | |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 451 | // We don't want to throw lvalue-to-rvalue casts on top of |
| 452 | // expressions of certain types in C++. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 453 | if (getLangOpts().CPlusPlus && |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 454 | (E->getType() == Context.OverloadTy || |
| 455 | T->isDependentType() || |
| 456 | T->isRecordType())) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 457 | return Owned(E); |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 458 | |
| 459 | // The C standard is actually really unclear on this point, and |
| 460 | // DR106 tells us what the result should be but not why. It's |
| 461 | // generally best to say that void types just doesn't undergo |
| 462 | // lvalue-to-rvalue at all. Note that expressions of unqualified |
| 463 | // 'void' type are never l-values, but qualified void can be. |
| 464 | if (T->isVoidType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 465 | return Owned(E); |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 466 | |
Argyrios Kyrtzidis | 8a285ae | 2011-04-26 17:41:22 +0000 | [diff] [blame] | 467 | CheckForNullPointerDereference(*this, E); |
| 468 | |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 469 | // C++ [conv.lval]p1: |
| 470 | // [...] If T is a non-class type, the type of the prvalue is the |
| 471 | // cv-unqualified version of T. Otherwise, the type of the |
| 472 | // rvalue is T. |
| 473 | // |
| 474 | // C99 6.3.2.1p2: |
| 475 | // If the lvalue has qualified type, the value has the unqualified |
| 476 | // version of the type of the lvalue; otherwise, the value has the |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 477 | // type of the lvalue. |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 478 | if (T.hasQualifiers()) |
| 479 | T = T.getUnqualifiedType(); |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 480 | |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 481 | UpdateMarkingForLValueToRValue(E); |
| 482 | |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 483 | ExprResult Res = Owned(ImplicitCastExpr::Create(Context, T, CK_LValueToRValue, |
| 484 | E, 0, VK_RValue)); |
| 485 | |
Douglas Gregor | f7ecc30 | 2012-04-12 17:51:55 +0000 | [diff] [blame] | 486 | // C11 6.3.2.1p2: |
| 487 | // ... if the lvalue has atomic type, the value has the non-atomic version |
| 488 | // of the type of the lvalue ... |
| 489 | if (const AtomicType *Atomic = T->getAs<AtomicType>()) { |
| 490 | T = Atomic->getValueType().getUnqualifiedType(); |
| 491 | Res = Owned(ImplicitCastExpr::Create(Context, T, CK_AtomicToNonAtomic, |
| 492 | Res.get(), 0, VK_RValue)); |
| 493 | } |
| 494 | |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 495 | return Res; |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 496 | } |
| 497 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 498 | ExprResult Sema::DefaultFunctionArrayLvalueConversion(Expr *E) { |
| 499 | ExprResult Res = DefaultFunctionArrayConversion(E); |
| 500 | if (Res.isInvalid()) |
| 501 | return ExprError(); |
| 502 | Res = DefaultLvalueConversion(Res.take()); |
| 503 | if (Res.isInvalid()) |
| 504 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 505 | return Res; |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 509 | /// UsualUnaryConversions - Performs various conversions that are common to most |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 510 | /// operators (C99 6.3). The conversions of array and function types are |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 511 | /// sometimes suppressed. For example, the array->pointer conversion doesn't |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 512 | /// apply if the array is an argument to the sizeof or address (&) operators. |
| 513 | /// In these instances, this routine should *not* be called. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 514 | ExprResult Sema::UsualUnaryConversions(Expr *E) { |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 515 | // First, convert to an r-value. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 516 | ExprResult Res = DefaultFunctionArrayLvalueConversion(E); |
| 517 | if (Res.isInvalid()) |
| 518 | return Owned(E); |
| 519 | E = Res.take(); |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 520 | |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 521 | QualType Ty = E->getType(); |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 522 | assert(!Ty.isNull() && "UsualUnaryConversions - missing type"); |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 523 | |
| 524 | // Half FP is a bit different: it's a storage-only type, meaning that any |
| 525 | // "use" of it should be promoted to float. |
| 526 | if (Ty->isHalfType()) |
| 527 | return ImpCastExprToType(Res.take(), Context.FloatTy, CK_FloatingCast); |
| 528 | |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 529 | // Try to perform integral promotions if the object has a theoretically |
| 530 | // promotable type. |
| 531 | if (Ty->isIntegralOrUnscopedEnumerationType()) { |
| 532 | // C99 6.3.1.1p2: |
| 533 | // |
| 534 | // The following may be used in an expression wherever an int or |
| 535 | // unsigned int may be used: |
| 536 | // - an object or expression with an integer type whose integer |
| 537 | // conversion rank is less than or equal to the rank of int |
| 538 | // and unsigned int. |
| 539 | // - A bit-field of type _Bool, int, signed int, or unsigned int. |
| 540 | // |
| 541 | // If an int can represent all values of the original type, the |
| 542 | // value is converted to an int; otherwise, it is converted to an |
| 543 | // unsigned int. These are called the integer promotions. All |
| 544 | // other types are unchanged by the integer promotions. |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 545 | |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 546 | QualType PTy = Context.isPromotableBitField(E); |
| 547 | if (!PTy.isNull()) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 548 | E = ImpCastExprToType(E, PTy, CK_IntegralCast).take(); |
| 549 | return Owned(E); |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 550 | } |
| 551 | if (Ty->isPromotableIntegerType()) { |
| 552 | QualType PT = Context.getPromotedIntegerType(Ty); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 553 | E = ImpCastExprToType(E, PT, CK_IntegralCast).take(); |
| 554 | return Owned(E); |
John McCall | 0ae287a | 2010-12-01 04:43:34 +0000 | [diff] [blame] | 555 | } |
Eli Friedman | 04e8357 | 2009-08-20 04:21:42 +0000 | [diff] [blame] | 556 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 557 | return Owned(E); |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 560 | /// DefaultArgumentPromotion (C99 6.5.2.2p6). Used for function calls that |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 561 | /// 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] | 562 | /// double. All other argument types are converted by UsualUnaryConversions(). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 563 | ExprResult Sema::DefaultArgumentPromotion(Expr *E) { |
| 564 | QualType Ty = E->getType(); |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 565 | assert(!Ty.isNull() && "DefaultArgumentPromotion - missing type"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 566 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 567 | ExprResult Res = UsualUnaryConversions(E); |
| 568 | if (Res.isInvalid()) |
| 569 | return Owned(E); |
| 570 | E = Res.take(); |
John McCall | 40c2913 | 2010-12-06 18:36:11 +0000 | [diff] [blame] | 571 | |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 572 | // If this is a 'float' (CVR qualified or typedef) promote to double. |
Chris Lattner | 4037833 | 2010-05-16 04:01:30 +0000 | [diff] [blame] | 573 | if (Ty->isSpecificBuiltinType(BuiltinType::Float)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 574 | E = ImpCastExprToType(E, Context.DoubleTy, CK_FloatingCast).take(); |
| 575 | |
John McCall | 96a914a | 2011-08-27 22:06:17 +0000 | [diff] [blame] | 576 | // C++ performs lvalue-to-rvalue conversion as a default argument |
John McCall | 709bca8 | 2011-08-29 23:55:37 +0000 | [diff] [blame] | 577 | // promotion, even on class types, but note: |
| 578 | // C++11 [conv.lval]p2: |
| 579 | // When an lvalue-to-rvalue conversion occurs in an unevaluated |
| 580 | // operand or a subexpression thereof the value contained in the |
| 581 | // referenced object is not accessed. Otherwise, if the glvalue |
| 582 | // has a class type, the conversion copy-initializes a temporary |
| 583 | // of type T from the glvalue and the result of the conversion |
| 584 | // is a prvalue for the temporary. |
Eli Friedman | 55693fb | 2012-01-17 02:13:45 +0000 | [diff] [blame] | 585 | // FIXME: add some way to gate this entire thing for correctness in |
| 586 | // potentially potentially evaluated contexts. |
David Blaikie | 71f55f7 | 2012-08-06 22:47:24 +0000 | [diff] [blame] | 587 | if (getLangOpts().CPlusPlus && E->isGLValue() && !isUnevaluatedContext()) { |
Eli Friedman | 55693fb | 2012-01-17 02:13:45 +0000 | [diff] [blame] | 588 | ExprResult Temp = PerformCopyInitialization( |
| 589 | InitializedEntity::InitializeTemporary(E->getType()), |
| 590 | E->getExprLoc(), |
| 591 | Owned(E)); |
| 592 | if (Temp.isInvalid()) |
| 593 | return ExprError(); |
| 594 | E = Temp.get(); |
John McCall | 5f8d604 | 2011-08-27 01:09:30 +0000 | [diff] [blame] | 595 | } |
| 596 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 597 | return Owned(E); |
Chris Lattner | 05faf17 | 2008-07-25 22:25:12 +0000 | [diff] [blame] | 598 | } |
| 599 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 600 | /// Determine the degree of POD-ness for an expression. |
| 601 | /// Incomplete types are considered POD, since this check can be performed |
| 602 | /// when we're in an unevaluated context. |
| 603 | Sema::VarArgKind Sema::isValidVarArgType(const QualType &Ty) { |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 604 | if (Ty->isIncompleteType()) { |
| 605 | if (Ty->isObjCObjectType()) |
| 606 | return VAK_Invalid; |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 607 | return VAK_Valid; |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 608 | } |
| 609 | |
| 610 | if (Ty.isCXX98PODType(Context)) |
| 611 | return VAK_Valid; |
| 612 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 613 | // C++0x [expr.call]p7: |
| 614 | // Passing a potentially-evaluated argument of class type (Clause 9) |
| 615 | // having a non-trivial copy constructor, a non-trivial move constructor, |
| 616 | // or a non-trivial destructor, with no corresponding parameter, |
| 617 | // is conditionally-supported with implementation-defined semantics. |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 618 | if (getLangOpts().CPlusPlus0x && !Ty->isDependentType()) |
| 619 | if (CXXRecordDecl *Record = Ty->getAsCXXRecordDecl()) |
| 620 | if (Record->hasTrivialCopyConstructor() && |
| 621 | Record->hasTrivialMoveConstructor() && |
| 622 | Record->hasTrivialDestructor()) |
| 623 | return VAK_ValidInCXX11; |
| 624 | |
| 625 | if (getLangOpts().ObjCAutoRefCount && Ty->isObjCLifetimeType()) |
| 626 | return VAK_Valid; |
| 627 | return VAK_Invalid; |
| 628 | } |
| 629 | |
| 630 | bool Sema::variadicArgumentPODCheck(const Expr *E, VariadicCallType CT) { |
| 631 | // Don't allow one to pass an Objective-C interface to a vararg. |
| 632 | const QualType & Ty = E->getType(); |
| 633 | |
| 634 | // Complain about passing non-POD types through varargs. |
| 635 | switch (isValidVarArgType(Ty)) { |
| 636 | case VAK_Valid: |
| 637 | break; |
| 638 | case VAK_ValidInCXX11: |
| 639 | DiagRuntimeBehavior(E->getLocStart(), 0, |
| 640 | PDiag(diag::warn_cxx98_compat_pass_non_pod_arg_to_vararg) |
| 641 | << E->getType() << CT); |
| 642 | break; |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 643 | case VAK_Invalid: { |
| 644 | if (Ty->isObjCObjectType()) |
| 645 | return DiagRuntimeBehavior(E->getLocStart(), 0, |
| 646 | PDiag(diag::err_cannot_pass_objc_interface_to_vararg) |
| 647 | << Ty << CT); |
| 648 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 649 | return DiagRuntimeBehavior(E->getLocStart(), 0, |
| 650 | PDiag(diag::warn_cannot_pass_non_pod_arg_to_vararg) |
| 651 | << getLangOpts().CPlusPlus0x << Ty << CT); |
| 652 | } |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 653 | } |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 654 | // c++ rules are enforced elsewhere. |
| 655 | return false; |
| 656 | } |
| 657 | |
Chris Lattner | 312531a | 2009-04-12 08:11:20 +0000 | [diff] [blame] | 658 | /// DefaultVariadicArgumentPromotion - Like DefaultArgumentPromotion, but |
Jordan Rose | ddcfbc9 | 2012-07-19 18:10:23 +0000 | [diff] [blame] | 659 | /// will create a trap if the resulting type is not a POD type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 660 | ExprResult Sema::DefaultVariadicArgumentPromotion(Expr *E, VariadicCallType CT, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 661 | FunctionDecl *FDecl) { |
Richard Smith | e1971a1 | 2012-06-27 20:29:39 +0000 | [diff] [blame] | 662 | if (const BuiltinType *PlaceholderTy = E->getType()->getAsPlaceholderType()) { |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 663 | // Strip the unbridged-cast placeholder expression off, if applicable. |
| 664 | if (PlaceholderTy->getKind() == BuiltinType::ARCUnbridgedCast && |
| 665 | (CT == VariadicMethod || |
| 666 | (FDecl && FDecl->hasAttr<CFAuditedTransferAttr>()))) { |
| 667 | E = stripARCUnbridgedCast(E); |
| 668 | |
| 669 | // Otherwise, do normal placeholder checking. |
| 670 | } else { |
| 671 | ExprResult ExprRes = CheckPlaceholderExpr(E); |
| 672 | if (ExprRes.isInvalid()) |
| 673 | return ExprError(); |
| 674 | E = ExprRes.take(); |
| 675 | } |
| 676 | } |
Douglas Gregor | 8d5e18c | 2011-06-17 00:15:10 +0000 | [diff] [blame] | 677 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 678 | ExprResult ExprRes = DefaultArgumentPromotion(E); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 679 | if (ExprRes.isInvalid()) |
| 680 | return ExprError(); |
| 681 | E = ExprRes.take(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 682 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 683 | // Diagnostics regarding non-POD argument types are |
| 684 | // emitted along with format string checking in Sema::CheckFunctionCall(). |
Richard Smith | 83ea530 | 2012-06-27 20:23:58 +0000 | [diff] [blame] | 685 | if (isValidVarArgType(E->getType()) == VAK_Invalid) { |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 686 | // Turn this into a trap. |
| 687 | CXXScopeSpec SS; |
| 688 | SourceLocation TemplateKWLoc; |
| 689 | UnqualifiedId Name; |
| 690 | Name.setIdentifier(PP.getIdentifierInfo("__builtin_trap"), |
| 691 | E->getLocStart()); |
| 692 | ExprResult TrapFn = ActOnIdExpression(TUScope, SS, TemplateKWLoc, |
| 693 | Name, true, false); |
| 694 | if (TrapFn.isInvalid()) |
| 695 | return ExprError(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 696 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 697 | ExprResult Call = ActOnCallExpr(TUScope, TrapFn.get(), |
| 698 | E->getLocStart(), MultiExprArg(), |
| 699 | E->getLocEnd()); |
| 700 | if (Call.isInvalid()) |
| 701 | return ExprError(); |
Douglas Gregor | 930a9ab | 2011-05-21 19:26:31 +0000 | [diff] [blame] | 702 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 703 | ExprResult Comma = ActOnBinOp(TUScope, E->getLocStart(), tok::comma, |
| 704 | Call.get(), E); |
| 705 | if (Comma.isInvalid()) |
| 706 | return ExprError(); |
| 707 | return Comma.get(); |
Douglas Gregor | 0fd228d | 2011-05-21 16:27:21 +0000 | [diff] [blame] | 708 | } |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 709 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 710 | if (!getLangOpts().CPlusPlus && |
Fariborz Jahanian | e853bb3 | 2012-03-01 23:42:00 +0000 | [diff] [blame] | 711 | RequireCompleteType(E->getExprLoc(), E->getType(), |
Fariborz Jahanian | a0e005b | 2012-03-02 17:05:03 +0000 | [diff] [blame] | 712 | diag::err_call_incomplete_argument)) |
Fariborz Jahanian | e853bb3 | 2012-03-01 23:42:00 +0000 | [diff] [blame] | 713 | return ExprError(); |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 714 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 715 | return Owned(E); |
Anders Carlsson | dce5e2c | 2009-01-16 16:48:51 +0000 | [diff] [blame] | 716 | } |
| 717 | |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 718 | /// \brief Converts an integer to complex float type. Helper function of |
| 719 | /// UsualArithmeticConversions() |
| 720 | /// |
| 721 | /// \return false if the integer expression is an integer type and is |
| 722 | /// successfully converted to the complex type. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 723 | static bool handleIntegerToComplexFloatConversion(Sema &S, ExprResult &IntExpr, |
| 724 | ExprResult &ComplexExpr, |
| 725 | QualType IntTy, |
| 726 | QualType ComplexTy, |
| 727 | bool SkipCast) { |
| 728 | if (IntTy->isComplexType() || IntTy->isRealFloatingType()) return true; |
| 729 | if (SkipCast) return false; |
| 730 | if (IntTy->isIntegerType()) { |
| 731 | QualType fpTy = cast<ComplexType>(ComplexTy)->getElementType(); |
| 732 | IntExpr = S.ImpCastExprToType(IntExpr.take(), fpTy, CK_IntegralToFloating); |
| 733 | IntExpr = S.ImpCastExprToType(IntExpr.take(), ComplexTy, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 734 | CK_FloatingRealToComplex); |
| 735 | } else { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 736 | assert(IntTy->isComplexIntegerType()); |
| 737 | IntExpr = S.ImpCastExprToType(IntExpr.take(), ComplexTy, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 738 | CK_IntegralComplexToFloatingComplex); |
| 739 | } |
| 740 | return false; |
| 741 | } |
| 742 | |
| 743 | /// \brief Takes two complex float types and converts them to the same type. |
| 744 | /// Helper function of UsualArithmeticConversions() |
| 745 | static QualType |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 746 | handleComplexFloatToComplexFloatConverstion(Sema &S, ExprResult &LHS, |
| 747 | ExprResult &RHS, QualType LHSType, |
| 748 | QualType RHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 749 | bool IsCompAssign) { |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 750 | int order = S.Context.getFloatingTypeOrder(LHSType, RHSType); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 751 | |
| 752 | if (order < 0) { |
| 753 | // _Complex float -> _Complex double |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 754 | if (!IsCompAssign) |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 755 | LHS = S.ImpCastExprToType(LHS.take(), RHSType, CK_FloatingComplexCast); |
| 756 | return RHSType; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 757 | } |
| 758 | if (order > 0) |
| 759 | // _Complex float -> _Complex double |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 760 | RHS = S.ImpCastExprToType(RHS.take(), LHSType, CK_FloatingComplexCast); |
| 761 | return LHSType; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 762 | } |
| 763 | |
| 764 | /// \brief Converts otherExpr to complex float and promotes complexExpr if |
| 765 | /// necessary. Helper function of UsualArithmeticConversions() |
| 766 | static QualType handleOtherComplexFloatConversion(Sema &S, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 767 | ExprResult &ComplexExpr, |
| 768 | ExprResult &OtherExpr, |
| 769 | QualType ComplexTy, |
| 770 | QualType OtherTy, |
| 771 | bool ConvertComplexExpr, |
| 772 | bool ConvertOtherExpr) { |
| 773 | int order = S.Context.getFloatingTypeOrder(ComplexTy, OtherTy); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 774 | |
| 775 | // If just the complexExpr is complex, the otherExpr needs to be converted, |
| 776 | // and the complexExpr might need to be promoted. |
| 777 | if (order > 0) { // complexExpr is wider |
| 778 | // float -> _Complex double |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 779 | if (ConvertOtherExpr) { |
| 780 | QualType fp = cast<ComplexType>(ComplexTy)->getElementType(); |
| 781 | OtherExpr = S.ImpCastExprToType(OtherExpr.take(), fp, CK_FloatingCast); |
| 782 | OtherExpr = S.ImpCastExprToType(OtherExpr.take(), ComplexTy, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 783 | CK_FloatingRealToComplex); |
| 784 | } |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 785 | return ComplexTy; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 786 | } |
| 787 | |
| 788 | // otherTy is at least as wide. Find its corresponding complex type. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 789 | QualType result = (order == 0 ? ComplexTy : |
| 790 | S.Context.getComplexType(OtherTy)); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 791 | |
| 792 | // double -> _Complex double |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 793 | if (ConvertOtherExpr) |
| 794 | OtherExpr = S.ImpCastExprToType(OtherExpr.take(), result, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 795 | CK_FloatingRealToComplex); |
| 796 | |
| 797 | // _Complex float -> _Complex double |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 798 | if (ConvertComplexExpr && order < 0) |
| 799 | ComplexExpr = S.ImpCastExprToType(ComplexExpr.take(), result, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 800 | CK_FloatingComplexCast); |
| 801 | |
| 802 | return result; |
| 803 | } |
| 804 | |
| 805 | /// \brief Handle arithmetic conversion with complex types. Helper function of |
| 806 | /// UsualArithmeticConversions() |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 807 | static QualType handleComplexFloatConversion(Sema &S, ExprResult &LHS, |
| 808 | ExprResult &RHS, QualType LHSType, |
| 809 | QualType RHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 810 | bool IsCompAssign) { |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 811 | // if we have an integer operand, the result is the complex type. |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 812 | if (!handleIntegerToComplexFloatConversion(S, RHS, LHS, RHSType, LHSType, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 813 | /*skipCast*/false)) |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 814 | return LHSType; |
| 815 | if (!handleIntegerToComplexFloatConversion(S, LHS, RHS, LHSType, RHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 816 | /*skipCast*/IsCompAssign)) |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 817 | return RHSType; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 818 | |
| 819 | // This handles complex/complex, complex/float, or float/complex. |
| 820 | // When both operands are complex, the shorter operand is converted to the |
| 821 | // type of the longer, and that is the type of the result. This corresponds |
| 822 | // to what is done when combining two real floating-point operands. |
| 823 | // The fun begins when size promotion occur across type domains. |
| 824 | // From H&S 6.3.4: When one operand is complex and the other is a real |
| 825 | // floating-point type, the less precise type is converted, within it's |
| 826 | // real or complex domain, to the precision of the other type. For example, |
| 827 | // when combining a "long double" with a "double _Complex", the |
| 828 | // "double _Complex" is promoted to "long double _Complex". |
| 829 | |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 830 | bool LHSComplexFloat = LHSType->isComplexType(); |
| 831 | bool RHSComplexFloat = RHSType->isComplexType(); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 832 | |
| 833 | // If both are complex, just cast to the more precise type. |
| 834 | if (LHSComplexFloat && RHSComplexFloat) |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 835 | return handleComplexFloatToComplexFloatConverstion(S, LHS, RHS, |
| 836 | LHSType, RHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 837 | IsCompAssign); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 838 | |
| 839 | // If only one operand is complex, promote it if necessary and convert the |
| 840 | // other operand to complex. |
| 841 | if (LHSComplexFloat) |
| 842 | return handleOtherComplexFloatConversion( |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 843 | S, LHS, RHS, LHSType, RHSType, /*convertComplexExpr*/!IsCompAssign, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 844 | /*convertOtherExpr*/ true); |
| 845 | |
| 846 | assert(RHSComplexFloat); |
| 847 | return handleOtherComplexFloatConversion( |
Richard Trieu | cafd30b | 2011-09-06 18:25:09 +0000 | [diff] [blame] | 848 | S, RHS, LHS, RHSType, LHSType, /*convertComplexExpr*/true, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 849 | /*convertOtherExpr*/ !IsCompAssign); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 850 | } |
| 851 | |
| 852 | /// \brief Hande arithmetic conversion from integer to float. Helper function |
| 853 | /// of UsualArithmeticConversions() |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 854 | static QualType handleIntToFloatConversion(Sema &S, ExprResult &FloatExpr, |
| 855 | ExprResult &IntExpr, |
| 856 | QualType FloatTy, QualType IntTy, |
| 857 | bool ConvertFloat, bool ConvertInt) { |
| 858 | if (IntTy->isIntegerType()) { |
| 859 | if (ConvertInt) |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 860 | // Convert intExpr to the lhs floating point type. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 861 | IntExpr = S.ImpCastExprToType(IntExpr.take(), FloatTy, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 862 | CK_IntegralToFloating); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 863 | return FloatTy; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 864 | } |
| 865 | |
| 866 | // Convert both sides to the appropriate complex float. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 867 | assert(IntTy->isComplexIntegerType()); |
| 868 | QualType result = S.Context.getComplexType(FloatTy); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 869 | |
| 870 | // _Complex int -> _Complex float |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 871 | if (ConvertInt) |
| 872 | IntExpr = S.ImpCastExprToType(IntExpr.take(), result, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 873 | CK_IntegralComplexToFloatingComplex); |
| 874 | |
| 875 | // float -> _Complex float |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 876 | if (ConvertFloat) |
| 877 | FloatExpr = S.ImpCastExprToType(FloatExpr.take(), result, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 878 | CK_FloatingRealToComplex); |
| 879 | |
| 880 | return result; |
| 881 | } |
| 882 | |
| 883 | /// \brief Handle arithmethic conversion with floating point types. Helper |
| 884 | /// function of UsualArithmeticConversions() |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 885 | static QualType handleFloatConversion(Sema &S, ExprResult &LHS, |
| 886 | ExprResult &RHS, QualType LHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 887 | QualType RHSType, bool IsCompAssign) { |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 888 | bool LHSFloat = LHSType->isRealFloatingType(); |
| 889 | bool RHSFloat = RHSType->isRealFloatingType(); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 890 | |
| 891 | // If we have two real floating types, convert the smaller operand |
| 892 | // to the bigger result. |
| 893 | if (LHSFloat && RHSFloat) { |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 894 | int order = S.Context.getFloatingTypeOrder(LHSType, RHSType); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 895 | if (order > 0) { |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 896 | RHS = S.ImpCastExprToType(RHS.take(), LHSType, CK_FloatingCast); |
| 897 | return LHSType; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 898 | } |
| 899 | |
| 900 | assert(order < 0 && "illegal float comparison"); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 901 | if (!IsCompAssign) |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 902 | LHS = S.ImpCastExprToType(LHS.take(), RHSType, CK_FloatingCast); |
| 903 | return RHSType; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 904 | } |
| 905 | |
| 906 | if (LHSFloat) |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 907 | return handleIntToFloatConversion(S, LHS, RHS, LHSType, RHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 908 | /*convertFloat=*/!IsCompAssign, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 909 | /*convertInt=*/ true); |
| 910 | assert(RHSFloat); |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 911 | return handleIntToFloatConversion(S, RHS, LHS, RHSType, LHSType, |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 912 | /*convertInt=*/ true, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 913 | /*convertFloat=*/!IsCompAssign); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 914 | } |
| 915 | |
| 916 | /// \brief Handle conversions with GCC complex int extension. Helper function |
Benjamin Kramer | 5cc8680 | 2011-09-06 19:57:14 +0000 | [diff] [blame] | 917 | /// of UsualArithmeticConversions() |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 918 | // FIXME: if the operands are (int, _Complex long), we currently |
| 919 | // don't promote the complex. Also, signedness? |
Benjamin Kramer | 5cc8680 | 2011-09-06 19:57:14 +0000 | [diff] [blame] | 920 | static QualType handleComplexIntConversion(Sema &S, ExprResult &LHS, |
| 921 | ExprResult &RHS, QualType LHSType, |
| 922 | QualType RHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 923 | bool IsCompAssign) { |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 924 | const ComplexType *LHSComplexInt = LHSType->getAsComplexIntegerType(); |
| 925 | const ComplexType *RHSComplexInt = RHSType->getAsComplexIntegerType(); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 926 | |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 927 | if (LHSComplexInt && RHSComplexInt) { |
| 928 | int order = S.Context.getIntegerTypeOrder(LHSComplexInt->getElementType(), |
| 929 | RHSComplexInt->getElementType()); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 930 | assert(order && "inequal types with equal element ordering"); |
| 931 | if (order > 0) { |
| 932 | // _Complex int -> _Complex long |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 933 | RHS = S.ImpCastExprToType(RHS.take(), LHSType, CK_IntegralComplexCast); |
| 934 | return LHSType; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 935 | } |
| 936 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 937 | if (!IsCompAssign) |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 938 | LHS = S.ImpCastExprToType(LHS.take(), RHSType, CK_IntegralComplexCast); |
| 939 | return RHSType; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 940 | } |
| 941 | |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 942 | if (LHSComplexInt) { |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 943 | // int -> _Complex int |
Eli Friedman | ddadaa4 | 2011-11-12 03:56:23 +0000 | [diff] [blame] | 944 | // FIXME: This needs to take integer ranks into account |
| 945 | RHS = S.ImpCastExprToType(RHS.take(), LHSComplexInt->getElementType(), |
| 946 | CK_IntegralCast); |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 947 | RHS = S.ImpCastExprToType(RHS.take(), LHSType, CK_IntegralRealToComplex); |
| 948 | return LHSType; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 949 | } |
| 950 | |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 951 | assert(RHSComplexInt); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 952 | // int -> _Complex int |
Eli Friedman | ddadaa4 | 2011-11-12 03:56:23 +0000 | [diff] [blame] | 953 | // FIXME: This needs to take integer ranks into account |
| 954 | if (!IsCompAssign) { |
| 955 | LHS = S.ImpCastExprToType(LHS.take(), RHSComplexInt->getElementType(), |
| 956 | CK_IntegralCast); |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 957 | LHS = S.ImpCastExprToType(LHS.take(), RHSType, CK_IntegralRealToComplex); |
Eli Friedman | ddadaa4 | 2011-11-12 03:56:23 +0000 | [diff] [blame] | 958 | } |
Richard Trieu | 8ef5c8e | 2011-09-06 18:38:41 +0000 | [diff] [blame] | 959 | return RHSType; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 960 | } |
| 961 | |
| 962 | /// \brief Handle integer arithmetic conversions. Helper function of |
| 963 | /// UsualArithmeticConversions() |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 964 | static QualType handleIntegerConversion(Sema &S, ExprResult &LHS, |
| 965 | ExprResult &RHS, QualType LHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 966 | QualType RHSType, bool IsCompAssign) { |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 967 | // The rules for this case are in C99 6.3.1.8 |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 968 | int order = S.Context.getIntegerTypeOrder(LHSType, RHSType); |
| 969 | bool LHSSigned = LHSType->hasSignedIntegerRepresentation(); |
| 970 | bool RHSSigned = RHSType->hasSignedIntegerRepresentation(); |
| 971 | if (LHSSigned == RHSSigned) { |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 972 | // Same signedness; use the higher-ranked type |
| 973 | if (order >= 0) { |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 974 | RHS = S.ImpCastExprToType(RHS.take(), LHSType, CK_IntegralCast); |
| 975 | return LHSType; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 976 | } else if (!IsCompAssign) |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 977 | LHS = S.ImpCastExprToType(LHS.take(), RHSType, CK_IntegralCast); |
| 978 | return RHSType; |
| 979 | } else if (order != (LHSSigned ? 1 : -1)) { |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 980 | // The unsigned type has greater than or equal rank to the |
| 981 | // signed type, so use the unsigned type |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 982 | if (RHSSigned) { |
| 983 | RHS = S.ImpCastExprToType(RHS.take(), LHSType, CK_IntegralCast); |
| 984 | return LHSType; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 985 | } else if (!IsCompAssign) |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 986 | LHS = S.ImpCastExprToType(LHS.take(), RHSType, CK_IntegralCast); |
| 987 | return RHSType; |
| 988 | } else if (S.Context.getIntWidth(LHSType) != S.Context.getIntWidth(RHSType)) { |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 989 | // The two types are different widths; if we are here, that |
| 990 | // means the signed type is larger than the unsigned type, so |
| 991 | // use the signed type. |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 992 | if (LHSSigned) { |
| 993 | RHS = S.ImpCastExprToType(RHS.take(), LHSType, CK_IntegralCast); |
| 994 | return LHSType; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 995 | } else if (!IsCompAssign) |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 996 | LHS = S.ImpCastExprToType(LHS.take(), RHSType, CK_IntegralCast); |
| 997 | return RHSType; |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 998 | } else { |
| 999 | // The signed type is higher-ranked than the unsigned type, |
| 1000 | // but isn't actually any bigger (like unsigned int and long |
| 1001 | // on most 32-bit systems). Use the unsigned type corresponding |
| 1002 | // to the signed type. |
| 1003 | QualType result = |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1004 | S.Context.getCorrespondingUnsignedType(LHSSigned ? LHSType : RHSType); |
| 1005 | RHS = S.ImpCastExprToType(RHS.take(), result, CK_IntegralCast); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1006 | if (!IsCompAssign) |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1007 | LHS = S.ImpCastExprToType(LHS.take(), result, CK_IntegralCast); |
Richard Trieu | 8289f49 | 2011-09-02 20:58:51 +0000 | [diff] [blame] | 1008 | return result; |
| 1009 | } |
| 1010 | } |
| 1011 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 1012 | /// UsualArithmeticConversions - Performs various conversions that are common to |
| 1013 | /// 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] | 1014 | /// routine returns the first non-arithmetic type found. The client is |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 1015 | /// responsible for emitting appropriate error diagnostics. |
| 1016 | /// FIXME: verify the conversion rules for "complex int" are consistent with |
| 1017 | /// GCC. |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1018 | QualType Sema::UsualArithmeticConversions(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1019 | bool IsCompAssign) { |
| 1020 | if (!IsCompAssign) { |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1021 | LHS = UsualUnaryConversions(LHS.take()); |
| 1022 | if (LHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1023 | return QualType(); |
| 1024 | } |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 1025 | |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1026 | RHS = UsualUnaryConversions(RHS.take()); |
| 1027 | if (RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1028 | return QualType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1029 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1030 | // For conversion purposes, we ignore any qualifiers. |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 1031 | // For example, "const float" and "float" are equivalent. |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1032 | QualType LHSType = |
| 1033 | Context.getCanonicalType(LHS.get()->getType()).getUnqualifiedType(); |
| 1034 | QualType RHSType = |
| 1035 | Context.getCanonicalType(RHS.get()->getType()).getUnqualifiedType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1036 | |
Eli Friedman | 860a319 | 2012-06-16 02:19:17 +0000 | [diff] [blame] | 1037 | // For conversion purposes, we ignore any atomic qualifier on the LHS. |
| 1038 | if (const AtomicType *AtomicLHS = LHSType->getAs<AtomicType>()) |
| 1039 | LHSType = AtomicLHS->getValueType(); |
| 1040 | |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1041 | // If both types are identical, no conversion is needed. |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1042 | if (LHSType == RHSType) |
| 1043 | return LHSType; |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1044 | |
| 1045 | // If either side is a non-arithmetic type (e.g. a pointer), we are done. |
| 1046 | // The caller can deal with this (e.g. pointer + int). |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1047 | if (!LHSType->isArithmeticType() || !RHSType->isArithmeticType()) |
Eli Friedman | 860a319 | 2012-06-16 02:19:17 +0000 | [diff] [blame] | 1048 | return QualType(); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1049 | |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 1050 | // Apply unary and bitfield promotions to the LHS's type. |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1051 | QualType LHSUnpromotedType = LHSType; |
| 1052 | if (LHSType->isPromotableIntegerType()) |
| 1053 | LHSType = Context.getPromotedIntegerType(LHSType); |
| 1054 | QualType LHSBitfieldPromoteTy = Context.isPromotableBitField(LHS.get()); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 1055 | if (!LHSBitfieldPromoteTy.isNull()) |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1056 | LHSType = LHSBitfieldPromoteTy; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1057 | if (LHSType != LHSUnpromotedType && !IsCompAssign) |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1058 | LHS = ImpCastExprToType(LHS.take(), LHSType, CK_IntegralCast); |
Douglas Gregor | 2d833e3 | 2009-05-02 00:36:19 +0000 | [diff] [blame] | 1059 | |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 1060 | // If both types are identical, no conversion is needed. |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1061 | if (LHSType == RHSType) |
| 1062 | return LHSType; |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 1063 | |
| 1064 | // At this point, we have two different arithmetic types. |
| 1065 | |
| 1066 | // Handle complex types first (C99 6.3.1.8p1). |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1067 | if (LHSType->isComplexType() || RHSType->isComplexType()) |
| 1068 | return handleComplexFloatConversion(*this, LHS, RHS, LHSType, RHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1069 | IsCompAssign); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 1070 | |
| 1071 | // Now handle "real" floating types (i.e. float, double, long double). |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1072 | if (LHSType->isRealFloatingType() || RHSType->isRealFloatingType()) |
| 1073 | return handleFloatConversion(*this, LHS, RHS, LHSType, RHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1074 | IsCompAssign); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 1075 | |
| 1076 | // Handle GCC complex int extension. |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1077 | if (LHSType->isComplexIntegerType() || RHSType->isComplexIntegerType()) |
Benjamin Kramer | 5cc8680 | 2011-09-06 19:57:14 +0000 | [diff] [blame] | 1078 | return handleComplexIntConversion(*this, LHS, RHS, LHSType, RHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1079 | IsCompAssign); |
John McCall | cf33b24 | 2010-11-13 08:17:45 +0000 | [diff] [blame] | 1080 | |
| 1081 | // Finally, we have two differing integer types. |
Richard Trieu | 2e8a95d | 2011-09-06 19:52:52 +0000 | [diff] [blame] | 1082 | return handleIntegerConversion(*this, LHS, RHS, LHSType, RHSType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1083 | IsCompAssign); |
Douglas Gregor | eb8f306 | 2008-11-12 17:17:38 +0000 | [diff] [blame] | 1084 | } |
| 1085 | |
Chris Lattner | e7a2e91 | 2008-07-25 21:10:04 +0000 | [diff] [blame] | 1086 | //===----------------------------------------------------------------------===// |
| 1087 | // Semantic Analysis for various Expression Types |
| 1088 | //===----------------------------------------------------------------------===// |
| 1089 | |
| 1090 | |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1091 | ExprResult |
| 1092 | Sema::ActOnGenericSelectionExpr(SourceLocation KeyLoc, |
| 1093 | SourceLocation DefaultLoc, |
| 1094 | SourceLocation RParenLoc, |
| 1095 | Expr *ControllingExpr, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1096 | MultiTypeArg ArgTypes, |
| 1097 | MultiExprArg ArgExprs) { |
| 1098 | unsigned NumAssocs = ArgTypes.size(); |
| 1099 | assert(NumAssocs == ArgExprs.size()); |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1100 | |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 1101 | ParsedType *ParsedTypes = ArgTypes.data(); |
| 1102 | Expr **Exprs = ArgExprs.data(); |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1103 | |
| 1104 | TypeSourceInfo **Types = new TypeSourceInfo*[NumAssocs]; |
| 1105 | for (unsigned i = 0; i < NumAssocs; ++i) { |
| 1106 | if (ParsedTypes[i]) |
| 1107 | (void) GetTypeFromParser(ParsedTypes[i], &Types[i]); |
| 1108 | else |
| 1109 | Types[i] = 0; |
| 1110 | } |
| 1111 | |
| 1112 | ExprResult ER = CreateGenericSelectionExpr(KeyLoc, DefaultLoc, RParenLoc, |
| 1113 | ControllingExpr, Types, Exprs, |
| 1114 | NumAssocs); |
Benjamin Kramer | 5bf47f7 | 2011-04-15 11:21:57 +0000 | [diff] [blame] | 1115 | delete [] Types; |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1116 | return ER; |
| 1117 | } |
| 1118 | |
| 1119 | ExprResult |
| 1120 | Sema::CreateGenericSelectionExpr(SourceLocation KeyLoc, |
| 1121 | SourceLocation DefaultLoc, |
| 1122 | SourceLocation RParenLoc, |
| 1123 | Expr *ControllingExpr, |
| 1124 | TypeSourceInfo **Types, |
| 1125 | Expr **Exprs, |
| 1126 | unsigned NumAssocs) { |
| 1127 | bool TypeErrorFound = false, |
| 1128 | IsResultDependent = ControllingExpr->isTypeDependent(), |
| 1129 | ContainsUnexpandedParameterPack |
| 1130 | = ControllingExpr->containsUnexpandedParameterPack(); |
| 1131 | |
| 1132 | for (unsigned i = 0; i < NumAssocs; ++i) { |
| 1133 | if (Exprs[i]->containsUnexpandedParameterPack()) |
| 1134 | ContainsUnexpandedParameterPack = true; |
| 1135 | |
| 1136 | if (Types[i]) { |
| 1137 | if (Types[i]->getType()->containsUnexpandedParameterPack()) |
| 1138 | ContainsUnexpandedParameterPack = true; |
| 1139 | |
| 1140 | if (Types[i]->getType()->isDependentType()) { |
| 1141 | IsResultDependent = true; |
| 1142 | } else { |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 1143 | // C11 6.5.1.1p2 "The type name in a generic association shall specify a |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1144 | // complete object type other than a variably modified type." |
| 1145 | unsigned D = 0; |
| 1146 | if (Types[i]->getType()->isIncompleteType()) |
| 1147 | D = diag::err_assoc_type_incomplete; |
| 1148 | else if (!Types[i]->getType()->isObjectType()) |
| 1149 | D = diag::err_assoc_type_nonobject; |
| 1150 | else if (Types[i]->getType()->isVariablyModifiedType()) |
| 1151 | D = diag::err_assoc_type_variably_modified; |
| 1152 | |
| 1153 | if (D != 0) { |
| 1154 | Diag(Types[i]->getTypeLoc().getBeginLoc(), D) |
| 1155 | << Types[i]->getTypeLoc().getSourceRange() |
| 1156 | << Types[i]->getType(); |
| 1157 | TypeErrorFound = true; |
| 1158 | } |
| 1159 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 1160 | // C11 6.5.1.1p2 "No two generic associations in the same generic |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1161 | // selection shall specify compatible types." |
| 1162 | for (unsigned j = i+1; j < NumAssocs; ++j) |
| 1163 | if (Types[j] && !Types[j]->getType()->isDependentType() && |
| 1164 | Context.typesAreCompatible(Types[i]->getType(), |
| 1165 | Types[j]->getType())) { |
| 1166 | Diag(Types[j]->getTypeLoc().getBeginLoc(), |
| 1167 | diag::err_assoc_compatible_types) |
| 1168 | << Types[j]->getTypeLoc().getSourceRange() |
| 1169 | << Types[j]->getType() |
| 1170 | << Types[i]->getType(); |
| 1171 | Diag(Types[i]->getTypeLoc().getBeginLoc(), |
| 1172 | diag::note_compat_assoc) |
| 1173 | << Types[i]->getTypeLoc().getSourceRange() |
| 1174 | << Types[i]->getType(); |
| 1175 | TypeErrorFound = true; |
| 1176 | } |
| 1177 | } |
| 1178 | } |
| 1179 | } |
| 1180 | if (TypeErrorFound) |
| 1181 | return ExprError(); |
| 1182 | |
| 1183 | // If we determined that the generic selection is result-dependent, don't |
| 1184 | // try to compute the result expression. |
| 1185 | if (IsResultDependent) |
| 1186 | return Owned(new (Context) GenericSelectionExpr( |
| 1187 | Context, KeyLoc, ControllingExpr, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 1188 | llvm::makeArrayRef(Types, NumAssocs), |
| 1189 | llvm::makeArrayRef(Exprs, NumAssocs), |
| 1190 | DefaultLoc, RParenLoc, ContainsUnexpandedParameterPack)); |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1191 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1192 | SmallVector<unsigned, 1> CompatIndices; |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1193 | unsigned DefaultIndex = -1U; |
| 1194 | for (unsigned i = 0; i < NumAssocs; ++i) { |
| 1195 | if (!Types[i]) |
| 1196 | DefaultIndex = i; |
| 1197 | else if (Context.typesAreCompatible(ControllingExpr->getType(), |
| 1198 | Types[i]->getType())) |
| 1199 | CompatIndices.push_back(i); |
| 1200 | } |
| 1201 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 1202 | // C11 6.5.1.1p2 "The controlling expression of a generic selection shall have |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1203 | // type compatible with at most one of the types named in its generic |
| 1204 | // association list." |
| 1205 | if (CompatIndices.size() > 1) { |
| 1206 | // We strip parens here because the controlling expression is typically |
| 1207 | // parenthesized in macro definitions. |
| 1208 | ControllingExpr = ControllingExpr->IgnoreParens(); |
| 1209 | Diag(ControllingExpr->getLocStart(), diag::err_generic_sel_multi_match) |
| 1210 | << ControllingExpr->getSourceRange() << ControllingExpr->getType() |
| 1211 | << (unsigned) CompatIndices.size(); |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1212 | for (SmallVector<unsigned, 1>::iterator I = CompatIndices.begin(), |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1213 | E = CompatIndices.end(); I != E; ++I) { |
| 1214 | Diag(Types[*I]->getTypeLoc().getBeginLoc(), |
| 1215 | diag::note_compat_assoc) |
| 1216 | << Types[*I]->getTypeLoc().getSourceRange() |
| 1217 | << Types[*I]->getType(); |
| 1218 | } |
| 1219 | return ExprError(); |
| 1220 | } |
| 1221 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 1222 | // C11 6.5.1.1p2 "If a generic selection has no default generic association, |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1223 | // its controlling expression shall have type compatible with exactly one of |
| 1224 | // the types named in its generic association list." |
| 1225 | if (DefaultIndex == -1U && CompatIndices.size() == 0) { |
| 1226 | // We strip parens here because the controlling expression is typically |
| 1227 | // parenthesized in macro definitions. |
| 1228 | ControllingExpr = ControllingExpr->IgnoreParens(); |
| 1229 | Diag(ControllingExpr->getLocStart(), diag::err_generic_sel_no_match) |
| 1230 | << ControllingExpr->getSourceRange() << ControllingExpr->getType(); |
| 1231 | return ExprError(); |
| 1232 | } |
| 1233 | |
Benjamin Kramer | ffbe9b9 | 2011-12-23 17:00:35 +0000 | [diff] [blame] | 1234 | // C11 6.5.1.1p3 "If a generic selection has a generic association with a |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1235 | // type name that is compatible with the type of the controlling expression, |
| 1236 | // then the result expression of the generic selection is the expression |
| 1237 | // in that generic association. Otherwise, the result expression of the |
| 1238 | // generic selection is the expression in the default generic association." |
| 1239 | unsigned ResultIndex = |
| 1240 | CompatIndices.size() ? CompatIndices[0] : DefaultIndex; |
| 1241 | |
| 1242 | return Owned(new (Context) GenericSelectionExpr( |
| 1243 | Context, KeyLoc, ControllingExpr, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 1244 | llvm::makeArrayRef(Types, NumAssocs), |
| 1245 | llvm::makeArrayRef(Exprs, NumAssocs), |
| 1246 | DefaultLoc, RParenLoc, ContainsUnexpandedParameterPack, |
Peter Collingbourne | f111d93 | 2011-04-15 00:35:48 +0000 | [diff] [blame] | 1247 | ResultIndex)); |
| 1248 | } |
| 1249 | |
Richard Smith | dd66be7 | 2012-03-08 01:34:56 +0000 | [diff] [blame] | 1250 | /// getUDSuffixLoc - Create a SourceLocation for a ud-suffix, given the |
| 1251 | /// location of the token and the offset of the ud-suffix within it. |
| 1252 | static SourceLocation getUDSuffixLoc(Sema &S, SourceLocation TokLoc, |
| 1253 | unsigned Offset) { |
| 1254 | return Lexer::AdvanceToTokenCharacter(TokLoc, Offset, S.getSourceManager(), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1255 | S.getLangOpts()); |
Richard Smith | dd66be7 | 2012-03-08 01:34:56 +0000 | [diff] [blame] | 1256 | } |
| 1257 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 1258 | /// BuildCookedLiteralOperatorCall - A user-defined literal was found. Look up |
| 1259 | /// the corresponding cooked (non-raw) literal operator, and build a call to it. |
| 1260 | static ExprResult BuildCookedLiteralOperatorCall(Sema &S, Scope *Scope, |
| 1261 | IdentifierInfo *UDSuffix, |
| 1262 | SourceLocation UDSuffixLoc, |
| 1263 | ArrayRef<Expr*> Args, |
| 1264 | SourceLocation LitEndLoc) { |
| 1265 | assert(Args.size() <= 2 && "too many arguments for literal operator"); |
| 1266 | |
| 1267 | QualType ArgTy[2]; |
| 1268 | for (unsigned ArgIdx = 0; ArgIdx != Args.size(); ++ArgIdx) { |
| 1269 | ArgTy[ArgIdx] = Args[ArgIdx]->getType(); |
| 1270 | if (ArgTy[ArgIdx]->isArrayType()) |
| 1271 | ArgTy[ArgIdx] = S.Context.getArrayDecayedType(ArgTy[ArgIdx]); |
| 1272 | } |
| 1273 | |
| 1274 | DeclarationName OpName = |
| 1275 | S.Context.DeclarationNames.getCXXLiteralOperatorName(UDSuffix); |
| 1276 | DeclarationNameInfo OpNameInfo(OpName, UDSuffixLoc); |
| 1277 | OpNameInfo.setCXXLiteralOperatorNameLoc(UDSuffixLoc); |
| 1278 | |
| 1279 | LookupResult R(S, OpName, UDSuffixLoc, Sema::LookupOrdinaryName); |
| 1280 | if (S.LookupLiteralOperator(Scope, R, llvm::makeArrayRef(ArgTy, Args.size()), |
| 1281 | /*AllowRawAndTemplate*/false) == Sema::LOLR_Error) |
| 1282 | return ExprError(); |
| 1283 | |
| 1284 | return S.BuildLiteralOperatorCall(R, OpNameInfo, Args, LitEndLoc); |
| 1285 | } |
| 1286 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 1287 | /// ActOnStringLiteral - The specified tokens were lexed as pasted string |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1288 | /// fragments (e.g. "foo" "bar" L"baz"). The result string has to handle string |
| 1289 | /// concatenation ([C99 5.1.1.2, translation phase #6]), so it may come from |
| 1290 | /// multiple tokens. However, the common case is that StringToks points to one |
| 1291 | /// string. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1292 | /// |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1293 | ExprResult |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 1294 | Sema::ActOnStringLiteral(const Token *StringToks, unsigned NumStringToks, |
| 1295 | Scope *UDLScope) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1296 | assert(NumStringToks && "Must have at least one string!"); |
| 1297 | |
Chris Lattner | bbee00b | 2009-01-16 18:51:42 +0000 | [diff] [blame] | 1298 | StringLiteralParser Literal(StringToks, NumStringToks, PP); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1299 | if (Literal.hadError) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1300 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1301 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 1302 | SmallVector<SourceLocation, 4> StringTokLocs; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1303 | for (unsigned i = 0; i != NumStringToks; ++i) |
| 1304 | StringTokLocs.push_back(StringToks[i].getLocation()); |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 1305 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 1306 | QualType StrTy = Context.CharTy; |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1307 | if (Literal.isWide()) |
Anders Carlsson | 96b4adc | 2011-04-06 18:42:48 +0000 | [diff] [blame] | 1308 | StrTy = Context.getWCharType(); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1309 | else if (Literal.isUTF16()) |
| 1310 | StrTy = Context.Char16Ty; |
| 1311 | else if (Literal.isUTF32()) |
| 1312 | StrTy = Context.Char32Ty; |
Eli Friedman | 64f45a2 | 2011-11-01 02:23:42 +0000 | [diff] [blame] | 1313 | else if (Literal.isPascal()) |
Anders Carlsson | 96b4adc | 2011-04-06 18:42:48 +0000 | [diff] [blame] | 1314 | StrTy = Context.UnsignedCharTy; |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 1315 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 1316 | StringLiteral::StringKind Kind = StringLiteral::Ascii; |
| 1317 | if (Literal.isWide()) |
| 1318 | Kind = StringLiteral::Wide; |
| 1319 | else if (Literal.isUTF8()) |
| 1320 | Kind = StringLiteral::UTF8; |
| 1321 | else if (Literal.isUTF16()) |
| 1322 | Kind = StringLiteral::UTF16; |
| 1323 | else if (Literal.isUTF32()) |
| 1324 | Kind = StringLiteral::UTF32; |
| 1325 | |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 1326 | // A C++ string literal has a const-qualified element type (C++ 2.13.4p1). |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1327 | if (getLangOpts().CPlusPlus || getLangOpts().ConstStrings) |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 1328 | StrTy.addConst(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 1329 | |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 1330 | // Get an array type for the string, according to C99 6.4.5. This includes |
| 1331 | // the nul terminator character as well as the string length for pascal |
| 1332 | // strings. |
| 1333 | StrTy = Context.getConstantArrayType(StrTy, |
Chris Lattner | dbb1ecc | 2009-02-26 23:01:51 +0000 | [diff] [blame] | 1334 | llvm::APInt(32, Literal.GetNumStringChars()+1), |
Chris Lattner | a7ad98f | 2008-02-11 00:02:17 +0000 | [diff] [blame] | 1335 | ArrayType::Normal, 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1336 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1337 | // Pass &StringTokLocs[0], StringTokLocs.size() to factory! |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 1338 | StringLiteral *Lit = StringLiteral::Create(Context, Literal.GetString(), |
| 1339 | Kind, Literal.Pascal, StrTy, |
| 1340 | &StringTokLocs[0], |
| 1341 | StringTokLocs.size()); |
| 1342 | if (Literal.getUDSuffix().empty()) |
| 1343 | return Owned(Lit); |
| 1344 | |
| 1345 | // We're building a user-defined literal. |
| 1346 | IdentifierInfo *UDSuffix = &Context.Idents.get(Literal.getUDSuffix()); |
Richard Smith | dd66be7 | 2012-03-08 01:34:56 +0000 | [diff] [blame] | 1347 | SourceLocation UDSuffixLoc = |
| 1348 | getUDSuffixLoc(*this, StringTokLocs[Literal.getUDSuffixToken()], |
| 1349 | Literal.getUDSuffixOffset()); |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 1350 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 1351 | // Make sure we're allowed user-defined literals here. |
| 1352 | if (!UDLScope) |
| 1353 | return ExprError(Diag(UDSuffixLoc, diag::err_invalid_string_udl)); |
| 1354 | |
Richard Smith | 9fcce65 | 2012-03-07 08:35:16 +0000 | [diff] [blame] | 1355 | // C++11 [lex.ext]p5: The literal L is treated as a call of the form |
| 1356 | // operator "" X (str, len) |
| 1357 | QualType SizeType = Context.getSizeType(); |
| 1358 | llvm::APInt Len(Context.getIntWidth(SizeType), Literal.GetNumStringChars()); |
| 1359 | IntegerLiteral *LenArg = IntegerLiteral::Create(Context, Len, SizeType, |
| 1360 | StringTokLocs[0]); |
| 1361 | Expr *Args[] = { Lit, LenArg }; |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 1362 | return BuildCookedLiteralOperatorCall(*this, UDLScope, UDSuffix, UDSuffixLoc, |
| 1363 | Args, StringTokLocs.back()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1364 | } |
| 1365 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1366 | ExprResult |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1367 | Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1368 | SourceLocation Loc, |
| 1369 | const CXXScopeSpec *SS) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1370 | DeclarationNameInfo NameInfo(D->getDeclName(), Loc); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 1371 | return BuildDeclRefExpr(D, Ty, VK, NameInfo, SS); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1372 | } |
| 1373 | |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1374 | /// BuildDeclRefExpr - Build an expression that references a |
| 1375 | /// declaration that does not require a closure capture. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1376 | ExprResult |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 1377 | Sema::BuildDeclRefExpr(ValueDecl *D, QualType Ty, ExprValueKind VK, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1378 | const DeclarationNameInfo &NameInfo, |
| 1379 | const CXXScopeSpec *SS) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1380 | if (getLangOpts().CUDA) |
Peter Collingbourne | 78dd67e | 2011-10-02 23:49:40 +0000 | [diff] [blame] | 1381 | if (const FunctionDecl *Caller = dyn_cast<FunctionDecl>(CurContext)) |
| 1382 | if (const FunctionDecl *Callee = dyn_cast<FunctionDecl>(D)) { |
| 1383 | CUDAFunctionTarget CallerTarget = IdentifyCUDATarget(Caller), |
| 1384 | CalleeTarget = IdentifyCUDATarget(Callee); |
| 1385 | if (CheckCUDATarget(CallerTarget, CalleeTarget)) { |
| 1386 | Diag(NameInfo.getLoc(), diag::err_ref_bad_target) |
| 1387 | << CalleeTarget << D->getIdentifier() << CallerTarget; |
| 1388 | Diag(D->getLocation(), diag::note_previous_decl) |
| 1389 | << D->getIdentifier(); |
| 1390 | return ExprError(); |
| 1391 | } |
| 1392 | } |
| 1393 | |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 1394 | bool refersToEnclosingScope = |
| 1395 | (CurContext != D->getDeclContext() && |
| 1396 | D->getDeclContext()->isFunctionOrMethod()); |
| 1397 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 1398 | DeclRefExpr *E = DeclRefExpr::Create(Context, |
| 1399 | SS ? SS->getWithLocInContext(Context) |
| 1400 | : NestedNameSpecifierLoc(), |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 1401 | SourceLocation(), |
| 1402 | D, refersToEnclosingScope, |
| 1403 | NameInfo, Ty, VK); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1404 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 1405 | MarkDeclRefReferenced(E); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1406 | |
| 1407 | // Just in case we're building an illegal pointer-to-member. |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 1408 | FieldDecl *FD = dyn_cast<FieldDecl>(D); |
| 1409 | if (FD && FD->isBitField()) |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 1410 | E->setObjectKind(OK_BitField); |
| 1411 | |
| 1412 | return Owned(E); |
Douglas Gregor | 1a49af9 | 2009-01-06 05:10:23 +0000 | [diff] [blame] | 1413 | } |
| 1414 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1415 | /// Decomposes the given name into a DeclarationNameInfo, its location, and |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1416 | /// possibly a list of template arguments. |
| 1417 | /// |
| 1418 | /// If this produces template arguments, it is permitted to call |
| 1419 | /// DecomposeTemplateName. |
| 1420 | /// |
| 1421 | /// This actually loses a lot of source location information for |
| 1422 | /// non-standard name kinds; we should consider preserving that in |
| 1423 | /// some way. |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 1424 | void |
| 1425 | Sema::DecomposeUnqualifiedId(const UnqualifiedId &Id, |
| 1426 | TemplateArgumentListInfo &Buffer, |
| 1427 | DeclarationNameInfo &NameInfo, |
| 1428 | const TemplateArgumentListInfo *&TemplateArgs) { |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1429 | if (Id.getKind() == UnqualifiedId::IK_TemplateId) { |
| 1430 | Buffer.setLAngleLoc(Id.TemplateId->LAngleLoc); |
| 1431 | Buffer.setRAngleLoc(Id.TemplateId->RAngleLoc); |
| 1432 | |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 1433 | ASTTemplateArgsPtr TemplateArgsPtr(Id.TemplateId->getTemplateArgs(), |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1434 | Id.TemplateId->NumArgs); |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1435 | translateTemplateArguments(TemplateArgsPtr, Buffer); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1436 | |
John McCall | 2b5289b | 2010-08-23 07:28:44 +0000 | [diff] [blame] | 1437 | TemplateName TName = Id.TemplateId->Template.get(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1438 | SourceLocation TNameLoc = Id.TemplateId->TemplateNameLoc; |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1439 | NameInfo = Context.getNameForTemplate(TName, TNameLoc); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1440 | TemplateArgs = &Buffer; |
| 1441 | } else { |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1442 | NameInfo = GetNameFromUnqualifiedId(Id); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1443 | TemplateArgs = 0; |
| 1444 | } |
| 1445 | } |
| 1446 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1447 | /// Diagnose an empty lookup. |
| 1448 | /// |
| 1449 | /// \return false if new lookup candidates were found |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1450 | bool Sema::DiagnoseEmptyLookup(Scope *S, CXXScopeSpec &SS, LookupResult &R, |
Kaelyn Uhrain | 4798f8d | 2012-01-18 05:58:54 +0000 | [diff] [blame] | 1451 | CorrectionCandidateCallback &CCC, |
Kaelyn Uhrain | ace5e76 | 2011-08-05 00:09:52 +0000 | [diff] [blame] | 1452 | TemplateArgumentListInfo *ExplicitTemplateArgs, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 1453 | llvm::ArrayRef<Expr *> Args) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1454 | DeclarationName Name = R.getLookupName(); |
| 1455 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1456 | unsigned diagnostic = diag::err_undeclared_var_use; |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1457 | unsigned diagnostic_suggest = diag::err_undeclared_var_use_suggest; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1458 | if (Name.getNameKind() == DeclarationName::CXXOperatorName || |
| 1459 | Name.getNameKind() == DeclarationName::CXXLiteralOperatorName || |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1460 | Name.getNameKind() == DeclarationName::CXXConversionFunctionName) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1461 | diagnostic = diag::err_undeclared_use; |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1462 | diagnostic_suggest = diag::err_undeclared_use_suggest; |
| 1463 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1464 | |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1465 | // If the original lookup was an unqualified lookup, fake an |
| 1466 | // unqualified lookup. This is useful when (for example) the |
| 1467 | // original lookup would not have found something because it was a |
| 1468 | // dependent name. |
David Blaikie | 4872e10 | 2012-05-28 01:26:45 +0000 | [diff] [blame] | 1469 | DeclContext *DC = (SS.isEmpty() && !CallsUndergoingInstantiation.empty()) |
| 1470 | ? CurContext : 0; |
Francois Pichet | c8ff915 | 2011-11-25 01:10:54 +0000 | [diff] [blame] | 1471 | while (DC) { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1472 | if (isa<CXXRecordDecl>(DC)) { |
| 1473 | LookupQualifiedName(R, DC); |
| 1474 | |
| 1475 | if (!R.empty()) { |
| 1476 | // Don't give errors about ambiguities in this lookup. |
| 1477 | R.suppressDiagnostics(); |
| 1478 | |
Francois Pichet | e6226ae | 2011-11-17 03:44:24 +0000 | [diff] [blame] | 1479 | // During a default argument instantiation the CurContext points |
| 1480 | // to a CXXMethodDecl; but we can't apply a this-> fixit inside a |
| 1481 | // function parameter list, hence add an explicit check. |
| 1482 | bool isDefaultArgument = !ActiveTemplateInstantiations.empty() && |
| 1483 | ActiveTemplateInstantiations.back().Kind == |
| 1484 | ActiveTemplateInstantiation::DefaultFunctionArgumentInstantiation; |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1485 | CXXMethodDecl *CurMethod = dyn_cast<CXXMethodDecl>(CurContext); |
| 1486 | bool isInstance = CurMethod && |
| 1487 | CurMethod->isInstance() && |
Francois Pichet | e6226ae | 2011-11-17 03:44:24 +0000 | [diff] [blame] | 1488 | DC == CurMethod->getParent() && !isDefaultArgument; |
| 1489 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1490 | |
| 1491 | // Give a code modification hint to insert 'this->'. |
| 1492 | // TODO: fixit for inserting 'Base<T>::' in the other cases. |
| 1493 | // Actually quite difficult! |
Nico Weber | 4b554f4 | 2012-06-20 20:21:42 +0000 | [diff] [blame] | 1494 | if (getLangOpts().MicrosoftMode) |
| 1495 | diagnostic = diag::warn_found_via_dependent_bases_lookup; |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1496 | if (isInstance) { |
Nico Weber | 94c4d61 | 2012-06-22 16:39:39 +0000 | [diff] [blame] | 1497 | Diag(R.getNameLoc(), diagnostic) << Name |
| 1498 | << FixItHint::CreateInsertion(R.getNameLoc(), "this->"); |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1499 | UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>( |
| 1500 | CallsUndergoingInstantiation.back()->getCallee()); |
Nico Weber | 94c4d61 | 2012-06-22 16:39:39 +0000 | [diff] [blame] | 1501 | |
| 1502 | |
| 1503 | CXXMethodDecl *DepMethod; |
| 1504 | if (CurMethod->getTemplatedKind() == |
| 1505 | FunctionDecl::TK_FunctionTemplateSpecialization) |
| 1506 | DepMethod = cast<CXXMethodDecl>(CurMethod->getPrimaryTemplate()-> |
| 1507 | getInstantiatedFromMemberTemplate()->getTemplatedDecl()); |
| 1508 | else |
| 1509 | DepMethod = cast<CXXMethodDecl>( |
| 1510 | CurMethod->getInstantiatedFromMemberFunction()); |
| 1511 | assert(DepMethod && "No template pattern found"); |
| 1512 | |
| 1513 | QualType DepThisType = DepMethod->getThisType(Context); |
| 1514 | CheckCXXThisCapture(R.getNameLoc()); |
| 1515 | CXXThisExpr *DepThis = new (Context) CXXThisExpr( |
| 1516 | R.getNameLoc(), DepThisType, false); |
| 1517 | TemplateArgumentListInfo TList; |
| 1518 | if (ULE->hasExplicitTemplateArgs()) |
| 1519 | ULE->copyTemplateArgumentsInto(TList); |
| 1520 | |
| 1521 | CXXScopeSpec SS; |
| 1522 | SS.Adopt(ULE->getQualifierLoc()); |
| 1523 | CXXDependentScopeMemberExpr *DepExpr = |
| 1524 | CXXDependentScopeMemberExpr::Create( |
| 1525 | Context, DepThis, DepThisType, true, SourceLocation(), |
| 1526 | SS.getWithLocInContext(Context), |
| 1527 | ULE->getTemplateKeywordLoc(), 0, |
| 1528 | R.getLookupNameInfo(), |
| 1529 | ULE->hasExplicitTemplateArgs() ? &TList : 0); |
| 1530 | CallsUndergoingInstantiation.back()->setCallee(DepExpr); |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1531 | } else { |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1532 | Diag(R.getNameLoc(), diagnostic) << Name; |
Nick Lewycky | 03d98c5 | 2010-07-06 19:51:49 +0000 | [diff] [blame] | 1533 | } |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1534 | |
| 1535 | // Do we really want to note all of these? |
| 1536 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) |
| 1537 | Diag((*I)->getLocation(), diag::note_dependent_var_use); |
| 1538 | |
Francois Pichet | e6226ae | 2011-11-17 03:44:24 +0000 | [diff] [blame] | 1539 | // Return true if we are inside a default argument instantiation |
| 1540 | // and the found name refers to an instance member function, otherwise |
| 1541 | // the function calling DiagnoseEmptyLookup will try to create an |
| 1542 | // implicit member call and this is wrong for default argument. |
| 1543 | if (isDefaultArgument && ((*R.begin())->isCXXInstanceMember())) { |
| 1544 | Diag(R.getNameLoc(), diag::err_member_call_without_object); |
| 1545 | return true; |
| 1546 | } |
| 1547 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1548 | // Tell the callee to try to recover. |
| 1549 | return false; |
| 1550 | } |
Douglas Gregor | e26f043 | 2010-08-09 22:38:14 +0000 | [diff] [blame] | 1551 | |
| 1552 | R.clear(); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1553 | } |
Francois Pichet | c8ff915 | 2011-11-25 01:10:54 +0000 | [diff] [blame] | 1554 | |
| 1555 | // In Microsoft mode, if we are performing lookup from within a friend |
| 1556 | // function definition declared at class scope then we must set |
| 1557 | // DC to the lexical parent to be able to search into the parent |
| 1558 | // class. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1559 | if (getLangOpts().MicrosoftMode && isa<FunctionDecl>(DC) && |
Francois Pichet | c8ff915 | 2011-11-25 01:10:54 +0000 | [diff] [blame] | 1560 | cast<FunctionDecl>(DC)->getFriendObjectKind() && |
| 1561 | DC->getLexicalParent()->isRecord()) |
| 1562 | DC = DC->getLexicalParent(); |
| 1563 | else |
| 1564 | DC = DC->getParent(); |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1565 | } |
| 1566 | |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1567 | // We didn't find anything, so try to correct for a typo. |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1568 | TypoCorrection Corrected; |
| 1569 | if (S && (Corrected = CorrectTypo(R.getLookupNameInfo(), R.getLookupKind(), |
Kaelyn Uhrain | 16e46dd | 2012-01-31 23:49:25 +0000 | [diff] [blame] | 1570 | S, &SS, CCC))) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1571 | std::string CorrectedStr(Corrected.getAsString(getLangOpts())); |
| 1572 | std::string CorrectedQuotedStr(Corrected.getQuoted(getLangOpts())); |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1573 | R.setLookupName(Corrected.getCorrection()); |
| 1574 | |
Hans Wennborg | 701d1e7 | 2011-07-12 08:45:31 +0000 | [diff] [blame] | 1575 | if (NamedDecl *ND = Corrected.getCorrectionDecl()) { |
Kaelyn Uhrain | f0c1d8f | 2011-08-03 20:36:05 +0000 | [diff] [blame] | 1576 | if (Corrected.isOverloaded()) { |
| 1577 | OverloadCandidateSet OCS(R.getNameLoc()); |
| 1578 | OverloadCandidateSet::iterator Best; |
| 1579 | for (TypoCorrection::decl_iterator CD = Corrected.begin(), |
| 1580 | CDEnd = Corrected.end(); |
| 1581 | CD != CDEnd; ++CD) { |
Kaelyn Uhrain | adc7a73 | 2011-08-08 17:35:31 +0000 | [diff] [blame] | 1582 | if (FunctionTemplateDecl *FTD = |
Kaelyn Uhrain | ace5e76 | 2011-08-05 00:09:52 +0000 | [diff] [blame] | 1583 | dyn_cast<FunctionTemplateDecl>(*CD)) |
| 1584 | AddTemplateOverloadCandidate( |
| 1585 | FTD, DeclAccessPair::make(FTD, AS_none), ExplicitTemplateArgs, |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 1586 | Args, OCS); |
Kaelyn Uhrain | adc7a73 | 2011-08-08 17:35:31 +0000 | [diff] [blame] | 1587 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(*CD)) |
| 1588 | if (!ExplicitTemplateArgs || ExplicitTemplateArgs->size() == 0) |
| 1589 | AddOverloadCandidate(FD, DeclAccessPair::make(FD, AS_none), |
Ahmed Charles | 13a140c | 2012-02-25 11:00:22 +0000 | [diff] [blame] | 1590 | Args, OCS); |
Kaelyn Uhrain | f0c1d8f | 2011-08-03 20:36:05 +0000 | [diff] [blame] | 1591 | } |
| 1592 | switch (OCS.BestViableFunction(*this, R.getNameLoc(), Best)) { |
| 1593 | case OR_Success: |
| 1594 | ND = Best->Function; |
| 1595 | break; |
| 1596 | default: |
Kaelyn Uhrain | 844d572 | 2011-08-04 23:30:54 +0000 | [diff] [blame] | 1597 | break; |
Kaelyn Uhrain | f0c1d8f | 2011-08-03 20:36:05 +0000 | [diff] [blame] | 1598 | } |
| 1599 | } |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1600 | R.addDecl(ND); |
| 1601 | if (isa<ValueDecl>(ND) || isa<FunctionTemplateDecl>(ND)) { |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1602 | if (SS.isEmpty()) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1603 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << CorrectedQuotedStr |
| 1604 | << FixItHint::CreateReplacement(R.getNameLoc(), CorrectedStr); |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1605 | else |
| 1606 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1607 | << Name << computeDeclContext(SS, false) << CorrectedQuotedStr |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1608 | << SS.getRange() |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1609 | << FixItHint::CreateReplacement(R.getNameLoc(), CorrectedStr); |
| 1610 | if (ND) |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1611 | Diag(ND->getLocation(), diag::note_previous_decl) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1612 | << CorrectedQuotedStr; |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1613 | |
| 1614 | // Tell the callee to try to recover. |
| 1615 | return false; |
| 1616 | } |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1617 | |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1618 | if (isa<TypeDecl>(ND) || isa<ObjCInterfaceDecl>(ND)) { |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1619 | // FIXME: If we ended up with a typo for a type name or |
| 1620 | // Objective-C class name, we're in trouble because the parser |
| 1621 | // is in the wrong place to recover. Suggest the typo |
| 1622 | // correction, but don't make it a fix-it since we're not going |
| 1623 | // to recover well anyway. |
| 1624 | if (SS.isEmpty()) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 1625 | Diag(R.getNameLoc(), diagnostic_suggest) |
| 1626 | << Name << CorrectedQuotedStr; |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1627 | else |
| 1628 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1629 | << Name << computeDeclContext(SS, false) << CorrectedQuotedStr |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1630 | << SS.getRange(); |
| 1631 | |
| 1632 | // Don't try to recover; it won't work. |
| 1633 | return true; |
| 1634 | } |
| 1635 | } else { |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1636 | // 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] | 1637 | // because we aren't able to recover. |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1638 | if (SS.isEmpty()) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1639 | Diag(R.getNameLoc(), diagnostic_suggest) << Name << CorrectedQuotedStr; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1640 | else |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1641 | Diag(R.getNameLoc(), diag::err_no_member_suggest) |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1642 | << Name << computeDeclContext(SS, false) << CorrectedQuotedStr |
Douglas Gregor | aaf8716 | 2010-04-14 20:04:41 +0000 | [diff] [blame] | 1643 | << SS.getRange(); |
Douglas Gregor | d203a16 | 2010-01-01 00:15:04 +0000 | [diff] [blame] | 1644 | return true; |
| 1645 | } |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1646 | } |
Douglas Gregor | d8bba9c | 2011-06-28 16:20:02 +0000 | [diff] [blame] | 1647 | R.clear(); |
Douglas Gregor | bb092ba | 2009-12-31 05:20:13 +0000 | [diff] [blame] | 1648 | |
| 1649 | // Emit a special diagnostic for failed member lookups. |
| 1650 | // FIXME: computing the declaration context might fail here (?) |
| 1651 | if (!SS.isEmpty()) { |
| 1652 | Diag(R.getNameLoc(), diag::err_no_member) |
| 1653 | << Name << computeDeclContext(SS, false) |
| 1654 | << SS.getRange(); |
| 1655 | return true; |
| 1656 | } |
| 1657 | |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1658 | // Give up, we can't recover. |
| 1659 | Diag(R.getNameLoc(), diagnostic) << Name; |
| 1660 | return true; |
| 1661 | } |
| 1662 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1663 | ExprResult Sema::ActOnIdExpression(Scope *S, |
John McCall | fb97e75 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 1664 | CXXScopeSpec &SS, |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1665 | SourceLocation TemplateKWLoc, |
John McCall | fb97e75 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 1666 | UnqualifiedId &Id, |
| 1667 | bool HasTrailingLParen, |
Kaelyn Uhrain | cd78e61 | 2012-01-25 20:49:08 +0000 | [diff] [blame] | 1668 | bool IsAddressOfOperand, |
| 1669 | CorrectionCandidateCallback *CCC) { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1670 | assert(!(IsAddressOfOperand && HasTrailingLParen) && |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1671 | "cannot be direct & operand and have a trailing lparen"); |
| 1672 | |
| 1673 | if (SS.isInvalid()) |
Douglas Gregor | 4c921ae | 2009-01-30 01:04:22 +0000 | [diff] [blame] | 1674 | return ExprError(); |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1675 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1676 | TemplateArgumentListInfo TemplateArgsBuffer; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1677 | |
| 1678 | // Decompose the UnqualifiedId into the following data. |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1679 | DeclarationNameInfo NameInfo; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1680 | const TemplateArgumentListInfo *TemplateArgs; |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 1681 | DecomposeUnqualifiedId(Id, TemplateArgsBuffer, NameInfo, TemplateArgs); |
Douglas Gregor | 5953d8b | 2009-03-19 17:26:29 +0000 | [diff] [blame] | 1682 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1683 | DeclarationName Name = NameInfo.getName(); |
Douglas Gregor | 10c4262 | 2008-11-18 15:03:34 +0000 | [diff] [blame] | 1684 | IdentifierInfo *II = Name.getAsIdentifierInfo(); |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1685 | SourceLocation NameLoc = NameInfo.getLoc(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 1686 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1687 | // C++ [temp.dep.expr]p3: |
| 1688 | // An id-expression is type-dependent if it contains: |
Douglas Gregor | 48026d2 | 2010-01-11 18:40:55 +0000 | [diff] [blame] | 1689 | // -- an identifier that was declared with a dependent type, |
| 1690 | // (note: handled after lookup) |
| 1691 | // -- a template-id that is dependent, |
| 1692 | // (note: handled in BuildTemplateIdExpr) |
| 1693 | // -- a conversion-function-id that specifies a dependent type, |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1694 | // -- a nested-name-specifier that contains a class-name that |
| 1695 | // names a dependent type. |
| 1696 | // Determine whether this is a member of an unknown specialization; |
| 1697 | // we need to handle these differently. |
Eli Friedman | 647c8b3 | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1698 | bool DependentID = false; |
| 1699 | if (Name.getNameKind() == DeclarationName::CXXConversionFunctionName && |
| 1700 | Name.getCXXNameType()->isDependentType()) { |
| 1701 | DependentID = true; |
| 1702 | } else if (SS.isSet()) { |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1703 | if (DeclContext *DC = computeDeclContext(SS, false)) { |
Eli Friedman | 647c8b3 | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1704 | if (RequireCompleteDeclContext(SS, DC)) |
| 1705 | return ExprError(); |
Eli Friedman | 647c8b3 | 2010-08-06 23:41:47 +0000 | [diff] [blame] | 1706 | } else { |
| 1707 | DependentID = true; |
| 1708 | } |
| 1709 | } |
| 1710 | |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1711 | if (DependentID) |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1712 | return ActOnDependentIdExpression(SS, TemplateKWLoc, NameInfo, |
| 1713 | IsAddressOfOperand, TemplateArgs); |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1714 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1715 | // Perform the required lookup. |
Fariborz Jahanian | 98a5403 | 2011-07-12 17:16:56 +0000 | [diff] [blame] | 1716 | LookupResult R(*this, NameInfo, |
| 1717 | (Id.getKind() == UnqualifiedId::IK_ImplicitSelfParam) |
| 1718 | ? LookupObjCImplicitSelfParam : LookupOrdinaryName); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1719 | if (TemplateArgs) { |
Douglas Gregor | d2235f6 | 2010-05-20 20:58:56 +0000 | [diff] [blame] | 1720 | // Lookup the template name again to correctly establish the context in |
| 1721 | // which it was found. This is really unfortunate as we already did the |
| 1722 | // lookup to determine that it was a template name in the first place. If |
| 1723 | // this becomes a performance hit, we can work harder to preserve those |
| 1724 | // results until we get here but it's likely not worth it. |
Douglas Gregor | 1fd6d44 | 2010-05-21 23:18:07 +0000 | [diff] [blame] | 1725 | bool MemberOfUnknownSpecialization; |
| 1726 | LookupTemplateName(R, S, SS, QualType(), /*EnteringContext=*/false, |
| 1727 | MemberOfUnknownSpecialization); |
Douglas Gregor | 2f9f89c | 2011-02-04 13:35:07 +0000 | [diff] [blame] | 1728 | |
| 1729 | if (MemberOfUnknownSpecialization || |
| 1730 | (R.getResultKind() == LookupResult::NotFoundInCurrentInstantiation)) |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1731 | return ActOnDependentIdExpression(SS, TemplateKWLoc, NameInfo, |
| 1732 | IsAddressOfOperand, TemplateArgs); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1733 | } else { |
Benjamin Kramer | b7ff74a | 2012-01-20 14:57:34 +0000 | [diff] [blame] | 1734 | bool IvarLookupFollowUp = II && !SS.isSet() && getCurMethodDecl(); |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1735 | LookupParsedName(R, S, &SS, !IvarLookupFollowUp); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1736 | |
Douglas Gregor | 2f9f89c | 2011-02-04 13:35:07 +0000 | [diff] [blame] | 1737 | // If the result might be in a dependent base class, this is a dependent |
| 1738 | // id-expression. |
| 1739 | if (R.getResultKind() == LookupResult::NotFoundInCurrentInstantiation) |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1740 | return ActOnDependentIdExpression(SS, TemplateKWLoc, NameInfo, |
| 1741 | IsAddressOfOperand, TemplateArgs); |
| 1742 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1743 | // If this reference is in an Objective-C method, then we need to do |
| 1744 | // some special Objective-C lookup, too. |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1745 | if (IvarLookupFollowUp) { |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1746 | ExprResult E(LookupInObjCMethod(R, S, II, true)); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1747 | if (E.isInvalid()) |
| 1748 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1749 | |
Chris Lattner | 337e550 | 2011-02-18 01:27:55 +0000 | [diff] [blame] | 1750 | if (Expr *Ex = E.takeAs<Expr>()) |
| 1751 | return Owned(Ex); |
Steve Naroff | e3e9add | 2008-06-02 23:03:37 +0000 | [diff] [blame] | 1752 | } |
Chris Lattner | 8a93423 | 2008-03-31 00:36:02 +0000 | [diff] [blame] | 1753 | } |
Douglas Gregor | c71e28c | 2009-02-16 19:28:42 +0000 | [diff] [blame] | 1754 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1755 | if (R.isAmbiguous()) |
| 1756 | return ExprError(); |
| 1757 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1758 | // Determine whether this name might be a candidate for |
| 1759 | // argument-dependent lookup. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1760 | bool ADL = UseArgumentDependentLookup(SS, R, HasTrailingLParen); |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 1761 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1762 | if (R.empty() && !ADL) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1763 | // Otherwise, this could be an implicitly declared function reference (legal |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1764 | // in C90, extension in C99, forbidden in C++). |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1765 | if (HasTrailingLParen && II && !getLangOpts().CPlusPlus) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1766 | NamedDecl *D = ImplicitlyDefineFunction(NameLoc, *II, S); |
| 1767 | if (D) R.addDecl(D); |
| 1768 | } |
| 1769 | |
| 1770 | // If this name wasn't predeclared and if this is not a function |
| 1771 | // call, diagnose the problem. |
| 1772 | if (R.empty()) { |
Francois Pichet | fce1a3a | 2011-09-24 10:38:05 +0000 | [diff] [blame] | 1773 | |
| 1774 | // In Microsoft mode, if we are inside a template class member function |
| 1775 | // and we can't resolve an identifier then assume the identifier is type |
| 1776 | // dependent. The goal is to postpone name lookup to instantiation time |
| 1777 | // to be able to search into type dependent base classes. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1778 | if (getLangOpts().MicrosoftMode && CurContext->isDependentContext() && |
Francois Pichet | fce1a3a | 2011-09-24 10:38:05 +0000 | [diff] [blame] | 1779 | isa<CXXMethodDecl>(CurContext)) |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1780 | return ActOnDependentIdExpression(SS, TemplateKWLoc, NameInfo, |
| 1781 | IsAddressOfOperand, TemplateArgs); |
Francois Pichet | fce1a3a | 2011-09-24 10:38:05 +0000 | [diff] [blame] | 1782 | |
Kaelyn Uhrain | 4798f8d | 2012-01-18 05:58:54 +0000 | [diff] [blame] | 1783 | CorrectionCandidateCallback DefaultValidator; |
Kaelyn Uhrain | cd78e61 | 2012-01-25 20:49:08 +0000 | [diff] [blame] | 1784 | if (DiagnoseEmptyLookup(S, SS, R, CCC ? *CCC : DefaultValidator)) |
John McCall | 578b69b | 2009-12-16 08:11:27 +0000 | [diff] [blame] | 1785 | return ExprError(); |
| 1786 | |
| 1787 | assert(!R.empty() && |
| 1788 | "DiagnoseEmptyLookup returned false but added no results"); |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1789 | |
| 1790 | // If we found an Objective-C instance variable, let |
| 1791 | // LookupInObjCMethod build the appropriate expression to |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1792 | // reference the ivar. |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1793 | if (ObjCIvarDecl *Ivar = R.getAsSingle<ObjCIvarDecl>()) { |
| 1794 | R.clear(); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1795 | ExprResult E(LookupInObjCMethod(R, S, Ivar->getIdentifier())); |
Fariborz Jahanian | bc2b91a | 2011-09-23 23:11:38 +0000 | [diff] [blame] | 1796 | // In a hopelessly buggy code, Objective-C instance variable |
| 1797 | // lookup fails and no expression will be built to reference it. |
| 1798 | if (!E.isInvalid() && !E.get()) |
| 1799 | return ExprError(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 1800 | return E; |
Douglas Gregor | f06cdae | 2010-01-03 18:01:57 +0000 | [diff] [blame] | 1801 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1802 | } |
| 1803 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1804 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1805 | // This is guaranteed from this point on. |
| 1806 | assert(!R.empty() || ADL); |
| 1807 | |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 1808 | // Check whether this might be a C++ implicit instance member access. |
John McCall | fb97e75 | 2010-08-24 22:52:39 +0000 | [diff] [blame] | 1809 | // C++ [class.mfct.non-static]p3: |
| 1810 | // When an id-expression that is not part of a class member access |
| 1811 | // syntax and not used to form a pointer to member is used in the |
| 1812 | // body of a non-static member function of class X, if name lookup |
| 1813 | // resolves the name in the id-expression to a non-static non-type |
| 1814 | // member of some class C, the id-expression is transformed into a |
| 1815 | // class member access expression using (*this) as the |
| 1816 | // postfix-expression to the left of the . operator. |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1817 | // |
| 1818 | // But we don't actually need to do this for '&' operands if R |
| 1819 | // resolved to a function or overloaded function set, because the |
| 1820 | // expression is ill-formed if it actually works out to be a |
| 1821 | // non-static member function: |
| 1822 | // |
| 1823 | // C++ [expr.ref]p4: |
| 1824 | // Otherwise, if E1.E2 refers to a non-static member function. . . |
| 1825 | // [t]he expression can be used only as the left-hand operand of a |
| 1826 | // member function call. |
| 1827 | // |
| 1828 | // There are other safeguards against such uses, but it's important |
| 1829 | // to get this right here so that we don't end up making a |
| 1830 | // spuriously dependent expression if we're inside a dependent |
| 1831 | // instance method. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 1832 | if (!R.empty() && (*R.begin())->isCXXClassMember()) { |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1833 | bool MightBeImplicitMember; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 1834 | if (!IsAddressOfOperand) |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1835 | MightBeImplicitMember = true; |
| 1836 | else if (!SS.isEmpty()) |
| 1837 | MightBeImplicitMember = false; |
| 1838 | else if (R.isOverloadedResult()) |
| 1839 | MightBeImplicitMember = false; |
Douglas Gregor | e2248be | 2010-08-30 16:00:47 +0000 | [diff] [blame] | 1840 | else if (R.isUnresolvableResult()) |
| 1841 | MightBeImplicitMember = true; |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1842 | else |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 1843 | MightBeImplicitMember = isa<FieldDecl>(R.getFoundDecl()) || |
| 1844 | isa<IndirectFieldDecl>(R.getFoundDecl()); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 1845 | |
| 1846 | if (MightBeImplicitMember) |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1847 | return BuildPossibleImplicitMemberExpr(SS, TemplateKWLoc, |
| 1848 | R, TemplateArgs); |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1849 | } |
| 1850 | |
Abramo Bagnara | 9d9922a | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 1851 | if (TemplateArgs || TemplateKWLoc.isValid()) |
| 1852 | return BuildTemplateIdExpr(SS, TemplateKWLoc, R, ADL, TemplateArgs); |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 1853 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1854 | return BuildDeclarationNameExpr(SS, R, ADL); |
| 1855 | } |
| 1856 | |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 1857 | /// BuildQualifiedDeclarationNameExpr - Build a C++ qualified |
| 1858 | /// declaration name, generally during template instantiation. |
| 1859 | /// There's a large number of things which don't need to be done along |
| 1860 | /// this path. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1861 | ExprResult |
Jeffrey Yasskin | 9ab1454 | 2010-04-08 16:38:48 +0000 | [diff] [blame] | 1862 | Sema::BuildQualifiedDeclarationNameExpr(CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1863 | const DeclarationNameInfo &NameInfo) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1864 | DeclContext *DC; |
Douglas Gregor | e6ec5c4 | 2010-04-28 07:04:26 +0000 | [diff] [blame] | 1865 | if (!(DC = computeDeclContext(SS, false)) || DC->isDependentContext()) |
Abramo Bagnara | 9d9922a | 2012-02-06 14:31:00 +0000 | [diff] [blame] | 1866 | return BuildDependentDeclRefExpr(SS, /*TemplateKWLoc=*/SourceLocation(), |
| 1867 | NameInfo, /*TemplateArgs=*/0); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1868 | |
John McCall | 77bb1aa | 2010-05-01 00:40:08 +0000 | [diff] [blame] | 1869 | if (RequireCompleteDeclContext(SS, DC)) |
Douglas Gregor | e6ec5c4 | 2010-04-28 07:04:26 +0000 | [diff] [blame] | 1870 | return ExprError(); |
| 1871 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1872 | LookupResult R(*this, NameInfo, LookupOrdinaryName); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1873 | LookupQualifiedName(R, DC); |
| 1874 | |
| 1875 | if (R.isAmbiguous()) |
| 1876 | return ExprError(); |
| 1877 | |
| 1878 | if (R.empty()) { |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 1879 | Diag(NameInfo.getLoc(), diag::err_no_member) |
| 1880 | << NameInfo.getName() << DC << SS.getRange(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1881 | return ExprError(); |
| 1882 | } |
| 1883 | |
| 1884 | return BuildDeclarationNameExpr(SS, R, /*ADL*/ false); |
| 1885 | } |
| 1886 | |
| 1887 | /// LookupInObjCMethod - The parser has read a name in, and Sema has |
| 1888 | /// detected that we're currently inside an ObjC method. Perform some |
| 1889 | /// additional lookup. |
| 1890 | /// |
| 1891 | /// Ideally, most of this would be done by lookup, but there's |
| 1892 | /// actually quite a lot of extra work involved. |
| 1893 | /// |
| 1894 | /// Returns a null sentinel to indicate trivial success. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 1895 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1896 | Sema::LookupInObjCMethod(LookupResult &Lookup, Scope *S, |
Chris Lattner | eb483eb | 2010-04-11 08:28:14 +0000 | [diff] [blame] | 1897 | IdentifierInfo *II, bool AllowBuiltinCreation) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1898 | SourceLocation Loc = Lookup.getNameLoc(); |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1899 | ObjCMethodDecl *CurMethod = getCurMethodDecl(); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 1900 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1901 | // There are two cases to handle here. 1) scoped lookup could have failed, |
| 1902 | // in which case we should look for an ivar. 2) scoped lookup could have |
| 1903 | // found a decl, but that decl is outside the current instance method (i.e. |
| 1904 | // a global variable). In these two cases, we do a lookup for an ivar with |
| 1905 | // this name, if the lookup sucedes, we replace it our current decl. |
| 1906 | |
| 1907 | // If we're in a class method, we don't normally want to look for |
| 1908 | // ivars. But if we don't find anything else, and there's an |
| 1909 | // ivar, that's an error. |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1910 | bool IsClassMethod = CurMethod->isClassMethod(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1911 | |
| 1912 | bool LookForIvars; |
| 1913 | if (Lookup.empty()) |
| 1914 | LookForIvars = true; |
| 1915 | else if (IsClassMethod) |
| 1916 | LookForIvars = false; |
| 1917 | else |
| 1918 | LookForIvars = (Lookup.isSingleResult() && |
| 1919 | Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod()); |
Fariborz Jahanian | 412e798 | 2010-02-09 19:31:38 +0000 | [diff] [blame] | 1920 | ObjCInterfaceDecl *IFace = 0; |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1921 | if (LookForIvars) { |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1922 | IFace = CurMethod->getClassInterface(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1923 | ObjCInterfaceDecl *ClassDeclared; |
Argyrios Kyrtzidis | 7c81c2a | 2011-10-19 02:25:16 +0000 | [diff] [blame] | 1924 | ObjCIvarDecl *IV = 0; |
| 1925 | if (IFace && (IV = IFace->lookupInstanceVariable(II, ClassDeclared))) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1926 | // Diagnose using an ivar in a class method. |
| 1927 | if (IsClassMethod) |
| 1928 | return ExprError(Diag(Loc, diag::error_ivar_use_in_class_method) |
| 1929 | << IV->getDeclName()); |
| 1930 | |
| 1931 | // If we're referencing an invalid decl, just return this as a silent |
| 1932 | // error node. The error diagnostic was already emitted on the decl. |
| 1933 | if (IV->isInvalidDecl()) |
| 1934 | return ExprError(); |
| 1935 | |
| 1936 | // Check if referencing a field with __attribute__((deprecated)). |
| 1937 | if (DiagnoseUseOfDecl(IV, Loc)) |
| 1938 | return ExprError(); |
| 1939 | |
| 1940 | // Diagnose the use of an ivar outside of the declaring class. |
| 1941 | if (IV->getAccessControl() == ObjCIvarDecl::Private && |
Fariborz Jahanian | 458a7fb | 2012-03-07 00:58:41 +0000 | [diff] [blame] | 1942 | !declaresSameEntity(ClassDeclared, IFace) && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1943 | !getLangOpts().DebuggerSupport) |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1944 | Diag(Loc, diag::error_private_ivar_access) << IV->getDeclName(); |
| 1945 | |
| 1946 | // FIXME: This should use a new expr for a direct reference, don't |
| 1947 | // turn this into Self->ivar, just return a BareIVarExpr or something. |
| 1948 | IdentifierInfo &II = Context.Idents.get("self"); |
| 1949 | UnqualifiedId SelfName; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 1950 | SelfName.setIdentifier(&II, SourceLocation()); |
Fariborz Jahanian | 98a5403 | 2011-07-12 17:16:56 +0000 | [diff] [blame] | 1951 | SelfName.setKind(UnqualifiedId::IK_ImplicitSelfParam); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1952 | CXXScopeSpec SelfScopeSpec; |
Abramo Bagnara | e4b9276 | 2012-01-27 09:46:47 +0000 | [diff] [blame] | 1953 | SourceLocation TemplateKWLoc; |
| 1954 | ExprResult SelfExpr = ActOnIdExpression(S, SelfScopeSpec, TemplateKWLoc, |
Douglas Gregor | e45bb6a | 2010-09-22 16:33:13 +0000 | [diff] [blame] | 1955 | SelfName, false, false); |
| 1956 | if (SelfExpr.isInvalid()) |
| 1957 | return ExprError(); |
| 1958 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1959 | SelfExpr = DefaultLvalueConversion(SelfExpr.take()); |
| 1960 | if (SelfExpr.isInvalid()) |
| 1961 | return ExprError(); |
John McCall | 409fa9a | 2010-12-06 20:48:59 +0000 | [diff] [blame] | 1962 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 1963 | MarkAnyDeclReferenced(Loc, IV); |
Fariborz Jahanian | ed6662d | 2012-08-08 16:41:04 +0000 | [diff] [blame] | 1964 | |
| 1965 | ObjCMethodFamily MF = CurMethod->getMethodFamily(); |
| 1966 | if (MF != OMF_init && MF != OMF_dealloc && MF != OMF_finalize) |
| 1967 | Diag(Loc, diag::warn_direct_ivar_access) << IV->getDeclName(); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1968 | return Owned(new (Context) |
| 1969 | ObjCIvarRefExpr(IV, IV->getType(), Loc, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 1970 | SelfExpr.take(), true, true)); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1971 | } |
Chris Lattner | aec43db | 2010-04-12 05:10:17 +0000 | [diff] [blame] | 1972 | } else if (CurMethod->isInstanceMethod()) { |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1973 | // We should warn if a local variable hides an ivar. |
Fariborz Jahanian | 90f7b62 | 2011-11-08 22:51:27 +0000 | [diff] [blame] | 1974 | if (ObjCInterfaceDecl *IFace = CurMethod->getClassInterface()) { |
| 1975 | ObjCInterfaceDecl *ClassDeclared; |
| 1976 | if (ObjCIvarDecl *IV = IFace->lookupInstanceVariable(II, ClassDeclared)) { |
| 1977 | if (IV->getAccessControl() != ObjCIvarDecl::Private || |
Douglas Gregor | 60ef308 | 2011-12-15 00:29:59 +0000 | [diff] [blame] | 1978 | declaresSameEntity(IFace, ClassDeclared)) |
Fariborz Jahanian | 90f7b62 | 2011-11-08 22:51:27 +0000 | [diff] [blame] | 1979 | Diag(Loc, diag::warn_ivar_use_hidden) << IV->getDeclName(); |
| 1980 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1981 | } |
Fariborz Jahanian | b5ea9db | 2011-12-20 22:21:08 +0000 | [diff] [blame] | 1982 | } else if (Lookup.isSingleResult() && |
| 1983 | Lookup.getFoundDecl()->isDefinedOutsideFunctionOrMethod()) { |
| 1984 | // If accessing a stand-alone ivar in a class method, this is an error. |
| 1985 | if (const ObjCIvarDecl *IV = dyn_cast<ObjCIvarDecl>(Lookup.getFoundDecl())) |
| 1986 | return ExprError(Diag(Loc, diag::error_ivar_use_in_class_method) |
| 1987 | << IV->getDeclName()); |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 1988 | } |
| 1989 | |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1990 | if (Lookup.empty() && II && AllowBuiltinCreation) { |
| 1991 | // FIXME. Consolidate this with similar code in LookupName. |
| 1992 | if (unsigned BuiltinID = II->getBuiltinID()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 1993 | if (!(getLangOpts().CPlusPlus && |
Fariborz Jahanian | 48c2d56 | 2010-01-12 23:58:59 +0000 | [diff] [blame] | 1994 | Context.BuiltinInfo.isPredefinedLibFunction(BuiltinID))) { |
| 1995 | NamedDecl *D = LazilyCreateBuiltin((IdentifierInfo *)II, BuiltinID, |
| 1996 | S, Lookup.isForRedeclaration(), |
| 1997 | Lookup.getNameLoc()); |
| 1998 | if (D) Lookup.addDecl(D); |
| 1999 | } |
| 2000 | } |
| 2001 | } |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2002 | // Sentinel value saying that we didn't do anything special. |
| 2003 | return Owned((Expr*) 0); |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 2004 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2005 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2006 | /// \brief Cast a base object to a member's actual type. |
| 2007 | /// |
| 2008 | /// Logically this happens in three phases: |
| 2009 | /// |
| 2010 | /// * First we cast from the base type to the naming class. |
| 2011 | /// The naming class is the class into which we were looking |
| 2012 | /// when we found the member; it's the qualifier type if a |
| 2013 | /// qualifier was provided, and otherwise it's the base type. |
| 2014 | /// |
| 2015 | /// * Next we cast from the naming class to the declaring class. |
| 2016 | /// If the member we found was brought into a class's scope by |
| 2017 | /// a using declaration, this is that class; otherwise it's |
| 2018 | /// the class declaring the member. |
| 2019 | /// |
| 2020 | /// * Finally we cast from the declaring class to the "true" |
| 2021 | /// declaring class of the member. This conversion does not |
| 2022 | /// obey access control. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2023 | ExprResult |
| 2024 | Sema::PerformObjectMemberConversion(Expr *From, |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2025 | NestedNameSpecifier *Qualifier, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2026 | NamedDecl *FoundDecl, |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2027 | NamedDecl *Member) { |
| 2028 | CXXRecordDecl *RD = dyn_cast<CXXRecordDecl>(Member->getDeclContext()); |
| 2029 | if (!RD) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2030 | return Owned(From); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2031 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2032 | QualType DestRecordType; |
| 2033 | QualType DestType; |
| 2034 | QualType FromRecordType; |
| 2035 | QualType FromType = From->getType(); |
| 2036 | bool PointerConversions = false; |
| 2037 | if (isa<FieldDecl>(Member)) { |
| 2038 | DestRecordType = Context.getCanonicalType(Context.getTypeDeclType(RD)); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2039 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2040 | if (FromType->getAs<PointerType>()) { |
| 2041 | DestType = Context.getPointerType(DestRecordType); |
| 2042 | FromRecordType = FromType->getPointeeType(); |
| 2043 | PointerConversions = true; |
| 2044 | } else { |
| 2045 | DestType = DestRecordType; |
| 2046 | FromRecordType = FromType; |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 2047 | } |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2048 | } else if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(Member)) { |
| 2049 | if (Method->isStatic()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2050 | return Owned(From); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2051 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2052 | DestType = Method->getThisType(Context); |
| 2053 | DestRecordType = DestType->getPointeeType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2054 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2055 | if (FromType->getAs<PointerType>()) { |
| 2056 | FromRecordType = FromType->getPointeeType(); |
| 2057 | PointerConversions = true; |
| 2058 | } else { |
| 2059 | FromRecordType = FromType; |
| 2060 | DestType = DestRecordType; |
| 2061 | } |
| 2062 | } else { |
| 2063 | // No conversion necessary. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2064 | return Owned(From); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2065 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2066 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2067 | if (DestType->isDependentType() || FromType->isDependentType()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2068 | return Owned(From); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2069 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2070 | // If the unqualified types are the same, no conversion is necessary. |
| 2071 | if (Context.hasSameUnqualifiedType(FromRecordType, DestRecordType)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2072 | return Owned(From); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2073 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2074 | SourceRange FromRange = From->getSourceRange(); |
| 2075 | SourceLocation FromLoc = FromRange.getBegin(); |
| 2076 | |
Eli Friedman | c1c0dfb | 2011-09-27 21:58:52 +0000 | [diff] [blame] | 2077 | ExprValueKind VK = From->getValueKind(); |
Sebastian Redl | 906082e | 2010-07-20 04:20:21 +0000 | [diff] [blame] | 2078 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2079 | // C++ [class.member.lookup]p8: |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2080 | // [...] Ambiguities can often be resolved by qualifying a name with its |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2081 | // class name. |
| 2082 | // |
| 2083 | // If the member was a qualified name and the qualified referred to a |
| 2084 | // specific base subobject type, we'll cast to that intermediate type |
| 2085 | // first and then to the object in which the member is declared. That allows |
| 2086 | // one to resolve ambiguities in, e.g., a diamond-shaped hierarchy such as: |
| 2087 | // |
| 2088 | // class Base { public: int x; }; |
| 2089 | // class Derived1 : public Base { }; |
| 2090 | // class Derived2 : public Base { }; |
| 2091 | // class VeryDerived : public Derived1, public Derived2 { void f(); }; |
| 2092 | // |
| 2093 | // void VeryDerived::f() { |
| 2094 | // x = 17; // error: ambiguous base subobjects |
| 2095 | // Derived1::x = 17; // okay, pick the Base subobject of Derived1 |
| 2096 | // } |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2097 | if (Qualifier) { |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2098 | QualType QType = QualType(Qualifier->getAsType(), 0); |
| 2099 | assert(!QType.isNull() && "lookup done with dependent qualifier?"); |
| 2100 | assert(QType->isRecordType() && "lookup done with non-record type"); |
| 2101 | |
| 2102 | QualType QRecordType = QualType(QType->getAs<RecordType>(), 0); |
| 2103 | |
| 2104 | // In C++98, the qualifier type doesn't actually have to be a base |
| 2105 | // type of the object type, in which case we just ignore it. |
| 2106 | // Otherwise build the appropriate casts. |
| 2107 | if (IsDerivedFrom(FromRecordType, QRecordType)) { |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2108 | CXXCastPath BasePath; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2109 | if (CheckDerivedToBaseConversion(FromRecordType, QRecordType, |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2110 | FromLoc, FromRange, &BasePath)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2111 | return ExprError(); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2112 | |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2113 | if (PointerConversions) |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2114 | QType = Context.getPointerType(QType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2115 | From = ImpCastExprToType(From, QType, CK_UncheckedDerivedToBase, |
| 2116 | VK, &BasePath).take(); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2117 | |
| 2118 | FromType = QType; |
| 2119 | FromRecordType = QRecordType; |
| 2120 | |
| 2121 | // If the qualifier type was the same as the destination type, |
| 2122 | // we're done. |
| 2123 | if (Context.hasSameUnqualifiedType(FromRecordType, DestRecordType)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2124 | return Owned(From); |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2125 | } |
| 2126 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2127 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2128 | bool IgnoreAccess = false; |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2129 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2130 | // If we actually found the member through a using declaration, cast |
| 2131 | // down to the using declaration's type. |
| 2132 | // |
| 2133 | // Pointer equality is fine here because only one declaration of a |
| 2134 | // class ever has member declarations. |
| 2135 | if (FoundDecl->getDeclContext() != Member->getDeclContext()) { |
| 2136 | assert(isa<UsingShadowDecl>(FoundDecl)); |
| 2137 | QualType URecordType = Context.getTypeDeclType( |
| 2138 | cast<CXXRecordDecl>(FoundDecl->getDeclContext())); |
| 2139 | |
| 2140 | // We only need to do this if the naming-class to declaring-class |
| 2141 | // conversion is non-trivial. |
| 2142 | if (!Context.hasSameUnqualifiedType(FromRecordType, URecordType)) { |
| 2143 | assert(IsDerivedFrom(FromRecordType, URecordType)); |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2144 | CXXCastPath BasePath; |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2145 | if (CheckDerivedToBaseConversion(FromRecordType, URecordType, |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2146 | FromLoc, FromRange, &BasePath)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2147 | return ExprError(); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 2148 | |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2149 | QualType UType = URecordType; |
| 2150 | if (PointerConversions) |
| 2151 | UType = Context.getPointerType(UType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2152 | From = ImpCastExprToType(From, UType, CK_UncheckedDerivedToBase, |
| 2153 | VK, &BasePath).take(); |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2154 | FromType = UType; |
| 2155 | FromRecordType = URecordType; |
| 2156 | } |
| 2157 | |
| 2158 | // We don't do access control for the conversion from the |
| 2159 | // declaring class to the true declaring class. |
| 2160 | IgnoreAccess = true; |
Douglas Gregor | 5fccd36 | 2010-03-03 23:55:11 +0000 | [diff] [blame] | 2161 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2162 | |
John McCall | f871d0c | 2010-08-07 06:22:56 +0000 | [diff] [blame] | 2163 | CXXCastPath BasePath; |
Anders Carlsson | cee2242 | 2010-04-24 19:22:20 +0000 | [diff] [blame] | 2164 | if (CheckDerivedToBaseConversion(FromRecordType, DestRecordType, |
| 2165 | FromLoc, FromRange, &BasePath, |
John McCall | 6bb8017 | 2010-03-30 21:47:33 +0000 | [diff] [blame] | 2166 | IgnoreAccess)) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2167 | return ExprError(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2168 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2169 | return ImpCastExprToType(From, DestType, CK_UncheckedDerivedToBase, |
| 2170 | VK, &BasePath); |
Fariborz Jahanian | 98a541e | 2009-07-29 18:40:24 +0000 | [diff] [blame] | 2171 | } |
Douglas Gregor | 751f9a4 | 2009-06-30 15:47:41 +0000 | [diff] [blame] | 2172 | |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2173 | bool Sema::UseArgumentDependentLookup(const CXXScopeSpec &SS, |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2174 | const LookupResult &R, |
| 2175 | bool HasTrailingLParen) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2176 | // Only when used directly as the postfix-expression of a call. |
| 2177 | if (!HasTrailingLParen) |
| 2178 | return false; |
| 2179 | |
| 2180 | // Never if a scope specifier was provided. |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2181 | if (SS.isSet()) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2182 | return false; |
| 2183 | |
| 2184 | // Only in C++ or ObjC++. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2185 | if (!getLangOpts().CPlusPlus) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2186 | return false; |
| 2187 | |
| 2188 | // Turn off ADL when we find certain kinds of declarations during |
| 2189 | // normal lookup: |
| 2190 | for (LookupResult::iterator I = R.begin(), E = R.end(); I != E; ++I) { |
| 2191 | NamedDecl *D = *I; |
| 2192 | |
| 2193 | // C++0x [basic.lookup.argdep]p3: |
| 2194 | // -- a declaration of a class member |
| 2195 | // Since using decls preserve this property, we check this on the |
| 2196 | // original decl. |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 2197 | if (D->isCXXClassMember()) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2198 | return false; |
| 2199 | |
| 2200 | // C++0x [basic.lookup.argdep]p3: |
| 2201 | // -- a block-scope function declaration that is not a |
| 2202 | // using-declaration |
| 2203 | // NOTE: we also trigger this for function templates (in fact, we |
| 2204 | // don't check the decl type at all, since all other decl types |
| 2205 | // turn off ADL anyway). |
| 2206 | if (isa<UsingShadowDecl>(D)) |
| 2207 | D = cast<UsingShadowDecl>(D)->getTargetDecl(); |
| 2208 | else if (D->getDeclContext()->isFunctionOrMethod()) |
| 2209 | return false; |
| 2210 | |
| 2211 | // C++0x [basic.lookup.argdep]p3: |
| 2212 | // -- a declaration that is neither a function or a function |
| 2213 | // template |
| 2214 | // And also for builtin functions. |
| 2215 | if (isa<FunctionDecl>(D)) { |
| 2216 | FunctionDecl *FDecl = cast<FunctionDecl>(D); |
| 2217 | |
| 2218 | // But also builtin functions. |
| 2219 | if (FDecl->getBuiltinID() && FDecl->isImplicit()) |
| 2220 | return false; |
| 2221 | } else if (!isa<FunctionTemplateDecl>(D)) |
| 2222 | return false; |
| 2223 | } |
| 2224 | |
| 2225 | return true; |
| 2226 | } |
| 2227 | |
| 2228 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2229 | /// Diagnoses obvious problems with the use of the given declaration |
| 2230 | /// as an expression. This is only actually called for lookups that |
| 2231 | /// were not overloaded, and it doesn't promise that the declaration |
| 2232 | /// will in fact be used. |
| 2233 | static bool CheckDeclInExpr(Sema &S, SourceLocation Loc, NamedDecl *D) { |
Richard Smith | 162e1c1 | 2011-04-15 14:24:37 +0000 | [diff] [blame] | 2234 | if (isa<TypedefNameDecl>(D)) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2235 | S.Diag(Loc, diag::err_unexpected_typedef) << D->getDeclName(); |
| 2236 | return true; |
| 2237 | } |
| 2238 | |
| 2239 | if (isa<ObjCInterfaceDecl>(D)) { |
| 2240 | S.Diag(Loc, diag::err_unexpected_interface) << D->getDeclName(); |
| 2241 | return true; |
| 2242 | } |
| 2243 | |
| 2244 | if (isa<NamespaceDecl>(D)) { |
| 2245 | S.Diag(Loc, diag::err_unexpected_namespace) << D->getDeclName(); |
| 2246 | return true; |
| 2247 | } |
| 2248 | |
| 2249 | return false; |
| 2250 | } |
| 2251 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2252 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2253 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2254 | LookupResult &R, |
| 2255 | bool NeedsADL) { |
John McCall | fead20c | 2009-12-08 22:45:53 +0000 | [diff] [blame] | 2256 | // If this is a single, fully-resolved result and we don't need ADL, |
| 2257 | // just build an ordinary singleton decl ref. |
Douglas Gregor | 86b8e09 | 2010-01-29 17:15:43 +0000 | [diff] [blame] | 2258 | if (!NeedsADL && R.isSingleResult() && !R.getAsSingle<FunctionTemplateDecl>()) |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2259 | return BuildDeclarationNameExpr(SS, R.getLookupNameInfo(), |
| 2260 | R.getFoundDecl()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2261 | |
| 2262 | // We only need to check the declaration if there's exactly one |
| 2263 | // result, because in the overloaded case the results can only be |
| 2264 | // functions and function templates. |
John McCall | 5b3f913 | 2009-11-22 01:44:31 +0000 | [diff] [blame] | 2265 | if (R.isSingleResult() && |
| 2266 | CheckDeclInExpr(*this, R.getNameLoc(), R.getFoundDecl())) |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2267 | return ExprError(); |
| 2268 | |
John McCall | c373d48 | 2010-01-27 01:50:18 +0000 | [diff] [blame] | 2269 | // Otherwise, just build an unresolved lookup expression. Suppress |
| 2270 | // any lookup-related diagnostics; we'll hash these out later, when |
| 2271 | // we've picked a target. |
| 2272 | R.suppressDiagnostics(); |
| 2273 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2274 | UnresolvedLookupExpr *ULE |
Douglas Gregor | bebbe0d | 2010-12-15 01:34:56 +0000 | [diff] [blame] | 2275 | = UnresolvedLookupExpr::Create(Context, R.getNamingClass(), |
Douglas Gregor | 4c9be89 | 2011-02-28 20:01:57 +0000 | [diff] [blame] | 2276 | SS.getWithLocInContext(Context), |
| 2277 | R.getLookupNameInfo(), |
Douglas Gregor | 5a84dec | 2010-05-23 18:57:34 +0000 | [diff] [blame] | 2278 | NeedsADL, R.isOverloadedResult(), |
| 2279 | R.begin(), R.end()); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2280 | |
| 2281 | return Owned(ULE); |
| 2282 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2283 | |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2284 | /// \brief Complete semantic analysis for a reference to the given declaration. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 2285 | ExprResult |
John McCall | f7a1a74 | 2009-11-24 19:00:30 +0000 | [diff] [blame] | 2286 | Sema::BuildDeclarationNameExpr(const CXXScopeSpec &SS, |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2287 | const DeclarationNameInfo &NameInfo, |
| 2288 | NamedDecl *D) { |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2289 | assert(D && "Cannot refer to a NULL declaration"); |
John McCall | 7453ed4 | 2009-11-22 00:44:51 +0000 | [diff] [blame] | 2290 | assert(!isa<FunctionTemplateDecl>(D) && |
| 2291 | "Cannot refer unambiguously to a function template"); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2292 | |
Abramo Bagnara | 2577743 | 2010-08-11 22:01:17 +0000 | [diff] [blame] | 2293 | SourceLocation Loc = NameInfo.getLoc(); |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2294 | if (CheckDeclInExpr(*this, Loc, D)) |
| 2295 | return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2296 | |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2297 | if (TemplateDecl *Template = dyn_cast<TemplateDecl>(D)) { |
| 2298 | // Specifically diagnose references to class templates that are missing |
| 2299 | // a template argument list. |
| 2300 | Diag(Loc, diag::err_template_decl_ref) |
| 2301 | << Template << SS.getRange(); |
| 2302 | Diag(Template->getLocation(), diag::note_template_decl_here); |
| 2303 | return ExprError(); |
| 2304 | } |
| 2305 | |
| 2306 | // Make sure that we're referring to a value. |
| 2307 | ValueDecl *VD = dyn_cast<ValueDecl>(D); |
| 2308 | if (!VD) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 2309 | Diag(Loc, diag::err_ref_non_value) |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2310 | << D << SS.getRange(); |
John McCall | 87cf670 | 2009-12-18 18:35:10 +0000 | [diff] [blame] | 2311 | Diag(D->getLocation(), diag::note_declared_at); |
Douglas Gregor | 9af2f52 | 2009-12-01 16:58:18 +0000 | [diff] [blame] | 2312 | return ExprError(); |
| 2313 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2314 | |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2315 | // Check whether this declaration can be used. Note that we suppress |
| 2316 | // this check when we're going to perform argument-dependent lookup |
| 2317 | // on this function name, because this might not be the function |
| 2318 | // that overload resolution actually selects. |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 2319 | if (DiagnoseUseOfDecl(VD, Loc)) |
Douglas Gregor | 48f3bb9 | 2009-02-18 21:56:37 +0000 | [diff] [blame] | 2320 | return ExprError(); |
| 2321 | |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 2322 | // Only create DeclRefExpr's for valid Decl's. |
| 2323 | if (VD->isInvalidDecl()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2324 | return ExprError(); |
| 2325 | |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 2326 | // Handle members of anonymous structs and unions. If we got here, |
| 2327 | // and the reference is to a class member indirect field, then this |
| 2328 | // must be the subject of a pointer-to-member expression. |
| 2329 | if (IndirectFieldDecl *indirectField = dyn_cast<IndirectFieldDecl>(VD)) |
| 2330 | if (!indirectField->isCXXClassMember()) |
| 2331 | return BuildAnonymousStructUnionMemberReference(SS, NameInfo.getLoc(), |
| 2332 | indirectField); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 2333 | |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 2334 | { |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2335 | QualType type = VD->getType(); |
Daniel Dunbar | b20de81 | 2011-02-10 18:29:28 +0000 | [diff] [blame] | 2336 | ExprValueKind valueKind = VK_RValue; |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2337 | |
| 2338 | switch (D->getKind()) { |
| 2339 | // Ignore all the non-ValueDecl kinds. |
| 2340 | #define ABSTRACT_DECL(kind) |
| 2341 | #define VALUE(type, base) |
| 2342 | #define DECL(type, base) \ |
| 2343 | case Decl::type: |
| 2344 | #include "clang/AST/DeclNodes.inc" |
| 2345 | llvm_unreachable("invalid value decl kind"); |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2346 | |
| 2347 | // These shouldn't make it here. |
| 2348 | case Decl::ObjCAtDefsField: |
| 2349 | case Decl::ObjCIvar: |
| 2350 | llvm_unreachable("forming non-member reference to ivar?"); |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2351 | |
| 2352 | // Enum constants are always r-values and never references. |
| 2353 | // Unresolved using declarations are dependent. |
| 2354 | case Decl::EnumConstant: |
| 2355 | case Decl::UnresolvedUsingValue: |
| 2356 | valueKind = VK_RValue; |
| 2357 | break; |
| 2358 | |
| 2359 | // Fields and indirect fields that got here must be for |
| 2360 | // pointer-to-member expressions; we just call them l-values for |
| 2361 | // internal consistency, because this subexpression doesn't really |
| 2362 | // exist in the high-level semantics. |
| 2363 | case Decl::Field: |
| 2364 | case Decl::IndirectField: |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2365 | assert(getLangOpts().CPlusPlus && |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2366 | "building reference to field in C?"); |
| 2367 | |
| 2368 | // These can't have reference type in well-formed programs, but |
| 2369 | // for internal consistency we do this anyway. |
| 2370 | type = type.getNonReferenceType(); |
| 2371 | valueKind = VK_LValue; |
| 2372 | break; |
| 2373 | |
| 2374 | // Non-type template parameters are either l-values or r-values |
| 2375 | // depending on the type. |
| 2376 | case Decl::NonTypeTemplateParm: { |
| 2377 | if (const ReferenceType *reftype = type->getAs<ReferenceType>()) { |
| 2378 | type = reftype->getPointeeType(); |
| 2379 | valueKind = VK_LValue; // even if the parameter is an r-value reference |
| 2380 | break; |
| 2381 | } |
| 2382 | |
| 2383 | // For non-references, we need to strip qualifiers just in case |
| 2384 | // the template parameter was declared as 'const int' or whatever. |
| 2385 | valueKind = VK_RValue; |
| 2386 | type = type.getUnqualifiedType(); |
| 2387 | break; |
| 2388 | } |
| 2389 | |
| 2390 | case Decl::Var: |
| 2391 | // In C, "extern void blah;" is valid and is an r-value. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2392 | if (!getLangOpts().CPlusPlus && |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2393 | !type.hasQualifiers() && |
| 2394 | type->isVoidType()) { |
| 2395 | valueKind = VK_RValue; |
| 2396 | break; |
| 2397 | } |
| 2398 | // fallthrough |
| 2399 | |
| 2400 | case Decl::ImplicitParam: |
Douglas Gregor | 6893284 | 2012-02-18 05:51:20 +0000 | [diff] [blame] | 2401 | case Decl::ParmVar: { |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2402 | // These are always l-values. |
| 2403 | valueKind = VK_LValue; |
| 2404 | type = type.getNonReferenceType(); |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 2405 | |
Douglas Gregor | 6893284 | 2012-02-18 05:51:20 +0000 | [diff] [blame] | 2406 | // FIXME: Does the addition of const really only apply in |
| 2407 | // potentially-evaluated contexts? Since the variable isn't actually |
| 2408 | // captured in an unevaluated context, it seems that the answer is no. |
David Blaikie | 71f55f7 | 2012-08-06 22:47:24 +0000 | [diff] [blame] | 2409 | if (!isUnevaluatedContext()) { |
Douglas Gregor | 6893284 | 2012-02-18 05:51:20 +0000 | [diff] [blame] | 2410 | QualType CapturedType = getCapturedDeclRefType(cast<VarDecl>(VD), Loc); |
| 2411 | if (!CapturedType.isNull()) |
| 2412 | type = CapturedType; |
| 2413 | } |
| 2414 | |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2415 | break; |
Douglas Gregor | 6893284 | 2012-02-18 05:51:20 +0000 | [diff] [blame] | 2416 | } |
| 2417 | |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2418 | case Decl::Function: { |
Eli Friedman | a6c66ce | 2012-08-31 00:14:07 +0000 | [diff] [blame^] | 2419 | if (unsigned BID = cast<FunctionDecl>(VD)->getBuiltinID()) { |
| 2420 | if (!Context.BuiltinInfo.isPredefinedLibFunction(BID)) { |
| 2421 | type = Context.BuiltinFnTy; |
| 2422 | valueKind = VK_RValue; |
| 2423 | break; |
| 2424 | } |
| 2425 | } |
| 2426 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2427 | const FunctionType *fty = type->castAs<FunctionType>(); |
| 2428 | |
| 2429 | // If we're referring to a function with an __unknown_anytype |
| 2430 | // result type, make the entire expression __unknown_anytype. |
| 2431 | if (fty->getResultType() == Context.UnknownAnyTy) { |
| 2432 | type = Context.UnknownAnyTy; |
| 2433 | valueKind = VK_RValue; |
| 2434 | break; |
| 2435 | } |
| 2436 | |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2437 | // Functions are l-values in C++. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2438 | if (getLangOpts().CPlusPlus) { |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2439 | valueKind = VK_LValue; |
| 2440 | break; |
| 2441 | } |
| 2442 | |
| 2443 | // C99 DR 316 says that, if a function type comes from a |
| 2444 | // function definition (without a prototype), that type is only |
| 2445 | // used for checking compatibility. Therefore, when referencing |
| 2446 | // the function, we pretend that we don't have the full function |
| 2447 | // type. |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2448 | if (!cast<FunctionDecl>(VD)->hasPrototype() && |
| 2449 | isa<FunctionProtoType>(fty)) |
| 2450 | type = Context.getFunctionNoProtoType(fty->getResultType(), |
| 2451 | fty->getExtInfo()); |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2452 | |
| 2453 | // Functions are r-values in C. |
| 2454 | valueKind = VK_RValue; |
| 2455 | break; |
| 2456 | } |
| 2457 | |
| 2458 | case Decl::CXXMethod: |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2459 | // If we're referring to a method with an __unknown_anytype |
| 2460 | // result type, make the entire expression __unknown_anytype. |
| 2461 | // This should only be possible with a type written directly. |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 2462 | if (const FunctionProtoType *proto |
| 2463 | = dyn_cast<FunctionProtoType>(VD->getType())) |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2464 | if (proto->getResultType() == Context.UnknownAnyTy) { |
| 2465 | type = Context.UnknownAnyTy; |
| 2466 | valueKind = VK_RValue; |
| 2467 | break; |
| 2468 | } |
| 2469 | |
John McCall | 76a4021 | 2011-02-09 01:13:10 +0000 | [diff] [blame] | 2470 | // C++ methods are l-values if static, r-values if non-static. |
| 2471 | if (cast<CXXMethodDecl>(VD)->isStatic()) { |
| 2472 | valueKind = VK_LValue; |
| 2473 | break; |
| 2474 | } |
| 2475 | // fallthrough |
| 2476 | |
| 2477 | case Decl::CXXConversion: |
| 2478 | case Decl::CXXDestructor: |
| 2479 | case Decl::CXXConstructor: |
| 2480 | valueKind = VK_RValue; |
| 2481 | break; |
| 2482 | } |
| 2483 | |
| 2484 | return BuildDeclRefExpr(VD, type, valueKind, NameInfo, &SS); |
| 2485 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2486 | } |
| 2487 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 2488 | ExprResult Sema::ActOnPredefinedExpr(SourceLocation Loc, tok::TokenKind Kind) { |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 2489 | PredefinedExpr::IdentType IT; |
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 | switch (Kind) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 2492 | default: llvm_unreachable("Unknown simple primary expr!"); |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 2493 | case tok::kw___func__: IT = PredefinedExpr::Func; break; // [C99 6.4.2.2] |
| 2494 | case tok::kw___FUNCTION__: IT = PredefinedExpr::Function; break; |
Nico Weber | 28ad063 | 2012-06-23 02:07:59 +0000 | [diff] [blame] | 2495 | case tok::kw_L__FUNCTION__: IT = PredefinedExpr::LFunction; break; |
Chris Lattner | d9f6910 | 2008-08-10 01:53:14 +0000 | [diff] [blame] | 2496 | case tok::kw___PRETTY_FUNCTION__: IT = PredefinedExpr::PrettyFunction; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2497 | } |
Chris Lattner | 1423ea4 | 2008-01-12 18:39:25 +0000 | [diff] [blame] | 2498 | |
Chris Lattner | fa28b30 | 2008-01-12 08:14:25 +0000 | [diff] [blame] | 2499 | // Pre-defined identifiers are of type char[x], where x is the length of the |
| 2500 | // string. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2501 | |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2502 | Decl *currentDecl = getCurFunctionOrMethodDecl(); |
Fariborz Jahanian | eb024ac | 2010-07-23 21:53:24 +0000 | [diff] [blame] | 2503 | if (!currentDecl && getCurBlock()) |
| 2504 | currentDecl = getCurBlock()->TheDecl; |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2505 | if (!currentDecl) { |
Chris Lattner | b0da923 | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 2506 | Diag(Loc, diag::ext_predef_outside_function); |
Anders Carlsson | 3a082d8 | 2009-09-08 18:24:21 +0000 | [diff] [blame] | 2507 | currentDecl = Context.getTranslationUnitDecl(); |
Chris Lattner | b0da923 | 2008-12-12 05:05:20 +0000 | [diff] [blame] | 2508 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2509 | |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2510 | QualType ResTy; |
| 2511 | if (cast<DeclContext>(currentDecl)->isDependentContext()) { |
| 2512 | ResTy = Context.DependentTy; |
| 2513 | } else { |
Anders Carlsson | 848fa64 | 2010-02-11 18:20:28 +0000 | [diff] [blame] | 2514 | unsigned Length = PredefinedExpr::ComputeName(IT, currentDecl).length(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2515 | |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2516 | llvm::APInt LengthI(32, Length + 1); |
Nico Weber | d68615f | 2012-06-29 16:39:58 +0000 | [diff] [blame] | 2517 | if (IT == PredefinedExpr::LFunction) |
Nico Weber | 28ad063 | 2012-06-23 02:07:59 +0000 | [diff] [blame] | 2518 | ResTy = Context.WCharTy.withConst(); |
| 2519 | else |
| 2520 | ResTy = Context.CharTy.withConst(); |
Anders Carlsson | 773f397 | 2009-09-11 01:22:35 +0000 | [diff] [blame] | 2521 | ResTy = Context.getConstantArrayType(ResTy, LengthI, ArrayType::Normal, 0); |
| 2522 | } |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2523 | return Owned(new (Context) PredefinedExpr(Loc, ResTy, IT)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2524 | } |
| 2525 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2526 | ExprResult Sema::ActOnCharacterConstant(const Token &Tok, Scope *UDLScope) { |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 2527 | SmallString<16> CharBuffer; |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2528 | bool Invalid = false; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 2529 | StringRef ThisTok = PP.getSpelling(Tok, CharBuffer, &Invalid); |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2530 | if (Invalid) |
| 2531 | return ExprError(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2532 | |
Benjamin Kramer | ddeea56 | 2010-02-27 13:44:12 +0000 | [diff] [blame] | 2533 | CharLiteralParser Literal(ThisTok.begin(), ThisTok.end(), Tok.getLocation(), |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2534 | PP, Tok.getKind()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2535 | if (Literal.hadError()) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2536 | return ExprError(); |
Chris Lattner | fc62bfd | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 2537 | |
Chris Lattner | e8337df | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 2538 | QualType Ty; |
Seth Cantrell | 79f0a82 | 2012-01-18 12:27:06 +0000 | [diff] [blame] | 2539 | if (Literal.isWide()) |
| 2540 | Ty = Context.WCharTy; // L'x' -> wchar_t in C and C++. |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2541 | else if (Literal.isUTF16()) |
Seth Cantrell | 79f0a82 | 2012-01-18 12:27:06 +0000 | [diff] [blame] | 2542 | Ty = Context.Char16Ty; // u'x' -> char16_t in C11 and C++11. |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2543 | else if (Literal.isUTF32()) |
Seth Cantrell | 79f0a82 | 2012-01-18 12:27:06 +0000 | [diff] [blame] | 2544 | Ty = Context.Char32Ty; // U'x' -> char32_t in C11 and C++11. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2545 | else if (!getLangOpts().CPlusPlus || Literal.isMultiChar()) |
Seth Cantrell | 79f0a82 | 2012-01-18 12:27:06 +0000 | [diff] [blame] | 2546 | Ty = Context.IntTy; // 'x' -> int in C, 'wxyz' -> int in C++. |
Chris Lattner | e8337df | 2009-12-30 21:19:39 +0000 | [diff] [blame] | 2547 | else |
| 2548 | Ty = Context.CharTy; // 'x' -> char in C++ |
Chris Lattner | fc62bfd | 2008-03-01 08:32:21 +0000 | [diff] [blame] | 2549 | |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 2550 | CharacterLiteral::CharacterKind Kind = CharacterLiteral::Ascii; |
| 2551 | if (Literal.isWide()) |
| 2552 | Kind = CharacterLiteral::Wide; |
| 2553 | else if (Literal.isUTF16()) |
| 2554 | Kind = CharacterLiteral::UTF16; |
| 2555 | else if (Literal.isUTF32()) |
| 2556 | Kind = CharacterLiteral::UTF32; |
| 2557 | |
Richard Smith | dd66be7 | 2012-03-08 01:34:56 +0000 | [diff] [blame] | 2558 | Expr *Lit = new (Context) CharacterLiteral(Literal.getValue(), Kind, Ty, |
| 2559 | Tok.getLocation()); |
| 2560 | |
| 2561 | if (Literal.getUDSuffix().empty()) |
| 2562 | return Owned(Lit); |
| 2563 | |
| 2564 | // We're building a user-defined literal. |
| 2565 | IdentifierInfo *UDSuffix = &Context.Idents.get(Literal.getUDSuffix()); |
| 2566 | SourceLocation UDSuffixLoc = |
| 2567 | getUDSuffixLoc(*this, Tok.getLocation(), Literal.getUDSuffixOffset()); |
| 2568 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2569 | // Make sure we're allowed user-defined literals here. |
| 2570 | if (!UDLScope) |
| 2571 | return ExprError(Diag(UDSuffixLoc, diag::err_invalid_character_udl)); |
| 2572 | |
Richard Smith | dd66be7 | 2012-03-08 01:34:56 +0000 | [diff] [blame] | 2573 | // C++11 [lex.ext]p6: The literal L is treated as a call of the form |
| 2574 | // operator "" X (ch) |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2575 | return BuildCookedLiteralOperatorCall(*this, UDLScope, UDSuffix, UDSuffixLoc, |
| 2576 | llvm::makeArrayRef(&Lit, 1), |
| 2577 | Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2578 | } |
| 2579 | |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 2580 | ExprResult Sema::ActOnIntegerConstant(SourceLocation Loc, uint64_t Val) { |
| 2581 | unsigned IntSize = Context.getTargetInfo().getIntWidth(); |
| 2582 | return Owned(IntegerLiteral::Create(Context, llvm::APInt(IntSize, Val), |
| 2583 | Context.IntTy, Loc)); |
| 2584 | } |
| 2585 | |
Richard Smith | b453ad3 | 2012-03-08 08:45:32 +0000 | [diff] [blame] | 2586 | static Expr *BuildFloatingLiteral(Sema &S, NumericLiteralParser &Literal, |
| 2587 | QualType Ty, SourceLocation Loc) { |
| 2588 | const llvm::fltSemantics &Format = S.Context.getFloatTypeSemantics(Ty); |
| 2589 | |
| 2590 | using llvm::APFloat; |
| 2591 | APFloat Val(Format); |
| 2592 | |
| 2593 | APFloat::opStatus result = Literal.GetFloatValue(Val); |
| 2594 | |
| 2595 | // Overflow is always an error, but underflow is only an error if |
| 2596 | // we underflowed to zero (APFloat reports denormals as underflow). |
| 2597 | if ((result & APFloat::opOverflow) || |
| 2598 | ((result & APFloat::opUnderflow) && Val.isZero())) { |
| 2599 | unsigned diagnostic; |
| 2600 | SmallString<20> buffer; |
| 2601 | if (result & APFloat::opOverflow) { |
| 2602 | diagnostic = diag::warn_float_overflow; |
| 2603 | APFloat::getLargest(Format).toString(buffer); |
| 2604 | } else { |
| 2605 | diagnostic = diag::warn_float_underflow; |
| 2606 | APFloat::getSmallest(Format).toString(buffer); |
| 2607 | } |
| 2608 | |
| 2609 | S.Diag(Loc, diagnostic) |
| 2610 | << Ty |
| 2611 | << StringRef(buffer.data(), buffer.size()); |
| 2612 | } |
| 2613 | |
| 2614 | bool isExact = (result == APFloat::opOK); |
| 2615 | return FloatingLiteral::Create(S.Context, Val, isExact, Ty, Loc); |
| 2616 | } |
| 2617 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2618 | ExprResult Sema::ActOnNumericConstant(const Token &Tok, Scope *UDLScope) { |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2619 | // Fast path for a single digit (which is quite common). A single digit |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2620 | // cannot have a trigraph, escaped newline, radix prefix, or suffix. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2621 | if (Tok.getLength() == 1) { |
Chris Lattner | 7216dc9 | 2009-01-26 22:36:52 +0000 | [diff] [blame] | 2622 | const char Val = PP.getSpellingOfSingleCharacterNumericConstant(Tok); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 2623 | return ActOnIntegerConstant(Tok.getLocation(), Val-'0'); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2624 | } |
Ted Kremenek | 2839660 | 2009-01-13 23:19:12 +0000 | [diff] [blame] | 2625 | |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 2626 | SmallString<512> IntegerBuffer; |
Chris Lattner | 2a29904 | 2008-09-30 20:53:45 +0000 | [diff] [blame] | 2627 | // Add padding so that NumericLiteralParser can overread by one character. |
| 2628 | IntegerBuffer.resize(Tok.getLength()+1); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2629 | const char *ThisTokBegin = &IntegerBuffer[0]; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2630 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2631 | // Get the spelling of the token, which eliminates trigraphs, etc. |
Douglas Gregor | 453091c | 2010-03-16 22:30:13 +0000 | [diff] [blame] | 2632 | bool Invalid = false; |
| 2633 | unsigned ActualLength = PP.getSpelling(Tok, ThisTokBegin, &Invalid); |
| 2634 | if (Invalid) |
| 2635 | return ExprError(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2636 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2637 | NumericLiteralParser Literal(ThisTokBegin, ThisTokBegin+ActualLength, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2638 | Tok.getLocation(), PP); |
| 2639 | if (Literal.hadError) |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2640 | return ExprError(); |
| 2641 | |
Richard Smith | b453ad3 | 2012-03-08 08:45:32 +0000 | [diff] [blame] | 2642 | if (Literal.hasUDSuffix()) { |
| 2643 | // We're building a user-defined literal. |
| 2644 | IdentifierInfo *UDSuffix = &Context.Idents.get(Literal.getUDSuffix()); |
| 2645 | SourceLocation UDSuffixLoc = |
| 2646 | getUDSuffixLoc(*this, Tok.getLocation(), Literal.getUDSuffixOffset()); |
| 2647 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2648 | // Make sure we're allowed user-defined literals here. |
| 2649 | if (!UDLScope) |
| 2650 | return ExprError(Diag(UDSuffixLoc, diag::err_invalid_numeric_udl)); |
Richard Smith | b453ad3 | 2012-03-08 08:45:32 +0000 | [diff] [blame] | 2651 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2652 | QualType CookedTy; |
Richard Smith | b453ad3 | 2012-03-08 08:45:32 +0000 | [diff] [blame] | 2653 | if (Literal.isFloatingLiteral()) { |
| 2654 | // C++11 [lex.ext]p4: If S contains a literal operator with parameter type |
| 2655 | // long double, the literal is treated as a call of the form |
| 2656 | // operator "" X (f L) |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2657 | CookedTy = Context.LongDoubleTy; |
Richard Smith | b453ad3 | 2012-03-08 08:45:32 +0000 | [diff] [blame] | 2658 | } else { |
| 2659 | // C++11 [lex.ext]p3: If S contains a literal operator with parameter type |
| 2660 | // unsigned long long, the literal is treated as a call of the form |
| 2661 | // operator "" X (n ULL) |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2662 | CookedTy = Context.UnsignedLongLongTy; |
Richard Smith | b453ad3 | 2012-03-08 08:45:32 +0000 | [diff] [blame] | 2663 | } |
| 2664 | |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2665 | DeclarationName OpName = |
| 2666 | Context.DeclarationNames.getCXXLiteralOperatorName(UDSuffix); |
| 2667 | DeclarationNameInfo OpNameInfo(OpName, UDSuffixLoc); |
| 2668 | OpNameInfo.setCXXLiteralOperatorNameLoc(UDSuffixLoc); |
| 2669 | |
| 2670 | // Perform literal operator lookup to determine if we're building a raw |
| 2671 | // literal or a cooked one. |
| 2672 | LookupResult R(*this, OpName, UDSuffixLoc, LookupOrdinaryName); |
| 2673 | switch (LookupLiteralOperator(UDLScope, R, llvm::makeArrayRef(&CookedTy, 1), |
| 2674 | /*AllowRawAndTemplate*/true)) { |
| 2675 | case LOLR_Error: |
| 2676 | return ExprError(); |
| 2677 | |
| 2678 | case LOLR_Cooked: { |
| 2679 | Expr *Lit; |
| 2680 | if (Literal.isFloatingLiteral()) { |
| 2681 | Lit = BuildFloatingLiteral(*this, Literal, CookedTy, Tok.getLocation()); |
| 2682 | } else { |
| 2683 | llvm::APInt ResultVal(Context.getTargetInfo().getLongLongWidth(), 0); |
| 2684 | if (Literal.GetIntegerValue(ResultVal)) |
| 2685 | Diag(Tok.getLocation(), diag::warn_integer_too_large); |
| 2686 | Lit = IntegerLiteral::Create(Context, ResultVal, CookedTy, |
| 2687 | Tok.getLocation()); |
| 2688 | } |
| 2689 | return BuildLiteralOperatorCall(R, OpNameInfo, |
| 2690 | llvm::makeArrayRef(&Lit, 1), |
| 2691 | Tok.getLocation()); |
| 2692 | } |
| 2693 | |
| 2694 | case LOLR_Raw: { |
| 2695 | // C++11 [lit.ext]p3, p4: If S contains a raw literal operator, the |
| 2696 | // literal is treated as a call of the form |
| 2697 | // operator "" X ("n") |
| 2698 | SourceLocation TokLoc = Tok.getLocation(); |
| 2699 | unsigned Length = Literal.getUDSuffixOffset(); |
| 2700 | QualType StrTy = Context.getConstantArrayType( |
| 2701 | Context.CharTy, llvm::APInt(32, Length + 1), |
| 2702 | ArrayType::Normal, 0); |
| 2703 | Expr *Lit = StringLiteral::Create( |
| 2704 | Context, StringRef(ThisTokBegin, Length), StringLiteral::Ascii, |
| 2705 | /*Pascal*/false, StrTy, &TokLoc, 1); |
| 2706 | return BuildLiteralOperatorCall(R, OpNameInfo, |
| 2707 | llvm::makeArrayRef(&Lit, 1), TokLoc); |
| 2708 | } |
| 2709 | |
| 2710 | case LOLR_Template: |
| 2711 | // C++11 [lit.ext]p3, p4: Otherwise (S contains a literal operator |
| 2712 | // template), L is treated as a call fo the form |
| 2713 | // operator "" X <'c1', 'c2', ... 'ck'>() |
| 2714 | // where n is the source character sequence c1 c2 ... ck. |
| 2715 | TemplateArgumentListInfo ExplicitArgs; |
| 2716 | unsigned CharBits = Context.getIntWidth(Context.CharTy); |
| 2717 | bool CharIsUnsigned = Context.CharTy->isUnsignedIntegerType(); |
| 2718 | llvm::APSInt Value(CharBits, CharIsUnsigned); |
| 2719 | for (unsigned I = 0, N = Literal.getUDSuffixOffset(); I != N; ++I) { |
| 2720 | Value = ThisTokBegin[I]; |
Benjamin Kramer | 8552437 | 2012-06-07 15:09:51 +0000 | [diff] [blame] | 2721 | TemplateArgument Arg(Context, Value, Context.CharTy); |
Richard Smith | 36f5cfe | 2012-03-09 08:00:36 +0000 | [diff] [blame] | 2722 | TemplateArgumentLocInfo ArgInfo; |
| 2723 | ExplicitArgs.addArgument(TemplateArgumentLoc(Arg, ArgInfo)); |
| 2724 | } |
| 2725 | return BuildLiteralOperatorCall(R, OpNameInfo, ArrayRef<Expr*>(), |
| 2726 | Tok.getLocation(), &ExplicitArgs); |
| 2727 | } |
| 2728 | |
| 2729 | llvm_unreachable("unexpected literal operator lookup result"); |
Richard Smith | b453ad3 | 2012-03-08 08:45:32 +0000 | [diff] [blame] | 2730 | } |
| 2731 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2732 | Expr *Res; |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2733 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2734 | if (Literal.isFloatingLiteral()) { |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2735 | QualType Ty; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2736 | if (Literal.isFloat) |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2737 | Ty = Context.FloatTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2738 | else if (!Literal.isLong) |
Chris Lattner | 525a050 | 2007-09-22 18:29:59 +0000 | [diff] [blame] | 2739 | Ty = Context.DoubleTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2740 | else |
Chris Lattner | 9e9b6dc | 2008-03-08 08:52:55 +0000 | [diff] [blame] | 2741 | Ty = Context.LongDoubleTy; |
Chris Lattner | b7cfe88 | 2008-06-30 18:32:54 +0000 | [diff] [blame] | 2742 | |
Richard Smith | b453ad3 | 2012-03-08 08:45:32 +0000 | [diff] [blame] | 2743 | Res = BuildFloatingLiteral(*this, Literal, Ty, Tok.getLocation()); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2744 | |
Peter Collingbourne | f4f7cb8 | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2745 | if (Ty == Context.DoubleTy) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2746 | if (getLangOpts().SinglePrecisionConstants) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2747 | Res = ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast).take(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2748 | } else if (getLangOpts().OpenCL && !getOpenCLOptions().cl_khr_fp64) { |
Peter Collingbourne | f4f7cb8 | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2749 | Diag(Tok.getLocation(), diag::warn_double_const_requires_fp64); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 2750 | Res = ImpCastExprToType(Res, Context.FloatTy, CK_FloatingCast).take(); |
Peter Collingbourne | f4f7cb8 | 2011-03-11 19:24:59 +0000 | [diff] [blame] | 2751 | } |
| 2752 | } |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2753 | } else if (!Literal.isIntegerLiteral()) { |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2754 | return ExprError(); |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2755 | } else { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2756 | QualType Ty; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2757 | |
Neil Booth | b944951 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 2758 | // long long is a C99 feature. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2759 | if (!getLangOpts().C99 && Literal.isLongLong) |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 2760 | Diag(Tok.getLocation(), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2761 | getLangOpts().CPlusPlus0x ? |
Richard Smith | ebaf0e6 | 2011-10-18 20:49:44 +0000 | [diff] [blame] | 2762 | diag::warn_cxx98_compat_longlong : diag::ext_longlong); |
Neil Booth | b944951 | 2007-08-29 22:00:19 +0000 | [diff] [blame] | 2763 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2764 | // Get the value in the widest-possible width. |
Stephen Canon | b9e05f1 | 2012-05-03 22:49:43 +0000 | [diff] [blame] | 2765 | unsigned MaxWidth = Context.getTargetInfo().getIntMaxTWidth(); |
| 2766 | // The microsoft literal suffix extensions support 128-bit literals, which |
| 2767 | // may be wider than [u]intmax_t. |
| 2768 | if (Literal.isMicrosoftInteger && MaxWidth < 128) |
| 2769 | MaxWidth = 128; |
| 2770 | llvm::APInt ResultVal(MaxWidth, 0); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2771 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2772 | if (Literal.GetIntegerValue(ResultVal)) { |
| 2773 | // If this value didn't fit into uintmax_t, warn and force to ull. |
| 2774 | Diag(Tok.getLocation(), diag::warn_integer_too_large); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2775 | Ty = Context.UnsignedLongLongTy; |
| 2776 | assert(Context.getTypeSize(Ty) == ResultVal.getBitWidth() && |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 2777 | "long long is not intmax_t?"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2778 | } else { |
| 2779 | // If this value fits into a ULL, try to figure out what else it fits into |
| 2780 | // according to the rules of C99 6.4.4.1p5. |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2781 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2782 | // Octal, Hexadecimal, and integers with a U suffix are allowed to |
| 2783 | // be an unsigned int. |
| 2784 | bool AllowUnsigned = Literal.isUnsigned || Literal.getRadix() != 10; |
| 2785 | |
| 2786 | // Check from smallest to largest, picking the smallest type we can. |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2787 | unsigned Width = 0; |
Chris Lattner | 97c5156 | 2007-08-23 21:58:08 +0000 | [diff] [blame] | 2788 | if (!Literal.isLong && !Literal.isLongLong) { |
| 2789 | // Are int/unsigned possibilities? |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2790 | unsigned IntSize = Context.getTargetInfo().getIntWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2791 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2792 | // Does it fit in a unsigned int? |
| 2793 | if (ResultVal.isIntN(IntSize)) { |
| 2794 | // Does it fit in a signed int? |
| 2795 | if (!Literal.isUnsigned && ResultVal[IntSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2796 | Ty = Context.IntTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2797 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2798 | Ty = Context.UnsignedIntTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2799 | Width = IntSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2800 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2801 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2802 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2803 | // Are long/unsigned long possibilities? |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2804 | if (Ty.isNull() && !Literal.isLongLong) { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2805 | unsigned LongSize = Context.getTargetInfo().getLongWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2806 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2807 | // Does it fit in a unsigned long? |
| 2808 | if (ResultVal.isIntN(LongSize)) { |
| 2809 | // Does it fit in a signed long? |
| 2810 | if (!Literal.isUnsigned && ResultVal[LongSize-1] == 0) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2811 | Ty = Context.LongTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2812 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2813 | Ty = Context.UnsignedLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2814 | Width = LongSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2815 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2816 | } |
| 2817 | |
Stephen Canon | b9e05f1 | 2012-05-03 22:49:43 +0000 | [diff] [blame] | 2818 | // Check long long if needed. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2819 | if (Ty.isNull()) { |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2820 | unsigned LongLongSize = Context.getTargetInfo().getLongLongWidth(); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2821 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2822 | // Does it fit in a unsigned long long? |
| 2823 | if (ResultVal.isIntN(LongLongSize)) { |
| 2824 | // Does it fit in a signed long long? |
Francois Pichet | 2432320 | 2011-01-11 23:38:13 +0000 | [diff] [blame] | 2825 | // To be compatible with MSVC, hex integer literals ending with the |
| 2826 | // LL or i64 suffix are always signed in Microsoft mode. |
Francois Pichet | a15a5ee | 2011-01-11 12:23:00 +0000 | [diff] [blame] | 2827 | if (!Literal.isUnsigned && (ResultVal[LongLongSize-1] == 0 || |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 2828 | (getLangOpts().MicrosoftExt && Literal.isLongLong))) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2829 | Ty = Context.LongLongTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2830 | else if (AllowUnsigned) |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2831 | Ty = Context.UnsignedLongLongTy; |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2832 | Width = LongLongSize; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2833 | } |
| 2834 | } |
Stephen Canon | b9e05f1 | 2012-05-03 22:49:43 +0000 | [diff] [blame] | 2835 | |
| 2836 | // If it doesn't fit in unsigned long long, and we're using Microsoft |
| 2837 | // extensions, then its a 128-bit integer literal. |
| 2838 | if (Ty.isNull() && Literal.isMicrosoftInteger) { |
| 2839 | if (Literal.isUnsigned) |
| 2840 | Ty = Context.UnsignedInt128Ty; |
| 2841 | else |
| 2842 | Ty = Context.Int128Ty; |
| 2843 | Width = 128; |
| 2844 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2845 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2846 | // If we still couldn't decide a type, we probably have something that |
| 2847 | // 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] | 2848 | if (Ty.isNull()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2849 | Diag(Tok.getLocation(), diag::warn_integer_too_large_for_signed); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2850 | Ty = Context.UnsignedLongLongTy; |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 2851 | Width = Context.getTargetInfo().getLongLongWidth(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2852 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2853 | |
Chris Lattner | 8cbcb0e | 2008-05-09 05:59:00 +0000 | [diff] [blame] | 2854 | if (ResultVal.getBitWidth() != Width) |
Jay Foad | 9f71a8f | 2010-12-07 08:25:34 +0000 | [diff] [blame] | 2855 | ResultVal = ResultVal.trunc(Width); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2856 | } |
Argyrios Kyrtzidis | 9996a7f | 2010-08-28 09:06:06 +0000 | [diff] [blame] | 2857 | Res = IntegerLiteral::Create(Context, ResultVal, Ty, Tok.getLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2858 | } |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2859 | |
Chris Lattner | 5d66145 | 2007-08-26 03:42:43 +0000 | [diff] [blame] | 2860 | // If this is an imaginary literal, create the ImaginaryLiteral wrapper. |
| 2861 | if (Literal.isImaginary) |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 2862 | Res = new (Context) ImaginaryLiteral(Res, |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2863 | Context.getComplexType(Res->getType())); |
Sebastian Redl | cd965b9 | 2009-01-18 18:53:16 +0000 | [diff] [blame] | 2864 | |
| 2865 | return Owned(Res); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2866 | } |
| 2867 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2868 | ExprResult Sema::ActOnParenExpr(SourceLocation L, SourceLocation R, Expr *E) { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 2869 | assert((E != 0) && "ActOnParenExpr() missing expr"); |
Steve Naroff | 6ece14c | 2009-01-21 00:14:39 +0000 | [diff] [blame] | 2870 | return Owned(new (Context) ParenExpr(L, R, E)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2871 | } |
| 2872 | |
Chandler Carruth | df1f377 | 2011-05-26 08:53:12 +0000 | [diff] [blame] | 2873 | static bool CheckVecStepTraitOperandType(Sema &S, QualType T, |
| 2874 | SourceLocation Loc, |
| 2875 | SourceRange ArgRange) { |
| 2876 | // [OpenCL 1.1 6.11.12] "The vec_step built-in function takes a built-in |
| 2877 | // scalar or vector data type argument..." |
| 2878 | // Every built-in scalar type (OpenCL 1.1 6.1.1) is either an arithmetic |
| 2879 | // type (C99 6.2.5p18) or void. |
| 2880 | if (!(T->isArithmeticType() || T->isVoidType() || T->isVectorType())) { |
| 2881 | S.Diag(Loc, diag::err_vecstep_non_scalar_vector_type) |
| 2882 | << T << ArgRange; |
| 2883 | return true; |
| 2884 | } |
| 2885 | |
| 2886 | assert((T->isVoidType() || !T->isIncompleteType()) && |
| 2887 | "Scalar types should always be complete"); |
| 2888 | return false; |
| 2889 | } |
| 2890 | |
Chandler Carruth | 42ec65d | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 2891 | static bool CheckExtensionTraitOperandType(Sema &S, QualType T, |
| 2892 | SourceLocation Loc, |
| 2893 | SourceRange ArgRange, |
| 2894 | UnaryExprOrTypeTrait TraitKind) { |
| 2895 | // C99 6.5.3.4p1: |
| 2896 | if (T->isFunctionType()) { |
| 2897 | // alignof(function) is allowed as an extension. |
| 2898 | if (TraitKind == UETT_SizeOf) |
| 2899 | S.Diag(Loc, diag::ext_sizeof_function_type) << ArgRange; |
| 2900 | return false; |
| 2901 | } |
| 2902 | |
| 2903 | // Allow sizeof(void)/alignof(void) as an extension. |
| 2904 | if (T->isVoidType()) { |
| 2905 | S.Diag(Loc, diag::ext_sizeof_void_type) << TraitKind << ArgRange; |
| 2906 | return false; |
| 2907 | } |
| 2908 | |
| 2909 | return true; |
| 2910 | } |
| 2911 | |
| 2912 | static bool CheckObjCTraitOperandConstraints(Sema &S, QualType T, |
| 2913 | SourceLocation Loc, |
| 2914 | SourceRange ArgRange, |
| 2915 | UnaryExprOrTypeTrait TraitKind) { |
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 2916 | // Reject sizeof(interface) and sizeof(interface<proto>) if the |
| 2917 | // runtime doesn't allow it. |
| 2918 | if (!S.LangOpts.ObjCRuntime.allowsSizeofAlignof() && T->isObjCObjectType()) { |
Chandler Carruth | 42ec65d | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 2919 | S.Diag(Loc, diag::err_sizeof_nonfragile_interface) |
| 2920 | << T << (TraitKind == UETT_SizeOf) |
| 2921 | << ArgRange; |
| 2922 | return true; |
| 2923 | } |
| 2924 | |
| 2925 | return false; |
| 2926 | } |
| 2927 | |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2928 | /// \brief Check the constrains on expression operands to unary type expression |
| 2929 | /// and type traits. |
| 2930 | /// |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2931 | /// Completes any types necessary and validates the constraints on the operand |
| 2932 | /// expression. The logic mostly mirrors the type-based overload, but may modify |
| 2933 | /// the expression as it completes the type for that expression through template |
| 2934 | /// instantiation, etc. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2935 | bool Sema::CheckUnaryExprOrTypeTraitOperand(Expr *E, |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2936 | UnaryExprOrTypeTrait ExprKind) { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2937 | QualType ExprTy = E->getType(); |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2938 | |
| 2939 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 2940 | // the result is the size of the referenced type." |
| 2941 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 2942 | // result shall be the alignment of the referenced type." |
| 2943 | if (const ReferenceType *Ref = ExprTy->getAs<ReferenceType>()) |
| 2944 | ExprTy = Ref->getPointeeType(); |
| 2945 | |
| 2946 | if (ExprKind == UETT_VecStep) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2947 | return CheckVecStepTraitOperandType(*this, ExprTy, E->getExprLoc(), |
| 2948 | E->getSourceRange()); |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2949 | |
| 2950 | // Whitelist some types as extensions |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2951 | if (!CheckExtensionTraitOperandType(*this, ExprTy, E->getExprLoc(), |
| 2952 | E->getSourceRange(), ExprKind)) |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2953 | return false; |
| 2954 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2955 | if (RequireCompleteExprType(E, |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 2956 | diag::err_sizeof_alignof_incomplete_type, |
| 2957 | ExprKind, E->getSourceRange())) |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2958 | return true; |
| 2959 | |
| 2960 | // Completeing the expression's type may have changed it. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2961 | ExprTy = E->getType(); |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2962 | if (const ReferenceType *Ref = ExprTy->getAs<ReferenceType>()) |
| 2963 | ExprTy = Ref->getPointeeType(); |
| 2964 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2965 | if (CheckObjCTraitOperandConstraints(*this, ExprTy, E->getExprLoc(), |
| 2966 | E->getSourceRange(), ExprKind)) |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2967 | return true; |
| 2968 | |
Nico Weber | cf73992 | 2011-06-15 02:47:03 +0000 | [diff] [blame] | 2969 | if (ExprKind == UETT_SizeOf) { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2970 | if (DeclRefExpr *DeclRef = dyn_cast<DeclRefExpr>(E->IgnoreParens())) { |
Nico Weber | cf73992 | 2011-06-15 02:47:03 +0000 | [diff] [blame] | 2971 | if (ParmVarDecl *PVD = dyn_cast<ParmVarDecl>(DeclRef->getFoundDecl())) { |
| 2972 | QualType OType = PVD->getOriginalType(); |
| 2973 | QualType Type = PVD->getType(); |
| 2974 | if (Type->isPointerType() && OType->isArrayType()) { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 2975 | Diag(E->getExprLoc(), diag::warn_sizeof_array_param) |
Nico Weber | cf73992 | 2011-06-15 02:47:03 +0000 | [diff] [blame] | 2976 | << Type << OType; |
| 2977 | Diag(PVD->getLocation(), diag::note_declared_at); |
| 2978 | } |
| 2979 | } |
| 2980 | } |
| 2981 | } |
| 2982 | |
Chandler Carruth | e4d645c | 2011-05-27 01:33:31 +0000 | [diff] [blame] | 2983 | return false; |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2984 | } |
| 2985 | |
| 2986 | /// \brief Check the constraints on operands to unary expression and type |
| 2987 | /// traits. |
| 2988 | /// |
| 2989 | /// This will complete any types necessary, and validate the various constraints |
| 2990 | /// on those operands. |
| 2991 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 2992 | /// The UsualUnaryConversions() function is *not* called by this routine. |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 2993 | /// C99 6.3.2.1p[2-4] all state: |
| 2994 | /// Except when it is the operand of the sizeof operator ... |
| 2995 | /// |
| 2996 | /// C++ [expr.sizeof]p4 |
| 2997 | /// The lvalue-to-rvalue, array-to-pointer, and function-to-pointer |
| 2998 | /// standard conversions are not applied to the operand of sizeof. |
| 2999 | /// |
| 3000 | /// This policy is followed for all of the unary trait expressions. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3001 | bool Sema::CheckUnaryExprOrTypeTraitOperand(QualType ExprType, |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3002 | SourceLocation OpLoc, |
| 3003 | SourceRange ExprRange, |
| 3004 | UnaryExprOrTypeTrait ExprKind) { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3005 | if (ExprType->isDependentType()) |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3006 | return false; |
| 3007 | |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 3008 | // C++ [expr.sizeof]p2: "When applied to a reference or a reference type, |
| 3009 | // the result is the size of the referenced type." |
| 3010 | // C++ [expr.alignof]p3: "When alignof is applied to a reference type, the |
| 3011 | // result shall be the alignment of the referenced type." |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3012 | if (const ReferenceType *Ref = ExprType->getAs<ReferenceType>()) |
| 3013 | ExprType = Ref->getPointeeType(); |
Sebastian Redl | 5d484e8 | 2009-11-23 17:18:46 +0000 | [diff] [blame] | 3014 | |
Chandler Carruth | df1f377 | 2011-05-26 08:53:12 +0000 | [diff] [blame] | 3015 | if (ExprKind == UETT_VecStep) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3016 | return CheckVecStepTraitOperandType(*this, ExprType, OpLoc, ExprRange); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3017 | |
Chandler Carruth | 42ec65d | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 3018 | // Whitelist some types as extensions |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3019 | if (!CheckExtensionTraitOperandType(*this, ExprType, OpLoc, ExprRange, |
Chandler Carruth | 42ec65d | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 3020 | ExprKind)) |
Chris Lattner | 0107292 | 2009-01-24 19:46:37 +0000 | [diff] [blame] | 3021 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3022 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3023 | if (RequireCompleteType(OpLoc, ExprType, |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3024 | diag::err_sizeof_alignof_incomplete_type, |
| 3025 | ExprKind, ExprRange)) |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3026 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3027 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3028 | if (CheckObjCTraitOperandConstraints(*this, ExprType, OpLoc, ExprRange, |
Chandler Carruth | 42ec65d | 2011-05-26 08:53:16 +0000 | [diff] [blame] | 3029 | ExprKind)) |
Chris Lattner | 5cb10d3 | 2009-04-24 22:30:50 +0000 | [diff] [blame] | 3030 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3031 | |
Chris Lattner | 1efaa95 | 2009-04-24 00:30:45 +0000 | [diff] [blame] | 3032 | return false; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3033 | } |
| 3034 | |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3035 | static bool CheckAlignOfExpr(Sema &S, Expr *E) { |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 3036 | E = E->IgnoreParens(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3037 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3038 | // alignof decl is always ok. |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 3039 | if (isa<DeclRefExpr>(E)) |
| 3040 | return false; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3041 | |
| 3042 | // Cannot know anything else if the expression is dependent. |
| 3043 | if (E->isTypeDependent()) |
| 3044 | return false; |
| 3045 | |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3046 | if (E->getBitField()) { |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3047 | S.Diag(E->getExprLoc(), diag::err_sizeof_alignof_bitfield) |
| 3048 | << 1 << E->getSourceRange(); |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3049 | return true; |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 3050 | } |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3051 | |
| 3052 | // Alignment of a field access is always okay, so long as it isn't a |
| 3053 | // bit-field. |
| 3054 | if (MemberExpr *ME = dyn_cast<MemberExpr>(E)) |
Mike Stump | 8e1fab2 | 2009-07-22 18:58:19 +0000 | [diff] [blame] | 3055 | if (isa<FieldDecl>(ME->getMemberDecl())) |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3056 | return false; |
| 3057 | |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3058 | return S.CheckUnaryExprOrTypeTraitOperand(E, UETT_AlignOf); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3059 | } |
| 3060 | |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3061 | bool Sema::CheckVecStepExpr(Expr *E) { |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3062 | E = E->IgnoreParens(); |
| 3063 | |
| 3064 | // Cannot know anything else if the expression is dependent. |
| 3065 | if (E->isTypeDependent()) |
| 3066 | return false; |
| 3067 | |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3068 | return CheckUnaryExprOrTypeTraitOperand(E, UETT_VecStep); |
Chris Lattner | 31e21e0 | 2009-01-24 20:17:12 +0000 | [diff] [blame] | 3069 | } |
| 3070 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3071 | /// \brief Build a sizeof or alignof expression given a type operand. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3072 | ExprResult |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3073 | Sema::CreateUnaryExprOrTypeTraitExpr(TypeSourceInfo *TInfo, |
| 3074 | SourceLocation OpLoc, |
| 3075 | UnaryExprOrTypeTrait ExprKind, |
| 3076 | SourceRange R) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3077 | if (!TInfo) |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3078 | return ExprError(); |
| 3079 | |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3080 | QualType T = TInfo->getType(); |
John McCall | 5ab7517 | 2009-11-04 07:28:41 +0000 | [diff] [blame] | 3081 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3082 | if (!T->isDependentType() && |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3083 | CheckUnaryExprOrTypeTraitOperand(T, OpLoc, R, ExprKind)) |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3084 | return ExprError(); |
| 3085 | |
| 3086 | // 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] | 3087 | return Owned(new (Context) UnaryExprOrTypeTraitExpr(ExprKind, TInfo, |
| 3088 | Context.getSizeType(), |
| 3089 | OpLoc, R.getEnd())); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3090 | } |
| 3091 | |
| 3092 | /// \brief Build a sizeof or alignof expression given an expression |
| 3093 | /// operand. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3094 | ExprResult |
Chandler Carruth | e72c55b | 2011-05-29 07:32:14 +0000 | [diff] [blame] | 3095 | Sema::CreateUnaryExprOrTypeTraitExpr(Expr *E, SourceLocation OpLoc, |
| 3096 | UnaryExprOrTypeTrait ExprKind) { |
Douglas Gregor | 4f0845e | 2011-06-22 23:21:00 +0000 | [diff] [blame] | 3097 | ExprResult PE = CheckPlaceholderExpr(E); |
| 3098 | if (PE.isInvalid()) |
| 3099 | return ExprError(); |
| 3100 | |
| 3101 | E = PE.get(); |
| 3102 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3103 | // Verify that the operand is valid. |
| 3104 | bool isInvalid = false; |
| 3105 | if (E->isTypeDependent()) { |
| 3106 | // Delay type-checking for type-dependent expressions. |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3107 | } else if (ExprKind == UETT_AlignOf) { |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3108 | isInvalid = CheckAlignOfExpr(*this, E); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3109 | } else if (ExprKind == UETT_VecStep) { |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3110 | isInvalid = CheckVecStepExpr(E); |
Douglas Gregor | 33bbbc5 | 2009-05-02 02:18:30 +0000 | [diff] [blame] | 3111 | } else if (E->getBitField()) { // C99 6.5.3.4p1. |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3112 | Diag(E->getExprLoc(), diag::err_sizeof_alignof_bitfield) << 0; |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3113 | isInvalid = true; |
| 3114 | } else { |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3115 | isInvalid = CheckUnaryExprOrTypeTraitOperand(E, UETT_SizeOf); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3116 | } |
| 3117 | |
| 3118 | if (isInvalid) |
| 3119 | return ExprError(); |
| 3120 | |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 3121 | if (ExprKind == UETT_SizeOf && E->getType()->isVariableArrayType()) { |
| 3122 | PE = TranformToPotentiallyEvaluated(E); |
| 3123 | if (PE.isInvalid()) return ExprError(); |
| 3124 | E = PE.take(); |
| 3125 | } |
| 3126 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3127 | // 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] | 3128 | return Owned(new (Context) UnaryExprOrTypeTraitExpr( |
Chandler Carruth | e72c55b | 2011-05-29 07:32:14 +0000 | [diff] [blame] | 3129 | ExprKind, E, Context.getSizeType(), OpLoc, |
Chandler Carruth | 9d342d0 | 2011-05-26 08:53:10 +0000 | [diff] [blame] | 3130 | E->getSourceRange().getEnd())); |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3131 | } |
| 3132 | |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3133 | /// ActOnUnaryExprOrTypeTraitExpr - Handle @c sizeof(type) and @c sizeof @c |
| 3134 | /// expr and the same for @c alignof and @c __alignof |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 3135 | /// Note that the ArgRange is invalid if isType is false. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3136 | ExprResult |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3137 | Sema::ActOnUnaryExprOrTypeTraitExpr(SourceLocation OpLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3138 | UnaryExprOrTypeTrait ExprKind, bool IsType, |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3139 | void *TyOrEx, const SourceRange &ArgRange) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3140 | // If error parsing type, ignore. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3141 | if (TyOrEx == 0) return ExprError(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3142 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3143 | if (IsType) { |
John McCall | a93c934 | 2009-12-07 02:54:59 +0000 | [diff] [blame] | 3144 | TypeSourceInfo *TInfo; |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 3145 | (void) GetTypeFromParser(ParsedType::getFromOpaquePtr(TyOrEx), &TInfo); |
Peter Collingbourne | f4e3cfb | 2011-03-11 19:24:49 +0000 | [diff] [blame] | 3146 | return CreateUnaryExprOrTypeTraitExpr(TInfo, OpLoc, ExprKind, ArgRange); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3147 | } |
Sebastian Redl | 0518999 | 2008-11-11 17:56:53 +0000 | [diff] [blame] | 3148 | |
Douglas Gregor | ba49817 | 2009-03-13 21:01:28 +0000 | [diff] [blame] | 3149 | Expr *ArgEx = (Expr *)TyOrEx; |
Chandler Carruth | e72c55b | 2011-05-29 07:32:14 +0000 | [diff] [blame] | 3150 | ExprResult Result = CreateUnaryExprOrTypeTraitExpr(ArgEx, OpLoc, ExprKind); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 3151 | return Result; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3152 | } |
| 3153 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3154 | static QualType CheckRealImagOperand(Sema &S, ExprResult &V, SourceLocation Loc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3155 | bool IsReal) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3156 | if (V.get()->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3157 | return S.Context.DependentTy; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3158 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3159 | // _Real and _Imag are only l-values for normal l-values. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3160 | if (V.get()->getObjectKind() != OK_Ordinary) { |
| 3161 | V = S.DefaultLvalueConversion(V.take()); |
| 3162 | if (V.isInvalid()) |
| 3163 | return QualType(); |
| 3164 | } |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3165 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 3166 | // These operators return the element type of a complex type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3167 | if (const ComplexType *CT = V.get()->getType()->getAs<ComplexType>()) |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 3168 | return CT->getElementType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3169 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 3170 | // Otherwise they pass through real integer and floating point types here. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3171 | if (V.get()->getType()->isArithmeticType()) |
| 3172 | return V.get()->getType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3173 | |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 3174 | // Test for placeholders. |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 3175 | ExprResult PR = S.CheckPlaceholderExpr(V.get()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 3176 | if (PR.isInvalid()) return QualType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3177 | if (PR.get() != V.get()) { |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 3178 | V = PR; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3179 | return CheckRealImagOperand(S, V, Loc, IsReal); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 3180 | } |
| 3181 | |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 3182 | // Reject anything else. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3183 | S.Diag(Loc, diag::err_realimag_invalid_type) << V.get()->getType() |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3184 | << (IsReal ? "__real" : "__imag"); |
Chris Lattner | cc26ed7 | 2007-08-26 05:39:26 +0000 | [diff] [blame] | 3185 | return QualType(); |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 3186 | } |
| 3187 | |
| 3188 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3189 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3190 | ExprResult |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3191 | Sema::ActOnPostfixUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3192 | tok::TokenKind Kind, Expr *Input) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3193 | UnaryOperatorKind Opc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3194 | switch (Kind) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 3195 | default: llvm_unreachable("Unknown unary op!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 3196 | case tok::plusplus: Opc = UO_PostInc; break; |
| 3197 | case tok::minusminus: Opc = UO_PostDec; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3198 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3199 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 3200 | // Since this might is a postfix expression, get rid of ParenListExprs. |
| 3201 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Input); |
| 3202 | if (Result.isInvalid()) return ExprError(); |
| 3203 | Input = Result.take(); |
| 3204 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3205 | return BuildUnaryOp(S, OpLoc, Opc, Input); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3206 | } |
| 3207 | |
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 3208 | /// \brief Diagnose if arithmetic on the given ObjC pointer is illegal. |
| 3209 | /// |
| 3210 | /// \return true on error |
| 3211 | static bool checkArithmeticOnObjCPointer(Sema &S, |
| 3212 | SourceLocation opLoc, |
| 3213 | Expr *op) { |
| 3214 | assert(op->getType()->isObjCObjectPointerType()); |
| 3215 | if (S.LangOpts.ObjCRuntime.allowsPointerArithmetic()) |
| 3216 | return false; |
| 3217 | |
| 3218 | S.Diag(opLoc, diag::err_arithmetic_nonfragile_interface) |
| 3219 | << op->getType()->castAs<ObjCObjectPointerType>()->getPointeeType() |
| 3220 | << op->getSourceRange(); |
| 3221 | return true; |
| 3222 | } |
| 3223 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3224 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3225 | Sema::ActOnArraySubscriptExpr(Scope *S, Expr *Base, SourceLocation LLoc, |
| 3226 | Expr *Idx, SourceLocation RLoc) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3227 | // Since this might be a postfix expression, get rid of ParenListExprs. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3228 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Base); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3229 | if (Result.isInvalid()) return ExprError(); |
| 3230 | Base = Result.take(); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3231 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3232 | Expr *LHSExp = Base, *RHSExp = Idx; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3233 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3234 | if (getLangOpts().CPlusPlus && |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 3235 | (LHSExp->isTypeDependent() || RHSExp->isTypeDependent())) { |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 3236 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3237 | Context.DependentTy, |
| 3238 | VK_LValue, OK_Ordinary, |
| 3239 | RLoc)); |
Douglas Gregor | 3384c9c | 2009-05-19 00:01:19 +0000 | [diff] [blame] | 3240 | } |
| 3241 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3242 | if (getLangOpts().CPlusPlus && |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3243 | (LHSExp->getType()->isRecordType() || |
Eli Friedman | 03f332a | 2008-12-15 22:34:21 +0000 | [diff] [blame] | 3244 | LHSExp->getType()->isEnumeralType() || |
| 3245 | RHSExp->getType()->isRecordType() || |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 3246 | RHSExp->getType()->isEnumeralType()) && |
| 3247 | !LHSExp->getType()->isObjCObjectPointerType()) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3248 | return CreateOverloadedArraySubscriptExpr(LLoc, RLoc, Base, Idx); |
Douglas Gregor | 337c6b9 | 2008-11-19 17:17:41 +0000 | [diff] [blame] | 3249 | } |
| 3250 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3251 | return CreateBuiltinArraySubscriptExpr(Base, LLoc, Idx, RLoc); |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 3252 | } |
| 3253 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3254 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3255 | Sema::CreateBuiltinArraySubscriptExpr(Expr *Base, SourceLocation LLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3256 | Expr *Idx, SourceLocation RLoc) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3257 | Expr *LHSExp = Base; |
| 3258 | Expr *RHSExp = Idx; |
Sebastian Redl | f322ed6 | 2009-10-29 20:17:01 +0000 | [diff] [blame] | 3259 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3260 | // Perform default conversions. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3261 | if (!LHSExp->getType()->getAs<VectorType>()) { |
| 3262 | ExprResult Result = DefaultFunctionArrayLvalueConversion(LHSExp); |
| 3263 | if (Result.isInvalid()) |
| 3264 | return ExprError(); |
| 3265 | LHSExp = Result.take(); |
| 3266 | } |
| 3267 | ExprResult Result = DefaultFunctionArrayLvalueConversion(RHSExp); |
| 3268 | if (Result.isInvalid()) |
| 3269 | return ExprError(); |
| 3270 | RHSExp = Result.take(); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3271 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3272 | QualType LHSTy = LHSExp->getType(), RHSTy = RHSExp->getType(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3273 | ExprValueKind VK = VK_LValue; |
| 3274 | ExprObjectKind OK = OK_Ordinary; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3275 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3276 | // 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] | 3277 | // 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] | 3278 | // 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] | 3279 | // and index from the expression types. |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3280 | Expr *BaseExpr, *IndexExpr; |
| 3281 | QualType ResultType; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 3282 | if (LHSTy->isDependentType() || RHSTy->isDependentType()) { |
| 3283 | BaseExpr = LHSExp; |
| 3284 | IndexExpr = RHSExp; |
| 3285 | ResultType = Context.DependentTy; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3286 | } else if (const PointerType *PTy = LHSTy->getAs<PointerType>()) { |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3287 | BaseExpr = LHSExp; |
| 3288 | IndexExpr = RHSExp; |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3289 | ResultType = PTy->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3290 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 3291 | LHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3292 | BaseExpr = LHSExp; |
| 3293 | IndexExpr = RHSExp; |
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 3294 | |
| 3295 | // Use custom logic if this should be the pseudo-object subscript |
| 3296 | // expression. |
| 3297 | if (!LangOpts.ObjCRuntime.isSubscriptPointerArithmetic()) |
| 3298 | return BuildObjCSubscriptExpression(RLoc, BaseExpr, IndexExpr, 0, 0); |
| 3299 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3300 | ResultType = PTy->getPointeeType(); |
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 3301 | if (!LangOpts.ObjCRuntime.allowsPointerArithmetic()) { |
| 3302 | Diag(LLoc, diag::err_subscript_nonfragile_interface) |
| 3303 | << ResultType << BaseExpr->getSourceRange(); |
| 3304 | return ExprError(); |
| 3305 | } |
Fariborz Jahanian | a78eca2 | 2012-03-28 17:56:49 +0000 | [diff] [blame] | 3306 | } else if (const PointerType *PTy = RHSTy->getAs<PointerType>()) { |
| 3307 | // Handle the uncommon case of "123[Ptr]". |
| 3308 | BaseExpr = RHSExp; |
| 3309 | IndexExpr = LHSExp; |
| 3310 | ResultType = PTy->getPointeeType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3311 | } else if (const ObjCObjectPointerType *PTy = |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3312 | RHSTy->getAs<ObjCObjectPointerType>()) { |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 3313 | // Handle the uncommon case of "123[Ptr]". |
| 3314 | BaseExpr = RHSExp; |
| 3315 | IndexExpr = LHSExp; |
| 3316 | ResultType = PTy->getPointeeType(); |
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 3317 | if (!LangOpts.ObjCRuntime.allowsPointerArithmetic()) { |
| 3318 | Diag(LLoc, diag::err_subscript_nonfragile_interface) |
| 3319 | << ResultType << BaseExpr->getSourceRange(); |
| 3320 | return ExprError(); |
| 3321 | } |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3322 | } else if (const VectorType *VTy = LHSTy->getAs<VectorType>()) { |
Chris Lattner | c862963 | 2007-07-31 19:29:30 +0000 | [diff] [blame] | 3323 | BaseExpr = LHSExp; // vectors: V[123] |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3324 | IndexExpr = RHSExp; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3325 | VK = LHSExp->getValueKind(); |
| 3326 | if (VK != VK_RValue) |
| 3327 | OK = OK_VectorComponent; |
Nate Begeman | 334a802 | 2009-01-18 00:45:31 +0000 | [diff] [blame] | 3328 | |
Chris Lattner | 12d9ff6 | 2007-07-16 00:14:47 +0000 | [diff] [blame] | 3329 | // FIXME: need to deal with const... |
| 3330 | ResultType = VTy->getElementType(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3331 | } else if (LHSTy->isArrayType()) { |
| 3332 | // If we see an array that wasn't promoted by |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 3333 | // DefaultFunctionArrayLvalueConversion, it must be an array that |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3334 | // wasn't promoted because of the C90 rule that doesn't |
| 3335 | // allow promoting non-lvalue arrays. Warn, then |
| 3336 | // force the promotion here. |
| 3337 | Diag(LHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 3338 | LHSExp->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3339 | LHSExp = ImpCastExprToType(LHSExp, Context.getArrayDecayedType(LHSTy), |
| 3340 | CK_ArrayToPointerDecay).take(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3341 | LHSTy = LHSExp->getType(); |
| 3342 | |
| 3343 | BaseExpr = LHSExp; |
| 3344 | IndexExpr = RHSExp; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3345 | ResultType = LHSTy->getAs<PointerType>()->getPointeeType(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3346 | } else if (RHSTy->isArrayType()) { |
| 3347 | // Same as previous, except for 123[f().a] case |
| 3348 | Diag(RHSExp->getLocStart(), diag::ext_subscript_non_lvalue) << |
| 3349 | RHSExp->getSourceRange(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3350 | RHSExp = ImpCastExprToType(RHSExp, Context.getArrayDecayedType(RHSTy), |
| 3351 | CK_ArrayToPointerDecay).take(); |
Eli Friedman | 7c32f8e | 2009-04-25 23:46:54 +0000 | [diff] [blame] | 3352 | RHSTy = RHSExp->getType(); |
| 3353 | |
| 3354 | BaseExpr = RHSExp; |
| 3355 | IndexExpr = LHSExp; |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 3356 | ResultType = RHSTy->getAs<PointerType>()->getPointeeType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3357 | } else { |
Chris Lattner | 338395d | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 3358 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_value) |
| 3359 | << LHSExp->getSourceRange() << RHSExp->getSourceRange()); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3360 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3361 | // C99 6.5.2.1p1 |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 3362 | if (!IndexExpr->getType()->isIntegerType() && !IndexExpr->isTypeDependent()) |
Chris Lattner | 338395d | 2009-04-25 22:50:55 +0000 | [diff] [blame] | 3363 | return ExprError(Diag(LLoc, diag::err_typecheck_subscript_not_integer) |
| 3364 | << IndexExpr->getSourceRange()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3365 | |
Daniel Dunbar | 7e88a60 | 2009-09-17 06:31:17 +0000 | [diff] [blame] | 3366 | if ((IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_S) || |
Sam Weinig | 0f9a5b5 | 2009-09-14 20:14:57 +0000 | [diff] [blame] | 3367 | IndexExpr->getType()->isSpecificBuiltinType(BuiltinType::Char_U)) |
| 3368 | && !IndexExpr->isTypeDependent()) |
Sam Weinig | 76e2b71 | 2009-09-14 01:58:58 +0000 | [diff] [blame] | 3369 | Diag(LLoc, diag::warn_subscript_is_char) << IndexExpr->getSourceRange(); |
| 3370 | |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3371 | // 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] | 3372 | // C++ [expr.sub]p1: The type "T" shall be a completely-defined object |
| 3373 | // type. Note that Functions are not objects, and that (in C99 parlance) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3374 | // incomplete types are not object types. |
| 3375 | if (ResultType->isFunctionType()) { |
| 3376 | Diag(BaseExpr->getLocStart(), diag::err_subscript_function_type) |
| 3377 | << ResultType << BaseExpr->getSourceRange(); |
| 3378 | return ExprError(); |
| 3379 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3380 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3381 | if (ResultType->isVoidType() && !getLangOpts().CPlusPlus) { |
Abramo Bagnara | 4635845 | 2010-09-13 06:50:07 +0000 | [diff] [blame] | 3382 | // GNU extension: subscripting on pointer to void |
Chandler Carruth | 6628969 | 2011-06-27 16:32:27 +0000 | [diff] [blame] | 3383 | Diag(LLoc, diag::ext_gnu_subscript_void_type) |
| 3384 | << BaseExpr->getSourceRange(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3385 | |
| 3386 | // C forbids expressions of unqualified void type from being l-values. |
| 3387 | // See IsCForbiddenLValueType. |
| 3388 | if (!ResultType.hasQualifiers()) VK = VK_RValue; |
Abramo Bagnara | 4635845 | 2010-09-13 06:50:07 +0000 | [diff] [blame] | 3389 | } else if (!ResultType->isDependentType() && |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3390 | RequireCompleteType(LLoc, ResultType, |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3391 | diag::err_subscript_incomplete_type, BaseExpr)) |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 3392 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3393 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3394 | assert(VK == VK_RValue || LangOpts.CPlusPlus || |
Douglas Gregor | 2b1ad8b | 2011-06-23 00:49:38 +0000 | [diff] [blame] | 3395 | !ResultType.isCForbiddenLValueType()); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 3396 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3397 | return Owned(new (Context) ArraySubscriptExpr(LHSExp, RHSExp, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 3398 | ResultType, VK, OK, RLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3399 | } |
| 3400 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3401 | ExprResult Sema::BuildCXXDefaultArgExpr(SourceLocation CallLoc, |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 3402 | FunctionDecl *FD, |
| 3403 | ParmVarDecl *Param) { |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3404 | if (Param->hasUnparsedDefaultArg()) { |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3405 | Diag(CallLoc, |
Nico Weber | 15d5c83 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 3406 | diag::err_use_of_default_argument_to_function_declared_later) << |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3407 | FD << cast<CXXRecordDecl>(FD->getDeclContext())->getDeclName(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3408 | Diag(UnparsedDefaultArgLocs[Param], |
Nico Weber | 15d5c83 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 3409 | diag::note_default_argument_declared_here); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3410 | return ExprError(); |
| 3411 | } |
| 3412 | |
| 3413 | if (Param->hasUninstantiatedDefaultArg()) { |
| 3414 | Expr *UninstExpr = Param->getUninstantiatedDefaultArg(); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3415 | |
Richard Smith | adb1d4c | 2012-07-22 23:45:10 +0000 | [diff] [blame] | 3416 | EnterExpressionEvaluationContext EvalContext(*this, PotentiallyEvaluated, |
| 3417 | Param); |
| 3418 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3419 | // Instantiate the expression. |
| 3420 | MultiLevelTemplateArgumentList ArgList |
| 3421 | = getTemplateInstantiationArgs(FD, 0, /*RelativeToPrimary=*/true); |
Anders Carlsson | 25cae7f | 2009-09-05 05:14:19 +0000 | [diff] [blame] | 3422 | |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 3423 | std::pair<const TemplateArgument *, unsigned> Innermost |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3424 | = ArgList.getInnermost(); |
Richard Smith | 7e54fb5 | 2012-07-16 01:09:10 +0000 | [diff] [blame] | 3425 | InstantiatingTemplate Inst(*this, CallLoc, Param, |
| 3426 | ArrayRef<TemplateArgument>(Innermost.first, |
| 3427 | Innermost.second)); |
Richard Smith | ab91ef1 | 2012-07-08 02:38:24 +0000 | [diff] [blame] | 3428 | if (Inst) |
| 3429 | return ExprError(); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3430 | |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 3431 | ExprResult Result; |
| 3432 | { |
| 3433 | // C++ [dcl.fct.default]p5: |
| 3434 | // The names in the [default argument] expression are bound, and |
| 3435 | // the semantic constraints are checked, at the point where the |
| 3436 | // default argument expression appears. |
Nico Weber | 15d5c83 | 2010-11-30 04:44:33 +0000 | [diff] [blame] | 3437 | ContextRAII SavedContext(*this, FD); |
Douglas Gregor | 7bdc152 | 2012-02-16 21:36:18 +0000 | [diff] [blame] | 3438 | LocalInstantiationScope Local(*this); |
Nico Weber | 08e41a6 | 2010-11-29 18:19:25 +0000 | [diff] [blame] | 3439 | Result = SubstExpr(UninstExpr, ArgList); |
| 3440 | } |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3441 | if (Result.isInvalid()) |
| 3442 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3443 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3444 | // Check the expression as an initializer for the parameter. |
| 3445 | InitializedEntity Entity |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 3446 | = InitializedEntity::InitializeParameter(Context, Param); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3447 | InitializationKind Kind |
| 3448 | = InitializationKind::CreateCopy(Param->getLocation(), |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3449 | /*FIXME:EqualLoc*/UninstExpr->getLocStart()); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3450 | Expr *ResultE = Result.takeAs<Expr>(); |
Douglas Gregor | 65222e8 | 2009-12-23 18:19:08 +0000 | [diff] [blame] | 3451 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3452 | InitializationSequence InitSeq(*this, Entity, Kind, &ResultE, 1); |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 3453 | Result = InitSeq.Perform(*this, Entity, Kind, ResultE); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3454 | if (Result.isInvalid()) |
| 3455 | return ExprError(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3456 | |
David Blaikie | c1c0725 | 2012-04-30 18:21:31 +0000 | [diff] [blame] | 3457 | Expr *Arg = Result.takeAs<Expr>(); |
David Blaikie | 9fb1ac5 | 2012-05-15 21:57:38 +0000 | [diff] [blame] | 3458 | CheckImplicitConversions(Arg, Param->getOuterLocStart()); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3459 | // Build the default argument expression. |
David Blaikie | c1c0725 | 2012-04-30 18:21:31 +0000 | [diff] [blame] | 3460 | return Owned(CXXDefaultArgExpr::Create(Context, CallLoc, Param, Arg)); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3461 | } |
| 3462 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3463 | // If the default expression creates temporaries, we need to |
| 3464 | // push them to the current stack of expression temporaries so they'll |
| 3465 | // be properly destroyed. |
| 3466 | // FIXME: We should really be rebuilding the default argument with new |
| 3467 | // bound temporaries; see the comment in PR5810. |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 3468 | // We don't need to do that with block decls, though, because |
| 3469 | // blocks in default argument expression can never capture anything. |
| 3470 | if (isa<ExprWithCleanups>(Param->getInit())) { |
| 3471 | // Set the "needs cleanups" bit regardless of whether there are |
| 3472 | // any explicit objects. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3473 | ExprNeedsCleanups = true; |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 3474 | |
| 3475 | // Append all the objects to the cleanup list. Right now, this |
| 3476 | // should always be a no-op, because blocks in default argument |
| 3477 | // expressions should never be able to capture anything. |
| 3478 | assert(!cast<ExprWithCleanups>(Param->getInit())->getNumObjects() && |
| 3479 | "default argument expression has capturing blocks?"); |
Douglas Gregor | 5833b0b | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 3480 | } |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 3481 | |
| 3482 | // We already type-checked the argument, so we know it works. |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 3483 | // Just mark all of the declarations in this potentially-evaluated expression |
| 3484 | // as being "referenced". |
Douglas Gregor | f4b7de1 | 2012-02-21 19:11:17 +0000 | [diff] [blame] | 3485 | MarkDeclarationsReferencedInExpr(Param->getDefaultArg(), |
| 3486 | /*SkipLocalVariables=*/true); |
Douglas Gregor | 036aed1 | 2009-12-23 23:03:06 +0000 | [diff] [blame] | 3487 | return Owned(CXXDefaultArgExpr::Create(Context, CallLoc, Param)); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3488 | } |
| 3489 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3490 | |
| 3491 | Sema::VariadicCallType |
| 3492 | Sema::getVariadicCallType(FunctionDecl *FDecl, const FunctionProtoType *Proto, |
| 3493 | Expr *Fn) { |
| 3494 | if (Proto && Proto->isVariadic()) { |
| 3495 | if (dyn_cast_or_null<CXXConstructorDecl>(FDecl)) |
| 3496 | return VariadicConstructor; |
| 3497 | else if (Fn && Fn->getType()->isBlockPointerType()) |
| 3498 | return VariadicBlock; |
| 3499 | else if (FDecl) { |
| 3500 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(FDecl)) |
| 3501 | if (Method->isInstance()) |
| 3502 | return VariadicMethod; |
| 3503 | } |
| 3504 | return VariadicFunction; |
| 3505 | } |
| 3506 | return VariadicDoesNotApply; |
| 3507 | } |
| 3508 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3509 | /// ConvertArgumentsForCall - Converts the arguments specified in |
| 3510 | /// Args/NumArgs to the parameter types of the function FDecl with |
| 3511 | /// function prototype Proto. Call is the call expression itself, and |
| 3512 | /// Fn is the function expression. For a C++ member function, this |
| 3513 | /// routine does not attempt to convert the object argument. Returns |
| 3514 | /// true if the call is ill-formed. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3515 | bool |
| 3516 | Sema::ConvertArgumentsForCall(CallExpr *Call, Expr *Fn, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3517 | FunctionDecl *FDecl, |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 3518 | const FunctionProtoType *Proto, |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3519 | Expr **Args, unsigned NumArgs, |
Peter Collingbourne | 1f24076 | 2011-10-02 23:49:29 +0000 | [diff] [blame] | 3520 | SourceLocation RParenLoc, |
| 3521 | bool IsExecConfig) { |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 3522 | // Bail out early if calling a builtin with custom typechecking. |
| 3523 | // We don't need to do this in the |
| 3524 | if (FDecl) |
| 3525 | if (unsigned ID = FDecl->getBuiltinID()) |
| 3526 | if (Context.BuiltinInfo.hasCustomTypechecking(ID)) |
| 3527 | return false; |
| 3528 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3529 | // 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] | 3530 | // assignment, to the types of the corresponding parameter, ... |
| 3531 | unsigned NumArgsInProto = Proto->getNumArgs(); |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 3532 | bool Invalid = false; |
Peter Collingbourne | af15b4d | 2011-10-02 23:49:20 +0000 | [diff] [blame] | 3533 | unsigned MinArgs = FDecl ? FDecl->getMinRequiredArguments() : NumArgsInProto; |
Peter Collingbourne | 1f24076 | 2011-10-02 23:49:29 +0000 | [diff] [blame] | 3534 | unsigned FnKind = Fn->getType()->isBlockPointerType() |
| 3535 | ? 1 /* block */ |
| 3536 | : (IsExecConfig ? 3 /* kernel function (exec config) */ |
| 3537 | : 0 /* function */); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3538 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3539 | // If too few arguments are available (and we don't have default |
| 3540 | // arguments for the remaining parameters), don't make the call. |
| 3541 | if (NumArgs < NumArgsInProto) { |
Peter Collingbourne | af15b4d | 2011-10-02 23:49:20 +0000 | [diff] [blame] | 3542 | if (NumArgs < MinArgs) { |
Richard Smith | f7b8056 | 2012-05-11 05:16:41 +0000 | [diff] [blame] | 3543 | if (MinArgs == 1 && FDecl && FDecl->getParamDecl(0)->getDeclName()) |
| 3544 | Diag(RParenLoc, MinArgs == NumArgsInProto && !Proto->isVariadic() |
| 3545 | ? diag::err_typecheck_call_too_few_args_one |
| 3546 | : diag::err_typecheck_call_too_few_args_at_least_one) |
| 3547 | << FnKind |
| 3548 | << FDecl->getParamDecl(0) << Fn->getSourceRange(); |
| 3549 | else |
| 3550 | Diag(RParenLoc, MinArgs == NumArgsInProto && !Proto->isVariadic() |
| 3551 | ? diag::err_typecheck_call_too_few_args |
| 3552 | : diag::err_typecheck_call_too_few_args_at_least) |
| 3553 | << FnKind |
| 3554 | << MinArgs << NumArgs << Fn->getSourceRange(); |
Peter Collingbourne | 9aab148 | 2011-07-29 00:24:42 +0000 | [diff] [blame] | 3555 | |
| 3556 | // Emit the location of the prototype. |
Peter Collingbourne | 1f24076 | 2011-10-02 23:49:29 +0000 | [diff] [blame] | 3557 | if (FDecl && !FDecl->getBuiltinID() && !IsExecConfig) |
Peter Collingbourne | 9aab148 | 2011-07-29 00:24:42 +0000 | [diff] [blame] | 3558 | Diag(FDecl->getLocStart(), diag::note_callee_decl) |
| 3559 | << FDecl; |
| 3560 | |
| 3561 | return true; |
| 3562 | } |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3563 | Call->setNumArgs(Context, NumArgsInProto); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3564 | } |
| 3565 | |
| 3566 | // If too many are passed and not variadic, error on the extras and drop |
| 3567 | // them. |
| 3568 | if (NumArgs > NumArgsInProto) { |
| 3569 | if (!Proto->isVariadic()) { |
Richard Smith | c608c3c | 2012-05-15 06:21:54 +0000 | [diff] [blame] | 3570 | if (NumArgsInProto == 1 && FDecl && FDecl->getParamDecl(0)->getDeclName()) |
| 3571 | Diag(Args[NumArgsInProto]->getLocStart(), |
| 3572 | MinArgs == NumArgsInProto |
| 3573 | ? diag::err_typecheck_call_too_many_args_one |
| 3574 | : diag::err_typecheck_call_too_many_args_at_most_one) |
| 3575 | << FnKind |
| 3576 | << FDecl->getParamDecl(0) << NumArgs << Fn->getSourceRange() |
| 3577 | << SourceRange(Args[NumArgsInProto]->getLocStart(), |
| 3578 | Args[NumArgs-1]->getLocEnd()); |
| 3579 | else |
| 3580 | Diag(Args[NumArgsInProto]->getLocStart(), |
| 3581 | MinArgs == NumArgsInProto |
| 3582 | ? diag::err_typecheck_call_too_many_args |
| 3583 | : diag::err_typecheck_call_too_many_args_at_most) |
| 3584 | << FnKind |
| 3585 | << NumArgsInProto << NumArgs << Fn->getSourceRange() |
| 3586 | << SourceRange(Args[NumArgsInProto]->getLocStart(), |
| 3587 | Args[NumArgs-1]->getLocEnd()); |
Ted Kremenek | 5862f0e | 2011-04-04 17:22:27 +0000 | [diff] [blame] | 3588 | |
| 3589 | // Emit the location of the prototype. |
Peter Collingbourne | 1f24076 | 2011-10-02 23:49:29 +0000 | [diff] [blame] | 3590 | if (FDecl && !FDecl->getBuiltinID() && !IsExecConfig) |
Peter Collingbourne | 9aab148 | 2011-07-29 00:24:42 +0000 | [diff] [blame] | 3591 | Diag(FDecl->getLocStart(), diag::note_callee_decl) |
| 3592 | << FDecl; |
Ted Kremenek | 5862f0e | 2011-04-04 17:22:27 +0000 | [diff] [blame] | 3593 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3594 | // This deletes the extra arguments. |
Ted Kremenek | 8189cde | 2009-02-07 01:47:29 +0000 | [diff] [blame] | 3595 | Call->setNumArgs(Context, NumArgsInProto); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3596 | return true; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3597 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3598 | } |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3599 | SmallVector<Expr *, 8> AllArgs; |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 3600 | VariadicCallType CallType = getVariadicCallType(FDecl, Proto, Fn); |
| 3601 | |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3602 | Invalid = GatherArgumentsForCall(Call->getLocStart(), FDecl, |
Fariborz Jahanian | 2fe168f | 2009-11-24 21:37:28 +0000 | [diff] [blame] | 3603 | Proto, 0, Args, NumArgs, AllArgs, CallType); |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3604 | if (Invalid) |
| 3605 | return true; |
| 3606 | unsigned TotalNumArgs = AllArgs.size(); |
| 3607 | for (unsigned i = 0; i < TotalNumArgs; ++i) |
| 3608 | Call->setArg(i, AllArgs[i]); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3609 | |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3610 | return false; |
| 3611 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3612 | |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3613 | bool Sema::GatherArgumentsForCall(SourceLocation CallLoc, |
| 3614 | FunctionDecl *FDecl, |
| 3615 | const FunctionProtoType *Proto, |
| 3616 | unsigned FirstProtoArg, |
| 3617 | Expr **Args, unsigned NumArgs, |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 3618 | SmallVector<Expr *, 8> &AllArgs, |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3619 | VariadicCallType CallType, |
| 3620 | bool AllowExplicit) { |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3621 | unsigned NumArgsInProto = Proto->getNumArgs(); |
| 3622 | unsigned NumArgsToCheck = NumArgs; |
| 3623 | bool Invalid = false; |
| 3624 | if (NumArgs != NumArgsInProto) |
| 3625 | // Use default arguments for missing arguments |
| 3626 | NumArgsToCheck = NumArgsInProto; |
| 3627 | unsigned ArgIx = 0; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3628 | // 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] | 3629 | for (unsigned i = FirstProtoArg; i != NumArgsToCheck; i++) { |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3630 | QualType ProtoArgType = Proto->getArgType(i); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3631 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3632 | Expr *Arg; |
Peter Collingbourne | 013e5ce | 2011-10-19 00:16:45 +0000 | [diff] [blame] | 3633 | ParmVarDecl *Param; |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3634 | if (ArgIx < NumArgs) { |
| 3635 | Arg = Args[ArgIx++]; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3636 | |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 3637 | if (RequireCompleteType(Arg->getLocStart(), |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3638 | ProtoArgType, |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 3639 | diag::err_call_incomplete_argument, Arg)) |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 3640 | return true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3641 | |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3642 | // Pass the argument |
Peter Collingbourne | 013e5ce | 2011-10-19 00:16:45 +0000 | [diff] [blame] | 3643 | Param = 0; |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3644 | if (FDecl && i < FDecl->getNumParams()) |
| 3645 | Param = FDecl->getParamDecl(i); |
Douglas Gregor | aa03731 | 2009-12-22 07:24:36 +0000 | [diff] [blame] | 3646 | |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 3647 | // Strip the unbridged-cast placeholder expression off, if applicable. |
| 3648 | if (Arg->getType() == Context.ARCUnbridgedCastTy && |
| 3649 | FDecl && FDecl->hasAttr<CFAuditedTransferAttr>() && |
| 3650 | (!Param || !Param->hasAttr<CFConsumedAttr>())) |
| 3651 | Arg = stripARCUnbridgedCast(Arg); |
| 3652 | |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3653 | InitializedEntity Entity = |
Fariborz Jahanian | 745da3a | 2010-09-24 17:30:16 +0000 | [diff] [blame] | 3654 | Param? InitializedEntity::InitializeParameter(Context, Param) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 3655 | : InitializedEntity::InitializeParameter(Context, ProtoArgType, |
| 3656 | Proto->isArgConsumed(i)); |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3657 | ExprResult ArgE = PerformCopyInitialization(Entity, |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 3658 | SourceLocation(), |
Douglas Gregor | ed878af | 2012-02-24 23:56:31 +0000 | [diff] [blame] | 3659 | Owned(Arg), |
| 3660 | /*TopLevelOfInitList=*/false, |
| 3661 | AllowExplicit); |
Douglas Gregor | a188ff2 | 2009-12-22 16:09:06 +0000 | [diff] [blame] | 3662 | if (ArgE.isInvalid()) |
| 3663 | return true; |
| 3664 | |
| 3665 | Arg = ArgE.takeAs<Expr>(); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 3666 | } else { |
Peter Collingbourne | 013e5ce | 2011-10-19 00:16:45 +0000 | [diff] [blame] | 3667 | Param = FDecl->getParamDecl(i); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3668 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3669 | ExprResult ArgExpr = |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3670 | BuildCXXDefaultArgExpr(CallLoc, FDecl, Param); |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3671 | if (ArgExpr.isInvalid()) |
| 3672 | return true; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3673 | |
Anders Carlsson | 56c5e33 | 2009-08-25 03:49:14 +0000 | [diff] [blame] | 3674 | Arg = ArgExpr.takeAs<Expr>(); |
Anders Carlsson | 5e300d1 | 2009-06-12 16:51:40 +0000 | [diff] [blame] | 3675 | } |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 3676 | |
| 3677 | // Check for array bounds violations for each argument to the call. This |
| 3678 | // check only triggers warnings when the argument isn't a more complex Expr |
| 3679 | // with its own checking, such as a BinaryOperator. |
| 3680 | CheckArrayAccess(Arg); |
| 3681 | |
Peter Collingbourne | 013e5ce | 2011-10-19 00:16:45 +0000 | [diff] [blame] | 3682 | // Check for violations of C99 static array rules (C99 6.7.5.3p7). |
| 3683 | CheckStaticArrayArgument(CallLoc, Param, Arg); |
| 3684 | |
Fariborz Jahanian | 048f52a | 2009-11-24 18:29:37 +0000 | [diff] [blame] | 3685 | AllArgs.push_back(Arg); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3686 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 3687 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3688 | // If this is a variadic call, handle args passed through "...". |
Fariborz Jahanian | 4cd1c70 | 2009-11-24 19:27:49 +0000 | [diff] [blame] | 3689 | if (CallType != VariadicDoesNotApply) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3690 | // Assume that extern "C" functions with variadic arguments that |
| 3691 | // return __unknown_anytype aren't *really* variadic. |
| 3692 | if (Proto->getResultType() == Context.UnknownAnyTy && |
| 3693 | FDecl && FDecl->isExternC()) { |
| 3694 | for (unsigned i = ArgIx; i != NumArgs; ++i) { |
| 3695 | ExprResult arg; |
| 3696 | if (isa<ExplicitCastExpr>(Args[i]->IgnoreParens())) |
| 3697 | arg = DefaultFunctionArrayLvalueConversion(Args[i]); |
| 3698 | else |
| 3699 | arg = DefaultVariadicArgumentPromotion(Args[i], CallType, FDecl); |
| 3700 | Invalid |= arg.isInvalid(); |
| 3701 | AllArgs.push_back(arg.take()); |
| 3702 | } |
| 3703 | |
| 3704 | // Otherwise do argument promotion, (C99 6.5.2.2p7). |
| 3705 | } else { |
| 3706 | for (unsigned i = ArgIx; i != NumArgs; ++i) { |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3707 | ExprResult Arg = DefaultVariadicArgumentPromotion(Args[i], CallType, |
| 3708 | FDecl); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3709 | Invalid |= Arg.isInvalid(); |
| 3710 | AllArgs.push_back(Arg.take()); |
| 3711 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3712 | } |
Ted Kremenek | 615eb7c | 2011-09-26 23:36:13 +0000 | [diff] [blame] | 3713 | |
| 3714 | // Check for array bounds violations. |
| 3715 | for (unsigned i = ArgIx; i != NumArgs; ++i) |
| 3716 | CheckArrayAccess(Args[i]); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3717 | } |
Douglas Gregor | 3fd56d7 | 2009-01-23 21:30:56 +0000 | [diff] [blame] | 3718 | return Invalid; |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3719 | } |
| 3720 | |
Peter Collingbourne | 013e5ce | 2011-10-19 00:16:45 +0000 | [diff] [blame] | 3721 | static void DiagnoseCalleeStaticArrayParam(Sema &S, ParmVarDecl *PVD) { |
| 3722 | TypeLoc TL = PVD->getTypeSourceInfo()->getTypeLoc(); |
| 3723 | if (ArrayTypeLoc *ATL = dyn_cast<ArrayTypeLoc>(&TL)) |
| 3724 | S.Diag(PVD->getLocation(), diag::note_callee_static_array) |
| 3725 | << ATL->getLocalSourceRange(); |
| 3726 | } |
| 3727 | |
| 3728 | /// CheckStaticArrayArgument - If the given argument corresponds to a static |
| 3729 | /// array parameter, check that it is non-null, and that if it is formed by |
| 3730 | /// array-to-pointer decay, the underlying array is sufficiently large. |
| 3731 | /// |
| 3732 | /// C99 6.7.5.3p7: If the keyword static also appears within the [ and ] of the |
| 3733 | /// array type derivation, then for each call to the function, the value of the |
| 3734 | /// corresponding actual argument shall provide access to the first element of |
| 3735 | /// an array with at least as many elements as specified by the size expression. |
| 3736 | void |
| 3737 | Sema::CheckStaticArrayArgument(SourceLocation CallLoc, |
| 3738 | ParmVarDecl *Param, |
| 3739 | const Expr *ArgExpr) { |
| 3740 | // Static array parameters are not supported in C++. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3741 | if (!Param || getLangOpts().CPlusPlus) |
Peter Collingbourne | 013e5ce | 2011-10-19 00:16:45 +0000 | [diff] [blame] | 3742 | return; |
| 3743 | |
| 3744 | QualType OrigTy = Param->getOriginalType(); |
| 3745 | |
| 3746 | const ArrayType *AT = Context.getAsArrayType(OrigTy); |
| 3747 | if (!AT || AT->getSizeModifier() != ArrayType::Static) |
| 3748 | return; |
| 3749 | |
| 3750 | if (ArgExpr->isNullPointerConstant(Context, |
| 3751 | Expr::NPC_NeverValueDependent)) { |
| 3752 | Diag(CallLoc, diag::warn_null_arg) << ArgExpr->getSourceRange(); |
| 3753 | DiagnoseCalleeStaticArrayParam(*this, Param); |
| 3754 | return; |
| 3755 | } |
| 3756 | |
| 3757 | const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(AT); |
| 3758 | if (!CAT) |
| 3759 | return; |
| 3760 | |
| 3761 | const ConstantArrayType *ArgCAT = |
| 3762 | Context.getAsConstantArrayType(ArgExpr->IgnoreParenImpCasts()->getType()); |
| 3763 | if (!ArgCAT) |
| 3764 | return; |
| 3765 | |
| 3766 | if (ArgCAT->getSize().ult(CAT->getSize())) { |
| 3767 | Diag(CallLoc, diag::warn_static_array_too_small) |
| 3768 | << ArgExpr->getSourceRange() |
| 3769 | << (unsigned) ArgCAT->getSize().getZExtValue() |
| 3770 | << (unsigned) CAT->getSize().getZExtValue(); |
| 3771 | DiagnoseCalleeStaticArrayParam(*this, Param); |
| 3772 | } |
| 3773 | } |
| 3774 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3775 | /// Given a function expression of unknown-any type, try to rebuild it |
| 3776 | /// to have a function type. |
| 3777 | static ExprResult rebuildUnknownAnyFunction(Sema &S, Expr *fn); |
| 3778 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 3779 | /// ActOnCallExpr - Handle a call to Fn with the specified array of arguments. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 3780 | /// This provides the location of the left/right parens and a list of comma |
| 3781 | /// locations. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3782 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3783 | Sema::ActOnCallExpr(Scope *S, Expr *Fn, SourceLocation LParenLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3784 | MultiExprArg ArgExprs, SourceLocation RParenLoc, |
Peter Collingbourne | 1f24076 | 2011-10-02 23:49:29 +0000 | [diff] [blame] | 3785 | Expr *ExecConfig, bool IsExecConfig) { |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 3786 | // Since this might be a postfix expression, get rid of ParenListExprs. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3787 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, Fn); |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 3788 | if (Result.isInvalid()) return ExprError(); |
| 3789 | Fn = Result.take(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3790 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 3791 | if (getLangOpts().CPlusPlus) { |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3792 | // If this is a pseudo-destructor expression, build the call immediately. |
| 3793 | if (isa<CXXPseudoDestructorExpr>(Fn)) { |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3794 | if (!ArgExprs.empty()) { |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3795 | // Pseudo-destructor calls should not have any arguments. |
| 3796 | Diag(Fn->getLocStart(), diag::err_pseudo_dtor_call_with_args) |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 3797 | << FixItHint::CreateRemoval( |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3798 | SourceRange(ArgExprs[0]->getLocStart(), |
| 3799 | ArgExprs.back()->getLocEnd())); |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3800 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3801 | |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3802 | return Owned(new (Context) CallExpr(Context, Fn, MultiExprArg(), |
| 3803 | Context.VoidTy, VK_RValue, |
| 3804 | RParenLoc)); |
Douglas Gregor | a71d819 | 2009-09-04 17:36:40 +0000 | [diff] [blame] | 3805 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 3806 | |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3807 | // Determine whether this is a dependent call inside a C++ template, |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3808 | // in which case we won't do any semantic analysis now. |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 3809 | // FIXME: Will need to cache the results of name lookup (including ADL) in |
| 3810 | // Fn. |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3811 | bool Dependent = false; |
| 3812 | if (Fn->isTypeDependent()) |
| 3813 | Dependent = true; |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3814 | else if (Expr::hasAnyTypeDependentArguments(ArgExprs)) |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3815 | Dependent = true; |
| 3816 | |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3817 | if (Dependent) { |
| 3818 | if (ExecConfig) { |
| 3819 | return Owned(new (Context) CUDAKernelCallExpr( |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3820 | Context, Fn, cast<CallExpr>(ExecConfig), ArgExprs, |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3821 | Context.DependentTy, VK_RValue, RParenLoc)); |
| 3822 | } else { |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3823 | return Owned(new (Context) CallExpr(Context, Fn, ArgExprs, |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3824 | Context.DependentTy, VK_RValue, |
| 3825 | RParenLoc)); |
| 3826 | } |
| 3827 | } |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3828 | |
| 3829 | // Determine whether this is a call to an object (C++ [over.call.object]). |
| 3830 | if (Fn->getType()->isRecordType()) |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3831 | return Owned(BuildCallToObjectOfClassType(S, Fn, LParenLoc, |
| 3832 | ArgExprs.data(), |
| 3833 | ArgExprs.size(), RParenLoc)); |
Douglas Gregor | 1733001 | 2009-02-04 15:01:18 +0000 | [diff] [blame] | 3834 | |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3835 | if (Fn->getType() == Context.UnknownAnyTy) { |
| 3836 | ExprResult result = rebuildUnknownAnyFunction(*this, Fn); |
| 3837 | if (result.isInvalid()) return ExprError(); |
| 3838 | Fn = result.take(); |
| 3839 | } |
| 3840 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3841 | if (Fn->getType() == Context.BoundMemberTy) { |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3842 | return BuildCallToMemberFunction(S, Fn, LParenLoc, ArgExprs.data(), |
| 3843 | ArgExprs.size(), RParenLoc); |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3844 | } |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3845 | } |
John McCall | 129e2df | 2009-11-30 22:42:35 +0000 | [diff] [blame] | 3846 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3847 | // Check for overloaded calls. This can happen even in C due to extensions. |
| 3848 | if (Fn->getType() == Context.OverloadTy) { |
| 3849 | OverloadExpr::FindResult find = OverloadExpr::find(Fn); |
| 3850 | |
Douglas Gregor | ee697e6 | 2011-10-13 18:10:35 +0000 | [diff] [blame] | 3851 | // We aren't supposed to apply this logic for if there's an '&' involved. |
Douglas Gregor | 64a371f | 2011-10-13 18:26:27 +0000 | [diff] [blame] | 3852 | if (!find.HasFormOfMemberPointer) { |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3853 | OverloadExpr *ovl = find.Expression; |
| 3854 | if (isa<UnresolvedLookupExpr>(ovl)) { |
| 3855 | UnresolvedLookupExpr *ULE = cast<UnresolvedLookupExpr>(ovl); |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3856 | return BuildOverloadedCallExpr(S, Fn, ULE, LParenLoc, ArgExprs.data(), |
| 3857 | ArgExprs.size(), RParenLoc, ExecConfig); |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3858 | } else { |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3859 | return BuildCallToMemberFunction(S, Fn, LParenLoc, ArgExprs.data(), |
| 3860 | ArgExprs.size(), RParenLoc); |
Anders Carlsson | 83ccfc3 | 2009-10-03 17:40:22 +0000 | [diff] [blame] | 3861 | } |
| 3862 | } |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 3863 | } |
| 3864 | |
Douglas Gregor | fa04764 | 2009-02-04 00:32:51 +0000 | [diff] [blame] | 3865 | // If we're directly calling a function, get the appropriate declaration. |
Douglas Gregor | f1d1ca5 | 2011-12-01 01:37:36 +0000 | [diff] [blame] | 3866 | if (Fn->getType() == Context.UnknownAnyTy) { |
| 3867 | ExprResult result = rebuildUnknownAnyFunction(*this, Fn); |
| 3868 | if (result.isInvalid()) return ExprError(); |
| 3869 | Fn = result.take(); |
| 3870 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 3871 | |
Eli Friedman | efa42f7 | 2009-12-26 03:35:45 +0000 | [diff] [blame] | 3872 | Expr *NakedFn = Fn->IgnoreParens(); |
Douglas Gregor | ef9b149 | 2010-11-09 20:03:54 +0000 | [diff] [blame] | 3873 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3874 | NamedDecl *NDecl = 0; |
Douglas Gregor | d8f0ade | 2010-10-25 20:48:33 +0000 | [diff] [blame] | 3875 | if (UnaryOperator *UnOp = dyn_cast<UnaryOperator>(NakedFn)) |
| 3876 | if (UnOp->getOpcode() == UO_AddrOf) |
| 3877 | NakedFn = UnOp->getSubExpr()->IgnoreParens(); |
| 3878 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3879 | if (isa<DeclRefExpr>(NakedFn)) |
| 3880 | NDecl = cast<DeclRefExpr>(NakedFn)->getDecl(); |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 3881 | else if (isa<MemberExpr>(NakedFn)) |
| 3882 | NDecl = cast<MemberExpr>(NakedFn)->getMemberDecl(); |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3883 | |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3884 | return BuildResolvedCallExpr(Fn, NDecl, LParenLoc, ArgExprs.data(), |
| 3885 | ArgExprs.size(), RParenLoc, ExecConfig, |
| 3886 | IsExecConfig); |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3887 | } |
| 3888 | |
| 3889 | ExprResult |
| 3890 | Sema::ActOnCUDAExecConfigExpr(Scope *S, SourceLocation LLLLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3891 | MultiExprArg ExecConfig, SourceLocation GGGLoc) { |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3892 | FunctionDecl *ConfigDecl = Context.getcudaConfigureCallDecl(); |
| 3893 | if (!ConfigDecl) |
| 3894 | return ExprError(Diag(LLLLoc, diag::err_undeclared_var_use) |
| 3895 | << "cudaConfigureCall"); |
| 3896 | QualType ConfigQTy = ConfigDecl->getType(); |
| 3897 | |
| 3898 | DeclRefExpr *ConfigDR = new (Context) DeclRefExpr( |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 3899 | ConfigDecl, false, ConfigQTy, VK_LValue, LLLLoc); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 3900 | MarkFunctionReferenced(LLLLoc, ConfigDecl); |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3901 | |
Peter Collingbourne | 1f24076 | 2011-10-02 23:49:29 +0000 | [diff] [blame] | 3902 | return ActOnCallExpr(S, ConfigDR, LLLLoc, ExecConfig, GGGLoc, 0, |
| 3903 | /*IsExecConfig=*/true); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3904 | } |
| 3905 | |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 3906 | /// ActOnAsTypeExpr - create a new asType (bitcast) from the arguments. |
| 3907 | /// |
| 3908 | /// __builtin_astype( value, dst type ) |
| 3909 | /// |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3910 | ExprResult Sema::ActOnAsTypeExpr(Expr *E, ParsedType ParsedDestTy, |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 3911 | SourceLocation BuiltinLoc, |
| 3912 | SourceLocation RParenLoc) { |
| 3913 | ExprValueKind VK = VK_RValue; |
| 3914 | ExprObjectKind OK = OK_Ordinary; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3915 | QualType DstTy = GetTypeFromParser(ParsedDestTy); |
| 3916 | QualType SrcTy = E->getType(); |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 3917 | if (Context.getTypeSize(DstTy) != Context.getTypeSize(SrcTy)) |
| 3918 | return ExprError(Diag(BuiltinLoc, |
| 3919 | diag::err_invalid_astype_of_different_size) |
Peter Collingbourne | af9cddf | 2011-06-08 15:15:17 +0000 | [diff] [blame] | 3920 | << DstTy |
| 3921 | << SrcTy |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 3922 | << E->getSourceRange()); |
| 3923 | return Owned(new (Context) AsTypeExpr(E, DstTy, VK, OK, BuiltinLoc, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 3924 | RParenLoc)); |
Tanya Lattner | 61eee0c | 2011-06-04 00:47:47 +0000 | [diff] [blame] | 3925 | } |
| 3926 | |
John McCall | 3b4294e | 2009-12-16 12:17:52 +0000 | [diff] [blame] | 3927 | /// BuildResolvedCallExpr - Build a call to a resolved expression, |
| 3928 | /// i.e. an expression not of \p OverloadTy. The expression should |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3929 | /// unary-convert to an expression of function-pointer or |
| 3930 | /// block-pointer type. |
| 3931 | /// |
| 3932 | /// \param NDecl the declaration being called, if available |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 3933 | ExprResult |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3934 | Sema::BuildResolvedCallExpr(Expr *Fn, NamedDecl *NDecl, |
| 3935 | SourceLocation LParenLoc, |
| 3936 | Expr **Args, unsigned NumArgs, |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3937 | SourceLocation RParenLoc, |
Peter Collingbourne | 1f24076 | 2011-10-02 23:49:29 +0000 | [diff] [blame] | 3938 | Expr *Config, bool IsExecConfig) { |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3939 | FunctionDecl *FDecl = dyn_cast_or_null<FunctionDecl>(NDecl); |
Eli Friedman | a6c66ce | 2012-08-31 00:14:07 +0000 | [diff] [blame^] | 3940 | unsigned BuiltinID = (FDecl ? FDecl->getBuiltinID() : 0); |
John McCall | aa81e16 | 2009-12-01 22:10:20 +0000 | [diff] [blame] | 3941 | |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 3942 | // Promote the function operand. |
Eli Friedman | a6c66ce | 2012-08-31 00:14:07 +0000 | [diff] [blame^] | 3943 | // We special-case function promotion here because we only allow promoting |
| 3944 | // builtin functions to function pointers in the callee of a call. |
| 3945 | ExprResult Result; |
| 3946 | if (BuiltinID && |
| 3947 | Fn->getType()->isSpecificBuiltinType(BuiltinType::BuiltinFn)) { |
| 3948 | Result = ImpCastExprToType(Fn, Context.getPointerType(FDecl->getType()), |
| 3949 | CK_BuiltinFnToFnPtr).take(); |
| 3950 | } else { |
| 3951 | Result = UsualUnaryConversions(Fn); |
| 3952 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 3953 | if (Result.isInvalid()) |
| 3954 | return ExprError(); |
| 3955 | Fn = Result.take(); |
Chris Lattner | 0442108 | 2008-04-08 04:40:51 +0000 | [diff] [blame] | 3956 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 3957 | // Make the call expr early, before semantic checks. This guarantees cleanup |
| 3958 | // of arguments and function on error. |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3959 | CallExpr *TheCall; |
Eric Christopher | a27cb25 | 2012-05-30 01:14:28 +0000 | [diff] [blame] | 3960 | if (Config) |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3961 | TheCall = new (Context) CUDAKernelCallExpr(Context, Fn, |
| 3962 | cast<CallExpr>(Config), |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3963 | llvm::makeArrayRef(Args,NumArgs), |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3964 | Context.BoolTy, |
| 3965 | VK_RValue, |
| 3966 | RParenLoc); |
Eric Christopher | a27cb25 | 2012-05-30 01:14:28 +0000 | [diff] [blame] | 3967 | else |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3968 | TheCall = new (Context) CallExpr(Context, Fn, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 3969 | llvm::makeArrayRef(Args, NumArgs), |
Peter Collingbourne | e08ce65 | 2011-02-09 21:07:24 +0000 | [diff] [blame] | 3970 | Context.BoolTy, |
| 3971 | VK_RValue, |
| 3972 | RParenLoc); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 3973 | |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 3974 | // Bail out early if calling a builtin with custom typechecking. |
| 3975 | if (BuiltinID && Context.BuiltinInfo.hasCustomTypechecking(BuiltinID)) |
| 3976 | return CheckBuiltinFunctionCall(BuiltinID, TheCall); |
| 3977 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 3978 | retry: |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 3979 | const FunctionType *FuncT; |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 3980 | if (const PointerType *PT = Fn->getType()->getAs<PointerType>()) { |
Steve Naroff | dd972f2 | 2008-09-05 22:11:13 +0000 | [diff] [blame] | 3981 | // C99 6.5.2.2p1 - "The expression that denotes the called function shall |
| 3982 | // have type pointer to function". |
John McCall | 183700f | 2009-09-21 23:43:11 +0000 | [diff] [blame] | 3983 | FuncT = PT->getPointeeType()->getAs<FunctionType>(); |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 3984 | if (FuncT == 0) |
| 3985 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 3986 | << Fn->getType() << Fn->getSourceRange()); |
| 3987 | } else if (const BlockPointerType *BPT = |
| 3988 | Fn->getType()->getAs<BlockPointerType>()) { |
| 3989 | FuncT = BPT->getPointeeType()->castAs<FunctionType>(); |
| 3990 | } else { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 3991 | // Handle calls to expressions of unknown-any type. |
| 3992 | if (Fn->getType() == Context.UnknownAnyTy) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 3993 | ExprResult rewrite = rebuildUnknownAnyFunction(*this, Fn); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 3994 | if (rewrite.isInvalid()) return ExprError(); |
| 3995 | Fn = rewrite.take(); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 3996 | TheCall->setCallee(Fn); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 3997 | goto retry; |
| 3998 | } |
| 3999 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4000 | return ExprError(Diag(LParenLoc, diag::err_typecheck_call_not_function) |
| 4001 | << Fn->getType() << Fn->getSourceRange()); |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 4002 | } |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4003 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4004 | if (getLangOpts().CUDA) { |
Peter Collingbourne | 0423fc6 | 2011-02-23 01:53:29 +0000 | [diff] [blame] | 4005 | if (Config) { |
| 4006 | // CUDA: Kernel calls must be to global functions |
| 4007 | if (FDecl && !FDecl->hasAttr<CUDAGlobalAttr>()) |
| 4008 | return ExprError(Diag(LParenLoc,diag::err_kern_call_not_global_function) |
| 4009 | << FDecl->getName() << Fn->getSourceRange()); |
| 4010 | |
| 4011 | // CUDA: Kernel function must have 'void' return type |
| 4012 | if (!FuncT->getResultType()->isVoidType()) |
| 4013 | return ExprError(Diag(LParenLoc, diag::err_kern_type_not_void_return) |
| 4014 | << Fn->getType() << Fn->getSourceRange()); |
Peter Collingbourne | 8591a7f | 2011-10-02 23:49:15 +0000 | [diff] [blame] | 4015 | } else { |
| 4016 | // CUDA: Calls to global functions must be configured |
| 4017 | if (FDecl && FDecl->hasAttr<CUDAGlobalAttr>()) |
| 4018 | return ExprError(Diag(LParenLoc, diag::err_global_call_not_config) |
| 4019 | << FDecl->getName() << Fn->getSourceRange()); |
Peter Collingbourne | 0423fc6 | 2011-02-23 01:53:29 +0000 | [diff] [blame] | 4020 | } |
| 4021 | } |
| 4022 | |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 4023 | // Check for a valid return type |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4024 | if (CheckCallReturnType(FuncT->getResultType(), |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4025 | Fn->getLocStart(), TheCall, |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 4026 | FDecl)) |
Eli Friedman | e7c6f7a | 2009-03-22 22:00:50 +0000 | [diff] [blame] | 4027 | return ExprError(); |
| 4028 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 4029 | // We know the result type of the call, set it. |
Douglas Gregor | 5291c3c | 2010-07-13 08:18:22 +0000 | [diff] [blame] | 4030 | TheCall->setType(FuncT->getCallResultType(Context)); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4031 | TheCall->setValueKind(Expr::getValueKindForType(FuncT->getResultType())); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4032 | |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 4033 | const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FuncT); |
| 4034 | if (Proto) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4035 | if (ConvertArgumentsForCall(TheCall, Fn, FDecl, Proto, Args, NumArgs, |
Peter Collingbourne | 1f24076 | 2011-10-02 23:49:29 +0000 | [diff] [blame] | 4036 | RParenLoc, IsExecConfig)) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4037 | return ExprError(); |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 4038 | } else { |
Douglas Gregor | 72564e7 | 2009-02-26 23:50:07 +0000 | [diff] [blame] | 4039 | assert(isa<FunctionNoProtoType>(FuncT) && "Unknown FunctionType!"); |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4040 | |
Douglas Gregor | 74734d5 | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 4041 | if (FDecl) { |
| 4042 | // Check if we have too few/too many template arguments, based |
| 4043 | // on our knowledge of the function definition. |
| 4044 | const FunctionDecl *Def = 0; |
Argyrios Kyrtzidis | 06a54a3 | 2010-07-07 11:31:19 +0000 | [diff] [blame] | 4045 | if (FDecl->hasBody(Def) && NumArgs != Def->param_size()) { |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 4046 | Proto = Def->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 4047 | if (!Proto || !(Proto->isVariadic() && NumArgs >= Def->param_size())) |
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 4048 | Diag(RParenLoc, diag::warn_call_wrong_number_of_arguments) |
| 4049 | << (NumArgs > Def->param_size()) << FDecl << Fn->getSourceRange(); |
Eli Friedman | bc4e29f | 2009-06-01 09:24:59 +0000 | [diff] [blame] | 4050 | } |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 4051 | |
| 4052 | // If the function we're calling isn't a function prototype, but we have |
| 4053 | // a function prototype from a prior declaratiom, use that prototype. |
| 4054 | if (!FDecl->hasPrototype()) |
| 4055 | Proto = FDecl->getType()->getAs<FunctionProtoType>(); |
Douglas Gregor | 74734d5 | 2009-04-02 15:37:10 +0000 | [diff] [blame] | 4056 | } |
| 4057 | |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 4058 | // Promote the arguments (C99 6.5.2.2p6). |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 4059 | for (unsigned i = 0; i != NumArgs; i++) { |
| 4060 | Expr *Arg = Args[i]; |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 4061 | |
| 4062 | if (Proto && i < Proto->getNumArgs()) { |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 4063 | InitializedEntity Entity |
| 4064 | = InitializedEntity::InitializeParameter(Context, |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4065 | Proto->getArgType(i), |
| 4066 | Proto->isArgConsumed(i)); |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 4067 | ExprResult ArgE = PerformCopyInitialization(Entity, |
| 4068 | SourceLocation(), |
| 4069 | Owned(Arg)); |
| 4070 | if (ArgE.isInvalid()) |
| 4071 | return true; |
| 4072 | |
| 4073 | Arg = ArgE.takeAs<Expr>(); |
| 4074 | |
| 4075 | } else { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4076 | ExprResult ArgE = DefaultArgumentPromotion(Arg); |
| 4077 | |
| 4078 | if (ArgE.isInvalid()) |
| 4079 | return true; |
| 4080 | |
| 4081 | Arg = ArgE.takeAs<Expr>(); |
Douglas Gregor | 4654241 | 2010-10-25 20:39:23 +0000 | [diff] [blame] | 4082 | } |
| 4083 | |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 4084 | if (RequireCompleteType(Arg->getLocStart(), |
Douglas Gregor | 0700bbf | 2010-10-26 05:45:40 +0000 | [diff] [blame] | 4085 | Arg->getType(), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4086 | diag::err_call_incomplete_argument, Arg)) |
Douglas Gregor | 0700bbf | 2010-10-26 05:45:40 +0000 | [diff] [blame] | 4087 | return ExprError(); |
| 4088 | |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 4089 | TheCall->setArg(i, Arg); |
Steve Naroff | b291ab6 | 2007-08-28 23:30:39 +0000 | [diff] [blame] | 4090 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4091 | } |
Chris Lattner | 925e60d | 2007-12-28 05:29:59 +0000 | [diff] [blame] | 4092 | |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4093 | if (CXXMethodDecl *Method = dyn_cast_or_null<CXXMethodDecl>(FDecl)) |
| 4094 | if (!Method->isStatic()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 4095 | return ExprError(Diag(LParenLoc, diag::err_member_call_without_object) |
| 4096 | << Fn->getSourceRange()); |
Douglas Gregor | 88a3514 | 2008-12-22 05:46:06 +0000 | [diff] [blame] | 4097 | |
Fariborz Jahanian | daf0415 | 2009-05-15 20:33:25 +0000 | [diff] [blame] | 4098 | // Check for sentinels |
| 4099 | if (NDecl) |
| 4100 | DiagnoseSentinelCalls(NDecl, LParenLoc, Args, NumArgs); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4101 | |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 4102 | // Do special checking on direct calls to functions. |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 4103 | if (FDecl) { |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 4104 | if (CheckFunctionCall(FDecl, TheCall, Proto)) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 4105 | return ExprError(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4106 | |
John McCall | 8e10f3b | 2011-02-26 05:39:39 +0000 | [diff] [blame] | 4107 | if (BuiltinID) |
Fariborz Jahanian | 67aba81 | 2010-11-30 17:35:24 +0000 | [diff] [blame] | 4108 | return CheckBuiltinFunctionCall(BuiltinID, TheCall); |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 4109 | } else if (NDecl) { |
Richard Smith | 831421f | 2012-06-25 20:30:08 +0000 | [diff] [blame] | 4110 | if (CheckBlockCall(NDecl, TheCall, Proto)) |
Anders Carlsson | d406bf0 | 2009-08-16 01:56:34 +0000 | [diff] [blame] | 4111 | return ExprError(); |
| 4112 | } |
Chris Lattner | 59907c4 | 2007-08-10 20:18:51 +0000 | [diff] [blame] | 4113 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4114 | return MaybeBindToTemporary(TheCall); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4115 | } |
| 4116 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4117 | ExprResult |
John McCall | b3d8748 | 2010-08-24 05:47:05 +0000 | [diff] [blame] | 4118 | Sema::ActOnCompoundLiteral(SourceLocation LParenLoc, ParsedType Ty, |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4119 | SourceLocation RParenLoc, Expr *InitExpr) { |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 4120 | assert((Ty != 0) && "ActOnCompoundLiteral(): missing type"); |
Steve Naroff | aff1edd | 2007-07-19 21:32:11 +0000 | [diff] [blame] | 4121 | // FIXME: put back this assert when initializers are worked out. |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 4122 | //assert((InitExpr != 0) && "ActOnCompoundLiteral(): missing expression"); |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 4123 | |
| 4124 | TypeSourceInfo *TInfo; |
| 4125 | QualType literalType = GetTypeFromParser(Ty, &TInfo); |
| 4126 | if (!TInfo) |
| 4127 | TInfo = Context.getTrivialTypeSourceInfo(literalType); |
| 4128 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4129 | return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, InitExpr); |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 4130 | } |
| 4131 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4132 | ExprResult |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 4133 | Sema::BuildCompoundLiteralExpr(SourceLocation LParenLoc, TypeSourceInfo *TInfo, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4134 | SourceLocation RParenLoc, Expr *LiteralExpr) { |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 4135 | QualType literalType = TInfo->getType(); |
Anders Carlsson | d35c832 | 2007-12-05 07:24:19 +0000 | [diff] [blame] | 4136 | |
Eli Friedman | 6223c22 | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 4137 | if (literalType->isArrayType()) { |
Argyrios Kyrtzidis | e6fe9a2 | 2010-11-08 19:14:19 +0000 | [diff] [blame] | 4138 | if (RequireCompleteType(LParenLoc, Context.getBaseElementType(literalType), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4139 | diag::err_illegal_decl_array_incomplete_type, |
| 4140 | SourceRange(LParenLoc, |
| 4141 | LiteralExpr->getSourceRange().getEnd()))) |
Argyrios Kyrtzidis | e6fe9a2 | 2010-11-08 19:14:19 +0000 | [diff] [blame] | 4142 | return ExprError(); |
Chris Lattner | c63a1f2 | 2008-08-04 07:31:14 +0000 | [diff] [blame] | 4143 | if (literalType->isVariableArrayType()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4144 | return ExprError(Diag(LParenLoc, diag::err_variable_object_no_init) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4145 | << SourceRange(LParenLoc, LiteralExpr->getSourceRange().getEnd())); |
Douglas Gregor | 690dc7f | 2009-05-21 23:48:18 +0000 | [diff] [blame] | 4146 | } else if (!literalType->isDependentType() && |
| 4147 | RequireCompleteType(LParenLoc, literalType, |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 4148 | diag::err_typecheck_decl_incomplete_type, |
| 4149 | SourceRange(LParenLoc, LiteralExpr->getSourceRange().getEnd()))) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4150 | return ExprError(); |
Eli Friedman | 6223c22 | 2008-05-20 05:22:08 +0000 | [diff] [blame] | 4151 | |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4152 | InitializedEntity Entity |
Douglas Gregor | d6542d8 | 2009-12-22 15:35:07 +0000 | [diff] [blame] | 4153 | = InitializedEntity::InitializeTemporary(literalType); |
Douglas Gregor | 99a2e60 | 2009-12-16 01:38:02 +0000 | [diff] [blame] | 4154 | InitializationKind Kind |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 4155 | = InitializationKind::CreateCStyleCast(LParenLoc, |
Sebastian Redl | 3a45c0e | 2012-02-12 16:37:36 +0000 | [diff] [blame] | 4156 | SourceRange(LParenLoc, RParenLoc), |
| 4157 | /*InitList=*/true); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4158 | InitializationSequence InitSeq(*this, Entity, Kind, &LiteralExpr, 1); |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 4159 | ExprResult Result = InitSeq.Perform(*this, Entity, Kind, LiteralExpr, |
| 4160 | &literalType); |
Eli Friedman | 0854462 | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 4161 | if (Result.isInvalid()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4162 | return ExprError(); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4163 | LiteralExpr = Result.get(); |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 4164 | |
Chris Lattner | 371f258 | 2008-12-04 23:50:19 +0000 | [diff] [blame] | 4165 | bool isFileScope = getCurFunctionOrMethodDecl() == 0; |
Steve Naroff | e9b1219 | 2008-01-14 18:19:28 +0000 | [diff] [blame] | 4166 | if (isFileScope) { // 6.5.2.5p3 |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4167 | if (CheckForConstantInitializer(LiteralExpr, literalType)) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4168 | return ExprError(); |
Steve Naroff | d0091aa | 2008-01-10 22:15:12 +0000 | [diff] [blame] | 4169 | } |
Eli Friedman | 0854462 | 2009-12-22 02:35:53 +0000 | [diff] [blame] | 4170 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4171 | // In C, compound literals are l-values for some reason. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4172 | ExprValueKind VK = getLangOpts().CPlusPlus ? VK_RValue : VK_LValue; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4173 | |
Douglas Gregor | 751ec9b | 2011-06-17 04:59:12 +0000 | [diff] [blame] | 4174 | return MaybeBindToTemporary( |
| 4175 | new (Context) CompoundLiteralExpr(LParenLoc, TInfo, literalType, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4176 | VK, LiteralExpr, isFileScope)); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 4177 | } |
| 4178 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4179 | ExprResult |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4180 | Sema::ActOnInitList(SourceLocation LBraceLoc, MultiExprArg InitArgList, |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4181 | SourceLocation RBraceLoc) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 4182 | // Immediately handle non-overload placeholders. Overloads can be |
| 4183 | // resolved contextually, but everything else here can't. |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 4184 | for (unsigned I = 0, E = InitArgList.size(); I != E; ++I) { |
| 4185 | if (InitArgList[I]->getType()->isNonOverloadPlaceholderType()) { |
| 4186 | ExprResult result = CheckPlaceholderExpr(InitArgList[I]); |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 4187 | |
| 4188 | // Ignore failures; dropping the entire initializer list because |
| 4189 | // of one failure would be terrible for indexing/etc. |
| 4190 | if (result.isInvalid()) continue; |
| 4191 | |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 4192 | InitArgList[I] = result.take(); |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 4193 | } |
| 4194 | } |
| 4195 | |
Steve Naroff | 08d92e4 | 2007-09-15 18:49:24 +0000 | [diff] [blame] | 4196 | // Semantic analysis for initializers is done by ActOnDeclarator() and |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4197 | // CheckInitializer() - it requires knowledge of the object being intialized. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4198 | |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 4199 | InitListExpr *E = new (Context) InitListExpr(Context, LBraceLoc, InitArgList, |
| 4200 | RBraceLoc); |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 4201 | E->setType(Context.VoidTy); // FIXME: just a place holder for now. |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4202 | return Owned(E); |
Steve Naroff | 4aa88f8 | 2007-07-19 01:06:55 +0000 | [diff] [blame] | 4203 | } |
| 4204 | |
John McCall | dc05b11 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 4205 | /// Do an explicit extend of the given block pointer if we're in ARC. |
| 4206 | static void maybeExtendBlockObject(Sema &S, ExprResult &E) { |
| 4207 | assert(E.get()->getType()->isBlockPointerType()); |
| 4208 | assert(E.get()->isRValue()); |
| 4209 | |
| 4210 | // Only do this in an r-value context. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4211 | if (!S.getLangOpts().ObjCAutoRefCount) return; |
John McCall | dc05b11 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 4212 | |
| 4213 | E = ImplicitCastExpr::Create(S.Context, E.get()->getType(), |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 4214 | CK_ARCExtendBlockObject, E.get(), |
John McCall | dc05b11 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 4215 | /*base path*/ 0, VK_RValue); |
| 4216 | S.ExprNeedsCleanups = true; |
| 4217 | } |
| 4218 | |
| 4219 | /// Prepare a conversion of the given expression to an ObjC object |
| 4220 | /// pointer type. |
| 4221 | CastKind Sema::PrepareCastToObjCObjectPointer(ExprResult &E) { |
| 4222 | QualType type = E.get()->getType(); |
| 4223 | if (type->isObjCObjectPointerType()) { |
| 4224 | return CK_BitCast; |
| 4225 | } else if (type->isBlockPointerType()) { |
| 4226 | maybeExtendBlockObject(*this, E); |
| 4227 | return CK_BlockPointerToObjCPointerCast; |
| 4228 | } else { |
| 4229 | assert(type->isPointerType()); |
| 4230 | return CK_CPointerToObjCPointerCast; |
| 4231 | } |
| 4232 | } |
| 4233 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4234 | /// Prepares for a scalar cast, performing all the necessary stages |
| 4235 | /// except the final cast and returning the kind required. |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4236 | CastKind Sema::PrepareScalarCast(ExprResult &Src, QualType DestTy) { |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4237 | // Both Src and Dest are scalar types, i.e. arithmetic or pointer. |
| 4238 | // Also, callers should have filtered out the invalid cases with |
| 4239 | // pointers. Everything else should be possible. |
| 4240 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4241 | QualType SrcTy = Src.get()->getType(); |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4242 | if (Context.hasSameUnqualifiedType(SrcTy, DestTy)) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4243 | return CK_NoOp; |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 4244 | |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4245 | switch (Type::ScalarTypeKind SrcKind = SrcTy->getScalarTypeKind()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4246 | case Type::STK_MemberPointer: |
| 4247 | llvm_unreachable("member pointer type in C"); |
Abramo Bagnara | bb03f5d | 2011-01-04 09:50:03 +0000 | [diff] [blame] | 4248 | |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4249 | case Type::STK_CPointer: |
| 4250 | case Type::STK_BlockPointer: |
| 4251 | case Type::STK_ObjCObjectPointer: |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4252 | switch (DestTy->getScalarTypeKind()) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4253 | case Type::STK_CPointer: |
| 4254 | return CK_BitCast; |
| 4255 | case Type::STK_BlockPointer: |
| 4256 | return (SrcKind == Type::STK_BlockPointer |
| 4257 | ? CK_BitCast : CK_AnyPointerToBlockPointerCast); |
| 4258 | case Type::STK_ObjCObjectPointer: |
| 4259 | if (SrcKind == Type::STK_ObjCObjectPointer) |
| 4260 | return CK_BitCast; |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4261 | if (SrcKind == Type::STK_CPointer) |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4262 | return CK_CPointerToObjCPointerCast; |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4263 | maybeExtendBlockObject(*this, Src); |
| 4264 | return CK_BlockPointerToObjCPointerCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4265 | case Type::STK_Bool: |
| 4266 | return CK_PointerToBoolean; |
| 4267 | case Type::STK_Integral: |
| 4268 | return CK_PointerToIntegral; |
| 4269 | case Type::STK_Floating: |
| 4270 | case Type::STK_FloatingComplex: |
| 4271 | case Type::STK_IntegralComplex: |
| 4272 | case Type::STK_MemberPointer: |
| 4273 | llvm_unreachable("illegal cast from pointer"); |
| 4274 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4275 | llvm_unreachable("Should have returned before this"); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4276 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4277 | case Type::STK_Bool: // casting from bool is like casting from an integer |
| 4278 | case Type::STK_Integral: |
| 4279 | switch (DestTy->getScalarTypeKind()) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4280 | case Type::STK_CPointer: |
| 4281 | case Type::STK_ObjCObjectPointer: |
| 4282 | case Type::STK_BlockPointer: |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4283 | if (Src.get()->isNullPointerConstant(Context, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4284 | Expr::NPC_ValueDependentIsNull)) |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 4285 | return CK_NullToPointer; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4286 | return CK_IntegralToPointer; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4287 | case Type::STK_Bool: |
| 4288 | return CK_IntegralToBoolean; |
| 4289 | case Type::STK_Integral: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4290 | return CK_IntegralCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4291 | case Type::STK_Floating: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4292 | return CK_IntegralToFloating; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4293 | case Type::STK_IntegralComplex: |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4294 | Src = ImpCastExprToType(Src.take(), |
| 4295 | DestTy->castAs<ComplexType>()->getElementType(), |
| 4296 | CK_IntegralCast); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4297 | return CK_IntegralRealToComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4298 | case Type::STK_FloatingComplex: |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4299 | Src = ImpCastExprToType(Src.take(), |
| 4300 | DestTy->castAs<ComplexType>()->getElementType(), |
| 4301 | CK_IntegralToFloating); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4302 | return CK_FloatingRealToComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4303 | case Type::STK_MemberPointer: |
| 4304 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4305 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4306 | llvm_unreachable("Should have returned before this"); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4307 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4308 | case Type::STK_Floating: |
| 4309 | switch (DestTy->getScalarTypeKind()) { |
| 4310 | case Type::STK_Floating: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4311 | return CK_FloatingCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4312 | case Type::STK_Bool: |
| 4313 | return CK_FloatingToBoolean; |
| 4314 | case Type::STK_Integral: |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4315 | return CK_FloatingToIntegral; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4316 | case Type::STK_FloatingComplex: |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4317 | Src = ImpCastExprToType(Src.take(), |
| 4318 | DestTy->castAs<ComplexType>()->getElementType(), |
| 4319 | CK_FloatingCast); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4320 | return CK_FloatingRealToComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4321 | case Type::STK_IntegralComplex: |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4322 | Src = ImpCastExprToType(Src.take(), |
| 4323 | DestTy->castAs<ComplexType>()->getElementType(), |
| 4324 | CK_FloatingToIntegral); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4325 | return CK_IntegralRealToComplex; |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4326 | case Type::STK_CPointer: |
| 4327 | case Type::STK_ObjCObjectPointer: |
| 4328 | case Type::STK_BlockPointer: |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4329 | llvm_unreachable("valid float->pointer cast?"); |
| 4330 | case Type::STK_MemberPointer: |
| 4331 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4332 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4333 | llvm_unreachable("Should have returned before this"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4334 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4335 | case Type::STK_FloatingComplex: |
| 4336 | switch (DestTy->getScalarTypeKind()) { |
| 4337 | case Type::STK_FloatingComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4338 | return CK_FloatingComplexCast; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4339 | case Type::STK_IntegralComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4340 | return CK_FloatingComplexToIntegralComplex; |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4341 | case Type::STK_Floating: { |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4342 | QualType ET = SrcTy->castAs<ComplexType>()->getElementType(); |
| 4343 | if (Context.hasSameType(ET, DestTy)) |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4344 | return CK_FloatingComplexToReal; |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4345 | Src = ImpCastExprToType(Src.take(), ET, CK_FloatingComplexToReal); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4346 | return CK_FloatingCast; |
| 4347 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4348 | case Type::STK_Bool: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4349 | return CK_FloatingComplexToBoolean; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4350 | case Type::STK_Integral: |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4351 | Src = ImpCastExprToType(Src.take(), |
| 4352 | SrcTy->castAs<ComplexType>()->getElementType(), |
| 4353 | CK_FloatingComplexToReal); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4354 | return CK_FloatingToIntegral; |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4355 | case Type::STK_CPointer: |
| 4356 | case Type::STK_ObjCObjectPointer: |
| 4357 | case Type::STK_BlockPointer: |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4358 | llvm_unreachable("valid complex float->pointer cast?"); |
| 4359 | case Type::STK_MemberPointer: |
| 4360 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4361 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4362 | llvm_unreachable("Should have returned before this"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4363 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4364 | case Type::STK_IntegralComplex: |
| 4365 | switch (DestTy->getScalarTypeKind()) { |
| 4366 | case Type::STK_FloatingComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4367 | return CK_IntegralComplexToFloatingComplex; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4368 | case Type::STK_IntegralComplex: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4369 | return CK_IntegralComplexCast; |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4370 | case Type::STK_Integral: { |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4371 | QualType ET = SrcTy->castAs<ComplexType>()->getElementType(); |
| 4372 | if (Context.hasSameType(ET, DestTy)) |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4373 | return CK_IntegralComplexToReal; |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4374 | Src = ImpCastExprToType(Src.take(), ET, CK_IntegralComplexToReal); |
John McCall | 8786da7 | 2010-12-14 17:51:41 +0000 | [diff] [blame] | 4375 | return CK_IntegralCast; |
| 4376 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4377 | case Type::STK_Bool: |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4378 | return CK_IntegralComplexToBoolean; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4379 | case Type::STK_Floating: |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4380 | Src = ImpCastExprToType(Src.take(), |
| 4381 | SrcTy->castAs<ComplexType>()->getElementType(), |
| 4382 | CK_IntegralComplexToReal); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4383 | return CK_IntegralToFloating; |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4384 | case Type::STK_CPointer: |
| 4385 | case Type::STK_ObjCObjectPointer: |
| 4386 | case Type::STK_BlockPointer: |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 4387 | llvm_unreachable("valid complex int->pointer cast?"); |
| 4388 | case Type::STK_MemberPointer: |
| 4389 | llvm_unreachable("member pointer type in C"); |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4390 | } |
David Blaikie | 7530c03 | 2012-01-17 06:56:22 +0000 | [diff] [blame] | 4391 | llvm_unreachable("Should have returned before this"); |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 4392 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4393 | |
John McCall | f3ea8cf | 2010-11-14 08:17:51 +0000 | [diff] [blame] | 4394 | llvm_unreachable("Unhandled scalar cast"); |
Anders Carlsson | 82debc7 | 2009-10-18 18:12:03 +0000 | [diff] [blame] | 4395 | } |
| 4396 | |
Anders Carlsson | c351632 | 2009-10-16 02:48:28 +0000 | [diff] [blame] | 4397 | bool Sema::CheckVectorCast(SourceRange R, QualType VectorTy, QualType Ty, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4398 | CastKind &Kind) { |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4399 | assert(VectorTy->isVectorType() && "Not a vector type!"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4400 | |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4401 | if (Ty->isVectorType() || Ty->isIntegerType()) { |
Chris Lattner | 98be494 | 2008-03-05 18:54:05 +0000 | [diff] [blame] | 4402 | if (Context.getTypeSize(VectorTy) != Context.getTypeSize(Ty)) |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4403 | return Diag(R.getBegin(), |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4404 | Ty->isVectorType() ? |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4405 | diag::err_invalid_conversion_between_vectors : |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4406 | diag::err_invalid_conversion_between_vector_and_integer) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4407 | << VectorTy << Ty << R; |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4408 | } else |
| 4409 | return Diag(R.getBegin(), |
Chris Lattner | fa25bbb | 2008-11-19 05:08:23 +0000 | [diff] [blame] | 4410 | diag::err_invalid_conversion_between_vector_and_scalar) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 4411 | << VectorTy << Ty << R; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4412 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4413 | Kind = CK_BitCast; |
Anders Carlsson | a64db8f | 2007-11-27 05:51:55 +0000 | [diff] [blame] | 4414 | return false; |
| 4415 | } |
| 4416 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4417 | ExprResult Sema::CheckExtVectorCast(SourceRange R, QualType DestTy, |
| 4418 | Expr *CastExpr, CastKind &Kind) { |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4419 | assert(DestTy->isExtVectorType() && "Not an extended vector type!"); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4420 | |
Anders Carlsson | 16a8904 | 2009-10-16 05:23:41 +0000 | [diff] [blame] | 4421 | QualType SrcTy = CastExpr->getType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4422 | |
Nate Begeman | 9b10da6 | 2009-06-27 22:05:55 +0000 | [diff] [blame] | 4423 | // If SrcTy is a VectorType, the total size must match to explicitly cast to |
| 4424 | // an ExtVectorType. |
Tobias Grosser | 9df05ea | 2011-09-22 13:03:14 +0000 | [diff] [blame] | 4425 | // In OpenCL, casts between vectors of different types are not allowed. |
| 4426 | // (See OpenCL 6.2). |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4427 | if (SrcTy->isVectorType()) { |
Tobias Grosser | 9df05ea | 2011-09-22 13:03:14 +0000 | [diff] [blame] | 4428 | if (Context.getTypeSize(DestTy) != Context.getTypeSize(SrcTy) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4429 | || (getLangOpts().OpenCL && |
Tobias Grosser | 9df05ea | 2011-09-22 13:03:14 +0000 | [diff] [blame] | 4430 | (DestTy.getCanonicalType() != SrcTy.getCanonicalType()))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4431 | Diag(R.getBegin(),diag::err_invalid_conversion_between_ext_vectors) |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4432 | << DestTy << SrcTy << R; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4433 | return ExprError(); |
| 4434 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4435 | Kind = CK_BitCast; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4436 | return Owned(CastExpr); |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4437 | } |
| 4438 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4439 | // All non-pointer scalars can be cast to ExtVector type. The appropriate |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4440 | // conversion will take place first from scalar to elt type, and then |
| 4441 | // splat from elt type to vector. |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 4442 | if (SrcTy->isPointerType()) |
| 4443 | return Diag(R.getBegin(), |
| 4444 | diag::err_invalid_conversion_between_vector_and_scalar) |
| 4445 | << DestTy << SrcTy << R; |
Eli Friedman | 73c39ab | 2009-10-20 08:27:19 +0000 | [diff] [blame] | 4446 | |
| 4447 | QualType DestElemTy = DestTy->getAs<ExtVectorType>()->getElementType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4448 | ExprResult CastExprRes = Owned(CastExpr); |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4449 | CastKind CK = PrepareScalarCast(CastExprRes, DestElemTy); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4450 | if (CastExprRes.isInvalid()) |
| 4451 | return ExprError(); |
| 4452 | CastExpr = ImpCastExprToType(CastExprRes.take(), DestElemTy, CK).take(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4453 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 4454 | Kind = CK_VectorSplat; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4455 | return Owned(CastExpr); |
Nate Begeman | 58d29a4 | 2009-06-26 00:50:28 +0000 | [diff] [blame] | 4456 | } |
| 4457 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4458 | ExprResult |
Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 4459 | Sema::ActOnCastExpr(Scope *S, SourceLocation LParenLoc, |
| 4460 | Declarator &D, ParsedType &Ty, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4461 | SourceLocation RParenLoc, Expr *CastExpr) { |
| 4462 | assert(!D.isInvalidType() && (CastExpr != 0) && |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 4463 | "ActOnCastExpr(): missing type or expr"); |
Steve Naroff | 16beff8 | 2007-07-16 23:25:18 +0000 | [diff] [blame] | 4464 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4465 | TypeSourceInfo *castTInfo = GetTypeForDeclaratorCast(D, CastExpr->getType()); |
Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 4466 | if (D.isInvalidType()) |
| 4467 | return ExprError(); |
| 4468 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4469 | if (getLangOpts().CPlusPlus) { |
Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 4470 | // Check that there are no default arguments (C++ only). |
| 4471 | CheckExtraCXXDefaultArguments(D); |
| 4472 | } |
| 4473 | |
John McCall | e82247a | 2011-10-01 05:17:03 +0000 | [diff] [blame] | 4474 | checkUnusedDeclAttributes(D); |
| 4475 | |
Argyrios Kyrtzidis | 0a85183 | 2011-07-01 22:22:59 +0000 | [diff] [blame] | 4476 | QualType castType = castTInfo->getType(); |
| 4477 | Ty = CreateParsedType(castType, castTInfo); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4478 | |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4479 | bool isVectorLiteral = false; |
| 4480 | |
| 4481 | // Check for an altivec or OpenCL literal, |
| 4482 | // i.e. all the elements are integer constants. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4483 | ParenExpr *PE = dyn_cast<ParenExpr>(CastExpr); |
| 4484 | ParenListExpr *PLE = dyn_cast<ParenListExpr>(CastExpr); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4485 | if ((getLangOpts().AltiVec || getLangOpts().OpenCL) |
Tobias Grosser | 37c31c2 | 2011-09-21 18:28:29 +0000 | [diff] [blame] | 4486 | && castType->isVectorType() && (PE || PLE)) { |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4487 | if (PLE && PLE->getNumExprs() == 0) { |
| 4488 | Diag(PLE->getExprLoc(), diag::err_altivec_empty_initializer); |
| 4489 | return ExprError(); |
| 4490 | } |
| 4491 | if (PE || PLE->getNumExprs() == 1) { |
| 4492 | Expr *E = (PE ? PE->getSubExpr() : PLE->getExpr(0)); |
| 4493 | if (!E->getType()->isVectorType()) |
| 4494 | isVectorLiteral = true; |
| 4495 | } |
| 4496 | else |
| 4497 | isVectorLiteral = true; |
| 4498 | } |
| 4499 | |
| 4500 | // If this is a vector initializer, '(' type ')' '(' init, ..., init ')' |
| 4501 | // then handle it as such. |
| 4502 | if (isVectorLiteral) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4503 | return BuildVectorLiteral(LParenLoc, RParenLoc, CastExpr, castTInfo); |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4504 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4505 | // If the Expr being casted is a ParenListExpr, handle it specially. |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4506 | // This is not an AltiVec-style cast, so turn the ParenListExpr into a |
| 4507 | // sequence of BinOp comma operators. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4508 | if (isa<ParenListExpr>(CastExpr)) { |
| 4509 | ExprResult Result = MaybeConvertParenListExprToParenExpr(S, CastExpr); |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4510 | if (Result.isInvalid()) return ExprError(); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4511 | CastExpr = Result.take(); |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4512 | } |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 4513 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4514 | return BuildCStyleCastExpr(LParenLoc, castTInfo, RParenLoc, CastExpr); |
John McCall | b042fdf | 2010-01-15 18:56:44 +0000 | [diff] [blame] | 4515 | } |
| 4516 | |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4517 | ExprResult Sema::BuildVectorLiteral(SourceLocation LParenLoc, |
| 4518 | SourceLocation RParenLoc, Expr *E, |
| 4519 | TypeSourceInfo *TInfo) { |
| 4520 | assert((isa<ParenListExpr>(E) || isa<ParenExpr>(E)) && |
| 4521 | "Expected paren or paren list expression"); |
| 4522 | |
| 4523 | Expr **exprs; |
| 4524 | unsigned numExprs; |
| 4525 | Expr *subExpr; |
| 4526 | if (ParenListExpr *PE = dyn_cast<ParenListExpr>(E)) { |
| 4527 | exprs = PE->getExprs(); |
| 4528 | numExprs = PE->getNumExprs(); |
| 4529 | } else { |
| 4530 | subExpr = cast<ParenExpr>(E)->getSubExpr(); |
| 4531 | exprs = &subExpr; |
| 4532 | numExprs = 1; |
| 4533 | } |
| 4534 | |
| 4535 | QualType Ty = TInfo->getType(); |
| 4536 | assert(Ty->isVectorType() && "Expected vector type"); |
| 4537 | |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 4538 | SmallVector<Expr *, 8> initExprs; |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4539 | const VectorType *VTy = Ty->getAs<VectorType>(); |
| 4540 | unsigned numElems = Ty->getAs<VectorType>()->getNumElements(); |
| 4541 | |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4542 | // '(...)' form of vector initialization in AltiVec: the number of |
| 4543 | // initializers must be one or must match the size of the vector. |
| 4544 | // If a single value is specified in the initializer then it will be |
| 4545 | // replicated to all the components of the vector |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4546 | if (VTy->getVectorKind() == VectorType::AltiVecVector) { |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4547 | // The number of initializers must be one or must match the size of the |
| 4548 | // vector. If a single value is specified in the initializer then it will |
| 4549 | // be replicated to all the components of the vector |
| 4550 | if (numExprs == 1) { |
| 4551 | QualType ElemTy = Ty->getAs<VectorType>()->getElementType(); |
Richard Smith | 61ffd09 | 2011-10-27 23:31:58 +0000 | [diff] [blame] | 4552 | ExprResult Literal = DefaultLvalueConversion(exprs[0]); |
| 4553 | if (Literal.isInvalid()) |
| 4554 | return ExprError(); |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4555 | Literal = ImpCastExprToType(Literal.take(), ElemTy, |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4556 | PrepareScalarCast(Literal, ElemTy)); |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4557 | return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Literal.take()); |
| 4558 | } |
| 4559 | else if (numExprs < numElems) { |
| 4560 | Diag(E->getExprLoc(), |
| 4561 | diag::err_incorrect_number_of_vector_initializers); |
| 4562 | return ExprError(); |
| 4563 | } |
| 4564 | else |
Benjamin Kramer | 14c5982 | 2012-02-14 12:06:21 +0000 | [diff] [blame] | 4565 | initExprs.append(exprs, exprs + numExprs); |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4566 | } |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4567 | else { |
| 4568 | // For OpenCL, when the number of initializers is a single value, |
| 4569 | // it will be replicated to all components of the vector. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4570 | if (getLangOpts().OpenCL && |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4571 | VTy->getVectorKind() == VectorType::GenericVector && |
| 4572 | numExprs == 1) { |
| 4573 | QualType ElemTy = Ty->getAs<VectorType>()->getElementType(); |
Richard Smith | 61ffd09 | 2011-10-27 23:31:58 +0000 | [diff] [blame] | 4574 | ExprResult Literal = DefaultLvalueConversion(exprs[0]); |
| 4575 | if (Literal.isInvalid()) |
| 4576 | return ExprError(); |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4577 | Literal = ImpCastExprToType(Literal.take(), ElemTy, |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 4578 | PrepareScalarCast(Literal, ElemTy)); |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4579 | return BuildCStyleCastExpr(LParenLoc, TInfo, RParenLoc, Literal.take()); |
| 4580 | } |
| 4581 | |
Benjamin Kramer | 14c5982 | 2012-02-14 12:06:21 +0000 | [diff] [blame] | 4582 | initExprs.append(exprs, exprs + numExprs); |
Tanya Lattner | 61b4bc8 | 2011-07-15 23:07:01 +0000 | [diff] [blame] | 4583 | } |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4584 | // FIXME: This means that pretty-printing the final AST will produce curly |
| 4585 | // braces instead of the original commas. |
| 4586 | InitListExpr *initE = new (Context) InitListExpr(Context, LParenLoc, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 4587 | initExprs, RParenLoc); |
Argyrios Kyrtzidis | 707f101 | 2011-07-01 22:22:54 +0000 | [diff] [blame] | 4588 | initE->setType(Ty); |
| 4589 | return BuildCompoundLiteralExpr(LParenLoc, TInfo, RParenLoc, initE); |
| 4590 | } |
| 4591 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 4592 | /// This is not an AltiVec-style cast or or C++ direct-initialization, so turn |
| 4593 | /// the ParenListExpr into a sequence of comma binary operators. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4594 | ExprResult |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4595 | Sema::MaybeConvertParenListExprToParenExpr(Scope *S, Expr *OrigExpr) { |
| 4596 | ParenListExpr *E = dyn_cast<ParenListExpr>(OrigExpr); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4597 | if (!E) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4598 | return Owned(OrigExpr); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4599 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 4600 | ExprResult Result(E->getExpr(0)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4601 | |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4602 | for (unsigned i = 1, e = E->getNumExprs(); i != e && !Result.isInvalid(); ++i) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4603 | Result = ActOnBinOp(S, E->getExprLoc(), tok::comma, Result.get(), |
| 4604 | E->getExpr(i)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4605 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 4606 | if (Result.isInvalid()) return ExprError(); |
| 4607 | |
| 4608 | return ActOnParenExpr(E->getLParenLoc(), E->getRParenLoc(), Result.get()); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4609 | } |
| 4610 | |
Sebastian Redl | 5b9cc5d | 2012-02-11 23:51:47 +0000 | [diff] [blame] | 4611 | ExprResult Sema::ActOnParenListExpr(SourceLocation L, |
| 4612 | SourceLocation R, |
| 4613 | MultiExprArg Val) { |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 4614 | assert(Val.data() != 0 && "ActOnParenOrParenListExpr() missing expr list"); |
| 4615 | Expr *expr = new (Context) ParenListExpr(Context, L, Val, R); |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4616 | return Owned(expr); |
| 4617 | } |
| 4618 | |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4619 | /// \brief Emit a specialized diagnostic when one expression is a null pointer |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4620 | /// constant and the other is not a pointer. Returns true if a diagnostic is |
| 4621 | /// emitted. |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4622 | bool Sema::DiagnoseConditionalForNull(Expr *LHSExpr, Expr *RHSExpr, |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4623 | SourceLocation QuestionLoc) { |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4624 | Expr *NullExpr = LHSExpr; |
| 4625 | Expr *NonPointerExpr = RHSExpr; |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4626 | Expr::NullPointerConstantKind NullKind = |
| 4627 | NullExpr->isNullPointerConstant(Context, |
| 4628 | Expr::NPC_ValueDependentIsNotNull); |
| 4629 | |
| 4630 | if (NullKind == Expr::NPCK_NotNull) { |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4631 | NullExpr = RHSExpr; |
| 4632 | NonPointerExpr = LHSExpr; |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4633 | NullKind = |
| 4634 | NullExpr->isNullPointerConstant(Context, |
| 4635 | Expr::NPC_ValueDependentIsNotNull); |
| 4636 | } |
| 4637 | |
| 4638 | if (NullKind == Expr::NPCK_NotNull) |
| 4639 | return false; |
| 4640 | |
David Blaikie | 50800fc | 2012-08-08 17:33:31 +0000 | [diff] [blame] | 4641 | if (NullKind == Expr::NPCK_ZeroExpression) |
| 4642 | return false; |
| 4643 | |
| 4644 | if (NullKind == Expr::NPCK_ZeroLiteral) { |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4645 | // In this case, check to make sure that we got here from a "NULL" |
| 4646 | // string in the source code. |
| 4647 | NullExpr = NullExpr->IgnoreParenImpCasts(); |
John McCall | 834e3f6 | 2011-03-08 07:59:04 +0000 | [diff] [blame] | 4648 | SourceLocation loc = NullExpr->getExprLoc(); |
| 4649 | if (!findMacroSpelling(loc, "NULL")) |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4650 | return false; |
| 4651 | } |
| 4652 | |
| 4653 | int DiagType = (NullKind == Expr::NPCK_CXX0X_nullptr); |
| 4654 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands_null) |
| 4655 | << NonPointerExpr->getType() << DiagType |
| 4656 | << NonPointerExpr->getSourceRange(); |
| 4657 | return true; |
| 4658 | } |
| 4659 | |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4660 | /// \brief Return false if the condition expression is valid, true otherwise. |
| 4661 | static bool checkCondition(Sema &S, Expr *Cond) { |
| 4662 | QualType CondTy = Cond->getType(); |
| 4663 | |
| 4664 | // C99 6.5.15p2 |
| 4665 | if (CondTy->isScalarType()) return false; |
| 4666 | |
| 4667 | // OpenCL: Sec 6.3.i says the condition is allowed to be a vector or scalar. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4668 | if (S.getLangOpts().OpenCL && CondTy->isVectorType()) |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4669 | return false; |
| 4670 | |
| 4671 | // Emit the proper error message. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4672 | S.Diag(Cond->getLocStart(), S.getLangOpts().OpenCL ? |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4673 | diag::err_typecheck_cond_expect_scalar : |
| 4674 | diag::err_typecheck_cond_expect_scalar_or_vector) |
| 4675 | << CondTy; |
| 4676 | return true; |
| 4677 | } |
| 4678 | |
| 4679 | /// \brief Return false if the two expressions can be converted to a vector, |
| 4680 | /// true otherwise |
| 4681 | static bool checkConditionalConvertScalarsToVectors(Sema &S, ExprResult &LHS, |
| 4682 | ExprResult &RHS, |
| 4683 | QualType CondTy) { |
| 4684 | // Both operands should be of scalar type. |
| 4685 | if (!LHS.get()->getType()->isScalarType()) { |
| 4686 | S.Diag(LHS.get()->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
| 4687 | << CondTy; |
| 4688 | return true; |
| 4689 | } |
| 4690 | if (!RHS.get()->getType()->isScalarType()) { |
| 4691 | S.Diag(RHS.get()->getLocStart(), diag::err_typecheck_cond_expect_scalar) |
| 4692 | << CondTy; |
| 4693 | return true; |
| 4694 | } |
| 4695 | |
| 4696 | // Implicity convert these scalars to the type of the condition. |
| 4697 | LHS = S.ImpCastExprToType(LHS.take(), CondTy, CK_IntegralCast); |
| 4698 | RHS = S.ImpCastExprToType(RHS.take(), CondTy, CK_IntegralCast); |
| 4699 | return false; |
| 4700 | } |
| 4701 | |
| 4702 | /// \brief Handle when one or both operands are void type. |
| 4703 | static QualType checkConditionalVoidType(Sema &S, ExprResult &LHS, |
| 4704 | ExprResult &RHS) { |
| 4705 | Expr *LHSExpr = LHS.get(); |
| 4706 | Expr *RHSExpr = RHS.get(); |
| 4707 | |
| 4708 | if (!LHSExpr->getType()->isVoidType()) |
| 4709 | S.Diag(RHSExpr->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 4710 | << RHSExpr->getSourceRange(); |
| 4711 | if (!RHSExpr->getType()->isVoidType()) |
| 4712 | S.Diag(LHSExpr->getLocStart(), diag::ext_typecheck_cond_one_void) |
| 4713 | << LHSExpr->getSourceRange(); |
| 4714 | LHS = S.ImpCastExprToType(LHS.take(), S.Context.VoidTy, CK_ToVoid); |
| 4715 | RHS = S.ImpCastExprToType(RHS.take(), S.Context.VoidTy, CK_ToVoid); |
| 4716 | return S.Context.VoidTy; |
| 4717 | } |
| 4718 | |
| 4719 | /// \brief Return false if the NullExpr can be promoted to PointerTy, |
| 4720 | /// true otherwise. |
| 4721 | static bool checkConditionalNullPointer(Sema &S, ExprResult &NullExpr, |
| 4722 | QualType PointerTy) { |
| 4723 | if ((!PointerTy->isAnyPointerType() && !PointerTy->isBlockPointerType()) || |
| 4724 | !NullExpr.get()->isNullPointerConstant(S.Context, |
| 4725 | Expr::NPC_ValueDependentIsNull)) |
| 4726 | return true; |
| 4727 | |
| 4728 | NullExpr = S.ImpCastExprToType(NullExpr.take(), PointerTy, CK_NullToPointer); |
| 4729 | return false; |
| 4730 | } |
| 4731 | |
| 4732 | /// \brief Checks compatibility between two pointers and return the resulting |
| 4733 | /// type. |
| 4734 | static QualType checkConditionalPointerCompatibility(Sema &S, ExprResult &LHS, |
| 4735 | ExprResult &RHS, |
| 4736 | SourceLocation Loc) { |
| 4737 | QualType LHSTy = LHS.get()->getType(); |
| 4738 | QualType RHSTy = RHS.get()->getType(); |
| 4739 | |
| 4740 | if (S.Context.hasSameType(LHSTy, RHSTy)) { |
| 4741 | // Two identical pointers types are always compatible. |
| 4742 | return LHSTy; |
| 4743 | } |
| 4744 | |
| 4745 | QualType lhptee, rhptee; |
| 4746 | |
| 4747 | // Get the pointee types. |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4748 | if (const BlockPointerType *LHSBTy = LHSTy->getAs<BlockPointerType>()) { |
| 4749 | lhptee = LHSBTy->getPointeeType(); |
| 4750 | rhptee = RHSTy->castAs<BlockPointerType>()->getPointeeType(); |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4751 | } else { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 4752 | lhptee = LHSTy->castAs<PointerType>()->getPointeeType(); |
| 4753 | rhptee = RHSTy->castAs<PointerType>()->getPointeeType(); |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4754 | } |
| 4755 | |
Eli Friedman | ae916a1 | 2012-04-05 22:30:04 +0000 | [diff] [blame] | 4756 | // C99 6.5.15p6: If both operands are pointers to compatible types or to |
| 4757 | // differently qualified versions of compatible types, the result type is |
| 4758 | // a pointer to an appropriately qualified version of the composite |
| 4759 | // type. |
| 4760 | |
| 4761 | // Only CVR-qualifiers exist in the standard, and the differently-qualified |
| 4762 | // clause doesn't make sense for our extensions. E.g. address space 2 should |
| 4763 | // be incompatible with address space 3: they may live on different devices or |
| 4764 | // anything. |
| 4765 | Qualifiers lhQual = lhptee.getQualifiers(); |
| 4766 | Qualifiers rhQual = rhptee.getQualifiers(); |
| 4767 | |
| 4768 | unsigned MergedCVRQual = lhQual.getCVRQualifiers() | rhQual.getCVRQualifiers(); |
| 4769 | lhQual.removeCVRQualifiers(); |
| 4770 | rhQual.removeCVRQualifiers(); |
| 4771 | |
| 4772 | lhptee = S.Context.getQualifiedType(lhptee.getUnqualifiedType(), lhQual); |
| 4773 | rhptee = S.Context.getQualifiedType(rhptee.getUnqualifiedType(), rhQual); |
| 4774 | |
| 4775 | QualType CompositeTy = S.Context.mergeTypes(lhptee, rhptee); |
| 4776 | |
| 4777 | if (CompositeTy.isNull()) { |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4778 | S.Diag(Loc, diag::warn_typecheck_cond_incompatible_pointers) |
| 4779 | << LHSTy << RHSTy << LHS.get()->getSourceRange() |
| 4780 | << RHS.get()->getSourceRange(); |
| 4781 | // In this situation, we assume void* type. No especially good |
| 4782 | // reason, but this is what gcc does, and we do have to pick |
| 4783 | // to get a consistent AST. |
| 4784 | QualType incompatTy = S.Context.getPointerType(S.Context.VoidTy); |
| 4785 | LHS = S.ImpCastExprToType(LHS.take(), incompatTy, CK_BitCast); |
| 4786 | RHS = S.ImpCastExprToType(RHS.take(), incompatTy, CK_BitCast); |
| 4787 | return incompatTy; |
| 4788 | } |
| 4789 | |
| 4790 | // The pointer types are compatible. |
Eli Friedman | ae916a1 | 2012-04-05 22:30:04 +0000 | [diff] [blame] | 4791 | QualType ResultTy = CompositeTy.withCVRQualifiers(MergedCVRQual); |
| 4792 | ResultTy = S.Context.getPointerType(ResultTy); |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4793 | |
Eli Friedman | ae916a1 | 2012-04-05 22:30:04 +0000 | [diff] [blame] | 4794 | LHS = S.ImpCastExprToType(LHS.take(), ResultTy, CK_BitCast); |
| 4795 | RHS = S.ImpCastExprToType(RHS.take(), ResultTy, CK_BitCast); |
| 4796 | return ResultTy; |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4797 | } |
| 4798 | |
| 4799 | /// \brief Return the resulting type when the operands are both block pointers. |
| 4800 | static QualType checkConditionalBlockPointerCompatibility(Sema &S, |
| 4801 | ExprResult &LHS, |
| 4802 | ExprResult &RHS, |
| 4803 | SourceLocation Loc) { |
| 4804 | QualType LHSTy = LHS.get()->getType(); |
| 4805 | QualType RHSTy = RHS.get()->getType(); |
| 4806 | |
| 4807 | if (!LHSTy->isBlockPointerType() || !RHSTy->isBlockPointerType()) { |
| 4808 | if (LHSTy->isVoidPointerType() || RHSTy->isVoidPointerType()) { |
| 4809 | QualType destType = S.Context.getPointerType(S.Context.VoidTy); |
| 4810 | LHS = S.ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
| 4811 | RHS = S.ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
| 4812 | return destType; |
| 4813 | } |
| 4814 | S.Diag(Loc, diag::err_typecheck_cond_incompatible_operands) |
| 4815 | << LHSTy << RHSTy << LHS.get()->getSourceRange() |
| 4816 | << RHS.get()->getSourceRange(); |
| 4817 | return QualType(); |
| 4818 | } |
| 4819 | |
| 4820 | // We have 2 block pointer types. |
| 4821 | return checkConditionalPointerCompatibility(S, LHS, RHS, Loc); |
| 4822 | } |
| 4823 | |
| 4824 | /// \brief Return the resulting type when the operands are both pointers. |
| 4825 | static QualType |
| 4826 | checkConditionalObjectPointersCompatibility(Sema &S, ExprResult &LHS, |
| 4827 | ExprResult &RHS, |
| 4828 | SourceLocation Loc) { |
| 4829 | // get the pointer types |
| 4830 | QualType LHSTy = LHS.get()->getType(); |
| 4831 | QualType RHSTy = RHS.get()->getType(); |
| 4832 | |
| 4833 | // get the "pointed to" types |
| 4834 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 4835 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
| 4836 | |
| 4837 | // ignore qualifiers on void (C99 6.5.15p3, clause 6) |
| 4838 | if (lhptee->isVoidType() && rhptee->isIncompleteOrObjectType()) { |
| 4839 | // Figure out necessary qualifiers (C99 6.5.15p6) |
| 4840 | QualType destPointee |
| 4841 | = S.Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
| 4842 | QualType destType = S.Context.getPointerType(destPointee); |
| 4843 | // Add qualifiers if necessary. |
| 4844 | LHS = S.ImpCastExprToType(LHS.take(), destType, CK_NoOp); |
| 4845 | // Promote to void*. |
| 4846 | RHS = S.ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
| 4847 | return destType; |
| 4848 | } |
| 4849 | if (rhptee->isVoidType() && lhptee->isIncompleteOrObjectType()) { |
| 4850 | QualType destPointee |
| 4851 | = S.Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
| 4852 | QualType destType = S.Context.getPointerType(destPointee); |
| 4853 | // Add qualifiers if necessary. |
| 4854 | RHS = S.ImpCastExprToType(RHS.take(), destType, CK_NoOp); |
| 4855 | // Promote to void*. |
| 4856 | LHS = S.ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
| 4857 | return destType; |
| 4858 | } |
| 4859 | |
| 4860 | return checkConditionalPointerCompatibility(S, LHS, RHS, Loc); |
| 4861 | } |
| 4862 | |
| 4863 | /// \brief Return false if the first expression is not an integer and the second |
| 4864 | /// expression is not a pointer, true otherwise. |
| 4865 | static bool checkPointerIntegerMismatch(Sema &S, ExprResult &Int, |
| 4866 | Expr* PointerExpr, SourceLocation Loc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4867 | bool IsIntFirstExpr) { |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4868 | if (!PointerExpr->getType()->isPointerType() || |
| 4869 | !Int.get()->getType()->isIntegerType()) |
| 4870 | return false; |
| 4871 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 4872 | Expr *Expr1 = IsIntFirstExpr ? Int.get() : PointerExpr; |
| 4873 | Expr *Expr2 = IsIntFirstExpr ? PointerExpr : Int.get(); |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4874 | |
| 4875 | S.Diag(Loc, diag::warn_typecheck_cond_pointer_integer_mismatch) |
| 4876 | << Expr1->getType() << Expr2->getType() |
| 4877 | << Expr1->getSourceRange() << Expr2->getSourceRange(); |
| 4878 | Int = S.ImpCastExprToType(Int.take(), PointerExpr->getType(), |
| 4879 | CK_IntegralToPointer); |
| 4880 | return true; |
| 4881 | } |
| 4882 | |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4883 | /// Note that LHS is not null here, even if this is the gnu "x ?: y" extension. |
| 4884 | /// In that case, LHS = cond. |
Chris Lattner | a119a3b | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 4885 | /// C99 6.5.15 |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 4886 | QualType Sema::CheckConditionalOperands(ExprResult &Cond, ExprResult &LHS, |
| 4887 | ExprResult &RHS, ExprValueKind &VK, |
| 4888 | ExprObjectKind &OK, |
Chris Lattner | a119a3b | 2009-02-18 04:38:20 +0000 | [diff] [blame] | 4889 | SourceLocation QuestionLoc) { |
Douglas Gregor | fadb53b | 2011-03-12 01:48:56 +0000 | [diff] [blame] | 4890 | |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4891 | ExprResult LHSResult = CheckPlaceholderExpr(LHS.get()); |
| 4892 | if (!LHSResult.isUsable()) return QualType(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4893 | LHS = LHSResult; |
Douglas Gregor | 7ad5d42 | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 4894 | |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 4895 | ExprResult RHSResult = CheckPlaceholderExpr(RHS.get()); |
| 4896 | if (!RHSResult.isUsable()) return QualType(); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 4897 | RHS = RHSResult; |
Douglas Gregor | 7ad5d42 | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 4898 | |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4899 | // C++ is sufficiently different to merit its own checker. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4900 | if (getLangOpts().CPlusPlus) |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 4901 | return CXXCheckConditionalOperands(Cond, LHS, RHS, VK, OK, QuestionLoc); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 4902 | |
| 4903 | VK = VK_RValue; |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 4904 | OK = OK_Ordinary; |
Sebastian Redl | 3201f6b | 2009-04-16 17:51:27 +0000 | [diff] [blame] | 4905 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4906 | Cond = UsualUnaryConversions(Cond.take()); |
| 4907 | if (Cond.isInvalid()) |
| 4908 | return QualType(); |
| 4909 | LHS = UsualUnaryConversions(LHS.take()); |
| 4910 | if (LHS.isInvalid()) |
| 4911 | return QualType(); |
| 4912 | RHS = UsualUnaryConversions(RHS.take()); |
| 4913 | if (RHS.isInvalid()) |
| 4914 | return QualType(); |
| 4915 | |
| 4916 | QualType CondTy = Cond.get()->getType(); |
| 4917 | QualType LHSTy = LHS.get()->getType(); |
| 4918 | QualType RHSTy = RHS.get()->getType(); |
Steve Naroff | c80b4ee | 2007-07-16 21:54:35 +0000 | [diff] [blame] | 4919 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4920 | // first, check the condition. |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4921 | if (checkCondition(*this, Cond.get())) |
| 4922 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4923 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4924 | // Now check the two expressions. |
Nate Begeman | 2ef13e5 | 2009-08-10 23:49:36 +0000 | [diff] [blame] | 4925 | if (LHSTy->isVectorType() || RHSTy->isVectorType()) |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 4926 | return CheckVectorOperands(LHS, RHS, QuestionLoc, /*isCompAssign*/false); |
Douglas Gregor | 898574e | 2008-12-05 23:32:09 +0000 | [diff] [blame] | 4927 | |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 4928 | // OpenCL: If the condition is a vector, and both operands are scalar, |
| 4929 | // attempt to implicity convert them to the vector type to act like the |
| 4930 | // built in select. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 4931 | if (getLangOpts().OpenCL && CondTy->isVectorType()) |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4932 | if (checkConditionalConvertScalarsToVectors(*this, LHS, RHS, CondTy)) |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 4933 | return QualType(); |
Nate Begeman | 6155d73 | 2010-09-20 22:41:17 +0000 | [diff] [blame] | 4934 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4935 | // If both operands have arithmetic type, do the usual arithmetic conversions |
| 4936 | // to find a common type: C99 6.5.15p3,5. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4937 | if (LHSTy->isArithmeticType() && RHSTy->isArithmeticType()) { |
| 4938 | UsualArithmeticConversions(LHS, RHS); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4939 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 4940 | return QualType(); |
| 4941 | return LHS.get()->getType(); |
Steve Naroff | a4332e2 | 2007-07-17 00:58:39 +0000 | [diff] [blame] | 4942 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4943 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4944 | // If both operands are the same structure or union type, the result is that |
| 4945 | // type. |
Ted Kremenek | 6217b80 | 2009-07-29 21:53:49 +0000 | [diff] [blame] | 4946 | if (const RecordType *LHSRT = LHSTy->getAs<RecordType>()) { // C99 6.5.15p3 |
| 4947 | if (const RecordType *RHSRT = RHSTy->getAs<RecordType>()) |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 4948 | if (LHSRT->getDecl() == RHSRT->getDecl()) |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4949 | // "If both the operands have structure or union type, the result has |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4950 | // that type." This implies that CV qualifiers are dropped. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4951 | return LHSTy.getUnqualifiedType(); |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 4952 | // FIXME: Type of conditional expression must be complete in C mode. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 4953 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 4954 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 4955 | // 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] | 4956 | // The following || allows only one side to be void (a GCC-ism). |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 4957 | if (LHSTy->isVoidType() || RHSTy->isVoidType()) { |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4958 | return checkConditionalVoidType(*this, LHS, RHS); |
Steve Naroff | e701c0a | 2008-05-12 21:44:38 +0000 | [diff] [blame] | 4959 | } |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4960 | |
Steve Naroff | b6d54e5 | 2008-01-08 01:11:38 +0000 | [diff] [blame] | 4961 | // C99 6.5.15p6 - "if one operand is a null pointer constant, the result has |
| 4962 | // the type of the other operand." |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4963 | if (!checkConditionalNullPointer(*this, RHS, LHSTy)) return LHSTy; |
| 4964 | if (!checkConditionalNullPointer(*this, LHS, RHSTy)) return RHSTy; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4965 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4966 | // All objective-c pointer type analysis is done here. |
| 4967 | QualType compositeType = FindCompositeObjCPointerType(LHS, RHS, |
| 4968 | QuestionLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4969 | if (LHS.isInvalid() || RHS.isInvalid()) |
| 4970 | return QualType(); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 4971 | if (!compositeType.isNull()) |
| 4972 | return compositeType; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4973 | |
| 4974 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4975 | // Handle block pointer types. |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4976 | if (LHSTy->isBlockPointerType() || RHSTy->isBlockPointerType()) |
| 4977 | return checkConditionalBlockPointerCompatibility(*this, LHS, RHS, |
| 4978 | QuestionLoc); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 4979 | |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4980 | // Check constraints for C object pointers types (C99 6.5.15p3,6). |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4981 | if (LHSTy->isPointerType() && RHSTy->isPointerType()) |
| 4982 | return checkConditionalObjectPointersCompatibility(*this, LHS, RHS, |
| 4983 | QuestionLoc); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 4984 | |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 4985 | // GCC compatibility: soften pointer/integer mismatch. Note that |
| 4986 | // null pointers have been filtered out by this point. |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4987 | if (checkPointerIntegerMismatch(*this, LHS, RHS.get(), QuestionLoc, |
| 4988 | /*isIntFirstExpr=*/true)) |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4989 | return RHSTy; |
Richard Trieu | 26f9607 | 2011-09-02 01:51:02 +0000 | [diff] [blame] | 4990 | if (checkPointerIntegerMismatch(*this, RHS, LHS.get(), QuestionLoc, |
| 4991 | /*isIntFirstExpr=*/false)) |
Steve Naroff | 7154a77 | 2009-07-01 14:36:47 +0000 | [diff] [blame] | 4992 | return LHSTy; |
Daniel Dunbar | 5e155f0 | 2008-09-11 23:12:46 +0000 | [diff] [blame] | 4993 | |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4994 | // Emit a better diagnostic if one of the expressions is a null pointer |
| 4995 | // constant and the other is not a pointer type. In this case, the user most |
| 4996 | // likely forgot to take the address of the other expression. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 4997 | if (DiagnoseConditionalForNull(LHS.get(), RHS.get(), QuestionLoc)) |
Chandler Carruth | 82214a8 | 2011-02-18 23:54:50 +0000 | [diff] [blame] | 4998 | return QualType(); |
| 4999 | |
Chris Lattner | 70d67a9 | 2008-01-06 22:42:25 +0000 | [diff] [blame] | 5000 | // Otherwise, the operands are not compatible. |
Chris Lattner | efdc39d | 2009-02-18 04:28:32 +0000 | [diff] [blame] | 5001 | Diag(QuestionLoc, diag::err_typecheck_cond_incompatible_operands) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5002 | << LHSTy << RHSTy << LHS.get()->getSourceRange() |
| 5003 | << RHS.get()->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5004 | return QualType(); |
| 5005 | } |
| 5006 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5007 | /// FindCompositeObjCPointerType - Helper method to find composite type of |
| 5008 | /// two objective-c pointer types of the two input expressions. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5009 | QualType Sema::FindCompositeObjCPointerType(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5010 | SourceLocation QuestionLoc) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5011 | QualType LHSTy = LHS.get()->getType(); |
| 5012 | QualType RHSTy = RHS.get()->getType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5013 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5014 | // Handle things like Class and struct objc_class*. Here we case the result |
| 5015 | // to the pseudo-builtin, because that will be implicitly cast back to the |
| 5016 | // redefinition type if an attempt is made to access its fields. |
| 5017 | if (LHSTy->isObjCClassType() && |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 5018 | (Context.hasSameType(RHSTy, Context.getObjCClassRedefinitionType()))) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5019 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_CPointerToObjCPointerCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5020 | return LHSTy; |
| 5021 | } |
| 5022 | if (RHSTy->isObjCClassType() && |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 5023 | (Context.hasSameType(LHSTy, Context.getObjCClassRedefinitionType()))) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5024 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_CPointerToObjCPointerCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5025 | return RHSTy; |
| 5026 | } |
| 5027 | // And the same for struct objc_object* / id |
| 5028 | if (LHSTy->isObjCIdType() && |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 5029 | (Context.hasSameType(RHSTy, Context.getObjCIdRedefinitionType()))) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5030 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_CPointerToObjCPointerCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5031 | return LHSTy; |
| 5032 | } |
| 5033 | if (RHSTy->isObjCIdType() && |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 5034 | (Context.hasSameType(LHSTy, Context.getObjCIdRedefinitionType()))) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5035 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_CPointerToObjCPointerCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5036 | return RHSTy; |
| 5037 | } |
| 5038 | // And the same for struct objc_selector* / SEL |
| 5039 | if (Context.isObjCSelType(LHSTy) && |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 5040 | (Context.hasSameType(RHSTy, Context.getObjCSelRedefinitionType()))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5041 | RHS = ImpCastExprToType(RHS.take(), LHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5042 | return LHSTy; |
| 5043 | } |
| 5044 | if (Context.isObjCSelType(RHSTy) && |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 5045 | (Context.hasSameType(LHSTy, Context.getObjCSelRedefinitionType()))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5046 | LHS = ImpCastExprToType(LHS.take(), RHSTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5047 | return RHSTy; |
| 5048 | } |
| 5049 | // Check constraints for Objective-C object pointers types. |
| 5050 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isObjCObjectPointerType()) { |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5051 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5052 | if (Context.getCanonicalType(LHSTy) == Context.getCanonicalType(RHSTy)) { |
| 5053 | // Two identical object pointer types are always compatible. |
| 5054 | return LHSTy; |
| 5055 | } |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5056 | const ObjCObjectPointerType *LHSOPT = LHSTy->castAs<ObjCObjectPointerType>(); |
| 5057 | const ObjCObjectPointerType *RHSOPT = RHSTy->castAs<ObjCObjectPointerType>(); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5058 | QualType compositeType = LHSTy; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5059 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5060 | // If both operands are interfaces and either operand can be |
| 5061 | // assigned to the other, use that type as the composite |
| 5062 | // type. This allows |
| 5063 | // xxx ? (A*) a : (B*) b |
| 5064 | // where B is a subclass of A. |
| 5065 | // |
| 5066 | // Additionally, as for assignment, if either type is 'id' |
| 5067 | // allow silent coercion. Finally, if the types are |
| 5068 | // incompatible then make sure to use 'id' as the composite |
| 5069 | // type so the result is acceptable for sending messages to. |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5070 | |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5071 | // FIXME: Consider unifying with 'areComparableObjCPointerTypes'. |
| 5072 | // It could return the composite type. |
| 5073 | if (Context.canAssignObjCInterfaces(LHSOPT, RHSOPT)) { |
| 5074 | compositeType = RHSOPT->isObjCBuiltinType() ? RHSTy : LHSTy; |
| 5075 | } else if (Context.canAssignObjCInterfaces(RHSOPT, LHSOPT)) { |
| 5076 | compositeType = LHSOPT->isObjCBuiltinType() ? LHSTy : RHSTy; |
| 5077 | } else if ((LHSTy->isObjCQualifiedIdType() || |
| 5078 | RHSTy->isObjCQualifiedIdType()) && |
| 5079 | Context.ObjCQualifiedIdTypesAreCompatible(LHSTy, RHSTy, true)) { |
| 5080 | // Need to handle "id<xx>" explicitly. |
| 5081 | // GCC allows qualified id and any Objective-C type to devolve to |
| 5082 | // id. Currently localizing to here until clear this should be |
| 5083 | // part of ObjCQualifiedIdTypesAreCompatible. |
| 5084 | compositeType = Context.getObjCIdType(); |
| 5085 | } else if (LHSTy->isObjCIdType() || RHSTy->isObjCIdType()) { |
| 5086 | compositeType = Context.getObjCIdType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5087 | } else if (!(compositeType = |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5088 | Context.areCommonBaseCompatible(LHSOPT, RHSOPT)).isNull()) |
| 5089 | ; |
| 5090 | else { |
| 5091 | Diag(QuestionLoc, diag::ext_typecheck_cond_incompatible_operands) |
| 5092 | << LHSTy << RHSTy |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5093 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5094 | QualType incompatTy = Context.getObjCIdType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5095 | LHS = ImpCastExprToType(LHS.take(), incompatTy, CK_BitCast); |
| 5096 | RHS = ImpCastExprToType(RHS.take(), incompatTy, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5097 | return incompatTy; |
| 5098 | } |
| 5099 | // The object pointer types are compatible. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5100 | LHS = ImpCastExprToType(LHS.take(), compositeType, CK_BitCast); |
| 5101 | RHS = ImpCastExprToType(RHS.take(), compositeType, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5102 | return compositeType; |
| 5103 | } |
| 5104 | // Check Objective-C object pointer types and 'void *' |
| 5105 | if (LHSTy->isVoidPointerType() && RHSTy->isObjCObjectPointerType()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5106 | if (getLangOpts().ObjCAutoRefCount) { |
Eli Friedman | a66eccb | 2012-02-25 00:23:44 +0000 | [diff] [blame] | 5107 | // ARC forbids the implicit conversion of object pointers to 'void *', |
| 5108 | // so these types are not compatible. |
| 5109 | Diag(QuestionLoc, diag::err_cond_voidptr_arc) << LHSTy << RHSTy |
| 5110 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
| 5111 | LHS = RHS = true; |
| 5112 | return QualType(); |
| 5113 | } |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5114 | QualType lhptee = LHSTy->getAs<PointerType>()->getPointeeType(); |
| 5115 | QualType rhptee = RHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 5116 | QualType destPointee |
| 5117 | = Context.getQualifiedType(lhptee, rhptee.getQualifiers()); |
| 5118 | QualType destType = Context.getPointerType(destPointee); |
| 5119 | // Add qualifiers if necessary. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5120 | LHS = ImpCastExprToType(LHS.take(), destType, CK_NoOp); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5121 | // Promote to void*. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5122 | RHS = ImpCastExprToType(RHS.take(), destType, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5123 | return destType; |
| 5124 | } |
| 5125 | if (LHSTy->isObjCObjectPointerType() && RHSTy->isVoidPointerType()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5126 | if (getLangOpts().ObjCAutoRefCount) { |
Eli Friedman | a66eccb | 2012-02-25 00:23:44 +0000 | [diff] [blame] | 5127 | // ARC forbids the implicit conversion of object pointers to 'void *', |
| 5128 | // so these types are not compatible. |
| 5129 | Diag(QuestionLoc, diag::err_cond_voidptr_arc) << LHSTy << RHSTy |
| 5130 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
| 5131 | LHS = RHS = true; |
| 5132 | return QualType(); |
| 5133 | } |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5134 | QualType lhptee = LHSTy->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 5135 | QualType rhptee = RHSTy->getAs<PointerType>()->getPointeeType(); |
| 5136 | QualType destPointee |
| 5137 | = Context.getQualifiedType(rhptee, lhptee.getQualifiers()); |
| 5138 | QualType destType = Context.getPointerType(destPointee); |
| 5139 | // Add qualifiers if necessary. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5140 | RHS = ImpCastExprToType(RHS.take(), destType, CK_NoOp); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5141 | // Promote to void*. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5142 | LHS = ImpCastExprToType(LHS.take(), destType, CK_BitCast); |
Fariborz Jahanian | eebc475 | 2009-12-10 19:47:41 +0000 | [diff] [blame] | 5143 | return destType; |
| 5144 | } |
| 5145 | return QualType(); |
| 5146 | } |
| 5147 | |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 5148 | /// SuggestParentheses - Emit a note with a fixit hint that wraps |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5149 | /// ParenRange in parentheses. |
| 5150 | static void SuggestParentheses(Sema &Self, SourceLocation Loc, |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 5151 | const PartialDiagnostic &Note, |
| 5152 | SourceRange ParenRange) { |
| 5153 | SourceLocation EndLoc = Self.PP.getLocForEndOfToken(ParenRange.getEnd()); |
| 5154 | if (ParenRange.getBegin().isFileID() && ParenRange.getEnd().isFileID() && |
| 5155 | EndLoc.isValid()) { |
| 5156 | Self.Diag(Loc, Note) |
| 5157 | << FixItHint::CreateInsertion(ParenRange.getBegin(), "(") |
| 5158 | << FixItHint::CreateInsertion(EndLoc, ")"); |
| 5159 | } else { |
| 5160 | // We can't display the parentheses, so just show the bare note. |
| 5161 | Self.Diag(Loc, Note) << ParenRange; |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5162 | } |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5163 | } |
| 5164 | |
| 5165 | static bool IsArithmeticOp(BinaryOperatorKind Opc) { |
| 5166 | return Opc >= BO_Mul && Opc <= BO_Shr; |
| 5167 | } |
| 5168 | |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 5169 | /// IsArithmeticBinaryExpr - Returns true if E is an arithmetic binary |
| 5170 | /// expression, either using a built-in or overloaded operator, |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 5171 | /// and sets *OpCode to the opcode and *RHSExprs to the right-hand side |
| 5172 | /// expression. |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 5173 | static bool IsArithmeticBinaryExpr(Expr *E, BinaryOperatorKind *Opcode, |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 5174 | Expr **RHSExprs) { |
Hans Wennborg | cb4d7c2 | 2011-09-12 12:07:30 +0000 | [diff] [blame] | 5175 | // Don't strip parenthesis: we should not warn if E is in parenthesis. |
| 5176 | E = E->IgnoreImpCasts(); |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 5177 | E = E->IgnoreConversionOperator(); |
Hans Wennborg | cb4d7c2 | 2011-09-12 12:07:30 +0000 | [diff] [blame] | 5178 | E = E->IgnoreImpCasts(); |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 5179 | |
| 5180 | // Built-in binary operator. |
| 5181 | if (BinaryOperator *OP = dyn_cast<BinaryOperator>(E)) { |
| 5182 | if (IsArithmeticOp(OP->getOpcode())) { |
| 5183 | *Opcode = OP->getOpcode(); |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 5184 | *RHSExprs = OP->getRHS(); |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 5185 | return true; |
| 5186 | } |
| 5187 | } |
| 5188 | |
| 5189 | // Overloaded operator. |
| 5190 | if (CXXOperatorCallExpr *Call = dyn_cast<CXXOperatorCallExpr>(E)) { |
| 5191 | if (Call->getNumArgs() != 2) |
| 5192 | return false; |
| 5193 | |
| 5194 | // Make sure this is really a binary operator that is safe to pass into |
| 5195 | // BinaryOperator::getOverloadedOpcode(), e.g. it's not a subscript op. |
| 5196 | OverloadedOperatorKind OO = Call->getOperator(); |
| 5197 | if (OO < OO_Plus || OO > OO_Arrow) |
| 5198 | return false; |
| 5199 | |
| 5200 | BinaryOperatorKind OpKind = BinaryOperator::getOverloadedOpcode(OO); |
| 5201 | if (IsArithmeticOp(OpKind)) { |
| 5202 | *Opcode = OpKind; |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 5203 | *RHSExprs = Call->getArg(1); |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 5204 | return true; |
| 5205 | } |
| 5206 | } |
| 5207 | |
| 5208 | return false; |
| 5209 | } |
| 5210 | |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5211 | static bool IsLogicOp(BinaryOperatorKind Opc) { |
| 5212 | return (Opc >= BO_LT && Opc <= BO_NE) || (Opc >= BO_LAnd && Opc <= BO_LOr); |
| 5213 | } |
| 5214 | |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 5215 | /// ExprLooksBoolean - Returns true if E looks boolean, i.e. it has boolean type |
| 5216 | /// or is a logical expression such as (x==y) which has int type, but is |
| 5217 | /// commonly interpreted as boolean. |
| 5218 | static bool ExprLooksBoolean(Expr *E) { |
| 5219 | E = E->IgnoreParenImpCasts(); |
| 5220 | |
| 5221 | if (E->getType()->isBooleanType()) |
| 5222 | return true; |
| 5223 | if (BinaryOperator *OP = dyn_cast<BinaryOperator>(E)) |
| 5224 | return IsLogicOp(OP->getOpcode()); |
| 5225 | if (UnaryOperator *OP = dyn_cast<UnaryOperator>(E)) |
| 5226 | return OP->getOpcode() == UO_LNot; |
| 5227 | |
| 5228 | return false; |
| 5229 | } |
| 5230 | |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5231 | /// DiagnoseConditionalPrecedence - Emit a warning when a conditional operator |
| 5232 | /// and binary operator are mixed in a way that suggests the programmer assumed |
| 5233 | /// the conditional operator has higher precedence, for example: |
| 5234 | /// "int x = a + someBinaryCondition ? 1 : 2". |
| 5235 | static void DiagnoseConditionalPrecedence(Sema &Self, |
| 5236 | SourceLocation OpLoc, |
Chandler Carruth | 43bc78d | 2011-06-16 01:05:08 +0000 | [diff] [blame] | 5237 | Expr *Condition, |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 5238 | Expr *LHSExpr, |
| 5239 | Expr *RHSExpr) { |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 5240 | BinaryOperatorKind CondOpcode; |
| 5241 | Expr *CondRHS; |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5242 | |
Chandler Carruth | 43bc78d | 2011-06-16 01:05:08 +0000 | [diff] [blame] | 5243 | if (!IsArithmeticBinaryExpr(Condition, &CondOpcode, &CondRHS)) |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 5244 | return; |
| 5245 | if (!ExprLooksBoolean(CondRHS)) |
| 5246 | return; |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5247 | |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 5248 | // The condition is an arithmetic binary expression, with a right- |
| 5249 | // hand side that looks boolean, so warn. |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5250 | |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 5251 | Self.Diag(OpLoc, diag::warn_precedence_conditional) |
Chandler Carruth | 43bc78d | 2011-06-16 01:05:08 +0000 | [diff] [blame] | 5252 | << Condition->getSourceRange() |
Hans Wennborg | 2f072b4 | 2011-06-09 17:06:51 +0000 | [diff] [blame] | 5253 | << BinaryOperator::getOpcodeStr(CondOpcode); |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5254 | |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 5255 | SuggestParentheses(Self, OpLoc, |
| 5256 | Self.PDiag(diag::note_precedence_conditional_silence) |
| 5257 | << BinaryOperator::getOpcodeStr(CondOpcode), |
| 5258 | SourceRange(Condition->getLocStart(), Condition->getLocEnd())); |
Chandler Carruth | 9d5353c | 2011-06-21 23:04:18 +0000 | [diff] [blame] | 5259 | |
| 5260 | SuggestParentheses(Self, OpLoc, |
| 5261 | Self.PDiag(diag::note_precedence_conditional_first), |
Richard Trieu | 33fc757 | 2011-09-06 20:06:39 +0000 | [diff] [blame] | 5262 | SourceRange(CondRHS->getLocStart(), RHSExpr->getLocEnd())); |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5263 | } |
| 5264 | |
Steve Naroff | f69936d | 2007-09-16 03:34:24 +0000 | [diff] [blame] | 5265 | /// ActOnConditionalOp - Parse a ?: operation. Note that 'LHS' may be null |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5266 | /// in the case of a the GNU conditional expr extension. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 5267 | ExprResult Sema::ActOnConditionalOp(SourceLocation QuestionLoc, |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5268 | SourceLocation ColonLoc, |
| 5269 | Expr *CondExpr, Expr *LHSExpr, |
| 5270 | Expr *RHSExpr) { |
Chris Lattner | a21ddb3 | 2007-11-26 01:40:58 +0000 | [diff] [blame] | 5271 | // If this is the gnu "x ?: y" extension, analyze the types as though the LHS |
| 5272 | // was the condition. |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5273 | OpaqueValueExpr *opaqueValue = 0; |
| 5274 | Expr *commonExpr = 0; |
| 5275 | if (LHSExpr == 0) { |
| 5276 | commonExpr = CondExpr; |
| 5277 | |
| 5278 | // We usually want to apply unary conversions *before* saving, except |
| 5279 | // in the special case of a C++ l-value conditional. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5280 | if (!(getLangOpts().CPlusPlus |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5281 | && !commonExpr->isTypeDependent() |
| 5282 | && commonExpr->getValueKind() == RHSExpr->getValueKind() |
| 5283 | && commonExpr->isGLValue() |
| 5284 | && commonExpr->isOrdinaryOrBitFieldObject() |
| 5285 | && RHSExpr->isOrdinaryOrBitFieldObject() |
| 5286 | && Context.hasSameType(commonExpr->getType(), RHSExpr->getType()))) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5287 | ExprResult commonRes = UsualUnaryConversions(commonExpr); |
| 5288 | if (commonRes.isInvalid()) |
| 5289 | return ExprError(); |
| 5290 | commonExpr = commonRes.take(); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5291 | } |
| 5292 | |
| 5293 | opaqueValue = new (Context) OpaqueValueExpr(commonExpr->getExprLoc(), |
| 5294 | commonExpr->getType(), |
| 5295 | commonExpr->getValueKind(), |
Douglas Gregor | 97df54e | 2012-02-23 22:17:26 +0000 | [diff] [blame] | 5296 | commonExpr->getObjectKind(), |
| 5297 | commonExpr); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5298 | LHSExpr = CondExpr = opaqueValue; |
Fariborz Jahanian | f9b949f | 2010-08-31 18:02:20 +0000 | [diff] [blame] | 5299 | } |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5300 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 5301 | ExprValueKind VK = VK_RValue; |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 5302 | ExprObjectKind OK = OK_Ordinary; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5303 | ExprResult Cond = Owned(CondExpr), LHS = Owned(LHSExpr), RHS = Owned(RHSExpr); |
| 5304 | QualType result = CheckConditionalOperands(Cond, LHS, RHS, |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5305 | VK, OK, QuestionLoc); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5306 | if (result.isNull() || Cond.isInvalid() || LHS.isInvalid() || |
| 5307 | RHS.isInvalid()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 5308 | return ExprError(); |
| 5309 | |
Hans Wennborg | 9cfdae3 | 2011-06-03 18:00:36 +0000 | [diff] [blame] | 5310 | DiagnoseConditionalPrecedence(*this, QuestionLoc, Cond.get(), LHS.get(), |
| 5311 | RHS.get()); |
| 5312 | |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5313 | if (!commonExpr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5314 | return Owned(new (Context) ConditionalOperator(Cond.take(), QuestionLoc, |
| 5315 | LHS.take(), ColonLoc, |
| 5316 | RHS.take(), result, VK, OK)); |
John McCall | 56ca35d | 2011-02-17 10:25:35 +0000 | [diff] [blame] | 5317 | |
| 5318 | return Owned(new (Context) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5319 | BinaryConditionalOperator(commonExpr, opaqueValue, Cond.take(), LHS.take(), |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5320 | RHS.take(), QuestionLoc, ColonLoc, result, VK, |
| 5321 | OK)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5322 | } |
| 5323 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5324 | // checkPointerTypesForAssignment - This is a very tricky routine (despite |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5325 | // being closely modeled after the C99 spec:-). The odd characteristic of this |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5326 | // routine is it effectively iqnores the qualifiers on the top level pointee. |
| 5327 | // This circumvents the usual type rules specified in 6.2.7p1 & 6.7.5.[1-3]. |
| 5328 | // FIXME: add a couple examples in this comment. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5329 | static Sema::AssignConvertType |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5330 | checkPointerTypesForAssignment(Sema &S, QualType LHSType, QualType RHSType) { |
| 5331 | assert(LHSType.isCanonical() && "LHS not canonicalized!"); |
| 5332 | assert(RHSType.isCanonical() && "RHS not canonicalized!"); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5333 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5334 | // get the "pointed to" type (ignoring qualifiers at the top level) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5335 | const Type *lhptee, *rhptee; |
| 5336 | Qualifiers lhq, rhq; |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5337 | llvm::tie(lhptee, lhq) = cast<PointerType>(LHSType)->getPointeeType().split(); |
| 5338 | llvm::tie(rhptee, rhq) = cast<PointerType>(RHSType)->getPointeeType().split(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5339 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5340 | Sema::AssignConvertType ConvTy = Sema::Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5341 | |
| 5342 | // C99 6.5.16.1p1: This following citation is common to constraints |
| 5343 | // 3 & 4 (below). ...and the type *pointed to* by the left has all the |
| 5344 | // qualifiers of the type *pointed to* by the right; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5345 | Qualifiers lq; |
| 5346 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5347 | // As a special case, 'non-__weak A *' -> 'non-__weak const *' is okay. |
| 5348 | if (lhq.getObjCLifetime() != rhq.getObjCLifetime() && |
| 5349 | lhq.compatiblyIncludesObjCLifetime(rhq)) { |
| 5350 | // Ignore lifetime for further calculation. |
| 5351 | lhq.removeObjCLifetime(); |
| 5352 | rhq.removeObjCLifetime(); |
| 5353 | } |
| 5354 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5355 | if (!lhq.compatiblyIncludes(rhq)) { |
| 5356 | // Treat address-space mismatches as fatal. TODO: address subspaces |
| 5357 | if (lhq.getAddressSpace() != rhq.getAddressSpace()) |
| 5358 | ConvTy = Sema::IncompatiblePointerDiscardsQualifiers; |
| 5359 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5360 | // 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] | 5361 | // and from void*. |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 5362 | else if (lhq.withoutObjCGCAttr().withoutObjCLifetime() |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5363 | .compatiblyIncludes( |
John McCall | 200fa53 | 2012-02-08 00:46:36 +0000 | [diff] [blame] | 5364 | rhq.withoutObjCGCAttr().withoutObjCLifetime()) |
John McCall | 2234873 | 2011-03-26 02:56:45 +0000 | [diff] [blame] | 5365 | && (lhptee->isVoidType() || rhptee->isVoidType())) |
| 5366 | ; // keep old |
| 5367 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 5368 | // Treat lifetime mismatches as fatal. |
| 5369 | else if (lhq.getObjCLifetime() != rhq.getObjCLifetime()) |
| 5370 | ConvTy = Sema::IncompatiblePointerDiscardsQualifiers; |
| 5371 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5372 | // For GCC compatibility, other qualifier mismatches are treated |
| 5373 | // as still compatible in C. |
| 5374 | else ConvTy = Sema::CompatiblePointerDiscardsQualifiers; |
| 5375 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5376 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5377 | // C99 6.5.16.1p1 (constraint 4): If one operand is a pointer to an object or |
| 5378 | // 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] | 5379 | // version of void... |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5380 | if (lhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5381 | if (rhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5382 | return ConvTy; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5383 | |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5384 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5385 | assert(rhptee->isFunctionType()); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5386 | return Sema::FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5387 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5388 | |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5389 | if (rhptee->isVoidType()) { |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5390 | if (lhptee->isIncompleteOrObjectType()) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5391 | return ConvTy; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5392 | |
| 5393 | // As an extension, we allow cast to/from void* to function pointer. |
Chris Lattner | d805bec | 2008-04-02 06:59:01 +0000 | [diff] [blame] | 5394 | assert(lhptee->isFunctionType()); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5395 | return Sema::FunctionVoidPointer; |
Chris Lattner | bfe639e | 2008-01-03 22:56:36 +0000 | [diff] [blame] | 5396 | } |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5397 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5398 | // 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] | 5399 | // unqualified versions of compatible types, ... |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5400 | QualType ltrans = QualType(lhptee, 0), rtrans = QualType(rhptee, 0); |
| 5401 | if (!S.Context.typesAreCompatible(ltrans, rtrans)) { |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5402 | // Check if the pointee types are compatible ignoring the sign. |
| 5403 | // We explicitly check for char so that we catch "char" vs |
| 5404 | // "unsigned char" on systems where "char" is unsigned. |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5405 | if (lhptee->isCharType()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5406 | ltrans = S.Context.UnsignedCharTy; |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 5407 | else if (lhptee->hasSignedIntegerRepresentation()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5408 | ltrans = S.Context.getCorrespondingUnsignedType(ltrans); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5409 | |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5410 | if (rhptee->isCharType()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5411 | rtrans = S.Context.UnsignedCharTy; |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 5412 | else if (rhptee->hasSignedIntegerRepresentation()) |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5413 | rtrans = S.Context.getCorrespondingUnsignedType(rtrans); |
Chris Lattner | 6a2b926 | 2009-10-17 20:33:28 +0000 | [diff] [blame] | 5414 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5415 | if (ltrans == rtrans) { |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5416 | // Types are compatible ignoring the sign. Qualifier incompatibility |
| 5417 | // takes priority over sign incompatibility because the sign |
| 5418 | // warning can be disabled. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5419 | if (ConvTy != Sema::Compatible) |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5420 | return ConvTy; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5421 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5422 | return Sema::IncompatiblePointerSign; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5423 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5424 | |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 5425 | // If we are a multi-level pointer, it's possible that our issue is simply |
| 5426 | // one of qualification - e.g. char ** -> const char ** is not allowed. If |
| 5427 | // the eventual target type is the same and the pointers have the same |
| 5428 | // level of indirection, this must be the issue. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5429 | if (isa<PointerType>(lhptee) && isa<PointerType>(rhptee)) { |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 5430 | do { |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5431 | lhptee = cast<PointerType>(lhptee)->getPointeeType().getTypePtr(); |
| 5432 | rhptee = cast<PointerType>(rhptee)->getPointeeType().getTypePtr(); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5433 | } while (isa<PointerType>(lhptee) && isa<PointerType>(rhptee)); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5434 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 5435 | if (lhptee == rhptee) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5436 | return Sema::IncompatibleNestedPointerQualifiers; |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 5437 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5438 | |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5439 | // General pointer incompatibility takes priority over qualifiers. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5440 | return Sema::IncompatiblePointer; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 5441 | } |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5442 | if (!S.getLangOpts().CPlusPlus && |
Fariborz Jahanian | 53c8167 | 2011-10-05 00:05:34 +0000 | [diff] [blame] | 5443 | S.IsNoReturnConversion(ltrans, rtrans, ltrans)) |
| 5444 | return Sema::IncompatiblePointer; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5445 | return ConvTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5446 | } |
| 5447 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5448 | /// checkBlockPointerTypesForAssignment - This routine determines whether two |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5449 | /// block pointer types are compatible or whether a block and normal pointer |
| 5450 | /// are compatible. It is more restrict than comparing two function pointer |
| 5451 | // types. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5452 | static Sema::AssignConvertType |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5453 | checkBlockPointerTypesForAssignment(Sema &S, QualType LHSType, |
| 5454 | QualType RHSType) { |
| 5455 | assert(LHSType.isCanonical() && "LHS not canonicalized!"); |
| 5456 | assert(RHSType.isCanonical() && "RHS not canonicalized!"); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5457 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5458 | QualType lhptee, rhptee; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5459 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5460 | // get the "pointed to" type (ignoring qualifiers at the top level) |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5461 | lhptee = cast<BlockPointerType>(LHSType)->getPointeeType(); |
| 5462 | rhptee = cast<BlockPointerType>(RHSType)->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5463 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5464 | // In C++, the types have to match exactly. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5465 | if (S.getLangOpts().CPlusPlus) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5466 | return Sema::IncompatibleBlockPointer; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5467 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5468 | Sema::AssignConvertType ConvTy = Sema::Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5469 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5470 | // For blocks we enforce that qualifiers are identical. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5471 | if (lhptee.getLocalQualifiers() != rhptee.getLocalQualifiers()) |
| 5472 | ConvTy = Sema::CompatiblePointerDiscardsQualifiers; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5473 | |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5474 | if (!S.Context.typesAreBlockPointerCompatible(LHSType, RHSType)) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5475 | return Sema::IncompatibleBlockPointer; |
| 5476 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5477 | return ConvTy; |
| 5478 | } |
| 5479 | |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5480 | /// checkObjCPointerTypesForAssignment - Compares two objective-c pointer types |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5481 | /// for assignment compatibility. |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5482 | static Sema::AssignConvertType |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5483 | checkObjCPointerTypesForAssignment(Sema &S, QualType LHSType, |
| 5484 | QualType RHSType) { |
| 5485 | assert(LHSType.isCanonical() && "LHS was not canonicalized!"); |
| 5486 | assert(RHSType.isCanonical() && "RHS was not canonicalized!"); |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5487 | |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5488 | if (LHSType->isObjCBuiltinType()) { |
Fariborz Jahanian | d4c6090 | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 5489 | // Class is not compatible with ObjC object pointers. |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5490 | if (LHSType->isObjCClassType() && !RHSType->isObjCBuiltinType() && |
| 5491 | !RHSType->isObjCQualifiedClassType()) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5492 | return Sema::IncompatiblePointer; |
| 5493 | return Sema::Compatible; |
Fariborz Jahanian | d4c6090 | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 5494 | } |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5495 | if (RHSType->isObjCBuiltinType()) { |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5496 | if (RHSType->isObjCClassType() && !LHSType->isObjCBuiltinType() && |
| 5497 | !LHSType->isObjCQualifiedClassType()) |
Fariborz Jahanian | 412a496 | 2011-09-15 20:40:18 +0000 | [diff] [blame] | 5498 | return Sema::IncompatiblePointer; |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5499 | return Sema::Compatible; |
Fariborz Jahanian | d4c6090 | 2010-03-19 18:06:10 +0000 | [diff] [blame] | 5500 | } |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5501 | QualType lhptee = LHSType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
| 5502 | QualType rhptee = RHSType->getAs<ObjCObjectPointerType>()->getPointeeType(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 5503 | |
Fariborz Jahanian | f2b4f7b | 2012-01-12 22:12:08 +0000 | [diff] [blame] | 5504 | if (!lhptee.isAtLeastAsQualifiedAs(rhptee) && |
| 5505 | // make an exception for id<P> |
| 5506 | !LHSType->isObjCQualifiedIdType()) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5507 | return Sema::CompatiblePointerDiscardsQualifiers; |
| 5508 | |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5509 | if (S.Context.typesAreCompatible(LHSType, RHSType)) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5510 | return Sema::Compatible; |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5511 | if (LHSType->isObjCQualifiedIdType() || RHSType->isObjCQualifiedIdType()) |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 5512 | return Sema::IncompatibleObjCQualifiedId; |
| 5513 | return Sema::IncompatiblePointer; |
Fariborz Jahanian | 52efc3f | 2009-12-08 18:24:49 +0000 | [diff] [blame] | 5514 | } |
| 5515 | |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5516 | Sema::AssignConvertType |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 5517 | Sema::CheckAssignmentConstraints(SourceLocation Loc, |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5518 | QualType LHSType, QualType RHSType) { |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5519 | // Fake up an opaque expression. We don't actually care about what |
| 5520 | // cast operations are required, so if CheckAssignmentConstraints |
| 5521 | // adds casts to this they'll be wasted, but fortunately that doesn't |
| 5522 | // usually happen on valid code. |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5523 | OpaqueValueExpr RHSExpr(Loc, RHSType, VK_RValue); |
| 5524 | ExprResult RHSPtr = &RHSExpr; |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5525 | CastKind K = CK_Invalid; |
| 5526 | |
Richard Trieu | 1da27a1 | 2011-09-06 20:21:22 +0000 | [diff] [blame] | 5527 | return CheckAssignmentConstraints(LHSType, RHSPtr, K); |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5528 | } |
| 5529 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5530 | /// CheckAssignmentConstraints (C99 6.5.16) - This routine currently |
| 5531 | /// has code to accommodate several GCC extensions when type checking |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5532 | /// pointers. Here are some objectionable examples that GCC considers warnings: |
| 5533 | /// |
| 5534 | /// int a, *pint; |
| 5535 | /// short *pshort; |
| 5536 | /// struct foo *pfoo; |
| 5537 | /// |
| 5538 | /// pint = pshort; // warning: assignment from incompatible pointer type |
| 5539 | /// a = pint; // warning: assignment makes integer from pointer without a cast |
| 5540 | /// pint = a; // warning: assignment makes pointer from integer without a cast |
| 5541 | /// pint = pfoo; // warning: assignment from incompatible pointer type |
| 5542 | /// |
| 5543 | /// 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] | 5544 | /// C99 spec dictates. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5545 | /// |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5546 | /// Sets 'Kind' for any result kind except Incompatible. |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5547 | Sema::AssignConvertType |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5548 | Sema::CheckAssignmentConstraints(QualType LHSType, ExprResult &RHS, |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5549 | CastKind &Kind) { |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5550 | QualType RHSType = RHS.get()->getType(); |
| 5551 | QualType OrigLHSType = LHSType; |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5552 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5553 | // Get canonical types. We're not formatting these types, just comparing |
| 5554 | // them. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5555 | LHSType = Context.getCanonicalType(LHSType).getUnqualifiedType(); |
| 5556 | RHSType = Context.getCanonicalType(RHSType).getUnqualifiedType(); |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5557 | |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5558 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5559 | // Common case: no conversion required. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5560 | if (LHSType == RHSType) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5561 | Kind = CK_NoOp; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5562 | return Compatible; |
David Chisnall | 0f43656 | 2009-08-17 16:35:33 +0000 | [diff] [blame] | 5563 | } |
| 5564 | |
Eli Friedman | 860a319 | 2012-06-16 02:19:17 +0000 | [diff] [blame] | 5565 | // If we have an atomic type, try a non-atomic assignment, then just add an |
| 5566 | // atomic qualification step. |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 5567 | if (const AtomicType *AtomicTy = dyn_cast<AtomicType>(LHSType)) { |
Eli Friedman | 860a319 | 2012-06-16 02:19:17 +0000 | [diff] [blame] | 5568 | Sema::AssignConvertType result = |
| 5569 | CheckAssignmentConstraints(AtomicTy->getValueType(), RHS, Kind); |
| 5570 | if (result != Compatible) |
| 5571 | return result; |
| 5572 | if (Kind != CK_NoOp) |
| 5573 | RHS = ImpCastExprToType(RHS.take(), AtomicTy->getValueType(), Kind); |
| 5574 | Kind = CK_NonAtomicToAtomic; |
| 5575 | return Compatible; |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 5576 | } |
| 5577 | |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 5578 | // If the left-hand side is a reference type, then we are in a |
| 5579 | // (rare!) case where we've allowed the use of references in C, |
| 5580 | // e.g., as a parameter type in a built-in function. In this case, |
| 5581 | // just make sure that the type referenced is compatible with the |
| 5582 | // right-hand side type. The caller is responsible for adjusting |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5583 | // LHSType so that the resulting expression does not have reference |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 5584 | // type. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5585 | if (const ReferenceType *LHSTypeRef = LHSType->getAs<ReferenceType>()) { |
| 5586 | if (Context.typesAreCompatible(LHSTypeRef->getPointeeType(), RHSType)) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5587 | Kind = CK_LValueBitCast; |
Anders Carlsson | 793680e | 2007-10-12 23:56:29 +0000 | [diff] [blame] | 5588 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5589 | } |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5590 | return Incompatible; |
Fariborz Jahanian | 411f373 | 2007-12-19 17:45:58 +0000 | [diff] [blame] | 5591 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5592 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5593 | // Allow scalar to ExtVector assignments, and assignments of an ExtVector type |
| 5594 | // to the same ExtVector type. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5595 | if (LHSType->isExtVectorType()) { |
| 5596 | if (RHSType->isExtVectorType()) |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5597 | return Incompatible; |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5598 | if (RHSType->isArithmeticType()) { |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5599 | // CK_VectorSplat does T -> vector T, so first cast to the |
| 5600 | // element type. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5601 | QualType elType = cast<ExtVectorType>(LHSType)->getElementType(); |
| 5602 | if (elType != RHSType) { |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 5603 | Kind = PrepareScalarCast(RHS, elType); |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5604 | RHS = ImpCastExprToType(RHS.take(), elType, Kind); |
John McCall | 1c23e91 | 2010-11-16 02:32:08 +0000 | [diff] [blame] | 5605 | } |
| 5606 | Kind = CK_VectorSplat; |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5607 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5608 | } |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 5609 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5610 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5611 | // Conversions to or from vector type. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5612 | if (LHSType->isVectorType() || RHSType->isVectorType()) { |
| 5613 | if (LHSType->isVectorType() && RHSType->isVectorType()) { |
Bob Wilson | de3deea | 2010-12-02 00:25:15 +0000 | [diff] [blame] | 5614 | // Allow assignments of an AltiVec vector type to an equivalent GCC |
| 5615 | // vector type and vice versa |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5616 | if (Context.areCompatibleVectorTypes(LHSType, RHSType)) { |
Bob Wilson | de3deea | 2010-12-02 00:25:15 +0000 | [diff] [blame] | 5617 | Kind = CK_BitCast; |
| 5618 | return Compatible; |
| 5619 | } |
| 5620 | |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5621 | // If we are allowing lax vector conversions, and LHS and RHS are both |
| 5622 | // vectors, the total size only needs to be the same. This is a bitcast; |
| 5623 | // no bits are changed but the result type is different. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5624 | if (getLangOpts().LaxVectorConversions && |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5625 | (Context.getTypeSize(LHSType) == Context.getTypeSize(RHSType))) { |
John McCall | 0c6d28d | 2010-11-15 10:08:00 +0000 | [diff] [blame] | 5626 | Kind = CK_BitCast; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 5627 | return IncompatibleVectors; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5628 | } |
Chris Lattner | e8b3e96 | 2008-01-04 23:32:24 +0000 | [diff] [blame] | 5629 | } |
| 5630 | return Incompatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5631 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5632 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5633 | // Arithmetic conversions. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5634 | if (LHSType->isArithmeticType() && RHSType->isArithmeticType() && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5635 | !(getLangOpts().CPlusPlus && LHSType->isEnumeralType())) { |
John McCall | a180f04 | 2011-10-06 23:25:11 +0000 | [diff] [blame] | 5636 | Kind = PrepareScalarCast(RHS, LHSType); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5637 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5638 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5639 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5640 | // Conversions to normal pointers. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5641 | if (const PointerType *LHSPointer = dyn_cast<PointerType>(LHSType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5642 | // U* -> T* |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5643 | if (isa<PointerType>(RHSType)) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5644 | Kind = CK_BitCast; |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5645 | return checkPointerTypesForAssignment(*this, LHSType, RHSType); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5646 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5647 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5648 | // int -> T* |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5649 | if (RHSType->isIntegerType()) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5650 | Kind = CK_IntegralToPointer; // FIXME: null? |
| 5651 | return IntToPointer; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5652 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5653 | |
| 5654 | // C pointers are not compatible with ObjC object pointers, |
| 5655 | // with two exceptions: |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5656 | if (isa<ObjCObjectPointerType>(RHSType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5657 | // - conversions to void* |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5658 | if (LHSPointer->getPointeeType()->isVoidType()) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5659 | Kind = CK_BitCast; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5660 | return Compatible; |
| 5661 | } |
| 5662 | |
| 5663 | // - conversions from 'Class' to the redefinition type |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5664 | if (RHSType->isObjCClassType() && |
| 5665 | Context.hasSameType(LHSType, |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 5666 | Context.getObjCClassRedefinitionType())) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5667 | Kind = CK_BitCast; |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 5668 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5669 | } |
Douglas Gregor | c737acb | 2011-09-27 16:10:05 +0000 | [diff] [blame] | 5670 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5671 | Kind = CK_BitCast; |
| 5672 | return IncompatiblePointer; |
| 5673 | } |
| 5674 | |
| 5675 | // U^ -> void* |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5676 | if (RHSType->getAs<BlockPointerType>()) { |
| 5677 | if (LHSPointer->getPointeeType()->isVoidType()) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5678 | Kind = CK_BitCast; |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5679 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5680 | } |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5681 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5682 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 5683 | return Incompatible; |
| 5684 | } |
| 5685 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5686 | // Conversions to block pointers. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5687 | if (isa<BlockPointerType>(LHSType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5688 | // U^ -> T^ |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5689 | if (RHSType->isBlockPointerType()) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5690 | Kind = CK_BitCast; |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5691 | return checkBlockPointerTypesForAssignment(*this, LHSType, RHSType); |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5692 | } |
| 5693 | |
| 5694 | // int or null -> T^ |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5695 | if (RHSType->isIntegerType()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5696 | Kind = CK_IntegralToPointer; // FIXME: null |
Eli Friedman | d8f4f43 | 2009-02-25 04:20:42 +0000 | [diff] [blame] | 5697 | return IntToBlockPointer; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5698 | } |
| 5699 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5700 | // id -> T^ |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5701 | if (getLangOpts().ObjC1 && RHSType->isObjCIdType()) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5702 | Kind = CK_AnyPointerToBlockPointerCast; |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5703 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5704 | } |
Steve Naroff | b440686 | 2008-09-29 18:10:17 +0000 | [diff] [blame] | 5705 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5706 | // void* -> T^ |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5707 | if (const PointerType *RHSPT = RHSType->getAs<PointerType>()) |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5708 | if (RHSPT->getPointeeType()->isVoidType()) { |
| 5709 | Kind = CK_AnyPointerToBlockPointerCast; |
Douglas Gregor | 63a9490 | 2008-11-27 00:44:28 +0000 | [diff] [blame] | 5710 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5711 | } |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5712 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5713 | return Incompatible; |
| 5714 | } |
| 5715 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5716 | // Conversions to Objective-C pointers. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5717 | if (isa<ObjCObjectPointerType>(LHSType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5718 | // A* -> B* |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5719 | if (RHSType->isObjCObjectPointerType()) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5720 | Kind = CK_BitCast; |
Fariborz Jahanian | 04e5a25 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 5721 | Sema::AssignConvertType result = |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5722 | checkObjCPointerTypesForAssignment(*this, LHSType, RHSType); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5723 | if (getLangOpts().ObjCAutoRefCount && |
Fariborz Jahanian | 04e5a25 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 5724 | result == Compatible && |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5725 | !CheckObjCARCUnavailableWeakConversion(OrigLHSType, RHSType)) |
Fariborz Jahanian | 7a084ec | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 5726 | result = IncompatibleObjCWeakRef; |
Fariborz Jahanian | 04e5a25 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 5727 | return result; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5728 | } |
| 5729 | |
| 5730 | // int or null -> A* |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5731 | if (RHSType->isIntegerType()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5732 | Kind = CK_IntegralToPointer; // FIXME: null |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5733 | return IntToPointer; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5734 | } |
| 5735 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5736 | // In general, C pointers are not compatible with ObjC object pointers, |
| 5737 | // with two exceptions: |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5738 | if (isa<PointerType>(RHSType)) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5739 | Kind = CK_CPointerToObjCPointerCast; |
| 5740 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5741 | // - conversions from 'void*' |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5742 | if (RHSType->isVoidPointerType()) { |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 5743 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5744 | } |
| 5745 | |
| 5746 | // - conversions to 'Class' from its redefinition type |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5747 | if (LHSType->isObjCClassType() && |
| 5748 | Context.hasSameType(RHSType, |
Douglas Gregor | 01a4cf1 | 2011-08-11 20:58:55 +0000 | [diff] [blame] | 5749 | Context.getObjCClassRedefinitionType())) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5750 | return Compatible; |
| 5751 | } |
| 5752 | |
Steve Naroff | 67ef8ea | 2009-07-20 17:56:53 +0000 | [diff] [blame] | 5753 | return IncompatiblePointer; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5754 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5755 | |
| 5756 | // T^ -> A* |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5757 | if (RHSType->isBlockPointerType()) { |
John McCall | dc05b11 | 2011-09-10 01:16:55 +0000 | [diff] [blame] | 5758 | maybeExtendBlockObject(*this, RHS); |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5759 | Kind = CK_BlockPointerToObjCPointerCast; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5760 | return Compatible; |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5761 | } |
| 5762 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5763 | return Incompatible; |
| 5764 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5765 | |
| 5766 | // Conversions from pointers that are not covered by the above. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5767 | if (isa<PointerType>(RHSType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5768 | // T* -> _Bool |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5769 | if (LHSType == Context.BoolTy) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5770 | Kind = CK_PointerToBoolean; |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5771 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5772 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5773 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5774 | // T* -> int |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5775 | if (LHSType->isIntegerType()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5776 | Kind = CK_PointerToIntegral; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 5777 | return PointerToInt; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5778 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5779 | |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5780 | return Incompatible; |
Chris Lattner | fc144e2 | 2008-01-04 23:18:45 +0000 | [diff] [blame] | 5781 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5782 | |
| 5783 | // Conversions from Objective-C pointers that are not covered by the above. |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5784 | if (isa<ObjCObjectPointerType>(RHSType)) { |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5785 | // T* -> _Bool |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5786 | if (LHSType == Context.BoolTy) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5787 | Kind = CK_PointerToBoolean; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5788 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5789 | } |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5790 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5791 | // T* -> int |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5792 | if (LHSType->isIntegerType()) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5793 | Kind = CK_PointerToIntegral; |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5794 | return PointerToInt; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5795 | } |
| 5796 | |
Steve Naroff | 14108da | 2009-07-10 23:34:53 +0000 | [diff] [blame] | 5797 | return Incompatible; |
| 5798 | } |
Eli Friedman | f8f873d | 2008-05-30 18:07:22 +0000 | [diff] [blame] | 5799 | |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5800 | // struct A -> struct B |
Richard Trieu | facef2e | 2011-09-06 20:30:53 +0000 | [diff] [blame] | 5801 | if (isa<TagType>(LHSType) && isa<TagType>(RHSType)) { |
| 5802 | if (Context.typesAreCompatible(LHSType, RHSType)) { |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5803 | Kind = CK_NoOp; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5804 | return Compatible; |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5805 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5806 | } |
John McCall | b6cfa24 | 2011-01-31 22:28:28 +0000 | [diff] [blame] | 5807 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5808 | return Incompatible; |
| 5809 | } |
| 5810 | |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5811 | /// \brief Constructs a transparent union from an expression that is |
| 5812 | /// used to initialize the transparent union. |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5813 | static void ConstructTransparentUnion(Sema &S, ASTContext &C, |
| 5814 | ExprResult &EResult, QualType UnionType, |
| 5815 | FieldDecl *Field) { |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5816 | // Build an initializer list that designates the appropriate member |
| 5817 | // of the transparent union. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5818 | Expr *E = EResult.take(); |
Ted Kremenek | 709210f | 2010-04-13 23:39:13 +0000 | [diff] [blame] | 5819 | InitListExpr *Initializer = new (C) InitListExpr(C, SourceLocation(), |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 5820 | E, SourceLocation()); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5821 | Initializer->setType(UnionType); |
| 5822 | Initializer->setInitializedFieldInUnion(Field); |
| 5823 | |
| 5824 | // Build a compound literal constructing a value of the transparent |
| 5825 | // union type from this initializer list. |
John McCall | 42f56b5 | 2010-01-18 19:35:47 +0000 | [diff] [blame] | 5826 | TypeSourceInfo *unionTInfo = C.getTrivialTypeSourceInfo(UnionType); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5827 | EResult = S.Owned( |
| 5828 | new (C) CompoundLiteralExpr(SourceLocation(), unionTInfo, UnionType, |
| 5829 | VK_RValue, Initializer, false)); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5830 | } |
| 5831 | |
| 5832 | Sema::AssignConvertType |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 5833 | Sema::CheckTransparentUnionArgumentConstraints(QualType ArgType, |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5834 | ExprResult &RHS) { |
| 5835 | QualType RHSType = RHS.get()->getType(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5836 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5837 | // 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] | 5838 | // transparent_union GCC extension. |
| 5839 | const RecordType *UT = ArgType->getAsUnionType(); |
Argyrios Kyrtzidis | 40b598e | 2009-06-30 02:34:44 +0000 | [diff] [blame] | 5840 | if (!UT || !UT->getDecl()->hasAttr<TransparentUnionAttr>()) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5841 | return Incompatible; |
| 5842 | |
| 5843 | // The field to initialize within the transparent union. |
| 5844 | RecordDecl *UD = UT->getDecl(); |
| 5845 | FieldDecl *InitField = 0; |
| 5846 | // It's compatible if the expression matches any of the fields. |
Argyrios Kyrtzidis | 17945a0 | 2009-06-30 02:36:12 +0000 | [diff] [blame] | 5847 | for (RecordDecl::field_iterator it = UD->field_begin(), |
| 5848 | itend = UD->field_end(); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5849 | it != itend; ++it) { |
| 5850 | if (it->getType()->isPointerType()) { |
| 5851 | // If the transparent union contains a pointer type, we allow: |
| 5852 | // 1) void pointer |
| 5853 | // 2) null pointer constant |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5854 | if (RHSType->isPointerType()) |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 5855 | if (RHSType->castAs<PointerType>()->getPointeeType()->isVoidType()) { |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5856 | RHS = ImpCastExprToType(RHS.take(), it->getType(), CK_BitCast); |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5857 | InitField = *it; |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5858 | break; |
| 5859 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 5860 | |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5861 | if (RHS.get()->isNullPointerConstant(Context, |
| 5862 | Expr::NPC_ValueDependentIsNull)) { |
| 5863 | RHS = ImpCastExprToType(RHS.take(), it->getType(), |
| 5864 | CK_NullToPointer); |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5865 | InitField = *it; |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5866 | break; |
| 5867 | } |
| 5868 | } |
| 5869 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5870 | CastKind Kind = CK_Invalid; |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5871 | if (CheckAssignmentConstraints(it->getType(), RHS, Kind) |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5872 | == Compatible) { |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5873 | RHS = ImpCastExprToType(RHS.take(), it->getType(), Kind); |
David Blaikie | 581deb3 | 2012-06-06 20:45:41 +0000 | [diff] [blame] | 5874 | InitField = *it; |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5875 | break; |
| 5876 | } |
| 5877 | } |
| 5878 | |
| 5879 | if (!InitField) |
| 5880 | return Incompatible; |
| 5881 | |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5882 | ConstructTransparentUnion(*this, Context, RHS, ArgType, InitField); |
Douglas Gregor | 0c74e8a | 2009-04-29 22:16:16 +0000 | [diff] [blame] | 5883 | return Compatible; |
| 5884 | } |
| 5885 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5886 | Sema::AssignConvertType |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 5887 | Sema::CheckSingleAssignmentConstraints(QualType LHSType, ExprResult &RHS, |
| 5888 | bool Diagnose) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5889 | if (getLangOpts().CPlusPlus) { |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5890 | if (!LHSType->isRecordType() && !LHSType->isAtomicType()) { |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5891 | // C++ 5.17p3: If the left operand is not of class type, the |
| 5892 | // expression is implicitly converted (C++ 4) to the |
| 5893 | // cv-unqualified type of the left operand. |
Sebastian Redl | 091fffe | 2011-10-16 18:19:06 +0000 | [diff] [blame] | 5894 | ExprResult Res; |
| 5895 | if (Diagnose) { |
| 5896 | Res = PerformImplicitConversion(RHS.get(), LHSType.getUnqualifiedType(), |
| 5897 | AA_Assigning); |
| 5898 | } else { |
| 5899 | ImplicitConversionSequence ICS = |
| 5900 | TryImplicitConversion(RHS.get(), LHSType.getUnqualifiedType(), |
| 5901 | /*SuppressUserConversions=*/false, |
| 5902 | /*AllowExplicit=*/false, |
| 5903 | /*InOverloadResolution=*/false, |
| 5904 | /*CStyle=*/false, |
| 5905 | /*AllowObjCWritebackConversion=*/false); |
| 5906 | if (ICS.isFailure()) |
| 5907 | return Incompatible; |
| 5908 | Res = PerformImplicitConversion(RHS.get(), LHSType.getUnqualifiedType(), |
| 5909 | ICS, AA_Assigning); |
| 5910 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5911 | if (Res.isInvalid()) |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5912 | return Incompatible; |
Fariborz Jahanian | 7a084ec | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 5913 | Sema::AssignConvertType result = Compatible; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 5914 | if (getLangOpts().ObjCAutoRefCount && |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5915 | !CheckObjCARCUnavailableWeakConversion(LHSType, |
| 5916 | RHS.get()->getType())) |
Fariborz Jahanian | 7a084ec | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 5917 | result = IncompatibleObjCWeakRef; |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 5918 | RHS = Res; |
Fariborz Jahanian | 7a084ec | 2011-07-07 23:04:17 +0000 | [diff] [blame] | 5919 | return result; |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5920 | } |
| 5921 | |
| 5922 | // FIXME: Currently, we fall through and treat C++ classes like C |
| 5923 | // structures. |
Eli Friedman | b001de7 | 2011-10-06 23:00:33 +0000 | [diff] [blame] | 5924 | // FIXME: We also fall through for atomics; not sure what should |
| 5925 | // happen there, though. |
Sebastian Redl | 14b0c19 | 2011-09-24 17:48:00 +0000 | [diff] [blame] | 5926 | } |
Douglas Gregor | 98cd599 | 2008-10-21 23:43:52 +0000 | [diff] [blame] | 5927 | |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 5928 | // C99 6.5.16.1p1: the left operand is a pointer and the right is |
| 5929 | // a null pointer constant. |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5930 | if ((LHSType->isPointerType() || |
| 5931 | LHSType->isObjCObjectPointerType() || |
| 5932 | LHSType->isBlockPointerType()) |
| 5933 | && RHS.get()->isNullPointerConstant(Context, |
| 5934 | Expr::NPC_ValueDependentIsNull)) { |
| 5935 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_NullToPointer); |
Steve Naroff | 529a4ad | 2007-11-27 17:58:44 +0000 | [diff] [blame] | 5936 | return Compatible; |
| 5937 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5938 | |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 5939 | // This check seems unnatural, however it is necessary to ensure the proper |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 5940 | // conversion of functions/arrays. If the conversion were done for all |
Douglas Gregor | 02a24ee | 2009-11-03 16:56:39 +0000 | [diff] [blame] | 5941 | // DeclExpr's (created by ActOnIdExpression), it would mess up the unary |
Nick Lewycky | c133e9e | 2010-08-05 06:27:49 +0000 | [diff] [blame] | 5942 | // expressions that suppress this implicit conversion (&, sizeof). |
Chris Lattner | 943140e | 2007-10-16 02:55:40 +0000 | [diff] [blame] | 5943 | // |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5944 | // Suppress this for references: C++ 8.5.3p5. |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5945 | if (!LHSType->isReferenceType()) { |
| 5946 | RHS = DefaultFunctionArrayLvalueConversion(RHS.take()); |
| 5947 | if (RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 5948 | return Incompatible; |
| 5949 | } |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 5950 | |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 5951 | CastKind Kind = CK_Invalid; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 5952 | Sema::AssignConvertType result = |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5953 | CheckAssignmentConstraints(LHSType, RHS, Kind); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5954 | |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 5955 | // C99 6.5.16.1p2: The value of the right operand is converted to the |
| 5956 | // type of the assignment expression. |
Douglas Gregor | 9d293df | 2008-10-28 00:22:11 +0000 | [diff] [blame] | 5957 | // CheckAssignmentConstraints allows the left-hand side to be a reference, |
| 5958 | // so that we can use references in built-in functions even in C. |
| 5959 | // The getNonReferenceType() call makes sure that the resulting expression |
| 5960 | // does not have reference type. |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5961 | if (result != Incompatible && RHS.get()->getType() != LHSType) |
| 5962 | RHS = ImpCastExprToType(RHS.take(), |
| 5963 | LHSType.getNonLValueExprType(Context), Kind); |
Steve Naroff | f1120de | 2007-08-24 22:33:52 +0000 | [diff] [blame] | 5964 | return result; |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 5965 | } |
| 5966 | |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5967 | QualType Sema::InvalidOperands(SourceLocation Loc, ExprResult &LHS, |
| 5968 | ExprResult &RHS) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 5969 | Diag(Loc, diag::err_typecheck_invalid_operands) |
Richard Trieu | f7720da | 2011-09-06 20:40:12 +0000 | [diff] [blame] | 5970 | << LHS.get()->getType() << RHS.get()->getType() |
| 5971 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 5972 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 5973 | } |
| 5974 | |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5975 | QualType Sema::CheckVectorOperands(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 5976 | SourceLocation Loc, bool IsCompAssign) { |
Richard Smith | 9c129f8 | 2011-10-28 03:31:48 +0000 | [diff] [blame] | 5977 | if (!IsCompAssign) { |
| 5978 | LHS = DefaultFunctionArrayLvalueConversion(LHS.take()); |
| 5979 | if (LHS.isInvalid()) |
| 5980 | return QualType(); |
| 5981 | } |
| 5982 | RHS = DefaultFunctionArrayLvalueConversion(RHS.take()); |
| 5983 | if (RHS.isInvalid()) |
| 5984 | return QualType(); |
| 5985 | |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5986 | // For conversion purposes, we ignore any qualifiers. |
Nate Begeman | 1330b0e | 2008-04-04 01:30:25 +0000 | [diff] [blame] | 5987 | // For example, "const float" and "float" are equivalent. |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5988 | QualType LHSType = |
| 5989 | Context.getCanonicalType(LHS.get()->getType()).getUnqualifiedType(); |
| 5990 | QualType RHSType = |
| 5991 | Context.getCanonicalType(RHS.get()->getType()).getUnqualifiedType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 5992 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 5993 | // If the vector types are identical, return. |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5994 | if (LHSType == RHSType) |
| 5995 | return LHSType; |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 5996 | |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 5997 | // Handle the case of equivalent AltiVec and GCC vector types |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 5998 | if (LHSType->isVectorType() && RHSType->isVectorType() && |
| 5999 | Context.areCompatibleVectorTypes(LHSType, RHSType)) { |
| 6000 | if (LHSType->isExtVectorType()) { |
| 6001 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); |
| 6002 | return LHSType; |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 6003 | } |
| 6004 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6005 | if (!IsCompAssign) |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6006 | LHS = ImpCastExprToType(LHS.take(), RHSType, CK_BitCast); |
| 6007 | return RHSType; |
Douglas Gregor | 255210e | 2010-08-06 10:14:59 +0000 | [diff] [blame] | 6008 | } |
| 6009 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6010 | if (getLangOpts().LaxVectorConversions && |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6011 | Context.getTypeSize(LHSType) == Context.getTypeSize(RHSType)) { |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 6012 | // If we are allowing lax vector conversions, and LHS and RHS are both |
| 6013 | // vectors, the total size only needs to be the same. This is a |
| 6014 | // bitcast; no bits are changed but the result type is different. |
| 6015 | // FIXME: Should we really be allowing this? |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6016 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); |
| 6017 | return LHSType; |
Eli Friedman | b9b4b78 | 2011-06-23 18:10:35 +0000 | [diff] [blame] | 6018 | } |
| 6019 | |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6020 | // Canonicalize the ExtVector to the LHS, remember if we swapped so we can |
| 6021 | // swap back (so that we don't reverse the inputs to a subtract, for instance. |
| 6022 | bool swapped = false; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6023 | if (RHSType->isExtVectorType() && !IsCompAssign) { |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6024 | swapped = true; |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6025 | std::swap(RHS, LHS); |
| 6026 | std::swap(RHSType, LHSType); |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6027 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6028 | |
Nate Begeman | dde2598 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 6029 | // Handle the case of an ext vector and scalar. |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6030 | if (const ExtVectorType *LV = LHSType->getAs<ExtVectorType>()) { |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6031 | QualType EltTy = LV->getElementType(); |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6032 | if (EltTy->isIntegralType(Context) && RHSType->isIntegralType(Context)) { |
| 6033 | int order = Context.getIntegerTypeOrder(EltTy, RHSType); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6034 | if (order > 0) |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6035 | RHS = ImpCastExprToType(RHS.take(), EltTy, CK_IntegralCast); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6036 | if (order >= 0) { |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6037 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_VectorSplat); |
| 6038 | if (swapped) std::swap(RHS, LHS); |
| 6039 | return LHSType; |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6040 | } |
| 6041 | } |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6042 | if (EltTy->isRealFloatingType() && RHSType->isScalarType() && |
| 6043 | RHSType->isRealFloatingType()) { |
| 6044 | int order = Context.getFloatingTypeOrder(EltTy, RHSType); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6045 | if (order > 0) |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6046 | RHS = ImpCastExprToType(RHS.take(), EltTy, CK_FloatingCast); |
John McCall | daa8e4e | 2010-11-15 09:13:47 +0000 | [diff] [blame] | 6047 | if (order >= 0) { |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6048 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_VectorSplat); |
| 6049 | if (swapped) std::swap(RHS, LHS); |
| 6050 | return LHSType; |
Nate Begeman | 1bd1f6e | 2009-06-28 02:36:38 +0000 | [diff] [blame] | 6051 | } |
Nate Begeman | 4119d1a | 2007-12-30 02:59:45 +0000 | [diff] [blame] | 6052 | } |
| 6053 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6054 | |
Nate Begeman | dde2598 | 2009-06-28 19:12:57 +0000 | [diff] [blame] | 6055 | // Vectors of different size or scalar and non-ext-vector are errors. |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6056 | if (swapped) std::swap(RHS, LHS); |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 6057 | Diag(Loc, diag::err_typecheck_vector_not_convertable) |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6058 | << LHS.get()->getType() << RHS.get()->getType() |
| 6059 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6060 | return QualType(); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 6061 | } |
| 6062 | |
Richard Trieu | 481037f | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 6063 | // checkArithmeticNull - Detect when a NULL constant is used improperly in an |
| 6064 | // expression. These are mainly cases where the null pointer is used as an |
| 6065 | // integer instead of a pointer. |
| 6066 | static void checkArithmeticNull(Sema &S, ExprResult &LHS, ExprResult &RHS, |
| 6067 | SourceLocation Loc, bool IsCompare) { |
| 6068 | // The canonical way to check for a GNU null is with isNullPointerConstant, |
| 6069 | // but we use a bit of a hack here for speed; this is a relatively |
| 6070 | // hot path, and isNullPointerConstant is slow. |
| 6071 | bool LHSNull = isa<GNUNullExpr>(LHS.get()->IgnoreParenImpCasts()); |
| 6072 | bool RHSNull = isa<GNUNullExpr>(RHS.get()->IgnoreParenImpCasts()); |
| 6073 | |
| 6074 | QualType NonNullType = LHSNull ? RHS.get()->getType() : LHS.get()->getType(); |
| 6075 | |
| 6076 | // Avoid analyzing cases where the result will either be invalid (and |
| 6077 | // diagnosed as such) or entirely valid and not something to warn about. |
| 6078 | if ((!LHSNull && !RHSNull) || NonNullType->isBlockPointerType() || |
| 6079 | NonNullType->isMemberPointerType() || NonNullType->isFunctionType()) |
| 6080 | return; |
| 6081 | |
| 6082 | // Comparison operations would not make sense with a null pointer no matter |
| 6083 | // what the other expression is. |
| 6084 | if (!IsCompare) { |
| 6085 | S.Diag(Loc, diag::warn_null_in_arithmetic_operation) |
| 6086 | << (LHSNull ? LHS.get()->getSourceRange() : SourceRange()) |
| 6087 | << (RHSNull ? RHS.get()->getSourceRange() : SourceRange()); |
| 6088 | return; |
| 6089 | } |
| 6090 | |
| 6091 | // The rest of the operations only make sense with a null pointer |
| 6092 | // if the other expression is a pointer. |
| 6093 | if (LHSNull == RHSNull || NonNullType->isAnyPointerType() || |
| 6094 | NonNullType->canDecayToPointerType()) |
| 6095 | return; |
| 6096 | |
| 6097 | S.Diag(Loc, diag::warn_null_in_comparison_operation) |
| 6098 | << LHSNull /* LHS is NULL */ << NonNullType |
| 6099 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
| 6100 | } |
| 6101 | |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6102 | QualType Sema::CheckMultiplyDivideOperands(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6103 | SourceLocation Loc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6104 | bool IsCompAssign, bool IsDiv) { |
Richard Trieu | 481037f | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 6105 | checkArithmeticNull(*this, LHS, RHS, Loc, /*isCompare=*/false); |
| 6106 | |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6107 | if (LHS.get()->getType()->isVectorType() || |
| 6108 | RHS.get()->getType()->isVectorType()) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6109 | return CheckVectorOperands(LHS, RHS, Loc, IsCompAssign); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6110 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6111 | QualType compType = UsualArithmeticConversions(LHS, RHS, IsCompAssign); |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6112 | if (LHS.isInvalid() || RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6113 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6114 | |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 6115 | |
Eli Friedman | 860a319 | 2012-06-16 02:19:17 +0000 | [diff] [blame] | 6116 | if (compType.isNull() || !compType->isArithmeticType()) |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6117 | return InvalidOperands(Loc, LHS, RHS); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6118 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6119 | // Check for division by zero. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6120 | if (IsDiv && |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6121 | RHS.get()->isNullPointerConstant(Context, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6122 | Expr::NPC_ValueDependentIsNotNull)) |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6123 | DiagRuntimeBehavior(Loc, RHS.get(), PDiag(diag::warn_division_by_zero) |
| 6124 | << RHS.get()->getSourceRange()); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6125 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6126 | return compType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6127 | } |
| 6128 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6129 | QualType Sema::CheckRemainderOperands( |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6130 | ExprResult &LHS, ExprResult &RHS, SourceLocation Loc, bool IsCompAssign) { |
Richard Trieu | 481037f | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 6131 | checkArithmeticNull(*this, LHS, RHS, Loc, /*isCompare=*/false); |
| 6132 | |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6133 | if (LHS.get()->getType()->isVectorType() || |
| 6134 | RHS.get()->getType()->isVectorType()) { |
| 6135 | if (LHS.get()->getType()->hasIntegerRepresentation() && |
| 6136 | RHS.get()->getType()->hasIntegerRepresentation()) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6137 | return CheckVectorOperands(LHS, RHS, Loc, IsCompAssign); |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6138 | return InvalidOperands(Loc, LHS, RHS); |
Daniel Dunbar | 523aa60 | 2009-01-05 22:55:36 +0000 | [diff] [blame] | 6139 | } |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 6140 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6141 | QualType compType = UsualArithmeticConversions(LHS, RHS, IsCompAssign); |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6142 | if (LHS.isInvalid() || RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6143 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6144 | |
Eli Friedman | 860a319 | 2012-06-16 02:19:17 +0000 | [diff] [blame] | 6145 | if (compType.isNull() || !compType->isIntegerType()) |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6146 | return InvalidOperands(Loc, LHS, RHS); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6147 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6148 | // Check for remainder by zero. |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6149 | if (RHS.get()->isNullPointerConstant(Context, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6150 | Expr::NPC_ValueDependentIsNotNull)) |
Richard Trieu | 08062aa | 2011-09-06 21:01:04 +0000 | [diff] [blame] | 6151 | DiagRuntimeBehavior(Loc, RHS.get(), PDiag(diag::warn_remainder_by_zero) |
| 6152 | << RHS.get()->getSourceRange()); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6153 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6154 | return compType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6155 | } |
| 6156 | |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6157 | /// \brief Diagnose invalid arithmetic on two void pointers. |
| 6158 | static void diagnoseArithmeticOnTwoVoidPointers(Sema &S, SourceLocation Loc, |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6159 | Expr *LHSExpr, Expr *RHSExpr) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6160 | S.Diag(Loc, S.getLangOpts().CPlusPlus |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6161 | ? diag::err_typecheck_pointer_arith_void_type |
| 6162 | : diag::ext_gnu_void_ptr) |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6163 | << 1 /* two pointers */ << LHSExpr->getSourceRange() |
| 6164 | << RHSExpr->getSourceRange(); |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6165 | } |
| 6166 | |
| 6167 | /// \brief Diagnose invalid arithmetic on a void pointer. |
| 6168 | static void diagnoseArithmeticOnVoidPointer(Sema &S, SourceLocation Loc, |
| 6169 | Expr *Pointer) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6170 | S.Diag(Loc, S.getLangOpts().CPlusPlus |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6171 | ? diag::err_typecheck_pointer_arith_void_type |
| 6172 | : diag::ext_gnu_void_ptr) |
| 6173 | << 0 /* one pointer */ << Pointer->getSourceRange(); |
| 6174 | } |
| 6175 | |
| 6176 | /// \brief Diagnose invalid arithmetic on two function pointers. |
| 6177 | static void diagnoseArithmeticOnTwoFunctionPointers(Sema &S, SourceLocation Loc, |
| 6178 | Expr *LHS, Expr *RHS) { |
| 6179 | assert(LHS->getType()->isAnyPointerType()); |
| 6180 | assert(RHS->getType()->isAnyPointerType()); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6181 | S.Diag(Loc, S.getLangOpts().CPlusPlus |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6182 | ? diag::err_typecheck_pointer_arith_function_type |
| 6183 | : diag::ext_gnu_ptr_func_arith) |
| 6184 | << 1 /* two pointers */ << LHS->getType()->getPointeeType() |
| 6185 | // We only show the second type if it differs from the first. |
| 6186 | << (unsigned)!S.Context.hasSameUnqualifiedType(LHS->getType(), |
| 6187 | RHS->getType()) |
| 6188 | << RHS->getType()->getPointeeType() |
| 6189 | << LHS->getSourceRange() << RHS->getSourceRange(); |
| 6190 | } |
| 6191 | |
| 6192 | /// \brief Diagnose invalid arithmetic on a function pointer. |
| 6193 | static void diagnoseArithmeticOnFunctionPointer(Sema &S, SourceLocation Loc, |
| 6194 | Expr *Pointer) { |
| 6195 | assert(Pointer->getType()->isAnyPointerType()); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6196 | S.Diag(Loc, S.getLangOpts().CPlusPlus |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6197 | ? diag::err_typecheck_pointer_arith_function_type |
| 6198 | : diag::ext_gnu_ptr_func_arith) |
| 6199 | << 0 /* one pointer */ << Pointer->getType()->getPointeeType() |
| 6200 | << 0 /* one pointer, so only one type */ |
| 6201 | << Pointer->getSourceRange(); |
| 6202 | } |
| 6203 | |
Richard Trieu | d9f1934 | 2011-09-12 18:08:02 +0000 | [diff] [blame] | 6204 | /// \brief Emit error if Operand is incomplete pointer type |
Richard Trieu | 097ecd2 | 2011-09-02 02:15:37 +0000 | [diff] [blame] | 6205 | /// |
| 6206 | /// \returns True if pointer has incomplete type |
| 6207 | static bool checkArithmeticIncompletePointerType(Sema &S, SourceLocation Loc, |
| 6208 | Expr *Operand) { |
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 6209 | assert(Operand->getType()->isAnyPointerType() && |
| 6210 | !Operand->getType()->isDependentType()); |
| 6211 | QualType PointeeTy = Operand->getType()->getPointeeType(); |
| 6212 | return S.RequireCompleteType(Loc, PointeeTy, |
| 6213 | diag::err_typecheck_arithmetic_incomplete_type, |
| 6214 | PointeeTy, Operand->getSourceRange()); |
Richard Trieu | 097ecd2 | 2011-09-02 02:15:37 +0000 | [diff] [blame] | 6215 | } |
| 6216 | |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6217 | /// \brief Check the validity of an arithmetic pointer operand. |
| 6218 | /// |
| 6219 | /// If the operand has pointer type, this code will check for pointer types |
| 6220 | /// which are invalid in arithmetic operations. These will be diagnosed |
| 6221 | /// appropriately, including whether or not the use is supported as an |
| 6222 | /// extension. |
| 6223 | /// |
| 6224 | /// \returns True when the operand is valid to use (even if as an extension). |
| 6225 | static bool checkArithmeticOpPointerOperand(Sema &S, SourceLocation Loc, |
| 6226 | Expr *Operand) { |
| 6227 | if (!Operand->getType()->isAnyPointerType()) return true; |
| 6228 | |
| 6229 | QualType PointeeTy = Operand->getType()->getPointeeType(); |
| 6230 | if (PointeeTy->isVoidType()) { |
| 6231 | diagnoseArithmeticOnVoidPointer(S, Loc, Operand); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6232 | return !S.getLangOpts().CPlusPlus; |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6233 | } |
| 6234 | if (PointeeTy->isFunctionType()) { |
| 6235 | diagnoseArithmeticOnFunctionPointer(S, Loc, Operand); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6236 | return !S.getLangOpts().CPlusPlus; |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6237 | } |
| 6238 | |
Richard Trieu | 097ecd2 | 2011-09-02 02:15:37 +0000 | [diff] [blame] | 6239 | if (checkArithmeticIncompletePointerType(S, Loc, Operand)) return false; |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6240 | |
| 6241 | return true; |
| 6242 | } |
| 6243 | |
| 6244 | /// \brief Check the validity of a binary arithmetic operation w.r.t. pointer |
| 6245 | /// operands. |
| 6246 | /// |
| 6247 | /// This routine will diagnose any invalid arithmetic on pointer operands much |
| 6248 | /// like \see checkArithmeticOpPointerOperand. However, it has special logic |
| 6249 | /// for emitting a single diagnostic even for operations where both LHS and RHS |
| 6250 | /// are (potentially problematic) pointers. |
| 6251 | /// |
| 6252 | /// \returns True when the operand is valid to use (even if as an extension). |
| 6253 | static bool checkArithmeticBinOpPointerOperands(Sema &S, SourceLocation Loc, |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6254 | Expr *LHSExpr, Expr *RHSExpr) { |
| 6255 | bool isLHSPointer = LHSExpr->getType()->isAnyPointerType(); |
| 6256 | bool isRHSPointer = RHSExpr->getType()->isAnyPointerType(); |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6257 | if (!isLHSPointer && !isRHSPointer) return true; |
| 6258 | |
| 6259 | QualType LHSPointeeTy, RHSPointeeTy; |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6260 | if (isLHSPointer) LHSPointeeTy = LHSExpr->getType()->getPointeeType(); |
| 6261 | if (isRHSPointer) RHSPointeeTy = RHSExpr->getType()->getPointeeType(); |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6262 | |
| 6263 | // Check for arithmetic on pointers to incomplete types. |
| 6264 | bool isLHSVoidPtr = isLHSPointer && LHSPointeeTy->isVoidType(); |
| 6265 | bool isRHSVoidPtr = isRHSPointer && RHSPointeeTy->isVoidType(); |
| 6266 | if (isLHSVoidPtr || isRHSVoidPtr) { |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6267 | if (!isRHSVoidPtr) diagnoseArithmeticOnVoidPointer(S, Loc, LHSExpr); |
| 6268 | else if (!isLHSVoidPtr) diagnoseArithmeticOnVoidPointer(S, Loc, RHSExpr); |
| 6269 | else diagnoseArithmeticOnTwoVoidPointers(S, Loc, LHSExpr, RHSExpr); |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6270 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6271 | return !S.getLangOpts().CPlusPlus; |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6272 | } |
| 6273 | |
| 6274 | bool isLHSFuncPtr = isLHSPointer && LHSPointeeTy->isFunctionType(); |
| 6275 | bool isRHSFuncPtr = isRHSPointer && RHSPointeeTy->isFunctionType(); |
| 6276 | if (isLHSFuncPtr || isRHSFuncPtr) { |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6277 | if (!isRHSFuncPtr) diagnoseArithmeticOnFunctionPointer(S, Loc, LHSExpr); |
| 6278 | else if (!isLHSFuncPtr) diagnoseArithmeticOnFunctionPointer(S, Loc, |
| 6279 | RHSExpr); |
| 6280 | else diagnoseArithmeticOnTwoFunctionPointers(S, Loc, LHSExpr, RHSExpr); |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6281 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6282 | return !S.getLangOpts().CPlusPlus; |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6283 | } |
| 6284 | |
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 6285 | if (isLHSPointer && checkArithmeticIncompletePointerType(S, Loc, LHSExpr)) |
| 6286 | return false; |
| 6287 | if (isRHSPointer && checkArithmeticIncompletePointerType(S, Loc, RHSExpr)) |
| 6288 | return false; |
Richard Trieu | 097ecd2 | 2011-09-02 02:15:37 +0000 | [diff] [blame] | 6289 | |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6290 | return true; |
| 6291 | } |
| 6292 | |
Nico Weber | 1cb2d74 | 2012-03-02 22:01:22 +0000 | [diff] [blame] | 6293 | /// diagnoseStringPlusInt - Emit a warning when adding an integer to a string |
| 6294 | /// literal. |
| 6295 | static void diagnoseStringPlusInt(Sema &Self, SourceLocation OpLoc, |
| 6296 | Expr *LHSExpr, Expr *RHSExpr) { |
| 6297 | StringLiteral* StrExpr = dyn_cast<StringLiteral>(LHSExpr->IgnoreImpCasts()); |
| 6298 | Expr* IndexExpr = RHSExpr; |
| 6299 | if (!StrExpr) { |
| 6300 | StrExpr = dyn_cast<StringLiteral>(RHSExpr->IgnoreImpCasts()); |
| 6301 | IndexExpr = LHSExpr; |
| 6302 | } |
| 6303 | |
| 6304 | bool IsStringPlusInt = StrExpr && |
| 6305 | IndexExpr->getType()->isIntegralOrUnscopedEnumerationType(); |
| 6306 | if (!IsStringPlusInt) |
| 6307 | return; |
| 6308 | |
| 6309 | llvm::APSInt index; |
| 6310 | if (IndexExpr->EvaluateAsInt(index, Self.getASTContext())) { |
| 6311 | unsigned StrLenWithNull = StrExpr->getLength() + 1; |
| 6312 | if (index.isNonNegative() && |
| 6313 | index <= llvm::APSInt(llvm::APInt(index.getBitWidth(), StrLenWithNull), |
| 6314 | index.isUnsigned())) |
| 6315 | return; |
| 6316 | } |
| 6317 | |
| 6318 | SourceRange DiagRange(LHSExpr->getLocStart(), RHSExpr->getLocEnd()); |
| 6319 | Self.Diag(OpLoc, diag::warn_string_plus_int) |
| 6320 | << DiagRange << IndexExpr->IgnoreImpCasts()->getType(); |
| 6321 | |
| 6322 | // Only print a fixit for "str" + int, not for int + "str". |
| 6323 | if (IndexExpr == RHSExpr) { |
| 6324 | SourceLocation EndLoc = Self.PP.getLocForEndOfToken(RHSExpr->getLocEnd()); |
| 6325 | Self.Diag(OpLoc, diag::note_string_plus_int_silence) |
| 6326 | << FixItHint::CreateInsertion(LHSExpr->getLocStart(), "&") |
| 6327 | << FixItHint::CreateReplacement(SourceRange(OpLoc), "[") |
| 6328 | << FixItHint::CreateInsertion(EndLoc, "]"); |
| 6329 | } else |
| 6330 | Self.Diag(OpLoc, diag::note_string_plus_int_silence); |
| 6331 | } |
| 6332 | |
Richard Trieu | d9f1934 | 2011-09-12 18:08:02 +0000 | [diff] [blame] | 6333 | /// \brief Emit error when two pointers are incompatible. |
Richard Trieu | db44a6b | 2011-09-01 22:53:23 +0000 | [diff] [blame] | 6334 | static void diagnosePointerIncompatibility(Sema &S, SourceLocation Loc, |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6335 | Expr *LHSExpr, Expr *RHSExpr) { |
| 6336 | assert(LHSExpr->getType()->isAnyPointerType()); |
| 6337 | assert(RHSExpr->getType()->isAnyPointerType()); |
Richard Trieu | db44a6b | 2011-09-01 22:53:23 +0000 | [diff] [blame] | 6338 | S.Diag(Loc, diag::err_typecheck_sub_ptr_compatible) |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6339 | << LHSExpr->getType() << RHSExpr->getType() << LHSExpr->getSourceRange() |
| 6340 | << RHSExpr->getSourceRange(); |
Richard Trieu | db44a6b | 2011-09-01 22:53:23 +0000 | [diff] [blame] | 6341 | } |
| 6342 | |
Chris Lattner | 7ef655a | 2010-01-12 21:23:57 +0000 | [diff] [blame] | 6343 | QualType Sema::CheckAdditionOperands( // C99 6.5.6 |
Nico Weber | 1cb2d74 | 2012-03-02 22:01:22 +0000 | [diff] [blame] | 6344 | ExprResult &LHS, ExprResult &RHS, SourceLocation Loc, unsigned Opc, |
| 6345 | QualType* CompLHSTy) { |
Richard Trieu | 481037f | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 6346 | checkArithmeticNull(*this, LHS, RHS, Loc, /*isCompare=*/false); |
| 6347 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6348 | if (LHS.get()->getType()->isVectorType() || |
| 6349 | RHS.get()->getType()->isVectorType()) { |
| 6350 | QualType compType = CheckVectorOperands(LHS, RHS, Loc, CompLHSTy); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6351 | if (CompLHSTy) *CompLHSTy = compType; |
| 6352 | return compType; |
| 6353 | } |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 6354 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6355 | QualType compType = UsualArithmeticConversions(LHS, RHS, CompLHSTy); |
| 6356 | if (LHS.isInvalid() || RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6357 | return QualType(); |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6358 | |
Nico Weber | 1cb2d74 | 2012-03-02 22:01:22 +0000 | [diff] [blame] | 6359 | // Diagnose "string literal" '+' int. |
| 6360 | if (Opc == BO_Add) |
| 6361 | diagnoseStringPlusInt(*this, Loc, LHS.get(), RHS.get()); |
| 6362 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6363 | // handle the common case first (both operands are arithmetic). |
Eli Friedman | 860a319 | 2012-06-16 02:19:17 +0000 | [diff] [blame] | 6364 | if (!compType.isNull() && compType->isArithmeticType()) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6365 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 6366 | return compType; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6367 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6368 | |
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 6369 | // Type-checking. Ultimately the pointer's going to be in PExp; |
| 6370 | // note that we bias towards the LHS being the pointer. |
| 6371 | Expr *PExp = LHS.get(), *IExp = RHS.get(); |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6372 | |
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 6373 | bool isObjCPointer; |
| 6374 | if (PExp->getType()->isPointerType()) { |
| 6375 | isObjCPointer = false; |
| 6376 | } else if (PExp->getType()->isObjCObjectPointerType()) { |
| 6377 | isObjCPointer = true; |
| 6378 | } else { |
| 6379 | std::swap(PExp, IExp); |
| 6380 | if (PExp->getType()->isPointerType()) { |
| 6381 | isObjCPointer = false; |
| 6382 | } else if (PExp->getType()->isObjCObjectPointerType()) { |
| 6383 | isObjCPointer = true; |
| 6384 | } else { |
| 6385 | return InvalidOperands(Loc, LHS, RHS); |
| 6386 | } |
| 6387 | } |
| 6388 | assert(PExp->getType()->isAnyPointerType()); |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6389 | |
Richard Trieu | 6eef9fb | 2011-09-12 18:37:54 +0000 | [diff] [blame] | 6390 | if (!IExp->getType()->isIntegerType()) |
| 6391 | return InvalidOperands(Loc, LHS, RHS); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6392 | |
Richard Trieu | 6eef9fb | 2011-09-12 18:37:54 +0000 | [diff] [blame] | 6393 | if (!checkArithmeticOpPointerOperand(*this, Loc, PExp)) |
| 6394 | return QualType(); |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 6395 | |
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 6396 | if (isObjCPointer && checkArithmeticOnObjCPointer(*this, Loc, PExp)) |
Richard Trieu | 6eef9fb | 2011-09-12 18:37:54 +0000 | [diff] [blame] | 6397 | return QualType(); |
| 6398 | |
| 6399 | // Check array bounds for pointer arithemtic |
| 6400 | CheckArrayAccess(PExp, IExp); |
| 6401 | |
| 6402 | if (CompLHSTy) { |
| 6403 | QualType LHSTy = Context.isPromotableBitField(LHS.get()); |
| 6404 | if (LHSTy.isNull()) { |
| 6405 | LHSTy = LHS.get()->getType(); |
| 6406 | if (LHSTy->isPromotableIntegerType()) |
| 6407 | LHSTy = Context.getPromotedIntegerType(LHSTy); |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6408 | } |
Richard Trieu | 6eef9fb | 2011-09-12 18:37:54 +0000 | [diff] [blame] | 6409 | *CompLHSTy = LHSTy; |
Eli Friedman | d72d16e | 2008-05-18 18:08:51 +0000 | [diff] [blame] | 6410 | } |
| 6411 | |
Richard Trieu | 6eef9fb | 2011-09-12 18:37:54 +0000 | [diff] [blame] | 6412 | return PExp->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6413 | } |
| 6414 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 6415 | // C99 6.5.6 |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6416 | QualType Sema::CheckSubtractionOperands(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6417 | SourceLocation Loc, |
| 6418 | QualType* CompLHSTy) { |
Richard Trieu | 481037f | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 6419 | checkArithmeticNull(*this, LHS, RHS, Loc, /*isCompare=*/false); |
| 6420 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6421 | if (LHS.get()->getType()->isVectorType() || |
| 6422 | RHS.get()->getType()->isVectorType()) { |
| 6423 | QualType compType = CheckVectorOperands(LHS, RHS, Loc, CompLHSTy); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6424 | if (CompLHSTy) *CompLHSTy = compType; |
| 6425 | return compType; |
| 6426 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6427 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6428 | QualType compType = UsualArithmeticConversions(LHS, RHS, CompLHSTy); |
| 6429 | if (LHS.isInvalid() || RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6430 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6431 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6432 | // Enforce type constraints: C99 6.5.6p3. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6433 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6434 | // Handle the common case first (both operands are arithmetic). |
Eli Friedman | 860a319 | 2012-06-16 02:19:17 +0000 | [diff] [blame] | 6435 | if (!compType.isNull() && compType->isArithmeticType()) { |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6436 | if (CompLHSTy) *CompLHSTy = compType; |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 6437 | return compType; |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6438 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6439 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6440 | // Either ptr - int or ptr - ptr. |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6441 | if (LHS.get()->getType()->isAnyPointerType()) { |
| 6442 | QualType lpointee = LHS.get()->getType()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6443 | |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 6444 | // Diagnose bad cases where we step over interface counts. |
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 6445 | if (LHS.get()->getType()->isObjCObjectPointerType() && |
| 6446 | checkArithmeticOnObjCPointer(*this, Loc, LHS.get())) |
Chris Lattner | b5f1562 | 2009-04-24 23:50:08 +0000 | [diff] [blame] | 6447 | return QualType(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6448 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6449 | // The result type of a pointer-int computation is the pointer type. |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6450 | if (RHS.get()->getType()->isIntegerType()) { |
| 6451 | if (!checkArithmeticOpPointerOperand(*this, Loc, LHS.get())) |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6452 | return QualType(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6453 | |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 6454 | // Check array bounds for pointer arithemtic |
Richard Smith | 25b009a | 2011-12-16 19:31:14 +0000 | [diff] [blame] | 6455 | CheckArrayAccess(LHS.get(), RHS.get(), /*ArraySubscriptExpr*/0, |
| 6456 | /*AllowOnePastEnd*/true, /*IndexNegated*/true); |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 6457 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6458 | if (CompLHSTy) *CompLHSTy = LHS.get()->getType(); |
| 6459 | return LHS.get()->getType(); |
Douglas Gregor | e7450f5 | 2009-03-24 19:52:54 +0000 | [diff] [blame] | 6460 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6461 | |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6462 | // Handle pointer-pointer subtractions. |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6463 | if (const PointerType *RHSPTy |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6464 | = RHS.get()->getType()->getAs<PointerType>()) { |
Eli Friedman | 8e54ad0 | 2008-02-08 01:19:44 +0000 | [diff] [blame] | 6465 | QualType rpointee = RHSPTy->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6466 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 6467 | if (getLangOpts().CPlusPlus) { |
Eli Friedman | 88d936b | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 6468 | // Pointee types must be the same: C++ [expr.add] |
| 6469 | if (!Context.hasSameUnqualifiedType(lpointee, rpointee)) { |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6470 | diagnosePointerIncompatibility(*this, Loc, LHS.get(), RHS.get()); |
Eli Friedman | 88d936b | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 6471 | } |
| 6472 | } else { |
| 6473 | // Pointee types must be compatible C99 6.5.6p3 |
| 6474 | if (!Context.typesAreCompatible( |
| 6475 | Context.getCanonicalType(lpointee).getUnqualifiedType(), |
| 6476 | Context.getCanonicalType(rpointee).getUnqualifiedType())) { |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6477 | diagnosePointerIncompatibility(*this, Loc, LHS.get(), RHS.get()); |
Eli Friedman | 88d936b | 2009-05-16 13:54:38 +0000 | [diff] [blame] | 6478 | return QualType(); |
| 6479 | } |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6480 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6481 | |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6482 | if (!checkArithmeticBinOpPointerOperands(*this, Loc, |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6483 | LHS.get(), RHS.get())) |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 6484 | return QualType(); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6485 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6486 | if (CompLHSTy) *CompLHSTy = LHS.get()->getType(); |
Chris Lattner | 6e4ab61 | 2007-12-09 21:53:25 +0000 | [diff] [blame] | 6487 | return Context.getPointerDiffType(); |
| 6488 | } |
| 6489 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6490 | |
Richard Trieu | def7584 | 2011-09-06 21:13:51 +0000 | [diff] [blame] | 6491 | return InvalidOperands(Loc, LHS, RHS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6492 | } |
| 6493 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6494 | static bool isScopedEnumerationType(QualType T) { |
| 6495 | if (const EnumType *ET = dyn_cast<EnumType>(T)) |
| 6496 | return ET->getDecl()->isScoped(); |
| 6497 | return false; |
| 6498 | } |
| 6499 | |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6500 | static void DiagnoseBadShiftValues(Sema& S, ExprResult &LHS, ExprResult &RHS, |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6501 | SourceLocation Loc, unsigned Opc, |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6502 | QualType LHSType) { |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6503 | llvm::APSInt Right; |
| 6504 | // Check right/shifter operand |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6505 | if (RHS.get()->isValueDependent() || |
| 6506 | !RHS.get()->isIntegerConstantExpr(Right, S.Context)) |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6507 | return; |
| 6508 | |
| 6509 | if (Right.isNegative()) { |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6510 | S.DiagRuntimeBehavior(Loc, RHS.get(), |
Ted Kremenek | 082bf7a | 2011-03-01 18:09:31 +0000 | [diff] [blame] | 6511 | S.PDiag(diag::warn_shift_negative) |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6512 | << RHS.get()->getSourceRange()); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6513 | return; |
| 6514 | } |
| 6515 | llvm::APInt LeftBits(Right.getBitWidth(), |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6516 | S.Context.getTypeSize(LHS.get()->getType())); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6517 | if (Right.uge(LeftBits)) { |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6518 | S.DiagRuntimeBehavior(Loc, RHS.get(), |
Ted Kremenek | 425a31e | 2011-03-01 19:13:22 +0000 | [diff] [blame] | 6519 | S.PDiag(diag::warn_shift_gt_typewidth) |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6520 | << RHS.get()->getSourceRange()); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6521 | return; |
| 6522 | } |
| 6523 | if (Opc != BO_Shl) |
| 6524 | return; |
| 6525 | |
| 6526 | // When left shifting an ICE which is signed, we can check for overflow which |
| 6527 | // according to C++ has undefined behavior ([expr.shift] 5.8/2). Unsigned |
| 6528 | // integers have defined behavior modulo one more than the maximum value |
| 6529 | // representable in the result type, so never warn for those. |
| 6530 | llvm::APSInt Left; |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6531 | if (LHS.get()->isValueDependent() || |
| 6532 | !LHS.get()->isIntegerConstantExpr(Left, S.Context) || |
| 6533 | LHSType->hasUnsignedIntegerRepresentation()) |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6534 | return; |
| 6535 | llvm::APInt ResultBits = |
| 6536 | static_cast<llvm::APInt&>(Right) + Left.getMinSignedBits(); |
| 6537 | if (LeftBits.uge(ResultBits)) |
| 6538 | return; |
| 6539 | llvm::APSInt Result = Left.extend(ResultBits.getLimitedValue()); |
| 6540 | Result = Result.shl(Right); |
| 6541 | |
Ted Kremenek | fa82138 | 2011-06-15 00:54:52 +0000 | [diff] [blame] | 6542 | // Print the bit representation of the signed integer as an unsigned |
| 6543 | // hexadecimal number. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 6544 | SmallString<40> HexResult; |
Ted Kremenek | fa82138 | 2011-06-15 00:54:52 +0000 | [diff] [blame] | 6545 | Result.toString(HexResult, 16, /*Signed =*/false, /*Literal =*/true); |
| 6546 | |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6547 | // If we are only missing a sign bit, this is less likely to result in actual |
| 6548 | // bugs -- if the result is cast back to an unsigned type, it will have the |
| 6549 | // expected value. Thus we place this behind a different warning that can be |
| 6550 | // turned off separately if needed. |
| 6551 | if (LeftBits == ResultBits - 1) { |
Ted Kremenek | fa82138 | 2011-06-15 00:54:52 +0000 | [diff] [blame] | 6552 | S.Diag(Loc, diag::warn_shift_result_sets_sign_bit) |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6553 | << HexResult.str() << LHSType |
| 6554 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6555 | return; |
| 6556 | } |
| 6557 | |
| 6558 | S.Diag(Loc, diag::warn_shift_result_gt_typewidth) |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6559 | << HexResult.str() << Result.getMinSignedBits() << LHSType |
| 6560 | << Left.getBitWidth() << LHS.get()->getSourceRange() |
| 6561 | << RHS.get()->getSourceRange(); |
Chandler Carruth | 21206d5 | 2011-02-23 23:34:11 +0000 | [diff] [blame] | 6562 | } |
| 6563 | |
Chris Lattner | eca7be6 | 2008-04-07 05:30:13 +0000 | [diff] [blame] | 6564 | // C99 6.5.7 |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6565 | QualType Sema::CheckShiftOperands(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6566 | SourceLocation Loc, unsigned Opc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6567 | bool IsCompAssign) { |
Richard Trieu | 481037f | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 6568 | checkArithmeticNull(*this, LHS, RHS, Loc, /*isCompare=*/false); |
| 6569 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6570 | // C99 6.5.7p2: Each of the operands shall have integer type. |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6571 | if (!LHS.get()->getType()->hasIntegerRepresentation() || |
| 6572 | !RHS.get()->getType()->hasIntegerRepresentation()) |
| 6573 | return InvalidOperands(Loc, LHS, RHS); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6574 | |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6575 | // C++0x: Don't allow scoped enums. FIXME: Use something better than |
| 6576 | // hasIntegerRepresentation() above instead of this. |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6577 | if (isScopedEnumerationType(LHS.get()->getType()) || |
| 6578 | isScopedEnumerationType(RHS.get()->getType())) { |
| 6579 | return InvalidOperands(Loc, LHS, RHS); |
Douglas Gregor | 1274ccd | 2010-10-08 23:50:27 +0000 | [diff] [blame] | 6580 | } |
| 6581 | |
Nate Begeman | 2207d79 | 2009-10-25 02:26:48 +0000 | [diff] [blame] | 6582 | // Vector shifts promote their scalar inputs to vector type. |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6583 | if (LHS.get()->getType()->isVectorType() || |
| 6584 | RHS.get()->getType()->isVectorType()) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6585 | return CheckVectorOperands(LHS, RHS, Loc, IsCompAssign); |
Nate Begeman | 2207d79 | 2009-10-25 02:26:48 +0000 | [diff] [blame] | 6586 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6587 | // Shifts don't perform usual arithmetic conversions, they just do integer |
| 6588 | // promotions on each operand. C99 6.5.7p3 |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 6589 | |
John McCall | 1bc80af | 2010-12-16 19:28:59 +0000 | [diff] [blame] | 6590 | // For the LHS, do usual unary conversions, but then reset them away |
| 6591 | // if this is a compound assignment. |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6592 | ExprResult OldLHS = LHS; |
| 6593 | LHS = UsualUnaryConversions(LHS.take()); |
| 6594 | if (LHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6595 | return QualType(); |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6596 | QualType LHSType = LHS.get()->getType(); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6597 | if (IsCompAssign) LHS = OldLHS; |
John McCall | 1bc80af | 2010-12-16 19:28:59 +0000 | [diff] [blame] | 6598 | |
| 6599 | // The RHS is simpler. |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6600 | RHS = UsualUnaryConversions(RHS.take()); |
| 6601 | if (RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6602 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6603 | |
Ryan Flynn | d043968 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 6604 | // Sanity-check shift operands |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6605 | DiagnoseBadShiftValues(*this, LHS, RHS, Loc, Opc, LHSType); |
Ryan Flynn | d043968 | 2009-08-07 16:20:20 +0000 | [diff] [blame] | 6606 | |
Chris Lattner | ca5eede | 2007-12-12 05:47:28 +0000 | [diff] [blame] | 6607 | // "The type of the result is that of the promoted left operand." |
Richard Trieu | 1c8cfbf | 2011-09-06 21:21:28 +0000 | [diff] [blame] | 6608 | return LHSType; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 6609 | } |
| 6610 | |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6611 | static bool IsWithinTemplateSpecialization(Decl *D) { |
| 6612 | if (DeclContext *DC = D->getDeclContext()) { |
| 6613 | if (isa<ClassTemplateSpecializationDecl>(DC)) |
| 6614 | return true; |
| 6615 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(DC)) |
| 6616 | return FD->isFunctionTemplateSpecialization(); |
| 6617 | } |
| 6618 | return false; |
| 6619 | } |
| 6620 | |
Richard Trieu | e648ac3 | 2011-09-02 03:48:46 +0000 | [diff] [blame] | 6621 | /// If two different enums are compared, raise a warning. |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6622 | static void checkEnumComparison(Sema &S, SourceLocation Loc, ExprResult &LHS, |
| 6623 | ExprResult &RHS) { |
| 6624 | QualType LHSStrippedType = LHS.get()->IgnoreParenImpCasts()->getType(); |
| 6625 | QualType RHSStrippedType = RHS.get()->IgnoreParenImpCasts()->getType(); |
Richard Trieu | e648ac3 | 2011-09-02 03:48:46 +0000 | [diff] [blame] | 6626 | |
| 6627 | const EnumType *LHSEnumType = LHSStrippedType->getAs<EnumType>(); |
| 6628 | if (!LHSEnumType) |
| 6629 | return; |
| 6630 | const EnumType *RHSEnumType = RHSStrippedType->getAs<EnumType>(); |
| 6631 | if (!RHSEnumType) |
| 6632 | return; |
| 6633 | |
| 6634 | // Ignore anonymous enums. |
| 6635 | if (!LHSEnumType->getDecl()->getIdentifier()) |
| 6636 | return; |
| 6637 | if (!RHSEnumType->getDecl()->getIdentifier()) |
| 6638 | return; |
| 6639 | |
| 6640 | if (S.Context.hasSameUnqualifiedType(LHSStrippedType, RHSStrippedType)) |
| 6641 | return; |
| 6642 | |
| 6643 | S.Diag(Loc, diag::warn_comparison_of_mixed_enum_types) |
| 6644 | << LHSStrippedType << RHSStrippedType |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6645 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Richard Trieu | e648ac3 | 2011-09-02 03:48:46 +0000 | [diff] [blame] | 6646 | } |
| 6647 | |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6648 | /// \brief Diagnose bad pointer comparisons. |
| 6649 | static void diagnoseDistinctPointerComparison(Sema &S, SourceLocation Loc, |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6650 | ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6651 | bool IsError) { |
| 6652 | S.Diag(Loc, IsError ? diag::err_typecheck_comparison_of_distinct_pointers |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6653 | : diag::ext_typecheck_comparison_of_distinct_pointers) |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6654 | << LHS.get()->getType() << RHS.get()->getType() |
| 6655 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6656 | } |
| 6657 | |
| 6658 | /// \brief Returns false if the pointers are converted to a composite type, |
| 6659 | /// true otherwise. |
| 6660 | static bool convertPointersToCompositeType(Sema &S, SourceLocation Loc, |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6661 | ExprResult &LHS, ExprResult &RHS) { |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6662 | // C++ [expr.rel]p2: |
| 6663 | // [...] Pointer conversions (4.10) and qualification |
| 6664 | // conversions (4.4) are performed on pointer operands (or on |
| 6665 | // a pointer operand and a null pointer constant) to bring |
| 6666 | // them to their composite pointer type. [...] |
| 6667 | // |
| 6668 | // C++ [expr.eq]p1 uses the same notion for (in)equality |
| 6669 | // comparisons of pointers. |
| 6670 | |
| 6671 | // C++ [expr.eq]p2: |
| 6672 | // In addition, pointers to members can be compared, or a pointer to |
| 6673 | // member and a null pointer constant. Pointer to member conversions |
| 6674 | // (4.11) and qualification conversions (4.4) are performed to bring |
| 6675 | // them to a common type. If one operand is a null pointer constant, |
| 6676 | // the common type is the type of the other operand. Otherwise, the |
| 6677 | // common type is a pointer to member type similar (4.4) to the type |
| 6678 | // of one of the operands, with a cv-qualification signature (4.4) |
| 6679 | // that is the union of the cv-qualification signatures of the operand |
| 6680 | // types. |
| 6681 | |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6682 | QualType LHSType = LHS.get()->getType(); |
| 6683 | QualType RHSType = RHS.get()->getType(); |
| 6684 | assert((LHSType->isPointerType() && RHSType->isPointerType()) || |
| 6685 | (LHSType->isMemberPointerType() && RHSType->isMemberPointerType())); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6686 | |
| 6687 | bool NonStandardCompositeType = false; |
Richard Trieu | 43dff1b | 2011-09-02 21:44:27 +0000 | [diff] [blame] | 6688 | bool *BoolPtr = S.isSFINAEContext() ? 0 : &NonStandardCompositeType; |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6689 | QualType T = S.FindCompositePointerType(Loc, LHS, RHS, BoolPtr); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6690 | if (T.isNull()) { |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6691 | diagnoseDistinctPointerComparison(S, Loc, LHS, RHS, /*isError*/true); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6692 | return true; |
| 6693 | } |
| 6694 | |
| 6695 | if (NonStandardCompositeType) |
| 6696 | S.Diag(Loc, diag::ext_typecheck_comparison_of_distinct_pointers_nonstandard) |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6697 | << LHSType << RHSType << T << LHS.get()->getSourceRange() |
| 6698 | << RHS.get()->getSourceRange(); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6699 | |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6700 | LHS = S.ImpCastExprToType(LHS.take(), T, CK_BitCast); |
| 6701 | RHS = S.ImpCastExprToType(RHS.take(), T, CK_BitCast); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6702 | return false; |
| 6703 | } |
| 6704 | |
| 6705 | static void diagnoseFunctionPointerToVoidComparison(Sema &S, SourceLocation Loc, |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6706 | ExprResult &LHS, |
| 6707 | ExprResult &RHS, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6708 | bool IsError) { |
| 6709 | S.Diag(Loc, IsError ? diag::err_typecheck_comparison_of_fptr_to_void |
| 6710 | : diag::ext_typecheck_comparison_of_fptr_to_void) |
Richard Trieu | ba26149 | 2011-09-06 21:27:33 +0000 | [diff] [blame] | 6711 | << LHS.get()->getType() << RHS.get()->getType() |
| 6712 | << LHS.get()->getSourceRange() << RHS.get()->getSourceRange(); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 6713 | } |
| 6714 | |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 6715 | static bool isObjCObjectLiteral(ExprResult &E) { |
| 6716 | switch (E.get()->getStmtClass()) { |
| 6717 | case Stmt::ObjCArrayLiteralClass: |
| 6718 | case Stmt::ObjCDictionaryLiteralClass: |
| 6719 | case Stmt::ObjCStringLiteralClass: |
| 6720 | case Stmt::ObjCBoxedExprClass: |
| 6721 | return true; |
| 6722 | default: |
| 6723 | // Note that ObjCBoolLiteral is NOT an object literal! |
| 6724 | return false; |
| 6725 | } |
| 6726 | } |
| 6727 | |
Jordan Rose | 8d872ca | 2012-07-17 17:46:40 +0000 | [diff] [blame] | 6728 | static bool hasIsEqualMethod(Sema &S, const Expr *LHS, const Expr *RHS) { |
| 6729 | // Get the LHS object's interface type. |
| 6730 | QualType Type = LHS->getType(); |
| 6731 | QualType InterfaceType; |
| 6732 | if (const ObjCObjectPointerType *PTy = Type->getAs<ObjCObjectPointerType>()) { |
| 6733 | InterfaceType = PTy->getPointeeType(); |
| 6734 | if (const ObjCObjectType *iQFaceTy = |
| 6735 | InterfaceType->getAsObjCQualifiedInterfaceType()) |
| 6736 | InterfaceType = iQFaceTy->getBaseType(); |
| 6737 | } else { |
| 6738 | // If this is not actually an Objective-C object, bail out. |
| 6739 | return false; |
| 6740 | } |
| 6741 | |
| 6742 | // If the RHS isn't an Objective-C object, bail out. |
| 6743 | if (!RHS->getType()->isObjCObjectPointerType()) |
| 6744 | return false; |
| 6745 | |
| 6746 | // Try to find the -isEqual: method. |
| 6747 | Selector IsEqualSel = S.NSAPIObj->getIsEqualSelector(); |
| 6748 | ObjCMethodDecl *Method = S.LookupMethodInObjectType(IsEqualSel, |
| 6749 | InterfaceType, |
| 6750 | /*instance=*/true); |
| 6751 | if (!Method) { |
| 6752 | if (Type->isObjCIdType()) { |
| 6753 | // For 'id', just check the global pool. |
| 6754 | Method = S.LookupInstanceMethodInGlobalPool(IsEqualSel, SourceRange(), |
| 6755 | /*receiverId=*/true, |
| 6756 | /*warn=*/false); |
| 6757 | } else { |
| 6758 | // Check protocols. |
| 6759 | Method = S.LookupMethodInQualifiedType(IsEqualSel, |
| 6760 | cast<ObjCObjectPointerType>(Type), |
| 6761 | /*instance=*/true); |
| 6762 | } |
| 6763 | } |
| 6764 | |
| 6765 | if (!Method) |
| 6766 | return false; |
| 6767 | |
| 6768 | QualType T = Method->param_begin()[0]->getType(); |
| 6769 | if (!T->isObjCObjectPointerType()) |
| 6770 | return false; |
| 6771 | |
| 6772 | QualType R = Method->getResultType(); |
| 6773 | if (!R->isScalarType()) |
| 6774 | return false; |
| 6775 | |
| 6776 | return true; |
| 6777 | } |
| 6778 | |
| 6779 | static void diagnoseObjCLiteralComparison(Sema &S, SourceLocation Loc, |
| 6780 | ExprResult &LHS, ExprResult &RHS, |
| 6781 | BinaryOperator::Opcode Opc){ |
Jordan Rose | d5209ae | 2012-07-17 17:46:48 +0000 | [diff] [blame] | 6782 | Expr *Literal; |
| 6783 | Expr *Other; |
| 6784 | if (isObjCObjectLiteral(LHS)) { |
| 6785 | Literal = LHS.get(); |
| 6786 | Other = RHS.get(); |
| 6787 | } else { |
| 6788 | Literal = RHS.get(); |
| 6789 | Other = LHS.get(); |
| 6790 | } |
| 6791 | |
| 6792 | // Don't warn on comparisons against nil. |
| 6793 | Other = Other->IgnoreParenCasts(); |
| 6794 | if (Other->isNullPointerConstant(S.getASTContext(), |
| 6795 | Expr::NPC_ValueDependentIsNotNull)) |
| 6796 | return; |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 6797 | |
Jordan Rose | eec207f | 2012-07-17 17:46:44 +0000 | [diff] [blame] | 6798 | // This should be kept in sync with warn_objc_literal_comparison. |
Jordan Rose | d5209ae | 2012-07-17 17:46:48 +0000 | [diff] [blame] | 6799 | // LK_String should always be last, since it has its own warning flag. |
Jordan Rose | eec207f | 2012-07-17 17:46:44 +0000 | [diff] [blame] | 6800 | enum { |
| 6801 | LK_Array, |
| 6802 | LK_Dictionary, |
| 6803 | LK_Numeric, |
| 6804 | LK_Boxed, |
| 6805 | LK_String |
| 6806 | } LiteralKind; |
| 6807 | |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 6808 | switch (Literal->getStmtClass()) { |
| 6809 | case Stmt::ObjCStringLiteralClass: |
| 6810 | // "string literal" |
Jordan Rose | eec207f | 2012-07-17 17:46:44 +0000 | [diff] [blame] | 6811 | LiteralKind = LK_String; |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 6812 | break; |
| 6813 | case Stmt::ObjCArrayLiteralClass: |
| 6814 | // "array literal" |
Jordan Rose | eec207f | 2012-07-17 17:46:44 +0000 | [diff] [blame] | 6815 | LiteralKind = LK_Array; |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 6816 | break; |
| 6817 | case Stmt::ObjCDictionaryLiteralClass: |
| 6818 | // "dictionary literal" |
Jordan Rose | eec207f | 2012-07-17 17:46:44 +0000 | [diff] [blame] | 6819 | LiteralKind = LK_Dictionary; |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 6820 | break; |
| 6821 | case Stmt::ObjCBoxedExprClass: { |
| 6822 | Expr *Inner = cast<ObjCBoxedExpr>(Literal)->getSubExpr(); |
| 6823 | switch (Inner->getStmtClass()) { |
| 6824 | case Stmt::IntegerLiteralClass: |
| 6825 | case Stmt::FloatingLiteralClass: |
| 6826 | case Stmt::CharacterLiteralClass: |
| 6827 | case Stmt::ObjCBoolLiteralExprClass: |
| 6828 | case Stmt::CXXBoolLiteralExprClass: |
| 6829 | // "numeric literal" |
Jordan Rose | eec207f | 2012-07-17 17:46:44 +0000 | [diff] [blame] | 6830 | LiteralKind = LK_Numeric; |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 6831 | break; |
| 6832 | case Stmt::ImplicitCastExprClass: { |
| 6833 | CastKind CK = cast<CastExpr>(Inner)->getCastKind(); |
| 6834 | // Boolean literals can be represented by implicit casts. |
| 6835 | if (CK == CK_IntegralToBoolean || CK == CK_IntegralCast) { |
Jordan Rose | eec207f | 2012-07-17 17:46:44 +0000 | [diff] [blame] | 6836 | LiteralKind = LK_Numeric; |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 6837 | break; |
| 6838 | } |
| 6839 | // FALLTHROUGH |
| 6840 | } |
| 6841 | default: |
| 6842 | // "boxed expression" |
Jordan Rose | eec207f | 2012-07-17 17:46:44 +0000 | [diff] [blame] | 6843 | LiteralKind = LK_Boxed; |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 6844 | break; |
| 6845 | } |
| 6846 | break; |
| 6847 | } |
| 6848 | default: |
| 6849 | llvm_unreachable("Unknown Objective-C object literal kind"); |
| 6850 | } |
| 6851 | |
Jordan Rose | eec207f | 2012-07-17 17:46:44 +0000 | [diff] [blame] | 6852 | if (LiteralKind == LK_String) |
| 6853 | S.Diag(Loc, diag::warn_objc_string_literal_comparison) |
| 6854 | << Literal->getSourceRange(); |
| 6855 | else |
| 6856 | S.Diag(Loc, diag::warn_objc_literal_comparison) |
| 6857 | << LiteralKind << Literal->getSourceRange(); |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 6858 | |
Jordan Rose | 8d872ca | 2012-07-17 17:46:40 +0000 | [diff] [blame] | 6859 | if (BinaryOperator::isEqualityOp(Opc) && |
| 6860 | hasIsEqualMethod(S, LHS.get(), RHS.get())) { |
| 6861 | SourceLocation Start = LHS.get()->getLocStart(); |
| 6862 | SourceLocation End = S.PP.getLocForEndOfToken(RHS.get()->getLocEnd()); |
| 6863 | SourceRange OpRange(Loc, S.PP.getLocForEndOfToken(Loc)); |
Jordan Rose | 6deae7c | 2012-07-09 16:54:44 +0000 | [diff] [blame] | 6864 | |
Jordan Rose | 8d872ca | 2012-07-17 17:46:40 +0000 | [diff] [blame] | 6865 | S.Diag(Loc, diag::note_objc_literal_comparison_isequal) |
| 6866 | << FixItHint::CreateInsertion(Start, Opc == BO_EQ ? "[" : "![") |
| 6867 | << FixItHint::CreateReplacement(OpRange, "isEqual:") |
| 6868 | << FixItHint::CreateInsertion(End, "]"); |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 6869 | } |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 6870 | } |
| 6871 | |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 6872 | // C99 6.5.8, C++ [expr.rel] |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6873 | QualType Sema::CheckCompareOperands(ExprResult &LHS, ExprResult &RHS, |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 6874 | SourceLocation Loc, unsigned OpaqueOpc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6875 | bool IsRelational) { |
Richard Trieu | 481037f | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 6876 | checkArithmeticNull(*this, LHS, RHS, Loc, /*isCompare=*/true); |
| 6877 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6878 | BinaryOperatorKind Opc = (BinaryOperatorKind) OpaqueOpc; |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6879 | |
Chris Lattner | 02dd4b1 | 2009-12-05 05:40:13 +0000 | [diff] [blame] | 6880 | // Handle vector comparisons separately. |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6881 | if (LHS.get()->getType()->isVectorType() || |
| 6882 | RHS.get()->getType()->isVectorType()) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6883 | return CheckVectorCompareOperands(LHS, RHS, Loc, IsRelational); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6884 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6885 | QualType LHSType = LHS.get()->getType(); |
| 6886 | QualType RHSType = RHS.get()->getType(); |
Benjamin Kramer | fec0959 | 2011-09-03 08:46:20 +0000 | [diff] [blame] | 6887 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6888 | Expr *LHSStripped = LHS.get()->IgnoreParenImpCasts(); |
| 6889 | Expr *RHSStripped = RHS.get()->IgnoreParenImpCasts(); |
Chandler Carruth | 543cb65 | 2011-02-17 08:37:06 +0000 | [diff] [blame] | 6890 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6891 | checkEnumComparison(*this, Loc, LHS, RHS); |
Chandler Carruth | 543cb65 | 2011-02-17 08:37:06 +0000 | [diff] [blame] | 6892 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6893 | if (!LHSType->hasFloatingRepresentation() && |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 6894 | !(LHSType->isBlockPointerType() && IsRelational) && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6895 | !LHS.get()->getLocStart().isMacroID() && |
| 6896 | !RHS.get()->getLocStart().isMacroID()) { |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6897 | // For non-floating point types, check for self-comparisons of the form |
| 6898 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 6899 | // often indicate logic errors in the program. |
Chandler Carruth | 64d092c | 2010-07-12 06:23:38 +0000 | [diff] [blame] | 6900 | // |
| 6901 | // NOTE: Don't warn about comparison expressions resulting from macro |
| 6902 | // expansion. Also don't warn about comparisons which are only self |
| 6903 | // comparisons within a template specialization. The warnings should catch |
| 6904 | // obvious cases in the definition of the template anyways. The idea is to |
| 6905 | // warn when the typed comparison operator will always evaluate to the same |
| 6906 | // result. |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6907 | if (DeclRefExpr* DRL = dyn_cast<DeclRefExpr>(LHSStripped)) { |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6908 | if (DeclRefExpr* DRR = dyn_cast<DeclRefExpr>(RHSStripped)) { |
Ted Kremenek | fbcb0eb | 2010-09-16 00:03:01 +0000 | [diff] [blame] | 6909 | if (DRL->getDecl() == DRR->getDecl() && |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6910 | !IsWithinTemplateSpecialization(DRL->getDecl())) { |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 6911 | DiagRuntimeBehavior(Loc, 0, PDiag(diag::warn_comparison_always) |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6912 | << 0 // self- |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6913 | << (Opc == BO_EQ |
| 6914 | || Opc == BO_LE |
| 6915 | || Opc == BO_GE)); |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6916 | } else if (LHSType->isArrayType() && RHSType->isArrayType() && |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6917 | !DRL->getDecl()->getType()->isReferenceType() && |
| 6918 | !DRR->getDecl()->getType()->isReferenceType()) { |
| 6919 | // what is it always going to eval to? |
| 6920 | char always_evals_to; |
| 6921 | switch(Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6922 | case BO_EQ: // e.g. array1 == array2 |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6923 | always_evals_to = 0; // false |
| 6924 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6925 | case BO_NE: // e.g. array1 != array2 |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6926 | always_evals_to = 1; // true |
| 6927 | break; |
| 6928 | default: |
| 6929 | // best we can say is 'a constant' |
| 6930 | always_evals_to = 2; // e.g. array1 <= array2 |
| 6931 | break; |
| 6932 | } |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 6933 | DiagRuntimeBehavior(Loc, 0, PDiag(diag::warn_comparison_always) |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6934 | << 1 // array |
| 6935 | << always_evals_to); |
| 6936 | } |
| 6937 | } |
Chandler Carruth | 9991947 | 2010-07-10 12:30:03 +0000 | [diff] [blame] | 6938 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6939 | |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6940 | if (isa<CastExpr>(LHSStripped)) |
| 6941 | LHSStripped = LHSStripped->IgnoreParenCasts(); |
| 6942 | if (isa<CastExpr>(RHSStripped)) |
| 6943 | RHSStripped = RHSStripped->IgnoreParenCasts(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 6944 | |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6945 | // Warn about comparisons against a string constant (unless the other |
| 6946 | // operand is null), the user probably wants strcmp. |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6947 | Expr *literalString = 0; |
| 6948 | Expr *literalStringStripped = 0; |
Chris Lattner | 55660a7 | 2009-03-08 19:39:53 +0000 | [diff] [blame] | 6949 | if ((isa<StringLiteral>(LHSStripped) || isa<ObjCEncodeExpr>(LHSStripped)) && |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6950 | !RHSStripped->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6951 | Expr::NPC_ValueDependentIsNull)) { |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6952 | literalString = LHS.get(); |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6953 | literalStringStripped = LHSStripped; |
Mike Stump | ac5fc7c | 2009-08-04 21:02:39 +0000 | [diff] [blame] | 6954 | } else if ((isa<StringLiteral>(RHSStripped) || |
| 6955 | isa<ObjCEncodeExpr>(RHSStripped)) && |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6956 | !LHSStripped->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 6957 | Expr::NPC_ValueDependentIsNull)) { |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6958 | literalString = RHS.get(); |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6959 | literalStringStripped = RHSStripped; |
| 6960 | } |
| 6961 | |
| 6962 | if (literalString) { |
| 6963 | std::string resultComparison; |
| 6964 | switch (Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 6965 | case BO_LT: resultComparison = ") < 0"; break; |
| 6966 | case BO_GT: resultComparison = ") > 0"; break; |
| 6967 | case BO_LE: resultComparison = ") <= 0"; break; |
| 6968 | case BO_GE: resultComparison = ") >= 0"; break; |
| 6969 | case BO_EQ: resultComparison = ") == 0"; break; |
| 6970 | case BO_NE: resultComparison = ") != 0"; break; |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 6971 | default: llvm_unreachable("Invalid comparison operator"); |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6972 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 6973 | |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 6974 | DiagRuntimeBehavior(Loc, 0, |
Douglas Gregor | d1e4d9b | 2010-01-12 23:18:54 +0000 | [diff] [blame] | 6975 | PDiag(diag::warn_stringcompare) |
| 6976 | << isa<ObjCEncodeExpr>(literalStringStripped) |
Ted Kremenek | 03a4bee | 2010-04-09 20:26:53 +0000 | [diff] [blame] | 6977 | << literalString->getSourceRange()); |
Douglas Gregor | a86b832 | 2009-04-06 18:45:53 +0000 | [diff] [blame] | 6978 | } |
Ted Kremenek | 3ca0bf2 | 2007-10-29 16:58:49 +0000 | [diff] [blame] | 6979 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 6980 | |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6981 | // C99 6.5.8p3 / C99 6.5.9p4 |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6982 | if (LHS.get()->getType()->isArithmeticType() && |
| 6983 | RHS.get()->getType()->isArithmeticType()) { |
| 6984 | UsualArithmeticConversions(LHS, RHS); |
| 6985 | if (LHS.isInvalid() || RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6986 | return QualType(); |
| 6987 | } |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6988 | else { |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6989 | LHS = UsualUnaryConversions(LHS.take()); |
| 6990 | if (LHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6991 | return QualType(); |
| 6992 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6993 | RHS = UsualUnaryConversions(RHS.take()); |
| 6994 | if (RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 6995 | return QualType(); |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 6996 | } |
| 6997 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 6998 | LHSType = LHS.get()->getType(); |
| 6999 | RHSType = RHS.get()->getType(); |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7000 | |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7001 | // The result of comparisons is 'bool' in C++, 'int' in C. |
Argyrios Kyrtzidis | 16f744b | 2011-02-18 20:55:15 +0000 | [diff] [blame] | 7002 | QualType ResultTy = Context.getLogicalOperationType(); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7003 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7004 | if (IsRelational) { |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7005 | if (LHSType->isRealType() && RHSType->isRealType()) |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7006 | return ResultTy; |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 7007 | } else { |
Ted Kremenek | 72cb1ae | 2007-10-29 17:13:39 +0000 | [diff] [blame] | 7008 | // Check for comparisons of floating point operands using != and ==. |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7009 | if (LHSType->hasFloatingRepresentation()) |
| 7010 | CheckFloatComparison(Loc, LHS.get(), RHS.get()); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7011 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7012 | if (LHSType->isArithmeticType() && RHSType->isArithmeticType()) |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7013 | return ResultTy; |
Chris Lattner | a5937dd | 2007-08-26 01:18:55 +0000 | [diff] [blame] | 7014 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7015 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7016 | bool LHSIsNull = LHS.get()->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 7017 | Expr::NPC_ValueDependentIsNull); |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7018 | bool RHSIsNull = RHS.get()->isNullPointerConstant(Context, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 7019 | Expr::NPC_ValueDependentIsNull); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7020 | |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7021 | // All of the following pointer-related warnings are GCC extensions, except |
| 7022 | // when handling null pointer constants. |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7023 | if (LHSType->isPointerType() && RHSType->isPointerType()) { // C99 6.5.8p2 |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 7024 | QualType LCanPointeeTy = |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 7025 | LHSType->castAs<PointerType>()->getPointeeType().getCanonicalType(); |
Chris Lattner | bc896f5 | 2008-04-03 05:07:25 +0000 | [diff] [blame] | 7026 | QualType RCanPointeeTy = |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 7027 | RHSType->castAs<PointerType>()->getPointeeType().getCanonicalType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7028 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7029 | if (getLangOpts().CPlusPlus) { |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 7030 | if (LCanPointeeTy == RCanPointeeTy) |
| 7031 | return ResultTy; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7032 | if (!IsRelational && |
Fariborz Jahanian | 51874dd | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 7033 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 7034 | // Valid unless comparison between non-null pointer and function pointer |
| 7035 | // This is a gcc extension compatibility comparison. |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7036 | // In a SFINAE context, we treat this as a hard error to maintain |
| 7037 | // conformance with the C++ standard. |
Fariborz Jahanian | 51874dd | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 7038 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
| 7039 | && !LHSIsNull && !RHSIsNull) { |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 7040 | diagnoseFunctionPointerToVoidComparison( |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7041 | *this, Loc, LHS, RHS, /*isError*/ isSFINAEContext()); |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7042 | |
| 7043 | if (isSFINAEContext()) |
| 7044 | return QualType(); |
| 7045 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7046 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); |
Fariborz Jahanian | 51874dd | 2009-12-21 18:19:17 +0000 | [diff] [blame] | 7047 | return ResultTy; |
| 7048 | } |
| 7049 | } |
Anders Carlsson | 0c8209e | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 7050 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7051 | if (convertPointersToCompositeType(*this, Loc, LHS, RHS)) |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7052 | return QualType(); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 7053 | else |
| 7054 | return ResultTy; |
Douglas Gregor | 0c6db94 | 2009-05-04 06:07:12 +0000 | [diff] [blame] | 7055 | } |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 7056 | // C99 6.5.9p2 and C99 6.5.8p2 |
| 7057 | if (Context.typesAreCompatible(LCanPointeeTy.getUnqualifiedType(), |
| 7058 | RCanPointeeTy.getUnqualifiedType())) { |
| 7059 | // Valid unless a relational comparison of function pointers |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7060 | if (IsRelational && LCanPointeeTy->isFunctionType()) { |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 7061 | Diag(Loc, diag::ext_typecheck_ordered_comparison_of_function_pointers) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7062 | << LHSType << RHSType << LHS.get()->getSourceRange() |
| 7063 | << RHS.get()->getSourceRange(); |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 7064 | } |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7065 | } else if (!IsRelational && |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 7066 | (LCanPointeeTy->isVoidType() || RCanPointeeTy->isVoidType())) { |
| 7067 | // Valid unless comparison between non-null pointer and function pointer |
| 7068 | if ((LCanPointeeTy->isFunctionType() || RCanPointeeTy->isFunctionType()) |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 7069 | && !LHSIsNull && !RHSIsNull) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7070 | diagnoseFunctionPointerToVoidComparison(*this, Loc, LHS, RHS, |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 7071 | /*isError*/false); |
Eli Friedman | 3075e76 | 2009-08-23 00:27:47 +0000 | [diff] [blame] | 7072 | } else { |
| 7073 | // Invalid |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7074 | diagnoseDistinctPointerComparison(*this, Loc, LHS, RHS, /*isError*/false); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7075 | } |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7076 | if (LCanPointeeTy != RCanPointeeTy) { |
| 7077 | if (LHSIsNull && !RHSIsNull) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7078 | LHS = ImpCastExprToType(LHS.take(), RHSType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7079 | else |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7080 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7081 | } |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7082 | return ResultTy; |
Steve Naroff | e77fd3c | 2007-08-16 21:48:38 +0000 | [diff] [blame] | 7083 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7084 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7085 | if (getLangOpts().CPlusPlus) { |
Anders Carlsson | 0c8209e | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 7086 | // Comparison of nullptr_t with itself. |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7087 | if (LHSType->isNullPtrType() && RHSType->isNullPtrType()) |
Anders Carlsson | 0c8209e | 2010-11-04 03:17:43 +0000 | [diff] [blame] | 7088 | return ResultTy; |
| 7089 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7090 | // Comparison of pointers with null pointer constants and equality |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7091 | // comparisons of member pointers to null pointer constants. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7092 | if (RHSIsNull && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7093 | ((LHSType->isAnyPointerType() || LHSType->isNullPtrType()) || |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7094 | (!IsRelational && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7095 | (LHSType->isMemberPointerType() || LHSType->isBlockPointerType())))) { |
| 7096 | RHS = ImpCastExprToType(RHS.take(), LHSType, |
| 7097 | LHSType->isMemberPointerType() |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7098 | ? CK_NullToMemberPointer |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7099 | : CK_NullToPointer); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 7100 | return ResultTy; |
| 7101 | } |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7102 | if (LHSIsNull && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7103 | ((RHSType->isAnyPointerType() || RHSType->isNullPtrType()) || |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7104 | (!IsRelational && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7105 | (RHSType->isMemberPointerType() || RHSType->isBlockPointerType())))) { |
| 7106 | LHS = ImpCastExprToType(LHS.take(), RHSType, |
| 7107 | RHSType->isMemberPointerType() |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7108 | ? CK_NullToMemberPointer |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7109 | : CK_NullToPointer); |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 7110 | return ResultTy; |
| 7111 | } |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7112 | |
| 7113 | // Comparison of member pointers. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7114 | if (!IsRelational && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7115 | LHSType->isMemberPointerType() && RHSType->isMemberPointerType()) { |
| 7116 | if (convertPointersToCompositeType(*this, Loc, LHS, RHS)) |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7117 | return QualType(); |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 7118 | else |
| 7119 | return ResultTy; |
Douglas Gregor | 20b3e99 | 2009-08-24 17:42:35 +0000 | [diff] [blame] | 7120 | } |
Douglas Gregor | 90566c0 | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 7121 | |
| 7122 | // Handle scoped enumeration types specifically, since they don't promote |
| 7123 | // to integers. |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7124 | if (LHS.get()->getType()->isEnumeralType() && |
| 7125 | Context.hasSameUnqualifiedType(LHS.get()->getType(), |
| 7126 | RHS.get()->getType())) |
Douglas Gregor | 90566c0 | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 7127 | return ResultTy; |
Sebastian Redl | 6e8ed16 | 2009-05-10 18:38:11 +0000 | [diff] [blame] | 7128 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7129 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7130 | // Handle block pointer types. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7131 | if (!IsRelational && LHSType->isBlockPointerType() && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7132 | RHSType->isBlockPointerType()) { |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 7133 | QualType lpointee = LHSType->castAs<BlockPointerType>()->getPointeeType(); |
| 7134 | QualType rpointee = RHSType->castAs<BlockPointerType>()->getPointeeType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7135 | |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7136 | if (!LHSIsNull && !RHSIsNull && |
Eli Friedman | 26784c1 | 2009-06-08 05:08:54 +0000 | [diff] [blame] | 7137 | !Context.typesAreCompatible(lpointee, rpointee)) { |
Chris Lattner | c9c7c4e | 2008-11-18 22:52:51 +0000 | [diff] [blame] | 7138 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7139 | << LHSType << RHSType << LHS.get()->getSourceRange() |
| 7140 | << RHS.get()->getSourceRange(); |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7141 | } |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7142 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7143 | return ResultTy; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7144 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7145 | |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 7146 | // Allow block pointers to be compared with null pointer constants. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7147 | if (!IsRelational |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7148 | && ((LHSType->isBlockPointerType() && RHSType->isPointerType()) |
| 7149 | || (LHSType->isPointerType() && RHSType->isBlockPointerType()))) { |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 7150 | if (!LHSIsNull && !RHSIsNull) { |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7151 | if (!((RHSType->isPointerType() && RHSType->castAs<PointerType>() |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 7152 | ->getPointeeType()->isVoidType()) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7153 | || (LHSType->isPointerType() && LHSType->castAs<PointerType>() |
Mike Stump | dd3e166 | 2009-05-07 03:14:14 +0000 | [diff] [blame] | 7154 | ->getPointeeType()->isVoidType()))) |
| 7155 | Diag(Loc, diag::err_typecheck_comparison_of_distinct_blocks) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7156 | << LHSType << RHSType << LHS.get()->getSourceRange() |
| 7157 | << RHS.get()->getSourceRange(); |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 7158 | } |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7159 | if (LHSIsNull && !RHSIsNull) |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 7160 | LHS = ImpCastExprToType(LHS.take(), RHSType, |
| 7161 | RHSType->isPointerType() ? CK_BitCast |
| 7162 | : CK_AnyPointerToBlockPointerCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7163 | else |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 7164 | RHS = ImpCastExprToType(RHS.take(), LHSType, |
| 7165 | LHSType->isPointerType() ? CK_BitCast |
| 7166 | : CK_AnyPointerToBlockPointerCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7167 | return ResultTy; |
Steve Naroff | 59f5394 | 2008-09-28 01:11:11 +0000 | [diff] [blame] | 7168 | } |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 7169 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7170 | if (LHSType->isObjCObjectPointerType() || |
| 7171 | RHSType->isObjCObjectPointerType()) { |
| 7172 | const PointerType *LPT = LHSType->getAs<PointerType>(); |
| 7173 | const PointerType *RPT = RHSType->getAs<PointerType>(); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7174 | if (LPT || RPT) { |
| 7175 | bool LPtrToVoid = LPT ? LPT->getPointeeType()->isVoidType() : false; |
| 7176 | bool RPtrToVoid = RPT ? RPT->getPointeeType()->isVoidType() : false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7177 | |
Steve Naroff | a8069f1 | 2008-11-17 19:49:16 +0000 | [diff] [blame] | 7178 | if (!LPtrToVoid && !RPtrToVoid && |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7179 | !Context.typesAreCompatible(LHSType, RHSType)) { |
| 7180 | diagnoseDistinctPointerComparison(*this, Loc, LHS, RHS, |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 7181 | /*isError*/false); |
Steve Naroff | a5ad863 | 2008-10-27 10:33:19 +0000 | [diff] [blame] | 7182 | } |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7183 | if (LHSIsNull && !RHSIsNull) |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 7184 | LHS = ImpCastExprToType(LHS.take(), RHSType, |
| 7185 | RPT ? CK_BitCast :CK_CPointerToObjCPointerCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7186 | else |
John McCall | 1d9b3b2 | 2011-09-09 05:25:32 +0000 | [diff] [blame] | 7187 | RHS = ImpCastExprToType(RHS.take(), LHSType, |
| 7188 | LPT ? CK_BitCast :CK_CPointerToObjCPointerCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7189 | return ResultTy; |
Steve Naroff | 87f3b93 | 2008-10-20 18:19:10 +0000 | [diff] [blame] | 7190 | } |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7191 | if (LHSType->isObjCObjectPointerType() && |
| 7192 | RHSType->isObjCObjectPointerType()) { |
| 7193 | if (!Context.areComparableObjCPointerTypes(LHSType, RHSType)) |
| 7194 | diagnoseDistinctPointerComparison(*this, Loc, LHS, RHS, |
Richard Trieu | 7be1be0 | 2011-09-02 02:55:45 +0000 | [diff] [blame] | 7195 | /*isError*/false); |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 7196 | if (isObjCObjectLiteral(LHS) || isObjCObjectLiteral(RHS)) |
Jordan Rose | 8d872ca | 2012-07-17 17:46:40 +0000 | [diff] [blame] | 7197 | diagnoseObjCLiteralComparison(*this, Loc, LHS, RHS, Opc); |
Jordan Rose | 9f63a45 | 2012-06-08 21:14:25 +0000 | [diff] [blame] | 7198 | |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7199 | if (LHSIsNull && !RHSIsNull) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7200 | LHS = ImpCastExprToType(LHS.take(), RHSType, CK_BitCast); |
John McCall | 34d6f93 | 2011-03-11 04:25:25 +0000 | [diff] [blame] | 7201 | else |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7202 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_BitCast); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7203 | return ResultTy; |
Steve Naroff | 2037322 | 2008-06-03 14:04:54 +0000 | [diff] [blame] | 7204 | } |
Fariborz Jahanian | 7359f04 | 2007-12-20 01:06:58 +0000 | [diff] [blame] | 7205 | } |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7206 | if ((LHSType->isAnyPointerType() && RHSType->isIntegerType()) || |
| 7207 | (LHSType->isIntegerType() && RHSType->isAnyPointerType())) { |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7208 | unsigned DiagID = 0; |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7209 | bool isError = false; |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7210 | if ((LHSIsNull && LHSType->isIntegerType()) || |
| 7211 | (RHSIsNull && RHSType->isIntegerType())) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7212 | if (IsRelational && !getLangOpts().CPlusPlus) |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7213 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_and_zero; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7214 | } else if (IsRelational && !getLangOpts().CPlusPlus) |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7215 | DiagID = diag::ext_typecheck_ordered_comparison_of_pointer_integer; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7216 | else if (getLangOpts().CPlusPlus) { |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7217 | DiagID = diag::err_typecheck_comparison_of_pointer_integer; |
| 7218 | isError = true; |
| 7219 | } else |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7220 | DiagID = diag::ext_typecheck_comparison_of_pointer_integer; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7221 | |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7222 | if (DiagID) { |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 7223 | Diag(Loc, DiagID) |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7224 | << LHSType << RHSType << LHS.get()->getSourceRange() |
| 7225 | << RHS.get()->getSourceRange(); |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7226 | if (isError) |
| 7227 | return QualType(); |
Chris Lattner | 6365e3e | 2009-08-22 18:58:31 +0000 | [diff] [blame] | 7228 | } |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7229 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7230 | if (LHSType->isIntegerType()) |
| 7231 | LHS = ImpCastExprToType(LHS.take(), RHSType, |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7232 | LHSIsNull ? CK_NullToPointer : CK_IntegralToPointer); |
Chris Lattner | 06c0f5b | 2009-08-23 00:03:44 +0000 | [diff] [blame] | 7233 | else |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7234 | RHS = ImpCastExprToType(RHS.take(), LHSType, |
John McCall | 404cd16 | 2010-11-13 01:35:44 +0000 | [diff] [blame] | 7235 | RHSIsNull ? CK_NullToPointer : CK_IntegralToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7236 | return ResultTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7237 | } |
Douglas Gregor | 6e5122c | 2010-06-15 21:38:40 +0000 | [diff] [blame] | 7238 | |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 7239 | // Handle block pointers. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7240 | if (!IsRelational && RHSIsNull |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7241 | && LHSType->isBlockPointerType() && RHSType->isIntegerType()) { |
| 7242 | RHS = ImpCastExprToType(RHS.take(), LHSType, CK_NullToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7243 | return ResultTy; |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 7244 | } |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7245 | if (!IsRelational && LHSIsNull |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7246 | && LHSType->isIntegerType() && RHSType->isBlockPointerType()) { |
| 7247 | LHS = ImpCastExprToType(LHS.take(), RHSType, CK_NullToPointer); |
Douglas Gregor | 447b69e | 2008-11-19 03:25:36 +0000 | [diff] [blame] | 7248 | return ResultTy; |
Steve Naroff | 39218df | 2008-09-04 16:56:14 +0000 | [diff] [blame] | 7249 | } |
Douglas Gregor | 90566c0 | 2011-03-01 17:16:20 +0000 | [diff] [blame] | 7250 | |
Richard Trieu | f1775fb | 2011-09-06 21:43:51 +0000 | [diff] [blame] | 7251 | return InvalidOperands(Loc, LHS, RHS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7252 | } |
| 7253 | |
Tanya Lattner | 4f692c2 | 2012-01-16 21:02:28 +0000 | [diff] [blame] | 7254 | |
| 7255 | // Return a signed type that is of identical size and number of elements. |
| 7256 | // For floating point vectors, return an integer type of identical size |
| 7257 | // and number of elements. |
| 7258 | QualType Sema::GetSignedVectorType(QualType V) { |
| 7259 | const VectorType *VTy = V->getAs<VectorType>(); |
| 7260 | unsigned TypeSize = Context.getTypeSize(VTy->getElementType()); |
| 7261 | if (TypeSize == Context.getTypeSize(Context.CharTy)) |
| 7262 | return Context.getExtVectorType(Context.CharTy, VTy->getNumElements()); |
| 7263 | else if (TypeSize == Context.getTypeSize(Context.ShortTy)) |
| 7264 | return Context.getExtVectorType(Context.ShortTy, VTy->getNumElements()); |
| 7265 | else if (TypeSize == Context.getTypeSize(Context.IntTy)) |
| 7266 | return Context.getExtVectorType(Context.IntTy, VTy->getNumElements()); |
| 7267 | else if (TypeSize == Context.getTypeSize(Context.LongTy)) |
| 7268 | return Context.getExtVectorType(Context.LongTy, VTy->getNumElements()); |
| 7269 | assert(TypeSize == Context.getTypeSize(Context.LongLongTy) && |
| 7270 | "Unhandled vector element size in vector compare"); |
| 7271 | return Context.getExtVectorType(Context.LongLongTy, VTy->getNumElements()); |
| 7272 | } |
| 7273 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7274 | /// CheckVectorCompareOperands - vector comparisons are a clang extension that |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7275 | /// operates on extended vector types. Instead of producing an IntTy result, |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7276 | /// like a scalar comparison, a vector comparison produces a vector of integer |
| 7277 | /// types. |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7278 | QualType Sema::CheckVectorCompareOperands(ExprResult &LHS, ExprResult &RHS, |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7279 | SourceLocation Loc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7280 | bool IsRelational) { |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7281 | // Check to make sure we're operating on vectors of the same type and width, |
| 7282 | // Allowing one side to be a scalar of element type. |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7283 | QualType vType = CheckVectorOperands(LHS, RHS, Loc, /*isCompAssign*/false); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7284 | if (vType.isNull()) |
| 7285 | return vType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7286 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7287 | QualType LHSType = LHS.get()->getType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7288 | |
Anton Yartsev | 7870b13 | 2011-03-27 15:36:07 +0000 | [diff] [blame] | 7289 | // If AltiVec, the comparison results in a numeric type, i.e. |
| 7290 | // bool for C++, int for C |
Anton Yartsev | 6305f72 | 2011-03-28 21:00:05 +0000 | [diff] [blame] | 7291 | if (vType->getAs<VectorType>()->getVectorKind() == VectorType::AltiVecVector) |
Anton Yartsev | 7870b13 | 2011-03-27 15:36:07 +0000 | [diff] [blame] | 7292 | return Context.getLogicalOperationType(); |
| 7293 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7294 | // For non-floating point types, check for self-comparisons of the form |
| 7295 | // x == x, x != x, x < x, etc. These always evaluate to a constant, and |
| 7296 | // often indicate logic errors in the program. |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7297 | if (!LHSType->hasFloatingRepresentation()) { |
Richard Smith | 9c129f8 | 2011-10-28 03:31:48 +0000 | [diff] [blame] | 7298 | if (DeclRefExpr* DRL |
| 7299 | = dyn_cast<DeclRefExpr>(LHS.get()->IgnoreParenImpCasts())) |
| 7300 | if (DeclRefExpr* DRR |
| 7301 | = dyn_cast<DeclRefExpr>(RHS.get()->IgnoreParenImpCasts())) |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7302 | if (DRL->getDecl() == DRR->getDecl()) |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 7303 | DiagRuntimeBehavior(Loc, 0, |
Douglas Gregor | d64fdd0 | 2010-06-08 19:50:34 +0000 | [diff] [blame] | 7304 | PDiag(diag::warn_comparison_always) |
| 7305 | << 0 // self- |
| 7306 | << 2 // "a constant" |
| 7307 | ); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7308 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7309 | |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7310 | // Check for comparisons of floating point operands using != and ==. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7311 | if (!IsRelational && LHSType->hasFloatingRepresentation()) { |
David Blaikie | 52e4c60 | 2012-01-16 05:16:03 +0000 | [diff] [blame] | 7312 | assert (RHS.get()->getType()->hasFloatingRepresentation()); |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7313 | CheckFloatComparison(Loc, LHS.get(), RHS.get()); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7314 | } |
Tanya Lattner | 4f692c2 | 2012-01-16 21:02:28 +0000 | [diff] [blame] | 7315 | |
| 7316 | // Return a signed type for the vector. |
| 7317 | return GetSignedVectorType(LHSType); |
| 7318 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7319 | |
Tanya Lattner | b0f9dd2 | 2012-01-19 01:16:16 +0000 | [diff] [blame] | 7320 | QualType Sema::CheckVectorLogicalOperands(ExprResult &LHS, ExprResult &RHS, |
| 7321 | SourceLocation Loc) { |
Tanya Lattner | 4f692c2 | 2012-01-16 21:02:28 +0000 | [diff] [blame] | 7322 | // Ensure that either both operands are of the same vector type, or |
| 7323 | // one operand is of a vector type and the other is of its element type. |
| 7324 | QualType vType = CheckVectorOperands(LHS, RHS, Loc, false); |
| 7325 | if (vType.isNull() || vType->isFloatingType()) |
| 7326 | return InvalidOperands(Loc, LHS, RHS); |
| 7327 | |
| 7328 | return GetSignedVectorType(LHS.get()->getType()); |
Nate Begeman | be2341d | 2008-07-14 18:02:46 +0000 | [diff] [blame] | 7329 | } |
| 7330 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7331 | inline QualType Sema::CheckBitwiseOperands( |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7332 | ExprResult &LHS, ExprResult &RHS, SourceLocation Loc, bool IsCompAssign) { |
Richard Trieu | 481037f | 2011-09-16 00:53:10 +0000 | [diff] [blame] | 7333 | checkArithmeticNull(*this, LHS, RHS, Loc, /*isCompare=*/false); |
| 7334 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7335 | if (LHS.get()->getType()->isVectorType() || |
| 7336 | RHS.get()->getType()->isVectorType()) { |
| 7337 | if (LHS.get()->getType()->hasIntegerRepresentation() && |
| 7338 | RHS.get()->getType()->hasIntegerRepresentation()) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7339 | return CheckVectorOperands(LHS, RHS, Loc, IsCompAssign); |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 7340 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7341 | return InvalidOperands(Loc, LHS, RHS); |
Douglas Gregor | f609462 | 2010-07-23 15:58:24 +0000 | [diff] [blame] | 7342 | } |
Steve Naroff | 90045e8 | 2007-07-13 23:32:42 +0000 | [diff] [blame] | 7343 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7344 | ExprResult LHSResult = Owned(LHS), RHSResult = Owned(RHS); |
| 7345 | QualType compType = UsualArithmeticConversions(LHSResult, RHSResult, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7346 | IsCompAssign); |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7347 | if (LHSResult.isInvalid() || RHSResult.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7348 | return QualType(); |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7349 | LHS = LHSResult.take(); |
| 7350 | RHS = RHSResult.take(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7351 | |
Eli Friedman | 860a319 | 2012-06-16 02:19:17 +0000 | [diff] [blame] | 7352 | if (!compType.isNull() && compType->isIntegralOrUnscopedEnumerationType()) |
Steve Naroff | 9f5fa9b | 2007-08-24 19:07:16 +0000 | [diff] [blame] | 7353 | return compType; |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7354 | return InvalidOperands(Loc, LHS, RHS); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7355 | } |
| 7356 | |
| 7357 | inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14] |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7358 | ExprResult &LHS, ExprResult &RHS, SourceLocation Loc, unsigned Opc) { |
Chris Lattner | 90a8f27 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 7359 | |
Tanya Lattner | 4f692c2 | 2012-01-16 21:02:28 +0000 | [diff] [blame] | 7360 | // Check vector operands differently. |
| 7361 | if (LHS.get()->getType()->isVectorType() || RHS.get()->getType()->isVectorType()) |
| 7362 | return CheckVectorLogicalOperands(LHS, RHS, Loc); |
| 7363 | |
Chris Lattner | 90a8f27 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 7364 | // Diagnose cases where the user write a logical and/or but probably meant a |
| 7365 | // bitwise one. We do this when the LHS is a non-bool integer and the RHS |
| 7366 | // is a constant. |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7367 | if (LHS.get()->getType()->isIntegerType() && |
| 7368 | !LHS.get()->getType()->isBooleanType() && |
| 7369 | RHS.get()->getType()->isIntegerType() && !RHS.get()->isValueDependent() && |
Richard Trieu | e5adf59 | 2011-07-15 00:00:51 +0000 | [diff] [blame] | 7370 | // Don't warn in macros or template instantiations. |
| 7371 | !Loc.isMacroID() && ActiveTemplateInstantiations.empty()) { |
Chris Lattner | b7690b4 | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 7372 | // If the RHS can be constant folded, and if it constant folds to something |
| 7373 | // that isn't 0 or 1 (which indicate a potential logical operation that |
| 7374 | // happened to fold to true/false) then warn. |
Chandler Carruth | 0683a14 | 2011-05-31 05:41:42 +0000 | [diff] [blame] | 7375 | // Parens on the RHS are ignored. |
Richard Smith | 909c555 | 2011-10-16 23:01:09 +0000 | [diff] [blame] | 7376 | llvm::APSInt Result; |
| 7377 | if (RHS.get()->EvaluateAsInt(Result, Context)) |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7378 | if ((getLangOpts().Bool && !RHS.get()->getType()->isBooleanType()) || |
Richard Smith | 909c555 | 2011-10-16 23:01:09 +0000 | [diff] [blame] | 7379 | (Result != 0 && Result != 1)) { |
Chandler Carruth | 0683a14 | 2011-05-31 05:41:42 +0000 | [diff] [blame] | 7380 | Diag(Loc, diag::warn_logical_instead_of_bitwise) |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7381 | << RHS.get()->getSourceRange() |
Matt Beaumont-Gay | 9b127f3 | 2011-08-15 17:50:06 +0000 | [diff] [blame] | 7382 | << (Opc == BO_LAnd ? "&&" : "||"); |
| 7383 | // Suggest replacing the logical operator with the bitwise version |
| 7384 | Diag(Loc, diag::note_logical_instead_of_bitwise_change_operator) |
| 7385 | << (Opc == BO_LAnd ? "&" : "|") |
| 7386 | << FixItHint::CreateReplacement(SourceRange( |
| 7387 | Loc, Lexer::getLocForEndOfToken(Loc, 0, getSourceManager(), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7388 | getLangOpts())), |
Matt Beaumont-Gay | 9b127f3 | 2011-08-15 17:50:06 +0000 | [diff] [blame] | 7389 | Opc == BO_LAnd ? "&" : "|"); |
| 7390 | if (Opc == BO_LAnd) |
| 7391 | // Suggest replacing "Foo() && kNonZero" with "Foo()" |
| 7392 | Diag(Loc, diag::note_logical_instead_of_bitwise_remove_constant) |
| 7393 | << FixItHint::CreateRemoval( |
| 7394 | SourceRange( |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7395 | Lexer::getLocForEndOfToken(LHS.get()->getLocEnd(), |
Matt Beaumont-Gay | 9b127f3 | 2011-08-15 17:50:06 +0000 | [diff] [blame] | 7396 | 0, getSourceManager(), |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7397 | getLangOpts()), |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7398 | RHS.get()->getLocEnd())); |
Matt Beaumont-Gay | 9b127f3 | 2011-08-15 17:50:06 +0000 | [diff] [blame] | 7399 | } |
Chris Lattner | b7690b4 | 2010-07-24 01:10:11 +0000 | [diff] [blame] | 7400 | } |
Chris Lattner | 90a8f27 | 2010-07-13 19:41:32 +0000 | [diff] [blame] | 7401 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7402 | if (!Context.getLangOpts().CPlusPlus) { |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7403 | LHS = UsualUnaryConversions(LHS.take()); |
| 7404 | if (LHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7405 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7406 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7407 | RHS = UsualUnaryConversions(RHS.take()); |
| 7408 | if (RHS.isInvalid()) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7409 | return QualType(); |
| 7410 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7411 | if (!LHS.get()->getType()->isScalarType() || |
| 7412 | !RHS.get()->getType()->isScalarType()) |
| 7413 | return InvalidOperands(Loc, LHS, RHS); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7414 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 7415 | return Context.IntTy; |
Anders Carlsson | 0490501 | 2009-10-16 01:44:21 +0000 | [diff] [blame] | 7416 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7417 | |
John McCall | 75f7c0f | 2010-06-04 00:29:51 +0000 | [diff] [blame] | 7418 | // The following is safe because we only use this method for |
| 7419 | // non-overloadable operands. |
| 7420 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 7421 | // C++ [expr.log.and]p1 |
| 7422 | // C++ [expr.log.or]p1 |
John McCall | 75f7c0f | 2010-06-04 00:29:51 +0000 | [diff] [blame] | 7423 | // The operands are both contextually converted to type bool. |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7424 | ExprResult LHSRes = PerformContextuallyConvertToBool(LHS.get()); |
| 7425 | if (LHSRes.isInvalid()) |
| 7426 | return InvalidOperands(Loc, LHS, RHS); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7427 | LHS = LHSRes; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7428 | |
Richard Trieu | 9f60dee | 2011-09-07 01:19:57 +0000 | [diff] [blame] | 7429 | ExprResult RHSRes = PerformContextuallyConvertToBool(RHS.get()); |
| 7430 | if (RHSRes.isInvalid()) |
| 7431 | return InvalidOperands(Loc, LHS, RHS); |
Benjamin Kramer | 3fe198b | 2012-08-23 21:35:17 +0000 | [diff] [blame] | 7432 | RHS = RHSRes; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 7433 | |
Anders Carlsson | a4c98cd | 2009-11-23 21:47:44 +0000 | [diff] [blame] | 7434 | // C++ [expr.log.and]p2 |
| 7435 | // C++ [expr.log.or]p2 |
| 7436 | // The result is a bool. |
| 7437 | return Context.BoolTy; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7438 | } |
| 7439 | |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 7440 | /// IsReadonlyProperty - Verify that otherwise a valid l-value expression |
| 7441 | /// is a read-only property; return true if so. A readonly property expression |
| 7442 | /// depends on various declarations and thus must be treated specially. |
| 7443 | /// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7444 | static bool IsReadonlyProperty(Expr *E, Sema &S) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7445 | const ObjCPropertyRefExpr *PropExpr = dyn_cast<ObjCPropertyRefExpr>(E); |
| 7446 | if (!PropExpr) return false; |
| 7447 | if (PropExpr->isImplicitProperty()) return false; |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 7448 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7449 | ObjCPropertyDecl *PDecl = PropExpr->getExplicitProperty(); |
| 7450 | QualType BaseType = PropExpr->isSuperReceiver() ? |
John McCall | 12f78a6 | 2010-12-02 01:19:52 +0000 | [diff] [blame] | 7451 | PropExpr->getSuperReceiverType() : |
Fariborz Jahanian | 8ac2d44 | 2010-10-14 16:04:05 +0000 | [diff] [blame] | 7452 | PropExpr->getBase()->getType(); |
| 7453 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7454 | if (const ObjCObjectPointerType *OPT = |
| 7455 | BaseType->getAsObjCInterfacePointerType()) |
| 7456 | if (ObjCInterfaceDecl *IFace = OPT->getInterfaceDecl()) |
| 7457 | if (S.isPropertyReadonly(PDecl, IFace)) |
| 7458 | return true; |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 7459 | return false; |
| 7460 | } |
| 7461 | |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 7462 | static bool IsReadonlyMessage(Expr *E, Sema &S) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7463 | const MemberExpr *ME = dyn_cast<MemberExpr>(E); |
| 7464 | if (!ME) return false; |
| 7465 | if (!isa<FieldDecl>(ME->getMemberDecl())) return false; |
| 7466 | ObjCMessageExpr *Base = |
| 7467 | dyn_cast<ObjCMessageExpr>(ME->getBase()->IgnoreParenImpCasts()); |
| 7468 | if (!Base) return false; |
| 7469 | return Base->getMethodDecl() != 0; |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 7470 | } |
| 7471 | |
John McCall | 78dae24 | 2012-03-13 00:37:01 +0000 | [diff] [blame] | 7472 | /// Is the given expression (which must be 'const') a reference to a |
| 7473 | /// variable which was originally non-const, but which has become |
| 7474 | /// 'const' due to being captured within a block? |
| 7475 | enum NonConstCaptureKind { NCCK_None, NCCK_Block, NCCK_Lambda }; |
| 7476 | static NonConstCaptureKind isReferenceToNonConstCapture(Sema &S, Expr *E) { |
| 7477 | assert(E->isLValue() && E->getType().isConstQualified()); |
| 7478 | E = E->IgnoreParens(); |
| 7479 | |
| 7480 | // Must be a reference to a declaration from an enclosing scope. |
| 7481 | DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E); |
| 7482 | if (!DRE) return NCCK_None; |
| 7483 | if (!DRE->refersToEnclosingLocal()) return NCCK_None; |
| 7484 | |
| 7485 | // The declaration must be a variable which is not declared 'const'. |
| 7486 | VarDecl *var = dyn_cast<VarDecl>(DRE->getDecl()); |
| 7487 | if (!var) return NCCK_None; |
| 7488 | if (var->getType().isConstQualified()) return NCCK_None; |
| 7489 | assert(var->hasLocalStorage() && "capture added 'const' to non-local?"); |
| 7490 | |
| 7491 | // Decide whether the first capture was for a block or a lambda. |
| 7492 | DeclContext *DC = S.CurContext; |
| 7493 | while (DC->getParent() != var->getDeclContext()) |
| 7494 | DC = DC->getParent(); |
| 7495 | return (isa<BlockDecl>(DC) ? NCCK_Block : NCCK_Lambda); |
| 7496 | } |
| 7497 | |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7498 | /// CheckForModifiableLvalue - Verify that E is a modifiable lvalue. If not, |
| 7499 | /// emit an error and return true. If so, return false. |
| 7500 | static bool CheckForModifiableLvalue(Expr *E, SourceLocation Loc, Sema &S) { |
Fariborz Jahanian | e7ea28a | 2012-04-10 17:30:10 +0000 | [diff] [blame] | 7501 | assert(!E->hasPlaceholderType(BuiltinType::PseudoObject)); |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 7502 | SourceLocation OrigLoc = Loc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7503 | Expr::isModifiableLvalueResult IsLV = E->isModifiableLvalue(S.Context, |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 7504 | &Loc); |
Fariborz Jahanian | d1fa644 | 2009-01-12 19:55:42 +0000 | [diff] [blame] | 7505 | if (IsLV == Expr::MLV_Valid && IsReadonlyProperty(E, S)) |
| 7506 | IsLV = Expr::MLV_ReadonlyProperty; |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 7507 | else if (IsLV == Expr::MLV_ClassTemporary && IsReadonlyMessage(E, S)) |
| 7508 | IsLV = Expr::MLV_InvalidMessageExpression; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7509 | if (IsLV == Expr::MLV_Valid) |
| 7510 | return false; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7511 | |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7512 | unsigned Diag = 0; |
| 7513 | bool NeedType = false; |
| 7514 | switch (IsLV) { // C99 6.5.16p2 |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7515 | case Expr::MLV_ConstQualified: |
| 7516 | Diag = diag::err_typecheck_assign_const; |
| 7517 | |
John McCall | 78dae24 | 2012-03-13 00:37:01 +0000 | [diff] [blame] | 7518 | // Use a specialized diagnostic when we're assigning to an object |
| 7519 | // from an enclosing function or block. |
| 7520 | if (NonConstCaptureKind NCCK = isReferenceToNonConstCapture(S, E)) { |
| 7521 | if (NCCK == NCCK_Block) |
| 7522 | Diag = diag::err_block_decl_ref_not_modifiable_lvalue; |
| 7523 | else |
| 7524 | Diag = diag::err_lambda_decl_ref_not_modifiable_lvalue; |
| 7525 | break; |
| 7526 | } |
| 7527 | |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 7528 | // In ARC, use some specialized diagnostics for occasions where we |
| 7529 | // infer 'const'. These are always pseudo-strong variables. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7530 | if (S.getLangOpts().ObjCAutoRefCount) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7531 | DeclRefExpr *declRef = dyn_cast<DeclRefExpr>(E->IgnoreParenCasts()); |
| 7532 | if (declRef && isa<VarDecl>(declRef->getDecl())) { |
| 7533 | VarDecl *var = cast<VarDecl>(declRef->getDecl()); |
| 7534 | |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 7535 | // Use the normal diagnostic if it's pseudo-__strong but the |
| 7536 | // user actually wrote 'const'. |
| 7537 | if (var->isARCPseudoStrong() && |
| 7538 | (!var->getTypeSourceInfo() || |
| 7539 | !var->getTypeSourceInfo()->getType().isConstQualified())) { |
| 7540 | // There are two pseudo-strong cases: |
| 7541 | // - self |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7542 | ObjCMethodDecl *method = S.getCurMethodDecl(); |
| 7543 | if (method && var == method->getSelfDecl()) |
Ted Kremenek | 2bbcd5c | 2011-11-14 21:59:25 +0000 | [diff] [blame] | 7544 | Diag = method->isClassMethod() |
| 7545 | ? diag::err_typecheck_arc_assign_self_class_method |
| 7546 | : diag::err_typecheck_arc_assign_self; |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 7547 | |
| 7548 | // - fast enumeration variables |
| 7549 | else |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7550 | Diag = diag::err_typecheck_arr_assign_enumeration; |
John McCall | 7acddac | 2011-06-17 06:42:21 +0000 | [diff] [blame] | 7551 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7552 | SourceRange Assign; |
| 7553 | if (Loc != OrigLoc) |
| 7554 | Assign = SourceRange(OrigLoc, OrigLoc); |
| 7555 | S.Diag(Loc, Diag) << E->getSourceRange() << Assign; |
| 7556 | // We need to preserve the AST regardless, so migration tool |
| 7557 | // can do its job. |
| 7558 | return false; |
| 7559 | } |
| 7560 | } |
| 7561 | } |
| 7562 | |
| 7563 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7564 | case Expr::MLV_ArrayType: |
Richard Smith | 36d02af | 2012-06-04 22:27:30 +0000 | [diff] [blame] | 7565 | case Expr::MLV_ArrayTemporary: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7566 | Diag = diag::err_typecheck_array_not_modifiable_lvalue; |
| 7567 | NeedType = true; |
| 7568 | break; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7569 | case Expr::MLV_NotObjectType: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7570 | Diag = diag::err_typecheck_non_object_not_modifiable_lvalue; |
| 7571 | NeedType = true; |
| 7572 | break; |
Chris Lattner | ca354fa | 2008-11-17 19:51:54 +0000 | [diff] [blame] | 7573 | case Expr::MLV_LValueCast: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7574 | Diag = diag::err_typecheck_lvalue_casts_not_supported; |
| 7575 | break; |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 7576 | case Expr::MLV_Valid: |
| 7577 | llvm_unreachable("did not take early return for MLV_Valid"); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7578 | case Expr::MLV_InvalidExpression: |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 7579 | case Expr::MLV_MemberFunction: |
| 7580 | case Expr::MLV_ClassTemporary: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7581 | Diag = diag::err_typecheck_expression_not_modifiable_lvalue; |
| 7582 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7583 | case Expr::MLV_IncompleteType: |
| 7584 | case Expr::MLV_IncompleteVoidType: |
Douglas Gregor | 86447ec | 2009-03-09 16:13:40 +0000 | [diff] [blame] | 7585 | return S.RequireCompleteType(Loc, E->getType(), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 7586 | diag::err_typecheck_incomplete_type_not_modifiable_lvalue, E); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7587 | case Expr::MLV_DuplicateVectorComponents: |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7588 | Diag = diag::err_typecheck_duplicate_vector_components_not_mlvalue; |
| 7589 | break; |
Fariborz Jahanian | 5daf570 | 2008-11-22 18:39:36 +0000 | [diff] [blame] | 7590 | case Expr::MLV_ReadonlyProperty: |
Fariborz Jahanian | ba8d2d6 | 2008-11-22 20:25:50 +0000 | [diff] [blame] | 7591 | case Expr::MLV_NoSetterProperty: |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7592 | llvm_unreachable("readonly properties should be processed differently"); |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 7593 | case Expr::MLV_InvalidMessageExpression: |
| 7594 | Diag = diag::error_readonly_message_assignment; |
| 7595 | break; |
Fariborz Jahanian | 2514a30 | 2009-12-15 23:59:41 +0000 | [diff] [blame] | 7596 | case Expr::MLV_SubObjCPropertySetting: |
| 7597 | Diag = diag::error_no_subobject_property_setting; |
| 7598 | break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7599 | } |
Steve Naroff | d1861fd | 2007-07-31 12:34:36 +0000 | [diff] [blame] | 7600 | |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 7601 | SourceRange Assign; |
| 7602 | if (Loc != OrigLoc) |
| 7603 | Assign = SourceRange(OrigLoc, OrigLoc); |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7604 | if (NeedType) |
Daniel Dunbar | 44e35f7 | 2009-04-15 00:08:05 +0000 | [diff] [blame] | 7605 | S.Diag(Loc, Diag) << E->getType() << E->getSourceRange() << Assign; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7606 | else |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 7607 | S.Diag(Loc, Diag) << E->getSourceRange() << Assign; |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7608 | return true; |
| 7609 | } |
| 7610 | |
Nico Weber | 7c81b43 | 2012-07-03 02:03:06 +0000 | [diff] [blame] | 7611 | static void CheckIdentityFieldAssignment(Expr *LHSExpr, Expr *RHSExpr, |
| 7612 | SourceLocation Loc, |
| 7613 | Sema &Sema) { |
| 7614 | // C / C++ fields |
Nico Weber | 43bb179 | 2012-06-28 23:53:12 +0000 | [diff] [blame] | 7615 | MemberExpr *ML = dyn_cast<MemberExpr>(LHSExpr); |
| 7616 | MemberExpr *MR = dyn_cast<MemberExpr>(RHSExpr); |
| 7617 | if (ML && MR && ML->getMemberDecl() == MR->getMemberDecl()) { |
| 7618 | if (isa<CXXThisExpr>(ML->getBase()) && isa<CXXThisExpr>(MR->getBase())) |
Nico Weber | 7c81b43 | 2012-07-03 02:03:06 +0000 | [diff] [blame] | 7619 | Sema.Diag(Loc, diag::warn_identity_field_assign) << 0; |
Nico Weber | 43bb179 | 2012-06-28 23:53:12 +0000 | [diff] [blame] | 7620 | } |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7621 | |
Nico Weber | 7c81b43 | 2012-07-03 02:03:06 +0000 | [diff] [blame] | 7622 | // Objective-C instance variables |
Nico Weber | 43bb179 | 2012-06-28 23:53:12 +0000 | [diff] [blame] | 7623 | ObjCIvarRefExpr *OL = dyn_cast<ObjCIvarRefExpr>(LHSExpr); |
| 7624 | ObjCIvarRefExpr *OR = dyn_cast<ObjCIvarRefExpr>(RHSExpr); |
| 7625 | if (OL && OR && OL->getDecl() == OR->getDecl()) { |
| 7626 | DeclRefExpr *RL = dyn_cast<DeclRefExpr>(OL->getBase()->IgnoreImpCasts()); |
| 7627 | DeclRefExpr *RR = dyn_cast<DeclRefExpr>(OR->getBase()->IgnoreImpCasts()); |
| 7628 | if (RL && RR && RL->getDecl() == RR->getDecl()) |
Nico Weber | 7c81b43 | 2012-07-03 02:03:06 +0000 | [diff] [blame] | 7629 | Sema.Diag(Loc, diag::warn_identity_field_assign) << 1; |
Nico Weber | 43bb179 | 2012-06-28 23:53:12 +0000 | [diff] [blame] | 7630 | } |
| 7631 | } |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7632 | |
| 7633 | // C99 6.5.16.1 |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7634 | QualType Sema::CheckAssignmentOperands(Expr *LHSExpr, ExprResult &RHS, |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7635 | SourceLocation Loc, |
| 7636 | QualType CompoundType) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7637 | assert(!LHSExpr->hasPlaceholderType(BuiltinType::PseudoObject)); |
| 7638 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7639 | // Verify that LHS is a modifiable lvalue, and emit error if not. |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7640 | if (CheckForModifiableLvalue(LHSExpr, Loc, *this)) |
Chris Lattner | f67bd9f | 2008-11-18 01:22:49 +0000 | [diff] [blame] | 7641 | return QualType(); |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7642 | |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7643 | QualType LHSType = LHSExpr->getType(); |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 7644 | QualType RHSType = CompoundType.isNull() ? RHS.get()->getType() : |
| 7645 | CompoundType; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7646 | AssignConvertType ConvTy; |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7647 | if (CompoundType.isNull()) { |
Nico Weber | 43bb179 | 2012-06-28 23:53:12 +0000 | [diff] [blame] | 7648 | Expr *RHSCheck = RHS.get(); |
| 7649 | |
Nico Weber | 7c81b43 | 2012-07-03 02:03:06 +0000 | [diff] [blame] | 7650 | CheckIdentityFieldAssignment(LHSExpr, RHSCheck, Loc, *this); |
Nico Weber | 43bb179 | 2012-06-28 23:53:12 +0000 | [diff] [blame] | 7651 | |
Fariborz Jahanian | e2a901a | 2010-06-07 22:02:01 +0000 | [diff] [blame] | 7652 | QualType LHSTy(LHSType); |
Fariborz Jahanian | e2a901a | 2010-06-07 22:02:01 +0000 | [diff] [blame] | 7653 | ConvTy = CheckSingleAssignmentConstraints(LHSTy, RHS); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7654 | if (RHS.isInvalid()) |
| 7655 | return QualType(); |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 7656 | // Special case of NSObject attributes on c-style pointer types. |
| 7657 | if (ConvTy == IncompatiblePointer && |
| 7658 | ((Context.isObjCNSObjectType(LHSType) && |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 7659 | RHSType->isObjCObjectPointerType()) || |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 7660 | (Context.isObjCNSObjectType(RHSType) && |
Steve Naroff | f495456 | 2009-07-16 15:41:00 +0000 | [diff] [blame] | 7661 | LHSType->isObjCObjectPointerType()))) |
Fariborz Jahanian | fa23c1d | 2009-01-13 23:34:40 +0000 | [diff] [blame] | 7662 | ConvTy = Compatible; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7663 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7664 | if (ConvTy == Compatible && |
Fariborz Jahanian | 466f45a | 2012-01-24 19:40:13 +0000 | [diff] [blame] | 7665 | LHSType->isObjCObjectType()) |
Fariborz Jahanian | 7b383e4 | 2012-01-24 18:05:45 +0000 | [diff] [blame] | 7666 | Diag(Loc, diag::err_objc_object_assignment) |
| 7667 | << LHSType; |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 7668 | |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7669 | // If the RHS is a unary plus or minus, check to see if they = and + are |
| 7670 | // right next to each other. If so, the user may have typo'd "x =+ 4" |
| 7671 | // instead of "x += 4". |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7672 | if (ImplicitCastExpr *ICE = dyn_cast<ImplicitCastExpr>(RHSCheck)) |
| 7673 | RHSCheck = ICE->getSubExpr(); |
| 7674 | if (UnaryOperator *UO = dyn_cast<UnaryOperator>(RHSCheck)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7675 | if ((UO->getOpcode() == UO_Plus || |
| 7676 | UO->getOpcode() == UO_Minus) && |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7677 | Loc.isFileID() && UO->getOperatorLoc().isFileID() && |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7678 | // Only if the two operators are exactly adjacent. |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 7679 | Loc.getLocWithOffset(1) == UO->getOperatorLoc() && |
Chris Lattner | 399bd1b | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 7680 | // And there is a space or other character before the subexpr of the |
| 7681 | // unary +/-. We don't want to warn on "x=-1". |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 7682 | Loc.getLocWithOffset(2) != UO->getSubExpr()->getLocStart() && |
Chris Lattner | 3e87209 | 2009-03-09 07:11:10 +0000 | [diff] [blame] | 7683 | UO->getSubExpr()->getLocStart().isFileID()) { |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 7684 | Diag(Loc, diag::warn_not_compound_assign) |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7685 | << (UO->getOpcode() == UO_Plus ? "+" : "-") |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 7686 | << SourceRange(UO->getOperatorLoc(), UO->getOperatorLoc()); |
Chris Lattner | 399bd1b | 2009-03-08 06:51:10 +0000 | [diff] [blame] | 7687 | } |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7688 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7689 | |
| 7690 | if (ConvTy == Compatible) { |
| 7691 | if (LHSType.getObjCLifetime() == Qualifiers::OCL_Strong) |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7692 | checkRetainCycles(LHSExpr, RHS.get()); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7693 | else if (getLangOpts().ObjCAutoRefCount) |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7694 | checkUnsafeExprAssigns(Loc, LHSExpr, RHS.get()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 7695 | } |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7696 | } else { |
| 7697 | // Compound assignment "x += y" |
Douglas Gregor | b608b98 | 2011-01-28 02:26:04 +0000 | [diff] [blame] | 7698 | ConvTy = CheckAssignmentConstraints(Loc, LHSType, RHSType); |
Chris Lattner | 2c15647 | 2008-08-21 18:04:13 +0000 | [diff] [blame] | 7699 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7700 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7701 | if (DiagnoseAssignmentResult(ConvTy, Loc, LHSType, RHSType, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7702 | RHS.get(), AA_Assigning)) |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 7703 | return QualType(); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7704 | |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 7705 | CheckForNullPointerDereference(*this, LHSExpr); |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 7706 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7707 | // C99 6.5.16p3: The type of an assignment expression is the type of the |
| 7708 | // left operand unless the left operand has qualified type, in which case |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7709 | // it is the unqualified version of the type of the left operand. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7710 | // C99 6.5.16.1p2: In simple assignment, the value of the right operand |
| 7711 | // is converted to the type of the assignment expression (above). |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 7712 | // 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] | 7713 | // operand. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7714 | return (getLangOpts().CPlusPlus |
John McCall | 2bf6f49 | 2010-10-12 02:19:57 +0000 | [diff] [blame] | 7715 | ? LHSType : LHSType.getUnqualifiedType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7716 | } |
| 7717 | |
Chris Lattner | 29a1cfb | 2008-11-18 01:30:42 +0000 | [diff] [blame] | 7718 | // C99 6.5.17 |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7719 | static QualType CheckCommaOperands(Sema &S, ExprResult &LHS, ExprResult &RHS, |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7720 | SourceLocation Loc) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 7721 | LHS = S.CheckPlaceholderExpr(LHS.take()); |
| 7722 | RHS = S.CheckPlaceholderExpr(RHS.take()); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7723 | if (LHS.isInvalid() || RHS.isInvalid()) |
Douglas Gregor | 7ad5d42 | 2010-11-09 21:07:58 +0000 | [diff] [blame] | 7724 | return QualType(); |
| 7725 | |
John McCall | cf2e506 | 2010-10-12 07:14:40 +0000 | [diff] [blame] | 7726 | // C's comma performs lvalue conversion (C99 6.3.2.1) on both its |
| 7727 | // operands, but not unary promotions. |
| 7728 | // 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] | 7729 | |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7730 | // So we treat the LHS as a ignored value, and in C++ we allow the |
| 7731 | // containing site to determine what should be done with the RHS. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7732 | LHS = S.IgnoredValueConversions(LHS.take()); |
| 7733 | if (LHS.isInvalid()) |
| 7734 | return QualType(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 7735 | |
Eli Friedman | a611506 | 2012-05-24 00:47:05 +0000 | [diff] [blame] | 7736 | S.DiagnoseUnusedExprResult(LHS.get()); |
| 7737 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7738 | if (!S.getLangOpts().CPlusPlus) { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7739 | RHS = S.DefaultFunctionArrayLvalueConversion(RHS.take()); |
| 7740 | if (RHS.isInvalid()) |
| 7741 | return QualType(); |
| 7742 | if (!RHS.get()->getType()->isVoidType()) |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 7743 | S.RequireCompleteType(Loc, RHS.get()->getType(), |
| 7744 | diag::err_incomplete_type); |
John McCall | cf2e506 | 2010-10-12 07:14:40 +0000 | [diff] [blame] | 7745 | } |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 7746 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 7747 | return RHS.get()->getType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7748 | } |
| 7749 | |
Steve Naroff | 49b4526 | 2007-07-13 16:58:59 +0000 | [diff] [blame] | 7750 | /// CheckIncrementDecrementOperand - unlike most "Check" methods, this routine |
| 7751 | /// doesn't need to call UsualUnaryConversions or UsualArithmeticConversions. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7752 | static QualType CheckIncrementDecrementOperand(Sema &S, Expr *Op, |
| 7753 | ExprValueKind &VK, |
| 7754 | SourceLocation OpLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7755 | bool IsInc, bool IsPrefix) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7756 | if (Op->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7757 | return S.Context.DependentTy; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 7758 | |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7759 | QualType ResType = Op->getType(); |
David Chisnall | 7a7ee30 | 2012-01-16 17:27:18 +0000 | [diff] [blame] | 7760 | // Atomic types can be used for increment / decrement where the non-atomic |
| 7761 | // versions can, so ignore the _Atomic() specifier for the purpose of |
| 7762 | // checking. |
| 7763 | if (const AtomicType *ResAtomicType = ResType->getAs<AtomicType>()) |
| 7764 | ResType = ResAtomicType->getValueType(); |
| 7765 | |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7766 | assert(!ResType.isNull() && "no type for increment/decrement expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7767 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7768 | if (S.getLangOpts().CPlusPlus && ResType->isBooleanType()) { |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 7769 | // Decrement of bool is not allowed. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7770 | if (!IsInc) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7771 | S.Diag(OpLoc, diag::err_decrement_bool) << Op->getSourceRange(); |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 7772 | return QualType(); |
| 7773 | } |
| 7774 | // Increment of bool sets it to true, but is deprecated. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7775 | S.Diag(OpLoc, diag::warn_increment_bool) << Op->getSourceRange(); |
Sebastian Redl | e6d5a4a | 2008-12-20 09:35:34 +0000 | [diff] [blame] | 7776 | } else if (ResType->isRealType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7777 | // OK! |
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 7778 | } else if (ResType->isPointerType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7779 | // C99 6.5.2.4p2, 6.5.6p2 |
Chandler Carruth | 13b21be | 2011-06-27 08:02:19 +0000 | [diff] [blame] | 7780 | if (!checkArithmeticOpPointerOperand(S, OpLoc, Op)) |
Douglas Gregor | 4ec339f | 2009-01-19 19:26:10 +0000 | [diff] [blame] | 7781 | return QualType(); |
John McCall | 1503f0d | 2012-07-31 05:14:30 +0000 | [diff] [blame] | 7782 | } else if (ResType->isObjCObjectPointerType()) { |
| 7783 | // On modern runtimes, ObjC pointer arithmetic is forbidden. |
| 7784 | // Otherwise, we just need a complete type. |
| 7785 | if (checkArithmeticIncompletePointerType(S, OpLoc, Op) || |
| 7786 | checkArithmeticOnObjCPointer(S, OpLoc, Op)) |
| 7787 | return QualType(); |
Eli Friedman | 5b088a1 | 2010-01-03 00:20:48 +0000 | [diff] [blame] | 7788 | } else if (ResType->isAnyComplexType()) { |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7789 | // C99 does not support ++/-- on complex types, we allow as an extension. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7790 | S.Diag(OpLoc, diag::ext_integer_increment_complex) |
Chris Lattner | d162584 | 2008-11-24 06:25:27 +0000 | [diff] [blame] | 7791 | << ResType << Op->getSourceRange(); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7792 | } else if (ResType->isPlaceholderType()) { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 7793 | ExprResult PR = S.CheckPlaceholderExpr(Op); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7794 | if (PR.isInvalid()) return QualType(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7795 | return CheckIncrementDecrementOperand(S, PR.take(), VK, OpLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7796 | IsInc, IsPrefix); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7797 | } else if (S.getLangOpts().AltiVec && ResType->isVectorType()) { |
Anton Yartsev | 683564a | 2011-02-07 02:17:30 +0000 | [diff] [blame] | 7798 | // OK! ( C/C++ Language Extensions for CBEA(Version 2.6) 10.3 ) |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7799 | } else { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7800 | S.Diag(OpLoc, diag::err_typecheck_illegal_increment_decrement) |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 7801 | << ResType << int(IsInc) << Op->getSourceRange(); |
Chris Lattner | 3528d35 | 2008-11-21 07:05:48 +0000 | [diff] [blame] | 7802 | return QualType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7803 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7804 | // 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] | 7805 | // Now make sure the operand is a modifiable lvalue. |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7806 | if (CheckForModifiableLvalue(Op, OpLoc, S)) |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7807 | return QualType(); |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 7808 | // In C++, a prefix increment is the same type as the operand. Otherwise |
| 7809 | // (in C or with postfix), the increment is the unqualified type of the |
| 7810 | // operand. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7811 | if (IsPrefix && S.getLangOpts().CPlusPlus) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7812 | VK = VK_LValue; |
| 7813 | return ResType; |
| 7814 | } else { |
| 7815 | VK = VK_RValue; |
| 7816 | return ResType.getUnqualifiedType(); |
| 7817 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7818 | } |
Fariborz Jahanian | c4e1a68 | 2010-09-14 23:02:38 +0000 | [diff] [blame] | 7819 | |
| 7820 | |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 7821 | /// getPrimaryDecl - Helper function for CheckAddressOfOperand(). |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7822 | /// This routine allows us to typecheck complex/recursive expressions |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7823 | /// where the declaration is needed for type checking. We only need to |
| 7824 | /// handle cases when the expression references a function designator |
| 7825 | /// or is an lvalue. Here are some examples: |
| 7826 | /// - &(x) => x |
| 7827 | /// - &*****f => f for f a function designator. |
| 7828 | /// - &s.xx => s |
| 7829 | /// - &s.zz[1].yy -> s, if zz is an array |
| 7830 | /// - *(x + 1) -> x, if x is an array |
| 7831 | /// - &"123"[2] -> 0 |
| 7832 | /// - & __real__ x -> x |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7833 | static ValueDecl *getPrimaryDecl(Expr *E) { |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7834 | switch (E->getStmtClass()) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7835 | case Stmt::DeclRefExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7836 | return cast<DeclRefExpr>(E)->getDecl(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7837 | case Stmt::MemberExprClass: |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7838 | // If this is an arrow operator, the address is an offset from |
| 7839 | // the base's value, so the object the base refers to is |
| 7840 | // irrelevant. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7841 | if (cast<MemberExpr>(E)->isArrow()) |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 7842 | return 0; |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7843 | // Otherwise, the expression refers to a part of the base |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7844 | return getPrimaryDecl(cast<MemberExpr>(E)->getBase()); |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 7845 | case Stmt::ArraySubscriptExprClass: { |
Mike Stump | 390b4cc | 2009-05-16 07:39:55 +0000 | [diff] [blame] | 7846 | // FIXME: This code shouldn't be necessary! We should catch the implicit |
| 7847 | // promotion of register arrays earlier. |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7848 | Expr* Base = cast<ArraySubscriptExpr>(E)->getBase(); |
| 7849 | if (ImplicitCastExpr* ICE = dyn_cast<ImplicitCastExpr>(Base)) { |
| 7850 | if (ICE->getSubExpr()->getType()->isArrayType()) |
| 7851 | return getPrimaryDecl(ICE->getSubExpr()); |
| 7852 | } |
| 7853 | return 0; |
Anders Carlsson | 369dee4 | 2008-02-01 07:15:58 +0000 | [diff] [blame] | 7854 | } |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7855 | case Stmt::UnaryOperatorClass: { |
| 7856 | UnaryOperator *UO = cast<UnaryOperator>(E); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7857 | |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7858 | switch(UO->getOpcode()) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7859 | case UO_Real: |
| 7860 | case UO_Imag: |
| 7861 | case UO_Extension: |
Daniel Dunbar | 1e76ce6 | 2008-08-04 20:02:37 +0000 | [diff] [blame] | 7862 | return getPrimaryDecl(UO->getSubExpr()); |
| 7863 | default: |
| 7864 | return 0; |
| 7865 | } |
| 7866 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7867 | case Stmt::ParenExprClass: |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7868 | return getPrimaryDecl(cast<ParenExpr>(E)->getSubExpr()); |
Chris Lattner | f82228f | 2007-11-16 17:46:48 +0000 | [diff] [blame] | 7869 | case Stmt::ImplicitCastExprClass: |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7870 | // If the result of an implicit cast is an l-value, we care about |
| 7871 | // the sub-expression; otherwise, the result here doesn't matter. |
Chris Lattner | f0467b3 | 2008-04-02 04:24:33 +0000 | [diff] [blame] | 7872 | return getPrimaryDecl(cast<ImplicitCastExpr>(E)->getSubExpr()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7873 | default: |
| 7874 | return 0; |
| 7875 | } |
| 7876 | } |
| 7877 | |
Richard Trieu | 5520f23 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 7878 | namespace { |
| 7879 | enum { |
| 7880 | AO_Bit_Field = 0, |
| 7881 | AO_Vector_Element = 1, |
| 7882 | AO_Property_Expansion = 2, |
| 7883 | AO_Register_Variable = 3, |
| 7884 | AO_No_Error = 4 |
| 7885 | }; |
| 7886 | } |
Richard Trieu | 09a26ad | 2011-09-02 00:47:55 +0000 | [diff] [blame] | 7887 | /// \brief Diagnose invalid operand for address of operations. |
| 7888 | /// |
| 7889 | /// \param Type The type of operand which cannot have its address taken. |
Richard Trieu | 09a26ad | 2011-09-02 00:47:55 +0000 | [diff] [blame] | 7890 | static void diagnoseAddressOfInvalidType(Sema &S, SourceLocation Loc, |
| 7891 | Expr *E, unsigned Type) { |
| 7892 | S.Diag(Loc, diag::err_typecheck_address_of) << Type << E->getSourceRange(); |
| 7893 | } |
| 7894 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7895 | /// CheckAddressOfOperand - The operand of & must be either a function |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 7896 | /// 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] | 7897 | /// 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] | 7898 | /// Note: The usual conversions are *not* applied to the operand of the & |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7899 | /// 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] | 7900 | /// 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] | 7901 | /// we allow the '&' but retain the overloaded-function type. |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7902 | static QualType CheckAddressOfOperand(Sema &S, ExprResult &OrigOp, |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7903 | SourceLocation OpLoc) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7904 | if (const BuiltinType *PTy = OrigOp.get()->getType()->getAsPlaceholderType()){ |
| 7905 | if (PTy->getKind() == BuiltinType::Overload) { |
| 7906 | if (!isa<OverloadExpr>(OrigOp.get()->IgnoreParens())) { |
| 7907 | S.Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof) |
| 7908 | << OrigOp.get()->getSourceRange(); |
| 7909 | return QualType(); |
| 7910 | } |
| 7911 | |
| 7912 | return S.Context.OverloadTy; |
| 7913 | } |
| 7914 | |
| 7915 | if (PTy->getKind() == BuiltinType::UnknownAny) |
| 7916 | return S.Context.UnknownAnyTy; |
| 7917 | |
| 7918 | if (PTy->getKind() == BuiltinType::BoundMember) { |
| 7919 | S.Diag(OpLoc, diag::err_invalid_form_pointer_member_function) |
| 7920 | << OrigOp.get()->getSourceRange(); |
Douglas Gregor | 44efed0 | 2011-10-09 19:10:41 +0000 | [diff] [blame] | 7921 | return QualType(); |
| 7922 | } |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7923 | |
| 7924 | OrigOp = S.CheckPlaceholderExpr(OrigOp.take()); |
| 7925 | if (OrigOp.isInvalid()) return QualType(); |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 7926 | } |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7927 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7928 | if (OrigOp.get()->isTypeDependent()) |
| 7929 | return S.Context.DependentTy; |
| 7930 | |
| 7931 | assert(!OrigOp.get()->getType()->isPlaceholderType()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 7932 | |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7933 | // Make sure to ignore parentheses in subsequent checks |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7934 | Expr *op = OrigOp.get()->IgnoreParens(); |
Douglas Gregor | 9103bb2 | 2008-12-17 22:52:20 +0000 | [diff] [blame] | 7935 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 7936 | if (S.getLangOpts().C99) { |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 7937 | // Implement C99-only parts of addressof rules. |
| 7938 | if (UnaryOperator* uOp = dyn_cast<UnaryOperator>(op)) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 7939 | if (uOp->getOpcode() == UO_Deref) |
Steve Naroff | 08f1967 | 2008-01-13 17:10:08 +0000 | [diff] [blame] | 7940 | // Per C99 6.5.3.2, the address of a deref always returns a valid result |
| 7941 | // (assuming the deref expression is valid). |
| 7942 | return uOp->getSubExpr()->getType(); |
| 7943 | } |
| 7944 | // Technically, there should be a check for array subscript |
| 7945 | // expressions here, but the result of one is always an lvalue anyway. |
| 7946 | } |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 7947 | ValueDecl *dcl = getPrimaryDecl(op); |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 7948 | Expr::LValueClassification lval = op->ClassifyLValue(S.Context); |
Richard Trieu | 5520f23 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 7949 | unsigned AddressOfError = AO_No_Error; |
Nuno Lopes | 6b6609f | 2008-12-16 22:59:47 +0000 | [diff] [blame] | 7950 | |
Fariborz Jahanian | 077f490 | 2011-03-26 19:48:30 +0000 | [diff] [blame] | 7951 | if (lval == Expr::LV_ClassTemporary) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7952 | bool sfinae = S.isSFINAEContext(); |
| 7953 | S.Diag(OpLoc, sfinae ? diag::err_typecheck_addrof_class_temporary |
| 7954 | : diag::ext_typecheck_addrof_class_temporary) |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 7955 | << op->getType() << op->getSourceRange(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7956 | if (sfinae) |
Douglas Gregor | e873fb7 | 2010-02-16 21:39:57 +0000 | [diff] [blame] | 7957 | return QualType(); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7958 | } else if (isa<ObjCSelectorExpr>(op)) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7959 | return S.Context.getPointerType(op->getType()); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7960 | } else if (lval == Expr::LV_MemberFunction) { |
| 7961 | // If it's an instance method, make a member pointer. |
| 7962 | // The expression must have exactly the form &A::foo. |
| 7963 | |
| 7964 | // If the underlying expression isn't a decl ref, give up. |
| 7965 | if (!isa<DeclRefExpr>(op)) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7966 | S.Diag(OpLoc, diag::err_invalid_form_pointer_member_function) |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7967 | << OrigOp.get()->getSourceRange(); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7968 | return QualType(); |
| 7969 | } |
| 7970 | DeclRefExpr *DRE = cast<DeclRefExpr>(op); |
| 7971 | CXXMethodDecl *MD = cast<CXXMethodDecl>(DRE->getDecl()); |
| 7972 | |
| 7973 | // The id-expression was parenthesized. |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7974 | if (OrigOp.get() != DRE) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7975 | S.Diag(OpLoc, diag::err_parens_pointer_member_function) |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7976 | << OrigOp.get()->getSourceRange(); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7977 | |
| 7978 | // The method was named without a qualifier. |
| 7979 | } else if (!DRE->getQualifier()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7980 | S.Diag(OpLoc, diag::err_unqualified_pointer_member_function) |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7981 | << op->getSourceRange(); |
| 7982 | } |
| 7983 | |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 7984 | return S.Context.getMemberPointerType(op->getType(), |
| 7985 | S.Context.getTypeDeclType(MD->getParent()).getTypePtr()); |
John McCall | 9c72c60 | 2010-08-27 09:08:28 +0000 | [diff] [blame] | 7986 | } else if (lval != Expr::LV_Valid && lval != Expr::LV_IncompleteVoidType) { |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 7987 | // C99 6.5.3.2p1 |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 7988 | // The operand must be either an l-value or a function designator |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 7989 | if (!op->getType()->isFunctionType()) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7990 | // Use a special diagnostic for loads from property references. |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 7991 | if (isa<PseudoObjectExpr>(op)) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 7992 | AddressOfError = AO_Property_Expansion; |
| 7993 | } else { |
| 7994 | // FIXME: emit more specific diag... |
| 7995 | S.Diag(OpLoc, diag::err_typecheck_invalid_lvalue_addrof) |
| 7996 | << op->getSourceRange(); |
| 7997 | return QualType(); |
| 7998 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7999 | } |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 8000 | } else if (op->getObjectKind() == OK_BitField) { // C99 6.5.3.2p1 |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8001 | // The operand cannot be a bit-field |
Richard Trieu | 5520f23 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 8002 | AddressOfError = AO_Bit_Field; |
John McCall | 7eb0a9e | 2010-11-24 05:12:34 +0000 | [diff] [blame] | 8003 | } else if (op->getObjectKind() == OK_VectorComponent) { |
Eli Friedman | 23d58ce | 2009-04-20 08:23:18 +0000 | [diff] [blame] | 8004 | // The operand cannot be an element of a vector |
Richard Trieu | 5520f23 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 8005 | AddressOfError = AO_Vector_Element; |
Steve Naroff | bcb2b61 | 2008-02-29 23:30:25 +0000 | [diff] [blame] | 8006 | } else if (dcl) { // C99 6.5.3.2p1 |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8007 | // 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] | 8008 | // with the register storage-class specifier. |
| 8009 | if (const VarDecl *vd = dyn_cast<VarDecl>(dcl)) { |
Fariborz Jahanian | 4020f87 | 2010-08-24 22:21:48 +0000 | [diff] [blame] | 8010 | // in C++ it is not error to take address of a register |
| 8011 | // variable (c++03 7.1.1P3) |
John McCall | d931b08 | 2010-08-26 03:08:43 +0000 | [diff] [blame] | 8012 | if (vd->getStorageClass() == SC_Register && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8013 | !S.getLangOpts().CPlusPlus) { |
Richard Trieu | 5520f23 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 8014 | AddressOfError = AO_Register_Variable; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8015 | } |
John McCall | ba13543 | 2009-11-21 08:51:07 +0000 | [diff] [blame] | 8016 | } else if (isa<FunctionTemplateDecl>(dcl)) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8017 | return S.Context.OverloadTy; |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 8018 | } else if (isa<FieldDecl>(dcl) || isa<IndirectFieldDecl>(dcl)) { |
Douglas Gregor | 2988205 | 2008-12-10 21:26:49 +0000 | [diff] [blame] | 8019 | // Okay: we can take the address of a field. |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 8020 | // Could be a pointer to member, though, if there is an explicit |
| 8021 | // scope qualifier for the class. |
Douglas Gregor | a2813ce | 2009-10-23 18:54:35 +0000 | [diff] [blame] | 8022 | if (isa<DeclRefExpr>(op) && cast<DeclRefExpr>(op)->getQualifier()) { |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 8023 | DeclContext *Ctx = dcl->getDeclContext(); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 8024 | if (Ctx && Ctx->isRecord()) { |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 8025 | if (dcl->getType()->isReferenceType()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8026 | S.Diag(OpLoc, |
| 8027 | diag::err_cannot_form_pointer_to_member_of_reference_type) |
John McCall | 5808ce4 | 2011-02-03 08:15:49 +0000 | [diff] [blame] | 8028 | << dcl->getDeclName() << dcl->getType(); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 8029 | return QualType(); |
| 8030 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 8031 | |
Argyrios Kyrtzidis | 0413db4 | 2011-01-31 07:04:29 +0000 | [diff] [blame] | 8032 | while (cast<RecordDecl>(Ctx)->isAnonymousStructOrUnion()) |
| 8033 | Ctx = Ctx->getParent(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8034 | return S.Context.getMemberPointerType(op->getType(), |
| 8035 | S.Context.getTypeDeclType(cast<RecordDecl>(Ctx)).getTypePtr()); |
Anders Carlsson | f9e48bd | 2009-07-08 21:45:58 +0000 | [diff] [blame] | 8036 | } |
Sebastian Redl | ebc07d5 | 2009-02-03 20:19:35 +0000 | [diff] [blame] | 8037 | } |
Eli Friedman | 7b2f51c | 2011-08-26 20:28:17 +0000 | [diff] [blame] | 8038 | } else if (!isa<FunctionDecl>(dcl) && !isa<NonTypeTemplateParmDecl>(dcl)) |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 8039 | llvm_unreachable("Unknown/unexpected decl type"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8040 | } |
Sebastian Redl | 33b399a | 2009-02-04 21:23:32 +0000 | [diff] [blame] | 8041 | |
Richard Trieu | 5520f23 | 2011-09-07 21:46:33 +0000 | [diff] [blame] | 8042 | if (AddressOfError != AO_No_Error) { |
| 8043 | diagnoseAddressOfInvalidType(S, OpLoc, op, AddressOfError); |
| 8044 | return QualType(); |
| 8045 | } |
| 8046 | |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 8047 | if (lval == Expr::LV_IncompleteVoidType) { |
| 8048 | // Taking the address of a void variable is technically illegal, but we |
| 8049 | // allow it in cases which are otherwise valid. |
| 8050 | // Example: "extern void x; void* y = &x;". |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8051 | S.Diag(OpLoc, diag::ext_typecheck_addrof_void) << op->getSourceRange(); |
Eli Friedman | 441cf10 | 2009-05-16 23:27:50 +0000 | [diff] [blame] | 8052 | } |
| 8053 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8054 | // 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] | 8055 | if (op->getType()->isObjCObjectType()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8056 | return S.Context.getObjCObjectPointerType(op->getType()); |
| 8057 | return S.Context.getPointerType(op->getType()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8058 | } |
| 8059 | |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8060 | /// CheckIndirectionOperand - Type check unary indirection (prefix '*'). |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8061 | static QualType CheckIndirectionOperand(Sema &S, Expr *Op, ExprValueKind &VK, |
| 8062 | SourceLocation OpLoc) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8063 | if (Op->isTypeDependent()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8064 | return S.Context.DependentTy; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8065 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8066 | ExprResult ConvResult = S.UsualUnaryConversions(Op); |
| 8067 | if (ConvResult.isInvalid()) |
| 8068 | return QualType(); |
| 8069 | Op = ConvResult.take(); |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8070 | QualType OpTy = Op->getType(); |
| 8071 | QualType Result; |
Argyrios Kyrtzidis | f4bbbf0 | 2011-05-02 18:21:19 +0000 | [diff] [blame] | 8072 | |
| 8073 | if (isa<CXXReinterpretCastExpr>(Op)) { |
| 8074 | QualType OpOrigType = Op->IgnoreParenCasts()->getType(); |
| 8075 | S.CheckCompatibleReinterpretCast(OpOrigType, OpTy, /*IsDereference*/true, |
| 8076 | Op->getSourceRange()); |
| 8077 | } |
| 8078 | |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8079 | // Note that per both C89 and C99, indirection is always legal, even if OpTy |
| 8080 | // is an incomplete type or void. It would be possible to warn about |
| 8081 | // dereferencing a void pointer, but it's completely well-defined, and such a |
| 8082 | // warning is unlikely to catch any mistakes. |
| 8083 | if (const PointerType *PT = OpTy->getAs<PointerType>()) |
| 8084 | Result = PT->getPointeeType(); |
| 8085 | else if (const ObjCObjectPointerType *OPT = |
| 8086 | OpTy->getAs<ObjCObjectPointerType>()) |
| 8087 | Result = OPT->getPointeeType(); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8088 | else { |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 8089 | ExprResult PR = S.CheckPlaceholderExpr(Op); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8090 | if (PR.isInvalid()) return QualType(); |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8091 | if (PR.take() != Op) |
| 8092 | return CheckIndirectionOperand(S, PR.take(), VK, OpLoc); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8093 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8094 | |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8095 | if (Result.isNull()) { |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8096 | S.Diag(OpLoc, diag::err_typecheck_indirection_requires_pointer) |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8097 | << OpTy << Op->getSourceRange(); |
| 8098 | return QualType(); |
| 8099 | } |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8100 | |
| 8101 | // Dereferences are usually l-values... |
| 8102 | VK = VK_LValue; |
| 8103 | |
| 8104 | // ...except that certain expressions are never l-values in C. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8105 | if (!S.getLangOpts().CPlusPlus && Result.isCForbiddenLValueType()) |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8106 | VK = VK_RValue; |
Chris Lattner | fd79a9d | 2010-07-05 19:17:26 +0000 | [diff] [blame] | 8107 | |
| 8108 | return Result; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8109 | } |
| 8110 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8111 | static inline BinaryOperatorKind ConvertTokenKindToBinaryOpcode( |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8112 | tok::TokenKind Kind) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8113 | BinaryOperatorKind Opc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8114 | switch (Kind) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 8115 | default: llvm_unreachable("Unknown binop!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8116 | case tok::periodstar: Opc = BO_PtrMemD; break; |
| 8117 | case tok::arrowstar: Opc = BO_PtrMemI; break; |
| 8118 | case tok::star: Opc = BO_Mul; break; |
| 8119 | case tok::slash: Opc = BO_Div; break; |
| 8120 | case tok::percent: Opc = BO_Rem; break; |
| 8121 | case tok::plus: Opc = BO_Add; break; |
| 8122 | case tok::minus: Opc = BO_Sub; break; |
| 8123 | case tok::lessless: Opc = BO_Shl; break; |
| 8124 | case tok::greatergreater: Opc = BO_Shr; break; |
| 8125 | case tok::lessequal: Opc = BO_LE; break; |
| 8126 | case tok::less: Opc = BO_LT; break; |
| 8127 | case tok::greaterequal: Opc = BO_GE; break; |
| 8128 | case tok::greater: Opc = BO_GT; break; |
| 8129 | case tok::exclaimequal: Opc = BO_NE; break; |
| 8130 | case tok::equalequal: Opc = BO_EQ; break; |
| 8131 | case tok::amp: Opc = BO_And; break; |
| 8132 | case tok::caret: Opc = BO_Xor; break; |
| 8133 | case tok::pipe: Opc = BO_Or; break; |
| 8134 | case tok::ampamp: Opc = BO_LAnd; break; |
| 8135 | case tok::pipepipe: Opc = BO_LOr; break; |
| 8136 | case tok::equal: Opc = BO_Assign; break; |
| 8137 | case tok::starequal: Opc = BO_MulAssign; break; |
| 8138 | case tok::slashequal: Opc = BO_DivAssign; break; |
| 8139 | case tok::percentequal: Opc = BO_RemAssign; break; |
| 8140 | case tok::plusequal: Opc = BO_AddAssign; break; |
| 8141 | case tok::minusequal: Opc = BO_SubAssign; break; |
| 8142 | case tok::lesslessequal: Opc = BO_ShlAssign; break; |
| 8143 | case tok::greatergreaterequal: Opc = BO_ShrAssign; break; |
| 8144 | case tok::ampequal: Opc = BO_AndAssign; break; |
| 8145 | case tok::caretequal: Opc = BO_XorAssign; break; |
| 8146 | case tok::pipeequal: Opc = BO_OrAssign; break; |
| 8147 | case tok::comma: Opc = BO_Comma; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8148 | } |
| 8149 | return Opc; |
| 8150 | } |
| 8151 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8152 | static inline UnaryOperatorKind ConvertTokenKindToUnaryOpcode( |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8153 | tok::TokenKind Kind) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8154 | UnaryOperatorKind Opc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8155 | switch (Kind) { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 8156 | default: llvm_unreachable("Unknown unary op!"); |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8157 | case tok::plusplus: Opc = UO_PreInc; break; |
| 8158 | case tok::minusminus: Opc = UO_PreDec; break; |
| 8159 | case tok::amp: Opc = UO_AddrOf; break; |
| 8160 | case tok::star: Opc = UO_Deref; break; |
| 8161 | case tok::plus: Opc = UO_Plus; break; |
| 8162 | case tok::minus: Opc = UO_Minus; break; |
| 8163 | case tok::tilde: Opc = UO_Not; break; |
| 8164 | case tok::exclaim: Opc = UO_LNot; break; |
| 8165 | case tok::kw___real: Opc = UO_Real; break; |
| 8166 | case tok::kw___imag: Opc = UO_Imag; break; |
| 8167 | case tok::kw___extension__: Opc = UO_Extension; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8168 | } |
| 8169 | return Opc; |
| 8170 | } |
| 8171 | |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 8172 | /// DiagnoseSelfAssignment - Emits a warning if a value is assigned to itself. |
| 8173 | /// This warning is only emitted for builtin assignment operations. It is also |
| 8174 | /// suppressed in the event of macro expansions. |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 8175 | static void DiagnoseSelfAssignment(Sema &S, Expr *LHSExpr, Expr *RHSExpr, |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 8176 | SourceLocation OpLoc) { |
| 8177 | if (!S.ActiveTemplateInstantiations.empty()) |
| 8178 | return; |
| 8179 | if (OpLoc.isInvalid() || OpLoc.isMacroID()) |
| 8180 | return; |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 8181 | LHSExpr = LHSExpr->IgnoreParenImpCasts(); |
| 8182 | RHSExpr = RHSExpr->IgnoreParenImpCasts(); |
| 8183 | const DeclRefExpr *LHSDeclRef = dyn_cast<DeclRefExpr>(LHSExpr); |
| 8184 | const DeclRefExpr *RHSDeclRef = dyn_cast<DeclRefExpr>(RHSExpr); |
| 8185 | if (!LHSDeclRef || !RHSDeclRef || |
| 8186 | LHSDeclRef->getLocation().isMacroID() || |
| 8187 | RHSDeclRef->getLocation().isMacroID()) |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 8188 | return; |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 8189 | const ValueDecl *LHSDecl = |
| 8190 | cast<ValueDecl>(LHSDeclRef->getDecl()->getCanonicalDecl()); |
| 8191 | const ValueDecl *RHSDecl = |
| 8192 | cast<ValueDecl>(RHSDeclRef->getDecl()->getCanonicalDecl()); |
| 8193 | if (LHSDecl != RHSDecl) |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 8194 | return; |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 8195 | if (LHSDecl->getType().isVolatileQualified()) |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 8196 | return; |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 8197 | if (const ReferenceType *RefTy = LHSDecl->getType()->getAs<ReferenceType>()) |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 8198 | if (RefTy->getPointeeType().isVolatileQualified()) |
| 8199 | return; |
| 8200 | |
| 8201 | S.Diag(OpLoc, diag::warn_self_assignment) |
Richard Trieu | 268942b | 2011-09-07 01:33:52 +0000 | [diff] [blame] | 8202 | << LHSDeclRef->getType() |
| 8203 | << LHSExpr->getSourceRange() << RHSExpr->getSourceRange(); |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 8204 | } |
| 8205 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8206 | /// CreateBuiltinBinOp - Creates a new built-in binary operation with |
| 8207 | /// operator @p Opc at location @c TokLoc. This routine only supports |
| 8208 | /// built-in operations; ActOnBinOp handles overloaded operators. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8209 | ExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc, |
Argyrios Kyrtzidis | b1fa3dc | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 8210 | BinaryOperatorKind Opc, |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8211 | Expr *LHSExpr, Expr *RHSExpr) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8212 | if (getLangOpts().CPlusPlus0x && isa<InitListExpr>(RHSExpr)) { |
Sebastian Redl | 0d8ab2e | 2012-02-27 20:34:02 +0000 | [diff] [blame] | 8213 | // The syntax only allows initializer lists on the RHS of assignment, |
| 8214 | // so we don't need to worry about accepting invalid code for |
| 8215 | // non-assignment operators. |
| 8216 | // C++11 5.17p9: |
| 8217 | // The meaning of x = {v} [...] is that of x = T(v) [...]. The meaning |
| 8218 | // of x = {} is x = T(). |
| 8219 | InitializationKind Kind = |
| 8220 | InitializationKind::CreateDirectList(RHSExpr->getLocStart()); |
| 8221 | InitializedEntity Entity = |
| 8222 | InitializedEntity::InitializeTemporary(LHSExpr->getType()); |
| 8223 | InitializationSequence InitSeq(*this, Entity, Kind, &RHSExpr, 1); |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 8224 | ExprResult Init = InitSeq.Perform(*this, Entity, Kind, RHSExpr); |
Sebastian Redl | 0d8ab2e | 2012-02-27 20:34:02 +0000 | [diff] [blame] | 8225 | if (Init.isInvalid()) |
| 8226 | return Init; |
| 8227 | RHSExpr = Init.take(); |
| 8228 | } |
| 8229 | |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8230 | ExprResult LHS = Owned(LHSExpr), RHS = Owned(RHSExpr); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8231 | QualType ResultTy; // Result type of the binary operator. |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8232 | // The following two variables are used for compound assignment operators |
| 8233 | QualType CompLHSTy; // Type of LHS after promotions for computation |
| 8234 | QualType CompResultTy; // Type of computation result |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8235 | ExprValueKind VK = VK_RValue; |
| 8236 | ExprObjectKind OK = OK_Ordinary; |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8237 | |
| 8238 | switch (Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8239 | case BO_Assign: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8240 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, QualType()); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8241 | if (getLangOpts().CPlusPlus && |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8242 | LHS.get()->getObjectKind() != OK_ObjCProperty) { |
| 8243 | VK = LHS.get()->getValueKind(); |
| 8244 | OK = LHS.get()->getObjectKind(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8245 | } |
Chandler Carruth | 9f7a6ee | 2011-01-04 06:52:15 +0000 | [diff] [blame] | 8246 | if (!ResultTy.isNull()) |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8247 | DiagnoseSelfAssignment(*this, LHS.get(), RHS.get(), OpLoc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8248 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8249 | case BO_PtrMemD: |
| 8250 | case BO_PtrMemI: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8251 | ResultTy = CheckPointerToMemberOperands(LHS, RHS, VK, OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8252 | Opc == BO_PtrMemI); |
Sebastian Redl | 2246050 | 2009-02-07 00:15:38 +0000 | [diff] [blame] | 8253 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8254 | case BO_Mul: |
| 8255 | case BO_Div: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8256 | ResultTy = CheckMultiplyDivideOperands(LHS, RHS, OpLoc, false, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8257 | Opc == BO_Div); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8258 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8259 | case BO_Rem: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8260 | ResultTy = CheckRemainderOperands(LHS, RHS, OpLoc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8261 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8262 | case BO_Add: |
Nico Weber | 1cb2d74 | 2012-03-02 22:01:22 +0000 | [diff] [blame] | 8263 | ResultTy = CheckAdditionOperands(LHS, RHS, OpLoc, Opc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8264 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8265 | case BO_Sub: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8266 | ResultTy = CheckSubtractionOperands(LHS, RHS, OpLoc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8267 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8268 | case BO_Shl: |
| 8269 | case BO_Shr: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8270 | ResultTy = CheckShiftOperands(LHS, RHS, OpLoc, Opc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8271 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8272 | case BO_LE: |
| 8273 | case BO_LT: |
| 8274 | case BO_GE: |
| 8275 | case BO_GT: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8276 | ResultTy = CheckCompareOperands(LHS, RHS, OpLoc, Opc, true); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8277 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8278 | case BO_EQ: |
| 8279 | case BO_NE: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8280 | ResultTy = CheckCompareOperands(LHS, RHS, OpLoc, Opc, false); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8281 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8282 | case BO_And: |
| 8283 | case BO_Xor: |
| 8284 | case BO_Or: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8285 | ResultTy = CheckBitwiseOperands(LHS, RHS, OpLoc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8286 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8287 | case BO_LAnd: |
| 8288 | case BO_LOr: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8289 | ResultTy = CheckLogicalOperands(LHS, RHS, OpLoc, Opc); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8290 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8291 | case BO_MulAssign: |
| 8292 | case BO_DivAssign: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8293 | CompResultTy = CheckMultiplyDivideOperands(LHS, RHS, OpLoc, true, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8294 | Opc == BO_DivAssign); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8295 | CompLHSTy = CompResultTy; |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8296 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) |
| 8297 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8298 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8299 | case BO_RemAssign: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8300 | CompResultTy = CheckRemainderOperands(LHS, RHS, OpLoc, true); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8301 | CompLHSTy = CompResultTy; |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8302 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) |
| 8303 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8304 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8305 | case BO_AddAssign: |
Nico Weber | 1cb2d74 | 2012-03-02 22:01:22 +0000 | [diff] [blame] | 8306 | CompResultTy = CheckAdditionOperands(LHS, RHS, OpLoc, Opc, &CompLHSTy); |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8307 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) |
| 8308 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8309 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8310 | case BO_SubAssign: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8311 | CompResultTy = CheckSubtractionOperands(LHS, RHS, OpLoc, &CompLHSTy); |
| 8312 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) |
| 8313 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8314 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8315 | case BO_ShlAssign: |
| 8316 | case BO_ShrAssign: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8317 | CompResultTy = CheckShiftOperands(LHS, RHS, OpLoc, Opc, true); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8318 | CompLHSTy = CompResultTy; |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8319 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) |
| 8320 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8321 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8322 | case BO_AndAssign: |
| 8323 | case BO_XorAssign: |
| 8324 | case BO_OrAssign: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8325 | CompResultTy = CheckBitwiseOperands(LHS, RHS, OpLoc, true); |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8326 | CompLHSTy = CompResultTy; |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8327 | if (!CompResultTy.isNull() && !LHS.isInvalid() && !RHS.isInvalid()) |
| 8328 | ResultTy = CheckAssignmentOperands(LHS.get(), RHS, OpLoc, CompResultTy); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8329 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8330 | case BO_Comma: |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8331 | ResultTy = CheckCommaOperands(*this, LHS, RHS, OpLoc); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8332 | if (getLangOpts().CPlusPlus && !RHS.isInvalid()) { |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8333 | VK = RHS.get()->getValueKind(); |
| 8334 | OK = RHS.get()->getObjectKind(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8335 | } |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8336 | break; |
| 8337 | } |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8338 | if (ResultTy.isNull() || LHS.isInvalid() || RHS.isInvalid()) |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 8339 | return ExprError(); |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8340 | |
| 8341 | // Check for array bounds violations for both sides of the BinaryOperator |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8342 | CheckArrayAccess(LHS.get()); |
| 8343 | CheckArrayAccess(RHS.get()); |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8344 | |
Eli Friedman | ab3a852 | 2009-03-28 01:22:36 +0000 | [diff] [blame] | 8345 | if (CompResultTy.isNull()) |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8346 | return Owned(new (Context) BinaryOperator(LHS.take(), RHS.take(), Opc, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8347 | ResultTy, VK, OK, OpLoc)); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8348 | if (getLangOpts().CPlusPlus && LHS.get()->getObjectKind() != |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 8349 | OK_ObjCProperty) { |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8350 | VK = VK_LValue; |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8351 | OK = LHS.get()->getObjectKind(); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8352 | } |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8353 | return Owned(new (Context) CompoundAssignOperator(LHS.take(), RHS.take(), Opc, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8354 | ResultTy, VK, OK, CompLHSTy, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8355 | CompResultTy, OpLoc)); |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8356 | } |
| 8357 | |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8358 | /// DiagnoseBitwisePrecedence - Emit a warning when bitwise and comparison |
| 8359 | /// operators are mixed in a way that suggests that the programmer forgot that |
| 8360 | /// comparison operators have higher precedence. The most typical example of |
| 8361 | /// such code is "flags & 0x0020 != 0", which is equivalent to "flags & 1". |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8362 | static void DiagnoseBitwisePrecedence(Sema &Self, BinaryOperatorKind Opc, |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8363 | SourceLocation OpLoc, Expr *LHSExpr, |
| 8364 | Expr *RHSExpr) { |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8365 | typedef BinaryOperator BinOp; |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8366 | BinOp::Opcode LHSopc = static_cast<BinOp::Opcode>(-1), |
| 8367 | RHSopc = static_cast<BinOp::Opcode>(-1); |
| 8368 | if (BinOp *BO = dyn_cast<BinOp>(LHSExpr)) |
| 8369 | LHSopc = BO->getOpcode(); |
| 8370 | if (BinOp *BO = dyn_cast<BinOp>(RHSExpr)) |
| 8371 | RHSopc = BO->getOpcode(); |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8372 | |
| 8373 | // Subs are not binary operators. |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8374 | if (LHSopc == -1 && RHSopc == -1) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8375 | return; |
| 8376 | |
| 8377 | // Bitwise operations are sometimes used as eager logical ops. |
| 8378 | // Don't diagnose this. |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8379 | if ((BinOp::isComparisonOp(LHSopc) || BinOp::isBitwiseOp(LHSopc)) && |
| 8380 | (BinOp::isComparisonOp(RHSopc) || BinOp::isBitwiseOp(RHSopc))) |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8381 | return; |
| 8382 | |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8383 | bool isLeftComp = BinOp::isComparisonOp(LHSopc); |
| 8384 | bool isRightComp = BinOp::isComparisonOp(RHSopc); |
Richard Trieu | 70979d4 | 2011-08-10 22:41:34 +0000 | [diff] [blame] | 8385 | if (!isLeftComp && !isRightComp) return; |
| 8386 | |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8387 | SourceRange DiagRange = isLeftComp ? SourceRange(LHSExpr->getLocStart(), |
| 8388 | OpLoc) |
| 8389 | : SourceRange(OpLoc, RHSExpr->getLocEnd()); |
| 8390 | std::string OpStr = isLeftComp ? BinOp::getOpcodeStr(LHSopc) |
| 8391 | : BinOp::getOpcodeStr(RHSopc); |
Richard Trieu | 70979d4 | 2011-08-10 22:41:34 +0000 | [diff] [blame] | 8392 | SourceRange ParensRange = isLeftComp ? |
Richard Trieu | 78ea78b | 2011-09-07 01:49:20 +0000 | [diff] [blame] | 8393 | SourceRange(cast<BinOp>(LHSExpr)->getRHS()->getLocStart(), |
| 8394 | RHSExpr->getLocEnd()) |
| 8395 | : SourceRange(LHSExpr->getLocStart(), |
| 8396 | cast<BinOp>(RHSExpr)->getLHS()->getLocStart()); |
Richard Trieu | 70979d4 | 2011-08-10 22:41:34 +0000 | [diff] [blame] | 8397 | |
| 8398 | Self.Diag(OpLoc, diag::warn_precedence_bitwise_rel) |
| 8399 | << DiagRange << BinOp::getOpcodeStr(Opc) << OpStr; |
| 8400 | SuggestParentheses(Self, OpLoc, |
| 8401 | Self.PDiag(diag::note_precedence_bitwise_silence) << OpStr, |
Nico Weber | 40e2999 | 2012-06-03 07:07:00 +0000 | [diff] [blame] | 8402 | (isLeftComp ? LHSExpr : RHSExpr)->getSourceRange()); |
Richard Trieu | 70979d4 | 2011-08-10 22:41:34 +0000 | [diff] [blame] | 8403 | SuggestParentheses(Self, OpLoc, |
| 8404 | Self.PDiag(diag::note_precedence_bitwise_first) << BinOp::getOpcodeStr(Opc), |
| 8405 | ParensRange); |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8406 | } |
| 8407 | |
Argyrios Kyrtzidis | 33f46e2 | 2011-06-20 18:41:26 +0000 | [diff] [blame] | 8408 | /// \brief It accepts a '&' expr that is inside a '|' one. |
| 8409 | /// Emit a diagnostic together with a fixit hint that wraps the '&' expression |
| 8410 | /// in parentheses. |
| 8411 | static void |
| 8412 | EmitDiagnosticForBitwiseAndInBitwiseOr(Sema &Self, SourceLocation OpLoc, |
| 8413 | BinaryOperator *Bop) { |
| 8414 | assert(Bop->getOpcode() == BO_And); |
| 8415 | Self.Diag(Bop->getOperatorLoc(), diag::warn_bitwise_and_in_bitwise_or) |
| 8416 | << Bop->getSourceRange() << OpLoc; |
| 8417 | SuggestParentheses(Self, Bop->getOperatorLoc(), |
| 8418 | Self.PDiag(diag::note_bitwise_and_in_bitwise_or_silence), |
| 8419 | Bop->getSourceRange()); |
| 8420 | } |
| 8421 | |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8422 | /// \brief It accepts a '&&' expr that is inside a '||' one. |
| 8423 | /// Emit a diagnostic together with a fixit hint that wraps the '&&' expression |
| 8424 | /// in parentheses. |
| 8425 | static void |
| 8426 | EmitDiagnosticForLogicalAndInLogicalOr(Sema &Self, SourceLocation OpLoc, |
Argyrios Kyrtzidis | a61aedc | 2011-04-22 19:16:27 +0000 | [diff] [blame] | 8427 | BinaryOperator *Bop) { |
| 8428 | assert(Bop->getOpcode() == BO_LAnd); |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 8429 | Self.Diag(Bop->getOperatorLoc(), diag::warn_logical_and_in_logical_or) |
| 8430 | << Bop->getSourceRange() << OpLoc; |
Argyrios Kyrtzidis | a61aedc | 2011-04-22 19:16:27 +0000 | [diff] [blame] | 8431 | SuggestParentheses(Self, Bop->getOperatorLoc(), |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8432 | Self.PDiag(diag::note_logical_and_in_logical_or_silence), |
Chandler Carruth | f0b60d6 | 2011-06-16 01:05:14 +0000 | [diff] [blame] | 8433 | Bop->getSourceRange()); |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8434 | } |
| 8435 | |
| 8436 | /// \brief Returns true if the given expression can be evaluated as a constant |
| 8437 | /// 'true'. |
| 8438 | static bool EvaluatesAsTrue(Sema &S, Expr *E) { |
| 8439 | bool Res; |
| 8440 | return E->EvaluateAsBooleanCondition(Res, S.getASTContext()) && Res; |
| 8441 | } |
| 8442 | |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8443 | /// \brief Returns true if the given expression can be evaluated as a constant |
| 8444 | /// 'false'. |
| 8445 | static bool EvaluatesAsFalse(Sema &S, Expr *E) { |
| 8446 | bool Res; |
| 8447 | return E->EvaluateAsBooleanCondition(Res, S.getASTContext()) && !Res; |
| 8448 | } |
| 8449 | |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8450 | /// \brief Look for '&&' in the left hand of a '||' expr. |
| 8451 | static void DiagnoseLogicalAndInLogicalOrLHS(Sema &S, SourceLocation OpLoc, |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8452 | Expr *LHSExpr, Expr *RHSExpr) { |
| 8453 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(LHSExpr)) { |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8454 | if (Bop->getOpcode() == BO_LAnd) { |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8455 | // If it's "a && b || 0" don't warn since the precedence doesn't matter. |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8456 | if (EvaluatesAsFalse(S, RHSExpr)) |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8457 | return; |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8458 | // If it's "1 && a || b" don't warn since the precedence doesn't matter. |
| 8459 | if (!EvaluatesAsTrue(S, Bop->getLHS())) |
| 8460 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, Bop); |
| 8461 | } else if (Bop->getOpcode() == BO_LOr) { |
| 8462 | if (BinaryOperator *RBop = dyn_cast<BinaryOperator>(Bop->getRHS())) { |
| 8463 | // If it's "a || b && 1 || c" we didn't warn earlier for |
| 8464 | // "a || b && 1", but warn now. |
| 8465 | if (RBop->getOpcode() == BO_LAnd && EvaluatesAsTrue(S, RBop->getRHS())) |
| 8466 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, RBop); |
| 8467 | } |
| 8468 | } |
| 8469 | } |
| 8470 | } |
| 8471 | |
| 8472 | /// \brief Look for '&&' in the right hand of a '||' expr. |
| 8473 | static void DiagnoseLogicalAndInLogicalOrRHS(Sema &S, SourceLocation OpLoc, |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8474 | Expr *LHSExpr, Expr *RHSExpr) { |
| 8475 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(RHSExpr)) { |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8476 | if (Bop->getOpcode() == BO_LAnd) { |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8477 | // If it's "0 || a && b" don't warn since the precedence doesn't matter. |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8478 | if (EvaluatesAsFalse(S, LHSExpr)) |
Argyrios Kyrtzidis | 47d512c | 2010-11-17 19:18:19 +0000 | [diff] [blame] | 8479 | return; |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8480 | // If it's "a || b && 1" don't warn since the precedence doesn't matter. |
| 8481 | if (!EvaluatesAsTrue(S, Bop->getRHS())) |
| 8482 | return EmitDiagnosticForLogicalAndInLogicalOr(S, OpLoc, Bop); |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8483 | } |
| 8484 | } |
| 8485 | } |
| 8486 | |
Argyrios Kyrtzidis | 33f46e2 | 2011-06-20 18:41:26 +0000 | [diff] [blame] | 8487 | /// \brief Look for '&' in the left or right hand of a '|' expr. |
| 8488 | static void DiagnoseBitwiseAndInBitwiseOr(Sema &S, SourceLocation OpLoc, |
| 8489 | Expr *OrArg) { |
| 8490 | if (BinaryOperator *Bop = dyn_cast<BinaryOperator>(OrArg)) { |
| 8491 | if (Bop->getOpcode() == BO_And) |
| 8492 | return EmitDiagnosticForBitwiseAndInBitwiseOr(S, OpLoc, Bop); |
| 8493 | } |
| 8494 | } |
| 8495 | |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8496 | /// DiagnoseBinOpPrecedence - Emit warnings for expressions with tricky |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8497 | /// precedence. |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8498 | static void DiagnoseBinOpPrecedence(Sema &Self, BinaryOperatorKind Opc, |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8499 | SourceLocation OpLoc, Expr *LHSExpr, |
| 8500 | Expr *RHSExpr){ |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8501 | // Diagnose "arg1 'bitwise' arg2 'eq' arg3". |
Sebastian Redl | aee3c93 | 2009-10-27 12:10:02 +0000 | [diff] [blame] | 8502 | if (BinaryOperator::isBitwiseOp(Opc)) |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8503 | DiagnoseBitwisePrecedence(Self, Opc, OpLoc, LHSExpr, RHSExpr); |
Argyrios Kyrtzidis | 33f46e2 | 2011-06-20 18:41:26 +0000 | [diff] [blame] | 8504 | |
| 8505 | // Diagnose "arg1 & arg2 | arg3" |
| 8506 | if (Opc == BO_Or && !OpLoc.isMacroID()/* Don't warn in macros. */) { |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8507 | DiagnoseBitwiseAndInBitwiseOr(Self, OpLoc, LHSExpr); |
| 8508 | DiagnoseBitwiseAndInBitwiseOr(Self, OpLoc, RHSExpr); |
Argyrios Kyrtzidis | 33f46e2 | 2011-06-20 18:41:26 +0000 | [diff] [blame] | 8509 | } |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8510 | |
Argyrios Kyrtzidis | 567bb71 | 2010-11-17 18:26:36 +0000 | [diff] [blame] | 8511 | // Warn about arg1 || arg2 && arg3, as GCC 4.3+ does. |
| 8512 | // 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] | 8513 | if (Opc == BO_LOr && !OpLoc.isMacroID()/* Don't warn in macros. */) { |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8514 | DiagnoseLogicalAndInLogicalOrLHS(Self, OpLoc, LHSExpr, RHSExpr); |
| 8515 | DiagnoseLogicalAndInLogicalOrRHS(Self, OpLoc, LHSExpr, RHSExpr); |
Argyrios Kyrtzidis | bee77f7 | 2010-11-16 21:00:12 +0000 | [diff] [blame] | 8516 | } |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8517 | } |
| 8518 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8519 | // Binary Operators. 'Tok' is the token for the operator. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8520 | ExprResult Sema::ActOnBinOp(Scope *S, SourceLocation TokLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8521 | tok::TokenKind Kind, |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8522 | Expr *LHSExpr, Expr *RHSExpr) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8523 | BinaryOperatorKind Opc = ConvertTokenKindToBinaryOpcode(Kind); |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8524 | assert((LHSExpr != 0) && "ActOnBinOp(): missing left expression"); |
| 8525 | assert((RHSExpr != 0) && "ActOnBinOp(): missing right expression"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8526 | |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8527 | // Emit warnings for tricky precedence issues, e.g. "bitfield & 0x4 == 0" |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8528 | DiagnoseBinOpPrecedence(*this, Opc, TokLoc, LHSExpr, RHSExpr); |
Sebastian Redl | 9e1d29b | 2009-10-26 15:24:15 +0000 | [diff] [blame] | 8529 | |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8530 | return BuildBinOp(S, TokLoc, Opc, LHSExpr, RHSExpr); |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8531 | } |
| 8532 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8533 | /// Build an overloaded binary operator expression in the given scope. |
| 8534 | static ExprResult BuildOverloadedBinOp(Sema &S, Scope *Sc, SourceLocation OpLoc, |
| 8535 | BinaryOperatorKind Opc, |
| 8536 | Expr *LHS, Expr *RHS) { |
| 8537 | // Find all of the overloaded operators visible from this |
| 8538 | // point. We perform both an operator-name lookup from the local |
| 8539 | // scope and an argument-dependent lookup based on the types of |
| 8540 | // the arguments. |
| 8541 | UnresolvedSet<16> Functions; |
| 8542 | OverloadedOperatorKind OverOp |
| 8543 | = BinaryOperator::getOverloadedOperator(Opc); |
| 8544 | if (Sc && OverOp != OO_None) |
| 8545 | S.LookupOverloadedOperatorName(OverOp, Sc, LHS->getType(), |
| 8546 | RHS->getType(), Functions); |
| 8547 | |
| 8548 | // Build the (potentially-overloaded, potentially-dependent) |
| 8549 | // binary operation. |
| 8550 | return S.CreateOverloadedBinOp(OpLoc, Opc, Functions, LHS, RHS); |
| 8551 | } |
| 8552 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8553 | ExprResult Sema::BuildBinOp(Scope *S, SourceLocation OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8554 | BinaryOperatorKind Opc, |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8555 | Expr *LHSExpr, Expr *RHSExpr) { |
John McCall | ac51650 | 2011-10-28 01:04:34 +0000 | [diff] [blame] | 8556 | // We want to end up calling one of checkPseudoObjectAssignment |
| 8557 | // (if the LHS is a pseudo-object), BuildOverloadedBinOp (if |
| 8558 | // both expressions are overloadable or either is type-dependent), |
| 8559 | // or CreateBuiltinBinOp (in any other case). We also want to get |
| 8560 | // any placeholder types out of the way. |
| 8561 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8562 | // Handle pseudo-objects in the LHS. |
| 8563 | if (const BuiltinType *pty = LHSExpr->getType()->getAsPlaceholderType()) { |
| 8564 | // Assignments with a pseudo-object l-value need special analysis. |
| 8565 | if (pty->getKind() == BuiltinType::PseudoObject && |
| 8566 | BinaryOperator::isAssignmentOp(Opc)) |
| 8567 | return checkPseudoObjectAssignment(S, OpLoc, Opc, LHSExpr, RHSExpr); |
| 8568 | |
| 8569 | // Don't resolve overloads if the other type is overloadable. |
| 8570 | if (pty->getKind() == BuiltinType::Overload) { |
| 8571 | // We can't actually test that if we still have a placeholder, |
| 8572 | // though. Fortunately, none of the exceptions we see in that |
John McCall | ac51650 | 2011-10-28 01:04:34 +0000 | [diff] [blame] | 8573 | // code below are valid when the LHS is an overload set. Note |
| 8574 | // that an overload set can be dependently-typed, but it never |
| 8575 | // instantiates to having an overloadable type. |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8576 | ExprResult resolvedRHS = CheckPlaceholderExpr(RHSExpr); |
| 8577 | if (resolvedRHS.isInvalid()) return ExprError(); |
| 8578 | RHSExpr = resolvedRHS.take(); |
| 8579 | |
John McCall | ac51650 | 2011-10-28 01:04:34 +0000 | [diff] [blame] | 8580 | if (RHSExpr->isTypeDependent() || |
| 8581 | RHSExpr->getType()->isOverloadableType()) |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8582 | return BuildOverloadedBinOp(*this, S, OpLoc, Opc, LHSExpr, RHSExpr); |
| 8583 | } |
| 8584 | |
| 8585 | ExprResult LHS = CheckPlaceholderExpr(LHSExpr); |
| 8586 | if (LHS.isInvalid()) return ExprError(); |
| 8587 | LHSExpr = LHS.take(); |
| 8588 | } |
| 8589 | |
| 8590 | // Handle pseudo-objects in the RHS. |
| 8591 | if (const BuiltinType *pty = RHSExpr->getType()->getAsPlaceholderType()) { |
| 8592 | // An overload in the RHS can potentially be resolved by the type |
| 8593 | // being assigned to. |
John McCall | ac51650 | 2011-10-28 01:04:34 +0000 | [diff] [blame] | 8594 | if (Opc == BO_Assign && pty->getKind() == BuiltinType::Overload) { |
| 8595 | if (LHSExpr->isTypeDependent() || RHSExpr->isTypeDependent()) |
| 8596 | return BuildOverloadedBinOp(*this, S, OpLoc, Opc, LHSExpr, RHSExpr); |
| 8597 | |
Eli Friedman | 8788491 | 2012-01-17 21:27:43 +0000 | [diff] [blame] | 8598 | if (LHSExpr->getType()->isOverloadableType()) |
| 8599 | return BuildOverloadedBinOp(*this, S, OpLoc, Opc, LHSExpr, RHSExpr); |
| 8600 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8601 | return CreateBuiltinBinOp(OpLoc, Opc, LHSExpr, RHSExpr); |
John McCall | ac51650 | 2011-10-28 01:04:34 +0000 | [diff] [blame] | 8602 | } |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8603 | |
| 8604 | // Don't resolve overloads if the other type is overloadable. |
| 8605 | if (pty->getKind() == BuiltinType::Overload && |
| 8606 | LHSExpr->getType()->isOverloadableType()) |
| 8607 | return BuildOverloadedBinOp(*this, S, OpLoc, Opc, LHSExpr, RHSExpr); |
| 8608 | |
| 8609 | ExprResult resolvedRHS = CheckPlaceholderExpr(RHSExpr); |
| 8610 | if (!resolvedRHS.isUsable()) return ExprError(); |
| 8611 | RHSExpr = resolvedRHS.take(); |
| 8612 | } |
| 8613 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8614 | if (getLangOpts().CPlusPlus) { |
John McCall | ac51650 | 2011-10-28 01:04:34 +0000 | [diff] [blame] | 8615 | // If either expression is type-dependent, always build an |
| 8616 | // overloaded op. |
| 8617 | if (LHSExpr->isTypeDependent() || RHSExpr->isTypeDependent()) |
| 8618 | return BuildOverloadedBinOp(*this, S, OpLoc, Opc, LHSExpr, RHSExpr); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8619 | |
John McCall | ac51650 | 2011-10-28 01:04:34 +0000 | [diff] [blame] | 8620 | // Otherwise, build an overloaded op if either expression has an |
| 8621 | // overloadable type. |
| 8622 | if (LHSExpr->getType()->isOverloadableType() || |
| 8623 | RHSExpr->getType()->isOverloadableType()) |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8624 | return BuildOverloadedBinOp(*this, S, OpLoc, Opc, LHSExpr, RHSExpr); |
Sebastian Redl | b8a6aca | 2009-01-19 22:31:54 +0000 | [diff] [blame] | 8625 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8626 | |
Douglas Gregor | eaebc75 | 2008-11-06 23:29:22 +0000 | [diff] [blame] | 8627 | // Build a built-in binary operation. |
Richard Trieu | befece1 | 2011-09-07 02:02:10 +0000 | [diff] [blame] | 8628 | return CreateBuiltinBinOp(OpLoc, Opc, LHSExpr, RHSExpr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8629 | } |
| 8630 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8631 | ExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc, |
Argyrios Kyrtzidis | b1fa3dc | 2011-01-05 20:09:36 +0000 | [diff] [blame] | 8632 | UnaryOperatorKind Opc, |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8633 | Expr *InputExpr) { |
| 8634 | ExprResult Input = Owned(InputExpr); |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8635 | ExprValueKind VK = VK_RValue; |
| 8636 | ExprObjectKind OK = OK_Ordinary; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8637 | QualType resultType; |
| 8638 | switch (Opc) { |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8639 | case UO_PreInc: |
| 8640 | case UO_PreDec: |
| 8641 | case UO_PostInc: |
| 8642 | case UO_PostDec: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8643 | resultType = CheckIncrementDecrementOperand(*this, Input.get(), VK, OpLoc, |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8644 | Opc == UO_PreInc || |
| 8645 | Opc == UO_PostInc, |
| 8646 | Opc == UO_PreInc || |
| 8647 | Opc == UO_PreDec); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8648 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8649 | case UO_AddrOf: |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8650 | resultType = CheckAddressOfOperand(*this, Input, OpLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8651 | break; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 8652 | case UO_Deref: { |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8653 | Input = DefaultFunctionArrayLvalueConversion(Input.take()); |
Eli Friedman | a6c66ce | 2012-08-31 00:14:07 +0000 | [diff] [blame^] | 8654 | if (Input.isInvalid()) return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8655 | resultType = CheckIndirectionOperand(*this, Input.get(), VK, OpLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8656 | break; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 8657 | } |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8658 | case UO_Plus: |
| 8659 | case UO_Minus: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8660 | Input = UsualUnaryConversions(Input.take()); |
| 8661 | if (Input.isInvalid()) return ExprError(); |
| 8662 | resultType = Input.get()->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8663 | if (resultType->isDependentType()) |
| 8664 | break; |
Douglas Gregor | 0061962 | 2010-06-22 23:41:02 +0000 | [diff] [blame] | 8665 | if (resultType->isArithmeticType() || // C99 6.5.3.3p1 |
| 8666 | resultType->isVectorType()) |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8667 | break; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8668 | else if (getLangOpts().CPlusPlus && // C++ [expr.unary.op]p6-7 |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8669 | resultType->isEnumeralType()) |
| 8670 | break; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8671 | else if (getLangOpts().CPlusPlus && // C++ [expr.unary.op]p6 |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8672 | Opc == UO_Plus && |
Douglas Gregor | 7425373 | 2008-11-19 15:42:04 +0000 | [diff] [blame] | 8673 | resultType->isPointerType()) |
| 8674 | break; |
| 8675 | |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8676 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8677 | << resultType << Input.get()->getSourceRange()); |
| 8678 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8679 | case UO_Not: // bitwise complement |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8680 | Input = UsualUnaryConversions(Input.take()); |
| 8681 | if (Input.isInvalid()) return ExprError(); |
| 8682 | resultType = Input.get()->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8683 | if (resultType->isDependentType()) |
| 8684 | break; |
Chris Lattner | 02a6514 | 2008-07-25 23:52:49 +0000 | [diff] [blame] | 8685 | // C99 6.5.3.3p1. We allow complex int and float as a GCC extension. |
| 8686 | if (resultType->isComplexType() || resultType->isComplexIntegerType()) |
| 8687 | // C99 does not support '~' for complex conjugation. |
Chris Lattner | d3a94e2 | 2008-11-20 06:06:08 +0000 | [diff] [blame] | 8688 | Diag(OpLoc, diag::ext_integer_complement_complex) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8689 | << resultType << Input.get()->getSourceRange(); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8690 | else if (resultType->hasIntegerRepresentation()) |
| 8691 | break; |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8692 | else { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8693 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8694 | << resultType << Input.get()->getSourceRange()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8695 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8696 | break; |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8697 | |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8698 | case UO_LNot: // logical negation |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8699 | // Unlike +/-/~, integer promotions aren't done here (C99 6.5.3.3p5). |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8700 | Input = DefaultFunctionArrayLvalueConversion(Input.take()); |
| 8701 | if (Input.isInvalid()) return ExprError(); |
| 8702 | resultType = Input.get()->getType(); |
Anton Korobeynikov | aa4a99b | 2011-10-14 23:23:15 +0000 | [diff] [blame] | 8703 | |
| 8704 | // Though we still have to promote half FP to float... |
| 8705 | if (resultType->isHalfType()) { |
| 8706 | Input = ImpCastExprToType(Input.take(), Context.FloatTy, CK_FloatingCast).take(); |
| 8707 | resultType = Context.FloatTy; |
| 8708 | } |
| 8709 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8710 | if (resultType->isDependentType()) |
| 8711 | break; |
Abramo Bagnara | 737d544 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 8712 | if (resultType->isScalarType()) { |
| 8713 | // C99 6.5.3.3p1: ok, fallthrough; |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8714 | if (Context.getLangOpts().CPlusPlus) { |
Abramo Bagnara | 737d544 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 8715 | // C++03 [expr.unary.op]p8, C++0x [expr.unary.op]p9: |
| 8716 | // operand contextually converted to bool. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8717 | Input = ImpCastExprToType(Input.take(), Context.BoolTy, |
| 8718 | ScalarTypeToBooleanCastKind(resultType)); |
Abramo Bagnara | 737d544 | 2011-04-07 09:26:19 +0000 | [diff] [blame] | 8719 | } |
Tanya Lattner | b0f9dd2 | 2012-01-19 01:16:16 +0000 | [diff] [blame] | 8720 | } else if (resultType->isExtVectorType()) { |
Tanya Lattner | 4f692c2 | 2012-01-16 21:02:28 +0000 | [diff] [blame] | 8721 | // Vector logical not returns the signed variant of the operand type. |
| 8722 | resultType = GetSignedVectorType(resultType); |
| 8723 | break; |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8724 | } else { |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8725 | return ExprError(Diag(OpLoc, diag::err_typecheck_unary_expr) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8726 | << resultType << Input.get()->getSourceRange()); |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8727 | } |
Douglas Gregor | ea844f3 | 2010-09-20 17:13:33 +0000 | [diff] [blame] | 8728 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8729 | // LNot always has type int. C99 6.5.3.3p5. |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8730 | // In C++, it's bool. C++ 5.3.1p8 |
Argyrios Kyrtzidis | 16f744b | 2011-02-18 20:55:15 +0000 | [diff] [blame] | 8731 | resultType = Context.getLogicalOperationType(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8732 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8733 | case UO_Real: |
| 8734 | case UO_Imag: |
John McCall | 0943168 | 2010-11-18 19:01:18 +0000 | [diff] [blame] | 8735 | resultType = CheckRealImagOperand(*this, Input, OpLoc, Opc == UO_Real); |
Richard Smith | dfb80de | 2012-02-18 20:53:32 +0000 | [diff] [blame] | 8736 | // _Real maps ordinary l-values into ordinary l-values. _Imag maps ordinary |
| 8737 | // complex l-values to ordinary l-values and all other values to r-values. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8738 | if (Input.isInvalid()) return ExprError(); |
Richard Smith | dfb80de | 2012-02-18 20:53:32 +0000 | [diff] [blame] | 8739 | if (Opc == UO_Real || Input.get()->getType()->isAnyComplexType()) { |
| 8740 | if (Input.get()->getValueKind() != VK_RValue && |
| 8741 | Input.get()->getObjectKind() == OK_Ordinary) |
| 8742 | VK = Input.get()->getValueKind(); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8743 | } else if (!getLangOpts().CPlusPlus) { |
Richard Smith | dfb80de | 2012-02-18 20:53:32 +0000 | [diff] [blame] | 8744 | // In C, a volatile scalar is read by __imag. In C++, it is not. |
| 8745 | Input = DefaultLvalueConversion(Input.take()); |
| 8746 | } |
Chris Lattner | dbb3697 | 2007-08-24 21:16:53 +0000 | [diff] [blame] | 8747 | break; |
John McCall | 2de56d1 | 2010-08-25 11:45:40 +0000 | [diff] [blame] | 8748 | case UO_Extension: |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8749 | resultType = Input.get()->getType(); |
| 8750 | VK = Input.get()->getValueKind(); |
| 8751 | OK = Input.get()->getObjectKind(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8752 | break; |
| 8753 | } |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8754 | if (resultType.isNull() || Input.isInvalid()) |
Sebastian Redl | 0eb2330 | 2009-01-19 00:08:26 +0000 | [diff] [blame] | 8755 | return ExprError(); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8756 | |
Kaelyn Uhrain | d6c8865 | 2011-08-05 23:18:04 +0000 | [diff] [blame] | 8757 | // Check for array bounds violations in the operand of the UnaryOperator, |
| 8758 | // except for the '*' and '&' operators that have to be handled specially |
| 8759 | // by CheckArrayAccess (as there are special cases like &array[arraysize] |
| 8760 | // that are explicitly defined as valid by the standard). |
| 8761 | if (Opc != UO_AddrOf && Opc != UO_Deref) |
| 8762 | CheckArrayAccess(Input.get()); |
| 8763 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8764 | return Owned(new (Context) UnaryOperator(Input.take(), Opc, resultType, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 8765 | VK, OK, OpLoc)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8766 | } |
| 8767 | |
Douglas Gregor | d3d0853 | 2011-12-14 21:23:13 +0000 | [diff] [blame] | 8768 | /// \brief Determine whether the given expression is a qualified member |
| 8769 | /// access expression, of a form that could be turned into a pointer to member |
| 8770 | /// with the address-of operator. |
| 8771 | static bool isQualifiedMemberAccess(Expr *E) { |
| 8772 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(E)) { |
| 8773 | if (!DRE->getQualifier()) |
| 8774 | return false; |
| 8775 | |
| 8776 | ValueDecl *VD = DRE->getDecl(); |
| 8777 | if (!VD->isCXXClassMember()) |
| 8778 | return false; |
| 8779 | |
| 8780 | if (isa<FieldDecl>(VD) || isa<IndirectFieldDecl>(VD)) |
| 8781 | return true; |
| 8782 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(VD)) |
| 8783 | return Method->isInstance(); |
| 8784 | |
| 8785 | return false; |
| 8786 | } |
| 8787 | |
| 8788 | if (UnresolvedLookupExpr *ULE = dyn_cast<UnresolvedLookupExpr>(E)) { |
| 8789 | if (!ULE->getQualifier()) |
| 8790 | return false; |
| 8791 | |
| 8792 | for (UnresolvedLookupExpr::decls_iterator D = ULE->decls_begin(), |
| 8793 | DEnd = ULE->decls_end(); |
| 8794 | D != DEnd; ++D) { |
| 8795 | if (CXXMethodDecl *Method = dyn_cast<CXXMethodDecl>(*D)) { |
| 8796 | if (Method->isInstance()) |
| 8797 | return true; |
| 8798 | } else { |
| 8799 | // Overload set does not contain methods. |
| 8800 | break; |
| 8801 | } |
| 8802 | } |
| 8803 | |
| 8804 | return false; |
| 8805 | } |
| 8806 | |
| 8807 | return false; |
| 8808 | } |
| 8809 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8810 | ExprResult Sema::BuildUnaryOp(Scope *S, SourceLocation OpLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8811 | UnaryOperatorKind Opc, Expr *Input) { |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 8812 | // First things first: handle placeholders so that the |
| 8813 | // overloaded-operator check considers the right type. |
| 8814 | if (const BuiltinType *pty = Input->getType()->getAsPlaceholderType()) { |
| 8815 | // Increment and decrement of pseudo-object references. |
| 8816 | if (pty->getKind() == BuiltinType::PseudoObject && |
| 8817 | UnaryOperator::isIncrementDecrementOp(Opc)) |
| 8818 | return checkPseudoObjectIncDec(S, OpLoc, Opc, Input); |
| 8819 | |
| 8820 | // extension is always a builtin operator. |
| 8821 | if (Opc == UO_Extension) |
| 8822 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
| 8823 | |
| 8824 | // & gets special logic for several kinds of placeholder. |
| 8825 | // The builtin code knows what to do. |
| 8826 | if (Opc == UO_AddrOf && |
| 8827 | (pty->getKind() == BuiltinType::Overload || |
| 8828 | pty->getKind() == BuiltinType::UnknownAny || |
| 8829 | pty->getKind() == BuiltinType::BoundMember)) |
| 8830 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
| 8831 | |
| 8832 | // Anything else needs to be handled now. |
| 8833 | ExprResult Result = CheckPlaceholderExpr(Input); |
| 8834 | if (Result.isInvalid()) return ExprError(); |
| 8835 | Input = Result.take(); |
| 8836 | } |
| 8837 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 8838 | if (getLangOpts().CPlusPlus && Input->getType()->isOverloadableType() && |
Douglas Gregor | d3d0853 | 2011-12-14 21:23:13 +0000 | [diff] [blame] | 8839 | UnaryOperator::getOverloadedOperator(Opc) != OO_None && |
| 8840 | !(Opc == UO_AddrOf && isQualifiedMemberAccess(Input))) { |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8841 | // Find all of the overloaded operators visible from this |
| 8842 | // point. We perform both an operator-name lookup from the local |
| 8843 | // scope and an argument-dependent lookup based on the types of |
| 8844 | // the arguments. |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8845 | UnresolvedSet<16> Functions; |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8846 | OverloadedOperatorKind OverOp = UnaryOperator::getOverloadedOperator(Opc); |
John McCall | 6e26689 | 2010-01-26 03:27:55 +0000 | [diff] [blame] | 8847 | if (S && OverOp != OO_None) |
| 8848 | LookupOverloadedOperatorName(OverOp, S, Input->getType(), QualType(), |
| 8849 | Functions); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8850 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8851 | return CreateOverloadedUnaryOp(OpLoc, Opc, Functions, Input); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8852 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 8853 | |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8854 | return CreateBuiltinUnaryOp(OpLoc, Opc, Input); |
Douglas Gregor | bc736fc | 2009-03-13 23:49:33 +0000 | [diff] [blame] | 8855 | } |
| 8856 | |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8857 | // Unary Operators. 'Tok' is the token for the operator. |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8858 | ExprResult Sema::ActOnUnaryOp(Scope *S, SourceLocation OpLoc, |
John McCall | f4c7371 | 2011-01-19 06:33:43 +0000 | [diff] [blame] | 8859 | tok::TokenKind Op, Expr *Input) { |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8860 | return BuildUnaryOp(S, OpLoc, ConvertTokenKindToUnaryOpcode(Op), Input); |
Douglas Gregor | 6ca7cfb | 2009-11-05 00:51:44 +0000 | [diff] [blame] | 8861 | } |
| 8862 | |
Steve Naroff | 1b273c4 | 2007-09-16 14:56:35 +0000 | [diff] [blame] | 8863 | /// ActOnAddrLabel - Parse the GNU address of label extension: "&&foo". |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 8864 | ExprResult Sema::ActOnAddrLabel(SourceLocation OpLoc, SourceLocation LabLoc, |
Chris Lattner | 57ad378 | 2011-02-17 20:34:02 +0000 | [diff] [blame] | 8865 | LabelDecl *TheDecl) { |
Chris Lattner | ad8dcf4 | 2011-02-17 07:39:24 +0000 | [diff] [blame] | 8866 | TheDecl->setUsed(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8867 | // 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] | 8868 | return Owned(new (Context) AddrLabelExpr(OpLoc, LabLoc, TheDecl, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8869 | Context.getPointerType(Context.VoidTy))); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 8870 | } |
| 8871 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8872 | /// Given the last statement in a statement-expression, check whether |
| 8873 | /// the result is a producing expression (like a call to an |
| 8874 | /// ns_returns_retained function) and, if so, rebuild it to hoist the |
| 8875 | /// release out of the full-expression. Otherwise, return null. |
| 8876 | /// Cannot fail. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8877 | static Expr *maybeRebuildARCConsumingStmt(Stmt *Statement) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8878 | // Should always be wrapped with one of these. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 8879 | ExprWithCleanups *cleanups = dyn_cast<ExprWithCleanups>(Statement); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8880 | if (!cleanups) return 0; |
| 8881 | |
| 8882 | ImplicitCastExpr *cast = dyn_cast<ImplicitCastExpr>(cleanups->getSubExpr()); |
John McCall | 33e56f3 | 2011-09-10 06:18:15 +0000 | [diff] [blame] | 8883 | if (!cast || cast->getCastKind() != CK_ARCConsumeObject) |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8884 | return 0; |
| 8885 | |
| 8886 | // Splice out the cast. This shouldn't modify any interesting |
| 8887 | // features of the statement. |
| 8888 | Expr *producer = cast->getSubExpr(); |
| 8889 | assert(producer->getType() == cast->getType()); |
| 8890 | assert(producer->getValueKind() == cast->getValueKind()); |
| 8891 | cleanups->setSubExpr(producer); |
| 8892 | return cleanups; |
| 8893 | } |
| 8894 | |
John McCall | 73f428c | 2012-04-04 01:27:53 +0000 | [diff] [blame] | 8895 | void Sema::ActOnStartStmtExpr() { |
| 8896 | PushExpressionEvaluationContext(ExprEvalContexts.back().Context); |
| 8897 | } |
| 8898 | |
| 8899 | void Sema::ActOnStmtExprError() { |
John McCall | 7f39d51 | 2012-04-06 18:20:53 +0000 | [diff] [blame] | 8900 | // Note that function is also called by TreeTransform when leaving a |
| 8901 | // StmtExpr scope without rebuilding anything. |
| 8902 | |
John McCall | 73f428c | 2012-04-04 01:27:53 +0000 | [diff] [blame] | 8903 | DiscardCleanupsInEvaluationContext(); |
| 8904 | PopExpressionEvaluationContext(); |
| 8905 | } |
| 8906 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8907 | ExprResult |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 8908 | Sema::ActOnStmtExpr(SourceLocation LPLoc, Stmt *SubStmt, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8909 | SourceLocation RPLoc) { // "({..})" |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8910 | assert(SubStmt && isa<CompoundStmt>(SubStmt) && "Invalid action invocation!"); |
| 8911 | CompoundStmt *Compound = cast<CompoundStmt>(SubStmt); |
| 8912 | |
John McCall | 73f428c | 2012-04-04 01:27:53 +0000 | [diff] [blame] | 8913 | if (hasAnyUnrecoverableErrorsInThisFunction()) |
| 8914 | DiscardCleanupsInEvaluationContext(); |
| 8915 | assert(!ExprNeedsCleanups && "cleanups within StmtExpr not correctly bound!"); |
| 8916 | PopExpressionEvaluationContext(); |
| 8917 | |
Douglas Gregor | dd8f569 | 2010-03-10 04:54:39 +0000 | [diff] [blame] | 8918 | bool isFileScope |
| 8919 | = (getCurFunctionOrMethodDecl() == 0) && (getCurBlock() == 0); |
Chris Lattner | 4a049f0 | 2009-04-25 19:11:05 +0000 | [diff] [blame] | 8920 | if (isFileScope) |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 8921 | return ExprError(Diag(LPLoc, diag::err_stmtexpr_file_scope)); |
Eli Friedman | dca2b73 | 2009-01-24 23:09:00 +0000 | [diff] [blame] | 8922 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8923 | // FIXME: there are a variety of strange constraints to enforce here, for |
| 8924 | // example, it is not possible to goto into a stmt expression apparently. |
| 8925 | // More semantic analysis is needed. |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8926 | |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8927 | // If there are sub stmts in the compound stmt, take the type of the last one |
| 8928 | // as the type of the stmtexpr. |
| 8929 | QualType Ty = Context.VoidTy; |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8930 | bool StmtExprMayBindToTemp = false; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8931 | if (!Compound->body_empty()) { |
| 8932 | Stmt *LastStmt = Compound->body_back(); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8933 | LabelStmt *LastLabelStmt = 0; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8934 | // If LastStmt is a label, skip down through into the body. |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8935 | while (LabelStmt *Label = dyn_cast<LabelStmt>(LastStmt)) { |
| 8936 | LastLabelStmt = Label; |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8937 | LastStmt = Label->getSubStmt(); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8938 | } |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8939 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8940 | if (Expr *LastE = dyn_cast<Expr>(LastStmt)) { |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8941 | // Do function/array conversion on the last expression, but not |
| 8942 | // lvalue-to-rvalue. However, initialize an unqualified type. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8943 | ExprResult LastExpr = DefaultFunctionArrayConversion(LastE); |
| 8944 | if (LastExpr.isInvalid()) |
| 8945 | return ExprError(); |
| 8946 | Ty = LastExpr.get()->getType().getUnqualifiedType(); |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 8947 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8948 | if (!Ty->isDependentType() && !LastExpr.get()->isTypeDependent()) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8949 | // In ARC, if the final expression ends in a consume, splice |
| 8950 | // the consume out and bind it later. In the alternate case |
| 8951 | // (when dealing with a retainable type), the result |
| 8952 | // initialization will create a produce. In both cases the |
| 8953 | // result will be +1, and we'll need to balance that out with |
| 8954 | // a bind. |
| 8955 | if (Expr *rebuiltLastStmt |
| 8956 | = maybeRebuildARCConsumingStmt(LastExpr.get())) { |
| 8957 | LastExpr = rebuiltLastStmt; |
| 8958 | } else { |
| 8959 | LastExpr = PerformCopyInitialization( |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8960 | InitializedEntity::InitializeResult(LPLoc, |
| 8961 | Ty, |
| 8962 | false), |
| 8963 | SourceLocation(), |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 8964 | LastExpr); |
| 8965 | } |
| 8966 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8967 | if (LastExpr.isInvalid()) |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8968 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8969 | if (LastExpr.get() != 0) { |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8970 | if (!LastLabelStmt) |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8971 | Compound->setLastStmt(LastExpr.take()); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8972 | else |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 8973 | LastLabelStmt->setSubStmt(LastExpr.take()); |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8974 | StmtExprMayBindToTemp = true; |
| 8975 | } |
| 8976 | } |
| 8977 | } |
Chris Lattner | 611b2ec | 2008-07-26 19:51:01 +0000 | [diff] [blame] | 8978 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 8979 | |
Eli Friedman | b1d796d | 2009-03-23 00:24:07 +0000 | [diff] [blame] | 8980 | // FIXME: Check that expression type is complete/non-abstract; statement |
| 8981 | // expressions are not lvalues. |
Fariborz Jahanian | e946fc8 | 2010-10-25 23:27:26 +0000 | [diff] [blame] | 8982 | Expr *ResStmtExpr = new (Context) StmtExpr(Compound, Ty, LPLoc, RPLoc); |
| 8983 | if (StmtExprMayBindToTemp) |
| 8984 | return MaybeBindToTemporary(ResStmtExpr); |
| 8985 | return Owned(ResStmtExpr); |
Chris Lattner | ab18c4c | 2007-07-24 16:58:17 +0000 | [diff] [blame] | 8986 | } |
Steve Naroff | d34e915 | 2007-08-01 22:05:33 +0000 | [diff] [blame] | 8987 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 8988 | ExprResult Sema::BuildBuiltinOffsetOf(SourceLocation BuiltinLoc, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 8989 | TypeSourceInfo *TInfo, |
| 8990 | OffsetOfComponent *CompPtr, |
| 8991 | unsigned NumComponents, |
| 8992 | SourceLocation RParenLoc) { |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8993 | QualType ArgTy = TInfo->getType(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 8994 | bool Dependent = ArgTy->isDependentType(); |
Abramo Bagnara | bd054db | 2010-05-20 10:00:11 +0000 | [diff] [blame] | 8995 | SourceRange TypeRange = TInfo->getTypeLoc().getLocalSourceRange(); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 8996 | |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 8997 | // We must have at least one component that refers to the type, and the first |
| 8998 | // one is known to be a field designator. Verify that the ArgTy represents |
| 8999 | // a struct/union/class. |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9000 | if (!Dependent && !ArgTy->isRecordType()) |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9001 | return ExprError(Diag(BuiltinLoc, diag::err_offsetof_record_type) |
| 9002 | << ArgTy << TypeRange); |
| 9003 | |
| 9004 | // Type must be complete per C99 7.17p3 because a declaring a variable |
| 9005 | // with an incomplete type would be ill-formed. |
| 9006 | if (!Dependent |
| 9007 | && RequireCompleteType(BuiltinLoc, ArgTy, |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 9008 | diag::err_offsetof_incomplete_type, TypeRange)) |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9009 | return ExprError(); |
| 9010 | |
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 9011 | // offsetof with non-identifier designators (e.g. "offsetof(x, a.b[c])") are a |
| 9012 | // GCC extension, diagnose them. |
Eli Friedman | 35183ac | 2009-02-27 06:44:11 +0000 | [diff] [blame] | 9013 | // FIXME: This diagnostic isn't actually visible because the location is in |
| 9014 | // a system header! |
Chris Lattner | 9e2b75c | 2007-08-31 21:49:13 +0000 | [diff] [blame] | 9015 | if (NumComponents != 1) |
Chris Lattner | dcd5ef1 | 2008-11-19 05:27:50 +0000 | [diff] [blame] | 9016 | Diag(BuiltinLoc, diag::ext_offsetof_extended_field_designator) |
| 9017 | << SourceRange(CompPtr[1].LocStart, CompPtr[NumComponents-1].LocEnd); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9018 | |
| 9019 | bool DidWarnAboutNonPOD = false; |
| 9020 | QualType CurrentType = ArgTy; |
| 9021 | typedef OffsetOfExpr::OffsetOfNode OffsetOfNode; |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9022 | SmallVector<OffsetOfNode, 4> Comps; |
| 9023 | SmallVector<Expr*, 4> Exprs; |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9024 | for (unsigned i = 0; i != NumComponents; ++i) { |
| 9025 | const OffsetOfComponent &OC = CompPtr[i]; |
| 9026 | if (OC.isBrackets) { |
| 9027 | // Offset of an array sub-field. TODO: Should we allow vector elements? |
| 9028 | if (!CurrentType->isDependentType()) { |
| 9029 | const ArrayType *AT = Context.getAsArrayType(CurrentType); |
| 9030 | if(!AT) |
| 9031 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_array_type) |
| 9032 | << CurrentType); |
| 9033 | CurrentType = AT->getElementType(); |
| 9034 | } else |
| 9035 | CurrentType = Context.DependentTy; |
| 9036 | |
Richard Smith | ea01143 | 2011-10-17 23:29:39 +0000 | [diff] [blame] | 9037 | ExprResult IdxRval = DefaultLvalueConversion(static_cast<Expr*>(OC.U.E)); |
| 9038 | if (IdxRval.isInvalid()) |
| 9039 | return ExprError(); |
| 9040 | Expr *Idx = IdxRval.take(); |
| 9041 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9042 | // The expression must be an integral expression. |
| 9043 | // FIXME: An integral constant expression? |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9044 | if (!Idx->isTypeDependent() && !Idx->isValueDependent() && |
| 9045 | !Idx->getType()->isIntegerType()) |
| 9046 | return ExprError(Diag(Idx->getLocStart(), |
| 9047 | diag::err_typecheck_subscript_not_integer) |
| 9048 | << Idx->getSourceRange()); |
Richard Smith | d82e5d3 | 2011-10-17 05:48:07 +0000 | [diff] [blame] | 9049 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9050 | // Record this array index. |
| 9051 | Comps.push_back(OffsetOfNode(OC.LocStart, Exprs.size(), OC.LocEnd)); |
Richard Smith | ea01143 | 2011-10-17 23:29:39 +0000 | [diff] [blame] | 9052 | Exprs.push_back(Idx); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9053 | continue; |
| 9054 | } |
| 9055 | |
| 9056 | // Offset of a field. |
| 9057 | if (CurrentType->isDependentType()) { |
| 9058 | // We have the offset of a field, but we can't look into the dependent |
| 9059 | // type. Just record the identifier of the field. |
| 9060 | Comps.push_back(OffsetOfNode(OC.LocStart, OC.U.IdentInfo, OC.LocEnd)); |
| 9061 | CurrentType = Context.DependentTy; |
| 9062 | continue; |
| 9063 | } |
| 9064 | |
| 9065 | // We need to have a complete type to look into. |
| 9066 | if (RequireCompleteType(OC.LocStart, CurrentType, |
| 9067 | diag::err_offsetof_incomplete_type)) |
| 9068 | return ExprError(); |
| 9069 | |
| 9070 | // Look for the designated field. |
| 9071 | const RecordType *RC = CurrentType->getAs<RecordType>(); |
| 9072 | if (!RC) |
| 9073 | return ExprError(Diag(OC.LocEnd, diag::err_offsetof_record_type) |
| 9074 | << CurrentType); |
| 9075 | RecordDecl *RD = RC->getDecl(); |
| 9076 | |
| 9077 | // C++ [lib.support.types]p5: |
| 9078 | // The macro offsetof accepts a restricted set of type arguments in this |
| 9079 | // International Standard. type shall be a POD structure or a POD union |
| 9080 | // (clause 9). |
Benjamin Kramer | 98f71aa | 2012-04-28 11:14:51 +0000 | [diff] [blame] | 9081 | // C++11 [support.types]p4: |
| 9082 | // If type is not a standard-layout class (Clause 9), the results are |
| 9083 | // undefined. |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9084 | if (CXXRecordDecl *CRD = dyn_cast<CXXRecordDecl>(RD)) { |
Benjamin Kramer | 98f71aa | 2012-04-28 11:14:51 +0000 | [diff] [blame] | 9085 | bool IsSafe = LangOpts.CPlusPlus0x? CRD->isStandardLayout() : CRD->isPOD(); |
| 9086 | unsigned DiagID = |
| 9087 | LangOpts.CPlusPlus0x? diag::warn_offsetof_non_standardlayout_type |
| 9088 | : diag::warn_offsetof_non_pod_type; |
| 9089 | |
| 9090 | if (!IsSafe && !DidWarnAboutNonPOD && |
Ted Kremenek | 762696f | 2011-02-23 01:51:43 +0000 | [diff] [blame] | 9091 | DiagRuntimeBehavior(BuiltinLoc, 0, |
Benjamin Kramer | 98f71aa | 2012-04-28 11:14:51 +0000 | [diff] [blame] | 9092 | PDiag(DiagID) |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9093 | << SourceRange(CompPtr[0].LocStart, OC.LocEnd) |
| 9094 | << CurrentType)) |
| 9095 | DidWarnAboutNonPOD = true; |
| 9096 | } |
| 9097 | |
| 9098 | // Look for the field. |
| 9099 | LookupResult R(*this, OC.U.IdentInfo, OC.LocStart, LookupMemberName); |
| 9100 | LookupQualifiedName(R, RD); |
| 9101 | FieldDecl *MemberDecl = R.getAsSingle<FieldDecl>(); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 9102 | IndirectFieldDecl *IndirectMemberDecl = 0; |
| 9103 | if (!MemberDecl) { |
Benjamin Kramer | d981146 | 2010-11-21 14:11:41 +0000 | [diff] [blame] | 9104 | if ((IndirectMemberDecl = R.getAsSingle<IndirectFieldDecl>())) |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 9105 | MemberDecl = IndirectMemberDecl->getAnonField(); |
| 9106 | } |
| 9107 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9108 | if (!MemberDecl) |
| 9109 | return ExprError(Diag(BuiltinLoc, diag::err_no_member) |
| 9110 | << OC.U.IdentInfo << RD << SourceRange(OC.LocStart, |
| 9111 | OC.LocEnd)); |
| 9112 | |
Douglas Gregor | 9d5d60f | 2010-04-28 22:36:06 +0000 | [diff] [blame] | 9113 | // C99 7.17p3: |
| 9114 | // (If the specified member is a bit-field, the behavior is undefined.) |
| 9115 | // |
| 9116 | // We diagnose this as an error. |
Richard Smith | a6b8b2c | 2011-10-10 18:28:20 +0000 | [diff] [blame] | 9117 | if (MemberDecl->isBitField()) { |
Douglas Gregor | 9d5d60f | 2010-04-28 22:36:06 +0000 | [diff] [blame] | 9118 | Diag(OC.LocEnd, diag::err_offsetof_bitfield) |
| 9119 | << MemberDecl->getDeclName() |
| 9120 | << SourceRange(BuiltinLoc, RParenLoc); |
| 9121 | Diag(MemberDecl->getLocation(), diag::note_bitfield_decl); |
| 9122 | return ExprError(); |
| 9123 | } |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 9124 | |
| 9125 | RecordDecl *Parent = MemberDecl->getParent(); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 9126 | if (IndirectMemberDecl) |
| 9127 | Parent = cast<RecordDecl>(IndirectMemberDecl->getDeclContext()); |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 9128 | |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 9129 | // If the member was found in a base class, introduce OffsetOfNodes for |
| 9130 | // the base class indirections. |
| 9131 | CXXBasePaths Paths(/*FindAmbiguities=*/true, /*RecordPaths=*/true, |
| 9132 | /*DetectVirtual=*/false); |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 9133 | if (IsDerivedFrom(CurrentType, Context.getTypeDeclType(Parent), Paths)) { |
Douglas Gregor | cc8a5d5 | 2010-04-29 00:18:15 +0000 | [diff] [blame] | 9134 | CXXBasePath &Path = Paths.front(); |
| 9135 | for (CXXBasePath::iterator B = Path.begin(), BEnd = Path.end(); |
| 9136 | B != BEnd; ++B) |
| 9137 | Comps.push_back(OffsetOfNode(B->Base)); |
| 9138 | } |
Eli Friedman | 19410a7 | 2010-08-05 10:11:36 +0000 | [diff] [blame] | 9139 | |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 9140 | if (IndirectMemberDecl) { |
| 9141 | for (IndirectFieldDecl::chain_iterator FI = |
| 9142 | IndirectMemberDecl->chain_begin(), |
| 9143 | FEnd = IndirectMemberDecl->chain_end(); FI != FEnd; FI++) { |
| 9144 | assert(isa<FieldDecl>(*FI)); |
| 9145 | Comps.push_back(OffsetOfNode(OC.LocStart, |
| 9146 | cast<FieldDecl>(*FI), OC.LocEnd)); |
| 9147 | } |
| 9148 | } else |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9149 | Comps.push_back(OffsetOfNode(OC.LocStart, MemberDecl, OC.LocEnd)); |
Francois Pichet | 87c2e12 | 2010-11-21 06:08:52 +0000 | [diff] [blame] | 9150 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9151 | CurrentType = MemberDecl->getType().getNonReferenceType(); |
| 9152 | } |
| 9153 | |
| 9154 | return Owned(OffsetOfExpr::Create(Context, Context.getSizeType(), BuiltinLoc, |
Benjamin Kramer | 3b6bef9 | 2012-08-24 11:54:20 +0000 | [diff] [blame] | 9155 | TInfo, Comps, Exprs, RParenLoc)); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9156 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9157 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9158 | ExprResult Sema::ActOnBuiltinOffsetOf(Scope *S, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9159 | SourceLocation BuiltinLoc, |
| 9160 | SourceLocation TypeLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9161 | ParsedType ParsedArgTy, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9162 | OffsetOfComponent *CompPtr, |
| 9163 | unsigned NumComponents, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9164 | SourceLocation RParenLoc) { |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9165 | |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9166 | TypeSourceInfo *ArgTInfo; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9167 | QualType ArgTy = GetTypeFromParser(ParsedArgTy, &ArgTInfo); |
Douglas Gregor | 8ecdb65 | 2010-04-28 22:16:22 +0000 | [diff] [blame] | 9168 | if (ArgTy.isNull()) |
| 9169 | return ExprError(); |
| 9170 | |
Eli Friedman | 5a15dc1 | 2010-08-05 10:15:45 +0000 | [diff] [blame] | 9171 | if (!ArgTInfo) |
| 9172 | ArgTInfo = Context.getTrivialTypeSourceInfo(ArgTy, TypeLoc); |
| 9173 | |
| 9174 | return BuildBuiltinOffsetOf(BuiltinLoc, ArgTInfo, CompPtr, NumComponents, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9175 | RParenLoc); |
Chris Lattner | 73d0d4f | 2007-08-30 17:45:32 +0000 | [diff] [blame] | 9176 | } |
| 9177 | |
| 9178 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9179 | ExprResult Sema::ActOnChooseExpr(SourceLocation BuiltinLoc, |
John McCall | 2cd11fe | 2010-10-12 02:09:17 +0000 | [diff] [blame] | 9180 | Expr *CondExpr, |
| 9181 | Expr *LHSExpr, Expr *RHSExpr, |
| 9182 | SourceLocation RPLoc) { |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 9183 | assert((CondExpr && LHSExpr && RHSExpr) && "Missing type argument(s)"); |
| 9184 | |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9185 | ExprValueKind VK = VK_RValue; |
| 9186 | ExprObjectKind OK = OK_Ordinary; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9187 | QualType resType; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 9188 | bool ValueDependent = false; |
Douglas Gregor | c9ecc57 | 2009-05-19 22:43:30 +0000 | [diff] [blame] | 9189 | if (CondExpr->isTypeDependent() || CondExpr->isValueDependent()) { |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9190 | resType = Context.DependentTy; |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 9191 | ValueDependent = true; |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9192 | } else { |
| 9193 | // The conditional expression is required to be a constant expression. |
| 9194 | llvm::APSInt condEval(32); |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 9195 | ExprResult CondICE |
| 9196 | = VerifyIntegerConstantExpression(CondExpr, &condEval, |
| 9197 | diag::err_typecheck_choose_expr_requires_constant, false); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 9198 | if (CondICE.isInvalid()) |
| 9199 | return ExprError(); |
| 9200 | CondExpr = CondICE.take(); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 9201 | |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9202 | // 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] | 9203 | Expr *ActiveExpr = condEval.getZExtValue() ? LHSExpr : RHSExpr; |
| 9204 | |
| 9205 | resType = ActiveExpr->getType(); |
| 9206 | ValueDependent = ActiveExpr->isValueDependent(); |
| 9207 | VK = ActiveExpr->getValueKind(); |
| 9208 | OK = ActiveExpr->getObjectKind(); |
Sebastian Redl | 2850784 | 2009-02-26 14:39:58 +0000 | [diff] [blame] | 9209 | } |
| 9210 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9211 | return Owned(new (Context) ChooseExpr(BuiltinLoc, CondExpr, LHSExpr, RHSExpr, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9212 | resType, VK, OK, RPLoc, |
Douglas Gregor | ce94049 | 2009-09-25 04:25:58 +0000 | [diff] [blame] | 9213 | resType->isDependentType(), |
| 9214 | ValueDependent)); |
Steve Naroff | d04fdd5 | 2007-08-03 21:21:27 +0000 | [diff] [blame] | 9215 | } |
| 9216 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9217 | //===----------------------------------------------------------------------===// |
| 9218 | // Clang Extensions. |
| 9219 | //===----------------------------------------------------------------------===// |
| 9220 | |
| 9221 | /// ActOnBlockStart - This callback is invoked when a block literal is started. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9222 | void Sema::ActOnBlockStart(SourceLocation CaretLoc, Scope *CurScope) { |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9223 | BlockDecl *Block = BlockDecl::Create(Context, CurContext, CaretLoc); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9224 | PushBlockScope(CurScope, Block); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9225 | CurContext->addDecl(Block); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9226 | if (CurScope) |
| 9227 | PushDeclContext(CurScope, Block); |
Fariborz Jahanian | a729da2 | 2010-07-09 18:44:02 +0000 | [diff] [blame] | 9228 | else |
| 9229 | CurContext = Block; |
John McCall | 538773c | 2011-11-11 03:19:12 +0000 | [diff] [blame] | 9230 | |
Eli Friedman | 84b007f | 2012-01-26 03:00:14 +0000 | [diff] [blame] | 9231 | getCurBlock()->HasImplicitReturnType = true; |
| 9232 | |
John McCall | 538773c | 2011-11-11 03:19:12 +0000 | [diff] [blame] | 9233 | // Enter a new evaluation context to insulate the block from any |
| 9234 | // cleanups from the enclosing full-expression. |
| 9235 | PushExpressionEvaluationContext(PotentiallyEvaluated); |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9236 | } |
| 9237 | |
Douglas Gregor | 03f1eb0 | 2012-06-15 16:59:29 +0000 | [diff] [blame] | 9238 | void Sema::ActOnBlockArguments(SourceLocation CaretLoc, Declarator &ParamInfo, |
| 9239 | Scope *CurScope) { |
Mike Stump | af199f3 | 2009-05-07 18:43:07 +0000 | [diff] [blame] | 9240 | assert(ParamInfo.getIdentifier()==0 && "block-id should have no identifier!"); |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9241 | assert(ParamInfo.getContext() == Declarator::BlockLiteralContext); |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9242 | BlockScopeInfo *CurBlock = getCurBlock(); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9243 | |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 9244 | TypeSourceInfo *Sig = GetTypeForDeclarator(ParamInfo, CurScope); |
John McCall | bf1a028 | 2010-06-04 23:28:52 +0000 | [diff] [blame] | 9245 | QualType T = Sig->getType(); |
Mike Stump | 98eb8a7 | 2009-02-04 22:31:32 +0000 | [diff] [blame] | 9246 | |
Douglas Gregor | 03f1eb0 | 2012-06-15 16:59:29 +0000 | [diff] [blame] | 9247 | // FIXME: We should allow unexpanded parameter packs here, but that would, |
| 9248 | // in turn, make the block expression contain unexpanded parameter packs. |
| 9249 | if (DiagnoseUnexpandedParameterPack(CaretLoc, Sig, UPPC_Block)) { |
| 9250 | // Drop the parameters. |
| 9251 | FunctionProtoType::ExtProtoInfo EPI; |
| 9252 | EPI.HasTrailingReturn = false; |
| 9253 | EPI.TypeQuals |= DeclSpec::TQ_const; |
| 9254 | T = Context.getFunctionType(Context.DependentTy, /*Args=*/0, /*NumArgs=*/0, |
| 9255 | EPI); |
| 9256 | Sig = Context.getTrivialTypeSourceInfo(T); |
| 9257 | } |
| 9258 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9259 | // GetTypeForDeclarator always produces a function type for a block |
| 9260 | // literal signature. Furthermore, it is always a FunctionProtoType |
| 9261 | // unless the function was written with a typedef. |
| 9262 | assert(T->isFunctionType() && |
| 9263 | "GetTypeForDeclarator made a non-function block signature"); |
| 9264 | |
| 9265 | // Look for an explicit signature in that function type. |
| 9266 | FunctionProtoTypeLoc ExplicitSignature; |
| 9267 | |
| 9268 | TypeLoc tmp = Sig->getTypeLoc().IgnoreParens(); |
| 9269 | if (isa<FunctionProtoTypeLoc>(tmp)) { |
| 9270 | ExplicitSignature = cast<FunctionProtoTypeLoc>(tmp); |
| 9271 | |
| 9272 | // Check whether that explicit signature was synthesized by |
| 9273 | // GetTypeForDeclarator. If so, don't save that as part of the |
| 9274 | // written signature. |
Abramo Bagnara | 796aa44 | 2011-03-12 11:17:06 +0000 | [diff] [blame] | 9275 | if (ExplicitSignature.getLocalRangeBegin() == |
| 9276 | ExplicitSignature.getLocalRangeEnd()) { |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9277 | // This would be much cheaper if we stored TypeLocs instead of |
| 9278 | // TypeSourceInfos. |
| 9279 | TypeLoc Result = ExplicitSignature.getResultLoc(); |
| 9280 | unsigned Size = Result.getFullDataSize(); |
| 9281 | Sig = Context.CreateTypeSourceInfo(Result.getType(), Size); |
| 9282 | Sig->getTypeLoc().initializeFullCopy(Result, Size); |
| 9283 | |
| 9284 | ExplicitSignature = FunctionProtoTypeLoc(); |
| 9285 | } |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9286 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9287 | |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9288 | CurBlock->TheDecl->setSignatureAsWritten(Sig); |
| 9289 | CurBlock->FunctionType = T; |
| 9290 | |
| 9291 | const FunctionType *Fn = T->getAs<FunctionType>(); |
| 9292 | QualType RetTy = Fn->getResultType(); |
| 9293 | bool isVariadic = |
| 9294 | (isa<FunctionProtoType>(Fn) && cast<FunctionProtoType>(Fn)->isVariadic()); |
| 9295 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9296 | CurBlock->TheDecl->setIsVariadic(isVariadic); |
Douglas Gregor | a873dfc | 2010-02-03 00:27:59 +0000 | [diff] [blame] | 9297 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9298 | // Don't allow returning a objc interface by value. |
| 9299 | if (RetTy->isObjCObjectType()) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 9300 | Diag(ParamInfo.getLocStart(), |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9301 | diag::err_object_cannot_be_passed_returned_by_value) << 0 << RetTy; |
| 9302 | return; |
| 9303 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9304 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9305 | // Context.DependentTy is used as a placeholder for a missing block |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9306 | // return type. TODO: what should we do with declarators like: |
| 9307 | // ^ * { ... } |
| 9308 | // If the answer is "apply template argument deduction".... |
Fariborz Jahanian | 0586520 | 2011-12-03 17:47:53 +0000 | [diff] [blame] | 9309 | if (RetTy != Context.DependentTy) { |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9310 | CurBlock->ReturnType = RetTy; |
Fariborz Jahanian | 0586520 | 2011-12-03 17:47:53 +0000 | [diff] [blame] | 9311 | CurBlock->TheDecl->setBlockMissingReturnType(false); |
Eli Friedman | 84b007f | 2012-01-26 03:00:14 +0000 | [diff] [blame] | 9312 | CurBlock->HasImplicitReturnType = false; |
Fariborz Jahanian | 0586520 | 2011-12-03 17:47:53 +0000 | [diff] [blame] | 9313 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9314 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9315 | // Push block parameters from the declarator if we had them. |
Chris Lattner | 5f9e272 | 2011-07-23 10:55:15 +0000 | [diff] [blame] | 9316 | SmallVector<ParmVarDecl*, 8> Params; |
John McCall | 711c52b | 2011-01-05 12:14:39 +0000 | [diff] [blame] | 9317 | if (ExplicitSignature) { |
| 9318 | for (unsigned I = 0, E = ExplicitSignature.getNumArgs(); I != E; ++I) { |
| 9319 | ParmVarDecl *Param = ExplicitSignature.getArg(I); |
Fariborz Jahanian | 9a66c30 | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 9320 | if (Param->getIdentifier() == 0 && |
| 9321 | !Param->isImplicit() && |
| 9322 | !Param->isInvalidDecl() && |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9323 | !getLangOpts().CPlusPlus) |
Fariborz Jahanian | 9a66c30 | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 9324 | Diag(Param->getLocation(), diag::err_parameter_name_omitted); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9325 | Params.push_back(Param); |
Fariborz Jahanian | 9a66c30 | 2010-02-12 21:53:14 +0000 | [diff] [blame] | 9326 | } |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9327 | |
| 9328 | // Fake up parameter variables if we have a typedef, like |
| 9329 | // ^ fntype { ... } |
| 9330 | } else if (const FunctionProtoType *Fn = T->getAs<FunctionProtoType>()) { |
| 9331 | for (FunctionProtoType::arg_type_iterator |
| 9332 | I = Fn->arg_type_begin(), E = Fn->arg_type_end(); I != E; ++I) { |
| 9333 | ParmVarDecl *Param = |
| 9334 | BuildParmVarDeclForTypedef(CurBlock->TheDecl, |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 9335 | ParamInfo.getLocStart(), |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9336 | *I); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9337 | Params.push_back(Param); |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9338 | } |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9339 | } |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9340 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9341 | // Set the parameters on the block decl. |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 9342 | if (!Params.empty()) { |
David Blaikie | 4278c65 | 2011-09-21 18:16:56 +0000 | [diff] [blame] | 9343 | CurBlock->TheDecl->setParams(Params); |
Douglas Gregor | 82aa713 | 2010-11-01 18:37:59 +0000 | [diff] [blame] | 9344 | CheckParmsForFunctionDef(CurBlock->TheDecl->param_begin(), |
| 9345 | CurBlock->TheDecl->param_end(), |
| 9346 | /*CheckParameterNames=*/false); |
| 9347 | } |
| 9348 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9349 | // Finally we can process decl attributes. |
Douglas Gregor | 9cdda0c | 2009-06-17 21:51:59 +0000 | [diff] [blame] | 9350 | ProcessDeclAttributes(CurScope, CurBlock->TheDecl, ParamInfo); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9351 | |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9352 | // Put the parameter variables in scope. We can bail out immediately |
| 9353 | // if we don't have any. |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9354 | if (Params.empty()) |
John McCall | 82dc009 | 2010-06-04 11:21:44 +0000 | [diff] [blame] | 9355 | return; |
| 9356 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9357 | for (BlockDecl::param_iterator AI = CurBlock->TheDecl->param_begin(), |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 9358 | E = CurBlock->TheDecl->param_end(); AI != E; ++AI) { |
| 9359 | (*AI)->setOwningFunction(CurBlock->TheDecl); |
| 9360 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9361 | // If this has an identifier, add it to the scope stack. |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9362 | if ((*AI)->getIdentifier()) { |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 9363 | CheckShadow(CurBlock->TheScope, *AI); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9364 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9365 | PushOnScopeChains(*AI, CurBlock->TheScope); |
John McCall | 053f4bd | 2010-03-22 09:20:08 +0000 | [diff] [blame] | 9366 | } |
John McCall | 7a9813c | 2010-01-22 00:28:27 +0000 | [diff] [blame] | 9367 | } |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9368 | } |
| 9369 | |
| 9370 | /// ActOnBlockError - If there is an error parsing a block, this callback |
| 9371 | /// is invoked to pop the information about the block from the action impl. |
| 9372 | void Sema::ActOnBlockError(SourceLocation CaretLoc, Scope *CurScope) { |
John McCall | 538773c | 2011-11-11 03:19:12 +0000 | [diff] [blame] | 9373 | // Leave the expression-evaluation context. |
| 9374 | DiscardCleanupsInEvaluationContext(); |
| 9375 | PopExpressionEvaluationContext(); |
| 9376 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9377 | // Pop off CurBlock, handle nested blocks. |
Chris Lattner | 5c59e2b | 2009-04-21 22:38:46 +0000 | [diff] [blame] | 9378 | PopDeclContext(); |
Eli Friedman | ec9ea72 | 2012-01-05 03:35:19 +0000 | [diff] [blame] | 9379 | PopFunctionScopeInfo(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9380 | } |
| 9381 | |
| 9382 | /// ActOnBlockStmtExpr - This is called when the body of a block statement |
| 9383 | /// literal was successfully completed. ^(int x){...} |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9384 | ExprResult Sema::ActOnBlockStmtExpr(SourceLocation CaretLoc, |
Chris Lattner | e476bdc | 2011-02-17 23:58:47 +0000 | [diff] [blame] | 9385 | Stmt *Body, Scope *CurScope) { |
Chris Lattner | 9af5500 | 2009-03-27 04:18:06 +0000 | [diff] [blame] | 9386 | // If blocks are disabled, emit an error. |
| 9387 | if (!LangOpts.Blocks) |
| 9388 | Diag(CaretLoc, diag::err_blocks_disable); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9389 | |
John McCall | 538773c | 2011-11-11 03:19:12 +0000 | [diff] [blame] | 9390 | // Leave the expression-evaluation context. |
John McCall | 1e5bc4f | 2012-03-08 22:00:17 +0000 | [diff] [blame] | 9391 | if (hasAnyUnrecoverableErrorsInThisFunction()) |
| 9392 | DiscardCleanupsInEvaluationContext(); |
John McCall | 538773c | 2011-11-11 03:19:12 +0000 | [diff] [blame] | 9393 | assert(!ExprNeedsCleanups && "cleanups within block not correctly bound!"); |
| 9394 | PopExpressionEvaluationContext(); |
| 9395 | |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9396 | BlockScopeInfo *BSI = cast<BlockScopeInfo>(FunctionScopes.back()); |
Jordan Rose | 7dd900e | 2012-07-02 21:19:23 +0000 | [diff] [blame] | 9397 | |
| 9398 | if (BSI->HasImplicitReturnType) |
| 9399 | deduceClosureReturnType(*BSI); |
| 9400 | |
Steve Naroff | 090276f | 2008-10-10 01:28:17 +0000 | [diff] [blame] | 9401 | PopDeclContext(); |
| 9402 | |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9403 | QualType RetTy = Context.VoidTy; |
Fariborz Jahanian | 7d5c74e | 2009-06-19 23:37:08 +0000 | [diff] [blame] | 9404 | if (!BSI->ReturnType.isNull()) |
| 9405 | RetTy = BSI->ReturnType; |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9406 | |
Mike Stump | 5692586 | 2009-07-28 22:04:01 +0000 | [diff] [blame] | 9407 | bool NoReturn = BSI->TheDecl->getAttr<NoReturnAttr>(); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9408 | QualType BlockTy; |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9409 | |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 9410 | // Set the captured variables on the block. |
Eli Friedman | b69b42c | 2012-01-11 02:36:31 +0000 | [diff] [blame] | 9411 | // FIXME: Share capture structure between BlockDecl and CapturingScopeInfo! |
| 9412 | SmallVector<BlockDecl::Capture, 4> Captures; |
| 9413 | for (unsigned i = 0, e = BSI->Captures.size(); i != e; i++) { |
| 9414 | CapturingScopeInfo::Capture &Cap = BSI->Captures[i]; |
| 9415 | if (Cap.isThisCapture()) |
| 9416 | continue; |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 9417 | BlockDecl::Capture NewCap(Cap.getVariable(), Cap.isBlockCapture(), |
Eli Friedman | b69b42c | 2012-01-11 02:36:31 +0000 | [diff] [blame] | 9418 | Cap.isNested(), Cap.getCopyExpr()); |
| 9419 | Captures.push_back(NewCap); |
| 9420 | } |
| 9421 | BSI->TheDecl->setCaptures(Context, Captures.begin(), Captures.end(), |
| 9422 | BSI->CXXThisCaptureIndex != 0); |
John McCall | 469a1eb | 2011-02-02 13:00:07 +0000 | [diff] [blame] | 9423 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9424 | // If the user wrote a function type in some form, try to use that. |
| 9425 | if (!BSI->FunctionType.isNull()) { |
| 9426 | const FunctionType *FTy = BSI->FunctionType->getAs<FunctionType>(); |
| 9427 | |
| 9428 | FunctionType::ExtInfo Ext = FTy->getExtInfo(); |
| 9429 | if (NoReturn && !Ext.getNoReturn()) Ext = Ext.withNoReturn(true); |
| 9430 | |
| 9431 | // Turn protoless block types into nullary block types. |
| 9432 | if (isa<FunctionNoProtoType>(FTy)) { |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9433 | FunctionProtoType::ExtProtoInfo EPI; |
| 9434 | EPI.ExtInfo = Ext; |
| 9435 | BlockTy = Context.getFunctionType(RetTy, 0, 0, EPI); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9436 | |
| 9437 | // Otherwise, if we don't need to change anything about the function type, |
| 9438 | // preserve its sugar structure. |
| 9439 | } else if (FTy->getResultType() == RetTy && |
| 9440 | (!NoReturn || FTy->getNoReturnAttr())) { |
| 9441 | BlockTy = BSI->FunctionType; |
| 9442 | |
| 9443 | // Otherwise, make the minimal modifications to the function type. |
| 9444 | } else { |
| 9445 | const FunctionProtoType *FPT = cast<FunctionProtoType>(FTy); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9446 | FunctionProtoType::ExtProtoInfo EPI = FPT->getExtProtoInfo(); |
| 9447 | EPI.TypeQuals = 0; // FIXME: silently? |
| 9448 | EPI.ExtInfo = Ext; |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9449 | BlockTy = Context.getFunctionType(RetTy, |
| 9450 | FPT->arg_type_begin(), |
| 9451 | FPT->getNumArgs(), |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9452 | EPI); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9453 | } |
| 9454 | |
| 9455 | // If we don't have a function type, just build one from nothing. |
| 9456 | } else { |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9457 | FunctionProtoType::ExtProtoInfo EPI; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9458 | EPI.ExtInfo = FunctionType::ExtInfo().withNoReturn(NoReturn); |
John McCall | e23cf43 | 2010-12-14 08:05:40 +0000 | [diff] [blame] | 9459 | BlockTy = Context.getFunctionType(RetTy, 0, 0, EPI); |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9460 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9461 | |
John McCall | c71a491 | 2010-06-04 19:02:56 +0000 | [diff] [blame] | 9462 | DiagnoseUnusedParameters(BSI->TheDecl->param_begin(), |
| 9463 | BSI->TheDecl->param_end()); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9464 | BlockTy = Context.getBlockPointerType(BlockTy); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9465 | |
Chris Lattner | 17a7830 | 2009-04-19 05:28:12 +0000 | [diff] [blame] | 9466 | // If needed, diagnose invalid gotos and switches in the block. |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9467 | if (getCurFunction()->NeedsScopeChecking() && |
Douglas Gregor | 27bec77 | 2012-08-17 05:12:08 +0000 | [diff] [blame] | 9468 | !hasAnyUnrecoverableErrorsInThisFunction() && |
| 9469 | !PP.isCodeCompletionEnabled()) |
John McCall | 9ae2f07 | 2010-08-23 23:25:46 +0000 | [diff] [blame] | 9470 | DiagnoseInvalidJumps(cast<CompoundStmt>(Body)); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9471 | |
Chris Lattner | e476bdc | 2011-02-17 23:58:47 +0000 | [diff] [blame] | 9472 | BSI->TheDecl->setBody(cast<CompoundStmt>(Body)); |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9473 | |
Jordan Rose | 7dd900e | 2012-07-02 21:19:23 +0000 | [diff] [blame] | 9474 | // Try to apply the named return value optimization. We have to check again |
| 9475 | // if we can do this, though, because blocks keep return statements around |
| 9476 | // to deduce an implicit return type. |
| 9477 | if (getLangOpts().CPlusPlus && RetTy->isRecordType() && |
| 9478 | !BSI->TheDecl->isDependentContext()) |
| 9479 | computeNRVO(Body, getCurBlock()); |
Douglas Gregor | f8b7f71 | 2011-09-06 20:46:03 +0000 | [diff] [blame] | 9480 | |
Benjamin Kramer | d248619 | 2011-07-12 14:11:05 +0000 | [diff] [blame] | 9481 | BlockExpr *Result = new (Context) BlockExpr(BSI->TheDecl, BlockTy); |
| 9482 | const AnalysisBasedWarnings::Policy &WP = AnalysisWarnings.getDefaultPolicy(); |
Eli Friedman | ec9ea72 | 2012-01-05 03:35:19 +0000 | [diff] [blame] | 9483 | PopFunctionScopeInfo(&WP, Result->getBlockDecl(), Result); |
Benjamin Kramer | d248619 | 2011-07-12 14:11:05 +0000 | [diff] [blame] | 9484 | |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 9485 | // If the block isn't obviously global, i.e. it captures anything at |
John McCall | 97b57a2 | 2012-04-13 01:08:17 +0000 | [diff] [blame] | 9486 | // all, then we need to do a few things in the surrounding context: |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 9487 | if (Result->getBlockDecl()->hasCaptures()) { |
John McCall | 97b57a2 | 2012-04-13 01:08:17 +0000 | [diff] [blame] | 9488 | // First, this expression has a new cleanup object. |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 9489 | ExprCleanupObjects.push_back(Result->getBlockDecl()); |
| 9490 | ExprNeedsCleanups = true; |
John McCall | 97b57a2 | 2012-04-13 01:08:17 +0000 | [diff] [blame] | 9491 | |
| 9492 | // It also gets a branch-protected scope if any of the captured |
| 9493 | // variables needs destruction. |
| 9494 | for (BlockDecl::capture_const_iterator |
| 9495 | ci = Result->getBlockDecl()->capture_begin(), |
| 9496 | ce = Result->getBlockDecl()->capture_end(); ci != ce; ++ci) { |
| 9497 | const VarDecl *var = ci->getVariable(); |
| 9498 | if (var->getType().isDestructedType() != QualType::DK_none) { |
| 9499 | getCurFunction()->setHasBranchProtectedScope(); |
| 9500 | break; |
| 9501 | } |
| 9502 | } |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 9503 | } |
Fariborz Jahanian | 27949f6 | 2012-03-06 18:41:35 +0000 | [diff] [blame] | 9504 | |
Douglas Gregor | 9ea9bdb | 2010-03-01 23:15:13 +0000 | [diff] [blame] | 9505 | return Owned(Result); |
Steve Naroff | 4eb206b | 2008-09-03 18:15:37 +0000 | [diff] [blame] | 9506 | } |
| 9507 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9508 | ExprResult Sema::ActOnVAArg(SourceLocation BuiltinLoc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9509 | Expr *E, ParsedType Ty, |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9510 | SourceLocation RPLoc) { |
Abramo Bagnara | 2cad900 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 9511 | TypeSourceInfo *TInfo; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 9512 | GetTypeFromParser(Ty, &TInfo); |
| 9513 | return BuildVAArgExpr(BuiltinLoc, E, TInfo, RPLoc); |
Abramo Bagnara | 2cad900 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 9514 | } |
| 9515 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9516 | ExprResult Sema::BuildVAArgExpr(SourceLocation BuiltinLoc, |
John McCall | f89e55a | 2010-11-18 06:31:45 +0000 | [diff] [blame] | 9517 | Expr *E, TypeSourceInfo *TInfo, |
| 9518 | SourceLocation RPLoc) { |
Chris Lattner | 0d20b8a | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 9519 | Expr *OrigExpr = E; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9520 | |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 9521 | // Get the va_list type |
| 9522 | QualType VaListType = Context.getBuiltinVaListType(); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 9523 | if (VaListType->isArrayType()) { |
| 9524 | // Deal with implicit array decay; for example, on x86-64, |
| 9525 | // va_list is an array, but it's supposed to decay to |
| 9526 | // a pointer for va_arg. |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 9527 | VaListType = Context.getArrayDecayedType(VaListType); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 9528 | // Make sure the input expression also decays appropriately. |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 9529 | ExprResult Result = UsualUnaryConversions(E); |
| 9530 | if (Result.isInvalid()) |
| 9531 | return ExprError(); |
| 9532 | E = Result.take(); |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 9533 | } else { |
| 9534 | // Otherwise, the va_list argument must be an l-value because |
| 9535 | // it is modified by va_arg. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 9536 | if (!E->isTypeDependent() && |
Douglas Gregor | dd02730 | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 9537 | CheckForModifiableLvalue(E, BuiltinLoc, *this)) |
Eli Friedman | 5c091ba | 2009-05-16 12:46:54 +0000 | [diff] [blame] | 9538 | return ExprError(); |
| 9539 | } |
Eli Friedman | c34bcde | 2008-08-09 23:32:40 +0000 | [diff] [blame] | 9540 | |
Douglas Gregor | dd02730 | 2009-05-19 23:10:31 +0000 | [diff] [blame] | 9541 | if (!E->isTypeDependent() && |
| 9542 | !Context.hasSameType(VaListType, E->getType())) { |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9543 | return ExprError(Diag(E->getLocStart(), |
| 9544 | diag::err_first_argument_to_va_arg_not_of_type_va_list) |
Chris Lattner | 0d20b8a | 2009-04-05 15:49:53 +0000 | [diff] [blame] | 9545 | << OrigExpr->getType() << E->getSourceRange()); |
Chris Lattner | 9dc8f19 | 2009-04-05 00:59:53 +0000 | [diff] [blame] | 9546 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9547 | |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 9548 | if (!TInfo->getType()->isDependentType()) { |
| 9549 | if (RequireCompleteType(TInfo->getTypeLoc().getBeginLoc(), TInfo->getType(), |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 9550 | diag::err_second_parameter_to_va_arg_incomplete, |
| 9551 | TInfo->getTypeLoc())) |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 9552 | return ExprError(); |
David Majnemer | db11b01 | 2011-06-13 06:37:03 +0000 | [diff] [blame] | 9553 | |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 9554 | if (RequireNonAbstractType(TInfo->getTypeLoc().getBeginLoc(), |
Douglas Gregor | 6a26e2e | 2012-05-04 17:09:59 +0000 | [diff] [blame] | 9555 | TInfo->getType(), |
| 9556 | diag::err_second_parameter_to_va_arg_abstract, |
| 9557 | TInfo->getTypeLoc())) |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 9558 | return ExprError(); |
| 9559 | |
Douglas Gregor | 4eb7522 | 2011-07-30 06:45:27 +0000 | [diff] [blame] | 9560 | if (!TInfo->getType().isPODType(Context)) { |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 9561 | Diag(TInfo->getTypeLoc().getBeginLoc(), |
Douglas Gregor | 4eb7522 | 2011-07-30 06:45:27 +0000 | [diff] [blame] | 9562 | TInfo->getType()->isObjCLifetimeType() |
| 9563 | ? diag::warn_second_parameter_to_va_arg_ownership_qualified |
| 9564 | : diag::warn_second_parameter_to_va_arg_not_pod) |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 9565 | << TInfo->getType() |
| 9566 | << TInfo->getTypeLoc().getSourceRange(); |
Douglas Gregor | 4eb7522 | 2011-07-30 06:45:27 +0000 | [diff] [blame] | 9567 | } |
Eli Friedman | 46d37c1 | 2011-07-11 21:45:59 +0000 | [diff] [blame] | 9568 | |
| 9569 | // Check for va_arg where arguments of the given type will be promoted |
| 9570 | // (i.e. this va_arg is guaranteed to have undefined behavior). |
| 9571 | QualType PromoteType; |
| 9572 | if (TInfo->getType()->isPromotableIntegerType()) { |
| 9573 | PromoteType = Context.getPromotedIntegerType(TInfo->getType()); |
| 9574 | if (Context.typesAreCompatible(PromoteType, TInfo->getType())) |
| 9575 | PromoteType = QualType(); |
| 9576 | } |
| 9577 | if (TInfo->getType()->isSpecificBuiltinType(BuiltinType::Float)) |
| 9578 | PromoteType = Context.DoubleTy; |
| 9579 | if (!PromoteType.isNull()) |
| 9580 | Diag(TInfo->getTypeLoc().getBeginLoc(), |
| 9581 | diag::warn_second_parameter_to_va_arg_never_compatible) |
| 9582 | << TInfo->getType() |
| 9583 | << PromoteType |
| 9584 | << TInfo->getTypeLoc().getSourceRange(); |
David Majnemer | 0adde12 | 2011-06-14 05:17:32 +0000 | [diff] [blame] | 9585 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9586 | |
Abramo Bagnara | 2cad900 | 2010-08-10 10:06:15 +0000 | [diff] [blame] | 9587 | QualType T = TInfo->getType().getNonLValueExprType(Context); |
| 9588 | return Owned(new (Context) VAArgExpr(BuiltinLoc, E, TInfo, RPLoc, T)); |
Anders Carlsson | 7c50aca | 2007-10-15 20:28:48 +0000 | [diff] [blame] | 9589 | } |
| 9590 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 9591 | ExprResult Sema::ActOnGNUNullExpr(SourceLocation TokenLoc) { |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 9592 | // The type of __null will be int or long, depending on the size of |
| 9593 | // pointers on the target. |
| 9594 | QualType Ty; |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 9595 | unsigned pw = Context.getTargetInfo().getPointerWidth(0); |
| 9596 | if (pw == Context.getTargetInfo().getIntWidth()) |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 9597 | Ty = Context.IntTy; |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 9598 | else if (pw == Context.getTargetInfo().getLongWidth()) |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 9599 | Ty = Context.LongTy; |
Douglas Gregor | bcfd1f5 | 2011-09-02 00:18:52 +0000 | [diff] [blame] | 9600 | else if (pw == Context.getTargetInfo().getLongLongWidth()) |
NAKAMURA Takumi | 6e5658d | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 9601 | Ty = Context.LongLongTy; |
| 9602 | else { |
David Blaikie | b219cfc | 2011-09-23 05:06:16 +0000 | [diff] [blame] | 9603 | llvm_unreachable("I don't know size of pointer!"); |
NAKAMURA Takumi | 6e5658d | 2011-01-19 00:11:41 +0000 | [diff] [blame] | 9604 | } |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 9605 | |
Sebastian Redl | f53597f | 2009-03-15 17:47:39 +0000 | [diff] [blame] | 9606 | return Owned(new (Context) GNUNullExpr(Ty, TokenLoc)); |
Douglas Gregor | 2d8b273 | 2008-11-29 04:51:27 +0000 | [diff] [blame] | 9607 | } |
| 9608 | |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9609 | static void MakeObjCStringLiteralFixItHint(Sema& SemaRef, QualType DstType, |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 9610 | Expr *SrcExpr, FixItHint &Hint) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9611 | if (!SemaRef.getLangOpts().ObjC1) |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 9612 | return; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9613 | |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 9614 | const ObjCObjectPointerType *PT = DstType->getAs<ObjCObjectPointerType>(); |
| 9615 | if (!PT) |
| 9616 | return; |
| 9617 | |
| 9618 | // Check if the destination is of type 'id'. |
| 9619 | if (!PT->isObjCIdType()) { |
| 9620 | // Check if the destination is the 'NSString' interface. |
| 9621 | const ObjCInterfaceDecl *ID = PT->getInterfaceDecl(); |
| 9622 | if (!ID || !ID->getIdentifier()->isStr("NSString")) |
| 9623 | return; |
| 9624 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9625 | |
John McCall | 4b9c2d2 | 2011-11-06 09:01:30 +0000 | [diff] [blame] | 9626 | // Ignore any parens, implicit casts (should only be |
| 9627 | // array-to-pointer decays), and not-so-opaque values. The last is |
| 9628 | // important for making this trigger for property assignments. |
| 9629 | SrcExpr = SrcExpr->IgnoreParenImpCasts(); |
| 9630 | if (OpaqueValueExpr *OV = dyn_cast<OpaqueValueExpr>(SrcExpr)) |
| 9631 | if (OV->getSourceExpr()) |
| 9632 | SrcExpr = OV->getSourceExpr()->IgnoreParenImpCasts(); |
| 9633 | |
| 9634 | StringLiteral *SL = dyn_cast<StringLiteral>(SrcExpr); |
Douglas Gregor | 5cee119 | 2011-07-27 05:40:30 +0000 | [diff] [blame] | 9635 | if (!SL || !SL->isAscii()) |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 9636 | return; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9637 | |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 9638 | Hint = FixItHint::CreateInsertion(SL->getLocStart(), "@"); |
Anders Carlsson | b76cd3d | 2009-11-10 04:46:30 +0000 | [diff] [blame] | 9639 | } |
| 9640 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9641 | bool Sema::DiagnoseAssignmentResult(AssignConvertType ConvTy, |
| 9642 | SourceLocation Loc, |
| 9643 | QualType DstType, QualType SrcType, |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 9644 | Expr *SrcExpr, AssignmentAction Action, |
| 9645 | bool *Complained) { |
| 9646 | if (Complained) |
| 9647 | *Complained = false; |
| 9648 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9649 | // Decode the result (notice that AST's are still created for extensions). |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 9650 | bool CheckInferredResultType = false; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9651 | bool isInvalid = false; |
Eli Friedman | fd81978 | 2012-02-29 20:59:56 +0000 | [diff] [blame] | 9652 | unsigned DiagKind = 0; |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 9653 | FixItHint Hint; |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 9654 | ConversionFixItGenerator ConvHints; |
| 9655 | bool MayHaveConvFixit = false; |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 9656 | bool MayHaveFunctionDiff = false; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 9657 | |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9658 | switch (ConvTy) { |
Fariborz Jahanian | 379b281 | 2012-07-17 18:00:08 +0000 | [diff] [blame] | 9659 | case Compatible: |
| 9660 | DiagnoseAssignmentEnum(DstType, SrcType, SrcExpr); |
| 9661 | return false; |
| 9662 | |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 9663 | case PointerToInt: |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9664 | DiagKind = diag::ext_typecheck_convert_pointer_int; |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 9665 | ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); |
| 9666 | MayHaveConvFixit = true; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9667 | break; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 9668 | case IntToPointer: |
| 9669 | DiagKind = diag::ext_typecheck_convert_int_pointer; |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 9670 | ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); |
| 9671 | MayHaveConvFixit = true; |
Chris Lattner | b7b6115 | 2008-01-04 18:22:42 +0000 | [diff] [blame] | 9672 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9673 | case IncompatiblePointer: |
Douglas Gregor | 849b243 | 2010-03-31 17:46:05 +0000 | [diff] [blame] | 9674 | MakeObjCStringLiteralFixItHint(*this, DstType, SrcExpr, Hint); |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9675 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer; |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 9676 | CheckInferredResultType = DstType->isObjCObjectPointerType() && |
| 9677 | SrcType->isObjCObjectPointerType(); |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 9678 | if (Hint.isNull() && !CheckInferredResultType) { |
| 9679 | ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); |
| 9680 | } |
| 9681 | MayHaveConvFixit = true; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9682 | break; |
Eli Friedman | f05c05d | 2009-03-22 23:59:44 +0000 | [diff] [blame] | 9683 | case IncompatiblePointerSign: |
| 9684 | DiagKind = diag::ext_typecheck_convert_incompatible_pointer_sign; |
| 9685 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9686 | case FunctionVoidPointer: |
| 9687 | DiagKind = diag::ext_typecheck_convert_pointer_void_func; |
| 9688 | break; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 9689 | case IncompatiblePointerDiscardsQualifiers: { |
John McCall | 40249e7 | 2011-02-01 23:28:01 +0000 | [diff] [blame] | 9690 | // Perform array-to-pointer decay if necessary. |
| 9691 | if (SrcType->isArrayType()) SrcType = Context.getArrayDecayedType(SrcType); |
| 9692 | |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 9693 | Qualifiers lhq = SrcType->getPointeeType().getQualifiers(); |
| 9694 | Qualifiers rhq = DstType->getPointeeType().getQualifiers(); |
| 9695 | if (lhq.getAddressSpace() != rhq.getAddressSpace()) { |
| 9696 | DiagKind = diag::err_typecheck_incompatible_address_space; |
| 9697 | break; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9698 | |
| 9699 | |
| 9700 | } else if (lhq.getObjCLifetime() != rhq.getObjCLifetime()) { |
Argyrios Kyrtzidis | b8b0313 | 2011-06-24 00:08:59 +0000 | [diff] [blame] | 9701 | DiagKind = diag::err_typecheck_incompatible_ownership; |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 9702 | break; |
John McCall | 86c05f3 | 2011-02-01 00:10:29 +0000 | [diff] [blame] | 9703 | } |
| 9704 | |
| 9705 | llvm_unreachable("unknown error case for discarding qualifiers!"); |
| 9706 | // fallthrough |
| 9707 | } |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9708 | case CompatiblePointerDiscardsQualifiers: |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 9709 | // If the qualifiers lost were because we were applying the |
| 9710 | // (deprecated) C++ conversion from a string literal to a char* |
| 9711 | // (or wchar_t*), then there was no error (C++ 4.2p2). FIXME: |
| 9712 | // Ideally, this check would be performed in |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 9713 | // checkPointerTypesForAssignment. However, that would require a |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 9714 | // bit of refactoring (so that the second argument is an |
| 9715 | // expression, rather than a type), which should be done as part |
John McCall | e4be87e | 2011-01-31 23:13:11 +0000 | [diff] [blame] | 9716 | // of a larger effort to fix checkPointerTypesForAssignment for |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 9717 | // C++ semantics. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9718 | if (getLangOpts().CPlusPlus && |
Douglas Gregor | 77a5223 | 2008-09-12 00:47:35 +0000 | [diff] [blame] | 9719 | IsStringLiteralToNonConstPointerConversion(SrcExpr, DstType)) |
| 9720 | return false; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9721 | DiagKind = diag::ext_typecheck_convert_discards_qualifiers; |
| 9722 | break; |
Sean Hunt | c9132b6 | 2009-11-08 07:46:34 +0000 | [diff] [blame] | 9723 | case IncompatibleNestedPointerQualifiers: |
Fariborz Jahanian | 3451e92 | 2009-11-09 22:16:37 +0000 | [diff] [blame] | 9724 | DiagKind = diag::ext_nested_pointer_qualifier_mismatch; |
Fariborz Jahanian | 36a862f | 2009-11-07 20:20:40 +0000 | [diff] [blame] | 9725 | break; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 9726 | case IntToBlockPointer: |
| 9727 | DiagKind = diag::err_int_to_block_pointer; |
| 9728 | break; |
| 9729 | case IncompatibleBlockPointer: |
Mike Stump | 25efa10 | 2009-04-21 22:51:42 +0000 | [diff] [blame] | 9730 | DiagKind = diag::err_typecheck_convert_incompatible_block_pointer; |
Steve Naroff | 1c7d067 | 2008-09-04 15:10:53 +0000 | [diff] [blame] | 9731 | break; |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 9732 | case IncompatibleObjCQualifiedId: |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9733 | // FIXME: Diagnose the problem in ObjCQualifiedIdTypesAreCompatible, since |
Steve Naroff | 3957907 | 2008-10-14 22:18:38 +0000 | [diff] [blame] | 9734 | // it can give a more specific diagnostic. |
| 9735 | DiagKind = diag::warn_incompatible_qualified_id; |
| 9736 | break; |
Anders Carlsson | b0f90cc | 2009-01-30 23:17:46 +0000 | [diff] [blame] | 9737 | case IncompatibleVectors: |
| 9738 | DiagKind = diag::warn_incompatible_vectors; |
| 9739 | break; |
Fariborz Jahanian | 04e5a25 | 2011-07-07 18:55:47 +0000 | [diff] [blame] | 9740 | case IncompatibleObjCWeakRef: |
| 9741 | DiagKind = diag::err_arc_weak_unavailable_assign; |
| 9742 | break; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9743 | case Incompatible: |
| 9744 | DiagKind = diag::err_typecheck_convert_incompatible; |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 9745 | ConvHints.tryToFixConversion(SrcExpr, SrcType, DstType, *this); |
| 9746 | MayHaveConvFixit = true; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9747 | isInvalid = true; |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 9748 | MayHaveFunctionDiff = true; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9749 | break; |
| 9750 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 9751 | |
Douglas Gregor | d4eea83 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 9752 | QualType FirstType, SecondType; |
| 9753 | switch (Action) { |
| 9754 | case AA_Assigning: |
| 9755 | case AA_Initializing: |
| 9756 | // The destination type comes first. |
| 9757 | FirstType = DstType; |
| 9758 | SecondType = SrcType; |
| 9759 | break; |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9760 | |
Douglas Gregor | d4eea83 | 2010-04-09 00:35:39 +0000 | [diff] [blame] | 9761 | case AA_Returning: |
| 9762 | case AA_Passing: |
| 9763 | case AA_Converting: |
| 9764 | case AA_Sending: |
| 9765 | case AA_Casting: |
| 9766 | // The source type comes first. |
| 9767 | FirstType = SrcType; |
| 9768 | SecondType = DstType; |
| 9769 | break; |
| 9770 | } |
Sean Hunt | 1e3f5ba | 2010-04-28 23:02:27 +0000 | [diff] [blame] | 9771 | |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 9772 | PartialDiagnostic FDiag = PDiag(DiagKind); |
| 9773 | FDiag << FirstType << SecondType << Action << SrcExpr->getSourceRange(); |
| 9774 | |
| 9775 | // If we can fix the conversion, suggest the FixIts. |
| 9776 | assert(ConvHints.isNull() || Hint.isNull()); |
| 9777 | if (!ConvHints.isNull()) { |
Benjamin Kramer | 1136ef0 | 2012-01-14 21:05:10 +0000 | [diff] [blame] | 9778 | for (std::vector<FixItHint>::iterator HI = ConvHints.Hints.begin(), |
| 9779 | HE = ConvHints.Hints.end(); HI != HE; ++HI) |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 9780 | FDiag << *HI; |
| 9781 | } else { |
| 9782 | FDiag << Hint; |
| 9783 | } |
| 9784 | if (MayHaveConvFixit) { FDiag << (unsigned) (ConvHints.Kind); } |
| 9785 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 9786 | if (MayHaveFunctionDiff) |
| 9787 | HandleFunctionTypeMismatch(FDiag, SecondType, FirstType); |
| 9788 | |
Anna Zaks | 6722155 | 2011-07-28 19:51:27 +0000 | [diff] [blame] | 9789 | Diag(Loc, FDiag); |
| 9790 | |
Richard Trieu | 6efd4c5 | 2011-11-23 22:32:32 +0000 | [diff] [blame] | 9791 | if (SecondType == Context.OverloadTy) |
| 9792 | NoteAllOverloadCandidates(OverloadExpr::find(SrcExpr).Expression, |
| 9793 | FirstType); |
| 9794 | |
Douglas Gregor | 926df6c | 2011-06-11 01:09:30 +0000 | [diff] [blame] | 9795 | if (CheckInferredResultType) |
| 9796 | EmitRelatedResultTypeNote(SrcExpr); |
| 9797 | |
Douglas Gregor | a41a8c5 | 2010-04-22 00:20:18 +0000 | [diff] [blame] | 9798 | if (Complained) |
| 9799 | *Complained = true; |
Chris Lattner | 5cf216b | 2008-01-04 18:04:52 +0000 | [diff] [blame] | 9800 | return isInvalid; |
| 9801 | } |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9802 | |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 9803 | ExprResult Sema::VerifyIntegerConstantExpression(Expr *E, |
| 9804 | llvm::APSInt *Result) { |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 9805 | class SimpleICEDiagnoser : public VerifyICEDiagnoser { |
| 9806 | public: |
| 9807 | virtual void diagnoseNotICE(Sema &S, SourceLocation Loc, SourceRange SR) { |
| 9808 | S.Diag(Loc, diag::err_expr_not_ice) << S.LangOpts.CPlusPlus << SR; |
| 9809 | } |
| 9810 | } Diagnoser; |
| 9811 | |
| 9812 | return VerifyIntegerConstantExpression(E, Result, Diagnoser); |
| 9813 | } |
| 9814 | |
| 9815 | ExprResult Sema::VerifyIntegerConstantExpression(Expr *E, |
| 9816 | llvm::APSInt *Result, |
| 9817 | unsigned DiagID, |
| 9818 | bool AllowFold) { |
| 9819 | class IDDiagnoser : public VerifyICEDiagnoser { |
| 9820 | unsigned DiagID; |
| 9821 | |
| 9822 | public: |
| 9823 | IDDiagnoser(unsigned DiagID) |
| 9824 | : VerifyICEDiagnoser(DiagID == 0), DiagID(DiagID) { } |
| 9825 | |
| 9826 | virtual void diagnoseNotICE(Sema &S, SourceLocation Loc, SourceRange SR) { |
| 9827 | S.Diag(Loc, DiagID) << SR; |
| 9828 | } |
| 9829 | } Diagnoser(DiagID); |
| 9830 | |
| 9831 | return VerifyIntegerConstantExpression(E, Result, Diagnoser, AllowFold); |
| 9832 | } |
| 9833 | |
| 9834 | void Sema::VerifyICEDiagnoser::diagnoseFold(Sema &S, SourceLocation Loc, |
| 9835 | SourceRange SR) { |
| 9836 | S.Diag(Loc, diag::ext_expr_not_ice) << SR << S.LangOpts.CPlusPlus; |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 9837 | } |
| 9838 | |
Benjamin Kramer | d448ce0 | 2012-04-18 14:22:41 +0000 | [diff] [blame] | 9839 | ExprResult |
| 9840 | Sema::VerifyIntegerConstantExpression(Expr *E, llvm::APSInt *Result, |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 9841 | VerifyICEDiagnoser &Diagnoser, |
| 9842 | bool AllowFold) { |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 9843 | SourceLocation DiagLoc = E->getLocStart(); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 9844 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9845 | if (getLangOpts().CPlusPlus0x) { |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 9846 | // C++11 [expr.const]p5: |
| 9847 | // If an expression of literal class type is used in a context where an |
| 9848 | // integral constant expression is required, then that class type shall |
| 9849 | // have a single non-explicit conversion function to an integral or |
| 9850 | // unscoped enumeration type |
| 9851 | ExprResult Converted; |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 9852 | if (!Diagnoser.Suppress) { |
| 9853 | class CXX11ConvertDiagnoser : public ICEConvertDiagnoser { |
| 9854 | public: |
| 9855 | CXX11ConvertDiagnoser() : ICEConvertDiagnoser(false, true) { } |
| 9856 | |
| 9857 | virtual DiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, |
| 9858 | QualType T) { |
| 9859 | return S.Diag(Loc, diag::err_ice_not_integral) << T; |
| 9860 | } |
| 9861 | |
| 9862 | virtual DiagnosticBuilder diagnoseIncomplete(Sema &S, |
| 9863 | SourceLocation Loc, |
| 9864 | QualType T) { |
| 9865 | return S.Diag(Loc, diag::err_ice_incomplete_type) << T; |
| 9866 | } |
| 9867 | |
| 9868 | virtual DiagnosticBuilder diagnoseExplicitConv(Sema &S, |
| 9869 | SourceLocation Loc, |
| 9870 | QualType T, |
| 9871 | QualType ConvTy) { |
| 9872 | return S.Diag(Loc, diag::err_ice_explicit_conversion) << T << ConvTy; |
| 9873 | } |
| 9874 | |
| 9875 | virtual DiagnosticBuilder noteExplicitConv(Sema &S, |
| 9876 | CXXConversionDecl *Conv, |
| 9877 | QualType ConvTy) { |
| 9878 | return S.Diag(Conv->getLocation(), diag::note_ice_conversion_here) |
| 9879 | << ConvTy->isEnumeralType() << ConvTy; |
| 9880 | } |
| 9881 | |
| 9882 | virtual DiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc, |
| 9883 | QualType T) { |
| 9884 | return S.Diag(Loc, diag::err_ice_ambiguous_conversion) << T; |
| 9885 | } |
| 9886 | |
| 9887 | virtual DiagnosticBuilder noteAmbiguous(Sema &S, |
| 9888 | CXXConversionDecl *Conv, |
| 9889 | QualType ConvTy) { |
| 9890 | return S.Diag(Conv->getLocation(), diag::note_ice_conversion_here) |
| 9891 | << ConvTy->isEnumeralType() << ConvTy; |
| 9892 | } |
| 9893 | |
| 9894 | virtual DiagnosticBuilder diagnoseConversion(Sema &S, |
| 9895 | SourceLocation Loc, |
| 9896 | QualType T, |
| 9897 | QualType ConvTy) { |
| 9898 | return DiagnosticBuilder::getEmpty(); |
| 9899 | } |
| 9900 | } ConvertDiagnoser; |
| 9901 | |
| 9902 | Converted = ConvertToIntegralOrEnumerationType(DiagLoc, E, |
| 9903 | ConvertDiagnoser, |
| 9904 | /*AllowScopedEnumerations*/ false); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 9905 | } else { |
| 9906 | // The caller wants to silently enquire whether this is an ICE. Don't |
| 9907 | // produce any diagnostics if it isn't. |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 9908 | class SilentICEConvertDiagnoser : public ICEConvertDiagnoser { |
| 9909 | public: |
| 9910 | SilentICEConvertDiagnoser() : ICEConvertDiagnoser(true, true) { } |
| 9911 | |
| 9912 | virtual DiagnosticBuilder diagnoseNotInt(Sema &S, SourceLocation Loc, |
| 9913 | QualType T) { |
| 9914 | return DiagnosticBuilder::getEmpty(); |
| 9915 | } |
| 9916 | |
| 9917 | virtual DiagnosticBuilder diagnoseIncomplete(Sema &S, |
| 9918 | SourceLocation Loc, |
| 9919 | QualType T) { |
| 9920 | return DiagnosticBuilder::getEmpty(); |
| 9921 | } |
| 9922 | |
| 9923 | virtual DiagnosticBuilder diagnoseExplicitConv(Sema &S, |
| 9924 | SourceLocation Loc, |
| 9925 | QualType T, |
| 9926 | QualType ConvTy) { |
| 9927 | return DiagnosticBuilder::getEmpty(); |
| 9928 | } |
| 9929 | |
| 9930 | virtual DiagnosticBuilder noteExplicitConv(Sema &S, |
| 9931 | CXXConversionDecl *Conv, |
| 9932 | QualType ConvTy) { |
| 9933 | return DiagnosticBuilder::getEmpty(); |
| 9934 | } |
| 9935 | |
| 9936 | virtual DiagnosticBuilder diagnoseAmbiguous(Sema &S, SourceLocation Loc, |
| 9937 | QualType T) { |
| 9938 | return DiagnosticBuilder::getEmpty(); |
| 9939 | } |
| 9940 | |
| 9941 | virtual DiagnosticBuilder noteAmbiguous(Sema &S, |
| 9942 | CXXConversionDecl *Conv, |
| 9943 | QualType ConvTy) { |
| 9944 | return DiagnosticBuilder::getEmpty(); |
| 9945 | } |
| 9946 | |
| 9947 | virtual DiagnosticBuilder diagnoseConversion(Sema &S, |
| 9948 | SourceLocation Loc, |
| 9949 | QualType T, |
| 9950 | QualType ConvTy) { |
| 9951 | return DiagnosticBuilder::getEmpty(); |
| 9952 | } |
| 9953 | } ConvertDiagnoser; |
| 9954 | |
| 9955 | Converted = ConvertToIntegralOrEnumerationType(DiagLoc, E, |
| 9956 | ConvertDiagnoser, false); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 9957 | } |
| 9958 | if (Converted.isInvalid()) |
| 9959 | return Converted; |
| 9960 | E = Converted.take(); |
| 9961 | if (!E->getType()->isIntegralOrUnscopedEnumerationType()) |
| 9962 | return ExprError(); |
| 9963 | } else if (!E->getType()->isIntegralOrUnscopedEnumerationType()) { |
| 9964 | // An ICE must be of integral or unscoped enumeration type. |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 9965 | if (!Diagnoser.Suppress) |
| 9966 | Diagnoser.diagnoseNotICE(*this, DiagLoc, E->getSourceRange()); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 9967 | return ExprError(); |
| 9968 | } |
| 9969 | |
Richard Smith | daaefc5 | 2011-12-14 23:32:26 +0000 | [diff] [blame] | 9970 | // Circumvent ICE checking in C++11 to avoid evaluating the expression twice |
| 9971 | // in the non-ICE case. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9972 | if (!getLangOpts().CPlusPlus0x && E->isIntegerConstantExpr(Context)) { |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 9973 | if (Result) |
| 9974 | *Result = E->EvaluateKnownConstInt(Context); |
| 9975 | return Owned(E); |
Eli Friedman | 3b5ccca | 2009-04-25 22:26:58 +0000 | [diff] [blame] | 9976 | } |
| 9977 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9978 | Expr::EvalResult EvalResult; |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 9979 | llvm::SmallVector<PartialDiagnosticAt, 8> Notes; |
| 9980 | EvalResult.Diag = &Notes; |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 9981 | |
Richard Smith | daaefc5 | 2011-12-14 23:32:26 +0000 | [diff] [blame] | 9982 | // Try to evaluate the expression, and produce diagnostics explaining why it's |
| 9983 | // not a constant expression as a side-effect. |
| 9984 | bool Folded = E->EvaluateAsRValue(EvalResult, Context) && |
| 9985 | EvalResult.Val.isInt() && !EvalResult.HasSideEffects; |
| 9986 | |
| 9987 | // In C++11, we can rely on diagnostics being produced for any expression |
| 9988 | // which is not a constant expression. If no diagnostics were produced, then |
| 9989 | // this is a constant expression. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 9990 | if (Folded && getLangOpts().CPlusPlus0x && Notes.empty()) { |
Richard Smith | daaefc5 | 2011-12-14 23:32:26 +0000 | [diff] [blame] | 9991 | if (Result) |
| 9992 | *Result = EvalResult.Val.getInt(); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 9993 | return Owned(E); |
| 9994 | } |
| 9995 | |
| 9996 | // If our only note is the usual "invalid subexpression" note, just point |
| 9997 | // the caret at its location rather than producing an essentially |
| 9998 | // redundant note. |
| 9999 | if (Notes.size() == 1 && Notes[0].second.getDiagID() == |
| 10000 | diag::note_invalid_subexpr_in_const_expr) { |
| 10001 | DiagLoc = Notes[0].first; |
| 10002 | Notes.clear(); |
Richard Smith | daaefc5 | 2011-12-14 23:32:26 +0000 | [diff] [blame] | 10003 | } |
| 10004 | |
| 10005 | if (!Folded || !AllowFold) { |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 10006 | if (!Diagnoser.Suppress) { |
| 10007 | Diagnoser.diagnoseNotICE(*this, DiagLoc, E->getSourceRange()); |
Richard Smith | dd1f29b | 2011-12-12 09:28:41 +0000 | [diff] [blame] | 10008 | for (unsigned I = 0, N = Notes.size(); I != N; ++I) |
| 10009 | Diag(Notes[I].first, Notes[I].second); |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 10010 | } |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 10011 | |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 10012 | return ExprError(); |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 10013 | } |
| 10014 | |
Douglas Gregor | ab41fe9 | 2012-05-04 22:38:52 +0000 | [diff] [blame] | 10015 | Diagnoser.diagnoseFold(*this, DiagLoc, E->getSourceRange()); |
Richard Smith | 244ee7b | 2012-01-15 03:51:30 +0000 | [diff] [blame] | 10016 | for (unsigned I = 0, N = Notes.size(); I != N; ++I) |
| 10017 | Diag(Notes[I].first, Notes[I].second); |
Mike Stump | eed9cac | 2009-02-19 03:04:26 +0000 | [diff] [blame] | 10018 | |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 10019 | if (Result) |
| 10020 | *Result = EvalResult.Val.getInt(); |
Richard Smith | 282e7e6 | 2012-02-04 09:53:13 +0000 | [diff] [blame] | 10021 | return Owned(E); |
Anders Carlsson | e21555e | 2008-11-30 19:50:32 +0000 | [diff] [blame] | 10022 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10023 | |
Eli Friedman | ef331b7 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 10024 | namespace { |
| 10025 | // Handle the case where we conclude a expression which we speculatively |
| 10026 | // considered to be unevaluated is actually evaluated. |
| 10027 | class TransformToPE : public TreeTransform<TransformToPE> { |
| 10028 | typedef TreeTransform<TransformToPE> BaseTransform; |
| 10029 | |
| 10030 | public: |
| 10031 | TransformToPE(Sema &SemaRef) : BaseTransform(SemaRef) { } |
| 10032 | |
| 10033 | // Make sure we redo semantic analysis |
| 10034 | bool AlwaysRebuild() { return true; } |
| 10035 | |
Eli Friedman | 56ff283 | 2012-02-06 23:29:57 +0000 | [diff] [blame] | 10036 | // Make sure we handle LabelStmts correctly. |
| 10037 | // FIXME: This does the right thing, but maybe we need a more general |
| 10038 | // fix to TreeTransform? |
| 10039 | StmtResult TransformLabelStmt(LabelStmt *S) { |
| 10040 | S->getDecl()->setStmt(0); |
| 10041 | return BaseTransform::TransformLabelStmt(S); |
| 10042 | } |
| 10043 | |
Eli Friedman | ef331b7 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 10044 | // We need to special-case DeclRefExprs referring to FieldDecls which |
| 10045 | // are not part of a member pointer formation; normal TreeTransforming |
| 10046 | // doesn't catch this case because of the way we represent them in the AST. |
| 10047 | // FIXME: This is a bit ugly; is it really the best way to handle this |
| 10048 | // case? |
| 10049 | // |
| 10050 | // Error on DeclRefExprs referring to FieldDecls. |
| 10051 | ExprResult TransformDeclRefExpr(DeclRefExpr *E) { |
| 10052 | if (isa<FieldDecl>(E->getDecl()) && |
David Blaikie | 71f55f7 | 2012-08-06 22:47:24 +0000 | [diff] [blame] | 10053 | !SemaRef.isUnevaluatedContext()) |
Eli Friedman | ef331b7 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 10054 | return SemaRef.Diag(E->getLocation(), |
| 10055 | diag::err_invalid_non_static_member_use) |
| 10056 | << E->getDecl() << E->getSourceRange(); |
| 10057 | |
| 10058 | return BaseTransform::TransformDeclRefExpr(E); |
| 10059 | } |
| 10060 | |
| 10061 | // Exception: filter out member pointer formation |
| 10062 | ExprResult TransformUnaryOperator(UnaryOperator *E) { |
| 10063 | if (E->getOpcode() == UO_AddrOf && E->getType()->isMemberPointerType()) |
| 10064 | return E; |
| 10065 | |
| 10066 | return BaseTransform::TransformUnaryOperator(E); |
| 10067 | } |
| 10068 | |
Douglas Gregor | e2c5913 | 2012-02-09 08:14:43 +0000 | [diff] [blame] | 10069 | ExprResult TransformLambdaExpr(LambdaExpr *E) { |
| 10070 | // Lambdas never need to be transformed. |
| 10071 | return E; |
| 10072 | } |
Eli Friedman | ef331b7 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 10073 | }; |
Eli Friedman | 93c878e | 2012-01-18 01:05:54 +0000 | [diff] [blame] | 10074 | } |
| 10075 | |
Eli Friedman | ef331b7 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 10076 | ExprResult Sema::TranformToPotentiallyEvaluated(Expr *E) { |
Eli Friedman | 72b8b1e | 2012-02-29 04:03:55 +0000 | [diff] [blame] | 10077 | assert(ExprEvalContexts.back().Context == Unevaluated && |
| 10078 | "Should only transform unevaluated expressions"); |
Eli Friedman | ef331b7 | 2012-01-20 01:26:23 +0000 | [diff] [blame] | 10079 | ExprEvalContexts.back().Context = |
| 10080 | ExprEvalContexts[ExprEvalContexts.size()-2].Context; |
| 10081 | if (ExprEvalContexts.back().Context == Unevaluated) |
| 10082 | return E; |
| 10083 | return TransformToPE(*this).TransformExpr(E); |
Eli Friedman | 93c878e | 2012-01-18 01:05:54 +0000 | [diff] [blame] | 10084 | } |
| 10085 | |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 10086 | void |
Douglas Gregor | ccc1b5e | 2012-02-21 00:37:24 +0000 | [diff] [blame] | 10087 | Sema::PushExpressionEvaluationContext(ExpressionEvaluationContext NewContext, |
Richard Smith | 76f3f69 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 10088 | Decl *LambdaContextDecl, |
| 10089 | bool IsDecltype) { |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 10090 | ExprEvalContexts.push_back( |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10091 | ExpressionEvaluationContextRecord(NewContext, |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 10092 | ExprCleanupObjects.size(), |
Douglas Gregor | ccc1b5e | 2012-02-21 00:37:24 +0000 | [diff] [blame] | 10093 | ExprNeedsCleanups, |
Richard Smith | 76f3f69 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 10094 | LambdaContextDecl, |
| 10095 | IsDecltype)); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10096 | ExprNeedsCleanups = false; |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10097 | if (!MaybeODRUseExprs.empty()) |
| 10098 | std::swap(MaybeODRUseExprs, ExprEvalContexts.back().SavedMaybeODRUseExprs); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 10099 | } |
| 10100 | |
Richard Trieu | 67e2933 | 2011-08-02 04:35:43 +0000 | [diff] [blame] | 10101 | void Sema::PopExpressionEvaluationContext() { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10102 | ExpressionEvaluationContextRecord& Rec = ExprEvalContexts.back(); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 10103 | |
Douglas Gregor | e2c5913 | 2012-02-09 08:14:43 +0000 | [diff] [blame] | 10104 | if (!Rec.Lambdas.empty()) { |
| 10105 | if (Rec.Context == Unevaluated) { |
| 10106 | // C++11 [expr.prim.lambda]p2: |
| 10107 | // A lambda-expression shall not appear in an unevaluated operand |
| 10108 | // (Clause 5). |
| 10109 | for (unsigned I = 0, N = Rec.Lambdas.size(); I != N; ++I) |
| 10110 | Diag(Rec.Lambdas[I]->getLocStart(), |
| 10111 | diag::err_lambda_unevaluated_operand); |
| 10112 | } else { |
| 10113 | // Mark the capture expressions odr-used. This was deferred |
| 10114 | // during lambda expression creation. |
| 10115 | for (unsigned I = 0, N = Rec.Lambdas.size(); I != N; ++I) { |
| 10116 | LambdaExpr *Lambda = Rec.Lambdas[I]; |
| 10117 | for (LambdaExpr::capture_init_iterator |
| 10118 | C = Lambda->capture_init_begin(), |
| 10119 | CEnd = Lambda->capture_init_end(); |
| 10120 | C != CEnd; ++C) { |
| 10121 | MarkDeclarationsReferencedInExpr(*C); |
| 10122 | } |
| 10123 | } |
| 10124 | } |
| 10125 | } |
| 10126 | |
Douglas Gregor | 2afce72 | 2009-11-26 00:44:06 +0000 | [diff] [blame] | 10127 | // When are coming out of an unevaluated context, clear out any |
| 10128 | // temporaries that we may have created as part of the evaluation of |
| 10129 | // the expression in that context: they aren't relevant because they |
| 10130 | // will never be constructed. |
Richard Smith | f6702a3 | 2011-12-20 02:08:33 +0000 | [diff] [blame] | 10131 | if (Rec.Context == Unevaluated || Rec.Context == ConstantEvaluated) { |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 10132 | ExprCleanupObjects.erase(ExprCleanupObjects.begin() + Rec.NumCleanupObjects, |
| 10133 | ExprCleanupObjects.end()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10134 | ExprNeedsCleanups = Rec.ParentNeedsCleanups; |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10135 | CleanupVarDeclMarking(); |
| 10136 | std::swap(MaybeODRUseExprs, Rec.SavedMaybeODRUseExprs); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10137 | // Otherwise, merge the contexts together. |
| 10138 | } else { |
| 10139 | ExprNeedsCleanups |= Rec.ParentNeedsCleanups; |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10140 | MaybeODRUseExprs.insert(Rec.SavedMaybeODRUseExprs.begin(), |
| 10141 | Rec.SavedMaybeODRUseExprs.end()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10142 | } |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10143 | |
| 10144 | // Pop the current expression evaluation context off the stack. |
| 10145 | ExprEvalContexts.pop_back(); |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 10146 | } |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10147 | |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10148 | void Sema::DiscardCleanupsInEvaluationContext() { |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 10149 | ExprCleanupObjects.erase( |
| 10150 | ExprCleanupObjects.begin() + ExprEvalContexts.back().NumCleanupObjects, |
| 10151 | ExprCleanupObjects.end()); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10152 | ExprNeedsCleanups = false; |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10153 | MaybeODRUseExprs.clear(); |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 10154 | } |
| 10155 | |
Eli Friedman | 71b8fb5 | 2012-01-21 01:01:51 +0000 | [diff] [blame] | 10156 | ExprResult Sema::HandleExprEvaluationContextForTypeof(Expr *E) { |
| 10157 | if (!E->getType()->isVariablyModifiedType()) |
| 10158 | return E; |
| 10159 | return TranformToPotentiallyEvaluated(E); |
| 10160 | } |
| 10161 | |
Benjamin Kramer | 5bbc385 | 2012-02-06 11:13:08 +0000 | [diff] [blame] | 10162 | static bool IsPotentiallyEvaluatedContext(Sema &SemaRef) { |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10163 | // Do not mark anything as "used" within a dependent context; wait for |
| 10164 | // an instantiation. |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10165 | if (SemaRef.CurContext->isDependentContext()) |
| 10166 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10167 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10168 | switch (SemaRef.ExprEvalContexts.back().Context) { |
| 10169 | case Sema::Unevaluated: |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 10170 | // We are in an expression that is not potentially evaluated; do nothing. |
Eli Friedman | 78a5424 | 2012-01-21 04:44:06 +0000 | [diff] [blame] | 10171 | // (Depending on how you read the standard, we actually do need to do |
| 10172 | // something here for null pointer constants, but the standard's |
| 10173 | // definition of a null pointer constant is completely crazy.) |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10174 | return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10175 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10176 | case Sema::ConstantEvaluated: |
| 10177 | case Sema::PotentiallyEvaluated: |
Eli Friedman | 78a5424 | 2012-01-21 04:44:06 +0000 | [diff] [blame] | 10178 | // We are in a potentially evaluated expression (or a constant-expression |
| 10179 | // in C++03); we need to do implicit template instantiation, implicitly |
| 10180 | // define class members, and mark most declarations as used. |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10181 | return true; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10182 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10183 | case Sema::PotentiallyEvaluatedIfUsed: |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 10184 | // Referenced declarations will only be used if the construct in the |
| 10185 | // containing expression is used. |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10186 | return false; |
Douglas Gregor | ac7610d | 2009-06-22 20:57:11 +0000 | [diff] [blame] | 10187 | } |
Matt Beaumont-Gay | 4f7dcdb | 2012-02-02 18:35:35 +0000 | [diff] [blame] | 10188 | llvm_unreachable("Invalid context"); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10189 | } |
| 10190 | |
| 10191 | /// \brief Mark a function referenced, and check whether it is odr-used |
| 10192 | /// (C++ [basic.def.odr]p2, C99 6.9p3) |
| 10193 | void Sema::MarkFunctionReferenced(SourceLocation Loc, FunctionDecl *Func) { |
| 10194 | assert(Func && "No function?"); |
| 10195 | |
| 10196 | Func->setReferenced(); |
| 10197 | |
Richard Smith | 57b9c4e | 2012-02-14 22:25:15 +0000 | [diff] [blame] | 10198 | // Don't mark this function as used multiple times, unless it's a constexpr |
| 10199 | // function which we need to instantiate. |
| 10200 | if (Func->isUsed(false) && |
| 10201 | !(Func->isConstexpr() && !Func->getBody() && |
| 10202 | Func->isImplicitlyInstantiable())) |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10203 | return; |
| 10204 | |
| 10205 | if (!IsPotentiallyEvaluatedContext(*this)) |
| 10206 | return; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10207 | |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10208 | // Note that this declaration has been used. |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10209 | if (CXXConstructorDecl *Constructor = dyn_cast<CXXConstructorDecl>(Func)) { |
Richard Smith | 03f6878 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 10210 | if (Constructor->isDefaulted() && !Constructor->isDeleted()) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10211 | if (Constructor->isDefaultConstructor()) { |
| 10212 | if (Constructor->isTrivial()) |
| 10213 | return; |
| 10214 | if (!Constructor->isUsed(false)) |
| 10215 | DefineImplicitDefaultConstructor(Loc, Constructor); |
| 10216 | } else if (Constructor->isCopyConstructor()) { |
| 10217 | if (!Constructor->isUsed(false)) |
| 10218 | DefineImplicitCopyConstructor(Loc, Constructor); |
| 10219 | } else if (Constructor->isMoveConstructor()) { |
| 10220 | if (!Constructor->isUsed(false)) |
| 10221 | DefineImplicitMoveConstructor(Loc, Constructor); |
| 10222 | } |
Fariborz Jahanian | 485f087 | 2009-06-22 23:34:40 +0000 | [diff] [blame] | 10223 | } |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10224 | |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10225 | MarkVTableUsed(Loc, Constructor->getParent()); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10226 | } else if (CXXDestructorDecl *Destructor = |
| 10227 | dyn_cast<CXXDestructorDecl>(Func)) { |
Richard Smith | 03f6878 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 10228 | if (Destructor->isDefaulted() && !Destructor->isDeleted() && |
| 10229 | !Destructor->isUsed(false)) |
Fariborz Jahanian | 8d2b356 | 2009-06-26 23:49:16 +0000 | [diff] [blame] | 10230 | DefineImplicitDestructor(Loc, Destructor); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10231 | if (Destructor->isVirtual()) |
| 10232 | MarkVTableUsed(Loc, Destructor->getParent()); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10233 | } else if (CXXMethodDecl *MethodDecl = dyn_cast<CXXMethodDecl>(Func)) { |
Richard Smith | 03f6878 | 2012-02-26 07:51:39 +0000 | [diff] [blame] | 10234 | if (MethodDecl->isDefaulted() && !MethodDecl->isDeleted() && |
| 10235 | MethodDecl->isOverloadedOperator() && |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 10236 | MethodDecl->getOverloadedOperator() == OO_Equal) { |
Sebastian Redl | 85ea7aa | 2011-08-30 19:58:05 +0000 | [diff] [blame] | 10237 | if (!MethodDecl->isUsed(false)) { |
| 10238 | if (MethodDecl->isCopyAssignmentOperator()) |
| 10239 | DefineImplicitCopyAssignment(Loc, MethodDecl); |
| 10240 | else |
| 10241 | DefineImplicitMoveAssignment(Loc, MethodDecl); |
| 10242 | } |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10243 | } else if (isa<CXXConversionDecl>(MethodDecl) && |
| 10244 | MethodDecl->getParent()->isLambda()) { |
| 10245 | CXXConversionDecl *Conversion = cast<CXXConversionDecl>(MethodDecl); |
| 10246 | if (Conversion->isLambdaToBlockPointerConversion()) |
| 10247 | DefineImplicitLambdaToBlockPointerConversion(Loc, Conversion); |
| 10248 | else |
| 10249 | DefineImplicitLambdaToFunctionPointerConversion(Loc, Conversion); |
Douglas Gregor | 6fb745b | 2010-05-13 16:44:06 +0000 | [diff] [blame] | 10250 | } else if (MethodDecl->isVirtual()) |
| 10251 | MarkVTableUsed(Loc, MethodDecl->getParent()); |
Fariborz Jahanian | c75bc2d | 2009-06-25 21:45:19 +0000 | [diff] [blame] | 10252 | } |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 10253 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10254 | // Recursive functions should be marked when used from another function. |
| 10255 | // FIXME: Is this really right? |
| 10256 | if (CurContext == Func) return; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 10257 | |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10258 | // Resolve the exception specification for any function which is |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 10259 | // used: CodeGen will need it. |
Richard Smith | 13bffc5 | 2012-04-19 00:08:28 +0000 | [diff] [blame] | 10260 | const FunctionProtoType *FPT = Func->getType()->getAs<FunctionProtoType>(); |
Richard Smith | b9d0b76 | 2012-07-27 04:22:15 +0000 | [diff] [blame] | 10261 | if (FPT && isUnresolvedExceptionSpec(FPT->getExceptionSpecType())) |
| 10262 | ResolveExceptionSpec(Loc, FPT); |
Richard Smith | e6975e9 | 2012-04-17 00:58:00 +0000 | [diff] [blame] | 10263 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10264 | // Implicit instantiation of function templates and member functions of |
| 10265 | // class templates. |
| 10266 | if (Func->isImplicitlyInstantiable()) { |
| 10267 | bool AlreadyInstantiated = false; |
Richard Smith | 57b9c4e | 2012-02-14 22:25:15 +0000 | [diff] [blame] | 10268 | SourceLocation PointOfInstantiation = Loc; |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10269 | if (FunctionTemplateSpecializationInfo *SpecInfo |
| 10270 | = Func->getTemplateSpecializationInfo()) { |
| 10271 | if (SpecInfo->getPointOfInstantiation().isInvalid()) |
| 10272 | SpecInfo->setPointOfInstantiation(Loc); |
| 10273 | else if (SpecInfo->getTemplateSpecializationKind() |
Richard Smith | 57b9c4e | 2012-02-14 22:25:15 +0000 | [diff] [blame] | 10274 | == TSK_ImplicitInstantiation) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10275 | AlreadyInstantiated = true; |
Richard Smith | 57b9c4e | 2012-02-14 22:25:15 +0000 | [diff] [blame] | 10276 | PointOfInstantiation = SpecInfo->getPointOfInstantiation(); |
| 10277 | } |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10278 | } else if (MemberSpecializationInfo *MSInfo |
| 10279 | = Func->getMemberSpecializationInfo()) { |
| 10280 | if (MSInfo->getPointOfInstantiation().isInvalid()) |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 10281 | MSInfo->setPointOfInstantiation(Loc); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10282 | else if (MSInfo->getTemplateSpecializationKind() |
Richard Smith | 57b9c4e | 2012-02-14 22:25:15 +0000 | [diff] [blame] | 10283 | == TSK_ImplicitInstantiation) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10284 | AlreadyInstantiated = true; |
Richard Smith | 57b9c4e | 2012-02-14 22:25:15 +0000 | [diff] [blame] | 10285 | PointOfInstantiation = MSInfo->getPointOfInstantiation(); |
| 10286 | } |
Douglas Gregor | b3ae4fc | 2009-10-12 20:18:28 +0000 | [diff] [blame] | 10287 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 10288 | |
Richard Smith | 57b9c4e | 2012-02-14 22:25:15 +0000 | [diff] [blame] | 10289 | if (!AlreadyInstantiated || Func->isConstexpr()) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10290 | if (isa<CXXRecordDecl>(Func->getDeclContext()) && |
| 10291 | cast<CXXRecordDecl>(Func->getDeclContext())->isLocalClass()) |
Richard Smith | 57b9c4e | 2012-02-14 22:25:15 +0000 | [diff] [blame] | 10292 | PendingLocalImplicitInstantiations.push_back( |
| 10293 | std::make_pair(Func, PointOfInstantiation)); |
| 10294 | else if (Func->isConstexpr()) |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10295 | // Do not defer instantiations of constexpr functions, to avoid the |
| 10296 | // expression evaluator needing to call back into Sema if it sees a |
| 10297 | // call to such a function. |
Richard Smith | 57b9c4e | 2012-02-14 22:25:15 +0000 | [diff] [blame] | 10298 | InstantiateFunctionDefinition(PointOfInstantiation, Func); |
Argyrios Kyrtzidis | 6d96836 | 2012-02-10 20:10:44 +0000 | [diff] [blame] | 10299 | else { |
Richard Smith | 57b9c4e | 2012-02-14 22:25:15 +0000 | [diff] [blame] | 10300 | PendingInstantiations.push_back(std::make_pair(Func, |
| 10301 | PointOfInstantiation)); |
Argyrios Kyrtzidis | 6d96836 | 2012-02-10 20:10:44 +0000 | [diff] [blame] | 10302 | // Notify the consumer that a function was implicitly instantiated. |
| 10303 | Consumer.HandleCXXImplicitFunctionInstantiation(Func); |
| 10304 | } |
John McCall | 15e310a | 2011-02-19 02:53:41 +0000 | [diff] [blame] | 10305 | } |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10306 | } else { |
| 10307 | // Walk redefinitions, as some of them may be instantiable. |
| 10308 | for (FunctionDecl::redecl_iterator i(Func->redecls_begin()), |
| 10309 | e(Func->redecls_end()); i != e; ++i) { |
| 10310 | if (!i->isUsed(false) && i->isImplicitlyInstantiable()) |
| 10311 | MarkFunctionReferenced(Loc, *i); |
| 10312 | } |
Sam Weinig | cce6ebc | 2009-09-11 03:29:30 +0000 | [diff] [blame] | 10313 | } |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10314 | |
| 10315 | // Keep track of used but undefined functions. |
| 10316 | if (!Func->isPure() && !Func->hasBody() && |
| 10317 | Func->getLinkage() != ExternalLinkage) { |
| 10318 | SourceLocation &old = UndefinedInternals[Func->getCanonicalDecl()]; |
| 10319 | if (old.isInvalid()) old = Loc; |
| 10320 | } |
| 10321 | |
| 10322 | Func->setUsed(true); |
| 10323 | } |
| 10324 | |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10325 | static void |
| 10326 | diagnoseUncapturableValueReference(Sema &S, SourceLocation loc, |
| 10327 | VarDecl *var, DeclContext *DC) { |
Eli Friedman | 0a29422 | 2012-02-07 00:15:00 +0000 | [diff] [blame] | 10328 | DeclContext *VarDC = var->getDeclContext(); |
| 10329 | |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10330 | // If the parameter still belongs to the translation unit, then |
| 10331 | // we're actually just using one parameter in the declaration of |
| 10332 | // the next. |
| 10333 | if (isa<ParmVarDecl>(var) && |
Eli Friedman | 0a29422 | 2012-02-07 00:15:00 +0000 | [diff] [blame] | 10334 | isa<TranslationUnitDecl>(VarDC)) |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10335 | return; |
| 10336 | |
Eli Friedman | 0a29422 | 2012-02-07 00:15:00 +0000 | [diff] [blame] | 10337 | // For C code, don't diagnose about capture if we're not actually in code |
| 10338 | // right now; it's impossible to write a non-constant expression outside of |
| 10339 | // function context, so we'll get other (more useful) diagnostics later. |
| 10340 | // |
| 10341 | // For C++, things get a bit more nasty... it would be nice to suppress this |
| 10342 | // diagnostic for certain cases like using a local variable in an array bound |
| 10343 | // for a member of a local class, but the correct predicate is not obvious. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10344 | if (!S.getLangOpts().CPlusPlus && !S.CurContext->isFunctionOrMethod()) |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10345 | return; |
| 10346 | |
Eli Friedman | 0a29422 | 2012-02-07 00:15:00 +0000 | [diff] [blame] | 10347 | if (isa<CXXMethodDecl>(VarDC) && |
| 10348 | cast<CXXRecordDecl>(VarDC->getParent())->isLambda()) { |
| 10349 | S.Diag(loc, diag::err_reference_to_local_var_in_enclosing_lambda) |
| 10350 | << var->getIdentifier(); |
| 10351 | } else if (FunctionDecl *fn = dyn_cast<FunctionDecl>(VarDC)) { |
| 10352 | S.Diag(loc, diag::err_reference_to_local_var_in_enclosing_function) |
| 10353 | << var->getIdentifier() << fn->getDeclName(); |
| 10354 | } else if (isa<BlockDecl>(VarDC)) { |
| 10355 | S.Diag(loc, diag::err_reference_to_local_var_in_enclosing_block) |
| 10356 | << var->getIdentifier(); |
| 10357 | } else { |
| 10358 | // FIXME: Is there any other context where a local variable can be |
| 10359 | // declared? |
| 10360 | S.Diag(loc, diag::err_reference_to_local_var_in_enclosing_context) |
| 10361 | << var->getIdentifier(); |
| 10362 | } |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10363 | |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10364 | S.Diag(var->getLocation(), diag::note_local_variable_declared_here) |
| 10365 | << var->getIdentifier(); |
Eli Friedman | 0a29422 | 2012-02-07 00:15:00 +0000 | [diff] [blame] | 10366 | |
| 10367 | // FIXME: Add additional diagnostic info about class etc. which prevents |
| 10368 | // capture. |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10369 | } |
| 10370 | |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10371 | /// \brief Capture the given variable in the given lambda expression. |
| 10372 | static ExprResult captureInLambda(Sema &S, LambdaScopeInfo *LSI, |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10373 | VarDecl *Var, QualType FieldType, |
| 10374 | QualType DeclRefType, |
Douglas Gregor | d57f52c | 2012-05-16 17:01:33 +0000 | [diff] [blame] | 10375 | SourceLocation Loc, |
| 10376 | bool RefersToEnclosingLocal) { |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10377 | CXXRecordDecl *Lambda = LSI->Lambda; |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10378 | |
Douglas Gregor | 1f9a5db | 2012-02-09 01:56:40 +0000 | [diff] [blame] | 10379 | // Build the non-static data member. |
| 10380 | FieldDecl *Field |
| 10381 | = FieldDecl::Create(S.Context, Lambda, Loc, Loc, 0, FieldType, |
| 10382 | S.Context.getTrivialTypeSourceInfo(FieldType, Loc), |
Richard Smith | ca52330 | 2012-06-10 03:12:00 +0000 | [diff] [blame] | 10383 | 0, false, ICIS_NoInit); |
Douglas Gregor | 1f9a5db | 2012-02-09 01:56:40 +0000 | [diff] [blame] | 10384 | Field->setImplicit(true); |
| 10385 | Field->setAccess(AS_private); |
Douglas Gregor | 20f87a4 | 2012-02-09 02:12:34 +0000 | [diff] [blame] | 10386 | Lambda->addDecl(Field); |
Douglas Gregor | 1f9a5db | 2012-02-09 01:56:40 +0000 | [diff] [blame] | 10387 | |
| 10388 | // C++11 [expr.prim.lambda]p21: |
| 10389 | // When the lambda-expression is evaluated, the entities that |
| 10390 | // are captured by copy are used to direct-initialize each |
| 10391 | // corresponding non-static data member of the resulting closure |
| 10392 | // object. (For array members, the array elements are |
| 10393 | // direct-initialized in increasing subscript order.) These |
| 10394 | // initializations are performed in the (unspecified) order in |
| 10395 | // which the non-static data members are declared. |
Douglas Gregor | 1f9a5db | 2012-02-09 01:56:40 +0000 | [diff] [blame] | 10396 | |
Douglas Gregor | e2c5913 | 2012-02-09 08:14:43 +0000 | [diff] [blame] | 10397 | // Introduce a new evaluation context for the initialization, so |
| 10398 | // that temporaries introduced as part of the capture are retained |
| 10399 | // to be re-"exported" from the lambda expression itself. |
Douglas Gregor | 1f9a5db | 2012-02-09 01:56:40 +0000 | [diff] [blame] | 10400 | S.PushExpressionEvaluationContext(Sema::PotentiallyEvaluated); |
| 10401 | |
Douglas Gregor | 73d9092 | 2012-02-10 09:26:04 +0000 | [diff] [blame] | 10402 | // C++ [expr.prim.labda]p12: |
| 10403 | // An entity captured by a lambda-expression is odr-used (3.2) in |
| 10404 | // the scope containing the lambda-expression. |
Douglas Gregor | d57f52c | 2012-05-16 17:01:33 +0000 | [diff] [blame] | 10405 | Expr *Ref = new (S.Context) DeclRefExpr(Var, RefersToEnclosingLocal, |
| 10406 | DeclRefType, VK_LValue, Loc); |
Eli Friedman | 88530d5 | 2012-03-01 21:32:56 +0000 | [diff] [blame] | 10407 | Var->setReferenced(true); |
Douglas Gregor | 73d9092 | 2012-02-10 09:26:04 +0000 | [diff] [blame] | 10408 | Var->setUsed(true); |
Douglas Gregor | 18fe084 | 2012-02-09 02:45:47 +0000 | [diff] [blame] | 10409 | |
| 10410 | // When the field has array type, create index variables for each |
| 10411 | // dimension of the array. We use these index variables to subscript |
| 10412 | // the source array, and other clients (e.g., CodeGen) will perform |
| 10413 | // the necessary iteration with these index variables. |
| 10414 | SmallVector<VarDecl *, 4> IndexVariables; |
Douglas Gregor | 18fe084 | 2012-02-09 02:45:47 +0000 | [diff] [blame] | 10415 | QualType BaseType = FieldType; |
| 10416 | QualType SizeType = S.Context.getSizeType(); |
Douglas Gregor | 9daa7bf | 2012-02-13 16:35:30 +0000 | [diff] [blame] | 10417 | LSI->ArrayIndexStarts.push_back(LSI->ArrayIndexVars.size()); |
Douglas Gregor | 18fe084 | 2012-02-09 02:45:47 +0000 | [diff] [blame] | 10418 | while (const ConstantArrayType *Array |
| 10419 | = S.Context.getAsConstantArrayType(BaseType)) { |
Douglas Gregor | 18fe084 | 2012-02-09 02:45:47 +0000 | [diff] [blame] | 10420 | // Create the iteration variable for this array index. |
| 10421 | IdentifierInfo *IterationVarName = 0; |
| 10422 | { |
| 10423 | SmallString<8> Str; |
| 10424 | llvm::raw_svector_ostream OS(Str); |
| 10425 | OS << "__i" << IndexVariables.size(); |
| 10426 | IterationVarName = &S.Context.Idents.get(OS.str()); |
| 10427 | } |
| 10428 | VarDecl *IterationVar |
| 10429 | = VarDecl::Create(S.Context, S.CurContext, Loc, Loc, |
| 10430 | IterationVarName, SizeType, |
| 10431 | S.Context.getTrivialTypeSourceInfo(SizeType, Loc), |
| 10432 | SC_None, SC_None); |
| 10433 | IndexVariables.push_back(IterationVar); |
Douglas Gregor | 9daa7bf | 2012-02-13 16:35:30 +0000 | [diff] [blame] | 10434 | LSI->ArrayIndexVars.push_back(IterationVar); |
| 10435 | |
Douglas Gregor | 18fe084 | 2012-02-09 02:45:47 +0000 | [diff] [blame] | 10436 | // Create a reference to the iteration variable. |
| 10437 | ExprResult IterationVarRef |
| 10438 | = S.BuildDeclRefExpr(IterationVar, SizeType, VK_LValue, Loc); |
| 10439 | assert(!IterationVarRef.isInvalid() && |
| 10440 | "Reference to invented variable cannot fail!"); |
| 10441 | IterationVarRef = S.DefaultLvalueConversion(IterationVarRef.take()); |
| 10442 | assert(!IterationVarRef.isInvalid() && |
| 10443 | "Conversion of invented variable cannot fail!"); |
| 10444 | |
| 10445 | // Subscript the array with this iteration variable. |
| 10446 | ExprResult Subscript = S.CreateBuiltinArraySubscriptExpr( |
| 10447 | Ref, Loc, IterationVarRef.take(), Loc); |
| 10448 | if (Subscript.isInvalid()) { |
| 10449 | S.CleanupVarDeclMarking(); |
| 10450 | S.DiscardCleanupsInEvaluationContext(); |
| 10451 | S.PopExpressionEvaluationContext(); |
| 10452 | return ExprError(); |
| 10453 | } |
| 10454 | |
| 10455 | Ref = Subscript.take(); |
| 10456 | BaseType = Array->getElementType(); |
| 10457 | } |
| 10458 | |
| 10459 | // Construct the entity that we will be initializing. For an array, this |
| 10460 | // will be first element in the array, which may require several levels |
| 10461 | // of array-subscript entities. |
| 10462 | SmallVector<InitializedEntity, 4> Entities; |
| 10463 | Entities.reserve(1 + IndexVariables.size()); |
Douglas Gregor | 4773654 | 2012-02-15 16:57:26 +0000 | [diff] [blame] | 10464 | Entities.push_back( |
| 10465 | InitializedEntity::InitializeLambdaCapture(Var, Field, Loc)); |
Douglas Gregor | 18fe084 | 2012-02-09 02:45:47 +0000 | [diff] [blame] | 10466 | for (unsigned I = 0, N = IndexVariables.size(); I != N; ++I) |
| 10467 | Entities.push_back(InitializedEntity::InitializeElement(S.Context, |
| 10468 | 0, |
| 10469 | Entities.back())); |
| 10470 | |
Douglas Gregor | 1f9a5db | 2012-02-09 01:56:40 +0000 | [diff] [blame] | 10471 | InitializationKind InitKind |
| 10472 | = InitializationKind::CreateDirect(Loc, Loc, Loc); |
Douglas Gregor | 18fe084 | 2012-02-09 02:45:47 +0000 | [diff] [blame] | 10473 | InitializationSequence Init(S, Entities.back(), InitKind, &Ref, 1); |
Douglas Gregor | 1f9a5db | 2012-02-09 01:56:40 +0000 | [diff] [blame] | 10474 | ExprResult Result(true); |
Douglas Gregor | 18fe084 | 2012-02-09 02:45:47 +0000 | [diff] [blame] | 10475 | if (!Init.Diagnose(S, Entities.back(), InitKind, &Ref, 1)) |
Benjamin Kramer | 5354e77 | 2012-08-23 23:38:35 +0000 | [diff] [blame] | 10476 | Result = Init.Perform(S, Entities.back(), InitKind, Ref); |
Douglas Gregor | 1f9a5db | 2012-02-09 01:56:40 +0000 | [diff] [blame] | 10477 | |
| 10478 | // If this initialization requires any cleanups (e.g., due to a |
| 10479 | // default argument to a copy constructor), note that for the |
| 10480 | // lambda. |
| 10481 | if (S.ExprNeedsCleanups) |
| 10482 | LSI->ExprNeedsCleanups = true; |
| 10483 | |
| 10484 | // Exit the expression evaluation context used for the capture. |
| 10485 | S.CleanupVarDeclMarking(); |
| 10486 | S.DiscardCleanupsInEvaluationContext(); |
| 10487 | S.PopExpressionEvaluationContext(); |
| 10488 | return Result; |
Douglas Gregor | 18fe084 | 2012-02-09 02:45:47 +0000 | [diff] [blame] | 10489 | } |
Douglas Gregor | 1f9a5db | 2012-02-09 01:56:40 +0000 | [diff] [blame] | 10490 | |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10491 | bool Sema::tryCaptureVariable(VarDecl *Var, SourceLocation Loc, |
| 10492 | TryCaptureKind Kind, SourceLocation EllipsisLoc, |
| 10493 | bool BuildAndDiagnose, |
| 10494 | QualType &CaptureType, |
| 10495 | QualType &DeclRefType) { |
| 10496 | bool Nested = false; |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10497 | |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 10498 | DeclContext *DC = CurContext; |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10499 | if (Var->getDeclContext() == DC) return true; |
| 10500 | if (!Var->hasLocalStorage()) return true; |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10501 | |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10502 | bool HasBlocksAttr = Var->hasAttr<BlocksAttr>(); |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10503 | |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10504 | // Walk up the stack to determine whether we can capture the variable, |
| 10505 | // performing the "simple" checks that don't depend on type. We stop when |
| 10506 | // we've either hit the declared scope of the variable or find an existing |
| 10507 | // capture of that variable. |
| 10508 | CaptureType = Var->getType(); |
| 10509 | DeclRefType = CaptureType.getNonReferenceType(); |
| 10510 | bool Explicit = (Kind != TryCapture_Implicit); |
| 10511 | unsigned FunctionScopesIndex = FunctionScopes.size() - 1; |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10512 | do { |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 10513 | // Only block literals and lambda expressions can capture; other |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10514 | // scopes don't work. |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 10515 | DeclContext *ParentDC; |
| 10516 | if (isa<BlockDecl>(DC)) |
| 10517 | ParentDC = DC->getParent(); |
| 10518 | else if (isa<CXXMethodDecl>(DC) && |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10519 | cast<CXXMethodDecl>(DC)->getOverloadedOperator() == OO_Call && |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 10520 | cast<CXXRecordDecl>(DC->getParent())->isLambda()) |
| 10521 | ParentDC = DC->getParent()->getParent(); |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10522 | else { |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10523 | if (BuildAndDiagnose) |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10524 | diagnoseUncapturableValueReference(*this, Loc, Var, DC); |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10525 | return true; |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10526 | } |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10527 | |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 10528 | CapturingScopeInfo *CSI = |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10529 | cast<CapturingScopeInfo>(FunctionScopes[FunctionScopesIndex]); |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10530 | |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 10531 | // Check whether we've already captured it. |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10532 | if (CSI->CaptureMap.count(Var)) { |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10533 | // If we found a capture, any subcaptures are nested. |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10534 | Nested = true; |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10535 | |
| 10536 | // Retrieve the capture type for this variable. |
| 10537 | CaptureType = CSI->getCapture(Var).getCaptureType(); |
| 10538 | |
| 10539 | // Compute the type of an expression that refers to this variable. |
| 10540 | DeclRefType = CaptureType.getNonReferenceType(); |
| 10541 | |
| 10542 | const CapturingScopeInfo::Capture &Cap = CSI->getCapture(Var); |
| 10543 | if (Cap.isCopyCapture() && |
| 10544 | !(isa<LambdaScopeInfo>(CSI) && cast<LambdaScopeInfo>(CSI)->Mutable)) |
| 10545 | DeclRefType.addConst(); |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10546 | break; |
| 10547 | } |
| 10548 | |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10549 | bool IsBlock = isa<BlockScopeInfo>(CSI); |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10550 | bool IsLambda = !IsBlock; |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 10551 | |
| 10552 | // Lambdas are not allowed to capture unnamed variables |
| 10553 | // (e.g. anonymous unions). |
| 10554 | // FIXME: The C++11 rule don't actually state this explicitly, but I'm |
| 10555 | // assuming that's the intent. |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10556 | if (IsLambda && !Var->getDeclName()) { |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10557 | if (BuildAndDiagnose) { |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10558 | Diag(Loc, diag::err_lambda_capture_anonymous_var); |
| 10559 | Diag(Var->getLocation(), diag::note_declared_at); |
| 10560 | } |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10561 | return true; |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 10562 | } |
| 10563 | |
| 10564 | // Prohibit variably-modified types; they're difficult to deal with. |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10565 | if (Var->getType()->isVariablyModifiedType()) { |
| 10566 | if (BuildAndDiagnose) { |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10567 | if (IsBlock) |
| 10568 | Diag(Loc, diag::err_ref_vm_type); |
| 10569 | else |
| 10570 | Diag(Loc, diag::err_lambda_capture_vm_type) << Var->getDeclName(); |
| 10571 | Diag(Var->getLocation(), diag::note_previous_decl) |
| 10572 | << Var->getDeclName(); |
| 10573 | } |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10574 | return true; |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10575 | } |
| 10576 | |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 10577 | // Lambdas are not allowed to capture __block variables; they don't |
| 10578 | // support the expected semantics. |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10579 | if (IsLambda && HasBlocksAttr) { |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10580 | if (BuildAndDiagnose) { |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10581 | Diag(Loc, diag::err_lambda_capture_block) |
| 10582 | << Var->getDeclName(); |
| 10583 | Diag(Var->getLocation(), diag::note_previous_decl) |
| 10584 | << Var->getDeclName(); |
| 10585 | } |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10586 | return true; |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 10587 | } |
| 10588 | |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10589 | if (CSI->ImpCaptureStyle == CapturingScopeInfo::ImpCap_None && !Explicit) { |
| 10590 | // No capture-default |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10591 | if (BuildAndDiagnose) { |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10592 | Diag(Loc, diag::err_lambda_impcap) << Var->getDeclName(); |
| 10593 | Diag(Var->getLocation(), diag::note_previous_decl) |
| 10594 | << Var->getDeclName(); |
| 10595 | Diag(cast<LambdaScopeInfo>(CSI)->Lambda->getLocStart(), |
| 10596 | diag::note_lambda_decl); |
| 10597 | } |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10598 | return true; |
Douglas Gregor | f8af982 | 2012-02-12 18:42:33 +0000 | [diff] [blame] | 10599 | } |
| 10600 | |
| 10601 | FunctionScopesIndex--; |
| 10602 | DC = ParentDC; |
| 10603 | Explicit = false; |
| 10604 | } while (!Var->getDeclContext()->Equals(DC)); |
| 10605 | |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10606 | // Walk back down the scope stack, computing the type of the capture at |
| 10607 | // each step, checking type-specific requirements, and adding captures if |
| 10608 | // requested. |
| 10609 | for (unsigned I = ++FunctionScopesIndex, N = FunctionScopes.size(); I != N; |
| 10610 | ++I) { |
| 10611 | CapturingScopeInfo *CSI = cast<CapturingScopeInfo>(FunctionScopes[I]); |
Douglas Gregor | 6893284 | 2012-02-18 05:51:20 +0000 | [diff] [blame] | 10612 | |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10613 | // Compute the type of the capture and of a reference to the capture within |
| 10614 | // this scope. |
| 10615 | if (isa<BlockScopeInfo>(CSI)) { |
| 10616 | Expr *CopyExpr = 0; |
| 10617 | bool ByRef = false; |
| 10618 | |
| 10619 | // Blocks are not allowed to capture arrays. |
| 10620 | if (CaptureType->isArrayType()) { |
| 10621 | if (BuildAndDiagnose) { |
| 10622 | Diag(Loc, diag::err_ref_array_type); |
| 10623 | Diag(Var->getLocation(), diag::note_previous_decl) |
| 10624 | << Var->getDeclName(); |
| 10625 | } |
| 10626 | return true; |
| 10627 | } |
| 10628 | |
John McCall | 100c649 | 2012-03-30 05:23:48 +0000 | [diff] [blame] | 10629 | // Forbid the block-capture of autoreleasing variables. |
| 10630 | if (CaptureType.getObjCLifetime() == Qualifiers::OCL_Autoreleasing) { |
| 10631 | if (BuildAndDiagnose) { |
| 10632 | Diag(Loc, diag::err_arc_autoreleasing_capture) |
| 10633 | << /*block*/ 0; |
| 10634 | Diag(Var->getLocation(), diag::note_previous_decl) |
| 10635 | << Var->getDeclName(); |
| 10636 | } |
| 10637 | return true; |
| 10638 | } |
| 10639 | |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10640 | if (HasBlocksAttr || CaptureType->isReferenceType()) { |
| 10641 | // Block capture by reference does not change the capture or |
| 10642 | // declaration reference types. |
| 10643 | ByRef = true; |
| 10644 | } else { |
| 10645 | // Block capture by copy introduces 'const'. |
| 10646 | CaptureType = CaptureType.getNonReferenceType().withConst(); |
| 10647 | DeclRefType = CaptureType; |
| 10648 | |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 10649 | if (getLangOpts().CPlusPlus && BuildAndDiagnose) { |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10650 | if (const RecordType *Record = DeclRefType->getAs<RecordType>()) { |
| 10651 | // The capture logic needs the destructor, so make sure we mark it. |
| 10652 | // Usually this is unnecessary because most local variables have |
| 10653 | // their destructors marked at declaration time, but parameters are |
| 10654 | // an exception because it's technically only the call site that |
| 10655 | // actually requires the destructor. |
| 10656 | if (isa<ParmVarDecl>(Var)) |
| 10657 | FinalizeVarWithDestructor(Var, Record); |
| 10658 | |
| 10659 | // According to the blocks spec, the capture of a variable from |
| 10660 | // the stack requires a const copy constructor. This is not true |
| 10661 | // of the copy/move done to move a __block variable to the heap. |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 10662 | Expr *DeclRef = new (Context) DeclRefExpr(Var, false, |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10663 | DeclRefType.withConst(), |
| 10664 | VK_LValue, Loc); |
| 10665 | ExprResult Result |
| 10666 | = PerformCopyInitialization( |
| 10667 | InitializedEntity::InitializeBlock(Var->getLocation(), |
| 10668 | CaptureType, false), |
| 10669 | Loc, Owned(DeclRef)); |
| 10670 | |
| 10671 | // Build a full-expression copy expression if initialization |
| 10672 | // succeeded and used a non-trivial constructor. Recover from |
| 10673 | // errors by pretending that the copy isn't necessary. |
| 10674 | if (!Result.isInvalid() && |
| 10675 | !cast<CXXConstructExpr>(Result.get())->getConstructor() |
| 10676 | ->isTrivial()) { |
| 10677 | Result = MaybeCreateExprWithCleanups(Result); |
| 10678 | CopyExpr = Result.take(); |
| 10679 | } |
| 10680 | } |
| 10681 | } |
| 10682 | } |
| 10683 | |
| 10684 | // Actually capture the variable. |
| 10685 | if (BuildAndDiagnose) |
| 10686 | CSI->addCapture(Var, HasBlocksAttr, ByRef, Nested, Loc, |
| 10687 | SourceLocation(), CaptureType, CopyExpr); |
| 10688 | Nested = true; |
| 10689 | continue; |
| 10690 | } |
Douglas Gregor | 6893284 | 2012-02-18 05:51:20 +0000 | [diff] [blame] | 10691 | |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10692 | LambdaScopeInfo *LSI = cast<LambdaScopeInfo>(CSI); |
| 10693 | |
| 10694 | // Determine whether we are capturing by reference or by value. |
| 10695 | bool ByRef = false; |
| 10696 | if (I == N - 1 && Kind != TryCapture_Implicit) { |
| 10697 | ByRef = (Kind == TryCapture_ExplicitByRef); |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 10698 | } else { |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10699 | ByRef = (LSI->ImpCaptureStyle == LambdaScopeInfo::ImpCap_LambdaByref); |
Eli Friedman | b942cb2 | 2012-02-03 22:47:37 +0000 | [diff] [blame] | 10700 | } |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10701 | |
| 10702 | // Compute the type of the field that will capture this variable. |
| 10703 | if (ByRef) { |
| 10704 | // C++11 [expr.prim.lambda]p15: |
| 10705 | // An entity is captured by reference if it is implicitly or |
| 10706 | // explicitly captured but not captured by copy. It is |
| 10707 | // unspecified whether additional unnamed non-static data |
| 10708 | // members are declared in the closure type for entities |
| 10709 | // captured by reference. |
| 10710 | // |
| 10711 | // FIXME: It is not clear whether we want to build an lvalue reference |
| 10712 | // to the DeclRefType or to CaptureType.getNonReferenceType(). GCC appears |
| 10713 | // to do the former, while EDG does the latter. Core issue 1249 will |
| 10714 | // clarify, but for now we follow GCC because it's a more permissive and |
| 10715 | // easily defensible position. |
| 10716 | CaptureType = Context.getLValueReferenceType(DeclRefType); |
| 10717 | } else { |
| 10718 | // C++11 [expr.prim.lambda]p14: |
| 10719 | // For each entity captured by copy, an unnamed non-static |
| 10720 | // data member is declared in the closure type. The |
| 10721 | // declaration order of these members is unspecified. The type |
| 10722 | // of such a data member is the type of the corresponding |
| 10723 | // captured entity if the entity is not a reference to an |
| 10724 | // object, or the referenced type otherwise. [Note: If the |
| 10725 | // captured entity is a reference to a function, the |
| 10726 | // corresponding data member is also a reference to a |
| 10727 | // function. - end note ] |
| 10728 | if (const ReferenceType *RefType = CaptureType->getAs<ReferenceType>()){ |
| 10729 | if (!RefType->getPointeeType()->isFunctionType()) |
| 10730 | CaptureType = RefType->getPointeeType(); |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10731 | } |
John McCall | 100c649 | 2012-03-30 05:23:48 +0000 | [diff] [blame] | 10732 | |
| 10733 | // Forbid the lambda copy-capture of autoreleasing variables. |
| 10734 | if (CaptureType.getObjCLifetime() == Qualifiers::OCL_Autoreleasing) { |
| 10735 | if (BuildAndDiagnose) { |
| 10736 | Diag(Loc, diag::err_arc_autoreleasing_capture) << /*lambda*/ 1; |
| 10737 | Diag(Var->getLocation(), diag::note_previous_decl) |
| 10738 | << Var->getDeclName(); |
| 10739 | } |
| 10740 | return true; |
| 10741 | } |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10742 | } |
| 10743 | |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10744 | // Capture this variable in the lambda. |
| 10745 | Expr *CopyExpr = 0; |
| 10746 | if (BuildAndDiagnose) { |
| 10747 | ExprResult Result = captureInLambda(*this, LSI, Var, CaptureType, |
Douglas Gregor | d57f52c | 2012-05-16 17:01:33 +0000 | [diff] [blame] | 10748 | DeclRefType, Loc, |
| 10749 | I == N-1); |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10750 | if (!Result.isInvalid()) |
| 10751 | CopyExpr = Result.take(); |
| 10752 | } |
| 10753 | |
| 10754 | // Compute the type of a reference to this captured variable. |
| 10755 | if (ByRef) |
| 10756 | DeclRefType = CaptureType.getNonReferenceType(); |
| 10757 | else { |
| 10758 | // C++ [expr.prim.lambda]p5: |
| 10759 | // The closure type for a lambda-expression has a public inline |
| 10760 | // function call operator [...]. This function call operator is |
| 10761 | // declared const (9.3.1) if and only if the lambda-expression’s |
| 10762 | // parameter-declaration-clause is not followed by mutable. |
| 10763 | DeclRefType = CaptureType.getNonReferenceType(); |
| 10764 | if (!LSI->Mutable && !CaptureType->isReferenceType()) |
| 10765 | DeclRefType.addConst(); |
| 10766 | } |
| 10767 | |
| 10768 | // Add the capture. |
| 10769 | if (BuildAndDiagnose) |
| 10770 | CSI->addCapture(Var, /*IsBlock=*/false, ByRef, Nested, Loc, |
| 10771 | EllipsisLoc, CaptureType, CopyExpr); |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10772 | Nested = true; |
| 10773 | } |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10774 | |
| 10775 | return false; |
| 10776 | } |
| 10777 | |
| 10778 | bool Sema::tryCaptureVariable(VarDecl *Var, SourceLocation Loc, |
| 10779 | TryCaptureKind Kind, SourceLocation EllipsisLoc) { |
| 10780 | QualType CaptureType; |
| 10781 | QualType DeclRefType; |
| 10782 | return tryCaptureVariable(Var, Loc, Kind, EllipsisLoc, |
| 10783 | /*BuildAndDiagnose=*/true, CaptureType, |
| 10784 | DeclRefType); |
| 10785 | } |
| 10786 | |
| 10787 | QualType Sema::getCapturedDeclRefType(VarDecl *Var, SourceLocation Loc) { |
| 10788 | QualType CaptureType; |
| 10789 | QualType DeclRefType; |
| 10790 | |
| 10791 | // Determine whether we can capture this variable. |
| 10792 | if (tryCaptureVariable(Var, Loc, TryCapture_Implicit, SourceLocation(), |
| 10793 | /*BuildAndDiagnose=*/false, CaptureType, DeclRefType)) |
| 10794 | return QualType(); |
| 10795 | |
| 10796 | return DeclRefType; |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10797 | } |
| 10798 | |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10799 | static void MarkVarDeclODRUsed(Sema &SemaRef, VarDecl *Var, |
| 10800 | SourceLocation Loc) { |
| 10801 | // Keep track of used but undefined variables. |
Eli Friedman | 0cc5d40 | 2012-02-04 00:54:05 +0000 | [diff] [blame] | 10802 | // FIXME: We shouldn't suppress this warning for static data members. |
Daniel Dunbar | 3d13c5a | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 10803 | if (Var->hasDefinition(SemaRef.Context) == VarDecl::DeclarationOnly && |
Eli Friedman | 0cc5d40 | 2012-02-04 00:54:05 +0000 | [diff] [blame] | 10804 | Var->getLinkage() != ExternalLinkage && |
| 10805 | !(Var->isStaticDataMember() && Var->hasInit())) { |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10806 | SourceLocation &old = SemaRef.UndefinedInternals[Var->getCanonicalDecl()]; |
| 10807 | if (old.isInvalid()) old = Loc; |
| 10808 | } |
| 10809 | |
Douglas Gregor | 999713e | 2012-02-18 09:37:24 +0000 | [diff] [blame] | 10810 | SemaRef.tryCaptureVariable(Var, Loc); |
Eli Friedman | 3c0e80e | 2012-02-03 02:04:35 +0000 | [diff] [blame] | 10811 | |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10812 | Var->setUsed(true); |
| 10813 | } |
| 10814 | |
| 10815 | void Sema::UpdateMarkingForLValueToRValue(Expr *E) { |
| 10816 | // Per C++11 [basic.def.odr], a variable is odr-used "unless it is |
| 10817 | // an object that satisfies the requirements for appearing in a |
| 10818 | // constant expression (5.19) and the lvalue-to-rvalue conversion (4.1) |
| 10819 | // is immediately applied." This function handles the lvalue-to-rvalue |
| 10820 | // conversion part. |
| 10821 | MaybeODRUseExprs.erase(E->IgnoreParens()); |
| 10822 | } |
| 10823 | |
Eli Friedman | ac62601 | 2012-02-29 03:16:56 +0000 | [diff] [blame] | 10824 | ExprResult Sema::ActOnConstantExpression(ExprResult Res) { |
| 10825 | if (!Res.isUsable()) |
| 10826 | return Res; |
| 10827 | |
| 10828 | // If a constant-expression is a reference to a variable where we delay |
| 10829 | // deciding whether it is an odr-use, just assume we will apply the |
| 10830 | // lvalue-to-rvalue conversion. In the one case where this doesn't happen |
| 10831 | // (a non-type template argument), we have special handling anyway. |
| 10832 | UpdateMarkingForLValueToRValue(Res.get()); |
| 10833 | return Res; |
| 10834 | } |
| 10835 | |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10836 | void Sema::CleanupVarDeclMarking() { |
| 10837 | for (llvm::SmallPtrSetIterator<Expr*> i = MaybeODRUseExprs.begin(), |
| 10838 | e = MaybeODRUseExprs.end(); |
| 10839 | i != e; ++i) { |
| 10840 | VarDecl *Var; |
| 10841 | SourceLocation Loc; |
John McCall | f4b88a4 | 2012-03-10 09:33:50 +0000 | [diff] [blame] | 10842 | if (DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(*i)) { |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10843 | Var = cast<VarDecl>(DRE->getDecl()); |
| 10844 | Loc = DRE->getLocation(); |
| 10845 | } else if (MemberExpr *ME = dyn_cast<MemberExpr>(*i)) { |
| 10846 | Var = cast<VarDecl>(ME->getMemberDecl()); |
| 10847 | Loc = ME->getMemberLoc(); |
| 10848 | } else { |
| 10849 | llvm_unreachable("Unexpcted expression"); |
| 10850 | } |
| 10851 | |
| 10852 | MarkVarDeclODRUsed(*this, Var, Loc); |
| 10853 | } |
| 10854 | |
| 10855 | MaybeODRUseExprs.clear(); |
| 10856 | } |
| 10857 | |
| 10858 | // Mark a VarDecl referenced, and perform the necessary handling to compute |
| 10859 | // odr-uses. |
| 10860 | static void DoMarkVarDeclReferenced(Sema &SemaRef, SourceLocation Loc, |
| 10861 | VarDecl *Var, Expr *E) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10862 | Var->setReferenced(); |
| 10863 | |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10864 | if (!IsPotentiallyEvaluatedContext(SemaRef)) |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10865 | return; |
| 10866 | |
| 10867 | // Implicit instantiation of static data members of class templates. |
Richard Smith | 37ce010 | 2012-02-15 02:42:50 +0000 | [diff] [blame] | 10868 | if (Var->isStaticDataMember() && Var->getInstantiatedFromStaticDataMember()) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10869 | MemberSpecializationInfo *MSInfo = Var->getMemberSpecializationInfo(); |
| 10870 | assert(MSInfo && "Missing member specialization information?"); |
Richard Smith | 37ce010 | 2012-02-15 02:42:50 +0000 | [diff] [blame] | 10871 | bool AlreadyInstantiated = !MSInfo->getPointOfInstantiation().isInvalid(); |
| 10872 | if (MSInfo->getTemplateSpecializationKind() == TSK_ImplicitInstantiation && |
Daniel Dunbar | 3d13c5a | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 10873 | (!AlreadyInstantiated || |
| 10874 | Var->isUsableInConstantExpressions(SemaRef.Context))) { |
Richard Smith | 37ce010 | 2012-02-15 02:42:50 +0000 | [diff] [blame] | 10875 | if (!AlreadyInstantiated) { |
| 10876 | // This is a modification of an existing AST node. Notify listeners. |
| 10877 | if (ASTMutationListener *L = SemaRef.getASTMutationListener()) |
| 10878 | L->StaticDataMemberInstantiated(Var); |
| 10879 | MSInfo->setPointOfInstantiation(Loc); |
| 10880 | } |
| 10881 | SourceLocation PointOfInstantiation = MSInfo->getPointOfInstantiation(); |
Daniel Dunbar | 3d13c5a | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 10882 | if (Var->isUsableInConstantExpressions(SemaRef.Context)) |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10883 | // Do not defer instantiations of variables which could be used in a |
| 10884 | // constant expression. |
Richard Smith | 37ce010 | 2012-02-15 02:42:50 +0000 | [diff] [blame] | 10885 | SemaRef.InstantiateStaticDataMemberDefinition(PointOfInstantiation,Var); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10886 | else |
Richard Smith | 37ce010 | 2012-02-15 02:42:50 +0000 | [diff] [blame] | 10887 | SemaRef.PendingInstantiations.push_back( |
| 10888 | std::make_pair(Var, PointOfInstantiation)); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10889 | } |
| 10890 | } |
| 10891 | |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10892 | // Per C++11 [basic.def.odr], a variable is odr-used "unless it is |
| 10893 | // an object that satisfies the requirements for appearing in a |
| 10894 | // constant expression (5.19) and the lvalue-to-rvalue conversion (4.1) |
| 10895 | // is immediately applied." We check the first part here, and |
| 10896 | // Sema::UpdateMarkingForLValueToRValue deals with the second part. |
| 10897 | // Note that we use the C++11 definition everywhere because nothing in |
Richard Smith | 1658133 | 2012-03-02 04:14:40 +0000 | [diff] [blame] | 10898 | // C++03 depends on whether we get the C++03 version correct. This does not |
| 10899 | // apply to references, since they are not objects. |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10900 | const VarDecl *DefVD; |
Richard Smith | 1658133 | 2012-03-02 04:14:40 +0000 | [diff] [blame] | 10901 | if (E && !isa<ParmVarDecl>(Var) && !Var->getType()->isReferenceType() && |
Daniel Dunbar | 3d13c5a | 2012-03-09 01:51:51 +0000 | [diff] [blame] | 10902 | Var->isUsableInConstantExpressions(SemaRef.Context) && |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10903 | Var->getAnyInitializer(DefVD) && DefVD->checkInitIsICE()) |
| 10904 | SemaRef.MaybeODRUseExprs.insert(E); |
| 10905 | else |
| 10906 | MarkVarDeclODRUsed(SemaRef, Var, Loc); |
| 10907 | } |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10908 | |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10909 | /// \brief Mark a variable referenced, and check whether it is odr-used |
| 10910 | /// (C++ [basic.def.odr]p2, C99 6.9p3). Note that this should not be |
| 10911 | /// used directly for normal expressions referring to VarDecl. |
| 10912 | void Sema::MarkVariableReferenced(SourceLocation Loc, VarDecl *Var) { |
| 10913 | DoMarkVarDeclReferenced(*this, Loc, Var, 0); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10914 | } |
| 10915 | |
| 10916 | static void MarkExprReferenced(Sema &SemaRef, SourceLocation Loc, |
| 10917 | Decl *D, Expr *E) { |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 10918 | if (VarDecl *Var = dyn_cast<VarDecl>(D)) { |
| 10919 | DoMarkVarDeclReferenced(SemaRef, Loc, Var, E); |
| 10920 | return; |
| 10921 | } |
| 10922 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10923 | SemaRef.MarkAnyDeclReferenced(Loc, D); |
Rafael Espindola | 0b4fe50 | 2012-06-26 17:45:31 +0000 | [diff] [blame] | 10924 | |
| 10925 | // If this is a call to a method via a cast, also mark the method in the |
| 10926 | // derived class used in case codegen can devirtualize the call. |
| 10927 | const MemberExpr *ME = dyn_cast<MemberExpr>(E); |
| 10928 | if (!ME) |
| 10929 | return; |
| 10930 | CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(ME->getMemberDecl()); |
| 10931 | if (!MD) |
| 10932 | return; |
| 10933 | const Expr *Base = ME->getBase(); |
Rafael Espindola | 8d852e3 | 2012-06-27 18:18:05 +0000 | [diff] [blame] | 10934 | const CXXRecordDecl *MostDerivedClassDecl = Base->getBestDynamicClassType(); |
Rafael Espindola | 0b4fe50 | 2012-06-26 17:45:31 +0000 | [diff] [blame] | 10935 | if (!MostDerivedClassDecl) |
| 10936 | return; |
| 10937 | CXXMethodDecl *DM = MD->getCorrespondingMethodInClass(MostDerivedClassDecl); |
Rafael Espindola | 0713d99 | 2012-06-27 17:44:39 +0000 | [diff] [blame] | 10938 | if (!DM) |
| 10939 | return; |
Rafael Espindola | 0b4fe50 | 2012-06-26 17:45:31 +0000 | [diff] [blame] | 10940 | SemaRef.MarkAnyDeclReferenced(Loc, DM); |
Douglas Gregor | f6e2e02 | 2012-02-16 01:06:16 +0000 | [diff] [blame] | 10941 | } |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10942 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10943 | /// \brief Perform reference-marking and odr-use handling for a DeclRefExpr. |
| 10944 | void Sema::MarkDeclRefReferenced(DeclRefExpr *E) { |
| 10945 | MarkExprReferenced(*this, E->getLocation(), E->getDecl(), E); |
| 10946 | } |
| 10947 | |
| 10948 | /// \brief Perform reference-marking and odr-use handling for a MemberExpr. |
| 10949 | void Sema::MarkMemberReferenced(MemberExpr *E) { |
| 10950 | MarkExprReferenced(*this, E->getMemberLoc(), E->getMemberDecl(), E); |
| 10951 | } |
| 10952 | |
Douglas Gregor | 73d9092 | 2012-02-10 09:26:04 +0000 | [diff] [blame] | 10953 | /// \brief Perform marking for a reference to an arbitrary declaration. It |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 10954 | /// marks the declaration referenced, and performs odr-use checking for functions |
| 10955 | /// and variables. This method should not be used when building an normal |
| 10956 | /// expression which refers to a variable. |
| 10957 | void Sema::MarkAnyDeclReferenced(SourceLocation Loc, Decl *D) { |
| 10958 | if (VarDecl *VD = dyn_cast<VarDecl>(D)) |
| 10959 | MarkVariableReferenced(Loc, VD); |
| 10960 | else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) |
| 10961 | MarkFunctionReferenced(Loc, FD); |
| 10962 | else |
| 10963 | D->setReferenced(); |
Douglas Gregor | e0762c9 | 2009-06-19 23:52:42 +0000 | [diff] [blame] | 10964 | } |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 10965 | |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10966 | namespace { |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10967 | // Mark all of the declarations referenced |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10968 | // FIXME: Not fully implemented yet! We need to have a better understanding |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10969 | // of when we're entering |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10970 | class MarkReferencedDecls : public RecursiveASTVisitor<MarkReferencedDecls> { |
| 10971 | Sema &S; |
| 10972 | SourceLocation Loc; |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10973 | |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10974 | public: |
| 10975 | typedef RecursiveASTVisitor<MarkReferencedDecls> Inherited; |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10976 | |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10977 | MarkReferencedDecls(Sema &S, SourceLocation Loc) : S(S), Loc(Loc) { } |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10978 | |
| 10979 | bool TraverseTemplateArgument(const TemplateArgument &Arg); |
| 10980 | bool TraverseRecordType(RecordType *T); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10981 | }; |
| 10982 | } |
| 10983 | |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10984 | bool MarkReferencedDecls::TraverseTemplateArgument( |
| 10985 | const TemplateArgument &Arg) { |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10986 | if (Arg.getKind() == TemplateArgument::Declaration) { |
Douglas Gregor | d2008e2 | 2012-04-06 22:40:38 +0000 | [diff] [blame] | 10987 | if (Decl *D = Arg.getAsDecl()) |
| 10988 | S.MarkAnyDeclReferenced(Loc, D); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10989 | } |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10990 | |
| 10991 | return Inherited::TraverseTemplateArgument(Arg); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10992 | } |
| 10993 | |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 10994 | bool MarkReferencedDecls::TraverseRecordType(RecordType *T) { |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10995 | if (ClassTemplateSpecializationDecl *Spec |
| 10996 | = dyn_cast<ClassTemplateSpecializationDecl>(T->getDecl())) { |
| 10997 | const TemplateArgumentList &Args = Spec->getTemplateArgs(); |
Douglas Gregor | 910f800 | 2010-11-07 23:05:16 +0000 | [diff] [blame] | 10998 | return TraverseTemplateArguments(Args.data(), Args.size()); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 10999 | } |
| 11000 | |
Chandler Carruth | e3e210c | 2010-06-10 10:31:57 +0000 | [diff] [blame] | 11001 | return true; |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 11002 | } |
| 11003 | |
| 11004 | void Sema::MarkDeclarationsReferencedInType(SourceLocation Loc, QualType T) { |
| 11005 | MarkReferencedDecls Marker(*this, Loc); |
Chandler Carruth | dfc35e3 | 2010-06-09 08:17:30 +0000 | [diff] [blame] | 11006 | Marker.TraverseType(Context.getCanonicalType(T)); |
Douglas Gregor | b4eeaff | 2010-05-07 23:12:07 +0000 | [diff] [blame] | 11007 | } |
| 11008 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11009 | namespace { |
| 11010 | /// \brief Helper class that marks all of the declarations referenced by |
| 11011 | /// potentially-evaluated subexpressions as "referenced". |
| 11012 | class EvaluatedExprMarker : public EvaluatedExprVisitor<EvaluatedExprMarker> { |
| 11013 | Sema &S; |
Douglas Gregor | f4b7de1 | 2012-02-21 19:11:17 +0000 | [diff] [blame] | 11014 | bool SkipLocalVariables; |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11015 | |
| 11016 | public: |
| 11017 | typedef EvaluatedExprVisitor<EvaluatedExprMarker> Inherited; |
| 11018 | |
Douglas Gregor | f4b7de1 | 2012-02-21 19:11:17 +0000 | [diff] [blame] | 11019 | EvaluatedExprMarker(Sema &S, bool SkipLocalVariables) |
| 11020 | : Inherited(S.Context), S(S), SkipLocalVariables(SkipLocalVariables) { } |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11021 | |
| 11022 | void VisitDeclRefExpr(DeclRefExpr *E) { |
Douglas Gregor | f4b7de1 | 2012-02-21 19:11:17 +0000 | [diff] [blame] | 11023 | // If we were asked not to visit local variables, don't. |
| 11024 | if (SkipLocalVariables) { |
| 11025 | if (VarDecl *VD = dyn_cast<VarDecl>(E->getDecl())) |
| 11026 | if (VD->hasLocalStorage()) |
| 11027 | return; |
| 11028 | } |
| 11029 | |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11030 | S.MarkDeclRefReferenced(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11031 | } |
| 11032 | |
| 11033 | void VisitMemberExpr(MemberExpr *E) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11034 | S.MarkMemberReferenced(E); |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 11035 | Inherited::VisitMemberExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11036 | } |
| 11037 | |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 11038 | void VisitCXXBindTemporaryExpr(CXXBindTemporaryExpr *E) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11039 | S.MarkFunctionReferenced(E->getLocStart(), |
John McCall | 80ee6e8 | 2011-11-10 05:35:25 +0000 | [diff] [blame] | 11040 | const_cast<CXXDestructorDecl*>(E->getTemporary()->getDestructor())); |
| 11041 | Visit(E->getSubExpr()); |
| 11042 | } |
| 11043 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11044 | void VisitCXXNewExpr(CXXNewExpr *E) { |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11045 | if (E->getOperatorNew()) |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11046 | S.MarkFunctionReferenced(E->getLocStart(), E->getOperatorNew()); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11047 | if (E->getOperatorDelete()) |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11048 | S.MarkFunctionReferenced(E->getLocStart(), E->getOperatorDelete()); |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 11049 | Inherited::VisitCXXNewExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11050 | } |
Sebastian Redl | 2aed8b8 | 2012-02-16 12:22:20 +0000 | [diff] [blame] | 11051 | |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11052 | void VisitCXXDeleteExpr(CXXDeleteExpr *E) { |
| 11053 | if (E->getOperatorDelete()) |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11054 | S.MarkFunctionReferenced(E->getLocStart(), E->getOperatorDelete()); |
Douglas Gregor | 5833b0b | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 11055 | QualType Destroyed = S.Context.getBaseElementType(E->getDestroyedType()); |
| 11056 | if (const RecordType *DestroyedRec = Destroyed->getAs<RecordType>()) { |
| 11057 | CXXRecordDecl *Record = cast<CXXRecordDecl>(DestroyedRec->getDecl()); |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11058 | S.MarkFunctionReferenced(E->getLocStart(), |
Douglas Gregor | 5833b0b | 2010-09-14 22:55:20 +0000 | [diff] [blame] | 11059 | S.LookupDestructor(Record)); |
| 11060 | } |
| 11061 | |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 11062 | Inherited::VisitCXXDeleteExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11063 | } |
| 11064 | |
| 11065 | void VisitCXXConstructExpr(CXXConstructExpr *E) { |
Eli Friedman | 5f2987c | 2012-02-02 03:46:19 +0000 | [diff] [blame] | 11066 | S.MarkFunctionReferenced(E->getLocStart(), E->getConstructor()); |
Douglas Gregor | 4fcf5b2 | 2010-09-11 23:32:50 +0000 | [diff] [blame] | 11067 | Inherited::VisitCXXConstructExpr(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11068 | } |
| 11069 | |
Douglas Gregor | 102ff97 | 2010-10-19 17:17:35 +0000 | [diff] [blame] | 11070 | void VisitCXXDefaultArgExpr(CXXDefaultArgExpr *E) { |
| 11071 | Visit(E->getExpr()); |
| 11072 | } |
Eli Friedman | d2cce13 | 2012-02-02 23:15:15 +0000 | [diff] [blame] | 11073 | |
| 11074 | void VisitImplicitCastExpr(ImplicitCastExpr *E) { |
| 11075 | Inherited::VisitImplicitCastExpr(E); |
| 11076 | |
| 11077 | if (E->getCastKind() == CK_LValueToRValue) |
| 11078 | S.UpdateMarkingForLValueToRValue(E->getSubExpr()); |
| 11079 | } |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11080 | }; |
| 11081 | } |
| 11082 | |
| 11083 | /// \brief Mark any declarations that appear within this expression or any |
| 11084 | /// potentially-evaluated subexpressions as "referenced". |
Douglas Gregor | f4b7de1 | 2012-02-21 19:11:17 +0000 | [diff] [blame] | 11085 | /// |
| 11086 | /// \param SkipLocalVariables If true, don't mark local variables as |
| 11087 | /// 'referenced'. |
| 11088 | void Sema::MarkDeclarationsReferencedInExpr(Expr *E, |
| 11089 | bool SkipLocalVariables) { |
| 11090 | EvaluatedExprMarker(*this, SkipLocalVariables).Visit(E); |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11091 | } |
| 11092 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 11093 | /// \brief Emit a diagnostic that describes an effect on the run-time behavior |
| 11094 | /// of the program being compiled. |
| 11095 | /// |
| 11096 | /// This routine emits the given diagnostic when the code currently being |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 11097 | /// type-checked is "potentially evaluated", meaning that there is a |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 11098 | /// possibility that the code will actually be executable. Code in sizeof() |
| 11099 | /// expressions, code used only during overload resolution, etc., are not |
| 11100 | /// potentially evaluated. This routine will suppress such diagnostics or, |
| 11101 | /// in the absolutely nutty case of potentially potentially evaluated |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 11102 | /// expressions (C++ typeid), queue the diagnostic to potentially emit it |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 11103 | /// later. |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 11104 | /// |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 11105 | /// This routine should be used for all diagnostics that describe the run-time |
| 11106 | /// behavior of a program, such as passing a non-POD value through an ellipsis. |
| 11107 | /// Failure to do so will likely result in spurious diagnostics or failures |
| 11108 | /// during overload resolution or within sizeof/alignof/typeof/typeid. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11109 | bool Sema::DiagRuntimeBehavior(SourceLocation Loc, const Stmt *Statement, |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 11110 | const PartialDiagnostic &PD) { |
John McCall | f85e193 | 2011-06-15 23:02:42 +0000 | [diff] [blame] | 11111 | switch (ExprEvalContexts.back().Context) { |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 11112 | case Unevaluated: |
| 11113 | // The argument will never be evaluated, so don't complain. |
| 11114 | break; |
Kovarththanan Rajaratnam | 1935754 | 2010-03-13 10:17:05 +0000 | [diff] [blame] | 11115 | |
Richard Smith | f6702a3 | 2011-12-20 02:08:33 +0000 | [diff] [blame] | 11116 | case ConstantEvaluated: |
| 11117 | // Relevant diagnostics should be produced by constant evaluation. |
| 11118 | break; |
| 11119 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 11120 | case PotentiallyEvaluated: |
Douglas Gregor | be0f7bd | 2010-09-11 20:24:53 +0000 | [diff] [blame] | 11121 | case PotentiallyEvaluatedIfUsed: |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11122 | if (Statement && getCurFunctionOrMethodDecl()) { |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 11123 | FunctionScopes.back()->PossiblyUnreachableDiags. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11124 | push_back(sema::PossiblyUnreachableDiag(PD, Loc, Statement)); |
Ted Kremenek | 351ba91 | 2011-02-23 01:52:04 +0000 | [diff] [blame] | 11125 | } |
| 11126 | else |
| 11127 | Diag(Loc, PD); |
| 11128 | |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 11129 | return true; |
Douglas Gregor | 1c7c3fb | 2009-12-22 01:01:55 +0000 | [diff] [blame] | 11130 | } |
| 11131 | |
| 11132 | return false; |
| 11133 | } |
| 11134 | |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 11135 | bool Sema::CheckCallReturnType(QualType ReturnType, SourceLocation Loc, |
| 11136 | CallExpr *CE, FunctionDecl *FD) { |
| 11137 | if (ReturnType->isVoidType() || !ReturnType->isIncompleteType()) |
| 11138 | return false; |
| 11139 | |
Richard Smith | 76f3f69 | 2012-02-22 02:04:18 +0000 | [diff] [blame] | 11140 | // If we're inside a decltype's expression, don't check for a valid return |
| 11141 | // type or construct temporaries until we know whether this is the last call. |
| 11142 | if (ExprEvalContexts.back().IsDecltype) { |
| 11143 | ExprEvalContexts.back().DelayedDecltypeCalls.push_back(CE); |
| 11144 | return false; |
| 11145 | } |
| 11146 | |
Douglas Gregor | f502d8e | 2012-05-04 16:48:41 +0000 | [diff] [blame] | 11147 | class CallReturnIncompleteDiagnoser : public TypeDiagnoser { |
Douglas Gregor | d10099e | 2012-05-04 16:32:21 +0000 | [diff] [blame] | 11148 | FunctionDecl *FD; |
| 11149 | CallExpr *CE; |
| 11150 | |
| 11151 | public: |
| 11152 | CallReturnIncompleteDiagnoser(FunctionDecl *FD, CallExpr *CE) |
| 11153 | : FD(FD), CE(CE) { } |
| 11154 | |
| 11155 | virtual void diagnose(Sema &S, SourceLocation Loc, QualType T) { |
| 11156 | if (!FD) { |
| 11157 | S.Diag(Loc, diag::err_call_incomplete_return) |
| 11158 | << T << CE->getSourceRange(); |
| 11159 | return; |
| 11160 | } |
| 11161 | |
| 11162 | S.Diag(Loc, diag::err_call_function_incomplete_return) |
| 11163 | << CE->getSourceRange() << FD->getDeclName() << T; |
| 11164 | S.Diag(FD->getLocation(), |
| 11165 | diag::note_function_with_incomplete_return_type_declared_here) |
| 11166 | << FD->getDeclName(); |
| 11167 | } |
| 11168 | } Diagnoser(FD, CE); |
| 11169 | |
| 11170 | if (RequireCompleteType(Loc, ReturnType, Diagnoser)) |
Anders Carlsson | 8c8d919 | 2009-10-09 23:51:55 +0000 | [diff] [blame] | 11171 | return true; |
| 11172 | |
| 11173 | return false; |
| 11174 | } |
| 11175 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 11176 | // Diagnose the s/=/==/ and s/\|=/!=/ typos. Note that adding parentheses |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 11177 | // will prevent this condition from triggering, which is what we want. |
| 11178 | void Sema::DiagnoseAssignmentAsCondition(Expr *E) { |
| 11179 | SourceLocation Loc; |
| 11180 | |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 11181 | unsigned diagnostic = diag::warn_condition_is_assignment; |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 11182 | bool IsOrAssign = false; |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 11183 | |
Chandler Carruth | b33c19f | 2011-08-16 22:30:10 +0000 | [diff] [blame] | 11184 | if (BinaryOperator *Op = dyn_cast<BinaryOperator>(E)) { |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 11185 | if (Op->getOpcode() != BO_Assign && Op->getOpcode() != BO_OrAssign) |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 11186 | return; |
| 11187 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 11188 | IsOrAssign = Op->getOpcode() == BO_OrAssign; |
| 11189 | |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 11190 | // Greylist some idioms by putting them into a warning subcategory. |
| 11191 | if (ObjCMessageExpr *ME |
| 11192 | = dyn_cast<ObjCMessageExpr>(Op->getRHS()->IgnoreParenCasts())) { |
| 11193 | Selector Sel = ME->getSelector(); |
| 11194 | |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 11195 | // self = [<foo> init...] |
Douglas Gregor | c737acb | 2011-09-27 16:10:05 +0000 | [diff] [blame] | 11196 | if (isSelfExpr(Op->getLHS()) && Sel.getNameForSlot(0).startswith("init")) |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 11197 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 11198 | |
| 11199 | // <foo> = [<bar> nextObject] |
Douglas Gregor | 813d834 | 2011-02-18 22:29:55 +0000 | [diff] [blame] | 11200 | else if (Sel.isUnarySelector() && Sel.getNameForSlot(0) == "nextObject") |
John McCall | c8d8ac5 | 2009-11-12 00:06:05 +0000 | [diff] [blame] | 11201 | diagnostic = diag::warn_condition_is_idiomatic_assignment; |
| 11202 | } |
John McCall | a52ef08 | 2009-11-11 02:41:58 +0000 | [diff] [blame] | 11203 | |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 11204 | Loc = Op->getOperatorLoc(); |
Chandler Carruth | b33c19f | 2011-08-16 22:30:10 +0000 | [diff] [blame] | 11205 | } else if (CXXOperatorCallExpr *Op = dyn_cast<CXXOperatorCallExpr>(E)) { |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 11206 | if (Op->getOperator() != OO_Equal && Op->getOperator() != OO_PipeEqual) |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 11207 | return; |
| 11208 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 11209 | IsOrAssign = Op->getOperator() == OO_PipeEqual; |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 11210 | Loc = Op->getOperatorLoc(); |
Fariborz Jahanian | a414a2f | 2012-08-29 17:17:11 +0000 | [diff] [blame] | 11211 | } else if (PseudoObjectExpr *POE = dyn_cast<PseudoObjectExpr>(E)) |
| 11212 | return DiagnoseAssignmentAsCondition(POE->getSyntacticForm()); |
| 11213 | else { |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 11214 | // Not an assignment. |
| 11215 | return; |
| 11216 | } |
| 11217 | |
Douglas Gregor | 55b3884 | 2010-04-14 16:09:52 +0000 | [diff] [blame] | 11218 | Diag(Loc, diagnostic) << E->getSourceRange(); |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 11219 | |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11220 | SourceLocation Open = E->getLocStart(); |
Argyrios Kyrtzidis | abdd3b3 | 2011-04-25 23:01:29 +0000 | [diff] [blame] | 11221 | SourceLocation Close = PP.getLocForEndOfToken(E->getSourceRange().getEnd()); |
| 11222 | Diag(Loc, diag::note_condition_assign_silence) |
| 11223 | << FixItHint::CreateInsertion(Open, "(") |
| 11224 | << FixItHint::CreateInsertion(Close, ")"); |
| 11225 | |
Douglas Gregor | 92c3a04 | 2011-01-19 16:50:08 +0000 | [diff] [blame] | 11226 | if (IsOrAssign) |
| 11227 | Diag(Loc, diag::note_condition_or_assign_to_comparison) |
| 11228 | << FixItHint::CreateReplacement(Loc, "!="); |
| 11229 | else |
| 11230 | Diag(Loc, diag::note_condition_assign_to_comparison) |
| 11231 | << FixItHint::CreateReplacement(Loc, "=="); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 11232 | } |
| 11233 | |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 11234 | /// \brief Redundant parentheses over an equality comparison can indicate |
| 11235 | /// that the user intended an assignment used as condition. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11236 | void Sema::DiagnoseEqualityWithExtraParens(ParenExpr *ParenE) { |
Argyrios Kyrtzidis | cf1620a | 2011-02-01 22:23:56 +0000 | [diff] [blame] | 11237 | // Don't warn if the parens came from a macro. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11238 | SourceLocation parenLoc = ParenE->getLocStart(); |
Argyrios Kyrtzidis | cf1620a | 2011-02-01 22:23:56 +0000 | [diff] [blame] | 11239 | if (parenLoc.isInvalid() || parenLoc.isMacroID()) |
| 11240 | return; |
Argyrios Kyrtzidis | 170a6a2 | 2011-03-28 23:52:04 +0000 | [diff] [blame] | 11241 | // Don't warn for dependent expressions. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11242 | if (ParenE->isTypeDependent()) |
Argyrios Kyrtzidis | 170a6a2 | 2011-03-28 23:52:04 +0000 | [diff] [blame] | 11243 | return; |
Argyrios Kyrtzidis | cf1620a | 2011-02-01 22:23:56 +0000 | [diff] [blame] | 11244 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11245 | Expr *E = ParenE->IgnoreParens(); |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 11246 | |
| 11247 | if (BinaryOperator *opE = dyn_cast<BinaryOperator>(E)) |
Argyrios Kyrtzidis | 70f2330 | 2011-02-01 19:32:59 +0000 | [diff] [blame] | 11248 | if (opE->getOpcode() == BO_EQ && |
| 11249 | opE->getLHS()->IgnoreParenImpCasts()->isModifiableLvalue(Context) |
| 11250 | == Expr::MLV_Valid) { |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 11251 | SourceLocation Loc = opE->getOperatorLoc(); |
Ted Kremenek | 006ae38 | 2011-02-01 22:36:09 +0000 | [diff] [blame] | 11252 | |
Ted Kremenek | f7275cd | 2011-02-02 02:20:30 +0000 | [diff] [blame] | 11253 | Diag(Loc, diag::warn_equality_with_extra_parens) << E->getSourceRange(); |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11254 | SourceRange ParenERange = ParenE->getSourceRange(); |
Ted Kremenek | f7275cd | 2011-02-02 02:20:30 +0000 | [diff] [blame] | 11255 | Diag(Loc, diag::note_equality_comparison_silence) |
Daniel Dunbar | 96a0014 | 2012-03-09 18:35:03 +0000 | [diff] [blame] | 11256 | << FixItHint::CreateRemoval(ParenERange.getBegin()) |
| 11257 | << FixItHint::CreateRemoval(ParenERange.getEnd()); |
Argyrios Kyrtzidis | abdd3b3 | 2011-04-25 23:01:29 +0000 | [diff] [blame] | 11258 | Diag(Loc, diag::note_equality_comparison_to_assign) |
| 11259 | << FixItHint::CreateReplacement(Loc, "="); |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 11260 | } |
| 11261 | } |
| 11262 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11263 | ExprResult Sema::CheckBooleanCondition(Expr *E, SourceLocation Loc) { |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 11264 | DiagnoseAssignmentAsCondition(E); |
Argyrios Kyrtzidis | 0e2dc3a | 2011-02-01 18:24:22 +0000 | [diff] [blame] | 11265 | if (ParenExpr *parenE = dyn_cast<ParenExpr>(E)) |
| 11266 | DiagnoseEqualityWithExtraParens(parenE); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 11267 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11268 | ExprResult result = CheckPlaceholderExpr(E); |
| 11269 | if (result.isInvalid()) return ExprError(); |
| 11270 | E = result.take(); |
Argyrios Kyrtzidis | 11ab790 | 2010-11-01 18:49:26 +0000 | [diff] [blame] | 11271 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11272 | if (!E->isTypeDependent()) { |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11273 | if (getLangOpts().CPlusPlus) |
John McCall | f6a1648 | 2010-12-04 03:47:34 +0000 | [diff] [blame] | 11274 | return CheckCXXBooleanCondition(E); // C++ 6.4p4 |
| 11275 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11276 | ExprResult ERes = DefaultFunctionArrayLvalueConversion(E); |
| 11277 | if (ERes.isInvalid()) |
| 11278 | return ExprError(); |
| 11279 | E = ERes.take(); |
John McCall | abc56c7 | 2010-12-04 06:09:13 +0000 | [diff] [blame] | 11280 | |
| 11281 | QualType T = E->getType(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11282 | if (!T->isScalarType()) { // C99 6.8.4.1p1 |
| 11283 | Diag(Loc, diag::err_typecheck_statement_requires_scalar) |
| 11284 | << T << E->getSourceRange(); |
| 11285 | return ExprError(); |
| 11286 | } |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 11287 | } |
| 11288 | |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11289 | return Owned(E); |
John McCall | 5a881bb | 2009-10-12 21:59:07 +0000 | [diff] [blame] | 11290 | } |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 11291 | |
John McCall | 60d7b3a | 2010-08-24 06:29:42 +0000 | [diff] [blame] | 11292 | ExprResult Sema::ActOnBooleanCondition(Scope *S, SourceLocation Loc, |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11293 | Expr *SubExpr) { |
| 11294 | if (!SubExpr) |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 11295 | return ExprError(); |
John Wiegley | 429bb27 | 2011-04-08 18:41:53 +0000 | [diff] [blame] | 11296 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11297 | return CheckBooleanCondition(SubExpr, Loc); |
Douglas Gregor | 586596f | 2010-05-06 17:25:47 +0000 | [diff] [blame] | 11298 | } |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 11299 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11300 | namespace { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11301 | /// A visitor for rebuilding a call to an __unknown_any expression |
| 11302 | /// to have an appropriate type. |
| 11303 | struct RebuildUnknownAnyFunction |
| 11304 | : StmtVisitor<RebuildUnknownAnyFunction, ExprResult> { |
| 11305 | |
| 11306 | Sema &S; |
| 11307 | |
| 11308 | RebuildUnknownAnyFunction(Sema &S) : S(S) {} |
| 11309 | |
| 11310 | ExprResult VisitStmt(Stmt *S) { |
| 11311 | llvm_unreachable("unexpected statement!"); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11312 | } |
| 11313 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11314 | ExprResult VisitExpr(Expr *E) { |
| 11315 | S.Diag(E->getExprLoc(), diag::err_unsupported_unknown_any_call) |
| 11316 | << E->getSourceRange(); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11317 | return ExprError(); |
| 11318 | } |
| 11319 | |
| 11320 | /// Rebuild an expression which simply semantically wraps another |
| 11321 | /// expression which it shares the type and value kind of. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11322 | template <class T> ExprResult rebuildSugarExpr(T *E) { |
| 11323 | ExprResult SubResult = Visit(E->getSubExpr()); |
| 11324 | if (SubResult.isInvalid()) return ExprError(); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11325 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11326 | Expr *SubExpr = SubResult.take(); |
| 11327 | E->setSubExpr(SubExpr); |
| 11328 | E->setType(SubExpr->getType()); |
| 11329 | E->setValueKind(SubExpr->getValueKind()); |
| 11330 | assert(E->getObjectKind() == OK_Ordinary); |
| 11331 | return E; |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11332 | } |
| 11333 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11334 | ExprResult VisitParenExpr(ParenExpr *E) { |
| 11335 | return rebuildSugarExpr(E); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11336 | } |
| 11337 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11338 | ExprResult VisitUnaryExtension(UnaryOperator *E) { |
| 11339 | return rebuildSugarExpr(E); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11340 | } |
| 11341 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11342 | ExprResult VisitUnaryAddrOf(UnaryOperator *E) { |
| 11343 | ExprResult SubResult = Visit(E->getSubExpr()); |
| 11344 | if (SubResult.isInvalid()) return ExprError(); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11345 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11346 | Expr *SubExpr = SubResult.take(); |
| 11347 | E->setSubExpr(SubExpr); |
| 11348 | E->setType(S.Context.getPointerType(SubExpr->getType())); |
| 11349 | assert(E->getValueKind() == VK_RValue); |
| 11350 | assert(E->getObjectKind() == OK_Ordinary); |
| 11351 | return E; |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11352 | } |
| 11353 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11354 | ExprResult resolveDecl(Expr *E, ValueDecl *VD) { |
| 11355 | if (!isa<FunctionDecl>(VD)) return VisitExpr(E); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11356 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11357 | E->setType(VD->getType()); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11358 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11359 | assert(E->getValueKind() == VK_RValue); |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11360 | if (S.getLangOpts().CPlusPlus && |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11361 | !(isa<CXXMethodDecl>(VD) && |
| 11362 | cast<CXXMethodDecl>(VD)->isInstance())) |
| 11363 | E->setValueKind(VK_LValue); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11364 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11365 | return E; |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11366 | } |
| 11367 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11368 | ExprResult VisitMemberExpr(MemberExpr *E) { |
| 11369 | return resolveDecl(E, E->getMemberDecl()); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11370 | } |
| 11371 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11372 | ExprResult VisitDeclRefExpr(DeclRefExpr *E) { |
| 11373 | return resolveDecl(E, E->getDecl()); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11374 | } |
| 11375 | }; |
| 11376 | } |
| 11377 | |
| 11378 | /// Given a function expression of unknown-any type, try to rebuild it |
| 11379 | /// to have a function type. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11380 | static ExprResult rebuildUnknownAnyFunction(Sema &S, Expr *FunctionExpr) { |
| 11381 | ExprResult Result = RebuildUnknownAnyFunction(S).Visit(FunctionExpr); |
| 11382 | if (Result.isInvalid()) return ExprError(); |
| 11383 | return S.DefaultFunctionArrayConversion(Result.take()); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11384 | } |
| 11385 | |
| 11386 | namespace { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11387 | /// A visitor for rebuilding an expression of type __unknown_anytype |
| 11388 | /// into one which resolves the type directly on the referring |
| 11389 | /// expression. Strict preservation of the original source |
| 11390 | /// structure is not a goal. |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11391 | struct RebuildUnknownAnyExpr |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 11392 | : StmtVisitor<RebuildUnknownAnyExpr, ExprResult> { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11393 | |
| 11394 | Sema &S; |
| 11395 | |
| 11396 | /// The current destination type. |
| 11397 | QualType DestType; |
| 11398 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11399 | RebuildUnknownAnyExpr(Sema &S, QualType CastType) |
| 11400 | : S(S), DestType(CastType) {} |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11401 | |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 11402 | ExprResult VisitStmt(Stmt *S) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11403 | llvm_unreachable("unexpected statement!"); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11404 | } |
| 11405 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11406 | ExprResult VisitExpr(Expr *E) { |
| 11407 | S.Diag(E->getExprLoc(), diag::err_unsupported_unknown_any_expr) |
| 11408 | << E->getSourceRange(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11409 | return ExprError(); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11410 | } |
| 11411 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11412 | ExprResult VisitCallExpr(CallExpr *E); |
| 11413 | ExprResult VisitObjCMessageExpr(ObjCMessageExpr *E); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11414 | |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 11415 | /// Rebuild an expression which simply semantically wraps another |
| 11416 | /// expression which it shares the type and value kind of. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11417 | template <class T> ExprResult rebuildSugarExpr(T *E) { |
| 11418 | ExprResult SubResult = Visit(E->getSubExpr()); |
| 11419 | if (SubResult.isInvalid()) return ExprError(); |
| 11420 | Expr *SubExpr = SubResult.take(); |
| 11421 | E->setSubExpr(SubExpr); |
| 11422 | E->setType(SubExpr->getType()); |
| 11423 | E->setValueKind(SubExpr->getValueKind()); |
| 11424 | assert(E->getObjectKind() == OK_Ordinary); |
| 11425 | return E; |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 11426 | } |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11427 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11428 | ExprResult VisitParenExpr(ParenExpr *E) { |
| 11429 | return rebuildSugarExpr(E); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 11430 | } |
| 11431 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11432 | ExprResult VisitUnaryExtension(UnaryOperator *E) { |
| 11433 | return rebuildSugarExpr(E); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 11434 | } |
| 11435 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11436 | ExprResult VisitUnaryAddrOf(UnaryOperator *E) { |
| 11437 | const PointerType *Ptr = DestType->getAs<PointerType>(); |
| 11438 | if (!Ptr) { |
| 11439 | S.Diag(E->getOperatorLoc(), diag::err_unknown_any_addrof) |
| 11440 | << E->getSourceRange(); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11441 | return ExprError(); |
| 11442 | } |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11443 | assert(E->getValueKind() == VK_RValue); |
| 11444 | assert(E->getObjectKind() == OK_Ordinary); |
| 11445 | E->setType(DestType); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11446 | |
| 11447 | // Build the sub-expression as if it were an object of the pointee type. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11448 | DestType = Ptr->getPointeeType(); |
| 11449 | ExprResult SubResult = Visit(E->getSubExpr()); |
| 11450 | if (SubResult.isInvalid()) return ExprError(); |
| 11451 | E->setSubExpr(SubResult.take()); |
| 11452 | return E; |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11453 | } |
| 11454 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11455 | ExprResult VisitImplicitCastExpr(ImplicitCastExpr *E); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 11456 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11457 | ExprResult resolveDecl(Expr *E, ValueDecl *VD); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 11458 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11459 | ExprResult VisitMemberExpr(MemberExpr *E) { |
| 11460 | return resolveDecl(E, E->getMemberDecl()); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11461 | } |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 11462 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11463 | ExprResult VisitDeclRefExpr(DeclRefExpr *E) { |
| 11464 | return resolveDecl(E, E->getDecl()); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11465 | } |
| 11466 | }; |
| 11467 | } |
| 11468 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11469 | /// Rebuilds a call expression which yielded __unknown_anytype. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11470 | ExprResult RebuildUnknownAnyExpr::VisitCallExpr(CallExpr *E) { |
| 11471 | Expr *CalleeExpr = E->getCallee(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11472 | |
| 11473 | enum FnKind { |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 11474 | FK_MemberFunction, |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11475 | FK_FunctionPointer, |
| 11476 | FK_BlockPointer |
| 11477 | }; |
| 11478 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11479 | FnKind Kind; |
| 11480 | QualType CalleeType = CalleeExpr->getType(); |
| 11481 | if (CalleeType == S.Context.BoundMemberTy) { |
| 11482 | assert(isa<CXXMemberCallExpr>(E) || isa<CXXOperatorCallExpr>(E)); |
| 11483 | Kind = FK_MemberFunction; |
| 11484 | CalleeType = Expr::findBoundMemberType(CalleeExpr); |
| 11485 | } else if (const PointerType *Ptr = CalleeType->getAs<PointerType>()) { |
| 11486 | CalleeType = Ptr->getPointeeType(); |
| 11487 | Kind = FK_FunctionPointer; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11488 | } else { |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11489 | CalleeType = CalleeType->castAs<BlockPointerType>()->getPointeeType(); |
| 11490 | Kind = FK_BlockPointer; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11491 | } |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11492 | const FunctionType *FnType = CalleeType->castAs<FunctionType>(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11493 | |
| 11494 | // Verify that this is a legal result type of a function. |
| 11495 | if (DestType->isArrayType() || DestType->isFunctionType()) { |
| 11496 | unsigned diagID = diag::err_func_returning_array_function; |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11497 | if (Kind == FK_BlockPointer) |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11498 | diagID = diag::err_block_returning_array_function; |
| 11499 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11500 | S.Diag(E->getExprLoc(), diagID) |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11501 | << DestType->isFunctionType() << DestType; |
| 11502 | return ExprError(); |
| 11503 | } |
| 11504 | |
| 11505 | // Otherwise, go ahead and set DestType as the call's result. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11506 | E->setType(DestType.getNonLValueExprType(S.Context)); |
| 11507 | E->setValueKind(Expr::getValueKindForType(DestType)); |
| 11508 | assert(E->getObjectKind() == OK_Ordinary); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11509 | |
| 11510 | // Rebuild the function type, replacing the result type with DestType. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11511 | if (const FunctionProtoType *Proto = dyn_cast<FunctionProtoType>(FnType)) |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11512 | DestType = S.Context.getFunctionType(DestType, |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11513 | Proto->arg_type_begin(), |
| 11514 | Proto->getNumArgs(), |
| 11515 | Proto->getExtProtoInfo()); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11516 | else |
| 11517 | DestType = S.Context.getFunctionNoProtoType(DestType, |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11518 | FnType->getExtInfo()); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11519 | |
| 11520 | // Rebuild the appropriate pointer-to-function type. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11521 | switch (Kind) { |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 11522 | case FK_MemberFunction: |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11523 | // Nothing to do. |
| 11524 | break; |
| 11525 | |
| 11526 | case FK_FunctionPointer: |
| 11527 | DestType = S.Context.getPointerType(DestType); |
| 11528 | break; |
| 11529 | |
| 11530 | case FK_BlockPointer: |
| 11531 | DestType = S.Context.getBlockPointerType(DestType); |
| 11532 | break; |
| 11533 | } |
| 11534 | |
| 11535 | // Finally, we can recurse. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11536 | ExprResult CalleeResult = Visit(CalleeExpr); |
| 11537 | if (!CalleeResult.isUsable()) return ExprError(); |
| 11538 | E->setCallee(CalleeResult.take()); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11539 | |
| 11540 | // Bind a temporary if necessary. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11541 | return S.MaybeBindToTemporary(E); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11542 | } |
| 11543 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11544 | ExprResult RebuildUnknownAnyExpr::VisitObjCMessageExpr(ObjCMessageExpr *E) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11545 | // Verify that this is a legal result type of a call. |
| 11546 | if (DestType->isArrayType() || DestType->isFunctionType()) { |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11547 | S.Diag(E->getExprLoc(), diag::err_func_returning_array_function) |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11548 | << DestType->isFunctionType() << DestType; |
| 11549 | return ExprError(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11550 | } |
| 11551 | |
John McCall | 48218c6 | 2011-07-13 17:56:40 +0000 | [diff] [blame] | 11552 | // Rewrite the method result type if available. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11553 | if (ObjCMethodDecl *Method = E->getMethodDecl()) { |
| 11554 | assert(Method->getResultType() == S.Context.UnknownAnyTy); |
| 11555 | Method->setResultType(DestType); |
John McCall | 48218c6 | 2011-07-13 17:56:40 +0000 | [diff] [blame] | 11556 | } |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11557 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11558 | // Change the type of the message. |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11559 | E->setType(DestType.getNonReferenceType()); |
| 11560 | E->setValueKind(Expr::getValueKindForType(DestType)); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11561 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11562 | return S.MaybeBindToTemporary(E); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11563 | } |
| 11564 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11565 | ExprResult RebuildUnknownAnyExpr::VisitImplicitCastExpr(ImplicitCastExpr *E) { |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11566 | // The only case we should ever see here is a function-to-pointer decay. |
Sean Callanan | ba66c6c | 2012-03-06 23:12:57 +0000 | [diff] [blame] | 11567 | if (E->getCastKind() == CK_FunctionToPointerDecay) { |
Sean Callanan | ce9c831 | 2012-03-06 21:34:12 +0000 | [diff] [blame] | 11568 | assert(E->getValueKind() == VK_RValue); |
| 11569 | assert(E->getObjectKind() == OK_Ordinary); |
| 11570 | |
| 11571 | E->setType(DestType); |
| 11572 | |
| 11573 | // Rebuild the sub-expression as the pointee (function) type. |
| 11574 | DestType = DestType->castAs<PointerType>()->getPointeeType(); |
| 11575 | |
| 11576 | ExprResult Result = Visit(E->getSubExpr()); |
| 11577 | if (!Result.isUsable()) return ExprError(); |
| 11578 | |
| 11579 | E->setSubExpr(Result.take()); |
| 11580 | return S.Owned(E); |
Sean Callanan | ba66c6c | 2012-03-06 23:12:57 +0000 | [diff] [blame] | 11581 | } else if (E->getCastKind() == CK_LValueToRValue) { |
Sean Callanan | ce9c831 | 2012-03-06 21:34:12 +0000 | [diff] [blame] | 11582 | assert(E->getValueKind() == VK_RValue); |
| 11583 | assert(E->getObjectKind() == OK_Ordinary); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11584 | |
Sean Callanan | ce9c831 | 2012-03-06 21:34:12 +0000 | [diff] [blame] | 11585 | assert(isa<BlockPointerType>(E->getType())); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11586 | |
Sean Callanan | ce9c831 | 2012-03-06 21:34:12 +0000 | [diff] [blame] | 11587 | E->setType(DestType); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11588 | |
Sean Callanan | ce9c831 | 2012-03-06 21:34:12 +0000 | [diff] [blame] | 11589 | // The sub-expression has to be a lvalue reference, so rebuild it as such. |
| 11590 | DestType = S.Context.getLValueReferenceType(DestType); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11591 | |
Sean Callanan | ce9c831 | 2012-03-06 21:34:12 +0000 | [diff] [blame] | 11592 | ExprResult Result = Visit(E->getSubExpr()); |
| 11593 | if (!Result.isUsable()) return ExprError(); |
| 11594 | |
| 11595 | E->setSubExpr(Result.take()); |
| 11596 | return S.Owned(E); |
Sean Callanan | ba66c6c | 2012-03-06 23:12:57 +0000 | [diff] [blame] | 11597 | } else { |
Sean Callanan | ce9c831 | 2012-03-06 21:34:12 +0000 | [diff] [blame] | 11598 | llvm_unreachable("Unhandled cast type!"); |
| 11599 | } |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11600 | } |
| 11601 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11602 | ExprResult RebuildUnknownAnyExpr::resolveDecl(Expr *E, ValueDecl *VD) { |
| 11603 | ExprValueKind ValueKind = VK_LValue; |
| 11604 | QualType Type = DestType; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11605 | |
| 11606 | // We know how to make this work for certain kinds of decls: |
| 11607 | |
| 11608 | // - functions |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11609 | if (FunctionDecl *FD = dyn_cast<FunctionDecl>(VD)) { |
| 11610 | if (const PointerType *Ptr = Type->getAs<PointerType>()) { |
| 11611 | DestType = Ptr->getPointeeType(); |
| 11612 | ExprResult Result = resolveDecl(E, VD); |
| 11613 | if (Result.isInvalid()) return ExprError(); |
| 11614 | return S.ImpCastExprToType(Result.take(), Type, |
John McCall | a19950e | 2011-08-10 04:12:23 +0000 | [diff] [blame] | 11615 | CK_FunctionToPointerDecay, VK_RValue); |
| 11616 | } |
| 11617 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11618 | if (!Type->isFunctionType()) { |
| 11619 | S.Diag(E->getExprLoc(), diag::err_unknown_any_function) |
| 11620 | << VD << E->getSourceRange(); |
John McCall | a19950e | 2011-08-10 04:12:23 +0000 | [diff] [blame] | 11621 | return ExprError(); |
| 11622 | } |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11623 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11624 | if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(FD)) |
| 11625 | if (MD->isInstance()) { |
| 11626 | ValueKind = VK_RValue; |
| 11627 | Type = S.Context.BoundMemberTy; |
John McCall | f530751 | 2011-04-27 00:36:17 +0000 | [diff] [blame] | 11628 | } |
| 11629 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11630 | // Function references aren't l-values in C. |
David Blaikie | 4e4d084 | 2012-03-11 07:00:24 +0000 | [diff] [blame] | 11631 | if (!S.getLangOpts().CPlusPlus) |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11632 | ValueKind = VK_RValue; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11633 | |
| 11634 | // - variables |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11635 | } else if (isa<VarDecl>(VD)) { |
| 11636 | if (const ReferenceType *RefTy = Type->getAs<ReferenceType>()) { |
| 11637 | Type = RefTy->getPointeeType(); |
| 11638 | } else if (Type->isFunctionType()) { |
| 11639 | S.Diag(E->getExprLoc(), diag::err_unknown_any_var_function_type) |
| 11640 | << VD << E->getSourceRange(); |
John McCall | 755d849 | 2011-04-12 00:42:48 +0000 | [diff] [blame] | 11641 | return ExprError(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11642 | } |
| 11643 | |
| 11644 | // - nothing else |
| 11645 | } else { |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11646 | S.Diag(E->getExprLoc(), diag::err_unsupported_unknown_any_decl) |
| 11647 | << VD << E->getSourceRange(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11648 | return ExprError(); |
| 11649 | } |
| 11650 | |
Richard Trieu | 5e4c80b | 2011-09-09 03:59:41 +0000 | [diff] [blame] | 11651 | VD->setType(DestType); |
| 11652 | E->setType(Type); |
| 11653 | E->setValueKind(ValueKind); |
| 11654 | return S.Owned(E); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11655 | } |
| 11656 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11657 | /// Check a cast of an unknown-any type. We intentionally only |
| 11658 | /// trigger this for C-style casts. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11659 | ExprResult Sema::checkUnknownAnyCast(SourceRange TypeRange, QualType CastType, |
| 11660 | Expr *CastExpr, CastKind &CastKind, |
| 11661 | ExprValueKind &VK, CXXCastPath &Path) { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11662 | // Rewrite the casted expression from scratch. |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11663 | ExprResult result = RebuildUnknownAnyExpr(*this, CastType).Visit(CastExpr); |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 11664 | if (!result.isUsable()) return ExprError(); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11665 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11666 | CastExpr = result.take(); |
| 11667 | VK = CastExpr->getValueKind(); |
| 11668 | CastKind = CK_NoOp; |
John McCall | a5fc472 | 2011-04-09 22:50:59 +0000 | [diff] [blame] | 11669 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11670 | return CastExpr; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11671 | } |
| 11672 | |
Douglas Gregor | f1d1ca5 | 2011-12-01 01:37:36 +0000 | [diff] [blame] | 11673 | ExprResult Sema::forceUnknownAnyToType(Expr *E, QualType ToType) { |
| 11674 | return RebuildUnknownAnyExpr(*this, ToType).Visit(E); |
| 11675 | } |
| 11676 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11677 | static ExprResult diagnoseUnknownAnyExpr(Sema &S, Expr *E) { |
| 11678 | Expr *orig = E; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11679 | unsigned diagID = diag::err_uncasted_use_of_unknown_any; |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11680 | while (true) { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11681 | E = E->IgnoreParenImpCasts(); |
| 11682 | if (CallExpr *call = dyn_cast<CallExpr>(E)) { |
| 11683 | E = call->getCallee(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11684 | diagID = diag::err_uncasted_call_of_unknown_any; |
| 11685 | } else { |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11686 | break; |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11687 | } |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11688 | } |
| 11689 | |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11690 | SourceLocation loc; |
| 11691 | NamedDecl *d; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11692 | if (DeclRefExpr *ref = dyn_cast<DeclRefExpr>(E)) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11693 | loc = ref->getLocation(); |
| 11694 | d = ref->getDecl(); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11695 | } else if (MemberExpr *mem = dyn_cast<MemberExpr>(E)) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11696 | loc = mem->getMemberLoc(); |
| 11697 | d = mem->getMemberDecl(); |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11698 | } else if (ObjCMessageExpr *msg = dyn_cast<ObjCMessageExpr>(E)) { |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11699 | diagID = diag::err_uncasted_call_of_unknown_any; |
Argyrios Kyrtzidis | 2071808 | 2011-10-03 06:36:51 +0000 | [diff] [blame] | 11700 | loc = msg->getSelectorStartLoc(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11701 | d = msg->getMethodDecl(); |
John McCall | 819e745 | 2011-08-31 20:57:36 +0000 | [diff] [blame] | 11702 | if (!d) { |
| 11703 | S.Diag(loc, diag::err_uncasted_send_to_unknown_any_method) |
| 11704 | << static_cast<unsigned>(msg->isClassMessage()) << msg->getSelector() |
| 11705 | << orig->getSourceRange(); |
| 11706 | return ExprError(); |
| 11707 | } |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11708 | } else { |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11709 | S.Diag(E->getExprLoc(), diag::err_unsupported_unknown_any_expr) |
| 11710 | << E->getSourceRange(); |
John McCall | 379b515 | 2011-04-11 07:02:50 +0000 | [diff] [blame] | 11711 | return ExprError(); |
| 11712 | } |
| 11713 | |
| 11714 | S.Diag(loc, diagID) << d << orig->getSourceRange(); |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11715 | |
| 11716 | // Never recoverable. |
| 11717 | return ExprError(); |
| 11718 | } |
| 11719 | |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 11720 | /// Check for operands with placeholder types and complain if found. |
| 11721 | /// Returns true if there was an error and no recovery was possible. |
John McCall | fb8721c | 2011-04-10 19:13:55 +0000 | [diff] [blame] | 11722 | ExprResult Sema::CheckPlaceholderExpr(Expr *E) { |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11723 | const BuiltinType *placeholderType = E->getType()->getAsPlaceholderType(); |
| 11724 | if (!placeholderType) return Owned(E); |
| 11725 | |
| 11726 | switch (placeholderType->getKind()) { |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 11727 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11728 | // Overloaded expressions. |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11729 | case BuiltinType::Overload: { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 11730 | // Try to resolve a single function template specialization. |
| 11731 | // This is obligatory. |
| 11732 | ExprResult result = Owned(E); |
| 11733 | if (ResolveAndFixSingleFunctionTemplateSpecialization(result, false)) { |
| 11734 | return result; |
| 11735 | |
| 11736 | // If that failed, try to recover with a call. |
| 11737 | } else { |
| 11738 | tryToRecoverWithCall(result, PDiag(diag::err_ovl_unresolvable), |
| 11739 | /*complain*/ true); |
| 11740 | return result; |
| 11741 | } |
| 11742 | } |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11743 | |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11744 | // Bound member functions. |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11745 | case BuiltinType::BoundMember: { |
John McCall | 6dbba4f | 2011-10-11 23:14:30 +0000 | [diff] [blame] | 11746 | ExprResult result = Owned(E); |
| 11747 | tryToRecoverWithCall(result, PDiag(diag::err_bound_member_function), |
| 11748 | /*complain*/ true); |
| 11749 | return result; |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11750 | } |
| 11751 | |
| 11752 | // ARC unbridged casts. |
| 11753 | case BuiltinType::ARCUnbridgedCast: { |
| 11754 | Expr *realCast = stripARCUnbridgedCast(E); |
| 11755 | diagnoseARCUnbridgedCast(realCast); |
| 11756 | return Owned(realCast); |
| 11757 | } |
John McCall | 864c041 | 2011-04-26 20:42:42 +0000 | [diff] [blame] | 11758 | |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11759 | // Expressions of unknown type. |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11760 | case BuiltinType::UnknownAny: |
John McCall | 1de4d4e | 2011-04-07 08:22:57 +0000 | [diff] [blame] | 11761 | return diagnoseUnknownAnyExpr(*this, E); |
| 11762 | |
John McCall | 3c3b7f9 | 2011-10-25 17:37:35 +0000 | [diff] [blame] | 11763 | // Pseudo-objects. |
| 11764 | case BuiltinType::PseudoObject: |
| 11765 | return checkPseudoObjectRValue(E); |
| 11766 | |
Eli Friedman | a6c66ce | 2012-08-31 00:14:07 +0000 | [diff] [blame^] | 11767 | case BuiltinType::BuiltinFn: |
| 11768 | Diag(E->getLocStart(), diag::err_builtin_fn_use); |
| 11769 | return ExprError(); |
| 11770 | |
John McCall | e0a22d0 | 2011-10-18 21:02:43 +0000 | [diff] [blame] | 11771 | // Everything else should be impossible. |
| 11772 | #define BUILTIN_TYPE(Id, SingletonId) \ |
| 11773 | case BuiltinType::Id: |
| 11774 | #define PLACEHOLDER_TYPE(Id, SingletonId) |
| 11775 | #include "clang/AST/BuiltinTypes.def" |
John McCall | 5acb0c9 | 2011-10-17 18:40:02 +0000 | [diff] [blame] | 11776 | break; |
| 11777 | } |
| 11778 | |
| 11779 | llvm_unreachable("invalid placeholder type!"); |
John McCall | 2a984ca | 2010-10-12 00:20:44 +0000 | [diff] [blame] | 11780 | } |
Richard Trieu | bb9b80c | 2011-04-21 21:44:26 +0000 | [diff] [blame] | 11781 | |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11782 | bool Sema::CheckCaseExpression(Expr *E) { |
| 11783 | if (E->isTypeDependent()) |
Richard Trieu | bb9b80c | 2011-04-21 21:44:26 +0000 | [diff] [blame] | 11784 | return true; |
Richard Trieu | ccd891a | 2011-09-09 01:45:06 +0000 | [diff] [blame] | 11785 | if (E->isValueDependent() || E->isIntegerConstantExpr(Context)) |
| 11786 | return E->getType()->isIntegralOrEnumerationType(); |
Richard Trieu | bb9b80c | 2011-04-21 21:44:26 +0000 | [diff] [blame] | 11787 | return false; |
| 11788 | } |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 11789 | |
| 11790 | /// ActOnObjCBoolLiteral - Parse {__objc_yes,__objc_no} literals. |
| 11791 | ExprResult |
| 11792 | Sema::ActOnObjCBoolLiteral(SourceLocation OpLoc, tok::TokenKind Kind) { |
| 11793 | assert((Kind == tok::kw___objc_yes || Kind == tok::kw___objc_no) && |
| 11794 | "Unknown Objective-C Boolean value!"); |
Fariborz Jahanian | 9617130 | 2012-08-30 18:49:41 +0000 | [diff] [blame] | 11795 | QualType BoolT = Context.ObjCBuiltinBoolTy; |
| 11796 | if (!Context.getBOOLDecl()) { |
| 11797 | LookupResult Result(*this, &Context.Idents.get("BOOL"), SourceLocation(), |
| 11798 | Sema::LookupOrdinaryName); |
| 11799 | if (LookupName(Result, getCurScope())) { |
| 11800 | NamedDecl *ND = Result.getFoundDecl(); |
| 11801 | if (TypedefDecl *TD = dyn_cast<TypedefDecl>(ND)) |
| 11802 | Context.setBOOLDecl(TD); |
| 11803 | } |
| 11804 | } |
| 11805 | if (Context.getBOOLDecl()) |
| 11806 | BoolT = Context.getBOOLType(); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 11807 | return Owned(new (Context) ObjCBoolLiteralExpr(Kind == tok::kw___objc_yes, |
Fariborz Jahanian | 9617130 | 2012-08-30 18:49:41 +0000 | [diff] [blame] | 11808 | BoolT, OpLoc)); |
Ted Kremenek | ebcb57a | 2012-03-06 20:05:56 +0000 | [diff] [blame] | 11809 | } |